script.c 322 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299
  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. #include "../common/cbasetypes.h"
  8. #include "../common/socket.h"
  9. #include "../common/timer.h"
  10. #include "../common/malloc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/utils.h"
  16. #include "map.h"
  17. #include "clif.h"
  18. #include "chrif.h"
  19. #include "itemdb.h"
  20. #include "pc.h"
  21. #include "status.h"
  22. #include "storage.h"
  23. #include "mob.h"
  24. #include "npc.h"
  25. #include "pet.h"
  26. #include "mercenary.h" //[orn]
  27. #include "intif.h"
  28. #include "skill.h"
  29. #include "chat.h"
  30. #include "battle.h"
  31. #include "party.h"
  32. #include "guild.h"
  33. #include "atcommand.h"
  34. #include "charcommand.h"
  35. #include "log.h"
  36. #include "unit.h"
  37. #include "irc.h"
  38. #include "pet.h"
  39. #include "script.h"
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <math.h>
  44. #ifndef WIN32
  45. #include <sys/time.h>
  46. #endif
  47. #include <time.h>
  48. #include <setjmp.h>
  49. //
  50. // struct script_state* st;
  51. //
  52. /// Returns the script_data at the target index
  53. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  54. /// Returns if the stack contains data at the target index
  55. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  56. /// Returns the index of the last data in the stack
  57. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  58. /// Pushes an int into the stack
  59. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  60. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  61. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  62. /// Pushes a nil into the stack
  63. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  64. /// Pushes a copy of the data in the target index
  65. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  66. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  67. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  68. #define script_getref(st,val) ((st)->stack->stack_data[(st)->start+(val)].ref)
  69. // Note: "top" functions/defines use indexes relative to the top of the stack
  70. // -1 is the index of the data at the top
  71. /// Returns the script_data at the target index relative to the top of the stack
  72. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  73. /// Pushes a copy of the data in the target index relative to the top of the stack
  74. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  75. /// Removes the range of values [start,end[ relative to the top of the stack
  76. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  77. //
  78. // struct script_data* data;
  79. //
  80. /// Returns if the script data is a string
  81. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  82. /// Returns if the script data is an int
  83. #define data_isint(data) ( (data)->type == C_INT )
  84. /// Returns if the script data is a reference
  85. #define data_isreference(data) ( (data)->type == C_NAME )
  86. /// Returns if the script data is a label
  87. #define data_islabel(data) ( (data)->type == C_POS )
  88. /// Returns if the script data is an internal script function label
  89. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  90. /// Returns the name of the reference
  91. #define data_referencename(data) ( str_buf + str_data[(data)->u.num&0x00ffffff].str )
  92. #define FETCH(n, t) \
  93. if( script_hasdata(st,n) ) \
  94. (t)=script_getnum(st,n);
  95. #define SCRIPT_BLOCK_SIZE 512
  96. enum { LABEL_NEXTLINE=1,LABEL_START };
  97. static unsigned char * script_buf = NULL;
  98. static int script_pos,script_size;
  99. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  100. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  101. #define GETSTRING(off) (str_buf+(off))
  102. static char *str_buf;
  103. static int str_pos,str_size;
  104. static struct str_data_struct {
  105. int type;
  106. int str;
  107. int backpatch;
  108. int label;
  109. int (*func)(struct script_state *st);
  110. int val;
  111. int next;
  112. } *str_data = NULL;
  113. int str_num=LABEL_START,str_data_size;
  114. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  115. #define SCRIPT_HASH_SIZE 1021
  116. int str_hash[SCRIPT_HASH_SIZE];
  117. //#define SCRIPT_HASH_DJB2
  118. //#define SCRIPT_HASH_SDBM
  119. //#define SCRIPT_HASH_ELF
  120. //#define SCRIPT_HASH_PJW
  121. static struct dbt *mapreg_db=NULL;
  122. static struct dbt *mapregstr_db=NULL;
  123. static int mapreg_dirty=-1;
  124. char mapreg_txt[256]="save/mapreg.txt";
  125. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  126. static struct dbt *scriptlabel_db=NULL;
  127. static struct dbt *userfunc_db=NULL;
  128. static int parse_options=0;
  129. struct dbt* script_get_label_db(){ return scriptlabel_db; }
  130. struct dbt* script_get_userfunc_db(){ return userfunc_db; }
  131. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  132. struct Script_Config script_config;
  133. static jmp_buf error_jump;
  134. static char* error_msg;
  135. static const char* error_pos;
  136. static int error_report; // if the error should produce output
  137. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  138. // [Eoe / jA 1080, 1081, 1094, 1164]
  139. enum curly_type {
  140. TYPE_NULL = 0,
  141. TYPE_IF,
  142. TYPE_SWITCH,
  143. TYPE_WHILE,
  144. TYPE_FOR,
  145. TYPE_DO,
  146. TYPE_USERFUNC,
  147. TYPE_ARGLIST // function argument list
  148. };
  149. #define ARGLIST_UNDEFINED 0
  150. #define ARGLIST_NO_PAREN 1
  151. #define ARGLIST_PAREN 2
  152. static struct {
  153. struct {
  154. int type;
  155. int index;
  156. int count;
  157. int flag;
  158. struct linkdb_node *case_label;
  159. } curly[256]; // 右カッコの情報
  160. int curly_count; // 右カッコの数
  161. int index; // スクリプト内で使用した構文の数
  162. } syntax;
  163. const char* parse_curly_close(const char* p);
  164. const char* parse_syntax_close(const char* p);
  165. const char* parse_syntax_close_sub(const char* p,int* flag);
  166. const char* parse_syntax(const char* p);
  167. static int parse_syntax_for_flag = 0;
  168. extern int current_equip_item_index; //for New CARS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  169. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  170. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  171. int potion_target=0;
  172. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  173. // [zBuffer] SQL Mapreg Saving/Loading Database Declaration
  174. char mapregsql_db[32] = "mapreg";
  175. char mapregsql_db_varname[32] = "varname";
  176. char mapregsql_db_index[32] = "index";
  177. char mapregsql_db_value[32] = "value";
  178. char tmp_sql[65535];
  179. // --------------------------------------------------------
  180. #endif
  181. int get_com(unsigned char *script,int *pos);
  182. int get_num(unsigned char *script,int *pos);
  183. extern struct script_function {
  184. int (*func)(struct script_state *st);
  185. const char *name;
  186. const char *arg;
  187. } buildin_func[];
  188. static struct linkdb_node *sleep_db;
  189. #define not_server_variable(prefix) (prefix != '$' && prefix != '.')
  190. /*==========================================
  191. * ローカルプロトタイプ宣言 (必要な物のみ)
  192. *------------------------------------------*/
  193. const char* parse_subexpr(const char* p,int limit);
  194. void push_val(struct script_stack *stack,int type,int val);
  195. int run_func(struct script_state *st);
  196. int mapreg_setreg(int num,int val);
  197. int mapreg_setregstr(int num,const char *str);
  198. enum c_op {
  199. C_NOP, // end of script/no value (nil)
  200. C_POS,
  201. C_INT, // number
  202. C_PARAM, // parameter variable (see pc_readparam/pc_setparam)
  203. C_FUNC, // buildin function call
  204. C_STR, // string (free'd automatically)
  205. C_CONSTSTR, // string (not free'd)
  206. C_ARG, // start of argument list
  207. C_NAME,
  208. C_EOL, // end of line (extra stack values are cleared)
  209. C_RETINFO,
  210. C_USERFUNC, // internal script function
  211. C_USERFUNC_POS, // internal script function label
  212. // operators
  213. C_OP3, // a ? b : c
  214. C_LOR, // a || b
  215. C_LAND, // a && b
  216. C_LE, // a <= b
  217. C_LT, // a < b
  218. C_GE, // a >= b
  219. C_GT, // a > b
  220. C_EQ, // a == b
  221. C_NE, // a != b
  222. C_XOR, // a ^ b
  223. C_OR, // a | b
  224. C_AND, // a & b
  225. C_ADD, // a + b
  226. C_SUB, // a - b
  227. C_MUL, // a * b
  228. C_DIV, // a / b
  229. C_MOD, // a % b
  230. C_NEG, // - a
  231. C_LNOT, // ! a
  232. C_NOT, // ~ a
  233. C_R_SHIFT, // a >> b
  234. C_L_SHIFT // a << b
  235. };
  236. enum {
  237. MF_NOMEMO, //0
  238. MF_NOTELEPORT,
  239. MF_NOSAVE,
  240. MF_NOBRANCH,
  241. MF_NOPENALTY,
  242. MF_NOZENYPENALTY,
  243. MF_PVP,
  244. MF_PVP_NOPARTY,
  245. MF_PVP_NOGUILD,
  246. MF_GVG,
  247. MF_GVG_NOPARTY, //10
  248. MF_NOTRADE,
  249. MF_NOSKILL,
  250. MF_NOWARP,
  251. MF_PARTYLOCK,
  252. MF_NOICEWALL,
  253. MF_SNOW,
  254. MF_FOG,
  255. MF_SAKURA,
  256. MF_LEAVES,
  257. MF_RAIN, //20
  258. MF_INDOORS,
  259. MF_NOGO,
  260. MF_CLOUDS,
  261. MF_CLOUDS2,
  262. MF_FIREWORKS,
  263. MF_GVG_CASTLE,
  264. MF_GVG_DUNGEON,
  265. MF_NIGHTENABLED,
  266. MF_NOBASEEXP,
  267. MF_NOJOBEXP, //30
  268. MF_NOMOBLOOT,
  269. MF_NOMVPLOOT,
  270. MF_NORETURN,
  271. MF_NOWARPTO,
  272. MF_NIGHTMAREDROP,
  273. MF_RESTRICTED,
  274. MF_NOCOMMAND,
  275. MF_NODROP,
  276. MF_JEXP,
  277. MF_BEXP, //40
  278. MF_NOVENDING,
  279. MF_LOADEVENT,
  280. MF_NOCHAT,
  281. MF_NOEXPPENALTY,
  282. MF_GUILDLOCK
  283. };
  284. //Reports on the console the src of a script error.
  285. static void report_src(struct script_state *st)
  286. {
  287. struct block_list *bl;
  288. if (!st->oid) return; //Can't report source.
  289. bl = map_id2bl(st->oid);
  290. if (!bl) return;
  291. switch (bl->type) {
  292. case BL_NPC:
  293. if (bl->m >=0)
  294. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  295. else
  296. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  297. break;
  298. default:
  299. if (bl->m >=0)
  300. 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);
  301. else
  302. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  303. break;
  304. }
  305. }
  306. /*==========================================
  307. * エラーメッセージ出力
  308. *------------------------------------------*/
  309. static void disp_error_message2(const char *mes,const char *pos,int report)
  310. {
  311. error_msg = aStrdup(mes);
  312. error_pos = pos;
  313. error_report = report;
  314. longjmp( error_jump, 1 );
  315. }
  316. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  317. /// Checks event parameter validity
  318. static void check_event(struct script_state *st, const char *evt)
  319. {
  320. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  321. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  322. report_src(st);
  323. }
  324. }
  325. /*==========================================
  326. * 文字列のハッシュを計算
  327. *------------------------------------------*/
  328. #define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE)
  329. static unsigned int calc_hash2(const unsigned char *p)
  330. {
  331. #if defined(SCRIPT_HASH_DJB2)
  332. unsigned int h = 5381;
  333. while( *p ) // hash*33 + c
  334. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  335. return h;
  336. #elif defined(SCRIPT_HASH_SDBM)
  337. unsigned int h = 0;
  338. while( *p )
  339. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  340. return h;
  341. #elif defined(SCRIPT_HASH_ELF)
  342. unsigned int h = 0;
  343. unsigned int g;
  344. while( *p ){ // UNIX ELF hash
  345. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  346. if ( g = h & 0xF0000000 )
  347. h ^= g >> 24;
  348. h &= ~g;
  349. }
  350. return h;
  351. #elif defined(SCRIPT_HASH_PJW)
  352. unsigned int h = 0;
  353. unsigned int g;
  354. while( *p ){
  355. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  356. if ( (g=h&0xF0000000) ) {
  357. h ^= g>>24;
  358. h ^= g;
  359. }
  360. }
  361. return h;
  362. #else
  363. unsigned int h = 0;
  364. while( *p ){
  365. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  366. h+=(unsigned char)TOLOWER(*p++);
  367. }
  368. return h;
  369. #endif
  370. }
  371. /*==========================================
  372. * str_dataの中に名前があるか検索する
  373. *------------------------------------------*/
  374. // 既存のであれば番号、無ければ-1
  375. static int search_str(const char *p)
  376. {
  377. int i;
  378. i=str_hash[calc_hash(p)];
  379. while(i){
  380. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  381. return i;
  382. }
  383. i=str_data[i].next;
  384. }
  385. return -1;
  386. }
  387. /*==========================================
  388. * str_dataに名前を登録
  389. *------------------------------------------*/
  390. // 既存のであれば番号、無ければ登録して新規番号
  391. int add_str(const char* p)
  392. {
  393. int i;
  394. int len;
  395. i=calc_hash(p);
  396. if(str_hash[i]==0){
  397. str_hash[i]=str_num;
  398. } else {
  399. i=str_hash[i];
  400. for(;;){
  401. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  402. return i;
  403. }
  404. if(str_data[i].next==0)
  405. break;
  406. i=str_data[i].next;
  407. }
  408. str_data[i].next=str_num;
  409. }
  410. if(str_num>=str_data_size){
  411. str_data_size+=128;
  412. RECREATE(str_data,struct str_data_struct,str_data_size);
  413. memset(str_data + (str_data_size - 128), '\0', 128);
  414. }
  415. len=(int)strlen(p);
  416. while(str_pos+len+1>=str_size){
  417. str_size+=256;
  418. RECREATE(str_buf,char,str_size);
  419. memset(str_buf + (str_size - 256), '\0', 256);
  420. }
  421. memcpy(str_buf+str_pos,p,len+1);
  422. str_data[str_num].type=C_NOP;
  423. str_data[str_num].str=str_pos;
  424. str_data[str_num].next=0;
  425. str_data[str_num].func=NULL;
  426. str_data[str_num].backpatch=-1;
  427. str_data[str_num].label=-1;
  428. str_pos+=len+1;
  429. return str_num++;
  430. }
  431. /*==========================================
  432. * スクリプトバッファサイズの確認と拡張
  433. *------------------------------------------*/
  434. static void expand_script_buf(void)
  435. {
  436. script_size+=SCRIPT_BLOCK_SIZE;
  437. RECREATE(script_buf,unsigned char,script_size);
  438. }
  439. /*==========================================
  440. * スクリプトバッファに1バイト書き込む
  441. *------------------------------------------*/
  442. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  443. #if 0
  444. static void add_scriptb(int a)
  445. {
  446. expand_script_buf();
  447. script_buf[script_pos++]=a;
  448. }
  449. #endif
  450. /*==========================================
  451. * スクリプトバッファにデータタイプを書き込む
  452. *------------------------------------------*/
  453. static void add_scriptc(int a)
  454. {
  455. while(a>=0x40){
  456. add_scriptb((a&0x3f)|0x40);
  457. a=(a-0x40)>>6;
  458. }
  459. add_scriptb(a&0x3f);
  460. }
  461. /*==========================================
  462. * スクリプトバッファに整数を書き込む
  463. *------------------------------------------*/
  464. static void add_scripti(int a)
  465. {
  466. while(a>=0x40){
  467. add_scriptb(a|0xc0);
  468. a=(a-0x40)>>6;
  469. }
  470. add_scriptb(a|0x80);
  471. }
  472. /*==========================================
  473. * スクリプトバッファにラベル/変数/関数を書き込む
  474. *------------------------------------------*/
  475. // 最大16Mまで
  476. static void add_scriptl(int l)
  477. {
  478. int backpatch = str_data[l].backpatch;
  479. switch(str_data[l].type){
  480. case C_POS:
  481. case C_USERFUNC_POS:
  482. add_scriptc(C_POS);
  483. add_scriptb(str_data[l].label);
  484. add_scriptb(str_data[l].label>>8);
  485. add_scriptb(str_data[l].label>>16);
  486. break;
  487. case C_NOP:
  488. case C_USERFUNC:
  489. // ラベルの可能性があるのでbackpatch用データ埋め込み
  490. add_scriptc(C_NAME);
  491. str_data[l].backpatch=script_pos;
  492. add_scriptb(backpatch);
  493. add_scriptb(backpatch>>8);
  494. add_scriptb(backpatch>>16);
  495. break;
  496. case C_INT:
  497. add_scripti(abs(str_data[l].val));
  498. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  499. add_scriptc(C_NEG);
  500. break;
  501. default:
  502. // もう他の用途と確定してるので数字をそのまま
  503. add_scriptc(C_NAME);
  504. add_scriptb(l);
  505. add_scriptb(l>>8);
  506. add_scriptb(l>>16);
  507. break;
  508. }
  509. }
  510. /*==========================================
  511. * ラベルを解決する
  512. *------------------------------------------*/
  513. void set_label(int l,int pos, const char* script_pos)
  514. {
  515. int i,next;
  516. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  517. { //Prevent overwriting constants values and parameters [Skotlex]
  518. disp_error_message("set_label: invalid label name",script_pos);
  519. return;
  520. }
  521. if(str_data[l].label!=-1){
  522. disp_error_message("set_label: dup label ",script_pos);
  523. return;
  524. }
  525. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  526. str_data[l].label=pos;
  527. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  528. next=GETVALUE(script_buf,i);
  529. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  530. SETVALUE(script_buf,i,pos);
  531. i=next;
  532. }
  533. }
  534. /// Skips spaces and/or comments.
  535. static
  536. const char* skip_space(const char* p)
  537. {
  538. for(;;)
  539. {
  540. while( ISSPACE(*p) )
  541. ++p;
  542. if( *p == '/' && p[1] == '/' )
  543. {// line comment
  544. while(*p && *p!='\n')
  545. ++p;
  546. }
  547. else if( *p == '/' && p[1] == '*' )
  548. {// block comment
  549. p += 2;
  550. for(;;)
  551. {
  552. if( *p == '\0' )
  553. disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  554. if( *p == '*' || p[1] == '/' )
  555. {// end of block comment
  556. p += 2;
  557. break;
  558. }
  559. ++p;
  560. }
  561. }
  562. else
  563. break;
  564. }
  565. return p;
  566. }
  567. /// Skips a word.
  568. /// A word consists of undercores and/or alfanumeric characters,
  569. /// and valid variable prefixes/postfixes.
  570. static
  571. const char* skip_word(const char* p)
  572. {
  573. // prefix
  574. switch( *p )
  575. {
  576. case '@':// temporary char variable
  577. ++p; break;
  578. case '#':// account variable
  579. p += ( p[1] == '#' ? 2 : 1 ); break;
  580. case '.':// npc variable
  581. p += ( p[1] == '@' ? 2 : 1 ); break;
  582. case '$':// global variable
  583. p += ( p[1] == '@' ? 2 : 1 ); break;
  584. }
  585. while( ISALNUM(*p) || *p == '_' )
  586. ++p;
  587. // postfix
  588. if( *p == '$' )// string
  589. p++;
  590. return p;
  591. }
  592. /// Adds a word to str_data.
  593. /// @see skip_word
  594. /// @see add_str
  595. static
  596. int add_word(const char* p)
  597. {
  598. char* word;
  599. int len;
  600. int i;
  601. // Check for a word
  602. len = skip_word(p) - p;
  603. if( len == 0 )
  604. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  605. // Duplicate the word
  606. CREATE(word, char, len+1);
  607. memcpy(word, p, len);
  608. word[len] = 0;
  609. // add the word
  610. i = add_str(word);
  611. aFree(word);
  612. return i;
  613. }
  614. /// Parses a function call.
  615. /// The argument list can have parenthesis or not.
  616. /// The number of arguments is checked.
  617. static
  618. const char* parse_callfunc(const char* p, int require_paren)
  619. {
  620. const char* p2;
  621. const char* arg=NULL;
  622. int func;
  623. func = add_word(p);
  624. if( str_data[func].type == C_FUNC ){
  625. // buildin function
  626. add_scriptl(func);
  627. add_scriptc(C_ARG);
  628. arg = buildin_func[str_data[func].val].arg;
  629. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  630. // script defined function
  631. int callsub = search_str("callsub");
  632. add_scriptl(callsub);
  633. add_scriptc(C_ARG);
  634. add_scriptl(func);
  635. arg = buildin_func[str_data[callsub].val].arg;
  636. if( *arg == 0 )
  637. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  638. if( *arg != '*' )
  639. ++arg; // count func as argument
  640. } else
  641. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  642. p = skip_word(p);
  643. p = skip_space(p);
  644. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  645. syntax.curly[syntax.curly_count].count = 0;
  646. if( *p == ';' )
  647. {// <func name> ';'
  648. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  649. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  650. {// <func name> '(' ')'
  651. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  652. p = p2;
  653. /*
  654. } else if( 0 && require_paren && *p != '(' )
  655. {// <func name>
  656. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  657. */
  658. } else
  659. {// <func name> <arg list>
  660. if( require_paren ){
  661. if( *p != '(' )
  662. disp_error_message("need '('",p);
  663. ++p; // skip '('
  664. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  665. } else if( *p == '(' ){
  666. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  667. } else {
  668. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  669. }
  670. ++syntax.curly_count;
  671. while( *arg ) {
  672. p2=parse_subexpr(p,-1);
  673. if( p == p2 )
  674. break; // not an argument
  675. if( *arg != '*' )
  676. ++arg; // next argument
  677. p=skip_space(p2);
  678. if( *arg == 0 || *p != ',' )
  679. break; // no more arguments
  680. ++p; // skip comma
  681. }
  682. --syntax.curly_count;
  683. }
  684. if( *arg && *arg != '?' && *arg != '*' )
  685. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  686. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  687. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  688. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  689. if( *p != ')' )
  690. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  691. ++p;
  692. }
  693. add_scriptc(C_FUNC);
  694. return p;
  695. }
  696. /*==========================================
  697. * 項の解析
  698. *------------------------------------------*/
  699. const char* parse_simpleexpr(const char *p)
  700. {
  701. int i;
  702. p=skip_space(p);
  703. #ifdef DEBUG_FUNCIN
  704. if(battle_config.etc_log)
  705. ShowDebug("parse_simpleexpr %s\n",p);
  706. #endif
  707. if(*p==';' || *p==',')
  708. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  709. if(*p=='('){
  710. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  711. ++syntax.curly[i].count;
  712. p=parse_subexpr(p+1,-1);
  713. p=skip_space(p);
  714. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  715. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  716. ){
  717. if( *p == ',' ){
  718. syntax.curly[i].flag = ARGLIST_PAREN;
  719. return p;
  720. } else
  721. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  722. }
  723. if( *p != ')' )
  724. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  725. ++p;
  726. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  727. char *np;
  728. i=strtoul(p,&np,0);
  729. add_scripti(i);
  730. p=np;
  731. } else if(*p=='"'){
  732. add_scriptc(C_STR);
  733. p++;
  734. while( *p && *p != '"' ){
  735. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  736. p++;
  737. else if( *p == '\n' )
  738. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  739. add_scriptb(*p++);
  740. }
  741. if(!*p)
  742. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  743. add_scriptb(0);
  744. p++; //'"'
  745. } else {
  746. int l;
  747. // label , register , function etc
  748. if(skip_word(p)==p)
  749. disp_error_message("parse_simpleexpr: unexpected character",p);
  750. l=add_word(p);
  751. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  752. return parse_callfunc(p,1);
  753. p=skip_word(p);
  754. if( *p == '[' ){
  755. // array(name[i] => getelementofarray(name,i) )
  756. add_scriptl(search_str("getelementofarray"));
  757. add_scriptc(C_ARG);
  758. add_scriptl(l);
  759. p=parse_subexpr(p+1,-1);
  760. p=skip_space(p);
  761. if( *p != ']' )
  762. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  763. ++p;
  764. add_scriptc(C_FUNC);
  765. }else
  766. add_scriptl(l);
  767. }
  768. #ifdef DEBUG_FUNCIN
  769. if(battle_config.etc_log)
  770. ShowDebug("parse_simpleexpr end %s\n",p);
  771. #endif
  772. return p;
  773. }
  774. /*==========================================
  775. * 式の解析
  776. *------------------------------------------*/
  777. const char* parse_subexpr(const char* p,int limit)
  778. {
  779. int op,opl,len;
  780. const char* tmpp;
  781. #ifdef DEBUG_FUNCIN
  782. if(battle_config.etc_log)
  783. ShowDebug("parse_subexpr %s\n",p);
  784. #endif
  785. p=skip_space(p);
  786. if(*p=='-'){
  787. tmpp=skip_space(p+1);
  788. if(*tmpp==';' || *tmpp==','){
  789. add_scriptl(LABEL_NEXTLINE);
  790. p++;
  791. return p;
  792. }
  793. }
  794. tmpp=p;
  795. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  796. p=parse_subexpr(p+1,10);
  797. add_scriptc(op);
  798. } else
  799. p=parse_simpleexpr(p);
  800. p=skip_space(p);
  801. while((
  802. (op=C_OP3,opl=0,len=1,*p=='?') ||
  803. (op=C_ADD,opl=8,len=1,*p=='+') ||
  804. (op=C_SUB,opl=8,len=1,*p=='-') ||
  805. (op=C_MUL,opl=9,len=1,*p=='*') ||
  806. (op=C_DIV,opl=9,len=1,*p=='/') ||
  807. (op=C_MOD,opl=9,len=1,*p=='%') ||
  808. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  809. (op=C_AND,opl=6,len=1,*p=='&') ||
  810. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  811. (op=C_OR,opl=5,len=1,*p=='|') ||
  812. (op=C_XOR,opl=4,len=1,*p=='^') ||
  813. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  814. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  815. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  816. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  817. (op=C_GT,opl=3,len=1,*p=='>') ||
  818. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  819. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  820. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  821. p+=len;
  822. if(op == C_OP3) {
  823. p=parse_subexpr(p,-1);
  824. p=skip_space(p);
  825. if( *(p++) != ':')
  826. disp_error_message("parse_subexpr: need ':'", p-1);
  827. p=parse_subexpr(p,-1);
  828. } else {
  829. p=parse_subexpr(p,opl);
  830. }
  831. add_scriptc(op);
  832. p=skip_space(p);
  833. }
  834. #ifdef DEBUG_FUNCIN
  835. if(battle_config.etc_log)
  836. ShowDebug("parse_subexpr end %s\n",p);
  837. #endif
  838. return p; /* return first untreated operator */
  839. }
  840. /*==========================================
  841. * 式の評価
  842. *------------------------------------------*/
  843. const char* parse_expr(const char *p)
  844. {
  845. #ifdef DEBUG_FUNCIN
  846. if(battle_config.etc_log)
  847. ShowDebug("parse_expr %s\n",p);
  848. #endif
  849. switch(*p){
  850. case ')': case ';': case ':': case '[': case ']':
  851. case '}':
  852. disp_error_message("parse_expr: unexpected char",p);
  853. }
  854. p=parse_subexpr(p,-1);
  855. #ifdef DEBUG_FUNCIN
  856. if(battle_config.etc_log)
  857. ShowDebug("parse_expr end %s\n",p);
  858. #endif
  859. return p;
  860. }
  861. /*==========================================
  862. * 行の解析
  863. *------------------------------------------*/
  864. const char* parse_line(const char* p)
  865. {
  866. const char* p2;
  867. p=skip_space(p);
  868. if(*p==';') {
  869. // if(); for(); while(); のために閉じ判定
  870. p = parse_syntax_close(p);
  871. return p+1;
  872. }
  873. if(*p==')' && parse_syntax_for_flag)
  874. return p+1;
  875. p = skip_space(p);
  876. if(p[0] == '{') {
  877. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  878. syntax.curly[syntax.curly_count].count = -1;
  879. syntax.curly[syntax.curly_count].index = -1;
  880. syntax.curly_count++;
  881. return p + 1;
  882. } else if(p[0] == '}') {
  883. return parse_curly_close(p);
  884. }
  885. // 構文関連の処理
  886. p2 = parse_syntax(p);
  887. if(p2 != NULL)
  888. return p2;
  889. p = parse_callfunc(p,0);
  890. p = skip_space(p);
  891. if(parse_syntax_for_flag) {
  892. if( *p != ')' )
  893. disp_error_message("parse_line: need ')'",p);
  894. } else {
  895. if( *p != ';' )
  896. disp_error_message("parse_line: need ';'",p);
  897. }
  898. // if, for , while の閉じ判定
  899. p = parse_syntax_close(p+1);
  900. return p;
  901. }
  902. // { ... } の閉じ処理
  903. const char* parse_curly_close(const char* p)
  904. {
  905. if(syntax.curly_count <= 0) {
  906. disp_error_message("parse_curly_close: unexpected string",p);
  907. return p + 1;
  908. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  909. syntax.curly_count--;
  910. // if, for , while の閉じ判定
  911. p = parse_syntax_close(p + 1);
  912. return p;
  913. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  914. // switch() 閉じ判定
  915. int pos = syntax.curly_count-1;
  916. char label[256];
  917. int l;
  918. // 一時変数を消す
  919. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  920. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  921. parse_line(label);
  922. syntax.curly_count--;
  923. // 無条件で終了ポインタに移動
  924. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  925. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  926. parse_line(label);
  927. syntax.curly_count--;
  928. // 現在地のラベルを付ける
  929. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  930. l=add_str(label);
  931. set_label(l,script_pos, p);
  932. if(syntax.curly[pos].flag) {
  933. // default が存在する
  934. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  935. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  936. parse_line(label);
  937. syntax.curly_count--;
  938. }
  939. // 終了ラベルを付ける
  940. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  941. l=add_str(label);
  942. set_label(l,script_pos, p);
  943. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  944. syntax.curly_count--;
  945. return p+1;
  946. } else {
  947. disp_error_message("parse_curly_close: unexpected string",p);
  948. return p + 1;
  949. }
  950. }
  951. // 構文関連の処理
  952. // break, case, continue, default, do, for, function,
  953. // if, switch, while をこの内部で処理します。
  954. const char* parse_syntax(const char* p)
  955. {
  956. const char *p2 = skip_word(p);
  957. switch(*p) {
  958. case 'B':
  959. case 'b':
  960. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  961. // break の処理
  962. char label[256];
  963. int pos = syntax.curly_count - 1;
  964. while(pos >= 0) {
  965. if(syntax.curly[pos].type == TYPE_DO) {
  966. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  967. break;
  968. } else if(syntax.curly[pos].type == TYPE_FOR) {
  969. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  970. break;
  971. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  972. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  973. break;
  974. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  975. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  976. break;
  977. }
  978. pos--;
  979. }
  980. if(pos < 0) {
  981. disp_error_message("parse_syntax: unexpected 'break'",p);
  982. } else {
  983. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  984. parse_line(label);
  985. syntax.curly_count--;
  986. }
  987. p = skip_space(p2);
  988. if(*p != ';') {
  989. disp_error_message("parse_syntax: need ';'",p);
  990. }
  991. p++;
  992. // if, for , while の閉じ判定
  993. p = parse_syntax_close(p + 1);
  994. return p;
  995. }
  996. break;
  997. case 'c':
  998. case 'C':
  999. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1000. // case の処理
  1001. int pos = syntax.curly_count-1;
  1002. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1003. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1004. return p+1;
  1005. } else {
  1006. char label[256];
  1007. int l,v;
  1008. char *np;
  1009. if(syntax.curly[pos].count != 1) {
  1010. // FALLTHRU 用のジャンプ
  1011. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1012. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1013. parse_line(label);
  1014. syntax.curly_count--;
  1015. // 現在地のラベルを付ける
  1016. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1017. l=add_str(label);
  1018. set_label(l,script_pos, p);
  1019. }
  1020. // switch 判定文
  1021. p = skip_space(p2);
  1022. if(p == p2) {
  1023. disp_error_message("parse_syntax: expect space ' '",p);
  1024. }
  1025. // check whether case label is integer or not
  1026. v = strtol(p,&np,0);
  1027. if(np == p) { //Check for constants
  1028. p2 = skip_word(p);
  1029. v = p2-p; // length of word at p2
  1030. memcpy(label,p,v);
  1031. label[v]='\0';
  1032. v = search_str(label);
  1033. if (v < 0 || str_data[v].type != C_INT)
  1034. disp_error_message("parse_syntax: 'case' label not integer",p);
  1035. v = str_data[v].val;
  1036. p = skip_word(p);
  1037. } else { //Numeric value
  1038. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1039. p++;
  1040. p = skip_word(p);
  1041. if(np != p)
  1042. disp_error_message("parse_syntax: 'case' label not integer",np);
  1043. }
  1044. p = skip_space(p);
  1045. if(*p != ':')
  1046. disp_error_message("parse_syntax: expect ':'",p);
  1047. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1048. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1049. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1050. // 2回parse しないとダメ
  1051. p2 = parse_line(label);
  1052. parse_line(p2);
  1053. syntax.curly_count--;
  1054. if(syntax.curly[pos].count != 1) {
  1055. // FALLTHRU 終了後のラベル
  1056. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1057. l=add_str(label);
  1058. set_label(l,script_pos,p);
  1059. }
  1060. // check duplication of case label [Rayce]
  1061. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1062. disp_error_message("parse_syntax: dup 'case'",p);
  1063. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1064. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1065. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1066. parse_line(label);
  1067. syntax.curly_count--;
  1068. syntax.curly[pos].count++;
  1069. }
  1070. return p + 1;
  1071. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1072. // continue の処理
  1073. char label[256];
  1074. int pos = syntax.curly_count - 1;
  1075. while(pos >= 0) {
  1076. if(syntax.curly[pos].type == TYPE_DO) {
  1077. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1078. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1079. break;
  1080. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1081. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1082. break;
  1083. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1084. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1085. break;
  1086. }
  1087. pos--;
  1088. }
  1089. if(pos < 0) {
  1090. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1091. } else {
  1092. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1093. parse_line(label);
  1094. syntax.curly_count--;
  1095. }
  1096. p = skip_space(p2);
  1097. if(*p != ';')
  1098. disp_error_message("parse_syntax: need ';'",p);
  1099. p++;
  1100. // if, for , while の閉じ判定
  1101. p = parse_syntax_close(p + 1);
  1102. return p;
  1103. }
  1104. break;
  1105. case 'd':
  1106. case 'D':
  1107. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1108. // switch - default の処理
  1109. int pos = syntax.curly_count-1;
  1110. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1111. disp_error_message("parse_syntax: unexpected 'default'",p);
  1112. } else if(syntax.curly[pos].flag) {
  1113. disp_error_message("parse_syntax: dup 'default'",p);
  1114. } else {
  1115. char label[256];
  1116. int l;
  1117. // 現在地のラベルを付ける
  1118. p = skip_space(p2);
  1119. if(*p != ':') {
  1120. disp_error_message("parse_syntax: need ':'",p);
  1121. }
  1122. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1123. l=add_str(label);
  1124. set_label(l,script_pos,p);
  1125. // 無条件で次のリンクに飛ばす
  1126. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1127. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1128. parse_line(label);
  1129. syntax.curly_count--;
  1130. // default のラベルを付ける
  1131. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1132. l=add_str(label);
  1133. set_label(l,script_pos,p);
  1134. syntax.curly[syntax.curly_count - 1].flag = 1;
  1135. syntax.curly[pos].count++;
  1136. }
  1137. return p + 1;
  1138. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1139. int l;
  1140. char label[256];
  1141. p=skip_space(p2);
  1142. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1143. syntax.curly[syntax.curly_count].count = 1;
  1144. syntax.curly[syntax.curly_count].index = syntax.index++;
  1145. syntax.curly[syntax.curly_count].flag = 0;
  1146. // 現在地のラベル形成する
  1147. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1148. l=add_str(label);
  1149. set_label(l,script_pos,p);
  1150. syntax.curly_count++;
  1151. return p;
  1152. }
  1153. break;
  1154. case 'f':
  1155. case 'F':
  1156. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1157. int l;
  1158. char label[256];
  1159. int pos = syntax.curly_count;
  1160. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1161. syntax.curly[syntax.curly_count].count = 1;
  1162. syntax.curly[syntax.curly_count].index = syntax.index++;
  1163. syntax.curly[syntax.curly_count].flag = 0;
  1164. syntax.curly_count++;
  1165. p=skip_space(p2);
  1166. if(*p != '(')
  1167. disp_error_message("parse_syntax: need '('",p);
  1168. p++;
  1169. // 初期化文を実行する
  1170. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1171. p=parse_line(p);
  1172. syntax.curly_count--;
  1173. // 条件判断開始のラベル形成する
  1174. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1175. l=add_str(label);
  1176. set_label(l,script_pos,p);
  1177. p=skip_space(p);
  1178. if(*p == ';') {
  1179. // for(;;) のパターンなので必ず真
  1180. ;
  1181. } else {
  1182. // 条件が偽なら終了地点に飛ばす
  1183. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1184. add_scriptl(add_str("jump_zero"));
  1185. add_scriptc(C_ARG);
  1186. p=parse_expr(p);
  1187. p=skip_space(p);
  1188. add_scriptl(add_str(label));
  1189. add_scriptc(C_FUNC);
  1190. }
  1191. if(*p != ';')
  1192. disp_error_message("parse_syntax: need ';'",p);
  1193. p++;
  1194. // ループ開始に飛ばす
  1195. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1196. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1197. parse_line(label);
  1198. syntax.curly_count--;
  1199. // 次のループへのラベル形成する
  1200. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1201. l=add_str(label);
  1202. set_label(l,script_pos,p);
  1203. // 次のループに入る時の処理
  1204. // for 最後の ')' を ';' として扱うフラグ
  1205. parse_syntax_for_flag = 1;
  1206. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1207. p=parse_line(p);
  1208. syntax.curly_count--;
  1209. parse_syntax_for_flag = 0;
  1210. // 条件判定処理に飛ばす
  1211. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1212. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1213. parse_line(label);
  1214. syntax.curly_count--;
  1215. // ループ開始のラベル付け
  1216. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1217. l=add_str(label);
  1218. set_label(l,script_pos,p);
  1219. return p;
  1220. }
  1221. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1222. {// internal script function
  1223. const char *func_name;
  1224. func_name = skip_space(p2);
  1225. p = skip_word(func_name);
  1226. if( p == func_name )
  1227. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1228. if( *skip_space(p) == ';' )
  1229. {// function <name> ;
  1230. // function declaration - just register the name
  1231. int l;
  1232. l = add_word(func_name);
  1233. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1234. str_data[l].type = C_USERFUNC;
  1235. return skip_space(p) + 1;
  1236. }
  1237. else
  1238. {// function <name> <line/block of code>
  1239. char label[256];
  1240. int l;
  1241. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1242. syntax.curly[syntax.curly_count].count = 1;
  1243. syntax.curly[syntax.curly_count].index = syntax.index++;
  1244. syntax.curly[syntax.curly_count].flag = 0;
  1245. ++syntax.curly_count;
  1246. // Jump over the function code
  1247. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1248. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1249. ++syntax.curly_count;
  1250. parse_line(label);
  1251. --syntax.curly_count;
  1252. // Set the position of the function (label)
  1253. l=add_word(func_name);
  1254. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1255. str_data[l].type = C_USERFUNC;
  1256. set_label(l, script_pos, p);
  1257. if( parse_options&SCRIPT_USE_LABEL_DB )
  1258. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1259. return skip_space(p);
  1260. }
  1261. }
  1262. break;
  1263. case 'i':
  1264. case 'I':
  1265. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1266. // if() の処理
  1267. char label[256];
  1268. p=skip_space(p2);
  1269. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1270. disp_error_message("need '('",p);
  1271. }
  1272. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1273. syntax.curly[syntax.curly_count].count = 1;
  1274. syntax.curly[syntax.curly_count].index = syntax.index++;
  1275. syntax.curly[syntax.curly_count].flag = 0;
  1276. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1277. syntax.curly_count++;
  1278. add_scriptl(add_str("jump_zero"));
  1279. add_scriptc(C_ARG);
  1280. p=parse_expr(p);
  1281. p=skip_space(p);
  1282. add_scriptl(add_str(label));
  1283. add_scriptc(C_FUNC);
  1284. return p;
  1285. }
  1286. break;
  1287. case 's':
  1288. case 'S':
  1289. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1290. // switch() の処理
  1291. char label[256];
  1292. p=skip_space(p2);
  1293. if(*p != '(') {
  1294. disp_error_message("need '('",p);
  1295. }
  1296. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1297. syntax.curly[syntax.curly_count].count = 1;
  1298. syntax.curly[syntax.curly_count].index = syntax.index++;
  1299. syntax.curly[syntax.curly_count].flag = 0;
  1300. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1301. syntax.curly_count++;
  1302. add_scriptl(add_str("set"));
  1303. add_scriptc(C_ARG);
  1304. add_scriptl(add_str(label));
  1305. p=parse_expr(p);
  1306. p=skip_space(p);
  1307. if(*p != '{') {
  1308. disp_error_message("parse_syntax: need '{'",p);
  1309. }
  1310. add_scriptc(C_FUNC);
  1311. return p + 1;
  1312. }
  1313. break;
  1314. case 'w':
  1315. case 'W':
  1316. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1317. int l;
  1318. char label[256];
  1319. p=skip_space(p2);
  1320. if(*p != '(') {
  1321. disp_error_message("need '('",p);
  1322. }
  1323. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1324. syntax.curly[syntax.curly_count].count = 1;
  1325. syntax.curly[syntax.curly_count].index = syntax.index++;
  1326. syntax.curly[syntax.curly_count].flag = 0;
  1327. // 条件判断開始のラベル形成する
  1328. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1329. l=add_str(label);
  1330. set_label(l,script_pos,p);
  1331. // 条件が偽なら終了地点に飛ばす
  1332. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1333. syntax.curly_count++;
  1334. add_scriptl(add_str("jump_zero"));
  1335. add_scriptc(C_ARG);
  1336. p=parse_expr(p);
  1337. p=skip_space(p);
  1338. add_scriptl(add_str(label));
  1339. add_scriptc(C_FUNC);
  1340. return p;
  1341. }
  1342. break;
  1343. }
  1344. return NULL;
  1345. }
  1346. const char* parse_syntax_close(const char *p) {
  1347. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1348. int flag;
  1349. do {
  1350. p = parse_syntax_close_sub(p,&flag);
  1351. } while(flag);
  1352. return p;
  1353. }
  1354. // if, for , while , do の閉じ判定
  1355. // flag == 1 : 閉じられた
  1356. // flag == 0 : 閉じられない
  1357. const char* parse_syntax_close_sub(const char* p,int* flag)
  1358. {
  1359. char label[256];
  1360. int pos = syntax.curly_count - 1;
  1361. int l;
  1362. *flag = 1;
  1363. if(syntax.curly_count <= 0) {
  1364. *flag = 0;
  1365. return p;
  1366. } else if(syntax.curly[pos].type == TYPE_IF) {
  1367. const char *bp = p;
  1368. const char *p2;
  1369. // if 最終場所へ飛ばす
  1370. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1371. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1372. parse_line(label);
  1373. syntax.curly_count--;
  1374. // 現在地のラベルを付ける
  1375. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1376. l=add_str(label);
  1377. set_label(l,script_pos,p);
  1378. syntax.curly[pos].count++;
  1379. p = skip_space(p);
  1380. p2 = skip_word(p);
  1381. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1382. // else or else - if
  1383. p = skip_space(p2);
  1384. p2 = skip_word(p);
  1385. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1386. // else - if
  1387. p=skip_space(p2);
  1388. if(*p != '(') {
  1389. disp_error_message("need '('",p);
  1390. }
  1391. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1392. add_scriptl(add_str("jump_zero"));
  1393. add_scriptc(C_ARG);
  1394. p=parse_expr(p);
  1395. p=skip_space(p);
  1396. add_scriptl(add_str(label));
  1397. add_scriptc(C_FUNC);
  1398. *flag = 0;
  1399. return p;
  1400. } else {
  1401. // else
  1402. if(!syntax.curly[pos].flag) {
  1403. syntax.curly[pos].flag = 1;
  1404. *flag = 0;
  1405. return p;
  1406. }
  1407. }
  1408. }
  1409. // if 閉じ
  1410. syntax.curly_count--;
  1411. // 最終地のラベルを付ける
  1412. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1413. l=add_str(label);
  1414. set_label(l,script_pos,p);
  1415. if(syntax.curly[pos].flag == 1) {
  1416. // このifに対するelseじゃないのでポインタの位置は同じ
  1417. return bp;
  1418. }
  1419. return p;
  1420. } else if(syntax.curly[pos].type == TYPE_DO) {
  1421. int l;
  1422. char label[256];
  1423. const char *p2;
  1424. if(syntax.curly[pos].flag) {
  1425. // 現在地のラベル形成する(continue でここに来る)
  1426. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1427. l=add_str(label);
  1428. set_label(l,script_pos,p);
  1429. }
  1430. // 条件が偽なら終了地点に飛ばす
  1431. p = skip_space(p);
  1432. p2 = skip_word(p);
  1433. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1434. disp_error_message("parse_syntax: need 'while'",p);
  1435. p = skip_space(p2);
  1436. if(*p != '(') {
  1437. disp_error_message("need '('",p);
  1438. }
  1439. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1440. add_scriptl(add_str("jump_zero"));
  1441. add_scriptc(C_ARG);
  1442. p=parse_expr(p);
  1443. p=skip_space(p);
  1444. add_scriptl(add_str(label));
  1445. add_scriptc(C_FUNC);
  1446. // 開始地点に飛ばす
  1447. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1448. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1449. parse_line(label);
  1450. syntax.curly_count--;
  1451. // 条件終了地点のラベル形成する
  1452. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1453. l=add_str(label);
  1454. set_label(l,script_pos,p);
  1455. p = skip_space(p);
  1456. if(*p != ';') {
  1457. disp_error_message("parse_syntax: need ';'",p);
  1458. return p+1;
  1459. }
  1460. p++;
  1461. syntax.curly_count--;
  1462. return p;
  1463. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1464. // 次のループに飛ばす
  1465. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1466. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1467. parse_line(label);
  1468. syntax.curly_count--;
  1469. // for 終了のラベル付け
  1470. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1471. l=add_str(label);
  1472. set_label(l,script_pos,p);
  1473. syntax.curly_count--;
  1474. return p;
  1475. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1476. // while 条件判断へ飛ばす
  1477. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1478. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1479. parse_line(label);
  1480. syntax.curly_count--;
  1481. // while 終了のラベル付け
  1482. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1483. l=add_str(label);
  1484. set_label(l,script_pos,p);
  1485. syntax.curly_count--;
  1486. return p;
  1487. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1488. int pos = syntax.curly_count-1;
  1489. char label[256];
  1490. int l;
  1491. // 戻す
  1492. sprintf(label,"return;");
  1493. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1494. parse_line(label);
  1495. syntax.curly_count--;
  1496. // 現在地のラベルを付ける
  1497. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1498. l=add_str(label);
  1499. set_label(l,script_pos,p);
  1500. syntax.curly_count--;
  1501. return p + 1;
  1502. } else {
  1503. *flag = 0;
  1504. return p;
  1505. }
  1506. }
  1507. /*==========================================
  1508. * 組み込み関数の追加
  1509. *------------------------------------------*/
  1510. static void add_buildin_func(void)
  1511. {
  1512. int i,n;
  1513. const char* p;
  1514. for( i=0; buildin_func[i].func; i++ ){
  1515. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1516. // 'v' - value (either string or int or reference)
  1517. // 's' - string
  1518. // 'i' - int
  1519. // 'r' - reference (of a variable)
  1520. // 'l' - label
  1521. // '?' - one optional parameter
  1522. // '*' - unknown number of optional parameters
  1523. p=buildin_func[i].arg;
  1524. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1525. while( *p == '?' ) ++p;
  1526. if( *p == '*' ) ++p;
  1527. if( *p != 0){
  1528. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1529. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1530. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1531. } else {
  1532. n=add_str(buildin_func[i].name);
  1533. str_data[n].type=C_FUNC;
  1534. str_data[n].val=i;
  1535. str_data[n].func=buildin_func[i].func;
  1536. }
  1537. }
  1538. }
  1539. /*==========================================
  1540. * 定数データベースの読み込み
  1541. *------------------------------------------*/
  1542. static void read_constdb(void)
  1543. {
  1544. FILE *fp;
  1545. char line[1024],name[1024],val[1024];
  1546. int n,type;
  1547. sprintf(line, "%s/const.txt", db_path);
  1548. fp=fopen(line, "r");
  1549. if(fp==NULL){
  1550. ShowError("can't read %s\n", line);
  1551. return ;
  1552. }
  1553. while(fgets(line, sizeof(line), fp))
  1554. {
  1555. if(line[0]=='/' && line[1]=='/')
  1556. continue;
  1557. type=0;
  1558. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1559. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1560. n=add_str(name);
  1561. if(type==0)
  1562. str_data[n].type=C_INT;
  1563. else
  1564. str_data[n].type=C_PARAM;
  1565. str_data[n].val= (int)strtol(val,NULL,0);
  1566. }
  1567. }
  1568. fclose(fp);
  1569. }
  1570. /*==========================================
  1571. * エラー表示
  1572. *------------------------------------------*/
  1573. const char* script_print_line( const char *p, const char *mark, int line )
  1574. {
  1575. int i;
  1576. if( p == NULL || !p[0] ) return NULL;
  1577. if( line < 0 )
  1578. printf("*% 5d : ", -line);
  1579. else
  1580. printf(" % 5d : ", line);
  1581. for(i=0;p[i] && p[i] != '\n';i++){
  1582. if(p + i != mark)
  1583. printf("%c",p[i]);
  1584. else
  1585. printf("\'%c\'",p[i]);
  1586. }
  1587. printf("\n");
  1588. return p+i+(p[i] == '\n' ? 1 : 0);
  1589. }
  1590. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos)
  1591. {
  1592. // エラーが発生した行を求める
  1593. int j;
  1594. int line = start_line;
  1595. const char *p;
  1596. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1597. for(p=src;p && *p;line++){
  1598. char *lineend=strchr(p,'\n');
  1599. if(lineend==NULL || error_pos<lineend){
  1600. break;
  1601. }
  1602. for( j = 0; j < 4; j++ ) {
  1603. linestart[j] = linestart[j+1];
  1604. }
  1605. linestart[4] = p;
  1606. p=lineend+1;
  1607. }
  1608. printf("\a\n");
  1609. printf("script error on %s line %d\n", file, line);
  1610. printf(" %s\n", error_msg);
  1611. for(j = 0; j < 5; j++ ) {
  1612. script_print_line( linestart[j], NULL, line + j - 5);
  1613. }
  1614. p = script_print_line( p, error_pos, -line);
  1615. for(j = 0; j < 5; j++) {
  1616. p = script_print_line( p, NULL, line + j + 1 );
  1617. }
  1618. }
  1619. /*==========================================
  1620. * スクリプトの解析
  1621. *------------------------------------------*/
  1622. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1623. {
  1624. const char *p,*tmpp;
  1625. int i;
  1626. struct script_code *code;
  1627. static int first=1;
  1628. memset(&syntax,0,sizeof(syntax));
  1629. if(first){
  1630. add_buildin_func();
  1631. read_constdb();
  1632. }
  1633. first=0;
  1634. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1635. script_pos=0;
  1636. script_size=SCRIPT_BLOCK_SIZE;
  1637. str_data[LABEL_NEXTLINE].type=C_NOP;
  1638. str_data[LABEL_NEXTLINE].backpatch=-1;
  1639. str_data[LABEL_NEXTLINE].label=-1;
  1640. for(i=LABEL_START;i<str_num;i++){
  1641. if(
  1642. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1643. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1644. ){
  1645. str_data[i].type=C_NOP;
  1646. str_data[i].backpatch=-1;
  1647. str_data[i].label=-1;
  1648. }
  1649. }
  1650. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1651. if( options&SCRIPT_USE_LABEL_DB )
  1652. scriptlabel_db->clear(scriptlabel_db, NULL);
  1653. parse_options = options;
  1654. if( setjmp( error_jump ) != 0 ) {
  1655. //Restore program state when script has problems. [from jA]
  1656. int i;
  1657. const int size = sizeof(syntax.curly)/sizeof(syntax.curly[0]);
  1658. if( error_report )
  1659. script_error(src,file,line,error_msg,error_pos);
  1660. aFree( error_msg );
  1661. aFree( script_buf );
  1662. script_pos = 0;
  1663. script_size = 0;
  1664. script_buf = NULL;
  1665. for(i=LABEL_START;i<str_num;i++)
  1666. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1667. for(i=0; i<size; i++)
  1668. linkdb_final(&syntax.curly[i].case_label);
  1669. return NULL;
  1670. }
  1671. parse_syntax_for_flag=0;
  1672. p=src;
  1673. p=skip_space(p);
  1674. if(*p!='{'){
  1675. disp_error_message("not found '{'",p);
  1676. }
  1677. p++;
  1678. p = skip_space(p);
  1679. if (p && *p == '}') {
  1680. // an empty function, just return
  1681. aFree( script_buf );
  1682. script_pos = 0;
  1683. script_size = 0;
  1684. script_buf = NULL;
  1685. return NULL;
  1686. }
  1687. while (p && *p && (*p!='}' || syntax.curly_count != 0)) {
  1688. p=skip_space(p);
  1689. // labelだけ特殊処理
  1690. tmpp=skip_space(skip_word(p));
  1691. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1692. i=add_word(p);
  1693. set_label(i,script_pos,p);
  1694. if( parse_options&SCRIPT_USE_LABEL_DB )
  1695. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1696. p=tmpp+1;
  1697. continue;
  1698. }
  1699. // 他は全部一緒くた
  1700. p=parse_line(p);
  1701. p=skip_space(p);
  1702. add_scriptc(C_EOL);
  1703. set_label(LABEL_NEXTLINE,script_pos,p);
  1704. str_data[LABEL_NEXTLINE].type=C_NOP;
  1705. str_data[LABEL_NEXTLINE].backpatch=-1;
  1706. str_data[LABEL_NEXTLINE].label=-1;
  1707. }
  1708. add_scriptc(C_NOP);
  1709. script_size = script_pos;
  1710. RECREATE(script_buf,unsigned char,script_pos);
  1711. // 未解決のラベルを解決
  1712. for(i=LABEL_START;i<str_num;i++){
  1713. if(str_data[i].type==C_NOP){
  1714. int j,next;
  1715. str_data[i].type=C_NAME;
  1716. str_data[i].label=i;
  1717. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1718. next=GETVALUE(script_buf,j);
  1719. SETVALUE(script_buf,j,i);
  1720. j=next;
  1721. }
  1722. }
  1723. }
  1724. #ifdef DEBUG_DISP
  1725. for(i=0;i<script_pos;i++){
  1726. if((i&15)==0) printf("%04x : ",i);
  1727. printf("%02x ",script_buf[i]);
  1728. if((i&15)==15) printf("\n");
  1729. }
  1730. printf("\n");
  1731. #endif
  1732. #ifdef DEBUG_DISASM
  1733. {
  1734. int i = 0,j;
  1735. while(i < script_pos) {
  1736. printf("%06x ",i);
  1737. j = i;
  1738. switch(get_com(script_buf,&i)) {
  1739. case C_EOL: printf("C_EOL"); break;
  1740. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1741. case C_POS:
  1742. printf("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1743. i += 3;
  1744. break;
  1745. case C_NAME:
  1746. j = (*(int*)(script_buf+i)&0xffffff);
  1747. printf("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1748. i += 3;
  1749. break;
  1750. case C_ARG: printf("C_ARG"); break;
  1751. case C_FUNC: printf("C_FUNC"); break;
  1752. case C_ADD: printf("C_ADD"); break;
  1753. case C_SUB: printf("C_SUB"); break;
  1754. case C_MUL: printf("C_MUL"); break;
  1755. case C_DIV: printf("C_DIV"); break;
  1756. case C_MOD: printf("C_MOD"); break;
  1757. case C_EQ: printf("C_EQ"); break;
  1758. case C_NE: printf("C_NE"); break;
  1759. case C_GT: printf("C_GT"); break;
  1760. case C_GE: printf("C_GE"); break;
  1761. case C_LT: printf("C_LT"); break;
  1762. case C_LE: printf("C_LE"); break;
  1763. case C_AND: printf("C_AND"); break;
  1764. case C_OR: printf("C_OR"); break;
  1765. case C_XOR: printf("C_XOR"); break;
  1766. case C_LAND: printf("C_LAND"); break;
  1767. case C_LOR: printf("C_LOR"); break;
  1768. case C_R_SHIFT: printf("C_R_SHIFT"); break;
  1769. case C_L_SHIFT: printf("C_L_SHIFT"); break;
  1770. case C_NEG: printf("C_NEG"); break;
  1771. case C_NOT: printf("C_NOT"); break;
  1772. case C_LNOT: printf("C_LNOT"); break;
  1773. case C_NOP: printf("C_NOP"); break;
  1774. case C_OP3: printf("C_OP3"); break;
  1775. case C_STR:
  1776. j = strlen(script_buf + i);
  1777. printf("C_STR %s",script_buf + i);
  1778. i+= j+1;
  1779. break;
  1780. default:
  1781. printf("unknown");
  1782. }
  1783. printf("\n");
  1784. }
  1785. }
  1786. #endif
  1787. CREATE(code,struct script_code,1);
  1788. code->script_buf = script_buf;
  1789. code->script_size = script_size;
  1790. code->script_vars = NULL;
  1791. return code;
  1792. }
  1793. //
  1794. // 実行系
  1795. //
  1796. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1797. /// Returns the player attached to this script, identified by the rid.
  1798. /// If there is no player attached, the script is terminated.
  1799. TBL_PC *script_rid2sd(struct script_state *st)
  1800. {
  1801. TBL_PC *sd=map_id2sd(st->rid);
  1802. if(!sd){
  1803. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1804. st->state = END;
  1805. }
  1806. return sd;
  1807. }
  1808. /*==========================================
  1809. * Retrieves the value of a script variable
  1810. *------------------------------------------*/
  1811. int get_val(struct script_state* st, struct script_data* data)
  1812. {
  1813. TBL_PC* sd = NULL;
  1814. char *name, prefix, postfix;
  1815. if(!data_isreference(data)) return 0;
  1816. name = str_buf + str_data[data->u.num&0x00ffffff].str;
  1817. prefix = name[0]; postfix = name[strlen(name)-1];
  1818. if(not_server_variable(prefix)) {
  1819. sd = script_rid2sd(st);
  1820. if (!sd) { // needs player attached
  1821. // throw error, load some meaningful default values and return
  1822. ShowError("get_val error, cannot access player variable '%s'\n", name);
  1823. if (postfix == '$') { data->type = C_CONSTSTR; data->u.str = ""; } else { data->type = C_INT; data->u.num = 0; }
  1824. return 0;
  1825. }
  1826. }
  1827. if(postfix == '$') { // string variable
  1828. data->type = C_CONSTSTR;
  1829. switch (prefix) {
  1830. case '@':
  1831. data->u.str = pc_readregstr(sd, data->u.num); break;
  1832. case '$':
  1833. data->u.str = (char *)idb_get(mapregstr_db,data->u.num); break;
  1834. case '#':
  1835. data->u.str = (name[1] == '#') ? pc_readaccountreg2str(sd, name) : pc_readaccountregstr(sd, name); break;
  1836. case '.': {
  1837. struct linkdb_node** n;
  1838. n = (data->ref) ? data->ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  1839. data->u.str = linkdb_search(n, (void*)data->u.num);
  1840. }
  1841. break;
  1842. default:
  1843. data->u.str = pc_readglobalreg_str(sd, name); break;
  1844. }
  1845. if( data->u.str == NULL )
  1846. data->u.str = "";
  1847. } else { // integer variable
  1848. data->type = C_INT;
  1849. if(str_data[data->u.num&0x00ffffff].type == C_INT) {
  1850. data->u.num = str_data[data->u.num&0x00ffffff].val;
  1851. } else if(str_data[data->u.num&0x00ffffff].type == C_PARAM) {
  1852. data->u.num = pc_readparam(sd, str_data[data->u.num&0x00ffffff].val);
  1853. }
  1854. else
  1855. switch (prefix) {
  1856. case '@':
  1857. data->u.num = pc_readreg(sd, data->u.num); break;
  1858. case '$':
  1859. data->u.num = (int)idb_get(mapreg_db, data->u.num); break;
  1860. case '#':
  1861. data->u.num = (name[1] == '#') ? pc_readaccountreg2(sd, name) : pc_readaccountreg(sd, name); break;
  1862. case '.': {
  1863. struct linkdb_node** n;
  1864. n = (data->ref) ? data->ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  1865. data->u.num = (int)linkdb_search(n, (void*)data->u.num);
  1866. }
  1867. break;
  1868. default:
  1869. data->u.num = pc_readglobalreg(sd, name); break;
  1870. }
  1871. }
  1872. return 0;
  1873. }
  1874. /*==========================================
  1875. * Retrieves the value of a script variable
  1876. *------------------------------------------*/
  1877. void* get_val2(struct script_state* st, int num, struct linkdb_node** ref)
  1878. {
  1879. struct script_data dat;
  1880. dat.type = C_NAME;
  1881. dat.u.num = num;
  1882. dat.ref = ref;
  1883. get_val(st, &dat);
  1884. return (dat.type == C_INT) ? (void*)dat.u.num : (void*)dat.u.str;
  1885. }
  1886. /*==========================================
  1887. * Stores the value of a script variable
  1888. * Return value is 0 on fail, 1 on success.
  1889. *------------------------------------------*/
  1890. static int set_reg(struct script_state* st, TBL_PC* sd, int num, char* name, void* value, struct linkdb_node** ref)
  1891. {
  1892. char prefix = name[0]; char postfix = name[strlen(name)-1];
  1893. if (postfix == '$') { // string variable
  1894. char* str = (char*)value;
  1895. switch (prefix) {
  1896. case '@':
  1897. return pc_setregstr(sd, num, str);
  1898. case '$':
  1899. return mapreg_setregstr(num, str);
  1900. case '#':
  1901. return (name[1] == '#') ?
  1902. pc_setaccountreg2str(sd, name, str) :
  1903. pc_setaccountregstr(sd, name, str);
  1904. case '.': {
  1905. char* p;
  1906. struct linkdb_node** n;
  1907. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  1908. p = linkdb_search(n, (void*)num);
  1909. if (p) {
  1910. linkdb_erase(n, (void*)num);
  1911. aFree(p);
  1912. }
  1913. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  1914. }
  1915. return 1;
  1916. default:
  1917. return pc_setglobalreg_str(sd, name, str);
  1918. }
  1919. } else { // integer variable
  1920. int val = (int)value;
  1921. if(str_data[num&0x00ffffff].type == C_PARAM)
  1922. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  1923. switch (prefix) {
  1924. case '@':
  1925. return pc_setreg(sd, num, val);
  1926. case '$':
  1927. return mapreg_setreg(num, val);
  1928. case '#':
  1929. return (name[1] == '#') ?
  1930. pc_setaccountreg2(sd, name, val) :
  1931. pc_setaccountreg(sd, name, val);
  1932. case '.': {
  1933. struct linkdb_node** n;
  1934. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  1935. if (val == 0)
  1936. linkdb_erase(n, (void*)num);
  1937. else
  1938. linkdb_replace(n, (void*)num, (void*)val);
  1939. }
  1940. return 1;
  1941. default:
  1942. return pc_setglobalreg(sd, name, val);
  1943. }
  1944. }
  1945. }
  1946. int set_var(TBL_PC* sd, char* name, void* val)
  1947. {
  1948. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  1949. }
  1950. /*==========================================
  1951. * 文字列への変換
  1952. *------------------------------------------*/
  1953. const char* conv_str(struct script_state *st,struct script_data *data)
  1954. {
  1955. get_val(st,data);
  1956. if(data_isint(data)){
  1957. char *buf;
  1958. CREATE(buf,char,ITEM_NAME_LENGTH);
  1959. snprintf(buf,ITEM_NAME_LENGTH, "%d",data->u.num);
  1960. buf[ITEM_NAME_LENGTH-1]=0;
  1961. data->type=C_STR;
  1962. data->u.str=buf;
  1963. } else if(data_islabel(data)) {
  1964. // Protect from crashes by passing labels to string-expected args [jA2200]
  1965. data->type = C_CONSTSTR;
  1966. data->u.str = "** SCRIPT ERROR **";
  1967. } else if(data_isreference(data)){
  1968. data->type=C_CONSTSTR;
  1969. data->u.str=str_buf+str_data[data->u.num].str;
  1970. }
  1971. return data->u.str;
  1972. }
  1973. /*==========================================
  1974. * 数値へ変換
  1975. *------------------------------------------*/
  1976. int conv_num(struct script_state *st,struct script_data *data)
  1977. {
  1978. char *p;
  1979. get_val(st,data);
  1980. if(data_isstring(data)){
  1981. p=data->u.str;
  1982. data->u.num = atoi(p);
  1983. if(data->type==C_STR)
  1984. aFree(p);
  1985. data->type=C_INT;
  1986. }
  1987. return data->u.num;
  1988. }
  1989. //
  1990. // Stack operations
  1991. //
  1992. /// Increases the size of the stack
  1993. void stack_expand(struct script_stack* stack)
  1994. {
  1995. stack->sp_max += 64;
  1996. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  1997. stack->sp_max * sizeof(stack->stack_data[0]) );
  1998. memset(stack->stack_data + (stack->sp_max - 64), 0,
  1999. 64 * sizeof(stack->stack_data[0]) );
  2000. }
  2001. /// Pushes a value into the stack
  2002. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2003. /// Pushes a value into the stack (with reference)
  2004. void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref)
  2005. {
  2006. if( stack->sp >= stack->sp_max )
  2007. stack_expand(stack);
  2008. stack->stack_data[stack->sp].type = type;
  2009. stack->stack_data[stack->sp].u.num = val;
  2010. stack->stack_data[stack->sp].ref = ref;
  2011. stack->sp++;
  2012. }
  2013. /// Pushes a string into the stack
  2014. void push_str(struct script_stack* stack, int type, char* str)
  2015. {
  2016. if( stack->sp >= stack->sp_max )
  2017. stack_expand(stack);
  2018. stack->stack_data[stack->sp].type = type;
  2019. stack->stack_data[stack->sp].u.str = str;
  2020. stack->stack_data[stack->sp].ref = NULL;
  2021. stack->sp++;
  2022. }
  2023. /// Pushes a copy of the target position into the stack
  2024. void push_copy(struct script_stack* stack, int pos)
  2025. {
  2026. switch( stack->stack_data[pos].type )
  2027. {
  2028. case C_CONSTSTR:
  2029. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2030. break;
  2031. case C_STR:
  2032. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2033. break;
  2034. default:
  2035. push_val2(
  2036. stack,stack->stack_data[pos].type,
  2037. stack->stack_data[pos].u.num,
  2038. stack->stack_data[pos].ref
  2039. );
  2040. break;
  2041. }
  2042. }
  2043. /// Removes the values in indexes [start,end[ from the stack
  2044. void pop_stack(struct script_stack* stack, int start, int end)
  2045. {
  2046. struct script_data* data;
  2047. int i;
  2048. if( start < 0 )
  2049. start = 0;
  2050. if( end > stack->sp_max )
  2051. end = stack->sp_max;
  2052. if( start >= end )
  2053. return;// nothing to pop
  2054. // free stack elements
  2055. for( i = start; i < end; i++ )
  2056. {
  2057. data = &stack->stack_data[i];
  2058. if( data->type == C_STR )
  2059. aFree(data->u.str);
  2060. data->type = C_NOP;
  2061. }
  2062. // move the rest of the elements
  2063. if( stack->sp > end )
  2064. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2065. stack->sp -= end - start;
  2066. }
  2067. ///
  2068. ///
  2069. ///
  2070. /*==========================================
  2071. * スクリプト依存変数、関数依存変数の解放
  2072. *------------------------------------------*/
  2073. void script_free_vars(struct linkdb_node **node)
  2074. {
  2075. struct linkdb_node *n = *node;
  2076. while(n) {
  2077. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2078. char postfix = name[strlen(name)-1];
  2079. if( postfix == '$' ) {
  2080. // 文字型変数なので、データ削除
  2081. aFree(n->data);
  2082. }
  2083. n = n->next;
  2084. }
  2085. linkdb_final( node );
  2086. }
  2087. /*==========================================
  2088. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2089. *------------------------------------------*/
  2090. void script_free_stack(struct script_stack *stack)
  2091. {
  2092. int i;
  2093. for(i = 0; i < stack->sp; i++) {
  2094. if( stack->stack_data[i].type == C_STR ) {
  2095. aFree(stack->stack_data[i].u.str);
  2096. stack->stack_data[i].type = C_INT;
  2097. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2098. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2099. script_free_vars( n );
  2100. aFree( n );
  2101. }
  2102. }
  2103. script_free_vars( stack->var_function );
  2104. aFree(stack->var_function);
  2105. aFree(stack->stack_data);
  2106. aFree(stack);
  2107. }
  2108. void script_free_code(struct script_code* code)
  2109. {
  2110. script_free_vars( &code->script_vars );
  2111. aFree( code->script_buf );
  2112. aFree( code );
  2113. }
  2114. //
  2115. // 実行部main
  2116. //
  2117. /*==========================================
  2118. * コマンドの読み取り
  2119. *------------------------------------------*/
  2120. static int unget_com_data=-1;
  2121. int get_com(unsigned char *script,int *pos)
  2122. {
  2123. int i,j;
  2124. if(unget_com_data>=0){
  2125. i=unget_com_data;
  2126. unget_com_data=-1;
  2127. return i;
  2128. }
  2129. if(script[*pos]>=0x80){
  2130. return C_INT;
  2131. }
  2132. i=0; j=0;
  2133. while(script[*pos]>=0x40){
  2134. i=script[(*pos)++]<<j;
  2135. j+=6;
  2136. }
  2137. return i+(script[(*pos)++]<<j);
  2138. }
  2139. /*==========================================
  2140. * コマンドのプッシュバック
  2141. *------------------------------------------*/
  2142. void unget_com(int c)
  2143. {
  2144. if(unget_com_data!=-1){
  2145. if(battle_config.error_log)
  2146. ShowError("unget_com can back only 1 data\n");
  2147. }
  2148. unget_com_data=c;
  2149. }
  2150. /*==========================================
  2151. * 数値の所得
  2152. *------------------------------------------*/
  2153. int get_num(unsigned char *script,int *pos)
  2154. {
  2155. int i,j;
  2156. i=0; j=0;
  2157. while(script[*pos]>=0xc0){
  2158. i+=(script[(*pos)++]&0x7f)<<j;
  2159. j+=6;
  2160. }
  2161. return i+((script[(*pos)++]&0x7f)<<j);
  2162. }
  2163. /*==========================================
  2164. * スタックから値を取り出す
  2165. *------------------------------------------*/
  2166. int pop_val(struct script_state* st)
  2167. {
  2168. if(st->stack->sp<=0)
  2169. return 0;
  2170. st->stack->sp--;
  2171. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2172. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2173. return st->stack->stack_data[st->stack->sp].u.num;
  2174. return 0;
  2175. }
  2176. int isstr(struct script_data *c)
  2177. {
  2178. if( data_isstring(c) )
  2179. return 1;
  2180. else if( data_isreference(c) ) {
  2181. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2182. char postfix = p[strlen(p)-1];
  2183. return (postfix == '$');
  2184. }
  2185. return 0;
  2186. }
  2187. /// Ternary operators
  2188. /// test ? if_true : if_false
  2189. void op_3(struct script_state* st, int op)
  2190. {
  2191. struct script_data* data;
  2192. int flag = 0;
  2193. data = script_getdatatop(st, -3);
  2194. get_val(st, data);
  2195. if( data_isstring(data) )
  2196. flag = data->u.str[0];
  2197. else if( data_isint(data) )
  2198. flag = data->u.num;
  2199. else
  2200. {
  2201. ShowError("script:op_3: invalid type of data op:%d data:%d\n", op, data->type);
  2202. report_src(st);
  2203. script_removetop(st, -3, 0);
  2204. script_pushnil(st);
  2205. return;
  2206. }
  2207. if( flag )
  2208. script_pushcopytop(st, -2);
  2209. else
  2210. script_pushcopytop(st, -1);
  2211. script_removetop(st, -4, -1);
  2212. }
  2213. /// Binary string operators
  2214. /// s1 EQ s2 -> i
  2215. /// s1 NE s2 -> i
  2216. /// s1 GT s2 -> i
  2217. /// s1 GE s2 -> i
  2218. /// s1 LT s2 -> i
  2219. /// s1 LE s2 -> i
  2220. /// s1 ADD s2 -> s
  2221. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2222. {
  2223. int a = 0;
  2224. switch(op){
  2225. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2226. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2227. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2228. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2229. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2230. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2231. case C_ADD:
  2232. {
  2233. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2234. strcpy(buf, s1);
  2235. strcat(buf, s2);
  2236. script_pushstr(st, buf);
  2237. return;
  2238. }
  2239. default:
  2240. ShowError("script:op2_str: unexpected string operator op:%d\n", op);
  2241. report_src(st);
  2242. script_pushnil(st);
  2243. st->state = END;
  2244. return;
  2245. }
  2246. script_pushint(st,a);
  2247. }
  2248. /// Binary number operators
  2249. /// i OP i -> i
  2250. void op_2num(struct script_state* st, int op, int i1, int i2)
  2251. {
  2252. int ret;
  2253. double ret_double;
  2254. switch( op )
  2255. {
  2256. case C_AND: ret = i1 & i2; break;
  2257. case C_OR: ret = i1 | i2; break;
  2258. case C_XOR: ret = i1 ^ i2; break;
  2259. case C_LAND: ret = (i1 && i2); break;
  2260. case C_LOR: ret = (i1 || i2); break;
  2261. case C_EQ: ret = (i1 == i2); break;
  2262. case C_NE: ret = (i1 != i2); break;
  2263. case C_GT: ret = (i1 > i2); break;
  2264. case C_GE: ret = (i1 >= i2); break;
  2265. case C_LT: ret = (i1 < i2); break;
  2266. case C_LE: ret = (i1 <= i2); break;
  2267. case C_R_SHIFT: ret = i1>>i2; break;
  2268. case C_L_SHIFT: ret = i1<<i2; break;
  2269. case C_DIV:
  2270. case C_MOD:
  2271. if( i2 == 0 )
  2272. {
  2273. ShowError("script:op_2num: division by zero detected op:%d\n", op);
  2274. report_src(st);
  2275. script_pushnil(st);
  2276. st->state = END;
  2277. return;
  2278. }
  2279. else if( op == C_DIV )
  2280. ret = i1 / i2;
  2281. else//if( op == C_MOD )
  2282. ret = i1 % i2;
  2283. break;
  2284. default:
  2285. switch( op )
  2286. {// operators that can overflow/underflow
  2287. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2288. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2289. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2290. default:
  2291. ShowError("script:op_2num: unexpected number operator op:%d\n", op);
  2292. report_src(st);
  2293. script_pushnil(st);
  2294. return;
  2295. }
  2296. if( ret_double < INT_MIN )
  2297. {
  2298. ShowWarning("script:op_2num: underflow detected op:%d\n", op);
  2299. report_src(st);
  2300. ret = INT_MIN;
  2301. }
  2302. else if( ret_double > INT_MAX )
  2303. {
  2304. ShowWarning("script:op_2num: overflow detected op:%d\n", op);
  2305. report_src(st);
  2306. ret = INT_MAX;
  2307. }
  2308. }
  2309. script_pushint(st, ret);
  2310. }
  2311. /// Binary operators
  2312. void op_2(struct script_state *st, int op)
  2313. {
  2314. struct script_data* left;
  2315. struct script_data* right;
  2316. left = script_getdatatop(st, -2);
  2317. right = script_getdatatop(st, -1);
  2318. get_val(st, left);
  2319. get_val(st, right);
  2320. // automatic conversions
  2321. switch( op )
  2322. {
  2323. case C_ADD:
  2324. if( data_isstring(left) || data_isstring(right) )
  2325. {// convert to string
  2326. conv_str(st, left);
  2327. conv_str(st, right);
  2328. }
  2329. break;
  2330. }
  2331. if( data_isstring(left) && data_isstring(right) )
  2332. {// ss => op_2str
  2333. op_2str(st, op, left->u.str, right->u.str);
  2334. script_removetop(st, -3, -1);// pop the two values before the top one
  2335. }
  2336. else if( data_isint(left) && data_isint(right) )
  2337. {// ii => op_2num
  2338. int i1 = left->u.num;
  2339. int i2 = right->u.num;
  2340. script_removetop(st, -2, 0);
  2341. op_2num(st, op, i1, i2);
  2342. }
  2343. else
  2344. {// invalid argument
  2345. ShowError("script:op_2: invalid type of data op:%d left:%d right:%d\n", op, left->type, right->type);
  2346. report_src(st);
  2347. script_removetop(st, -2, 0);
  2348. script_pushnil(st);
  2349. st->state = END;
  2350. }
  2351. }
  2352. /// Unary operators
  2353. /// NEG i -> i
  2354. /// NOT i -> i
  2355. /// LNOT i -> i
  2356. void op_1(struct script_state* st, int op)
  2357. {
  2358. struct script_data* data;
  2359. int i1;
  2360. data = script_getdatatop(st, -1);
  2361. get_val(st, data);
  2362. if( !data_isint(data) )
  2363. {// not a number
  2364. ShowError("script:op_1: invalid type of data op:%d data:%d\n", op, data->type);
  2365. report_src(st);
  2366. script_pushnil(st);
  2367. st->state = END;
  2368. return;
  2369. }
  2370. i1 = data->u.num;
  2371. script_removetop(st, -1, 0);
  2372. switch( op )
  2373. {
  2374. case C_NEG: i1 = -i1; break;
  2375. case C_NOT: i1 = ~i1; break;
  2376. case C_LNOT: i1 = !i1; break;
  2377. default:
  2378. ShowError("script:op_1: unexpected operator op:%d\n", op);
  2379. report_src(st);
  2380. script_pushnil(st);
  2381. st->state = END;
  2382. return;
  2383. }
  2384. script_pushint(st, i1);
  2385. }
  2386. /*==========================================
  2387. * 関数の実行
  2388. *------------------------------------------*/
  2389. int run_func(struct script_state *st)
  2390. {
  2391. int i,start_sp,end_sp,func;
  2392. end_sp=st->stack->sp;
  2393. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2394. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2395. if(battle_config.error_log)
  2396. ShowError("function not found\n");
  2397. // st->stack->sp=0;
  2398. st->state=END;
  2399. report_src(st);
  2400. return 1;
  2401. }
  2402. start_sp=i-1;
  2403. st->start=i-1;
  2404. st->end=end_sp;
  2405. func=st->stack->stack_data[st->start].u.num;
  2406. #ifdef DEBUG_RUN
  2407. if(battle_config.etc_log) {
  2408. 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);
  2409. ShowDebug("stack dump :");
  2410. for(i=0;i<end_sp;i++){
  2411. switch(st->stack->stack_data[i].type){
  2412. case C_INT:
  2413. printf(" int(%d)",st->stack->stack_data[i].u.num);
  2414. break;
  2415. case C_NAME:
  2416. printf(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2417. break;
  2418. case C_ARG:
  2419. printf(" arg");
  2420. break;
  2421. case C_POS:
  2422. printf(" pos(%d)",st->stack->stack_data[i].u.num);
  2423. break;
  2424. case C_STR:
  2425. printf(" str(%s)",st->stack->stack_data[i].u.str);
  2426. break;
  2427. case C_CONSTSTR:
  2428. printf(" cstr(%s)",st->stack->stack_data[i].u.str);
  2429. break;
  2430. default:
  2431. printf(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2432. }
  2433. }
  2434. printf("\n");
  2435. }
  2436. #endif
  2437. if(str_data[func].type!=C_FUNC ){
  2438. ShowMessage ("run_func: '"CL_WHITE"%s"CL_RESET"' (type %d) is not function and command!\n", str_buf+str_data[func].str, str_data[func].type);
  2439. // st->stack->sp=0;
  2440. st->state=END;
  2441. report_src(st);
  2442. return 1;
  2443. }
  2444. #ifdef DEBUG_RUN
  2445. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2446. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2447. );
  2448. #endif
  2449. if(str_data[func].func){
  2450. if (str_data[func].func(st)) //Report error
  2451. report_src(st);
  2452. } else {
  2453. if(battle_config.error_log)
  2454. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2455. script_pushint(st,0);
  2456. report_src(st);
  2457. }
  2458. // Stack's datum are used when re-run functions [Eoe]
  2459. if(st->state != RERUNLINE) {
  2460. pop_stack(st->stack,start_sp,end_sp);
  2461. }
  2462. if(st->state==RETFUNC){
  2463. // ユーザー定義関数からの復帰
  2464. int olddefsp=st->stack->defsp;
  2465. int i;
  2466. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2467. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2468. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2469. st->state=END;
  2470. report_src(st);
  2471. return 1;
  2472. }
  2473. script_free_vars( st->stack->var_function );
  2474. aFree(st->stack->var_function);
  2475. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2476. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2477. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2478. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2479. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2480. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2481. st->state=GOTO;
  2482. }
  2483. return 0;
  2484. }
  2485. /*==========================================
  2486. * スクリプトの実行
  2487. *------------------------------------------*/
  2488. void run_script_main(struct script_state *st);
  2489. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2490. {
  2491. struct script_state *st;
  2492. TBL_PC *sd=NULL;
  2493. if(rootscript==NULL || pos<0)
  2494. return;
  2495. if (rid) sd = map_id2sd(rid);
  2496. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2497. //Resume script.
  2498. st = sd->st;
  2499. } else {
  2500. st = aCalloc(sizeof(struct script_state), 1);
  2501. // the script is different, make new script_state and stack
  2502. st->stack = aMalloc (sizeof(struct script_stack));
  2503. st->stack->sp=0;
  2504. st->stack->sp_max=64;
  2505. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2506. st->stack->defsp = st->stack->sp;
  2507. st->stack->var_function = aCalloc(1, sizeof(struct linkdb_node*));
  2508. st->state = RUN;
  2509. st->script = rootscript;
  2510. }
  2511. st->pos = pos;
  2512. st->rid = rid;
  2513. st->oid = oid;
  2514. st->sleep.timer = -1;
  2515. run_script_main(st);
  2516. }
  2517. void script_stop_sleeptimers(int id)
  2518. {
  2519. struct script_state* st;
  2520. for(;;)
  2521. {
  2522. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2523. if( st == NULL )
  2524. break; // no more sleep timers
  2525. if( st->sleep.timer != INVALID_TIMER )
  2526. delete_timer(st->sleep.timer, run_script_timer);
  2527. script_free_stack(st->stack);
  2528. aFree(st);
  2529. }
  2530. }
  2531. /*==========================================
  2532. * 指定ノードをsleep_dbから削除
  2533. *------------------------------------------*/
  2534. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2535. {
  2536. struct linkdb_node *retnode;
  2537. if( n == NULL)
  2538. return NULL;
  2539. if( n->prev == NULL )
  2540. sleep_db = n->next;
  2541. else
  2542. n->prev->next = n->next;
  2543. if( n->next )
  2544. n->next->prev = n->prev;
  2545. retnode = n->next;
  2546. aFree( n );
  2547. return retnode; // 次のノードを返す
  2548. }
  2549. /*==========================================
  2550. * sleep用タイマー関数
  2551. *------------------------------------------*/
  2552. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2553. {
  2554. struct script_state *st = (struct script_state *)data;
  2555. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2556. TBL_PC *sd = map_id2sd(st->rid);
  2557. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2558. { //Character mismatch. Cancel execution.
  2559. st->rid = 0;
  2560. st->state = END;
  2561. }
  2562. while( node && st->sleep.timer != -1 ) {
  2563. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2564. script_erase_sleepdb(node);
  2565. st->sleep.timer = -1;
  2566. break;
  2567. }
  2568. node = node->next;
  2569. }
  2570. if(st->state != RERUNLINE)
  2571. st->sleep.tick = 0;
  2572. run_script_main(st);
  2573. return 0;
  2574. }
  2575. /*==========================================
  2576. * スクリプトの実行メイン部分
  2577. *------------------------------------------*/
  2578. void run_script_main(struct script_state *st)
  2579. {
  2580. int c;
  2581. int cmdcount=script_config.check_cmdcount;
  2582. int gotocount=script_config.check_gotocount;
  2583. TBL_PC *sd;
  2584. //For backing up purposes
  2585. struct script_state *bk_st = NULL;
  2586. int bk_npcid = 0;
  2587. struct script_stack *stack=st->stack;
  2588. sd = st->rid?map_id2sd(st->rid):NULL;
  2589. if(sd){
  2590. if(sd->st != st){
  2591. bk_st = sd->st;
  2592. bk_npcid = sd->npc_id;
  2593. }
  2594. sd->st = st;
  2595. sd->npc_id = st->oid;
  2596. }
  2597. if(st->state == RERUNLINE) {
  2598. st->state = RUN;
  2599. run_func(st);
  2600. if(st->state == GOTO)
  2601. st->state = RUN;
  2602. } else if(st->state != END)
  2603. st->state = RUN;
  2604. while(st->state == RUN){
  2605. c= get_com(st->script->script_buf,&st->pos);
  2606. switch(c){
  2607. case C_EOL:
  2608. if( stack->sp != stack->defsp )
  2609. {
  2610. if( stack->sp > stack->defsp )
  2611. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2612. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2613. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2614. } else if( battle_config.error_log )
  2615. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2616. stack->sp = stack->defsp;
  2617. }
  2618. break;
  2619. case C_INT:
  2620. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2621. break;
  2622. case C_POS:
  2623. case C_NAME:
  2624. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2625. st->pos+=3;
  2626. break;
  2627. case C_ARG:
  2628. push_val(stack,c,0);
  2629. break;
  2630. case C_STR:
  2631. push_str(stack,C_CONSTSTR,(st->script->script_buf+st->pos));
  2632. while(st->script->script_buf[st->pos++]);
  2633. break;
  2634. case C_FUNC:
  2635. run_func(st);
  2636. if(st->state==GOTO){
  2637. st->state = RUN;
  2638. if( gotocount>0 && (--gotocount)<=0 ){
  2639. ShowError("run_script: infinity loop !\n");
  2640. report_src(st);
  2641. st->state=END;
  2642. }
  2643. }
  2644. break;
  2645. case C_NEG:
  2646. case C_NOT:
  2647. case C_LNOT:
  2648. op_1(st ,c);
  2649. break;
  2650. case C_ADD:
  2651. case C_SUB:
  2652. case C_MUL:
  2653. case C_DIV:
  2654. case C_MOD:
  2655. case C_EQ:
  2656. case C_NE:
  2657. case C_GT:
  2658. case C_GE:
  2659. case C_LT:
  2660. case C_LE:
  2661. case C_AND:
  2662. case C_OR:
  2663. case C_XOR:
  2664. case C_LAND:
  2665. case C_LOR:
  2666. case C_R_SHIFT:
  2667. case C_L_SHIFT:
  2668. op_2(st, c);
  2669. break;
  2670. case C_OP3:
  2671. op_3(st, c);
  2672. break;
  2673. case C_NOP:
  2674. st->state=END;
  2675. break;
  2676. default:
  2677. if(battle_config.error_log)
  2678. ShowError("unknown command : %d @ %d\n",c,pos);
  2679. st->state=END;
  2680. break;
  2681. }
  2682. if( cmdcount>0 && (--cmdcount)<=0 ){
  2683. ShowError("run_script: infinity loop !\n");
  2684. report_src(st);
  2685. st->state=END;
  2686. }
  2687. }
  2688. if(st->sleep.tick > 0) {
  2689. //Delay execution
  2690. st->sleep.charid = sd?sd->status.char_id:0;
  2691. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2692. run_script_timer, st->sleep.charid, (int)st);
  2693. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2694. //Restore previous script
  2695. if (sd) {
  2696. sd->st = bk_st;
  2697. sd->npc_id = bk_npcid;
  2698. bk_st = NULL; //Remove tag for removal.
  2699. }
  2700. }
  2701. else if(st->state != END && sd){
  2702. //Resume later (st is already attached to player).
  2703. if(bk_st) {
  2704. ShowWarning("Unable to restore stack! Double continuation!\n");
  2705. //Report BOTH scripts to see if that can help somehow.
  2706. ShowDebug("Previous script (lost):\n");
  2707. report_src(bk_st);
  2708. ShowDebug("Current script:\n");
  2709. report_src(st);
  2710. }
  2711. } else {
  2712. //Dispose of script.
  2713. if (sd)
  2714. { //Restore previous stack and save char.
  2715. if(sd->state.using_fake_npc){
  2716. clif_clearchar_id(sd->npc_id, 0, sd->fd);
  2717. sd->state.using_fake_npc = 0;
  2718. }
  2719. //Restore previous script if any.
  2720. sd->st = bk_st;
  2721. sd->npc_id = bk_npcid;
  2722. if (!bk_st)
  2723. npc_event_dequeue(sd);
  2724. else
  2725. bk_st = NULL; //Remove tag for removal.
  2726. if (sd->state.reg_dirty&2)
  2727. intif_saveregistry(sd,2);
  2728. if (sd->state.reg_dirty&1)
  2729. intif_saveregistry(sd,1);
  2730. }
  2731. st->pos = -1;
  2732. script_free_stack (st->stack);
  2733. aFree(st);
  2734. }
  2735. if (bk_st)
  2736. { //Remove previous script
  2737. bk_st->pos = -1;
  2738. script_free_stack(bk_st->stack);
  2739. aFree(bk_st);
  2740. bk_st = NULL;
  2741. }
  2742. }
  2743. /*==========================================
  2744. * マップ変数の変更
  2745. *------------------------------------------*/
  2746. int mapreg_setreg(int num,int val)
  2747. {
  2748. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2749. int i=num>>24;
  2750. char *name=str_buf+str_data[num&0x00ffffff].str;
  2751. char tmp_str[64];
  2752. #endif
  2753. if(val!=0) {
  2754. if(idb_put(mapreg_db,num,(void*)val))
  2755. ;
  2756. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2757. else if(name[1] != '@') {
  2758. sprintf(tmp_sql,"INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%d')",mapregsql_db,mapregsql_db_varname,mapregsql_db_index,mapregsql_db_value,jstrescapecpy(tmp_str,name),i,val);
  2759. if(mysql_query(&mmysql_handle,tmp_sql)){
  2760. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2761. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2762. }
  2763. }
  2764. #endif
  2765. } else { // [zBuffer]
  2766. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2767. if(name[1] != '@') { // Remove from database because it is unused.
  2768. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_varname,name,mapregsql_db_index,i);
  2769. if(mysql_query(&mmysql_handle,tmp_sql)){
  2770. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2771. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2772. }
  2773. }
  2774. #endif
  2775. idb_remove(mapreg_db,num);
  2776. }
  2777. mapreg_dirty=1;
  2778. return 1;
  2779. }
  2780. /*==========================================
  2781. * 文字列型マップ変数の変更
  2782. *------------------------------------------*/
  2783. int mapreg_setregstr(int num,const char *str)
  2784. {
  2785. char *p;
  2786. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2787. char tmp_str[64];
  2788. char tmp_str2[512];
  2789. int i=num>>24; // [zBuffer]
  2790. char *name=str_buf+str_data[num&0x00ffffff].str;
  2791. #endif
  2792. if( str==NULL || *str==0 ){
  2793. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2794. if(name[1] != '@') {
  2795. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_varname,name,mapregsql_db_index,i);
  2796. if(mysql_query(&mmysql_handle,tmp_sql)){
  2797. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2798. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2799. }
  2800. }
  2801. #endif
  2802. idb_remove(mapregstr_db,num);
  2803. mapreg_dirty=1;
  2804. return 1;
  2805. }
  2806. p=(char *)aMallocA((strlen(str)+1)*sizeof(char));
  2807. strcpy(p,str);
  2808. if (idb_put(mapregstr_db,num,p))
  2809. ;
  2810. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2811. else if(name[1] != '@'){ //put returned null, so we must insert.
  2812. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  2813. sprintf(tmp_sql,"INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%s')",mapregsql_db,mapregsql_db_varname,mapregsql_db_index,mapregsql_db_value,jstrescapecpy(tmp_str,name),i,jstrescapecpy(tmp_str2,p));
  2814. if(mysql_query(&mmysql_handle,tmp_sql)){
  2815. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2816. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2817. }
  2818. }
  2819. #endif
  2820. mapreg_dirty=1;
  2821. return 1;
  2822. }
  2823. /*==========================================
  2824. * 永続的マップ変数の読み込み
  2825. *------------------------------------------*/
  2826. static int script_load_mapreg(void)
  2827. {
  2828. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2829. FILE *fp;
  2830. char line[1024];
  2831. if( (fp=fopen(mapreg_txt,"rt"))==NULL )
  2832. return -1;
  2833. while(fgets(line,sizeof(line),fp))
  2834. {
  2835. char buf1[256],buf2[1024],*p;
  2836. int n,v,s,i;
  2837. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  2838. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  2839. continue;
  2840. if( buf1[strlen(buf1)-1]=='$' ){
  2841. if( sscanf(line+n,"%[^\n\r]",buf2)!=1 ){
  2842. ShowError("%s: %s broken data !\n",mapreg_txt,buf1);
  2843. continue;
  2844. }
  2845. p=(char *)aMallocA((strlen(buf2) + 1)*sizeof(char));
  2846. strcpy(p,buf2);
  2847. s= add_str(buf1);
  2848. idb_put(mapregstr_db,(i<<24)|s,p);
  2849. }else{
  2850. if( sscanf(line+n,"%d",&v)!=1 ){
  2851. ShowError("%s: %s broken data !\n",mapreg_txt,buf1);
  2852. continue;
  2853. }
  2854. s= add_str(buf1);
  2855. idb_put(mapreg_db,(i<<24)|s,(void*)v);
  2856. }
  2857. }
  2858. fclose(fp);
  2859. mapreg_dirty=0;
  2860. return 0;
  2861. #else
  2862. // SQL mapreg code start [zBuffer]
  2863. /*
  2864. 0 1 2
  2865. +-------------------------+
  2866. | varname | index | value |
  2867. +-------------------------+
  2868. */
  2869. unsigned int perfomance = (unsigned int)time(NULL);
  2870. sprintf(tmp_sql,"SELECT * FROM `%s`",mapregsql_db);
  2871. ShowInfo("Querying script_load_mapreg ...\n");
  2872. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  2873. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2874. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2875. return -1;
  2876. }
  2877. ShowInfo("Success! Returning results ...\n");
  2878. sql_res = mysql_store_result(&mmysql_handle);
  2879. if (sql_res) {
  2880. while ((sql_row = mysql_fetch_row(sql_res))) {
  2881. char buf1[33], *p = NULL;
  2882. int i,v,s;
  2883. strcpy(buf1,sql_row[0]);
  2884. if( buf1[strlen(buf1)-1]=='$' ){
  2885. i = atoi(sql_row[1]);
  2886. p=(char *)aMallocA((strlen(sql_row[2]) + 1)*sizeof(char));
  2887. strcpy(p,sql_row[2]);
  2888. s= add_str(buf1);
  2889. idb_put(mapregstr_db,(i<<24)|s,p);
  2890. }else{
  2891. s= add_str(buf1);
  2892. v= atoi(sql_row[2]);
  2893. i = atoi(sql_row[1]);
  2894. idb_put(mapreg_db,(i<<24)|s,(void *)v);
  2895. }
  2896. }
  2897. }
  2898. ShowInfo("Freeing results...\n");
  2899. mysql_free_result(sql_res);
  2900. mapreg_dirty=0;
  2901. perfomance = (((unsigned int)time(NULL)) - perfomance);
  2902. ShowInfo("SQL Mapreg Loading Completed Under %d Seconds.\n",perfomance);
  2903. return 0;
  2904. #endif /* TXT_ONLY */
  2905. }
  2906. /*==========================================
  2907. * 永続的マップ変数の書き込み
  2908. *------------------------------------------*/
  2909. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  2910. {
  2911. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2912. FILE *fp=va_arg(ap,FILE*);
  2913. int num=key.i&0x00ffffff, i=key.i>>24;
  2914. char *name=str_buf+str_data[num].str;
  2915. if( name[1]!='@' ){
  2916. if(i==0)
  2917. fprintf(fp,"%s\t%d\n", name, (int)data);
  2918. else
  2919. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  2920. }
  2921. return 0;
  2922. #else
  2923. int num=key.i&0x00ffffff, i=key.i>>24; // [zBuffer]
  2924. char *name=str_buf+str_data[num].str;
  2925. if ( name[1] != '@') {
  2926. sprintf(tmp_sql,"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);
  2927. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  2928. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2929. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2930. }
  2931. }
  2932. return 0;
  2933. #endif
  2934. }
  2935. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  2936. {
  2937. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2938. FILE *fp=va_arg(ap,FILE*);
  2939. int num=key.i&0x00ffffff, i=key.i>>24;
  2940. char *name=str_buf+str_data[num].str;
  2941. if( name[1]!='@' ){
  2942. if(i==0)
  2943. fprintf(fp,"%s\t%s\n", name, (char *)data);
  2944. else
  2945. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  2946. }
  2947. return 0;
  2948. #else
  2949. char tmp_str2[512];
  2950. int num=key.i&0x00ffffff, i=key.i>>24;
  2951. char *name=str_buf+str_data[num].str;
  2952. if ( name[1] != '@') {
  2953. sprintf(tmp_sql,"UPDATE `%s` SET `%s`='%s' WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_value,jstrescapecpy(tmp_str2,(char *)data),mapregsql_db_varname,name,mapregsql_db_index,i);
  2954. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  2955. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2956. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2957. }
  2958. }
  2959. return 0;
  2960. #endif
  2961. }
  2962. static int script_save_mapreg(void)
  2963. {
  2964. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2965. FILE *fp;
  2966. int lock;
  2967. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  2968. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  2969. return -1;
  2970. }
  2971. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  2972. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  2973. lock_fclose(fp,mapreg_txt,&lock);
  2974. #else
  2975. unsigned int perfomance = (unsigned int)time(NULL);
  2976. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub); // [zBuffer]
  2977. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  2978. perfomance = ((unsigned int)time(NULL) - perfomance);
  2979. if(perfomance > 2)
  2980. ShowWarning("Slow Query: MapregSQL Saving @ %d second(s).\n", perfomance);
  2981. #endif
  2982. mapreg_dirty=0;
  2983. return 0;
  2984. }
  2985. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  2986. {
  2987. if(mapreg_dirty)
  2988. if (script_save_mapreg() == -1)
  2989. ShowError("Failed to save the mapreg data!\n");
  2990. return 0;
  2991. }
  2992. /*==========================================
  2993. *
  2994. *------------------------------------------*/
  2995. static int set_posword(char *p)
  2996. {
  2997. char* np,* str[15];
  2998. int i=0;
  2999. for(i=0;i<11;i++) {
  3000. if((np=strchr(p,','))!=NULL) {
  3001. str[i]=p;
  3002. *np=0;
  3003. p=np+1;
  3004. } else {
  3005. str[i]=p;
  3006. p+=strlen(p);
  3007. }
  3008. if(str[i])
  3009. strcpy(pos[i],str[i]);
  3010. }
  3011. return 0;
  3012. }
  3013. int script_config_read_sub(char *cfgName)
  3014. {
  3015. int i;
  3016. char line[1024],w1[1024],w2[1024];
  3017. FILE *fp;
  3018. fp=fopen(cfgName,"r");
  3019. if(fp==NULL){
  3020. ShowError("file not found: [%s]\n", cfgName);
  3021. return 1;
  3022. }
  3023. while(fgets(line, sizeof(line), fp))
  3024. {
  3025. if(line[0] == '/' && line[1] == '/')
  3026. continue;
  3027. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3028. if(i!=2)
  3029. continue;
  3030. if(strcmpi(w1,"refine_posword")==0) {
  3031. set_posword(w2);
  3032. }
  3033. else if(strcmpi(w1,"verbose_mode")==0) {
  3034. script_config.verbose_mode = config_switch(w2);
  3035. }
  3036. else if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3037. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3038. }
  3039. else if(strcmpi(w1,"check_cmdcount")==0) {
  3040. script_config.check_cmdcount = config_switch(w2);
  3041. }
  3042. else if(strcmpi(w1,"check_gotocount")==0) {
  3043. script_config.check_gotocount = config_switch(w2);
  3044. }
  3045. else if(strcmpi(w1,"event_script_type")==0) {
  3046. script_config.event_script_type = config_switch(w2);
  3047. }
  3048. else if(strcmpi(w1,"event_requires_trigger")==0) {
  3049. script_config.event_requires_trigger = config_switch(w2);
  3050. }
  3051. else if(strcmpi(w1,"die_event_name")==0) {
  3052. strncpy(script_config.die_event_name, w2, NAME_LENGTH-1);
  3053. if (strlen(script_config.die_event_name) != strlen(w2))
  3054. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.die_event_name);
  3055. }
  3056. else if(strcmpi(w1,"kill_pc_event_name")==0) {
  3057. strncpy(script_config.kill_pc_event_name, w2, NAME_LENGTH-1);
  3058. if (strlen(script_config.kill_pc_event_name) != strlen(w2))
  3059. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_pc_event_name);
  3060. }
  3061. else if(strcmpi(w1,"kill_mob_event_name")==0) {
  3062. strncpy(script_config.kill_mob_event_name, w2, NAME_LENGTH-1);
  3063. if (strlen(script_config.kill_mob_event_name) != strlen(w2))
  3064. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_mob_event_name);
  3065. }
  3066. else if(strcmpi(w1,"login_event_name")==0) {
  3067. strncpy(script_config.login_event_name, w2, NAME_LENGTH-1);
  3068. if (strlen(script_config.login_event_name) != strlen(w2))
  3069. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.login_event_name);
  3070. }
  3071. else if(strcmpi(w1,"logout_event_name")==0) {
  3072. strncpy(script_config.logout_event_name, w2, NAME_LENGTH-1);
  3073. if (strlen(script_config.logout_event_name) != strlen(w2))
  3074. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.logout_event_name);
  3075. }
  3076. else if(strcmpi(w1,"loadmap_event_name")==0) {
  3077. strncpy(script_config.loadmap_event_name, w2, NAME_LENGTH-1);
  3078. if (strlen(script_config.loadmap_event_name) != strlen(w2))
  3079. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.loadmap_event_name);
  3080. }
  3081. else if(strcmpi(w1,"baselvup_event_name")==0) {
  3082. strncpy(script_config.baselvup_event_name, w2, NAME_LENGTH-1);
  3083. if (strlen(script_config.baselvup_event_name) != strlen(w2))
  3084. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.baselvup_event_name);
  3085. }
  3086. else if(strcmpi(w1,"joblvup_event_name")==0) {
  3087. strncpy(script_config.joblvup_event_name, w2, NAME_LENGTH-1);
  3088. if (strlen(script_config.joblvup_event_name) != strlen(w2))
  3089. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.joblvup_event_name);
  3090. }
  3091. else if(strcmpi(w1,"import")==0){
  3092. script_config_read_sub(w2);
  3093. }
  3094. }
  3095. fclose(fp);
  3096. return 0;
  3097. }
  3098. int script_config_read(char *cfgName)
  3099. { //Script related variables should be initialized once! [Skotlex]
  3100. memset (&script_config, 0, sizeof(script_config));
  3101. script_config.verbose_mode = 0;
  3102. script_config.warn_func_mismatch_paramnum = 1;
  3103. script_config.check_cmdcount = 65535;
  3104. script_config.check_gotocount = 2048;
  3105. script_config.event_script_type = 0;
  3106. script_config.event_requires_trigger = 1;
  3107. return script_config_read_sub(cfgName);
  3108. }
  3109. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3110. {
  3111. struct script_code *code = (struct script_code *)data;
  3112. if(code){
  3113. script_free_vars( &code->script_vars );
  3114. aFree( code->script_buf );
  3115. }
  3116. return 0;
  3117. }
  3118. /*==========================================
  3119. * 終了
  3120. *------------------------------------------*/
  3121. int do_final_script()
  3122. {
  3123. #ifdef DEBUG_RUN
  3124. if (battle_config.etc_log)
  3125. {
  3126. FILE *fp = fopen("hash_dump.txt","wt");
  3127. if(fp) {
  3128. int i,count[SCRIPT_HASH_SIZE];
  3129. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3130. int n=0;
  3131. int min=INT_MAX,max=0,zero=0;
  3132. double mean=0.0f;
  3133. double median=0.0f;
  3134. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3135. memset(count, 0, sizeof(count));
  3136. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3137. fprintf(fp,"---------------------------------------------------------------\n");
  3138. for(i=LABEL_START; i<str_num; i++) {
  3139. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3140. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3141. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3142. ++count[h];
  3143. }
  3144. fprintf(fp,"--------------------\n\n");
  3145. memset(count2, 0, sizeof(count2));
  3146. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3147. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3148. if(min > count[i])
  3149. min = count[i]; // minimun count of collision
  3150. if(max < count[i])
  3151. max = count[i]; // maximun count of collision
  3152. if(count[i] == 0)
  3153. zero++;
  3154. ++count2[count[i]];
  3155. }
  3156. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3157. for( i=min; i <= max; ++i ){
  3158. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3159. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3160. }
  3161. for( i=min; i <= max; ++i ){
  3162. n += count2[i];
  3163. if( n*2 >= SCRIPT_HASH_SIZE )
  3164. {
  3165. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3166. median = (i+i+1)/2.0f;
  3167. else
  3168. median = i;
  3169. break;
  3170. }
  3171. }
  3172. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3173. fclose(fp);
  3174. }
  3175. }
  3176. #endif
  3177. if(mapreg_dirty>=0)
  3178. script_save_mapreg();
  3179. mapreg_db->destroy(mapreg_db,NULL);
  3180. mapregstr_db->destroy(mapregstr_db,NULL);
  3181. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3182. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3183. if(sleep_db) {
  3184. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3185. while(n) {
  3186. struct script_state *st = (struct script_state *)n->data;
  3187. script_free_stack(st->stack);
  3188. aFree(st);
  3189. n = n->next;
  3190. }
  3191. linkdb_final(&sleep_db);
  3192. }
  3193. if (str_data)
  3194. aFree(str_data);
  3195. if (str_buf)
  3196. aFree(str_buf);
  3197. return 0;
  3198. }
  3199. /*==========================================
  3200. * 初期化
  3201. *------------------------------------------*/
  3202. int do_init_script()
  3203. {
  3204. mapreg_db= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_BASE,sizeof(int));
  3205. mapregstr_db=db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3206. userfunc_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_RELEASE_BOTH,50);
  3207. scriptlabel_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA,50);
  3208. script_load_mapreg();
  3209. add_timer_func_list(script_autosave_mapreg,"script_autosave_mapreg");
  3210. add_timer_interval(gettick()+MAPREG_AUTOSAVE_INTERVAL,
  3211. script_autosave_mapreg,0,0,MAPREG_AUTOSAVE_INTERVAL);
  3212. return 0;
  3213. }
  3214. int script_reload()
  3215. {
  3216. if(mapreg_dirty>=0)
  3217. script_save_mapreg();
  3218. mapreg_db->clear(mapreg_db, NULL);
  3219. mapregstr_db->clear(mapregstr_db, NULL);
  3220. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3221. scriptlabel_db->clear(scriptlabel_db, NULL);
  3222. if(sleep_db) {
  3223. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3224. while(n) {
  3225. struct script_state *st = (struct script_state *)n->data;
  3226. if( st->sleep.timer != INVALID_TIMER )
  3227. delete_timer(st->sleep.timer, run_script_timer);
  3228. script_free_stack(st->stack);
  3229. aFree(st);
  3230. n = n->next;
  3231. }
  3232. linkdb_final(&sleep_db);
  3233. }
  3234. script_load_mapreg();
  3235. return 0;
  3236. }
  3237. //-----------------------------------------------------------------------------
  3238. // buildin functions
  3239. //
  3240. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3241. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3242. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3243. BUILDIN_FUNC(mes);
  3244. BUILDIN_FUNC(goto);
  3245. BUILDIN_FUNC(callsub);
  3246. BUILDIN_FUNC(callfunc);
  3247. BUILDIN_FUNC(return);
  3248. BUILDIN_FUNC(getarg);
  3249. BUILDIN_FUNC(next);
  3250. BUILDIN_FUNC(close);
  3251. BUILDIN_FUNC(close2);
  3252. BUILDIN_FUNC(menu);
  3253. BUILDIN_FUNC(rand);
  3254. BUILDIN_FUNC(warp);
  3255. BUILDIN_FUNC(areawarp);
  3256. BUILDIN_FUNC(warpchar); // [LuzZza]
  3257. BUILDIN_FUNC(warpparty); //[Fredzilla]
  3258. BUILDIN_FUNC(warpguild); //[Fredzilla]
  3259. BUILDIN_FUNC(heal);
  3260. BUILDIN_FUNC(itemheal);
  3261. BUILDIN_FUNC(percentheal);
  3262. BUILDIN_FUNC(jobchange);
  3263. BUILDIN_FUNC(jobname);
  3264. BUILDIN_FUNC(input);
  3265. BUILDIN_FUNC(setlook);
  3266. BUILDIN_FUNC(set);
  3267. BUILDIN_FUNC(setarray);
  3268. BUILDIN_FUNC(cleararray);
  3269. BUILDIN_FUNC(copyarray);
  3270. BUILDIN_FUNC(getarraysize);
  3271. BUILDIN_FUNC(deletearray);
  3272. BUILDIN_FUNC(getelementofarray);
  3273. BUILDIN_FUNC(getitem);
  3274. BUILDIN_FUNC(getitem2);
  3275. BUILDIN_FUNC(getnameditem);
  3276. BUILDIN_FUNC(grouprandomitem);
  3277. BUILDIN_FUNC(makeitem);
  3278. BUILDIN_FUNC(delitem);
  3279. BUILDIN_FUNC(delitem2);
  3280. BUILDIN_FUNC(enableitemuse);
  3281. BUILDIN_FUNC(disableitemuse);
  3282. BUILDIN_FUNC(viewpoint);
  3283. BUILDIN_FUNC(countitem);
  3284. BUILDIN_FUNC(countitem2);
  3285. BUILDIN_FUNC(checkweight);
  3286. BUILDIN_FUNC(readparam);
  3287. BUILDIN_FUNC(getcharid);
  3288. BUILDIN_FUNC(getpartyname);
  3289. BUILDIN_FUNC(getpartymember);
  3290. BUILDIN_FUNC(getpartyleader);
  3291. BUILDIN_FUNC(getguildname);
  3292. BUILDIN_FUNC(getguildmaster);
  3293. BUILDIN_FUNC(getguildmasterid);
  3294. BUILDIN_FUNC(strcharinfo);
  3295. BUILDIN_FUNC(getequipid);
  3296. BUILDIN_FUNC(getequipname);
  3297. BUILDIN_FUNC(getbrokenid); // [Valaris]
  3298. BUILDIN_FUNC(repair); // [Valaris]
  3299. BUILDIN_FUNC(getequipisequiped);
  3300. BUILDIN_FUNC(getequipisenableref);
  3301. BUILDIN_FUNC(getequipisidentify);
  3302. BUILDIN_FUNC(getequiprefinerycnt);
  3303. BUILDIN_FUNC(getequipweaponlv);
  3304. BUILDIN_FUNC(getequippercentrefinery);
  3305. BUILDIN_FUNC(successrefitem);
  3306. BUILDIN_FUNC(failedrefitem);
  3307. BUILDIN_FUNC(cutin);
  3308. BUILDIN_FUNC(statusup);
  3309. BUILDIN_FUNC(statusup2);
  3310. BUILDIN_FUNC(bonus);
  3311. BUILDIN_FUNC(bonus2);
  3312. BUILDIN_FUNC(bonus3);
  3313. BUILDIN_FUNC(bonus4);
  3314. BUILDIN_FUNC(bonus5);
  3315. BUILDIN_FUNC(skill);
  3316. BUILDIN_FUNC(addtoskill); // [Valaris]
  3317. BUILDIN_FUNC(guildskill);
  3318. BUILDIN_FUNC(getskilllv);
  3319. BUILDIN_FUNC(getgdskilllv);
  3320. BUILDIN_FUNC(basicskillcheck);
  3321. BUILDIN_FUNC(getgmlevel);
  3322. BUILDIN_FUNC(end);
  3323. BUILDIN_FUNC(checkoption);
  3324. BUILDIN_FUNC(setoption);
  3325. BUILDIN_FUNC(setcart);
  3326. BUILDIN_FUNC(checkcart); // check cart [Valaris]
  3327. BUILDIN_FUNC(setfalcon);
  3328. BUILDIN_FUNC(checkfalcon); // check falcon [Valaris]
  3329. BUILDIN_FUNC(setriding);
  3330. BUILDIN_FUNC(checkriding); // check for pecopeco [Valaris]
  3331. BUILDIN_FUNC(savepoint);
  3332. BUILDIN_FUNC(gettimetick);
  3333. BUILDIN_FUNC(gettime);
  3334. BUILDIN_FUNC(gettimestr);
  3335. BUILDIN_FUNC(openstorage);
  3336. BUILDIN_FUNC(guildopenstorage);
  3337. BUILDIN_FUNC(itemskill);
  3338. BUILDIN_FUNC(produce);
  3339. BUILDIN_FUNC(monster);
  3340. BUILDIN_FUNC(areamonster);
  3341. BUILDIN_FUNC(killmonster);
  3342. BUILDIN_FUNC(killmonsterall);
  3343. BUILDIN_FUNC(clone);
  3344. BUILDIN_FUNC(doevent);
  3345. BUILDIN_FUNC(donpcevent);
  3346. BUILDIN_FUNC(addtimer);
  3347. BUILDIN_FUNC(deltimer);
  3348. BUILDIN_FUNC(addtimercount);
  3349. BUILDIN_FUNC(initnpctimer);
  3350. BUILDIN_FUNC(stopnpctimer);
  3351. BUILDIN_FUNC(startnpctimer);
  3352. BUILDIN_FUNC(setnpctimer);
  3353. BUILDIN_FUNC(getnpctimer);
  3354. BUILDIN_FUNC(attachnpctimer); // [celest]
  3355. BUILDIN_FUNC(detachnpctimer); // [celest]
  3356. BUILDIN_FUNC(playerattached); // [Skotlex]
  3357. BUILDIN_FUNC(announce);
  3358. BUILDIN_FUNC(mapannounce);
  3359. BUILDIN_FUNC(areaannounce);
  3360. BUILDIN_FUNC(getusers);
  3361. BUILDIN_FUNC(getmapguildusers);
  3362. BUILDIN_FUNC(getmapusers);
  3363. BUILDIN_FUNC(getareausers);
  3364. BUILDIN_FUNC(getareadropitem);
  3365. BUILDIN_FUNC(enablenpc);
  3366. BUILDIN_FUNC(disablenpc);
  3367. BUILDIN_FUNC(enablearena); // Added by RoVeRT
  3368. BUILDIN_FUNC(disablearena); // Added by RoVeRT
  3369. BUILDIN_FUNC(hideoffnpc);
  3370. BUILDIN_FUNC(hideonnpc);
  3371. BUILDIN_FUNC(sc_start);
  3372. BUILDIN_FUNC(sc_start2);
  3373. BUILDIN_FUNC(sc_start4);
  3374. BUILDIN_FUNC(sc_end);
  3375. BUILDIN_FUNC(getscrate);
  3376. BUILDIN_FUNC(debugmes);
  3377. BUILDIN_FUNC(catchpet);
  3378. BUILDIN_FUNC(birthpet);
  3379. BUILDIN_FUNC(resetlvl);
  3380. BUILDIN_FUNC(resetstatus);
  3381. BUILDIN_FUNC(resetskill);
  3382. BUILDIN_FUNC(skillpointcount);
  3383. BUILDIN_FUNC(changebase);
  3384. BUILDIN_FUNC(changesex);
  3385. BUILDIN_FUNC(waitingroom);
  3386. BUILDIN_FUNC(delwaitingroom);
  3387. BUILDIN_FUNC(waitingroomkickall);
  3388. BUILDIN_FUNC(enablewaitingroomevent);
  3389. BUILDIN_FUNC(disablewaitingroomevent);
  3390. BUILDIN_FUNC(getwaitingroomstate);
  3391. BUILDIN_FUNC(warpwaitingpc);
  3392. BUILDIN_FUNC(attachrid);
  3393. BUILDIN_FUNC(detachrid);
  3394. BUILDIN_FUNC(isloggedin);
  3395. BUILDIN_FUNC(setmapflagnosave);
  3396. BUILDIN_FUNC(setmapflag);
  3397. BUILDIN_FUNC(removemapflag);
  3398. BUILDIN_FUNC(pvpon);
  3399. BUILDIN_FUNC(pvpoff);
  3400. BUILDIN_FUNC(gvgon);
  3401. BUILDIN_FUNC(gvgoff);
  3402. BUILDIN_FUNC(emotion);
  3403. BUILDIN_FUNC(maprespawnguildid);
  3404. BUILDIN_FUNC(agitstart); // <Agit>
  3405. BUILDIN_FUNC(agitend);
  3406. BUILDIN_FUNC(agitcheck); // <Agitcheck>
  3407. BUILDIN_FUNC(flagemblem); // Flag Emblem
  3408. BUILDIN_FUNC(getcastlename);
  3409. BUILDIN_FUNC(getcastledata);
  3410. BUILDIN_FUNC(setcastledata);
  3411. BUILDIN_FUNC(requestguildinfo);
  3412. BUILDIN_FUNC(getequipcardcnt);
  3413. BUILDIN_FUNC(successremovecards);
  3414. BUILDIN_FUNC(failedremovecards);
  3415. BUILDIN_FUNC(marriage);
  3416. BUILDIN_FUNC(wedding_effect);
  3417. BUILDIN_FUNC(divorce);
  3418. BUILDIN_FUNC(ispartneron); // MouseJstr
  3419. BUILDIN_FUNC(getpartnerid); // MouseJstr
  3420. BUILDIN_FUNC(getchildid); // Skotlex
  3421. BUILDIN_FUNC(getmotherid); // Lupus
  3422. BUILDIN_FUNC(getfatherid); // Lupus
  3423. BUILDIN_FUNC(warppartner); // MouseJstr
  3424. BUILDIN_FUNC(getitemname);
  3425. BUILDIN_FUNC(getitemslots);
  3426. BUILDIN_FUNC(makepet);
  3427. BUILDIN_FUNC(getexp);
  3428. BUILDIN_FUNC(getinventorylist);
  3429. BUILDIN_FUNC(getskilllist);
  3430. BUILDIN_FUNC(clearitem);
  3431. BUILDIN_FUNC(classchange);
  3432. BUILDIN_FUNC(misceffect);
  3433. BUILDIN_FUNC(soundeffect);
  3434. BUILDIN_FUNC(soundeffectall);
  3435. BUILDIN_FUNC(setcastledata);
  3436. BUILDIN_FUNC(mapwarp);
  3437. BUILDIN_FUNC(inittimer);
  3438. BUILDIN_FUNC(stoptimer);
  3439. BUILDIN_FUNC(cmdothernpc);
  3440. BUILDIN_FUNC(mobcount);
  3441. BUILDIN_FUNC(strmobinfo); // Script for displaying mob info [Valaris]
  3442. BUILDIN_FUNC(guardian); // Script for displaying mob info [Valaris]
  3443. BUILDIN_FUNC(guardianinfo); // Script for displaying mob info [Valaris]
  3444. BUILDIN_FUNC(petskillbonus); // petskillbonus [Valaris]
  3445. BUILDIN_FUNC(petrecovery); // pet skill for curing status [Valaris]
  3446. BUILDIN_FUNC(petloot); // pet looting [Valaris]
  3447. BUILDIN_FUNC(petheal); // pet healing [Valaris]
  3448. //BUILDIN_FUNC(petmag); // pet magnificat [Valaris]
  3449. BUILDIN_FUNC(petskillattack); // pet skill attacks [Skotlex]
  3450. BUILDIN_FUNC(petskillattack2); // pet skill attacks [Skotlex]
  3451. BUILDIN_FUNC(petskillsupport); // pet support skill [Valaris]
  3452. BUILDIN_FUNC(skilleffect); // skill effects [Celest]
  3453. BUILDIN_FUNC(npcskilleffect); // skill effects for npcs [Valaris]
  3454. BUILDIN_FUNC(specialeffect); // special effect script [Valaris]
  3455. BUILDIN_FUNC(specialeffect2); // special effect script [Valaris]
  3456. BUILDIN_FUNC(nude); // nude [Valaris]
  3457. BUILDIN_FUNC(atcommand); // [MouseJstr]
  3458. BUILDIN_FUNC(charcommand); // [MouseJstr]
  3459. BUILDIN_FUNC(movenpc); // [MouseJstr]
  3460. BUILDIN_FUNC(message); // [MouseJstr]
  3461. BUILDIN_FUNC(npctalk); // [Valaris]
  3462. BUILDIN_FUNC(hasitems); // [Valaris]
  3463. BUILDIN_FUNC(getlook); //Lorky [Lupus]
  3464. BUILDIN_FUNC(getsavepoint); //Lorky [Lupus]
  3465. BUILDIN_FUNC(npcspeed); // [Valaris]
  3466. BUILDIN_FUNC(npcwalkto); // [Valaris]
  3467. BUILDIN_FUNC(npcstop); // [Valaris]
  3468. BUILDIN_FUNC(getmapxy); //get map position for player/npc/pet/mob by Lorky [Lupus]
  3469. BUILDIN_FUNC(checkoption1); // [celest]
  3470. BUILDIN_FUNC(checkoption2); // [celest]
  3471. BUILDIN_FUNC(guildgetexp); // [celest]
  3472. BUILDIN_FUNC(guildchangegm); // [Skotlex]
  3473. BUILDIN_FUNC(logmes); // [Lupus]
  3474. BUILDIN_FUNC(summon); // [celest]
  3475. BUILDIN_FUNC(isnight); // [celest]
  3476. BUILDIN_FUNC(isday); // [celest]
  3477. BUILDIN_FUNC(isequipped); // [celest]
  3478. BUILDIN_FUNC(isequippedcnt); // [celest]
  3479. BUILDIN_FUNC(cardscnt); // [Lupus]
  3480. BUILDIN_FUNC(getrefine); // [celest]
  3481. BUILDIN_FUNC(adopt);
  3482. BUILDIN_FUNC(night);
  3483. BUILDIN_FUNC(day);
  3484. BUILDIN_FUNC(getusersname); //jA commands added [Lupus]
  3485. BUILDIN_FUNC(dispbottom);
  3486. BUILDIN_FUNC(recovery);
  3487. BUILDIN_FUNC(getpetinfo);
  3488. BUILDIN_FUNC(checkequipedcard);
  3489. BUILDIN_FUNC(globalmes);
  3490. BUILDIN_FUNC(jump_zero);
  3491. BUILDIN_FUNC(select);
  3492. BUILDIN_FUNC(prompt);
  3493. BUILDIN_FUNC(getmapmobs); //jA addition end
  3494. BUILDIN_FUNC(unequip); // unequip [Spectre]
  3495. BUILDIN_FUNC(getstrlen); //strlen [valaris]
  3496. BUILDIN_FUNC(charisalpha);//isalpha [valaris]
  3497. BUILDIN_FUNC(fakenpcname); // [Lance]
  3498. BUILDIN_FUNC(compare); // Lordalfa, to bring strstr to Scripting Engine
  3499. BUILDIN_FUNC(getiteminfo); //[Lupus] returns Items Buy / sell Price, etc info
  3500. BUILDIN_FUNC(setiteminfo); //[Lupus] set Items Buy / sell Price, etc info
  3501. BUILDIN_FUNC(getequipcardid); //[Lupus] returns card id from quipped item card slot N
  3502. // [zBuffer] List of mathematics commands --->
  3503. BUILDIN_FUNC(sqrt);
  3504. BUILDIN_FUNC(pow);
  3505. BUILDIN_FUNC(distance);
  3506. BUILDIN_FUNC(checkcell);
  3507. // <--- [zBuffer] List of mathematics commands
  3508. // [zBuffer] List of dynamic var commands --->
  3509. BUILDIN_FUNC(getd);
  3510. BUILDIN_FUNC(setd);
  3511. // <--- [zBuffer] List of dynamic var commands
  3512. BUILDIN_FUNC(petstat); // [Lance] Pet Stat Rq: Dubby
  3513. BUILDIN_FUNC(callshop); // [Skotlex]
  3514. BUILDIN_FUNC(npcshopitem); // [Lance]
  3515. BUILDIN_FUNC(npcshopadditem);
  3516. BUILDIN_FUNC(npcshopdelitem);
  3517. BUILDIN_FUNC(npcshopattach);
  3518. BUILDIN_FUNC(equip);
  3519. BUILDIN_FUNC(autoequip);
  3520. BUILDIN_FUNC(setbattleflag);
  3521. BUILDIN_FUNC(getbattleflag);
  3522. BUILDIN_FUNC(query_sql);
  3523. BUILDIN_FUNC(escape_sql);
  3524. BUILDIN_FUNC(atoi);
  3525. BUILDIN_FUNC(axtoi);
  3526. // [zBuffer] List of player cont commands --->
  3527. BUILDIN_FUNC(rid2name);
  3528. BUILDIN_FUNC(pcfollow);
  3529. BUILDIN_FUNC(pcstopfollow);
  3530. BUILDIN_FUNC(pcblockmove);
  3531. // <--- [zBuffer] List of player cont commands
  3532. // [zBuffer] List of mob control commands --->
  3533. BUILDIN_FUNC(mobspawn);
  3534. BUILDIN_FUNC(mobremove);
  3535. BUILDIN_FUNC(getmobdata);
  3536. BUILDIN_FUNC(setmobdata);
  3537. BUILDIN_FUNC(mobassist);
  3538. BUILDIN_FUNC(mobattach);
  3539. BUILDIN_FUNC(unitwalk);
  3540. BUILDIN_FUNC(unitkill);
  3541. BUILDIN_FUNC(unitwarp);
  3542. BUILDIN_FUNC(unitattack);
  3543. BUILDIN_FUNC(unitstop);
  3544. BUILDIN_FUNC(unittalk);
  3545. BUILDIN_FUNC(unitemote);
  3546. BUILDIN_FUNC(unitdeadsit);
  3547. BUILDIN_FUNC(unitskilluseid); // originally by Qamera [celest]
  3548. BUILDIN_FUNC(unitskillusepos); // originally by Qamera [celest]
  3549. // <--- [zBuffer] List of mob control commands
  3550. BUILDIN_FUNC(sleep);
  3551. BUILDIN_FUNC(sleep2);
  3552. BUILDIN_FUNC(awake);
  3553. BUILDIN_FUNC(getvariableofnpc);
  3554. BUILDIN_FUNC(warpportal);
  3555. BUILDIN_FUNC(homunculus_evolution) ; //[orn]
  3556. BUILDIN_FUNC(eaclass);
  3557. BUILDIN_FUNC(roclass);
  3558. BUILDIN_FUNC(setitemscript);
  3559. BUILDIN_FUNC(disguise);
  3560. BUILDIN_FUNC(undisguise);
  3561. BUILDIN_FUNC(getmonsterinfo); // [Lupus]
  3562. BUILDIN_FUNC(checkvending); // check vending [Nab4]
  3563. BUILDIN_FUNC(checkchatting); // check chatting [Marka]
  3564. #ifdef PCRE_SUPPORT
  3565. BUILDIN_FUNC(defpattern); // MouseJstr
  3566. BUILDIN_FUNC(activatepset); // MouseJstr
  3567. BUILDIN_FUNC(deactivatepset); // MouseJstr
  3568. BUILDIN_FUNC(deletepset); // MouseJstr
  3569. #endif
  3570. struct script_function buildin_func[] = {
  3571. // NPC interaction
  3572. BUILDIN_DEF(mes,"s"),
  3573. BUILDIN_DEF(next,""),
  3574. BUILDIN_DEF(close,""),
  3575. BUILDIN_DEF(close2,""),
  3576. BUILDIN_DEF(menu,"sl*"),
  3577. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  3578. BUILDIN_DEF(prompt,"s*"),
  3579. //
  3580. BUILDIN_DEF(goto,"l"),
  3581. BUILDIN_DEF(callsub,"i*"),
  3582. BUILDIN_DEF(callfunc,"s*"),
  3583. BUILDIN_DEF(return,"?"),
  3584. BUILDIN_DEF(getarg,"i"),
  3585. BUILDIN_DEF(jobchange,"i*"),
  3586. BUILDIN_DEF(jobname,"i"),
  3587. BUILDIN_DEF(input,"v"),
  3588. BUILDIN_DEF(warp,"sii"),
  3589. BUILDIN_DEF(areawarp,"siiiisii"),
  3590. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  3591. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  3592. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  3593. BUILDIN_DEF(setlook,"ii"),
  3594. BUILDIN_DEF(set,"ii"),
  3595. BUILDIN_DEF(setarray,"ii*"),
  3596. BUILDIN_DEF(cleararray,"iii"),
  3597. BUILDIN_DEF(copyarray,"iii"),
  3598. BUILDIN_DEF(getarraysize,"i"),
  3599. BUILDIN_DEF(deletearray,"ii"),
  3600. BUILDIN_DEF(getelementofarray,"ii"),
  3601. BUILDIN_DEF(getitem,"vi?"),
  3602. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  3603. BUILDIN_DEF(getnameditem,"is"),
  3604. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  3605. BUILDIN_DEF(makeitem,"iisii"),
  3606. BUILDIN_DEF(delitem,"ii"),
  3607. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  3608. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  3609. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  3610. BUILDIN_DEF(cutin,"si"),
  3611. BUILDIN_DEF(viewpoint,"iiiii"),
  3612. BUILDIN_DEF(heal,"ii"),
  3613. BUILDIN_DEF(itemheal,"ii"),
  3614. BUILDIN_DEF(percentheal,"ii"),
  3615. BUILDIN_DEF(rand,"i?"),
  3616. BUILDIN_DEF(countitem,"i"),
  3617. BUILDIN_DEF(countitem2,"iiiiiiii"),
  3618. BUILDIN_DEF(checkweight,"ii"),
  3619. BUILDIN_DEF(readparam,"i*"),
  3620. BUILDIN_DEF(getcharid,"i*"),
  3621. BUILDIN_DEF(getpartyname,"i"),
  3622. BUILDIN_DEF(getpartymember,"i*"),
  3623. BUILDIN_DEF(getpartyleader,"i?"),
  3624. BUILDIN_DEF(getguildname,"i"),
  3625. BUILDIN_DEF(getguildmaster,"i"),
  3626. BUILDIN_DEF(getguildmasterid,"i"),
  3627. BUILDIN_DEF(strcharinfo,"i"),
  3628. BUILDIN_DEF(getequipid,"i"),
  3629. BUILDIN_DEF(getequipname,"i"),
  3630. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  3631. BUILDIN_DEF(repair,"i"), // [Valaris]
  3632. BUILDIN_DEF(getequipisequiped,"i"),
  3633. BUILDIN_DEF(getequipisenableref,"i"),
  3634. BUILDIN_DEF(getequipisidentify,"i"),
  3635. BUILDIN_DEF(getequiprefinerycnt,"i"),
  3636. BUILDIN_DEF(getequipweaponlv,"i"),
  3637. BUILDIN_DEF(getequippercentrefinery,"i"),
  3638. BUILDIN_DEF(successrefitem,"i"),
  3639. BUILDIN_DEF(failedrefitem,"i"),
  3640. BUILDIN_DEF(statusup,"i"),
  3641. BUILDIN_DEF(statusup2,"ii"),
  3642. BUILDIN_DEF(bonus,"ii"),
  3643. BUILDIN_DEF2(bonus,"bonus2","iii"),
  3644. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  3645. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  3646. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  3647. BUILDIN_DEF(skill,"ii?"),
  3648. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  3649. BUILDIN_DEF(guildskill,"ii"),
  3650. BUILDIN_DEF(getskilllv,"i"),
  3651. BUILDIN_DEF(getgdskilllv,"ii"),
  3652. BUILDIN_DEF(basicskillcheck,""),
  3653. BUILDIN_DEF(getgmlevel,""),
  3654. BUILDIN_DEF(end,""),
  3655. // BUILDIN_DEF2(end,"break",""), this might confuse advanced scripting support [Eoe]
  3656. BUILDIN_DEF(checkoption,"i"),
  3657. BUILDIN_DEF(setoption,"i?"),
  3658. BUILDIN_DEF(setcart,"?"),
  3659. BUILDIN_DEF(checkcart,""),
  3660. BUILDIN_DEF(setfalcon,"?"),
  3661. BUILDIN_DEF(checkfalcon,""),
  3662. BUILDIN_DEF(setriding,"?"),
  3663. BUILDIN_DEF(checkriding,""),
  3664. BUILDIN_DEF2(savepoint,"save","sii"),
  3665. BUILDIN_DEF(savepoint,"sii"),
  3666. BUILDIN_DEF(gettimetick,"i"),
  3667. BUILDIN_DEF(gettime,"i"),
  3668. BUILDIN_DEF(gettimestr,"si"),
  3669. BUILDIN_DEF(openstorage,""),
  3670. BUILDIN_DEF(guildopenstorage,"*"),
  3671. BUILDIN_DEF(itemskill,"ii"),
  3672. BUILDIN_DEF(produce,"i"),
  3673. BUILDIN_DEF(monster,"siisii*"),
  3674. BUILDIN_DEF(areamonster,"siiiisii*"),
  3675. BUILDIN_DEF(killmonster,"ss"),
  3676. BUILDIN_DEF(killmonsterall,"s"),
  3677. BUILDIN_DEF(clone,"siisi*"),
  3678. BUILDIN_DEF(doevent,"s"),
  3679. BUILDIN_DEF(donpcevent,"s"),
  3680. BUILDIN_DEF(addtimer,"is"),
  3681. BUILDIN_DEF(deltimer,"s"),
  3682. BUILDIN_DEF(addtimercount,"si"),
  3683. BUILDIN_DEF(initnpctimer,"??"),
  3684. BUILDIN_DEF(stopnpctimer,"??"),
  3685. BUILDIN_DEF(startnpctimer,"??"),
  3686. BUILDIN_DEF(setnpctimer,"i?"),
  3687. BUILDIN_DEF(getnpctimer,"i?"),
  3688. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  3689. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  3690. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  3691. BUILDIN_DEF(announce,"si*"),
  3692. BUILDIN_DEF(mapannounce,"ssi*"),
  3693. BUILDIN_DEF(areaannounce,"siiiisi*"),
  3694. BUILDIN_DEF(getusers,"i"),
  3695. BUILDIN_DEF(getmapguildusers,"si"),
  3696. BUILDIN_DEF(getmapusers,"s"),
  3697. BUILDIN_DEF(getareausers,"siiii"),
  3698. BUILDIN_DEF(getareadropitem,"siiiii"),
  3699. BUILDIN_DEF(enablenpc,"s"),
  3700. BUILDIN_DEF(disablenpc,"s"),
  3701. BUILDIN_DEF(enablearena,""), // Added by RoVeRT
  3702. BUILDIN_DEF(disablearena,""), // Added by RoVeRT
  3703. BUILDIN_DEF(hideoffnpc,"s"),
  3704. BUILDIN_DEF(hideonnpc,"s"),
  3705. BUILDIN_DEF(sc_start,"iii*"),
  3706. BUILDIN_DEF(sc_start2,"iiii*"),
  3707. BUILDIN_DEF(sc_start4,"iiiiii*"),
  3708. BUILDIN_DEF(sc_end,"i"),
  3709. BUILDIN_DEF(getscrate,"ii*"),
  3710. BUILDIN_DEF(debugmes,"s"),
  3711. BUILDIN_DEF2(catchpet,"pet","i"),
  3712. BUILDIN_DEF2(birthpet,"bpet",""),
  3713. BUILDIN_DEF(resetlvl,"i"),
  3714. BUILDIN_DEF(resetstatus,""),
  3715. BUILDIN_DEF(resetskill,""),
  3716. BUILDIN_DEF(skillpointcount,""),
  3717. BUILDIN_DEF(changebase,"i"),
  3718. BUILDIN_DEF(changesex,""),
  3719. BUILDIN_DEF(waitingroom,"si??"),
  3720. BUILDIN_DEF(delwaitingroom,"?"),
  3721. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  3722. BUILDIN_DEF(enablewaitingroomevent,"?"),
  3723. BUILDIN_DEF(disablewaitingroomevent,"?"),
  3724. BUILDIN_DEF(getwaitingroomstate,"i?"),
  3725. BUILDIN_DEF(warpwaitingpc,"sii?"),
  3726. BUILDIN_DEF(attachrid,"i"),
  3727. BUILDIN_DEF(detachrid,""),
  3728. BUILDIN_DEF(isloggedin,"i"),
  3729. BUILDIN_DEF(setmapflagnosave,"ssii"),
  3730. BUILDIN_DEF(setmapflag,"si*"),
  3731. BUILDIN_DEF(removemapflag,"si"),
  3732. BUILDIN_DEF(pvpon,"s"),
  3733. BUILDIN_DEF(pvpoff,"s"),
  3734. BUILDIN_DEF(gvgon,"s"),
  3735. BUILDIN_DEF(gvgoff,"s"),
  3736. BUILDIN_DEF(emotion,"i*"),
  3737. BUILDIN_DEF(maprespawnguildid,"sii"),
  3738. BUILDIN_DEF(agitstart,""), // <Agit>
  3739. BUILDIN_DEF(agitend,""),
  3740. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  3741. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  3742. BUILDIN_DEF(getcastlename,"s"),
  3743. BUILDIN_DEF(getcastledata,"si*"),
  3744. BUILDIN_DEF(setcastledata,"sii"),
  3745. BUILDIN_DEF(requestguildinfo,"i*"),
  3746. BUILDIN_DEF(getequipcardcnt,"i"),
  3747. BUILDIN_DEF(successremovecards,"i"),
  3748. BUILDIN_DEF(failedremovecards,"ii"),
  3749. BUILDIN_DEF(marriage,"s"),
  3750. BUILDIN_DEF2(wedding_effect,"wedding",""),
  3751. BUILDIN_DEF(divorce,""),
  3752. BUILDIN_DEF(ispartneron,""),
  3753. BUILDIN_DEF(getpartnerid,""),
  3754. BUILDIN_DEF(getchildid,""),
  3755. BUILDIN_DEF(getmotherid,""),
  3756. BUILDIN_DEF(getfatherid,""),
  3757. BUILDIN_DEF(warppartner,"sii"),
  3758. BUILDIN_DEF(getitemname,"i"),
  3759. BUILDIN_DEF(getitemslots,"i"),
  3760. BUILDIN_DEF(makepet,"i"),
  3761. BUILDIN_DEF(getexp,"ii"),
  3762. BUILDIN_DEF(getinventorylist,""),
  3763. BUILDIN_DEF(getskilllist,""),
  3764. BUILDIN_DEF(clearitem,""),
  3765. BUILDIN_DEF(classchange,"ii"),
  3766. BUILDIN_DEF(misceffect,"i"),
  3767. BUILDIN_DEF(soundeffect,"si"),
  3768. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  3769. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  3770. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  3771. BUILDIN_DEF(guardianinfo,"i"), // display guardian data [Valaris]
  3772. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  3773. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  3774. BUILDIN_DEF(petloot,"i"), // [Valaris]
  3775. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  3776. // BUILDIN_DEF(petmag,"iiii"), // [Valaris]
  3777. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  3778. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  3779. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  3780. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  3781. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  3782. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  3783. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  3784. BUILDIN_DEF(nude,""), // nude command [Valaris]
  3785. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  3786. BUILDIN_DEF(inittimer,""),
  3787. BUILDIN_DEF(stoptimer,""),
  3788. BUILDIN_DEF(cmdothernpc,"ss"),
  3789. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  3790. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  3791. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  3792. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  3793. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  3794. BUILDIN_DEF(hasitems,"*"), // [Valaris]
  3795. BUILDIN_DEF(mobcount,"ss"),
  3796. BUILDIN_DEF(getlook,"i"),
  3797. BUILDIN_DEF(getsavepoint,"i"),
  3798. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  3799. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  3800. BUILDIN_DEF(npcstop,""), // [Valaris]
  3801. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  3802. BUILDIN_DEF(checkoption1,"i"),
  3803. BUILDIN_DEF(checkoption2,"i"),
  3804. BUILDIN_DEF(guildgetexp,"i"),
  3805. BUILDIN_DEF(guildchangegm,"is"),
  3806. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  3807. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  3808. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  3809. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  3810. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  3811. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  3812. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  3813. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  3814. BUILDIN_DEF(adopt,"sss"), // allows 2 parents to adopt a child
  3815. BUILDIN_DEF(night,""), // sets the server to night time
  3816. BUILDIN_DEF(day,""), // sets the server to day time
  3817. #ifdef PCRE_SUPPORT
  3818. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  3819. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  3820. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  3821. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  3822. #endif
  3823. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  3824. BUILDIN_DEF(getusersname,"*"),
  3825. BUILDIN_DEF(recovery,""),
  3826. BUILDIN_DEF(getpetinfo,"i"),
  3827. BUILDIN_DEF(checkequipedcard,"i"),
  3828. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  3829. BUILDIN_DEF(globalmes,"s*"),
  3830. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  3831. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  3832. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  3833. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  3834. BUILDIN_DEF(fakenpcname,"ssi"), // [Lance]
  3835. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  3836. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  3837. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  3838. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  3839. // [zBuffer] List of mathematics commands --->
  3840. BUILDIN_DEF(sqrt,"i"),
  3841. BUILDIN_DEF(pow,"ii"),
  3842. BUILDIN_DEF(distance,"iiii"),
  3843. BUILDIN_DEF(checkcell,"siii"),
  3844. // <--- [zBuffer] List of mathematics commands
  3845. // [zBuffer] List of dynamic var commands --->
  3846. BUILDIN_DEF(getd,"*"),
  3847. BUILDIN_DEF(setd,"*"),
  3848. // <--- [zBuffer] List of dynamic var commands
  3849. BUILDIN_DEF(petstat,"i"),
  3850. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  3851. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  3852. BUILDIN_DEF(npcshopadditem,"sii*"),
  3853. BUILDIN_DEF(npcshopdelitem,"si*"),
  3854. BUILDIN_DEF(npcshopattach,"s?"),
  3855. BUILDIN_DEF(equip,"i"),
  3856. BUILDIN_DEF(autoequip,"ii"),
  3857. BUILDIN_DEF(setbattleflag,"ss"),
  3858. BUILDIN_DEF(getbattleflag,"s"),
  3859. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  3860. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  3861. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  3862. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  3863. BUILDIN_DEF(axtoi,"s"),
  3864. BUILDIN_DEF(query_sql,"s*"),
  3865. BUILDIN_DEF(escape_sql,"s"),
  3866. BUILDIN_DEF(atoi,"s"),
  3867. // [zBuffer] List of player cont commands --->
  3868. BUILDIN_DEF(rid2name,"i"),
  3869. BUILDIN_DEF(pcfollow,"ii"),
  3870. BUILDIN_DEF(pcstopfollow,"i"),
  3871. BUILDIN_DEF(pcblockmove,"ii"),
  3872. // <--- [zBuffer] List of player cont commands
  3873. // [zBuffer] List of mob control commands --->
  3874. BUILDIN_DEF(mobspawn,"*"),
  3875. BUILDIN_DEF(mobremove,"i"),
  3876. BUILDIN_DEF(getmobdata,"i*"),
  3877. BUILDIN_DEF(setmobdata,"iii"),
  3878. BUILDIN_DEF(mobassist,"i?"),
  3879. BUILDIN_DEF(mobattach,"i?"),
  3880. BUILDIN_DEF(unitwalk,"ii?"),
  3881. BUILDIN_DEF(unitkill,"i"),
  3882. BUILDIN_DEF(unitwarp,"isii"),
  3883. BUILDIN_DEF(unitattack,"iv?"),
  3884. BUILDIN_DEF(unitstop,"i"),
  3885. BUILDIN_DEF(unittalk,"is"),
  3886. BUILDIN_DEF(unitemote,"ii"),
  3887. BUILDIN_DEF(unitdeadsit,"ii"),
  3888. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  3889. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  3890. // <--- [zBuffer] List of mob control commands
  3891. BUILDIN_DEF(sleep,"i"),
  3892. BUILDIN_DEF(sleep2,"i"),
  3893. BUILDIN_DEF(awake,"s"),
  3894. BUILDIN_DEF(getvariableofnpc,"rs"),
  3895. BUILDIN_DEF(warpportal,"iisii"),
  3896. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  3897. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  3898. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  3899. BUILDIN_DEF(checkvending,"*"),
  3900. BUILDIN_DEF(checkchatting,"*"),
  3901. {NULL,NULL,NULL},
  3902. };
  3903. /////////////////////////////////////////////////////////////////////
  3904. // NPC interaction
  3905. //
  3906. /// Appends a message to the npc dialog.
  3907. /// If a dialog doesn't exist yet, one is created.
  3908. /// TODO does the client support dialogs with different oid's at the same time?
  3909. ///
  3910. /// mes "<message>";
  3911. BUILDIN_FUNC(mes)
  3912. {
  3913. TBL_PC* sd = script_rid2sd(st);
  3914. if( sd == NULL )
  3915. return 1;
  3916. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3917. return 0;
  3918. }
  3919. /// Displays the button 'next' in the npc dialog.
  3920. /// The dialog text is cleared and the script continues when the button is pressed.
  3921. ///
  3922. /// next;
  3923. BUILDIN_FUNC(next)
  3924. {
  3925. TBL_PC* sd;
  3926. sd = script_rid2sd(st);
  3927. if( sd == NULL )
  3928. return 1;
  3929. st->state = STOP;
  3930. clif_scriptnext(sd, st->oid);
  3931. return 0;
  3932. }
  3933. /// Ends the script and displays the button 'close' on the npc dialog.
  3934. /// The dialog is closed when the button is pressed.
  3935. ///
  3936. /// close;
  3937. BUILDIN_FUNC(close)
  3938. {
  3939. TBL_PC* sd;
  3940. sd = script_rid2sd(st);
  3941. if( sd == NULL )
  3942. return 1;
  3943. st->state = END;
  3944. clif_scriptclose(sd, st->oid);
  3945. return 0;
  3946. }
  3947. /// Displays the button 'close' on the npc dialog.
  3948. /// The dialog is closed and the script continues when the button is pressed.
  3949. ///
  3950. /// close2;
  3951. BUILDIN_FUNC(close2)
  3952. {
  3953. TBL_PC* sd;
  3954. sd = script_rid2sd(st);
  3955. if( sd == NULL )
  3956. return 1;
  3957. st->state = STOP;
  3958. clif_scriptclose(sd, st->oid);
  3959. return 0;
  3960. }
  3961. /// Counts the number of valid and total number of options in 'str'
  3962. /// If max_count > 0 the counting stops when that valid option is reached
  3963. /// total is incremented for each option (NULL is supported)
  3964. static int menu_countoptions(const char* str, int max_count, int* total)
  3965. {
  3966. int count = 0;
  3967. int bogus_total;
  3968. if( total == NULL )
  3969. total = &bogus_total;
  3970. ++(*total);
  3971. // initial empty options
  3972. while( *str == ':' )
  3973. {
  3974. ++str;
  3975. ++(*total);
  3976. }
  3977. // count menu options
  3978. while( *str != '\0' )
  3979. {
  3980. ++count;
  3981. --max_count;
  3982. if( max_count == 0 )
  3983. break;
  3984. while( *str != ':' && *str != '\0' )
  3985. ++str;
  3986. while( *str == ':' )
  3987. {
  3988. ++str;
  3989. ++(*total);
  3990. }
  3991. }
  3992. return count;
  3993. }
  3994. /// Displays a menu with options and goes to the target label.
  3995. /// The script is stopped if cancel is pressed.
  3996. /// Options with no text are not displayed in the client.
  3997. ///
  3998. /// Options can be grouped together, separated by the character ':' in the text:
  3999. /// ex: menu "A:B:C",L_target;
  4000. /// All these options go to the specified target label.
  4001. ///
  4002. /// The index of the selected option is put in the variable @menu.
  4003. /// Indexes start with 1 and are consistent with grouped and empty options.
  4004. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4005. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4006. ///
  4007. /// NOTE: the client closes the npc dialog when cancel is pressed
  4008. ///
  4009. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4010. BUILDIN_FUNC(menu)
  4011. {
  4012. int i;
  4013. const char* text;
  4014. TBL_PC* sd;
  4015. sd = script_rid2sd(st);
  4016. if( sd == NULL )
  4017. return 1;
  4018. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4019. if( sd->state.menu_or_input == 0 )
  4020. {
  4021. struct StringBuf* buf;
  4022. struct script_data* data;
  4023. if( script_lastdata(st) % 2 == 0 )
  4024. {// argument count is not even (1st argument is at index 2)
  4025. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4026. st->state = END;
  4027. return 1;
  4028. }
  4029. buf = StringBuf_Malloc();
  4030. for( i = 2, sd->npc_menu = 0; i < script_lastdata(st); i += 2 )
  4031. {
  4032. // menu options
  4033. data = script_getdata(st, i);
  4034. get_val(st, data);
  4035. if( data_isstring(data) && data_isint(data) )
  4036. {// not a string (or compatible)
  4037. StringBuf_Free(buf);
  4038. ShowError("script:menu: argument #%d (from 1) is not a string or compatible.\n", (i - 1));
  4039. st->state = END;
  4040. return 1;
  4041. }
  4042. text = conv_str(st, data);// convert to string
  4043. // target label
  4044. data = script_getdata(st, i+1);
  4045. if( !data_islabel(data) )
  4046. {// not a label
  4047. StringBuf_Free(buf);
  4048. ShowError("script:menu: label argument of menu option #%d (from 1) is not a label.\n", (script_lastdata(st) - 1));
  4049. st->state = END;
  4050. return 1;
  4051. }
  4052. // append option(s)
  4053. if( text[0] == '\0' )
  4054. continue;// empty string, ignore
  4055. if( sd->npc_menu > 0 )
  4056. StringBuf_AppendStr(buf, ":");
  4057. StringBuf_AppendStr(buf, text);
  4058. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4059. }
  4060. st->state = RERUNLINE;
  4061. sd->state.menu_or_input = 1;
  4062. clif_scriptmenu(sd, st->oid, StringBuf_Value(buf));
  4063. StringBuf_Free(buf);
  4064. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  4065. }
  4066. else if( sd->npc_menu == 0xff )
  4067. {// Cancel was pressed
  4068. sd->state.menu_or_input = 0;
  4069. st->state = END;
  4070. }
  4071. else
  4072. {// goto target label
  4073. int menu = 0;
  4074. sd->state.menu_or_input = 0;
  4075. if( sd->npc_menu <= 0 )
  4076. {
  4077. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4078. st->state = END;
  4079. return 1;
  4080. }
  4081. // get target label
  4082. for( i = 2; i < script_lastdata(st); i += 2 )
  4083. {
  4084. text = script_getstr(st, i);
  4085. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4086. if( sd->npc_menu <= 0 )
  4087. break;// entry found
  4088. }
  4089. if( sd->npc_menu > 0 )
  4090. {// Invalid selection
  4091. ShowDebug("script:menu: selection is out of range, expected %d extra menu options\n", sd->npc_menu);
  4092. st->state = END;
  4093. return 1;
  4094. }
  4095. if( !data_islabel(script_getdata(st, i + 1)) )
  4096. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4097. st->state = END;
  4098. return 0;
  4099. }
  4100. pc_setreg(sd, add_str("@menu"), menu);
  4101. st->pos = script_getnum(st, i + 1);
  4102. st->state = GOTO;
  4103. }
  4104. return 0;
  4105. }
  4106. /// Displays a menu with options and returns the selected option.
  4107. /// Behaves like 'menu' without the target labels.
  4108. ///
  4109. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4110. ///
  4111. /// @see menu
  4112. BUILDIN_FUNC(select)
  4113. {
  4114. int i;
  4115. const char* text;
  4116. TBL_PC* sd;
  4117. sd = script_rid2sd(st);
  4118. if( sd == NULL )
  4119. return 1;
  4120. if( sd->state.menu_or_input == 0 )
  4121. {
  4122. struct StringBuf* buf;
  4123. buf = StringBuf_Malloc();
  4124. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  4125. {
  4126. text = script_getstr(st, i);
  4127. if( sd->npc_menu > 0 )
  4128. StringBuf_AppendStr(buf, ":");
  4129. StringBuf_AppendStr(buf, script_getstr(st, i));
  4130. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  4131. }
  4132. st->state = RERUNLINE;
  4133. sd->state.menu_or_input = 1;
  4134. clif_scriptmenu(sd, st->oid, StringBuf_Value(buf));
  4135. StringBuf_Free(buf);
  4136. }
  4137. else if( sd->npc_menu == 0xff )
  4138. {// Cancel was pressed
  4139. sd->state.menu_or_input = 0;
  4140. st->state = END;
  4141. }
  4142. else
  4143. {// return selected option
  4144. int menu = 0;
  4145. sd->state.menu_or_input = 0;
  4146. for( i = 2; i <= script_lastdata(st); ++i )
  4147. {
  4148. text = script_getstr(st, i);
  4149. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4150. if( sd->npc_menu <= 0 )
  4151. break;// entry found
  4152. }
  4153. pc_setreg(sd, add_str("@menu"), menu);
  4154. script_pushint(st, menu);
  4155. }
  4156. return 0;
  4157. }
  4158. /// Displays a menu with options and returns the selected option.
  4159. /// Behaves like 'menu' without the target labels, except when cancel is
  4160. /// pressed.
  4161. /// When cancel is pressed, the script continues and 255 is returned.
  4162. ///
  4163. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4164. ///
  4165. /// @see menu
  4166. BUILDIN_FUNC(prompt)
  4167. {
  4168. int i;
  4169. const char *text;
  4170. TBL_PC* sd;
  4171. sd = script_rid2sd(st);
  4172. if( sd == NULL )
  4173. return 1;
  4174. if( sd->state.menu_or_input == 0 )
  4175. {
  4176. struct StringBuf* buf;
  4177. buf = StringBuf_Malloc();
  4178. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  4179. {
  4180. text = script_getstr(st, i);
  4181. if( sd->npc_menu > 0 )
  4182. StringBuf_AppendStr(buf, ":");
  4183. StringBuf_AppendStr(buf, script_getstr(st, i));
  4184. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  4185. }
  4186. st->state = RERUNLINE;
  4187. sd->state.menu_or_input = 1;
  4188. clif_scriptmenu(sd, st->oid, StringBuf_Value(buf));
  4189. StringBuf_Free(buf);
  4190. }
  4191. else if( sd->npc_menu == 0xff )
  4192. {// Cancel was pressed
  4193. sd->state.menu_or_input = 0;
  4194. pc_setreg(sd, add_str("@menu"), 0xff);
  4195. script_pushint(st, 0xff);
  4196. }
  4197. else
  4198. {// return selected option
  4199. int menu = 0;
  4200. sd->state.menu_or_input = 0;
  4201. for( i = 2; i <= script_lastdata(st); ++i )
  4202. {
  4203. text = script_getstr(st, i);
  4204. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4205. if( sd->npc_menu <= 0 )
  4206. break;// entry found
  4207. }
  4208. pc_setreg(sd, add_str("@menu"), menu);
  4209. script_pushint(st, menu);
  4210. }
  4211. return 0;
  4212. }
  4213. /////////////////////////////////////////////////////////////////////
  4214. // ...
  4215. //
  4216. /// Jumps to the target script label.
  4217. ///
  4218. /// goto <label>;
  4219. BUILDIN_FUNC(goto)
  4220. {
  4221. if( !data_islabel(script_getdata(st,2)) )
  4222. {
  4223. ShowError("script:goto: not label!\n");
  4224. st->state = END;
  4225. return 1;
  4226. }
  4227. st->pos = script_getnum(st,2);
  4228. st->state = GOTO;
  4229. return 0;
  4230. }
  4231. /*==========================================
  4232. * ユーザー定義関数の呼び出し
  4233. *------------------------------------------*/
  4234. BUILDIN_FUNC(callfunc)
  4235. {
  4236. struct script_code *scr, *oldscr;
  4237. const char *str=script_getstr(st,2);
  4238. if( (scr=strdb_get(userfunc_db,(unsigned char*)str)) ){
  4239. int i,j;
  4240. struct linkdb_node **oldval = st->stack->var_function;
  4241. for(i=st->start+3,j=0;i<st->end;i++,j++)
  4242. push_copy(st->stack,i);
  4243. script_pushint(st,j); // 引数の数をプッシュ
  4244. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  4245. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  4246. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  4247. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  4248. oldscr = st->script;
  4249. st->pos=0;
  4250. st->script=scr;
  4251. st->stack->defsp=st->start+5+j;
  4252. st->state=GOTO;
  4253. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  4254. // ' 変数の引き継ぎ
  4255. for(i = 0; i < j; i++) {
  4256. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  4257. if( data_isreference(s) && !s->ref ) {
  4258. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  4259. // '@ 変数の引き継ぎ
  4260. if( name[0] == '.' && name[1] == '@' ) {
  4261. s->ref = oldval;
  4262. } else if( name[0] == '.' ) {
  4263. s->ref = &oldscr->script_vars;
  4264. }
  4265. }
  4266. }
  4267. }else{
  4268. ShowWarning("script:callfunc: function not found! [%s]\n",str);
  4269. st->state=END;
  4270. return 1;
  4271. }
  4272. return 0;
  4273. }
  4274. /*==========================================
  4275. * サブルーティンの呼び出し
  4276. *------------------------------------------*/
  4277. BUILDIN_FUNC(callsub)
  4278. {
  4279. int pos=script_getnum(st,2);
  4280. int i,j;
  4281. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  4282. ShowError("script: callsub: not label !\n");
  4283. st->state=END;
  4284. return 1;
  4285. } else {
  4286. struct linkdb_node **oldval = st->stack->var_function;
  4287. for(i=st->start+3,j=0;i<st->end;i++,j++)
  4288. push_copy(st->stack,i);
  4289. script_pushint(st,j); // 引数の数をプッシュ
  4290. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  4291. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  4292. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  4293. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  4294. st->pos=pos;
  4295. st->stack->defsp=st->start+5+j;
  4296. st->state=GOTO;
  4297. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  4298. // ' 変数の引き継ぎ
  4299. for(i = 0; i < j; i++) {
  4300. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  4301. if( data_isreference(s) && !s->ref ) {
  4302. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  4303. // '@ 変数の引き継ぎ
  4304. if( name[0] == '.' && name[1] == '@' ) {
  4305. s->ref = oldval;
  4306. }
  4307. }
  4308. }
  4309. }
  4310. return 0;
  4311. }
  4312. /*==========================================
  4313. * 引数の所得
  4314. *------------------------------------------*/
  4315. BUILDIN_FUNC(getarg)
  4316. {
  4317. int num=script_getnum(st,2);
  4318. int max,stsp;
  4319. if( st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO ){
  4320. ShowWarning("script:getarg without callfunc or callsub!\n");
  4321. st->state=END;
  4322. return 1;
  4323. }
  4324. max=conv_num(st,& (st->stack->stack_data[st->stack->defsp-5]));
  4325. stsp=st->stack->defsp - max -5;
  4326. if( num >= max ){
  4327. ShowWarning("script:getarg arg1(%d) out of range(%d) !\n",num,max);
  4328. st->state=END;
  4329. return 1;
  4330. }
  4331. push_copy(st->stack,stsp+num);
  4332. return 0;
  4333. }
  4334. /// Returns from the current function, optionaly returning a value from the functions.
  4335. /// Don't use outside script functions.
  4336. ///
  4337. /// return;
  4338. /// return <value>;
  4339. BUILDIN_FUNC(return)
  4340. {
  4341. if( script_hasdata(st,2) )
  4342. {// return value
  4343. struct script_data* data;
  4344. script_pushcopy(st, 2);
  4345. data = script_getdatatop(st, -1);
  4346. if( data_isreference(data) )
  4347. {
  4348. char* name = data_referencename(data);
  4349. if( name[0] == '.' && name[1] == '@' )
  4350. {// temporary script variable, convert to value
  4351. get_val(st, data);
  4352. if( data_isstring(data) )
  4353. {// duplicate the string
  4354. data->type = C_STR;
  4355. data->u.str = aStrdup(data->u.str);
  4356. }
  4357. }
  4358. else if( name[0] == '.' && !data->ref )
  4359. {// script variable, link to current script
  4360. data->ref = &st->script->script_vars;
  4361. }
  4362. }
  4363. }
  4364. else
  4365. {// no return value
  4366. script_pushnil(st);
  4367. }
  4368. st->state = RETFUNC;
  4369. return 0;
  4370. }
  4371. /// Returns a random number from 0 to <range>-1.
  4372. /// Or returns a random number from <min> to <max>.
  4373. /// If <min> is greater than <max>, their numbers are switched.
  4374. /// rand(<range>) -> <int>
  4375. /// rand(<min>,<max>) -> <int>
  4376. BUILDIN_FUNC(rand)
  4377. {
  4378. int range;
  4379. int min;
  4380. int max;
  4381. if( script_hasdata(st,3) )
  4382. {// min,max
  4383. min = script_getnum(st,2);
  4384. max = script_getnum(st,3);
  4385. if( max < min )
  4386. swap(min, max);
  4387. range = max - min + 1;
  4388. }
  4389. else
  4390. {// range
  4391. min = 0;
  4392. range = script_getnum(st,2);
  4393. }
  4394. if( range <= 1 )
  4395. script_pushint(st, min);
  4396. else
  4397. script_pushint(st, rand()%range + min);
  4398. return 0;
  4399. }
  4400. /*==========================================
  4401. *
  4402. *------------------------------------------*/
  4403. BUILDIN_FUNC(warp)
  4404. {
  4405. int x,y;
  4406. const char *str;
  4407. TBL_PC *sd=script_rid2sd(st);
  4408. nullpo_retr(0, sd);
  4409. str=script_getstr(st,2);
  4410. x=script_getnum(st,3);
  4411. y=script_getnum(st,4);
  4412. if(strcmp(str,"Random")==0)
  4413. pc_randomwarp(sd,3);
  4414. else if(strcmp(str,"SavePoint")==0){
  4415. pc_setpos(sd,sd->status.save_point.map,
  4416. sd->status.save_point.x,sd->status.save_point.y,3);
  4417. }else if(strcmp(str,"Save")==0){
  4418. pc_setpos(sd,sd->status.save_point.map,
  4419. sd->status.save_point.x,sd->status.save_point.y,3);
  4420. }else
  4421. pc_setpos(sd,mapindex_name2id(str),x,y,0);
  4422. return 0;
  4423. }
  4424. /*==========================================
  4425. * エリア指定ワープ
  4426. *------------------------------------------*/
  4427. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4428. {
  4429. int x,y;
  4430. unsigned int map;
  4431. map=va_arg(ap, unsigned int);
  4432. x=va_arg(ap,int);
  4433. y=va_arg(ap,int);
  4434. if(map == 0)
  4435. pc_randomwarp((TBL_PC *)bl,3);
  4436. else
  4437. pc_setpos((TBL_PC *)bl,map,x,y,0);
  4438. return 0;
  4439. }
  4440. BUILDIN_FUNC(areawarp)
  4441. {
  4442. int x,y,m;
  4443. unsigned int index;
  4444. const char *str;
  4445. const char *mapname;
  4446. int x0,y0,x1,y1;
  4447. mapname=script_getstr(st,2);
  4448. x0=script_getnum(st,3);
  4449. y0=script_getnum(st,4);
  4450. x1=script_getnum(st,5);
  4451. y1=script_getnum(st,6);
  4452. str=script_getstr(st,7);
  4453. x=script_getnum(st,8);
  4454. y=script_getnum(st,9);
  4455. if( (m=map_mapname2mapid(mapname))< 0)
  4456. return 0;
  4457. if(strcmp(str,"Random")==0)
  4458. index = 0;
  4459. else if(!(index=mapindex_name2id(str)))
  4460. return 0;
  4461. map_foreachinarea(buildin_areawarp_sub,
  4462. m,x0,y0,x1,y1,BL_PC, index,x,y );
  4463. return 0;
  4464. }
  4465. /*==========================================
  4466. * warpchar [LuzZza]
  4467. * Useful for warp one player from
  4468. * another player npc-session.
  4469. * Using: warpchar "mapname",x,y,Char_ID;
  4470. *------------------------------------------*/
  4471. BUILDIN_FUNC(warpchar)
  4472. {
  4473. int x,y,a,i;
  4474. const char *str;
  4475. TBL_PC *sd, **pl_allsd;
  4476. int users;
  4477. str=script_getstr(st,2);
  4478. x=script_getnum(st,3);
  4479. y=script_getnum(st,4);
  4480. a=script_getnum(st,5);
  4481. pl_allsd = map_getallusers(&users);
  4482. for(i=0; i<users; i++) {
  4483. sd = pl_allsd[i];
  4484. if(sd->status.char_id == a) {
  4485. if(strcmp(str, "Random") == 0)
  4486. pc_randomwarp(sd, 3);
  4487. else if(strcmp(str, "SavePoint") == 0)
  4488. pc_setpos(sd, sd->status.save_point.map,
  4489. sd->status.save_point.x, sd->status.save_point.y, 3);
  4490. else
  4491. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  4492. }
  4493. }
  4494. return 0;
  4495. }
  4496. /*==========================================
  4497. * Warpparty - [Fredzilla]
  4498. * Syntax: warpparty "mapname",x,y,Party_ID;
  4499. *------------------------------------------*/
  4500. BUILDIN_FUNC(warpparty)
  4501. {
  4502. int x,y;
  4503. const char *str;
  4504. int p_id;
  4505. int i;
  4506. unsigned short mapindex;
  4507. TBL_PC *pl_sd;
  4508. struct party_data *p=NULL;
  4509. str=script_getstr(st,2);
  4510. x=script_getnum(st,3);
  4511. y=script_getnum(st,4);
  4512. p_id=script_getnum(st,5);
  4513. if(p_id < 1)
  4514. return 0;
  4515. p = party_search(p_id);
  4516. if (!p)
  4517. return 0;
  4518. if(strcmp(str,"Random")==0)
  4519. {
  4520. for (i = 0; i < MAX_PARTY; i++)
  4521. {
  4522. if ((pl_sd = p->data[i].sd))
  4523. {
  4524. if(map[pl_sd->bl.m].flag.nowarp)
  4525. continue;
  4526. pc_randomwarp(pl_sd,3);
  4527. }
  4528. }
  4529. }
  4530. else if(strcmp(str,"SavePointAll")==0)
  4531. {
  4532. for (i = 0; i < MAX_PARTY; i++)
  4533. {
  4534. if ((pl_sd = p->data[i].sd))
  4535. {
  4536. if(map[pl_sd->bl.m].flag.noreturn)
  4537. continue;
  4538. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4539. }
  4540. }
  4541. }
  4542. else if(strcmp(str,"SavePoint")==0)
  4543. {
  4544. pl_sd=script_rid2sd(st);
  4545. if (!pl_sd) return 0;
  4546. mapindex=pl_sd->status.save_point.map;
  4547. x=pl_sd->status.save_point.x;
  4548. y=pl_sd->status.save_point.y;
  4549. for (i = 0; i < MAX_PARTY; i++)
  4550. {
  4551. if ((pl_sd = p->data[i].sd))
  4552. {
  4553. if(map[pl_sd->bl.m].flag.noreturn)
  4554. continue;
  4555. pc_setpos(pl_sd,mapindex,x,y,3);
  4556. }
  4557. }
  4558. }
  4559. else
  4560. {
  4561. mapindex = mapindex_name2id(str);
  4562. if (!mapindex) //Show source of npc error.
  4563. return 1;
  4564. for (i = 0; i < MAX_PARTY; i++)
  4565. {
  4566. if ((pl_sd = p->data[i].sd))
  4567. {
  4568. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4569. continue;
  4570. pc_setpos(pl_sd,mapindex,x,y,3);
  4571. }
  4572. }
  4573. }
  4574. return 0;
  4575. }
  4576. /*==========================================
  4577. * Warpguild - [Fredzilla]
  4578. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4579. *------------------------------------------*/
  4580. BUILDIN_FUNC(warpguild)
  4581. {
  4582. int x,y;
  4583. unsigned short mapindex;
  4584. const char *str;
  4585. int g;
  4586. int i;
  4587. TBL_PC *pl_sd, **pl_allsd;
  4588. int users;
  4589. TBL_PC *sd;
  4590. str=script_getstr(st,2);
  4591. x=script_getnum(st,3);
  4592. y=script_getnum(st,4);
  4593. g=script_getnum(st,5);
  4594. sd=script_rid2sd(st);
  4595. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4596. return 0;
  4597. if(g < 1)
  4598. return 0;
  4599. pl_allsd = map_getallusers(&users);
  4600. if(strcmp(str,"Random")==0)
  4601. {
  4602. for (i = 0; i < users; i++)
  4603. {
  4604. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4605. {
  4606. if(map[pl_sd->bl.m].flag.nowarp)
  4607. continue;
  4608. pc_randomwarp(pl_sd,3);
  4609. }
  4610. }
  4611. }
  4612. else if(strcmp(str,"SavePointAll")==0)
  4613. {
  4614. if(map[sd->bl.m].flag.noreturn)
  4615. return 0;
  4616. for (i = 0; i < users; i++)
  4617. {
  4618. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4619. {
  4620. if(map[pl_sd->bl.m].flag.noreturn)
  4621. continue;
  4622. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4623. }
  4624. }
  4625. }
  4626. else if(strcmp(str,"SavePoint")==0)
  4627. {
  4628. if(map[sd->bl.m].flag.noreturn)
  4629. return 0;
  4630. mapindex=sd->status.save_point.map;
  4631. x=sd->status.save_point.x;
  4632. y=sd->status.save_point.y;
  4633. for (i = 0; i < users; i++)
  4634. {
  4635. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4636. {
  4637. if(map[pl_sd->bl.m].flag.noreturn)
  4638. continue;
  4639. pc_setpos(pl_sd,mapindex,x,y,3);
  4640. }
  4641. }
  4642. }
  4643. else
  4644. {
  4645. mapindex = mapindex_name2id(str);
  4646. for (i = 0; i < users; i++)
  4647. {
  4648. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4649. {
  4650. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4651. continue;
  4652. pc_setpos(pl_sd,mapindex,x,y,3);
  4653. }
  4654. }
  4655. }
  4656. return 0;
  4657. }
  4658. /*==========================================
  4659. *
  4660. *------------------------------------------*/
  4661. BUILDIN_FUNC(heal)
  4662. {
  4663. TBL_PC *sd;
  4664. int hp,sp;
  4665. sd = script_rid2sd(st);
  4666. if (!sd) return 0;
  4667. hp=script_getnum(st,2);
  4668. sp=script_getnum(st,3);
  4669. status_heal(&sd->bl, hp, sp, 1);
  4670. return 0;
  4671. }
  4672. /*==========================================
  4673. *
  4674. *------------------------------------------*/
  4675. BUILDIN_FUNC(itemheal)
  4676. {
  4677. TBL_PC *sd;
  4678. int hp,sp;
  4679. hp=script_getnum(st,2);
  4680. sp=script_getnum(st,3);
  4681. if(potion_flag==1) {
  4682. potion_hp = hp;
  4683. potion_sp = sp;
  4684. return 0;
  4685. }
  4686. sd = script_rid2sd(st);
  4687. if (!sd) return 0;
  4688. pc_itemheal(sd,sd->itemid,hp,sp);
  4689. return 0;
  4690. }
  4691. /*==========================================
  4692. *
  4693. *------------------------------------------*/
  4694. BUILDIN_FUNC(percentheal)
  4695. {
  4696. int hp,sp;
  4697. hp=script_getnum(st,2);
  4698. sp=script_getnum(st,3);
  4699. if(potion_flag==1) {
  4700. potion_per_hp = hp;
  4701. potion_per_sp = sp;
  4702. return 0;
  4703. }
  4704. pc_percentheal(script_rid2sd(st),hp,sp);
  4705. return 0;
  4706. }
  4707. /*==========================================
  4708. *
  4709. *------------------------------------------*/
  4710. BUILDIN_FUNC(jobchange)
  4711. {
  4712. int job, upper=-1;
  4713. job=script_getnum(st,2);
  4714. if( script_hasdata(st,3) )
  4715. upper=script_getnum(st,3);
  4716. if ((job >= 0 && job < MAX_PC_CLASS))
  4717. {
  4718. pc_jobchange(script_rid2sd(st),job, upper);
  4719. if(use_irc && irc_announce_jobchange_flag)
  4720. irc_announce_jobchange(script_rid2sd(st));
  4721. }
  4722. return 0;
  4723. }
  4724. /*==========================================
  4725. *
  4726. *------------------------------------------*/
  4727. BUILDIN_FUNC(jobname)
  4728. {
  4729. int class_=script_getnum(st,2);
  4730. script_pushconststr(st,job_name(class_));
  4731. return 0;
  4732. }
  4733. /*==========================================
  4734. *
  4735. *------------------------------------------*/
  4736. BUILDIN_FUNC(input)
  4737. {
  4738. TBL_PC *sd = script_rid2sd(st);
  4739. struct script_data *data = script_getdata(st,2);
  4740. int num = data->u.num;
  4741. char *name=str_buf+str_data[num&0x00ffffff].str;
  4742. char postfix = name[strlen(name)-1];
  4743. if (!sd) return 1;
  4744. if( !data_isreference(data) ){
  4745. ShowError("script: buildin_input: given argument is not a variable!\n");
  4746. return 1;
  4747. }
  4748. if(sd->state.menu_or_input){
  4749. sd->state.menu_or_input=0;
  4750. if( postfix=='$' )
  4751. {
  4752. set_reg(st,sd,num,name,(void*)sd->npc_str,
  4753. script_getref(st,2));
  4754. return 0;
  4755. }
  4756. // Yor, Lupus & Fritz have messed with this.
  4757. // Basicly it prevents negative input since most scripts do not account for them.
  4758. sd->npc_amount = cap_value(sd->npc_amount, 0, battle_config.vending_max_value);
  4759. set_reg(st,sd,num,name,(void*)sd->npc_amount,
  4760. script_getref(st,2));
  4761. return 0;
  4762. }
  4763. //state.menu_or_input = 0
  4764. st->state=RERUNLINE;
  4765. if( postfix=='$' )
  4766. clif_scriptinputstr(sd,st->oid);
  4767. else
  4768. clif_scriptinput(sd,st->oid);
  4769. sd->state.menu_or_input=1;
  4770. return 0;
  4771. }
  4772. /*==========================================
  4773. * 変数設定
  4774. *------------------------------------------*/
  4775. BUILDIN_FUNC(set)
  4776. {
  4777. TBL_PC *sd=NULL;
  4778. int num=st->stack->stack_data[st->start+2].u.num;
  4779. char *name=str_buf+str_data[num&0x00ffffff].str;
  4780. char prefix=*name;
  4781. char postfix=name[strlen(name)-1];
  4782. if( !data_isreference(script_getdata(st,2)) ){
  4783. ShowError("script: buildin_set: not name\n");
  4784. return 1;
  4785. }
  4786. if(not_server_variable(prefix))
  4787. sd=script_rid2sd(st);
  4788. if( postfix=='$' ){
  4789. // 文字列
  4790. const char *str = script_getstr(st,3);
  4791. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  4792. }else{
  4793. // 数値
  4794. int val = script_getnum(st,3);
  4795. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  4796. }
  4797. return 0;
  4798. }
  4799. /*==========================================
  4800. * 配列変数設定
  4801. *------------------------------------------*/
  4802. BUILDIN_FUNC(setarray)
  4803. {
  4804. TBL_PC *sd=NULL;
  4805. int num=st->stack->stack_data[st->start+2].u.num;
  4806. char *name=str_buf+str_data[num&0x00ffffff].str;
  4807. char prefix=*name;
  4808. char postfix=name[strlen(name)-1];
  4809. int i,j;
  4810. if( prefix!='$' && prefix!='@' && prefix!='.'){
  4811. ShowWarning("buildin_setarray: illegal scope !\n");
  4812. return 1;
  4813. }
  4814. if(not_server_variable(prefix))
  4815. sd=script_rid2sd(st);
  4816. for(j=0,i=st->start+3; i<st->end && j<128;i++,j++){
  4817. void *v;
  4818. if( postfix=='$' )
  4819. v=(void*)conv_str(st,& (st->stack->stack_data[i]));
  4820. else
  4821. v=(void*)conv_num(st,& (st->stack->stack_data[i]));
  4822. set_reg(st, sd, num+(j<<24), name, v, script_getref(st,2));
  4823. }
  4824. return 0;
  4825. }
  4826. /*==========================================
  4827. * 配列変数クリア
  4828. *------------------------------------------*/
  4829. BUILDIN_FUNC(cleararray)
  4830. {
  4831. TBL_PC *sd=NULL;
  4832. int num=st->stack->stack_data[st->start+2].u.num;
  4833. char *name=str_buf+str_data[num&0x00ffffff].str;
  4834. char prefix=*name;
  4835. char postfix=name[strlen(name)-1];
  4836. int sz=script_getnum(st,4);
  4837. int i;
  4838. void *v;
  4839. if( prefix!='$' && prefix!='@' && prefix!='.'){
  4840. ShowWarning("buildin_cleararray: illegal scope !\n");
  4841. return 1;
  4842. }
  4843. if( not_server_variable(prefix) )
  4844. sd=script_rid2sd(st);
  4845. if( postfix=='$' )
  4846. v=(void*)script_getstr(st,3);
  4847. else
  4848. v=(void*)script_getnum(st,3);
  4849. for(i=0;i<sz;i++)
  4850. set_reg(st,sd,num+(i<<24),name,v,script_getref(st,2));
  4851. return 0;
  4852. }
  4853. /*==========================================
  4854. * 配列変数コピー
  4855. *------------------------------------------*/
  4856. BUILDIN_FUNC(copyarray)
  4857. {
  4858. TBL_PC *sd=NULL;
  4859. int num=st->stack->stack_data[st->start+2].u.num;
  4860. char *name=str_buf+str_data[num&0x00ffffff].str;
  4861. char prefix=*name;
  4862. char postfix=name[strlen(name)-1];
  4863. int num2=st->stack->stack_data[st->start+3].u.num;
  4864. char *name2=str_buf+str_data[num2&0x00ffffff].str;
  4865. char prefix2=*name2;
  4866. char postfix2=name2[strlen(name2)-1];
  4867. int sz=script_getnum(st,4);
  4868. int i;
  4869. if( prefix!='$' && prefix!='@' && prefix!='.' ){
  4870. printf("buildin_copyarray: illeagal scope !\n");
  4871. return 0;
  4872. }
  4873. if( prefix2!='$' && prefix2!='@' && prefix2!='.' ) {
  4874. printf("buildin_copyarray: illeagal scope !\n");
  4875. return 0;
  4876. }
  4877. if( (postfix=='$' || postfix2=='$') && postfix!=postfix2 ){
  4878. printf("buildin_copyarray: type mismatch !\n");
  4879. return 0;
  4880. }
  4881. if( not_server_variable(prefix) || not_server_variable(prefix2) )
  4882. sd=script_rid2sd(st);
  4883. if((num & 0x00FFFFFF) == (num2 & 0x00FFFFFF) && (num & 0xFF000000) > (num2 & 0xFF000000)) {
  4884. // 同じ配列で、num > num2 の場合大きい方からコピーしないといけない
  4885. for(i=sz-1;i>=0;i--)
  4886. set_reg(
  4887. st,sd,num+(i<<24),name,
  4888. get_val2(st,num2+(i<<24),script_getref(st,3)),
  4889. script_getref(st,2)
  4890. );
  4891. } else {
  4892. for(i=0;i<sz;i++)
  4893. set_reg(
  4894. st,sd,num+(i<<24),name,
  4895. get_val2(st,num2+(i<<24),script_getref(st,3)),
  4896. script_getref(st,2)
  4897. );
  4898. }
  4899. return 0;
  4900. }
  4901. /*==========================================
  4902. * Returns the size of the specified array
  4903. *------------------------------------------*/
  4904. static int getarraysize(struct script_state* st, int num, int postfix, struct linkdb_node** ref)
  4905. {
  4906. //TODO: unreadable; remove +-1 trick, remove weird for loop, remove typecast
  4907. int i = (num>>24), c = (i==0?-1:i); // Moded to -1 because even if the first element is 0, it will still report as 1 [Lance]
  4908. if(postfix == '$') {
  4909. for(; i < 128; i++) {
  4910. void* v = get_val2(st, (num & 0x00FFFFFF) + (i<<24), ref);
  4911. if(*((char*)v)) c = i;
  4912. }
  4913. } else {
  4914. for(; i < 128; i++) {
  4915. void* v = get_val2(st, (num & 0x00FFFFFF) + (i<<24), ref);
  4916. if((int)v) c = i;
  4917. }
  4918. }
  4919. return c + 1;
  4920. }
  4921. BUILDIN_FUNC(getarraysize)
  4922. {
  4923. int num = st->stack->stack_data[st->start+2].u.num;
  4924. char* name = str_buf + str_data[num&0x00ffffff].str;
  4925. char prefix = name[0], postfix = name[strlen(name)-1];
  4926. if( prefix != '$' && prefix != '@' && prefix != '.' ) {
  4927. ShowWarning("buildin_getarraysize: illegal scope !\n");
  4928. script_pushint(st,0);
  4929. return 1;
  4930. }
  4931. script_pushint(st,getarraysize(st, num, postfix, script_getref(st,2)));
  4932. return 0;
  4933. }
  4934. /*==========================================
  4935. * 配列変数から要素削除
  4936. *------------------------------------------*/
  4937. BUILDIN_FUNC(deletearray)
  4938. {
  4939. TBL_PC *sd=NULL;
  4940. int num=st->stack->stack_data[st->start+2].u.num;
  4941. char *name=str_buf+str_data[num&0x00ffffff].str;
  4942. char prefix=*name;
  4943. char postfix=name[strlen(name)-1];
  4944. int count=1;
  4945. int i,sz=getarraysize(st,num,postfix,script_getref(st,2))-(num>>24)-count+1;
  4946. if( script_hasdata(st,3) )
  4947. count=script_getnum(st,3);
  4948. if( prefix!='$' && prefix!='@' && prefix!='.' ){
  4949. ShowWarning("buildin_deletearray: illegal scope !\n");
  4950. return 1;
  4951. }
  4952. if( not_server_variable(prefix) )
  4953. sd=script_rid2sd(st);
  4954. for(i=0;i<sz;i++){
  4955. set_reg(
  4956. st,sd,num+(i<<24),name,
  4957. get_val2(st,num+((i+count)<<24),script_getref(st,2)),
  4958. script_getref(st,2)
  4959. );
  4960. }
  4961. if(postfix != '$'){
  4962. for(;i<(128-(num>>24));i++)
  4963. set_reg(st,sd,num+(i<<24),name, 0,script_getref(st,2));
  4964. } else {
  4965. for(;i<(128-(num>>24));i++)
  4966. set_reg(st,sd,num+(i<<24),name, (void *) "",script_getref(st,2));
  4967. }
  4968. return 0;
  4969. }
  4970. /*==========================================
  4971. * 指定要素を表す値(キー)を所得する
  4972. *------------------------------------------*/
  4973. BUILDIN_FUNC(getelementofarray)
  4974. {
  4975. if( data_isreference(script_getdata(st, 2)) ){
  4976. int i=script_getnum(st,3);
  4977. if(i>127 || i<0){
  4978. ShowWarning("script: getelementofarray (operator[]): param2 illegal number %d\n",i);
  4979. script_pushint(st,0);
  4980. return 1;
  4981. }else{
  4982. push_val2(st->stack,C_NAME,
  4983. (i<<24) | st->stack->stack_data[st->start+2].u.num, script_getref(st,2) );
  4984. }
  4985. }else{
  4986. ShowError("script: getelementofarray (operator[]): param1 not name !\n");
  4987. script_pushint(st,0);
  4988. }
  4989. return 0;
  4990. }
  4991. /*==========================================
  4992. *
  4993. *------------------------------------------*/
  4994. BUILDIN_FUNC(setlook)
  4995. {
  4996. int type,val;
  4997. type=script_getnum(st,2);
  4998. val=script_getnum(st,3);
  4999. pc_changelook(script_rid2sd(st),type,val);
  5000. return 0;
  5001. }
  5002. /*==========================================
  5003. *
  5004. *------------------------------------------*/
  5005. BUILDIN_FUNC(cutin)
  5006. {
  5007. clif_cutin(script_rid2sd(st),script_getstr(st,2),script_getnum(st,3));
  5008. return 0;
  5009. }
  5010. /*==========================================
  5011. *
  5012. *------------------------------------------*/
  5013. BUILDIN_FUNC(viewpoint)
  5014. {
  5015. int type,x,y,id,color;
  5016. type=script_getnum(st,2);
  5017. x=script_getnum(st,3);
  5018. y=script_getnum(st,4);
  5019. id=script_getnum(st,5);
  5020. color=script_getnum(st,6);
  5021. clif_viewpoint(script_rid2sd(st),st->oid,type,x,y,id,color);
  5022. return 0;
  5023. }
  5024. /*==========================================
  5025. *
  5026. *------------------------------------------*/
  5027. BUILDIN_FUNC(countitem)
  5028. {
  5029. int nameid, i;
  5030. int count = 0;
  5031. struct script_data* data;
  5032. TBL_PC* sd = script_rid2sd(st);
  5033. if (!sd) {
  5034. script_pushint(st,0);
  5035. return 0;
  5036. }
  5037. data = script_getdata(st,2);
  5038. get_val(st,data);
  5039. if( data_isstring(data) ) {
  5040. const char* name = conv_str(st,data);
  5041. struct item_data* item_data;
  5042. if((item_data = itemdb_searchname(name)) != NULL)
  5043. nameid = item_data->nameid;
  5044. else
  5045. nameid = 0;
  5046. } else
  5047. nameid = conv_num(st,data);
  5048. if (nameid < 500) {
  5049. if(battle_config.error_log) ShowError("wrong item ID : countitem(%i)\n", nameid);
  5050. script_pushint(st,0);
  5051. return 1;
  5052. }
  5053. for(i = 0; i < MAX_INVENTORY; i++)
  5054. if(sd->status.inventory[i].nameid == nameid)
  5055. count += sd->status.inventory[i].amount;
  5056. script_pushint(st,count);
  5057. return 0;
  5058. }
  5059. /*==========================================
  5060. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5061. * returns number of items that meet the conditions
  5062. *------------------------------------------*/
  5063. BUILDIN_FUNC(countitem2)
  5064. {
  5065. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5066. int count = 0;
  5067. int i;
  5068. struct script_data* data;
  5069. TBL_PC* sd = script_rid2sd(st);
  5070. if (!sd) {
  5071. script_pushint(st,0);
  5072. return 0;
  5073. }
  5074. data = script_getdata(st,2);
  5075. get_val(st,data);
  5076. if( data_isstring(data) ) {
  5077. const char* name = conv_str(st,data);
  5078. struct item_data* item_data;
  5079. if((item_data = itemdb_searchname(name)) != NULL)
  5080. nameid = item_data->nameid;
  5081. else
  5082. nameid = 0;
  5083. } else
  5084. nameid = conv_num(st,data);
  5085. iden = script_getnum(st,3);
  5086. ref = script_getnum(st,4);
  5087. attr = script_getnum(st,5);
  5088. c1 = (short)script_getnum(st,6);
  5089. c2 = (short)script_getnum(st,7);
  5090. c3 = (short)script_getnum(st,8);
  5091. c4 = (short)script_getnum(st,9);
  5092. if (nameid < 500) {
  5093. if(battle_config.error_log) ShowError("wrong item ID : countitem2(%i)\n", nameid);
  5094. script_pushint(st,0);
  5095. return 1;
  5096. }
  5097. for(i = 0; i < MAX_INVENTORY; i++)
  5098. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5099. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5100. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5101. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5102. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5103. sd->status.inventory[i].card[3] == c4
  5104. )
  5105. count += sd->status.inventory[i].amount;
  5106. script_pushint(st,count);
  5107. return 0;
  5108. }
  5109. /*==========================================
  5110. * 重量チェック
  5111. *------------------------------------------*/
  5112. BUILDIN_FUNC(checkweight)
  5113. {
  5114. int nameid=0,amount,i;
  5115. unsigned long weight;
  5116. TBL_PC *sd;
  5117. struct script_data *data;
  5118. sd = script_rid2sd(st);
  5119. data=script_getdata(st,2);
  5120. get_val(st,data);
  5121. if( data_isstring(data) ){
  5122. const char *name=conv_str(st,data);
  5123. struct item_data *item_data = itemdb_searchname(name);
  5124. if( item_data )
  5125. nameid=item_data->nameid;
  5126. }else
  5127. nameid=conv_num(st,data);
  5128. amount=script_getnum(st,3);
  5129. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  5130. script_pushint(st,0);
  5131. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  5132. return 1;
  5133. }
  5134. weight = itemdb_weight(nameid)*amount;
  5135. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  5136. script_pushint(st,0);
  5137. } else {
  5138. //Check if the inventory ain't full.
  5139. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  5140. i = pc_search_inventory(sd,0);
  5141. if (i >= 0) //Empty slot available.
  5142. script_pushint(st,1);
  5143. else //Inventory full
  5144. script_pushint(st,0);
  5145. }
  5146. return 0;
  5147. }
  5148. /*==========================================
  5149. * getitem <item id>,<amount>{,<character ID>};
  5150. * getitem "<item name>",<amount>{,<character ID>};
  5151. *------------------------------------------*/
  5152. BUILDIN_FUNC(getitem)
  5153. {
  5154. int nameid,amount,flag = 0;
  5155. struct item it;
  5156. TBL_PC *sd;
  5157. struct script_data *data;
  5158. data=script_getdata(st,2);
  5159. get_val(st,data);
  5160. if( data_isstring(data) )
  5161. {// "<item name>"
  5162. const char *name=conv_str(st,data);
  5163. struct item_data *item_data = itemdb_searchname(name);
  5164. if( item_data == NULL ){
  5165. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5166. report_src(st);
  5167. return 1; //No item created.
  5168. }
  5169. nameid=item_data->nameid;
  5170. } else if( data_isint(data) )
  5171. {// <item id>
  5172. nameid=conv_num(st,data);
  5173. //Violet Box, Blue Box, etc - random item pick
  5174. if( nameid < 0 ) {
  5175. nameid=itemdb_searchrandomid(-nameid);
  5176. flag = 1;
  5177. }
  5178. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5179. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5180. report_src(st);
  5181. return 1; //No item created.
  5182. }
  5183. } else {
  5184. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5185. report_src(st);
  5186. return 1;
  5187. }
  5188. // <amount>
  5189. if( (amount=script_getnum(st,3)) <= 0)
  5190. return 0; //return if amount <=0, skip the useles iteration
  5191. memset(&it,0,sizeof(it));
  5192. it.nameid=nameid;
  5193. if(!flag)
  5194. it.identify=1;
  5195. else
  5196. it.identify=itemdb_isidentified(nameid);
  5197. if( script_hasdata(st,4) )
  5198. {// <character ID>
  5199. sd=map_id2sd(script_getnum(st,4));
  5200. } else
  5201. {// attached player
  5202. sd=script_rid2sd(st);
  5203. }
  5204. if( sd == NULL ) // no target
  5205. return 0;
  5206. if( pet_create_egg(sd, nameid) )
  5207. amount = 1; //This is a pet!
  5208. else if( (flag=pc_additem(sd,&it,amount)) ){
  5209. clif_additem(sd,0,0,flag);
  5210. if( pc_candrop(sd,&it) )
  5211. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  5212. }
  5213. //Logs items, got from (N)PC scripts [Lupus]
  5214. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  5215. log_pick_pc(sd, "N", nameid, amount, NULL);
  5216. return 0;
  5217. }
  5218. /*==========================================
  5219. *
  5220. *------------------------------------------*/
  5221. BUILDIN_FUNC(getitem2)
  5222. {
  5223. int nameid,amount,flag = 0;
  5224. int iden,ref,attr,c1,c2,c3,c4;
  5225. struct item_data *item_data;
  5226. struct item item_tmp;
  5227. TBL_PC *sd;
  5228. struct script_data *data;
  5229. sd = script_rid2sd(st);
  5230. data=script_getdata(st,2);
  5231. get_val(st,data);
  5232. if( data_isstring(data) ){
  5233. const char *name=conv_str(st,data);
  5234. struct item_data *item_data = itemdb_searchname(name);
  5235. if( item_data )
  5236. nameid=item_data->nameid;
  5237. else
  5238. nameid=UNKNOWN_ITEM_ID;
  5239. }else
  5240. nameid=conv_num(st,data);
  5241. amount=script_getnum(st,3);
  5242. iden=script_getnum(st,4);
  5243. ref=script_getnum(st,5);
  5244. attr=script_getnum(st,6);
  5245. c1=script_getnum(st,7);
  5246. c2=script_getnum(st,8);
  5247. c3=script_getnum(st,9);
  5248. c4=script_getnum(st,10);
  5249. if( script_hasdata(st,11) ) //アイテムを指定したIDに渡す
  5250. sd=map_id2sd(script_getnum(st,11));
  5251. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  5252. return 0;
  5253. if(nameid<0) { // ランダム
  5254. nameid=itemdb_searchrandomid(-nameid);
  5255. flag = 1;
  5256. }
  5257. if(nameid > 0) {
  5258. memset(&item_tmp,0,sizeof(item_tmp));
  5259. item_data=itemdb_exists(nameid);
  5260. if (item_data == NULL)
  5261. return -1;
  5262. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5263. if(ref > 10) ref = 10;
  5264. }
  5265. else if(item_data->type==IT_PETEGG) {
  5266. iden = 1;
  5267. ref = 0;
  5268. }
  5269. else {
  5270. iden = 1;
  5271. ref = attr = 0;
  5272. }
  5273. item_tmp.nameid=nameid;
  5274. if(!flag)
  5275. item_tmp.identify=iden;
  5276. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5277. item_tmp.identify=0;
  5278. item_tmp.refine=ref;
  5279. item_tmp.attribute=attr;
  5280. item_tmp.card[0]=c1;
  5281. item_tmp.card[1]=c2;
  5282. item_tmp.card[2]=c3;
  5283. item_tmp.card[3]=c4;
  5284. if((flag = pc_additem(sd,&item_tmp,amount))) {
  5285. clif_additem(sd,0,0,flag);
  5286. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  5287. }
  5288. //Logs items, got from (N)PC scripts [Lupus]
  5289. if(log_config.enable_logs&0x40)
  5290. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  5291. }
  5292. return 0;
  5293. }
  5294. /*==========================================
  5295. * gets an item with someone's name inscribed [Skotlex]
  5296. * getinscribeditem item_num, character_name
  5297. * Returned Qty is always 1, only works on equip-able
  5298. * equipment
  5299. *------------------------------------------*/
  5300. BUILDIN_FUNC(getnameditem)
  5301. {
  5302. int nameid;
  5303. struct item item_tmp;
  5304. TBL_PC *sd, *tsd;
  5305. struct script_data *data;
  5306. sd = script_rid2sd(st);
  5307. if (sd == NULL)
  5308. { //Player not attached!
  5309. script_pushint(st,0);
  5310. return 0;
  5311. }
  5312. data=script_getdata(st,2);
  5313. get_val(st,data);
  5314. if( data_isstring(data) ){
  5315. const char *name=conv_str(st,data);
  5316. struct item_data *item_data = itemdb_searchname(name);
  5317. if( item_data == NULL)
  5318. { //Failed
  5319. script_pushint(st,0);
  5320. return 0;
  5321. }
  5322. nameid = item_data->nameid;
  5323. }else
  5324. nameid = conv_num(st,data);
  5325. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5326. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5327. script_pushint(st,0);
  5328. return 0;
  5329. }
  5330. data=script_getdata(st,3);
  5331. get_val(st,data);
  5332. if( data_isstring(data) ) //Char Name
  5333. tsd=map_nick2sd(conv_str(st,data));
  5334. else //Char Id was given
  5335. tsd=map_charid2sd(conv_num(st,data));
  5336. if( tsd == NULL )
  5337. { //Failed
  5338. script_pushint(st,0);
  5339. return 0;
  5340. }
  5341. memset(&item_tmp,0,sizeof(item_tmp));
  5342. item_tmp.nameid=nameid;
  5343. item_tmp.amount=1;
  5344. item_tmp.identify=1;
  5345. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5346. item_tmp.card[2]=tsd->status.char_id;
  5347. item_tmp.card[3]=tsd->status.char_id >> 16;
  5348. if(pc_additem(sd,&item_tmp,1)) {
  5349. script_pushint(st,0);
  5350. return 0; //Failed to add item, we will not drop if they don't fit
  5351. }
  5352. //Logs items, got from (N)PC scripts [Lupus]
  5353. if(log_config.enable_logs&0x40)
  5354. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5355. script_pushint(st,1);
  5356. return 0;
  5357. }
  5358. /*==========================================
  5359. * gets a random item ID from an item group [Skotlex]
  5360. * groupranditem group_num
  5361. *------------------------------------------*/
  5362. BUILDIN_FUNC(grouprandomitem)
  5363. {
  5364. int group;
  5365. group = script_getnum(st,2);
  5366. script_pushint(st,itemdb_searchrandomid(group));
  5367. return 0;
  5368. }
  5369. /*==========================================
  5370. *
  5371. *------------------------------------------*/
  5372. BUILDIN_FUNC(makeitem)
  5373. {
  5374. int nameid,amount,flag = 0;
  5375. int x,y,m;
  5376. const char *mapname;
  5377. struct item item_tmp;
  5378. struct script_data *data;
  5379. data=script_getdata(st,2);
  5380. get_val(st,data);
  5381. if( data_isstring(data) ){
  5382. const char *name=conv_str(st,data);
  5383. struct item_data *item_data = itemdb_searchname(name);
  5384. if( item_data )
  5385. nameid=item_data->nameid;
  5386. else
  5387. nameid=UNKNOWN_ITEM_ID;
  5388. }else
  5389. nameid=conv_num(st,data);
  5390. amount=script_getnum(st,3);
  5391. mapname =script_getstr(st,4);
  5392. x =script_getnum(st,5);
  5393. y =script_getnum(st,6);
  5394. if(strcmp(mapname,"this")==0)
  5395. {
  5396. TBL_PC *sd;
  5397. sd = script_rid2sd(st);
  5398. if (!sd) return 0; //Failed...
  5399. m=sd->bl.m;
  5400. } else
  5401. m=map_mapname2mapid(mapname);
  5402. if(nameid<0) { // ランダム
  5403. nameid=itemdb_searchrandomid(-nameid);
  5404. flag = 1;
  5405. }
  5406. if(nameid > 0) {
  5407. memset(&item_tmp,0,sizeof(item_tmp));
  5408. item_tmp.nameid=nameid;
  5409. if(!flag)
  5410. item_tmp.identify=1;
  5411. else
  5412. item_tmp.identify=itemdb_isidentified(nameid);
  5413. map_addflooritem(&item_tmp,amount,m,x,y,NULL,NULL,NULL,0);
  5414. }
  5415. return 0;
  5416. }
  5417. /*==========================================
  5418. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5419. *------------------------------------------*/
  5420. BUILDIN_FUNC(delitem)
  5421. {
  5422. int nameid=0,amount,i,important_item=0;
  5423. TBL_PC *sd;
  5424. struct script_data *data;
  5425. sd = script_rid2sd(st);
  5426. data=script_getdata(st,2);
  5427. get_val(st,data);
  5428. if( data_isstring(data) ){
  5429. const char *name=conv_str(st,data);
  5430. struct item_data *item_data = itemdb_searchname(name);
  5431. //nameid=UNKNOWN_ITEM_ID;
  5432. if( item_data )
  5433. nameid=item_data->nameid;
  5434. }else
  5435. nameid=conv_num(st,data);
  5436. amount=script_getnum(st,3);
  5437. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5438. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5439. return 0;
  5440. }
  5441. //1st pass
  5442. //here we won't delete items with CARDS, named items but we count them
  5443. for(i=0;i<MAX_INVENTORY;i++){
  5444. //we don't delete wrong item or equipped item
  5445. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5446. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5447. continue;
  5448. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5449. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5450. sd->status.inventory[i].card[0] == CARD0_PET)
  5451. {
  5452. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5453. continue; //pet couldn't be sent for deletion.
  5454. } else
  5455. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5456. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5457. sd->status.inventory[i].card[0] ||
  5458. sd->status.inventory[i].refine) {
  5459. //this is important item, count it (except for pet eggs)
  5460. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5461. important_item++;
  5462. continue;
  5463. }
  5464. if(sd->status.inventory[i].amount>=amount){
  5465. //Logs items, got from (N)PC scripts [Lupus]
  5466. if(log_config.enable_logs&0x40)
  5467. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5468. pc_delitem(sd,i,amount,0);
  5469. return 0; //we deleted exact amount of items. now exit
  5470. } else {
  5471. amount-=sd->status.inventory[i].amount;
  5472. //Logs items, got from (N)PC scripts [Lupus]
  5473. if(log_config.enable_logs&0x40) {
  5474. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5475. }
  5476. //Logs
  5477. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5478. }
  5479. }
  5480. //2nd pass
  5481. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5482. if (important_item>0 && amount>0)
  5483. for(i=0;i<MAX_INVENTORY;i++){
  5484. //we don't delete wrong item
  5485. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5486. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5487. continue;
  5488. if(sd->status.inventory[i].amount>=amount){
  5489. //Logs items, got from (N)PC scripts [Lupus]
  5490. if(log_config.enable_logs&0x40)
  5491. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5492. pc_delitem(sd,i,amount,0);
  5493. return 0; //we deleted exact amount of items. now exit
  5494. } else {
  5495. amount-=sd->status.inventory[i].amount;
  5496. //Logs items, got from (N)PC scripts [Lupus]
  5497. if(log_config.enable_logs&0x40)
  5498. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5499. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5500. }
  5501. }
  5502. return 0;
  5503. }
  5504. /*==========================================
  5505. * advanced version of delitem [modified by Mihilion]
  5506. *------------------------------------------*/
  5507. BUILDIN_FUNC(delitem2)
  5508. {
  5509. int nameid=0,amount,i=0;
  5510. int iden,ref,attr,c1,c2,c3,c4;
  5511. TBL_PC *sd;
  5512. struct script_data *data;
  5513. sd = script_rid2sd(st);
  5514. data=script_getdata(st,2);
  5515. get_val(st,data);
  5516. if( data_isstring(data) ){
  5517. const char *name=conv_str(st,data);
  5518. struct item_data *item_data = itemdb_searchname(name);
  5519. //nameid=UNKNOWN_ITEM_ID;
  5520. if( item_data )
  5521. nameid=item_data->nameid;
  5522. }else
  5523. nameid=conv_num(st,data);
  5524. amount=script_getnum(st,3);
  5525. iden=script_getnum(st,4);
  5526. ref=script_getnum(st,5);
  5527. attr=script_getnum(st,6);
  5528. c1=script_getnum(st,7);
  5529. c2=script_getnum(st,8);
  5530. c3=script_getnum(st,9);
  5531. c4=script_getnum(st,10);
  5532. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5533. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5534. return 0;
  5535. }
  5536. for(i=0;i<MAX_INVENTORY;i++){
  5537. //we don't delete wrong item or equipped item
  5538. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5539. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5540. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5541. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5542. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5543. sd->status.inventory[i].card[3]!=c4)
  5544. continue;
  5545. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5546. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5547. {
  5548. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5549. continue; //Failed to send delete the pet.
  5550. }
  5551. if(sd->status.inventory[i].amount>=amount){
  5552. //Logs items, got from (N)PC scripts [Lupus]
  5553. if(log_config.enable_logs&0x40)
  5554. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5555. pc_delitem(sd,i,amount,0);
  5556. return 0; //we deleted exact amount of items. now exit
  5557. } else {
  5558. amount-=sd->status.inventory[i].amount;
  5559. //Logs items, got from (N)PC scripts [Lupus]
  5560. if(log_config.enable_logs&0x40)
  5561. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5562. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5563. }
  5564. }
  5565. return 0;
  5566. }
  5567. /*==========================================
  5568. * Enables/Disables use of items while in an NPC [Skotlex]
  5569. *------------------------------------------*/
  5570. BUILDIN_FUNC(enableitemuse)
  5571. {
  5572. TBL_PC *sd;
  5573. sd=script_rid2sd(st);
  5574. if (sd)
  5575. sd->npc_item_flag = st->oid;
  5576. return 0;
  5577. }
  5578. BUILDIN_FUNC(disableitemuse)
  5579. {
  5580. TBL_PC *sd;
  5581. sd=script_rid2sd(st);
  5582. if (sd)
  5583. sd->npc_item_flag = 0;
  5584. return 0;
  5585. }
  5586. /*==========================================
  5587. *キャラ関係のパラメータ取得
  5588. *------------------------------------------*/
  5589. BUILDIN_FUNC(readparam)
  5590. {
  5591. int type;
  5592. TBL_PC *sd;
  5593. type=script_getnum(st,2);
  5594. if( script_hasdata(st,3) )
  5595. sd=map_nick2sd(script_getstr(st,3));
  5596. else
  5597. sd=script_rid2sd(st);
  5598. if(sd==NULL){
  5599. script_pushint(st,-1);
  5600. return 0;
  5601. }
  5602. script_pushint(st,pc_readparam(sd,type));
  5603. return 0;
  5604. }
  5605. /*==========================================
  5606. *キャラ関係のID取得
  5607. *------------------------------------------*/
  5608. BUILDIN_FUNC(getcharid)
  5609. {
  5610. int num;
  5611. TBL_PC *sd;
  5612. num=script_getnum(st,2);
  5613. if( script_hasdata(st,3) )
  5614. sd=map_nick2sd(script_getstr(st,3));
  5615. else
  5616. sd=script_rid2sd(st);
  5617. if(sd==NULL || num<0 || num>3){
  5618. script_pushint(st,0); //return 0, according docs
  5619. return 0;
  5620. }
  5621. if(num==0)
  5622. script_pushint(st,sd->status.char_id);
  5623. if(num==1)
  5624. script_pushint(st,sd->status.party_id);
  5625. if(num==2)
  5626. script_pushint(st,sd->status.guild_id);
  5627. if(num==3)
  5628. script_pushint(st,sd->status.account_id);
  5629. return 0;
  5630. }
  5631. /*==========================================
  5632. *指定IDのPT名取得
  5633. *------------------------------------------*/
  5634. char *buildin_getpartyname_sub(int party_id)
  5635. {
  5636. struct party_data *p;
  5637. p=party_search(party_id);
  5638. if(p!=NULL){
  5639. char *buf;
  5640. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5641. memcpy(buf, p->party.name, NAME_LENGTH);
  5642. buf[NAME_LENGTH-1] = '\0';
  5643. return buf;
  5644. }
  5645. return 0;
  5646. }
  5647. BUILDIN_FUNC(getpartyname)
  5648. {
  5649. char *name;
  5650. int party_id;
  5651. party_id=script_getnum(st,2);
  5652. name=buildin_getpartyname_sub(party_id);
  5653. if(name != NULL)
  5654. script_pushstr(st,name);
  5655. else
  5656. script_pushconststr(st,"null");
  5657. return 0;
  5658. }
  5659. /*==========================================
  5660. *指定IDのPT人数とメンバーID取得
  5661. *------------------------------------------*/
  5662. BUILDIN_FUNC(getpartymember)
  5663. {
  5664. struct party_data *p;
  5665. int i,j=0,type=0;
  5666. p=party_search(script_getnum(st,2));
  5667. if( script_hasdata(st,3) )
  5668. type=script_getnum(st,3);
  5669. if(p!=NULL){
  5670. for(i=0;i<MAX_PARTY;i++){
  5671. if(p->party.member[i].account_id){
  5672. switch (type) {
  5673. case 2:
  5674. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5675. break;
  5676. case 1:
  5677. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5678. break;
  5679. default:
  5680. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5681. }
  5682. j++;
  5683. }
  5684. }
  5685. }
  5686. mapreg_setreg(add_str("$@partymembercount"),j);
  5687. return 0;
  5688. }
  5689. /*==========================================
  5690. * Retrieves party leader. if flag is specified,
  5691. * return some of the leader data. Otherwise, return name.
  5692. *------------------------------------------*/
  5693. BUILDIN_FUNC(getpartyleader)
  5694. {
  5695. int party_id, type = 0, i=0;
  5696. struct party_data *p;
  5697. party_id=script_getnum(st,2);
  5698. if( script_hasdata(st,3) )
  5699. type=script_getnum(st,3);
  5700. p=party_search(party_id);
  5701. if (p) //Search leader
  5702. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5703. if (!p || i == MAX_PARTY) { //leader not found
  5704. if (type)
  5705. script_pushint(st,-1);
  5706. else
  5707. script_pushconststr(st,"null");
  5708. return 0;
  5709. }
  5710. switch (type) {
  5711. case 1:
  5712. script_pushint(st,p->party.member[i].account_id);
  5713. break;
  5714. case 2:
  5715. script_pushint(st,p->party.member[i].char_id);
  5716. break;
  5717. case 3:
  5718. script_pushint(st,p->party.member[i].class_);
  5719. break;
  5720. case 4:
  5721. script_pushstr(st,aStrdup(mapindex_id2name(p->party.member[i].map)));
  5722. break;
  5723. case 5:
  5724. script_pushint(st,p->party.member[i].lv);
  5725. break;
  5726. default:
  5727. script_pushstr(st,aStrdup(p->party.member[i].name));
  5728. break;
  5729. }
  5730. return 0;
  5731. }
  5732. /*==========================================
  5733. *指定IDのギルド名取得
  5734. *------------------------------------------*/
  5735. char *buildin_getguildname_sub(int guild_id)
  5736. {
  5737. struct guild *g=NULL;
  5738. g=guild_search(guild_id);
  5739. if(g!=NULL){
  5740. char *buf;
  5741. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5742. memcpy(buf, g->name, NAME_LENGTH);
  5743. buf[NAME_LENGTH-1] = '\0';
  5744. return buf;
  5745. }
  5746. return NULL;
  5747. }
  5748. BUILDIN_FUNC(getguildname)
  5749. {
  5750. char *name;
  5751. int guild_id=script_getnum(st,2);
  5752. name=buildin_getguildname_sub(guild_id);
  5753. if(name != NULL)
  5754. script_pushstr(st,name);
  5755. else
  5756. script_pushconststr(st,"null");
  5757. return 0;
  5758. }
  5759. /*==========================================
  5760. *指定IDのGuildMaster名取得
  5761. *------------------------------------------*/
  5762. char *buildin_getguildmaster_sub(int guild_id)
  5763. {
  5764. struct guild *g=NULL;
  5765. g=guild_search(guild_id);
  5766. if(g!=NULL){
  5767. char *buf;
  5768. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5769. memcpy(buf, g->master, NAME_LENGTH);
  5770. buf[NAME_LENGTH-1] = '\0';
  5771. return buf;
  5772. }
  5773. return 0;
  5774. }
  5775. BUILDIN_FUNC(getguildmaster)
  5776. {
  5777. char *master;
  5778. int guild_id=script_getnum(st,2);
  5779. master=buildin_getguildmaster_sub(guild_id);
  5780. if(master!=0)
  5781. script_pushstr(st,master);
  5782. else
  5783. script_pushconststr(st,"null");
  5784. return 0;
  5785. }
  5786. BUILDIN_FUNC(getguildmasterid)
  5787. {
  5788. char *master;
  5789. TBL_PC *sd=NULL;
  5790. int guild_id=script_getnum(st,2);
  5791. master=buildin_getguildmaster_sub(guild_id);
  5792. if(master!=0){
  5793. if((sd=map_nick2sd(master)) == NULL){
  5794. script_pushint(st,0);
  5795. return 0;
  5796. }
  5797. script_pushint(st,sd->status.char_id);
  5798. }else{
  5799. script_pushint(st,0);
  5800. }
  5801. return 0;
  5802. }
  5803. /*==========================================
  5804. * キャラクタの名前
  5805. *------------------------------------------*/
  5806. BUILDIN_FUNC(strcharinfo)
  5807. {
  5808. TBL_PC *sd;
  5809. int num;
  5810. char *buf;
  5811. sd=script_rid2sd(st);
  5812. if (!sd) { //Avoid crashing....
  5813. script_pushconststr(st,"");
  5814. return 0;
  5815. }
  5816. num=script_getnum(st,2);
  5817. switch(num){
  5818. case 0:
  5819. script_pushstr(st,aStrdup(sd->status.name));
  5820. break;
  5821. case 1:
  5822. buf=buildin_getpartyname_sub(sd->status.party_id);
  5823. if(buf!=0)
  5824. script_pushstr(st,buf);
  5825. else
  5826. script_pushconststr(st,"");
  5827. break;
  5828. case 2:
  5829. buf=buildin_getguildname_sub(sd->status.guild_id);
  5830. if(buf != NULL)
  5831. script_pushstr(st,buf);
  5832. else
  5833. script_pushconststr(st,"");
  5834. break;
  5835. default:
  5836. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5837. script_pushconststr(st,"");
  5838. break;
  5839. }
  5840. return 0;
  5841. }
  5842. 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};
  5843. /*==========================================
  5844. * GetEquipID(Pos); Pos: 1-10
  5845. *------------------------------------------*/
  5846. BUILDIN_FUNC(getequipid)
  5847. {
  5848. int i,num;
  5849. TBL_PC *sd;
  5850. struct item_data* item;
  5851. sd=script_rid2sd(st);
  5852. if(sd == NULL)
  5853. {
  5854. ShowError("getequipid: sd == NULL\n");
  5855. return 0;
  5856. }
  5857. num=script_getnum(st,2);
  5858. i=pc_checkequip(sd,equip[num-1]);
  5859. if(i >= 0){
  5860. item=sd->inventory_data[i];
  5861. if(item)
  5862. script_pushint(st,item->nameid);
  5863. else
  5864. script_pushint(st,0);
  5865. }else{
  5866. script_pushint(st,-1);
  5867. }
  5868. return 0;
  5869. }
  5870. /*==========================================
  5871. * 装備名文字列(精錬メニュー用)
  5872. *------------------------------------------*/
  5873. BUILDIN_FUNC(getequipname)
  5874. {
  5875. int i,num;
  5876. TBL_PC *sd;
  5877. struct item_data* item;
  5878. char *buf;
  5879. buf=(char *)aMallocA(64*sizeof(char));
  5880. sd=script_rid2sd(st);
  5881. num=script_getnum(st,2);
  5882. i=pc_checkequip(sd,equip[num-1]);
  5883. if(i >= 0){
  5884. item=sd->inventory_data[i];
  5885. if(item)
  5886. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5887. else
  5888. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5889. }else{
  5890. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5891. }
  5892. script_pushstr(st,buf);
  5893. return 0;
  5894. }
  5895. /*==========================================
  5896. * getbrokenid [Valaris]
  5897. *------------------------------------------*/
  5898. BUILDIN_FUNC(getbrokenid)
  5899. {
  5900. int i,num,id=0,brokencounter=0;
  5901. TBL_PC *sd;
  5902. sd=script_rid2sd(st);
  5903. num=script_getnum(st,2);
  5904. for(i=0; i<MAX_INVENTORY; i++) {
  5905. if(sd->status.inventory[i].attribute==1){
  5906. brokencounter++;
  5907. if(num==brokencounter){
  5908. id=sd->status.inventory[i].nameid;
  5909. break;
  5910. }
  5911. }
  5912. }
  5913. script_pushint(st,id);
  5914. return 0;
  5915. }
  5916. /*==========================================
  5917. * repair [Valaris]
  5918. *------------------------------------------*/
  5919. BUILDIN_FUNC(repair)
  5920. {
  5921. int i,num;
  5922. int repaircounter=0;
  5923. TBL_PC *sd;
  5924. sd=script_rid2sd(st);
  5925. num=script_getnum(st,2);
  5926. for(i=0; i<MAX_INVENTORY; i++) {
  5927. if(sd->status.inventory[i].attribute==1){
  5928. repaircounter++;
  5929. if(num==repaircounter){
  5930. sd->status.inventory[i].attribute=0;
  5931. clif_equiplist(sd);
  5932. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5933. clif_misceffect(&sd->bl, 3);
  5934. break;
  5935. }
  5936. }
  5937. }
  5938. return 0;
  5939. }
  5940. /*==========================================
  5941. * 装備チェック
  5942. *------------------------------------------*/
  5943. BUILDIN_FUNC(getequipisequiped)
  5944. {
  5945. int i,num;
  5946. TBL_PC *sd;
  5947. num=script_getnum(st,2);
  5948. sd=script_rid2sd(st);
  5949. if ((num - 1) >= (sizeof(equip) / sizeof(equip[0])))
  5950. i = -1;
  5951. else
  5952. i=pc_checkequip(sd,equip[num-1]);
  5953. if(i >= 0)
  5954. script_pushint(st,1);
  5955. else
  5956. script_pushint(st,0);
  5957. return 0;
  5958. }
  5959. /*==========================================
  5960. * 装備品精錬可能チェック
  5961. *------------------------------------------*/
  5962. BUILDIN_FUNC(getequipisenableref)
  5963. {
  5964. int i,num;
  5965. TBL_PC *sd;
  5966. num=script_getnum(st,2);
  5967. sd=script_rid2sd(st);
  5968. i=pc_checkequip(sd,equip[num-1]);
  5969. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5970. {
  5971. script_pushint(st,1);
  5972. } else {
  5973. script_pushint(st,0);
  5974. }
  5975. return 0;
  5976. }
  5977. /*==========================================
  5978. * 装備品鑑定チェック
  5979. *------------------------------------------*/
  5980. BUILDIN_FUNC(getequipisidentify)
  5981. {
  5982. int i,num;
  5983. TBL_PC *sd;
  5984. num=script_getnum(st,2);
  5985. sd=script_rid2sd(st);
  5986. i=pc_checkequip(sd,equip[num-1]);
  5987. if(i >= 0)
  5988. script_pushint(st,sd->status.inventory[i].identify);
  5989. else
  5990. script_pushint(st,0);
  5991. return 0;
  5992. }
  5993. /*==========================================
  5994. * 装備品精錬度
  5995. *------------------------------------------*/
  5996. BUILDIN_FUNC(getequiprefinerycnt)
  5997. {
  5998. int i,num;
  5999. TBL_PC *sd;
  6000. num=script_getnum(st,2);
  6001. sd=script_rid2sd(st);
  6002. i=pc_checkequip(sd,equip[num-1]);
  6003. if(i >= 0)
  6004. script_pushint(st,sd->status.inventory[i].refine);
  6005. else
  6006. script_pushint(st,0);
  6007. return 0;
  6008. }
  6009. /*==========================================
  6010. * 装備品武器LV
  6011. *------------------------------------------*/
  6012. BUILDIN_FUNC(getequipweaponlv)
  6013. {
  6014. int i,num;
  6015. TBL_PC *sd;
  6016. num=script_getnum(st,2);
  6017. sd=script_rid2sd(st);
  6018. i=pc_checkequip(sd,equip[num-1]);
  6019. if(i >= 0 && sd->inventory_data[i])
  6020. script_pushint(st,sd->inventory_data[i]->wlv);
  6021. else
  6022. script_pushint(st,0);
  6023. return 0;
  6024. }
  6025. /*==========================================
  6026. * 装備品精錬成功率
  6027. *------------------------------------------*/
  6028. BUILDIN_FUNC(getequippercentrefinery)
  6029. {
  6030. int i,num;
  6031. TBL_PC *sd;
  6032. num=script_getnum(st,2);
  6033. sd=script_rid2sd(st);
  6034. i=pc_checkequip(sd,equip[num-1]);
  6035. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6036. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  6037. else
  6038. script_pushint(st,0);
  6039. return 0;
  6040. }
  6041. /*==========================================
  6042. * 精錬成功
  6043. *------------------------------------------*/
  6044. BUILDIN_FUNC(successrefitem)
  6045. {
  6046. int i,num,ep;
  6047. TBL_PC *sd;
  6048. num=script_getnum(st,2);
  6049. sd=script_rid2sd(st);
  6050. i=pc_checkequip(sd,equip[num-1]);
  6051. if(i >= 0) {
  6052. ep=sd->status.inventory[i].equip;
  6053. //Logs items, got from (N)PC scripts [Lupus]
  6054. if(log_config.enable_logs&0x40)
  6055. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6056. sd->status.inventory[i].refine++;
  6057. pc_unequipitem(sd,i,2);
  6058. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6059. clif_delitem(sd,i,1);
  6060. //Logs items, got from (N)PC scripts [Lupus]
  6061. if(log_config.enable_logs&0x40)
  6062. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  6063. clif_additem(sd,i,1,0);
  6064. pc_equipitem(sd,i,ep);
  6065. clif_misceffect(&sd->bl,3);
  6066. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6067. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6068. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6069. ){ // Fame point system [DracoRPG]
  6070. switch (sd->inventory_data[i]->wlv){
  6071. case 1:
  6072. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6073. break;
  6074. case 2:
  6075. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6076. break;
  6077. case 3:
  6078. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6079. break;
  6080. }
  6081. }
  6082. }
  6083. return 0;
  6084. }
  6085. /*==========================================
  6086. * 精錬失敗
  6087. *------------------------------------------*/
  6088. BUILDIN_FUNC(failedrefitem)
  6089. {
  6090. int i,num;
  6091. TBL_PC *sd;
  6092. num=script_getnum(st,2);
  6093. sd=script_rid2sd(st);
  6094. i=pc_checkequip(sd,equip[num-1]);
  6095. if(i >= 0) {
  6096. //Logs items, got from (N)PC scripts [Lupus]
  6097. if(log_config.enable_logs&0x40)
  6098. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6099. sd->status.inventory[i].refine = 0;
  6100. pc_unequipitem(sd,i,3);
  6101. // 精錬失敗エフェクトのパケット
  6102. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6103. pc_delitem(sd,i,1,0);
  6104. // 他の人にも失敗を通知
  6105. clif_misceffect(&sd->bl,2);
  6106. }
  6107. return 0;
  6108. }
  6109. /*==========================================
  6110. *
  6111. *------------------------------------------*/
  6112. BUILDIN_FUNC(statusup)
  6113. {
  6114. int type;
  6115. TBL_PC *sd;
  6116. type=script_getnum(st,2);
  6117. sd=script_rid2sd(st);
  6118. pc_statusup(sd,type);
  6119. return 0;
  6120. }
  6121. /*==========================================
  6122. *
  6123. *------------------------------------------*/
  6124. BUILDIN_FUNC(statusup2)
  6125. {
  6126. int type,val;
  6127. TBL_PC *sd;
  6128. type=script_getnum(st,2);
  6129. val=script_getnum(st,3);
  6130. sd=script_rid2sd(st);
  6131. pc_statusup2(sd,type,val);
  6132. return 0;
  6133. }
  6134. /// See 'doc/item_bonus.txt'
  6135. ///
  6136. /// bonus <bonus type>,<val1>;
  6137. /// bonus2 <bonus type>,<val1>,<val2>;
  6138. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6139. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6140. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6141. BUILDIN_FUNC(bonus)
  6142. {
  6143. int type;
  6144. int type2;
  6145. int type3;
  6146. int type4;
  6147. int type5;
  6148. int val;
  6149. TBL_PC* sd;
  6150. sd = script_rid2sd(st);
  6151. if( sd == NULL )
  6152. return 1; // no player attached
  6153. type = script_getnum(st,2);
  6154. switch( script_lastdata(st) ){
  6155. case 3:
  6156. val = script_getnum(st,3);
  6157. pc_bonus(sd, type, val);
  6158. break;
  6159. case 4:
  6160. type2 = script_getnum(st,3);
  6161. val = script_getnum(st,4);
  6162. pc_bonus2(sd, type, type2, val);
  6163. break;
  6164. case 5:
  6165. type2 = script_getnum(st,3);
  6166. type3 = script_getnum(st,4);
  6167. val = script_getnum(st,5);
  6168. pc_bonus3(sd, type, type2, type3, val);
  6169. break;
  6170. case 6:
  6171. type2 = script_getnum(st,3);
  6172. type3 = script_getnum(st,4);
  6173. type4 = script_getnum(st,5);
  6174. val = script_getnum(st,6);
  6175. pc_bonus4(sd, type, type2, type3, type4, val);
  6176. break;
  6177. case 7:
  6178. type2 = script_getnum(st,3);
  6179. type3 = script_getnum(st,4);
  6180. type4 = script_getnum(st,5);
  6181. type5 = script_getnum(st,6);
  6182. val = script_getnum(st,7);
  6183. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  6184. break;
  6185. default:
  6186. ShowDebug("buildin_bonus: unexpected last data (%d)\n", script_lastdata(st));
  6187. }
  6188. return 0;
  6189. }
  6190. /// Changes the level of a player skill.
  6191. /// <flag> defaults to 1
  6192. /// <flag>=0 : set the level of the skill
  6193. /// <flag>=1 : set the temporary level of the skill
  6194. /// <flag>=2 : add to the level of the skill
  6195. ///
  6196. /// skill <skill id>,<level>,<flag>
  6197. /// skill <skill id>,<level>
  6198. BUILDIN_FUNC(skill)
  6199. {
  6200. int id;
  6201. int level;
  6202. int flag = 1;
  6203. TBL_PC* sd;
  6204. sd = script_rid2sd(st);
  6205. if( sd == NULL )
  6206. return 1;// no player attached, report source
  6207. id = script_getnum(st,2);
  6208. level = script_getnum(st,3);
  6209. if( script_hasdata(st,4) )
  6210. flag = script_getnum(st,4);
  6211. pc_skill(sd, id, level, flag);
  6212. return 0;
  6213. }
  6214. /// Changes the level of a player skill.
  6215. /// like skill, but <flag> defaults to 2
  6216. ///
  6217. /// addtoskill <skill id>,<amount>,<flag>
  6218. /// addtoskill <skill id>,<amount>
  6219. ///
  6220. /// @see skill
  6221. BUILDIN_FUNC(addtoskill)
  6222. {
  6223. int id;
  6224. int level;
  6225. int flag = 2;
  6226. TBL_PC* sd;
  6227. sd = script_rid2sd(st);
  6228. if( sd == NULL )
  6229. return 1;// no player attached, report source
  6230. id = script_getnum(st,2);
  6231. level = script_getnum(st,3);
  6232. if( script_hasdata(st,4) )
  6233. flag = script_getnum(st,4);
  6234. pc_skill(sd, id, level, flag);
  6235. return 0;
  6236. }
  6237. /// Increases the level of a guild skill.
  6238. ///
  6239. /// guildskill <skill id>,<amount>;
  6240. BUILDIN_FUNC(guildskill)
  6241. {
  6242. int id;
  6243. int level;
  6244. TBL_PC* sd;
  6245. int i;
  6246. sd = script_rid2sd(st);
  6247. if( sd == NULL )
  6248. return 1;// no player attached, report source
  6249. id = script_getnum(st,2);
  6250. level = script_getnum(st,3);
  6251. for( i=0; i < level; i++ )
  6252. guild_skillup(sd, id);
  6253. return 0;
  6254. }
  6255. /// Returns the level of the player skill.
  6256. ///
  6257. /// getskilllv(<skill id>) -> <level>
  6258. BUILDIN_FUNC(getskilllv)
  6259. {
  6260. int id;
  6261. TBL_PC* sd;
  6262. sd = script_rid2sd(st);
  6263. if( sd == NULL )
  6264. return 1;// no player attached, report source
  6265. id = script_getnum(st,2);
  6266. script_pushint(st, pc_checkskill(sd,id));
  6267. return 0;
  6268. }
  6269. /// Returns the level of the guild skill.
  6270. ///
  6271. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6272. BUILDIN_FUNC(getgdskilllv)
  6273. {
  6274. int guild_id;
  6275. int skill_id;
  6276. struct guild* g;
  6277. guild_id = script_getnum(st,2);
  6278. skill_id = script_getnum(st,3);
  6279. g = guild_search(guild_id);
  6280. if( g == NULL )
  6281. script_pushint(st, -1);
  6282. else
  6283. script_pushint(st, guild_checkskill(g,skill_id));
  6284. return 0;
  6285. }
  6286. /// Returns the 'basic_skill_check' setting.
  6287. /// This config determines if the server checks the skill level of NV_BASIC
  6288. /// before allowing the basic actions.
  6289. ///
  6290. /// basicskillcheck() -> <bool>
  6291. BUILDIN_FUNC(basicskillcheck)
  6292. {
  6293. script_pushint(st, battle_config.basic_skill_check);
  6294. return 0;
  6295. }
  6296. /// Returns the GM level of the player.
  6297. ///
  6298. /// getgmlevel() -> <level>
  6299. BUILDIN_FUNC(getgmlevel)
  6300. {
  6301. TBL_PC* sd;
  6302. sd = script_rid2sd(st);
  6303. if( sd == NULL )
  6304. return 1;// no player attached, report source
  6305. script_pushint(st, pc_isGM(sd));
  6306. return 0;
  6307. }
  6308. /// Terminates the execution of this script instance.
  6309. ///
  6310. /// end
  6311. BUILDIN_FUNC(end)
  6312. {
  6313. st->state = END;
  6314. return 0;
  6315. }
  6316. /// Checks if the player has that effect state (option).
  6317. ///
  6318. /// checkoption(<option>) -> <bool>
  6319. BUILDIN_FUNC(checkoption)
  6320. {
  6321. int option;
  6322. TBL_PC* sd;
  6323. sd = script_rid2sd(st);
  6324. if( sd == NULL )
  6325. return 1;// no player attached, report source
  6326. option = script_getnum(st,2);
  6327. if( sd->sc.option&option )
  6328. script_pushint(st, 1);
  6329. else
  6330. script_pushint(st, 0);
  6331. return 0;
  6332. }
  6333. /// Checks if the player is in that body state (opt1).
  6334. ///
  6335. /// checkoption1(<opt1>) -> <bool>
  6336. BUILDIN_FUNC(checkoption1)
  6337. {
  6338. int opt1;
  6339. TBL_PC* sd;
  6340. sd = script_rid2sd(st);
  6341. if( sd == NULL )
  6342. return 1;// no player attached, report source
  6343. opt1 = script_getnum(st,2);
  6344. if( sd->sc.opt1 == opt1 )
  6345. script_pushint(st, 1);
  6346. else
  6347. script_pushint(st, 0);
  6348. return 0;
  6349. }
  6350. /// Checks if the player has that health state (opt2).
  6351. ///
  6352. /// checkoption2(<opt2>) -> <bool>
  6353. BUILDIN_FUNC(checkoption2)
  6354. {
  6355. int opt2;
  6356. TBL_PC* sd;
  6357. sd = script_rid2sd(st);
  6358. if( sd == NULL )
  6359. return 1;// no player attached, report source
  6360. opt2 = script_getnum(st,2);
  6361. if( sd->sc.opt2&opt2 )
  6362. script_pushint(st, 1);
  6363. else
  6364. script_pushint(st, 0);
  6365. return 0;
  6366. }
  6367. /// Changes the effect state (option) of the player.
  6368. /// <flag> defaults to 1
  6369. /// <flag>=0 : removes the option
  6370. /// <flag>=other : adds the option
  6371. ///
  6372. /// setoption <option>,<flag>;
  6373. /// setoption <option>;
  6374. BUILDIN_FUNC(setoption)
  6375. {
  6376. int option;
  6377. int flag = 1;
  6378. TBL_PC* sd;
  6379. sd = script_rid2sd(st);
  6380. if( sd == NULL )
  6381. return 1;// no player attached, report source
  6382. option = script_getnum(st,2);
  6383. if( script_hasdata(st,3) )
  6384. flag = script_getnum(st,3);
  6385. else if( !option ){// Request to remove everything.
  6386. flag = 0;
  6387. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6388. }
  6389. if( flag ){// Add option
  6390. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6391. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6392. pc_setoption(sd, sd->sc.option|option);
  6393. } else// Remove option
  6394. pc_setoption(sd, sd->sc.option&~option);
  6395. return 0;
  6396. }
  6397. /// Returns if the player has a cart.
  6398. ///
  6399. /// checkcart() -> <bool>
  6400. ///
  6401. /// @author Valaris
  6402. BUILDIN_FUNC(checkcart)
  6403. {
  6404. TBL_PC* sd;
  6405. sd = script_rid2sd(st);
  6406. if( sd == NULL )
  6407. return 1;// no player attached, report source
  6408. if( pc_iscarton(sd) )
  6409. script_pushint(st, 1);
  6410. else
  6411. script_pushint(st, 0);
  6412. return 0;
  6413. }
  6414. /// Sets the cart of the player.
  6415. /// <type> defaults to 1
  6416. /// <type>=0 : removes the cart
  6417. /// <type>=1 : Normal cart
  6418. /// <type>=2 : Wooden cart
  6419. /// <type>=3 : Covered cart with flowers and ferns
  6420. /// <type>=4 : Wooden cart with a Panda doll on the back
  6421. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6422. ///
  6423. /// setcart <type>;
  6424. /// setcart;
  6425. BUILDIN_FUNC(setcart)
  6426. {
  6427. int type = 1;
  6428. TBL_PC* sd;
  6429. sd = script_rid2sd(st);
  6430. if( sd == NULL )
  6431. return 1;// no player attached, report source
  6432. if( script_hasdata(st,2) )
  6433. type = script_getnum(st,2);
  6434. pc_setcart(sd, type);
  6435. return 0;
  6436. }
  6437. /// Returns if the player has a falcon.
  6438. ///
  6439. /// checkfalcon() -> <bool>
  6440. ///
  6441. /// @author Valaris
  6442. BUILDIN_FUNC(checkfalcon)
  6443. {
  6444. TBL_PC* sd;
  6445. sd = script_rid2sd(st);
  6446. if( sd == NULL )
  6447. return 1;// no player attached, report source
  6448. if( pc_isfalcon(sd) )
  6449. script_pushint(st, 1);
  6450. else
  6451. script_pushint(st, 0);
  6452. return 0;
  6453. }
  6454. /// Sets if the player has a falcon or not.
  6455. /// <flag> defaults to 1
  6456. ///
  6457. /// setfalcon <flag>;
  6458. /// setfalcon;
  6459. BUILDIN_FUNC(setfalcon)
  6460. {
  6461. int flag = 1;
  6462. TBL_PC* sd;
  6463. sd = script_rid2sd(st);
  6464. if( sd == NULL )
  6465. return 1;// no player attached, report source
  6466. if( script_hasdata(st,2) )
  6467. flag = script_getnum(st,2);
  6468. pc_setfalcon(sd, flag);
  6469. return 0;
  6470. }
  6471. /// Returns if the player is riding.
  6472. ///
  6473. /// checkriding() -> <bool>
  6474. ///
  6475. /// @author Valaris
  6476. BUILDIN_FUNC(checkriding)
  6477. {
  6478. TBL_PC* sd;
  6479. sd = script_rid2sd(st);
  6480. if( sd == NULL )
  6481. return 1;// no player attached, report source
  6482. if( pc_isriding(sd) )
  6483. script_pushint(st, 1);
  6484. else
  6485. script_pushint(st, 0);
  6486. return 0;
  6487. }
  6488. /// Sets if the player is riding.
  6489. /// <flag> defaults to 1
  6490. ///
  6491. /// setriding <flag>;
  6492. /// setriding;
  6493. BUILDIN_FUNC(setriding)
  6494. {
  6495. int flag = 1;
  6496. TBL_PC* sd;
  6497. sd = script_rid2sd(st);
  6498. if( sd == NULL )
  6499. return 1;// no player attached, report source
  6500. if( script_hasdata(st,2) )
  6501. flag = script_getnum(st,2);
  6502. pc_setriding(sd, flag);
  6503. return 0;
  6504. }
  6505. /// Sets the save point of the player.
  6506. ///
  6507. /// save "<map name>",<x>,<y>
  6508. /// savepoint "<map name>",<x>,<y>
  6509. BUILDIN_FUNC(savepoint)
  6510. {
  6511. int x;
  6512. int y;
  6513. short map;
  6514. const char* str;
  6515. TBL_PC* sd;
  6516. sd = script_rid2sd(st);
  6517. if( sd == NULL )
  6518. return 1;// no player attached, report source
  6519. str = script_getstr(st, 2);
  6520. x = script_getnum(st,3);
  6521. y = script_getnum(st,4);
  6522. map = mapindex_name2id(str);
  6523. if( map )
  6524. pc_setsavepoint(sd, map, x, y);
  6525. return 0;
  6526. }
  6527. /*==========================================
  6528. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6529. *------------------------------------------*/
  6530. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6531. {
  6532. int type;
  6533. time_t timer;
  6534. struct tm *t;
  6535. type=script_getnum(st,2);
  6536. switch(type){
  6537. case 2:
  6538. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6539. // from the system clock.)
  6540. script_pushint(st,(int)time(NULL));
  6541. break;
  6542. case 1:
  6543. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6544. time(&timer);
  6545. t=localtime(&timer);
  6546. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6547. break;
  6548. case 0:
  6549. default:
  6550. //type 0:(System Ticks)
  6551. script_pushint(st,gettick());
  6552. break;
  6553. }
  6554. return 0;
  6555. }
  6556. /*==========================================
  6557. * GetTime(Type);
  6558. * 1: Sec 2: Min 3: Hour
  6559. * 4: WeekDay 5: MonthDay 6: Month
  6560. * 7: Year
  6561. *------------------------------------------*/
  6562. BUILDIN_FUNC(gettime) /* Asgard Version */
  6563. {
  6564. int type;
  6565. time_t timer;
  6566. struct tm *t;
  6567. type=script_getnum(st,2);
  6568. time(&timer);
  6569. t=localtime(&timer);
  6570. switch(type){
  6571. case 1://Sec(0~59)
  6572. script_pushint(st,t->tm_sec);
  6573. break;
  6574. case 2://Min(0~59)
  6575. script_pushint(st,t->tm_min);
  6576. break;
  6577. case 3://Hour(0~23)
  6578. script_pushint(st,t->tm_hour);
  6579. break;
  6580. case 4://WeekDay(0~6)
  6581. script_pushint(st,t->tm_wday);
  6582. break;
  6583. case 5://MonthDay(01~31)
  6584. script_pushint(st,t->tm_mday);
  6585. break;
  6586. case 6://Month(01~12)
  6587. script_pushint(st,t->tm_mon+1);
  6588. break;
  6589. case 7://Year(20xx)
  6590. script_pushint(st,t->tm_year+1900);
  6591. break;
  6592. case 8://Year Day(01~366)
  6593. script_pushint(st,t->tm_yday+1);
  6594. break;
  6595. default://(format error)
  6596. script_pushint(st,-1);
  6597. break;
  6598. }
  6599. return 0;
  6600. }
  6601. /*==========================================
  6602. * GetTimeStr("TimeFMT", Length);
  6603. *------------------------------------------*/
  6604. BUILDIN_FUNC(gettimestr)
  6605. {
  6606. char *tmpstr;
  6607. const char *fmtstr;
  6608. int maxlen;
  6609. time_t now = time(NULL);
  6610. fmtstr=script_getstr(st,2);
  6611. maxlen=script_getnum(st,3);
  6612. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6613. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6614. tmpstr[maxlen]='\0';
  6615. script_pushstr(st,tmpstr);
  6616. return 0;
  6617. }
  6618. /*==========================================
  6619. * カプラ倉庫を開く
  6620. *------------------------------------------*/
  6621. BUILDIN_FUNC(openstorage)
  6622. {
  6623. storage_storageopen(script_rid2sd(st));
  6624. return 0;
  6625. }
  6626. BUILDIN_FUNC(guildopenstorage)
  6627. {
  6628. TBL_PC *sd=script_rid2sd(st);
  6629. int ret;
  6630. ret = storage_guild_storageopen(sd);
  6631. script_pushint(st,ret);
  6632. return 0;
  6633. }
  6634. /*==========================================
  6635. * アイテムによるスキル発動
  6636. *------------------------------------------*/
  6637. BUILDIN_FUNC(itemskill)
  6638. {
  6639. int id,lv;
  6640. TBL_PC *sd=script_rid2sd(st);
  6641. id=script_getnum(st,2);
  6642. lv=script_getnum(st,3);
  6643. if(!sd || sd->ud.skilltimer != -1)
  6644. return 0;
  6645. sd->skillitem=id;
  6646. sd->skillitemlv=lv;
  6647. clif_item_skill(sd,id,lv);
  6648. return 0;
  6649. }
  6650. /*==========================================
  6651. * アイテム作成
  6652. *------------------------------------------*/
  6653. BUILDIN_FUNC(produce)
  6654. {
  6655. int trigger;
  6656. TBL_PC *sd=script_rid2sd(st);
  6657. trigger=script_getnum(st,2);
  6658. clif_skill_produce_mix_list(sd, trigger);
  6659. return 0;
  6660. }
  6661. /*==========================================
  6662. * NPCでペット作る
  6663. *------------------------------------------*/
  6664. BUILDIN_FUNC(makepet)
  6665. {
  6666. TBL_PC *sd = script_rid2sd(st);
  6667. int id,pet_id;
  6668. id=script_getnum(st,2);
  6669. pet_id = search_petDB_index(id, PET_CLASS);
  6670. if (pet_id < 0)
  6671. pet_id = search_petDB_index(id, PET_EGG);
  6672. if (pet_id >= 0 && sd) {
  6673. sd->catch_target_class = pet_db[pet_id].class_;
  6674. intif_create_pet(
  6675. sd->status.account_id, sd->status.char_id,
  6676. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6677. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6678. 100, 0, 1, pet_db[pet_id].jname);
  6679. }
  6680. return 0;
  6681. }
  6682. /*==========================================
  6683. * NPCで経験値上げる
  6684. *------------------------------------------*/
  6685. BUILDIN_FUNC(getexp)
  6686. {
  6687. TBL_PC *sd = script_rid2sd(st);
  6688. int base=0,job=0;
  6689. base=script_getnum(st,2);
  6690. job =script_getnum(st,3);
  6691. if(base<0 || job<0)
  6692. return 0;
  6693. if(sd)
  6694. pc_gainexp(sd,NULL,base,job);
  6695. return 0;
  6696. }
  6697. /*==========================================
  6698. * Gain guild exp [Celest]
  6699. *------------------------------------------*/
  6700. BUILDIN_FUNC(guildgetexp)
  6701. {
  6702. TBL_PC *sd = script_rid2sd(st);
  6703. int exp;
  6704. exp = script_getnum(st,2);
  6705. if(exp < 0)
  6706. return 0;
  6707. if(sd && sd->status.guild_id > 0)
  6708. guild_getexp (sd, exp);
  6709. return 0;
  6710. }
  6711. /*==========================================
  6712. * Changes the guild master of a guild [Skotlex]
  6713. *------------------------------------------*/
  6714. BUILDIN_FUNC(guildchangegm)
  6715. {
  6716. TBL_PC *sd;
  6717. int guild_id;
  6718. const char *name;
  6719. guild_id = script_getnum(st,2);
  6720. name = script_getstr(st,3);
  6721. sd=map_nick2sd(name);
  6722. if (!sd)
  6723. script_pushint(st,0);
  6724. else
  6725. script_pushint(st,guild_gm_change(guild_id, sd));
  6726. return 0;
  6727. }
  6728. /*==========================================
  6729. * モンスター発生
  6730. *------------------------------------------*/
  6731. BUILDIN_FUNC(monster)
  6732. {
  6733. int class_,amount,x,y;
  6734. const char *str,*map,*event="";
  6735. map =script_getstr(st,2);
  6736. x =script_getnum(st,3);
  6737. y =script_getnum(st,4);
  6738. str =script_getstr(st,5);
  6739. class_=script_getnum(st,6);
  6740. amount=script_getnum(st,7);
  6741. if( script_hasdata(st,8) ){
  6742. event=script_getstr(st,8);
  6743. check_event(st, event);
  6744. }
  6745. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6746. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6747. return 1;
  6748. }
  6749. mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,amount,event);
  6750. return 0;
  6751. }
  6752. /*==========================================
  6753. * モンスター発生
  6754. *------------------------------------------*/
  6755. BUILDIN_FUNC(areamonster)
  6756. {
  6757. int class_,amount,x0,y0,x1,y1;
  6758. const char *str,*map,*event="";
  6759. map =script_getstr(st,2);
  6760. x0 =script_getnum(st,3);
  6761. y0 =script_getnum(st,4);
  6762. x1 =script_getnum(st,5);
  6763. y1 =script_getnum(st,6);
  6764. str =script_getstr(st,7);
  6765. class_=script_getnum(st,8);
  6766. amount=script_getnum(st,9);
  6767. if( script_hasdata(st,10) ){
  6768. event=script_getstr(st,10);
  6769. check_event(st, event);
  6770. }
  6771. mob_once_spawn_area(map_id2sd(st->rid),map,x0,y0,x1,y1,str,class_,amount,event);
  6772. return 0;
  6773. }
  6774. /*==========================================
  6775. * モンスター削除
  6776. *------------------------------------------*/
  6777. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6778. {
  6779. TBL_MOB* md = (TBL_MOB*)bl;
  6780. char *event=va_arg(ap,char *);
  6781. int allflag=va_arg(ap,int);
  6782. if(!allflag){
  6783. if(strcmp(event,md->npc_event)==0)
  6784. status_kill(bl);
  6785. }else{
  6786. if(!md->spawn)
  6787. status_kill(bl);
  6788. }
  6789. return 0;
  6790. }
  6791. BUILDIN_FUNC(killmonster)
  6792. {
  6793. const char *mapname,*event;
  6794. int m,allflag=0;
  6795. mapname=script_getstr(st,2);
  6796. event=script_getstr(st,3);
  6797. if(strcmp(event,"All")==0)
  6798. allflag = 1;
  6799. else
  6800. check_event(st, event);
  6801. if( (m=map_mapname2mapid(mapname))<0 )
  6802. return 0;
  6803. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6804. return 0;
  6805. }
  6806. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6807. {
  6808. status_kill(bl);
  6809. return 0;
  6810. }
  6811. BUILDIN_FUNC(killmonsterall)
  6812. {
  6813. const char *mapname;
  6814. int m;
  6815. mapname=script_getstr(st,2);
  6816. if( (m=map_mapname2mapid(mapname))<0 )
  6817. return 0;
  6818. map_foreachinmap(buildin_killmonsterall_sub,
  6819. m,BL_MOB);
  6820. return 0;
  6821. }
  6822. /*==========================================
  6823. * Creates a clone of a player.
  6824. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6825. *------------------------------------------*/
  6826. BUILDIN_FUNC(clone)
  6827. {
  6828. TBL_PC *sd, *msd=NULL;
  6829. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6830. unsigned int duration = 0;
  6831. const char *map,*event="";
  6832. map=script_getstr(st,2);
  6833. x=script_getnum(st,3);
  6834. y=script_getnum(st,4);
  6835. event=script_getstr(st,5);
  6836. char_id=script_getnum(st,6);
  6837. if( script_hasdata(st,7) )
  6838. master_id=script_getnum(st,7);
  6839. if( script_hasdata(st,8) )
  6840. mode=script_getnum(st,8);
  6841. if( script_hasdata(st,9) )
  6842. flag=script_getnum(st,9);
  6843. if( script_hasdata(st,10) )
  6844. duration=script_getnum(st,10);
  6845. check_event(st, event);
  6846. m = map_mapname2mapid(map);
  6847. if (m < 0) return 0;
  6848. sd = map_charid2sd(char_id);
  6849. if (master_id) {
  6850. msd = map_charid2sd(master_id);
  6851. if (msd)
  6852. master_id = msd->bl.id;
  6853. else
  6854. master_id = 0;
  6855. }
  6856. if (sd) //Return ID of newly crafted clone.
  6857. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6858. else //Failed to create clone.
  6859. script_pushint(st,0);
  6860. return 0;
  6861. }
  6862. /*==========================================
  6863. * イベント実行
  6864. *------------------------------------------*/
  6865. BUILDIN_FUNC(doevent)
  6866. {
  6867. const char *event;
  6868. event=script_getstr(st,2);
  6869. check_event(st, event);
  6870. npc_event(map_id2sd(st->rid),event,0);
  6871. return 0;
  6872. }
  6873. /*==========================================
  6874. * NPC主体イベント実行
  6875. *------------------------------------------*/
  6876. BUILDIN_FUNC(donpcevent)
  6877. {
  6878. const char *event;
  6879. event=script_getstr(st,2);
  6880. check_event(st, event);
  6881. npc_event_do(event);
  6882. return 0;
  6883. }
  6884. /*==========================================
  6885. * イベントタイマー追加
  6886. *------------------------------------------*/
  6887. BUILDIN_FUNC(addtimer)
  6888. {
  6889. const char *event;
  6890. int tick;
  6891. tick=script_getnum(st,2);
  6892. event=script_getstr(st, 3);
  6893. check_event(st, event);
  6894. pc_addeventtimer(script_rid2sd(st),tick,event);
  6895. return 0;
  6896. }
  6897. /*==========================================
  6898. * イベントタイマー削除
  6899. *------------------------------------------*/
  6900. BUILDIN_FUNC(deltimer)
  6901. {
  6902. const char *event;
  6903. event=script_getstr(st, 2);
  6904. check_event(st, event);
  6905. pc_deleventtimer(script_rid2sd(st),event);
  6906. return 0;
  6907. }
  6908. /*==========================================
  6909. * イベントタイマーのカウント値追加
  6910. *------------------------------------------*/
  6911. BUILDIN_FUNC(addtimercount)
  6912. {
  6913. const char *event;
  6914. int tick;
  6915. event=script_getstr(st, 2);
  6916. tick=script_getnum(st,3);
  6917. check_event(st, event);
  6918. pc_addeventtimercount(script_rid2sd(st),event,tick);
  6919. return 0;
  6920. }
  6921. /*==========================================
  6922. * NPCタイマー初期化
  6923. *------------------------------------------*/
  6924. BUILDIN_FUNC(initnpctimer)
  6925. {
  6926. struct npc_data *nd;
  6927. int flag = 0;
  6928. if( script_hasdata(st,3) )
  6929. { //Two arguments: NPC name and attach flag.
  6930. nd = npc_name2id(script_getstr(st, 2));
  6931. flag = script_getnum(st,3);
  6932. } else
  6933. if( script_hasdata(st,2) )
  6934. { //Check if argument is numeric (flag) or string (npc name)
  6935. struct script_data *data;
  6936. data = script_getdata(st,2);
  6937. get_val(st,data);
  6938. if( data_isstring(data) ) //NPC name
  6939. nd = npc_name2id(script_getstr(st, 2));
  6940. else if( data_isint(data) ) { //Flag
  6941. nd = (struct npc_data *)map_id2bl(st->oid);
  6942. flag = script_getnum(st,3);
  6943. } else {
  6944. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6945. return 1;
  6946. }
  6947. } else
  6948. nd=(struct npc_data *)map_id2bl(st->oid);
  6949. if (!nd) return 0;
  6950. if (flag) { //Attach
  6951. TBL_PC* sd = script_rid2sd(st);
  6952. if (sd) nd->u.scr.rid = sd->bl.id;
  6953. }
  6954. npc_settimerevent_tick(nd,0);
  6955. npc_timerevent_start(nd, st->rid);
  6956. return 0;
  6957. }
  6958. /*==========================================
  6959. * NPCタイマー開始
  6960. *------------------------------------------*/
  6961. BUILDIN_FUNC(startnpctimer)
  6962. {
  6963. struct npc_data *nd;
  6964. int flag = 0;
  6965. if( script_hasdata(st,3) )
  6966. { //Two arguments: NPC name and attach flag.
  6967. nd = npc_name2id(script_getstr(st, 2));
  6968. flag = script_getnum(st,3);
  6969. } else
  6970. if( script_hasdata(st,2) )
  6971. { //Check if argument is numeric (flag) or string (npc name)
  6972. struct script_data *data;
  6973. data = script_getdata(st,2);
  6974. get_val(st,data);
  6975. if( data_isstring(data) ) //NPC name
  6976. nd = npc_name2id(script_getstr(st, 2));
  6977. else if( data_isint(data) ) { //Flag
  6978. nd = (struct npc_data *)map_id2bl(st->oid);
  6979. flag = script_getnum(st,3);
  6980. } else {
  6981. ShowError("startnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6982. return 1;
  6983. }
  6984. } else
  6985. nd=(struct npc_data *)map_id2bl(st->oid);
  6986. if (!nd) return 0;
  6987. if (flag) { //Attach
  6988. TBL_PC* sd = script_rid2sd(st);
  6989. if (sd) nd->u.scr.rid = sd->bl.id;
  6990. }
  6991. npc_timerevent_start(nd, st->rid);
  6992. return 0;
  6993. }
  6994. /*==========================================
  6995. * NPCタイマー停止
  6996. *------------------------------------------*/
  6997. BUILDIN_FUNC(stopnpctimer)
  6998. {
  6999. struct npc_data *nd;
  7000. int flag = 0;
  7001. if( script_hasdata(st,3) )
  7002. { //Two arguments: NPC name and attach flag.
  7003. nd = npc_name2id(script_getstr(st, 2));
  7004. flag = script_getnum(st,3);
  7005. } else
  7006. if( script_hasdata(st,2) )
  7007. { //Check if argument is numeric (flag) or string (npc name)
  7008. struct script_data *data;
  7009. data = script_getdata(st,2);
  7010. get_val(st,data);
  7011. if( data_isstring(data) ) //NPC name
  7012. nd = npc_name2id(script_getstr(st, 2));
  7013. else if( data_isint(data) ) { //Flag
  7014. nd = (struct npc_data *)map_id2bl(st->oid);
  7015. flag = script_getnum(st,3);
  7016. } else {
  7017. ShowError("stopnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7018. return 1;
  7019. }
  7020. } else
  7021. nd=(struct npc_data *)map_id2bl(st->oid);
  7022. if (!nd) return 0;
  7023. if (flag) //Detach
  7024. nd->u.scr.rid = 0;
  7025. npc_timerevent_stop(nd);
  7026. return 0;
  7027. }
  7028. /*==========================================
  7029. * NPCタイマー情報所得
  7030. *------------------------------------------*/
  7031. BUILDIN_FUNC(getnpctimer)
  7032. {
  7033. struct npc_data *nd;
  7034. TBL_PC *sd;
  7035. int type=script_getnum(st,2);
  7036. int val=0;
  7037. if( script_hasdata(st,3) )
  7038. nd = npc_name2id(script_getstr(st,3));
  7039. else
  7040. nd = (struct npc_data *)map_id2bl(st->oid);
  7041. if (!nd || nd->bl.type != BL_NPC)
  7042. {
  7043. script_pushint(st,0);
  7044. if (battle_config.error_log)
  7045. ShowError("getnpctimer: Invalid NPC\n");
  7046. return 1;
  7047. }
  7048. switch(type){
  7049. case 0: val=npc_gettimerevent_tick(nd); break;
  7050. case 1:
  7051. if (nd->u.scr.rid) {
  7052. sd = map_id2sd(nd->u.scr.rid);
  7053. if (!sd) {
  7054. if(battle_config.error_log)
  7055. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7056. break;
  7057. }
  7058. val = (sd->npc_timer_id != -1);
  7059. } else
  7060. val= (nd->u.scr.timerid !=-1);
  7061. break;
  7062. case 2: val= nd->u.scr.timeramount; break;
  7063. }
  7064. script_pushint(st,val);
  7065. return 0;
  7066. }
  7067. /*==========================================
  7068. * NPCタイマー値設定
  7069. *------------------------------------------*/
  7070. BUILDIN_FUNC(setnpctimer)
  7071. {
  7072. int tick;
  7073. struct npc_data *nd;
  7074. tick=script_getnum(st,2);
  7075. if( script_hasdata(st,3) )
  7076. nd=npc_name2id(script_getstr(st,3));
  7077. else
  7078. nd=(struct npc_data *)map_id2bl(st->oid);
  7079. npc_settimerevent_tick(nd,tick);
  7080. return 0;
  7081. }
  7082. /*==========================================
  7083. * attaches the player rid to the timer [Celest]
  7084. *------------------------------------------*/
  7085. BUILDIN_FUNC(attachnpctimer)
  7086. {
  7087. TBL_PC *sd;
  7088. struct npc_data *nd;
  7089. nd=(struct npc_data *)map_id2bl(st->oid);
  7090. if( script_hasdata(st,2) )
  7091. sd=map_nick2sd(script_getstr(st,2));
  7092. else
  7093. sd = script_rid2sd(st);
  7094. if (sd==NULL)
  7095. return 0;
  7096. nd->u.scr.rid = sd->bl.id;
  7097. return 0;
  7098. }
  7099. /*==========================================
  7100. * detaches a player rid from the timer [Celest]
  7101. *------------------------------------------*/
  7102. BUILDIN_FUNC(detachnpctimer)
  7103. {
  7104. struct npc_data *nd;
  7105. if( script_hasdata(st,2) )
  7106. nd=npc_name2id(script_getstr(st,2));
  7107. else
  7108. nd=(struct npc_data *)map_id2bl(st->oid);
  7109. nd->u.scr.rid = 0;
  7110. return 0;
  7111. }
  7112. /*==========================================
  7113. * To avoid "player not attached" script errors, this function is provided,
  7114. * it checks if there is a player attached to the current script. [Skotlex]
  7115. * If no, returns 0, if yes, returns the account_id of the attached player.
  7116. *------------------------------------------*/
  7117. BUILDIN_FUNC(playerattached)
  7118. {
  7119. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7120. script_pushint(st,0);
  7121. else
  7122. script_pushint(st,st->rid);
  7123. return 0;
  7124. }
  7125. /*==========================================
  7126. * 天の声アナウンス
  7127. *------------------------------------------*/
  7128. BUILDIN_FUNC(announce)
  7129. {
  7130. const char *str, *color=NULL;
  7131. int flag;
  7132. str=script_getstr(st,2);
  7133. flag=script_getnum(st,3);
  7134. if (script_hasdata(st,4))
  7135. color=script_getstr(st,4);
  7136. if(flag&0x0f){
  7137. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  7138. (struct block_list *)script_rid2sd(st);
  7139. if (color)
  7140. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  7141. else
  7142. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  7143. }else {
  7144. if (color)
  7145. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  7146. else
  7147. intif_GMmessage(str,(int)strlen(str)+1,flag);
  7148. }
  7149. return 0;
  7150. }
  7151. /*==========================================
  7152. * 天の声アナウンス(特定マップ)
  7153. *------------------------------------------*/
  7154. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  7155. {
  7156. char *str, *color;
  7157. int len,flag;
  7158. str=va_arg(ap,char *);
  7159. len=va_arg(ap,int);
  7160. flag=va_arg(ap,int);
  7161. color=va_arg(ap,char *);
  7162. if (color)
  7163. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7164. else
  7165. clif_GMmessage(bl,str,len,flag|3);
  7166. return 0;
  7167. }
  7168. BUILDIN_FUNC(mapannounce)
  7169. {
  7170. const char *mapname,*str, *color=NULL;
  7171. int flag,m;
  7172. mapname=script_getstr(st,2);
  7173. str=script_getstr(st,3);
  7174. flag=script_getnum(st,4);
  7175. if (script_hasdata(st,5))
  7176. color=script_getstr(st,5);
  7177. if( (m=map_mapname2mapid(mapname))<0 )
  7178. return 0;
  7179. map_foreachinmap(buildin_mapannounce_sub,
  7180. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7181. return 0;
  7182. }
  7183. /*==========================================
  7184. * 天の声アナウンス(特定エリア)
  7185. *------------------------------------------*/
  7186. BUILDIN_FUNC(areaannounce)
  7187. {
  7188. const char *map,*str,*color=NULL;
  7189. int flag,m;
  7190. int x0,y0,x1,y1;
  7191. map=script_getstr(st,2);
  7192. x0=script_getnum(st,3);
  7193. y0=script_getnum(st,4);
  7194. x1=script_getnum(st,5);
  7195. y1=script_getnum(st,6);
  7196. str=script_getstr(st,7);
  7197. flag=script_getnum(st,8);
  7198. if (script_hasdata(st,9))
  7199. color=script_getstr(st,9);
  7200. if( (m=map_mapname2mapid(map))<0 )
  7201. return 0;
  7202. map_foreachinarea(buildin_mapannounce_sub,
  7203. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7204. return 0;
  7205. }
  7206. /*==========================================
  7207. * ユーザー数所得
  7208. *------------------------------------------*/
  7209. BUILDIN_FUNC(getusers)
  7210. {
  7211. int flag=script_getnum(st,2);
  7212. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7213. int val=0;
  7214. switch(flag&0x07){
  7215. case 0: val=map[bl->m].users; break;
  7216. case 1: val=map_getusers(); break;
  7217. }
  7218. script_pushint(st,val);
  7219. return 0;
  7220. }
  7221. /*==========================================
  7222. * Works like @WHO - displays all online users names in window
  7223. *------------------------------------------*/
  7224. BUILDIN_FUNC(getusersname)
  7225. {
  7226. TBL_PC *sd, *pl_sd = NULL, **pl_allsd;
  7227. int i=0,disp_num=1, users;
  7228. sd = script_rid2sd(st);
  7229. if (!sd) return 0;
  7230. pl_allsd = map_getallusers(&users);
  7231. for (i=0;i<users;i++)
  7232. {
  7233. pl_sd = pl_allsd[i];
  7234. if( !(battle_config.hide_GM_session && pc_isGM(pl_sd)) )
  7235. {
  7236. if((disp_num++)%10==0)
  7237. clif_scriptnext(sd,st->oid);
  7238. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7239. }
  7240. }
  7241. return 0;
  7242. }
  7243. /*==========================================
  7244. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7245. *------------------------------------------*/
  7246. BUILDIN_FUNC(getmapguildusers)
  7247. {
  7248. const char *str;
  7249. int m, gid;
  7250. int i=0,c=0;
  7251. struct guild *g = NULL;
  7252. str=script_getstr(st,2);
  7253. gid=script_getnum(st,3);
  7254. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7255. script_pushint(st,-1);
  7256. return 0;
  7257. }
  7258. g = guild_search(gid);
  7259. if (g){
  7260. for(i = 0; i < g->max_member; i++)
  7261. {
  7262. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7263. c++;
  7264. }
  7265. }
  7266. script_pushint(st,c);
  7267. return 0;
  7268. }
  7269. /*==========================================
  7270. * マップ指定ユーザー数所得
  7271. *------------------------------------------*/
  7272. BUILDIN_FUNC(getmapusers)
  7273. {
  7274. const char *str;
  7275. int m;
  7276. str=script_getstr(st,2);
  7277. if( (m=map_mapname2mapid(str))< 0){
  7278. script_pushint(st,-1);
  7279. return 0;
  7280. }
  7281. script_pushint(st,map[m].users);
  7282. return 0;
  7283. }
  7284. /*==========================================
  7285. * エリア指定ユーザー数所得
  7286. *------------------------------------------*/
  7287. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7288. {
  7289. int *users=va_arg(ap,int *);
  7290. (*users)++;
  7291. return 0;
  7292. }
  7293. BUILDIN_FUNC(getareausers)
  7294. {
  7295. const char *str;
  7296. int m,x0,y0,x1,y1,users=0;
  7297. str=script_getstr(st,2);
  7298. x0=script_getnum(st,3);
  7299. y0=script_getnum(st,4);
  7300. x1=script_getnum(st,5);
  7301. y1=script_getnum(st,6);
  7302. if( (m=map_mapname2mapid(str))< 0){
  7303. script_pushint(st,-1);
  7304. return 0;
  7305. }
  7306. map_foreachinarea(buildin_getareausers_sub,
  7307. m,x0,y0,x1,y1,BL_PC,&users);
  7308. script_pushint(st,users);
  7309. return 0;
  7310. }
  7311. /*==========================================
  7312. * エリア指定ドロップアイテム数所得
  7313. *------------------------------------------*/
  7314. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7315. {
  7316. int item=va_arg(ap,int);
  7317. int *amount=va_arg(ap,int *);
  7318. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7319. if(drop->item_data.nameid==item)
  7320. (*amount)+=drop->item_data.amount;
  7321. return 0;
  7322. }
  7323. BUILDIN_FUNC(getareadropitem)
  7324. {
  7325. const char *str;
  7326. int m,x0,y0,x1,y1,item,amount=0;
  7327. struct script_data *data;
  7328. str=script_getstr(st,2);
  7329. x0=script_getnum(st,3);
  7330. y0=script_getnum(st,4);
  7331. x1=script_getnum(st,5);
  7332. y1=script_getnum(st,6);
  7333. data=script_getdata(st,7);
  7334. get_val(st,data);
  7335. if( data_isstring(data) ){
  7336. const char *name=conv_str(st,data);
  7337. struct item_data *item_data = itemdb_searchname(name);
  7338. item=UNKNOWN_ITEM_ID;
  7339. if( item_data )
  7340. item=item_data->nameid;
  7341. }else
  7342. item=conv_num(st,data);
  7343. if( (m=map_mapname2mapid(str))< 0){
  7344. script_pushint(st,-1);
  7345. return 0;
  7346. }
  7347. map_foreachinarea(buildin_getareadropitem_sub,
  7348. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7349. script_pushint(st,amount);
  7350. return 0;
  7351. }
  7352. /*==========================================
  7353. * NPCの有効化
  7354. *------------------------------------------*/
  7355. BUILDIN_FUNC(enablenpc)
  7356. {
  7357. const char *str;
  7358. str=script_getstr(st,2);
  7359. npc_enable(str,1);
  7360. return 0;
  7361. }
  7362. /*==========================================
  7363. * NPCの無効化
  7364. *------------------------------------------*/
  7365. BUILDIN_FUNC(disablenpc)
  7366. {
  7367. const char *str;
  7368. str=script_getstr(st,2);
  7369. npc_enable(str,0);
  7370. return 0;
  7371. }
  7372. /*==========================================
  7373. * 隠れているNPCの表示
  7374. *------------------------------------------*/
  7375. BUILDIN_FUNC(hideoffnpc)
  7376. {
  7377. const char *str;
  7378. str=script_getstr(st,2);
  7379. npc_enable(str,2);
  7380. return 0;
  7381. }
  7382. /*==========================================
  7383. * NPCをハイディング
  7384. *------------------------------------------*/
  7385. BUILDIN_FUNC(hideonnpc)
  7386. {
  7387. const char *str;
  7388. str=script_getstr(st,2);
  7389. npc_enable(str,4);
  7390. return 0;
  7391. }
  7392. /*==========================================
  7393. * 状態異常にかかる
  7394. *------------------------------------------*/
  7395. BUILDIN_FUNC(sc_start)
  7396. {
  7397. struct block_list *bl;
  7398. int type,tick,val1,val4=0;
  7399. type=script_getnum(st,2);
  7400. tick=script_getnum(st,3);
  7401. val1=script_getnum(st,4);
  7402. if( script_hasdata(st,5) ) //指定したキャラを状態異常にする
  7403. bl = map_id2bl(script_getnum(st,5));
  7404. else
  7405. bl = map_id2bl(st->rid);
  7406. if (potion_flag==1 && potion_target) {
  7407. bl = map_id2bl(potion_target);
  7408. tick/=2; //Thrown potions only last half.
  7409. val4 = 1; //Mark that this was a thrown sc_effect
  7410. }
  7411. if (type >= 0 && type < SC_MAX && val1 && !tick)
  7412. { //When there isn't a duration specified, try to get it from the skill_db
  7413. tick = StatusSkillChangeTable[type];
  7414. if (tick)
  7415. tick = skill_get_time(tick,val1);
  7416. else //Failed to retrieve duration, reset to what it was.
  7417. tick = 0;
  7418. }
  7419. if (bl)
  7420. status_change_start(bl,type,10000,val1,0,0,val4,tick,11);
  7421. return 0;
  7422. }
  7423. /*==========================================
  7424. * 状態異常にかかる(確率指定)
  7425. *------------------------------------------*/
  7426. BUILDIN_FUNC(sc_start2)
  7427. {
  7428. struct block_list *bl;
  7429. int type,tick,val1,val4=0,per;
  7430. type=script_getnum(st,2);
  7431. tick=script_getnum(st,3);
  7432. val1=script_getnum(st,4);
  7433. per=script_getnum(st,5);
  7434. if( script_hasdata(st,6) ) //指定したキャラを状態異常にする
  7435. bl = map_id2bl(script_getnum(st,6));
  7436. else
  7437. bl = map_id2bl(st->rid);
  7438. if (type >= 0 && type < SC_MAX && val1 && !tick)
  7439. { //When there isn't a duration specified, try to get it from the skill_db
  7440. tick = StatusSkillChangeTable[type];
  7441. if (tick)
  7442. tick = skill_get_time(tick,val1);
  7443. else //Failed to retrieve duration, reset to what it was.
  7444. tick = 0;
  7445. }
  7446. if (potion_flag==1 && potion_target) {
  7447. bl = map_id2bl(potion_target);
  7448. tick/=2;
  7449. val4 = 1;
  7450. }
  7451. if(bl)
  7452. status_change_start(bl,type,per,val1,0,0,val4,tick,11);
  7453. return 0;
  7454. }
  7455. /*==========================================
  7456. * Starts a SC_ change with the four values passed. [Skotlex]
  7457. * Final optional argument is the ID of player to affect.
  7458. * sc_start4 type, duration, val1, val2, val3, val4, <id>;
  7459. *------------------------------------------*/
  7460. BUILDIN_FUNC(sc_start4)
  7461. {
  7462. struct block_list *bl;
  7463. int type,tick,val1,val2,val3,val4;
  7464. type=script_getnum(st,2);
  7465. tick=script_getnum(st,3);
  7466. val1=script_getnum(st,4);
  7467. val2=script_getnum(st,5);
  7468. val3=script_getnum(st,6);
  7469. val4=script_getnum(st,7);
  7470. if( script_hasdata(st,8) )
  7471. bl = map_id2bl(script_getnum(st,8));
  7472. else
  7473. bl = map_id2bl(st->rid);
  7474. if (type >= 0 && type < SC_MAX && val1 && !tick)
  7475. { //When there isn't a duration specified, try to get it from the skill_db
  7476. tick = StatusSkillChangeTable[type];
  7477. if (tick)
  7478. tick = skill_get_time(tick,val1);
  7479. else //Failed to retrieve duration, reset to what it was.
  7480. tick = 0;
  7481. }
  7482. if (potion_flag==1 && potion_target) {
  7483. bl = map_id2bl(potion_target);
  7484. tick/=2;
  7485. }
  7486. if (bl)
  7487. status_change_start(bl,type,10000,val1,val2,val3,val4,tick,11);
  7488. return 0;
  7489. }
  7490. /*==========================================
  7491. * 状態異常が直る
  7492. *------------------------------------------*/
  7493. BUILDIN_FUNC(sc_end)
  7494. {
  7495. struct block_list *bl;
  7496. int type;
  7497. type=script_getnum(st,2);
  7498. bl = map_id2bl(st->rid);
  7499. if (potion_flag==1 && potion_target)
  7500. bl = map_id2bl(potion_target);
  7501. if (!bl) return 0;
  7502. if (type >= 0)
  7503. status_change_end(bl,type,-1);
  7504. else
  7505. status_change_clear(bl, 2);
  7506. return 0;
  7507. }
  7508. /*==========================================
  7509. * 状態異常耐性を計算した確率を返す
  7510. *------------------------------------------*/
  7511. BUILDIN_FUNC(getscrate)
  7512. {
  7513. struct block_list *bl;
  7514. int type,rate;
  7515. type=script_getnum(st,2);
  7516. rate=script_getnum(st,3);
  7517. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7518. bl = map_id2bl(script_getnum(st,4));
  7519. else
  7520. bl = map_id2bl(st->rid);
  7521. if (bl)
  7522. rate = status_get_sc_def(bl,type, 10000, 10000, 0);
  7523. script_pushint(st,rate);
  7524. return 0;
  7525. }
  7526. /*==========================================
  7527. *
  7528. *------------------------------------------*/
  7529. BUILDIN_FUNC(debugmes)
  7530. {
  7531. const char *str;
  7532. str=script_getstr(st,2);
  7533. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7534. return 0;
  7535. }
  7536. /*==========================================
  7537. *捕獲アイテム使用
  7538. *------------------------------------------*/
  7539. BUILDIN_FUNC(catchpet)
  7540. {
  7541. int pet_id;
  7542. TBL_PC *sd;
  7543. pet_id= script_getnum(st,2);
  7544. sd=script_rid2sd(st);
  7545. pet_catch_process1(sd,pet_id);
  7546. return 0;
  7547. }
  7548. /*==========================================
  7549. * [orn]
  7550. *------------------------------------------*/
  7551. BUILDIN_FUNC(homunculus_evolution)
  7552. {
  7553. TBL_PC *sd;
  7554. sd=script_rid2sd(st);
  7555. if ( sd->hd && sd->hd->homunculusDB->evo_class && sd->hd->homunculus.intimacy > 91000 ) {
  7556. return !merc_hom_evolution(sd->hd) ;
  7557. }
  7558. clif_emotion(&sd->hd->bl, 4) ; //swt
  7559. return 0;
  7560. }
  7561. //These two functions bring the eA MAPID_* class functionality to scripts.
  7562. BUILDIN_FUNC(eaclass)
  7563. {
  7564. int class_;
  7565. if( script_hasdata(st,2) )
  7566. class_ = script_getnum(st,2);
  7567. else {
  7568. TBL_PC *sd;
  7569. sd=script_rid2sd(st);
  7570. if (!sd) {
  7571. script_pushint(st,-1);
  7572. return 0;
  7573. }
  7574. class_ = sd->status.class_;
  7575. }
  7576. script_pushint(st,pc_jobid2mapid(class_));
  7577. return 0;
  7578. }
  7579. BUILDIN_FUNC(roclass)
  7580. {
  7581. int class_ =script_getnum(st,2);
  7582. int sex;
  7583. if( script_hasdata(st,3) )
  7584. sex = script_getnum(st,3);
  7585. else {
  7586. TBL_PC *sd;
  7587. if (st->rid && (sd=script_rid2sd(st)))
  7588. sex = sd->status.sex;
  7589. else
  7590. sex = 1; //Just use male when not found.
  7591. }
  7592. script_pushint(st,pc_mapid2jobid(class_, sex));
  7593. return 0;
  7594. }
  7595. /*==========================================
  7596. *携帯卵孵化機使用
  7597. *------------------------------------------*/
  7598. BUILDIN_FUNC(birthpet)
  7599. {
  7600. TBL_PC *sd;
  7601. sd=script_rid2sd(st);
  7602. clif_sendegg(sd);
  7603. return 0;
  7604. }
  7605. /*==========================================
  7606. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7607. *------------------------------------------*/
  7608. BUILDIN_FUNC(resetlvl)
  7609. {
  7610. TBL_PC *sd;
  7611. int type=script_getnum(st,2);
  7612. sd=script_rid2sd(st);
  7613. pc_resetlvl(sd,type);
  7614. return 0;
  7615. }
  7616. /*==========================================
  7617. * ステータスリセット
  7618. *------------------------------------------*/
  7619. BUILDIN_FUNC(resetstatus)
  7620. {
  7621. TBL_PC *sd;
  7622. sd=script_rid2sd(st);
  7623. pc_resetstate(sd);
  7624. return 0;
  7625. }
  7626. /*==========================================
  7627. * script command resetskill
  7628. *------------------------------------------*/
  7629. BUILDIN_FUNC(resetskill)
  7630. {
  7631. TBL_PC *sd;
  7632. sd=script_rid2sd(st);
  7633. pc_resetskill(sd,1);
  7634. return 0;
  7635. }
  7636. /*==========================================
  7637. * Counts total amount of skill points.
  7638. *------------------------------------------*/
  7639. BUILDIN_FUNC(skillpointcount)
  7640. {
  7641. TBL_PC *sd;
  7642. sd=script_rid2sd(st);
  7643. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7644. return 0;
  7645. }
  7646. /*==========================================
  7647. *
  7648. *------------------------------------------*/
  7649. BUILDIN_FUNC(changebase)
  7650. {
  7651. TBL_PC *sd=NULL;
  7652. int vclass;
  7653. if( script_hasdata(st,3) )
  7654. sd=map_id2sd(script_getnum(st,3));
  7655. else
  7656. sd=script_rid2sd(st);
  7657. if(sd == NULL)
  7658. return 0;
  7659. vclass = script_getnum(st,2);
  7660. if(vclass == JOB_WEDDING)
  7661. {
  7662. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7663. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7664. )
  7665. return 0;
  7666. }
  7667. if(!sd->disguise && vclass != sd->vd.class_) {
  7668. status_set_viewdata(&sd->bl, vclass);
  7669. //Updated client view. Base, Weapon and Cloth Colors.
  7670. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7671. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7672. if (sd->vd.cloth_color)
  7673. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7674. }
  7675. return 0;
  7676. }
  7677. /*==========================================
  7678. * 性別変換
  7679. *------------------------------------------*/
  7680. BUILDIN_FUNC(changesex)
  7681. {
  7682. TBL_PC *sd = NULL;
  7683. sd = script_rid2sd(st);
  7684. if (sd->status.sex == 0) {
  7685. sd->status.sex = 1;
  7686. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7687. sd->status.class_ -= 1;
  7688. } else if (sd->status.sex == 1) {
  7689. sd->status.sex = 0;
  7690. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7691. sd->status.class_ += 1;
  7692. }
  7693. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  7694. chrif_save(sd,0);
  7695. return 0;
  7696. }
  7697. /*==========================================
  7698. * Works like 'announce' but outputs in the common chat window
  7699. *------------------------------------------*/
  7700. BUILDIN_FUNC(globalmes)
  7701. {
  7702. struct block_list *bl = map_id2bl(st->oid);
  7703. struct npc_data *nd = (struct npc_data *)bl;
  7704. const char *name=NULL,*mes;
  7705. mes=script_getstr(st,2); // メッセージの取得
  7706. if(mes==NULL) return 0;
  7707. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7708. name=script_getstr(st,3);
  7709. } else {
  7710. name=nd->name;
  7711. }
  7712. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7713. return 0;
  7714. }
  7715. /////////////////////////////////////////////////////////////////////
  7716. // NPC waiting room (chat room)
  7717. //
  7718. /// Creates a waiting room (chat room) for this npc.
  7719. ///
  7720. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7721. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7722. /// waitingroom "<title>",<limit>,"<event>";
  7723. /// waitingroom "<title>",<limit>;
  7724. BUILDIN_FUNC(waitingroom)
  7725. {
  7726. struct npc_data* nd;
  7727. const char* title;
  7728. const char* ev = "";
  7729. int limit;
  7730. int trigger = 0;
  7731. int pub = 1;
  7732. title = script_getstr(st, 2);
  7733. limit = script_getnum(st, 3);
  7734. if( limit == 0 )
  7735. pub = 3;
  7736. if( script_hasdata(st,5) )
  7737. {
  7738. struct script_data* last = script_getdata(st, 5);
  7739. get_val(st, last);
  7740. if( data_isstring(last) )
  7741. {// ,<trigger>,"<event>"
  7742. trigger = script_getnum(st, 4);
  7743. ev = script_getstr(st, 5);
  7744. }
  7745. else
  7746. {// ,"<event>",<trigger>
  7747. ev = script_getstr(st, 4);
  7748. trigger=script_getnum(st,5);
  7749. }
  7750. }
  7751. else if( script_hasdata(st,4) )
  7752. {// ,"<event>"
  7753. ev = script_getstr(st, 4);
  7754. }
  7755. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  7756. chat_createnpcchat(nd, limit, pub, trigger, title, (int)strlen(title), ev);
  7757. return 0;
  7758. }
  7759. /// Removes the waiting room of the current or target npc.
  7760. ///
  7761. /// delwaitingroom "<npc_name>";
  7762. /// delwaitingroom;
  7763. BUILDIN_FUNC(delwaitingroom)
  7764. {
  7765. struct npc_data* nd;
  7766. if( script_hasdata(st,2) )
  7767. nd = npc_name2id(script_getstr(st, 2));
  7768. else
  7769. nd = (struct npc_data *)map_id2bl(st->oid);
  7770. if( nd != NULL )
  7771. chat_deletenpcchat(nd);
  7772. return 0;
  7773. }
  7774. /// Kicks all the players from the waiting room of the current or target npc.
  7775. ///
  7776. /// kickwaitingroomall "<npc_name>";
  7777. /// kickwaitingroomall;
  7778. BUILDIN_FUNC(waitingroomkickall)
  7779. {
  7780. struct npc_data* nd;
  7781. struct chat_data* cd;
  7782. if( script_hasdata(st,2) )
  7783. nd = npc_name2id(script_getstr(st,2));
  7784. else
  7785. nd = (struct npc_data *)map_id2bl(st->oid);
  7786. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7787. chat_npckickall(cd);
  7788. return 0;
  7789. }
  7790. /// Enables the waiting room event of the current or target npc.
  7791. ///
  7792. /// enablewaitingroomevent "<npc_name>";
  7793. /// enablewaitingroomevent;
  7794. BUILDIN_FUNC(enablewaitingroomevent)
  7795. {
  7796. struct npc_data* nd;
  7797. struct chat_data* cd;
  7798. if( script_hasdata(st,2) )
  7799. nd = npc_name2id(script_getstr(st, 2));
  7800. else
  7801. nd = (struct npc_data *)map_id2bl(st->oid);
  7802. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7803. chat_enableevent(cd);
  7804. return 0;
  7805. }
  7806. /// Disables the waiting room event of the current or target npc.
  7807. ///
  7808. /// disablewaitingroomevent "<npc_name>";
  7809. /// disablewaitingroomevent;
  7810. BUILDIN_FUNC(disablewaitingroomevent)
  7811. {
  7812. struct npc_data *nd;
  7813. struct chat_data *cd;
  7814. if( script_hasdata(st,2) )
  7815. nd = npc_name2id(script_getstr(st, 2));
  7816. else
  7817. nd = (struct npc_data *)map_id2bl(st->oid);
  7818. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7819. chat_disableevent(cd);
  7820. return 0;
  7821. }
  7822. /// Returns info on the waiting room of the current or target npc.
  7823. /// Returns -1 if the type unknown
  7824. /// <type>=0 : current number of users
  7825. /// <type>=1 : maximum number of users allowed
  7826. /// <type>=2 : the number of users that trigger the event
  7827. /// <type>=3 : if the trigger is disabled
  7828. /// <type>=4 : the title of the waiting room
  7829. /// <type>=5 : the password of the waiting room
  7830. /// <type>=16 : the name of the waiting room event
  7831. /// <type>=32 : if the waiting room is full
  7832. /// <type>=33 : if there are enough users to trigger the event
  7833. ///
  7834. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7835. /// getwaitingroomstate(<type>) -> <info>
  7836. BUILDIN_FUNC(getwaitingroomstate)
  7837. {
  7838. struct npc_data *nd;
  7839. struct chat_data *cd;
  7840. int type;
  7841. type = script_getnum(st,2);
  7842. if( script_hasdata(st,3) )
  7843. nd = npc_name2id(script_getstr(st, 3));
  7844. else
  7845. nd = (struct npc_data *)map_id2bl(st->oid);
  7846. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7847. {
  7848. script_pushint(st, -1);
  7849. return 0;
  7850. }
  7851. switch(type)
  7852. {
  7853. case 0: script_pushint(st, cd->users); break;
  7854. case 1: script_pushint(st, cd->limit); break;
  7855. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7856. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7857. case 4: script_pushconststr(st, cd->title); break;
  7858. case 5: script_pushconststr(st, cd->pass); break;
  7859. case 16: script_pushconststr(st, cd->npc_event);break;
  7860. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7861. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7862. default: script_pushint(st, -1); break;
  7863. }
  7864. return 0;
  7865. }
  7866. /// Warps the trigger or target amount of players to the target map and position.
  7867. /// Players are automatically removed from the waiting room.
  7868. /// Those waiting the longest will get warped first.
  7869. /// The target map can be "Random" for a random position in the current map,
  7870. /// and "SavePoint" for the savepoint map+position.
  7871. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7872. ///
  7873. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7874. /// The total number of teleported players is put into $@warpwaitingpcnum
  7875. ///
  7876. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7877. /// warpwaitingpc "<map name>",<x>,<y>;
  7878. BUILDIN_FUNC(warpwaitingpc)
  7879. {
  7880. int x;
  7881. int y;
  7882. int i;
  7883. int n;
  7884. const char* map_name;
  7885. struct npc_data* nd;
  7886. struct chat_data* cd;
  7887. TBL_PC* sd;
  7888. nd = (struct npc_data *)map_id2bl(st->oid);
  7889. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7890. return 0;
  7891. map_name = script_getstr(st,2);
  7892. x = script_getnum(st,3);
  7893. y = script_getnum(st,4);
  7894. n = cd->trigger&0x7f;
  7895. if( script_hasdata(st,5) )
  7896. n = script_getnum(st,5);
  7897. for( i = 0; i < n && cd->users > 0; i++ )
  7898. {
  7899. sd = cd->usersd[0];
  7900. if( sd == NULL )
  7901. {
  7902. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7903. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7904. continue;// Broken npc chat room?
  7905. }
  7906. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7907. if( strcmp(map_name,"Random") == 0 )
  7908. pc_randomwarp(sd,3);
  7909. else if( strcmp(map_name,"SavePoint") == 0 )
  7910. {
  7911. if( map[sd->bl.m].flag.noteleport )
  7912. return 0;// can't teleport on this map
  7913. pc_setpos(sd,sd->status.save_point.map,
  7914. sd->status.save_point.x, sd->status.save_point.y, 3);
  7915. }
  7916. else
  7917. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7918. }
  7919. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7920. return 0;
  7921. }
  7922. /////////////////////////////////////////////////////////////////////
  7923. // ...
  7924. //
  7925. /// TODO what is this suposed to do?
  7926. ///
  7927. /// @author RoVeRT
  7928. BUILDIN_FUNC(enablearena)
  7929. {
  7930. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  7931. struct chat_data *cd;
  7932. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL)
  7933. return 0;
  7934. npc_enable(nd->name, 1);
  7935. nd->arenaflag = 1;
  7936. if( cd->users >= cd->trigger && cd->npc_event[0] )
  7937. npc_timer_event(cd->npc_event);
  7938. return 0;
  7939. }
  7940. /// TODO what is this suposed to do?
  7941. ///
  7942. /// @author RoVeRT
  7943. BUILDIN_FUNC(disablearena)
  7944. {
  7945. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  7946. nd->arenaflag=0;
  7947. return 0;
  7948. }
  7949. /////////////////////////////////////////////////////////////////////
  7950. // ...
  7951. //
  7952. /*==========================================
  7953. * RIDのアタッチ
  7954. *------------------------------------------*/
  7955. BUILDIN_FUNC(attachrid)
  7956. {
  7957. st->rid=script_getnum(st,2);
  7958. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7959. return 0;
  7960. }
  7961. /*==========================================
  7962. * RIDのデタッチ
  7963. *------------------------------------------*/
  7964. BUILDIN_FUNC(detachrid)
  7965. {
  7966. st->rid=0;
  7967. return 0;
  7968. }
  7969. /*==========================================
  7970. * 存在チェック
  7971. *------------------------------------------*/
  7972. BUILDIN_FUNC(isloggedin)
  7973. {
  7974. push_val(st->stack,C_INT, map_id2sd(
  7975. script_getnum(st,2) )!=NULL );
  7976. return 0;
  7977. }
  7978. /*==========================================
  7979. *
  7980. *------------------------------------------*/
  7981. BUILDIN_FUNC(setmapflagnosave)
  7982. {
  7983. int m,x,y;
  7984. unsigned short mapindex;
  7985. const char *str,*str2;
  7986. str=script_getstr(st,2);
  7987. str2=script_getstr(st,3);
  7988. x=script_getnum(st,4);
  7989. y=script_getnum(st,5);
  7990. m = map_mapname2mapid(str);
  7991. mapindex = mapindex_name2id(str2);
  7992. if(m >= 0 && mapindex) {
  7993. map[m].flag.nosave=1;
  7994. map[m].save.map=mapindex;
  7995. map[m].save.x=x;
  7996. map[m].save.y=y;
  7997. }
  7998. return 0;
  7999. }
  8000. BUILDIN_FUNC(setmapflag)
  8001. {
  8002. int m,i;
  8003. const char *str;
  8004. const char *val=NULL;
  8005. str=script_getstr(st,2);
  8006. i=script_getnum(st,3);
  8007. if(script_hasdata(st,4)){
  8008. val=script_getstr(st,4);
  8009. }
  8010. m = map_mapname2mapid(str);
  8011. if(m >= 0) {
  8012. switch(i) {
  8013. case MF_NOMEMO:
  8014. map[m].flag.nomemo=1;
  8015. break;
  8016. case MF_NOTELEPORT:
  8017. map[m].flag.noteleport=1;
  8018. break;
  8019. case MF_NOBRANCH:
  8020. map[m].flag.nobranch=1;
  8021. break;
  8022. case MF_NOPENALTY:
  8023. map[m].flag.noexppenalty=1;
  8024. map[m].flag.nozenypenalty=1;
  8025. break;
  8026. case MF_NOZENYPENALTY:
  8027. map[m].flag.nozenypenalty=1;
  8028. break;
  8029. case MF_PVP:
  8030. map[m].flag.pvp=1;
  8031. break;
  8032. case MF_PVP_NOPARTY:
  8033. map[m].flag.pvp_noparty=1;
  8034. break;
  8035. case MF_PVP_NOGUILD:
  8036. map[m].flag.pvp_noguild=1;
  8037. break;
  8038. case MF_GVG:
  8039. map[m].flag.gvg=1;
  8040. break;
  8041. case MF_GVG_NOPARTY:
  8042. map[m].flag.gvg_noparty=1;
  8043. break;
  8044. case MF_GVG_DUNGEON:
  8045. map[m].flag.gvg_dungeon=1;
  8046. break;
  8047. case MF_GVG_CASTLE:
  8048. map[m].flag.gvg_castle=1;
  8049. break;
  8050. case MF_NOTRADE:
  8051. map[m].flag.notrade=1;
  8052. break;
  8053. case MF_NODROP:
  8054. map[m].flag.nodrop=1;
  8055. break;
  8056. case MF_NOSKILL:
  8057. map[m].flag.noskill=1;
  8058. break;
  8059. case MF_NOWARP:
  8060. map[m].flag.nowarp=1;
  8061. break;
  8062. case MF_NOICEWALL: // [Valaris]
  8063. map[m].flag.noicewall=1;
  8064. break;
  8065. case MF_SNOW: // [Valaris]
  8066. map[m].flag.snow=1;
  8067. break;
  8068. case MF_CLOUDS:
  8069. map[m].flag.clouds=1;
  8070. break;
  8071. case MF_CLOUDS2: // [Valaris]
  8072. map[m].flag.clouds2=1;
  8073. break;
  8074. case MF_FOG: // [Valaris]
  8075. map[m].flag.fog=1;
  8076. break;
  8077. case MF_FIREWORKS:
  8078. map[m].flag.fireworks=1;
  8079. break;
  8080. case MF_SAKURA: // [Valaris]
  8081. map[m].flag.sakura=1;
  8082. break;
  8083. case MF_LEAVES: // [Valaris]
  8084. map[m].flag.leaves=1;
  8085. break;
  8086. case MF_RAIN: // [Valaris]
  8087. map[m].flag.rain=1;
  8088. break;
  8089. case MF_INDOORS: // celest
  8090. map[m].flag.indoors=1;
  8091. break;
  8092. case MF_NIGHTENABLED:
  8093. map[m].flag.nightenabled=1;
  8094. break;
  8095. case MF_NOGO: // celest
  8096. map[m].flag.nogo=1;
  8097. break;
  8098. case MF_NOBASEEXP:
  8099. map[m].flag.nobaseexp=1;
  8100. break;
  8101. case MF_NOJOBEXP:
  8102. map[m].flag.nojobexp=1;
  8103. break;
  8104. case MF_NOMOBLOOT:
  8105. map[m].flag.nomobloot=1;
  8106. break;
  8107. case MF_NOMVPLOOT:
  8108. map[m].flag.nomvploot=1;
  8109. break;
  8110. case MF_NORETURN:
  8111. map[m].flag.noreturn=1;
  8112. break;
  8113. case MF_NOWARPTO:
  8114. map[m].flag.nowarpto=1;
  8115. break;
  8116. case MF_NIGHTMAREDROP:
  8117. map[m].flag.pvp_nightmaredrop=1;
  8118. break;
  8119. case MF_RESTRICTED:
  8120. map[m].flag.restricted=1;
  8121. break;
  8122. case MF_NOCOMMAND:
  8123. map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val);
  8124. break;
  8125. case MF_JEXP:
  8126. map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val);
  8127. break;
  8128. case MF_BEXP:
  8129. map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val);
  8130. break;
  8131. case MF_NOVENDING:
  8132. map[m].flag.novending=1;
  8133. break;
  8134. case MF_LOADEVENT:
  8135. map[m].flag.loadevent=1;
  8136. break;
  8137. case MF_NOCHAT:
  8138. map[m].flag.nochat=1;
  8139. break;
  8140. case MF_PARTYLOCK:
  8141. map[m].flag.partylock=1;
  8142. break;
  8143. case MF_GUILDLOCK:
  8144. map[m].flag.guildlock=1;
  8145. break;
  8146. }
  8147. }
  8148. return 0;
  8149. }
  8150. BUILDIN_FUNC(removemapflag)
  8151. {
  8152. int m,i;
  8153. const char *str;
  8154. str=script_getstr(st,2);
  8155. i=script_getnum(st,3);
  8156. m = map_mapname2mapid(str);
  8157. if(m >= 0) {
  8158. switch(i) {
  8159. case MF_NOMEMO:
  8160. map[m].flag.nomemo=0;
  8161. break;
  8162. case MF_NOTELEPORT:
  8163. map[m].flag.noteleport=0;
  8164. break;
  8165. case MF_NOSAVE:
  8166. map[m].flag.nosave=0;
  8167. break;
  8168. case MF_NOBRANCH:
  8169. map[m].flag.nobranch=0;
  8170. break;
  8171. case MF_NOPENALTY:
  8172. map[m].flag.noexppenalty=0;
  8173. map[m].flag.nozenypenalty=0;
  8174. break;
  8175. case MF_PVP:
  8176. map[m].flag.pvp=0;
  8177. break;
  8178. case MF_PVP_NOPARTY:
  8179. map[m].flag.pvp_noparty=0;
  8180. break;
  8181. case MF_PVP_NOGUILD:
  8182. map[m].flag.pvp_noguild=0;
  8183. break;
  8184. case MF_GVG:
  8185. map[m].flag.gvg=0;
  8186. break;
  8187. case MF_GVG_NOPARTY:
  8188. map[m].flag.gvg_noparty=0;
  8189. break;
  8190. case MF_GVG_DUNGEON:
  8191. map[m].flag.gvg_dungeon=0;
  8192. break;
  8193. case MF_GVG_CASTLE:
  8194. map[m].flag.gvg_castle=0;
  8195. break;
  8196. case MF_NOZENYPENALTY:
  8197. map[m].flag.nozenypenalty=0;
  8198. break;
  8199. case MF_NOTRADE:
  8200. map[m].flag.notrade=0;
  8201. break;
  8202. case MF_NODROP:
  8203. map[m].flag.nodrop=0;
  8204. break;
  8205. case MF_NOSKILL:
  8206. map[m].flag.noskill=0;
  8207. break;
  8208. case MF_NOWARP:
  8209. map[m].flag.nowarp=0;
  8210. break;
  8211. case MF_NOICEWALL: // [Valaris]
  8212. map[m].flag.noicewall=0;
  8213. break;
  8214. case MF_SNOW: // [Valaris]
  8215. map[m].flag.snow=0;
  8216. break;
  8217. case MF_CLOUDS:
  8218. map[m].flag.clouds=0;
  8219. break;
  8220. case MF_CLOUDS2: // [Valaris]
  8221. map[m].flag.clouds2=0;
  8222. break;
  8223. case MF_FOG: // [Valaris]
  8224. map[m].flag.fog=0;
  8225. break;
  8226. case MF_FIREWORKS:
  8227. map[m].flag.fireworks=0;
  8228. break;
  8229. case MF_SAKURA: // [Valaris]
  8230. map[m].flag.sakura=0;
  8231. break;
  8232. case MF_LEAVES: // [Valaris]
  8233. map[m].flag.leaves=0;
  8234. break;
  8235. case MF_RAIN: // [Valaris]
  8236. map[m].flag.rain=0;
  8237. break;
  8238. case MF_INDOORS: // celest
  8239. map[m].flag.indoors=0;
  8240. break;
  8241. case MF_NIGHTENABLED:
  8242. map[m].flag.nightenabled=0;
  8243. break;
  8244. case MF_NOGO: // celest
  8245. map[m].flag.nogo=0;
  8246. break;
  8247. case MF_NOBASEEXP:
  8248. map[m].flag.nobaseexp=0;
  8249. break;
  8250. case MF_NOJOBEXP:
  8251. map[m].flag.nojobexp=0;
  8252. break;
  8253. case MF_NOMOBLOOT:
  8254. map[m].flag.nomobloot=0;
  8255. break;
  8256. case MF_NOMVPLOOT:
  8257. map[m].flag.nomvploot=0;
  8258. break;
  8259. case MF_NORETURN:
  8260. map[m].flag.noreturn=0;
  8261. break;
  8262. case MF_NOWARPTO:
  8263. map[m].flag.nowarpto=0;
  8264. break;
  8265. case MF_NIGHTMAREDROP:
  8266. map[m].flag.pvp_nightmaredrop=0;
  8267. break;
  8268. case MF_RESTRICTED:
  8269. map[m].flag.restricted=0;
  8270. break;
  8271. case MF_NOCOMMAND:
  8272. map[m].nocommand=0;
  8273. break;
  8274. case MF_JEXP:
  8275. map[m].jexp=100;
  8276. break;
  8277. case MF_BEXP:
  8278. map[m].bexp=100;
  8279. break;
  8280. case MF_NOVENDING:
  8281. map[m].flag.novending=0;
  8282. break;
  8283. case MF_LOADEVENT:
  8284. map[m].flag.loadevent=0;
  8285. break;
  8286. case MF_NOCHAT:
  8287. map[m].flag.nochat=0;
  8288. break;
  8289. case MF_PARTYLOCK:
  8290. map[m].flag.partylock=0;
  8291. break;
  8292. case MF_GUILDLOCK:
  8293. map[m].flag.guildlock=0;
  8294. break;
  8295. }
  8296. }
  8297. return 0;
  8298. }
  8299. BUILDIN_FUNC(pvpon)
  8300. {
  8301. int m,i,users;
  8302. const char *str;
  8303. TBL_PC *pl_sd=NULL, **pl_allsd;
  8304. str=script_getstr(st,2);
  8305. m = map_mapname2mapid(str);
  8306. if(m >= 0 && !map[m].flag.pvp) {
  8307. map[m].flag.pvp = 1;
  8308. clif_send0199(m,1);
  8309. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8310. return 0;
  8311. pl_allsd = map_getallusers(&users);
  8312. for(i=0;i<users;i++)
  8313. {
  8314. if ((pl_sd = pl_allsd[i]) && m == pl_sd->bl.m && pl_sd->pvp_timer == -1)
  8315. {
  8316. pl_sd->pvp_timer=add_timer(gettick()+200,pc_calc_pvprank_timer,pl_sd->bl.id,0);
  8317. pl_sd->pvp_rank=0;
  8318. pl_sd->pvp_lastusers=0;
  8319. pl_sd->pvp_point=5;
  8320. pl_sd->pvp_won = 0;
  8321. pl_sd->pvp_lost = 0;
  8322. }
  8323. }
  8324. }
  8325. return 0;
  8326. }
  8327. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap) {
  8328. TBL_PC* sd = (TBL_PC*)bl;
  8329. clif_pvpset(sd, 0, 0, 2);
  8330. if (sd->pvp_timer != UINT_MAX) {
  8331. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8332. sd->pvp_timer = UINT_MAX;
  8333. }
  8334. return 0;
  8335. }
  8336. BUILDIN_FUNC(pvpoff)
  8337. {
  8338. int m;
  8339. const char *str;
  8340. str=script_getstr(st,2);
  8341. m = map_mapname2mapid(str);
  8342. if(m < 0 || !map[m].flag.pvp)
  8343. return 0; //fixed Lupus
  8344. map[m].flag.pvp = 0;
  8345. clif_send0199(m,0);
  8346. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8347. return 0;
  8348. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8349. return 0;
  8350. }
  8351. BUILDIN_FUNC(gvgon)
  8352. {
  8353. int m;
  8354. const char *str;
  8355. str=script_getstr(st,2);
  8356. m = map_mapname2mapid(str);
  8357. if(m >= 0 && !map[m].flag.gvg) {
  8358. map[m].flag.gvg = 1;
  8359. clif_send0199(m,3);
  8360. }
  8361. return 0;
  8362. }
  8363. BUILDIN_FUNC(gvgoff)
  8364. {
  8365. int m;
  8366. const char *str;
  8367. str=script_getstr(st,2);
  8368. m = map_mapname2mapid(str);
  8369. if(m >= 0 && map[m].flag.gvg) {
  8370. map[m].flag.gvg = 0;
  8371. clif_send0199(m,0);
  8372. }
  8373. return 0;
  8374. }
  8375. /*==========================================
  8376. * Shows an emoticon on top of the player/npc
  8377. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8378. *------------------------------------------*/
  8379. //Optional second parameter added by [Skotlex]
  8380. BUILDIN_FUNC(emotion)
  8381. {
  8382. int type;
  8383. int player=0;
  8384. type=script_getnum(st,2);
  8385. if(type < 0 || type > 100)
  8386. return 0;
  8387. if( script_hasdata(st,3) )
  8388. player=script_getnum(st,3);
  8389. if (player) {
  8390. TBL_PC *sd = script_rid2sd(st);
  8391. if (sd)
  8392. clif_emotion(&sd->bl,type);
  8393. } else
  8394. clif_emotion(map_id2bl(st->oid),type);
  8395. return 0;
  8396. }
  8397. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  8398. {
  8399. int m=va_arg(ap,int);
  8400. int g_id=va_arg(ap,int);
  8401. int flag=va_arg(ap,int);
  8402. TBL_PC *sd = (TBL_PC*)data;
  8403. if(!sd || sd->bl.m != m)
  8404. return 0;
  8405. if(
  8406. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8407. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8408. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8409. )
  8410. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8411. return 1;
  8412. }
  8413. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8414. {
  8415. struct mob_data *md=(struct mob_data *)bl;
  8416. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8417. status_kill(bl);
  8418. return 0;
  8419. }
  8420. BUILDIN_FUNC(maprespawnguildid)
  8421. {
  8422. const char *mapname=script_getstr(st,2);
  8423. int g_id=script_getnum(st,3);
  8424. int flag=script_getnum(st,4);
  8425. int m=map_mapname2mapid(mapname);
  8426. if(m == -1)
  8427. return 0;
  8428. //Catch ALL players (in case some are 'between maps' on execution time)
  8429. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8430. if (flag&4) //Remove script mobs.
  8431. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8432. return 0;
  8433. }
  8434. BUILDIN_FUNC(agitstart)
  8435. {
  8436. if(agit_flag==1) return 0; // Agit already Start.
  8437. agit_flag=1;
  8438. guild_agit_start();
  8439. return 0;
  8440. }
  8441. BUILDIN_FUNC(agitend)
  8442. {
  8443. if(agit_flag==0) return 0; // Agit already End.
  8444. agit_flag=0;
  8445. guild_agit_end();
  8446. return 0;
  8447. }
  8448. /*==========================================
  8449. * Returns whether woe is on or off. // choice script
  8450. *------------------------------------------*/
  8451. BUILDIN_FUNC(agitcheck)
  8452. {
  8453. script_pushint(st,agit_flag);
  8454. return 0;
  8455. }
  8456. BUILDIN_FUNC(flagemblem)
  8457. {
  8458. int g_id=script_getnum(st,2);
  8459. if(g_id < 0) return 0;
  8460. // printf("Script.c: [FlagEmblem] GuildID=%d, Emblem=%d.\n", g->guild_id, g->emblem_id);
  8461. ((struct npc_data *)map_id2bl(st->oid))->u.scr.guild_id = g_id;
  8462. return 0;
  8463. }
  8464. BUILDIN_FUNC(getcastlename)
  8465. {
  8466. char mapname[MAP_NAME_LENGTH_EXT];
  8467. struct guild_castle *gc;
  8468. strncpy(mapname, script_getstr(st,2), MAP_NAME_LENGTH_EXT);
  8469. gc = guild_mapname2gc(mapname);
  8470. if(gc)
  8471. script_pushconststr(st,gc->castle_name);
  8472. else
  8473. script_pushconststr(st,"");
  8474. return 0;
  8475. }
  8476. BUILDIN_FUNC(getcastledata)
  8477. {
  8478. char mapname[MAP_NAME_LENGTH_EXT];
  8479. int index=script_getnum(st,3);
  8480. const char *event=NULL;
  8481. struct guild_castle *gc;
  8482. int i;
  8483. strncpy(mapname, script_getstr(st,2), MAP_NAME_LENGTH_EXT);
  8484. gc = guild_mapname2gc(mapname);
  8485. if(script_hasdata(st,4) && index==0 && gc) {
  8486. event=script_getstr(st,4);
  8487. check_event(st, event);
  8488. guild_addcastleinfoevent(gc->castle_id,17,event);
  8489. }
  8490. if(gc){
  8491. switch(index){
  8492. case 0:
  8493. for(i=1;i<26;i++) // Initialize[AgitInit]
  8494. guild_castledataload(gc->castle_id,i);
  8495. break;
  8496. case 1:
  8497. script_pushint(st,gc->guild_id); break;
  8498. case 2:
  8499. script_pushint(st,gc->economy); break;
  8500. case 3:
  8501. script_pushint(st,gc->defense); break;
  8502. case 4:
  8503. script_pushint(st,gc->triggerE); break;
  8504. case 5:
  8505. script_pushint(st,gc->triggerD); break;
  8506. case 6:
  8507. script_pushint(st,gc->nextTime); break;
  8508. case 7:
  8509. script_pushint(st,gc->payTime); break;
  8510. case 8:
  8511. script_pushint(st,gc->createTime); break;
  8512. case 9:
  8513. script_pushint(st,gc->visibleC); break;
  8514. case 10:
  8515. case 11:
  8516. case 12:
  8517. case 13:
  8518. case 14:
  8519. case 15:
  8520. case 16:
  8521. case 17:
  8522. script_pushint(st,gc->guardian[index-10].visible); break;
  8523. case 18:
  8524. case 19:
  8525. case 20:
  8526. case 21:
  8527. case 22:
  8528. case 23:
  8529. case 24:
  8530. case 25:
  8531. script_pushint(st,gc->guardian[index-18].hp); break;
  8532. default:
  8533. script_pushint(st,0); break;
  8534. }
  8535. return 0;
  8536. }
  8537. script_pushint(st,0);
  8538. return 0;
  8539. }
  8540. BUILDIN_FUNC(setcastledata)
  8541. {
  8542. char mapname[MAP_NAME_LENGTH_EXT];
  8543. int index=script_getnum(st,3);
  8544. int value=script_getnum(st,4);
  8545. struct guild_castle *gc;
  8546. strncpy(mapname, script_getstr(st,2), MAP_NAME_LENGTH_EXT);
  8547. gc = guild_mapname2gc(mapname);
  8548. if(gc) {
  8549. // Save Data byself First
  8550. switch(index){
  8551. case 1:
  8552. gc->guild_id = value; break;
  8553. case 2:
  8554. gc->economy = value; break;
  8555. case 3:
  8556. gc->defense = value; break;
  8557. case 4:
  8558. gc->triggerE = value; break;
  8559. case 5:
  8560. gc->triggerD = value; break;
  8561. case 6:
  8562. gc->nextTime = value; break;
  8563. case 7:
  8564. gc->payTime = value; break;
  8565. case 8:
  8566. gc->createTime = value; break;
  8567. case 9:
  8568. gc->visibleC = value; break;
  8569. case 10:
  8570. case 11:
  8571. case 12:
  8572. case 13:
  8573. case 14:
  8574. case 15:
  8575. case 16:
  8576. case 17:
  8577. gc->guardian[index-10].visible = value; break;
  8578. case 18:
  8579. case 19:
  8580. case 20:
  8581. case 21:
  8582. case 22:
  8583. case 23:
  8584. case 24:
  8585. case 25:
  8586. gc->guardian[index-18].hp = value;
  8587. if (gc->guardian[index-18].id)
  8588. { //Update this mob's HP.
  8589. struct block_list *bl = map_id2bl(gc->guardian[index-18].id);
  8590. if (!bl)
  8591. { //Wrong target?
  8592. gc->guardian[index-18].id = 0;
  8593. break;
  8594. }
  8595. if (value < 1) {
  8596. status_kill(bl);
  8597. break;
  8598. }
  8599. status_set_hp(bl, value, 0);
  8600. }
  8601. break;
  8602. default:
  8603. return 0;
  8604. }
  8605. guild_castledatasave(gc->castle_id,index,value);
  8606. }
  8607. return 0;
  8608. }
  8609. /* =====================================================================
  8610. * ギルド情報を要求する
  8611. * ---------------------------------------------------------------------*/
  8612. BUILDIN_FUNC(requestguildinfo)
  8613. {
  8614. int guild_id=script_getnum(st,2);
  8615. const char *event=NULL;
  8616. if( script_hasdata(st,3) ){
  8617. event=script_getstr(st,3);
  8618. check_event(st, event);
  8619. }
  8620. if(guild_id>0)
  8621. guild_npc_request_info(guild_id,event);
  8622. return 0;
  8623. }
  8624. /* =====================================================================
  8625. * カードの数を得る
  8626. * ---------------------------------------------------------------------*/
  8627. BUILDIN_FUNC(getequipcardcnt)
  8628. {
  8629. int i,num;
  8630. TBL_PC *sd;
  8631. int c=MAX_SLOTS;
  8632. num=script_getnum(st,2);
  8633. sd=script_rid2sd(st);
  8634. i=pc_checkequip(sd,equip[num-1]);
  8635. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8636. {
  8637. script_pushint(st,0);
  8638. return 0;
  8639. }
  8640. do{
  8641. if(sd->status.inventory[i].card[c-1] &&
  8642. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8643. script_pushint(st,(c));
  8644. return 0;
  8645. }
  8646. }while(c--);
  8647. script_pushint(st,0);
  8648. return 0;
  8649. }
  8650. /* ================================================================
  8651. * カード取り外し成功
  8652. * ----------------------------------------------------------------*/
  8653. BUILDIN_FUNC(successremovecards)
  8654. {
  8655. int i,j,num,cardflag=0,flag;
  8656. TBL_PC *sd;
  8657. struct item item_tmp;
  8658. int c=MAX_SLOTS;
  8659. num=script_getnum(st,2);
  8660. sd=script_rid2sd(st);
  8661. i=pc_checkequip(sd,equip[num-1]);
  8662. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8663. return 0;
  8664. do{
  8665. if(sd->status.inventory[i].card[c-1] &&
  8666. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8667. cardflag = 1;
  8668. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8669. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8670. item_tmp.attribute=0;
  8671. for (j = 0; j < MAX_SLOTS; j++)
  8672. item_tmp.card[j]=0;
  8673. //Logs items, got from (N)PC scripts [Lupus]
  8674. if(log_config.enable_logs&0x40)
  8675. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8676. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8677. clif_additem(sd,0,0,flag);
  8678. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8679. }
  8680. }
  8681. }while(c--);
  8682. if(cardflag == 1){ // カードを取り除いたアイテム所得
  8683. flag=0;
  8684. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8685. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8686. item_tmp.attribute=sd->status.inventory[i].attribute;
  8687. //Logs items, got from (N)PC scripts [Lupus]
  8688. if(log_config.enable_logs&0x40)
  8689. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8690. for (j = 0; j < MAX_SLOTS; j++)
  8691. item_tmp.card[j]=0;
  8692. pc_delitem(sd,i,1,0);
  8693. //Logs items, got from (N)PC scripts [Lupus]
  8694. if(log_config.enable_logs&0x40)
  8695. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8696. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8697. clif_additem(sd,0,0,flag);
  8698. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8699. }
  8700. clif_misceffect(&sd->bl,3);
  8701. return 0;
  8702. }
  8703. return 0;
  8704. }
  8705. /* ================================================================
  8706. * カード取り外し失敗 slot,type
  8707. * type=0: 両方損失、1:カード損失、2:武具損失、3:損失無し
  8708. * ----------------------------------------------------------------*/
  8709. BUILDIN_FUNC(failedremovecards)
  8710. {
  8711. int i,j,num,cardflag=0,flag,typefail;
  8712. TBL_PC *sd;
  8713. struct item item_tmp;
  8714. int c=MAX_SLOTS;
  8715. num=script_getnum(st,2);
  8716. typefail=script_getnum(st,3);
  8717. sd=script_rid2sd(st);
  8718. i=pc_checkequip(sd,equip[num-1]);
  8719. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8720. return 0;
  8721. do{
  8722. if(sd->status.inventory[i].card[c-1] &&
  8723. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8724. cardflag = 1;
  8725. if(typefail == 2){ // 武具のみ損失なら、カードは受け取らせる
  8726. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8727. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8728. item_tmp.attribute=0;
  8729. for (j = 0; j < MAX_SLOTS; j++)
  8730. item_tmp.card[j]=0;
  8731. //Logs items, got from (N)PC scripts [Lupus]
  8732. if(log_config.enable_logs&0x40)
  8733. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8734. if((flag=pc_additem(sd,&item_tmp,1))){
  8735. clif_additem(sd,0,0,flag);
  8736. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8737. }
  8738. }
  8739. }
  8740. }while(c--);
  8741. if(cardflag == 1){
  8742. if(typefail == 0 || typefail == 2){ // 武具損失
  8743. //Logs items, got from (N)PC scripts [Lupus]
  8744. if(log_config.enable_logs&0x40)
  8745. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8746. pc_delitem(sd,i,1,0);
  8747. clif_misceffect(&sd->bl,2);
  8748. return 0;
  8749. }
  8750. if(typefail == 1){ // カードのみ損失(武具を返す)
  8751. flag=0;
  8752. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8753. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8754. item_tmp.attribute=sd->status.inventory[i].attribute;
  8755. //Logs items, got from (N)PC scripts [Lupus]
  8756. if(log_config.enable_logs&0x40)
  8757. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8758. for (j = 0; j < MAX_SLOTS; j++)
  8759. item_tmp.card[j]=0;
  8760. pc_delitem(sd,i,1,0);
  8761. //Logs items, got from (N)PC scripts [Lupus]
  8762. if(log_config.enable_logs&0x40)
  8763. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8764. if((flag=pc_additem(sd,&item_tmp,1))){
  8765. clif_additem(sd,0,0,flag);
  8766. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8767. }
  8768. }
  8769. clif_misceffect(&sd->bl,2);
  8770. return 0;
  8771. }
  8772. return 0;
  8773. }
  8774. /* ================================================================
  8775. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8776. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8777. * improved by [Lance]
  8778. * ================================================================*/
  8779. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8780. {
  8781. int x,y,m,check_val=0,check_ID=0,i=0;
  8782. struct guild *g = NULL;
  8783. struct party_data *p = NULL;
  8784. const char *str;
  8785. const char *mapname;
  8786. unsigned int index;
  8787. mapname=script_getstr(st,2);
  8788. str=script_getstr(st,3);
  8789. x=script_getnum(st,4);
  8790. y=script_getnum(st,5);
  8791. if(script_hasdata(st,7)){
  8792. check_val=script_getnum(st,6);
  8793. check_ID=script_getnum(st,7);
  8794. }
  8795. if((m=map_mapname2mapid(mapname))< 0)
  8796. return 0;
  8797. if(!(index=mapindex_name2id(str)))
  8798. return 0;
  8799. switch(check_val){
  8800. case 1:
  8801. g = guild_search(check_ID);
  8802. if (g){
  8803. for( i=0; i < g->max_member; i++)
  8804. {
  8805. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8806. pc_setpos(g->member[i].sd,index,x,y,3);
  8807. }
  8808. }
  8809. }
  8810. break;
  8811. case 2:
  8812. p = party_search(check_ID);
  8813. if(p){
  8814. for(i=0;i<MAX_PARTY; i++){
  8815. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8816. pc_setpos(p->data[i].sd,index,x,y,3);
  8817. }
  8818. }
  8819. }
  8820. break;
  8821. default:
  8822. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8823. break;
  8824. }
  8825. return 0;
  8826. }
  8827. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8828. {
  8829. const char *npc,*command;
  8830. npc=script_getstr(st,2);
  8831. command=script_getstr(st,3);
  8832. npc_command(map_id2sd(st->rid),npc,command);
  8833. return 0;
  8834. }
  8835. BUILDIN_FUNC(inittimer) // Added by RoVeRT
  8836. {
  8837. // struct npc_data *nd=(struct npc_data*)map_id2bl(st->oid);
  8838. // nd->lastaction=nd->timer=gettick();
  8839. npc_do_ontimer(st->oid, 1);
  8840. return 0;
  8841. }
  8842. BUILDIN_FUNC(stoptimer) // Added by RoVeRT
  8843. {
  8844. // struct npc_data *nd=(struct npc_data*)map_id2bl(st->oid);
  8845. // nd->lastaction=nd->timer=-1;
  8846. npc_do_ontimer(st->oid, 0);
  8847. return 0;
  8848. }
  8849. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8850. {
  8851. char *event=va_arg(ap,char *);
  8852. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8853. return 1;
  8854. return 0;
  8855. }
  8856. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8857. {
  8858. const char *mapname,*event;
  8859. int m;
  8860. mapname=script_getstr(st,2);
  8861. event=script_getstr(st,3);
  8862. check_event(st, event);
  8863. if( (m=map_mapname2mapid(mapname))<0 ) {
  8864. script_pushint(st,-1);
  8865. return 0;
  8866. }
  8867. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8868. return 0;
  8869. }
  8870. BUILDIN_FUNC(marriage)
  8871. {
  8872. const char *partner=script_getstr(st,2);
  8873. TBL_PC *sd=script_rid2sd(st);
  8874. TBL_PC *p_sd=map_nick2sd(partner);
  8875. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8876. script_pushint(st,0);
  8877. return 0;
  8878. }
  8879. script_pushint(st,1);
  8880. return 0;
  8881. }
  8882. BUILDIN_FUNC(wedding_effect)
  8883. {
  8884. TBL_PC *sd=script_rid2sd(st);
  8885. struct block_list *bl;
  8886. if(sd==NULL) {
  8887. bl=map_id2bl(st->oid);
  8888. } else
  8889. bl=&sd->bl;
  8890. clif_wedding_effect(bl);
  8891. return 0;
  8892. }
  8893. BUILDIN_FUNC(divorce)
  8894. {
  8895. TBL_PC *sd=script_rid2sd(st);
  8896. if(sd==NULL || pc_divorce(sd) < 0){
  8897. script_pushint(st,0);
  8898. return 0;
  8899. }
  8900. script_pushint(st,1);
  8901. return 0;
  8902. }
  8903. BUILDIN_FUNC(ispartneron)
  8904. {
  8905. TBL_PC *sd=script_rid2sd(st);
  8906. TBL_PC *p_sd=NULL;
  8907. if(sd==NULL || !pc_ismarried(sd) ||
  8908. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8909. script_pushint(st,0);
  8910. return 0;
  8911. }
  8912. script_pushint(st,1);
  8913. return 0;
  8914. }
  8915. BUILDIN_FUNC(getpartnerid)
  8916. {
  8917. TBL_PC *sd=script_rid2sd(st);
  8918. if (sd == NULL) {
  8919. script_pushint(st,0);
  8920. return 0;
  8921. }
  8922. script_pushint(st,sd->status.partner_id);
  8923. return 0;
  8924. }
  8925. BUILDIN_FUNC(getchildid)
  8926. {
  8927. TBL_PC *sd=script_rid2sd(st);
  8928. if (sd == NULL) {
  8929. script_pushint(st,0);
  8930. return 0;
  8931. }
  8932. script_pushint(st,sd->status.child);
  8933. return 0;
  8934. }
  8935. BUILDIN_FUNC(getmotherid)
  8936. {
  8937. TBL_PC *sd=script_rid2sd(st);
  8938. if (sd == NULL) {
  8939. script_pushint(st,0);
  8940. return 0;
  8941. }
  8942. script_pushint(st,sd->status.mother);
  8943. return 0;
  8944. }
  8945. BUILDIN_FUNC(getfatherid)
  8946. {
  8947. TBL_PC *sd=script_rid2sd(st);
  8948. if (sd == NULL) {
  8949. script_pushint(st,0);
  8950. return 0;
  8951. }
  8952. script_pushint(st,sd->status.father);
  8953. return 0;
  8954. }
  8955. BUILDIN_FUNC(warppartner)
  8956. {
  8957. int x,y;
  8958. unsigned short mapindex;
  8959. const char *str;
  8960. TBL_PC *sd=script_rid2sd(st);
  8961. TBL_PC *p_sd=NULL;
  8962. if(sd==NULL || !pc_ismarried(sd) ||
  8963. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8964. script_pushint(st,0);
  8965. return 0;
  8966. }
  8967. str=script_getstr(st,2);
  8968. x=script_getnum(st,3);
  8969. y=script_getnum(st,4);
  8970. mapindex = mapindex_name2id(str);
  8971. if (mapindex) {
  8972. pc_setpos(p_sd,mapindex,x,y,0);
  8973. script_pushint(st,1);
  8974. } else
  8975. script_pushint(st,0);
  8976. return 0;
  8977. }
  8978. /*================================================
  8979. * Script for Displaying MOB Information [Valaris]
  8980. *------------------------------------------------*/
  8981. BUILDIN_FUNC(strmobinfo)
  8982. {
  8983. int num=script_getnum(st,2);
  8984. int class_=script_getnum(st,3);
  8985. if(!mobdb_checkid(class_))
  8986. {
  8987. script_pushint(st,0);
  8988. return 0;
  8989. }
  8990. switch (num) {
  8991. case 1:
  8992. script_pushconststr(st,mob_db(class_)->name);
  8993. break;
  8994. case 2:
  8995. script_pushconststr(st,mob_db(class_)->jname);
  8996. break;
  8997. case 3:
  8998. script_pushint(st,mob_db(class_)->lv);
  8999. break;
  9000. case 4:
  9001. script_pushint(st,mob_db(class_)->status.max_hp);
  9002. break;
  9003. case 5:
  9004. script_pushint(st,mob_db(class_)->status.max_sp);
  9005. break;
  9006. case 6:
  9007. script_pushint(st,mob_db(class_)->base_exp);
  9008. break;
  9009. case 7:
  9010. script_pushint(st,mob_db(class_)->job_exp);
  9011. break;
  9012. default:
  9013. script_pushint(st,0);
  9014. break;
  9015. }
  9016. return 0;
  9017. }
  9018. /*==========================================
  9019. * Summon guardians [Valaris]
  9020. * guardian "<map name>",<x>,<y>,"<name to show>",<mob id>,{,"<event label>"}{,<guardian index>};
  9021. *------------------------------------------*/
  9022. BUILDIN_FUNC(guardian)
  9023. {
  9024. int class_=0,x=0,y=0,guardian=0;
  9025. const char *str,*map,*evt="";
  9026. struct script_data *data;
  9027. map =script_getstr(st,2);
  9028. x =script_getnum(st,3);
  9029. y =script_getnum(st,4);
  9030. str =script_getstr(st,5);
  9031. class_=script_getnum(st,6);
  9032. if( script_hasdata(st,8) )
  9033. {// "<event label>",<guardian index>
  9034. evt=script_getstr(st,7);
  9035. guardian=script_getnum(st,8);
  9036. } else if( script_hasdata(st,7) ){
  9037. data=script_getdata(st,7);
  9038. get_val(st,data);
  9039. if( data_isstring(data) )
  9040. {// "<event label>"
  9041. evt=script_getstr(st,7);
  9042. } else if( data_isint(data) )
  9043. {// <guardian index>
  9044. guardian=script_getnum(st,7);
  9045. } else {
  9046. ShowError("buildin_guardian: invalid data type for argument #8 (%d).", data->type);
  9047. report_src(st);
  9048. return 1;
  9049. }
  9050. }
  9051. check_event(st, evt);
  9052. mob_spawn_guardian(map,x,y,str,class_,evt,guardian);
  9053. return 0;
  9054. }
  9055. /*================================================
  9056. * Script for Displaying Guardian Info [Valaris]
  9057. *------------------------------------------------*/
  9058. BUILDIN_FUNC(guardianinfo)
  9059. {
  9060. int guardian=script_getnum(st,2);
  9061. TBL_PC *sd=script_rid2sd(st);
  9062. struct guild_castle *gc=guild_mapname2gc(map[sd->bl.m].name);
  9063. if (guardian < 0 || guardian >= MAX_GUARDIANS || gc==NULL)
  9064. {
  9065. script_pushint(st,-1);
  9066. return 0;
  9067. }
  9068. if(gc->guardian[guardian].visible)
  9069. script_pushint(st,gc->guardian[guardian].hp);
  9070. else script_pushint(st,-1);
  9071. return 0;
  9072. }
  9073. /*==========================================
  9074. * IDからItem名
  9075. *------------------------------------------*/
  9076. BUILDIN_FUNC(getitemname)
  9077. {
  9078. int item_id=0;
  9079. struct item_data *i_data;
  9080. char *item_name;
  9081. struct script_data *data;
  9082. data=script_getdata(st,2);
  9083. get_val(st,data);
  9084. if( data_isstring(data) ){
  9085. const char *name=conv_str(st,data);
  9086. struct item_data *item_data = itemdb_searchname(name);
  9087. if( item_data )
  9088. item_id=item_data->nameid;
  9089. }else
  9090. item_id=conv_num(st,data);
  9091. i_data = itemdb_exists(item_id);
  9092. if (i_data == NULL)
  9093. {
  9094. script_pushconststr(st,"null");
  9095. return 0;
  9096. }
  9097. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9098. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9099. script_pushstr(st,item_name);
  9100. return 0;
  9101. }
  9102. /*==========================================
  9103. * Returns number of slots an item has. [Skotlex]
  9104. *------------------------------------------*/
  9105. BUILDIN_FUNC(getitemslots)
  9106. {
  9107. int item_id;
  9108. struct item_data *i_data;
  9109. item_id=script_getnum(st,2);
  9110. i_data = itemdb_exists(item_id);
  9111. if (i_data)
  9112. script_pushint(st,i_data->slot);
  9113. else
  9114. script_pushint(st,-1);
  9115. return 0;
  9116. }
  9117. /*==========================================
  9118. * Returns some values of an item [Lupus]
  9119. * Price, Weight, etc...
  9120. getiteminfo(itemID,n), where n
  9121. 0 value_buy;
  9122. 1 value_sell;
  9123. 2 type;
  9124. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9125. if = 0, then monsters don't drop it at all (rare or a quest item)
  9126. if = 10000, then this item is sold in NPC shops only
  9127. 4 sex;
  9128. 5 equip;
  9129. 6 weight;
  9130. 7 atk;
  9131. 8 def;
  9132. 9 range;
  9133. 10 slot;
  9134. 11 look;
  9135. 12 elv;
  9136. 13 wlv;
  9137. 14 view id
  9138. *------------------------------------------*/
  9139. BUILDIN_FUNC(getiteminfo)
  9140. {
  9141. int item_id,n;
  9142. int *item_arr;
  9143. struct item_data *i_data;
  9144. item_id = script_getnum(st,2);
  9145. n = script_getnum(st,3);
  9146. i_data = itemdb_exists(item_id);
  9147. if (i_data && n>=0 && n<14) {
  9148. item_arr = (int*)&i_data->value_buy;
  9149. script_pushint(st,item_arr[n]);
  9150. } else
  9151. script_pushint(st,-1);
  9152. return 0;
  9153. }
  9154. /*==========================================
  9155. * Set some values of an item [Lupus]
  9156. * Price, Weight, etc...
  9157. setiteminfo(itemID,n,Value), where n
  9158. 0 value_buy;
  9159. 1 value_sell;
  9160. 2 type;
  9161. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9162. if = 0, then monsters don't drop it at all (rare or a quest item)
  9163. if = 10000, then this item is sold in NPC shops only
  9164. 4 sex;
  9165. 5 equip;
  9166. 6 weight;
  9167. 7 atk;
  9168. 8 def;
  9169. 9 range;
  9170. 10 slot;
  9171. 11 look;
  9172. 12 elv;
  9173. 13 wlv;
  9174. 14 view id
  9175. * Returns Value or -1 if the wrong field's been set
  9176. *------------------------------------------*/
  9177. BUILDIN_FUNC(setiteminfo)
  9178. {
  9179. int item_id,n,value;
  9180. int *item_arr;
  9181. struct item_data *i_data;
  9182. item_id = script_getnum(st,2);
  9183. n = script_getnum(st,3);
  9184. value = script_getnum(st,4);
  9185. i_data = itemdb_exists(item_id);
  9186. if (i_data && n>=0 && n<14) {
  9187. item_arr = (int*)&i_data->value_buy;
  9188. item_arr[n] = value;
  9189. script_pushint(st,value);
  9190. } else
  9191. script_pushint(st,-1);
  9192. return 0;
  9193. }
  9194. /*==========================================
  9195. * Returns value from equipped item slot n [Lupus]
  9196. getequipcardid(num,slot)
  9197. where
  9198. num = eqip position slot
  9199. slot = 0,1,2,3 (Card Slot N)
  9200. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9201. it's useful when you want to check item cards or if it's signed
  9202. Useful for such quests as "Sign this refined item with players name" etc
  9203. Hat[0] +4 -> Player's Hat[0] +4
  9204. *------------------------------------------*/
  9205. BUILDIN_FUNC(getequipcardid)
  9206. {
  9207. int i,num,slot;
  9208. TBL_PC *sd;
  9209. num=script_getnum(st,2);
  9210. slot=script_getnum(st,3);
  9211. sd=script_rid2sd(st);
  9212. i=pc_checkequip(sd,equip[num-1]);
  9213. if(i >= 0 && slot>=0 && slot<4)
  9214. script_pushint(st,sd->status.inventory[i].card[slot]);
  9215. else
  9216. script_pushint(st,0);
  9217. return 0;
  9218. }
  9219. /*==========================================
  9220. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9221. *------------------------------------------*/
  9222. BUILDIN_FUNC(petskillbonus)
  9223. {
  9224. struct pet_data *pd;
  9225. TBL_PC *sd=script_rid2sd(st);
  9226. if(sd==NULL || sd->pd==NULL)
  9227. return 0;
  9228. pd=sd->pd;
  9229. if (pd->bonus)
  9230. { //Clear previous bonus
  9231. if (pd->bonus->timer != -1)
  9232. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9233. } else //init
  9234. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9235. pd->bonus->type=script_getnum(st,2);
  9236. pd->bonus->val=script_getnum(st,3);
  9237. pd->bonus->duration=script_getnum(st,4);
  9238. pd->bonus->delay=script_getnum(st,5);
  9239. if (pd->state.skillbonus == 1)
  9240. pd->state.skillbonus=0; // waiting state
  9241. // wait for timer to start
  9242. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9243. pd->bonus->timer=-1;
  9244. else
  9245. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9246. return 0;
  9247. }
  9248. /*==========================================
  9249. * pet looting [Valaris] //Rewritten by [Skotlex]
  9250. *------------------------------------------*/
  9251. BUILDIN_FUNC(petloot)
  9252. {
  9253. int max;
  9254. struct pet_data *pd;
  9255. TBL_PC *sd=script_rid2sd(st);
  9256. if(sd==NULL || sd->pd==NULL)
  9257. return 0;
  9258. max=script_getnum(st,2);
  9259. if(max < 1)
  9260. max = 1; //Let'em loot at least 1 item.
  9261. else if (max > MAX_PETLOOT_SIZE)
  9262. max = MAX_PETLOOT_SIZE;
  9263. pd = sd->pd;
  9264. if (pd->loot != NULL)
  9265. { //Release whatever was there already and reallocate memory
  9266. pet_lootitem_drop(pd, pd->msd);
  9267. aFree(pd->loot->item);
  9268. }
  9269. else
  9270. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9271. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9272. pd->loot->max=max;
  9273. pd->loot->count = 0;
  9274. pd->loot->weight = 0;
  9275. return 0;
  9276. }
  9277. /*==========================================
  9278. * PCの所持品情報読み取り
  9279. *------------------------------------------*/
  9280. BUILDIN_FUNC(getinventorylist)
  9281. {
  9282. TBL_PC *sd=script_rid2sd(st);
  9283. unsigned char card_var[NAME_LENGTH];
  9284. int i,j=0,k;
  9285. if(!sd) return 0;
  9286. for(i=0;i<MAX_INVENTORY;i++){
  9287. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9288. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9289. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9290. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9291. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9292. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9293. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9294. for (k = 0; k < MAX_SLOTS; k++)
  9295. {
  9296. sprintf(card_var, "@inventorylist_card%d",k+1);
  9297. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9298. }
  9299. j++;
  9300. }
  9301. }
  9302. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9303. return 0;
  9304. }
  9305. BUILDIN_FUNC(getskilllist)
  9306. {
  9307. TBL_PC *sd=script_rid2sd(st);
  9308. int i,j=0;
  9309. if(!sd) return 0;
  9310. for(i=0;i<MAX_SKILL;i++){
  9311. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9312. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9313. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9314. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9315. j++;
  9316. }
  9317. }
  9318. pc_setreg(sd,add_str("@skilllist_count"),j);
  9319. return 0;
  9320. }
  9321. BUILDIN_FUNC(clearitem)
  9322. {
  9323. TBL_PC *sd=script_rid2sd(st);
  9324. int i;
  9325. if(sd==NULL) return 0;
  9326. for (i=0; i<MAX_INVENTORY; i++) {
  9327. if (sd->status.inventory[i].amount) {
  9328. //Logs items, got from (N)PC scripts [Lupus]
  9329. if(log_config.enable_logs&0x40)
  9330. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9331. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9332. }
  9333. }
  9334. return 0;
  9335. }
  9336. /*==========================================
  9337. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9338. *------------------------------------------*/
  9339. BUILDIN_FUNC(disguise)
  9340. {
  9341. int id;
  9342. TBL_PC* sd = script_rid2sd(st);
  9343. if (sd == NULL) return 0;
  9344. id = script_getnum(st,2);
  9345. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9346. pc_disguise(sd, id);
  9347. script_pushint(st,id);
  9348. } else
  9349. script_pushint(st,0);
  9350. return 0;
  9351. }
  9352. /*==========================================
  9353. * Undisguise Player (returns 1 if success, 0 on fail)
  9354. *------------------------------------------*/
  9355. BUILDIN_FUNC(undisguise)
  9356. {
  9357. TBL_PC* sd = script_rid2sd(st);
  9358. if (sd == NULL) return 0;
  9359. if (sd->disguise) {
  9360. pc_disguise(sd, 0);
  9361. script_pushint(st,0);
  9362. } else {
  9363. script_pushint(st,1);
  9364. }
  9365. return 0;
  9366. }
  9367. /*==========================================
  9368. * NPCクラスチェンジ
  9369. * classは変わりたいclass
  9370. * typeは通常0なのかな?
  9371. *------------------------------------------*/
  9372. BUILDIN_FUNC(classchange)
  9373. {
  9374. int _class,type;
  9375. struct block_list *bl=map_id2bl(st->oid);
  9376. if(bl==NULL) return 0;
  9377. _class=script_getnum(st,2);
  9378. type=script_getnum(st,3);
  9379. clif_class_change(bl,_class,type);
  9380. return 0;
  9381. }
  9382. /*==========================================
  9383. * NPCから発生するエフェクト
  9384. *------------------------------------------*/
  9385. BUILDIN_FUNC(misceffect)
  9386. {
  9387. int type;
  9388. type=script_getnum(st,2);
  9389. if(st->oid && st->oid != fake_nd->bl.id) {
  9390. struct block_list *bl = map_id2bl(st->oid);
  9391. if (bl)
  9392. clif_misceffect2(bl,type);
  9393. } else{
  9394. TBL_PC *sd=script_rid2sd(st);
  9395. if(sd)
  9396. clif_misceffect2(&sd->bl,type);
  9397. }
  9398. return 0;
  9399. }
  9400. /*==========================================
  9401. * サウンドエフェクト
  9402. *------------------------------------------*/
  9403. BUILDIN_FUNC(soundeffect)
  9404. {
  9405. // Redundn
  9406. TBL_PC *sd=script_rid2sd(st);
  9407. const char *name;
  9408. int type=0;
  9409. name=script_getstr(st,2);
  9410. type=script_getnum(st,3);
  9411. if(sd){
  9412. if(!st->rid)
  9413. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9414. else{
  9415. clif_soundeffect(sd,&sd->bl,name,type);
  9416. }
  9417. }
  9418. return 0;
  9419. }
  9420. int soundeffect_sub(struct block_list* bl,va_list ap)
  9421. {
  9422. char *name;
  9423. int type;
  9424. nullpo_retr(0, bl);
  9425. nullpo_retr(0, ap);
  9426. name = va_arg(ap,char *);
  9427. type = va_arg(ap,int);
  9428. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9429. return 0;
  9430. }
  9431. BUILDIN_FUNC(soundeffectall)
  9432. {
  9433. // [Lance] - Improved.
  9434. const char *name, *map = NULL;
  9435. struct block_list *bl;
  9436. int type, coverage, x0, y0, x1, y1;
  9437. name=script_getstr(st,2);
  9438. type=script_getnum(st,3);
  9439. coverage=script_getnum(st,4);
  9440. if(!st->rid)
  9441. bl = map_id2bl(st->oid);
  9442. else
  9443. bl = &(script_rid2sd(st)->bl);
  9444. if(bl){
  9445. if(coverage < 23){
  9446. clif_soundeffectall(bl,name,type,coverage);
  9447. }else {
  9448. if(script_hasdata(st,9)){
  9449. map= script_getstr(st,5);
  9450. x0 = script_getnum(st,6);
  9451. y0 = script_getnum(st,7);
  9452. x1 = script_getnum(st,8);
  9453. y1 = script_getnum(st,9);
  9454. map_foreachinarea(soundeffect_sub,map_mapname2mapid(map),x0,y0,x1,y1,BL_PC,name,type);
  9455. } else {
  9456. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9457. }
  9458. }
  9459. }
  9460. return 0;
  9461. }
  9462. /*==========================================
  9463. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9464. *------------------------------------------*/
  9465. BUILDIN_FUNC(petrecovery)
  9466. {
  9467. struct pet_data *pd;
  9468. TBL_PC *sd=script_rid2sd(st);
  9469. if(sd==NULL || sd->pd==NULL)
  9470. return 0;
  9471. pd=sd->pd;
  9472. if (pd->recovery)
  9473. { //Halt previous bonus
  9474. if (pd->recovery->timer != -1)
  9475. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9476. } else //Init
  9477. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9478. pd->recovery->type=script_getnum(st,2);
  9479. pd->recovery->delay=script_getnum(st,3);
  9480. pd->recovery->timer=-1;
  9481. return 0;
  9482. }
  9483. /*==========================================
  9484. * pet healing [Valaris] //Rewritten by [Skotlex]
  9485. *------------------------------------------*/
  9486. BUILDIN_FUNC(petheal)
  9487. {
  9488. struct pet_data *pd;
  9489. TBL_PC *sd=script_rid2sd(st);
  9490. if(sd==NULL || sd->pd==NULL)
  9491. return 0;
  9492. pd=sd->pd;
  9493. if (pd->s_skill)
  9494. { //Clear previous skill
  9495. if (pd->s_skill->timer != -1)
  9496. {
  9497. if (pd->s_skill->id)
  9498. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9499. else
  9500. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9501. }
  9502. } else //init memory
  9503. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9504. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9505. //Use the lv as the amount to heal
  9506. pd->s_skill->lv=script_getnum(st,2);
  9507. pd->s_skill->delay=script_getnum(st,3);
  9508. pd->s_skill->hp=script_getnum(st,4);
  9509. pd->s_skill->sp=script_getnum(st,5);
  9510. //Use delay as initial offset to avoid skill/heal exploits
  9511. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9512. pd->s_skill->timer=-1;
  9513. else
  9514. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9515. return 0;
  9516. }
  9517. /*==========================================
  9518. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9519. *------------------------------------------*/
  9520. BUILDIN_FUNC(petskillattack)
  9521. {
  9522. struct pet_data *pd;
  9523. TBL_PC *sd=script_rid2sd(st);
  9524. if(sd==NULL || sd->pd==NULL)
  9525. return 0;
  9526. pd=sd->pd;
  9527. if (pd->a_skill == NULL)
  9528. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9529. pd->a_skill->id=script_getnum(st,2);
  9530. pd->a_skill->lv=script_getnum(st,3);
  9531. pd->a_skill->div_ = 0;
  9532. pd->a_skill->rate=script_getnum(st,4);
  9533. pd->a_skill->bonusrate=script_getnum(st,5);
  9534. return 0;
  9535. }
  9536. /*==========================================
  9537. * pet attack skills [Valaris]
  9538. *------------------------------------------*/
  9539. BUILDIN_FUNC(petskillattack2)
  9540. {
  9541. struct pet_data *pd;
  9542. TBL_PC *sd=script_rid2sd(st);
  9543. if(sd==NULL || sd->pd==NULL)
  9544. return 0;
  9545. pd=sd->pd;
  9546. if (pd->a_skill == NULL)
  9547. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9548. pd->a_skill->id=script_getnum(st,2);
  9549. pd->a_skill->lv=script_getnum(st,3);
  9550. pd->a_skill->div_ = script_getnum(st,4);
  9551. pd->a_skill->rate=script_getnum(st,5);
  9552. pd->a_skill->bonusrate=script_getnum(st,6);
  9553. return 0;
  9554. }
  9555. /*==========================================
  9556. * pet support skills [Skotlex]
  9557. *------------------------------------------*/
  9558. BUILDIN_FUNC(petskillsupport)
  9559. {
  9560. struct pet_data *pd;
  9561. TBL_PC *sd=script_rid2sd(st);
  9562. if(sd==NULL || sd->pd==NULL)
  9563. return 0;
  9564. pd=sd->pd;
  9565. if (pd->s_skill)
  9566. { //Clear previous skill
  9567. if (pd->s_skill->timer != -1)
  9568. {
  9569. if (pd->s_skill->id)
  9570. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9571. else
  9572. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9573. }
  9574. } else //init memory
  9575. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9576. pd->s_skill->id=script_getnum(st,2);
  9577. pd->s_skill->lv=script_getnum(st,3);
  9578. pd->s_skill->delay=script_getnum(st,4);
  9579. pd->s_skill->hp=script_getnum(st,5);
  9580. pd->s_skill->sp=script_getnum(st,6);
  9581. //Use delay as initial offset to avoid skill/heal exploits
  9582. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9583. pd->s_skill->timer=-1;
  9584. else
  9585. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9586. return 0;
  9587. }
  9588. /*==========================================
  9589. * Scripted skill effects [Celest]
  9590. *------------------------------------------*/
  9591. BUILDIN_FUNC(skilleffect)
  9592. {
  9593. TBL_PC *sd;
  9594. int skillid=script_getnum(st,2);
  9595. int skilllv=script_getnum(st,3);
  9596. sd=script_rid2sd(st);
  9597. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9598. return 0;
  9599. }
  9600. /*==========================================
  9601. * NPC skill effects [Valaris]
  9602. *------------------------------------------*/
  9603. BUILDIN_FUNC(npcskilleffect)
  9604. {
  9605. struct block_list *bl= map_id2bl(st->oid);
  9606. int skillid=script_getnum(st,2);
  9607. int skilllv=script_getnum(st,3);
  9608. int x=script_getnum(st,4);
  9609. int y=script_getnum(st,5);
  9610. if (bl)
  9611. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9612. return 0;
  9613. }
  9614. /*==========================================
  9615. * Special effects [Valaris]
  9616. *------------------------------------------*/
  9617. BUILDIN_FUNC(specialeffect)
  9618. {
  9619. struct block_list *bl=map_id2bl(st->oid);
  9620. if(bl==NULL)
  9621. return 0;
  9622. clif_specialeffect(bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9623. return 0;
  9624. }
  9625. BUILDIN_FUNC(specialeffect2)
  9626. {
  9627. TBL_PC *sd=script_rid2sd(st);
  9628. if(sd==NULL)
  9629. return 0;
  9630. clif_specialeffect(&sd->bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9631. return 0;
  9632. }
  9633. /*==========================================
  9634. * Nude [Valaris]
  9635. *------------------------------------------*/
  9636. BUILDIN_FUNC(nude)
  9637. {
  9638. TBL_PC *sd=script_rid2sd(st);
  9639. int i,calcflag=0;
  9640. if(sd==NULL)
  9641. return 0;
  9642. for(i=0;i<11;i++)
  9643. if(sd->equip_index[i] >= 0) {
  9644. if(!calcflag)
  9645. calcflag=1;
  9646. pc_unequipitem(sd,sd->equip_index[i],2);
  9647. }
  9648. if(calcflag)
  9649. status_calc_pc(sd,0);
  9650. return 0;
  9651. }
  9652. /*==========================================
  9653. * gmcommand [MouseJstr]
  9654. *
  9655. * suggested on the forums...
  9656. * splitted into atcommand & charcommand by [Skotlex]
  9657. *------------------------------------------*/
  9658. BUILDIN_FUNC(atcommand)
  9659. {
  9660. TBL_PC *sd=NULL;
  9661. const char *cmd;
  9662. cmd = script_getstr(st,2);
  9663. if (st->rid)
  9664. sd = script_rid2sd(st);
  9665. if (sd){
  9666. if(cmd[0] != atcommand_symbol){
  9667. cmd += strlen(sd->status.name);
  9668. while(*cmd != atcommand_symbol && *cmd != 0)
  9669. cmd++;
  9670. }
  9671. is_atcommand_sub(sd->fd, sd, cmd, 99);
  9672. } else { //Use a dummy character.
  9673. TBL_PC dummy_sd;
  9674. struct block_list *bl = NULL;
  9675. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9676. if (st->oid) bl = map_id2bl(st->oid);
  9677. if (bl) {
  9678. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9679. if (bl->type == BL_NPC)
  9680. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9681. }
  9682. if(cmd[0] != atcommand_symbol){
  9683. cmd += strlen(dummy_sd.status.name);
  9684. while(*cmd != atcommand_symbol && *cmd != 0)
  9685. cmd++;
  9686. }
  9687. is_atcommand_sub(0, &dummy_sd, cmd, 99);
  9688. }
  9689. return 0;
  9690. }
  9691. BUILDIN_FUNC(charcommand)
  9692. {
  9693. TBL_PC *sd=NULL;
  9694. const char *cmd;
  9695. cmd = script_getstr(st,2);
  9696. if (st->rid)
  9697. sd = script_rid2sd(st);
  9698. if (sd){
  9699. if(cmd[0] != charcommand_symbol){
  9700. cmd += strlen(sd->status.name);
  9701. while(*cmd != charcommand_symbol && *cmd != 0)
  9702. cmd++;
  9703. }
  9704. is_charcommand_sub(sd->fd, sd, cmd,99);
  9705. } else { //Use a dummy character.
  9706. TBL_PC dummy_sd;
  9707. struct block_list *bl = NULL;
  9708. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9709. if (st->oid) bl = map_id2bl(st->oid);
  9710. if (bl) {
  9711. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9712. if (bl->type == BL_NPC)
  9713. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9714. }
  9715. if(cmd[0] != charcommand_symbol){
  9716. cmd += strlen(dummy_sd.status.name);
  9717. while(*cmd != charcommand_symbol && *cmd != 0)
  9718. cmd++;
  9719. }
  9720. is_charcommand_sub(0, &dummy_sd, cmd, 99);
  9721. }
  9722. return 0;
  9723. }
  9724. /*==========================================
  9725. * Displays a message for the player only (like system messages like "you got an apple" )
  9726. *------------------------------------------*/
  9727. BUILDIN_FUNC(dispbottom)
  9728. {
  9729. TBL_PC *sd=script_rid2sd(st);
  9730. const char *message;
  9731. message=script_getstr(st,2);
  9732. if(sd)
  9733. clif_disp_onlyself(sd,message,(int)strlen(message));
  9734. return 0;
  9735. }
  9736. /*==========================================
  9737. * All The Players Full Recovery
  9738. * (HP/SP full restore and resurrect if need)
  9739. *------------------------------------------*/
  9740. BUILDIN_FUNC(recovery)
  9741. {
  9742. TBL_PC *sd, **all_sd;
  9743. int i = 0, users;
  9744. all_sd = map_getallusers(&users);
  9745. for (i = 0; i < users; i++)
  9746. {
  9747. sd = all_sd[i];
  9748. if(pc_isdead(sd))
  9749. status_revive(&sd->bl, 100, 100);
  9750. else
  9751. status_percent_heal(&sd->bl, 100, 100);
  9752. clif_displaymessage(sd->fd,"You have been recovered!");
  9753. }
  9754. return 0;
  9755. }
  9756. /*==========================================
  9757. * Get your pet info: getpetinfo(n)
  9758. * n -> 0:pet_id 1:pet_class 2:pet_name
  9759. * 3:friendly 4:hungry, 5: rename flag.
  9760. *------------------------------------------*/
  9761. BUILDIN_FUNC(getpetinfo)
  9762. {
  9763. TBL_PC *sd=script_rid2sd(st);
  9764. struct pet_data *pd;
  9765. int type=script_getnum(st,2);
  9766. if(sd && sd->status.pet_id && sd->pd){
  9767. pd = sd->pd;
  9768. switch(type){
  9769. case 0:
  9770. script_pushint(st,sd->status.pet_id);
  9771. break;
  9772. case 1:
  9773. script_pushint(st,pd->pet.class_);
  9774. break;
  9775. case 2:
  9776. if(pd->pet.name)
  9777. script_pushconststr(st,pd->pet.name);
  9778. else
  9779. script_pushconststr(st,"null");
  9780. break;
  9781. case 3:
  9782. script_pushint(st,pd->pet.intimate);
  9783. break;
  9784. case 4:
  9785. script_pushint(st,pd->pet.hungry);
  9786. break;
  9787. case 5:
  9788. script_pushint(st,pd->pet.rename_flag);
  9789. break;
  9790. default:
  9791. script_pushint(st,0);
  9792. break;
  9793. }
  9794. }else{
  9795. script_pushint(st,0);
  9796. }
  9797. return 0;
  9798. }
  9799. /*==========================================
  9800. * Shows wether your inventory(and equips) contain
  9801. selected card or not.
  9802. checkequipedcard(4001);
  9803. *------------------------------------------*/
  9804. BUILDIN_FUNC(checkequipedcard)
  9805. {
  9806. TBL_PC *sd=script_rid2sd(st);
  9807. int n,i,c=0;
  9808. c=script_getnum(st,2);
  9809. if(sd){
  9810. for(i=0;i<MAX_INVENTORY;i++){
  9811. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9812. for(n=0;n<MAX_SLOTS;n++){
  9813. if(sd->status.inventory[i].card[n]==c){
  9814. script_pushint(st,1);
  9815. return 0;
  9816. }
  9817. }
  9818. }
  9819. }
  9820. }
  9821. script_pushint(st,0);
  9822. return 0;
  9823. }
  9824. BUILDIN_FUNC(jump_zero)
  9825. {
  9826. int sel;
  9827. sel=script_getnum(st,2);
  9828. if(!sel) {
  9829. int pos;
  9830. if( !data_islabel(script_getdata(st,3)) ){
  9831. ShowError("script: jump_zero: not label !\n");
  9832. st->state=END;
  9833. return 1;
  9834. }
  9835. pos=script_getnum(st,3);
  9836. st->pos=pos;
  9837. st->state=GOTO;
  9838. // printf("script: jump_zero: jumpto : %d\n",pos);
  9839. } else {
  9840. // printf("script: jump_zero: fail\n");
  9841. }
  9842. return 0;
  9843. }
  9844. /*==========================================
  9845. * GetMapMobs
  9846. returns mob counts on a set map:
  9847. e.g. GetMapMobs("prontera")
  9848. use "this" - for player's map
  9849. *------------------------------------------*/
  9850. BUILDIN_FUNC(getmapmobs)
  9851. {
  9852. const char *str=NULL;
  9853. int m=-1,bx,by,i;
  9854. int count=0,c;
  9855. struct block_list *bl;
  9856. str=script_getstr(st,2);
  9857. if(strcmp(str,"this")==0){
  9858. TBL_PC *sd=script_rid2sd(st);
  9859. if(sd)
  9860. m=sd->bl.m;
  9861. else{
  9862. script_pushint(st,-1);
  9863. return 0;
  9864. }
  9865. }else
  9866. m=map_mapname2mapid(str);
  9867. if(m < 0){
  9868. script_pushint(st,-1);
  9869. return 0;
  9870. }
  9871. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++){
  9872. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++){
  9873. bl = map[m].block_mob[bx+by*map[m].bxs];
  9874. c = map[m].block_mob_count[bx+by*map[m].bxs];
  9875. for(i=0;i<c && bl;i++,bl=bl->next){
  9876. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9877. count++;
  9878. }
  9879. }
  9880. }
  9881. script_pushint(st,count);
  9882. return 0;
  9883. }
  9884. /*==========================================
  9885. * movenpc [MouseJstr]
  9886. *------------------------------------------*/
  9887. BUILDIN_FUNC(movenpc)
  9888. {
  9889. TBL_NPC *nd = NULL;
  9890. const char *npc;
  9891. int x,y;
  9892. npc = script_getstr(st,2);
  9893. x = script_getnum(st,3);
  9894. y = script_getnum(st,4);
  9895. if ((nd = npc_name2id(npc)) == NULL)
  9896. return -1;
  9897. npc_movenpc(nd, x, y);
  9898. return 0;
  9899. }
  9900. /*==========================================
  9901. * message [MouseJstr]
  9902. *------------------------------------------*/
  9903. BUILDIN_FUNC(message)
  9904. {
  9905. TBL_PC *sd;
  9906. const char *msg,*player;
  9907. TBL_PC *pl_sd = NULL;
  9908. sd = script_rid2sd(st);
  9909. player = script_getstr(st,2);
  9910. msg = script_getstr(st,3);
  9911. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9912. return 0;
  9913. clif_displaymessage(pl_sd->fd, msg);
  9914. return 0;
  9915. }
  9916. /*==========================================
  9917. * npctalk (sends message to surrounding area)
  9918. *------------------------------------------*/
  9919. BUILDIN_FUNC(npctalk)
  9920. {
  9921. const char *str;
  9922. char message[255];
  9923. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9924. str=script_getstr(st,2);
  9925. if(nd) {
  9926. memcpy(message, nd->name, NAME_LENGTH);
  9927. strcat(message," : ");
  9928. strncat(message,str, 254); //Prevent overflow possibility. [Skotlex]
  9929. clif_message(&(nd->bl), message);
  9930. }
  9931. return 0;
  9932. }
  9933. /*==========================================
  9934. * hasitems (checks to see if player has any
  9935. * items on them, if so will return a 1)
  9936. * [Valaris]
  9937. *------------------------------------------*/
  9938. BUILDIN_FUNC(hasitems)
  9939. {
  9940. int i;
  9941. TBL_PC *sd;
  9942. sd=script_rid2sd(st);
  9943. for(i=0; i<MAX_INVENTORY; i++) {
  9944. if(sd->status.inventory[i].amount && sd->status.inventory[i].nameid!=2364 && sd->status.inventory[i].nameid!=2365) {
  9945. script_pushint(st,1);
  9946. return 0;
  9947. }
  9948. }
  9949. script_pushint(st,0);
  9950. return 0;
  9951. }
  9952. // change npc walkspeed [Valaris]
  9953. BUILDIN_FUNC(npcspeed)
  9954. {
  9955. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9956. int x=0;
  9957. x=script_getnum(st,2);
  9958. if(nd) {
  9959. nd->speed=x;
  9960. }
  9961. return 0;
  9962. }
  9963. // make an npc walk to a position [Valaris]
  9964. BUILDIN_FUNC(npcwalkto)
  9965. {
  9966. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9967. int x=0,y=0;
  9968. x=script_getnum(st,2);
  9969. y=script_getnum(st,3);
  9970. if(nd) {
  9971. unit_walktoxy(&nd->bl,x,y,0);
  9972. }
  9973. return 0;
  9974. }
  9975. // stop an npc's movement [Valaris]
  9976. BUILDIN_FUNC(npcstop)
  9977. {
  9978. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9979. if(nd) {
  9980. unit_stop_walking(&nd->bl,1);
  9981. }
  9982. return 0;
  9983. }
  9984. /*==========================================
  9985. * getlook char info. getlook(arg)
  9986. *------------------------------------------*/
  9987. BUILDIN_FUNC(getlook)
  9988. {
  9989. int type,val;
  9990. TBL_PC *sd;
  9991. sd=script_rid2sd(st);
  9992. type=script_getnum(st,2);
  9993. val=-1;
  9994. switch(type){
  9995. case LOOK_HAIR: //1
  9996. val=sd->status.hair;
  9997. break;
  9998. case LOOK_WEAPON: //2
  9999. val=sd->status.weapon;
  10000. break;
  10001. case LOOK_HEAD_BOTTOM: //3
  10002. val=sd->status.head_bottom;
  10003. break;
  10004. case LOOK_HEAD_TOP: //4
  10005. val=sd->status.head_top;
  10006. break;
  10007. case LOOK_HEAD_MID: //5
  10008. val=sd->status.head_mid;
  10009. break;
  10010. case LOOK_HAIR_COLOR: //6
  10011. val=sd->status.hair_color;
  10012. break;
  10013. case LOOK_CLOTHES_COLOR: //7
  10014. val=sd->status.clothes_color;
  10015. break;
  10016. case LOOK_SHIELD: //8
  10017. val=sd->status.shield;
  10018. break;
  10019. case LOOK_SHOES: //9
  10020. break;
  10021. }
  10022. script_pushint(st,val);
  10023. return 0;
  10024. }
  10025. /*==========================================
  10026. * get char save point. argument: 0- map name, 1- x, 2- y
  10027. *------------------------------------------*/
  10028. BUILDIN_FUNC(getsavepoint)
  10029. {
  10030. int x,y,type;
  10031. char *mapname;
  10032. TBL_PC *sd;
  10033. sd=script_rid2sd(st);
  10034. type=script_getnum(st,2);
  10035. x=sd->status.save_point.x;
  10036. y=sd->status.save_point.y;
  10037. switch(type){
  10038. case 0:
  10039. mapname=(char *) aMallocA((MAP_NAME_LENGTH)*sizeof(char));
  10040. memcpy(mapname, mapindex_id2name(sd->status.save_point.map), MAP_NAME_LENGTH);
  10041. mapname[MAP_NAME_LENGTH-1]='\0';
  10042. script_pushstr(st,mapname);
  10043. break;
  10044. case 1:
  10045. script_pushint(st,x);
  10046. break;
  10047. case 2:
  10048. script_pushint(st,y);
  10049. break;
  10050. default:
  10051. script_pushint(st,0);
  10052. break;
  10053. }
  10054. return 0;
  10055. }
  10056. /*==========================================
  10057. * Get position for char/npc/pet/mob objects. Added by Lorky
  10058. *
  10059. * int getMapXY(MapName$,MaxX,MapY,type,[CharName$]);
  10060. * where type:
  10061. * MapName$ - String variable for output map name
  10062. * MapX - Integer variable for output coord X
  10063. * MapY - Integer variable for output coord Y
  10064. * type - type of object
  10065. * 0 - Character coord
  10066. * 1 - NPC coord
  10067. * 2 - Pet coord
  10068. * 3 - Mob coord (not released)
  10069. * 4 - Homun coord
  10070. * CharName$ - Name object. If miss or "this" the current object
  10071. *
  10072. * Return:
  10073. * 0 - success
  10074. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10075. *------------------------------------------*/
  10076. BUILDIN_FUNC(getmapxy)
  10077. {
  10078. struct block_list *bl = NULL;
  10079. TBL_PC *sd=NULL;
  10080. int num;
  10081. char *name;
  10082. char prefix;
  10083. int x,y,type;
  10084. char mapname[MAP_NAME_LENGTH];
  10085. memset(mapname, 0, sizeof(mapname));
  10086. if( !data_isreference(script_getdata(st,2)) ){
  10087. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10088. script_pushint(st,-1);
  10089. return 1;
  10090. }
  10091. if( !data_isreference(script_getdata(st,3)) ){
  10092. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10093. script_pushint(st,-1);
  10094. return 1;
  10095. }
  10096. if( !data_isreference(script_getdata(st,4)) ){
  10097. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10098. script_pushint(st,-1);
  10099. return 1;
  10100. }
  10101. //??????????? >>> Possible needly check function parameters on C_STR,C_INT,C_INT <<< ???????????//
  10102. type=script_getnum(st,5);
  10103. switch (type){
  10104. case 0: //Get Character Position
  10105. if( script_hasdata(st,6) )
  10106. sd=map_nick2sd(script_getstr(st,6));
  10107. else
  10108. sd=script_rid2sd(st);
  10109. if (sd)
  10110. bl = &sd->bl;
  10111. break;
  10112. case 1: //Get NPC Position
  10113. if( script_hasdata(st,6) )
  10114. {
  10115. struct npc_data *nd;
  10116. nd=npc_name2id(script_getstr(st,6));
  10117. if (nd)
  10118. bl = &nd->bl;
  10119. } else //In case the origin is not an npc?
  10120. bl=map_id2bl(st->oid);
  10121. break;
  10122. case 2: //Get Pet Position
  10123. if(script_hasdata(st,6))
  10124. sd=map_nick2sd(script_getstr(st,6));
  10125. else
  10126. sd=script_rid2sd(st);
  10127. if (sd && sd->pd)
  10128. bl = &sd->pd->bl;
  10129. break;
  10130. case 3: //Get Mob Position
  10131. break; //Not supported?
  10132. case 4: //Get Homun Position
  10133. if(script_hasdata(st,6))
  10134. sd=map_nick2sd(script_getstr(st,6));
  10135. else
  10136. sd=script_rid2sd(st);
  10137. if (sd && sd->hd)
  10138. bl = &sd->hd->bl;
  10139. break;
  10140. }
  10141. if (!bl) { //No object found.
  10142. script_pushint(st,-1);
  10143. return 0;
  10144. }
  10145. x= bl->x;
  10146. y= bl->y;
  10147. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10148. //Set MapName$
  10149. num=st->stack->stack_data[st->start+2].u.num;
  10150. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10151. prefix=*name;
  10152. if(not_server_variable(prefix))
  10153. sd=script_rid2sd(st);
  10154. else
  10155. sd=NULL;
  10156. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10157. //Set MapX
  10158. num=st->stack->stack_data[st->start+3].u.num;
  10159. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10160. prefix=*name;
  10161. if(not_server_variable(prefix))
  10162. sd=script_rid2sd(st);
  10163. else
  10164. sd=NULL;
  10165. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10166. //Set MapY
  10167. num=st->stack->stack_data[st->start+4].u.num;
  10168. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10169. prefix=*name;
  10170. if(not_server_variable(prefix))
  10171. sd=script_rid2sd(st);
  10172. else
  10173. sd=NULL;
  10174. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10175. //Return Success value
  10176. script_pushint(st,0);
  10177. return 0;
  10178. }
  10179. /*==========================================
  10180. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10181. *------------------------------------------*/
  10182. BUILDIN_FUNC(logmes)
  10183. {
  10184. const char *str;
  10185. if (log_config.npc <= 0 ) return 0;
  10186. str = script_getstr(st,2);
  10187. log_npc(script_rid2sd(st),str);
  10188. return 0;
  10189. }
  10190. BUILDIN_FUNC(summon)
  10191. {
  10192. int _class, timeout=0;
  10193. const char *str,*event="";
  10194. TBL_PC *sd;
  10195. struct mob_data *md;
  10196. int tick = gettick();
  10197. sd=script_rid2sd(st);
  10198. if (!sd) return 0;
  10199. str =script_getstr(st,2);
  10200. _class=script_getnum(st,3);
  10201. if( script_hasdata(st,4) )
  10202. timeout=script_getnum(st,4);
  10203. if( script_hasdata(st,5) ){
  10204. event=script_getstr(st,5);
  10205. check_event(st, event);
  10206. }
  10207. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10208. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10209. if (md) {
  10210. md->master_id=sd->bl.id;
  10211. md->special_state.ai=1;
  10212. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10213. mob_spawn (md); //Now it is ready for spawning.
  10214. clif_misceffect2(&md->bl,344);
  10215. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10216. }
  10217. return 0;
  10218. }
  10219. /*==========================================
  10220. * Checks whether it is daytime/nighttime
  10221. *------------------------------------------*/
  10222. BUILDIN_FUNC(isnight)
  10223. {
  10224. script_pushint(st,(night_flag == 1));
  10225. return 0;
  10226. }
  10227. BUILDIN_FUNC(isday)
  10228. {
  10229. script_pushint(st,(night_flag == 0));
  10230. return 0;
  10231. }
  10232. /*================================================
  10233. * Check whether another item/card has been
  10234. * equipped - used for 2/15's cards patch [celest]
  10235. *------------------------------------------------*/
  10236. // leave this here, just in case
  10237. #if 0
  10238. BUILDIN_FUNC(isequipped)
  10239. {
  10240. TBL_PC *sd;
  10241. int i, j, k, id = 1;
  10242. int ret = -1;
  10243. sd = script_rid2sd(st);
  10244. for (i=0; id!=0; i++) {
  10245. int flag = 0;
  10246. FETCH (i+2, id) else id = 0;
  10247. if (id <= 0)
  10248. continue;
  10249. for (j=0; j<EQI_MAX; j++) {
  10250. int index;
  10251. index = sd->equip_index[j];
  10252. if(index < 0) continue;
  10253. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10254. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10255. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10256. if(!sd->inventory_data[index])
  10257. continue;
  10258. if(itemdb_type(id) != IT_CARD) { //Non card
  10259. if (sd->inventory_data[index]->nameid != id)
  10260. continue;
  10261. flag = 1;
  10262. break;
  10263. } else { //Card
  10264. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10265. continue;
  10266. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10267. if (sd->status.inventory[index].card[k] == id)
  10268. {
  10269. flag = 1;
  10270. break;
  10271. }
  10272. }
  10273. }
  10274. if (flag) break;
  10275. }
  10276. if (ret == -1)
  10277. ret = flag;
  10278. else
  10279. ret &= flag;
  10280. if (!ret) break;
  10281. }
  10282. script_pushint(st,ret);
  10283. return 0;
  10284. }
  10285. #endif
  10286. /*================================================
  10287. * Check how many items/cards in the list are
  10288. * equipped - used for 2/15's cards patch [celest]
  10289. *------------------------------------------------*/
  10290. BUILDIN_FUNC(isequippedcnt)
  10291. {
  10292. TBL_PC *sd;
  10293. int i, j, k, id = 1;
  10294. int ret = 0;
  10295. sd = script_rid2sd(st);
  10296. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10297. script_pushint(st,0);
  10298. return 0;
  10299. }
  10300. for (i=0; id!=0; i++) {
  10301. FETCH (i+2, id) else id = 0;
  10302. if (id <= 0)
  10303. continue;
  10304. for (j=0; j<EQI_MAX; j++) {
  10305. int index;
  10306. index = sd->equip_index[j];
  10307. if(index < 0) continue;
  10308. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10309. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10310. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10311. if(!sd->inventory_data[index])
  10312. continue;
  10313. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10314. if (sd->inventory_data[index]->nameid == id)
  10315. ret+= sd->status.inventory[index].amount;
  10316. } else { //Count cards.
  10317. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10318. continue; //No cards
  10319. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10320. if (sd->status.inventory[index].card[k] == id)
  10321. ret++; //[Lupus]
  10322. }
  10323. }
  10324. }
  10325. }
  10326. script_pushint(st,ret);
  10327. return 0;
  10328. }
  10329. /*================================================
  10330. * Check whether another card has been
  10331. * equipped - used for 2/15's cards patch [celest]
  10332. * -- Items checked cannot be reused in another
  10333. * card set to prevent exploits
  10334. *------------------------------------------------*/
  10335. BUILDIN_FUNC(isequipped)
  10336. {
  10337. TBL_PC *sd;
  10338. int i, j, k, id = 1;
  10339. int index, flag;
  10340. int ret = -1;
  10341. //Original hash to reverse it when full check fails.
  10342. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10343. sd = script_rid2sd(st);
  10344. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10345. script_pushint(st,0);
  10346. return 0;
  10347. }
  10348. setitem_hash = sd->setitem_hash;
  10349. setitem_hash2 = sd->setitem_hash2;
  10350. for (i=0; id!=0; i++)
  10351. {
  10352. FETCH (i+2, id) else id = 0;
  10353. if (id <= 0)
  10354. continue;
  10355. flag = 0;
  10356. for (j=0; j<EQI_MAX; j++)
  10357. {
  10358. index = sd->equip_index[j];
  10359. if(index < 0) continue;
  10360. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10361. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10362. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10363. if(!sd->inventory_data[index])
  10364. continue;
  10365. if (itemdb_type(id) != IT_CARD) {
  10366. if (sd->inventory_data[index]->nameid != id)
  10367. continue;
  10368. flag = 1;
  10369. break;
  10370. } else { //Cards
  10371. if (sd->inventory_data[index]->slot == 0 ||
  10372. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10373. continue;
  10374. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10375. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10376. unsigned int hash = 0;
  10377. if (sd->status.inventory[index].card[k] != id)
  10378. continue;
  10379. hash = 1<<((j<5?j:j-5)*4 + k);
  10380. // check if card is already used by another set
  10381. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10382. continue;
  10383. // We have found a match
  10384. flag = 1;
  10385. // Set hash so this card cannot be used by another
  10386. if (j<5)
  10387. sd->setitem_hash |= hash;
  10388. else
  10389. sd->setitem_hash2 |= hash;
  10390. break;
  10391. }
  10392. }
  10393. if (flag) break; //Card found
  10394. }
  10395. if (ret == -1)
  10396. ret = flag;
  10397. else
  10398. ret &= flag;
  10399. if (!ret) break;
  10400. }
  10401. if (!ret)
  10402. { //When check fails, restore original hash values. [Skotlex]
  10403. sd->setitem_hash = setitem_hash;
  10404. sd->setitem_hash2 = setitem_hash2;
  10405. }
  10406. script_pushint(st,ret);
  10407. return 0;
  10408. }
  10409. /*================================================
  10410. * Check how many given inserted cards in the CURRENT
  10411. * weapon - used for 2/15's cards patch [Lupus]
  10412. *------------------------------------------------*/
  10413. BUILDIN_FUNC(cardscnt)
  10414. {
  10415. TBL_PC *sd;
  10416. int i, k, id = 1;
  10417. int ret = 0;
  10418. int index;
  10419. sd = script_rid2sd(st);
  10420. for (i=0; id!=0; i++) {
  10421. FETCH (i+2, id) else id = 0;
  10422. if (id <= 0)
  10423. continue;
  10424. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10425. if(index < 0) continue;
  10426. if(!sd->inventory_data[index])
  10427. continue;
  10428. if(itemdb_type(id) != IT_CARD) {
  10429. if (sd->inventory_data[index]->nameid == id)
  10430. ret+= sd->status.inventory[index].amount;
  10431. } else {
  10432. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10433. continue;
  10434. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10435. if (sd->status.inventory[index].card[k] == id)
  10436. ret++;
  10437. }
  10438. }
  10439. }
  10440. script_pushint(st,ret);
  10441. // script_pushint(st,current_equip_item_index);
  10442. return 0;
  10443. }
  10444. /*=======================================================
  10445. * Returns the refined number of the current item, or an
  10446. * item with inventory index specified
  10447. *-------------------------------------------------------*/
  10448. BUILDIN_FUNC(getrefine)
  10449. {
  10450. TBL_PC *sd;
  10451. if ((sd = script_rid2sd(st))!= NULL)
  10452. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10453. else
  10454. script_pushint(st,0);
  10455. return 0;
  10456. }
  10457. /*=======================================================
  10458. * Allows 2 Parents to adopt a character as a Baby
  10459. *-------------------------------------------------------*/
  10460. BUILDIN_FUNC(adopt)
  10461. {
  10462. int ret;
  10463. const char *parent1 = script_getstr(st,2);
  10464. const char *parent2 = script_getstr(st,3);
  10465. const char *child = script_getstr(st,4);
  10466. TBL_PC *p1_sd = map_nick2sd(parent1);
  10467. TBL_PC *p2_sd = map_nick2sd(parent2);
  10468. TBL_PC *c_sd = map_nick2sd(child);
  10469. if (!p1_sd || !p2_sd || !c_sd ||
  10470. p1_sd->status.base_level < 70 ||
  10471. p2_sd->status.base_level < 70)
  10472. return 0;
  10473. ret = pc_adoption(p1_sd, p2_sd, c_sd);
  10474. script_pushint(st,ret);
  10475. return 0;
  10476. }
  10477. /*=======================================================
  10478. * Day/Night controls
  10479. *-------------------------------------------------------*/
  10480. BUILDIN_FUNC(night)
  10481. {
  10482. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10483. return 0;
  10484. }
  10485. BUILDIN_FUNC(day)
  10486. {
  10487. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10488. return 0;
  10489. }
  10490. //=======================================================
  10491. // Unequip [Spectre]
  10492. //-------------------------------------------------------
  10493. BUILDIN_FUNC(unequip)
  10494. {
  10495. int i;
  10496. size_t num;
  10497. TBL_PC *sd;
  10498. num = script_getnum(st,2) - 1;
  10499. sd=script_rid2sd(st);
  10500. if(sd!=NULL && num<10)
  10501. {
  10502. i=pc_checkequip(sd,equip[num]);
  10503. pc_unequipitem(sd,i,2);
  10504. return 0;
  10505. }
  10506. return 0;
  10507. }
  10508. BUILDIN_FUNC(equip)
  10509. {
  10510. int nameid=0,i;
  10511. TBL_PC *sd;
  10512. struct item_data *item_data;
  10513. sd = script_rid2sd(st);
  10514. nameid=script_getnum(st,2);
  10515. if((item_data = itemdb_exists(nameid)) == NULL)
  10516. {
  10517. if(battle_config.error_log)
  10518. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10519. return 1;
  10520. }
  10521. for(i=0;i<MAX_INVENTORY && sd->status.inventory[i].nameid!=nameid;i++);
  10522. if(i==MAX_INVENTORY) return 0;
  10523. pc_equipitem(sd,i,item_data->equip);
  10524. return 0;
  10525. }
  10526. BUILDIN_FUNC(autoequip)
  10527. {
  10528. int nameid, flag;
  10529. struct item_data *item_data;
  10530. nameid=script_getnum(st,2);
  10531. flag=script_getnum(st,3);
  10532. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10533. item_data->flag.autoequip = flag>0?1:0;
  10534. }
  10535. return 0;
  10536. }
  10537. BUILDIN_FUNC(setbattleflag)
  10538. {
  10539. const char *flag, *value;
  10540. flag = script_getstr(st,2);
  10541. value = script_getstr(st,3);
  10542. if (battle_set_value(flag, value) == 0)
  10543. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10544. else
  10545. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10546. return 0;
  10547. }
  10548. BUILDIN_FUNC(getbattleflag)
  10549. {
  10550. const char *flag;
  10551. flag = script_getstr(st,2);
  10552. script_pushint(st,battle_get_value(flag));
  10553. return 0;
  10554. }
  10555. //=======================================================
  10556. // strlen [Valaris]
  10557. //-------------------------------------------------------
  10558. BUILDIN_FUNC(getstrlen)
  10559. {
  10560. const char *str = script_getstr(st,2);
  10561. int len = (str) ? (int)strlen(str) : 0;
  10562. script_pushint(st,len);
  10563. return 0;
  10564. }
  10565. //=======================================================
  10566. // isalpha [Valaris]
  10567. //-------------------------------------------------------
  10568. BUILDIN_FUNC(charisalpha)
  10569. {
  10570. const char *str=script_getstr(st,2);
  10571. int pos=script_getnum(st,3);
  10572. int val = ( str && pos>0 && (unsigned int)pos<strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10573. script_pushint(st,val);
  10574. return 0;
  10575. }
  10576. // [Lance]
  10577. BUILDIN_FUNC(fakenpcname)
  10578. {
  10579. const char *name;
  10580. const char *newname;
  10581. int look;
  10582. name = script_getstr(st,2);
  10583. newname = script_getstr(st,3);
  10584. look = script_getnum(st,4);
  10585. if(look > 32767 || look < -32768) {
  10586. ShowError("buildin_fakenpcname: Invalid look value %d\n",look);
  10587. return 1; // Safety measure to prevent runtime errors
  10588. }
  10589. npc_changename(name,newname,(short)look);
  10590. return 0;
  10591. }
  10592. BUILDIN_FUNC(atoi)
  10593. {
  10594. const char *value;
  10595. value = script_getstr(st,2);
  10596. script_pushint(st,atoi(value));
  10597. return 0;
  10598. }
  10599. //-----------------------------------------------------------------------//
  10600. // BRING STRSTR TO SCRIPTING ENGINE - LORDALFA START //
  10601. //-----------------------------------------------------------------------//
  10602. BUILDIN_FUNC(compare)
  10603. {
  10604. const char *message;
  10605. const char *cmpstring;
  10606. message = script_getstr(st,2);
  10607. cmpstring = script_getstr(st,3);
  10608. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10609. return 0;
  10610. }
  10611. //-----------------------------------------------------------------------//
  10612. // BRING STRSTR TO SCRIPTING ENGINE - LORDALFA END //
  10613. //-----------------------------------------------------------------------//
  10614. // [zBuffer] List of mathematics commands --->
  10615. BUILDIN_FUNC(sqrt)
  10616. {
  10617. double i, a;
  10618. i = script_getnum(st,2);
  10619. a = sqrt(i);
  10620. script_pushint(st,(int)a);
  10621. return 0;
  10622. }
  10623. BUILDIN_FUNC(pow)
  10624. {
  10625. double i, a, b;
  10626. a = script_getnum(st,2);
  10627. b = script_getnum(st,3);
  10628. i = pow(a,b);
  10629. script_pushint(st,(int)i);
  10630. return 0;
  10631. }
  10632. BUILDIN_FUNC(distance)
  10633. {
  10634. int x0, y0, x1, y1;
  10635. x0 = script_getnum(st,2);
  10636. y0 = script_getnum(st,3);
  10637. x1 = script_getnum(st,4);
  10638. y1 = script_getnum(st,5);
  10639. script_pushint(st,distance(x0-x1, y0-y1));
  10640. return 0;
  10641. }
  10642. BUILDIN_FUNC(checkcell)
  10643. {
  10644. int m;
  10645. const char *map = script_getstr(st, 2);
  10646. m = mapindex_name2id(map);
  10647. if(m){
  10648. script_pushint(st,map_getcell(m, script_getnum(st,3), script_getnum(st,4),script_getnum(st,5)));
  10649. } else {
  10650. script_pushint(st,0);
  10651. }
  10652. return 0;
  10653. }
  10654. // <--- [zBuffer] List of mathematics commands
  10655. // [zBuffer] List of dynamic var commands --->
  10656. void setd_sub(struct script_state *st, TBL_PC *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10657. {
  10658. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10659. return;
  10660. }
  10661. BUILDIN_FUNC(setd)
  10662. {
  10663. TBL_PC *sd=NULL;
  10664. char varname[100];
  10665. const char *value, *buffer;
  10666. int elem;
  10667. buffer = script_getstr(st, 2);
  10668. value = script_getstr(st, 3);
  10669. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10670. elem = 0;
  10671. if(st->rid)
  10672. sd = script_rid2sd(st);
  10673. if(varname[strlen(varname)-1] != '$') {
  10674. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10675. } else {
  10676. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10677. }
  10678. return 0;
  10679. }
  10680. BUILDIN_FUNC(query_sql)
  10681. {
  10682. #ifndef TXT_ONLY
  10683. char *name = NULL;
  10684. const char *query;
  10685. int num, i = 0,j, nb_rows;
  10686. struct { char * dst_var_name; char type; } row[32];
  10687. TBL_PC *sd = (st->rid)? script_rid2sd(st) : NULL;
  10688. query = script_getstr(st,2);
  10689. strcpy(tmp_sql, query);
  10690. if(mysql_query(&mmysql_handle,tmp_sql)){
  10691. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  10692. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  10693. script_pushint(st,0);
  10694. return 1;
  10695. }
  10696. // If some data was returned
  10697. if((sql_res = mysql_store_result(&mmysql_handle))){
  10698. // Count the number of rows to store
  10699. nb_rows = mysql_num_fields(sql_res);
  10700. // Can't store more row than variable
  10701. if (nb_rows > st->end - (st->start+3))
  10702. nb_rows = st->end - (st->start+3);
  10703. if (!nb_rows)
  10704. {
  10705. script_pushint(st,0);
  10706. return 0; // Nothing to store
  10707. }
  10708. if (nb_rows > 32)
  10709. {
  10710. ShowWarning("buildin_query_sql: too many rows!\n");
  10711. script_pushint(st,0);
  10712. return 1;
  10713. }
  10714. memset(row, 0, sizeof(row));
  10715. // Verify argument types
  10716. for(j=0; j < nb_rows; j++)
  10717. {
  10718. if(!data_isreference(script_getdata(st, 3+j))){
  10719. ShowWarning("buildin_query_sql: Parameter %d is not a variable!\n", j);
  10720. script_pushint(st,0);
  10721. return 0;
  10722. } else {
  10723. // Store type of variable (string = 0/int = 1)
  10724. num=st->stack->stack_data[st->start+3+j].u.num;
  10725. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10726. if(name[strlen(name)-1] != '$') {
  10727. row[j].type = 1;
  10728. }
  10729. row[j].dst_var_name = name;
  10730. }
  10731. }
  10732. // Store data
  10733. while(i<128 && (sql_row = mysql_fetch_row(sql_res))){
  10734. for(j=0; j < nb_rows; j++)
  10735. {
  10736. if (row[j].type == 1)
  10737. setd_sub(st,sd, row[j].dst_var_name, i, (void *)atoi(sql_row[j]),script_getref(st,3+j));
  10738. else
  10739. setd_sub(st,sd, row[j].dst_var_name, i, (void *)sql_row[j],script_getref(st,3+j));
  10740. }
  10741. i++;
  10742. }
  10743. // Free data
  10744. mysql_free_result(sql_res);
  10745. }
  10746. script_pushint(st,i);
  10747. #else
  10748. //for TXT version, we always return -1
  10749. script_pushint(st,-1);
  10750. #endif
  10751. return 0;
  10752. }
  10753. //Allows escaping of a given string.
  10754. BUILDIN_FUNC(escape_sql)
  10755. {
  10756. const char *query;
  10757. char *t_query;
  10758. query = script_getstr(st,2);
  10759. t_query = aMallocA((strlen(query)*2+1)*sizeof(char));
  10760. jstrescapecpy(t_query,query);
  10761. script_pushstr(st,t_query);
  10762. return 0;
  10763. }
  10764. BUILDIN_FUNC(getd)
  10765. {
  10766. char varname[100];
  10767. const char *buffer;
  10768. //struct script_data dat;
  10769. int elem;
  10770. buffer = script_getstr(st, 2);
  10771. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10772. elem = 0;
  10773. // Push the 'pointer' so it's more flexible [Lance]
  10774. push_val(st->stack,C_NAME,
  10775. (elem<<24) | add_str(varname));
  10776. return 0;
  10777. }
  10778. // <--- [zBuffer] List of dynamic var commands
  10779. // Pet stat [Lance]
  10780. BUILDIN_FUNC(petstat)
  10781. {
  10782. TBL_PC *sd = NULL;
  10783. struct pet_data *pd;
  10784. char *tmp;
  10785. int flag = script_getnum(st,2);
  10786. sd = script_rid2sd(st);
  10787. if(!sd || !sd->status.pet_id || !sd->pd){
  10788. if(flag == 2)
  10789. push_str(st->stack, C_CONSTSTR, "");
  10790. else
  10791. script_pushint(st,0);
  10792. return 0;
  10793. }
  10794. pd = sd->pd;
  10795. switch(flag){
  10796. case 1:
  10797. script_pushint(st,(int)pd->pet.class_);
  10798. break;
  10799. case 2:
  10800. tmp = aStrdup(pd->pet.name);
  10801. push_str(st->stack, C_STR, tmp);
  10802. break;
  10803. case 3:
  10804. script_pushint(st,(int)pd->pet.level);
  10805. break;
  10806. case 4:
  10807. script_pushint(st,(int)pd->pet.hungry);
  10808. break;
  10809. case 5:
  10810. script_pushint(st,(int)pd->pet.intimate);
  10811. break;
  10812. default:
  10813. script_pushint(st,0);
  10814. break;
  10815. }
  10816. return 0;
  10817. }
  10818. BUILDIN_FUNC(callshop)
  10819. {
  10820. TBL_PC *sd = NULL;
  10821. struct npc_data *nd;
  10822. const char *shopname;
  10823. int flag = 0;
  10824. sd = script_rid2sd(st);
  10825. if (!sd) {
  10826. script_pushint(st,0);
  10827. return 0;
  10828. }
  10829. shopname = script_getstr(st, 2);
  10830. if( script_hasdata(st,3) )
  10831. flag = script_getnum(st,3);
  10832. nd = npc_name2id(shopname);
  10833. if (!nd || nd->bl.type!=BL_NPC || nd->bl.subtype!=SHOP) {
  10834. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)", shopname);
  10835. script_pushint(st,0);
  10836. return 1;
  10837. }
  10838. switch (flag) {
  10839. case 1: //Buy window
  10840. npc_buysellsel(sd,nd->bl.id,0);
  10841. break;
  10842. case 2: //Sell window
  10843. npc_buysellsel(sd,nd->bl.id,1);
  10844. break;
  10845. default: //Show menu
  10846. clif_npcbuysell(sd,nd->bl.id);
  10847. break;
  10848. }
  10849. sd->npc_shopid = nd->bl.id;
  10850. script_pushint(st,1);
  10851. return 0;
  10852. }
  10853. #ifndef MAX_SHOPITEM
  10854. #define MAX_SHOPITEM 100
  10855. #endif
  10856. BUILDIN_FUNC(npcshopitem)
  10857. {
  10858. struct npc_data *nd= NULL;
  10859. int n = 0;
  10860. int i = 3;
  10861. int amount;
  10862. const char* npcname = script_getstr(st, 2);
  10863. nd = npc_name2id(npcname);
  10864. if(!nd || nd->bl.subtype!=SHOP)
  10865. { //Not found.
  10866. script_pushint(st,0);
  10867. return 0;
  10868. }
  10869. // st->end - 2 = nameid + value # ... / 2 = number of items ... + 1 to end it
  10870. amount = ((st->end-2)/2)+1;
  10871. //Reinsert as realloc could change the pointer address.
  10872. map_deliddb(&nd->bl);
  10873. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10874. sizeof(nd->u.shop_item[0]) * amount);
  10875. map_addiddb(&nd->bl);
  10876. // Reset sell list.
  10877. memset(nd->u.shop_item, 0, sizeof(nd->u.shop_item[0]) * amount);
  10878. n = 0;
  10879. while (script_hasdata(st,i)) {
  10880. nd->u.shop_item[n].nameid = script_getnum(st,i);
  10881. i++;
  10882. nd->u.shop_item[n].value = script_getnum(st,i);
  10883. i++;
  10884. n++;
  10885. }
  10886. return 0;
  10887. }
  10888. BUILDIN_FUNC(npcshopadditem)
  10889. {
  10890. struct npc_data *nd=NULL;
  10891. int n = 0;
  10892. int i = 3;
  10893. int amount;
  10894. const char* npcname = script_getstr(st, 2);
  10895. nd = npc_name2id(npcname);
  10896. if (!nd || nd->bl.subtype!=SHOP)
  10897. { //Not found.
  10898. script_pushint(st,0);
  10899. return 0;
  10900. }
  10901. amount = ((st->end-2)/2)+1;
  10902. while (nd->u.shop_item[n].nameid && n < MAX_SHOPITEM)
  10903. n++;
  10904. //Reinsert as realloc could change the pointer address.
  10905. map_deliddb(&nd->bl);
  10906. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10907. sizeof(nd->u.shop_item[0]) * (amount+n));
  10908. map_addiddb(&nd->bl);
  10909. while(script_hasdata(st,i)){
  10910. nd->u.shop_item[n].nameid = script_getnum(st,i);
  10911. i++;
  10912. nd->u.shop_item[n].value = script_getnum(st,i);
  10913. i++;
  10914. n++;
  10915. }
  10916. // Marks the last of our stuff..
  10917. nd->u.shop_item[n].value = 0;
  10918. nd->u.shop_item[n].nameid = 0;
  10919. script_pushint(st,1);
  10920. return 0;
  10921. }
  10922. BUILDIN_FUNC(npcshopdelitem)
  10923. {
  10924. struct npc_data *nd=NULL;
  10925. int n=0;
  10926. int i=3;
  10927. int size = 0;
  10928. const char* npcname = script_getstr(st, 2);
  10929. nd = npc_name2id(npcname);
  10930. if (!nd || nd->bl.subtype!=SHOP)
  10931. { //Not found.
  10932. script_pushint(st,0);
  10933. return 0;
  10934. }
  10935. while (nd->u.shop_item[size].nameid)
  10936. size++;
  10937. while (script_hasdata(st,i)) {
  10938. for(n=0;nd->u.shop_item[n].nameid && n < MAX_SHOPITEM;n++) {
  10939. if (nd->u.shop_item[n].nameid == script_getnum(st,i)) {
  10940. // We're moving 1 extra empty block. Junk data is eliminated later.
  10941. memmove(&nd->u.shop_item[n], &nd->u.shop_item[n+1], sizeof(nd->u.shop_item[0])*(size-n));
  10942. }
  10943. }
  10944. i++;
  10945. }
  10946. size = 0;
  10947. while (nd->u.shop_item[size].nameid)
  10948. size++;
  10949. //Reinsert as realloc could change the pointer address.
  10950. map_deliddb(&nd->bl);
  10951. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10952. sizeof(nd->u.shop_item[0]) * (size+1));
  10953. map_addiddb(&nd->bl);
  10954. script_pushint(st,1);
  10955. return 0;
  10956. }
  10957. //Sets a script to attach to an npc.
  10958. BUILDIN_FUNC(npcshopattach)
  10959. {
  10960. struct npc_data *nd=NULL;
  10961. const char* npcname = script_getstr(st, 2);
  10962. int flag = 1;
  10963. if( script_hasdata(st,3) )
  10964. flag = script_getnum(st,3);
  10965. nd = npc_name2id(npcname);
  10966. if (!nd || nd->bl.subtype!=SHOP)
  10967. { //Not found.
  10968. script_pushint(st,0);
  10969. return 0;
  10970. }
  10971. if (flag)
  10972. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10973. else
  10974. nd->master_nd = NULL;
  10975. script_pushint(st,1);
  10976. return 0;
  10977. }
  10978. /*==========================================
  10979. * Returns some values of an item [Lupus]
  10980. * Price, Weight, etc...
  10981. setitemscript(itemID,"{new item bonus script}",[n]);
  10982. Where n:
  10983. 0 - script
  10984. 1 - Equip script
  10985. 2 - Unequip script
  10986. *------------------------------------------*/
  10987. BUILDIN_FUNC(setitemscript)
  10988. {
  10989. int item_id,n=0;
  10990. const char *script;
  10991. struct item_data *i_data;
  10992. struct script_code **dstscript;
  10993. item_id = script_getnum(st,2);
  10994. script = script_getstr(st,3);
  10995. if( script_hasdata(st,4) )
  10996. n=script_getnum(st,4);
  10997. i_data = itemdb_exists(item_id);
  10998. if (!i_data || script==NULL || script[0]!='{') {
  10999. script_pushint(st,0);
  11000. return 0;
  11001. }
  11002. switch (n) {
  11003. case 2:
  11004. dstscript = &i_data->unequip_script;
  11005. break;
  11006. case 1:
  11007. dstscript = &i_data->equip_script;
  11008. break;
  11009. default:
  11010. dstscript = &i_data->script;
  11011. break;
  11012. }
  11013. if(*dstscript)
  11014. script_free_code(*dstscript);
  11015. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11016. script_pushint(st,1);
  11017. return 0;
  11018. }
  11019. /* Work In Progress [Lupus]
  11020. BUILDIN_FUNC(addmonsterdrop)
  11021. {
  11022. int class_,item_id,chance;
  11023. class_=script_getnum(st,2);
  11024. item_id=script_getnum(st,3);
  11025. chance=script_getnum(st,4);
  11026. if(class_>1000 && item_id>500 && chance>0) {
  11027. script_pushint(st,1);
  11028. } else {
  11029. script_pushint(st,0);
  11030. }
  11031. }
  11032. BUILDIN_FUNC(delmonsterdrop)
  11033. {
  11034. int class_,item_id;
  11035. class_=script_getnum(st,2);
  11036. item_id=script_getnum(st,3);
  11037. if(class_>1000 && item_id>500) {
  11038. script_pushint(st,1);
  11039. } else {
  11040. script_pushint(st,0);
  11041. }
  11042. }
  11043. */
  11044. /*==========================================
  11045. * Returns some values of a monster [Lupus]
  11046. * Name, Level, race, size, etc...
  11047. getmonsterinfo(monsterID,queryIndex);
  11048. *------------------------------------------*/
  11049. BUILDIN_FUNC(getmonsterinfo)
  11050. {
  11051. struct mob_db *mob;
  11052. int mob_id;
  11053. mob_id = script_getnum(st,2);
  11054. if (!mobdb_checkid(mob_id)) {
  11055. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i", mob_id);
  11056. script_pushint(st,-1);
  11057. return -1;
  11058. }
  11059. mob = mob_db(mob_id);
  11060. switch ( script_getnum(st,3) ) {
  11061. case 0: //Name
  11062. script_pushconststr(st,mob->jname);
  11063. break;
  11064. case 1: //Lvl
  11065. script_pushint(st,mob->lv);
  11066. break;
  11067. case 2: //MaxHP
  11068. script_pushint(st,mob->status.max_hp);
  11069. break;
  11070. case 3: //Base EXP
  11071. script_pushint(st,mob->base_exp);
  11072. break;
  11073. case 4: //Job EXP
  11074. script_pushint(st,mob->job_exp);
  11075. break;
  11076. case 5: //Atk1
  11077. script_pushint(st,mob->status.rhw.atk);
  11078. break;
  11079. case 6: //Atk2
  11080. script_pushint(st,mob->status.rhw.atk2);
  11081. break;
  11082. case 7: //Def
  11083. script_pushint(st,mob->status.def);
  11084. break;
  11085. case 8: //Mdef
  11086. script_pushint(st,mob->status.mdef);
  11087. break;
  11088. case 9: //Str
  11089. script_pushint(st,mob->status.str);
  11090. break;
  11091. case 10: //Agi
  11092. script_pushint(st,mob->status.agi);
  11093. break;
  11094. case 11: //Vit
  11095. script_pushint(st,mob->status.vit);
  11096. break;
  11097. case 12: //Int
  11098. script_pushint(st,mob->status.int_);
  11099. break;
  11100. case 13: //Dex
  11101. script_pushint(st,mob->status.dex);
  11102. break;
  11103. case 14: //Luk
  11104. script_pushint(st,mob->status.luk);
  11105. break;
  11106. case 15: //Range
  11107. script_pushint(st,mob->status.rhw.range);
  11108. break;
  11109. case 16: //Range2
  11110. script_pushint(st,mob->range2);
  11111. break;
  11112. case 17: //Range3
  11113. script_pushint(st,mob->range3);
  11114. break;
  11115. case 18: //Size
  11116. script_pushint(st,mob->status.size);
  11117. break;
  11118. case 19: //Race
  11119. script_pushint(st,mob->status.race);
  11120. break;
  11121. case 20: //Element
  11122. script_pushint(st,mob->status.def_ele);
  11123. break;
  11124. case 21: //Mode
  11125. script_pushint(st,mob->status.mode);
  11126. break;
  11127. default: //wrong Index
  11128. script_pushint(st,-1);
  11129. }
  11130. return 0;
  11131. }
  11132. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11133. {
  11134. TBL_PC *sd = NULL;
  11135. if(script_hasdata(st,2))
  11136. sd = map_nick2sd(script_getstr(st,2));
  11137. else
  11138. sd = script_rid2sd(st);
  11139. if(sd)
  11140. script_pushint(st,(sd->vender_id != 0));
  11141. else
  11142. script_pushint(st,0);
  11143. return 0;
  11144. }
  11145. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11146. {
  11147. TBL_PC *sd = NULL;
  11148. if(script_hasdata(st,2))
  11149. sd = map_nick2sd(script_getstr(st,2));
  11150. else
  11151. sd = script_rid2sd(st);
  11152. if(sd)
  11153. script_pushint(st,(sd->chatID != 0));
  11154. else
  11155. script_pushint(st,0);
  11156. return 0;
  11157. }
  11158. int axtoi(const char *hexStg)
  11159. {
  11160. int n = 0; // position in string
  11161. int m = 0; // position in digit[] to shift
  11162. int count; // loop index
  11163. int intValue = 0; // integer value of hex string
  11164. int digit[11]; // hold values to convert
  11165. while (n < 10) {
  11166. if (hexStg[n]=='\0')
  11167. break;
  11168. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11169. digit[n] = hexStg[n] & 0x0f; //convert to int
  11170. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11171. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11172. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11173. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11174. else break;
  11175. n++;
  11176. }
  11177. count = n;
  11178. m = n - 1;
  11179. n = 0;
  11180. while(n < count) {
  11181. // digit[n] is value of hex digit at position n
  11182. // (m << 2) is the number of positions to shift
  11183. // OR the bits into return value
  11184. intValue = intValue | (digit[n] << (m << 2));
  11185. m--; // adjust the position to set
  11186. n++; // next digit to process
  11187. }
  11188. return (intValue);
  11189. }
  11190. // [Lance] Hex string to integer converter
  11191. BUILDIN_FUNC(axtoi)
  11192. {
  11193. const char *hex = script_getstr(st,2);
  11194. script_pushint(st,axtoi(hex));
  11195. return 0;
  11196. }
  11197. // [zBuffer] List of player cont commands --->
  11198. BUILDIN_FUNC(rid2name)
  11199. {
  11200. struct block_list *bl = NULL;
  11201. int rid = script_getnum(st,2);
  11202. if((bl = map_id2bl(rid))){
  11203. switch(bl->type){
  11204. case BL_MOB:
  11205. script_pushconststr(st,((TBL_MOB*)bl)->name);
  11206. break;
  11207. case BL_PC:
  11208. script_pushconststr(st,((TBL_PC*)bl)->status.name);
  11209. break;
  11210. case BL_NPC:
  11211. script_pushconststr(st,((TBL_NPC*)bl)->exname);
  11212. break;
  11213. case BL_PET:
  11214. script_pushconststr(st,((TBL_PET*)bl)->pet.name);
  11215. break;
  11216. case BL_HOM:
  11217. script_pushconststr(st,((TBL_HOM*)bl)->homunculus.name);
  11218. break;
  11219. default:
  11220. ShowError("buildin_rid2name: BL type unknown.\n");
  11221. script_pushconststr(st,"");
  11222. break;
  11223. }
  11224. } else {
  11225. ShowError("buildin_rid2name: invalid RID\n");
  11226. script_pushconststr(st,"(null)");
  11227. }
  11228. return 0;
  11229. }
  11230. BUILDIN_FUNC(pcblockmove)
  11231. {
  11232. int id, flag;
  11233. TBL_PC *sd = NULL;
  11234. id = script_getnum(st,2);
  11235. flag = script_getnum(st,3);
  11236. if(id)
  11237. sd = map_id2sd(id);
  11238. else
  11239. sd = script_rid2sd(st);
  11240. if(sd)
  11241. sd->state.blockedmove = flag > 0;
  11242. return 0;
  11243. }
  11244. BUILDIN_FUNC(pcfollow)
  11245. {
  11246. int id, targetid;
  11247. TBL_PC *sd = NULL;
  11248. id = script_getnum(st,2);
  11249. targetid = script_getnum(st,3);
  11250. if(id)
  11251. sd = map_id2sd(id);
  11252. else
  11253. sd = script_rid2sd(st);
  11254. if(sd)
  11255. pc_follow(sd, targetid);
  11256. return 0;
  11257. }
  11258. BUILDIN_FUNC(pcstopfollow)
  11259. {
  11260. int id;
  11261. TBL_PC *sd = NULL;
  11262. id = script_getnum(st,2);
  11263. if(id)
  11264. sd = map_id2sd(id);
  11265. else
  11266. sd = script_rid2sd(st);
  11267. if(sd)
  11268. pc_stop_following(sd);
  11269. return 0;
  11270. }
  11271. // <--- [zBuffer] List of player cont commands
  11272. // [zBuffer] List of mob control commands --->
  11273. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11274. BUILDIN_FUNC(mobspawn)
  11275. {
  11276. int class_,x,y,id;
  11277. const char *str,*map;
  11278. // Who?
  11279. str =script_getstr(st,2);
  11280. // What?
  11281. class_ =script_getnum(st,3);
  11282. // Where?
  11283. map =script_getstr(st,4);
  11284. x =script_getnum(st,5);
  11285. y =script_getnum(st,6);
  11286. id = mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,1,"");
  11287. script_pushint(st,id);
  11288. return 0;
  11289. }
  11290. BUILDIN_FUNC(mobremove)
  11291. {
  11292. int id;
  11293. struct block_list *bl = NULL;
  11294. id = script_getnum(st,2);
  11295. bl = map_id2bl(id);
  11296. if (bl && bl->type == BL_MOB)
  11297. unit_free(bl,0);
  11298. return 0;
  11299. }
  11300. BUILDIN_FUNC(getmobdata)
  11301. {
  11302. int num, id;
  11303. char *name;
  11304. struct mob_data *md = NULL;
  11305. TBL_PC *sd = st->rid?map_id2sd(st->rid):NULL;
  11306. id = script_getnum(st,2);
  11307. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  11308. ShowWarning("buildin_getmobdata: Error in argument!\n");
  11309. return -1;
  11310. }
  11311. num=st->stack->stack_data[st->start+3].u.num;
  11312. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  11313. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  11314. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  11315. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  11316. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  11317. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  11318. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  11319. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  11320. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  11321. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  11322. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  11323. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  11324. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  11325. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  11326. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  11327. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  11328. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  11329. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  11330. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  11331. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  11332. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  11333. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  11334. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  11335. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  11336. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  11337. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  11338. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  11339. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  11340. return 0;
  11341. }
  11342. /// Changes the data of a mob
  11343. ///
  11344. /// setmobdata <mob unit id>,<type>,<value>;
  11345. BUILDIN_FUNC(setmobdata)
  11346. {
  11347. struct block_list* mob_bl;
  11348. mob_bl = map_id2bl(script_getnum(st,2));
  11349. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11350. {
  11351. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11352. int type;
  11353. int value;
  11354. type = script_getnum(st,3);
  11355. value = script_getnum(st,4);
  11356. switch( type )
  11357. {
  11358. case 0:
  11359. md->class_ = (short)value;
  11360. break;
  11361. case 1:
  11362. md->level = (unsigned short)value;
  11363. break;
  11364. case 2:
  11365. md->status.hp = (unsigned int)value;
  11366. break;
  11367. case 3:
  11368. md->status.max_hp = (unsigned int)value;
  11369. break;
  11370. case 4:
  11371. md->master_id = value;
  11372. break;
  11373. case 5:
  11374. md->bl.m = (short)value;
  11375. break;
  11376. case 6:
  11377. md->bl.x = (short)value;
  11378. break;
  11379. case 7:
  11380. md->bl.y = (short)value;
  11381. break;
  11382. case 8:
  11383. md->status.speed = (unsigned short)value;
  11384. break;
  11385. case 9:
  11386. md->status.mode = (unsigned short)value;
  11387. break;
  11388. case 10:
  11389. md->special_state.ai = (unsigned int)value;
  11390. break;
  11391. case 11:
  11392. md->sc.option = (unsigned short)value;
  11393. break;
  11394. case 12:
  11395. md->vd->sex = (char)value;
  11396. break;
  11397. case 13:
  11398. md->vd->class_ = (unsigned short)value;
  11399. break;
  11400. case 14:
  11401. md->vd->hair_style = (unsigned short)value;
  11402. break;
  11403. case 15:
  11404. md->vd->hair_color = (unsigned short)value;
  11405. break;
  11406. case 16:
  11407. md->vd->head_bottom = (unsigned short)value;
  11408. break;
  11409. case 17:
  11410. md->vd->head_mid = (unsigned short)value;
  11411. break;
  11412. case 18:
  11413. md->vd->head_top = (unsigned short)value;
  11414. break;
  11415. case 19:
  11416. md->vd->cloth_color = (unsigned short)value;
  11417. break;
  11418. case 20:
  11419. md->vd->shield = (unsigned short)value;
  11420. break;
  11421. case 21:
  11422. md->vd->weapon = (unsigned short)value;
  11423. break;
  11424. case 22:
  11425. md->vd->shield = (unsigned short)value;
  11426. break;
  11427. case 23:
  11428. md->ud.dir = (unsigned char)value;
  11429. break;
  11430. case 24:
  11431. md->state.killer = value > 0 ? 1 : 0;
  11432. break;
  11433. case 25:
  11434. md->callback_flag = (short)value;
  11435. break;
  11436. case 26:
  11437. md->state.no_random_walk = value > 0 ? 1 : 0;
  11438. break;
  11439. default:
  11440. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  11441. return 1;
  11442. }
  11443. }
  11444. return 0;
  11445. }
  11446. /// Makes the mob assist the target unit as a slave
  11447. ///
  11448. /// mobassist <mob unit id>,"<player name>";
  11449. /// mobassist <mob unit id>,<target id>;
  11450. BUILDIN_FUNC(mobassist)
  11451. {
  11452. struct block_list* mob_bl;
  11453. // get mob
  11454. mob_bl = map_id2bl(script_getnum(st,2));
  11455. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11456. {
  11457. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11458. struct block_list* target_bl = NULL;
  11459. struct script_data* data;
  11460. // get target
  11461. data = script_getdata(st, 3);
  11462. get_val(st, data);
  11463. if( data_isstring(data) )
  11464. {
  11465. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11466. if( sd != NULL )
  11467. target_bl = &sd->bl;
  11468. }
  11469. if( target_bl == NULL )
  11470. target_bl = map_id2bl(conv_num(st, data));
  11471. // set mob as slave
  11472. if( target_bl != NULL )
  11473. {
  11474. struct unit_data* ud;
  11475. md->master_id = target_bl->id;
  11476. md->state.killer = 1;
  11477. mob_convertslave(md);
  11478. ud = unit_bl2ud(mob_bl);
  11479. if( ud != NULL )
  11480. {
  11481. if( ud->target != 0 )
  11482. md->target_id = ud->target;
  11483. else if( ud->skilltarget != 0 )
  11484. md->target_id = ud->skilltarget;
  11485. if( md->target_id != 0 )
  11486. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  11487. }
  11488. }
  11489. }
  11490. return 0;
  11491. }
  11492. /// Attaches the current npc or the target npc to the mob unit
  11493. ///
  11494. /// mobattach <mob unit id>{,"<npc name>"};
  11495. BUILDIN_FUNC(mobattach)
  11496. {
  11497. struct block_list* mob_bl;
  11498. mob_bl = map_id2bl(script_getnum(st,2));
  11499. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11500. {
  11501. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11502. TBL_NPC* nd = NULL;
  11503. if( script_hasdata(st,3) )
  11504. nd = npc_name2id(script_getstr(st, 3));
  11505. else
  11506. {
  11507. struct block_list* npc_bl = map_id2bl(st->oid);
  11508. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  11509. nd = (TBL_NPC*)npc_bl;
  11510. }
  11511. if( nd != NULL )
  11512. md->nd = nd;
  11513. }
  11514. return 0;
  11515. }
  11516. /// Makes the unit walk to target position or map
  11517. /// Returns if it was successfull
  11518. ///
  11519. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11520. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11521. BUILDIN_FUNC(unitwalk)
  11522. {
  11523. struct block_list* bl;
  11524. bl = map_id2bl(script_getnum(st,2));
  11525. if( bl == NULL )
  11526. {
  11527. script_pushint(st, 0);
  11528. }
  11529. else if( script_hasdata(st,4) )
  11530. {
  11531. int x = script_getnum(st,3);
  11532. int y = script_getnum(st,4);
  11533. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11534. }
  11535. else
  11536. {
  11537. int map_id = script_getnum(st,3);
  11538. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11539. }
  11540. return 0;
  11541. }
  11542. /// Kills the unit
  11543. ///
  11544. /// unitkill <unit_id>;
  11545. BUILDIN_FUNC(unitkill)
  11546. {
  11547. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11548. if( bl != NULL )
  11549. status_kill(bl);
  11550. return 0;
  11551. }
  11552. /// Warps the unit to the target position in the target map
  11553. /// Returns if it was successfull
  11554. ///
  11555. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11556. BUILDIN_FUNC(unitwarp)
  11557. {
  11558. int unit_id;
  11559. int map;
  11560. short x;
  11561. short y;
  11562. struct block_list* bl;
  11563. unit_id = script_getnum(st,2);
  11564. map = map_mapname2mapid(script_getstr(st, 3));
  11565. x = (short)script_getnum(st,4);
  11566. y = (short)script_getnum(st,5);
  11567. bl = map_id2bl(unit_id);
  11568. if( map >= 0 && bl != NULL )
  11569. script_pushint(st, unit_warp(bl,map,x,y,0));
  11570. else
  11571. script_pushint(st, 0);
  11572. return 0;
  11573. }
  11574. /// Makes the unit attack the target.
  11575. /// If the unit is a player and <action type> is not 0, it does a continuous
  11576. /// attack instead of a single attack.
  11577. /// Returns if the request was successfull.
  11578. ///
  11579. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11580. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11581. BUILDIN_FUNC(unitattack)
  11582. {
  11583. struct block_list* unit_bl;
  11584. struct block_list* target_bl = NULL;
  11585. struct script_data* data;
  11586. int actiontype = 0;
  11587. // get unit
  11588. unit_bl = map_id2bl(script_getnum(st,2));
  11589. if( unit_bl == NULL ) {
  11590. script_pushint(st, 0);
  11591. return 0;
  11592. }
  11593. data = script_getdata(st, 3);
  11594. get_val(st, data);
  11595. if( data_isstring(data) )
  11596. {
  11597. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11598. if( sd != NULL )
  11599. target_bl = &sd->bl;
  11600. } else
  11601. target_bl = map_id2bl(conv_num(st, data));
  11602. // request the attack
  11603. if( target_bl == NULL )
  11604. {
  11605. script_pushint(st, 0);
  11606. return 0;
  11607. }
  11608. // get actiontype
  11609. if( script_hasdata(st,4) )
  11610. actiontype = script_getnum(st,4);
  11611. switch( unit_bl->type )
  11612. {
  11613. case BL_PC:
  11614. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11615. script_pushint(st, 1);
  11616. return 0;
  11617. case BL_MOB:
  11618. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11619. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11620. break;
  11621. case BL_PET:
  11622. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11623. break;
  11624. default:
  11625. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11626. script_pushint(st, 0);
  11627. return 0;
  11628. }
  11629. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11630. return 0;
  11631. }
  11632. /// Makes the unit stop attacking and moving
  11633. ///
  11634. /// unitstop <unit_id>;
  11635. BUILDIN_FUNC(unitstop)
  11636. {
  11637. int unit_id;
  11638. struct block_list* bl;
  11639. unit_id = script_getnum(st,2);
  11640. bl = map_id2bl(unit_id);
  11641. if( bl != NULL )
  11642. {
  11643. unit_stop_attack(bl);
  11644. unit_stop_walking(bl,0);
  11645. if( bl->type == BL_MOB )
  11646. ((TBL_MOB*)bl)->target_id = 0;
  11647. }
  11648. return 0;
  11649. }
  11650. /// Makes the unit say the message
  11651. ///
  11652. /// unittalk <unit_id>,"<message>";
  11653. BUILDIN_FUNC(unittalk)
  11654. {
  11655. int unit_id;
  11656. const char* message;
  11657. struct block_list* bl;
  11658. unit_id = script_getnum(st,2);
  11659. message = script_getstr(st, 3);
  11660. bl = map_id2bl(unit_id);
  11661. if( bl != NULL )
  11662. {
  11663. struct StringBuf* buf = StringBuf_Malloc();
  11664. StringBuf_Printf(buf, "%s : %s", status_get_name(bl), message);
  11665. clif_message(bl, StringBuf_Value(buf));
  11666. if( bl->type == BL_PC )
  11667. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(buf));
  11668. StringBuf_Free(buf);
  11669. }
  11670. return 0;
  11671. }
  11672. /// Makes the unit do an emotion
  11673. ///
  11674. /// unitemote <unit_id>,<emotion>;
  11675. ///
  11676. /// @see e_* in const.txt
  11677. BUILDIN_FUNC(unitemote)
  11678. {
  11679. int unit_id;
  11680. int emotion;
  11681. struct block_list* bl;
  11682. unit_id = script_getnum(st,2);
  11683. emotion = script_getnum(st,3);
  11684. bl = map_id2bl(unit_id);
  11685. if( bl != NULL )
  11686. clif_emotion(bl, emotion);
  11687. return 0;
  11688. }
  11689. /// Makes the unit do an action
  11690. /// TODO actions
  11691. ///
  11692. /// unitdeadsit <unit_id>,<action>;
  11693. BUILDIN_FUNC(unitdeadsit)
  11694. {
  11695. int unit_id;
  11696. int action;
  11697. struct block_list* bl;
  11698. unit_id = script_getnum(st,2);
  11699. action = script_getnum(st,3);
  11700. bl = map_id2bl(unit_id);
  11701. if( bl != NULL )
  11702. {
  11703. if( action > -1 && action < 4 )
  11704. {
  11705. unsigned char buf[61] = "";
  11706. struct view_data *vd = status_get_viewdata(bl);
  11707. if (vd) vd->dead_sit = action;
  11708. WBUFW(buf, 0) = 0x8a;
  11709. WBUFL(buf, 2) = bl->id;
  11710. WBUFB(buf,26) = (unsigned char)action;
  11711. clif_send(buf, 61, bl, AREA);
  11712. } else
  11713. {
  11714. ShowWarning("script:unitdeadsit: %d is not a valid action\n", action);
  11715. report_src(st);
  11716. }
  11717. }
  11718. return 0;
  11719. }
  11720. /// Makes the unit cast the skill on the target or self if no target is specified
  11721. ///
  11722. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11723. BUILDIN_FUNC(unitskilluseid)
  11724. {
  11725. int unit_id;
  11726. int skill_id;
  11727. int skill_lv;
  11728. int target_id;
  11729. struct block_list* bl;
  11730. unit_id = script_getnum(st,2);
  11731. skill_id = script_getnum(st,3);
  11732. skill_lv = script_getnum(st,4);
  11733. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11734. bl = map_id2bl(unit_id);
  11735. if( bl != NULL )
  11736. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11737. return 0;
  11738. }
  11739. /// Makes the unit cast the skill on the target position.
  11740. ///
  11741. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11742. BUILDIN_FUNC(unitskillusepos)
  11743. {
  11744. int unit_id;
  11745. int skill_id;
  11746. int skill_lv;
  11747. int skill_x;
  11748. int skill_y;
  11749. struct block_list* bl;
  11750. unit_id = script_getnum(st,2);
  11751. skill_id = script_getnum(st,3);
  11752. skill_lv = script_getnum(st,4);
  11753. skill_x = script_getnum(st,5);
  11754. skill_y = script_getnum(st,6);
  11755. bl = map_id2bl(unit_id);
  11756. if( bl != NULL )
  11757. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11758. return 0;
  11759. }
  11760. // <--- [zBuffer] List of mob control commands
  11761. /// Pauses the execution of the script, detaching the player
  11762. ///
  11763. /// sleep <mili seconds>;
  11764. BUILDIN_FUNC(sleep)
  11765. {
  11766. int ticks;
  11767. TBL_PC* sd;
  11768. ticks = script_getnum(st,2);
  11769. sd = map_id2sd(st->rid);
  11770. // detach the player
  11771. if( sd && sd->npc_id == st->oid )
  11772. {
  11773. sd->npc_id = 0;
  11774. }
  11775. st->rid = 0;
  11776. if( ticks <= 0 )
  11777. {// do nothing
  11778. }
  11779. else if( st->sleep.tick == 0 )
  11780. {// sleep for the target amount of time
  11781. st->state = RERUNLINE;
  11782. st->sleep.tick = ticks;
  11783. }
  11784. else
  11785. {// sleep time is over
  11786. st->sleep.tick = 0;
  11787. }
  11788. return 0;
  11789. }
  11790. /// Pauses the execution of the script, keeping the player attached
  11791. /// Returns if a player is still attached
  11792. ///
  11793. /// sleep2(<mili secconds>) -> <bool>
  11794. BUILDIN_FUNC(sleep2)
  11795. {
  11796. int ticks;
  11797. ticks = script_getnum(st,2);
  11798. if( ticks <= 0 )
  11799. {// do nothing
  11800. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11801. }
  11802. else if( !st->sleep.tick )
  11803. {// sleep for the target amount of time
  11804. st->state = RERUNLINE;
  11805. st->sleep.tick = ticks;
  11806. }
  11807. else
  11808. {// sleep time is over
  11809. st->sleep.tick = 0;
  11810. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11811. }
  11812. return 0;
  11813. }
  11814. /// Awakes all the sleep timers of the target npc
  11815. ///
  11816. /// awake "<npc name>";
  11817. BUILDIN_FUNC(awake)
  11818. {
  11819. struct npc_data* nd;
  11820. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11821. nd = npc_name2id(script_getstr(st, 2));
  11822. if( nd == NULL )
  11823. return 0;
  11824. while( node )
  11825. {
  11826. if( (int)node->key == nd->bl.id )
  11827. {// sleep timer for the npc
  11828. struct script_state* tst = (struct script_state*)node->data;
  11829. TBL_PC* sd = map_id2sd(tst->rid);
  11830. if( tst->sleep.timer == -1 )
  11831. {// already awake ???
  11832. node = node->next;
  11833. continue;
  11834. }
  11835. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11836. {// char not online anymore / another char of the same account is online - Cancel execution
  11837. tst->state = END;
  11838. tst->rid = 0;
  11839. }
  11840. delete_timer(tst->sleep.timer, run_script_timer);
  11841. node = script_erase_sleepdb(node);
  11842. tst->sleep.timer = -1;
  11843. tst->sleep.tick = 0;
  11844. run_script_main(tst);
  11845. }
  11846. else
  11847. {
  11848. node = node->next;
  11849. }
  11850. }
  11851. return 0;
  11852. }
  11853. /// Returns a reference to a variable of the target NPC.
  11854. /// Returns 0 if an error occurs.
  11855. ///
  11856. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11857. BUILDIN_FUNC(getvariableofnpc)
  11858. {
  11859. struct script_data* data;
  11860. data = script_getdata(st,2);
  11861. if( !data_isreference(data) )
  11862. {// Not a reference (aka varaible name)
  11863. ShowError("script: getvariableofnpc: first argument is not a variable name\n");
  11864. st->state = END;
  11865. return 1;
  11866. }
  11867. else
  11868. {
  11869. int num = data->u.num;
  11870. char* var_name = str_buf + str_data[num&0x00ffffff].str;
  11871. const char* npc_name = script_getstr(st, 3);
  11872. struct npc_data* nd = npc_name2id(npc_name);
  11873. if( var_name[0] != '.' || var_name[1] == '@' )
  11874. {// not a npc variable
  11875. ShowError("script: getvariableofnpc: invalid scope %s (not npc variable)\n", var_name);
  11876. st->state = END;
  11877. return 1;
  11878. }
  11879. if( nd == NULL || nd->bl.subtype != SCRIPT || nd->u.scr.script == NULL )
  11880. {// NPC not found or has no script
  11881. ShowError("script: getvariableofnpc: can't find npc %s\n", npc_name);
  11882. st->state = END;
  11883. return 1;
  11884. }
  11885. push_val2(st->stack, C_NAME, num, &nd->u.scr.script->script_vars );
  11886. }
  11887. return 0;
  11888. }
  11889. /// Opens a warp portal.
  11890. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11891. ///
  11892. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11893. ///
  11894. /// @author blackhole89
  11895. BUILDIN_FUNC(warpportal)
  11896. {
  11897. int spx;
  11898. int spy;
  11899. unsigned short mapindex;
  11900. int tpx;
  11901. int tpy;
  11902. struct skill_unit_group* group;
  11903. struct block_list* bl;
  11904. bl = map_id2bl(st->oid);
  11905. if( bl == NULL )
  11906. {
  11907. ShowError("script:warpportal: npc is needed");
  11908. return 1;
  11909. }
  11910. spx = script_getnum(st,2);
  11911. spy = script_getnum(st,3);
  11912. mapindex = mapindex_name2id(script_getstr(st, 4));
  11913. tpx = script_getnum(st,5);
  11914. tpy = script_getnum(st,6);
  11915. if( mapindex == 0 )
  11916. return 0;// map not found
  11917. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 1);
  11918. if( group == NULL )
  11919. return 0;// failed
  11920. group->val2 = (tpx<<16) | tpy;
  11921. group->val3 = mapindex;
  11922. return 0;
  11923. }