script.c 332 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_FUNCIN
  4. //#define DEBUG_DISP
  5. //#define DEBUG_DISASM
  6. //#define DEBUG_RUN
  7. //#define DEBUG_HASH
  8. #include "../common/cbasetypes.h"
  9. #include "../common/timer.h"
  10. #include "../common/malloc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/utils.h"
  16. #include "map.h"
  17. #include "clif.h"
  18. #include "chrif.h"
  19. #include "itemdb.h"
  20. #include "pc.h"
  21. #include "status.h"
  22. #include "storage.h"
  23. #include "mob.h"
  24. #include "npc.h"
  25. #include "pet.h"
  26. #include "mercenary.h" //[orn]
  27. #include "intif.h"
  28. #include "skill.h"
  29. #include "chat.h"
  30. #include "battle.h"
  31. #include "party.h"
  32. #include "guild.h"
  33. #include "atcommand.h"
  34. #include "charcommand.h"
  35. #include "log.h"
  36. #include "unit.h"
  37. #include "irc.h"
  38. #include "pet.h"
  39. #include "mail.h"
  40. #include "script.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <math.h>
  45. #ifndef WIN32
  46. #include <sys/time.h>
  47. #endif
  48. #include <time.h>
  49. #include <setjmp.h>
  50. #include <errno.h>
  51. ///////////////////////////////////////////////////////////////////////////////
  52. //## TODO possible enhancements: [FlavioJS]
  53. // - 'callfunc' supporting labels in the current npc "::LabelName"
  54. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  55. // - 'function FuncName;' function declarations reverting to global functions
  56. // if local label isn't found
  57. // - join callfunc and callsub's functionality
  58. //
  59. // struct script_state* st;
  60. //
  61. /// Returns the script_data at the target index
  62. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  63. /// Returns if the stack contains data at the target index
  64. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  65. /// Returns the index of the last data in the stack
  66. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  67. /// Pushes an int into the stack
  68. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  69. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  70. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  71. /// Pushes a nil into the stack
  72. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  73. /// Pushes a copy of the data in the target index
  74. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  75. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  76. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  77. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  78. // Note: "top" functions/defines use indexes relative to the top of the stack
  79. // -1 is the index of the data at the top
  80. /// Returns the script_data at the target index relative to the top of the stack
  81. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  82. /// Pushes a copy of the data in the target index relative to the top of the stack
  83. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  84. /// Removes the range of values [start,end[ relative to the top of the stack
  85. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  86. //
  87. // struct script_data* data;
  88. //
  89. /// Returns if the script data is a string
  90. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  91. /// Returns if the script data is an int
  92. #define data_isint(data) ( (data)->type == C_INT )
  93. /// Returns if the script data is a reference
  94. #define data_isreference(data) ( (data)->type == C_NAME )
  95. /// Returns if the script data is a label
  96. #define data_islabel(data) ( (data)->type == C_POS )
  97. /// Returns if the script data is an internal script function label
  98. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  99. /// Returns if this is a reference to a constant
  100. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  101. /// Returns if this a reference to a param
  102. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  103. /// Returns if this a reference to a variable
  104. //##TODO confirm it's C_NAME [FlavioJS]
  105. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  106. /// Returns the unique id of the reference (id and index)
  107. #define reference_getuid(data) ( (data)->u.num )
  108. /// Returns the id of the reference
  109. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  110. /// Returns the array index of the reference
  111. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  112. /// Returns the name of the reference
  113. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  114. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  115. #define reference_getref(data) ( (data)->ref )
  116. /// Returns the value of the constant
  117. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  118. /// Returns the type of param
  119. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  120. /// Composes the uid of a reference from the id and the index
  121. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  122. #define FETCH(n, t) \
  123. if( script_hasdata(st,n) ) \
  124. (t)=script_getnum(st,n);
  125. #define SCRIPT_BLOCK_SIZE 512
  126. enum { LABEL_NEXTLINE=1,LABEL_START };
  127. static unsigned char * script_buf = NULL;
  128. static int script_pos,script_size;
  129. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  130. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  131. #define GETSTRING(off) (str_buf+(off))
  132. static char *str_buf;
  133. static int str_pos,str_size;
  134. static struct str_data_struct {
  135. int type;
  136. int str;
  137. int backpatch;
  138. int label;
  139. int (*func)(struct script_state *st);
  140. int val;
  141. int next;
  142. } *str_data = NULL;
  143. int str_num=LABEL_START,str_data_size;
  144. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  145. #define SCRIPT_HASH_SIZE 1021
  146. int str_hash[SCRIPT_HASH_SIZE];
  147. //#define SCRIPT_HASH_DJB2
  148. #define SCRIPT_HASH_SDBM
  149. //#define SCRIPT_HASH_ELF
  150. //#define SCRIPT_HASH_PJW
  151. static struct dbt *mapreg_db=NULL;
  152. static struct dbt *mapregstr_db=NULL;
  153. static int mapreg_dirty=-1;
  154. char mapreg_txt[256]="save/mapreg.txt";
  155. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  156. static struct dbt *scriptlabel_db=NULL;
  157. static struct dbt *userfunc_db=NULL;
  158. static int parse_options=0;
  159. struct dbt* script_get_label_db(){ return scriptlabel_db; }
  160. struct dbt* script_get_userfunc_db(){ return userfunc_db; }
  161. struct Script_Config script_config;
  162. static jmp_buf error_jump;
  163. static char* error_msg;
  164. static const char* error_pos;
  165. static int error_report; // if the error should produce output
  166. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  167. // [Eoe / jA 1080, 1081, 1094, 1164]
  168. enum curly_type {
  169. TYPE_NULL = 0,
  170. TYPE_IF,
  171. TYPE_SWITCH,
  172. TYPE_WHILE,
  173. TYPE_FOR,
  174. TYPE_DO,
  175. TYPE_USERFUNC,
  176. TYPE_ARGLIST // function argument list
  177. };
  178. #define ARGLIST_UNDEFINED 0
  179. #define ARGLIST_NO_PAREN 1
  180. #define ARGLIST_PAREN 2
  181. static struct {
  182. struct {
  183. int type;
  184. int index;
  185. int count;
  186. int flag;
  187. struct linkdb_node *case_label;
  188. } curly[256]; // 右カッコの情報
  189. int curly_count; // 右カッコの数
  190. int index; // スクリプト内で使用した構文の数
  191. } syntax;
  192. const char* parse_curly_close(const char* p);
  193. const char* parse_syntax_close(const char* p);
  194. const char* parse_syntax_close_sub(const char* p,int* flag);
  195. const char* parse_syntax(const char* p);
  196. static int parse_syntax_for_flag = 0;
  197. extern int current_equip_item_index; //for New CARS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  198. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  199. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  200. int potion_target=0;
  201. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  202. char mapregsql_db[32] = "mapreg";
  203. char mapregsql_db_varname[32] = "varname";
  204. char mapregsql_db_index[32] = "index";
  205. char mapregsql_db_value[32] = "value";
  206. #endif
  207. int get_com(unsigned char *script,int *pos);
  208. int get_num(unsigned char *script,int *pos);
  209. extern struct script_function {
  210. int (*func)(struct script_state *st);
  211. const char *name;
  212. const char *arg;
  213. } buildin_func[];
  214. static struct linkdb_node *sleep_db;
  215. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  216. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  217. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  218. /*==========================================
  219. * ローカルプロトタイプ宣言 (必要な物のみ)
  220. *------------------------------------------*/
  221. const char* parse_subexpr(const char* p,int limit);
  222. void push_val(struct script_stack *stack,int type,int val);
  223. int run_func(struct script_state *st);
  224. int mapreg_setreg(int num,int val);
  225. int mapreg_setregstr(int num,const char *str);
  226. enum c_op {
  227. C_NOP, // end of script/no value (nil)
  228. C_POS,
  229. C_INT, // number
  230. C_PARAM, // parameter variable (see pc_readparam/pc_setparam)
  231. C_FUNC, // buildin function call
  232. C_STR, // string (free'd automatically)
  233. C_CONSTSTR, // string (not free'd)
  234. C_ARG, // start of argument list
  235. C_NAME,
  236. C_EOL, // end of line (extra stack values are cleared)
  237. C_RETINFO,
  238. C_USERFUNC, // internal script function
  239. C_USERFUNC_POS, // internal script function label
  240. // operators
  241. C_OP3, // a ? b : c
  242. C_LOR, // a || b
  243. C_LAND, // a && b
  244. C_LE, // a <= b
  245. C_LT, // a < b
  246. C_GE, // a >= b
  247. C_GT, // a > b
  248. C_EQ, // a == b
  249. C_NE, // a != b
  250. C_XOR, // a ^ b
  251. C_OR, // a | b
  252. C_AND, // a & b
  253. C_ADD, // a + b
  254. C_SUB, // a - b
  255. C_MUL, // a * b
  256. C_DIV, // a / b
  257. C_MOD, // a % b
  258. C_NEG, // - a
  259. C_LNOT, // ! a
  260. C_NOT, // ~ a
  261. C_R_SHIFT, // a >> b
  262. C_L_SHIFT // a << b
  263. };
  264. enum {
  265. MF_NOMEMO, //0
  266. MF_NOTELEPORT,
  267. MF_NOSAVE,
  268. MF_NOBRANCH,
  269. MF_NOPENALTY,
  270. MF_NOZENYPENALTY,
  271. MF_PVP,
  272. MF_PVP_NOPARTY,
  273. MF_PVP_NOGUILD,
  274. MF_GVG,
  275. MF_GVG_NOPARTY, //10
  276. MF_NOTRADE,
  277. MF_NOSKILL,
  278. MF_NOWARP,
  279. MF_PARTYLOCK,
  280. MF_NOICEWALL,
  281. MF_SNOW,
  282. MF_FOG,
  283. MF_SAKURA,
  284. MF_LEAVES,
  285. MF_RAIN, //20
  286. MF_INDOORS,
  287. MF_NOGO,
  288. MF_CLOUDS,
  289. MF_CLOUDS2,
  290. MF_FIREWORKS,
  291. MF_GVG_CASTLE,
  292. MF_GVG_DUNGEON,
  293. MF_NIGHTENABLED,
  294. MF_NOBASEEXP,
  295. MF_NOJOBEXP, //30
  296. MF_NOMOBLOOT,
  297. MF_NOMVPLOOT,
  298. MF_NORETURN,
  299. MF_NOWARPTO,
  300. MF_NIGHTMAREDROP,
  301. MF_RESTRICTED,
  302. MF_NOCOMMAND,
  303. MF_NODROP,
  304. MF_JEXP,
  305. MF_BEXP, //40
  306. MF_NOVENDING,
  307. MF_LOADEVENT,
  308. MF_NOCHAT,
  309. MF_NOEXPPENALTY,
  310. MF_GUILDLOCK
  311. };
  312. const char* script_op2name(int op)
  313. {
  314. #define RETURN_OP_NAME(type) case type: return #type
  315. switch( op )
  316. {
  317. RETURN_OP_NAME(C_NOP);
  318. RETURN_OP_NAME(C_POS);
  319. RETURN_OP_NAME(C_INT);
  320. RETURN_OP_NAME(C_PARAM);
  321. RETURN_OP_NAME(C_FUNC);
  322. RETURN_OP_NAME(C_STR);
  323. RETURN_OP_NAME(C_CONSTSTR);
  324. RETURN_OP_NAME(C_ARG);
  325. RETURN_OP_NAME(C_NAME);
  326. RETURN_OP_NAME(C_EOL);
  327. RETURN_OP_NAME(C_RETINFO);
  328. RETURN_OP_NAME(C_USERFUNC);
  329. RETURN_OP_NAME(C_USERFUNC_POS);
  330. // operators
  331. RETURN_OP_NAME(C_OP3);
  332. RETURN_OP_NAME(C_LOR);
  333. RETURN_OP_NAME(C_LAND);
  334. RETURN_OP_NAME(C_LE);
  335. RETURN_OP_NAME(C_LT);
  336. RETURN_OP_NAME(C_GE);
  337. RETURN_OP_NAME(C_GT);
  338. RETURN_OP_NAME(C_EQ);
  339. RETURN_OP_NAME(C_NE);
  340. RETURN_OP_NAME(C_XOR);
  341. RETURN_OP_NAME(C_OR);
  342. RETURN_OP_NAME(C_AND);
  343. RETURN_OP_NAME(C_ADD);
  344. RETURN_OP_NAME(C_SUB);
  345. RETURN_OP_NAME(C_MUL);
  346. RETURN_OP_NAME(C_DIV);
  347. RETURN_OP_NAME(C_MOD);
  348. RETURN_OP_NAME(C_NEG);
  349. RETURN_OP_NAME(C_LNOT);
  350. RETURN_OP_NAME(C_NOT);
  351. RETURN_OP_NAME(C_R_SHIFT);
  352. RETURN_OP_NAME(C_L_SHIFT);
  353. default:
  354. ShowDebug("script_op2name: unexpected op=%d\n", op);
  355. return "???";
  356. }
  357. #undef RETURN_OP_NAME
  358. }
  359. /// Reports on the console the src of a script error.
  360. static void script_reportsrc(struct script_state *st)
  361. {
  362. struct block_list* bl;
  363. if( st->oid == 0 )
  364. return; //Can't report source.
  365. bl = map_id2bl(st->oid);
  366. if( bl == NULL )
  367. return;
  368. switch( bl->type )
  369. {
  370. case BL_NPC:
  371. if( bl->m >= 0 )
  372. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  373. else
  374. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  375. break;
  376. default:
  377. if( bl->m >= 0 )
  378. 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);
  379. else
  380. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  381. break;
  382. }
  383. }
  384. /// Reports on the console information about the script data.
  385. static void script_reportdata(struct script_data* data)
  386. {
  387. if( data == NULL )
  388. return;
  389. switch( data->type )
  390. {
  391. case C_NOP:// no value
  392. ShowDebug("Data: nothing (nil)\n");
  393. break;
  394. case C_INT:// number
  395. ShowDebug("Data: number value=%d\n", data->u.num);
  396. break;
  397. case C_STR:
  398. case C_CONSTSTR:// string
  399. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  400. break;
  401. case C_NAME:// reference
  402. if( reference_tovariable(data) )
  403. {// variable
  404. char* name = reference_getname(data);
  405. if( not_array_variable(*name) )
  406. ShowDebug("Data: variable name='%s'\n", name);
  407. else
  408. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  409. }
  410. else if( reference_toconstant(data) )
  411. {// constant
  412. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  413. }
  414. else if( reference_toparam(data) )
  415. {// param
  416. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  417. }
  418. else
  419. {// ???
  420. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  421. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  422. }
  423. break;
  424. case C_POS:// label
  425. ShowDebug("Data: label pos=%d\n", data->u.num);
  426. break;
  427. default:
  428. ShowDebug("Data: %s\n", script_op2name(data->type));
  429. break;
  430. }
  431. }
  432. /*==========================================
  433. * エラーメッセージ出力
  434. *------------------------------------------*/
  435. static void disp_error_message2(const char *mes,const char *pos,int report)
  436. {
  437. error_msg = aStrdup(mes);
  438. error_pos = pos;
  439. error_report = report;
  440. longjmp( error_jump, 1 );
  441. }
  442. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  443. /// Checks event parameter validity
  444. static void check_event(struct script_state *st, const char *evt)
  445. {
  446. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  447. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  448. script_reportsrc(st);
  449. }
  450. }
  451. /*==========================================
  452. * 文字列のハッシュを計算
  453. *------------------------------------------*/
  454. #define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE)
  455. static unsigned int calc_hash2(const char* p)
  456. {
  457. #if defined(SCRIPT_HASH_DJB2)
  458. unsigned int h = 5381;
  459. while( *p ) // hash*33 + c
  460. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  461. return h;
  462. #elif defined(SCRIPT_HASH_SDBM)
  463. unsigned int h = 0;
  464. while( *p )
  465. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  466. return h;
  467. #elif defined(SCRIPT_HASH_ELF)
  468. unsigned int h = 0;
  469. unsigned int g;
  470. while( *p ){ // UNIX ELF hash
  471. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  472. g = h & 0xF0000000;
  473. if( g )
  474. h ^= g >> 24;
  475. h &= ~g;
  476. }
  477. return h;
  478. #elif defined(SCRIPT_HASH_PJW)
  479. unsigned int h = 0;
  480. unsigned int g;
  481. while( *p ){
  482. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  483. g = h & 0xF0000000;
  484. if( g )
  485. {
  486. h ^= g>>24;
  487. h ^= g;
  488. }
  489. }
  490. return h;
  491. #else
  492. unsigned int h = 0;
  493. while( *p ){
  494. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  495. h += (unsigned char)TOLOWER(*p++);
  496. }
  497. return h;
  498. #endif
  499. }
  500. /*==========================================
  501. * str_dataの中に名前があるか検索する
  502. *------------------------------------------*/
  503. // 既存のであれば番号、無ければ-1
  504. static int search_str(const char *p)
  505. {
  506. int i;
  507. i=str_hash[calc_hash(p)];
  508. while(i){
  509. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  510. return i;
  511. }
  512. i=str_data[i].next;
  513. }
  514. return -1;
  515. }
  516. /*==========================================
  517. * str_dataに名前を登録
  518. *------------------------------------------*/
  519. // 既存のであれば番号、無ければ登録して新規番号
  520. int add_str(const char* p)
  521. {
  522. int i;
  523. int len;
  524. i=calc_hash(p);
  525. if(str_hash[i]==0){
  526. str_hash[i]=str_num;
  527. } else {
  528. i=str_hash[i];
  529. for(;;){
  530. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  531. return i;
  532. }
  533. if(str_data[i].next==0)
  534. break;
  535. i=str_data[i].next;
  536. }
  537. str_data[i].next=str_num;
  538. }
  539. if(str_num>=str_data_size){
  540. str_data_size+=128;
  541. RECREATE(str_data,struct str_data_struct,str_data_size);
  542. memset(str_data + (str_data_size - 128), '\0', 128);
  543. }
  544. len=(int)strlen(p);
  545. while(str_pos+len+1>=str_size){
  546. str_size+=256;
  547. RECREATE(str_buf,char,str_size);
  548. memset(str_buf + (str_size - 256), '\0', 256);
  549. }
  550. memcpy(str_buf+str_pos,p,len+1);
  551. str_data[str_num].type=C_NOP;
  552. str_data[str_num].str=str_pos;
  553. str_data[str_num].next=0;
  554. str_data[str_num].func=NULL;
  555. str_data[str_num].backpatch=-1;
  556. str_data[str_num].label=-1;
  557. str_pos+=len+1;
  558. return str_num++;
  559. }
  560. /*==========================================
  561. * スクリプトバッファサイズの確認と拡張
  562. *------------------------------------------*/
  563. static void expand_script_buf(void)
  564. {
  565. script_size+=SCRIPT_BLOCK_SIZE;
  566. RECREATE(script_buf,unsigned char,script_size);
  567. }
  568. /*==========================================
  569. * スクリプトバッファに1バイト書き込む
  570. *------------------------------------------*/
  571. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  572. #if 0
  573. static void add_scriptb(int a)
  574. {
  575. expand_script_buf();
  576. script_buf[script_pos++]=a;
  577. }
  578. #endif
  579. /*==========================================
  580. * スクリプトバッファにデータタイプを書き込む
  581. *------------------------------------------*/
  582. static void add_scriptc(int a)
  583. {
  584. while(a>=0x40){
  585. add_scriptb((a&0x3f)|0x40);
  586. a=(a-0x40)>>6;
  587. }
  588. add_scriptb(a&0x3f);
  589. }
  590. /*==========================================
  591. * スクリプトバッファに整数を書き込む
  592. *------------------------------------------*/
  593. static void add_scripti(int a)
  594. {
  595. while(a>=0x40){
  596. add_scriptb(a|0xc0);
  597. a=(a-0x40)>>6;
  598. }
  599. add_scriptb(a|0x80);
  600. }
  601. /*==========================================
  602. * スクリプトバッファにラベル/変数/関数を書き込む
  603. *------------------------------------------*/
  604. // 最大16Mまで
  605. static void add_scriptl(int l)
  606. {
  607. int backpatch = str_data[l].backpatch;
  608. switch(str_data[l].type){
  609. case C_POS:
  610. case C_USERFUNC_POS:
  611. add_scriptc(C_POS);
  612. add_scriptb(str_data[l].label);
  613. add_scriptb(str_data[l].label>>8);
  614. add_scriptb(str_data[l].label>>16);
  615. break;
  616. case C_NOP:
  617. case C_USERFUNC:
  618. // ラベルの可能性があるのでbackpatch用データ埋め込み
  619. add_scriptc(C_NAME);
  620. str_data[l].backpatch=script_pos;
  621. add_scriptb(backpatch);
  622. add_scriptb(backpatch>>8);
  623. add_scriptb(backpatch>>16);
  624. break;
  625. case C_INT:
  626. add_scripti(abs(str_data[l].val));
  627. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  628. add_scriptc(C_NEG);
  629. break;
  630. default:
  631. // もう他の用途と確定してるので数字をそのまま
  632. add_scriptc(C_NAME);
  633. add_scriptb(l);
  634. add_scriptb(l>>8);
  635. add_scriptb(l>>16);
  636. break;
  637. }
  638. }
  639. /*==========================================
  640. * ラベルを解決する
  641. *------------------------------------------*/
  642. void set_label(int l,int pos, const char* script_pos)
  643. {
  644. int i,next;
  645. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  646. { //Prevent overwriting constants values and parameters [Skotlex]
  647. disp_error_message("set_label: invalid label name",script_pos);
  648. return;
  649. }
  650. if(str_data[l].label!=-1){
  651. disp_error_message("set_label: dup label ",script_pos);
  652. return;
  653. }
  654. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  655. str_data[l].label=pos;
  656. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  657. next=GETVALUE(script_buf,i);
  658. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  659. SETVALUE(script_buf,i,pos);
  660. i=next;
  661. }
  662. }
  663. /// Skips spaces and/or comments.
  664. const char* skip_space(const char* p)
  665. {
  666. if( p == NULL )
  667. return NULL;
  668. for(;;)
  669. {
  670. while( ISSPACE(*p) )
  671. ++p;
  672. if( *p == '/' && p[1] == '/' )
  673. {// line comment
  674. while(*p && *p!='\n')
  675. ++p;
  676. }
  677. else if( *p == '/' && p[1] == '*' )
  678. {// block comment
  679. p += 2;
  680. for(;;)
  681. {
  682. if( *p == '\0' )
  683. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  684. if( *p == '*' && p[1] == '/' )
  685. {// end of block comment
  686. p += 2;
  687. break;
  688. }
  689. ++p;
  690. }
  691. }
  692. else
  693. break;
  694. }
  695. return p;
  696. }
  697. /// Skips a word.
  698. /// A word consists of undercores and/or alfanumeric characters,
  699. /// and valid variable prefixes/postfixes.
  700. static
  701. const char* skip_word(const char* p)
  702. {
  703. // prefix
  704. switch( *p )
  705. {
  706. case '@':// temporary char variable
  707. ++p; break;
  708. case '#':// account variable
  709. p += ( p[1] == '#' ? 2 : 1 ); break;
  710. case '.':// npc variable
  711. p += ( p[1] == '@' ? 2 : 1 ); break;
  712. case '$':// global variable
  713. p += ( p[1] == '@' ? 2 : 1 ); break;
  714. }
  715. while( ISALNUM(*p) || *p == '_' )
  716. ++p;
  717. // postfix
  718. if( *p == '$' )// string
  719. p++;
  720. return p;
  721. }
  722. /// Adds a word to str_data.
  723. /// @see skip_word
  724. /// @see add_str
  725. static
  726. int add_word(const char* p)
  727. {
  728. char* word;
  729. int len;
  730. int i;
  731. // Check for a word
  732. len = skip_word(p) - p;
  733. if( len == 0 )
  734. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  735. // Duplicate the word
  736. CREATE(word, char, len+1);
  737. memcpy(word, p, len);
  738. word[len] = 0;
  739. // add the word
  740. i = add_str(word);
  741. aFree(word);
  742. return i;
  743. }
  744. /// Parses a function call.
  745. /// The argument list can have parenthesis or not.
  746. /// The number of arguments is checked.
  747. static
  748. const char* parse_callfunc(const char* p, int require_paren)
  749. {
  750. const char* p2;
  751. const char* arg=NULL;
  752. int func;
  753. func = add_word(p);
  754. if( str_data[func].type == C_FUNC ){
  755. // buildin function
  756. add_scriptl(func);
  757. add_scriptc(C_ARG);
  758. arg = buildin_func[str_data[func].val].arg;
  759. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  760. // script defined function
  761. int callsub = search_str("callsub");
  762. add_scriptl(callsub);
  763. add_scriptc(C_ARG);
  764. add_scriptl(func);
  765. arg = buildin_func[str_data[callsub].val].arg;
  766. if( *arg == 0 )
  767. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  768. if( *arg != '*' )
  769. ++arg; // count func as argument
  770. } else
  771. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  772. p = skip_word(p);
  773. p = skip_space(p);
  774. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  775. syntax.curly[syntax.curly_count].count = 0;
  776. if( *p == ';' )
  777. {// <func name> ';'
  778. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  779. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  780. {// <func name> '(' ')'
  781. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  782. p = p2;
  783. /*
  784. } else if( 0 && require_paren && *p != '(' )
  785. {// <func name>
  786. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  787. */
  788. } else
  789. {// <func name> <arg list>
  790. if( require_paren ){
  791. if( *p != '(' )
  792. disp_error_message("need '('",p);
  793. ++p; // skip '('
  794. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  795. } else if( *p == '(' ){
  796. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  797. } else {
  798. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  799. }
  800. ++syntax.curly_count;
  801. while( *arg ) {
  802. p2=parse_subexpr(p,-1);
  803. if( p == p2 )
  804. break; // not an argument
  805. if( *arg != '*' )
  806. ++arg; // next argument
  807. p=skip_space(p2);
  808. if( *arg == 0 || *p != ',' )
  809. break; // no more arguments
  810. ++p; // skip comma
  811. }
  812. --syntax.curly_count;
  813. }
  814. if( *arg && *arg != '?' && *arg != '*' )
  815. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  816. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  817. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  818. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  819. if( *p != ')' )
  820. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  821. ++p;
  822. }
  823. add_scriptc(C_FUNC);
  824. return p;
  825. }
  826. /*==========================================
  827. * 項の解析
  828. *------------------------------------------*/
  829. const char* parse_simpleexpr(const char *p)
  830. {
  831. int i;
  832. p=skip_space(p);
  833. #ifdef DEBUG_FUNCIN
  834. if(battle_config.etc_log)
  835. ShowDebug("parse_simpleexpr %s\n",p);
  836. #endif
  837. if(*p==';' || *p==',')
  838. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  839. if(*p=='('){
  840. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  841. ++syntax.curly[i].count;
  842. p=parse_subexpr(p+1,-1);
  843. p=skip_space(p);
  844. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  845. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  846. ){
  847. if( *p == ',' ){
  848. syntax.curly[i].flag = ARGLIST_PAREN;
  849. return p;
  850. } else
  851. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  852. }
  853. if( *p != ')' )
  854. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  855. ++p;
  856. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  857. char *np;
  858. i=strtoul(p,&np,0);
  859. add_scripti(i);
  860. p=np;
  861. } else if(*p=='"'){
  862. add_scriptc(C_STR);
  863. p++;
  864. while( *p && *p != '"' ){
  865. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  866. p++;
  867. else if( *p == '\n' )
  868. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  869. add_scriptb(*p++);
  870. }
  871. if(!*p)
  872. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  873. add_scriptb(0);
  874. p++; //'"'
  875. } else {
  876. int l;
  877. // label , register , function etc
  878. if(skip_word(p)==p)
  879. disp_error_message("parse_simpleexpr: unexpected character",p);
  880. l=add_word(p);
  881. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  882. return parse_callfunc(p,1);
  883. p=skip_word(p);
  884. if( *p == '[' ){
  885. // array(name[i] => getelementofarray(name,i) )
  886. add_scriptl(search_str("getelementofarray"));
  887. add_scriptc(C_ARG);
  888. add_scriptl(l);
  889. p=parse_subexpr(p+1,-1);
  890. p=skip_space(p);
  891. if( *p != ']' )
  892. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  893. ++p;
  894. add_scriptc(C_FUNC);
  895. }else
  896. add_scriptl(l);
  897. }
  898. #ifdef DEBUG_FUNCIN
  899. if(battle_config.etc_log)
  900. ShowDebug("parse_simpleexpr end %s\n",p);
  901. #endif
  902. return p;
  903. }
  904. /*==========================================
  905. * 式の解析
  906. *------------------------------------------*/
  907. const char* parse_subexpr(const char* p,int limit)
  908. {
  909. int op,opl,len;
  910. const char* tmpp;
  911. #ifdef DEBUG_FUNCIN
  912. if(battle_config.etc_log)
  913. ShowDebug("parse_subexpr %s\n",p);
  914. #endif
  915. p=skip_space(p);
  916. if(*p=='-'){
  917. tmpp=skip_space(p+1);
  918. if(*tmpp==';' || *tmpp==','){
  919. add_scriptl(LABEL_NEXTLINE);
  920. p++;
  921. return p;
  922. }
  923. }
  924. tmpp=p;
  925. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  926. p=parse_subexpr(p+1,10);
  927. add_scriptc(op);
  928. } else
  929. p=parse_simpleexpr(p);
  930. p=skip_space(p);
  931. while((
  932. (op=C_OP3,opl=0,len=1,*p=='?') ||
  933. (op=C_ADD,opl=8,len=1,*p=='+') ||
  934. (op=C_SUB,opl=8,len=1,*p=='-') ||
  935. (op=C_MUL,opl=9,len=1,*p=='*') ||
  936. (op=C_DIV,opl=9,len=1,*p=='/') ||
  937. (op=C_MOD,opl=9,len=1,*p=='%') ||
  938. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  939. (op=C_AND,opl=6,len=1,*p=='&') ||
  940. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  941. (op=C_OR,opl=5,len=1,*p=='|') ||
  942. (op=C_XOR,opl=4,len=1,*p=='^') ||
  943. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  944. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  945. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  946. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  947. (op=C_GT,opl=3,len=1,*p=='>') ||
  948. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  949. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  950. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  951. p+=len;
  952. if(op == C_OP3) {
  953. p=parse_subexpr(p,-1);
  954. p=skip_space(p);
  955. if( *(p++) != ':')
  956. disp_error_message("parse_subexpr: need ':'", p-1);
  957. p=parse_subexpr(p,-1);
  958. } else {
  959. p=parse_subexpr(p,opl);
  960. }
  961. add_scriptc(op);
  962. p=skip_space(p);
  963. }
  964. #ifdef DEBUG_FUNCIN
  965. if(battle_config.etc_log)
  966. ShowDebug("parse_subexpr end %s\n",p);
  967. #endif
  968. return p; /* return first untreated operator */
  969. }
  970. /*==========================================
  971. * 式の評価
  972. *------------------------------------------*/
  973. const char* parse_expr(const char *p)
  974. {
  975. #ifdef DEBUG_FUNCIN
  976. if(battle_config.etc_log)
  977. ShowDebug("parse_expr %s\n",p);
  978. #endif
  979. switch(*p){
  980. case ')': case ';': case ':': case '[': case ']':
  981. case '}':
  982. disp_error_message("parse_expr: unexpected char",p);
  983. }
  984. p=parse_subexpr(p,-1);
  985. #ifdef DEBUG_FUNCIN
  986. if(battle_config.etc_log)
  987. ShowDebug("parse_expr end %s\n",p);
  988. #endif
  989. return p;
  990. }
  991. /*==========================================
  992. * 行の解析
  993. *------------------------------------------*/
  994. const char* parse_line(const char* p)
  995. {
  996. const char* p2;
  997. p=skip_space(p);
  998. if(*p==';') {
  999. // if(); for(); while(); のために閉じ判定
  1000. p = parse_syntax_close(p);
  1001. return p+1;
  1002. }
  1003. if(*p==')' && parse_syntax_for_flag)
  1004. return p+1;
  1005. p = skip_space(p);
  1006. if(p[0] == '{') {
  1007. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1008. syntax.curly[syntax.curly_count].count = -1;
  1009. syntax.curly[syntax.curly_count].index = -1;
  1010. syntax.curly_count++;
  1011. return p + 1;
  1012. } else if(p[0] == '}') {
  1013. return parse_curly_close(p);
  1014. }
  1015. // 構文関連の処理
  1016. p2 = parse_syntax(p);
  1017. if(p2 != NULL)
  1018. return p2;
  1019. p = parse_callfunc(p,0);
  1020. p = skip_space(p);
  1021. if(parse_syntax_for_flag) {
  1022. if( *p != ')' )
  1023. disp_error_message("parse_line: need ')'",p);
  1024. } else {
  1025. if( *p != ';' )
  1026. disp_error_message("parse_line: need ';'",p);
  1027. }
  1028. // if, for , while の閉じ判定
  1029. p = parse_syntax_close(p+1);
  1030. return p;
  1031. }
  1032. // { ... } の閉じ処理
  1033. const char* parse_curly_close(const char* p)
  1034. {
  1035. if(syntax.curly_count <= 0) {
  1036. disp_error_message("parse_curly_close: unexpected string",p);
  1037. return p + 1;
  1038. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1039. syntax.curly_count--;
  1040. // if, for , while の閉じ判定
  1041. p = parse_syntax_close(p + 1);
  1042. return p;
  1043. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1044. // switch() 閉じ判定
  1045. int pos = syntax.curly_count-1;
  1046. char label[256];
  1047. int l;
  1048. // 一時変数を消す
  1049. sprintf(label,"set $@__SW%x_VAL,0;",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,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1055. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1056. parse_line(label);
  1057. syntax.curly_count--;
  1058. // 現在地のラベルを付ける
  1059. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1060. l=add_str(label);
  1061. set_label(l,script_pos, p);
  1062. if(syntax.curly[pos].flag) {
  1063. // default が存在する
  1064. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1065. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1066. parse_line(label);
  1067. syntax.curly_count--;
  1068. }
  1069. // 終了ラベルを付ける
  1070. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1071. l=add_str(label);
  1072. set_label(l,script_pos, p);
  1073. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1074. syntax.curly_count--;
  1075. return p+1;
  1076. } else {
  1077. disp_error_message("parse_curly_close: unexpected string",p);
  1078. return p + 1;
  1079. }
  1080. }
  1081. // 構文関連の処理
  1082. // break, case, continue, default, do, for, function,
  1083. // if, switch, while をこの内部で処理します。
  1084. const char* parse_syntax(const char* p)
  1085. {
  1086. const char *p2 = skip_word(p);
  1087. switch(*p) {
  1088. case 'B':
  1089. case 'b':
  1090. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1091. // break の処理
  1092. char label[256];
  1093. int pos = syntax.curly_count - 1;
  1094. while(pos >= 0) {
  1095. if(syntax.curly[pos].type == TYPE_DO) {
  1096. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1097. break;
  1098. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1099. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1100. break;
  1101. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1102. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1103. break;
  1104. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1105. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1106. break;
  1107. }
  1108. pos--;
  1109. }
  1110. if(pos < 0) {
  1111. disp_error_message("parse_syntax: unexpected 'break'",p);
  1112. } else {
  1113. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1114. parse_line(label);
  1115. syntax.curly_count--;
  1116. }
  1117. p = skip_space(p2);
  1118. if(*p != ';') {
  1119. disp_error_message("parse_syntax: need ';'",p);
  1120. }
  1121. p++;
  1122. // if, for , while の閉じ判定
  1123. p = parse_syntax_close(p + 1);
  1124. return p;
  1125. }
  1126. break;
  1127. case 'c':
  1128. case 'C':
  1129. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1130. // case の処理
  1131. int pos = syntax.curly_count-1;
  1132. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1133. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1134. return p+1;
  1135. } else {
  1136. char label[256];
  1137. int l,v;
  1138. char *np;
  1139. if(syntax.curly[pos].count != 1) {
  1140. // FALLTHRU 用のジャンプ
  1141. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1142. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1143. parse_line(label);
  1144. syntax.curly_count--;
  1145. // 現在地のラベルを付ける
  1146. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1147. l=add_str(label);
  1148. set_label(l,script_pos, p);
  1149. }
  1150. // switch 判定文
  1151. p = skip_space(p2);
  1152. if(p == p2) {
  1153. disp_error_message("parse_syntax: expect space ' '",p);
  1154. }
  1155. // check whether case label is integer or not
  1156. v = strtol(p,&np,0);
  1157. if(np == p) { //Check for constants
  1158. p2 = skip_word(p);
  1159. v = p2-p; // length of word at p2
  1160. memcpy(label,p,v);
  1161. label[v]='\0';
  1162. v = search_str(label);
  1163. if (v < 0 || str_data[v].type != C_INT)
  1164. disp_error_message("parse_syntax: 'case' label not integer",p);
  1165. v = str_data[v].val;
  1166. p = skip_word(p);
  1167. } else { //Numeric value
  1168. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1169. p++;
  1170. p = skip_word(p);
  1171. if(np != p)
  1172. disp_error_message("parse_syntax: 'case' label not integer",np);
  1173. }
  1174. p = skip_space(p);
  1175. if(*p != ':')
  1176. disp_error_message("parse_syntax: expect ':'",p);
  1177. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1178. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1179. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1180. // 2回parse しないとダメ
  1181. p2 = parse_line(label);
  1182. parse_line(p2);
  1183. syntax.curly_count--;
  1184. if(syntax.curly[pos].count != 1) {
  1185. // FALLTHRU 終了後のラベル
  1186. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1187. l=add_str(label);
  1188. set_label(l,script_pos,p);
  1189. }
  1190. // check duplication of case label [Rayce]
  1191. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1192. disp_error_message("parse_syntax: dup 'case'",p);
  1193. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1194. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1195. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1196. parse_line(label);
  1197. syntax.curly_count--;
  1198. syntax.curly[pos].count++;
  1199. }
  1200. return p + 1;
  1201. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1202. // continue の処理
  1203. char label[256];
  1204. int pos = syntax.curly_count - 1;
  1205. while(pos >= 0) {
  1206. if(syntax.curly[pos].type == TYPE_DO) {
  1207. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1208. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1209. break;
  1210. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1211. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1212. break;
  1213. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1214. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1215. break;
  1216. }
  1217. pos--;
  1218. }
  1219. if(pos < 0) {
  1220. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1221. } else {
  1222. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1223. parse_line(label);
  1224. syntax.curly_count--;
  1225. }
  1226. p = skip_space(p2);
  1227. if(*p != ';')
  1228. disp_error_message("parse_syntax: need ';'",p);
  1229. p++;
  1230. // if, for , while の閉じ判定
  1231. p = parse_syntax_close(p + 1);
  1232. return p;
  1233. }
  1234. break;
  1235. case 'd':
  1236. case 'D':
  1237. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1238. // switch - default の処理
  1239. int pos = syntax.curly_count-1;
  1240. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1241. disp_error_message("parse_syntax: unexpected 'default'",p);
  1242. } else if(syntax.curly[pos].flag) {
  1243. disp_error_message("parse_syntax: dup 'default'",p);
  1244. } else {
  1245. char label[256];
  1246. int l;
  1247. // 現在地のラベルを付ける
  1248. p = skip_space(p2);
  1249. if(*p != ':') {
  1250. disp_error_message("parse_syntax: need ':'",p);
  1251. }
  1252. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1253. l=add_str(label);
  1254. set_label(l,script_pos,p);
  1255. // 無条件で次のリンクに飛ばす
  1256. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1257. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1258. parse_line(label);
  1259. syntax.curly_count--;
  1260. // default のラベルを付ける
  1261. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1262. l=add_str(label);
  1263. set_label(l,script_pos,p);
  1264. syntax.curly[syntax.curly_count - 1].flag = 1;
  1265. syntax.curly[pos].count++;
  1266. }
  1267. return p + 1;
  1268. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1269. int l;
  1270. char label[256];
  1271. p=skip_space(p2);
  1272. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1273. syntax.curly[syntax.curly_count].count = 1;
  1274. syntax.curly[syntax.curly_count].index = syntax.index++;
  1275. syntax.curly[syntax.curly_count].flag = 0;
  1276. // 現在地のラベル形成する
  1277. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1278. l=add_str(label);
  1279. set_label(l,script_pos,p);
  1280. syntax.curly_count++;
  1281. return p;
  1282. }
  1283. break;
  1284. case 'f':
  1285. case 'F':
  1286. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1287. int l;
  1288. char label[256];
  1289. int pos = syntax.curly_count;
  1290. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1291. syntax.curly[syntax.curly_count].count = 1;
  1292. syntax.curly[syntax.curly_count].index = syntax.index++;
  1293. syntax.curly[syntax.curly_count].flag = 0;
  1294. syntax.curly_count++;
  1295. p=skip_space(p2);
  1296. if(*p != '(')
  1297. disp_error_message("parse_syntax: need '('",p);
  1298. p++;
  1299. // 初期化文を実行する
  1300. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1301. p=parse_line(p);
  1302. syntax.curly_count--;
  1303. // 条件判断開始のラベル形成する
  1304. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1305. l=add_str(label);
  1306. set_label(l,script_pos,p);
  1307. p=skip_space(p);
  1308. if(*p == ';') {
  1309. // for(;;) のパターンなので必ず真
  1310. ;
  1311. } else {
  1312. // 条件が偽なら終了地点に飛ばす
  1313. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1314. add_scriptl(add_str("jump_zero"));
  1315. add_scriptc(C_ARG);
  1316. p=parse_expr(p);
  1317. p=skip_space(p);
  1318. add_scriptl(add_str(label));
  1319. add_scriptc(C_FUNC);
  1320. }
  1321. if(*p != ';')
  1322. disp_error_message("parse_syntax: need ';'",p);
  1323. p++;
  1324. // ループ開始に飛ばす
  1325. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1326. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1327. parse_line(label);
  1328. syntax.curly_count--;
  1329. // 次のループへのラベル形成する
  1330. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1331. l=add_str(label);
  1332. set_label(l,script_pos,p);
  1333. // 次のループに入る時の処理
  1334. // for 最後の ')' を ';' として扱うフラグ
  1335. parse_syntax_for_flag = 1;
  1336. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1337. p=parse_line(p);
  1338. syntax.curly_count--;
  1339. parse_syntax_for_flag = 0;
  1340. // 条件判定処理に飛ばす
  1341. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1342. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1343. parse_line(label);
  1344. syntax.curly_count--;
  1345. // ループ開始のラベル付け
  1346. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1347. l=add_str(label);
  1348. set_label(l,script_pos,p);
  1349. return p;
  1350. }
  1351. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1352. {// internal script function
  1353. const char *func_name;
  1354. func_name = skip_space(p2);
  1355. p = skip_word(func_name);
  1356. if( p == func_name )
  1357. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1358. if( *skip_space(p) == ';' )
  1359. {// function <name> ;
  1360. // function declaration - just register the name
  1361. int l;
  1362. l = add_word(func_name);
  1363. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1364. str_data[l].type = C_USERFUNC;
  1365. return skip_space(p) + 1;
  1366. }
  1367. else
  1368. {// function <name> <line/block of code>
  1369. char label[256];
  1370. int l;
  1371. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1372. syntax.curly[syntax.curly_count].count = 1;
  1373. syntax.curly[syntax.curly_count].index = syntax.index++;
  1374. syntax.curly[syntax.curly_count].flag = 0;
  1375. ++syntax.curly_count;
  1376. // Jump over the function code
  1377. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1378. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1379. ++syntax.curly_count;
  1380. parse_line(label);
  1381. --syntax.curly_count;
  1382. // Set the position of the function (label)
  1383. l=add_word(func_name);
  1384. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1385. str_data[l].type = C_USERFUNC;
  1386. set_label(l, script_pos, p);
  1387. if( parse_options&SCRIPT_USE_LABEL_DB )
  1388. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1389. return skip_space(p);
  1390. }
  1391. }
  1392. break;
  1393. case 'i':
  1394. case 'I':
  1395. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1396. // if() の処理
  1397. char label[256];
  1398. p=skip_space(p2);
  1399. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1400. disp_error_message("need '('",p);
  1401. }
  1402. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1403. syntax.curly[syntax.curly_count].count = 1;
  1404. syntax.curly[syntax.curly_count].index = syntax.index++;
  1405. syntax.curly[syntax.curly_count].flag = 0;
  1406. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1407. syntax.curly_count++;
  1408. add_scriptl(add_str("jump_zero"));
  1409. add_scriptc(C_ARG);
  1410. p=parse_expr(p);
  1411. p=skip_space(p);
  1412. add_scriptl(add_str(label));
  1413. add_scriptc(C_FUNC);
  1414. return p;
  1415. }
  1416. break;
  1417. case 's':
  1418. case 'S':
  1419. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1420. // switch() の処理
  1421. char label[256];
  1422. p=skip_space(p2);
  1423. if(*p != '(') {
  1424. disp_error_message("need '('",p);
  1425. }
  1426. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1427. syntax.curly[syntax.curly_count].count = 1;
  1428. syntax.curly[syntax.curly_count].index = syntax.index++;
  1429. syntax.curly[syntax.curly_count].flag = 0;
  1430. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1431. syntax.curly_count++;
  1432. add_scriptl(add_str("set"));
  1433. add_scriptc(C_ARG);
  1434. add_scriptl(add_str(label));
  1435. p=parse_expr(p);
  1436. p=skip_space(p);
  1437. if(*p != '{') {
  1438. disp_error_message("parse_syntax: need '{'",p);
  1439. }
  1440. add_scriptc(C_FUNC);
  1441. return p + 1;
  1442. }
  1443. break;
  1444. case 'w':
  1445. case 'W':
  1446. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1447. int l;
  1448. char label[256];
  1449. p=skip_space(p2);
  1450. if(*p != '(') {
  1451. disp_error_message("need '('",p);
  1452. }
  1453. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1454. syntax.curly[syntax.curly_count].count = 1;
  1455. syntax.curly[syntax.curly_count].index = syntax.index++;
  1456. syntax.curly[syntax.curly_count].flag = 0;
  1457. // 条件判断開始のラベル形成する
  1458. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1459. l=add_str(label);
  1460. set_label(l,script_pos,p);
  1461. // 条件が偽なら終了地点に飛ばす
  1462. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1463. syntax.curly_count++;
  1464. add_scriptl(add_str("jump_zero"));
  1465. add_scriptc(C_ARG);
  1466. p=parse_expr(p);
  1467. p=skip_space(p);
  1468. add_scriptl(add_str(label));
  1469. add_scriptc(C_FUNC);
  1470. return p;
  1471. }
  1472. break;
  1473. }
  1474. return NULL;
  1475. }
  1476. const char* parse_syntax_close(const char *p) {
  1477. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1478. int flag;
  1479. do {
  1480. p = parse_syntax_close_sub(p,&flag);
  1481. } while(flag);
  1482. return p;
  1483. }
  1484. // if, for , while , do の閉じ判定
  1485. // flag == 1 : 閉じられた
  1486. // flag == 0 : 閉じられない
  1487. const char* parse_syntax_close_sub(const char* p,int* flag)
  1488. {
  1489. char label[256];
  1490. int pos = syntax.curly_count - 1;
  1491. int l;
  1492. *flag = 1;
  1493. if(syntax.curly_count <= 0) {
  1494. *flag = 0;
  1495. return p;
  1496. } else if(syntax.curly[pos].type == TYPE_IF) {
  1497. const char *bp = p;
  1498. const char *p2;
  1499. // if 最終場所へ飛ばす
  1500. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1501. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1502. parse_line(label);
  1503. syntax.curly_count--;
  1504. // 現在地のラベルを付ける
  1505. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1506. l=add_str(label);
  1507. set_label(l,script_pos,p);
  1508. syntax.curly[pos].count++;
  1509. p = skip_space(p);
  1510. p2 = skip_word(p);
  1511. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1512. // else or else - if
  1513. p = skip_space(p2);
  1514. p2 = skip_word(p);
  1515. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1516. // else - if
  1517. p=skip_space(p2);
  1518. if(*p != '(') {
  1519. disp_error_message("need '('",p);
  1520. }
  1521. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1522. add_scriptl(add_str("jump_zero"));
  1523. add_scriptc(C_ARG);
  1524. p=parse_expr(p);
  1525. p=skip_space(p);
  1526. add_scriptl(add_str(label));
  1527. add_scriptc(C_FUNC);
  1528. *flag = 0;
  1529. return p;
  1530. } else {
  1531. // else
  1532. if(!syntax.curly[pos].flag) {
  1533. syntax.curly[pos].flag = 1;
  1534. *flag = 0;
  1535. return p;
  1536. }
  1537. }
  1538. }
  1539. // if 閉じ
  1540. syntax.curly_count--;
  1541. // 最終地のラベルを付ける
  1542. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1543. l=add_str(label);
  1544. set_label(l,script_pos,p);
  1545. if(syntax.curly[pos].flag == 1) {
  1546. // このifに対するelseじゃないのでポインタの位置は同じ
  1547. return bp;
  1548. }
  1549. return p;
  1550. } else if(syntax.curly[pos].type == TYPE_DO) {
  1551. int l;
  1552. char label[256];
  1553. const char *p2;
  1554. if(syntax.curly[pos].flag) {
  1555. // 現在地のラベル形成する(continue でここに来る)
  1556. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1557. l=add_str(label);
  1558. set_label(l,script_pos,p);
  1559. }
  1560. // 条件が偽なら終了地点に飛ばす
  1561. p = skip_space(p);
  1562. p2 = skip_word(p);
  1563. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1564. disp_error_message("parse_syntax: need 'while'",p);
  1565. p = skip_space(p2);
  1566. if(*p != '(') {
  1567. disp_error_message("need '('",p);
  1568. }
  1569. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1570. add_scriptl(add_str("jump_zero"));
  1571. add_scriptc(C_ARG);
  1572. p=parse_expr(p);
  1573. p=skip_space(p);
  1574. add_scriptl(add_str(label));
  1575. add_scriptc(C_FUNC);
  1576. // 開始地点に飛ばす
  1577. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1578. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1579. parse_line(label);
  1580. syntax.curly_count--;
  1581. // 条件終了地点のラベル形成する
  1582. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1583. l=add_str(label);
  1584. set_label(l,script_pos,p);
  1585. p = skip_space(p);
  1586. if(*p != ';') {
  1587. disp_error_message("parse_syntax: need ';'",p);
  1588. return p+1;
  1589. }
  1590. p++;
  1591. syntax.curly_count--;
  1592. return p;
  1593. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1594. // 次のループに飛ばす
  1595. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1596. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1597. parse_line(label);
  1598. syntax.curly_count--;
  1599. // for 終了のラベル付け
  1600. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1601. l=add_str(label);
  1602. set_label(l,script_pos,p);
  1603. syntax.curly_count--;
  1604. return p;
  1605. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1606. // while 条件判断へ飛ばす
  1607. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1608. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1609. parse_line(label);
  1610. syntax.curly_count--;
  1611. // while 終了のラベル付け
  1612. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1613. l=add_str(label);
  1614. set_label(l,script_pos,p);
  1615. syntax.curly_count--;
  1616. return p;
  1617. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1618. int pos = syntax.curly_count-1;
  1619. char label[256];
  1620. int l;
  1621. // 戻す
  1622. sprintf(label,"return;");
  1623. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1624. parse_line(label);
  1625. syntax.curly_count--;
  1626. // 現在地のラベルを付ける
  1627. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1628. l=add_str(label);
  1629. set_label(l,script_pos,p);
  1630. syntax.curly_count--;
  1631. return p + 1;
  1632. } else {
  1633. *flag = 0;
  1634. return p;
  1635. }
  1636. }
  1637. /*==========================================
  1638. * 組み込み関数の追加
  1639. *------------------------------------------*/
  1640. static void add_buildin_func(void)
  1641. {
  1642. int i,n;
  1643. const char* p;
  1644. for( i=0; buildin_func[i].func; i++ ){
  1645. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1646. // 'v' - value (either string or int or reference)
  1647. // 's' - string
  1648. // 'i' - int
  1649. // 'r' - reference (of a variable)
  1650. // 'l' - label
  1651. // '?' - one optional parameter
  1652. // '*' - unknown number of optional parameters
  1653. p=buildin_func[i].arg;
  1654. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1655. while( *p == '?' ) ++p;
  1656. if( *p == '*' ) ++p;
  1657. if( *p != 0){
  1658. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1659. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1660. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1661. } else {
  1662. n=add_str(buildin_func[i].name);
  1663. str_data[n].type=C_FUNC;
  1664. str_data[n].val=i;
  1665. str_data[n].func=buildin_func[i].func;
  1666. }
  1667. }
  1668. }
  1669. /*==========================================
  1670. * 定数データベースの読み込み
  1671. *------------------------------------------*/
  1672. static void read_constdb(void)
  1673. {
  1674. FILE *fp;
  1675. char line[1024],name[1024],val[1024];
  1676. int n,type;
  1677. sprintf(line, "%s/const.txt", db_path);
  1678. fp=fopen(line, "r");
  1679. if(fp==NULL){
  1680. ShowError("can't read %s\n", line);
  1681. return ;
  1682. }
  1683. while(fgets(line, sizeof(line), fp))
  1684. {
  1685. if(line[0]=='/' && line[1]=='/')
  1686. continue;
  1687. type=0;
  1688. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1689. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1690. n=add_str(name);
  1691. if(type==0)
  1692. str_data[n].type=C_INT;
  1693. else
  1694. str_data[n].type=C_PARAM;
  1695. str_data[n].val= (int)strtol(val,NULL,0);
  1696. }
  1697. }
  1698. fclose(fp);
  1699. }
  1700. /*==========================================
  1701. * エラー表示
  1702. *------------------------------------------*/
  1703. const char* script_print_line( const char *p, const char *mark, int line )
  1704. {
  1705. int i;
  1706. if( p == NULL || !p[0] ) return NULL;
  1707. if( line < 0 )
  1708. printf("*% 5d : ", -line);
  1709. else
  1710. printf(" % 5d : ", line);
  1711. for(i=0;p[i] && p[i] != '\n';i++){
  1712. if(p + i != mark)
  1713. printf("%c",p[i]);
  1714. else
  1715. printf("\'%c\'",p[i]);
  1716. }
  1717. printf("\n");
  1718. return p+i+(p[i] == '\n' ? 1 : 0);
  1719. }
  1720. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos)
  1721. {
  1722. // エラーが発生した行を求める
  1723. int j;
  1724. int line = start_line;
  1725. const char *p;
  1726. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1727. for(p=src;p && *p;line++){
  1728. 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. printf("\a\n");
  1739. printf("script error on %s line %d\n", file, line);
  1740. printf(" %s\n", error_msg);
  1741. for(j = 0; j < 5; j++ ) {
  1742. script_print_line( linestart[j], NULL, line + j - 5);
  1743. }
  1744. p = script_print_line( p, error_pos, -line);
  1745. for(j = 0; j < 5; j++) {
  1746. p = script_print_line( p, NULL, line + j + 1 );
  1747. }
  1748. }
  1749. /*==========================================
  1750. * スクリプトの解析
  1751. *------------------------------------------*/
  1752. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1753. {
  1754. const char *p,*tmpp;
  1755. int i;
  1756. struct script_code* code = NULL;
  1757. static int first=1;
  1758. char end;
  1759. if( src == NULL )
  1760. return NULL;// empty script
  1761. memset(&syntax,0,sizeof(syntax));
  1762. if(first){
  1763. add_buildin_func();
  1764. read_constdb();
  1765. }
  1766. first=0;
  1767. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1768. script_pos=0;
  1769. script_size=SCRIPT_BLOCK_SIZE;
  1770. str_data[LABEL_NEXTLINE].type=C_NOP;
  1771. str_data[LABEL_NEXTLINE].backpatch=-1;
  1772. str_data[LABEL_NEXTLINE].label=-1;
  1773. for(i=LABEL_START;i<str_num;i++){
  1774. if(
  1775. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1776. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1777. ){
  1778. str_data[i].type=C_NOP;
  1779. str_data[i].backpatch=-1;
  1780. str_data[i].label=-1;
  1781. }
  1782. }
  1783. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1784. if( options&SCRIPT_USE_LABEL_DB )
  1785. scriptlabel_db->clear(scriptlabel_db, NULL);
  1786. parse_options = options;
  1787. if( setjmp( error_jump ) != 0 ) {
  1788. //Restore program state when script has problems. [from jA]
  1789. int i;
  1790. const int size = ARRAYLENGTH(syntax.curly);
  1791. if( error_report )
  1792. script_error(src,file,line,error_msg,error_pos);
  1793. aFree( error_msg );
  1794. aFree( script_buf );
  1795. script_pos = 0;
  1796. script_size = 0;
  1797. script_buf = NULL;
  1798. for(i=LABEL_START;i<str_num;i++)
  1799. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1800. for(i=0; i<size; i++)
  1801. linkdb_final(&syntax.curly[i].case_label);
  1802. return NULL;
  1803. }
  1804. parse_syntax_for_flag=0;
  1805. p=src;
  1806. p=skip_space(p);
  1807. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1808. {// does not require brackets around the script
  1809. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1810. {// empty script and can return NULL
  1811. aFree( script_buf );
  1812. script_pos = 0;
  1813. script_size = 0;
  1814. script_buf = NULL;
  1815. return NULL;
  1816. }
  1817. end = '\0';
  1818. }
  1819. else
  1820. {// requires brackets around the script
  1821. if( *p != '{' )
  1822. disp_error_message("not found '{'",p);
  1823. p = skip_space(p+1);
  1824. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1825. {// empty script and can return NULL
  1826. aFree( script_buf );
  1827. script_pos = 0;
  1828. script_size = 0;
  1829. script_buf = NULL;
  1830. return NULL;
  1831. }
  1832. end = '}';
  1833. }
  1834. while( syntax.curly_count != 0 || *p != end )
  1835. {
  1836. if( *p == '\0' )
  1837. disp_error_message("unexpected end of script",p);
  1838. // labelだけ特殊処理
  1839. tmpp=skip_space(skip_word(p));
  1840. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1841. i=add_word(p);
  1842. set_label(i,script_pos,p);
  1843. if( parse_options&SCRIPT_USE_LABEL_DB )
  1844. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1845. p=tmpp+1;
  1846. p=skip_space(p);
  1847. continue;
  1848. }
  1849. // 他は全部一緒くた
  1850. p=parse_line(p);
  1851. p=skip_space(p);
  1852. add_scriptc(C_EOL);
  1853. set_label(LABEL_NEXTLINE,script_pos,p);
  1854. str_data[LABEL_NEXTLINE].type=C_NOP;
  1855. str_data[LABEL_NEXTLINE].backpatch=-1;
  1856. str_data[LABEL_NEXTLINE].label=-1;
  1857. }
  1858. add_scriptc(C_NOP);
  1859. script_size = script_pos;
  1860. RECREATE(script_buf,unsigned char,script_pos);
  1861. // 未解決のラベルを解決
  1862. for(i=LABEL_START;i<str_num;i++){
  1863. if(str_data[i].type==C_NOP){
  1864. int j,next;
  1865. str_data[i].type=C_NAME;
  1866. str_data[i].label=i;
  1867. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1868. next=GETVALUE(script_buf,j);
  1869. SETVALUE(script_buf,j,i);
  1870. j=next;
  1871. }
  1872. }
  1873. }
  1874. #ifdef DEBUG_DISP
  1875. for(i=0;i<script_pos;i++){
  1876. if((i&15)==0) printf("%04x : ",i);
  1877. printf("%02x ",script_buf[i]);
  1878. if((i&15)==15) printf("\n");
  1879. }
  1880. printf("\n");
  1881. #endif
  1882. #ifdef DEBUG_DISASM
  1883. {
  1884. int i = 0,j;
  1885. while(i < script_pos) {
  1886. printf("%06x ",i);
  1887. j = i;
  1888. switch(get_com(script_buf,&i)) {
  1889. case C_EOL: printf("C_EOL"); break;
  1890. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1891. case C_POS:
  1892. printf("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1893. i += 3;
  1894. break;
  1895. case C_NAME:
  1896. j = (*(int*)(script_buf+i)&0xffffff);
  1897. printf("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1898. i += 3;
  1899. break;
  1900. case C_ARG: printf("C_ARG"); break;
  1901. case C_FUNC: printf("C_FUNC"); break;
  1902. case C_ADD: printf("C_ADD"); break;
  1903. case C_SUB: printf("C_SUB"); break;
  1904. case C_MUL: printf("C_MUL"); break;
  1905. case C_DIV: printf("C_DIV"); break;
  1906. case C_MOD: printf("C_MOD"); break;
  1907. case C_EQ: printf("C_EQ"); break;
  1908. case C_NE: printf("C_NE"); break;
  1909. case C_GT: printf("C_GT"); break;
  1910. case C_GE: printf("C_GE"); break;
  1911. case C_LT: printf("C_LT"); break;
  1912. case C_LE: printf("C_LE"); break;
  1913. case C_AND: printf("C_AND"); break;
  1914. case C_OR: printf("C_OR"); break;
  1915. case C_XOR: printf("C_XOR"); break;
  1916. case C_LAND: printf("C_LAND"); break;
  1917. case C_LOR: printf("C_LOR"); break;
  1918. case C_R_SHIFT: printf("C_R_SHIFT"); break;
  1919. case C_L_SHIFT: printf("C_L_SHIFT"); break;
  1920. case C_NEG: printf("C_NEG"); break;
  1921. case C_NOT: printf("C_NOT"); break;
  1922. case C_LNOT: printf("C_LNOT"); break;
  1923. case C_NOP: printf("C_NOP"); break;
  1924. case C_OP3: printf("C_OP3"); break;
  1925. case C_STR:
  1926. j = strlen(script_buf + i);
  1927. printf("C_STR %s",script_buf + i);
  1928. i+= j+1;
  1929. break;
  1930. default:
  1931. printf("unknown");
  1932. }
  1933. printf("\n");
  1934. }
  1935. }
  1936. #endif
  1937. CREATE(code,struct script_code,1);
  1938. code->script_buf = script_buf;
  1939. code->script_size = script_size;
  1940. code->script_vars = NULL;
  1941. return code;
  1942. }
  1943. //
  1944. // Script state
  1945. //
  1946. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1947. /// Returns the player attached to this script, identified by the rid.
  1948. /// If there is no player attached, the script is terminated.
  1949. TBL_PC *script_rid2sd(struct script_state *st)
  1950. {
  1951. TBL_PC *sd=map_id2sd(st->rid);
  1952. if(!sd){
  1953. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1954. st->state = END;
  1955. }
  1956. return sd;
  1957. }
  1958. /// Retrieves the value of a script data
  1959. int get_val(struct script_state* st, struct script_data* data)
  1960. {
  1961. char* name;
  1962. char prefix;
  1963. char postfix;
  1964. TBL_PC* sd = NULL;
  1965. if( !data_isreference(data) )
  1966. return 0;// not a variable
  1967. name = reference_getname(data);
  1968. prefix = name[0];
  1969. postfix = name[strlen(name) - 1];
  1970. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1971. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1972. {
  1973. sd = script_rid2sd(st);
  1974. if( sd == NULL )
  1975. {// needs player attached
  1976. if( postfix == '$' )
  1977. {// string variable
  1978. ShowError("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1979. data->type = C_CONSTSTR;
  1980. data->u.str = "";
  1981. }
  1982. else
  1983. {// integer variable
  1984. ShowError("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1985. data->type = C_INT;
  1986. data->u.num = 0;
  1987. }
  1988. return 0;
  1989. }
  1990. }
  1991. if( postfix == '$' )
  1992. {// string variable
  1993. data->type = C_CONSTSTR;
  1994. switch( prefix )
  1995. {
  1996. case '@':
  1997. data->u.str = pc_readregstr(sd, data->u.num);
  1998. break;
  1999. case '$':
  2000. data->u.str = (char *)idb_get(mapregstr_db, data->u.num);
  2001. break;
  2002. case '#':
  2003. if( name[1] == '#' )
  2004. data->u.str = pc_readaccountreg2str(sd, name);// global
  2005. else
  2006. data->u.str = pc_readaccountregstr(sd, name);// local
  2007. break;
  2008. case '.':
  2009. {
  2010. struct linkdb_node** n =
  2011. data->ref ? data->ref:
  2012. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2013. &st->script->script_vars;// npc variable
  2014. data->u.str = linkdb_search(n, (void*)reference_getuid(data));
  2015. }
  2016. break;
  2017. default:
  2018. data->u.str = pc_readglobalreg_str(sd, name);
  2019. break;
  2020. }
  2021. if( data->u.str == NULL )
  2022. data->u.str = "";
  2023. }
  2024. else
  2025. {// integer variable
  2026. data->type = C_INT;
  2027. if( reference_toconstant(data) )
  2028. {
  2029. data->u.num = reference_getconstant(data);
  2030. }
  2031. else if( reference_toparam(data) )
  2032. {
  2033. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2034. }
  2035. else
  2036. switch( prefix )
  2037. {
  2038. case '@':
  2039. data->u.num = pc_readreg(sd, data->u.num);
  2040. break;
  2041. case '$':
  2042. data->u.num = (int)idb_get(mapreg_db, data->u.num);
  2043. break;
  2044. case '#':
  2045. if( name[1] == '#' )
  2046. data->u.num = pc_readaccountreg2(sd, name);// global
  2047. else
  2048. data->u.num = pc_readaccountreg(sd, name);// local
  2049. break;
  2050. case '.':
  2051. {
  2052. struct linkdb_node** n =
  2053. data->ref ? data->ref:
  2054. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2055. &st->script->script_vars;// npc variable
  2056. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2057. }
  2058. break;
  2059. default:
  2060. data->u.num = pc_readglobalreg(sd, name);
  2061. break;
  2062. }
  2063. }
  2064. return 0;
  2065. }
  2066. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2067. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2068. {
  2069. struct script_data data;
  2070. data.type = C_NAME;
  2071. data.u.num = uid;
  2072. data.ref = ref;
  2073. get_val(st, &data);
  2074. return (data.type == C_INT ? (void*)data.u.num : (void*)data.u.str);
  2075. }
  2076. /*==========================================
  2077. * Stores the value of a script variable
  2078. * Return value is 0 on fail, 1 on success.
  2079. *------------------------------------------*/
  2080. static int set_reg(struct script_state* st, TBL_PC* sd, int num, char* name, void* value, struct linkdb_node** ref)
  2081. {
  2082. char prefix = name[0]; char postfix = name[strlen(name)-1];
  2083. if (postfix == '$') { // string variable
  2084. char* str = (char*)value;
  2085. switch (prefix) {
  2086. case '@':
  2087. return pc_setregstr(sd, num, str);
  2088. case '$':
  2089. return mapreg_setregstr(num, str);
  2090. case '#':
  2091. return (name[1] == '#') ?
  2092. pc_setaccountreg2str(sd, name, str) :
  2093. pc_setaccountregstr(sd, name, str);
  2094. case '.': {
  2095. char* p;
  2096. struct linkdb_node** n;
  2097. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2098. p = linkdb_search(n, (void*)num);
  2099. if (p) {
  2100. linkdb_erase(n, (void*)num);
  2101. aFree(p);
  2102. }
  2103. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2104. }
  2105. return 1;
  2106. default:
  2107. return pc_setglobalreg_str(sd, name, str);
  2108. }
  2109. } else { // integer variable
  2110. int val = (int)value;
  2111. if(str_data[num&0x00ffffff].type == C_PARAM)
  2112. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2113. switch (prefix) {
  2114. case '@':
  2115. return pc_setreg(sd, num, val);
  2116. case '$':
  2117. return mapreg_setreg(num, val);
  2118. case '#':
  2119. return (name[1] == '#') ?
  2120. pc_setaccountreg2(sd, name, val) :
  2121. pc_setaccountreg(sd, name, val);
  2122. case '.': {
  2123. struct linkdb_node** n;
  2124. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2125. if (val == 0)
  2126. linkdb_erase(n, (void*)num);
  2127. else
  2128. linkdb_replace(n, (void*)num, (void*)val);
  2129. }
  2130. return 1;
  2131. default:
  2132. return pc_setglobalreg(sd, name, val);
  2133. }
  2134. }
  2135. }
  2136. int set_var(TBL_PC* sd, char* name, void* val)
  2137. {
  2138. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  2139. }
  2140. /// Converts the data to a string
  2141. const char* conv_str(struct script_state* st, struct script_data* data)
  2142. {
  2143. char* p;
  2144. get_val(st, data);
  2145. if( data_isstring(data) )
  2146. {// nothing to convert
  2147. }
  2148. else if( data_isint(data) )
  2149. {// int -> string
  2150. CREATE(p, char, ITEM_NAME_LENGTH);
  2151. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2152. p[ITEM_NAME_LENGTH-1] = '\0';
  2153. data->type = C_STR;
  2154. data->u.str = p;
  2155. }
  2156. else if( data_isreference(data) )
  2157. {// reference -> string
  2158. //##TODO when does this happen (check get_val) [FlavioJS]
  2159. data->type = C_CONSTSTR;
  2160. data->u.str = reference_getname(data);
  2161. }
  2162. else
  2163. {// unsupported data type
  2164. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2165. script_reportdata(data);
  2166. script_reportsrc(st);
  2167. data->type = C_CONSTSTR;
  2168. data->u.str = "";
  2169. }
  2170. return data->u.str;
  2171. }
  2172. /// Converts the data to an int
  2173. int conv_num(struct script_state* st, struct script_data* data)
  2174. {
  2175. char* p;
  2176. long num;
  2177. get_val(st, data);
  2178. if( data_isint(data) )
  2179. {// nothing to convert
  2180. }
  2181. else if( data_isstring(data) )
  2182. {// string -> int
  2183. // the result does not overflow or underflow, it is capped instead
  2184. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2185. p = data->u.str;
  2186. errno = 0;
  2187. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2188. if( errno == ERANGE
  2189. #if LONG_MAX > INT_MAX
  2190. || num < INT_MIN || num > INT_MAX
  2191. #endif
  2192. )
  2193. {
  2194. if( num <= INT_MIN )
  2195. {
  2196. num = INT_MIN;
  2197. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2198. }
  2199. else//if( num >= INT_MAX )
  2200. {
  2201. num = INT_MAX;
  2202. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2203. }
  2204. script_reportdata(data);
  2205. script_reportsrc(st);
  2206. }
  2207. if( data->type == C_STR )
  2208. aFree(p);
  2209. data->type = C_INT;
  2210. data->u.num = (int)num;
  2211. }
  2212. #if 0
  2213. // FIXME this function is being used to retrieve the position of labels and
  2214. // probably other stuff [FlavioJS]
  2215. else
  2216. {// unsupported data type
  2217. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2218. script_reportdata(data);
  2219. script_reportsrc(st);
  2220. data->type = C_INT;
  2221. data->u.num = 0;
  2222. }
  2223. #endif
  2224. return data->u.num;
  2225. }
  2226. //
  2227. // Stack operations
  2228. //
  2229. /// Increases the size of the stack
  2230. void stack_expand(struct script_stack* stack)
  2231. {
  2232. stack->sp_max += 64;
  2233. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2234. stack->sp_max * sizeof(stack->stack_data[0]) );
  2235. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2236. 64 * sizeof(stack->stack_data[0]) );
  2237. }
  2238. /// Pushes a value into the stack
  2239. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2240. /// Pushes a value into the stack (with reference)
  2241. void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref)
  2242. {
  2243. if( stack->sp >= stack->sp_max )
  2244. stack_expand(stack);
  2245. stack->stack_data[stack->sp].type = type;
  2246. stack->stack_data[stack->sp].u.num = val;
  2247. stack->stack_data[stack->sp].ref = ref;
  2248. stack->sp++;
  2249. }
  2250. /// Pushes a string into the stack
  2251. void push_str(struct script_stack* stack, int type, char* str)
  2252. {
  2253. if( stack->sp >= stack->sp_max )
  2254. stack_expand(stack);
  2255. stack->stack_data[stack->sp].type = type;
  2256. stack->stack_data[stack->sp].u.str = str;
  2257. stack->stack_data[stack->sp].ref = NULL;
  2258. stack->sp++;
  2259. }
  2260. /// Pushes a copy of the target position into the stack
  2261. void push_copy(struct script_stack* stack, int pos)
  2262. {
  2263. switch( stack->stack_data[pos].type )
  2264. {
  2265. case C_CONSTSTR:
  2266. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2267. break;
  2268. case C_STR:
  2269. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2270. break;
  2271. default:
  2272. push_val2(
  2273. stack,stack->stack_data[pos].type,
  2274. stack->stack_data[pos].u.num,
  2275. stack->stack_data[pos].ref
  2276. );
  2277. break;
  2278. }
  2279. }
  2280. /// Removes the values in indexes [start,end[ from the stack
  2281. void pop_stack(struct script_stack* stack, int start, int end)
  2282. {
  2283. struct script_data* data;
  2284. int i;
  2285. if( start < 0 )
  2286. start = 0;
  2287. if( end > stack->sp_max )
  2288. end = stack->sp_max;
  2289. if( start >= end )
  2290. return;// nothing to pop
  2291. // free stack elements
  2292. for( i = start; i < end; i++ )
  2293. {
  2294. data = &stack->stack_data[i];
  2295. if( data->type == C_STR )
  2296. aFree(data->u.str);
  2297. data->type = C_NOP;
  2298. }
  2299. // move the rest of the elements
  2300. if( stack->sp > end )
  2301. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2302. stack->sp -= end - start;
  2303. }
  2304. ///
  2305. ///
  2306. ///
  2307. /*==========================================
  2308. * スクリプト依存変数、関数依存変数の解放
  2309. *------------------------------------------*/
  2310. void script_free_vars(struct linkdb_node **node)
  2311. {
  2312. struct linkdb_node *n = *node;
  2313. while(n) {
  2314. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2315. char postfix = name[strlen(name)-1];
  2316. if( postfix == '$' ) {
  2317. // 文字型変数なので、データ削除
  2318. aFree(n->data);
  2319. }
  2320. n = n->next;
  2321. }
  2322. linkdb_final( node );
  2323. }
  2324. /*==========================================
  2325. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2326. *------------------------------------------*/
  2327. void script_free_stack(struct script_stack *stack)
  2328. {
  2329. int i;
  2330. for(i = 0; i < stack->sp; i++) {
  2331. if( stack->stack_data[i].type == C_STR ) {
  2332. aFree(stack->stack_data[i].u.str);
  2333. stack->stack_data[i].type = C_INT;
  2334. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2335. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2336. script_free_vars( n );
  2337. aFree( n );
  2338. }
  2339. }
  2340. script_free_vars( stack->var_function );
  2341. aFree(stack->var_function);
  2342. aFree(stack->stack_data);
  2343. aFree(stack);
  2344. }
  2345. void script_free_code(struct script_code* code)
  2346. {
  2347. script_free_vars( &code->script_vars );
  2348. aFree( code->script_buf );
  2349. aFree( code );
  2350. }
  2351. //
  2352. // 実行部main
  2353. //
  2354. /*==========================================
  2355. * コマンドの読み取り
  2356. *------------------------------------------*/
  2357. static int unget_com_data=-1;
  2358. int get_com(unsigned char *script,int *pos)
  2359. {
  2360. int i,j;
  2361. if(unget_com_data>=0){
  2362. i=unget_com_data;
  2363. unget_com_data=-1;
  2364. return i;
  2365. }
  2366. if(script[*pos]>=0x80){
  2367. return C_INT;
  2368. }
  2369. i=0; j=0;
  2370. while(script[*pos]>=0x40){
  2371. i=script[(*pos)++]<<j;
  2372. j+=6;
  2373. }
  2374. return i+(script[(*pos)++]<<j);
  2375. }
  2376. /*==========================================
  2377. * コマンドのプッシュバック
  2378. *------------------------------------------*/
  2379. void unget_com(int c)
  2380. {
  2381. if(unget_com_data!=-1){
  2382. if(battle_config.error_log)
  2383. ShowError("unget_com can back only 1 data\n");
  2384. }
  2385. unget_com_data=c;
  2386. }
  2387. /*==========================================
  2388. * 数値の所得
  2389. *------------------------------------------*/
  2390. int get_num(unsigned char *script,int *pos)
  2391. {
  2392. int i,j;
  2393. i=0; j=0;
  2394. while(script[*pos]>=0xc0){
  2395. i+=(script[(*pos)++]&0x7f)<<j;
  2396. j+=6;
  2397. }
  2398. return i+((script[(*pos)++]&0x7f)<<j);
  2399. }
  2400. /*==========================================
  2401. * スタックから値を取り出す
  2402. *------------------------------------------*/
  2403. int pop_val(struct script_state* st)
  2404. {
  2405. if(st->stack->sp<=0)
  2406. return 0;
  2407. st->stack->sp--;
  2408. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2409. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2410. return st->stack->stack_data[st->stack->sp].u.num;
  2411. return 0;
  2412. }
  2413. int isstr(struct script_data *c)
  2414. {
  2415. if( data_isstring(c) )
  2416. return 1;
  2417. else if( data_isreference(c) ) {
  2418. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2419. char postfix = p[strlen(p)-1];
  2420. return (postfix == '$');
  2421. }
  2422. return 0;
  2423. }
  2424. /// Ternary operators
  2425. /// test ? if_true : if_false
  2426. void op_3(struct script_state* st, int op)
  2427. {
  2428. struct script_data* data;
  2429. int flag = 0;
  2430. data = script_getdatatop(st, -3);
  2431. get_val(st, data);
  2432. if( data_isstring(data) )
  2433. flag = data->u.str[0];// "" -> false
  2434. else if( data_isint(data) )
  2435. flag = data->u.num;// 0 -> false
  2436. else
  2437. {
  2438. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2439. script_reportdata(data);
  2440. script_reportsrc(st);
  2441. script_removetop(st, -3, 0);
  2442. script_pushnil(st);
  2443. return;
  2444. }
  2445. if( flag )
  2446. script_pushcopytop(st, -2);
  2447. else
  2448. script_pushcopytop(st, -1);
  2449. script_removetop(st, -4, -1);
  2450. }
  2451. /// Binary string operators
  2452. /// s1 EQ s2 -> i
  2453. /// s1 NE s2 -> i
  2454. /// s1 GT s2 -> i
  2455. /// s1 GE s2 -> i
  2456. /// s1 LT s2 -> i
  2457. /// s1 LE s2 -> i
  2458. /// s1 ADD s2 -> s
  2459. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2460. {
  2461. int a = 0;
  2462. switch(op){
  2463. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2464. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2465. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2466. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2467. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2468. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2469. case C_ADD:
  2470. {
  2471. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2472. strcpy(buf, s1);
  2473. strcat(buf, s2);
  2474. script_pushstr(st, buf);
  2475. return;
  2476. }
  2477. default:
  2478. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2479. script_reportsrc(st);
  2480. script_pushnil(st);
  2481. st->state = END;
  2482. return;
  2483. }
  2484. script_pushint(st,a);
  2485. }
  2486. /// Binary number operators
  2487. /// i OP i -> i
  2488. void op_2num(struct script_state* st, int op, int i1, int i2)
  2489. {
  2490. int ret;
  2491. double ret_double;
  2492. switch( op )
  2493. {
  2494. case C_AND: ret = i1 & i2; break;
  2495. case C_OR: ret = i1 | i2; break;
  2496. case C_XOR: ret = i1 ^ i2; break;
  2497. case C_LAND: ret = (i1 && i2); break;
  2498. case C_LOR: ret = (i1 || i2); break;
  2499. case C_EQ: ret = (i1 == i2); break;
  2500. case C_NE: ret = (i1 != i2); break;
  2501. case C_GT: ret = (i1 > i2); break;
  2502. case C_GE: ret = (i1 >= i2); break;
  2503. case C_LT: ret = (i1 < i2); break;
  2504. case C_LE: ret = (i1 <= i2); break;
  2505. case C_R_SHIFT: ret = i1>>i2; break;
  2506. case C_L_SHIFT: ret = i1<<i2; break;
  2507. case C_DIV:
  2508. case C_MOD:
  2509. if( i2 == 0 )
  2510. {
  2511. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2512. script_reportsrc(st);
  2513. script_pushnil(st);
  2514. st->state = END;
  2515. return;
  2516. }
  2517. else if( op == C_DIV )
  2518. ret = i1 / i2;
  2519. else//if( op == C_MOD )
  2520. ret = i1 % i2;
  2521. break;
  2522. default:
  2523. switch( op )
  2524. {// operators that can overflow/underflow
  2525. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2526. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2527. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2528. default:
  2529. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2530. script_reportsrc(st);
  2531. script_pushnil(st);
  2532. return;
  2533. }
  2534. if( ret_double < (double)INT_MIN )
  2535. {
  2536. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2537. script_reportsrc(st);
  2538. ret = INT_MIN;
  2539. }
  2540. else if( ret_double > (double)INT_MAX )
  2541. {
  2542. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2543. script_reportsrc(st);
  2544. ret = INT_MAX;
  2545. }
  2546. }
  2547. script_pushint(st, ret);
  2548. }
  2549. /// Binary operators
  2550. void op_2(struct script_state *st, int op)
  2551. {
  2552. struct script_data* left;
  2553. struct script_data* right;
  2554. left = script_getdatatop(st, -2);
  2555. right = script_getdatatop(st, -1);
  2556. get_val(st, left);
  2557. get_val(st, right);
  2558. // automatic conversions
  2559. switch( op )
  2560. {
  2561. case C_ADD:
  2562. if( data_isint(left) && data_isstring(right) )
  2563. {// convert int-string to string-string
  2564. conv_str(st, left);
  2565. }
  2566. else if( data_isstring(left) && data_isint(right) )
  2567. {// convert string-int to string-string
  2568. conv_str(st, right);
  2569. }
  2570. break;
  2571. }
  2572. if( data_isstring(left) && data_isstring(right) )
  2573. {// ss => op_2str
  2574. op_2str(st, op, left->u.str, right->u.str);
  2575. script_removetop(st, -3, -1);// pop the two values before the top one
  2576. }
  2577. else if( data_isint(left) && data_isint(right) )
  2578. {// ii => op_2num
  2579. int i1 = left->u.num;
  2580. int i2 = right->u.num;
  2581. script_removetop(st, -2, 0);
  2582. op_2num(st, op, i1, i2);
  2583. }
  2584. else
  2585. {// invalid argument
  2586. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2587. script_reportdata(left);
  2588. script_reportdata(right);
  2589. script_reportsrc(st);
  2590. script_removetop(st, -2, 0);
  2591. script_pushnil(st);
  2592. st->state = END;
  2593. }
  2594. }
  2595. /// Unary operators
  2596. /// NEG i -> i
  2597. /// NOT i -> i
  2598. /// LNOT i -> i
  2599. void op_1(struct script_state* st, int op)
  2600. {
  2601. struct script_data* data;
  2602. int i1;
  2603. data = script_getdatatop(st, -1);
  2604. get_val(st, data);
  2605. if( !data_isint(data) )
  2606. {// not a number
  2607. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2608. script_reportdata(data);
  2609. script_reportsrc(st);
  2610. script_pushnil(st);
  2611. st->state = END;
  2612. return;
  2613. }
  2614. i1 = data->u.num;
  2615. script_removetop(st, -1, 0);
  2616. switch( op )
  2617. {
  2618. case C_NEG: i1 = -i1; break;
  2619. case C_NOT: i1 = ~i1; break;
  2620. case C_LNOT: i1 = !i1; break;
  2621. default:
  2622. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2623. script_reportsrc(st);
  2624. script_pushnil(st);
  2625. st->state = END;
  2626. return;
  2627. }
  2628. script_pushint(st, i1);
  2629. }
  2630. /*==========================================
  2631. * 関数の実行
  2632. *------------------------------------------*/
  2633. int run_func(struct script_state *st)
  2634. {
  2635. int i,start_sp,end_sp,func;
  2636. end_sp=st->stack->sp;
  2637. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2638. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2639. if(battle_config.error_log)
  2640. ShowError("function not found\n");
  2641. // st->stack->sp=0;
  2642. st->state=END;
  2643. script_reportsrc(st);
  2644. return 1;
  2645. }
  2646. start_sp=i-1;
  2647. st->start=i-1;
  2648. st->end=end_sp;
  2649. func=st->stack->stack_data[st->start].u.num;
  2650. #ifdef DEBUG_RUN
  2651. if(battle_config.etc_log) {
  2652. ShowDebug("run_func : %s? (%d(%d)) sp=%d (%d...%d)\n",str_buf+str_data[func].str, func, str_data[func].type, st->stack->sp, st->start, st->end);
  2653. ShowDebug("stack dump :");
  2654. for(i=0;i<end_sp;i++){
  2655. switch(st->stack->stack_data[i].type){
  2656. case C_INT:
  2657. printf(" int(%d)",st->stack->stack_data[i].u.num);
  2658. break;
  2659. case C_NAME:
  2660. printf(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2661. break;
  2662. case C_ARG:
  2663. printf(" arg");
  2664. break;
  2665. case C_POS:
  2666. printf(" pos(%d)",st->stack->stack_data[i].u.num);
  2667. break;
  2668. case C_STR:
  2669. printf(" str(%s)",st->stack->stack_data[i].u.str);
  2670. break;
  2671. case C_CONSTSTR:
  2672. printf(" cstr(%s)",st->stack->stack_data[i].u.str);
  2673. break;
  2674. default:
  2675. printf(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2676. }
  2677. }
  2678. printf("\n");
  2679. }
  2680. #endif
  2681. if(str_data[func].type!=C_FUNC ){
  2682. ShowMessage ("run_func: '"CL_WHITE"%s"CL_RESET"' (type %d) is not function and command!\n", str_buf+str_data[func].str, str_data[func].type);
  2683. // st->stack->sp=0;
  2684. st->state=END;
  2685. script_reportsrc(st);
  2686. return 1;
  2687. }
  2688. #ifdef DEBUG_RUN
  2689. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2690. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2691. );
  2692. #endif
  2693. if(str_data[func].func){
  2694. if (str_data[func].func(st)) //Report error
  2695. script_reportsrc(st);
  2696. } else {
  2697. if(battle_config.error_log)
  2698. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2699. script_pushint(st,0);
  2700. script_reportsrc(st);
  2701. }
  2702. // Stack's datum are used when re-run functions [Eoe]
  2703. if(st->state != RERUNLINE) {
  2704. pop_stack(st->stack,start_sp,end_sp);
  2705. }
  2706. if(st->state==RETFUNC){
  2707. // ユーザー定義関数からの復帰
  2708. int olddefsp=st->stack->defsp;
  2709. int i;
  2710. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2711. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2712. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2713. st->state=END;
  2714. script_reportsrc(st);
  2715. return 1;
  2716. }
  2717. script_free_vars( st->stack->var_function );
  2718. aFree(st->stack->var_function);
  2719. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2720. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2721. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2722. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2723. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2724. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2725. st->state=GOTO;
  2726. }
  2727. return 0;
  2728. }
  2729. /*==========================================
  2730. * スクリプトの実行
  2731. *------------------------------------------*/
  2732. void run_script_main(struct script_state *st);
  2733. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2734. {
  2735. struct script_state *st;
  2736. TBL_PC *sd=NULL;
  2737. if(rootscript==NULL || pos<0)
  2738. return;
  2739. if (rid) sd = map_id2sd(rid);
  2740. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2741. //Resume script.
  2742. st = sd->st;
  2743. } else {
  2744. st = aCalloc(sizeof(struct script_state), 1);
  2745. // the script is different, make new script_state and stack
  2746. st->stack = aMalloc (sizeof(struct script_stack));
  2747. st->stack->sp=0;
  2748. st->stack->sp_max=64;
  2749. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2750. st->stack->defsp = st->stack->sp;
  2751. st->stack->var_function = aCalloc(1, sizeof(struct linkdb_node*));
  2752. st->state = RUN;
  2753. st->script = rootscript;
  2754. }
  2755. st->pos = pos;
  2756. st->rid = rid;
  2757. st->oid = oid;
  2758. st->sleep.timer = INVALID_TIMER;
  2759. run_script_main(st);
  2760. }
  2761. void script_stop_sleeptimers(int id)
  2762. {
  2763. struct script_state* st;
  2764. for(;;)
  2765. {
  2766. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2767. if( st == NULL )
  2768. break; // no more sleep timers
  2769. if( st->sleep.timer != INVALID_TIMER )
  2770. delete_timer(st->sleep.timer, run_script_timer);
  2771. script_free_stack(st->stack);
  2772. aFree(st);
  2773. }
  2774. }
  2775. /*==========================================
  2776. * 指定ノードをsleep_dbから削除
  2777. *------------------------------------------*/
  2778. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2779. {
  2780. struct linkdb_node *retnode;
  2781. if( n == NULL)
  2782. return NULL;
  2783. if( n->prev == NULL )
  2784. sleep_db = n->next;
  2785. else
  2786. n->prev->next = n->next;
  2787. if( n->next )
  2788. n->next->prev = n->prev;
  2789. retnode = n->next;
  2790. aFree( n );
  2791. return retnode; // 次のノードを返す
  2792. }
  2793. /*==========================================
  2794. * sleep用タイマー関数
  2795. *------------------------------------------*/
  2796. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2797. {
  2798. struct script_state *st = (struct script_state *)data;
  2799. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2800. TBL_PC *sd = map_id2sd(st->rid);
  2801. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2802. { //Character mismatch. Cancel execution.
  2803. st->rid = 0;
  2804. st->state = END;
  2805. }
  2806. while( node && st->sleep.timer != -1 ) {
  2807. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2808. script_erase_sleepdb(node);
  2809. st->sleep.timer = -1;
  2810. break;
  2811. }
  2812. node = node->next;
  2813. }
  2814. if(st->state != RERUNLINE)
  2815. st->sleep.tick = 0;
  2816. run_script_main(st);
  2817. return 0;
  2818. }
  2819. /*==========================================
  2820. * スクリプトの実行メイン部分
  2821. *------------------------------------------*/
  2822. void run_script_main(struct script_state *st)
  2823. {
  2824. int c;
  2825. int cmdcount=script_config.check_cmdcount;
  2826. int gotocount=script_config.check_gotocount;
  2827. TBL_PC *sd;
  2828. //For backing up purposes
  2829. struct script_state *bk_st = NULL;
  2830. int bk_npcid = 0;
  2831. struct script_stack *stack=st->stack;
  2832. sd = st->rid?map_id2sd(st->rid):NULL;
  2833. if(sd){
  2834. if(sd->st != st){
  2835. bk_st = sd->st;
  2836. bk_npcid = sd->npc_id;
  2837. }
  2838. sd->st = st;
  2839. sd->npc_id = st->oid;
  2840. }
  2841. if(st->state == RERUNLINE) {
  2842. st->state = RUN;
  2843. run_func(st);
  2844. if(st->state == GOTO)
  2845. st->state = RUN;
  2846. } else if(st->state != END)
  2847. st->state = RUN;
  2848. while(st->state == RUN){
  2849. c= get_com(st->script->script_buf,&st->pos);
  2850. switch(c){
  2851. case C_EOL:
  2852. if( stack->sp != stack->defsp )
  2853. {
  2854. if( stack->sp > stack->defsp )
  2855. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2856. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2857. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2858. } else if( battle_config.error_log )
  2859. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2860. stack->sp = stack->defsp;
  2861. }
  2862. break;
  2863. case C_INT:
  2864. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2865. break;
  2866. case C_POS:
  2867. case C_NAME:
  2868. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2869. st->pos+=3;
  2870. break;
  2871. case C_ARG:
  2872. push_val(stack,c,0);
  2873. break;
  2874. case C_STR:
  2875. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2876. while(st->script->script_buf[st->pos++]);
  2877. break;
  2878. case C_FUNC:
  2879. run_func(st);
  2880. if(st->state==GOTO){
  2881. st->state = RUN;
  2882. if( gotocount>0 && (--gotocount)<=0 ){
  2883. ShowError("run_script: infinity loop !\n");
  2884. script_reportsrc(st);
  2885. st->state=END;
  2886. }
  2887. }
  2888. break;
  2889. case C_NEG:
  2890. case C_NOT:
  2891. case C_LNOT:
  2892. op_1(st ,c);
  2893. break;
  2894. case C_ADD:
  2895. case C_SUB:
  2896. case C_MUL:
  2897. case C_DIV:
  2898. case C_MOD:
  2899. case C_EQ:
  2900. case C_NE:
  2901. case C_GT:
  2902. case C_GE:
  2903. case C_LT:
  2904. case C_LE:
  2905. case C_AND:
  2906. case C_OR:
  2907. case C_XOR:
  2908. case C_LAND:
  2909. case C_LOR:
  2910. case C_R_SHIFT:
  2911. case C_L_SHIFT:
  2912. op_2(st, c);
  2913. break;
  2914. case C_OP3:
  2915. op_3(st, c);
  2916. break;
  2917. case C_NOP:
  2918. st->state=END;
  2919. break;
  2920. default:
  2921. if(battle_config.error_log)
  2922. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2923. st->state=END;
  2924. break;
  2925. }
  2926. if( cmdcount>0 && (--cmdcount)<=0 ){
  2927. ShowError("run_script: infinity loop !\n");
  2928. script_reportsrc(st);
  2929. st->state=END;
  2930. }
  2931. }
  2932. if(st->sleep.tick > 0) {
  2933. //Delay execution
  2934. st->sleep.charid = sd?sd->status.char_id:0;
  2935. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2936. run_script_timer, st->sleep.charid, (int)st);
  2937. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2938. //Restore previous script
  2939. if (sd) {
  2940. sd->st = bk_st;
  2941. sd->npc_id = bk_npcid;
  2942. bk_st = NULL; //Remove tag for removal.
  2943. }
  2944. }
  2945. else if(st->state != END && sd){
  2946. //Resume later (st is already attached to player).
  2947. if(bk_st) {
  2948. ShowWarning("Unable to restore stack! Double continuation!\n");
  2949. //Report BOTH scripts to see if that can help somehow.
  2950. ShowDebug("Previous script (lost):\n");
  2951. script_reportsrc(bk_st);
  2952. ShowDebug("Current script:\n");
  2953. script_reportsrc(st);
  2954. }
  2955. } else {
  2956. //Dispose of script.
  2957. if (sd)
  2958. { //Restore previous stack and save char.
  2959. if(sd->state.using_fake_npc){
  2960. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2961. sd->state.using_fake_npc = 0;
  2962. }
  2963. //Restore previous script if any.
  2964. sd->st = bk_st;
  2965. sd->npc_id = bk_npcid;
  2966. if (!bk_st)
  2967. npc_event_dequeue(sd);
  2968. else
  2969. bk_st = NULL; //Remove tag for removal.
  2970. if (sd->state.reg_dirty&2)
  2971. intif_saveregistry(sd,2);
  2972. if (sd->state.reg_dirty&1)
  2973. intif_saveregistry(sd,1);
  2974. }
  2975. st->pos = -1;
  2976. script_free_stack (st->stack);
  2977. aFree(st);
  2978. }
  2979. if (bk_st)
  2980. { //Remove previous script
  2981. bk_st->pos = -1;
  2982. script_free_stack(bk_st->stack);
  2983. aFree(bk_st);
  2984. bk_st = NULL;
  2985. }
  2986. }
  2987. /*==========================================
  2988. * マップ変数の変更
  2989. *------------------------------------------*/
  2990. int mapreg_setreg(int num, int val)
  2991. {
  2992. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2993. int i = num >> 24;
  2994. char* name = str_buf + str_data[num&0x00ffffff].str;
  2995. if( val != 0 ) {
  2996. if(idb_put(mapreg_db,num,(void*)val))
  2997. ;
  2998. else if(name[1] != '@') {
  2999. char tmp_str[32*2+1];
  3000. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  3001. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%d')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, val) )
  3002. Sql_ShowDebug(mmysql_handle);
  3003. }
  3004. } else { // val == 0
  3005. if(name[1] != '@') { // Remove from database because it is unused.
  3006. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3007. Sql_ShowDebug(mmysql_handle);
  3008. }
  3009. idb_remove(mapreg_db,num);
  3010. }
  3011. #else
  3012. if(val != 0)
  3013. idb_put(mapreg_db,num,(void*)val);
  3014. else
  3015. idb_remove(mapreg_db,num);
  3016. #endif
  3017. mapreg_dirty = 1;
  3018. return 1;
  3019. }
  3020. /*==========================================
  3021. * 文字列型マップ変数の変更
  3022. *------------------------------------------*/
  3023. int mapreg_setregstr(int num, const char* str)
  3024. {
  3025. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  3026. int i = num >> 24;
  3027. char* name = str_buf + str_data[num&0x00ffffff].str;
  3028. if( str==NULL || *str==0 ) {
  3029. if(name[1] != '@') {
  3030. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3031. Sql_ShowDebug(mmysql_handle);
  3032. }
  3033. idb_remove(mapregstr_db,num);
  3034. mapreg_dirty = 1;
  3035. return 1;
  3036. }
  3037. if (idb_put(mapregstr_db,num, aStrdup(str)))
  3038. ;
  3039. else if(name[1] != '@') { //put returned null, so we must insert.
  3040. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  3041. char tmp_str[32*2+1];
  3042. char tmp_str2[255*2+1];
  3043. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  3044. Sql_EscapeStringLen(mmysql_handle, tmp_str2, str, strnlen(str, 255));
  3045. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%s')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, tmp_str2) )
  3046. Sql_ShowDebug(mmysql_handle);
  3047. }
  3048. #else
  3049. if( str==NULL || *str==0 )
  3050. idb_remove(mapregstr_db,num);
  3051. else
  3052. idb_put(mapregstr_db,num,aStrdup(str));
  3053. #endif
  3054. mapreg_dirty = 1;
  3055. return 1;
  3056. }
  3057. /*==========================================
  3058. * 永続的マップ変数の読み込み
  3059. *------------------------------------------*/
  3060. static int script_load_mapreg(void)
  3061. {
  3062. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3063. FILE* fp;
  3064. char line[1024];
  3065. if( (fp=fopen(mapreg_txt,"rt")) == NULL )
  3066. return -1;
  3067. while(fgets(line,sizeof(line),fp))
  3068. {
  3069. char buf1[256],buf2[1024];
  3070. char* p;
  3071. int n,v,s,i;
  3072. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  3073. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  3074. continue;
  3075. if( buf1[strlen(buf1)-1] == '$' ) {
  3076. if( sscanf(line + n, "%[^\n\r]", buf2) != 1 ) {
  3077. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3078. continue;
  3079. }
  3080. p = aStrdup(buf2);
  3081. s = add_str(buf1);
  3082. idb_put(mapregstr_db, (i<<24)|s, p);
  3083. } else {
  3084. if( sscanf(line + n, "%d", &v) != 1 ) {
  3085. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3086. continue;
  3087. }
  3088. s = add_str(buf1);
  3089. idb_put(mapreg_db, (i<<24)|s, (void*)v);
  3090. }
  3091. }
  3092. fclose(fp);
  3093. mapreg_dirty = 0;
  3094. return 0;
  3095. #else
  3096. /*
  3097. 0 1 2
  3098. +-------------------------+
  3099. | varname | index | value |
  3100. +-------------------------+
  3101. */
  3102. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  3103. char varname[32+1];
  3104. int index;
  3105. char value[255+1];
  3106. uint32 length;
  3107. if ( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `%s`, `%s`, `%s` FROM `%s`", mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, mapregsql_db)
  3108. || SQL_ERROR == SqlStmt_Execute(stmt)
  3109. ) {
  3110. SqlStmt_ShowDebug(stmt);
  3111. SqlStmt_Free(stmt);
  3112. return -1;
  3113. }
  3114. SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &varname[0], 32, &length, NULL);
  3115. SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
  3116. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &value[0], 255, NULL, NULL);
  3117. while ( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  3118. {
  3119. if( varname[length-1] == '$' ) {
  3120. int s = add_str(varname);
  3121. int i = index;
  3122. char* p = aStrdup(value);
  3123. idb_put(mapregstr_db, (i<<24)|s, p);
  3124. } else {
  3125. int s = add_str(varname);
  3126. int i = index;
  3127. int v = atoi(value);
  3128. idb_put(mapreg_db, (i<<24)|s, (void *)v);
  3129. }
  3130. }
  3131. SqlStmt_Free(stmt);
  3132. mapreg_dirty = 0;
  3133. return 0;
  3134. #endif /* TXT_ONLY */
  3135. }
  3136. /*==========================================
  3137. * 永続的マップ変数の書き込み
  3138. *------------------------------------------*/
  3139. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  3140. {
  3141. int num=key.i&0x00ffffff, i=key.i>>24;
  3142. char *name=str_buf+str_data[num].str;
  3143. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3144. FILE *fp=va_arg(ap,FILE*);
  3145. if( name[1]!='@' ) {
  3146. if(i==0)
  3147. fprintf(fp,"%s\t%d\n", name, (int)data);
  3148. else
  3149. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  3150. }
  3151. #else
  3152. if ( name[1] != '@') {
  3153. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, (int)data, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3154. Sql_ShowDebug(mmysql_handle);
  3155. }
  3156. #endif
  3157. return 0;
  3158. }
  3159. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  3160. {
  3161. int num=key.i&0x00ffffff, i=key.i>>24;
  3162. char *name=str_buf+str_data[num].str;
  3163. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3164. FILE *fp=va_arg(ap,FILE*);
  3165. if( name[1]!='@' ) {
  3166. if(i==0)
  3167. fprintf(fp,"%s\t%s\n", name, (char *)data);
  3168. else
  3169. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  3170. }
  3171. #else
  3172. if ( name[1] != '@') {
  3173. char tmp_str2[2*255+1];
  3174. Sql_EscapeStringLen(mmysql_handle, tmp_str2, (char*)data, safestrnlen((char*)data, 255));
  3175. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%s' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, tmp_str2, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3176. Sql_ShowDebug(mmysql_handle);
  3177. }
  3178. #endif
  3179. return 0;
  3180. }
  3181. static int script_save_mapreg(void)
  3182. {
  3183. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3184. FILE *fp;
  3185. int lock;
  3186. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  3187. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  3188. return -1;
  3189. }
  3190. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  3191. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  3192. lock_fclose(fp,mapreg_txt,&lock);
  3193. #else
  3194. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub);
  3195. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  3196. #endif
  3197. mapreg_dirty=0;
  3198. return 0;
  3199. }
  3200. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  3201. {
  3202. if(mapreg_dirty)
  3203. if (script_save_mapreg() == -1)
  3204. ShowError("Failed to save the mapreg data!\n");
  3205. return 0;
  3206. }
  3207. int script_config_read_sub(char *cfgName)
  3208. {
  3209. int i;
  3210. char line[1024],w1[1024],w2[1024];
  3211. FILE *fp;
  3212. fp=fopen(cfgName,"r");
  3213. if(fp==NULL){
  3214. ShowError("file not found: [%s]\n", cfgName);
  3215. return 1;
  3216. }
  3217. while(fgets(line, sizeof(line), fp))
  3218. {
  3219. if(line[0] == '/' && line[1] == '/')
  3220. continue;
  3221. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3222. if(i!=2)
  3223. continue;
  3224. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3225. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3226. }
  3227. else if(strcmpi(w1,"check_cmdcount")==0) {
  3228. script_config.check_cmdcount = config_switch(w2);
  3229. }
  3230. else if(strcmpi(w1,"check_gotocount")==0) {
  3231. script_config.check_gotocount = config_switch(w2);
  3232. }
  3233. else if(strcmpi(w1,"event_script_type")==0) {
  3234. script_config.event_script_type = config_switch(w2);
  3235. }
  3236. else if(strcmpi(w1,"event_requires_trigger")==0) {
  3237. script_config.event_requires_trigger = config_switch(w2);
  3238. }
  3239. else if(strcmpi(w1,"die_event_name")==0) {
  3240. strncpy(script_config.die_event_name, w2, NAME_LENGTH-1);
  3241. if (strlen(script_config.die_event_name) != strlen(w2))
  3242. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.die_event_name);
  3243. }
  3244. else if(strcmpi(w1,"kill_pc_event_name")==0) {
  3245. strncpy(script_config.kill_pc_event_name, w2, NAME_LENGTH-1);
  3246. if (strlen(script_config.kill_pc_event_name) != strlen(w2))
  3247. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_pc_event_name);
  3248. }
  3249. else if(strcmpi(w1,"kill_mob_event_name")==0) {
  3250. strncpy(script_config.kill_mob_event_name, w2, NAME_LENGTH-1);
  3251. if (strlen(script_config.kill_mob_event_name) != strlen(w2))
  3252. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_mob_event_name);
  3253. }
  3254. else if(strcmpi(w1,"login_event_name")==0) {
  3255. strncpy(script_config.login_event_name, w2, NAME_LENGTH-1);
  3256. if (strlen(script_config.login_event_name) != strlen(w2))
  3257. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.login_event_name);
  3258. }
  3259. else if(strcmpi(w1,"logout_event_name")==0) {
  3260. strncpy(script_config.logout_event_name, w2, NAME_LENGTH-1);
  3261. if (strlen(script_config.logout_event_name) != strlen(w2))
  3262. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.logout_event_name);
  3263. }
  3264. else if(strcmpi(w1,"loadmap_event_name")==0) {
  3265. strncpy(script_config.loadmap_event_name, w2, NAME_LENGTH-1);
  3266. if (strlen(script_config.loadmap_event_name) != strlen(w2))
  3267. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.loadmap_event_name);
  3268. }
  3269. else if(strcmpi(w1,"baselvup_event_name")==0) {
  3270. strncpy(script_config.baselvup_event_name, w2, NAME_LENGTH-1);
  3271. if (strlen(script_config.baselvup_event_name) != strlen(w2))
  3272. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.baselvup_event_name);
  3273. }
  3274. else if(strcmpi(w1,"joblvup_event_name")==0) {
  3275. strncpy(script_config.joblvup_event_name, w2, NAME_LENGTH-1);
  3276. if (strlen(script_config.joblvup_event_name) != strlen(w2))
  3277. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.joblvup_event_name);
  3278. }
  3279. else if(strcmpi(w1,"import")==0){
  3280. script_config_read_sub(w2);
  3281. }
  3282. }
  3283. fclose(fp);
  3284. return 0;
  3285. }
  3286. int script_config_read(char *cfgName)
  3287. { //Script related variables should be initialized once! [Skotlex]
  3288. memset (&script_config, 0, sizeof(script_config));
  3289. script_config.warn_func_mismatch_paramnum = 1;
  3290. script_config.check_cmdcount = 65535;
  3291. script_config.check_gotocount = 2048;
  3292. script_config.event_script_type = 0;
  3293. script_config.event_requires_trigger = 1;
  3294. return script_config_read_sub(cfgName);
  3295. }
  3296. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3297. {
  3298. struct script_code *code = (struct script_code *)data;
  3299. if(code){
  3300. script_free_vars( &code->script_vars );
  3301. aFree( code->script_buf );
  3302. aFree( code );
  3303. }
  3304. return 0;
  3305. }
  3306. /*==========================================
  3307. * 終了
  3308. *------------------------------------------*/
  3309. int do_final_script()
  3310. {
  3311. #ifdef DEBUG_HASH
  3312. if (battle_config.etc_log)
  3313. {
  3314. FILE *fp = fopen("hash_dump.txt","wt");
  3315. if(fp) {
  3316. int i,count[SCRIPT_HASH_SIZE];
  3317. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3318. int n=0;
  3319. int min=INT_MAX,max=0,zero=0;
  3320. double mean=0.0f;
  3321. double median=0.0f;
  3322. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3323. memset(count, 0, sizeof(count));
  3324. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3325. fprintf(fp,"---------------------------------------------------------------\n");
  3326. for(i=LABEL_START; i<str_num; i++) {
  3327. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3328. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3329. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3330. ++count[h];
  3331. }
  3332. fprintf(fp,"--------------------\n\n");
  3333. memset(count2, 0, sizeof(count2));
  3334. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3335. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3336. if(min > count[i])
  3337. min = count[i]; // minimun count of collision
  3338. if(max < count[i])
  3339. max = count[i]; // maximun count of collision
  3340. if(count[i] == 0)
  3341. zero++;
  3342. ++count2[count[i]];
  3343. }
  3344. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3345. for( i=min; i <= max; ++i ){
  3346. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3347. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3348. }
  3349. for( i=min; i <= max; ++i ){
  3350. n += count2[i];
  3351. if( n*2 >= SCRIPT_HASH_SIZE )
  3352. {
  3353. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3354. median = (i+i+1)/2.0f;
  3355. else
  3356. median = i;
  3357. break;
  3358. }
  3359. }
  3360. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3361. fclose(fp);
  3362. }
  3363. }
  3364. #endif
  3365. if(mapreg_dirty>=0)
  3366. script_save_mapreg();
  3367. mapreg_db->destroy(mapreg_db,NULL);
  3368. mapregstr_db->destroy(mapregstr_db,NULL);
  3369. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3370. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3371. if(sleep_db) {
  3372. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3373. while(n) {
  3374. struct script_state *st = (struct script_state *)n->data;
  3375. script_free_stack(st->stack);
  3376. aFree(st);
  3377. n = n->next;
  3378. }
  3379. linkdb_final(&sleep_db);
  3380. }
  3381. if (str_data)
  3382. aFree(str_data);
  3383. if (str_buf)
  3384. aFree(str_buf);
  3385. return 0;
  3386. }
  3387. /*==========================================
  3388. * 初期化
  3389. *------------------------------------------*/
  3390. int do_init_script()
  3391. {
  3392. mapreg_db= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_BASE,sizeof(int));
  3393. mapregstr_db=db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3394. userfunc_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_DUP_KEY,0);
  3395. scriptlabel_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA,50);
  3396. script_load_mapreg();
  3397. add_timer_func_list(script_autosave_mapreg, "script_autosave_mapreg");
  3398. add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL, script_autosave_mapreg, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
  3399. return 0;
  3400. }
  3401. int script_reload()
  3402. {
  3403. if(mapreg_dirty>=0)
  3404. script_save_mapreg();
  3405. mapreg_db->clear(mapreg_db, NULL);
  3406. mapregstr_db->clear(mapregstr_db, NULL);
  3407. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3408. scriptlabel_db->clear(scriptlabel_db, NULL);
  3409. if(sleep_db) {
  3410. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3411. while(n) {
  3412. struct script_state *st = (struct script_state *)n->data;
  3413. if( st->sleep.timer != INVALID_TIMER )
  3414. delete_timer(st->sleep.timer, run_script_timer);
  3415. script_free_stack(st->stack);
  3416. aFree(st);
  3417. n = n->next;
  3418. }
  3419. linkdb_final(&sleep_db);
  3420. }
  3421. script_load_mapreg();
  3422. return 0;
  3423. }
  3424. //-----------------------------------------------------------------------------
  3425. // buildin functions
  3426. //
  3427. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3428. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3429. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3430. BUILDIN_FUNC(mes);
  3431. BUILDIN_FUNC(goto);
  3432. BUILDIN_FUNC(callsub);
  3433. BUILDIN_FUNC(callfunc);
  3434. BUILDIN_FUNC(return);
  3435. BUILDIN_FUNC(getarg);
  3436. BUILDIN_FUNC(next);
  3437. BUILDIN_FUNC(close);
  3438. BUILDIN_FUNC(close2);
  3439. BUILDIN_FUNC(menu);
  3440. BUILDIN_FUNC(rand);
  3441. BUILDIN_FUNC(warp);
  3442. BUILDIN_FUNC(areawarp);
  3443. BUILDIN_FUNC(warpchar); // [LuzZza]
  3444. BUILDIN_FUNC(warpparty); //[Fredzilla]
  3445. BUILDIN_FUNC(warpguild); //[Fredzilla]
  3446. BUILDIN_FUNC(heal);
  3447. BUILDIN_FUNC(itemheal);
  3448. BUILDIN_FUNC(percentheal);
  3449. BUILDIN_FUNC(jobchange);
  3450. BUILDIN_FUNC(jobname);
  3451. BUILDIN_FUNC(input);
  3452. BUILDIN_FUNC(setlook);
  3453. BUILDIN_FUNC(set);
  3454. BUILDIN_FUNC(setarray);
  3455. BUILDIN_FUNC(cleararray);
  3456. BUILDIN_FUNC(copyarray);
  3457. BUILDIN_FUNC(getarraysize);
  3458. BUILDIN_FUNC(deletearray);
  3459. BUILDIN_FUNC(getelementofarray);
  3460. BUILDIN_FUNC(getitem);
  3461. BUILDIN_FUNC(getitem2);
  3462. BUILDIN_FUNC(getnameditem);
  3463. BUILDIN_FUNC(grouprandomitem);
  3464. BUILDIN_FUNC(makeitem);
  3465. BUILDIN_FUNC(delitem);
  3466. BUILDIN_FUNC(delitem2);
  3467. BUILDIN_FUNC(enableitemuse);
  3468. BUILDIN_FUNC(disableitemuse);
  3469. BUILDIN_FUNC(viewpoint);
  3470. BUILDIN_FUNC(countitem);
  3471. BUILDIN_FUNC(countitem2);
  3472. BUILDIN_FUNC(checkweight);
  3473. BUILDIN_FUNC(readparam);
  3474. BUILDIN_FUNC(getcharid);
  3475. BUILDIN_FUNC(getpartyname);
  3476. BUILDIN_FUNC(getpartymember);
  3477. BUILDIN_FUNC(getpartyleader);
  3478. BUILDIN_FUNC(getguildname);
  3479. BUILDIN_FUNC(getguildmaster);
  3480. BUILDIN_FUNC(getguildmasterid);
  3481. BUILDIN_FUNC(strcharinfo);
  3482. BUILDIN_FUNC(getequipid);
  3483. BUILDIN_FUNC(getequipname);
  3484. BUILDIN_FUNC(getbrokenid); // [Valaris]
  3485. BUILDIN_FUNC(repair); // [Valaris]
  3486. BUILDIN_FUNC(getequipisequiped);
  3487. BUILDIN_FUNC(getequipisenableref);
  3488. BUILDIN_FUNC(getequipisidentify);
  3489. BUILDIN_FUNC(getequiprefinerycnt);
  3490. BUILDIN_FUNC(getequipweaponlv);
  3491. BUILDIN_FUNC(getequippercentrefinery);
  3492. BUILDIN_FUNC(successrefitem);
  3493. BUILDIN_FUNC(failedrefitem);
  3494. BUILDIN_FUNC(cutin);
  3495. BUILDIN_FUNC(statusup);
  3496. BUILDIN_FUNC(statusup2);
  3497. BUILDIN_FUNC(bonus);
  3498. BUILDIN_FUNC(bonus2);
  3499. BUILDIN_FUNC(bonus3);
  3500. BUILDIN_FUNC(bonus4);
  3501. BUILDIN_FUNC(bonus5);
  3502. BUILDIN_FUNC(bonusautoscript);
  3503. BUILDIN_FUNC(bonusautoscript2);
  3504. BUILDIN_FUNC(skill);
  3505. BUILDIN_FUNC(addtoskill); // [Valaris]
  3506. BUILDIN_FUNC(guildskill);
  3507. BUILDIN_FUNC(getskilllv);
  3508. BUILDIN_FUNC(getgdskilllv);
  3509. BUILDIN_FUNC(basicskillcheck);
  3510. BUILDIN_FUNC(getgmlevel);
  3511. BUILDIN_FUNC(end);
  3512. BUILDIN_FUNC(checkoption);
  3513. BUILDIN_FUNC(setoption);
  3514. BUILDIN_FUNC(setcart);
  3515. BUILDIN_FUNC(checkcart); // check cart [Valaris]
  3516. BUILDIN_FUNC(setfalcon);
  3517. BUILDIN_FUNC(checkfalcon); // check falcon [Valaris]
  3518. BUILDIN_FUNC(setriding);
  3519. BUILDIN_FUNC(checkriding); // check for pecopeco [Valaris]
  3520. BUILDIN_FUNC(savepoint);
  3521. BUILDIN_FUNC(gettimetick);
  3522. BUILDIN_FUNC(gettime);
  3523. BUILDIN_FUNC(gettimestr);
  3524. BUILDIN_FUNC(openstorage);
  3525. BUILDIN_FUNC(guildopenstorage);
  3526. BUILDIN_FUNC(itemskill);
  3527. BUILDIN_FUNC(produce);
  3528. BUILDIN_FUNC(monster);
  3529. BUILDIN_FUNC(areamonster);
  3530. BUILDIN_FUNC(killmonster);
  3531. BUILDIN_FUNC(killmonsterall);
  3532. BUILDIN_FUNC(clone);
  3533. BUILDIN_FUNC(doevent);
  3534. BUILDIN_FUNC(donpcevent);
  3535. BUILDIN_FUNC(cmdothernpc);
  3536. BUILDIN_FUNC(addtimer);
  3537. BUILDIN_FUNC(deltimer);
  3538. BUILDIN_FUNC(addtimercount);
  3539. BUILDIN_FUNC(initnpctimer);
  3540. BUILDIN_FUNC(stopnpctimer);
  3541. BUILDIN_FUNC(startnpctimer);
  3542. BUILDIN_FUNC(setnpctimer);
  3543. BUILDIN_FUNC(getnpctimer);
  3544. BUILDIN_FUNC(attachnpctimer); // [celest]
  3545. BUILDIN_FUNC(detachnpctimer); // [celest]
  3546. BUILDIN_FUNC(playerattached); // [Skotlex]
  3547. BUILDIN_FUNC(announce);
  3548. BUILDIN_FUNC(mapannounce);
  3549. BUILDIN_FUNC(areaannounce);
  3550. BUILDIN_FUNC(getusers);
  3551. BUILDIN_FUNC(getmapguildusers);
  3552. BUILDIN_FUNC(getmapusers);
  3553. BUILDIN_FUNC(getareausers);
  3554. BUILDIN_FUNC(getareadropitem);
  3555. BUILDIN_FUNC(enablenpc);
  3556. BUILDIN_FUNC(disablenpc);
  3557. BUILDIN_FUNC(hideoffnpc);
  3558. BUILDIN_FUNC(hideonnpc);
  3559. BUILDIN_FUNC(sc_start);
  3560. BUILDIN_FUNC(sc_start2);
  3561. BUILDIN_FUNC(sc_start4);
  3562. BUILDIN_FUNC(sc_end);
  3563. BUILDIN_FUNC(getscrate);
  3564. BUILDIN_FUNC(debugmes);
  3565. BUILDIN_FUNC(catchpet);
  3566. BUILDIN_FUNC(birthpet);
  3567. BUILDIN_FUNC(resetlvl);
  3568. BUILDIN_FUNC(resetstatus);
  3569. BUILDIN_FUNC(resetskill);
  3570. BUILDIN_FUNC(skillpointcount);
  3571. BUILDIN_FUNC(changebase);
  3572. BUILDIN_FUNC(changesex);
  3573. BUILDIN_FUNC(waitingroom);
  3574. BUILDIN_FUNC(delwaitingroom);
  3575. BUILDIN_FUNC(waitingroomkickall);
  3576. BUILDIN_FUNC(enablewaitingroomevent);
  3577. BUILDIN_FUNC(disablewaitingroomevent);
  3578. BUILDIN_FUNC(getwaitingroomstate);
  3579. BUILDIN_FUNC(warpwaitingpc);
  3580. BUILDIN_FUNC(enablearena); // Added by RoVeRT
  3581. BUILDIN_FUNC(disablearena); // Added by RoVeRT
  3582. BUILDIN_FUNC(attachrid);
  3583. BUILDIN_FUNC(detachrid);
  3584. BUILDIN_FUNC(isloggedin);
  3585. BUILDIN_FUNC(setmapflagnosave);
  3586. BUILDIN_FUNC(setmapflag);
  3587. BUILDIN_FUNC(removemapflag);
  3588. BUILDIN_FUNC(pvpon);
  3589. BUILDIN_FUNC(pvpoff);
  3590. BUILDIN_FUNC(gvgon);
  3591. BUILDIN_FUNC(gvgoff);
  3592. BUILDIN_FUNC(emotion);
  3593. BUILDIN_FUNC(maprespawnguildid);
  3594. BUILDIN_FUNC(agitstart); // <Agit>
  3595. BUILDIN_FUNC(agitend);
  3596. BUILDIN_FUNC(agitcheck); // <Agitcheck>
  3597. BUILDIN_FUNC(flagemblem); // Flag Emblem
  3598. BUILDIN_FUNC(getcastlename);
  3599. BUILDIN_FUNC(getcastledata);
  3600. BUILDIN_FUNC(setcastledata);
  3601. BUILDIN_FUNC(requestguildinfo);
  3602. BUILDIN_FUNC(getequipcardcnt);
  3603. BUILDIN_FUNC(successremovecards);
  3604. BUILDIN_FUNC(failedremovecards);
  3605. BUILDIN_FUNC(marriage);
  3606. BUILDIN_FUNC(wedding_effect);
  3607. BUILDIN_FUNC(divorce);
  3608. BUILDIN_FUNC(ispartneron); // MouseJstr
  3609. BUILDIN_FUNC(getpartnerid); // MouseJstr
  3610. BUILDIN_FUNC(getchildid); // Skotlex
  3611. BUILDIN_FUNC(getmotherid); // Lupus
  3612. BUILDIN_FUNC(getfatherid); // Lupus
  3613. BUILDIN_FUNC(warppartner); // MouseJstr
  3614. BUILDIN_FUNC(getitemname);
  3615. BUILDIN_FUNC(getitemslots);
  3616. BUILDIN_FUNC(makepet);
  3617. BUILDIN_FUNC(getexp);
  3618. BUILDIN_FUNC(getinventorylist);
  3619. BUILDIN_FUNC(getskilllist);
  3620. BUILDIN_FUNC(clearitem);
  3621. BUILDIN_FUNC(classchange);
  3622. BUILDIN_FUNC(misceffect);
  3623. BUILDIN_FUNC(soundeffect);
  3624. BUILDIN_FUNC(soundeffectall);
  3625. BUILDIN_FUNC(setcastledata);
  3626. BUILDIN_FUNC(mapwarp);
  3627. BUILDIN_FUNC(mobcount);
  3628. BUILDIN_FUNC(strmobinfo); // Script for displaying mob info [Valaris]
  3629. BUILDIN_FUNC(guardian); // Script for displaying mob info [Valaris]
  3630. BUILDIN_FUNC(guardianinfo); // Script for displaying mob info [Valaris]
  3631. BUILDIN_FUNC(petskillbonus); // petskillbonus [Valaris]
  3632. BUILDIN_FUNC(petrecovery); // pet skill for curing status [Valaris]
  3633. BUILDIN_FUNC(petloot); // pet looting [Valaris]
  3634. BUILDIN_FUNC(petheal); // pet healing [Valaris]
  3635. //BUILDIN_FUNC(petmag); // pet magnificat [Valaris]
  3636. BUILDIN_FUNC(petskillattack); // pet skill attacks [Skotlex]
  3637. BUILDIN_FUNC(petskillattack2); // pet skill attacks [Skotlex]
  3638. BUILDIN_FUNC(petskillsupport); // pet support skill [Valaris]
  3639. BUILDIN_FUNC(skilleffect); // skill effects [Celest]
  3640. BUILDIN_FUNC(npcskilleffect); // skill effects for npcs [Valaris]
  3641. BUILDIN_FUNC(specialeffect); // special effect script [Valaris]
  3642. BUILDIN_FUNC(specialeffect2); // special effect script [Valaris]
  3643. BUILDIN_FUNC(nude); // nude [Valaris]
  3644. BUILDIN_FUNC(atcommand); // [MouseJstr]
  3645. BUILDIN_FUNC(charcommand); // [MouseJstr]
  3646. BUILDIN_FUNC(movenpc); // [MouseJstr]
  3647. BUILDIN_FUNC(message); // [MouseJstr]
  3648. BUILDIN_FUNC(npctalk); // [Valaris]
  3649. BUILDIN_FUNC(getlook); //Lorky [Lupus]
  3650. BUILDIN_FUNC(getsavepoint); //Lorky [Lupus]
  3651. BUILDIN_FUNC(npcspeed); // [Valaris]
  3652. BUILDIN_FUNC(npcwalkto); // [Valaris]
  3653. BUILDIN_FUNC(npcstop); // [Valaris]
  3654. BUILDIN_FUNC(getmapxy); //get map position for player/npc/pet/mob by Lorky [Lupus]
  3655. BUILDIN_FUNC(checkoption1); // [celest]
  3656. BUILDIN_FUNC(checkoption2); // [celest]
  3657. BUILDIN_FUNC(guildgetexp); // [celest]
  3658. BUILDIN_FUNC(guildchangegm); // [Skotlex]
  3659. BUILDIN_FUNC(logmes); // [Lupus]
  3660. BUILDIN_FUNC(summon); // [celest]
  3661. BUILDIN_FUNC(isnight); // [celest]
  3662. BUILDIN_FUNC(isday); // [celest]
  3663. BUILDIN_FUNC(isequipped); // [celest]
  3664. BUILDIN_FUNC(isequippedcnt); // [celest]
  3665. BUILDIN_FUNC(cardscnt); // [Lupus]
  3666. BUILDIN_FUNC(getrefine); // [celest]
  3667. BUILDIN_FUNC(adopt);
  3668. BUILDIN_FUNC(night);
  3669. BUILDIN_FUNC(day);
  3670. BUILDIN_FUNC(getusersname); //jA commands added [Lupus]
  3671. BUILDIN_FUNC(dispbottom);
  3672. BUILDIN_FUNC(recovery);
  3673. BUILDIN_FUNC(getpetinfo);
  3674. BUILDIN_FUNC(gethominfo);
  3675. BUILDIN_FUNC(checkequipedcard);
  3676. BUILDIN_FUNC(globalmes);
  3677. BUILDIN_FUNC(jump_zero);
  3678. BUILDIN_FUNC(select);
  3679. BUILDIN_FUNC(prompt);
  3680. BUILDIN_FUNC(getmapmobs); //jA addition end
  3681. BUILDIN_FUNC(unequip); // unequip [Spectre]
  3682. BUILDIN_FUNC(getstrlen); //strlen [valaris]
  3683. BUILDIN_FUNC(charisalpha);//isalpha [valaris]
  3684. BUILDIN_FUNC(fakenpcname); // [Lance]
  3685. BUILDIN_FUNC(compare); // Lordalfa, to bring strstr to Scripting Engine
  3686. BUILDIN_FUNC(getiteminfo); //[Lupus] returns Items Buy / sell Price, etc info
  3687. BUILDIN_FUNC(setiteminfo); //[Lupus] set Items Buy / sell Price, etc info
  3688. BUILDIN_FUNC(getequipcardid); //[Lupus] returns card id from quipped item card slot N
  3689. // [zBuffer] List of mathematics commands --->
  3690. BUILDIN_FUNC(sqrt);
  3691. BUILDIN_FUNC(pow);
  3692. BUILDIN_FUNC(distance);
  3693. BUILDIN_FUNC(checkcell);
  3694. // <--- [zBuffer] List of mathematics commands
  3695. // [zBuffer] List of dynamic var commands --->
  3696. BUILDIN_FUNC(getd);
  3697. BUILDIN_FUNC(setd);
  3698. // <--- [zBuffer] List of dynamic var commands
  3699. BUILDIN_FUNC(petstat); // [Lance] Pet Stat Rq: Dubby
  3700. BUILDIN_FUNC(callshop); // [Skotlex]
  3701. BUILDIN_FUNC(npcshopitem); // [Lance]
  3702. BUILDIN_FUNC(npcshopadditem);
  3703. BUILDIN_FUNC(npcshopdelitem);
  3704. BUILDIN_FUNC(npcshopattach);
  3705. BUILDIN_FUNC(equip);
  3706. BUILDIN_FUNC(autoequip);
  3707. BUILDIN_FUNC(setbattleflag);
  3708. BUILDIN_FUNC(getbattleflag);
  3709. BUILDIN_FUNC(query_sql);
  3710. BUILDIN_FUNC(escape_sql);
  3711. BUILDIN_FUNC(atoi);
  3712. BUILDIN_FUNC(axtoi);
  3713. // [zBuffer] List of player cont commands --->
  3714. BUILDIN_FUNC(rid2name);
  3715. BUILDIN_FUNC(pcfollow);
  3716. BUILDIN_FUNC(pcstopfollow);
  3717. BUILDIN_FUNC(pcblockmove);
  3718. // <--- [zBuffer] List of player cont commands
  3719. // [zBuffer] List of mob control commands --->
  3720. BUILDIN_FUNC(mobspawn);
  3721. BUILDIN_FUNC(mobremove);
  3722. BUILDIN_FUNC(getmobdata);
  3723. BUILDIN_FUNC(setmobdata);
  3724. BUILDIN_FUNC(mobassist);
  3725. BUILDIN_FUNC(mobattach);
  3726. BUILDIN_FUNC(unitwalk);
  3727. BUILDIN_FUNC(unitkill);
  3728. BUILDIN_FUNC(unitwarp);
  3729. BUILDIN_FUNC(unitattack);
  3730. BUILDIN_FUNC(unitstop);
  3731. BUILDIN_FUNC(unittalk);
  3732. BUILDIN_FUNC(unitemote);
  3733. BUILDIN_FUNC(unitskilluseid); // originally by Qamera [celest]
  3734. BUILDIN_FUNC(unitskillusepos); // originally by Qamera [celest]
  3735. // <--- [zBuffer] List of mob control commands
  3736. BUILDIN_FUNC(sleep);
  3737. BUILDIN_FUNC(sleep2);
  3738. BUILDIN_FUNC(awake);
  3739. BUILDIN_FUNC(getvariableofnpc);
  3740. BUILDIN_FUNC(warpportal);
  3741. BUILDIN_FUNC(homunculus_evolution) ; //[orn]
  3742. BUILDIN_FUNC(homunculus_shuffle); // [Zephyrus]
  3743. BUILDIN_FUNC(eaclass);
  3744. BUILDIN_FUNC(roclass);
  3745. BUILDIN_FUNC(setitemscript);
  3746. BUILDIN_FUNC(disguise);
  3747. BUILDIN_FUNC(undisguise);
  3748. BUILDIN_FUNC(getmonsterinfo); // [Lupus]
  3749. BUILDIN_FUNC(checkvending); // check vending [Nab4]
  3750. BUILDIN_FUNC(checkchatting); // check chatting [Marka]
  3751. #ifndef TXT_ONLY
  3752. BUILDIN_FUNC(openmail); // [Mail System]
  3753. #endif
  3754. #ifdef PCRE_SUPPORT
  3755. BUILDIN_FUNC(defpattern); // MouseJstr
  3756. BUILDIN_FUNC(activatepset); // MouseJstr
  3757. BUILDIN_FUNC(deactivatepset); // MouseJstr
  3758. BUILDIN_FUNC(deletepset); // MouseJstr
  3759. #endif
  3760. struct script_function buildin_func[] = {
  3761. // NPC interaction
  3762. BUILDIN_DEF(mes,"s"),
  3763. BUILDIN_DEF(next,""),
  3764. BUILDIN_DEF(close,""),
  3765. BUILDIN_DEF(close2,""),
  3766. BUILDIN_DEF(menu,"sl*"),
  3767. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  3768. BUILDIN_DEF(prompt,"s*"),
  3769. //
  3770. BUILDIN_DEF(goto,"l"),
  3771. BUILDIN_DEF(callsub,"i*"),
  3772. BUILDIN_DEF(callfunc,"s*"),
  3773. BUILDIN_DEF(return,"?"),
  3774. BUILDIN_DEF(getarg,"i?"),
  3775. BUILDIN_DEF(jobchange,"i*"),
  3776. BUILDIN_DEF(jobname,"i"),
  3777. BUILDIN_DEF(input,"v"),
  3778. BUILDIN_DEF(warp,"sii"),
  3779. BUILDIN_DEF(areawarp,"siiiisii"),
  3780. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  3781. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  3782. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  3783. BUILDIN_DEF(setlook,"ii"),
  3784. BUILDIN_DEF(set,"ii"),
  3785. BUILDIN_DEF(setarray,"rv*"),
  3786. BUILDIN_DEF(cleararray,"rvi"),
  3787. BUILDIN_DEF(copyarray,"rri"),
  3788. BUILDIN_DEF(getarraysize,"r"),
  3789. BUILDIN_DEF(deletearray,"r?"),
  3790. BUILDIN_DEF(getelementofarray,"ri"),
  3791. BUILDIN_DEF(getitem,"vi?"),
  3792. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  3793. BUILDIN_DEF(getnameditem,"is"),
  3794. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  3795. BUILDIN_DEF(makeitem,"iisii"),
  3796. BUILDIN_DEF(delitem,"ii"),
  3797. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  3798. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  3799. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  3800. BUILDIN_DEF(cutin,"si"),
  3801. BUILDIN_DEF(viewpoint,"iiiii"),
  3802. BUILDIN_DEF(heal,"ii"),
  3803. BUILDIN_DEF(itemheal,"ii"),
  3804. BUILDIN_DEF(percentheal,"ii"),
  3805. BUILDIN_DEF(rand,"i?"),
  3806. BUILDIN_DEF(countitem,"i"),
  3807. BUILDIN_DEF(countitem2,"iiiiiiii"),
  3808. BUILDIN_DEF(checkweight,"ii"),
  3809. BUILDIN_DEF(readparam,"i*"),
  3810. BUILDIN_DEF(getcharid,"i*"),
  3811. BUILDIN_DEF(getpartyname,"i"),
  3812. BUILDIN_DEF(getpartymember,"i*"),
  3813. BUILDIN_DEF(getpartyleader,"i?"),
  3814. BUILDIN_DEF(getguildname,"i"),
  3815. BUILDIN_DEF(getguildmaster,"i"),
  3816. BUILDIN_DEF(getguildmasterid,"i"),
  3817. BUILDIN_DEF(strcharinfo,"i"),
  3818. BUILDIN_DEF(getequipid,"i"),
  3819. BUILDIN_DEF(getequipname,"i"),
  3820. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  3821. BUILDIN_DEF(repair,"i"), // [Valaris]
  3822. BUILDIN_DEF(getequipisequiped,"i"),
  3823. BUILDIN_DEF(getequipisenableref,"i"),
  3824. BUILDIN_DEF(getequipisidentify,"i"),
  3825. BUILDIN_DEF(getequiprefinerycnt,"i"),
  3826. BUILDIN_DEF(getequipweaponlv,"i"),
  3827. BUILDIN_DEF(getequippercentrefinery,"i"),
  3828. BUILDIN_DEF(successrefitem,"i"),
  3829. BUILDIN_DEF(failedrefitem,"i"),
  3830. BUILDIN_DEF(statusup,"i"),
  3831. BUILDIN_DEF(statusup2,"ii"),
  3832. BUILDIN_DEF(bonus,"ii"),
  3833. BUILDIN_DEF2(bonus,"bonus2","iii"),
  3834. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  3835. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  3836. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  3837. BUILDIN_DEF(bonusautoscript,"si?"),
  3838. BUILDIN_DEF(bonusautoscript2,"si?"),
  3839. BUILDIN_DEF(skill,"ii?"),
  3840. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  3841. BUILDIN_DEF(guildskill,"ii"),
  3842. BUILDIN_DEF(getskilllv,"i"),
  3843. BUILDIN_DEF(getgdskilllv,"ii"),
  3844. BUILDIN_DEF(basicskillcheck,""),
  3845. BUILDIN_DEF(getgmlevel,""),
  3846. BUILDIN_DEF(end,""),
  3847. // BUILDIN_DEF2(end,"break",""), this might confuse advanced scripting support [Eoe]
  3848. BUILDIN_DEF(checkoption,"i"),
  3849. BUILDIN_DEF(setoption,"i?"),
  3850. BUILDIN_DEF(setcart,"?"),
  3851. BUILDIN_DEF(checkcart,""),
  3852. BUILDIN_DEF(setfalcon,"?"),
  3853. BUILDIN_DEF(checkfalcon,""),
  3854. BUILDIN_DEF(setriding,"?"),
  3855. BUILDIN_DEF(checkriding,""),
  3856. BUILDIN_DEF2(savepoint,"save","sii"),
  3857. BUILDIN_DEF(savepoint,"sii"),
  3858. BUILDIN_DEF(gettimetick,"i"),
  3859. BUILDIN_DEF(gettime,"i"),
  3860. BUILDIN_DEF(gettimestr,"si"),
  3861. BUILDIN_DEF(openstorage,""),
  3862. BUILDIN_DEF(guildopenstorage,"*"),
  3863. BUILDIN_DEF(itemskill,"ii"),
  3864. BUILDIN_DEF(produce,"i"),
  3865. BUILDIN_DEF(monster,"siisii*"),
  3866. BUILDIN_DEF(areamonster,"siiiisii*"),
  3867. BUILDIN_DEF(killmonster,"ss"),
  3868. BUILDIN_DEF(killmonsterall,"s"),
  3869. BUILDIN_DEF(clone,"siisi*"),
  3870. BUILDIN_DEF(doevent,"s"),
  3871. BUILDIN_DEF(donpcevent,"s"),
  3872. BUILDIN_DEF(cmdothernpc,"ss"),
  3873. BUILDIN_DEF(addtimer,"is"),
  3874. BUILDIN_DEF(deltimer,"s"),
  3875. BUILDIN_DEF(addtimercount,"si"),
  3876. BUILDIN_DEF(initnpctimer,"??"),
  3877. BUILDIN_DEF(stopnpctimer,"??"),
  3878. BUILDIN_DEF(startnpctimer,"??"),
  3879. BUILDIN_DEF(setnpctimer,"i?"),
  3880. BUILDIN_DEF(getnpctimer,"i?"),
  3881. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  3882. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  3883. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  3884. BUILDIN_DEF(announce,"si*"),
  3885. BUILDIN_DEF(mapannounce,"ssi*"),
  3886. BUILDIN_DEF(areaannounce,"siiiisi*"),
  3887. BUILDIN_DEF(getusers,"i"),
  3888. BUILDIN_DEF(getmapguildusers,"si"),
  3889. BUILDIN_DEF(getmapusers,"s"),
  3890. BUILDIN_DEF(getareausers,"siiii"),
  3891. BUILDIN_DEF(getareadropitem,"siiiii"),
  3892. BUILDIN_DEF(enablenpc,"s"),
  3893. BUILDIN_DEF(disablenpc,"s"),
  3894. BUILDIN_DEF(hideoffnpc,"s"),
  3895. BUILDIN_DEF(hideonnpc,"s"),
  3896. BUILDIN_DEF(sc_start,"iii?"),
  3897. BUILDIN_DEF(sc_start2,"iiii?"),
  3898. BUILDIN_DEF(sc_start4,"iiiiii?"),
  3899. BUILDIN_DEF(sc_end,"i?"),
  3900. BUILDIN_DEF(getscrate,"ii*"),
  3901. BUILDIN_DEF(debugmes,"s"),
  3902. BUILDIN_DEF2(catchpet,"pet","i"),
  3903. BUILDIN_DEF2(birthpet,"bpet",""),
  3904. BUILDIN_DEF(resetlvl,"i"),
  3905. BUILDIN_DEF(resetstatus,""),
  3906. BUILDIN_DEF(resetskill,""),
  3907. BUILDIN_DEF(skillpointcount,""),
  3908. BUILDIN_DEF(changebase,"i"),
  3909. BUILDIN_DEF(changesex,""),
  3910. BUILDIN_DEF(waitingroom,"si??"),
  3911. BUILDIN_DEF(delwaitingroom,"?"),
  3912. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  3913. BUILDIN_DEF(enablewaitingroomevent,"?"),
  3914. BUILDIN_DEF(disablewaitingroomevent,"?"),
  3915. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  3916. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  3917. BUILDIN_DEF(getwaitingroomstate,"i?"),
  3918. BUILDIN_DEF(warpwaitingpc,"sii?"),
  3919. BUILDIN_DEF(attachrid,"i"),
  3920. BUILDIN_DEF(detachrid,""),
  3921. BUILDIN_DEF(isloggedin,"i?"),
  3922. BUILDIN_DEF(setmapflagnosave,"ssii"),
  3923. BUILDIN_DEF(setmapflag,"si*"),
  3924. BUILDIN_DEF(removemapflag,"si"),
  3925. BUILDIN_DEF(pvpon,"s"),
  3926. BUILDIN_DEF(pvpoff,"s"),
  3927. BUILDIN_DEF(gvgon,"s"),
  3928. BUILDIN_DEF(gvgoff,"s"),
  3929. BUILDIN_DEF(emotion,"i*"),
  3930. BUILDIN_DEF(maprespawnguildid,"sii"),
  3931. BUILDIN_DEF(agitstart,""), // <Agit>
  3932. BUILDIN_DEF(agitend,""),
  3933. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  3934. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  3935. BUILDIN_DEF(getcastlename,"s"),
  3936. BUILDIN_DEF(getcastledata,"si*"),
  3937. BUILDIN_DEF(setcastledata,"sii"),
  3938. BUILDIN_DEF(requestguildinfo,"i*"),
  3939. BUILDIN_DEF(getequipcardcnt,"i"),
  3940. BUILDIN_DEF(successremovecards,"i"),
  3941. BUILDIN_DEF(failedremovecards,"ii"),
  3942. BUILDIN_DEF(marriage,"s"),
  3943. BUILDIN_DEF2(wedding_effect,"wedding",""),
  3944. BUILDIN_DEF(divorce,""),
  3945. BUILDIN_DEF(ispartneron,""),
  3946. BUILDIN_DEF(getpartnerid,""),
  3947. BUILDIN_DEF(getchildid,""),
  3948. BUILDIN_DEF(getmotherid,""),
  3949. BUILDIN_DEF(getfatherid,""),
  3950. BUILDIN_DEF(warppartner,"sii"),
  3951. BUILDIN_DEF(getitemname,"i"),
  3952. BUILDIN_DEF(getitemslots,"i"),
  3953. BUILDIN_DEF(makepet,"i"),
  3954. BUILDIN_DEF(getexp,"ii"),
  3955. BUILDIN_DEF(getinventorylist,""),
  3956. BUILDIN_DEF(getskilllist,""),
  3957. BUILDIN_DEF(clearitem,""),
  3958. BUILDIN_DEF(classchange,"ii"),
  3959. BUILDIN_DEF(misceffect,"i"),
  3960. BUILDIN_DEF(soundeffect,"si"),
  3961. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  3962. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  3963. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  3964. BUILDIN_DEF(guardianinfo,"i"), // display guardian data [Valaris]
  3965. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  3966. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  3967. BUILDIN_DEF(petloot,"i"), // [Valaris]
  3968. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  3969. // BUILDIN_DEF(petmag,"iiii"), // [Valaris]
  3970. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  3971. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  3972. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  3973. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  3974. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  3975. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  3976. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  3977. BUILDIN_DEF(nude,""), // nude command [Valaris]
  3978. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  3979. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  3980. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  3981. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  3982. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  3983. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  3984. BUILDIN_DEF(mobcount,"ss"),
  3985. BUILDIN_DEF(getlook,"i"),
  3986. BUILDIN_DEF(getsavepoint,"i"),
  3987. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  3988. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  3989. BUILDIN_DEF(npcstop,""), // [Valaris]
  3990. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  3991. BUILDIN_DEF(checkoption1,"i"),
  3992. BUILDIN_DEF(checkoption2,"i"),
  3993. BUILDIN_DEF(guildgetexp,"i"),
  3994. BUILDIN_DEF(guildchangegm,"is"),
  3995. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  3996. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  3997. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  3998. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  3999. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  4000. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  4001. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  4002. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  4003. BUILDIN_DEF(adopt,"sss"), // allows 2 parents to adopt a child
  4004. BUILDIN_DEF(night,""), // sets the server to night time
  4005. BUILDIN_DEF(day,""), // sets the server to day time
  4006. #ifdef PCRE_SUPPORT
  4007. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  4008. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  4009. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  4010. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  4011. #endif
  4012. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  4013. BUILDIN_DEF(getusersname,"*"),
  4014. BUILDIN_DEF(recovery,""),
  4015. BUILDIN_DEF(getpetinfo,"i"),
  4016. BUILDIN_DEF(gethominfo,"i"),
  4017. BUILDIN_DEF(checkequipedcard,"i"),
  4018. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  4019. BUILDIN_DEF(globalmes,"s*"),
  4020. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  4021. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  4022. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  4023. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  4024. BUILDIN_DEF(fakenpcname,"ssi"), // [Lance]
  4025. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  4026. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  4027. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  4028. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  4029. // [zBuffer] List of mathematics commands --->
  4030. BUILDIN_DEF(sqrt,"i"),
  4031. BUILDIN_DEF(pow,"ii"),
  4032. BUILDIN_DEF(distance,"iiii"),
  4033. BUILDIN_DEF(checkcell,"siii"),
  4034. // <--- [zBuffer] List of mathematics commands
  4035. // [zBuffer] List of dynamic var commands --->
  4036. BUILDIN_DEF(getd,"*"),
  4037. BUILDIN_DEF(setd,"*"),
  4038. // <--- [zBuffer] List of dynamic var commands
  4039. BUILDIN_DEF(petstat,"i"),
  4040. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  4041. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  4042. BUILDIN_DEF(npcshopadditem,"sii*"),
  4043. BUILDIN_DEF(npcshopdelitem,"si*"),
  4044. BUILDIN_DEF(npcshopattach,"s?"),
  4045. BUILDIN_DEF(equip,"i"),
  4046. BUILDIN_DEF(autoequip,"ii"),
  4047. BUILDIN_DEF(setbattleflag,"ss"),
  4048. BUILDIN_DEF(getbattleflag,"s"),
  4049. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  4050. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  4051. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  4052. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  4053. BUILDIN_DEF(axtoi,"s"),
  4054. BUILDIN_DEF(query_sql,"s*"),
  4055. BUILDIN_DEF(escape_sql,"s"),
  4056. BUILDIN_DEF(atoi,"s"),
  4057. // [zBuffer] List of player cont commands --->
  4058. BUILDIN_DEF(rid2name,"i"),
  4059. BUILDIN_DEF(pcfollow,"ii"),
  4060. BUILDIN_DEF(pcstopfollow,"i"),
  4061. BUILDIN_DEF(pcblockmove,"ii"),
  4062. // <--- [zBuffer] List of player cont commands
  4063. // [zBuffer] List of mob control commands --->
  4064. BUILDIN_DEF(mobspawn,"*"),
  4065. BUILDIN_DEF(mobremove,"i"),
  4066. BUILDIN_DEF(getmobdata,"i*"),
  4067. BUILDIN_DEF(setmobdata,"iii"),
  4068. BUILDIN_DEF(mobassist,"i?"),
  4069. BUILDIN_DEF(mobattach,"i?"),
  4070. BUILDIN_DEF(unitwalk,"ii?"),
  4071. BUILDIN_DEF(unitkill,"i"),
  4072. BUILDIN_DEF(unitwarp,"isii"),
  4073. BUILDIN_DEF(unitattack,"iv?"),
  4074. BUILDIN_DEF(unitstop,"i"),
  4075. BUILDIN_DEF(unittalk,"is"),
  4076. BUILDIN_DEF(unitemote,"ii"),
  4077. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  4078. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  4079. // <--- [zBuffer] List of mob control commands
  4080. BUILDIN_DEF(sleep,"i"),
  4081. BUILDIN_DEF(sleep2,"i"),
  4082. BUILDIN_DEF(awake,"s"),
  4083. BUILDIN_DEF(getvariableofnpc,"rs"),
  4084. BUILDIN_DEF(warpportal,"iisii"),
  4085. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  4086. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  4087. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  4088. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  4089. BUILDIN_DEF(checkvending,"*"),
  4090. BUILDIN_DEF(checkchatting,"*"),
  4091. #ifndef TXT_ONLY
  4092. BUILDIN_DEF(openmail,""),
  4093. #endif
  4094. {NULL,NULL,NULL},
  4095. };
  4096. /////////////////////////////////////////////////////////////////////
  4097. // NPC interaction
  4098. //
  4099. /// Appends a message to the npc dialog.
  4100. /// If a dialog doesn't exist yet, one is created.
  4101. ///
  4102. /// mes "<message>";
  4103. BUILDIN_FUNC(mes)
  4104. {
  4105. TBL_PC* sd = script_rid2sd(st);
  4106. if( sd == NULL )
  4107. return 1;
  4108. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4109. return 0;
  4110. }
  4111. /// Displays the button 'next' in the npc dialog.
  4112. /// The dialog text is cleared and the script continues when the button is pressed.
  4113. ///
  4114. /// next;
  4115. BUILDIN_FUNC(next)
  4116. {
  4117. TBL_PC* sd;
  4118. sd = script_rid2sd(st);
  4119. if( sd == NULL )
  4120. return 1;
  4121. st->state = STOP;
  4122. clif_scriptnext(sd, st->oid);
  4123. return 0;
  4124. }
  4125. /// Ends the script and displays the button 'close' on the npc dialog.
  4126. /// The dialog is closed when the button is pressed.
  4127. ///
  4128. /// close;
  4129. BUILDIN_FUNC(close)
  4130. {
  4131. TBL_PC* sd;
  4132. sd = script_rid2sd(st);
  4133. if( sd == NULL )
  4134. return 1;
  4135. st->state = END;
  4136. clif_scriptclose(sd, st->oid);
  4137. return 0;
  4138. }
  4139. /// Displays the button 'close' on the npc dialog.
  4140. /// The dialog is closed and the script continues when the button is pressed.
  4141. ///
  4142. /// close2;
  4143. BUILDIN_FUNC(close2)
  4144. {
  4145. TBL_PC* sd;
  4146. sd = script_rid2sd(st);
  4147. if( sd == NULL )
  4148. return 1;
  4149. st->state = STOP;
  4150. clif_scriptclose(sd, st->oid);
  4151. return 0;
  4152. }
  4153. /// Counts the number of valid and total number of options in 'str'
  4154. /// If max_count > 0 the counting stops when that valid option is reached
  4155. /// total is incremented for each option (NULL is supported)
  4156. static int menu_countoptions(const char* str, int max_count, int* total)
  4157. {
  4158. int count = 0;
  4159. int bogus_total;
  4160. if( total == NULL )
  4161. total = &bogus_total;
  4162. ++(*total);
  4163. // initial empty options
  4164. while( *str == ':' )
  4165. {
  4166. ++str;
  4167. ++(*total);
  4168. }
  4169. // count menu options
  4170. while( *str != '\0' )
  4171. {
  4172. ++count;
  4173. --max_count;
  4174. if( max_count == 0 )
  4175. break;
  4176. while( *str != ':' && *str != '\0' )
  4177. ++str;
  4178. while( *str == ':' )
  4179. {
  4180. ++str;
  4181. ++(*total);
  4182. }
  4183. }
  4184. return count;
  4185. }
  4186. /// Displays a menu with options and goes to the target label.
  4187. /// The script is stopped if cancel is pressed.
  4188. /// Options with no text are not displayed in the client.
  4189. ///
  4190. /// Options can be grouped together, separated by the character ':' in the text:
  4191. /// ex: menu "A:B:C",L_target;
  4192. /// All these options go to the specified target label.
  4193. ///
  4194. /// The index of the selected option is put in the variable @menu.
  4195. /// Indexes start with 1 and are consistent with grouped and empty options.
  4196. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4197. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4198. ///
  4199. /// NOTE: the client closes the npc dialog when cancel is pressed
  4200. ///
  4201. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4202. BUILDIN_FUNC(menu)
  4203. {
  4204. int i;
  4205. const char* text;
  4206. TBL_PC* sd;
  4207. sd = script_rid2sd(st);
  4208. if( sd == NULL )
  4209. return 1;
  4210. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4211. if( sd->state.menu_or_input == 0 )
  4212. {
  4213. struct StringBuf buf;
  4214. struct script_data* data;
  4215. if( script_lastdata(st) % 2 == 0 )
  4216. {// argument count is not even (1st argument is at index 2)
  4217. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4218. st->state = END;
  4219. return 1;
  4220. }
  4221. StringBuf_Init(&buf);
  4222. for( i = 2, sd->npc_menu = 0; i < script_lastdata(st); i += 2 )
  4223. {
  4224. // menu options
  4225. text = script_getstr(st, i);
  4226. // target label
  4227. data = script_getdata(st, i+1);
  4228. if( !data_islabel(data) )
  4229. {// not a label
  4230. StringBuf_Destroy(&buf);
  4231. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4232. script_reportdata(data);
  4233. st->state = END;
  4234. return 1;
  4235. }
  4236. // append option(s)
  4237. if( text[0] == '\0' )
  4238. continue;// empty string, ignore
  4239. if( sd->npc_menu > 0 )
  4240. StringBuf_AppendStr(&buf, ":");
  4241. StringBuf_AppendStr(&buf, text);
  4242. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4243. }
  4244. st->state = RERUNLINE;
  4245. sd->state.menu_or_input = 1;
  4246. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4247. StringBuf_Destroy(&buf);
  4248. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  4249. }
  4250. else if( sd->npc_menu == 0xff )
  4251. {// Cancel was pressed
  4252. sd->state.menu_or_input = 0;
  4253. st->state = END;
  4254. }
  4255. else
  4256. {// goto target label
  4257. int menu = 0;
  4258. sd->state.menu_or_input = 0;
  4259. if( sd->npc_menu <= 0 )
  4260. {
  4261. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4262. st->state = END;
  4263. return 1;
  4264. }
  4265. // get target label
  4266. for( i = 2; i < script_lastdata(st); i += 2 )
  4267. {
  4268. text = script_getstr(st, i);
  4269. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4270. if( sd->npc_menu <= 0 )
  4271. break;// entry found
  4272. }
  4273. if( sd->npc_menu > 0 )
  4274. {// Invalid selection
  4275. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4276. st->state = END;
  4277. return 1;
  4278. }
  4279. if( !data_islabel(script_getdata(st, i + 1)) )
  4280. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4281. ShowError("script:menu: unexpected data in label argument\n");
  4282. script_reportdata(script_getdata(st, i + 1));
  4283. st->state = END;
  4284. return 1;
  4285. }
  4286. pc_setreg(sd, add_str("@menu"), menu);
  4287. st->pos = script_getnum(st, i + 1);
  4288. st->state = GOTO;
  4289. }
  4290. return 0;
  4291. }
  4292. /// Displays a menu with options and returns the selected option.
  4293. /// Behaves like 'menu' without the target labels.
  4294. ///
  4295. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4296. ///
  4297. /// @see menu
  4298. BUILDIN_FUNC(select)
  4299. {
  4300. int i;
  4301. const char* text;
  4302. TBL_PC* sd;
  4303. sd = script_rid2sd(st);
  4304. if( sd == NULL )
  4305. return 1;
  4306. if( sd->state.menu_or_input == 0 )
  4307. {
  4308. struct StringBuf buf;
  4309. StringBuf_Init(&buf);
  4310. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  4311. {
  4312. text = script_getstr(st, i);
  4313. if( sd->npc_menu > 0 )
  4314. StringBuf_AppendStr(&buf, ":");
  4315. StringBuf_AppendStr(&buf, text);
  4316. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  4317. }
  4318. st->state = RERUNLINE;
  4319. sd->state.menu_or_input = 1;
  4320. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4321. StringBuf_Destroy(&buf);
  4322. }
  4323. else if( sd->npc_menu == 0xff )
  4324. {// Cancel was pressed
  4325. sd->state.menu_or_input = 0;
  4326. st->state = END;
  4327. }
  4328. else
  4329. {// return selected option
  4330. int menu = 0;
  4331. sd->state.menu_or_input = 0;
  4332. for( i = 2; i <= script_lastdata(st); ++i )
  4333. {
  4334. text = script_getstr(st, i);
  4335. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4336. if( sd->npc_menu <= 0 )
  4337. break;// entry found
  4338. }
  4339. pc_setreg(sd, add_str("@menu"), menu);
  4340. script_pushint(st, menu);
  4341. }
  4342. return 0;
  4343. }
  4344. /// Displays a menu with options and returns the selected option.
  4345. /// Behaves like 'menu' without the target labels, except when cancel is
  4346. /// pressed.
  4347. /// When cancel is pressed, the script continues and 255 is returned.
  4348. ///
  4349. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4350. ///
  4351. /// @see menu
  4352. BUILDIN_FUNC(prompt)
  4353. {
  4354. int i;
  4355. const char *text;
  4356. TBL_PC* sd;
  4357. sd = script_rid2sd(st);
  4358. if( sd == NULL )
  4359. return 1;
  4360. if( sd->state.menu_or_input == 0 )
  4361. {
  4362. struct StringBuf buf;
  4363. StringBuf_Init(&buf);
  4364. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  4365. {
  4366. text = script_getstr(st, i);
  4367. if( sd->npc_menu > 0 )
  4368. StringBuf_AppendStr(&buf, ":");
  4369. StringBuf_AppendStr(&buf, text);
  4370. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  4371. }
  4372. st->state = RERUNLINE;
  4373. sd->state.menu_or_input = 1;
  4374. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4375. StringBuf_Destroy(&buf);
  4376. }
  4377. else if( sd->npc_menu == 0xff )
  4378. {// Cancel was pressed
  4379. sd->state.menu_or_input = 0;
  4380. pc_setreg(sd, add_str("@menu"), 0xff);
  4381. script_pushint(st, 0xff);
  4382. }
  4383. else
  4384. {// return selected option
  4385. int menu = 0;
  4386. sd->state.menu_or_input = 0;
  4387. for( i = 2; i <= script_lastdata(st); ++i )
  4388. {
  4389. text = script_getstr(st, i);
  4390. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4391. if( sd->npc_menu <= 0 )
  4392. break;// entry found
  4393. }
  4394. pc_setreg(sd, add_str("@menu"), menu);
  4395. script_pushint(st, menu);
  4396. }
  4397. return 0;
  4398. }
  4399. /////////////////////////////////////////////////////////////////////
  4400. // ...
  4401. //
  4402. /// Jumps to the target script label.
  4403. ///
  4404. /// goto <label>;
  4405. BUILDIN_FUNC(goto)
  4406. {
  4407. if( !data_islabel(script_getdata(st,2)) )
  4408. {
  4409. ShowError("script:goto: not a label\n");
  4410. script_reportdata(script_getdata(st,2));
  4411. st->state = END;
  4412. return 1;
  4413. }
  4414. st->pos = script_getnum(st,2);
  4415. st->state = GOTO;
  4416. return 0;
  4417. }
  4418. /*==========================================
  4419. * ユーザー定義関数の呼び出し
  4420. *------------------------------------------*/
  4421. BUILDIN_FUNC(callfunc)
  4422. {
  4423. int i, j;
  4424. struct linkdb_node** oldval;
  4425. struct script_code *scr, *oldscr;
  4426. const char* str = script_getstr(st,2);
  4427. scr = strdb_get(userfunc_db, str);
  4428. if( !scr )
  4429. {
  4430. ShowError("script:callfunc: function not found! [%s]\n", str);
  4431. st->state = END;
  4432. return 1;
  4433. }
  4434. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4435. push_copy(st->stack,i);
  4436. script_pushint(st,j); // 引数の数をプッシュ
  4437. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  4438. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  4439. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  4440. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  4441. oldscr = st->script;
  4442. oldval = st->stack->var_function;
  4443. st->pos = 0;
  4444. st->script = scr;
  4445. st->stack->defsp = st->start+5+j;
  4446. st->state = GOTO;
  4447. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  4448. // ' 変数の引き継ぎ
  4449. for( i = 0; i < j; i++ )
  4450. {
  4451. struct script_data* s = script_getdatatop(st, -6-i);
  4452. if( data_isreference(s) && !s->ref )
  4453. {
  4454. char* name = str_buf + str_data[s->u.num&0x00ffffff].str;
  4455. // '@ 変数の引き継ぎ
  4456. if( name[0] == '.' && name[1] == '@' ) {
  4457. s->ref = oldval;
  4458. } else if( name[0] == '.' ) {
  4459. s->ref = &oldscr->script_vars;
  4460. }
  4461. }
  4462. }
  4463. return 0;
  4464. }
  4465. /*==========================================
  4466. * サブルーティンの呼び出し
  4467. *------------------------------------------*/
  4468. BUILDIN_FUNC(callsub)
  4469. {
  4470. int pos=script_getnum(st,2);
  4471. int i,j;
  4472. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  4473. ShowError("script:callsub: argument is not a label\n");
  4474. script_reportdata(script_getdata(st,2));
  4475. st->state=END;
  4476. return 1;
  4477. } else {
  4478. struct linkdb_node **oldval = st->stack->var_function;
  4479. for(i=st->start+3,j=0;i<st->end;i++,j++)
  4480. push_copy(st->stack,i);
  4481. script_pushint(st,j); // 引数の数をプッシュ
  4482. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  4483. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  4484. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  4485. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  4486. st->pos=pos;
  4487. st->stack->defsp=st->start+5+j;
  4488. st->state=GOTO;
  4489. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  4490. // ' 変数の引き継ぎ
  4491. for(i = 0; i < j; i++) {
  4492. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  4493. if( data_isreference(s) && !s->ref ) {
  4494. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  4495. // '@ 変数の引き継ぎ
  4496. if( name[0] == '.' && name[1] == '@' ) {
  4497. s->ref = oldval;
  4498. }
  4499. }
  4500. }
  4501. }
  4502. return 0;
  4503. }
  4504. /// Retrieves an argument provided to callfunc/callsub.
  4505. /// If the argument doesn't exist
  4506. ///
  4507. /// getarg(<index>{,<default_value>}) -> <value>
  4508. BUILDIN_FUNC(getarg)
  4509. {
  4510. int idx;
  4511. int count;
  4512. int stsp;
  4513. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4514. {
  4515. ShowError("script:getarg: no callfunc or callsub!\n");
  4516. st->state = END;
  4517. return 1;
  4518. }
  4519. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  4520. stsp = st->stack->defsp - count - 5;
  4521. idx = script_getnum(st,2);
  4522. if( idx < count )
  4523. push_copy(st->stack, stsp + idx);
  4524. else if( script_hasdata(st,3) )
  4525. script_pushcopy(st, 3);
  4526. else
  4527. {
  4528. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  4529. st->state = END;
  4530. return 1;
  4531. }
  4532. return 0;
  4533. }
  4534. /// Returns from the current function, optionaly returning a value from the functions.
  4535. /// Don't use outside script functions.
  4536. ///
  4537. /// return;
  4538. /// return <value>;
  4539. BUILDIN_FUNC(return)
  4540. {
  4541. if( script_hasdata(st,2) )
  4542. {// return value
  4543. struct script_data* data;
  4544. script_pushcopy(st, 2);
  4545. data = script_getdatatop(st, -1);
  4546. if( data_isreference(data) )
  4547. {
  4548. char* name = reference_getname(data);
  4549. if( name[0] == '.' && name[1] == '@' )
  4550. {// temporary script variable, convert to value
  4551. get_val(st, data);
  4552. if( data_isstring(data) )
  4553. {// duplicate the string
  4554. data->type = C_STR;
  4555. data->u.str = aStrdup(data->u.str);
  4556. }
  4557. }
  4558. else if( name[0] == '.' && !data->ref )
  4559. {// script variable, link to current script
  4560. data->ref = &st->script->script_vars;
  4561. }
  4562. }
  4563. }
  4564. else
  4565. {// no return value
  4566. script_pushnil(st);
  4567. }
  4568. st->state = RETFUNC;
  4569. return 0;
  4570. }
  4571. /// Returns a random number from 0 to <range>-1.
  4572. /// Or returns a random number from <min> to <max>.
  4573. /// If <min> is greater than <max>, their numbers are switched.
  4574. /// rand(<range>) -> <int>
  4575. /// rand(<min>,<max>) -> <int>
  4576. BUILDIN_FUNC(rand)
  4577. {
  4578. int range;
  4579. int min;
  4580. int max;
  4581. if( script_hasdata(st,3) )
  4582. {// min,max
  4583. min = script_getnum(st,2);
  4584. max = script_getnum(st,3);
  4585. if( max < min )
  4586. swap(min, max);
  4587. range = max - min + 1;
  4588. }
  4589. else
  4590. {// range
  4591. min = 0;
  4592. range = script_getnum(st,2);
  4593. }
  4594. if( range <= 1 )
  4595. script_pushint(st, min);
  4596. else
  4597. script_pushint(st, rand()%range + min);
  4598. return 0;
  4599. }
  4600. /*==========================================
  4601. *
  4602. *------------------------------------------*/
  4603. BUILDIN_FUNC(warp)
  4604. {
  4605. int x,y;
  4606. const char *str;
  4607. TBL_PC *sd=script_rid2sd(st);
  4608. nullpo_retr(0, sd);
  4609. str=script_getstr(st,2);
  4610. x=script_getnum(st,3);
  4611. y=script_getnum(st,4);
  4612. if(strcmp(str,"Random")==0)
  4613. pc_randomwarp(sd,3);
  4614. else if(strcmp(str,"SavePoint")==0){
  4615. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  4616. }else if(strcmp(str,"Save")==0){
  4617. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  4618. }else
  4619. pc_setpos(sd,mapindex_name2id(str),x,y,0);
  4620. return 0;
  4621. }
  4622. /*==========================================
  4623. * エリア指定ワープ
  4624. *------------------------------------------*/
  4625. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4626. {
  4627. int x,y;
  4628. unsigned int map;
  4629. map=va_arg(ap, unsigned int);
  4630. x=va_arg(ap,int);
  4631. y=va_arg(ap,int);
  4632. if(map == 0)
  4633. pc_randomwarp((TBL_PC *)bl,3);
  4634. else
  4635. pc_setpos((TBL_PC *)bl,map,x,y,0);
  4636. return 0;
  4637. }
  4638. BUILDIN_FUNC(areawarp)
  4639. {
  4640. int x,y,m;
  4641. unsigned int index;
  4642. const char *str;
  4643. const char *mapname;
  4644. int x0,y0,x1,y1;
  4645. mapname=script_getstr(st,2);
  4646. x0=script_getnum(st,3);
  4647. y0=script_getnum(st,4);
  4648. x1=script_getnum(st,5);
  4649. y1=script_getnum(st,6);
  4650. str=script_getstr(st,7);
  4651. x=script_getnum(st,8);
  4652. y=script_getnum(st,9);
  4653. if( (m=map_mapname2mapid(mapname))< 0)
  4654. return 0;
  4655. if(strcmp(str,"Random")==0)
  4656. index = 0;
  4657. else if(!(index=mapindex_name2id(str)))
  4658. return 0;
  4659. map_foreachinarea(buildin_areawarp_sub,
  4660. m,x0,y0,x1,y1,BL_PC, index,x,y );
  4661. return 0;
  4662. }
  4663. /*==========================================
  4664. * warpchar [LuzZza]
  4665. * Useful for warp one player from
  4666. * another player npc-session.
  4667. * Using: warpchar "mapname",x,y,Char_ID;
  4668. *------------------------------------------*/
  4669. BUILDIN_FUNC(warpchar)
  4670. {
  4671. int x,y,a,i;
  4672. const char *str;
  4673. TBL_PC *sd, **pl_allsd;
  4674. int users;
  4675. str=script_getstr(st,2);
  4676. x=script_getnum(st,3);
  4677. y=script_getnum(st,4);
  4678. a=script_getnum(st,5);
  4679. pl_allsd = map_getallusers(&users);
  4680. for(i=0; i<users; i++) {
  4681. sd = pl_allsd[i];
  4682. if(sd->status.char_id == a) {
  4683. if(strcmp(str, "Random") == 0)
  4684. pc_randomwarp(sd, 3);
  4685. else if(strcmp(str, "SavePoint") == 0)
  4686. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  4687. else
  4688. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  4689. }
  4690. }
  4691. return 0;
  4692. }
  4693. /*==========================================
  4694. * Warpparty - [Fredzilla]
  4695. * Syntax: warpparty "mapname",x,y,Party_ID;
  4696. *------------------------------------------*/
  4697. BUILDIN_FUNC(warpparty)
  4698. {
  4699. int x,y;
  4700. const char *str;
  4701. int p_id;
  4702. int i;
  4703. unsigned short mapindex;
  4704. TBL_PC *pl_sd;
  4705. struct party_data *p=NULL;
  4706. str=script_getstr(st,2);
  4707. x=script_getnum(st,3);
  4708. y=script_getnum(st,4);
  4709. p_id=script_getnum(st,5);
  4710. if(p_id < 1)
  4711. return 0;
  4712. p = party_search(p_id);
  4713. if (!p)
  4714. return 0;
  4715. if(strcmp(str,"Random")==0)
  4716. {
  4717. for (i = 0; i < MAX_PARTY; i++)
  4718. {
  4719. if ((pl_sd = p->data[i].sd))
  4720. {
  4721. if(map[pl_sd->bl.m].flag.nowarp)
  4722. continue;
  4723. pc_randomwarp(pl_sd,3);
  4724. }
  4725. }
  4726. }
  4727. else if(strcmp(str,"SavePointAll")==0)
  4728. {
  4729. for (i = 0; i < MAX_PARTY; i++)
  4730. {
  4731. if ((pl_sd = p->data[i].sd))
  4732. {
  4733. if(map[pl_sd->bl.m].flag.noreturn)
  4734. continue;
  4735. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4736. }
  4737. }
  4738. }
  4739. else if(strcmp(str,"SavePoint")==0)
  4740. {
  4741. pl_sd=script_rid2sd(st);
  4742. if (!pl_sd) return 0;
  4743. mapindex=pl_sd->status.save_point.map;
  4744. x=pl_sd->status.save_point.x;
  4745. y=pl_sd->status.save_point.y;
  4746. for (i = 0; i < MAX_PARTY; i++)
  4747. {
  4748. if ((pl_sd = p->data[i].sd))
  4749. {
  4750. if(map[pl_sd->bl.m].flag.noreturn)
  4751. continue;
  4752. pc_setpos(pl_sd,mapindex,x,y,3);
  4753. }
  4754. }
  4755. }
  4756. else if(strcmp(str,"Leader")==0)
  4757. {
  4758. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4759. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4760. return 0;
  4761. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  4762. return 0;
  4763. mapindex = p->data[i].sd->mapindex;
  4764. x = p->data[i].sd->bl.x;
  4765. y = p->data[i].sd->bl.y;
  4766. for (i = 0; i < MAX_PARTY; i++)
  4767. {
  4768. pl_sd = p->data[i].sd;
  4769. if (!pl_sd)
  4770. continue;
  4771. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4772. continue;
  4773. pc_setpos(pl_sd,mapindex,x,y,3);
  4774. }
  4775. }
  4776. else
  4777. {
  4778. mapindex = mapindex_name2id(str);
  4779. if (!mapindex) //Show source of npc error.
  4780. return 1;
  4781. for (i = 0; i < MAX_PARTY; i++)
  4782. {
  4783. if ((pl_sd = p->data[i].sd))
  4784. {
  4785. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4786. continue;
  4787. pc_setpos(pl_sd,mapindex,x,y,3);
  4788. }
  4789. }
  4790. }
  4791. return 0;
  4792. }
  4793. /*==========================================
  4794. * Warpguild - [Fredzilla]
  4795. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4796. *------------------------------------------*/
  4797. BUILDIN_FUNC(warpguild)
  4798. {
  4799. int x,y;
  4800. unsigned short mapindex;
  4801. const char *str;
  4802. int g;
  4803. int i;
  4804. TBL_PC *pl_sd, **pl_allsd;
  4805. int users;
  4806. TBL_PC *sd;
  4807. str=script_getstr(st,2);
  4808. x=script_getnum(st,3);
  4809. y=script_getnum(st,4);
  4810. g=script_getnum(st,5);
  4811. sd=script_rid2sd(st);
  4812. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4813. return 0;
  4814. if(g < 1)
  4815. return 0;
  4816. pl_allsd = map_getallusers(&users);
  4817. if(strcmp(str,"Random")==0)
  4818. {
  4819. for (i = 0; i < users; i++)
  4820. {
  4821. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4822. {
  4823. if(map[pl_sd->bl.m].flag.nowarp)
  4824. continue;
  4825. pc_randomwarp(pl_sd,3);
  4826. }
  4827. }
  4828. }
  4829. else if(strcmp(str,"SavePointAll")==0)
  4830. {
  4831. if(map[sd->bl.m].flag.noreturn)
  4832. return 0;
  4833. for (i = 0; i < users; i++)
  4834. {
  4835. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4836. {
  4837. if(map[pl_sd->bl.m].flag.noreturn)
  4838. continue;
  4839. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4840. }
  4841. }
  4842. }
  4843. else if(strcmp(str,"SavePoint")==0)
  4844. {
  4845. if(map[sd->bl.m].flag.noreturn)
  4846. return 0;
  4847. mapindex=sd->status.save_point.map;
  4848. x=sd->status.save_point.x;
  4849. y=sd->status.save_point.y;
  4850. for (i = 0; i < users; i++)
  4851. {
  4852. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4853. {
  4854. if(map[pl_sd->bl.m].flag.noreturn)
  4855. continue;
  4856. pc_setpos(pl_sd,mapindex,x,y,3);
  4857. }
  4858. }
  4859. }
  4860. else
  4861. {
  4862. mapindex = mapindex_name2id(str);
  4863. for (i = 0; i < users; i++)
  4864. {
  4865. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4866. {
  4867. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4868. continue;
  4869. pc_setpos(pl_sd,mapindex,x,y,3);
  4870. }
  4871. }
  4872. }
  4873. return 0;
  4874. }
  4875. /*==========================================
  4876. *
  4877. *------------------------------------------*/
  4878. BUILDIN_FUNC(heal)
  4879. {
  4880. TBL_PC *sd;
  4881. int hp,sp;
  4882. sd = script_rid2sd(st);
  4883. if (!sd) return 0;
  4884. hp=script_getnum(st,2);
  4885. sp=script_getnum(st,3);
  4886. status_heal(&sd->bl, hp, sp, 1);
  4887. return 0;
  4888. }
  4889. /*==========================================
  4890. *
  4891. *------------------------------------------*/
  4892. BUILDIN_FUNC(itemheal)
  4893. {
  4894. TBL_PC *sd;
  4895. int hp,sp;
  4896. hp=script_getnum(st,2);
  4897. sp=script_getnum(st,3);
  4898. if(potion_flag==1) {
  4899. potion_hp = hp;
  4900. potion_sp = sp;
  4901. return 0;
  4902. }
  4903. sd = script_rid2sd(st);
  4904. if (!sd) return 0;
  4905. pc_itemheal(sd,sd->itemid,hp,sp);
  4906. return 0;
  4907. }
  4908. /*==========================================
  4909. *
  4910. *------------------------------------------*/
  4911. BUILDIN_FUNC(percentheal)
  4912. {
  4913. int hp,sp;
  4914. hp=script_getnum(st,2);
  4915. sp=script_getnum(st,3);
  4916. if(potion_flag==1) {
  4917. potion_per_hp = hp;
  4918. potion_per_sp = sp;
  4919. return 0;
  4920. }
  4921. pc_percentheal(script_rid2sd(st),hp,sp);
  4922. return 0;
  4923. }
  4924. /*==========================================
  4925. *
  4926. *------------------------------------------*/
  4927. BUILDIN_FUNC(jobchange)
  4928. {
  4929. int job, upper=-1;
  4930. job=script_getnum(st,2);
  4931. if( script_hasdata(st,3) )
  4932. upper=script_getnum(st,3);
  4933. if (pcdb_checkid(job))
  4934. {
  4935. pc_jobchange(script_rid2sd(st),job, upper);
  4936. if(use_irc && irc_announce_jobchange_flag)
  4937. irc_announce_jobchange(script_rid2sd(st));
  4938. }
  4939. return 0;
  4940. }
  4941. /*==========================================
  4942. *
  4943. *------------------------------------------*/
  4944. BUILDIN_FUNC(jobname)
  4945. {
  4946. int class_=script_getnum(st,2);
  4947. script_pushconststr(st,job_name(class_));
  4948. return 0;
  4949. }
  4950. /*==========================================
  4951. *
  4952. *------------------------------------------*/
  4953. BUILDIN_FUNC(input)
  4954. {
  4955. TBL_PC *sd = script_rid2sd(st);
  4956. struct script_data *data = script_getdata(st,2);
  4957. int num = data->u.num;
  4958. char *name=str_buf+str_data[num&0x00ffffff].str;
  4959. char postfix = name[strlen(name)-1];
  4960. if (!sd) return 1;
  4961. if( !data_isreference(data) ){
  4962. ShowError("script:input: not a variable\n");
  4963. script_reportdata(data);
  4964. return 1;
  4965. }
  4966. if( !sd->state.menu_or_input )
  4967. { // first invocation, display npc input box
  4968. sd->state.menu_or_input = 1;
  4969. st->state = RERUNLINE;
  4970. if( postfix == '$' )
  4971. clif_scriptinputstr(sd,st->oid);
  4972. else
  4973. clif_scriptinput(sd,st->oid);
  4974. }
  4975. else
  4976. { // take received text/value and store it in the designated variable
  4977. sd->state.menu_or_input = 0;
  4978. if( postfix == '$' )
  4979. {
  4980. set_reg(st,sd,num,name,(void*)sd->npc_str,script_getref(st,2));
  4981. }
  4982. else
  4983. {
  4984. // limit the input to a non-negative value smaller than 'vending_max_value' (for scripts that didn't check this)
  4985. sd->npc_amount = cap_value(sd->npc_amount, 0, battle_config.vending_max_value);
  4986. set_reg(st,sd,num,name,(void*)sd->npc_amount,script_getref(st,2));
  4987. }
  4988. }
  4989. return 0;
  4990. }
  4991. /*==========================================
  4992. * 変数設定
  4993. *------------------------------------------*/
  4994. BUILDIN_FUNC(set)
  4995. {
  4996. TBL_PC *sd=NULL;
  4997. int num=st->stack->stack_data[st->start+2].u.num;
  4998. char *name=str_buf+str_data[num&0x00ffffff].str;
  4999. char prefix=*name;
  5000. char postfix=name[strlen(name)-1];
  5001. if( !data_isreference(script_getdata(st,2)) ){
  5002. ShowError("script:set: not a variable\n");
  5003. script_reportdata(script_getdata(st,2));
  5004. st->state = END;
  5005. return 1;
  5006. }
  5007. if(not_server_variable(prefix))
  5008. sd=script_rid2sd(st);
  5009. if( postfix=='$' ){
  5010. // 文字列
  5011. const char *str = script_getstr(st,3);
  5012. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  5013. }else{
  5014. // 数値
  5015. int val = script_getnum(st,3);
  5016. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  5017. }
  5018. return 0;
  5019. }
  5020. /////////////////////////////////////////////////////////////////////
  5021. /// Array variables
  5022. ///
  5023. /// Returns the size of the specified array
  5024. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  5025. {
  5026. int32 ret = idx;
  5027. if( isstring )
  5028. {
  5029. for( ; idx < 128; ++idx )
  5030. {
  5031. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  5032. if( str && *str )
  5033. ret = idx + 1;
  5034. }
  5035. }
  5036. else
  5037. {
  5038. for( ; idx < 128; ++idx )
  5039. {
  5040. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  5041. if( num )
  5042. ret = idx + 1;
  5043. }
  5044. }
  5045. return ret;
  5046. }
  5047. /// Sets values of an array, from the starting index.
  5048. /// ex: setarray arr[1],1,2,3;
  5049. ///
  5050. /// setarray <array variable>,<value1>{,<value2>...};
  5051. BUILDIN_FUNC(setarray)
  5052. {
  5053. struct script_data* data;
  5054. char* name;
  5055. int32 start;
  5056. int32 end;
  5057. int32 id;
  5058. int32 i;
  5059. TBL_PC* sd = NULL;
  5060. data = script_getdata(st, 2);
  5061. if( !data_isreference(data) )
  5062. {
  5063. ShowError("script:setarray: not a variable\n");
  5064. script_reportdata(data);
  5065. st->state = END;
  5066. return 1;// not a variable
  5067. }
  5068. id = reference_getid(data);
  5069. start = reference_getindex(data);
  5070. name = reference_getname(data);
  5071. if( not_array_variable(*name) )
  5072. {
  5073. ShowError("script:setarray: illegal scope\n");
  5074. script_reportdata(data);
  5075. st->state = END;
  5076. return 1;// not supported
  5077. }
  5078. if( not_server_variable(*name) )
  5079. {
  5080. sd = script_rid2sd(st);
  5081. if( sd == NULL )
  5082. return 1;// no player attached
  5083. }
  5084. end = start + script_lastdata(st) - 2;
  5085. if( end > 127 )
  5086. end = 127;
  5087. if( is_string_variable(name) )
  5088. {// string array
  5089. for( i = 3; start < end; ++start, ++i )
  5090. {
  5091. void* v = (void*)script_getstr(st,i);
  5092. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5093. }
  5094. }
  5095. else
  5096. {// int array
  5097. for( i = 3; start < end; ++start, ++i )
  5098. {
  5099. void* v = (void*)script_getnum(st,i);
  5100. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5101. }
  5102. }
  5103. return 0;
  5104. }
  5105. /// Sets count values of an array, from the starting index.
  5106. /// ex: cleararray arr[0],0,1;
  5107. ///
  5108. /// cleararray <array variable>,<value>,<count>;
  5109. BUILDIN_FUNC(cleararray)
  5110. {
  5111. struct script_data* data;
  5112. char* name;
  5113. int32 start;
  5114. int32 end;
  5115. int32 id;
  5116. void* v;
  5117. TBL_PC* sd = NULL;
  5118. data = script_getdata(st, 2);
  5119. if( !data_isreference(data) )
  5120. {
  5121. ShowError("script:cleararray: not a variable\n");
  5122. script_reportdata(data);
  5123. st->state = END;
  5124. return 1;// not a variable
  5125. }
  5126. id = reference_getid(data);
  5127. start = reference_getindex(data);
  5128. name = reference_getname(data);
  5129. if( not_array_variable(*name) )
  5130. {
  5131. ShowError("script:cleararray: illegal scope\n");
  5132. script_reportdata(data);
  5133. st->state = END;
  5134. return 1;// not supported
  5135. }
  5136. if( not_server_variable(*name) )
  5137. {
  5138. sd = script_rid2sd(st);
  5139. if( sd == NULL )
  5140. return 1;// no player attached
  5141. }
  5142. if( is_string_variable(name) )
  5143. v = (void*)script_getstr(st, 3);
  5144. else
  5145. v = (void*)script_getnum(st, 3);
  5146. end = start + script_getnum(st, 4);
  5147. if( end > 127 )
  5148. end = 127;
  5149. for( ; start < end; ++start )
  5150. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5151. return 0;
  5152. }
  5153. /// Copies data from one array to another.
  5154. /// ex: copyarray arr[0],arr[2],2;
  5155. ///
  5156. /// copyarray <destination array variable>,<source array variable>,<count>;
  5157. BUILDIN_FUNC(copyarray)
  5158. {
  5159. struct script_data* data1;
  5160. struct script_data* data2;
  5161. char* name1;
  5162. char* name2;
  5163. int32 idx1;
  5164. int32 idx2;
  5165. int32 id1;
  5166. int32 id2;
  5167. void* v;
  5168. int32 i;
  5169. int32 count;
  5170. TBL_PC* sd = NULL;
  5171. data1 = script_getdata(st, 2);
  5172. data2 = script_getdata(st, 3);
  5173. if( !data_isreference(data1) || !data_isreference(data2) )
  5174. {
  5175. ShowError("script:copyarray: not a variable\n");
  5176. script_reportdata(data1);
  5177. script_reportdata(data2);
  5178. st->state = END;
  5179. return 1;// not a variable
  5180. }
  5181. id1 = reference_getid(data1);
  5182. id2 = reference_getid(data2);
  5183. idx1 = reference_getindex(data1);
  5184. idx2 = reference_getindex(data2);
  5185. name1 = reference_getname(data1);
  5186. name2 = reference_getname(data2);
  5187. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5188. {
  5189. ShowError("script:copyarray: illegal scope\n");
  5190. script_reportdata(data1);
  5191. script_reportdata(data2);
  5192. st->state = END;
  5193. return 1;// not supported
  5194. }
  5195. if( is_string_variable(name1) != is_string_variable(name2) )
  5196. {
  5197. ShowError("script:copyarray: type mismatch\n");
  5198. script_reportdata(data1);
  5199. script_reportdata(data2);
  5200. st->state = END;
  5201. return 1;// data type mismatch
  5202. }
  5203. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5204. {
  5205. sd = script_rid2sd(st);
  5206. if( sd == NULL )
  5207. return 1;// no player attached
  5208. }
  5209. count = script_getnum(st, 4);
  5210. if( count > 128 - idx1 )
  5211. count = 128 - idx1;
  5212. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5213. return 0;// nothing to copy
  5214. if( id1 == id2 && idx1 > idx2 )
  5215. {// destination might be overlapping the source - copy in reverse order
  5216. for( i = count - 1; i >= 0; --i )
  5217. {
  5218. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5219. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5220. }
  5221. }
  5222. else
  5223. {// normal copy
  5224. for( i = 0; i < count; ++i )
  5225. {
  5226. if( idx2 + i < 128 )
  5227. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5228. else// out of range - assume ""/0
  5229. v = (void*)(is_string_variable(name1) ? "" : 0);
  5230. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5231. }
  5232. }
  5233. return 0;
  5234. }
  5235. /// Returns the size of the array.
  5236. /// Assumes that everything before the starting index exists.
  5237. /// ex: getarraysize(arr[3])
  5238. ///
  5239. /// getarraysize(<array variable>) -> <int>
  5240. BUILDIN_FUNC(getarraysize)
  5241. {
  5242. struct script_data* data;
  5243. char* name;
  5244. data = script_getdata(st, 2);
  5245. if( !data_isreference(data) )
  5246. {
  5247. ShowError("script:getarraysize: not a variable\n");
  5248. script_reportdata(data);
  5249. script_pushnil(st);
  5250. st->state = END;
  5251. return 1;// not a variable
  5252. }
  5253. name = reference_getname(data);
  5254. if( not_array_variable(*name) )
  5255. {
  5256. ShowError("script:getarraysize: illegal scope\n");
  5257. script_reportdata(data);
  5258. script_pushnil(st);
  5259. st->state = END;
  5260. return 1;// not supported
  5261. }
  5262. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5263. return 0;
  5264. }
  5265. /// Deletes count or all the elements in an array, from the starting index.
  5266. /// ex: deletearray arr[4],2;
  5267. ///
  5268. /// deletearray <array variable>;
  5269. /// deletearray <array variable>,<count>;
  5270. BUILDIN_FUNC(deletearray)
  5271. {
  5272. struct script_data* data;
  5273. char* name;
  5274. int start;
  5275. int end;
  5276. int id;
  5277. TBL_PC *sd = NULL;
  5278. data = script_getdata(st, 2);
  5279. if( !data_isreference(data) )
  5280. {
  5281. ShowError("script:deletearray: not a variable\n");
  5282. script_reportdata(data);
  5283. st->state = END;
  5284. return 1;// not a variable
  5285. }
  5286. id = reference_getid(data);
  5287. start = reference_getindex(data);
  5288. name = reference_getname(data);
  5289. if( not_array_variable(*name) )
  5290. {
  5291. ShowError("script:deletearray: illegal scope\n");
  5292. script_reportdata(data);
  5293. st->state = END;
  5294. return 1;// not supported
  5295. }
  5296. if( not_server_variable(*name) )
  5297. {
  5298. sd = script_rid2sd(st);
  5299. if( sd == NULL )
  5300. return 1;// no player attached
  5301. }
  5302. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  5303. if( start >= end )
  5304. return 0;// nothing to free
  5305. if( script_hasdata(st,3) )
  5306. {
  5307. int count = script_getnum(st, 3);
  5308. if( count > end - start )
  5309. count = end - start;
  5310. if( count <= 0 )
  5311. return 0;// nothing to free
  5312. // move rest of the elements backward
  5313. for( ; start + count < end; ++start )
  5314. {
  5315. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5316. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5317. }
  5318. }
  5319. // clear the rest of the array
  5320. if( is_string_variable(name) )
  5321. {
  5322. for( ; start < end; ++start )
  5323. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5324. }
  5325. else
  5326. {
  5327. for( ; start < end; ++start )
  5328. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5329. }
  5330. return 0;
  5331. }
  5332. /// Returns a reference to the target index of the array variable.
  5333. /// Equivalent to var[index].
  5334. ///
  5335. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5336. BUILDIN_FUNC(getelementofarray)
  5337. {
  5338. struct script_data* data;
  5339. char* name;
  5340. int32 id;
  5341. int i;
  5342. data = script_getdata(st, 2);
  5343. if( !data_isreference(data) )
  5344. {
  5345. ShowError("script:getelementofarray: not a variable\n");
  5346. script_reportdata(data);
  5347. script_pushnil(st);
  5348. st->state = END;
  5349. return 1;// not a variable
  5350. }
  5351. id = reference_getid(data);
  5352. name = reference_getname(data);
  5353. if( not_array_variable(*name) )
  5354. {
  5355. ShowError("script:getelementofarray: illegal scope\n");
  5356. script_reportdata(data);
  5357. script_pushnil(st);
  5358. st->state = END;
  5359. return 1;// not supported
  5360. }
  5361. i = script_getnum(st, 3);
  5362. if( i < 0 || i >= 128 )
  5363. {
  5364. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5365. script_reportdata(data);
  5366. script_pushnil(st);
  5367. st->state = END;
  5368. return 1;// out of range
  5369. }
  5370. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5371. return 0;
  5372. }
  5373. /////////////////////////////////////////////////////////////////////
  5374. /// ...
  5375. ///
  5376. /*==========================================
  5377. *
  5378. *------------------------------------------*/
  5379. BUILDIN_FUNC(setlook)
  5380. {
  5381. int type,val;
  5382. type=script_getnum(st,2);
  5383. val=script_getnum(st,3);
  5384. pc_changelook(script_rid2sd(st),type,val);
  5385. return 0;
  5386. }
  5387. /*==========================================
  5388. *
  5389. *------------------------------------------*/
  5390. BUILDIN_FUNC(cutin)
  5391. {
  5392. clif_cutin(script_rid2sd(st),script_getstr(st,2),script_getnum(st,3));
  5393. return 0;
  5394. }
  5395. /*==========================================
  5396. *
  5397. *------------------------------------------*/
  5398. BUILDIN_FUNC(viewpoint)
  5399. {
  5400. int type,x,y,id,color;
  5401. type=script_getnum(st,2);
  5402. x=script_getnum(st,3);
  5403. y=script_getnum(st,4);
  5404. id=script_getnum(st,5);
  5405. color=script_getnum(st,6);
  5406. clif_viewpoint(script_rid2sd(st),st->oid,type,x,y,id,color);
  5407. return 0;
  5408. }
  5409. /*==========================================
  5410. *
  5411. *------------------------------------------*/
  5412. BUILDIN_FUNC(countitem)
  5413. {
  5414. int nameid, i;
  5415. int count = 0;
  5416. struct script_data* data;
  5417. TBL_PC* sd = script_rid2sd(st);
  5418. if (!sd) {
  5419. script_pushint(st,0);
  5420. return 0;
  5421. }
  5422. data = script_getdata(st,2);
  5423. get_val(st,data);
  5424. if( data_isstring(data) ) {
  5425. const char* name = conv_str(st,data);
  5426. struct item_data* item_data;
  5427. if((item_data = itemdb_searchname(name)) != NULL)
  5428. nameid = item_data->nameid;
  5429. else
  5430. nameid = 0;
  5431. } else
  5432. nameid = conv_num(st,data);
  5433. if (nameid < 500) {
  5434. if(battle_config.error_log) ShowError("wrong item ID : countitem(%i)\n", nameid);
  5435. script_reportsrc(st);
  5436. script_pushint(st,0);
  5437. return 1;
  5438. }
  5439. for(i = 0; i < MAX_INVENTORY; i++)
  5440. if(sd->status.inventory[i].nameid == nameid)
  5441. count += sd->status.inventory[i].amount;
  5442. script_pushint(st,count);
  5443. return 0;
  5444. }
  5445. /*==========================================
  5446. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5447. * returns number of items that meet the conditions
  5448. *------------------------------------------*/
  5449. BUILDIN_FUNC(countitem2)
  5450. {
  5451. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5452. int count = 0;
  5453. int i;
  5454. struct script_data* data;
  5455. TBL_PC* sd = script_rid2sd(st);
  5456. if (!sd) {
  5457. script_pushint(st,0);
  5458. return 0;
  5459. }
  5460. data = script_getdata(st,2);
  5461. get_val(st,data);
  5462. if( data_isstring(data) ) {
  5463. const char* name = conv_str(st,data);
  5464. struct item_data* item_data;
  5465. if((item_data = itemdb_searchname(name)) != NULL)
  5466. nameid = item_data->nameid;
  5467. else
  5468. nameid = 0;
  5469. } else
  5470. nameid = conv_num(st,data);
  5471. iden = script_getnum(st,3);
  5472. ref = script_getnum(st,4);
  5473. attr = script_getnum(st,5);
  5474. c1 = (short)script_getnum(st,6);
  5475. c2 = (short)script_getnum(st,7);
  5476. c3 = (short)script_getnum(st,8);
  5477. c4 = (short)script_getnum(st,9);
  5478. if (nameid < 500) {
  5479. if(battle_config.error_log) ShowError("wrong item ID : countitem2(%i)\n", nameid);
  5480. script_pushint(st,0);
  5481. return 1;
  5482. }
  5483. for(i = 0; i < MAX_INVENTORY; i++)
  5484. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5485. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5486. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5487. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5488. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5489. sd->status.inventory[i].card[3] == c4
  5490. )
  5491. count += sd->status.inventory[i].amount;
  5492. script_pushint(st,count);
  5493. return 0;
  5494. }
  5495. /*==========================================
  5496. * 重量チェック
  5497. *------------------------------------------*/
  5498. BUILDIN_FUNC(checkweight)
  5499. {
  5500. int nameid=0,amount,i;
  5501. unsigned long weight;
  5502. TBL_PC *sd;
  5503. struct script_data *data;
  5504. sd = script_rid2sd(st);
  5505. data=script_getdata(st,2);
  5506. get_val(st,data);
  5507. if( data_isstring(data) ){
  5508. const char *name=conv_str(st,data);
  5509. struct item_data *item_data = itemdb_searchname(name);
  5510. if( item_data )
  5511. nameid=item_data->nameid;
  5512. }else
  5513. nameid=conv_num(st,data);
  5514. amount=script_getnum(st,3);
  5515. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  5516. script_pushint(st,0);
  5517. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  5518. return 1;
  5519. }
  5520. weight = itemdb_weight(nameid)*amount;
  5521. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  5522. script_pushint(st,0);
  5523. } else {
  5524. //Check if the inventory ain't full.
  5525. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  5526. i = pc_search_inventory(sd,0);
  5527. if (i >= 0) //Empty slot available.
  5528. script_pushint(st,1);
  5529. else //Inventory full
  5530. script_pushint(st,0);
  5531. }
  5532. return 0;
  5533. }
  5534. /*==========================================
  5535. * getitem <item id>,<amount>{,<character ID>};
  5536. * getitem "<item name>",<amount>{,<character ID>};
  5537. *------------------------------------------*/
  5538. BUILDIN_FUNC(getitem)
  5539. {
  5540. int nameid,amount,flag = 0;
  5541. struct item it;
  5542. TBL_PC *sd;
  5543. struct script_data *data;
  5544. data=script_getdata(st,2);
  5545. get_val(st,data);
  5546. if( data_isstring(data) )
  5547. {// "<item name>"
  5548. const char *name=conv_str(st,data);
  5549. struct item_data *item_data = itemdb_searchname(name);
  5550. if( item_data == NULL ){
  5551. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5552. return 1; //No item created.
  5553. }
  5554. nameid=item_data->nameid;
  5555. } else if( data_isint(data) )
  5556. {// <item id>
  5557. nameid=conv_num(st,data);
  5558. //Violet Box, Blue Box, etc - random item pick
  5559. if( nameid < 0 ) {
  5560. nameid=itemdb_searchrandomid(-nameid);
  5561. flag = 1;
  5562. }
  5563. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5564. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5565. return 1; //No item created.
  5566. }
  5567. } else {
  5568. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5569. return 1;
  5570. }
  5571. // <amount>
  5572. if( (amount=script_getnum(st,3)) <= 0)
  5573. return 0; //return if amount <=0, skip the useles iteration
  5574. memset(&it,0,sizeof(it));
  5575. it.nameid=nameid;
  5576. if(!flag)
  5577. it.identify=1;
  5578. else
  5579. it.identify=itemdb_isidentified(nameid);
  5580. if( script_hasdata(st,4) )
  5581. {// <character ID>
  5582. sd=map_id2sd(script_getnum(st,4));
  5583. } else
  5584. {// attached player
  5585. sd=script_rid2sd(st);
  5586. }
  5587. if( sd == NULL ) // no target
  5588. return 0;
  5589. if( pet_create_egg(sd, nameid) )
  5590. amount = 1; //This is a pet!
  5591. else if( (flag=pc_additem(sd,&it,amount)) ){
  5592. clif_additem(sd,0,0,flag);
  5593. if( pc_candrop(sd,&it) )
  5594. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5595. }
  5596. //Logs items, got from (N)PC scripts [Lupus]
  5597. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  5598. log_pick_pc(sd, "N", nameid, amount, NULL);
  5599. return 0;
  5600. }
  5601. /*==========================================
  5602. *
  5603. *------------------------------------------*/
  5604. BUILDIN_FUNC(getitem2)
  5605. {
  5606. int nameid,amount,flag = 0;
  5607. int iden,ref,attr,c1,c2,c3,c4;
  5608. struct item_data *item_data;
  5609. struct item item_tmp;
  5610. TBL_PC *sd;
  5611. struct script_data *data;
  5612. sd = script_rid2sd(st);
  5613. data=script_getdata(st,2);
  5614. get_val(st,data);
  5615. if( data_isstring(data) ){
  5616. const char *name=conv_str(st,data);
  5617. struct item_data *item_data = itemdb_searchname(name);
  5618. if( item_data )
  5619. nameid=item_data->nameid;
  5620. else
  5621. nameid=UNKNOWN_ITEM_ID;
  5622. }else
  5623. nameid=conv_num(st,data);
  5624. amount=script_getnum(st,3);
  5625. iden=script_getnum(st,4);
  5626. ref=script_getnum(st,5);
  5627. attr=script_getnum(st,6);
  5628. c1=script_getnum(st,7);
  5629. c2=script_getnum(st,8);
  5630. c3=script_getnum(st,9);
  5631. c4=script_getnum(st,10);
  5632. if( script_hasdata(st,11) ) //アイテムを指定したIDに渡す
  5633. sd=map_id2sd(script_getnum(st,11));
  5634. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  5635. return 0;
  5636. if(nameid<0) { // ランダム
  5637. nameid=itemdb_searchrandomid(-nameid);
  5638. flag = 1;
  5639. }
  5640. if(nameid > 0) {
  5641. memset(&item_tmp,0,sizeof(item_tmp));
  5642. item_data=itemdb_exists(nameid);
  5643. if (item_data == NULL)
  5644. return -1;
  5645. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5646. if(ref > 10) ref = 10;
  5647. }
  5648. else if(item_data->type==IT_PETEGG) {
  5649. iden = 1;
  5650. ref = 0;
  5651. }
  5652. else {
  5653. iden = 1;
  5654. ref = attr = 0;
  5655. }
  5656. item_tmp.nameid=nameid;
  5657. if(!flag)
  5658. item_tmp.identify=iden;
  5659. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5660. item_tmp.identify=0;
  5661. item_tmp.refine=ref;
  5662. item_tmp.attribute=attr;
  5663. item_tmp.card[0]=c1;
  5664. item_tmp.card[1]=c2;
  5665. item_tmp.card[2]=c3;
  5666. item_tmp.card[3]=c4;
  5667. if((flag = pc_additem(sd,&item_tmp,amount))) {
  5668. clif_additem(sd,0,0,flag);
  5669. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5670. }
  5671. //Logs items, got from (N)PC scripts [Lupus]
  5672. if(log_config.enable_logs&0x40)
  5673. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  5674. }
  5675. return 0;
  5676. }
  5677. /*==========================================
  5678. * gets an item with someone's name inscribed [Skotlex]
  5679. * getinscribeditem item_num, character_name
  5680. * Returned Qty is always 1, only works on equip-able
  5681. * equipment
  5682. *------------------------------------------*/
  5683. BUILDIN_FUNC(getnameditem)
  5684. {
  5685. int nameid;
  5686. struct item item_tmp;
  5687. TBL_PC *sd, *tsd;
  5688. struct script_data *data;
  5689. sd = script_rid2sd(st);
  5690. if (sd == NULL)
  5691. { //Player not attached!
  5692. script_pushint(st,0);
  5693. return 0;
  5694. }
  5695. data=script_getdata(st,2);
  5696. get_val(st,data);
  5697. if( data_isstring(data) ){
  5698. const char *name=conv_str(st,data);
  5699. struct item_data *item_data = itemdb_searchname(name);
  5700. if( item_data == NULL)
  5701. { //Failed
  5702. script_pushint(st,0);
  5703. return 0;
  5704. }
  5705. nameid = item_data->nameid;
  5706. }else
  5707. nameid = conv_num(st,data);
  5708. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5709. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5710. script_pushint(st,0);
  5711. return 0;
  5712. }
  5713. data=script_getdata(st,3);
  5714. get_val(st,data);
  5715. if( data_isstring(data) ) //Char Name
  5716. tsd=map_nick2sd(conv_str(st,data));
  5717. else //Char Id was given
  5718. tsd=map_charid2sd(conv_num(st,data));
  5719. if( tsd == NULL )
  5720. { //Failed
  5721. script_pushint(st,0);
  5722. return 0;
  5723. }
  5724. memset(&item_tmp,0,sizeof(item_tmp));
  5725. item_tmp.nameid=nameid;
  5726. item_tmp.amount=1;
  5727. item_tmp.identify=1;
  5728. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5729. item_tmp.card[2]=tsd->status.char_id;
  5730. item_tmp.card[3]=tsd->status.char_id >> 16;
  5731. if(pc_additem(sd,&item_tmp,1)) {
  5732. script_pushint(st,0);
  5733. return 0; //Failed to add item, we will not drop if they don't fit
  5734. }
  5735. //Logs items, got from (N)PC scripts [Lupus]
  5736. if(log_config.enable_logs&0x40)
  5737. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5738. script_pushint(st,1);
  5739. return 0;
  5740. }
  5741. /*==========================================
  5742. * gets a random item ID from an item group [Skotlex]
  5743. * groupranditem group_num
  5744. *------------------------------------------*/
  5745. BUILDIN_FUNC(grouprandomitem)
  5746. {
  5747. int group;
  5748. group = script_getnum(st,2);
  5749. script_pushint(st,itemdb_searchrandomid(group));
  5750. return 0;
  5751. }
  5752. /*==========================================
  5753. *
  5754. *------------------------------------------*/
  5755. BUILDIN_FUNC(makeitem)
  5756. {
  5757. int nameid,amount,flag = 0;
  5758. int x,y,m;
  5759. const char *mapname;
  5760. struct item item_tmp;
  5761. struct script_data *data;
  5762. data=script_getdata(st,2);
  5763. get_val(st,data);
  5764. if( data_isstring(data) ){
  5765. const char *name=conv_str(st,data);
  5766. struct item_data *item_data = itemdb_searchname(name);
  5767. if( item_data )
  5768. nameid=item_data->nameid;
  5769. else
  5770. nameid=UNKNOWN_ITEM_ID;
  5771. }else
  5772. nameid=conv_num(st,data);
  5773. amount=script_getnum(st,3);
  5774. mapname =script_getstr(st,4);
  5775. x =script_getnum(st,5);
  5776. y =script_getnum(st,6);
  5777. if(strcmp(mapname,"this")==0)
  5778. {
  5779. TBL_PC *sd;
  5780. sd = script_rid2sd(st);
  5781. if (!sd) return 0; //Failed...
  5782. m=sd->bl.m;
  5783. } else
  5784. m=map_mapname2mapid(mapname);
  5785. if(nameid<0) { // ランダム
  5786. nameid=itemdb_searchrandomid(-nameid);
  5787. flag = 1;
  5788. }
  5789. if(nameid > 0) {
  5790. memset(&item_tmp,0,sizeof(item_tmp));
  5791. item_tmp.nameid=nameid;
  5792. if(!flag)
  5793. item_tmp.identify=1;
  5794. else
  5795. item_tmp.identify=itemdb_isidentified(nameid);
  5796. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5797. }
  5798. return 0;
  5799. }
  5800. /*==========================================
  5801. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5802. *------------------------------------------*/
  5803. BUILDIN_FUNC(delitem)
  5804. {
  5805. int nameid=0,amount,i,important_item=0;
  5806. TBL_PC *sd;
  5807. struct script_data *data;
  5808. sd = script_rid2sd(st);
  5809. data=script_getdata(st,2);
  5810. get_val(st,data);
  5811. if( data_isstring(data) ){
  5812. const char *name=conv_str(st,data);
  5813. struct item_data *item_data = itemdb_searchname(name);
  5814. //nameid=UNKNOWN_ITEM_ID;
  5815. if( item_data )
  5816. nameid=item_data->nameid;
  5817. }else
  5818. nameid=conv_num(st,data);
  5819. amount=script_getnum(st,3);
  5820. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5821. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5822. return 0;
  5823. }
  5824. //1st pass
  5825. //here we won't delete items with CARDS, named items but we count them
  5826. for(i=0;i<MAX_INVENTORY;i++){
  5827. //we don't delete wrong item or equipped item
  5828. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5829. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5830. continue;
  5831. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5832. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5833. sd->status.inventory[i].card[0] == CARD0_PET)
  5834. {
  5835. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5836. continue; //pet couldn't be sent for deletion.
  5837. } else
  5838. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5839. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5840. sd->status.inventory[i].card[0] ||
  5841. sd->status.inventory[i].refine) {
  5842. //this is important item, count it (except for pet eggs)
  5843. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5844. important_item++;
  5845. continue;
  5846. }
  5847. if(sd->status.inventory[i].amount>=amount){
  5848. //Logs items, got from (N)PC scripts [Lupus]
  5849. if(log_config.enable_logs&0x40)
  5850. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5851. pc_delitem(sd,i,amount,0);
  5852. return 0; //we deleted exact amount of items. now exit
  5853. } else {
  5854. amount-=sd->status.inventory[i].amount;
  5855. //Logs items, got from (N)PC scripts [Lupus]
  5856. if(log_config.enable_logs&0x40) {
  5857. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5858. }
  5859. //Logs
  5860. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5861. }
  5862. }
  5863. //2nd pass
  5864. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5865. if (important_item>0 && amount>0)
  5866. for(i=0;i<MAX_INVENTORY;i++){
  5867. //we don't delete wrong item
  5868. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5869. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5870. continue;
  5871. if(sd->status.inventory[i].amount>=amount){
  5872. //Logs items, got from (N)PC scripts [Lupus]
  5873. if(log_config.enable_logs&0x40)
  5874. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5875. pc_delitem(sd,i,amount,0);
  5876. return 0; //we deleted exact amount of items. now exit
  5877. } else {
  5878. amount-=sd->status.inventory[i].amount;
  5879. //Logs items, got from (N)PC scripts [Lupus]
  5880. if(log_config.enable_logs&0x40)
  5881. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5882. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5883. }
  5884. }
  5885. return 0;
  5886. }
  5887. /*==========================================
  5888. * advanced version of delitem [modified by Mihilion]
  5889. *------------------------------------------*/
  5890. BUILDIN_FUNC(delitem2)
  5891. {
  5892. int nameid=0,amount,i=0;
  5893. int iden,ref,attr,c1,c2,c3,c4;
  5894. TBL_PC *sd;
  5895. struct script_data *data;
  5896. sd = script_rid2sd(st);
  5897. data=script_getdata(st,2);
  5898. get_val(st,data);
  5899. if( data_isstring(data) ){
  5900. const char *name=conv_str(st,data);
  5901. struct item_data *item_data = itemdb_searchname(name);
  5902. //nameid=UNKNOWN_ITEM_ID;
  5903. if( item_data )
  5904. nameid=item_data->nameid;
  5905. }else
  5906. nameid=conv_num(st,data);
  5907. amount=script_getnum(st,3);
  5908. iden=script_getnum(st,4);
  5909. ref=script_getnum(st,5);
  5910. attr=script_getnum(st,6);
  5911. c1=script_getnum(st,7);
  5912. c2=script_getnum(st,8);
  5913. c3=script_getnum(st,9);
  5914. c4=script_getnum(st,10);
  5915. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5916. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5917. return 0;
  5918. }
  5919. for(i=0;i<MAX_INVENTORY;i++){
  5920. //we don't delete wrong item or equipped item
  5921. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5922. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5923. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5924. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5925. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5926. sd->status.inventory[i].card[3]!=c4)
  5927. continue;
  5928. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5929. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5930. {
  5931. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5932. continue; //Failed to send delete the pet.
  5933. }
  5934. if(sd->status.inventory[i].amount>=amount){
  5935. //Logs items, got from (N)PC scripts [Lupus]
  5936. if(log_config.enable_logs&0x40)
  5937. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5938. pc_delitem(sd,i,amount,0);
  5939. return 0; //we deleted exact amount of items. now exit
  5940. } else {
  5941. amount-=sd->status.inventory[i].amount;
  5942. //Logs items, got from (N)PC scripts [Lupus]
  5943. if(log_config.enable_logs&0x40)
  5944. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5945. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5946. }
  5947. }
  5948. return 0;
  5949. }
  5950. /*==========================================
  5951. * Enables/Disables use of items while in an NPC [Skotlex]
  5952. *------------------------------------------*/
  5953. BUILDIN_FUNC(enableitemuse)
  5954. {
  5955. TBL_PC *sd;
  5956. sd=script_rid2sd(st);
  5957. if (sd)
  5958. sd->npc_item_flag = st->oid;
  5959. return 0;
  5960. }
  5961. BUILDIN_FUNC(disableitemuse)
  5962. {
  5963. TBL_PC *sd;
  5964. sd=script_rid2sd(st);
  5965. if (sd)
  5966. sd->npc_item_flag = 0;
  5967. return 0;
  5968. }
  5969. /*==========================================
  5970. *キャラ関係のパラメータ取得
  5971. *------------------------------------------*/
  5972. BUILDIN_FUNC(readparam)
  5973. {
  5974. int type;
  5975. TBL_PC *sd;
  5976. type=script_getnum(st,2);
  5977. if( script_hasdata(st,3) )
  5978. sd=map_nick2sd(script_getstr(st,3));
  5979. else
  5980. sd=script_rid2sd(st);
  5981. if(sd==NULL){
  5982. script_pushint(st,-1);
  5983. return 0;
  5984. }
  5985. script_pushint(st,pc_readparam(sd,type));
  5986. return 0;
  5987. }
  5988. /*==========================================
  5989. *キャラ関係のID取得
  5990. *------------------------------------------*/
  5991. BUILDIN_FUNC(getcharid)
  5992. {
  5993. int num;
  5994. TBL_PC *sd;
  5995. num=script_getnum(st,2);
  5996. if( script_hasdata(st,3) )
  5997. sd=map_nick2sd(script_getstr(st,3));
  5998. else
  5999. sd=script_rid2sd(st);
  6000. if(sd==NULL || num<0 || num>3){
  6001. script_pushint(st,0); //return 0, according docs
  6002. return 0;
  6003. }
  6004. switch (num) {
  6005. case 0: script_pushint(st,sd->status.char_id); break;
  6006. case 1: script_pushint(st,sd->status.party_id); break;
  6007. case 2: script_pushint(st,sd->status.guild_id); break;
  6008. case 3: script_pushint(st,sd->status.account_id); break;
  6009. default:
  6010. ShowError("buildin_getcharid: invalid .\n");
  6011. script_pushconststr(st,"");
  6012. break;
  6013. }
  6014. return 0;
  6015. }
  6016. /*==========================================
  6017. *指定IDのPT名取得
  6018. *------------------------------------------*/
  6019. char *buildin_getpartyname_sub(int party_id)
  6020. {
  6021. struct party_data *p;
  6022. p=party_search(party_id);
  6023. if(p!=NULL){
  6024. char *buf;
  6025. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  6026. memcpy(buf, p->party.name, NAME_LENGTH);
  6027. buf[NAME_LENGTH-1] = '\0';
  6028. return buf;
  6029. }
  6030. return 0;
  6031. }
  6032. BUILDIN_FUNC(getpartyname)
  6033. {
  6034. char *name;
  6035. int party_id;
  6036. party_id=script_getnum(st,2);
  6037. name=buildin_getpartyname_sub(party_id);
  6038. if(name != NULL)
  6039. script_pushstr(st,name);
  6040. else
  6041. script_pushconststr(st,"null");
  6042. return 0;
  6043. }
  6044. /*==========================================
  6045. *指定IDのPT人数とメンバーID取得
  6046. *------------------------------------------*/
  6047. BUILDIN_FUNC(getpartymember)
  6048. {
  6049. struct party_data *p;
  6050. int i,j=0,type=0;
  6051. p=party_search(script_getnum(st,2));
  6052. if( script_hasdata(st,3) )
  6053. type=script_getnum(st,3);
  6054. if(p!=NULL){
  6055. for(i=0;i<MAX_PARTY;i++){
  6056. if(p->party.member[i].account_id){
  6057. switch (type) {
  6058. case 2:
  6059. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  6060. break;
  6061. case 1:
  6062. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  6063. break;
  6064. default:
  6065. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  6066. }
  6067. j++;
  6068. }
  6069. }
  6070. }
  6071. mapreg_setreg(add_str("$@partymembercount"),j);
  6072. return 0;
  6073. }
  6074. /*==========================================
  6075. * Retrieves party leader. if flag is specified,
  6076. * return some of the leader data. Otherwise, return name.
  6077. *------------------------------------------*/
  6078. BUILDIN_FUNC(getpartyleader)
  6079. {
  6080. int party_id, type = 0, i=0;
  6081. struct party_data *p;
  6082. party_id=script_getnum(st,2);
  6083. if( script_hasdata(st,3) )
  6084. type=script_getnum(st,3);
  6085. p=party_search(party_id);
  6086. if (p) //Search leader
  6087. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6088. if (!p || i == MAX_PARTY) { //leader not found
  6089. if (type)
  6090. script_pushint(st,-1);
  6091. else
  6092. script_pushconststr(st,"null");
  6093. return 0;
  6094. }
  6095. switch (type) {
  6096. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6097. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6098. case 3: script_pushint(st,p->party.member[i].class_); break;
  6099. case 4: script_pushstr(st,aStrdup(mapindex_id2name(p->party.member[i].map))); break;
  6100. case 5: script_pushint(st,p->party.member[i].lv); break;
  6101. default: script_pushstr(st,aStrdup(p->party.member[i].name)); break;
  6102. }
  6103. return 0;
  6104. }
  6105. /*==========================================
  6106. *指定IDのギルド名取得
  6107. *------------------------------------------*/
  6108. char *buildin_getguildname_sub(int guild_id)
  6109. {
  6110. struct guild *g=NULL;
  6111. g=guild_search(guild_id);
  6112. if(g!=NULL){
  6113. char *buf;
  6114. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  6115. memcpy(buf, g->name, NAME_LENGTH);
  6116. buf[NAME_LENGTH-1] = '\0';
  6117. return buf;
  6118. }
  6119. return NULL;
  6120. }
  6121. BUILDIN_FUNC(getguildname)
  6122. {
  6123. char *name;
  6124. int guild_id=script_getnum(st,2);
  6125. name=buildin_getguildname_sub(guild_id);
  6126. if(name != NULL)
  6127. script_pushstr(st,name);
  6128. else
  6129. script_pushconststr(st,"null");
  6130. return 0;
  6131. }
  6132. /*==========================================
  6133. *指定IDのGuildMaster名取得
  6134. *------------------------------------------*/
  6135. char *buildin_getguildmaster_sub(int guild_id)
  6136. {
  6137. struct guild *g=NULL;
  6138. g=guild_search(guild_id);
  6139. if(g!=NULL){
  6140. char *buf;
  6141. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  6142. memcpy(buf, g->master, NAME_LENGTH);
  6143. buf[NAME_LENGTH-1] = '\0';
  6144. return buf;
  6145. }
  6146. return 0;
  6147. }
  6148. BUILDIN_FUNC(getguildmaster)
  6149. {
  6150. char *master;
  6151. int guild_id=script_getnum(st,2);
  6152. master=buildin_getguildmaster_sub(guild_id);
  6153. if(master!=0)
  6154. script_pushstr(st,master);
  6155. else
  6156. script_pushconststr(st,"null");
  6157. return 0;
  6158. }
  6159. BUILDIN_FUNC(getguildmasterid)
  6160. {
  6161. char *master;
  6162. TBL_PC *sd=NULL;
  6163. int guild_id=script_getnum(st,2);
  6164. master=buildin_getguildmaster_sub(guild_id);
  6165. if(master!=0){
  6166. if((sd=map_nick2sd(master)) == NULL){
  6167. script_pushint(st,0);
  6168. return 0;
  6169. }
  6170. script_pushint(st,sd->status.char_id);
  6171. }else{
  6172. script_pushint(st,0);
  6173. }
  6174. return 0;
  6175. }
  6176. /*==========================================
  6177. * キャラクタの名前
  6178. *------------------------------------------*/
  6179. BUILDIN_FUNC(strcharinfo)
  6180. {
  6181. TBL_PC *sd;
  6182. int num;
  6183. char *buf;
  6184. sd=script_rid2sd(st);
  6185. if (!sd) { //Avoid crashing....
  6186. script_pushconststr(st,"");
  6187. return 0;
  6188. }
  6189. num=script_getnum(st,2);
  6190. switch(num){
  6191. case 0:
  6192. script_pushstr(st,aStrdup(sd->status.name));
  6193. break;
  6194. case 1:
  6195. buf=buildin_getpartyname_sub(sd->status.party_id);
  6196. if(buf!=0)
  6197. script_pushstr(st,buf);
  6198. else
  6199. script_pushconststr(st,"");
  6200. break;
  6201. case 2:
  6202. buf=buildin_getguildname_sub(sd->status.guild_id);
  6203. if(buf != NULL)
  6204. script_pushstr(st,buf);
  6205. else
  6206. script_pushconststr(st,"");
  6207. break;
  6208. default:
  6209. ShowWarning("buildin_strcharinfo: unknown parameter.");
  6210. script_pushconststr(st,"");
  6211. break;
  6212. }
  6213. return 0;
  6214. }
  6215. unsigned int equip[10]={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};
  6216. /*==========================================
  6217. * GetEquipID(Pos); Pos: 1-10
  6218. *------------------------------------------*/
  6219. BUILDIN_FUNC(getequipid)
  6220. {
  6221. int i=-1,num;
  6222. TBL_PC *sd;
  6223. struct item_data* item;
  6224. sd=script_rid2sd(st);
  6225. if(sd == NULL)
  6226. {
  6227. ShowError("getequipid: sd == NULL\n");
  6228. return 0;
  6229. }
  6230. num=script_getnum(st,2);
  6231. if (num > 0 && num <= ARRAYLENGTH(equip))
  6232. i=pc_checkequip(sd,equip[num-1]);
  6233. if(i >= 0){
  6234. item=sd->inventory_data[i];
  6235. if(item)
  6236. script_pushint(st,item->nameid);
  6237. else
  6238. script_pushint(st,0);
  6239. }else{
  6240. script_pushint(st,-1);
  6241. }
  6242. return 0;
  6243. }
  6244. /*==========================================
  6245. * 装備名文字列(精錬メニュー用)
  6246. *------------------------------------------*/
  6247. BUILDIN_FUNC(getequipname)
  6248. {
  6249. int i=-1,num;
  6250. TBL_PC *sd;
  6251. struct item_data* item;
  6252. char *buf;
  6253. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  6254. buf=(char *)aMallocA(64*sizeof(char));
  6255. sd=script_rid2sd(st);
  6256. num=script_getnum(st,2);
  6257. if (num > 0 && num <= ARRAYLENGTH(equip))
  6258. i=pc_checkequip(sd,equip[num-1]);
  6259. if(i >= 0){
  6260. item=sd->inventory_data[i];
  6261. if(item)
  6262. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  6263. else
  6264. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  6265. }else{
  6266. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  6267. }
  6268. script_pushstr(st,buf);
  6269. return 0;
  6270. }
  6271. /*==========================================
  6272. * getbrokenid [Valaris]
  6273. *------------------------------------------*/
  6274. BUILDIN_FUNC(getbrokenid)
  6275. {
  6276. int i,num,id=0,brokencounter=0;
  6277. TBL_PC *sd;
  6278. sd=script_rid2sd(st);
  6279. num=script_getnum(st,2);
  6280. for(i=0; i<MAX_INVENTORY; i++) {
  6281. if(sd->status.inventory[i].attribute==1){
  6282. brokencounter++;
  6283. if(num==brokencounter){
  6284. id=sd->status.inventory[i].nameid;
  6285. break;
  6286. }
  6287. }
  6288. }
  6289. script_pushint(st,id);
  6290. return 0;
  6291. }
  6292. /*==========================================
  6293. * repair [Valaris]
  6294. *------------------------------------------*/
  6295. BUILDIN_FUNC(repair)
  6296. {
  6297. int i,num;
  6298. int repaircounter=0;
  6299. TBL_PC *sd;
  6300. sd=script_rid2sd(st);
  6301. num=script_getnum(st,2);
  6302. for(i=0; i<MAX_INVENTORY; i++) {
  6303. if(sd->status.inventory[i].attribute==1){
  6304. repaircounter++;
  6305. if(num==repaircounter){
  6306. sd->status.inventory[i].attribute=0;
  6307. clif_equiplist(sd);
  6308. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6309. clif_misceffect(&sd->bl, 3);
  6310. break;
  6311. }
  6312. }
  6313. }
  6314. return 0;
  6315. }
  6316. /*==========================================
  6317. * 装備チェック
  6318. *------------------------------------------*/
  6319. BUILDIN_FUNC(getequipisequiped)
  6320. {
  6321. int i=-1,num;
  6322. TBL_PC *sd;
  6323. num=script_getnum(st,2);
  6324. sd=script_rid2sd(st);
  6325. if (num > 0 && num <= ARRAYLENGTH(equip))
  6326. i=pc_checkequip(sd,equip[num-1]);
  6327. if(i >= 0)
  6328. script_pushint(st,1);
  6329. else
  6330. script_pushint(st,0);
  6331. return 0;
  6332. }
  6333. /*==========================================
  6334. * 装備品精錬可能チェック
  6335. *------------------------------------------*/
  6336. BUILDIN_FUNC(getequipisenableref)
  6337. {
  6338. int i=-1,num;
  6339. TBL_PC *sd;
  6340. num=script_getnum(st,2);
  6341. sd=script_rid2sd(st);
  6342. if (num > 0 && num <= ARRAYLENGTH(equip))
  6343. i=pc_checkequip(sd,equip[num-1]);
  6344. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  6345. {
  6346. script_pushint(st,1);
  6347. } else {
  6348. script_pushint(st,0);
  6349. }
  6350. return 0;
  6351. }
  6352. /*==========================================
  6353. * 装備品鑑定チェック
  6354. *------------------------------------------*/
  6355. BUILDIN_FUNC(getequipisidentify)
  6356. {
  6357. int i=-1,num;
  6358. TBL_PC *sd;
  6359. num=script_getnum(st,2);
  6360. sd=script_rid2sd(st);
  6361. if (num > 0 && num <= ARRAYLENGTH(equip))
  6362. i=pc_checkequip(sd,equip[num-1]);
  6363. if(i >= 0)
  6364. script_pushint(st,sd->status.inventory[i].identify);
  6365. else
  6366. script_pushint(st,0);
  6367. return 0;
  6368. }
  6369. /*==========================================
  6370. * 装備品精錬度
  6371. *------------------------------------------*/
  6372. BUILDIN_FUNC(getequiprefinerycnt)
  6373. {
  6374. int i=-1,num;
  6375. TBL_PC *sd;
  6376. num=script_getnum(st,2);
  6377. sd=script_rid2sd(st);
  6378. if (num > 0 && num <= ARRAYLENGTH(equip))
  6379. i=pc_checkequip(sd,equip[num-1]);
  6380. if(i >= 0)
  6381. script_pushint(st,sd->status.inventory[i].refine);
  6382. else
  6383. script_pushint(st,0);
  6384. return 0;
  6385. }
  6386. /*==========================================
  6387. * 装備品武器LV
  6388. *------------------------------------------*/
  6389. BUILDIN_FUNC(getequipweaponlv)
  6390. {
  6391. int i=-1,num;
  6392. TBL_PC *sd;
  6393. num=script_getnum(st,2);
  6394. sd=script_rid2sd(st);
  6395. if (num > 0 && num <= ARRAYLENGTH(equip))
  6396. i=pc_checkequip(sd,equip[num-1]);
  6397. if(i >= 0 && sd->inventory_data[i])
  6398. script_pushint(st,sd->inventory_data[i]->wlv);
  6399. else
  6400. script_pushint(st,0);
  6401. return 0;
  6402. }
  6403. /*==========================================
  6404. * 装備品精錬成功率
  6405. *------------------------------------------*/
  6406. BUILDIN_FUNC(getequippercentrefinery)
  6407. {
  6408. int i=-1,num;
  6409. TBL_PC *sd;
  6410. num=script_getnum(st,2);
  6411. sd=script_rid2sd(st);
  6412. if (num > 0 && num <= ARRAYLENGTH(equip))
  6413. i=pc_checkequip(sd,equip[num-1]);
  6414. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6415. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  6416. else
  6417. script_pushint(st,0);
  6418. return 0;
  6419. }
  6420. /*==========================================
  6421. * 精錬成功
  6422. *------------------------------------------*/
  6423. BUILDIN_FUNC(successrefitem)
  6424. {
  6425. int i=-1,num,ep;
  6426. TBL_PC *sd;
  6427. num=script_getnum(st,2);
  6428. sd=script_rid2sd(st);
  6429. if (num > 0 && num <= ARRAYLENGTH(equip))
  6430. i=pc_checkequip(sd,equip[num-1]);
  6431. if(i >= 0) {
  6432. ep=sd->status.inventory[i].equip;
  6433. //Logs items, got from (N)PC scripts [Lupus]
  6434. if(log_config.enable_logs&0x40)
  6435. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6436. sd->status.inventory[i].refine++;
  6437. pc_unequipitem(sd,i,2);
  6438. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6439. clif_delitem(sd,i,1);
  6440. //Logs items, got from (N)PC scripts [Lupus]
  6441. if(log_config.enable_logs&0x40)
  6442. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  6443. clif_additem(sd,i,1,0);
  6444. pc_equipitem(sd,i,ep);
  6445. clif_misceffect(&sd->bl,3);
  6446. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6447. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6448. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6449. ){ // Fame point system [DracoRPG]
  6450. switch (sd->inventory_data[i]->wlv){
  6451. case 1:
  6452. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6453. break;
  6454. case 2:
  6455. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6456. break;
  6457. case 3:
  6458. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6459. break;
  6460. }
  6461. }
  6462. }
  6463. return 0;
  6464. }
  6465. /*==========================================
  6466. * 精錬失敗
  6467. *------------------------------------------*/
  6468. BUILDIN_FUNC(failedrefitem)
  6469. {
  6470. int i=-1,num;
  6471. TBL_PC *sd;
  6472. num=script_getnum(st,2);
  6473. sd=script_rid2sd(st);
  6474. if (num > 0 && num <= ARRAYLENGTH(equip))
  6475. i=pc_checkequip(sd,equip[num-1]);
  6476. if(i >= 0) {
  6477. //Logs items, got from (N)PC scripts [Lupus]
  6478. if(log_config.enable_logs&0x40)
  6479. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6480. sd->status.inventory[i].refine = 0;
  6481. pc_unequipitem(sd,i,3);
  6482. // 精錬失敗エフェクトのパケット
  6483. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6484. pc_delitem(sd,i,1,0);
  6485. // 他の人にも失敗を通知
  6486. clif_misceffect(&sd->bl,2);
  6487. }
  6488. return 0;
  6489. }
  6490. /*==========================================
  6491. *
  6492. *------------------------------------------*/
  6493. BUILDIN_FUNC(statusup)
  6494. {
  6495. int type;
  6496. TBL_PC *sd;
  6497. type=script_getnum(st,2);
  6498. sd=script_rid2sd(st);
  6499. pc_statusup(sd,type);
  6500. return 0;
  6501. }
  6502. /*==========================================
  6503. *
  6504. *------------------------------------------*/
  6505. BUILDIN_FUNC(statusup2)
  6506. {
  6507. int type,val;
  6508. TBL_PC *sd;
  6509. type=script_getnum(st,2);
  6510. val=script_getnum(st,3);
  6511. sd=script_rid2sd(st);
  6512. pc_statusup2(sd,type,val);
  6513. return 0;
  6514. }
  6515. /// See 'doc/item_bonus.txt'
  6516. ///
  6517. /// bonus <bonus type>,<val1>;
  6518. /// bonus2 <bonus type>,<val1>,<val2>;
  6519. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6520. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6521. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6522. BUILDIN_FUNC(bonus)
  6523. {
  6524. int type;
  6525. int type2;
  6526. int type3;
  6527. int type4;
  6528. int type5;
  6529. int val;
  6530. TBL_PC* sd;
  6531. sd = script_rid2sd(st);
  6532. if( sd == NULL )
  6533. return 1; // no player attached
  6534. type = script_getnum(st,2);
  6535. switch( script_lastdata(st) ){
  6536. case 3:
  6537. val = script_getnum(st,3);
  6538. pc_bonus(sd, type, val);
  6539. break;
  6540. case 4:
  6541. type2 = script_getnum(st,3);
  6542. val = script_getnum(st,4);
  6543. pc_bonus2(sd, type, type2, val);
  6544. break;
  6545. case 5:
  6546. type2 = script_getnum(st,3);
  6547. type3 = script_getnum(st,4);
  6548. val = script_getnum(st,5);
  6549. pc_bonus3(sd, type, type2, type3, val);
  6550. break;
  6551. case 6:
  6552. type2 = script_getnum(st,3);
  6553. type3 = script_getnum(st,4);
  6554. type4 = script_getnum(st,5);
  6555. val = script_getnum(st,6);
  6556. pc_bonus4(sd, type, type2, type3, type4, val);
  6557. break;
  6558. case 7:
  6559. type2 = script_getnum(st,3);
  6560. type3 = script_getnum(st,4);
  6561. type4 = script_getnum(st,5);
  6562. type5 = script_getnum(st,6);
  6563. val = script_getnum(st,7);
  6564. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  6565. break;
  6566. default:
  6567. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 2));
  6568. }
  6569. return 0;
  6570. }
  6571. /// Bonus script that has a chance of being executed on attack.
  6572. BUILDIN_FUNC(bonusautoscript)
  6573. {
  6574. int rate, flag = 0;
  6575. const char *str;
  6576. struct script_code *script;
  6577. TBL_PC* sd;
  6578. sd = script_rid2sd(st);
  6579. if( sd == NULL )
  6580. return 1;// no player attached, report source
  6581. str = script_getstr(st,2);
  6582. rate = script_getnum(st,3);
  6583. if( script_hasdata(st,4) )
  6584. flag = script_getnum(st,4);
  6585. script = parse_script(str, "autoscript bonus", 0, 0);
  6586. if (!script)
  6587. return 1;
  6588. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  6589. {
  6590. script_free_code(script);
  6591. return 1;
  6592. }
  6593. return 0;
  6594. }
  6595. /// Bonus script that has a chance of being executed when attacked.
  6596. BUILDIN_FUNC(bonusautoscript2)
  6597. {
  6598. int rate, flag = 0;
  6599. const char *str;
  6600. struct script_code *script;
  6601. TBL_PC* sd;
  6602. sd = script_rid2sd(st);
  6603. if( sd == NULL )
  6604. return 1;// no player attached, report source
  6605. str = script_getstr(st,2);
  6606. rate = script_getnum(st,3);
  6607. if( script_hasdata(st,4) )
  6608. flag = script_getnum(st,4);
  6609. script = parse_script(str, "autoscript2 bonus", 0, 0);
  6610. if (!script)
  6611. return 1;
  6612. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  6613. {
  6614. script_free_code(script);
  6615. return 1;
  6616. }
  6617. return 0;
  6618. }
  6619. /// Changes the level of a player skill.
  6620. /// <flag> defaults to 1
  6621. /// <flag>=0 : set the level of the skill
  6622. /// <flag>=1 : set the temporary level of the skill
  6623. /// <flag>=2 : add to the level of the skill
  6624. ///
  6625. /// skill <skill id>,<level>,<flag>
  6626. /// skill <skill id>,<level>
  6627. BUILDIN_FUNC(skill)
  6628. {
  6629. int id;
  6630. int level;
  6631. int flag = 1;
  6632. TBL_PC* sd;
  6633. sd = script_rid2sd(st);
  6634. if( sd == NULL )
  6635. return 1;// no player attached, report source
  6636. id = script_getnum(st,2);
  6637. level = script_getnum(st,3);
  6638. if( script_hasdata(st,4) )
  6639. flag = script_getnum(st,4);
  6640. pc_skill(sd, id, level, flag);
  6641. return 0;
  6642. }
  6643. /// Changes the level of a player skill.
  6644. /// like skill, but <flag> defaults to 2
  6645. ///
  6646. /// addtoskill <skill id>,<amount>,<flag>
  6647. /// addtoskill <skill id>,<amount>
  6648. ///
  6649. /// @see skill
  6650. BUILDIN_FUNC(addtoskill)
  6651. {
  6652. int id;
  6653. int level;
  6654. int flag = 2;
  6655. TBL_PC* sd;
  6656. sd = script_rid2sd(st);
  6657. if( sd == NULL )
  6658. return 1;// no player attached, report source
  6659. id = script_getnum(st,2);
  6660. level = script_getnum(st,3);
  6661. if( script_hasdata(st,4) )
  6662. flag = script_getnum(st,4);
  6663. pc_skill(sd, id, level, flag);
  6664. return 0;
  6665. }
  6666. /// Increases the level of a guild skill.
  6667. ///
  6668. /// guildskill <skill id>,<amount>;
  6669. BUILDIN_FUNC(guildskill)
  6670. {
  6671. int id;
  6672. int level;
  6673. TBL_PC* sd;
  6674. int i;
  6675. sd = script_rid2sd(st);
  6676. if( sd == NULL )
  6677. return 1;// no player attached, report source
  6678. id = script_getnum(st,2);
  6679. level = script_getnum(st,3);
  6680. for( i=0; i < level; i++ )
  6681. guild_skillup(sd, id);
  6682. return 0;
  6683. }
  6684. /// Returns the level of the player skill.
  6685. ///
  6686. /// getskilllv(<skill id>) -> <level>
  6687. BUILDIN_FUNC(getskilllv)
  6688. {
  6689. int id;
  6690. TBL_PC* sd;
  6691. sd = script_rid2sd(st);
  6692. if( sd == NULL )
  6693. return 1;// no player attached, report source
  6694. id = script_getnum(st,2);
  6695. script_pushint(st, pc_checkskill(sd,id));
  6696. return 0;
  6697. }
  6698. /// Returns the level of the guild skill.
  6699. ///
  6700. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6701. BUILDIN_FUNC(getgdskilllv)
  6702. {
  6703. int guild_id;
  6704. int skill_id;
  6705. struct guild* g;
  6706. guild_id = script_getnum(st,2);
  6707. skill_id = script_getnum(st,3);
  6708. g = guild_search(guild_id);
  6709. if( g == NULL )
  6710. script_pushint(st, -1);
  6711. else
  6712. script_pushint(st, guild_checkskill(g,skill_id));
  6713. return 0;
  6714. }
  6715. /// Returns the 'basic_skill_check' setting.
  6716. /// This config determines if the server checks the skill level of NV_BASIC
  6717. /// before allowing the basic actions.
  6718. ///
  6719. /// basicskillcheck() -> <bool>
  6720. BUILDIN_FUNC(basicskillcheck)
  6721. {
  6722. script_pushint(st, battle_config.basic_skill_check);
  6723. return 0;
  6724. }
  6725. /// Returns the GM level of the player.
  6726. ///
  6727. /// getgmlevel() -> <level>
  6728. BUILDIN_FUNC(getgmlevel)
  6729. {
  6730. TBL_PC* sd;
  6731. sd = script_rid2sd(st);
  6732. if( sd == NULL )
  6733. return 1;// no player attached, report source
  6734. script_pushint(st, pc_isGM(sd));
  6735. return 0;
  6736. }
  6737. /// Terminates the execution of this script instance.
  6738. ///
  6739. /// end
  6740. BUILDIN_FUNC(end)
  6741. {
  6742. st->state = END;
  6743. return 0;
  6744. }
  6745. /// Checks if the player has that effect state (option).
  6746. ///
  6747. /// checkoption(<option>) -> <bool>
  6748. BUILDIN_FUNC(checkoption)
  6749. {
  6750. int option;
  6751. TBL_PC* sd;
  6752. sd = script_rid2sd(st);
  6753. if( sd == NULL )
  6754. return 1;// no player attached, report source
  6755. option = script_getnum(st,2);
  6756. if( sd->sc.option&option )
  6757. script_pushint(st, 1);
  6758. else
  6759. script_pushint(st, 0);
  6760. return 0;
  6761. }
  6762. /// Checks if the player is in that body state (opt1).
  6763. ///
  6764. /// checkoption1(<opt1>) -> <bool>
  6765. BUILDIN_FUNC(checkoption1)
  6766. {
  6767. int opt1;
  6768. TBL_PC* sd;
  6769. sd = script_rid2sd(st);
  6770. if( sd == NULL )
  6771. return 1;// no player attached, report source
  6772. opt1 = script_getnum(st,2);
  6773. if( sd->sc.opt1 == opt1 )
  6774. script_pushint(st, 1);
  6775. else
  6776. script_pushint(st, 0);
  6777. return 0;
  6778. }
  6779. /// Checks if the player has that health state (opt2).
  6780. ///
  6781. /// checkoption2(<opt2>) -> <bool>
  6782. BUILDIN_FUNC(checkoption2)
  6783. {
  6784. int opt2;
  6785. TBL_PC* sd;
  6786. sd = script_rid2sd(st);
  6787. if( sd == NULL )
  6788. return 1;// no player attached, report source
  6789. opt2 = script_getnum(st,2);
  6790. if( sd->sc.opt2&opt2 )
  6791. script_pushint(st, 1);
  6792. else
  6793. script_pushint(st, 0);
  6794. return 0;
  6795. }
  6796. /// Changes the effect state (option) of the player.
  6797. /// <flag> defaults to 1
  6798. /// <flag>=0 : removes the option
  6799. /// <flag>=other : adds the option
  6800. ///
  6801. /// setoption <option>,<flag>;
  6802. /// setoption <option>;
  6803. BUILDIN_FUNC(setoption)
  6804. {
  6805. int option;
  6806. int flag = 1;
  6807. TBL_PC* sd;
  6808. sd = script_rid2sd(st);
  6809. if( sd == NULL )
  6810. return 1;// no player attached, report source
  6811. option = script_getnum(st,2);
  6812. if( script_hasdata(st,3) )
  6813. flag = script_getnum(st,3);
  6814. else if( !option ){// Request to remove everything.
  6815. flag = 0;
  6816. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6817. }
  6818. if( flag ){// Add option
  6819. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6820. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6821. pc_setoption(sd, sd->sc.option|option);
  6822. } else// Remove option
  6823. pc_setoption(sd, sd->sc.option&~option);
  6824. return 0;
  6825. }
  6826. /// Returns if the player has a cart.
  6827. ///
  6828. /// checkcart() -> <bool>
  6829. ///
  6830. /// @author Valaris
  6831. BUILDIN_FUNC(checkcart)
  6832. {
  6833. TBL_PC* sd;
  6834. sd = script_rid2sd(st);
  6835. if( sd == NULL )
  6836. return 1;// no player attached, report source
  6837. if( pc_iscarton(sd) )
  6838. script_pushint(st, 1);
  6839. else
  6840. script_pushint(st, 0);
  6841. return 0;
  6842. }
  6843. /// Sets the cart of the player.
  6844. /// <type> defaults to 1
  6845. /// <type>=0 : removes the cart
  6846. /// <type>=1 : Normal cart
  6847. /// <type>=2 : Wooden cart
  6848. /// <type>=3 : Covered cart with flowers and ferns
  6849. /// <type>=4 : Wooden cart with a Panda doll on the back
  6850. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6851. ///
  6852. /// setcart <type>;
  6853. /// setcart;
  6854. BUILDIN_FUNC(setcart)
  6855. {
  6856. int type = 1;
  6857. TBL_PC* sd;
  6858. sd = script_rid2sd(st);
  6859. if( sd == NULL )
  6860. return 1;// no player attached, report source
  6861. if( script_hasdata(st,2) )
  6862. type = script_getnum(st,2);
  6863. pc_setcart(sd, type);
  6864. return 0;
  6865. }
  6866. /// Returns if the player has a falcon.
  6867. ///
  6868. /// checkfalcon() -> <bool>
  6869. ///
  6870. /// @author Valaris
  6871. BUILDIN_FUNC(checkfalcon)
  6872. {
  6873. TBL_PC* sd;
  6874. sd = script_rid2sd(st);
  6875. if( sd == NULL )
  6876. return 1;// no player attached, report source
  6877. if( pc_isfalcon(sd) )
  6878. script_pushint(st, 1);
  6879. else
  6880. script_pushint(st, 0);
  6881. return 0;
  6882. }
  6883. /// Sets if the player has a falcon or not.
  6884. /// <flag> defaults to 1
  6885. ///
  6886. /// setfalcon <flag>;
  6887. /// setfalcon;
  6888. BUILDIN_FUNC(setfalcon)
  6889. {
  6890. int flag = 1;
  6891. TBL_PC* sd;
  6892. sd = script_rid2sd(st);
  6893. if( sd == NULL )
  6894. return 1;// no player attached, report source
  6895. if( script_hasdata(st,2) )
  6896. flag = script_getnum(st,2);
  6897. pc_setfalcon(sd, flag);
  6898. return 0;
  6899. }
  6900. /// Returns if the player is riding.
  6901. ///
  6902. /// checkriding() -> <bool>
  6903. ///
  6904. /// @author Valaris
  6905. BUILDIN_FUNC(checkriding)
  6906. {
  6907. TBL_PC* sd;
  6908. sd = script_rid2sd(st);
  6909. if( sd == NULL )
  6910. return 1;// no player attached, report source
  6911. if( pc_isriding(sd) )
  6912. script_pushint(st, 1);
  6913. else
  6914. script_pushint(st, 0);
  6915. return 0;
  6916. }
  6917. /// Sets if the player is riding.
  6918. /// <flag> defaults to 1
  6919. ///
  6920. /// setriding <flag>;
  6921. /// setriding;
  6922. BUILDIN_FUNC(setriding)
  6923. {
  6924. int flag = 1;
  6925. TBL_PC* sd;
  6926. sd = script_rid2sd(st);
  6927. if( sd == NULL )
  6928. return 1;// no player attached, report source
  6929. if( script_hasdata(st,2) )
  6930. flag = script_getnum(st,2);
  6931. pc_setriding(sd, flag);
  6932. return 0;
  6933. }
  6934. /// Sets the save point of the player.
  6935. ///
  6936. /// save "<map name>",<x>,<y>
  6937. /// savepoint "<map name>",<x>,<y>
  6938. BUILDIN_FUNC(savepoint)
  6939. {
  6940. int x;
  6941. int y;
  6942. short map;
  6943. const char* str;
  6944. TBL_PC* sd;
  6945. sd = script_rid2sd(st);
  6946. if( sd == NULL )
  6947. return 1;// no player attached, report source
  6948. str = script_getstr(st, 2);
  6949. x = script_getnum(st,3);
  6950. y = script_getnum(st,4);
  6951. map = mapindex_name2id(str);
  6952. if( map )
  6953. pc_setsavepoint(sd, map, x, y);
  6954. return 0;
  6955. }
  6956. /*==========================================
  6957. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6958. *------------------------------------------*/
  6959. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6960. {
  6961. int type;
  6962. time_t timer;
  6963. struct tm *t;
  6964. type=script_getnum(st,2);
  6965. switch(type){
  6966. case 2:
  6967. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6968. // from the system clock.)
  6969. script_pushint(st,(int)time(NULL));
  6970. break;
  6971. case 1:
  6972. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6973. time(&timer);
  6974. t=localtime(&timer);
  6975. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6976. break;
  6977. case 0:
  6978. default:
  6979. //type 0:(System Ticks)
  6980. script_pushint(st,gettick());
  6981. break;
  6982. }
  6983. return 0;
  6984. }
  6985. /*==========================================
  6986. * GetTime(Type);
  6987. * 1: Sec 2: Min 3: Hour
  6988. * 4: WeekDay 5: MonthDay 6: Month
  6989. * 7: Year
  6990. *------------------------------------------*/
  6991. BUILDIN_FUNC(gettime) /* Asgard Version */
  6992. {
  6993. int type;
  6994. time_t timer;
  6995. struct tm *t;
  6996. type=script_getnum(st,2);
  6997. time(&timer);
  6998. t=localtime(&timer);
  6999. switch(type){
  7000. case 1://Sec(0~59)
  7001. script_pushint(st,t->tm_sec);
  7002. break;
  7003. case 2://Min(0~59)
  7004. script_pushint(st,t->tm_min);
  7005. break;
  7006. case 3://Hour(0~23)
  7007. script_pushint(st,t->tm_hour);
  7008. break;
  7009. case 4://WeekDay(0~6)
  7010. script_pushint(st,t->tm_wday);
  7011. break;
  7012. case 5://MonthDay(01~31)
  7013. script_pushint(st,t->tm_mday);
  7014. break;
  7015. case 6://Month(01~12)
  7016. script_pushint(st,t->tm_mon+1);
  7017. break;
  7018. case 7://Year(20xx)
  7019. script_pushint(st,t->tm_year+1900);
  7020. break;
  7021. case 8://Year Day(01~366)
  7022. script_pushint(st,t->tm_yday+1);
  7023. break;
  7024. default://(format error)
  7025. script_pushint(st,-1);
  7026. break;
  7027. }
  7028. return 0;
  7029. }
  7030. /*==========================================
  7031. * GetTimeStr("TimeFMT", Length);
  7032. *------------------------------------------*/
  7033. BUILDIN_FUNC(gettimestr)
  7034. {
  7035. char *tmpstr;
  7036. const char *fmtstr;
  7037. int maxlen;
  7038. time_t now = time(NULL);
  7039. fmtstr=script_getstr(st,2);
  7040. maxlen=script_getnum(st,3);
  7041. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  7042. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7043. tmpstr[maxlen]='\0';
  7044. script_pushstr(st,tmpstr);
  7045. return 0;
  7046. }
  7047. /*==========================================
  7048. * カプラ倉庫を開く
  7049. *------------------------------------------*/
  7050. BUILDIN_FUNC(openstorage)
  7051. {
  7052. storage_storageopen(script_rid2sd(st));
  7053. return 0;
  7054. }
  7055. BUILDIN_FUNC(guildopenstorage)
  7056. {
  7057. TBL_PC *sd=script_rid2sd(st);
  7058. int ret;
  7059. ret = storage_guild_storageopen(sd);
  7060. script_pushint(st,ret);
  7061. return 0;
  7062. }
  7063. /*==========================================
  7064. * アイテムによるスキル発動
  7065. *------------------------------------------*/
  7066. BUILDIN_FUNC(itemskill)
  7067. {
  7068. int id,lv;
  7069. TBL_PC *sd=script_rid2sd(st);
  7070. id=script_getnum(st,2);
  7071. lv=script_getnum(st,3);
  7072. if(!sd || sd->ud.skilltimer != -1)
  7073. return 0;
  7074. sd->skillitem=id;
  7075. sd->skillitemlv=lv;
  7076. clif_item_skill(sd,id,lv);
  7077. return 0;
  7078. }
  7079. /*==========================================
  7080. * アイテム作成
  7081. *------------------------------------------*/
  7082. BUILDIN_FUNC(produce)
  7083. {
  7084. int trigger;
  7085. TBL_PC *sd=script_rid2sd(st);
  7086. trigger=script_getnum(st,2);
  7087. clif_skill_produce_mix_list(sd, trigger);
  7088. return 0;
  7089. }
  7090. /*==========================================
  7091. * NPCでペット作る
  7092. *------------------------------------------*/
  7093. BUILDIN_FUNC(makepet)
  7094. {
  7095. TBL_PC *sd = script_rid2sd(st);
  7096. int id,pet_id;
  7097. id=script_getnum(st,2);
  7098. pet_id = search_petDB_index(id, PET_CLASS);
  7099. if (pet_id < 0)
  7100. pet_id = search_petDB_index(id, PET_EGG);
  7101. if (pet_id >= 0 && sd) {
  7102. sd->catch_target_class = pet_db[pet_id].class_;
  7103. intif_create_pet(
  7104. sd->status.account_id, sd->status.char_id,
  7105. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7106. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7107. 100, 0, 1, pet_db[pet_id].jname);
  7108. }
  7109. return 0;
  7110. }
  7111. /*==========================================
  7112. * NPCで経験値上げる
  7113. *------------------------------------------*/
  7114. BUILDIN_FUNC(getexp)
  7115. {
  7116. TBL_PC *sd = script_rid2sd(st);
  7117. int base=0,job=0;
  7118. double bonus;
  7119. nullpo_retr(0, sd);
  7120. base=script_getnum(st,2);
  7121. job =script_getnum(st,3);
  7122. if(base<0 || job<0)
  7123. return 0;
  7124. // bonus for npc-given exp
  7125. bonus = battle_config.quest_exp_rate / 100.;
  7126. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7127. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7128. pc_gainexp(sd, NULL, base, job);
  7129. return 0;
  7130. }
  7131. /*==========================================
  7132. * Gain guild exp [Celest]
  7133. *------------------------------------------*/
  7134. BUILDIN_FUNC(guildgetexp)
  7135. {
  7136. TBL_PC *sd = script_rid2sd(st);
  7137. int exp;
  7138. exp = script_getnum(st,2);
  7139. if(exp < 0)
  7140. return 0;
  7141. if(sd && sd->status.guild_id > 0)
  7142. guild_getexp (sd, exp);
  7143. return 0;
  7144. }
  7145. /*==========================================
  7146. * Changes the guild master of a guild [Skotlex]
  7147. *------------------------------------------*/
  7148. BUILDIN_FUNC(guildchangegm)
  7149. {
  7150. TBL_PC *sd;
  7151. int guild_id;
  7152. const char *name;
  7153. guild_id = script_getnum(st,2);
  7154. name = script_getstr(st,3);
  7155. sd=map_nick2sd(name);
  7156. if (!sd)
  7157. script_pushint(st,0);
  7158. else
  7159. script_pushint(st,guild_gm_change(guild_id, sd));
  7160. return 0;
  7161. }
  7162. /*==========================================
  7163. * モンスター発生
  7164. *------------------------------------------*/
  7165. BUILDIN_FUNC(monster)
  7166. {
  7167. int class_,amount,x,y;
  7168. const char *str,*map,*event="";
  7169. map =script_getstr(st,2);
  7170. x =script_getnum(st,3);
  7171. y =script_getnum(st,4);
  7172. str =script_getstr(st,5);
  7173. class_=script_getnum(st,6);
  7174. amount=script_getnum(st,7);
  7175. if( script_hasdata(st,8) ){
  7176. event=script_getstr(st,8);
  7177. check_event(st, event);
  7178. }
  7179. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7180. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7181. return 1;
  7182. }
  7183. mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,amount,event);
  7184. return 0;
  7185. }
  7186. /*==========================================
  7187. * モンスター発生
  7188. *------------------------------------------*/
  7189. BUILDIN_FUNC(areamonster)
  7190. {
  7191. int class_,amount,x0,y0,x1,y1;
  7192. const char *str,*map,*event="";
  7193. map =script_getstr(st,2);
  7194. x0 =script_getnum(st,3);
  7195. y0 =script_getnum(st,4);
  7196. x1 =script_getnum(st,5);
  7197. y1 =script_getnum(st,6);
  7198. str =script_getstr(st,7);
  7199. class_=script_getnum(st,8);
  7200. amount=script_getnum(st,9);
  7201. if( script_hasdata(st,10) ){
  7202. event=script_getstr(st,10);
  7203. check_event(st, event);
  7204. }
  7205. mob_once_spawn_area(map_id2sd(st->rid),map,x0,y0,x1,y1,str,class_,amount,event);
  7206. return 0;
  7207. }
  7208. /*==========================================
  7209. * モンスター削除
  7210. *------------------------------------------*/
  7211. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7212. {
  7213. TBL_MOB* md = (TBL_MOB*)bl;
  7214. char *event=va_arg(ap,char *);
  7215. int allflag=va_arg(ap,int);
  7216. if(!allflag){
  7217. if(strcmp(event,md->npc_event)==0)
  7218. status_kill(bl);
  7219. }else{
  7220. if(!md->spawn)
  7221. status_kill(bl);
  7222. }
  7223. return 0;
  7224. }
  7225. BUILDIN_FUNC(killmonster)
  7226. {
  7227. const char *mapname,*event;
  7228. int m,allflag=0;
  7229. mapname=script_getstr(st,2);
  7230. event=script_getstr(st,3);
  7231. if(strcmp(event,"All")==0)
  7232. allflag = 1;
  7233. else
  7234. check_event(st, event);
  7235. if( (m=map_mapname2mapid(mapname))<0 )
  7236. return 0;
  7237. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7238. return 0;
  7239. }
  7240. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7241. {
  7242. status_kill(bl);
  7243. return 0;
  7244. }
  7245. BUILDIN_FUNC(killmonsterall)
  7246. {
  7247. const char *mapname;
  7248. int m;
  7249. mapname=script_getstr(st,2);
  7250. if( (m=map_mapname2mapid(mapname))<0 )
  7251. return 0;
  7252. map_foreachinmap(buildin_killmonsterall_sub,
  7253. m,BL_MOB);
  7254. return 0;
  7255. }
  7256. /*==========================================
  7257. * Creates a clone of a player.
  7258. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7259. *------------------------------------------*/
  7260. BUILDIN_FUNC(clone)
  7261. {
  7262. TBL_PC *sd, *msd=NULL;
  7263. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7264. unsigned int duration = 0;
  7265. const char *map,*event="";
  7266. map=script_getstr(st,2);
  7267. x=script_getnum(st,3);
  7268. y=script_getnum(st,4);
  7269. event=script_getstr(st,5);
  7270. char_id=script_getnum(st,6);
  7271. if( script_hasdata(st,7) )
  7272. master_id=script_getnum(st,7);
  7273. if( script_hasdata(st,8) )
  7274. mode=script_getnum(st,8);
  7275. if( script_hasdata(st,9) )
  7276. flag=script_getnum(st,9);
  7277. if( script_hasdata(st,10) )
  7278. duration=script_getnum(st,10);
  7279. check_event(st, event);
  7280. m = map_mapname2mapid(map);
  7281. if (m < 0) return 0;
  7282. sd = map_charid2sd(char_id);
  7283. if (master_id) {
  7284. msd = map_charid2sd(master_id);
  7285. if (msd)
  7286. master_id = msd->bl.id;
  7287. else
  7288. master_id = 0;
  7289. }
  7290. if (sd) //Return ID of newly crafted clone.
  7291. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7292. else //Failed to create clone.
  7293. script_pushint(st,0);
  7294. return 0;
  7295. }
  7296. /*==========================================
  7297. * イベント実行
  7298. *------------------------------------------*/
  7299. BUILDIN_FUNC(doevent)
  7300. {
  7301. const char* event = script_getstr(st,2);
  7302. check_event(st, event);
  7303. npc_event(map_id2sd(st->rid),event,0);
  7304. return 0;
  7305. }
  7306. /*==========================================
  7307. * NPC主体イベント実行
  7308. *------------------------------------------*/
  7309. BUILDIN_FUNC(donpcevent)
  7310. {
  7311. const char* event = script_getstr(st,2);
  7312. check_event(st, event);
  7313. npc_event_do(event);
  7314. return 0;
  7315. }
  7316. /// for Aegis compatibility
  7317. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7318. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7319. {
  7320. const char* npc = script_getstr(st,2);
  7321. const char* command = script_getstr(st,3);
  7322. char event[51];
  7323. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  7324. check_event(st, event);
  7325. npc_event_do(event);
  7326. return 0;
  7327. }
  7328. /*==========================================
  7329. * イベントタイマー追加
  7330. *------------------------------------------*/
  7331. BUILDIN_FUNC(addtimer)
  7332. {
  7333. int tick = script_getnum(st,2);
  7334. const char* event = script_getstr(st, 3);
  7335. check_event(st, event);
  7336. pc_addeventtimer(script_rid2sd(st),tick,event);
  7337. return 0;
  7338. }
  7339. /*==========================================
  7340. * イベントタイマー削除
  7341. *------------------------------------------*/
  7342. BUILDIN_FUNC(deltimer)
  7343. {
  7344. const char *event;
  7345. event=script_getstr(st, 2);
  7346. check_event(st, event);
  7347. pc_deleventtimer(script_rid2sd(st),event);
  7348. return 0;
  7349. }
  7350. /*==========================================
  7351. * イベントタイマーのカウント値追加
  7352. *------------------------------------------*/
  7353. BUILDIN_FUNC(addtimercount)
  7354. {
  7355. const char *event;
  7356. int tick;
  7357. event=script_getstr(st, 2);
  7358. tick=script_getnum(st,3);
  7359. check_event(st, event);
  7360. pc_addeventtimercount(script_rid2sd(st),event,tick);
  7361. return 0;
  7362. }
  7363. /*==========================================
  7364. * NPCタイマー初期化
  7365. *------------------------------------------*/
  7366. BUILDIN_FUNC(initnpctimer)
  7367. {
  7368. struct npc_data *nd;
  7369. int flag = 0;
  7370. if( script_hasdata(st,3) )
  7371. { //Two arguments: NPC name and attach flag.
  7372. nd = npc_name2id(script_getstr(st, 2));
  7373. flag = script_getnum(st,3);
  7374. } else
  7375. if( script_hasdata(st,2) )
  7376. { //Check if argument is numeric (flag) or string (npc name)
  7377. struct script_data *data;
  7378. data = script_getdata(st,2);
  7379. get_val(st,data);
  7380. if( data_isstring(data) ) //NPC name
  7381. nd = npc_name2id(script_getstr(st, 2));
  7382. else if( data_isint(data) ) { //Flag
  7383. nd = (struct npc_data *)map_id2bl(st->oid);
  7384. flag = script_getnum(st,3);
  7385. } else {
  7386. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7387. return 1;
  7388. }
  7389. } else
  7390. nd=(struct npc_data *)map_id2bl(st->oid);
  7391. if (!nd) return 0;
  7392. if (flag) { //Attach
  7393. TBL_PC* sd = script_rid2sd(st);
  7394. if (sd) nd->u.scr.rid = sd->bl.id;
  7395. }
  7396. npc_settimerevent_tick(nd,0);
  7397. npc_timerevent_start(nd, st->rid);
  7398. return 0;
  7399. }
  7400. /*==========================================
  7401. * NPCタイマー開始
  7402. *------------------------------------------*/
  7403. BUILDIN_FUNC(startnpctimer)
  7404. {
  7405. struct npc_data *nd;
  7406. int flag = 0;
  7407. if( script_hasdata(st,3) )
  7408. { //Two arguments: NPC name and attach flag.
  7409. nd = npc_name2id(script_getstr(st, 2));
  7410. flag = script_getnum(st,3);
  7411. } else
  7412. if( script_hasdata(st,2) )
  7413. { //Check if argument is numeric (flag) or string (npc name)
  7414. struct script_data *data;
  7415. data = script_getdata(st,2);
  7416. get_val(st,data);
  7417. if( data_isstring(data) ) //NPC name
  7418. nd = npc_name2id(script_getstr(st, 2));
  7419. else if( data_isint(data) ) { //Flag
  7420. nd = (struct npc_data *)map_id2bl(st->oid);
  7421. flag = script_getnum(st,3);
  7422. } else {
  7423. ShowError("startnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7424. return 1;
  7425. }
  7426. } else
  7427. nd=(struct npc_data *)map_id2bl(st->oid);
  7428. if (!nd) return 0;
  7429. if (flag) { //Attach
  7430. TBL_PC* sd = script_rid2sd(st);
  7431. if (sd) nd->u.scr.rid = sd->bl.id;
  7432. }
  7433. npc_timerevent_start(nd, st->rid);
  7434. return 0;
  7435. }
  7436. /*==========================================
  7437. * NPCタイマー停止
  7438. *------------------------------------------*/
  7439. BUILDIN_FUNC(stopnpctimer)
  7440. {
  7441. struct npc_data *nd;
  7442. int flag = 0;
  7443. if( script_hasdata(st,3) )
  7444. { //Two arguments: NPC name and attach flag.
  7445. nd = npc_name2id(script_getstr(st, 2));
  7446. flag = script_getnum(st,3);
  7447. } else
  7448. if( script_hasdata(st,2) )
  7449. { //Check if argument is numeric (flag) or string (npc name)
  7450. struct script_data *data;
  7451. data = script_getdata(st,2);
  7452. get_val(st,data);
  7453. if( data_isstring(data) ) //NPC name
  7454. nd = npc_name2id(script_getstr(st, 2));
  7455. else if( data_isint(data) ) { //Flag
  7456. nd = (struct npc_data *)map_id2bl(st->oid);
  7457. flag = script_getnum(st,3);
  7458. } else {
  7459. ShowError("stopnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7460. return 1;
  7461. }
  7462. } else
  7463. nd=(struct npc_data *)map_id2bl(st->oid);
  7464. if (!nd) return 0;
  7465. if (flag) //Detach
  7466. nd->u.scr.rid = 0;
  7467. npc_timerevent_stop(nd);
  7468. return 0;
  7469. }
  7470. /*==========================================
  7471. * NPCタイマー情報所得
  7472. *------------------------------------------*/
  7473. BUILDIN_FUNC(getnpctimer)
  7474. {
  7475. struct npc_data *nd;
  7476. TBL_PC *sd;
  7477. int type=script_getnum(st,2);
  7478. int val=0;
  7479. if( script_hasdata(st,3) )
  7480. nd = npc_name2id(script_getstr(st,3));
  7481. else
  7482. nd = (struct npc_data *)map_id2bl(st->oid);
  7483. if (!nd || nd->bl.type != BL_NPC)
  7484. {
  7485. script_pushint(st,0);
  7486. if (battle_config.error_log)
  7487. ShowError("getnpctimer: Invalid NPC\n");
  7488. return 1;
  7489. }
  7490. switch(type){
  7491. case 0: val=npc_gettimerevent_tick(nd); break;
  7492. case 1:
  7493. if (nd->u.scr.rid) {
  7494. sd = map_id2sd(nd->u.scr.rid);
  7495. if (!sd) {
  7496. if(battle_config.error_log)
  7497. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7498. break;
  7499. }
  7500. val = (sd->npc_timer_id != -1);
  7501. } else
  7502. val= (nd->u.scr.timerid !=-1);
  7503. break;
  7504. case 2: val= nd->u.scr.timeramount; break;
  7505. }
  7506. script_pushint(st,val);
  7507. return 0;
  7508. }
  7509. /*==========================================
  7510. * NPCタイマー値設定
  7511. *------------------------------------------*/
  7512. BUILDIN_FUNC(setnpctimer)
  7513. {
  7514. int tick;
  7515. struct npc_data *nd;
  7516. tick=script_getnum(st,2);
  7517. if( script_hasdata(st,3) )
  7518. nd=npc_name2id(script_getstr(st,3));
  7519. else
  7520. nd=(struct npc_data *)map_id2bl(st->oid);
  7521. npc_settimerevent_tick(nd,tick);
  7522. return 0;
  7523. }
  7524. /*==========================================
  7525. * attaches the player rid to the timer [Celest]
  7526. *------------------------------------------*/
  7527. BUILDIN_FUNC(attachnpctimer)
  7528. {
  7529. TBL_PC *sd;
  7530. struct npc_data *nd;
  7531. nd=(struct npc_data *)map_id2bl(st->oid);
  7532. if( script_hasdata(st,2) )
  7533. sd=map_nick2sd(script_getstr(st,2));
  7534. else
  7535. sd = script_rid2sd(st);
  7536. if (sd==NULL)
  7537. return 0;
  7538. nd->u.scr.rid = sd->bl.id;
  7539. return 0;
  7540. }
  7541. /*==========================================
  7542. * detaches a player rid from the timer [Celest]
  7543. *------------------------------------------*/
  7544. BUILDIN_FUNC(detachnpctimer)
  7545. {
  7546. struct npc_data *nd;
  7547. if( script_hasdata(st,2) )
  7548. nd=npc_name2id(script_getstr(st,2));
  7549. else
  7550. nd=(struct npc_data *)map_id2bl(st->oid);
  7551. nd->u.scr.rid = 0;
  7552. return 0;
  7553. }
  7554. /*==========================================
  7555. * To avoid "player not attached" script errors, this function is provided,
  7556. * it checks if there is a player attached to the current script. [Skotlex]
  7557. * If no, returns 0, if yes, returns the account_id of the attached player.
  7558. *------------------------------------------*/
  7559. BUILDIN_FUNC(playerattached)
  7560. {
  7561. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7562. script_pushint(st,0);
  7563. else
  7564. script_pushint(st,st->rid);
  7565. return 0;
  7566. }
  7567. /*==========================================
  7568. * 天の声アナウンス
  7569. *------------------------------------------*/
  7570. BUILDIN_FUNC(announce)
  7571. {
  7572. const char *str, *color=NULL;
  7573. int flag;
  7574. str=script_getstr(st,2);
  7575. flag=script_getnum(st,3);
  7576. if (script_hasdata(st,4))
  7577. color=script_getstr(st,4);
  7578. if(flag&0x0f){
  7579. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  7580. (struct block_list *)script_rid2sd(st);
  7581. if (color)
  7582. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  7583. else
  7584. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  7585. }else {
  7586. if (color)
  7587. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  7588. else
  7589. intif_GMmessage(str,(int)strlen(str)+1,flag);
  7590. }
  7591. return 0;
  7592. }
  7593. /*==========================================
  7594. * 天の声アナウンス(特定マップ)
  7595. *------------------------------------------*/
  7596. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  7597. {
  7598. char *str, *color;
  7599. int len,flag;
  7600. str=va_arg(ap,char *);
  7601. len=va_arg(ap,int);
  7602. flag=va_arg(ap,int);
  7603. color=va_arg(ap,char *);
  7604. if (color)
  7605. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7606. else
  7607. clif_GMmessage(bl,str,len,flag|3);
  7608. return 0;
  7609. }
  7610. BUILDIN_FUNC(mapannounce)
  7611. {
  7612. const char *mapname,*str, *color=NULL;
  7613. int flag,m;
  7614. mapname=script_getstr(st,2);
  7615. str=script_getstr(st,3);
  7616. flag=script_getnum(st,4);
  7617. if (script_hasdata(st,5))
  7618. color=script_getstr(st,5);
  7619. if( (m=map_mapname2mapid(mapname))<0 )
  7620. return 0;
  7621. map_foreachinmap(buildin_mapannounce_sub,
  7622. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7623. return 0;
  7624. }
  7625. /*==========================================
  7626. * 天の声アナウンス(特定エリア)
  7627. *------------------------------------------*/
  7628. BUILDIN_FUNC(areaannounce)
  7629. {
  7630. const char *map,*str,*color=NULL;
  7631. int flag,m;
  7632. int x0,y0,x1,y1;
  7633. map=script_getstr(st,2);
  7634. x0=script_getnum(st,3);
  7635. y0=script_getnum(st,4);
  7636. x1=script_getnum(st,5);
  7637. y1=script_getnum(st,6);
  7638. str=script_getstr(st,7);
  7639. flag=script_getnum(st,8);
  7640. if (script_hasdata(st,9))
  7641. color=script_getstr(st,9);
  7642. if( (m=map_mapname2mapid(map))<0 )
  7643. return 0;
  7644. map_foreachinarea(buildin_mapannounce_sub,
  7645. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7646. return 0;
  7647. }
  7648. /*==========================================
  7649. * ユーザー数所得
  7650. *------------------------------------------*/
  7651. BUILDIN_FUNC(getusers)
  7652. {
  7653. int flag=script_getnum(st,2);
  7654. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7655. int val=0;
  7656. switch(flag&0x07){
  7657. case 0: val=map[bl->m].users; break;
  7658. case 1: val=map_getusers(); break;
  7659. }
  7660. script_pushint(st,val);
  7661. return 0;
  7662. }
  7663. /*==========================================
  7664. * Works like @WHO - displays all online users names in window
  7665. *------------------------------------------*/
  7666. BUILDIN_FUNC(getusersname)
  7667. {
  7668. TBL_PC *sd, *pl_sd = NULL, **pl_allsd;
  7669. int i=0,disp_num=1, users;
  7670. sd = script_rid2sd(st);
  7671. if (!sd) return 0;
  7672. pl_allsd = map_getallusers(&users);
  7673. for (i=0;i<users;i++)
  7674. {
  7675. pl_sd = pl_allsd[i];
  7676. if( !(battle_config.hide_GM_session && pc_isGM(pl_sd)) )
  7677. {
  7678. if((disp_num++)%10==0)
  7679. clif_scriptnext(sd,st->oid);
  7680. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7681. }
  7682. }
  7683. return 0;
  7684. }
  7685. /*==========================================
  7686. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7687. *------------------------------------------*/
  7688. BUILDIN_FUNC(getmapguildusers)
  7689. {
  7690. const char *str;
  7691. int m, gid;
  7692. int i=0,c=0;
  7693. struct guild *g = NULL;
  7694. str=script_getstr(st,2);
  7695. gid=script_getnum(st,3);
  7696. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7697. script_pushint(st,-1);
  7698. return 0;
  7699. }
  7700. g = guild_search(gid);
  7701. if (g){
  7702. for(i = 0; i < g->max_member; i++)
  7703. {
  7704. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7705. c++;
  7706. }
  7707. }
  7708. script_pushint(st,c);
  7709. return 0;
  7710. }
  7711. /*==========================================
  7712. * マップ指定ユーザー数所得
  7713. *------------------------------------------*/
  7714. BUILDIN_FUNC(getmapusers)
  7715. {
  7716. const char *str;
  7717. int m;
  7718. str=script_getstr(st,2);
  7719. if( (m=map_mapname2mapid(str))< 0){
  7720. script_pushint(st,-1);
  7721. return 0;
  7722. }
  7723. script_pushint(st,map[m].users);
  7724. return 0;
  7725. }
  7726. /*==========================================
  7727. * エリア指定ユーザー数所得
  7728. *------------------------------------------*/
  7729. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7730. {
  7731. int *users=va_arg(ap,int *);
  7732. (*users)++;
  7733. return 0;
  7734. }
  7735. BUILDIN_FUNC(getareausers)
  7736. {
  7737. const char *str;
  7738. int m,x0,y0,x1,y1,users=0;
  7739. str=script_getstr(st,2);
  7740. x0=script_getnum(st,3);
  7741. y0=script_getnum(st,4);
  7742. x1=script_getnum(st,5);
  7743. y1=script_getnum(st,6);
  7744. if( (m=map_mapname2mapid(str))< 0){
  7745. script_pushint(st,-1);
  7746. return 0;
  7747. }
  7748. map_foreachinarea(buildin_getareausers_sub,
  7749. m,x0,y0,x1,y1,BL_PC,&users);
  7750. script_pushint(st,users);
  7751. return 0;
  7752. }
  7753. /*==========================================
  7754. * エリア指定ドロップアイテム数所得
  7755. *------------------------------------------*/
  7756. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7757. {
  7758. int item=va_arg(ap,int);
  7759. int *amount=va_arg(ap,int *);
  7760. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7761. if(drop->item_data.nameid==item)
  7762. (*amount)+=drop->item_data.amount;
  7763. return 0;
  7764. }
  7765. BUILDIN_FUNC(getareadropitem)
  7766. {
  7767. const char *str;
  7768. int m,x0,y0,x1,y1,item,amount=0;
  7769. struct script_data *data;
  7770. str=script_getstr(st,2);
  7771. x0=script_getnum(st,3);
  7772. y0=script_getnum(st,4);
  7773. x1=script_getnum(st,5);
  7774. y1=script_getnum(st,6);
  7775. data=script_getdata(st,7);
  7776. get_val(st,data);
  7777. if( data_isstring(data) ){
  7778. const char *name=conv_str(st,data);
  7779. struct item_data *item_data = itemdb_searchname(name);
  7780. item=UNKNOWN_ITEM_ID;
  7781. if( item_data )
  7782. item=item_data->nameid;
  7783. }else
  7784. item=conv_num(st,data);
  7785. if( (m=map_mapname2mapid(str))< 0){
  7786. script_pushint(st,-1);
  7787. return 0;
  7788. }
  7789. map_foreachinarea(buildin_getareadropitem_sub,
  7790. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7791. script_pushint(st,amount);
  7792. return 0;
  7793. }
  7794. /*==========================================
  7795. * NPCの有効化
  7796. *------------------------------------------*/
  7797. BUILDIN_FUNC(enablenpc)
  7798. {
  7799. const char *str;
  7800. str=script_getstr(st,2);
  7801. npc_enable(str,1);
  7802. return 0;
  7803. }
  7804. /*==========================================
  7805. * NPCの無効化
  7806. *------------------------------------------*/
  7807. BUILDIN_FUNC(disablenpc)
  7808. {
  7809. const char *str;
  7810. str=script_getstr(st,2);
  7811. npc_enable(str,0);
  7812. return 0;
  7813. }
  7814. /*==========================================
  7815. * 隠れているNPCの表示
  7816. *------------------------------------------*/
  7817. BUILDIN_FUNC(hideoffnpc)
  7818. {
  7819. const char *str;
  7820. str=script_getstr(st,2);
  7821. npc_enable(str,2);
  7822. return 0;
  7823. }
  7824. /*==========================================
  7825. * NPCをハイディング
  7826. *------------------------------------------*/
  7827. BUILDIN_FUNC(hideonnpc)
  7828. {
  7829. const char *str;
  7830. str=script_getstr(st,2);
  7831. npc_enable(str,4);
  7832. return 0;
  7833. }
  7834. /// Starts a status effect on the target unit or on the attached player.
  7835. ///
  7836. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7837. BUILDIN_FUNC(sc_start)
  7838. {
  7839. struct block_list* bl;
  7840. int type;
  7841. int tick;
  7842. int val1;
  7843. int val4 = 0;
  7844. type = script_getnum(st,2);
  7845. tick = script_getnum(st,3);
  7846. val1 = script_getnum(st,4);
  7847. if( script_hasdata(st,5) )
  7848. bl = map_id2bl(script_getnum(st,5));
  7849. else
  7850. bl = map_id2bl(st->rid);
  7851. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7852. {// When there isn't a duration specified, try to get it from the skill_db
  7853. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7854. }
  7855. if( potion_flag == 1 && potion_target )
  7856. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7857. bl = map_id2bl(potion_target);
  7858. tick /= 2;// Thrown potions only last half.
  7859. val4 = 1;// Mark that this was a thrown sc_effect
  7860. }
  7861. if( bl )
  7862. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 1|2|8);
  7863. return 0;
  7864. }
  7865. /// Starts a status effect on the target unit or on the attached player.
  7866. ///
  7867. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7868. BUILDIN_FUNC(sc_start2)
  7869. {
  7870. struct block_list* bl;
  7871. int type;
  7872. int tick;
  7873. int val1;
  7874. int val4 = 0;
  7875. int rate;
  7876. type = script_getnum(st,2);
  7877. tick = script_getnum(st,3);
  7878. val1 = script_getnum(st,4);
  7879. rate = script_getnum(st,5);
  7880. if( script_hasdata(st,6) )
  7881. bl = map_id2bl(script_getnum(st,6));
  7882. else
  7883. bl = map_id2bl(st->rid);
  7884. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7885. {// When there isn't a duration specified, try to get it from the skill_db
  7886. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7887. }
  7888. if( potion_flag == 1 && potion_target )
  7889. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7890. bl = map_id2bl(potion_target);
  7891. tick /= 2;// Thrown potions only last half.
  7892. val4 = 1;// Mark that this was a thrown sc_effect
  7893. }
  7894. if( bl )
  7895. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 1|2|8);
  7896. return 0;
  7897. }
  7898. /// Starts a status effect on the target unit or on the attached player.
  7899. ///
  7900. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7901. BUILDIN_FUNC(sc_start4)
  7902. {
  7903. struct block_list* bl;
  7904. int type;
  7905. int tick;
  7906. int val1;
  7907. int val2;
  7908. int val3;
  7909. int val4;
  7910. type = script_getnum(st,2);
  7911. tick = script_getnum(st,3);
  7912. val1 = script_getnum(st,4);
  7913. val2 = script_getnum(st,5);
  7914. val3 = script_getnum(st,6);
  7915. val4 = script_getnum(st,7);
  7916. if( script_hasdata(st,8) )
  7917. bl = map_id2bl(script_getnum(st,8));
  7918. else
  7919. bl = map_id2bl(st->rid);
  7920. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7921. {// When there isn't a duration specified, try to get it from the skill_db
  7922. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7923. }
  7924. if( potion_flag == 1 && potion_target )
  7925. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7926. bl = map_id2bl(potion_target);
  7927. tick /= 2;// Thrown potions only last half.
  7928. }
  7929. if( bl )
  7930. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 1|2|8);
  7931. return 0;
  7932. }
  7933. /// Ends one or all status effects on the target unit or on the attached player.
  7934. ///
  7935. /// sc_end <effect_id>{,<unit_id>};
  7936. BUILDIN_FUNC(sc_end)
  7937. {
  7938. struct block_list* bl;
  7939. int type;
  7940. type = script_getnum(st,2);
  7941. if( script_hasdata(st,3) )
  7942. bl = map_id2bl(script_getnum(st,3));
  7943. else
  7944. bl = map_id2bl(st->rid);
  7945. if( potion_flag==1 && potion_target )
  7946. {//##TODO how does this work [FlavioJS]
  7947. bl = map_id2bl(potion_target);
  7948. }
  7949. if( bl )
  7950. {
  7951. if( type >= 0 )
  7952. status_change_end(bl, type, INVALID_TIMER);
  7953. else
  7954. status_change_clear(bl, 2);// remove all effects
  7955. }
  7956. return 0;
  7957. }
  7958. /*==========================================
  7959. * 状態異常耐性を計算した確率を返す
  7960. *------------------------------------------*/
  7961. BUILDIN_FUNC(getscrate)
  7962. {
  7963. struct block_list *bl;
  7964. int type,rate;
  7965. type=script_getnum(st,2);
  7966. rate=script_getnum(st,3);
  7967. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7968. bl = map_id2bl(script_getnum(st,4));
  7969. else
  7970. bl = map_id2bl(st->rid);
  7971. if (bl)
  7972. rate = status_get_sc_def(bl,type, 10000, 10000, 0);
  7973. script_pushint(st,rate);
  7974. return 0;
  7975. }
  7976. /*==========================================
  7977. *
  7978. *------------------------------------------*/
  7979. BUILDIN_FUNC(debugmes)
  7980. {
  7981. const char *str;
  7982. str=script_getstr(st,2);
  7983. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7984. return 0;
  7985. }
  7986. /*==========================================
  7987. *捕獲アイテム使用
  7988. *------------------------------------------*/
  7989. BUILDIN_FUNC(catchpet)
  7990. {
  7991. int pet_id;
  7992. TBL_PC *sd;
  7993. pet_id= script_getnum(st,2);
  7994. sd=script_rid2sd(st);
  7995. pet_catch_process1(sd,pet_id);
  7996. return 0;
  7997. }
  7998. /*==========================================
  7999. * [orn]
  8000. *------------------------------------------*/
  8001. BUILDIN_FUNC(homunculus_evolution)
  8002. {
  8003. TBL_PC *sd;
  8004. sd=script_rid2sd(st);
  8005. if(merc_is_hom_active(sd->hd))
  8006. {
  8007. if (sd->hd->homunculus.intimacy > 91000)
  8008. merc_hom_evolution(sd->hd);
  8009. else
  8010. clif_emotion(&sd->hd->bl, 4) ; //swt
  8011. }
  8012. return 0;
  8013. }
  8014. // [Zephyrus]
  8015. BUILDIN_FUNC(homunculus_shuffle)
  8016. {
  8017. TBL_PC *sd;
  8018. sd=script_rid2sd(st);
  8019. if(merc_is_hom_active(sd->hd))
  8020. merc_hom_shuffle(sd->hd);
  8021. return 0;
  8022. }
  8023. //These two functions bring the eA MAPID_* class functionality to scripts.
  8024. BUILDIN_FUNC(eaclass)
  8025. {
  8026. int class_;
  8027. if( script_hasdata(st,2) )
  8028. class_ = script_getnum(st,2);
  8029. else {
  8030. TBL_PC *sd;
  8031. sd=script_rid2sd(st);
  8032. if (!sd) {
  8033. script_pushint(st,-1);
  8034. return 0;
  8035. }
  8036. class_ = sd->status.class_;
  8037. }
  8038. script_pushint(st,pc_jobid2mapid(class_));
  8039. return 0;
  8040. }
  8041. BUILDIN_FUNC(roclass)
  8042. {
  8043. int class_ =script_getnum(st,2);
  8044. int sex;
  8045. if( script_hasdata(st,3) )
  8046. sex = script_getnum(st,3);
  8047. else {
  8048. TBL_PC *sd;
  8049. if (st->rid && (sd=script_rid2sd(st)))
  8050. sex = sd->status.sex;
  8051. else
  8052. sex = 1; //Just use male when not found.
  8053. }
  8054. script_pushint(st,pc_mapid2jobid(class_, sex));
  8055. return 0;
  8056. }
  8057. /*==========================================
  8058. *携帯卵孵化機使用
  8059. *------------------------------------------*/
  8060. BUILDIN_FUNC(birthpet)
  8061. {
  8062. TBL_PC *sd;
  8063. sd=script_rid2sd(st);
  8064. clif_sendegg(sd);
  8065. return 0;
  8066. }
  8067. /*==========================================
  8068. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8069. *------------------------------------------*/
  8070. BUILDIN_FUNC(resetlvl)
  8071. {
  8072. TBL_PC *sd;
  8073. int type=script_getnum(st,2);
  8074. sd=script_rid2sd(st);
  8075. pc_resetlvl(sd,type);
  8076. return 0;
  8077. }
  8078. /*==========================================
  8079. * ステータスリセット
  8080. *------------------------------------------*/
  8081. BUILDIN_FUNC(resetstatus)
  8082. {
  8083. TBL_PC *sd;
  8084. sd=script_rid2sd(st);
  8085. pc_resetstate(sd);
  8086. return 0;
  8087. }
  8088. /*==========================================
  8089. * script command resetskill
  8090. *------------------------------------------*/
  8091. BUILDIN_FUNC(resetskill)
  8092. {
  8093. TBL_PC *sd;
  8094. sd=script_rid2sd(st);
  8095. pc_resetskill(sd,1);
  8096. return 0;
  8097. }
  8098. /*==========================================
  8099. * Counts total amount of skill points.
  8100. *------------------------------------------*/
  8101. BUILDIN_FUNC(skillpointcount)
  8102. {
  8103. TBL_PC *sd;
  8104. sd=script_rid2sd(st);
  8105. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8106. return 0;
  8107. }
  8108. /*==========================================
  8109. *
  8110. *------------------------------------------*/
  8111. BUILDIN_FUNC(changebase)
  8112. {
  8113. TBL_PC *sd=NULL;
  8114. int vclass;
  8115. if( script_hasdata(st,3) )
  8116. sd=map_id2sd(script_getnum(st,3));
  8117. else
  8118. sd=script_rid2sd(st);
  8119. if(sd == NULL)
  8120. return 0;
  8121. vclass = script_getnum(st,2);
  8122. if(vclass == JOB_WEDDING)
  8123. {
  8124. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8125. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8126. )
  8127. return 0;
  8128. }
  8129. if(!sd->disguise && vclass != sd->vd.class_) {
  8130. status_set_viewdata(&sd->bl, vclass);
  8131. //Updated client view. Base, Weapon and Cloth Colors.
  8132. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8133. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8134. if (sd->vd.cloth_color)
  8135. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8136. }
  8137. return 0;
  8138. }
  8139. /*==========================================
  8140. * 性別変換
  8141. *------------------------------------------*/
  8142. BUILDIN_FUNC(changesex)
  8143. {
  8144. TBL_PC *sd = NULL;
  8145. sd = script_rid2sd(st);
  8146. if (sd->status.sex == 0) {
  8147. sd->status.sex = 1;
  8148. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  8149. sd->status.class_ -= 1;
  8150. } else if (sd->status.sex == 1) {
  8151. sd->status.sex = 0;
  8152. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  8153. sd->status.class_ += 1;
  8154. }
  8155. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  8156. chrif_save(sd,0);
  8157. return 0;
  8158. }
  8159. /*==========================================
  8160. * Works like 'announce' but outputs in the common chat window
  8161. *------------------------------------------*/
  8162. BUILDIN_FUNC(globalmes)
  8163. {
  8164. struct block_list *bl = map_id2bl(st->oid);
  8165. struct npc_data *nd = (struct npc_data *)bl;
  8166. const char *name=NULL,*mes;
  8167. mes=script_getstr(st,2); // メッセージの取得
  8168. if(mes==NULL) return 0;
  8169. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8170. name=script_getstr(st,3);
  8171. } else {
  8172. name=nd->name;
  8173. }
  8174. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8175. return 0;
  8176. }
  8177. /////////////////////////////////////////////////////////////////////
  8178. // NPC waiting room (chat room)
  8179. //
  8180. /// Creates a waiting room (chat room) for this npc.
  8181. ///
  8182. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  8183. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  8184. /// waitingroom "<title>",<limit>,"<event>";
  8185. /// waitingroom "<title>",<limit>;
  8186. BUILDIN_FUNC(waitingroom)
  8187. {
  8188. struct npc_data* nd;
  8189. const char* title;
  8190. const char* ev = "";
  8191. int limit;
  8192. int trigger = 0;
  8193. int pub = 1;
  8194. title = script_getstr(st, 2);
  8195. limit = script_getnum(st, 3);
  8196. if( script_hasdata(st,5) )
  8197. {
  8198. struct script_data* last = script_getdata(st, 5);
  8199. get_val(st, last);
  8200. if( data_isstring(last) )
  8201. {// ,<trigger>,"<event>"
  8202. trigger = script_getnum(st, 4);
  8203. ev = script_getstr(st, 5);
  8204. }
  8205. else
  8206. {// ,"<event>",<trigger>
  8207. ev = script_getstr(st, 4);
  8208. trigger = script_getnum(st,5);
  8209. }
  8210. }
  8211. else if( script_hasdata(st,4) )
  8212. {// ,"<event>"
  8213. ev = script_getstr(st, 4);
  8214. trigger = limit;
  8215. }
  8216. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  8217. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  8218. return 0;
  8219. }
  8220. /// Removes the waiting room of the current or target npc.
  8221. ///
  8222. /// delwaitingroom "<npc_name>";
  8223. /// delwaitingroom;
  8224. BUILDIN_FUNC(delwaitingroom)
  8225. {
  8226. struct npc_data* nd;
  8227. if( script_hasdata(st,2) )
  8228. nd = npc_name2id(script_getstr(st, 2));
  8229. else
  8230. nd = (struct npc_data *)map_id2bl(st->oid);
  8231. if( nd != NULL )
  8232. chat_deletenpcchat(nd);
  8233. return 0;
  8234. }
  8235. /// Kicks all the players from the waiting room of the current or target npc.
  8236. ///
  8237. /// kickwaitingroomall "<npc_name>";
  8238. /// kickwaitingroomall;
  8239. BUILDIN_FUNC(waitingroomkickall)
  8240. {
  8241. struct npc_data* nd;
  8242. struct chat_data* cd;
  8243. if( script_hasdata(st,2) )
  8244. nd = npc_name2id(script_getstr(st,2));
  8245. else
  8246. nd = (struct npc_data *)map_id2bl(st->oid);
  8247. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8248. chat_npckickall(cd);
  8249. return 0;
  8250. }
  8251. /// Enables the waiting room event of the current or target npc.
  8252. ///
  8253. /// enablewaitingroomevent "<npc_name>";
  8254. /// enablewaitingroomevent;
  8255. BUILDIN_FUNC(enablewaitingroomevent)
  8256. {
  8257. struct npc_data* nd;
  8258. struct chat_data* cd;
  8259. if( script_hasdata(st,2) )
  8260. nd = npc_name2id(script_getstr(st, 2));
  8261. else
  8262. nd = (struct npc_data *)map_id2bl(st->oid);
  8263. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8264. chat_enableevent(cd);
  8265. return 0;
  8266. }
  8267. /// Disables the waiting room event of the current or target npc.
  8268. ///
  8269. /// disablewaitingroomevent "<npc_name>";
  8270. /// disablewaitingroomevent;
  8271. BUILDIN_FUNC(disablewaitingroomevent)
  8272. {
  8273. struct npc_data *nd;
  8274. struct chat_data *cd;
  8275. if( script_hasdata(st,2) )
  8276. nd = npc_name2id(script_getstr(st, 2));
  8277. else
  8278. nd = (struct npc_data *)map_id2bl(st->oid);
  8279. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8280. chat_disableevent(cd);
  8281. return 0;
  8282. }
  8283. /// Returns info on the waiting room of the current or target npc.
  8284. /// Returns -1 if the type unknown
  8285. /// <type>=0 : current number of users
  8286. /// <type>=1 : maximum number of users allowed
  8287. /// <type>=2 : the number of users that trigger the event
  8288. /// <type>=3 : if the trigger is disabled
  8289. /// <type>=4 : the title of the waiting room
  8290. /// <type>=5 : the password of the waiting room
  8291. /// <type>=16 : the name of the waiting room event
  8292. /// <type>=32 : if the waiting room is full
  8293. /// <type>=33 : if there are enough users to trigger the event
  8294. ///
  8295. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8296. /// getwaitingroomstate(<type>) -> <info>
  8297. BUILDIN_FUNC(getwaitingroomstate)
  8298. {
  8299. struct npc_data *nd;
  8300. struct chat_data *cd;
  8301. int type;
  8302. type = script_getnum(st,2);
  8303. if( script_hasdata(st,3) )
  8304. nd = npc_name2id(script_getstr(st, 3));
  8305. else
  8306. nd = (struct npc_data *)map_id2bl(st->oid);
  8307. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8308. {
  8309. script_pushint(st, -1);
  8310. return 0;
  8311. }
  8312. switch(type)
  8313. {
  8314. case 0: script_pushint(st, cd->users); break;
  8315. case 1: script_pushint(st, cd->limit); break;
  8316. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8317. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8318. case 4: script_pushconststr(st, cd->title); break;
  8319. case 5: script_pushconststr(st, cd->pass); break;
  8320. case 16: script_pushconststr(st, cd->npc_event);break;
  8321. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8322. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8323. default: script_pushint(st, -1); break;
  8324. }
  8325. return 0;
  8326. }
  8327. /// Warps the trigger or target amount of players to the target map and position.
  8328. /// Players are automatically removed from the waiting room.
  8329. /// Those waiting the longest will get warped first.
  8330. /// The target map can be "Random" for a random position in the current map,
  8331. /// and "SavePoint" for the savepoint map+position.
  8332. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8333. ///
  8334. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8335. /// The total number of teleported players is put into $@warpwaitingpcnum
  8336. ///
  8337. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8338. /// warpwaitingpc "<map name>",<x>,<y>;
  8339. BUILDIN_FUNC(warpwaitingpc)
  8340. {
  8341. int x;
  8342. int y;
  8343. int i;
  8344. int n;
  8345. const char* map_name;
  8346. struct npc_data* nd;
  8347. struct chat_data* cd;
  8348. TBL_PC* sd;
  8349. nd = (struct npc_data *)map_id2bl(st->oid);
  8350. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8351. return 0;
  8352. map_name = script_getstr(st,2);
  8353. x = script_getnum(st,3);
  8354. y = script_getnum(st,4);
  8355. n = cd->trigger&0x7f;
  8356. if( script_hasdata(st,5) )
  8357. n = script_getnum(st,5);
  8358. for( i = 0; i < n && cd->users > 0; i++ )
  8359. {
  8360. sd = cd->usersd[0];
  8361. if( sd == NULL )
  8362. {
  8363. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  8364. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  8365. continue;// Broken npc chat room?
  8366. }
  8367. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  8368. if( strcmp(map_name,"Random") == 0 )
  8369. pc_randomwarp(sd,3);
  8370. else if( strcmp(map_name,"SavePoint") == 0 )
  8371. {
  8372. if( map[sd->bl.m].flag.noteleport )
  8373. return 0;// can't teleport on this map
  8374. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  8375. }
  8376. else
  8377. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  8378. }
  8379. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8380. return 0;
  8381. }
  8382. /////////////////////////////////////////////////////////////////////
  8383. // ...
  8384. //
  8385. /*==========================================
  8386. * RIDのアタッチ
  8387. *------------------------------------------*/
  8388. BUILDIN_FUNC(attachrid)
  8389. {
  8390. st->rid=script_getnum(st,2);
  8391. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  8392. return 0;
  8393. }
  8394. /*==========================================
  8395. * RIDのデタッチ
  8396. *------------------------------------------*/
  8397. BUILDIN_FUNC(detachrid)
  8398. {
  8399. st->rid=0;
  8400. return 0;
  8401. }
  8402. /*==========================================
  8403. * 存在チェック
  8404. *------------------------------------------*/
  8405. BUILDIN_FUNC(isloggedin)
  8406. {
  8407. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8408. if (script_hasdata(st,3) && sd &&
  8409. sd->status.char_id != script_getnum(st,3))
  8410. sd = NULL;
  8411. push_val(st->stack,C_INT,sd!=NULL);
  8412. return 0;
  8413. }
  8414. /*==========================================
  8415. *
  8416. *------------------------------------------*/
  8417. BUILDIN_FUNC(setmapflagnosave)
  8418. {
  8419. int m,x,y;
  8420. unsigned short mapindex;
  8421. const char *str,*str2;
  8422. str=script_getstr(st,2);
  8423. str2=script_getstr(st,3);
  8424. x=script_getnum(st,4);
  8425. y=script_getnum(st,5);
  8426. m = map_mapname2mapid(str);
  8427. mapindex = mapindex_name2id(str2);
  8428. if(m >= 0 && mapindex) {
  8429. map[m].flag.nosave=1;
  8430. map[m].save.map=mapindex;
  8431. map[m].save.x=x;
  8432. map[m].save.y=y;
  8433. }
  8434. return 0;
  8435. }
  8436. BUILDIN_FUNC(setmapflag)
  8437. {
  8438. int m,i;
  8439. const char *str;
  8440. const char *val=NULL;
  8441. str=script_getstr(st,2);
  8442. i=script_getnum(st,3);
  8443. if(script_hasdata(st,4)){
  8444. val=script_getstr(st,4);
  8445. }
  8446. m = map_mapname2mapid(str);
  8447. if(m >= 0) {
  8448. switch(i) {
  8449. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  8450. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  8451. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  8452. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  8453. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  8454. case MF_PVP: map[m].flag.pvp=1; break;
  8455. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  8456. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  8457. case MF_GVG: map[m].flag.gvg=1; break;
  8458. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  8459. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  8460. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  8461. case MF_NOTRADE: map[m].flag.notrade=1; break;
  8462. case MF_NODROP: map[m].flag.nodrop=1; break;
  8463. case MF_NOSKILL: map[m].flag.noskill=1; break;
  8464. case MF_NOWARP: map[m].flag.nowarp=1; break;
  8465. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  8466. case MF_SNOW: map[m].flag.snow=1; break;
  8467. case MF_CLOUDS: map[m].flag.clouds=1; break;
  8468. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  8469. case MF_FOG: map[m].flag.fog=1; break;
  8470. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  8471. case MF_SAKURA: map[m].flag.sakura=1; break;
  8472. case MF_LEAVES: map[m].flag.leaves=1; break;
  8473. case MF_RAIN: map[m].flag.rain=1; break;
  8474. case MF_INDOORS: map[m].flag.indoors=1; break;
  8475. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  8476. case MF_NOGO: map[m].flag.nogo=1; break;
  8477. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  8478. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  8479. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  8480. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  8481. case MF_NORETURN: map[m].flag.noreturn=1; break;
  8482. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  8483. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  8484. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  8485. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8486. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8487. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8488. case MF_NOVENDING: map[m].flag.novending=1; break;
  8489. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  8490. case MF_NOCHAT: map[m].flag.nochat=1; break;
  8491. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  8492. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  8493. }
  8494. }
  8495. return 0;
  8496. }
  8497. BUILDIN_FUNC(removemapflag)
  8498. {
  8499. int m,i;
  8500. const char *str;
  8501. str=script_getstr(st,2);
  8502. i=script_getnum(st,3);
  8503. m = map_mapname2mapid(str);
  8504. if(m >= 0) {
  8505. switch(i) {
  8506. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  8507. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  8508. case MF_NOSAVE: map[m].flag.nosave=0; break;
  8509. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  8510. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  8511. case MF_PVP: map[m].flag.pvp=0; break;
  8512. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  8513. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  8514. case MF_GVG: map[m].flag.gvg=0; break;
  8515. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8516. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8517. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8518. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8519. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8520. case MF_NODROP: map[m].flag.nodrop=0; break;
  8521. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8522. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8523. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8524. case MF_SNOW: map[m].flag.snow=0; break;
  8525. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8526. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8527. case MF_FOG: map[m].flag.fog=0; break;
  8528. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8529. case MF_SAKURA: map[m].flag.sakura=0; break;
  8530. case MF_LEAVES: map[m].flag.leaves=0; break;
  8531. case MF_RAIN: map[m].flag.rain=0; break;
  8532. case MF_INDOORS: map[m].flag.indoors=0; break;
  8533. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8534. case MF_NOGO: map[m].flag.nogo=0; break;
  8535. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8536. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8537. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8538. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8539. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8540. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8541. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8542. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8543. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8544. case MF_JEXP: map[m].jexp=100; break;
  8545. case MF_BEXP: map[m].bexp=100; break;
  8546. case MF_NOVENDING: map[m].flag.novending=0; break;
  8547. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8548. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8549. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8550. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8551. }
  8552. }
  8553. return 0;
  8554. }
  8555. BUILDIN_FUNC(pvpon)
  8556. {
  8557. int m,i,users;
  8558. const char *str;
  8559. TBL_PC *pl_sd=NULL, **pl_allsd;
  8560. str=script_getstr(st,2);
  8561. m = map_mapname2mapid(str);
  8562. if(m >= 0 && !map[m].flag.pvp) {
  8563. map[m].flag.pvp = 1;
  8564. clif_send0199(m,1);
  8565. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8566. return 0;
  8567. pl_allsd = map_getallusers(&users);
  8568. for(i=0;i<users;i++)
  8569. {
  8570. if ((pl_sd = pl_allsd[i]) && m == pl_sd->bl.m && pl_sd->pvp_timer == -1)
  8571. {
  8572. pl_sd->pvp_timer=add_timer(gettick()+200,pc_calc_pvprank_timer,pl_sd->bl.id,0);
  8573. pl_sd->pvp_rank=0;
  8574. pl_sd->pvp_lastusers=0;
  8575. pl_sd->pvp_point=5;
  8576. pl_sd->pvp_won = 0;
  8577. pl_sd->pvp_lost = 0;
  8578. }
  8579. }
  8580. }
  8581. return 0;
  8582. }
  8583. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8584. {
  8585. TBL_PC* sd = (TBL_PC*)bl;
  8586. clif_pvpset(sd, 0, 0, 2);
  8587. if (sd->pvp_timer != -1) {
  8588. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8589. sd->pvp_timer = -1;
  8590. }
  8591. return 0;
  8592. }
  8593. BUILDIN_FUNC(pvpoff)
  8594. {
  8595. int m;
  8596. const char *str;
  8597. str=script_getstr(st,2);
  8598. m = map_mapname2mapid(str);
  8599. if(m < 0 || !map[m].flag.pvp)
  8600. return 0; //fixed Lupus
  8601. map[m].flag.pvp = 0;
  8602. clif_send0199(m,0);
  8603. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8604. return 0;
  8605. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8606. return 0;
  8607. }
  8608. BUILDIN_FUNC(gvgon)
  8609. {
  8610. int m;
  8611. const char *str;
  8612. str=script_getstr(st,2);
  8613. m = map_mapname2mapid(str);
  8614. if(m >= 0 && !map[m].flag.gvg) {
  8615. map[m].flag.gvg = 1;
  8616. clif_send0199(m,3);
  8617. }
  8618. return 0;
  8619. }
  8620. BUILDIN_FUNC(gvgoff)
  8621. {
  8622. int m;
  8623. const char *str;
  8624. str=script_getstr(st,2);
  8625. m = map_mapname2mapid(str);
  8626. if(m >= 0 && map[m].flag.gvg) {
  8627. map[m].flag.gvg = 0;
  8628. clif_send0199(m,0);
  8629. }
  8630. return 0;
  8631. }
  8632. /*==========================================
  8633. * Shows an emoticon on top of the player/npc
  8634. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8635. *------------------------------------------*/
  8636. //Optional second parameter added by [Skotlex]
  8637. BUILDIN_FUNC(emotion)
  8638. {
  8639. int type;
  8640. int player=0;
  8641. type=script_getnum(st,2);
  8642. if(type < 0 || type > 100)
  8643. return 0;
  8644. if( script_hasdata(st,3) )
  8645. player=script_getnum(st,3);
  8646. if (player) {
  8647. TBL_PC *sd = script_rid2sd(st);
  8648. if (sd)
  8649. clif_emotion(&sd->bl,type);
  8650. } else
  8651. clif_emotion(map_id2bl(st->oid),type);
  8652. return 0;
  8653. }
  8654. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  8655. {
  8656. int m=va_arg(ap,int);
  8657. int g_id=va_arg(ap,int);
  8658. int flag=va_arg(ap,int);
  8659. TBL_PC *sd = (TBL_PC*)data;
  8660. if(!sd || sd->bl.m != m)
  8661. return 0;
  8662. if(
  8663. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8664. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8665. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8666. )
  8667. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8668. return 1;
  8669. }
  8670. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8671. {
  8672. struct mob_data *md=(struct mob_data *)bl;
  8673. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8674. status_kill(bl);
  8675. return 0;
  8676. }
  8677. BUILDIN_FUNC(maprespawnguildid)
  8678. {
  8679. const char *mapname=script_getstr(st,2);
  8680. int g_id=script_getnum(st,3);
  8681. int flag=script_getnum(st,4);
  8682. int m=map_mapname2mapid(mapname);
  8683. if(m == -1)
  8684. return 0;
  8685. //Catch ALL players (in case some are 'between maps' on execution time)
  8686. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8687. if (flag&4) //Remove script mobs.
  8688. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8689. return 0;
  8690. }
  8691. BUILDIN_FUNC(agitstart)
  8692. {
  8693. if(agit_flag==1) return 0; // Agit already Start.
  8694. agit_flag=1;
  8695. guild_agit_start();
  8696. return 0;
  8697. }
  8698. BUILDIN_FUNC(agitend)
  8699. {
  8700. if(agit_flag==0) return 0; // Agit already End.
  8701. agit_flag=0;
  8702. guild_agit_end();
  8703. return 0;
  8704. }
  8705. /*==========================================
  8706. * Returns whether woe is on or off. // choice script
  8707. *------------------------------------------*/
  8708. BUILDIN_FUNC(agitcheck)
  8709. {
  8710. script_pushint(st,agit_flag);
  8711. return 0;
  8712. }
  8713. BUILDIN_FUNC(flagemblem)
  8714. {
  8715. int g_id=script_getnum(st,2);
  8716. if(g_id < 0) return 0;
  8717. // printf("Script.c: [FlagEmblem] GuildID=%d, Emblem=%d.\n", g->guild_id, g->emblem_id);
  8718. ((struct npc_data *)map_id2bl(st->oid))->u.scr.guild_id = g_id;
  8719. return 0;
  8720. }
  8721. BUILDIN_FUNC(getcastlename)
  8722. {
  8723. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8724. struct guild_castle* gc = guild_mapname2gc(mapname);
  8725. char* name = (gc) ? gc->castle_name : "";
  8726. script_pushconststr(st,name);
  8727. return 0;
  8728. }
  8729. BUILDIN_FUNC(getcastledata)
  8730. {
  8731. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8732. int index = script_getnum(st,3);
  8733. struct guild_castle* gc = guild_mapname2gc(mapname);
  8734. if(script_hasdata(st,4) && index==0 && gc) {
  8735. const char* event = script_getstr(st,4);
  8736. check_event(st, event);
  8737. guild_addcastleinfoevent(gc->castle_id,17,event);
  8738. }
  8739. if(gc){
  8740. switch(index){
  8741. case 0: {
  8742. int i;
  8743. for(i=1;i<26;i++) // Initialize[AgitInit]
  8744. guild_castledataload(gc->castle_id,i);
  8745. } break;
  8746. case 1:
  8747. script_pushint(st,gc->guild_id); break;
  8748. case 2:
  8749. script_pushint(st,gc->economy); break;
  8750. case 3:
  8751. script_pushint(st,gc->defense); break;
  8752. case 4:
  8753. script_pushint(st,gc->triggerE); break;
  8754. case 5:
  8755. script_pushint(st,gc->triggerD); break;
  8756. case 6:
  8757. script_pushint(st,gc->nextTime); break;
  8758. case 7:
  8759. script_pushint(st,gc->payTime); break;
  8760. case 8:
  8761. script_pushint(st,gc->createTime); break;
  8762. case 9:
  8763. script_pushint(st,gc->visibleC); break;
  8764. case 10:
  8765. case 11:
  8766. case 12:
  8767. case 13:
  8768. case 14:
  8769. case 15:
  8770. case 16:
  8771. case 17:
  8772. script_pushint(st,gc->guardian[index-10].visible); break;
  8773. case 18:
  8774. case 19:
  8775. case 20:
  8776. case 21:
  8777. case 22:
  8778. case 23:
  8779. case 24:
  8780. case 25:
  8781. script_pushint(st,gc->guardian[index-18].hp); break;
  8782. default:
  8783. script_pushint(st,0); break;
  8784. }
  8785. return 0;
  8786. }
  8787. script_pushint(st,0);
  8788. return 0;
  8789. }
  8790. BUILDIN_FUNC(setcastledata)
  8791. {
  8792. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8793. int index = script_getnum(st,3);
  8794. int value = script_getnum(st,4);
  8795. struct guild_castle* gc = guild_mapname2gc(mapname);
  8796. if(gc) {
  8797. // Save Data byself First
  8798. switch(index){
  8799. case 1:
  8800. gc->guild_id = value; break;
  8801. case 2:
  8802. gc->economy = value; break;
  8803. case 3:
  8804. gc->defense = value; break;
  8805. case 4:
  8806. gc->triggerE = value; break;
  8807. case 5:
  8808. gc->triggerD = value; break;
  8809. case 6:
  8810. gc->nextTime = value; break;
  8811. case 7:
  8812. gc->payTime = value; break;
  8813. case 8:
  8814. gc->createTime = value; break;
  8815. case 9:
  8816. gc->visibleC = value; break;
  8817. case 10:
  8818. case 11:
  8819. case 12:
  8820. case 13:
  8821. case 14:
  8822. case 15:
  8823. case 16:
  8824. case 17:
  8825. gc->guardian[index-10].visible = value; break;
  8826. case 18:
  8827. case 19:
  8828. case 20:
  8829. case 21:
  8830. case 22:
  8831. case 23:
  8832. case 24:
  8833. case 25:
  8834. gc->guardian[index-18].hp = value;
  8835. if (gc->guardian[index-18].id)
  8836. { //Update this mob's HP.
  8837. struct block_list *bl = map_id2bl(gc->guardian[index-18].id);
  8838. if (!bl)
  8839. { //Wrong target?
  8840. gc->guardian[index-18].id = 0;
  8841. break;
  8842. }
  8843. if (value < 1) {
  8844. status_kill(bl);
  8845. break;
  8846. }
  8847. status_set_hp(bl, value, 0);
  8848. }
  8849. break;
  8850. default:
  8851. return 0;
  8852. }
  8853. guild_castledatasave(gc->castle_id,index,value);
  8854. }
  8855. return 0;
  8856. }
  8857. /* =====================================================================
  8858. * ギルド情報を要求する
  8859. * ---------------------------------------------------------------------*/
  8860. BUILDIN_FUNC(requestguildinfo)
  8861. {
  8862. int guild_id=script_getnum(st,2);
  8863. const char *event=NULL;
  8864. if( script_hasdata(st,3) ){
  8865. event=script_getstr(st,3);
  8866. check_event(st, event);
  8867. }
  8868. if(guild_id>0)
  8869. guild_npc_request_info(guild_id,event);
  8870. return 0;
  8871. }
  8872. /* =====================================================================
  8873. * カードの数を得る
  8874. * ---------------------------------------------------------------------*/
  8875. BUILDIN_FUNC(getequipcardcnt)
  8876. {
  8877. int i=-1,num;
  8878. TBL_PC *sd;
  8879. int c=MAX_SLOTS;
  8880. num=script_getnum(st,2);
  8881. sd=script_rid2sd(st);
  8882. if (num > 0 && num <= ARRAYLENGTH(equip))
  8883. i=pc_checkequip(sd,equip[num-1]);
  8884. if (i < 0) {
  8885. script_pushint(st,0);
  8886. return 0;
  8887. }
  8888. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8889. {
  8890. script_pushint(st,0);
  8891. return 0;
  8892. }
  8893. do{
  8894. if(sd->status.inventory[i].card[c-1] &&
  8895. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8896. script_pushint(st,(c));
  8897. return 0;
  8898. }
  8899. }while(c--);
  8900. script_pushint(st,0);
  8901. return 0;
  8902. }
  8903. /* ================================================================
  8904. * カード取り外し成功
  8905. * ----------------------------------------------------------------*/
  8906. BUILDIN_FUNC(successremovecards)
  8907. {
  8908. int i=-1,j,num,cardflag=0,flag;
  8909. TBL_PC *sd;
  8910. struct item item_tmp;
  8911. int c=MAX_SLOTS;
  8912. num=script_getnum(st,2);
  8913. sd=script_rid2sd(st);
  8914. if (num > 0 && num <= ARRAYLENGTH(equip))
  8915. i=pc_checkequip(sd,equip[num-1]);
  8916. if (i < 0) {
  8917. script_pushint(st,0);
  8918. return 0;
  8919. }
  8920. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8921. return 0;
  8922. do{
  8923. if(sd->status.inventory[i].card[c-1] &&
  8924. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8925. cardflag = 1;
  8926. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8927. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8928. item_tmp.attribute=0;
  8929. for (j = 0; j < MAX_SLOTS; j++)
  8930. item_tmp.card[j]=0;
  8931. //Logs items, got from (N)PC scripts [Lupus]
  8932. if(log_config.enable_logs&0x40)
  8933. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8934. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8935. clif_additem(sd,0,0,flag);
  8936. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8937. }
  8938. }
  8939. }while(c--);
  8940. if(cardflag == 1){ // カードを取り除いたアイテム所得
  8941. flag=0;
  8942. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8943. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8944. item_tmp.attribute=sd->status.inventory[i].attribute;
  8945. //Logs items, got from (N)PC scripts [Lupus]
  8946. if(log_config.enable_logs&0x40)
  8947. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8948. for (j = 0; j < MAX_SLOTS; j++)
  8949. item_tmp.card[j]=0;
  8950. pc_delitem(sd,i,1,0);
  8951. //Logs items, got from (N)PC scripts [Lupus]
  8952. if(log_config.enable_logs&0x40)
  8953. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8954. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8955. clif_additem(sd,0,0,flag);
  8956. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8957. }
  8958. clif_misceffect(&sd->bl,3);
  8959. return 0;
  8960. }
  8961. return 0;
  8962. }
  8963. /* ================================================================
  8964. * カード取り外し失敗 slot,type
  8965. * type=0: 両方損失、1:カード損失、2:武具損失、3:損失無し
  8966. * ----------------------------------------------------------------*/
  8967. BUILDIN_FUNC(failedremovecards)
  8968. {
  8969. int i=-1,j,num,cardflag=0,flag,typefail;
  8970. TBL_PC *sd;
  8971. struct item item_tmp;
  8972. int c=MAX_SLOTS;
  8973. num=script_getnum(st,2);
  8974. typefail=script_getnum(st,3);
  8975. sd=script_rid2sd(st);
  8976. if (num > 0 && num <= ARRAYLENGTH(equip))
  8977. i=pc_checkequip(sd,equip[num-1]);
  8978. if (i < 0) {
  8979. script_pushint(st,0);
  8980. return 0;
  8981. }
  8982. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8983. return 0;
  8984. do{
  8985. if(sd->status.inventory[i].card[c-1] &&
  8986. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8987. cardflag = 1;
  8988. if(typefail == 2){ // 武具のみ損失なら、カードは受け取らせる
  8989. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8990. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8991. item_tmp.attribute=0;
  8992. for (j = 0; j < MAX_SLOTS; j++)
  8993. item_tmp.card[j]=0;
  8994. //Logs items, got from (N)PC scripts [Lupus]
  8995. if(log_config.enable_logs&0x40)
  8996. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8997. if((flag=pc_additem(sd,&item_tmp,1))){
  8998. clif_additem(sd,0,0,flag);
  8999. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9000. }
  9001. }
  9002. }
  9003. }while(c--);
  9004. if(cardflag == 1){
  9005. if(typefail == 0 || typefail == 2){ // 武具損失
  9006. //Logs items, got from (N)PC scripts [Lupus]
  9007. if(log_config.enable_logs&0x40)
  9008. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9009. pc_delitem(sd,i,1,0);
  9010. clif_misceffect(&sd->bl,2);
  9011. return 0;
  9012. }
  9013. if(typefail == 1){ // カードのみ損失(武具を返す)
  9014. flag=0;
  9015. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9016. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9017. item_tmp.attribute=sd->status.inventory[i].attribute;
  9018. //Logs items, got from (N)PC scripts [Lupus]
  9019. if(log_config.enable_logs&0x40)
  9020. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9021. for (j = 0; j < MAX_SLOTS; j++)
  9022. item_tmp.card[j]=0;
  9023. pc_delitem(sd,i,1,0);
  9024. //Logs items, got from (N)PC scripts [Lupus]
  9025. if(log_config.enable_logs&0x40)
  9026. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  9027. if((flag=pc_additem(sd,&item_tmp,1))){
  9028. clif_additem(sd,0,0,flag);
  9029. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9030. }
  9031. }
  9032. clif_misceffect(&sd->bl,2);
  9033. return 0;
  9034. }
  9035. return 0;
  9036. }
  9037. /* ================================================================
  9038. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  9039. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  9040. * improved by [Lance]
  9041. * ================================================================*/
  9042. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  9043. {
  9044. int x,y,m,check_val=0,check_ID=0,i=0;
  9045. struct guild *g = NULL;
  9046. struct party_data *p = NULL;
  9047. const char *str;
  9048. const char *mapname;
  9049. unsigned int index;
  9050. mapname=script_getstr(st,2);
  9051. str=script_getstr(st,3);
  9052. x=script_getnum(st,4);
  9053. y=script_getnum(st,5);
  9054. if(script_hasdata(st,7)){
  9055. check_val=script_getnum(st,6);
  9056. check_ID=script_getnum(st,7);
  9057. }
  9058. if((m=map_mapname2mapid(mapname))< 0)
  9059. return 0;
  9060. if(!(index=mapindex_name2id(str)))
  9061. return 0;
  9062. switch(check_val){
  9063. case 1:
  9064. g = guild_search(check_ID);
  9065. if (g){
  9066. for( i=0; i < g->max_member; i++)
  9067. {
  9068. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9069. pc_setpos(g->member[i].sd,index,x,y,3);
  9070. }
  9071. }
  9072. }
  9073. break;
  9074. case 2:
  9075. p = party_search(check_ID);
  9076. if(p){
  9077. for(i=0;i<MAX_PARTY; i++){
  9078. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9079. pc_setpos(p->data[i].sd,index,x,y,3);
  9080. }
  9081. }
  9082. }
  9083. break;
  9084. default:
  9085. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  9086. break;
  9087. }
  9088. return 0;
  9089. }
  9090. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9091. {
  9092. char *event=va_arg(ap,char *);
  9093. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  9094. return 1;
  9095. return 0;
  9096. }
  9097. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9098. {
  9099. const char *mapname,*event;
  9100. int m;
  9101. mapname=script_getstr(st,2);
  9102. event=script_getstr(st,3);
  9103. check_event(st, event);
  9104. if( (m=map_mapname2mapid(mapname))<0 ) {
  9105. script_pushint(st,-1);
  9106. return 0;
  9107. }
  9108. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9109. return 0;
  9110. }
  9111. BUILDIN_FUNC(marriage)
  9112. {
  9113. const char *partner=script_getstr(st,2);
  9114. TBL_PC *sd=script_rid2sd(st);
  9115. TBL_PC *p_sd=map_nick2sd(partner);
  9116. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9117. script_pushint(st,0);
  9118. return 0;
  9119. }
  9120. script_pushint(st,1);
  9121. return 0;
  9122. }
  9123. BUILDIN_FUNC(wedding_effect)
  9124. {
  9125. TBL_PC *sd=script_rid2sd(st);
  9126. struct block_list *bl;
  9127. if(sd==NULL) {
  9128. bl=map_id2bl(st->oid);
  9129. } else
  9130. bl=&sd->bl;
  9131. clif_wedding_effect(bl);
  9132. return 0;
  9133. }
  9134. BUILDIN_FUNC(divorce)
  9135. {
  9136. TBL_PC *sd=script_rid2sd(st);
  9137. if(sd==NULL || pc_divorce(sd) < 0){
  9138. script_pushint(st,0);
  9139. return 0;
  9140. }
  9141. script_pushint(st,1);
  9142. return 0;
  9143. }
  9144. BUILDIN_FUNC(ispartneron)
  9145. {
  9146. TBL_PC *sd=script_rid2sd(st);
  9147. TBL_PC *p_sd=NULL;
  9148. if(sd==NULL || !pc_ismarried(sd) ||
  9149. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9150. script_pushint(st,0);
  9151. return 0;
  9152. }
  9153. script_pushint(st,1);
  9154. return 0;
  9155. }
  9156. BUILDIN_FUNC(getpartnerid)
  9157. {
  9158. TBL_PC *sd=script_rid2sd(st);
  9159. if (sd == NULL) {
  9160. script_pushint(st,0);
  9161. return 0;
  9162. }
  9163. script_pushint(st,sd->status.partner_id);
  9164. return 0;
  9165. }
  9166. BUILDIN_FUNC(getchildid)
  9167. {
  9168. TBL_PC *sd=script_rid2sd(st);
  9169. if (sd == NULL) {
  9170. script_pushint(st,0);
  9171. return 0;
  9172. }
  9173. script_pushint(st,sd->status.child);
  9174. return 0;
  9175. }
  9176. BUILDIN_FUNC(getmotherid)
  9177. {
  9178. TBL_PC *sd=script_rid2sd(st);
  9179. if (sd == NULL) {
  9180. script_pushint(st,0);
  9181. return 0;
  9182. }
  9183. script_pushint(st,sd->status.mother);
  9184. return 0;
  9185. }
  9186. BUILDIN_FUNC(getfatherid)
  9187. {
  9188. TBL_PC *sd=script_rid2sd(st);
  9189. if (sd == NULL) {
  9190. script_pushint(st,0);
  9191. return 0;
  9192. }
  9193. script_pushint(st,sd->status.father);
  9194. return 0;
  9195. }
  9196. BUILDIN_FUNC(warppartner)
  9197. {
  9198. int x,y;
  9199. unsigned short mapindex;
  9200. const char *str;
  9201. TBL_PC *sd=script_rid2sd(st);
  9202. TBL_PC *p_sd=NULL;
  9203. if(sd==NULL || !pc_ismarried(sd) ||
  9204. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9205. script_pushint(st,0);
  9206. return 0;
  9207. }
  9208. str=script_getstr(st,2);
  9209. x=script_getnum(st,3);
  9210. y=script_getnum(st,4);
  9211. mapindex = mapindex_name2id(str);
  9212. if (mapindex) {
  9213. pc_setpos(p_sd,mapindex,x,y,0);
  9214. script_pushint(st,1);
  9215. } else
  9216. script_pushint(st,0);
  9217. return 0;
  9218. }
  9219. /*================================================
  9220. * Script for Displaying MOB Information [Valaris]
  9221. *------------------------------------------------*/
  9222. BUILDIN_FUNC(strmobinfo)
  9223. {
  9224. int num=script_getnum(st,2);
  9225. int class_=script_getnum(st,3);
  9226. if(!mobdb_checkid(class_))
  9227. {
  9228. script_pushint(st,0);
  9229. return 0;
  9230. }
  9231. switch (num) {
  9232. case 1: script_pushconststr(st,mob_db(class_)->name); break;
  9233. case 2: script_pushconststr(st,mob_db(class_)->jname); break;
  9234. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9235. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9236. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9237. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9238. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9239. default:
  9240. script_pushint(st,0);
  9241. break;
  9242. }
  9243. return 0;
  9244. }
  9245. /*==========================================
  9246. * Summon guardians [Valaris]
  9247. * guardian "<map name>",<x>,<y>,"<name to show>",<mob id>,{,"<event label>"}{,<guardian index>};
  9248. *------------------------------------------*/
  9249. BUILDIN_FUNC(guardian)
  9250. {
  9251. int class_=0,x=0,y=0,guardian=0;
  9252. const char *str,*map,*evt="";
  9253. struct script_data *data;
  9254. map =script_getstr(st,2);
  9255. x =script_getnum(st,3);
  9256. y =script_getnum(st,4);
  9257. str =script_getstr(st,5);
  9258. class_=script_getnum(st,6);
  9259. if( script_hasdata(st,8) )
  9260. {// "<event label>",<guardian index>
  9261. evt=script_getstr(st,7);
  9262. guardian=script_getnum(st,8);
  9263. } else if( script_hasdata(st,7) ){
  9264. data=script_getdata(st,7);
  9265. get_val(st,data);
  9266. if( data_isstring(data) )
  9267. {// "<event label>"
  9268. evt=script_getstr(st,7);
  9269. } else if( data_isint(data) )
  9270. {// <guardian index>
  9271. guardian=script_getnum(st,7);
  9272. } else {
  9273. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9274. script_reportdata(data);
  9275. return 1;
  9276. }
  9277. }
  9278. check_event(st, evt);
  9279. mob_spawn_guardian(map,x,y,str,class_,evt,guardian);
  9280. return 0;
  9281. }
  9282. /*================================================
  9283. * Script for Displaying Guardian Info [Valaris]
  9284. *------------------------------------------------*/
  9285. BUILDIN_FUNC(guardianinfo)
  9286. {
  9287. int guardian=script_getnum(st,2);
  9288. TBL_PC *sd=script_rid2sd(st);
  9289. struct guild_castle *gc=guild_mapname2gc(map[sd->bl.m].name);
  9290. if (guardian < 0 || guardian >= MAX_GUARDIANS || gc==NULL)
  9291. {
  9292. script_pushint(st,-1);
  9293. return 0;
  9294. }
  9295. if(gc->guardian[guardian].visible)
  9296. script_pushint(st,gc->guardian[guardian].hp);
  9297. else script_pushint(st,-1);
  9298. return 0;
  9299. }
  9300. /*==========================================
  9301. * IDからItem名
  9302. *------------------------------------------*/
  9303. BUILDIN_FUNC(getitemname)
  9304. {
  9305. int item_id=0;
  9306. struct item_data *i_data;
  9307. char *item_name;
  9308. struct script_data *data;
  9309. data=script_getdata(st,2);
  9310. get_val(st,data);
  9311. if( data_isstring(data) ){
  9312. const char *name=conv_str(st,data);
  9313. struct item_data *item_data = itemdb_searchname(name);
  9314. if( item_data )
  9315. item_id=item_data->nameid;
  9316. }else
  9317. item_id=conv_num(st,data);
  9318. i_data = itemdb_exists(item_id);
  9319. if (i_data == NULL)
  9320. {
  9321. script_pushconststr(st,"null");
  9322. return 0;
  9323. }
  9324. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9325. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9326. script_pushstr(st,item_name);
  9327. return 0;
  9328. }
  9329. /*==========================================
  9330. * Returns number of slots an item has. [Skotlex]
  9331. *------------------------------------------*/
  9332. BUILDIN_FUNC(getitemslots)
  9333. {
  9334. int item_id;
  9335. struct item_data *i_data;
  9336. item_id=script_getnum(st,2);
  9337. i_data = itemdb_exists(item_id);
  9338. if (i_data)
  9339. script_pushint(st,i_data->slot);
  9340. else
  9341. script_pushint(st,-1);
  9342. return 0;
  9343. }
  9344. /*==========================================
  9345. * Returns some values of an item [Lupus]
  9346. * Price, Weight, etc...
  9347. getiteminfo(itemID,n), where n
  9348. 0 value_buy;
  9349. 1 value_sell;
  9350. 2 type;
  9351. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9352. if = 0, then monsters don't drop it at all (rare or a quest item)
  9353. if = 10000, then this item is sold in NPC shops only
  9354. 4 sex;
  9355. 5 equip;
  9356. 6 weight;
  9357. 7 atk;
  9358. 8 def;
  9359. 9 range;
  9360. 10 slot;
  9361. 11 look;
  9362. 12 elv;
  9363. 13 wlv;
  9364. 14 view id
  9365. *------------------------------------------*/
  9366. BUILDIN_FUNC(getiteminfo)
  9367. {
  9368. int item_id,n;
  9369. int *item_arr;
  9370. struct item_data *i_data;
  9371. item_id = script_getnum(st,2);
  9372. n = script_getnum(st,3);
  9373. i_data = itemdb_exists(item_id);
  9374. if (i_data && n>=0 && n<14) {
  9375. item_arr = (int*)&i_data->value_buy;
  9376. script_pushint(st,item_arr[n]);
  9377. } else
  9378. script_pushint(st,-1);
  9379. return 0;
  9380. }
  9381. /*==========================================
  9382. * Set some values of an item [Lupus]
  9383. * Price, Weight, etc...
  9384. setiteminfo(itemID,n,Value), where n
  9385. 0 value_buy;
  9386. 1 value_sell;
  9387. 2 type;
  9388. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9389. if = 0, then monsters don't drop it at all (rare or a quest item)
  9390. if = 10000, then this item is sold in NPC shops only
  9391. 4 sex;
  9392. 5 equip;
  9393. 6 weight;
  9394. 7 atk;
  9395. 8 def;
  9396. 9 range;
  9397. 10 slot;
  9398. 11 look;
  9399. 12 elv;
  9400. 13 wlv;
  9401. 14 view id
  9402. * Returns Value or -1 if the wrong field's been set
  9403. *------------------------------------------*/
  9404. BUILDIN_FUNC(setiteminfo)
  9405. {
  9406. int item_id,n,value;
  9407. int *item_arr;
  9408. struct item_data *i_data;
  9409. item_id = script_getnum(st,2);
  9410. n = script_getnum(st,3);
  9411. value = script_getnum(st,4);
  9412. i_data = itemdb_exists(item_id);
  9413. if (i_data && n>=0 && n<14) {
  9414. item_arr = (int*)&i_data->value_buy;
  9415. item_arr[n] = value;
  9416. script_pushint(st,value);
  9417. } else
  9418. script_pushint(st,-1);
  9419. return 0;
  9420. }
  9421. /*==========================================
  9422. * Returns value from equipped item slot n [Lupus]
  9423. getequipcardid(num,slot)
  9424. where
  9425. num = eqip position slot
  9426. slot = 0,1,2,3 (Card Slot N)
  9427. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9428. it's useful when you want to check item cards or if it's signed
  9429. Useful for such quests as "Sign this refined item with players name" etc
  9430. Hat[0] +4 -> Player's Hat[0] +4
  9431. *------------------------------------------*/
  9432. BUILDIN_FUNC(getequipcardid)
  9433. {
  9434. int i=-1,num,slot;
  9435. TBL_PC *sd;
  9436. num=script_getnum(st,2);
  9437. slot=script_getnum(st,3);
  9438. sd=script_rid2sd(st);
  9439. if (num > 0 && num <= ARRAYLENGTH(equip))
  9440. i=pc_checkequip(sd,equip[num-1]);
  9441. if(i >= 0 && slot>=0 && slot<4)
  9442. script_pushint(st,sd->status.inventory[i].card[slot]);
  9443. else
  9444. script_pushint(st,0);
  9445. return 0;
  9446. }
  9447. /*==========================================
  9448. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9449. *------------------------------------------*/
  9450. BUILDIN_FUNC(petskillbonus)
  9451. {
  9452. struct pet_data *pd;
  9453. TBL_PC *sd=script_rid2sd(st);
  9454. if(sd==NULL || sd->pd==NULL)
  9455. return 0;
  9456. pd=sd->pd;
  9457. if (pd->bonus)
  9458. { //Clear previous bonus
  9459. if (pd->bonus->timer != -1)
  9460. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9461. } else //init
  9462. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9463. pd->bonus->type=script_getnum(st,2);
  9464. pd->bonus->val=script_getnum(st,3);
  9465. pd->bonus->duration=script_getnum(st,4);
  9466. pd->bonus->delay=script_getnum(st,5);
  9467. if (pd->state.skillbonus == 1)
  9468. pd->state.skillbonus=0; // waiting state
  9469. // wait for timer to start
  9470. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9471. pd->bonus->timer=-1;
  9472. else
  9473. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9474. return 0;
  9475. }
  9476. /*==========================================
  9477. * pet looting [Valaris] //Rewritten by [Skotlex]
  9478. *------------------------------------------*/
  9479. BUILDIN_FUNC(petloot)
  9480. {
  9481. int max;
  9482. struct pet_data *pd;
  9483. TBL_PC *sd=script_rid2sd(st);
  9484. if(sd==NULL || sd->pd==NULL)
  9485. return 0;
  9486. max=script_getnum(st,2);
  9487. if(max < 1)
  9488. max = 1; //Let'em loot at least 1 item.
  9489. else if (max > MAX_PETLOOT_SIZE)
  9490. max = MAX_PETLOOT_SIZE;
  9491. pd = sd->pd;
  9492. if (pd->loot != NULL)
  9493. { //Release whatever was there already and reallocate memory
  9494. pet_lootitem_drop(pd, pd->msd);
  9495. aFree(pd->loot->item);
  9496. }
  9497. else
  9498. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9499. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9500. pd->loot->max=max;
  9501. pd->loot->count = 0;
  9502. pd->loot->weight = 0;
  9503. return 0;
  9504. }
  9505. /*==========================================
  9506. * PCの所持品情報読み取り
  9507. *------------------------------------------*/
  9508. BUILDIN_FUNC(getinventorylist)
  9509. {
  9510. TBL_PC *sd=script_rid2sd(st);
  9511. char card_var[NAME_LENGTH];
  9512. int i,j=0,k;
  9513. if(!sd) return 0;
  9514. for(i=0;i<MAX_INVENTORY;i++){
  9515. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9516. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9517. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9518. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9519. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9520. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9521. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9522. for (k = 0; k < MAX_SLOTS; k++)
  9523. {
  9524. sprintf(card_var, "@inventorylist_card%d",k+1);
  9525. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9526. }
  9527. j++;
  9528. }
  9529. }
  9530. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9531. return 0;
  9532. }
  9533. BUILDIN_FUNC(getskilllist)
  9534. {
  9535. TBL_PC *sd=script_rid2sd(st);
  9536. int i,j=0;
  9537. if(!sd) return 0;
  9538. for(i=0;i<MAX_SKILL;i++){
  9539. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9540. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9541. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9542. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9543. j++;
  9544. }
  9545. }
  9546. pc_setreg(sd,add_str("@skilllist_count"),j);
  9547. return 0;
  9548. }
  9549. BUILDIN_FUNC(clearitem)
  9550. {
  9551. TBL_PC *sd=script_rid2sd(st);
  9552. int i;
  9553. if(sd==NULL) return 0;
  9554. for (i=0; i<MAX_INVENTORY; i++) {
  9555. if (sd->status.inventory[i].amount) {
  9556. //Logs items, got from (N)PC scripts [Lupus]
  9557. if(log_config.enable_logs&0x40)
  9558. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9559. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9560. }
  9561. }
  9562. return 0;
  9563. }
  9564. /*==========================================
  9565. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9566. *------------------------------------------*/
  9567. BUILDIN_FUNC(disguise)
  9568. {
  9569. int id;
  9570. TBL_PC* sd = script_rid2sd(st);
  9571. if (sd == NULL) return 0;
  9572. id = script_getnum(st,2);
  9573. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9574. pc_disguise(sd, id);
  9575. script_pushint(st,id);
  9576. } else
  9577. script_pushint(st,0);
  9578. return 0;
  9579. }
  9580. /*==========================================
  9581. * Undisguise Player (returns 1 if success, 0 on fail)
  9582. *------------------------------------------*/
  9583. BUILDIN_FUNC(undisguise)
  9584. {
  9585. TBL_PC* sd = script_rid2sd(st);
  9586. if (sd == NULL) return 0;
  9587. if (sd->disguise) {
  9588. pc_disguise(sd, 0);
  9589. script_pushint(st,0);
  9590. } else {
  9591. script_pushint(st,1);
  9592. }
  9593. return 0;
  9594. }
  9595. /*==========================================
  9596. * NPCクラスチェンジ
  9597. * classは変わりたいclass
  9598. * typeは通常0なのかな?
  9599. *------------------------------------------*/
  9600. BUILDIN_FUNC(classchange)
  9601. {
  9602. int _class,type;
  9603. struct block_list *bl=map_id2bl(st->oid);
  9604. if(bl==NULL) return 0;
  9605. _class=script_getnum(st,2);
  9606. type=script_getnum(st,3);
  9607. clif_class_change(bl,_class,type);
  9608. return 0;
  9609. }
  9610. /*==========================================
  9611. * NPCから発生するエフェクト
  9612. *------------------------------------------*/
  9613. BUILDIN_FUNC(misceffect)
  9614. {
  9615. int type;
  9616. type=script_getnum(st,2);
  9617. if(st->oid && st->oid != fake_nd->bl.id) {
  9618. struct block_list *bl = map_id2bl(st->oid);
  9619. if (bl)
  9620. clif_misceffect2(bl,type);
  9621. } else{
  9622. TBL_PC *sd=script_rid2sd(st);
  9623. if(sd)
  9624. clif_misceffect2(&sd->bl,type);
  9625. }
  9626. return 0;
  9627. }
  9628. /*==========================================
  9629. * サウンドエフェクト
  9630. *------------------------------------------*/
  9631. BUILDIN_FUNC(soundeffect)
  9632. {
  9633. TBL_PC* sd = script_rid2sd(st);
  9634. const char* name = script_getstr(st,2);
  9635. int type = script_getnum(st,3);
  9636. if(sd)
  9637. {
  9638. if(!st->rid)
  9639. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9640. else
  9641. clif_soundeffect(sd,&sd->bl,name,type);
  9642. }
  9643. return 0;
  9644. }
  9645. int soundeffect_sub(struct block_list* bl,va_list ap)
  9646. {
  9647. char* name = va_arg(ap,char*);
  9648. int type = va_arg(ap,int);
  9649. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9650. return 0;
  9651. }
  9652. /*==========================================
  9653. * Play a sound effect (.wav) on multiple clients
  9654. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9655. *------------------------------------------*/
  9656. BUILDIN_FUNC(soundeffectall)
  9657. {
  9658. struct block_list* bl;
  9659. const char* name;
  9660. int type;
  9661. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9662. if (!bl)
  9663. return 0;
  9664. name = script_getstr(st,2);
  9665. type = script_getnum(st,3);
  9666. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9667. if(!script_hasdata(st,4))
  9668. { // area around
  9669. clif_soundeffectall(bl, name, type, AREA);
  9670. }
  9671. else
  9672. if(!script_hasdata(st,5))
  9673. { // entire map
  9674. const char* map = script_getstr(st,4);
  9675. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9676. }
  9677. else
  9678. if(script_hasdata(st,9))
  9679. { // specified part of map
  9680. const char* map = script_getstr(st,5);
  9681. int x0 = script_getnum(st,6);
  9682. int y0 = script_getnum(st,7);
  9683. int x1 = script_getnum(st,8);
  9684. int y1 = script_getnum(st,9);
  9685. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9686. }
  9687. else
  9688. {
  9689. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9690. }
  9691. return 0;
  9692. }
  9693. /*==========================================
  9694. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9695. *------------------------------------------*/
  9696. BUILDIN_FUNC(petrecovery)
  9697. {
  9698. struct pet_data *pd;
  9699. TBL_PC *sd=script_rid2sd(st);
  9700. if(sd==NULL || sd->pd==NULL)
  9701. return 0;
  9702. pd=sd->pd;
  9703. if (pd->recovery)
  9704. { //Halt previous bonus
  9705. if (pd->recovery->timer != -1)
  9706. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9707. } else //Init
  9708. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9709. pd->recovery->type=script_getnum(st,2);
  9710. pd->recovery->delay=script_getnum(st,3);
  9711. pd->recovery->timer=-1;
  9712. return 0;
  9713. }
  9714. /*==========================================
  9715. * pet healing [Valaris] //Rewritten by [Skotlex]
  9716. *------------------------------------------*/
  9717. BUILDIN_FUNC(petheal)
  9718. {
  9719. struct pet_data *pd;
  9720. TBL_PC *sd=script_rid2sd(st);
  9721. if(sd==NULL || sd->pd==NULL)
  9722. return 0;
  9723. pd=sd->pd;
  9724. if (pd->s_skill)
  9725. { //Clear previous skill
  9726. if (pd->s_skill->timer != -1)
  9727. {
  9728. if (pd->s_skill->id)
  9729. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9730. else
  9731. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9732. }
  9733. } else //init memory
  9734. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9735. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9736. //Use the lv as the amount to heal
  9737. pd->s_skill->lv=script_getnum(st,2);
  9738. pd->s_skill->delay=script_getnum(st,3);
  9739. pd->s_skill->hp=script_getnum(st,4);
  9740. pd->s_skill->sp=script_getnum(st,5);
  9741. //Use delay as initial offset to avoid skill/heal exploits
  9742. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9743. pd->s_skill->timer=-1;
  9744. else
  9745. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9746. return 0;
  9747. }
  9748. /*==========================================
  9749. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9750. *------------------------------------------*/
  9751. BUILDIN_FUNC(petskillattack)
  9752. {
  9753. struct pet_data *pd;
  9754. TBL_PC *sd=script_rid2sd(st);
  9755. if(sd==NULL || sd->pd==NULL)
  9756. return 0;
  9757. pd=sd->pd;
  9758. if (pd->a_skill == NULL)
  9759. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9760. pd->a_skill->id=script_getnum(st,2);
  9761. pd->a_skill->lv=script_getnum(st,3);
  9762. pd->a_skill->div_ = 0;
  9763. pd->a_skill->rate=script_getnum(st,4);
  9764. pd->a_skill->bonusrate=script_getnum(st,5);
  9765. return 0;
  9766. }
  9767. /*==========================================
  9768. * pet attack skills [Valaris]
  9769. *------------------------------------------*/
  9770. BUILDIN_FUNC(petskillattack2)
  9771. {
  9772. struct pet_data *pd;
  9773. TBL_PC *sd=script_rid2sd(st);
  9774. if(sd==NULL || sd->pd==NULL)
  9775. return 0;
  9776. pd=sd->pd;
  9777. if (pd->a_skill == NULL)
  9778. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9779. pd->a_skill->id=script_getnum(st,2);
  9780. pd->a_skill->lv=script_getnum(st,3);
  9781. pd->a_skill->div_ = script_getnum(st,4);
  9782. pd->a_skill->rate=script_getnum(st,5);
  9783. pd->a_skill->bonusrate=script_getnum(st,6);
  9784. return 0;
  9785. }
  9786. /*==========================================
  9787. * pet support skills [Skotlex]
  9788. *------------------------------------------*/
  9789. BUILDIN_FUNC(petskillsupport)
  9790. {
  9791. struct pet_data *pd;
  9792. TBL_PC *sd=script_rid2sd(st);
  9793. if(sd==NULL || sd->pd==NULL)
  9794. return 0;
  9795. pd=sd->pd;
  9796. if (pd->s_skill)
  9797. { //Clear previous skill
  9798. if (pd->s_skill->timer != -1)
  9799. {
  9800. if (pd->s_skill->id)
  9801. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9802. else
  9803. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9804. }
  9805. } else //init memory
  9806. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9807. pd->s_skill->id=script_getnum(st,2);
  9808. pd->s_skill->lv=script_getnum(st,3);
  9809. pd->s_skill->delay=script_getnum(st,4);
  9810. pd->s_skill->hp=script_getnum(st,5);
  9811. pd->s_skill->sp=script_getnum(st,6);
  9812. //Use delay as initial offset to avoid skill/heal exploits
  9813. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9814. pd->s_skill->timer=-1;
  9815. else
  9816. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9817. return 0;
  9818. }
  9819. /*==========================================
  9820. * Scripted skill effects [Celest]
  9821. *------------------------------------------*/
  9822. BUILDIN_FUNC(skilleffect)
  9823. {
  9824. TBL_PC *sd;
  9825. int skillid=script_getnum(st,2);
  9826. int skilllv=script_getnum(st,3);
  9827. sd=script_rid2sd(st);
  9828. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9829. return 0;
  9830. }
  9831. /*==========================================
  9832. * NPC skill effects [Valaris]
  9833. *------------------------------------------*/
  9834. BUILDIN_FUNC(npcskilleffect)
  9835. {
  9836. struct block_list *bl= map_id2bl(st->oid);
  9837. int skillid=script_getnum(st,2);
  9838. int skilllv=script_getnum(st,3);
  9839. int x=script_getnum(st,4);
  9840. int y=script_getnum(st,5);
  9841. if (bl)
  9842. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9843. return 0;
  9844. }
  9845. /*==========================================
  9846. * Special effects [Valaris]
  9847. *------------------------------------------*/
  9848. BUILDIN_FUNC(specialeffect)
  9849. {
  9850. struct block_list *bl=map_id2bl(st->oid);
  9851. if(bl==NULL)
  9852. return 0;
  9853. clif_specialeffect(bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9854. return 0;
  9855. }
  9856. BUILDIN_FUNC(specialeffect2)
  9857. {
  9858. TBL_PC *sd=script_rid2sd(st);
  9859. if(sd==NULL)
  9860. return 0;
  9861. clif_specialeffect(&sd->bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9862. return 0;
  9863. }
  9864. /*==========================================
  9865. * Nude [Valaris]
  9866. *------------------------------------------*/
  9867. BUILDIN_FUNC(nude)
  9868. {
  9869. TBL_PC *sd=script_rid2sd(st);
  9870. int i,calcflag=0;
  9871. if(sd==NULL)
  9872. return 0;
  9873. for(i=0;i<11;i++)
  9874. if(sd->equip_index[i] >= 0) {
  9875. if(!calcflag)
  9876. calcflag=1;
  9877. pc_unequipitem(sd,sd->equip_index[i],2);
  9878. }
  9879. if(calcflag)
  9880. status_calc_pc(sd,0);
  9881. return 0;
  9882. }
  9883. /*==========================================
  9884. * gmcommand [MouseJstr]
  9885. *
  9886. * suggested on the forums...
  9887. * splitted into atcommand & charcommand by [Skotlex]
  9888. *------------------------------------------*/
  9889. BUILDIN_FUNC(atcommand)
  9890. {
  9891. TBL_PC *sd=NULL;
  9892. const char *cmd;
  9893. cmd = script_getstr(st,2);
  9894. if (st->rid)
  9895. sd = script_rid2sd(st);
  9896. if (sd){
  9897. if(cmd[0] != atcommand_symbol){
  9898. cmd += strlen(sd->status.name);
  9899. while(*cmd != atcommand_symbol && *cmd != 0)
  9900. cmd++;
  9901. }
  9902. is_atcommand_sub(sd->fd, sd, cmd, 99);
  9903. } else { //Use a dummy character.
  9904. TBL_PC dummy_sd;
  9905. struct block_list *bl = NULL;
  9906. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9907. if (st->oid) bl = map_id2bl(st->oid);
  9908. if (bl) {
  9909. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9910. if (bl->type == BL_NPC)
  9911. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9912. }
  9913. if(cmd[0] != atcommand_symbol){
  9914. cmd += strlen(dummy_sd.status.name);
  9915. while(*cmd != atcommand_symbol && *cmd != 0)
  9916. cmd++;
  9917. }
  9918. is_atcommand_sub(0, &dummy_sd, cmd, 99);
  9919. }
  9920. return 0;
  9921. }
  9922. BUILDIN_FUNC(charcommand)
  9923. {
  9924. TBL_PC *sd=NULL;
  9925. const char *cmd;
  9926. cmd = script_getstr(st,2);
  9927. if (st->rid)
  9928. sd = script_rid2sd(st);
  9929. if (sd){
  9930. if(cmd[0] != charcommand_symbol){
  9931. cmd += strlen(sd->status.name);
  9932. while(*cmd != charcommand_symbol && *cmd != 0)
  9933. cmd++;
  9934. }
  9935. is_charcommand_sub(sd->fd, sd, cmd,99);
  9936. } else { //Use a dummy character.
  9937. TBL_PC dummy_sd;
  9938. struct block_list *bl = NULL;
  9939. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9940. if (st->oid) bl = map_id2bl(st->oid);
  9941. if (bl) {
  9942. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9943. if (bl->type == BL_NPC)
  9944. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9945. }
  9946. if(cmd[0] != charcommand_symbol){
  9947. cmd += strlen(dummy_sd.status.name);
  9948. while(*cmd != charcommand_symbol && *cmd != 0)
  9949. cmd++;
  9950. }
  9951. is_charcommand_sub(0, &dummy_sd, cmd, 99);
  9952. }
  9953. return 0;
  9954. }
  9955. /*==========================================
  9956. * Displays a message for the player only (like system messages like "you got an apple" )
  9957. *------------------------------------------*/
  9958. BUILDIN_FUNC(dispbottom)
  9959. {
  9960. TBL_PC *sd=script_rid2sd(st);
  9961. const char *message;
  9962. message=script_getstr(st,2);
  9963. if(sd)
  9964. clif_disp_onlyself(sd,message,(int)strlen(message));
  9965. return 0;
  9966. }
  9967. /*==========================================
  9968. * All The Players Full Recovery
  9969. * (HP/SP full restore and resurrect if need)
  9970. *------------------------------------------*/
  9971. BUILDIN_FUNC(recovery)
  9972. {
  9973. TBL_PC *sd, **all_sd;
  9974. int i = 0, users;
  9975. all_sd = map_getallusers(&users);
  9976. for (i = 0; i < users; i++)
  9977. {
  9978. sd = all_sd[i];
  9979. if(pc_isdead(sd))
  9980. status_revive(&sd->bl, 100, 100);
  9981. else
  9982. status_percent_heal(&sd->bl, 100, 100);
  9983. clif_displaymessage(sd->fd,"You have been recovered!");
  9984. }
  9985. return 0;
  9986. }
  9987. /*==========================================
  9988. * Get your pet info: getpetinfo(n)
  9989. * n -> 0:pet_id 1:pet_class 2:pet_name
  9990. * 3:friendly 4:hungry, 5: rename flag.
  9991. *------------------------------------------*/
  9992. BUILDIN_FUNC(getpetinfo)
  9993. {
  9994. TBL_PC *sd=script_rid2sd(st);
  9995. TBL_PET *pd;
  9996. int type=script_getnum(st,2);
  9997. if(!sd || !sd->pd) {
  9998. if (type == 2)
  9999. script_pushconststr(st,"null");
  10000. else
  10001. script_pushint(st,0);
  10002. return 0;
  10003. }
  10004. pd = sd->pd;
  10005. switch(type){
  10006. case 0: script_pushint(st,pd->pet.pet_id); break;
  10007. case 1: script_pushint(st,pd->pet.class_); break;
  10008. case 2: script_pushstr(st,aStrdup(pd->pet.name)); break;
  10009. case 3: script_pushint(st,pd->pet.intimate); break;
  10010. case 4: script_pushint(st,pd->pet.hungry); break;
  10011. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10012. default:
  10013. script_pushint(st,0);
  10014. break;
  10015. }
  10016. return 0;
  10017. }
  10018. /*==========================================
  10019. * Get your homunculus info: gethominfo(n)
  10020. * n -> 0:hom_id 1:class 2:name
  10021. * 3:friendly 4:hungry, 5: rename flag.
  10022. * 6: level
  10023. *------------------------------------------*/
  10024. BUILDIN_FUNC(gethominfo)
  10025. {
  10026. TBL_PC *sd=script_rid2sd(st);
  10027. TBL_HOM *hd;
  10028. int type=script_getnum(st,2);
  10029. hd = sd?sd->hd:NULL;
  10030. if(!merc_is_hom_active(hd))
  10031. {
  10032. if (type == 2)
  10033. script_pushconststr(st,"null");
  10034. else
  10035. script_pushint(st,0);
  10036. return 0;
  10037. }
  10038. switch(type){
  10039. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10040. case 1: script_pushint(st,hd->homunculus.class_); break;
  10041. case 2: script_pushstr(st,aStrdup(hd->homunculus.name)); break;
  10042. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10043. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10044. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10045. case 6: script_pushint(st,hd->homunculus.level); break;
  10046. default:
  10047. script_pushint(st,0);
  10048. break;
  10049. }
  10050. return 0;
  10051. }
  10052. /*==========================================
  10053. * Shows wether your inventory(and equips) contain
  10054. selected card or not.
  10055. checkequipedcard(4001);
  10056. *------------------------------------------*/
  10057. BUILDIN_FUNC(checkequipedcard)
  10058. {
  10059. TBL_PC *sd=script_rid2sd(st);
  10060. int n,i,c=0;
  10061. c=script_getnum(st,2);
  10062. if(sd){
  10063. for(i=0;i<MAX_INVENTORY;i++){
  10064. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  10065. for(n=0;n<MAX_SLOTS;n++){
  10066. if(sd->status.inventory[i].card[n]==c){
  10067. script_pushint(st,1);
  10068. return 0;
  10069. }
  10070. }
  10071. }
  10072. }
  10073. }
  10074. script_pushint(st,0);
  10075. return 0;
  10076. }
  10077. BUILDIN_FUNC(jump_zero)
  10078. {
  10079. int sel;
  10080. sel=script_getnum(st,2);
  10081. if(!sel) {
  10082. int pos;
  10083. if( !data_islabel(script_getdata(st,3)) ){
  10084. ShowError("script: jump_zero: not label !\n");
  10085. st->state=END;
  10086. return 1;
  10087. }
  10088. pos=script_getnum(st,3);
  10089. st->pos=pos;
  10090. st->state=GOTO;
  10091. // printf("script: jump_zero: jumpto : %d\n",pos);
  10092. } else {
  10093. // printf("script: jump_zero: fail\n");
  10094. }
  10095. return 0;
  10096. }
  10097. /*==========================================
  10098. * GetMapMobs
  10099. returns mob counts on a set map:
  10100. e.g. GetMapMobs("prontera")
  10101. use "this" - for player's map
  10102. *------------------------------------------*/
  10103. BUILDIN_FUNC(getmapmobs)
  10104. {
  10105. const char *str=NULL;
  10106. int m=-1,bx,by,i;
  10107. int count=0,c;
  10108. struct block_list *bl;
  10109. str=script_getstr(st,2);
  10110. if(strcmp(str,"this")==0){
  10111. TBL_PC *sd=script_rid2sd(st);
  10112. if(sd)
  10113. m=sd->bl.m;
  10114. else{
  10115. script_pushint(st,-1);
  10116. return 0;
  10117. }
  10118. }else
  10119. m=map_mapname2mapid(str);
  10120. if(m < 0){
  10121. script_pushint(st,-1);
  10122. return 0;
  10123. }
  10124. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++){
  10125. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++){
  10126. bl = map[m].block_mob[bx+by*map[m].bxs];
  10127. c = map[m].block_mob_count[bx+by*map[m].bxs];
  10128. for(i=0;i<c && bl;i++,bl=bl->next){
  10129. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  10130. count++;
  10131. }
  10132. }
  10133. }
  10134. script_pushint(st,count);
  10135. return 0;
  10136. }
  10137. /*==========================================
  10138. * movenpc [MouseJstr]
  10139. *------------------------------------------*/
  10140. BUILDIN_FUNC(movenpc)
  10141. {
  10142. TBL_NPC *nd = NULL;
  10143. const char *npc;
  10144. int x,y;
  10145. npc = script_getstr(st,2);
  10146. x = script_getnum(st,3);
  10147. y = script_getnum(st,4);
  10148. if ((nd = npc_name2id(npc)) == NULL)
  10149. return -1;
  10150. npc_movenpc(nd, x, y);
  10151. return 0;
  10152. }
  10153. /*==========================================
  10154. * message [MouseJstr]
  10155. *------------------------------------------*/
  10156. BUILDIN_FUNC(message)
  10157. {
  10158. TBL_PC *sd;
  10159. const char *msg,*player;
  10160. TBL_PC *pl_sd = NULL;
  10161. sd = script_rid2sd(st);
  10162. player = script_getstr(st,2);
  10163. msg = script_getstr(st,3);
  10164. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10165. return 0;
  10166. clif_displaymessage(pl_sd->fd, msg);
  10167. return 0;
  10168. }
  10169. /*==========================================
  10170. * npctalk (sends message to surrounding area)
  10171. *------------------------------------------*/
  10172. BUILDIN_FUNC(npctalk)
  10173. {
  10174. const char* str;
  10175. char message[255];
  10176. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10177. str = script_getstr(st,2);
  10178. if(nd) {
  10179. memcpy(message, nd->name, NAME_LENGTH);
  10180. strtok(message, "#"); // discard extra name identifier if present
  10181. strcat(message, " : ");
  10182. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  10183. clif_message(&(nd->bl), message);
  10184. }
  10185. return 0;
  10186. }
  10187. // change npc walkspeed [Valaris]
  10188. BUILDIN_FUNC(npcspeed)
  10189. {
  10190. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10191. int x=0;
  10192. x=script_getnum(st,2);
  10193. if(nd) {
  10194. nd->speed=x;
  10195. }
  10196. return 0;
  10197. }
  10198. // make an npc walk to a position [Valaris]
  10199. BUILDIN_FUNC(npcwalkto)
  10200. {
  10201. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10202. int x=0,y=0;
  10203. x=script_getnum(st,2);
  10204. y=script_getnum(st,3);
  10205. if(nd) {
  10206. unit_walktoxy(&nd->bl,x,y,0);
  10207. }
  10208. return 0;
  10209. }
  10210. // stop an npc's movement [Valaris]
  10211. BUILDIN_FUNC(npcstop)
  10212. {
  10213. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10214. if(nd) {
  10215. unit_stop_walking(&nd->bl,1);
  10216. }
  10217. return 0;
  10218. }
  10219. /*==========================================
  10220. * getlook char info. getlook(arg)
  10221. *------------------------------------------*/
  10222. BUILDIN_FUNC(getlook)
  10223. {
  10224. int type,val;
  10225. TBL_PC *sd;
  10226. sd=script_rid2sd(st);
  10227. type=script_getnum(st,2);
  10228. val=-1;
  10229. switch(type) {
  10230. case LOOK_HAIR: val=sd->status.hair; break; //1
  10231. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10232. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10233. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10234. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10235. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10236. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10237. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10238. case LOOK_SHOES: break; //9
  10239. }
  10240. script_pushint(st,val);
  10241. return 0;
  10242. }
  10243. /*==========================================
  10244. * get char save point. argument: 0- map name, 1- x, 2- y
  10245. *------------------------------------------*/
  10246. BUILDIN_FUNC(getsavepoint)
  10247. {
  10248. TBL_PC* sd;
  10249. int type;
  10250. sd = script_rid2sd(st);
  10251. if (sd == NULL) {
  10252. script_pushint(st,0);
  10253. return 0;
  10254. }
  10255. type = script_getnum(st,2);
  10256. switch(type) {
  10257. case 0: script_pushstr(st,aStrdup(mapindex_id2name(sd->status.save_point.map))); break;
  10258. case 1: script_pushint(st,sd->status.save_point.x); break;
  10259. case 2: script_pushint(st,sd->status.save_point.y); break;
  10260. default:
  10261. script_pushint(st,0);
  10262. break;
  10263. }
  10264. return 0;
  10265. }
  10266. /*==========================================
  10267. * Get position for char/npc/pet/mob objects. Added by Lorky
  10268. *
  10269. * int getMapXY(MapName$,MaxX,MapY,type,[CharName$]);
  10270. * where type:
  10271. * MapName$ - String variable for output map name
  10272. * MapX - Integer variable for output coord X
  10273. * MapY - Integer variable for output coord Y
  10274. * type - type of object
  10275. * 0 - Character coord
  10276. * 1 - NPC coord
  10277. * 2 - Pet coord
  10278. * 3 - Mob coord (not released)
  10279. * 4 - Homun coord
  10280. * CharName$ - Name object. If miss or "this" the current object
  10281. *
  10282. * Return:
  10283. * 0 - success
  10284. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10285. *------------------------------------------*/
  10286. BUILDIN_FUNC(getmapxy)
  10287. {
  10288. struct block_list *bl = NULL;
  10289. TBL_PC *sd=NULL;
  10290. int num;
  10291. char *name;
  10292. char prefix;
  10293. int x,y,type;
  10294. char mapname[MAP_NAME_LENGTH];
  10295. memset(mapname, 0, sizeof(mapname));
  10296. if( !data_isreference(script_getdata(st,2)) ){
  10297. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10298. script_pushint(st,-1);
  10299. return 1;
  10300. }
  10301. if( !data_isreference(script_getdata(st,3)) ){
  10302. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10303. script_pushint(st,-1);
  10304. return 1;
  10305. }
  10306. if( !data_isreference(script_getdata(st,4)) ){
  10307. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10308. script_pushint(st,-1);
  10309. return 1;
  10310. }
  10311. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10312. type=script_getnum(st,5);
  10313. switch (type){
  10314. case 0: //Get Character Position
  10315. if( script_hasdata(st,6) )
  10316. sd=map_nick2sd(script_getstr(st,6));
  10317. else
  10318. sd=script_rid2sd(st);
  10319. if (sd)
  10320. bl = &sd->bl;
  10321. break;
  10322. case 1: //Get NPC Position
  10323. if( script_hasdata(st,6) )
  10324. {
  10325. struct npc_data *nd;
  10326. nd=npc_name2id(script_getstr(st,6));
  10327. if (nd)
  10328. bl = &nd->bl;
  10329. } else //In case the origin is not an npc?
  10330. bl=map_id2bl(st->oid);
  10331. break;
  10332. case 2: //Get Pet Position
  10333. if(script_hasdata(st,6))
  10334. sd=map_nick2sd(script_getstr(st,6));
  10335. else
  10336. sd=script_rid2sd(st);
  10337. if (sd && sd->pd)
  10338. bl = &sd->pd->bl;
  10339. break;
  10340. case 3: //Get Mob Position
  10341. break; //Not supported?
  10342. case 4: //Get Homun Position
  10343. if(script_hasdata(st,6))
  10344. sd=map_nick2sd(script_getstr(st,6));
  10345. else
  10346. sd=script_rid2sd(st);
  10347. if (sd && sd->hd)
  10348. bl = &sd->hd->bl;
  10349. break;
  10350. }
  10351. if (!bl) { //No object found.
  10352. script_pushint(st,-1);
  10353. return 0;
  10354. }
  10355. x= bl->x;
  10356. y= bl->y;
  10357. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10358. //Set MapName$
  10359. num=st->stack->stack_data[st->start+2].u.num;
  10360. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10361. prefix=*name;
  10362. if(not_server_variable(prefix))
  10363. sd=script_rid2sd(st);
  10364. else
  10365. sd=NULL;
  10366. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10367. //Set MapX
  10368. num=st->stack->stack_data[st->start+3].u.num;
  10369. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10370. prefix=*name;
  10371. if(not_server_variable(prefix))
  10372. sd=script_rid2sd(st);
  10373. else
  10374. sd=NULL;
  10375. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10376. //Set MapY
  10377. num=st->stack->stack_data[st->start+4].u.num;
  10378. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10379. prefix=*name;
  10380. if(not_server_variable(prefix))
  10381. sd=script_rid2sd(st);
  10382. else
  10383. sd=NULL;
  10384. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10385. //Return Success value
  10386. script_pushint(st,0);
  10387. return 0;
  10388. }
  10389. /*==========================================
  10390. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10391. *------------------------------------------*/
  10392. BUILDIN_FUNC(logmes)
  10393. {
  10394. const char *str;
  10395. if (log_config.npc <= 0 ) return 0;
  10396. str = script_getstr(st,2);
  10397. log_npc(script_rid2sd(st),str);
  10398. return 0;
  10399. }
  10400. BUILDIN_FUNC(summon)
  10401. {
  10402. int _class, timeout=0;
  10403. const char *str,*event="";
  10404. TBL_PC *sd;
  10405. struct mob_data *md;
  10406. int tick = gettick();
  10407. sd=script_rid2sd(st);
  10408. if (!sd) return 0;
  10409. str =script_getstr(st,2);
  10410. _class=script_getnum(st,3);
  10411. if( script_hasdata(st,4) )
  10412. timeout=script_getnum(st,4);
  10413. if( script_hasdata(st,5) ){
  10414. event=script_getstr(st,5);
  10415. check_event(st, event);
  10416. }
  10417. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10418. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10419. if (md) {
  10420. md->master_id=sd->bl.id;
  10421. md->special_state.ai=1;
  10422. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10423. mob_spawn (md); //Now it is ready for spawning.
  10424. clif_misceffect2(&md->bl,344);
  10425. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10426. }
  10427. return 0;
  10428. }
  10429. /*==========================================
  10430. * Checks whether it is daytime/nighttime
  10431. *------------------------------------------*/
  10432. BUILDIN_FUNC(isnight)
  10433. {
  10434. script_pushint(st,(night_flag == 1));
  10435. return 0;
  10436. }
  10437. BUILDIN_FUNC(isday)
  10438. {
  10439. script_pushint(st,(night_flag == 0));
  10440. return 0;
  10441. }
  10442. /*================================================
  10443. * Check whether another item/card has been
  10444. * equipped - used for 2/15's cards patch [celest]
  10445. *------------------------------------------------*/
  10446. // leave this here, just in case
  10447. #if 0
  10448. BUILDIN_FUNC(isequipped)
  10449. {
  10450. TBL_PC *sd;
  10451. int i, j, k, id = 1;
  10452. int ret = -1;
  10453. sd = script_rid2sd(st);
  10454. for (i=0; id!=0; i++) {
  10455. int flag = 0;
  10456. FETCH (i+2, id) else id = 0;
  10457. if (id <= 0)
  10458. continue;
  10459. for (j=0; j<EQI_MAX; j++) {
  10460. int index;
  10461. index = sd->equip_index[j];
  10462. if(index < 0) continue;
  10463. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10464. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10465. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10466. if(!sd->inventory_data[index])
  10467. continue;
  10468. if(itemdb_type(id) != IT_CARD) { //Non card
  10469. if (sd->inventory_data[index]->nameid != id)
  10470. continue;
  10471. flag = 1;
  10472. break;
  10473. } else { //Card
  10474. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10475. continue;
  10476. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10477. if (sd->status.inventory[index].card[k] == id)
  10478. {
  10479. flag = 1;
  10480. break;
  10481. }
  10482. }
  10483. }
  10484. if (flag) break;
  10485. }
  10486. if (ret == -1)
  10487. ret = flag;
  10488. else
  10489. ret &= flag;
  10490. if (!ret) break;
  10491. }
  10492. script_pushint(st,ret);
  10493. return 0;
  10494. }
  10495. #endif
  10496. /*================================================
  10497. * Check how many items/cards in the list are
  10498. * equipped - used for 2/15's cards patch [celest]
  10499. *------------------------------------------------*/
  10500. BUILDIN_FUNC(isequippedcnt)
  10501. {
  10502. TBL_PC *sd;
  10503. int i, j, k, id = 1;
  10504. int ret = 0;
  10505. sd = script_rid2sd(st);
  10506. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10507. script_pushint(st,0);
  10508. return 0;
  10509. }
  10510. for (i=0; id!=0; i++) {
  10511. FETCH (i+2, id) else id = 0;
  10512. if (id <= 0)
  10513. continue;
  10514. for (j=0; j<EQI_MAX; j++) {
  10515. int index;
  10516. index = sd->equip_index[j];
  10517. if(index < 0) continue;
  10518. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10519. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10520. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10521. if(!sd->inventory_data[index])
  10522. continue;
  10523. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10524. if (sd->inventory_data[index]->nameid == id)
  10525. ret+= sd->status.inventory[index].amount;
  10526. } else { //Count cards.
  10527. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10528. continue; //No cards
  10529. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10530. if (sd->status.inventory[index].card[k] == id)
  10531. ret++; //[Lupus]
  10532. }
  10533. }
  10534. }
  10535. }
  10536. script_pushint(st,ret);
  10537. return 0;
  10538. }
  10539. /*================================================
  10540. * Check whether another card has been
  10541. * equipped - used for 2/15's cards patch [celest]
  10542. * -- Items checked cannot be reused in another
  10543. * card set to prevent exploits
  10544. *------------------------------------------------*/
  10545. BUILDIN_FUNC(isequipped)
  10546. {
  10547. TBL_PC *sd;
  10548. int i, j, k, id = 1;
  10549. int index, flag;
  10550. int ret = -1;
  10551. //Original hash to reverse it when full check fails.
  10552. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10553. sd = script_rid2sd(st);
  10554. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10555. script_pushint(st,0);
  10556. return 0;
  10557. }
  10558. setitem_hash = sd->setitem_hash;
  10559. setitem_hash2 = sd->setitem_hash2;
  10560. for (i=0; id!=0; i++)
  10561. {
  10562. FETCH (i+2, id) else id = 0;
  10563. if (id <= 0)
  10564. continue;
  10565. flag = 0;
  10566. for (j=0; j<EQI_MAX; j++)
  10567. {
  10568. index = sd->equip_index[j];
  10569. if(index < 0) continue;
  10570. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10571. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10572. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10573. if(!sd->inventory_data[index])
  10574. continue;
  10575. if (itemdb_type(id) != IT_CARD) {
  10576. if (sd->inventory_data[index]->nameid != id)
  10577. continue;
  10578. flag = 1;
  10579. break;
  10580. } else { //Cards
  10581. if (sd->inventory_data[index]->slot == 0 ||
  10582. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10583. continue;
  10584. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10585. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10586. unsigned int hash = 0;
  10587. if (sd->status.inventory[index].card[k] != id)
  10588. continue;
  10589. hash = 1<<((j<5?j:j-5)*4 + k);
  10590. // check if card is already used by another set
  10591. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10592. continue;
  10593. // We have found a match
  10594. flag = 1;
  10595. // Set hash so this card cannot be used by another
  10596. if (j<5)
  10597. sd->setitem_hash |= hash;
  10598. else
  10599. sd->setitem_hash2 |= hash;
  10600. break;
  10601. }
  10602. }
  10603. if (flag) break; //Card found
  10604. }
  10605. if (ret == -1)
  10606. ret = flag;
  10607. else
  10608. ret &= flag;
  10609. if (!ret) break;
  10610. }
  10611. if (!ret)
  10612. { //When check fails, restore original hash values. [Skotlex]
  10613. sd->setitem_hash = setitem_hash;
  10614. sd->setitem_hash2 = setitem_hash2;
  10615. }
  10616. script_pushint(st,ret);
  10617. return 0;
  10618. }
  10619. /*================================================
  10620. * Check how many given inserted cards in the CURRENT
  10621. * weapon - used for 2/15's cards patch [Lupus]
  10622. *------------------------------------------------*/
  10623. BUILDIN_FUNC(cardscnt)
  10624. {
  10625. TBL_PC *sd;
  10626. int i, k, id = 1;
  10627. int ret = 0;
  10628. int index;
  10629. sd = script_rid2sd(st);
  10630. for (i=0; id!=0; i++) {
  10631. FETCH (i+2, id) else id = 0;
  10632. if (id <= 0)
  10633. continue;
  10634. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10635. if(index < 0) continue;
  10636. if(!sd->inventory_data[index])
  10637. continue;
  10638. if(itemdb_type(id) != IT_CARD) {
  10639. if (sd->inventory_data[index]->nameid == id)
  10640. ret+= sd->status.inventory[index].amount;
  10641. } else {
  10642. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10643. continue;
  10644. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10645. if (sd->status.inventory[index].card[k] == id)
  10646. ret++;
  10647. }
  10648. }
  10649. }
  10650. script_pushint(st,ret);
  10651. // script_pushint(st,current_equip_item_index);
  10652. return 0;
  10653. }
  10654. /*=======================================================
  10655. * Returns the refined number of the current item, or an
  10656. * item with inventory index specified
  10657. *-------------------------------------------------------*/
  10658. BUILDIN_FUNC(getrefine)
  10659. {
  10660. TBL_PC *sd;
  10661. if ((sd = script_rid2sd(st))!= NULL)
  10662. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10663. else
  10664. script_pushint(st,0);
  10665. return 0;
  10666. }
  10667. /*=======================================================
  10668. * Allows 2 Parents to adopt a character as a Baby
  10669. *-------------------------------------------------------*/
  10670. BUILDIN_FUNC(adopt)
  10671. {
  10672. int ret;
  10673. const char *parent1 = script_getstr(st,2);
  10674. const char *parent2 = script_getstr(st,3);
  10675. const char *child = script_getstr(st,4);
  10676. TBL_PC *p1_sd = map_nick2sd(parent1);
  10677. TBL_PC *p2_sd = map_nick2sd(parent2);
  10678. TBL_PC *c_sd = map_nick2sd(child);
  10679. if (!p1_sd || !p2_sd || !c_sd ||
  10680. p1_sd->status.base_level < 70 ||
  10681. p2_sd->status.base_level < 70)
  10682. return 0;
  10683. ret = pc_adoption(p1_sd, p2_sd, c_sd);
  10684. script_pushint(st,ret);
  10685. return 0;
  10686. }
  10687. /*=======================================================
  10688. * Day/Night controls
  10689. *-------------------------------------------------------*/
  10690. BUILDIN_FUNC(night)
  10691. {
  10692. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10693. return 0;
  10694. }
  10695. BUILDIN_FUNC(day)
  10696. {
  10697. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10698. return 0;
  10699. }
  10700. //=======================================================
  10701. // Unequip [Spectre]
  10702. //-------------------------------------------------------
  10703. BUILDIN_FUNC(unequip)
  10704. {
  10705. int i;
  10706. size_t num;
  10707. TBL_PC *sd;
  10708. num = script_getnum(st,2);
  10709. sd = script_rid2sd(st);
  10710. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10711. {
  10712. i = pc_checkequip(sd,equip[num-1]);
  10713. if (i >= 0)
  10714. pc_unequipitem(sd,i,2);
  10715. return 0;
  10716. }
  10717. return 0;
  10718. }
  10719. BUILDIN_FUNC(equip)
  10720. {
  10721. int nameid=0,i;
  10722. TBL_PC *sd;
  10723. struct item_data *item_data;
  10724. sd = script_rid2sd(st);
  10725. nameid=script_getnum(st,2);
  10726. if((item_data = itemdb_exists(nameid)) == NULL)
  10727. {
  10728. if(battle_config.error_log)
  10729. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10730. return 1;
  10731. }
  10732. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10733. if( i < MAX_INVENTORY )
  10734. pc_equipitem(sd,i,item_data->equip);
  10735. return 0;
  10736. }
  10737. BUILDIN_FUNC(autoequip)
  10738. {
  10739. int nameid, flag;
  10740. struct item_data *item_data;
  10741. nameid=script_getnum(st,2);
  10742. flag=script_getnum(st,3);
  10743. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10744. item_data->flag.autoequip = flag>0?1:0;
  10745. }
  10746. return 0;
  10747. }
  10748. BUILDIN_FUNC(setbattleflag)
  10749. {
  10750. const char *flag, *value;
  10751. flag = script_getstr(st,2);
  10752. value = script_getstr(st,3);
  10753. if (battle_set_value(flag, value) == 0)
  10754. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10755. else
  10756. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10757. return 0;
  10758. }
  10759. BUILDIN_FUNC(getbattleflag)
  10760. {
  10761. const char *flag;
  10762. flag = script_getstr(st,2);
  10763. script_pushint(st,battle_get_value(flag));
  10764. return 0;
  10765. }
  10766. //=======================================================
  10767. // strlen [Valaris]
  10768. //-------------------------------------------------------
  10769. BUILDIN_FUNC(getstrlen)
  10770. {
  10771. const char *str = script_getstr(st,2);
  10772. int len = (str) ? (int)strlen(str) : 0;
  10773. script_pushint(st,len);
  10774. return 0;
  10775. }
  10776. //=======================================================
  10777. // isalpha [Valaris]
  10778. //-------------------------------------------------------
  10779. BUILDIN_FUNC(charisalpha)
  10780. {
  10781. const char *str=script_getstr(st,2);
  10782. int pos=script_getnum(st,3);
  10783. int val = ( str && pos>0 && (unsigned int)pos<strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10784. script_pushint(st,val);
  10785. return 0;
  10786. }
  10787. // [Lance]
  10788. BUILDIN_FUNC(fakenpcname)
  10789. {
  10790. const char *name;
  10791. const char *newname;
  10792. int look;
  10793. name = script_getstr(st,2);
  10794. newname = script_getstr(st,3);
  10795. look = script_getnum(st,4);
  10796. if(look > 32767 || look < -32768) {
  10797. ShowError("buildin_fakenpcname: Invalid look value %d\n",look);
  10798. return 1; // Safety measure to prevent runtime errors
  10799. }
  10800. npc_changename(name,newname,(short)look);
  10801. return 0;
  10802. }
  10803. BUILDIN_FUNC(atoi)
  10804. {
  10805. const char *value;
  10806. value = script_getstr(st,2);
  10807. script_pushint(st,atoi(value));
  10808. return 0;
  10809. }
  10810. // case-insensitive substring search [lordalfa]
  10811. BUILDIN_FUNC(compare)
  10812. {
  10813. const char *message;
  10814. const char *cmpstring;
  10815. message = script_getstr(st,2);
  10816. cmpstring = script_getstr(st,3);
  10817. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10818. return 0;
  10819. }
  10820. // [zBuffer] List of mathematics commands --->
  10821. BUILDIN_FUNC(sqrt)
  10822. {
  10823. double i, a;
  10824. i = script_getnum(st,2);
  10825. a = sqrt(i);
  10826. script_pushint(st,(int)a);
  10827. return 0;
  10828. }
  10829. BUILDIN_FUNC(pow)
  10830. {
  10831. double i, a, b;
  10832. a = script_getnum(st,2);
  10833. b = script_getnum(st,3);
  10834. i = pow(a,b);
  10835. script_pushint(st,(int)i);
  10836. return 0;
  10837. }
  10838. BUILDIN_FUNC(distance)
  10839. {
  10840. int x0, y0, x1, y1;
  10841. x0 = script_getnum(st,2);
  10842. y0 = script_getnum(st,3);
  10843. x1 = script_getnum(st,4);
  10844. y1 = script_getnum(st,5);
  10845. script_pushint(st,distance(x0-x1, y0-y1));
  10846. return 0;
  10847. }
  10848. BUILDIN_FUNC(checkcell)
  10849. {
  10850. int m;
  10851. const char *map = script_getstr(st, 2);
  10852. m = mapindex_name2id(map);
  10853. if(m){
  10854. script_pushint(st,map_getcell(m, script_getnum(st,3), script_getnum(st,4),script_getnum(st,5)));
  10855. } else {
  10856. script_pushint(st,0);
  10857. }
  10858. return 0;
  10859. }
  10860. // <--- [zBuffer] List of mathematics commands
  10861. // [zBuffer] List of dynamic var commands --->
  10862. //FIXME: some other functions are using this private function
  10863. void setd_sub(struct script_state *st, TBL_PC *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10864. {
  10865. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10866. return;
  10867. }
  10868. BUILDIN_FUNC(setd)
  10869. {
  10870. TBL_PC *sd=NULL;
  10871. char varname[100];
  10872. const char *value, *buffer;
  10873. int elem;
  10874. buffer = script_getstr(st, 2);
  10875. value = script_getstr(st, 3);
  10876. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10877. elem = 0;
  10878. if(st->rid)
  10879. sd = script_rid2sd(st);
  10880. if(varname[strlen(varname)-1] != '$') {
  10881. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10882. } else {
  10883. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10884. }
  10885. return 0;
  10886. }
  10887. BUILDIN_FUNC(query_sql)
  10888. {
  10889. #ifndef TXT_ONLY
  10890. int i, j;
  10891. TBL_PC* sd = NULL;
  10892. const char* query;
  10893. struct script_data* data;
  10894. char* name;
  10895. int max_rows = 128;// maximum number of rows
  10896. int num_vars;
  10897. int num_cols;
  10898. // check target variables
  10899. for( i = 3; script_hasdata(st,i); ++i )
  10900. {
  10901. data = script_getdata(st, i);
  10902. if( data_isreference(data) && reference_tovariable(data) )
  10903. {// it's a variable
  10904. name = reference_getname(data);
  10905. if( not_server_variable(*name) && sd == NULL )
  10906. {// requires a player
  10907. sd = script_rid2sd(st);
  10908. if( sd == NULL )
  10909. {// no player attached
  10910. script_reportdata(data);
  10911. st->state = END;
  10912. return 1;
  10913. }
  10914. }
  10915. if( not_array_variable(*name) )
  10916. max_rows = 1;// not an array, limit to one row
  10917. }
  10918. else
  10919. {
  10920. ShowError("script:query_sql: not a variable\n");
  10921. script_reportdata(data);
  10922. st->state = END;
  10923. return 1;
  10924. }
  10925. }
  10926. num_vars = i - 3;
  10927. // Execute the query
  10928. query = script_getstr(st,2);
  10929. if( SQL_ERROR == Sql_Query(mmysql_handle, query) )
  10930. {
  10931. Sql_ShowDebug(mmysql_handle);
  10932. script_pushint(st, 0);
  10933. return 1;
  10934. }
  10935. if( Sql_NumRows(mmysql_handle) == 0 )
  10936. {// No data received
  10937. Sql_FreeResult(mmysql_handle);
  10938. script_pushint(st, 0);
  10939. return 0;
  10940. }
  10941. // Count the number of columns to store
  10942. num_cols = Sql_NumColumns(mmysql_handle);
  10943. if( num_vars < num_cols )
  10944. {
  10945. ShowWarning("script:query_sql: Too many columns, discarting last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10946. script_reportsrc(st);
  10947. }
  10948. else if( num_vars > num_cols )
  10949. {
  10950. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10951. script_reportsrc(st);
  10952. }
  10953. // Store data
  10954. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(mmysql_handle); ++i )
  10955. {
  10956. for( j = 0; j < num_vars; ++j )
  10957. {
  10958. char* str = NULL;
  10959. if( j < num_cols )
  10960. Sql_GetData(mmysql_handle, j, &str, NULL);
  10961. data = script_getdata(st, j+3);
  10962. name = reference_getname(data);
  10963. if( is_string_variable(name) )
  10964. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10965. else
  10966. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10967. }
  10968. }
  10969. if( i == max_rows && max_rows < Sql_NumRows(mmysql_handle) )
  10970. {
  10971. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(mmysql_handle));
  10972. script_reportsrc(st);
  10973. }
  10974. // Free data
  10975. Sql_FreeResult(mmysql_handle);
  10976. script_pushint(st, i);
  10977. #else
  10978. //for TXT version, we always return -1
  10979. script_pushint(st,-1);
  10980. #endif
  10981. return 0;
  10982. }
  10983. //Allows escaping of a given string.
  10984. BUILDIN_FUNC(escape_sql)
  10985. {
  10986. const char *str;
  10987. char *esc_str;
  10988. size_t len;
  10989. str = script_getstr(st,2);
  10990. len = strlen(str);
  10991. esc_str = aMallocA(len*2+1);
  10992. #if defined(TXT_ONLY)
  10993. jstrescapecpy(esc_str, str);
  10994. #else
  10995. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10996. #endif
  10997. script_pushstr(st, esc_str);
  10998. return 0;
  10999. }
  11000. BUILDIN_FUNC(getd)
  11001. {
  11002. char varname[100];
  11003. const char *buffer;
  11004. //struct script_data dat;
  11005. int elem;
  11006. buffer = script_getstr(st, 2);
  11007. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  11008. elem = 0;
  11009. // Push the 'pointer' so it's more flexible [Lance]
  11010. push_val(st->stack,C_NAME,
  11011. (elem<<24) | add_str(varname));
  11012. return 0;
  11013. }
  11014. // <--- [zBuffer] List of dynamic var commands
  11015. // Pet stat [Lance]
  11016. BUILDIN_FUNC(petstat)
  11017. {
  11018. TBL_PC *sd = NULL;
  11019. struct pet_data *pd;
  11020. int flag = script_getnum(st,2);
  11021. sd = script_rid2sd(st);
  11022. if(!sd || !sd->status.pet_id || !sd->pd){
  11023. if(flag == 2)
  11024. script_pushconststr(st, "");
  11025. else
  11026. script_pushint(st,0);
  11027. return 0;
  11028. }
  11029. pd = sd->pd;
  11030. switch(flag){
  11031. case 1: script_pushint(st,(int)pd->pet.class_); break;
  11032. case 2: script_pushstr(st, aStrdup(pd->pet.name)); break;
  11033. case 3: script_pushint(st,(int)pd->pet.level); break;
  11034. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  11035. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  11036. default:
  11037. script_pushint(st,0);
  11038. break;
  11039. }
  11040. return 0;
  11041. }
  11042. BUILDIN_FUNC(callshop)
  11043. {
  11044. TBL_PC *sd = NULL;
  11045. struct npc_data *nd;
  11046. const char *shopname;
  11047. int flag = 0;
  11048. sd = script_rid2sd(st);
  11049. if (!sd) {
  11050. script_pushint(st,0);
  11051. return 0;
  11052. }
  11053. shopname = script_getstr(st, 2);
  11054. if( script_hasdata(st,3) )
  11055. flag = script_getnum(st,3);
  11056. nd = npc_name2id(shopname);
  11057. if (!nd || nd->bl.type!=BL_NPC || nd->bl.subtype!=SHOP) {
  11058. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)", shopname);
  11059. script_pushint(st,0);
  11060. return 1;
  11061. }
  11062. switch (flag) {
  11063. case 1: //Buy window
  11064. npc_buysellsel(sd,nd->bl.id,0);
  11065. break;
  11066. case 2: //Sell window
  11067. npc_buysellsel(sd,nd->bl.id,1);
  11068. break;
  11069. default: //Show menu
  11070. clif_npcbuysell(sd,nd->bl.id);
  11071. break;
  11072. }
  11073. sd->npc_shopid = nd->bl.id;
  11074. script_pushint(st,1);
  11075. return 0;
  11076. }
  11077. #ifndef MAX_SHOPITEM
  11078. #define MAX_SHOPITEM 100
  11079. #endif
  11080. BUILDIN_FUNC(npcshopitem)
  11081. {
  11082. struct npc_data *nd= NULL;
  11083. int n = 0;
  11084. int i = 3;
  11085. int amount;
  11086. const char* npcname = script_getstr(st, 2);
  11087. nd = npc_name2id(npcname);
  11088. if(!nd || nd->bl.subtype!=SHOP)
  11089. { //Not found.
  11090. script_pushint(st,0);
  11091. return 0;
  11092. }
  11093. // st->end - 2 = nameid + value # ... / 2 = number of items ... + 1 to end it
  11094. amount = ((st->end-2)/2)+1;
  11095. //Reinsert as realloc could change the pointer address.
  11096. map_deliddb(&nd->bl);
  11097. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  11098. sizeof(nd->u.shop_item[0]) * amount);
  11099. map_addiddb(&nd->bl);
  11100. // Reset sell list.
  11101. memset(nd->u.shop_item, 0, sizeof(nd->u.shop_item[0]) * amount);
  11102. n = 0;
  11103. while (script_hasdata(st,i)) {
  11104. nd->u.shop_item[n].nameid = script_getnum(st,i);
  11105. i++;
  11106. nd->u.shop_item[n].value = script_getnum(st,i);
  11107. i++;
  11108. n++;
  11109. }
  11110. return 0;
  11111. }
  11112. BUILDIN_FUNC(npcshopadditem)
  11113. {
  11114. struct npc_data *nd=NULL;
  11115. int n = 0;
  11116. int i = 3;
  11117. int amount;
  11118. const char* npcname = script_getstr(st, 2);
  11119. nd = npc_name2id(npcname);
  11120. if (!nd || nd->bl.subtype!=SHOP)
  11121. { //Not found.
  11122. script_pushint(st,0);
  11123. return 0;
  11124. }
  11125. amount = ((st->end-2)/2)+1;
  11126. while (nd->u.shop_item[n].nameid && n < MAX_SHOPITEM)
  11127. n++;
  11128. //Reinsert as realloc could change the pointer address.
  11129. map_deliddb(&nd->bl);
  11130. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  11131. sizeof(nd->u.shop_item[0]) * (amount+n));
  11132. map_addiddb(&nd->bl);
  11133. while(script_hasdata(st,i)){
  11134. nd->u.shop_item[n].nameid = script_getnum(st,i);
  11135. i++;
  11136. nd->u.shop_item[n].value = script_getnum(st,i);
  11137. i++;
  11138. n++;
  11139. }
  11140. // Marks the last of our stuff..
  11141. nd->u.shop_item[n].value = 0;
  11142. nd->u.shop_item[n].nameid = 0;
  11143. script_pushint(st,1);
  11144. return 0;
  11145. }
  11146. BUILDIN_FUNC(npcshopdelitem)
  11147. {
  11148. struct npc_data *nd=NULL;
  11149. int n=0;
  11150. int i=3;
  11151. int size = 0;
  11152. const char* npcname = script_getstr(st, 2);
  11153. nd = npc_name2id(npcname);
  11154. if (!nd || nd->bl.subtype!=SHOP)
  11155. { //Not found.
  11156. script_pushint(st,0);
  11157. return 0;
  11158. }
  11159. while (nd->u.shop_item[size].nameid)
  11160. size++;
  11161. while (script_hasdata(st,i)) {
  11162. for(n=0;nd->u.shop_item[n].nameid && n < MAX_SHOPITEM;n++) {
  11163. if (nd->u.shop_item[n].nameid == script_getnum(st,i)) {
  11164. // We're moving 1 extra empty block. Junk data is eliminated later.
  11165. memmove(&nd->u.shop_item[n], &nd->u.shop_item[n+1], sizeof(nd->u.shop_item[0])*(size-n));
  11166. }
  11167. }
  11168. i++;
  11169. }
  11170. size = 0;
  11171. while (nd->u.shop_item[size].nameid)
  11172. size++;
  11173. //Reinsert as realloc could change the pointer address.
  11174. map_deliddb(&nd->bl);
  11175. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  11176. sizeof(nd->u.shop_item[0]) * (size+1));
  11177. map_addiddb(&nd->bl);
  11178. script_pushint(st,1);
  11179. return 0;
  11180. }
  11181. //Sets a script to attach to an npc.
  11182. BUILDIN_FUNC(npcshopattach)
  11183. {
  11184. struct npc_data *nd=NULL;
  11185. const char* npcname = script_getstr(st, 2);
  11186. int flag = 1;
  11187. if( script_hasdata(st,3) )
  11188. flag = script_getnum(st,3);
  11189. nd = npc_name2id(npcname);
  11190. if (!nd || nd->bl.subtype!=SHOP)
  11191. { //Not found.
  11192. script_pushint(st,0);
  11193. return 0;
  11194. }
  11195. if (flag)
  11196. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  11197. else
  11198. nd->master_nd = NULL;
  11199. script_pushint(st,1);
  11200. return 0;
  11201. }
  11202. /*==========================================
  11203. * Returns some values of an item [Lupus]
  11204. * Price, Weight, etc...
  11205. setitemscript(itemID,"{new item bonus script}",[n]);
  11206. Where n:
  11207. 0 - script
  11208. 1 - Equip script
  11209. 2 - Unequip script
  11210. *------------------------------------------*/
  11211. BUILDIN_FUNC(setitemscript)
  11212. {
  11213. int item_id,n=0;
  11214. const char *script;
  11215. struct item_data *i_data;
  11216. struct script_code **dstscript;
  11217. item_id = script_getnum(st,2);
  11218. script = script_getstr(st,3);
  11219. if( script_hasdata(st,4) )
  11220. n=script_getnum(st,4);
  11221. i_data = itemdb_exists(item_id);
  11222. if (!i_data || script==NULL || script[0]!='{') {
  11223. script_pushint(st,0);
  11224. return 0;
  11225. }
  11226. switch (n) {
  11227. case 2:
  11228. dstscript = &i_data->unequip_script;
  11229. break;
  11230. case 1:
  11231. dstscript = &i_data->equip_script;
  11232. break;
  11233. default:
  11234. dstscript = &i_data->script;
  11235. break;
  11236. }
  11237. if(*dstscript)
  11238. script_free_code(*dstscript);
  11239. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11240. script_pushint(st,1);
  11241. return 0;
  11242. }
  11243. /* Work In Progress [Lupus]
  11244. BUILDIN_FUNC(addmonsterdrop)
  11245. {
  11246. int class_,item_id,chance;
  11247. class_=script_getnum(st,2);
  11248. item_id=script_getnum(st,3);
  11249. chance=script_getnum(st,4);
  11250. if(class_>1000 && item_id>500 && chance>0) {
  11251. script_pushint(st,1);
  11252. } else {
  11253. script_pushint(st,0);
  11254. }
  11255. }
  11256. BUILDIN_FUNC(delmonsterdrop)
  11257. {
  11258. int class_,item_id;
  11259. class_=script_getnum(st,2);
  11260. item_id=script_getnum(st,3);
  11261. if(class_>1000 && item_id>500) {
  11262. script_pushint(st,1);
  11263. } else {
  11264. script_pushint(st,0);
  11265. }
  11266. }
  11267. */
  11268. /*==========================================
  11269. * Returns some values of a monster [Lupus]
  11270. * Name, Level, race, size, etc...
  11271. getmonsterinfo(monsterID,queryIndex);
  11272. *------------------------------------------*/
  11273. BUILDIN_FUNC(getmonsterinfo)
  11274. {
  11275. struct mob_db *mob;
  11276. int mob_id;
  11277. mob_id = script_getnum(st,2);
  11278. if (!mobdb_checkid(mob_id)) {
  11279. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i", mob_id);
  11280. script_pushint(st,-1);
  11281. return -1;
  11282. }
  11283. mob = mob_db(mob_id);
  11284. switch ( script_getnum(st,3) ) {
  11285. case 0: script_pushconststr(st,mob->jname); break;
  11286. case 1: script_pushint(st,mob->lv); break;
  11287. case 2: script_pushint(st,mob->status.max_hp); break;
  11288. case 3: script_pushint(st,mob->base_exp); break;
  11289. case 4: script_pushint(st,mob->job_exp); break;
  11290. case 5: script_pushint(st,mob->status.rhw.atk); break;
  11291. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  11292. case 7: script_pushint(st,mob->status.def); break;
  11293. case 8: script_pushint(st,mob->status.mdef); break;
  11294. case 9: script_pushint(st,mob->status.str); break;
  11295. case 10: script_pushint(st,mob->status.agi); break;
  11296. case 11: script_pushint(st,mob->status.vit); break;
  11297. case 12: script_pushint(st,mob->status.int_); break;
  11298. case 13: script_pushint(st,mob->status.dex); break;
  11299. case 14: script_pushint(st,mob->status.luk); break;
  11300. case 15: script_pushint(st,mob->status.rhw.range); break;
  11301. case 16: script_pushint(st,mob->range2); break;
  11302. case 17: script_pushint(st,mob->range3); break;
  11303. case 18: script_pushint(st,mob->status.size); break;
  11304. case 19: script_pushint(st,mob->status.race); break;
  11305. case 20: script_pushint(st,mob->status.def_ele); break;
  11306. case 21: script_pushint(st,mob->status.mode); break;
  11307. default: script_pushint(st,-1); //wrong Index
  11308. }
  11309. return 0;
  11310. }
  11311. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11312. {
  11313. TBL_PC *sd = NULL;
  11314. if(script_hasdata(st,2))
  11315. sd = map_nick2sd(script_getstr(st,2));
  11316. else
  11317. sd = script_rid2sd(st);
  11318. if(sd)
  11319. script_pushint(st,(sd->vender_id != 0));
  11320. else
  11321. script_pushint(st,0);
  11322. return 0;
  11323. }
  11324. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11325. {
  11326. TBL_PC *sd = NULL;
  11327. if(script_hasdata(st,2))
  11328. sd = map_nick2sd(script_getstr(st,2));
  11329. else
  11330. sd = script_rid2sd(st);
  11331. if(sd)
  11332. script_pushint(st,(sd->chatID != 0));
  11333. else
  11334. script_pushint(st,0);
  11335. return 0;
  11336. }
  11337. int axtoi(const char *hexStg)
  11338. {
  11339. int n = 0; // position in string
  11340. int m = 0; // position in digit[] to shift
  11341. int count; // loop index
  11342. int intValue = 0; // integer value of hex string
  11343. int digit[11]; // hold values to convert
  11344. while (n < 10) {
  11345. if (hexStg[n]=='\0')
  11346. break;
  11347. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11348. digit[n] = hexStg[n] & 0x0f; //convert to int
  11349. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11350. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11351. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11352. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11353. else break;
  11354. n++;
  11355. }
  11356. count = n;
  11357. m = n - 1;
  11358. n = 0;
  11359. while(n < count) {
  11360. // digit[n] is value of hex digit at position n
  11361. // (m << 2) is the number of positions to shift
  11362. // OR the bits into return value
  11363. intValue = intValue | (digit[n] << (m << 2));
  11364. m--; // adjust the position to set
  11365. n++; // next digit to process
  11366. }
  11367. return (intValue);
  11368. }
  11369. // [Lance] Hex string to integer converter
  11370. BUILDIN_FUNC(axtoi)
  11371. {
  11372. const char *hex = script_getstr(st,2);
  11373. script_pushint(st,axtoi(hex));
  11374. return 0;
  11375. }
  11376. // [zBuffer] List of player cont commands --->
  11377. BUILDIN_FUNC(rid2name)
  11378. {
  11379. struct block_list *bl = NULL;
  11380. int rid = script_getnum(st,2);
  11381. if((bl = map_id2bl(rid)))
  11382. {
  11383. switch(bl->type) {
  11384. case BL_MOB: script_pushconststr(st,((TBL_MOB*)bl)->name); break;
  11385. case BL_PC: script_pushconststr(st,((TBL_PC*)bl)->status.name); break;
  11386. case BL_NPC: script_pushconststr(st,((TBL_NPC*)bl)->exname); break;
  11387. case BL_PET: script_pushconststr(st,((TBL_PET*)bl)->pet.name); break;
  11388. case BL_HOM: script_pushconststr(st,((TBL_HOM*)bl)->homunculus.name); break;
  11389. default:
  11390. ShowError("buildin_rid2name: BL type unknown.\n");
  11391. script_pushconststr(st,"");
  11392. break;
  11393. }
  11394. } else {
  11395. ShowError("buildin_rid2name: invalid RID\n");
  11396. script_pushconststr(st,"(null)");
  11397. }
  11398. return 0;
  11399. }
  11400. BUILDIN_FUNC(pcblockmove)
  11401. {
  11402. int id, flag;
  11403. TBL_PC *sd = NULL;
  11404. id = script_getnum(st,2);
  11405. flag = script_getnum(st,3);
  11406. if(id)
  11407. sd = map_id2sd(id);
  11408. else
  11409. sd = script_rid2sd(st);
  11410. if(sd)
  11411. sd->state.blockedmove = flag > 0;
  11412. return 0;
  11413. }
  11414. BUILDIN_FUNC(pcfollow)
  11415. {
  11416. int id, targetid;
  11417. TBL_PC *sd = NULL;
  11418. id = script_getnum(st,2);
  11419. targetid = script_getnum(st,3);
  11420. if(id)
  11421. sd = map_id2sd(id);
  11422. else
  11423. sd = script_rid2sd(st);
  11424. if(sd)
  11425. pc_follow(sd, targetid);
  11426. return 0;
  11427. }
  11428. BUILDIN_FUNC(pcstopfollow)
  11429. {
  11430. int id;
  11431. TBL_PC *sd = NULL;
  11432. id = script_getnum(st,2);
  11433. if(id)
  11434. sd = map_id2sd(id);
  11435. else
  11436. sd = script_rid2sd(st);
  11437. if(sd)
  11438. pc_stop_following(sd);
  11439. return 0;
  11440. }
  11441. // <--- [zBuffer] List of player cont commands
  11442. // [zBuffer] List of mob control commands --->
  11443. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11444. BUILDIN_FUNC(mobspawn)
  11445. {
  11446. int class_,x,y,id;
  11447. const char *str,*map;
  11448. // Who?
  11449. str =script_getstr(st,2);
  11450. // What?
  11451. class_ =script_getnum(st,3);
  11452. // Where?
  11453. map =script_getstr(st,4);
  11454. x =script_getnum(st,5);
  11455. y =script_getnum(st,6);
  11456. id = mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,1,"");
  11457. script_pushint(st,id);
  11458. return 0;
  11459. }
  11460. BUILDIN_FUNC(mobremove)
  11461. {
  11462. int id;
  11463. struct block_list *bl = NULL;
  11464. id = script_getnum(st,2);
  11465. bl = map_id2bl(id);
  11466. if (bl && bl->type == BL_MOB)
  11467. unit_free(bl,0);
  11468. return 0;
  11469. }
  11470. BUILDIN_FUNC(getmobdata)
  11471. {
  11472. int num, id;
  11473. char *name;
  11474. struct mob_data *md = NULL;
  11475. TBL_PC *sd = st->rid?map_id2sd(st->rid):NULL;
  11476. id = script_getnum(st,2);
  11477. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  11478. ShowWarning("buildin_getmobdata: Error in argument!\n");
  11479. return -1;
  11480. }
  11481. num=st->stack->stack_data[st->start+3].u.num;
  11482. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  11483. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  11484. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  11485. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  11486. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  11487. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  11488. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  11489. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  11490. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  11491. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  11492. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  11493. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  11494. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  11495. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  11496. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  11497. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  11498. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  11499. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  11500. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  11501. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  11502. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  11503. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  11504. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  11505. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  11506. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  11507. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  11508. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  11509. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  11510. return 0;
  11511. }
  11512. /// Changes the data of a mob
  11513. ///
  11514. /// setmobdata <mob unit id>,<type>,<value>;
  11515. BUILDIN_FUNC(setmobdata)
  11516. {
  11517. struct block_list* mob_bl;
  11518. mob_bl = map_id2bl(script_getnum(st,2));
  11519. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11520. {
  11521. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11522. int type;
  11523. int value;
  11524. type = script_getnum(st,3);
  11525. value = script_getnum(st,4);
  11526. switch( type )
  11527. {
  11528. case 0: md->class_ = (short)value; break;
  11529. case 1: md->level = (unsigned short)value; break;
  11530. case 2: md->status.hp = (unsigned int)value; break;
  11531. case 3: md->status.max_hp = (unsigned int)value; break;
  11532. case 4: md->master_id = value; break;
  11533. case 5: md->bl.m = (short)value; break;
  11534. case 6: md->bl.x = (short)value; break;
  11535. case 7: md->bl.y = (short)value; break;
  11536. case 8: md->status.speed = (unsigned short)value; break;
  11537. case 9: md->status.mode = (unsigned short)value; break;
  11538. case 10: md->special_state.ai = (unsigned int)value; break;
  11539. case 11: md->sc.option = (unsigned short)value; break;
  11540. case 12: md->vd->sex = (char)value; break;
  11541. case 13: md->vd->class_ = (unsigned short)value; break;
  11542. case 14: md->vd->hair_style = (unsigned short)value; break;
  11543. case 15: md->vd->hair_color = (unsigned short)value; break;
  11544. case 16: md->vd->head_bottom = (unsigned short)value; break;
  11545. case 17: md->vd->head_mid = (unsigned short)value; break;
  11546. case 18: md->vd->head_top = (unsigned short)value; break;
  11547. case 19: md->vd->cloth_color = (unsigned short)value; break;
  11548. case 20: md->vd->shield = (unsigned short)value; break;
  11549. case 21: md->vd->weapon = (unsigned short)value; break;
  11550. case 22: md->vd->shield = (unsigned short)value; break;
  11551. case 23: md->ud.dir = (unsigned char)value; break;
  11552. case 24: md->state.killer = value > 0 ? 1 : 0; break;
  11553. case 25: md->callback_flag = (short)value; break;
  11554. case 26: md->state.no_random_walk = value > 0 ? 1 : 0; break;
  11555. default:
  11556. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  11557. return 1;
  11558. }
  11559. }
  11560. return 0;
  11561. }
  11562. /// Makes the mob assist the target unit as a slave
  11563. ///
  11564. /// mobassist <mob unit id>,"<player name>";
  11565. /// mobassist <mob unit id>,<target id>;
  11566. BUILDIN_FUNC(mobassist)
  11567. {
  11568. struct block_list* mob_bl;
  11569. // get mob
  11570. mob_bl = map_id2bl(script_getnum(st,2));
  11571. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11572. {
  11573. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11574. struct block_list* target_bl = NULL;
  11575. struct script_data* data;
  11576. // get target
  11577. data = script_getdata(st, 3);
  11578. get_val(st, data);
  11579. if( data_isstring(data) )
  11580. {
  11581. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11582. if( sd != NULL )
  11583. target_bl = &sd->bl;
  11584. }
  11585. if( target_bl == NULL )
  11586. target_bl = map_id2bl(conv_num(st, data));
  11587. // set mob as slave
  11588. if( target_bl != NULL )
  11589. {
  11590. struct unit_data* ud;
  11591. md->master_id = target_bl->id;
  11592. md->state.killer = 1;
  11593. mob_convertslave(md);
  11594. ud = unit_bl2ud(mob_bl);
  11595. if( ud != NULL )
  11596. {
  11597. if( ud->target != 0 )
  11598. md->target_id = ud->target;
  11599. else if( ud->skilltarget != 0 )
  11600. md->target_id = ud->skilltarget;
  11601. if( md->target_id != 0 )
  11602. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  11603. }
  11604. }
  11605. }
  11606. return 0;
  11607. }
  11608. /// Attaches the current npc or the target npc to the mob unit
  11609. ///
  11610. /// mobattach <mob unit id>{,"<npc name>"};
  11611. BUILDIN_FUNC(mobattach)
  11612. {
  11613. struct block_list* mob_bl;
  11614. mob_bl = map_id2bl(script_getnum(st,2));
  11615. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11616. {
  11617. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11618. TBL_NPC* nd = NULL;
  11619. if( script_hasdata(st,3) )
  11620. nd = npc_name2id(script_getstr(st, 3));
  11621. else
  11622. {
  11623. struct block_list* npc_bl = map_id2bl(st->oid);
  11624. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  11625. nd = (TBL_NPC*)npc_bl;
  11626. }
  11627. if( nd != NULL )
  11628. md->nd = nd;
  11629. }
  11630. return 0;
  11631. }
  11632. /// Makes the unit walk to target position or map
  11633. /// Returns if it was successfull
  11634. ///
  11635. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11636. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11637. BUILDIN_FUNC(unitwalk)
  11638. {
  11639. struct block_list* bl;
  11640. bl = map_id2bl(script_getnum(st,2));
  11641. if( bl == NULL )
  11642. {
  11643. script_pushint(st, 0);
  11644. }
  11645. else if( script_hasdata(st,4) )
  11646. {
  11647. int x = script_getnum(st,3);
  11648. int y = script_getnum(st,4);
  11649. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11650. }
  11651. else
  11652. {
  11653. int map_id = script_getnum(st,3);
  11654. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11655. }
  11656. return 0;
  11657. }
  11658. /// Kills the unit
  11659. ///
  11660. /// unitkill <unit_id>;
  11661. BUILDIN_FUNC(unitkill)
  11662. {
  11663. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11664. if( bl != NULL )
  11665. status_kill(bl);
  11666. return 0;
  11667. }
  11668. /// Warps the unit to the target position in the target map
  11669. /// Returns if it was successfull
  11670. ///
  11671. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11672. BUILDIN_FUNC(unitwarp)
  11673. {
  11674. int unit_id;
  11675. int map;
  11676. short x;
  11677. short y;
  11678. struct block_list* bl;
  11679. unit_id = script_getnum(st,2);
  11680. map = map_mapname2mapid(script_getstr(st, 3));
  11681. x = (short)script_getnum(st,4);
  11682. y = (short)script_getnum(st,5);
  11683. bl = map_id2bl(unit_id);
  11684. if( map >= 0 && bl != NULL )
  11685. script_pushint(st, unit_warp(bl,map,x,y,0));
  11686. else
  11687. script_pushint(st, 0);
  11688. return 0;
  11689. }
  11690. /// Makes the unit attack the target.
  11691. /// If the unit is a player and <action type> is not 0, it does a continuous
  11692. /// attack instead of a single attack.
  11693. /// Returns if the request was successfull.
  11694. ///
  11695. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11696. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11697. BUILDIN_FUNC(unitattack)
  11698. {
  11699. struct block_list* unit_bl;
  11700. struct block_list* target_bl = NULL;
  11701. struct script_data* data;
  11702. int actiontype = 0;
  11703. // get unit
  11704. unit_bl = map_id2bl(script_getnum(st,2));
  11705. if( unit_bl == NULL ) {
  11706. script_pushint(st, 0);
  11707. return 0;
  11708. }
  11709. data = script_getdata(st, 3);
  11710. get_val(st, data);
  11711. if( data_isstring(data) )
  11712. {
  11713. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11714. if( sd != NULL )
  11715. target_bl = &sd->bl;
  11716. } else
  11717. target_bl = map_id2bl(conv_num(st, data));
  11718. // request the attack
  11719. if( target_bl == NULL )
  11720. {
  11721. script_pushint(st, 0);
  11722. return 0;
  11723. }
  11724. // get actiontype
  11725. if( script_hasdata(st,4) )
  11726. actiontype = script_getnum(st,4);
  11727. switch( unit_bl->type )
  11728. {
  11729. case BL_PC:
  11730. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11731. script_pushint(st, 1);
  11732. return 0;
  11733. case BL_MOB:
  11734. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11735. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11736. break;
  11737. case BL_PET:
  11738. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11739. break;
  11740. default:
  11741. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11742. script_pushint(st, 0);
  11743. return 1;
  11744. }
  11745. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11746. return 0;
  11747. }
  11748. /// Makes the unit stop attacking and moving
  11749. ///
  11750. /// unitstop <unit_id>;
  11751. BUILDIN_FUNC(unitstop)
  11752. {
  11753. int unit_id;
  11754. struct block_list* bl;
  11755. unit_id = script_getnum(st,2);
  11756. bl = map_id2bl(unit_id);
  11757. if( bl != NULL )
  11758. {
  11759. unit_stop_attack(bl);
  11760. unit_stop_walking(bl,0);
  11761. if( bl->type == BL_MOB )
  11762. ((TBL_MOB*)bl)->target_id = 0;
  11763. }
  11764. return 0;
  11765. }
  11766. /// Makes the unit say the message
  11767. ///
  11768. /// unittalk <unit_id>,"<message>";
  11769. BUILDIN_FUNC(unittalk)
  11770. {
  11771. int unit_id;
  11772. const char* message;
  11773. struct block_list* bl;
  11774. unit_id = script_getnum(st,2);
  11775. message = script_getstr(st, 3);
  11776. bl = map_id2bl(unit_id);
  11777. if( bl != NULL )
  11778. {
  11779. struct StringBuf sbuf;
  11780. StringBuf_Init(&sbuf);
  11781. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11782. clif_message(bl, StringBuf_Value(&sbuf));
  11783. if( bl->type == BL_PC )
  11784. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11785. StringBuf_Destroy(&sbuf);
  11786. }
  11787. return 0;
  11788. }
  11789. /// Makes the unit do an emotion
  11790. ///
  11791. /// unitemote <unit_id>,<emotion>;
  11792. ///
  11793. /// @see e_* in const.txt
  11794. BUILDIN_FUNC(unitemote)
  11795. {
  11796. int unit_id;
  11797. int emotion;
  11798. struct block_list* bl;
  11799. unit_id = script_getnum(st,2);
  11800. emotion = script_getnum(st,3);
  11801. bl = map_id2bl(unit_id);
  11802. if( bl != NULL )
  11803. clif_emotion(bl, emotion);
  11804. return 0;
  11805. }
  11806. /// Makes the unit cast the skill on the target or self if no target is specified
  11807. ///
  11808. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11809. BUILDIN_FUNC(unitskilluseid)
  11810. {
  11811. int unit_id;
  11812. int skill_id;
  11813. int skill_lv;
  11814. int target_id;
  11815. struct block_list* bl;
  11816. unit_id = script_getnum(st,2);
  11817. skill_id = script_getnum(st,3);
  11818. skill_lv = script_getnum(st,4);
  11819. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11820. bl = map_id2bl(unit_id);
  11821. if( bl != NULL )
  11822. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11823. return 0;
  11824. }
  11825. /// Makes the unit cast the skill on the target position.
  11826. ///
  11827. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11828. BUILDIN_FUNC(unitskillusepos)
  11829. {
  11830. int unit_id;
  11831. int skill_id;
  11832. int skill_lv;
  11833. int skill_x;
  11834. int skill_y;
  11835. struct block_list* bl;
  11836. unit_id = script_getnum(st,2);
  11837. skill_id = script_getnum(st,3);
  11838. skill_lv = script_getnum(st,4);
  11839. skill_x = script_getnum(st,5);
  11840. skill_y = script_getnum(st,6);
  11841. bl = map_id2bl(unit_id);
  11842. if( bl != NULL )
  11843. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11844. return 0;
  11845. }
  11846. // <--- [zBuffer] List of mob control commands
  11847. /// Pauses the execution of the script, detaching the player
  11848. ///
  11849. /// sleep <mili seconds>;
  11850. BUILDIN_FUNC(sleep)
  11851. {
  11852. int ticks;
  11853. TBL_PC* sd;
  11854. ticks = script_getnum(st,2);
  11855. sd = map_id2sd(st->rid);
  11856. // detach the player
  11857. if( sd && sd->npc_id == st->oid )
  11858. {
  11859. sd->npc_id = 0;
  11860. }
  11861. st->rid = 0;
  11862. if( ticks <= 0 )
  11863. {// do nothing
  11864. }
  11865. else if( st->sleep.tick == 0 )
  11866. {// sleep for the target amount of time
  11867. st->state = RERUNLINE;
  11868. st->sleep.tick = ticks;
  11869. }
  11870. else
  11871. {// sleep time is over
  11872. st->sleep.tick = 0;
  11873. }
  11874. return 0;
  11875. }
  11876. /// Pauses the execution of the script, keeping the player attached
  11877. /// Returns if a player is still attached
  11878. ///
  11879. /// sleep2(<mili secconds>) -> <bool>
  11880. BUILDIN_FUNC(sleep2)
  11881. {
  11882. int ticks;
  11883. ticks = script_getnum(st,2);
  11884. if( ticks <= 0 )
  11885. {// do nothing
  11886. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11887. }
  11888. else if( !st->sleep.tick )
  11889. {// sleep for the target amount of time
  11890. st->state = RERUNLINE;
  11891. st->sleep.tick = ticks;
  11892. }
  11893. else
  11894. {// sleep time is over
  11895. st->sleep.tick = 0;
  11896. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11897. }
  11898. return 0;
  11899. }
  11900. /// Awakes all the sleep timers of the target npc
  11901. ///
  11902. /// awake "<npc name>";
  11903. BUILDIN_FUNC(awake)
  11904. {
  11905. struct npc_data* nd;
  11906. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11907. nd = npc_name2id(script_getstr(st, 2));
  11908. if( nd == NULL )
  11909. return 0;
  11910. while( node )
  11911. {
  11912. if( (int)node->key == nd->bl.id )
  11913. {// sleep timer for the npc
  11914. struct script_state* tst = (struct script_state*)node->data;
  11915. TBL_PC* sd = map_id2sd(tst->rid);
  11916. if( tst->sleep.timer == -1 )
  11917. {// already awake ???
  11918. node = node->next;
  11919. continue;
  11920. }
  11921. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11922. {// char not online anymore / another char of the same account is online - Cancel execution
  11923. tst->state = END;
  11924. tst->rid = 0;
  11925. }
  11926. delete_timer(tst->sleep.timer, run_script_timer);
  11927. node = script_erase_sleepdb(node);
  11928. tst->sleep.timer = -1;
  11929. tst->sleep.tick = 0;
  11930. run_script_main(tst);
  11931. }
  11932. else
  11933. {
  11934. node = node->next;
  11935. }
  11936. }
  11937. return 0;
  11938. }
  11939. /// Returns a reference to a variable of the target NPC.
  11940. /// Returns 0 if an error occurs.
  11941. ///
  11942. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11943. BUILDIN_FUNC(getvariableofnpc)
  11944. {
  11945. struct script_data* data;
  11946. char* name;
  11947. struct npc_data* nd;
  11948. data = script_getdata(st,2);
  11949. if( !data_isreference(data) )
  11950. {// Not a reference (aka varaible name)
  11951. ShowError("script:getvariableofnpc: not a variable\n");
  11952. script_reportdata(data);
  11953. script_pushnil(st);
  11954. st->state = END;
  11955. return 1;
  11956. }
  11957. name = reference_getname(data);
  11958. if( *name != '.' || name[1] == '@' )
  11959. {// not a npc variable
  11960. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11961. script_reportdata(data);
  11962. script_pushnil(st);
  11963. st->state = END;
  11964. return 1;
  11965. }
  11966. nd = npc_name2id(script_getstr(st,3));
  11967. if( nd == NULL || nd->bl.subtype != SCRIPT || nd->u.scr.script == NULL )
  11968. {// NPC not found or has no script
  11969. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11970. script_pushnil(st);
  11971. st->state = END;
  11972. return 1;
  11973. }
  11974. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11975. return 0;
  11976. }
  11977. /// Opens a warp portal.
  11978. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11979. ///
  11980. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11981. ///
  11982. /// @author blackhole89
  11983. BUILDIN_FUNC(warpportal)
  11984. {
  11985. int spx;
  11986. int spy;
  11987. unsigned short mapindex;
  11988. int tpx;
  11989. int tpy;
  11990. struct skill_unit_group* group;
  11991. struct block_list* bl;
  11992. bl = map_id2bl(st->oid);
  11993. if( bl == NULL )
  11994. {
  11995. ShowError("script:warpportal: npc is needed\n");
  11996. return 1;
  11997. }
  11998. spx = script_getnum(st,2);
  11999. spy = script_getnum(st,3);
  12000. mapindex = mapindex_name2id(script_getstr(st, 4));
  12001. tpx = script_getnum(st,5);
  12002. tpy = script_getnum(st,6);
  12003. if( mapindex == 0 )
  12004. return 0;// map not found
  12005. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 1);
  12006. if( group == NULL )
  12007. return 0;// failed
  12008. group->val2 = (tpx<<16) | tpy;
  12009. group->val3 = mapindex;
  12010. return 0;
  12011. }
  12012. #ifndef TXT_ONLY
  12013. BUILDIN_FUNC(openmail)
  12014. {
  12015. mail_openmail(script_rid2sd(st));
  12016. return 0;
  12017. }
  12018. #endif