script.c 322 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_FUNCIN
  4. //#define DEBUG_DISP
  5. //#define DEBUG_DISASM
  6. //#define DEBUG_RUN
  7. //#define DEBUG_HASH
  8. #include "../common/cbasetypes.h"
  9. #include "../common/timer.h"
  10. #include "../common/malloc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/utils.h"
  16. #include "map.h"
  17. #include "clif.h"
  18. #include "chrif.h"
  19. #include "itemdb.h"
  20. #include "pc.h"
  21. #include "status.h"
  22. #include "storage.h"
  23. #include "mob.h"
  24. #include "npc.h"
  25. #include "pet.h"
  26. #include "mercenary.h" //[orn]
  27. #include "intif.h"
  28. #include "skill.h"
  29. #include "chat.h"
  30. #include "battle.h"
  31. #include "party.h"
  32. #include "guild.h"
  33. #include "atcommand.h"
  34. #include "charcommand.h"
  35. #include "log.h"
  36. #include "unit.h"
  37. #include "irc.h"
  38. #include "pet.h"
  39. #include "mail.h"
  40. #include "script.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <math.h>
  45. #ifndef WIN32
  46. #include <sys/time.h>
  47. #endif
  48. #include <time.h>
  49. #include <setjmp.h>
  50. #include <errno.h>
  51. ///////////////////////////////////////////////////////////////////////////////
  52. //## TODO possible enhancements: [FlavioJS]
  53. // - 'callfunc' supporting labels in the current npc "::LabelName"
  54. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  55. // - 'function FuncName;' function declarations reverting to global functions
  56. // if local label isn't found
  57. // - join callfunc and callsub's functionality
  58. //
  59. // struct script_state* st;
  60. //
  61. /// Returns the script_data at the target index
  62. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  63. /// Returns if the stack contains data at the target index
  64. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  65. /// Returns the index of the last data in the stack
  66. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  67. /// Pushes an int into the stack
  68. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  69. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  70. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  71. /// Pushes a nil into the stack
  72. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  73. /// Pushes a copy of the data in the target index
  74. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  75. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  76. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  77. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  78. // Note: "top" functions/defines use indexes relative to the top of the stack
  79. // -1 is the index of the data at the top
  80. /// Returns the script_data at the target index relative to the top of the stack
  81. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  82. /// Pushes a copy of the data in the target index relative to the top of the stack
  83. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  84. /// Removes the range of values [start,end[ relative to the top of the stack
  85. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  86. //
  87. // struct script_data* data;
  88. //
  89. /// Returns if the script data is a string
  90. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  91. /// Returns if the script data is an int
  92. #define data_isint(data) ( (data)->type == C_INT )
  93. /// Returns if the script data is a reference
  94. #define data_isreference(data) ( (data)->type == C_NAME )
  95. /// Returns if the script data is a label
  96. #define data_islabel(data) ( (data)->type == C_POS )
  97. /// Returns if the script data is an internal script function label
  98. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  99. /// Returns if this is a reference to a constant
  100. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  101. /// Returns if this a reference to a param
  102. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  103. /// Returns if this a reference to a variable
  104. //##TODO confirm it's C_NAME [FlavioJS]
  105. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  106. /// Returns the unique id of the reference (id and index)
  107. #define reference_getuid(data) ( (data)->u.num )
  108. /// Returns the id of the reference
  109. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  110. /// Returns the array index of the reference
  111. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  112. /// Returns the name of the reference
  113. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  114. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  115. #define reference_getref(data) ( (data)->ref )
  116. /// Returns the value of the constant
  117. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  118. /// Returns the type of param
  119. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  120. /// Composes the uid of a reference from the id and the index
  121. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  122. #define FETCH(n, t) \
  123. if( script_hasdata(st,n) ) \
  124. (t)=script_getnum(st,n);
  125. #define SCRIPT_BLOCK_SIZE 512
  126. enum { LABEL_NEXTLINE=1,LABEL_START };
  127. static unsigned char * script_buf = NULL;
  128. static int script_pos,script_size;
  129. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  130. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  131. #define GETSTRING(off) (str_buf+(off))
  132. static char *str_buf;
  133. static int str_pos,str_size;
  134. static struct str_data_struct {
  135. enum c_op type;
  136. int str;
  137. int backpatch;
  138. int label;
  139. int (*func)(struct script_state *st);
  140. int val;
  141. int next;
  142. } *str_data = NULL;
  143. int str_num=LABEL_START,str_data_size;
  144. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  145. #define SCRIPT_HASH_SIZE 1021
  146. int str_hash[SCRIPT_HASH_SIZE];
  147. //#define SCRIPT_HASH_DJB2
  148. #define SCRIPT_HASH_SDBM
  149. //#define SCRIPT_HASH_ELF
  150. //#define SCRIPT_HASH_PJW
  151. static DBMap* mapreg_db=NULL; // int var_id -> int value
  152. static DBMap* mapregstr_db=NULL; // int var_id -> char* value
  153. static int mapreg_dirty=-1;
  154. char mapreg_txt[256]="save/mapreg.txt";
  155. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  156. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  157. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  158. static int parse_options=0;
  159. DBMap* script_get_label_db(){ return scriptlabel_db; }
  160. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  161. struct Script_Config script_config;
  162. static jmp_buf error_jump;
  163. static char* error_msg;
  164. static const char* error_pos;
  165. static int error_report; // if the error should produce output
  166. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  167. // [Eoe / jA 1080, 1081, 1094, 1164]
  168. enum curly_type {
  169. TYPE_NULL = 0,
  170. TYPE_IF,
  171. TYPE_SWITCH,
  172. TYPE_WHILE,
  173. TYPE_FOR,
  174. TYPE_DO,
  175. TYPE_USERFUNC,
  176. TYPE_ARGLIST // function argument list
  177. };
  178. #define ARGLIST_UNDEFINED 0
  179. #define ARGLIST_NO_PAREN 1
  180. #define ARGLIST_PAREN 2
  181. static struct {
  182. struct {
  183. enum curly_type type;
  184. int index;
  185. int count;
  186. int flag;
  187. struct linkdb_node *case_label;
  188. } curly[256]; // 右カッコの情報
  189. int curly_count; // 右カッコの数
  190. int index; // スクリプト内で使用した構文の数
  191. } syntax;
  192. const char* parse_curly_close(const char* p);
  193. const char* parse_syntax_close(const char* p);
  194. const char* parse_syntax_close_sub(const char* p,int* flag);
  195. const char* parse_syntax(const char* p);
  196. static int parse_syntax_for_flag = 0;
  197. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  198. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  199. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  200. int potion_target=0;
  201. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  202. char mapregsql_db[32] = "mapreg";
  203. char mapregsql_db_varname[32] = "varname";
  204. char mapregsql_db_index[32] = "index";
  205. char mapregsql_db_value[32] = "value";
  206. #endif
  207. int get_com(unsigned char *script,int *pos);
  208. int get_num(unsigned char *script,int *pos);
  209. struct script_function {
  210. int (*func)(struct script_state *st);
  211. const char *name;
  212. const char *arg;
  213. } buildin_func[];
  214. static struct linkdb_node *sleep_db;
  215. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  216. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  217. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  218. /*==========================================
  219. * ローカルプロトタイプ宣言 (必要な物のみ)
  220. *------------------------------------------*/
  221. const char* parse_subexpr(const char* p,int limit);
  222. void push_val(struct script_stack *stack,int type,int val);
  223. int run_func(struct script_state *st);
  224. int mapreg_setreg(int num,int val);
  225. int mapreg_setregstr(int num,const char *str);
  226. enum {
  227. MF_NOMEMO, //0
  228. MF_NOTELEPORT,
  229. MF_NOSAVE,
  230. MF_NOBRANCH,
  231. MF_NOPENALTY,
  232. MF_NOZENYPENALTY,
  233. MF_PVP,
  234. MF_PVP_NOPARTY,
  235. MF_PVP_NOGUILD,
  236. MF_GVG,
  237. MF_GVG_NOPARTY, //10
  238. MF_NOTRADE,
  239. MF_NOSKILL,
  240. MF_NOWARP,
  241. MF_PARTYLOCK,
  242. MF_NOICEWALL,
  243. MF_SNOW,
  244. MF_FOG,
  245. MF_SAKURA,
  246. MF_LEAVES,
  247. MF_RAIN, //20
  248. MF_INDOORS,
  249. MF_NOGO,
  250. MF_CLOUDS,
  251. MF_CLOUDS2,
  252. MF_FIREWORKS,
  253. MF_GVG_CASTLE,
  254. MF_GVG_DUNGEON,
  255. MF_NIGHTENABLED,
  256. MF_NOBASEEXP,
  257. MF_NOJOBEXP, //30
  258. MF_NOMOBLOOT,
  259. MF_NOMVPLOOT,
  260. MF_NORETURN,
  261. MF_NOWARPTO,
  262. MF_NIGHTMAREDROP,
  263. MF_RESTRICTED,
  264. MF_NOCOMMAND,
  265. MF_NODROP,
  266. MF_JEXP,
  267. MF_BEXP, //40
  268. MF_NOVENDING,
  269. MF_LOADEVENT,
  270. MF_NOCHAT,
  271. MF_NOEXPPENALTY,
  272. MF_GUILDLOCK
  273. };
  274. const char* script_op2name(int op)
  275. {
  276. #define RETURN_OP_NAME(type) case type: return #type
  277. switch( op )
  278. {
  279. RETURN_OP_NAME(C_NOP);
  280. RETURN_OP_NAME(C_POS);
  281. RETURN_OP_NAME(C_INT);
  282. RETURN_OP_NAME(C_PARAM);
  283. RETURN_OP_NAME(C_FUNC);
  284. RETURN_OP_NAME(C_STR);
  285. RETURN_OP_NAME(C_CONSTSTR);
  286. RETURN_OP_NAME(C_ARG);
  287. RETURN_OP_NAME(C_NAME);
  288. RETURN_OP_NAME(C_EOL);
  289. RETURN_OP_NAME(C_RETINFO);
  290. RETURN_OP_NAME(C_USERFUNC);
  291. RETURN_OP_NAME(C_USERFUNC_POS);
  292. // operators
  293. RETURN_OP_NAME(C_OP3);
  294. RETURN_OP_NAME(C_LOR);
  295. RETURN_OP_NAME(C_LAND);
  296. RETURN_OP_NAME(C_LE);
  297. RETURN_OP_NAME(C_LT);
  298. RETURN_OP_NAME(C_GE);
  299. RETURN_OP_NAME(C_GT);
  300. RETURN_OP_NAME(C_EQ);
  301. RETURN_OP_NAME(C_NE);
  302. RETURN_OP_NAME(C_XOR);
  303. RETURN_OP_NAME(C_OR);
  304. RETURN_OP_NAME(C_AND);
  305. RETURN_OP_NAME(C_ADD);
  306. RETURN_OP_NAME(C_SUB);
  307. RETURN_OP_NAME(C_MUL);
  308. RETURN_OP_NAME(C_DIV);
  309. RETURN_OP_NAME(C_MOD);
  310. RETURN_OP_NAME(C_NEG);
  311. RETURN_OP_NAME(C_LNOT);
  312. RETURN_OP_NAME(C_NOT);
  313. RETURN_OP_NAME(C_R_SHIFT);
  314. RETURN_OP_NAME(C_L_SHIFT);
  315. default:
  316. ShowDebug("script_op2name: unexpected op=%d\n", op);
  317. return "???";
  318. }
  319. #undef RETURN_OP_NAME
  320. }
  321. /// Reports on the console the src of a script error.
  322. static void script_reportsrc(struct script_state *st)
  323. {
  324. struct block_list* bl;
  325. if( st->oid == 0 )
  326. return; //Can't report source.
  327. bl = map_id2bl(st->oid);
  328. if( bl == NULL )
  329. return;
  330. switch( bl->type )
  331. {
  332. case BL_NPC:
  333. if( bl->m >= 0 )
  334. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  335. else
  336. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  337. break;
  338. default:
  339. if( bl->m >= 0 )
  340. 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);
  341. else
  342. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  343. break;
  344. }
  345. }
  346. /// Reports on the console information about the script data.
  347. static void script_reportdata(struct script_data* data)
  348. {
  349. if( data == NULL )
  350. return;
  351. switch( data->type )
  352. {
  353. case C_NOP:// no value
  354. ShowDebug("Data: nothing (nil)\n");
  355. break;
  356. case C_INT:// number
  357. ShowDebug("Data: number value=%d\n", data->u.num);
  358. break;
  359. case C_STR:
  360. case C_CONSTSTR:// string
  361. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  362. break;
  363. case C_NAME:// reference
  364. if( reference_tovariable(data) )
  365. {// variable
  366. char* name = reference_getname(data);
  367. if( not_array_variable(*name) )
  368. ShowDebug("Data: variable name='%s'\n", name);
  369. else
  370. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  371. }
  372. else if( reference_toconstant(data) )
  373. {// constant
  374. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  375. }
  376. else if( reference_toparam(data) )
  377. {// param
  378. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  379. }
  380. else
  381. {// ???
  382. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  383. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  384. }
  385. break;
  386. case C_POS:// label
  387. ShowDebug("Data: label pos=%d\n", data->u.num);
  388. break;
  389. default:
  390. ShowDebug("Data: %s\n", script_op2name(data->type));
  391. break;
  392. }
  393. }
  394. /*==========================================
  395. * エラーメッセージ出力
  396. *------------------------------------------*/
  397. static void disp_error_message2(const char *mes,const char *pos,int report)
  398. {
  399. error_msg = aStrdup(mes);
  400. error_pos = pos;
  401. error_report = report;
  402. longjmp( error_jump, 1 );
  403. }
  404. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  405. /// Checks event parameter validity
  406. static void check_event(struct script_state *st, const char *evt)
  407. {
  408. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  409. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  410. script_reportsrc(st);
  411. }
  412. }
  413. /*==========================================
  414. * 文字列のハッシュを計算
  415. *------------------------------------------*/
  416. #define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE)
  417. static unsigned int calc_hash2(const char* p)
  418. {
  419. #if defined(SCRIPT_HASH_DJB2)
  420. unsigned int h = 5381;
  421. while( *p ) // hash*33 + c
  422. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  423. return h;
  424. #elif defined(SCRIPT_HASH_SDBM)
  425. unsigned int h = 0;
  426. while( *p )
  427. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  428. return h;
  429. #elif defined(SCRIPT_HASH_ELF)
  430. unsigned int h = 0;
  431. unsigned int g;
  432. while( *p ){ // UNIX ELF hash
  433. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  434. g = h & 0xF0000000;
  435. if( g )
  436. h ^= g >> 24;
  437. h &= ~g;
  438. }
  439. return h;
  440. #elif defined(SCRIPT_HASH_PJW)
  441. unsigned int h = 0;
  442. unsigned int g;
  443. while( *p ){
  444. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  445. g = h & 0xF0000000;
  446. if( g )
  447. {
  448. h ^= g>>24;
  449. h ^= g;
  450. }
  451. }
  452. return h;
  453. #else
  454. unsigned int h = 0;
  455. while( *p ){
  456. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  457. h += (unsigned char)TOLOWER(*p++);
  458. }
  459. return h;
  460. #endif
  461. }
  462. /*==========================================
  463. * str_dataの中に名前があるか検索する
  464. *------------------------------------------*/
  465. // 既存のであれば番号、無ければ-1
  466. static int search_str(const char *p)
  467. {
  468. int i;
  469. i=str_hash[calc_hash(p)];
  470. while(i){
  471. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  472. return i;
  473. }
  474. i=str_data[i].next;
  475. }
  476. return -1;
  477. }
  478. /*==========================================
  479. * str_dataに名前を登録
  480. *------------------------------------------*/
  481. // 既存のであれば番号、無ければ登録して新規番号
  482. int add_str(const char* p)
  483. {
  484. int i;
  485. int len;
  486. i=calc_hash(p);
  487. if(str_hash[i]==0){
  488. str_hash[i]=str_num;
  489. } else {
  490. i=str_hash[i];
  491. for(;;){
  492. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  493. return i;
  494. }
  495. if(str_data[i].next==0)
  496. break;
  497. i=str_data[i].next;
  498. }
  499. str_data[i].next=str_num;
  500. }
  501. if(str_num>=str_data_size){
  502. str_data_size+=128;
  503. RECREATE(str_data,struct str_data_struct,str_data_size);
  504. memset(str_data + (str_data_size - 128), '\0', 128);
  505. }
  506. len=(int)strlen(p);
  507. while(str_pos+len+1>=str_size){
  508. str_size+=256;
  509. RECREATE(str_buf,char,str_size);
  510. memset(str_buf + (str_size - 256), '\0', 256);
  511. }
  512. memcpy(str_buf+str_pos,p,len+1);
  513. str_data[str_num].type=C_NOP;
  514. str_data[str_num].str=str_pos;
  515. str_data[str_num].next=0;
  516. str_data[str_num].func=NULL;
  517. str_data[str_num].backpatch=-1;
  518. str_data[str_num].label=-1;
  519. str_pos+=len+1;
  520. return str_num++;
  521. }
  522. /*==========================================
  523. * スクリプトバッファサイズの確認と拡張
  524. *------------------------------------------*/
  525. static void expand_script_buf(void)
  526. {
  527. script_size+=SCRIPT_BLOCK_SIZE;
  528. RECREATE(script_buf,unsigned char,script_size);
  529. }
  530. /*==========================================
  531. * スクリプトバッファに1バイト書き込む
  532. *------------------------------------------*/
  533. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  534. #if 0
  535. static void add_scriptb(int a)
  536. {
  537. expand_script_buf();
  538. script_buf[script_pos++]=a;
  539. }
  540. #endif
  541. /*==========================================
  542. * スクリプトバッファにデータタイプを書き込む
  543. *------------------------------------------*/
  544. static void add_scriptc(int a)
  545. {
  546. while(a>=0x40){
  547. add_scriptb((a&0x3f)|0x40);
  548. a=(a-0x40)>>6;
  549. }
  550. add_scriptb(a&0x3f);
  551. }
  552. /*==========================================
  553. * スクリプトバッファに整数を書き込む
  554. *------------------------------------------*/
  555. static void add_scripti(int a)
  556. {
  557. while(a>=0x40){
  558. add_scriptb(a|0xc0);
  559. a=(a-0x40)>>6;
  560. }
  561. add_scriptb(a|0x80);
  562. }
  563. /*==========================================
  564. * スクリプトバッファにラベル/変数/関数を書き込む
  565. *------------------------------------------*/
  566. // 最大16Mまで
  567. static void add_scriptl(int l)
  568. {
  569. int backpatch = str_data[l].backpatch;
  570. switch(str_data[l].type){
  571. case C_POS:
  572. case C_USERFUNC_POS:
  573. add_scriptc(C_POS);
  574. add_scriptb(str_data[l].label);
  575. add_scriptb(str_data[l].label>>8);
  576. add_scriptb(str_data[l].label>>16);
  577. break;
  578. case C_NOP:
  579. case C_USERFUNC:
  580. // ラベルの可能性があるのでbackpatch用データ埋め込み
  581. add_scriptc(C_NAME);
  582. str_data[l].backpatch=script_pos;
  583. add_scriptb(backpatch);
  584. add_scriptb(backpatch>>8);
  585. add_scriptb(backpatch>>16);
  586. break;
  587. case C_INT:
  588. add_scripti(abs(str_data[l].val));
  589. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  590. add_scriptc(C_NEG);
  591. break;
  592. default:
  593. // もう他の用途と確定してるので数字をそのまま
  594. add_scriptc(C_NAME);
  595. add_scriptb(l);
  596. add_scriptb(l>>8);
  597. add_scriptb(l>>16);
  598. break;
  599. }
  600. }
  601. /*==========================================
  602. * ラベルを解決する
  603. *------------------------------------------*/
  604. void set_label(int l,int pos, const char* script_pos)
  605. {
  606. int i,next;
  607. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  608. { //Prevent overwriting constants values and parameters [Skotlex]
  609. disp_error_message("set_label: invalid label name",script_pos);
  610. return;
  611. }
  612. if(str_data[l].label!=-1){
  613. disp_error_message("set_label: dup label ",script_pos);
  614. return;
  615. }
  616. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  617. str_data[l].label=pos;
  618. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  619. next=GETVALUE(script_buf,i);
  620. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  621. SETVALUE(script_buf,i,pos);
  622. i=next;
  623. }
  624. }
  625. /// Skips spaces and/or comments.
  626. const char* skip_space(const char* p)
  627. {
  628. if( p == NULL )
  629. return NULL;
  630. for(;;)
  631. {
  632. while( ISSPACE(*p) )
  633. ++p;
  634. if( *p == '/' && p[1] == '/' )
  635. {// line comment
  636. while(*p && *p!='\n')
  637. ++p;
  638. }
  639. else if( *p == '/' && p[1] == '*' )
  640. {// block comment
  641. p += 2;
  642. for(;;)
  643. {
  644. if( *p == '\0' )
  645. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  646. if( *p == '*' && p[1] == '/' )
  647. {// end of block comment
  648. p += 2;
  649. break;
  650. }
  651. ++p;
  652. }
  653. }
  654. else
  655. break;
  656. }
  657. return p;
  658. }
  659. /// Skips a word.
  660. /// A word consists of undercores and/or alfanumeric characters,
  661. /// and valid variable prefixes/postfixes.
  662. static
  663. const char* skip_word(const char* p)
  664. {
  665. // prefix
  666. switch( *p )
  667. {
  668. case '@':// temporary char variable
  669. ++p; break;
  670. case '#':// account variable
  671. p += ( p[1] == '#' ? 2 : 1 ); break;
  672. case '.':// npc variable
  673. p += ( p[1] == '@' ? 2 : 1 ); break;
  674. case '$':// global variable
  675. p += ( p[1] == '@' ? 2 : 1 ); break;
  676. }
  677. while( ISALNUM(*p) || *p == '_' )
  678. ++p;
  679. // postfix
  680. if( *p == '$' )// string
  681. p++;
  682. return p;
  683. }
  684. /// Adds a word to str_data.
  685. /// @see skip_word
  686. /// @see add_str
  687. static
  688. int add_word(const char* p)
  689. {
  690. char* word;
  691. int len;
  692. int i;
  693. // Check for a word
  694. len = skip_word(p) - p;
  695. if( len == 0 )
  696. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  697. // Duplicate the word
  698. CREATE(word, char, len+1);
  699. memcpy(word, p, len);
  700. word[len] = 0;
  701. // add the word
  702. i = add_str(word);
  703. aFree(word);
  704. return i;
  705. }
  706. /// Parses a function call.
  707. /// The argument list can have parenthesis or not.
  708. /// The number of arguments is checked.
  709. static
  710. const char* parse_callfunc(const char* p, int require_paren)
  711. {
  712. const char* p2;
  713. const char* arg=NULL;
  714. int func;
  715. func = add_word(p);
  716. if( str_data[func].type == C_FUNC ){
  717. // buildin function
  718. add_scriptl(func);
  719. add_scriptc(C_ARG);
  720. arg = buildin_func[str_data[func].val].arg;
  721. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  722. // script defined function
  723. int callsub = search_str("callsub");
  724. add_scriptl(callsub);
  725. add_scriptc(C_ARG);
  726. add_scriptl(func);
  727. arg = buildin_func[str_data[callsub].val].arg;
  728. if( *arg == 0 )
  729. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  730. if( *arg != '*' )
  731. ++arg; // count func as argument
  732. } else
  733. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  734. p = skip_word(p);
  735. p = skip_space(p);
  736. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  737. syntax.curly[syntax.curly_count].count = 0;
  738. if( *p == ';' )
  739. {// <func name> ';'
  740. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  741. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  742. {// <func name> '(' ')'
  743. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  744. p = p2;
  745. /*
  746. } else if( 0 && require_paren && *p != '(' )
  747. {// <func name>
  748. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  749. */
  750. } else
  751. {// <func name> <arg list>
  752. if( require_paren ){
  753. if( *p != '(' )
  754. disp_error_message("need '('",p);
  755. ++p; // skip '('
  756. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  757. } else if( *p == '(' ){
  758. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  759. } else {
  760. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  761. }
  762. ++syntax.curly_count;
  763. while( *arg ) {
  764. p2=parse_subexpr(p,-1);
  765. if( p == p2 )
  766. break; // not an argument
  767. if( *arg != '*' )
  768. ++arg; // next argument
  769. p=skip_space(p2);
  770. if( *arg == 0 || *p != ',' )
  771. break; // no more arguments
  772. ++p; // skip comma
  773. }
  774. --syntax.curly_count;
  775. }
  776. if( *arg && *arg != '?' && *arg != '*' )
  777. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  778. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  779. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  780. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  781. if( *p != ')' )
  782. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  783. ++p;
  784. }
  785. add_scriptc(C_FUNC);
  786. return p;
  787. }
  788. /*==========================================
  789. * 項の解析
  790. *------------------------------------------*/
  791. const char* parse_simpleexpr(const char *p)
  792. {
  793. int i;
  794. p=skip_space(p);
  795. #ifdef DEBUG_FUNCIN
  796. if(battle_config.etc_log)
  797. ShowDebug("parse_simpleexpr %s\n",p);
  798. #endif
  799. if(*p==';' || *p==',')
  800. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  801. if(*p=='('){
  802. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  803. ++syntax.curly[i].count;
  804. p=parse_subexpr(p+1,-1);
  805. p=skip_space(p);
  806. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  807. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  808. ){
  809. if( *p == ',' ){
  810. syntax.curly[i].flag = ARGLIST_PAREN;
  811. return p;
  812. } else
  813. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  814. }
  815. if( *p != ')' )
  816. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  817. ++p;
  818. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  819. char *np;
  820. i=strtoul(p,&np,0);
  821. add_scripti(i);
  822. p=np;
  823. } else if(*p=='"'){
  824. add_scriptc(C_STR);
  825. p++;
  826. while( *p && *p != '"' ){
  827. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  828. p++;
  829. else if( *p == '\n' )
  830. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  831. add_scriptb(*p++);
  832. }
  833. if(!*p)
  834. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  835. add_scriptb(0);
  836. p++; //'"'
  837. } else {
  838. int l;
  839. // label , register , function etc
  840. if(skip_word(p)==p)
  841. disp_error_message("parse_simpleexpr: unexpected character",p);
  842. l=add_word(p);
  843. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  844. return parse_callfunc(p,1);
  845. p=skip_word(p);
  846. if( *p == '[' ){
  847. // array(name[i] => getelementofarray(name,i) )
  848. add_scriptl(search_str("getelementofarray"));
  849. add_scriptc(C_ARG);
  850. add_scriptl(l);
  851. p=parse_subexpr(p+1,-1);
  852. p=skip_space(p);
  853. if( *p != ']' )
  854. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  855. ++p;
  856. add_scriptc(C_FUNC);
  857. }else
  858. add_scriptl(l);
  859. }
  860. #ifdef DEBUG_FUNCIN
  861. if(battle_config.etc_log)
  862. ShowDebug("parse_simpleexpr end %s\n",p);
  863. #endif
  864. return p;
  865. }
  866. /*==========================================
  867. * 式の解析
  868. *------------------------------------------*/
  869. const char* parse_subexpr(const char* p,int limit)
  870. {
  871. int op,opl,len;
  872. const char* tmpp;
  873. #ifdef DEBUG_FUNCIN
  874. if(battle_config.etc_log)
  875. ShowDebug("parse_subexpr %s\n",p);
  876. #endif
  877. p=skip_space(p);
  878. if(*p=='-'){
  879. tmpp=skip_space(p+1);
  880. if(*tmpp==';' || *tmpp==','){
  881. add_scriptl(LABEL_NEXTLINE);
  882. p++;
  883. return p;
  884. }
  885. }
  886. tmpp=p;
  887. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  888. p=parse_subexpr(p+1,10);
  889. add_scriptc(op);
  890. } else
  891. p=parse_simpleexpr(p);
  892. p=skip_space(p);
  893. while((
  894. (op=C_OP3,opl=0,len=1,*p=='?') ||
  895. (op=C_ADD,opl=8,len=1,*p=='+') ||
  896. (op=C_SUB,opl=8,len=1,*p=='-') ||
  897. (op=C_MUL,opl=9,len=1,*p=='*') ||
  898. (op=C_DIV,opl=9,len=1,*p=='/') ||
  899. (op=C_MOD,opl=9,len=1,*p=='%') ||
  900. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  901. (op=C_AND,opl=6,len=1,*p=='&') ||
  902. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  903. (op=C_OR,opl=5,len=1,*p=='|') ||
  904. (op=C_XOR,opl=4,len=1,*p=='^') ||
  905. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  906. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  907. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  908. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  909. (op=C_GT,opl=3,len=1,*p=='>') ||
  910. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  911. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  912. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  913. p+=len;
  914. if(op == C_OP3) {
  915. p=parse_subexpr(p,-1);
  916. p=skip_space(p);
  917. if( *(p++) != ':')
  918. disp_error_message("parse_subexpr: need ':'", p-1);
  919. p=parse_subexpr(p,-1);
  920. } else {
  921. p=parse_subexpr(p,opl);
  922. }
  923. add_scriptc(op);
  924. p=skip_space(p);
  925. }
  926. #ifdef DEBUG_FUNCIN
  927. if(battle_config.etc_log)
  928. ShowDebug("parse_subexpr end %s\n",p);
  929. #endif
  930. return p; /* return first untreated operator */
  931. }
  932. /*==========================================
  933. * 式の評価
  934. *------------------------------------------*/
  935. const char* parse_expr(const char *p)
  936. {
  937. #ifdef DEBUG_FUNCIN
  938. if(battle_config.etc_log)
  939. ShowDebug("parse_expr %s\n",p);
  940. #endif
  941. switch(*p){
  942. case ')': case ';': case ':': case '[': case ']':
  943. case '}':
  944. disp_error_message("parse_expr: unexpected char",p);
  945. }
  946. p=parse_subexpr(p,-1);
  947. #ifdef DEBUG_FUNCIN
  948. if(battle_config.etc_log)
  949. ShowDebug("parse_expr end %s\n",p);
  950. #endif
  951. return p;
  952. }
  953. /*==========================================
  954. * 行の解析
  955. *------------------------------------------*/
  956. const char* parse_line(const char* p)
  957. {
  958. const char* p2;
  959. p=skip_space(p);
  960. if(*p==';') {
  961. // if(); for(); while(); のために閉じ判定
  962. p = parse_syntax_close(p + 1);
  963. return p;
  964. }
  965. if(*p==')' && parse_syntax_for_flag)
  966. return p+1;
  967. p = skip_space(p);
  968. if(p[0] == '{') {
  969. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  970. syntax.curly[syntax.curly_count].count = -1;
  971. syntax.curly[syntax.curly_count].index = -1;
  972. syntax.curly_count++;
  973. return p + 1;
  974. } else if(p[0] == '}') {
  975. return parse_curly_close(p);
  976. }
  977. // 構文関連の処理
  978. p2 = parse_syntax(p);
  979. if(p2 != NULL)
  980. return p2;
  981. p = parse_callfunc(p,0);
  982. p = skip_space(p);
  983. if(parse_syntax_for_flag) {
  984. if( *p != ')' )
  985. disp_error_message("parse_line: need ')'",p);
  986. } else {
  987. if( *p != ';' )
  988. disp_error_message("parse_line: need ';'",p);
  989. }
  990. // if, for , while の閉じ判定
  991. p = parse_syntax_close(p+1);
  992. return p;
  993. }
  994. // { ... } の閉じ処理
  995. const char* parse_curly_close(const char* p)
  996. {
  997. if(syntax.curly_count <= 0) {
  998. disp_error_message("parse_curly_close: unexpected string",p);
  999. return p + 1;
  1000. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1001. syntax.curly_count--;
  1002. // if, for , while の閉じ判定
  1003. p = parse_syntax_close(p + 1);
  1004. return p;
  1005. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1006. // switch() 閉じ判定
  1007. int pos = syntax.curly_count-1;
  1008. char label[256];
  1009. int l;
  1010. // 一時変数を消す
  1011. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1012. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1013. parse_line(label);
  1014. syntax.curly_count--;
  1015. // 無条件で終了ポインタに移動
  1016. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1017. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1018. parse_line(label);
  1019. syntax.curly_count--;
  1020. // 現在地のラベルを付ける
  1021. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1022. l=add_str(label);
  1023. set_label(l,script_pos, p);
  1024. if(syntax.curly[pos].flag) {
  1025. // default が存在する
  1026. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1027. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1028. parse_line(label);
  1029. syntax.curly_count--;
  1030. }
  1031. // 終了ラベルを付ける
  1032. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1033. l=add_str(label);
  1034. set_label(l,script_pos, p);
  1035. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1036. syntax.curly_count--;
  1037. return p+1;
  1038. } else {
  1039. disp_error_message("parse_curly_close: unexpected string",p);
  1040. return p + 1;
  1041. }
  1042. }
  1043. // 構文関連の処理
  1044. // break, case, continue, default, do, for, function,
  1045. // if, switch, while をこの内部で処理します。
  1046. const char* parse_syntax(const char* p)
  1047. {
  1048. const char *p2 = skip_word(p);
  1049. switch(*p) {
  1050. case 'B':
  1051. case 'b':
  1052. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1053. // break の処理
  1054. char label[256];
  1055. int pos = syntax.curly_count - 1;
  1056. while(pos >= 0) {
  1057. if(syntax.curly[pos].type == TYPE_DO) {
  1058. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1059. break;
  1060. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1061. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1062. break;
  1063. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1064. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1065. break;
  1066. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1067. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1068. break;
  1069. }
  1070. pos--;
  1071. }
  1072. if(pos < 0) {
  1073. disp_error_message("parse_syntax: unexpected 'break'",p);
  1074. } else {
  1075. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1076. parse_line(label);
  1077. syntax.curly_count--;
  1078. }
  1079. p = skip_space(p2);
  1080. if(*p != ';')
  1081. disp_error_message("parse_syntax: need ';'",p);
  1082. // if, for , while の閉じ判定
  1083. p = parse_syntax_close(p + 1);
  1084. return p;
  1085. }
  1086. break;
  1087. case 'c':
  1088. case 'C':
  1089. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1090. // case の処理
  1091. int pos = syntax.curly_count-1;
  1092. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1093. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1094. return p+1;
  1095. } else {
  1096. char label[256];
  1097. int l,v;
  1098. char *np;
  1099. if(syntax.curly[pos].count != 1) {
  1100. // FALLTHRU 用のジャンプ
  1101. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1102. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1103. parse_line(label);
  1104. syntax.curly_count--;
  1105. // 現在地のラベルを付ける
  1106. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1107. l=add_str(label);
  1108. set_label(l,script_pos, p);
  1109. }
  1110. // switch 判定文
  1111. p = skip_space(p2);
  1112. if(p == p2) {
  1113. disp_error_message("parse_syntax: expect space ' '",p);
  1114. }
  1115. // check whether case label is integer or not
  1116. v = strtol(p,&np,0);
  1117. if(np == p) { //Check for constants
  1118. p2 = skip_word(p);
  1119. v = p2-p; // length of word at p2
  1120. memcpy(label,p,v);
  1121. label[v]='\0';
  1122. v = search_str(label);
  1123. if (v < 0 || str_data[v].type != C_INT)
  1124. disp_error_message("parse_syntax: 'case' label not integer",p);
  1125. v = str_data[v].val;
  1126. p = skip_word(p);
  1127. } else { //Numeric value
  1128. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1129. p++;
  1130. p = skip_word(p);
  1131. if(np != p)
  1132. disp_error_message("parse_syntax: 'case' label not integer",np);
  1133. }
  1134. p = skip_space(p);
  1135. if(*p != ':')
  1136. disp_error_message("parse_syntax: expect ':'",p);
  1137. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1138. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1139. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1140. // 2回parse しないとダメ
  1141. p2 = parse_line(label);
  1142. parse_line(p2);
  1143. syntax.curly_count--;
  1144. if(syntax.curly[pos].count != 1) {
  1145. // FALLTHRU 終了後のラベル
  1146. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1147. l=add_str(label);
  1148. set_label(l,script_pos,p);
  1149. }
  1150. // check duplication of case label [Rayce]
  1151. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1152. disp_error_message("parse_syntax: dup 'case'",p);
  1153. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1154. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1155. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1156. parse_line(label);
  1157. syntax.curly_count--;
  1158. syntax.curly[pos].count++;
  1159. }
  1160. return p + 1;
  1161. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1162. // continue の処理
  1163. char label[256];
  1164. int pos = syntax.curly_count - 1;
  1165. while(pos >= 0) {
  1166. if(syntax.curly[pos].type == TYPE_DO) {
  1167. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1168. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1169. break;
  1170. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1171. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1172. break;
  1173. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1174. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1175. break;
  1176. }
  1177. pos--;
  1178. }
  1179. if(pos < 0) {
  1180. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1181. } else {
  1182. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1183. parse_line(label);
  1184. syntax.curly_count--;
  1185. }
  1186. p = skip_space(p2);
  1187. if(*p != ';')
  1188. disp_error_message("parse_syntax: need ';'",p);
  1189. // if, for , while の閉じ判定
  1190. p = parse_syntax_close(p + 1);
  1191. return p;
  1192. }
  1193. break;
  1194. case 'd':
  1195. case 'D':
  1196. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1197. // switch - default の処理
  1198. int pos = syntax.curly_count-1;
  1199. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1200. disp_error_message("parse_syntax: unexpected 'default'",p);
  1201. } else if(syntax.curly[pos].flag) {
  1202. disp_error_message("parse_syntax: dup 'default'",p);
  1203. } else {
  1204. char label[256];
  1205. int l;
  1206. // 現在地のラベルを付ける
  1207. p = skip_space(p2);
  1208. if(*p != ':') {
  1209. disp_error_message("parse_syntax: need ':'",p);
  1210. }
  1211. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1212. l=add_str(label);
  1213. set_label(l,script_pos,p);
  1214. // 無条件で次のリンクに飛ばす
  1215. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1216. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1217. parse_line(label);
  1218. syntax.curly_count--;
  1219. // default のラベルを付ける
  1220. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1221. l=add_str(label);
  1222. set_label(l,script_pos,p);
  1223. syntax.curly[syntax.curly_count - 1].flag = 1;
  1224. syntax.curly[pos].count++;
  1225. }
  1226. return p + 1;
  1227. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1228. int l;
  1229. char label[256];
  1230. p=skip_space(p2);
  1231. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1232. syntax.curly[syntax.curly_count].count = 1;
  1233. syntax.curly[syntax.curly_count].index = syntax.index++;
  1234. syntax.curly[syntax.curly_count].flag = 0;
  1235. // 現在地のラベル形成する
  1236. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1237. l=add_str(label);
  1238. set_label(l,script_pos,p);
  1239. syntax.curly_count++;
  1240. return p;
  1241. }
  1242. break;
  1243. case 'f':
  1244. case 'F':
  1245. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1246. int l;
  1247. char label[256];
  1248. int pos = syntax.curly_count;
  1249. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1250. syntax.curly[syntax.curly_count].count = 1;
  1251. syntax.curly[syntax.curly_count].index = syntax.index++;
  1252. syntax.curly[syntax.curly_count].flag = 0;
  1253. syntax.curly_count++;
  1254. p=skip_space(p2);
  1255. if(*p != '(')
  1256. disp_error_message("parse_syntax: need '('",p);
  1257. p++;
  1258. // 初期化文を実行する
  1259. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1260. p=parse_line(p);
  1261. syntax.curly_count--;
  1262. // 条件判断開始のラベル形成する
  1263. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1264. l=add_str(label);
  1265. set_label(l,script_pos,p);
  1266. p=skip_space(p);
  1267. if(*p == ';') {
  1268. // for(;;) のパターンなので必ず真
  1269. ;
  1270. } else {
  1271. // 条件が偽なら終了地点に飛ばす
  1272. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1273. add_scriptl(add_str("jump_zero"));
  1274. add_scriptc(C_ARG);
  1275. p=parse_expr(p);
  1276. p=skip_space(p);
  1277. add_scriptl(add_str(label));
  1278. add_scriptc(C_FUNC);
  1279. }
  1280. if(*p != ';')
  1281. disp_error_message("parse_syntax: need ';'",p);
  1282. p++;
  1283. // ループ開始に飛ばす
  1284. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1285. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1286. parse_line(label);
  1287. syntax.curly_count--;
  1288. // 次のループへのラベル形成する
  1289. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1290. l=add_str(label);
  1291. set_label(l,script_pos,p);
  1292. // 次のループに入る時の処理
  1293. // for 最後の ')' を ';' として扱うフラグ
  1294. parse_syntax_for_flag = 1;
  1295. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1296. p=parse_line(p);
  1297. syntax.curly_count--;
  1298. parse_syntax_for_flag = 0;
  1299. // 条件判定処理に飛ばす
  1300. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1301. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1302. parse_line(label);
  1303. syntax.curly_count--;
  1304. // ループ開始のラベル付け
  1305. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1306. l=add_str(label);
  1307. set_label(l,script_pos,p);
  1308. return p;
  1309. }
  1310. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1311. {// internal script function
  1312. const char *func_name;
  1313. func_name = skip_space(p2);
  1314. p = skip_word(func_name);
  1315. if( p == func_name )
  1316. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1317. p2 = skip_space(p);
  1318. if( *p2 == ';' )
  1319. {// function <name> ;
  1320. // function declaration - just register the name
  1321. int l;
  1322. l = add_word(func_name);
  1323. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1324. str_data[l].type = C_USERFUNC;
  1325. // if, for , while の閉じ判定
  1326. p = parse_syntax_close(p2 + 1);
  1327. return p; }
  1328. else if(*p2 == '{')
  1329. {// function <name> <line/block of code>
  1330. char label[256];
  1331. int l;
  1332. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1333. syntax.curly[syntax.curly_count].count = 1;
  1334. syntax.curly[syntax.curly_count].index = syntax.index++;
  1335. syntax.curly[syntax.curly_count].flag = 0;
  1336. ++syntax.curly_count;
  1337. // Jump over the function code
  1338. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1339. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1340. ++syntax.curly_count;
  1341. parse_line(label);
  1342. --syntax.curly_count;
  1343. // Set the position of the function (label)
  1344. l=add_word(func_name);
  1345. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1346. str_data[l].type = C_USERFUNC;
  1347. set_label(l, script_pos, p);
  1348. if( parse_options&SCRIPT_USE_LABEL_DB )
  1349. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1350. return skip_space(p);
  1351. }
  1352. else
  1353. {
  1354. disp_error_message("expect ';' or '{' at function syntax",p);
  1355. }
  1356. }
  1357. break;
  1358. case 'i':
  1359. case 'I':
  1360. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1361. // if() の処理
  1362. char label[256];
  1363. p=skip_space(p2);
  1364. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1365. disp_error_message("need '('",p);
  1366. }
  1367. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1368. syntax.curly[syntax.curly_count].count = 1;
  1369. syntax.curly[syntax.curly_count].index = syntax.index++;
  1370. syntax.curly[syntax.curly_count].flag = 0;
  1371. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1372. syntax.curly_count++;
  1373. add_scriptl(add_str("jump_zero"));
  1374. add_scriptc(C_ARG);
  1375. p=parse_expr(p);
  1376. p=skip_space(p);
  1377. add_scriptl(add_str(label));
  1378. add_scriptc(C_FUNC);
  1379. return p;
  1380. }
  1381. break;
  1382. case 's':
  1383. case 'S':
  1384. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1385. // switch() の処理
  1386. char label[256];
  1387. p=skip_space(p2);
  1388. if(*p != '(') {
  1389. disp_error_message("need '('",p);
  1390. }
  1391. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1392. syntax.curly[syntax.curly_count].count = 1;
  1393. syntax.curly[syntax.curly_count].index = syntax.index++;
  1394. syntax.curly[syntax.curly_count].flag = 0;
  1395. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1396. syntax.curly_count++;
  1397. add_scriptl(add_str("set"));
  1398. add_scriptc(C_ARG);
  1399. add_scriptl(add_str(label));
  1400. p=parse_expr(p);
  1401. p=skip_space(p);
  1402. if(*p != '{') {
  1403. disp_error_message("parse_syntax: need '{'",p);
  1404. }
  1405. add_scriptc(C_FUNC);
  1406. return p + 1;
  1407. }
  1408. break;
  1409. case 'w':
  1410. case 'W':
  1411. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1412. int l;
  1413. char label[256];
  1414. p=skip_space(p2);
  1415. if(*p != '(') {
  1416. disp_error_message("need '('",p);
  1417. }
  1418. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1419. syntax.curly[syntax.curly_count].count = 1;
  1420. syntax.curly[syntax.curly_count].index = syntax.index++;
  1421. syntax.curly[syntax.curly_count].flag = 0;
  1422. // 条件判断開始のラベル形成する
  1423. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1424. l=add_str(label);
  1425. set_label(l,script_pos,p);
  1426. // 条件が偽なら終了地点に飛ばす
  1427. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1428. syntax.curly_count++;
  1429. add_scriptl(add_str("jump_zero"));
  1430. add_scriptc(C_ARG);
  1431. p=parse_expr(p);
  1432. p=skip_space(p);
  1433. add_scriptl(add_str(label));
  1434. add_scriptc(C_FUNC);
  1435. return p;
  1436. }
  1437. break;
  1438. }
  1439. return NULL;
  1440. }
  1441. const char* parse_syntax_close(const char *p) {
  1442. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1443. int flag;
  1444. do {
  1445. p = parse_syntax_close_sub(p,&flag);
  1446. } while(flag);
  1447. return p;
  1448. }
  1449. // if, for , while , do の閉じ判定
  1450. // flag == 1 : 閉じられた
  1451. // flag == 0 : 閉じられない
  1452. const char* parse_syntax_close_sub(const char* p,int* flag)
  1453. {
  1454. char label[256];
  1455. int pos = syntax.curly_count - 1;
  1456. int l;
  1457. *flag = 1;
  1458. if(syntax.curly_count <= 0) {
  1459. *flag = 0;
  1460. return p;
  1461. } else if(syntax.curly[pos].type == TYPE_IF) {
  1462. const char *bp = p;
  1463. const char *p2;
  1464. // if 最終場所へ飛ばす
  1465. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1466. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1467. parse_line(label);
  1468. syntax.curly_count--;
  1469. // 現在地のラベルを付ける
  1470. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1471. l=add_str(label);
  1472. set_label(l,script_pos,p);
  1473. syntax.curly[pos].count++;
  1474. p = skip_space(p);
  1475. p2 = skip_word(p);
  1476. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1477. // else or else - if
  1478. p = skip_space(p2);
  1479. p2 = skip_word(p);
  1480. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1481. // else - if
  1482. p=skip_space(p2);
  1483. if(*p != '(') {
  1484. disp_error_message("need '('",p);
  1485. }
  1486. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1487. add_scriptl(add_str("jump_zero"));
  1488. add_scriptc(C_ARG);
  1489. p=parse_expr(p);
  1490. p=skip_space(p);
  1491. add_scriptl(add_str(label));
  1492. add_scriptc(C_FUNC);
  1493. *flag = 0;
  1494. return p;
  1495. } else {
  1496. // else
  1497. if(!syntax.curly[pos].flag) {
  1498. syntax.curly[pos].flag = 1;
  1499. *flag = 0;
  1500. return p;
  1501. }
  1502. }
  1503. }
  1504. // if 閉じ
  1505. syntax.curly_count--;
  1506. // 最終地のラベルを付ける
  1507. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1508. l=add_str(label);
  1509. set_label(l,script_pos,p);
  1510. if(syntax.curly[pos].flag == 1) {
  1511. // このifに対するelseじゃないのでポインタの位置は同じ
  1512. return bp;
  1513. }
  1514. return p;
  1515. } else if(syntax.curly[pos].type == TYPE_DO) {
  1516. int l;
  1517. char label[256];
  1518. const char *p2;
  1519. if(syntax.curly[pos].flag) {
  1520. // 現在地のラベル形成する(continue でここに来る)
  1521. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1522. l=add_str(label);
  1523. set_label(l,script_pos,p);
  1524. }
  1525. // 条件が偽なら終了地点に飛ばす
  1526. p = skip_space(p);
  1527. p2 = skip_word(p);
  1528. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1529. disp_error_message("parse_syntax: need 'while'",p);
  1530. p = skip_space(p2);
  1531. if(*p != '(') {
  1532. disp_error_message("need '('",p);
  1533. }
  1534. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1535. add_scriptl(add_str("jump_zero"));
  1536. add_scriptc(C_ARG);
  1537. p=parse_expr(p);
  1538. p=skip_space(p);
  1539. add_scriptl(add_str(label));
  1540. add_scriptc(C_FUNC);
  1541. // 開始地点に飛ばす
  1542. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1543. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1544. parse_line(label);
  1545. syntax.curly_count--;
  1546. // 条件終了地点のラベル形成する
  1547. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1548. l=add_str(label);
  1549. set_label(l,script_pos,p);
  1550. p = skip_space(p);
  1551. if(*p != ';') {
  1552. disp_error_message("parse_syntax: need ';'",p);
  1553. return p+1;
  1554. }
  1555. p++;
  1556. syntax.curly_count--;
  1557. return p;
  1558. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1559. // 次のループに飛ばす
  1560. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1561. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1562. parse_line(label);
  1563. syntax.curly_count--;
  1564. // for 終了のラベル付け
  1565. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1566. l=add_str(label);
  1567. set_label(l,script_pos,p);
  1568. syntax.curly_count--;
  1569. return p;
  1570. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1571. // while 条件判断へ飛ばす
  1572. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1573. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1574. parse_line(label);
  1575. syntax.curly_count--;
  1576. // while 終了のラベル付け
  1577. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1578. l=add_str(label);
  1579. set_label(l,script_pos,p);
  1580. syntax.curly_count--;
  1581. return p;
  1582. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1583. int pos = syntax.curly_count-1;
  1584. char label[256];
  1585. int l;
  1586. // 戻す
  1587. sprintf(label,"return;");
  1588. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1589. parse_line(label);
  1590. syntax.curly_count--;
  1591. // 現在地のラベルを付ける
  1592. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1593. l=add_str(label);
  1594. set_label(l,script_pos,p);
  1595. syntax.curly_count--;
  1596. return p;
  1597. } else {
  1598. *flag = 0;
  1599. return p;
  1600. }
  1601. }
  1602. /*==========================================
  1603. * 組み込み関数の追加
  1604. *------------------------------------------*/
  1605. static void add_buildin_func(void)
  1606. {
  1607. int i,n;
  1608. const char* p;
  1609. for( i = 0; buildin_func[i].func; i++ )
  1610. {
  1611. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1612. // 'v' - value (either string or int or reference)
  1613. // 's' - string
  1614. // 'i' - int
  1615. // 'r' - reference (of a variable)
  1616. // 'l' - label
  1617. // '?' - one optional parameter
  1618. // '*' - unknown number of optional parameters
  1619. p = buildin_func[i].arg;
  1620. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1621. while( *p == '?' ) ++p;
  1622. if( *p == '*' ) ++p;
  1623. if( *p != 0){
  1624. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1625. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1626. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1627. } else {
  1628. n = add_str(buildin_func[i].name);
  1629. str_data[n].type = C_FUNC;
  1630. str_data[n].val = i;
  1631. str_data[n].func = buildin_func[i].func;
  1632. }
  1633. }
  1634. }
  1635. /*==========================================
  1636. * 定数データベースの読み込み
  1637. *------------------------------------------*/
  1638. static void read_constdb(void)
  1639. {
  1640. FILE *fp;
  1641. char line[1024],name[1024],val[1024];
  1642. int n,type;
  1643. sprintf(line, "%s/const.txt", db_path);
  1644. fp=fopen(line, "r");
  1645. if(fp==NULL){
  1646. ShowError("can't read %s\n", line);
  1647. return ;
  1648. }
  1649. while(fgets(line, sizeof(line), fp))
  1650. {
  1651. if(line[0]=='/' && line[1]=='/')
  1652. continue;
  1653. type=0;
  1654. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1655. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1656. n=add_str(name);
  1657. if(type==0)
  1658. str_data[n].type=C_INT;
  1659. else
  1660. str_data[n].type=C_PARAM;
  1661. str_data[n].val= (int)strtol(val,NULL,0);
  1662. }
  1663. }
  1664. fclose(fp);
  1665. }
  1666. /*==========================================
  1667. * エラー表示
  1668. *------------------------------------------*/
  1669. const char* script_print_line( const char *p, const char *mark, int line )
  1670. {
  1671. int i;
  1672. if( p == NULL || !p[0] ) return NULL;
  1673. if( line < 0 )
  1674. printf("*% 5d : ", -line);
  1675. else
  1676. printf(" % 5d : ", line);
  1677. for(i=0;p[i] && p[i] != '\n';i++){
  1678. if(p + i != mark)
  1679. printf("%c",p[i]);
  1680. else
  1681. printf("\'%c\'",p[i]);
  1682. }
  1683. printf("\n");
  1684. return p+i+(p[i] == '\n' ? 1 : 0);
  1685. }
  1686. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos)
  1687. {
  1688. // エラーが発生した行を求める
  1689. int j;
  1690. int line = start_line;
  1691. const char *p;
  1692. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1693. for(p=src;p && *p;line++){
  1694. char *lineend=strchr(p,'\n');
  1695. if(lineend==NULL || error_pos<lineend){
  1696. break;
  1697. }
  1698. for( j = 0; j < 4; j++ ) {
  1699. linestart[j] = linestart[j+1];
  1700. }
  1701. linestart[4] = p;
  1702. p=lineend+1;
  1703. }
  1704. printf("\a\n");
  1705. printf("script error on %s line %d\n", file, line);
  1706. printf(" %s\n", error_msg);
  1707. for(j = 0; j < 5; j++ ) {
  1708. script_print_line( linestart[j], NULL, line + j - 5);
  1709. }
  1710. p = script_print_line( p, error_pos, -line);
  1711. for(j = 0; j < 5; j++) {
  1712. p = script_print_line( p, NULL, line + j + 1 );
  1713. }
  1714. }
  1715. /*==========================================
  1716. * スクリプトの解析
  1717. *------------------------------------------*/
  1718. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1719. {
  1720. const char *p,*tmpp;
  1721. int i;
  1722. struct script_code* code = NULL;
  1723. static int first=1;
  1724. char end;
  1725. if( src == NULL )
  1726. return NULL;// empty script
  1727. memset(&syntax,0,sizeof(syntax));
  1728. if(first){
  1729. add_buildin_func();
  1730. read_constdb();
  1731. }
  1732. first=0;
  1733. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1734. script_pos=0;
  1735. script_size=SCRIPT_BLOCK_SIZE;
  1736. str_data[LABEL_NEXTLINE].type=C_NOP;
  1737. str_data[LABEL_NEXTLINE].backpatch=-1;
  1738. str_data[LABEL_NEXTLINE].label=-1;
  1739. for(i=LABEL_START;i<str_num;i++){
  1740. if(
  1741. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1742. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1743. ){
  1744. str_data[i].type=C_NOP;
  1745. str_data[i].backpatch=-1;
  1746. str_data[i].label=-1;
  1747. }
  1748. }
  1749. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1750. if( options&SCRIPT_USE_LABEL_DB )
  1751. scriptlabel_db->clear(scriptlabel_db, NULL);
  1752. parse_options = options;
  1753. if( setjmp( error_jump ) != 0 ) {
  1754. //Restore program state when script has problems. [from jA]
  1755. int i;
  1756. const int size = ARRAYLENGTH(syntax.curly);
  1757. if( error_report )
  1758. script_error(src,file,line,error_msg,error_pos);
  1759. aFree( error_msg );
  1760. aFree( script_buf );
  1761. script_pos = 0;
  1762. script_size = 0;
  1763. script_buf = NULL;
  1764. for(i=LABEL_START;i<str_num;i++)
  1765. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1766. for(i=0; i<size; i++)
  1767. linkdb_final(&syntax.curly[i].case_label);
  1768. return NULL;
  1769. }
  1770. parse_syntax_for_flag=0;
  1771. p=src;
  1772. p=skip_space(p);
  1773. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1774. {// does not require brackets around the script
  1775. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1776. {// empty script and can return NULL
  1777. aFree( script_buf );
  1778. script_pos = 0;
  1779. script_size = 0;
  1780. script_buf = NULL;
  1781. return NULL;
  1782. }
  1783. end = '\0';
  1784. }
  1785. else
  1786. {// requires brackets around the script
  1787. if( *p != '{' )
  1788. disp_error_message("not found '{'",p);
  1789. p = skip_space(p+1);
  1790. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1791. {// empty script and can return NULL
  1792. aFree( script_buf );
  1793. script_pos = 0;
  1794. script_size = 0;
  1795. script_buf = NULL;
  1796. return NULL;
  1797. }
  1798. end = '}';
  1799. }
  1800. while( syntax.curly_count != 0 || *p != end )
  1801. {
  1802. if( *p == '\0' )
  1803. disp_error_message("unexpected end of script",p);
  1804. // labelだけ特殊処理
  1805. tmpp=skip_space(skip_word(p));
  1806. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1807. i=add_word(p);
  1808. set_label(i,script_pos,p);
  1809. if( parse_options&SCRIPT_USE_LABEL_DB )
  1810. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1811. p=tmpp+1;
  1812. p=skip_space(p);
  1813. continue;
  1814. }
  1815. // 他は全部一緒くた
  1816. p=parse_line(p);
  1817. p=skip_space(p);
  1818. add_scriptc(C_EOL);
  1819. set_label(LABEL_NEXTLINE,script_pos,p);
  1820. str_data[LABEL_NEXTLINE].type=C_NOP;
  1821. str_data[LABEL_NEXTLINE].backpatch=-1;
  1822. str_data[LABEL_NEXTLINE].label=-1;
  1823. }
  1824. add_scriptc(C_NOP);
  1825. script_size = script_pos;
  1826. RECREATE(script_buf,unsigned char,script_pos);
  1827. // 未解決のラベルを解決
  1828. for(i=LABEL_START;i<str_num;i++){
  1829. if(str_data[i].type==C_NOP){
  1830. int j,next;
  1831. str_data[i].type=C_NAME;
  1832. str_data[i].label=i;
  1833. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1834. next=GETVALUE(script_buf,j);
  1835. SETVALUE(script_buf,j,i);
  1836. j=next;
  1837. }
  1838. }
  1839. }
  1840. #ifdef DEBUG_DISP
  1841. for(i=0;i<script_pos;i++){
  1842. if((i&15)==0) printf("%04x : ",i);
  1843. printf("%02x ",script_buf[i]);
  1844. if((i&15)==15) printf("\n");
  1845. }
  1846. printf("\n");
  1847. #endif
  1848. #ifdef DEBUG_DISASM
  1849. {
  1850. int i = 0,j;
  1851. while(i < script_pos) {
  1852. printf("%06x ",i);
  1853. j = i;
  1854. switch(get_com(script_buf,&i)) {
  1855. case C_EOL: printf("C_EOL"); break;
  1856. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1857. case C_POS:
  1858. printf("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1859. i += 3;
  1860. break;
  1861. case C_NAME:
  1862. j = (*(int*)(script_buf+i)&0xffffff);
  1863. printf("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1864. i += 3;
  1865. break;
  1866. case C_ARG: printf("C_ARG"); break;
  1867. case C_FUNC: printf("C_FUNC"); break;
  1868. case C_ADD: printf("C_ADD"); break;
  1869. case C_SUB: printf("C_SUB"); break;
  1870. case C_MUL: printf("C_MUL"); break;
  1871. case C_DIV: printf("C_DIV"); break;
  1872. case C_MOD: printf("C_MOD"); break;
  1873. case C_EQ: printf("C_EQ"); break;
  1874. case C_NE: printf("C_NE"); break;
  1875. case C_GT: printf("C_GT"); break;
  1876. case C_GE: printf("C_GE"); break;
  1877. case C_LT: printf("C_LT"); break;
  1878. case C_LE: printf("C_LE"); break;
  1879. case C_AND: printf("C_AND"); break;
  1880. case C_OR: printf("C_OR"); break;
  1881. case C_XOR: printf("C_XOR"); break;
  1882. case C_LAND: printf("C_LAND"); break;
  1883. case C_LOR: printf("C_LOR"); break;
  1884. case C_R_SHIFT: printf("C_R_SHIFT"); break;
  1885. case C_L_SHIFT: printf("C_L_SHIFT"); break;
  1886. case C_NEG: printf("C_NEG"); break;
  1887. case C_NOT: printf("C_NOT"); break;
  1888. case C_LNOT: printf("C_LNOT"); break;
  1889. case C_NOP: printf("C_NOP"); break;
  1890. case C_OP3: printf("C_OP3"); break;
  1891. case C_STR:
  1892. j = strlen(script_buf + i);
  1893. printf("C_STR %s",script_buf + i);
  1894. i+= j+1;
  1895. break;
  1896. default:
  1897. printf("unknown");
  1898. }
  1899. printf("\n");
  1900. }
  1901. }
  1902. #endif
  1903. CREATE(code,struct script_code,1);
  1904. code->script_buf = script_buf;
  1905. code->script_size = script_size;
  1906. code->script_vars = NULL;
  1907. return code;
  1908. }
  1909. //
  1910. // Script state
  1911. //
  1912. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1913. /// Returns the player attached to this script, identified by the rid.
  1914. /// If there is no player attached, the script is terminated.
  1915. TBL_PC *script_rid2sd(struct script_state *st)
  1916. {
  1917. TBL_PC *sd=map_id2sd(st->rid);
  1918. if(!sd){
  1919. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1920. st->state = END;
  1921. }
  1922. return sd;
  1923. }
  1924. /// Retrieves the value of a script data
  1925. int get_val(struct script_state* st, struct script_data* data)
  1926. {
  1927. char* name;
  1928. char prefix;
  1929. char postfix;
  1930. TBL_PC* sd = NULL;
  1931. if( !data_isreference(data) )
  1932. return 0;// not a variable
  1933. name = reference_getname(data);
  1934. prefix = name[0];
  1935. postfix = name[strlen(name) - 1];
  1936. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1937. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1938. {
  1939. sd = script_rid2sd(st);
  1940. if( sd == NULL )
  1941. {// needs player attached
  1942. if( postfix == '$' )
  1943. {// string variable
  1944. ShowError("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1945. data->type = C_CONSTSTR;
  1946. data->u.str = "";
  1947. }
  1948. else
  1949. {// integer variable
  1950. ShowError("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1951. data->type = C_INT;
  1952. data->u.num = 0;
  1953. }
  1954. return 0;
  1955. }
  1956. }
  1957. if( postfix == '$' )
  1958. {// string variable
  1959. data->type = C_CONSTSTR;
  1960. switch( prefix )
  1961. {
  1962. case '@':
  1963. data->u.str = pc_readregstr(sd, data->u.num);
  1964. break;
  1965. case '$':
  1966. data->u.str = (char *)idb_get(mapregstr_db, data->u.num);
  1967. break;
  1968. case '#':
  1969. if( name[1] == '#' )
  1970. data->u.str = pc_readaccountreg2str(sd, name);// global
  1971. else
  1972. data->u.str = pc_readaccountregstr(sd, name);// local
  1973. break;
  1974. case '.':
  1975. {
  1976. struct linkdb_node** n =
  1977. data->ref ? data->ref:
  1978. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  1979. &st->script->script_vars;// npc variable
  1980. data->u.str = linkdb_search(n, (void*)reference_getuid(data));
  1981. }
  1982. break;
  1983. default:
  1984. data->u.str = pc_readglobalreg_str(sd, name);
  1985. break;
  1986. }
  1987. if( data->u.str == NULL )
  1988. data->u.str = "";
  1989. }
  1990. else
  1991. {// integer variable
  1992. data->type = C_INT;
  1993. if( reference_toconstant(data) )
  1994. {
  1995. data->u.num = reference_getconstant(data);
  1996. }
  1997. else if( reference_toparam(data) )
  1998. {
  1999. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2000. }
  2001. else
  2002. switch( prefix )
  2003. {
  2004. case '@':
  2005. data->u.num = pc_readreg(sd, data->u.num);
  2006. break;
  2007. case '$':
  2008. data->u.num = (int)idb_get(mapreg_db, data->u.num);
  2009. break;
  2010. case '#':
  2011. if( name[1] == '#' )
  2012. data->u.num = pc_readaccountreg2(sd, name);// global
  2013. else
  2014. data->u.num = pc_readaccountreg(sd, name);// local
  2015. break;
  2016. case '.':
  2017. {
  2018. struct linkdb_node** n =
  2019. data->ref ? data->ref:
  2020. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2021. &st->script->script_vars;// npc variable
  2022. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2023. }
  2024. break;
  2025. default:
  2026. data->u.num = pc_readglobalreg(sd, name);
  2027. break;
  2028. }
  2029. }
  2030. return 0;
  2031. }
  2032. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2033. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2034. {
  2035. struct script_data data;
  2036. data.type = C_NAME;
  2037. data.u.num = uid;
  2038. data.ref = ref;
  2039. get_val(st, &data);
  2040. return (data.type == C_INT ? (void*)data.u.num : (void*)data.u.str);
  2041. }
  2042. /*==========================================
  2043. * Stores the value of a script variable
  2044. * Return value is 0 on fail, 1 on success.
  2045. *------------------------------------------*/
  2046. static int set_reg(struct script_state* st, TBL_PC* sd, int num, char* name, void* value, struct linkdb_node** ref)
  2047. {
  2048. char prefix = name[0]; char postfix = name[strlen(name)-1];
  2049. if (postfix == '$') { // string variable
  2050. char* str = (char*)value;
  2051. switch (prefix) {
  2052. case '@':
  2053. return pc_setregstr(sd, num, str);
  2054. case '$':
  2055. return mapreg_setregstr(num, str);
  2056. case '#':
  2057. return (name[1] == '#') ?
  2058. pc_setaccountreg2str(sd, name, str) :
  2059. pc_setaccountregstr(sd, name, str);
  2060. case '.': {
  2061. char* p;
  2062. struct linkdb_node** n;
  2063. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2064. p = linkdb_search(n, (void*)num);
  2065. if (p) {
  2066. linkdb_erase(n, (void*)num);
  2067. aFree(p);
  2068. }
  2069. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2070. }
  2071. return 1;
  2072. default:
  2073. return pc_setglobalreg_str(sd, name, str);
  2074. }
  2075. } else { // integer variable
  2076. int val = (int)value;
  2077. if(str_data[num&0x00ffffff].type == C_PARAM)
  2078. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2079. switch (prefix) {
  2080. case '@':
  2081. return pc_setreg(sd, num, val);
  2082. case '$':
  2083. return mapreg_setreg(num, val);
  2084. case '#':
  2085. return (name[1] == '#') ?
  2086. pc_setaccountreg2(sd, name, val) :
  2087. pc_setaccountreg(sd, name, val);
  2088. case '.': {
  2089. struct linkdb_node** n;
  2090. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2091. if (val == 0)
  2092. linkdb_erase(n, (void*)num);
  2093. else
  2094. linkdb_replace(n, (void*)num, (void*)val);
  2095. }
  2096. return 1;
  2097. default:
  2098. return pc_setglobalreg(sd, name, val);
  2099. }
  2100. }
  2101. }
  2102. int set_var(TBL_PC* sd, char* name, void* val)
  2103. {
  2104. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  2105. }
  2106. /// Converts the data to a string
  2107. const char* conv_str(struct script_state* st, struct script_data* data)
  2108. {
  2109. char* p;
  2110. get_val(st, data);
  2111. if( data_isstring(data) )
  2112. {// nothing to convert
  2113. }
  2114. else if( data_isint(data) )
  2115. {// int -> string
  2116. CREATE(p, char, ITEM_NAME_LENGTH);
  2117. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2118. p[ITEM_NAME_LENGTH-1] = '\0';
  2119. data->type = C_STR;
  2120. data->u.str = p;
  2121. }
  2122. else if( data_isreference(data) )
  2123. {// reference -> string
  2124. //##TODO when does this happen (check get_val) [FlavioJS]
  2125. data->type = C_CONSTSTR;
  2126. data->u.str = reference_getname(data);
  2127. }
  2128. else
  2129. {// unsupported data type
  2130. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2131. script_reportdata(data);
  2132. script_reportsrc(st);
  2133. data->type = C_CONSTSTR;
  2134. data->u.str = "";
  2135. }
  2136. return data->u.str;
  2137. }
  2138. /// Converts the data to an int
  2139. int conv_num(struct script_state* st, struct script_data* data)
  2140. {
  2141. char* p;
  2142. long num;
  2143. get_val(st, data);
  2144. if( data_isint(data) )
  2145. {// nothing to convert
  2146. }
  2147. else if( data_isstring(data) )
  2148. {// string -> int
  2149. // the result does not overflow or underflow, it is capped instead
  2150. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2151. p = data->u.str;
  2152. errno = 0;
  2153. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2154. if( errno == ERANGE
  2155. #if LONG_MAX > INT_MAX
  2156. || num < INT_MIN || num > INT_MAX
  2157. #endif
  2158. )
  2159. {
  2160. if( num <= INT_MIN )
  2161. {
  2162. num = INT_MIN;
  2163. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2164. }
  2165. else//if( num >= INT_MAX )
  2166. {
  2167. num = INT_MAX;
  2168. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2169. }
  2170. script_reportdata(data);
  2171. script_reportsrc(st);
  2172. }
  2173. if( data->type == C_STR )
  2174. aFree(p);
  2175. data->type = C_INT;
  2176. data->u.num = (int)num;
  2177. }
  2178. #if 0
  2179. // FIXME this function is being used to retrieve the position of labels and
  2180. // probably other stuff [FlavioJS]
  2181. else
  2182. {// unsupported data type
  2183. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2184. script_reportdata(data);
  2185. script_reportsrc(st);
  2186. data->type = C_INT;
  2187. data->u.num = 0;
  2188. }
  2189. #endif
  2190. return data->u.num;
  2191. }
  2192. //
  2193. // Stack operations
  2194. //
  2195. /// Increases the size of the stack
  2196. void stack_expand(struct script_stack* stack)
  2197. {
  2198. stack->sp_max += 64;
  2199. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2200. stack->sp_max * sizeof(stack->stack_data[0]) );
  2201. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2202. 64 * sizeof(stack->stack_data[0]) );
  2203. }
  2204. /// Pushes a value into the stack
  2205. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2206. /// Pushes a value into the stack (with reference)
  2207. void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref)
  2208. {
  2209. if( stack->sp >= stack->sp_max )
  2210. stack_expand(stack);
  2211. stack->stack_data[stack->sp].type = type;
  2212. stack->stack_data[stack->sp].u.num = val;
  2213. stack->stack_data[stack->sp].ref = ref;
  2214. stack->sp++;
  2215. }
  2216. /// Pushes a string into the stack
  2217. void push_str(struct script_stack* stack, int type, char* str)
  2218. {
  2219. if( stack->sp >= stack->sp_max )
  2220. stack_expand(stack);
  2221. stack->stack_data[stack->sp].type = type;
  2222. stack->stack_data[stack->sp].u.str = str;
  2223. stack->stack_data[stack->sp].ref = NULL;
  2224. stack->sp++;
  2225. }
  2226. /// Pushes a copy of the target position into the stack
  2227. void push_copy(struct script_stack* stack, int pos)
  2228. {
  2229. switch( stack->stack_data[pos].type )
  2230. {
  2231. case C_CONSTSTR:
  2232. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2233. break;
  2234. case C_STR:
  2235. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2236. break;
  2237. default:
  2238. push_val2(
  2239. stack,stack->stack_data[pos].type,
  2240. stack->stack_data[pos].u.num,
  2241. stack->stack_data[pos].ref
  2242. );
  2243. break;
  2244. }
  2245. }
  2246. /// Removes the values in indexes [start,end[ from the stack
  2247. void pop_stack(struct script_stack* stack, int start, int end)
  2248. {
  2249. struct script_data* data;
  2250. int i;
  2251. if( start < 0 )
  2252. start = 0;
  2253. if( end > stack->sp_max )
  2254. end = stack->sp_max;
  2255. if( start >= end )
  2256. return;// nothing to pop
  2257. // free stack elements
  2258. for( i = start; i < end; i++ )
  2259. {
  2260. data = &stack->stack_data[i];
  2261. if( data->type == C_STR )
  2262. aFree(data->u.str);
  2263. data->type = C_NOP;
  2264. }
  2265. // move the rest of the elements
  2266. if( stack->sp > end )
  2267. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2268. stack->sp -= end - start;
  2269. }
  2270. ///
  2271. ///
  2272. ///
  2273. /*==========================================
  2274. * スクリプト依存変数、関数依存変数の解放
  2275. *------------------------------------------*/
  2276. void script_free_vars(struct linkdb_node **node)
  2277. {
  2278. struct linkdb_node *n = *node;
  2279. while(n) {
  2280. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2281. char postfix = name[strlen(name)-1];
  2282. if( postfix == '$' ) {
  2283. // 文字型変数なので、データ削除
  2284. aFree(n->data);
  2285. }
  2286. n = n->next;
  2287. }
  2288. linkdb_final( node );
  2289. }
  2290. /*==========================================
  2291. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2292. *------------------------------------------*/
  2293. void script_free_stack(struct script_stack *stack)
  2294. {
  2295. int i;
  2296. for(i = 0; i < stack->sp; i++) {
  2297. if( stack->stack_data[i].type == C_STR ) {
  2298. aFree(stack->stack_data[i].u.str);
  2299. stack->stack_data[i].type = C_INT;
  2300. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2301. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2302. script_free_vars( n );
  2303. aFree( n );
  2304. }
  2305. }
  2306. script_free_vars( stack->var_function );
  2307. aFree(stack->var_function);
  2308. aFree(stack->stack_data);
  2309. aFree(stack);
  2310. }
  2311. void script_free_code(struct script_code* code)
  2312. {
  2313. script_free_vars( &code->script_vars );
  2314. aFree( code->script_buf );
  2315. aFree( code );
  2316. }
  2317. //
  2318. // 実行部main
  2319. //
  2320. /*==========================================
  2321. * コマンドの読み取り
  2322. *------------------------------------------*/
  2323. static int unget_com_data=-1;
  2324. int get_com(unsigned char *script,int *pos)
  2325. {
  2326. int i,j;
  2327. if(unget_com_data>=0){
  2328. i=unget_com_data;
  2329. unget_com_data=-1;
  2330. return i;
  2331. }
  2332. if(script[*pos]>=0x80){
  2333. return C_INT;
  2334. }
  2335. i=0; j=0;
  2336. while(script[*pos]>=0x40){
  2337. i=script[(*pos)++]<<j;
  2338. j+=6;
  2339. }
  2340. return i+(script[(*pos)++]<<j);
  2341. }
  2342. /*==========================================
  2343. * コマンドのプッシュバック
  2344. *------------------------------------------*/
  2345. void unget_com(int c)
  2346. {
  2347. if(unget_com_data!=-1)
  2348. ShowError("unget_com can back only 1 data\n");
  2349. unget_com_data=c;
  2350. }
  2351. /*==========================================
  2352. * 数値の所得
  2353. *------------------------------------------*/
  2354. int get_num(unsigned char *script,int *pos)
  2355. {
  2356. int i,j;
  2357. i=0; j=0;
  2358. while(script[*pos]>=0xc0){
  2359. i+=(script[(*pos)++]&0x7f)<<j;
  2360. j+=6;
  2361. }
  2362. return i+((script[(*pos)++]&0x7f)<<j);
  2363. }
  2364. /*==========================================
  2365. * スタックから値を取り出す
  2366. *------------------------------------------*/
  2367. int pop_val(struct script_state* st)
  2368. {
  2369. if(st->stack->sp<=0)
  2370. return 0;
  2371. st->stack->sp--;
  2372. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2373. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2374. return st->stack->stack_data[st->stack->sp].u.num;
  2375. return 0;
  2376. }
  2377. int isstr(struct script_data *c)
  2378. {
  2379. if( data_isstring(c) )
  2380. return 1;
  2381. else if( data_isreference(c) ) {
  2382. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2383. char postfix = p[strlen(p)-1];
  2384. return (postfix == '$');
  2385. }
  2386. return 0;
  2387. }
  2388. /// Ternary operators
  2389. /// test ? if_true : if_false
  2390. void op_3(struct script_state* st, int op)
  2391. {
  2392. struct script_data* data;
  2393. int flag = 0;
  2394. data = script_getdatatop(st, -3);
  2395. get_val(st, data);
  2396. if( data_isstring(data) )
  2397. flag = data->u.str[0];// "" -> false
  2398. else if( data_isint(data) )
  2399. flag = data->u.num;// 0 -> false
  2400. else
  2401. {
  2402. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2403. script_reportdata(data);
  2404. script_reportsrc(st);
  2405. script_removetop(st, -3, 0);
  2406. script_pushnil(st);
  2407. return;
  2408. }
  2409. if( flag )
  2410. script_pushcopytop(st, -2);
  2411. else
  2412. script_pushcopytop(st, -1);
  2413. script_removetop(st, -4, -1);
  2414. }
  2415. /// Binary string operators
  2416. /// s1 EQ s2 -> i
  2417. /// s1 NE s2 -> i
  2418. /// s1 GT s2 -> i
  2419. /// s1 GE s2 -> i
  2420. /// s1 LT s2 -> i
  2421. /// s1 LE s2 -> i
  2422. /// s1 ADD s2 -> s
  2423. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2424. {
  2425. int a = 0;
  2426. switch(op){
  2427. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2428. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2429. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2430. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2431. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2432. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2433. case C_ADD:
  2434. {
  2435. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2436. strcpy(buf, s1);
  2437. strcat(buf, s2);
  2438. script_pushstr(st, buf);
  2439. return;
  2440. }
  2441. default:
  2442. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2443. script_reportsrc(st);
  2444. script_pushnil(st);
  2445. st->state = END;
  2446. return;
  2447. }
  2448. script_pushint(st,a);
  2449. }
  2450. /// Binary number operators
  2451. /// i OP i -> i
  2452. void op_2num(struct script_state* st, int op, int i1, int i2)
  2453. {
  2454. int ret;
  2455. double ret_double;
  2456. switch( op )
  2457. {
  2458. case C_AND: ret = i1 & i2; break;
  2459. case C_OR: ret = i1 | i2; break;
  2460. case C_XOR: ret = i1 ^ i2; break;
  2461. case C_LAND: ret = (i1 && i2); break;
  2462. case C_LOR: ret = (i1 || i2); break;
  2463. case C_EQ: ret = (i1 == i2); break;
  2464. case C_NE: ret = (i1 != i2); break;
  2465. case C_GT: ret = (i1 > i2); break;
  2466. case C_GE: ret = (i1 >= i2); break;
  2467. case C_LT: ret = (i1 < i2); break;
  2468. case C_LE: ret = (i1 <= i2); break;
  2469. case C_R_SHIFT: ret = i1>>i2; break;
  2470. case C_L_SHIFT: ret = i1<<i2; break;
  2471. case C_DIV:
  2472. case C_MOD:
  2473. if( i2 == 0 )
  2474. {
  2475. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2476. script_reportsrc(st);
  2477. script_pushnil(st);
  2478. st->state = END;
  2479. return;
  2480. }
  2481. else if( op == C_DIV )
  2482. ret = i1 / i2;
  2483. else//if( op == C_MOD )
  2484. ret = i1 % i2;
  2485. break;
  2486. default:
  2487. switch( op )
  2488. {// operators that can overflow/underflow
  2489. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2490. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2491. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2492. default:
  2493. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2494. script_reportsrc(st);
  2495. script_pushnil(st);
  2496. return;
  2497. }
  2498. if( ret_double < (double)INT_MIN )
  2499. {
  2500. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2501. script_reportsrc(st);
  2502. ret = INT_MIN;
  2503. }
  2504. else if( ret_double > (double)INT_MAX )
  2505. {
  2506. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2507. script_reportsrc(st);
  2508. ret = INT_MAX;
  2509. }
  2510. }
  2511. script_pushint(st, ret);
  2512. }
  2513. /// Binary operators
  2514. void op_2(struct script_state *st, int op)
  2515. {
  2516. struct script_data* left;
  2517. struct script_data* right;
  2518. left = script_getdatatop(st, -2);
  2519. right = script_getdatatop(st, -1);
  2520. get_val(st, left);
  2521. get_val(st, right);
  2522. // automatic conversions
  2523. switch( op )
  2524. {
  2525. case C_ADD:
  2526. if( data_isint(left) && data_isstring(right) )
  2527. {// convert int-string to string-string
  2528. conv_str(st, left);
  2529. }
  2530. else if( data_isstring(left) && data_isint(right) )
  2531. {// convert string-int to string-string
  2532. conv_str(st, right);
  2533. }
  2534. break;
  2535. }
  2536. if( data_isstring(left) && data_isstring(right) )
  2537. {// ss => op_2str
  2538. op_2str(st, op, left->u.str, right->u.str);
  2539. script_removetop(st, -3, -1);// pop the two values before the top one
  2540. }
  2541. else if( data_isint(left) && data_isint(right) )
  2542. {// ii => op_2num
  2543. int i1 = left->u.num;
  2544. int i2 = right->u.num;
  2545. script_removetop(st, -2, 0);
  2546. op_2num(st, op, i1, i2);
  2547. }
  2548. else
  2549. {// invalid argument
  2550. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2551. script_reportdata(left);
  2552. script_reportdata(right);
  2553. script_reportsrc(st);
  2554. script_removetop(st, -2, 0);
  2555. script_pushnil(st);
  2556. st->state = END;
  2557. }
  2558. }
  2559. /// Unary operators
  2560. /// NEG i -> i
  2561. /// NOT i -> i
  2562. /// LNOT i -> i
  2563. void op_1(struct script_state* st, int op)
  2564. {
  2565. struct script_data* data;
  2566. int i1;
  2567. data = script_getdatatop(st, -1);
  2568. get_val(st, data);
  2569. if( !data_isint(data) )
  2570. {// not a number
  2571. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2572. script_reportdata(data);
  2573. script_reportsrc(st);
  2574. script_pushnil(st);
  2575. st->state = END;
  2576. return;
  2577. }
  2578. i1 = data->u.num;
  2579. script_removetop(st, -1, 0);
  2580. switch( op )
  2581. {
  2582. case C_NEG: i1 = -i1; break;
  2583. case C_NOT: i1 = ~i1; break;
  2584. case C_LNOT: i1 = !i1; break;
  2585. default:
  2586. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2587. script_reportsrc(st);
  2588. script_pushnil(st);
  2589. st->state = END;
  2590. return;
  2591. }
  2592. script_pushint(st, i1);
  2593. }
  2594. /*==========================================
  2595. * 関数の実行
  2596. *------------------------------------------*/
  2597. int run_func(struct script_state *st)
  2598. {
  2599. int i,start_sp,end_sp,func;
  2600. end_sp=st->stack->sp;
  2601. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2602. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2603. ShowError("function not found\n");
  2604. // st->stack->sp=0;
  2605. st->state=END;
  2606. script_reportsrc(st);
  2607. return 1;
  2608. }
  2609. start_sp=i-1;
  2610. st->start=i-1;
  2611. st->end=end_sp;
  2612. func=st->stack->stack_data[st->start].u.num;
  2613. #ifdef DEBUG_RUN
  2614. if(battle_config.etc_log) {
  2615. 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);
  2616. ShowDebug("stack dump :");
  2617. for(i=0;i<end_sp;i++){
  2618. switch(st->stack->stack_data[i].type){
  2619. case C_INT:
  2620. printf(" int(%d)",st->stack->stack_data[i].u.num);
  2621. break;
  2622. case C_NAME:
  2623. printf(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2624. break;
  2625. case C_ARG:
  2626. printf(" arg");
  2627. break;
  2628. case C_POS:
  2629. printf(" pos(%d)",st->stack->stack_data[i].u.num);
  2630. break;
  2631. case C_STR:
  2632. printf(" str(%s)",st->stack->stack_data[i].u.str);
  2633. break;
  2634. case C_CONSTSTR:
  2635. printf(" cstr(%s)",st->stack->stack_data[i].u.str);
  2636. break;
  2637. default:
  2638. printf(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2639. }
  2640. }
  2641. printf("\n");
  2642. }
  2643. #endif
  2644. if(str_data[func].type!=C_FUNC ){
  2645. 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);
  2646. // st->stack->sp=0;
  2647. st->state=END;
  2648. script_reportsrc(st);
  2649. return 1;
  2650. }
  2651. #ifdef DEBUG_RUN
  2652. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2653. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2654. );
  2655. #endif
  2656. if(str_data[func].func){
  2657. if (str_data[func].func(st)) //Report error
  2658. script_reportsrc(st);
  2659. } else {
  2660. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2661. script_pushint(st,0);
  2662. script_reportsrc(st);
  2663. }
  2664. // Stack's datum are used when re-run functions [Eoe]
  2665. if(st->state != RERUNLINE) {
  2666. pop_stack(st->stack,start_sp,end_sp);
  2667. }
  2668. if(st->state==RETFUNC){
  2669. // ユーザー定義関数からの復帰
  2670. int olddefsp=st->stack->defsp;
  2671. int i;
  2672. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2673. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2674. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2675. st->state=END;
  2676. script_reportsrc(st);
  2677. return 1;
  2678. }
  2679. script_free_vars( st->stack->var_function );
  2680. aFree(st->stack->var_function);
  2681. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2682. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2683. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2684. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2685. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2686. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2687. st->state=GOTO;
  2688. }
  2689. return 0;
  2690. }
  2691. /*==========================================
  2692. * スクリプトの実行
  2693. *------------------------------------------*/
  2694. void run_script_main(struct script_state *st);
  2695. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2696. {
  2697. struct script_state *st;
  2698. TBL_PC *sd=NULL;
  2699. if(rootscript==NULL || pos<0)
  2700. return;
  2701. if (rid) sd = map_id2sd(rid);
  2702. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2703. //Resume script.
  2704. st = sd->st;
  2705. } else {
  2706. st = aCalloc(sizeof(struct script_state), 1);
  2707. // the script is different, make new script_state and stack
  2708. st->stack = aMalloc (sizeof(struct script_stack));
  2709. st->stack->sp=0;
  2710. st->stack->sp_max=64;
  2711. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2712. st->stack->defsp = st->stack->sp;
  2713. st->stack->var_function = aCalloc(1, sizeof(struct linkdb_node*));
  2714. st->state = RUN;
  2715. st->script = rootscript;
  2716. }
  2717. st->pos = pos;
  2718. st->rid = rid;
  2719. st->oid = oid;
  2720. st->sleep.timer = INVALID_TIMER;
  2721. run_script_main(st);
  2722. }
  2723. void script_stop_sleeptimers(int id)
  2724. {
  2725. struct script_state* st;
  2726. for(;;)
  2727. {
  2728. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2729. if( st == NULL )
  2730. break; // no more sleep timers
  2731. if( st->sleep.timer != INVALID_TIMER )
  2732. delete_timer(st->sleep.timer, run_script_timer);
  2733. script_free_stack(st->stack);
  2734. aFree(st);
  2735. }
  2736. }
  2737. /*==========================================
  2738. * 指定ノードをsleep_dbから削除
  2739. *------------------------------------------*/
  2740. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2741. {
  2742. struct linkdb_node *retnode;
  2743. if( n == NULL)
  2744. return NULL;
  2745. if( n->prev == NULL )
  2746. sleep_db = n->next;
  2747. else
  2748. n->prev->next = n->next;
  2749. if( n->next )
  2750. n->next->prev = n->prev;
  2751. retnode = n->next;
  2752. aFree( n );
  2753. return retnode; // 次のノードを返す
  2754. }
  2755. /*==========================================
  2756. * sleep用タイマー関数
  2757. *------------------------------------------*/
  2758. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2759. {
  2760. struct script_state *st = (struct script_state *)data;
  2761. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2762. TBL_PC *sd = map_id2sd(st->rid);
  2763. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2764. { //Character mismatch. Cancel execution.
  2765. st->rid = 0;
  2766. st->state = END;
  2767. }
  2768. while( node && st->sleep.timer != -1 ) {
  2769. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2770. script_erase_sleepdb(node);
  2771. st->sleep.timer = -1;
  2772. break;
  2773. }
  2774. node = node->next;
  2775. }
  2776. if(st->state != RERUNLINE)
  2777. st->sleep.tick = 0;
  2778. run_script_main(st);
  2779. return 0;
  2780. }
  2781. /*==========================================
  2782. * スクリプトの実行メイン部分
  2783. *------------------------------------------*/
  2784. void run_script_main(struct script_state *st)
  2785. {
  2786. int c;
  2787. int cmdcount=script_config.check_cmdcount;
  2788. int gotocount=script_config.check_gotocount;
  2789. TBL_PC *sd;
  2790. //For backing up purposes
  2791. struct script_state *bk_st = NULL;
  2792. int bk_npcid = 0;
  2793. struct script_stack *stack=st->stack;
  2794. sd = st->rid?map_id2sd(st->rid):NULL;
  2795. if(sd){
  2796. if(sd->st != st){
  2797. bk_st = sd->st;
  2798. bk_npcid = sd->npc_id;
  2799. }
  2800. sd->st = st;
  2801. sd->npc_id = st->oid;
  2802. }
  2803. if(st->state == RERUNLINE) {
  2804. st->state = RUN;
  2805. run_func(st);
  2806. if(st->state == GOTO)
  2807. st->state = RUN;
  2808. } else if(st->state != END)
  2809. st->state = RUN;
  2810. while(st->state == RUN){
  2811. c= get_com(st->script->script_buf,&st->pos);
  2812. switch(c){
  2813. case C_EOL:
  2814. if( stack->sp != stack->defsp )
  2815. {
  2816. if( stack->sp > stack->defsp )
  2817. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2818. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2819. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2820. } else
  2821. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2822. stack->sp = stack->defsp;
  2823. }
  2824. break;
  2825. case C_INT:
  2826. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2827. break;
  2828. case C_POS:
  2829. case C_NAME:
  2830. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2831. st->pos+=3;
  2832. break;
  2833. case C_ARG:
  2834. push_val(stack,c,0);
  2835. break;
  2836. case C_STR:
  2837. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2838. while(st->script->script_buf[st->pos++]);
  2839. break;
  2840. case C_FUNC:
  2841. run_func(st);
  2842. if(st->state==GOTO){
  2843. st->state = RUN;
  2844. if( gotocount>0 && (--gotocount)<=0 ){
  2845. ShowError("run_script: infinity loop !\n");
  2846. script_reportsrc(st);
  2847. st->state=END;
  2848. }
  2849. }
  2850. break;
  2851. case C_NEG:
  2852. case C_NOT:
  2853. case C_LNOT:
  2854. op_1(st ,c);
  2855. break;
  2856. case C_ADD:
  2857. case C_SUB:
  2858. case C_MUL:
  2859. case C_DIV:
  2860. case C_MOD:
  2861. case C_EQ:
  2862. case C_NE:
  2863. case C_GT:
  2864. case C_GE:
  2865. case C_LT:
  2866. case C_LE:
  2867. case C_AND:
  2868. case C_OR:
  2869. case C_XOR:
  2870. case C_LAND:
  2871. case C_LOR:
  2872. case C_R_SHIFT:
  2873. case C_L_SHIFT:
  2874. op_2(st, c);
  2875. break;
  2876. case C_OP3:
  2877. op_3(st, c);
  2878. break;
  2879. case C_NOP:
  2880. st->state=END;
  2881. break;
  2882. default:
  2883. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2884. st->state=END;
  2885. break;
  2886. }
  2887. if( cmdcount>0 && (--cmdcount)<=0 ){
  2888. ShowError("run_script: infinity loop !\n");
  2889. script_reportsrc(st);
  2890. st->state=END;
  2891. }
  2892. }
  2893. if(st->sleep.tick > 0) {
  2894. //Delay execution
  2895. st->sleep.charid = sd?sd->status.char_id:0;
  2896. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2897. run_script_timer, st->sleep.charid, (int)st);
  2898. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2899. //Restore previous script
  2900. if (sd) {
  2901. sd->st = bk_st;
  2902. sd->npc_id = bk_npcid;
  2903. bk_st = NULL; //Remove tag for removal.
  2904. }
  2905. }
  2906. else if(st->state != END && sd){
  2907. //Resume later (st is already attached to player).
  2908. if(bk_st) {
  2909. ShowWarning("Unable to restore stack! Double continuation!\n");
  2910. //Report BOTH scripts to see if that can help somehow.
  2911. ShowDebug("Previous script (lost):\n");
  2912. script_reportsrc(bk_st);
  2913. ShowDebug("Current script:\n");
  2914. script_reportsrc(st);
  2915. }
  2916. } else {
  2917. //Dispose of script.
  2918. if (sd)
  2919. { //Restore previous stack and save char.
  2920. if(sd->state.using_fake_npc){
  2921. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2922. sd->state.using_fake_npc = 0;
  2923. }
  2924. //Restore previous script if any.
  2925. sd->st = bk_st;
  2926. sd->npc_id = bk_npcid;
  2927. if (!bk_st)
  2928. npc_event_dequeue(sd);
  2929. else
  2930. bk_st = NULL; //Remove tag for removal.
  2931. if (sd->state.reg_dirty&2)
  2932. intif_saveregistry(sd,2);
  2933. if (sd->state.reg_dirty&1)
  2934. intif_saveregistry(sd,1);
  2935. }
  2936. st->pos = -1;
  2937. script_free_stack (st->stack);
  2938. aFree(st);
  2939. }
  2940. if (bk_st)
  2941. { //Remove previous script
  2942. bk_st->pos = -1;
  2943. script_free_stack(bk_st->stack);
  2944. aFree(bk_st);
  2945. bk_st = NULL;
  2946. }
  2947. }
  2948. /*==========================================
  2949. * マップ変数の変更
  2950. *------------------------------------------*/
  2951. int mapreg_setreg(int num, int val)
  2952. {
  2953. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2954. int i = num >> 24;
  2955. char* name = str_buf + str_data[num&0x00ffffff].str;
  2956. if( val != 0 ) {
  2957. if(idb_put(mapreg_db,num,(void*)val))
  2958. ;
  2959. else if(name[1] != '@') {
  2960. char tmp_str[32*2+1];
  2961. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  2962. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%d')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, val) )
  2963. Sql_ShowDebug(mmysql_handle);
  2964. }
  2965. } else { // val == 0
  2966. if(name[1] != '@') { // Remove from database because it is unused.
  2967. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  2968. Sql_ShowDebug(mmysql_handle);
  2969. }
  2970. idb_remove(mapreg_db,num);
  2971. }
  2972. #else
  2973. if(val != 0)
  2974. idb_put(mapreg_db,num,(void*)val);
  2975. else
  2976. idb_remove(mapreg_db,num);
  2977. #endif
  2978. mapreg_dirty = 1;
  2979. return 1;
  2980. }
  2981. /*==========================================
  2982. * 文字列型マップ変数の変更
  2983. *------------------------------------------*/
  2984. int mapreg_setregstr(int num, const char* str)
  2985. {
  2986. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2987. int i = num >> 24;
  2988. char* name = str_buf + str_data[num&0x00ffffff].str;
  2989. if( str==NULL || *str==0 ) {
  2990. if(name[1] != '@') {
  2991. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  2992. Sql_ShowDebug(mmysql_handle);
  2993. }
  2994. idb_remove(mapregstr_db,num);
  2995. mapreg_dirty = 1;
  2996. return 1;
  2997. }
  2998. if (idb_put(mapregstr_db,num, aStrdup(str)))
  2999. ;
  3000. else if(name[1] != '@') { //put returned null, so we must insert.
  3001. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  3002. char tmp_str[32*2+1];
  3003. char tmp_str2[255*2+1];
  3004. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  3005. Sql_EscapeStringLen(mmysql_handle, tmp_str2, str, strnlen(str, 255));
  3006. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%s')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, tmp_str2) )
  3007. Sql_ShowDebug(mmysql_handle);
  3008. }
  3009. #else
  3010. if( str==NULL || *str==0 )
  3011. idb_remove(mapregstr_db,num);
  3012. else
  3013. idb_put(mapregstr_db,num,aStrdup(str));
  3014. #endif
  3015. mapreg_dirty = 1;
  3016. return 1;
  3017. }
  3018. /*==========================================
  3019. * 永続的マップ変数の読み込み
  3020. *------------------------------------------*/
  3021. static int script_load_mapreg(void)
  3022. {
  3023. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3024. FILE* fp;
  3025. char line[1024];
  3026. if( (fp=fopen(mapreg_txt,"rt")) == NULL )
  3027. return -1;
  3028. while(fgets(line,sizeof(line),fp))
  3029. {
  3030. char buf1[256],buf2[1024];
  3031. char* p;
  3032. int n,v,s,i;
  3033. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  3034. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  3035. continue;
  3036. if( buf1[strlen(buf1)-1] == '$' ) {
  3037. if( sscanf(line + n, "%[^\n\r]", buf2) != 1 ) {
  3038. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3039. continue;
  3040. }
  3041. p = aStrdup(buf2);
  3042. s = add_str(buf1);
  3043. idb_put(mapregstr_db, (i<<24)|s, p);
  3044. } else {
  3045. if( sscanf(line + n, "%d", &v) != 1 ) {
  3046. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3047. continue;
  3048. }
  3049. s = add_str(buf1);
  3050. idb_put(mapreg_db, (i<<24)|s, (void*)v);
  3051. }
  3052. }
  3053. fclose(fp);
  3054. mapreg_dirty = 0;
  3055. return 0;
  3056. #else
  3057. /*
  3058. 0 1 2
  3059. +-------------------------+
  3060. | varname | index | value |
  3061. +-------------------------+
  3062. */
  3063. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  3064. char varname[32+1];
  3065. int index;
  3066. char value[255+1];
  3067. uint32 length;
  3068. if ( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `%s`, `%s`, `%s` FROM `%s`", mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, mapregsql_db)
  3069. || SQL_ERROR == SqlStmt_Execute(stmt)
  3070. ) {
  3071. SqlStmt_ShowDebug(stmt);
  3072. SqlStmt_Free(stmt);
  3073. return -1;
  3074. }
  3075. SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &varname[0], 32, &length, NULL);
  3076. SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
  3077. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &value[0], 255, NULL, NULL);
  3078. while ( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  3079. {
  3080. if( varname[length-1] == '$' ) {
  3081. int s = add_str(varname);
  3082. int i = index;
  3083. char* p = aStrdup(value);
  3084. idb_put(mapregstr_db, (i<<24)|s, p);
  3085. } else {
  3086. int s = add_str(varname);
  3087. int i = index;
  3088. int v = atoi(value);
  3089. idb_put(mapreg_db, (i<<24)|s, (void *)v);
  3090. }
  3091. }
  3092. SqlStmt_Free(stmt);
  3093. mapreg_dirty = 0;
  3094. return 0;
  3095. #endif /* TXT_ONLY */
  3096. }
  3097. /*==========================================
  3098. * 永続的マップ変数の書き込み
  3099. *------------------------------------------*/
  3100. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  3101. {
  3102. int num=key.i&0x00ffffff, i=key.i>>24;
  3103. char *name=str_buf+str_data[num].str;
  3104. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3105. FILE *fp=va_arg(ap,FILE*);
  3106. if( name[1]!='@' ) {
  3107. if(i==0)
  3108. fprintf(fp,"%s\t%d\n", name, (int)data);
  3109. else
  3110. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  3111. }
  3112. #else
  3113. if ( name[1] != '@') {
  3114. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, (int)data, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3115. Sql_ShowDebug(mmysql_handle);
  3116. }
  3117. #endif
  3118. return 0;
  3119. }
  3120. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  3121. {
  3122. int num=key.i&0x00ffffff, i=key.i>>24;
  3123. char *name=str_buf+str_data[num].str;
  3124. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3125. FILE *fp=va_arg(ap,FILE*);
  3126. if( name[1]!='@' ) {
  3127. if(i==0)
  3128. fprintf(fp,"%s\t%s\n", name, (char *)data);
  3129. else
  3130. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  3131. }
  3132. #else
  3133. if ( name[1] != '@') {
  3134. char tmp_str2[2*255+1];
  3135. Sql_EscapeStringLen(mmysql_handle, tmp_str2, (char*)data, safestrnlen((char*)data, 255));
  3136. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%s' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, tmp_str2, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3137. Sql_ShowDebug(mmysql_handle);
  3138. }
  3139. #endif
  3140. return 0;
  3141. }
  3142. static int script_save_mapreg(void)
  3143. {
  3144. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3145. FILE *fp;
  3146. int lock;
  3147. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  3148. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  3149. return -1;
  3150. }
  3151. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  3152. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  3153. lock_fclose(fp,mapreg_txt,&lock);
  3154. #else
  3155. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub);
  3156. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  3157. #endif
  3158. mapreg_dirty=0;
  3159. return 0;
  3160. }
  3161. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  3162. {
  3163. if(mapreg_dirty)
  3164. if (script_save_mapreg() == -1)
  3165. ShowError("Failed to save the mapreg data!\n");
  3166. return 0;
  3167. }
  3168. int script_config_read_sub(char *cfgName)
  3169. {
  3170. int i;
  3171. char line[1024],w1[1024],w2[1024];
  3172. FILE *fp;
  3173. fp=fopen(cfgName,"r");
  3174. if(fp==NULL){
  3175. ShowError("file not found: [%s]\n", cfgName);
  3176. return 1;
  3177. }
  3178. while(fgets(line, sizeof(line), fp))
  3179. {
  3180. if(line[0] == '/' && line[1] == '/')
  3181. continue;
  3182. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3183. if(i!=2)
  3184. continue;
  3185. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3186. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3187. }
  3188. else if(strcmpi(w1,"check_cmdcount")==0) {
  3189. script_config.check_cmdcount = config_switch(w2);
  3190. }
  3191. else if(strcmpi(w1,"check_gotocount")==0) {
  3192. script_config.check_gotocount = config_switch(w2);
  3193. }
  3194. else if(strcmpi(w1,"event_script_type")==0) {
  3195. script_config.event_script_type = config_switch(w2);
  3196. }
  3197. else if(strcmpi(w1,"event_requires_trigger")==0) {
  3198. script_config.event_requires_trigger = config_switch(w2);
  3199. }
  3200. else if(strcmpi(w1,"die_event_name")==0) {
  3201. strncpy(script_config.die_event_name, w2, NAME_LENGTH-1);
  3202. if (strlen(script_config.die_event_name) != strlen(w2))
  3203. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.die_event_name);
  3204. }
  3205. else if(strcmpi(w1,"kill_pc_event_name")==0) {
  3206. strncpy(script_config.kill_pc_event_name, w2, NAME_LENGTH-1);
  3207. if (strlen(script_config.kill_pc_event_name) != strlen(w2))
  3208. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_pc_event_name);
  3209. }
  3210. else if(strcmpi(w1,"kill_mob_event_name")==0) {
  3211. strncpy(script_config.kill_mob_event_name, w2, NAME_LENGTH-1);
  3212. if (strlen(script_config.kill_mob_event_name) != strlen(w2))
  3213. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_mob_event_name);
  3214. }
  3215. else if(strcmpi(w1,"login_event_name")==0) {
  3216. strncpy(script_config.login_event_name, w2, NAME_LENGTH-1);
  3217. if (strlen(script_config.login_event_name) != strlen(w2))
  3218. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.login_event_name);
  3219. }
  3220. else if(strcmpi(w1,"logout_event_name")==0) {
  3221. strncpy(script_config.logout_event_name, w2, NAME_LENGTH-1);
  3222. if (strlen(script_config.logout_event_name) != strlen(w2))
  3223. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.logout_event_name);
  3224. }
  3225. else if(strcmpi(w1,"loadmap_event_name")==0) {
  3226. strncpy(script_config.loadmap_event_name, w2, NAME_LENGTH-1);
  3227. if (strlen(script_config.loadmap_event_name) != strlen(w2))
  3228. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.loadmap_event_name);
  3229. }
  3230. else if(strcmpi(w1,"baselvup_event_name")==0) {
  3231. strncpy(script_config.baselvup_event_name, w2, NAME_LENGTH-1);
  3232. if (strlen(script_config.baselvup_event_name) != strlen(w2))
  3233. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.baselvup_event_name);
  3234. }
  3235. else if(strcmpi(w1,"joblvup_event_name")==0) {
  3236. strncpy(script_config.joblvup_event_name, w2, NAME_LENGTH-1);
  3237. if (strlen(script_config.joblvup_event_name) != strlen(w2))
  3238. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.joblvup_event_name);
  3239. }
  3240. else if(strcmpi(w1,"import")==0){
  3241. script_config_read_sub(w2);
  3242. }
  3243. }
  3244. fclose(fp);
  3245. return 0;
  3246. }
  3247. int script_config_read(char *cfgName)
  3248. { //Script related variables should be initialized once! [Skotlex]
  3249. memset (&script_config, 0, sizeof(script_config));
  3250. script_config.warn_func_mismatch_paramnum = 1;
  3251. script_config.check_cmdcount = 65535;
  3252. script_config.check_gotocount = 2048;
  3253. script_config.event_script_type = 0;
  3254. script_config.event_requires_trigger = 1;
  3255. return script_config_read_sub(cfgName);
  3256. }
  3257. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3258. {
  3259. struct script_code *code = (struct script_code *)data;
  3260. if(code){
  3261. script_free_vars( &code->script_vars );
  3262. aFree( code->script_buf );
  3263. aFree( code );
  3264. }
  3265. return 0;
  3266. }
  3267. /*==========================================
  3268. * 終了
  3269. *------------------------------------------*/
  3270. int do_final_script()
  3271. {
  3272. #ifdef DEBUG_HASH
  3273. if (battle_config.etc_log)
  3274. {
  3275. FILE *fp = fopen("hash_dump.txt","wt");
  3276. if(fp) {
  3277. int i,count[SCRIPT_HASH_SIZE];
  3278. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3279. int n=0;
  3280. int min=INT_MAX,max=0,zero=0;
  3281. double mean=0.0f;
  3282. double median=0.0f;
  3283. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3284. memset(count, 0, sizeof(count));
  3285. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3286. fprintf(fp,"---------------------------------------------------------------\n");
  3287. for(i=LABEL_START; i<str_num; i++) {
  3288. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3289. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3290. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3291. ++count[h];
  3292. }
  3293. fprintf(fp,"--------------------\n\n");
  3294. memset(count2, 0, sizeof(count2));
  3295. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3296. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3297. if(min > count[i])
  3298. min = count[i]; // minimun count of collision
  3299. if(max < count[i])
  3300. max = count[i]; // maximun count of collision
  3301. if(count[i] == 0)
  3302. zero++;
  3303. ++count2[count[i]];
  3304. }
  3305. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3306. for( i=min; i <= max; ++i ){
  3307. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3308. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3309. }
  3310. for( i=min; i <= max; ++i ){
  3311. n += count2[i];
  3312. if( n*2 >= SCRIPT_HASH_SIZE )
  3313. {
  3314. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3315. median = (i+i+1)/2.0f;
  3316. else
  3317. median = i;
  3318. break;
  3319. }
  3320. }
  3321. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3322. fclose(fp);
  3323. }
  3324. }
  3325. #endif
  3326. if(mapreg_dirty>=0)
  3327. script_save_mapreg();
  3328. mapreg_db->destroy(mapreg_db,NULL);
  3329. mapregstr_db->destroy(mapregstr_db,NULL);
  3330. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3331. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3332. if(sleep_db) {
  3333. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3334. while(n) {
  3335. struct script_state *st = (struct script_state *)n->data;
  3336. script_free_stack(st->stack);
  3337. aFree(st);
  3338. n = n->next;
  3339. }
  3340. linkdb_final(&sleep_db);
  3341. }
  3342. if (str_data)
  3343. aFree(str_data);
  3344. if (str_buf)
  3345. aFree(str_buf);
  3346. return 0;
  3347. }
  3348. /*==========================================
  3349. * 初期化
  3350. *------------------------------------------*/
  3351. int do_init_script()
  3352. {
  3353. mapreg_db= idb_alloc(DB_OPT_BASE);
  3354. mapregstr_db=idb_alloc(DB_OPT_RELEASE_DATA);
  3355. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3356. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA,50);
  3357. script_load_mapreg();
  3358. add_timer_func_list(script_autosave_mapreg, "script_autosave_mapreg");
  3359. add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL, script_autosave_mapreg, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
  3360. return 0;
  3361. }
  3362. int script_reload()
  3363. {
  3364. if(mapreg_dirty>=0)
  3365. script_save_mapreg();
  3366. mapreg_db->clear(mapreg_db, NULL);
  3367. mapregstr_db->clear(mapregstr_db, NULL);
  3368. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3369. scriptlabel_db->clear(scriptlabel_db, NULL);
  3370. if(sleep_db) {
  3371. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3372. while(n) {
  3373. struct script_state *st = (struct script_state *)n->data;
  3374. if( st->sleep.timer != INVALID_TIMER )
  3375. delete_timer(st->sleep.timer, run_script_timer);
  3376. script_free_stack(st->stack);
  3377. aFree(st);
  3378. n = n->next;
  3379. }
  3380. linkdb_final(&sleep_db);
  3381. }
  3382. script_load_mapreg();
  3383. return 0;
  3384. }
  3385. //-----------------------------------------------------------------------------
  3386. // buildin functions
  3387. //
  3388. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3389. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3390. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3391. /////////////////////////////////////////////////////////////////////
  3392. // NPC interaction
  3393. //
  3394. /// Appends a message to the npc dialog.
  3395. /// If a dialog doesn't exist yet, one is created.
  3396. ///
  3397. /// mes "<message>";
  3398. BUILDIN_FUNC(mes)
  3399. {
  3400. TBL_PC* sd = script_rid2sd(st);
  3401. if( sd == NULL )
  3402. return 1;
  3403. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3404. return 0;
  3405. }
  3406. /// Displays the button 'next' in the npc dialog.
  3407. /// The dialog text is cleared and the script continues when the button is pressed.
  3408. ///
  3409. /// next;
  3410. BUILDIN_FUNC(next)
  3411. {
  3412. TBL_PC* sd;
  3413. sd = script_rid2sd(st);
  3414. if( sd == NULL )
  3415. return 1;
  3416. st->state = STOP;
  3417. clif_scriptnext(sd, st->oid);
  3418. return 0;
  3419. }
  3420. /// Ends the script and displays the button 'close' on the npc dialog.
  3421. /// The dialog is closed when the button is pressed.
  3422. ///
  3423. /// close;
  3424. BUILDIN_FUNC(close)
  3425. {
  3426. TBL_PC* sd;
  3427. sd = script_rid2sd(st);
  3428. if( sd == NULL )
  3429. return 1;
  3430. st->state = END;
  3431. clif_scriptclose(sd, st->oid);
  3432. return 0;
  3433. }
  3434. /// Displays the button 'close' on the npc dialog.
  3435. /// The dialog is closed and the script continues when the button is pressed.
  3436. ///
  3437. /// close2;
  3438. BUILDIN_FUNC(close2)
  3439. {
  3440. TBL_PC* sd;
  3441. sd = script_rid2sd(st);
  3442. if( sd == NULL )
  3443. return 1;
  3444. st->state = STOP;
  3445. clif_scriptclose(sd, st->oid);
  3446. return 0;
  3447. }
  3448. /// Counts the number of valid and total number of options in 'str'
  3449. /// If max_count > 0 the counting stops when that valid option is reached
  3450. /// total is incremented for each option (NULL is supported)
  3451. static int menu_countoptions(const char* str, int max_count, int* total)
  3452. {
  3453. int count = 0;
  3454. int bogus_total;
  3455. if( total == NULL )
  3456. total = &bogus_total;
  3457. ++(*total);
  3458. // initial empty options
  3459. while( *str == ':' )
  3460. {
  3461. ++str;
  3462. ++(*total);
  3463. }
  3464. // count menu options
  3465. while( *str != '\0' )
  3466. {
  3467. ++count;
  3468. --max_count;
  3469. if( max_count == 0 )
  3470. break;
  3471. while( *str != ':' && *str != '\0' )
  3472. ++str;
  3473. while( *str == ':' )
  3474. {
  3475. ++str;
  3476. ++(*total);
  3477. }
  3478. }
  3479. return count;
  3480. }
  3481. /// Displays a menu with options and goes to the target label.
  3482. /// The script is stopped if cancel is pressed.
  3483. /// Options with no text are not displayed in the client.
  3484. ///
  3485. /// Options can be grouped together, separated by the character ':' in the text:
  3486. /// ex: menu "A:B:C",L_target;
  3487. /// All these options go to the specified target label.
  3488. ///
  3489. /// The index of the selected option is put in the variable @menu.
  3490. /// Indexes start with 1 and are consistent with grouped and empty options.
  3491. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3492. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3493. ///
  3494. /// NOTE: the client closes the npc dialog when cancel is pressed
  3495. ///
  3496. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3497. BUILDIN_FUNC(menu)
  3498. {
  3499. int i;
  3500. const char* text;
  3501. TBL_PC* sd;
  3502. sd = script_rid2sd(st);
  3503. if( sd == NULL )
  3504. return 1;
  3505. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3506. if( sd->state.menu_or_input == 0 )
  3507. {
  3508. struct StringBuf buf;
  3509. struct script_data* data;
  3510. if( script_lastdata(st) % 2 == 0 )
  3511. {// argument count is not even (1st argument is at index 2)
  3512. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3513. st->state = END;
  3514. return 1;
  3515. }
  3516. StringBuf_Init(&buf);
  3517. for( i = 2, sd->npc_menu = 0; i < script_lastdata(st); i += 2 )
  3518. {
  3519. // menu options
  3520. text = script_getstr(st, i);
  3521. // target label
  3522. data = script_getdata(st, i+1);
  3523. if( !data_islabel(data) )
  3524. {// not a label
  3525. StringBuf_Destroy(&buf);
  3526. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3527. script_reportdata(data);
  3528. st->state = END;
  3529. return 1;
  3530. }
  3531. // append option(s)
  3532. if( text[0] == '\0' )
  3533. continue;// empty string, ignore
  3534. if( sd->npc_menu > 0 )
  3535. StringBuf_AppendStr(&buf, ":");
  3536. StringBuf_AppendStr(&buf, text);
  3537. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3538. }
  3539. st->state = RERUNLINE;
  3540. sd->state.menu_or_input = 1;
  3541. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3542. StringBuf_Destroy(&buf);
  3543. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3544. }
  3545. else if( sd->npc_menu == 0xff )
  3546. {// Cancel was pressed
  3547. sd->state.menu_or_input = 0;
  3548. st->state = END;
  3549. }
  3550. else
  3551. {// goto target label
  3552. int menu = 0;
  3553. sd->state.menu_or_input = 0;
  3554. if( sd->npc_menu <= 0 )
  3555. {
  3556. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3557. st->state = END;
  3558. return 1;
  3559. }
  3560. // get target label
  3561. for( i = 2; i < script_lastdata(st); i += 2 )
  3562. {
  3563. text = script_getstr(st, i);
  3564. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3565. if( sd->npc_menu <= 0 )
  3566. break;// entry found
  3567. }
  3568. if( sd->npc_menu > 0 )
  3569. {// Invalid selection
  3570. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3571. st->state = END;
  3572. return 1;
  3573. }
  3574. if( !data_islabel(script_getdata(st, i + 1)) )
  3575. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3576. ShowError("script:menu: unexpected data in label argument\n");
  3577. script_reportdata(script_getdata(st, i + 1));
  3578. st->state = END;
  3579. return 1;
  3580. }
  3581. pc_setreg(sd, add_str("@menu"), menu);
  3582. st->pos = script_getnum(st, i + 1);
  3583. st->state = GOTO;
  3584. }
  3585. return 0;
  3586. }
  3587. /// Displays a menu with options and returns the selected option.
  3588. /// Behaves like 'menu' without the target labels.
  3589. ///
  3590. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3591. ///
  3592. /// @see menu
  3593. BUILDIN_FUNC(select)
  3594. {
  3595. int i;
  3596. const char* text;
  3597. TBL_PC* sd;
  3598. sd = script_rid2sd(st);
  3599. if( sd == NULL )
  3600. return 1;
  3601. if( sd->state.menu_or_input == 0 )
  3602. {
  3603. struct StringBuf buf;
  3604. StringBuf_Init(&buf);
  3605. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3606. {
  3607. text = script_getstr(st, i);
  3608. if( sd->npc_menu > 0 )
  3609. StringBuf_AppendStr(&buf, ":");
  3610. StringBuf_AppendStr(&buf, text);
  3611. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3612. }
  3613. st->state = RERUNLINE;
  3614. sd->state.menu_or_input = 1;
  3615. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3616. StringBuf_Destroy(&buf);
  3617. }
  3618. else if( sd->npc_menu == 0xff )
  3619. {// Cancel was pressed
  3620. sd->state.menu_or_input = 0;
  3621. st->state = END;
  3622. }
  3623. else
  3624. {// return selected option
  3625. int menu = 0;
  3626. sd->state.menu_or_input = 0;
  3627. for( i = 2; i <= script_lastdata(st); ++i )
  3628. {
  3629. text = script_getstr(st, i);
  3630. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3631. if( sd->npc_menu <= 0 )
  3632. break;// entry found
  3633. }
  3634. pc_setreg(sd, add_str("@menu"), menu);
  3635. script_pushint(st, menu);
  3636. }
  3637. return 0;
  3638. }
  3639. /// Displays a menu with options and returns the selected option.
  3640. /// Behaves like 'menu' without the target labels, except when cancel is
  3641. /// pressed.
  3642. /// When cancel is pressed, the script continues and 255 is returned.
  3643. ///
  3644. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3645. ///
  3646. /// @see menu
  3647. BUILDIN_FUNC(prompt)
  3648. {
  3649. int i;
  3650. const char *text;
  3651. TBL_PC* sd;
  3652. sd = script_rid2sd(st);
  3653. if( sd == NULL )
  3654. return 1;
  3655. if( sd->state.menu_or_input == 0 )
  3656. {
  3657. struct StringBuf buf;
  3658. StringBuf_Init(&buf);
  3659. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3660. {
  3661. text = script_getstr(st, i);
  3662. if( sd->npc_menu > 0 )
  3663. StringBuf_AppendStr(&buf, ":");
  3664. StringBuf_AppendStr(&buf, text);
  3665. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3666. }
  3667. st->state = RERUNLINE;
  3668. sd->state.menu_or_input = 1;
  3669. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3670. StringBuf_Destroy(&buf);
  3671. }
  3672. else if( sd->npc_menu == 0xff )
  3673. {// Cancel was pressed
  3674. sd->state.menu_or_input = 0;
  3675. pc_setreg(sd, add_str("@menu"), 0xff);
  3676. script_pushint(st, 0xff);
  3677. }
  3678. else
  3679. {// return selected option
  3680. int menu = 0;
  3681. sd->state.menu_or_input = 0;
  3682. for( i = 2; i <= script_lastdata(st); ++i )
  3683. {
  3684. text = script_getstr(st, i);
  3685. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3686. if( sd->npc_menu <= 0 )
  3687. break;// entry found
  3688. }
  3689. pc_setreg(sd, add_str("@menu"), menu);
  3690. script_pushint(st, menu);
  3691. }
  3692. return 0;
  3693. }
  3694. /////////////////////////////////////////////////////////////////////
  3695. // ...
  3696. //
  3697. /// Jumps to the target script label.
  3698. ///
  3699. /// goto <label>;
  3700. BUILDIN_FUNC(goto)
  3701. {
  3702. if( !data_islabel(script_getdata(st,2)) )
  3703. {
  3704. ShowError("script:goto: not a label\n");
  3705. script_reportdata(script_getdata(st,2));
  3706. st->state = END;
  3707. return 1;
  3708. }
  3709. st->pos = script_getnum(st,2);
  3710. st->state = GOTO;
  3711. return 0;
  3712. }
  3713. /*==========================================
  3714. * ユーザー定義関数の呼び出し
  3715. *------------------------------------------*/
  3716. BUILDIN_FUNC(callfunc)
  3717. {
  3718. int i, j;
  3719. struct linkdb_node** oldval;
  3720. struct script_code *scr, *oldscr;
  3721. const char* str = script_getstr(st,2);
  3722. scr = strdb_get(userfunc_db, str);
  3723. if( !scr )
  3724. {
  3725. ShowError("script:callfunc: function not found! [%s]\n", str);
  3726. st->state = END;
  3727. return 1;
  3728. }
  3729. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3730. push_copy(st->stack,i);
  3731. script_pushint(st,j); // 引数の数をプッシュ
  3732. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3733. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3734. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3735. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3736. oldscr = st->script;
  3737. oldval = st->stack->var_function;
  3738. st->pos = 0;
  3739. st->script = scr;
  3740. st->stack->defsp = st->start+5+j;
  3741. st->state = GOTO;
  3742. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3743. // ' 変数の引き継ぎ
  3744. for( i = 0; i < j; i++ )
  3745. {
  3746. struct script_data* s = script_getdatatop(st, -6-i);
  3747. if( data_isreference(s) && !s->ref )
  3748. {
  3749. char* name = str_buf + str_data[s->u.num&0x00ffffff].str;
  3750. // '@ 変数の引き継ぎ
  3751. if( name[0] == '.' && name[1] == '@' ) {
  3752. s->ref = oldval;
  3753. } else if( name[0] == '.' ) {
  3754. s->ref = &oldscr->script_vars;
  3755. }
  3756. }
  3757. }
  3758. return 0;
  3759. }
  3760. /*==========================================
  3761. * サブルーティンの呼び出し
  3762. *------------------------------------------*/
  3763. BUILDIN_FUNC(callsub)
  3764. {
  3765. int pos=script_getnum(st,2);
  3766. int i,j;
  3767. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  3768. ShowError("script:callsub: argument is not a label\n");
  3769. script_reportdata(script_getdata(st,2));
  3770. st->state=END;
  3771. return 1;
  3772. } else {
  3773. struct linkdb_node **oldval = st->stack->var_function;
  3774. for(i=st->start+3,j=0;i<st->end;i++,j++)
  3775. push_copy(st->stack,i);
  3776. script_pushint(st,j); // 引数の数をプッシュ
  3777. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3778. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3779. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3780. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3781. st->pos=pos;
  3782. st->stack->defsp=st->start+5+j;
  3783. st->state=GOTO;
  3784. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3785. // ' 変数の引き継ぎ
  3786. for(i = 0; i < j; i++) {
  3787. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  3788. if( data_isreference(s) && !s->ref ) {
  3789. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  3790. // '@ 変数の引き継ぎ
  3791. if( name[0] == '.' && name[1] == '@' ) {
  3792. s->ref = oldval;
  3793. }
  3794. }
  3795. }
  3796. }
  3797. return 0;
  3798. }
  3799. /// Retrieves an argument provided to callfunc/callsub.
  3800. /// If the argument doesn't exist
  3801. ///
  3802. /// getarg(<index>{,<default_value>}) -> <value>
  3803. BUILDIN_FUNC(getarg)
  3804. {
  3805. int idx;
  3806. int count;
  3807. int stsp;
  3808. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3809. {
  3810. ShowError("script:getarg: no callfunc or callsub!\n");
  3811. st->state = END;
  3812. return 1;
  3813. }
  3814. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  3815. stsp = st->stack->defsp - count - 5;
  3816. idx = script_getnum(st,2);
  3817. if( idx < count )
  3818. push_copy(st->stack, stsp + idx);
  3819. else if( script_hasdata(st,3) )
  3820. script_pushcopy(st, 3);
  3821. else
  3822. {
  3823. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  3824. st->state = END;
  3825. return 1;
  3826. }
  3827. return 0;
  3828. }
  3829. /// Returns from the current function, optionaly returning a value from the functions.
  3830. /// Don't use outside script functions.
  3831. ///
  3832. /// return;
  3833. /// return <value>;
  3834. BUILDIN_FUNC(return)
  3835. {
  3836. if( script_hasdata(st,2) )
  3837. {// return value
  3838. struct script_data* data;
  3839. script_pushcopy(st, 2);
  3840. data = script_getdatatop(st, -1);
  3841. if( data_isreference(data) )
  3842. {
  3843. char* name = reference_getname(data);
  3844. if( name[0] == '.' && name[1] == '@' )
  3845. {// temporary script variable, convert to value
  3846. get_val(st, data);
  3847. if( data_isstring(data) )
  3848. {// duplicate the string
  3849. data->type = C_STR;
  3850. data->u.str = aStrdup(data->u.str);
  3851. }
  3852. }
  3853. else if( name[0] == '.' && !data->ref )
  3854. {// script variable, link to current script
  3855. data->ref = &st->script->script_vars;
  3856. }
  3857. }
  3858. }
  3859. else
  3860. {// no return value
  3861. script_pushnil(st);
  3862. }
  3863. st->state = RETFUNC;
  3864. return 0;
  3865. }
  3866. /// Returns a random number from 0 to <range>-1.
  3867. /// Or returns a random number from <min> to <max>.
  3868. /// If <min> is greater than <max>, their numbers are switched.
  3869. /// rand(<range>) -> <int>
  3870. /// rand(<min>,<max>) -> <int>
  3871. BUILDIN_FUNC(rand)
  3872. {
  3873. int range;
  3874. int min;
  3875. int max;
  3876. if( script_hasdata(st,3) )
  3877. {// min,max
  3878. min = script_getnum(st,2);
  3879. max = script_getnum(st,3);
  3880. if( max < min )
  3881. swap(min, max);
  3882. range = max - min + 1;
  3883. }
  3884. else
  3885. {// range
  3886. min = 0;
  3887. range = script_getnum(st,2);
  3888. }
  3889. if( range <= 1 )
  3890. script_pushint(st, min);
  3891. else
  3892. script_pushint(st, rand()%range + min);
  3893. return 0;
  3894. }
  3895. /*==========================================
  3896. *
  3897. *------------------------------------------*/
  3898. BUILDIN_FUNC(warp)
  3899. {
  3900. int ret;
  3901. int x,y;
  3902. const char* str;
  3903. TBL_PC* sd = script_rid2sd(st);
  3904. nullpo_retr(0, sd);
  3905. str = script_getstr(st,2);
  3906. x = script_getnum(st,3);
  3907. y = script_getnum(st,4);
  3908. if(strcmp(str,"Random")==0)
  3909. ret = pc_randomwarp(sd,3);
  3910. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3911. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3912. else
  3913. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3914. if( ret ) {
  3915. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3916. script_reportsrc(st);
  3917. }
  3918. return 0;
  3919. }
  3920. /*==========================================
  3921. * エリア指定ワープ
  3922. *------------------------------------------*/
  3923. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3924. {
  3925. int x,y;
  3926. unsigned int map;
  3927. map=va_arg(ap, unsigned int);
  3928. x=va_arg(ap,int);
  3929. y=va_arg(ap,int);
  3930. if(map == 0)
  3931. pc_randomwarp((TBL_PC *)bl,3);
  3932. else
  3933. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3934. return 0;
  3935. }
  3936. BUILDIN_FUNC(areawarp)
  3937. {
  3938. int x,y,m;
  3939. unsigned int index;
  3940. const char *str;
  3941. const char *mapname;
  3942. int x0,y0,x1,y1;
  3943. mapname=script_getstr(st,2);
  3944. x0=script_getnum(st,3);
  3945. y0=script_getnum(st,4);
  3946. x1=script_getnum(st,5);
  3947. y1=script_getnum(st,6);
  3948. str=script_getstr(st,7);
  3949. x=script_getnum(st,8);
  3950. y=script_getnum(st,9);
  3951. if( (m=map_mapname2mapid(mapname))< 0)
  3952. return 0;
  3953. if(strcmp(str,"Random")==0)
  3954. index = 0;
  3955. else if(!(index=mapindex_name2id(str)))
  3956. return 0;
  3957. map_foreachinarea(buildin_areawarp_sub,
  3958. m,x0,y0,x1,y1,BL_PC, index,x,y );
  3959. return 0;
  3960. }
  3961. /*==========================================
  3962. * warpchar [LuzZza]
  3963. * Useful for warp one player from
  3964. * another player npc-session.
  3965. * Using: warpchar "mapname",x,y,Char_ID;
  3966. *------------------------------------------*/
  3967. BUILDIN_FUNC(warpchar)
  3968. {
  3969. int x,y,a,i;
  3970. const char *str;
  3971. TBL_PC *sd, **pl_allsd;
  3972. int users;
  3973. str=script_getstr(st,2);
  3974. x=script_getnum(st,3);
  3975. y=script_getnum(st,4);
  3976. a=script_getnum(st,5);
  3977. pl_allsd = map_getallusers(&users);
  3978. for(i=0; i<users; i++) {
  3979. sd = pl_allsd[i];
  3980. if(sd->status.char_id == a) {
  3981. if(strcmp(str, "Random") == 0)
  3982. pc_randomwarp(sd, 3);
  3983. else if(strcmp(str, "SavePoint") == 0)
  3984. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3985. else
  3986. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3987. }
  3988. }
  3989. return 0;
  3990. }
  3991. /*==========================================
  3992. * Warpparty - [Fredzilla]
  3993. * Syntax: warpparty "mapname",x,y,Party_ID;
  3994. *------------------------------------------*/
  3995. BUILDIN_FUNC(warpparty)
  3996. {
  3997. int x,y;
  3998. const char *str;
  3999. int p_id;
  4000. int i;
  4001. unsigned short mapindex;
  4002. TBL_PC *pl_sd;
  4003. struct party_data *p=NULL;
  4004. str=script_getstr(st,2);
  4005. x=script_getnum(st,3);
  4006. y=script_getnum(st,4);
  4007. p_id=script_getnum(st,5);
  4008. if(p_id < 1)
  4009. return 0;
  4010. p = party_search(p_id);
  4011. if (!p)
  4012. return 0;
  4013. if(strcmp(str,"Random")==0)
  4014. {
  4015. for (i = 0; i < MAX_PARTY; i++)
  4016. {
  4017. if ((pl_sd = p->data[i].sd))
  4018. {
  4019. if(map[pl_sd->bl.m].flag.nowarp)
  4020. continue;
  4021. pc_randomwarp(pl_sd,3);
  4022. }
  4023. }
  4024. }
  4025. else if(strcmp(str,"SavePointAll")==0)
  4026. {
  4027. for (i = 0; i < MAX_PARTY; i++)
  4028. {
  4029. if ((pl_sd = p->data[i].sd))
  4030. {
  4031. if(map[pl_sd->bl.m].flag.noreturn)
  4032. continue;
  4033. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4034. }
  4035. }
  4036. }
  4037. else if(strcmp(str,"SavePoint")==0)
  4038. {
  4039. pl_sd=script_rid2sd(st);
  4040. if (!pl_sd) return 0;
  4041. mapindex=pl_sd->status.save_point.map;
  4042. x=pl_sd->status.save_point.x;
  4043. y=pl_sd->status.save_point.y;
  4044. for (i = 0; i < MAX_PARTY; i++)
  4045. {
  4046. if ((pl_sd = p->data[i].sd))
  4047. {
  4048. if(map[pl_sd->bl.m].flag.noreturn)
  4049. continue;
  4050. pc_setpos(pl_sd,mapindex,x,y,3);
  4051. }
  4052. }
  4053. }
  4054. else if(strcmp(str,"Leader")==0)
  4055. {
  4056. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4057. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4058. return 0;
  4059. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  4060. return 0;
  4061. mapindex = p->data[i].sd->mapindex;
  4062. x = p->data[i].sd->bl.x;
  4063. y = p->data[i].sd->bl.y;
  4064. for (i = 0; i < MAX_PARTY; i++)
  4065. {
  4066. pl_sd = p->data[i].sd;
  4067. if (!pl_sd)
  4068. continue;
  4069. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4070. continue;
  4071. pc_setpos(pl_sd,mapindex,x,y,3);
  4072. }
  4073. }
  4074. else
  4075. {
  4076. mapindex = mapindex_name2id(str);
  4077. if (!mapindex) //Show source of npc error.
  4078. return 1;
  4079. for (i = 0; i < MAX_PARTY; i++)
  4080. {
  4081. if ((pl_sd = p->data[i].sd))
  4082. {
  4083. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4084. continue;
  4085. pc_setpos(pl_sd,mapindex,x,y,3);
  4086. }
  4087. }
  4088. }
  4089. return 0;
  4090. }
  4091. /*==========================================
  4092. * Warpguild - [Fredzilla]
  4093. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4094. *------------------------------------------*/
  4095. BUILDIN_FUNC(warpguild)
  4096. {
  4097. int x,y;
  4098. unsigned short mapindex;
  4099. const char *str;
  4100. int g;
  4101. int i;
  4102. TBL_PC *pl_sd, **pl_allsd;
  4103. int users;
  4104. TBL_PC *sd;
  4105. str=script_getstr(st,2);
  4106. x=script_getnum(st,3);
  4107. y=script_getnum(st,4);
  4108. g=script_getnum(st,5);
  4109. sd=script_rid2sd(st);
  4110. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4111. return 0;
  4112. if(g < 1)
  4113. return 0;
  4114. pl_allsd = map_getallusers(&users);
  4115. if(strcmp(str,"Random")==0)
  4116. {
  4117. for (i = 0; i < users; i++)
  4118. {
  4119. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4120. {
  4121. if(map[pl_sd->bl.m].flag.nowarp)
  4122. continue;
  4123. pc_randomwarp(pl_sd,3);
  4124. }
  4125. }
  4126. }
  4127. else if(strcmp(str,"SavePointAll")==0)
  4128. {
  4129. if(map[sd->bl.m].flag.noreturn)
  4130. return 0;
  4131. for (i = 0; i < users; i++)
  4132. {
  4133. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4134. {
  4135. if(map[pl_sd->bl.m].flag.noreturn)
  4136. continue;
  4137. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4138. }
  4139. }
  4140. }
  4141. else if(strcmp(str,"SavePoint")==0)
  4142. {
  4143. if(map[sd->bl.m].flag.noreturn)
  4144. return 0;
  4145. mapindex=sd->status.save_point.map;
  4146. x=sd->status.save_point.x;
  4147. y=sd->status.save_point.y;
  4148. for (i = 0; i < users; i++)
  4149. {
  4150. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4151. {
  4152. if(map[pl_sd->bl.m].flag.noreturn)
  4153. continue;
  4154. pc_setpos(pl_sd,mapindex,x,y,3);
  4155. }
  4156. }
  4157. }
  4158. else
  4159. {
  4160. mapindex = mapindex_name2id(str);
  4161. for (i = 0; i < users; i++)
  4162. {
  4163. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4164. {
  4165. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4166. continue;
  4167. pc_setpos(pl_sd,mapindex,x,y,3);
  4168. }
  4169. }
  4170. }
  4171. return 0;
  4172. }
  4173. /*==========================================
  4174. *
  4175. *------------------------------------------*/
  4176. BUILDIN_FUNC(heal)
  4177. {
  4178. TBL_PC *sd;
  4179. int hp,sp;
  4180. sd = script_rid2sd(st);
  4181. if (!sd) return 0;
  4182. hp=script_getnum(st,2);
  4183. sp=script_getnum(st,3);
  4184. status_heal(&sd->bl, hp, sp, 1);
  4185. return 0;
  4186. }
  4187. /*==========================================
  4188. *
  4189. *------------------------------------------*/
  4190. BUILDIN_FUNC(itemheal)
  4191. {
  4192. TBL_PC *sd;
  4193. int hp,sp;
  4194. hp=script_getnum(st,2);
  4195. sp=script_getnum(st,3);
  4196. if(potion_flag==1) {
  4197. potion_hp = hp;
  4198. potion_sp = sp;
  4199. return 0;
  4200. }
  4201. sd = script_rid2sd(st);
  4202. if (!sd) return 0;
  4203. pc_itemheal(sd,sd->itemid,hp,sp);
  4204. return 0;
  4205. }
  4206. /*==========================================
  4207. *
  4208. *------------------------------------------*/
  4209. BUILDIN_FUNC(percentheal)
  4210. {
  4211. int hp,sp;
  4212. hp=script_getnum(st,2);
  4213. sp=script_getnum(st,3);
  4214. if(potion_flag==1) {
  4215. potion_per_hp = hp;
  4216. potion_per_sp = sp;
  4217. return 0;
  4218. }
  4219. pc_percentheal(script_rid2sd(st),hp,sp);
  4220. return 0;
  4221. }
  4222. /*==========================================
  4223. *
  4224. *------------------------------------------*/
  4225. BUILDIN_FUNC(jobchange)
  4226. {
  4227. int job, upper=-1;
  4228. job=script_getnum(st,2);
  4229. if( script_hasdata(st,3) )
  4230. upper=script_getnum(st,3);
  4231. if (pcdb_checkid(job))
  4232. {
  4233. pc_jobchange(script_rid2sd(st),job, upper);
  4234. if(use_irc && irc_announce_jobchange_flag)
  4235. irc_announce_jobchange(script_rid2sd(st));
  4236. }
  4237. return 0;
  4238. }
  4239. /*==========================================
  4240. *
  4241. *------------------------------------------*/
  4242. BUILDIN_FUNC(jobname)
  4243. {
  4244. int class_=script_getnum(st,2);
  4245. script_pushconststr(st,job_name(class_));
  4246. return 0;
  4247. }
  4248. /*==========================================
  4249. *
  4250. *------------------------------------------*/
  4251. BUILDIN_FUNC(input)
  4252. {
  4253. TBL_PC *sd = script_rid2sd(st);
  4254. struct script_data *data = script_getdata(st,2);
  4255. int num = data->u.num;
  4256. char *name=str_buf+str_data[num&0x00ffffff].str;
  4257. char postfix = name[strlen(name)-1];
  4258. if (!sd) return 1;
  4259. if( !data_isreference(data) ){
  4260. ShowError("script:input: not a variable\n");
  4261. script_reportdata(data);
  4262. return 1;
  4263. }
  4264. if( !sd->state.menu_or_input )
  4265. { // first invocation, display npc input box
  4266. sd->state.menu_or_input = 1;
  4267. st->state = RERUNLINE;
  4268. if( postfix == '$' )
  4269. clif_scriptinputstr(sd,st->oid);
  4270. else
  4271. clif_scriptinput(sd,st->oid);
  4272. }
  4273. else
  4274. { // take received text/value and store it in the designated variable
  4275. sd->state.menu_or_input = 0;
  4276. if( postfix == '$' )
  4277. {
  4278. set_reg(st,sd,num,name,(void*)sd->npc_str,script_getref(st,2));
  4279. }
  4280. else
  4281. {
  4282. // limit the input to a non-negative value smaller than 'vending_max_value' (for scripts that didn't check this)
  4283. sd->npc_amount = cap_value(sd->npc_amount, 0, battle_config.vending_max_value);
  4284. set_reg(st,sd,num,name,(void*)sd->npc_amount,script_getref(st,2));
  4285. }
  4286. }
  4287. return 0;
  4288. }
  4289. /*==========================================
  4290. * 変数設定
  4291. *------------------------------------------*/
  4292. BUILDIN_FUNC(set)
  4293. {
  4294. TBL_PC *sd=NULL;
  4295. int num=st->stack->stack_data[st->start+2].u.num;
  4296. char *name=str_buf+str_data[num&0x00ffffff].str;
  4297. char prefix=*name;
  4298. char postfix=name[strlen(name)-1];
  4299. if( !data_isreference(script_getdata(st,2)) ){
  4300. ShowError("script:set: not a variable\n");
  4301. script_reportdata(script_getdata(st,2));
  4302. st->state = END;
  4303. return 1;
  4304. }
  4305. if(not_server_variable(prefix))
  4306. sd=script_rid2sd(st);
  4307. if( postfix=='$' ){
  4308. // 文字列
  4309. const char *str = script_getstr(st,3);
  4310. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  4311. }else{
  4312. // 数値
  4313. int val = script_getnum(st,3);
  4314. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  4315. }
  4316. return 0;
  4317. }
  4318. /////////////////////////////////////////////////////////////////////
  4319. /// Array variables
  4320. ///
  4321. /// Returns the size of the specified array
  4322. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4323. {
  4324. int32 ret = idx;
  4325. if( isstring )
  4326. {
  4327. for( ; idx < 128; ++idx )
  4328. {
  4329. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4330. if( str && *str )
  4331. ret = idx + 1;
  4332. }
  4333. }
  4334. else
  4335. {
  4336. for( ; idx < 128; ++idx )
  4337. {
  4338. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4339. if( num )
  4340. ret = idx + 1;
  4341. }
  4342. }
  4343. return ret;
  4344. }
  4345. /// Sets values of an array, from the starting index.
  4346. /// ex: setarray arr[1],1,2,3;
  4347. ///
  4348. /// setarray <array variable>,<value1>{,<value2>...};
  4349. BUILDIN_FUNC(setarray)
  4350. {
  4351. struct script_data* data;
  4352. char* name;
  4353. int32 start;
  4354. int32 end;
  4355. int32 id;
  4356. int32 i;
  4357. TBL_PC* sd = NULL;
  4358. data = script_getdata(st, 2);
  4359. if( !data_isreference(data) )
  4360. {
  4361. ShowError("script:setarray: not a variable\n");
  4362. script_reportdata(data);
  4363. st->state = END;
  4364. return 1;// not a variable
  4365. }
  4366. id = reference_getid(data);
  4367. start = reference_getindex(data);
  4368. name = reference_getname(data);
  4369. if( not_array_variable(*name) )
  4370. {
  4371. ShowError("script:setarray: illegal scope\n");
  4372. script_reportdata(data);
  4373. st->state = END;
  4374. return 1;// not supported
  4375. }
  4376. if( not_server_variable(*name) )
  4377. {
  4378. sd = script_rid2sd(st);
  4379. if( sd == NULL )
  4380. return 1;// no player attached
  4381. }
  4382. end = start + script_lastdata(st) - 2;
  4383. if( end > 127 )
  4384. end = 127;
  4385. if( is_string_variable(name) )
  4386. {// string array
  4387. for( i = 3; start < end; ++start, ++i )
  4388. {
  4389. void* v = (void*)script_getstr(st,i);
  4390. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4391. }
  4392. }
  4393. else
  4394. {// int array
  4395. for( i = 3; start < end; ++start, ++i )
  4396. {
  4397. void* v = (void*)script_getnum(st,i);
  4398. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4399. }
  4400. }
  4401. return 0;
  4402. }
  4403. /// Sets count values of an array, from the starting index.
  4404. /// ex: cleararray arr[0],0,1;
  4405. ///
  4406. /// cleararray <array variable>,<value>,<count>;
  4407. BUILDIN_FUNC(cleararray)
  4408. {
  4409. struct script_data* data;
  4410. char* name;
  4411. int32 start;
  4412. int32 end;
  4413. int32 id;
  4414. void* v;
  4415. TBL_PC* sd = NULL;
  4416. data = script_getdata(st, 2);
  4417. if( !data_isreference(data) )
  4418. {
  4419. ShowError("script:cleararray: not a variable\n");
  4420. script_reportdata(data);
  4421. st->state = END;
  4422. return 1;// not a variable
  4423. }
  4424. id = reference_getid(data);
  4425. start = reference_getindex(data);
  4426. name = reference_getname(data);
  4427. if( not_array_variable(*name) )
  4428. {
  4429. ShowError("script:cleararray: illegal scope\n");
  4430. script_reportdata(data);
  4431. st->state = END;
  4432. return 1;// not supported
  4433. }
  4434. if( not_server_variable(*name) )
  4435. {
  4436. sd = script_rid2sd(st);
  4437. if( sd == NULL )
  4438. return 1;// no player attached
  4439. }
  4440. if( is_string_variable(name) )
  4441. v = (void*)script_getstr(st, 3);
  4442. else
  4443. v = (void*)script_getnum(st, 3);
  4444. end = start + script_getnum(st, 4);
  4445. if( end > 127 )
  4446. end = 127;
  4447. for( ; start < end; ++start )
  4448. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4449. return 0;
  4450. }
  4451. /// Copies data from one array to another.
  4452. /// ex: copyarray arr[0],arr[2],2;
  4453. ///
  4454. /// copyarray <destination array variable>,<source array variable>,<count>;
  4455. BUILDIN_FUNC(copyarray)
  4456. {
  4457. struct script_data* data1;
  4458. struct script_data* data2;
  4459. char* name1;
  4460. char* name2;
  4461. int32 idx1;
  4462. int32 idx2;
  4463. int32 id1;
  4464. int32 id2;
  4465. void* v;
  4466. int32 i;
  4467. int32 count;
  4468. TBL_PC* sd = NULL;
  4469. data1 = script_getdata(st, 2);
  4470. data2 = script_getdata(st, 3);
  4471. if( !data_isreference(data1) || !data_isreference(data2) )
  4472. {
  4473. ShowError("script:copyarray: not a variable\n");
  4474. script_reportdata(data1);
  4475. script_reportdata(data2);
  4476. st->state = END;
  4477. return 1;// not a variable
  4478. }
  4479. id1 = reference_getid(data1);
  4480. id2 = reference_getid(data2);
  4481. idx1 = reference_getindex(data1);
  4482. idx2 = reference_getindex(data2);
  4483. name1 = reference_getname(data1);
  4484. name2 = reference_getname(data2);
  4485. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4486. {
  4487. ShowError("script:copyarray: illegal scope\n");
  4488. script_reportdata(data1);
  4489. script_reportdata(data2);
  4490. st->state = END;
  4491. return 1;// not supported
  4492. }
  4493. if( is_string_variable(name1) != is_string_variable(name2) )
  4494. {
  4495. ShowError("script:copyarray: type mismatch\n");
  4496. script_reportdata(data1);
  4497. script_reportdata(data2);
  4498. st->state = END;
  4499. return 1;// data type mismatch
  4500. }
  4501. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4502. {
  4503. sd = script_rid2sd(st);
  4504. if( sd == NULL )
  4505. return 1;// no player attached
  4506. }
  4507. count = script_getnum(st, 4);
  4508. if( count > 128 - idx1 )
  4509. count = 128 - idx1;
  4510. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4511. return 0;// nothing to copy
  4512. if( id1 == id2 && idx1 > idx2 )
  4513. {// destination might be overlapping the source - copy in reverse order
  4514. for( i = count - 1; i >= 0; --i )
  4515. {
  4516. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4517. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4518. }
  4519. }
  4520. else
  4521. {// normal copy
  4522. for( i = 0; i < count; ++i )
  4523. {
  4524. if( idx2 + i < 128 )
  4525. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4526. else// out of range - assume ""/0
  4527. v = (void*)(is_string_variable(name1) ? "" : 0);
  4528. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4529. }
  4530. }
  4531. return 0;
  4532. }
  4533. /// Returns the size of the array.
  4534. /// Assumes that everything before the starting index exists.
  4535. /// ex: getarraysize(arr[3])
  4536. ///
  4537. /// getarraysize(<array variable>) -> <int>
  4538. BUILDIN_FUNC(getarraysize)
  4539. {
  4540. struct script_data* data;
  4541. char* name;
  4542. data = script_getdata(st, 2);
  4543. if( !data_isreference(data) )
  4544. {
  4545. ShowError("script:getarraysize: not a variable\n");
  4546. script_reportdata(data);
  4547. script_pushnil(st);
  4548. st->state = END;
  4549. return 1;// not a variable
  4550. }
  4551. name = reference_getname(data);
  4552. if( not_array_variable(*name) )
  4553. {
  4554. ShowError("script:getarraysize: illegal scope\n");
  4555. script_reportdata(data);
  4556. script_pushnil(st);
  4557. st->state = END;
  4558. return 1;// not supported
  4559. }
  4560. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4561. return 0;
  4562. }
  4563. /// Deletes count or all the elements in an array, from the starting index.
  4564. /// ex: deletearray arr[4],2;
  4565. ///
  4566. /// deletearray <array variable>;
  4567. /// deletearray <array variable>,<count>;
  4568. BUILDIN_FUNC(deletearray)
  4569. {
  4570. struct script_data* data;
  4571. char* name;
  4572. int start;
  4573. int end;
  4574. int id;
  4575. TBL_PC *sd = NULL;
  4576. data = script_getdata(st, 2);
  4577. if( !data_isreference(data) )
  4578. {
  4579. ShowError("script:deletearray: not a variable\n");
  4580. script_reportdata(data);
  4581. st->state = END;
  4582. return 1;// not a variable
  4583. }
  4584. id = reference_getid(data);
  4585. start = reference_getindex(data);
  4586. name = reference_getname(data);
  4587. if( not_array_variable(*name) )
  4588. {
  4589. ShowError("script:deletearray: illegal scope\n");
  4590. script_reportdata(data);
  4591. st->state = END;
  4592. return 1;// not supported
  4593. }
  4594. if( not_server_variable(*name) )
  4595. {
  4596. sd = script_rid2sd(st);
  4597. if( sd == NULL )
  4598. return 1;// no player attached
  4599. }
  4600. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4601. if( start >= end )
  4602. return 0;// nothing to free
  4603. if( script_hasdata(st,3) )
  4604. {
  4605. int count = script_getnum(st, 3);
  4606. if( count > end - start )
  4607. count = end - start;
  4608. if( count <= 0 )
  4609. return 0;// nothing to free
  4610. // move rest of the elements backward
  4611. for( ; start + count < end; ++start )
  4612. {
  4613. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4614. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4615. }
  4616. }
  4617. // clear the rest of the array
  4618. if( is_string_variable(name) )
  4619. {
  4620. for( ; start < end; ++start )
  4621. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4622. }
  4623. else
  4624. {
  4625. for( ; start < end; ++start )
  4626. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4627. }
  4628. return 0;
  4629. }
  4630. /// Returns a reference to the target index of the array variable.
  4631. /// Equivalent to var[index].
  4632. ///
  4633. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4634. BUILDIN_FUNC(getelementofarray)
  4635. {
  4636. struct script_data* data;
  4637. char* name;
  4638. int32 id;
  4639. int i;
  4640. data = script_getdata(st, 2);
  4641. if( !data_isreference(data) )
  4642. {
  4643. ShowError("script:getelementofarray: not a variable\n");
  4644. script_reportdata(data);
  4645. script_pushnil(st);
  4646. st->state = END;
  4647. return 1;// not a variable
  4648. }
  4649. id = reference_getid(data);
  4650. name = reference_getname(data);
  4651. if( not_array_variable(*name) )
  4652. {
  4653. ShowError("script:getelementofarray: illegal scope\n");
  4654. script_reportdata(data);
  4655. script_pushnil(st);
  4656. st->state = END;
  4657. return 1;// not supported
  4658. }
  4659. i = script_getnum(st, 3);
  4660. if( i < 0 || i >= 128 )
  4661. {
  4662. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4663. script_reportdata(data);
  4664. script_pushnil(st);
  4665. st->state = END;
  4666. return 1;// out of range
  4667. }
  4668. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4669. return 0;
  4670. }
  4671. /////////////////////////////////////////////////////////////////////
  4672. /// ...
  4673. ///
  4674. /*==========================================
  4675. *
  4676. *------------------------------------------*/
  4677. BUILDIN_FUNC(setlook)
  4678. {
  4679. int type,val;
  4680. type=script_getnum(st,2);
  4681. val=script_getnum(st,3);
  4682. pc_changelook(script_rid2sd(st),type,val);
  4683. return 0;
  4684. }
  4685. /*==========================================
  4686. *
  4687. *------------------------------------------*/
  4688. BUILDIN_FUNC(cutin)
  4689. {
  4690. clif_cutin(script_rid2sd(st),script_getstr(st,2),script_getnum(st,3));
  4691. return 0;
  4692. }
  4693. /*==========================================
  4694. *
  4695. *------------------------------------------*/
  4696. BUILDIN_FUNC(viewpoint)
  4697. {
  4698. int type,x,y,id,color;
  4699. type=script_getnum(st,2);
  4700. x=script_getnum(st,3);
  4701. y=script_getnum(st,4);
  4702. id=script_getnum(st,5);
  4703. color=script_getnum(st,6);
  4704. clif_viewpoint(script_rid2sd(st),st->oid,type,x,y,id,color);
  4705. return 0;
  4706. }
  4707. /*==========================================
  4708. *
  4709. *------------------------------------------*/
  4710. BUILDIN_FUNC(countitem)
  4711. {
  4712. int nameid, i;
  4713. int count = 0;
  4714. struct script_data* data;
  4715. TBL_PC* sd = script_rid2sd(st);
  4716. if (!sd) {
  4717. script_pushint(st,0);
  4718. return 0;
  4719. }
  4720. data = script_getdata(st,2);
  4721. get_val(st,data);
  4722. if( data_isstring(data) ) {
  4723. const char* name = conv_str(st,data);
  4724. struct item_data* item_data;
  4725. if((item_data = itemdb_searchname(name)) != NULL)
  4726. nameid = item_data->nameid;
  4727. else
  4728. nameid = 0;
  4729. } else
  4730. nameid = conv_num(st,data);
  4731. if (nameid < 500) {
  4732. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4733. script_reportsrc(st);
  4734. script_pushint(st,0);
  4735. return 1;
  4736. }
  4737. for(i = 0; i < MAX_INVENTORY; i++)
  4738. if(sd->status.inventory[i].nameid == nameid)
  4739. count += sd->status.inventory[i].amount;
  4740. script_pushint(st,count);
  4741. return 0;
  4742. }
  4743. /*==========================================
  4744. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4745. * returns number of items that meet the conditions
  4746. *------------------------------------------*/
  4747. BUILDIN_FUNC(countitem2)
  4748. {
  4749. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4750. int count = 0;
  4751. int i;
  4752. struct script_data* data;
  4753. TBL_PC* sd = script_rid2sd(st);
  4754. if (!sd) {
  4755. script_pushint(st,0);
  4756. return 0;
  4757. }
  4758. data = script_getdata(st,2);
  4759. get_val(st,data);
  4760. if( data_isstring(data) ) {
  4761. const char* name = conv_str(st,data);
  4762. struct item_data* item_data;
  4763. if((item_data = itemdb_searchname(name)) != NULL)
  4764. nameid = item_data->nameid;
  4765. else
  4766. nameid = 0;
  4767. } else
  4768. nameid = conv_num(st,data);
  4769. iden = script_getnum(st,3);
  4770. ref = script_getnum(st,4);
  4771. attr = script_getnum(st,5);
  4772. c1 = (short)script_getnum(st,6);
  4773. c2 = (short)script_getnum(st,7);
  4774. c3 = (short)script_getnum(st,8);
  4775. c4 = (short)script_getnum(st,9);
  4776. if (nameid < 500) {
  4777. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4778. script_pushint(st,0);
  4779. return 1;
  4780. }
  4781. for(i = 0; i < MAX_INVENTORY; i++)
  4782. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4783. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4784. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4785. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4786. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4787. sd->status.inventory[i].card[3] == c4
  4788. )
  4789. count += sd->status.inventory[i].amount;
  4790. script_pushint(st,count);
  4791. return 0;
  4792. }
  4793. /*==========================================
  4794. * 重量チェック
  4795. *------------------------------------------*/
  4796. BUILDIN_FUNC(checkweight)
  4797. {
  4798. int nameid=0,amount,i;
  4799. unsigned long weight;
  4800. TBL_PC *sd;
  4801. struct script_data *data;
  4802. sd = script_rid2sd(st);
  4803. data=script_getdata(st,2);
  4804. get_val(st,data);
  4805. if( data_isstring(data) ){
  4806. const char *name=conv_str(st,data);
  4807. struct item_data *item_data = itemdb_searchname(name);
  4808. if( item_data )
  4809. nameid=item_data->nameid;
  4810. }else
  4811. nameid=conv_num(st,data);
  4812. amount=script_getnum(st,3);
  4813. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4814. script_pushint(st,0);
  4815. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4816. return 1;
  4817. }
  4818. weight = itemdb_weight(nameid)*amount;
  4819. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4820. script_pushint(st,0);
  4821. } else {
  4822. //Check if the inventory ain't full.
  4823. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4824. i = pc_search_inventory(sd,0);
  4825. if (i >= 0) //Empty slot available.
  4826. script_pushint(st,1);
  4827. else //Inventory full
  4828. script_pushint(st,0);
  4829. }
  4830. return 0;
  4831. }
  4832. /*==========================================
  4833. * getitem <item id>,<amount>{,<character ID>};
  4834. * getitem "<item name>",<amount>{,<character ID>};
  4835. *------------------------------------------*/
  4836. BUILDIN_FUNC(getitem)
  4837. {
  4838. int nameid,amount,flag = 0;
  4839. struct item it;
  4840. TBL_PC *sd;
  4841. struct script_data *data;
  4842. data=script_getdata(st,2);
  4843. get_val(st,data);
  4844. if( data_isstring(data) )
  4845. {// "<item name>"
  4846. const char *name=conv_str(st,data);
  4847. struct item_data *item_data = itemdb_searchname(name);
  4848. if( item_data == NULL ){
  4849. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4850. return 1; //No item created.
  4851. }
  4852. nameid=item_data->nameid;
  4853. } else if( data_isint(data) )
  4854. {// <item id>
  4855. nameid=conv_num(st,data);
  4856. //Violet Box, Blue Box, etc - random item pick
  4857. if( nameid < 0 ) {
  4858. nameid=itemdb_searchrandomid(-nameid);
  4859. flag = 1;
  4860. }
  4861. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4862. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4863. return 1; //No item created.
  4864. }
  4865. } else {
  4866. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4867. return 1;
  4868. }
  4869. // <amount>
  4870. if( (amount=script_getnum(st,3)) <= 0)
  4871. return 0; //return if amount <=0, skip the useles iteration
  4872. memset(&it,0,sizeof(it));
  4873. it.nameid=nameid;
  4874. if(!flag)
  4875. it.identify=1;
  4876. else
  4877. it.identify=itemdb_isidentified(nameid);
  4878. if( script_hasdata(st,4) )
  4879. {// <character ID>
  4880. sd=map_id2sd(script_getnum(st,4));
  4881. } else
  4882. {// attached player
  4883. sd=script_rid2sd(st);
  4884. }
  4885. if( sd == NULL ) // no target
  4886. return 0;
  4887. if( pet_create_egg(sd, nameid) )
  4888. amount = 1; //This is a pet!
  4889. else if( (flag=pc_additem(sd,&it,amount)) ){
  4890. clif_additem(sd,0,0,flag);
  4891. if( pc_candrop(sd,&it) )
  4892. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4893. }
  4894. //Logs items, got from (N)PC scripts [Lupus]
  4895. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4896. log_pick_pc(sd, "N", nameid, amount, NULL);
  4897. return 0;
  4898. }
  4899. /*==========================================
  4900. *
  4901. *------------------------------------------*/
  4902. BUILDIN_FUNC(getitem2)
  4903. {
  4904. int nameid,amount,flag = 0;
  4905. int iden,ref,attr,c1,c2,c3,c4;
  4906. struct item_data *item_data;
  4907. struct item item_tmp;
  4908. TBL_PC *sd;
  4909. struct script_data *data;
  4910. sd = script_rid2sd(st);
  4911. data=script_getdata(st,2);
  4912. get_val(st,data);
  4913. if( data_isstring(data) ){
  4914. const char *name=conv_str(st,data);
  4915. struct item_data *item_data = itemdb_searchname(name);
  4916. if( item_data )
  4917. nameid=item_data->nameid;
  4918. else
  4919. nameid=UNKNOWN_ITEM_ID;
  4920. }else
  4921. nameid=conv_num(st,data);
  4922. amount=script_getnum(st,3);
  4923. iden=script_getnum(st,4);
  4924. ref=script_getnum(st,5);
  4925. attr=script_getnum(st,6);
  4926. c1=script_getnum(st,7);
  4927. c2=script_getnum(st,8);
  4928. c3=script_getnum(st,9);
  4929. c4=script_getnum(st,10);
  4930. if( script_hasdata(st,11) ) //アイテムを指定したIDに渡す
  4931. sd=map_id2sd(script_getnum(st,11));
  4932. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  4933. return 0;
  4934. if(nameid<0) { // ランダム
  4935. nameid=itemdb_searchrandomid(-nameid);
  4936. flag = 1;
  4937. }
  4938. if(nameid > 0) {
  4939. memset(&item_tmp,0,sizeof(item_tmp));
  4940. item_data=itemdb_exists(nameid);
  4941. if (item_data == NULL)
  4942. return -1;
  4943. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4944. if(ref > 10) ref = 10;
  4945. }
  4946. else if(item_data->type==IT_PETEGG) {
  4947. iden = 1;
  4948. ref = 0;
  4949. }
  4950. else {
  4951. iden = 1;
  4952. ref = attr = 0;
  4953. }
  4954. item_tmp.nameid=nameid;
  4955. if(!flag)
  4956. item_tmp.identify=iden;
  4957. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4958. item_tmp.identify=0;
  4959. item_tmp.refine=ref;
  4960. item_tmp.attribute=attr;
  4961. item_tmp.card[0]=c1;
  4962. item_tmp.card[1]=c2;
  4963. item_tmp.card[2]=c3;
  4964. item_tmp.card[3]=c4;
  4965. if((flag = pc_additem(sd,&item_tmp,amount))) {
  4966. clif_additem(sd,0,0,flag);
  4967. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4968. }
  4969. //Logs items, got from (N)PC scripts [Lupus]
  4970. if(log_config.enable_logs&0x40)
  4971. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4972. }
  4973. return 0;
  4974. }
  4975. /*==========================================
  4976. * gets an item with someone's name inscribed [Skotlex]
  4977. * getinscribeditem item_num, character_name
  4978. * Returned Qty is always 1, only works on equip-able
  4979. * equipment
  4980. *------------------------------------------*/
  4981. BUILDIN_FUNC(getnameditem)
  4982. {
  4983. int nameid;
  4984. struct item item_tmp;
  4985. TBL_PC *sd, *tsd;
  4986. struct script_data *data;
  4987. sd = script_rid2sd(st);
  4988. if (sd == NULL)
  4989. { //Player not attached!
  4990. script_pushint(st,0);
  4991. return 0;
  4992. }
  4993. data=script_getdata(st,2);
  4994. get_val(st,data);
  4995. if( data_isstring(data) ){
  4996. const char *name=conv_str(st,data);
  4997. struct item_data *item_data = itemdb_searchname(name);
  4998. if( item_data == NULL)
  4999. { //Failed
  5000. script_pushint(st,0);
  5001. return 0;
  5002. }
  5003. nameid = item_data->nameid;
  5004. }else
  5005. nameid = conv_num(st,data);
  5006. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5007. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5008. script_pushint(st,0);
  5009. return 0;
  5010. }
  5011. data=script_getdata(st,3);
  5012. get_val(st,data);
  5013. if( data_isstring(data) ) //Char Name
  5014. tsd=map_nick2sd(conv_str(st,data));
  5015. else //Char Id was given
  5016. tsd=map_charid2sd(conv_num(st,data));
  5017. if( tsd == NULL )
  5018. { //Failed
  5019. script_pushint(st,0);
  5020. return 0;
  5021. }
  5022. memset(&item_tmp,0,sizeof(item_tmp));
  5023. item_tmp.nameid=nameid;
  5024. item_tmp.amount=1;
  5025. item_tmp.identify=1;
  5026. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5027. item_tmp.card[2]=tsd->status.char_id;
  5028. item_tmp.card[3]=tsd->status.char_id >> 16;
  5029. if(pc_additem(sd,&item_tmp,1)) {
  5030. script_pushint(st,0);
  5031. return 0; //Failed to add item, we will not drop if they don't fit
  5032. }
  5033. //Logs items, got from (N)PC scripts [Lupus]
  5034. if(log_config.enable_logs&0x40)
  5035. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5036. script_pushint(st,1);
  5037. return 0;
  5038. }
  5039. /*==========================================
  5040. * gets a random item ID from an item group [Skotlex]
  5041. * groupranditem group_num
  5042. *------------------------------------------*/
  5043. BUILDIN_FUNC(grouprandomitem)
  5044. {
  5045. int group;
  5046. group = script_getnum(st,2);
  5047. script_pushint(st,itemdb_searchrandomid(group));
  5048. return 0;
  5049. }
  5050. /*==========================================
  5051. *
  5052. *------------------------------------------*/
  5053. BUILDIN_FUNC(makeitem)
  5054. {
  5055. int nameid,amount,flag = 0;
  5056. int x,y,m;
  5057. const char *mapname;
  5058. struct item item_tmp;
  5059. struct script_data *data;
  5060. data=script_getdata(st,2);
  5061. get_val(st,data);
  5062. if( data_isstring(data) ){
  5063. const char *name=conv_str(st,data);
  5064. struct item_data *item_data = itemdb_searchname(name);
  5065. if( item_data )
  5066. nameid=item_data->nameid;
  5067. else
  5068. nameid=UNKNOWN_ITEM_ID;
  5069. }else
  5070. nameid=conv_num(st,data);
  5071. amount=script_getnum(st,3);
  5072. mapname =script_getstr(st,4);
  5073. x =script_getnum(st,5);
  5074. y =script_getnum(st,6);
  5075. if(strcmp(mapname,"this")==0)
  5076. {
  5077. TBL_PC *sd;
  5078. sd = script_rid2sd(st);
  5079. if (!sd) return 0; //Failed...
  5080. m=sd->bl.m;
  5081. } else
  5082. m=map_mapname2mapid(mapname);
  5083. if(nameid<0) { // ランダム
  5084. nameid=itemdb_searchrandomid(-nameid);
  5085. flag = 1;
  5086. }
  5087. if(nameid > 0) {
  5088. memset(&item_tmp,0,sizeof(item_tmp));
  5089. item_tmp.nameid=nameid;
  5090. if(!flag)
  5091. item_tmp.identify=1;
  5092. else
  5093. item_tmp.identify=itemdb_isidentified(nameid);
  5094. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5095. }
  5096. return 0;
  5097. }
  5098. /*==========================================
  5099. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5100. *------------------------------------------*/
  5101. BUILDIN_FUNC(delitem)
  5102. {
  5103. int nameid=0,amount,i,important_item=0;
  5104. TBL_PC *sd;
  5105. struct script_data *data;
  5106. sd = script_rid2sd(st);
  5107. data=script_getdata(st,2);
  5108. get_val(st,data);
  5109. if( data_isstring(data) ){
  5110. const char *name=conv_str(st,data);
  5111. struct item_data *item_data = itemdb_searchname(name);
  5112. //nameid=UNKNOWN_ITEM_ID;
  5113. if( item_data )
  5114. nameid=item_data->nameid;
  5115. }else
  5116. nameid=conv_num(st,data);
  5117. amount=script_getnum(st,3);
  5118. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5119. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5120. return 0;
  5121. }
  5122. //1st pass
  5123. //here we won't delete items with CARDS, named items but we count them
  5124. for(i=0;i<MAX_INVENTORY;i++){
  5125. //we don't delete wrong item or equipped item
  5126. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5127. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5128. continue;
  5129. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5130. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5131. sd->status.inventory[i].card[0] == CARD0_PET)
  5132. {
  5133. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5134. continue; //pet couldn't be sent for deletion.
  5135. } else
  5136. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5137. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5138. sd->status.inventory[i].card[0] ||
  5139. sd->status.inventory[i].refine) {
  5140. //this is important item, count it (except for pet eggs)
  5141. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5142. important_item++;
  5143. continue;
  5144. }
  5145. if(sd->status.inventory[i].amount>=amount){
  5146. //Logs items, got from (N)PC scripts [Lupus]
  5147. if(log_config.enable_logs&0x40)
  5148. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5149. pc_delitem(sd,i,amount,0);
  5150. return 0; //we deleted exact amount of items. now exit
  5151. } else {
  5152. amount-=sd->status.inventory[i].amount;
  5153. //Logs items, got from (N)PC scripts [Lupus]
  5154. if(log_config.enable_logs&0x40) {
  5155. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5156. }
  5157. //Logs
  5158. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5159. }
  5160. }
  5161. //2nd pass
  5162. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5163. if (important_item>0 && amount>0)
  5164. for(i=0;i<MAX_INVENTORY;i++){
  5165. //we don't delete wrong item
  5166. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5167. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5168. continue;
  5169. if(sd->status.inventory[i].amount>=amount){
  5170. //Logs items, got from (N)PC scripts [Lupus]
  5171. if(log_config.enable_logs&0x40)
  5172. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5173. pc_delitem(sd,i,amount,0);
  5174. return 0; //we deleted exact amount of items. now exit
  5175. } else {
  5176. amount-=sd->status.inventory[i].amount;
  5177. //Logs items, got from (N)PC scripts [Lupus]
  5178. if(log_config.enable_logs&0x40)
  5179. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5180. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5181. }
  5182. }
  5183. return 0;
  5184. }
  5185. /*==========================================
  5186. * advanced version of delitem [modified by Mihilion]
  5187. *------------------------------------------*/
  5188. BUILDIN_FUNC(delitem2)
  5189. {
  5190. int nameid=0,amount,i=0;
  5191. int iden,ref,attr,c1,c2,c3,c4;
  5192. TBL_PC *sd;
  5193. struct script_data *data;
  5194. sd = script_rid2sd(st);
  5195. data=script_getdata(st,2);
  5196. get_val(st,data);
  5197. if( data_isstring(data) ){
  5198. const char *name=conv_str(st,data);
  5199. struct item_data *item_data = itemdb_searchname(name);
  5200. //nameid=UNKNOWN_ITEM_ID;
  5201. if( item_data )
  5202. nameid=item_data->nameid;
  5203. }else
  5204. nameid=conv_num(st,data);
  5205. amount=script_getnum(st,3);
  5206. iden=script_getnum(st,4);
  5207. ref=script_getnum(st,5);
  5208. attr=script_getnum(st,6);
  5209. c1=script_getnum(st,7);
  5210. c2=script_getnum(st,8);
  5211. c3=script_getnum(st,9);
  5212. c4=script_getnum(st,10);
  5213. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5214. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5215. return 0;
  5216. }
  5217. for(i=0;i<MAX_INVENTORY;i++){
  5218. //we don't delete wrong item or equipped item
  5219. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5220. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5221. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5222. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5223. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5224. sd->status.inventory[i].card[3]!=c4)
  5225. continue;
  5226. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5227. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5228. {
  5229. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5230. continue; //Failed to send delete the pet.
  5231. }
  5232. if(sd->status.inventory[i].amount>=amount){
  5233. //Logs items, got from (N)PC scripts [Lupus]
  5234. if(log_config.enable_logs&0x40)
  5235. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5236. pc_delitem(sd,i,amount,0);
  5237. return 0; //we deleted exact amount of items. now exit
  5238. } else {
  5239. amount-=sd->status.inventory[i].amount;
  5240. //Logs items, got from (N)PC scripts [Lupus]
  5241. if(log_config.enable_logs&0x40)
  5242. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5243. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5244. }
  5245. }
  5246. return 0;
  5247. }
  5248. /*==========================================
  5249. * Enables/Disables use of items while in an NPC [Skotlex]
  5250. *------------------------------------------*/
  5251. BUILDIN_FUNC(enableitemuse)
  5252. {
  5253. TBL_PC *sd;
  5254. sd=script_rid2sd(st);
  5255. if (sd)
  5256. sd->npc_item_flag = st->oid;
  5257. return 0;
  5258. }
  5259. BUILDIN_FUNC(disableitemuse)
  5260. {
  5261. TBL_PC *sd;
  5262. sd=script_rid2sd(st);
  5263. if (sd)
  5264. sd->npc_item_flag = 0;
  5265. return 0;
  5266. }
  5267. /*==========================================
  5268. *キャラ関係のパラメータ取得
  5269. *------------------------------------------*/
  5270. BUILDIN_FUNC(readparam)
  5271. {
  5272. int type;
  5273. TBL_PC *sd;
  5274. type=script_getnum(st,2);
  5275. if( script_hasdata(st,3) )
  5276. sd=map_nick2sd(script_getstr(st,3));
  5277. else
  5278. sd=script_rid2sd(st);
  5279. if(sd==NULL){
  5280. script_pushint(st,-1);
  5281. return 0;
  5282. }
  5283. script_pushint(st,pc_readparam(sd,type));
  5284. return 0;
  5285. }
  5286. /*==========================================
  5287. *キャラ関係のID取得
  5288. *------------------------------------------*/
  5289. BUILDIN_FUNC(getcharid)
  5290. {
  5291. int num;
  5292. TBL_PC *sd;
  5293. num = script_getnum(st,2);
  5294. if( script_hasdata(st,3) )
  5295. sd=map_nick2sd(script_getstr(st,3));
  5296. else
  5297. sd=script_rid2sd(st);
  5298. if(sd==NULL){
  5299. script_pushint(st,0); //return 0, according docs
  5300. return 0;
  5301. }
  5302. switch( num ) {
  5303. case 0: script_pushint(st,sd->status.char_id); break;
  5304. case 1: script_pushint(st,sd->status.party_id); break;
  5305. case 2: script_pushint(st,sd->status.guild_id); break;
  5306. case 3: script_pushint(st,sd->status.account_id); break;
  5307. default:
  5308. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5309. script_pushint(st,0);
  5310. break;
  5311. }
  5312. return 0;
  5313. }
  5314. /*==========================================
  5315. *指定IDのPT名取得
  5316. *------------------------------------------*/
  5317. char *buildin_getpartyname_sub(int party_id)
  5318. {
  5319. struct party_data *p;
  5320. p=party_search(party_id);
  5321. if(p!=NULL){
  5322. char *buf;
  5323. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5324. memcpy(buf, p->party.name, NAME_LENGTH);
  5325. buf[NAME_LENGTH-1] = '\0';
  5326. return buf;
  5327. }
  5328. return 0;
  5329. }
  5330. BUILDIN_FUNC(getpartyname)
  5331. {
  5332. char *name;
  5333. int party_id;
  5334. party_id=script_getnum(st,2);
  5335. name=buildin_getpartyname_sub(party_id);
  5336. if(name != NULL)
  5337. script_pushstr(st,name);
  5338. else
  5339. script_pushconststr(st,"null");
  5340. return 0;
  5341. }
  5342. /*==========================================
  5343. *指定IDのPT人数とメンバーID取得
  5344. *------------------------------------------*/
  5345. BUILDIN_FUNC(getpartymember)
  5346. {
  5347. struct party_data *p;
  5348. int i,j=0,type=0;
  5349. p=party_search(script_getnum(st,2));
  5350. if( script_hasdata(st,3) )
  5351. type=script_getnum(st,3);
  5352. if(p!=NULL){
  5353. for(i=0;i<MAX_PARTY;i++){
  5354. if(p->party.member[i].account_id){
  5355. switch (type) {
  5356. case 2:
  5357. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5358. break;
  5359. case 1:
  5360. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5361. break;
  5362. default:
  5363. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5364. }
  5365. j++;
  5366. }
  5367. }
  5368. }
  5369. mapreg_setreg(add_str("$@partymembercount"),j);
  5370. return 0;
  5371. }
  5372. /*==========================================
  5373. * Retrieves party leader. if flag is specified,
  5374. * return some of the leader data. Otherwise, return name.
  5375. *------------------------------------------*/
  5376. BUILDIN_FUNC(getpartyleader)
  5377. {
  5378. int party_id, type = 0, i=0;
  5379. struct party_data *p;
  5380. party_id=script_getnum(st,2);
  5381. if( script_hasdata(st,3) )
  5382. type=script_getnum(st,3);
  5383. p=party_search(party_id);
  5384. if (p) //Search leader
  5385. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5386. if (!p || i == MAX_PARTY) { //leader not found
  5387. if (type)
  5388. script_pushint(st,-1);
  5389. else
  5390. script_pushconststr(st,"null");
  5391. return 0;
  5392. }
  5393. switch (type) {
  5394. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5395. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5396. case 3: script_pushint(st,p->party.member[i].class_); break;
  5397. case 4: script_pushstr(st,aStrdup(mapindex_id2name(p->party.member[i].map))); break;
  5398. case 5: script_pushint(st,p->party.member[i].lv); break;
  5399. default: script_pushstr(st,aStrdup(p->party.member[i].name)); break;
  5400. }
  5401. return 0;
  5402. }
  5403. /*==========================================
  5404. *指定IDのギルド名取得
  5405. *------------------------------------------*/
  5406. char *buildin_getguildname_sub(int guild_id)
  5407. {
  5408. struct guild *g=NULL;
  5409. g=guild_search(guild_id);
  5410. if(g!=NULL){
  5411. char *buf;
  5412. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5413. memcpy(buf, g->name, NAME_LENGTH);
  5414. buf[NAME_LENGTH-1] = '\0';
  5415. return buf;
  5416. }
  5417. return NULL;
  5418. }
  5419. BUILDIN_FUNC(getguildname)
  5420. {
  5421. char *name;
  5422. int guild_id=script_getnum(st,2);
  5423. name=buildin_getguildname_sub(guild_id);
  5424. if(name != NULL)
  5425. script_pushstr(st,name);
  5426. else
  5427. script_pushconststr(st,"null");
  5428. return 0;
  5429. }
  5430. /*==========================================
  5431. *指定IDのGuildMaster名取得
  5432. *------------------------------------------*/
  5433. char *buildin_getguildmaster_sub(int guild_id)
  5434. {
  5435. struct guild *g=NULL;
  5436. g=guild_search(guild_id);
  5437. if(g!=NULL){
  5438. char *buf;
  5439. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5440. memcpy(buf, g->master, NAME_LENGTH);
  5441. buf[NAME_LENGTH-1] = '\0';
  5442. return buf;
  5443. }
  5444. return 0;
  5445. }
  5446. BUILDIN_FUNC(getguildmaster)
  5447. {
  5448. char *master;
  5449. int guild_id=script_getnum(st,2);
  5450. master=buildin_getguildmaster_sub(guild_id);
  5451. if(master!=0)
  5452. script_pushstr(st,master);
  5453. else
  5454. script_pushconststr(st,"null");
  5455. return 0;
  5456. }
  5457. BUILDIN_FUNC(getguildmasterid)
  5458. {
  5459. char *master;
  5460. TBL_PC *sd=NULL;
  5461. int guild_id=script_getnum(st,2);
  5462. master=buildin_getguildmaster_sub(guild_id);
  5463. if(master!=0){
  5464. if((sd=map_nick2sd(master)) == NULL){
  5465. script_pushint(st,0);
  5466. return 0;
  5467. }
  5468. script_pushint(st,sd->status.char_id);
  5469. }else{
  5470. script_pushint(st,0);
  5471. }
  5472. return 0;
  5473. }
  5474. /*==========================================
  5475. * キャラクタの名前
  5476. *------------------------------------------*/
  5477. BUILDIN_FUNC(strcharinfo)
  5478. {
  5479. TBL_PC *sd;
  5480. int num;
  5481. char *buf;
  5482. sd=script_rid2sd(st);
  5483. if (!sd) { //Avoid crashing....
  5484. script_pushconststr(st,"");
  5485. return 0;
  5486. }
  5487. num=script_getnum(st,2);
  5488. switch(num){
  5489. case 0:
  5490. script_pushstr(st,aStrdup(sd->status.name));
  5491. break;
  5492. case 1:
  5493. buf=buildin_getpartyname_sub(sd->status.party_id);
  5494. if(buf!=0)
  5495. script_pushstr(st,buf);
  5496. else
  5497. script_pushconststr(st,"");
  5498. break;
  5499. case 2:
  5500. buf=buildin_getguildname_sub(sd->status.guild_id);
  5501. if(buf != NULL)
  5502. script_pushstr(st,buf);
  5503. else
  5504. script_pushconststr(st,"");
  5505. break;
  5506. default:
  5507. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5508. script_pushconststr(st,"");
  5509. break;
  5510. }
  5511. return 0;
  5512. }
  5513. /*==========================================
  5514. * 呼び出し元のNPC情報を取得する
  5515. *------------------------------------------*/
  5516. BUILDIN_FUNC(strnpcinfo)
  5517. {
  5518. TBL_NPC* nd;
  5519. int num;
  5520. char *buf,*name=NULL;
  5521. nd = map_id2nd(st->oid);
  5522. if (!nd) {
  5523. script_pushconststr(st, "");
  5524. return 0;
  5525. }
  5526. num = script_getnum(st,2);
  5527. switch(num){
  5528. case 0: // display name
  5529. name = aStrdup(nd->name);
  5530. break;
  5531. case 1: // visible part of display name name
  5532. if((buf = strchr(nd->name,'#')) != NULL)
  5533. {
  5534. name = aStrdup(nd->name);
  5535. name[buf - nd->name] = 0;
  5536. }
  5537. break;
  5538. case 2: // # fragment
  5539. if((buf = strchr(nd->name,'#')) != NULL)
  5540. name = aStrdup(buf+1);
  5541. break;
  5542. case 3: // unique name
  5543. name = aStrdup(nd->exname);
  5544. break;
  5545. }
  5546. if(name)
  5547. script_pushstr(st, name);
  5548. else
  5549. script_pushconststr(st, "");
  5550. return 0;
  5551. }
  5552. 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};
  5553. /*==========================================
  5554. * GetEquipID(Pos); Pos: 1-10
  5555. *------------------------------------------*/
  5556. BUILDIN_FUNC(getequipid)
  5557. {
  5558. int i=-1,num;
  5559. TBL_PC *sd;
  5560. struct item_data* item;
  5561. sd=script_rid2sd(st);
  5562. if(sd == NULL)
  5563. {
  5564. ShowError("getequipid: sd == NULL\n");
  5565. return 0;
  5566. }
  5567. num=script_getnum(st,2);
  5568. if (num > 0 && num <= ARRAYLENGTH(equip))
  5569. i=pc_checkequip(sd,equip[num-1]);
  5570. if(i >= 0){
  5571. item=sd->inventory_data[i];
  5572. if(item)
  5573. script_pushint(st,item->nameid);
  5574. else
  5575. script_pushint(st,0);
  5576. }else{
  5577. script_pushint(st,-1);
  5578. }
  5579. return 0;
  5580. }
  5581. /*==========================================
  5582. * 装備名文字列(精錬メニュー用)
  5583. *------------------------------------------*/
  5584. BUILDIN_FUNC(getequipname)
  5585. {
  5586. int i=-1,num;
  5587. TBL_PC *sd;
  5588. struct item_data* item;
  5589. char *buf;
  5590. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  5591. buf=(char *)aMallocA(64*sizeof(char));
  5592. sd=script_rid2sd(st);
  5593. num=script_getnum(st,2);
  5594. if (num > 0 && num <= ARRAYLENGTH(equip))
  5595. i=pc_checkequip(sd,equip[num-1]);
  5596. if(i >= 0){
  5597. item=sd->inventory_data[i];
  5598. if(item)
  5599. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5600. else
  5601. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5602. }else{
  5603. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5604. }
  5605. script_pushstr(st,buf);
  5606. return 0;
  5607. }
  5608. /*==========================================
  5609. * getbrokenid [Valaris]
  5610. *------------------------------------------*/
  5611. BUILDIN_FUNC(getbrokenid)
  5612. {
  5613. int i,num,id=0,brokencounter=0;
  5614. TBL_PC *sd;
  5615. sd=script_rid2sd(st);
  5616. num=script_getnum(st,2);
  5617. for(i=0; i<MAX_INVENTORY; i++) {
  5618. if(sd->status.inventory[i].attribute==1){
  5619. brokencounter++;
  5620. if(num==brokencounter){
  5621. id=sd->status.inventory[i].nameid;
  5622. break;
  5623. }
  5624. }
  5625. }
  5626. script_pushint(st,id);
  5627. return 0;
  5628. }
  5629. /*==========================================
  5630. * repair [Valaris]
  5631. *------------------------------------------*/
  5632. BUILDIN_FUNC(repair)
  5633. {
  5634. int i,num;
  5635. int repaircounter=0;
  5636. TBL_PC *sd;
  5637. sd=script_rid2sd(st);
  5638. num=script_getnum(st,2);
  5639. for(i=0; i<MAX_INVENTORY; i++) {
  5640. if(sd->status.inventory[i].attribute==1){
  5641. repaircounter++;
  5642. if(num==repaircounter){
  5643. sd->status.inventory[i].attribute=0;
  5644. clif_equiplist(sd);
  5645. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5646. clif_misceffect(&sd->bl, 3);
  5647. break;
  5648. }
  5649. }
  5650. }
  5651. return 0;
  5652. }
  5653. /*==========================================
  5654. * 装備チェック
  5655. *------------------------------------------*/
  5656. BUILDIN_FUNC(getequipisequiped)
  5657. {
  5658. int i=-1,num;
  5659. TBL_PC *sd;
  5660. num=script_getnum(st,2);
  5661. sd=script_rid2sd(st);
  5662. if (num > 0 && num <= ARRAYLENGTH(equip))
  5663. i=pc_checkequip(sd,equip[num-1]);
  5664. if(i >= 0)
  5665. script_pushint(st,1);
  5666. else
  5667. script_pushint(st,0);
  5668. return 0;
  5669. }
  5670. /*==========================================
  5671. * 装備品精錬可能チェック
  5672. *------------------------------------------*/
  5673. BUILDIN_FUNC(getequipisenableref)
  5674. {
  5675. int i=-1,num;
  5676. TBL_PC *sd;
  5677. num=script_getnum(st,2);
  5678. sd=script_rid2sd(st);
  5679. if (num > 0 && num <= ARRAYLENGTH(equip))
  5680. i=pc_checkequip(sd,equip[num-1]);
  5681. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5682. {
  5683. script_pushint(st,1);
  5684. } else {
  5685. script_pushint(st,0);
  5686. }
  5687. return 0;
  5688. }
  5689. /*==========================================
  5690. * 装備品鑑定チェック
  5691. *------------------------------------------*/
  5692. BUILDIN_FUNC(getequipisidentify)
  5693. {
  5694. int i=-1,num;
  5695. TBL_PC *sd;
  5696. num=script_getnum(st,2);
  5697. sd=script_rid2sd(st);
  5698. if (num > 0 && num <= ARRAYLENGTH(equip))
  5699. i=pc_checkequip(sd,equip[num-1]);
  5700. if(i >= 0)
  5701. script_pushint(st,sd->status.inventory[i].identify);
  5702. else
  5703. script_pushint(st,0);
  5704. return 0;
  5705. }
  5706. /*==========================================
  5707. * 装備品精錬度
  5708. *------------------------------------------*/
  5709. BUILDIN_FUNC(getequiprefinerycnt)
  5710. {
  5711. int i=-1,num;
  5712. TBL_PC *sd;
  5713. num=script_getnum(st,2);
  5714. sd=script_rid2sd(st);
  5715. if (num > 0 && num <= ARRAYLENGTH(equip))
  5716. i=pc_checkequip(sd,equip[num-1]);
  5717. if(i >= 0)
  5718. script_pushint(st,sd->status.inventory[i].refine);
  5719. else
  5720. script_pushint(st,0);
  5721. return 0;
  5722. }
  5723. /*==========================================
  5724. * 装備品武器LV
  5725. *------------------------------------------*/
  5726. BUILDIN_FUNC(getequipweaponlv)
  5727. {
  5728. int i=-1,num;
  5729. TBL_PC *sd;
  5730. num=script_getnum(st,2);
  5731. sd=script_rid2sd(st);
  5732. if (num > 0 && num <= ARRAYLENGTH(equip))
  5733. i=pc_checkequip(sd,equip[num-1]);
  5734. if(i >= 0 && sd->inventory_data[i])
  5735. script_pushint(st,sd->inventory_data[i]->wlv);
  5736. else
  5737. script_pushint(st,0);
  5738. return 0;
  5739. }
  5740. /*==========================================
  5741. * 装備品精錬成功率
  5742. *------------------------------------------*/
  5743. BUILDIN_FUNC(getequippercentrefinery)
  5744. {
  5745. int i=-1,num;
  5746. TBL_PC *sd;
  5747. num=script_getnum(st,2);
  5748. sd=script_rid2sd(st);
  5749. if (num > 0 && num <= ARRAYLENGTH(equip))
  5750. i=pc_checkequip(sd,equip[num-1]);
  5751. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5752. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5753. else
  5754. script_pushint(st,0);
  5755. return 0;
  5756. }
  5757. /*==========================================
  5758. * 精錬成功
  5759. *------------------------------------------*/
  5760. BUILDIN_FUNC(successrefitem)
  5761. {
  5762. int i=-1,num,ep;
  5763. TBL_PC *sd;
  5764. num=script_getnum(st,2);
  5765. sd=script_rid2sd(st);
  5766. if (num > 0 && num <= ARRAYLENGTH(equip))
  5767. i=pc_checkequip(sd,equip[num-1]);
  5768. if(i >= 0) {
  5769. ep=sd->status.inventory[i].equip;
  5770. //Logs items, got from (N)PC scripts [Lupus]
  5771. if(log_config.enable_logs&0x40)
  5772. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5773. sd->status.inventory[i].refine++;
  5774. pc_unequipitem(sd,i,2);
  5775. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5776. clif_delitem(sd,i,1);
  5777. //Logs items, got from (N)PC scripts [Lupus]
  5778. if(log_config.enable_logs&0x40)
  5779. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5780. clif_additem(sd,i,1,0);
  5781. pc_equipitem(sd,i,ep);
  5782. clif_misceffect(&sd->bl,3);
  5783. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5784. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5785. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5786. ){ // Fame point system [DracoRPG]
  5787. switch (sd->inventory_data[i]->wlv){
  5788. case 1:
  5789. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5790. break;
  5791. case 2:
  5792. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5793. break;
  5794. case 3:
  5795. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5796. break;
  5797. }
  5798. }
  5799. }
  5800. return 0;
  5801. }
  5802. /*==========================================
  5803. * 精錬失敗
  5804. *------------------------------------------*/
  5805. BUILDIN_FUNC(failedrefitem)
  5806. {
  5807. int i=-1,num;
  5808. TBL_PC *sd;
  5809. num=script_getnum(st,2);
  5810. sd=script_rid2sd(st);
  5811. if (num > 0 && num <= ARRAYLENGTH(equip))
  5812. i=pc_checkequip(sd,equip[num-1]);
  5813. if(i >= 0) {
  5814. //Logs items, got from (N)PC scripts [Lupus]
  5815. if(log_config.enable_logs&0x40)
  5816. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5817. sd->status.inventory[i].refine = 0;
  5818. pc_unequipitem(sd,i,3);
  5819. // 精錬失敗エフェクトのパケット
  5820. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5821. pc_delitem(sd,i,1,0);
  5822. // 他の人にも失敗を通知
  5823. clif_misceffect(&sd->bl,2);
  5824. }
  5825. return 0;
  5826. }
  5827. /*==========================================
  5828. *
  5829. *------------------------------------------*/
  5830. BUILDIN_FUNC(statusup)
  5831. {
  5832. int type;
  5833. TBL_PC *sd;
  5834. type=script_getnum(st,2);
  5835. sd=script_rid2sd(st);
  5836. pc_statusup(sd,type);
  5837. return 0;
  5838. }
  5839. /*==========================================
  5840. *
  5841. *------------------------------------------*/
  5842. BUILDIN_FUNC(statusup2)
  5843. {
  5844. int type,val;
  5845. TBL_PC *sd;
  5846. type=script_getnum(st,2);
  5847. val=script_getnum(st,3);
  5848. sd=script_rid2sd(st);
  5849. pc_statusup2(sd,type,val);
  5850. return 0;
  5851. }
  5852. /// See 'doc/item_bonus.txt'
  5853. ///
  5854. /// bonus <bonus type>,<val1>;
  5855. /// bonus2 <bonus type>,<val1>,<val2>;
  5856. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5857. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5858. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5859. BUILDIN_FUNC(bonus)
  5860. {
  5861. int type;
  5862. int type2;
  5863. int type3;
  5864. int type4;
  5865. int type5;
  5866. int val;
  5867. TBL_PC* sd;
  5868. sd = script_rid2sd(st);
  5869. if( sd == NULL )
  5870. return 1; // no player attached
  5871. type = script_getnum(st,2);
  5872. switch( script_lastdata(st) ){
  5873. case 3:
  5874. val = script_getnum(st,3);
  5875. pc_bonus(sd, type, val);
  5876. break;
  5877. case 4:
  5878. type2 = script_getnum(st,3);
  5879. val = script_getnum(st,4);
  5880. pc_bonus2(sd, type, type2, val);
  5881. break;
  5882. case 5:
  5883. type2 = script_getnum(st,3);
  5884. type3 = script_getnum(st,4);
  5885. val = script_getnum(st,5);
  5886. pc_bonus3(sd, type, type2, type3, val);
  5887. break;
  5888. case 6:
  5889. type2 = script_getnum(st,3);
  5890. type3 = script_getnum(st,4);
  5891. type4 = script_getnum(st,5);
  5892. val = script_getnum(st,6);
  5893. pc_bonus4(sd, type, type2, type3, type4, val);
  5894. break;
  5895. case 7:
  5896. type2 = script_getnum(st,3);
  5897. type3 = script_getnum(st,4);
  5898. type4 = script_getnum(st,5);
  5899. type5 = script_getnum(st,6);
  5900. val = script_getnum(st,7);
  5901. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  5902. break;
  5903. default:
  5904. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 2));
  5905. }
  5906. return 0;
  5907. }
  5908. /// Bonus script that has a chance of being executed on attack.
  5909. BUILDIN_FUNC(bonusautoscript)
  5910. {
  5911. int rate, flag = 0;
  5912. const char *str;
  5913. struct script_code *script;
  5914. TBL_PC* sd;
  5915. sd = script_rid2sd(st);
  5916. if( sd == NULL )
  5917. return 1;// no player attached, report source
  5918. str = script_getstr(st,2);
  5919. rate = script_getnum(st,3);
  5920. if( script_hasdata(st,4) )
  5921. flag = script_getnum(st,4);
  5922. script = parse_script(str, "autoscript bonus", 0, 0);
  5923. if (!script)
  5924. return 1;
  5925. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  5926. {
  5927. script_free_code(script);
  5928. return 1;
  5929. }
  5930. return 0;
  5931. }
  5932. /// Bonus script that has a chance of being executed when attacked.
  5933. BUILDIN_FUNC(bonusautoscript2)
  5934. {
  5935. int rate, flag = 0;
  5936. const char *str;
  5937. struct script_code *script;
  5938. TBL_PC* sd;
  5939. sd = script_rid2sd(st);
  5940. if( sd == NULL )
  5941. return 1;// no player attached, report source
  5942. str = script_getstr(st,2);
  5943. rate = script_getnum(st,3);
  5944. if( script_hasdata(st,4) )
  5945. flag = script_getnum(st,4);
  5946. script = parse_script(str, "autoscript2 bonus", 0, 0);
  5947. if (!script)
  5948. return 1;
  5949. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  5950. {
  5951. script_free_code(script);
  5952. return 1;
  5953. }
  5954. return 0;
  5955. }
  5956. /// Changes the level of a player skill.
  5957. /// <flag> defaults to 1
  5958. /// <flag>=0 : set the level of the skill
  5959. /// <flag>=1 : set the temporary level of the skill
  5960. /// <flag>=2 : add to the level of the skill
  5961. ///
  5962. /// skill <skill id>,<level>,<flag>
  5963. /// skill <skill id>,<level>
  5964. BUILDIN_FUNC(skill)
  5965. {
  5966. int id;
  5967. int level;
  5968. int flag = 1;
  5969. TBL_PC* sd;
  5970. sd = script_rid2sd(st);
  5971. if( sd == NULL )
  5972. return 1;// no player attached, report source
  5973. id = script_getnum(st,2);
  5974. level = script_getnum(st,3);
  5975. if( script_hasdata(st,4) )
  5976. flag = script_getnum(st,4);
  5977. pc_skill(sd, id, level, flag);
  5978. return 0;
  5979. }
  5980. /// Changes the level of a player skill.
  5981. /// like skill, but <flag> defaults to 2
  5982. ///
  5983. /// addtoskill <skill id>,<amount>,<flag>
  5984. /// addtoskill <skill id>,<amount>
  5985. ///
  5986. /// @see skill
  5987. BUILDIN_FUNC(addtoskill)
  5988. {
  5989. int id;
  5990. int level;
  5991. int flag = 2;
  5992. TBL_PC* sd;
  5993. sd = script_rid2sd(st);
  5994. if( sd == NULL )
  5995. return 1;// no player attached, report source
  5996. id = script_getnum(st,2);
  5997. level = script_getnum(st,3);
  5998. if( script_hasdata(st,4) )
  5999. flag = script_getnum(st,4);
  6000. pc_skill(sd, id, level, flag);
  6001. return 0;
  6002. }
  6003. /// Increases the level of a guild skill.
  6004. ///
  6005. /// guildskill <skill id>,<amount>;
  6006. BUILDIN_FUNC(guildskill)
  6007. {
  6008. int id;
  6009. int level;
  6010. TBL_PC* sd;
  6011. int i;
  6012. sd = script_rid2sd(st);
  6013. if( sd == NULL )
  6014. return 1;// no player attached, report source
  6015. id = script_getnum(st,2);
  6016. level = script_getnum(st,3);
  6017. for( i=0; i < level; i++ )
  6018. guild_skillup(sd, id);
  6019. return 0;
  6020. }
  6021. /// Returns the level of the player skill.
  6022. ///
  6023. /// getskilllv(<skill id>) -> <level>
  6024. BUILDIN_FUNC(getskilllv)
  6025. {
  6026. int id;
  6027. TBL_PC* sd;
  6028. sd = script_rid2sd(st);
  6029. if( sd == NULL )
  6030. return 1;// no player attached, report source
  6031. id = script_getnum(st,2);
  6032. script_pushint(st, pc_checkskill(sd,id));
  6033. return 0;
  6034. }
  6035. /// Returns the level of the guild skill.
  6036. ///
  6037. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6038. BUILDIN_FUNC(getgdskilllv)
  6039. {
  6040. int guild_id;
  6041. int skill_id;
  6042. struct guild* g;
  6043. guild_id = script_getnum(st,2);
  6044. skill_id = script_getnum(st,3);
  6045. g = guild_search(guild_id);
  6046. if( g == NULL )
  6047. script_pushint(st, -1);
  6048. else
  6049. script_pushint(st, guild_checkskill(g,skill_id));
  6050. return 0;
  6051. }
  6052. /// Returns the 'basic_skill_check' setting.
  6053. /// This config determines if the server checks the skill level of NV_BASIC
  6054. /// before allowing the basic actions.
  6055. ///
  6056. /// basicskillcheck() -> <bool>
  6057. BUILDIN_FUNC(basicskillcheck)
  6058. {
  6059. script_pushint(st, battle_config.basic_skill_check);
  6060. return 0;
  6061. }
  6062. /// Returns the GM level of the player.
  6063. ///
  6064. /// getgmlevel() -> <level>
  6065. BUILDIN_FUNC(getgmlevel)
  6066. {
  6067. TBL_PC* sd;
  6068. sd = script_rid2sd(st);
  6069. if( sd == NULL )
  6070. return 1;// no player attached, report source
  6071. script_pushint(st, pc_isGM(sd));
  6072. return 0;
  6073. }
  6074. /// Terminates the execution of this script instance.
  6075. ///
  6076. /// end
  6077. BUILDIN_FUNC(end)
  6078. {
  6079. st->state = END;
  6080. return 0;
  6081. }
  6082. /// Checks if the player has that effect state (option).
  6083. ///
  6084. /// checkoption(<option>) -> <bool>
  6085. BUILDIN_FUNC(checkoption)
  6086. {
  6087. int option;
  6088. TBL_PC* sd;
  6089. sd = script_rid2sd(st);
  6090. if( sd == NULL )
  6091. return 1;// no player attached, report source
  6092. option = script_getnum(st,2);
  6093. if( sd->sc.option&option )
  6094. script_pushint(st, 1);
  6095. else
  6096. script_pushint(st, 0);
  6097. return 0;
  6098. }
  6099. /// Checks if the player is in that body state (opt1).
  6100. ///
  6101. /// checkoption1(<opt1>) -> <bool>
  6102. BUILDIN_FUNC(checkoption1)
  6103. {
  6104. int opt1;
  6105. TBL_PC* sd;
  6106. sd = script_rid2sd(st);
  6107. if( sd == NULL )
  6108. return 1;// no player attached, report source
  6109. opt1 = script_getnum(st,2);
  6110. if( sd->sc.opt1 == opt1 )
  6111. script_pushint(st, 1);
  6112. else
  6113. script_pushint(st, 0);
  6114. return 0;
  6115. }
  6116. /// Checks if the player has that health state (opt2).
  6117. ///
  6118. /// checkoption2(<opt2>) -> <bool>
  6119. BUILDIN_FUNC(checkoption2)
  6120. {
  6121. int opt2;
  6122. TBL_PC* sd;
  6123. sd = script_rid2sd(st);
  6124. if( sd == NULL )
  6125. return 1;// no player attached, report source
  6126. opt2 = script_getnum(st,2);
  6127. if( sd->sc.opt2&opt2 )
  6128. script_pushint(st, 1);
  6129. else
  6130. script_pushint(st, 0);
  6131. return 0;
  6132. }
  6133. /// Changes the effect state (option) of the player.
  6134. /// <flag> defaults to 1
  6135. /// <flag>=0 : removes the option
  6136. /// <flag>=other : adds the option
  6137. ///
  6138. /// setoption <option>,<flag>;
  6139. /// setoption <option>;
  6140. BUILDIN_FUNC(setoption)
  6141. {
  6142. int option;
  6143. int flag = 1;
  6144. TBL_PC* sd;
  6145. sd = script_rid2sd(st);
  6146. if( sd == NULL )
  6147. return 1;// no player attached, report source
  6148. option = script_getnum(st,2);
  6149. if( script_hasdata(st,3) )
  6150. flag = script_getnum(st,3);
  6151. else if( !option ){// Request to remove everything.
  6152. flag = 0;
  6153. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6154. }
  6155. if( flag ){// Add option
  6156. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6157. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6158. pc_setoption(sd, sd->sc.option|option);
  6159. } else// Remove option
  6160. pc_setoption(sd, sd->sc.option&~option);
  6161. return 0;
  6162. }
  6163. /// Returns if the player has a cart.
  6164. ///
  6165. /// checkcart() -> <bool>
  6166. ///
  6167. /// @author Valaris
  6168. BUILDIN_FUNC(checkcart)
  6169. {
  6170. TBL_PC* sd;
  6171. sd = script_rid2sd(st);
  6172. if( sd == NULL )
  6173. return 1;// no player attached, report source
  6174. if( pc_iscarton(sd) )
  6175. script_pushint(st, 1);
  6176. else
  6177. script_pushint(st, 0);
  6178. return 0;
  6179. }
  6180. /// Sets the cart of the player.
  6181. /// <type> defaults to 1
  6182. /// <type>=0 : removes the cart
  6183. /// <type>=1 : Normal cart
  6184. /// <type>=2 : Wooden cart
  6185. /// <type>=3 : Covered cart with flowers and ferns
  6186. /// <type>=4 : Wooden cart with a Panda doll on the back
  6187. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6188. ///
  6189. /// setcart <type>;
  6190. /// setcart;
  6191. BUILDIN_FUNC(setcart)
  6192. {
  6193. int type = 1;
  6194. TBL_PC* sd;
  6195. sd = script_rid2sd(st);
  6196. if( sd == NULL )
  6197. return 1;// no player attached, report source
  6198. if( script_hasdata(st,2) )
  6199. type = script_getnum(st,2);
  6200. pc_setcart(sd, type);
  6201. return 0;
  6202. }
  6203. /// Returns if the player has a falcon.
  6204. ///
  6205. /// checkfalcon() -> <bool>
  6206. ///
  6207. /// @author Valaris
  6208. BUILDIN_FUNC(checkfalcon)
  6209. {
  6210. TBL_PC* sd;
  6211. sd = script_rid2sd(st);
  6212. if( sd == NULL )
  6213. return 1;// no player attached, report source
  6214. if( pc_isfalcon(sd) )
  6215. script_pushint(st, 1);
  6216. else
  6217. script_pushint(st, 0);
  6218. return 0;
  6219. }
  6220. /// Sets if the player has a falcon or not.
  6221. /// <flag> defaults to 1
  6222. ///
  6223. /// setfalcon <flag>;
  6224. /// setfalcon;
  6225. BUILDIN_FUNC(setfalcon)
  6226. {
  6227. int flag = 1;
  6228. TBL_PC* sd;
  6229. sd = script_rid2sd(st);
  6230. if( sd == NULL )
  6231. return 1;// no player attached, report source
  6232. if( script_hasdata(st,2) )
  6233. flag = script_getnum(st,2);
  6234. pc_setfalcon(sd, flag);
  6235. return 0;
  6236. }
  6237. /// Returns if the player is riding.
  6238. ///
  6239. /// checkriding() -> <bool>
  6240. ///
  6241. /// @author Valaris
  6242. BUILDIN_FUNC(checkriding)
  6243. {
  6244. TBL_PC* sd;
  6245. sd = script_rid2sd(st);
  6246. if( sd == NULL )
  6247. return 1;// no player attached, report source
  6248. if( pc_isriding(sd) )
  6249. script_pushint(st, 1);
  6250. else
  6251. script_pushint(st, 0);
  6252. return 0;
  6253. }
  6254. /// Sets if the player is riding.
  6255. /// <flag> defaults to 1
  6256. ///
  6257. /// setriding <flag>;
  6258. /// setriding;
  6259. BUILDIN_FUNC(setriding)
  6260. {
  6261. int flag = 1;
  6262. TBL_PC* sd;
  6263. sd = script_rid2sd(st);
  6264. if( sd == NULL )
  6265. return 1;// no player attached, report source
  6266. if( script_hasdata(st,2) )
  6267. flag = script_getnum(st,2);
  6268. pc_setriding(sd, flag);
  6269. return 0;
  6270. }
  6271. /// Sets the save point of the player.
  6272. ///
  6273. /// save "<map name>",<x>,<y>
  6274. /// savepoint "<map name>",<x>,<y>
  6275. BUILDIN_FUNC(savepoint)
  6276. {
  6277. int x;
  6278. int y;
  6279. short map;
  6280. const char* str;
  6281. TBL_PC* sd;
  6282. sd = script_rid2sd(st);
  6283. if( sd == NULL )
  6284. return 1;// no player attached, report source
  6285. str = script_getstr(st, 2);
  6286. x = script_getnum(st,3);
  6287. y = script_getnum(st,4);
  6288. map = mapindex_name2id(str);
  6289. if( map )
  6290. pc_setsavepoint(sd, map, x, y);
  6291. return 0;
  6292. }
  6293. /*==========================================
  6294. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6295. *------------------------------------------*/
  6296. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6297. {
  6298. int type;
  6299. time_t timer;
  6300. struct tm *t;
  6301. type=script_getnum(st,2);
  6302. switch(type){
  6303. case 2:
  6304. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6305. // from the system clock.)
  6306. script_pushint(st,(int)time(NULL));
  6307. break;
  6308. case 1:
  6309. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6310. time(&timer);
  6311. t=localtime(&timer);
  6312. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6313. break;
  6314. case 0:
  6315. default:
  6316. //type 0:(System Ticks)
  6317. script_pushint(st,gettick());
  6318. break;
  6319. }
  6320. return 0;
  6321. }
  6322. /*==========================================
  6323. * GetTime(Type);
  6324. * 1: Sec 2: Min 3: Hour
  6325. * 4: WeekDay 5: MonthDay 6: Month
  6326. * 7: Year
  6327. *------------------------------------------*/
  6328. BUILDIN_FUNC(gettime) /* Asgard Version */
  6329. {
  6330. int type;
  6331. time_t timer;
  6332. struct tm *t;
  6333. type=script_getnum(st,2);
  6334. time(&timer);
  6335. t=localtime(&timer);
  6336. switch(type){
  6337. case 1://Sec(0~59)
  6338. script_pushint(st,t->tm_sec);
  6339. break;
  6340. case 2://Min(0~59)
  6341. script_pushint(st,t->tm_min);
  6342. break;
  6343. case 3://Hour(0~23)
  6344. script_pushint(st,t->tm_hour);
  6345. break;
  6346. case 4://WeekDay(0~6)
  6347. script_pushint(st,t->tm_wday);
  6348. break;
  6349. case 5://MonthDay(01~31)
  6350. script_pushint(st,t->tm_mday);
  6351. break;
  6352. case 6://Month(01~12)
  6353. script_pushint(st,t->tm_mon+1);
  6354. break;
  6355. case 7://Year(20xx)
  6356. script_pushint(st,t->tm_year+1900);
  6357. break;
  6358. case 8://Year Day(01~366)
  6359. script_pushint(st,t->tm_yday+1);
  6360. break;
  6361. default://(format error)
  6362. script_pushint(st,-1);
  6363. break;
  6364. }
  6365. return 0;
  6366. }
  6367. /*==========================================
  6368. * GetTimeStr("TimeFMT", Length);
  6369. *------------------------------------------*/
  6370. BUILDIN_FUNC(gettimestr)
  6371. {
  6372. char *tmpstr;
  6373. const char *fmtstr;
  6374. int maxlen;
  6375. time_t now = time(NULL);
  6376. fmtstr=script_getstr(st,2);
  6377. maxlen=script_getnum(st,3);
  6378. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6379. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6380. tmpstr[maxlen]='\0';
  6381. script_pushstr(st,tmpstr);
  6382. return 0;
  6383. }
  6384. /*==========================================
  6385. * カプラ倉庫を開く
  6386. *------------------------------------------*/
  6387. BUILDIN_FUNC(openstorage)
  6388. {
  6389. storage_storageopen(script_rid2sd(st));
  6390. return 0;
  6391. }
  6392. BUILDIN_FUNC(guildopenstorage)
  6393. {
  6394. TBL_PC *sd=script_rid2sd(st);
  6395. int ret;
  6396. ret = storage_guild_storageopen(sd);
  6397. script_pushint(st,ret);
  6398. return 0;
  6399. }
  6400. /*==========================================
  6401. * アイテムによるスキル発動
  6402. *------------------------------------------*/
  6403. BUILDIN_FUNC(itemskill)
  6404. {
  6405. int id,lv;
  6406. TBL_PC *sd=script_rid2sd(st);
  6407. id=script_getnum(st,2);
  6408. lv=script_getnum(st,3);
  6409. if(!sd || sd->ud.skilltimer != -1)
  6410. return 0;
  6411. sd->skillitem=id;
  6412. sd->skillitemlv=lv;
  6413. clif_item_skill(sd,id,lv);
  6414. return 0;
  6415. }
  6416. /*==========================================
  6417. * アイテム作成
  6418. *------------------------------------------*/
  6419. BUILDIN_FUNC(produce)
  6420. {
  6421. int trigger;
  6422. TBL_PC *sd=script_rid2sd(st);
  6423. trigger=script_getnum(st,2);
  6424. clif_skill_produce_mix_list(sd, trigger);
  6425. return 0;
  6426. }
  6427. /*==========================================
  6428. * NPCでペット作る
  6429. *------------------------------------------*/
  6430. BUILDIN_FUNC(makepet)
  6431. {
  6432. TBL_PC *sd = script_rid2sd(st);
  6433. int id,pet_id;
  6434. id=script_getnum(st,2);
  6435. pet_id = search_petDB_index(id, PET_CLASS);
  6436. if (pet_id < 0)
  6437. pet_id = search_petDB_index(id, PET_EGG);
  6438. if (pet_id >= 0 && sd) {
  6439. sd->catch_target_class = pet_db[pet_id].class_;
  6440. intif_create_pet(
  6441. sd->status.account_id, sd->status.char_id,
  6442. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6443. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6444. 100, 0, 1, pet_db[pet_id].jname);
  6445. }
  6446. return 0;
  6447. }
  6448. /*==========================================
  6449. * NPCで経験値上げる
  6450. *------------------------------------------*/
  6451. BUILDIN_FUNC(getexp)
  6452. {
  6453. TBL_PC *sd = script_rid2sd(st);
  6454. int base=0,job=0;
  6455. double bonus;
  6456. nullpo_retr(0, sd);
  6457. base=script_getnum(st,2);
  6458. job =script_getnum(st,3);
  6459. if(base<0 || job<0)
  6460. return 0;
  6461. // bonus for npc-given exp
  6462. bonus = battle_config.quest_exp_rate / 100.;
  6463. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6464. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6465. pc_gainexp(sd, NULL, base, job);
  6466. return 0;
  6467. }
  6468. /*==========================================
  6469. * Gain guild exp [Celest]
  6470. *------------------------------------------*/
  6471. BUILDIN_FUNC(guildgetexp)
  6472. {
  6473. TBL_PC *sd = script_rid2sd(st);
  6474. int exp;
  6475. exp = script_getnum(st,2);
  6476. if(exp < 0)
  6477. return 0;
  6478. if(sd && sd->status.guild_id > 0)
  6479. guild_getexp (sd, exp);
  6480. return 0;
  6481. }
  6482. /*==========================================
  6483. * Changes the guild master of a guild [Skotlex]
  6484. *------------------------------------------*/
  6485. BUILDIN_FUNC(guildchangegm)
  6486. {
  6487. TBL_PC *sd;
  6488. int guild_id;
  6489. const char *name;
  6490. guild_id = script_getnum(st,2);
  6491. name = script_getstr(st,3);
  6492. sd=map_nick2sd(name);
  6493. if (!sd)
  6494. script_pushint(st,0);
  6495. else
  6496. script_pushint(st,guild_gm_change(guild_id, sd));
  6497. return 0;
  6498. }
  6499. /*==========================================
  6500. * モンスター発生
  6501. *------------------------------------------*/
  6502. BUILDIN_FUNC(monster)
  6503. {
  6504. int class_,amount,x,y;
  6505. const char *str,*map,*event="";
  6506. map =script_getstr(st,2);
  6507. x =script_getnum(st,3);
  6508. y =script_getnum(st,4);
  6509. str =script_getstr(st,5);
  6510. class_=script_getnum(st,6);
  6511. amount=script_getnum(st,7);
  6512. if( script_hasdata(st,8) ){
  6513. event=script_getstr(st,8);
  6514. check_event(st, event);
  6515. }
  6516. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6517. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6518. return 1;
  6519. }
  6520. mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,amount,event);
  6521. return 0;
  6522. }
  6523. /*==========================================
  6524. * モンスター発生
  6525. *------------------------------------------*/
  6526. BUILDIN_FUNC(areamonster)
  6527. {
  6528. int class_,amount,x0,y0,x1,y1;
  6529. const char *str,*map,*event="";
  6530. map =script_getstr(st,2);
  6531. x0 =script_getnum(st,3);
  6532. y0 =script_getnum(st,4);
  6533. x1 =script_getnum(st,5);
  6534. y1 =script_getnum(st,6);
  6535. str =script_getstr(st,7);
  6536. class_=script_getnum(st,8);
  6537. amount=script_getnum(st,9);
  6538. if( script_hasdata(st,10) ){
  6539. event=script_getstr(st,10);
  6540. check_event(st, event);
  6541. }
  6542. mob_once_spawn_area(map_id2sd(st->rid),map,x0,y0,x1,y1,str,class_,amount,event);
  6543. return 0;
  6544. }
  6545. /*==========================================
  6546. * モンスター削除
  6547. *------------------------------------------*/
  6548. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6549. {
  6550. TBL_MOB* md = (TBL_MOB*)bl;
  6551. char *event=va_arg(ap,char *);
  6552. int allflag=va_arg(ap,int);
  6553. if(!allflag){
  6554. if(strcmp(event,md->npc_event)==0)
  6555. status_kill(bl);
  6556. }else{
  6557. if(!md->spawn)
  6558. status_kill(bl);
  6559. }
  6560. return 0;
  6561. }
  6562. BUILDIN_FUNC(killmonster)
  6563. {
  6564. const char *mapname,*event;
  6565. int m,allflag=0;
  6566. mapname=script_getstr(st,2);
  6567. event=script_getstr(st,3);
  6568. if(strcmp(event,"All")==0)
  6569. allflag = 1;
  6570. else
  6571. check_event(st, event);
  6572. if( (m=map_mapname2mapid(mapname))<0 )
  6573. return 0;
  6574. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6575. return 0;
  6576. }
  6577. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6578. {
  6579. status_kill(bl);
  6580. return 0;
  6581. }
  6582. BUILDIN_FUNC(killmonsterall)
  6583. {
  6584. const char *mapname;
  6585. int m;
  6586. mapname=script_getstr(st,2);
  6587. if( (m=map_mapname2mapid(mapname))<0 )
  6588. return 0;
  6589. map_foreachinmap(buildin_killmonsterall_sub,
  6590. m,BL_MOB);
  6591. return 0;
  6592. }
  6593. /*==========================================
  6594. * Creates a clone of a player.
  6595. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6596. *------------------------------------------*/
  6597. BUILDIN_FUNC(clone)
  6598. {
  6599. TBL_PC *sd, *msd=NULL;
  6600. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6601. unsigned int duration = 0;
  6602. const char *map,*event="";
  6603. map=script_getstr(st,2);
  6604. x=script_getnum(st,3);
  6605. y=script_getnum(st,4);
  6606. event=script_getstr(st,5);
  6607. char_id=script_getnum(st,6);
  6608. if( script_hasdata(st,7) )
  6609. master_id=script_getnum(st,7);
  6610. if( script_hasdata(st,8) )
  6611. mode=script_getnum(st,8);
  6612. if( script_hasdata(st,9) )
  6613. flag=script_getnum(st,9);
  6614. if( script_hasdata(st,10) )
  6615. duration=script_getnum(st,10);
  6616. check_event(st, event);
  6617. m = map_mapname2mapid(map);
  6618. if (m < 0) return 0;
  6619. sd = map_charid2sd(char_id);
  6620. if (master_id) {
  6621. msd = map_charid2sd(master_id);
  6622. if (msd)
  6623. master_id = msd->bl.id;
  6624. else
  6625. master_id = 0;
  6626. }
  6627. if (sd) //Return ID of newly crafted clone.
  6628. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6629. else //Failed to create clone.
  6630. script_pushint(st,0);
  6631. return 0;
  6632. }
  6633. /*==========================================
  6634. * イベント実行
  6635. *------------------------------------------*/
  6636. BUILDIN_FUNC(doevent)
  6637. {
  6638. const char* event = script_getstr(st,2);
  6639. check_event(st, event);
  6640. npc_event(map_id2sd(st->rid),event,0);
  6641. return 0;
  6642. }
  6643. /*==========================================
  6644. * NPC主体イベント実行
  6645. *------------------------------------------*/
  6646. BUILDIN_FUNC(donpcevent)
  6647. {
  6648. const char* event = script_getstr(st,2);
  6649. check_event(st, event);
  6650. npc_event_do(event);
  6651. return 0;
  6652. }
  6653. /// for Aegis compatibility
  6654. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6655. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6656. {
  6657. const char* npc = script_getstr(st,2);
  6658. const char* command = script_getstr(st,3);
  6659. char event[51];
  6660. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6661. check_event(st, event);
  6662. npc_event_do(event);
  6663. return 0;
  6664. }
  6665. /*==========================================
  6666. * イベントタイマー追加
  6667. *------------------------------------------*/
  6668. BUILDIN_FUNC(addtimer)
  6669. {
  6670. int tick = script_getnum(st,2);
  6671. const char* event = script_getstr(st, 3);
  6672. check_event(st, event);
  6673. pc_addeventtimer(script_rid2sd(st),tick,event);
  6674. return 0;
  6675. }
  6676. /*==========================================
  6677. * イベントタイマー削除
  6678. *------------------------------------------*/
  6679. BUILDIN_FUNC(deltimer)
  6680. {
  6681. const char *event;
  6682. event=script_getstr(st, 2);
  6683. check_event(st, event);
  6684. pc_deleventtimer(script_rid2sd(st),event);
  6685. return 0;
  6686. }
  6687. /*==========================================
  6688. * イベントタイマーのカウント値追加
  6689. *------------------------------------------*/
  6690. BUILDIN_FUNC(addtimercount)
  6691. {
  6692. const char *event;
  6693. int tick;
  6694. event=script_getstr(st, 2);
  6695. tick=script_getnum(st,3);
  6696. check_event(st, event);
  6697. pc_addeventtimercount(script_rid2sd(st),event,tick);
  6698. return 0;
  6699. }
  6700. /*==========================================
  6701. * NPCタイマー初期化
  6702. *------------------------------------------*/
  6703. BUILDIN_FUNC(initnpctimer)
  6704. {
  6705. struct npc_data *nd;
  6706. int flag = 0;
  6707. if( script_hasdata(st,3) )
  6708. { //Two arguments: NPC name and attach flag.
  6709. nd = npc_name2id(script_getstr(st, 2));
  6710. flag = script_getnum(st,3);
  6711. } else
  6712. if( script_hasdata(st,2) )
  6713. { //Check if argument is numeric (flag) or string (npc name)
  6714. struct script_data *data;
  6715. data = script_getdata(st,2);
  6716. get_val(st,data);
  6717. if( data_isstring(data) ) //NPC name
  6718. nd = npc_name2id(script_getstr(st, 2));
  6719. else if( data_isint(data) ) { //Flag
  6720. nd = (struct npc_data *)map_id2bl(st->oid);
  6721. flag = script_getnum(st,3);
  6722. } else {
  6723. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6724. return 1;
  6725. }
  6726. } else
  6727. nd=(struct npc_data *)map_id2bl(st->oid);
  6728. if (!nd) return 0;
  6729. if (flag) { //Attach
  6730. TBL_PC* sd = script_rid2sd(st);
  6731. if (sd) nd->u.scr.rid = sd->bl.id;
  6732. }
  6733. npc_settimerevent_tick(nd,0);
  6734. npc_timerevent_start(nd, st->rid);
  6735. return 0;
  6736. }
  6737. /*==========================================
  6738. * NPCタイマー開始
  6739. *------------------------------------------*/
  6740. BUILDIN_FUNC(startnpctimer)
  6741. {
  6742. struct npc_data *nd;
  6743. int flag = 0;
  6744. if( script_hasdata(st,3) )
  6745. { //Two arguments: NPC name and attach flag.
  6746. nd = npc_name2id(script_getstr(st, 2));
  6747. flag = script_getnum(st,3);
  6748. } else
  6749. if( script_hasdata(st,2) )
  6750. { //Check if argument is numeric (flag) or string (npc name)
  6751. struct script_data *data;
  6752. data = script_getdata(st,2);
  6753. get_val(st,data);
  6754. if( data_isstring(data) ) //NPC name
  6755. nd = npc_name2id(script_getstr(st, 2));
  6756. else if( data_isint(data) ) { //Flag
  6757. nd = (struct npc_data *)map_id2bl(st->oid);
  6758. flag = script_getnum(st,3);
  6759. } else {
  6760. ShowError("startnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6761. return 1;
  6762. }
  6763. } else
  6764. nd=(struct npc_data *)map_id2bl(st->oid);
  6765. if (!nd) return 0;
  6766. if (flag) { //Attach
  6767. TBL_PC* sd = script_rid2sd(st);
  6768. if (sd) nd->u.scr.rid = sd->bl.id;
  6769. }
  6770. npc_timerevent_start(nd, st->rid);
  6771. return 0;
  6772. }
  6773. /*==========================================
  6774. * NPCタイマー停止
  6775. *------------------------------------------*/
  6776. BUILDIN_FUNC(stopnpctimer)
  6777. {
  6778. struct npc_data *nd;
  6779. int flag = 0;
  6780. if( script_hasdata(st,3) )
  6781. { //Two arguments: NPC name and attach flag.
  6782. nd = npc_name2id(script_getstr(st, 2));
  6783. flag = script_getnum(st,3);
  6784. } else
  6785. if( script_hasdata(st,2) )
  6786. { //Check if argument is numeric (flag) or string (npc name)
  6787. struct script_data *data;
  6788. data = script_getdata(st,2);
  6789. get_val(st,data);
  6790. if( data_isstring(data) ) //NPC name
  6791. nd = npc_name2id(script_getstr(st, 2));
  6792. else if( data_isint(data) ) { //Flag
  6793. nd = (struct npc_data *)map_id2bl(st->oid);
  6794. flag = script_getnum(st,3);
  6795. } else {
  6796. ShowError("stopnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6797. return 1;
  6798. }
  6799. } else
  6800. nd=(struct npc_data *)map_id2bl(st->oid);
  6801. if (!nd) return 0;
  6802. if (flag) //Detach
  6803. nd->u.scr.rid = 0;
  6804. npc_timerevent_stop(nd);
  6805. return 0;
  6806. }
  6807. /*==========================================
  6808. * NPCタイマー情報所得
  6809. *------------------------------------------*/
  6810. BUILDIN_FUNC(getnpctimer)
  6811. {
  6812. struct npc_data *nd;
  6813. TBL_PC *sd;
  6814. int type=script_getnum(st,2);
  6815. int val=0;
  6816. if( script_hasdata(st,3) )
  6817. nd = npc_name2id(script_getstr(st,3));
  6818. else
  6819. nd = (struct npc_data *)map_id2bl(st->oid);
  6820. if (!nd || nd->bl.type != BL_NPC)
  6821. {
  6822. script_pushint(st,0);
  6823. ShowError("getnpctimer: Invalid NPC\n");
  6824. return 1;
  6825. }
  6826. switch(type){
  6827. case 0: val=npc_gettimerevent_tick(nd); break;
  6828. case 1:
  6829. if (nd->u.scr.rid) {
  6830. sd = map_id2sd(nd->u.scr.rid);
  6831. if (!sd) {
  6832. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6833. break;
  6834. }
  6835. val = (sd->npc_timer_id != -1);
  6836. } else
  6837. val= (nd->u.scr.timerid !=-1);
  6838. break;
  6839. case 2: val= nd->u.scr.timeramount; break;
  6840. }
  6841. script_pushint(st,val);
  6842. return 0;
  6843. }
  6844. /*==========================================
  6845. * NPCタイマー値設定
  6846. *------------------------------------------*/
  6847. BUILDIN_FUNC(setnpctimer)
  6848. {
  6849. int tick;
  6850. struct npc_data *nd;
  6851. tick=script_getnum(st,2);
  6852. if( script_hasdata(st,3) )
  6853. nd=npc_name2id(script_getstr(st,3));
  6854. else
  6855. nd=(struct npc_data *)map_id2bl(st->oid);
  6856. npc_settimerevent_tick(nd,tick);
  6857. return 0;
  6858. }
  6859. /*==========================================
  6860. * attaches the player rid to the timer [Celest]
  6861. *------------------------------------------*/
  6862. BUILDIN_FUNC(attachnpctimer)
  6863. {
  6864. TBL_PC *sd;
  6865. struct npc_data *nd;
  6866. nd=(struct npc_data *)map_id2bl(st->oid);
  6867. if( script_hasdata(st,2) )
  6868. sd=map_nick2sd(script_getstr(st,2));
  6869. else
  6870. sd = script_rid2sd(st);
  6871. if (sd==NULL)
  6872. return 0;
  6873. nd->u.scr.rid = sd->bl.id;
  6874. return 0;
  6875. }
  6876. /*==========================================
  6877. * detaches a player rid from the timer [Celest]
  6878. *------------------------------------------*/
  6879. BUILDIN_FUNC(detachnpctimer)
  6880. {
  6881. struct npc_data *nd;
  6882. if( script_hasdata(st,2) )
  6883. nd=npc_name2id(script_getstr(st,2));
  6884. else
  6885. nd=(struct npc_data *)map_id2bl(st->oid);
  6886. nd->u.scr.rid = 0;
  6887. return 0;
  6888. }
  6889. /*==========================================
  6890. * To avoid "player not attached" script errors, this function is provided,
  6891. * it checks if there is a player attached to the current script. [Skotlex]
  6892. * If no, returns 0, if yes, returns the account_id of the attached player.
  6893. *------------------------------------------*/
  6894. BUILDIN_FUNC(playerattached)
  6895. {
  6896. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  6897. script_pushint(st,0);
  6898. else
  6899. script_pushint(st,st->rid);
  6900. return 0;
  6901. }
  6902. /*==========================================
  6903. * 天の声アナウンス
  6904. *------------------------------------------*/
  6905. BUILDIN_FUNC(announce)
  6906. {
  6907. const char *str, *color=NULL;
  6908. int flag;
  6909. str=script_getstr(st,2);
  6910. flag=script_getnum(st,3);
  6911. if (script_hasdata(st,4))
  6912. color=script_getstr(st,4);
  6913. if(flag&0x0f){
  6914. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  6915. (struct block_list *)script_rid2sd(st);
  6916. if (color)
  6917. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  6918. else
  6919. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  6920. }else {
  6921. if (color)
  6922. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  6923. else
  6924. intif_GMmessage(str,(int)strlen(str)+1,flag);
  6925. }
  6926. return 0;
  6927. }
  6928. /*==========================================
  6929. * 天の声アナウンス(特定マップ)
  6930. *------------------------------------------*/
  6931. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  6932. {
  6933. char *str, *color;
  6934. int len,flag;
  6935. str=va_arg(ap,char *);
  6936. len=va_arg(ap,int);
  6937. flag=va_arg(ap,int);
  6938. color=va_arg(ap,char *);
  6939. if (color)
  6940. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  6941. else
  6942. clif_GMmessage(bl,str,len,flag|3);
  6943. return 0;
  6944. }
  6945. BUILDIN_FUNC(mapannounce)
  6946. {
  6947. const char *mapname,*str, *color=NULL;
  6948. int flag,m;
  6949. mapname=script_getstr(st,2);
  6950. str=script_getstr(st,3);
  6951. flag=script_getnum(st,4);
  6952. if (script_hasdata(st,5))
  6953. color=script_getstr(st,5);
  6954. if( (m=map_mapname2mapid(mapname))<0 )
  6955. return 0;
  6956. map_foreachinmap(buildin_mapannounce_sub,
  6957. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  6958. return 0;
  6959. }
  6960. /*==========================================
  6961. * 天の声アナウンス(特定エリア)
  6962. *------------------------------------------*/
  6963. BUILDIN_FUNC(areaannounce)
  6964. {
  6965. const char *map,*str,*color=NULL;
  6966. int flag,m;
  6967. int x0,y0,x1,y1;
  6968. map=script_getstr(st,2);
  6969. x0=script_getnum(st,3);
  6970. y0=script_getnum(st,4);
  6971. x1=script_getnum(st,5);
  6972. y1=script_getnum(st,6);
  6973. str=script_getstr(st,7);
  6974. flag=script_getnum(st,8);
  6975. if (script_hasdata(st,9))
  6976. color=script_getstr(st,9);
  6977. if( (m=map_mapname2mapid(map))<0 )
  6978. return 0;
  6979. map_foreachinarea(buildin_mapannounce_sub,
  6980. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  6981. return 0;
  6982. }
  6983. /*==========================================
  6984. * ユーザー数所得
  6985. *------------------------------------------*/
  6986. BUILDIN_FUNC(getusers)
  6987. {
  6988. int flag=script_getnum(st,2);
  6989. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  6990. int val=0;
  6991. switch(flag&0x07){
  6992. case 0: val=map[bl->m].users; break;
  6993. case 1: val=map_getusers(); break;
  6994. }
  6995. script_pushint(st,val);
  6996. return 0;
  6997. }
  6998. /*==========================================
  6999. * Works like @WHO - displays all online users names in window
  7000. *------------------------------------------*/
  7001. BUILDIN_FUNC(getusersname)
  7002. {
  7003. TBL_PC *sd, *pl_sd = NULL, **pl_allsd;
  7004. int i=0,disp_num=1, users;
  7005. sd = script_rid2sd(st);
  7006. if (!sd) return 0;
  7007. pl_allsd = map_getallusers(&users);
  7008. for (i=0;i<users;i++)
  7009. {
  7010. pl_sd = pl_allsd[i];
  7011. if( !(battle_config.hide_GM_session && pc_isGM(pl_sd)) )
  7012. {
  7013. if((disp_num++)%10==0)
  7014. clif_scriptnext(sd,st->oid);
  7015. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7016. }
  7017. }
  7018. return 0;
  7019. }
  7020. /*==========================================
  7021. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7022. *------------------------------------------*/
  7023. BUILDIN_FUNC(getmapguildusers)
  7024. {
  7025. const char *str;
  7026. int m, gid;
  7027. int i=0,c=0;
  7028. struct guild *g = NULL;
  7029. str=script_getstr(st,2);
  7030. gid=script_getnum(st,3);
  7031. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7032. script_pushint(st,-1);
  7033. return 0;
  7034. }
  7035. g = guild_search(gid);
  7036. if (g){
  7037. for(i = 0; i < g->max_member; i++)
  7038. {
  7039. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7040. c++;
  7041. }
  7042. }
  7043. script_pushint(st,c);
  7044. return 0;
  7045. }
  7046. /*==========================================
  7047. * マップ指定ユーザー数所得
  7048. *------------------------------------------*/
  7049. BUILDIN_FUNC(getmapusers)
  7050. {
  7051. const char *str;
  7052. int m;
  7053. str=script_getstr(st,2);
  7054. if( (m=map_mapname2mapid(str))< 0){
  7055. script_pushint(st,-1);
  7056. return 0;
  7057. }
  7058. script_pushint(st,map[m].users);
  7059. return 0;
  7060. }
  7061. /*==========================================
  7062. * エリア指定ユーザー数所得
  7063. *------------------------------------------*/
  7064. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7065. {
  7066. int *users=va_arg(ap,int *);
  7067. (*users)++;
  7068. return 0;
  7069. }
  7070. BUILDIN_FUNC(getareausers)
  7071. {
  7072. const char *str;
  7073. int m,x0,y0,x1,y1,users=0;
  7074. str=script_getstr(st,2);
  7075. x0=script_getnum(st,3);
  7076. y0=script_getnum(st,4);
  7077. x1=script_getnum(st,5);
  7078. y1=script_getnum(st,6);
  7079. if( (m=map_mapname2mapid(str))< 0){
  7080. script_pushint(st,-1);
  7081. return 0;
  7082. }
  7083. map_foreachinarea(buildin_getareausers_sub,
  7084. m,x0,y0,x1,y1,BL_PC,&users);
  7085. script_pushint(st,users);
  7086. return 0;
  7087. }
  7088. /*==========================================
  7089. * エリア指定ドロップアイテム数所得
  7090. *------------------------------------------*/
  7091. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7092. {
  7093. int item=va_arg(ap,int);
  7094. int *amount=va_arg(ap,int *);
  7095. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7096. if(drop->item_data.nameid==item)
  7097. (*amount)+=drop->item_data.amount;
  7098. return 0;
  7099. }
  7100. BUILDIN_FUNC(getareadropitem)
  7101. {
  7102. const char *str;
  7103. int m,x0,y0,x1,y1,item,amount=0;
  7104. struct script_data *data;
  7105. str=script_getstr(st,2);
  7106. x0=script_getnum(st,3);
  7107. y0=script_getnum(st,4);
  7108. x1=script_getnum(st,5);
  7109. y1=script_getnum(st,6);
  7110. data=script_getdata(st,7);
  7111. get_val(st,data);
  7112. if( data_isstring(data) ){
  7113. const char *name=conv_str(st,data);
  7114. struct item_data *item_data = itemdb_searchname(name);
  7115. item=UNKNOWN_ITEM_ID;
  7116. if( item_data )
  7117. item=item_data->nameid;
  7118. }else
  7119. item=conv_num(st,data);
  7120. if( (m=map_mapname2mapid(str))< 0){
  7121. script_pushint(st,-1);
  7122. return 0;
  7123. }
  7124. map_foreachinarea(buildin_getareadropitem_sub,
  7125. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7126. script_pushint(st,amount);
  7127. return 0;
  7128. }
  7129. /*==========================================
  7130. * NPCの有効化
  7131. *------------------------------------------*/
  7132. BUILDIN_FUNC(enablenpc)
  7133. {
  7134. const char *str;
  7135. str=script_getstr(st,2);
  7136. npc_enable(str,1);
  7137. return 0;
  7138. }
  7139. /*==========================================
  7140. * NPCの無効化
  7141. *------------------------------------------*/
  7142. BUILDIN_FUNC(disablenpc)
  7143. {
  7144. const char *str;
  7145. str=script_getstr(st,2);
  7146. npc_enable(str,0);
  7147. return 0;
  7148. }
  7149. /*==========================================
  7150. * 隠れているNPCの表示
  7151. *------------------------------------------*/
  7152. BUILDIN_FUNC(hideoffnpc)
  7153. {
  7154. const char *str;
  7155. str=script_getstr(st,2);
  7156. npc_enable(str,2);
  7157. return 0;
  7158. }
  7159. /*==========================================
  7160. * NPCをハイディング
  7161. *------------------------------------------*/
  7162. BUILDIN_FUNC(hideonnpc)
  7163. {
  7164. const char *str;
  7165. str=script_getstr(st,2);
  7166. npc_enable(str,4);
  7167. return 0;
  7168. }
  7169. /// Starts a status effect on the target unit or on the attached player.
  7170. ///
  7171. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7172. BUILDIN_FUNC(sc_start)
  7173. {
  7174. struct block_list* bl;
  7175. int type;
  7176. int tick;
  7177. int val1;
  7178. int val4 = 0;
  7179. type = script_getnum(st,2);
  7180. tick = script_getnum(st,3);
  7181. val1 = script_getnum(st,4);
  7182. if( script_hasdata(st,5) )
  7183. bl = map_id2bl(script_getnum(st,5));
  7184. else
  7185. bl = map_id2bl(st->rid);
  7186. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7187. {// When there isn't a duration specified, try to get it from the skill_db
  7188. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7189. }
  7190. if( potion_flag == 1 && potion_target )
  7191. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7192. bl = map_id2bl(potion_target);
  7193. tick /= 2;// Thrown potions only last half.
  7194. val4 = 1;// Mark that this was a thrown sc_effect
  7195. }
  7196. if( bl )
  7197. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 1|2|8);
  7198. return 0;
  7199. }
  7200. /// Starts a status effect on the target unit or on the attached player.
  7201. ///
  7202. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7203. BUILDIN_FUNC(sc_start2)
  7204. {
  7205. struct block_list* bl;
  7206. int type;
  7207. int tick;
  7208. int val1;
  7209. int val4 = 0;
  7210. int rate;
  7211. type = script_getnum(st,2);
  7212. tick = script_getnum(st,3);
  7213. val1 = script_getnum(st,4);
  7214. rate = script_getnum(st,5);
  7215. if( script_hasdata(st,6) )
  7216. bl = map_id2bl(script_getnum(st,6));
  7217. else
  7218. bl = map_id2bl(st->rid);
  7219. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7220. {// When there isn't a duration specified, try to get it from the skill_db
  7221. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7222. }
  7223. if( potion_flag == 1 && potion_target )
  7224. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7225. bl = map_id2bl(potion_target);
  7226. tick /= 2;// Thrown potions only last half.
  7227. val4 = 1;// Mark that this was a thrown sc_effect
  7228. }
  7229. if( bl )
  7230. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 1|2|8);
  7231. return 0;
  7232. }
  7233. /// Starts a status effect on the target unit or on the attached player.
  7234. ///
  7235. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7236. BUILDIN_FUNC(sc_start4)
  7237. {
  7238. struct block_list* bl;
  7239. int type;
  7240. int tick;
  7241. int val1;
  7242. int val2;
  7243. int val3;
  7244. int val4;
  7245. type = script_getnum(st,2);
  7246. tick = script_getnum(st,3);
  7247. val1 = script_getnum(st,4);
  7248. val2 = script_getnum(st,5);
  7249. val3 = script_getnum(st,6);
  7250. val4 = script_getnum(st,7);
  7251. if( script_hasdata(st,8) )
  7252. bl = map_id2bl(script_getnum(st,8));
  7253. else
  7254. bl = map_id2bl(st->rid);
  7255. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7256. {// When there isn't a duration specified, try to get it from the skill_db
  7257. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7258. }
  7259. if( potion_flag == 1 && potion_target )
  7260. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7261. bl = map_id2bl(potion_target);
  7262. tick /= 2;// Thrown potions only last half.
  7263. }
  7264. if( bl )
  7265. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 1|2|8);
  7266. return 0;
  7267. }
  7268. /// Ends one or all status effects on the target unit or on the attached player.
  7269. ///
  7270. /// sc_end <effect_id>{,<unit_id>};
  7271. BUILDIN_FUNC(sc_end)
  7272. {
  7273. struct block_list* bl;
  7274. int type;
  7275. type = script_getnum(st,2);
  7276. if( script_hasdata(st,3) )
  7277. bl = map_id2bl(script_getnum(st,3));
  7278. else
  7279. bl = map_id2bl(st->rid);
  7280. if( potion_flag==1 && potion_target )
  7281. {//##TODO how does this work [FlavioJS]
  7282. bl = map_id2bl(potion_target);
  7283. }
  7284. if( bl )
  7285. {
  7286. if( type >= 0 )
  7287. status_change_end(bl, type, INVALID_TIMER);
  7288. else
  7289. status_change_clear(bl, 2);// remove all effects
  7290. }
  7291. return 0;
  7292. }
  7293. /*==========================================
  7294. * 状態異常耐性を計算した確率を返す
  7295. *------------------------------------------*/
  7296. BUILDIN_FUNC(getscrate)
  7297. {
  7298. struct block_list *bl;
  7299. int type,rate;
  7300. type=script_getnum(st,2);
  7301. rate=script_getnum(st,3);
  7302. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7303. bl = map_id2bl(script_getnum(st,4));
  7304. else
  7305. bl = map_id2bl(st->rid);
  7306. if (bl)
  7307. rate = status_get_sc_def(bl,type, 10000, 10000, 0);
  7308. script_pushint(st,rate);
  7309. return 0;
  7310. }
  7311. /*==========================================
  7312. *
  7313. *------------------------------------------*/
  7314. BUILDIN_FUNC(debugmes)
  7315. {
  7316. const char *str;
  7317. str=script_getstr(st,2);
  7318. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7319. return 0;
  7320. }
  7321. /*==========================================
  7322. *捕獲アイテム使用
  7323. *------------------------------------------*/
  7324. BUILDIN_FUNC(catchpet)
  7325. {
  7326. int pet_id;
  7327. TBL_PC *sd;
  7328. pet_id= script_getnum(st,2);
  7329. sd=script_rid2sd(st);
  7330. pet_catch_process1(sd,pet_id);
  7331. return 0;
  7332. }
  7333. /*==========================================
  7334. * [orn]
  7335. *------------------------------------------*/
  7336. BUILDIN_FUNC(homunculus_evolution)
  7337. {
  7338. TBL_PC *sd;
  7339. sd=script_rid2sd(st);
  7340. if(merc_is_hom_active(sd->hd))
  7341. {
  7342. if (sd->hd->homunculus.intimacy > 91000)
  7343. merc_hom_evolution(sd->hd);
  7344. else
  7345. clif_emotion(&sd->hd->bl, 4) ; //swt
  7346. }
  7347. return 0;
  7348. }
  7349. // [Zephyrus]
  7350. BUILDIN_FUNC(homunculus_shuffle)
  7351. {
  7352. TBL_PC *sd;
  7353. sd=script_rid2sd(st);
  7354. if(merc_is_hom_active(sd->hd))
  7355. merc_hom_shuffle(sd->hd);
  7356. return 0;
  7357. }
  7358. //These two functions bring the eA MAPID_* class functionality to scripts.
  7359. BUILDIN_FUNC(eaclass)
  7360. {
  7361. int class_;
  7362. if( script_hasdata(st,2) )
  7363. class_ = script_getnum(st,2);
  7364. else {
  7365. TBL_PC *sd;
  7366. sd=script_rid2sd(st);
  7367. if (!sd) {
  7368. script_pushint(st,-1);
  7369. return 0;
  7370. }
  7371. class_ = sd->status.class_;
  7372. }
  7373. script_pushint(st,pc_jobid2mapid(class_));
  7374. return 0;
  7375. }
  7376. BUILDIN_FUNC(roclass)
  7377. {
  7378. int class_ =script_getnum(st,2);
  7379. int sex;
  7380. if( script_hasdata(st,3) )
  7381. sex = script_getnum(st,3);
  7382. else {
  7383. TBL_PC *sd;
  7384. if (st->rid && (sd=script_rid2sd(st)))
  7385. sex = sd->status.sex;
  7386. else
  7387. sex = 1; //Just use male when not found.
  7388. }
  7389. script_pushint(st,pc_mapid2jobid(class_, sex));
  7390. return 0;
  7391. }
  7392. /*==========================================
  7393. *携帯卵孵化機使用
  7394. *------------------------------------------*/
  7395. BUILDIN_FUNC(birthpet)
  7396. {
  7397. TBL_PC *sd;
  7398. sd=script_rid2sd(st);
  7399. clif_sendegg(sd);
  7400. return 0;
  7401. }
  7402. /*==========================================
  7403. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7404. *------------------------------------------*/
  7405. BUILDIN_FUNC(resetlvl)
  7406. {
  7407. TBL_PC *sd;
  7408. int type=script_getnum(st,2);
  7409. sd=script_rid2sd(st);
  7410. pc_resetlvl(sd,type);
  7411. return 0;
  7412. }
  7413. /*==========================================
  7414. * ステータスリセット
  7415. *------------------------------------------*/
  7416. BUILDIN_FUNC(resetstatus)
  7417. {
  7418. TBL_PC *sd;
  7419. sd=script_rid2sd(st);
  7420. pc_resetstate(sd);
  7421. return 0;
  7422. }
  7423. /*==========================================
  7424. * script command resetskill
  7425. *------------------------------------------*/
  7426. BUILDIN_FUNC(resetskill)
  7427. {
  7428. TBL_PC *sd;
  7429. sd=script_rid2sd(st);
  7430. pc_resetskill(sd,1);
  7431. return 0;
  7432. }
  7433. /*==========================================
  7434. * Counts total amount of skill points.
  7435. *------------------------------------------*/
  7436. BUILDIN_FUNC(skillpointcount)
  7437. {
  7438. TBL_PC *sd;
  7439. sd=script_rid2sd(st);
  7440. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7441. return 0;
  7442. }
  7443. /*==========================================
  7444. *
  7445. *------------------------------------------*/
  7446. BUILDIN_FUNC(changebase)
  7447. {
  7448. TBL_PC *sd=NULL;
  7449. int vclass;
  7450. if( script_hasdata(st,3) )
  7451. sd=map_id2sd(script_getnum(st,3));
  7452. else
  7453. sd=script_rid2sd(st);
  7454. if(sd == NULL)
  7455. return 0;
  7456. vclass = script_getnum(st,2);
  7457. if(vclass == JOB_WEDDING)
  7458. {
  7459. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7460. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7461. )
  7462. return 0;
  7463. }
  7464. if(!sd->disguise && vclass != sd->vd.class_) {
  7465. status_set_viewdata(&sd->bl, vclass);
  7466. //Updated client view. Base, Weapon and Cloth Colors.
  7467. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7468. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7469. if (sd->vd.cloth_color)
  7470. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7471. }
  7472. return 0;
  7473. }
  7474. /*==========================================
  7475. * 性別変換
  7476. *------------------------------------------*/
  7477. BUILDIN_FUNC(changesex)
  7478. {
  7479. TBL_PC *sd = NULL;
  7480. sd = script_rid2sd(st);
  7481. if (sd->status.sex == 0) {
  7482. sd->status.sex = 1;
  7483. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7484. sd->status.class_ -= 1;
  7485. } else if (sd->status.sex == 1) {
  7486. sd->status.sex = 0;
  7487. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7488. sd->status.class_ += 1;
  7489. }
  7490. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  7491. chrif_save(sd,0);
  7492. return 0;
  7493. }
  7494. /*==========================================
  7495. * Works like 'announce' but outputs in the common chat window
  7496. *------------------------------------------*/
  7497. BUILDIN_FUNC(globalmes)
  7498. {
  7499. struct block_list *bl = map_id2bl(st->oid);
  7500. struct npc_data *nd = (struct npc_data *)bl;
  7501. const char *name=NULL,*mes;
  7502. mes=script_getstr(st,2); // メッセージの取得
  7503. if(mes==NULL) return 0;
  7504. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7505. name=script_getstr(st,3);
  7506. } else {
  7507. name=nd->name;
  7508. }
  7509. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7510. return 0;
  7511. }
  7512. /////////////////////////////////////////////////////////////////////
  7513. // NPC waiting room (chat room)
  7514. //
  7515. /// Creates a waiting room (chat room) for this npc.
  7516. ///
  7517. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7518. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7519. /// waitingroom "<title>",<limit>,"<event>";
  7520. /// waitingroom "<title>",<limit>;
  7521. BUILDIN_FUNC(waitingroom)
  7522. {
  7523. struct npc_data* nd;
  7524. const char* title;
  7525. const char* ev = "";
  7526. int limit;
  7527. int trigger = 0;
  7528. int pub = 1;
  7529. title = script_getstr(st, 2);
  7530. limit = script_getnum(st, 3);
  7531. if( script_hasdata(st,5) )
  7532. {
  7533. struct script_data* last = script_getdata(st, 5);
  7534. get_val(st, last);
  7535. if( data_isstring(last) )
  7536. {// ,<trigger>,"<event>"
  7537. trigger = script_getnum(st, 4);
  7538. ev = script_getstr(st, 5);
  7539. }
  7540. else
  7541. {// ,"<event>",<trigger>
  7542. ev = script_getstr(st, 4);
  7543. trigger = script_getnum(st,5);
  7544. }
  7545. }
  7546. else if( script_hasdata(st,4) )
  7547. {// ,"<event>"
  7548. ev = script_getstr(st, 4);
  7549. trigger = limit;
  7550. }
  7551. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  7552. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7553. return 0;
  7554. }
  7555. /// Removes the waiting room of the current or target npc.
  7556. ///
  7557. /// delwaitingroom "<npc_name>";
  7558. /// delwaitingroom;
  7559. BUILDIN_FUNC(delwaitingroom)
  7560. {
  7561. struct npc_data* nd;
  7562. if( script_hasdata(st,2) )
  7563. nd = npc_name2id(script_getstr(st, 2));
  7564. else
  7565. nd = (struct npc_data *)map_id2bl(st->oid);
  7566. if( nd != NULL )
  7567. chat_deletenpcchat(nd);
  7568. return 0;
  7569. }
  7570. /// Kicks all the players from the waiting room of the current or target npc.
  7571. ///
  7572. /// kickwaitingroomall "<npc_name>";
  7573. /// kickwaitingroomall;
  7574. BUILDIN_FUNC(waitingroomkickall)
  7575. {
  7576. struct npc_data* nd;
  7577. struct chat_data* cd;
  7578. if( script_hasdata(st,2) )
  7579. nd = npc_name2id(script_getstr(st,2));
  7580. else
  7581. nd = (struct npc_data *)map_id2bl(st->oid);
  7582. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7583. chat_npckickall(cd);
  7584. return 0;
  7585. }
  7586. /// Enables the waiting room event of the current or target npc.
  7587. ///
  7588. /// enablewaitingroomevent "<npc_name>";
  7589. /// enablewaitingroomevent;
  7590. BUILDIN_FUNC(enablewaitingroomevent)
  7591. {
  7592. struct npc_data* nd;
  7593. struct chat_data* cd;
  7594. if( script_hasdata(st,2) )
  7595. nd = npc_name2id(script_getstr(st, 2));
  7596. else
  7597. nd = (struct npc_data *)map_id2bl(st->oid);
  7598. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7599. chat_enableevent(cd);
  7600. return 0;
  7601. }
  7602. /// Disables the waiting room event of the current or target npc.
  7603. ///
  7604. /// disablewaitingroomevent "<npc_name>";
  7605. /// disablewaitingroomevent;
  7606. BUILDIN_FUNC(disablewaitingroomevent)
  7607. {
  7608. struct npc_data *nd;
  7609. struct chat_data *cd;
  7610. if( script_hasdata(st,2) )
  7611. nd = npc_name2id(script_getstr(st, 2));
  7612. else
  7613. nd = (struct npc_data *)map_id2bl(st->oid);
  7614. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7615. chat_disableevent(cd);
  7616. return 0;
  7617. }
  7618. /// Returns info on the waiting room of the current or target npc.
  7619. /// Returns -1 if the type unknown
  7620. /// <type>=0 : current number of users
  7621. /// <type>=1 : maximum number of users allowed
  7622. /// <type>=2 : the number of users that trigger the event
  7623. /// <type>=3 : if the trigger is disabled
  7624. /// <type>=4 : the title of the waiting room
  7625. /// <type>=5 : the password of the waiting room
  7626. /// <type>=16 : the name of the waiting room event
  7627. /// <type>=32 : if the waiting room is full
  7628. /// <type>=33 : if there are enough users to trigger the event
  7629. ///
  7630. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7631. /// getwaitingroomstate(<type>) -> <info>
  7632. BUILDIN_FUNC(getwaitingroomstate)
  7633. {
  7634. struct npc_data *nd;
  7635. struct chat_data *cd;
  7636. int type;
  7637. type = script_getnum(st,2);
  7638. if( script_hasdata(st,3) )
  7639. nd = npc_name2id(script_getstr(st, 3));
  7640. else
  7641. nd = (struct npc_data *)map_id2bl(st->oid);
  7642. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7643. {
  7644. script_pushint(st, -1);
  7645. return 0;
  7646. }
  7647. switch(type)
  7648. {
  7649. case 0: script_pushint(st, cd->users); break;
  7650. case 1: script_pushint(st, cd->limit); break;
  7651. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7652. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7653. case 4: script_pushconststr(st, cd->title); break;
  7654. case 5: script_pushconststr(st, cd->pass); break;
  7655. case 16: script_pushconststr(st, cd->npc_event);break;
  7656. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7657. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7658. default: script_pushint(st, -1); break;
  7659. }
  7660. return 0;
  7661. }
  7662. /// Warps the trigger or target amount of players to the target map and position.
  7663. /// Players are automatically removed from the waiting room.
  7664. /// Those waiting the longest will get warped first.
  7665. /// The target map can be "Random" for a random position in the current map,
  7666. /// and "SavePoint" for the savepoint map+position.
  7667. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7668. ///
  7669. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7670. /// The total number of teleported players is put into $@warpwaitingpcnum
  7671. ///
  7672. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7673. /// warpwaitingpc "<map name>",<x>,<y>;
  7674. BUILDIN_FUNC(warpwaitingpc)
  7675. {
  7676. int x;
  7677. int y;
  7678. int i;
  7679. int n;
  7680. const char* map_name;
  7681. struct npc_data* nd;
  7682. struct chat_data* cd;
  7683. TBL_PC* sd;
  7684. nd = (struct npc_data *)map_id2bl(st->oid);
  7685. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7686. return 0;
  7687. map_name = script_getstr(st,2);
  7688. x = script_getnum(st,3);
  7689. y = script_getnum(st,4);
  7690. n = cd->trigger&0x7f;
  7691. if( script_hasdata(st,5) )
  7692. n = script_getnum(st,5);
  7693. for( i = 0; i < n && cd->users > 0; i++ )
  7694. {
  7695. sd = cd->usersd[0];
  7696. if( sd == NULL )
  7697. {
  7698. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7699. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7700. continue;// Broken npc chat room?
  7701. }
  7702. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7703. if( strcmp(map_name,"Random") == 0 )
  7704. pc_randomwarp(sd,3);
  7705. else if( strcmp(map_name,"SavePoint") == 0 )
  7706. {
  7707. if( map[sd->bl.m].flag.noteleport )
  7708. return 0;// can't teleport on this map
  7709. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  7710. }
  7711. else
  7712. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7713. }
  7714. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7715. return 0;
  7716. }
  7717. /////////////////////////////////////////////////////////////////////
  7718. // ...
  7719. //
  7720. /*==========================================
  7721. * RIDのアタッチ
  7722. *------------------------------------------*/
  7723. BUILDIN_FUNC(attachrid)
  7724. {
  7725. st->rid=script_getnum(st,2);
  7726. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7727. return 0;
  7728. }
  7729. /*==========================================
  7730. * RIDのデタッチ
  7731. *------------------------------------------*/
  7732. BUILDIN_FUNC(detachrid)
  7733. {
  7734. st->rid=0;
  7735. return 0;
  7736. }
  7737. /*==========================================
  7738. * 存在チェック
  7739. *------------------------------------------*/
  7740. BUILDIN_FUNC(isloggedin)
  7741. {
  7742. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  7743. if (script_hasdata(st,3) && sd &&
  7744. sd->status.char_id != script_getnum(st,3))
  7745. sd = NULL;
  7746. push_val(st->stack,C_INT,sd!=NULL);
  7747. return 0;
  7748. }
  7749. /*==========================================
  7750. *
  7751. *------------------------------------------*/
  7752. BUILDIN_FUNC(setmapflagnosave)
  7753. {
  7754. int m,x,y;
  7755. unsigned short mapindex;
  7756. const char *str,*str2;
  7757. str=script_getstr(st,2);
  7758. str2=script_getstr(st,3);
  7759. x=script_getnum(st,4);
  7760. y=script_getnum(st,5);
  7761. m = map_mapname2mapid(str);
  7762. mapindex = mapindex_name2id(str2);
  7763. if(m >= 0 && mapindex) {
  7764. map[m].flag.nosave=1;
  7765. map[m].save.map=mapindex;
  7766. map[m].save.x=x;
  7767. map[m].save.y=y;
  7768. }
  7769. return 0;
  7770. }
  7771. BUILDIN_FUNC(setmapflag)
  7772. {
  7773. int m,i;
  7774. const char *str;
  7775. const char *val=NULL;
  7776. str=script_getstr(st,2);
  7777. i=script_getnum(st,3);
  7778. if(script_hasdata(st,4)){
  7779. val=script_getstr(st,4);
  7780. }
  7781. m = map_mapname2mapid(str);
  7782. if(m >= 0) {
  7783. switch(i) {
  7784. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  7785. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  7786. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  7787. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  7788. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  7789. case MF_PVP: map[m].flag.pvp=1; break;
  7790. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  7791. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  7792. case MF_GVG: map[m].flag.gvg=1; break;
  7793. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  7794. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  7795. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  7796. case MF_NOTRADE: map[m].flag.notrade=1; break;
  7797. case MF_NODROP: map[m].flag.nodrop=1; break;
  7798. case MF_NOSKILL: map[m].flag.noskill=1; break;
  7799. case MF_NOWARP: map[m].flag.nowarp=1; break;
  7800. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  7801. case MF_SNOW: map[m].flag.snow=1; break;
  7802. case MF_CLOUDS: map[m].flag.clouds=1; break;
  7803. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  7804. case MF_FOG: map[m].flag.fog=1; break;
  7805. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  7806. case MF_SAKURA: map[m].flag.sakura=1; break;
  7807. case MF_LEAVES: map[m].flag.leaves=1; break;
  7808. case MF_RAIN: map[m].flag.rain=1; break;
  7809. case MF_INDOORS: map[m].flag.indoors=1; break;
  7810. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  7811. case MF_NOGO: map[m].flag.nogo=1; break;
  7812. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  7813. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  7814. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  7815. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  7816. case MF_NORETURN: map[m].flag.noreturn=1; break;
  7817. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  7818. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  7819. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  7820. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  7821. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7822. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7823. case MF_NOVENDING: map[m].flag.novending=1; break;
  7824. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  7825. case MF_NOCHAT: map[m].flag.nochat=1; break;
  7826. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  7827. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  7828. }
  7829. }
  7830. return 0;
  7831. }
  7832. BUILDIN_FUNC(removemapflag)
  7833. {
  7834. int m,i;
  7835. const char *str;
  7836. str=script_getstr(st,2);
  7837. i=script_getnum(st,3);
  7838. m = map_mapname2mapid(str);
  7839. if(m >= 0) {
  7840. switch(i) {
  7841. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  7842. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  7843. case MF_NOSAVE: map[m].flag.nosave=0; break;
  7844. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  7845. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  7846. case MF_PVP: map[m].flag.pvp=0; break;
  7847. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  7848. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  7849. case MF_GVG: map[m].flag.gvg=0; break;
  7850. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  7851. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  7852. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  7853. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  7854. case MF_NOTRADE: map[m].flag.notrade=0; break;
  7855. case MF_NODROP: map[m].flag.nodrop=0; break;
  7856. case MF_NOSKILL: map[m].flag.noskill=0; break;
  7857. case MF_NOWARP: map[m].flag.nowarp=0; break;
  7858. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  7859. case MF_SNOW: map[m].flag.snow=0; break;
  7860. case MF_CLOUDS: map[m].flag.clouds=0; break;
  7861. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  7862. case MF_FOG: map[m].flag.fog=0; break;
  7863. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  7864. case MF_SAKURA: map[m].flag.sakura=0; break;
  7865. case MF_LEAVES: map[m].flag.leaves=0; break;
  7866. case MF_RAIN: map[m].flag.rain=0; break;
  7867. case MF_INDOORS: map[m].flag.indoors=0; break;
  7868. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  7869. case MF_NOGO: map[m].flag.nogo=0; break;
  7870. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  7871. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  7872. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  7873. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  7874. case MF_NORETURN: map[m].flag.noreturn=0; break;
  7875. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  7876. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  7877. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  7878. case MF_NOCOMMAND: map[m].nocommand=0; break;
  7879. case MF_JEXP: map[m].jexp=100; break;
  7880. case MF_BEXP: map[m].bexp=100; break;
  7881. case MF_NOVENDING: map[m].flag.novending=0; break;
  7882. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  7883. case MF_NOCHAT: map[m].flag.nochat=0; break;
  7884. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  7885. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  7886. }
  7887. }
  7888. return 0;
  7889. }
  7890. BUILDIN_FUNC(pvpon)
  7891. {
  7892. int m,i,users;
  7893. const char *str;
  7894. TBL_PC *pl_sd=NULL, **pl_allsd;
  7895. str=script_getstr(st,2);
  7896. m = map_mapname2mapid(str);
  7897. if(m >= 0 && !map[m].flag.pvp) {
  7898. map[m].flag.pvp = 1;
  7899. clif_send0199(m,1);
  7900. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  7901. return 0;
  7902. pl_allsd = map_getallusers(&users);
  7903. for(i=0;i<users;i++)
  7904. {
  7905. if ((pl_sd = pl_allsd[i]) && m == pl_sd->bl.m && pl_sd->pvp_timer == -1)
  7906. {
  7907. pl_sd->pvp_timer=add_timer(gettick()+200,pc_calc_pvprank_timer,pl_sd->bl.id,0);
  7908. pl_sd->pvp_rank=0;
  7909. pl_sd->pvp_lastusers=0;
  7910. pl_sd->pvp_point=5;
  7911. pl_sd->pvp_won = 0;
  7912. pl_sd->pvp_lost = 0;
  7913. }
  7914. }
  7915. }
  7916. return 0;
  7917. }
  7918. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  7919. {
  7920. TBL_PC* sd = (TBL_PC*)bl;
  7921. clif_pvpset(sd, 0, 0, 2);
  7922. if (sd->pvp_timer != -1) {
  7923. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  7924. sd->pvp_timer = -1;
  7925. }
  7926. return 0;
  7927. }
  7928. BUILDIN_FUNC(pvpoff)
  7929. {
  7930. int m;
  7931. const char *str;
  7932. str=script_getstr(st,2);
  7933. m = map_mapname2mapid(str);
  7934. if(m < 0 || !map[m].flag.pvp)
  7935. return 0; //fixed Lupus
  7936. map[m].flag.pvp = 0;
  7937. clif_send0199(m,0);
  7938. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  7939. return 0;
  7940. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  7941. return 0;
  7942. }
  7943. BUILDIN_FUNC(gvgon)
  7944. {
  7945. int m;
  7946. const char *str;
  7947. str=script_getstr(st,2);
  7948. m = map_mapname2mapid(str);
  7949. if(m >= 0 && !map[m].flag.gvg) {
  7950. map[m].flag.gvg = 1;
  7951. clif_send0199(m,3);
  7952. }
  7953. return 0;
  7954. }
  7955. BUILDIN_FUNC(gvgoff)
  7956. {
  7957. int m;
  7958. const char *str;
  7959. str=script_getstr(st,2);
  7960. m = map_mapname2mapid(str);
  7961. if(m >= 0 && map[m].flag.gvg) {
  7962. map[m].flag.gvg = 0;
  7963. clif_send0199(m,0);
  7964. }
  7965. return 0;
  7966. }
  7967. /*==========================================
  7968. * Shows an emoticon on top of the player/npc
  7969. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  7970. *------------------------------------------*/
  7971. //Optional second parameter added by [Skotlex]
  7972. BUILDIN_FUNC(emotion)
  7973. {
  7974. int type;
  7975. int player=0;
  7976. type=script_getnum(st,2);
  7977. if(type < 0 || type > 100)
  7978. return 0;
  7979. if( script_hasdata(st,3) )
  7980. player=script_getnum(st,3);
  7981. if (player) {
  7982. TBL_PC *sd = script_rid2sd(st);
  7983. if (sd)
  7984. clif_emotion(&sd->bl,type);
  7985. } else
  7986. clif_emotion(map_id2bl(st->oid),type);
  7987. return 0;
  7988. }
  7989. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  7990. {
  7991. int m=va_arg(ap,int);
  7992. int g_id=va_arg(ap,int);
  7993. int flag=va_arg(ap,int);
  7994. TBL_PC *sd = (TBL_PC*)data;
  7995. if(!sd || sd->bl.m != m)
  7996. return 0;
  7997. if(
  7998. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  7999. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8000. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8001. )
  8002. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8003. return 1;
  8004. }
  8005. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8006. {
  8007. struct mob_data *md=(struct mob_data *)bl;
  8008. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8009. status_kill(bl);
  8010. return 0;
  8011. }
  8012. BUILDIN_FUNC(maprespawnguildid)
  8013. {
  8014. const char *mapname=script_getstr(st,2);
  8015. int g_id=script_getnum(st,3);
  8016. int flag=script_getnum(st,4);
  8017. int m=map_mapname2mapid(mapname);
  8018. if(m == -1)
  8019. return 0;
  8020. //Catch ALL players (in case some are 'between maps' on execution time)
  8021. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8022. if (flag&4) //Remove script mobs.
  8023. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8024. return 0;
  8025. }
  8026. BUILDIN_FUNC(agitstart)
  8027. {
  8028. if(agit_flag==1) return 0; // Agit already Start.
  8029. agit_flag=1;
  8030. guild_agit_start();
  8031. return 0;
  8032. }
  8033. BUILDIN_FUNC(agitend)
  8034. {
  8035. if(agit_flag==0) return 0; // Agit already End.
  8036. agit_flag=0;
  8037. guild_agit_end();
  8038. return 0;
  8039. }
  8040. /*==========================================
  8041. * Returns whether woe is on or off. // choice script
  8042. *------------------------------------------*/
  8043. BUILDIN_FUNC(agitcheck)
  8044. {
  8045. script_pushint(st,agit_flag);
  8046. return 0;
  8047. }
  8048. BUILDIN_FUNC(flagemblem)
  8049. {
  8050. int g_id=script_getnum(st,2);
  8051. if(g_id < 0) return 0;
  8052. // printf("Script.c: [FlagEmblem] GuildID=%d, Emblem=%d.\n", g->guild_id, g->emblem_id);
  8053. ((struct npc_data *)map_id2bl(st->oid))->u.scr.guild_id = g_id;
  8054. return 0;
  8055. }
  8056. BUILDIN_FUNC(getcastlename)
  8057. {
  8058. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8059. struct guild_castle* gc = guild_mapname2gc(mapname);
  8060. char* name = (gc) ? gc->castle_name : "";
  8061. script_pushconststr(st,name);
  8062. return 0;
  8063. }
  8064. BUILDIN_FUNC(getcastledata)
  8065. {
  8066. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8067. int index = script_getnum(st,3);
  8068. struct guild_castle* gc = guild_mapname2gc(mapname);
  8069. if(script_hasdata(st,4) && index==0 && gc) {
  8070. const char* event = script_getstr(st,4);
  8071. check_event(st, event);
  8072. guild_addcastleinfoevent(gc->castle_id,17,event);
  8073. }
  8074. if(gc){
  8075. switch(index){
  8076. case 0: {
  8077. int i;
  8078. for(i=1;i<26;i++) // Initialize[AgitInit]
  8079. guild_castledataload(gc->castle_id,i);
  8080. } break;
  8081. case 1:
  8082. script_pushint(st,gc->guild_id); break;
  8083. case 2:
  8084. script_pushint(st,gc->economy); break;
  8085. case 3:
  8086. script_pushint(st,gc->defense); break;
  8087. case 4:
  8088. script_pushint(st,gc->triggerE); break;
  8089. case 5:
  8090. script_pushint(st,gc->triggerD); break;
  8091. case 6:
  8092. script_pushint(st,gc->nextTime); break;
  8093. case 7:
  8094. script_pushint(st,gc->payTime); break;
  8095. case 8:
  8096. script_pushint(st,gc->createTime); break;
  8097. case 9:
  8098. script_pushint(st,gc->visibleC); break;
  8099. case 10:
  8100. case 11:
  8101. case 12:
  8102. case 13:
  8103. case 14:
  8104. case 15:
  8105. case 16:
  8106. case 17:
  8107. script_pushint(st,gc->guardian[index-10].visible); break;
  8108. case 18:
  8109. case 19:
  8110. case 20:
  8111. case 21:
  8112. case 22:
  8113. case 23:
  8114. case 24:
  8115. case 25:
  8116. script_pushint(st,gc->guardian[index-18].hp); break;
  8117. default:
  8118. script_pushint(st,0); break;
  8119. }
  8120. return 0;
  8121. }
  8122. script_pushint(st,0);
  8123. return 0;
  8124. }
  8125. BUILDIN_FUNC(setcastledata)
  8126. {
  8127. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8128. int index = script_getnum(st,3);
  8129. int value = script_getnum(st,4);
  8130. struct guild_castle* gc = guild_mapname2gc(mapname);
  8131. if(gc) {
  8132. // Save Data byself First
  8133. switch(index){
  8134. case 1:
  8135. gc->guild_id = value; break;
  8136. case 2:
  8137. gc->economy = value; break;
  8138. case 3:
  8139. gc->defense = value; break;
  8140. case 4:
  8141. gc->triggerE = value; break;
  8142. case 5:
  8143. gc->triggerD = value; break;
  8144. case 6:
  8145. gc->nextTime = value; break;
  8146. case 7:
  8147. gc->payTime = value; break;
  8148. case 8:
  8149. gc->createTime = value; break;
  8150. case 9:
  8151. gc->visibleC = value; break;
  8152. case 10:
  8153. case 11:
  8154. case 12:
  8155. case 13:
  8156. case 14:
  8157. case 15:
  8158. case 16:
  8159. case 17:
  8160. gc->guardian[index-10].visible = value; break;
  8161. case 18:
  8162. case 19:
  8163. case 20:
  8164. case 21:
  8165. case 22:
  8166. case 23:
  8167. case 24:
  8168. case 25:
  8169. gc->guardian[index-18].hp = value;
  8170. if (gc->guardian[index-18].id)
  8171. { //Update this mob's HP.
  8172. struct block_list *bl = map_id2bl(gc->guardian[index-18].id);
  8173. if (!bl)
  8174. { //Wrong target?
  8175. gc->guardian[index-18].id = 0;
  8176. break;
  8177. }
  8178. if (value < 1) {
  8179. status_kill(bl);
  8180. break;
  8181. }
  8182. status_set_hp(bl, value, 0);
  8183. }
  8184. break;
  8185. default:
  8186. return 0;
  8187. }
  8188. guild_castledatasave(gc->castle_id,index,value);
  8189. }
  8190. return 0;
  8191. }
  8192. /* =====================================================================
  8193. * ギルド情報を要求する
  8194. * ---------------------------------------------------------------------*/
  8195. BUILDIN_FUNC(requestguildinfo)
  8196. {
  8197. int guild_id=script_getnum(st,2);
  8198. const char *event=NULL;
  8199. if( script_hasdata(st,3) ){
  8200. event=script_getstr(st,3);
  8201. check_event(st, event);
  8202. }
  8203. if(guild_id>0)
  8204. guild_npc_request_info(guild_id,event);
  8205. return 0;
  8206. }
  8207. /* =====================================================================
  8208. * カードの数を得る
  8209. * ---------------------------------------------------------------------*/
  8210. BUILDIN_FUNC(getequipcardcnt)
  8211. {
  8212. int i=-1,num;
  8213. TBL_PC *sd;
  8214. int c=MAX_SLOTS;
  8215. num=script_getnum(st,2);
  8216. sd=script_rid2sd(st);
  8217. if (num > 0 && num <= ARRAYLENGTH(equip))
  8218. i=pc_checkequip(sd,equip[num-1]);
  8219. if (i < 0) {
  8220. script_pushint(st,0);
  8221. return 0;
  8222. }
  8223. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8224. {
  8225. script_pushint(st,0);
  8226. return 0;
  8227. }
  8228. do{
  8229. if(sd->status.inventory[i].card[c-1] &&
  8230. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8231. script_pushint(st,(c));
  8232. return 0;
  8233. }
  8234. }while(c--);
  8235. script_pushint(st,0);
  8236. return 0;
  8237. }
  8238. /* ================================================================
  8239. * カード取り外し成功
  8240. * ----------------------------------------------------------------*/
  8241. BUILDIN_FUNC(successremovecards)
  8242. {
  8243. int i=-1,j,num,cardflag=0,flag;
  8244. TBL_PC *sd;
  8245. struct item item_tmp;
  8246. int c=MAX_SLOTS;
  8247. num=script_getnum(st,2);
  8248. sd=script_rid2sd(st);
  8249. if (num > 0 && num <= ARRAYLENGTH(equip))
  8250. i=pc_checkequip(sd,equip[num-1]);
  8251. if (i < 0) {
  8252. script_pushint(st,0);
  8253. return 0;
  8254. }
  8255. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8256. return 0;
  8257. do{
  8258. if(sd->status.inventory[i].card[c-1] &&
  8259. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8260. cardflag = 1;
  8261. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8262. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8263. item_tmp.attribute=0;
  8264. for (j = 0; j < MAX_SLOTS; j++)
  8265. item_tmp.card[j]=0;
  8266. //Logs items, got from (N)PC scripts [Lupus]
  8267. if(log_config.enable_logs&0x40)
  8268. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8269. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8270. clif_additem(sd,0,0,flag);
  8271. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8272. }
  8273. }
  8274. }while(c--);
  8275. if(cardflag == 1){ // カードを取り除いたアイテム所得
  8276. flag=0;
  8277. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8278. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8279. item_tmp.attribute=sd->status.inventory[i].attribute;
  8280. //Logs items, got from (N)PC scripts [Lupus]
  8281. if(log_config.enable_logs&0x40)
  8282. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8283. for (j = 0; j < MAX_SLOTS; j++)
  8284. item_tmp.card[j]=0;
  8285. pc_delitem(sd,i,1,0);
  8286. //Logs items, got from (N)PC scripts [Lupus]
  8287. if(log_config.enable_logs&0x40)
  8288. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8289. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8290. clif_additem(sd,0,0,flag);
  8291. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8292. }
  8293. clif_misceffect(&sd->bl,3);
  8294. return 0;
  8295. }
  8296. return 0;
  8297. }
  8298. /* ================================================================
  8299. * カード取り外し失敗 slot,type
  8300. * type=0: 両方損失、1:カード損失、2:武具損失、3:損失無し
  8301. * ----------------------------------------------------------------*/
  8302. BUILDIN_FUNC(failedremovecards)
  8303. {
  8304. int i=-1,j,num,cardflag=0,flag,typefail;
  8305. TBL_PC *sd;
  8306. struct item item_tmp;
  8307. int c=MAX_SLOTS;
  8308. num=script_getnum(st,2);
  8309. typefail=script_getnum(st,3);
  8310. sd=script_rid2sd(st);
  8311. if (num > 0 && num <= ARRAYLENGTH(equip))
  8312. i=pc_checkequip(sd,equip[num-1]);
  8313. if (i < 0) {
  8314. script_pushint(st,0);
  8315. return 0;
  8316. }
  8317. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8318. return 0;
  8319. do{
  8320. if(sd->status.inventory[i].card[c-1] &&
  8321. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8322. cardflag = 1;
  8323. if(typefail == 2){ // 武具のみ損失なら、カードは受け取らせる
  8324. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8325. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8326. item_tmp.attribute=0;
  8327. for (j = 0; j < MAX_SLOTS; j++)
  8328. item_tmp.card[j]=0;
  8329. //Logs items, got from (N)PC scripts [Lupus]
  8330. if(log_config.enable_logs&0x40)
  8331. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8332. if((flag=pc_additem(sd,&item_tmp,1))){
  8333. clif_additem(sd,0,0,flag);
  8334. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8335. }
  8336. }
  8337. }
  8338. }while(c--);
  8339. if(cardflag == 1){
  8340. if(typefail == 0 || typefail == 2){ // 武具損失
  8341. //Logs items, got from (N)PC scripts [Lupus]
  8342. if(log_config.enable_logs&0x40)
  8343. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8344. pc_delitem(sd,i,1,0);
  8345. clif_misceffect(&sd->bl,2);
  8346. return 0;
  8347. }
  8348. if(typefail == 1){ // カードのみ損失(武具を返す)
  8349. flag=0;
  8350. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8351. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8352. item_tmp.attribute=sd->status.inventory[i].attribute;
  8353. //Logs items, got from (N)PC scripts [Lupus]
  8354. if(log_config.enable_logs&0x40)
  8355. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8356. for (j = 0; j < MAX_SLOTS; j++)
  8357. item_tmp.card[j]=0;
  8358. pc_delitem(sd,i,1,0);
  8359. //Logs items, got from (N)PC scripts [Lupus]
  8360. if(log_config.enable_logs&0x40)
  8361. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8362. if((flag=pc_additem(sd,&item_tmp,1))){
  8363. clif_additem(sd,0,0,flag);
  8364. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8365. }
  8366. }
  8367. clif_misceffect(&sd->bl,2);
  8368. return 0;
  8369. }
  8370. return 0;
  8371. }
  8372. /* ================================================================
  8373. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8374. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8375. * improved by [Lance]
  8376. * ================================================================*/
  8377. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8378. {
  8379. int x,y,m,check_val=0,check_ID=0,i=0;
  8380. struct guild *g = NULL;
  8381. struct party_data *p = NULL;
  8382. const char *str;
  8383. const char *mapname;
  8384. unsigned int index;
  8385. mapname=script_getstr(st,2);
  8386. str=script_getstr(st,3);
  8387. x=script_getnum(st,4);
  8388. y=script_getnum(st,5);
  8389. if(script_hasdata(st,7)){
  8390. check_val=script_getnum(st,6);
  8391. check_ID=script_getnum(st,7);
  8392. }
  8393. if((m=map_mapname2mapid(mapname))< 0)
  8394. return 0;
  8395. if(!(index=mapindex_name2id(str)))
  8396. return 0;
  8397. switch(check_val){
  8398. case 1:
  8399. g = guild_search(check_ID);
  8400. if (g){
  8401. for( i=0; i < g->max_member; i++)
  8402. {
  8403. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8404. pc_setpos(g->member[i].sd,index,x,y,3);
  8405. }
  8406. }
  8407. }
  8408. break;
  8409. case 2:
  8410. p = party_search(check_ID);
  8411. if(p){
  8412. for(i=0;i<MAX_PARTY; i++){
  8413. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8414. pc_setpos(p->data[i].sd,index,x,y,3);
  8415. }
  8416. }
  8417. }
  8418. break;
  8419. default:
  8420. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8421. break;
  8422. }
  8423. return 0;
  8424. }
  8425. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8426. {
  8427. char *event=va_arg(ap,char *);
  8428. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8429. return 1;
  8430. return 0;
  8431. }
  8432. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8433. {
  8434. const char *mapname,*event;
  8435. int m;
  8436. mapname=script_getstr(st,2);
  8437. event=script_getstr(st,3);
  8438. check_event(st, event);
  8439. if( (m=map_mapname2mapid(mapname))<0 ) {
  8440. script_pushint(st,-1);
  8441. return 0;
  8442. }
  8443. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8444. return 0;
  8445. }
  8446. BUILDIN_FUNC(marriage)
  8447. {
  8448. const char *partner=script_getstr(st,2);
  8449. TBL_PC *sd=script_rid2sd(st);
  8450. TBL_PC *p_sd=map_nick2sd(partner);
  8451. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8452. script_pushint(st,0);
  8453. return 0;
  8454. }
  8455. script_pushint(st,1);
  8456. return 0;
  8457. }
  8458. BUILDIN_FUNC(wedding_effect)
  8459. {
  8460. TBL_PC *sd=script_rid2sd(st);
  8461. struct block_list *bl;
  8462. if(sd==NULL) {
  8463. bl=map_id2bl(st->oid);
  8464. } else
  8465. bl=&sd->bl;
  8466. clif_wedding_effect(bl);
  8467. return 0;
  8468. }
  8469. BUILDIN_FUNC(divorce)
  8470. {
  8471. TBL_PC *sd=script_rid2sd(st);
  8472. if(sd==NULL || pc_divorce(sd) < 0){
  8473. script_pushint(st,0);
  8474. return 0;
  8475. }
  8476. script_pushint(st,1);
  8477. return 0;
  8478. }
  8479. BUILDIN_FUNC(ispartneron)
  8480. {
  8481. TBL_PC *sd=script_rid2sd(st);
  8482. TBL_PC *p_sd=NULL;
  8483. if(sd==NULL || !pc_ismarried(sd) ||
  8484. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8485. script_pushint(st,0);
  8486. return 0;
  8487. }
  8488. script_pushint(st,1);
  8489. return 0;
  8490. }
  8491. BUILDIN_FUNC(getpartnerid)
  8492. {
  8493. TBL_PC *sd=script_rid2sd(st);
  8494. if (sd == NULL) {
  8495. script_pushint(st,0);
  8496. return 0;
  8497. }
  8498. script_pushint(st,sd->status.partner_id);
  8499. return 0;
  8500. }
  8501. BUILDIN_FUNC(getchildid)
  8502. {
  8503. TBL_PC *sd=script_rid2sd(st);
  8504. if (sd == NULL) {
  8505. script_pushint(st,0);
  8506. return 0;
  8507. }
  8508. script_pushint(st,sd->status.child);
  8509. return 0;
  8510. }
  8511. BUILDIN_FUNC(getmotherid)
  8512. {
  8513. TBL_PC *sd=script_rid2sd(st);
  8514. if (sd == NULL) {
  8515. script_pushint(st,0);
  8516. return 0;
  8517. }
  8518. script_pushint(st,sd->status.mother);
  8519. return 0;
  8520. }
  8521. BUILDIN_FUNC(getfatherid)
  8522. {
  8523. TBL_PC *sd=script_rid2sd(st);
  8524. if (sd == NULL) {
  8525. script_pushint(st,0);
  8526. return 0;
  8527. }
  8528. script_pushint(st,sd->status.father);
  8529. return 0;
  8530. }
  8531. BUILDIN_FUNC(warppartner)
  8532. {
  8533. int x,y;
  8534. unsigned short mapindex;
  8535. const char *str;
  8536. TBL_PC *sd=script_rid2sd(st);
  8537. TBL_PC *p_sd=NULL;
  8538. if(sd==NULL || !pc_ismarried(sd) ||
  8539. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8540. script_pushint(st,0);
  8541. return 0;
  8542. }
  8543. str=script_getstr(st,2);
  8544. x=script_getnum(st,3);
  8545. y=script_getnum(st,4);
  8546. mapindex = mapindex_name2id(str);
  8547. if (mapindex) {
  8548. pc_setpos(p_sd,mapindex,x,y,0);
  8549. script_pushint(st,1);
  8550. } else
  8551. script_pushint(st,0);
  8552. return 0;
  8553. }
  8554. /*================================================
  8555. * Script for Displaying MOB Information [Valaris]
  8556. *------------------------------------------------*/
  8557. BUILDIN_FUNC(strmobinfo)
  8558. {
  8559. int num=script_getnum(st,2);
  8560. int class_=script_getnum(st,3);
  8561. if(!mobdb_checkid(class_))
  8562. {
  8563. script_pushint(st,0);
  8564. return 0;
  8565. }
  8566. switch (num) {
  8567. case 1: script_pushconststr(st,mob_db(class_)->name); break;
  8568. case 2: script_pushconststr(st,mob_db(class_)->jname); break;
  8569. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8570. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8571. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8572. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8573. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8574. default:
  8575. script_pushint(st,0);
  8576. break;
  8577. }
  8578. return 0;
  8579. }
  8580. /*==========================================
  8581. * Summon guardians [Valaris]
  8582. * guardian "<map name>",<x>,<y>,"<name to show>",<mob id>,{,"<event label>"}{,<guardian index>};
  8583. *------------------------------------------*/
  8584. BUILDIN_FUNC(guardian)
  8585. {
  8586. int class_=0,x=0,y=0,guardian=0;
  8587. const char *str,*map,*evt="";
  8588. struct script_data *data;
  8589. map =script_getstr(st,2);
  8590. x =script_getnum(st,3);
  8591. y =script_getnum(st,4);
  8592. str =script_getstr(st,5);
  8593. class_=script_getnum(st,6);
  8594. if( script_hasdata(st,8) )
  8595. {// "<event label>",<guardian index>
  8596. evt=script_getstr(st,7);
  8597. guardian=script_getnum(st,8);
  8598. } else if( script_hasdata(st,7) ){
  8599. data=script_getdata(st,7);
  8600. get_val(st,data);
  8601. if( data_isstring(data) )
  8602. {// "<event label>"
  8603. evt=script_getstr(st,7);
  8604. } else if( data_isint(data) )
  8605. {// <guardian index>
  8606. guardian=script_getnum(st,7);
  8607. } else {
  8608. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8609. script_reportdata(data);
  8610. return 1;
  8611. }
  8612. }
  8613. check_event(st, evt);
  8614. mob_spawn_guardian(map,x,y,str,class_,evt,guardian);
  8615. return 0;
  8616. }
  8617. /*================================================
  8618. * Script for Displaying Guardian Info [Valaris]
  8619. *------------------------------------------------*/
  8620. BUILDIN_FUNC(guardianinfo)
  8621. {
  8622. int guardian=script_getnum(st,2);
  8623. TBL_PC *sd=script_rid2sd(st);
  8624. struct guild_castle *gc=guild_mapname2gc(map[sd->bl.m].name);
  8625. if (guardian < 0 || guardian >= MAX_GUARDIANS || gc==NULL)
  8626. {
  8627. script_pushint(st,-1);
  8628. return 0;
  8629. }
  8630. if(gc->guardian[guardian].visible)
  8631. script_pushint(st,gc->guardian[guardian].hp);
  8632. else script_pushint(st,-1);
  8633. return 0;
  8634. }
  8635. /*==========================================
  8636. * IDからItem名
  8637. *------------------------------------------*/
  8638. BUILDIN_FUNC(getitemname)
  8639. {
  8640. int item_id=0;
  8641. struct item_data *i_data;
  8642. char *item_name;
  8643. struct script_data *data;
  8644. data=script_getdata(st,2);
  8645. get_val(st,data);
  8646. if( data_isstring(data) ){
  8647. const char *name=conv_str(st,data);
  8648. struct item_data *item_data = itemdb_searchname(name);
  8649. if( item_data )
  8650. item_id=item_data->nameid;
  8651. }else
  8652. item_id=conv_num(st,data);
  8653. i_data = itemdb_exists(item_id);
  8654. if (i_data == NULL)
  8655. {
  8656. script_pushconststr(st,"null");
  8657. return 0;
  8658. }
  8659. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8660. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8661. script_pushstr(st,item_name);
  8662. return 0;
  8663. }
  8664. /*==========================================
  8665. * Returns number of slots an item has. [Skotlex]
  8666. *------------------------------------------*/
  8667. BUILDIN_FUNC(getitemslots)
  8668. {
  8669. int item_id;
  8670. struct item_data *i_data;
  8671. item_id=script_getnum(st,2);
  8672. i_data = itemdb_exists(item_id);
  8673. if (i_data)
  8674. script_pushint(st,i_data->slot);
  8675. else
  8676. script_pushint(st,-1);
  8677. return 0;
  8678. }
  8679. /*==========================================
  8680. * Returns some values of an item [Lupus]
  8681. * Price, Weight, etc...
  8682. getiteminfo(itemID,n), where n
  8683. 0 value_buy;
  8684. 1 value_sell;
  8685. 2 type;
  8686. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8687. if = 0, then monsters don't drop it at all (rare or a quest item)
  8688. if = 10000, then this item is sold in NPC shops only
  8689. 4 sex;
  8690. 5 equip;
  8691. 6 weight;
  8692. 7 atk;
  8693. 8 def;
  8694. 9 range;
  8695. 10 slot;
  8696. 11 look;
  8697. 12 elv;
  8698. 13 wlv;
  8699. 14 view id
  8700. *------------------------------------------*/
  8701. BUILDIN_FUNC(getiteminfo)
  8702. {
  8703. int item_id,n;
  8704. int *item_arr;
  8705. struct item_data *i_data;
  8706. item_id = script_getnum(st,2);
  8707. n = script_getnum(st,3);
  8708. i_data = itemdb_exists(item_id);
  8709. if (i_data && n>=0 && n<14) {
  8710. item_arr = (int*)&i_data->value_buy;
  8711. script_pushint(st,item_arr[n]);
  8712. } else
  8713. script_pushint(st,-1);
  8714. return 0;
  8715. }
  8716. /*==========================================
  8717. * Set some values of an item [Lupus]
  8718. * Price, Weight, etc...
  8719. setiteminfo(itemID,n,Value), where n
  8720. 0 value_buy;
  8721. 1 value_sell;
  8722. 2 type;
  8723. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8724. if = 0, then monsters don't drop it at all (rare or a quest item)
  8725. if = 10000, then this item is sold in NPC shops only
  8726. 4 sex;
  8727. 5 equip;
  8728. 6 weight;
  8729. 7 atk;
  8730. 8 def;
  8731. 9 range;
  8732. 10 slot;
  8733. 11 look;
  8734. 12 elv;
  8735. 13 wlv;
  8736. 14 view id
  8737. * Returns Value or -1 if the wrong field's been set
  8738. *------------------------------------------*/
  8739. BUILDIN_FUNC(setiteminfo)
  8740. {
  8741. int item_id,n,value;
  8742. int *item_arr;
  8743. struct item_data *i_data;
  8744. item_id = script_getnum(st,2);
  8745. n = script_getnum(st,3);
  8746. value = script_getnum(st,4);
  8747. i_data = itemdb_exists(item_id);
  8748. if (i_data && n>=0 && n<14) {
  8749. item_arr = (int*)&i_data->value_buy;
  8750. item_arr[n] = value;
  8751. script_pushint(st,value);
  8752. } else
  8753. script_pushint(st,-1);
  8754. return 0;
  8755. }
  8756. /*==========================================
  8757. * Returns value from equipped item slot n [Lupus]
  8758. getequipcardid(num,slot)
  8759. where
  8760. num = eqip position slot
  8761. slot = 0,1,2,3 (Card Slot N)
  8762. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  8763. it's useful when you want to check item cards or if it's signed
  8764. Useful for such quests as "Sign this refined item with players name" etc
  8765. Hat[0] +4 -> Player's Hat[0] +4
  8766. *------------------------------------------*/
  8767. BUILDIN_FUNC(getequipcardid)
  8768. {
  8769. int i=-1,num,slot;
  8770. TBL_PC *sd;
  8771. num=script_getnum(st,2);
  8772. slot=script_getnum(st,3);
  8773. sd=script_rid2sd(st);
  8774. if (num > 0 && num <= ARRAYLENGTH(equip))
  8775. i=pc_checkequip(sd,equip[num-1]);
  8776. if(i >= 0 && slot>=0 && slot<4)
  8777. script_pushint(st,sd->status.inventory[i].card[slot]);
  8778. else
  8779. script_pushint(st,0);
  8780. return 0;
  8781. }
  8782. /*==========================================
  8783. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  8784. *------------------------------------------*/
  8785. BUILDIN_FUNC(petskillbonus)
  8786. {
  8787. struct pet_data *pd;
  8788. TBL_PC *sd=script_rid2sd(st);
  8789. if(sd==NULL || sd->pd==NULL)
  8790. return 0;
  8791. pd=sd->pd;
  8792. if (pd->bonus)
  8793. { //Clear previous bonus
  8794. if (pd->bonus->timer != -1)
  8795. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  8796. } else //init
  8797. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  8798. pd->bonus->type=script_getnum(st,2);
  8799. pd->bonus->val=script_getnum(st,3);
  8800. pd->bonus->duration=script_getnum(st,4);
  8801. pd->bonus->delay=script_getnum(st,5);
  8802. if (pd->state.skillbonus == 1)
  8803. pd->state.skillbonus=0; // waiting state
  8804. // wait for timer to start
  8805. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  8806. pd->bonus->timer=-1;
  8807. else
  8808. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  8809. return 0;
  8810. }
  8811. /*==========================================
  8812. * pet looting [Valaris] //Rewritten by [Skotlex]
  8813. *------------------------------------------*/
  8814. BUILDIN_FUNC(petloot)
  8815. {
  8816. int max;
  8817. struct pet_data *pd;
  8818. TBL_PC *sd=script_rid2sd(st);
  8819. if(sd==NULL || sd->pd==NULL)
  8820. return 0;
  8821. max=script_getnum(st,2);
  8822. if(max < 1)
  8823. max = 1; //Let'em loot at least 1 item.
  8824. else if (max > MAX_PETLOOT_SIZE)
  8825. max = MAX_PETLOOT_SIZE;
  8826. pd = sd->pd;
  8827. if (pd->loot != NULL)
  8828. { //Release whatever was there already and reallocate memory
  8829. pet_lootitem_drop(pd, pd->msd);
  8830. aFree(pd->loot->item);
  8831. }
  8832. else
  8833. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  8834. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  8835. pd->loot->max=max;
  8836. pd->loot->count = 0;
  8837. pd->loot->weight = 0;
  8838. return 0;
  8839. }
  8840. /*==========================================
  8841. * PCの所持品情報読み取り
  8842. *------------------------------------------*/
  8843. BUILDIN_FUNC(getinventorylist)
  8844. {
  8845. TBL_PC *sd=script_rid2sd(st);
  8846. char card_var[NAME_LENGTH];
  8847. int i,j=0,k;
  8848. if(!sd) return 0;
  8849. for(i=0;i<MAX_INVENTORY;i++){
  8850. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  8851. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  8852. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  8853. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  8854. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  8855. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  8856. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  8857. for (k = 0; k < MAX_SLOTS; k++)
  8858. {
  8859. sprintf(card_var, "@inventorylist_card%d",k+1);
  8860. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  8861. }
  8862. j++;
  8863. }
  8864. }
  8865. pc_setreg(sd,add_str("@inventorylist_count"),j);
  8866. return 0;
  8867. }
  8868. BUILDIN_FUNC(getskilllist)
  8869. {
  8870. TBL_PC *sd=script_rid2sd(st);
  8871. int i,j=0;
  8872. if(!sd) return 0;
  8873. for(i=0;i<MAX_SKILL;i++){
  8874. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  8875. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  8876. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  8877. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  8878. j++;
  8879. }
  8880. }
  8881. pc_setreg(sd,add_str("@skilllist_count"),j);
  8882. return 0;
  8883. }
  8884. BUILDIN_FUNC(clearitem)
  8885. {
  8886. TBL_PC *sd=script_rid2sd(st);
  8887. int i;
  8888. if(sd==NULL) return 0;
  8889. for (i=0; i<MAX_INVENTORY; i++) {
  8890. if (sd->status.inventory[i].amount) {
  8891. //Logs items, got from (N)PC scripts [Lupus]
  8892. if(log_config.enable_logs&0x40)
  8893. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  8894. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  8895. }
  8896. }
  8897. return 0;
  8898. }
  8899. /*==========================================
  8900. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  8901. *------------------------------------------*/
  8902. BUILDIN_FUNC(disguise)
  8903. {
  8904. int id;
  8905. TBL_PC* sd = script_rid2sd(st);
  8906. if (sd == NULL) return 0;
  8907. id = script_getnum(st,2);
  8908. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  8909. pc_disguise(sd, id);
  8910. script_pushint(st,id);
  8911. } else
  8912. script_pushint(st,0);
  8913. return 0;
  8914. }
  8915. /*==========================================
  8916. * Undisguise Player (returns 1 if success, 0 on fail)
  8917. *------------------------------------------*/
  8918. BUILDIN_FUNC(undisguise)
  8919. {
  8920. TBL_PC* sd = script_rid2sd(st);
  8921. if (sd == NULL) return 0;
  8922. if (sd->disguise) {
  8923. pc_disguise(sd, 0);
  8924. script_pushint(st,0);
  8925. } else {
  8926. script_pushint(st,1);
  8927. }
  8928. return 0;
  8929. }
  8930. /*==========================================
  8931. * NPCクラスチェンジ
  8932. * classは変わりたいclass
  8933. * typeは通常0なのかな?
  8934. *------------------------------------------*/
  8935. BUILDIN_FUNC(classchange)
  8936. {
  8937. int _class,type;
  8938. struct block_list *bl=map_id2bl(st->oid);
  8939. if(bl==NULL) return 0;
  8940. _class=script_getnum(st,2);
  8941. type=script_getnum(st,3);
  8942. clif_class_change(bl,_class,type);
  8943. return 0;
  8944. }
  8945. /*==========================================
  8946. * NPCから発生するエフェクト
  8947. *------------------------------------------*/
  8948. BUILDIN_FUNC(misceffect)
  8949. {
  8950. int type;
  8951. type=script_getnum(st,2);
  8952. if(st->oid && st->oid != fake_nd->bl.id) {
  8953. struct block_list *bl = map_id2bl(st->oid);
  8954. if (bl)
  8955. clif_misceffect2(bl,type);
  8956. } else{
  8957. TBL_PC *sd=script_rid2sd(st);
  8958. if(sd)
  8959. clif_misceffect2(&sd->bl,type);
  8960. }
  8961. return 0;
  8962. }
  8963. /*==========================================
  8964. * サウンドエフェクト
  8965. *------------------------------------------*/
  8966. BUILDIN_FUNC(soundeffect)
  8967. {
  8968. TBL_PC* sd = script_rid2sd(st);
  8969. const char* name = script_getstr(st,2);
  8970. int type = script_getnum(st,3);
  8971. if(sd)
  8972. {
  8973. if(!st->rid)
  8974. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  8975. else
  8976. clif_soundeffect(sd,&sd->bl,name,type);
  8977. }
  8978. return 0;
  8979. }
  8980. int soundeffect_sub(struct block_list* bl,va_list ap)
  8981. {
  8982. char* name = va_arg(ap,char*);
  8983. int type = va_arg(ap,int);
  8984. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  8985. return 0;
  8986. }
  8987. /*==========================================
  8988. * Play a sound effect (.wav) on multiple clients
  8989. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  8990. *------------------------------------------*/
  8991. BUILDIN_FUNC(soundeffectall)
  8992. {
  8993. struct block_list* bl;
  8994. const char* name;
  8995. int type;
  8996. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  8997. if (!bl)
  8998. return 0;
  8999. name = script_getstr(st,2);
  9000. type = script_getnum(st,3);
  9001. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9002. if(!script_hasdata(st,4))
  9003. { // area around
  9004. clif_soundeffectall(bl, name, type, AREA);
  9005. }
  9006. else
  9007. if(!script_hasdata(st,5))
  9008. { // entire map
  9009. const char* map = script_getstr(st,4);
  9010. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9011. }
  9012. else
  9013. if(script_hasdata(st,9))
  9014. { // specified part of map
  9015. const char* map = script_getstr(st,5);
  9016. int x0 = script_getnum(st,6);
  9017. int y0 = script_getnum(st,7);
  9018. int x1 = script_getnum(st,8);
  9019. int y1 = script_getnum(st,9);
  9020. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9021. }
  9022. else
  9023. {
  9024. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9025. }
  9026. return 0;
  9027. }
  9028. /*==========================================
  9029. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9030. *------------------------------------------*/
  9031. BUILDIN_FUNC(petrecovery)
  9032. {
  9033. struct pet_data *pd;
  9034. TBL_PC *sd=script_rid2sd(st);
  9035. if(sd==NULL || sd->pd==NULL)
  9036. return 0;
  9037. pd=sd->pd;
  9038. if (pd->recovery)
  9039. { //Halt previous bonus
  9040. if (pd->recovery->timer != -1)
  9041. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9042. } else //Init
  9043. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9044. pd->recovery->type=script_getnum(st,2);
  9045. pd->recovery->delay=script_getnum(st,3);
  9046. pd->recovery->timer=-1;
  9047. return 0;
  9048. }
  9049. /*==========================================
  9050. * pet healing [Valaris] //Rewritten by [Skotlex]
  9051. *------------------------------------------*/
  9052. BUILDIN_FUNC(petheal)
  9053. {
  9054. struct pet_data *pd;
  9055. TBL_PC *sd=script_rid2sd(st);
  9056. if(sd==NULL || sd->pd==NULL)
  9057. return 0;
  9058. pd=sd->pd;
  9059. if (pd->s_skill)
  9060. { //Clear previous skill
  9061. if (pd->s_skill->timer != -1)
  9062. {
  9063. if (pd->s_skill->id)
  9064. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9065. else
  9066. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9067. }
  9068. } else //init memory
  9069. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9070. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9071. //Use the lv as the amount to heal
  9072. pd->s_skill->lv=script_getnum(st,2);
  9073. pd->s_skill->delay=script_getnum(st,3);
  9074. pd->s_skill->hp=script_getnum(st,4);
  9075. pd->s_skill->sp=script_getnum(st,5);
  9076. //Use delay as initial offset to avoid skill/heal exploits
  9077. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9078. pd->s_skill->timer=-1;
  9079. else
  9080. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9081. return 0;
  9082. }
  9083. /*==========================================
  9084. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9085. *------------------------------------------*/
  9086. BUILDIN_FUNC(petskillattack)
  9087. {
  9088. struct pet_data *pd;
  9089. TBL_PC *sd=script_rid2sd(st);
  9090. if(sd==NULL || sd->pd==NULL)
  9091. return 0;
  9092. pd=sd->pd;
  9093. if (pd->a_skill == NULL)
  9094. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9095. pd->a_skill->id=script_getnum(st,2);
  9096. pd->a_skill->lv=script_getnum(st,3);
  9097. pd->a_skill->div_ = 0;
  9098. pd->a_skill->rate=script_getnum(st,4);
  9099. pd->a_skill->bonusrate=script_getnum(st,5);
  9100. return 0;
  9101. }
  9102. /*==========================================
  9103. * pet attack skills [Valaris]
  9104. *------------------------------------------*/
  9105. BUILDIN_FUNC(petskillattack2)
  9106. {
  9107. struct pet_data *pd;
  9108. TBL_PC *sd=script_rid2sd(st);
  9109. if(sd==NULL || sd->pd==NULL)
  9110. return 0;
  9111. pd=sd->pd;
  9112. if (pd->a_skill == NULL)
  9113. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9114. pd->a_skill->id=script_getnum(st,2);
  9115. pd->a_skill->lv=script_getnum(st,3);
  9116. pd->a_skill->div_ = script_getnum(st,4);
  9117. pd->a_skill->rate=script_getnum(st,5);
  9118. pd->a_skill->bonusrate=script_getnum(st,6);
  9119. return 0;
  9120. }
  9121. /*==========================================
  9122. * pet support skills [Skotlex]
  9123. *------------------------------------------*/
  9124. BUILDIN_FUNC(petskillsupport)
  9125. {
  9126. struct pet_data *pd;
  9127. TBL_PC *sd=script_rid2sd(st);
  9128. if(sd==NULL || sd->pd==NULL)
  9129. return 0;
  9130. pd=sd->pd;
  9131. if (pd->s_skill)
  9132. { //Clear previous skill
  9133. if (pd->s_skill->timer != -1)
  9134. {
  9135. if (pd->s_skill->id)
  9136. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9137. else
  9138. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9139. }
  9140. } else //init memory
  9141. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9142. pd->s_skill->id=script_getnum(st,2);
  9143. pd->s_skill->lv=script_getnum(st,3);
  9144. pd->s_skill->delay=script_getnum(st,4);
  9145. pd->s_skill->hp=script_getnum(st,5);
  9146. pd->s_skill->sp=script_getnum(st,6);
  9147. //Use delay as initial offset to avoid skill/heal exploits
  9148. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9149. pd->s_skill->timer=-1;
  9150. else
  9151. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9152. return 0;
  9153. }
  9154. /*==========================================
  9155. * Scripted skill effects [Celest]
  9156. *------------------------------------------*/
  9157. BUILDIN_FUNC(skilleffect)
  9158. {
  9159. TBL_PC *sd;
  9160. int skillid=script_getnum(st,2);
  9161. int skilllv=script_getnum(st,3);
  9162. sd=script_rid2sd(st);
  9163. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9164. return 0;
  9165. }
  9166. /*==========================================
  9167. * NPC skill effects [Valaris]
  9168. *------------------------------------------*/
  9169. BUILDIN_FUNC(npcskilleffect)
  9170. {
  9171. struct block_list *bl= map_id2bl(st->oid);
  9172. int skillid=script_getnum(st,2);
  9173. int skilllv=script_getnum(st,3);
  9174. int x=script_getnum(st,4);
  9175. int y=script_getnum(st,5);
  9176. if (bl)
  9177. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9178. return 0;
  9179. }
  9180. /*==========================================
  9181. * Special effects [Valaris]
  9182. *------------------------------------------*/
  9183. BUILDIN_FUNC(specialeffect)
  9184. {
  9185. struct block_list *bl=map_id2bl(st->oid);
  9186. if(bl==NULL)
  9187. return 0;
  9188. clif_specialeffect(bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9189. return 0;
  9190. }
  9191. BUILDIN_FUNC(specialeffect2)
  9192. {
  9193. TBL_PC *sd=script_rid2sd(st);
  9194. if(sd==NULL)
  9195. return 0;
  9196. clif_specialeffect(&sd->bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9197. return 0;
  9198. }
  9199. /*==========================================
  9200. * Nude [Valaris]
  9201. *------------------------------------------*/
  9202. BUILDIN_FUNC(nude)
  9203. {
  9204. TBL_PC *sd=script_rid2sd(st);
  9205. int i,calcflag=0;
  9206. if(sd==NULL)
  9207. return 0;
  9208. for(i=0;i<11;i++)
  9209. if(sd->equip_index[i] >= 0) {
  9210. if(!calcflag)
  9211. calcflag=1;
  9212. pc_unequipitem(sd,sd->equip_index[i],2);
  9213. }
  9214. if(calcflag)
  9215. status_calc_pc(sd,0);
  9216. return 0;
  9217. }
  9218. /*==========================================
  9219. * gmcommand [MouseJstr]
  9220. *
  9221. * suggested on the forums...
  9222. * splitted into atcommand & charcommand by [Skotlex]
  9223. *------------------------------------------*/
  9224. BUILDIN_FUNC(atcommand)
  9225. {
  9226. TBL_PC dummy_sd;
  9227. TBL_PC* sd;
  9228. int fd;
  9229. const char* cmd;
  9230. cmd = script_getstr(st,2);
  9231. if (st->rid) {
  9232. sd = script_rid2sd(st);
  9233. fd = sd->fd;
  9234. } else { //Use a dummy character.
  9235. sd = &dummy_sd;
  9236. fd = 0;
  9237. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9238. if (st->oid)
  9239. {
  9240. struct block_list* bl = map_id2bl(st->oid);
  9241. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9242. if (bl->type == BL_NPC)
  9243. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9244. }
  9245. }
  9246. // compatibility with previous implementation (deprecated!)
  9247. if(cmd[0] != atcommand_symbol)
  9248. {
  9249. cmd += strlen(sd->status.name);
  9250. while(*cmd != atcommand_symbol && *cmd != 0)
  9251. cmd++;
  9252. }
  9253. is_atcommand_sub(fd, sd, cmd, 99);
  9254. return 0;
  9255. }
  9256. BUILDIN_FUNC(charcommand)
  9257. {
  9258. TBL_PC dummy_sd;
  9259. TBL_PC* sd;
  9260. int fd;
  9261. const char* cmd;
  9262. cmd = script_getstr(st,2);
  9263. if (st->rid) {
  9264. sd = script_rid2sd(st);
  9265. fd = sd->fd;
  9266. } else { //Use a dummy character.
  9267. sd = &dummy_sd;
  9268. fd = 0;
  9269. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9270. if (st->oid)
  9271. {
  9272. struct block_list* bl = map_id2bl(st->oid);
  9273. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9274. if (bl->type == BL_NPC)
  9275. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9276. }
  9277. }
  9278. // compatibility with previous implementation (deprecated!)
  9279. if(cmd[0] != charcommand_symbol)
  9280. {
  9281. cmd += strlen(sd->status.name);
  9282. while(*cmd != charcommand_symbol && *cmd != 0)
  9283. cmd++;
  9284. }
  9285. is_charcommand_sub(fd, sd, cmd, 99);
  9286. return 0;
  9287. }
  9288. /*==========================================
  9289. * Displays a message for the player only (like system messages like "you got an apple" )
  9290. *------------------------------------------*/
  9291. BUILDIN_FUNC(dispbottom)
  9292. {
  9293. TBL_PC *sd=script_rid2sd(st);
  9294. const char *message;
  9295. message=script_getstr(st,2);
  9296. if(sd)
  9297. clif_disp_onlyself(sd,message,(int)strlen(message));
  9298. return 0;
  9299. }
  9300. /*==========================================
  9301. * All The Players Full Recovery
  9302. * (HP/SP full restore and resurrect if need)
  9303. *------------------------------------------*/
  9304. BUILDIN_FUNC(recovery)
  9305. {
  9306. TBL_PC *sd, **all_sd;
  9307. int i = 0, users;
  9308. all_sd = map_getallusers(&users);
  9309. for (i = 0; i < users; i++)
  9310. {
  9311. sd = all_sd[i];
  9312. if(pc_isdead(sd))
  9313. status_revive(&sd->bl, 100, 100);
  9314. else
  9315. status_percent_heal(&sd->bl, 100, 100);
  9316. clif_displaymessage(sd->fd,"You have been recovered!");
  9317. }
  9318. return 0;
  9319. }
  9320. /*==========================================
  9321. * Get your pet info: getpetinfo(n)
  9322. * n -> 0:pet_id 1:pet_class 2:pet_name
  9323. * 3:friendly 4:hungry, 5: rename flag.
  9324. *------------------------------------------*/
  9325. BUILDIN_FUNC(getpetinfo)
  9326. {
  9327. TBL_PC *sd=script_rid2sd(st);
  9328. TBL_PET *pd;
  9329. int type=script_getnum(st,2);
  9330. if(!sd || !sd->pd) {
  9331. if (type == 2)
  9332. script_pushconststr(st,"null");
  9333. else
  9334. script_pushint(st,0);
  9335. return 0;
  9336. }
  9337. pd = sd->pd;
  9338. switch(type){
  9339. case 0: script_pushint(st,pd->pet.pet_id); break;
  9340. case 1: script_pushint(st,pd->pet.class_); break;
  9341. case 2: script_pushstr(st,aStrdup(pd->pet.name)); break;
  9342. case 3: script_pushint(st,pd->pet.intimate); break;
  9343. case 4: script_pushint(st,pd->pet.hungry); break;
  9344. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9345. default:
  9346. script_pushint(st,0);
  9347. break;
  9348. }
  9349. return 0;
  9350. }
  9351. /*==========================================
  9352. * Get your homunculus info: gethominfo(n)
  9353. * n -> 0:hom_id 1:class 2:name
  9354. * 3:friendly 4:hungry, 5: rename flag.
  9355. * 6: level
  9356. *------------------------------------------*/
  9357. BUILDIN_FUNC(gethominfo)
  9358. {
  9359. TBL_PC *sd=script_rid2sd(st);
  9360. TBL_HOM *hd;
  9361. int type=script_getnum(st,2);
  9362. hd = sd?sd->hd:NULL;
  9363. if(!merc_is_hom_active(hd))
  9364. {
  9365. if (type == 2)
  9366. script_pushconststr(st,"null");
  9367. else
  9368. script_pushint(st,0);
  9369. return 0;
  9370. }
  9371. switch(type){
  9372. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9373. case 1: script_pushint(st,hd->homunculus.class_); break;
  9374. case 2: script_pushstr(st,aStrdup(hd->homunculus.name)); break;
  9375. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9376. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9377. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9378. case 6: script_pushint(st,hd->homunculus.level); break;
  9379. default:
  9380. script_pushint(st,0);
  9381. break;
  9382. }
  9383. return 0;
  9384. }
  9385. /*==========================================
  9386. * Shows wether your inventory(and equips) contain
  9387. selected card or not.
  9388. checkequipedcard(4001);
  9389. *------------------------------------------*/
  9390. BUILDIN_FUNC(checkequipedcard)
  9391. {
  9392. TBL_PC *sd=script_rid2sd(st);
  9393. int n,i,c=0;
  9394. c=script_getnum(st,2);
  9395. if(sd){
  9396. for(i=0;i<MAX_INVENTORY;i++){
  9397. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9398. for(n=0;n<MAX_SLOTS;n++){
  9399. if(sd->status.inventory[i].card[n]==c){
  9400. script_pushint(st,1);
  9401. return 0;
  9402. }
  9403. }
  9404. }
  9405. }
  9406. }
  9407. script_pushint(st,0);
  9408. return 0;
  9409. }
  9410. BUILDIN_FUNC(jump_zero)
  9411. {
  9412. int sel;
  9413. sel=script_getnum(st,2);
  9414. if(!sel) {
  9415. int pos;
  9416. if( !data_islabel(script_getdata(st,3)) ){
  9417. ShowError("script: jump_zero: not label !\n");
  9418. st->state=END;
  9419. return 1;
  9420. }
  9421. pos=script_getnum(st,3);
  9422. st->pos=pos;
  9423. st->state=GOTO;
  9424. // printf("script: jump_zero: jumpto : %d\n",pos);
  9425. } else {
  9426. // printf("script: jump_zero: fail\n");
  9427. }
  9428. return 0;
  9429. }
  9430. /*==========================================
  9431. * GetMapMobs
  9432. returns mob counts on a set map:
  9433. e.g. GetMapMobs("prontera")
  9434. use "this" - for player's map
  9435. *------------------------------------------*/
  9436. BUILDIN_FUNC(getmapmobs)
  9437. {
  9438. const char *str=NULL;
  9439. int m=-1,bx,by,i;
  9440. int count=0,c;
  9441. struct block_list *bl;
  9442. str=script_getstr(st,2);
  9443. if(strcmp(str,"this")==0){
  9444. TBL_PC *sd=script_rid2sd(st);
  9445. if(sd)
  9446. m=sd->bl.m;
  9447. else{
  9448. script_pushint(st,-1);
  9449. return 0;
  9450. }
  9451. }else
  9452. m=map_mapname2mapid(str);
  9453. if(m < 0){
  9454. script_pushint(st,-1);
  9455. return 0;
  9456. }
  9457. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++){
  9458. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++){
  9459. bl = map[m].block_mob[bx+by*map[m].bxs];
  9460. c = map[m].block_mob_count[bx+by*map[m].bxs];
  9461. for(i=0;i<c && bl;i++,bl=bl->next){
  9462. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9463. count++;
  9464. }
  9465. }
  9466. }
  9467. script_pushint(st,count);
  9468. return 0;
  9469. }
  9470. /*==========================================
  9471. * movenpc [MouseJstr]
  9472. *------------------------------------------*/
  9473. BUILDIN_FUNC(movenpc)
  9474. {
  9475. TBL_NPC *nd = NULL;
  9476. const char *npc;
  9477. int x,y;
  9478. npc = script_getstr(st,2);
  9479. x = script_getnum(st,3);
  9480. y = script_getnum(st,4);
  9481. if ((nd = npc_name2id(npc)) == NULL)
  9482. return -1;
  9483. npc_movenpc(nd, x, y);
  9484. return 0;
  9485. }
  9486. /*==========================================
  9487. * message [MouseJstr]
  9488. *------------------------------------------*/
  9489. BUILDIN_FUNC(message)
  9490. {
  9491. TBL_PC *sd;
  9492. const char *msg,*player;
  9493. TBL_PC *pl_sd = NULL;
  9494. sd = script_rid2sd(st);
  9495. player = script_getstr(st,2);
  9496. msg = script_getstr(st,3);
  9497. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9498. return 0;
  9499. clif_displaymessage(pl_sd->fd, msg);
  9500. return 0;
  9501. }
  9502. /*==========================================
  9503. * npctalk (sends message to surrounding area)
  9504. *------------------------------------------*/
  9505. BUILDIN_FUNC(npctalk)
  9506. {
  9507. const char* str;
  9508. char message[255];
  9509. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9510. str = script_getstr(st,2);
  9511. if(nd) {
  9512. memcpy(message, nd->name, NAME_LENGTH);
  9513. strtok(message, "#"); // discard extra name identifier if present
  9514. strcat(message, " : ");
  9515. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9516. clif_message(&(nd->bl), message);
  9517. }
  9518. return 0;
  9519. }
  9520. // change npc walkspeed [Valaris]
  9521. BUILDIN_FUNC(npcspeed)
  9522. {
  9523. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9524. int x=0;
  9525. x=script_getnum(st,2);
  9526. if(nd) {
  9527. nd->speed=x;
  9528. }
  9529. return 0;
  9530. }
  9531. // make an npc walk to a position [Valaris]
  9532. BUILDIN_FUNC(npcwalkto)
  9533. {
  9534. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9535. int x=0,y=0;
  9536. x=script_getnum(st,2);
  9537. y=script_getnum(st,3);
  9538. if(nd) {
  9539. unit_walktoxy(&nd->bl,x,y,0);
  9540. }
  9541. return 0;
  9542. }
  9543. // stop an npc's movement [Valaris]
  9544. BUILDIN_FUNC(npcstop)
  9545. {
  9546. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9547. if(nd) {
  9548. unit_stop_walking(&nd->bl,1);
  9549. }
  9550. return 0;
  9551. }
  9552. /*==========================================
  9553. * getlook char info. getlook(arg)
  9554. *------------------------------------------*/
  9555. BUILDIN_FUNC(getlook)
  9556. {
  9557. int type,val;
  9558. TBL_PC *sd;
  9559. sd=script_rid2sd(st);
  9560. type=script_getnum(st,2);
  9561. val=-1;
  9562. switch(type) {
  9563. case LOOK_HAIR: val=sd->status.hair; break; //1
  9564. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9565. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9566. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9567. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9568. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9569. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9570. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9571. case LOOK_SHOES: break; //9
  9572. }
  9573. script_pushint(st,val);
  9574. return 0;
  9575. }
  9576. /*==========================================
  9577. * get char save point. argument: 0- map name, 1- x, 2- y
  9578. *------------------------------------------*/
  9579. BUILDIN_FUNC(getsavepoint)
  9580. {
  9581. TBL_PC* sd;
  9582. int type;
  9583. sd = script_rid2sd(st);
  9584. if (sd == NULL) {
  9585. script_pushint(st,0);
  9586. return 0;
  9587. }
  9588. type = script_getnum(st,2);
  9589. switch(type) {
  9590. case 0: script_pushstr(st,aStrdup(mapindex_id2name(sd->status.save_point.map))); break;
  9591. case 1: script_pushint(st,sd->status.save_point.x); break;
  9592. case 2: script_pushint(st,sd->status.save_point.y); break;
  9593. default:
  9594. script_pushint(st,0);
  9595. break;
  9596. }
  9597. return 0;
  9598. }
  9599. /*==========================================
  9600. * Get position for char/npc/pet/mob objects. Added by Lorky
  9601. *
  9602. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  9603. * where type:
  9604. * MapName$ - String variable for output map name
  9605. * MapX - Integer variable for output coord X
  9606. * MapY - Integer variable for output coord Y
  9607. * type - type of object
  9608. * 0 - Character coord
  9609. * 1 - NPC coord
  9610. * 2 - Pet coord
  9611. * 3 - Mob coord (not released)
  9612. * 4 - Homun coord
  9613. * CharName$ - Name object. If miss or "this" the current object
  9614. *
  9615. * Return:
  9616. * 0 - success
  9617. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9618. *------------------------------------------*/
  9619. BUILDIN_FUNC(getmapxy)
  9620. {
  9621. struct block_list *bl = NULL;
  9622. TBL_PC *sd=NULL;
  9623. int num;
  9624. char *name;
  9625. char prefix;
  9626. int x,y,type;
  9627. char mapname[MAP_NAME_LENGTH];
  9628. memset(mapname, 0, sizeof(mapname));
  9629. if( !data_isreference(script_getdata(st,2)) ){
  9630. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9631. script_pushint(st,-1);
  9632. return 1;
  9633. }
  9634. if( !data_isreference(script_getdata(st,3)) ){
  9635. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9636. script_pushint(st,-1);
  9637. return 1;
  9638. }
  9639. if( !data_isreference(script_getdata(st,4)) ){
  9640. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9641. script_pushint(st,-1);
  9642. return 1;
  9643. }
  9644. // Possible needly check function parameters on C_STR,C_INT,C_INT
  9645. type=script_getnum(st,5);
  9646. switch (type){
  9647. case 0: //Get Character Position
  9648. if( script_hasdata(st,6) )
  9649. sd=map_nick2sd(script_getstr(st,6));
  9650. else
  9651. sd=script_rid2sd(st);
  9652. if (sd)
  9653. bl = &sd->bl;
  9654. break;
  9655. case 1: //Get NPC Position
  9656. if( script_hasdata(st,6) )
  9657. {
  9658. struct npc_data *nd;
  9659. nd=npc_name2id(script_getstr(st,6));
  9660. if (nd)
  9661. bl = &nd->bl;
  9662. } else //In case the origin is not an npc?
  9663. bl=map_id2bl(st->oid);
  9664. break;
  9665. case 2: //Get Pet Position
  9666. if(script_hasdata(st,6))
  9667. sd=map_nick2sd(script_getstr(st,6));
  9668. else
  9669. sd=script_rid2sd(st);
  9670. if (sd && sd->pd)
  9671. bl = &sd->pd->bl;
  9672. break;
  9673. case 3: //Get Mob Position
  9674. break; //Not supported?
  9675. case 4: //Get Homun Position
  9676. if(script_hasdata(st,6))
  9677. sd=map_nick2sd(script_getstr(st,6));
  9678. else
  9679. sd=script_rid2sd(st);
  9680. if (sd && sd->hd)
  9681. bl = &sd->hd->bl;
  9682. break;
  9683. }
  9684. if (!bl) { //No object found.
  9685. script_pushint(st,-1);
  9686. return 0;
  9687. }
  9688. x= bl->x;
  9689. y= bl->y;
  9690. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9691. //Set MapName$
  9692. num=st->stack->stack_data[st->start+2].u.num;
  9693. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9694. prefix=*name;
  9695. if(not_server_variable(prefix))
  9696. sd=script_rid2sd(st);
  9697. else
  9698. sd=NULL;
  9699. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  9700. //Set MapX
  9701. num=st->stack->stack_data[st->start+3].u.num;
  9702. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9703. prefix=*name;
  9704. if(not_server_variable(prefix))
  9705. sd=script_rid2sd(st);
  9706. else
  9707. sd=NULL;
  9708. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  9709. //Set MapY
  9710. num=st->stack->stack_data[st->start+4].u.num;
  9711. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9712. prefix=*name;
  9713. if(not_server_variable(prefix))
  9714. sd=script_rid2sd(st);
  9715. else
  9716. sd=NULL;
  9717. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  9718. //Return Success value
  9719. script_pushint(st,0);
  9720. return 0;
  9721. }
  9722. /*==========================================
  9723. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  9724. *------------------------------------------*/
  9725. BUILDIN_FUNC(logmes)
  9726. {
  9727. const char *str;
  9728. if (log_config.npc <= 0 ) return 0;
  9729. str = script_getstr(st,2);
  9730. log_npc(script_rid2sd(st),str);
  9731. return 0;
  9732. }
  9733. BUILDIN_FUNC(summon)
  9734. {
  9735. int _class, timeout=0;
  9736. const char *str,*event="";
  9737. TBL_PC *sd;
  9738. struct mob_data *md;
  9739. int tick = gettick();
  9740. sd=script_rid2sd(st);
  9741. if (!sd) return 0;
  9742. str =script_getstr(st,2);
  9743. _class=script_getnum(st,3);
  9744. if( script_hasdata(st,4) )
  9745. timeout=script_getnum(st,4);
  9746. if( script_hasdata(st,5) ){
  9747. event=script_getstr(st,5);
  9748. check_event(st, event);
  9749. }
  9750. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  9751. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  9752. if (md) {
  9753. md->master_id=sd->bl.id;
  9754. md->special_state.ai=1;
  9755. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  9756. mob_spawn (md); //Now it is ready for spawning.
  9757. clif_misceffect2(&md->bl,344);
  9758. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  9759. }
  9760. return 0;
  9761. }
  9762. /*==========================================
  9763. * Checks whether it is daytime/nighttime
  9764. *------------------------------------------*/
  9765. BUILDIN_FUNC(isnight)
  9766. {
  9767. script_pushint(st,(night_flag == 1));
  9768. return 0;
  9769. }
  9770. BUILDIN_FUNC(isday)
  9771. {
  9772. script_pushint(st,(night_flag == 0));
  9773. return 0;
  9774. }
  9775. /*================================================
  9776. * Check whether another item/card has been
  9777. * equipped - used for 2/15's cards patch [celest]
  9778. *------------------------------------------------*/
  9779. // leave this here, just in case
  9780. #if 0
  9781. BUILDIN_FUNC(isequipped)
  9782. {
  9783. TBL_PC *sd;
  9784. int i, j, k, id = 1;
  9785. int ret = -1;
  9786. sd = script_rid2sd(st);
  9787. for (i=0; id!=0; i++) {
  9788. int flag = 0;
  9789. FETCH (i+2, id) else id = 0;
  9790. if (id <= 0)
  9791. continue;
  9792. for (j=0; j<EQI_MAX; j++) {
  9793. int index;
  9794. index = sd->equip_index[j];
  9795. if(index < 0) continue;
  9796. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9797. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9798. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9799. if(!sd->inventory_data[index])
  9800. continue;
  9801. if(itemdb_type(id) != IT_CARD) { //Non card
  9802. if (sd->inventory_data[index]->nameid != id)
  9803. continue;
  9804. flag = 1;
  9805. break;
  9806. } else { //Card
  9807. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9808. continue;
  9809. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9810. if (sd->status.inventory[index].card[k] == id)
  9811. {
  9812. flag = 1;
  9813. break;
  9814. }
  9815. }
  9816. }
  9817. if (flag) break;
  9818. }
  9819. if (ret == -1)
  9820. ret = flag;
  9821. else
  9822. ret &= flag;
  9823. if (!ret) break;
  9824. }
  9825. script_pushint(st,ret);
  9826. return 0;
  9827. }
  9828. #endif
  9829. /*================================================
  9830. * Check how many items/cards in the list are
  9831. * equipped - used for 2/15's cards patch [celest]
  9832. *------------------------------------------------*/
  9833. BUILDIN_FUNC(isequippedcnt)
  9834. {
  9835. TBL_PC *sd;
  9836. int i, j, k, id = 1;
  9837. int ret = 0;
  9838. sd = script_rid2sd(st);
  9839. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9840. script_pushint(st,0);
  9841. return 0;
  9842. }
  9843. for (i=0; id!=0; i++) {
  9844. FETCH (i+2, id) else id = 0;
  9845. if (id <= 0)
  9846. continue;
  9847. for (j=0; j<EQI_MAX; j++) {
  9848. int index;
  9849. index = sd->equip_index[j];
  9850. if(index < 0) continue;
  9851. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9852. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9853. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9854. if(!sd->inventory_data[index])
  9855. continue;
  9856. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  9857. if (sd->inventory_data[index]->nameid == id)
  9858. ret+= sd->status.inventory[index].amount;
  9859. } else { //Count cards.
  9860. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9861. continue; //No cards
  9862. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9863. if (sd->status.inventory[index].card[k] == id)
  9864. ret++; //[Lupus]
  9865. }
  9866. }
  9867. }
  9868. }
  9869. script_pushint(st,ret);
  9870. return 0;
  9871. }
  9872. /*================================================
  9873. * Check whether another card has been
  9874. * equipped - used for 2/15's cards patch [celest]
  9875. * -- Items checked cannot be reused in another
  9876. * card set to prevent exploits
  9877. *------------------------------------------------*/
  9878. BUILDIN_FUNC(isequipped)
  9879. {
  9880. TBL_PC *sd;
  9881. int i, j, k, id = 1;
  9882. int index, flag;
  9883. int ret = -1;
  9884. //Original hash to reverse it when full check fails.
  9885. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  9886. sd = script_rid2sd(st);
  9887. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9888. script_pushint(st,0);
  9889. return 0;
  9890. }
  9891. setitem_hash = sd->setitem_hash;
  9892. setitem_hash2 = sd->setitem_hash2;
  9893. for (i=0; id!=0; i++)
  9894. {
  9895. FETCH (i+2, id) else id = 0;
  9896. if (id <= 0)
  9897. continue;
  9898. flag = 0;
  9899. for (j=0; j<EQI_MAX; j++)
  9900. {
  9901. index = sd->equip_index[j];
  9902. if(index < 0) continue;
  9903. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9904. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9905. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9906. if(!sd->inventory_data[index])
  9907. continue;
  9908. if (itemdb_type(id) != IT_CARD) {
  9909. if (sd->inventory_data[index]->nameid != id)
  9910. continue;
  9911. flag = 1;
  9912. break;
  9913. } else { //Cards
  9914. if (sd->inventory_data[index]->slot == 0 ||
  9915. itemdb_isspecial(sd->status.inventory[index].card[0]))
  9916. continue;
  9917. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  9918. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  9919. unsigned int hash = 0;
  9920. if (sd->status.inventory[index].card[k] != id)
  9921. continue;
  9922. hash = 1<<((j<5?j:j-5)*4 + k);
  9923. // check if card is already used by another set
  9924. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  9925. continue;
  9926. // We have found a match
  9927. flag = 1;
  9928. // Set hash so this card cannot be used by another
  9929. if (j<5)
  9930. sd->setitem_hash |= hash;
  9931. else
  9932. sd->setitem_hash2 |= hash;
  9933. break;
  9934. }
  9935. }
  9936. if (flag) break; //Card found
  9937. }
  9938. if (ret == -1)
  9939. ret = flag;
  9940. else
  9941. ret &= flag;
  9942. if (!ret) break;
  9943. }
  9944. if (!ret)
  9945. { //When check fails, restore original hash values. [Skotlex]
  9946. sd->setitem_hash = setitem_hash;
  9947. sd->setitem_hash2 = setitem_hash2;
  9948. }
  9949. script_pushint(st,ret);
  9950. return 0;
  9951. }
  9952. /*================================================
  9953. * Check how many given inserted cards in the CURRENT
  9954. * weapon - used for 2/15's cards patch [Lupus]
  9955. *------------------------------------------------*/
  9956. BUILDIN_FUNC(cardscnt)
  9957. {
  9958. TBL_PC *sd;
  9959. int i, k, id = 1;
  9960. int ret = 0;
  9961. int index;
  9962. sd = script_rid2sd(st);
  9963. for (i=0; id!=0; i++) {
  9964. FETCH (i+2, id) else id = 0;
  9965. if (id <= 0)
  9966. continue;
  9967. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  9968. if(index < 0) continue;
  9969. if(!sd->inventory_data[index])
  9970. continue;
  9971. if(itemdb_type(id) != IT_CARD) {
  9972. if (sd->inventory_data[index]->nameid == id)
  9973. ret+= sd->status.inventory[index].amount;
  9974. } else {
  9975. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9976. continue;
  9977. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9978. if (sd->status.inventory[index].card[k] == id)
  9979. ret++;
  9980. }
  9981. }
  9982. }
  9983. script_pushint(st,ret);
  9984. // script_pushint(st,current_equip_item_index);
  9985. return 0;
  9986. }
  9987. /*=======================================================
  9988. * Returns the refined number of the current item, or an
  9989. * item with inventory index specified
  9990. *-------------------------------------------------------*/
  9991. BUILDIN_FUNC(getrefine)
  9992. {
  9993. TBL_PC *sd;
  9994. if ((sd = script_rid2sd(st))!= NULL)
  9995. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  9996. else
  9997. script_pushint(st,0);
  9998. return 0;
  9999. }
  10000. /*=======================================================
  10001. * Allows 2 Parents to adopt a character as a Baby
  10002. *-------------------------------------------------------*/
  10003. BUILDIN_FUNC(adopt)
  10004. {
  10005. int ret;
  10006. const char *parent1 = script_getstr(st,2);
  10007. const char *parent2 = script_getstr(st,3);
  10008. const char *child = script_getstr(st,4);
  10009. TBL_PC *p1_sd = map_nick2sd(parent1);
  10010. TBL_PC *p2_sd = map_nick2sd(parent2);
  10011. TBL_PC *c_sd = map_nick2sd(child);
  10012. if (!p1_sd || !p2_sd || !c_sd ||
  10013. p1_sd->status.base_level < 70 ||
  10014. p2_sd->status.base_level < 70)
  10015. return 0;
  10016. ret = pc_adoption(p1_sd, p2_sd, c_sd);
  10017. script_pushint(st,ret);
  10018. return 0;
  10019. }
  10020. /*=======================================================
  10021. * Day/Night controls
  10022. *-------------------------------------------------------*/
  10023. BUILDIN_FUNC(night)
  10024. {
  10025. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10026. return 0;
  10027. }
  10028. BUILDIN_FUNC(day)
  10029. {
  10030. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10031. return 0;
  10032. }
  10033. //=======================================================
  10034. // Unequip [Spectre]
  10035. //-------------------------------------------------------
  10036. BUILDIN_FUNC(unequip)
  10037. {
  10038. int i;
  10039. size_t num;
  10040. TBL_PC *sd;
  10041. num = script_getnum(st,2);
  10042. sd = script_rid2sd(st);
  10043. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10044. {
  10045. i = pc_checkequip(sd,equip[num-1]);
  10046. if (i >= 0)
  10047. pc_unequipitem(sd,i,2);
  10048. return 0;
  10049. }
  10050. return 0;
  10051. }
  10052. BUILDIN_FUNC(equip)
  10053. {
  10054. int nameid=0,i;
  10055. TBL_PC *sd;
  10056. struct item_data *item_data;
  10057. sd = script_rid2sd(st);
  10058. nameid=script_getnum(st,2);
  10059. if((item_data = itemdb_exists(nameid)) == NULL)
  10060. {
  10061. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10062. return 1;
  10063. }
  10064. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10065. if( i < MAX_INVENTORY )
  10066. pc_equipitem(sd,i,item_data->equip);
  10067. return 0;
  10068. }
  10069. BUILDIN_FUNC(autoequip)
  10070. {
  10071. int nameid, flag;
  10072. struct item_data *item_data;
  10073. nameid=script_getnum(st,2);
  10074. flag=script_getnum(st,3);
  10075. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10076. item_data->flag.autoequip = flag>0?1:0;
  10077. }
  10078. return 0;
  10079. }
  10080. BUILDIN_FUNC(setbattleflag)
  10081. {
  10082. const char *flag, *value;
  10083. flag = script_getstr(st,2);
  10084. value = script_getstr(st,3);
  10085. if (battle_set_value(flag, value) == 0)
  10086. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10087. else
  10088. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10089. return 0;
  10090. }
  10091. BUILDIN_FUNC(getbattleflag)
  10092. {
  10093. const char *flag;
  10094. flag = script_getstr(st,2);
  10095. script_pushint(st,battle_get_value(flag));
  10096. return 0;
  10097. }
  10098. //=======================================================
  10099. // strlen [Valaris]
  10100. //-------------------------------------------------------
  10101. BUILDIN_FUNC(getstrlen)
  10102. {
  10103. const char *str = script_getstr(st,2);
  10104. int len = (str) ? (int)strlen(str) : 0;
  10105. script_pushint(st,len);
  10106. return 0;
  10107. }
  10108. //=======================================================
  10109. // isalpha [Valaris]
  10110. //-------------------------------------------------------
  10111. BUILDIN_FUNC(charisalpha)
  10112. {
  10113. const char *str=script_getstr(st,2);
  10114. int pos=script_getnum(st,3);
  10115. int val = ( str && pos>0 && (unsigned int)pos<strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10116. script_pushint(st,val);
  10117. return 0;
  10118. }
  10119. // [Lance]
  10120. BUILDIN_FUNC(fakenpcname)
  10121. {
  10122. const char *name;
  10123. const char *newname;
  10124. int look;
  10125. name = script_getstr(st,2);
  10126. newname = script_getstr(st,3);
  10127. look = script_getnum(st,4);
  10128. if(look > 32767 || look < -32768) {
  10129. ShowError("buildin_fakenpcname: Invalid look value %d\n",look);
  10130. return 1; // Safety measure to prevent runtime errors
  10131. }
  10132. npc_changename(name,newname,(short)look);
  10133. return 0;
  10134. }
  10135. BUILDIN_FUNC(atoi)
  10136. {
  10137. const char *value;
  10138. value = script_getstr(st,2);
  10139. script_pushint(st,atoi(value));
  10140. return 0;
  10141. }
  10142. // case-insensitive substring search [lordalfa]
  10143. BUILDIN_FUNC(compare)
  10144. {
  10145. const char *message;
  10146. const char *cmpstring;
  10147. message = script_getstr(st,2);
  10148. cmpstring = script_getstr(st,3);
  10149. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10150. return 0;
  10151. }
  10152. // [zBuffer] List of mathematics commands --->
  10153. BUILDIN_FUNC(sqrt)
  10154. {
  10155. double i, a;
  10156. i = script_getnum(st,2);
  10157. a = sqrt(i);
  10158. script_pushint(st,(int)a);
  10159. return 0;
  10160. }
  10161. BUILDIN_FUNC(pow)
  10162. {
  10163. double i, a, b;
  10164. a = script_getnum(st,2);
  10165. b = script_getnum(st,3);
  10166. i = pow(a,b);
  10167. script_pushint(st,(int)i);
  10168. return 0;
  10169. }
  10170. BUILDIN_FUNC(distance)
  10171. {
  10172. int x0, y0, x1, y1;
  10173. x0 = script_getnum(st,2);
  10174. y0 = script_getnum(st,3);
  10175. x1 = script_getnum(st,4);
  10176. y1 = script_getnum(st,5);
  10177. script_pushint(st,distance(x0-x1, y0-y1));
  10178. return 0;
  10179. }
  10180. BUILDIN_FUNC(checkcell)
  10181. {
  10182. int m;
  10183. const char *map = script_getstr(st, 2);
  10184. m = mapindex_name2id(map);
  10185. if(m){
  10186. script_pushint(st,map_getcell(m, script_getnum(st,3), script_getnum(st,4),script_getnum(st,5)));
  10187. } else {
  10188. script_pushint(st,0);
  10189. }
  10190. return 0;
  10191. }
  10192. // <--- [zBuffer] List of mathematics commands
  10193. // [zBuffer] List of dynamic var commands --->
  10194. //FIXME: some other functions are using this private function
  10195. void setd_sub(struct script_state *st, TBL_PC *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10196. {
  10197. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10198. return;
  10199. }
  10200. BUILDIN_FUNC(setd)
  10201. {
  10202. TBL_PC *sd=NULL;
  10203. char varname[100];
  10204. const char *value, *buffer;
  10205. int elem;
  10206. buffer = script_getstr(st, 2);
  10207. value = script_getstr(st, 3);
  10208. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10209. elem = 0;
  10210. if(st->rid)
  10211. sd = script_rid2sd(st);
  10212. if(varname[strlen(varname)-1] != '$') {
  10213. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10214. } else {
  10215. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10216. }
  10217. return 0;
  10218. }
  10219. BUILDIN_FUNC(query_sql)
  10220. {
  10221. #ifndef TXT_ONLY
  10222. int i, j;
  10223. TBL_PC* sd = NULL;
  10224. const char* query;
  10225. struct script_data* data;
  10226. char* name;
  10227. int max_rows = 128;// maximum number of rows
  10228. int num_vars;
  10229. int num_cols;
  10230. // check target variables
  10231. for( i = 3; script_hasdata(st,i); ++i )
  10232. {
  10233. data = script_getdata(st, i);
  10234. if( data_isreference(data) && reference_tovariable(data) )
  10235. {// it's a variable
  10236. name = reference_getname(data);
  10237. if( not_server_variable(*name) && sd == NULL )
  10238. {// requires a player
  10239. sd = script_rid2sd(st);
  10240. if( sd == NULL )
  10241. {// no player attached
  10242. script_reportdata(data);
  10243. st->state = END;
  10244. return 1;
  10245. }
  10246. }
  10247. if( not_array_variable(*name) )
  10248. max_rows = 1;// not an array, limit to one row
  10249. }
  10250. else
  10251. {
  10252. ShowError("script:query_sql: not a variable\n");
  10253. script_reportdata(data);
  10254. st->state = END;
  10255. return 1;
  10256. }
  10257. }
  10258. num_vars = i - 3;
  10259. // Execute the query
  10260. query = script_getstr(st,2);
  10261. if( SQL_ERROR == Sql_Query(mmysql_handle, query) )
  10262. {
  10263. Sql_ShowDebug(mmysql_handle);
  10264. script_pushint(st, 0);
  10265. return 1;
  10266. }
  10267. if( Sql_NumRows(mmysql_handle) == 0 )
  10268. {// No data received
  10269. Sql_FreeResult(mmysql_handle);
  10270. script_pushint(st, 0);
  10271. return 0;
  10272. }
  10273. // Count the number of columns to store
  10274. num_cols = Sql_NumColumns(mmysql_handle);
  10275. if( num_vars < num_cols )
  10276. {
  10277. ShowWarning("script:query_sql: Too many columns, discarting last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10278. script_reportsrc(st);
  10279. }
  10280. else if( num_vars > num_cols )
  10281. {
  10282. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10283. script_reportsrc(st);
  10284. }
  10285. // Store data
  10286. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(mmysql_handle); ++i )
  10287. {
  10288. for( j = 0; j < num_vars; ++j )
  10289. {
  10290. char* str = NULL;
  10291. if( j < num_cols )
  10292. Sql_GetData(mmysql_handle, j, &str, NULL);
  10293. data = script_getdata(st, j+3);
  10294. name = reference_getname(data);
  10295. if( is_string_variable(name) )
  10296. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10297. else
  10298. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10299. }
  10300. }
  10301. if( i == max_rows && max_rows < Sql_NumRows(mmysql_handle) )
  10302. {
  10303. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(mmysql_handle));
  10304. script_reportsrc(st);
  10305. }
  10306. // Free data
  10307. Sql_FreeResult(mmysql_handle);
  10308. script_pushint(st, i);
  10309. #else
  10310. //for TXT version, we always return -1
  10311. script_pushint(st,-1);
  10312. #endif
  10313. return 0;
  10314. }
  10315. //Allows escaping of a given string.
  10316. BUILDIN_FUNC(escape_sql)
  10317. {
  10318. const char *str;
  10319. char *esc_str;
  10320. size_t len;
  10321. str = script_getstr(st,2);
  10322. len = strlen(str);
  10323. esc_str = aMallocA(len*2+1);
  10324. #if defined(TXT_ONLY)
  10325. jstrescapecpy(esc_str, str);
  10326. #else
  10327. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10328. #endif
  10329. script_pushstr(st, esc_str);
  10330. return 0;
  10331. }
  10332. BUILDIN_FUNC(getd)
  10333. {
  10334. char varname[100];
  10335. const char *buffer;
  10336. int elem;
  10337. buffer = script_getstr(st, 2);
  10338. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10339. elem = 0;
  10340. // Push the 'pointer' so it's more flexible [Lance]
  10341. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10342. return 0;
  10343. }
  10344. // <--- [zBuffer] List of dynamic var commands
  10345. // Pet stat [Lance]
  10346. BUILDIN_FUNC(petstat)
  10347. {
  10348. TBL_PC *sd = NULL;
  10349. struct pet_data *pd;
  10350. int flag = script_getnum(st,2);
  10351. sd = script_rid2sd(st);
  10352. if(!sd || !sd->status.pet_id || !sd->pd){
  10353. if(flag == 2)
  10354. script_pushconststr(st, "");
  10355. else
  10356. script_pushint(st,0);
  10357. return 0;
  10358. }
  10359. pd = sd->pd;
  10360. switch(flag){
  10361. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10362. case 2: script_pushstr(st, aStrdup(pd->pet.name)); break;
  10363. case 3: script_pushint(st,(int)pd->pet.level); break;
  10364. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10365. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10366. default:
  10367. script_pushint(st,0);
  10368. break;
  10369. }
  10370. return 0;
  10371. }
  10372. BUILDIN_FUNC(callshop)
  10373. {
  10374. TBL_PC *sd = NULL;
  10375. struct npc_data *nd;
  10376. const char *shopname;
  10377. int flag = 0;
  10378. sd = script_rid2sd(st);
  10379. if (!sd) {
  10380. script_pushint(st,0);
  10381. return 0;
  10382. }
  10383. shopname = script_getstr(st, 2);
  10384. if( script_hasdata(st,3) )
  10385. flag = script_getnum(st,3);
  10386. nd = npc_name2id(shopname);
  10387. if (!nd || nd->bl.type!=BL_NPC || nd->bl.subtype!=SHOP) {
  10388. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10389. script_pushint(st,0);
  10390. return 1;
  10391. }
  10392. switch (flag) {
  10393. case 1: //Buy window
  10394. npc_buysellsel(sd,nd->bl.id,0);
  10395. break;
  10396. case 2: //Sell window
  10397. npc_buysellsel(sd,nd->bl.id,1);
  10398. break;
  10399. default: //Show menu
  10400. clif_npcbuysell(sd,nd->bl.id);
  10401. break;
  10402. }
  10403. sd->npc_shopid = nd->bl.id;
  10404. script_pushint(st,1);
  10405. return 0;
  10406. }
  10407. #ifndef MAX_SHOPITEM
  10408. #define MAX_SHOPITEM 100
  10409. #endif
  10410. BUILDIN_FUNC(npcshopitem)
  10411. {
  10412. struct npc_data *nd= NULL;
  10413. int n = 0;
  10414. int i = 3;
  10415. int amount;
  10416. const char* npcname = script_getstr(st, 2);
  10417. nd = npc_name2id(npcname);
  10418. if(!nd || nd->bl.subtype!=SHOP)
  10419. { //Not found.
  10420. script_pushint(st,0);
  10421. return 0;
  10422. }
  10423. // st->end - 2 = nameid + value # ... / 2 = number of items ... + 1 to end it
  10424. amount = ((st->end-2)/2)+1;
  10425. //Reinsert as realloc could change the pointer address.
  10426. map_deliddb(&nd->bl);
  10427. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10428. sizeof(nd->u.shop_item[0]) * amount);
  10429. map_addiddb(&nd->bl);
  10430. // Reset sell list.
  10431. memset(nd->u.shop_item, 0, sizeof(nd->u.shop_item[0]) * amount);
  10432. n = 0;
  10433. while (script_hasdata(st,i)) {
  10434. nd->u.shop_item[n].nameid = script_getnum(st,i);
  10435. i++;
  10436. nd->u.shop_item[n].value = script_getnum(st,i);
  10437. i++;
  10438. n++;
  10439. }
  10440. return 0;
  10441. }
  10442. BUILDIN_FUNC(npcshopadditem)
  10443. {
  10444. struct npc_data *nd=NULL;
  10445. int n = 0;
  10446. int i = 3;
  10447. int amount;
  10448. const char* npcname = script_getstr(st, 2);
  10449. nd = npc_name2id(npcname);
  10450. if (!nd || nd->bl.subtype!=SHOP)
  10451. { //Not found.
  10452. script_pushint(st,0);
  10453. return 0;
  10454. }
  10455. amount = ((st->end-2)/2)+1;
  10456. while (nd->u.shop_item[n].nameid && n < MAX_SHOPITEM)
  10457. n++;
  10458. //Reinsert as realloc could change the pointer address.
  10459. map_deliddb(&nd->bl);
  10460. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10461. sizeof(nd->u.shop_item[0]) * (amount+n));
  10462. map_addiddb(&nd->bl);
  10463. while(script_hasdata(st,i)){
  10464. nd->u.shop_item[n].nameid = script_getnum(st,i);
  10465. i++;
  10466. nd->u.shop_item[n].value = script_getnum(st,i);
  10467. i++;
  10468. n++;
  10469. }
  10470. // Marks the last of our stuff..
  10471. nd->u.shop_item[n].value = 0;
  10472. nd->u.shop_item[n].nameid = 0;
  10473. script_pushint(st,1);
  10474. return 0;
  10475. }
  10476. BUILDIN_FUNC(npcshopdelitem)
  10477. {
  10478. struct npc_data *nd=NULL;
  10479. int n=0;
  10480. int i=3;
  10481. int size = 0;
  10482. const char* npcname = script_getstr(st, 2);
  10483. nd = npc_name2id(npcname);
  10484. if (!nd || nd->bl.subtype!=SHOP)
  10485. { //Not found.
  10486. script_pushint(st,0);
  10487. return 0;
  10488. }
  10489. while (nd->u.shop_item[size].nameid)
  10490. size++;
  10491. while (script_hasdata(st,i)) {
  10492. for(n=0;nd->u.shop_item[n].nameid && n < MAX_SHOPITEM;n++) {
  10493. if (nd->u.shop_item[n].nameid == script_getnum(st,i)) {
  10494. // We're moving 1 extra empty block. Junk data is eliminated later.
  10495. memmove(&nd->u.shop_item[n], &nd->u.shop_item[n+1], sizeof(nd->u.shop_item[0])*(size-n));
  10496. }
  10497. }
  10498. i++;
  10499. }
  10500. size = 0;
  10501. while (nd->u.shop_item[size].nameid)
  10502. size++;
  10503. //Reinsert as realloc could change the pointer address.
  10504. map_deliddb(&nd->bl);
  10505. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10506. sizeof(nd->u.shop_item[0]) * (size+1));
  10507. map_addiddb(&nd->bl);
  10508. script_pushint(st,1);
  10509. return 0;
  10510. }
  10511. //Sets a script to attach to an npc.
  10512. BUILDIN_FUNC(npcshopattach)
  10513. {
  10514. struct npc_data *nd=NULL;
  10515. const char* npcname = script_getstr(st, 2);
  10516. int flag = 1;
  10517. if( script_hasdata(st,3) )
  10518. flag = script_getnum(st,3);
  10519. nd = npc_name2id(npcname);
  10520. if (!nd || nd->bl.subtype!=SHOP)
  10521. { //Not found.
  10522. script_pushint(st,0);
  10523. return 0;
  10524. }
  10525. if (flag)
  10526. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10527. else
  10528. nd->master_nd = NULL;
  10529. script_pushint(st,1);
  10530. return 0;
  10531. }
  10532. /*==========================================
  10533. * Returns some values of an item [Lupus]
  10534. * Price, Weight, etc...
  10535. setitemscript(itemID,"{new item bonus script}",[n]);
  10536. Where n:
  10537. 0 - script
  10538. 1 - Equip script
  10539. 2 - Unequip script
  10540. *------------------------------------------*/
  10541. BUILDIN_FUNC(setitemscript)
  10542. {
  10543. int item_id,n=0;
  10544. const char *script;
  10545. struct item_data *i_data;
  10546. struct script_code **dstscript;
  10547. item_id = script_getnum(st,2);
  10548. script = script_getstr(st,3);
  10549. if( script_hasdata(st,4) )
  10550. n=script_getnum(st,4);
  10551. i_data = itemdb_exists(item_id);
  10552. if (!i_data || script==NULL || script[0]!='{') {
  10553. script_pushint(st,0);
  10554. return 0;
  10555. }
  10556. switch (n) {
  10557. case 2:
  10558. dstscript = &i_data->unequip_script;
  10559. break;
  10560. case 1:
  10561. dstscript = &i_data->equip_script;
  10562. break;
  10563. default:
  10564. dstscript = &i_data->script;
  10565. break;
  10566. }
  10567. if(*dstscript)
  10568. script_free_code(*dstscript);
  10569. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  10570. script_pushint(st,1);
  10571. return 0;
  10572. }
  10573. /* Work In Progress [Lupus]
  10574. BUILDIN_FUNC(addmonsterdrop)
  10575. {
  10576. int class_,item_id,chance;
  10577. class_=script_getnum(st,2);
  10578. item_id=script_getnum(st,3);
  10579. chance=script_getnum(st,4);
  10580. if(class_>1000 && item_id>500 && chance>0) {
  10581. script_pushint(st,1);
  10582. } else {
  10583. script_pushint(st,0);
  10584. }
  10585. }
  10586. BUILDIN_FUNC(delmonsterdrop)
  10587. {
  10588. int class_,item_id;
  10589. class_=script_getnum(st,2);
  10590. item_id=script_getnum(st,3);
  10591. if(class_>1000 && item_id>500) {
  10592. script_pushint(st,1);
  10593. } else {
  10594. script_pushint(st,0);
  10595. }
  10596. }
  10597. */
  10598. /*==========================================
  10599. * Returns some values of a monster [Lupus]
  10600. * Name, Level, race, size, etc...
  10601. getmonsterinfo(monsterID,queryIndex);
  10602. *------------------------------------------*/
  10603. BUILDIN_FUNC(getmonsterinfo)
  10604. {
  10605. struct mob_db *mob;
  10606. int mob_id;
  10607. mob_id = script_getnum(st,2);
  10608. if (!mobdb_checkid(mob_id)) {
  10609. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i", mob_id);
  10610. script_pushint(st,-1);
  10611. return -1;
  10612. }
  10613. mob = mob_db(mob_id);
  10614. switch ( script_getnum(st,3) ) {
  10615. case 0: script_pushconststr(st,mob->jname); break;
  10616. case 1: script_pushint(st,mob->lv); break;
  10617. case 2: script_pushint(st,mob->status.max_hp); break;
  10618. case 3: script_pushint(st,mob->base_exp); break;
  10619. case 4: script_pushint(st,mob->job_exp); break;
  10620. case 5: script_pushint(st,mob->status.rhw.atk); break;
  10621. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  10622. case 7: script_pushint(st,mob->status.def); break;
  10623. case 8: script_pushint(st,mob->status.mdef); break;
  10624. case 9: script_pushint(st,mob->status.str); break;
  10625. case 10: script_pushint(st,mob->status.agi); break;
  10626. case 11: script_pushint(st,mob->status.vit); break;
  10627. case 12: script_pushint(st,mob->status.int_); break;
  10628. case 13: script_pushint(st,mob->status.dex); break;
  10629. case 14: script_pushint(st,mob->status.luk); break;
  10630. case 15: script_pushint(st,mob->status.rhw.range); break;
  10631. case 16: script_pushint(st,mob->range2); break;
  10632. case 17: script_pushint(st,mob->range3); break;
  10633. case 18: script_pushint(st,mob->status.size); break;
  10634. case 19: script_pushint(st,mob->status.race); break;
  10635. case 20: script_pushint(st,mob->status.def_ele); break;
  10636. case 21: script_pushint(st,mob->status.mode); break;
  10637. default: script_pushint(st,-1); //wrong Index
  10638. }
  10639. return 0;
  10640. }
  10641. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  10642. {
  10643. TBL_PC *sd = NULL;
  10644. if(script_hasdata(st,2))
  10645. sd = map_nick2sd(script_getstr(st,2));
  10646. else
  10647. sd = script_rid2sd(st);
  10648. if(sd)
  10649. script_pushint(st,(sd->vender_id != 0));
  10650. else
  10651. script_pushint(st,0);
  10652. return 0;
  10653. }
  10654. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  10655. {
  10656. TBL_PC *sd = NULL;
  10657. if(script_hasdata(st,2))
  10658. sd = map_nick2sd(script_getstr(st,2));
  10659. else
  10660. sd = script_rid2sd(st);
  10661. if(sd)
  10662. script_pushint(st,(sd->chatID != 0));
  10663. else
  10664. script_pushint(st,0);
  10665. return 0;
  10666. }
  10667. int axtoi(const char *hexStg)
  10668. {
  10669. int n = 0; // position in string
  10670. int m = 0; // position in digit[] to shift
  10671. int count; // loop index
  10672. int intValue = 0; // integer value of hex string
  10673. int digit[11]; // hold values to convert
  10674. while (n < 10) {
  10675. if (hexStg[n]=='\0')
  10676. break;
  10677. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  10678. digit[n] = hexStg[n] & 0x0f; //convert to int
  10679. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  10680. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10681. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  10682. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10683. else break;
  10684. n++;
  10685. }
  10686. count = n;
  10687. m = n - 1;
  10688. n = 0;
  10689. while(n < count) {
  10690. // digit[n] is value of hex digit at position n
  10691. // (m << 2) is the number of positions to shift
  10692. // OR the bits into return value
  10693. intValue = intValue | (digit[n] << (m << 2));
  10694. m--; // adjust the position to set
  10695. n++; // next digit to process
  10696. }
  10697. return (intValue);
  10698. }
  10699. // [Lance] Hex string to integer converter
  10700. BUILDIN_FUNC(axtoi)
  10701. {
  10702. const char *hex = script_getstr(st,2);
  10703. script_pushint(st,axtoi(hex));
  10704. return 0;
  10705. }
  10706. // [zBuffer] List of player cont commands --->
  10707. BUILDIN_FUNC(rid2name)
  10708. {
  10709. struct block_list *bl = NULL;
  10710. int rid = script_getnum(st,2);
  10711. if((bl = map_id2bl(rid)))
  10712. {
  10713. switch(bl->type) {
  10714. case BL_MOB: script_pushconststr(st,((TBL_MOB*)bl)->name); break;
  10715. case BL_PC: script_pushconststr(st,((TBL_PC*)bl)->status.name); break;
  10716. case BL_NPC: script_pushconststr(st,((TBL_NPC*)bl)->exname); break;
  10717. case BL_PET: script_pushconststr(st,((TBL_PET*)bl)->pet.name); break;
  10718. case BL_HOM: script_pushconststr(st,((TBL_HOM*)bl)->homunculus.name); break;
  10719. default:
  10720. ShowError("buildin_rid2name: BL type unknown.\n");
  10721. script_pushconststr(st,"");
  10722. break;
  10723. }
  10724. } else {
  10725. ShowError("buildin_rid2name: invalid RID\n");
  10726. script_pushconststr(st,"(null)");
  10727. }
  10728. return 0;
  10729. }
  10730. BUILDIN_FUNC(pcblockmove)
  10731. {
  10732. int id, flag;
  10733. TBL_PC *sd = NULL;
  10734. id = script_getnum(st,2);
  10735. flag = script_getnum(st,3);
  10736. if(id)
  10737. sd = map_id2sd(id);
  10738. else
  10739. sd = script_rid2sd(st);
  10740. if(sd)
  10741. sd->state.blockedmove = flag > 0;
  10742. return 0;
  10743. }
  10744. BUILDIN_FUNC(pcfollow)
  10745. {
  10746. int id, targetid;
  10747. TBL_PC *sd = NULL;
  10748. id = script_getnum(st,2);
  10749. targetid = script_getnum(st,3);
  10750. if(id)
  10751. sd = map_id2sd(id);
  10752. else
  10753. sd = script_rid2sd(st);
  10754. if(sd)
  10755. pc_follow(sd, targetid);
  10756. return 0;
  10757. }
  10758. BUILDIN_FUNC(pcstopfollow)
  10759. {
  10760. int id;
  10761. TBL_PC *sd = NULL;
  10762. id = script_getnum(st,2);
  10763. if(id)
  10764. sd = map_id2sd(id);
  10765. else
  10766. sd = script_rid2sd(st);
  10767. if(sd)
  10768. pc_stop_following(sd);
  10769. return 0;
  10770. }
  10771. // <--- [zBuffer] List of player cont commands
  10772. // [zBuffer] List of mob control commands --->
  10773. //## TODO always return if the request/whatever was successfull [FlavioJS]
  10774. BUILDIN_FUNC(mobspawn)
  10775. {
  10776. int class_,x,y,id;
  10777. const char *str,*map;
  10778. // Who?
  10779. str =script_getstr(st,2);
  10780. // What?
  10781. class_ =script_getnum(st,3);
  10782. // Where?
  10783. map =script_getstr(st,4);
  10784. x =script_getnum(st,5);
  10785. y =script_getnum(st,6);
  10786. id = mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,1,"");
  10787. script_pushint(st,id);
  10788. return 0;
  10789. }
  10790. BUILDIN_FUNC(mobremove)
  10791. {
  10792. int id;
  10793. struct block_list *bl = NULL;
  10794. id = script_getnum(st,2);
  10795. bl = map_id2bl(id);
  10796. if (bl && bl->type == BL_MOB)
  10797. unit_free(bl,0);
  10798. return 0;
  10799. }
  10800. BUILDIN_FUNC(getmobdata)
  10801. {
  10802. int num, id;
  10803. char *name;
  10804. struct mob_data *md = NULL;
  10805. TBL_PC *sd = st->rid?map_id2sd(st->rid):NULL;
  10806. id = script_getnum(st,2);
  10807. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  10808. ShowWarning("buildin_getmobdata: Error in argument!\n");
  10809. return -1;
  10810. }
  10811. num=st->stack->stack_data[st->start+3].u.num;
  10812. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10813. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  10814. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  10815. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  10816. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  10817. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  10818. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  10819. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  10820. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  10821. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  10822. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  10823. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  10824. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  10825. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  10826. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  10827. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  10828. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  10829. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  10830. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  10831. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  10832. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  10833. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  10834. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  10835. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  10836. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  10837. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  10838. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  10839. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  10840. return 0;
  10841. }
  10842. /// Changes the data of a mob
  10843. ///
  10844. /// setmobdata <mob unit id>,<type>,<value>;
  10845. BUILDIN_FUNC(setmobdata)
  10846. {
  10847. struct block_list* mob_bl;
  10848. mob_bl = map_id2bl(script_getnum(st,2));
  10849. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  10850. {
  10851. TBL_MOB* md = (TBL_MOB*)mob_bl;
  10852. int type;
  10853. int value;
  10854. type = script_getnum(st,3);
  10855. value = script_getnum(st,4);
  10856. switch( type )
  10857. {
  10858. case 0: md->class_ = (short)value; break;
  10859. case 1: md->level = (unsigned short)value; break;
  10860. case 2: md->status.hp = (unsigned int)value; break;
  10861. case 3: md->status.max_hp = (unsigned int)value; break;
  10862. case 4: md->master_id = value; break;
  10863. case 5: md->bl.m = (short)value; break;
  10864. case 6: md->bl.x = (short)value; break;
  10865. case 7: md->bl.y = (short)value; break;
  10866. case 8: md->status.speed = (unsigned short)value; break;
  10867. case 9: md->status.mode = (unsigned short)value; break;
  10868. case 10: md->special_state.ai = (unsigned int)value; break;
  10869. case 11: md->sc.option = (unsigned short)value; break;
  10870. case 12: md->vd->sex = (char)value; break;
  10871. case 13: md->vd->class_ = (unsigned short)value; break;
  10872. case 14: md->vd->hair_style = (unsigned short)value; break;
  10873. case 15: md->vd->hair_color = (unsigned short)value; break;
  10874. case 16: md->vd->head_bottom = (unsigned short)value; break;
  10875. case 17: md->vd->head_mid = (unsigned short)value; break;
  10876. case 18: md->vd->head_top = (unsigned short)value; break;
  10877. case 19: md->vd->cloth_color = (unsigned short)value; break;
  10878. case 20: md->vd->shield = (unsigned short)value; break;
  10879. case 21: md->vd->weapon = (unsigned short)value; break;
  10880. case 22: md->vd->shield = (unsigned short)value; break;
  10881. case 23: md->ud.dir = (unsigned char)value; break;
  10882. case 24: md->state.killer = value > 0 ? 1 : 0; break;
  10883. case 25: md->callback_flag = (short)value; break;
  10884. case 26: md->state.no_random_walk = value > 0 ? 1 : 0; break;
  10885. default:
  10886. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  10887. return 1;
  10888. }
  10889. }
  10890. return 0;
  10891. }
  10892. /// Makes the mob assist the target unit as a slave
  10893. ///
  10894. /// mobassist <mob unit id>,"<player name>";
  10895. /// mobassist <mob unit id>,<target id>;
  10896. BUILDIN_FUNC(mobassist)
  10897. {
  10898. struct block_list* mob_bl;
  10899. // get mob
  10900. mob_bl = map_id2bl(script_getnum(st,2));
  10901. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  10902. {
  10903. TBL_MOB* md = (TBL_MOB*)mob_bl;
  10904. struct block_list* target_bl = NULL;
  10905. struct script_data* data;
  10906. // get target
  10907. data = script_getdata(st, 3);
  10908. get_val(st, data);
  10909. if( data_isstring(data) )
  10910. {
  10911. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  10912. if( sd != NULL )
  10913. target_bl = &sd->bl;
  10914. }
  10915. if( target_bl == NULL )
  10916. target_bl = map_id2bl(conv_num(st, data));
  10917. // set mob as slave
  10918. if( target_bl != NULL )
  10919. {
  10920. struct unit_data* ud;
  10921. md->master_id = target_bl->id;
  10922. md->state.killer = 1;
  10923. mob_convertslave(md);
  10924. ud = unit_bl2ud(mob_bl);
  10925. if( ud != NULL )
  10926. {
  10927. if( ud->target != 0 )
  10928. md->target_id = ud->target;
  10929. else if( ud->skilltarget != 0 )
  10930. md->target_id = ud->skilltarget;
  10931. if( md->target_id != 0 )
  10932. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  10933. }
  10934. }
  10935. }
  10936. return 0;
  10937. }
  10938. /// Attaches the current npc or the target npc to the mob unit
  10939. ///
  10940. /// mobattach <mob unit id>{,"<npc name>"};
  10941. BUILDIN_FUNC(mobattach)
  10942. {
  10943. struct block_list* mob_bl;
  10944. mob_bl = map_id2bl(script_getnum(st,2));
  10945. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  10946. {
  10947. TBL_MOB* md = (TBL_MOB*)mob_bl;
  10948. TBL_NPC* nd = NULL;
  10949. if( script_hasdata(st,3) )
  10950. nd = npc_name2id(script_getstr(st, 3));
  10951. else
  10952. {
  10953. struct block_list* npc_bl = map_id2bl(st->oid);
  10954. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  10955. nd = (TBL_NPC*)npc_bl;
  10956. }
  10957. if( nd != NULL )
  10958. md->nd = nd;
  10959. }
  10960. return 0;
  10961. }
  10962. /// Makes the unit walk to target position or map
  10963. /// Returns if it was successfull
  10964. ///
  10965. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  10966. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  10967. BUILDIN_FUNC(unitwalk)
  10968. {
  10969. struct block_list* bl;
  10970. bl = map_id2bl(script_getnum(st,2));
  10971. if( bl == NULL )
  10972. {
  10973. script_pushint(st, 0);
  10974. }
  10975. else if( script_hasdata(st,4) )
  10976. {
  10977. int x = script_getnum(st,3);
  10978. int y = script_getnum(st,4);
  10979. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  10980. }
  10981. else
  10982. {
  10983. int map_id = script_getnum(st,3);
  10984. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  10985. }
  10986. return 0;
  10987. }
  10988. /// Kills the unit
  10989. ///
  10990. /// unitkill <unit_id>;
  10991. BUILDIN_FUNC(unitkill)
  10992. {
  10993. struct block_list* bl = map_id2bl(script_getnum(st,2));
  10994. if( bl != NULL )
  10995. status_kill(bl);
  10996. return 0;
  10997. }
  10998. /// Warps the unit to the target position in the target map
  10999. /// Returns if it was successfull
  11000. ///
  11001. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11002. BUILDIN_FUNC(unitwarp)
  11003. {
  11004. int unit_id;
  11005. int map;
  11006. short x;
  11007. short y;
  11008. struct block_list* bl;
  11009. unit_id = script_getnum(st,2);
  11010. map = map_mapname2mapid(script_getstr(st, 3));
  11011. x = (short)script_getnum(st,4);
  11012. y = (short)script_getnum(st,5);
  11013. bl = map_id2bl(unit_id);
  11014. if( map >= 0 && bl != NULL )
  11015. script_pushint(st, unit_warp(bl,map,x,y,0));
  11016. else
  11017. script_pushint(st, 0);
  11018. return 0;
  11019. }
  11020. /// Makes the unit attack the target.
  11021. /// If the unit is a player and <action type> is not 0, it does a continuous
  11022. /// attack instead of a single attack.
  11023. /// Returns if the request was successfull.
  11024. ///
  11025. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11026. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11027. BUILDIN_FUNC(unitattack)
  11028. {
  11029. struct block_list* unit_bl;
  11030. struct block_list* target_bl = NULL;
  11031. struct script_data* data;
  11032. int actiontype = 0;
  11033. // get unit
  11034. unit_bl = map_id2bl(script_getnum(st,2));
  11035. if( unit_bl == NULL ) {
  11036. script_pushint(st, 0);
  11037. return 0;
  11038. }
  11039. data = script_getdata(st, 3);
  11040. get_val(st, data);
  11041. if( data_isstring(data) )
  11042. {
  11043. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11044. if( sd != NULL )
  11045. target_bl = &sd->bl;
  11046. } else
  11047. target_bl = map_id2bl(conv_num(st, data));
  11048. // request the attack
  11049. if( target_bl == NULL )
  11050. {
  11051. script_pushint(st, 0);
  11052. return 0;
  11053. }
  11054. // get actiontype
  11055. if( script_hasdata(st,4) )
  11056. actiontype = script_getnum(st,4);
  11057. switch( unit_bl->type )
  11058. {
  11059. case BL_PC:
  11060. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11061. script_pushint(st, 1);
  11062. return 0;
  11063. case BL_MOB:
  11064. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11065. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11066. break;
  11067. case BL_PET:
  11068. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11069. break;
  11070. default:
  11071. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11072. script_pushint(st, 0);
  11073. return 1;
  11074. }
  11075. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11076. return 0;
  11077. }
  11078. /// Makes the unit stop attacking and moving
  11079. ///
  11080. /// unitstop <unit_id>;
  11081. BUILDIN_FUNC(unitstop)
  11082. {
  11083. int unit_id;
  11084. struct block_list* bl;
  11085. unit_id = script_getnum(st,2);
  11086. bl = map_id2bl(unit_id);
  11087. if( bl != NULL )
  11088. {
  11089. unit_stop_attack(bl);
  11090. unit_stop_walking(bl,0);
  11091. if( bl->type == BL_MOB )
  11092. ((TBL_MOB*)bl)->target_id = 0;
  11093. }
  11094. return 0;
  11095. }
  11096. /// Makes the unit say the message
  11097. ///
  11098. /// unittalk <unit_id>,"<message>";
  11099. BUILDIN_FUNC(unittalk)
  11100. {
  11101. int unit_id;
  11102. const char* message;
  11103. struct block_list* bl;
  11104. unit_id = script_getnum(st,2);
  11105. message = script_getstr(st, 3);
  11106. bl = map_id2bl(unit_id);
  11107. if( bl != NULL )
  11108. {
  11109. struct StringBuf sbuf;
  11110. StringBuf_Init(&sbuf);
  11111. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11112. clif_message(bl, StringBuf_Value(&sbuf));
  11113. if( bl->type == BL_PC )
  11114. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11115. StringBuf_Destroy(&sbuf);
  11116. }
  11117. return 0;
  11118. }
  11119. /// Makes the unit do an emotion
  11120. ///
  11121. /// unitemote <unit_id>,<emotion>;
  11122. ///
  11123. /// @see e_* in const.txt
  11124. BUILDIN_FUNC(unitemote)
  11125. {
  11126. int unit_id;
  11127. int emotion;
  11128. struct block_list* bl;
  11129. unit_id = script_getnum(st,2);
  11130. emotion = script_getnum(st,3);
  11131. bl = map_id2bl(unit_id);
  11132. if( bl != NULL )
  11133. clif_emotion(bl, emotion);
  11134. return 0;
  11135. }
  11136. /// Makes the unit cast the skill on the target or self if no target is specified
  11137. ///
  11138. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11139. BUILDIN_FUNC(unitskilluseid)
  11140. {
  11141. int unit_id;
  11142. int skill_id;
  11143. int skill_lv;
  11144. int target_id;
  11145. struct block_list* bl;
  11146. unit_id = script_getnum(st,2);
  11147. skill_id = script_getnum(st,3);
  11148. skill_lv = script_getnum(st,4);
  11149. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11150. bl = map_id2bl(unit_id);
  11151. if( bl != NULL )
  11152. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11153. return 0;
  11154. }
  11155. /// Makes the unit cast the skill on the target position.
  11156. ///
  11157. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11158. BUILDIN_FUNC(unitskillusepos)
  11159. {
  11160. int unit_id;
  11161. int skill_id;
  11162. int skill_lv;
  11163. int skill_x;
  11164. int skill_y;
  11165. struct block_list* bl;
  11166. unit_id = script_getnum(st,2);
  11167. skill_id = script_getnum(st,3);
  11168. skill_lv = script_getnum(st,4);
  11169. skill_x = script_getnum(st,5);
  11170. skill_y = script_getnum(st,6);
  11171. bl = map_id2bl(unit_id);
  11172. if( bl != NULL )
  11173. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11174. return 0;
  11175. }
  11176. // <--- [zBuffer] List of mob control commands
  11177. /// Pauses the execution of the script, detaching the player
  11178. ///
  11179. /// sleep <mili seconds>;
  11180. BUILDIN_FUNC(sleep)
  11181. {
  11182. int ticks;
  11183. TBL_PC* sd;
  11184. ticks = script_getnum(st,2);
  11185. sd = map_id2sd(st->rid);
  11186. // detach the player
  11187. if( sd && sd->npc_id == st->oid )
  11188. {
  11189. sd->npc_id = 0;
  11190. }
  11191. st->rid = 0;
  11192. if( ticks <= 0 )
  11193. {// do nothing
  11194. }
  11195. else if( st->sleep.tick == 0 )
  11196. {// sleep for the target amount of time
  11197. st->state = RERUNLINE;
  11198. st->sleep.tick = ticks;
  11199. }
  11200. else
  11201. {// sleep time is over
  11202. st->sleep.tick = 0;
  11203. }
  11204. return 0;
  11205. }
  11206. /// Pauses the execution of the script, keeping the player attached
  11207. /// Returns if a player is still attached
  11208. ///
  11209. /// sleep2(<mili secconds>) -> <bool>
  11210. BUILDIN_FUNC(sleep2)
  11211. {
  11212. int ticks;
  11213. ticks = script_getnum(st,2);
  11214. if( ticks <= 0 )
  11215. {// do nothing
  11216. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11217. }
  11218. else if( !st->sleep.tick )
  11219. {// sleep for the target amount of time
  11220. st->state = RERUNLINE;
  11221. st->sleep.tick = ticks;
  11222. }
  11223. else
  11224. {// sleep time is over
  11225. st->sleep.tick = 0;
  11226. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11227. }
  11228. return 0;
  11229. }
  11230. /// Awakes all the sleep timers of the target npc
  11231. ///
  11232. /// awake "<npc name>";
  11233. BUILDIN_FUNC(awake)
  11234. {
  11235. struct npc_data* nd;
  11236. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11237. nd = npc_name2id(script_getstr(st, 2));
  11238. if( nd == NULL )
  11239. return 0;
  11240. while( node )
  11241. {
  11242. if( (int)node->key == nd->bl.id )
  11243. {// sleep timer for the npc
  11244. struct script_state* tst = (struct script_state*)node->data;
  11245. TBL_PC* sd = map_id2sd(tst->rid);
  11246. if( tst->sleep.timer == -1 )
  11247. {// already awake ???
  11248. node = node->next;
  11249. continue;
  11250. }
  11251. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11252. {// char not online anymore / another char of the same account is online - Cancel execution
  11253. tst->state = END;
  11254. tst->rid = 0;
  11255. }
  11256. delete_timer(tst->sleep.timer, run_script_timer);
  11257. node = script_erase_sleepdb(node);
  11258. tst->sleep.timer = -1;
  11259. tst->sleep.tick = 0;
  11260. run_script_main(tst);
  11261. }
  11262. else
  11263. {
  11264. node = node->next;
  11265. }
  11266. }
  11267. return 0;
  11268. }
  11269. /// Returns a reference to a variable of the target NPC.
  11270. /// Returns 0 if an error occurs.
  11271. ///
  11272. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11273. BUILDIN_FUNC(getvariableofnpc)
  11274. {
  11275. struct script_data* data;
  11276. char* name;
  11277. struct npc_data* nd;
  11278. data = script_getdata(st,2);
  11279. if( !data_isreference(data) )
  11280. {// Not a reference (aka varaible name)
  11281. ShowError("script:getvariableofnpc: not a variable\n");
  11282. script_reportdata(data);
  11283. script_pushnil(st);
  11284. st->state = END;
  11285. return 1;
  11286. }
  11287. name = reference_getname(data);
  11288. if( *name != '.' || name[1] == '@' )
  11289. {// not a npc variable
  11290. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11291. script_reportdata(data);
  11292. script_pushnil(st);
  11293. st->state = END;
  11294. return 1;
  11295. }
  11296. nd = npc_name2id(script_getstr(st,3));
  11297. if( nd == NULL || nd->bl.subtype != SCRIPT || nd->u.scr.script == NULL )
  11298. {// NPC not found or has no script
  11299. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11300. script_pushnil(st);
  11301. st->state = END;
  11302. return 1;
  11303. }
  11304. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11305. return 0;
  11306. }
  11307. /// Opens a warp portal.
  11308. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11309. ///
  11310. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11311. ///
  11312. /// @author blackhole89
  11313. BUILDIN_FUNC(warpportal)
  11314. {
  11315. int spx;
  11316. int spy;
  11317. unsigned short mapindex;
  11318. int tpx;
  11319. int tpy;
  11320. struct skill_unit_group* group;
  11321. struct block_list* bl;
  11322. bl = map_id2bl(st->oid);
  11323. if( bl == NULL )
  11324. {
  11325. ShowError("script:warpportal: npc is needed\n");
  11326. return 1;
  11327. }
  11328. spx = script_getnum(st,2);
  11329. spy = script_getnum(st,3);
  11330. mapindex = mapindex_name2id(script_getstr(st, 4));
  11331. tpx = script_getnum(st,5);
  11332. tpy = script_getnum(st,6);
  11333. if( mapindex == 0 )
  11334. return 0;// map not found
  11335. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 1);
  11336. if( group == NULL )
  11337. return 0;// failed
  11338. group->val2 = (tpx<<16) | tpy;
  11339. group->val3 = mapindex;
  11340. return 0;
  11341. }
  11342. BUILDIN_FUNC(openmail)
  11343. {
  11344. #ifndef TXT_ONLY
  11345. mail_openmail(script_rid2sd(st));
  11346. #endif
  11347. return 0;
  11348. }
  11349. // declarations that were supposed to be exported from npc_chat.c
  11350. #ifdef PCRE_SUPPORT
  11351. BUILDIN_FUNC(defpattern);
  11352. BUILDIN_FUNC(activatepset);
  11353. BUILDIN_FUNC(deactivatepset);
  11354. BUILDIN_FUNC(deletepset);
  11355. #endif
  11356. struct script_function buildin_func[] = {
  11357. // NPC interaction
  11358. BUILDIN_DEF(mes,"s"),
  11359. BUILDIN_DEF(next,""),
  11360. BUILDIN_DEF(close,""),
  11361. BUILDIN_DEF(close2,""),
  11362. BUILDIN_DEF(menu,"sl*"),
  11363. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  11364. BUILDIN_DEF(prompt,"s*"),
  11365. //
  11366. BUILDIN_DEF(goto,"l"),
  11367. BUILDIN_DEF(callsub,"i*"),
  11368. BUILDIN_DEF(callfunc,"s*"),
  11369. BUILDIN_DEF(return,"?"),
  11370. BUILDIN_DEF(getarg,"i?"),
  11371. BUILDIN_DEF(jobchange,"i*"),
  11372. BUILDIN_DEF(jobname,"i"),
  11373. BUILDIN_DEF(input,"v"),
  11374. BUILDIN_DEF(warp,"sii"),
  11375. BUILDIN_DEF(areawarp,"siiiisii"),
  11376. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  11377. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  11378. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  11379. BUILDIN_DEF(setlook,"ii"),
  11380. BUILDIN_DEF(set,"ii"),
  11381. BUILDIN_DEF(setarray,"rv*"),
  11382. BUILDIN_DEF(cleararray,"rvi"),
  11383. BUILDIN_DEF(copyarray,"rri"),
  11384. BUILDIN_DEF(getarraysize,"r"),
  11385. BUILDIN_DEF(deletearray,"r?"),
  11386. BUILDIN_DEF(getelementofarray,"ri"),
  11387. BUILDIN_DEF(getitem,"vi?"),
  11388. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  11389. BUILDIN_DEF(getnameditem,"is"),
  11390. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  11391. BUILDIN_DEF(makeitem,"iisii"),
  11392. BUILDIN_DEF(delitem,"ii"),
  11393. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  11394. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  11395. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  11396. BUILDIN_DEF(cutin,"si"),
  11397. BUILDIN_DEF(viewpoint,"iiiii"),
  11398. BUILDIN_DEF(heal,"ii"),
  11399. BUILDIN_DEF(itemheal,"ii"),
  11400. BUILDIN_DEF(percentheal,"ii"),
  11401. BUILDIN_DEF(rand,"i?"),
  11402. BUILDIN_DEF(countitem,"i"),
  11403. BUILDIN_DEF(countitem2,"iiiiiiii"),
  11404. BUILDIN_DEF(checkweight,"ii"),
  11405. BUILDIN_DEF(readparam,"i*"),
  11406. BUILDIN_DEF(getcharid,"i*"),
  11407. BUILDIN_DEF(getpartyname,"i"),
  11408. BUILDIN_DEF(getpartymember,"i*"),
  11409. BUILDIN_DEF(getpartyleader,"i?"),
  11410. BUILDIN_DEF(getguildname,"i"),
  11411. BUILDIN_DEF(getguildmaster,"i"),
  11412. BUILDIN_DEF(getguildmasterid,"i"),
  11413. BUILDIN_DEF(strcharinfo,"i"),
  11414. BUILDIN_DEF(strnpcinfo,"i"),
  11415. BUILDIN_DEF(getequipid,"i"),
  11416. BUILDIN_DEF(getequipname,"i"),
  11417. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  11418. BUILDIN_DEF(repair,"i"), // [Valaris]
  11419. BUILDIN_DEF(getequipisequiped,"i"),
  11420. BUILDIN_DEF(getequipisenableref,"i"),
  11421. BUILDIN_DEF(getequipisidentify,"i"),
  11422. BUILDIN_DEF(getequiprefinerycnt,"i"),
  11423. BUILDIN_DEF(getequipweaponlv,"i"),
  11424. BUILDIN_DEF(getequippercentrefinery,"i"),
  11425. BUILDIN_DEF(successrefitem,"i"),
  11426. BUILDIN_DEF(failedrefitem,"i"),
  11427. BUILDIN_DEF(statusup,"i"),
  11428. BUILDIN_DEF(statusup2,"ii"),
  11429. BUILDIN_DEF(bonus,"ii"),
  11430. BUILDIN_DEF2(bonus,"bonus2","iii"),
  11431. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  11432. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  11433. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  11434. BUILDIN_DEF(bonusautoscript,"si?"),
  11435. BUILDIN_DEF(bonusautoscript2,"si?"),
  11436. BUILDIN_DEF(skill,"ii?"),
  11437. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  11438. BUILDIN_DEF(guildskill,"ii"),
  11439. BUILDIN_DEF(getskilllv,"i"),
  11440. BUILDIN_DEF(getgdskilllv,"ii"),
  11441. BUILDIN_DEF(basicskillcheck,""),
  11442. BUILDIN_DEF(getgmlevel,""),
  11443. BUILDIN_DEF(end,""),
  11444. BUILDIN_DEF(checkoption,"i"),
  11445. BUILDIN_DEF(setoption,"i?"),
  11446. BUILDIN_DEF(setcart,"?"),
  11447. BUILDIN_DEF(checkcart,""),
  11448. BUILDIN_DEF(setfalcon,"?"),
  11449. BUILDIN_DEF(checkfalcon,""),
  11450. BUILDIN_DEF(setriding,"?"),
  11451. BUILDIN_DEF(checkriding,""),
  11452. BUILDIN_DEF2(savepoint,"save","sii"),
  11453. BUILDIN_DEF(savepoint,"sii"),
  11454. BUILDIN_DEF(gettimetick,"i"),
  11455. BUILDIN_DEF(gettime,"i"),
  11456. BUILDIN_DEF(gettimestr,"si"),
  11457. BUILDIN_DEF(openstorage,""),
  11458. BUILDIN_DEF(guildopenstorage,"*"),
  11459. BUILDIN_DEF(itemskill,"ii"),
  11460. BUILDIN_DEF(produce,"i"),
  11461. BUILDIN_DEF(monster,"siisii*"),
  11462. BUILDIN_DEF(areamonster,"siiiisii*"),
  11463. BUILDIN_DEF(killmonster,"ss"),
  11464. BUILDIN_DEF(killmonsterall,"s"),
  11465. BUILDIN_DEF(clone,"siisi*"),
  11466. BUILDIN_DEF(doevent,"s"),
  11467. BUILDIN_DEF(donpcevent,"s"),
  11468. BUILDIN_DEF(cmdothernpc,"ss"),
  11469. BUILDIN_DEF(addtimer,"is"),
  11470. BUILDIN_DEF(deltimer,"s"),
  11471. BUILDIN_DEF(addtimercount,"si"),
  11472. BUILDIN_DEF(initnpctimer,"??"),
  11473. BUILDIN_DEF(stopnpctimer,"??"),
  11474. BUILDIN_DEF(startnpctimer,"??"),
  11475. BUILDIN_DEF(setnpctimer,"i?"),
  11476. BUILDIN_DEF(getnpctimer,"i?"),
  11477. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  11478. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  11479. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  11480. BUILDIN_DEF(announce,"si*"),
  11481. BUILDIN_DEF(mapannounce,"ssi*"),
  11482. BUILDIN_DEF(areaannounce,"siiiisi*"),
  11483. BUILDIN_DEF(getusers,"i"),
  11484. BUILDIN_DEF(getmapguildusers,"si"),
  11485. BUILDIN_DEF(getmapusers,"s"),
  11486. BUILDIN_DEF(getareausers,"siiii"),
  11487. BUILDIN_DEF(getareadropitem,"siiiii"),
  11488. BUILDIN_DEF(enablenpc,"s"),
  11489. BUILDIN_DEF(disablenpc,"s"),
  11490. BUILDIN_DEF(hideoffnpc,"s"),
  11491. BUILDIN_DEF(hideonnpc,"s"),
  11492. BUILDIN_DEF(sc_start,"iii?"),
  11493. BUILDIN_DEF(sc_start2,"iiii?"),
  11494. BUILDIN_DEF(sc_start4,"iiiiii?"),
  11495. BUILDIN_DEF(sc_end,"i?"),
  11496. BUILDIN_DEF(getscrate,"ii*"),
  11497. BUILDIN_DEF(debugmes,"s"),
  11498. BUILDIN_DEF2(catchpet,"pet","i"),
  11499. BUILDIN_DEF2(birthpet,"bpet",""),
  11500. BUILDIN_DEF(resetlvl,"i"),
  11501. BUILDIN_DEF(resetstatus,""),
  11502. BUILDIN_DEF(resetskill,""),
  11503. BUILDIN_DEF(skillpointcount,""),
  11504. BUILDIN_DEF(changebase,"i"),
  11505. BUILDIN_DEF(changesex,""),
  11506. BUILDIN_DEF(waitingroom,"si??"),
  11507. BUILDIN_DEF(delwaitingroom,"?"),
  11508. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  11509. BUILDIN_DEF(enablewaitingroomevent,"?"),
  11510. BUILDIN_DEF(disablewaitingroomevent,"?"),
  11511. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  11512. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  11513. BUILDIN_DEF(getwaitingroomstate,"i?"),
  11514. BUILDIN_DEF(warpwaitingpc,"sii?"),
  11515. BUILDIN_DEF(attachrid,"i"),
  11516. BUILDIN_DEF(detachrid,""),
  11517. BUILDIN_DEF(isloggedin,"i?"),
  11518. BUILDIN_DEF(setmapflagnosave,"ssii"),
  11519. BUILDIN_DEF(setmapflag,"si*"),
  11520. BUILDIN_DEF(removemapflag,"si"),
  11521. BUILDIN_DEF(pvpon,"s"),
  11522. BUILDIN_DEF(pvpoff,"s"),
  11523. BUILDIN_DEF(gvgon,"s"),
  11524. BUILDIN_DEF(gvgoff,"s"),
  11525. BUILDIN_DEF(emotion,"i*"),
  11526. BUILDIN_DEF(maprespawnguildid,"sii"),
  11527. BUILDIN_DEF(agitstart,""), // <Agit>
  11528. BUILDIN_DEF(agitend,""),
  11529. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  11530. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  11531. BUILDIN_DEF(getcastlename,"s"),
  11532. BUILDIN_DEF(getcastledata,"si*"),
  11533. BUILDIN_DEF(setcastledata,"sii"),
  11534. BUILDIN_DEF(requestguildinfo,"i*"),
  11535. BUILDIN_DEF(getequipcardcnt,"i"),
  11536. BUILDIN_DEF(successremovecards,"i"),
  11537. BUILDIN_DEF(failedremovecards,"ii"),
  11538. BUILDIN_DEF(marriage,"s"),
  11539. BUILDIN_DEF2(wedding_effect,"wedding",""),
  11540. BUILDIN_DEF(divorce,""),
  11541. BUILDIN_DEF(ispartneron,""),
  11542. BUILDIN_DEF(getpartnerid,""),
  11543. BUILDIN_DEF(getchildid,""),
  11544. BUILDIN_DEF(getmotherid,""),
  11545. BUILDIN_DEF(getfatherid,""),
  11546. BUILDIN_DEF(warppartner,"sii"),
  11547. BUILDIN_DEF(getitemname,"i"),
  11548. BUILDIN_DEF(getitemslots,"i"),
  11549. BUILDIN_DEF(makepet,"i"),
  11550. BUILDIN_DEF(getexp,"ii"),
  11551. BUILDIN_DEF(getinventorylist,""),
  11552. BUILDIN_DEF(getskilllist,""),
  11553. BUILDIN_DEF(clearitem,""),
  11554. BUILDIN_DEF(classchange,"ii"),
  11555. BUILDIN_DEF(misceffect,"i"),
  11556. BUILDIN_DEF(soundeffect,"si"),
  11557. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  11558. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  11559. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  11560. BUILDIN_DEF(guardianinfo,"i"), // display guardian data [Valaris]
  11561. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  11562. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  11563. BUILDIN_DEF(petloot,"i"), // [Valaris]
  11564. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  11565. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  11566. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  11567. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  11568. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  11569. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  11570. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  11571. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  11572. BUILDIN_DEF(nude,""), // nude command [Valaris]
  11573. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  11574. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  11575. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  11576. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  11577. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  11578. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  11579. BUILDIN_DEF(mobcount,"ss"),
  11580. BUILDIN_DEF(getlook,"i"),
  11581. BUILDIN_DEF(getsavepoint,"i"),
  11582. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  11583. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  11584. BUILDIN_DEF(npcstop,""), // [Valaris]
  11585. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  11586. BUILDIN_DEF(checkoption1,"i"),
  11587. BUILDIN_DEF(checkoption2,"i"),
  11588. BUILDIN_DEF(guildgetexp,"i"),
  11589. BUILDIN_DEF(guildchangegm,"is"),
  11590. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  11591. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  11592. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  11593. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  11594. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  11595. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  11596. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  11597. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  11598. BUILDIN_DEF(adopt,"sss"), // allows 2 parents to adopt a child
  11599. BUILDIN_DEF(night,""), // sets the server to night time
  11600. BUILDIN_DEF(day,""), // sets the server to day time
  11601. #ifdef PCRE_SUPPORT
  11602. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  11603. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  11604. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  11605. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  11606. #endif
  11607. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  11608. BUILDIN_DEF(getusersname,"*"),
  11609. BUILDIN_DEF(recovery,""),
  11610. BUILDIN_DEF(getpetinfo,"i"),
  11611. BUILDIN_DEF(gethominfo,"i"),
  11612. BUILDIN_DEF(checkequipedcard,"i"),
  11613. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  11614. BUILDIN_DEF(globalmes,"s*"),
  11615. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  11616. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  11617. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  11618. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  11619. BUILDIN_DEF(fakenpcname,"ssi"), // [Lance]
  11620. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  11621. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  11622. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  11623. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  11624. // [zBuffer] List of mathematics commands --->
  11625. BUILDIN_DEF(sqrt,"i"),
  11626. BUILDIN_DEF(pow,"ii"),
  11627. BUILDIN_DEF(distance,"iiii"),
  11628. BUILDIN_DEF(checkcell,"siii"),
  11629. // <--- [zBuffer] List of mathematics commands
  11630. // [zBuffer] List of dynamic var commands --->
  11631. BUILDIN_DEF(getd,"*"),
  11632. BUILDIN_DEF(setd,"*"),
  11633. // <--- [zBuffer] List of dynamic var commands
  11634. BUILDIN_DEF(petstat,"i"),
  11635. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  11636. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  11637. BUILDIN_DEF(npcshopadditem,"sii*"),
  11638. BUILDIN_DEF(npcshopdelitem,"si*"),
  11639. BUILDIN_DEF(npcshopattach,"s?"),
  11640. BUILDIN_DEF(equip,"i"),
  11641. BUILDIN_DEF(autoequip,"ii"),
  11642. BUILDIN_DEF(setbattleflag,"ss"),
  11643. BUILDIN_DEF(getbattleflag,"s"),
  11644. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  11645. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  11646. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  11647. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  11648. BUILDIN_DEF(axtoi,"s"),
  11649. BUILDIN_DEF(query_sql,"s*"),
  11650. BUILDIN_DEF(escape_sql,"s"),
  11651. BUILDIN_DEF(atoi,"s"),
  11652. // [zBuffer] List of player cont commands --->
  11653. BUILDIN_DEF(rid2name,"i"),
  11654. BUILDIN_DEF(pcfollow,"ii"),
  11655. BUILDIN_DEF(pcstopfollow,"i"),
  11656. BUILDIN_DEF(pcblockmove,"ii"),
  11657. // <--- [zBuffer] List of player cont commands
  11658. // [zBuffer] List of mob control commands --->
  11659. BUILDIN_DEF(mobspawn,"*"),
  11660. BUILDIN_DEF(mobremove,"i"),
  11661. BUILDIN_DEF(getmobdata,"i*"),
  11662. BUILDIN_DEF(setmobdata,"iii"),
  11663. BUILDIN_DEF(mobassist,"i?"),
  11664. BUILDIN_DEF(mobattach,"i?"),
  11665. BUILDIN_DEF(unitwalk,"ii?"),
  11666. BUILDIN_DEF(unitkill,"i"),
  11667. BUILDIN_DEF(unitwarp,"isii"),
  11668. BUILDIN_DEF(unitattack,"iv?"),
  11669. BUILDIN_DEF(unitstop,"i"),
  11670. BUILDIN_DEF(unittalk,"is"),
  11671. BUILDIN_DEF(unitemote,"ii"),
  11672. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  11673. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  11674. // <--- [zBuffer] List of mob control commands
  11675. BUILDIN_DEF(sleep,"i"),
  11676. BUILDIN_DEF(sleep2,"i"),
  11677. BUILDIN_DEF(awake,"s"),
  11678. BUILDIN_DEF(getvariableofnpc,"rs"),
  11679. BUILDIN_DEF(warpportal,"iisii"),
  11680. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  11681. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  11682. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  11683. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  11684. BUILDIN_DEF(checkvending,"*"),
  11685. BUILDIN_DEF(checkchatting,"*"),
  11686. BUILDIN_DEF(openmail,""),
  11687. {NULL,NULL,NULL},
  11688. };