script.c 323 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338
  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 "path.h"
  18. #include "clif.h"
  19. #include "chrif.h"
  20. #include "itemdb.h"
  21. #include "pc.h"
  22. #include "status.h"
  23. #include "storage.h"
  24. #include "mob.h"
  25. #include "npc.h"
  26. #include "pet.h"
  27. #include "mercenary.h" //[orn]
  28. #include "intif.h"
  29. #include "skill.h"
  30. #include "chat.h"
  31. #include "battle.h"
  32. #include "party.h"
  33. #include "guild.h"
  34. #include "atcommand.h"
  35. #include "charcommand.h"
  36. #include "log.h"
  37. #include "unit.h"
  38. #include "irc.h"
  39. #include "pet.h"
  40. #include "mail.h"
  41. #include "script.h"
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <math.h>
  46. #ifndef WIN32
  47. #include <sys/time.h>
  48. #endif
  49. #include <time.h>
  50. #include <setjmp.h>
  51. #include <errno.h>
  52. ///////////////////////////////////////////////////////////////////////////////
  53. //## TODO possible enhancements: [FlavioJS]
  54. // - 'callfunc' supporting labels in the current npc "::LabelName"
  55. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  56. // - 'function FuncName;' function declarations reverting to global functions
  57. // if local label isn't found
  58. // - join callfunc and callsub's functionality
  59. //
  60. // struct script_state* st;
  61. //
  62. /// Returns the script_data at the target index
  63. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  64. /// Returns if the stack contains data at the target index
  65. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  66. /// Returns the index of the last data in the stack
  67. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  68. /// Pushes an int into the stack
  69. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  70. /// Pushes a string into the stack (script engine frees it automatically)
  71. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  72. /// Pushes a copy of a string into the stack
  73. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  74. /// Pushes a constant string into the stack (must never change or be freed)
  75. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  76. /// Pushes a nil into the stack
  77. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  78. /// Pushes a copy of the data in the target index
  79. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  80. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  81. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  82. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  83. // Note: "top" functions/defines use indexes relative to the top of the stack
  84. // -1 is the index of the data at the top
  85. /// Returns the script_data at the target index relative to the top of the stack
  86. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  87. /// Pushes a copy of the data in the target index relative to the top of the stack
  88. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  89. /// Removes the range of values [start,end[ relative to the top of the stack
  90. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  91. //
  92. // struct script_data* data;
  93. //
  94. /// Returns if the script data is a string
  95. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  96. /// Returns if the script data is an int
  97. #define data_isint(data) ( (data)->type == C_INT )
  98. /// Returns if the script data is a reference
  99. #define data_isreference(data) ( (data)->type == C_NAME )
  100. /// Returns if the script data is a label
  101. #define data_islabel(data) ( (data)->type == C_POS )
  102. /// Returns if the script data is an internal script function label
  103. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  104. /// Returns if this is a reference to a constant
  105. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  106. /// Returns if this a reference to a param
  107. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  108. /// Returns if this a reference to a variable
  109. //##TODO confirm it's C_NAME [FlavioJS]
  110. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  111. /// Returns the unique id of the reference (id and index)
  112. #define reference_getuid(data) ( (data)->u.num )
  113. /// Returns the id of the reference
  114. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  115. /// Returns the array index of the reference
  116. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  117. /// Returns the name of the reference
  118. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  119. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  120. #define reference_getref(data) ( (data)->ref )
  121. /// Returns the value of the constant
  122. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  123. /// Returns the type of param
  124. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  125. /// Composes the uid of a reference from the id and the index
  126. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  127. #define FETCH(n, t) \
  128. if( script_hasdata(st,n) ) \
  129. (t)=script_getnum(st,n);
  130. #define SCRIPT_BLOCK_SIZE 512
  131. enum { LABEL_NEXTLINE=1,LABEL_START };
  132. static unsigned char * script_buf = NULL;
  133. static int script_pos,script_size;
  134. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  135. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  136. #define GETSTRING(off) (str_buf+(off))
  137. static char *str_buf;
  138. static int str_pos,str_size;
  139. static struct str_data_struct {
  140. enum c_op type;
  141. int str;
  142. int backpatch;
  143. int label;
  144. int (*func)(struct script_state *st);
  145. int val;
  146. int next;
  147. } *str_data = NULL;
  148. int str_num=LABEL_START,str_data_size;
  149. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  150. #define SCRIPT_HASH_SIZE 1021
  151. int str_hash[SCRIPT_HASH_SIZE];
  152. //#define SCRIPT_HASH_DJB2
  153. #define SCRIPT_HASH_SDBM
  154. //#define SCRIPT_HASH_ELF
  155. //#define SCRIPT_HASH_PJW
  156. static DBMap* mapreg_db=NULL; // int var_id -> int value
  157. static DBMap* mapregstr_db=NULL; // int var_id -> char* value
  158. static int mapreg_dirty=-1;
  159. char mapreg_txt[256]="save/mapreg.txt";
  160. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  161. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  162. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  163. static int parse_options=0;
  164. DBMap* script_get_label_db(){ return scriptlabel_db; }
  165. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  166. struct Script_Config script_config = {
  167. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  168. "OnPCDieEvent", //die_event_name
  169. "OnPCKillEvent", //kill_pc_event_name
  170. "OnNPCKillEvent", //kill_mob_event_name
  171. "OnPCLoginEvent", //login_event_name
  172. "OnPCLogoutEvent", //logout_event_name
  173. "OnPCLoadMapEvent", //loadmap_event_name
  174. "OnPCBaseLvUpEvent", //baselvup_event_name
  175. "OnPCJobLvUpEvent" //joblvup_event_name
  176. };
  177. static jmp_buf error_jump;
  178. static char* error_msg;
  179. static const char* error_pos;
  180. static int error_report; // if the error should produce output
  181. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  182. // [Eoe / jA 1080, 1081, 1094, 1164]
  183. enum curly_type {
  184. TYPE_NULL = 0,
  185. TYPE_IF,
  186. TYPE_SWITCH,
  187. TYPE_WHILE,
  188. TYPE_FOR,
  189. TYPE_DO,
  190. TYPE_USERFUNC,
  191. TYPE_ARGLIST // function argument list
  192. };
  193. #define ARGLIST_UNDEFINED 0
  194. #define ARGLIST_NO_PAREN 1
  195. #define ARGLIST_PAREN 2
  196. static struct {
  197. struct {
  198. enum curly_type type;
  199. int index;
  200. int count;
  201. int flag;
  202. struct linkdb_node *case_label;
  203. } curly[256]; // 右カッコの情報
  204. int curly_count; // 右カッコの数
  205. int index; // スクリプト内で使用した構文の数
  206. } syntax;
  207. const char* parse_curly_close(const char* p);
  208. const char* parse_syntax_close(const char* p);
  209. const char* parse_syntax_close_sub(const char* p,int* flag);
  210. const char* parse_syntax(const char* p);
  211. static int parse_syntax_for_flag = 0;
  212. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  213. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  214. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  215. int potion_target=0;
  216. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  217. char mapregsql_db[32] = "mapreg";
  218. char mapregsql_db_varname[32] = "varname";
  219. char mapregsql_db_index[32] = "index";
  220. char mapregsql_db_value[32] = "value";
  221. #endif
  222. int get_com(unsigned char *script,int *pos);
  223. int get_num(unsigned char *script,int *pos);
  224. struct script_function {
  225. int (*func)(struct script_state *st);
  226. const char *name;
  227. const char *arg;
  228. } buildin_func[];
  229. static struct linkdb_node *sleep_db;
  230. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  231. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  232. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  233. /*==========================================
  234. * ローカルプロトタイプ宣言 (必要な物のみ)
  235. *------------------------------------------*/
  236. const char* parse_subexpr(const char* p,int limit);
  237. void push_val(struct script_stack *stack,int type,int val);
  238. int run_func(struct script_state *st);
  239. int mapreg_setreg(int num,int val);
  240. int mapreg_setregstr(int num,const char *str);
  241. enum {
  242. MF_NOMEMO, //0
  243. MF_NOTELEPORT,
  244. MF_NOSAVE,
  245. MF_NOBRANCH,
  246. MF_NOPENALTY,
  247. MF_NOZENYPENALTY,
  248. MF_PVP,
  249. MF_PVP_NOPARTY,
  250. MF_PVP_NOGUILD,
  251. MF_GVG,
  252. MF_GVG_NOPARTY, //10
  253. MF_NOTRADE,
  254. MF_NOSKILL,
  255. MF_NOWARP,
  256. MF_PARTYLOCK,
  257. MF_NOICEWALL,
  258. MF_SNOW,
  259. MF_FOG,
  260. MF_SAKURA,
  261. MF_LEAVES,
  262. MF_RAIN, //20
  263. MF_INDOORS,
  264. MF_NOGO,
  265. MF_CLOUDS,
  266. MF_CLOUDS2,
  267. MF_FIREWORKS,
  268. MF_GVG_CASTLE,
  269. MF_GVG_DUNGEON,
  270. MF_NIGHTENABLED,
  271. MF_NOBASEEXP,
  272. MF_NOJOBEXP, //30
  273. MF_NOMOBLOOT,
  274. MF_NOMVPLOOT,
  275. MF_NORETURN,
  276. MF_NOWARPTO,
  277. MF_NIGHTMAREDROP,
  278. MF_RESTRICTED,
  279. MF_NOCOMMAND,
  280. MF_NODROP,
  281. MF_JEXP,
  282. MF_BEXP, //40
  283. MF_NOVENDING,
  284. MF_LOADEVENT,
  285. MF_NOCHAT,
  286. MF_NOEXPPENALTY,
  287. MF_GUILDLOCK
  288. };
  289. const char* script_op2name(int op)
  290. {
  291. #define RETURN_OP_NAME(type) case type: return #type
  292. switch( op )
  293. {
  294. RETURN_OP_NAME(C_NOP);
  295. RETURN_OP_NAME(C_POS);
  296. RETURN_OP_NAME(C_INT);
  297. RETURN_OP_NAME(C_PARAM);
  298. RETURN_OP_NAME(C_FUNC);
  299. RETURN_OP_NAME(C_STR);
  300. RETURN_OP_NAME(C_CONSTSTR);
  301. RETURN_OP_NAME(C_ARG);
  302. RETURN_OP_NAME(C_NAME);
  303. RETURN_OP_NAME(C_EOL);
  304. RETURN_OP_NAME(C_RETINFO);
  305. RETURN_OP_NAME(C_USERFUNC);
  306. RETURN_OP_NAME(C_USERFUNC_POS);
  307. // operators
  308. RETURN_OP_NAME(C_OP3);
  309. RETURN_OP_NAME(C_LOR);
  310. RETURN_OP_NAME(C_LAND);
  311. RETURN_OP_NAME(C_LE);
  312. RETURN_OP_NAME(C_LT);
  313. RETURN_OP_NAME(C_GE);
  314. RETURN_OP_NAME(C_GT);
  315. RETURN_OP_NAME(C_EQ);
  316. RETURN_OP_NAME(C_NE);
  317. RETURN_OP_NAME(C_XOR);
  318. RETURN_OP_NAME(C_OR);
  319. RETURN_OP_NAME(C_AND);
  320. RETURN_OP_NAME(C_ADD);
  321. RETURN_OP_NAME(C_SUB);
  322. RETURN_OP_NAME(C_MUL);
  323. RETURN_OP_NAME(C_DIV);
  324. RETURN_OP_NAME(C_MOD);
  325. RETURN_OP_NAME(C_NEG);
  326. RETURN_OP_NAME(C_LNOT);
  327. RETURN_OP_NAME(C_NOT);
  328. RETURN_OP_NAME(C_R_SHIFT);
  329. RETURN_OP_NAME(C_L_SHIFT);
  330. default:
  331. ShowDebug("script_op2name: unexpected op=%d\n", op);
  332. return "???";
  333. }
  334. #undef RETURN_OP_NAME
  335. }
  336. /// Reports on the console the src of a script error.
  337. static void script_reportsrc(struct script_state *st)
  338. {
  339. struct block_list* bl;
  340. if( st->oid == 0 )
  341. return; //Can't report source.
  342. bl = map_id2bl(st->oid);
  343. if( bl == NULL )
  344. return;
  345. switch( bl->type )
  346. {
  347. case BL_NPC:
  348. if( bl->m >= 0 )
  349. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  350. else
  351. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  352. break;
  353. default:
  354. if( bl->m >= 0 )
  355. 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);
  356. else
  357. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  358. break;
  359. }
  360. }
  361. /// Reports on the console information about the script data.
  362. static void script_reportdata(struct script_data* data)
  363. {
  364. if( data == NULL )
  365. return;
  366. switch( data->type )
  367. {
  368. case C_NOP:// no value
  369. ShowDebug("Data: nothing (nil)\n");
  370. break;
  371. case C_INT:// number
  372. ShowDebug("Data: number value=%d\n", data->u.num);
  373. break;
  374. case C_STR:
  375. case C_CONSTSTR:// string
  376. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  377. break;
  378. case C_NAME:// reference
  379. if( reference_tovariable(data) )
  380. {// variable
  381. char* name = reference_getname(data);
  382. if( not_array_variable(*name) )
  383. ShowDebug("Data: variable name='%s'\n", name);
  384. else
  385. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  386. }
  387. else if( reference_toconstant(data) )
  388. {// constant
  389. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  390. }
  391. else if( reference_toparam(data) )
  392. {// param
  393. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  394. }
  395. else
  396. {// ???
  397. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  398. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  399. }
  400. break;
  401. case C_POS:// label
  402. ShowDebug("Data: label pos=%d\n", data->u.num);
  403. break;
  404. default:
  405. ShowDebug("Data: %s\n", script_op2name(data->type));
  406. break;
  407. }
  408. }
  409. /*==========================================
  410. * エラーメッセージ出力
  411. *------------------------------------------*/
  412. static void disp_error_message2(const char *mes,const char *pos,int report)
  413. {
  414. error_msg = aStrdup(mes);
  415. error_pos = pos;
  416. error_report = report;
  417. longjmp( error_jump, 1 );
  418. }
  419. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  420. /// Checks event parameter validity
  421. static void check_event(struct script_state *st, const char *evt)
  422. {
  423. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  424. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  425. script_reportsrc(st);
  426. }
  427. }
  428. /*==========================================
  429. * 文字列のハッシュを計算
  430. *------------------------------------------*/
  431. #define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE)
  432. static unsigned int calc_hash2(const char* p)
  433. {
  434. #if defined(SCRIPT_HASH_DJB2)
  435. unsigned int h = 5381;
  436. while( *p ) // hash*33 + c
  437. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  438. return h;
  439. #elif defined(SCRIPT_HASH_SDBM)
  440. unsigned int h = 0;
  441. while( *p )
  442. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  443. return h;
  444. #elif defined(SCRIPT_HASH_ELF)
  445. unsigned int h = 0;
  446. unsigned int g;
  447. while( *p ){ // UNIX ELF hash
  448. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  449. g = h & 0xF0000000;
  450. if( g )
  451. h ^= g >> 24;
  452. h &= ~g;
  453. }
  454. return h;
  455. #elif defined(SCRIPT_HASH_PJW)
  456. unsigned int h = 0;
  457. unsigned int g;
  458. while( *p ){
  459. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  460. g = h & 0xF0000000;
  461. if( g )
  462. {
  463. h ^= g>>24;
  464. h ^= g;
  465. }
  466. }
  467. return h;
  468. #else
  469. unsigned int h = 0;
  470. while( *p ){
  471. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  472. h += (unsigned char)TOLOWER(*p++);
  473. }
  474. return h;
  475. #endif
  476. }
  477. /*==========================================
  478. * str_dataの中に名前があるか検索する
  479. *------------------------------------------*/
  480. // 既存のであれば番号、無ければ-1
  481. static int search_str(const char *p)
  482. {
  483. int i;
  484. i=str_hash[calc_hash(p)];
  485. while(i){
  486. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  487. return i;
  488. }
  489. i=str_data[i].next;
  490. }
  491. return -1;
  492. }
  493. /*==========================================
  494. * str_dataに名前を登録
  495. *------------------------------------------*/
  496. // 既存のであれば番号、無ければ登録して新規番号
  497. int add_str(const char* p)
  498. {
  499. int i;
  500. int len;
  501. i=calc_hash(p);
  502. if(str_hash[i]==0){
  503. str_hash[i]=str_num;
  504. } else {
  505. i=str_hash[i];
  506. for(;;){
  507. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  508. return i;
  509. }
  510. if(str_data[i].next==0)
  511. break;
  512. i=str_data[i].next;
  513. }
  514. str_data[i].next=str_num;
  515. }
  516. if(str_num>=str_data_size){
  517. str_data_size+=128;
  518. RECREATE(str_data,struct str_data_struct,str_data_size);
  519. memset(str_data + (str_data_size - 128), '\0', 128);
  520. }
  521. len=(int)strlen(p);
  522. while(str_pos+len+1>=str_size){
  523. str_size+=256;
  524. RECREATE(str_buf,char,str_size);
  525. memset(str_buf + (str_size - 256), '\0', 256);
  526. }
  527. memcpy(str_buf+str_pos,p,len+1);
  528. str_data[str_num].type=C_NOP;
  529. str_data[str_num].str=str_pos;
  530. str_data[str_num].next=0;
  531. str_data[str_num].func=NULL;
  532. str_data[str_num].backpatch=-1;
  533. str_data[str_num].label=-1;
  534. str_pos+=len+1;
  535. return str_num++;
  536. }
  537. /*==========================================
  538. * スクリプトバッファサイズの確認と拡張
  539. *------------------------------------------*/
  540. static void expand_script_buf(void)
  541. {
  542. script_size+=SCRIPT_BLOCK_SIZE;
  543. RECREATE(script_buf,unsigned char,script_size);
  544. }
  545. /*==========================================
  546. * スクリプトバッファに1バイト書き込む
  547. *------------------------------------------*/
  548. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  549. #if 0
  550. static void add_scriptb(int a)
  551. {
  552. expand_script_buf();
  553. script_buf[script_pos++]=a;
  554. }
  555. #endif
  556. /*==========================================
  557. * スクリプトバッファにデータタイプを書き込む
  558. *------------------------------------------*/
  559. static void add_scriptc(int a)
  560. {
  561. while(a>=0x40){
  562. add_scriptb((a&0x3f)|0x40);
  563. a=(a-0x40)>>6;
  564. }
  565. add_scriptb(a&0x3f);
  566. }
  567. /*==========================================
  568. * スクリプトバッファに整数を書き込む
  569. *------------------------------------------*/
  570. static void add_scripti(int a)
  571. {
  572. while(a>=0x40){
  573. add_scriptb(a|0xc0);
  574. a=(a-0x40)>>6;
  575. }
  576. add_scriptb(a|0x80);
  577. }
  578. /*==========================================
  579. * スクリプトバッファにラベル/変数/関数を書き込む
  580. *------------------------------------------*/
  581. // 最大16Mまで
  582. static void add_scriptl(int l)
  583. {
  584. int backpatch = str_data[l].backpatch;
  585. switch(str_data[l].type){
  586. case C_POS:
  587. case C_USERFUNC_POS:
  588. add_scriptc(C_POS);
  589. add_scriptb(str_data[l].label);
  590. add_scriptb(str_data[l].label>>8);
  591. add_scriptb(str_data[l].label>>16);
  592. break;
  593. case C_NOP:
  594. case C_USERFUNC:
  595. // ラベルの可能性があるのでbackpatch用データ埋め込み
  596. add_scriptc(C_NAME);
  597. str_data[l].backpatch=script_pos;
  598. add_scriptb(backpatch);
  599. add_scriptb(backpatch>>8);
  600. add_scriptb(backpatch>>16);
  601. break;
  602. case C_INT:
  603. add_scripti(abs(str_data[l].val));
  604. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  605. add_scriptc(C_NEG);
  606. break;
  607. default:
  608. // もう他の用途と確定してるので数字をそのまま
  609. add_scriptc(C_NAME);
  610. add_scriptb(l);
  611. add_scriptb(l>>8);
  612. add_scriptb(l>>16);
  613. break;
  614. }
  615. }
  616. /*==========================================
  617. * ラベルを解決する
  618. *------------------------------------------*/
  619. void set_label(int l,int pos, const char* script_pos)
  620. {
  621. int i,next;
  622. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  623. { //Prevent overwriting constants values and parameters [Skotlex]
  624. disp_error_message("set_label: invalid label name",script_pos);
  625. return;
  626. }
  627. if(str_data[l].label!=-1){
  628. disp_error_message("set_label: dup label ",script_pos);
  629. return;
  630. }
  631. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  632. str_data[l].label=pos;
  633. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  634. next=GETVALUE(script_buf,i);
  635. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  636. SETVALUE(script_buf,i,pos);
  637. i=next;
  638. }
  639. }
  640. /// Skips spaces and/or comments.
  641. const char* skip_space(const char* p)
  642. {
  643. if( p == NULL )
  644. return NULL;
  645. for(;;)
  646. {
  647. while( ISSPACE(*p) )
  648. ++p;
  649. if( *p == '/' && p[1] == '/' )
  650. {// line comment
  651. while(*p && *p!='\n')
  652. ++p;
  653. }
  654. else if( *p == '/' && p[1] == '*' )
  655. {// block comment
  656. p += 2;
  657. for(;;)
  658. {
  659. if( *p == '\0' )
  660. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  661. if( *p == '*' && p[1] == '/' )
  662. {// end of block comment
  663. p += 2;
  664. break;
  665. }
  666. ++p;
  667. }
  668. }
  669. else
  670. break;
  671. }
  672. return p;
  673. }
  674. /// Skips a word.
  675. /// A word consists of undercores and/or alfanumeric characters,
  676. /// and valid variable prefixes/postfixes.
  677. static
  678. const char* skip_word(const char* p)
  679. {
  680. // prefix
  681. switch( *p )
  682. {
  683. case '@':// temporary char variable
  684. ++p; break;
  685. case '#':// account variable
  686. p += ( p[1] == '#' ? 2 : 1 ); break;
  687. case '.':// npc variable
  688. p += ( p[1] == '@' ? 2 : 1 ); break;
  689. case '$':// global variable
  690. p += ( p[1] == '@' ? 2 : 1 ); break;
  691. }
  692. while( ISALNUM(*p) || *p == '_' )
  693. ++p;
  694. // postfix
  695. if( *p == '$' )// string
  696. p++;
  697. return p;
  698. }
  699. /// Adds a word to str_data.
  700. /// @see skip_word
  701. /// @see add_str
  702. static
  703. int add_word(const char* p)
  704. {
  705. char* word;
  706. int len;
  707. int i;
  708. // Check for a word
  709. len = skip_word(p) - p;
  710. if( len == 0 )
  711. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  712. // Duplicate the word
  713. CREATE(word, char, len+1);
  714. memcpy(word, p, len);
  715. word[len] = 0;
  716. // add the word
  717. i = add_str(word);
  718. aFree(word);
  719. return i;
  720. }
  721. /// Parses a function call.
  722. /// The argument list can have parenthesis or not.
  723. /// The number of arguments is checked.
  724. static
  725. const char* parse_callfunc(const char* p, int require_paren)
  726. {
  727. const char* p2;
  728. const char* arg=NULL;
  729. int func;
  730. func = add_word(p);
  731. if( str_data[func].type == C_FUNC ){
  732. // buildin function
  733. add_scriptl(func);
  734. add_scriptc(C_ARG);
  735. arg = buildin_func[str_data[func].val].arg;
  736. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  737. // script defined function
  738. int callsub = search_str("callsub");
  739. add_scriptl(callsub);
  740. add_scriptc(C_ARG);
  741. add_scriptl(func);
  742. arg = buildin_func[str_data[callsub].val].arg;
  743. if( *arg == 0 )
  744. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  745. if( *arg != '*' )
  746. ++arg; // count func as argument
  747. } else
  748. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  749. p = skip_word(p);
  750. p = skip_space(p);
  751. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  752. syntax.curly[syntax.curly_count].count = 0;
  753. if( *p == ';' )
  754. {// <func name> ';'
  755. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  756. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  757. {// <func name> '(' ')'
  758. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  759. p = p2;
  760. /*
  761. } else if( 0 && require_paren && *p != '(' )
  762. {// <func name>
  763. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  764. */
  765. } else
  766. {// <func name> <arg list>
  767. if( require_paren ){
  768. if( *p != '(' )
  769. disp_error_message("need '('",p);
  770. ++p; // skip '('
  771. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  772. } else if( *p == '(' ){
  773. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  774. } else {
  775. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  776. }
  777. ++syntax.curly_count;
  778. while( *arg ) {
  779. p2=parse_subexpr(p,-1);
  780. if( p == p2 )
  781. break; // not an argument
  782. if( *arg != '*' )
  783. ++arg; // next argument
  784. p=skip_space(p2);
  785. if( *arg == 0 || *p != ',' )
  786. break; // no more arguments
  787. ++p; // skip comma
  788. }
  789. --syntax.curly_count;
  790. }
  791. if( *arg && *arg != '?' && *arg != '*' )
  792. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  793. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  794. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  795. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  796. if( *p != ')' )
  797. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  798. ++p;
  799. }
  800. add_scriptc(C_FUNC);
  801. return p;
  802. }
  803. /*==========================================
  804. * 項の解析
  805. *------------------------------------------*/
  806. const char* parse_simpleexpr(const char *p)
  807. {
  808. int i;
  809. p=skip_space(p);
  810. #ifdef DEBUG_FUNCIN
  811. if(battle_config.etc_log)
  812. ShowDebug("parse_simpleexpr %s\n",p);
  813. #endif
  814. if(*p==';' || *p==',')
  815. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  816. if(*p=='('){
  817. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  818. ++syntax.curly[i].count;
  819. p=parse_subexpr(p+1,-1);
  820. p=skip_space(p);
  821. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  822. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  823. ){
  824. if( *p == ',' ){
  825. syntax.curly[i].flag = ARGLIST_PAREN;
  826. return p;
  827. } else
  828. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  829. }
  830. if( *p != ')' )
  831. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  832. ++p;
  833. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  834. char *np;
  835. i=strtoul(p,&np,0);
  836. add_scripti(i);
  837. p=np;
  838. } else if(*p=='"'){
  839. add_scriptc(C_STR);
  840. p++;
  841. while( *p && *p != '"' ){
  842. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  843. p++;
  844. else if( *p == '\n' )
  845. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  846. add_scriptb(*p++);
  847. }
  848. if(!*p)
  849. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  850. add_scriptb(0);
  851. p++; //'"'
  852. } else {
  853. int l;
  854. // label , register , function etc
  855. if(skip_word(p)==p)
  856. disp_error_message("parse_simpleexpr: unexpected character",p);
  857. l=add_word(p);
  858. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  859. return parse_callfunc(p,1);
  860. p=skip_word(p);
  861. if( *p == '[' ){
  862. // array(name[i] => getelementofarray(name,i) )
  863. add_scriptl(search_str("getelementofarray"));
  864. add_scriptc(C_ARG);
  865. add_scriptl(l);
  866. p=parse_subexpr(p+1,-1);
  867. p=skip_space(p);
  868. if( *p != ']' )
  869. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  870. ++p;
  871. add_scriptc(C_FUNC);
  872. }else
  873. add_scriptl(l);
  874. }
  875. #ifdef DEBUG_FUNCIN
  876. if(battle_config.etc_log)
  877. ShowDebug("parse_simpleexpr end %s\n",p);
  878. #endif
  879. return p;
  880. }
  881. /*==========================================
  882. * 式の解析
  883. *------------------------------------------*/
  884. const char* parse_subexpr(const char* p,int limit)
  885. {
  886. int op,opl,len;
  887. const char* tmpp;
  888. #ifdef DEBUG_FUNCIN
  889. if(battle_config.etc_log)
  890. ShowDebug("parse_subexpr %s\n",p);
  891. #endif
  892. p=skip_space(p);
  893. if(*p=='-'){
  894. tmpp=skip_space(p+1);
  895. if(*tmpp==';' || *tmpp==','){
  896. add_scriptl(LABEL_NEXTLINE);
  897. p++;
  898. return p;
  899. }
  900. }
  901. tmpp=p;
  902. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  903. p=parse_subexpr(p+1,10);
  904. add_scriptc(op);
  905. } else
  906. p=parse_simpleexpr(p);
  907. p=skip_space(p);
  908. while((
  909. (op=C_OP3,opl=0,len=1,*p=='?') ||
  910. (op=C_ADD,opl=8,len=1,*p=='+') ||
  911. (op=C_SUB,opl=8,len=1,*p=='-') ||
  912. (op=C_MUL,opl=9,len=1,*p=='*') ||
  913. (op=C_DIV,opl=9,len=1,*p=='/') ||
  914. (op=C_MOD,opl=9,len=1,*p=='%') ||
  915. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  916. (op=C_AND,opl=6,len=1,*p=='&') ||
  917. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  918. (op=C_OR,opl=5,len=1,*p=='|') ||
  919. (op=C_XOR,opl=4,len=1,*p=='^') ||
  920. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  921. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  922. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  923. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  924. (op=C_GT,opl=3,len=1,*p=='>') ||
  925. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  926. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  927. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  928. p+=len;
  929. if(op == C_OP3) {
  930. p=parse_subexpr(p,-1);
  931. p=skip_space(p);
  932. if( *(p++) != ':')
  933. disp_error_message("parse_subexpr: need ':'", p-1);
  934. p=parse_subexpr(p,-1);
  935. } else {
  936. p=parse_subexpr(p,opl);
  937. }
  938. add_scriptc(op);
  939. p=skip_space(p);
  940. }
  941. #ifdef DEBUG_FUNCIN
  942. if(battle_config.etc_log)
  943. ShowDebug("parse_subexpr end %s\n",p);
  944. #endif
  945. return p; /* return first untreated operator */
  946. }
  947. /*==========================================
  948. * 式の評価
  949. *------------------------------------------*/
  950. const char* parse_expr(const char *p)
  951. {
  952. #ifdef DEBUG_FUNCIN
  953. if(battle_config.etc_log)
  954. ShowDebug("parse_expr %s\n",p);
  955. #endif
  956. switch(*p){
  957. case ')': case ';': case ':': case '[': case ']':
  958. case '}':
  959. disp_error_message("parse_expr: unexpected char",p);
  960. }
  961. p=parse_subexpr(p,-1);
  962. #ifdef DEBUG_FUNCIN
  963. if(battle_config.etc_log)
  964. ShowDebug("parse_expr end %s\n",p);
  965. #endif
  966. return p;
  967. }
  968. /*==========================================
  969. * 行の解析
  970. *------------------------------------------*/
  971. const char* parse_line(const char* p)
  972. {
  973. const char* p2;
  974. p=skip_space(p);
  975. if(*p==';') {
  976. // if(); for(); while(); のために閉じ判定
  977. p = parse_syntax_close(p + 1);
  978. return p;
  979. }
  980. if(*p==')' && parse_syntax_for_flag)
  981. return p+1;
  982. p = skip_space(p);
  983. if(p[0] == '{') {
  984. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  985. syntax.curly[syntax.curly_count].count = -1;
  986. syntax.curly[syntax.curly_count].index = -1;
  987. syntax.curly_count++;
  988. return p + 1;
  989. } else if(p[0] == '}') {
  990. return parse_curly_close(p);
  991. }
  992. // 構文関連の処理
  993. p2 = parse_syntax(p);
  994. if(p2 != NULL)
  995. return p2;
  996. p = parse_callfunc(p,0);
  997. p = skip_space(p);
  998. if(parse_syntax_for_flag) {
  999. if( *p != ')' )
  1000. disp_error_message("parse_line: need ')'",p);
  1001. } else {
  1002. if( *p != ';' )
  1003. disp_error_message("parse_line: need ';'",p);
  1004. }
  1005. // if, for , while の閉じ判定
  1006. p = parse_syntax_close(p+1);
  1007. return p;
  1008. }
  1009. // { ... } の閉じ処理
  1010. const char* parse_curly_close(const char* p)
  1011. {
  1012. if(syntax.curly_count <= 0) {
  1013. disp_error_message("parse_curly_close: unexpected string",p);
  1014. return p + 1;
  1015. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1016. syntax.curly_count--;
  1017. // if, for , while の閉じ判定
  1018. p = parse_syntax_close(p + 1);
  1019. return p;
  1020. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1021. // switch() 閉じ判定
  1022. int pos = syntax.curly_count-1;
  1023. char label[256];
  1024. int l;
  1025. // 一時変数を消す
  1026. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1027. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1028. parse_line(label);
  1029. syntax.curly_count--;
  1030. // 無条件で終了ポインタに移動
  1031. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1032. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1033. parse_line(label);
  1034. syntax.curly_count--;
  1035. // 現在地のラベルを付ける
  1036. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1037. l=add_str(label);
  1038. set_label(l,script_pos, p);
  1039. if(syntax.curly[pos].flag) {
  1040. // default が存在する
  1041. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1042. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1043. parse_line(label);
  1044. syntax.curly_count--;
  1045. }
  1046. // 終了ラベルを付ける
  1047. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1048. l=add_str(label);
  1049. set_label(l,script_pos, p);
  1050. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1051. syntax.curly_count--;
  1052. return p+1;
  1053. } else {
  1054. disp_error_message("parse_curly_close: unexpected string",p);
  1055. return p + 1;
  1056. }
  1057. }
  1058. // 構文関連の処理
  1059. // break, case, continue, default, do, for, function,
  1060. // if, switch, while をこの内部で処理します。
  1061. const char* parse_syntax(const char* p)
  1062. {
  1063. const char *p2 = skip_word(p);
  1064. switch(*p) {
  1065. case 'B':
  1066. case 'b':
  1067. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1068. // break の処理
  1069. char label[256];
  1070. int pos = syntax.curly_count - 1;
  1071. while(pos >= 0) {
  1072. if(syntax.curly[pos].type == TYPE_DO) {
  1073. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1074. break;
  1075. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1076. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1077. break;
  1078. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1079. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1080. break;
  1081. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1082. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1083. break;
  1084. }
  1085. pos--;
  1086. }
  1087. if(pos < 0) {
  1088. disp_error_message("parse_syntax: unexpected 'break'",p);
  1089. } else {
  1090. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1091. parse_line(label);
  1092. syntax.curly_count--;
  1093. }
  1094. p = skip_space(p2);
  1095. if(*p != ';')
  1096. disp_error_message("parse_syntax: need ';'",p);
  1097. // if, for , while の閉じ判定
  1098. p = parse_syntax_close(p + 1);
  1099. return p;
  1100. }
  1101. break;
  1102. case 'c':
  1103. case 'C':
  1104. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1105. // case の処理
  1106. int pos = syntax.curly_count-1;
  1107. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1108. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1109. return p+1;
  1110. } else {
  1111. char label[256];
  1112. int l,v;
  1113. char *np;
  1114. if(syntax.curly[pos].count != 1) {
  1115. // FALLTHRU 用のジャンプ
  1116. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1117. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1118. parse_line(label);
  1119. syntax.curly_count--;
  1120. // 現在地のラベルを付ける
  1121. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1122. l=add_str(label);
  1123. set_label(l,script_pos, p);
  1124. }
  1125. // switch 判定文
  1126. p = skip_space(p2);
  1127. if(p == p2) {
  1128. disp_error_message("parse_syntax: expect space ' '",p);
  1129. }
  1130. // check whether case label is integer or not
  1131. v = strtol(p,&np,0);
  1132. if(np == p) { //Check for constants
  1133. p2 = skip_word(p);
  1134. v = p2-p; // length of word at p2
  1135. memcpy(label,p,v);
  1136. label[v]='\0';
  1137. v = search_str(label);
  1138. if (v < 0 || str_data[v].type != C_INT)
  1139. disp_error_message("parse_syntax: 'case' label not integer",p);
  1140. v = str_data[v].val;
  1141. p = skip_word(p);
  1142. } else { //Numeric value
  1143. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1144. p++;
  1145. p = skip_word(p);
  1146. if(np != p)
  1147. disp_error_message("parse_syntax: 'case' label not integer",np);
  1148. }
  1149. p = skip_space(p);
  1150. if(*p != ':')
  1151. disp_error_message("parse_syntax: expect ':'",p);
  1152. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1153. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1154. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1155. // 2回parse しないとダメ
  1156. p2 = parse_line(label);
  1157. parse_line(p2);
  1158. syntax.curly_count--;
  1159. if(syntax.curly[pos].count != 1) {
  1160. // FALLTHRU 終了後のラベル
  1161. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1162. l=add_str(label);
  1163. set_label(l,script_pos,p);
  1164. }
  1165. // check duplication of case label [Rayce]
  1166. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1167. disp_error_message("parse_syntax: dup 'case'",p);
  1168. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1169. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1170. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1171. parse_line(label);
  1172. syntax.curly_count--;
  1173. syntax.curly[pos].count++;
  1174. }
  1175. return p + 1;
  1176. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1177. // continue の処理
  1178. char label[256];
  1179. int pos = syntax.curly_count - 1;
  1180. while(pos >= 0) {
  1181. if(syntax.curly[pos].type == TYPE_DO) {
  1182. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1183. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1184. break;
  1185. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1186. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1187. break;
  1188. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1189. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1190. break;
  1191. }
  1192. pos--;
  1193. }
  1194. if(pos < 0) {
  1195. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1196. } else {
  1197. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1198. parse_line(label);
  1199. syntax.curly_count--;
  1200. }
  1201. p = skip_space(p2);
  1202. if(*p != ';')
  1203. disp_error_message("parse_syntax: need ';'",p);
  1204. // if, for , while の閉じ判定
  1205. p = parse_syntax_close(p + 1);
  1206. return p;
  1207. }
  1208. break;
  1209. case 'd':
  1210. case 'D':
  1211. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1212. // switch - default の処理
  1213. int pos = syntax.curly_count-1;
  1214. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1215. disp_error_message("parse_syntax: unexpected 'default'",p);
  1216. } else if(syntax.curly[pos].flag) {
  1217. disp_error_message("parse_syntax: dup 'default'",p);
  1218. } else {
  1219. char label[256];
  1220. int l;
  1221. // 現在地のラベルを付ける
  1222. p = skip_space(p2);
  1223. if(*p != ':') {
  1224. disp_error_message("parse_syntax: need ':'",p);
  1225. }
  1226. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1227. l=add_str(label);
  1228. set_label(l,script_pos,p);
  1229. // 無条件で次のリンクに飛ばす
  1230. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1231. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1232. parse_line(label);
  1233. syntax.curly_count--;
  1234. // default のラベルを付ける
  1235. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1236. l=add_str(label);
  1237. set_label(l,script_pos,p);
  1238. syntax.curly[syntax.curly_count - 1].flag = 1;
  1239. syntax.curly[pos].count++;
  1240. }
  1241. return p + 1;
  1242. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1243. int l;
  1244. char label[256];
  1245. p=skip_space(p2);
  1246. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1247. syntax.curly[syntax.curly_count].count = 1;
  1248. syntax.curly[syntax.curly_count].index = syntax.index++;
  1249. syntax.curly[syntax.curly_count].flag = 0;
  1250. // 現在地のラベル形成する
  1251. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1252. l=add_str(label);
  1253. set_label(l,script_pos,p);
  1254. syntax.curly_count++;
  1255. return p;
  1256. }
  1257. break;
  1258. case 'f':
  1259. case 'F':
  1260. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1261. int l;
  1262. char label[256];
  1263. int pos = syntax.curly_count;
  1264. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1265. syntax.curly[syntax.curly_count].count = 1;
  1266. syntax.curly[syntax.curly_count].index = syntax.index++;
  1267. syntax.curly[syntax.curly_count].flag = 0;
  1268. syntax.curly_count++;
  1269. p=skip_space(p2);
  1270. if(*p != '(')
  1271. disp_error_message("parse_syntax: need '('",p);
  1272. p++;
  1273. // 初期化文を実行する
  1274. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1275. p=parse_line(p);
  1276. syntax.curly_count--;
  1277. // 条件判断開始のラベル形成する
  1278. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1279. l=add_str(label);
  1280. set_label(l,script_pos,p);
  1281. p=skip_space(p);
  1282. if(*p == ';') {
  1283. // for(;;) のパターンなので必ず真
  1284. ;
  1285. } else {
  1286. // 条件が偽なら終了地点に飛ばす
  1287. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1288. add_scriptl(add_str("jump_zero"));
  1289. add_scriptc(C_ARG);
  1290. p=parse_expr(p);
  1291. p=skip_space(p);
  1292. add_scriptl(add_str(label));
  1293. add_scriptc(C_FUNC);
  1294. }
  1295. if(*p != ';')
  1296. disp_error_message("parse_syntax: need ';'",p);
  1297. p++;
  1298. // ループ開始に飛ばす
  1299. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1300. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1301. parse_line(label);
  1302. syntax.curly_count--;
  1303. // 次のループへのラベル形成する
  1304. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1305. l=add_str(label);
  1306. set_label(l,script_pos,p);
  1307. // 次のループに入る時の処理
  1308. // for 最後の ')' を ';' として扱うフラグ
  1309. parse_syntax_for_flag = 1;
  1310. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1311. p=parse_line(p);
  1312. syntax.curly_count--;
  1313. parse_syntax_for_flag = 0;
  1314. // 条件判定処理に飛ばす
  1315. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1316. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1317. parse_line(label);
  1318. syntax.curly_count--;
  1319. // ループ開始のラベル付け
  1320. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1321. l=add_str(label);
  1322. set_label(l,script_pos,p);
  1323. return p;
  1324. }
  1325. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1326. {// internal script function
  1327. const char *func_name;
  1328. func_name = skip_space(p2);
  1329. p = skip_word(func_name);
  1330. if( p == func_name )
  1331. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1332. p2 = skip_space(p);
  1333. if( *p2 == ';' )
  1334. {// function <name> ;
  1335. // function declaration - just register the name
  1336. int l;
  1337. l = add_word(func_name);
  1338. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1339. str_data[l].type = C_USERFUNC;
  1340. // if, for , while の閉じ判定
  1341. p = parse_syntax_close(p2 + 1);
  1342. return p; }
  1343. else if(*p2 == '{')
  1344. {// function <name> <line/block of code>
  1345. char label[256];
  1346. int l;
  1347. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1348. syntax.curly[syntax.curly_count].count = 1;
  1349. syntax.curly[syntax.curly_count].index = syntax.index++;
  1350. syntax.curly[syntax.curly_count].flag = 0;
  1351. ++syntax.curly_count;
  1352. // Jump over the function code
  1353. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1354. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1355. ++syntax.curly_count;
  1356. parse_line(label);
  1357. --syntax.curly_count;
  1358. // Set the position of the function (label)
  1359. l=add_word(func_name);
  1360. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1361. str_data[l].type = C_USERFUNC;
  1362. set_label(l, script_pos, p);
  1363. if( parse_options&SCRIPT_USE_LABEL_DB )
  1364. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1365. return skip_space(p);
  1366. }
  1367. else
  1368. {
  1369. disp_error_message("expect ';' or '{' at function syntax",p);
  1370. }
  1371. }
  1372. break;
  1373. case 'i':
  1374. case 'I':
  1375. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1376. // if() の処理
  1377. char label[256];
  1378. p=skip_space(p2);
  1379. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1380. disp_error_message("need '('",p);
  1381. }
  1382. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1383. syntax.curly[syntax.curly_count].count = 1;
  1384. syntax.curly[syntax.curly_count].index = syntax.index++;
  1385. syntax.curly[syntax.curly_count].flag = 0;
  1386. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1387. syntax.curly_count++;
  1388. add_scriptl(add_str("jump_zero"));
  1389. add_scriptc(C_ARG);
  1390. p=parse_expr(p);
  1391. p=skip_space(p);
  1392. add_scriptl(add_str(label));
  1393. add_scriptc(C_FUNC);
  1394. return p;
  1395. }
  1396. break;
  1397. case 's':
  1398. case 'S':
  1399. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1400. // switch() の処理
  1401. char label[256];
  1402. p=skip_space(p2);
  1403. if(*p != '(') {
  1404. disp_error_message("need '('",p);
  1405. }
  1406. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1407. syntax.curly[syntax.curly_count].count = 1;
  1408. syntax.curly[syntax.curly_count].index = syntax.index++;
  1409. syntax.curly[syntax.curly_count].flag = 0;
  1410. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1411. syntax.curly_count++;
  1412. add_scriptl(add_str("set"));
  1413. add_scriptc(C_ARG);
  1414. add_scriptl(add_str(label));
  1415. p=parse_expr(p);
  1416. p=skip_space(p);
  1417. if(*p != '{') {
  1418. disp_error_message("parse_syntax: need '{'",p);
  1419. }
  1420. add_scriptc(C_FUNC);
  1421. return p + 1;
  1422. }
  1423. break;
  1424. case 'w':
  1425. case 'W':
  1426. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1427. int l;
  1428. char label[256];
  1429. p=skip_space(p2);
  1430. if(*p != '(') {
  1431. disp_error_message("need '('",p);
  1432. }
  1433. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1434. syntax.curly[syntax.curly_count].count = 1;
  1435. syntax.curly[syntax.curly_count].index = syntax.index++;
  1436. syntax.curly[syntax.curly_count].flag = 0;
  1437. // 条件判断開始のラベル形成する
  1438. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1439. l=add_str(label);
  1440. set_label(l,script_pos,p);
  1441. // 条件が偽なら終了地点に飛ばす
  1442. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1443. syntax.curly_count++;
  1444. add_scriptl(add_str("jump_zero"));
  1445. add_scriptc(C_ARG);
  1446. p=parse_expr(p);
  1447. p=skip_space(p);
  1448. add_scriptl(add_str(label));
  1449. add_scriptc(C_FUNC);
  1450. return p;
  1451. }
  1452. break;
  1453. }
  1454. return NULL;
  1455. }
  1456. const char* parse_syntax_close(const char *p) {
  1457. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1458. int flag;
  1459. do {
  1460. p = parse_syntax_close_sub(p,&flag);
  1461. } while(flag);
  1462. return p;
  1463. }
  1464. // if, for , while , do の閉じ判定
  1465. // flag == 1 : 閉じられた
  1466. // flag == 0 : 閉じられない
  1467. const char* parse_syntax_close_sub(const char* p,int* flag)
  1468. {
  1469. char label[256];
  1470. int pos = syntax.curly_count - 1;
  1471. int l;
  1472. *flag = 1;
  1473. if(syntax.curly_count <= 0) {
  1474. *flag = 0;
  1475. return p;
  1476. } else if(syntax.curly[pos].type == TYPE_IF) {
  1477. const char *bp = p;
  1478. const char *p2;
  1479. // if 最終場所へ飛ばす
  1480. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1481. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1482. parse_line(label);
  1483. syntax.curly_count--;
  1484. // 現在地のラベルを付ける
  1485. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1486. l=add_str(label);
  1487. set_label(l,script_pos,p);
  1488. syntax.curly[pos].count++;
  1489. p = skip_space(p);
  1490. p2 = skip_word(p);
  1491. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1492. // else or else - if
  1493. p = skip_space(p2);
  1494. p2 = skip_word(p);
  1495. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1496. // else - if
  1497. p=skip_space(p2);
  1498. if(*p != '(') {
  1499. disp_error_message("need '('",p);
  1500. }
  1501. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1502. add_scriptl(add_str("jump_zero"));
  1503. add_scriptc(C_ARG);
  1504. p=parse_expr(p);
  1505. p=skip_space(p);
  1506. add_scriptl(add_str(label));
  1507. add_scriptc(C_FUNC);
  1508. *flag = 0;
  1509. return p;
  1510. } else {
  1511. // else
  1512. if(!syntax.curly[pos].flag) {
  1513. syntax.curly[pos].flag = 1;
  1514. *flag = 0;
  1515. return p;
  1516. }
  1517. }
  1518. }
  1519. // if 閉じ
  1520. syntax.curly_count--;
  1521. // 最終地のラベルを付ける
  1522. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1523. l=add_str(label);
  1524. set_label(l,script_pos,p);
  1525. if(syntax.curly[pos].flag == 1) {
  1526. // このifに対するelseじゃないのでポインタの位置は同じ
  1527. return bp;
  1528. }
  1529. return p;
  1530. } else if(syntax.curly[pos].type == TYPE_DO) {
  1531. int l;
  1532. char label[256];
  1533. const char *p2;
  1534. if(syntax.curly[pos].flag) {
  1535. // 現在地のラベル形成する(continue でここに来る)
  1536. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1537. l=add_str(label);
  1538. set_label(l,script_pos,p);
  1539. }
  1540. // 条件が偽なら終了地点に飛ばす
  1541. p = skip_space(p);
  1542. p2 = skip_word(p);
  1543. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1544. disp_error_message("parse_syntax: need 'while'",p);
  1545. p = skip_space(p2);
  1546. if(*p != '(') {
  1547. disp_error_message("need '('",p);
  1548. }
  1549. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1550. add_scriptl(add_str("jump_zero"));
  1551. add_scriptc(C_ARG);
  1552. p=parse_expr(p);
  1553. p=skip_space(p);
  1554. add_scriptl(add_str(label));
  1555. add_scriptc(C_FUNC);
  1556. // 開始地点に飛ばす
  1557. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1558. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1559. parse_line(label);
  1560. syntax.curly_count--;
  1561. // 条件終了地点のラベル形成する
  1562. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1563. l=add_str(label);
  1564. set_label(l,script_pos,p);
  1565. p = skip_space(p);
  1566. if(*p != ';') {
  1567. disp_error_message("parse_syntax: need ';'",p);
  1568. return p+1;
  1569. }
  1570. p++;
  1571. syntax.curly_count--;
  1572. return p;
  1573. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1574. // 次のループに飛ばす
  1575. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1576. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1577. parse_line(label);
  1578. syntax.curly_count--;
  1579. // for 終了のラベル付け
  1580. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1581. l=add_str(label);
  1582. set_label(l,script_pos,p);
  1583. syntax.curly_count--;
  1584. return p;
  1585. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1586. // while 条件判断へ飛ばす
  1587. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1588. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1589. parse_line(label);
  1590. syntax.curly_count--;
  1591. // while 終了のラベル付け
  1592. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1593. l=add_str(label);
  1594. set_label(l,script_pos,p);
  1595. syntax.curly_count--;
  1596. return p;
  1597. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1598. int pos = syntax.curly_count-1;
  1599. char label[256];
  1600. int l;
  1601. // 戻す
  1602. sprintf(label,"return;");
  1603. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1604. parse_line(label);
  1605. syntax.curly_count--;
  1606. // 現在地のラベルを付ける
  1607. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1608. l=add_str(label);
  1609. set_label(l,script_pos,p);
  1610. syntax.curly_count--;
  1611. return p;
  1612. } else {
  1613. *flag = 0;
  1614. return p;
  1615. }
  1616. }
  1617. /*==========================================
  1618. * 組み込み関数の追加
  1619. *------------------------------------------*/
  1620. static void add_buildin_func(void)
  1621. {
  1622. int i,n;
  1623. const char* p;
  1624. for( i = 0; buildin_func[i].func; i++ )
  1625. {
  1626. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1627. // 'v' - value (either string or int or reference)
  1628. // 's' - string
  1629. // 'i' - int
  1630. // 'r' - reference (of a variable)
  1631. // 'l' - label
  1632. // '?' - one optional parameter
  1633. // '*' - unknown number of optional parameters
  1634. p = buildin_func[i].arg;
  1635. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1636. while( *p == '?' ) ++p;
  1637. if( *p == '*' ) ++p;
  1638. if( *p != 0){
  1639. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1640. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1641. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1642. } else {
  1643. n = add_str(buildin_func[i].name);
  1644. str_data[n].type = C_FUNC;
  1645. str_data[n].val = i;
  1646. str_data[n].func = buildin_func[i].func;
  1647. }
  1648. }
  1649. }
  1650. /*==========================================
  1651. * 定数データベースの読み込み
  1652. *------------------------------------------*/
  1653. static void read_constdb(void)
  1654. {
  1655. FILE *fp;
  1656. char line[1024],name[1024],val[1024];
  1657. int n,type;
  1658. sprintf(line, "%s/const.txt", db_path);
  1659. fp=fopen(line, "r");
  1660. if(fp==NULL){
  1661. ShowError("can't read %s\n", line);
  1662. return ;
  1663. }
  1664. while(fgets(line, sizeof(line), fp))
  1665. {
  1666. if(line[0]=='/' && line[1]=='/')
  1667. continue;
  1668. type=0;
  1669. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1670. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1671. n=add_str(name);
  1672. if(type==0)
  1673. str_data[n].type=C_INT;
  1674. else
  1675. str_data[n].type=C_PARAM;
  1676. str_data[n].val= (int)strtol(val,NULL,0);
  1677. }
  1678. }
  1679. fclose(fp);
  1680. }
  1681. /*==========================================
  1682. * エラー表示
  1683. *------------------------------------------*/
  1684. const char* script_print_line( const char *p, const char *mark, int line )
  1685. {
  1686. int i;
  1687. if( p == NULL || !p[0] ) return NULL;
  1688. if( line < 0 )
  1689. ShowMessage("*% 5d : ", -line);
  1690. else
  1691. ShowMessage(" % 5d : ", line);
  1692. for(i=0;p[i] && p[i] != '\n';i++){
  1693. if(p + i != mark)
  1694. ShowMessage("%c",p[i]);
  1695. else
  1696. ShowMessage("\'%c\'",p[i]);
  1697. }
  1698. ShowMessage("\n");
  1699. return p+i+(p[i] == '\n' ? 1 : 0);
  1700. }
  1701. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos)
  1702. {
  1703. // エラーが発生した行を求める
  1704. int j;
  1705. int line = start_line;
  1706. const char *p;
  1707. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1708. for(p=src;p && *p;line++){
  1709. char *lineend=strchr(p,'\n');
  1710. if(lineend==NULL || error_pos<lineend){
  1711. break;
  1712. }
  1713. for( j = 0; j < 4; j++ ) {
  1714. linestart[j] = linestart[j+1];
  1715. }
  1716. linestart[4] = p;
  1717. p=lineend+1;
  1718. }
  1719. ShowMessage("\a\n");
  1720. ShowMessage("script error on %s line %d\n", file, line);
  1721. ShowMessage(" %s\n", error_msg);
  1722. for(j = 0; j < 5; j++ ) {
  1723. script_print_line( linestart[j], NULL, line + j - 5);
  1724. }
  1725. p = script_print_line( p, error_pos, -line);
  1726. for(j = 0; j < 5; j++) {
  1727. p = script_print_line( p, NULL, line + j + 1 );
  1728. }
  1729. }
  1730. /*==========================================
  1731. * スクリプトの解析
  1732. *------------------------------------------*/
  1733. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1734. {
  1735. const char *p,*tmpp;
  1736. int i;
  1737. struct script_code* code = NULL;
  1738. static int first=1;
  1739. char end;
  1740. if( src == NULL )
  1741. return NULL;// empty script
  1742. memset(&syntax,0,sizeof(syntax));
  1743. if(first){
  1744. add_buildin_func();
  1745. read_constdb();
  1746. first=0;
  1747. }
  1748. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1749. script_pos=0;
  1750. script_size=SCRIPT_BLOCK_SIZE;
  1751. str_data[LABEL_NEXTLINE].type=C_NOP;
  1752. str_data[LABEL_NEXTLINE].backpatch=-1;
  1753. str_data[LABEL_NEXTLINE].label=-1;
  1754. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1755. if( options&SCRIPT_USE_LABEL_DB )
  1756. scriptlabel_db->clear(scriptlabel_db, NULL);
  1757. parse_options = options;
  1758. if( setjmp( error_jump ) != 0 ) {
  1759. //Restore program state when script has problems. [from jA]
  1760. int i;
  1761. const int size = ARRAYLENGTH(syntax.curly);
  1762. if( error_report )
  1763. script_error(src,file,line,error_msg,error_pos);
  1764. aFree( error_msg );
  1765. aFree( script_buf );
  1766. script_pos = 0;
  1767. script_size = 0;
  1768. script_buf = NULL;
  1769. for(i=LABEL_START;i<str_num;i++)
  1770. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1771. for(i=0; i<size; i++)
  1772. linkdb_final(&syntax.curly[i].case_label);
  1773. return NULL;
  1774. }
  1775. parse_syntax_for_flag=0;
  1776. p=src;
  1777. p=skip_space(p);
  1778. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1779. {// does not require brackets around the script
  1780. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1781. {// empty script and can return NULL
  1782. aFree( script_buf );
  1783. script_pos = 0;
  1784. script_size = 0;
  1785. script_buf = NULL;
  1786. return NULL;
  1787. }
  1788. end = '\0';
  1789. }
  1790. else
  1791. {// requires brackets around the script
  1792. if( *p != '{' )
  1793. disp_error_message("not found '{'",p);
  1794. p = skip_space(p+1);
  1795. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1796. {// empty script and can return NULL
  1797. aFree( script_buf );
  1798. script_pos = 0;
  1799. script_size = 0;
  1800. script_buf = NULL;
  1801. return NULL;
  1802. }
  1803. end = '}';
  1804. }
  1805. // clear references of labels, variables and internal functions
  1806. for(i=LABEL_START;i<str_num;i++){
  1807. if(
  1808. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1809. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1810. ){
  1811. str_data[i].type=C_NOP;
  1812. str_data[i].backpatch=-1;
  1813. str_data[i].label=-1;
  1814. }
  1815. }
  1816. while( syntax.curly_count != 0 || *p != end )
  1817. {
  1818. if( *p == '\0' )
  1819. disp_error_message("unexpected end of script",p);
  1820. // labelだけ特殊処理
  1821. tmpp=skip_space(skip_word(p));
  1822. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1823. i=add_word(p);
  1824. set_label(i,script_pos,p);
  1825. if( parse_options&SCRIPT_USE_LABEL_DB )
  1826. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1827. p=tmpp+1;
  1828. p=skip_space(p);
  1829. continue;
  1830. }
  1831. // 他は全部一緒くた
  1832. p=parse_line(p);
  1833. p=skip_space(p);
  1834. add_scriptc(C_EOL);
  1835. set_label(LABEL_NEXTLINE,script_pos,p);
  1836. str_data[LABEL_NEXTLINE].type=C_NOP;
  1837. str_data[LABEL_NEXTLINE].backpatch=-1;
  1838. str_data[LABEL_NEXTLINE].label=-1;
  1839. }
  1840. add_scriptc(C_NOP);
  1841. // trim code to size
  1842. script_size = script_pos;
  1843. RECREATE(script_buf,unsigned char,script_pos);
  1844. // default unknown references to variables
  1845. for(i=LABEL_START;i<str_num;i++){
  1846. if(str_data[i].type==C_NOP){
  1847. int j,next;
  1848. str_data[i].type=C_NAME;
  1849. str_data[i].label=i;
  1850. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1851. next=GETVALUE(script_buf,j);
  1852. SETVALUE(script_buf,j,i);
  1853. j=next;
  1854. }
  1855. }
  1856. }
  1857. #ifdef DEBUG_DISP
  1858. for(i=0;i<script_pos;i++){
  1859. if((i&15)==0) printf("%04x : ",i);
  1860. ShowMessage("%02x ",script_buf[i]);
  1861. if((i&15)==15) printf("\n");
  1862. }
  1863. ShowMessage("\n");
  1864. #endif
  1865. #ifdef DEBUG_DISASM
  1866. {
  1867. int i = 0,j;
  1868. while(i < script_pos) {
  1869. ShowMessage("%06x ",i);
  1870. j = i;
  1871. switch(get_com(script_buf,&i)) {
  1872. case C_EOL: printf("C_EOL"); break;
  1873. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1874. case C_POS:
  1875. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1876. i += 3;
  1877. break;
  1878. case C_NAME:
  1879. j = (*(int*)(script_buf+i)&0xffffff);
  1880. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1881. i += 3;
  1882. break;
  1883. case C_ARG: ShowMessage("C_ARG"); break;
  1884. case C_FUNC: ShowMessage("C_FUNC"); break;
  1885. case C_ADD: ShowMessage("C_ADD"); break;
  1886. case C_SUB: ShowMessage("C_SUB"); break;
  1887. case C_MUL: ShowMessage("C_MUL"); break;
  1888. case C_DIV: ShowMessage("C_DIV"); break;
  1889. case C_MOD: ShowMessage("C_MOD"); break;
  1890. case C_EQ: ShowMessage("C_EQ"); break;
  1891. case C_NE: ShowMessage("C_NE"); break;
  1892. case C_GT: ShowMessage("C_GT"); break;
  1893. case C_GE: ShowMessage("C_GE"); break;
  1894. case C_LT: ShowMessage("C_LT"); break;
  1895. case C_LE: ShowMessage("C_LE"); break;
  1896. case C_AND: ShowMessage("C_AND"); break;
  1897. case C_OR: ShowMessage("C_OR"); break;
  1898. case C_XOR: ShowMessage("C_XOR"); break;
  1899. case C_LAND: ShowMessage("C_LAND"); break;
  1900. case C_LOR: ShowMessage("C_LOR"); break;
  1901. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1902. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1903. case C_NEG: ShowMessage("C_NEG"); break;
  1904. case C_NOT: ShowMessage("C_NOT"); break;
  1905. case C_LNOT: ShowMessage("C_LNOT"); break;
  1906. case C_NOP: ShowMessage("C_NOP"); break;
  1907. case C_OP3: ShowMessage("C_OP3"); break;
  1908. case C_STR:
  1909. j = strlen(script_buf + i);
  1910. ShowMessage("C_STR %s",script_buf + i);
  1911. i+= j+1;
  1912. break;
  1913. default:
  1914. ShowMessage("unknown");
  1915. }
  1916. ShowMessage("\n");
  1917. }
  1918. }
  1919. #endif
  1920. CREATE(code,struct script_code,1);
  1921. code->script_buf = script_buf;
  1922. code->script_size = script_size;
  1923. code->script_vars = NULL;
  1924. return code;
  1925. }
  1926. //
  1927. // Script state
  1928. //
  1929. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1930. /// Returns the player attached to this script, identified by the rid.
  1931. /// If there is no player attached, the script is terminated.
  1932. TBL_PC *script_rid2sd(struct script_state *st)
  1933. {
  1934. TBL_PC *sd=map_id2sd(st->rid);
  1935. if(!sd){
  1936. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1937. script_reportsrc(st);
  1938. st->state = END;
  1939. }
  1940. return sd;
  1941. }
  1942. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1943. ///
  1944. /// @param st Script state
  1945. /// @param data Variable/constant
  1946. void get_val(struct script_state* st, struct script_data* data)
  1947. {
  1948. char* name;
  1949. char prefix;
  1950. char postfix;
  1951. TBL_PC* sd = NULL;
  1952. if( !data_isreference(data) )
  1953. return;// not a variable/constant
  1954. name = reference_getname(data);
  1955. prefix = name[0];
  1956. postfix = name[strlen(name) - 1];
  1957. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1958. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1959. {
  1960. sd = script_rid2sd(st);
  1961. if( sd == NULL )
  1962. {// needs player attached
  1963. if( postfix == '$' )
  1964. {// string variable
  1965. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1966. data->type = C_CONSTSTR;
  1967. data->u.str = "";
  1968. }
  1969. else
  1970. {// integer variable
  1971. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1972. data->type = C_INT;
  1973. data->u.num = 0;
  1974. }
  1975. return;
  1976. }
  1977. }
  1978. if( postfix == '$' )
  1979. {// string variable
  1980. switch( prefix )
  1981. {
  1982. case '@':
  1983. data->u.str = pc_readregstr(sd, data->u.num);
  1984. break;
  1985. case '$':
  1986. data->u.str = (char *)idb_get(mapregstr_db, data->u.num);
  1987. break;
  1988. case '#':
  1989. if( name[1] == '#' )
  1990. data->u.str = pc_readaccountreg2str(sd, name);// global
  1991. else
  1992. data->u.str = pc_readaccountregstr(sd, name);// local
  1993. break;
  1994. case '.':
  1995. {
  1996. struct linkdb_node** n =
  1997. data->ref ? data->ref:
  1998. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  1999. &st->script->script_vars;// npc variable
  2000. data->u.str = linkdb_search(n, (void*)reference_getuid(data));
  2001. }
  2002. break;
  2003. default:
  2004. data->u.str = pc_readglobalreg_str(sd, name);
  2005. break;
  2006. }
  2007. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2008. {// empty string
  2009. data->type = C_CONSTSTR;
  2010. data->u.str = "";
  2011. }
  2012. else
  2013. {// duplicate string
  2014. data->type = C_STR;
  2015. data->u.str = aStrdup(data->u.str);
  2016. }
  2017. }
  2018. else
  2019. {// integer variable
  2020. data->type = C_INT;
  2021. if( reference_toconstant(data) )
  2022. {
  2023. data->u.num = reference_getconstant(data);
  2024. }
  2025. else if( reference_toparam(data) )
  2026. {
  2027. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2028. }
  2029. else
  2030. switch( prefix )
  2031. {
  2032. case '@':
  2033. data->u.num = pc_readreg(sd, data->u.num);
  2034. break;
  2035. case '$':
  2036. data->u.num = (int)idb_get(mapreg_db, data->u.num);
  2037. break;
  2038. case '#':
  2039. if( name[1] == '#' )
  2040. data->u.num = pc_readaccountreg2(sd, name);// global
  2041. else
  2042. data->u.num = pc_readaccountreg(sd, name);// local
  2043. break;
  2044. case '.':
  2045. {
  2046. struct linkdb_node** n =
  2047. data->ref ? data->ref:
  2048. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2049. &st->script->script_vars;// npc variable
  2050. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2051. }
  2052. break;
  2053. default:
  2054. data->u.num = pc_readglobalreg(sd, name);
  2055. break;
  2056. }
  2057. }
  2058. return;
  2059. }
  2060. void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref);
  2061. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2062. /// The value is left in the top of the stack and needs to be removed manually.
  2063. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2064. {
  2065. struct script_data* data;
  2066. push_val2(st->stack, C_NAME, uid, ref);
  2067. data = script_getdatatop(st, -1);
  2068. get_val(st, data);
  2069. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2070. }
  2071. /*==========================================
  2072. * Stores the value of a script variable
  2073. * Return value is 0 on fail, 1 on success.
  2074. *------------------------------------------*/
  2075. static int set_reg(struct script_state* st, TBL_PC* sd, int num, char* name, void* value, struct linkdb_node** ref)
  2076. {
  2077. char prefix = name[0]; char postfix = name[strlen(name)-1];
  2078. if (postfix == '$') { // string variable
  2079. char* str = (char*)value;
  2080. switch (prefix) {
  2081. case '@':
  2082. return pc_setregstr(sd, num, str);
  2083. case '$':
  2084. return mapreg_setregstr(num, str);
  2085. case '#':
  2086. return (name[1] == '#') ?
  2087. pc_setaccountreg2str(sd, name, str) :
  2088. pc_setaccountregstr(sd, name, str);
  2089. case '.': {
  2090. char* p;
  2091. struct linkdb_node** n;
  2092. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2093. p = linkdb_erase(n, (void*)num);
  2094. if (p) aFree(p);
  2095. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2096. }
  2097. return 1;
  2098. default:
  2099. return pc_setglobalreg_str(sd, name, str);
  2100. }
  2101. } else { // integer variable
  2102. int val = (int)value;
  2103. if(str_data[num&0x00ffffff].type == C_PARAM)
  2104. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2105. switch (prefix) {
  2106. case '@':
  2107. return pc_setreg(sd, num, val);
  2108. case '$':
  2109. return mapreg_setreg(num, val);
  2110. case '#':
  2111. return (name[1] == '#') ?
  2112. pc_setaccountreg2(sd, name, val) :
  2113. pc_setaccountreg(sd, name, val);
  2114. case '.': {
  2115. struct linkdb_node** n;
  2116. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2117. if (val == 0)
  2118. linkdb_erase(n, (void*)num);
  2119. else
  2120. linkdb_replace(n, (void*)num, (void*)val);
  2121. }
  2122. return 1;
  2123. default:
  2124. return pc_setglobalreg(sd, name, val);
  2125. }
  2126. }
  2127. }
  2128. int set_var(TBL_PC* sd, char* name, void* val)
  2129. {
  2130. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  2131. }
  2132. /// Converts the data to a string
  2133. const char* conv_str(struct script_state* st, struct script_data* data)
  2134. {
  2135. char* p;
  2136. get_val(st, data);
  2137. if( data_isstring(data) )
  2138. {// nothing to convert
  2139. }
  2140. else if( data_isint(data) )
  2141. {// int -> string
  2142. CREATE(p, char, ITEM_NAME_LENGTH);
  2143. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2144. p[ITEM_NAME_LENGTH-1] = '\0';
  2145. data->type = C_STR;
  2146. data->u.str = p;
  2147. }
  2148. else if( data_isreference(data) )
  2149. {// reference -> string
  2150. //##TODO when does this happen (check get_val) [FlavioJS]
  2151. data->type = C_CONSTSTR;
  2152. data->u.str = reference_getname(data);
  2153. }
  2154. else
  2155. {// unsupported data type
  2156. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2157. script_reportdata(data);
  2158. script_reportsrc(st);
  2159. data->type = C_CONSTSTR;
  2160. data->u.str = "";
  2161. }
  2162. return data->u.str;
  2163. }
  2164. /// Converts the data to an int
  2165. int conv_num(struct script_state* st, struct script_data* data)
  2166. {
  2167. char* p;
  2168. long num;
  2169. get_val(st, data);
  2170. if( data_isint(data) )
  2171. {// nothing to convert
  2172. }
  2173. else if( data_isstring(data) )
  2174. {// string -> int
  2175. // the result does not overflow or underflow, it is capped instead
  2176. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2177. p = data->u.str;
  2178. errno = 0;
  2179. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2180. if( errno == ERANGE
  2181. #if LONG_MAX > INT_MAX
  2182. || num < INT_MIN || num > INT_MAX
  2183. #endif
  2184. )
  2185. {
  2186. if( num <= INT_MIN )
  2187. {
  2188. num = INT_MIN;
  2189. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2190. }
  2191. else//if( num >= INT_MAX )
  2192. {
  2193. num = INT_MAX;
  2194. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2195. }
  2196. script_reportdata(data);
  2197. script_reportsrc(st);
  2198. }
  2199. if( data->type == C_STR )
  2200. aFree(p);
  2201. data->type = C_INT;
  2202. data->u.num = (int)num;
  2203. }
  2204. #if 0
  2205. // FIXME this function is being used to retrieve the position of labels and
  2206. // probably other stuff [FlavioJS]
  2207. else
  2208. {// unsupported data type
  2209. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2210. script_reportdata(data);
  2211. script_reportsrc(st);
  2212. data->type = C_INT;
  2213. data->u.num = 0;
  2214. }
  2215. #endif
  2216. return data->u.num;
  2217. }
  2218. //
  2219. // Stack operations
  2220. //
  2221. /// Increases the size of the stack
  2222. void stack_expand(struct script_stack* stack)
  2223. {
  2224. stack->sp_max += 64;
  2225. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2226. stack->sp_max * sizeof(stack->stack_data[0]) );
  2227. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2228. 64 * sizeof(stack->stack_data[0]) );
  2229. }
  2230. /// Pushes a value into the stack
  2231. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2232. /// Pushes a value into the stack (with reference)
  2233. void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref)
  2234. {
  2235. if( stack->sp >= stack->sp_max )
  2236. stack_expand(stack);
  2237. stack->stack_data[stack->sp].type = type;
  2238. stack->stack_data[stack->sp].u.num = val;
  2239. stack->stack_data[stack->sp].ref = ref;
  2240. stack->sp++;
  2241. }
  2242. /// Pushes a string into the stack
  2243. void push_str(struct script_stack* stack, int type, char* str)
  2244. {
  2245. if( stack->sp >= stack->sp_max )
  2246. stack_expand(stack);
  2247. stack->stack_data[stack->sp].type = type;
  2248. stack->stack_data[stack->sp].u.str = str;
  2249. stack->stack_data[stack->sp].ref = NULL;
  2250. stack->sp++;
  2251. }
  2252. /// Pushes a copy of the target position into the stack
  2253. void push_copy(struct script_stack* stack, int pos)
  2254. {
  2255. switch( stack->stack_data[pos].type )
  2256. {
  2257. case C_CONSTSTR:
  2258. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2259. break;
  2260. case C_STR:
  2261. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2262. break;
  2263. default:
  2264. push_val2(
  2265. stack,stack->stack_data[pos].type,
  2266. stack->stack_data[pos].u.num,
  2267. stack->stack_data[pos].ref
  2268. );
  2269. break;
  2270. }
  2271. }
  2272. /// Removes the values in indexes [start,end[ from the stack
  2273. void pop_stack(struct script_stack* stack, int start, int end)
  2274. {
  2275. struct script_data* data;
  2276. int i;
  2277. if( start < 0 )
  2278. start = 0;
  2279. if( end > stack->sp_max )
  2280. end = stack->sp_max;
  2281. if( start >= end )
  2282. return;// nothing to pop
  2283. // free stack elements
  2284. for( i = start; i < end; i++ )
  2285. {
  2286. data = &stack->stack_data[i];
  2287. if( data->type == C_STR )
  2288. aFree(data->u.str);
  2289. data->type = C_NOP;
  2290. }
  2291. // move the rest of the elements
  2292. if( stack->sp > end )
  2293. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2294. stack->sp -= end - start;
  2295. }
  2296. ///
  2297. ///
  2298. ///
  2299. /*==========================================
  2300. * スクリプト依存変数、関数依存変数の解放
  2301. *------------------------------------------*/
  2302. void script_free_vars(struct linkdb_node **node)
  2303. {
  2304. struct linkdb_node *n = *node;
  2305. while(n) {
  2306. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2307. char postfix = name[strlen(name)-1];
  2308. if( postfix == '$' ) {
  2309. // 文字型変数なので、データ削除
  2310. aFree(n->data);
  2311. }
  2312. n = n->next;
  2313. }
  2314. linkdb_final( node );
  2315. }
  2316. /*==========================================
  2317. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2318. *------------------------------------------*/
  2319. void script_free_stack(struct script_stack *stack)
  2320. {
  2321. int i;
  2322. for(i = 0; i < stack->sp; i++) {
  2323. if( stack->stack_data[i].type == C_STR ) {
  2324. aFree(stack->stack_data[i].u.str);
  2325. stack->stack_data[i].type = C_INT;
  2326. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2327. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2328. script_free_vars( n );
  2329. aFree( n );
  2330. }
  2331. }
  2332. script_free_vars( stack->var_function );
  2333. aFree(stack->var_function);
  2334. aFree(stack->stack_data);
  2335. aFree(stack);
  2336. }
  2337. void script_free_code(struct script_code* code)
  2338. {
  2339. script_free_vars( &code->script_vars );
  2340. aFree( code->script_buf );
  2341. aFree( code );
  2342. }
  2343. //
  2344. // 実行部main
  2345. //
  2346. /*==========================================
  2347. * コマンドの読み取り
  2348. *------------------------------------------*/
  2349. static int unget_com_data=-1;
  2350. int get_com(unsigned char *script,int *pos)
  2351. {
  2352. int i,j;
  2353. if(unget_com_data>=0){
  2354. i=unget_com_data;
  2355. unget_com_data=-1;
  2356. return i;
  2357. }
  2358. if(script[*pos]>=0x80){
  2359. return C_INT;
  2360. }
  2361. i=0; j=0;
  2362. while(script[*pos]>=0x40){
  2363. i=script[(*pos)++]<<j;
  2364. j+=6;
  2365. }
  2366. return i+(script[(*pos)++]<<j);
  2367. }
  2368. /*==========================================
  2369. * コマンドのプッシュバック
  2370. *------------------------------------------*/
  2371. void unget_com(int c)
  2372. {
  2373. if(unget_com_data!=-1)
  2374. ShowError("unget_com can back only 1 data\n");
  2375. unget_com_data=c;
  2376. }
  2377. /*==========================================
  2378. * 数値の所得
  2379. *------------------------------------------*/
  2380. int get_num(unsigned char *script,int *pos)
  2381. {
  2382. int i,j;
  2383. i=0; j=0;
  2384. while(script[*pos]>=0xc0){
  2385. i+=(script[(*pos)++]&0x7f)<<j;
  2386. j+=6;
  2387. }
  2388. return i+((script[(*pos)++]&0x7f)<<j);
  2389. }
  2390. /*==========================================
  2391. * スタックから値を取り出す
  2392. *------------------------------------------*/
  2393. int pop_val(struct script_state* st)
  2394. {
  2395. if(st->stack->sp<=0)
  2396. return 0;
  2397. st->stack->sp--;
  2398. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2399. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2400. return st->stack->stack_data[st->stack->sp].u.num;
  2401. return 0;
  2402. }
  2403. int isstr(struct script_data *c)
  2404. {
  2405. if( data_isstring(c) )
  2406. return 1;
  2407. else if( data_isreference(c) ) {
  2408. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2409. char postfix = p[strlen(p)-1];
  2410. return (postfix == '$');
  2411. }
  2412. return 0;
  2413. }
  2414. /// Ternary operators
  2415. /// test ? if_true : if_false
  2416. void op_3(struct script_state* st, int op)
  2417. {
  2418. struct script_data* data;
  2419. int flag = 0;
  2420. data = script_getdatatop(st, -3);
  2421. get_val(st, data);
  2422. if( data_isstring(data) )
  2423. flag = data->u.str[0];// "" -> false
  2424. else if( data_isint(data) )
  2425. flag = data->u.num;// 0 -> false
  2426. else
  2427. {
  2428. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2429. script_reportdata(data);
  2430. script_reportsrc(st);
  2431. script_removetop(st, -3, 0);
  2432. script_pushnil(st);
  2433. return;
  2434. }
  2435. if( flag )
  2436. script_pushcopytop(st, -2);
  2437. else
  2438. script_pushcopytop(st, -1);
  2439. script_removetop(st, -4, -1);
  2440. }
  2441. /// Binary string operators
  2442. /// s1 EQ s2 -> i
  2443. /// s1 NE s2 -> i
  2444. /// s1 GT s2 -> i
  2445. /// s1 GE s2 -> i
  2446. /// s1 LT s2 -> i
  2447. /// s1 LE s2 -> i
  2448. /// s1 ADD s2 -> s
  2449. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2450. {
  2451. int a = 0;
  2452. switch(op){
  2453. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2454. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2455. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2456. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2457. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2458. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2459. case C_ADD:
  2460. {
  2461. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2462. strcpy(buf, s1);
  2463. strcat(buf, s2);
  2464. script_pushstr(st, buf);
  2465. return;
  2466. }
  2467. default:
  2468. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2469. script_reportsrc(st);
  2470. script_pushnil(st);
  2471. st->state = END;
  2472. return;
  2473. }
  2474. script_pushint(st,a);
  2475. }
  2476. /// Binary number operators
  2477. /// i OP i -> i
  2478. void op_2num(struct script_state* st, int op, int i1, int i2)
  2479. {
  2480. int ret;
  2481. double ret_double;
  2482. switch( op )
  2483. {
  2484. case C_AND: ret = i1 & i2; break;
  2485. case C_OR: ret = i1 | i2; break;
  2486. case C_XOR: ret = i1 ^ i2; break;
  2487. case C_LAND: ret = (i1 && i2); break;
  2488. case C_LOR: ret = (i1 || i2); break;
  2489. case C_EQ: ret = (i1 == i2); break;
  2490. case C_NE: ret = (i1 != i2); break;
  2491. case C_GT: ret = (i1 > i2); break;
  2492. case C_GE: ret = (i1 >= i2); break;
  2493. case C_LT: ret = (i1 < i2); break;
  2494. case C_LE: ret = (i1 <= i2); break;
  2495. case C_R_SHIFT: ret = i1>>i2; break;
  2496. case C_L_SHIFT: ret = i1<<i2; break;
  2497. case C_DIV:
  2498. case C_MOD:
  2499. if( i2 == 0 )
  2500. {
  2501. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2502. script_reportsrc(st);
  2503. script_pushnil(st);
  2504. st->state = END;
  2505. return;
  2506. }
  2507. else if( op == C_DIV )
  2508. ret = i1 / i2;
  2509. else//if( op == C_MOD )
  2510. ret = i1 % i2;
  2511. break;
  2512. default:
  2513. switch( op )
  2514. {// operators that can overflow/underflow
  2515. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2516. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2517. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2518. default:
  2519. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2520. script_reportsrc(st);
  2521. script_pushnil(st);
  2522. return;
  2523. }
  2524. if( ret_double < (double)INT_MIN )
  2525. {
  2526. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2527. script_reportsrc(st);
  2528. ret = INT_MIN;
  2529. }
  2530. else if( ret_double > (double)INT_MAX )
  2531. {
  2532. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2533. script_reportsrc(st);
  2534. ret = INT_MAX;
  2535. }
  2536. }
  2537. script_pushint(st, ret);
  2538. }
  2539. /// Binary operators
  2540. void op_2(struct script_state *st, int op)
  2541. {
  2542. struct script_data* left;
  2543. struct script_data* right;
  2544. left = script_getdatatop(st, -2);
  2545. right = script_getdatatop(st, -1);
  2546. get_val(st, left);
  2547. get_val(st, right);
  2548. // automatic conversions
  2549. switch( op )
  2550. {
  2551. case C_ADD:
  2552. if( data_isint(left) && data_isstring(right) )
  2553. {// convert int-string to string-string
  2554. conv_str(st, left);
  2555. }
  2556. else if( data_isstring(left) && data_isint(right) )
  2557. {// convert string-int to string-string
  2558. conv_str(st, right);
  2559. }
  2560. break;
  2561. }
  2562. if( data_isstring(left) && data_isstring(right) )
  2563. {// ss => op_2str
  2564. op_2str(st, op, left->u.str, right->u.str);
  2565. script_removetop(st, -3, -1);// pop the two values before the top one
  2566. }
  2567. else if( data_isint(left) && data_isint(right) )
  2568. {// ii => op_2num
  2569. int i1 = left->u.num;
  2570. int i2 = right->u.num;
  2571. script_removetop(st, -2, 0);
  2572. op_2num(st, op, i1, i2);
  2573. }
  2574. else
  2575. {// invalid argument
  2576. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2577. script_reportdata(left);
  2578. script_reportdata(right);
  2579. script_reportsrc(st);
  2580. script_removetop(st, -2, 0);
  2581. script_pushnil(st);
  2582. st->state = END;
  2583. }
  2584. }
  2585. /// Unary operators
  2586. /// NEG i -> i
  2587. /// NOT i -> i
  2588. /// LNOT i -> i
  2589. void op_1(struct script_state* st, int op)
  2590. {
  2591. struct script_data* data;
  2592. int i1;
  2593. data = script_getdatatop(st, -1);
  2594. get_val(st, data);
  2595. if( !data_isint(data) )
  2596. {// not a number
  2597. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2598. script_reportdata(data);
  2599. script_reportsrc(st);
  2600. script_pushnil(st);
  2601. st->state = END;
  2602. return;
  2603. }
  2604. i1 = data->u.num;
  2605. script_removetop(st, -1, 0);
  2606. switch( op )
  2607. {
  2608. case C_NEG: i1 = -i1; break;
  2609. case C_NOT: i1 = ~i1; break;
  2610. case C_LNOT: i1 = !i1; break;
  2611. default:
  2612. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2613. script_reportsrc(st);
  2614. script_pushnil(st);
  2615. st->state = END;
  2616. return;
  2617. }
  2618. script_pushint(st, i1);
  2619. }
  2620. /*==========================================
  2621. * 関数の実行
  2622. *------------------------------------------*/
  2623. int run_func(struct script_state *st)
  2624. {
  2625. int i,start_sp,end_sp,func;
  2626. end_sp=st->stack->sp;
  2627. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2628. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2629. ShowError("function not found\n");
  2630. // st->stack->sp=0;
  2631. st->state=END;
  2632. script_reportsrc(st);
  2633. return 1;
  2634. }
  2635. start_sp=i-1;
  2636. st->start=i-1;
  2637. st->end=end_sp;
  2638. func=st->stack->stack_data[st->start].u.num;
  2639. #ifdef DEBUG_RUN
  2640. if(battle_config.etc_log) {
  2641. 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);
  2642. ShowDebug("stack dump :");
  2643. for(i=0;i<end_sp;i++){
  2644. switch(st->stack->stack_data[i].type){
  2645. case C_INT:
  2646. ShowMessage(" int(%d)",st->stack->stack_data[i].u.num);
  2647. break;
  2648. case C_NAME:
  2649. ShowMessage(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2650. break;
  2651. case C_ARG:
  2652. ShowMessage(" arg");
  2653. break;
  2654. case C_POS:
  2655. ShowMessage(" pos(%d)",st->stack->stack_data[i].u.num);
  2656. break;
  2657. case C_STR:
  2658. ShowMessage(" str(%s)",st->stack->stack_data[i].u.str);
  2659. break;
  2660. case C_CONSTSTR:
  2661. ShowMessage(" cstr(%s)",st->stack->stack_data[i].u.str);
  2662. break;
  2663. default:
  2664. ShowMessage(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2665. }
  2666. }
  2667. ShowMessage("\n");
  2668. }
  2669. #endif
  2670. if(str_data[func].type!=C_FUNC ){
  2671. ShowError("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);
  2672. // st->stack->sp=0;
  2673. st->state=END;
  2674. script_reportsrc(st);
  2675. return 1;
  2676. }
  2677. #ifdef DEBUG_RUN
  2678. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2679. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2680. );
  2681. #endif
  2682. if(str_data[func].func){
  2683. if (str_data[func].func(st)) //Report error
  2684. script_reportsrc(st);
  2685. } else {
  2686. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2687. script_pushint(st,0);
  2688. script_reportsrc(st);
  2689. }
  2690. // Stack's datum are used when re-run functions [Eoe]
  2691. if(st->state != RERUNLINE) {
  2692. pop_stack(st->stack,start_sp,end_sp);
  2693. }
  2694. if(st->state==RETFUNC){
  2695. // ユーザー定義関数からの復帰
  2696. int olddefsp=st->stack->defsp;
  2697. int i;
  2698. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2699. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2700. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2701. st->state=END;
  2702. script_reportsrc(st);
  2703. return 1;
  2704. }
  2705. script_free_vars( st->stack->var_function );
  2706. aFree(st->stack->var_function);
  2707. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2708. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2709. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2710. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2711. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2712. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2713. st->state=GOTO;
  2714. }
  2715. return 0;
  2716. }
  2717. /*==========================================
  2718. * スクリプトの実行
  2719. *------------------------------------------*/
  2720. void run_script_main(struct script_state *st);
  2721. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2722. {
  2723. struct script_state *st;
  2724. TBL_PC *sd=NULL;
  2725. if(rootscript==NULL || pos<0)
  2726. return;
  2727. if (rid) sd = map_id2sd(rid);
  2728. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2729. //Resume script.
  2730. st = sd->st;
  2731. } else {
  2732. st = aCalloc(sizeof(struct script_state), 1);
  2733. // the script is different, make new script_state and stack
  2734. st->stack = aMalloc (sizeof(struct script_stack));
  2735. st->stack->sp=0;
  2736. st->stack->sp_max=64;
  2737. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2738. st->stack->defsp = st->stack->sp;
  2739. st->stack->var_function = aCalloc(1, sizeof(struct linkdb_node*));
  2740. st->state = RUN;
  2741. st->script = rootscript;
  2742. }
  2743. st->pos = pos;
  2744. st->rid = rid;
  2745. st->oid = oid;
  2746. st->sleep.timer = INVALID_TIMER;
  2747. run_script_main(st);
  2748. }
  2749. void script_stop_sleeptimers(int id)
  2750. {
  2751. struct script_state* st;
  2752. for(;;)
  2753. {
  2754. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2755. if( st == NULL )
  2756. break; // no more sleep timers
  2757. if( st->sleep.timer != INVALID_TIMER )
  2758. delete_timer(st->sleep.timer, run_script_timer);
  2759. script_free_stack(st->stack);
  2760. aFree(st);
  2761. }
  2762. }
  2763. /*==========================================
  2764. * 指定ノードをsleep_dbから削除
  2765. *------------------------------------------*/
  2766. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2767. {
  2768. struct linkdb_node *retnode;
  2769. if( n == NULL)
  2770. return NULL;
  2771. if( n->prev == NULL )
  2772. sleep_db = n->next;
  2773. else
  2774. n->prev->next = n->next;
  2775. if( n->next )
  2776. n->next->prev = n->prev;
  2777. retnode = n->next;
  2778. aFree( n );
  2779. return retnode; // 次のノードを返す
  2780. }
  2781. /*==========================================
  2782. * sleep用タイマー関数
  2783. *------------------------------------------*/
  2784. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2785. {
  2786. struct script_state *st = (struct script_state *)data;
  2787. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2788. TBL_PC *sd = map_id2sd(st->rid);
  2789. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2790. { //Character mismatch. Cancel execution.
  2791. st->rid = 0;
  2792. st->state = END;
  2793. }
  2794. while( node && st->sleep.timer != -1 ) {
  2795. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2796. script_erase_sleepdb(node);
  2797. st->sleep.timer = -1;
  2798. break;
  2799. }
  2800. node = node->next;
  2801. }
  2802. if(st->state != RERUNLINE)
  2803. st->sleep.tick = 0;
  2804. run_script_main(st);
  2805. return 0;
  2806. }
  2807. /*==========================================
  2808. * スクリプトの実行メイン部分
  2809. *------------------------------------------*/
  2810. void run_script_main(struct script_state *st)
  2811. {
  2812. int c;
  2813. int cmdcount=script_config.check_cmdcount;
  2814. int gotocount=script_config.check_gotocount;
  2815. TBL_PC *sd;
  2816. //For backing up purposes
  2817. struct script_state *bk_st = NULL;
  2818. int bk_npcid = 0;
  2819. struct script_stack *stack=st->stack;
  2820. sd = st->rid?map_id2sd(st->rid):NULL;
  2821. if(sd){
  2822. if(sd->st != st){
  2823. bk_st = sd->st;
  2824. bk_npcid = sd->npc_id;
  2825. }
  2826. sd->st = st;
  2827. sd->npc_id = st->oid;
  2828. }
  2829. if(st->state == RERUNLINE) {
  2830. st->state = RUN;
  2831. run_func(st);
  2832. if(st->state == GOTO)
  2833. st->state = RUN;
  2834. } else if(st->state != END)
  2835. st->state = RUN;
  2836. while(st->state == RUN){
  2837. c= get_com(st->script->script_buf,&st->pos);
  2838. switch(c){
  2839. case C_EOL:
  2840. if( stack->sp != stack->defsp )
  2841. {
  2842. if( stack->sp > stack->defsp )
  2843. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2844. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2845. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2846. } else
  2847. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2848. stack->sp = stack->defsp;
  2849. }
  2850. break;
  2851. case C_INT:
  2852. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2853. break;
  2854. case C_POS:
  2855. case C_NAME:
  2856. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2857. st->pos+=3;
  2858. break;
  2859. case C_ARG:
  2860. push_val(stack,c,0);
  2861. break;
  2862. case C_STR:
  2863. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2864. while(st->script->script_buf[st->pos++]);
  2865. break;
  2866. case C_FUNC:
  2867. run_func(st);
  2868. if(st->state==GOTO){
  2869. st->state = RUN;
  2870. if( gotocount>0 && (--gotocount)<=0 ){
  2871. ShowError("run_script: infinity loop !\n");
  2872. script_reportsrc(st);
  2873. st->state=END;
  2874. }
  2875. }
  2876. break;
  2877. case C_NEG:
  2878. case C_NOT:
  2879. case C_LNOT:
  2880. op_1(st ,c);
  2881. break;
  2882. case C_ADD:
  2883. case C_SUB:
  2884. case C_MUL:
  2885. case C_DIV:
  2886. case C_MOD:
  2887. case C_EQ:
  2888. case C_NE:
  2889. case C_GT:
  2890. case C_GE:
  2891. case C_LT:
  2892. case C_LE:
  2893. case C_AND:
  2894. case C_OR:
  2895. case C_XOR:
  2896. case C_LAND:
  2897. case C_LOR:
  2898. case C_R_SHIFT:
  2899. case C_L_SHIFT:
  2900. op_2(st, c);
  2901. break;
  2902. case C_OP3:
  2903. op_3(st, c);
  2904. break;
  2905. case C_NOP:
  2906. st->state=END;
  2907. break;
  2908. default:
  2909. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2910. st->state=END;
  2911. break;
  2912. }
  2913. if( cmdcount>0 && (--cmdcount)<=0 ){
  2914. ShowError("run_script: infinity loop !\n");
  2915. script_reportsrc(st);
  2916. st->state=END;
  2917. }
  2918. }
  2919. if(st->sleep.tick > 0) {
  2920. //Delay execution
  2921. st->sleep.charid = sd?sd->status.char_id:0;
  2922. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2923. run_script_timer, st->sleep.charid, (int)st);
  2924. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2925. //Restore previous script
  2926. if (sd) {
  2927. sd->st = bk_st;
  2928. sd->npc_id = bk_npcid;
  2929. bk_st = NULL; //Remove tag for removal.
  2930. }
  2931. }
  2932. else if(st->state != END && sd){
  2933. //Resume later (st is already attached to player).
  2934. if(bk_st) {
  2935. ShowWarning("Unable to restore stack! Double continuation!\n");
  2936. //Report BOTH scripts to see if that can help somehow.
  2937. ShowDebug("Previous script (lost):\n");
  2938. script_reportsrc(bk_st);
  2939. ShowDebug("Current script:\n");
  2940. script_reportsrc(st);
  2941. }
  2942. } else {
  2943. //Dispose of script.
  2944. if (sd)
  2945. { //Restore previous stack and save char.
  2946. if(sd->state.using_fake_npc){
  2947. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2948. sd->state.using_fake_npc = 0;
  2949. }
  2950. //Restore previous script if any.
  2951. sd->st = bk_st;
  2952. sd->npc_id = bk_npcid;
  2953. if (!bk_st)
  2954. npc_event_dequeue(sd);
  2955. else
  2956. bk_st = NULL; //Remove tag for removal.
  2957. if (sd->state.reg_dirty&2)
  2958. intif_saveregistry(sd,2);
  2959. if (sd->state.reg_dirty&1)
  2960. intif_saveregistry(sd,1);
  2961. }
  2962. st->pos = -1;
  2963. script_free_stack (st->stack);
  2964. aFree(st);
  2965. }
  2966. if (bk_st)
  2967. { //Remove previous script
  2968. bk_st->pos = -1;
  2969. script_free_stack(bk_st->stack);
  2970. aFree(bk_st);
  2971. bk_st = NULL;
  2972. }
  2973. }
  2974. /*==========================================
  2975. * マップ変数の変更
  2976. *------------------------------------------*/
  2977. int mapreg_setreg(int num, int val)
  2978. {
  2979. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2980. int i = num >> 24;
  2981. char* name = str_buf + str_data[num&0x00ffffff].str;
  2982. if( val != 0 ) {
  2983. if(idb_put(mapreg_db,num,(void*)val))
  2984. ;
  2985. else if(name[1] != '@') {
  2986. char tmp_str[32*2+1];
  2987. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  2988. 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) )
  2989. Sql_ShowDebug(mmysql_handle);
  2990. }
  2991. } else { // val == 0
  2992. if(name[1] != '@') { // Remove from database because it is unused.
  2993. 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) )
  2994. Sql_ShowDebug(mmysql_handle);
  2995. }
  2996. idb_remove(mapreg_db,num);
  2997. }
  2998. #else
  2999. if(val != 0)
  3000. idb_put(mapreg_db,num,(void*)val);
  3001. else
  3002. idb_remove(mapreg_db,num);
  3003. #endif
  3004. mapreg_dirty = 1;
  3005. return 1;
  3006. }
  3007. /*==========================================
  3008. * 文字列型マップ変数の変更
  3009. *------------------------------------------*/
  3010. int mapreg_setregstr(int num, const char* str)
  3011. {
  3012. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  3013. int i = num >> 24;
  3014. char* name = str_buf + str_data[num&0x00ffffff].str;
  3015. if( str==NULL || *str==0 ) {
  3016. if(name[1] != '@') {
  3017. 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) )
  3018. Sql_ShowDebug(mmysql_handle);
  3019. }
  3020. idb_remove(mapregstr_db,num);
  3021. mapreg_dirty = 1;
  3022. return 1;
  3023. }
  3024. if (idb_put(mapregstr_db,num, aStrdup(str)))
  3025. ;
  3026. else if(name[1] != '@') { //put returned null, so we must insert.
  3027. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  3028. char tmp_str[32*2+1];
  3029. char tmp_str2[255*2+1];
  3030. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  3031. Sql_EscapeStringLen(mmysql_handle, tmp_str2, str, strnlen(str, 255));
  3032. 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) )
  3033. Sql_ShowDebug(mmysql_handle);
  3034. }
  3035. #else
  3036. if( str==NULL || *str==0 )
  3037. idb_remove(mapregstr_db,num);
  3038. else
  3039. idb_put(mapregstr_db,num,aStrdup(str));
  3040. #endif
  3041. mapreg_dirty = 1;
  3042. return 1;
  3043. }
  3044. /*==========================================
  3045. * 永続的マップ変数の読み込み
  3046. *------------------------------------------*/
  3047. static int script_load_mapreg(void)
  3048. {
  3049. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3050. FILE* fp;
  3051. char line[1024];
  3052. if( (fp=fopen(mapreg_txt,"rt")) == NULL )
  3053. return -1;
  3054. while(fgets(line,sizeof(line),fp))
  3055. {
  3056. char buf1[256],buf2[1024];
  3057. char* p;
  3058. int n,v,s,i;
  3059. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  3060. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  3061. continue;
  3062. if( buf1[strlen(buf1)-1] == '$' ) {
  3063. if( sscanf(line + n, "%[^\n\r]", buf2) != 1 ) {
  3064. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3065. continue;
  3066. }
  3067. p = aStrdup(buf2);
  3068. s = add_str(buf1);
  3069. idb_put(mapregstr_db, (i<<24)|s, p);
  3070. } else {
  3071. if( sscanf(line + n, "%d", &v) != 1 ) {
  3072. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3073. continue;
  3074. }
  3075. s = add_str(buf1);
  3076. idb_put(mapreg_db, (i<<24)|s, (void*)v);
  3077. }
  3078. }
  3079. fclose(fp);
  3080. mapreg_dirty = 0;
  3081. return 0;
  3082. #else
  3083. /*
  3084. 0 1 2
  3085. +-------------------------+
  3086. | varname | index | value |
  3087. +-------------------------+
  3088. */
  3089. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  3090. char varname[32+1];
  3091. int index;
  3092. char value[255+1];
  3093. uint32 length;
  3094. if ( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `%s`, `%s`, `%s` FROM `%s`", mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, mapregsql_db)
  3095. || SQL_ERROR == SqlStmt_Execute(stmt)
  3096. ) {
  3097. SqlStmt_ShowDebug(stmt);
  3098. SqlStmt_Free(stmt);
  3099. return -1;
  3100. }
  3101. SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &varname[0], 32, &length, NULL);
  3102. SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
  3103. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &value[0], 255, NULL, NULL);
  3104. while ( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  3105. {
  3106. if( varname[length-1] == '$' ) {
  3107. int s = add_str(varname);
  3108. int i = index;
  3109. char* p = aStrdup(value);
  3110. idb_put(mapregstr_db, (i<<24)|s, p);
  3111. } else {
  3112. int s = add_str(varname);
  3113. int i = index;
  3114. int v = atoi(value);
  3115. idb_put(mapreg_db, (i<<24)|s, (void *)v);
  3116. }
  3117. }
  3118. SqlStmt_Free(stmt);
  3119. mapreg_dirty = 0;
  3120. return 0;
  3121. #endif /* TXT_ONLY */
  3122. }
  3123. /*==========================================
  3124. * 永続的マップ変数の書き込み
  3125. *------------------------------------------*/
  3126. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  3127. {
  3128. int num=key.i&0x00ffffff, i=key.i>>24;
  3129. char *name=str_buf+str_data[num].str;
  3130. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3131. FILE *fp=va_arg(ap,FILE*);
  3132. if( name[1]!='@' ) {
  3133. if(i==0)
  3134. fprintf(fp,"%s\t%d\n", name, (int)data);
  3135. else
  3136. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  3137. }
  3138. #else
  3139. if ( name[1] != '@') {
  3140. 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) )
  3141. Sql_ShowDebug(mmysql_handle);
  3142. }
  3143. #endif
  3144. return 0;
  3145. }
  3146. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  3147. {
  3148. int num=key.i&0x00ffffff, i=key.i>>24;
  3149. char *name=str_buf+str_data[num].str;
  3150. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3151. FILE *fp=va_arg(ap,FILE*);
  3152. if( name[1]!='@' ) {
  3153. if(i==0)
  3154. fprintf(fp,"%s\t%s\n", name, (char *)data);
  3155. else
  3156. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  3157. }
  3158. #else
  3159. if ( name[1] != '@') {
  3160. char tmp_str2[2*255+1];
  3161. Sql_EscapeStringLen(mmysql_handle, tmp_str2, (char*)data, safestrnlen((char*)data, 255));
  3162. 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) )
  3163. Sql_ShowDebug(mmysql_handle);
  3164. }
  3165. #endif
  3166. return 0;
  3167. }
  3168. static int script_save_mapreg(void)
  3169. {
  3170. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3171. FILE *fp;
  3172. int lock;
  3173. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  3174. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  3175. return -1;
  3176. }
  3177. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  3178. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  3179. lock_fclose(fp,mapreg_txt,&lock);
  3180. #else
  3181. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub);
  3182. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  3183. #endif
  3184. mapreg_dirty=0;
  3185. return 0;
  3186. }
  3187. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  3188. {
  3189. if(mapreg_dirty)
  3190. if (script_save_mapreg() == -1)
  3191. ShowError("Failed to save the mapreg data!\n");
  3192. return 0;
  3193. }
  3194. int script_config_read(char *cfgName)
  3195. {
  3196. int i;
  3197. char line[1024],w1[1024],w2[1024];
  3198. FILE *fp;
  3199. fp=fopen(cfgName,"r");
  3200. if(fp==NULL){
  3201. ShowError("file not found: [%s]\n", cfgName);
  3202. return 1;
  3203. }
  3204. while(fgets(line, sizeof(line), fp))
  3205. {
  3206. if(line[0] == '/' && line[1] == '/')
  3207. continue;
  3208. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3209. if(i!=2)
  3210. continue;
  3211. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3212. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3213. }
  3214. else if(strcmpi(w1,"check_cmdcount")==0) {
  3215. script_config.check_cmdcount = config_switch(w2);
  3216. }
  3217. else if(strcmpi(w1,"check_gotocount")==0) {
  3218. script_config.check_gotocount = config_switch(w2);
  3219. }
  3220. else if(strcmpi(w1,"import")==0){
  3221. script_config_read(w2);
  3222. }
  3223. }
  3224. fclose(fp);
  3225. return 0;
  3226. }
  3227. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3228. {
  3229. struct script_code *code = (struct script_code *)data;
  3230. if(code){
  3231. script_free_vars( &code->script_vars );
  3232. aFree( code->script_buf );
  3233. aFree( code );
  3234. }
  3235. return 0;
  3236. }
  3237. /*==========================================
  3238. * 終了
  3239. *------------------------------------------*/
  3240. int do_final_script()
  3241. {
  3242. #ifdef DEBUG_HASH
  3243. if (battle_config.etc_log)
  3244. {
  3245. FILE *fp = fopen("hash_dump.txt","wt");
  3246. if(fp) {
  3247. int i,count[SCRIPT_HASH_SIZE];
  3248. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3249. int n=0;
  3250. int min=INT_MAX,max=0,zero=0;
  3251. double mean=0.0f;
  3252. double median=0.0f;
  3253. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3254. memset(count, 0, sizeof(count));
  3255. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3256. fprintf(fp,"---------------------------------------------------------------\n");
  3257. for(i=LABEL_START; i<str_num; i++) {
  3258. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3259. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3260. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3261. ++count[h];
  3262. }
  3263. fprintf(fp,"--------------------\n\n");
  3264. memset(count2, 0, sizeof(count2));
  3265. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3266. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3267. if(min > count[i])
  3268. min = count[i]; // minimun count of collision
  3269. if(max < count[i])
  3270. max = count[i]; // maximun count of collision
  3271. if(count[i] == 0)
  3272. zero++;
  3273. ++count2[count[i]];
  3274. }
  3275. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3276. for( i=min; i <= max; ++i ){
  3277. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3278. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3279. }
  3280. for( i=min; i <= max; ++i ){
  3281. n += count2[i];
  3282. if( n*2 >= SCRIPT_HASH_SIZE )
  3283. {
  3284. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3285. median = (i+i+1)/2.0f;
  3286. else
  3287. median = i;
  3288. break;
  3289. }
  3290. }
  3291. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3292. fclose(fp);
  3293. }
  3294. }
  3295. #endif
  3296. if(mapreg_dirty>=0)
  3297. script_save_mapreg();
  3298. mapreg_db->destroy(mapreg_db,NULL);
  3299. mapregstr_db->destroy(mapregstr_db,NULL);
  3300. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3301. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3302. if(sleep_db) {
  3303. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3304. while(n) {
  3305. struct script_state *st = (struct script_state *)n->data;
  3306. script_free_stack(st->stack);
  3307. aFree(st);
  3308. n = n->next;
  3309. }
  3310. linkdb_final(&sleep_db);
  3311. }
  3312. if (str_data)
  3313. aFree(str_data);
  3314. if (str_buf)
  3315. aFree(str_buf);
  3316. return 0;
  3317. }
  3318. /*==========================================
  3319. * 初期化
  3320. *------------------------------------------*/
  3321. int do_init_script()
  3322. {
  3323. mapreg_db= idb_alloc(DB_OPT_BASE);
  3324. mapregstr_db=idb_alloc(DB_OPT_RELEASE_DATA);
  3325. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3326. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA,50);
  3327. script_load_mapreg();
  3328. add_timer_func_list(script_autosave_mapreg, "script_autosave_mapreg");
  3329. add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL, script_autosave_mapreg, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
  3330. return 0;
  3331. }
  3332. int script_reload()
  3333. {
  3334. if(mapreg_dirty>=0)
  3335. script_save_mapreg();
  3336. mapreg_db->clear(mapreg_db, NULL);
  3337. mapregstr_db->clear(mapregstr_db, NULL);
  3338. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3339. scriptlabel_db->clear(scriptlabel_db, NULL);
  3340. if(sleep_db) {
  3341. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3342. while(n) {
  3343. struct script_state *st = (struct script_state *)n->data;
  3344. if( st->sleep.timer != INVALID_TIMER )
  3345. delete_timer(st->sleep.timer, run_script_timer);
  3346. script_free_stack(st->stack);
  3347. aFree(st);
  3348. n = n->next;
  3349. }
  3350. linkdb_final(&sleep_db);
  3351. }
  3352. script_load_mapreg();
  3353. return 0;
  3354. }
  3355. //-----------------------------------------------------------------------------
  3356. // buildin functions
  3357. //
  3358. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3359. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3360. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3361. /////////////////////////////////////////////////////////////////////
  3362. // NPC interaction
  3363. //
  3364. /// Appends a message to the npc dialog.
  3365. /// If a dialog doesn't exist yet, one is created.
  3366. ///
  3367. /// mes "<message>";
  3368. BUILDIN_FUNC(mes)
  3369. {
  3370. TBL_PC* sd = script_rid2sd(st);
  3371. if( sd == NULL )
  3372. return 0;
  3373. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3374. return 0;
  3375. }
  3376. /// Displays the button 'next' in the npc dialog.
  3377. /// The dialog text is cleared and the script continues when the button is pressed.
  3378. ///
  3379. /// next;
  3380. BUILDIN_FUNC(next)
  3381. {
  3382. TBL_PC* sd;
  3383. sd = script_rid2sd(st);
  3384. if( sd == NULL )
  3385. return 0;
  3386. st->state = STOP;
  3387. clif_scriptnext(sd, st->oid);
  3388. return 0;
  3389. }
  3390. /// Ends the script and displays the button 'close' on the npc dialog.
  3391. /// The dialog is closed when the button is pressed.
  3392. ///
  3393. /// close;
  3394. BUILDIN_FUNC(close)
  3395. {
  3396. TBL_PC* sd;
  3397. sd = script_rid2sd(st);
  3398. if( sd == NULL )
  3399. return 0;
  3400. st->state = END;
  3401. clif_scriptclose(sd, st->oid);
  3402. return 0;
  3403. }
  3404. /// Displays the button 'close' on the npc dialog.
  3405. /// The dialog is closed and the script continues when the button is pressed.
  3406. ///
  3407. /// close2;
  3408. BUILDIN_FUNC(close2)
  3409. {
  3410. TBL_PC* sd;
  3411. sd = script_rid2sd(st);
  3412. if( sd == NULL )
  3413. return 0;
  3414. st->state = STOP;
  3415. clif_scriptclose(sd, st->oid);
  3416. return 0;
  3417. }
  3418. /// Counts the number of valid and total number of options in 'str'
  3419. /// If max_count > 0 the counting stops when that valid option is reached
  3420. /// total is incremented for each option (NULL is supported)
  3421. static int menu_countoptions(const char* str, int max_count, int* total)
  3422. {
  3423. int count = 0;
  3424. int bogus_total;
  3425. if( total == NULL )
  3426. total = &bogus_total;
  3427. ++(*total);
  3428. // initial empty options
  3429. while( *str == ':' )
  3430. {
  3431. ++str;
  3432. ++(*total);
  3433. }
  3434. // count menu options
  3435. while( *str != '\0' )
  3436. {
  3437. ++count;
  3438. --max_count;
  3439. if( max_count == 0 )
  3440. break;
  3441. while( *str != ':' && *str != '\0' )
  3442. ++str;
  3443. while( *str == ':' )
  3444. {
  3445. ++str;
  3446. ++(*total);
  3447. }
  3448. }
  3449. return count;
  3450. }
  3451. /// Displays a menu with options and goes to the target label.
  3452. /// The script is stopped if cancel is pressed.
  3453. /// Options with no text are not displayed in the client.
  3454. ///
  3455. /// Options can be grouped together, separated by the character ':' in the text:
  3456. /// ex: menu "A:B:C",L_target;
  3457. /// All these options go to the specified target label.
  3458. ///
  3459. /// The index of the selected option is put in the variable @menu.
  3460. /// Indexes start with 1 and are consistent with grouped and empty options.
  3461. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3462. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3463. ///
  3464. /// NOTE: the client closes the npc dialog when cancel is pressed
  3465. ///
  3466. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3467. BUILDIN_FUNC(menu)
  3468. {
  3469. int i;
  3470. const char* text;
  3471. TBL_PC* sd;
  3472. sd = script_rid2sd(st);
  3473. if( sd == NULL )
  3474. return 0;
  3475. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3476. if( sd->state.menu_or_input == 0 )
  3477. {
  3478. struct StringBuf buf;
  3479. struct script_data* data;
  3480. if( script_lastdata(st) % 2 == 0 )
  3481. {// argument count is not even (1st argument is at index 2)
  3482. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3483. st->state = END;
  3484. return 1;
  3485. }
  3486. StringBuf_Init(&buf);
  3487. for( i = 2, sd->npc_menu = 0; i < script_lastdata(st); i += 2 )
  3488. {
  3489. // menu options
  3490. text = script_getstr(st, i);
  3491. // target label
  3492. data = script_getdata(st, i+1);
  3493. if( !data_islabel(data) )
  3494. {// not a label
  3495. StringBuf_Destroy(&buf);
  3496. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3497. script_reportdata(data);
  3498. st->state = END;
  3499. return 1;
  3500. }
  3501. // append option(s)
  3502. if( text[0] == '\0' )
  3503. continue;// empty string, ignore
  3504. if( sd->npc_menu > 0 )
  3505. StringBuf_AppendStr(&buf, ":");
  3506. StringBuf_AppendStr(&buf, text);
  3507. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3508. }
  3509. st->state = RERUNLINE;
  3510. sd->state.menu_or_input = 1;
  3511. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3512. StringBuf_Destroy(&buf);
  3513. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3514. }
  3515. else if( sd->npc_menu == 0xff )
  3516. {// Cancel was pressed
  3517. sd->state.menu_or_input = 0;
  3518. st->state = END;
  3519. }
  3520. else
  3521. {// goto target label
  3522. int menu = 0;
  3523. sd->state.menu_or_input = 0;
  3524. if( sd->npc_menu <= 0 )
  3525. {
  3526. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3527. st->state = END;
  3528. return 1;
  3529. }
  3530. // get target label
  3531. for( i = 2; i < script_lastdata(st); i += 2 )
  3532. {
  3533. text = script_getstr(st, i);
  3534. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3535. if( sd->npc_menu <= 0 )
  3536. break;// entry found
  3537. }
  3538. if( sd->npc_menu > 0 )
  3539. {// Invalid selection
  3540. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3541. st->state = END;
  3542. return 1;
  3543. }
  3544. if( !data_islabel(script_getdata(st, i + 1)) )
  3545. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3546. ShowError("script:menu: unexpected data in label argument\n");
  3547. script_reportdata(script_getdata(st, i + 1));
  3548. st->state = END;
  3549. return 1;
  3550. }
  3551. pc_setreg(sd, add_str("@menu"), menu);
  3552. st->pos = script_getnum(st, i + 1);
  3553. st->state = GOTO;
  3554. }
  3555. return 0;
  3556. }
  3557. /// Displays a menu with options and returns the selected option.
  3558. /// Behaves like 'menu' without the target labels.
  3559. ///
  3560. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3561. ///
  3562. /// @see menu
  3563. BUILDIN_FUNC(select)
  3564. {
  3565. int i;
  3566. const char* text;
  3567. TBL_PC* sd;
  3568. sd = script_rid2sd(st);
  3569. if( sd == NULL )
  3570. return 0;
  3571. if( sd->state.menu_or_input == 0 )
  3572. {
  3573. struct StringBuf buf;
  3574. StringBuf_Init(&buf);
  3575. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3576. {
  3577. text = script_getstr(st, i);
  3578. if( sd->npc_menu > 0 )
  3579. StringBuf_AppendStr(&buf, ":");
  3580. StringBuf_AppendStr(&buf, text);
  3581. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3582. }
  3583. st->state = RERUNLINE;
  3584. sd->state.menu_or_input = 1;
  3585. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3586. StringBuf_Destroy(&buf);
  3587. }
  3588. else if( sd->npc_menu == 0xff )
  3589. {// Cancel was pressed
  3590. sd->state.menu_or_input = 0;
  3591. st->state = END;
  3592. }
  3593. else
  3594. {// return selected option
  3595. int menu = 0;
  3596. sd->state.menu_or_input = 0;
  3597. for( i = 2; i <= script_lastdata(st); ++i )
  3598. {
  3599. text = script_getstr(st, i);
  3600. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3601. if( sd->npc_menu <= 0 )
  3602. break;// entry found
  3603. }
  3604. pc_setreg(sd, add_str("@menu"), menu);
  3605. script_pushint(st, menu);
  3606. }
  3607. return 0;
  3608. }
  3609. /// Displays a menu with options and returns the selected option.
  3610. /// Behaves like 'menu' without the target labels, except when cancel is
  3611. /// pressed.
  3612. /// When cancel is pressed, the script continues and 255 is returned.
  3613. ///
  3614. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3615. ///
  3616. /// @see menu
  3617. BUILDIN_FUNC(prompt)
  3618. {
  3619. int i;
  3620. const char *text;
  3621. TBL_PC* sd;
  3622. sd = script_rid2sd(st);
  3623. if( sd == NULL )
  3624. return 0;
  3625. if( sd->state.menu_or_input == 0 )
  3626. {
  3627. struct StringBuf buf;
  3628. StringBuf_Init(&buf);
  3629. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3630. {
  3631. text = script_getstr(st, i);
  3632. if( sd->npc_menu > 0 )
  3633. StringBuf_AppendStr(&buf, ":");
  3634. StringBuf_AppendStr(&buf, text);
  3635. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3636. }
  3637. st->state = RERUNLINE;
  3638. sd->state.menu_or_input = 1;
  3639. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3640. StringBuf_Destroy(&buf);
  3641. }
  3642. else if( sd->npc_menu == 0xff )
  3643. {// Cancel was pressed
  3644. sd->state.menu_or_input = 0;
  3645. pc_setreg(sd, add_str("@menu"), 0xff);
  3646. script_pushint(st, 0xff);
  3647. }
  3648. else
  3649. {// return selected option
  3650. int menu = 0;
  3651. sd->state.menu_or_input = 0;
  3652. for( i = 2; i <= script_lastdata(st); ++i )
  3653. {
  3654. text = script_getstr(st, i);
  3655. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3656. if( sd->npc_menu <= 0 )
  3657. break;// entry found
  3658. }
  3659. pc_setreg(sd, add_str("@menu"), menu);
  3660. script_pushint(st, menu);
  3661. }
  3662. return 0;
  3663. }
  3664. /////////////////////////////////////////////////////////////////////
  3665. // ...
  3666. //
  3667. /// Jumps to the target script label.
  3668. ///
  3669. /// goto <label>;
  3670. BUILDIN_FUNC(goto)
  3671. {
  3672. if( !data_islabel(script_getdata(st,2)) )
  3673. {
  3674. ShowError("script:goto: not a label\n");
  3675. script_reportdata(script_getdata(st,2));
  3676. st->state = END;
  3677. return 1;
  3678. }
  3679. st->pos = script_getnum(st,2);
  3680. st->state = GOTO;
  3681. return 0;
  3682. }
  3683. /*==========================================
  3684. * ユーザー定義関数の呼び出し
  3685. *------------------------------------------*/
  3686. BUILDIN_FUNC(callfunc)
  3687. {
  3688. int i, j;
  3689. struct linkdb_node** oldval;
  3690. struct script_code *scr, *oldscr;
  3691. const char* str = script_getstr(st,2);
  3692. scr = strdb_get(userfunc_db, str);
  3693. if( !scr )
  3694. {
  3695. ShowError("script:callfunc: function not found! [%s]\n", str);
  3696. st->state = END;
  3697. return 1;
  3698. }
  3699. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3700. push_copy(st->stack,i);
  3701. script_pushint(st,j); // 引数の数をプッシュ
  3702. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3703. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3704. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3705. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3706. oldscr = st->script;
  3707. oldval = st->stack->var_function;
  3708. st->pos = 0;
  3709. st->script = scr;
  3710. st->stack->defsp = st->start+5+j;
  3711. st->state = GOTO;
  3712. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3713. // ' 変数の引き継ぎ
  3714. for( i = 0; i < j; i++ )
  3715. {
  3716. struct script_data* s = script_getdatatop(st, -6-i);
  3717. if( data_isreference(s) && !s->ref )
  3718. {
  3719. char* name = str_buf + str_data[s->u.num&0x00ffffff].str;
  3720. // '@ 変数の引き継ぎ
  3721. if( name[0] == '.' && name[1] == '@' ) {
  3722. s->ref = oldval;
  3723. } else if( name[0] == '.' ) {
  3724. s->ref = &oldscr->script_vars;
  3725. }
  3726. }
  3727. }
  3728. return 0;
  3729. }
  3730. /*==========================================
  3731. * サブルーティンの呼び出し
  3732. *------------------------------------------*/
  3733. BUILDIN_FUNC(callsub)
  3734. {
  3735. int pos=script_getnum(st,2);
  3736. int i,j;
  3737. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  3738. ShowError("script:callsub: argument is not a label\n");
  3739. script_reportdata(script_getdata(st,2));
  3740. st->state=END;
  3741. return 1;
  3742. } else {
  3743. struct linkdb_node **oldval = st->stack->var_function;
  3744. for(i=st->start+3,j=0;i<st->end;i++,j++)
  3745. push_copy(st->stack,i);
  3746. script_pushint(st,j); // 引数の数をプッシュ
  3747. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3748. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3749. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3750. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3751. st->pos=pos;
  3752. st->stack->defsp=st->start+5+j;
  3753. st->state=GOTO;
  3754. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3755. // ' 変数の引き継ぎ
  3756. for(i = 0; i < j; i++) {
  3757. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  3758. if( data_isreference(s) && !s->ref ) {
  3759. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  3760. // '@ 変数の引き継ぎ
  3761. if( name[0] == '.' && name[1] == '@' ) {
  3762. s->ref = oldval;
  3763. }
  3764. }
  3765. }
  3766. }
  3767. return 0;
  3768. }
  3769. /// Retrieves an argument provided to callfunc/callsub.
  3770. /// If the argument doesn't exist
  3771. ///
  3772. /// getarg(<index>{,<default_value>}) -> <value>
  3773. BUILDIN_FUNC(getarg)
  3774. {
  3775. int idx;
  3776. int count;
  3777. int stsp;
  3778. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3779. {
  3780. ShowError("script:getarg: no callfunc or callsub!\n");
  3781. st->state = END;
  3782. return 1;
  3783. }
  3784. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  3785. stsp = st->stack->defsp - count - 5;
  3786. idx = script_getnum(st,2);
  3787. if( idx < count )
  3788. push_copy(st->stack, stsp + idx);
  3789. else if( script_hasdata(st,3) )
  3790. script_pushcopy(st, 3);
  3791. else
  3792. {
  3793. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  3794. st->state = END;
  3795. return 1;
  3796. }
  3797. return 0;
  3798. }
  3799. /// Returns from the current function, optionaly returning a value from the functions.
  3800. /// Don't use outside script functions.
  3801. ///
  3802. /// return;
  3803. /// return <value>;
  3804. BUILDIN_FUNC(return)
  3805. {
  3806. if( script_hasdata(st,2) )
  3807. {// return value
  3808. struct script_data* data;
  3809. script_pushcopy(st, 2);
  3810. data = script_getdatatop(st, -1);
  3811. if( data_isreference(data) )
  3812. {
  3813. char* name = reference_getname(data);
  3814. if( name[0] == '.' && name[1] == '@' )
  3815. {// temporary script variable, convert to value
  3816. get_val(st, data);
  3817. if( data_isstring(data) )
  3818. {// duplicate the string
  3819. data->type = C_STR;
  3820. data->u.str = aStrdup(data->u.str);
  3821. }
  3822. }
  3823. else if( name[0] == '.' && !data->ref )
  3824. {// script variable, link to current script
  3825. data->ref = &st->script->script_vars;
  3826. }
  3827. }
  3828. }
  3829. else
  3830. {// no return value
  3831. script_pushnil(st);
  3832. }
  3833. st->state = RETFUNC;
  3834. return 0;
  3835. }
  3836. /// Returns a random number from 0 to <range>-1.
  3837. /// Or returns a random number from <min> to <max>.
  3838. /// If <min> is greater than <max>, their numbers are switched.
  3839. /// rand(<range>) -> <int>
  3840. /// rand(<min>,<max>) -> <int>
  3841. BUILDIN_FUNC(rand)
  3842. {
  3843. int range;
  3844. int min;
  3845. int max;
  3846. if( script_hasdata(st,3) )
  3847. {// min,max
  3848. min = script_getnum(st,2);
  3849. max = script_getnum(st,3);
  3850. if( max < min )
  3851. swap(min, max);
  3852. range = max - min + 1;
  3853. }
  3854. else
  3855. {// range
  3856. min = 0;
  3857. range = script_getnum(st,2);
  3858. }
  3859. if( range <= 1 )
  3860. script_pushint(st, min);
  3861. else
  3862. script_pushint(st, rand()%range + min);
  3863. return 0;
  3864. }
  3865. /*==========================================
  3866. *
  3867. *------------------------------------------*/
  3868. BUILDIN_FUNC(warp)
  3869. {
  3870. int ret;
  3871. int x,y;
  3872. const char* str;
  3873. TBL_PC* sd;
  3874. sd = script_rid2sd(st);
  3875. if( sd == NULL )
  3876. return 0;
  3877. str = script_getstr(st,2);
  3878. x = script_getnum(st,3);
  3879. y = script_getnum(st,4);
  3880. if(strcmp(str,"Random")==0)
  3881. ret = pc_randomwarp(sd,3);
  3882. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3883. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3884. else
  3885. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3886. if( ret ) {
  3887. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3888. script_reportsrc(st);
  3889. }
  3890. return 0;
  3891. }
  3892. /*==========================================
  3893. * エリア指定ワープ
  3894. *------------------------------------------*/
  3895. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3896. {
  3897. int x,y;
  3898. unsigned int map;
  3899. map=va_arg(ap, unsigned int);
  3900. x=va_arg(ap,int);
  3901. y=va_arg(ap,int);
  3902. if(map == 0)
  3903. pc_randomwarp((TBL_PC *)bl,3);
  3904. else
  3905. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3906. return 0;
  3907. }
  3908. BUILDIN_FUNC(areawarp)
  3909. {
  3910. int x,y,m;
  3911. unsigned int index;
  3912. const char *str;
  3913. const char *mapname;
  3914. int x0,y0,x1,y1;
  3915. mapname=script_getstr(st,2);
  3916. x0=script_getnum(st,3);
  3917. y0=script_getnum(st,4);
  3918. x1=script_getnum(st,5);
  3919. y1=script_getnum(st,6);
  3920. str=script_getstr(st,7);
  3921. x=script_getnum(st,8);
  3922. y=script_getnum(st,9);
  3923. if( (m=map_mapname2mapid(mapname))< 0)
  3924. return 0;
  3925. if(strcmp(str,"Random")==0)
  3926. index = 0;
  3927. else if(!(index=mapindex_name2id(str)))
  3928. return 0;
  3929. map_foreachinarea(buildin_areawarp_sub,
  3930. m,x0,y0,x1,y1,BL_PC, index,x,y );
  3931. return 0;
  3932. }
  3933. /*==========================================
  3934. * warpchar [LuzZza]
  3935. * Useful for warp one player from
  3936. * another player npc-session.
  3937. * Using: warpchar "mapname",x,y,Char_ID;
  3938. *------------------------------------------*/
  3939. BUILDIN_FUNC(warpchar)
  3940. {
  3941. int x,y,a,i;
  3942. const char *str;
  3943. TBL_PC *sd, **pl_allsd;
  3944. int users;
  3945. str=script_getstr(st,2);
  3946. x=script_getnum(st,3);
  3947. y=script_getnum(st,4);
  3948. a=script_getnum(st,5);
  3949. pl_allsd = map_getallusers(&users);
  3950. for(i=0; i<users; i++) {
  3951. sd = pl_allsd[i];
  3952. if(sd->status.char_id == a) {
  3953. if(strcmp(str, "Random") == 0)
  3954. pc_randomwarp(sd, 3);
  3955. else if(strcmp(str, "SavePoint") == 0)
  3956. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3957. else
  3958. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3959. }
  3960. }
  3961. return 0;
  3962. }
  3963. /*==========================================
  3964. * Warpparty - [Fredzilla]
  3965. * Syntax: warpparty "mapname",x,y,Party_ID;
  3966. *------------------------------------------*/
  3967. BUILDIN_FUNC(warpparty)
  3968. {
  3969. int x,y;
  3970. const char *str;
  3971. int p_id;
  3972. int i;
  3973. unsigned short mapindex;
  3974. TBL_PC *pl_sd;
  3975. struct party_data *p=NULL;
  3976. str=script_getstr(st,2);
  3977. x=script_getnum(st,3);
  3978. y=script_getnum(st,4);
  3979. p_id=script_getnum(st,5);
  3980. if(p_id < 1)
  3981. return 0;
  3982. p = party_search(p_id);
  3983. if (!p)
  3984. return 0;
  3985. if(strcmp(str,"Random")==0)
  3986. {
  3987. for (i = 0; i < MAX_PARTY; i++)
  3988. {
  3989. if ((pl_sd = p->data[i].sd))
  3990. {
  3991. if(map[pl_sd->bl.m].flag.nowarp)
  3992. continue;
  3993. pc_randomwarp(pl_sd,3);
  3994. }
  3995. }
  3996. }
  3997. else if(strcmp(str,"SavePointAll")==0)
  3998. {
  3999. for (i = 0; i < MAX_PARTY; i++)
  4000. {
  4001. if ((pl_sd = p->data[i].sd))
  4002. {
  4003. if(map[pl_sd->bl.m].flag.noreturn)
  4004. continue;
  4005. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4006. }
  4007. }
  4008. }
  4009. else if(strcmp(str,"SavePoint")==0)
  4010. {
  4011. pl_sd=script_rid2sd(st);
  4012. if (!pl_sd) return 0;
  4013. mapindex=pl_sd->status.save_point.map;
  4014. x=pl_sd->status.save_point.x;
  4015. y=pl_sd->status.save_point.y;
  4016. for (i = 0; i < MAX_PARTY; i++)
  4017. {
  4018. if ((pl_sd = p->data[i].sd))
  4019. {
  4020. if(map[pl_sd->bl.m].flag.noreturn)
  4021. continue;
  4022. pc_setpos(pl_sd,mapindex,x,y,3);
  4023. }
  4024. }
  4025. }
  4026. else if(strcmp(str,"Leader")==0)
  4027. {
  4028. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4029. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4030. return 0;
  4031. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  4032. return 0;
  4033. mapindex = p->data[i].sd->mapindex;
  4034. x = p->data[i].sd->bl.x;
  4035. y = p->data[i].sd->bl.y;
  4036. for (i = 0; i < MAX_PARTY; i++)
  4037. {
  4038. pl_sd = p->data[i].sd;
  4039. if (!pl_sd)
  4040. continue;
  4041. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4042. continue;
  4043. pc_setpos(pl_sd,mapindex,x,y,3);
  4044. }
  4045. }
  4046. else
  4047. {
  4048. mapindex = mapindex_name2id(str);
  4049. if (!mapindex) //Show source of npc error.
  4050. return 1;
  4051. for (i = 0; i < MAX_PARTY; i++)
  4052. {
  4053. if ((pl_sd = p->data[i].sd))
  4054. {
  4055. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4056. continue;
  4057. pc_setpos(pl_sd,mapindex,x,y,3);
  4058. }
  4059. }
  4060. }
  4061. return 0;
  4062. }
  4063. /*==========================================
  4064. * Warpguild - [Fredzilla]
  4065. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4066. *------------------------------------------*/
  4067. BUILDIN_FUNC(warpguild)
  4068. {
  4069. int x,y;
  4070. unsigned short mapindex;
  4071. const char *str;
  4072. int g;
  4073. int i;
  4074. TBL_PC *pl_sd, **pl_allsd;
  4075. int users;
  4076. TBL_PC *sd;
  4077. str=script_getstr(st,2);
  4078. x=script_getnum(st,3);
  4079. y=script_getnum(st,4);
  4080. g=script_getnum(st,5);
  4081. sd=script_rid2sd(st);
  4082. if( sd == NULL )
  4083. return 0;
  4084. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4085. return 0;
  4086. if(g < 1)
  4087. return 0;
  4088. pl_allsd = map_getallusers(&users);
  4089. if(strcmp(str,"Random")==0)
  4090. {
  4091. for (i = 0; i < users; i++)
  4092. {
  4093. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4094. {
  4095. if(map[pl_sd->bl.m].flag.nowarp)
  4096. continue;
  4097. pc_randomwarp(pl_sd,3);
  4098. }
  4099. }
  4100. }
  4101. else if(strcmp(str,"SavePointAll")==0)
  4102. {
  4103. if(map[sd->bl.m].flag.noreturn)
  4104. return 0;
  4105. for (i = 0; i < users; i++)
  4106. {
  4107. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4108. {
  4109. if(map[pl_sd->bl.m].flag.noreturn)
  4110. continue;
  4111. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4112. }
  4113. }
  4114. }
  4115. else if(strcmp(str,"SavePoint")==0)
  4116. {
  4117. if(map[sd->bl.m].flag.noreturn)
  4118. return 0;
  4119. mapindex=sd->status.save_point.map;
  4120. x=sd->status.save_point.x;
  4121. y=sd->status.save_point.y;
  4122. for (i = 0; i < users; i++)
  4123. {
  4124. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4125. {
  4126. if(map[pl_sd->bl.m].flag.noreturn)
  4127. continue;
  4128. pc_setpos(pl_sd,mapindex,x,y,3);
  4129. }
  4130. }
  4131. }
  4132. else
  4133. {
  4134. mapindex = mapindex_name2id(str);
  4135. for (i = 0; i < users; i++)
  4136. {
  4137. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4138. {
  4139. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4140. continue;
  4141. pc_setpos(pl_sd,mapindex,x,y,3);
  4142. }
  4143. }
  4144. }
  4145. return 0;
  4146. }
  4147. /*==========================================
  4148. *
  4149. *------------------------------------------*/
  4150. BUILDIN_FUNC(heal)
  4151. {
  4152. TBL_PC *sd;
  4153. int hp,sp;
  4154. sd = script_rid2sd(st);
  4155. if (!sd) return 0;
  4156. hp=script_getnum(st,2);
  4157. sp=script_getnum(st,3);
  4158. status_heal(&sd->bl, hp, sp, 1);
  4159. return 0;
  4160. }
  4161. /*==========================================
  4162. *
  4163. *------------------------------------------*/
  4164. BUILDIN_FUNC(itemheal)
  4165. {
  4166. TBL_PC *sd;
  4167. int hp,sp;
  4168. hp=script_getnum(st,2);
  4169. sp=script_getnum(st,3);
  4170. if(potion_flag==1) {
  4171. potion_hp = hp;
  4172. potion_sp = sp;
  4173. return 0;
  4174. }
  4175. sd = script_rid2sd(st);
  4176. if (!sd) return 0;
  4177. pc_itemheal(sd,sd->itemid,hp,sp);
  4178. return 0;
  4179. }
  4180. /*==========================================
  4181. *
  4182. *------------------------------------------*/
  4183. BUILDIN_FUNC(percentheal)
  4184. {
  4185. int hp,sp;
  4186. TBL_PC* sd;
  4187. hp=script_getnum(st,2);
  4188. sp=script_getnum(st,3);
  4189. if(potion_flag==1) {
  4190. potion_per_hp = hp;
  4191. potion_per_sp = sp;
  4192. return 0;
  4193. }
  4194. sd = script_rid2sd(st);
  4195. if( sd == NULL )
  4196. return 0;
  4197. pc_percentheal(sd,hp,sp);
  4198. return 0;
  4199. }
  4200. /*==========================================
  4201. *
  4202. *------------------------------------------*/
  4203. BUILDIN_FUNC(jobchange)
  4204. {
  4205. int job, upper=-1;
  4206. job=script_getnum(st,2);
  4207. if( script_hasdata(st,3) )
  4208. upper=script_getnum(st,3);
  4209. if (pcdb_checkid(job))
  4210. {
  4211. TBL_PC* sd;
  4212. sd = script_rid2sd(st);
  4213. if( sd == NULL )
  4214. return 0;
  4215. pc_jobchange(sd, job, upper);
  4216. if(use_irc && irc_announce_jobchange_flag)
  4217. irc_announce_jobchange(sd);
  4218. }
  4219. return 0;
  4220. }
  4221. /*==========================================
  4222. *
  4223. *------------------------------------------*/
  4224. BUILDIN_FUNC(jobname)
  4225. {
  4226. int class_=script_getnum(st,2);
  4227. script_pushconststr(st,job_name(class_));
  4228. return 0;
  4229. }
  4230. /*==========================================
  4231. *
  4232. *------------------------------------------*/
  4233. BUILDIN_FUNC(input)
  4234. {
  4235. TBL_PC *sd = script_rid2sd(st);
  4236. struct script_data *data = script_getdata(st,2);
  4237. int num = data->u.num;
  4238. char *name=str_buf+str_data[num&0x00ffffff].str;
  4239. char postfix = name[strlen(name)-1];
  4240. if (!sd) return 0;
  4241. if( !data_isreference(data) ){
  4242. ShowError("script:input: not a variable\n");
  4243. script_reportdata(data);
  4244. return 1;
  4245. }
  4246. if( !sd->state.menu_or_input )
  4247. { // first invocation, display npc input box
  4248. sd->state.menu_or_input = 1;
  4249. st->state = RERUNLINE;
  4250. if( postfix == '$' )
  4251. clif_scriptinputstr(sd,st->oid);
  4252. else
  4253. clif_scriptinput(sd,st->oid);
  4254. }
  4255. else
  4256. { // take received text/value and store it in the designated variable
  4257. sd->state.menu_or_input = 0;
  4258. if( postfix == '$' )
  4259. {
  4260. set_reg(st,sd,num,name,(void*)sd->npc_str,script_getref(st,2));
  4261. }
  4262. else
  4263. {
  4264. // limit the input to a non-negative value smaller than 'vending_max_value' (for scripts that didn't check this)
  4265. sd->npc_amount = cap_value(sd->npc_amount, 0, battle_config.vending_max_value);
  4266. set_reg(st,sd,num,name,(void*)sd->npc_amount,script_getref(st,2));
  4267. }
  4268. }
  4269. return 0;
  4270. }
  4271. /*==========================================
  4272. * 変数設定
  4273. *------------------------------------------*/
  4274. BUILDIN_FUNC(set)
  4275. {
  4276. TBL_PC *sd=NULL;
  4277. int num=st->stack->stack_data[st->start+2].u.num;
  4278. char *name=str_buf+str_data[num&0x00ffffff].str;
  4279. char prefix=*name;
  4280. char postfix=name[strlen(name)-1];
  4281. if( !data_isreference(script_getdata(st,2)) ){
  4282. ShowError("script:set: not a variable\n");
  4283. script_reportdata(script_getdata(st,2));
  4284. st->state = END;
  4285. return 1;
  4286. }
  4287. if(not_server_variable(prefix))
  4288. {
  4289. sd=script_rid2sd(st);
  4290. if( sd == NULL )
  4291. {
  4292. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4293. return 0;
  4294. }
  4295. }
  4296. if( postfix=='$' ){
  4297. // 文字列
  4298. const char *str = script_getstr(st,3);
  4299. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  4300. }else{
  4301. // 数値
  4302. int val = script_getnum(st,3);
  4303. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  4304. }
  4305. return 0;
  4306. }
  4307. /////////////////////////////////////////////////////////////////////
  4308. /// Array variables
  4309. ///
  4310. /// Returns the size of the specified array
  4311. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4312. {
  4313. int32 ret = idx;
  4314. if( isstring )
  4315. {
  4316. for( ; idx < 128; ++idx )
  4317. {
  4318. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4319. if( str && *str )
  4320. ret = idx + 1;
  4321. script_removetop(st, -1, 0);
  4322. }
  4323. }
  4324. else
  4325. {
  4326. for( ; idx < 128; ++idx )
  4327. {
  4328. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4329. if( num )
  4330. ret = idx + 1;
  4331. script_removetop(st, -1, 0);
  4332. }
  4333. }
  4334. return ret;
  4335. }
  4336. /// Sets values of an array, from the starting index.
  4337. /// ex: setarray arr[1],1,2,3;
  4338. ///
  4339. /// setarray <array variable>,<value1>{,<value2>...};
  4340. BUILDIN_FUNC(setarray)
  4341. {
  4342. struct script_data* data;
  4343. char* name;
  4344. int32 start;
  4345. int32 end;
  4346. int32 id;
  4347. int32 i;
  4348. TBL_PC* sd = NULL;
  4349. data = script_getdata(st, 2);
  4350. if( !data_isreference(data) )
  4351. {
  4352. ShowError("script:setarray: not a variable\n");
  4353. script_reportdata(data);
  4354. st->state = END;
  4355. return 1;// not a variable
  4356. }
  4357. id = reference_getid(data);
  4358. start = reference_getindex(data);
  4359. name = reference_getname(data);
  4360. if( not_array_variable(*name) )
  4361. {
  4362. ShowError("script:setarray: illegal scope\n");
  4363. script_reportdata(data);
  4364. st->state = END;
  4365. return 1;// not supported
  4366. }
  4367. if( not_server_variable(*name) )
  4368. {
  4369. sd = script_rid2sd(st);
  4370. if( sd == NULL )
  4371. return 0;// no player attached
  4372. }
  4373. end = start + script_lastdata(st) - 2;
  4374. if( end > 127 )
  4375. end = 127;
  4376. if( is_string_variable(name) )
  4377. {// string array
  4378. for( i = 3; start < end; ++start, ++i )
  4379. {
  4380. void* v = (void*)script_getstr(st,i);
  4381. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4382. }
  4383. }
  4384. else
  4385. {// int array
  4386. for( i = 3; start < end; ++start, ++i )
  4387. {
  4388. void* v = (void*)script_getnum(st,i);
  4389. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4390. }
  4391. }
  4392. return 0;
  4393. }
  4394. /// Sets count values of an array, from the starting index.
  4395. /// ex: cleararray arr[0],0,1;
  4396. ///
  4397. /// cleararray <array variable>,<value>,<count>;
  4398. BUILDIN_FUNC(cleararray)
  4399. {
  4400. struct script_data* data;
  4401. char* name;
  4402. int32 start;
  4403. int32 end;
  4404. int32 id;
  4405. void* v;
  4406. TBL_PC* sd = NULL;
  4407. data = script_getdata(st, 2);
  4408. if( !data_isreference(data) )
  4409. {
  4410. ShowError("script:cleararray: not a variable\n");
  4411. script_reportdata(data);
  4412. st->state = END;
  4413. return 1;// not a variable
  4414. }
  4415. id = reference_getid(data);
  4416. start = reference_getindex(data);
  4417. name = reference_getname(data);
  4418. if( not_array_variable(*name) )
  4419. {
  4420. ShowError("script:cleararray: illegal scope\n");
  4421. script_reportdata(data);
  4422. st->state = END;
  4423. return 1;// not supported
  4424. }
  4425. if( not_server_variable(*name) )
  4426. {
  4427. sd = script_rid2sd(st);
  4428. if( sd == NULL )
  4429. return 0;// no player attached
  4430. }
  4431. if( is_string_variable(name) )
  4432. v = (void*)script_getstr(st, 3);
  4433. else
  4434. v = (void*)script_getnum(st, 3);
  4435. end = start + script_getnum(st, 4);
  4436. if( end > 127 )
  4437. end = 127;
  4438. for( ; start < end; ++start )
  4439. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4440. return 0;
  4441. }
  4442. /// Copies data from one array to another.
  4443. /// ex: copyarray arr[0],arr[2],2;
  4444. ///
  4445. /// copyarray <destination array variable>,<source array variable>,<count>;
  4446. BUILDIN_FUNC(copyarray)
  4447. {
  4448. struct script_data* data1;
  4449. struct script_data* data2;
  4450. char* name1;
  4451. char* name2;
  4452. int32 idx1;
  4453. int32 idx2;
  4454. int32 id1;
  4455. int32 id2;
  4456. void* v;
  4457. int32 i;
  4458. int32 count;
  4459. TBL_PC* sd = NULL;
  4460. data1 = script_getdata(st, 2);
  4461. data2 = script_getdata(st, 3);
  4462. if( !data_isreference(data1) || !data_isreference(data2) )
  4463. {
  4464. ShowError("script:copyarray: not a variable\n");
  4465. script_reportdata(data1);
  4466. script_reportdata(data2);
  4467. st->state = END;
  4468. return 1;// not a variable
  4469. }
  4470. id1 = reference_getid(data1);
  4471. id2 = reference_getid(data2);
  4472. idx1 = reference_getindex(data1);
  4473. idx2 = reference_getindex(data2);
  4474. name1 = reference_getname(data1);
  4475. name2 = reference_getname(data2);
  4476. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4477. {
  4478. ShowError("script:copyarray: illegal scope\n");
  4479. script_reportdata(data1);
  4480. script_reportdata(data2);
  4481. st->state = END;
  4482. return 1;// not supported
  4483. }
  4484. if( is_string_variable(name1) != is_string_variable(name2) )
  4485. {
  4486. ShowError("script:copyarray: type mismatch\n");
  4487. script_reportdata(data1);
  4488. script_reportdata(data2);
  4489. st->state = END;
  4490. return 1;// data type mismatch
  4491. }
  4492. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4493. {
  4494. sd = script_rid2sd(st);
  4495. if( sd == NULL )
  4496. return 0;// no player attached
  4497. }
  4498. count = script_getnum(st, 4);
  4499. if( count > 128 - idx1 )
  4500. count = 128 - idx1;
  4501. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4502. return 0;// nothing to copy
  4503. if( id1 == id2 && idx1 > idx2 )
  4504. {// destination might be overlapping the source - copy in reverse order
  4505. for( i = count - 1; i >= 0; --i )
  4506. {
  4507. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4508. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4509. script_removetop(st, -1, 0);
  4510. }
  4511. }
  4512. else
  4513. {// normal copy
  4514. for( i = 0; i < count; ++i )
  4515. {
  4516. if( idx2 + i < 128 )
  4517. {
  4518. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4519. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4520. script_removetop(st, -1, 0);
  4521. }
  4522. else// out of range - assume ""/0
  4523. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4524. }
  4525. }
  4526. return 0;
  4527. }
  4528. /// Returns the size of the array.
  4529. /// Assumes that everything before the starting index exists.
  4530. /// ex: getarraysize(arr[3])
  4531. ///
  4532. /// getarraysize(<array variable>) -> <int>
  4533. BUILDIN_FUNC(getarraysize)
  4534. {
  4535. struct script_data* data;
  4536. char* name;
  4537. data = script_getdata(st, 2);
  4538. if( !data_isreference(data) )
  4539. {
  4540. ShowError("script:getarraysize: not a variable\n");
  4541. script_reportdata(data);
  4542. script_pushnil(st);
  4543. st->state = END;
  4544. return 1;// not a variable
  4545. }
  4546. name = reference_getname(data);
  4547. if( not_array_variable(*name) )
  4548. {
  4549. ShowError("script:getarraysize: illegal scope\n");
  4550. script_reportdata(data);
  4551. script_pushnil(st);
  4552. st->state = END;
  4553. return 1;// not supported
  4554. }
  4555. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4556. return 0;
  4557. }
  4558. /// Deletes count or all the elements in an array, from the starting index.
  4559. /// ex: deletearray arr[4],2;
  4560. ///
  4561. /// deletearray <array variable>;
  4562. /// deletearray <array variable>,<count>;
  4563. BUILDIN_FUNC(deletearray)
  4564. {
  4565. struct script_data* data;
  4566. char* name;
  4567. int start;
  4568. int end;
  4569. int id;
  4570. TBL_PC *sd = NULL;
  4571. data = script_getdata(st, 2);
  4572. if( !data_isreference(data) )
  4573. {
  4574. ShowError("script:deletearray: not a variable\n");
  4575. script_reportdata(data);
  4576. st->state = END;
  4577. return 1;// not a variable
  4578. }
  4579. id = reference_getid(data);
  4580. start = reference_getindex(data);
  4581. name = reference_getname(data);
  4582. if( not_array_variable(*name) )
  4583. {
  4584. ShowError("script:deletearray: illegal scope\n");
  4585. script_reportdata(data);
  4586. st->state = END;
  4587. return 1;// not supported
  4588. }
  4589. if( not_server_variable(*name) )
  4590. {
  4591. sd = script_rid2sd(st);
  4592. if( sd == NULL )
  4593. return 0;// no player attached
  4594. }
  4595. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4596. if( start >= end )
  4597. return 0;// nothing to free
  4598. if( script_hasdata(st,3) )
  4599. {
  4600. int count = script_getnum(st, 3);
  4601. if( count > end - start )
  4602. count = end - start;
  4603. if( count <= 0 )
  4604. return 0;// nothing to free
  4605. // move rest of the elements backward
  4606. for( ; start + count < end; ++start )
  4607. {
  4608. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4609. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4610. script_removetop(st, -1, 0);
  4611. }
  4612. }
  4613. // clear the rest of the array
  4614. if( is_string_variable(name) )
  4615. {
  4616. for( ; start < end; ++start )
  4617. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4618. }
  4619. else
  4620. {
  4621. for( ; start < end; ++start )
  4622. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4623. }
  4624. return 0;
  4625. }
  4626. /// Returns a reference to the target index of the array variable.
  4627. /// Equivalent to var[index].
  4628. ///
  4629. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4630. BUILDIN_FUNC(getelementofarray)
  4631. {
  4632. struct script_data* data;
  4633. char* name;
  4634. int32 id;
  4635. int i;
  4636. data = script_getdata(st, 2);
  4637. if( !data_isreference(data) )
  4638. {
  4639. ShowError("script:getelementofarray: not a variable\n");
  4640. script_reportdata(data);
  4641. script_pushnil(st);
  4642. st->state = END;
  4643. return 1;// not a variable
  4644. }
  4645. id = reference_getid(data);
  4646. name = reference_getname(data);
  4647. if( not_array_variable(*name) )
  4648. {
  4649. ShowError("script:getelementofarray: illegal scope\n");
  4650. script_reportdata(data);
  4651. script_pushnil(st);
  4652. st->state = END;
  4653. return 1;// not supported
  4654. }
  4655. i = script_getnum(st, 3);
  4656. if( i < 0 || i >= 128 )
  4657. {
  4658. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4659. script_reportdata(data);
  4660. script_pushnil(st);
  4661. st->state = END;
  4662. return 1;// out of range
  4663. }
  4664. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4665. return 0;
  4666. }
  4667. /////////////////////////////////////////////////////////////////////
  4668. /// ...
  4669. ///
  4670. /*==========================================
  4671. *
  4672. *------------------------------------------*/
  4673. BUILDIN_FUNC(setlook)
  4674. {
  4675. int type,val;
  4676. TBL_PC* sd;
  4677. type=script_getnum(st,2);
  4678. val=script_getnum(st,3);
  4679. sd = script_rid2sd(st);
  4680. if( sd == NULL )
  4681. return 0;
  4682. pc_changelook(sd,type,val);
  4683. return 0;
  4684. }
  4685. /*==========================================
  4686. *
  4687. *------------------------------------------*/
  4688. BUILDIN_FUNC(cutin)
  4689. {
  4690. TBL_PC* sd;
  4691. sd = script_rid2sd(st);
  4692. if( sd == NULL )
  4693. return 0;
  4694. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4695. return 0;
  4696. }
  4697. /*==========================================
  4698. *
  4699. *------------------------------------------*/
  4700. BUILDIN_FUNC(viewpoint)
  4701. {
  4702. int type,x,y,id,color;
  4703. TBL_PC* sd;
  4704. type=script_getnum(st,2);
  4705. x=script_getnum(st,3);
  4706. y=script_getnum(st,4);
  4707. id=script_getnum(st,5);
  4708. color=script_getnum(st,6);
  4709. sd = script_rid2sd(st);
  4710. if( sd == NULL )
  4711. return 0;
  4712. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4713. return 0;
  4714. }
  4715. /*==========================================
  4716. *
  4717. *------------------------------------------*/
  4718. BUILDIN_FUNC(countitem)
  4719. {
  4720. int nameid, i;
  4721. int count = 0;
  4722. struct script_data* data;
  4723. TBL_PC* sd = script_rid2sd(st);
  4724. if (!sd) {
  4725. script_pushint(st,0);
  4726. return 0;
  4727. }
  4728. data = script_getdata(st,2);
  4729. get_val(st,data);
  4730. if( data_isstring(data) ) {
  4731. const char* name = conv_str(st,data);
  4732. struct item_data* item_data;
  4733. if((item_data = itemdb_searchname(name)) != NULL)
  4734. nameid = item_data->nameid;
  4735. else
  4736. nameid = 0;
  4737. } else
  4738. nameid = conv_num(st,data);
  4739. if (nameid < 500) {
  4740. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4741. script_reportsrc(st);
  4742. script_pushint(st,0);
  4743. return 1;
  4744. }
  4745. for(i = 0; i < MAX_INVENTORY; i++)
  4746. if(sd->status.inventory[i].nameid == nameid)
  4747. count += sd->status.inventory[i].amount;
  4748. script_pushint(st,count);
  4749. return 0;
  4750. }
  4751. /*==========================================
  4752. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4753. * returns number of items that meet the conditions
  4754. *------------------------------------------*/
  4755. BUILDIN_FUNC(countitem2)
  4756. {
  4757. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4758. int count = 0;
  4759. int i;
  4760. struct script_data* data;
  4761. TBL_PC* sd = script_rid2sd(st);
  4762. if (!sd) {
  4763. script_pushint(st,0);
  4764. return 0;
  4765. }
  4766. data = script_getdata(st,2);
  4767. get_val(st,data);
  4768. if( data_isstring(data) ) {
  4769. const char* name = conv_str(st,data);
  4770. struct item_data* item_data;
  4771. if((item_data = itemdb_searchname(name)) != NULL)
  4772. nameid = item_data->nameid;
  4773. else
  4774. nameid = 0;
  4775. } else
  4776. nameid = conv_num(st,data);
  4777. iden = script_getnum(st,3);
  4778. ref = script_getnum(st,4);
  4779. attr = script_getnum(st,5);
  4780. c1 = (short)script_getnum(st,6);
  4781. c2 = (short)script_getnum(st,7);
  4782. c3 = (short)script_getnum(st,8);
  4783. c4 = (short)script_getnum(st,9);
  4784. if (nameid < 500) {
  4785. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4786. script_pushint(st,0);
  4787. return 1;
  4788. }
  4789. for(i = 0; i < MAX_INVENTORY; i++)
  4790. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4791. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4792. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4793. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4794. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4795. sd->status.inventory[i].card[3] == c4
  4796. )
  4797. count += sd->status.inventory[i].amount;
  4798. script_pushint(st,count);
  4799. return 0;
  4800. }
  4801. /*==========================================
  4802. * 重量チェック
  4803. *------------------------------------------*/
  4804. BUILDIN_FUNC(checkweight)
  4805. {
  4806. int nameid=0,amount,i;
  4807. unsigned long weight;
  4808. TBL_PC *sd;
  4809. struct script_data *data;
  4810. sd = script_rid2sd(st);
  4811. if( sd == NULL )
  4812. return 0;
  4813. data=script_getdata(st,2);
  4814. get_val(st,data);
  4815. if( data_isstring(data) ){
  4816. const char *name=conv_str(st,data);
  4817. struct item_data *item_data = itemdb_searchname(name);
  4818. if( item_data )
  4819. nameid=item_data->nameid;
  4820. }else
  4821. nameid=conv_num(st,data);
  4822. amount=script_getnum(st,3);
  4823. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4824. script_pushint(st,0);
  4825. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4826. return 1;
  4827. }
  4828. weight = itemdb_weight(nameid)*amount;
  4829. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4830. script_pushint(st,0);
  4831. } else {
  4832. //Check if the inventory ain't full.
  4833. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4834. i = pc_search_inventory(sd,0);
  4835. if (i >= 0) //Empty slot available.
  4836. script_pushint(st,1);
  4837. else //Inventory full
  4838. script_pushint(st,0);
  4839. }
  4840. return 0;
  4841. }
  4842. /*==========================================
  4843. * getitem <item id>,<amount>{,<character ID>};
  4844. * getitem "<item name>",<amount>{,<character ID>};
  4845. *------------------------------------------*/
  4846. BUILDIN_FUNC(getitem)
  4847. {
  4848. int nameid,amount,flag = 0;
  4849. struct item it;
  4850. TBL_PC *sd;
  4851. struct script_data *data;
  4852. data=script_getdata(st,2);
  4853. get_val(st,data);
  4854. if( data_isstring(data) )
  4855. {// "<item name>"
  4856. const char *name=conv_str(st,data);
  4857. struct item_data *item_data = itemdb_searchname(name);
  4858. if( item_data == NULL ){
  4859. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4860. return 1; //No item created.
  4861. }
  4862. nameid=item_data->nameid;
  4863. } else if( data_isint(data) )
  4864. {// <item id>
  4865. nameid=conv_num(st,data);
  4866. //Violet Box, Blue Box, etc - random item pick
  4867. if( nameid < 0 ) {
  4868. nameid=itemdb_searchrandomid(-nameid);
  4869. flag = 1;
  4870. }
  4871. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4872. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4873. return 1; //No item created.
  4874. }
  4875. } else {
  4876. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4877. return 1;
  4878. }
  4879. // <amount>
  4880. if( (amount=script_getnum(st,3)) <= 0)
  4881. return 0; //return if amount <=0, skip the useles iteration
  4882. memset(&it,0,sizeof(it));
  4883. it.nameid=nameid;
  4884. if(!flag)
  4885. it.identify=1;
  4886. else
  4887. it.identify=itemdb_isidentified(nameid);
  4888. if( script_hasdata(st,4) )
  4889. {// <character ID>
  4890. sd=map_id2sd(script_getnum(st,4));
  4891. } else
  4892. {// attached player
  4893. sd=script_rid2sd(st);
  4894. }
  4895. if( sd == NULL ) // no target
  4896. return 0;
  4897. if( pet_create_egg(sd, nameid) )
  4898. amount = 1; //This is a pet!
  4899. else if( (flag=pc_additem(sd,&it,amount)) ){
  4900. clif_additem(sd,0,0,flag);
  4901. if( pc_candrop(sd,&it) )
  4902. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4903. }
  4904. //Logs items, got from (N)PC scripts [Lupus]
  4905. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4906. log_pick_pc(sd, "N", nameid, amount, NULL);
  4907. return 0;
  4908. }
  4909. /*==========================================
  4910. *
  4911. *------------------------------------------*/
  4912. BUILDIN_FUNC(getitem2)
  4913. {
  4914. int nameid,amount,flag = 0;
  4915. int iden,ref,attr,c1,c2,c3,c4;
  4916. struct item_data *item_data;
  4917. struct item item_tmp;
  4918. TBL_PC *sd;
  4919. struct script_data *data;
  4920. sd = script_rid2sd(st);
  4921. if( sd == NULL )
  4922. return 0;
  4923. data=script_getdata(st,2);
  4924. get_val(st,data);
  4925. if( data_isstring(data) ){
  4926. const char *name=conv_str(st,data);
  4927. struct item_data *item_data = itemdb_searchname(name);
  4928. if( item_data )
  4929. nameid=item_data->nameid;
  4930. else
  4931. nameid=UNKNOWN_ITEM_ID;
  4932. }else
  4933. nameid=conv_num(st,data);
  4934. amount=script_getnum(st,3);
  4935. iden=script_getnum(st,4);
  4936. ref=script_getnum(st,5);
  4937. attr=script_getnum(st,6);
  4938. c1=script_getnum(st,7);
  4939. c2=script_getnum(st,8);
  4940. c3=script_getnum(st,9);
  4941. c4=script_getnum(st,10);
  4942. if( script_hasdata(st,11) ) //アイテムを指定したIDに渡す
  4943. sd=map_id2sd(script_getnum(st,11));
  4944. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  4945. return 0;
  4946. if(nameid<0) { // ランダム
  4947. nameid=itemdb_searchrandomid(-nameid);
  4948. flag = 1;
  4949. }
  4950. if(nameid > 0) {
  4951. memset(&item_tmp,0,sizeof(item_tmp));
  4952. item_data=itemdb_exists(nameid);
  4953. if (item_data == NULL)
  4954. return -1;
  4955. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4956. if(ref > 10) ref = 10;
  4957. }
  4958. else if(item_data->type==IT_PETEGG) {
  4959. iden = 1;
  4960. ref = 0;
  4961. }
  4962. else {
  4963. iden = 1;
  4964. ref = attr = 0;
  4965. }
  4966. item_tmp.nameid=nameid;
  4967. if(!flag)
  4968. item_tmp.identify=iden;
  4969. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4970. item_tmp.identify=0;
  4971. item_tmp.refine=ref;
  4972. item_tmp.attribute=attr;
  4973. item_tmp.card[0]=c1;
  4974. item_tmp.card[1]=c2;
  4975. item_tmp.card[2]=c3;
  4976. item_tmp.card[3]=c4;
  4977. if((flag = pc_additem(sd,&item_tmp,amount))) {
  4978. clif_additem(sd,0,0,flag);
  4979. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4980. }
  4981. //Logs items, got from (N)PC scripts [Lupus]
  4982. if(log_config.enable_logs&0x40)
  4983. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4984. }
  4985. return 0;
  4986. }
  4987. /*==========================================
  4988. * gets an item with someone's name inscribed [Skotlex]
  4989. * getinscribeditem item_num, character_name
  4990. * Returned Qty is always 1, only works on equip-able
  4991. * equipment
  4992. *------------------------------------------*/
  4993. BUILDIN_FUNC(getnameditem)
  4994. {
  4995. int nameid;
  4996. struct item item_tmp;
  4997. TBL_PC *sd, *tsd;
  4998. struct script_data *data;
  4999. sd = script_rid2sd(st);
  5000. if (sd == NULL)
  5001. { //Player not attached!
  5002. script_pushint(st,0);
  5003. return 0;
  5004. }
  5005. data=script_getdata(st,2);
  5006. get_val(st,data);
  5007. if( data_isstring(data) ){
  5008. const char *name=conv_str(st,data);
  5009. struct item_data *item_data = itemdb_searchname(name);
  5010. if( item_data == NULL)
  5011. { //Failed
  5012. script_pushint(st,0);
  5013. return 0;
  5014. }
  5015. nameid = item_data->nameid;
  5016. }else
  5017. nameid = conv_num(st,data);
  5018. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5019. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5020. script_pushint(st,0);
  5021. return 0;
  5022. }
  5023. data=script_getdata(st,3);
  5024. get_val(st,data);
  5025. if( data_isstring(data) ) //Char Name
  5026. tsd=map_nick2sd(conv_str(st,data));
  5027. else //Char Id was given
  5028. tsd=map_charid2sd(conv_num(st,data));
  5029. if( tsd == NULL )
  5030. { //Failed
  5031. script_pushint(st,0);
  5032. return 0;
  5033. }
  5034. memset(&item_tmp,0,sizeof(item_tmp));
  5035. item_tmp.nameid=nameid;
  5036. item_tmp.amount=1;
  5037. item_tmp.identify=1;
  5038. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5039. item_tmp.card[2]=tsd->status.char_id;
  5040. item_tmp.card[3]=tsd->status.char_id >> 16;
  5041. if(pc_additem(sd,&item_tmp,1)) {
  5042. script_pushint(st,0);
  5043. return 0; //Failed to add item, we will not drop if they don't fit
  5044. }
  5045. //Logs items, got from (N)PC scripts [Lupus]
  5046. if(log_config.enable_logs&0x40)
  5047. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5048. script_pushint(st,1);
  5049. return 0;
  5050. }
  5051. /*==========================================
  5052. * gets a random item ID from an item group [Skotlex]
  5053. * groupranditem group_num
  5054. *------------------------------------------*/
  5055. BUILDIN_FUNC(grouprandomitem)
  5056. {
  5057. int group;
  5058. group = script_getnum(st,2);
  5059. script_pushint(st,itemdb_searchrandomid(group));
  5060. return 0;
  5061. }
  5062. /*==========================================
  5063. *
  5064. *------------------------------------------*/
  5065. BUILDIN_FUNC(makeitem)
  5066. {
  5067. int nameid,amount,flag = 0;
  5068. int x,y,m;
  5069. const char *mapname;
  5070. struct item item_tmp;
  5071. struct script_data *data;
  5072. data=script_getdata(st,2);
  5073. get_val(st,data);
  5074. if( data_isstring(data) ){
  5075. const char *name=conv_str(st,data);
  5076. struct item_data *item_data = itemdb_searchname(name);
  5077. if( item_data )
  5078. nameid=item_data->nameid;
  5079. else
  5080. nameid=UNKNOWN_ITEM_ID;
  5081. }else
  5082. nameid=conv_num(st,data);
  5083. amount=script_getnum(st,3);
  5084. mapname =script_getstr(st,4);
  5085. x =script_getnum(st,5);
  5086. y =script_getnum(st,6);
  5087. if(strcmp(mapname,"this")==0)
  5088. {
  5089. TBL_PC *sd;
  5090. sd = script_rid2sd(st);
  5091. if (!sd) return 0; //Failed...
  5092. m=sd->bl.m;
  5093. } else
  5094. m=map_mapname2mapid(mapname);
  5095. if(nameid<0) { // ランダム
  5096. nameid=itemdb_searchrandomid(-nameid);
  5097. flag = 1;
  5098. }
  5099. if(nameid > 0) {
  5100. memset(&item_tmp,0,sizeof(item_tmp));
  5101. item_tmp.nameid=nameid;
  5102. if(!flag)
  5103. item_tmp.identify=1;
  5104. else
  5105. item_tmp.identify=itemdb_isidentified(nameid);
  5106. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5107. }
  5108. return 0;
  5109. }
  5110. /*==========================================
  5111. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5112. *------------------------------------------*/
  5113. BUILDIN_FUNC(delitem)
  5114. {
  5115. int nameid=0,amount,i,important_item=0;
  5116. TBL_PC *sd;
  5117. struct script_data *data;
  5118. sd = script_rid2sd(st);
  5119. if( sd == NULL )
  5120. return 0;
  5121. data=script_getdata(st,2);
  5122. get_val(st,data);
  5123. if( data_isstring(data) ){
  5124. const char *name=conv_str(st,data);
  5125. struct item_data *item_data = itemdb_searchname(name);
  5126. //nameid=UNKNOWN_ITEM_ID;
  5127. if( item_data )
  5128. nameid=item_data->nameid;
  5129. }else
  5130. nameid=conv_num(st,data);
  5131. amount=script_getnum(st,3);
  5132. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5133. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5134. return 0;
  5135. }
  5136. //1st pass
  5137. //here we won't delete items with CARDS, named items but we count them
  5138. for(i=0;i<MAX_INVENTORY;i++){
  5139. //we don't delete wrong item or equipped item
  5140. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5141. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5142. continue;
  5143. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5144. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5145. sd->status.inventory[i].card[0] == CARD0_PET)
  5146. {
  5147. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5148. continue; //pet couldn't be sent for deletion.
  5149. } else
  5150. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5151. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5152. sd->status.inventory[i].card[0] ||
  5153. sd->status.inventory[i].refine) {
  5154. //this is important item, count it (except for pet eggs)
  5155. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5156. important_item++;
  5157. continue;
  5158. }
  5159. if(sd->status.inventory[i].amount>=amount){
  5160. //Logs items, got from (N)PC scripts [Lupus]
  5161. if(log_config.enable_logs&0x40)
  5162. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5163. pc_delitem(sd,i,amount,0);
  5164. return 0; //we deleted exact amount of items. now exit
  5165. } else {
  5166. amount-=sd->status.inventory[i].amount;
  5167. //Logs items, got from (N)PC scripts [Lupus]
  5168. if(log_config.enable_logs&0x40) {
  5169. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5170. }
  5171. //Logs
  5172. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5173. }
  5174. }
  5175. //2nd pass
  5176. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5177. if (important_item>0 && amount>0)
  5178. for(i=0;i<MAX_INVENTORY;i++){
  5179. //we don't delete wrong item
  5180. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5181. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5182. continue;
  5183. if(sd->status.inventory[i].amount>=amount){
  5184. //Logs items, got from (N)PC scripts [Lupus]
  5185. if(log_config.enable_logs&0x40)
  5186. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5187. pc_delitem(sd,i,amount,0);
  5188. return 0; //we deleted exact amount of items. now exit
  5189. } else {
  5190. amount-=sd->status.inventory[i].amount;
  5191. //Logs items, got from (N)PC scripts [Lupus]
  5192. if(log_config.enable_logs&0x40)
  5193. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5194. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5195. }
  5196. }
  5197. return 0;
  5198. }
  5199. /*==========================================
  5200. * advanced version of delitem [modified by Mihilion]
  5201. *------------------------------------------*/
  5202. BUILDIN_FUNC(delitem2)
  5203. {
  5204. int nameid=0,amount,i=0;
  5205. int iden,ref,attr,c1,c2,c3,c4;
  5206. TBL_PC *sd;
  5207. struct script_data *data;
  5208. sd = script_rid2sd(st);
  5209. if( sd == NULL )
  5210. return 0;
  5211. data=script_getdata(st,2);
  5212. get_val(st,data);
  5213. if( data_isstring(data) ){
  5214. const char *name=conv_str(st,data);
  5215. struct item_data *item_data = itemdb_searchname(name);
  5216. //nameid=UNKNOWN_ITEM_ID;
  5217. if( item_data )
  5218. nameid=item_data->nameid;
  5219. }else
  5220. nameid=conv_num(st,data);
  5221. amount=script_getnum(st,3);
  5222. iden=script_getnum(st,4);
  5223. ref=script_getnum(st,5);
  5224. attr=script_getnum(st,6);
  5225. c1=script_getnum(st,7);
  5226. c2=script_getnum(st,8);
  5227. c3=script_getnum(st,9);
  5228. c4=script_getnum(st,10);
  5229. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5230. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5231. return 0;
  5232. }
  5233. for(i=0;i<MAX_INVENTORY;i++){
  5234. //we don't delete wrong item or equipped item
  5235. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5236. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5237. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5238. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5239. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5240. sd->status.inventory[i].card[3]!=c4)
  5241. continue;
  5242. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5243. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5244. {
  5245. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5246. continue; //Failed to send delete the pet.
  5247. }
  5248. if(sd->status.inventory[i].amount>=amount){
  5249. //Logs items, got from (N)PC scripts [Lupus]
  5250. if(log_config.enable_logs&0x40)
  5251. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5252. pc_delitem(sd,i,amount,0);
  5253. return 0; //we deleted exact amount of items. now exit
  5254. } else {
  5255. amount-=sd->status.inventory[i].amount;
  5256. //Logs items, got from (N)PC scripts [Lupus]
  5257. if(log_config.enable_logs&0x40)
  5258. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5259. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5260. }
  5261. }
  5262. return 0;
  5263. }
  5264. /*==========================================
  5265. * Enables/Disables use of items while in an NPC [Skotlex]
  5266. *------------------------------------------*/
  5267. BUILDIN_FUNC(enableitemuse)
  5268. {
  5269. TBL_PC *sd;
  5270. sd=script_rid2sd(st);
  5271. if (sd)
  5272. sd->npc_item_flag = st->oid;
  5273. return 0;
  5274. }
  5275. BUILDIN_FUNC(disableitemuse)
  5276. {
  5277. TBL_PC *sd;
  5278. sd=script_rid2sd(st);
  5279. if (sd)
  5280. sd->npc_item_flag = 0;
  5281. return 0;
  5282. }
  5283. /*==========================================
  5284. *キャラ関係のパラメータ取得
  5285. *------------------------------------------*/
  5286. BUILDIN_FUNC(readparam)
  5287. {
  5288. int type;
  5289. TBL_PC *sd;
  5290. type=script_getnum(st,2);
  5291. if( script_hasdata(st,3) )
  5292. sd=map_nick2sd(script_getstr(st,3));
  5293. else
  5294. sd=script_rid2sd(st);
  5295. if(sd==NULL){
  5296. script_pushint(st,-1);
  5297. return 0;
  5298. }
  5299. script_pushint(st,pc_readparam(sd,type));
  5300. return 0;
  5301. }
  5302. /*==========================================
  5303. *キャラ関係のID取得
  5304. *------------------------------------------*/
  5305. BUILDIN_FUNC(getcharid)
  5306. {
  5307. int num;
  5308. TBL_PC *sd;
  5309. num = script_getnum(st,2);
  5310. if( script_hasdata(st,3) )
  5311. sd=map_nick2sd(script_getstr(st,3));
  5312. else
  5313. sd=script_rid2sd(st);
  5314. if(sd==NULL){
  5315. script_pushint(st,0); //return 0, according docs
  5316. return 0;
  5317. }
  5318. switch( num ) {
  5319. case 0: script_pushint(st,sd->status.char_id); break;
  5320. case 1: script_pushint(st,sd->status.party_id); break;
  5321. case 2: script_pushint(st,sd->status.guild_id); break;
  5322. case 3: script_pushint(st,sd->status.account_id); break;
  5323. default:
  5324. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5325. script_pushint(st,0);
  5326. break;
  5327. }
  5328. return 0;
  5329. }
  5330. /*==========================================
  5331. *指定IDのPT名取得
  5332. *------------------------------------------*/
  5333. char *buildin_getpartyname_sub(int party_id)
  5334. {
  5335. struct party_data *p;
  5336. p=party_search(party_id);
  5337. if(p!=NULL){
  5338. char *buf;
  5339. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5340. memcpy(buf, p->party.name, NAME_LENGTH);
  5341. buf[NAME_LENGTH-1] = '\0';
  5342. return buf;
  5343. }
  5344. return 0;
  5345. }
  5346. BUILDIN_FUNC(getpartyname)
  5347. {
  5348. char *name;
  5349. int party_id;
  5350. party_id=script_getnum(st,2);
  5351. name=buildin_getpartyname_sub(party_id);
  5352. if(name != NULL)
  5353. script_pushstr(st,name);
  5354. else
  5355. script_pushconststr(st,"null");
  5356. return 0;
  5357. }
  5358. /*==========================================
  5359. *指定IDのPT人数とメンバーID取得
  5360. *------------------------------------------*/
  5361. BUILDIN_FUNC(getpartymember)
  5362. {
  5363. struct party_data *p;
  5364. int i,j=0,type=0;
  5365. p=party_search(script_getnum(st,2));
  5366. if( script_hasdata(st,3) )
  5367. type=script_getnum(st,3);
  5368. if(p!=NULL){
  5369. for(i=0;i<MAX_PARTY;i++){
  5370. if(p->party.member[i].account_id){
  5371. switch (type) {
  5372. case 2:
  5373. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5374. break;
  5375. case 1:
  5376. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5377. break;
  5378. default:
  5379. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5380. }
  5381. j++;
  5382. }
  5383. }
  5384. }
  5385. mapreg_setreg(add_str("$@partymembercount"),j);
  5386. return 0;
  5387. }
  5388. /*==========================================
  5389. * Retrieves party leader. if flag is specified,
  5390. * return some of the leader data. Otherwise, return name.
  5391. *------------------------------------------*/
  5392. BUILDIN_FUNC(getpartyleader)
  5393. {
  5394. int party_id, type = 0, i=0;
  5395. struct party_data *p;
  5396. party_id=script_getnum(st,2);
  5397. if( script_hasdata(st,3) )
  5398. type=script_getnum(st,3);
  5399. p=party_search(party_id);
  5400. if (p) //Search leader
  5401. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5402. if (!p || i == MAX_PARTY) { //leader not found
  5403. if (type)
  5404. script_pushint(st,-1);
  5405. else
  5406. script_pushconststr(st,"null");
  5407. return 0;
  5408. }
  5409. switch (type) {
  5410. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5411. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5412. case 3: script_pushint(st,p->party.member[i].class_); break;
  5413. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5414. case 5: script_pushint(st,p->party.member[i].lv); break;
  5415. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5416. }
  5417. return 0;
  5418. }
  5419. /*==========================================
  5420. *指定IDのギルド名取得
  5421. *------------------------------------------*/
  5422. char *buildin_getguildname_sub(int guild_id)
  5423. {
  5424. struct guild *g=NULL;
  5425. g=guild_search(guild_id);
  5426. if(g!=NULL){
  5427. char *buf;
  5428. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5429. memcpy(buf, g->name, NAME_LENGTH);
  5430. buf[NAME_LENGTH-1] = '\0';
  5431. return buf;
  5432. }
  5433. return NULL;
  5434. }
  5435. BUILDIN_FUNC(getguildname)
  5436. {
  5437. char *name;
  5438. int guild_id=script_getnum(st,2);
  5439. name=buildin_getguildname_sub(guild_id);
  5440. if(name != NULL)
  5441. script_pushstr(st,name);
  5442. else
  5443. script_pushconststr(st,"null");
  5444. return 0;
  5445. }
  5446. /*==========================================
  5447. *指定IDのGuildMaster名取得
  5448. *------------------------------------------*/
  5449. char *buildin_getguildmaster_sub(int guild_id)
  5450. {
  5451. struct guild *g=NULL;
  5452. g=guild_search(guild_id);
  5453. if(g!=NULL){
  5454. char *buf;
  5455. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5456. memcpy(buf, g->master, NAME_LENGTH);
  5457. buf[NAME_LENGTH-1] = '\0';
  5458. return buf;
  5459. }
  5460. return 0;
  5461. }
  5462. BUILDIN_FUNC(getguildmaster)
  5463. {
  5464. char *master;
  5465. int guild_id=script_getnum(st,2);
  5466. master=buildin_getguildmaster_sub(guild_id);
  5467. if(master!=0)
  5468. script_pushstr(st,master);
  5469. else
  5470. script_pushconststr(st,"null");
  5471. return 0;
  5472. }
  5473. BUILDIN_FUNC(getguildmasterid)
  5474. {
  5475. char *master;
  5476. TBL_PC *sd=NULL;
  5477. int guild_id=script_getnum(st,2);
  5478. master=buildin_getguildmaster_sub(guild_id);
  5479. if(master!=0){
  5480. if((sd=map_nick2sd(master)) == NULL){
  5481. script_pushint(st,0);
  5482. return 0;
  5483. }
  5484. script_pushint(st,sd->status.char_id);
  5485. }else{
  5486. script_pushint(st,0);
  5487. }
  5488. return 0;
  5489. }
  5490. /*==========================================
  5491. * キャラクタの名前
  5492. *------------------------------------------*/
  5493. BUILDIN_FUNC(strcharinfo)
  5494. {
  5495. TBL_PC *sd;
  5496. int num;
  5497. char *buf;
  5498. sd=script_rid2sd(st);
  5499. if (!sd) { //Avoid crashing....
  5500. script_pushconststr(st,"");
  5501. return 0;
  5502. }
  5503. num=script_getnum(st,2);
  5504. switch(num){
  5505. case 0:
  5506. script_pushstrcopy(st,sd->status.name);
  5507. break;
  5508. case 1:
  5509. buf=buildin_getpartyname_sub(sd->status.party_id);
  5510. if(buf!=0)
  5511. script_pushstr(st,buf);
  5512. else
  5513. script_pushconststr(st,"");
  5514. break;
  5515. case 2:
  5516. buf=buildin_getguildname_sub(sd->status.guild_id);
  5517. if(buf != NULL)
  5518. script_pushstr(st,buf);
  5519. else
  5520. script_pushconststr(st,"");
  5521. break;
  5522. default:
  5523. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5524. script_pushconststr(st,"");
  5525. break;
  5526. }
  5527. return 0;
  5528. }
  5529. /*==========================================
  5530. * 呼び出し元のNPC情報を取得する
  5531. *------------------------------------------*/
  5532. BUILDIN_FUNC(strnpcinfo)
  5533. {
  5534. TBL_NPC* nd;
  5535. int num;
  5536. char *buf,*name=NULL;
  5537. nd = map_id2nd(st->oid);
  5538. if (!nd) {
  5539. script_pushconststr(st, "");
  5540. return 0;
  5541. }
  5542. num = script_getnum(st,2);
  5543. switch(num){
  5544. case 0: // display name
  5545. name = aStrdup(nd->name);
  5546. break;
  5547. case 1: // visible part of display name name
  5548. if((buf = strchr(nd->name,'#')) != NULL)
  5549. {
  5550. name = aStrdup(nd->name);
  5551. name[buf - nd->name] = 0;
  5552. }
  5553. break;
  5554. case 2: // # fragment
  5555. if((buf = strchr(nd->name,'#')) != NULL)
  5556. name = aStrdup(buf+1);
  5557. break;
  5558. case 3: // unique name
  5559. name = aStrdup(nd->exname);
  5560. break;
  5561. }
  5562. if(name)
  5563. script_pushstr(st, name);
  5564. else
  5565. script_pushconststr(st, "");
  5566. return 0;
  5567. }
  5568. 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};
  5569. /*==========================================
  5570. * GetEquipID(Pos); Pos: 1-10
  5571. *------------------------------------------*/
  5572. BUILDIN_FUNC(getequipid)
  5573. {
  5574. int i=-1,num;
  5575. TBL_PC *sd;
  5576. struct item_data* item;
  5577. sd=script_rid2sd(st);
  5578. if(sd == NULL)
  5579. return 0;
  5580. num=script_getnum(st,2);
  5581. if (num > 0 && num <= ARRAYLENGTH(equip))
  5582. i=pc_checkequip(sd,equip[num-1]);
  5583. if(i >= 0){
  5584. item=sd->inventory_data[i];
  5585. if(item)
  5586. script_pushint(st,item->nameid);
  5587. else
  5588. script_pushint(st,0);
  5589. }else{
  5590. script_pushint(st,-1);
  5591. }
  5592. return 0;
  5593. }
  5594. /*==========================================
  5595. * 装備名文字列(精錬メニュー用)
  5596. *------------------------------------------*/
  5597. BUILDIN_FUNC(getequipname)
  5598. {
  5599. int i=-1,num;
  5600. TBL_PC *sd;
  5601. struct item_data* item;
  5602. char *buf;
  5603. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  5604. sd = script_rid2sd(st);
  5605. if( sd == NULL )
  5606. return 0;
  5607. buf=(char *)aMallocA(64*sizeof(char));
  5608. num=script_getnum(st,2);
  5609. if (num > 0 && num <= ARRAYLENGTH(equip))
  5610. i=pc_checkequip(sd,equip[num-1]);
  5611. if(i >= 0){
  5612. item=sd->inventory_data[i];
  5613. if(item)
  5614. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5615. else
  5616. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5617. }else{
  5618. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5619. }
  5620. script_pushstr(st,buf);
  5621. return 0;
  5622. }
  5623. /*==========================================
  5624. * getbrokenid [Valaris]
  5625. *------------------------------------------*/
  5626. BUILDIN_FUNC(getbrokenid)
  5627. {
  5628. int i,num,id=0,brokencounter=0;
  5629. TBL_PC *sd;
  5630. sd = script_rid2sd(st);
  5631. if( sd == NULL )
  5632. return 0;
  5633. num=script_getnum(st,2);
  5634. for(i=0; i<MAX_INVENTORY; i++) {
  5635. if(sd->status.inventory[i].attribute==1){
  5636. brokencounter++;
  5637. if(num==brokencounter){
  5638. id=sd->status.inventory[i].nameid;
  5639. break;
  5640. }
  5641. }
  5642. }
  5643. script_pushint(st,id);
  5644. return 0;
  5645. }
  5646. /*==========================================
  5647. * repair [Valaris]
  5648. *------------------------------------------*/
  5649. BUILDIN_FUNC(repair)
  5650. {
  5651. int i,num;
  5652. int repaircounter=0;
  5653. TBL_PC *sd;
  5654. sd = script_rid2sd(st);
  5655. if( sd == NULL )
  5656. return 0;
  5657. num=script_getnum(st,2);
  5658. for(i=0; i<MAX_INVENTORY; i++) {
  5659. if(sd->status.inventory[i].attribute==1){
  5660. repaircounter++;
  5661. if(num==repaircounter){
  5662. sd->status.inventory[i].attribute=0;
  5663. clif_equiplist(sd);
  5664. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5665. clif_misceffect(&sd->bl, 3);
  5666. break;
  5667. }
  5668. }
  5669. }
  5670. return 0;
  5671. }
  5672. /*==========================================
  5673. * 装備チェック
  5674. *------------------------------------------*/
  5675. BUILDIN_FUNC(getequipisequiped)
  5676. {
  5677. int i=-1,num;
  5678. TBL_PC *sd;
  5679. num=script_getnum(st,2);
  5680. sd = script_rid2sd(st);
  5681. if( sd == NULL )
  5682. return 0;
  5683. if (num > 0 && num <= ARRAYLENGTH(equip))
  5684. i=pc_checkequip(sd,equip[num-1]);
  5685. if(i >= 0)
  5686. script_pushint(st,1);
  5687. else
  5688. script_pushint(st,0);
  5689. return 0;
  5690. }
  5691. /*==========================================
  5692. * 装備品精錬可能チェック
  5693. *------------------------------------------*/
  5694. BUILDIN_FUNC(getequipisenableref)
  5695. {
  5696. int i=-1,num;
  5697. TBL_PC *sd;
  5698. num=script_getnum(st,2);
  5699. sd = script_rid2sd(st);
  5700. if( sd == NULL )
  5701. return 0;
  5702. if (num > 0 && num <= ARRAYLENGTH(equip))
  5703. i=pc_checkequip(sd,equip[num-1]);
  5704. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5705. {
  5706. script_pushint(st,1);
  5707. } else {
  5708. script_pushint(st,0);
  5709. }
  5710. return 0;
  5711. }
  5712. /*==========================================
  5713. * 装備品鑑定チェック
  5714. *------------------------------------------*/
  5715. BUILDIN_FUNC(getequipisidentify)
  5716. {
  5717. int i=-1,num;
  5718. TBL_PC *sd;
  5719. num=script_getnum(st,2);
  5720. sd = script_rid2sd(st);
  5721. if( sd == NULL )
  5722. return 0;
  5723. if (num > 0 && num <= ARRAYLENGTH(equip))
  5724. i=pc_checkequip(sd,equip[num-1]);
  5725. if(i >= 0)
  5726. script_pushint(st,sd->status.inventory[i].identify);
  5727. else
  5728. script_pushint(st,0);
  5729. return 0;
  5730. }
  5731. /*==========================================
  5732. * 装備品精錬度
  5733. *------------------------------------------*/
  5734. BUILDIN_FUNC(getequiprefinerycnt)
  5735. {
  5736. int i=-1,num;
  5737. TBL_PC *sd;
  5738. num=script_getnum(st,2);
  5739. sd = script_rid2sd(st);
  5740. if( sd == NULL )
  5741. return 0;
  5742. if (num > 0 && num <= ARRAYLENGTH(equip))
  5743. i=pc_checkequip(sd,equip[num-1]);
  5744. if(i >= 0)
  5745. script_pushint(st,sd->status.inventory[i].refine);
  5746. else
  5747. script_pushint(st,0);
  5748. return 0;
  5749. }
  5750. /*==========================================
  5751. * 装備品武器LV
  5752. *------------------------------------------*/
  5753. BUILDIN_FUNC(getequipweaponlv)
  5754. {
  5755. int i=-1,num;
  5756. TBL_PC *sd;
  5757. num=script_getnum(st,2);
  5758. sd = script_rid2sd(st);
  5759. if( sd == NULL )
  5760. return 0;
  5761. if (num > 0 && num <= ARRAYLENGTH(equip))
  5762. i=pc_checkequip(sd,equip[num-1]);
  5763. if(i >= 0 && sd->inventory_data[i])
  5764. script_pushint(st,sd->inventory_data[i]->wlv);
  5765. else
  5766. script_pushint(st,0);
  5767. return 0;
  5768. }
  5769. /*==========================================
  5770. * 装備品精錬成功率
  5771. *------------------------------------------*/
  5772. BUILDIN_FUNC(getequippercentrefinery)
  5773. {
  5774. int i=-1,num;
  5775. TBL_PC *sd;
  5776. num=script_getnum(st,2);
  5777. sd = script_rid2sd(st);
  5778. if( sd == NULL )
  5779. return 0;
  5780. if (num > 0 && num <= ARRAYLENGTH(equip))
  5781. i=pc_checkequip(sd,equip[num-1]);
  5782. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5783. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5784. else
  5785. script_pushint(st,0);
  5786. return 0;
  5787. }
  5788. /*==========================================
  5789. * 精錬成功
  5790. *------------------------------------------*/
  5791. BUILDIN_FUNC(successrefitem)
  5792. {
  5793. int i=-1,num,ep;
  5794. TBL_PC *sd;
  5795. num=script_getnum(st,2);
  5796. sd = script_rid2sd(st);
  5797. if( sd == NULL )
  5798. return 0;
  5799. if (num > 0 && num <= ARRAYLENGTH(equip))
  5800. i=pc_checkequip(sd,equip[num-1]);
  5801. if(i >= 0) {
  5802. ep=sd->status.inventory[i].equip;
  5803. //Logs items, got from (N)PC scripts [Lupus]
  5804. if(log_config.enable_logs&0x40)
  5805. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5806. sd->status.inventory[i].refine++;
  5807. pc_unequipitem(sd,i,2);
  5808. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5809. clif_delitem(sd,i,1);
  5810. //Logs items, got from (N)PC scripts [Lupus]
  5811. if(log_config.enable_logs&0x40)
  5812. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5813. clif_additem(sd,i,1,0);
  5814. pc_equipitem(sd,i,ep);
  5815. clif_misceffect(&sd->bl,3);
  5816. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5817. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5818. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5819. ){ // Fame point system [DracoRPG]
  5820. switch (sd->inventory_data[i]->wlv){
  5821. case 1:
  5822. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5823. break;
  5824. case 2:
  5825. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5826. break;
  5827. case 3:
  5828. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5829. break;
  5830. }
  5831. }
  5832. }
  5833. return 0;
  5834. }
  5835. /*==========================================
  5836. * 精錬失敗
  5837. *------------------------------------------*/
  5838. BUILDIN_FUNC(failedrefitem)
  5839. {
  5840. int i=-1,num;
  5841. TBL_PC *sd;
  5842. num=script_getnum(st,2);
  5843. sd = script_rid2sd(st);
  5844. if( sd == NULL )
  5845. return 0;
  5846. if (num > 0 && num <= ARRAYLENGTH(equip))
  5847. i=pc_checkequip(sd,equip[num-1]);
  5848. if(i >= 0) {
  5849. //Logs items, got from (N)PC scripts [Lupus]
  5850. if(log_config.enable_logs&0x40)
  5851. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5852. sd->status.inventory[i].refine = 0;
  5853. pc_unequipitem(sd,i,3);
  5854. // 精錬失敗エフェクトのパケット
  5855. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5856. pc_delitem(sd,i,1,0);
  5857. // 他の人にも失敗を通知
  5858. clif_misceffect(&sd->bl,2);
  5859. }
  5860. return 0;
  5861. }
  5862. /*==========================================
  5863. *
  5864. *------------------------------------------*/
  5865. BUILDIN_FUNC(statusup)
  5866. {
  5867. int type;
  5868. TBL_PC *sd;
  5869. type=script_getnum(st,2);
  5870. sd = script_rid2sd(st);
  5871. if( sd == NULL )
  5872. return 0;
  5873. pc_statusup(sd,type);
  5874. return 0;
  5875. }
  5876. /*==========================================
  5877. *
  5878. *------------------------------------------*/
  5879. BUILDIN_FUNC(statusup2)
  5880. {
  5881. int type,val;
  5882. TBL_PC *sd;
  5883. type=script_getnum(st,2);
  5884. val=script_getnum(st,3);
  5885. sd = script_rid2sd(st);
  5886. if( sd == NULL )
  5887. return 0;
  5888. pc_statusup2(sd,type,val);
  5889. return 0;
  5890. }
  5891. /// See 'doc/item_bonus.txt'
  5892. ///
  5893. /// bonus <bonus type>,<val1>;
  5894. /// bonus2 <bonus type>,<val1>,<val2>;
  5895. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5896. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5897. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5898. BUILDIN_FUNC(bonus)
  5899. {
  5900. int type;
  5901. int type2;
  5902. int type3;
  5903. int type4;
  5904. int type5;
  5905. int val;
  5906. TBL_PC* sd;
  5907. sd = script_rid2sd(st);
  5908. if( sd == NULL )
  5909. return 0; // no player attached
  5910. type = script_getnum(st,2);
  5911. switch( script_lastdata(st) ){
  5912. case 3:
  5913. val = script_getnum(st,3);
  5914. pc_bonus(sd, type, val);
  5915. break;
  5916. case 4:
  5917. type2 = script_getnum(st,3);
  5918. val = script_getnum(st,4);
  5919. pc_bonus2(sd, type, type2, val);
  5920. break;
  5921. case 5:
  5922. type2 = script_getnum(st,3);
  5923. type3 = script_getnum(st,4);
  5924. val = script_getnum(st,5);
  5925. pc_bonus3(sd, type, type2, type3, val);
  5926. break;
  5927. case 6:
  5928. type2 = script_getnum(st,3);
  5929. type3 = script_getnum(st,4);
  5930. type4 = script_getnum(st,5);
  5931. val = script_getnum(st,6);
  5932. pc_bonus4(sd, type, type2, type3, type4, val);
  5933. break;
  5934. case 7:
  5935. type2 = script_getnum(st,3);
  5936. type3 = script_getnum(st,4);
  5937. type4 = script_getnum(st,5);
  5938. type5 = script_getnum(st,6);
  5939. val = script_getnum(st,7);
  5940. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  5941. break;
  5942. default:
  5943. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 2));
  5944. }
  5945. return 0;
  5946. }
  5947. /// Bonus script that has a chance of being executed on attack.
  5948. BUILDIN_FUNC(bonusautoscript)
  5949. {
  5950. int rate, flag = 0;
  5951. const char *str;
  5952. struct script_code *script;
  5953. TBL_PC* sd;
  5954. sd = script_rid2sd(st);
  5955. if( sd == NULL )
  5956. return 0;// no player attached, report source
  5957. str = script_getstr(st,2);
  5958. rate = script_getnum(st,3);
  5959. if( script_hasdata(st,4) )
  5960. flag = script_getnum(st,4);
  5961. script = parse_script(str, "autoscript bonus", 0, 0);
  5962. if (!script)
  5963. return 1;
  5964. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  5965. {
  5966. script_free_code(script);
  5967. return 1;
  5968. }
  5969. return 0;
  5970. }
  5971. /// Bonus script that has a chance of being executed when attacked.
  5972. BUILDIN_FUNC(bonusautoscript2)
  5973. {
  5974. int rate, flag = 0;
  5975. const char *str;
  5976. struct script_code *script;
  5977. TBL_PC* sd;
  5978. sd = script_rid2sd(st);
  5979. if( sd == NULL )
  5980. return 0;// no player attached, report source
  5981. str = script_getstr(st,2);
  5982. rate = script_getnum(st,3);
  5983. if( script_hasdata(st,4) )
  5984. flag = script_getnum(st,4);
  5985. script = parse_script(str, "autoscript2 bonus", 0, 0);
  5986. if (!script)
  5987. return 1;
  5988. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  5989. {
  5990. script_free_code(script);
  5991. return 1;
  5992. }
  5993. return 0;
  5994. }
  5995. /// Changes the level of a player skill.
  5996. /// <flag> defaults to 1
  5997. /// <flag>=0 : set the level of the skill
  5998. /// <flag>=1 : set the temporary level of the skill
  5999. /// <flag>=2 : add to the level of the skill
  6000. ///
  6001. /// skill <skill id>,<level>,<flag>
  6002. /// skill <skill id>,<level>
  6003. BUILDIN_FUNC(skill)
  6004. {
  6005. int id;
  6006. int level;
  6007. int flag = 1;
  6008. TBL_PC* sd;
  6009. sd = script_rid2sd(st);
  6010. if( sd == NULL )
  6011. return 0;// no player attached, report source
  6012. id = script_getnum(st,2);
  6013. level = script_getnum(st,3);
  6014. if( script_hasdata(st,4) )
  6015. flag = script_getnum(st,4);
  6016. pc_skill(sd, id, level, flag);
  6017. return 0;
  6018. }
  6019. /// Changes the level of a player skill.
  6020. /// like skill, but <flag> defaults to 2
  6021. ///
  6022. /// addtoskill <skill id>,<amount>,<flag>
  6023. /// addtoskill <skill id>,<amount>
  6024. ///
  6025. /// @see skill
  6026. BUILDIN_FUNC(addtoskill)
  6027. {
  6028. int id;
  6029. int level;
  6030. int flag = 2;
  6031. TBL_PC* sd;
  6032. sd = script_rid2sd(st);
  6033. if( sd == NULL )
  6034. return 0;// no player attached, report source
  6035. id = script_getnum(st,2);
  6036. level = script_getnum(st,3);
  6037. if( script_hasdata(st,4) )
  6038. flag = script_getnum(st,4);
  6039. pc_skill(sd, id, level, flag);
  6040. return 0;
  6041. }
  6042. /// Increases the level of a guild skill.
  6043. ///
  6044. /// guildskill <skill id>,<amount>;
  6045. BUILDIN_FUNC(guildskill)
  6046. {
  6047. int id;
  6048. int level;
  6049. TBL_PC* sd;
  6050. int i;
  6051. sd = script_rid2sd(st);
  6052. if( sd == NULL )
  6053. return 0;// no player attached, report source
  6054. id = script_getnum(st,2);
  6055. level = script_getnum(st,3);
  6056. for( i=0; i < level; i++ )
  6057. guild_skillup(sd, id);
  6058. return 0;
  6059. }
  6060. /// Returns the level of the player skill.
  6061. ///
  6062. /// getskilllv(<skill id>) -> <level>
  6063. BUILDIN_FUNC(getskilllv)
  6064. {
  6065. int id;
  6066. TBL_PC* sd;
  6067. sd = script_rid2sd(st);
  6068. if( sd == NULL )
  6069. return 0;// no player attached, report source
  6070. id = script_getnum(st,2);
  6071. script_pushint(st, pc_checkskill(sd,id));
  6072. return 0;
  6073. }
  6074. /// Returns the level of the guild skill.
  6075. ///
  6076. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6077. BUILDIN_FUNC(getgdskilllv)
  6078. {
  6079. int guild_id;
  6080. int skill_id;
  6081. struct guild* g;
  6082. guild_id = script_getnum(st,2);
  6083. skill_id = script_getnum(st,3);
  6084. g = guild_search(guild_id);
  6085. if( g == NULL )
  6086. script_pushint(st, -1);
  6087. else
  6088. script_pushint(st, guild_checkskill(g,skill_id));
  6089. return 0;
  6090. }
  6091. /// Returns the 'basic_skill_check' setting.
  6092. /// This config determines if the server checks the skill level of NV_BASIC
  6093. /// before allowing the basic actions.
  6094. ///
  6095. /// basicskillcheck() -> <bool>
  6096. BUILDIN_FUNC(basicskillcheck)
  6097. {
  6098. script_pushint(st, battle_config.basic_skill_check);
  6099. return 0;
  6100. }
  6101. /// Returns the GM level of the player.
  6102. ///
  6103. /// getgmlevel() -> <level>
  6104. BUILDIN_FUNC(getgmlevel)
  6105. {
  6106. TBL_PC* sd;
  6107. sd = script_rid2sd(st);
  6108. if( sd == NULL )
  6109. return 0;// no player attached, report source
  6110. script_pushint(st, pc_isGM(sd));
  6111. return 0;
  6112. }
  6113. /// Terminates the execution of this script instance.
  6114. ///
  6115. /// end
  6116. BUILDIN_FUNC(end)
  6117. {
  6118. st->state = END;
  6119. return 0;
  6120. }
  6121. /// Checks if the player has that effect state (option).
  6122. ///
  6123. /// checkoption(<option>) -> <bool>
  6124. BUILDIN_FUNC(checkoption)
  6125. {
  6126. int option;
  6127. TBL_PC* sd;
  6128. sd = script_rid2sd(st);
  6129. if( sd == NULL )
  6130. return 0;// no player attached, report source
  6131. option = script_getnum(st,2);
  6132. if( sd->sc.option&option )
  6133. script_pushint(st, 1);
  6134. else
  6135. script_pushint(st, 0);
  6136. return 0;
  6137. }
  6138. /// Checks if the player is in that body state (opt1).
  6139. ///
  6140. /// checkoption1(<opt1>) -> <bool>
  6141. BUILDIN_FUNC(checkoption1)
  6142. {
  6143. int opt1;
  6144. TBL_PC* sd;
  6145. sd = script_rid2sd(st);
  6146. if( sd == NULL )
  6147. return 0;// no player attached, report source
  6148. opt1 = script_getnum(st,2);
  6149. if( sd->sc.opt1 == opt1 )
  6150. script_pushint(st, 1);
  6151. else
  6152. script_pushint(st, 0);
  6153. return 0;
  6154. }
  6155. /// Checks if the player has that health state (opt2).
  6156. ///
  6157. /// checkoption2(<opt2>) -> <bool>
  6158. BUILDIN_FUNC(checkoption2)
  6159. {
  6160. int opt2;
  6161. TBL_PC* sd;
  6162. sd = script_rid2sd(st);
  6163. if( sd == NULL )
  6164. return 0;// no player attached, report source
  6165. opt2 = script_getnum(st,2);
  6166. if( sd->sc.opt2&opt2 )
  6167. script_pushint(st, 1);
  6168. else
  6169. script_pushint(st, 0);
  6170. return 0;
  6171. }
  6172. /// Changes the effect state (option) of the player.
  6173. /// <flag> defaults to 1
  6174. /// <flag>=0 : removes the option
  6175. /// <flag>=other : adds the option
  6176. ///
  6177. /// setoption <option>,<flag>;
  6178. /// setoption <option>;
  6179. BUILDIN_FUNC(setoption)
  6180. {
  6181. int option;
  6182. int flag = 1;
  6183. TBL_PC* sd;
  6184. sd = script_rid2sd(st);
  6185. if( sd == NULL )
  6186. return 0;// no player attached, report source
  6187. option = script_getnum(st,2);
  6188. if( script_hasdata(st,3) )
  6189. flag = script_getnum(st,3);
  6190. else if( !option ){// Request to remove everything.
  6191. flag = 0;
  6192. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6193. }
  6194. if( flag ){// Add option
  6195. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6196. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6197. pc_setoption(sd, sd->sc.option|option);
  6198. } else// Remove option
  6199. pc_setoption(sd, sd->sc.option&~option);
  6200. return 0;
  6201. }
  6202. /// Returns if the player has a cart.
  6203. ///
  6204. /// checkcart() -> <bool>
  6205. ///
  6206. /// @author Valaris
  6207. BUILDIN_FUNC(checkcart)
  6208. {
  6209. TBL_PC* sd;
  6210. sd = script_rid2sd(st);
  6211. if( sd == NULL )
  6212. return 0;// no player attached, report source
  6213. if( pc_iscarton(sd) )
  6214. script_pushint(st, 1);
  6215. else
  6216. script_pushint(st, 0);
  6217. return 0;
  6218. }
  6219. /// Sets the cart of the player.
  6220. /// <type> defaults to 1
  6221. /// <type>=0 : removes the cart
  6222. /// <type>=1 : Normal cart
  6223. /// <type>=2 : Wooden cart
  6224. /// <type>=3 : Covered cart with flowers and ferns
  6225. /// <type>=4 : Wooden cart with a Panda doll on the back
  6226. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6227. ///
  6228. /// setcart <type>;
  6229. /// setcart;
  6230. BUILDIN_FUNC(setcart)
  6231. {
  6232. int type = 1;
  6233. TBL_PC* sd;
  6234. sd = script_rid2sd(st);
  6235. if( sd == NULL )
  6236. return 0;// no player attached, report source
  6237. if( script_hasdata(st,2) )
  6238. type = script_getnum(st,2);
  6239. pc_setcart(sd, type);
  6240. return 0;
  6241. }
  6242. /// Returns if the player has a falcon.
  6243. ///
  6244. /// checkfalcon() -> <bool>
  6245. ///
  6246. /// @author Valaris
  6247. BUILDIN_FUNC(checkfalcon)
  6248. {
  6249. TBL_PC* sd;
  6250. sd = script_rid2sd(st);
  6251. if( sd == NULL )
  6252. return 0;// no player attached, report source
  6253. if( pc_isfalcon(sd) )
  6254. script_pushint(st, 1);
  6255. else
  6256. script_pushint(st, 0);
  6257. return 0;
  6258. }
  6259. /// Sets if the player has a falcon or not.
  6260. /// <flag> defaults to 1
  6261. ///
  6262. /// setfalcon <flag>;
  6263. /// setfalcon;
  6264. BUILDIN_FUNC(setfalcon)
  6265. {
  6266. int flag = 1;
  6267. TBL_PC* sd;
  6268. sd = script_rid2sd(st);
  6269. if( sd == NULL )
  6270. return 0;// no player attached, report source
  6271. if( script_hasdata(st,2) )
  6272. flag = script_getnum(st,2);
  6273. pc_setfalcon(sd, flag);
  6274. return 0;
  6275. }
  6276. /// Returns if the player is riding.
  6277. ///
  6278. /// checkriding() -> <bool>
  6279. ///
  6280. /// @author Valaris
  6281. BUILDIN_FUNC(checkriding)
  6282. {
  6283. TBL_PC* sd;
  6284. sd = script_rid2sd(st);
  6285. if( sd == NULL )
  6286. return 0;// no player attached, report source
  6287. if( pc_isriding(sd) )
  6288. script_pushint(st, 1);
  6289. else
  6290. script_pushint(st, 0);
  6291. return 0;
  6292. }
  6293. /// Sets if the player is riding.
  6294. /// <flag> defaults to 1
  6295. ///
  6296. /// setriding <flag>;
  6297. /// setriding;
  6298. BUILDIN_FUNC(setriding)
  6299. {
  6300. int flag = 1;
  6301. TBL_PC* sd;
  6302. sd = script_rid2sd(st);
  6303. if( sd == NULL )
  6304. return 0;// no player attached, report source
  6305. if( script_hasdata(st,2) )
  6306. flag = script_getnum(st,2);
  6307. pc_setriding(sd, flag);
  6308. return 0;
  6309. }
  6310. /// Sets the save point of the player.
  6311. ///
  6312. /// save "<map name>",<x>,<y>
  6313. /// savepoint "<map name>",<x>,<y>
  6314. BUILDIN_FUNC(savepoint)
  6315. {
  6316. int x;
  6317. int y;
  6318. short map;
  6319. const char* str;
  6320. TBL_PC* sd;
  6321. sd = script_rid2sd(st);
  6322. if( sd == NULL )
  6323. return 0;// no player attached, report source
  6324. str = script_getstr(st, 2);
  6325. x = script_getnum(st,3);
  6326. y = script_getnum(st,4);
  6327. map = mapindex_name2id(str);
  6328. if( map )
  6329. pc_setsavepoint(sd, map, x, y);
  6330. return 0;
  6331. }
  6332. /*==========================================
  6333. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6334. *------------------------------------------*/
  6335. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6336. {
  6337. int type;
  6338. time_t timer;
  6339. struct tm *t;
  6340. type=script_getnum(st,2);
  6341. switch(type){
  6342. case 2:
  6343. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6344. // from the system clock.)
  6345. script_pushint(st,(int)time(NULL));
  6346. break;
  6347. case 1:
  6348. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6349. time(&timer);
  6350. t=localtime(&timer);
  6351. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6352. break;
  6353. case 0:
  6354. default:
  6355. //type 0:(System Ticks)
  6356. script_pushint(st,gettick());
  6357. break;
  6358. }
  6359. return 0;
  6360. }
  6361. /*==========================================
  6362. * GetTime(Type);
  6363. * 1: Sec 2: Min 3: Hour
  6364. * 4: WeekDay 5: MonthDay 6: Month
  6365. * 7: Year
  6366. *------------------------------------------*/
  6367. BUILDIN_FUNC(gettime) /* Asgard Version */
  6368. {
  6369. int type;
  6370. time_t timer;
  6371. struct tm *t;
  6372. type=script_getnum(st,2);
  6373. time(&timer);
  6374. t=localtime(&timer);
  6375. switch(type){
  6376. case 1://Sec(0~59)
  6377. script_pushint(st,t->tm_sec);
  6378. break;
  6379. case 2://Min(0~59)
  6380. script_pushint(st,t->tm_min);
  6381. break;
  6382. case 3://Hour(0~23)
  6383. script_pushint(st,t->tm_hour);
  6384. break;
  6385. case 4://WeekDay(0~6)
  6386. script_pushint(st,t->tm_wday);
  6387. break;
  6388. case 5://MonthDay(01~31)
  6389. script_pushint(st,t->tm_mday);
  6390. break;
  6391. case 6://Month(01~12)
  6392. script_pushint(st,t->tm_mon+1);
  6393. break;
  6394. case 7://Year(20xx)
  6395. script_pushint(st,t->tm_year+1900);
  6396. break;
  6397. case 8://Year Day(01~366)
  6398. script_pushint(st,t->tm_yday+1);
  6399. break;
  6400. default://(format error)
  6401. script_pushint(st,-1);
  6402. break;
  6403. }
  6404. return 0;
  6405. }
  6406. /*==========================================
  6407. * GetTimeStr("TimeFMT", Length);
  6408. *------------------------------------------*/
  6409. BUILDIN_FUNC(gettimestr)
  6410. {
  6411. char *tmpstr;
  6412. const char *fmtstr;
  6413. int maxlen;
  6414. time_t now = time(NULL);
  6415. fmtstr=script_getstr(st,2);
  6416. maxlen=script_getnum(st,3);
  6417. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6418. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6419. tmpstr[maxlen]='\0';
  6420. script_pushstr(st,tmpstr);
  6421. return 0;
  6422. }
  6423. /*==========================================
  6424. * カプラ倉庫を開く
  6425. *------------------------------------------*/
  6426. BUILDIN_FUNC(openstorage)
  6427. {
  6428. TBL_PC* sd;
  6429. sd = script_rid2sd(st);
  6430. if( sd == NULL )
  6431. return 0;
  6432. storage_storageopen(sd);
  6433. return 0;
  6434. }
  6435. BUILDIN_FUNC(guildopenstorage)
  6436. {
  6437. TBL_PC* sd;
  6438. int ret;
  6439. sd = script_rid2sd(st);
  6440. if( sd == NULL )
  6441. return 0;
  6442. ret = storage_guild_storageopen(sd);
  6443. script_pushint(st,ret);
  6444. return 0;
  6445. }
  6446. /*==========================================
  6447. * アイテムによるスキル発動
  6448. *------------------------------------------*/
  6449. BUILDIN_FUNC(itemskill)
  6450. {
  6451. int id,lv;
  6452. TBL_PC *sd=script_rid2sd(st);
  6453. id=script_getnum(st,2);
  6454. lv=script_getnum(st,3);
  6455. if(!sd || sd->ud.skilltimer != -1)
  6456. return 0;
  6457. sd->skillitem=id;
  6458. sd->skillitemlv=lv;
  6459. clif_item_skill(sd,id,lv);
  6460. return 0;
  6461. }
  6462. /*==========================================
  6463. * アイテム作成
  6464. *------------------------------------------*/
  6465. BUILDIN_FUNC(produce)
  6466. {
  6467. int trigger;
  6468. TBL_PC* sd;
  6469. sd = script_rid2sd(st);
  6470. if( sd == NULL )
  6471. return 0;
  6472. trigger=script_getnum(st,2);
  6473. clif_skill_produce_mix_list(sd, trigger);
  6474. return 0;
  6475. }
  6476. /*==========================================
  6477. * NPCでペット作る
  6478. *------------------------------------------*/
  6479. BUILDIN_FUNC(makepet)
  6480. {
  6481. TBL_PC* sd;
  6482. int id,pet_id;
  6483. id=script_getnum(st,2);
  6484. sd = script_rid2sd(st);
  6485. if( sd == NULL )
  6486. return 0;
  6487. pet_id = search_petDB_index(id, PET_CLASS);
  6488. if (pet_id < 0)
  6489. pet_id = search_petDB_index(id, PET_EGG);
  6490. if (pet_id >= 0 && sd) {
  6491. sd->catch_target_class = pet_db[pet_id].class_;
  6492. intif_create_pet(
  6493. sd->status.account_id, sd->status.char_id,
  6494. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6495. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6496. 100, 0, 1, pet_db[pet_id].jname);
  6497. }
  6498. return 0;
  6499. }
  6500. /*==========================================
  6501. * NPCで経験値上げる
  6502. *------------------------------------------*/
  6503. BUILDIN_FUNC(getexp)
  6504. {
  6505. TBL_PC* sd;
  6506. int base=0,job=0;
  6507. double bonus;
  6508. sd = script_rid2sd(st);
  6509. if( sd == NULL )
  6510. return 0;
  6511. base=script_getnum(st,2);
  6512. job =script_getnum(st,3);
  6513. if(base<0 || job<0)
  6514. return 0;
  6515. // bonus for npc-given exp
  6516. bonus = battle_config.quest_exp_rate / 100.;
  6517. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6518. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6519. pc_gainexp(sd, NULL, base, job);
  6520. return 0;
  6521. }
  6522. /*==========================================
  6523. * Gain guild exp [Celest]
  6524. *------------------------------------------*/
  6525. BUILDIN_FUNC(guildgetexp)
  6526. {
  6527. TBL_PC* sd;
  6528. int exp;
  6529. sd = script_rid2sd(st);
  6530. if( sd == NULL )
  6531. return 0;
  6532. exp = script_getnum(st,2);
  6533. if(exp < 0)
  6534. return 0;
  6535. if(sd && sd->status.guild_id > 0)
  6536. guild_getexp (sd, exp);
  6537. return 0;
  6538. }
  6539. /*==========================================
  6540. * Changes the guild master of a guild [Skotlex]
  6541. *------------------------------------------*/
  6542. BUILDIN_FUNC(guildchangegm)
  6543. {
  6544. TBL_PC *sd;
  6545. int guild_id;
  6546. const char *name;
  6547. guild_id = script_getnum(st,2);
  6548. name = script_getstr(st,3);
  6549. sd=map_nick2sd(name);
  6550. if (!sd)
  6551. script_pushint(st,0);
  6552. else
  6553. script_pushint(st,guild_gm_change(guild_id, sd));
  6554. return 0;
  6555. }
  6556. /*==========================================
  6557. * モンスター発生
  6558. *------------------------------------------*/
  6559. BUILDIN_FUNC(monster)
  6560. {
  6561. const char* map = script_getstr(st,2);
  6562. int x = script_getnum(st,3);
  6563. int y = script_getnum(st,4);
  6564. const char* str = script_getstr(st,5);
  6565. int class_ = script_getnum(st,6);
  6566. int amount = script_getnum(st,7);
  6567. const char* event = "";
  6568. struct map_session_data* sd;
  6569. int m;
  6570. if( script_hasdata(st,8) )
  6571. {
  6572. event = script_getstr(st,8);
  6573. check_event(st, event);
  6574. }
  6575. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6576. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6577. return 1;
  6578. }
  6579. sd = map_id2sd(st->rid);
  6580. if( sd && strcmp(map,"this") == 0 )
  6581. m = sd->bl.m;
  6582. else
  6583. m = map_mapname2mapid(map);
  6584. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6585. return 0;
  6586. }
  6587. /*==========================================
  6588. * モンスター発生
  6589. *------------------------------------------*/
  6590. BUILDIN_FUNC(areamonster)
  6591. {
  6592. const char* map = script_getstr(st,2);
  6593. int x0 = script_getnum(st,3);
  6594. int y0 = script_getnum(st,4);
  6595. int x1 = script_getnum(st,5);
  6596. int y1 = script_getnum(st,6);
  6597. const char* str = script_getstr(st,7);
  6598. int class_ = script_getnum(st,8);
  6599. int amount = script_getnum(st,9);
  6600. const char* event = "";
  6601. struct map_session_data* sd;
  6602. int m;
  6603. if( script_hasdata(st,10) )
  6604. {
  6605. event = script_getstr(st,10);
  6606. check_event(st, event);
  6607. }
  6608. sd = map_id2sd(st->rid);
  6609. if( sd && strcmp(map,"this") == 0 )
  6610. m = sd->bl.m;
  6611. else
  6612. m = map_mapname2mapid(map);
  6613. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6614. return 0;
  6615. }
  6616. /*==========================================
  6617. * モンスター削除
  6618. *------------------------------------------*/
  6619. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6620. {
  6621. TBL_MOB* md = (TBL_MOB*)bl;
  6622. char *event=va_arg(ap,char *);
  6623. int allflag=va_arg(ap,int);
  6624. if(!allflag){
  6625. if(strcmp(event,md->npc_event)==0)
  6626. status_kill(bl);
  6627. }else{
  6628. if(!md->spawn)
  6629. status_kill(bl);
  6630. }
  6631. return 0;
  6632. }
  6633. BUILDIN_FUNC(killmonster)
  6634. {
  6635. const char *mapname,*event;
  6636. int m,allflag=0;
  6637. mapname=script_getstr(st,2);
  6638. event=script_getstr(st,3);
  6639. if(strcmp(event,"All")==0)
  6640. allflag = 1;
  6641. else
  6642. check_event(st, event);
  6643. if( (m=map_mapname2mapid(mapname))<0 )
  6644. return 0;
  6645. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6646. return 0;
  6647. }
  6648. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6649. {
  6650. status_kill(bl);
  6651. return 0;
  6652. }
  6653. BUILDIN_FUNC(killmonsterall)
  6654. {
  6655. const char *mapname;
  6656. int m;
  6657. mapname=script_getstr(st,2);
  6658. if( (m=map_mapname2mapid(mapname))<0 )
  6659. return 0;
  6660. map_foreachinmap(buildin_killmonsterall_sub,
  6661. m,BL_MOB);
  6662. return 0;
  6663. }
  6664. /*==========================================
  6665. * Creates a clone of a player.
  6666. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6667. *------------------------------------------*/
  6668. BUILDIN_FUNC(clone)
  6669. {
  6670. TBL_PC *sd, *msd=NULL;
  6671. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6672. unsigned int duration = 0;
  6673. const char *map,*event="";
  6674. map=script_getstr(st,2);
  6675. x=script_getnum(st,3);
  6676. y=script_getnum(st,4);
  6677. event=script_getstr(st,5);
  6678. char_id=script_getnum(st,6);
  6679. if( script_hasdata(st,7) )
  6680. master_id=script_getnum(st,7);
  6681. if( script_hasdata(st,8) )
  6682. mode=script_getnum(st,8);
  6683. if( script_hasdata(st,9) )
  6684. flag=script_getnum(st,9);
  6685. if( script_hasdata(st,10) )
  6686. duration=script_getnum(st,10);
  6687. check_event(st, event);
  6688. m = map_mapname2mapid(map);
  6689. if (m < 0) return 0;
  6690. sd = map_charid2sd(char_id);
  6691. if (master_id) {
  6692. msd = map_charid2sd(master_id);
  6693. if (msd)
  6694. master_id = msd->bl.id;
  6695. else
  6696. master_id = 0;
  6697. }
  6698. if (sd) //Return ID of newly crafted clone.
  6699. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6700. else //Failed to create clone.
  6701. script_pushint(st,0);
  6702. return 0;
  6703. }
  6704. /*==========================================
  6705. * イベント実行
  6706. *------------------------------------------*/
  6707. BUILDIN_FUNC(doevent)
  6708. {
  6709. const char* event = script_getstr(st,2);
  6710. check_event(st, event);
  6711. npc_event(map_id2sd(st->rid),event,0);
  6712. return 0;
  6713. }
  6714. /*==========================================
  6715. * NPC主体イベント実行
  6716. *------------------------------------------*/
  6717. BUILDIN_FUNC(donpcevent)
  6718. {
  6719. const char* event = script_getstr(st,2);
  6720. check_event(st, event);
  6721. npc_event_do(event);
  6722. return 0;
  6723. }
  6724. /// for Aegis compatibility
  6725. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6726. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6727. {
  6728. const char* npc = script_getstr(st,2);
  6729. const char* command = script_getstr(st,3);
  6730. char event[51];
  6731. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6732. check_event(st, event);
  6733. npc_event_do(event);
  6734. return 0;
  6735. }
  6736. /*==========================================
  6737. * イベントタイマー追加
  6738. *------------------------------------------*/
  6739. BUILDIN_FUNC(addtimer)
  6740. {
  6741. int tick = script_getnum(st,2);
  6742. const char* event = script_getstr(st, 3);
  6743. TBL_PC* sd;
  6744. check_event(st, event);
  6745. sd = script_rid2sd(st);
  6746. if( sd == NULL )
  6747. return 0;
  6748. pc_addeventtimer(sd,tick,event);
  6749. return 0;
  6750. }
  6751. /*==========================================
  6752. * イベントタイマー削除
  6753. *------------------------------------------*/
  6754. BUILDIN_FUNC(deltimer)
  6755. {
  6756. const char *event;
  6757. TBL_PC* sd;
  6758. event=script_getstr(st, 2);
  6759. sd = script_rid2sd(st);
  6760. if( sd == NULL )
  6761. return 0;
  6762. check_event(st, event);
  6763. pc_deleventtimer(sd,event);
  6764. return 0;
  6765. }
  6766. /*==========================================
  6767. * イベントタイマーのカウント値追加
  6768. *------------------------------------------*/
  6769. BUILDIN_FUNC(addtimercount)
  6770. {
  6771. const char *event;
  6772. int tick;
  6773. TBL_PC* sd;
  6774. event=script_getstr(st, 2);
  6775. tick=script_getnum(st,3);
  6776. sd = script_rid2sd(st);
  6777. if( sd == NULL )
  6778. return 0;
  6779. check_event(st, event);
  6780. pc_addeventtimercount(sd,event,tick);
  6781. return 0;
  6782. }
  6783. /*==========================================
  6784. * NPCタイマー初期化
  6785. *------------------------------------------*/
  6786. BUILDIN_FUNC(initnpctimer)
  6787. {
  6788. struct npc_data *nd;
  6789. int flag = 0;
  6790. if( script_hasdata(st,3) )
  6791. { //Two arguments: NPC name and attach flag.
  6792. nd = npc_name2id(script_getstr(st, 2));
  6793. flag = script_getnum(st,3);
  6794. } else
  6795. if( script_hasdata(st,2) )
  6796. { //Check if argument is numeric (flag) or string (npc name)
  6797. struct script_data *data;
  6798. data = script_getdata(st,2);
  6799. get_val(st,data);
  6800. if( data_isstring(data) ) //NPC name
  6801. nd = npc_name2id(script_getstr(st, 2));
  6802. else if( data_isint(data) ) { //Flag
  6803. nd = (struct npc_data *)map_id2bl(st->oid);
  6804. flag = script_getnum(st,3);
  6805. } else {
  6806. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6807. return 1;
  6808. }
  6809. } else
  6810. nd=(struct npc_data *)map_id2bl(st->oid);
  6811. if (!nd) return 0;
  6812. if (flag) { //Attach
  6813. TBL_PC* sd = script_rid2sd(st);
  6814. if( sd == NULL )
  6815. return 0;
  6816. nd->u.scr.rid = sd->bl.id;
  6817. }
  6818. npc_settimerevent_tick(nd,0);
  6819. npc_timerevent_start(nd, st->rid);
  6820. return 0;
  6821. }
  6822. /*==========================================
  6823. * NPCタイマー開始
  6824. *------------------------------------------*/
  6825. BUILDIN_FUNC(startnpctimer)
  6826. {
  6827. struct npc_data *nd;
  6828. int flag = 0;
  6829. if( script_hasdata(st,3) )
  6830. { //Two arguments: NPC name and attach flag.
  6831. nd = npc_name2id(script_getstr(st, 2));
  6832. flag = script_getnum(st,3);
  6833. } else
  6834. if( script_hasdata(st,2) )
  6835. { //Check if argument is numeric (flag) or string (npc name)
  6836. struct script_data *data;
  6837. data = script_getdata(st,2);
  6838. get_val(st,data);
  6839. if( data_isstring(data) ) //NPC name
  6840. nd = npc_name2id(script_getstr(st, 2));
  6841. else if( data_isint(data) ) { //Flag
  6842. nd = (struct npc_data *)map_id2bl(st->oid);
  6843. flag = script_getnum(st,3);
  6844. } else {
  6845. ShowError("startnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6846. return 1;
  6847. }
  6848. } else
  6849. nd=(struct npc_data *)map_id2bl(st->oid);
  6850. if (!nd) return 0;
  6851. if (flag) { //Attach
  6852. TBL_PC* sd = script_rid2sd(st);
  6853. if( sd == NULL )
  6854. return 0;
  6855. nd->u.scr.rid = sd->bl.id;
  6856. }
  6857. npc_timerevent_start(nd, st->rid);
  6858. return 0;
  6859. }
  6860. /*==========================================
  6861. * NPCタイマー停止
  6862. *------------------------------------------*/
  6863. BUILDIN_FUNC(stopnpctimer)
  6864. {
  6865. struct npc_data *nd;
  6866. int flag = 0;
  6867. if( script_hasdata(st,3) )
  6868. { //Two arguments: NPC name and attach flag.
  6869. nd = npc_name2id(script_getstr(st, 2));
  6870. flag = script_getnum(st,3);
  6871. } else
  6872. if( script_hasdata(st,2) )
  6873. { //Check if argument is numeric (flag) or string (npc name)
  6874. struct script_data *data;
  6875. data = script_getdata(st,2);
  6876. get_val(st,data);
  6877. if( data_isstring(data) ) //NPC name
  6878. nd = npc_name2id(script_getstr(st, 2));
  6879. else if( data_isint(data) ) { //Flag
  6880. nd = (struct npc_data *)map_id2bl(st->oid);
  6881. flag = script_getnum(st,3);
  6882. } else {
  6883. ShowError("stopnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6884. return 1;
  6885. }
  6886. } else
  6887. nd=(struct npc_data *)map_id2bl(st->oid);
  6888. if (!nd) return 0;
  6889. if (flag) //Detach
  6890. nd->u.scr.rid = 0;
  6891. npc_timerevent_stop(nd);
  6892. return 0;
  6893. }
  6894. /*==========================================
  6895. * NPCタイマー情報所得
  6896. *------------------------------------------*/
  6897. BUILDIN_FUNC(getnpctimer)
  6898. {
  6899. struct npc_data *nd;
  6900. TBL_PC *sd;
  6901. int type=script_getnum(st,2);
  6902. int val=0;
  6903. if( script_hasdata(st,3) )
  6904. nd = npc_name2id(script_getstr(st,3));
  6905. else
  6906. nd = (struct npc_data *)map_id2bl(st->oid);
  6907. if (!nd || nd->bl.type != BL_NPC)
  6908. {
  6909. script_pushint(st,0);
  6910. ShowError("getnpctimer: Invalid NPC\n");
  6911. return 1;
  6912. }
  6913. switch(type){
  6914. case 0: val=npc_gettimerevent_tick(nd); break;
  6915. case 1:
  6916. if (nd->u.scr.rid) {
  6917. sd = map_id2sd(nd->u.scr.rid);
  6918. if (!sd) {
  6919. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6920. break;
  6921. }
  6922. val = (sd->npc_timer_id != -1);
  6923. } else
  6924. val= (nd->u.scr.timerid !=-1);
  6925. break;
  6926. case 2: val= nd->u.scr.timeramount; break;
  6927. }
  6928. script_pushint(st,val);
  6929. return 0;
  6930. }
  6931. /*==========================================
  6932. * NPCタイマー値設定
  6933. *------------------------------------------*/
  6934. BUILDIN_FUNC(setnpctimer)
  6935. {
  6936. int tick;
  6937. struct npc_data *nd;
  6938. tick=script_getnum(st,2);
  6939. if( script_hasdata(st,3) )
  6940. nd=npc_name2id(script_getstr(st,3));
  6941. else
  6942. nd=(struct npc_data *)map_id2bl(st->oid);
  6943. npc_settimerevent_tick(nd,tick);
  6944. return 0;
  6945. }
  6946. /*==========================================
  6947. * attaches the player rid to the timer [Celest]
  6948. *------------------------------------------*/
  6949. BUILDIN_FUNC(attachnpctimer)
  6950. {
  6951. TBL_PC *sd;
  6952. struct npc_data *nd;
  6953. nd=(struct npc_data *)map_id2bl(st->oid);
  6954. if( script_hasdata(st,2) )
  6955. sd=map_nick2sd(script_getstr(st,2));
  6956. else
  6957. sd = script_rid2sd(st);
  6958. if (sd==NULL)
  6959. return 0;
  6960. nd->u.scr.rid = sd->bl.id;
  6961. return 0;
  6962. }
  6963. /*==========================================
  6964. * detaches a player rid from the timer [Celest]
  6965. *------------------------------------------*/
  6966. BUILDIN_FUNC(detachnpctimer)
  6967. {
  6968. struct npc_data *nd;
  6969. if( script_hasdata(st,2) )
  6970. nd=npc_name2id(script_getstr(st,2));
  6971. else
  6972. nd=(struct npc_data *)map_id2bl(st->oid);
  6973. nd->u.scr.rid = 0;
  6974. return 0;
  6975. }
  6976. /*==========================================
  6977. * To avoid "player not attached" script errors, this function is provided,
  6978. * it checks if there is a player attached to the current script. [Skotlex]
  6979. * If no, returns 0, if yes, returns the account_id of the attached player.
  6980. *------------------------------------------*/
  6981. BUILDIN_FUNC(playerattached)
  6982. {
  6983. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  6984. script_pushint(st,0);
  6985. else
  6986. script_pushint(st,st->rid);
  6987. return 0;
  6988. }
  6989. /*==========================================
  6990. * 天の声アナウンス
  6991. *------------------------------------------*/
  6992. BUILDIN_FUNC(announce)
  6993. {
  6994. const char *str, *color=NULL;
  6995. int flag;
  6996. str=script_getstr(st,2);
  6997. flag=script_getnum(st,3);
  6998. if (script_hasdata(st,4))
  6999. color=script_getstr(st,4);
  7000. if(flag&0x0f){
  7001. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  7002. (struct block_list *)script_rid2sd(st);
  7003. if( bl == NULL )
  7004. return 0;
  7005. if (color)
  7006. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  7007. else
  7008. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  7009. }else {
  7010. if (color)
  7011. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  7012. else
  7013. intif_GMmessage(str,(int)strlen(str)+1,flag);
  7014. }
  7015. return 0;
  7016. }
  7017. /*==========================================
  7018. * 天の声アナウンス(特定マップ)
  7019. *------------------------------------------*/
  7020. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  7021. {
  7022. char *str, *color;
  7023. int len,flag;
  7024. str=va_arg(ap,char *);
  7025. len=va_arg(ap,int);
  7026. flag=va_arg(ap,int);
  7027. color=va_arg(ap,char *);
  7028. if (color)
  7029. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7030. else
  7031. clif_GMmessage(bl,str,len,flag|3);
  7032. return 0;
  7033. }
  7034. BUILDIN_FUNC(mapannounce)
  7035. {
  7036. const char *mapname,*str, *color=NULL;
  7037. int flag,m;
  7038. mapname=script_getstr(st,2);
  7039. str=script_getstr(st,3);
  7040. flag=script_getnum(st,4);
  7041. if (script_hasdata(st,5))
  7042. color=script_getstr(st,5);
  7043. if( (m=map_mapname2mapid(mapname))<0 )
  7044. return 0;
  7045. map_foreachinmap(buildin_mapannounce_sub,
  7046. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7047. return 0;
  7048. }
  7049. /*==========================================
  7050. * 天の声アナウンス(特定エリア)
  7051. *------------------------------------------*/
  7052. BUILDIN_FUNC(areaannounce)
  7053. {
  7054. const char *map,*str,*color=NULL;
  7055. int flag,m;
  7056. int x0,y0,x1,y1;
  7057. map=script_getstr(st,2);
  7058. x0=script_getnum(st,3);
  7059. y0=script_getnum(st,4);
  7060. x1=script_getnum(st,5);
  7061. y1=script_getnum(st,6);
  7062. str=script_getstr(st,7);
  7063. flag=script_getnum(st,8);
  7064. if (script_hasdata(st,9))
  7065. color=script_getstr(st,9);
  7066. if( (m=map_mapname2mapid(map))<0 )
  7067. return 0;
  7068. map_foreachinarea(buildin_mapannounce_sub,
  7069. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7070. return 0;
  7071. }
  7072. /*==========================================
  7073. * ユーザー数所得
  7074. *------------------------------------------*/
  7075. BUILDIN_FUNC(getusers)
  7076. {
  7077. int flag=script_getnum(st,2);
  7078. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7079. int val=0;
  7080. switch(flag&0x07){
  7081. case 0: val=map[bl->m].users; break;
  7082. case 1: val=map_getusers(); break;
  7083. }
  7084. script_pushint(st,val);
  7085. return 0;
  7086. }
  7087. /*==========================================
  7088. * Works like @WHO - displays all online users names in window
  7089. *------------------------------------------*/
  7090. BUILDIN_FUNC(getusersname)
  7091. {
  7092. TBL_PC *sd, *pl_sd = NULL, **pl_allsd;
  7093. int i=0,disp_num=1, users;
  7094. sd = script_rid2sd(st);
  7095. if (!sd) return 0;
  7096. pl_allsd = map_getallusers(&users);
  7097. for (i=0;i<users;i++)
  7098. {
  7099. pl_sd = pl_allsd[i];
  7100. if( !(battle_config.hide_GM_session && pc_isGM(pl_sd)) )
  7101. {
  7102. if((disp_num++)%10==0)
  7103. clif_scriptnext(sd,st->oid);
  7104. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7105. }
  7106. }
  7107. return 0;
  7108. }
  7109. /*==========================================
  7110. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7111. *------------------------------------------*/
  7112. BUILDIN_FUNC(getmapguildusers)
  7113. {
  7114. const char *str;
  7115. int m, gid;
  7116. int i=0,c=0;
  7117. struct guild *g = NULL;
  7118. str=script_getstr(st,2);
  7119. gid=script_getnum(st,3);
  7120. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7121. script_pushint(st,-1);
  7122. return 0;
  7123. }
  7124. g = guild_search(gid);
  7125. if (g){
  7126. for(i = 0; i < g->max_member; i++)
  7127. {
  7128. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7129. c++;
  7130. }
  7131. }
  7132. script_pushint(st,c);
  7133. return 0;
  7134. }
  7135. /*==========================================
  7136. * マップ指定ユーザー数所得
  7137. *------------------------------------------*/
  7138. BUILDIN_FUNC(getmapusers)
  7139. {
  7140. const char *str;
  7141. int m;
  7142. str=script_getstr(st,2);
  7143. if( (m=map_mapname2mapid(str))< 0){
  7144. script_pushint(st,-1);
  7145. return 0;
  7146. }
  7147. script_pushint(st,map[m].users);
  7148. return 0;
  7149. }
  7150. /*==========================================
  7151. * エリア指定ユーザー数所得
  7152. *------------------------------------------*/
  7153. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7154. {
  7155. int *users=va_arg(ap,int *);
  7156. (*users)++;
  7157. return 0;
  7158. }
  7159. BUILDIN_FUNC(getareausers)
  7160. {
  7161. const char *str;
  7162. int m,x0,y0,x1,y1,users=0;
  7163. str=script_getstr(st,2);
  7164. x0=script_getnum(st,3);
  7165. y0=script_getnum(st,4);
  7166. x1=script_getnum(st,5);
  7167. y1=script_getnum(st,6);
  7168. if( (m=map_mapname2mapid(str))< 0){
  7169. script_pushint(st,-1);
  7170. return 0;
  7171. }
  7172. map_foreachinarea(buildin_getareausers_sub,
  7173. m,x0,y0,x1,y1,BL_PC,&users);
  7174. script_pushint(st,users);
  7175. return 0;
  7176. }
  7177. /*==========================================
  7178. * エリア指定ドロップアイテム数所得
  7179. *------------------------------------------*/
  7180. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7181. {
  7182. int item=va_arg(ap,int);
  7183. int *amount=va_arg(ap,int *);
  7184. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7185. if(drop->item_data.nameid==item)
  7186. (*amount)+=drop->item_data.amount;
  7187. return 0;
  7188. }
  7189. BUILDIN_FUNC(getareadropitem)
  7190. {
  7191. const char *str;
  7192. int m,x0,y0,x1,y1,item,amount=0;
  7193. struct script_data *data;
  7194. str=script_getstr(st,2);
  7195. x0=script_getnum(st,3);
  7196. y0=script_getnum(st,4);
  7197. x1=script_getnum(st,5);
  7198. y1=script_getnum(st,6);
  7199. data=script_getdata(st,7);
  7200. get_val(st,data);
  7201. if( data_isstring(data) ){
  7202. const char *name=conv_str(st,data);
  7203. struct item_data *item_data = itemdb_searchname(name);
  7204. item=UNKNOWN_ITEM_ID;
  7205. if( item_data )
  7206. item=item_data->nameid;
  7207. }else
  7208. item=conv_num(st,data);
  7209. if( (m=map_mapname2mapid(str))< 0){
  7210. script_pushint(st,-1);
  7211. return 0;
  7212. }
  7213. map_foreachinarea(buildin_getareadropitem_sub,
  7214. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7215. script_pushint(st,amount);
  7216. return 0;
  7217. }
  7218. /*==========================================
  7219. * NPCの有効化
  7220. *------------------------------------------*/
  7221. BUILDIN_FUNC(enablenpc)
  7222. {
  7223. const char *str;
  7224. str=script_getstr(st,2);
  7225. npc_enable(str,1);
  7226. return 0;
  7227. }
  7228. /*==========================================
  7229. * NPCの無効化
  7230. *------------------------------------------*/
  7231. BUILDIN_FUNC(disablenpc)
  7232. {
  7233. const char *str;
  7234. str=script_getstr(st,2);
  7235. npc_enable(str,0);
  7236. return 0;
  7237. }
  7238. /*==========================================
  7239. * 隠れているNPCの表示
  7240. *------------------------------------------*/
  7241. BUILDIN_FUNC(hideoffnpc)
  7242. {
  7243. const char *str;
  7244. str=script_getstr(st,2);
  7245. npc_enable(str,2);
  7246. return 0;
  7247. }
  7248. /*==========================================
  7249. * NPCをハイディング
  7250. *------------------------------------------*/
  7251. BUILDIN_FUNC(hideonnpc)
  7252. {
  7253. const char *str;
  7254. str=script_getstr(st,2);
  7255. npc_enable(str,4);
  7256. return 0;
  7257. }
  7258. /// Starts a status effect on the target unit or on the attached player.
  7259. ///
  7260. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7261. BUILDIN_FUNC(sc_start)
  7262. {
  7263. struct block_list* bl;
  7264. int type;
  7265. int tick;
  7266. int val1;
  7267. int val4 = 0;
  7268. type = script_getnum(st,2);
  7269. tick = script_getnum(st,3);
  7270. val1 = script_getnum(st,4);
  7271. if( script_hasdata(st,5) )
  7272. bl = map_id2bl(script_getnum(st,5));
  7273. else
  7274. bl = map_id2bl(st->rid);
  7275. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7276. {// When there isn't a duration specified, try to get it from the skill_db
  7277. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7278. }
  7279. if( potion_flag == 1 && potion_target )
  7280. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7281. bl = map_id2bl(potion_target);
  7282. tick /= 2;// Thrown potions only last half.
  7283. val4 = 1;// Mark that this was a thrown sc_effect
  7284. }
  7285. if( bl )
  7286. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 1|2|8);
  7287. return 0;
  7288. }
  7289. /// Starts a status effect on the target unit or on the attached player.
  7290. ///
  7291. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7292. BUILDIN_FUNC(sc_start2)
  7293. {
  7294. struct block_list* bl;
  7295. int type;
  7296. int tick;
  7297. int val1;
  7298. int val4 = 0;
  7299. int rate;
  7300. type = script_getnum(st,2);
  7301. tick = script_getnum(st,3);
  7302. val1 = script_getnum(st,4);
  7303. rate = script_getnum(st,5);
  7304. if( script_hasdata(st,6) )
  7305. bl = map_id2bl(script_getnum(st,6));
  7306. else
  7307. bl = map_id2bl(st->rid);
  7308. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7309. {// When there isn't a duration specified, try to get it from the skill_db
  7310. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7311. }
  7312. if( potion_flag == 1 && potion_target )
  7313. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7314. bl = map_id2bl(potion_target);
  7315. tick /= 2;// Thrown potions only last half.
  7316. val4 = 1;// Mark that this was a thrown sc_effect
  7317. }
  7318. if( bl )
  7319. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 1|2|8);
  7320. return 0;
  7321. }
  7322. /// Starts a status effect on the target unit or on the attached player.
  7323. ///
  7324. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7325. BUILDIN_FUNC(sc_start4)
  7326. {
  7327. struct block_list* bl;
  7328. int type;
  7329. int tick;
  7330. int val1;
  7331. int val2;
  7332. int val3;
  7333. int val4;
  7334. type = script_getnum(st,2);
  7335. tick = script_getnum(st,3);
  7336. val1 = script_getnum(st,4);
  7337. val2 = script_getnum(st,5);
  7338. val3 = script_getnum(st,6);
  7339. val4 = script_getnum(st,7);
  7340. if( script_hasdata(st,8) )
  7341. bl = map_id2bl(script_getnum(st,8));
  7342. else
  7343. bl = map_id2bl(st->rid);
  7344. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7345. {// When there isn't a duration specified, try to get it from the skill_db
  7346. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7347. }
  7348. if( potion_flag == 1 && potion_target )
  7349. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7350. bl = map_id2bl(potion_target);
  7351. tick /= 2;// Thrown potions only last half.
  7352. }
  7353. if( bl )
  7354. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 1|2|8);
  7355. return 0;
  7356. }
  7357. /// Ends one or all status effects on the target unit or on the attached player.
  7358. ///
  7359. /// sc_end <effect_id>{,<unit_id>};
  7360. BUILDIN_FUNC(sc_end)
  7361. {
  7362. struct block_list* bl;
  7363. int type;
  7364. type = script_getnum(st,2);
  7365. if( script_hasdata(st,3) )
  7366. bl = map_id2bl(script_getnum(st,3));
  7367. else
  7368. bl = map_id2bl(st->rid);
  7369. if( potion_flag==1 && potion_target )
  7370. {//##TODO how does this work [FlavioJS]
  7371. bl = map_id2bl(potion_target);
  7372. }
  7373. if( !bl ) return 0l;
  7374. if( type >= 0 && type < SC_MAX )
  7375. {
  7376. struct status_change *sc = status_get_sc(bl);
  7377. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7378. if (!sce) return 0;
  7379. //This should help status_change_end force disabling the SC in case it has no limit.
  7380. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7381. status_change_end(bl, type, INVALID_TIMER);
  7382. } else
  7383. status_change_clear(bl, 2);// remove all effects
  7384. return 0;
  7385. }
  7386. /*==========================================
  7387. * 状態異常耐性を計算した確率を返す
  7388. *------------------------------------------*/
  7389. BUILDIN_FUNC(getscrate)
  7390. {
  7391. struct block_list *bl;
  7392. int type,rate;
  7393. type=script_getnum(st,2);
  7394. rate=script_getnum(st,3);
  7395. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7396. bl = map_id2bl(script_getnum(st,4));
  7397. else
  7398. bl = map_id2bl(st->rid);
  7399. if (bl)
  7400. rate = status_get_sc_def(bl,type, 10000, 10000, 0);
  7401. script_pushint(st,rate);
  7402. return 0;
  7403. }
  7404. /*==========================================
  7405. *
  7406. *------------------------------------------*/
  7407. BUILDIN_FUNC(debugmes)
  7408. {
  7409. const char *str;
  7410. str=script_getstr(st,2);
  7411. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7412. return 0;
  7413. }
  7414. /*==========================================
  7415. *捕獲アイテム使用
  7416. *------------------------------------------*/
  7417. BUILDIN_FUNC(catchpet)
  7418. {
  7419. int pet_id;
  7420. TBL_PC *sd;
  7421. pet_id= script_getnum(st,2);
  7422. sd=script_rid2sd(st);
  7423. if( sd == NULL )
  7424. return 0;
  7425. pet_catch_process1(sd,pet_id);
  7426. return 0;
  7427. }
  7428. /*==========================================
  7429. * [orn]
  7430. *------------------------------------------*/
  7431. BUILDIN_FUNC(homunculus_evolution)
  7432. {
  7433. TBL_PC *sd;
  7434. sd=script_rid2sd(st);
  7435. if( sd == NULL )
  7436. return 0;
  7437. if(merc_is_hom_active(sd->hd))
  7438. {
  7439. if (sd->hd->homunculus.intimacy > 91000)
  7440. merc_hom_evolution(sd->hd);
  7441. else
  7442. clif_emotion(&sd->hd->bl, 4) ; //swt
  7443. }
  7444. return 0;
  7445. }
  7446. // [Zephyrus]
  7447. BUILDIN_FUNC(homunculus_shuffle)
  7448. {
  7449. TBL_PC *sd;
  7450. sd=script_rid2sd(st);
  7451. if( sd == NULL )
  7452. return 0;
  7453. if(merc_is_hom_active(sd->hd))
  7454. merc_hom_shuffle(sd->hd);
  7455. return 0;
  7456. }
  7457. //These two functions bring the eA MAPID_* class functionality to scripts.
  7458. BUILDIN_FUNC(eaclass)
  7459. {
  7460. int class_;
  7461. if( script_hasdata(st,2) )
  7462. class_ = script_getnum(st,2);
  7463. else {
  7464. TBL_PC *sd;
  7465. sd=script_rid2sd(st);
  7466. if (!sd) {
  7467. script_pushint(st,-1);
  7468. return 0;
  7469. }
  7470. class_ = sd->status.class_;
  7471. }
  7472. script_pushint(st,pc_jobid2mapid(class_));
  7473. return 0;
  7474. }
  7475. BUILDIN_FUNC(roclass)
  7476. {
  7477. int class_ =script_getnum(st,2);
  7478. int sex;
  7479. if( script_hasdata(st,3) )
  7480. sex = script_getnum(st,3);
  7481. else {
  7482. TBL_PC *sd;
  7483. if (st->rid && (sd=script_rid2sd(st)))
  7484. sex = sd->status.sex;
  7485. else
  7486. sex = 1; //Just use male when not found.
  7487. }
  7488. script_pushint(st,pc_mapid2jobid(class_, sex));
  7489. return 0;
  7490. }
  7491. /*==========================================
  7492. *携帯卵孵化機使用
  7493. *------------------------------------------*/
  7494. BUILDIN_FUNC(birthpet)
  7495. {
  7496. TBL_PC *sd;
  7497. sd=script_rid2sd(st);
  7498. if( sd == NULL )
  7499. return 0;
  7500. clif_sendegg(sd);
  7501. return 0;
  7502. }
  7503. /*==========================================
  7504. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7505. *------------------------------------------*/
  7506. BUILDIN_FUNC(resetlvl)
  7507. {
  7508. TBL_PC *sd;
  7509. int type=script_getnum(st,2);
  7510. sd=script_rid2sd(st);
  7511. if( sd == NULL )
  7512. return 0;
  7513. pc_resetlvl(sd,type);
  7514. return 0;
  7515. }
  7516. /*==========================================
  7517. * ステータスリセット
  7518. *------------------------------------------*/
  7519. BUILDIN_FUNC(resetstatus)
  7520. {
  7521. TBL_PC *sd;
  7522. sd=script_rid2sd(st);
  7523. pc_resetstate(sd);
  7524. return 0;
  7525. }
  7526. /*==========================================
  7527. * script command resetskill
  7528. *------------------------------------------*/
  7529. BUILDIN_FUNC(resetskill)
  7530. {
  7531. TBL_PC *sd;
  7532. sd=script_rid2sd(st);
  7533. pc_resetskill(sd,1);
  7534. return 0;
  7535. }
  7536. /*==========================================
  7537. * Counts total amount of skill points.
  7538. *------------------------------------------*/
  7539. BUILDIN_FUNC(skillpointcount)
  7540. {
  7541. TBL_PC *sd;
  7542. sd=script_rid2sd(st);
  7543. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7544. return 0;
  7545. }
  7546. /*==========================================
  7547. *
  7548. *------------------------------------------*/
  7549. BUILDIN_FUNC(changebase)
  7550. {
  7551. TBL_PC *sd=NULL;
  7552. int vclass;
  7553. if( script_hasdata(st,3) )
  7554. sd=map_id2sd(script_getnum(st,3));
  7555. else
  7556. sd=script_rid2sd(st);
  7557. if(sd == NULL)
  7558. return 0;
  7559. vclass = script_getnum(st,2);
  7560. if(vclass == JOB_WEDDING)
  7561. {
  7562. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7563. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7564. )
  7565. return 0;
  7566. }
  7567. if(!sd->disguise && vclass != sd->vd.class_) {
  7568. status_set_viewdata(&sd->bl, vclass);
  7569. //Updated client view. Base, Weapon and Cloth Colors.
  7570. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7571. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7572. if (sd->vd.cloth_color)
  7573. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7574. }
  7575. return 0;
  7576. }
  7577. /*==========================================
  7578. * 性別変換
  7579. *------------------------------------------*/
  7580. BUILDIN_FUNC(changesex)
  7581. {
  7582. TBL_PC *sd = NULL;
  7583. sd = script_rid2sd(st);
  7584. if (sd->status.sex == 0) {
  7585. sd->status.sex = 1;
  7586. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7587. sd->status.class_ -= 1;
  7588. } else if (sd->status.sex == 1) {
  7589. sd->status.sex = 0;
  7590. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7591. sd->status.class_ += 1;
  7592. }
  7593. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  7594. chrif_save(sd,0);
  7595. return 0;
  7596. }
  7597. /*==========================================
  7598. * Works like 'announce' but outputs in the common chat window
  7599. *------------------------------------------*/
  7600. BUILDIN_FUNC(globalmes)
  7601. {
  7602. struct block_list *bl = map_id2bl(st->oid);
  7603. struct npc_data *nd = (struct npc_data *)bl;
  7604. const char *name=NULL,*mes;
  7605. mes=script_getstr(st,2); // メッセージの取得
  7606. if(mes==NULL) return 0;
  7607. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7608. name=script_getstr(st,3);
  7609. } else {
  7610. name=nd->name;
  7611. }
  7612. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7613. return 0;
  7614. }
  7615. /////////////////////////////////////////////////////////////////////
  7616. // NPC waiting room (chat room)
  7617. //
  7618. /// Creates a waiting room (chat room) for this npc.
  7619. ///
  7620. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7621. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7622. /// waitingroom "<title>",<limit>,"<event>";
  7623. /// waitingroom "<title>",<limit>;
  7624. BUILDIN_FUNC(waitingroom)
  7625. {
  7626. struct npc_data* nd;
  7627. const char* title;
  7628. const char* ev = "";
  7629. int limit;
  7630. int trigger = 0;
  7631. int pub = 1;
  7632. title = script_getstr(st, 2);
  7633. limit = script_getnum(st, 3);
  7634. if( script_hasdata(st,5) )
  7635. {
  7636. struct script_data* last = script_getdata(st, 5);
  7637. get_val(st, last);
  7638. if( data_isstring(last) )
  7639. {// ,<trigger>,"<event>"
  7640. trigger = script_getnum(st, 4);
  7641. ev = script_getstr(st, 5);
  7642. }
  7643. else
  7644. {// ,"<event>",<trigger>
  7645. ev = script_getstr(st, 4);
  7646. trigger = script_getnum(st,5);
  7647. }
  7648. }
  7649. else if( script_hasdata(st,4) )
  7650. {// ,"<event>"
  7651. ev = script_getstr(st, 4);
  7652. trigger = limit;
  7653. }
  7654. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  7655. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7656. return 0;
  7657. }
  7658. /// Removes the waiting room of the current or target npc.
  7659. ///
  7660. /// delwaitingroom "<npc_name>";
  7661. /// delwaitingroom;
  7662. BUILDIN_FUNC(delwaitingroom)
  7663. {
  7664. struct npc_data* nd;
  7665. if( script_hasdata(st,2) )
  7666. nd = npc_name2id(script_getstr(st, 2));
  7667. else
  7668. nd = (struct npc_data *)map_id2bl(st->oid);
  7669. if( nd != NULL )
  7670. chat_deletenpcchat(nd);
  7671. return 0;
  7672. }
  7673. /// Kicks all the players from the waiting room of the current or target npc.
  7674. ///
  7675. /// kickwaitingroomall "<npc_name>";
  7676. /// kickwaitingroomall;
  7677. BUILDIN_FUNC(waitingroomkickall)
  7678. {
  7679. struct npc_data* nd;
  7680. struct chat_data* cd;
  7681. if( script_hasdata(st,2) )
  7682. nd = npc_name2id(script_getstr(st,2));
  7683. else
  7684. nd = (struct npc_data *)map_id2bl(st->oid);
  7685. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7686. chat_npckickall(cd);
  7687. return 0;
  7688. }
  7689. /// Enables the waiting room event of the current or target npc.
  7690. ///
  7691. /// enablewaitingroomevent "<npc_name>";
  7692. /// enablewaitingroomevent;
  7693. BUILDIN_FUNC(enablewaitingroomevent)
  7694. {
  7695. struct npc_data* nd;
  7696. struct chat_data* cd;
  7697. if( script_hasdata(st,2) )
  7698. nd = npc_name2id(script_getstr(st, 2));
  7699. else
  7700. nd = (struct npc_data *)map_id2bl(st->oid);
  7701. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7702. chat_enableevent(cd);
  7703. return 0;
  7704. }
  7705. /// Disables the waiting room event of the current or target npc.
  7706. ///
  7707. /// disablewaitingroomevent "<npc_name>";
  7708. /// disablewaitingroomevent;
  7709. BUILDIN_FUNC(disablewaitingroomevent)
  7710. {
  7711. struct npc_data *nd;
  7712. struct chat_data *cd;
  7713. if( script_hasdata(st,2) )
  7714. nd = npc_name2id(script_getstr(st, 2));
  7715. else
  7716. nd = (struct npc_data *)map_id2bl(st->oid);
  7717. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7718. chat_disableevent(cd);
  7719. return 0;
  7720. }
  7721. /// Returns info on the waiting room of the current or target npc.
  7722. /// Returns -1 if the type unknown
  7723. /// <type>=0 : current number of users
  7724. /// <type>=1 : maximum number of users allowed
  7725. /// <type>=2 : the number of users that trigger the event
  7726. /// <type>=3 : if the trigger is disabled
  7727. /// <type>=4 : the title of the waiting room
  7728. /// <type>=5 : the password of the waiting room
  7729. /// <type>=16 : the name of the waiting room event
  7730. /// <type>=32 : if the waiting room is full
  7731. /// <type>=33 : if there are enough users to trigger the event
  7732. ///
  7733. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7734. /// getwaitingroomstate(<type>) -> <info>
  7735. BUILDIN_FUNC(getwaitingroomstate)
  7736. {
  7737. struct npc_data *nd;
  7738. struct chat_data *cd;
  7739. int type;
  7740. type = script_getnum(st,2);
  7741. if( script_hasdata(st,3) )
  7742. nd = npc_name2id(script_getstr(st, 3));
  7743. else
  7744. nd = (struct npc_data *)map_id2bl(st->oid);
  7745. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7746. {
  7747. script_pushint(st, -1);
  7748. return 0;
  7749. }
  7750. switch(type)
  7751. {
  7752. case 0: script_pushint(st, cd->users); break;
  7753. case 1: script_pushint(st, cd->limit); break;
  7754. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7755. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7756. case 4: script_pushstrcopy(st, cd->title); break;
  7757. case 5: script_pushstrcopy(st, cd->pass); break;
  7758. case 16: script_pushstrcopy(st, cd->npc_event);break;
  7759. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7760. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7761. default: script_pushint(st, -1); break;
  7762. }
  7763. return 0;
  7764. }
  7765. /// Warps the trigger or target amount of players to the target map and position.
  7766. /// Players are automatically removed from the waiting room.
  7767. /// Those waiting the longest will get warped first.
  7768. /// The target map can be "Random" for a random position in the current map,
  7769. /// and "SavePoint" for the savepoint map+position.
  7770. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7771. ///
  7772. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7773. /// The total number of teleported players is put into $@warpwaitingpcnum
  7774. ///
  7775. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7776. /// warpwaitingpc "<map name>",<x>,<y>;
  7777. BUILDIN_FUNC(warpwaitingpc)
  7778. {
  7779. int x;
  7780. int y;
  7781. int i;
  7782. int n;
  7783. const char* map_name;
  7784. struct npc_data* nd;
  7785. struct chat_data* cd;
  7786. TBL_PC* sd;
  7787. nd = (struct npc_data *)map_id2bl(st->oid);
  7788. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7789. return 0;
  7790. map_name = script_getstr(st,2);
  7791. x = script_getnum(st,3);
  7792. y = script_getnum(st,4);
  7793. n = cd->trigger&0x7f;
  7794. if( script_hasdata(st,5) )
  7795. n = script_getnum(st,5);
  7796. for( i = 0; i < n && cd->users > 0; i++ )
  7797. {
  7798. sd = cd->usersd[0];
  7799. if( sd == NULL )
  7800. {
  7801. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7802. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7803. continue;// Broken npc chat room?
  7804. }
  7805. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7806. if( strcmp(map_name,"Random") == 0 )
  7807. pc_randomwarp(sd,3);
  7808. else if( strcmp(map_name,"SavePoint") == 0 )
  7809. {
  7810. if( map[sd->bl.m].flag.noteleport )
  7811. return 0;// can't teleport on this map
  7812. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  7813. }
  7814. else
  7815. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7816. }
  7817. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7818. return 0;
  7819. }
  7820. /////////////////////////////////////////////////////////////////////
  7821. // ...
  7822. //
  7823. /*==========================================
  7824. * RIDのアタッチ
  7825. *------------------------------------------*/
  7826. BUILDIN_FUNC(attachrid)
  7827. {
  7828. st->rid=script_getnum(st,2);
  7829. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7830. return 0;
  7831. }
  7832. /*==========================================
  7833. * RIDのデタッチ
  7834. *------------------------------------------*/
  7835. BUILDIN_FUNC(detachrid)
  7836. {
  7837. st->rid=0;
  7838. return 0;
  7839. }
  7840. /*==========================================
  7841. * 存在チェック
  7842. *------------------------------------------*/
  7843. BUILDIN_FUNC(isloggedin)
  7844. {
  7845. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  7846. if (script_hasdata(st,3) && sd &&
  7847. sd->status.char_id != script_getnum(st,3))
  7848. sd = NULL;
  7849. push_val(st->stack,C_INT,sd!=NULL);
  7850. return 0;
  7851. }
  7852. /*==========================================
  7853. *
  7854. *------------------------------------------*/
  7855. BUILDIN_FUNC(setmapflagnosave)
  7856. {
  7857. int m,x,y;
  7858. unsigned short mapindex;
  7859. const char *str,*str2;
  7860. str=script_getstr(st,2);
  7861. str2=script_getstr(st,3);
  7862. x=script_getnum(st,4);
  7863. y=script_getnum(st,5);
  7864. m = map_mapname2mapid(str);
  7865. mapindex = mapindex_name2id(str2);
  7866. if(m >= 0 && mapindex) {
  7867. map[m].flag.nosave=1;
  7868. map[m].save.map=mapindex;
  7869. map[m].save.x=x;
  7870. map[m].save.y=y;
  7871. }
  7872. return 0;
  7873. }
  7874. BUILDIN_FUNC(setmapflag)
  7875. {
  7876. int m,i;
  7877. const char *str;
  7878. const char *val=NULL;
  7879. str=script_getstr(st,2);
  7880. i=script_getnum(st,3);
  7881. if(script_hasdata(st,4)){
  7882. val=script_getstr(st,4);
  7883. }
  7884. m = map_mapname2mapid(str);
  7885. if(m >= 0) {
  7886. switch(i) {
  7887. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  7888. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  7889. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  7890. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  7891. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  7892. case MF_PVP: map[m].flag.pvp=1; break;
  7893. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  7894. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  7895. case MF_GVG: map[m].flag.gvg=1; break;
  7896. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  7897. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  7898. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  7899. case MF_NOTRADE: map[m].flag.notrade=1; break;
  7900. case MF_NODROP: map[m].flag.nodrop=1; break;
  7901. case MF_NOSKILL: map[m].flag.noskill=1; break;
  7902. case MF_NOWARP: map[m].flag.nowarp=1; break;
  7903. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  7904. case MF_SNOW: map[m].flag.snow=1; break;
  7905. case MF_CLOUDS: map[m].flag.clouds=1; break;
  7906. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  7907. case MF_FOG: map[m].flag.fog=1; break;
  7908. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  7909. case MF_SAKURA: map[m].flag.sakura=1; break;
  7910. case MF_LEAVES: map[m].flag.leaves=1; break;
  7911. case MF_RAIN: map[m].flag.rain=1; break;
  7912. case MF_INDOORS: map[m].flag.indoors=1; break;
  7913. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  7914. case MF_NOGO: map[m].flag.nogo=1; break;
  7915. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  7916. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  7917. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  7918. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  7919. case MF_NORETURN: map[m].flag.noreturn=1; break;
  7920. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  7921. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  7922. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  7923. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  7924. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7925. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7926. case MF_NOVENDING: map[m].flag.novending=1; break;
  7927. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  7928. case MF_NOCHAT: map[m].flag.nochat=1; break;
  7929. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  7930. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  7931. }
  7932. }
  7933. return 0;
  7934. }
  7935. BUILDIN_FUNC(removemapflag)
  7936. {
  7937. int m,i;
  7938. const char *str;
  7939. str=script_getstr(st,2);
  7940. i=script_getnum(st,3);
  7941. m = map_mapname2mapid(str);
  7942. if(m >= 0) {
  7943. switch(i) {
  7944. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  7945. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  7946. case MF_NOSAVE: map[m].flag.nosave=0; break;
  7947. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  7948. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  7949. case MF_PVP: map[m].flag.pvp=0; break;
  7950. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  7951. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  7952. case MF_GVG: map[m].flag.gvg=0; break;
  7953. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  7954. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  7955. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  7956. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  7957. case MF_NOTRADE: map[m].flag.notrade=0; break;
  7958. case MF_NODROP: map[m].flag.nodrop=0; break;
  7959. case MF_NOSKILL: map[m].flag.noskill=0; break;
  7960. case MF_NOWARP: map[m].flag.nowarp=0; break;
  7961. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  7962. case MF_SNOW: map[m].flag.snow=0; break;
  7963. case MF_CLOUDS: map[m].flag.clouds=0; break;
  7964. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  7965. case MF_FOG: map[m].flag.fog=0; break;
  7966. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  7967. case MF_SAKURA: map[m].flag.sakura=0; break;
  7968. case MF_LEAVES: map[m].flag.leaves=0; break;
  7969. case MF_RAIN: map[m].flag.rain=0; break;
  7970. case MF_INDOORS: map[m].flag.indoors=0; break;
  7971. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  7972. case MF_NOGO: map[m].flag.nogo=0; break;
  7973. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  7974. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  7975. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  7976. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  7977. case MF_NORETURN: map[m].flag.noreturn=0; break;
  7978. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  7979. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  7980. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  7981. case MF_NOCOMMAND: map[m].nocommand=0; break;
  7982. case MF_JEXP: map[m].jexp=100; break;
  7983. case MF_BEXP: map[m].bexp=100; break;
  7984. case MF_NOVENDING: map[m].flag.novending=0; break;
  7985. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  7986. case MF_NOCHAT: map[m].flag.nochat=0; break;
  7987. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  7988. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  7989. }
  7990. }
  7991. return 0;
  7992. }
  7993. BUILDIN_FUNC(pvpon)
  7994. {
  7995. int m,i,users;
  7996. const char *str;
  7997. TBL_PC *pl_sd=NULL, **pl_allsd;
  7998. str=script_getstr(st,2);
  7999. m = map_mapname2mapid(str);
  8000. if(m >= 0 && !map[m].flag.pvp) {
  8001. map[m].flag.pvp = 1;
  8002. clif_send0199(m,1);
  8003. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8004. return 0;
  8005. pl_allsd = map_getallusers(&users);
  8006. for(i=0;i<users;i++)
  8007. {
  8008. if ((pl_sd = pl_allsd[i]) && m == pl_sd->bl.m && pl_sd->pvp_timer == -1)
  8009. {
  8010. pl_sd->pvp_timer=add_timer(gettick()+200,pc_calc_pvprank_timer,pl_sd->bl.id,0);
  8011. pl_sd->pvp_rank=0;
  8012. pl_sd->pvp_lastusers=0;
  8013. pl_sd->pvp_point=5;
  8014. pl_sd->pvp_won = 0;
  8015. pl_sd->pvp_lost = 0;
  8016. }
  8017. }
  8018. }
  8019. return 0;
  8020. }
  8021. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8022. {
  8023. TBL_PC* sd = (TBL_PC*)bl;
  8024. clif_pvpset(sd, 0, 0, 2);
  8025. if (sd->pvp_timer != -1) {
  8026. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8027. sd->pvp_timer = -1;
  8028. }
  8029. return 0;
  8030. }
  8031. BUILDIN_FUNC(pvpoff)
  8032. {
  8033. int m;
  8034. const char *str;
  8035. str=script_getstr(st,2);
  8036. m = map_mapname2mapid(str);
  8037. if(m < 0 || !map[m].flag.pvp)
  8038. return 0; //fixed Lupus
  8039. map[m].flag.pvp = 0;
  8040. clif_send0199(m,0);
  8041. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8042. return 0;
  8043. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8044. return 0;
  8045. }
  8046. BUILDIN_FUNC(gvgon)
  8047. {
  8048. int m;
  8049. const char *str;
  8050. str=script_getstr(st,2);
  8051. m = map_mapname2mapid(str);
  8052. if(m >= 0 && !map[m].flag.gvg) {
  8053. map[m].flag.gvg = 1;
  8054. clif_send0199(m,3);
  8055. }
  8056. return 0;
  8057. }
  8058. BUILDIN_FUNC(gvgoff)
  8059. {
  8060. int m;
  8061. const char *str;
  8062. str=script_getstr(st,2);
  8063. m = map_mapname2mapid(str);
  8064. if(m >= 0 && map[m].flag.gvg) {
  8065. map[m].flag.gvg = 0;
  8066. clif_send0199(m,0);
  8067. }
  8068. return 0;
  8069. }
  8070. /*==========================================
  8071. * Shows an emoticon on top of the player/npc
  8072. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8073. *------------------------------------------*/
  8074. //Optional second parameter added by [Skotlex]
  8075. BUILDIN_FUNC(emotion)
  8076. {
  8077. int type;
  8078. int player=0;
  8079. type=script_getnum(st,2);
  8080. if(type < 0 || type > 100)
  8081. return 0;
  8082. if( script_hasdata(st,3) )
  8083. player=script_getnum(st,3);
  8084. if (player) {
  8085. TBL_PC *sd = script_rid2sd(st);
  8086. if (sd)
  8087. clif_emotion(&sd->bl,type);
  8088. } else
  8089. clif_emotion(map_id2bl(st->oid),type);
  8090. return 0;
  8091. }
  8092. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  8093. {
  8094. int m=va_arg(ap,int);
  8095. int g_id=va_arg(ap,int);
  8096. int flag=va_arg(ap,int);
  8097. TBL_PC *sd = (TBL_PC*)data;
  8098. if(!sd || sd->bl.m != m)
  8099. return 0;
  8100. if(
  8101. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8102. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8103. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8104. )
  8105. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8106. return 1;
  8107. }
  8108. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8109. {
  8110. struct mob_data *md=(struct mob_data *)bl;
  8111. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8112. status_kill(bl);
  8113. return 0;
  8114. }
  8115. BUILDIN_FUNC(maprespawnguildid)
  8116. {
  8117. const char *mapname=script_getstr(st,2);
  8118. int g_id=script_getnum(st,3);
  8119. int flag=script_getnum(st,4);
  8120. int m=map_mapname2mapid(mapname);
  8121. if(m == -1)
  8122. return 0;
  8123. //Catch ALL players (in case some are 'between maps' on execution time)
  8124. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8125. if (flag&4) //Remove script mobs.
  8126. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8127. return 0;
  8128. }
  8129. BUILDIN_FUNC(agitstart)
  8130. {
  8131. if(agit_flag==1) return 0; // Agit already Start.
  8132. agit_flag=1;
  8133. guild_agit_start();
  8134. return 0;
  8135. }
  8136. BUILDIN_FUNC(agitend)
  8137. {
  8138. if(agit_flag==0) return 0; // Agit already End.
  8139. agit_flag=0;
  8140. guild_agit_end();
  8141. return 0;
  8142. }
  8143. /*==========================================
  8144. * Returns whether woe is on or off. // choice script
  8145. *------------------------------------------*/
  8146. BUILDIN_FUNC(agitcheck)
  8147. {
  8148. script_pushint(st,agit_flag);
  8149. return 0;
  8150. }
  8151. BUILDIN_FUNC(flagemblem)
  8152. {
  8153. int g_id=script_getnum(st,2);
  8154. if(g_id < 0) return 0;
  8155. // ShowMessage("Script.c: [FlagEmblem] GuildID=%d, Emblem=%d.\n", g->guild_id, g->emblem_id);
  8156. ((struct npc_data *)map_id2bl(st->oid))->u.scr.guild_id = g_id;
  8157. return 0;
  8158. }
  8159. BUILDIN_FUNC(getcastlename)
  8160. {
  8161. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8162. struct guild_castle* gc = guild_mapname2gc(mapname);
  8163. char* name = (gc) ? gc->castle_name : "";
  8164. script_pushstrcopy(st,name);
  8165. return 0;
  8166. }
  8167. BUILDIN_FUNC(getcastledata)
  8168. {
  8169. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8170. int index = script_getnum(st,3);
  8171. struct guild_castle* gc = guild_mapname2gc(mapname);
  8172. if(script_hasdata(st,4) && index==0 && gc) {
  8173. const char* event = script_getstr(st,4);
  8174. check_event(st, event);
  8175. guild_addcastleinfoevent(gc->castle_id,17,event);
  8176. }
  8177. if(gc){
  8178. switch(index){
  8179. case 0: {
  8180. int i;
  8181. for(i=1;i<18;i++) // Initialize[AgitInit]
  8182. guild_castledataload(gc->castle_id,i);
  8183. } break;
  8184. case 1:
  8185. script_pushint(st,gc->guild_id); break;
  8186. case 2:
  8187. script_pushint(st,gc->economy); break;
  8188. case 3:
  8189. script_pushint(st,gc->defense); break;
  8190. case 4:
  8191. script_pushint(st,gc->triggerE); break;
  8192. case 5:
  8193. script_pushint(st,gc->triggerD); break;
  8194. case 6:
  8195. script_pushint(st,gc->nextTime); break;
  8196. case 7:
  8197. script_pushint(st,gc->payTime); break;
  8198. case 8:
  8199. script_pushint(st,gc->createTime); break;
  8200. case 9:
  8201. script_pushint(st,gc->visibleC); break;
  8202. case 10:
  8203. case 11:
  8204. case 12:
  8205. case 13:
  8206. case 14:
  8207. case 15:
  8208. case 16:
  8209. case 17:
  8210. script_pushint(st,gc->guardian[index-10].visible); break;
  8211. default:
  8212. script_pushint(st,0); break;
  8213. }
  8214. return 0;
  8215. }
  8216. script_pushint(st,0);
  8217. return 0;
  8218. }
  8219. BUILDIN_FUNC(setcastledata)
  8220. {
  8221. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8222. int index = script_getnum(st,3);
  8223. int value = script_getnum(st,4);
  8224. struct guild_castle* gc = guild_mapname2gc(mapname);
  8225. if(gc) {
  8226. // Save Data byself First
  8227. switch(index){
  8228. case 1:
  8229. gc->guild_id = value; break;
  8230. case 2:
  8231. gc->economy = value; break;
  8232. case 3:
  8233. gc->defense = value; break;
  8234. case 4:
  8235. gc->triggerE = value; break;
  8236. case 5:
  8237. gc->triggerD = value; break;
  8238. case 6:
  8239. gc->nextTime = value; break;
  8240. case 7:
  8241. gc->payTime = value; break;
  8242. case 8:
  8243. gc->createTime = value; break;
  8244. case 9:
  8245. gc->visibleC = value; break;
  8246. case 10:
  8247. case 11:
  8248. case 12:
  8249. case 13:
  8250. case 14:
  8251. case 15:
  8252. case 16:
  8253. case 17:
  8254. gc->guardian[index-10].visible = value; break;
  8255. default:
  8256. return 0;
  8257. }
  8258. guild_castledatasave(gc->castle_id,index,value);
  8259. }
  8260. return 0;
  8261. }
  8262. /* =====================================================================
  8263. * ギルド情報を要求する
  8264. * ---------------------------------------------------------------------*/
  8265. BUILDIN_FUNC(requestguildinfo)
  8266. {
  8267. int guild_id=script_getnum(st,2);
  8268. const char *event=NULL;
  8269. if( script_hasdata(st,3) ){
  8270. event=script_getstr(st,3);
  8271. check_event(st, event);
  8272. }
  8273. if(guild_id>0)
  8274. guild_npc_request_info(guild_id,event);
  8275. return 0;
  8276. }
  8277. /* =====================================================================
  8278. * カードの数を得る
  8279. * ---------------------------------------------------------------------*/
  8280. BUILDIN_FUNC(getequipcardcnt)
  8281. {
  8282. int i=-1,num;
  8283. TBL_PC *sd;
  8284. int c=MAX_SLOTS;
  8285. num=script_getnum(st,2);
  8286. sd=script_rid2sd(st);
  8287. if (num > 0 && num <= ARRAYLENGTH(equip))
  8288. i=pc_checkequip(sd,equip[num-1]);
  8289. if (i < 0) {
  8290. script_pushint(st,0);
  8291. return 0;
  8292. }
  8293. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8294. {
  8295. script_pushint(st,0);
  8296. return 0;
  8297. }
  8298. do{
  8299. if(sd->status.inventory[i].card[c-1] &&
  8300. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8301. script_pushint(st,(c));
  8302. return 0;
  8303. }
  8304. }while(c--);
  8305. script_pushint(st,0);
  8306. return 0;
  8307. }
  8308. /* ================================================================
  8309. * カード取り外し成功
  8310. * ----------------------------------------------------------------*/
  8311. BUILDIN_FUNC(successremovecards)
  8312. {
  8313. int i=-1,j,num,cardflag=0,flag;
  8314. TBL_PC *sd;
  8315. struct item item_tmp;
  8316. int c=MAX_SLOTS;
  8317. num=script_getnum(st,2);
  8318. sd=script_rid2sd(st);
  8319. if (num > 0 && num <= ARRAYLENGTH(equip))
  8320. i=pc_checkequip(sd,equip[num-1]);
  8321. if (i < 0) {
  8322. script_pushint(st,0);
  8323. return 0;
  8324. }
  8325. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8326. return 0;
  8327. do{
  8328. if(sd->status.inventory[i].card[c-1] &&
  8329. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8330. cardflag = 1;
  8331. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8332. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8333. item_tmp.attribute=0;
  8334. for (j = 0; j < MAX_SLOTS; j++)
  8335. item_tmp.card[j]=0;
  8336. //Logs items, got from (N)PC scripts [Lupus]
  8337. if(log_config.enable_logs&0x40)
  8338. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8339. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8340. clif_additem(sd,0,0,flag);
  8341. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8342. }
  8343. }
  8344. }while(c--);
  8345. if(cardflag == 1){ // カードを取り除いたアイテム所得
  8346. flag=0;
  8347. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8348. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8349. item_tmp.attribute=sd->status.inventory[i].attribute;
  8350. //Logs items, got from (N)PC scripts [Lupus]
  8351. if(log_config.enable_logs&0x40)
  8352. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8353. for (j = 0; j < MAX_SLOTS; j++)
  8354. item_tmp.card[j]=0;
  8355. pc_delitem(sd,i,1,0);
  8356. //Logs items, got from (N)PC scripts [Lupus]
  8357. if(log_config.enable_logs&0x40)
  8358. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8359. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8360. clif_additem(sd,0,0,flag);
  8361. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8362. }
  8363. clif_misceffect(&sd->bl,3);
  8364. return 0;
  8365. }
  8366. return 0;
  8367. }
  8368. /* ================================================================
  8369. * カード取り外し失敗 slot,type
  8370. * type=0: 両方損失、1:カード損失、2:武具損失、3:損失無し
  8371. * ----------------------------------------------------------------*/
  8372. BUILDIN_FUNC(failedremovecards)
  8373. {
  8374. int i=-1,j,num,cardflag=0,flag,typefail;
  8375. TBL_PC *sd;
  8376. struct item item_tmp;
  8377. int c=MAX_SLOTS;
  8378. num=script_getnum(st,2);
  8379. typefail=script_getnum(st,3);
  8380. sd=script_rid2sd(st);
  8381. if (num > 0 && num <= ARRAYLENGTH(equip))
  8382. i=pc_checkequip(sd,equip[num-1]);
  8383. if (i < 0) {
  8384. script_pushint(st,0);
  8385. return 0;
  8386. }
  8387. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8388. return 0;
  8389. do{
  8390. if(sd->status.inventory[i].card[c-1] &&
  8391. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8392. cardflag = 1;
  8393. if(typefail == 2){ // 武具のみ損失なら、カードは受け取らせる
  8394. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8395. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8396. item_tmp.attribute=0;
  8397. for (j = 0; j < MAX_SLOTS; j++)
  8398. item_tmp.card[j]=0;
  8399. //Logs items, got from (N)PC scripts [Lupus]
  8400. if(log_config.enable_logs&0x40)
  8401. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8402. if((flag=pc_additem(sd,&item_tmp,1))){
  8403. clif_additem(sd,0,0,flag);
  8404. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8405. }
  8406. }
  8407. }
  8408. }while(c--);
  8409. if(cardflag == 1){
  8410. if(typefail == 0 || typefail == 2){ // 武具損失
  8411. //Logs items, got from (N)PC scripts [Lupus]
  8412. if(log_config.enable_logs&0x40)
  8413. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8414. pc_delitem(sd,i,1,0);
  8415. clif_misceffect(&sd->bl,2);
  8416. return 0;
  8417. }
  8418. if(typefail == 1){ // カードのみ損失(武具を返す)
  8419. flag=0;
  8420. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8421. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8422. item_tmp.attribute=sd->status.inventory[i].attribute;
  8423. //Logs items, got from (N)PC scripts [Lupus]
  8424. if(log_config.enable_logs&0x40)
  8425. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8426. for (j = 0; j < MAX_SLOTS; j++)
  8427. item_tmp.card[j]=0;
  8428. pc_delitem(sd,i,1,0);
  8429. //Logs items, got from (N)PC scripts [Lupus]
  8430. if(log_config.enable_logs&0x40)
  8431. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8432. if((flag=pc_additem(sd,&item_tmp,1))){
  8433. clif_additem(sd,0,0,flag);
  8434. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8435. }
  8436. }
  8437. clif_misceffect(&sd->bl,2);
  8438. return 0;
  8439. }
  8440. return 0;
  8441. }
  8442. /* ================================================================
  8443. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8444. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8445. * improved by [Lance]
  8446. * ================================================================*/
  8447. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8448. {
  8449. int x,y,m,check_val=0,check_ID=0,i=0;
  8450. struct guild *g = NULL;
  8451. struct party_data *p = NULL;
  8452. const char *str;
  8453. const char *mapname;
  8454. unsigned int index;
  8455. mapname=script_getstr(st,2);
  8456. str=script_getstr(st,3);
  8457. x=script_getnum(st,4);
  8458. y=script_getnum(st,5);
  8459. if(script_hasdata(st,7)){
  8460. check_val=script_getnum(st,6);
  8461. check_ID=script_getnum(st,7);
  8462. }
  8463. if((m=map_mapname2mapid(mapname))< 0)
  8464. return 0;
  8465. if(!(index=mapindex_name2id(str)))
  8466. return 0;
  8467. switch(check_val){
  8468. case 1:
  8469. g = guild_search(check_ID);
  8470. if (g){
  8471. for( i=0; i < g->max_member; i++)
  8472. {
  8473. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8474. pc_setpos(g->member[i].sd,index,x,y,3);
  8475. }
  8476. }
  8477. }
  8478. break;
  8479. case 2:
  8480. p = party_search(check_ID);
  8481. if(p){
  8482. for(i=0;i<MAX_PARTY; i++){
  8483. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8484. pc_setpos(p->data[i].sd,index,x,y,3);
  8485. }
  8486. }
  8487. }
  8488. break;
  8489. default:
  8490. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8491. break;
  8492. }
  8493. return 0;
  8494. }
  8495. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8496. {
  8497. char *event=va_arg(ap,char *);
  8498. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8499. return 1;
  8500. return 0;
  8501. }
  8502. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8503. {
  8504. const char *mapname,*event;
  8505. int m;
  8506. mapname=script_getstr(st,2);
  8507. event=script_getstr(st,3);
  8508. check_event(st, event);
  8509. if( (m=map_mapname2mapid(mapname))<0 ) {
  8510. script_pushint(st,-1);
  8511. return 0;
  8512. }
  8513. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8514. return 0;
  8515. }
  8516. BUILDIN_FUNC(marriage)
  8517. {
  8518. const char *partner=script_getstr(st,2);
  8519. TBL_PC *sd=script_rid2sd(st);
  8520. TBL_PC *p_sd=map_nick2sd(partner);
  8521. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8522. script_pushint(st,0);
  8523. return 0;
  8524. }
  8525. script_pushint(st,1);
  8526. return 0;
  8527. }
  8528. BUILDIN_FUNC(wedding_effect)
  8529. {
  8530. TBL_PC *sd=script_rid2sd(st);
  8531. struct block_list *bl;
  8532. if(sd==NULL) {
  8533. bl=map_id2bl(st->oid);
  8534. } else
  8535. bl=&sd->bl;
  8536. clif_wedding_effect(bl);
  8537. return 0;
  8538. }
  8539. BUILDIN_FUNC(divorce)
  8540. {
  8541. TBL_PC *sd=script_rid2sd(st);
  8542. if(sd==NULL || pc_divorce(sd) < 0){
  8543. script_pushint(st,0);
  8544. return 0;
  8545. }
  8546. script_pushint(st,1);
  8547. return 0;
  8548. }
  8549. BUILDIN_FUNC(ispartneron)
  8550. {
  8551. TBL_PC *sd=script_rid2sd(st);
  8552. TBL_PC *p_sd=NULL;
  8553. if(sd==NULL || !pc_ismarried(sd) ||
  8554. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8555. script_pushint(st,0);
  8556. return 0;
  8557. }
  8558. script_pushint(st,1);
  8559. return 0;
  8560. }
  8561. BUILDIN_FUNC(getpartnerid)
  8562. {
  8563. TBL_PC *sd=script_rid2sd(st);
  8564. if (sd == NULL) {
  8565. script_pushint(st,0);
  8566. return 0;
  8567. }
  8568. script_pushint(st,sd->status.partner_id);
  8569. return 0;
  8570. }
  8571. BUILDIN_FUNC(getchildid)
  8572. {
  8573. TBL_PC *sd=script_rid2sd(st);
  8574. if (sd == NULL) {
  8575. script_pushint(st,0);
  8576. return 0;
  8577. }
  8578. script_pushint(st,sd->status.child);
  8579. return 0;
  8580. }
  8581. BUILDIN_FUNC(getmotherid)
  8582. {
  8583. TBL_PC *sd=script_rid2sd(st);
  8584. if (sd == NULL) {
  8585. script_pushint(st,0);
  8586. return 0;
  8587. }
  8588. script_pushint(st,sd->status.mother);
  8589. return 0;
  8590. }
  8591. BUILDIN_FUNC(getfatherid)
  8592. {
  8593. TBL_PC *sd=script_rid2sd(st);
  8594. if (sd == NULL) {
  8595. script_pushint(st,0);
  8596. return 0;
  8597. }
  8598. script_pushint(st,sd->status.father);
  8599. return 0;
  8600. }
  8601. BUILDIN_FUNC(warppartner)
  8602. {
  8603. int x,y;
  8604. unsigned short mapindex;
  8605. const char *str;
  8606. TBL_PC *sd=script_rid2sd(st);
  8607. TBL_PC *p_sd=NULL;
  8608. if(sd==NULL || !pc_ismarried(sd) ||
  8609. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8610. script_pushint(st,0);
  8611. return 0;
  8612. }
  8613. str=script_getstr(st,2);
  8614. x=script_getnum(st,3);
  8615. y=script_getnum(st,4);
  8616. mapindex = mapindex_name2id(str);
  8617. if (mapindex) {
  8618. pc_setpos(p_sd,mapindex,x,y,0);
  8619. script_pushint(st,1);
  8620. } else
  8621. script_pushint(st,0);
  8622. return 0;
  8623. }
  8624. /*================================================
  8625. * Script for Displaying MOB Information [Valaris]
  8626. *------------------------------------------------*/
  8627. BUILDIN_FUNC(strmobinfo)
  8628. {
  8629. int num=script_getnum(st,2);
  8630. int class_=script_getnum(st,3);
  8631. if(!mobdb_checkid(class_))
  8632. {
  8633. script_pushint(st,0);
  8634. return 0;
  8635. }
  8636. switch (num) {
  8637. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  8638. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  8639. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8640. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8641. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8642. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8643. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8644. default:
  8645. script_pushint(st,0);
  8646. break;
  8647. }
  8648. return 0;
  8649. }
  8650. /*==========================================
  8651. * Summon guardians [Valaris]
  8652. * guardian "<map name>",<x>,<y>,"<name to show>",<mob id>,{,"<event label>"}{,<guardian index>};
  8653. *------------------------------------------*/
  8654. BUILDIN_FUNC(guardian)
  8655. {
  8656. int class_=0,x=0,y=0,guardian=0;
  8657. const char *str,*map,*evt="";
  8658. struct script_data *data;
  8659. map =script_getstr(st,2);
  8660. x =script_getnum(st,3);
  8661. y =script_getnum(st,4);
  8662. str =script_getstr(st,5);
  8663. class_=script_getnum(st,6);
  8664. if( script_hasdata(st,8) )
  8665. {// "<event label>",<guardian index>
  8666. evt=script_getstr(st,7);
  8667. guardian=script_getnum(st,8);
  8668. } else if( script_hasdata(st,7) ){
  8669. data=script_getdata(st,7);
  8670. get_val(st,data);
  8671. if( data_isstring(data) )
  8672. {// "<event label>"
  8673. evt=script_getstr(st,7);
  8674. } else if( data_isint(data) )
  8675. {// <guardian index>
  8676. guardian=script_getnum(st,7);
  8677. } else {
  8678. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8679. script_reportdata(data);
  8680. return 1;
  8681. }
  8682. }
  8683. check_event(st, evt);
  8684. mob_spawn_guardian(map,x,y,str,class_,evt,guardian);
  8685. return 0;
  8686. }
  8687. /// Retrieves various information about the specified guardian.
  8688. ///
  8689. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  8690. /// type: 0 - whether it is deployed or not
  8691. /// 1 - maximum hp
  8692. /// 2 - current hp
  8693. ///
  8694. BUILDIN_FUNC(guardianinfo)
  8695. {
  8696. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8697. int id = script_getnum(st,3);
  8698. int type = script_getnum(st,4);
  8699. struct guild_castle* gc = guild_mapname2gc(mapname);
  8700. struct mob_data* gd;
  8701. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  8702. {
  8703. script_pushint(st,-1);
  8704. return 0;
  8705. }
  8706. if( type == 0 )
  8707. script_pushint(st, gc->guardian[id].visible);
  8708. else
  8709. if( !gc->guardian[id].visible )
  8710. script_pushint(st,-1);
  8711. else
  8712. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  8713. script_pushint(st,-1);
  8714. else
  8715. {
  8716. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  8717. else if( type == 2 ) script_pushint(st,gd->status.hp);
  8718. else
  8719. script_pushint(st,-1);
  8720. }
  8721. return 0;
  8722. }
  8723. /*==========================================
  8724. * IDからItem名
  8725. *------------------------------------------*/
  8726. BUILDIN_FUNC(getitemname)
  8727. {
  8728. int item_id=0;
  8729. struct item_data *i_data;
  8730. char *item_name;
  8731. struct script_data *data;
  8732. data=script_getdata(st,2);
  8733. get_val(st,data);
  8734. if( data_isstring(data) ){
  8735. const char *name=conv_str(st,data);
  8736. struct item_data *item_data = itemdb_searchname(name);
  8737. if( item_data )
  8738. item_id=item_data->nameid;
  8739. }else
  8740. item_id=conv_num(st,data);
  8741. i_data = itemdb_exists(item_id);
  8742. if (i_data == NULL)
  8743. {
  8744. script_pushconststr(st,"null");
  8745. return 0;
  8746. }
  8747. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8748. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8749. script_pushstr(st,item_name);
  8750. return 0;
  8751. }
  8752. /*==========================================
  8753. * Returns number of slots an item has. [Skotlex]
  8754. *------------------------------------------*/
  8755. BUILDIN_FUNC(getitemslots)
  8756. {
  8757. int item_id;
  8758. struct item_data *i_data;
  8759. item_id=script_getnum(st,2);
  8760. i_data = itemdb_exists(item_id);
  8761. if (i_data)
  8762. script_pushint(st,i_data->slot);
  8763. else
  8764. script_pushint(st,-1);
  8765. return 0;
  8766. }
  8767. /*==========================================
  8768. * Returns some values of an item [Lupus]
  8769. * Price, Weight, etc...
  8770. getiteminfo(itemID,n), where n
  8771. 0 value_buy;
  8772. 1 value_sell;
  8773. 2 type;
  8774. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8775. if = 0, then monsters don't drop it at all (rare or a quest item)
  8776. if = 10000, then this item is sold in NPC shops only
  8777. 4 sex;
  8778. 5 equip;
  8779. 6 weight;
  8780. 7 atk;
  8781. 8 def;
  8782. 9 range;
  8783. 10 slot;
  8784. 11 look;
  8785. 12 elv;
  8786. 13 wlv;
  8787. 14 view id
  8788. *------------------------------------------*/
  8789. BUILDIN_FUNC(getiteminfo)
  8790. {
  8791. int item_id,n;
  8792. int *item_arr;
  8793. struct item_data *i_data;
  8794. item_id = script_getnum(st,2);
  8795. n = script_getnum(st,3);
  8796. i_data = itemdb_exists(item_id);
  8797. if (i_data && n>=0 && n<14) {
  8798. item_arr = (int*)&i_data->value_buy;
  8799. script_pushint(st,item_arr[n]);
  8800. } else
  8801. script_pushint(st,-1);
  8802. return 0;
  8803. }
  8804. /*==========================================
  8805. * Set some values of an item [Lupus]
  8806. * Price, Weight, etc...
  8807. setiteminfo(itemID,n,Value), where n
  8808. 0 value_buy;
  8809. 1 value_sell;
  8810. 2 type;
  8811. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8812. if = 0, then monsters don't drop it at all (rare or a quest item)
  8813. if = 10000, then this item is sold in NPC shops only
  8814. 4 sex;
  8815. 5 equip;
  8816. 6 weight;
  8817. 7 atk;
  8818. 8 def;
  8819. 9 range;
  8820. 10 slot;
  8821. 11 look;
  8822. 12 elv;
  8823. 13 wlv;
  8824. 14 view id
  8825. * Returns Value or -1 if the wrong field's been set
  8826. *------------------------------------------*/
  8827. BUILDIN_FUNC(setiteminfo)
  8828. {
  8829. int item_id,n,value;
  8830. int *item_arr;
  8831. struct item_data *i_data;
  8832. item_id = script_getnum(st,2);
  8833. n = script_getnum(st,3);
  8834. value = script_getnum(st,4);
  8835. i_data = itemdb_exists(item_id);
  8836. if (i_data && n>=0 && n<14) {
  8837. item_arr = (int*)&i_data->value_buy;
  8838. item_arr[n] = value;
  8839. script_pushint(st,value);
  8840. } else
  8841. script_pushint(st,-1);
  8842. return 0;
  8843. }
  8844. /*==========================================
  8845. * Returns value from equipped item slot n [Lupus]
  8846. getequipcardid(num,slot)
  8847. where
  8848. num = eqip position slot
  8849. slot = 0,1,2,3 (Card Slot N)
  8850. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  8851. it's useful when you want to check item cards or if it's signed
  8852. Useful for such quests as "Sign this refined item with players name" etc
  8853. Hat[0] +4 -> Player's Hat[0] +4
  8854. *------------------------------------------*/
  8855. BUILDIN_FUNC(getequipcardid)
  8856. {
  8857. int i=-1,num,slot;
  8858. TBL_PC *sd;
  8859. num=script_getnum(st,2);
  8860. slot=script_getnum(st,3);
  8861. sd=script_rid2sd(st);
  8862. if (num > 0 && num <= ARRAYLENGTH(equip))
  8863. i=pc_checkequip(sd,equip[num-1]);
  8864. if(i >= 0 && slot>=0 && slot<4)
  8865. script_pushint(st,sd->status.inventory[i].card[slot]);
  8866. else
  8867. script_pushint(st,0);
  8868. return 0;
  8869. }
  8870. /*==========================================
  8871. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  8872. *------------------------------------------*/
  8873. BUILDIN_FUNC(petskillbonus)
  8874. {
  8875. struct pet_data *pd;
  8876. TBL_PC *sd=script_rid2sd(st);
  8877. if(sd==NULL || sd->pd==NULL)
  8878. return 0;
  8879. pd=sd->pd;
  8880. if (pd->bonus)
  8881. { //Clear previous bonus
  8882. if (pd->bonus->timer != -1)
  8883. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  8884. } else //init
  8885. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  8886. pd->bonus->type=script_getnum(st,2);
  8887. pd->bonus->val=script_getnum(st,3);
  8888. pd->bonus->duration=script_getnum(st,4);
  8889. pd->bonus->delay=script_getnum(st,5);
  8890. if (pd->state.skillbonus == 1)
  8891. pd->state.skillbonus=0; // waiting state
  8892. // wait for timer to start
  8893. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  8894. pd->bonus->timer=-1;
  8895. else
  8896. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  8897. return 0;
  8898. }
  8899. /*==========================================
  8900. * pet looting [Valaris] //Rewritten by [Skotlex]
  8901. *------------------------------------------*/
  8902. BUILDIN_FUNC(petloot)
  8903. {
  8904. int max;
  8905. struct pet_data *pd;
  8906. TBL_PC *sd=script_rid2sd(st);
  8907. if(sd==NULL || sd->pd==NULL)
  8908. return 0;
  8909. max=script_getnum(st,2);
  8910. if(max < 1)
  8911. max = 1; //Let'em loot at least 1 item.
  8912. else if (max > MAX_PETLOOT_SIZE)
  8913. max = MAX_PETLOOT_SIZE;
  8914. pd = sd->pd;
  8915. if (pd->loot != NULL)
  8916. { //Release whatever was there already and reallocate memory
  8917. pet_lootitem_drop(pd, pd->msd);
  8918. aFree(pd->loot->item);
  8919. }
  8920. else
  8921. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  8922. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  8923. pd->loot->max=max;
  8924. pd->loot->count = 0;
  8925. pd->loot->weight = 0;
  8926. return 0;
  8927. }
  8928. /*==========================================
  8929. * PCの所持品情報読み取り
  8930. *------------------------------------------*/
  8931. BUILDIN_FUNC(getinventorylist)
  8932. {
  8933. TBL_PC *sd=script_rid2sd(st);
  8934. char card_var[NAME_LENGTH];
  8935. int i,j=0,k;
  8936. if(!sd) return 0;
  8937. for(i=0;i<MAX_INVENTORY;i++){
  8938. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  8939. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  8940. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  8941. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  8942. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  8943. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  8944. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  8945. for (k = 0; k < MAX_SLOTS; k++)
  8946. {
  8947. sprintf(card_var, "@inventorylist_card%d",k+1);
  8948. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  8949. }
  8950. j++;
  8951. }
  8952. }
  8953. pc_setreg(sd,add_str("@inventorylist_count"),j);
  8954. return 0;
  8955. }
  8956. BUILDIN_FUNC(getskilllist)
  8957. {
  8958. TBL_PC *sd=script_rid2sd(st);
  8959. int i,j=0;
  8960. if(!sd) return 0;
  8961. for(i=0;i<MAX_SKILL;i++){
  8962. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  8963. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  8964. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  8965. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  8966. j++;
  8967. }
  8968. }
  8969. pc_setreg(sd,add_str("@skilllist_count"),j);
  8970. return 0;
  8971. }
  8972. BUILDIN_FUNC(clearitem)
  8973. {
  8974. TBL_PC *sd=script_rid2sd(st);
  8975. int i;
  8976. if(sd==NULL) return 0;
  8977. for (i=0; i<MAX_INVENTORY; i++) {
  8978. if (sd->status.inventory[i].amount) {
  8979. //Logs items, got from (N)PC scripts [Lupus]
  8980. if(log_config.enable_logs&0x40)
  8981. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  8982. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  8983. }
  8984. }
  8985. return 0;
  8986. }
  8987. /*==========================================
  8988. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  8989. *------------------------------------------*/
  8990. BUILDIN_FUNC(disguise)
  8991. {
  8992. int id;
  8993. TBL_PC* sd = script_rid2sd(st);
  8994. if (sd == NULL) return 0;
  8995. id = script_getnum(st,2);
  8996. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  8997. pc_disguise(sd, id);
  8998. script_pushint(st,id);
  8999. } else
  9000. script_pushint(st,0);
  9001. return 0;
  9002. }
  9003. /*==========================================
  9004. * Undisguise Player (returns 1 if success, 0 on fail)
  9005. *------------------------------------------*/
  9006. BUILDIN_FUNC(undisguise)
  9007. {
  9008. TBL_PC* sd = script_rid2sd(st);
  9009. if (sd == NULL) return 0;
  9010. if (sd->disguise) {
  9011. pc_disguise(sd, 0);
  9012. script_pushint(st,0);
  9013. } else {
  9014. script_pushint(st,1);
  9015. }
  9016. return 0;
  9017. }
  9018. /*==========================================
  9019. * NPCクラスチェンジ
  9020. * classは変わりたいclass
  9021. * typeは通常0なのかな?
  9022. *------------------------------------------*/
  9023. BUILDIN_FUNC(classchange)
  9024. {
  9025. int _class,type;
  9026. struct block_list *bl=map_id2bl(st->oid);
  9027. if(bl==NULL) return 0;
  9028. _class=script_getnum(st,2);
  9029. type=script_getnum(st,3);
  9030. clif_class_change(bl,_class,type);
  9031. return 0;
  9032. }
  9033. /*==========================================
  9034. * NPCから発生するエフェクト
  9035. *------------------------------------------*/
  9036. BUILDIN_FUNC(misceffect)
  9037. {
  9038. int type;
  9039. type=script_getnum(st,2);
  9040. if(st->oid && st->oid != fake_nd->bl.id) {
  9041. struct block_list *bl = map_id2bl(st->oid);
  9042. if (bl)
  9043. clif_misceffect2(bl,type);
  9044. } else{
  9045. TBL_PC *sd=script_rid2sd(st);
  9046. if(sd)
  9047. clif_misceffect2(&sd->bl,type);
  9048. }
  9049. return 0;
  9050. }
  9051. /*==========================================
  9052. * サウンドエフェクト
  9053. *------------------------------------------*/
  9054. BUILDIN_FUNC(soundeffect)
  9055. {
  9056. TBL_PC* sd = script_rid2sd(st);
  9057. const char* name = script_getstr(st,2);
  9058. int type = script_getnum(st,3);
  9059. if(sd)
  9060. {
  9061. if(!st->rid)
  9062. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9063. else
  9064. clif_soundeffect(sd,&sd->bl,name,type);
  9065. }
  9066. return 0;
  9067. }
  9068. int soundeffect_sub(struct block_list* bl,va_list ap)
  9069. {
  9070. char* name = va_arg(ap,char*);
  9071. int type = va_arg(ap,int);
  9072. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9073. return 0;
  9074. }
  9075. /*==========================================
  9076. * Play a sound effect (.wav) on multiple clients
  9077. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9078. *------------------------------------------*/
  9079. BUILDIN_FUNC(soundeffectall)
  9080. {
  9081. struct block_list* bl;
  9082. const char* name;
  9083. int type;
  9084. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9085. if (!bl)
  9086. return 0;
  9087. name = script_getstr(st,2);
  9088. type = script_getnum(st,3);
  9089. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9090. if(!script_hasdata(st,4))
  9091. { // area around
  9092. clif_soundeffectall(bl, name, type, AREA);
  9093. }
  9094. else
  9095. if(!script_hasdata(st,5))
  9096. { // entire map
  9097. const char* map = script_getstr(st,4);
  9098. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9099. }
  9100. else
  9101. if(script_hasdata(st,9))
  9102. { // specified part of map
  9103. const char* map = script_getstr(st,5);
  9104. int x0 = script_getnum(st,6);
  9105. int y0 = script_getnum(st,7);
  9106. int x1 = script_getnum(st,8);
  9107. int y1 = script_getnum(st,9);
  9108. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9109. }
  9110. else
  9111. {
  9112. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9113. }
  9114. return 0;
  9115. }
  9116. /*==========================================
  9117. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9118. *------------------------------------------*/
  9119. BUILDIN_FUNC(petrecovery)
  9120. {
  9121. struct pet_data *pd;
  9122. TBL_PC *sd=script_rid2sd(st);
  9123. if(sd==NULL || sd->pd==NULL)
  9124. return 0;
  9125. pd=sd->pd;
  9126. if (pd->recovery)
  9127. { //Halt previous bonus
  9128. if (pd->recovery->timer != -1)
  9129. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9130. } else //Init
  9131. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9132. pd->recovery->type=script_getnum(st,2);
  9133. pd->recovery->delay=script_getnum(st,3);
  9134. pd->recovery->timer=-1;
  9135. return 0;
  9136. }
  9137. /*==========================================
  9138. * pet healing [Valaris] //Rewritten by [Skotlex]
  9139. *------------------------------------------*/
  9140. BUILDIN_FUNC(petheal)
  9141. {
  9142. struct pet_data *pd;
  9143. TBL_PC *sd=script_rid2sd(st);
  9144. if(sd==NULL || sd->pd==NULL)
  9145. return 0;
  9146. pd=sd->pd;
  9147. if (pd->s_skill)
  9148. { //Clear previous skill
  9149. if (pd->s_skill->timer != -1)
  9150. {
  9151. if (pd->s_skill->id)
  9152. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9153. else
  9154. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9155. }
  9156. } else //init memory
  9157. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9158. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9159. //Use the lv as the amount to heal
  9160. pd->s_skill->lv=script_getnum(st,2);
  9161. pd->s_skill->delay=script_getnum(st,3);
  9162. pd->s_skill->hp=script_getnum(st,4);
  9163. pd->s_skill->sp=script_getnum(st,5);
  9164. //Use delay as initial offset to avoid skill/heal exploits
  9165. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9166. pd->s_skill->timer=-1;
  9167. else
  9168. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9169. return 0;
  9170. }
  9171. /*==========================================
  9172. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9173. *------------------------------------------*/
  9174. BUILDIN_FUNC(petskillattack)
  9175. {
  9176. struct pet_data *pd;
  9177. TBL_PC *sd=script_rid2sd(st);
  9178. if(sd==NULL || sd->pd==NULL)
  9179. return 0;
  9180. pd=sd->pd;
  9181. if (pd->a_skill == NULL)
  9182. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9183. pd->a_skill->id=script_getnum(st,2);
  9184. pd->a_skill->lv=script_getnum(st,3);
  9185. pd->a_skill->div_ = 0;
  9186. pd->a_skill->rate=script_getnum(st,4);
  9187. pd->a_skill->bonusrate=script_getnum(st,5);
  9188. return 0;
  9189. }
  9190. /*==========================================
  9191. * pet attack skills [Valaris]
  9192. *------------------------------------------*/
  9193. BUILDIN_FUNC(petskillattack2)
  9194. {
  9195. struct pet_data *pd;
  9196. TBL_PC *sd=script_rid2sd(st);
  9197. if(sd==NULL || sd->pd==NULL)
  9198. return 0;
  9199. pd=sd->pd;
  9200. if (pd->a_skill == NULL)
  9201. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9202. pd->a_skill->id=script_getnum(st,2);
  9203. pd->a_skill->lv=script_getnum(st,3);
  9204. pd->a_skill->div_ = script_getnum(st,4);
  9205. pd->a_skill->rate=script_getnum(st,5);
  9206. pd->a_skill->bonusrate=script_getnum(st,6);
  9207. return 0;
  9208. }
  9209. /*==========================================
  9210. * pet support skills [Skotlex]
  9211. *------------------------------------------*/
  9212. BUILDIN_FUNC(petskillsupport)
  9213. {
  9214. struct pet_data *pd;
  9215. TBL_PC *sd=script_rid2sd(st);
  9216. if(sd==NULL || sd->pd==NULL)
  9217. return 0;
  9218. pd=sd->pd;
  9219. if (pd->s_skill)
  9220. { //Clear previous skill
  9221. if (pd->s_skill->timer != -1)
  9222. {
  9223. if (pd->s_skill->id)
  9224. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9225. else
  9226. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9227. }
  9228. } else //init memory
  9229. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9230. pd->s_skill->id=script_getnum(st,2);
  9231. pd->s_skill->lv=script_getnum(st,3);
  9232. pd->s_skill->delay=script_getnum(st,4);
  9233. pd->s_skill->hp=script_getnum(st,5);
  9234. pd->s_skill->sp=script_getnum(st,6);
  9235. //Use delay as initial offset to avoid skill/heal exploits
  9236. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9237. pd->s_skill->timer=-1;
  9238. else
  9239. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9240. return 0;
  9241. }
  9242. /*==========================================
  9243. * Scripted skill effects [Celest]
  9244. *------------------------------------------*/
  9245. BUILDIN_FUNC(skilleffect)
  9246. {
  9247. TBL_PC *sd;
  9248. int skillid=script_getnum(st,2);
  9249. int skilllv=script_getnum(st,3);
  9250. sd=script_rid2sd(st);
  9251. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9252. return 0;
  9253. }
  9254. /*==========================================
  9255. * NPC skill effects [Valaris]
  9256. *------------------------------------------*/
  9257. BUILDIN_FUNC(npcskilleffect)
  9258. {
  9259. struct block_list *bl= map_id2bl(st->oid);
  9260. int skillid=script_getnum(st,2);
  9261. int skilllv=script_getnum(st,3);
  9262. int x=script_getnum(st,4);
  9263. int y=script_getnum(st,5);
  9264. if (bl)
  9265. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9266. return 0;
  9267. }
  9268. /*==========================================
  9269. * Special effects [Valaris]
  9270. *------------------------------------------*/
  9271. BUILDIN_FUNC(specialeffect)
  9272. {
  9273. struct block_list *bl=map_id2bl(st->oid);
  9274. if(bl==NULL)
  9275. return 0;
  9276. clif_specialeffect(bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9277. return 0;
  9278. }
  9279. BUILDIN_FUNC(specialeffect2)
  9280. {
  9281. TBL_PC *sd=script_rid2sd(st);
  9282. if(sd==NULL)
  9283. return 0;
  9284. clif_specialeffect(&sd->bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9285. return 0;
  9286. }
  9287. /*==========================================
  9288. * Nude [Valaris]
  9289. *------------------------------------------*/
  9290. BUILDIN_FUNC(nude)
  9291. {
  9292. TBL_PC *sd=script_rid2sd(st);
  9293. int i,calcflag=0;
  9294. if(sd==NULL)
  9295. return 0;
  9296. for(i=0;i<11;i++)
  9297. if(sd->equip_index[i] >= 0) {
  9298. if(!calcflag)
  9299. calcflag=1;
  9300. pc_unequipitem(sd,sd->equip_index[i],2);
  9301. }
  9302. if(calcflag)
  9303. status_calc_pc(sd,0);
  9304. return 0;
  9305. }
  9306. /*==========================================
  9307. * gmcommand [MouseJstr]
  9308. *
  9309. * suggested on the forums...
  9310. * splitted into atcommand & charcommand by [Skotlex]
  9311. *------------------------------------------*/
  9312. BUILDIN_FUNC(atcommand)
  9313. {
  9314. TBL_PC dummy_sd;
  9315. TBL_PC* sd;
  9316. int fd;
  9317. const char* cmd;
  9318. cmd = script_getstr(st,2);
  9319. if (st->rid) {
  9320. sd = script_rid2sd(st);
  9321. fd = sd->fd;
  9322. } else { //Use a dummy character.
  9323. sd = &dummy_sd;
  9324. fd = 0;
  9325. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9326. if (st->oid)
  9327. {
  9328. struct block_list* bl = map_id2bl(st->oid);
  9329. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9330. if (bl->type == BL_NPC)
  9331. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9332. }
  9333. }
  9334. // compatibility with previous implementation (deprecated!)
  9335. if(cmd[0] != atcommand_symbol)
  9336. {
  9337. cmd += strlen(sd->status.name);
  9338. while(*cmd != atcommand_symbol && *cmd != 0)
  9339. cmd++;
  9340. }
  9341. is_atcommand_sub(fd, sd, cmd, 99);
  9342. return 0;
  9343. }
  9344. BUILDIN_FUNC(charcommand)
  9345. {
  9346. TBL_PC dummy_sd;
  9347. TBL_PC* sd;
  9348. int fd;
  9349. const char* cmd;
  9350. cmd = script_getstr(st,2);
  9351. if (st->rid) {
  9352. sd = script_rid2sd(st);
  9353. fd = sd->fd;
  9354. } else { //Use a dummy character.
  9355. sd = &dummy_sd;
  9356. fd = 0;
  9357. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9358. if (st->oid)
  9359. {
  9360. struct block_list* bl = map_id2bl(st->oid);
  9361. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9362. if (bl->type == BL_NPC)
  9363. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9364. }
  9365. }
  9366. // compatibility with previous implementation (deprecated!)
  9367. if(cmd[0] != charcommand_symbol)
  9368. {
  9369. cmd += strlen(sd->status.name);
  9370. while(*cmd != charcommand_symbol && *cmd != 0)
  9371. cmd++;
  9372. }
  9373. is_charcommand_sub(fd, sd, cmd, 99);
  9374. return 0;
  9375. }
  9376. /*==========================================
  9377. * Displays a message for the player only (like system messages like "you got an apple" )
  9378. *------------------------------------------*/
  9379. BUILDIN_FUNC(dispbottom)
  9380. {
  9381. TBL_PC *sd=script_rid2sd(st);
  9382. const char *message;
  9383. message=script_getstr(st,2);
  9384. if(sd)
  9385. clif_disp_onlyself(sd,message,(int)strlen(message));
  9386. return 0;
  9387. }
  9388. /*==========================================
  9389. * All The Players Full Recovery
  9390. * (HP/SP full restore and resurrect if need)
  9391. *------------------------------------------*/
  9392. BUILDIN_FUNC(recovery)
  9393. {
  9394. TBL_PC *sd, **all_sd;
  9395. int i = 0, users;
  9396. all_sd = map_getallusers(&users);
  9397. for (i = 0; i < users; i++)
  9398. {
  9399. sd = all_sd[i];
  9400. if(pc_isdead(sd))
  9401. status_revive(&sd->bl, 100, 100);
  9402. else
  9403. status_percent_heal(&sd->bl, 100, 100);
  9404. clif_displaymessage(sd->fd,"You have been recovered!");
  9405. }
  9406. return 0;
  9407. }
  9408. /*==========================================
  9409. * Get your pet info: getpetinfo(n)
  9410. * n -> 0:pet_id 1:pet_class 2:pet_name
  9411. * 3:friendly 4:hungry, 5: rename flag.
  9412. *------------------------------------------*/
  9413. BUILDIN_FUNC(getpetinfo)
  9414. {
  9415. TBL_PC *sd=script_rid2sd(st);
  9416. TBL_PET *pd;
  9417. int type=script_getnum(st,2);
  9418. if(!sd || !sd->pd) {
  9419. if (type == 2)
  9420. script_pushconststr(st,"null");
  9421. else
  9422. script_pushint(st,0);
  9423. return 0;
  9424. }
  9425. pd = sd->pd;
  9426. switch(type){
  9427. case 0: script_pushint(st,pd->pet.pet_id); break;
  9428. case 1: script_pushint(st,pd->pet.class_); break;
  9429. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9430. case 3: script_pushint(st,pd->pet.intimate); break;
  9431. case 4: script_pushint(st,pd->pet.hungry); break;
  9432. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9433. default:
  9434. script_pushint(st,0);
  9435. break;
  9436. }
  9437. return 0;
  9438. }
  9439. /*==========================================
  9440. * Get your homunculus info: gethominfo(n)
  9441. * n -> 0:hom_id 1:class 2:name
  9442. * 3:friendly 4:hungry, 5: rename flag.
  9443. * 6: level
  9444. *------------------------------------------*/
  9445. BUILDIN_FUNC(gethominfo)
  9446. {
  9447. TBL_PC *sd=script_rid2sd(st);
  9448. TBL_HOM *hd;
  9449. int type=script_getnum(st,2);
  9450. hd = sd?sd->hd:NULL;
  9451. if(!merc_is_hom_active(hd))
  9452. {
  9453. if (type == 2)
  9454. script_pushconststr(st,"null");
  9455. else
  9456. script_pushint(st,0);
  9457. return 0;
  9458. }
  9459. switch(type){
  9460. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9461. case 1: script_pushint(st,hd->homunculus.class_); break;
  9462. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9463. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9464. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9465. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9466. case 6: script_pushint(st,hd->homunculus.level); break;
  9467. default:
  9468. script_pushint(st,0);
  9469. break;
  9470. }
  9471. return 0;
  9472. }
  9473. /*==========================================
  9474. * Shows wether your inventory(and equips) contain
  9475. selected card or not.
  9476. checkequipedcard(4001);
  9477. *------------------------------------------*/
  9478. BUILDIN_FUNC(checkequipedcard)
  9479. {
  9480. TBL_PC *sd=script_rid2sd(st);
  9481. int n,i,c=0;
  9482. c=script_getnum(st,2);
  9483. if(sd){
  9484. for(i=0;i<MAX_INVENTORY;i++){
  9485. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9486. for(n=0;n<MAX_SLOTS;n++){
  9487. if(sd->status.inventory[i].card[n]==c){
  9488. script_pushint(st,1);
  9489. return 0;
  9490. }
  9491. }
  9492. }
  9493. }
  9494. }
  9495. script_pushint(st,0);
  9496. return 0;
  9497. }
  9498. BUILDIN_FUNC(jump_zero)
  9499. {
  9500. int sel;
  9501. sel=script_getnum(st,2);
  9502. if(!sel) {
  9503. int pos;
  9504. if( !data_islabel(script_getdata(st,3)) ){
  9505. ShowError("script: jump_zero: not label !\n");
  9506. st->state=END;
  9507. return 1;
  9508. }
  9509. pos=script_getnum(st,3);
  9510. st->pos=pos;
  9511. st->state=GOTO;
  9512. // printf("script: jump_zero: jumpto : %d\n",pos);
  9513. } else {
  9514. // printf("script: jump_zero: fail\n");
  9515. }
  9516. return 0;
  9517. }
  9518. /*==========================================
  9519. * GetMapMobs
  9520. returns mob counts on a set map:
  9521. e.g. GetMapMobs("prontera")
  9522. use "this" - for player's map
  9523. *------------------------------------------*/
  9524. BUILDIN_FUNC(getmapmobs)
  9525. {
  9526. const char *str=NULL;
  9527. int m=-1,bx,by;
  9528. int count=0;
  9529. struct block_list *bl;
  9530. str=script_getstr(st,2);
  9531. if(strcmp(str,"this")==0){
  9532. TBL_PC *sd=script_rid2sd(st);
  9533. if(sd)
  9534. m=sd->bl.m;
  9535. else{
  9536. script_pushint(st,-1);
  9537. return 0;
  9538. }
  9539. }else
  9540. m=map_mapname2mapid(str);
  9541. if(m < 0){
  9542. script_pushint(st,-1);
  9543. return 0;
  9544. }
  9545. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  9546. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  9547. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  9548. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9549. count++;
  9550. script_pushint(st,count);
  9551. return 0;
  9552. }
  9553. /*==========================================
  9554. * movenpc [MouseJstr]
  9555. *------------------------------------------*/
  9556. BUILDIN_FUNC(movenpc)
  9557. {
  9558. TBL_NPC *nd = NULL;
  9559. const char *npc;
  9560. int x,y;
  9561. npc = script_getstr(st,2);
  9562. x = script_getnum(st,3);
  9563. y = script_getnum(st,4);
  9564. if ((nd = npc_name2id(npc)) == NULL)
  9565. return -1;
  9566. npc_movenpc(nd, x, y);
  9567. return 0;
  9568. }
  9569. /*==========================================
  9570. * message [MouseJstr]
  9571. *------------------------------------------*/
  9572. BUILDIN_FUNC(message)
  9573. {
  9574. TBL_PC *sd;
  9575. const char *msg,*player;
  9576. TBL_PC *pl_sd = NULL;
  9577. sd = script_rid2sd(st);
  9578. player = script_getstr(st,2);
  9579. msg = script_getstr(st,3);
  9580. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9581. return 0;
  9582. clif_displaymessage(pl_sd->fd, msg);
  9583. return 0;
  9584. }
  9585. /*==========================================
  9586. * npctalk (sends message to surrounding area)
  9587. *------------------------------------------*/
  9588. BUILDIN_FUNC(npctalk)
  9589. {
  9590. const char* str;
  9591. char message[255];
  9592. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9593. str = script_getstr(st,2);
  9594. if(nd) {
  9595. memcpy(message, nd->name, NAME_LENGTH);
  9596. strtok(message, "#"); // discard extra name identifier if present
  9597. strcat(message, " : ");
  9598. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9599. clif_message(&(nd->bl), message);
  9600. }
  9601. return 0;
  9602. }
  9603. // change npc walkspeed [Valaris]
  9604. BUILDIN_FUNC(npcspeed)
  9605. {
  9606. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9607. int x=0;
  9608. x=script_getnum(st,2);
  9609. if(nd) {
  9610. nd->speed=x;
  9611. }
  9612. return 0;
  9613. }
  9614. // make an npc walk to a position [Valaris]
  9615. BUILDIN_FUNC(npcwalkto)
  9616. {
  9617. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9618. int x=0,y=0;
  9619. x=script_getnum(st,2);
  9620. y=script_getnum(st,3);
  9621. if(nd) {
  9622. unit_walktoxy(&nd->bl,x,y,0);
  9623. }
  9624. return 0;
  9625. }
  9626. // stop an npc's movement [Valaris]
  9627. BUILDIN_FUNC(npcstop)
  9628. {
  9629. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9630. if(nd) {
  9631. unit_stop_walking(&nd->bl,1);
  9632. }
  9633. return 0;
  9634. }
  9635. /*==========================================
  9636. * getlook char info. getlook(arg)
  9637. *------------------------------------------*/
  9638. BUILDIN_FUNC(getlook)
  9639. {
  9640. int type,val;
  9641. TBL_PC *sd;
  9642. sd=script_rid2sd(st);
  9643. type=script_getnum(st,2);
  9644. val=-1;
  9645. switch(type) {
  9646. case LOOK_HAIR: val=sd->status.hair; break; //1
  9647. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9648. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9649. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9650. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9651. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9652. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9653. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9654. case LOOK_SHOES: break; //9
  9655. }
  9656. script_pushint(st,val);
  9657. return 0;
  9658. }
  9659. /*==========================================
  9660. * get char save point. argument: 0- map name, 1- x, 2- y
  9661. *------------------------------------------*/
  9662. BUILDIN_FUNC(getsavepoint)
  9663. {
  9664. TBL_PC* sd;
  9665. int type;
  9666. sd = script_rid2sd(st);
  9667. if (sd == NULL) {
  9668. script_pushint(st,0);
  9669. return 0;
  9670. }
  9671. type = script_getnum(st,2);
  9672. switch(type) {
  9673. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  9674. case 1: script_pushint(st,sd->status.save_point.x); break;
  9675. case 2: script_pushint(st,sd->status.save_point.y); break;
  9676. default:
  9677. script_pushint(st,0);
  9678. break;
  9679. }
  9680. return 0;
  9681. }
  9682. /*==========================================
  9683. * Get position for char/npc/pet/mob objects. Added by Lorky
  9684. *
  9685. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  9686. * where type:
  9687. * MapName$ - String variable for output map name
  9688. * MapX - Integer variable for output coord X
  9689. * MapY - Integer variable for output coord Y
  9690. * type - type of object
  9691. * 0 - Character coord
  9692. * 1 - NPC coord
  9693. * 2 - Pet coord
  9694. * 3 - Mob coord (not released)
  9695. * 4 - Homun coord
  9696. * CharName$ - Name object. If miss or "this" the current object
  9697. *
  9698. * Return:
  9699. * 0 - success
  9700. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9701. *------------------------------------------*/
  9702. BUILDIN_FUNC(getmapxy)
  9703. {
  9704. struct block_list *bl = NULL;
  9705. TBL_PC *sd=NULL;
  9706. int num;
  9707. char *name;
  9708. char prefix;
  9709. int x,y,type;
  9710. char mapname[MAP_NAME_LENGTH];
  9711. memset(mapname, 0, sizeof(mapname));
  9712. if( !data_isreference(script_getdata(st,2)) ){
  9713. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9714. script_pushint(st,-1);
  9715. return 1;
  9716. }
  9717. if( !data_isreference(script_getdata(st,3)) ){
  9718. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9719. script_pushint(st,-1);
  9720. return 1;
  9721. }
  9722. if( !data_isreference(script_getdata(st,4)) ){
  9723. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9724. script_pushint(st,-1);
  9725. return 1;
  9726. }
  9727. // Possible needly check function parameters on C_STR,C_INT,C_INT
  9728. type=script_getnum(st,5);
  9729. switch (type){
  9730. case 0: //Get Character Position
  9731. if( script_hasdata(st,6) )
  9732. sd=map_nick2sd(script_getstr(st,6));
  9733. else
  9734. sd=script_rid2sd(st);
  9735. if (sd)
  9736. bl = &sd->bl;
  9737. break;
  9738. case 1: //Get NPC Position
  9739. if( script_hasdata(st,6) )
  9740. {
  9741. struct npc_data *nd;
  9742. nd=npc_name2id(script_getstr(st,6));
  9743. if (nd)
  9744. bl = &nd->bl;
  9745. } else //In case the origin is not an npc?
  9746. bl=map_id2bl(st->oid);
  9747. break;
  9748. case 2: //Get Pet Position
  9749. if(script_hasdata(st,6))
  9750. sd=map_nick2sd(script_getstr(st,6));
  9751. else
  9752. sd=script_rid2sd(st);
  9753. if (sd && sd->pd)
  9754. bl = &sd->pd->bl;
  9755. break;
  9756. case 3: //Get Mob Position
  9757. break; //Not supported?
  9758. case 4: //Get Homun Position
  9759. if(script_hasdata(st,6))
  9760. sd=map_nick2sd(script_getstr(st,6));
  9761. else
  9762. sd=script_rid2sd(st);
  9763. if (sd && sd->hd)
  9764. bl = &sd->hd->bl;
  9765. break;
  9766. }
  9767. if (!bl) { //No object found.
  9768. script_pushint(st,-1);
  9769. return 0;
  9770. }
  9771. x= bl->x;
  9772. y= bl->y;
  9773. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9774. //Set MapName$
  9775. num=st->stack->stack_data[st->start+2].u.num;
  9776. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9777. prefix=*name;
  9778. if(not_server_variable(prefix))
  9779. sd=script_rid2sd(st);
  9780. else
  9781. sd=NULL;
  9782. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  9783. //Set MapX
  9784. num=st->stack->stack_data[st->start+3].u.num;
  9785. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9786. prefix=*name;
  9787. if(not_server_variable(prefix))
  9788. sd=script_rid2sd(st);
  9789. else
  9790. sd=NULL;
  9791. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  9792. //Set MapY
  9793. num=st->stack->stack_data[st->start+4].u.num;
  9794. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9795. prefix=*name;
  9796. if(not_server_variable(prefix))
  9797. sd=script_rid2sd(st);
  9798. else
  9799. sd=NULL;
  9800. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  9801. //Return Success value
  9802. script_pushint(st,0);
  9803. return 0;
  9804. }
  9805. /*==========================================
  9806. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  9807. *------------------------------------------*/
  9808. BUILDIN_FUNC(logmes)
  9809. {
  9810. const char *str;
  9811. if (log_config.npc <= 0 ) return 0;
  9812. str = script_getstr(st,2);
  9813. log_npc(script_rid2sd(st),str);
  9814. return 0;
  9815. }
  9816. BUILDIN_FUNC(summon)
  9817. {
  9818. int _class, timeout=0;
  9819. const char *str,*event="";
  9820. TBL_PC *sd;
  9821. struct mob_data *md;
  9822. int tick = gettick();
  9823. sd=script_rid2sd(st);
  9824. if (!sd) return 0;
  9825. str =script_getstr(st,2);
  9826. _class=script_getnum(st,3);
  9827. if( script_hasdata(st,4) )
  9828. timeout=script_getnum(st,4);
  9829. if( script_hasdata(st,5) ){
  9830. event=script_getstr(st,5);
  9831. check_event(st, event);
  9832. }
  9833. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  9834. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  9835. if (md) {
  9836. md->master_id=sd->bl.id;
  9837. md->special_state.ai=1;
  9838. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  9839. mob_spawn (md); //Now it is ready for spawning.
  9840. clif_misceffect2(&md->bl,344);
  9841. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  9842. }
  9843. return 0;
  9844. }
  9845. /*==========================================
  9846. * Checks whether it is daytime/nighttime
  9847. *------------------------------------------*/
  9848. BUILDIN_FUNC(isnight)
  9849. {
  9850. script_pushint(st,(night_flag == 1));
  9851. return 0;
  9852. }
  9853. BUILDIN_FUNC(isday)
  9854. {
  9855. script_pushint(st,(night_flag == 0));
  9856. return 0;
  9857. }
  9858. /*================================================
  9859. * Check whether another item/card has been
  9860. * equipped - used for 2/15's cards patch [celest]
  9861. *------------------------------------------------*/
  9862. // leave this here, just in case
  9863. #if 0
  9864. BUILDIN_FUNC(isequipped)
  9865. {
  9866. TBL_PC *sd;
  9867. int i, j, k, id = 1;
  9868. int ret = -1;
  9869. sd = script_rid2sd(st);
  9870. for (i=0; id!=0; i++) {
  9871. int flag = 0;
  9872. FETCH (i+2, id) else id = 0;
  9873. if (id <= 0)
  9874. continue;
  9875. for (j=0; j<EQI_MAX; j++) {
  9876. int index;
  9877. index = sd->equip_index[j];
  9878. if(index < 0) continue;
  9879. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9880. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9881. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9882. if(!sd->inventory_data[index])
  9883. continue;
  9884. if(itemdb_type(id) != IT_CARD) { //Non card
  9885. if (sd->inventory_data[index]->nameid != id)
  9886. continue;
  9887. flag = 1;
  9888. break;
  9889. } else { //Card
  9890. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9891. continue;
  9892. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9893. if (sd->status.inventory[index].card[k] == id)
  9894. {
  9895. flag = 1;
  9896. break;
  9897. }
  9898. }
  9899. }
  9900. if (flag) break;
  9901. }
  9902. if (ret == -1)
  9903. ret = flag;
  9904. else
  9905. ret &= flag;
  9906. if (!ret) break;
  9907. }
  9908. script_pushint(st,ret);
  9909. return 0;
  9910. }
  9911. #endif
  9912. /*================================================
  9913. * Check how many items/cards in the list are
  9914. * equipped - used for 2/15's cards patch [celest]
  9915. *------------------------------------------------*/
  9916. BUILDIN_FUNC(isequippedcnt)
  9917. {
  9918. TBL_PC *sd;
  9919. int i, j, k, id = 1;
  9920. int ret = 0;
  9921. sd = script_rid2sd(st);
  9922. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9923. script_pushint(st,0);
  9924. return 0;
  9925. }
  9926. for (i=0; id!=0; i++) {
  9927. FETCH (i+2, id) else id = 0;
  9928. if (id <= 0)
  9929. continue;
  9930. for (j=0; j<EQI_MAX; j++) {
  9931. int index;
  9932. index = sd->equip_index[j];
  9933. if(index < 0) continue;
  9934. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9935. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9936. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9937. if(!sd->inventory_data[index])
  9938. continue;
  9939. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  9940. if (sd->inventory_data[index]->nameid == id)
  9941. ret+= sd->status.inventory[index].amount;
  9942. } else { //Count cards.
  9943. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9944. continue; //No cards
  9945. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9946. if (sd->status.inventory[index].card[k] == id)
  9947. ret++; //[Lupus]
  9948. }
  9949. }
  9950. }
  9951. }
  9952. script_pushint(st,ret);
  9953. return 0;
  9954. }
  9955. /*================================================
  9956. * Check whether another card has been
  9957. * equipped - used for 2/15's cards patch [celest]
  9958. * -- Items checked cannot be reused in another
  9959. * card set to prevent exploits
  9960. *------------------------------------------------*/
  9961. BUILDIN_FUNC(isequipped)
  9962. {
  9963. TBL_PC *sd;
  9964. int i, j, k, id = 1;
  9965. int index, flag;
  9966. int ret = -1;
  9967. //Original hash to reverse it when full check fails.
  9968. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  9969. sd = script_rid2sd(st);
  9970. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9971. script_pushint(st,0);
  9972. return 0;
  9973. }
  9974. setitem_hash = sd->setitem_hash;
  9975. setitem_hash2 = sd->setitem_hash2;
  9976. for (i=0; id!=0; i++)
  9977. {
  9978. FETCH (i+2, id) else id = 0;
  9979. if (id <= 0)
  9980. continue;
  9981. flag = 0;
  9982. for (j=0; j<EQI_MAX; j++)
  9983. {
  9984. index = sd->equip_index[j];
  9985. if(index < 0) continue;
  9986. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9987. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9988. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9989. if(!sd->inventory_data[index])
  9990. continue;
  9991. if (itemdb_type(id) != IT_CARD) {
  9992. if (sd->inventory_data[index]->nameid != id)
  9993. continue;
  9994. flag = 1;
  9995. break;
  9996. } else { //Cards
  9997. if (sd->inventory_data[index]->slot == 0 ||
  9998. itemdb_isspecial(sd->status.inventory[index].card[0]))
  9999. continue;
  10000. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10001. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10002. unsigned int hash = 0;
  10003. if (sd->status.inventory[index].card[k] != id)
  10004. continue;
  10005. hash = 1<<((j<5?j:j-5)*4 + k);
  10006. // check if card is already used by another set
  10007. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10008. continue;
  10009. // We have found a match
  10010. flag = 1;
  10011. // Set hash so this card cannot be used by another
  10012. if (j<5)
  10013. sd->setitem_hash |= hash;
  10014. else
  10015. sd->setitem_hash2 |= hash;
  10016. break;
  10017. }
  10018. }
  10019. if (flag) break; //Card found
  10020. }
  10021. if (ret == -1)
  10022. ret = flag;
  10023. else
  10024. ret &= flag;
  10025. if (!ret) break;
  10026. }
  10027. if (!ret)
  10028. { //When check fails, restore original hash values. [Skotlex]
  10029. sd->setitem_hash = setitem_hash;
  10030. sd->setitem_hash2 = setitem_hash2;
  10031. }
  10032. script_pushint(st,ret);
  10033. return 0;
  10034. }
  10035. /*================================================
  10036. * Check how many given inserted cards in the CURRENT
  10037. * weapon - used for 2/15's cards patch [Lupus]
  10038. *------------------------------------------------*/
  10039. BUILDIN_FUNC(cardscnt)
  10040. {
  10041. TBL_PC *sd;
  10042. int i, k, id = 1;
  10043. int ret = 0;
  10044. int index;
  10045. sd = script_rid2sd(st);
  10046. for (i=0; id!=0; i++) {
  10047. FETCH (i+2, id) else id = 0;
  10048. if (id <= 0)
  10049. continue;
  10050. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10051. if(index < 0) continue;
  10052. if(!sd->inventory_data[index])
  10053. continue;
  10054. if(itemdb_type(id) != IT_CARD) {
  10055. if (sd->inventory_data[index]->nameid == id)
  10056. ret+= sd->status.inventory[index].amount;
  10057. } else {
  10058. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10059. continue;
  10060. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10061. if (sd->status.inventory[index].card[k] == id)
  10062. ret++;
  10063. }
  10064. }
  10065. }
  10066. script_pushint(st,ret);
  10067. // script_pushint(st,current_equip_item_index);
  10068. return 0;
  10069. }
  10070. /*=======================================================
  10071. * Returns the refined number of the current item, or an
  10072. * item with inventory index specified
  10073. *-------------------------------------------------------*/
  10074. BUILDIN_FUNC(getrefine)
  10075. {
  10076. TBL_PC *sd;
  10077. if ((sd = script_rid2sd(st))!= NULL)
  10078. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10079. else
  10080. script_pushint(st,0);
  10081. return 0;
  10082. }
  10083. /*=======================================================
  10084. * Allows 2 Parents to adopt a character as a Baby
  10085. *-------------------------------------------------------*/
  10086. BUILDIN_FUNC(adopt)
  10087. {
  10088. int ret;
  10089. const char *parent1 = script_getstr(st,2);
  10090. const char *parent2 = script_getstr(st,3);
  10091. const char *child = script_getstr(st,4);
  10092. TBL_PC *p1_sd = map_nick2sd(parent1);
  10093. TBL_PC *p2_sd = map_nick2sd(parent2);
  10094. TBL_PC *c_sd = map_nick2sd(child);
  10095. if (!p1_sd || !p2_sd || !c_sd ||
  10096. p1_sd->status.base_level < 70 ||
  10097. p2_sd->status.base_level < 70)
  10098. return 0;
  10099. ret = pc_adoption(p1_sd, p2_sd, c_sd);
  10100. script_pushint(st,ret);
  10101. return 0;
  10102. }
  10103. /*=======================================================
  10104. * Day/Night controls
  10105. *-------------------------------------------------------*/
  10106. BUILDIN_FUNC(night)
  10107. {
  10108. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10109. return 0;
  10110. }
  10111. BUILDIN_FUNC(day)
  10112. {
  10113. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10114. return 0;
  10115. }
  10116. //=======================================================
  10117. // Unequip [Spectre]
  10118. //-------------------------------------------------------
  10119. BUILDIN_FUNC(unequip)
  10120. {
  10121. int i;
  10122. size_t num;
  10123. TBL_PC *sd;
  10124. num = script_getnum(st,2);
  10125. sd = script_rid2sd(st);
  10126. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10127. {
  10128. i = pc_checkequip(sd,equip[num-1]);
  10129. if (i >= 0)
  10130. pc_unequipitem(sd,i,2);
  10131. return 0;
  10132. }
  10133. return 0;
  10134. }
  10135. BUILDIN_FUNC(equip)
  10136. {
  10137. int nameid=0,i;
  10138. TBL_PC *sd;
  10139. struct item_data *item_data;
  10140. sd = script_rid2sd(st);
  10141. nameid=script_getnum(st,2);
  10142. if((item_data = itemdb_exists(nameid)) == NULL)
  10143. {
  10144. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10145. return 1;
  10146. }
  10147. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10148. if( i < MAX_INVENTORY )
  10149. pc_equipitem(sd,i,item_data->equip);
  10150. return 0;
  10151. }
  10152. BUILDIN_FUNC(autoequip)
  10153. {
  10154. int nameid, flag;
  10155. struct item_data *item_data;
  10156. nameid=script_getnum(st,2);
  10157. flag=script_getnum(st,3);
  10158. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10159. item_data->flag.autoequip = flag>0?1:0;
  10160. }
  10161. return 0;
  10162. }
  10163. BUILDIN_FUNC(setbattleflag)
  10164. {
  10165. const char *flag, *value;
  10166. flag = script_getstr(st,2);
  10167. value = script_getstr(st,3);
  10168. if (battle_set_value(flag, value) == 0)
  10169. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10170. else
  10171. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10172. return 0;
  10173. }
  10174. BUILDIN_FUNC(getbattleflag)
  10175. {
  10176. const char *flag;
  10177. flag = script_getstr(st,2);
  10178. script_pushint(st,battle_get_value(flag));
  10179. return 0;
  10180. }
  10181. //=======================================================
  10182. // strlen [Valaris]
  10183. //-------------------------------------------------------
  10184. BUILDIN_FUNC(getstrlen)
  10185. {
  10186. const char *str = script_getstr(st,2);
  10187. int len = (str) ? (int)strlen(str) : 0;
  10188. script_pushint(st,len);
  10189. return 0;
  10190. }
  10191. //=======================================================
  10192. // isalpha [Valaris]
  10193. //-------------------------------------------------------
  10194. BUILDIN_FUNC(charisalpha)
  10195. {
  10196. const char *str=script_getstr(st,2);
  10197. int pos=script_getnum(st,3);
  10198. int val = ( str && pos>0 && (unsigned int)pos<strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10199. script_pushint(st,val);
  10200. return 0;
  10201. }
  10202. /// Changes the display name and/or display class of the npc.
  10203. /// Returns 0 is successful, 1 if the npc does not exist.
  10204. ///
  10205. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10206. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10207. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10208. BUILDIN_FUNC(setnpcdisplay)
  10209. {
  10210. const char* name;
  10211. const char* newname = NULL;
  10212. int class_ = -1;
  10213. struct script_data* data;
  10214. struct npc_data* nd;
  10215. name = script_getstr(st,2);
  10216. data = script_getdata(st,3);
  10217. get_val(st, data);
  10218. if( script_hasdata(st,4) )
  10219. {
  10220. newname = conv_str(st,data);
  10221. class_ = script_getnum(st,4);
  10222. }
  10223. else if( data_isstring(data) )
  10224. {
  10225. newname = conv_str(st,data);
  10226. }
  10227. else if( data_isint(data) )
  10228. {
  10229. class_ = conv_num(st,data);
  10230. }
  10231. else
  10232. {
  10233. ShowError("script:setnpcdisplay: expected a string or number\n");
  10234. script_reportdata(data);
  10235. return 1;
  10236. }
  10237. nd = npc_name2id(name);
  10238. if( nd == NULL )
  10239. {// not found
  10240. script_pushint(st,1);
  10241. return 0;
  10242. }
  10243. // update npc
  10244. if( newname )
  10245. npc_setdisplayname(nd, newname);
  10246. if( class_ != -1 )
  10247. npc_setclass(nd, class_);
  10248. script_pushint(st,0);
  10249. return 0;
  10250. }
  10251. BUILDIN_FUNC(atoi)
  10252. {
  10253. const char *value;
  10254. value = script_getstr(st,2);
  10255. script_pushint(st,atoi(value));
  10256. return 0;
  10257. }
  10258. // case-insensitive substring search [lordalfa]
  10259. BUILDIN_FUNC(compare)
  10260. {
  10261. const char *message;
  10262. const char *cmpstring;
  10263. message = script_getstr(st,2);
  10264. cmpstring = script_getstr(st,3);
  10265. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10266. return 0;
  10267. }
  10268. // [zBuffer] List of mathematics commands --->
  10269. BUILDIN_FUNC(sqrt)
  10270. {
  10271. double i, a;
  10272. i = script_getnum(st,2);
  10273. a = sqrt(i);
  10274. script_pushint(st,(int)a);
  10275. return 0;
  10276. }
  10277. BUILDIN_FUNC(pow)
  10278. {
  10279. double i, a, b;
  10280. a = script_getnum(st,2);
  10281. b = script_getnum(st,3);
  10282. i = pow(a,b);
  10283. script_pushint(st,(int)i);
  10284. return 0;
  10285. }
  10286. BUILDIN_FUNC(distance)
  10287. {
  10288. int x0, y0, x1, y1;
  10289. x0 = script_getnum(st,2);
  10290. y0 = script_getnum(st,3);
  10291. x1 = script_getnum(st,4);
  10292. y1 = script_getnum(st,5);
  10293. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10294. return 0;
  10295. }
  10296. BUILDIN_FUNC(checkcell)
  10297. {
  10298. int m;
  10299. const char *map = script_getstr(st, 2);
  10300. m = mapindex_name2id(map);
  10301. if(m){
  10302. script_pushint(st,map_getcell(m, script_getnum(st,3), script_getnum(st,4),script_getnum(st,5)));
  10303. } else {
  10304. script_pushint(st,0);
  10305. }
  10306. return 0;
  10307. }
  10308. // <--- [zBuffer] List of mathematics commands
  10309. // [zBuffer] List of dynamic var commands --->
  10310. //FIXME: some other functions are using this private function
  10311. void setd_sub(struct script_state *st, TBL_PC *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10312. {
  10313. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10314. return;
  10315. }
  10316. BUILDIN_FUNC(setd)
  10317. {
  10318. TBL_PC *sd=NULL;
  10319. char varname[100];
  10320. const char *value, *buffer;
  10321. int elem;
  10322. buffer = script_getstr(st, 2);
  10323. value = script_getstr(st, 3);
  10324. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10325. elem = 0;
  10326. if( not_server_variable(*varname) )
  10327. {
  10328. sd = script_rid2sd(st);
  10329. if( sd == NULL )
  10330. {
  10331. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10332. return 0;
  10333. }
  10334. }
  10335. if(varname[strlen(varname)-1] != '$') {
  10336. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10337. } else {
  10338. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10339. }
  10340. return 0;
  10341. }
  10342. #ifndef TXT_ONLY
  10343. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10344. {
  10345. int i, j;
  10346. TBL_PC* sd = NULL;
  10347. const char* query;
  10348. struct script_data* data;
  10349. char* name;
  10350. int max_rows = 128;// maximum number of rows
  10351. int num_vars;
  10352. int num_cols;
  10353. // check target variables
  10354. for( i = 3; script_hasdata(st,i); ++i )
  10355. {
  10356. data = script_getdata(st, i);
  10357. if( data_isreference(data) && reference_tovariable(data) )
  10358. {// it's a variable
  10359. name = reference_getname(data);
  10360. if( not_server_variable(*name) && sd == NULL )
  10361. {// requires a player
  10362. sd = script_rid2sd(st);
  10363. if( sd == NULL )
  10364. {// no player attached
  10365. script_reportdata(data);
  10366. st->state = END;
  10367. return 1;
  10368. }
  10369. }
  10370. if( not_array_variable(*name) )
  10371. max_rows = 1;// not an array, limit to one row
  10372. }
  10373. else
  10374. {
  10375. ShowError("script:query_sql: not a variable\n");
  10376. script_reportdata(data);
  10377. st->state = END;
  10378. return 1;
  10379. }
  10380. }
  10381. num_vars = i - 3;
  10382. // Execute the query
  10383. query = script_getstr(st,2);
  10384. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10385. {
  10386. Sql_ShowDebug(handle);
  10387. script_pushint(st, 0);
  10388. return 1;
  10389. }
  10390. if( Sql_NumRows(handle) == 0 )
  10391. {// No data received
  10392. Sql_FreeResult(handle);
  10393. script_pushint(st, 0);
  10394. return 0;
  10395. }
  10396. // Count the number of columns to store
  10397. num_cols = Sql_NumColumns(handle);
  10398. if( num_vars < num_cols )
  10399. {
  10400. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10401. script_reportsrc(st);
  10402. }
  10403. else if( num_vars > num_cols )
  10404. {
  10405. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10406. script_reportsrc(st);
  10407. }
  10408. // Store data
  10409. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10410. {
  10411. for( j = 0; j < num_vars; ++j )
  10412. {
  10413. char* str = NULL;
  10414. if( j < num_cols )
  10415. Sql_GetData(handle, j, &str, NULL);
  10416. data = script_getdata(st, j+3);
  10417. name = reference_getname(data);
  10418. if( is_string_variable(name) )
  10419. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10420. else
  10421. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10422. }
  10423. }
  10424. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10425. {
  10426. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10427. script_reportsrc(st);
  10428. }
  10429. // Free data
  10430. Sql_FreeResult(handle);
  10431. script_pushint(st, i);
  10432. return 0;
  10433. }
  10434. #endif
  10435. BUILDIN_FUNC(query_sql)
  10436. {
  10437. #ifndef TXT_ONLY
  10438. return buildin_query_sql_sub(st, mmysql_handle);
  10439. #else
  10440. //for TXT version, we always return -1
  10441. script_pushint(st,-1);
  10442. return 0;
  10443. #endif
  10444. }
  10445. BUILDIN_FUNC(query_logsql)
  10446. {
  10447. #ifndef TXT_ONLY
  10448. return buildin_query_sql_sub(st, logmysql_handle);
  10449. #else
  10450. //for TXT version, we always return -1
  10451. script_pushint(st,-1);
  10452. return 0;
  10453. #endif
  10454. }
  10455. //Allows escaping of a given string.
  10456. BUILDIN_FUNC(escape_sql)
  10457. {
  10458. const char *str;
  10459. char *esc_str;
  10460. size_t len;
  10461. str = script_getstr(st,2);
  10462. len = strlen(str);
  10463. esc_str = aMallocA(len*2+1);
  10464. #if defined(TXT_ONLY)
  10465. jstrescapecpy(esc_str, str);
  10466. #else
  10467. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10468. #endif
  10469. script_pushstr(st, esc_str);
  10470. return 0;
  10471. }
  10472. BUILDIN_FUNC(getd)
  10473. {
  10474. char varname[100];
  10475. const char *buffer;
  10476. int elem;
  10477. buffer = script_getstr(st, 2);
  10478. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10479. elem = 0;
  10480. // Push the 'pointer' so it's more flexible [Lance]
  10481. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10482. return 0;
  10483. }
  10484. // <--- [zBuffer] List of dynamic var commands
  10485. // Pet stat [Lance]
  10486. BUILDIN_FUNC(petstat)
  10487. {
  10488. TBL_PC *sd = NULL;
  10489. struct pet_data *pd;
  10490. int flag = script_getnum(st,2);
  10491. sd = script_rid2sd(st);
  10492. if(!sd || !sd->status.pet_id || !sd->pd){
  10493. if(flag == 2)
  10494. script_pushconststr(st, "");
  10495. else
  10496. script_pushint(st,0);
  10497. return 0;
  10498. }
  10499. pd = sd->pd;
  10500. switch(flag){
  10501. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10502. case 2: script_pushstrcopy(st, pd->pet.name); break;
  10503. case 3: script_pushint(st,(int)pd->pet.level); break;
  10504. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10505. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10506. default:
  10507. script_pushint(st,0);
  10508. break;
  10509. }
  10510. return 0;
  10511. }
  10512. BUILDIN_FUNC(callshop)
  10513. {
  10514. TBL_PC *sd = NULL;
  10515. struct npc_data *nd;
  10516. const char *shopname;
  10517. int flag = 0;
  10518. sd = script_rid2sd(st);
  10519. if (!sd) {
  10520. script_pushint(st,0);
  10521. return 0;
  10522. }
  10523. shopname = script_getstr(st, 2);
  10524. if( script_hasdata(st,3) )
  10525. flag = script_getnum(st,3);
  10526. nd = npc_name2id(shopname);
  10527. if (!nd || nd->bl.type!=BL_NPC || nd->subtype!=SHOP) {
  10528. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10529. script_pushint(st,0);
  10530. return 1;
  10531. }
  10532. switch (flag) {
  10533. case 1: //Buy window
  10534. npc_buysellsel(sd,nd->bl.id,0);
  10535. break;
  10536. case 2: //Sell window
  10537. npc_buysellsel(sd,nd->bl.id,1);
  10538. break;
  10539. default: //Show menu
  10540. clif_npcbuysell(sd,nd->bl.id);
  10541. break;
  10542. }
  10543. sd->npc_shopid = nd->bl.id;
  10544. script_pushint(st,1);
  10545. return 0;
  10546. }
  10547. BUILDIN_FUNC(npcshopitem)
  10548. {
  10549. const char* npcname = script_getstr(st, 2);
  10550. struct npc_data* nd = npc_name2id(npcname);
  10551. int n, i;
  10552. int amount;
  10553. if( !nd || nd->subtype != SHOP )
  10554. { //Not found.
  10555. script_pushint(st,0);
  10556. return 0;
  10557. }
  10558. // get the count of new entries
  10559. amount = (script_lastdata(st)-2)/2;
  10560. // generate new shop item list
  10561. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  10562. for( n = 0, i = 3; n < amount; n++, i+=2 )
  10563. {
  10564. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10565. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10566. }
  10567. nd->u.shop.count = n;
  10568. script_pushint(st,1);
  10569. return 0;
  10570. }
  10571. BUILDIN_FUNC(npcshopadditem)
  10572. {
  10573. const char* npcname = script_getstr(st,2);
  10574. struct npc_data* nd = npc_name2id(npcname);
  10575. int n, i;
  10576. int amount;
  10577. if( !nd || nd->subtype != SHOP )
  10578. { //Not found.
  10579. script_pushint(st,0);
  10580. return 0;
  10581. }
  10582. // get the count of new entries
  10583. amount = (script_lastdata(st)-2)/2;
  10584. // append new items to existing shop item list
  10585. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  10586. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  10587. {
  10588. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10589. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10590. }
  10591. nd->u.shop.count = n;
  10592. script_pushint(st,1);
  10593. return 0;
  10594. }
  10595. BUILDIN_FUNC(npcshopdelitem)
  10596. {
  10597. const char* npcname = script_getstr(st,2);
  10598. struct npc_data* nd = npc_name2id(npcname);
  10599. int n, i;
  10600. int amount;
  10601. int size;
  10602. if( !nd || nd->subtype != SHOP )
  10603. { //Not found.
  10604. script_pushint(st,0);
  10605. return 0;
  10606. }
  10607. amount = script_lastdata(st)-2;
  10608. size = nd->u.shop.count;
  10609. // remove specified items from the shop item list
  10610. for( i = 3; i < 3 + amount; i++ )
  10611. {
  10612. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  10613. if( n < size )
  10614. {
  10615. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  10616. size--;
  10617. }
  10618. }
  10619. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  10620. nd->u.shop.count = size;
  10621. script_pushint(st,1);
  10622. return 0;
  10623. }
  10624. //Sets a script to attach to a shop npc.
  10625. BUILDIN_FUNC(npcshopattach)
  10626. {
  10627. const char* npcname = script_getstr(st,2);
  10628. struct npc_data* nd = npc_name2id(npcname);
  10629. int flag = 1;
  10630. if( script_hasdata(st,3) )
  10631. flag = script_getnum(st,3);
  10632. if( !nd || nd->subtype != SHOP )
  10633. { //Not found.
  10634. script_pushint(st,0);
  10635. return 0;
  10636. }
  10637. if (flag)
  10638. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10639. else
  10640. nd->master_nd = NULL;
  10641. script_pushint(st,1);
  10642. return 0;
  10643. }
  10644. /*==========================================
  10645. * Returns some values of an item [Lupus]
  10646. * Price, Weight, etc...
  10647. setitemscript(itemID,"{new item bonus script}",[n]);
  10648. Where n:
  10649. 0 - script
  10650. 1 - Equip script
  10651. 2 - Unequip script
  10652. *------------------------------------------*/
  10653. BUILDIN_FUNC(setitemscript)
  10654. {
  10655. int item_id,n=0;
  10656. const char *script;
  10657. struct item_data *i_data;
  10658. struct script_code **dstscript;
  10659. item_id = script_getnum(st,2);
  10660. script = script_getstr(st,3);
  10661. if( script_hasdata(st,4) )
  10662. n=script_getnum(st,4);
  10663. i_data = itemdb_exists(item_id);
  10664. if (!i_data || script==NULL || script[0]!='{') {
  10665. script_pushint(st,0);
  10666. return 0;
  10667. }
  10668. switch (n) {
  10669. case 2:
  10670. dstscript = &i_data->unequip_script;
  10671. break;
  10672. case 1:
  10673. dstscript = &i_data->equip_script;
  10674. break;
  10675. default:
  10676. dstscript = &i_data->script;
  10677. break;
  10678. }
  10679. if(*dstscript)
  10680. script_free_code(*dstscript);
  10681. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  10682. script_pushint(st,1);
  10683. return 0;
  10684. }
  10685. /* Work In Progress [Lupus]
  10686. BUILDIN_FUNC(addmonsterdrop)
  10687. {
  10688. int class_,item_id,chance;
  10689. class_=script_getnum(st,2);
  10690. item_id=script_getnum(st,3);
  10691. chance=script_getnum(st,4);
  10692. if(class_>1000 && item_id>500 && chance>0) {
  10693. script_pushint(st,1);
  10694. } else {
  10695. script_pushint(st,0);
  10696. }
  10697. }
  10698. BUILDIN_FUNC(delmonsterdrop)
  10699. {
  10700. int class_,item_id;
  10701. class_=script_getnum(st,2);
  10702. item_id=script_getnum(st,3);
  10703. if(class_>1000 && item_id>500) {
  10704. script_pushint(st,1);
  10705. } else {
  10706. script_pushint(st,0);
  10707. }
  10708. }
  10709. */
  10710. /*==========================================
  10711. * Returns some values of a monster [Lupus]
  10712. * Name, Level, race, size, etc...
  10713. getmonsterinfo(monsterID,queryIndex);
  10714. *------------------------------------------*/
  10715. BUILDIN_FUNC(getmonsterinfo)
  10716. {
  10717. struct mob_db *mob;
  10718. int mob_id;
  10719. mob_id = script_getnum(st,2);
  10720. if (!mobdb_checkid(mob_id)) {
  10721. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i", mob_id);
  10722. script_pushint(st,-1);
  10723. return -1;
  10724. }
  10725. mob = mob_db(mob_id);
  10726. switch ( script_getnum(st,3) ) {
  10727. case 0: script_pushstrcopy(st,mob->jname); break;
  10728. case 1: script_pushint(st,mob->lv); break;
  10729. case 2: script_pushint(st,mob->status.max_hp); break;
  10730. case 3: script_pushint(st,mob->base_exp); break;
  10731. case 4: script_pushint(st,mob->job_exp); break;
  10732. case 5: script_pushint(st,mob->status.rhw.atk); break;
  10733. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  10734. case 7: script_pushint(st,mob->status.def); break;
  10735. case 8: script_pushint(st,mob->status.mdef); break;
  10736. case 9: script_pushint(st,mob->status.str); break;
  10737. case 10: script_pushint(st,mob->status.agi); break;
  10738. case 11: script_pushint(st,mob->status.vit); break;
  10739. case 12: script_pushint(st,mob->status.int_); break;
  10740. case 13: script_pushint(st,mob->status.dex); break;
  10741. case 14: script_pushint(st,mob->status.luk); break;
  10742. case 15: script_pushint(st,mob->status.rhw.range); break;
  10743. case 16: script_pushint(st,mob->range2); break;
  10744. case 17: script_pushint(st,mob->range3); break;
  10745. case 18: script_pushint(st,mob->status.size); break;
  10746. case 19: script_pushint(st,mob->status.race); break;
  10747. case 20: script_pushint(st,mob->status.def_ele); break;
  10748. case 21: script_pushint(st,mob->status.mode); break;
  10749. default: script_pushint(st,-1); //wrong Index
  10750. }
  10751. return 0;
  10752. }
  10753. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  10754. {
  10755. TBL_PC *sd = NULL;
  10756. if(script_hasdata(st,2))
  10757. sd = map_nick2sd(script_getstr(st,2));
  10758. else
  10759. sd = script_rid2sd(st);
  10760. if(sd)
  10761. script_pushint(st,(sd->vender_id != 0));
  10762. else
  10763. script_pushint(st,0);
  10764. return 0;
  10765. }
  10766. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  10767. {
  10768. TBL_PC *sd = NULL;
  10769. if(script_hasdata(st,2))
  10770. sd = map_nick2sd(script_getstr(st,2));
  10771. else
  10772. sd = script_rid2sd(st);
  10773. if(sd)
  10774. script_pushint(st,(sd->chatID != 0));
  10775. else
  10776. script_pushint(st,0);
  10777. return 0;
  10778. }
  10779. int axtoi(const char *hexStg)
  10780. {
  10781. int n = 0; // position in string
  10782. int m = 0; // position in digit[] to shift
  10783. int count; // loop index
  10784. int intValue = 0; // integer value of hex string
  10785. int digit[11]; // hold values to convert
  10786. while (n < 10) {
  10787. if (hexStg[n]=='\0')
  10788. break;
  10789. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  10790. digit[n] = hexStg[n] & 0x0f; //convert to int
  10791. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  10792. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10793. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  10794. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10795. else break;
  10796. n++;
  10797. }
  10798. count = n;
  10799. m = n - 1;
  10800. n = 0;
  10801. while(n < count) {
  10802. // digit[n] is value of hex digit at position n
  10803. // (m << 2) is the number of positions to shift
  10804. // OR the bits into return value
  10805. intValue = intValue | (digit[n] << (m << 2));
  10806. m--; // adjust the position to set
  10807. n++; // next digit to process
  10808. }
  10809. return (intValue);
  10810. }
  10811. // [Lance] Hex string to integer converter
  10812. BUILDIN_FUNC(axtoi)
  10813. {
  10814. const char *hex = script_getstr(st,2);
  10815. script_pushint(st,axtoi(hex));
  10816. return 0;
  10817. }
  10818. // [zBuffer] List of player cont commands --->
  10819. BUILDIN_FUNC(rid2name)
  10820. {
  10821. struct block_list *bl = NULL;
  10822. int rid = script_getnum(st,2);
  10823. if((bl = map_id2bl(rid)))
  10824. {
  10825. switch(bl->type) {
  10826. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  10827. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  10828. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  10829. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  10830. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  10831. default:
  10832. ShowError("buildin_rid2name: BL type unknown.\n");
  10833. script_pushconststr(st,"");
  10834. break;
  10835. }
  10836. } else {
  10837. ShowError("buildin_rid2name: invalid RID\n");
  10838. script_pushconststr(st,"(null)");
  10839. }
  10840. return 0;
  10841. }
  10842. BUILDIN_FUNC(pcblockmove)
  10843. {
  10844. int id, flag;
  10845. TBL_PC *sd = NULL;
  10846. id = script_getnum(st,2);
  10847. flag = script_getnum(st,3);
  10848. if(id)
  10849. sd = map_id2sd(id);
  10850. else
  10851. sd = script_rid2sd(st);
  10852. if(sd)
  10853. sd->state.blockedmove = flag > 0;
  10854. return 0;
  10855. }
  10856. BUILDIN_FUNC(pcfollow)
  10857. {
  10858. int id, targetid;
  10859. TBL_PC *sd = NULL;
  10860. id = script_getnum(st,2);
  10861. targetid = script_getnum(st,3);
  10862. if(id)
  10863. sd = map_id2sd(id);
  10864. else
  10865. sd = script_rid2sd(st);
  10866. if(sd)
  10867. pc_follow(sd, targetid);
  10868. return 0;
  10869. }
  10870. BUILDIN_FUNC(pcstopfollow)
  10871. {
  10872. int id;
  10873. TBL_PC *sd = NULL;
  10874. id = script_getnum(st,2);
  10875. if(id)
  10876. sd = map_id2sd(id);
  10877. else
  10878. sd = script_rid2sd(st);
  10879. if(sd)
  10880. pc_stop_following(sd);
  10881. return 0;
  10882. }
  10883. // <--- [zBuffer] List of player cont commands
  10884. // [zBuffer] List of mob control commands --->
  10885. //## TODO always return if the request/whatever was successfull [FlavioJS]
  10886. BUILDIN_FUNC(mobspawn)
  10887. {
  10888. int class_,x,y,id;
  10889. const char *str,*map;
  10890. // Who?
  10891. str =script_getstr(st,2);
  10892. // What?
  10893. class_ =script_getnum(st,3);
  10894. // Where?
  10895. map =script_getstr(st,4);
  10896. x =script_getnum(st,5);
  10897. y =script_getnum(st,6);
  10898. id = mob_once_spawn(map_id2sd(st->rid),map_mapname2mapid(map),x,y,str,class_,1,"");
  10899. script_pushint(st,id);
  10900. return 0;
  10901. }
  10902. BUILDIN_FUNC(mobremove)
  10903. {
  10904. int id;
  10905. struct block_list *bl = NULL;
  10906. id = script_getnum(st,2);
  10907. bl = map_id2bl(id);
  10908. if (bl && bl->type == BL_MOB)
  10909. unit_free(bl,0);
  10910. return 0;
  10911. }
  10912. BUILDIN_FUNC(getmobdata)
  10913. {
  10914. int num, id;
  10915. char *name;
  10916. struct mob_data *md = NULL;
  10917. TBL_PC *sd = st->rid?map_id2sd(st->rid):NULL;
  10918. id = script_getnum(st,2);
  10919. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  10920. ShowWarning("buildin_getmobdata: Error in argument!\n");
  10921. return -1;
  10922. }
  10923. num=st->stack->stack_data[st->start+3].u.num;
  10924. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10925. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  10926. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  10927. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  10928. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  10929. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  10930. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  10931. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  10932. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  10933. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  10934. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  10935. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  10936. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  10937. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  10938. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  10939. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  10940. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  10941. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  10942. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  10943. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  10944. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  10945. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  10946. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  10947. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  10948. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  10949. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  10950. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  10951. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  10952. return 0;
  10953. }
  10954. /// Changes the data of a mob
  10955. ///
  10956. /// setmobdata <mob unit id>,<type>,<value>;
  10957. BUILDIN_FUNC(setmobdata)
  10958. {
  10959. struct block_list* mob_bl;
  10960. mob_bl = map_id2bl(script_getnum(st,2));
  10961. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  10962. {
  10963. TBL_MOB* md = (TBL_MOB*)mob_bl;
  10964. int type;
  10965. int value;
  10966. type = script_getnum(st,3);
  10967. value = script_getnum(st,4);
  10968. switch( type )
  10969. {
  10970. case 0: md->class_ = (short)value; break;
  10971. case 1: md->level = (unsigned short)value; break;
  10972. case 2: md->status.hp = (unsigned int)value; break;
  10973. case 3: md->status.max_hp = (unsigned int)value; break;
  10974. case 4: md->master_id = value; break;
  10975. case 5: md->bl.m = (short)value; break;
  10976. case 6: md->bl.x = (short)value; break;
  10977. case 7: md->bl.y = (short)value; break;
  10978. case 8: md->status.speed = (unsigned short)value; break;
  10979. case 9: md->status.mode = (unsigned short)value; break;
  10980. case 10: md->special_state.ai = (unsigned int)value; break;
  10981. case 11: md->sc.option = (unsigned short)value; break;
  10982. case 12: md->vd->sex = (char)value; break;
  10983. case 13: md->vd->class_ = (unsigned short)value; break;
  10984. case 14: md->vd->hair_style = (unsigned short)value; break;
  10985. case 15: md->vd->hair_color = (unsigned short)value; break;
  10986. case 16: md->vd->head_bottom = (unsigned short)value; break;
  10987. case 17: md->vd->head_mid = (unsigned short)value; break;
  10988. case 18: md->vd->head_top = (unsigned short)value; break;
  10989. case 19: md->vd->cloth_color = (unsigned short)value; break;
  10990. case 20: md->vd->shield = (unsigned short)value; break;
  10991. case 21: md->vd->weapon = (unsigned short)value; break;
  10992. case 22: md->vd->shield = (unsigned short)value; break;
  10993. case 23: md->ud.dir = (unsigned char)value; break;
  10994. case 24: md->state.killer = value > 0 ? 1 : 0; break;
  10995. case 25: md->callback_flag = (short)value; break;
  10996. case 26: md->state.no_random_walk = value > 0 ? 1 : 0; break;
  10997. default:
  10998. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  10999. return 1;
  11000. }
  11001. }
  11002. return 0;
  11003. }
  11004. /// Makes the mob assist the target unit as a slave
  11005. ///
  11006. /// mobassist <mob unit id>,"<player name>";
  11007. /// mobassist <mob unit id>,<target id>;
  11008. BUILDIN_FUNC(mobassist)
  11009. {
  11010. struct block_list* mob_bl;
  11011. // get mob
  11012. mob_bl = map_id2bl(script_getnum(st,2));
  11013. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11014. {
  11015. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11016. struct block_list* target_bl = NULL;
  11017. struct script_data* data;
  11018. // get target
  11019. data = script_getdata(st, 3);
  11020. get_val(st, data);
  11021. if( data_isstring(data) )
  11022. {
  11023. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11024. if( sd != NULL )
  11025. target_bl = &sd->bl;
  11026. }
  11027. if( target_bl == NULL )
  11028. target_bl = map_id2bl(conv_num(st, data));
  11029. // set mob as slave
  11030. if( target_bl != NULL )
  11031. {
  11032. struct unit_data* ud;
  11033. md->master_id = target_bl->id;
  11034. md->state.killer = 1;
  11035. mob_convertslave(md);
  11036. ud = unit_bl2ud(mob_bl);
  11037. if( ud != NULL )
  11038. {
  11039. if( ud->target != 0 )
  11040. md->target_id = ud->target;
  11041. else if( ud->skilltarget != 0 )
  11042. md->target_id = ud->skilltarget;
  11043. if( md->target_id != 0 )
  11044. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  11045. }
  11046. }
  11047. }
  11048. return 0;
  11049. }
  11050. /// Attaches the current npc or the target npc to the mob unit
  11051. ///
  11052. /// mobattach <mob unit id>{,"<npc name>"};
  11053. BUILDIN_FUNC(mobattach)
  11054. {
  11055. struct block_list* mob_bl;
  11056. mob_bl = map_id2bl(script_getnum(st,2));
  11057. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11058. {
  11059. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11060. TBL_NPC* nd = NULL;
  11061. if( script_hasdata(st,3) )
  11062. nd = npc_name2id(script_getstr(st, 3));
  11063. else
  11064. {
  11065. struct block_list* npc_bl = map_id2bl(st->oid);
  11066. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  11067. nd = (TBL_NPC*)npc_bl;
  11068. }
  11069. if( nd != NULL )
  11070. md->nd = nd;
  11071. }
  11072. return 0;
  11073. }
  11074. /// Makes the unit walk to target position or map
  11075. /// Returns if it was successfull
  11076. ///
  11077. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11078. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11079. BUILDIN_FUNC(unitwalk)
  11080. {
  11081. struct block_list* bl;
  11082. bl = map_id2bl(script_getnum(st,2));
  11083. if( bl == NULL )
  11084. {
  11085. script_pushint(st, 0);
  11086. }
  11087. else if( script_hasdata(st,4) )
  11088. {
  11089. int x = script_getnum(st,3);
  11090. int y = script_getnum(st,4);
  11091. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11092. }
  11093. else
  11094. {
  11095. int map_id = script_getnum(st,3);
  11096. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11097. }
  11098. return 0;
  11099. }
  11100. /// Kills the unit
  11101. ///
  11102. /// unitkill <unit_id>;
  11103. BUILDIN_FUNC(unitkill)
  11104. {
  11105. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11106. if( bl != NULL )
  11107. status_kill(bl);
  11108. return 0;
  11109. }
  11110. /// Warps the unit to the target position in the target map
  11111. /// Returns if it was successfull
  11112. ///
  11113. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11114. BUILDIN_FUNC(unitwarp)
  11115. {
  11116. int unit_id;
  11117. int map;
  11118. short x;
  11119. short y;
  11120. struct block_list* bl;
  11121. unit_id = script_getnum(st,2);
  11122. map = map_mapname2mapid(script_getstr(st, 3));
  11123. x = (short)script_getnum(st,4);
  11124. y = (short)script_getnum(st,5);
  11125. bl = map_id2bl(unit_id);
  11126. if( map >= 0 && bl != NULL )
  11127. script_pushint(st, unit_warp(bl,map,x,y,0));
  11128. else
  11129. script_pushint(st, 0);
  11130. return 0;
  11131. }
  11132. /// Makes the unit attack the target.
  11133. /// If the unit is a player and <action type> is not 0, it does a continuous
  11134. /// attack instead of a single attack.
  11135. /// Returns if the request was successfull.
  11136. ///
  11137. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11138. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11139. BUILDIN_FUNC(unitattack)
  11140. {
  11141. struct block_list* unit_bl;
  11142. struct block_list* target_bl = NULL;
  11143. struct script_data* data;
  11144. int actiontype = 0;
  11145. // get unit
  11146. unit_bl = map_id2bl(script_getnum(st,2));
  11147. if( unit_bl == NULL ) {
  11148. script_pushint(st, 0);
  11149. return 0;
  11150. }
  11151. data = script_getdata(st, 3);
  11152. get_val(st, data);
  11153. if( data_isstring(data) )
  11154. {
  11155. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11156. if( sd != NULL )
  11157. target_bl = &sd->bl;
  11158. } else
  11159. target_bl = map_id2bl(conv_num(st, data));
  11160. // request the attack
  11161. if( target_bl == NULL )
  11162. {
  11163. script_pushint(st, 0);
  11164. return 0;
  11165. }
  11166. // get actiontype
  11167. if( script_hasdata(st,4) )
  11168. actiontype = script_getnum(st,4);
  11169. switch( unit_bl->type )
  11170. {
  11171. case BL_PC:
  11172. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11173. script_pushint(st, 1);
  11174. return 0;
  11175. case BL_MOB:
  11176. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11177. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11178. break;
  11179. case BL_PET:
  11180. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11181. break;
  11182. default:
  11183. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11184. script_pushint(st, 0);
  11185. return 1;
  11186. }
  11187. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11188. return 0;
  11189. }
  11190. /// Makes the unit stop attacking and moving
  11191. ///
  11192. /// unitstop <unit_id>;
  11193. BUILDIN_FUNC(unitstop)
  11194. {
  11195. int unit_id;
  11196. struct block_list* bl;
  11197. unit_id = script_getnum(st,2);
  11198. bl = map_id2bl(unit_id);
  11199. if( bl != NULL )
  11200. {
  11201. unit_stop_attack(bl);
  11202. unit_stop_walking(bl,0);
  11203. if( bl->type == BL_MOB )
  11204. ((TBL_MOB*)bl)->target_id = 0;
  11205. }
  11206. return 0;
  11207. }
  11208. /// Makes the unit say the message
  11209. ///
  11210. /// unittalk <unit_id>,"<message>";
  11211. BUILDIN_FUNC(unittalk)
  11212. {
  11213. int unit_id;
  11214. const char* message;
  11215. struct block_list* bl;
  11216. unit_id = script_getnum(st,2);
  11217. message = script_getstr(st, 3);
  11218. bl = map_id2bl(unit_id);
  11219. if( bl != NULL )
  11220. {
  11221. struct StringBuf sbuf;
  11222. StringBuf_Init(&sbuf);
  11223. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11224. clif_message(bl, StringBuf_Value(&sbuf));
  11225. if( bl->type == BL_PC )
  11226. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11227. StringBuf_Destroy(&sbuf);
  11228. }
  11229. return 0;
  11230. }
  11231. /// Makes the unit do an emotion
  11232. ///
  11233. /// unitemote <unit_id>,<emotion>;
  11234. ///
  11235. /// @see e_* in const.txt
  11236. BUILDIN_FUNC(unitemote)
  11237. {
  11238. int unit_id;
  11239. int emotion;
  11240. struct block_list* bl;
  11241. unit_id = script_getnum(st,2);
  11242. emotion = script_getnum(st,3);
  11243. bl = map_id2bl(unit_id);
  11244. if( bl != NULL )
  11245. clif_emotion(bl, emotion);
  11246. return 0;
  11247. }
  11248. /// Makes the unit cast the skill on the target or self if no target is specified
  11249. ///
  11250. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11251. BUILDIN_FUNC(unitskilluseid)
  11252. {
  11253. int unit_id;
  11254. int skill_id;
  11255. int skill_lv;
  11256. int target_id;
  11257. struct block_list* bl;
  11258. unit_id = script_getnum(st,2);
  11259. skill_id = script_getnum(st,3);
  11260. skill_lv = script_getnum(st,4);
  11261. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11262. bl = map_id2bl(unit_id);
  11263. if( bl != NULL )
  11264. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11265. return 0;
  11266. }
  11267. /// Makes the unit cast the skill on the target position.
  11268. ///
  11269. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11270. BUILDIN_FUNC(unitskillusepos)
  11271. {
  11272. int unit_id;
  11273. int skill_id;
  11274. int skill_lv;
  11275. int skill_x;
  11276. int skill_y;
  11277. struct block_list* bl;
  11278. unit_id = script_getnum(st,2);
  11279. skill_id = script_getnum(st,3);
  11280. skill_lv = script_getnum(st,4);
  11281. skill_x = script_getnum(st,5);
  11282. skill_y = script_getnum(st,6);
  11283. bl = map_id2bl(unit_id);
  11284. if( bl != NULL )
  11285. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11286. return 0;
  11287. }
  11288. // <--- [zBuffer] List of mob control commands
  11289. /// Pauses the execution of the script, detaching the player
  11290. ///
  11291. /// sleep <mili seconds>;
  11292. BUILDIN_FUNC(sleep)
  11293. {
  11294. int ticks;
  11295. TBL_PC* sd;
  11296. ticks = script_getnum(st,2);
  11297. sd = map_id2sd(st->rid);
  11298. // detach the player
  11299. if( sd && sd->npc_id == st->oid )
  11300. {
  11301. sd->npc_id = 0;
  11302. }
  11303. st->rid = 0;
  11304. if( ticks <= 0 )
  11305. {// do nothing
  11306. }
  11307. else if( st->sleep.tick == 0 )
  11308. {// sleep for the target amount of time
  11309. st->state = RERUNLINE;
  11310. st->sleep.tick = ticks;
  11311. }
  11312. else
  11313. {// sleep time is over
  11314. st->sleep.tick = 0;
  11315. }
  11316. return 0;
  11317. }
  11318. /// Pauses the execution of the script, keeping the player attached
  11319. /// Returns if a player is still attached
  11320. ///
  11321. /// sleep2(<mili secconds>) -> <bool>
  11322. BUILDIN_FUNC(sleep2)
  11323. {
  11324. int ticks;
  11325. ticks = script_getnum(st,2);
  11326. if( ticks <= 0 )
  11327. {// do nothing
  11328. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11329. }
  11330. else if( !st->sleep.tick )
  11331. {// sleep for the target amount of time
  11332. st->state = RERUNLINE;
  11333. st->sleep.tick = ticks;
  11334. }
  11335. else
  11336. {// sleep time is over
  11337. st->sleep.tick = 0;
  11338. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11339. }
  11340. return 0;
  11341. }
  11342. /// Awakes all the sleep timers of the target npc
  11343. ///
  11344. /// awake "<npc name>";
  11345. BUILDIN_FUNC(awake)
  11346. {
  11347. struct npc_data* nd;
  11348. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11349. nd = npc_name2id(script_getstr(st, 2));
  11350. if( nd == NULL )
  11351. return 0;
  11352. while( node )
  11353. {
  11354. if( (int)node->key == nd->bl.id )
  11355. {// sleep timer for the npc
  11356. struct script_state* tst = (struct script_state*)node->data;
  11357. TBL_PC* sd = map_id2sd(tst->rid);
  11358. if( tst->sleep.timer == -1 )
  11359. {// already awake ???
  11360. node = node->next;
  11361. continue;
  11362. }
  11363. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11364. {// char not online anymore / another char of the same account is online - Cancel execution
  11365. tst->state = END;
  11366. tst->rid = 0;
  11367. }
  11368. delete_timer(tst->sleep.timer, run_script_timer);
  11369. node = script_erase_sleepdb(node);
  11370. tst->sleep.timer = -1;
  11371. tst->sleep.tick = 0;
  11372. run_script_main(tst);
  11373. }
  11374. else
  11375. {
  11376. node = node->next;
  11377. }
  11378. }
  11379. return 0;
  11380. }
  11381. /// Returns a reference to a variable of the target NPC.
  11382. /// Returns 0 if an error occurs.
  11383. ///
  11384. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11385. BUILDIN_FUNC(getvariableofnpc)
  11386. {
  11387. struct script_data* data;
  11388. char* name;
  11389. struct npc_data* nd;
  11390. data = script_getdata(st,2);
  11391. if( !data_isreference(data) )
  11392. {// Not a reference (aka varaible name)
  11393. ShowError("script:getvariableofnpc: not a variable\n");
  11394. script_reportdata(data);
  11395. script_pushnil(st);
  11396. st->state = END;
  11397. return 1;
  11398. }
  11399. name = reference_getname(data);
  11400. if( *name != '.' || name[1] == '@' )
  11401. {// not a npc variable
  11402. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11403. script_reportdata(data);
  11404. script_pushnil(st);
  11405. st->state = END;
  11406. return 1;
  11407. }
  11408. nd = npc_name2id(script_getstr(st,3));
  11409. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11410. {// NPC not found or has no script
  11411. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11412. script_pushnil(st);
  11413. st->state = END;
  11414. return 1;
  11415. }
  11416. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11417. return 0;
  11418. }
  11419. /// Opens a warp portal.
  11420. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11421. ///
  11422. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11423. ///
  11424. /// @author blackhole89
  11425. BUILDIN_FUNC(warpportal)
  11426. {
  11427. int spx;
  11428. int spy;
  11429. unsigned short mapindex;
  11430. int tpx;
  11431. int tpy;
  11432. struct skill_unit_group* group;
  11433. struct block_list* bl;
  11434. bl = map_id2bl(st->oid);
  11435. if( bl == NULL )
  11436. {
  11437. ShowError("script:warpportal: npc is needed\n");
  11438. return 1;
  11439. }
  11440. spx = script_getnum(st,2);
  11441. spy = script_getnum(st,3);
  11442. mapindex = mapindex_name2id(script_getstr(st, 4));
  11443. tpx = script_getnum(st,5);
  11444. tpy = script_getnum(st,6);
  11445. if( mapindex == 0 )
  11446. return 0;// map not found
  11447. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 1);
  11448. if( group == NULL )
  11449. return 0;// failed
  11450. group->val2 = (tpx<<16) | tpy;
  11451. group->val3 = mapindex;
  11452. return 0;
  11453. }
  11454. BUILDIN_FUNC(openmail)
  11455. {
  11456. TBL_PC* sd;
  11457. sd = script_rid2sd(st);
  11458. if( sd == NULL )
  11459. return 0;
  11460. #ifndef TXT_ONLY
  11461. mail_openmail(sd);
  11462. #endif
  11463. return 0;
  11464. }
  11465. /// Modifies flags of cells in the specified area.
  11466. ///
  11467. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11468. ///
  11469. /// @see cell_* constants in const.txt for the types
  11470. BUILDIN_FUNC(setcell)
  11471. {
  11472. int m = map_mapname2mapid(script_getstr(st,2));
  11473. int x1 = script_getnum(st,3);
  11474. int y1 = script_getnum(st,4);
  11475. int x2 = script_getnum(st,5);
  11476. int y2 = script_getnum(st,6);
  11477. cell_t type = (cell_t)script_getnum(st,7);
  11478. bool flag = (bool)script_getnum(st,8);
  11479. int x,y;
  11480. if( x1 > x2 ) swap(x1,x2);
  11481. if( y1 > y2 ) swap(y1,y2);
  11482. for( y = y1; y <= y2; ++y )
  11483. for( x = x1; x <= x2; ++x )
  11484. map_setcell(m, x, y, type, flag);
  11485. return 0;
  11486. }
  11487. // declarations that were supposed to be exported from npc_chat.c
  11488. #ifdef PCRE_SUPPORT
  11489. BUILDIN_FUNC(defpattern);
  11490. BUILDIN_FUNC(activatepset);
  11491. BUILDIN_FUNC(deactivatepset);
  11492. BUILDIN_FUNC(deletepset);
  11493. #endif
  11494. struct script_function buildin_func[] = {
  11495. // NPC interaction
  11496. BUILDIN_DEF(mes,"s"),
  11497. BUILDIN_DEF(next,""),
  11498. BUILDIN_DEF(close,""),
  11499. BUILDIN_DEF(close2,""),
  11500. BUILDIN_DEF(menu,"sl*"),
  11501. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  11502. BUILDIN_DEF(prompt,"s*"),
  11503. //
  11504. BUILDIN_DEF(goto,"l"),
  11505. BUILDIN_DEF(callsub,"i*"),
  11506. BUILDIN_DEF(callfunc,"s*"),
  11507. BUILDIN_DEF(return,"?"),
  11508. BUILDIN_DEF(getarg,"i?"),
  11509. BUILDIN_DEF(jobchange,"i*"),
  11510. BUILDIN_DEF(jobname,"i"),
  11511. BUILDIN_DEF(input,"v"),
  11512. BUILDIN_DEF(warp,"sii"),
  11513. BUILDIN_DEF(areawarp,"siiiisii"),
  11514. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  11515. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  11516. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  11517. BUILDIN_DEF(setlook,"ii"),
  11518. BUILDIN_DEF(set,"ii"),
  11519. BUILDIN_DEF(setarray,"rv*"),
  11520. BUILDIN_DEF(cleararray,"rvi"),
  11521. BUILDIN_DEF(copyarray,"rri"),
  11522. BUILDIN_DEF(getarraysize,"r"),
  11523. BUILDIN_DEF(deletearray,"r?"),
  11524. BUILDIN_DEF(getelementofarray,"ri"),
  11525. BUILDIN_DEF(getitem,"vi?"),
  11526. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  11527. BUILDIN_DEF(getnameditem,"is"),
  11528. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  11529. BUILDIN_DEF(makeitem,"iisii"),
  11530. BUILDIN_DEF(delitem,"ii"),
  11531. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  11532. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  11533. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  11534. BUILDIN_DEF(cutin,"si"),
  11535. BUILDIN_DEF(viewpoint,"iiiii"),
  11536. BUILDIN_DEF(heal,"ii"),
  11537. BUILDIN_DEF(itemheal,"ii"),
  11538. BUILDIN_DEF(percentheal,"ii"),
  11539. BUILDIN_DEF(rand,"i?"),
  11540. BUILDIN_DEF(countitem,"i"),
  11541. BUILDIN_DEF(countitem2,"iiiiiiii"),
  11542. BUILDIN_DEF(checkweight,"ii"),
  11543. BUILDIN_DEF(readparam,"i*"),
  11544. BUILDIN_DEF(getcharid,"i*"),
  11545. BUILDIN_DEF(getpartyname,"i"),
  11546. BUILDIN_DEF(getpartymember,"i*"),
  11547. BUILDIN_DEF(getpartyleader,"i?"),
  11548. BUILDIN_DEF(getguildname,"i"),
  11549. BUILDIN_DEF(getguildmaster,"i"),
  11550. BUILDIN_DEF(getguildmasterid,"i"),
  11551. BUILDIN_DEF(strcharinfo,"i"),
  11552. BUILDIN_DEF(strnpcinfo,"i"),
  11553. BUILDIN_DEF(getequipid,"i"),
  11554. BUILDIN_DEF(getequipname,"i"),
  11555. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  11556. BUILDIN_DEF(repair,"i"), // [Valaris]
  11557. BUILDIN_DEF(getequipisequiped,"i"),
  11558. BUILDIN_DEF(getequipisenableref,"i"),
  11559. BUILDIN_DEF(getequipisidentify,"i"),
  11560. BUILDIN_DEF(getequiprefinerycnt,"i"),
  11561. BUILDIN_DEF(getequipweaponlv,"i"),
  11562. BUILDIN_DEF(getequippercentrefinery,"i"),
  11563. BUILDIN_DEF(successrefitem,"i"),
  11564. BUILDIN_DEF(failedrefitem,"i"),
  11565. BUILDIN_DEF(statusup,"i"),
  11566. BUILDIN_DEF(statusup2,"ii"),
  11567. BUILDIN_DEF(bonus,"ii"),
  11568. BUILDIN_DEF2(bonus,"bonus2","iii"),
  11569. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  11570. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  11571. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  11572. BUILDIN_DEF(bonusautoscript,"si?"),
  11573. BUILDIN_DEF(bonusautoscript2,"si?"),
  11574. BUILDIN_DEF(skill,"ii?"),
  11575. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  11576. BUILDIN_DEF(guildskill,"ii"),
  11577. BUILDIN_DEF(getskilllv,"i"),
  11578. BUILDIN_DEF(getgdskilllv,"ii"),
  11579. BUILDIN_DEF(basicskillcheck,""),
  11580. BUILDIN_DEF(getgmlevel,""),
  11581. BUILDIN_DEF(end,""),
  11582. BUILDIN_DEF(checkoption,"i"),
  11583. BUILDIN_DEF(setoption,"i?"),
  11584. BUILDIN_DEF(setcart,"?"),
  11585. BUILDIN_DEF(checkcart,""),
  11586. BUILDIN_DEF(setfalcon,"?"),
  11587. BUILDIN_DEF(checkfalcon,""),
  11588. BUILDIN_DEF(setriding,"?"),
  11589. BUILDIN_DEF(checkriding,""),
  11590. BUILDIN_DEF2(savepoint,"save","sii"),
  11591. BUILDIN_DEF(savepoint,"sii"),
  11592. BUILDIN_DEF(gettimetick,"i"),
  11593. BUILDIN_DEF(gettime,"i"),
  11594. BUILDIN_DEF(gettimestr,"si"),
  11595. BUILDIN_DEF(openstorage,""),
  11596. BUILDIN_DEF(guildopenstorage,"*"),
  11597. BUILDIN_DEF(itemskill,"ii"),
  11598. BUILDIN_DEF(produce,"i"),
  11599. BUILDIN_DEF(monster,"siisii*"),
  11600. BUILDIN_DEF(areamonster,"siiiisii*"),
  11601. BUILDIN_DEF(killmonster,"ss"),
  11602. BUILDIN_DEF(killmonsterall,"s"),
  11603. BUILDIN_DEF(clone,"siisi*"),
  11604. BUILDIN_DEF(doevent,"s"),
  11605. BUILDIN_DEF(donpcevent,"s"),
  11606. BUILDIN_DEF(cmdothernpc,"ss"),
  11607. BUILDIN_DEF(addtimer,"is"),
  11608. BUILDIN_DEF(deltimer,"s"),
  11609. BUILDIN_DEF(addtimercount,"si"),
  11610. BUILDIN_DEF(initnpctimer,"??"),
  11611. BUILDIN_DEF(stopnpctimer,"??"),
  11612. BUILDIN_DEF(startnpctimer,"??"),
  11613. BUILDIN_DEF(setnpctimer,"i?"),
  11614. BUILDIN_DEF(getnpctimer,"i?"),
  11615. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  11616. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  11617. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  11618. BUILDIN_DEF(announce,"si*"),
  11619. BUILDIN_DEF(mapannounce,"ssi*"),
  11620. BUILDIN_DEF(areaannounce,"siiiisi*"),
  11621. BUILDIN_DEF(getusers,"i"),
  11622. BUILDIN_DEF(getmapguildusers,"si"),
  11623. BUILDIN_DEF(getmapusers,"s"),
  11624. BUILDIN_DEF(getareausers,"siiii"),
  11625. BUILDIN_DEF(getareadropitem,"siiiii"),
  11626. BUILDIN_DEF(enablenpc,"s"),
  11627. BUILDIN_DEF(disablenpc,"s"),
  11628. BUILDIN_DEF(hideoffnpc,"s"),
  11629. BUILDIN_DEF(hideonnpc,"s"),
  11630. BUILDIN_DEF(sc_start,"iii?"),
  11631. BUILDIN_DEF(sc_start2,"iiii?"),
  11632. BUILDIN_DEF(sc_start4,"iiiiii?"),
  11633. BUILDIN_DEF(sc_end,"i?"),
  11634. BUILDIN_DEF(getscrate,"ii*"),
  11635. BUILDIN_DEF(debugmes,"s"),
  11636. BUILDIN_DEF2(catchpet,"pet","i"),
  11637. BUILDIN_DEF2(birthpet,"bpet",""),
  11638. BUILDIN_DEF(resetlvl,"i"),
  11639. BUILDIN_DEF(resetstatus,""),
  11640. BUILDIN_DEF(resetskill,""),
  11641. BUILDIN_DEF(skillpointcount,""),
  11642. BUILDIN_DEF(changebase,"i"),
  11643. BUILDIN_DEF(changesex,""),
  11644. BUILDIN_DEF(waitingroom,"si??"),
  11645. BUILDIN_DEF(delwaitingroom,"?"),
  11646. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  11647. BUILDIN_DEF(enablewaitingroomevent,"?"),
  11648. BUILDIN_DEF(disablewaitingroomevent,"?"),
  11649. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  11650. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  11651. BUILDIN_DEF(getwaitingroomstate,"i?"),
  11652. BUILDIN_DEF(warpwaitingpc,"sii?"),
  11653. BUILDIN_DEF(attachrid,"i"),
  11654. BUILDIN_DEF(detachrid,""),
  11655. BUILDIN_DEF(isloggedin,"i?"),
  11656. BUILDIN_DEF(setmapflagnosave,"ssii"),
  11657. BUILDIN_DEF(setmapflag,"si*"),
  11658. BUILDIN_DEF(removemapflag,"si"),
  11659. BUILDIN_DEF(pvpon,"s"),
  11660. BUILDIN_DEF(pvpoff,"s"),
  11661. BUILDIN_DEF(gvgon,"s"),
  11662. BUILDIN_DEF(gvgoff,"s"),
  11663. BUILDIN_DEF(emotion,"i*"),
  11664. BUILDIN_DEF(maprespawnguildid,"sii"),
  11665. BUILDIN_DEF(agitstart,""), // <Agit>
  11666. BUILDIN_DEF(agitend,""),
  11667. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  11668. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  11669. BUILDIN_DEF(getcastlename,"s"),
  11670. BUILDIN_DEF(getcastledata,"si*"),
  11671. BUILDIN_DEF(setcastledata,"sii"),
  11672. BUILDIN_DEF(requestguildinfo,"i*"),
  11673. BUILDIN_DEF(getequipcardcnt,"i"),
  11674. BUILDIN_DEF(successremovecards,"i"),
  11675. BUILDIN_DEF(failedremovecards,"ii"),
  11676. BUILDIN_DEF(marriage,"s"),
  11677. BUILDIN_DEF2(wedding_effect,"wedding",""),
  11678. BUILDIN_DEF(divorce,""),
  11679. BUILDIN_DEF(ispartneron,""),
  11680. BUILDIN_DEF(getpartnerid,""),
  11681. BUILDIN_DEF(getchildid,""),
  11682. BUILDIN_DEF(getmotherid,""),
  11683. BUILDIN_DEF(getfatherid,""),
  11684. BUILDIN_DEF(warppartner,"sii"),
  11685. BUILDIN_DEF(getitemname,"i"),
  11686. BUILDIN_DEF(getitemslots,"i"),
  11687. BUILDIN_DEF(makepet,"i"),
  11688. BUILDIN_DEF(getexp,"ii"),
  11689. BUILDIN_DEF(getinventorylist,""),
  11690. BUILDIN_DEF(getskilllist,""),
  11691. BUILDIN_DEF(clearitem,""),
  11692. BUILDIN_DEF(classchange,"ii"),
  11693. BUILDIN_DEF(misceffect,"i"),
  11694. BUILDIN_DEF(soundeffect,"si"),
  11695. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  11696. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  11697. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  11698. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  11699. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  11700. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  11701. BUILDIN_DEF(petloot,"i"), // [Valaris]
  11702. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  11703. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  11704. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  11705. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  11706. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  11707. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  11708. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  11709. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  11710. BUILDIN_DEF(nude,""), // nude command [Valaris]
  11711. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  11712. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  11713. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  11714. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  11715. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  11716. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  11717. BUILDIN_DEF(mobcount,"ss"),
  11718. BUILDIN_DEF(getlook,"i"),
  11719. BUILDIN_DEF(getsavepoint,"i"),
  11720. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  11721. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  11722. BUILDIN_DEF(npcstop,""), // [Valaris]
  11723. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  11724. BUILDIN_DEF(checkoption1,"i"),
  11725. BUILDIN_DEF(checkoption2,"i"),
  11726. BUILDIN_DEF(guildgetexp,"i"),
  11727. BUILDIN_DEF(guildchangegm,"is"),
  11728. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  11729. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  11730. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  11731. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  11732. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  11733. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  11734. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  11735. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  11736. BUILDIN_DEF(adopt,"sss"), // allows 2 parents to adopt a child
  11737. BUILDIN_DEF(night,""), // sets the server to night time
  11738. BUILDIN_DEF(day,""), // sets the server to day time
  11739. #ifdef PCRE_SUPPORT
  11740. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  11741. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  11742. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  11743. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  11744. #endif
  11745. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  11746. BUILDIN_DEF(getusersname,"*"),
  11747. BUILDIN_DEF(recovery,""),
  11748. BUILDIN_DEF(getpetinfo,"i"),
  11749. BUILDIN_DEF(gethominfo,"i"),
  11750. BUILDIN_DEF(checkequipedcard,"i"),
  11751. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  11752. BUILDIN_DEF(globalmes,"s*"),
  11753. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  11754. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  11755. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  11756. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  11757. BUILDIN_DEF(setnpcdisplay,"sv?"),
  11758. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  11759. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  11760. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  11761. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  11762. // [zBuffer] List of mathematics commands --->
  11763. BUILDIN_DEF(sqrt,"i"),
  11764. BUILDIN_DEF(pow,"ii"),
  11765. BUILDIN_DEF(distance,"iiii"),
  11766. BUILDIN_DEF(checkcell,"siii"),
  11767. // <--- [zBuffer] List of mathematics commands
  11768. // [zBuffer] List of dynamic var commands --->
  11769. BUILDIN_DEF(getd,"*"),
  11770. BUILDIN_DEF(setd,"*"),
  11771. // <--- [zBuffer] List of dynamic var commands
  11772. BUILDIN_DEF(petstat,"i"),
  11773. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  11774. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  11775. BUILDIN_DEF(npcshopadditem,"sii*"),
  11776. BUILDIN_DEF(npcshopdelitem,"si*"),
  11777. BUILDIN_DEF(npcshopattach,"s?"),
  11778. BUILDIN_DEF(equip,"i"),
  11779. BUILDIN_DEF(autoequip,"ii"),
  11780. BUILDIN_DEF(setbattleflag,"ss"),
  11781. BUILDIN_DEF(getbattleflag,"s"),
  11782. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  11783. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  11784. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  11785. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  11786. BUILDIN_DEF(axtoi,"s"),
  11787. BUILDIN_DEF(query_sql,"s*"),
  11788. BUILDIN_DEF(query_logsql,"s*"),
  11789. BUILDIN_DEF(escape_sql,"s"),
  11790. BUILDIN_DEF(atoi,"s"),
  11791. // [zBuffer] List of player cont commands --->
  11792. BUILDIN_DEF(rid2name,"i"),
  11793. BUILDIN_DEF(pcfollow,"ii"),
  11794. BUILDIN_DEF(pcstopfollow,"i"),
  11795. BUILDIN_DEF(pcblockmove,"ii"),
  11796. // <--- [zBuffer] List of player cont commands
  11797. // [zBuffer] List of mob control commands --->
  11798. BUILDIN_DEF(mobspawn,"*"),
  11799. BUILDIN_DEF(mobremove,"i"),
  11800. BUILDIN_DEF(getmobdata,"i*"),
  11801. BUILDIN_DEF(setmobdata,"iii"),
  11802. BUILDIN_DEF(mobassist,"i?"),
  11803. BUILDIN_DEF(mobattach,"i?"),
  11804. BUILDIN_DEF(unitwalk,"ii?"),
  11805. BUILDIN_DEF(unitkill,"i"),
  11806. BUILDIN_DEF(unitwarp,"isii"),
  11807. BUILDIN_DEF(unitattack,"iv?"),
  11808. BUILDIN_DEF(unitstop,"i"),
  11809. BUILDIN_DEF(unittalk,"is"),
  11810. BUILDIN_DEF(unitemote,"ii"),
  11811. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  11812. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  11813. // <--- [zBuffer] List of mob control commands
  11814. BUILDIN_DEF(sleep,"i"),
  11815. BUILDIN_DEF(sleep2,"i"),
  11816. BUILDIN_DEF(awake,"s"),
  11817. BUILDIN_DEF(getvariableofnpc,"rs"),
  11818. BUILDIN_DEF(warpportal,"iisii"),
  11819. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  11820. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  11821. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  11822. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  11823. BUILDIN_DEF(checkvending,"*"),
  11824. BUILDIN_DEF(checkchatting,"*"),
  11825. BUILDIN_DEF(openmail,""),
  11826. BUILDIN_DEF(setcell,"siiiiii"),
  11827. {NULL,NULL,NULL},
  11828. };