script.c 320 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_FUNCIN
  4. //#define DEBUG_DISP
  5. //#define DEBUG_DISASM
  6. //#define DEBUG_RUN
  7. #include "../common/cbasetypes.h"
  8. #include "../common/socket.h"
  9. #include "../common/timer.h"
  10. #include "../common/malloc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/utils.h"
  16. #include "map.h"
  17. #include "clif.h"
  18. #include "chrif.h"
  19. #include "itemdb.h"
  20. #include "pc.h"
  21. #include "status.h"
  22. #include "storage.h"
  23. #include "mob.h"
  24. #include "npc.h"
  25. #include "pet.h"
  26. #include "mercenary.h" //[orn]
  27. #include "intif.h"
  28. #include "skill.h"
  29. #include "chat.h"
  30. #include "battle.h"
  31. #include "party.h"
  32. #include "guild.h"
  33. #include "atcommand.h"
  34. #include "charcommand.h"
  35. #include "log.h"
  36. #include "unit.h"
  37. #include "irc.h"
  38. #include "pet.h"
  39. #include "script.h"
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <math.h>
  44. #ifndef WIN32
  45. #include <sys/time.h>
  46. #endif
  47. #include <time.h>
  48. #include <setjmp.h>
  49. //
  50. // struct script_state* st;
  51. //
  52. /// Returns the stack_data at the target index
  53. #define script_getdata(st,i) &((st)->stack->stack_data[(st)->start+(i)])
  54. /// Returns if the stack contains data at the target index
  55. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  56. /// Returns the index of the last data in the stack
  57. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  58. /// Pushes an int into the stack
  59. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  60. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  61. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  62. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  63. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  64. #define script_getref(st,val) ((st)->stack->stack_data[(st)->start+(val)].ref)
  65. //
  66. // struct script_data* data;
  67. //
  68. /// Returns if the script data is a string
  69. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  70. /// Returns if the script data is an int
  71. #define data_isint(data) ( (data)->type == C_INT )
  72. /// Returns if the script data is a reference
  73. #define data_isreference(data) ( (data)->type == C_NAME )
  74. /// Returns if the script data is a label
  75. #define data_islabel(data) ( (data)->type == C_POS )
  76. /// Returns if the script data is an internal script function label
  77. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  78. #define FETCH(n, t) \
  79. if( script_hasdata(st,n) ) \
  80. (t)=script_getnum(st,n);
  81. #define SCRIPT_BLOCK_SIZE 512
  82. enum { LABEL_NEXTLINE=1,LABEL_START };
  83. static unsigned char * script_buf = NULL;
  84. static int script_pos,script_size;
  85. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  86. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  87. #define GETSTRING(off) (str_buf+(off))
  88. static char *str_buf;
  89. static int str_pos,str_size;
  90. static struct str_data_struct {
  91. int type;
  92. int str;
  93. int backpatch;
  94. int label;
  95. int (*func)(struct script_state *st);
  96. int val;
  97. int next;
  98. } *str_data = NULL;
  99. int str_num=LABEL_START,str_data_size;
  100. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  101. #define SCRIPT_HASH_SIZE 1021
  102. int str_hash[SCRIPT_HASH_SIZE];
  103. //#define SCRIPT_HASH_DJB2
  104. //#define SCRIPT_HASH_SDBM
  105. //#define SCRIPT_HASH_ELF
  106. //#define SCRIPT_HASH_PJW
  107. static struct dbt *mapreg_db=NULL;
  108. static struct dbt *mapregstr_db=NULL;
  109. static int mapreg_dirty=-1;
  110. char mapreg_txt[256]="save/mapreg.txt";
  111. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  112. static struct dbt *scriptlabel_db=NULL;
  113. static struct dbt *userfunc_db=NULL;
  114. static int parse_options=0;
  115. struct dbt* script_get_label_db(){ return scriptlabel_db; }
  116. struct dbt* script_get_userfunc_db(){ return userfunc_db; }
  117. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  118. struct Script_Config script_config;
  119. static jmp_buf error_jump;
  120. static char* error_msg;
  121. static const char* error_pos;
  122. static int error_report; // if the error should produce output
  123. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  124. // [Eoe / jA 1080, 1081, 1094, 1164]
  125. enum curly_type {
  126. TYPE_NULL = 0,
  127. TYPE_IF,
  128. TYPE_SWITCH,
  129. TYPE_WHILE,
  130. TYPE_FOR,
  131. TYPE_DO,
  132. TYPE_USERFUNC,
  133. TYPE_ARGLIST // function argument list
  134. };
  135. #define ARGLIST_UNDEFINED 0
  136. #define ARGLIST_NO_PAREN 1
  137. #define ARGLIST_PAREN 2
  138. static struct {
  139. struct {
  140. int type;
  141. int index;
  142. int count;
  143. int flag;
  144. struct linkdb_node *case_label;
  145. } curly[256]; // 右カッコの情報
  146. int curly_count; // 右カッコの数
  147. int index; // スクリプト内で使用した構文の数
  148. } syntax;
  149. const char* parse_curly_close(const char* p);
  150. const char* parse_syntax_close(const char* p);
  151. const char* parse_syntax_close_sub(const char* p,int* flag);
  152. const char* parse_syntax(const char* p);
  153. static int parse_syntax_for_flag = 0;
  154. extern int current_equip_item_index; //for New CARS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  155. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  156. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  157. int potion_target=0;
  158. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  159. // [zBuffer] SQL Mapreg Saving/Loading Database Declaration
  160. char mapregsql_db[32] = "mapreg";
  161. char mapregsql_db_varname[32] = "varname";
  162. char mapregsql_db_index[32] = "index";
  163. char mapregsql_db_value[32] = "value";
  164. char tmp_sql[65535];
  165. // --------------------------------------------------------
  166. #endif
  167. int get_com(unsigned char *script,int *pos);
  168. int get_num(unsigned char *script,int *pos);
  169. extern struct script_function {
  170. int (*func)(struct script_state *st);
  171. const char *name;
  172. const char *arg;
  173. } buildin_func[];
  174. static struct linkdb_node *sleep_db;
  175. #define not_server_variable(prefix) (prefix != '$' && prefix != '.')
  176. /*==========================================
  177. * ローカルプロトタイプ宣言 (必要な物のみ)
  178. *------------------------------------------
  179. */
  180. const char* parse_subexpr(const char* p,int limit);
  181. void push_val(struct script_stack *stack,int type,int val);
  182. int run_func(struct script_state *st);
  183. int mapreg_setreg(int num,int val);
  184. int mapreg_setregstr(int num,const char *str);
  185. enum c_op {
  186. C_NOP,
  187. C_POS,
  188. C_INT, // number
  189. C_PARAM, // parameter variable (see pc_readparam/pc_setparam)
  190. C_FUNC, // buildin function call
  191. C_STR, // string (free'd automatically)
  192. C_CONSTSTR, // string (not free'd)
  193. C_ARG, // start of argument list
  194. C_NAME,
  195. C_EOL, // end of line (extra stack values are cleared)
  196. C_RETINFO,
  197. C_USERFUNC, // internal script function
  198. C_USERFUNC_POS, // internal script function label
  199. // operators
  200. C_OP3, // a ? b : c
  201. C_LOR, // a || b
  202. C_LAND, // a && b
  203. C_LE, // a <= b
  204. C_LT, // a < b
  205. C_GE, // a >= b
  206. C_GT, // a > b
  207. C_EQ, // a == b
  208. C_NE, // a != b
  209. C_XOR, // a ^ b
  210. C_OR, // a | b
  211. C_AND, // a & b
  212. C_ADD, // a + b
  213. C_SUB, // a - b
  214. C_MUL, // a * b
  215. C_DIV, // a / b
  216. C_MOD, // a % b
  217. C_NEG, // - a
  218. C_LNOT, // ! a
  219. C_NOT, // ~ a
  220. C_R_SHIFT, // a >> b
  221. C_L_SHIFT // a << b
  222. };
  223. enum {
  224. MF_NOMEMO, //0
  225. MF_NOTELEPORT,
  226. MF_NOSAVE,
  227. MF_NOBRANCH,
  228. MF_NOPENALTY,
  229. MF_NOZENYPENALTY,
  230. MF_PVP,
  231. MF_PVP_NOPARTY,
  232. MF_PVP_NOGUILD,
  233. MF_GVG,
  234. MF_GVG_NOPARTY, //10
  235. MF_NOTRADE,
  236. MF_NOSKILL,
  237. MF_NOWARP,
  238. MF_PARTYLOCK,
  239. MF_NOICEWALL,
  240. MF_SNOW,
  241. MF_FOG,
  242. MF_SAKURA,
  243. MF_LEAVES,
  244. MF_RAIN, //20
  245. MF_INDOORS,
  246. MF_NOGO,
  247. MF_CLOUDS,
  248. MF_CLOUDS2,
  249. MF_FIREWORKS,
  250. MF_GVG_CASTLE,
  251. MF_GVG_DUNGEON,
  252. MF_NIGHTENABLED,
  253. MF_NOBASEEXP,
  254. MF_NOJOBEXP, //30
  255. MF_NOMOBLOOT,
  256. MF_NOMVPLOOT,
  257. MF_NORETURN,
  258. MF_NOWARPTO,
  259. MF_NIGHTMAREDROP,
  260. MF_RESTRICTED,
  261. MF_NOCOMMAND,
  262. MF_NODROP,
  263. MF_JEXP,
  264. MF_BEXP, //40
  265. MF_NOVENDING,
  266. MF_LOADEVENT,
  267. MF_NOCHAT,
  268. MF_NOEXPPENALTY,
  269. MF_GUILDLOCK
  270. };
  271. //Reports on the console the src of a script error.
  272. static void report_src(struct script_state *st) {
  273. struct block_list *bl;
  274. if (!st->oid) return; //Can't report source.
  275. bl = map_id2bl(st->oid);
  276. if (!bl) return;
  277. switch (bl->type) {
  278. case BL_NPC:
  279. if (bl->m >=0)
  280. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  281. else
  282. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  283. break;
  284. default:
  285. if (bl->m >=0)
  286. 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);
  287. else
  288. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  289. break;
  290. }
  291. }
  292. /*==========================================
  293. * エラーメッセージ出力
  294. *------------------------------------------
  295. */
  296. static void disp_error_message2(const char *mes,const char *pos,int report)
  297. {
  298. error_msg = aStrdup(mes);
  299. error_pos = pos;
  300. error_report = report;
  301. longjmp( error_jump, 1 );
  302. }
  303. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  304. /// Checks event parameter validity
  305. static void check_event(struct script_state *st, const char *evt)
  306. {
  307. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  308. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  309. report_src(st);
  310. }
  311. }
  312. /*==========================================
  313. * 文字列のハッシュを計算
  314. *------------------------------------------
  315. */
  316. #define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE)
  317. static unsigned int calc_hash2(const unsigned char *p)
  318. {
  319. #if defined(SCRIPT_HASH_DJB2)
  320. unsigned int h = 5381;
  321. while( *p ) // hash*33 + c
  322. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  323. return h;
  324. #elif defined(SCRIPT_HASH_SDBM)
  325. unsigned int h = 0;
  326. while( *p )
  327. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  328. return h;
  329. #elif defined(SCRIPT_HASH_ELF)
  330. unsigned int h = 0;
  331. unsigned int g;
  332. while( *p ){ // UNIX ELF hash
  333. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  334. if ( g = h & 0xF0000000 )
  335. h ^= g >> 24;
  336. h &= ~g;
  337. }
  338. return h;
  339. #elif defined(SCRIPT_HASH_PJW)
  340. unsigned int h = 0;
  341. unsigned int g;
  342. while( *p ){
  343. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  344. if ( (g=h&0xF0000000) ) {
  345. h ^= g>>24;
  346. h ^= g;
  347. }
  348. }
  349. return h;
  350. #else
  351. unsigned int h = 0;
  352. while( *p ){
  353. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  354. h+=(unsigned char)TOLOWER(*p++);
  355. }
  356. return h;
  357. #endif
  358. }
  359. /*==========================================
  360. * str_dataの中に名前があるか検索する
  361. *------------------------------------------
  362. */
  363. // 既存のであれば番号、無ければ-1
  364. static int search_str(const char *p)
  365. {
  366. int i;
  367. i=str_hash[calc_hash(p)];
  368. while(i){
  369. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  370. return i;
  371. }
  372. i=str_data[i].next;
  373. }
  374. return -1;
  375. }
  376. /*==========================================
  377. * str_dataに名前を登録
  378. *------------------------------------------
  379. */
  380. // 既存のであれば番号、無ければ登録して新規番号
  381. int add_str(const char* p)
  382. {
  383. int i;
  384. int len;
  385. i=calc_hash(p);
  386. if(str_hash[i]==0){
  387. str_hash[i]=str_num;
  388. } else {
  389. i=str_hash[i];
  390. for(;;){
  391. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  392. return i;
  393. }
  394. if(str_data[i].next==0)
  395. break;
  396. i=str_data[i].next;
  397. }
  398. str_data[i].next=str_num;
  399. }
  400. if(str_num>=str_data_size){
  401. str_data_size+=128;
  402. RECREATE(str_data,struct str_data_struct,str_data_size);
  403. memset(str_data + (str_data_size - 128), '\0', 128);
  404. }
  405. len=(int)strlen(p);
  406. while(str_pos+len+1>=str_size){
  407. str_size+=256;
  408. RECREATE(str_buf,char,str_size);
  409. memset(str_buf + (str_size - 256), '\0', 256);
  410. }
  411. memcpy(str_buf+str_pos,p,len+1);
  412. str_data[str_num].type=C_NOP;
  413. str_data[str_num].str=str_pos;
  414. str_data[str_num].next=0;
  415. str_data[str_num].func=NULL;
  416. str_data[str_num].backpatch=-1;
  417. str_data[str_num].label=-1;
  418. str_pos+=len+1;
  419. return str_num++;
  420. }
  421. /*==========================================
  422. * スクリプトバッファサイズの確認と拡張
  423. *------------------------------------------
  424. */
  425. static void expand_script_buf(void)
  426. {
  427. script_size+=SCRIPT_BLOCK_SIZE;
  428. RECREATE(script_buf,unsigned char,script_size);
  429. }
  430. /*==========================================
  431. * スクリプトバッファに1バイト書き込む
  432. *------------------------------------------
  433. */
  434. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  435. #if 0
  436. static void add_scriptb(int a)
  437. {
  438. expand_script_buf();
  439. script_buf[script_pos++]=a;
  440. }
  441. #endif
  442. /*==========================================
  443. * スクリプトバッファにデータタイプを書き込む
  444. *------------------------------------------
  445. */
  446. static void add_scriptc(int a)
  447. {
  448. while(a>=0x40){
  449. add_scriptb((a&0x3f)|0x40);
  450. a=(a-0x40)>>6;
  451. }
  452. add_scriptb(a&0x3f);
  453. }
  454. /*==========================================
  455. * スクリプトバッファに整数を書き込む
  456. *------------------------------------------
  457. */
  458. static void add_scripti(int a)
  459. {
  460. while(a>=0x40){
  461. add_scriptb(a|0xc0);
  462. a=(a-0x40)>>6;
  463. }
  464. add_scriptb(a|0x80);
  465. }
  466. /*==========================================
  467. * スクリプトバッファにラベル/変数/関数を書き込む
  468. *------------------------------------------
  469. */
  470. // 最大16Mまで
  471. static void add_scriptl(int l)
  472. {
  473. int backpatch = str_data[l].backpatch;
  474. switch(str_data[l].type){
  475. case C_POS:
  476. case C_USERFUNC_POS:
  477. add_scriptc(C_POS);
  478. add_scriptb(str_data[l].label);
  479. add_scriptb(str_data[l].label>>8);
  480. add_scriptb(str_data[l].label>>16);
  481. break;
  482. case C_NOP:
  483. case C_USERFUNC:
  484. // ラベルの可能性があるのでbackpatch用データ埋め込み
  485. add_scriptc(C_NAME);
  486. str_data[l].backpatch=script_pos;
  487. add_scriptb(backpatch);
  488. add_scriptb(backpatch>>8);
  489. add_scriptb(backpatch>>16);
  490. break;
  491. case C_INT:
  492. add_scripti(abs(str_data[l].val));
  493. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  494. add_scriptc(C_NEG);
  495. break;
  496. default:
  497. // もう他の用途と確定してるので数字をそのまま
  498. add_scriptc(C_NAME);
  499. add_scriptb(l);
  500. add_scriptb(l>>8);
  501. add_scriptb(l>>16);
  502. break;
  503. }
  504. }
  505. /*==========================================
  506. * ラベルを解決する
  507. *------------------------------------------
  508. */
  509. void set_label(int l,int pos, const char* script_pos)
  510. {
  511. int i,next;
  512. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  513. { //Prevent overwriting constants values and parameters [Skotlex]
  514. disp_error_message("set_label: invalid label name",script_pos);
  515. return;
  516. }
  517. if(str_data[l].label!=-1){
  518. disp_error_message("set_label: dup label ",script_pos);
  519. return;
  520. }
  521. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  522. str_data[l].label=pos;
  523. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  524. next=GETVALUE(script_buf,i);
  525. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  526. SETVALUE(script_buf,i,pos);
  527. i=next;
  528. }
  529. }
  530. /// Skips spaces and/or comments.
  531. static
  532. const char* skip_space(const char* p)
  533. {
  534. for(;;)
  535. {
  536. while( ISSPACE(*p) )
  537. ++p;
  538. if( *p == '/' && p[1] == '/' )
  539. {// line comment
  540. while(*p && *p!='\n')
  541. ++p;
  542. }
  543. else if( *p == '/' && p[1] == '*' )
  544. {// block comment
  545. p += 2;
  546. for(;;)
  547. {
  548. if( *p == '\0' )
  549. disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  550. if( *p == '*' || p[1] == '/' )
  551. {// end of block comment
  552. p += 2;
  553. break;
  554. }
  555. ++p;
  556. }
  557. }
  558. else
  559. break;
  560. }
  561. return p;
  562. }
  563. /// Skips a word.
  564. /// A word consists of undercores and/or alfanumeric characters,
  565. /// and valid variable prefixes/postfixes.
  566. static
  567. const char* skip_word(const char* p)
  568. {
  569. // prefix
  570. switch( *p )
  571. {
  572. case '@':// temporary char variable
  573. ++p; break;
  574. case '#':// account variable
  575. p += ( p[1] == '#' ? 2 : 1 ); break;
  576. case '.':// npc variable
  577. p += ( p[1] == '@' ? 2 : 1 ); break;
  578. case '$':// global variable
  579. p += ( p[1] == '@' ? 2 : 1 ); break;
  580. }
  581. while( ISALNUM(*p) || *p == '_' )
  582. ++p;
  583. // postfix
  584. if( *p == '$' )// string
  585. p++;
  586. return p;
  587. }
  588. /// Adds a word to str_data.
  589. /// @see skip_word
  590. /// @see add_str
  591. static
  592. int add_word(const char* p)
  593. {
  594. char* word;
  595. int len;
  596. int i;
  597. // Check for a word
  598. len = skip_word(p) - p;
  599. if( len == 0 )
  600. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  601. // Duplicate the word
  602. CREATE(word, char, len+1);
  603. memcpy(word, p, len);
  604. word[len] = 0;
  605. // add the word
  606. i = add_str(word);
  607. aFree(word);
  608. return i;
  609. }
  610. /// Parses a function call.
  611. /// The argument list can have parenthesis or not.
  612. /// The number of arguments is checked.
  613. static
  614. const char* parse_callfunc(const char* p, int require_paren)
  615. {
  616. const char* p2;
  617. const char* arg=NULL;
  618. int func;
  619. func = add_word(p);
  620. if( str_data[func].type == C_FUNC ){
  621. // buildin function
  622. add_scriptl(func);
  623. add_scriptc(C_ARG);
  624. arg = buildin_func[str_data[func].val].arg;
  625. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  626. // script defined function
  627. int callsub = search_str("callsub");
  628. add_scriptl(callsub);
  629. add_scriptc(C_ARG);
  630. add_scriptl(func);
  631. arg = buildin_func[str_data[callsub].val].arg;
  632. if( *arg == 0 )
  633. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  634. if( *arg != '*' )
  635. ++arg; // count func as argument
  636. } else
  637. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  638. p = skip_word(p);
  639. p = skip_space(p);
  640. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  641. syntax.curly[syntax.curly_count].count = 0;
  642. if( *p == ';' )
  643. {// <func name> ';'
  644. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  645. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  646. {// <func name> '(' ')'
  647. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  648. p = p2;
  649. /*
  650. } else if( 0 && require_paren && *p != '(' )
  651. {// <func name>
  652. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  653. */
  654. } else
  655. {// <func name> <arg list>
  656. if( require_paren ){
  657. if( *p != '(' )
  658. disp_error_message("need '('",p);
  659. ++p; // skip '('
  660. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  661. } else if( *p == '(' ){
  662. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  663. } else {
  664. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  665. }
  666. ++syntax.curly_count;
  667. while( *arg ) {
  668. p2=parse_subexpr(p,-1);
  669. if( p == p2 )
  670. break; // not an argument
  671. if( *arg != '*' )
  672. ++arg; // next argument
  673. p=skip_space(p2);
  674. if( *arg == 0 || *p != ',' )
  675. break; // no more arguments
  676. ++p; // skip comma
  677. }
  678. --syntax.curly_count;
  679. }
  680. if( *arg && *arg != '?' && *arg != '*' )
  681. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  682. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  683. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  684. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  685. if( *p != ')' )
  686. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  687. ++p;
  688. }
  689. add_scriptc(C_FUNC);
  690. return p;
  691. }
  692. /*==========================================
  693. * 項の解析
  694. *------------------------------------------
  695. */
  696. const char* parse_simpleexpr(const char *p)
  697. {
  698. int i;
  699. p=skip_space(p);
  700. #ifdef DEBUG_FUNCIN
  701. if(battle_config.etc_log)
  702. ShowDebug("parse_simpleexpr %s\n",p);
  703. #endif
  704. if(*p==';' || *p==',')
  705. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  706. if(*p=='('){
  707. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  708. ++syntax.curly[i].count;
  709. p=parse_subexpr(p+1,-1);
  710. p=skip_space(p);
  711. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  712. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  713. ){
  714. if( *p == ',' ){
  715. syntax.curly[i].flag = ARGLIST_PAREN;
  716. return p;
  717. } else
  718. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  719. }
  720. if( *p != ')' )
  721. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  722. ++p;
  723. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  724. char *np;
  725. i=strtoul(p,&np,0);
  726. add_scripti(i);
  727. p=np;
  728. } else if(*p=='"'){
  729. add_scriptc(C_STR);
  730. p++;
  731. while( *p && *p != '"' ){
  732. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  733. p++;
  734. else if( *p == '\n' )
  735. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  736. add_scriptb(*p++);
  737. }
  738. if(!*p)
  739. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  740. add_scriptb(0);
  741. p++; //'"'
  742. } else {
  743. int l;
  744. // label , register , function etc
  745. if(skip_word(p)==p)
  746. disp_error_message("parse_simpleexpr: unexpected character",p);
  747. l=add_word(p);
  748. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  749. return parse_callfunc(p,1);
  750. p=skip_word(p);
  751. if( *p == '[' ){
  752. // array(name[i] => getelementofarray(name,i) )
  753. add_scriptl(search_str("getelementofarray"));
  754. add_scriptc(C_ARG);
  755. add_scriptl(l);
  756. p=parse_subexpr(p+1,-1);
  757. p=skip_space(p);
  758. if( *p != ']' )
  759. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  760. ++p;
  761. add_scriptc(C_FUNC);
  762. }else
  763. add_scriptl(l);
  764. }
  765. #ifdef DEBUG_FUNCIN
  766. if(battle_config.etc_log)
  767. ShowDebug("parse_simpleexpr end %s\n",p);
  768. #endif
  769. return p;
  770. }
  771. /*==========================================
  772. * 式の解析
  773. *------------------------------------------
  774. */
  775. const char* parse_subexpr(const char* p,int limit)
  776. {
  777. int op,opl,len;
  778. const char* tmpp;
  779. #ifdef DEBUG_FUNCIN
  780. if(battle_config.etc_log)
  781. ShowDebug("parse_subexpr %s\n",p);
  782. #endif
  783. p=skip_space(p);
  784. if(*p=='-'){
  785. tmpp=skip_space(p+1);
  786. if(*tmpp==';' || *tmpp==','){
  787. add_scriptl(LABEL_NEXTLINE);
  788. p++;
  789. return p;
  790. }
  791. }
  792. tmpp=p;
  793. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  794. p=parse_subexpr(p+1,10);
  795. add_scriptc(op);
  796. } else
  797. p=parse_simpleexpr(p);
  798. p=skip_space(p);
  799. while((
  800. (op=C_OP3,opl=0,len=1,*p=='?') ||
  801. (op=C_ADD,opl=8,len=1,*p=='+') ||
  802. (op=C_SUB,opl=8,len=1,*p=='-') ||
  803. (op=C_MUL,opl=9,len=1,*p=='*') ||
  804. (op=C_DIV,opl=9,len=1,*p=='/') ||
  805. (op=C_MOD,opl=9,len=1,*p=='%') ||
  806. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  807. (op=C_AND,opl=6,len=1,*p=='&') ||
  808. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  809. (op=C_OR,opl=5,len=1,*p=='|') ||
  810. (op=C_XOR,opl=4,len=1,*p=='^') ||
  811. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  812. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  813. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  814. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  815. (op=C_GT,opl=3,len=1,*p=='>') ||
  816. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  817. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  818. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  819. p+=len;
  820. if(op == C_OP3) {
  821. p=parse_subexpr(p,-1);
  822. p=skip_space(p);
  823. if( *(p++) != ':')
  824. disp_error_message("parse_subexpr: need ':'", p-1);
  825. p=parse_subexpr(p,-1);
  826. } else {
  827. p=parse_subexpr(p,opl);
  828. }
  829. add_scriptc(op);
  830. p=skip_space(p);
  831. }
  832. #ifdef DEBUG_FUNCIN
  833. if(battle_config.etc_log)
  834. ShowDebug("parse_subexpr end %s\n",p);
  835. #endif
  836. return p; /* return first untreated operator */
  837. }
  838. /*==========================================
  839. * 式の評価
  840. *------------------------------------------
  841. */
  842. const char* parse_expr(const char *p)
  843. {
  844. #ifdef DEBUG_FUNCIN
  845. if(battle_config.etc_log)
  846. ShowDebug("parse_expr %s\n",p);
  847. #endif
  848. switch(*p){
  849. case ')': case ';': case ':': case '[': case ']':
  850. case '}':
  851. disp_error_message("parse_expr: unexpected char",p);
  852. }
  853. p=parse_subexpr(p,-1);
  854. #ifdef DEBUG_FUNCIN
  855. if(battle_config.etc_log)
  856. ShowDebug("parse_expr end %s\n",p);
  857. #endif
  858. return p;
  859. }
  860. /*==========================================
  861. * 行の解析
  862. *------------------------------------------
  863. */
  864. const char* parse_line(const char* p)
  865. {
  866. const char* p2;
  867. p=skip_space(p);
  868. if(*p==';') {
  869. // if(); for(); while(); のために閉じ判定
  870. p = parse_syntax_close(p);
  871. return p+1;
  872. }
  873. if(*p==')' && parse_syntax_for_flag)
  874. return p+1;
  875. p = skip_space(p);
  876. if(p[0] == '{') {
  877. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  878. syntax.curly[syntax.curly_count].count = -1;
  879. syntax.curly[syntax.curly_count].index = -1;
  880. syntax.curly_count++;
  881. return p + 1;
  882. } else if(p[0] == '}') {
  883. return parse_curly_close(p);
  884. }
  885. // 構文関連の処理
  886. p2 = parse_syntax(p);
  887. if(p2 != NULL)
  888. return p2;
  889. p = parse_callfunc(p,0);
  890. p = skip_space(p);
  891. if(parse_syntax_for_flag) {
  892. if( *p != ')' )
  893. disp_error_message("parse_line: need ')'",p);
  894. } else {
  895. if( *p != ';' )
  896. disp_error_message("parse_line: need ';'",p);
  897. }
  898. // if, for , while の閉じ判定
  899. p = parse_syntax_close(p+1);
  900. return p;
  901. }
  902. // { ... } の閉じ処理
  903. const char* parse_curly_close(const char* p) {
  904. if(syntax.curly_count <= 0) {
  905. disp_error_message("parse_curly_close: unexpected string",p);
  906. return p + 1;
  907. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  908. syntax.curly_count--;
  909. // if, for , while の閉じ判定
  910. p = parse_syntax_close(p + 1);
  911. return p;
  912. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  913. // switch() 閉じ判定
  914. int pos = syntax.curly_count-1;
  915. char label[256];
  916. int l;
  917. // 一時変数を消す
  918. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  919. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  920. parse_line(label);
  921. syntax.curly_count--;
  922. // 無条件で終了ポインタに移動
  923. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  924. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  925. parse_line(label);
  926. syntax.curly_count--;
  927. // 現在地のラベルを付ける
  928. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  929. l=add_str(label);
  930. set_label(l,script_pos, p);
  931. if(syntax.curly[pos].flag) {
  932. // default が存在する
  933. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  934. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  935. parse_line(label);
  936. syntax.curly_count--;
  937. }
  938. // 終了ラベルを付ける
  939. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  940. l=add_str(label);
  941. set_label(l,script_pos, p);
  942. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  943. syntax.curly_count--;
  944. return p+1;
  945. } else {
  946. disp_error_message("parse_curly_close: unexpected string",p);
  947. return p + 1;
  948. }
  949. }
  950. // 構文関連の処理
  951. // break, case, continue, default, do, for, function,
  952. // if, switch, while をこの内部で処理します。
  953. const char* parse_syntax(const char* p)
  954. {
  955. const char *p2 = skip_word(p);
  956. switch(*p) {
  957. case 'B':
  958. case 'b':
  959. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  960. // break の処理
  961. char label[256];
  962. int pos = syntax.curly_count - 1;
  963. while(pos >= 0) {
  964. if(syntax.curly[pos].type == TYPE_DO) {
  965. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  966. break;
  967. } else if(syntax.curly[pos].type == TYPE_FOR) {
  968. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  969. break;
  970. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  971. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  972. break;
  973. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  974. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  975. break;
  976. }
  977. pos--;
  978. }
  979. if(pos < 0) {
  980. disp_error_message("parse_syntax: unexpected 'break'",p);
  981. } else {
  982. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  983. parse_line(label);
  984. syntax.curly_count--;
  985. }
  986. p = skip_space(p2);
  987. if(*p != ';') {
  988. disp_error_message("parse_syntax: need ';'",p);
  989. }
  990. p++;
  991. // if, for , while の閉じ判定
  992. p = parse_syntax_close(p + 1);
  993. return p;
  994. }
  995. break;
  996. case 'c':
  997. case 'C':
  998. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  999. // case の処理
  1000. int pos = syntax.curly_count-1;
  1001. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1002. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1003. return p+1;
  1004. } else {
  1005. char label[256];
  1006. int l,v;
  1007. char *np;
  1008. if(syntax.curly[pos].count != 1) {
  1009. // FALLTHRU 用のジャンプ
  1010. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1011. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1012. parse_line(label);
  1013. syntax.curly_count--;
  1014. // 現在地のラベルを付ける
  1015. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1016. l=add_str(label);
  1017. set_label(l,script_pos, p);
  1018. }
  1019. // switch 判定文
  1020. p = skip_space(p2);
  1021. if(p == p2) {
  1022. disp_error_message("parse_syntax: expect space ' '",p);
  1023. }
  1024. // check whether case label is integer or not
  1025. v = strtol(p,&np,0);
  1026. if(np == p) { //Check for constants
  1027. p2 = skip_word(p);
  1028. v = p2-p; // length of word at p2
  1029. memcpy(label,p,v);
  1030. label[v]='\0';
  1031. v = search_str(label);
  1032. if (v < 0 || str_data[v].type != C_INT)
  1033. disp_error_message("parse_syntax: 'case' label not integer",p);
  1034. v = str_data[v].val;
  1035. p = skip_word(p);
  1036. } else { //Numeric value
  1037. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1038. p++;
  1039. p = skip_word(p);
  1040. if(np != p)
  1041. disp_error_message("parse_syntax: 'case' label not integer",np);
  1042. }
  1043. p = skip_space(p);
  1044. if(*p != ':')
  1045. disp_error_message("parse_syntax: expect ':'",p);
  1046. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1047. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1048. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1049. // 2回parse しないとダメ
  1050. p2 = parse_line(label);
  1051. parse_line(p2);
  1052. syntax.curly_count--;
  1053. if(syntax.curly[pos].count != 1) {
  1054. // FALLTHRU 終了後のラベル
  1055. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1056. l=add_str(label);
  1057. set_label(l,script_pos,p);
  1058. }
  1059. // check duplication of case label [Rayce]
  1060. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1061. disp_error_message("parse_syntax: dup 'case'",p);
  1062. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1063. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1064. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1065. parse_line(label);
  1066. syntax.curly_count--;
  1067. syntax.curly[pos].count++;
  1068. }
  1069. return p + 1;
  1070. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1071. // continue の処理
  1072. char label[256];
  1073. int pos = syntax.curly_count - 1;
  1074. while(pos >= 0) {
  1075. if(syntax.curly[pos].type == TYPE_DO) {
  1076. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1077. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1078. break;
  1079. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1080. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1081. break;
  1082. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1083. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1084. break;
  1085. }
  1086. pos--;
  1087. }
  1088. if(pos < 0) {
  1089. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1090. } else {
  1091. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1092. parse_line(label);
  1093. syntax.curly_count--;
  1094. }
  1095. p = skip_space(p2);
  1096. if(*p != ';')
  1097. disp_error_message("parse_syntax: need ';'",p);
  1098. p++;
  1099. // if, for , while の閉じ判定
  1100. p = parse_syntax_close(p + 1);
  1101. return p;
  1102. }
  1103. break;
  1104. case 'd':
  1105. case 'D':
  1106. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1107. // switch - default の処理
  1108. int pos = syntax.curly_count-1;
  1109. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1110. disp_error_message("parse_syntax: unexpected 'default'",p);
  1111. } else if(syntax.curly[pos].flag) {
  1112. disp_error_message("parse_syntax: dup 'default'",p);
  1113. } else {
  1114. char label[256];
  1115. int l;
  1116. // 現在地のラベルを付ける
  1117. p = skip_space(p2);
  1118. if(*p != ':') {
  1119. disp_error_message("parse_syntax: need ':'",p);
  1120. }
  1121. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1122. l=add_str(label);
  1123. set_label(l,script_pos,p);
  1124. // 無条件で次のリンクに飛ばす
  1125. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1126. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1127. parse_line(label);
  1128. syntax.curly_count--;
  1129. // default のラベルを付ける
  1130. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1131. l=add_str(label);
  1132. set_label(l,script_pos,p);
  1133. syntax.curly[syntax.curly_count - 1].flag = 1;
  1134. syntax.curly[pos].count++;
  1135. }
  1136. return p + 1;
  1137. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1138. int l;
  1139. char label[256];
  1140. p=skip_space(p2);
  1141. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1142. syntax.curly[syntax.curly_count].count = 1;
  1143. syntax.curly[syntax.curly_count].index = syntax.index++;
  1144. syntax.curly[syntax.curly_count].flag = 0;
  1145. // 現在地のラベル形成する
  1146. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1147. l=add_str(label);
  1148. set_label(l,script_pos,p);
  1149. syntax.curly_count++;
  1150. return p;
  1151. }
  1152. break;
  1153. case 'f':
  1154. case 'F':
  1155. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1156. int l;
  1157. char label[256];
  1158. int pos = syntax.curly_count;
  1159. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1160. syntax.curly[syntax.curly_count].count = 1;
  1161. syntax.curly[syntax.curly_count].index = syntax.index++;
  1162. syntax.curly[syntax.curly_count].flag = 0;
  1163. syntax.curly_count++;
  1164. p=skip_space(p2);
  1165. if(*p != '(')
  1166. disp_error_message("parse_syntax: need '('",p);
  1167. p++;
  1168. // 初期化文を実行する
  1169. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1170. p=parse_line(p);
  1171. syntax.curly_count--;
  1172. // 条件判断開始のラベル形成する
  1173. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1174. l=add_str(label);
  1175. set_label(l,script_pos,p);
  1176. p=skip_space(p);
  1177. if(*p == ';') {
  1178. // for(;;) のパターンなので必ず真
  1179. ;
  1180. } else {
  1181. // 条件が偽なら終了地点に飛ばす
  1182. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1183. add_scriptl(add_str("jump_zero"));
  1184. add_scriptc(C_ARG);
  1185. p=parse_expr(p);
  1186. p=skip_space(p);
  1187. add_scriptl(add_str(label));
  1188. add_scriptc(C_FUNC);
  1189. }
  1190. if(*p != ';')
  1191. disp_error_message("parse_syntax: need ';'",p);
  1192. p++;
  1193. // ループ開始に飛ばす
  1194. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1195. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1196. parse_line(label);
  1197. syntax.curly_count--;
  1198. // 次のループへのラベル形成する
  1199. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1200. l=add_str(label);
  1201. set_label(l,script_pos,p);
  1202. // 次のループに入る時の処理
  1203. // for 最後の ')' を ';' として扱うフラグ
  1204. parse_syntax_for_flag = 1;
  1205. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1206. p=parse_line(p);
  1207. syntax.curly_count--;
  1208. parse_syntax_for_flag = 0;
  1209. // 条件判定処理に飛ばす
  1210. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1211. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1212. parse_line(label);
  1213. syntax.curly_count--;
  1214. // ループ開始のラベル付け
  1215. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1216. l=add_str(label);
  1217. set_label(l,script_pos,p);
  1218. return p;
  1219. }
  1220. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1221. {// internal script function
  1222. const char *func_name;
  1223. func_name = skip_space(p2);
  1224. p = skip_word(func_name);
  1225. if( p == func_name )
  1226. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1227. if( *skip_space(p) == ';' )
  1228. {// function <name> ;
  1229. // function declaration - just register the name
  1230. int l;
  1231. l = add_word(func_name);
  1232. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1233. str_data[l].type = C_USERFUNC;
  1234. return skip_space(p) + 1;
  1235. }
  1236. else
  1237. {// function <name> <line/block of code>
  1238. char label[256];
  1239. int l;
  1240. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1241. syntax.curly[syntax.curly_count].count = 1;
  1242. syntax.curly[syntax.curly_count].index = syntax.index++;
  1243. syntax.curly[syntax.curly_count].flag = 0;
  1244. ++syntax.curly_count;
  1245. // Jump over the function code
  1246. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1247. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1248. ++syntax.curly_count;
  1249. parse_line(label);
  1250. --syntax.curly_count;
  1251. // Set the position of the function (label)
  1252. l=add_word(func_name);
  1253. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1254. str_data[l].type = C_USERFUNC;
  1255. set_label(l, script_pos, p);
  1256. if( parse_options&SCRIPT_USE_LABEL_DB )
  1257. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1258. return skip_space(p);
  1259. }
  1260. }
  1261. break;
  1262. case 'i':
  1263. case 'I':
  1264. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1265. // if() の処理
  1266. char label[256];
  1267. p=skip_space(p2);
  1268. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1269. disp_error_message("need '('",p);
  1270. }
  1271. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1272. syntax.curly[syntax.curly_count].count = 1;
  1273. syntax.curly[syntax.curly_count].index = syntax.index++;
  1274. syntax.curly[syntax.curly_count].flag = 0;
  1275. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1276. syntax.curly_count++;
  1277. add_scriptl(add_str("jump_zero"));
  1278. add_scriptc(C_ARG);
  1279. p=parse_expr(p);
  1280. p=skip_space(p);
  1281. add_scriptl(add_str(label));
  1282. add_scriptc(C_FUNC);
  1283. return p;
  1284. }
  1285. break;
  1286. case 's':
  1287. case 'S':
  1288. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1289. // switch() の処理
  1290. char label[256];
  1291. p=skip_space(p2);
  1292. if(*p != '(') {
  1293. disp_error_message("need '('",p);
  1294. }
  1295. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1296. syntax.curly[syntax.curly_count].count = 1;
  1297. syntax.curly[syntax.curly_count].index = syntax.index++;
  1298. syntax.curly[syntax.curly_count].flag = 0;
  1299. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1300. syntax.curly_count++;
  1301. add_scriptl(add_str("set"));
  1302. add_scriptc(C_ARG);
  1303. add_scriptl(add_str(label));
  1304. p=parse_expr(p);
  1305. p=skip_space(p);
  1306. if(*p != '{') {
  1307. disp_error_message("parse_syntax: need '{'",p);
  1308. }
  1309. add_scriptc(C_FUNC);
  1310. return p + 1;
  1311. }
  1312. break;
  1313. case 'w':
  1314. case 'W':
  1315. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1316. int l;
  1317. char label[256];
  1318. p=skip_space(p2);
  1319. if(*p != '(') {
  1320. disp_error_message("need '('",p);
  1321. }
  1322. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1323. syntax.curly[syntax.curly_count].count = 1;
  1324. syntax.curly[syntax.curly_count].index = syntax.index++;
  1325. syntax.curly[syntax.curly_count].flag = 0;
  1326. // 条件判断開始のラベル形成する
  1327. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1328. l=add_str(label);
  1329. set_label(l,script_pos,p);
  1330. // 条件が偽なら終了地点に飛ばす
  1331. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1332. syntax.curly_count++;
  1333. add_scriptl(add_str("jump_zero"));
  1334. add_scriptc(C_ARG);
  1335. p=parse_expr(p);
  1336. p=skip_space(p);
  1337. add_scriptl(add_str(label));
  1338. add_scriptc(C_FUNC);
  1339. return p;
  1340. }
  1341. break;
  1342. }
  1343. return NULL;
  1344. }
  1345. const char* parse_syntax_close(const char *p) {
  1346. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1347. int flag;
  1348. do {
  1349. p = parse_syntax_close_sub(p,&flag);
  1350. } while(flag);
  1351. return p;
  1352. }
  1353. // if, for , while , do の閉じ判定
  1354. // flag == 1 : 閉じられた
  1355. // flag == 0 : 閉じられない
  1356. const char* parse_syntax_close_sub(const char* p,int* flag) {
  1357. char label[256];
  1358. int pos = syntax.curly_count - 1;
  1359. int l;
  1360. *flag = 1;
  1361. if(syntax.curly_count <= 0) {
  1362. *flag = 0;
  1363. return p;
  1364. } else if(syntax.curly[pos].type == TYPE_IF) {
  1365. const char *bp = p;
  1366. const char *p2;
  1367. // if 最終場所へ飛ばす
  1368. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1369. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1370. parse_line(label);
  1371. syntax.curly_count--;
  1372. // 現在地のラベルを付ける
  1373. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1374. l=add_str(label);
  1375. set_label(l,script_pos,p);
  1376. syntax.curly[pos].count++;
  1377. p = skip_space(p);
  1378. p2 = skip_word(p);
  1379. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1380. // else or else - if
  1381. p = skip_space(p2);
  1382. p2 = skip_word(p);
  1383. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1384. // else - if
  1385. p=skip_space(p2);
  1386. if(*p != '(') {
  1387. disp_error_message("need '('",p);
  1388. }
  1389. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1390. add_scriptl(add_str("jump_zero"));
  1391. add_scriptc(C_ARG);
  1392. p=parse_expr(p);
  1393. p=skip_space(p);
  1394. add_scriptl(add_str(label));
  1395. add_scriptc(C_FUNC);
  1396. *flag = 0;
  1397. return p;
  1398. } else {
  1399. // else
  1400. if(!syntax.curly[pos].flag) {
  1401. syntax.curly[pos].flag = 1;
  1402. *flag = 0;
  1403. return p;
  1404. }
  1405. }
  1406. }
  1407. // if 閉じ
  1408. syntax.curly_count--;
  1409. // 最終地のラベルを付ける
  1410. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1411. l=add_str(label);
  1412. set_label(l,script_pos,p);
  1413. if(syntax.curly[pos].flag == 1) {
  1414. // このifに対するelseじゃないのでポインタの位置は同じ
  1415. return bp;
  1416. }
  1417. return p;
  1418. } else if(syntax.curly[pos].type == TYPE_DO) {
  1419. int l;
  1420. char label[256];
  1421. const char *p2;
  1422. if(syntax.curly[pos].flag) {
  1423. // 現在地のラベル形成する(continue でここに来る)
  1424. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1425. l=add_str(label);
  1426. set_label(l,script_pos,p);
  1427. }
  1428. // 条件が偽なら終了地点に飛ばす
  1429. p = skip_space(p);
  1430. p2 = skip_word(p);
  1431. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1432. disp_error_message("parse_syntax: need 'while'",p);
  1433. p = skip_space(p2);
  1434. if(*p != '(') {
  1435. disp_error_message("need '('",p);
  1436. }
  1437. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1438. add_scriptl(add_str("jump_zero"));
  1439. add_scriptc(C_ARG);
  1440. p=parse_expr(p);
  1441. p=skip_space(p);
  1442. add_scriptl(add_str(label));
  1443. add_scriptc(C_FUNC);
  1444. // 開始地点に飛ばす
  1445. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1446. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1447. parse_line(label);
  1448. syntax.curly_count--;
  1449. // 条件終了地点のラベル形成する
  1450. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1451. l=add_str(label);
  1452. set_label(l,script_pos,p);
  1453. p = skip_space(p);
  1454. if(*p != ';') {
  1455. disp_error_message("parse_syntax: need ';'",p);
  1456. return p+1;
  1457. }
  1458. p++;
  1459. syntax.curly_count--;
  1460. return p;
  1461. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1462. // 次のループに飛ばす
  1463. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1464. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1465. parse_line(label);
  1466. syntax.curly_count--;
  1467. // for 終了のラベル付け
  1468. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1469. l=add_str(label);
  1470. set_label(l,script_pos,p);
  1471. syntax.curly_count--;
  1472. return p;
  1473. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1474. // while 条件判断へ飛ばす
  1475. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1476. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1477. parse_line(label);
  1478. syntax.curly_count--;
  1479. // while 終了のラベル付け
  1480. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1481. l=add_str(label);
  1482. set_label(l,script_pos,p);
  1483. syntax.curly_count--;
  1484. return p;
  1485. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1486. int pos = syntax.curly_count-1;
  1487. char label[256];
  1488. int l;
  1489. // 戻す
  1490. sprintf(label,"return;");
  1491. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1492. parse_line(label);
  1493. syntax.curly_count--;
  1494. // 現在地のラベルを付ける
  1495. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1496. l=add_str(label);
  1497. set_label(l,script_pos,p);
  1498. syntax.curly_count--;
  1499. return p + 1;
  1500. } else {
  1501. *flag = 0;
  1502. return p;
  1503. }
  1504. }
  1505. /*==========================================
  1506. * 組み込み関数の追加
  1507. *------------------------------------------
  1508. */
  1509. static void add_buildin_func(void)
  1510. {
  1511. int i,n;
  1512. const char* p;
  1513. for( i=0; buildin_func[i].func; i++ ){
  1514. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1515. // 'v' - value (either string or int or reference)
  1516. // 's' - string
  1517. // 'i' - int
  1518. // 'r' - reference (of a variable)
  1519. // 'l' - label
  1520. // '?' - one optional parameter
  1521. // '*' - unknown number of optional parameters
  1522. p=buildin_func[i].arg;
  1523. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1524. while( *p == '?' ) ++p;
  1525. if( *p == '*' ) ++p;
  1526. if( *p != 0){
  1527. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1528. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1529. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1530. } else {
  1531. n=add_str(buildin_func[i].name);
  1532. str_data[n].type=C_FUNC;
  1533. str_data[n].val=i;
  1534. str_data[n].func=buildin_func[i].func;
  1535. }
  1536. }
  1537. }
  1538. /*==========================================
  1539. * 定数データベースの読み込み
  1540. *------------------------------------------
  1541. */
  1542. static void read_constdb(void)
  1543. {
  1544. FILE *fp;
  1545. char line[1024],name[1024],val[1024];
  1546. int n,type;
  1547. sprintf(line, "%s/const.txt", db_path);
  1548. fp=fopen(line, "r");
  1549. if(fp==NULL){
  1550. ShowError("can't read %s\n", line);
  1551. return ;
  1552. }
  1553. while(fgets(line,1020,fp)){
  1554. if(line[0]=='/' && line[1]=='/')
  1555. continue;
  1556. type=0;
  1557. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1558. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1559. n=add_str(name);
  1560. if(type==0)
  1561. str_data[n].type=C_INT;
  1562. else
  1563. str_data[n].type=C_PARAM;
  1564. str_data[n].val= (int)strtol(val,NULL,0);
  1565. }
  1566. }
  1567. fclose(fp);
  1568. }
  1569. /*==========================================
  1570. * エラー表示
  1571. *------------------------------------------
  1572. */
  1573. const char* script_print_line( const char *p, const char *mark, int line ) {
  1574. int i;
  1575. if( p == NULL || !p[0] ) return NULL;
  1576. if( line < 0 )
  1577. printf("*% 5d : ", -line);
  1578. else
  1579. printf(" % 5d : ", line);
  1580. for(i=0;p[i] && p[i] != '\n';i++){
  1581. if(p + i != mark)
  1582. printf("%c",p[i]);
  1583. else
  1584. printf("\'%c\'",p[i]);
  1585. }
  1586. printf("\n");
  1587. return p+i+(p[i] == '\n' ? 1 : 0);
  1588. }
  1589. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos) {
  1590. // エラーが発生した行を求める
  1591. int j;
  1592. int line = start_line;
  1593. const char *p;
  1594. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1595. for(p=src;p && *p;line++){
  1596. char *lineend=strchr(p,'\n');
  1597. if(lineend==NULL || error_pos<lineend){
  1598. break;
  1599. }
  1600. for( j = 0; j < 4; j++ ) {
  1601. linestart[j] = linestart[j+1];
  1602. }
  1603. linestart[4] = p;
  1604. p=lineend+1;
  1605. }
  1606. printf("\a\n");
  1607. printf("script error on %s line %d\n", file, line);
  1608. printf(" %s\n", error_msg);
  1609. for(j = 0; j < 5; j++ ) {
  1610. script_print_line( linestart[j], NULL, line + j - 5);
  1611. }
  1612. p = script_print_line( p, error_pos, -line);
  1613. for(j = 0; j < 5; j++) {
  1614. p = script_print_line( p, NULL, line + j + 1 );
  1615. }
  1616. }
  1617. /*==========================================
  1618. * スクリプトの解析
  1619. *------------------------------------------
  1620. */
  1621. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1622. {
  1623. const char *p,*tmpp;
  1624. int i;
  1625. struct script_code *code;
  1626. static int first=1;
  1627. memset(&syntax,0,sizeof(syntax));
  1628. if(first){
  1629. add_buildin_func();
  1630. read_constdb();
  1631. }
  1632. first=0;
  1633. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1634. script_pos=0;
  1635. script_size=SCRIPT_BLOCK_SIZE;
  1636. str_data[LABEL_NEXTLINE].type=C_NOP;
  1637. str_data[LABEL_NEXTLINE].backpatch=-1;
  1638. str_data[LABEL_NEXTLINE].label=-1;
  1639. for(i=LABEL_START;i<str_num;i++){
  1640. if(
  1641. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1642. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1643. ){
  1644. str_data[i].type=C_NOP;
  1645. str_data[i].backpatch=-1;
  1646. str_data[i].label=-1;
  1647. }
  1648. }
  1649. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1650. if( options&SCRIPT_USE_LABEL_DB )
  1651. scriptlabel_db->clear(scriptlabel_db, NULL);
  1652. parse_options = options;
  1653. if( setjmp( error_jump ) != 0 ) {
  1654. //Restore program state when script has problems. [from jA]
  1655. int i;
  1656. const int size = sizeof(syntax.curly)/sizeof(syntax.curly[0]);
  1657. if( error_report )
  1658. script_error(src,file,line,error_msg,error_pos);
  1659. aFree( error_msg );
  1660. aFree( script_buf );
  1661. script_pos = 0;
  1662. script_size = 0;
  1663. script_buf = NULL;
  1664. for(i=LABEL_START;i<str_num;i++)
  1665. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1666. for(i=0; i<size; i++)
  1667. linkdb_final(&syntax.curly[i].case_label);
  1668. return NULL;
  1669. }
  1670. parse_syntax_for_flag=0;
  1671. p=src;
  1672. p=skip_space(p);
  1673. if(*p!='{'){
  1674. disp_error_message("not found '{'",p);
  1675. }
  1676. p++;
  1677. p = skip_space(p);
  1678. if (p && *p == '}') {
  1679. // an empty function, just return
  1680. aFree( script_buf );
  1681. script_pos = 0;
  1682. script_size = 0;
  1683. script_buf = NULL;
  1684. return NULL;
  1685. }
  1686. while (p && *p && (*p!='}' || syntax.curly_count != 0)) {
  1687. p=skip_space(p);
  1688. // labelだけ特殊処理
  1689. tmpp=skip_space(skip_word(p));
  1690. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1691. i=add_word(p);
  1692. set_label(i,script_pos,p);
  1693. if( parse_options&SCRIPT_USE_LABEL_DB )
  1694. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1695. p=tmpp+1;
  1696. continue;
  1697. }
  1698. // 他は全部一緒くた
  1699. p=parse_line(p);
  1700. p=skip_space(p);
  1701. add_scriptc(C_EOL);
  1702. set_label(LABEL_NEXTLINE,script_pos,p);
  1703. str_data[LABEL_NEXTLINE].type=C_NOP;
  1704. str_data[LABEL_NEXTLINE].backpatch=-1;
  1705. str_data[LABEL_NEXTLINE].label=-1;
  1706. }
  1707. add_scriptc(C_NOP);
  1708. script_size = script_pos;
  1709. RECREATE(script_buf,unsigned char,script_pos);
  1710. // 未解決のラベルを解決
  1711. for(i=LABEL_START;i<str_num;i++){
  1712. if(str_data[i].type==C_NOP){
  1713. int j,next;
  1714. str_data[i].type=C_NAME;
  1715. str_data[i].label=i;
  1716. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1717. next=GETVALUE(script_buf,j);
  1718. SETVALUE(script_buf,j,i);
  1719. j=next;
  1720. }
  1721. }
  1722. }
  1723. #ifdef DEBUG_DISP
  1724. for(i=0;i<script_pos;i++){
  1725. if((i&15)==0) printf("%04x : ",i);
  1726. printf("%02x ",script_buf[i]);
  1727. if((i&15)==15) printf("\n");
  1728. }
  1729. printf("\n");
  1730. #endif
  1731. #ifdef DEBUG_DISASM
  1732. {
  1733. int i = 0,j;
  1734. while(i < script_pos) {
  1735. printf("%06x ",i);
  1736. j = i;
  1737. switch(get_com(script_buf,&i)) {
  1738. case C_EOL: printf("C_EOL"); break;
  1739. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1740. case C_POS:
  1741. printf("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1742. i += 3;
  1743. break;
  1744. case C_NAME:
  1745. j = (*(int*)(script_buf+i)&0xffffff);
  1746. printf("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1747. i += 3;
  1748. break;
  1749. case C_ARG: printf("C_ARG"); break;
  1750. case C_FUNC: printf("C_FUNC"); break;
  1751. case C_ADD: printf("C_ADD"); break;
  1752. case C_SUB: printf("C_SUB"); break;
  1753. case C_MUL: printf("C_MUL"); break;
  1754. case C_DIV: printf("C_DIV"); break;
  1755. case C_MOD: printf("C_MOD"); break;
  1756. case C_EQ: printf("C_EQ"); break;
  1757. case C_NE: printf("C_NE"); break;
  1758. case C_GT: printf("C_GT"); break;
  1759. case C_GE: printf("C_GE"); break;
  1760. case C_LT: printf("C_LT"); break;
  1761. case C_LE: printf("C_LE"); break;
  1762. case C_AND: printf("C_AND"); break;
  1763. case C_OR: printf("C_OR"); break;
  1764. case C_XOR: printf("C_XOR"); break;
  1765. case C_LAND: printf("C_LAND"); break;
  1766. case C_LOR: printf("C_LOR"); break;
  1767. case C_R_SHIFT: printf("C_R_SHIFT"); break;
  1768. case C_L_SHIFT: printf("C_L_SHIFT"); break;
  1769. case C_NEG: printf("C_NEG"); break;
  1770. case C_NOT: printf("C_NOT"); break;
  1771. case C_LNOT: printf("C_LNOT"); break;
  1772. case C_NOP: printf("C_NOP"); break;
  1773. case C_OP3: printf("C_OP3"); break;
  1774. case C_STR:
  1775. j = strlen(script_buf + i);
  1776. printf("C_STR %s",script_buf + i);
  1777. i+= j+1;
  1778. break;
  1779. default:
  1780. printf("unknown");
  1781. }
  1782. printf("\n");
  1783. }
  1784. }
  1785. #endif
  1786. CREATE(code,struct script_code,1);
  1787. code->script_buf = script_buf;
  1788. code->script_size = script_size;
  1789. code->script_vars = NULL;
  1790. return code;
  1791. }
  1792. //
  1793. // 実行系
  1794. //
  1795. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1796. /// Returns the player attached to this script, identified by the rid.
  1797. /// If there is no player attached, the script is terminated.
  1798. struct map_session_data *script_rid2sd(struct script_state *st)
  1799. {
  1800. struct map_session_data *sd=map_id2sd(st->rid);
  1801. if(!sd){
  1802. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1803. st->state = END;
  1804. }
  1805. return sd;
  1806. }
  1807. /*==========================================
  1808. * Retrieves the value of a script variable
  1809. *------------------------------------------*/
  1810. int get_val(struct script_state* st, struct script_data* data)
  1811. {
  1812. struct map_session_data* sd = NULL;
  1813. char *name, prefix, postfix;
  1814. if(!data_isreference(data)) return 0;
  1815. name = str_buf + str_data[data->u.num&0x00ffffff].str;
  1816. prefix = name[0]; postfix = name[strlen(name)-1];
  1817. if(not_server_variable(prefix)) {
  1818. sd = script_rid2sd(st);
  1819. if (!sd) { // needs player attached
  1820. // throw error, load some meaningful default values and return
  1821. ShowError("get_val error, cannot access player variable '%s'\n", name);
  1822. if (postfix == '$') { data->type = C_CONSTSTR; data->u.str = ""; } else { data->type = C_INT; data->u.num = 0; }
  1823. return 0;
  1824. }
  1825. }
  1826. if(postfix == '$') { // string variable
  1827. data->type = C_CONSTSTR;
  1828. switch (prefix) {
  1829. case '@':
  1830. data->u.str = pc_readregstr(sd, data->u.num); break;
  1831. case '$':
  1832. data->u.str = (char *)idb_get(mapregstr_db,data->u.num); break;
  1833. case '#':
  1834. data->u.str = (name[1] == '#') ? pc_readaccountreg2str(sd, name) : pc_readaccountregstr(sd, name); break;
  1835. case '.': {
  1836. struct linkdb_node** n;
  1837. n = (data->ref) ? data->ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  1838. data->u.str = linkdb_search(n, (void*)data->u.num);
  1839. }
  1840. break;
  1841. default:
  1842. data->u.str = pc_readglobalreg_str(sd, name); break;
  1843. }
  1844. if( data->u.str == NULL )
  1845. data->u.str = "";
  1846. } else { // integer variable
  1847. data->type = C_INT;
  1848. if(str_data[data->u.num&0x00ffffff].type == C_INT) {
  1849. data->u.num = str_data[data->u.num&0x00ffffff].val;
  1850. } else if(str_data[data->u.num&0x00ffffff].type == C_PARAM) {
  1851. data->u.num = pc_readparam(sd, str_data[data->u.num&0x00ffffff].val);
  1852. }
  1853. else
  1854. switch (prefix) {
  1855. case '@':
  1856. data->u.num = pc_readreg(sd, data->u.num); break;
  1857. case '$':
  1858. data->u.num = (int)idb_get(mapreg_db, data->u.num); break;
  1859. case '#':
  1860. data->u.num = (name[1] == '#') ? pc_readaccountreg2(sd, name) : pc_readaccountreg(sd, name); break;
  1861. case '.': {
  1862. struct linkdb_node** n;
  1863. n = (data->ref) ? data->ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  1864. data->u.num = (int)linkdb_search(n, (void*)data->u.num);
  1865. }
  1866. break;
  1867. default:
  1868. data->u.num = pc_readglobalreg(sd, name); break;
  1869. }
  1870. }
  1871. return 0;
  1872. }
  1873. /*==========================================
  1874. * Retrieves the value of a script variable
  1875. *------------------------------------------*/
  1876. void* get_val2(struct script_state* st, int num, struct linkdb_node** ref)
  1877. {
  1878. struct script_data dat;
  1879. dat.type = C_NAME;
  1880. dat.u.num = num;
  1881. dat.ref = ref;
  1882. get_val(st, &dat);
  1883. return (dat.type == C_INT) ? (void*)dat.u.num : (void*)dat.u.str;
  1884. }
  1885. /*==========================================
  1886. * Stores the value of a script variable
  1887. * Return value is 0 on fail, 1 on success.
  1888. *------------------------------------------*/
  1889. static int set_reg(struct script_state* st, struct map_session_data* sd, int num, char* name, void* value, struct linkdb_node** ref)
  1890. {
  1891. char prefix = name[0]; char postfix = name[strlen(name)-1];
  1892. if (postfix == '$') { // string variable
  1893. char* str = (char*)value;
  1894. switch (prefix) {
  1895. case '@':
  1896. return pc_setregstr(sd, num, str);
  1897. case '$':
  1898. return mapreg_setregstr(num, str);
  1899. case '#':
  1900. return (name[1] == '#') ?
  1901. pc_setaccountreg2str(sd, name, str) :
  1902. pc_setaccountregstr(sd, name, str);
  1903. case '.': {
  1904. char* p;
  1905. struct linkdb_node** n;
  1906. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  1907. p = linkdb_search(n, (void*)num);
  1908. if (p) {
  1909. linkdb_erase(n, (void*)num);
  1910. aFree(p);
  1911. }
  1912. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  1913. }
  1914. return 1;
  1915. default:
  1916. return pc_setglobalreg_str(sd, name, str);
  1917. }
  1918. } else { // integer variable
  1919. int val = (int)value;
  1920. if(str_data[num&0x00ffffff].type == C_PARAM)
  1921. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  1922. switch (prefix) {
  1923. case '@':
  1924. return pc_setreg(sd, num, val);
  1925. case '$':
  1926. return mapreg_setreg(num, val);
  1927. case '#':
  1928. return (name[1] == '#') ?
  1929. pc_setaccountreg2(sd, name, val) :
  1930. pc_setaccountreg(sd, name, val);
  1931. case '.': {
  1932. struct linkdb_node** n;
  1933. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  1934. if (val == 0)
  1935. linkdb_erase(n, (void*)num);
  1936. else
  1937. linkdb_replace(n, (void*)num, (void*)val);
  1938. }
  1939. return 1;
  1940. default:
  1941. return pc_setglobalreg(sd, name, val);
  1942. }
  1943. }
  1944. }
  1945. int set_var(struct map_session_data* sd, char* name, void* val)
  1946. {
  1947. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  1948. }
  1949. /*==========================================
  1950. * 文字列への変換
  1951. *------------------------------------------
  1952. */
  1953. const char* conv_str(struct script_state *st,struct script_data *data)
  1954. {
  1955. get_val(st,data);
  1956. if(data_isint(data)){
  1957. char *buf;
  1958. CREATE(buf,char,ITEM_NAME_LENGTH);
  1959. snprintf(buf,ITEM_NAME_LENGTH, "%d",data->u.num);
  1960. buf[ITEM_NAME_LENGTH-1]=0;
  1961. data->type=C_STR;
  1962. data->u.str=buf;
  1963. } else if(data_islabel(data)) {
  1964. // Protect from crashes by passing labels to string-expected args [jA2200]
  1965. data->type = C_CONSTSTR;
  1966. data->u.str = "** SCRIPT ERROR **";
  1967. } else if(data_isreference(data)){
  1968. data->type=C_CONSTSTR;
  1969. data->u.str=str_buf+str_data[data->u.num].str;
  1970. }
  1971. return data->u.str;
  1972. }
  1973. /*==========================================
  1974. * 数値へ変換
  1975. *------------------------------------------
  1976. */
  1977. int conv_num(struct script_state *st,struct script_data *data)
  1978. {
  1979. char *p;
  1980. get_val(st,data);
  1981. if(data_isstring(data)){
  1982. p=data->u.str;
  1983. data->u.num = atoi(p);
  1984. if(data->type==C_STR)
  1985. aFree(p);
  1986. data->type=C_INT;
  1987. }
  1988. return data->u.num;
  1989. }
  1990. /*==========================================
  1991. * スタックへ数値をプッシュ
  1992. *------------------------------------------
  1993. */
  1994. void push_val(struct script_stack *stack,int type,int val)
  1995. {
  1996. if(stack->sp >= stack->sp_max){
  1997. stack->sp_max += 64;
  1998. stack->stack_data = (struct script_data *)aRealloc(stack->stack_data,
  1999. sizeof(stack->stack_data[0]) * stack->sp_max);
  2000. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2001. 64 * sizeof(*(stack->stack_data)));
  2002. }
  2003. // if(battle_config.etc_log)
  2004. // printf("push (%d,%d)-> %d\n",type,val,stack->sp);
  2005. stack->stack_data[stack->sp].type=type;
  2006. stack->stack_data[stack->sp].u.num=val;
  2007. stack->stack_data[stack->sp].ref = NULL;
  2008. stack->sp++;
  2009. }
  2010. /*==========================================
  2011. * スタックへ数値+リファレンスをプッシュ
  2012. *------------------------------------------
  2013. */
  2014. void push_val2(struct script_stack *stack,int type,int val,struct linkdb_node** ref) {
  2015. push_val(stack,type,val);
  2016. stack->stack_data[stack->sp-1].ref = ref;
  2017. }
  2018. /*==========================================
  2019. * スタックへ文字列をプッシュ
  2020. *------------------------------------------
  2021. */
  2022. void push_str(struct script_stack *stack,int type,char *str)
  2023. {
  2024. if(stack->sp>=stack->sp_max){
  2025. stack->sp_max += 64;
  2026. stack->stack_data = (struct script_data *)aRealloc(stack->stack_data,
  2027. sizeof(stack->stack_data[0]) * stack->sp_max);
  2028. memset(stack->stack_data + (stack->sp_max - 64), '\0',
  2029. 64 * sizeof(*(stack->stack_data)));
  2030. }
  2031. // if(battle_config.etc_log)
  2032. // printf("push (%d,%x)-> %d\n",type,str,stack->sp);
  2033. stack->stack_data[stack->sp].type =type;
  2034. stack->stack_data[stack->sp].u.str=str;
  2035. stack->stack_data[stack->sp].ref =NULL;
  2036. stack->sp++;
  2037. }
  2038. /*==========================================
  2039. * スタックへ複製をプッシュ
  2040. *------------------------------------------
  2041. */
  2042. void push_copy(struct script_stack *stack,int pos)
  2043. {
  2044. switch(stack->stack_data[pos].type){
  2045. case C_CONSTSTR:
  2046. push_str(stack,C_CONSTSTR,stack->stack_data[pos].u.str);
  2047. break;
  2048. case C_STR:
  2049. push_str(stack,C_STR,aStrdup(stack->stack_data[pos].u.str));
  2050. break;
  2051. default:
  2052. push_val2(
  2053. stack,stack->stack_data[pos].type,stack->stack_data[pos].u.num,
  2054. stack->stack_data[pos].ref
  2055. );
  2056. break;
  2057. }
  2058. }
  2059. /*==========================================
  2060. * スタックからポップ
  2061. *------------------------------------------
  2062. */
  2063. void pop_stack(struct script_stack* stack,int start,int end)
  2064. {
  2065. int i;
  2066. for(i=start;i<end;i++){
  2067. if(stack->stack_data[i].type==C_STR){
  2068. aFree(stack->stack_data[i].u.str);
  2069. stack->stack_data[i].type=C_INT; //Might not be correct, but it's done in case to prevent pointer errors later on. [Skotlex]
  2070. }
  2071. }
  2072. if(stack->sp>end){
  2073. memmove(&stack->stack_data[start],&stack->stack_data[end],sizeof(stack->stack_data[0])*(stack->sp-end));
  2074. }
  2075. stack->sp-=end-start;
  2076. }
  2077. /*==========================================
  2078. * スクリプト依存変数、関数依存変数の解放
  2079. *------------------------------------------
  2080. */
  2081. void script_free_vars(struct linkdb_node **node) {
  2082. struct linkdb_node *n = *node;
  2083. while(n) {
  2084. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2085. char postfix = name[strlen(name)-1];
  2086. if( postfix == '$' ) {
  2087. // 文字型変数なので、データ削除
  2088. aFree(n->data);
  2089. }
  2090. n = n->next;
  2091. }
  2092. linkdb_final( node );
  2093. }
  2094. /*==========================================
  2095. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2096. *------------------------------------------
  2097. */
  2098. void script_free_stack(struct script_stack *stack) {
  2099. int i;
  2100. for(i = 0; i < stack->sp; i++) {
  2101. if( stack->stack_data[i].type == C_STR ) {
  2102. aFree(stack->stack_data[i].u.str);
  2103. stack->stack_data[i].type = C_INT;
  2104. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2105. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2106. script_free_vars( n );
  2107. aFree( n );
  2108. }
  2109. }
  2110. script_free_vars( stack->var_function );
  2111. aFree(stack->var_function);
  2112. aFree(stack->stack_data);
  2113. aFree(stack);
  2114. }
  2115. void script_free_code(struct script_code* code) {
  2116. script_free_vars( &code->script_vars );
  2117. aFree( code->script_buf );
  2118. aFree( code );
  2119. }
  2120. //
  2121. // 実行部main
  2122. //
  2123. /*==========================================
  2124. * コマンドの読み取り
  2125. *------------------------------------------
  2126. */
  2127. static int unget_com_data=-1;
  2128. int get_com(unsigned char *script,int *pos)
  2129. {
  2130. int i,j;
  2131. if(unget_com_data>=0){
  2132. i=unget_com_data;
  2133. unget_com_data=-1;
  2134. return i;
  2135. }
  2136. if(script[*pos]>=0x80){
  2137. return C_INT;
  2138. }
  2139. i=0; j=0;
  2140. while(script[*pos]>=0x40){
  2141. i=script[(*pos)++]<<j;
  2142. j+=6;
  2143. }
  2144. return i+(script[(*pos)++]<<j);
  2145. }
  2146. /*==========================================
  2147. * コマンドのプッシュバック
  2148. *------------------------------------------
  2149. */
  2150. void unget_com(int c)
  2151. {
  2152. if(unget_com_data!=-1){
  2153. if(battle_config.error_log)
  2154. ShowError("unget_com can back only 1 data\n");
  2155. }
  2156. unget_com_data=c;
  2157. }
  2158. /*==========================================
  2159. * 数値の所得
  2160. *------------------------------------------
  2161. */
  2162. int get_num(unsigned char *script,int *pos)
  2163. {
  2164. int i,j;
  2165. i=0; j=0;
  2166. while(script[*pos]>=0xc0){
  2167. i+=(script[(*pos)++]&0x7f)<<j;
  2168. j+=6;
  2169. }
  2170. return i+((script[(*pos)++]&0x7f)<<j);
  2171. }
  2172. /*==========================================
  2173. * スタックから値を取り出す
  2174. *------------------------------------------
  2175. */
  2176. int pop_val(struct script_state* st)
  2177. {
  2178. if(st->stack->sp<=0)
  2179. return 0;
  2180. st->stack->sp--;
  2181. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2182. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2183. return st->stack->stack_data[st->stack->sp].u.num;
  2184. return 0;
  2185. }
  2186. int isstr(struct script_data *c) {
  2187. if( data_isstring(c) )
  2188. return 1;
  2189. else if( data_isreference(c) ) {
  2190. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2191. char postfix = p[strlen(p)-1];
  2192. return (postfix == '$');
  2193. }
  2194. return 0;
  2195. }
  2196. /*==========================================
  2197. * Three-section operator
  2198. * test ? if_true : if_false
  2199. *------------------------------------------
  2200. */
  2201. void op_3(struct script_state *st) {
  2202. int flag = 0;
  2203. if( isstr(&st->stack->stack_data[st->stack->sp-3])) {
  2204. const char *str = conv_str(st,& (st->stack->stack_data[st->stack->sp-3]));
  2205. flag = str[0];
  2206. } else {
  2207. flag = conv_num(st,& (st->stack->stack_data[st->stack->sp-3]));
  2208. }
  2209. if( flag ) {
  2210. push_copy(st->stack, st->stack->sp-2 );
  2211. } else {
  2212. push_copy(st->stack, st->stack->sp-1 );
  2213. }
  2214. pop_stack(st->stack,st->stack->sp-4,st->stack->sp-1);
  2215. }
  2216. /*==========================================
  2217. * 加算演算子
  2218. *------------------------------------------
  2219. */
  2220. void op_add(struct script_state* st)
  2221. {
  2222. st->stack->sp--;
  2223. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2224. get_val(st,&(st->stack->stack_data[st->stack->sp-1]));
  2225. if(isstr(&st->stack->stack_data[st->stack->sp]) || isstr(&st->stack->stack_data[st->stack->sp-1])){
  2226. conv_str(st,&(st->stack->stack_data[st->stack->sp]));
  2227. conv_str(st,&(st->stack->stack_data[st->stack->sp-1]));
  2228. }
  2229. if(st->stack->stack_data[st->stack->sp].type==C_INT){ // ii
  2230. int *i1 = &st->stack->stack_data[st->stack->sp-1].u.num;
  2231. int *i2 = &st->stack->stack_data[st->stack->sp].u.num;
  2232. int ret = *i1 + *i2;
  2233. double ret_double = (double)*i1 + (double)*i2;
  2234. if(ret_double > INT_MAX|| ret_double < INT_MIN) {
  2235. ShowWarning("script::op_add overflow detected op:%d\n",C_ADD);
  2236. report_src(st);
  2237. ret = cap_value(ret, INT_MIN, INT_MAX);
  2238. }
  2239. *i1 = ret;
  2240. } else { // ssの予定
  2241. char *buf;
  2242. buf=(char *)aMallocA((strlen(st->stack->stack_data[st->stack->sp-1].u.str)+
  2243. strlen(st->stack->stack_data[st->stack->sp].u.str)+1)*sizeof(char));
  2244. strcpy(buf,st->stack->stack_data[st->stack->sp-1].u.str);
  2245. strcat(buf,st->stack->stack_data[st->stack->sp].u.str);
  2246. if(st->stack->stack_data[st->stack->sp-1].type==C_STR)
  2247. {
  2248. aFree(st->stack->stack_data[st->stack->sp-1].u.str);
  2249. st->stack->stack_data[st->stack->sp-1].type=C_INT;
  2250. }
  2251. if(st->stack->stack_data[st->stack->sp].type==C_STR)
  2252. {
  2253. aFree(st->stack->stack_data[st->stack->sp].u.str);
  2254. st->stack->stack_data[st->stack->sp].type=C_INT;
  2255. }
  2256. st->stack->stack_data[st->stack->sp-1].type=C_STR;
  2257. st->stack->stack_data[st->stack->sp-1].u.str=buf;
  2258. }
  2259. st->stack->stack_data[st->stack->sp-1].ref = NULL;
  2260. }
  2261. /*==========================================
  2262. * 二項演算子(文字列)
  2263. *------------------------------------------
  2264. */
  2265. void op_2str(struct script_state *st,int op,int sp1,int sp2)
  2266. {
  2267. char *s1=st->stack->stack_data[sp1].u.str,
  2268. *s2=st->stack->stack_data[sp2].u.str;
  2269. int a=0;
  2270. switch(op){
  2271. case C_EQ:
  2272. a= (strcmp(s1,s2)==0);
  2273. break;
  2274. case C_NE:
  2275. a= (strcmp(s1,s2)!=0);
  2276. break;
  2277. case C_GT:
  2278. a= (strcmp(s1,s2)> 0);
  2279. break;
  2280. case C_GE:
  2281. a= (strcmp(s1,s2)>=0);
  2282. break;
  2283. case C_LT:
  2284. a= (strcmp(s1,s2)< 0);
  2285. break;
  2286. case C_LE:
  2287. a= (strcmp(s1,s2)<=0);
  2288. break;
  2289. default:
  2290. ShowWarning("script: illegal string operator\n");
  2291. break;
  2292. }
  2293. // Because push_val() overwrite stack_data[sp1], C_STR on stack_data[sp1] won't be freed.
  2294. // So, call push_val() after freeing strings. [jA1783]
  2295. // script_pushint(st,a);
  2296. if(st->stack->stack_data[sp1].type==C_STR)
  2297. {
  2298. aFree(s1);
  2299. st->stack->stack_data[sp1].type=C_INT;
  2300. }
  2301. if(st->stack->stack_data[sp2].type==C_STR)
  2302. {
  2303. aFree(s2);
  2304. st->stack->stack_data[sp2].type=C_INT;
  2305. }
  2306. script_pushint(st,a);
  2307. }
  2308. /*==========================================
  2309. * 二項演算子(数値)
  2310. *------------------------------------------
  2311. */
  2312. void op_2num(struct script_state *st,int op,int i1,int i2)
  2313. {
  2314. int ret = 0;
  2315. double ret_double = 0;
  2316. switch(op){
  2317. case C_MOD: ret = i2 ? i1 % i2 : 0; break;
  2318. case C_AND: ret = i1 & i2; break;
  2319. case C_OR: ret = i1 | i2; break;
  2320. case C_XOR: ret = i1 ^ i2; break;
  2321. case C_LAND: ret = (i1 && i2); break;
  2322. case C_LOR: ret = (i1 || i2); break;
  2323. case C_EQ: ret = (i1 == i2); break;
  2324. case C_NE: ret = (i1 != i2); break;
  2325. case C_GT: ret = (i1 > i2); break;
  2326. case C_GE: ret = (i1 >= i2); break;
  2327. case C_LT: ret = (i1 < i2); break;
  2328. case C_LE: ret = (i1 <= i2); break;
  2329. case C_R_SHIFT: ret = i1>>i2; break;
  2330. case C_L_SHIFT: ret = i1<<i2; break;
  2331. default:
  2332. switch(op) {
  2333. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2334. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2335. case C_DIV:
  2336. if(i2 == 0) {
  2337. printf("script::op_2num division by zero.\n");
  2338. ret = INT_MAX;
  2339. ret_double = 0; // doubleの精度が怪しいのでオーバーフロー対策を飛ばす
  2340. } else {
  2341. ret = i1 / i2; ret_double = (double)i1 / (double)i2;
  2342. }
  2343. break;
  2344. }
  2345. if(ret_double > INT_MAX || ret_double < INT_MIN) {
  2346. printf("script::op_2num overflow detected op:%d\n",op);
  2347. report_src(st);
  2348. ret = (int)cap_value(ret_double,INT_MAX,INT_MIN);
  2349. }
  2350. }
  2351. script_pushint(st,ret);
  2352. }
  2353. /*==========================================
  2354. * 二項演算子
  2355. *------------------------------------------
  2356. */
  2357. void op_2(struct script_state *st,int op)
  2358. {
  2359. int i1,i2;
  2360. char *s1=NULL,*s2=NULL;
  2361. i2=pop_val(st);
  2362. if( isstr(&st->stack->stack_data[st->stack->sp]) )
  2363. s2=st->stack->stack_data[st->stack->sp].u.str;
  2364. i1=pop_val(st);
  2365. if( isstr(&st->stack->stack_data[st->stack->sp]) )
  2366. s1=st->stack->stack_data[st->stack->sp].u.str;
  2367. if( s1!=NULL && s2!=NULL ){
  2368. // ss => op_2str
  2369. op_2str(st,op,st->stack->sp,st->stack->sp+1);
  2370. }else if( s1==NULL && s2==NULL ){
  2371. // ii => op_2num
  2372. op_2num(st,op,i1,i2);
  2373. }else{
  2374. // si,is => error
  2375. ShowWarning("script: op_2: int&str, str&int not allow.\n");
  2376. report_src(st);
  2377. if(s1 && st->stack->stack_data[st->stack->sp].type == C_STR)
  2378. {
  2379. aFree(s1);
  2380. st->stack->stack_data[st->stack->sp].type = C_INT;
  2381. }
  2382. if(s2 && st->stack->stack_data[st->stack->sp+1].type == C_STR)
  2383. {
  2384. aFree(s2);
  2385. st->stack->stack_data[st->stack->sp+1].type = C_INT;
  2386. }
  2387. script_pushint(st,0);
  2388. }
  2389. }
  2390. /*==========================================
  2391. * 単項演算子
  2392. *------------------------------------------
  2393. */
  2394. void op_1num(struct script_state *st,int op)
  2395. {
  2396. int i1;
  2397. i1=pop_val(st);
  2398. switch(op){
  2399. case C_NEG:
  2400. i1=-i1;
  2401. break;
  2402. case C_NOT:
  2403. i1=~i1;
  2404. break;
  2405. case C_LNOT:
  2406. i1=!i1;
  2407. break;
  2408. }
  2409. script_pushint(st,i1);
  2410. }
  2411. /*==========================================
  2412. * 関数の実行
  2413. *------------------------------------------
  2414. */
  2415. int run_func(struct script_state *st)
  2416. {
  2417. int i,start_sp,end_sp,func;
  2418. end_sp=st->stack->sp;
  2419. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2420. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2421. if(battle_config.error_log)
  2422. ShowError("function not found\n");
  2423. // st->stack->sp=0;
  2424. st->state=END;
  2425. report_src(st);
  2426. return 1;
  2427. }
  2428. start_sp=i-1;
  2429. st->start=i-1;
  2430. st->end=end_sp;
  2431. func=st->stack->stack_data[st->start].u.num;
  2432. #ifdef DEBUG_RUN
  2433. if(battle_config.etc_log) {
  2434. 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);
  2435. ShowDebug("stack dump :");
  2436. for(i=0;i<end_sp;i++){
  2437. switch(st->stack->stack_data[i].type){
  2438. case C_INT:
  2439. printf(" int(%d)",st->stack->stack_data[i].u.num);
  2440. break;
  2441. case C_NAME:
  2442. printf(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2443. break;
  2444. case C_ARG:
  2445. printf(" arg");
  2446. break;
  2447. case C_POS:
  2448. printf(" pos(%d)",st->stack->stack_data[i].u.num);
  2449. break;
  2450. case C_STR:
  2451. printf(" str(%s)",st->stack->stack_data[i].u.str);
  2452. break;
  2453. case C_CONSTSTR:
  2454. printf(" cstr(%s)",st->stack->stack_data[i].u.str);
  2455. break;
  2456. default:
  2457. printf(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2458. }
  2459. }
  2460. printf("\n");
  2461. }
  2462. #endif
  2463. if(str_data[func].type!=C_FUNC ){
  2464. 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);
  2465. // st->stack->sp=0;
  2466. st->state=END;
  2467. report_src(st);
  2468. return 1;
  2469. }
  2470. #ifdef DEBUG_RUN
  2471. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2472. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2473. );
  2474. #endif
  2475. if(str_data[func].func){
  2476. if (str_data[func].func(st)) //Report error
  2477. report_src(st);
  2478. } else {
  2479. if(battle_config.error_log)
  2480. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2481. script_pushint(st,0);
  2482. report_src(st);
  2483. }
  2484. // Stack's datum are used when re-run functions [Eoe]
  2485. if(st->state != RERUNLINE) {
  2486. pop_stack(st->stack,start_sp,end_sp);
  2487. }
  2488. if(st->state==RETFUNC){
  2489. // ユーザー定義関数からの復帰
  2490. int olddefsp=st->stack->defsp;
  2491. int i;
  2492. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2493. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2494. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2495. st->state=END;
  2496. report_src(st);
  2497. return 1;
  2498. }
  2499. script_free_vars( st->stack->var_function );
  2500. aFree(st->stack->var_function);
  2501. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2502. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2503. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2504. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2505. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2506. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2507. st->state=GOTO;
  2508. }
  2509. return 0;
  2510. }
  2511. /*==========================================
  2512. * スクリプトの実行
  2513. *------------------------------------------
  2514. */
  2515. void run_script_main(struct script_state *st);
  2516. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2517. {
  2518. struct script_state *st;
  2519. struct map_session_data *sd=NULL;
  2520. if(rootscript==NULL || pos<0)
  2521. return;
  2522. if (rid) sd = map_id2sd(rid);
  2523. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2524. //Resume script.
  2525. st = sd->st;
  2526. } else {
  2527. st = aCalloc(sizeof(struct script_state), 1);
  2528. // the script is different, make new script_state and stack
  2529. st->stack = aMalloc (sizeof(struct script_stack));
  2530. st->stack->sp=0;
  2531. st->stack->sp_max=64;
  2532. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2533. st->stack->defsp = st->stack->sp;
  2534. st->stack->var_function = aCalloc(1, sizeof(struct linkdb_node*));
  2535. st->state = RUN;
  2536. st->script = rootscript;
  2537. }
  2538. st->pos = pos;
  2539. st->rid = rid;
  2540. st->oid = oid;
  2541. st->sleep.timer = -1;
  2542. run_script_main(st);
  2543. }
  2544. void script_stop_sleeptimers(int id)
  2545. {
  2546. struct script_state* st;
  2547. for(;;)
  2548. {
  2549. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2550. if( st == NULL )
  2551. break; // no more sleep timers
  2552. if( st->sleep.timer != INVALID_TIMER )
  2553. delete_timer(st->sleep.timer, run_script_timer);
  2554. script_free_stack(st->stack);
  2555. aFree(st);
  2556. }
  2557. }
  2558. /*==========================================
  2559. * 指定ノードをsleep_dbから削除
  2560. *------------------------------------------
  2561. */
  2562. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2563. {
  2564. struct linkdb_node *retnode;
  2565. if( n == NULL)
  2566. return NULL;
  2567. if( n->prev == NULL )
  2568. sleep_db = n->next;
  2569. else
  2570. n->prev->next = n->next;
  2571. if( n->next )
  2572. n->next->prev = n->prev;
  2573. retnode = n->next;
  2574. aFree( n );
  2575. return retnode; // 次のノードを返す
  2576. }
  2577. /*==========================================
  2578. * sleep用タイマー関数
  2579. *------------------------------------------
  2580. */
  2581. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2582. {
  2583. struct script_state *st = (struct script_state *)data;
  2584. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2585. struct map_session_data *sd = map_id2sd(st->rid);
  2586. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2587. { //Character mismatch. Cancel execution.
  2588. st->rid = 0;
  2589. st->state = END;
  2590. }
  2591. while( node && st->sleep.timer != -1 ) {
  2592. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2593. script_erase_sleepdb(node);
  2594. st->sleep.timer = -1;
  2595. break;
  2596. }
  2597. node = node->next;
  2598. }
  2599. if(st->state != RERUNLINE)
  2600. st->sleep.tick = 0;
  2601. run_script_main(st);
  2602. return 0;
  2603. }
  2604. /*==========================================
  2605. * スクリプトの実行メイン部分
  2606. *------------------------------------------
  2607. */
  2608. void run_script_main(struct script_state *st)
  2609. {
  2610. int c;
  2611. int cmdcount=script_config.check_cmdcount;
  2612. int gotocount=script_config.check_gotocount;
  2613. struct map_session_data *sd;
  2614. //For backing up purposes
  2615. struct script_state *bk_st = NULL;
  2616. int bk_npcid = 0;
  2617. struct script_stack *stack=st->stack;
  2618. sd = st->rid?map_id2sd(st->rid):NULL;
  2619. if(sd){
  2620. if(sd->st != st){
  2621. bk_st = sd->st;
  2622. bk_npcid = sd->npc_id;
  2623. }
  2624. sd->st = st;
  2625. sd->npc_id = st->oid;
  2626. }
  2627. if(st->state == RERUNLINE) {
  2628. st->state = RUN;
  2629. run_func(st);
  2630. if(st->state == GOTO)
  2631. st->state = RUN;
  2632. } else if(st->state != END)
  2633. st->state = RUN;
  2634. while(st->state == RUN){
  2635. c= get_com(st->script->script_buf,&st->pos);
  2636. switch(c){
  2637. case C_EOL:
  2638. if( stack->sp != stack->defsp )
  2639. {
  2640. if( stack->sp > stack->defsp )
  2641. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2642. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2643. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2644. } else if( battle_config.error_log )
  2645. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2646. stack->sp = stack->defsp;
  2647. }
  2648. break;
  2649. case C_INT:
  2650. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2651. break;
  2652. case C_POS:
  2653. case C_NAME:
  2654. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2655. st->pos+=3;
  2656. break;
  2657. case C_ARG:
  2658. push_val(stack,c,0);
  2659. break;
  2660. case C_STR:
  2661. push_str(stack,C_CONSTSTR,(st->script->script_buf+st->pos));
  2662. while(st->script->script_buf[st->pos++]);
  2663. break;
  2664. case C_FUNC:
  2665. run_func(st);
  2666. if(st->state==GOTO){
  2667. st->state = RUN;
  2668. if( gotocount>0 && (--gotocount)<=0 ){
  2669. ShowError("run_script: infinity loop !\n");
  2670. report_src(st);
  2671. st->state=END;
  2672. }
  2673. }
  2674. break;
  2675. case C_ADD:
  2676. op_add(st);
  2677. break;
  2678. case C_SUB:
  2679. case C_MUL:
  2680. case C_DIV:
  2681. case C_MOD:
  2682. case C_EQ:
  2683. case C_NE:
  2684. case C_GT:
  2685. case C_GE:
  2686. case C_LT:
  2687. case C_LE:
  2688. case C_AND:
  2689. case C_OR:
  2690. case C_XOR:
  2691. case C_LAND:
  2692. case C_LOR:
  2693. case C_R_SHIFT:
  2694. case C_L_SHIFT:
  2695. op_2(st,c);
  2696. break;
  2697. case C_NEG:
  2698. case C_NOT:
  2699. case C_LNOT:
  2700. op_1num(st,c);
  2701. break;
  2702. case C_OP3:
  2703. op_3(st);
  2704. break;
  2705. case C_NOP:
  2706. st->state=END;
  2707. break;
  2708. default:
  2709. if(battle_config.error_log)
  2710. ShowError("unknown command : %d @ %d\n",c,pos);
  2711. st->state=END;
  2712. break;
  2713. }
  2714. if( cmdcount>0 && (--cmdcount)<=0 ){
  2715. ShowError("run_script: infinity loop !\n");
  2716. report_src(st);
  2717. st->state=END;
  2718. }
  2719. }
  2720. if(st->sleep.tick > 0) {
  2721. //Delay execution
  2722. st->sleep.charid = sd?sd->status.char_id:0;
  2723. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2724. run_script_timer, st->sleep.charid, (int)st);
  2725. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2726. //Restore previous script
  2727. if (sd) {
  2728. sd->st = bk_st;
  2729. sd->npc_id = bk_npcid;
  2730. bk_st = NULL; //Remove tag for removal.
  2731. }
  2732. }
  2733. else if(st->state != END && sd){
  2734. //Resume later (st is already attached to player).
  2735. if(bk_st) {
  2736. ShowWarning("Unable to restore stack! Double continuation!\n");
  2737. //Report BOTH scripts to see if that can help somehow.
  2738. ShowDebug("Previous script (lost):\n");
  2739. report_src(bk_st);
  2740. ShowDebug("Current script:\n");
  2741. report_src(st);
  2742. }
  2743. } else {
  2744. //Dispose of script.
  2745. if (sd)
  2746. { //Restore previous stack and save char.
  2747. if(sd->state.using_fake_npc){
  2748. clif_clearchar_id(sd->npc_id, 0, sd->fd);
  2749. sd->state.using_fake_npc = 0;
  2750. }
  2751. //Restore previous script if any.
  2752. sd->st = bk_st;
  2753. sd->npc_id = bk_npcid;
  2754. if (!bk_st)
  2755. npc_event_dequeue(sd);
  2756. else
  2757. bk_st = NULL; //Remove tag for removal.
  2758. if (sd->state.reg_dirty&2)
  2759. intif_saveregistry(sd,2);
  2760. if (sd->state.reg_dirty&1)
  2761. intif_saveregistry(sd,1);
  2762. }
  2763. st->pos = -1;
  2764. script_free_stack (st->stack);
  2765. aFree(st);
  2766. }
  2767. if (bk_st)
  2768. { //Remove previous script
  2769. bk_st->pos = -1;
  2770. script_free_stack(bk_st->stack);
  2771. aFree(bk_st);
  2772. bk_st = NULL;
  2773. }
  2774. }
  2775. /*==========================================
  2776. * マップ変数の変更
  2777. *------------------------------------------
  2778. */
  2779. int mapreg_setreg(int num,int val)
  2780. {
  2781. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2782. int i=num>>24;
  2783. char *name=str_buf+str_data[num&0x00ffffff].str;
  2784. char tmp_str[64];
  2785. #endif
  2786. if(val!=0) {
  2787. if(idb_put(mapreg_db,num,(void*)val))
  2788. ;
  2789. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2790. else if(name[1] != '@') {
  2791. sprintf(tmp_sql,"INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%d')",mapregsql_db,mapregsql_db_varname,mapregsql_db_index,mapregsql_db_value,jstrescapecpy(tmp_str,name),i,val);
  2792. if(mysql_query(&mmysql_handle,tmp_sql)){
  2793. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2794. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2795. }
  2796. }
  2797. #endif
  2798. } else { // [zBuffer]
  2799. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2800. if(name[1] != '@') { // Remove from database because it is unused.
  2801. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_varname,name,mapregsql_db_index,i);
  2802. if(mysql_query(&mmysql_handle,tmp_sql)){
  2803. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2804. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2805. }
  2806. }
  2807. #endif
  2808. idb_remove(mapreg_db,num);
  2809. }
  2810. mapreg_dirty=1;
  2811. return 1;
  2812. }
  2813. /*==========================================
  2814. * 文字列型マップ変数の変更
  2815. *------------------------------------------
  2816. */
  2817. int mapreg_setregstr(int num,const char *str)
  2818. {
  2819. char *p;
  2820. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2821. char tmp_str[64];
  2822. char tmp_str2[512];
  2823. int i=num>>24; // [zBuffer]
  2824. char *name=str_buf+str_data[num&0x00ffffff].str;
  2825. #endif
  2826. if( str==NULL || *str==0 ){
  2827. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2828. if(name[1] != '@') {
  2829. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_varname,name,mapregsql_db_index,i);
  2830. if(mysql_query(&mmysql_handle,tmp_sql)){
  2831. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2832. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2833. }
  2834. }
  2835. #endif
  2836. idb_remove(mapregstr_db,num);
  2837. mapreg_dirty=1;
  2838. return 1;
  2839. }
  2840. p=(char *)aMallocA((strlen(str)+1)*sizeof(char));
  2841. strcpy(p,str);
  2842. if (idb_put(mapregstr_db,num,p))
  2843. ;
  2844. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2845. else if(name[1] != '@'){ //put returned null, so we must insert.
  2846. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  2847. sprintf(tmp_sql,"INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%s')",mapregsql_db,mapregsql_db_varname,mapregsql_db_index,mapregsql_db_value,jstrescapecpy(tmp_str,name),i,jstrescapecpy(tmp_str2,p));
  2848. if(mysql_query(&mmysql_handle,tmp_sql)){
  2849. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2850. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2851. }
  2852. }
  2853. #endif
  2854. mapreg_dirty=1;
  2855. return 1;
  2856. }
  2857. /*==========================================
  2858. * 永続的マップ変数の読み込み
  2859. *------------------------------------------
  2860. */
  2861. static int script_load_mapreg(void)
  2862. {
  2863. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2864. FILE *fp;
  2865. char line[1024];
  2866. if( (fp=fopen(mapreg_txt,"rt"))==NULL )
  2867. return -1;
  2868. while(fgets(line,sizeof(line),fp)){
  2869. char buf1[256],buf2[1024],*p;
  2870. int n,v,s,i;
  2871. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  2872. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  2873. continue;
  2874. if( buf1[strlen(buf1)-1]=='$' ){
  2875. if( sscanf(line+n,"%[^\n\r]",buf2)!=1 ){
  2876. ShowError("%s: %s broken data !\n",mapreg_txt,buf1);
  2877. continue;
  2878. }
  2879. p=(char *)aMallocA((strlen(buf2) + 1)*sizeof(char));
  2880. strcpy(p,buf2);
  2881. s= add_str(buf1);
  2882. idb_put(mapregstr_db,(i<<24)|s,p);
  2883. }else{
  2884. if( sscanf(line+n,"%d",&v)!=1 ){
  2885. ShowError("%s: %s broken data !\n",mapreg_txt,buf1);
  2886. continue;
  2887. }
  2888. s= add_str(buf1);
  2889. idb_put(mapreg_db,(i<<24)|s,(void*)v);
  2890. }
  2891. }
  2892. fclose(fp);
  2893. mapreg_dirty=0;
  2894. return 0;
  2895. #else
  2896. // SQL mapreg code start [zBuffer]
  2897. /*
  2898. 0 1 2
  2899. +-------------------------+
  2900. | varname | index | value |
  2901. +-------------------------+
  2902. */
  2903. unsigned int perfomance = (unsigned int)time(NULL);
  2904. sprintf(tmp_sql,"SELECT * FROM `%s`",mapregsql_db);
  2905. ShowInfo("Querying script_load_mapreg ...\n");
  2906. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  2907. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2908. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2909. return -1;
  2910. }
  2911. ShowInfo("Success! Returning results ...\n");
  2912. sql_res = mysql_store_result(&mmysql_handle);
  2913. if (sql_res) {
  2914. while ((sql_row = mysql_fetch_row(sql_res))) {
  2915. char buf1[33], *p = NULL;
  2916. int i,v,s;
  2917. strcpy(buf1,sql_row[0]);
  2918. if( buf1[strlen(buf1)-1]=='$' ){
  2919. i = atoi(sql_row[1]);
  2920. p=(char *)aMallocA((strlen(sql_row[2]) + 1)*sizeof(char));
  2921. strcpy(p,sql_row[2]);
  2922. s= add_str(buf1);
  2923. idb_put(mapregstr_db,(i<<24)|s,p);
  2924. }else{
  2925. s= add_str(buf1);
  2926. v= atoi(sql_row[2]);
  2927. i = atoi(sql_row[1]);
  2928. idb_put(mapreg_db,(i<<24)|s,(void *)v);
  2929. }
  2930. }
  2931. }
  2932. ShowInfo("Freeing results...\n");
  2933. mysql_free_result(sql_res);
  2934. mapreg_dirty=0;
  2935. perfomance = (((unsigned int)time(NULL)) - perfomance);
  2936. ShowInfo("SQL Mapreg Loading Completed Under %d Seconds.\n",perfomance);
  2937. return 0;
  2938. #endif /* TXT_ONLY */
  2939. }
  2940. /*==========================================
  2941. * 永続的マップ変数の書き込み
  2942. *------------------------------------------
  2943. */
  2944. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  2945. {
  2946. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2947. FILE *fp=va_arg(ap,FILE*);
  2948. int num=key.i&0x00ffffff, i=key.i>>24;
  2949. char *name=str_buf+str_data[num].str;
  2950. if( name[1]!='@' ){
  2951. if(i==0)
  2952. fprintf(fp,"%s\t%d\n", name, (int)data);
  2953. else
  2954. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  2955. }
  2956. return 0;
  2957. #else
  2958. int num=key.i&0x00ffffff, i=key.i>>24; // [zBuffer]
  2959. char *name=str_buf+str_data[num].str;
  2960. if ( name[1] != '@') {
  2961. sprintf(tmp_sql,"UPDATE `%s` SET `%s`='%d' WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_value,(int)data,mapregsql_db_varname,name,mapregsql_db_index,i);
  2962. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  2963. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2964. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2965. }
  2966. }
  2967. return 0;
  2968. #endif
  2969. }
  2970. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  2971. {
  2972. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2973. FILE *fp=va_arg(ap,FILE*);
  2974. int num=key.i&0x00ffffff, i=key.i>>24;
  2975. char *name=str_buf+str_data[num].str;
  2976. if( name[1]!='@' ){
  2977. if(i==0)
  2978. fprintf(fp,"%s\t%s\n", name, (char *)data);
  2979. else
  2980. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  2981. }
  2982. return 0;
  2983. #else
  2984. char tmp_str2[512];
  2985. int num=key.i&0x00ffffff, i=key.i>>24;
  2986. char *name=str_buf+str_data[num].str;
  2987. if ( name[1] != '@') {
  2988. sprintf(tmp_sql,"UPDATE `%s` SET `%s`='%s' WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_value,jstrescapecpy(tmp_str2,(char *)data),mapregsql_db_varname,name,mapregsql_db_index,i);
  2989. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  2990. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2991. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2992. }
  2993. }
  2994. return 0;
  2995. #endif
  2996. }
  2997. static int script_save_mapreg(void)
  2998. {
  2999. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3000. FILE *fp;
  3001. int lock;
  3002. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  3003. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  3004. return -1;
  3005. }
  3006. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  3007. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  3008. lock_fclose(fp,mapreg_txt,&lock);
  3009. #else
  3010. unsigned int perfomance = (unsigned int)time(NULL);
  3011. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub); // [zBuffer]
  3012. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  3013. perfomance = ((unsigned int)time(NULL) - perfomance);
  3014. if(perfomance > 2)
  3015. ShowWarning("Slow Query: MapregSQL Saving @ %d second(s).\n", perfomance);
  3016. #endif
  3017. mapreg_dirty=0;
  3018. return 0;
  3019. }
  3020. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  3021. {
  3022. if(mapreg_dirty)
  3023. if (script_save_mapreg() == -1)
  3024. ShowError("Failed to save the mapreg data!\n");
  3025. return 0;
  3026. }
  3027. /*==========================================
  3028. *
  3029. *------------------------------------------
  3030. */
  3031. static int set_posword(char *p)
  3032. {
  3033. char* np,* str[15];
  3034. int i=0;
  3035. for(i=0;i<11;i++) {
  3036. if((np=strchr(p,','))!=NULL) {
  3037. str[i]=p;
  3038. *np=0;
  3039. p=np+1;
  3040. } else {
  3041. str[i]=p;
  3042. p+=strlen(p);
  3043. }
  3044. if(str[i])
  3045. strcpy(pos[i],str[i]);
  3046. }
  3047. return 0;
  3048. }
  3049. int script_config_read_sub(char *cfgName)
  3050. {
  3051. int i;
  3052. char line[1024],w1[1024],w2[1024];
  3053. FILE *fp;
  3054. fp=fopen(cfgName,"r");
  3055. if(fp==NULL){
  3056. ShowError("file not found: [%s]\n", cfgName);
  3057. return 1;
  3058. }
  3059. while(fgets(line,sizeof(line)-1,fp)){
  3060. if(line[0] == '/' && line[1] == '/')
  3061. continue;
  3062. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3063. if(i!=2)
  3064. continue;
  3065. if(strcmpi(w1,"refine_posword")==0) {
  3066. set_posword(w2);
  3067. }
  3068. else if(strcmpi(w1,"verbose_mode")==0) {
  3069. script_config.verbose_mode = config_switch(w2);
  3070. }
  3071. else if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3072. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3073. }
  3074. else if(strcmpi(w1,"check_cmdcount")==0) {
  3075. script_config.check_cmdcount = config_switch(w2);
  3076. }
  3077. else if(strcmpi(w1,"check_gotocount")==0) {
  3078. script_config.check_gotocount = config_switch(w2);
  3079. }
  3080. else if(strcmpi(w1,"event_script_type")==0) {
  3081. script_config.event_script_type = config_switch(w2);
  3082. }
  3083. else if(strcmpi(w1,"event_requires_trigger")==0) {
  3084. script_config.event_requires_trigger = config_switch(w2);
  3085. }
  3086. else if(strcmpi(w1,"die_event_name")==0) {
  3087. strncpy(script_config.die_event_name, w2, NAME_LENGTH-1);
  3088. if (strlen(script_config.die_event_name) != strlen(w2))
  3089. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.die_event_name);
  3090. }
  3091. else if(strcmpi(w1,"kill_pc_event_name")==0) {
  3092. strncpy(script_config.kill_pc_event_name, w2, NAME_LENGTH-1);
  3093. if (strlen(script_config.kill_pc_event_name) != strlen(w2))
  3094. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_pc_event_name);
  3095. }
  3096. else if(strcmpi(w1,"kill_mob_event_name")==0) {
  3097. strncpy(script_config.kill_mob_event_name, w2, NAME_LENGTH-1);
  3098. if (strlen(script_config.kill_mob_event_name) != strlen(w2))
  3099. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_mob_event_name);
  3100. }
  3101. else if(strcmpi(w1,"login_event_name")==0) {
  3102. strncpy(script_config.login_event_name, w2, NAME_LENGTH-1);
  3103. if (strlen(script_config.login_event_name) != strlen(w2))
  3104. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.login_event_name);
  3105. }
  3106. else if(strcmpi(w1,"logout_event_name")==0) {
  3107. strncpy(script_config.logout_event_name, w2, NAME_LENGTH-1);
  3108. if (strlen(script_config.logout_event_name) != strlen(w2))
  3109. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.logout_event_name);
  3110. }
  3111. else if(strcmpi(w1,"loadmap_event_name")==0) {
  3112. strncpy(script_config.loadmap_event_name, w2, NAME_LENGTH-1);
  3113. if (strlen(script_config.loadmap_event_name) != strlen(w2))
  3114. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.loadmap_event_name);
  3115. }
  3116. else if(strcmpi(w1,"baselvup_event_name")==0) {
  3117. strncpy(script_config.baselvup_event_name, w2, NAME_LENGTH-1);
  3118. if (strlen(script_config.baselvup_event_name) != strlen(w2))
  3119. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.baselvup_event_name);
  3120. }
  3121. else if(strcmpi(w1,"joblvup_event_name")==0) {
  3122. strncpy(script_config.joblvup_event_name, w2, NAME_LENGTH-1);
  3123. if (strlen(script_config.joblvup_event_name) != strlen(w2))
  3124. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.joblvup_event_name);
  3125. }
  3126. else if(strcmpi(w1,"import")==0){
  3127. script_config_read_sub(w2);
  3128. }
  3129. }
  3130. fclose(fp);
  3131. return 0;
  3132. }
  3133. int script_config_read(char *cfgName)
  3134. { //Script related variables should be initialized once! [Skotlex]
  3135. memset (&script_config, 0, sizeof(script_config));
  3136. script_config.verbose_mode = 0;
  3137. script_config.warn_func_mismatch_paramnum = 1;
  3138. script_config.check_cmdcount = 65535;
  3139. script_config.check_gotocount = 2048;
  3140. script_config.event_script_type = 0;
  3141. script_config.event_requires_trigger = 1;
  3142. return script_config_read_sub(cfgName);
  3143. }
  3144. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3145. {
  3146. struct script_code *code = (struct script_code *)data;
  3147. if(code){
  3148. script_free_vars( &code->script_vars );
  3149. aFree( code->script_buf );
  3150. }
  3151. return 0;
  3152. }
  3153. /*==========================================
  3154. * 終了
  3155. *------------------------------------------
  3156. */
  3157. int do_final_script()
  3158. {
  3159. #ifdef DEBUG_RUN
  3160. if (battle_config.etc_log)
  3161. {
  3162. FILE *fp = fopen("hash_dump.txt","wt");
  3163. if(fp) {
  3164. int i,count[SCRIPT_HASH_SIZE];
  3165. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3166. int n=0;
  3167. int min=INT_MAX,max=0,zero=0;
  3168. double mean=0.0f;
  3169. double median=0.0f;
  3170. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3171. memset(count, 0, sizeof(count));
  3172. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3173. fprintf(fp,"---------------------------------------------------------------\n");
  3174. for(i=LABEL_START; i<str_num; i++) {
  3175. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3176. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3177. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3178. ++count[h];
  3179. }
  3180. fprintf(fp,"--------------------\n\n");
  3181. memset(count2, 0, sizeof(count2));
  3182. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3183. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3184. if(min > count[i])
  3185. min = count[i]; // minimun count of collision
  3186. if(max < count[i])
  3187. max = count[i]; // maximun count of collision
  3188. if(count[i] == 0)
  3189. zero++;
  3190. ++count2[count[i]];
  3191. }
  3192. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3193. for( i=min; i <= max; ++i ){
  3194. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3195. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3196. }
  3197. for( i=min; i <= max; ++i ){
  3198. n += count2[i];
  3199. if( n*2 >= SCRIPT_HASH_SIZE )
  3200. {
  3201. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3202. median = (i+i+1)/2.0f;
  3203. else
  3204. median = i;
  3205. break;
  3206. }
  3207. }
  3208. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3209. fclose(fp);
  3210. }
  3211. }
  3212. #endif
  3213. if(mapreg_dirty>=0)
  3214. script_save_mapreg();
  3215. mapreg_db->destroy(mapreg_db,NULL);
  3216. mapregstr_db->destroy(mapregstr_db,NULL);
  3217. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3218. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3219. if(sleep_db) {
  3220. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3221. while(n) {
  3222. struct script_state *st = (struct script_state *)n->data;
  3223. script_free_stack(st->stack);
  3224. aFree(st);
  3225. n = n->next;
  3226. }
  3227. linkdb_final(&sleep_db);
  3228. }
  3229. if (str_data)
  3230. aFree(str_data);
  3231. if (str_buf)
  3232. aFree(str_buf);
  3233. return 0;
  3234. }
  3235. /*==========================================
  3236. * 初期化
  3237. *------------------------------------------
  3238. */
  3239. int do_init_script()
  3240. {
  3241. mapreg_db= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_BASE,sizeof(int));
  3242. mapregstr_db=db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3243. userfunc_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_RELEASE_BOTH,50);
  3244. scriptlabel_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA,50);
  3245. script_load_mapreg();
  3246. add_timer_func_list(script_autosave_mapreg,"script_autosave_mapreg");
  3247. add_timer_interval(gettick()+MAPREG_AUTOSAVE_INTERVAL,
  3248. script_autosave_mapreg,0,0,MAPREG_AUTOSAVE_INTERVAL);
  3249. return 0;
  3250. }
  3251. int script_reload()
  3252. {
  3253. if(mapreg_dirty>=0)
  3254. script_save_mapreg();
  3255. mapreg_db->clear(mapreg_db, NULL);
  3256. mapregstr_db->clear(mapregstr_db, NULL);
  3257. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3258. scriptlabel_db->clear(scriptlabel_db, NULL);
  3259. if(sleep_db) {
  3260. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3261. while(n) {
  3262. struct script_state *st = (struct script_state *)n->data;
  3263. if( st->sleep.timer != INVALID_TIMER )
  3264. delete_timer(st->sleep.timer, run_script_timer);
  3265. script_free_stack(st->stack);
  3266. aFree(st);
  3267. n = n->next;
  3268. }
  3269. linkdb_final(&sleep_db);
  3270. }
  3271. script_load_mapreg();
  3272. return 0;
  3273. }
  3274. //-----------------------------------------------------------------------------
  3275. // buildin functions
  3276. //
  3277. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3278. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3279. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3280. BUILDIN_FUNC(mes);
  3281. BUILDIN_FUNC(goto);
  3282. BUILDIN_FUNC(callsub);
  3283. BUILDIN_FUNC(callfunc);
  3284. BUILDIN_FUNC(return);
  3285. BUILDIN_FUNC(getarg);
  3286. BUILDIN_FUNC(next);
  3287. BUILDIN_FUNC(close);
  3288. BUILDIN_FUNC(close2);
  3289. BUILDIN_FUNC(menu);
  3290. BUILDIN_FUNC(rand);
  3291. BUILDIN_FUNC(warp);
  3292. BUILDIN_FUNC(areawarp);
  3293. BUILDIN_FUNC(warpchar); // [LuzZza]
  3294. BUILDIN_FUNC(warpparty); //[Fredzilla]
  3295. BUILDIN_FUNC(warpguild); //[Fredzilla]
  3296. BUILDIN_FUNC(heal);
  3297. BUILDIN_FUNC(itemheal);
  3298. BUILDIN_FUNC(percentheal);
  3299. BUILDIN_FUNC(jobchange);
  3300. BUILDIN_FUNC(jobname);
  3301. BUILDIN_FUNC(input);
  3302. BUILDIN_FUNC(setlook);
  3303. BUILDIN_FUNC(set);
  3304. BUILDIN_FUNC(setarray);
  3305. BUILDIN_FUNC(cleararray);
  3306. BUILDIN_FUNC(copyarray);
  3307. BUILDIN_FUNC(getarraysize);
  3308. BUILDIN_FUNC(deletearray);
  3309. BUILDIN_FUNC(getelementofarray);
  3310. BUILDIN_FUNC(getitem);
  3311. BUILDIN_FUNC(getitem2);
  3312. BUILDIN_FUNC(getnameditem);
  3313. BUILDIN_FUNC(grouprandomitem);
  3314. BUILDIN_FUNC(makeitem);
  3315. BUILDIN_FUNC(delitem);
  3316. BUILDIN_FUNC(delitem2);
  3317. BUILDIN_FUNC(enableitemuse);
  3318. BUILDIN_FUNC(disableitemuse);
  3319. BUILDIN_FUNC(viewpoint);
  3320. BUILDIN_FUNC(countitem);
  3321. BUILDIN_FUNC(countitem2);
  3322. BUILDIN_FUNC(checkweight);
  3323. BUILDIN_FUNC(readparam);
  3324. BUILDIN_FUNC(getcharid);
  3325. BUILDIN_FUNC(getpartyname);
  3326. BUILDIN_FUNC(getpartymember);
  3327. BUILDIN_FUNC(getpartyleader);
  3328. BUILDIN_FUNC(getguildname);
  3329. BUILDIN_FUNC(getguildmaster);
  3330. BUILDIN_FUNC(getguildmasterid);
  3331. BUILDIN_FUNC(strcharinfo);
  3332. BUILDIN_FUNC(getequipid);
  3333. BUILDIN_FUNC(getequipname);
  3334. BUILDIN_FUNC(getbrokenid); // [Valaris]
  3335. BUILDIN_FUNC(repair); // [Valaris]
  3336. BUILDIN_FUNC(getequipisequiped);
  3337. BUILDIN_FUNC(getequipisenableref);
  3338. BUILDIN_FUNC(getequipisidentify);
  3339. BUILDIN_FUNC(getequiprefinerycnt);
  3340. BUILDIN_FUNC(getequipweaponlv);
  3341. BUILDIN_FUNC(getequippercentrefinery);
  3342. BUILDIN_FUNC(successrefitem);
  3343. BUILDIN_FUNC(failedrefitem);
  3344. BUILDIN_FUNC(cutin);
  3345. BUILDIN_FUNC(statusup);
  3346. BUILDIN_FUNC(statusup2);
  3347. BUILDIN_FUNC(bonus);
  3348. BUILDIN_FUNC(bonus2);
  3349. BUILDIN_FUNC(bonus3);
  3350. BUILDIN_FUNC(bonus4);
  3351. BUILDIN_FUNC(bonus5);
  3352. BUILDIN_FUNC(skill);
  3353. BUILDIN_FUNC(addtoskill); // [Valaris]
  3354. BUILDIN_FUNC(guildskill);
  3355. BUILDIN_FUNC(getskilllv);
  3356. BUILDIN_FUNC(getgdskilllv);
  3357. BUILDIN_FUNC(basicskillcheck);
  3358. BUILDIN_FUNC(getgmlevel);
  3359. BUILDIN_FUNC(end);
  3360. BUILDIN_FUNC(checkoption);
  3361. BUILDIN_FUNC(setoption);
  3362. BUILDIN_FUNC(setcart);
  3363. BUILDIN_FUNC(checkcart); // check cart [Valaris]
  3364. BUILDIN_FUNC(setfalcon);
  3365. BUILDIN_FUNC(checkfalcon); // check falcon [Valaris]
  3366. BUILDIN_FUNC(setriding);
  3367. BUILDIN_FUNC(checkriding); // check for pecopeco [Valaris]
  3368. BUILDIN_FUNC(savepoint);
  3369. BUILDIN_FUNC(gettimetick);
  3370. BUILDIN_FUNC(gettime);
  3371. BUILDIN_FUNC(gettimestr);
  3372. BUILDIN_FUNC(openstorage);
  3373. BUILDIN_FUNC(guildopenstorage);
  3374. BUILDIN_FUNC(itemskill);
  3375. BUILDIN_FUNC(produce);
  3376. BUILDIN_FUNC(monster);
  3377. BUILDIN_FUNC(areamonster);
  3378. BUILDIN_FUNC(killmonster);
  3379. BUILDIN_FUNC(killmonsterall);
  3380. BUILDIN_FUNC(clone);
  3381. BUILDIN_FUNC(doevent);
  3382. BUILDIN_FUNC(donpcevent);
  3383. BUILDIN_FUNC(addtimer);
  3384. BUILDIN_FUNC(deltimer);
  3385. BUILDIN_FUNC(addtimercount);
  3386. BUILDIN_FUNC(initnpctimer);
  3387. BUILDIN_FUNC(stopnpctimer);
  3388. BUILDIN_FUNC(startnpctimer);
  3389. BUILDIN_FUNC(setnpctimer);
  3390. BUILDIN_FUNC(getnpctimer);
  3391. BUILDIN_FUNC(attachnpctimer); // [celest]
  3392. BUILDIN_FUNC(detachnpctimer); // [celest]
  3393. BUILDIN_FUNC(playerattached); // [Skotlex]
  3394. BUILDIN_FUNC(announce);
  3395. BUILDIN_FUNC(mapannounce);
  3396. BUILDIN_FUNC(areaannounce);
  3397. BUILDIN_FUNC(getusers);
  3398. BUILDIN_FUNC(getmapguildusers);
  3399. BUILDIN_FUNC(getmapusers);
  3400. BUILDIN_FUNC(getareausers);
  3401. BUILDIN_FUNC(getareadropitem);
  3402. BUILDIN_FUNC(enablenpc);
  3403. BUILDIN_FUNC(disablenpc);
  3404. BUILDIN_FUNC(enablearena); // Added by RoVeRT
  3405. BUILDIN_FUNC(disablearena); // Added by RoVeRT
  3406. BUILDIN_FUNC(hideoffnpc);
  3407. BUILDIN_FUNC(hideonnpc);
  3408. BUILDIN_FUNC(sc_start);
  3409. BUILDIN_FUNC(sc_start2);
  3410. BUILDIN_FUNC(sc_start4);
  3411. BUILDIN_FUNC(sc_end);
  3412. BUILDIN_FUNC(getscrate);
  3413. BUILDIN_FUNC(debugmes);
  3414. BUILDIN_FUNC(catchpet);
  3415. BUILDIN_FUNC(birthpet);
  3416. BUILDIN_FUNC(resetlvl);
  3417. BUILDIN_FUNC(resetstatus);
  3418. BUILDIN_FUNC(resetskill);
  3419. BUILDIN_FUNC(skillpointcount);
  3420. BUILDIN_FUNC(changebase);
  3421. BUILDIN_FUNC(changesex);
  3422. BUILDIN_FUNC(waitingroom);
  3423. BUILDIN_FUNC(delwaitingroom);
  3424. BUILDIN_FUNC(waitingroomkickall);
  3425. BUILDIN_FUNC(enablewaitingroomevent);
  3426. BUILDIN_FUNC(disablewaitingroomevent);
  3427. BUILDIN_FUNC(getwaitingroomstate);
  3428. BUILDIN_FUNC(warpwaitingpc);
  3429. BUILDIN_FUNC(attachrid);
  3430. BUILDIN_FUNC(detachrid);
  3431. BUILDIN_FUNC(isloggedin);
  3432. BUILDIN_FUNC(setmapflagnosave);
  3433. BUILDIN_FUNC(setmapflag);
  3434. BUILDIN_FUNC(removemapflag);
  3435. BUILDIN_FUNC(pvpon);
  3436. BUILDIN_FUNC(pvpoff);
  3437. BUILDIN_FUNC(gvgon);
  3438. BUILDIN_FUNC(gvgoff);
  3439. BUILDIN_FUNC(emotion);
  3440. BUILDIN_FUNC(maprespawnguildid);
  3441. BUILDIN_FUNC(agitstart); // <Agit>
  3442. BUILDIN_FUNC(agitend);
  3443. BUILDIN_FUNC(agitcheck); // <Agitcheck>
  3444. BUILDIN_FUNC(flagemblem); // Flag Emblem
  3445. BUILDIN_FUNC(getcastlename);
  3446. BUILDIN_FUNC(getcastledata);
  3447. BUILDIN_FUNC(setcastledata);
  3448. BUILDIN_FUNC(requestguildinfo);
  3449. BUILDIN_FUNC(getequipcardcnt);
  3450. BUILDIN_FUNC(successremovecards);
  3451. BUILDIN_FUNC(failedremovecards);
  3452. BUILDIN_FUNC(marriage);
  3453. BUILDIN_FUNC(wedding_effect);
  3454. BUILDIN_FUNC(divorce);
  3455. BUILDIN_FUNC(ispartneron); // MouseJstr
  3456. BUILDIN_FUNC(getpartnerid); // MouseJstr
  3457. BUILDIN_FUNC(getchildid); // Skotlex
  3458. BUILDIN_FUNC(getmotherid); // Lupus
  3459. BUILDIN_FUNC(getfatherid); // Lupus
  3460. BUILDIN_FUNC(warppartner); // MouseJstr
  3461. BUILDIN_FUNC(getitemname);
  3462. BUILDIN_FUNC(getitemslots);
  3463. BUILDIN_FUNC(makepet);
  3464. BUILDIN_FUNC(getexp);
  3465. BUILDIN_FUNC(getinventorylist);
  3466. BUILDIN_FUNC(getskilllist);
  3467. BUILDIN_FUNC(clearitem);
  3468. BUILDIN_FUNC(classchange);
  3469. BUILDIN_FUNC(misceffect);
  3470. BUILDIN_FUNC(soundeffect);
  3471. BUILDIN_FUNC(soundeffectall);
  3472. BUILDIN_FUNC(setcastledata);
  3473. BUILDIN_FUNC(mapwarp);
  3474. BUILDIN_FUNC(inittimer);
  3475. BUILDIN_FUNC(stoptimer);
  3476. BUILDIN_FUNC(cmdothernpc);
  3477. BUILDIN_FUNC(mobcount);
  3478. BUILDIN_FUNC(strmobinfo); // Script for displaying mob info [Valaris]
  3479. BUILDIN_FUNC(guardian); // Script for displaying mob info [Valaris]
  3480. BUILDIN_FUNC(guardianinfo); // Script for displaying mob info [Valaris]
  3481. BUILDIN_FUNC(petskillbonus); // petskillbonus [Valaris]
  3482. BUILDIN_FUNC(petrecovery); // pet skill for curing status [Valaris]
  3483. BUILDIN_FUNC(petloot); // pet looting [Valaris]
  3484. BUILDIN_FUNC(petheal); // pet healing [Valaris]
  3485. //BUILDIN_FUNC(petmag); // pet magnificat [Valaris]
  3486. BUILDIN_FUNC(petskillattack); // pet skill attacks [Skotlex]
  3487. BUILDIN_FUNC(petskillattack2); // pet skill attacks [Skotlex]
  3488. BUILDIN_FUNC(petskillsupport); // pet support skill [Valaris]
  3489. BUILDIN_FUNC(skilleffect); // skill effects [Celest]
  3490. BUILDIN_FUNC(npcskilleffect); // skill effects for npcs [Valaris]
  3491. BUILDIN_FUNC(specialeffect); // special effect script [Valaris]
  3492. BUILDIN_FUNC(specialeffect2); // special effect script [Valaris]
  3493. BUILDIN_FUNC(nude); // nude [Valaris]
  3494. BUILDIN_FUNC(atcommand); // [MouseJstr]
  3495. BUILDIN_FUNC(charcommand); // [MouseJstr]
  3496. BUILDIN_FUNC(movenpc); // [MouseJstr]
  3497. BUILDIN_FUNC(message); // [MouseJstr]
  3498. BUILDIN_FUNC(npctalk); // [Valaris]
  3499. BUILDIN_FUNC(hasitems); // [Valaris]
  3500. BUILDIN_FUNC(getlook); //Lorky [Lupus]
  3501. BUILDIN_FUNC(getsavepoint); //Lorky [Lupus]
  3502. BUILDIN_FUNC(npcspeed); // [Valaris]
  3503. BUILDIN_FUNC(npcwalkto); // [Valaris]
  3504. BUILDIN_FUNC(npcstop); // [Valaris]
  3505. BUILDIN_FUNC(getmapxy); //get map position for player/npc/pet/mob by Lorky [Lupus]
  3506. BUILDIN_FUNC(checkoption1); // [celest]
  3507. BUILDIN_FUNC(checkoption2); // [celest]
  3508. BUILDIN_FUNC(guildgetexp); // [celest]
  3509. BUILDIN_FUNC(guildchangegm); // [Skotlex]
  3510. BUILDIN_FUNC(logmes); // [Lupus]
  3511. BUILDIN_FUNC(summon); // [celest]
  3512. BUILDIN_FUNC(isnight); // [celest]
  3513. BUILDIN_FUNC(isday); // [celest]
  3514. BUILDIN_FUNC(isequipped); // [celest]
  3515. BUILDIN_FUNC(isequippedcnt); // [celest]
  3516. BUILDIN_FUNC(cardscnt); // [Lupus]
  3517. BUILDIN_FUNC(getrefine); // [celest]
  3518. BUILDIN_FUNC(adopt);
  3519. BUILDIN_FUNC(night);
  3520. BUILDIN_FUNC(day);
  3521. BUILDIN_FUNC(getusersname); //jA commands added [Lupus]
  3522. BUILDIN_FUNC(dispbottom);
  3523. BUILDIN_FUNC(recovery);
  3524. BUILDIN_FUNC(getpetinfo);
  3525. BUILDIN_FUNC(checkequipedcard);
  3526. BUILDIN_FUNC(globalmes);
  3527. BUILDIN_FUNC(jump_zero);
  3528. BUILDIN_FUNC(select);
  3529. BUILDIN_FUNC(prompt);
  3530. BUILDIN_FUNC(getmapmobs); //jA addition end
  3531. BUILDIN_FUNC(unequip); // unequip [Spectre]
  3532. BUILDIN_FUNC(getstrlen); //strlen [valaris]
  3533. BUILDIN_FUNC(charisalpha);//isalpha [valaris]
  3534. BUILDIN_FUNC(fakenpcname); // [Lance]
  3535. BUILDIN_FUNC(compare); // Lordalfa, to bring strstr to Scripting Engine
  3536. BUILDIN_FUNC(getiteminfo); //[Lupus] returns Items Buy / sell Price, etc info
  3537. BUILDIN_FUNC(setiteminfo); //[Lupus] set Items Buy / sell Price, etc info
  3538. BUILDIN_FUNC(getequipcardid); //[Lupus] returns card id from quipped item card slot N
  3539. // [zBuffer] List of mathematics commands --->
  3540. BUILDIN_FUNC(sqrt);
  3541. BUILDIN_FUNC(pow);
  3542. BUILDIN_FUNC(distance);
  3543. BUILDIN_FUNC(checkcell);
  3544. // <--- [zBuffer] List of mathematics commands
  3545. // [zBuffer] List of dynamic var commands --->
  3546. BUILDIN_FUNC(getd);
  3547. BUILDIN_FUNC(setd);
  3548. // <--- [zBuffer] List of dynamic var commands
  3549. BUILDIN_FUNC(petstat); // [Lance] Pet Stat Rq: Dubby
  3550. BUILDIN_FUNC(callshop); // [Skotlex]
  3551. BUILDIN_FUNC(npcshopitem); // [Lance]
  3552. BUILDIN_FUNC(npcshopadditem);
  3553. BUILDIN_FUNC(npcshopdelitem);
  3554. BUILDIN_FUNC(npcshopattach);
  3555. BUILDIN_FUNC(equip);
  3556. BUILDIN_FUNC(autoequip);
  3557. BUILDIN_FUNC(setbattleflag);
  3558. BUILDIN_FUNC(getbattleflag);
  3559. BUILDIN_FUNC(query_sql);
  3560. BUILDIN_FUNC(escape_sql);
  3561. BUILDIN_FUNC(atoi);
  3562. BUILDIN_FUNC(axtoi);
  3563. // [zBuffer] List of player cont commands --->
  3564. BUILDIN_FUNC(rid2name);
  3565. BUILDIN_FUNC(pcfollow);
  3566. BUILDIN_FUNC(pcstopfollow);
  3567. BUILDIN_FUNC(pcblockmove);
  3568. // <--- [zBuffer] List of player cont commands
  3569. // [zBuffer] List of mob control commands --->
  3570. BUILDIN_FUNC(mobspawn);
  3571. BUILDIN_FUNC(mobremove);
  3572. BUILDIN_FUNC(getmobdata);
  3573. BUILDIN_FUNC(setmobdata);
  3574. BUILDIN_FUNC(mobassist);
  3575. BUILDIN_FUNC(mobattach);
  3576. BUILDIN_FUNC(unitwalk);
  3577. BUILDIN_FUNC(unitkill);
  3578. BUILDIN_FUNC(unitwarp);
  3579. BUILDIN_FUNC(unitattack);
  3580. BUILDIN_FUNC(unitstop);
  3581. BUILDIN_FUNC(unittalk);
  3582. BUILDIN_FUNC(unitemote);
  3583. BUILDIN_FUNC(unitdeadsit);
  3584. BUILDIN_FUNC(unitskilluseid); // originally by Qamera [celest]
  3585. BUILDIN_FUNC(unitskillusepos); // originally by Qamera [celest]
  3586. // <--- [zBuffer] List of mob control commands
  3587. BUILDIN_FUNC(sleep);
  3588. BUILDIN_FUNC(sleep2);
  3589. BUILDIN_FUNC(awake);
  3590. BUILDIN_FUNC(getvariableofnpc);
  3591. // [blackhole89] -->
  3592. BUILDIN_FUNC(warpportal);
  3593. // <-- [blackhole89]
  3594. BUILDIN_FUNC(homunculus_evolution) ; //[orn]
  3595. BUILDIN_FUNC(eaclass);
  3596. BUILDIN_FUNC(roclass);
  3597. BUILDIN_FUNC(setitemscript);
  3598. BUILDIN_FUNC(disguise);
  3599. BUILDIN_FUNC(undisguise);
  3600. BUILDIN_FUNC(getmonsterinfo); // [Lupus]
  3601. BUILDIN_FUNC(checkvending); // check vending [Nab4]
  3602. BUILDIN_FUNC(checkchatting); // check chatting [Marka]
  3603. #ifdef PCRE_SUPPORT
  3604. BUILDIN_FUNC(defpattern); // MouseJstr
  3605. BUILDIN_FUNC(activatepset); // MouseJstr
  3606. BUILDIN_FUNC(deactivatepset); // MouseJstr
  3607. BUILDIN_FUNC(deletepset); // MouseJstr
  3608. #endif
  3609. struct script_function buildin_func[] = {
  3610. BUILDIN_DEF(mes,"s"),
  3611. BUILDIN_DEF(next,""),
  3612. BUILDIN_DEF(close,""),
  3613. BUILDIN_DEF(close2,""),
  3614. BUILDIN_DEF(menu,"*"),
  3615. BUILDIN_DEF(goto,"l"),
  3616. BUILDIN_DEF(callsub,"i*"),
  3617. BUILDIN_DEF(callfunc,"s*"),
  3618. BUILDIN_DEF(return,"*"),
  3619. BUILDIN_DEF(getarg,"i"),
  3620. BUILDIN_DEF(jobchange,"i*"),
  3621. BUILDIN_DEF(jobname,"i"),
  3622. BUILDIN_DEF(input,"v"),
  3623. BUILDIN_DEF(warp,"sii"),
  3624. BUILDIN_DEF(areawarp,"siiiisii"),
  3625. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  3626. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  3627. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  3628. BUILDIN_DEF(setlook,"ii"),
  3629. BUILDIN_DEF(set,"ii"),
  3630. BUILDIN_DEF(setarray,"ii*"),
  3631. BUILDIN_DEF(cleararray,"iii"),
  3632. BUILDIN_DEF(copyarray,"iii"),
  3633. BUILDIN_DEF(getarraysize,"i"),
  3634. BUILDIN_DEF(deletearray,"ii"),
  3635. BUILDIN_DEF(getelementofarray,"ii"),
  3636. BUILDIN_DEF(getitem,"vi?"),
  3637. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  3638. BUILDIN_DEF(getnameditem,"is"),
  3639. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  3640. BUILDIN_DEF(makeitem,"iisii"),
  3641. BUILDIN_DEF(delitem,"ii"),
  3642. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  3643. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  3644. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  3645. BUILDIN_DEF(cutin,"si"),
  3646. BUILDIN_DEF(viewpoint,"iiiii"),
  3647. BUILDIN_DEF(heal,"ii"),
  3648. BUILDIN_DEF(itemheal,"ii"),
  3649. BUILDIN_DEF(percentheal,"ii"),
  3650. BUILDIN_DEF(rand,"i?"),
  3651. BUILDIN_DEF(countitem,"i"),
  3652. BUILDIN_DEF(countitem2,"iiiiiiii"),
  3653. BUILDIN_DEF(checkweight,"ii"),
  3654. BUILDIN_DEF(readparam,"i*"),
  3655. BUILDIN_DEF(getcharid,"i*"),
  3656. BUILDIN_DEF(getpartyname,"i"),
  3657. BUILDIN_DEF(getpartymember,"i*"),
  3658. BUILDIN_DEF(getpartyleader,"i?"),
  3659. BUILDIN_DEF(getguildname,"i"),
  3660. BUILDIN_DEF(getguildmaster,"i"),
  3661. BUILDIN_DEF(getguildmasterid,"i"),
  3662. BUILDIN_DEF(strcharinfo,"i"),
  3663. BUILDIN_DEF(getequipid,"i"),
  3664. BUILDIN_DEF(getequipname,"i"),
  3665. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  3666. BUILDIN_DEF(repair,"i"), // [Valaris]
  3667. BUILDIN_DEF(getequipisequiped,"i"),
  3668. BUILDIN_DEF(getequipisenableref,"i"),
  3669. BUILDIN_DEF(getequipisidentify,"i"),
  3670. BUILDIN_DEF(getequiprefinerycnt,"i"),
  3671. BUILDIN_DEF(getequipweaponlv,"i"),
  3672. BUILDIN_DEF(getequippercentrefinery,"i"),
  3673. BUILDIN_DEF(successrefitem,"i"),
  3674. BUILDIN_DEF(failedrefitem,"i"),
  3675. BUILDIN_DEF(statusup,"i"),
  3676. BUILDIN_DEF(statusup2,"ii"),
  3677. BUILDIN_DEF(bonus,"ii"),
  3678. BUILDIN_DEF2(bonus,"bonus2","iii"),
  3679. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  3680. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  3681. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  3682. BUILDIN_DEF(skill,"ii?"),
  3683. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  3684. BUILDIN_DEF(guildskill,"ii"),
  3685. BUILDIN_DEF(getskilllv,"i"),
  3686. BUILDIN_DEF(getgdskilllv,"ii"),
  3687. BUILDIN_DEF(basicskillcheck,""),
  3688. BUILDIN_DEF(getgmlevel,""),
  3689. BUILDIN_DEF(end,""),
  3690. // BUILDIN_DEF2(end,"break",""), this might confuse advanced scripting support [Eoe]
  3691. BUILDIN_DEF(checkoption,"i"),
  3692. BUILDIN_DEF(setoption,"i?"),
  3693. BUILDIN_DEF(setcart,"?"),
  3694. BUILDIN_DEF(checkcart,""),
  3695. BUILDIN_DEF(setfalcon,"?"),
  3696. BUILDIN_DEF(checkfalcon,""),
  3697. BUILDIN_DEF(setriding,"?"),
  3698. BUILDIN_DEF(checkriding,""),
  3699. BUILDIN_DEF2(savepoint,"save","sii"),
  3700. BUILDIN_DEF(savepoint,"sii"),
  3701. BUILDIN_DEF(gettimetick,"i"),
  3702. BUILDIN_DEF(gettime,"i"),
  3703. BUILDIN_DEF(gettimestr,"si"),
  3704. BUILDIN_DEF(openstorage,""),
  3705. BUILDIN_DEF(guildopenstorage,"*"),
  3706. BUILDIN_DEF(itemskill,"ii"),
  3707. BUILDIN_DEF(produce,"i"),
  3708. BUILDIN_DEF(monster,"siisii*"),
  3709. BUILDIN_DEF(areamonster,"siiiisii*"),
  3710. BUILDIN_DEF(killmonster,"ss"),
  3711. BUILDIN_DEF(killmonsterall,"s"),
  3712. BUILDIN_DEF(clone,"siisi*"),
  3713. BUILDIN_DEF(doevent,"s"),
  3714. BUILDIN_DEF(donpcevent,"s"),
  3715. BUILDIN_DEF(addtimer,"is"),
  3716. BUILDIN_DEF(deltimer,"s"),
  3717. BUILDIN_DEF(addtimercount,"si"),
  3718. BUILDIN_DEF(initnpctimer,"??"),
  3719. BUILDIN_DEF(stopnpctimer,"??"),
  3720. BUILDIN_DEF(startnpctimer,"??"),
  3721. BUILDIN_DEF(setnpctimer,"i?"),
  3722. BUILDIN_DEF(getnpctimer,"i?"),
  3723. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  3724. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  3725. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  3726. BUILDIN_DEF(announce,"si*"),
  3727. BUILDIN_DEF(mapannounce,"ssi*"),
  3728. BUILDIN_DEF(areaannounce,"siiiisi*"),
  3729. BUILDIN_DEF(getusers,"i"),
  3730. BUILDIN_DEF(getmapguildusers,"si"),
  3731. BUILDIN_DEF(getmapusers,"s"),
  3732. BUILDIN_DEF(getareausers,"siiii"),
  3733. BUILDIN_DEF(getareadropitem,"siiiii"),
  3734. BUILDIN_DEF(enablenpc,"s"),
  3735. BUILDIN_DEF(disablenpc,"s"),
  3736. BUILDIN_DEF(enablearena,""), // Added by RoVeRT
  3737. BUILDIN_DEF(disablearena,""), // Added by RoVeRT
  3738. BUILDIN_DEF(hideoffnpc,"s"),
  3739. BUILDIN_DEF(hideonnpc,"s"),
  3740. BUILDIN_DEF(sc_start,"iii*"),
  3741. BUILDIN_DEF(sc_start2,"iiii*"),
  3742. BUILDIN_DEF(sc_start4,"iiiiii*"),
  3743. BUILDIN_DEF(sc_end,"i"),
  3744. BUILDIN_DEF(getscrate,"ii*"),
  3745. BUILDIN_DEF(debugmes,"s"),
  3746. BUILDIN_DEF2(catchpet,"pet","i"),
  3747. BUILDIN_DEF2(birthpet,"bpet",""),
  3748. BUILDIN_DEF(resetlvl,"i"),
  3749. BUILDIN_DEF(resetstatus,""),
  3750. BUILDIN_DEF(resetskill,""),
  3751. BUILDIN_DEF(skillpointcount,""),
  3752. BUILDIN_DEF(changebase,"i"),
  3753. BUILDIN_DEF(changesex,""),
  3754. BUILDIN_DEF(waitingroom,"si*"),
  3755. BUILDIN_DEF(delwaitingroom,"*"),
  3756. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","*"),
  3757. BUILDIN_DEF(enablewaitingroomevent,"*"),
  3758. BUILDIN_DEF(disablewaitingroomevent,"*"),
  3759. BUILDIN_DEF(getwaitingroomstate,"i*"),
  3760. BUILDIN_DEF(warpwaitingpc,"sii?"),
  3761. BUILDIN_DEF(attachrid,"i"),
  3762. BUILDIN_DEF(detachrid,""),
  3763. BUILDIN_DEF(isloggedin,"i"),
  3764. BUILDIN_DEF(setmapflagnosave,"ssii"),
  3765. BUILDIN_DEF(setmapflag,"si*"),
  3766. BUILDIN_DEF(removemapflag,"si"),
  3767. BUILDIN_DEF(pvpon,"s"),
  3768. BUILDIN_DEF(pvpoff,"s"),
  3769. BUILDIN_DEF(gvgon,"s"),
  3770. BUILDIN_DEF(gvgoff,"s"),
  3771. BUILDIN_DEF(emotion,"i*"),
  3772. BUILDIN_DEF(maprespawnguildid,"sii"),
  3773. BUILDIN_DEF(agitstart,""), // <Agit>
  3774. BUILDIN_DEF(agitend,""),
  3775. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  3776. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  3777. BUILDIN_DEF(getcastlename,"s"),
  3778. BUILDIN_DEF(getcastledata,"si*"),
  3779. BUILDIN_DEF(setcastledata,"sii"),
  3780. BUILDIN_DEF(requestguildinfo,"i*"),
  3781. BUILDIN_DEF(getequipcardcnt,"i"),
  3782. BUILDIN_DEF(successremovecards,"i"),
  3783. BUILDIN_DEF(failedremovecards,"ii"),
  3784. BUILDIN_DEF(marriage,"s"),
  3785. BUILDIN_DEF2(wedding_effect,"wedding",""),
  3786. BUILDIN_DEF(divorce,""),
  3787. BUILDIN_DEF(ispartneron,""),
  3788. BUILDIN_DEF(getpartnerid,""),
  3789. BUILDIN_DEF(getchildid,""),
  3790. BUILDIN_DEF(getmotherid,""),
  3791. BUILDIN_DEF(getfatherid,""),
  3792. BUILDIN_DEF(warppartner,"sii"),
  3793. BUILDIN_DEF(getitemname,"i"),
  3794. BUILDIN_DEF(getitemslots,"i"),
  3795. BUILDIN_DEF(makepet,"i"),
  3796. BUILDIN_DEF(getexp,"ii"),
  3797. BUILDIN_DEF(getinventorylist,""),
  3798. BUILDIN_DEF(getskilllist,""),
  3799. BUILDIN_DEF(clearitem,""),
  3800. BUILDIN_DEF(classchange,"ii"),
  3801. BUILDIN_DEF(misceffect,"i"),
  3802. BUILDIN_DEF(soundeffect,"si"),
  3803. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  3804. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  3805. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  3806. BUILDIN_DEF(guardianinfo,"i"), // display guardian data [Valaris]
  3807. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  3808. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  3809. BUILDIN_DEF(petloot,"i"), // [Valaris]
  3810. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  3811. // BUILDIN_DEF(petmag,"iiii"), // [Valaris]
  3812. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  3813. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  3814. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  3815. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  3816. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  3817. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  3818. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  3819. BUILDIN_DEF(nude,""), // nude command [Valaris]
  3820. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  3821. BUILDIN_DEF(inittimer,""),
  3822. BUILDIN_DEF(stoptimer,""),
  3823. BUILDIN_DEF(cmdothernpc,"ss"),
  3824. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  3825. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  3826. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  3827. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  3828. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  3829. BUILDIN_DEF(hasitems,"*"), // [Valaris]
  3830. BUILDIN_DEF(mobcount,"ss"),
  3831. BUILDIN_DEF(getlook,"i"),
  3832. BUILDIN_DEF(getsavepoint,"i"),
  3833. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  3834. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  3835. BUILDIN_DEF(npcstop,""), // [Valaris]
  3836. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  3837. BUILDIN_DEF(checkoption1,"i"),
  3838. BUILDIN_DEF(checkoption2,"i"),
  3839. BUILDIN_DEF(guildgetexp,"i"),
  3840. BUILDIN_DEF(guildchangegm,"is"),
  3841. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  3842. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  3843. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  3844. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  3845. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  3846. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  3847. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  3848. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  3849. BUILDIN_DEF(adopt,"sss"), // allows 2 parents to adopt a child
  3850. BUILDIN_DEF(night,""), // sets the server to night time
  3851. BUILDIN_DEF(day,""), // sets the server to day time
  3852. #ifdef PCRE_SUPPORT
  3853. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  3854. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  3855. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  3856. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  3857. #endif
  3858. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  3859. BUILDIN_DEF(getusersname,"*"),
  3860. BUILDIN_DEF(recovery,""),
  3861. BUILDIN_DEF(getpetinfo,"i"),
  3862. BUILDIN_DEF(checkequipedcard,"i"),
  3863. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  3864. BUILDIN_DEF(select,"*"), //for future jA script compatibility
  3865. BUILDIN_DEF(prompt,"*"),
  3866. BUILDIN_DEF(globalmes,"s*"),
  3867. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  3868. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  3869. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  3870. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  3871. BUILDIN_DEF(fakenpcname,"ssi"), // [Lance]
  3872. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  3873. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  3874. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  3875. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  3876. // [zBuffer] List of mathematics commands --->
  3877. BUILDIN_DEF(sqrt,"i"),
  3878. BUILDIN_DEF(pow,"ii"),
  3879. BUILDIN_DEF(distance,"iiii"),
  3880. BUILDIN_DEF(checkcell,"siii"),
  3881. // <--- [zBuffer] List of mathematics commands
  3882. // [zBuffer] List of dynamic var commands --->
  3883. BUILDIN_DEF(getd,"*"),
  3884. BUILDIN_DEF(setd,"*"),
  3885. // <--- [zBuffer] List of dynamic var commands
  3886. BUILDIN_DEF(petstat,"i"),
  3887. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  3888. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  3889. BUILDIN_DEF(npcshopadditem,"sii*"),
  3890. BUILDIN_DEF(npcshopdelitem,"si*"),
  3891. BUILDIN_DEF(npcshopattach,"s?"),
  3892. BUILDIN_DEF(equip,"i"),
  3893. BUILDIN_DEF(autoequip,"ii"),
  3894. BUILDIN_DEF(setbattleflag,"ss"),
  3895. BUILDIN_DEF(getbattleflag,"s"),
  3896. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  3897. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  3898. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  3899. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  3900. BUILDIN_DEF(axtoi,"s"),
  3901. BUILDIN_DEF(query_sql,"s*"),
  3902. BUILDIN_DEF(escape_sql,"s"),
  3903. BUILDIN_DEF(atoi,"s"),
  3904. // [zBuffer] List of player cont commands --->
  3905. BUILDIN_DEF(rid2name,"i"),
  3906. BUILDIN_DEF(pcfollow,"ii"),
  3907. BUILDIN_DEF(pcstopfollow,"i"),
  3908. BUILDIN_DEF(pcblockmove,"ii"),
  3909. // <--- [zBuffer] List of player cont commands
  3910. // [zBuffer] List of mob control commands --->
  3911. BUILDIN_DEF(mobspawn,"*"),
  3912. BUILDIN_DEF(mobremove,"i"),
  3913. BUILDIN_DEF(getmobdata,"i*"),
  3914. BUILDIN_DEF(setmobdata,"iii"),
  3915. BUILDIN_DEF(mobassist,"i?"),
  3916. BUILDIN_DEF(mobattach,"i?"),
  3917. BUILDIN_DEF(unitwalk,"ii?"),
  3918. BUILDIN_DEF(unitkill,"i"),
  3919. BUILDIN_DEF(unitwarp,"isii"),
  3920. BUILDIN_DEF(unitattack,"iv?"),
  3921. BUILDIN_DEF(unitstop,"i"),
  3922. BUILDIN_DEF(unittalk,"is"),
  3923. BUILDIN_DEF(unitemote,"ii"),
  3924. BUILDIN_DEF(unitdeadsit,"ii"),
  3925. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  3926. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  3927. // <--- [zBuffer] List of mob control commands
  3928. BUILDIN_DEF(sleep,"i"),
  3929. BUILDIN_DEF(sleep2,"i"),
  3930. BUILDIN_DEF(awake,"s"),
  3931. BUILDIN_DEF(getvariableofnpc,"rs"),
  3932. // [blackhole89] -->
  3933. BUILDIN_DEF(warpportal,"iisii"),
  3934. // <--- [blackhole89]
  3935. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  3936. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  3937. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  3938. BUILDIN_DEF(checkvending,"*"),
  3939. BUILDIN_DEF(checkchatting,"*"),
  3940. {NULL,NULL,NULL},
  3941. };
  3942. /*==========================================
  3943. *
  3944. *------------------------------------------
  3945. */
  3946. BUILDIN_FUNC(mes)
  3947. {
  3948. struct map_session_data *sd = script_rid2sd(st);
  3949. const char *mes = script_getstr(st, 2);
  3950. if (sd)
  3951. clif_scriptmes(sd, st->oid, mes);
  3952. return 0;
  3953. }
  3954. /*==========================================
  3955. *
  3956. *------------------------------------------
  3957. */
  3958. BUILDIN_FUNC(goto)
  3959. {
  3960. int pos;
  3961. if( !data_islabel(script_getdata(st,2))){
  3962. ShowMessage("script: goto: not label!\n");
  3963. st->state=END;
  3964. return 1;
  3965. }
  3966. pos=script_getnum(st,2);
  3967. st->pos=pos;
  3968. st->state=GOTO;
  3969. return 0;
  3970. }
  3971. /*==========================================
  3972. * ユーザー定義関数の呼び出し
  3973. *------------------------------------------
  3974. */
  3975. BUILDIN_FUNC(callfunc)
  3976. {
  3977. struct script_code *scr, *oldscr;
  3978. const char *str=script_getstr(st,2);
  3979. if( (scr=strdb_get(userfunc_db,(unsigned char*)str)) ){
  3980. int i,j;
  3981. struct linkdb_node **oldval = st->stack->var_function;
  3982. for(i=st->start+3,j=0;i<st->end;i++,j++)
  3983. push_copy(st->stack,i);
  3984. script_pushint(st,j); // 引数の数をプッシュ
  3985. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3986. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3987. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3988. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3989. oldscr = st->script;
  3990. st->pos=0;
  3991. st->script=scr;
  3992. st->stack->defsp=st->start+5+j;
  3993. st->state=GOTO;
  3994. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3995. // ' 変数の引き継ぎ
  3996. for(i = 0; i < j; i++) {
  3997. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  3998. if( data_isreference(s) && !s->ref ) {
  3999. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  4000. // '@ 変数の引き継ぎ
  4001. if( name[0] == '.' && name[1] == '@' ) {
  4002. s->ref = oldval;
  4003. } else if( name[0] == '.' ) {
  4004. s->ref = &oldscr->script_vars;
  4005. }
  4006. }
  4007. }
  4008. }else{
  4009. ShowWarning("script:callfunc: function not found! [%s]\n",str);
  4010. st->state=END;
  4011. return 1;
  4012. }
  4013. return 0;
  4014. }
  4015. /*==========================================
  4016. * サブルーティンの呼び出し
  4017. *------------------------------------------
  4018. */
  4019. BUILDIN_FUNC(callsub)
  4020. {
  4021. int pos=script_getnum(st,2);
  4022. int i,j;
  4023. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  4024. ShowError("script: callsub: not label !\n");
  4025. st->state=END;
  4026. return 1;
  4027. } else {
  4028. struct linkdb_node **oldval = st->stack->var_function;
  4029. for(i=st->start+3,j=0;i<st->end;i++,j++)
  4030. push_copy(st->stack,i);
  4031. script_pushint(st,j); // 引数の数をプッシュ
  4032. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  4033. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  4034. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  4035. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  4036. st->pos=pos;
  4037. st->stack->defsp=st->start+5+j;
  4038. st->state=GOTO;
  4039. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  4040. // ' 変数の引き継ぎ
  4041. for(i = 0; i < j; i++) {
  4042. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  4043. if( data_isreference(s) && !s->ref ) {
  4044. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  4045. // '@ 変数の引き継ぎ
  4046. if( name[0] == '.' && name[1] == '@' ) {
  4047. s->ref = oldval;
  4048. }
  4049. }
  4050. }
  4051. }
  4052. return 0;
  4053. }
  4054. /*==========================================
  4055. * 引数の所得
  4056. *------------------------------------------
  4057. */
  4058. BUILDIN_FUNC(getarg)
  4059. {
  4060. int num=script_getnum(st,2);
  4061. int max,stsp;
  4062. if( st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO ){
  4063. ShowWarning("script:getarg without callfunc or callsub!\n");
  4064. st->state=END;
  4065. return 1;
  4066. }
  4067. max=conv_num(st,& (st->stack->stack_data[st->stack->defsp-5]));
  4068. stsp=st->stack->defsp - max -5;
  4069. if( num >= max ){
  4070. ShowWarning("script:getarg arg1(%d) out of range(%d) !\n",num,max);
  4071. st->state=END;
  4072. return 1;
  4073. }
  4074. push_copy(st->stack,stsp+num);
  4075. return 0;
  4076. }
  4077. /*==========================================
  4078. * サブルーチン/ユーザー定義関数の終了
  4079. *------------------------------------------
  4080. */
  4081. BUILDIN_FUNC(return)
  4082. {
  4083. if(script_hasdata(st,2)){ // 戻り値有り
  4084. struct script_data *sd;
  4085. push_copy(st->stack,st->start+2);
  4086. sd = &st->stack->stack_data[st->stack->sp-1];
  4087. if(data_isreference(sd)) {
  4088. char *name = str_buf + str_data[sd->u.num&0x00ffffff].str;
  4089. if( name[0] == '.' && name[1] == '@') {
  4090. // '@ 変数を参照渡しにすると危険なので値渡しにする
  4091. get_val(st,sd);
  4092. //Fix dangling pointer crash due when returning a temporary
  4093. // script variable (from Rayce/jA)
  4094. if(isstr(sd)) {
  4095. sd->type = C_STR;
  4096. sd->u.str = (char *)aStrdup(sd->u.str);
  4097. }
  4098. } else if( name[0] == '.' && !sd->ref) {
  4099. // ' 変数は参照渡しでも良いが、参照元が設定されていないと
  4100. // 元のスクリプトの値を差してしまうので補正する。
  4101. sd->ref = &st->script->script_vars;
  4102. }
  4103. }
  4104. }
  4105. st->state=RETFUNC;
  4106. return 0;
  4107. }
  4108. /*==========================================
  4109. *
  4110. *------------------------------------------
  4111. */
  4112. BUILDIN_FUNC(next)
  4113. {
  4114. st->state=STOP;
  4115. clif_scriptnext(script_rid2sd(st),st->oid);
  4116. return 0;
  4117. }
  4118. /*==========================================
  4119. *
  4120. *------------------------------------------
  4121. */
  4122. BUILDIN_FUNC(close)
  4123. {
  4124. st->state=END;
  4125. clif_scriptclose(script_rid2sd(st),st->oid);
  4126. return 0;
  4127. }
  4128. BUILDIN_FUNC(close2)
  4129. {
  4130. st->state=STOP;
  4131. clif_scriptclose(script_rid2sd(st),st->oid);
  4132. return 0;
  4133. }
  4134. /*==========================================
  4135. *
  4136. *------------------------------------------
  4137. */
  4138. BUILDIN_FUNC(menu)
  4139. {
  4140. char *buf, *ptr;
  4141. int len,i;
  4142. struct map_session_data *sd = script_rid2sd(st);
  4143. nullpo_retr(0, sd);
  4144. if(sd->state.menu_or_input==0){
  4145. st->state=RERUNLINE;
  4146. sd->state.menu_or_input=1;
  4147. if( (st->end - st->start - 2) % 2 == 1 ) {
  4148. // 引数の数が奇数なのでエラー扱い
  4149. ShowError("buildin_menu: illegal argument count(%d).\n", st->end - st->start - 2);
  4150. sd->state.menu_or_input=0;
  4151. st->state=END;
  4152. return 1;
  4153. }
  4154. for(i=st->start+2,len=0;i<st->end;i+=2){
  4155. conv_str(st,& (st->stack->stack_data[i]));
  4156. len+=(int)strlen(st->stack->stack_data[i].u.str)+1;
  4157. }
  4158. buf=(char *)aMallocA((len+1)*sizeof(char));
  4159. buf[0]=0;
  4160. for(i=st->start+2,len=0;i<st->end;i+=2){
  4161. if( st->stack->stack_data[i].u.str[0] ) {
  4162. strcat(buf,st->stack->stack_data[i].u.str);
  4163. strcat(buf,":");
  4164. }
  4165. }
  4166. ptr = buf;
  4167. sd->npc_menu = 0; //Reuse to store max menu entries. Avoids the need of an extra variable.
  4168. while (ptr && (ptr = strchr(ptr, ':')) != NULL)
  4169. { sd->npc_menu++; ptr++; }
  4170. clif_scriptmenu(sd,st->oid,buf);
  4171. aFree(buf);
  4172. } else if(sd->npc_menu==0xff){ // cancel
  4173. sd->state.menu_or_input=0;
  4174. st->state=END;
  4175. } else { // goto動作
  4176. sd->state.menu_or_input=0;
  4177. if(sd->npc_menu>0){
  4178. //Skip empty menu entries which weren't displayed on the client (blackhole89)
  4179. for(i=st->start+2;i<=(st->start+sd->npc_menu*2) && sd->npc_menu<(st->end-st->start)/2;i+=2) {
  4180. conv_str(st,& (st->stack->stack_data[i])); // we should convert variables to strings before access it [jA1983] [EoE]
  4181. if((int)strlen(st->stack->stack_data[i].u.str) < 1)
  4182. sd->npc_menu++; //Empty selection which wasn't displayed on the client.
  4183. }
  4184. if(sd->npc_menu >= (st->end-st->start)/2) {
  4185. //Invalid selection.
  4186. st->state=END;
  4187. return 0;
  4188. }
  4189. if( !data_islabel(script_getdata(st, sd->npc_menu*2+1)) ){
  4190. ShowError("script: menu: not label !\n");
  4191. st->state=END;
  4192. return 1;
  4193. }
  4194. pc_setreg(sd,add_str("@menu"),sd->npc_menu);
  4195. st->pos=script_getnum(st,sd->npc_menu*2+1);
  4196. st->state=GOTO;
  4197. }
  4198. }
  4199. return 0;
  4200. }
  4201. /// Returns a random number from 0 to <range>-1.
  4202. /// Or returns a random number from <min> to <max>.
  4203. /// If <min> is greater than <max>, their numbers are switched.
  4204. /// rand(<range>) -> <int>
  4205. /// rand(<min>,<max>) -> <int>
  4206. BUILDIN_FUNC(rand)
  4207. {
  4208. int range;
  4209. int min;
  4210. int max;
  4211. if( script_hasdata(st,3) )
  4212. {// min,max
  4213. min = script_getnum(st,2);
  4214. max = script_getnum(st,3);
  4215. if( max < min )
  4216. swap(min, max);
  4217. range = max - min + 1;
  4218. }
  4219. else
  4220. {// range
  4221. min = 0;
  4222. range = script_getnum(st,2);
  4223. }
  4224. if( range <= 1 )
  4225. script_pushint(st, min);
  4226. else
  4227. script_pushint(st, rand()%range + min);
  4228. return 0;
  4229. }
  4230. /*==========================================
  4231. *
  4232. *------------------------------------------
  4233. */
  4234. BUILDIN_FUNC(warp)
  4235. {
  4236. int x,y;
  4237. const char *str;
  4238. struct map_session_data *sd=script_rid2sd(st);
  4239. nullpo_retr(0, sd);
  4240. str=script_getstr(st,2);
  4241. x=script_getnum(st,3);
  4242. y=script_getnum(st,4);
  4243. if(strcmp(str,"Random")==0)
  4244. pc_randomwarp(sd,3);
  4245. else if(strcmp(str,"SavePoint")==0){
  4246. pc_setpos(sd,sd->status.save_point.map,
  4247. sd->status.save_point.x,sd->status.save_point.y,3);
  4248. }else if(strcmp(str,"Save")==0){
  4249. pc_setpos(sd,sd->status.save_point.map,
  4250. sd->status.save_point.x,sd->status.save_point.y,3);
  4251. }else
  4252. pc_setpos(sd,mapindex_name2id(str),x,y,0);
  4253. return 0;
  4254. }
  4255. /*==========================================
  4256. * エリア指定ワープ
  4257. *------------------------------------------
  4258. */
  4259. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4260. {
  4261. int x,y;
  4262. unsigned int map;
  4263. map=va_arg(ap, unsigned int);
  4264. x=va_arg(ap,int);
  4265. y=va_arg(ap,int);
  4266. if(map == 0)
  4267. pc_randomwarp((struct map_session_data *)bl,3);
  4268. else
  4269. pc_setpos((struct map_session_data *)bl,map,x,y,0);
  4270. return 0;
  4271. }
  4272. BUILDIN_FUNC(areawarp)
  4273. {
  4274. int x,y,m;
  4275. unsigned int index;
  4276. const char *str;
  4277. const char *mapname;
  4278. int x0,y0,x1,y1;
  4279. mapname=script_getstr(st,2);
  4280. x0=script_getnum(st,3);
  4281. y0=script_getnum(st,4);
  4282. x1=script_getnum(st,5);
  4283. y1=script_getnum(st,6);
  4284. str=script_getstr(st,7);
  4285. x=script_getnum(st,8);
  4286. y=script_getnum(st,9);
  4287. if( (m=map_mapname2mapid(mapname))< 0)
  4288. return 0;
  4289. if(strcmp(str,"Random")==0)
  4290. index = 0;
  4291. else if(!(index=mapindex_name2id(str)))
  4292. return 0;
  4293. map_foreachinarea(buildin_areawarp_sub,
  4294. m,x0,y0,x1,y1,BL_PC, index,x,y );
  4295. return 0;
  4296. }
  4297. /*==========================================
  4298. * warpchar [LuzZza]
  4299. * Useful for warp one player from
  4300. * another player npc-session.
  4301. * Using: warpchar "mapname",x,y,Char_ID;
  4302. *------------------------------------------
  4303. */
  4304. BUILDIN_FUNC(warpchar)
  4305. {
  4306. int x,y,a,i;
  4307. const char *str;
  4308. struct map_session_data *sd, **pl_allsd;
  4309. int users;
  4310. str=script_getstr(st,2);
  4311. x=script_getnum(st,3);
  4312. y=script_getnum(st,4);
  4313. a=script_getnum(st,5);
  4314. pl_allsd = map_getallusers(&users);
  4315. for(i=0; i<users; i++) {
  4316. sd = pl_allsd[i];
  4317. if(sd->status.char_id == a) {
  4318. if(strcmp(str, "Random") == 0)
  4319. pc_randomwarp(sd, 3);
  4320. else if(strcmp(str, "SavePoint") == 0)
  4321. pc_setpos(sd, sd->status.save_point.map,
  4322. sd->status.save_point.x, sd->status.save_point.y, 3);
  4323. else
  4324. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  4325. }
  4326. }
  4327. return 0;
  4328. }
  4329. /*==========================================
  4330. * Warpparty - [Fredzilla]
  4331. * Syntax: warpparty "mapname",x,y,Party_ID;
  4332. *------------------------------------------
  4333. */
  4334. BUILDIN_FUNC(warpparty)
  4335. {
  4336. int x,y;
  4337. const char *str;
  4338. int p_id;
  4339. int i;
  4340. unsigned short mapindex;
  4341. struct map_session_data *pl_sd;
  4342. struct party_data *p=NULL;
  4343. str=script_getstr(st,2);
  4344. x=script_getnum(st,3);
  4345. y=script_getnum(st,4);
  4346. p_id=script_getnum(st,5);
  4347. if(p_id < 1)
  4348. return 0;
  4349. p = party_search(p_id);
  4350. if (!p)
  4351. return 0;
  4352. if(strcmp(str,"Random")==0)
  4353. {
  4354. for (i = 0; i < MAX_PARTY; i++)
  4355. {
  4356. if ((pl_sd = p->data[i].sd))
  4357. {
  4358. if(map[pl_sd->bl.m].flag.nowarp)
  4359. continue;
  4360. pc_randomwarp(pl_sd,3);
  4361. }
  4362. }
  4363. }
  4364. else if(strcmp(str,"SavePointAll")==0)
  4365. {
  4366. for (i = 0; i < MAX_PARTY; i++)
  4367. {
  4368. if ((pl_sd = p->data[i].sd))
  4369. {
  4370. if(map[pl_sd->bl.m].flag.noreturn)
  4371. continue;
  4372. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4373. }
  4374. }
  4375. }
  4376. else if(strcmp(str,"SavePoint")==0)
  4377. {
  4378. pl_sd=script_rid2sd(st);
  4379. if (!pl_sd) return 0;
  4380. mapindex=pl_sd->status.save_point.map;
  4381. x=pl_sd->status.save_point.x;
  4382. y=pl_sd->status.save_point.y;
  4383. for (i = 0; i < MAX_PARTY; i++)
  4384. {
  4385. if ((pl_sd = p->data[i].sd))
  4386. {
  4387. if(map[pl_sd->bl.m].flag.noreturn)
  4388. continue;
  4389. pc_setpos(pl_sd,mapindex,x,y,3);
  4390. }
  4391. }
  4392. }
  4393. else
  4394. {
  4395. mapindex = mapindex_name2id(str);
  4396. if (!mapindex) //Show source of npc error.
  4397. return 1;
  4398. for (i = 0; i < MAX_PARTY; i++)
  4399. {
  4400. if ((pl_sd = p->data[i].sd))
  4401. {
  4402. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4403. continue;
  4404. pc_setpos(pl_sd,mapindex,x,y,3);
  4405. }
  4406. }
  4407. }
  4408. return 0;
  4409. }
  4410. /*==========================================
  4411. * Warpguild - [Fredzilla]
  4412. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4413. *------------------------------------------
  4414. */
  4415. BUILDIN_FUNC(warpguild)
  4416. {
  4417. int x,y;
  4418. unsigned short mapindex;
  4419. const char *str;
  4420. int g;
  4421. int i;
  4422. struct map_session_data *pl_sd, **pl_allsd;
  4423. int users;
  4424. struct map_session_data *sd;
  4425. str=script_getstr(st,2);
  4426. x=script_getnum(st,3);
  4427. y=script_getnum(st,4);
  4428. g=script_getnum(st,5);
  4429. sd=script_rid2sd(st);
  4430. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4431. return 0;
  4432. if(g < 1)
  4433. return 0;
  4434. pl_allsd = map_getallusers(&users);
  4435. if(strcmp(str,"Random")==0)
  4436. {
  4437. for (i = 0; i < users; i++)
  4438. {
  4439. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4440. {
  4441. if(map[pl_sd->bl.m].flag.nowarp)
  4442. continue;
  4443. pc_randomwarp(pl_sd,3);
  4444. }
  4445. }
  4446. }
  4447. else if(strcmp(str,"SavePointAll")==0)
  4448. {
  4449. if(map[sd->bl.m].flag.noreturn)
  4450. return 0;
  4451. for (i = 0; i < users; i++)
  4452. {
  4453. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4454. {
  4455. if(map[pl_sd->bl.m].flag.noreturn)
  4456. continue;
  4457. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4458. }
  4459. }
  4460. }
  4461. else if(strcmp(str,"SavePoint")==0)
  4462. {
  4463. if(map[sd->bl.m].flag.noreturn)
  4464. return 0;
  4465. mapindex=sd->status.save_point.map;
  4466. x=sd->status.save_point.x;
  4467. y=sd->status.save_point.y;
  4468. for (i = 0; i < users; i++)
  4469. {
  4470. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4471. {
  4472. if(map[pl_sd->bl.m].flag.noreturn)
  4473. continue;
  4474. pc_setpos(pl_sd,mapindex,x,y,3);
  4475. }
  4476. }
  4477. }
  4478. else
  4479. {
  4480. mapindex = mapindex_name2id(str);
  4481. for (i = 0; i < users; i++)
  4482. {
  4483. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4484. {
  4485. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4486. continue;
  4487. pc_setpos(pl_sd,mapindex,x,y,3);
  4488. }
  4489. }
  4490. }
  4491. return 0;
  4492. }
  4493. /*==========================================
  4494. *
  4495. *------------------------------------------
  4496. */
  4497. BUILDIN_FUNC(heal)
  4498. {
  4499. struct map_session_data *sd;
  4500. int hp,sp;
  4501. sd = script_rid2sd(st);
  4502. if (!sd) return 0;
  4503. hp=script_getnum(st,2);
  4504. sp=script_getnum(st,3);
  4505. status_heal(&sd->bl, hp, sp, 1);
  4506. return 0;
  4507. }
  4508. /*==========================================
  4509. *
  4510. *------------------------------------------
  4511. */
  4512. BUILDIN_FUNC(itemheal)
  4513. {
  4514. struct map_session_data *sd;
  4515. int hp,sp;
  4516. hp=script_getnum(st,2);
  4517. sp=script_getnum(st,3);
  4518. if(potion_flag==1) {
  4519. potion_hp = hp;
  4520. potion_sp = sp;
  4521. return 0;
  4522. }
  4523. sd = script_rid2sd(st);
  4524. if (!sd) return 0;
  4525. pc_itemheal(sd,sd->itemid,hp,sp);
  4526. return 0;
  4527. }
  4528. /*==========================================
  4529. *
  4530. *------------------------------------------
  4531. */
  4532. BUILDIN_FUNC(percentheal)
  4533. {
  4534. int hp,sp;
  4535. hp=script_getnum(st,2);
  4536. sp=script_getnum(st,3);
  4537. if(potion_flag==1) {
  4538. potion_per_hp = hp;
  4539. potion_per_sp = sp;
  4540. return 0;
  4541. }
  4542. pc_percentheal(script_rid2sd(st),hp,sp);
  4543. return 0;
  4544. }
  4545. /*==========================================
  4546. *
  4547. *------------------------------------------
  4548. */
  4549. BUILDIN_FUNC(jobchange)
  4550. {
  4551. int job, upper=-1;
  4552. job=script_getnum(st,2);
  4553. if( script_hasdata(st,3) )
  4554. upper=script_getnum(st,3);
  4555. if ((job >= 0 && job < MAX_PC_CLASS))
  4556. {
  4557. pc_jobchange(script_rid2sd(st),job, upper);
  4558. if(use_irc && irc_announce_jobchange_flag)
  4559. irc_announce_jobchange(script_rid2sd(st));
  4560. }
  4561. return 0;
  4562. }
  4563. /*==========================================
  4564. *
  4565. *------------------------------------------
  4566. */
  4567. BUILDIN_FUNC(jobname)
  4568. {
  4569. int class_=script_getnum(st,2);
  4570. script_pushconststr(st,job_name(class_));
  4571. return 0;
  4572. }
  4573. /*==========================================
  4574. *
  4575. *------------------------------------------
  4576. */
  4577. BUILDIN_FUNC(input)
  4578. {
  4579. struct map_session_data *sd = script_rid2sd(st);
  4580. struct script_data *data = script_getdata(st,2);
  4581. int num = data->u.num;
  4582. char *name=str_buf+str_data[num&0x00ffffff].str;
  4583. char postfix = name[strlen(name)-1];
  4584. if (!sd) return 1;
  4585. if( !data_isreference(data) ){
  4586. ShowError("script: buildin_input: given argument is not a variable!\n");
  4587. return 1;
  4588. }
  4589. if(sd->state.menu_or_input){
  4590. sd->state.menu_or_input=0;
  4591. if( postfix=='$' )
  4592. {
  4593. set_reg(st,sd,num,name,(void*)sd->npc_str,
  4594. script_getref(st,2));
  4595. return 0;
  4596. }
  4597. // Yor, Lupus & Fritz have messed with this.
  4598. // Basicly it prevents negative input since most scripts do not account for them.
  4599. sd->npc_amount = cap_value(sd->npc_amount, 0, battle_config.vending_max_value);
  4600. set_reg(st,sd,num,name,(void*)sd->npc_amount,
  4601. script_getref(st,2));
  4602. return 0;
  4603. }
  4604. //state.menu_or_input = 0
  4605. st->state=RERUNLINE;
  4606. if( postfix=='$' )
  4607. clif_scriptinputstr(sd,st->oid);
  4608. else
  4609. clif_scriptinput(sd,st->oid);
  4610. sd->state.menu_or_input=1;
  4611. return 0;
  4612. }
  4613. /*==========================================
  4614. * 変数設定
  4615. *------------------------------------------
  4616. */
  4617. BUILDIN_FUNC(set)
  4618. {
  4619. struct map_session_data *sd=NULL;
  4620. int num=st->stack->stack_data[st->start+2].u.num;
  4621. char *name=str_buf+str_data[num&0x00ffffff].str;
  4622. char prefix=*name;
  4623. char postfix=name[strlen(name)-1];
  4624. if( !data_isreference(script_getdata(st,2)) ){
  4625. ShowError("script: buildin_set: not name\n");
  4626. return 1;
  4627. }
  4628. if(not_server_variable(prefix))
  4629. sd=script_rid2sd(st);
  4630. if( postfix=='$' ){
  4631. // 文字列
  4632. const char *str = script_getstr(st,3);
  4633. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  4634. }else{
  4635. // 数値
  4636. int val = script_getnum(st,3);
  4637. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  4638. }
  4639. return 0;
  4640. }
  4641. /*==========================================
  4642. * 配列変数設定
  4643. *------------------------------------------
  4644. */
  4645. BUILDIN_FUNC(setarray)
  4646. {
  4647. struct map_session_data *sd=NULL;
  4648. int num=st->stack->stack_data[st->start+2].u.num;
  4649. char *name=str_buf+str_data[num&0x00ffffff].str;
  4650. char prefix=*name;
  4651. char postfix=name[strlen(name)-1];
  4652. int i,j;
  4653. if( prefix!='$' && prefix!='@' && prefix!='.'){
  4654. ShowWarning("buildin_setarray: illegal scope !\n");
  4655. return 1;
  4656. }
  4657. if(not_server_variable(prefix))
  4658. sd=script_rid2sd(st);
  4659. for(j=0,i=st->start+3; i<st->end && j<128;i++,j++){
  4660. void *v;
  4661. if( postfix=='$' )
  4662. v=(void*)conv_str(st,& (st->stack->stack_data[i]));
  4663. else
  4664. v=(void*)conv_num(st,& (st->stack->stack_data[i]));
  4665. set_reg(st, sd, num+(j<<24), name, v, script_getref(st,2));
  4666. }
  4667. return 0;
  4668. }
  4669. /*==========================================
  4670. * 配列変数クリア
  4671. *------------------------------------------
  4672. */
  4673. BUILDIN_FUNC(cleararray)
  4674. {
  4675. struct map_session_data *sd=NULL;
  4676. int num=st->stack->stack_data[st->start+2].u.num;
  4677. char *name=str_buf+str_data[num&0x00ffffff].str;
  4678. char prefix=*name;
  4679. char postfix=name[strlen(name)-1];
  4680. int sz=script_getnum(st,4);
  4681. int i;
  4682. void *v;
  4683. if( prefix!='$' && prefix!='@' && prefix!='.'){
  4684. ShowWarning("buildin_cleararray: illegal scope !\n");
  4685. return 1;
  4686. }
  4687. if( not_server_variable(prefix) )
  4688. sd=script_rid2sd(st);
  4689. if( postfix=='$' )
  4690. v=(void*)script_getstr(st,3);
  4691. else
  4692. v=(void*)script_getnum(st,3);
  4693. for(i=0;i<sz;i++)
  4694. set_reg(st,sd,num+(i<<24),name,v,script_getref(st,2));
  4695. return 0;
  4696. }
  4697. /*==========================================
  4698. * 配列変数コピー
  4699. *------------------------------------------
  4700. */
  4701. BUILDIN_FUNC(copyarray)
  4702. {
  4703. struct map_session_data *sd=NULL;
  4704. int num=st->stack->stack_data[st->start+2].u.num;
  4705. char *name=str_buf+str_data[num&0x00ffffff].str;
  4706. char prefix=*name;
  4707. char postfix=name[strlen(name)-1];
  4708. int num2=st->stack->stack_data[st->start+3].u.num;
  4709. char *name2=str_buf+str_data[num2&0x00ffffff].str;
  4710. char prefix2=*name2;
  4711. char postfix2=name2[strlen(name2)-1];
  4712. int sz=script_getnum(st,4);
  4713. int i;
  4714. if( prefix!='$' && prefix!='@' && prefix!='.' ){
  4715. printf("buildin_copyarray: illeagal scope !\n");
  4716. return 0;
  4717. }
  4718. if( prefix2!='$' && prefix2!='@' && prefix2!='.' ) {
  4719. printf("buildin_copyarray: illeagal scope !\n");
  4720. return 0;
  4721. }
  4722. if( (postfix=='$' || postfix2=='$') && postfix!=postfix2 ){
  4723. printf("buildin_copyarray: type mismatch !\n");
  4724. return 0;
  4725. }
  4726. if( not_server_variable(prefix) || not_server_variable(prefix2) )
  4727. sd=script_rid2sd(st);
  4728. if((num & 0x00FFFFFF) == (num2 & 0x00FFFFFF) && (num & 0xFF000000) > (num2 & 0xFF000000)) {
  4729. // 同じ配列で、num > num2 の場合大きい方からコピーしないといけない
  4730. for(i=sz-1;i>=0;i--)
  4731. set_reg(
  4732. st,sd,num+(i<<24),name,
  4733. get_val2(st,num2+(i<<24),script_getref(st,3)),
  4734. script_getref(st,2)
  4735. );
  4736. } else {
  4737. for(i=0;i<sz;i++)
  4738. set_reg(
  4739. st,sd,num+(i<<24),name,
  4740. get_val2(st,num2+(i<<24),script_getref(st,3)),
  4741. script_getref(st,2)
  4742. );
  4743. }
  4744. return 0;
  4745. }
  4746. /*==========================================
  4747. * Returns the size of the specified array
  4748. *------------------------------------------*/
  4749. static int getarraysize(struct script_state* st, int num, int postfix, struct linkdb_node** ref)
  4750. {
  4751. //TODO: unreadable; remove +-1 trick, remove weird for loop, remove typecast
  4752. int i = (num>>24), c = (i==0?-1:i); // Moded to -1 because even if the first element is 0, it will still report as 1 [Lance]
  4753. if(postfix == '$') {
  4754. for(; i < 128; i++) {
  4755. void* v = get_val2(st, (num & 0x00FFFFFF) + (i<<24), ref);
  4756. if(*((char*)v)) c = i;
  4757. }
  4758. } else {
  4759. for(; i < 128; i++) {
  4760. void* v = get_val2(st, (num & 0x00FFFFFF) + (i<<24), ref);
  4761. if((int)v) c = i;
  4762. }
  4763. }
  4764. return c + 1;
  4765. }
  4766. BUILDIN_FUNC(getarraysize)
  4767. {
  4768. int num = st->stack->stack_data[st->start+2].u.num;
  4769. char* name = str_buf + str_data[num&0x00ffffff].str;
  4770. char prefix = name[0], postfix = name[strlen(name)-1];
  4771. if( prefix != '$' && prefix != '@' && prefix != '.' ) {
  4772. ShowWarning("buildin_getarraysize: illegal scope !\n");
  4773. script_pushint(st,0);
  4774. return 1;
  4775. }
  4776. script_pushint(st,getarraysize(st, num, postfix, script_getref(st,2)));
  4777. return 0;
  4778. }
  4779. /*==========================================
  4780. * 配列変数から要素削除
  4781. *------------------------------------------
  4782. */
  4783. BUILDIN_FUNC(deletearray)
  4784. {
  4785. struct map_session_data *sd=NULL;
  4786. int num=st->stack->stack_data[st->start+2].u.num;
  4787. char *name=str_buf+str_data[num&0x00ffffff].str;
  4788. char prefix=*name;
  4789. char postfix=name[strlen(name)-1];
  4790. int count=1;
  4791. int i,sz=getarraysize(st,num,postfix,script_getref(st,2))-(num>>24)-count+1;
  4792. if( script_hasdata(st,3) )
  4793. count=script_getnum(st,3);
  4794. if( prefix!='$' && prefix!='@' && prefix!='.' ){
  4795. ShowWarning("buildin_deletearray: illegal scope !\n");
  4796. return 1;
  4797. }
  4798. if( not_server_variable(prefix) )
  4799. sd=script_rid2sd(st);
  4800. for(i=0;i<sz;i++){
  4801. set_reg(
  4802. st,sd,num+(i<<24),name,
  4803. get_val2(st,num+((i+count)<<24),script_getref(st,2)),
  4804. script_getref(st,2)
  4805. );
  4806. }
  4807. if(postfix != '$'){
  4808. for(;i<(128-(num>>24));i++)
  4809. set_reg(st,sd,num+(i<<24),name, 0,script_getref(st,2));
  4810. } else {
  4811. for(;i<(128-(num>>24));i++)
  4812. set_reg(st,sd,num+(i<<24),name, (void *) "",script_getref(st,2));
  4813. }
  4814. return 0;
  4815. }
  4816. /*==========================================
  4817. * 指定要素を表す値(キー)を所得する
  4818. *------------------------------------------
  4819. */
  4820. BUILDIN_FUNC(getelementofarray)
  4821. {
  4822. if( data_isreference(script_getdata(st, 2)) ){
  4823. int i=script_getnum(st,3);
  4824. if(i>127 || i<0){
  4825. ShowWarning("script: getelementofarray (operator[]): param2 illegal number %d\n",i);
  4826. script_pushint(st,0);
  4827. return 1;
  4828. }else{
  4829. push_val2(st->stack,C_NAME,
  4830. (i<<24) | st->stack->stack_data[st->start+2].u.num, script_getref(st,2) );
  4831. }
  4832. }else{
  4833. ShowError("script: getelementofarray (operator[]): param1 not name !\n");
  4834. script_pushint(st,0);
  4835. }
  4836. return 0;
  4837. }
  4838. /*==========================================
  4839. *
  4840. *------------------------------------------
  4841. */
  4842. BUILDIN_FUNC(setlook)
  4843. {
  4844. int type,val;
  4845. type=script_getnum(st,2);
  4846. val=script_getnum(st,3);
  4847. pc_changelook(script_rid2sd(st),type,val);
  4848. return 0;
  4849. }
  4850. /*==========================================
  4851. *
  4852. *------------------------------------------
  4853. */
  4854. BUILDIN_FUNC(cutin)
  4855. {
  4856. clif_cutin(script_rid2sd(st),script_getstr(st,2),script_getnum(st,3));
  4857. return 0;
  4858. }
  4859. /*==========================================
  4860. *
  4861. *------------------------------------------
  4862. */
  4863. BUILDIN_FUNC(viewpoint)
  4864. {
  4865. int type,x,y,id,color;
  4866. type=script_getnum(st,2);
  4867. x=script_getnum(st,3);
  4868. y=script_getnum(st,4);
  4869. id=script_getnum(st,5);
  4870. color=script_getnum(st,6);
  4871. clif_viewpoint(script_rid2sd(st),st->oid,type,x,y,id,color);
  4872. return 0;
  4873. }
  4874. /*==========================================
  4875. *
  4876. *------------------------------------------
  4877. */
  4878. BUILDIN_FUNC(countitem)
  4879. {
  4880. int nameid=0,count=0,i;
  4881. struct map_session_data *sd;
  4882. struct script_data *data;
  4883. sd = script_rid2sd(st);
  4884. if (!sd) {
  4885. script_pushint(st,0);
  4886. return 0;
  4887. }
  4888. data=script_getdata(st,2);
  4889. get_val(st,data);
  4890. if( data_isstring(data) ){
  4891. const char *name=conv_str(st,data);
  4892. struct item_data *item_data;
  4893. if( (item_data = itemdb_searchname(name)) != NULL)
  4894. nameid=item_data->nameid;
  4895. }else
  4896. nameid=conv_num(st,data);
  4897. if (nameid>=500) //if no such ID then skip this iteration
  4898. for(i=0;i<MAX_INVENTORY;i++){
  4899. if(sd->status.inventory[i].nameid==nameid)
  4900. count+=sd->status.inventory[i].amount;
  4901. }
  4902. else{
  4903. if(battle_config.error_log)
  4904. ShowError("wrong item ID : countitem(%i)\n",nameid);
  4905. script_pushint(st,0);
  4906. return 1;
  4907. }
  4908. script_pushint(st,count);
  4909. return 0;
  4910. }
  4911. /*==========================================
  4912. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4913. * returns number of items that met the conditions
  4914. *------------------------------------------
  4915. */
  4916. BUILDIN_FUNC(countitem2)
  4917. {
  4918. int nameid=0,count=0,i;
  4919. int iden,ref,attr,c1,c2,c3,c4;
  4920. struct map_session_data *sd;
  4921. struct script_data *data;
  4922. sd = script_rid2sd(st);
  4923. if (!sd) {
  4924. script_pushint(st,0);
  4925. return 0;
  4926. }
  4927. data=script_getdata(st,2);
  4928. get_val(st,data);
  4929. if( data_isstring(data) ){
  4930. const char *name=conv_str(st,data);
  4931. struct item_data *item_data;
  4932. if( (item_data = itemdb_searchname(name)) != NULL)
  4933. nameid=item_data->nameid;
  4934. }else
  4935. nameid=conv_num(st,data);
  4936. iden=script_getnum(st,3);
  4937. ref=script_getnum(st,4);
  4938. attr=script_getnum(st,5);
  4939. c1=script_getnum(st,6);
  4940. c2=script_getnum(st,7);
  4941. c3=script_getnum(st,8);
  4942. c4=script_getnum(st,9);
  4943. if (nameid>=500) //if no such ID then skip this iteration
  4944. for(i=0;i<MAX_INVENTORY;i++){
  4945. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  4946. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  4947. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  4948. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  4949. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  4950. sd->status.inventory[i].card[3]!=c4)
  4951. continue;
  4952. count+=sd->status.inventory[i].amount;
  4953. }
  4954. else{
  4955. if(battle_config.error_log)
  4956. ShowError("wrong item ID : countitem2(%i)\n",nameid);
  4957. script_pushint(st,0);
  4958. return 1;
  4959. }
  4960. script_pushint(st,count);
  4961. return 0;
  4962. }
  4963. /*==========================================
  4964. * 重量チェック
  4965. *------------------------------------------
  4966. */
  4967. BUILDIN_FUNC(checkweight)
  4968. {
  4969. int nameid=0,amount,i;
  4970. unsigned long weight;
  4971. struct map_session_data *sd;
  4972. struct script_data *data;
  4973. sd = script_rid2sd(st);
  4974. data=script_getdata(st,2);
  4975. get_val(st,data);
  4976. if( data_isstring(data) ){
  4977. const char *name=conv_str(st,data);
  4978. struct item_data *item_data = itemdb_searchname(name);
  4979. if( item_data )
  4980. nameid=item_data->nameid;
  4981. }else
  4982. nameid=conv_num(st,data);
  4983. amount=script_getnum(st,3);
  4984. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4985. script_pushint(st,0);
  4986. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4987. return 1;
  4988. }
  4989. weight = itemdb_weight(nameid)*amount;
  4990. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4991. script_pushint(st,0);
  4992. } else {
  4993. //Check if the inventory ain't full.
  4994. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4995. i = pc_search_inventory(sd,0);
  4996. if (i >= 0) //Empty slot available.
  4997. script_pushint(st,1);
  4998. else //Inventory full
  4999. script_pushint(st,0);
  5000. }
  5001. return 0;
  5002. }
  5003. /*==========================================
  5004. * getitem <item id>,<amount>{,<character ID>};
  5005. * getitem "<item name>",<amount>{,<character ID>};
  5006. *------------------------------------------
  5007. */
  5008. BUILDIN_FUNC(getitem)
  5009. {
  5010. int nameid,amount,flag = 0;
  5011. struct item it;
  5012. struct map_session_data *sd;
  5013. struct script_data *data;
  5014. data=script_getdata(st,2);
  5015. get_val(st,data);
  5016. if( data_isstring(data) )
  5017. {// "<item name>"
  5018. const char *name=conv_str(st,data);
  5019. struct item_data *item_data = itemdb_searchname(name);
  5020. if( item_data == NULL ){
  5021. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5022. report_src(st);
  5023. return 1; //No item created.
  5024. }
  5025. nameid=item_data->nameid;
  5026. } else if( data_isint(data) )
  5027. {// <item id>
  5028. nameid=conv_num(st,data);
  5029. //Violet Box, Blue Box, etc - random item pick
  5030. if( nameid < 0 ) {
  5031. nameid=itemdb_searchrandomid(-nameid);
  5032. flag = 1;
  5033. }
  5034. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5035. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5036. report_src(st);
  5037. return 1; //No item created.
  5038. }
  5039. } else {
  5040. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5041. report_src(st);
  5042. return 1;
  5043. }
  5044. // <amount>
  5045. if( (amount=script_getnum(st,3)) <= 0)
  5046. return 0; //return if amount <=0, skip the useles iteration
  5047. memset(&it,0,sizeof(it));
  5048. it.nameid=nameid;
  5049. if(!flag)
  5050. it.identify=1;
  5051. else
  5052. it.identify=itemdb_isidentified(nameid);
  5053. if( script_hasdata(st,4) )
  5054. {// <character ID>
  5055. sd=map_id2sd(script_getnum(st,4));
  5056. } else
  5057. {// attached player
  5058. sd=script_rid2sd(st);
  5059. }
  5060. if( sd == NULL ) // no target
  5061. return 0;
  5062. if( pet_create_egg(sd, nameid) )
  5063. amount = 1; //This is a pet!
  5064. else if( (flag=pc_additem(sd,&it,amount)) ){
  5065. clif_additem(sd,0,0,flag);
  5066. if( pc_candrop(sd,&it) )
  5067. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  5068. }
  5069. //Logs items, got from (N)PC scripts [Lupus]
  5070. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  5071. log_pick_pc(sd, "N", nameid, amount, NULL);
  5072. return 0;
  5073. }
  5074. /*==========================================
  5075. *
  5076. *------------------------------------------
  5077. */
  5078. BUILDIN_FUNC(getitem2)
  5079. {
  5080. int nameid,amount,flag = 0;
  5081. int iden,ref,attr,c1,c2,c3,c4;
  5082. struct item_data *item_data;
  5083. struct item item_tmp;
  5084. struct map_session_data *sd;
  5085. struct script_data *data;
  5086. sd = script_rid2sd(st);
  5087. data=script_getdata(st,2);
  5088. get_val(st,data);
  5089. if( data_isstring(data) ){
  5090. const char *name=conv_str(st,data);
  5091. struct item_data *item_data = itemdb_searchname(name);
  5092. if( item_data )
  5093. nameid=item_data->nameid;
  5094. else
  5095. nameid=UNKNOWN_ITEM_ID;
  5096. }else
  5097. nameid=conv_num(st,data);
  5098. amount=script_getnum(st,3);
  5099. iden=script_getnum(st,4);
  5100. ref=script_getnum(st,5);
  5101. attr=script_getnum(st,6);
  5102. c1=script_getnum(st,7);
  5103. c2=script_getnum(st,8);
  5104. c3=script_getnum(st,9);
  5105. c4=script_getnum(st,10);
  5106. if( script_hasdata(st,11) ) //アイテムを指定したIDに渡す
  5107. sd=map_id2sd(script_getnum(st,11));
  5108. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  5109. return 0;
  5110. if(nameid<0) { // ランダム
  5111. nameid=itemdb_searchrandomid(-nameid);
  5112. flag = 1;
  5113. }
  5114. if(nameid > 0) {
  5115. memset(&item_tmp,0,sizeof(item_tmp));
  5116. item_data=itemdb_exists(nameid);
  5117. if (item_data == NULL)
  5118. return -1;
  5119. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5120. if(ref > 10) ref = 10;
  5121. }
  5122. else if(item_data->type==IT_PETEGG) {
  5123. iden = 1;
  5124. ref = 0;
  5125. }
  5126. else {
  5127. iden = 1;
  5128. ref = attr = 0;
  5129. }
  5130. item_tmp.nameid=nameid;
  5131. if(!flag)
  5132. item_tmp.identify=iden;
  5133. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5134. item_tmp.identify=0;
  5135. item_tmp.refine=ref;
  5136. item_tmp.attribute=attr;
  5137. item_tmp.card[0]=c1;
  5138. item_tmp.card[1]=c2;
  5139. item_tmp.card[2]=c3;
  5140. item_tmp.card[3]=c4;
  5141. if((flag = pc_additem(sd,&item_tmp,amount))) {
  5142. clif_additem(sd,0,0,flag);
  5143. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  5144. }
  5145. //Logs items, got from (N)PC scripts [Lupus]
  5146. if(log_config.enable_logs&0x40)
  5147. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  5148. }
  5149. return 0;
  5150. }
  5151. /*==========================================
  5152. * gets an item with someone's name inscribed [Skotlex]
  5153. * getinscribeditem item_num, character_name
  5154. * Returned Qty is always 1, only works on equip-able
  5155. * equipment
  5156. *------------------------------------------
  5157. */
  5158. BUILDIN_FUNC(getnameditem)
  5159. {
  5160. int nameid;
  5161. struct item item_tmp;
  5162. struct map_session_data *sd, *tsd;
  5163. struct script_data *data;
  5164. sd = script_rid2sd(st);
  5165. if (sd == NULL)
  5166. { //Player not attached!
  5167. script_pushint(st,0);
  5168. return 0;
  5169. }
  5170. data=script_getdata(st,2);
  5171. get_val(st,data);
  5172. if( data_isstring(data) ){
  5173. const char *name=conv_str(st,data);
  5174. struct item_data *item_data = itemdb_searchname(name);
  5175. if( item_data == NULL)
  5176. { //Failed
  5177. script_pushint(st,0);
  5178. return 0;
  5179. }
  5180. nameid = item_data->nameid;
  5181. }else
  5182. nameid = conv_num(st,data);
  5183. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5184. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5185. script_pushint(st,0);
  5186. return 0;
  5187. }
  5188. data=script_getdata(st,3);
  5189. get_val(st,data);
  5190. if( data_isstring(data) ) //Char Name
  5191. tsd=map_nick2sd(conv_str(st,data));
  5192. else //Char Id was given
  5193. tsd=map_charid2sd(conv_num(st,data));
  5194. if( tsd == NULL )
  5195. { //Failed
  5196. script_pushint(st,0);
  5197. return 0;
  5198. }
  5199. memset(&item_tmp,0,sizeof(item_tmp));
  5200. item_tmp.nameid=nameid;
  5201. item_tmp.amount=1;
  5202. item_tmp.identify=1;
  5203. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5204. item_tmp.card[2]=tsd->status.char_id;
  5205. item_tmp.card[3]=tsd->status.char_id >> 16;
  5206. if(pc_additem(sd,&item_tmp,1)) {
  5207. script_pushint(st,0);
  5208. return 0; //Failed to add item, we will not drop if they don't fit
  5209. }
  5210. //Logs items, got from (N)PC scripts [Lupus]
  5211. if(log_config.enable_logs&0x40)
  5212. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5213. script_pushint(st,1);
  5214. return 0;
  5215. }
  5216. /*==========================================
  5217. * gets a random item ID from an item group [Skotlex]
  5218. * groupranditem group_num
  5219. *------------------------------------------
  5220. */
  5221. BUILDIN_FUNC(grouprandomitem)
  5222. {
  5223. int group;
  5224. group = script_getnum(st,2);
  5225. script_pushint(st,itemdb_searchrandomid(group));
  5226. return 0;
  5227. }
  5228. /*==========================================
  5229. *
  5230. *------------------------------------------
  5231. */
  5232. BUILDIN_FUNC(makeitem)
  5233. {
  5234. int nameid,amount,flag = 0;
  5235. int x,y,m;
  5236. const char *mapname;
  5237. struct item item_tmp;
  5238. struct script_data *data;
  5239. data=script_getdata(st,2);
  5240. get_val(st,data);
  5241. if( data_isstring(data) ){
  5242. const char *name=conv_str(st,data);
  5243. struct item_data *item_data = itemdb_searchname(name);
  5244. if( item_data )
  5245. nameid=item_data->nameid;
  5246. else
  5247. nameid=UNKNOWN_ITEM_ID;
  5248. }else
  5249. nameid=conv_num(st,data);
  5250. amount=script_getnum(st,3);
  5251. mapname =script_getstr(st,4);
  5252. x =script_getnum(st,5);
  5253. y =script_getnum(st,6);
  5254. if(strcmp(mapname,"this")==0)
  5255. {
  5256. struct map_session_data *sd;
  5257. sd = script_rid2sd(st);
  5258. if (!sd) return 0; //Failed...
  5259. m=sd->bl.m;
  5260. } else
  5261. m=map_mapname2mapid(mapname);
  5262. if(nameid<0) { // ランダム
  5263. nameid=itemdb_searchrandomid(-nameid);
  5264. flag = 1;
  5265. }
  5266. if(nameid > 0) {
  5267. memset(&item_tmp,0,sizeof(item_tmp));
  5268. item_tmp.nameid=nameid;
  5269. if(!flag)
  5270. item_tmp.identify=1;
  5271. else
  5272. item_tmp.identify=itemdb_isidentified(nameid);
  5273. map_addflooritem(&item_tmp,amount,m,x,y,NULL,NULL,NULL,0);
  5274. }
  5275. return 0;
  5276. }
  5277. /*==========================================
  5278. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5279. *------------------------------------------
  5280. */
  5281. BUILDIN_FUNC(delitem)
  5282. {
  5283. int nameid=0,amount,i,important_item=0;
  5284. struct map_session_data *sd;
  5285. struct script_data *data;
  5286. sd = script_rid2sd(st);
  5287. data=script_getdata(st,2);
  5288. get_val(st,data);
  5289. if( data_isstring(data) ){
  5290. const char *name=conv_str(st,data);
  5291. struct item_data *item_data = itemdb_searchname(name);
  5292. //nameid=UNKNOWN_ITEM_ID;
  5293. if( item_data )
  5294. nameid=item_data->nameid;
  5295. }else
  5296. nameid=conv_num(st,data);
  5297. amount=script_getnum(st,3);
  5298. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5299. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5300. return 0;
  5301. }
  5302. //1st pass
  5303. //here we won't delete items with CARDS, named items but we count them
  5304. for(i=0;i<MAX_INVENTORY;i++){
  5305. //we don't delete wrong item or equipped item
  5306. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5307. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5308. continue;
  5309. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5310. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5311. sd->status.inventory[i].card[0] == CARD0_PET)
  5312. {
  5313. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5314. continue; //pet couldn't be sent for deletion.
  5315. } else
  5316. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5317. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5318. sd->status.inventory[i].card[0] ||
  5319. sd->status.inventory[i].refine) {
  5320. //this is important item, count it (except for pet eggs)
  5321. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5322. important_item++;
  5323. continue;
  5324. }
  5325. if(sd->status.inventory[i].amount>=amount){
  5326. //Logs items, got from (N)PC scripts [Lupus]
  5327. if(log_config.enable_logs&0x40)
  5328. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5329. pc_delitem(sd,i,amount,0);
  5330. return 0; //we deleted exact amount of items. now exit
  5331. } else {
  5332. amount-=sd->status.inventory[i].amount;
  5333. //Logs items, got from (N)PC scripts [Lupus]
  5334. if(log_config.enable_logs&0x40) {
  5335. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5336. }
  5337. //Logs
  5338. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5339. }
  5340. }
  5341. //2nd pass
  5342. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5343. if (important_item>0 && amount>0)
  5344. for(i=0;i<MAX_INVENTORY;i++){
  5345. //we don't delete wrong item
  5346. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5347. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5348. continue;
  5349. if(sd->status.inventory[i].amount>=amount){
  5350. //Logs items, got from (N)PC scripts [Lupus]
  5351. if(log_config.enable_logs&0x40)
  5352. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5353. pc_delitem(sd,i,amount,0);
  5354. return 0; //we deleted exact amount of items. now exit
  5355. } else {
  5356. amount-=sd->status.inventory[i].amount;
  5357. //Logs items, got from (N)PC scripts [Lupus]
  5358. if(log_config.enable_logs&0x40)
  5359. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5360. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5361. }
  5362. }
  5363. return 0;
  5364. }
  5365. /*==========================================
  5366. * advanced version of delitem [modified by Mihilion]
  5367. *------------------------------------------
  5368. */
  5369. BUILDIN_FUNC(delitem2)
  5370. {
  5371. int nameid=0,amount,i=0;
  5372. int iden,ref,attr,c1,c2,c3,c4;
  5373. struct map_session_data *sd;
  5374. struct script_data *data;
  5375. sd = script_rid2sd(st);
  5376. data=script_getdata(st,2);
  5377. get_val(st,data);
  5378. if( data_isstring(data) ){
  5379. const char *name=conv_str(st,data);
  5380. struct item_data *item_data = itemdb_searchname(name);
  5381. //nameid=UNKNOWN_ITEM_ID;
  5382. if( item_data )
  5383. nameid=item_data->nameid;
  5384. }else
  5385. nameid=conv_num(st,data);
  5386. amount=script_getnum(st,3);
  5387. iden=script_getnum(st,4);
  5388. ref=script_getnum(st,5);
  5389. attr=script_getnum(st,6);
  5390. c1=script_getnum(st,7);
  5391. c2=script_getnum(st,8);
  5392. c3=script_getnum(st,9);
  5393. c4=script_getnum(st,10);
  5394. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5395. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5396. return 0;
  5397. }
  5398. for(i=0;i<MAX_INVENTORY;i++){
  5399. //we don't delete wrong item or equipped item
  5400. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5401. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5402. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5403. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5404. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5405. sd->status.inventory[i].card[3]!=c4)
  5406. continue;
  5407. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5408. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5409. {
  5410. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5411. continue; //Failed to send delete the pet.
  5412. }
  5413. if(sd->status.inventory[i].amount>=amount){
  5414. //Logs items, got from (N)PC scripts [Lupus]
  5415. if(log_config.enable_logs&0x40)
  5416. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5417. pc_delitem(sd,i,amount,0);
  5418. return 0; //we deleted exact amount of items. now exit
  5419. } else {
  5420. amount-=sd->status.inventory[i].amount;
  5421. //Logs items, got from (N)PC scripts [Lupus]
  5422. if(log_config.enable_logs&0x40)
  5423. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5424. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5425. }
  5426. }
  5427. return 0;
  5428. }
  5429. /*==========================================
  5430. * Enables/Disables use of items while in an NPC [Skotlex]
  5431. *------------------------------------------
  5432. */
  5433. BUILDIN_FUNC(enableitemuse)
  5434. {
  5435. struct map_session_data *sd;
  5436. sd=script_rid2sd(st);
  5437. if (sd)
  5438. sd->npc_item_flag = st->oid;
  5439. return 0;
  5440. }
  5441. BUILDIN_FUNC(disableitemuse)
  5442. {
  5443. struct map_session_data *sd;
  5444. sd=script_rid2sd(st);
  5445. if (sd)
  5446. sd->npc_item_flag = 0;
  5447. return 0;
  5448. }
  5449. /*==========================================
  5450. *キャラ関係のパラメータ取得
  5451. *------------------------------------------
  5452. */
  5453. BUILDIN_FUNC(readparam)
  5454. {
  5455. int type;
  5456. struct map_session_data *sd;
  5457. type=script_getnum(st,2);
  5458. if( script_hasdata(st,3) )
  5459. sd=map_nick2sd(script_getstr(st,3));
  5460. else
  5461. sd=script_rid2sd(st);
  5462. if(sd==NULL){
  5463. script_pushint(st,-1);
  5464. return 0;
  5465. }
  5466. script_pushint(st,pc_readparam(sd,type));
  5467. return 0;
  5468. }
  5469. /*==========================================
  5470. *キャラ関係のID取得
  5471. *------------------------------------------
  5472. */
  5473. BUILDIN_FUNC(getcharid)
  5474. {
  5475. int num;
  5476. struct map_session_data *sd;
  5477. num=script_getnum(st,2);
  5478. if( script_hasdata(st,3) )
  5479. sd=map_nick2sd(script_getstr(st,3));
  5480. else
  5481. sd=script_rid2sd(st);
  5482. if(sd==NULL || num<0 || num>3){
  5483. script_pushint(st,0); //return 0, according docs
  5484. return 0;
  5485. }
  5486. if(num==0)
  5487. script_pushint(st,sd->status.char_id);
  5488. if(num==1)
  5489. script_pushint(st,sd->status.party_id);
  5490. if(num==2)
  5491. script_pushint(st,sd->status.guild_id);
  5492. if(num==3)
  5493. script_pushint(st,sd->status.account_id);
  5494. return 0;
  5495. }
  5496. /*==========================================
  5497. *指定IDのPT名取得
  5498. *------------------------------------------
  5499. */
  5500. char *buildin_getpartyname_sub(int party_id)
  5501. {
  5502. struct party_data *p;
  5503. p=party_search(party_id);
  5504. if(p!=NULL){
  5505. char *buf;
  5506. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5507. memcpy(buf, p->party.name, NAME_LENGTH-1);
  5508. buf[NAME_LENGTH-1] = '\0';
  5509. return buf;
  5510. }
  5511. return 0;
  5512. }
  5513. BUILDIN_FUNC(getpartyname)
  5514. {
  5515. char *name;
  5516. int party_id;
  5517. party_id=script_getnum(st,2);
  5518. name=buildin_getpartyname_sub(party_id);
  5519. if(name != NULL)
  5520. script_pushstr(st,name);
  5521. else
  5522. script_pushconststr(st,"null");
  5523. return 0;
  5524. }
  5525. /*==========================================
  5526. *指定IDのPT人数とメンバーID取得
  5527. *------------------------------------------
  5528. */
  5529. BUILDIN_FUNC(getpartymember)
  5530. {
  5531. struct party_data *p;
  5532. int i,j=0,type=0;
  5533. p=party_search(script_getnum(st,2));
  5534. if( script_hasdata(st,3) )
  5535. type=script_getnum(st,3);
  5536. if(p!=NULL){
  5537. for(i=0;i<MAX_PARTY;i++){
  5538. if(p->party.member[i].account_id){
  5539. switch (type) {
  5540. case 2:
  5541. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5542. break;
  5543. case 1:
  5544. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5545. break;
  5546. default:
  5547. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5548. }
  5549. j++;
  5550. }
  5551. }
  5552. }
  5553. mapreg_setreg(add_str("$@partymembercount"),j);
  5554. return 0;
  5555. }
  5556. /*==========================================
  5557. * Retrieves party leader. if flag is specified,
  5558. * return some of the leader data. Otherwise, return name.
  5559. *------------------------------------------
  5560. */
  5561. BUILDIN_FUNC(getpartyleader)
  5562. {
  5563. int party_id, type = 0, i=0;
  5564. struct party_data *p;
  5565. party_id=script_getnum(st,2);
  5566. if( script_hasdata(st,3) )
  5567. type=script_getnum(st,3);
  5568. p=party_search(party_id);
  5569. if (p) //Search leader
  5570. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5571. if (!p || i == MAX_PARTY) { //leader not found
  5572. if (type)
  5573. script_pushint(st,-1);
  5574. else
  5575. script_pushconststr(st,"null");
  5576. return 0;
  5577. }
  5578. switch (type) {
  5579. case 1:
  5580. script_pushint(st,p->party.member[i].account_id);
  5581. break;
  5582. case 2:
  5583. script_pushint(st,p->party.member[i].char_id);
  5584. break;
  5585. case 3:
  5586. script_pushint(st,p->party.member[i].class_);
  5587. break;
  5588. case 4:
  5589. script_pushstr(st,aStrdup(mapindex_id2name(p->party.member[i].map)));
  5590. break;
  5591. case 5:
  5592. script_pushint(st,p->party.member[i].lv);
  5593. break;
  5594. default:
  5595. script_pushstr(st,aStrdup(p->party.member[i].name));
  5596. break;
  5597. }
  5598. return 0;
  5599. }
  5600. /*==========================================
  5601. *指定IDのギルド名取得
  5602. *------------------------------------------
  5603. */
  5604. char *buildin_getguildname_sub(int guild_id)
  5605. {
  5606. struct guild *g=NULL;
  5607. g=guild_search(guild_id);
  5608. if(g!=NULL){
  5609. char *buf;
  5610. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5611. memcpy(buf, g->name, NAME_LENGTH-1);
  5612. buf[NAME_LENGTH-1] = '\0';
  5613. return buf;
  5614. }
  5615. return NULL;
  5616. }
  5617. BUILDIN_FUNC(getguildname)
  5618. {
  5619. char *name;
  5620. int guild_id=script_getnum(st,2);
  5621. name=buildin_getguildname_sub(guild_id);
  5622. if(name != NULL)
  5623. script_pushstr(st,name);
  5624. else
  5625. script_pushconststr(st,"null");
  5626. return 0;
  5627. }
  5628. /*==========================================
  5629. *指定IDのGuildMaster名取得
  5630. *------------------------------------------
  5631. */
  5632. char *buildin_getguildmaster_sub(int guild_id)
  5633. {
  5634. struct guild *g=NULL;
  5635. g=guild_search(guild_id);
  5636. if(g!=NULL){
  5637. char *buf;
  5638. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5639. memcpy(buf, g->master, NAME_LENGTH-1);
  5640. buf[NAME_LENGTH-1] = '\0';
  5641. return buf;
  5642. }
  5643. return 0;
  5644. }
  5645. BUILDIN_FUNC(getguildmaster)
  5646. {
  5647. char *master;
  5648. int guild_id=script_getnum(st,2);
  5649. master=buildin_getguildmaster_sub(guild_id);
  5650. if(master!=0)
  5651. script_pushstr(st,master);
  5652. else
  5653. script_pushconststr(st,"null");
  5654. return 0;
  5655. }
  5656. BUILDIN_FUNC(getguildmasterid)
  5657. {
  5658. char *master;
  5659. struct map_session_data *sd=NULL;
  5660. int guild_id=script_getnum(st,2);
  5661. master=buildin_getguildmaster_sub(guild_id);
  5662. if(master!=0){
  5663. if((sd=map_nick2sd(master)) == NULL){
  5664. script_pushint(st,0);
  5665. return 0;
  5666. }
  5667. script_pushint(st,sd->status.char_id);
  5668. }else{
  5669. script_pushint(st,0);
  5670. }
  5671. return 0;
  5672. }
  5673. /*==========================================
  5674. * キャラクタの名前
  5675. *------------------------------------------
  5676. */
  5677. BUILDIN_FUNC(strcharinfo)
  5678. {
  5679. struct map_session_data *sd;
  5680. int num;
  5681. char *buf;
  5682. sd=script_rid2sd(st);
  5683. if (!sd) { //Avoid crashing....
  5684. script_pushconststr(st,"");
  5685. return 0;
  5686. }
  5687. num=script_getnum(st,2);
  5688. switch(num){
  5689. case 0:
  5690. script_pushstr(st,aStrdup(sd->status.name));
  5691. break;
  5692. case 1:
  5693. buf=buildin_getpartyname_sub(sd->status.party_id);
  5694. if(buf!=0)
  5695. script_pushstr(st,buf);
  5696. else
  5697. script_pushconststr(st,"");
  5698. break;
  5699. case 2:
  5700. buf=buildin_getguildname_sub(sd->status.guild_id);
  5701. if(buf != NULL)
  5702. script_pushstr(st,buf);
  5703. else
  5704. script_pushconststr(st,"");
  5705. break;
  5706. default:
  5707. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5708. script_pushconststr(st,"");
  5709. break;
  5710. }
  5711. return 0;
  5712. }
  5713. 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};
  5714. /*==========================================
  5715. * GetEquipID(Pos); Pos: 1-10
  5716. *------------------------------------------
  5717. */
  5718. BUILDIN_FUNC(getequipid)
  5719. {
  5720. int i,num;
  5721. struct map_session_data *sd;
  5722. struct item_data* item;
  5723. sd=script_rid2sd(st);
  5724. if(sd == NULL)
  5725. {
  5726. ShowError("getequipid: sd == NULL\n");
  5727. return 0;
  5728. }
  5729. num=script_getnum(st,2);
  5730. i=pc_checkequip(sd,equip[num-1]);
  5731. if(i >= 0){
  5732. item=sd->inventory_data[i];
  5733. if(item)
  5734. script_pushint(st,item->nameid);
  5735. else
  5736. script_pushint(st,0);
  5737. }else{
  5738. script_pushint(st,-1);
  5739. }
  5740. return 0;
  5741. }
  5742. /*==========================================
  5743. * 装備名文字列(精錬メニュー用)
  5744. *------------------------------------------
  5745. */
  5746. BUILDIN_FUNC(getequipname)
  5747. {
  5748. int i,num;
  5749. struct map_session_data *sd;
  5750. struct item_data* item;
  5751. char *buf;
  5752. buf=(char *)aMallocA(64*sizeof(char));
  5753. sd=script_rid2sd(st);
  5754. num=script_getnum(st,2);
  5755. i=pc_checkequip(sd,equip[num-1]);
  5756. if(i >= 0){
  5757. item=sd->inventory_data[i];
  5758. if(item)
  5759. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5760. else
  5761. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5762. }else{
  5763. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5764. }
  5765. script_pushstr(st,buf);
  5766. return 0;
  5767. }
  5768. /*==========================================
  5769. * getbrokenid [Valaris]
  5770. *------------------------------------------
  5771. */
  5772. BUILDIN_FUNC(getbrokenid)
  5773. {
  5774. int i,num,id=0,brokencounter=0;
  5775. struct map_session_data *sd;
  5776. sd=script_rid2sd(st);
  5777. num=script_getnum(st,2);
  5778. for(i=0; i<MAX_INVENTORY; i++) {
  5779. if(sd->status.inventory[i].attribute==1){
  5780. brokencounter++;
  5781. if(num==brokencounter){
  5782. id=sd->status.inventory[i].nameid;
  5783. break;
  5784. }
  5785. }
  5786. }
  5787. script_pushint(st,id);
  5788. return 0;
  5789. }
  5790. /*==========================================
  5791. * repair [Valaris]
  5792. *------------------------------------------
  5793. */
  5794. BUILDIN_FUNC(repair)
  5795. {
  5796. int i,num;
  5797. int repaircounter=0;
  5798. struct map_session_data *sd;
  5799. sd=script_rid2sd(st);
  5800. num=script_getnum(st,2);
  5801. for(i=0; i<MAX_INVENTORY; i++) {
  5802. if(sd->status.inventory[i].attribute==1){
  5803. repaircounter++;
  5804. if(num==repaircounter){
  5805. sd->status.inventory[i].attribute=0;
  5806. clif_equiplist(sd);
  5807. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5808. clif_misceffect(&sd->bl, 3);
  5809. break;
  5810. }
  5811. }
  5812. }
  5813. return 0;
  5814. }
  5815. /*==========================================
  5816. * 装備チェック
  5817. *------------------------------------------
  5818. */
  5819. BUILDIN_FUNC(getequipisequiped)
  5820. {
  5821. int i,num;
  5822. struct map_session_data *sd;
  5823. num=script_getnum(st,2);
  5824. sd=script_rid2sd(st);
  5825. if ((num - 1) >= (sizeof(equip) / sizeof(equip[0])))
  5826. i = -1;
  5827. else
  5828. i=pc_checkequip(sd,equip[num-1]);
  5829. if(i >= 0)
  5830. script_pushint(st,1);
  5831. else
  5832. script_pushint(st,0);
  5833. return 0;
  5834. }
  5835. /*==========================================
  5836. * 装備品精錬可能チェック
  5837. *------------------------------------------
  5838. */
  5839. BUILDIN_FUNC(getequipisenableref)
  5840. {
  5841. int i,num;
  5842. struct map_session_data *sd;
  5843. num=script_getnum(st,2);
  5844. sd=script_rid2sd(st);
  5845. i=pc_checkequip(sd,equip[num-1]);
  5846. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5847. {
  5848. script_pushint(st,1);
  5849. } else {
  5850. script_pushint(st,0);
  5851. }
  5852. return 0;
  5853. }
  5854. /*==========================================
  5855. * 装備品鑑定チェック
  5856. *------------------------------------------
  5857. */
  5858. BUILDIN_FUNC(getequipisidentify)
  5859. {
  5860. int i,num;
  5861. struct map_session_data *sd;
  5862. num=script_getnum(st,2);
  5863. sd=script_rid2sd(st);
  5864. i=pc_checkequip(sd,equip[num-1]);
  5865. if(i >= 0)
  5866. script_pushint(st,sd->status.inventory[i].identify);
  5867. else
  5868. script_pushint(st,0);
  5869. return 0;
  5870. }
  5871. /*==========================================
  5872. * 装備品精錬度
  5873. *------------------------------------------
  5874. */
  5875. BUILDIN_FUNC(getequiprefinerycnt)
  5876. {
  5877. int i,num;
  5878. struct map_session_data *sd;
  5879. num=script_getnum(st,2);
  5880. sd=script_rid2sd(st);
  5881. i=pc_checkequip(sd,equip[num-1]);
  5882. if(i >= 0)
  5883. script_pushint(st,sd->status.inventory[i].refine);
  5884. else
  5885. script_pushint(st,0);
  5886. return 0;
  5887. }
  5888. /*==========================================
  5889. * 装備品武器LV
  5890. *------------------------------------------
  5891. */
  5892. BUILDIN_FUNC(getequipweaponlv)
  5893. {
  5894. int i,num;
  5895. struct map_session_data *sd;
  5896. num=script_getnum(st,2);
  5897. sd=script_rid2sd(st);
  5898. i=pc_checkequip(sd,equip[num-1]);
  5899. if(i >= 0 && sd->inventory_data[i])
  5900. script_pushint(st,sd->inventory_data[i]->wlv);
  5901. else
  5902. script_pushint(st,0);
  5903. return 0;
  5904. }
  5905. /*==========================================
  5906. * 装備品精錬成功率
  5907. *------------------------------------------
  5908. */
  5909. BUILDIN_FUNC(getequippercentrefinery)
  5910. {
  5911. int i,num;
  5912. struct map_session_data *sd;
  5913. num=script_getnum(st,2);
  5914. sd=script_rid2sd(st);
  5915. i=pc_checkequip(sd,equip[num-1]);
  5916. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5917. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5918. else
  5919. script_pushint(st,0);
  5920. return 0;
  5921. }
  5922. /*==========================================
  5923. * 精錬成功
  5924. *------------------------------------------
  5925. */
  5926. BUILDIN_FUNC(successrefitem)
  5927. {
  5928. int i,num,ep;
  5929. struct map_session_data *sd;
  5930. num=script_getnum(st,2);
  5931. sd=script_rid2sd(st);
  5932. i=pc_checkequip(sd,equip[num-1]);
  5933. if(i >= 0) {
  5934. ep=sd->status.inventory[i].equip;
  5935. //Logs items, got from (N)PC scripts [Lupus]
  5936. if(log_config.enable_logs&0x40)
  5937. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5938. sd->status.inventory[i].refine++;
  5939. pc_unequipitem(sd,i,2);
  5940. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5941. clif_delitem(sd,i,1);
  5942. //Logs items, got from (N)PC scripts [Lupus]
  5943. if(log_config.enable_logs&0x40)
  5944. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5945. clif_additem(sd,i,1,0);
  5946. pc_equipitem(sd,i,ep);
  5947. clif_misceffect(&sd->bl,3);
  5948. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5949. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5950. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5951. ){ // Fame point system [DracoRPG]
  5952. switch (sd->inventory_data[i]->wlv){
  5953. case 1:
  5954. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5955. break;
  5956. case 2:
  5957. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5958. break;
  5959. case 3:
  5960. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5961. break;
  5962. }
  5963. }
  5964. }
  5965. return 0;
  5966. }
  5967. /*==========================================
  5968. * 精錬失敗
  5969. *------------------------------------------
  5970. */
  5971. BUILDIN_FUNC(failedrefitem)
  5972. {
  5973. int i,num;
  5974. struct map_session_data *sd;
  5975. num=script_getnum(st,2);
  5976. sd=script_rid2sd(st);
  5977. i=pc_checkequip(sd,equip[num-1]);
  5978. if(i >= 0) {
  5979. //Logs items, got from (N)PC scripts [Lupus]
  5980. if(log_config.enable_logs&0x40)
  5981. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5982. sd->status.inventory[i].refine = 0;
  5983. pc_unequipitem(sd,i,3);
  5984. // 精錬失敗エフェクトのパケット
  5985. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5986. pc_delitem(sd,i,1,0);
  5987. // 他の人にも失敗を通知
  5988. clif_misceffect(&sd->bl,2);
  5989. }
  5990. return 0;
  5991. }
  5992. /*==========================================
  5993. *
  5994. *------------------------------------------
  5995. */
  5996. BUILDIN_FUNC(statusup)
  5997. {
  5998. int type;
  5999. struct map_session_data *sd;
  6000. type=script_getnum(st,2);
  6001. sd=script_rid2sd(st);
  6002. pc_statusup(sd,type);
  6003. return 0;
  6004. }
  6005. /*==========================================
  6006. *
  6007. *------------------------------------------
  6008. */
  6009. BUILDIN_FUNC(statusup2)
  6010. {
  6011. int type,val;
  6012. struct map_session_data *sd;
  6013. type=script_getnum(st,2);
  6014. val=script_getnum(st,3);
  6015. sd=script_rid2sd(st);
  6016. pc_statusup2(sd,type,val);
  6017. return 0;
  6018. }
  6019. /// See 'doc/item_bonus.txt'
  6020. /// bonus <bonus type>,<val1>
  6021. /// bonus2 <bonus type>,<val1>,<val2>
  6022. /// bonus3 <bonus type>,<val1>,<val2>,<val3>
  6023. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>
  6024. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>
  6025. BUILDIN_FUNC(bonus)
  6026. {
  6027. int type;
  6028. int type2;
  6029. int type3;
  6030. int type4;
  6031. int type5;
  6032. int val;
  6033. TBL_PC* sd;
  6034. sd = script_rid2sd(st);
  6035. if( sd == NULL )
  6036. return 1; // no player attached
  6037. type = script_getnum(st,2);
  6038. switch( script_lastdata(st) ){
  6039. case 3:
  6040. val = script_getnum(st,3);
  6041. pc_bonus(sd, type, val);
  6042. break;
  6043. case 4:
  6044. type2 = script_getnum(st,3);
  6045. val = script_getnum(st,4);
  6046. pc_bonus2(sd, type, type2, val);
  6047. break;
  6048. case 5:
  6049. type2 = script_getnum(st,3);
  6050. type3 = script_getnum(st,4);
  6051. val = script_getnum(st,5);
  6052. pc_bonus3(sd, type, type2, type3, val);
  6053. break;
  6054. case 6:
  6055. type2 = script_getnum(st,3);
  6056. type3 = script_getnum(st,4);
  6057. type4 = script_getnum(st,5);
  6058. val = script_getnum(st,6);
  6059. pc_bonus4(sd, type, type2, type3, type4, val);
  6060. break;
  6061. case 7:
  6062. type2 = script_getnum(st,3);
  6063. type3 = script_getnum(st,4);
  6064. type4 = script_getnum(st,5);
  6065. type5 = script_getnum(st,6);
  6066. val = script_getnum(st,7);
  6067. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  6068. break;
  6069. default:
  6070. ShowDebug("buildin_bonus: unexpected last data (%d)\n", script_lastdata(st));
  6071. }
  6072. return 0;
  6073. }
  6074. /// Changes the level of a player skill.
  6075. /// skill <skill id>,<level>{,<flag>}
  6076. /// @see pc_skill() for flag
  6077. BUILDIN_FUNC(skill)
  6078. {
  6079. int id;
  6080. int level;
  6081. int flag = 1;
  6082. TBL_PC* sd;
  6083. sd = script_rid2sd(st);
  6084. if( sd == NULL )
  6085. return 1;// no player attached, report source
  6086. id = script_getnum(st,2);
  6087. level = script_getnum(st,3);
  6088. if( script_hasdata(st,4) )
  6089. flag = script_getnum(st,4);
  6090. pc_skill(sd, id, level, flag);
  6091. return 0;
  6092. }
  6093. /// Changes the level of a player skill.
  6094. /// addtoskill <skill id>,<level>{,<flag>}
  6095. /// @see pc_skill() for flag
  6096. BUILDIN_FUNC(addtoskill)
  6097. {
  6098. int id;
  6099. int level;
  6100. int flag = 2;
  6101. TBL_PC* sd;
  6102. sd = script_rid2sd(st);
  6103. if( sd == NULL )
  6104. return 1;// no player attached, report source
  6105. id = script_getnum(st,2);
  6106. level = script_getnum(st,3);
  6107. if( script_hasdata(st,4) )
  6108. flag = script_getnum(st,4);
  6109. pc_skill(sd, id, level, flag);
  6110. return 0;
  6111. }
  6112. /// Increases the level of the guild skill.
  6113. /// guildskill <skill id>,<level>
  6114. BUILDIN_FUNC(guildskill)
  6115. {
  6116. int id;
  6117. int level;
  6118. TBL_PC* sd;
  6119. int i;
  6120. sd = script_rid2sd(st);
  6121. if( sd == NULL )
  6122. return 1;// no player attached, report source
  6123. id = script_getnum(st,2);
  6124. level = script_getnum(st,3);
  6125. for( i=0; i < level; i++ )
  6126. guild_skillup(sd, id);
  6127. return 0;
  6128. }
  6129. /// Returns the level of the player skill.
  6130. /// getskilllv(<skill id>) -> <level>
  6131. BUILDIN_FUNC(getskilllv)
  6132. {
  6133. int id;
  6134. TBL_PC* sd;
  6135. sd = script_rid2sd(st);
  6136. if( sd == NULL )
  6137. return 1;// no player attached, report source
  6138. id = script_getnum(st,2);
  6139. script_pushint(st, pc_checkskill(sd,id));
  6140. return 0;
  6141. }
  6142. /// Returns the level of the guild skill.
  6143. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6144. BUILDIN_FUNC(getgdskilllv)
  6145. {
  6146. int guild_id;
  6147. int skill_id;
  6148. struct guild* g;
  6149. guild_id = script_getnum(st,2);
  6150. skill_id = script_getnum(st,3);
  6151. g = guild_search(guild_id);
  6152. if( g == NULL )
  6153. script_pushint(st, -1);
  6154. else
  6155. script_pushint(st, guild_checkskill(g,skill_id));
  6156. return 0;
  6157. }
  6158. /// Returns the 'basic_skill_check' setting.
  6159. /// basicskillcheck() -> <setting>
  6160. BUILDIN_FUNC(basicskillcheck)
  6161. {
  6162. script_pushint(st, battle_config.basic_skill_check);
  6163. return 0;
  6164. }
  6165. /// Returns the GM level of the player.
  6166. /// getgmlevel() -> <level>
  6167. BUILDIN_FUNC(getgmlevel)
  6168. {
  6169. TBL_PC* sd;
  6170. sd = script_rid2sd(st);
  6171. if( sd == NULL )
  6172. return 1;// no player attached, report source
  6173. script_pushint(st, pc_isGM(sd));
  6174. return 0;
  6175. }
  6176. /// Terminates the execution of this script instance.
  6177. /// end
  6178. BUILDIN_FUNC(end)
  6179. {
  6180. st->state = END;
  6181. return 0;
  6182. }
  6183. /// Checks if the player has that option.
  6184. /// checkoption(<option>) -> <bool>
  6185. BUILDIN_FUNC(checkoption)
  6186. {
  6187. int option;
  6188. TBL_PC* sd;
  6189. sd = script_rid2sd(st);
  6190. if( sd == NULL )
  6191. return 1;// no player attached, report source
  6192. option = script_getnum(st,2);
  6193. if( sd->sc.option&option )
  6194. script_pushint(st, 1);
  6195. else
  6196. script_pushint(st, 0);
  6197. return 0;
  6198. }
  6199. /// Checks if the player is in that opt1 state.
  6200. /// checkoption1(<opt1>) -> <bool>
  6201. BUILDIN_FUNC(checkoption1)
  6202. {
  6203. int opt1;
  6204. TBL_PC* sd;
  6205. sd = script_rid2sd(st);
  6206. if( sd == NULL )
  6207. return 1;// no player attached, report source
  6208. opt1 = script_getnum(st,2);
  6209. if( sd->sc.opt1 == opt1 )
  6210. script_pushint(st, 1);
  6211. else
  6212. script_pushint(st, 0);
  6213. return 0;
  6214. }
  6215. /// Checks if the player has that opt2.
  6216. /// checkoption2(<opt2>) -> <bool>
  6217. BUILDIN_FUNC(checkoption2)
  6218. {
  6219. int opt2;
  6220. TBL_PC* sd;
  6221. sd = script_rid2sd(st);
  6222. if( sd == NULL )
  6223. return 1;// no player attached, report source
  6224. opt2 = script_getnum(st,2);
  6225. if( sd->sc.opt2&opt2 )
  6226. script_pushint(st, 1);
  6227. else
  6228. script_pushint(st, 0);
  6229. return 0;
  6230. }
  6231. /// Changes the option of the player.
  6232. /// setoption <option number>{,<flag>}
  6233. BUILDIN_FUNC(setoption)
  6234. {
  6235. int option;
  6236. int flag = 1;
  6237. TBL_PC* sd;
  6238. sd = script_rid2sd(st);
  6239. if( sd == NULL )
  6240. return 1;// no player attached, report source
  6241. option = script_getnum(st,2);
  6242. if( script_hasdata(st,3) )
  6243. flag = script_getnum(st,3);
  6244. else if( !option ){// Request to remove everything.
  6245. flag = 0;
  6246. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6247. }
  6248. if( flag ){// Add option
  6249. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6250. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6251. pc_setoption(sd, sd->sc.option|option);
  6252. } else// Remove option
  6253. pc_setoption(sd, sd->sc.option&~option);
  6254. return 0;
  6255. }
  6256. /// Returns if the player has a cart.
  6257. /// checkcart() -> <bool>
  6258. /// @author Valaris
  6259. BUILDIN_FUNC(checkcart)
  6260. {
  6261. TBL_PC* sd;
  6262. sd = script_rid2sd(st);
  6263. if( sd == NULL )
  6264. return 1;// no player attached, report source
  6265. if( pc_iscarton(sd) )
  6266. script_pushint(st, 1);
  6267. else
  6268. script_pushint(st, 0);
  6269. return 0;
  6270. }
  6271. /// Sets the cart of the player.
  6272. /// setcart {<type>}
  6273. BUILDIN_FUNC(setcart)
  6274. {
  6275. int type = 1;
  6276. TBL_PC* sd;
  6277. sd = script_rid2sd(st);
  6278. if( sd == NULL )
  6279. return 1;// no player attached, report source
  6280. if( script_hasdata(st,2) )
  6281. type = script_getnum(st,2);
  6282. pc_setcart(sd, type);
  6283. return 0;
  6284. }
  6285. /// Returns if the player has a falcon.
  6286. /// checkfalcon() -> <bool>
  6287. /// @author Valaris
  6288. BUILDIN_FUNC(checkfalcon)
  6289. {
  6290. TBL_PC* sd;
  6291. sd = script_rid2sd(st);
  6292. if( sd == NULL )
  6293. return 1;// no player attached, report source
  6294. if( pc_isfalcon(sd) )
  6295. script_pushint(st, 1);
  6296. else
  6297. script_pushint(st, 0);
  6298. return 0;
  6299. }
  6300. /// Sets if the player has a falcon or not.
  6301. /// setfalcon {<flag>}
  6302. BUILDIN_FUNC(setfalcon)
  6303. {
  6304. int flag = 1;
  6305. TBL_PC* sd;
  6306. sd = script_rid2sd(st);
  6307. if( sd == NULL )
  6308. return 1;// no player attached, report source
  6309. if( script_hasdata(st,2) )
  6310. flag = script_getnum(st,2);
  6311. pc_setfalcon(sd, flag);
  6312. return 0;
  6313. }
  6314. /// Returns if the player is riding.
  6315. /// checkriding() -> <bool>
  6316. /// @author Valaris
  6317. BUILDIN_FUNC(checkriding)
  6318. {
  6319. TBL_PC* sd;
  6320. sd = script_rid2sd(st);
  6321. if( sd == NULL )
  6322. return 1;// no player attached, report source
  6323. if( pc_isriding(sd) )
  6324. script_pushint(st, 1);
  6325. else
  6326. script_pushint(st, 0);
  6327. return 0;
  6328. }
  6329. /// Sets if the player is riding.
  6330. /// setriding {<flag>}
  6331. BUILDIN_FUNC(setriding)
  6332. {
  6333. int flag = 1;
  6334. TBL_PC* sd;
  6335. sd = script_rid2sd(st);
  6336. if( sd == NULL )
  6337. return 1;// no player attached, report source
  6338. if( script_hasdata(st,2) )
  6339. flag = script_getnum(st,2);
  6340. pc_setriding(sd, flag);
  6341. return 0;
  6342. }
  6343. /// Sets the save point of the player.
  6344. /// save "<map name>",<x>,<y>
  6345. /// savepoint "<map name>",<x>,<y>
  6346. BUILDIN_FUNC(savepoint)
  6347. {
  6348. int x;
  6349. int y;
  6350. short map;
  6351. const char* str;
  6352. TBL_PC* sd;
  6353. sd = script_rid2sd(st);
  6354. if( sd == NULL )
  6355. return 1;// no player attached, report source
  6356. str = script_getstr(st, 2);
  6357. x = script_getnum(st,3);
  6358. y = script_getnum(st,4);
  6359. map = mapindex_name2id(str);
  6360. if( map )
  6361. pc_setsavepoint(sd, map, x, y);
  6362. return 0;
  6363. }
  6364. /*==========================================
  6365. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6366. *------------------------------------------
  6367. */
  6368. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6369. {
  6370. int type;
  6371. time_t timer;
  6372. struct tm *t;
  6373. type=script_getnum(st,2);
  6374. switch(type){
  6375. case 2:
  6376. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6377. // from the system clock.)
  6378. script_pushint(st,(int)time(NULL));
  6379. break;
  6380. case 1:
  6381. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6382. time(&timer);
  6383. t=localtime(&timer);
  6384. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6385. break;
  6386. case 0:
  6387. default:
  6388. //type 0:(System Ticks)
  6389. script_pushint(st,gettick());
  6390. break;
  6391. }
  6392. return 0;
  6393. }
  6394. /*==========================================
  6395. * GetTime(Type);
  6396. * 1: Sec 2: Min 3: Hour
  6397. * 4: WeekDay 5: MonthDay 6: Month
  6398. * 7: Year
  6399. *------------------------------------------
  6400. */
  6401. BUILDIN_FUNC(gettime) /* Asgard Version */
  6402. {
  6403. int type;
  6404. time_t timer;
  6405. struct tm *t;
  6406. type=script_getnum(st,2);
  6407. time(&timer);
  6408. t=localtime(&timer);
  6409. switch(type){
  6410. case 1://Sec(0~59)
  6411. script_pushint(st,t->tm_sec);
  6412. break;
  6413. case 2://Min(0~59)
  6414. script_pushint(st,t->tm_min);
  6415. break;
  6416. case 3://Hour(0~23)
  6417. script_pushint(st,t->tm_hour);
  6418. break;
  6419. case 4://WeekDay(0~6)
  6420. script_pushint(st,t->tm_wday);
  6421. break;
  6422. case 5://MonthDay(01~31)
  6423. script_pushint(st,t->tm_mday);
  6424. break;
  6425. case 6://Month(01~12)
  6426. script_pushint(st,t->tm_mon+1);
  6427. break;
  6428. case 7://Year(20xx)
  6429. script_pushint(st,t->tm_year+1900);
  6430. break;
  6431. case 8://Year Day(01~366)
  6432. script_pushint(st,t->tm_yday+1);
  6433. break;
  6434. default://(format error)
  6435. script_pushint(st,-1);
  6436. break;
  6437. }
  6438. return 0;
  6439. }
  6440. /*==========================================
  6441. * GetTimeStr("TimeFMT", Length);
  6442. *------------------------------------------
  6443. */
  6444. BUILDIN_FUNC(gettimestr)
  6445. {
  6446. char *tmpstr;
  6447. const char *fmtstr;
  6448. int maxlen;
  6449. time_t now = time(NULL);
  6450. fmtstr=script_getstr(st,2);
  6451. maxlen=script_getnum(st,3);
  6452. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6453. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6454. tmpstr[maxlen]='\0';
  6455. script_pushstr(st,tmpstr);
  6456. return 0;
  6457. }
  6458. /*==========================================
  6459. * カプラ倉庫を開く
  6460. *------------------------------------------
  6461. */
  6462. BUILDIN_FUNC(openstorage)
  6463. {
  6464. storage_storageopen(script_rid2sd(st));
  6465. return 0;
  6466. }
  6467. BUILDIN_FUNC(guildopenstorage)
  6468. {
  6469. struct map_session_data *sd=script_rid2sd(st);
  6470. int ret;
  6471. ret = storage_guild_storageopen(sd);
  6472. script_pushint(st,ret);
  6473. return 0;
  6474. }
  6475. /*==========================================
  6476. * アイテムによるスキル発動
  6477. *------------------------------------------
  6478. */
  6479. BUILDIN_FUNC(itemskill)
  6480. {
  6481. int id,lv;
  6482. struct map_session_data *sd=script_rid2sd(st);
  6483. id=script_getnum(st,2);
  6484. lv=script_getnum(st,3);
  6485. // 詠唱中にスキルアイテムは使用できない
  6486. if(sd->ud.skilltimer != -1)
  6487. return 0;
  6488. sd->skillitem=id;
  6489. sd->skillitemlv=lv;
  6490. clif_item_skill(sd,id,lv);
  6491. return 0;
  6492. }
  6493. /*==========================================
  6494. * アイテム作成
  6495. *------------------------------------------
  6496. */
  6497. BUILDIN_FUNC(produce)
  6498. {
  6499. int trigger;
  6500. struct map_session_data *sd=script_rid2sd(st);
  6501. trigger=script_getnum(st,2);
  6502. clif_skill_produce_mix_list(sd, trigger);
  6503. return 0;
  6504. }
  6505. /*==========================================
  6506. * NPCでペット作る
  6507. *------------------------------------------
  6508. */
  6509. BUILDIN_FUNC(makepet)
  6510. {
  6511. struct map_session_data *sd = script_rid2sd(st);
  6512. int id,pet_id;
  6513. id=script_getnum(st,2);
  6514. pet_id = search_petDB_index(id, PET_CLASS);
  6515. if (pet_id < 0)
  6516. pet_id = search_petDB_index(id, PET_EGG);
  6517. if (pet_id >= 0 && sd) {
  6518. sd->catch_target_class = pet_db[pet_id].class_;
  6519. intif_create_pet(
  6520. sd->status.account_id, sd->status.char_id,
  6521. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6522. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6523. 100, 0, 1, pet_db[pet_id].jname);
  6524. }
  6525. return 0;
  6526. }
  6527. /*==========================================
  6528. * NPCで経験値上げる
  6529. *------------------------------------------
  6530. */
  6531. BUILDIN_FUNC(getexp)
  6532. {
  6533. struct map_session_data *sd = script_rid2sd(st);
  6534. int base=0,job=0;
  6535. base=script_getnum(st,2);
  6536. job =script_getnum(st,3);
  6537. if(base<0 || job<0)
  6538. return 0;
  6539. if(sd)
  6540. pc_gainexp(sd,NULL,base,job);
  6541. return 0;
  6542. }
  6543. /*==========================================
  6544. * Gain guild exp [Celest]
  6545. *------------------------------------------
  6546. */
  6547. BUILDIN_FUNC(guildgetexp)
  6548. {
  6549. struct map_session_data *sd = script_rid2sd(st);
  6550. int exp;
  6551. exp = script_getnum(st,2);
  6552. if(exp < 0)
  6553. return 0;
  6554. if(sd && sd->status.guild_id > 0)
  6555. guild_getexp (sd, exp);
  6556. return 0;
  6557. }
  6558. /*==========================================
  6559. * Changes the guild master of a guild [Skotlex]
  6560. *------------------------------------------
  6561. */
  6562. BUILDIN_FUNC(guildchangegm)
  6563. {
  6564. struct map_session_data *sd;
  6565. int guild_id;
  6566. const char *name;
  6567. guild_id = script_getnum(st,2);
  6568. name = script_getstr(st,3);
  6569. sd=map_nick2sd(name);
  6570. if (!sd)
  6571. script_pushint(st,0);
  6572. else
  6573. script_pushint(st,guild_gm_change(guild_id, sd));
  6574. return 0;
  6575. }
  6576. /*==========================================
  6577. * モンスター発生
  6578. *------------------------------------------
  6579. */
  6580. BUILDIN_FUNC(monster)
  6581. {
  6582. int class_,amount,x,y;
  6583. const char *str,*map,*event="";
  6584. map =script_getstr(st,2);
  6585. x =script_getnum(st,3);
  6586. y =script_getnum(st,4);
  6587. str =script_getstr(st,5);
  6588. class_=script_getnum(st,6);
  6589. amount=script_getnum(st,7);
  6590. if( script_hasdata(st,8) ){
  6591. event=script_getstr(st,8);
  6592. check_event(st, event);
  6593. }
  6594. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6595. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6596. return 1;
  6597. }
  6598. mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,amount,event);
  6599. return 0;
  6600. }
  6601. /*==========================================
  6602. * モンスター発生
  6603. *------------------------------------------
  6604. */
  6605. BUILDIN_FUNC(areamonster)
  6606. {
  6607. int class_,amount,x0,y0,x1,y1;
  6608. const char *str,*map,*event="";
  6609. map =script_getstr(st,2);
  6610. x0 =script_getnum(st,3);
  6611. y0 =script_getnum(st,4);
  6612. x1 =script_getnum(st,5);
  6613. y1 =script_getnum(st,6);
  6614. str =script_getstr(st,7);
  6615. class_=script_getnum(st,8);
  6616. amount=script_getnum(st,9);
  6617. if( script_hasdata(st,10) ){
  6618. event=script_getstr(st,10);
  6619. check_event(st, event);
  6620. }
  6621. mob_once_spawn_area(map_id2sd(st->rid),map,x0,y0,x1,y1,str,class_,amount,event);
  6622. return 0;
  6623. }
  6624. /*==========================================
  6625. * モンスター削除
  6626. *------------------------------------------
  6627. */
  6628. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6629. {
  6630. TBL_MOB* md = (TBL_MOB*)bl;
  6631. char *event=va_arg(ap,char *);
  6632. int allflag=va_arg(ap,int);
  6633. if(!allflag){
  6634. if(strcmp(event,md->npc_event)==0)
  6635. status_kill(bl);
  6636. }else{
  6637. if(!md->spawn)
  6638. status_kill(bl);
  6639. }
  6640. return 0;
  6641. }
  6642. BUILDIN_FUNC(killmonster)
  6643. {
  6644. const char *mapname,*event;
  6645. int m,allflag=0;
  6646. mapname=script_getstr(st,2);
  6647. event=script_getstr(st,3);
  6648. if(strcmp(event,"All")==0)
  6649. allflag = 1;
  6650. else
  6651. check_event(st, event);
  6652. if( (m=map_mapname2mapid(mapname))<0 )
  6653. return 0;
  6654. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6655. return 0;
  6656. }
  6657. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6658. {
  6659. status_kill(bl);
  6660. return 0;
  6661. }
  6662. BUILDIN_FUNC(killmonsterall)
  6663. {
  6664. const char *mapname;
  6665. int m;
  6666. mapname=script_getstr(st,2);
  6667. if( (m=map_mapname2mapid(mapname))<0 )
  6668. return 0;
  6669. map_foreachinmap(buildin_killmonsterall_sub,
  6670. m,BL_MOB);
  6671. return 0;
  6672. }
  6673. /*==========================================
  6674. * Creates a clone of a player.
  6675. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6676. *------------------------------------------
  6677. */
  6678. BUILDIN_FUNC(clone)
  6679. {
  6680. struct map_session_data *sd, *msd=NULL;
  6681. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6682. unsigned int duration = 0;
  6683. const char *map,*event="";
  6684. map=script_getstr(st,2);
  6685. x=script_getnum(st,3);
  6686. y=script_getnum(st,4);
  6687. event=script_getstr(st,5);
  6688. char_id=script_getnum(st,6);
  6689. if( script_hasdata(st,7) )
  6690. master_id=script_getnum(st,7);
  6691. if( script_hasdata(st,8) )
  6692. mode=script_getnum(st,8);
  6693. if( script_hasdata(st,9) )
  6694. flag=script_getnum(st,9);
  6695. if( script_hasdata(st,10) )
  6696. duration=script_getnum(st,10);
  6697. check_event(st, event);
  6698. m = map_mapname2mapid(map);
  6699. if (m < 0) return 0;
  6700. sd = map_charid2sd(char_id);
  6701. if (master_id) {
  6702. msd = map_charid2sd(master_id);
  6703. if (msd)
  6704. master_id = msd->bl.id;
  6705. else
  6706. master_id = 0;
  6707. }
  6708. if (sd) //Return ID of newly crafted clone.
  6709. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6710. else //Failed to create clone.
  6711. script_pushint(st,0);
  6712. return 0;
  6713. }
  6714. /*==========================================
  6715. * イベント実行
  6716. *------------------------------------------
  6717. */
  6718. BUILDIN_FUNC(doevent)
  6719. {
  6720. const char *event;
  6721. event=script_getstr(st,2);
  6722. check_event(st, event);
  6723. npc_event(map_id2sd(st->rid),event,0);
  6724. return 0;
  6725. }
  6726. /*==========================================
  6727. * NPC主体イベント実行
  6728. *------------------------------------------
  6729. */
  6730. BUILDIN_FUNC(donpcevent)
  6731. {
  6732. const char *event;
  6733. event=script_getstr(st,2);
  6734. check_event(st, event);
  6735. npc_event_do(event);
  6736. return 0;
  6737. }
  6738. /*==========================================
  6739. * イベントタイマー追加
  6740. *------------------------------------------
  6741. */
  6742. BUILDIN_FUNC(addtimer)
  6743. {
  6744. const char *event;
  6745. int tick;
  6746. tick=script_getnum(st,2);
  6747. event=script_getstr(st, 3);
  6748. check_event(st, event);
  6749. pc_addeventtimer(script_rid2sd(st),tick,event);
  6750. return 0;
  6751. }
  6752. /*==========================================
  6753. * イベントタイマー削除
  6754. *------------------------------------------
  6755. */
  6756. BUILDIN_FUNC(deltimer)
  6757. {
  6758. const char *event;
  6759. event=script_getstr(st, 2);
  6760. check_event(st, event);
  6761. pc_deleventtimer(script_rid2sd(st),event);
  6762. return 0;
  6763. }
  6764. /*==========================================
  6765. * イベントタイマーのカウント値追加
  6766. *------------------------------------------
  6767. */
  6768. BUILDIN_FUNC(addtimercount)
  6769. {
  6770. const char *event;
  6771. int tick;
  6772. event=script_getstr(st, 2);
  6773. tick=script_getnum(st,3);
  6774. check_event(st, event);
  6775. pc_addeventtimercount(script_rid2sd(st),event,tick);
  6776. return 0;
  6777. }
  6778. /*==========================================
  6779. * NPCタイマー初期化
  6780. *------------------------------------------
  6781. */
  6782. BUILDIN_FUNC(initnpctimer)
  6783. {
  6784. struct npc_data *nd;
  6785. int flag = 0;
  6786. if( script_hasdata(st,3) )
  6787. { //Two arguments: NPC name and attach flag.
  6788. nd = npc_name2id(script_getstr(st, 2));
  6789. flag = script_getnum(st,3);
  6790. } else
  6791. if( script_hasdata(st,2) )
  6792. { //Check if argument is numeric (flag) or string (npc name)
  6793. struct script_data *data;
  6794. data = script_getdata(st,2);
  6795. get_val(st,data);
  6796. if( data_isstring(data) ) //NPC name
  6797. nd = npc_name2id(script_getstr(st, 2));
  6798. else if( data_isint(data) ) { //Flag
  6799. nd = (struct npc_data *)map_id2bl(st->oid);
  6800. flag = script_getnum(st,3);
  6801. } else {
  6802. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6803. return 1;
  6804. }
  6805. } else
  6806. nd=(struct npc_data *)map_id2bl(st->oid);
  6807. if (!nd) return 0;
  6808. if (flag) { //Attach
  6809. TBL_PC* sd = script_rid2sd(st);
  6810. if (sd) nd->u.scr.rid = sd->bl.id;
  6811. }
  6812. npc_settimerevent_tick(nd,0);
  6813. npc_timerevent_start(nd, st->rid);
  6814. return 0;
  6815. }
  6816. /*==========================================
  6817. * NPCタイマー開始
  6818. *------------------------------------------
  6819. */
  6820. BUILDIN_FUNC(startnpctimer)
  6821. {
  6822. struct npc_data *nd;
  6823. int flag = 0;
  6824. if( script_hasdata(st,3) )
  6825. { //Two arguments: NPC name and attach flag.
  6826. nd = npc_name2id(script_getstr(st, 2));
  6827. flag = script_getnum(st,3);
  6828. } else
  6829. if( script_hasdata(st,2) )
  6830. { //Check if argument is numeric (flag) or string (npc name)
  6831. struct script_data *data;
  6832. data = script_getdata(st,2);
  6833. get_val(st,data);
  6834. if( data_isstring(data) ) //NPC name
  6835. nd = npc_name2id(script_getstr(st, 2));
  6836. else if( data_isint(data) ) { //Flag
  6837. nd = (struct npc_data *)map_id2bl(st->oid);
  6838. flag = script_getnum(st,3);
  6839. } else {
  6840. ShowError("startnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6841. return 1;
  6842. }
  6843. } else
  6844. nd=(struct npc_data *)map_id2bl(st->oid);
  6845. if (!nd) return 0;
  6846. if (flag) { //Attach
  6847. TBL_PC* sd = script_rid2sd(st);
  6848. if (sd) nd->u.scr.rid = sd->bl.id;
  6849. }
  6850. npc_timerevent_start(nd, st->rid);
  6851. return 0;
  6852. }
  6853. /*==========================================
  6854. * NPCタイマー停止
  6855. *------------------------------------------
  6856. */
  6857. BUILDIN_FUNC(stopnpctimer)
  6858. {
  6859. struct npc_data *nd;
  6860. int flag = 0;
  6861. if( script_hasdata(st,3) )
  6862. { //Two arguments: NPC name and attach flag.
  6863. nd = npc_name2id(script_getstr(st, 2));
  6864. flag = script_getnum(st,3);
  6865. } else
  6866. if( script_hasdata(st,2) )
  6867. { //Check if argument is numeric (flag) or string (npc name)
  6868. struct script_data *data;
  6869. data = script_getdata(st,2);
  6870. get_val(st,data);
  6871. if( data_isstring(data) ) //NPC name
  6872. nd = npc_name2id(script_getstr(st, 2));
  6873. else if( data_isint(data) ) { //Flag
  6874. nd = (struct npc_data *)map_id2bl(st->oid);
  6875. flag = script_getnum(st,3);
  6876. } else {
  6877. ShowError("stopnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6878. return 1;
  6879. }
  6880. } else
  6881. nd=(struct npc_data *)map_id2bl(st->oid);
  6882. if (!nd) return 0;
  6883. if (flag) //Detach
  6884. nd->u.scr.rid = 0;
  6885. npc_timerevent_stop(nd);
  6886. return 0;
  6887. }
  6888. /*==========================================
  6889. * NPCタイマー情報所得
  6890. *------------------------------------------
  6891. */
  6892. BUILDIN_FUNC(getnpctimer)
  6893. {
  6894. struct npc_data *nd;
  6895. struct map_session_data *sd;
  6896. int type=script_getnum(st,2);
  6897. int val=0;
  6898. if( script_hasdata(st,3) )
  6899. nd = npc_name2id(script_getstr(st,3));
  6900. else
  6901. nd = (struct npc_data *)map_id2bl(st->oid);
  6902. if (!nd || nd->bl.type != BL_NPC)
  6903. {
  6904. script_pushint(st,0);
  6905. if (battle_config.error_log)
  6906. ShowError("getnpctimer: Invalid NPC\n");
  6907. return 1;
  6908. }
  6909. switch(type){
  6910. case 0: val=npc_gettimerevent_tick(nd); break;
  6911. case 1:
  6912. if (nd->u.scr.rid) {
  6913. sd = map_id2sd(nd->u.scr.rid);
  6914. if (!sd) {
  6915. if(battle_config.error_log)
  6916. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6917. break;
  6918. }
  6919. val = (sd->npc_timer_id != -1);
  6920. } else
  6921. val= (nd->u.scr.timerid !=-1);
  6922. break;
  6923. case 2: val= nd->u.scr.timeramount; break;
  6924. }
  6925. script_pushint(st,val);
  6926. return 0;
  6927. }
  6928. /*==========================================
  6929. * NPCタイマー値設定
  6930. *------------------------------------------
  6931. */
  6932. BUILDIN_FUNC(setnpctimer)
  6933. {
  6934. int tick;
  6935. struct npc_data *nd;
  6936. tick=script_getnum(st,2);
  6937. if( script_hasdata(st,3) )
  6938. nd=npc_name2id(script_getstr(st,3));
  6939. else
  6940. nd=(struct npc_data *)map_id2bl(st->oid);
  6941. npc_settimerevent_tick(nd,tick);
  6942. return 0;
  6943. }
  6944. /*==========================================
  6945. * attaches the player rid to the timer [Celest]
  6946. *------------------------------------------
  6947. */
  6948. BUILDIN_FUNC(attachnpctimer)
  6949. {
  6950. struct map_session_data *sd;
  6951. struct npc_data *nd;
  6952. nd=(struct npc_data *)map_id2bl(st->oid);
  6953. if( script_hasdata(st,2) )
  6954. sd=map_nick2sd(script_getstr(st,2));
  6955. else
  6956. sd = script_rid2sd(st);
  6957. if (sd==NULL)
  6958. return 0;
  6959. nd->u.scr.rid = sd->bl.id;
  6960. return 0;
  6961. }
  6962. /*==========================================
  6963. * detaches a player rid from the timer [Celest]
  6964. *------------------------------------------
  6965. */
  6966. BUILDIN_FUNC(detachnpctimer)
  6967. {
  6968. struct npc_data *nd;
  6969. if( script_hasdata(st,2) )
  6970. nd=npc_name2id(script_getstr(st,2));
  6971. else
  6972. nd=(struct npc_data *)map_id2bl(st->oid);
  6973. nd->u.scr.rid = 0;
  6974. return 0;
  6975. }
  6976. /*==========================================
  6977. * To avoid "player not attached" script errors, this function is provided,
  6978. * it checks if there is a player attached to the current script. [Skotlex]
  6979. * If no, returns 0, if yes, returns the account_id of the attached player.
  6980. *------------------------------------------
  6981. */
  6982. BUILDIN_FUNC(playerattached)
  6983. {
  6984. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  6985. script_pushint(st,0);
  6986. else
  6987. script_pushint(st,st->rid);
  6988. return 0;
  6989. }
  6990. /*==========================================
  6991. * 天の声アナウンス
  6992. *------------------------------------------
  6993. */
  6994. BUILDIN_FUNC(announce)
  6995. {
  6996. const char *str, *color=NULL;
  6997. int flag;
  6998. str=script_getstr(st,2);
  6999. flag=script_getnum(st,3);
  7000. if (script_hasdata(st,4))
  7001. color=script_getstr(st,4);
  7002. if(flag&0x0f){
  7003. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  7004. (struct block_list *)script_rid2sd(st);
  7005. if (color)
  7006. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  7007. else
  7008. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  7009. }else {
  7010. if (color)
  7011. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  7012. else
  7013. intif_GMmessage(str,(int)strlen(str)+1,flag);
  7014. }
  7015. return 0;
  7016. }
  7017. /*==========================================
  7018. * 天の声アナウンス(特定マップ)
  7019. *------------------------------------------
  7020. */
  7021. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  7022. {
  7023. char *str, *color;
  7024. int len,flag;
  7025. str=va_arg(ap,char *);
  7026. len=va_arg(ap,int);
  7027. flag=va_arg(ap,int);
  7028. color=va_arg(ap,char *);
  7029. if (color)
  7030. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7031. else
  7032. clif_GMmessage(bl,str,len,flag|3);
  7033. return 0;
  7034. }
  7035. BUILDIN_FUNC(mapannounce)
  7036. {
  7037. const char *mapname,*str, *color=NULL;
  7038. int flag,m;
  7039. mapname=script_getstr(st,2);
  7040. str=script_getstr(st,3);
  7041. flag=script_getnum(st,4);
  7042. if (script_hasdata(st,5))
  7043. color=script_getstr(st,5);
  7044. if( (m=map_mapname2mapid(mapname))<0 )
  7045. return 0;
  7046. map_foreachinmap(buildin_mapannounce_sub,
  7047. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7048. return 0;
  7049. }
  7050. /*==========================================
  7051. * 天の声アナウンス(特定エリア)
  7052. *------------------------------------------
  7053. */
  7054. BUILDIN_FUNC(areaannounce)
  7055. {
  7056. const char *map,*str,*color=NULL;
  7057. int flag,m;
  7058. int x0,y0,x1,y1;
  7059. map=script_getstr(st,2);
  7060. x0=script_getnum(st,3);
  7061. y0=script_getnum(st,4);
  7062. x1=script_getnum(st,5);
  7063. y1=script_getnum(st,6);
  7064. str=script_getstr(st,7);
  7065. flag=script_getnum(st,8);
  7066. if (script_hasdata(st,9))
  7067. color=script_getstr(st,9);
  7068. if( (m=map_mapname2mapid(map))<0 )
  7069. return 0;
  7070. map_foreachinarea(buildin_mapannounce_sub,
  7071. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7072. return 0;
  7073. }
  7074. /*==========================================
  7075. * ユーザー数所得
  7076. *------------------------------------------
  7077. */
  7078. BUILDIN_FUNC(getusers)
  7079. {
  7080. int flag=script_getnum(st,2);
  7081. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7082. int val=0;
  7083. switch(flag&0x07){
  7084. case 0: val=map[bl->m].users; break;
  7085. case 1: val=map_getusers(); break;
  7086. }
  7087. script_pushint(st,val);
  7088. return 0;
  7089. }
  7090. /*==========================================
  7091. * Works like @WHO - displays all online users names in window
  7092. *------------------------------------------
  7093. */
  7094. BUILDIN_FUNC(getusersname)
  7095. {
  7096. struct map_session_data *sd, *pl_sd = NULL, **pl_allsd;
  7097. int i=0,disp_num=1, users;
  7098. sd = script_rid2sd(st);
  7099. if (!sd) return 0;
  7100. pl_allsd = map_getallusers(&users);
  7101. for (i=0;i<users;i++)
  7102. {
  7103. pl_sd = pl_allsd[i];
  7104. if( !(battle_config.hide_GM_session && pc_isGM(pl_sd)) )
  7105. {
  7106. if((disp_num++)%10==0)
  7107. clif_scriptnext(sd,st->oid);
  7108. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7109. }
  7110. }
  7111. return 0;
  7112. }
  7113. /*==========================================
  7114. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7115. *------------------------------------------
  7116. */
  7117. BUILDIN_FUNC(getmapguildusers)
  7118. {
  7119. const char *str;
  7120. int m, gid;
  7121. int i=0,c=0;
  7122. struct guild *g = NULL;
  7123. str=script_getstr(st,2);
  7124. gid=script_getnum(st,3);
  7125. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7126. script_pushint(st,-1);
  7127. return 0;
  7128. }
  7129. g = guild_search(gid);
  7130. if (g){
  7131. for(i = 0; i < g->max_member; i++)
  7132. {
  7133. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7134. c++;
  7135. }
  7136. }
  7137. script_pushint(st,c);
  7138. return 0;
  7139. }
  7140. /*==========================================
  7141. * マップ指定ユーザー数所得
  7142. *------------------------------------------
  7143. */
  7144. BUILDIN_FUNC(getmapusers)
  7145. {
  7146. const char *str;
  7147. int m;
  7148. str=script_getstr(st,2);
  7149. if( (m=map_mapname2mapid(str))< 0){
  7150. script_pushint(st,-1);
  7151. return 0;
  7152. }
  7153. script_pushint(st,map[m].users);
  7154. return 0;
  7155. }
  7156. /*==========================================
  7157. * エリア指定ユーザー数所得
  7158. *------------------------------------------
  7159. */
  7160. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7161. {
  7162. int *users=va_arg(ap,int *);
  7163. (*users)++;
  7164. return 0;
  7165. }
  7166. BUILDIN_FUNC(getareausers)
  7167. {
  7168. const char *str;
  7169. int m,x0,y0,x1,y1,users=0;
  7170. str=script_getstr(st,2);
  7171. x0=script_getnum(st,3);
  7172. y0=script_getnum(st,4);
  7173. x1=script_getnum(st,5);
  7174. y1=script_getnum(st,6);
  7175. if( (m=map_mapname2mapid(str))< 0){
  7176. script_pushint(st,-1);
  7177. return 0;
  7178. }
  7179. map_foreachinarea(buildin_getareausers_sub,
  7180. m,x0,y0,x1,y1,BL_PC,&users);
  7181. script_pushint(st,users);
  7182. return 0;
  7183. }
  7184. /*==========================================
  7185. * エリア指定ドロップアイテム数所得
  7186. *------------------------------------------
  7187. */
  7188. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7189. {
  7190. int item=va_arg(ap,int);
  7191. int *amount=va_arg(ap,int *);
  7192. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7193. if(drop->item_data.nameid==item)
  7194. (*amount)+=drop->item_data.amount;
  7195. return 0;
  7196. }
  7197. BUILDIN_FUNC(getareadropitem)
  7198. {
  7199. const char *str;
  7200. int m,x0,y0,x1,y1,item,amount=0;
  7201. struct script_data *data;
  7202. str=script_getstr(st,2);
  7203. x0=script_getnum(st,3);
  7204. y0=script_getnum(st,4);
  7205. x1=script_getnum(st,5);
  7206. y1=script_getnum(st,6);
  7207. data=script_getdata(st,7);
  7208. get_val(st,data);
  7209. if( data_isstring(data) ){
  7210. const char *name=conv_str(st,data);
  7211. struct item_data *item_data = itemdb_searchname(name);
  7212. item=UNKNOWN_ITEM_ID;
  7213. if( item_data )
  7214. item=item_data->nameid;
  7215. }else
  7216. item=conv_num(st,data);
  7217. if( (m=map_mapname2mapid(str))< 0){
  7218. script_pushint(st,-1);
  7219. return 0;
  7220. }
  7221. map_foreachinarea(buildin_getareadropitem_sub,
  7222. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7223. script_pushint(st,amount);
  7224. return 0;
  7225. }
  7226. /*==========================================
  7227. * NPCの有効化
  7228. *------------------------------------------
  7229. */
  7230. BUILDIN_FUNC(enablenpc)
  7231. {
  7232. const char *str;
  7233. str=script_getstr(st,2);
  7234. npc_enable(str,1);
  7235. return 0;
  7236. }
  7237. /*==========================================
  7238. * NPCの無効化
  7239. *------------------------------------------
  7240. */
  7241. BUILDIN_FUNC(disablenpc)
  7242. {
  7243. const char *str;
  7244. str=script_getstr(st,2);
  7245. npc_enable(str,0);
  7246. return 0;
  7247. }
  7248. BUILDIN_FUNC(enablearena) // Added by RoVeRT
  7249. {
  7250. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  7251. struct chat_data *cd;
  7252. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL)
  7253. return 0;
  7254. npc_enable(nd->name,1);
  7255. nd->arenaflag=1;
  7256. if(cd->users>=cd->trigger && cd->npc_event[0])
  7257. npc_timer_event(cd->npc_event);
  7258. return 0;
  7259. }
  7260. BUILDIN_FUNC(disablearena) // Added by RoVeRT
  7261. {
  7262. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  7263. nd->arenaflag=0;
  7264. return 0;
  7265. }
  7266. /*==========================================
  7267. * 隠れているNPCの表示
  7268. *------------------------------------------
  7269. */
  7270. BUILDIN_FUNC(hideoffnpc)
  7271. {
  7272. const char *str;
  7273. str=script_getstr(st,2);
  7274. npc_enable(str,2);
  7275. return 0;
  7276. }
  7277. /*==========================================
  7278. * NPCをハイディング
  7279. *------------------------------------------
  7280. */
  7281. BUILDIN_FUNC(hideonnpc)
  7282. {
  7283. const char *str;
  7284. str=script_getstr(st,2);
  7285. npc_enable(str,4);
  7286. return 0;
  7287. }
  7288. /*==========================================
  7289. * 状態異常にかかる
  7290. *------------------------------------------
  7291. */
  7292. BUILDIN_FUNC(sc_start)
  7293. {
  7294. struct block_list *bl;
  7295. int type,tick,val1,val4=0;
  7296. type=script_getnum(st,2);
  7297. tick=script_getnum(st,3);
  7298. val1=script_getnum(st,4);
  7299. if( script_hasdata(st,5) ) //指定したキャラを状態異常にする
  7300. bl = map_id2bl(script_getnum(st,5));
  7301. else
  7302. bl = map_id2bl(st->rid);
  7303. if (potion_flag==1 && potion_target) {
  7304. bl = map_id2bl(potion_target);
  7305. tick/=2; //Thrown potions only last half.
  7306. val4 = 1; //Mark that this was a thrown sc_effect
  7307. }
  7308. if (type >= 0 && type < SC_MAX && val1 && !tick)
  7309. { //When there isn't a duration specified, try to get it from the skill_db
  7310. tick = StatusSkillChangeTable[type];
  7311. if (tick)
  7312. tick = skill_get_time(tick,val1);
  7313. else //Failed to retrieve duration, reset to what it was.
  7314. tick = 0;
  7315. }
  7316. if (bl)
  7317. status_change_start(bl,type,10000,val1,0,0,val4,tick,11);
  7318. return 0;
  7319. }
  7320. /*==========================================
  7321. * 状態異常にかかる(確率指定)
  7322. *------------------------------------------
  7323. */
  7324. BUILDIN_FUNC(sc_start2)
  7325. {
  7326. struct block_list *bl;
  7327. int type,tick,val1,val4=0,per;
  7328. type=script_getnum(st,2);
  7329. tick=script_getnum(st,3);
  7330. val1=script_getnum(st,4);
  7331. per=script_getnum(st,5);
  7332. if( script_hasdata(st,6) ) //指定したキャラを状態異常にする
  7333. bl = map_id2bl(script_getnum(st,6));
  7334. else
  7335. bl = map_id2bl(st->rid);
  7336. if (type >= 0 && type < SC_MAX && val1 && !tick)
  7337. { //When there isn't a duration specified, try to get it from the skill_db
  7338. tick = StatusSkillChangeTable[type];
  7339. if (tick)
  7340. tick = skill_get_time(tick,val1);
  7341. else //Failed to retrieve duration, reset to what it was.
  7342. tick = 0;
  7343. }
  7344. if (potion_flag==1 && potion_target) {
  7345. bl = map_id2bl(potion_target);
  7346. tick/=2;
  7347. val4 = 1;
  7348. }
  7349. if(bl)
  7350. status_change_start(bl,type,per,val1,0,0,val4,tick,11);
  7351. return 0;
  7352. }
  7353. /*==========================================
  7354. * Starts a SC_ change with the four values passed. [Skotlex]
  7355. * Final optional argument is the ID of player to affect.
  7356. * sc_start4 type, duration, val1, val2, val3, val4, <id>;
  7357. *------------------------------------------
  7358. */
  7359. BUILDIN_FUNC(sc_start4)
  7360. {
  7361. struct block_list *bl;
  7362. int type,tick,val1,val2,val3,val4;
  7363. type=script_getnum(st,2);
  7364. tick=script_getnum(st,3);
  7365. val1=script_getnum(st,4);
  7366. val2=script_getnum(st,5);
  7367. val3=script_getnum(st,6);
  7368. val4=script_getnum(st,7);
  7369. if( script_hasdata(st,8) )
  7370. bl = map_id2bl(script_getnum(st,8));
  7371. else
  7372. bl = map_id2bl(st->rid);
  7373. if (type >= 0 && type < SC_MAX && val1 && !tick)
  7374. { //When there isn't a duration specified, try to get it from the skill_db
  7375. tick = StatusSkillChangeTable[type];
  7376. if (tick)
  7377. tick = skill_get_time(tick,val1);
  7378. else //Failed to retrieve duration, reset to what it was.
  7379. tick = 0;
  7380. }
  7381. if (potion_flag==1 && potion_target) {
  7382. bl = map_id2bl(potion_target);
  7383. tick/=2;
  7384. }
  7385. if (bl)
  7386. status_change_start(bl,type,10000,val1,val2,val3,val4,tick,11);
  7387. return 0;
  7388. }
  7389. /*==========================================
  7390. * 状態異常が直る
  7391. *------------------------------------------
  7392. */
  7393. BUILDIN_FUNC(sc_end)
  7394. {
  7395. struct block_list *bl;
  7396. int type;
  7397. type=script_getnum(st,2);
  7398. bl = map_id2bl(st->rid);
  7399. if (potion_flag==1 && potion_target)
  7400. bl = map_id2bl(potion_target);
  7401. if (!bl) return 0;
  7402. if (type >= 0)
  7403. status_change_end(bl,type,-1);
  7404. else
  7405. status_change_clear(bl, 2);
  7406. return 0;
  7407. }
  7408. /*==========================================
  7409. * 状態異常耐性を計算した確率を返す
  7410. *------------------------------------------
  7411. */
  7412. BUILDIN_FUNC(getscrate)
  7413. {
  7414. struct block_list *bl;
  7415. int type,rate;
  7416. type=script_getnum(st,2);
  7417. rate=script_getnum(st,3);
  7418. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7419. bl = map_id2bl(script_getnum(st,4));
  7420. else
  7421. bl = map_id2bl(st->rid);
  7422. if (bl)
  7423. rate = status_get_sc_def(bl,type, 10000, 10000, 0);
  7424. script_pushint(st,rate);
  7425. return 0;
  7426. }
  7427. /*==========================================
  7428. *
  7429. *------------------------------------------
  7430. */
  7431. BUILDIN_FUNC(debugmes)
  7432. {
  7433. const char *str;
  7434. str=script_getstr(st,2);
  7435. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7436. return 0;
  7437. }
  7438. /*==========================================
  7439. *捕獲アイテム使用
  7440. *------------------------------------------
  7441. */
  7442. BUILDIN_FUNC(catchpet)
  7443. {
  7444. int pet_id;
  7445. struct map_session_data *sd;
  7446. pet_id= script_getnum(st,2);
  7447. sd=script_rid2sd(st);
  7448. pet_catch_process1(sd,pet_id);
  7449. return 0;
  7450. }
  7451. /*==========================================
  7452. * [orn]
  7453. *------------------------------------------
  7454. */
  7455. BUILDIN_FUNC(homunculus_evolution)
  7456. {
  7457. struct map_session_data *sd;
  7458. sd=script_rid2sd(st);
  7459. if ( sd->hd && sd->hd->homunculusDB->evo_class && sd->hd->homunculus.intimacy > 91000 ) {
  7460. return !merc_hom_evolution(sd->hd) ;
  7461. }
  7462. clif_emotion(&sd->hd->bl, 4) ; //swt
  7463. return 0;
  7464. }
  7465. //These two functions bring the eA MAPID_* class functionality to scripts.
  7466. BUILDIN_FUNC(eaclass)
  7467. {
  7468. int class_;
  7469. if( script_hasdata(st,2) )
  7470. class_ = script_getnum(st,2);
  7471. else {
  7472. struct map_session_data *sd;
  7473. sd=script_rid2sd(st);
  7474. if (!sd) {
  7475. script_pushint(st,-1);
  7476. return 0;
  7477. }
  7478. class_ = sd->status.class_;
  7479. }
  7480. script_pushint(st,pc_jobid2mapid(class_));
  7481. return 0;
  7482. }
  7483. BUILDIN_FUNC(roclass)
  7484. {
  7485. int class_ =script_getnum(st,2);
  7486. int sex;
  7487. if( script_hasdata(st,3) )
  7488. sex = script_getnum(st,3);
  7489. else {
  7490. struct map_session_data *sd;
  7491. if (st->rid && (sd=script_rid2sd(st)))
  7492. sex = sd->status.sex;
  7493. else
  7494. sex = 1; //Just use male when not found.
  7495. }
  7496. script_pushint(st,pc_mapid2jobid(class_, sex));
  7497. return 0;
  7498. }
  7499. /*==========================================
  7500. *携帯卵孵化機使用
  7501. *------------------------------------------
  7502. */
  7503. BUILDIN_FUNC(birthpet)
  7504. {
  7505. struct map_session_data *sd;
  7506. sd=script_rid2sd(st);
  7507. clif_sendegg(sd);
  7508. return 0;
  7509. }
  7510. /*==========================================
  7511. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7512. *------------------------------------------
  7513. */
  7514. BUILDIN_FUNC(resetlvl)
  7515. {
  7516. struct map_session_data *sd;
  7517. int type=script_getnum(st,2);
  7518. sd=script_rid2sd(st);
  7519. pc_resetlvl(sd,type);
  7520. return 0;
  7521. }
  7522. /*==========================================
  7523. * ステータスリセット
  7524. *------------------------------------------
  7525. */
  7526. BUILDIN_FUNC(resetstatus)
  7527. {
  7528. struct map_session_data *sd;
  7529. sd=script_rid2sd(st);
  7530. pc_resetstate(sd);
  7531. return 0;
  7532. }
  7533. /*==========================================
  7534. * script command resetskill
  7535. *------------------------------------------
  7536. */
  7537. BUILDIN_FUNC(resetskill)
  7538. {
  7539. struct map_session_data *sd;
  7540. sd=script_rid2sd(st);
  7541. pc_resetskill(sd,1);
  7542. return 0;
  7543. }
  7544. /*==========================================
  7545. * Counts total amount of skill points.
  7546. *------------------------------------------
  7547. */
  7548. BUILDIN_FUNC(skillpointcount)
  7549. {
  7550. struct map_session_data *sd;
  7551. sd=script_rid2sd(st);
  7552. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7553. return 0;
  7554. }
  7555. /*==========================================
  7556. *
  7557. *------------------------------------------
  7558. */
  7559. BUILDIN_FUNC(changebase)
  7560. {
  7561. struct map_session_data *sd=NULL;
  7562. int vclass;
  7563. if( script_hasdata(st,3) )
  7564. sd=map_id2sd(script_getnum(st,3));
  7565. else
  7566. sd=script_rid2sd(st);
  7567. if(sd == NULL)
  7568. return 0;
  7569. vclass = script_getnum(st,2);
  7570. if(vclass == JOB_WEDDING)
  7571. {
  7572. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7573. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7574. )
  7575. return 0;
  7576. }
  7577. if(!sd->disguise && vclass != sd->vd.class_) {
  7578. status_set_viewdata(&sd->bl, vclass);
  7579. //Updated client view. Base, Weapon and Cloth Colors.
  7580. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7581. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7582. if (sd->vd.cloth_color)
  7583. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7584. }
  7585. return 0;
  7586. }
  7587. /*==========================================
  7588. * 性別変換
  7589. *------------------------------------------
  7590. */
  7591. BUILDIN_FUNC(changesex)
  7592. {
  7593. struct map_session_data *sd = NULL;
  7594. sd = script_rid2sd(st);
  7595. if (sd->status.sex == 0) {
  7596. sd->status.sex = 1;
  7597. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7598. sd->status.class_ -= 1;
  7599. } else if (sd->status.sex == 1) {
  7600. sd->status.sex = 0;
  7601. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7602. sd->status.class_ += 1;
  7603. }
  7604. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  7605. chrif_save(sd,0);
  7606. return 0;
  7607. }
  7608. /*==========================================
  7609. * npcチャット作成
  7610. *------------------------------------------
  7611. */
  7612. BUILDIN_FUNC(waitingroom)
  7613. {
  7614. const char *name,*ev="";
  7615. int limit, trigger = 0,pub=1;
  7616. name=script_getstr(st,2);
  7617. limit= script_getnum(st,3);
  7618. if(limit==0)
  7619. pub=3;
  7620. if( script_hasdata(st,5) ){
  7621. struct script_data* data=script_getdata(st,5);
  7622. get_val(st,data);
  7623. if( data_isstring(data) ){
  7624. // eathena仕様
  7625. trigger=script_getnum(st,4);
  7626. ev=script_getstr(st,5);
  7627. }else{
  7628. // 新Athena仕様(旧Athena仕様と互換性あり)
  7629. ev=script_getstr(st,4);
  7630. trigger=script_getnum(st,5);
  7631. }
  7632. }else{
  7633. // 旧Athena仕様
  7634. if( script_hasdata(st,4) )
  7635. ev=script_getstr(st,4);
  7636. }
  7637. chat_createnpcchat( (struct npc_data *)map_id2bl(st->oid),
  7638. limit,pub,trigger,name,(int)strlen(name)+1,ev);
  7639. return 0;
  7640. }
  7641. /*==========================================
  7642. * Works like 'announce' but outputs in the common chat window
  7643. *------------------------------------------
  7644. */
  7645. BUILDIN_FUNC(globalmes)
  7646. {
  7647. struct block_list *bl = map_id2bl(st->oid);
  7648. struct npc_data *nd = (struct npc_data *)bl;
  7649. const char *name=NULL,*mes;
  7650. mes=script_getstr(st,2); // メッセージの取得
  7651. if(mes==NULL) return 0;
  7652. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7653. name=script_getstr(st,3);
  7654. } else {
  7655. name=nd->name;
  7656. }
  7657. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7658. return 0;
  7659. }
  7660. /*==========================================
  7661. * npcチャット削除
  7662. *------------------------------------------
  7663. */
  7664. BUILDIN_FUNC(delwaitingroom)
  7665. {
  7666. struct npc_data *nd;
  7667. if( script_hasdata(st,2) )
  7668. nd=npc_name2id(script_getstr(st,2));
  7669. else
  7670. nd=(struct npc_data *)map_id2bl(st->oid);
  7671. chat_deletenpcchat(nd);
  7672. return 0;
  7673. }
  7674. /*==========================================
  7675. * npcチャット全員蹴り出す
  7676. *------------------------------------------
  7677. */
  7678. BUILDIN_FUNC(waitingroomkickall)
  7679. {
  7680. struct npc_data *nd;
  7681. struct chat_data *cd;
  7682. if( script_hasdata(st,2) )
  7683. nd=npc_name2id(script_getstr(st,2));
  7684. else
  7685. nd=(struct npc_data *)map_id2bl(st->oid);
  7686. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL )
  7687. return 0;
  7688. chat_npckickall(cd);
  7689. return 0;
  7690. }
  7691. /*==========================================
  7692. * npcチャットイベント有効化
  7693. *------------------------------------------
  7694. */
  7695. BUILDIN_FUNC(enablewaitingroomevent)
  7696. {
  7697. struct npc_data *nd;
  7698. struct chat_data *cd;
  7699. if( script_hasdata(st,2) )
  7700. nd=npc_name2id(script_getstr(st,2));
  7701. else
  7702. nd=(struct npc_data *)map_id2bl(st->oid);
  7703. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL )
  7704. return 0;
  7705. chat_enableevent(cd);
  7706. return 0;
  7707. }
  7708. /*==========================================
  7709. * npcチャットイベント無効化
  7710. *------------------------------------------
  7711. */
  7712. BUILDIN_FUNC(disablewaitingroomevent)
  7713. {
  7714. struct npc_data *nd;
  7715. struct chat_data *cd;
  7716. if( script_hasdata(st,2) )
  7717. nd=npc_name2id(script_getstr(st,2));
  7718. else
  7719. nd=(struct npc_data *)map_id2bl(st->oid);
  7720. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL )
  7721. return 0;
  7722. chat_disableevent(cd);
  7723. return 0;
  7724. }
  7725. /*==========================================
  7726. * npcチャット状態所得
  7727. *------------------------------------------
  7728. */
  7729. BUILDIN_FUNC(getwaitingroomstate)
  7730. {
  7731. struct npc_data *nd;
  7732. struct chat_data *cd;
  7733. int val=0,type;
  7734. type=script_getnum(st,2);
  7735. if( script_hasdata(st,3) )
  7736. nd=npc_name2id(script_getstr(st,3));
  7737. else
  7738. nd=(struct npc_data *)map_id2bl(st->oid);
  7739. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL ){
  7740. script_pushint(st,-1);
  7741. return 0;
  7742. }
  7743. switch(type){
  7744. case 0: val=cd->users; break;
  7745. case 1: val=cd->limit; break;
  7746. case 2: val=cd->trigger&0x7f; break;
  7747. case 3: val=((cd->trigger&0x80)>0); break;
  7748. case 32: val=(cd->users >= cd->limit); break;
  7749. case 33: val=(cd->users >= cd->trigger); break;
  7750. case 4:
  7751. script_pushconststr(st,cd->title);
  7752. return 0;
  7753. case 5:
  7754. script_pushconststr(st,cd->pass);
  7755. return 0;
  7756. case 16:
  7757. script_pushconststr(st,cd->npc_event);
  7758. return 0;
  7759. }
  7760. script_pushint(st,val);
  7761. return 0;
  7762. }
  7763. /*==========================================
  7764. * チャットメンバー(規定人数)ワープ
  7765. *------------------------------------------
  7766. */
  7767. BUILDIN_FUNC(warpwaitingpc)
  7768. {
  7769. int x,y,i,n;
  7770. const char *str;
  7771. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  7772. struct chat_data *cd;
  7773. struct map_session_data *sd;
  7774. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL )
  7775. return 0;
  7776. n=cd->trigger&0x7f;
  7777. str=script_getstr(st,2);
  7778. x=script_getnum(st,3);
  7779. y=script_getnum(st,4);
  7780. if( script_hasdata(st,5) )
  7781. n=script_getnum(st,5);
  7782. for(i=0;i<n;i++){
  7783. sd=cd->usersd[0];
  7784. if (!sd) continue; //Broken npc chat room?
  7785. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24),sd->bl.id);
  7786. if(strcmp(str,"Random")==0)
  7787. pc_randomwarp(sd,3);
  7788. else if(strcmp(str,"SavePoint")==0){
  7789. if(map[sd->bl.m].flag.noteleport) // テレポ禁止
  7790. return 0;
  7791. pc_setpos(sd,sd->status.save_point.map,
  7792. sd->status.save_point.x,sd->status.save_point.y,3);
  7793. }else
  7794. pc_setpos(sd,mapindex_name2id(str),x,y,0);
  7795. }
  7796. mapreg_setreg(add_str("$@warpwaitingpcnum"),n);
  7797. return 0;
  7798. }
  7799. /*==========================================
  7800. * RIDのアタッチ
  7801. *------------------------------------------
  7802. */
  7803. BUILDIN_FUNC(attachrid)
  7804. {
  7805. st->rid=script_getnum(st,2);
  7806. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7807. return 0;
  7808. }
  7809. /*==========================================
  7810. * RIDのデタッチ
  7811. *------------------------------------------
  7812. */
  7813. BUILDIN_FUNC(detachrid)
  7814. {
  7815. st->rid=0;
  7816. return 0;
  7817. }
  7818. /*==========================================
  7819. * 存在チェック
  7820. *------------------------------------------
  7821. */
  7822. BUILDIN_FUNC(isloggedin)
  7823. {
  7824. push_val(st->stack,C_INT, map_id2sd(
  7825. script_getnum(st,2) )!=NULL );
  7826. return 0;
  7827. }
  7828. /*==========================================
  7829. *
  7830. *------------------------------------------
  7831. */
  7832. BUILDIN_FUNC(setmapflagnosave)
  7833. {
  7834. int m,x,y;
  7835. unsigned short mapindex;
  7836. const char *str,*str2;
  7837. str=script_getstr(st,2);
  7838. str2=script_getstr(st,3);
  7839. x=script_getnum(st,4);
  7840. y=script_getnum(st,5);
  7841. m = map_mapname2mapid(str);
  7842. mapindex = mapindex_name2id(str2);
  7843. if(m >= 0 && mapindex) {
  7844. map[m].flag.nosave=1;
  7845. map[m].save.map=mapindex;
  7846. map[m].save.x=x;
  7847. map[m].save.y=y;
  7848. }
  7849. return 0;
  7850. }
  7851. BUILDIN_FUNC(setmapflag)
  7852. {
  7853. int m,i;
  7854. const char *str;
  7855. const char *val=NULL;
  7856. str=script_getstr(st,2);
  7857. i=script_getnum(st,3);
  7858. if(script_hasdata(st,4)){
  7859. val=script_getstr(st,4);
  7860. }
  7861. m = map_mapname2mapid(str);
  7862. if(m >= 0) {
  7863. switch(i) {
  7864. case MF_NOMEMO:
  7865. map[m].flag.nomemo=1;
  7866. break;
  7867. case MF_NOTELEPORT:
  7868. map[m].flag.noteleport=1;
  7869. break;
  7870. case MF_NOBRANCH:
  7871. map[m].flag.nobranch=1;
  7872. break;
  7873. case MF_NOPENALTY:
  7874. map[m].flag.noexppenalty=1;
  7875. map[m].flag.nozenypenalty=1;
  7876. break;
  7877. case MF_NOZENYPENALTY:
  7878. map[m].flag.nozenypenalty=1;
  7879. break;
  7880. case MF_PVP:
  7881. map[m].flag.pvp=1;
  7882. break;
  7883. case MF_PVP_NOPARTY:
  7884. map[m].flag.pvp_noparty=1;
  7885. break;
  7886. case MF_PVP_NOGUILD:
  7887. map[m].flag.pvp_noguild=1;
  7888. break;
  7889. case MF_GVG:
  7890. map[m].flag.gvg=1;
  7891. break;
  7892. case MF_GVG_NOPARTY:
  7893. map[m].flag.gvg_noparty=1;
  7894. break;
  7895. case MF_GVG_DUNGEON:
  7896. map[m].flag.gvg_dungeon=1;
  7897. break;
  7898. case MF_GVG_CASTLE:
  7899. map[m].flag.gvg_castle=1;
  7900. break;
  7901. case MF_NOTRADE:
  7902. map[m].flag.notrade=1;
  7903. break;
  7904. case MF_NODROP:
  7905. map[m].flag.nodrop=1;
  7906. break;
  7907. case MF_NOSKILL:
  7908. map[m].flag.noskill=1;
  7909. break;
  7910. case MF_NOWARP:
  7911. map[m].flag.nowarp=1;
  7912. break;
  7913. case MF_NOICEWALL: // [Valaris]
  7914. map[m].flag.noicewall=1;
  7915. break;
  7916. case MF_SNOW: // [Valaris]
  7917. map[m].flag.snow=1;
  7918. break;
  7919. case MF_CLOUDS:
  7920. map[m].flag.clouds=1;
  7921. break;
  7922. case MF_CLOUDS2: // [Valaris]
  7923. map[m].flag.clouds2=1;
  7924. break;
  7925. case MF_FOG: // [Valaris]
  7926. map[m].flag.fog=1;
  7927. break;
  7928. case MF_FIREWORKS:
  7929. map[m].flag.fireworks=1;
  7930. break;
  7931. case MF_SAKURA: // [Valaris]
  7932. map[m].flag.sakura=1;
  7933. break;
  7934. case MF_LEAVES: // [Valaris]
  7935. map[m].flag.leaves=1;
  7936. break;
  7937. case MF_RAIN: // [Valaris]
  7938. map[m].flag.rain=1;
  7939. break;
  7940. case MF_INDOORS: // celest
  7941. map[m].flag.indoors=1;
  7942. break;
  7943. case MF_NIGHTENABLED:
  7944. map[m].flag.nightenabled=1;
  7945. break;
  7946. case MF_NOGO: // celest
  7947. map[m].flag.nogo=1;
  7948. break;
  7949. case MF_NOBASEEXP:
  7950. map[m].flag.nobaseexp=1;
  7951. break;
  7952. case MF_NOJOBEXP:
  7953. map[m].flag.nojobexp=1;
  7954. break;
  7955. case MF_NOMOBLOOT:
  7956. map[m].flag.nomobloot=1;
  7957. break;
  7958. case MF_NOMVPLOOT:
  7959. map[m].flag.nomvploot=1;
  7960. break;
  7961. case MF_NORETURN:
  7962. map[m].flag.noreturn=1;
  7963. break;
  7964. case MF_NOWARPTO:
  7965. map[m].flag.nowarpto=1;
  7966. break;
  7967. case MF_NIGHTMAREDROP:
  7968. map[m].flag.pvp_nightmaredrop=1;
  7969. break;
  7970. case MF_RESTRICTED:
  7971. map[m].flag.restricted=1;
  7972. break;
  7973. case MF_NOCOMMAND:
  7974. map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val);
  7975. break;
  7976. case MF_JEXP:
  7977. map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val);
  7978. break;
  7979. case MF_BEXP:
  7980. map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val);
  7981. break;
  7982. case MF_NOVENDING:
  7983. map[m].flag.novending=1;
  7984. break;
  7985. case MF_LOADEVENT:
  7986. map[m].flag.loadevent=1;
  7987. break;
  7988. case MF_NOCHAT:
  7989. map[m].flag.nochat=1;
  7990. break;
  7991. case MF_PARTYLOCK:
  7992. map[m].flag.partylock=1;
  7993. break;
  7994. case MF_GUILDLOCK:
  7995. map[m].flag.guildlock=1;
  7996. break;
  7997. }
  7998. }
  7999. return 0;
  8000. }
  8001. BUILDIN_FUNC(removemapflag)
  8002. {
  8003. int m,i;
  8004. const char *str;
  8005. str=script_getstr(st,2);
  8006. i=script_getnum(st,3);
  8007. m = map_mapname2mapid(str);
  8008. if(m >= 0) {
  8009. switch(i) {
  8010. case MF_NOMEMO:
  8011. map[m].flag.nomemo=0;
  8012. break;
  8013. case MF_NOTELEPORT:
  8014. map[m].flag.noteleport=0;
  8015. break;
  8016. case MF_NOSAVE:
  8017. map[m].flag.nosave=0;
  8018. break;
  8019. case MF_NOBRANCH:
  8020. map[m].flag.nobranch=0;
  8021. break;
  8022. case MF_NOPENALTY:
  8023. map[m].flag.noexppenalty=0;
  8024. map[m].flag.nozenypenalty=0;
  8025. break;
  8026. case MF_PVP:
  8027. map[m].flag.pvp=0;
  8028. break;
  8029. case MF_PVP_NOPARTY:
  8030. map[m].flag.pvp_noparty=0;
  8031. break;
  8032. case MF_PVP_NOGUILD:
  8033. map[m].flag.pvp_noguild=0;
  8034. break;
  8035. case MF_GVG:
  8036. map[m].flag.gvg=0;
  8037. break;
  8038. case MF_GVG_NOPARTY:
  8039. map[m].flag.gvg_noparty=0;
  8040. break;
  8041. case MF_GVG_DUNGEON:
  8042. map[m].flag.gvg_dungeon=0;
  8043. break;
  8044. case MF_GVG_CASTLE:
  8045. map[m].flag.gvg_castle=0;
  8046. break;
  8047. case MF_NOZENYPENALTY:
  8048. map[m].flag.nozenypenalty=0;
  8049. break;
  8050. case MF_NOTRADE:
  8051. map[m].flag.notrade=0;
  8052. break;
  8053. case MF_NODROP:
  8054. map[m].flag.nodrop=0;
  8055. break;
  8056. case MF_NOSKILL:
  8057. map[m].flag.noskill=0;
  8058. break;
  8059. case MF_NOWARP:
  8060. map[m].flag.nowarp=0;
  8061. break;
  8062. case MF_NOICEWALL: // [Valaris]
  8063. map[m].flag.noicewall=0;
  8064. break;
  8065. case MF_SNOW: // [Valaris]
  8066. map[m].flag.snow=0;
  8067. break;
  8068. case MF_CLOUDS:
  8069. map[m].flag.clouds=0;
  8070. break;
  8071. case MF_CLOUDS2: // [Valaris]
  8072. map[m].flag.clouds2=0;
  8073. break;
  8074. case MF_FOG: // [Valaris]
  8075. map[m].flag.fog=0;
  8076. break;
  8077. case MF_FIREWORKS:
  8078. map[m].flag.fireworks=0;
  8079. break;
  8080. case MF_SAKURA: // [Valaris]
  8081. map[m].flag.sakura=0;
  8082. break;
  8083. case MF_LEAVES: // [Valaris]
  8084. map[m].flag.leaves=0;
  8085. break;
  8086. case MF_RAIN: // [Valaris]
  8087. map[m].flag.rain=0;
  8088. break;
  8089. case MF_INDOORS: // celest
  8090. map[m].flag.indoors=0;
  8091. break;
  8092. case MF_NIGHTENABLED:
  8093. map[m].flag.nightenabled=0;
  8094. break;
  8095. case MF_NOGO: // celest
  8096. map[m].flag.nogo=0;
  8097. break;
  8098. case MF_NOBASEEXP:
  8099. map[m].flag.nobaseexp=0;
  8100. break;
  8101. case MF_NOJOBEXP:
  8102. map[m].flag.nojobexp=0;
  8103. break;
  8104. case MF_NOMOBLOOT:
  8105. map[m].flag.nomobloot=0;
  8106. break;
  8107. case MF_NOMVPLOOT:
  8108. map[m].flag.nomvploot=0;
  8109. break;
  8110. case MF_NORETURN:
  8111. map[m].flag.noreturn=0;
  8112. break;
  8113. case MF_NOWARPTO:
  8114. map[m].flag.nowarpto=0;
  8115. break;
  8116. case MF_NIGHTMAREDROP:
  8117. map[m].flag.pvp_nightmaredrop=0;
  8118. break;
  8119. case MF_RESTRICTED:
  8120. map[m].flag.restricted=0;
  8121. break;
  8122. case MF_NOCOMMAND:
  8123. map[m].nocommand=0;
  8124. break;
  8125. case MF_JEXP:
  8126. map[m].jexp=100;
  8127. break;
  8128. case MF_BEXP:
  8129. map[m].bexp=100;
  8130. break;
  8131. case MF_NOVENDING:
  8132. map[m].flag.novending=0;
  8133. break;
  8134. case MF_LOADEVENT:
  8135. map[m].flag.loadevent=0;
  8136. break;
  8137. case MF_NOCHAT:
  8138. map[m].flag.nochat=0;
  8139. break;
  8140. case MF_PARTYLOCK:
  8141. map[m].flag.partylock=0;
  8142. break;
  8143. case MF_GUILDLOCK:
  8144. map[m].flag.guildlock=0;
  8145. break;
  8146. }
  8147. }
  8148. return 0;
  8149. }
  8150. BUILDIN_FUNC(pvpon)
  8151. {
  8152. int m,i,users;
  8153. const char *str;
  8154. struct map_session_data *pl_sd=NULL, **pl_allsd;
  8155. str=script_getstr(st,2);
  8156. m = map_mapname2mapid(str);
  8157. if(m >= 0 && !map[m].flag.pvp) {
  8158. map[m].flag.pvp = 1;
  8159. clif_send0199(m,1);
  8160. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8161. return 0;
  8162. pl_allsd = map_getallusers(&users);
  8163. for(i=0;i<users;i++)
  8164. {
  8165. if ((pl_sd = pl_allsd[i]) && m == pl_sd->bl.m && pl_sd->pvp_timer == -1)
  8166. {
  8167. pl_sd->pvp_timer=add_timer(gettick()+200,pc_calc_pvprank_timer,pl_sd->bl.id,0);
  8168. pl_sd->pvp_rank=0;
  8169. pl_sd->pvp_lastusers=0;
  8170. pl_sd->pvp_point=5;
  8171. pl_sd->pvp_won = 0;
  8172. pl_sd->pvp_lost = 0;
  8173. }
  8174. }
  8175. }
  8176. return 0;
  8177. }
  8178. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap) {
  8179. TBL_PC* sd = (TBL_PC*)bl;
  8180. clif_pvpset(sd, 0, 0, 2);
  8181. if (sd->pvp_timer != UINT_MAX) {
  8182. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8183. sd->pvp_timer = UINT_MAX;
  8184. }
  8185. return 0;
  8186. }
  8187. BUILDIN_FUNC(pvpoff)
  8188. {
  8189. int m;
  8190. const char *str;
  8191. str=script_getstr(st,2);
  8192. m = map_mapname2mapid(str);
  8193. if(m < 0 || !map[m].flag.pvp)
  8194. return 0; //fixed Lupus
  8195. map[m].flag.pvp = 0;
  8196. clif_send0199(m,0);
  8197. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8198. return 0;
  8199. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8200. return 0;
  8201. }
  8202. BUILDIN_FUNC(gvgon)
  8203. {
  8204. int m;
  8205. const char *str;
  8206. str=script_getstr(st,2);
  8207. m = map_mapname2mapid(str);
  8208. if(m >= 0 && !map[m].flag.gvg) {
  8209. map[m].flag.gvg = 1;
  8210. clif_send0199(m,3);
  8211. }
  8212. return 0;
  8213. }
  8214. BUILDIN_FUNC(gvgoff)
  8215. {
  8216. int m;
  8217. const char *str;
  8218. str=script_getstr(st,2);
  8219. m = map_mapname2mapid(str);
  8220. if(m >= 0 && map[m].flag.gvg) {
  8221. map[m].flag.gvg = 0;
  8222. clif_send0199(m,0);
  8223. }
  8224. return 0;
  8225. }
  8226. /*==========================================
  8227. * Shows an emoticon on top of the player/npc
  8228. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8229. *------------------------------------------
  8230. */
  8231. //Optional second parameter added by [Skotlex]
  8232. BUILDIN_FUNC(emotion)
  8233. {
  8234. int type;
  8235. int player=0;
  8236. type=script_getnum(st,2);
  8237. if(type < 0 || type > 100)
  8238. return 0;
  8239. if( script_hasdata(st,3) )
  8240. player=script_getnum(st,3);
  8241. if (player) {
  8242. struct map_session_data *sd = script_rid2sd(st);
  8243. if (sd)
  8244. clif_emotion(&sd->bl,type);
  8245. } else
  8246. clif_emotion(map_id2bl(st->oid),type);
  8247. return 0;
  8248. }
  8249. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  8250. {
  8251. int m=va_arg(ap,int);
  8252. int g_id=va_arg(ap,int);
  8253. int flag=va_arg(ap,int);
  8254. struct map_session_data *sd = (TBL_PC*)data;
  8255. if(!sd || sd->bl.m != m)
  8256. return 0;
  8257. if(
  8258. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8259. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8260. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8261. )
  8262. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8263. return 1;
  8264. }
  8265. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8266. {
  8267. struct mob_data *md=(struct mob_data *)bl;
  8268. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8269. status_kill(bl);
  8270. return 0;
  8271. }
  8272. BUILDIN_FUNC(maprespawnguildid)
  8273. {
  8274. const char *mapname=script_getstr(st,2);
  8275. int g_id=script_getnum(st,3);
  8276. int flag=script_getnum(st,4);
  8277. int m=map_mapname2mapid(mapname);
  8278. if(m == -1)
  8279. return 0;
  8280. //Catch ALL players (in case some are 'between maps' on execution time)
  8281. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8282. if (flag&4) //Remove script mobs.
  8283. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8284. return 0;
  8285. }
  8286. BUILDIN_FUNC(agitstart)
  8287. {
  8288. if(agit_flag==1) return 0; // Agit already Start.
  8289. agit_flag=1;
  8290. guild_agit_start();
  8291. return 0;
  8292. }
  8293. BUILDIN_FUNC(agitend)
  8294. {
  8295. if(agit_flag==0) return 0; // Agit already End.
  8296. agit_flag=0;
  8297. guild_agit_end();
  8298. return 0;
  8299. }
  8300. /*==========================================
  8301. * Returns whether woe is on or off. // choice script
  8302. *------------------------------------------
  8303. */
  8304. BUILDIN_FUNC(agitcheck)
  8305. {
  8306. script_pushint(st,agit_flag);
  8307. return 0;
  8308. }
  8309. BUILDIN_FUNC(flagemblem)
  8310. {
  8311. int g_id=script_getnum(st,2);
  8312. if(g_id < 0) return 0;
  8313. // printf("Script.c: [FlagEmblem] GuildID=%d, Emblem=%d.\n", g->guild_id, g->emblem_id);
  8314. ((struct npc_data *)map_id2bl(st->oid))->u.scr.guild_id = g_id;
  8315. return 0;
  8316. }
  8317. BUILDIN_FUNC(getcastlename)
  8318. {
  8319. char mapname[MAP_NAME_LENGTH_EXT];
  8320. struct guild_castle *gc;
  8321. strncpy(mapname, script_getstr(st,2), MAP_NAME_LENGTH_EXT);
  8322. gc = guild_mapname2gc(mapname);
  8323. if(gc)
  8324. script_pushconststr(st,gc->castle_name);
  8325. else
  8326. script_pushconststr(st,"");
  8327. return 0;
  8328. }
  8329. BUILDIN_FUNC(getcastledata)
  8330. {
  8331. char mapname[MAP_NAME_LENGTH_EXT];
  8332. int index=script_getnum(st,3);
  8333. const char *event=NULL;
  8334. struct guild_castle *gc;
  8335. int i;
  8336. strncpy(mapname, script_getstr(st,2), MAP_NAME_LENGTH_EXT);
  8337. gc = guild_mapname2gc(mapname);
  8338. if(script_hasdata(st,4) && index==0 && gc) {
  8339. event=script_getstr(st,4);
  8340. check_event(st, event);
  8341. guild_addcastleinfoevent(gc->castle_id,17,event);
  8342. }
  8343. if(gc){
  8344. switch(index){
  8345. case 0:
  8346. for(i=1;i<26;i++) // Initialize[AgitInit]
  8347. guild_castledataload(gc->castle_id,i);
  8348. break;
  8349. case 1:
  8350. script_pushint(st,gc->guild_id); break;
  8351. case 2:
  8352. script_pushint(st,gc->economy); break;
  8353. case 3:
  8354. script_pushint(st,gc->defense); break;
  8355. case 4:
  8356. script_pushint(st,gc->triggerE); break;
  8357. case 5:
  8358. script_pushint(st,gc->triggerD); break;
  8359. case 6:
  8360. script_pushint(st,gc->nextTime); break;
  8361. case 7:
  8362. script_pushint(st,gc->payTime); break;
  8363. case 8:
  8364. script_pushint(st,gc->createTime); break;
  8365. case 9:
  8366. script_pushint(st,gc->visibleC); break;
  8367. case 10:
  8368. case 11:
  8369. case 12:
  8370. case 13:
  8371. case 14:
  8372. case 15:
  8373. case 16:
  8374. case 17:
  8375. script_pushint(st,gc->guardian[index-10].visible); break;
  8376. case 18:
  8377. case 19:
  8378. case 20:
  8379. case 21:
  8380. case 22:
  8381. case 23:
  8382. case 24:
  8383. case 25:
  8384. script_pushint(st,gc->guardian[index-18].hp); break;
  8385. default:
  8386. script_pushint(st,0); break;
  8387. }
  8388. return 0;
  8389. }
  8390. script_pushint(st,0);
  8391. return 0;
  8392. }
  8393. BUILDIN_FUNC(setcastledata)
  8394. {
  8395. char mapname[MAP_NAME_LENGTH_EXT];
  8396. int index=script_getnum(st,3);
  8397. int value=script_getnum(st,4);
  8398. struct guild_castle *gc;
  8399. strncpy(mapname, script_getstr(st,2), MAP_NAME_LENGTH_EXT);
  8400. gc = guild_mapname2gc(mapname);
  8401. if(gc) {
  8402. // Save Data byself First
  8403. switch(index){
  8404. case 1:
  8405. gc->guild_id = value; break;
  8406. case 2:
  8407. gc->economy = value; break;
  8408. case 3:
  8409. gc->defense = value; break;
  8410. case 4:
  8411. gc->triggerE = value; break;
  8412. case 5:
  8413. gc->triggerD = value; break;
  8414. case 6:
  8415. gc->nextTime = value; break;
  8416. case 7:
  8417. gc->payTime = value; break;
  8418. case 8:
  8419. gc->createTime = value; break;
  8420. case 9:
  8421. gc->visibleC = value; break;
  8422. case 10:
  8423. case 11:
  8424. case 12:
  8425. case 13:
  8426. case 14:
  8427. case 15:
  8428. case 16:
  8429. case 17:
  8430. gc->guardian[index-10].visible = value; break;
  8431. case 18:
  8432. case 19:
  8433. case 20:
  8434. case 21:
  8435. case 22:
  8436. case 23:
  8437. case 24:
  8438. case 25:
  8439. gc->guardian[index-18].hp = value;
  8440. if (gc->guardian[index-18].id)
  8441. { //Update this mob's HP.
  8442. struct block_list *bl = map_id2bl(gc->guardian[index-18].id);
  8443. if (!bl)
  8444. { //Wrong target?
  8445. gc->guardian[index-18].id = 0;
  8446. break;
  8447. }
  8448. if (value < 1) {
  8449. status_kill(bl);
  8450. break;
  8451. }
  8452. status_set_hp(bl, value, 0);
  8453. }
  8454. break;
  8455. default:
  8456. return 0;
  8457. }
  8458. guild_castledatasave(gc->castle_id,index,value);
  8459. }
  8460. return 0;
  8461. }
  8462. /* =====================================================================
  8463. * ギルド情報を要求する
  8464. * ---------------------------------------------------------------------
  8465. */
  8466. BUILDIN_FUNC(requestguildinfo)
  8467. {
  8468. int guild_id=script_getnum(st,2);
  8469. const char *event=NULL;
  8470. if( script_hasdata(st,3) ){
  8471. event=script_getstr(st,3);
  8472. check_event(st, event);
  8473. }
  8474. if(guild_id>0)
  8475. guild_npc_request_info(guild_id,event);
  8476. return 0;
  8477. }
  8478. /* =====================================================================
  8479. * カードの数を得る
  8480. * ---------------------------------------------------------------------
  8481. */
  8482. BUILDIN_FUNC(getequipcardcnt)
  8483. {
  8484. int i,num;
  8485. struct map_session_data *sd;
  8486. int c=MAX_SLOTS;
  8487. num=script_getnum(st,2);
  8488. sd=script_rid2sd(st);
  8489. i=pc_checkequip(sd,equip[num-1]);
  8490. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8491. {
  8492. script_pushint(st,0);
  8493. return 0;
  8494. }
  8495. do{
  8496. if(sd->status.inventory[i].card[c-1] &&
  8497. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8498. script_pushint(st,(c));
  8499. return 0;
  8500. }
  8501. }while(c--);
  8502. script_pushint(st,0);
  8503. return 0;
  8504. }
  8505. /* ================================================================
  8506. * カード取り外し成功
  8507. * ----------------------------------------------------------------
  8508. */
  8509. BUILDIN_FUNC(successremovecards)
  8510. {
  8511. int i,j,num,cardflag=0,flag;
  8512. struct map_session_data *sd;
  8513. struct item item_tmp;
  8514. int c=MAX_SLOTS;
  8515. num=script_getnum(st,2);
  8516. sd=script_rid2sd(st);
  8517. i=pc_checkequip(sd,equip[num-1]);
  8518. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8519. return 0;
  8520. do{
  8521. if(sd->status.inventory[i].card[c-1] &&
  8522. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8523. cardflag = 1;
  8524. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8525. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8526. item_tmp.attribute=0;
  8527. for (j = 0; j < MAX_SLOTS; j++)
  8528. item_tmp.card[j]=0;
  8529. //Logs items, got from (N)PC scripts [Lupus]
  8530. if(log_config.enable_logs&0x40)
  8531. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8532. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8533. clif_additem(sd,0,0,flag);
  8534. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8535. }
  8536. }
  8537. }while(c--);
  8538. if(cardflag == 1){ // カードを取り除いたアイテム所得
  8539. flag=0;
  8540. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8541. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8542. item_tmp.attribute=sd->status.inventory[i].attribute;
  8543. //Logs items, got from (N)PC scripts [Lupus]
  8544. if(log_config.enable_logs&0x40)
  8545. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8546. for (j = 0; j < MAX_SLOTS; j++)
  8547. item_tmp.card[j]=0;
  8548. pc_delitem(sd,i,1,0);
  8549. //Logs items, got from (N)PC scripts [Lupus]
  8550. if(log_config.enable_logs&0x40)
  8551. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8552. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8553. clif_additem(sd,0,0,flag);
  8554. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8555. }
  8556. clif_misceffect(&sd->bl,3);
  8557. return 0;
  8558. }
  8559. return 0;
  8560. }
  8561. /* ================================================================
  8562. * カード取り外し失敗 slot,type
  8563. * type=0: 両方損失、1:カード損失、2:武具損失、3:損失無し
  8564. * ----------------------------------------------------------------
  8565. */
  8566. BUILDIN_FUNC(failedremovecards)
  8567. {
  8568. int i,j,num,cardflag=0,flag,typefail;
  8569. struct map_session_data *sd;
  8570. struct item item_tmp;
  8571. int c=MAX_SLOTS;
  8572. num=script_getnum(st,2);
  8573. typefail=script_getnum(st,3);
  8574. sd=script_rid2sd(st);
  8575. i=pc_checkequip(sd,equip[num-1]);
  8576. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8577. return 0;
  8578. do{
  8579. if(sd->status.inventory[i].card[c-1] &&
  8580. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8581. cardflag = 1;
  8582. if(typefail == 2){ // 武具のみ損失なら、カードは受け取らせる
  8583. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8584. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8585. item_tmp.attribute=0;
  8586. for (j = 0; j < MAX_SLOTS; j++)
  8587. item_tmp.card[j]=0;
  8588. //Logs items, got from (N)PC scripts [Lupus]
  8589. if(log_config.enable_logs&0x40)
  8590. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8591. if((flag=pc_additem(sd,&item_tmp,1))){
  8592. clif_additem(sd,0,0,flag);
  8593. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8594. }
  8595. }
  8596. }
  8597. }while(c--);
  8598. if(cardflag == 1){
  8599. if(typefail == 0 || typefail == 2){ // 武具損失
  8600. //Logs items, got from (N)PC scripts [Lupus]
  8601. if(log_config.enable_logs&0x40)
  8602. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8603. pc_delitem(sd,i,1,0);
  8604. clif_misceffect(&sd->bl,2);
  8605. return 0;
  8606. }
  8607. if(typefail == 1){ // カードのみ損失(武具を返す)
  8608. flag=0;
  8609. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8610. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8611. item_tmp.attribute=sd->status.inventory[i].attribute;
  8612. //Logs items, got from (N)PC scripts [Lupus]
  8613. if(log_config.enable_logs&0x40)
  8614. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8615. for (j = 0; j < MAX_SLOTS; j++)
  8616. item_tmp.card[j]=0;
  8617. pc_delitem(sd,i,1,0);
  8618. //Logs items, got from (N)PC scripts [Lupus]
  8619. if(log_config.enable_logs&0x40)
  8620. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8621. if((flag=pc_additem(sd,&item_tmp,1))){
  8622. clif_additem(sd,0,0,flag);
  8623. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8624. }
  8625. }
  8626. clif_misceffect(&sd->bl,2);
  8627. return 0;
  8628. }
  8629. return 0;
  8630. }
  8631. /* ================================================================
  8632. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8633. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8634. * improved by [Lance]
  8635. * ================================================================
  8636. */
  8637. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8638. {
  8639. int x,y,m,check_val=0,check_ID=0,i=0;
  8640. struct guild *g = NULL;
  8641. struct party_data *p = NULL;
  8642. const char *str;
  8643. const char *mapname;
  8644. unsigned int index;
  8645. mapname=script_getstr(st,2);
  8646. str=script_getstr(st,3);
  8647. x=script_getnum(st,4);
  8648. y=script_getnum(st,5);
  8649. if(script_hasdata(st,7)){
  8650. check_val=script_getnum(st,6);
  8651. check_ID=script_getnum(st,7);
  8652. }
  8653. if((m=map_mapname2mapid(mapname))< 0)
  8654. return 0;
  8655. if(!(index=mapindex_name2id(str)))
  8656. return 0;
  8657. switch(check_val){
  8658. case 1:
  8659. g = guild_search(check_ID);
  8660. if (g){
  8661. for( i=0; i < g->max_member; i++)
  8662. {
  8663. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8664. pc_setpos(g->member[i].sd,index,x,y,3);
  8665. }
  8666. }
  8667. }
  8668. break;
  8669. case 2:
  8670. p = party_search(check_ID);
  8671. if(p){
  8672. for(i=0;i<MAX_PARTY; i++){
  8673. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8674. pc_setpos(p->data[i].sd,index,x,y,3);
  8675. }
  8676. }
  8677. }
  8678. break;
  8679. default:
  8680. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8681. break;
  8682. }
  8683. return 0;
  8684. }
  8685. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8686. {
  8687. const char *npc,*command;
  8688. npc=script_getstr(st,2);
  8689. command=script_getstr(st,3);
  8690. npc_command(map_id2sd(st->rid),npc,command);
  8691. return 0;
  8692. }
  8693. BUILDIN_FUNC(inittimer) // Added by RoVeRT
  8694. {
  8695. // struct npc_data *nd=(struct npc_data*)map_id2bl(st->oid);
  8696. // nd->lastaction=nd->timer=gettick();
  8697. npc_do_ontimer(st->oid, 1);
  8698. return 0;
  8699. }
  8700. BUILDIN_FUNC(stoptimer) // Added by RoVeRT
  8701. {
  8702. // struct npc_data *nd=(struct npc_data*)map_id2bl(st->oid);
  8703. // nd->lastaction=nd->timer=-1;
  8704. npc_do_ontimer(st->oid, 0);
  8705. return 0;
  8706. }
  8707. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8708. {
  8709. char *event=va_arg(ap,char *);
  8710. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8711. return 1;
  8712. return 0;
  8713. }
  8714. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8715. {
  8716. const char *mapname,*event;
  8717. int m;
  8718. mapname=script_getstr(st,2);
  8719. event=script_getstr(st,3);
  8720. check_event(st, event);
  8721. if( (m=map_mapname2mapid(mapname))<0 ) {
  8722. script_pushint(st,-1);
  8723. return 0;
  8724. }
  8725. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8726. return 0;
  8727. }
  8728. BUILDIN_FUNC(marriage)
  8729. {
  8730. const char *partner=script_getstr(st,2);
  8731. struct map_session_data *sd=script_rid2sd(st);
  8732. struct map_session_data *p_sd=map_nick2sd(partner);
  8733. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8734. script_pushint(st,0);
  8735. return 0;
  8736. }
  8737. script_pushint(st,1);
  8738. return 0;
  8739. }
  8740. BUILDIN_FUNC(wedding_effect)
  8741. {
  8742. struct map_session_data *sd=script_rid2sd(st);
  8743. struct block_list *bl;
  8744. if(sd==NULL) {
  8745. bl=map_id2bl(st->oid);
  8746. } else
  8747. bl=&sd->bl;
  8748. clif_wedding_effect(bl);
  8749. return 0;
  8750. }
  8751. BUILDIN_FUNC(divorce)
  8752. {
  8753. struct map_session_data *sd=script_rid2sd(st);
  8754. if(sd==NULL || pc_divorce(sd) < 0){
  8755. script_pushint(st,0);
  8756. return 0;
  8757. }
  8758. script_pushint(st,1);
  8759. return 0;
  8760. }
  8761. BUILDIN_FUNC(ispartneron)
  8762. {
  8763. struct map_session_data *sd=script_rid2sd(st);
  8764. struct map_session_data *p_sd=NULL;
  8765. if(sd==NULL || !pc_ismarried(sd) ||
  8766. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8767. script_pushint(st,0);
  8768. return 0;
  8769. }
  8770. script_pushint(st,1);
  8771. return 0;
  8772. }
  8773. BUILDIN_FUNC(getpartnerid)
  8774. {
  8775. struct map_session_data *sd=script_rid2sd(st);
  8776. if (sd == NULL) {
  8777. script_pushint(st,0);
  8778. return 0;
  8779. }
  8780. script_pushint(st,sd->status.partner_id);
  8781. return 0;
  8782. }
  8783. BUILDIN_FUNC(getchildid)
  8784. {
  8785. struct map_session_data *sd=script_rid2sd(st);
  8786. if (sd == NULL) {
  8787. script_pushint(st,0);
  8788. return 0;
  8789. }
  8790. script_pushint(st,sd->status.child);
  8791. return 0;
  8792. }
  8793. BUILDIN_FUNC(getmotherid)
  8794. {
  8795. struct map_session_data *sd=script_rid2sd(st);
  8796. if (sd == NULL) {
  8797. script_pushint(st,0);
  8798. return 0;
  8799. }
  8800. script_pushint(st,sd->status.mother);
  8801. return 0;
  8802. }
  8803. BUILDIN_FUNC(getfatherid)
  8804. {
  8805. struct map_session_data *sd=script_rid2sd(st);
  8806. if (sd == NULL) {
  8807. script_pushint(st,0);
  8808. return 0;
  8809. }
  8810. script_pushint(st,sd->status.father);
  8811. return 0;
  8812. }
  8813. BUILDIN_FUNC(warppartner)
  8814. {
  8815. int x,y;
  8816. unsigned short mapindex;
  8817. const char *str;
  8818. struct map_session_data *sd=script_rid2sd(st);
  8819. struct map_session_data *p_sd=NULL;
  8820. if(sd==NULL || !pc_ismarried(sd) ||
  8821. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8822. script_pushint(st,0);
  8823. return 0;
  8824. }
  8825. str=script_getstr(st,2);
  8826. x=script_getnum(st,3);
  8827. y=script_getnum(st,4);
  8828. mapindex = mapindex_name2id(str);
  8829. if (mapindex) {
  8830. pc_setpos(p_sd,mapindex,x,y,0);
  8831. script_pushint(st,1);
  8832. } else
  8833. script_pushint(st,0);
  8834. return 0;
  8835. }
  8836. /*================================================
  8837. * Script for Displaying MOB Information [Valaris]
  8838. *------------------------------------------------
  8839. */
  8840. BUILDIN_FUNC(strmobinfo)
  8841. {
  8842. int num=script_getnum(st,2);
  8843. int class_=script_getnum(st,3);
  8844. if(!mobdb_checkid(class_))
  8845. {
  8846. script_pushint(st,0);
  8847. return 0;
  8848. }
  8849. switch (num) {
  8850. case 1:
  8851. script_pushconststr(st,mob_db(class_)->name);
  8852. break;
  8853. case 2:
  8854. script_pushconststr(st,mob_db(class_)->jname);
  8855. break;
  8856. case 3:
  8857. script_pushint(st,mob_db(class_)->lv);
  8858. break;
  8859. case 4:
  8860. script_pushint(st,mob_db(class_)->status.max_hp);
  8861. break;
  8862. case 5:
  8863. script_pushint(st,mob_db(class_)->status.max_sp);
  8864. break;
  8865. case 6:
  8866. script_pushint(st,mob_db(class_)->base_exp);
  8867. break;
  8868. case 7:
  8869. script_pushint(st,mob_db(class_)->job_exp);
  8870. break;
  8871. default:
  8872. script_pushint(st,0);
  8873. break;
  8874. }
  8875. return 0;
  8876. }
  8877. /*==========================================
  8878. * Summon guardians [Valaris]
  8879. * guardian "<map name>",<x>,<y>,"<name to show>",<mob id>,{,"<event label>"}{,<guardian index>};
  8880. *------------------------------------------
  8881. */
  8882. BUILDIN_FUNC(guardian)
  8883. {
  8884. int class_=0,x=0,y=0,guardian=0;
  8885. const char *str,*map,*evt="";
  8886. struct script_data *data;
  8887. map =script_getstr(st,2);
  8888. x =script_getnum(st,3);
  8889. y =script_getnum(st,4);
  8890. str =script_getstr(st,5);
  8891. class_=script_getnum(st,6);
  8892. if( script_hasdata(st,8) )
  8893. {// "<event label>",<guardian index>
  8894. evt=script_getstr(st,7);
  8895. guardian=script_getnum(st,8);
  8896. } else if( script_hasdata(st,7) ){
  8897. data=script_getdata(st,7);
  8898. get_val(st,data);
  8899. if( data_isstring(data) )
  8900. {// "<event label>"
  8901. evt=script_getstr(st,7);
  8902. } else if( data_isint(data) )
  8903. {// <guardian index>
  8904. guardian=script_getnum(st,7);
  8905. } else {
  8906. ShowError("buildin_guardian: invalid data type for argument #8 (%d).", data->type);
  8907. report_src(st);
  8908. return 1;
  8909. }
  8910. }
  8911. check_event(st, evt);
  8912. mob_spawn_guardian(map,x,y,str,class_,evt,guardian);
  8913. return 0;
  8914. }
  8915. /*================================================
  8916. * Script for Displaying Guardian Info [Valaris]
  8917. *------------------------------------------------
  8918. */
  8919. BUILDIN_FUNC(guardianinfo)
  8920. {
  8921. int guardian=script_getnum(st,2);
  8922. struct map_session_data *sd=script_rid2sd(st);
  8923. struct guild_castle *gc=guild_mapname2gc(map[sd->bl.m].name);
  8924. if (guardian < 0 || guardian >= MAX_GUARDIANS || gc==NULL)
  8925. {
  8926. script_pushint(st,-1);
  8927. return 0;
  8928. }
  8929. if(gc->guardian[guardian].visible)
  8930. script_pushint(st,gc->guardian[guardian].hp);
  8931. else script_pushint(st,-1);
  8932. return 0;
  8933. }
  8934. /*==========================================
  8935. * IDからItem名
  8936. *------------------------------------------
  8937. */
  8938. BUILDIN_FUNC(getitemname)
  8939. {
  8940. int item_id=0;
  8941. struct item_data *i_data;
  8942. char *item_name;
  8943. struct script_data *data;
  8944. data=script_getdata(st,2);
  8945. get_val(st,data);
  8946. if( data_isstring(data) ){
  8947. const char *name=conv_str(st,data);
  8948. struct item_data *item_data = itemdb_searchname(name);
  8949. if( item_data )
  8950. item_id=item_data->nameid;
  8951. }else
  8952. item_id=conv_num(st,data);
  8953. i_data = itemdb_exists(item_id);
  8954. if (i_data == NULL)
  8955. {
  8956. script_pushconststr(st,"null");
  8957. return 0;
  8958. }
  8959. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8960. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8961. script_pushstr(st,item_name);
  8962. return 0;
  8963. }
  8964. /*==========================================
  8965. * Returns number of slots an item has. [Skotlex]
  8966. *------------------------------------------
  8967. */
  8968. BUILDIN_FUNC(getitemslots)
  8969. {
  8970. int item_id;
  8971. struct item_data *i_data;
  8972. item_id=script_getnum(st,2);
  8973. i_data = itemdb_exists(item_id);
  8974. if (i_data)
  8975. script_pushint(st,i_data->slot);
  8976. else
  8977. script_pushint(st,-1);
  8978. return 0;
  8979. }
  8980. /*==========================================
  8981. * Returns some values of an item [Lupus]
  8982. * Price, Weight, etc...
  8983. getiteminfo(itemID,n), where n
  8984. 0 value_buy;
  8985. 1 value_sell;
  8986. 2 type;
  8987. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8988. if = 0, then monsters don't drop it at all (rare or a quest item)
  8989. if = 10000, then this item is sold in NPC shops only
  8990. 4 sex;
  8991. 5 equip;
  8992. 6 weight;
  8993. 7 atk;
  8994. 8 def;
  8995. 9 range;
  8996. 10 slot;
  8997. 11 look;
  8998. 12 elv;
  8999. 13 wlv;
  9000. 14 view id
  9001. *------------------------------------------
  9002. */
  9003. BUILDIN_FUNC(getiteminfo)
  9004. {
  9005. int item_id,n;
  9006. int *item_arr;
  9007. struct item_data *i_data;
  9008. item_id = script_getnum(st,2);
  9009. n = script_getnum(st,3);
  9010. i_data = itemdb_exists(item_id);
  9011. if (i_data && n>=0 && n<14) {
  9012. item_arr = (int*)&i_data->value_buy;
  9013. script_pushint(st,item_arr[n]);
  9014. } else
  9015. script_pushint(st,-1);
  9016. return 0;
  9017. }
  9018. /*==========================================
  9019. * Set some values of an item [Lupus]
  9020. * Price, Weight, etc...
  9021. setiteminfo(itemID,n,Value), where n
  9022. 0 value_buy;
  9023. 1 value_sell;
  9024. 2 type;
  9025. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9026. if = 0, then monsters don't drop it at all (rare or a quest item)
  9027. if = 10000, then this item is sold in NPC shops only
  9028. 4 sex;
  9029. 5 equip;
  9030. 6 weight;
  9031. 7 atk;
  9032. 8 def;
  9033. 9 range;
  9034. 10 slot;
  9035. 11 look;
  9036. 12 elv;
  9037. 13 wlv;
  9038. 14 view id
  9039. * Returns Value or -1 if the wrong field's been set
  9040. *------------------------------------------
  9041. */
  9042. BUILDIN_FUNC(setiteminfo)
  9043. {
  9044. int item_id,n,value;
  9045. int *item_arr;
  9046. struct item_data *i_data;
  9047. item_id = script_getnum(st,2);
  9048. n = script_getnum(st,3);
  9049. value = script_getnum(st,4);
  9050. i_data = itemdb_exists(item_id);
  9051. if (i_data && n>=0 && n<14) {
  9052. item_arr = (int*)&i_data->value_buy;
  9053. item_arr[n] = value;
  9054. script_pushint(st,value);
  9055. } else
  9056. script_pushint(st,-1);
  9057. return 0;
  9058. }
  9059. /*==========================================
  9060. * Returns value from equipped item slot n [Lupus]
  9061. getequipcardid(num,slot)
  9062. where
  9063. num = eqip position slot
  9064. slot = 0,1,2,3 (Card Slot N)
  9065. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9066. it's useful when you want to check item cards or if it's signed
  9067. Useful for such quests as "Sign this refined item with players name" etc
  9068. Hat[0] +4 -> Player's Hat[0] +4
  9069. *------------------------------------------
  9070. */
  9071. BUILDIN_FUNC(getequipcardid)
  9072. {
  9073. int i,num,slot;
  9074. struct map_session_data *sd;
  9075. num=script_getnum(st,2);
  9076. slot=script_getnum(st,3);
  9077. sd=script_rid2sd(st);
  9078. i=pc_checkequip(sd,equip[num-1]);
  9079. if(i >= 0 && slot>=0 && slot<4)
  9080. script_pushint(st,sd->status.inventory[i].card[slot]);
  9081. else
  9082. script_pushint(st,0);
  9083. return 0;
  9084. }
  9085. /*==========================================
  9086. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9087. *------------------------------------------
  9088. */
  9089. BUILDIN_FUNC(petskillbonus)
  9090. {
  9091. struct pet_data *pd;
  9092. struct map_session_data *sd=script_rid2sd(st);
  9093. if(sd==NULL || sd->pd==NULL)
  9094. return 0;
  9095. pd=sd->pd;
  9096. if (pd->bonus)
  9097. { //Clear previous bonus
  9098. if (pd->bonus->timer != -1)
  9099. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9100. } else //init
  9101. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9102. pd->bonus->type=script_getnum(st,2);
  9103. pd->bonus->val=script_getnum(st,3);
  9104. pd->bonus->duration=script_getnum(st,4);
  9105. pd->bonus->delay=script_getnum(st,5);
  9106. if (pd->state.skillbonus == 1)
  9107. pd->state.skillbonus=0; // waiting state
  9108. // wait for timer to start
  9109. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9110. pd->bonus->timer=-1;
  9111. else
  9112. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9113. return 0;
  9114. }
  9115. /*==========================================
  9116. * pet looting [Valaris] //Rewritten by [Skotlex]
  9117. *------------------------------------------
  9118. */
  9119. BUILDIN_FUNC(petloot)
  9120. {
  9121. int max;
  9122. struct pet_data *pd;
  9123. struct map_session_data *sd=script_rid2sd(st);
  9124. if(sd==NULL || sd->pd==NULL)
  9125. return 0;
  9126. max=script_getnum(st,2);
  9127. if(max < 1)
  9128. max = 1; //Let'em loot at least 1 item.
  9129. else if (max > MAX_PETLOOT_SIZE)
  9130. max = MAX_PETLOOT_SIZE;
  9131. pd = sd->pd;
  9132. if (pd->loot != NULL)
  9133. { //Release whatever was there already and reallocate memory
  9134. pet_lootitem_drop(pd, pd->msd);
  9135. aFree(pd->loot->item);
  9136. }
  9137. else
  9138. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9139. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9140. pd->loot->max=max;
  9141. pd->loot->count = 0;
  9142. pd->loot->weight = 0;
  9143. return 0;
  9144. }
  9145. /*==========================================
  9146. * PCの所持品情報読み取り
  9147. *------------------------------------------
  9148. */
  9149. BUILDIN_FUNC(getinventorylist)
  9150. {
  9151. struct map_session_data *sd=script_rid2sd(st);
  9152. unsigned char card_var[NAME_LENGTH];
  9153. int i,j=0,k;
  9154. if(!sd) return 0;
  9155. for(i=0;i<MAX_INVENTORY;i++){
  9156. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9157. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9158. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9159. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9160. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9161. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9162. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9163. for (k = 0; k < MAX_SLOTS; k++)
  9164. {
  9165. sprintf(card_var, "@inventorylist_card%d",k+1);
  9166. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9167. }
  9168. j++;
  9169. }
  9170. }
  9171. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9172. return 0;
  9173. }
  9174. BUILDIN_FUNC(getskilllist)
  9175. {
  9176. struct map_session_data *sd=script_rid2sd(st);
  9177. int i,j=0;
  9178. if(!sd) return 0;
  9179. for(i=0;i<MAX_SKILL;i++){
  9180. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9181. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9182. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9183. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9184. j++;
  9185. }
  9186. }
  9187. pc_setreg(sd,add_str("@skilllist_count"),j);
  9188. return 0;
  9189. }
  9190. BUILDIN_FUNC(clearitem)
  9191. {
  9192. struct map_session_data *sd=script_rid2sd(st);
  9193. int i;
  9194. if(sd==NULL) return 0;
  9195. for (i=0; i<MAX_INVENTORY; i++) {
  9196. if (sd->status.inventory[i].amount) {
  9197. //Logs items, got from (N)PC scripts [Lupus]
  9198. if(log_config.enable_logs&0x40)
  9199. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9200. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9201. }
  9202. }
  9203. return 0;
  9204. }
  9205. /*==========================================
  9206. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9207. *------------------------------------------*/
  9208. BUILDIN_FUNC(disguise)
  9209. {
  9210. int id;
  9211. struct map_session_data* sd = script_rid2sd(st);
  9212. if (sd == NULL) return 0;
  9213. id = script_getnum(st,2);
  9214. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9215. pc_disguise(sd, id);
  9216. script_pushint(st,id);
  9217. } else
  9218. script_pushint(st,0);
  9219. return 0;
  9220. }
  9221. /*==========================================
  9222. * Undisguise Player (returns 1 if success, 0 on fail)
  9223. *------------------------------------------*/
  9224. BUILDIN_FUNC(undisguise)
  9225. {
  9226. struct map_session_data* sd = script_rid2sd(st);
  9227. if (sd == NULL) return 0;
  9228. if (sd->disguise) {
  9229. pc_disguise(sd, 0);
  9230. script_pushint(st,0);
  9231. } else {
  9232. script_pushint(st,1);
  9233. }
  9234. return 0;
  9235. }
  9236. /*==========================================
  9237. * NPCクラスチェンジ
  9238. * classは変わりたいclass
  9239. * typeは通常0なのかな?
  9240. *------------------------------------------
  9241. */
  9242. BUILDIN_FUNC(classchange)
  9243. {
  9244. int _class,type;
  9245. struct block_list *bl=map_id2bl(st->oid);
  9246. if(bl==NULL) return 0;
  9247. _class=script_getnum(st,2);
  9248. type=script_getnum(st,3);
  9249. clif_class_change(bl,_class,type);
  9250. return 0;
  9251. }
  9252. /*==========================================
  9253. * NPCから発生するエフェクト
  9254. *------------------------------------------
  9255. */
  9256. BUILDIN_FUNC(misceffect)
  9257. {
  9258. int type;
  9259. type=script_getnum(st,2);
  9260. if(st->oid && st->oid != fake_nd->bl.id) {
  9261. struct block_list *bl = map_id2bl(st->oid);
  9262. if (bl)
  9263. clif_misceffect2(bl,type);
  9264. } else{
  9265. struct map_session_data *sd=script_rid2sd(st);
  9266. if(sd)
  9267. clif_misceffect2(&sd->bl,type);
  9268. }
  9269. return 0;
  9270. }
  9271. /*==========================================
  9272. * サウンドエフェクト
  9273. *------------------------------------------
  9274. */
  9275. BUILDIN_FUNC(soundeffect)
  9276. {
  9277. // Redundn
  9278. struct map_session_data *sd=script_rid2sd(st);
  9279. const char *name;
  9280. int type=0;
  9281. name=script_getstr(st,2);
  9282. type=script_getnum(st,3);
  9283. if(sd){
  9284. if(!st->rid)
  9285. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9286. else{
  9287. clif_soundeffect(sd,&sd->bl,name,type);
  9288. }
  9289. }
  9290. return 0;
  9291. }
  9292. int soundeffect_sub(struct block_list* bl,va_list ap)
  9293. {
  9294. char *name;
  9295. int type;
  9296. nullpo_retr(0, bl);
  9297. nullpo_retr(0, ap);
  9298. name = va_arg(ap,char *);
  9299. type = va_arg(ap,int);
  9300. clif_soundeffect((struct map_session_data *)bl, bl, name, type);
  9301. return 0;
  9302. }
  9303. BUILDIN_FUNC(soundeffectall)
  9304. {
  9305. // [Lance] - Improved.
  9306. const char *name, *map = NULL;
  9307. struct block_list *bl;
  9308. int type, coverage, x0, y0, x1, y1;
  9309. name=script_getstr(st,2);
  9310. type=script_getnum(st,3);
  9311. coverage=script_getnum(st,4);
  9312. if(!st->rid)
  9313. bl = map_id2bl(st->oid);
  9314. else
  9315. bl = &(script_rid2sd(st)->bl);
  9316. if(bl){
  9317. if(coverage < 23){
  9318. clif_soundeffectall(bl,name,type,coverage);
  9319. }else {
  9320. if(script_hasdata(st,9)){
  9321. map= script_getstr(st,5);
  9322. x0 = script_getnum(st,6);
  9323. y0 = script_getnum(st,7);
  9324. x1 = script_getnum(st,8);
  9325. y1 = script_getnum(st,9);
  9326. map_foreachinarea(soundeffect_sub,map_mapname2mapid(map),x0,y0,x1,y1,BL_PC,name,type);
  9327. } else {
  9328. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9329. }
  9330. }
  9331. }
  9332. return 0;
  9333. }
  9334. /*==========================================
  9335. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9336. *------------------------------------------
  9337. */
  9338. BUILDIN_FUNC(petrecovery)
  9339. {
  9340. struct pet_data *pd;
  9341. struct map_session_data *sd=script_rid2sd(st);
  9342. if(sd==NULL || sd->pd==NULL)
  9343. return 0;
  9344. pd=sd->pd;
  9345. if (pd->recovery)
  9346. { //Halt previous bonus
  9347. if (pd->recovery->timer != -1)
  9348. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9349. } else //Init
  9350. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9351. pd->recovery->type=script_getnum(st,2);
  9352. pd->recovery->delay=script_getnum(st,3);
  9353. pd->recovery->timer=-1;
  9354. return 0;
  9355. }
  9356. /*==========================================
  9357. * pet healing [Valaris] //Rewritten by [Skotlex]
  9358. *------------------------------------------
  9359. */
  9360. BUILDIN_FUNC(petheal)
  9361. {
  9362. struct pet_data *pd;
  9363. struct map_session_data *sd=script_rid2sd(st);
  9364. if(sd==NULL || sd->pd==NULL)
  9365. return 0;
  9366. pd=sd->pd;
  9367. if (pd->s_skill)
  9368. { //Clear previous skill
  9369. if (pd->s_skill->timer != -1)
  9370. {
  9371. if (pd->s_skill->id)
  9372. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9373. else
  9374. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9375. }
  9376. } else //init memory
  9377. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9378. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9379. //Use the lv as the amount to heal
  9380. pd->s_skill->lv=script_getnum(st,2);
  9381. pd->s_skill->delay=script_getnum(st,3);
  9382. pd->s_skill->hp=script_getnum(st,4);
  9383. pd->s_skill->sp=script_getnum(st,5);
  9384. //Use delay as initial offset to avoid skill/heal exploits
  9385. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9386. pd->s_skill->timer=-1;
  9387. else
  9388. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9389. return 0;
  9390. }
  9391. /*==========================================
  9392. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9393. *------------------------------------------
  9394. */
  9395. BUILDIN_FUNC(petskillattack)
  9396. {
  9397. struct pet_data *pd;
  9398. struct map_session_data *sd=script_rid2sd(st);
  9399. if(sd==NULL || sd->pd==NULL)
  9400. return 0;
  9401. pd=sd->pd;
  9402. if (pd->a_skill == NULL)
  9403. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9404. pd->a_skill->id=script_getnum(st,2);
  9405. pd->a_skill->lv=script_getnum(st,3);
  9406. pd->a_skill->div_ = 0;
  9407. pd->a_skill->rate=script_getnum(st,4);
  9408. pd->a_skill->bonusrate=script_getnum(st,5);
  9409. return 0;
  9410. }
  9411. /*==========================================
  9412. * pet attack skills [Valaris]
  9413. *------------------------------------------
  9414. */
  9415. BUILDIN_FUNC(petskillattack2)
  9416. {
  9417. struct pet_data *pd;
  9418. struct map_session_data *sd=script_rid2sd(st);
  9419. if(sd==NULL || sd->pd==NULL)
  9420. return 0;
  9421. pd=sd->pd;
  9422. if (pd->a_skill == NULL)
  9423. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9424. pd->a_skill->id=script_getnum(st,2);
  9425. pd->a_skill->lv=script_getnum(st,3);
  9426. pd->a_skill->div_ = script_getnum(st,4);
  9427. pd->a_skill->rate=script_getnum(st,5);
  9428. pd->a_skill->bonusrate=script_getnum(st,6);
  9429. return 0;
  9430. }
  9431. /*==========================================
  9432. * pet support skills [Skotlex]
  9433. *------------------------------------------
  9434. */
  9435. BUILDIN_FUNC(petskillsupport)
  9436. {
  9437. struct pet_data *pd;
  9438. struct map_session_data *sd=script_rid2sd(st);
  9439. if(sd==NULL || sd->pd==NULL)
  9440. return 0;
  9441. pd=sd->pd;
  9442. if (pd->s_skill)
  9443. { //Clear previous skill
  9444. if (pd->s_skill->timer != -1)
  9445. {
  9446. if (pd->s_skill->id)
  9447. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9448. else
  9449. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9450. }
  9451. } else //init memory
  9452. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9453. pd->s_skill->id=script_getnum(st,2);
  9454. pd->s_skill->lv=script_getnum(st,3);
  9455. pd->s_skill->delay=script_getnum(st,4);
  9456. pd->s_skill->hp=script_getnum(st,5);
  9457. pd->s_skill->sp=script_getnum(st,6);
  9458. //Use delay as initial offset to avoid skill/heal exploits
  9459. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9460. pd->s_skill->timer=-1;
  9461. else
  9462. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9463. return 0;
  9464. }
  9465. /*==========================================
  9466. * Scripted skill effects [Celest]
  9467. *------------------------------------------
  9468. */
  9469. BUILDIN_FUNC(skilleffect)
  9470. {
  9471. struct map_session_data *sd;
  9472. int skillid=script_getnum(st,2);
  9473. int skilllv=script_getnum(st,3);
  9474. sd=script_rid2sd(st);
  9475. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9476. return 0;
  9477. }
  9478. /*==========================================
  9479. * NPC skill effects [Valaris]
  9480. *------------------------------------------
  9481. */
  9482. BUILDIN_FUNC(npcskilleffect)
  9483. {
  9484. struct block_list *bl= map_id2bl(st->oid);
  9485. int skillid=script_getnum(st,2);
  9486. int skilllv=script_getnum(st,3);
  9487. int x=script_getnum(st,4);
  9488. int y=script_getnum(st,5);
  9489. if (bl)
  9490. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9491. return 0;
  9492. }
  9493. /*==========================================
  9494. * Special effects [Valaris]
  9495. *------------------------------------------
  9496. */
  9497. BUILDIN_FUNC(specialeffect)
  9498. {
  9499. struct block_list *bl=map_id2bl(st->oid);
  9500. if(bl==NULL)
  9501. return 0;
  9502. clif_specialeffect(bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9503. return 0;
  9504. }
  9505. BUILDIN_FUNC(specialeffect2)
  9506. {
  9507. struct map_session_data *sd=script_rid2sd(st);
  9508. if(sd==NULL)
  9509. return 0;
  9510. clif_specialeffect(&sd->bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9511. return 0;
  9512. }
  9513. /*==========================================
  9514. * Nude [Valaris]
  9515. *------------------------------------------
  9516. */
  9517. BUILDIN_FUNC(nude)
  9518. {
  9519. struct map_session_data *sd=script_rid2sd(st);
  9520. int i,calcflag=0;
  9521. if(sd==NULL)
  9522. return 0;
  9523. for(i=0;i<11;i++)
  9524. if(sd->equip_index[i] >= 0) {
  9525. if(!calcflag)
  9526. calcflag=1;
  9527. pc_unequipitem(sd,sd->equip_index[i],2);
  9528. }
  9529. if(calcflag)
  9530. status_calc_pc(sd,0);
  9531. return 0;
  9532. }
  9533. /*==========================================
  9534. * gmcommand [MouseJstr]
  9535. *
  9536. * suggested on the forums...
  9537. * splitted into atcommand & charcommand by [Skotlex]
  9538. *------------------------------------------
  9539. */
  9540. BUILDIN_FUNC(atcommand)
  9541. {
  9542. struct map_session_data *sd=NULL;
  9543. const char *cmd;
  9544. cmd = script_getstr(st,2);
  9545. if (st->rid)
  9546. sd = script_rid2sd(st);
  9547. if (sd){
  9548. if(cmd[0] != atcommand_symbol){
  9549. cmd += strlen(sd->status.name);
  9550. while(*cmd != atcommand_symbol && *cmd != 0)
  9551. cmd++;
  9552. }
  9553. is_atcommand_sub(sd->fd, sd, cmd, 99);
  9554. } else { //Use a dummy character.
  9555. struct map_session_data dummy_sd;
  9556. struct block_list *bl = NULL;
  9557. memset(&dummy_sd, 0, sizeof(struct map_session_data));
  9558. if (st->oid) bl = map_id2bl(st->oid);
  9559. if (bl) {
  9560. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9561. if (bl->type == BL_NPC)
  9562. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9563. }
  9564. if(cmd[0] != atcommand_symbol){
  9565. cmd += strlen(dummy_sd.status.name);
  9566. while(*cmd != atcommand_symbol && *cmd != 0)
  9567. cmd++;
  9568. }
  9569. is_atcommand_sub(0, &dummy_sd, cmd, 99);
  9570. }
  9571. return 0;
  9572. }
  9573. BUILDIN_FUNC(charcommand)
  9574. {
  9575. struct map_session_data *sd=NULL;
  9576. const char *cmd;
  9577. cmd = script_getstr(st,2);
  9578. if (st->rid)
  9579. sd = script_rid2sd(st);
  9580. if (sd){
  9581. if(cmd[0] != charcommand_symbol){
  9582. cmd += strlen(sd->status.name);
  9583. while(*cmd != charcommand_symbol && *cmd != 0)
  9584. cmd++;
  9585. }
  9586. is_charcommand_sub(sd->fd, sd, cmd,99);
  9587. } else { //Use a dummy character.
  9588. struct map_session_data dummy_sd;
  9589. struct block_list *bl = NULL;
  9590. memset(&dummy_sd, 0, sizeof(struct map_session_data));
  9591. if (st->oid) bl = map_id2bl(st->oid);
  9592. if (bl) {
  9593. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9594. if (bl->type == BL_NPC)
  9595. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9596. }
  9597. if(cmd[0] != charcommand_symbol){
  9598. cmd += strlen(dummy_sd.status.name);
  9599. while(*cmd != charcommand_symbol && *cmd != 0)
  9600. cmd++;
  9601. }
  9602. is_charcommand_sub(0, &dummy_sd, cmd, 99);
  9603. }
  9604. return 0;
  9605. }
  9606. /*==========================================
  9607. * Displays a message for the player only (like system messages like "you got an apple" )
  9608. *------------------------------------------
  9609. */
  9610. BUILDIN_FUNC(dispbottom)
  9611. {
  9612. struct map_session_data *sd=script_rid2sd(st);
  9613. const char *message;
  9614. message=script_getstr(st,2);
  9615. if(sd)
  9616. clif_disp_onlyself(sd,message,(int)strlen(message));
  9617. return 0;
  9618. }
  9619. /*==========================================
  9620. * All The Players Full Recovery
  9621. (HP/SP full restore and resurrect if need)
  9622. *------------------------------------------
  9623. */
  9624. BUILDIN_FUNC(recovery)
  9625. {
  9626. struct map_session_data *sd, **all_sd;
  9627. int i = 0, users;
  9628. all_sd = map_getallusers(&users);
  9629. for (i = 0; i < users; i++)
  9630. {
  9631. sd = all_sd[i];
  9632. if(pc_isdead(sd))
  9633. status_revive(&sd->bl, 100, 100);
  9634. else
  9635. status_percent_heal(&sd->bl, 100, 100);
  9636. clif_displaymessage(sd->fd,"You have been recovered!");
  9637. }
  9638. return 0;
  9639. }
  9640. /*==========================================
  9641. * Get your pet info: getpetinfo(n)
  9642. * n -> 0:pet_id 1:pet_class 2:pet_name
  9643. * 3:friendly 4:hungry, 5: rename flag.
  9644. *------------------------------------------
  9645. */
  9646. BUILDIN_FUNC(getpetinfo)
  9647. {
  9648. struct map_session_data *sd=script_rid2sd(st);
  9649. struct pet_data *pd;
  9650. int type=script_getnum(st,2);
  9651. if(sd && sd->status.pet_id && sd->pd){
  9652. pd = sd->pd;
  9653. switch(type){
  9654. case 0:
  9655. script_pushint(st,sd->status.pet_id);
  9656. break;
  9657. case 1:
  9658. script_pushint(st,pd->pet.class_);
  9659. break;
  9660. case 2:
  9661. if(pd->pet.name)
  9662. script_pushconststr(st,pd->pet.name);
  9663. else
  9664. script_pushconststr(st,"null");
  9665. break;
  9666. case 3:
  9667. script_pushint(st,pd->pet.intimate);
  9668. break;
  9669. case 4:
  9670. script_pushint(st,pd->pet.hungry);
  9671. break;
  9672. case 5:
  9673. script_pushint(st,pd->pet.rename_flag);
  9674. break;
  9675. default:
  9676. script_pushint(st,0);
  9677. break;
  9678. }
  9679. }else{
  9680. script_pushint(st,0);
  9681. }
  9682. return 0;
  9683. }
  9684. /*==========================================
  9685. * Shows wether your inventory(and equips) contain
  9686. selected card or not.
  9687. checkequipedcard(4001);
  9688. *------------------------------------------
  9689. */
  9690. BUILDIN_FUNC(checkequipedcard)
  9691. {
  9692. struct map_session_data *sd=script_rid2sd(st);
  9693. int n,i,c=0;
  9694. c=script_getnum(st,2);
  9695. if(sd){
  9696. for(i=0;i<MAX_INVENTORY;i++){
  9697. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9698. for(n=0;n<MAX_SLOTS;n++){
  9699. if(sd->status.inventory[i].card[n]==c){
  9700. script_pushint(st,1);
  9701. return 0;
  9702. }
  9703. }
  9704. }
  9705. }
  9706. }
  9707. script_pushint(st,0);
  9708. return 0;
  9709. }
  9710. BUILDIN_FUNC(jump_zero)
  9711. {
  9712. int sel;
  9713. sel=script_getnum(st,2);
  9714. if(!sel) {
  9715. int pos;
  9716. if( !data_islabel(script_getdata(st,3)) ){
  9717. ShowError("script: jump_zero: not label !\n");
  9718. st->state=END;
  9719. return 1;
  9720. }
  9721. pos=script_getnum(st,3);
  9722. st->pos=pos;
  9723. st->state=GOTO;
  9724. // printf("script: jump_zero: jumpto : %d\n",pos);
  9725. } else {
  9726. // printf("script: jump_zero: fail\n");
  9727. }
  9728. return 0;
  9729. }
  9730. BUILDIN_FUNC(select)
  9731. {
  9732. char *buf, *ptr;
  9733. int len,i;
  9734. struct map_session_data *sd;
  9735. sd=script_rid2sd(st);
  9736. nullpo_retr(0, sd);
  9737. if(sd->state.menu_or_input==0){
  9738. st->state=RERUNLINE;
  9739. sd->state.menu_or_input=1;
  9740. for(i=st->start+2,len=16;i<st->end;i++){
  9741. conv_str(st,& (st->stack->stack_data[i]));
  9742. len+=(int)strlen(st->stack->stack_data[i].u.str)+1;
  9743. }
  9744. buf=(char *)aMalloc((len+1)*sizeof(char));
  9745. buf[0]=0;
  9746. for(i=st->start+2,len=0;i<st->end;i++){
  9747. strcat(buf,st->stack->stack_data[i].u.str);
  9748. strcat(buf,":");
  9749. }
  9750. ptr = buf;
  9751. sd->npc_menu = 0; //Reuse to store max menu entries. Avoids the need of an extra variable.
  9752. while (ptr && (ptr = strchr(ptr, ':')) != NULL)
  9753. { sd->npc_menu++; ptr++; }
  9754. clif_scriptmenu(sd,st->oid,buf);
  9755. aFree(buf);
  9756. } else if(sd->npc_menu==0xff){
  9757. sd->state.menu_or_input=0;
  9758. st->state=END;
  9759. } else {
  9760. //Skip empty menu entries which weren't displayed on the client (Skotlex)
  9761. for(i=st->start+2;i< (st->start+2+sd->npc_menu) && sd->npc_menu < (st->end-st->start-2);i++) {
  9762. conv_str(st,& (st->stack->stack_data[i])); // we should convert variables to strings before access it [jA1983] [EoE]
  9763. if((int)strlen(st->stack->stack_data[i].u.str) < 1)
  9764. sd->npc_menu++; //Empty selection which wasn't displayed on the client.
  9765. }
  9766. pc_setreg(sd,add_str("@menu"),sd->npc_menu);
  9767. sd->state.menu_or_input=0;
  9768. script_pushint(st,sd->npc_menu);
  9769. }
  9770. return 0;
  9771. }
  9772. BUILDIN_FUNC(prompt)
  9773. {
  9774. char *buf, *ptr;
  9775. int len,i;
  9776. struct map_session_data *sd;
  9777. sd=script_rid2sd(st);
  9778. nullpo_retr(0, sd);
  9779. if(sd->state.menu_or_input==0){
  9780. st->state=RERUNLINE;
  9781. sd->state.menu_or_input=1;
  9782. for(i=st->start+2,len=16;i<st->end;i++){
  9783. conv_str(st,& (st->stack->stack_data[i]));
  9784. len+=(int)strlen(st->stack->stack_data[i].u.str)+1;
  9785. }
  9786. buf=(char *)aMalloc((len+1)*sizeof(char));
  9787. buf[0]=0;
  9788. for(i=st->start+2,len=0;i<st->end;i++){
  9789. strcat(buf,st->stack->stack_data[i].u.str);
  9790. strcat(buf,":");
  9791. }
  9792. ptr = buf;
  9793. sd->npc_menu = 0; //Reuse to store max menu entries. Avoids the need of an extra variable.
  9794. while (ptr && (ptr = strchr(ptr, ':')) != NULL)
  9795. { sd->npc_menu++; ptr++; }
  9796. clif_scriptmenu(sd,st->oid,buf);
  9797. aFree(buf);
  9798. } else {
  9799. if(sd->npc_menu != 0xff){
  9800. //Skip empty menu entries which weren't displayed on the client (Skotlex)
  9801. for(i=st->start+2;i< (st->start+2+sd->npc_menu) && sd->npc_menu < (st->end-st->start-2);i++) {
  9802. conv_str(st,& (st->stack->stack_data[i])); // we should convert variables to strings before access it [jA1983] [EoE]
  9803. if((int)strlen(st->stack->stack_data[i].u.str) < 1)
  9804. sd->npc_menu++; //Empty selection which wasn't displayed on the client.
  9805. }
  9806. }
  9807. pc_setreg(sd,add_str("@menu"),sd->npc_menu);
  9808. sd->state.menu_or_input=0;
  9809. script_pushint(st,sd->npc_menu);
  9810. }
  9811. return 0;
  9812. }
  9813. /*==========================================
  9814. * GetMapMobs
  9815. returns mob counts on a set map:
  9816. e.g. GetMapMobs("prontera")
  9817. use "this" - for player's map
  9818. *------------------------------------------
  9819. */
  9820. BUILDIN_FUNC(getmapmobs)
  9821. {
  9822. const char *str=NULL;
  9823. int m=-1,bx,by,i;
  9824. int count=0,c;
  9825. struct block_list *bl;
  9826. str=script_getstr(st,2);
  9827. if(strcmp(str,"this")==0){
  9828. struct map_session_data *sd=script_rid2sd(st);
  9829. if(sd)
  9830. m=sd->bl.m;
  9831. else{
  9832. script_pushint(st,-1);
  9833. return 0;
  9834. }
  9835. }else
  9836. m=map_mapname2mapid(str);
  9837. if(m < 0){
  9838. script_pushint(st,-1);
  9839. return 0;
  9840. }
  9841. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++){
  9842. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++){
  9843. bl = map[m].block_mob[bx+by*map[m].bxs];
  9844. c = map[m].block_mob_count[bx+by*map[m].bxs];
  9845. for(i=0;i<c && bl;i++,bl=bl->next){
  9846. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9847. count++;
  9848. }
  9849. }
  9850. }
  9851. script_pushint(st,count);
  9852. return 0;
  9853. }
  9854. /*==========================================
  9855. * movenpc [MouseJstr]
  9856. *------------------------------------------
  9857. */
  9858. BUILDIN_FUNC(movenpc)
  9859. {
  9860. TBL_NPC *nd = NULL;
  9861. const char *npc;
  9862. int x,y;
  9863. npc = script_getstr(st,2);
  9864. x = script_getnum(st,3);
  9865. y = script_getnum(st,4);
  9866. if ((nd = npc_name2id(npc)) == NULL)
  9867. return -1;
  9868. npc_movenpc(nd, x, y);
  9869. return 0;
  9870. }
  9871. /*==========================================
  9872. * message [MouseJstr]
  9873. *------------------------------------------
  9874. */
  9875. BUILDIN_FUNC(message)
  9876. {
  9877. struct map_session_data *sd;
  9878. const char *msg,*player;
  9879. struct map_session_data *pl_sd = NULL;
  9880. sd = script_rid2sd(st);
  9881. player = script_getstr(st,2);
  9882. msg = script_getstr(st,3);
  9883. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9884. return 0;
  9885. clif_displaymessage(pl_sd->fd, msg);
  9886. return 0;
  9887. }
  9888. /*==========================================
  9889. * npctalk (sends message to surrounding area)
  9890. *------------------------------------------*/
  9891. BUILDIN_FUNC(npctalk)
  9892. {
  9893. const char *str;
  9894. char message[255];
  9895. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9896. str=script_getstr(st,2);
  9897. if(nd) {
  9898. memcpy(message, nd->name, NAME_LENGTH);
  9899. strcat(message," : ");
  9900. strncat(message,str, 254); //Prevent overflow possibility. [Skotlex]
  9901. clif_message(&(nd->bl), message);
  9902. }
  9903. return 0;
  9904. }
  9905. /*==========================================
  9906. * hasitems (checks to see if player has any
  9907. * items on them, if so will return a 1)
  9908. * [Valaris]
  9909. *------------------------------------------
  9910. */
  9911. BUILDIN_FUNC(hasitems)
  9912. {
  9913. int i;
  9914. struct map_session_data *sd;
  9915. sd=script_rid2sd(st);
  9916. for(i=0; i<MAX_INVENTORY; i++) {
  9917. if(sd->status.inventory[i].amount && sd->status.inventory[i].nameid!=2364 && sd->status.inventory[i].nameid!=2365) {
  9918. script_pushint(st,1);
  9919. return 0;
  9920. }
  9921. }
  9922. script_pushint(st,0);
  9923. return 0;
  9924. }
  9925. // change npc walkspeed [Valaris]
  9926. BUILDIN_FUNC(npcspeed)
  9927. {
  9928. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9929. int x=0;
  9930. x=script_getnum(st,2);
  9931. if(nd) {
  9932. nd->speed=x;
  9933. }
  9934. return 0;
  9935. }
  9936. // make an npc walk to a position [Valaris]
  9937. BUILDIN_FUNC(npcwalkto)
  9938. {
  9939. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9940. int x=0,y=0;
  9941. x=script_getnum(st,2);
  9942. y=script_getnum(st,3);
  9943. if(nd) {
  9944. unit_walktoxy(&nd->bl,x,y,0);
  9945. }
  9946. return 0;
  9947. }
  9948. // stop an npc's movement [Valaris]
  9949. BUILDIN_FUNC(npcstop)
  9950. {
  9951. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9952. if(nd) {
  9953. unit_stop_walking(&nd->bl,1);
  9954. }
  9955. return 0;
  9956. }
  9957. /*==========================================
  9958. * getlook char info. getlook(arg)
  9959. *------------------------------------------
  9960. */
  9961. BUILDIN_FUNC(getlook)
  9962. {
  9963. int type,val;
  9964. struct map_session_data *sd;
  9965. sd=script_rid2sd(st);
  9966. type=script_getnum(st,2);
  9967. val=-1;
  9968. switch(type){
  9969. case LOOK_HAIR: //1
  9970. val=sd->status.hair;
  9971. break;
  9972. case LOOK_WEAPON: //2
  9973. val=sd->status.weapon;
  9974. break;
  9975. case LOOK_HEAD_BOTTOM: //3
  9976. val=sd->status.head_bottom;
  9977. break;
  9978. case LOOK_HEAD_TOP: //4
  9979. val=sd->status.head_top;
  9980. break;
  9981. case LOOK_HEAD_MID: //5
  9982. val=sd->status.head_mid;
  9983. break;
  9984. case LOOK_HAIR_COLOR: //6
  9985. val=sd->status.hair_color;
  9986. break;
  9987. case LOOK_CLOTHES_COLOR: //7
  9988. val=sd->status.clothes_color;
  9989. break;
  9990. case LOOK_SHIELD: //8
  9991. val=sd->status.shield;
  9992. break;
  9993. case LOOK_SHOES: //9
  9994. break;
  9995. }
  9996. script_pushint(st,val);
  9997. return 0;
  9998. }
  9999. /*==========================================
  10000. * get char save point. argument: 0- map name, 1- x, 2- y
  10001. *------------------------------------------
  10002. */
  10003. BUILDIN_FUNC(getsavepoint)
  10004. {
  10005. int x,y,type;
  10006. char *mapname;
  10007. struct map_session_data *sd;
  10008. sd=script_rid2sd(st);
  10009. type=script_getnum(st,2);
  10010. x=sd->status.save_point.x;
  10011. y=sd->status.save_point.y;
  10012. switch(type){
  10013. case 0:
  10014. mapname=(char *) aMallocA((MAP_NAME_LENGTH)*sizeof(char));
  10015. memcpy(mapname, mapindex_id2name(sd->status.save_point.map), MAP_NAME_LENGTH);
  10016. mapname[MAP_NAME_LENGTH-1]='\0';
  10017. script_pushstr(st,mapname);
  10018. break;
  10019. case 1:
  10020. script_pushint(st,x);
  10021. break;
  10022. case 2:
  10023. script_pushint(st,y);
  10024. break;
  10025. default:
  10026. script_pushint(st,0);
  10027. break;
  10028. }
  10029. return 0;
  10030. }
  10031. /*==========================================
  10032. * Get position for char/npc/pet/mob objects. Added by Lorky
  10033. *
  10034. * int getMapXY(MapName$,MaxX,MapY,type,[CharName$]);
  10035. * where type:
  10036. * MapName$ - String variable for output map name
  10037. * MapX - Integer variable for output coord X
  10038. * MapY - Integer variable for output coord Y
  10039. * type - type of object
  10040. * 0 - Character coord
  10041. * 1 - NPC coord
  10042. * 2 - Pet coord
  10043. * 3 - Mob coord (not released)
  10044. * 4 - Homun coord
  10045. * CharName$ - Name object. If miss or "this" the current object
  10046. *
  10047. * Return:
  10048. * 0 - success
  10049. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10050. *------------------------------------------
  10051. */
  10052. BUILDIN_FUNC(getmapxy)
  10053. {
  10054. struct block_list *bl = NULL;
  10055. struct map_session_data *sd=NULL;
  10056. int num;
  10057. char *name;
  10058. char prefix;
  10059. int x,y,type;
  10060. char mapname[MAP_NAME_LENGTH];
  10061. memset(mapname, 0, sizeof(mapname));
  10062. if( !data_isreference(script_getdata(st,2)) ){
  10063. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10064. script_pushint(st,-1);
  10065. return 1;
  10066. }
  10067. if( !data_isreference(script_getdata(st,3)) ){
  10068. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10069. script_pushint(st,-1);
  10070. return 1;
  10071. }
  10072. if( !data_isreference(script_getdata(st,4)) ){
  10073. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10074. script_pushint(st,-1);
  10075. return 1;
  10076. }
  10077. //??????????? >>> Possible needly check function parameters on C_STR,C_INT,C_INT <<< ???????????//
  10078. type=script_getnum(st,5);
  10079. switch (type){
  10080. case 0: //Get Character Position
  10081. if( script_hasdata(st,6) )
  10082. sd=map_nick2sd(script_getstr(st,6));
  10083. else
  10084. sd=script_rid2sd(st);
  10085. if (sd)
  10086. bl = &sd->bl;
  10087. break;
  10088. case 1: //Get NPC Position
  10089. if( script_hasdata(st,6) )
  10090. {
  10091. struct npc_data *nd;
  10092. nd=npc_name2id(script_getstr(st,6));
  10093. if (nd)
  10094. bl = &nd->bl;
  10095. } else //In case the origin is not an npc?
  10096. bl=map_id2bl(st->oid);
  10097. break;
  10098. case 2: //Get Pet Position
  10099. if(script_hasdata(st,6))
  10100. sd=map_nick2sd(script_getstr(st,6));
  10101. else
  10102. sd=script_rid2sd(st);
  10103. if (sd && sd->pd)
  10104. bl = &sd->pd->bl;
  10105. break;
  10106. case 3: //Get Mob Position
  10107. break; //Not supported?
  10108. case 4: //Get Homun Position
  10109. if(script_hasdata(st,6))
  10110. sd=map_nick2sd(script_getstr(st,6));
  10111. else
  10112. sd=script_rid2sd(st);
  10113. if (sd && sd->hd)
  10114. bl = &sd->hd->bl;
  10115. break;
  10116. }
  10117. if (!bl) { //No object found.
  10118. script_pushint(st,-1);
  10119. return 0;
  10120. }
  10121. x= bl->x;
  10122. y= bl->y;
  10123. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10124. //Set MapName$
  10125. num=st->stack->stack_data[st->start+2].u.num;
  10126. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10127. prefix=*name;
  10128. if(not_server_variable(prefix))
  10129. sd=script_rid2sd(st);
  10130. else
  10131. sd=NULL;
  10132. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10133. //Set MapX
  10134. num=st->stack->stack_data[st->start+3].u.num;
  10135. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10136. prefix=*name;
  10137. if(not_server_variable(prefix))
  10138. sd=script_rid2sd(st);
  10139. else
  10140. sd=NULL;
  10141. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10142. //Set MapY
  10143. num=st->stack->stack_data[st->start+4].u.num;
  10144. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10145. prefix=*name;
  10146. if(not_server_variable(prefix))
  10147. sd=script_rid2sd(st);
  10148. else
  10149. sd=NULL;
  10150. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10151. //Return Success value
  10152. script_pushint(st,0);
  10153. return 0;
  10154. }
  10155. /*==========================================
  10156. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10157. *------------------------------------------
  10158. */
  10159. BUILDIN_FUNC(logmes)
  10160. {
  10161. const char *str;
  10162. if (log_config.npc <= 0 ) return 0;
  10163. str = script_getstr(st,2);
  10164. log_npc(script_rid2sd(st),str);
  10165. return 0;
  10166. }
  10167. BUILDIN_FUNC(summon)
  10168. {
  10169. int _class, timeout=0;
  10170. const char *str,*event="";
  10171. struct map_session_data *sd;
  10172. struct mob_data *md;
  10173. int tick = gettick();
  10174. sd=script_rid2sd(st);
  10175. if (!sd) return 0;
  10176. str =script_getstr(st,2);
  10177. _class=script_getnum(st,3);
  10178. if( script_hasdata(st,4) )
  10179. timeout=script_getnum(st,4);
  10180. if( script_hasdata(st,5) ){
  10181. event=script_getstr(st,5);
  10182. check_event(st, event);
  10183. }
  10184. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10185. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10186. if (md) {
  10187. md->master_id=sd->bl.id;
  10188. md->special_state.ai=1;
  10189. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10190. mob_spawn (md); //Now it is ready for spawning.
  10191. clif_misceffect2(&md->bl,344);
  10192. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10193. }
  10194. return 0;
  10195. }
  10196. /*==========================================
  10197. * Checks whether it is daytime/nighttime
  10198. *------------------------------------------
  10199. */
  10200. BUILDIN_FUNC(isnight)
  10201. {
  10202. script_pushint(st,(night_flag == 1));
  10203. return 0;
  10204. }
  10205. BUILDIN_FUNC(isday)
  10206. {
  10207. script_pushint(st,(night_flag == 0));
  10208. return 0;
  10209. }
  10210. /*================================================
  10211. * Check whether another item/card has been
  10212. * equipped - used for 2/15's cards patch [celest]
  10213. *------------------------------------------------
  10214. */
  10215. // leave this here, just in case
  10216. #if 0
  10217. BUILDIN_FUNC(isequipped)
  10218. {
  10219. struct map_session_data *sd;
  10220. int i, j, k, id = 1;
  10221. int ret = -1;
  10222. sd = script_rid2sd(st);
  10223. for (i=0; id!=0; i++) {
  10224. int flag = 0;
  10225. FETCH (i+2, id) else id = 0;
  10226. if (id <= 0)
  10227. continue;
  10228. for (j=0; j<EQI_MAX; j++) {
  10229. int index;
  10230. index = sd->equip_index[j];
  10231. if(index < 0) continue;
  10232. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10233. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10234. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10235. if(!sd->inventory_data[index])
  10236. continue;
  10237. if(itemdb_type(id) != IT_CARD) { //Non card
  10238. if (sd->inventory_data[index]->nameid != id)
  10239. continue;
  10240. flag = 1;
  10241. break;
  10242. } else { //Card
  10243. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10244. continue;
  10245. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10246. if (sd->status.inventory[index].card[k] == id)
  10247. {
  10248. flag = 1;
  10249. break;
  10250. }
  10251. }
  10252. }
  10253. if (flag) break;
  10254. }
  10255. if (ret == -1)
  10256. ret = flag;
  10257. else
  10258. ret &= flag;
  10259. if (!ret) break;
  10260. }
  10261. script_pushint(st,ret);
  10262. return 0;
  10263. }
  10264. #endif
  10265. /*================================================
  10266. * Check how many items/cards in the list are
  10267. * equipped - used for 2/15's cards patch [celest]
  10268. *------------------------------------------------
  10269. */
  10270. BUILDIN_FUNC(isequippedcnt)
  10271. {
  10272. struct map_session_data *sd;
  10273. int i, j, k, id = 1;
  10274. int ret = 0;
  10275. sd = script_rid2sd(st);
  10276. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10277. script_pushint(st,0);
  10278. return 0;
  10279. }
  10280. for (i=0; id!=0; i++) {
  10281. FETCH (i+2, id) else id = 0;
  10282. if (id <= 0)
  10283. continue;
  10284. for (j=0; j<EQI_MAX; j++) {
  10285. int index;
  10286. index = sd->equip_index[j];
  10287. if(index < 0) continue;
  10288. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10289. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10290. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10291. if(!sd->inventory_data[index])
  10292. continue;
  10293. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10294. if (sd->inventory_data[index]->nameid == id)
  10295. ret+= sd->status.inventory[index].amount;
  10296. } else { //Count cards.
  10297. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10298. continue; //No cards
  10299. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10300. if (sd->status.inventory[index].card[k] == id)
  10301. ret++; //[Lupus]
  10302. }
  10303. }
  10304. }
  10305. }
  10306. script_pushint(st,ret);
  10307. return 0;
  10308. }
  10309. /*================================================
  10310. * Check whether another card has been
  10311. * equipped - used for 2/15's cards patch [celest]
  10312. * -- Items checked cannot be reused in another
  10313. * card set to prevent exploits
  10314. *------------------------------------------------
  10315. */
  10316. BUILDIN_FUNC(isequipped)
  10317. {
  10318. struct map_session_data *sd;
  10319. int i, j, k, id = 1;
  10320. int index, flag;
  10321. int ret = -1;
  10322. //Original hash to reverse it when full check fails.
  10323. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10324. sd = script_rid2sd(st);
  10325. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10326. script_pushint(st,0);
  10327. return 0;
  10328. }
  10329. setitem_hash = sd->setitem_hash;
  10330. setitem_hash2 = sd->setitem_hash2;
  10331. for (i=0; id!=0; i++)
  10332. {
  10333. FETCH (i+2, id) else id = 0;
  10334. if (id <= 0)
  10335. continue;
  10336. flag = 0;
  10337. for (j=0; j<EQI_MAX; j++)
  10338. {
  10339. index = sd->equip_index[j];
  10340. if(index < 0) continue;
  10341. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10342. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10343. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10344. if(!sd->inventory_data[index])
  10345. continue;
  10346. if (itemdb_type(id) != IT_CARD) {
  10347. if (sd->inventory_data[index]->nameid != id)
  10348. continue;
  10349. flag = 1;
  10350. break;
  10351. } else { //Cards
  10352. if (sd->inventory_data[index]->slot == 0 ||
  10353. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10354. continue;
  10355. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10356. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10357. unsigned int hash = 0;
  10358. if (sd->status.inventory[index].card[k] != id)
  10359. continue;
  10360. hash = 1<<((j<5?j:j-5)*4 + k);
  10361. // check if card is already used by another set
  10362. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10363. continue;
  10364. // We have found a match
  10365. flag = 1;
  10366. // Set hash so this card cannot be used by another
  10367. if (j<5)
  10368. sd->setitem_hash |= hash;
  10369. else
  10370. sd->setitem_hash2 |= hash;
  10371. break;
  10372. }
  10373. }
  10374. if (flag) break; //Card found
  10375. }
  10376. if (ret == -1)
  10377. ret = flag;
  10378. else
  10379. ret &= flag;
  10380. if (!ret) break;
  10381. }
  10382. if (!ret)
  10383. { //When check fails, restore original hash values. [Skotlex]
  10384. sd->setitem_hash = setitem_hash;
  10385. sd->setitem_hash2 = setitem_hash2;
  10386. }
  10387. script_pushint(st,ret);
  10388. return 0;
  10389. }
  10390. /*================================================
  10391. * Check how many given inserted cards in the CURRENT
  10392. * weapon - used for 2/15's cards patch [Lupus]
  10393. *------------------------------------------------
  10394. */
  10395. BUILDIN_FUNC(cardscnt)
  10396. {
  10397. struct map_session_data *sd;
  10398. int i, k, id = 1;
  10399. int ret = 0;
  10400. int index;
  10401. sd = script_rid2sd(st);
  10402. for (i=0; id!=0; i++) {
  10403. FETCH (i+2, id) else id = 0;
  10404. if (id <= 0)
  10405. continue;
  10406. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10407. if(index < 0) continue;
  10408. if(!sd->inventory_data[index])
  10409. continue;
  10410. if(itemdb_type(id) != IT_CARD) {
  10411. if (sd->inventory_data[index]->nameid == id)
  10412. ret+= sd->status.inventory[index].amount;
  10413. } else {
  10414. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10415. continue;
  10416. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10417. if (sd->status.inventory[index].card[k] == id)
  10418. ret++;
  10419. }
  10420. }
  10421. }
  10422. script_pushint(st,ret);
  10423. // script_pushint(st,current_equip_item_index);
  10424. return 0;
  10425. }
  10426. /*=======================================================
  10427. * Returns the refined number of the current item, or an
  10428. * item with inventory index specified
  10429. *-------------------------------------------------------
  10430. */
  10431. BUILDIN_FUNC(getrefine)
  10432. {
  10433. struct map_session_data *sd;
  10434. if ((sd = script_rid2sd(st))!= NULL)
  10435. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10436. else
  10437. script_pushint(st,0);
  10438. return 0;
  10439. }
  10440. /*=======================================================
  10441. * Allows 2 Parents to adopt a character as a Baby
  10442. *-------------------------------------------------------
  10443. */
  10444. BUILDIN_FUNC(adopt)
  10445. {
  10446. int ret;
  10447. const char *parent1 = script_getstr(st,2);
  10448. const char *parent2 = script_getstr(st,3);
  10449. const char *child = script_getstr(st,4);
  10450. struct map_session_data *p1_sd = map_nick2sd(parent1);
  10451. struct map_session_data *p2_sd = map_nick2sd(parent2);
  10452. struct map_session_data *c_sd = map_nick2sd(child);
  10453. if (!p1_sd || !p2_sd || !c_sd ||
  10454. p1_sd->status.base_level < 70 ||
  10455. p2_sd->status.base_level < 70)
  10456. return 0;
  10457. ret = pc_adoption(p1_sd, p2_sd, c_sd);
  10458. script_pushint(st,ret);
  10459. return 0;
  10460. }
  10461. /*=======================================================
  10462. * Day/Night controls
  10463. *-------------------------------------------------------
  10464. */
  10465. BUILDIN_FUNC(night)
  10466. {
  10467. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10468. return 0;
  10469. }
  10470. BUILDIN_FUNC(day)
  10471. {
  10472. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10473. return 0;
  10474. }
  10475. //=======================================================
  10476. // Unequip [Spectre]
  10477. //-------------------------------------------------------
  10478. BUILDIN_FUNC(unequip)
  10479. {
  10480. int i;
  10481. size_t num;
  10482. struct map_session_data *sd;
  10483. num = script_getnum(st,2) - 1;
  10484. sd=script_rid2sd(st);
  10485. if(sd!=NULL && num<10)
  10486. {
  10487. i=pc_checkequip(sd,equip[num]);
  10488. pc_unequipitem(sd,i,2);
  10489. return 0;
  10490. }
  10491. return 0;
  10492. }
  10493. BUILDIN_FUNC(equip)
  10494. {
  10495. int nameid=0,i;
  10496. struct map_session_data *sd;
  10497. struct item_data *item_data;
  10498. sd = script_rid2sd(st);
  10499. nameid=script_getnum(st,2);
  10500. if((item_data = itemdb_exists(nameid)) == NULL)
  10501. {
  10502. if(battle_config.error_log)
  10503. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10504. return 1;
  10505. }
  10506. for(i=0;i<MAX_INVENTORY && sd->status.inventory[i].nameid!=nameid;i++);
  10507. if(i==MAX_INVENTORY) return 0;
  10508. pc_equipitem(sd,i,item_data->equip);
  10509. return 0;
  10510. }
  10511. BUILDIN_FUNC(autoequip)
  10512. {
  10513. int nameid, flag;
  10514. struct item_data *item_data;
  10515. nameid=script_getnum(st,2);
  10516. flag=script_getnum(st,3);
  10517. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10518. item_data->flag.autoequip = flag>0?1:0;
  10519. }
  10520. return 0;
  10521. }
  10522. BUILDIN_FUNC(setbattleflag)
  10523. {
  10524. const char *flag, *value;
  10525. flag = script_getstr(st,2);
  10526. value = script_getstr(st,3);
  10527. if (battle_set_value(flag, value) == 0)
  10528. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10529. else
  10530. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10531. return 0;
  10532. }
  10533. BUILDIN_FUNC(getbattleflag)
  10534. {
  10535. const char *flag;
  10536. flag = script_getstr(st,2);
  10537. script_pushint(st,battle_get_value(flag));
  10538. return 0;
  10539. }
  10540. //=======================================================
  10541. // strlen [Valaris]
  10542. //-------------------------------------------------------
  10543. BUILDIN_FUNC(getstrlen)
  10544. {
  10545. const char *str = script_getstr(st,2);
  10546. int len = (str) ? (int)strlen(str) : 0;
  10547. script_pushint(st,len);
  10548. return 0;
  10549. }
  10550. //=======================================================
  10551. // isalpha [Valaris]
  10552. //-------------------------------------------------------
  10553. BUILDIN_FUNC(charisalpha)
  10554. {
  10555. const char *str=script_getstr(st,2);
  10556. int pos=script_getnum(st,3);
  10557. int val = ( str && pos>0 && (unsigned int)pos<strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10558. script_pushint(st,val);
  10559. return 0;
  10560. }
  10561. // [Lance]
  10562. BUILDIN_FUNC(fakenpcname)
  10563. {
  10564. const char *name;
  10565. const char *newname;
  10566. int look;
  10567. name = script_getstr(st,2);
  10568. newname = script_getstr(st,3);
  10569. look = script_getnum(st,4);
  10570. if(look > 32767 || look < -32768) {
  10571. ShowError("buildin_fakenpcname: Invalid look value %d\n",look);
  10572. return 1; // Safety measure to prevent runtime errors
  10573. }
  10574. npc_changename(name,newname,(short)look);
  10575. return 0;
  10576. }
  10577. BUILDIN_FUNC(atoi)
  10578. {
  10579. const char *value;
  10580. value = script_getstr(st,2);
  10581. script_pushint(st,atoi(value));
  10582. return 0;
  10583. }
  10584. //-----------------------------------------------------------------------//
  10585. // BRING STRSTR TO SCRIPTING ENGINE - LORDALFA START //
  10586. //-----------------------------------------------------------------------//
  10587. BUILDIN_FUNC(compare)
  10588. {
  10589. const char *message;
  10590. const char *cmpstring;
  10591. message = script_getstr(st,2);
  10592. cmpstring = script_getstr(st,3);
  10593. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10594. return 0;
  10595. }
  10596. //-----------------------------------------------------------------------//
  10597. // BRING STRSTR TO SCRIPTING ENGINE - LORDALFA END //
  10598. //-----------------------------------------------------------------------//
  10599. // [zBuffer] List of mathematics commands --->
  10600. BUILDIN_FUNC(sqrt)
  10601. {
  10602. double i, a;
  10603. i = script_getnum(st,2);
  10604. a = sqrt(i);
  10605. script_pushint(st,(int)a);
  10606. return 0;
  10607. }
  10608. BUILDIN_FUNC(pow)
  10609. {
  10610. double i, a, b;
  10611. a = script_getnum(st,2);
  10612. b = script_getnum(st,3);
  10613. i = pow(a,b);
  10614. script_pushint(st,(int)i);
  10615. return 0;
  10616. }
  10617. BUILDIN_FUNC(distance)
  10618. {
  10619. int x0, y0, x1, y1;
  10620. x0 = script_getnum(st,2);
  10621. y0 = script_getnum(st,3);
  10622. x1 = script_getnum(st,4);
  10623. y1 = script_getnum(st,5);
  10624. script_pushint(st,distance(x0-x1, y0-y1));
  10625. return 0;
  10626. }
  10627. BUILDIN_FUNC(checkcell)
  10628. {
  10629. int m;
  10630. const char *map = script_getstr(st, 2);
  10631. m = mapindex_name2id(map);
  10632. if(m){
  10633. script_pushint(st,map_getcell(m, script_getnum(st,3), script_getnum(st,4),script_getnum(st,5)));
  10634. } else {
  10635. script_pushint(st,0);
  10636. }
  10637. return 0;
  10638. }
  10639. // <--- [zBuffer] List of mathematics commands
  10640. // [zBuffer] List of dynamic var commands --->
  10641. void setd_sub(struct script_state *st, struct map_session_data *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10642. {
  10643. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10644. return;
  10645. }
  10646. BUILDIN_FUNC(setd)
  10647. {
  10648. struct map_session_data *sd=NULL;
  10649. char varname[100];
  10650. const char *value, *buffer;
  10651. int elem;
  10652. buffer = script_getstr(st, 2);
  10653. value = script_getstr(st, 3);
  10654. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10655. elem = 0;
  10656. if(st->rid)
  10657. sd = script_rid2sd(st);
  10658. if(varname[strlen(varname)-1] != '$') {
  10659. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10660. } else {
  10661. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10662. }
  10663. return 0;
  10664. }
  10665. BUILDIN_FUNC(query_sql)
  10666. {
  10667. #ifndef TXT_ONLY
  10668. char *name = NULL;
  10669. const char *query;
  10670. int num, i = 0,j, nb_rows;
  10671. struct { char * dst_var_name; char type; } row[32];
  10672. struct map_session_data *sd = (st->rid)? script_rid2sd(st) : NULL;
  10673. query = script_getstr(st,2);
  10674. strcpy(tmp_sql, query);
  10675. if(mysql_query(&mmysql_handle,tmp_sql)){
  10676. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  10677. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  10678. script_pushint(st,0);
  10679. return 1;
  10680. }
  10681. // If some data was returned
  10682. if((sql_res = mysql_store_result(&mmysql_handle))){
  10683. // Count the number of rows to store
  10684. nb_rows = mysql_num_fields(sql_res);
  10685. // Can't store more row than variable
  10686. if (nb_rows > st->end - (st->start+3))
  10687. nb_rows = st->end - (st->start+3);
  10688. if (!nb_rows)
  10689. {
  10690. script_pushint(st,0);
  10691. return 0; // Nothing to store
  10692. }
  10693. if (nb_rows > 32)
  10694. {
  10695. ShowWarning("buildin_query_sql: too many rows!\n");
  10696. script_pushint(st,0);
  10697. return 1;
  10698. }
  10699. memset(row, 0, sizeof(row));
  10700. // Verify argument types
  10701. for(j=0; j < nb_rows; j++)
  10702. {
  10703. if(!data_isreference(script_getdata(st, 3+j))){
  10704. ShowWarning("buildin_query_sql: Parameter %d is not a variable!\n", j);
  10705. script_pushint(st,0);
  10706. return 0;
  10707. } else {
  10708. // Store type of variable (string = 0/int = 1)
  10709. num=st->stack->stack_data[st->start+3+j].u.num;
  10710. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10711. if(name[strlen(name)-1] != '$') {
  10712. row[j].type = 1;
  10713. }
  10714. row[j].dst_var_name = name;
  10715. }
  10716. }
  10717. // Store data
  10718. while(i<128 && (sql_row = mysql_fetch_row(sql_res))){
  10719. for(j=0; j < nb_rows; j++)
  10720. {
  10721. if (row[j].type == 1)
  10722. setd_sub(st,sd, row[j].dst_var_name, i, (void *)atoi(sql_row[j]),script_getref(st,3+j));
  10723. else
  10724. setd_sub(st,sd, row[j].dst_var_name, i, (void *)sql_row[j],script_getref(st,3+j));
  10725. }
  10726. i++;
  10727. }
  10728. // Free data
  10729. mysql_free_result(sql_res);
  10730. }
  10731. script_pushint(st,i);
  10732. #else
  10733. //for TXT version, we always return -1
  10734. script_pushint(st,-1);
  10735. #endif
  10736. return 0;
  10737. }
  10738. //Allows escaping of a given string.
  10739. BUILDIN_FUNC(escape_sql)
  10740. {
  10741. const char *query;
  10742. char *t_query;
  10743. query = script_getstr(st,2);
  10744. t_query = aMallocA((strlen(query)*2+1)*sizeof(char));
  10745. jstrescapecpy(t_query,query);
  10746. script_pushstr(st,t_query);
  10747. return 0;
  10748. }
  10749. BUILDIN_FUNC(getd)
  10750. {
  10751. char varname[100];
  10752. const char *buffer;
  10753. //struct script_data dat;
  10754. int elem;
  10755. buffer = script_getstr(st, 2);
  10756. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10757. elem = 0;
  10758. // Push the 'pointer' so it's more flexible [Lance]
  10759. push_val(st->stack,C_NAME,
  10760. (elem<<24) | add_str(varname));
  10761. return 0;
  10762. }
  10763. // <--- [zBuffer] List of dynamic var commands
  10764. // Pet stat [Lance]
  10765. BUILDIN_FUNC(petstat)
  10766. {
  10767. struct map_session_data *sd = NULL;
  10768. struct pet_data *pd;
  10769. char *tmp;
  10770. int flag = script_getnum(st,2);
  10771. sd = script_rid2sd(st);
  10772. if(!sd || !sd->status.pet_id || !sd->pd){
  10773. if(flag == 2)
  10774. push_str(st->stack, C_CONSTSTR, "");
  10775. else
  10776. script_pushint(st,0);
  10777. return 0;
  10778. }
  10779. pd = sd->pd;
  10780. switch(flag){
  10781. case 1:
  10782. script_pushint(st,(int)pd->pet.class_);
  10783. break;
  10784. case 2:
  10785. tmp = aStrdup(pd->pet.name);
  10786. push_str(st->stack, C_STR, tmp);
  10787. break;
  10788. case 3:
  10789. script_pushint(st,(int)pd->pet.level);
  10790. break;
  10791. case 4:
  10792. script_pushint(st,(int)pd->pet.hungry);
  10793. break;
  10794. case 5:
  10795. script_pushint(st,(int)pd->pet.intimate);
  10796. break;
  10797. default:
  10798. script_pushint(st,0);
  10799. break;
  10800. }
  10801. return 0;
  10802. }
  10803. BUILDIN_FUNC(callshop)
  10804. {
  10805. struct map_session_data *sd = NULL;
  10806. struct npc_data *nd;
  10807. const char *shopname;
  10808. int flag = 0;
  10809. sd = script_rid2sd(st);
  10810. if (!sd) {
  10811. script_pushint(st,0);
  10812. return 0;
  10813. }
  10814. shopname = script_getstr(st, 2);
  10815. if( script_hasdata(st,3) )
  10816. flag = script_getnum(st,3);
  10817. nd = npc_name2id(shopname);
  10818. if (!nd || nd->bl.type!=BL_NPC || nd->bl.subtype!=SHOP) {
  10819. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)", shopname);
  10820. script_pushint(st,0);
  10821. return 1;
  10822. }
  10823. switch (flag) {
  10824. case 1: //Buy window
  10825. npc_buysellsel(sd,nd->bl.id,0);
  10826. break;
  10827. case 2: //Sell window
  10828. npc_buysellsel(sd,nd->bl.id,1);
  10829. break;
  10830. default: //Show menu
  10831. clif_npcbuysell(sd,nd->bl.id);
  10832. break;
  10833. }
  10834. sd->npc_shopid = nd->bl.id;
  10835. script_pushint(st,1);
  10836. return 0;
  10837. }
  10838. #ifndef MAX_SHOPITEM
  10839. #define MAX_SHOPITEM 100
  10840. #endif
  10841. BUILDIN_FUNC(npcshopitem)
  10842. {
  10843. struct npc_data *nd= NULL;
  10844. int n = 0;
  10845. int i = 3;
  10846. int amount;
  10847. const char* npcname = script_getstr(st, 2);
  10848. nd = npc_name2id(npcname);
  10849. if(!nd || nd->bl.subtype!=SHOP)
  10850. { //Not found.
  10851. script_pushint(st,0);
  10852. return 0;
  10853. }
  10854. // st->end - 2 = nameid + value # ... / 2 = number of items ... + 1 to end it
  10855. amount = ((st->end-2)/2)+1;
  10856. //Reinsert as realloc could change the pointer address.
  10857. map_deliddb(&nd->bl);
  10858. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10859. sizeof(nd->u.shop_item[0]) * amount);
  10860. map_addiddb(&nd->bl);
  10861. // Reset sell list.
  10862. memset(nd->u.shop_item, 0, sizeof(nd->u.shop_item[0]) * amount);
  10863. n = 0;
  10864. while (script_hasdata(st,i)) {
  10865. nd->u.shop_item[n].nameid = script_getnum(st,i);
  10866. i++;
  10867. nd->u.shop_item[n].value = script_getnum(st,i);
  10868. i++;
  10869. n++;
  10870. }
  10871. return 0;
  10872. }
  10873. BUILDIN_FUNC(npcshopadditem)
  10874. {
  10875. struct npc_data *nd=NULL;
  10876. int n = 0;
  10877. int i = 3;
  10878. int amount;
  10879. const char* npcname = script_getstr(st, 2);
  10880. nd = npc_name2id(npcname);
  10881. if (!nd || nd->bl.subtype!=SHOP)
  10882. { //Not found.
  10883. script_pushint(st,0);
  10884. return 0;
  10885. }
  10886. amount = ((st->end-2)/2)+1;
  10887. while (nd->u.shop_item[n].nameid && n < MAX_SHOPITEM)
  10888. n++;
  10889. //Reinsert as realloc could change the pointer address.
  10890. map_deliddb(&nd->bl);
  10891. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10892. sizeof(nd->u.shop_item[0]) * (amount+n));
  10893. map_addiddb(&nd->bl);
  10894. while(script_hasdata(st,i)){
  10895. nd->u.shop_item[n].nameid = script_getnum(st,i);
  10896. i++;
  10897. nd->u.shop_item[n].value = script_getnum(st,i);
  10898. i++;
  10899. n++;
  10900. }
  10901. // Marks the last of our stuff..
  10902. nd->u.shop_item[n].value = 0;
  10903. nd->u.shop_item[n].nameid = 0;
  10904. script_pushint(st,1);
  10905. return 0;
  10906. }
  10907. BUILDIN_FUNC(npcshopdelitem)
  10908. {
  10909. struct npc_data *nd=NULL;
  10910. int n=0;
  10911. int i=3;
  10912. int size = 0;
  10913. const char* npcname = script_getstr(st, 2);
  10914. nd = npc_name2id(npcname);
  10915. if (!nd || nd->bl.subtype!=SHOP)
  10916. { //Not found.
  10917. script_pushint(st,0);
  10918. return 0;
  10919. }
  10920. while (nd->u.shop_item[size].nameid)
  10921. size++;
  10922. while (script_hasdata(st,i)) {
  10923. for(n=0;nd->u.shop_item[n].nameid && n < MAX_SHOPITEM;n++) {
  10924. if (nd->u.shop_item[n].nameid == script_getnum(st,i)) {
  10925. // We're moving 1 extra empty block. Junk data is eliminated later.
  10926. memmove(&nd->u.shop_item[n], &nd->u.shop_item[n+1], sizeof(nd->u.shop_item[0])*(size-n));
  10927. }
  10928. }
  10929. i++;
  10930. }
  10931. size = 0;
  10932. while (nd->u.shop_item[size].nameid)
  10933. size++;
  10934. //Reinsert as realloc could change the pointer address.
  10935. map_deliddb(&nd->bl);
  10936. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10937. sizeof(nd->u.shop_item[0]) * (size+1));
  10938. map_addiddb(&nd->bl);
  10939. script_pushint(st,1);
  10940. return 0;
  10941. }
  10942. //Sets a script to attach to an npc.
  10943. BUILDIN_FUNC(npcshopattach)
  10944. {
  10945. struct npc_data *nd=NULL;
  10946. const char* npcname = script_getstr(st, 2);
  10947. int flag = 1;
  10948. if( script_hasdata(st,3) )
  10949. flag = script_getnum(st,3);
  10950. nd = npc_name2id(npcname);
  10951. if (!nd || nd->bl.subtype!=SHOP)
  10952. { //Not found.
  10953. script_pushint(st,0);
  10954. return 0;
  10955. }
  10956. if (flag)
  10957. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10958. else
  10959. nd->master_nd = NULL;
  10960. script_pushint(st,1);
  10961. return 0;
  10962. }
  10963. /*==========================================
  10964. * Returns some values of an item [Lupus]
  10965. * Price, Weight, etc...
  10966. setitemscript(itemID,"{new item bonus script}",[n]);
  10967. Where n:
  10968. 0 - script
  10969. 1 - Equip script
  10970. 2 - Unequip script
  10971. *------------------------------------------
  10972. */
  10973. BUILDIN_FUNC(setitemscript)
  10974. {
  10975. int item_id,n=0;
  10976. const char *script;
  10977. struct item_data *i_data;
  10978. struct script_code **dstscript;
  10979. item_id = script_getnum(st,2);
  10980. script = script_getstr(st,3);
  10981. if( script_hasdata(st,4) )
  10982. n=script_getnum(st,4);
  10983. i_data = itemdb_exists(item_id);
  10984. if (!i_data || script==NULL || script[0]!='{') {
  10985. script_pushint(st,0);
  10986. return 0;
  10987. }
  10988. switch (n) {
  10989. case 2:
  10990. dstscript = &i_data->unequip_script;
  10991. break;
  10992. case 1:
  10993. dstscript = &i_data->equip_script;
  10994. break;
  10995. default:
  10996. dstscript = &i_data->script;
  10997. break;
  10998. }
  10999. if(*dstscript)
  11000. script_free_code(*dstscript);
  11001. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11002. script_pushint(st,1);
  11003. return 0;
  11004. }
  11005. /* Work In Progress [Lupus]
  11006. BUILDIN_FUNC(addmonsterdrop)
  11007. {
  11008. int class_,item_id,chance;
  11009. class_=script_getnum(st,2);
  11010. item_id=script_getnum(st,3);
  11011. chance=script_getnum(st,4);
  11012. if(class_>1000 && item_id>500 && chance>0) {
  11013. script_pushint(st,1);
  11014. } else {
  11015. script_pushint(st,0);
  11016. }
  11017. }
  11018. BUILDIN_FUNC(delmonsterdrop)
  11019. {
  11020. int class_,item_id;
  11021. class_=script_getnum(st,2);
  11022. item_id=script_getnum(st,3);
  11023. if(class_>1000 && item_id>500) {
  11024. script_pushint(st,1);
  11025. } else {
  11026. script_pushint(st,0);
  11027. }
  11028. }
  11029. */
  11030. /*==========================================
  11031. * Returns some values of a monster [Lupus]
  11032. * Name, Level, race, size, etc...
  11033. getmonsterinfo(monsterID,queryIndex);
  11034. *------------------------------------------
  11035. */
  11036. BUILDIN_FUNC(getmonsterinfo)
  11037. {
  11038. struct mob_db *mob;
  11039. int mob_id;
  11040. mob_id = script_getnum(st,2);
  11041. if (!mobdb_checkid(mob_id)) {
  11042. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i", mob_id);
  11043. script_pushint(st,-1);
  11044. return -1;
  11045. }
  11046. mob = mob_db(mob_id);
  11047. switch ( script_getnum(st,3) ) {
  11048. case 0: //Name
  11049. script_pushconststr(st,mob->jname);
  11050. break;
  11051. case 1: //Lvl
  11052. script_pushint(st,mob->lv);
  11053. break;
  11054. case 2: //MaxHP
  11055. script_pushint(st,mob->status.max_hp);
  11056. break;
  11057. case 3: //Base EXP
  11058. script_pushint(st,mob->base_exp);
  11059. break;
  11060. case 4: //Job EXP
  11061. script_pushint(st,mob->job_exp);
  11062. break;
  11063. case 5: //Atk1
  11064. script_pushint(st,mob->status.rhw.atk);
  11065. break;
  11066. case 6: //Atk2
  11067. script_pushint(st,mob->status.rhw.atk2);
  11068. break;
  11069. case 7: //Def
  11070. script_pushint(st,mob->status.def);
  11071. break;
  11072. case 8: //Mdef
  11073. script_pushint(st,mob->status.mdef);
  11074. break;
  11075. case 9: //Str
  11076. script_pushint(st,mob->status.str);
  11077. break;
  11078. case 10: //Agi
  11079. script_pushint(st,mob->status.agi);
  11080. break;
  11081. case 11: //Vit
  11082. script_pushint(st,mob->status.vit);
  11083. break;
  11084. case 12: //Int
  11085. script_pushint(st,mob->status.int_);
  11086. break;
  11087. case 13: //Dex
  11088. script_pushint(st,mob->status.dex);
  11089. break;
  11090. case 14: //Luk
  11091. script_pushint(st,mob->status.luk);
  11092. break;
  11093. case 15: //Range
  11094. script_pushint(st,mob->status.rhw.range);
  11095. break;
  11096. case 16: //Range2
  11097. script_pushint(st,mob->range2);
  11098. break;
  11099. case 17: //Range3
  11100. script_pushint(st,mob->range3);
  11101. break;
  11102. case 18: //Size
  11103. script_pushint(st,mob->status.size);
  11104. break;
  11105. case 19: //Race
  11106. script_pushint(st,mob->status.race);
  11107. break;
  11108. case 20: //Element
  11109. script_pushint(st,mob->status.def_ele);
  11110. break;
  11111. case 21: //Mode
  11112. script_pushint(st,mob->status.mode);
  11113. break;
  11114. default: //wrong Index
  11115. script_pushint(st,-1);
  11116. }
  11117. return 0;
  11118. }
  11119. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11120. {
  11121. struct map_session_data *sd = NULL;
  11122. if(script_hasdata(st,2))
  11123. sd = map_nick2sd(script_getstr(st,2));
  11124. else
  11125. sd = script_rid2sd(st);
  11126. if(sd)
  11127. script_pushint(st,(sd->vender_id != 0));
  11128. else
  11129. script_pushint(st,0);
  11130. return 0;
  11131. }
  11132. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11133. {
  11134. struct map_session_data *sd = NULL;
  11135. if(script_hasdata(st,2))
  11136. sd = map_nick2sd(script_getstr(st,2));
  11137. else
  11138. sd = script_rid2sd(st);
  11139. if(sd)
  11140. script_pushint(st,(sd->chatID != 0));
  11141. else
  11142. script_pushint(st,0);
  11143. return 0;
  11144. }
  11145. int axtoi(const char *hexStg)
  11146. {
  11147. int n = 0; // position in string
  11148. int m = 0; // position in digit[] to shift
  11149. int count; // loop index
  11150. int intValue = 0; // integer value of hex string
  11151. int digit[11]; // hold values to convert
  11152. while (n < 10) {
  11153. if (hexStg[n]=='\0')
  11154. break;
  11155. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11156. digit[n] = hexStg[n] & 0x0f; //convert to int
  11157. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11158. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11159. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11160. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11161. else break;
  11162. n++;
  11163. }
  11164. count = n;
  11165. m = n - 1;
  11166. n = 0;
  11167. while(n < count) {
  11168. // digit[n] is value of hex digit at position n
  11169. // (m << 2) is the number of positions to shift
  11170. // OR the bits into return value
  11171. intValue = intValue | (digit[n] << (m << 2));
  11172. m--; // adjust the position to set
  11173. n++; // next digit to process
  11174. }
  11175. return (intValue);
  11176. }
  11177. // [Lance] Hex string to integer converter
  11178. BUILDIN_FUNC(axtoi)
  11179. {
  11180. const char *hex = script_getstr(st,2);
  11181. script_pushint(st,axtoi(hex));
  11182. return 0;
  11183. }
  11184. // [zBuffer] List of player cont commands --->
  11185. BUILDIN_FUNC(rid2name)
  11186. {
  11187. struct block_list *bl = NULL;
  11188. int rid = script_getnum(st,2);
  11189. if((bl = map_id2bl(rid))){
  11190. switch(bl->type){
  11191. case BL_MOB:
  11192. script_pushconststr(st,((TBL_MOB*)bl)->name);
  11193. break;
  11194. case BL_PC:
  11195. script_pushconststr(st,((TBL_PC*)bl)->status.name);
  11196. break;
  11197. case BL_NPC:
  11198. script_pushconststr(st,((TBL_NPC*)bl)->exname);
  11199. break;
  11200. case BL_PET:
  11201. script_pushconststr(st,((TBL_PET*)bl)->pet.name);
  11202. break;
  11203. case BL_HOM:
  11204. script_pushconststr(st,((TBL_HOM*)bl)->homunculus.name);
  11205. break;
  11206. default:
  11207. ShowError("buildin_rid2name: BL type unknown.\n");
  11208. script_pushconststr(st,"");
  11209. break;
  11210. }
  11211. } else {
  11212. ShowError("buildin_rid2name: invalid RID\n");
  11213. script_pushconststr(st,"(null)");
  11214. }
  11215. return 0;
  11216. }
  11217. BUILDIN_FUNC(pcblockmove)
  11218. {
  11219. int id, flag;
  11220. struct map_session_data *sd = NULL;
  11221. id = script_getnum(st,2);
  11222. flag = script_getnum(st,3);
  11223. if(id)
  11224. sd = map_id2sd(id);
  11225. else
  11226. sd = script_rid2sd(st);
  11227. if(sd)
  11228. sd->state.blockedmove = flag > 0;
  11229. return 0;
  11230. }
  11231. BUILDIN_FUNC(pcfollow)
  11232. {
  11233. int id, targetid;
  11234. struct map_session_data *sd = NULL;
  11235. id = script_getnum(st,2);
  11236. targetid = script_getnum(st,3);
  11237. if(id)
  11238. sd = map_id2sd(id);
  11239. else
  11240. sd = script_rid2sd(st);
  11241. if(sd)
  11242. pc_follow(sd, targetid);
  11243. return 0;
  11244. }
  11245. BUILDIN_FUNC(pcstopfollow)
  11246. {
  11247. int id;
  11248. struct map_session_data *sd = NULL;
  11249. id = script_getnum(st,2);
  11250. if(id)
  11251. sd = map_id2sd(id);
  11252. else
  11253. sd = script_rid2sd(st);
  11254. if(sd)
  11255. pc_stop_following(sd);
  11256. return 0;
  11257. }
  11258. // <--- [zBuffer] List of player cont commands
  11259. // [zBuffer] List of mob control commands --->
  11260. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11261. BUILDIN_FUNC(mobspawn)
  11262. {
  11263. int class_,x,y,id;
  11264. const char *str,*map;
  11265. // Who?
  11266. str =script_getstr(st,2);
  11267. // What?
  11268. class_ =script_getnum(st,3);
  11269. // Where?
  11270. map =script_getstr(st,4);
  11271. x =script_getnum(st,5);
  11272. y =script_getnum(st,6);
  11273. id = mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,1,"");
  11274. script_pushint(st,id);
  11275. return 0;
  11276. }
  11277. BUILDIN_FUNC(mobremove)
  11278. {
  11279. int id;
  11280. struct block_list *bl = NULL;
  11281. id = script_getnum(st,2);
  11282. bl = map_id2bl(id);
  11283. if (bl && bl->type == BL_MOB)
  11284. unit_free(bl,0);
  11285. return 0;
  11286. }
  11287. BUILDIN_FUNC(getmobdata)
  11288. {
  11289. int num, id;
  11290. char *name;
  11291. struct mob_data *md = NULL;
  11292. struct map_session_data *sd = st->rid?map_id2sd(st->rid):NULL;
  11293. id = script_getnum(st,2);
  11294. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  11295. ShowWarning("buildin_getmobdata: Error in argument!\n");
  11296. return -1;
  11297. }
  11298. num=st->stack->stack_data[st->start+3].u.num;
  11299. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  11300. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  11301. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  11302. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  11303. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  11304. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  11305. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  11306. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  11307. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  11308. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  11309. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  11310. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  11311. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  11312. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  11313. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  11314. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  11315. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  11316. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  11317. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  11318. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  11319. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  11320. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  11321. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  11322. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  11323. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  11324. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  11325. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  11326. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  11327. return 0;
  11328. }
  11329. /// Changes the data of a mob
  11330. ///
  11331. /// setmobdata <mob unit id>,<type>,<value>;
  11332. BUILDIN_FUNC(setmobdata)
  11333. {
  11334. struct block_list* mob_bl;
  11335. mob_bl = map_id2bl(script_getnum(st,2));
  11336. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11337. {
  11338. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11339. int type;
  11340. int value;
  11341. type = script_getnum(st,3);
  11342. value = script_getnum(st,4);
  11343. switch( type )
  11344. {
  11345. case 0:
  11346. md->class_ = (short)value;
  11347. break;
  11348. case 1:
  11349. md->level = (unsigned short)value;
  11350. break;
  11351. case 2:
  11352. md->status.hp = (unsigned int)value;
  11353. break;
  11354. case 3:
  11355. md->status.max_hp = (unsigned int)value;
  11356. break;
  11357. case 4:
  11358. md->master_id = value;
  11359. break;
  11360. case 5:
  11361. md->bl.m = (short)value;
  11362. break;
  11363. case 6:
  11364. md->bl.x = (short)value;
  11365. break;
  11366. case 7:
  11367. md->bl.y = (short)value;
  11368. break;
  11369. case 8:
  11370. md->status.speed = (unsigned short)value;
  11371. break;
  11372. case 9:
  11373. md->status.mode = (unsigned short)value;
  11374. break;
  11375. case 10:
  11376. md->special_state.ai = (unsigned int)value;
  11377. break;
  11378. case 11:
  11379. md->sc.option = (unsigned short)value;
  11380. break;
  11381. case 12:
  11382. md->vd->sex = (char)value;
  11383. break;
  11384. case 13:
  11385. md->vd->class_ = (unsigned short)value;
  11386. break;
  11387. case 14:
  11388. md->vd->hair_style = (unsigned short)value;
  11389. break;
  11390. case 15:
  11391. md->vd->hair_color = (unsigned short)value;
  11392. break;
  11393. case 16:
  11394. md->vd->head_bottom = (unsigned short)value;
  11395. break;
  11396. case 17:
  11397. md->vd->head_mid = (unsigned short)value;
  11398. break;
  11399. case 18:
  11400. md->vd->head_top = (unsigned short)value;
  11401. break;
  11402. case 19:
  11403. md->vd->cloth_color = (unsigned short)value;
  11404. break;
  11405. case 20:
  11406. md->vd->shield = (unsigned short)value;
  11407. break;
  11408. case 21:
  11409. md->vd->weapon = (unsigned short)value;
  11410. break;
  11411. case 22:
  11412. md->vd->shield = (unsigned short)value;
  11413. break;
  11414. case 23:
  11415. md->ud.dir = (unsigned char)value;
  11416. break;
  11417. case 24:
  11418. md->state.killer = value > 0 ? 1 : 0;
  11419. break;
  11420. case 25:
  11421. md->callback_flag = (short)value;
  11422. break;
  11423. case 26:
  11424. md->state.no_random_walk = value > 0 ? 1 : 0;
  11425. break;
  11426. default:
  11427. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  11428. return 1;
  11429. }
  11430. }
  11431. return 0;
  11432. }
  11433. /// Makes the mob assist the target unit as a slave
  11434. ///
  11435. /// mobassist <mob unit id>,"<player name>";
  11436. /// mobassist <mob unit id>,<target id>;
  11437. BUILDIN_FUNC(mobassist)
  11438. {
  11439. struct block_list* mob_bl;
  11440. // get mob
  11441. mob_bl = map_id2bl(script_getnum(st,2));
  11442. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11443. {
  11444. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11445. struct block_list* target_bl = NULL;
  11446. struct script_data* data;
  11447. // get target
  11448. data = script_getdata(st, 3);
  11449. get_val(st, data);
  11450. if( data_isstring(data) )
  11451. {
  11452. struct map_session_data* sd = map_nick2sd(conv_str(st, data));
  11453. if( sd != NULL )
  11454. target_bl = &sd->bl;
  11455. }
  11456. if( target_bl == NULL )
  11457. target_bl = map_id2bl(conv_num(st, data));
  11458. // set mob as slave
  11459. if( target_bl != NULL )
  11460. {
  11461. struct unit_data* ud;
  11462. md->master_id = target_bl->id;
  11463. md->state.killer = 1;
  11464. mob_convertslave(md);
  11465. ud = unit_bl2ud(mob_bl);
  11466. if( ud != NULL )
  11467. {
  11468. if( ud->target != 0 )
  11469. md->target_id = ud->target;
  11470. else if( ud->skilltarget != 0 )
  11471. md->target_id = ud->skilltarget;
  11472. if( md->target_id != 0 )
  11473. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  11474. }
  11475. }
  11476. }
  11477. return 0;
  11478. }
  11479. /// Attaches the current npc or the target npc to the mob unit
  11480. ///
  11481. /// mobattach <mob unit id>{,"<npc name>"};
  11482. BUILDIN_FUNC(mobattach)
  11483. {
  11484. struct block_list* mob_bl;
  11485. mob_bl = map_id2bl(script_getnum(st,2));
  11486. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11487. {
  11488. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11489. TBL_NPC* nd = NULL;
  11490. if( script_hasdata(st,3) )
  11491. nd = npc_name2id(script_getstr(st, 3));
  11492. else
  11493. {
  11494. struct block_list* npc_bl = map_id2bl(st->oid);
  11495. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  11496. nd = (TBL_NPC*)npc_bl;
  11497. }
  11498. if( nd != NULL )
  11499. md->nd = nd;
  11500. }
  11501. return 0;
  11502. }
  11503. /// Makes the unit walk to target position or map
  11504. /// Returns if it was successfull
  11505. ///
  11506. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11507. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11508. BUILDIN_FUNC(unitwalk)
  11509. {
  11510. struct block_list* bl;
  11511. bl = map_id2bl(script_getnum(st,2));
  11512. if( bl == NULL )
  11513. {
  11514. script_pushint(st, 0);
  11515. }
  11516. else if( script_hasdata(st,4) )
  11517. {
  11518. int x = script_getnum(st,3);
  11519. int y = script_getnum(st,4);
  11520. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11521. }
  11522. else
  11523. {
  11524. int map_id = script_getnum(st,3);
  11525. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11526. }
  11527. return 0;
  11528. }
  11529. /// Kills the unit
  11530. ///
  11531. /// unitkill <unit_id>;
  11532. BUILDIN_FUNC(unitkill)
  11533. {
  11534. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11535. if( bl != NULL )
  11536. status_kill(bl);
  11537. return 0;
  11538. }
  11539. /// Warps the unit to the target position in the target map
  11540. /// Returns if it was successfull
  11541. ///
  11542. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11543. BUILDIN_FUNC(unitwarp)
  11544. {
  11545. int unit_id;
  11546. int map;
  11547. short x;
  11548. short y;
  11549. struct block_list* bl;
  11550. unit_id = script_getnum(st,2);
  11551. map = map_mapname2mapid(script_getstr(st, 3));
  11552. x = (short)script_getnum(st,4);
  11553. y = (short)script_getnum(st,5);
  11554. bl = map_id2bl(unit_id);
  11555. if( map >= 0 && bl != NULL )
  11556. script_pushint(st, unit_warp(bl,map,x,y,0));
  11557. else
  11558. script_pushint(st, 0);
  11559. return 0;
  11560. }
  11561. /// Makes the unit attack the target.
  11562. /// If the unit is a player and <action type> is not 0, it does a continuous
  11563. /// attack instead of a single attack.
  11564. /// Returns if the request was successfull.
  11565. ///
  11566. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11567. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11568. BUILDIN_FUNC(unitattack)
  11569. {
  11570. struct block_list* unit_bl;
  11571. struct block_list* target_bl = NULL;
  11572. struct script_data* data;
  11573. int actiontype = 0;
  11574. // get unit
  11575. unit_bl = map_id2bl(script_getnum(st,2));
  11576. if( unit_bl == NULL ) {
  11577. script_pushint(st, 0);
  11578. return 0;
  11579. }
  11580. data = script_getdata(st, 3);
  11581. get_val(st, data);
  11582. if( data_isstring(data) )
  11583. {
  11584. struct map_session_data* sd = map_nick2sd(conv_str(st, data));
  11585. if( sd != NULL )
  11586. target_bl = &sd->bl;
  11587. } else
  11588. target_bl = map_id2bl(conv_num(st, data));
  11589. // request the attack
  11590. if( target_bl == NULL )
  11591. {
  11592. script_pushint(st, 0);
  11593. return 0;
  11594. }
  11595. // get actiontype
  11596. if( script_hasdata(st,4) )
  11597. actiontype = script_getnum(st,4);
  11598. switch( unit_bl->type )
  11599. {
  11600. case BL_PC:
  11601. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11602. script_pushint(st, 1);
  11603. return 0;
  11604. case BL_MOB:
  11605. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11606. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11607. break;
  11608. case BL_PET:
  11609. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11610. break;
  11611. default:
  11612. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11613. script_pushint(st, 0);
  11614. return 0;
  11615. }
  11616. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11617. return 0;
  11618. }
  11619. /// Makes the unit stop attacking and moving
  11620. ///
  11621. /// unitstop <unit_id>;
  11622. BUILDIN_FUNC(unitstop)
  11623. {
  11624. int unit_id;
  11625. struct block_list* bl;
  11626. unit_id = script_getnum(st,2);
  11627. bl = map_id2bl(unit_id);
  11628. if( bl != NULL )
  11629. {
  11630. unit_stop_attack(bl);
  11631. unit_stop_walking(bl,0);
  11632. if( bl->type == BL_MOB )
  11633. ((TBL_MOB*)bl)->target_id = 0;
  11634. }
  11635. return 0;
  11636. }
  11637. /// Makes the unit say the message
  11638. ///
  11639. /// unittalk <unit_id>,"<message>";
  11640. BUILDIN_FUNC(unittalk)
  11641. {
  11642. int unit_id;
  11643. const char* message;
  11644. struct block_list* bl;
  11645. unit_id = script_getnum(st,2);
  11646. message = script_getstr(st, 3);
  11647. bl = map_id2bl(unit_id);
  11648. if( bl != NULL )
  11649. {
  11650. struct StringBuf* buf = StringBuf_Malloc();
  11651. StringBuf_Printf(buf, "%s : %s", status_get_name(bl), message);
  11652. clif_message(bl, StringBuf_Value(buf));
  11653. if( bl->type == BL_PC )
  11654. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(buf));
  11655. StringBuf_Free(buf);
  11656. }
  11657. return 0;
  11658. }
  11659. /// Makes the unit do an emotion
  11660. ///
  11661. /// unitemote <unit_id>,<emotion>;
  11662. ///
  11663. /// @see e_* in const.txt
  11664. BUILDIN_FUNC(unitemote)
  11665. {
  11666. int unit_id;
  11667. int emotion;
  11668. struct block_list* bl;
  11669. unit_id = script_getnum(st,2);
  11670. emotion = script_getnum(st,3);
  11671. bl = map_id2bl(unit_id);
  11672. if( bl != NULL )
  11673. clif_emotion(bl, emotion);
  11674. return 0;
  11675. }
  11676. /// Makes the unit do an action
  11677. /// TODO actions
  11678. ///
  11679. /// unitdeadsit <unit_id>,<action>;
  11680. BUILDIN_FUNC(unitdeadsit)
  11681. {
  11682. int unit_id;
  11683. int action;
  11684. struct block_list* bl;
  11685. unit_id = script_getnum(st,2);
  11686. action = script_getnum(st,3);
  11687. bl = map_id2bl(unit_id);
  11688. if( bl != NULL )
  11689. {
  11690. if( action > -1 && action < 4 )
  11691. {
  11692. unsigned char buf[61] = "";
  11693. struct view_data *vd = status_get_viewdata(bl);
  11694. if (vd) vd->dead_sit = action;
  11695. WBUFW(buf, 0) = 0x8a;
  11696. WBUFL(buf, 2) = bl->id;
  11697. WBUFB(buf,26) = (unsigned char)action;
  11698. clif_send(buf, 61, bl, AREA);
  11699. } else
  11700. {
  11701. ShowWarning("script:unitdeadsit: %d is not a valid action\n", action);
  11702. report_src(st);
  11703. }
  11704. }
  11705. return 0;
  11706. }
  11707. /// Makes the unit cast the skill on the target or self if no target is specified
  11708. ///
  11709. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11710. BUILDIN_FUNC(unitskilluseid)
  11711. {
  11712. int unit_id;
  11713. int skill_id;
  11714. int skill_lv;
  11715. int target_id;
  11716. struct block_list* bl;
  11717. unit_id = script_getnum(st,2);
  11718. skill_id = script_getnum(st,3);
  11719. skill_lv = script_getnum(st,4);
  11720. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11721. bl = map_id2bl(unit_id);
  11722. if( bl != NULL )
  11723. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11724. return 0;
  11725. }
  11726. /// Makes the unit cast the skill on the target position.
  11727. ///
  11728. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11729. BUILDIN_FUNC(unitskillusepos)
  11730. {
  11731. int unit_id;
  11732. int skill_id;
  11733. int skill_lv;
  11734. int skill_x;
  11735. int skill_y;
  11736. struct block_list* bl;
  11737. unit_id = script_getnum(st,2);
  11738. skill_id = script_getnum(st,3);
  11739. skill_lv = script_getnum(st,4);
  11740. skill_x = script_getnum(st,5);
  11741. skill_y = script_getnum(st,6);
  11742. bl = map_id2bl(unit_id);
  11743. if( bl != NULL )
  11744. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11745. return 0;
  11746. }
  11747. // <--- [zBuffer] List of mob control commands
  11748. /// Pauses the execution of the script, detaching the player
  11749. ///
  11750. /// sleep <mili seconds>;
  11751. BUILDIN_FUNC(sleep)
  11752. {
  11753. int ticks;
  11754. TBL_PC* sd;
  11755. ticks = script_getnum(st,2);
  11756. sd = map_id2sd(st->rid);
  11757. // detach the player
  11758. if( sd && sd->npc_id == st->oid )
  11759. {
  11760. sd->npc_id = 0;
  11761. }
  11762. st->rid = 0;
  11763. if( ticks <= 0 )
  11764. {// do nothing
  11765. }
  11766. else if( st->sleep.tick == 0 )
  11767. {// sleep for the target amount of time
  11768. st->state = RERUNLINE;
  11769. st->sleep.tick = ticks;
  11770. }
  11771. else
  11772. {// sleep time is over
  11773. st->sleep.tick = 0;
  11774. }
  11775. return 0;
  11776. }
  11777. /// Pauses the execution of the script, keeping the player attached
  11778. /// Returns if a player is still attached
  11779. ///
  11780. /// sleep2(<mili secconds>) -> <bool>
  11781. BUILDIN_FUNC(sleep2)
  11782. {
  11783. int ticks;
  11784. ticks = script_getnum(st,2);
  11785. if( ticks <= 0 )
  11786. {// do nothing
  11787. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11788. }
  11789. else if( !st->sleep.tick )
  11790. {// sleep for the target amount of time
  11791. st->state = RERUNLINE;
  11792. st->sleep.tick = ticks;
  11793. }
  11794. else
  11795. {// sleep time is over
  11796. st->sleep.tick = 0;
  11797. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11798. }
  11799. return 0;
  11800. }
  11801. /// Awakes all the sleep timers of the target npc
  11802. ///
  11803. /// awake "<npc name>";
  11804. BUILDIN_FUNC(awake)
  11805. {
  11806. struct npc_data* nd;
  11807. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11808. nd = npc_name2id(script_getstr(st, 2));
  11809. if( nd == NULL )
  11810. return 0;
  11811. while( node )
  11812. {
  11813. if( (int)node->key == nd->bl.id )
  11814. {// sleep timer for the npc
  11815. struct script_state* tst = (struct script_state*)node->data;
  11816. TBL_PC* sd = map_id2sd(tst->rid);
  11817. if( tst->sleep.timer == -1 )
  11818. {// already awake ???
  11819. node = node->next;
  11820. continue;
  11821. }
  11822. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11823. {// char not online anymore / another char of the same account is online - Cancel execution
  11824. tst->state = END;
  11825. tst->rid = 0;
  11826. }
  11827. delete_timer(tst->sleep.timer, run_script_timer);
  11828. node = script_erase_sleepdb(node);
  11829. tst->sleep.timer = -1;
  11830. tst->sleep.tick = 0;
  11831. run_script_main(tst);
  11832. }
  11833. else
  11834. {
  11835. node = node->next;
  11836. }
  11837. }
  11838. return 0;
  11839. }
  11840. /// Returns a reference to a variable of the target NPC.
  11841. /// Returns 0 if an error occurs.
  11842. ///
  11843. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11844. BUILDIN_FUNC(getvariableofnpc)
  11845. {
  11846. struct script_data* data;
  11847. data = script_getdata(st,2);
  11848. if( !data_isreference(data) )
  11849. {// Not a reference (aka varaible name)
  11850. ShowError("script: getvariableofnpc: first argument is not a variable name\n");
  11851. st->state = END;
  11852. return 1;
  11853. }
  11854. else
  11855. {
  11856. int num = data->u.num;
  11857. char* var_name = str_buf + str_data[num&0x00ffffff].str;
  11858. const char* npc_name = script_getstr(st, 3);
  11859. struct npc_data* nd = npc_name2id(npc_name);
  11860. if( var_name[0] != '.' || var_name[1] == '@' )
  11861. {// not a npc variable
  11862. ShowError("script: getvariableofnpc: invalid scope %s (not npc variable)\n", var_name);
  11863. st->state = END;
  11864. return 1;
  11865. }
  11866. if( nd == NULL || nd->bl.subtype != SCRIPT || nd->u.scr.script == NULL )
  11867. {// NPC not found or has no script
  11868. ShowError("script: getvariableofnpc: can't find npc %s\n", npc_name);
  11869. st->state = END;
  11870. return 1;
  11871. }
  11872. push_val2(st->stack, C_NAME, num, &nd->u.scr.script->script_vars );
  11873. }
  11874. return 0;
  11875. }
  11876. /// Opens a warp portal.
  11877. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11878. ///
  11879. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11880. ///
  11881. /// @author blackhole89
  11882. BUILDIN_FUNC(warpportal)
  11883. {
  11884. int spx;
  11885. int spy;
  11886. unsigned short mapindex;
  11887. int tpx;
  11888. int tpy;
  11889. struct skill_unit_group* group;
  11890. struct block_list* bl;
  11891. bl = map_id2bl(st->oid);
  11892. if( bl == NULL )
  11893. {
  11894. ShowError("script:warpportal: npc is needed");
  11895. return 1;
  11896. }
  11897. spx = script_getnum(st,2);
  11898. spy = script_getnum(st,3);
  11899. mapindex = mapindex_name2id(script_getstr(st, 4));
  11900. tpx = script_getnum(st,5);
  11901. tpy = script_getnum(st,6);
  11902. if( mapindex == 0 )
  11903. return 0;// map not found
  11904. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 1);
  11905. if( group == NULL )
  11906. return 0;// failed
  11907. group->val2 = (tpx<<16) | tpy;
  11908. group->val3 = mapindex;
  11909. return 0;
  11910. }