script.c 349 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903
  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 "script.h"
  23. #include "storage.h"
  24. #include "mob.h"
  25. #include "npc.h"
  26. #include "pet.h"
  27. #include "mercenary.h" //[orn]
  28. #include "intif.h"
  29. #include "skill.h"
  30. #include "chat.h"
  31. #include "battle.h"
  32. #include "party.h"
  33. #include "guild.h"
  34. #include "atcommand.h"
  35. #include "charcommand.h"
  36. #include "log.h"
  37. #include "unit.h"
  38. #include "irc.h"
  39. #include "pet.h"
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <math.h>
  45. #ifndef WIN32
  46. #include <sys/time.h>
  47. #endif
  48. #include <time.h>
  49. #include <setjmp.h>
  50. ///////////////////////////////////////////////////////////////////////////////
  51. /// Returns the stack_data at the target index
  52. #define script_getdata(st,i) &((st)->stack->stack_data[(st)->start+(i)])
  53. /// Returns if the stack contains data at the target index
  54. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  55. /// Returns the index of the last data in the stack
  56. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  57. /// Pushes an int into the stack
  58. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  59. /// Returns if the stack data is a string
  60. #define script_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  61. /// Returns if the stack data is an int
  62. #define script_isint(data) ( (data)->type == C_INT )
  63. #define FETCH(n, t) \
  64. if( script_hasdata(st,n) ) \
  65. (t)=conv_num(st,script_getdata(st,n));
  66. #define SCRIPT_BLOCK_SIZE 512
  67. enum { LABEL_NEXTLINE=1,LABEL_START };
  68. static unsigned char * script_buf = NULL;
  69. static int script_pos,script_size;
  70. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  71. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  72. #define GETSTRING(off) (str_buf+(off))
  73. static char *str_buf;
  74. static int str_pos,str_size;
  75. static struct str_data_struct {
  76. int type;
  77. int str;
  78. int backpatch;
  79. int label;
  80. int (*func)(struct script_state *st);
  81. int val;
  82. int next;
  83. } *str_data = NULL;
  84. int str_num=LABEL_START,str_data_size;
  85. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  86. #define SCRIPT_HASH_SIZE 1021
  87. int str_hash[SCRIPT_HASH_SIZE];
  88. //#define SCRIPT_HASH_DJB2
  89. //#define SCRIPT_HASH_SDBM
  90. //#define SCRIPT_HASH_ELF
  91. //#define SCRIPT_HASH_PJW
  92. static struct dbt *mapreg_db=NULL;
  93. static struct dbt *mapregstr_db=NULL;
  94. static int mapreg_dirty=-1;
  95. char mapreg_txt[256]="save/mapreg.txt";
  96. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  97. static struct dbt *scriptlabel_db=NULL;
  98. static struct dbt *userfunc_db=NULL;
  99. static int parse_options=0;
  100. struct dbt* script_get_label_db(){ return scriptlabel_db; }
  101. struct dbt* script_get_userfunc_db(){ return userfunc_db; }
  102. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  103. struct Script_Config script_config;
  104. static jmp_buf error_jump;
  105. static char* error_msg;
  106. static const char* error_pos;
  107. static int error_report; // if the error should produce output
  108. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  109. // [Eoe / jA 1080, 1081, 1094, 1164]
  110. enum curly_type {
  111. TYPE_NULL = 0,
  112. TYPE_IF,
  113. TYPE_SWITCH,
  114. TYPE_WHILE,
  115. TYPE_FOR,
  116. TYPE_DO,
  117. TYPE_USERFUNC,
  118. TYPE_ARGLIST // function argument list
  119. };
  120. #define ARGLIST_UNDEFINED 0
  121. #define ARGLIST_NO_PAREN 1
  122. #define ARGLIST_PAREN 2
  123. static struct {
  124. struct {
  125. int type;
  126. int index;
  127. int count;
  128. int flag;
  129. struct linkdb_node *case_label;
  130. } curly[256]; // 右カッコの情報
  131. int curly_count; // 右カッコの数
  132. int index; // スクリプト内で使用した構文の数
  133. } syntax;
  134. const char* parse_curly_close(const char* p);
  135. const char* parse_syntax_close(const char* p);
  136. const char* parse_syntax_close_sub(const char* p,int* flag);
  137. const char* parse_syntax(const char* p);
  138. static int parse_syntax_for_flag = 0;
  139. extern int current_equip_item_index; //for New CARS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  140. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  141. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  142. int potion_target=0;
  143. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  144. // [zBuffer] SQL Mapreg Saving/Loading Database Declaration
  145. char mapregsql_db[32] = "mapreg";
  146. char mapregsql_db_varname[32] = "varname";
  147. char mapregsql_db_index[32] = "index";
  148. char mapregsql_db_value[32] = "value";
  149. char tmp_sql[65535];
  150. // --------------------------------------------------------
  151. #endif
  152. int get_com(unsigned char *script,int *pos);
  153. int get_num(unsigned char *script,int *pos);
  154. extern struct script_function {
  155. int (*func)(struct script_state *st);
  156. const char *name;
  157. const char *arg;
  158. } buildin_func[];
  159. static struct linkdb_node *sleep_db;
  160. #define not_server_variable(prefix) (prefix != '$' && prefix != '.')
  161. /*==========================================
  162. * ローカルプロトタイプ宣言 (必要な物のみ)
  163. *------------------------------------------
  164. */
  165. const char* parse_subexpr(const char* p,int limit);
  166. void push_val(struct script_stack *stack,int type,int val);
  167. int run_func(struct script_state *st);
  168. int mapreg_setreg(int num,int val);
  169. int mapreg_setregstr(int num,const char *str);
  170. enum c_op {
  171. C_NOP,C_POS,C_INT,C_PARAM,C_FUNC,C_STR,C_CONSTSTR,C_ARG,
  172. C_NAME,C_EOL, C_RETINFO,
  173. C_USERFUNC, C_USERFUNC_POS, // user defined functions
  174. C_OP3,C_LOR,C_LAND,C_LE,C_LT,C_GE,C_GT,C_EQ,C_NE, //operator
  175. C_XOR,C_OR,C_AND,C_ADD,C_SUB,C_MUL,C_DIV,C_MOD,C_NEG,C_LNOT,C_NOT,C_R_SHIFT,C_L_SHIFT
  176. };
  177. enum {
  178. MF_NOMEMO, //0
  179. MF_NOTELEPORT,
  180. MF_NOSAVE,
  181. MF_NOBRANCH,
  182. MF_NOPENALTY,
  183. MF_NOZENYPENALTY,
  184. MF_PVP,
  185. MF_PVP_NOPARTY,
  186. MF_PVP_NOGUILD,
  187. MF_GVG,
  188. MF_GVG_NOPARTY, //10
  189. MF_NOTRADE,
  190. MF_NOSKILL,
  191. MF_NOWARP,
  192. MF_PARTYLOCK,
  193. MF_NOICEWALL,
  194. MF_SNOW,
  195. MF_FOG,
  196. MF_SAKURA,
  197. MF_LEAVES,
  198. MF_RAIN, //20
  199. MF_INDOORS,
  200. MF_NOGO,
  201. MF_CLOUDS,
  202. MF_CLOUDS2,
  203. MF_FIREWORKS,
  204. MF_GVG_CASTLE,
  205. MF_GVG_DUNGEON,
  206. MF_NIGHTENABLED,
  207. MF_NOBASEEXP,
  208. MF_NOJOBEXP, //30
  209. MF_NOMOBLOOT,
  210. MF_NOMVPLOOT,
  211. MF_NORETURN,
  212. MF_NOWARPTO,
  213. MF_NIGHTMAREDROP,
  214. MF_RESTRICTED,
  215. MF_NOCOMMAND,
  216. MF_NODROP,
  217. MF_JEXP,
  218. MF_BEXP, //40
  219. MF_NOVENDING,
  220. MF_LOADEVENT,
  221. MF_NOCHAT,
  222. MF_NOEXPPENALTY,
  223. MF_GUILDLOCK
  224. };
  225. //Reports on the console the src of an script error.
  226. static void report_src(struct script_state *st) {
  227. struct block_list *bl;
  228. if (!st->oid) return; //Can't report source.
  229. bl = map_id2bl(st->oid);
  230. if (!bl) return;
  231. switch (bl->type) {
  232. case BL_NPC:
  233. if (bl->m >=0)
  234. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  235. else
  236. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  237. break;
  238. default:
  239. if (bl->m >=0)
  240. 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);
  241. else
  242. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  243. break;
  244. }
  245. }
  246. /*==========================================
  247. * エラーメッセージ出力
  248. *------------------------------------------
  249. */
  250. static void disp_error_message2(const char *mes,const char *pos,int report)
  251. {
  252. error_msg = aStrdup(mes);
  253. error_pos = pos;
  254. error_report = report;
  255. longjmp( error_jump, 1 );
  256. }
  257. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  258. /// Checks event parameter validity
  259. static void check_event(struct script_state *st, const char *evt)
  260. {
  261. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  262. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  263. report_src(st);
  264. }
  265. }
  266. /*==========================================
  267. * 文字列のハッシュを計算
  268. *------------------------------------------
  269. */
  270. #define calc_hash(x) calc_hash2(x)%SCRIPT_HASH_SIZE
  271. static unsigned int calc_hash2(const unsigned char *p)
  272. {
  273. #if defined(SCRIPT_HASH_DJB2)
  274. unsigned int h = 5381;
  275. while( *p ) // hash*33 + c
  276. h = ( h << 5 ) + h + ((unsigned char)tolower(*p++));
  277. return h;
  278. #elif defined(SCRIPT_HASH_SDBM)
  279. unsigned int h = 0;
  280. while( *p )
  281. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)tolower(*p++));
  282. return h;
  283. #elif defined(SCRIPT_HASH_ELF)
  284. unsigned int h = 0;
  285. unsigned int g;
  286. while( *p ){ // UNIX ELF hash
  287. h = ( h << 4 ) + ((unsigned char)tolower(*p++));
  288. if ( g = h & 0xF0000000 )
  289. h ^= g >> 24;
  290. h &= ~g;
  291. }
  292. return h;
  293. #elif defined(SCRIPT_HASH_PJW)
  294. unsigned int h = 0;
  295. unsigned int g;
  296. while( *p ){
  297. h = ( h << 4 ) + ((unsigned char)tolower(*p++));
  298. if ( (g=h&0xF0000000) ) {
  299. h ^= g>>24;
  300. h ^= g;
  301. }
  302. }
  303. return h;
  304. #else
  305. unsigned int h = 0;
  306. while( *p ){
  307. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  308. h+=(unsigned char)tolower(*p++);
  309. }
  310. return h;
  311. #endif
  312. }
  313. /*==========================================
  314. * str_dataの中に名前があるか検索する
  315. *------------------------------------------
  316. */
  317. // 既存のであれば番号、無ければ-1
  318. static int search_str(const char *p)
  319. {
  320. int i;
  321. i=str_hash[calc_hash(p)];
  322. while(i){
  323. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  324. return i;
  325. }
  326. i=str_data[i].next;
  327. }
  328. return -1;
  329. }
  330. /*==========================================
  331. * str_dataに名前を登録
  332. *------------------------------------------
  333. */
  334. // 既存のであれば番号、無ければ登録して新規番号
  335. int add_str(const char* p)
  336. {
  337. int i;
  338. int len;
  339. i=calc_hash(p);
  340. if(str_hash[i]==0){
  341. str_hash[i]=str_num;
  342. } else {
  343. i=str_hash[i];
  344. for(;;){
  345. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  346. return i;
  347. }
  348. if(str_data[i].next==0)
  349. break;
  350. i=str_data[i].next;
  351. }
  352. str_data[i].next=str_num;
  353. }
  354. if(str_num>=str_data_size){
  355. str_data_size+=128;
  356. RECREATE(str_data,struct str_data_struct,str_data_size);
  357. memset(str_data + (str_data_size - 128), '\0', 128);
  358. }
  359. len=(int)strlen(p);
  360. while(str_pos+len+1>=str_size){
  361. str_size+=256;
  362. RECREATE(str_buf,char,str_size);
  363. memset(str_buf + (str_size - 256), '\0', 256);
  364. }
  365. memcpy(str_buf+str_pos,p,len+1);
  366. str_data[str_num].type=C_NOP;
  367. str_data[str_num].str=str_pos;
  368. str_data[str_num].next=0;
  369. str_data[str_num].func=NULL;
  370. str_data[str_num].backpatch=-1;
  371. str_data[str_num].label=-1;
  372. str_pos+=len+1;
  373. return str_num++;
  374. }
  375. /*==========================================
  376. * スクリプトバッファサイズの確認と拡張
  377. *------------------------------------------
  378. */
  379. static void expand_script_buf(void)
  380. {
  381. script_size+=SCRIPT_BLOCK_SIZE;
  382. RECREATE(script_buf,unsigned char,script_size);
  383. }
  384. /*==========================================
  385. * スクリプトバッファに1バイト書き込む
  386. *------------------------------------------
  387. */
  388. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  389. #if 0
  390. static void add_scriptb(int a)
  391. {
  392. expand_script_buf();
  393. script_buf[script_pos++]=a;
  394. }
  395. #endif
  396. /*==========================================
  397. * スクリプトバッファにデータタイプを書き込む
  398. *------------------------------------------
  399. */
  400. static void add_scriptc(int a)
  401. {
  402. while(a>=0x40){
  403. add_scriptb((a&0x3f)|0x40);
  404. a=(a-0x40)>>6;
  405. }
  406. add_scriptb(a&0x3f);
  407. }
  408. /*==========================================
  409. * スクリプトバッファに整数を書き込む
  410. *------------------------------------------
  411. */
  412. static void add_scripti(int a)
  413. {
  414. while(a>=0x40){
  415. add_scriptb(a|0xc0);
  416. a=(a-0x40)>>6;
  417. }
  418. add_scriptb(a|0x80);
  419. }
  420. /*==========================================
  421. * スクリプトバッファにラベル/変数/関数を書き込む
  422. *------------------------------------------
  423. */
  424. // 最大16Mまで
  425. static void add_scriptl(int l)
  426. {
  427. int backpatch = str_data[l].backpatch;
  428. switch(str_data[l].type){
  429. case C_POS:
  430. case C_USERFUNC_POS:
  431. add_scriptc(C_POS);
  432. add_scriptb(str_data[l].label);
  433. add_scriptb(str_data[l].label>>8);
  434. add_scriptb(str_data[l].label>>16);
  435. break;
  436. case C_NOP:
  437. case C_USERFUNC:
  438. // ラベルの可能性があるのでbackpatch用データ埋め込み
  439. add_scriptc(C_NAME);
  440. str_data[l].backpatch=script_pos;
  441. add_scriptb(backpatch);
  442. add_scriptb(backpatch>>8);
  443. add_scriptb(backpatch>>16);
  444. break;
  445. case C_INT:
  446. add_scripti(abs(str_data[l].val));
  447. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  448. add_scriptc(C_NEG);
  449. break;
  450. default:
  451. // もう他の用途と確定してるので数字をそのまま
  452. add_scriptc(C_NAME);
  453. add_scriptb(l);
  454. add_scriptb(l>>8);
  455. add_scriptb(l>>16);
  456. break;
  457. }
  458. }
  459. /*==========================================
  460. * ラベルを解決する
  461. *------------------------------------------
  462. */
  463. void set_label(int l,int pos, const char* script_pos)
  464. {
  465. int i,next;
  466. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  467. { //Prevent overwriting constants values and parameters [Skotlex]
  468. disp_error_message("set_label: invalid label name",script_pos);
  469. return;
  470. }
  471. if(str_data[l].label!=-1){
  472. disp_error_message("set_label: dup label ",script_pos);
  473. return;
  474. }
  475. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  476. str_data[l].label=pos;
  477. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  478. next=GETVALUE(script_buf,i);
  479. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  480. SETVALUE(script_buf,i,pos);
  481. i=next;
  482. }
  483. }
  484. /*==========================================
  485. * スペース/コメント読み飛ばし
  486. *------------------------------------------
  487. */
  488. static const char *skip_space(const char *p)
  489. {
  490. for(;;){
  491. while(ISSPACE(*p))
  492. ++p;
  493. if( *p=='/' && p[1]=='/' ){
  494. while(*p && *p!='\n')
  495. ++p;
  496. } else if( *p=='/' && p[1]=='*' ){
  497. p+=2;
  498. if(*p) ++p;
  499. while( *p && (p[-1]!='*' || *p!='/') )
  500. p++;
  501. if(*p)
  502. ++p;
  503. else
  504. disp_error_message("skip_space: unexpected eof @ block comment",p);
  505. } else
  506. break;
  507. }
  508. return p;
  509. }
  510. /*==========================================
  511. * 1単語スキップ
  512. *------------------------------------------
  513. */
  514. static const char *skip_word(const char *p)
  515. {
  516. // prefix
  517. if(*p=='.') p++;
  518. if(*p=='$') p++; // MAP鯖内共有変数用
  519. if(*p=='@') p++; // 一時的変数用(like weiss)
  520. if(*p=='#') p++; // account変数用
  521. if(*p=='#') p++; // ワールドaccount変数用
  522. //# Changing from unsigned char to signed char makes p never be able to go above 0x81, but what IS 0x81 for? [Skotlex]
  523. //# It's for multibyte encodings like Shift-JIS. Unfortunately this can be problematic for singlebyte encodings.
  524. // Using (*p)>>7 would yield the appropriate result but it's better to restrict words to ASCII characters only. [FlavioJS]
  525. while( ISALNUM(*p) || *p == '_' )
  526. ++p;
  527. // postfix
  528. if(*p=='$') p++; // 文字列変数
  529. return p;
  530. }
  531. /// Adds a word to str_data
  532. int add_word(const char *p)
  533. {
  534. char *word;
  535. int len;
  536. int i;
  537. // Check for a word
  538. len = skip_word(p)-p;
  539. if( len == 0 )
  540. disp_error_message("add_word: not a word",p);
  541. // Copy the word
  542. CREATE(word,char,len+1);
  543. memcpy(word,p,len);
  544. word[len]=0;
  545. // add the word
  546. i=add_str(word);
  547. aFree(word);
  548. return i;
  549. }
  550. /// Parses a function call.
  551. /// The argument list can have parenthesis or not.
  552. /// The number of arguments is checked.
  553. static const char* parse_callfunc(const char *p, int require_paren)
  554. {
  555. const char* p2;
  556. const char* arg=NULL;
  557. int func;
  558. func = add_word(p);
  559. if( str_data[func].type == C_FUNC ){
  560. // buildin function
  561. add_scriptl(func);
  562. add_scriptc(C_ARG);
  563. arg = buildin_func[str_data[func].val].arg;
  564. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  565. // script defined function
  566. int callsub = search_str("callsub");
  567. add_scriptl(callsub);
  568. add_scriptc(C_ARG);
  569. add_scriptl(func);
  570. arg = buildin_func[str_data[callsub].val].arg;
  571. if( *arg == 0 )
  572. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  573. if( *arg != '*' )
  574. ++arg; // count func as argument
  575. } else
  576. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  577. p = skip_word(p);
  578. p = skip_space(p);
  579. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  580. syntax.curly[syntax.curly_count].count = 0;
  581. if( *p == ';' )
  582. {// <func name> ';'
  583. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  584. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  585. {// <func name> '(' ')'
  586. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  587. p = p2;
  588. /*
  589. } else if( 0 && require_paren && *p != '(' )
  590. {// <func name>
  591. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  592. */
  593. } else
  594. {// <func name> <arg list>
  595. if( require_paren ){
  596. if( *p != '(' )
  597. disp_error_message("need '('",p);
  598. ++p; // skip '('
  599. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  600. } else if( *p == '(' ){
  601. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  602. } else {
  603. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  604. }
  605. ++syntax.curly_count;
  606. while( *arg ) {
  607. p2=parse_subexpr(p,-1);
  608. if( p == p2 )
  609. break; // not an argument
  610. if( *arg != '*' )
  611. ++arg; // next argument
  612. p=skip_space(p2);
  613. if( *arg == 0 || *p != ',' )
  614. break; // no more arguments
  615. ++p; // skip comma
  616. }
  617. --syntax.curly_count;
  618. }
  619. if( *arg && *arg != '?' && *arg != '*' )
  620. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  621. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  622. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  623. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  624. if( *p != ')' )
  625. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  626. ++p;
  627. }
  628. add_scriptc(C_FUNC);
  629. return p;
  630. }
  631. /*==========================================
  632. * 項の解析
  633. *------------------------------------------
  634. */
  635. const char* parse_simpleexpr(const char *p)
  636. {
  637. int i;
  638. p=skip_space(p);
  639. #ifdef DEBUG_FUNCIN
  640. if(battle_config.etc_log)
  641. ShowDebug("parse_simpleexpr %s\n",p);
  642. #endif
  643. if(*p==';' || *p==',')
  644. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  645. if(*p=='('){
  646. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  647. ++syntax.curly[i].count;
  648. p=parse_subexpr(p+1,-1);
  649. p=skip_space(p);
  650. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  651. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  652. ){
  653. if( *p == ',' ){
  654. syntax.curly[i].flag = ARGLIST_PAREN;
  655. return p;
  656. } else
  657. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  658. }
  659. if( *p != ')' )
  660. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  661. ++p;
  662. } else if(isdigit(*p) || ((*p=='-' || *p=='+') && isdigit(p[1]))){
  663. char *np;
  664. i=strtoul(p,&np,0);
  665. add_scripti(i);
  666. p=np;
  667. } else if(*p=='"'){
  668. add_scriptc(C_STR);
  669. p++;
  670. while( *p && *p != '"' ){
  671. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  672. p++;
  673. else if( *p == '\n' )
  674. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  675. add_scriptb(*p++);
  676. }
  677. if(!*p)
  678. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  679. add_scriptb(0);
  680. p++; //'"'
  681. } else {
  682. int l;
  683. // label , register , function etc
  684. if(skip_word(p)==p)
  685. disp_error_message("parse_simpleexpr: unexpected character",p);
  686. l=add_word(p);
  687. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  688. return parse_callfunc(p,1);
  689. p=skip_word(p);
  690. if( *p == '[' ){
  691. // array(name[i] => getelementofarray(name,i) )
  692. add_scriptl(search_str("getelementofarray"));
  693. add_scriptc(C_ARG);
  694. add_scriptl(l);
  695. p=parse_subexpr(p+1,-1);
  696. p=skip_space(p);
  697. if( *p != ']' )
  698. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  699. ++p;
  700. add_scriptc(C_FUNC);
  701. }else
  702. add_scriptl(l);
  703. }
  704. #ifdef DEBUG_FUNCIN
  705. if(battle_config.etc_log)
  706. ShowDebug("parse_simpleexpr end %s\n",p);
  707. #endif
  708. return p;
  709. }
  710. /*==========================================
  711. * 式の解析
  712. *------------------------------------------
  713. */
  714. const char* parse_subexpr(const char* p,int limit)
  715. {
  716. int op,opl,len;
  717. const char* tmpp;
  718. #ifdef DEBUG_FUNCIN
  719. if(battle_config.etc_log)
  720. ShowDebug("parse_subexpr %s\n",p);
  721. #endif
  722. p=skip_space(p);
  723. if(*p=='-'){
  724. tmpp=skip_space(p+1);
  725. if(*tmpp==';' || *tmpp==','){
  726. add_scriptl(LABEL_NEXTLINE);
  727. p++;
  728. return p;
  729. }
  730. }
  731. tmpp=p;
  732. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  733. p=parse_subexpr(p+1,10);
  734. add_scriptc(op);
  735. } else
  736. p=parse_simpleexpr(p);
  737. p=skip_space(p);
  738. while((
  739. (op=C_OP3,opl=0,len=1,*p=='?') ||
  740. (op=C_ADD,opl=8,len=1,*p=='+') ||
  741. (op=C_SUB,opl=8,len=1,*p=='-') ||
  742. (op=C_MUL,opl=9,len=1,*p=='*') ||
  743. (op=C_DIV,opl=9,len=1,*p=='/') ||
  744. (op=C_MOD,opl=9,len=1,*p=='%') ||
  745. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  746. (op=C_AND,opl=6,len=1,*p=='&') ||
  747. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  748. (op=C_OR,opl=5,len=1,*p=='|') ||
  749. (op=C_XOR,opl=4,len=1,*p=='^') ||
  750. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  751. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  752. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  753. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  754. (op=C_GT,opl=3,len=1,*p=='>') ||
  755. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  756. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  757. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  758. p+=len;
  759. if(op == C_OP3) {
  760. p=parse_subexpr(p,-1);
  761. p=skip_space(p);
  762. if( *(p++) != ':')
  763. disp_error_message("parse_subexpr: need ':'", p-1);
  764. p=parse_subexpr(p,-1);
  765. } else {
  766. p=parse_subexpr(p,opl);
  767. }
  768. add_scriptc(op);
  769. p=skip_space(p);
  770. }
  771. #ifdef DEBUG_FUNCIN
  772. if(battle_config.etc_log)
  773. ShowDebug("parse_subexpr end %s\n",p);
  774. #endif
  775. return p; /* return first untreated operator */
  776. }
  777. /*==========================================
  778. * 式の評価
  779. *------------------------------------------
  780. */
  781. const char* parse_expr(const char *p)
  782. {
  783. #ifdef DEBUG_FUNCIN
  784. if(battle_config.etc_log)
  785. ShowDebug("parse_expr %s\n",p);
  786. #endif
  787. switch(*p){
  788. case ')': case ';': case ':': case '[': case ']':
  789. case '}':
  790. disp_error_message("parse_expr: unexpected char",p);
  791. }
  792. p=parse_subexpr(p,-1);
  793. #ifdef DEBUG_FUNCIN
  794. if(battle_config.etc_log)
  795. ShowDebug("parse_expr end %s\n",p);
  796. #endif
  797. return p;
  798. }
  799. /*==========================================
  800. * 行の解析
  801. *------------------------------------------
  802. */
  803. const char* parse_line(const char* p)
  804. {
  805. const char* p2;
  806. p=skip_space(p);
  807. if(*p==';') {
  808. // if(); for(); while(); のために閉じ判定
  809. p = parse_syntax_close(p);
  810. return p+1;
  811. }
  812. if(*p==')' && parse_syntax_for_flag)
  813. return p+1;
  814. p = skip_space(p);
  815. if(p[0] == '{') {
  816. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  817. syntax.curly[syntax.curly_count].count = -1;
  818. syntax.curly[syntax.curly_count].index = -1;
  819. syntax.curly_count++;
  820. return p + 1;
  821. } else if(p[0] == '}') {
  822. return parse_curly_close(p);
  823. }
  824. // 構文関連の処理
  825. p2 = parse_syntax(p);
  826. if(p2 != NULL)
  827. return p2;
  828. p = parse_callfunc(p,0);
  829. p = skip_space(p);
  830. if(parse_syntax_for_flag) {
  831. if( *p != ')' )
  832. disp_error_message("parse_line: need ')'",p);
  833. } else {
  834. if( *p != ';' )
  835. disp_error_message("parse_line: need ';'",p);
  836. }
  837. // if, for , while の閉じ判定
  838. p = parse_syntax_close(p+1);
  839. return p;
  840. }
  841. // { ... } の閉じ処理
  842. const char* parse_curly_close(const char* p) {
  843. if(syntax.curly_count <= 0) {
  844. disp_error_message("parse_curly_close: unexpected string",p);
  845. return p + 1;
  846. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  847. syntax.curly_count--;
  848. // if, for , while の閉じ判定
  849. p = parse_syntax_close(p + 1);
  850. return p;
  851. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  852. // switch() 閉じ判定
  853. int pos = syntax.curly_count-1;
  854. char label[256];
  855. int l;
  856. // 一時変数を消す
  857. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  858. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  859. parse_line(label);
  860. syntax.curly_count--;
  861. // 無条件で終了ポインタに移動
  862. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  863. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  864. parse_line(label);
  865. syntax.curly_count--;
  866. // 現在地のラベルを付ける
  867. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  868. l=add_str(label);
  869. set_label(l,script_pos, p);
  870. if(syntax.curly[pos].flag) {
  871. // default が存在する
  872. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  873. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  874. parse_line(label);
  875. syntax.curly_count--;
  876. }
  877. // 終了ラベルを付ける
  878. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  879. l=add_str(label);
  880. set_label(l,script_pos, p);
  881. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  882. syntax.curly_count--;
  883. return p+1;
  884. } else {
  885. disp_error_message("parse_curly_close: unexpected string",p);
  886. return p + 1;
  887. }
  888. }
  889. // 構文関連の処理
  890. // break, case, continue, default, do, for, function,
  891. // if, switch, while をこの内部で処理します。
  892. const char* parse_syntax(const char* p) {
  893. const char *p2 = skip_word(p);
  894. switch(*p) {
  895. case 'B':
  896. case 'b':
  897. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  898. // break の処理
  899. char label[256];
  900. int pos = syntax.curly_count - 1;
  901. while(pos >= 0) {
  902. if(syntax.curly[pos].type == TYPE_DO) {
  903. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  904. break;
  905. } else if(syntax.curly[pos].type == TYPE_FOR) {
  906. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  907. break;
  908. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  909. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  910. break;
  911. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  912. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  913. break;
  914. }
  915. pos--;
  916. }
  917. if(pos < 0) {
  918. disp_error_message("parse_syntax: unexpected 'break'",p);
  919. } else {
  920. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  921. parse_line(label);
  922. syntax.curly_count--;
  923. }
  924. p = skip_space(p2);
  925. if(*p != ';') {
  926. disp_error_message("parse_syntax: need ';'",p);
  927. }
  928. p++;
  929. // if, for , while の閉じ判定
  930. p = parse_syntax_close(p + 1);
  931. return p;
  932. }
  933. break;
  934. case 'c':
  935. case 'C':
  936. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  937. // case の処理
  938. int pos = syntax.curly_count-1;
  939. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  940. disp_error_message("parse_syntax: unexpected 'case' ",p);
  941. return p+1;
  942. } else {
  943. char label[256];
  944. int l,v;
  945. char *np;
  946. if(syntax.curly[pos].count != 1) {
  947. // FALLTHRU 用のジャンプ
  948. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  949. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  950. parse_line(label);
  951. syntax.curly_count--;
  952. // 現在地のラベルを付ける
  953. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  954. l=add_str(label);
  955. set_label(l,script_pos, p);
  956. }
  957. // switch 判定文
  958. p = skip_space(p2);
  959. if(p == p2) {
  960. disp_error_message("parse_syntax: expect space ' '",p);
  961. }
  962. // check whether case label is integer or not
  963. v = strtol(p,&np,0);
  964. if(np == p) { //Check for constants
  965. p2 = skip_word(p);
  966. v = p2-p; // length of word at p2
  967. memcpy(label,p,v);
  968. label[v]='\0';
  969. v = search_str(label);
  970. if (v < 0 || str_data[v].type != C_INT)
  971. disp_error_message("parse_syntax: 'case' label not integer",p);
  972. v = str_data[v].val;
  973. p = skip_word(p);
  974. } else { //Numeric value
  975. if((*p == '-' || *p == '+') && isdigit(p[1])) // pre-skip because '-' can not skip_word
  976. p++;
  977. p = skip_word(p);
  978. if(np != p)
  979. disp_error_message("parse_syntax: 'case' label not integer",np);
  980. }
  981. p = skip_space(p);
  982. if(*p != ':')
  983. disp_error_message("parse_syntax: expect ':'",p);
  984. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  985. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  986. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  987. // 2回parse しないとダメ
  988. p2 = parse_line(label);
  989. parse_line(p2);
  990. syntax.curly_count--;
  991. if(syntax.curly[pos].count != 1) {
  992. // FALLTHRU 終了後のラベル
  993. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  994. l=add_str(label);
  995. set_label(l,script_pos,p);
  996. }
  997. // check duplication of case label [Rayce]
  998. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  999. disp_error_message("parse_syntax: dup 'case'",p);
  1000. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1001. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1002. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1003. parse_line(label);
  1004. syntax.curly_count--;
  1005. syntax.curly[pos].count++;
  1006. }
  1007. return p + 1;
  1008. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1009. // continue の処理
  1010. char label[256];
  1011. int pos = syntax.curly_count - 1;
  1012. while(pos >= 0) {
  1013. if(syntax.curly[pos].type == TYPE_DO) {
  1014. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1015. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1016. break;
  1017. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1018. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1019. break;
  1020. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1021. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1022. break;
  1023. }
  1024. pos--;
  1025. }
  1026. if(pos < 0) {
  1027. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1028. } else {
  1029. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1030. parse_line(label);
  1031. syntax.curly_count--;
  1032. }
  1033. p = skip_space(p2);
  1034. if(*p != ';')
  1035. disp_error_message("parse_syntax: need ';'",p);
  1036. p++;
  1037. // if, for , while の閉じ判定
  1038. p = parse_syntax_close(p + 1);
  1039. return p;
  1040. }
  1041. break;
  1042. case 'd':
  1043. case 'D':
  1044. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1045. // switch - default の処理
  1046. int pos = syntax.curly_count-1;
  1047. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1048. disp_error_message("parse_syntax: unexpected 'default'",p);
  1049. } else if(syntax.curly[pos].flag) {
  1050. disp_error_message("parse_syntax: dup 'default'",p);
  1051. } else {
  1052. char label[256];
  1053. int l;
  1054. // 現在地のラベルを付ける
  1055. p = skip_space(p2);
  1056. if(*p != ':') {
  1057. disp_error_message("parse_syntax: need ':'",p);
  1058. }
  1059. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1060. l=add_str(label);
  1061. set_label(l,script_pos,p);
  1062. // 無条件で次のリンクに飛ばす
  1063. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1064. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1065. parse_line(label);
  1066. syntax.curly_count--;
  1067. // default のラベルを付ける
  1068. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1069. l=add_str(label);
  1070. set_label(l,script_pos,p);
  1071. syntax.curly[syntax.curly_count - 1].flag = 1;
  1072. syntax.curly[pos].count++;
  1073. }
  1074. return p + 1;
  1075. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1076. int l;
  1077. char label[256];
  1078. p=skip_space(p2);
  1079. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1080. syntax.curly[syntax.curly_count].count = 1;
  1081. syntax.curly[syntax.curly_count].index = syntax.index++;
  1082. syntax.curly[syntax.curly_count].flag = 0;
  1083. // 現在地のラベル形成する
  1084. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1085. l=add_str(label);
  1086. set_label(l,script_pos,p);
  1087. syntax.curly_count++;
  1088. return p;
  1089. }
  1090. break;
  1091. case 'f':
  1092. case 'F':
  1093. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1094. int l;
  1095. char label[256];
  1096. int pos = syntax.curly_count;
  1097. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1098. syntax.curly[syntax.curly_count].count = 1;
  1099. syntax.curly[syntax.curly_count].index = syntax.index++;
  1100. syntax.curly[syntax.curly_count].flag = 0;
  1101. syntax.curly_count++;
  1102. p=skip_space(p2);
  1103. if(*p != '(')
  1104. disp_error_message("parse_syntax: need '('",p);
  1105. p++;
  1106. // 初期化文を実行する
  1107. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1108. p=parse_line(p);
  1109. syntax.curly_count--;
  1110. // 条件判断開始のラベル形成する
  1111. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1112. l=add_str(label);
  1113. set_label(l,script_pos,p);
  1114. p=skip_space(p);
  1115. if(*p == ';') {
  1116. // for(;;) のパターンなので必ず真
  1117. ;
  1118. } else {
  1119. // 条件が偽なら終了地点に飛ばす
  1120. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1121. add_scriptl(add_str("jump_zero"));
  1122. add_scriptc(C_ARG);
  1123. p=parse_expr(p);
  1124. p=skip_space(p);
  1125. add_scriptl(add_str(label));
  1126. add_scriptc(C_FUNC);
  1127. }
  1128. if(*p != ';')
  1129. disp_error_message("parse_syntax: need ';'",p);
  1130. p++;
  1131. // ループ開始に飛ばす
  1132. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1133. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1134. parse_line(label);
  1135. syntax.curly_count--;
  1136. // 次のループへのラベル形成する
  1137. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1138. l=add_str(label);
  1139. set_label(l,script_pos,p);
  1140. // 次のループに入る時の処理
  1141. // for 最後の ')' を ';' として扱うフラグ
  1142. parse_syntax_for_flag = 1;
  1143. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1144. p=parse_line(p);
  1145. syntax.curly_count--;
  1146. parse_syntax_for_flag = 0;
  1147. // 条件判定処理に飛ばす
  1148. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1149. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1150. parse_line(label);
  1151. syntax.curly_count--;
  1152. // ループ開始のラベル付け
  1153. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1154. l=add_str(label);
  1155. set_label(l,script_pos,p);
  1156. return p;
  1157. } else if(p2 - p == 8 && !strncasecmp(p,"function",8)) {
  1158. const char *func_name;
  1159. // function
  1160. p=skip_space(p2);
  1161. if(p == p2)
  1162. disp_error_message("parse_syntax: expect space ' '",p);
  1163. // function - name
  1164. func_name = p;
  1165. p=skip_word(p);
  1166. if(*skip_space(p) == ';') {
  1167. // 関数の宣言 - 名前を登録して終わり
  1168. int l;
  1169. l=add_word(func_name);
  1170. if(str_data[l].type == C_NOP)
  1171. str_data[l].type = C_USERFUNC;
  1172. return skip_space(p) + 1;
  1173. } else {
  1174. // 関数の中身
  1175. char label[256];
  1176. int l;
  1177. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1178. syntax.curly[syntax.curly_count].count = 1;
  1179. syntax.curly[syntax.curly_count].index = syntax.index++;
  1180. syntax.curly[syntax.curly_count].flag = 0;
  1181. syntax.curly_count++;
  1182. // 関数終了まで飛ばす
  1183. sprintf(label,"goto __FN%x_FIN;",syntax.curly[syntax.curly_count-1].index);
  1184. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1185. parse_line(label);
  1186. syntax.curly_count--;
  1187. // 関数名のラベルを付ける
  1188. l=add_word(func_name);
  1189. if(str_data[l].type == C_NOP)
  1190. str_data[l].type = C_USERFUNC;
  1191. set_label(l,script_pos,p);
  1192. if( parse_options&SCRIPT_USE_LABEL_DB )
  1193. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1194. return skip_space(p);
  1195. }
  1196. }
  1197. break;
  1198. case 'i':
  1199. case 'I':
  1200. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1201. // if() の処理
  1202. char label[256];
  1203. p=skip_space(p2);
  1204. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1205. disp_error_message("need '('",p);
  1206. }
  1207. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1208. syntax.curly[syntax.curly_count].count = 1;
  1209. syntax.curly[syntax.curly_count].index = syntax.index++;
  1210. syntax.curly[syntax.curly_count].flag = 0;
  1211. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1212. syntax.curly_count++;
  1213. add_scriptl(add_str("jump_zero"));
  1214. add_scriptc(C_ARG);
  1215. p=parse_expr(p);
  1216. p=skip_space(p);
  1217. add_scriptl(add_str(label));
  1218. add_scriptc(C_FUNC);
  1219. return p;
  1220. }
  1221. break;
  1222. case 's':
  1223. case 'S':
  1224. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1225. // switch() の処理
  1226. char label[256];
  1227. p=skip_space(p2);
  1228. if(*p != '(') {
  1229. disp_error_message("need '('",p);
  1230. }
  1231. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1232. syntax.curly[syntax.curly_count].count = 1;
  1233. syntax.curly[syntax.curly_count].index = syntax.index++;
  1234. syntax.curly[syntax.curly_count].flag = 0;
  1235. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1236. syntax.curly_count++;
  1237. add_scriptl(add_str("set"));
  1238. add_scriptc(C_ARG);
  1239. add_scriptl(add_str(label));
  1240. p=parse_expr(p);
  1241. p=skip_space(p);
  1242. if(*p != '{') {
  1243. disp_error_message("parse_syntax: need '{'",p);
  1244. }
  1245. add_scriptc(C_FUNC);
  1246. return p + 1;
  1247. }
  1248. break;
  1249. case 'w':
  1250. case 'W':
  1251. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1252. int l;
  1253. char label[256];
  1254. p=skip_space(p2);
  1255. if(*p != '(') {
  1256. disp_error_message("need '('",p);
  1257. }
  1258. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1259. syntax.curly[syntax.curly_count].count = 1;
  1260. syntax.curly[syntax.curly_count].index = syntax.index++;
  1261. syntax.curly[syntax.curly_count].flag = 0;
  1262. // 条件判断開始のラベル形成する
  1263. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1264. l=add_str(label);
  1265. set_label(l,script_pos,p);
  1266. // 条件が偽なら終了地点に飛ばす
  1267. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1268. syntax.curly_count++;
  1269. add_scriptl(add_str("jump_zero"));
  1270. add_scriptc(C_ARG);
  1271. p=parse_expr(p);
  1272. p=skip_space(p);
  1273. add_scriptl(add_str(label));
  1274. add_scriptc(C_FUNC);
  1275. return p;
  1276. }
  1277. break;
  1278. }
  1279. return NULL;
  1280. }
  1281. const char* parse_syntax_close(const char *p) {
  1282. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1283. int flag;
  1284. do {
  1285. p = parse_syntax_close_sub(p,&flag);
  1286. } while(flag);
  1287. return p;
  1288. }
  1289. // if, for , while , do の閉じ判定
  1290. // flag == 1 : 閉じられた
  1291. // flag == 0 : 閉じられない
  1292. const char* parse_syntax_close_sub(const char* p,int* flag) {
  1293. char label[256];
  1294. int pos = syntax.curly_count - 1;
  1295. int l;
  1296. *flag = 1;
  1297. if(syntax.curly_count <= 0) {
  1298. *flag = 0;
  1299. return p;
  1300. } else if(syntax.curly[pos].type == TYPE_IF) {
  1301. const char *bp = p;
  1302. const char *p2;
  1303. // if 最終場所へ飛ばす
  1304. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1305. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1306. parse_line(label);
  1307. syntax.curly_count--;
  1308. // 現在地のラベルを付ける
  1309. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1310. l=add_str(label);
  1311. set_label(l,script_pos,p);
  1312. syntax.curly[pos].count++;
  1313. p = skip_space(p);
  1314. p2 = skip_word(p);
  1315. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1316. // else or else - if
  1317. p = skip_space(p2);
  1318. p2 = skip_word(p);
  1319. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1320. // else - if
  1321. p=skip_space(p2);
  1322. if(*p != '(') {
  1323. disp_error_message("need '('",p);
  1324. }
  1325. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1326. add_scriptl(add_str("jump_zero"));
  1327. add_scriptc(C_ARG);
  1328. p=parse_expr(p);
  1329. p=skip_space(p);
  1330. add_scriptl(add_str(label));
  1331. add_scriptc(C_FUNC);
  1332. *flag = 0;
  1333. return p;
  1334. } else {
  1335. // else
  1336. if(!syntax.curly[pos].flag) {
  1337. syntax.curly[pos].flag = 1;
  1338. *flag = 0;
  1339. return p;
  1340. }
  1341. }
  1342. }
  1343. // if 閉じ
  1344. syntax.curly_count--;
  1345. // 最終地のラベルを付ける
  1346. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1347. l=add_str(label);
  1348. set_label(l,script_pos,p);
  1349. if(syntax.curly[pos].flag == 1) {
  1350. // このifに対するelseじゃないのでポインタの位置は同じ
  1351. return bp;
  1352. }
  1353. return p;
  1354. } else if(syntax.curly[pos].type == TYPE_DO) {
  1355. int l;
  1356. char label[256];
  1357. const char *p2;
  1358. if(syntax.curly[pos].flag) {
  1359. // 現在地のラベル形成する(continue でここに来る)
  1360. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1361. l=add_str(label);
  1362. set_label(l,script_pos,p);
  1363. }
  1364. // 条件が偽なら終了地点に飛ばす
  1365. p = skip_space(p);
  1366. p2 = skip_word(p);
  1367. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1368. disp_error_message("parse_syntax: need 'while'",p);
  1369. p = skip_space(p2);
  1370. if(*p != '(') {
  1371. disp_error_message("need '('",p);
  1372. }
  1373. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1374. add_scriptl(add_str("jump_zero"));
  1375. add_scriptc(C_ARG);
  1376. p=parse_expr(p);
  1377. p=skip_space(p);
  1378. add_scriptl(add_str(label));
  1379. add_scriptc(C_FUNC);
  1380. // 開始地点に飛ばす
  1381. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1382. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1383. parse_line(label);
  1384. syntax.curly_count--;
  1385. // 条件終了地点のラベル形成する
  1386. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1387. l=add_str(label);
  1388. set_label(l,script_pos,p);
  1389. p = skip_space(p);
  1390. if(*p != ';') {
  1391. disp_error_message("parse_syntax: need ';'",p);
  1392. return p+1;
  1393. }
  1394. p++;
  1395. syntax.curly_count--;
  1396. return p;
  1397. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1398. // 次のループに飛ばす
  1399. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1400. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1401. parse_line(label);
  1402. syntax.curly_count--;
  1403. // for 終了のラベル付け
  1404. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1405. l=add_str(label);
  1406. set_label(l,script_pos,p);
  1407. syntax.curly_count--;
  1408. return p;
  1409. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1410. // while 条件判断へ飛ばす
  1411. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1412. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1413. parse_line(label);
  1414. syntax.curly_count--;
  1415. // while 終了のラベル付け
  1416. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1417. l=add_str(label);
  1418. set_label(l,script_pos,p);
  1419. syntax.curly_count--;
  1420. return p;
  1421. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1422. int pos = syntax.curly_count-1;
  1423. char label[256];
  1424. int l;
  1425. // 戻す
  1426. sprintf(label,"return;");
  1427. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1428. parse_line(label);
  1429. syntax.curly_count--;
  1430. // 現在地のラベルを付ける
  1431. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1432. l=add_str(label);
  1433. set_label(l,script_pos,p);
  1434. syntax.curly_count--;
  1435. return p + 1;
  1436. } else {
  1437. *flag = 0;
  1438. return p;
  1439. }
  1440. }
  1441. /*==========================================
  1442. * 組み込み関数の追加
  1443. *------------------------------------------
  1444. */
  1445. static void add_buildin_func(void)
  1446. {
  1447. int i,n;
  1448. const char* p;
  1449. for( i=0; buildin_func[i].func; i++ ){
  1450. /// arg must follow the pattern: (v|s|i|l)*\?*\*?
  1451. /// 'v' - value (either string or int)
  1452. /// 's' - string
  1453. /// 'i' - int
  1454. /// 'l' - label
  1455. /// '?' - one optional parameter
  1456. /// '*' - unknown number of optional parameters
  1457. p=buildin_func[i].arg;
  1458. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'l' ) ++p;
  1459. while( *p == '?' ) ++p;
  1460. if( *p == '*' ) ++p;
  1461. if( *p != 0){
  1462. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1463. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1464. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1465. } else {
  1466. n=add_str(buildin_func[i].name);
  1467. str_data[n].type=C_FUNC;
  1468. str_data[n].val=i;
  1469. str_data[n].func=buildin_func[i].func;
  1470. }
  1471. }
  1472. }
  1473. /*==========================================
  1474. * 定数データベースの読み込み
  1475. *------------------------------------------
  1476. */
  1477. static void read_constdb(void)
  1478. {
  1479. FILE *fp;
  1480. char line[1024],name[1024],val[1024];
  1481. int n,type;
  1482. sprintf(line, "%s/const.txt", db_path);
  1483. fp=fopen(line, "r");
  1484. if(fp==NULL){
  1485. ShowError("can't read %s\n", line);
  1486. return ;
  1487. }
  1488. while(fgets(line,1020,fp)){
  1489. if(line[0]=='/' && line[1]=='/')
  1490. continue;
  1491. type=0;
  1492. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1493. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1494. n=add_str(name);
  1495. if(type==0)
  1496. str_data[n].type=C_INT;
  1497. else
  1498. str_data[n].type=C_PARAM;
  1499. str_data[n].val= (int)strtol(val,NULL,0);
  1500. }
  1501. }
  1502. fclose(fp);
  1503. }
  1504. /*==========================================
  1505. * エラー表示
  1506. *------------------------------------------
  1507. */
  1508. const char* script_print_line( const char *p, const char *mark, int line ) {
  1509. int i;
  1510. if( p == NULL || !p[0] ) return NULL;
  1511. if( line < 0 )
  1512. printf("*% 5d : ", -line);
  1513. else
  1514. printf(" % 5d : ", line);
  1515. for(i=0;p[i] && p[i] != '\n';i++){
  1516. if(p + i != mark)
  1517. printf("%c",p[i]);
  1518. else
  1519. printf("\'%c\'",p[i]);
  1520. }
  1521. printf("\n");
  1522. return p+i+(p[i] == '\n' ? 1 : 0);
  1523. }
  1524. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos) {
  1525. // エラーが発生した行を求める
  1526. int j;
  1527. int line = start_line;
  1528. const char *p;
  1529. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1530. for(p=src;p && *p;line++){
  1531. char *lineend=strchr(p,'\n');
  1532. if(lineend==NULL || error_pos<lineend){
  1533. break;
  1534. }
  1535. for( j = 0; j < 4; j++ ) {
  1536. linestart[j] = linestart[j+1];
  1537. }
  1538. linestart[4] = p;
  1539. p=lineend+1;
  1540. }
  1541. printf("\a\n");
  1542. printf("script error on %s line %d\n", file, line);
  1543. printf(" %s\n", error_msg);
  1544. for(j = 0; j < 5; j++ ) {
  1545. script_print_line( linestart[j], NULL, line + j - 5);
  1546. }
  1547. p = script_print_line( p, error_pos, -line);
  1548. for(j = 0; j < 5; j++) {
  1549. p = script_print_line( p, NULL, line + j + 1 );
  1550. }
  1551. }
  1552. /*==========================================
  1553. * スクリプトの解析
  1554. *------------------------------------------
  1555. */
  1556. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1557. {
  1558. const char *p,*tmpp;
  1559. int i;
  1560. struct script_code *code;
  1561. static int first=1;
  1562. memset(&syntax,0,sizeof(syntax));
  1563. if(first){
  1564. add_buildin_func();
  1565. read_constdb();
  1566. }
  1567. first=0;
  1568. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1569. script_pos=0;
  1570. script_size=SCRIPT_BLOCK_SIZE;
  1571. str_data[LABEL_NEXTLINE].type=C_NOP;
  1572. str_data[LABEL_NEXTLINE].backpatch=-1;
  1573. str_data[LABEL_NEXTLINE].label=-1;
  1574. for(i=LABEL_START;i<str_num;i++){
  1575. if(
  1576. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1577. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1578. ){
  1579. str_data[i].type=C_NOP;
  1580. str_data[i].backpatch=-1;
  1581. str_data[i].label=-1;
  1582. }
  1583. }
  1584. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1585. if( options&SCRIPT_USE_LABEL_DB )
  1586. scriptlabel_db->clear(scriptlabel_db, NULL);
  1587. parse_options = options;
  1588. if( setjmp( error_jump ) != 0 ) {
  1589. //Restore program state when script has problems. [from jA]
  1590. int i;
  1591. const int size = sizeof(syntax.curly)/sizeof(syntax.curly[0]);
  1592. if( error_report )
  1593. script_error(src,file,line,error_msg,error_pos);
  1594. aFree( error_msg );
  1595. aFree( script_buf );
  1596. script_pos = 0;
  1597. script_size = 0;
  1598. script_buf = NULL;
  1599. for(i=LABEL_START;i<str_num;i++)
  1600. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1601. for(i=0; i<size; i++)
  1602. linkdb_final(&syntax.curly[i].case_label);
  1603. return NULL;
  1604. }
  1605. parse_syntax_for_flag=0;
  1606. p=src;
  1607. p=skip_space(p);
  1608. if(*p!='{'){
  1609. disp_error_message("not found '{'",p);
  1610. }
  1611. p++;
  1612. p = skip_space(p);
  1613. if (p && *p == '}') {
  1614. // an empty function, just return
  1615. aFree( script_buf );
  1616. script_pos = 0;
  1617. script_size = 0;
  1618. script_buf = NULL;
  1619. return NULL;
  1620. }
  1621. while (p && *p && (*p!='}' || syntax.curly_count != 0)) {
  1622. p=skip_space(p);
  1623. // labelだけ特殊処理
  1624. tmpp=skip_space(skip_word(p));
  1625. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1626. i=add_word(p);
  1627. set_label(i,script_pos,p);
  1628. if( parse_options&SCRIPT_USE_LABEL_DB )
  1629. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1630. p=tmpp+1;
  1631. continue;
  1632. }
  1633. // 他は全部一緒くた
  1634. p=parse_line(p);
  1635. p=skip_space(p);
  1636. add_scriptc(C_EOL);
  1637. set_label(LABEL_NEXTLINE,script_pos,p);
  1638. str_data[LABEL_NEXTLINE].type=C_NOP;
  1639. str_data[LABEL_NEXTLINE].backpatch=-1;
  1640. str_data[LABEL_NEXTLINE].label=-1;
  1641. }
  1642. add_scriptc(C_NOP);
  1643. script_size = script_pos;
  1644. RECREATE(script_buf,unsigned char,script_pos);
  1645. // 未解決のラベルを解決
  1646. for(i=LABEL_START;i<str_num;i++){
  1647. if(str_data[i].type==C_NOP){
  1648. int j,next;
  1649. str_data[i].type=C_NAME;
  1650. str_data[i].label=i;
  1651. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1652. next=GETVALUE(script_buf,j);
  1653. SETVALUE(script_buf,j,i);
  1654. j=next;
  1655. }
  1656. }
  1657. }
  1658. #ifdef DEBUG_DISP
  1659. for(i=0;i<script_pos;i++){
  1660. if((i&15)==0) printf("%04x : ",i);
  1661. printf("%02x ",script_buf[i]);
  1662. if((i&15)==15) printf("\n");
  1663. }
  1664. printf("\n");
  1665. #endif
  1666. #ifdef DEBUG_DISASM
  1667. {
  1668. int i = 0,j;
  1669. while(i < script_pos) {
  1670. printf("%06x ",i);
  1671. j = i;
  1672. switch(get_com(script_buf,&i)) {
  1673. case C_EOL: printf("C_EOL"); break;
  1674. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1675. case C_POS:
  1676. printf("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1677. i += 3;
  1678. break;
  1679. case C_NAME:
  1680. j = (*(int*)(script_buf+i)&0xffffff);
  1681. printf("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1682. i += 3;
  1683. break;
  1684. case C_ARG: printf("C_ARG"); break;
  1685. case C_FUNC: printf("C_FUNC"); break;
  1686. case C_ADD: printf("C_ADD"); break;
  1687. case C_SUB: printf("C_SUB"); break;
  1688. case C_MUL: printf("C_MUL"); break;
  1689. case C_DIV: printf("C_DIV"); break;
  1690. case C_MOD: printf("C_MOD"); break;
  1691. case C_EQ: printf("C_EQ"); break;
  1692. case C_NE: printf("C_NE"); break;
  1693. case C_GT: printf("C_GT"); break;
  1694. case C_GE: printf("C_GE"); break;
  1695. case C_LT: printf("C_LT"); break;
  1696. case C_LE: printf("C_LE"); break;
  1697. case C_AND: printf("C_AND"); break;
  1698. case C_OR: printf("C_OR"); break;
  1699. case C_XOR: printf("C_XOR"); break;
  1700. case C_LAND: printf("C_LAND"); break;
  1701. case C_LOR: printf("C_LOR"); break;
  1702. case C_R_SHIFT: printf("C_R_SHIFT"); break;
  1703. case C_L_SHIFT: printf("C_L_SHIFT"); break;
  1704. case C_NEG: printf("C_NEG"); break;
  1705. case C_NOT: printf("C_NOT"); break;
  1706. case C_LNOT: printf("C_LNOT"); break;
  1707. case C_NOP: printf("C_NOP"); break;
  1708. case C_OP3: printf("C_OP3"); break;
  1709. case C_STR:
  1710. j = strlen(script_buf + i);
  1711. printf("C_STR %s",script_buf + i);
  1712. i+= j+1;
  1713. break;
  1714. default:
  1715. printf("unknown");
  1716. }
  1717. printf("\n");
  1718. }
  1719. }
  1720. #endif
  1721. CREATE(code,struct script_code,1);
  1722. code->script_buf = script_buf;
  1723. code->script_size = script_size;
  1724. code->script_vars = NULL;
  1725. return code;
  1726. }
  1727. //
  1728. // 実行系
  1729. //
  1730. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1731. /*==========================================
  1732. * ridからsdへの解決
  1733. *------------------------------------------
  1734. */
  1735. struct map_session_data *script_rid2sd(struct script_state *st)
  1736. {
  1737. struct map_session_data *sd=map_id2sd(st->rid);
  1738. if(!sd){
  1739. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1740. report_src(st);
  1741. //## I would also terminate script execution. [FlavioJS]
  1742. //st->state = END;
  1743. }
  1744. return sd;
  1745. }
  1746. /*==========================================
  1747. * 変数の読み取り
  1748. *------------------------------------------
  1749. */
  1750. int get_val(struct script_state*st,struct script_data* data)
  1751. {
  1752. struct map_session_data *sd=NULL;
  1753. if(data->type==C_NAME){
  1754. char *name=str_buf+str_data[data->u.num&0x00ffffff].str;
  1755. char prefix=*name;
  1756. char postfix=name[strlen(name)-1];
  1757. if(not_server_variable(prefix)){
  1758. if((sd=script_rid2sd(st))==NULL)
  1759. ShowError("get_val error name?:%s\n",name);
  1760. }
  1761. if(postfix=='$'){
  1762. data->type=C_CONSTSTR;
  1763. if( prefix=='@'){
  1764. if(sd)
  1765. data->u.str = pc_readregstr(sd,data->u.num);
  1766. }else if(prefix=='$'){
  1767. data->u.str = (char *)idb_get(mapregstr_db,data->u.num);
  1768. }else if(prefix=='#'){
  1769. if( name[1]=='#'){
  1770. if(sd)
  1771. data->u.str = pc_readaccountreg2str(sd,name);
  1772. }else{
  1773. if(sd)
  1774. data->u.str = pc_readaccountregstr(sd,name);
  1775. }
  1776. }else if(prefix=='.') {
  1777. struct linkdb_node **n;
  1778. if( data->ref ) {
  1779. n = data->ref;
  1780. } else if( name[1] == '@' ) {
  1781. n = st->stack->var_function;
  1782. } else {
  1783. n = &st->script->script_vars;
  1784. }
  1785. data->u.str = linkdb_search(n, (void*)data->u.num );
  1786. }else{
  1787. if(sd)
  1788. data->u.str = pc_readglobalreg_str(sd,name);
  1789. } // [zBuffer]
  1790. /*else{
  1791. ShowWarning("script: get_val: illegal scope string variable.\n");
  1792. data->u.str = "!!ERROR!!";
  1793. }*/
  1794. if( data->u.str == NULL )
  1795. data->u.str ="";
  1796. }else{
  1797. data->type=C_INT;
  1798. if(str_data[data->u.num&0x00ffffff].type==C_INT){
  1799. data->u.num = str_data[data->u.num&0x00ffffff].val;
  1800. }else if(str_data[data->u.num&0x00ffffff].type==C_PARAM){
  1801. if(sd)
  1802. data->u.num = pc_readparam(sd,str_data[data->u.num&0x00ffffff].val);
  1803. }else if(prefix=='@'){
  1804. if(sd)
  1805. data->u.num = pc_readreg(sd,data->u.num);
  1806. }else if(prefix=='$'){
  1807. data->u.num = (int)idb_get(mapreg_db,data->u.num);
  1808. }else if(prefix=='#'){
  1809. if( name[1]=='#'){
  1810. if(sd)
  1811. data->u.num = pc_readaccountreg2(sd,name);
  1812. }else{
  1813. if(sd)
  1814. data->u.num = pc_readaccountreg(sd,name);
  1815. }
  1816. }else if(prefix=='.'){
  1817. struct linkdb_node **n;
  1818. if( data->ref ) {
  1819. n = data->ref;
  1820. } else if( name[1] == '@' ) {
  1821. n = st->stack->var_function;
  1822. } else {
  1823. n = &st->script->script_vars;
  1824. }
  1825. data->u.num = (int)linkdb_search(n, (void*)data->u.num);
  1826. }else{
  1827. if(sd)
  1828. data->u.num = pc_readglobalreg(sd,name);
  1829. }
  1830. }
  1831. }
  1832. return 0;
  1833. }
  1834. /*==========================================
  1835. * 変数の読み取り2
  1836. *------------------------------------------
  1837. */
  1838. void* get_val2(struct script_state*st,int num,struct linkdb_node **ref)
  1839. {
  1840. struct script_data dat;
  1841. dat.type=C_NAME;
  1842. dat.u.num=num;
  1843. dat.ref=ref;
  1844. get_val(st,&dat);
  1845. if( dat.type==C_INT ) return (void*)dat.u.num;
  1846. else return (void*)dat.u.str;
  1847. }
  1848. /*==========================================
  1849. * 変数設定用
  1850. *------------------------------------------
  1851. */
  1852. static int set_reg(struct script_state*st,struct map_session_data *sd,int num,char *name,void *v,struct linkdb_node** ref)
  1853. {
  1854. char prefix=*name;
  1855. char postfix=name[strlen(name)-1];
  1856. if( postfix=='$' ){
  1857. char *str=(char*)v;
  1858. if( prefix=='@'){
  1859. pc_setregstr(sd,num,str);
  1860. }else if(prefix=='$') {
  1861. mapreg_setregstr(num,str);
  1862. }else if(prefix=='#') {
  1863. if( name[1]=='#' )
  1864. pc_setaccountreg2str(sd,name,str);
  1865. else
  1866. pc_setaccountregstr(sd,name,str);
  1867. }else if(prefix=='.') {
  1868. char *p;
  1869. struct linkdb_node **n;
  1870. if( ref ) {
  1871. n = ref;
  1872. } else if( name[1] == '@' ) {
  1873. n = st->stack->var_function;
  1874. } else {
  1875. n = &st->script->script_vars;
  1876. }
  1877. p = linkdb_search(n, (void*)num);
  1878. if(p) {
  1879. linkdb_erase(n, (void*)num);
  1880. aFree(p);
  1881. }
  1882. if( ((char*)v)[0] )
  1883. linkdb_insert(n, (void*)num, aStrdup(v));
  1884. }else{
  1885. pc_setglobalreg_str(sd,name,str);
  1886. } // [zBuffer]
  1887. }else{
  1888. // 数値
  1889. int val = (int)v;
  1890. if(str_data[num&0x00ffffff].type==C_PARAM){
  1891. pc_setparam(sd,str_data[num&0x00ffffff].val,val);
  1892. }else if(prefix=='@') {
  1893. pc_setreg(sd,num,val);
  1894. }else if(prefix=='$') {
  1895. mapreg_setreg(num,val);
  1896. }else if(prefix=='#') {
  1897. if( name[1]=='#' )
  1898. pc_setaccountreg2(sd,name,val);
  1899. else
  1900. pc_setaccountreg(sd,name,val);
  1901. }else if(prefix == '.') {
  1902. struct linkdb_node **n;
  1903. if( ref ) {
  1904. n = ref;
  1905. } else if( name[1] == '@' ) {
  1906. n = st->stack->var_function;
  1907. } else {
  1908. n = &st->script->script_vars;
  1909. }
  1910. if( val == 0 ) {
  1911. linkdb_erase(n, (void*)num);
  1912. } else {
  1913. linkdb_replace(n, (void*)num, (void*)val);
  1914. }
  1915. }else{
  1916. pc_setglobalreg(sd,name,val);
  1917. }
  1918. }
  1919. return 0;
  1920. }
  1921. int set_var(struct map_session_data *sd, char *name, void *val)
  1922. {
  1923. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  1924. }
  1925. /*==========================================
  1926. * 文字列への変換
  1927. *------------------------------------------
  1928. */
  1929. char* conv_str(struct script_state *st,struct script_data *data)
  1930. {
  1931. get_val(st,data);
  1932. if(data->type==C_INT){
  1933. char *buf;
  1934. CREATE(buf,char,ITEM_NAME_LENGTH);
  1935. snprintf(buf,ITEM_NAME_LENGTH, "%d",data->u.num);
  1936. buf[ITEM_NAME_LENGTH-1]=0;
  1937. data->type=C_STR;
  1938. data->u.str=buf;
  1939. } else if(data->type==C_POS) {
  1940. // Protect form crashes by passing labels to string-expected args [jA2200]
  1941. data->type = C_CONSTSTR;
  1942. data->u.str = "** SCRIPT ERROR **";
  1943. } else if(data->type==C_NAME){
  1944. data->type=C_CONSTSTR;
  1945. data->u.str=str_buf+str_data[data->u.num].str;
  1946. }
  1947. return data->u.str;
  1948. }
  1949. /*==========================================
  1950. * 数値へ変換
  1951. *------------------------------------------
  1952. */
  1953. int conv_num(struct script_state *st,struct script_data *data)
  1954. {
  1955. char *p;
  1956. get_val(st,data);
  1957. if(data->type==C_STR || data->type==C_CONSTSTR){
  1958. p=data->u.str;
  1959. data->u.num = atoi(p);
  1960. if(data->type==C_STR)
  1961. aFree(p);
  1962. data->type=C_INT;
  1963. }
  1964. return data->u.num;
  1965. }
  1966. /*==========================================
  1967. * スタックへ数値をプッシュ
  1968. *------------------------------------------
  1969. */
  1970. void push_val(struct script_stack *stack,int type,int val)
  1971. {
  1972. if(stack->sp >= stack->sp_max){
  1973. stack->sp_max += 64;
  1974. stack->stack_data = (struct script_data *)aRealloc(stack->stack_data,
  1975. sizeof(stack->stack_data[0]) * stack->sp_max);
  1976. memset(stack->stack_data + (stack->sp_max - 64), 0,
  1977. 64 * sizeof(*(stack->stack_data)));
  1978. }
  1979. // if(battle_config.etc_log)
  1980. // printf("push (%d,%d)-> %d\n",type,val,stack->sp);
  1981. stack->stack_data[stack->sp].type=type;
  1982. stack->stack_data[stack->sp].u.num=val;
  1983. stack->stack_data[stack->sp].ref = NULL;
  1984. stack->sp++;
  1985. }
  1986. /*==========================================
  1987. * スタックへ数値+リファレンスをプッシュ
  1988. *------------------------------------------
  1989. */
  1990. void push_val2(struct script_stack *stack,int type,int val,struct linkdb_node** ref) {
  1991. push_val(stack,type,val);
  1992. stack->stack_data[stack->sp-1].ref = ref;
  1993. }
  1994. /*==========================================
  1995. * スタックへ文字列をプッシュ
  1996. *------------------------------------------
  1997. */
  1998. void push_str(struct script_stack *stack,int type,char *str)
  1999. {
  2000. if(stack->sp>=stack->sp_max){
  2001. stack->sp_max += 64;
  2002. stack->stack_data = (struct script_data *)aRealloc(stack->stack_data,
  2003. sizeof(stack->stack_data[0]) * stack->sp_max);
  2004. memset(stack->stack_data + (stack->sp_max - 64), '\0',
  2005. 64 * sizeof(*(stack->stack_data)));
  2006. }
  2007. // if(battle_config.etc_log)
  2008. // printf("push (%d,%x)-> %d\n",type,str,stack->sp);
  2009. stack->stack_data[stack->sp].type =type;
  2010. stack->stack_data[stack->sp].u.str=str;
  2011. stack->stack_data[stack->sp].ref =NULL;
  2012. stack->sp++;
  2013. }
  2014. /*==========================================
  2015. * スタックへ複製をプッシュ
  2016. *------------------------------------------
  2017. */
  2018. void push_copy(struct script_stack *stack,int pos)
  2019. {
  2020. switch(stack->stack_data[pos].type){
  2021. case C_CONSTSTR:
  2022. push_str(stack,C_CONSTSTR,stack->stack_data[pos].u.str);
  2023. break;
  2024. case C_STR:
  2025. push_str(stack,C_STR,aStrdup(stack->stack_data[pos].u.str));
  2026. break;
  2027. default:
  2028. push_val2(
  2029. stack,stack->stack_data[pos].type,stack->stack_data[pos].u.num,
  2030. stack->stack_data[pos].ref
  2031. );
  2032. break;
  2033. }
  2034. }
  2035. /*==========================================
  2036. * スタックからポップ
  2037. *------------------------------------------
  2038. */
  2039. void pop_stack(struct script_stack* stack,int start,int end)
  2040. {
  2041. int i;
  2042. for(i=start;i<end;i++){
  2043. if(stack->stack_data[i].type==C_STR){
  2044. aFree(stack->stack_data[i].u.str);
  2045. stack->stack_data[i].type=C_INT; //Might not be correct, but it's done in case to prevent pointer errors later on. [Skotlex]
  2046. }
  2047. }
  2048. if(stack->sp>end){
  2049. memmove(&stack->stack_data[start],&stack->stack_data[end],sizeof(stack->stack_data[0])*(stack->sp-end));
  2050. }
  2051. stack->sp-=end-start;
  2052. }
  2053. /*==========================================
  2054. * スクリプト依存変数、関数依存変数の解放
  2055. *------------------------------------------
  2056. */
  2057. void script_free_vars(struct linkdb_node **node) {
  2058. struct linkdb_node *n = *node;
  2059. while(n) {
  2060. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2061. char postfix = name[strlen(name)-1];
  2062. if( postfix == '$' ) {
  2063. // 文字型変数なので、データ削除
  2064. aFree(n->data);
  2065. }
  2066. n = n->next;
  2067. }
  2068. linkdb_final( node );
  2069. }
  2070. /*==========================================
  2071. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2072. *------------------------------------------
  2073. */
  2074. void script_free_stack(struct script_stack *stack) {
  2075. int i;
  2076. for(i = 0; i < stack->sp; i++) {
  2077. if( stack->stack_data[i].type == C_STR ) {
  2078. aFree(stack->stack_data[i].u.str);
  2079. stack->stack_data[i].type = C_INT;
  2080. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2081. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2082. script_free_vars( n );
  2083. aFree( n );
  2084. }
  2085. }
  2086. script_free_vars( stack->var_function );
  2087. aFree(stack->var_function);
  2088. aFree(stack->stack_data);
  2089. aFree(stack);
  2090. }
  2091. void script_free_code(struct script_code* code) {
  2092. script_free_vars( &code->script_vars );
  2093. aFree( code->script_buf );
  2094. aFree( code );
  2095. }
  2096. //
  2097. // 実行部main
  2098. //
  2099. /*==========================================
  2100. * コマンドの読み取り
  2101. *------------------------------------------
  2102. */
  2103. static int unget_com_data=-1;
  2104. int get_com(unsigned char *script,int *pos)
  2105. {
  2106. int i,j;
  2107. if(unget_com_data>=0){
  2108. i=unget_com_data;
  2109. unget_com_data=-1;
  2110. return i;
  2111. }
  2112. if(script[*pos]>=0x80){
  2113. return C_INT;
  2114. }
  2115. i=0; j=0;
  2116. while(script[*pos]>=0x40){
  2117. i=script[(*pos)++]<<j;
  2118. j+=6;
  2119. }
  2120. return i+(script[(*pos)++]<<j);
  2121. }
  2122. /*==========================================
  2123. * コマンドのプッシュバック
  2124. *------------------------------------------
  2125. */
  2126. void unget_com(int c)
  2127. {
  2128. if(unget_com_data!=-1){
  2129. if(battle_config.error_log)
  2130. ShowError("unget_com can back only 1 data\n");
  2131. }
  2132. unget_com_data=c;
  2133. }
  2134. /*==========================================
  2135. * 数値の所得
  2136. *------------------------------------------
  2137. */
  2138. int get_num(unsigned char *script,int *pos)
  2139. {
  2140. int i,j;
  2141. i=0; j=0;
  2142. while(script[*pos]>=0xc0){
  2143. i+=(script[(*pos)++]&0x7f)<<j;
  2144. j+=6;
  2145. }
  2146. return i+((script[(*pos)++]&0x7f)<<j);
  2147. }
  2148. /*==========================================
  2149. * スタックから値を取り出す
  2150. *------------------------------------------
  2151. */
  2152. int pop_val(struct script_state* st)
  2153. {
  2154. if(st->stack->sp<=0)
  2155. return 0;
  2156. st->stack->sp--;
  2157. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2158. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2159. return st->stack->stack_data[st->stack->sp].u.num;
  2160. return 0;
  2161. }
  2162. int isstr(struct script_data *c) {
  2163. if( c->type == C_STR || c->type == C_CONSTSTR )
  2164. return 1;
  2165. else if( c->type == C_NAME ) {
  2166. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2167. char postfix = p[strlen(p)-1];
  2168. return (postfix == '$');
  2169. }
  2170. return 0;
  2171. }
  2172. /*==========================================
  2173. * Three-section operator
  2174. * test ? if_true : if_false
  2175. *------------------------------------------
  2176. */
  2177. void op_3(struct script_state *st) {
  2178. int flag = 0;
  2179. if( isstr(&st->stack->stack_data[st->stack->sp-3])) {
  2180. char *str = conv_str(st,& (st->stack->stack_data[st->stack->sp-3]));
  2181. flag = str[0];
  2182. } else {
  2183. flag = conv_num(st,& (st->stack->stack_data[st->stack->sp-3]));
  2184. }
  2185. if( flag ) {
  2186. push_copy(st->stack, st->stack->sp-2 );
  2187. } else {
  2188. push_copy(st->stack, st->stack->sp-1 );
  2189. }
  2190. pop_stack(st->stack,st->stack->sp-4,st->stack->sp-1);
  2191. }
  2192. /*==========================================
  2193. * 加算演算子
  2194. *------------------------------------------
  2195. */
  2196. void op_add(struct script_state* st)
  2197. {
  2198. st->stack->sp--;
  2199. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2200. get_val(st,&(st->stack->stack_data[st->stack->sp-1]));
  2201. if(isstr(&st->stack->stack_data[st->stack->sp]) || isstr(&st->stack->stack_data[st->stack->sp-1])){
  2202. conv_str(st,&(st->stack->stack_data[st->stack->sp]));
  2203. conv_str(st,&(st->stack->stack_data[st->stack->sp-1]));
  2204. }
  2205. if(st->stack->stack_data[st->stack->sp].type==C_INT){ // ii
  2206. int *i1 = &st->stack->stack_data[st->stack->sp-1].u.num;
  2207. int *i2 = &st->stack->stack_data[st->stack->sp].u.num;
  2208. int ret = *i1 + *i2;
  2209. double ret_double = (double)*i1 + (double)*i2;
  2210. if(ret_double > INT_MAX|| ret_double < INT_MIN) {
  2211. ShowWarning("script::op_add overflow detected op:%d\n",C_ADD);
  2212. report_src(st);
  2213. ret = cap_value(ret, INT_MIN, INT_MAX);
  2214. }
  2215. *i1 = ret;
  2216. } else { // ssの予定
  2217. char *buf;
  2218. buf=(char *)aMallocA((strlen(st->stack->stack_data[st->stack->sp-1].u.str)+
  2219. strlen(st->stack->stack_data[st->stack->sp].u.str)+1)*sizeof(char));
  2220. strcpy(buf,st->stack->stack_data[st->stack->sp-1].u.str);
  2221. strcat(buf,st->stack->stack_data[st->stack->sp].u.str);
  2222. if(st->stack->stack_data[st->stack->sp-1].type==C_STR)
  2223. {
  2224. aFree(st->stack->stack_data[st->stack->sp-1].u.str);
  2225. st->stack->stack_data[st->stack->sp-1].type=C_INT;
  2226. }
  2227. if(st->stack->stack_data[st->stack->sp].type==C_STR)
  2228. {
  2229. aFree(st->stack->stack_data[st->stack->sp].u.str);
  2230. st->stack->stack_data[st->stack->sp].type=C_INT;
  2231. }
  2232. st->stack->stack_data[st->stack->sp-1].type=C_STR;
  2233. st->stack->stack_data[st->stack->sp-1].u.str=buf;
  2234. }
  2235. st->stack->stack_data[st->stack->sp-1].ref = NULL;
  2236. }
  2237. /*==========================================
  2238. * 二項演算子(文字列)
  2239. *------------------------------------------
  2240. */
  2241. void op_2str(struct script_state *st,int op,int sp1,int sp2)
  2242. {
  2243. char *s1=st->stack->stack_data[sp1].u.str,
  2244. *s2=st->stack->stack_data[sp2].u.str;
  2245. int a=0;
  2246. switch(op){
  2247. case C_EQ:
  2248. a= (strcmp(s1,s2)==0);
  2249. break;
  2250. case C_NE:
  2251. a= (strcmp(s1,s2)!=0);
  2252. break;
  2253. case C_GT:
  2254. a= (strcmp(s1,s2)> 0);
  2255. break;
  2256. case C_GE:
  2257. a= (strcmp(s1,s2)>=0);
  2258. break;
  2259. case C_LT:
  2260. a= (strcmp(s1,s2)< 0);
  2261. break;
  2262. case C_LE:
  2263. a= (strcmp(s1,s2)<=0);
  2264. break;
  2265. default:
  2266. ShowWarning("script: illegal string operator\n");
  2267. break;
  2268. }
  2269. // Because push_val() overwrite stack_data[sp1], C_STR on stack_data[sp1] won't be freed.
  2270. // So, call push_val() after freeing strings. [jA1783]
  2271. // push_val(st->stack,C_INT,a);
  2272. if(st->stack->stack_data[sp1].type==C_STR)
  2273. {
  2274. aFree(s1);
  2275. st->stack->stack_data[sp1].type=C_INT;
  2276. }
  2277. if(st->stack->stack_data[sp2].type==C_STR)
  2278. {
  2279. aFree(s2);
  2280. st->stack->stack_data[sp2].type=C_INT;
  2281. }
  2282. push_val(st->stack,C_INT,a);
  2283. }
  2284. /*==========================================
  2285. * 二項演算子(数値)
  2286. *------------------------------------------
  2287. */
  2288. void op_2num(struct script_state *st,int op,int i1,int i2)
  2289. {
  2290. int ret = 0;
  2291. double ret_double = 0;
  2292. switch(op){
  2293. case C_MOD: ret = i1 % i2; break;
  2294. case C_AND: ret = i1 & i2; break;
  2295. case C_OR: ret = i1 | i2; break;
  2296. case C_XOR: ret = i1 ^ i2; break;
  2297. case C_LAND: ret = (i1 && i2); break;
  2298. case C_LOR: ret = (i1 || i2); break;
  2299. case C_EQ: ret = (i1 == i2); break;
  2300. case C_NE: ret = (i1 != i2); break;
  2301. case C_GT: ret = (i1 > i2); break;
  2302. case C_GE: ret = (i1 >= i2); break;
  2303. case C_LT: ret = (i1 < i2); break;
  2304. case C_LE: ret = (i1 <= i2); break;
  2305. case C_R_SHIFT: ret = i1>>i2; break;
  2306. case C_L_SHIFT: ret = i1<<i2; break;
  2307. default:
  2308. switch(op) {
  2309. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2310. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2311. case C_DIV:
  2312. if(i2 == 0) {
  2313. printf("script::op_2num division by zero.\n");
  2314. ret = INT_MAX;
  2315. ret_double = 0; // doubleの精度が怪しいのでオーバーフロー対策を飛ばす
  2316. } else {
  2317. ret = i1 / i2; ret_double = (double)i1 / (double)i2;
  2318. }
  2319. break;
  2320. }
  2321. if(ret_double > INT_MAX || ret_double < INT_MIN) {
  2322. printf("script::op_2num overflow detected op:%d\n",op);
  2323. report_src(st);
  2324. ret = (int)cap_value(ret_double,INT_MAX,INT_MIN);
  2325. }
  2326. }
  2327. push_val(st->stack,C_INT,ret);
  2328. }
  2329. /*==========================================
  2330. * 二項演算子
  2331. *------------------------------------------
  2332. */
  2333. void op_2(struct script_state *st,int op)
  2334. {
  2335. int i1,i2;
  2336. char *s1=NULL,*s2=NULL;
  2337. i2=pop_val(st);
  2338. if( isstr(&st->stack->stack_data[st->stack->sp]) )
  2339. s2=st->stack->stack_data[st->stack->sp].u.str;
  2340. i1=pop_val(st);
  2341. if( isstr(&st->stack->stack_data[st->stack->sp]) )
  2342. s1=st->stack->stack_data[st->stack->sp].u.str;
  2343. if( s1!=NULL && s2!=NULL ){
  2344. // ss => op_2str
  2345. op_2str(st,op,st->stack->sp,st->stack->sp+1);
  2346. }else if( s1==NULL && s2==NULL ){
  2347. // ii => op_2num
  2348. op_2num(st,op,i1,i2);
  2349. }else{
  2350. // si,is => error
  2351. ShowWarning("script: op_2: int&str, str&int not allow.\n");
  2352. report_src(st);
  2353. if(s1 && st->stack->stack_data[st->stack->sp].type == C_STR)
  2354. {
  2355. aFree(s1);
  2356. st->stack->stack_data[st->stack->sp].type = C_INT;
  2357. }
  2358. if(s2 && st->stack->stack_data[st->stack->sp+1].type == C_STR)
  2359. {
  2360. aFree(s2);
  2361. st->stack->stack_data[st->stack->sp+1].type = C_INT;
  2362. }
  2363. push_val(st->stack,C_INT,0);
  2364. }
  2365. }
  2366. /*==========================================
  2367. * 単項演算子
  2368. *------------------------------------------
  2369. */
  2370. void op_1num(struct script_state *st,int op)
  2371. {
  2372. int i1;
  2373. i1=pop_val(st);
  2374. switch(op){
  2375. case C_NEG:
  2376. i1=-i1;
  2377. break;
  2378. case C_NOT:
  2379. i1=~i1;
  2380. break;
  2381. case C_LNOT:
  2382. i1=!i1;
  2383. break;
  2384. }
  2385. push_val(st->stack,C_INT,i1);
  2386. }
  2387. /*==========================================
  2388. * 関数の実行
  2389. *------------------------------------------
  2390. */
  2391. int run_func(struct script_state *st)
  2392. {
  2393. int i,start_sp,end_sp,func;
  2394. end_sp=st->stack->sp;
  2395. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2396. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2397. if(battle_config.error_log)
  2398. ShowError("function not found\n");
  2399. // st->stack->sp=0;
  2400. st->state=END;
  2401. report_src(st);
  2402. return 1;
  2403. }
  2404. start_sp=i-1;
  2405. st->start=i-1;
  2406. st->end=end_sp;
  2407. func=st->stack->stack_data[st->start].u.num;
  2408. #ifdef DEBUG_RUN
  2409. if(battle_config.etc_log) {
  2410. 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);
  2411. ShowDebug("stack dump :");
  2412. for(i=0;i<end_sp;i++){
  2413. switch(st->stack->stack_data[i].type){
  2414. case C_INT:
  2415. printf(" int(%d)",st->stack->stack_data[i].u.num);
  2416. break;
  2417. case C_NAME:
  2418. printf(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2419. break;
  2420. case C_ARG:
  2421. printf(" arg");
  2422. break;
  2423. case C_POS:
  2424. printf(" pos(%d)",st->stack->stack_data[i].u.num);
  2425. break;
  2426. case C_STR:
  2427. printf(" str(%s)",st->stack->stack_data[i].u.str);
  2428. break;
  2429. case C_CONSTSTR:
  2430. printf(" cstr(%s)",st->stack->stack_data[i].u.str);
  2431. break;
  2432. default:
  2433. printf(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2434. }
  2435. }
  2436. printf("\n");
  2437. }
  2438. #endif
  2439. if(str_data[func].type!=C_FUNC ){
  2440. 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);
  2441. // st->stack->sp=0;
  2442. st->state=END;
  2443. report_src(st);
  2444. return 1;
  2445. }
  2446. #ifdef DEBUG_RUN
  2447. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2448. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2449. );
  2450. #endif
  2451. if(str_data[func].func){
  2452. if (str_data[func].func(st)) //Report error
  2453. report_src(st);
  2454. } else {
  2455. if(battle_config.error_log)
  2456. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2457. push_val(st->stack,C_INT,0);
  2458. report_src(st);
  2459. }
  2460. // Stack's datum are used when re-run functions [Eoe]
  2461. if(st->state != RERUNLINE) {
  2462. pop_stack(st->stack,start_sp,end_sp);
  2463. }
  2464. if(st->state==RETFUNC){
  2465. // ユーザー定義関数からの復帰
  2466. int olddefsp=st->stack->defsp;
  2467. int i;
  2468. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2469. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2470. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2471. st->state=END;
  2472. report_src(st);
  2473. return 1;
  2474. }
  2475. script_free_vars( st->stack->var_function );
  2476. aFree(st->stack->var_function);
  2477. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2478. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2479. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2480. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2481. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2482. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2483. st->state=GOTO;
  2484. }
  2485. return 0;
  2486. }
  2487. /*==========================================
  2488. * スクリプトの実行
  2489. *------------------------------------------
  2490. */
  2491. void run_script_main(struct script_state *st);
  2492. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2493. {
  2494. struct script_state *st;
  2495. struct map_session_data *sd=NULL;
  2496. if(rootscript==NULL || pos<0)
  2497. return;
  2498. if (rid) sd = map_id2sd(rid);
  2499. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2500. //Resume script.
  2501. st = sd->st;
  2502. } else {
  2503. st = aCalloc(sizeof(struct script_state), 1);
  2504. // the script is different, make new script_state and stack
  2505. st->stack = aMalloc (sizeof(struct script_stack));
  2506. st->stack->sp=0;
  2507. st->stack->sp_max=64;
  2508. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2509. st->stack->defsp = st->stack->sp;
  2510. st->stack->var_function = aCalloc(1, sizeof(struct linkdb_node*));
  2511. st->state = RUN;
  2512. st->script = rootscript;
  2513. }
  2514. st->pos = pos;
  2515. st->rid = rid;
  2516. st->oid = oid;
  2517. st->sleep.timer = -1;
  2518. run_script_main(st);
  2519. }
  2520. void script_stop_sleeptimers(int id)
  2521. {
  2522. struct script_state* st;
  2523. for(;;)
  2524. {
  2525. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2526. if( st == NULL )
  2527. break; // no more sleep timers
  2528. if( st->sleep.timer != INVALID_TIMER )
  2529. delete_timer(st->sleep.timer, run_script_timer);
  2530. script_free_stack(st->stack);
  2531. aFree(st);
  2532. }
  2533. }
  2534. /*==========================================
  2535. * 指定ノードをsleep_dbから削除
  2536. *------------------------------------------
  2537. */
  2538. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2539. {
  2540. struct linkdb_node *retnode;
  2541. if( n == NULL)
  2542. return NULL;
  2543. if( n->prev == NULL )
  2544. sleep_db = n->next;
  2545. else
  2546. n->prev->next = n->next;
  2547. if( n->next )
  2548. n->next->prev = n->prev;
  2549. retnode = n->next;
  2550. aFree( n );
  2551. return retnode; // 次のノードを返す
  2552. }
  2553. /*==========================================
  2554. * sleep用タイマー関数
  2555. *------------------------------------------
  2556. */
  2557. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2558. {
  2559. struct script_state *st = (struct script_state *)data;
  2560. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2561. struct map_session_data *sd = map_id2sd(st->rid);
  2562. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2563. { //Character mismatch. Cancel execution.
  2564. st->rid = 0;
  2565. st->state = END;
  2566. }
  2567. while( node && st->sleep.timer != -1 ) {
  2568. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2569. script_erase_sleepdb(node);
  2570. st->sleep.timer = -1;
  2571. break;
  2572. }
  2573. node = node->next;
  2574. }
  2575. if(st->state != RERUNLINE)
  2576. st->sleep.tick = 0;
  2577. run_script_main(st);
  2578. return 0;
  2579. }
  2580. /*==========================================
  2581. * スクリプトの実行メイン部分
  2582. *------------------------------------------
  2583. */
  2584. void run_script_main(struct script_state *st)
  2585. {
  2586. int c;
  2587. int cmdcount=script_config.check_cmdcount;
  2588. int gotocount=script_config.check_gotocount;
  2589. struct map_session_data *sd;
  2590. //For backing up purposes
  2591. struct script_state *bk_st = NULL;
  2592. int bk_npcid = 0;
  2593. struct script_stack *stack=st->stack;
  2594. sd = st->rid?map_id2sd(st->rid):NULL;
  2595. if(sd){
  2596. if(sd->st != st){
  2597. bk_st = sd->st;
  2598. bk_npcid = sd->npc_id;
  2599. }
  2600. sd->st = st;
  2601. sd->npc_id = st->oid;
  2602. }
  2603. if(st->state == RERUNLINE) {
  2604. st->state = RUN;
  2605. run_func(st);
  2606. if(st->state == GOTO)
  2607. st->state = RUN;
  2608. } else if(st->state != END)
  2609. st->state = RUN;
  2610. while(st->state == RUN){
  2611. c= get_com(st->script->script_buf,&st->pos);
  2612. switch(c){
  2613. case C_EOL:
  2614. if(stack->sp!=stack->defsp){
  2615. if(stack->sp > stack->defsp)
  2616. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2617. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2618. if (battle_config.etc_log)
  2619. {
  2620. ShowWarning("Clearing unused stack stack.sp(%d) -> default(%d)\n",stack->sp,stack->defsp);
  2621. report_src(st);
  2622. }
  2623. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2624. } else if(battle_config.error_log)
  2625. ShowError("stack.sp(%d) != default(%d)\n",stack->sp,stack->defsp);
  2626. stack->sp=stack->defsp;
  2627. }
  2628. break;
  2629. case C_INT:
  2630. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2631. break;
  2632. case C_POS:
  2633. case C_NAME:
  2634. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2635. st->pos+=3;
  2636. break;
  2637. case C_ARG:
  2638. push_val(stack,c,0);
  2639. break;
  2640. case C_STR:
  2641. push_str(stack,C_CONSTSTR,(st->script->script_buf+st->pos));
  2642. while(st->script->script_buf[st->pos++]);
  2643. break;
  2644. case C_FUNC:
  2645. run_func(st);
  2646. if(st->state==GOTO){
  2647. st->state = RUN;
  2648. if( gotocount>0 && (--gotocount)<=0 ){
  2649. ShowError("run_script: infinity loop !\n");
  2650. report_src(st);
  2651. st->state=END;
  2652. }
  2653. }
  2654. break;
  2655. case C_ADD:
  2656. op_add(st);
  2657. break;
  2658. case C_SUB:
  2659. case C_MUL:
  2660. case C_DIV:
  2661. case C_MOD:
  2662. case C_EQ:
  2663. case C_NE:
  2664. case C_GT:
  2665. case C_GE:
  2666. case C_LT:
  2667. case C_LE:
  2668. case C_AND:
  2669. case C_OR:
  2670. case C_XOR:
  2671. case C_LAND:
  2672. case C_LOR:
  2673. case C_R_SHIFT:
  2674. case C_L_SHIFT:
  2675. op_2(st,c);
  2676. break;
  2677. case C_NEG:
  2678. case C_NOT:
  2679. case C_LNOT:
  2680. op_1num(st,c);
  2681. break;
  2682. case C_OP3:
  2683. op_3(st);
  2684. break;
  2685. case C_NOP:
  2686. st->state=END;
  2687. break;
  2688. default:
  2689. if(battle_config.error_log)
  2690. ShowError("unknown command : %d @ %d\n",c,pos);
  2691. st->state=END;
  2692. break;
  2693. }
  2694. if( cmdcount>0 && (--cmdcount)<=0 ){
  2695. ShowError("run_script: infinity loop !\n");
  2696. report_src(st);
  2697. st->state=END;
  2698. }
  2699. }
  2700. if(st->sleep.tick > 0) {
  2701. //Delay execution
  2702. st->sleep.charid = sd?sd->status.char_id:0;
  2703. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2704. run_script_timer, st->sleep.charid, (int)st);
  2705. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2706. //Restore previous script
  2707. if (sd) {
  2708. sd->st = bk_st;
  2709. sd->npc_id = bk_npcid;
  2710. bk_st = NULL; //Remove tag for removal.
  2711. }
  2712. }
  2713. else if(st->state != END && sd){
  2714. //Resume later (st is already attached to player).
  2715. if(bk_st) {
  2716. ShowWarning("Unable to restore stack! Double continuation!\n");
  2717. //Report BOTH scripts to see if that can help somehow.
  2718. ShowDebug("Previous script (lost):\n");
  2719. report_src(bk_st);
  2720. ShowDebug("Current script:\n");
  2721. report_src(st);
  2722. }
  2723. } else {
  2724. //Dispose of script.
  2725. if (sd)
  2726. { //Restore previous stack and save char.
  2727. if(sd->state.using_fake_npc){
  2728. clif_clearchar_id(sd->npc_id, 0, sd->fd);
  2729. sd->state.using_fake_npc = 0;
  2730. }
  2731. //Restore previous script if any.
  2732. sd->st = bk_st;
  2733. sd->npc_id = bk_npcid;
  2734. if (!bk_st)
  2735. npc_event_dequeue(sd);
  2736. else
  2737. bk_st = NULL; //Remove tag for removal.
  2738. if (sd->state.reg_dirty&2)
  2739. intif_saveregistry(sd,2);
  2740. if (sd->state.reg_dirty&1)
  2741. intif_saveregistry(sd,1);
  2742. }
  2743. st->pos = -1;
  2744. script_free_stack (st->stack);
  2745. aFree(st);
  2746. }
  2747. if (bk_st)
  2748. { //Remove previous script
  2749. bk_st->pos = -1;
  2750. script_free_stack(bk_st->stack);
  2751. aFree(bk_st);
  2752. bk_st = NULL;
  2753. }
  2754. }
  2755. /*==========================================
  2756. * マップ変数の変更
  2757. *------------------------------------------
  2758. */
  2759. int mapreg_setreg(int num,int val)
  2760. {
  2761. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2762. int i=num>>24;
  2763. char *name=str_buf+str_data[num&0x00ffffff].str;
  2764. char tmp_str[64];
  2765. #endif
  2766. if(val!=0) {
  2767. if(idb_put(mapreg_db,num,(void*)val))
  2768. ;
  2769. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2770. else if(name[1] != '@') {
  2771. 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);
  2772. if(mysql_query(&mmysql_handle,tmp_sql)){
  2773. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2774. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2775. }
  2776. }
  2777. #endif
  2778. } else { // [zBuffer]
  2779. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2780. if(name[1] != '@') { // Remove from database because it is unused.
  2781. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_varname,name,mapregsql_db_index,i);
  2782. if(mysql_query(&mmysql_handle,tmp_sql)){
  2783. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2784. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2785. }
  2786. }
  2787. #endif
  2788. idb_remove(mapreg_db,num);
  2789. }
  2790. mapreg_dirty=1;
  2791. return 0;
  2792. }
  2793. /*==========================================
  2794. * 文字列型マップ変数の変更
  2795. *------------------------------------------
  2796. */
  2797. int mapreg_setregstr(int num,const char *str)
  2798. {
  2799. char *p;
  2800. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2801. char tmp_str[64];
  2802. char tmp_str2[512];
  2803. int i=num>>24; // [zBuffer]
  2804. char *name=str_buf+str_data[num&0x00ffffff].str;
  2805. #endif
  2806. if( str==NULL || *str==0 ){
  2807. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2808. if(name[1] != '@') {
  2809. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_varname,name,mapregsql_db_index,i);
  2810. if(mysql_query(&mmysql_handle,tmp_sql)){
  2811. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2812. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2813. }
  2814. }
  2815. #endif
  2816. idb_remove(mapregstr_db,num);
  2817. mapreg_dirty=1;
  2818. return 0;
  2819. }
  2820. p=(char *)aMallocA((strlen(str)+1)*sizeof(char));
  2821. strcpy(p,str);
  2822. if (idb_put(mapregstr_db,num,p))
  2823. ;
  2824. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2825. else if(name[1] != '@'){ //put returned null, so we must insert.
  2826. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  2827. 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));
  2828. if(mysql_query(&mmysql_handle,tmp_sql)){
  2829. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2830. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2831. }
  2832. }
  2833. #endif
  2834. mapreg_dirty=1;
  2835. return 0;
  2836. }
  2837. /*==========================================
  2838. * 永続的マップ変数の読み込み
  2839. *------------------------------------------
  2840. */
  2841. static int script_load_mapreg(void)
  2842. {
  2843. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2844. FILE *fp;
  2845. char line[1024];
  2846. if( (fp=fopen(mapreg_txt,"rt"))==NULL )
  2847. return -1;
  2848. while(fgets(line,sizeof(line),fp)){
  2849. char buf1[256],buf2[1024],*p;
  2850. int n,v,s,i;
  2851. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  2852. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  2853. continue;
  2854. if( buf1[strlen(buf1)-1]=='$' ){
  2855. if( sscanf(line+n,"%[^\n\r]",buf2)!=1 ){
  2856. ShowError("%s: %s broken data !\n",mapreg_txt,buf1);
  2857. continue;
  2858. }
  2859. p=(char *)aMallocA((strlen(buf2) + 1)*sizeof(char));
  2860. strcpy(p,buf2);
  2861. s= add_str(buf1);
  2862. idb_put(mapregstr_db,(i<<24)|s,p);
  2863. }else{
  2864. if( sscanf(line+n,"%d",&v)!=1 ){
  2865. ShowError("%s: %s broken data !\n",mapreg_txt,buf1);
  2866. continue;
  2867. }
  2868. s= add_str(buf1);
  2869. idb_put(mapreg_db,(i<<24)|s,(void*)v);
  2870. }
  2871. }
  2872. fclose(fp);
  2873. mapreg_dirty=0;
  2874. return 0;
  2875. #else
  2876. // SQL mapreg code start [zBuffer]
  2877. /*
  2878. 0 1 2
  2879. +-------------------------+
  2880. | varname | index | value |
  2881. +-------------------------+
  2882. */
  2883. unsigned int perfomance = (unsigned int)time(NULL);
  2884. sprintf(tmp_sql,"SELECT * FROM `%s`",mapregsql_db);
  2885. ShowInfo("Querying script_load_mapreg ...\n");
  2886. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  2887. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2888. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2889. return -1;
  2890. }
  2891. ShowInfo("Success! Returning results ...\n");
  2892. sql_res = mysql_store_result(&mmysql_handle);
  2893. if (sql_res) {
  2894. while ((sql_row = mysql_fetch_row(sql_res))) {
  2895. char buf1[33], *p = NULL;
  2896. int i,v,s;
  2897. strcpy(buf1,sql_row[0]);
  2898. if( buf1[strlen(buf1)-1]=='$' ){
  2899. i = atoi(sql_row[1]);
  2900. p=(char *)aMallocA((strlen(sql_row[2]) + 1)*sizeof(char));
  2901. strcpy(p,sql_row[2]);
  2902. s= add_str(buf1);
  2903. idb_put(mapregstr_db,(i<<24)|s,p);
  2904. }else{
  2905. s= add_str(buf1);
  2906. v= atoi(sql_row[2]);
  2907. i = atoi(sql_row[1]);
  2908. idb_put(mapreg_db,(i<<24)|s,(void *)v);
  2909. }
  2910. }
  2911. }
  2912. ShowInfo("Freeing results...\n");
  2913. mysql_free_result(sql_res);
  2914. mapreg_dirty=0;
  2915. perfomance = (((unsigned int)time(NULL)) - perfomance);
  2916. ShowInfo("SQL Mapreg Loading Completed Under %d Seconds.\n",perfomance);
  2917. return 0;
  2918. #endif /* TXT_ONLY */
  2919. }
  2920. /*==========================================
  2921. * 永続的マップ変数の書き込み
  2922. *------------------------------------------
  2923. */
  2924. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  2925. {
  2926. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2927. FILE *fp=va_arg(ap,FILE*);
  2928. int num=key.i&0x00ffffff, i=key.i>>24;
  2929. char *name=str_buf+str_data[num].str;
  2930. if( name[1]!='@' ){
  2931. if(i==0)
  2932. fprintf(fp,"%s\t%d\n", name, (int)data);
  2933. else
  2934. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  2935. }
  2936. return 0;
  2937. #else
  2938. int num=key.i&0x00ffffff, i=key.i>>24; // [zBuffer]
  2939. char *name=str_buf+str_data[num].str;
  2940. if ( name[1] != '@') {
  2941. 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);
  2942. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  2943. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2944. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2945. }
  2946. }
  2947. return 0;
  2948. #endif
  2949. }
  2950. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  2951. {
  2952. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2953. FILE *fp=va_arg(ap,FILE*);
  2954. int num=key.i&0x00ffffff, i=key.i>>24;
  2955. char *name=str_buf+str_data[num].str;
  2956. if( name[1]!='@' ){
  2957. if(i==0)
  2958. fprintf(fp,"%s\t%s\n", name, (char *)data);
  2959. else
  2960. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  2961. }
  2962. return 0;
  2963. #else
  2964. char tmp_str2[512];
  2965. int num=key.i&0x00ffffff, i=key.i>>24;
  2966. char *name=str_buf+str_data[num].str;
  2967. if ( name[1] != '@') {
  2968. 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);
  2969. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  2970. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2971. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2972. }
  2973. }
  2974. return 0;
  2975. #endif
  2976. }
  2977. static int script_save_mapreg(void)
  2978. {
  2979. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  2980. FILE *fp;
  2981. int lock;
  2982. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  2983. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  2984. return -1;
  2985. }
  2986. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  2987. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  2988. lock_fclose(fp,mapreg_txt,&lock);
  2989. #else
  2990. unsigned int perfomance = (unsigned int)time(NULL);
  2991. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub); // [zBuffer]
  2992. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  2993. perfomance = ((unsigned int)time(NULL) - perfomance);
  2994. if(perfomance > 2)
  2995. ShowWarning("Slow Query: MapregSQL Saving @ %d second(s).\n", perfomance);
  2996. #endif
  2997. mapreg_dirty=0;
  2998. return 0;
  2999. }
  3000. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  3001. {
  3002. if(mapreg_dirty)
  3003. if (script_save_mapreg() == -1)
  3004. ShowError("Failed to save the mapreg data!\n");
  3005. return 0;
  3006. }
  3007. /*==========================================
  3008. *
  3009. *------------------------------------------
  3010. */
  3011. static int set_posword(char *p)
  3012. {
  3013. char* np,* str[15];
  3014. int i=0;
  3015. for(i=0;i<11;i++) {
  3016. if((np=strchr(p,','))!=NULL) {
  3017. str[i]=p;
  3018. *np=0;
  3019. p=np+1;
  3020. } else {
  3021. str[i]=p;
  3022. p+=strlen(p);
  3023. }
  3024. if(str[i])
  3025. strcpy(pos[i],str[i]);
  3026. }
  3027. return 0;
  3028. }
  3029. int script_config_read_sub(char *cfgName)
  3030. {
  3031. int i;
  3032. char line[1024],w1[1024],w2[1024];
  3033. FILE *fp;
  3034. fp=fopen(cfgName,"r");
  3035. if(fp==NULL){
  3036. ShowError("file not found: [%s]\n", cfgName);
  3037. return 1;
  3038. }
  3039. while(fgets(line,sizeof(line)-1,fp)){
  3040. if(line[0] == '/' && line[1] == '/')
  3041. continue;
  3042. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3043. if(i!=2)
  3044. continue;
  3045. if(strcmpi(w1,"refine_posword")==0) {
  3046. set_posword(w2);
  3047. }
  3048. else if(strcmpi(w1,"verbose_mode")==0) {
  3049. script_config.verbose_mode = battle_config_switch(w2);
  3050. }
  3051. else if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3052. script_config.warn_func_mismatch_paramnum = battle_config_switch(w2);
  3053. }
  3054. else if(strcmpi(w1,"check_cmdcount")==0) {
  3055. script_config.check_cmdcount = battle_config_switch(w2);
  3056. }
  3057. else if(strcmpi(w1,"check_gotocount")==0) {
  3058. script_config.check_gotocount = battle_config_switch(w2);
  3059. }
  3060. else if(strcmpi(w1,"event_script_type")==0) {
  3061. script_config.event_script_type = battle_config_switch(w2);
  3062. }
  3063. else if(strcmpi(w1,"event_requires_trigger")==0) {
  3064. script_config.event_requires_trigger = battle_config_switch(w2);
  3065. }
  3066. else if(strcmpi(w1,"die_event_name")==0) {
  3067. strncpy(script_config.die_event_name, w2, NAME_LENGTH-1);
  3068. if (strlen(script_config.die_event_name) != strlen(w2))
  3069. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.die_event_name);
  3070. }
  3071. else if(strcmpi(w1,"kill_pc_event_name")==0) {
  3072. strncpy(script_config.kill_pc_event_name, w2, NAME_LENGTH-1);
  3073. if (strlen(script_config.kill_pc_event_name) != strlen(w2))
  3074. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_pc_event_name);
  3075. }
  3076. else if(strcmpi(w1,"kill_mob_event_name")==0) {
  3077. strncpy(script_config.kill_mob_event_name, w2, NAME_LENGTH-1);
  3078. if (strlen(script_config.kill_mob_event_name) != strlen(w2))
  3079. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_mob_event_name);
  3080. }
  3081. else if(strcmpi(w1,"login_event_name")==0) {
  3082. strncpy(script_config.login_event_name, w2, NAME_LENGTH-1);
  3083. if (strlen(script_config.login_event_name) != strlen(w2))
  3084. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.login_event_name);
  3085. }
  3086. else if(strcmpi(w1,"logout_event_name")==0) {
  3087. strncpy(script_config.logout_event_name, w2, NAME_LENGTH-1);
  3088. if (strlen(script_config.logout_event_name) != strlen(w2))
  3089. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.logout_event_name);
  3090. }
  3091. else if(strcmpi(w1,"loadmap_event_name")==0) {
  3092. strncpy(script_config.loadmap_event_name, w2, NAME_LENGTH-1);
  3093. if (strlen(script_config.loadmap_event_name) != strlen(w2))
  3094. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.loadmap_event_name);
  3095. }
  3096. else if(strcmpi(w1,"baselvup_event_name")==0) {
  3097. strncpy(script_config.baselvup_event_name, w2, NAME_LENGTH-1);
  3098. if (strlen(script_config.baselvup_event_name) != strlen(w2))
  3099. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.baselvup_event_name);
  3100. }
  3101. else if(strcmpi(w1,"joblvup_event_name")==0) {
  3102. strncpy(script_config.joblvup_event_name, w2, NAME_LENGTH-1);
  3103. if (strlen(script_config.joblvup_event_name) != strlen(w2))
  3104. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.joblvup_event_name);
  3105. }
  3106. else if(strcmpi(w1,"import")==0){
  3107. script_config_read_sub(w2);
  3108. }
  3109. }
  3110. fclose(fp);
  3111. return 0;
  3112. }
  3113. int script_config_read(char *cfgName)
  3114. { //Script related variables should be initialized once! [Skotlex]
  3115. memset (&script_config, 0, sizeof(script_config));
  3116. script_config.verbose_mode = 0;
  3117. script_config.warn_func_mismatch_paramnum = 1;
  3118. script_config.check_cmdcount = 65535;
  3119. script_config.check_gotocount = 2048;
  3120. script_config.event_script_type = 0;
  3121. script_config.event_requires_trigger = 1;
  3122. return script_config_read_sub(cfgName);
  3123. }
  3124. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3125. {
  3126. struct script_code *code = (struct script_code *)data;
  3127. if(code){
  3128. script_free_vars( &code->script_vars );
  3129. aFree( code->script_buf );
  3130. }
  3131. return 0;
  3132. }
  3133. /*==========================================
  3134. * 終了
  3135. *------------------------------------------
  3136. */
  3137. int do_final_script()
  3138. {
  3139. if (battle_config.etc_log)
  3140. {
  3141. FILE *fp = fopen("hash_dump.txt","wt");
  3142. if(fp) {
  3143. int i,count[SCRIPT_HASH_SIZE];
  3144. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3145. int n=0;
  3146. int min=INT_MAX,max=0,zero=0;
  3147. double mean=0.0f;
  3148. double median=0.0f;
  3149. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3150. memset(count, 0, sizeof(count));
  3151. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3152. fprintf(fp,"---------------------------------------------------------------\n");
  3153. for(i=LABEL_START; i<str_num; i++) {
  3154. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3155. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3156. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3157. ++count[h];
  3158. }
  3159. fprintf(fp,"--------------------\n\n");
  3160. memset(count2, 0, sizeof(count2));
  3161. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3162. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3163. if(min > count[i])
  3164. min = count[i]; // minimun count of collision
  3165. if(max < count[i])
  3166. max = count[i]; // maximun count of collision
  3167. if(count[i] == 0)
  3168. zero++;
  3169. ++count2[count[i]];
  3170. }
  3171. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3172. for( i=min; i <= max; ++i ){
  3173. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3174. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3175. }
  3176. for( i=min; i <= max; ++i ){
  3177. n += count2[i];
  3178. if( n*2 >= SCRIPT_HASH_SIZE )
  3179. {
  3180. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3181. median = (i+i+1)/2.0f;
  3182. else
  3183. median = i;
  3184. break;
  3185. }
  3186. }
  3187. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3188. fclose(fp);
  3189. }
  3190. }
  3191. if(mapreg_dirty>=0)
  3192. script_save_mapreg();
  3193. mapreg_db->destroy(mapreg_db,NULL);
  3194. mapregstr_db->destroy(mapregstr_db,NULL);
  3195. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3196. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3197. if(sleep_db) {
  3198. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3199. while(n) {
  3200. struct script_state *st = (struct script_state *)n->data;
  3201. script_free_stack(st->stack);
  3202. aFree(st);
  3203. n = n->next;
  3204. }
  3205. linkdb_final(&sleep_db);
  3206. }
  3207. if (str_data)
  3208. aFree(str_data);
  3209. if (str_buf)
  3210. aFree(str_buf);
  3211. return 0;
  3212. }
  3213. /*==========================================
  3214. * 初期化
  3215. *------------------------------------------
  3216. */
  3217. int do_init_script()
  3218. {
  3219. mapreg_db= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_BASE,sizeof(int));
  3220. mapregstr_db=db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3221. userfunc_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_RELEASE_BOTH,50);
  3222. scriptlabel_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA,50);
  3223. script_load_mapreg();
  3224. add_timer_func_list(script_autosave_mapreg,"script_autosave_mapreg");
  3225. add_timer_interval(gettick()+MAPREG_AUTOSAVE_INTERVAL,
  3226. script_autosave_mapreg,0,0,MAPREG_AUTOSAVE_INTERVAL);
  3227. return 0;
  3228. }
  3229. int script_reload()
  3230. {
  3231. if(mapreg_dirty>=0)
  3232. script_save_mapreg();
  3233. mapreg_db->clear(mapreg_db, NULL);
  3234. mapregstr_db->clear(mapregstr_db, NULL);
  3235. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3236. scriptlabel_db->clear(scriptlabel_db, NULL);
  3237. if(sleep_db) {
  3238. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3239. while(n) {
  3240. struct script_state *st = (struct script_state *)n->data;
  3241. if( st->sleep.timer != INVALID_TIMER )
  3242. delete_timer(st->sleep.timer, run_script_timer);
  3243. script_free_stack(st->stack);
  3244. aFree(st);
  3245. n = n->next;
  3246. }
  3247. linkdb_final(&sleep_db);
  3248. }
  3249. script_load_mapreg();
  3250. return 0;
  3251. }
  3252. int buildin_mes(struct script_state *st);
  3253. int buildin_goto(struct script_state *st);
  3254. int buildin_callsub(struct script_state *st);
  3255. int buildin_callfunc(struct script_state *st);
  3256. int buildin_return(struct script_state *st);
  3257. int buildin_getarg(struct script_state *st);
  3258. int buildin_next(struct script_state *st);
  3259. int buildin_close(struct script_state *st);
  3260. int buildin_close2(struct script_state *st);
  3261. int buildin_menu(struct script_state *st);
  3262. int buildin_rand(struct script_state *st);
  3263. int buildin_warp(struct script_state *st);
  3264. int buildin_areawarp(struct script_state *st);
  3265. int buildin_warpchar(struct script_state *st); // [LuzZza]
  3266. int buildin_warpparty(struct script_state *st); //[Fredzilla]
  3267. int buildin_warpguild(struct script_state *st); //[Fredzilla]
  3268. int buildin_heal(struct script_state *st);
  3269. int buildin_itemheal(struct script_state *st);
  3270. int buildin_percentheal(struct script_state *st);
  3271. int buildin_jobchange(struct script_state *st);
  3272. int buildin_jobname(struct script_state *st);
  3273. int buildin_input(struct script_state *st);
  3274. int buildin_setlook(struct script_state *st);
  3275. int buildin_set(struct script_state *st);
  3276. int buildin_setarray(struct script_state *st);
  3277. int buildin_cleararray(struct script_state *st);
  3278. int buildin_copyarray(struct script_state *st);
  3279. int buildin_getarraysize(struct script_state *st);
  3280. int buildin_deletearray(struct script_state *st);
  3281. int buildin_getelementofarray(struct script_state *st);
  3282. int buildin_getitem(struct script_state *st);
  3283. int buildin_getitem2(struct script_state *st);
  3284. int buildin_getnameditem(struct script_state *st);
  3285. int buildin_grouprandomitem(struct script_state *st);
  3286. int buildin_makeitem(struct script_state *st);
  3287. int buildin_delitem(struct script_state *st);
  3288. int buildin_delitem2(struct script_state *st);
  3289. int buildin_enableitemuse(struct script_state *st);
  3290. int buildin_disableitemuse(struct script_state *st);
  3291. int buildin_viewpoint(struct script_state *st);
  3292. int buildin_countitem(struct script_state *st);
  3293. int buildin_countitem2(struct script_state *st);
  3294. int buildin_checkweight(struct script_state *st);
  3295. int buildin_readparam(struct script_state *st);
  3296. int buildin_getcharid(struct script_state *st);
  3297. int buildin_getpartyname(struct script_state *st);
  3298. int buildin_getpartymember(struct script_state *st);
  3299. int buildin_getpartyleader(struct script_state *st);
  3300. int buildin_getguildname(struct script_state *st);
  3301. int buildin_getguildmaster(struct script_state *st);
  3302. int buildin_getguildmasterid(struct script_state *st);
  3303. int buildin_strcharinfo(struct script_state *st);
  3304. int buildin_getequipid(struct script_state *st);
  3305. int buildin_getequipname(struct script_state *st);
  3306. int buildin_getbrokenid(struct script_state *st); // [Valaris]
  3307. int buildin_repair(struct script_state *st); // [Valaris]
  3308. int buildin_getequipisequiped(struct script_state *st);
  3309. int buildin_getequipisenableref(struct script_state *st);
  3310. int buildin_getequipisidentify(struct script_state *st);
  3311. int buildin_getequiprefinerycnt(struct script_state *st);
  3312. int buildin_getequipweaponlv(struct script_state *st);
  3313. int buildin_getequippercentrefinery(struct script_state *st);
  3314. int buildin_successrefitem(struct script_state *st);
  3315. int buildin_failedrefitem(struct script_state *st);
  3316. int buildin_cutin(struct script_state *st);
  3317. int buildin_statusup(struct script_state *st);
  3318. int buildin_statusup2(struct script_state *st);
  3319. int buildin_bonus(struct script_state *st);
  3320. int buildin_bonus2(struct script_state *st);
  3321. int buildin_bonus3(struct script_state *st);
  3322. int buildin_bonus4(struct script_state *st);
  3323. int buildin_skill(struct script_state *st);
  3324. int buildin_addtoskill(struct script_state *st); // [Valaris]
  3325. int buildin_guildskill(struct script_state *st);
  3326. int buildin_getskilllv(struct script_state *st);
  3327. int buildin_getgdskilllv(struct script_state *st);
  3328. int buildin_basicskillcheck(struct script_state *st);
  3329. int buildin_getgmlevel(struct script_state *st);
  3330. int buildin_end(struct script_state *st);
  3331. int buildin_checkoption(struct script_state *st);
  3332. int buildin_setoption(struct script_state *st);
  3333. int buildin_setcart(struct script_state *st);
  3334. int buildin_checkcart(struct script_state *st); // check cart [Valaris]
  3335. int buildin_setfalcon(struct script_state *st);
  3336. int buildin_checkfalcon(struct script_state *st); // check falcon [Valaris]
  3337. int buildin_setriding(struct script_state *st);
  3338. int buildin_checkriding(struct script_state *st); // check for pecopeco [Valaris]
  3339. int buildin_savepoint(struct script_state *st);
  3340. int buildin_gettimetick(struct script_state *st);
  3341. int buildin_gettime(struct script_state *st);
  3342. int buildin_gettimestr(struct script_state *st);
  3343. int buildin_openstorage(struct script_state *st);
  3344. int buildin_guildopenstorage(struct script_state *st);
  3345. int buildin_itemskill(struct script_state *st);
  3346. int buildin_produce(struct script_state *st);
  3347. int buildin_monster(struct script_state *st);
  3348. int buildin_areamonster(struct script_state *st);
  3349. int buildin_killmonster(struct script_state *st);
  3350. int buildin_killmonsterall(struct script_state *st);
  3351. int buildin_clone(struct script_state *st);
  3352. int buildin_doevent(struct script_state *st);
  3353. int buildin_donpcevent(struct script_state *st);
  3354. int buildin_addtimer(struct script_state *st);
  3355. int buildin_deltimer(struct script_state *st);
  3356. int buildin_addtimercount(struct script_state *st);
  3357. int buildin_initnpctimer(struct script_state *st);
  3358. int buildin_stopnpctimer(struct script_state *st);
  3359. int buildin_startnpctimer(struct script_state *st);
  3360. int buildin_setnpctimer(struct script_state *st);
  3361. int buildin_getnpctimer(struct script_state *st);
  3362. int buildin_attachnpctimer(struct script_state *st); // [celest]
  3363. int buildin_detachnpctimer(struct script_state *st); // [celest]
  3364. int buildin_playerattached(struct script_state *st); // [Skotlex]
  3365. int buildin_announce(struct script_state *st);
  3366. int buildin_mapannounce(struct script_state *st);
  3367. int buildin_areaannounce(struct script_state *st);
  3368. int buildin_getusers(struct script_state *st);
  3369. int buildin_getmapguildusers(struct script_state *st);
  3370. int buildin_getmapusers(struct script_state *st);
  3371. int buildin_getareausers(struct script_state *st);
  3372. int buildin_getareadropitem(struct script_state *st);
  3373. int buildin_enablenpc(struct script_state *st);
  3374. int buildin_disablenpc(struct script_state *st);
  3375. int buildin_enablearena(struct script_state *st); // Added by RoVeRT
  3376. int buildin_disablearena(struct script_state *st); // Added by RoVeRT
  3377. int buildin_hideoffnpc(struct script_state *st);
  3378. int buildin_hideonnpc(struct script_state *st);
  3379. int buildin_sc_start(struct script_state *st);
  3380. int buildin_sc_start2(struct script_state *st);
  3381. int buildin_sc_start4(struct script_state *st);
  3382. int buildin_sc_end(struct script_state *st);
  3383. int buildin_getscrate(struct script_state *st);
  3384. int buildin_debugmes(struct script_state *st);
  3385. int buildin_catchpet(struct script_state *st);
  3386. int buildin_birthpet(struct script_state *st);
  3387. int buildin_resetlvl(struct script_state *st);
  3388. int buildin_resetstatus(struct script_state *st);
  3389. int buildin_resetskill(struct script_state *st);
  3390. int buildin_skillpointcount(struct script_state *st);
  3391. int buildin_changebase(struct script_state *st);
  3392. int buildin_changesex(struct script_state *st);
  3393. int buildin_waitingroom(struct script_state *st);
  3394. int buildin_delwaitingroom(struct script_state *st);
  3395. int buildin_waitingroomkickall(struct script_state *st);
  3396. int buildin_enablewaitingroomevent(struct script_state *st);
  3397. int buildin_disablewaitingroomevent(struct script_state *st);
  3398. int buildin_getwaitingroomstate(struct script_state *st);
  3399. int buildin_warpwaitingpc(struct script_state *st);
  3400. int buildin_attachrid(struct script_state *st);
  3401. int buildin_detachrid(struct script_state *st);
  3402. int buildin_isloggedin(struct script_state *st);
  3403. int buildin_setmapflagnosave(struct script_state *st);
  3404. int buildin_setmapflag(struct script_state *st);
  3405. int buildin_removemapflag(struct script_state *st);
  3406. int buildin_pvpon(struct script_state *st);
  3407. int buildin_pvpoff(struct script_state *st);
  3408. int buildin_gvgon(struct script_state *st);
  3409. int buildin_gvgoff(struct script_state *st);
  3410. int buildin_emotion(struct script_state *st);
  3411. int buildin_maprespawnguildid(struct script_state *st);
  3412. int buildin_agitstart(struct script_state *st); // <Agit>
  3413. int buildin_agitend(struct script_state *st);
  3414. int buildin_agitcheck(struct script_state *st); // <Agitcheck>
  3415. int buildin_flagemblem(struct script_state *st); // Flag Emblem
  3416. int buildin_getcastlename(struct script_state *st);
  3417. int buildin_getcastledata(struct script_state *st);
  3418. int buildin_setcastledata(struct script_state *st);
  3419. int buildin_requestguildinfo(struct script_state *st);
  3420. int buildin_getequipcardcnt(struct script_state *st);
  3421. int buildin_successremovecards(struct script_state *st);
  3422. int buildin_failedremovecards(struct script_state *st);
  3423. int buildin_marriage(struct script_state *st);
  3424. int buildin_wedding_effect(struct script_state *st);
  3425. int buildin_divorce(struct script_state *st);
  3426. int buildin_ispartneron(struct script_state *st); // MouseJstr
  3427. int buildin_getpartnerid(struct script_state *st); // MouseJstr
  3428. int buildin_getchildid(struct script_state *st); // Skotlex
  3429. int buildin_getmotherid(struct script_state *st); // Lupus
  3430. int buildin_getfatherid(struct script_state *st); // Lupus
  3431. int buildin_warppartner(struct script_state *st); // MouseJstr
  3432. int buildin_getitemname(struct script_state *st);
  3433. int buildin_getitemslots(struct script_state *st);
  3434. int buildin_makepet(struct script_state *st);
  3435. int buildin_getexp(struct script_state *st);
  3436. int buildin_getinventorylist(struct script_state *st);
  3437. int buildin_getskilllist(struct script_state *st);
  3438. int buildin_clearitem(struct script_state *st);
  3439. int buildin_classchange(struct script_state *st);
  3440. int buildin_misceffect(struct script_state *st);
  3441. int buildin_soundeffect(struct script_state *st);
  3442. int buildin_soundeffectall(struct script_state *st);
  3443. int buildin_setcastledata(struct script_state *st);
  3444. int buildin_mapwarp(struct script_state *st);
  3445. int buildin_inittimer(struct script_state *st);
  3446. int buildin_stoptimer(struct script_state *st);
  3447. int buildin_cmdothernpc(struct script_state *st);
  3448. int buildin_mobcount(struct script_state *st);
  3449. int buildin_strmobinfo(struct script_state *st); // Script for displaying mob info [Valaris]
  3450. int buildin_guardian(struct script_state *st); // Script for displaying mob info [Valaris]
  3451. int buildin_guardianinfo(struct script_state *st); // Script for displaying mob info [Valaris]
  3452. int buildin_petskillbonus(struct script_state *st); // petskillbonus [Valaris]
  3453. int buildin_petrecovery(struct script_state *st); // pet skill for curing status [Valaris]
  3454. int buildin_petloot(struct script_state *st); // pet looting [Valaris]
  3455. int buildin_petheal(struct script_state *st); // pet healing [Valaris]
  3456. //int buildin_petmag(struct script_state *st); // pet magnificat [Valaris]
  3457. int buildin_petskillattack(struct script_state *st); // pet skill attacks [Skotlex]
  3458. int buildin_petskillattack2(struct script_state *st); // pet skill attacks [Skotlex]
  3459. int buildin_petskillsupport(struct script_state *st); // pet support skill [Valaris]
  3460. int buildin_skilleffect(struct script_state *st); // skill effects [Celest]
  3461. int buildin_npcskilleffect(struct script_state *st); // skill effects for npcs [Valaris]
  3462. int buildin_specialeffect(struct script_state *st); // special effect script [Valaris]
  3463. int buildin_specialeffect2(struct script_state *st); // special effect script [Valaris]
  3464. int buildin_nude(struct script_state *st); // nude [Valaris]
  3465. int buildin_atcommand(struct script_state *st); // [MouseJstr]
  3466. int buildin_charcommand(struct script_state *st); // [MouseJstr]
  3467. int buildin_movenpc(struct script_state *st); // [MouseJstr]
  3468. int buildin_message(struct script_state *st); // [MouseJstr]
  3469. int buildin_npctalk(struct script_state *st); // [Valaris]
  3470. int buildin_hasitems(struct script_state *st); // [Valaris]
  3471. int buildin_getlook(struct script_state *st); //Lorky [Lupus]
  3472. int buildin_getsavepoint(struct script_state *st); //Lorky [Lupus]
  3473. int buildin_npcspeed(struct script_state *st); // [Valaris]
  3474. int buildin_npcwalkto(struct script_state *st); // [Valaris]
  3475. int buildin_npcstop(struct script_state *st); // [Valaris]
  3476. int buildin_getmapxy(struct script_state *st); //get map position for player/npc/pet/mob by Lorky [Lupus]
  3477. int buildin_checkoption1(struct script_state *st); // [celest]
  3478. int buildin_checkoption2(struct script_state *st); // [celest]
  3479. int buildin_guildgetexp(struct script_state *st); // [celest]
  3480. int buildin_guildchangegm(struct script_state *st); // [Skotlex]
  3481. int buildin_logmes(struct script_state *st); // [Lupus]
  3482. int buildin_summon(struct script_state *st); // [celest]
  3483. int buildin_isnight(struct script_state *st); // [celest]
  3484. int buildin_isday(struct script_state *st); // [celest]
  3485. int buildin_isequipped(struct script_state *st); // [celest]
  3486. int buildin_isequippedcnt(struct script_state *st); // [celest]
  3487. int buildin_cardscnt(struct script_state *st); // [Lupus]
  3488. int buildin_getrefine(struct script_state *st); // [celest]
  3489. int buildin_adopt(struct script_state *st);
  3490. int buildin_night(struct script_state *st);
  3491. int buildin_day(struct script_state *st);
  3492. int buildin_getusersname(struct script_state *st); //jA commands added [Lupus]
  3493. int buildin_dispbottom(struct script_state *st);
  3494. int buildin_recovery(struct script_state *st);
  3495. int buildin_getpetinfo(struct script_state *st);
  3496. int buildin_checkequipedcard(struct script_state *st);
  3497. int buildin_globalmes(struct script_state *st);
  3498. int buildin_jump_zero(struct script_state *st);
  3499. int buildin_select(struct script_state *st);
  3500. int buildin_prompt(struct script_state *st);
  3501. int buildin_getmapmobs(struct script_state *st); //jA addition end
  3502. int buildin_unequip(struct script_state *st); // unequip [Spectre]
  3503. int buildin_getstrlen(struct script_state *st); //strlen [valaris]
  3504. int buildin_charisalpha(struct script_state *st);//isalpha [valaris]
  3505. int buildin_fakenpcname(struct script_state *st); // [Lance]
  3506. int buildin_compare(struct script_state *st); // Lordalfa, to bring strstr to Scripting Engine
  3507. int buildin_getiteminfo(struct script_state *st); //[Lupus] returns Items Buy / sell Price, etc info
  3508. int buildin_getequipcardid(struct script_state *st); //[Lupus] returns card id from quipped item card slot N
  3509. // [zBuffer] List of mathematics commands --->
  3510. int buildin_sqrt(struct script_state *st);
  3511. int buildin_pow(struct script_state *st);
  3512. int buildin_distance(struct script_state *st);
  3513. int buildin_checkcell(struct script_state *st);
  3514. // <--- [zBuffer] List of mathematics commands
  3515. // [zBuffer] List of dynamic var commands --->
  3516. int buildin_getd(struct script_state *st);
  3517. int buildin_setd(struct script_state *st);
  3518. // <--- [zBuffer] List of dynamic var commands
  3519. int buildin_petstat(struct script_state *st); // [Lance] Pet Stat Rq: Dubby
  3520. int buildin_callshop(struct script_state *st); // [Skotlex]
  3521. int buildin_npcshopitem(struct script_state *st); // [Lance]
  3522. int buildin_npcshopadditem(struct script_state *st);
  3523. int buildin_npcshopdelitem(struct script_state *st);
  3524. int buildin_npcshopattach(struct script_state *st);
  3525. int buildin_equip(struct script_state *st);
  3526. int buildin_autoequip(struct script_state *st);
  3527. int buildin_setbattleflag(struct script_state *st);
  3528. int buildin_getbattleflag(struct script_state *st);
  3529. int buildin_query_sql(struct script_state *st);
  3530. int buildin_escape_sql(struct script_state *st);
  3531. int buildin_atoi(struct script_state *st);
  3532. int buildin_axtoi(struct script_state *st);
  3533. // [zBuffer] List of player cont commands --->
  3534. int buildin_rid2name(struct script_state *st);
  3535. int buildin_pcfollow(struct script_state *st);
  3536. int buildin_pcstopfollow(struct script_state *st);
  3537. int buildin_pcblockmove(struct script_state *st);
  3538. // <--- [zBuffer] List of player cont commands
  3539. // [zBuffer] List of mob control commands --->
  3540. int buildin_mobspawn(struct script_state *st);
  3541. int buildin_mobremove(struct script_state *st);
  3542. int buildin_getmobdata(struct script_state *st);
  3543. int buildin_setmobdata(struct script_state *st);
  3544. int buildin_mobassist(struct script_state *st);
  3545. int buildin_mobattach(struct script_state *st);
  3546. int buildin_unitwalk(struct script_state *st);
  3547. int buildin_unitkill(struct script_state *st);
  3548. int buildin_unitwarp(struct script_state *st);
  3549. int buildin_unitattack(struct script_state *st);
  3550. int buildin_unitstop(struct script_state *st);
  3551. int buildin_unittalk(struct script_state *st);
  3552. int buildin_unitemote(struct script_state *st);
  3553. int buildin_unitdeadsit(struct script_state *st);
  3554. int buildin_unitskilluseid(struct script_state *st); // originally by Qamera [celest]
  3555. int buildin_unitskillusepos(struct script_state *st); // originally by Qamera [celest]
  3556. // <--- [zBuffer] List of mob control commands
  3557. int buildin_sleep(struct script_state *st);
  3558. int buildin_sleep2(struct script_state *st);
  3559. int buildin_awake(struct script_state *st);
  3560. int buildin_getvariableofnpc(struct script_state *st);
  3561. // [blackhole89] -->
  3562. int buildin_warpportal(struct script_state *st);
  3563. // <-- [blackhole89]
  3564. int buildin_homunculus_evolution(struct script_state *st) ; //[orn]
  3565. int buildin_eaclass(struct script_state *st);
  3566. int buildin_roclass(struct script_state *st);
  3567. int buildin_setitemscript(struct script_state *st);
  3568. int buildin_disguise(struct script_state *st);
  3569. int buildin_undisguise(struct script_state *st);
  3570. int buildin_getmonsterinfo(struct script_state *st); // [Lupus]
  3571. int buildin_checkvending(struct script_state *st); // check vending [Nab4]
  3572. int buildin_checkchatting(struct script_state *st); // check chatting [Marka]
  3573. #ifdef PCRE_SUPPORT
  3574. int buildin_defpattern(struct script_state *st); // MouseJstr
  3575. int buildin_activatepset(struct script_state *st); // MouseJstr
  3576. int buildin_deactivatepset(struct script_state *st); // MouseJstr
  3577. int buildin_deletepset(struct script_state *st); // MouseJstr
  3578. #endif
  3579. struct script_function buildin_func[] = {
  3580. {buildin_mes,"mes","s"},
  3581. {buildin_next,"next",""},
  3582. {buildin_close,"close",""},
  3583. {buildin_close2,"close2",""},
  3584. {buildin_menu,"menu","*"},
  3585. {buildin_goto,"goto","l"},
  3586. {buildin_callsub,"callsub","i*"},
  3587. {buildin_callfunc,"callfunc","s*"},
  3588. {buildin_return,"return","*"},
  3589. {buildin_getarg,"getarg","i"},
  3590. {buildin_jobchange,"jobchange","i*"},
  3591. {buildin_jobname,"jobname","i"},
  3592. {buildin_input,"input","*"},
  3593. {buildin_warp,"warp","sii"},
  3594. {buildin_areawarp,"areawarp","siiiisii"},
  3595. {buildin_warpchar,"warpchar","siii"}, // [LuzZza]
  3596. {buildin_warpparty,"warpparty","siii"}, // [Fredzilla]
  3597. {buildin_warpguild,"warpguild","siii"}, // [Fredzilla]
  3598. {buildin_setlook,"setlook","ii"},
  3599. {buildin_set,"set","ii"},
  3600. {buildin_setarray,"setarray","ii*"},
  3601. {buildin_cleararray,"cleararray","iii"},
  3602. {buildin_copyarray,"copyarray","iii"},
  3603. {buildin_getarraysize,"getarraysize","i"},
  3604. {buildin_deletearray,"deletearray","ii"},
  3605. {buildin_getelementofarray,"getelementofarray","ii"},
  3606. {buildin_getitem,"getitem","vi?"},
  3607. {buildin_getitem2,"getitem2","iiiiiiiii*"},
  3608. {buildin_getnameditem,"getnameditem","is"},
  3609. {buildin_grouprandomitem,"groupranditem","i"},
  3610. {buildin_makeitem,"makeitem","iisii"},
  3611. {buildin_delitem,"delitem","ii"},
  3612. {buildin_delitem2,"delitem2","iiiiiiiii"},
  3613. {buildin_enableitemuse,"enable_items",""},
  3614. {buildin_disableitemuse,"disable_items",""},
  3615. {buildin_cutin,"cutin","si"},
  3616. {buildin_viewpoint,"viewpoint","iiiii"},
  3617. {buildin_heal,"heal","ii"},
  3618. {buildin_itemheal,"itemheal","ii"},
  3619. {buildin_percentheal,"percentheal","ii"},
  3620. {buildin_rand,"rand","i*"},
  3621. {buildin_countitem,"countitem","i"},
  3622. {buildin_countitem2,"countitem2","iiiiiiii"},
  3623. {buildin_checkweight,"checkweight","ii"},
  3624. {buildin_readparam,"readparam","i*"},
  3625. {buildin_getcharid,"getcharid","i*"},
  3626. {buildin_getpartyname,"getpartyname","i"},
  3627. {buildin_getpartymember,"getpartymember","i*"},
  3628. {buildin_getpartyleader,"getpartyleader","i*"},
  3629. {buildin_getguildname,"getguildname","i"},
  3630. {buildin_getguildmaster,"getguildmaster","i"},
  3631. {buildin_getguildmasterid,"getguildmasterid","i"},
  3632. {buildin_strcharinfo,"strcharinfo","i"},
  3633. {buildin_getequipid,"getequipid","i"},
  3634. {buildin_getequipname,"getequipname","i"},
  3635. {buildin_getbrokenid,"getbrokenid","i"}, // [Valaris]
  3636. {buildin_repair,"repair","i"}, // [Valaris]
  3637. {buildin_getequipisequiped,"getequipisequiped","i"},
  3638. {buildin_getequipisenableref,"getequipisenableref","i"},
  3639. {buildin_getequipisidentify,"getequipisidentify","i"},
  3640. {buildin_getequiprefinerycnt,"getequiprefinerycnt","i"},
  3641. {buildin_getequipweaponlv,"getequipweaponlv","i"},
  3642. {buildin_getequippercentrefinery,"getequippercentrefinery","i"},
  3643. {buildin_successrefitem,"successrefitem","i"},
  3644. {buildin_failedrefitem,"failedrefitem","i"},
  3645. {buildin_statusup,"statusup","i"},
  3646. {buildin_statusup2,"statusup2","ii"},
  3647. {buildin_bonus,"bonus","ii"},
  3648. {buildin_bonus,"bonus2","iii"},
  3649. {buildin_bonus,"bonus3","iiii"},
  3650. {buildin_bonus,"bonus4","iiiii"},
  3651. {buildin_skill,"skill","ii?"},
  3652. {buildin_addtoskill,"addtoskill","ii?"}, // [Valaris]
  3653. {buildin_guildskill,"guildskill","ii"},
  3654. {buildin_getskilllv,"getskilllv","i"},
  3655. {buildin_getgdskilllv,"getgdskilllv","ii"},
  3656. {buildin_basicskillcheck,"basicskillcheck",""},
  3657. {buildin_getgmlevel,"getgmlevel",""},
  3658. {buildin_end,"end",""},
  3659. // {buildin_end,"break",""}, this might confuse advanced scripting support [Eoe]
  3660. {buildin_checkoption,"checkoption","i"},
  3661. {buildin_setoption,"setoption","i?"},
  3662. {buildin_setcart,"setcart","?"},
  3663. {buildin_checkcart,"checkcart",""},
  3664. {buildin_setfalcon,"setfalcon","?"},
  3665. {buildin_checkfalcon,"checkfalcon",""},
  3666. {buildin_setriding,"setriding","?"},
  3667. {buildin_checkriding,"checkriding",""},
  3668. {buildin_savepoint,"save","sii"},
  3669. {buildin_savepoint,"savepoint","sii"},
  3670. {buildin_gettimetick,"gettimetick","i"},
  3671. {buildin_gettime,"gettime","i"},
  3672. {buildin_gettimestr,"gettimestr","si"},
  3673. {buildin_openstorage,"openstorage",""},
  3674. {buildin_guildopenstorage,"guildopenstorage","*"},
  3675. {buildin_itemskill,"itemskill","iis"},
  3676. {buildin_produce,"produce","i"},
  3677. {buildin_monster,"monster","siisii*"},
  3678. {buildin_areamonster,"areamonster","siiiisii*"},
  3679. {buildin_killmonster,"killmonster","ss"},
  3680. {buildin_killmonsterall,"killmonsterall","s"},
  3681. {buildin_clone,"clone","siisi*"},
  3682. {buildin_doevent,"doevent","s"},
  3683. {buildin_donpcevent,"donpcevent","s"},
  3684. {buildin_addtimer,"addtimer","is"},
  3685. {buildin_deltimer,"deltimer","s"},
  3686. {buildin_addtimercount,"addtimercount","si"},
  3687. {buildin_initnpctimer,"initnpctimer","*"},
  3688. {buildin_stopnpctimer,"stopnpctimer","*"},
  3689. {buildin_startnpctimer,"startnpctimer","*"},
  3690. {buildin_setnpctimer,"setnpctimer","*"},
  3691. {buildin_getnpctimer,"getnpctimer","i*"},
  3692. {buildin_attachnpctimer,"attachnpctimer","*"}, // attached the player id to the npc timer [Celest]
  3693. {buildin_detachnpctimer,"detachnpctimer","*"}, // detached the player id from the npc timer [Celest]
  3694. {buildin_playerattached,"playerattached",""}, // returns id of the current attached player. [Skotlex]
  3695. {buildin_announce,"announce","si*"},
  3696. {buildin_mapannounce,"mapannounce","ssi*"},
  3697. {buildin_areaannounce,"areaannounce","siiiisi*"},
  3698. {buildin_getusers,"getusers","i"},
  3699. {buildin_getmapguildusers,"getmapguildusers","si"},
  3700. {buildin_getmapusers,"getmapusers","s"},
  3701. {buildin_getareausers,"getareausers","siiii"},
  3702. {buildin_getareadropitem,"getareadropitem","siiiii"},
  3703. {buildin_enablenpc,"enablenpc","s"},
  3704. {buildin_disablenpc,"disablenpc","s"},
  3705. {buildin_enablearena,"enablearena",""}, // Added by RoVeRT
  3706. {buildin_disablearena,"disablearena",""}, // Added by RoVeRT
  3707. {buildin_hideoffnpc,"hideoffnpc","s"},
  3708. {buildin_hideonnpc,"hideonnpc","s"},
  3709. {buildin_sc_start,"sc_start","iii*"},
  3710. {buildin_sc_start2,"sc_start2","iiii*"},
  3711. {buildin_sc_start4,"sc_start4","iiiiii*"},
  3712. {buildin_sc_end,"sc_end","i"},
  3713. {buildin_getscrate,"getscrate","ii*"},
  3714. {buildin_debugmes,"debugmes","s"},
  3715. {buildin_catchpet,"pet","i"},
  3716. {buildin_birthpet,"bpet",""},
  3717. {buildin_resetlvl,"resetlvl","i"},
  3718. {buildin_resetstatus,"resetstatus",""},
  3719. {buildin_resetskill,"resetskill",""},
  3720. {buildin_skillpointcount,"skillpointcount",""},
  3721. {buildin_changebase,"changebase","i"},
  3722. {buildin_changesex,"changesex",""},
  3723. {buildin_waitingroom,"waitingroom","si*"},
  3724. {buildin_warpwaitingpc,"warpwaitingpc","sii"},
  3725. {buildin_delwaitingroom,"delwaitingroom","*"},
  3726. {buildin_waitingroomkickall,"kickwaitingroomall","*"},
  3727. {buildin_enablewaitingroomevent,"enablewaitingroomevent","*"},
  3728. {buildin_disablewaitingroomevent,"disablewaitingroomevent","*"},
  3729. {buildin_getwaitingroomstate,"getwaitingroomstate","i*"},
  3730. {buildin_warpwaitingpc,"warpwaitingpc","sii*"},
  3731. {buildin_attachrid,"attachrid","i"},
  3732. {buildin_detachrid,"detachrid",""},
  3733. {buildin_isloggedin,"isloggedin","i"},
  3734. {buildin_setmapflagnosave,"setmapflagnosave","ssii"},
  3735. {buildin_setmapflag,"setmapflag","si*"},
  3736. {buildin_removemapflag,"removemapflag","si"},
  3737. {buildin_pvpon,"pvpon","s"},
  3738. {buildin_pvpoff,"pvpoff","s"},
  3739. {buildin_gvgon,"gvgon","s"},
  3740. {buildin_gvgoff,"gvgoff","s"},
  3741. {buildin_emotion,"emotion","i*"},
  3742. {buildin_maprespawnguildid,"maprespawnguildid","sii"},
  3743. {buildin_agitstart,"agitstart",""}, // <Agit>
  3744. {buildin_agitend,"agitend",""},
  3745. {buildin_agitcheck,"agitcheck",""}, // <Agitcheck>
  3746. {buildin_flagemblem,"flagemblem","i"}, // Flag Emblem
  3747. {buildin_getcastlename,"getcastlename","s"},
  3748. {buildin_getcastledata,"getcastledata","si*"},
  3749. {buildin_setcastledata,"setcastledata","sii"},
  3750. {buildin_requestguildinfo,"requestguildinfo","i*"},
  3751. {buildin_getequipcardcnt,"getequipcardcnt","i"},
  3752. {buildin_successremovecards,"successremovecards","i"},
  3753. {buildin_failedremovecards,"failedremovecards","ii"},
  3754. {buildin_marriage,"marriage","s"},
  3755. {buildin_wedding_effect,"wedding",""},
  3756. {buildin_divorce,"divorce",""},
  3757. {buildin_ispartneron,"ispartneron",""},
  3758. {buildin_getpartnerid,"getpartnerid",""},
  3759. {buildin_getchildid,"getchildid",""},
  3760. {buildin_getmotherid,"getmotherid",""},
  3761. {buildin_getfatherid,"getfatherid",""},
  3762. {buildin_warppartner,"warppartner","sii"},
  3763. {buildin_getitemname,"getitemname","i"},
  3764. {buildin_getitemslots,"getitemslots","i"},
  3765. {buildin_makepet,"makepet","i"},
  3766. {buildin_getexp,"getexp","ii"},
  3767. {buildin_getinventorylist,"getinventorylist",""},
  3768. {buildin_getskilllist,"getskilllist",""},
  3769. {buildin_clearitem,"clearitem",""},
  3770. {buildin_classchange,"classchange","ii"},
  3771. {buildin_misceffect,"misceffect","i"},
  3772. {buildin_soundeffect,"soundeffect","si"},
  3773. {buildin_soundeffectall,"soundeffectall","si*"}, // SoundEffectAll [Codemaster]
  3774. {buildin_strmobinfo,"strmobinfo","ii"}, // display mob data [Valaris]
  3775. {buildin_guardian,"guardian","siisii??"}, // summon guardians
  3776. {buildin_guardianinfo,"guardianinfo","i"}, // display guardian data [Valaris]
  3777. {buildin_petskillbonus,"petskillbonus","iiii"}, // [Valaris]
  3778. {buildin_petrecovery,"petrecovery","ii"}, // [Valaris]
  3779. {buildin_petloot,"petloot","i"}, // [Valaris]
  3780. {buildin_petheal,"petheal","iiii"}, // [Valaris]
  3781. // {buildin_petmag,"petmag","iiii"}, // [Valaris]
  3782. {buildin_petskillattack,"petskillattack","iiii"}, // [Skotlex]
  3783. {buildin_petskillattack2,"petskillattack2","iiiii"}, // [Valaris]
  3784. {buildin_petskillsupport,"petskillsupport","iiiii"}, // [Skotlex]
  3785. {buildin_skilleffect,"skilleffect","ii"}, // skill effect [Celest]
  3786. {buildin_npcskilleffect,"npcskilleffect","iiii"}, // npc skill effect [Valaris]
  3787. {buildin_specialeffect,"specialeffect","i*"}, // npc skill effect [Valaris]
  3788. {buildin_specialeffect2,"specialeffect2","i*"}, // skill effect on players[Valaris]
  3789. {buildin_nude,"nude",""}, // nude command [Valaris]
  3790. {buildin_mapwarp,"mapwarp","ssii*"}, // Added by RoVeRT
  3791. {buildin_inittimer,"inittimer",""},
  3792. {buildin_stoptimer,"stoptimer",""},
  3793. {buildin_cmdothernpc,"cmdothernpc","ss"},
  3794. {buildin_atcommand,"atcommand","*"}, // [MouseJstr]
  3795. {buildin_charcommand,"charcommand","*"}, // [MouseJstr]
  3796. {buildin_movenpc,"movenpc","sii"}, // [MouseJstr]
  3797. {buildin_message,"message","s*"}, // [MouseJstr]
  3798. {buildin_npctalk,"npctalk","*"}, // [Valaris]
  3799. {buildin_hasitems,"hasitems","*"}, // [Valaris]
  3800. {buildin_mobcount,"mobcount","ss"},
  3801. {buildin_getlook,"getlook","i"},
  3802. {buildin_getsavepoint,"getsavepoint","i"},
  3803. {buildin_npcspeed,"npcspeed","i"}, // [Valaris]
  3804. {buildin_npcwalkto,"npcwalkto","ii"}, // [Valaris]
  3805. {buildin_npcstop,"npcstop",""}, // [Valaris]
  3806. {buildin_getmapxy,"getmapxy","siii*"}, //by Lorky [Lupus]
  3807. {buildin_checkoption1,"checkoption1","i"},
  3808. {buildin_checkoption2,"checkoption2","i"},
  3809. {buildin_guildgetexp,"guildgetexp","i"},
  3810. {buildin_guildchangegm,"guildchangegm","is"},
  3811. {buildin_logmes,"logmes","s"}, //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  3812. {buildin_summon,"summon","si*"}, // summons a slave monster [Celest]
  3813. {buildin_isnight,"isnight",""}, // check whether it is night time [Celest]
  3814. {buildin_isday,"isday",""}, // check whether it is day time [Celest]
  3815. {buildin_isequipped,"isequipped","i*"}, // check whether another item/card has been equipped [Celest]
  3816. {buildin_isequippedcnt,"isequippedcnt","i*"}, // check how many items/cards are being equipped [Celest]
  3817. {buildin_cardscnt,"cardscnt","i*"}, // check how many items/cards are being equipped in the same arm [Lupus]
  3818. {buildin_getrefine,"getrefine","*"}, // returns the refined number of the current item, or an item with index specified [celest]
  3819. {buildin_adopt,"adopt","sss"}, // allows 2 parents to adopt a child
  3820. {buildin_night,"night",""}, // sets the server to night time
  3821. {buildin_day,"day",""}, // sets the server to day time
  3822. #ifdef PCRE_SUPPORT
  3823. {buildin_defpattern, "defpattern", "iss"}, // Define pattern to listen for [MouseJstr]
  3824. {buildin_activatepset, "activatepset", "i"}, // Activate a pattern set [MouseJstr]
  3825. {buildin_deactivatepset, "deactivatepset", "i"}, // Deactive a pattern set [MouseJstr]
  3826. {buildin_deletepset, "deletepset", "i"}, // Delete a pattern set [MouseJstr]
  3827. #endif
  3828. {buildin_dispbottom,"dispbottom","s"}, //added from jA [Lupus]
  3829. {buildin_getusersname,"getusersname","*"},
  3830. {buildin_recovery,"recovery",""},
  3831. {buildin_getpetinfo,"getpetinfo","i"},
  3832. {buildin_checkequipedcard,"checkequipedcard","i"},
  3833. {buildin_jump_zero,"jump_zero","ii"}, //for future jA script compatibility
  3834. {buildin_select,"select","*"}, //for future jA script compatibility
  3835. {buildin_prompt,"prompt","*"},
  3836. {buildin_globalmes,"globalmes","s*"},
  3837. {buildin_getmapmobs,"getmapmobs","s"}, //end jA addition
  3838. {buildin_unequip,"unequip","i"}, // unequip command [Spectre]
  3839. {buildin_getstrlen,"getstrlen","s"}, //strlen [Valaris]
  3840. {buildin_charisalpha,"charisalpha","si"}, //isalpha [Valaris]
  3841. {buildin_fakenpcname,"fakenpcname","ssi"}, // [Lance]
  3842. {buildin_compare,"compare","ss"}, // Lordalfa - To bring strstr to scripting Engine.
  3843. {buildin_getiteminfo,"getiteminfo","ii"}, //[Lupus] returns Items Buy / sell Price, etc info
  3844. {buildin_getequipcardid,"getequipcardid","ii"}, //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  3845. // [zBuffer] List of mathematics commands --->
  3846. {buildin_sqrt,"sqrt","i"},
  3847. {buildin_pow,"pow","ii"},
  3848. {buildin_distance,"distance","iiii"},
  3849. {buildin_checkcell,"checkcell","siii"},
  3850. // <--- [zBuffer] List of mathematics commands
  3851. // [zBuffer] List of dynamic var commands --->
  3852. {buildin_getd,"getd","*"},
  3853. {buildin_setd,"setd","*"},
  3854. // <--- [zBuffer] List of dynamic var commands
  3855. {buildin_petstat,"petstat","i"},
  3856. {buildin_callshop,"callshop","si"}, // [Skotlex]
  3857. {buildin_npcshopitem,"npcshopitem","sii*"}, // [Lance]
  3858. {buildin_npcshopadditem,"npcshopadditem","sii*"},
  3859. {buildin_npcshopdelitem,"npcshopdelitem","si*"},
  3860. {buildin_npcshopattach,"npcshopattach","s?"},
  3861. {buildin_equip,"equip","i"},
  3862. {buildin_autoequip,"autoequip","ii"},
  3863. {buildin_setbattleflag,"setbattleflag","ss"},
  3864. {buildin_getbattleflag,"getbattleflag","s"},
  3865. {buildin_setitemscript,"setitemscript","is"}, //Set NEW item bonus script. Lupus
  3866. {buildin_disguise,"disguise","i"}, //disguise player. Lupus
  3867. {buildin_undisguise,"undisguise","*"}, //undisguise player. Lupus
  3868. {buildin_getmonsterinfo,"getmonsterinfo","ii"}, //Lupus
  3869. {buildin_axtoi,"axtoi","s"},
  3870. {buildin_query_sql, "query_sql", "s*"},
  3871. {buildin_escape_sql, "escape_sql", "s"},
  3872. {buildin_atoi,"atoi","s"},
  3873. // [zBuffer] List of player cont commands --->
  3874. {buildin_rid2name,"rid2name","i"},
  3875. {buildin_pcfollow,"pcfollow","ii"},
  3876. {buildin_pcstopfollow,"pcstopfollow","i"},
  3877. {buildin_pcblockmove,"pcblockmove","ii"},
  3878. // <--- [zBuffer] List of player cont commands
  3879. // [zBuffer] List of mob control commands --->
  3880. {buildin_mobspawn,"mobspawn","*"},
  3881. {buildin_mobremove,"mobremove","i"},
  3882. {buildin_getmobdata,"getmobdata","i*"},
  3883. {buildin_setmobdata,"setmobdata","iii"},
  3884. {buildin_mobassist,"mobassist","i*"},
  3885. {buildin_mobattach,"mobattach","i*"},
  3886. {buildin_unitwalk,"unitwalk","i*"},
  3887. {buildin_unitkill,"unitkill","i"},
  3888. {buildin_unitwarp,"unitwarp","isii"},
  3889. {buildin_unitattack,"unitattack","i*"},
  3890. {buildin_unitstop,"unitstop","i"},
  3891. {buildin_unittalk,"unittalk","is"},
  3892. {buildin_unitemote,"unitemote","ii"},
  3893. {buildin_unitdeadsit,"unitdeadsit","ii"},
  3894. {buildin_unitskilluseid,"unitskilluseid","iii*"}, // originally by Qamera [Celest]
  3895. {buildin_unitskillusepos,"unitskillusepos","iiiii"}, // [Celest]
  3896. // <--- [zBuffer] List of mob control commands
  3897. {buildin_sleep,"sleep","i"},
  3898. {buildin_sleep2,"sleep2","i"},
  3899. {buildin_awake,"awake","s"},
  3900. {buildin_getvariableofnpc,"getvariableofnpc","is"},
  3901. // [blackhole89] -->
  3902. {buildin_warpportal,"warpportal","iisii"},
  3903. // <--- [blackhole89]
  3904. {buildin_homunculus_evolution,"homevolution",""}, //[orn]
  3905. {buildin_eaclass,"eaclass","*"}, //[Skotlex]
  3906. {buildin_roclass,"roclass","i*"}, //[Skotlex]
  3907. {buildin_checkvending,"checkvending","*"},
  3908. {buildin_checkchatting,"checkchatting","*"},
  3909. {NULL,NULL,NULL},
  3910. };
  3911. /*==========================================
  3912. *
  3913. *------------------------------------------
  3914. */
  3915. int buildin_mes(struct script_state *st)
  3916. {
  3917. struct map_session_data *sd = script_rid2sd(st);
  3918. char *mes = conv_str(st, &(st->stack->stack_data[st->start+2]));
  3919. if (sd)
  3920. clif_scriptmes(sd, st->oid, mes);
  3921. return 0;
  3922. }
  3923. /*==========================================
  3924. *
  3925. *------------------------------------------
  3926. */
  3927. int buildin_goto(struct script_state *st)
  3928. {
  3929. int pos;
  3930. if( st->stack->stack_data[st->start+2].type!=C_POS){
  3931. ShowMessage("script: goto: not label!\n");
  3932. st->state=END;
  3933. return 1;
  3934. }
  3935. pos=conv_num(st,& (st->stack->stack_data[st->start+2]));
  3936. st->pos=pos;
  3937. st->state=GOTO;
  3938. return 0;
  3939. }
  3940. /*==========================================
  3941. * ユーザー定義関数の呼び出し
  3942. *------------------------------------------
  3943. */
  3944. int buildin_callfunc(struct script_state *st)
  3945. {
  3946. struct script_code *scr, *oldscr;
  3947. char *str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  3948. if( (scr=strdb_get(userfunc_db,str)) ){
  3949. int i,j;
  3950. struct linkdb_node **oldval = st->stack->var_function;
  3951. for(i=st->start+3,j=0;i<st->end;i++,j++)
  3952. push_copy(st->stack,i);
  3953. push_val(st->stack,C_INT,j); // 引数の数をプッシュ
  3954. push_val(st->stack,C_INT,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3955. push_val(st->stack,C_INT,(int)st->script); // 現在のスクリプトをプッシュ
  3956. push_val(st->stack,C_INT,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3957. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3958. oldscr = st->script;
  3959. st->pos=0;
  3960. st->script=scr;
  3961. st->stack->defsp=st->start+5+j;
  3962. st->state=GOTO;
  3963. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3964. // ' 変数の引き継ぎ
  3965. for(i = 0; i < j; i++) {
  3966. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  3967. if( s->type == C_NAME && !s->ref ) {
  3968. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  3969. // '@ 変数の引き継ぎ
  3970. if( name[0] == '.' && name[1] == '@' ) {
  3971. s->ref = oldval;
  3972. } else if( name[0] == '.' ) {
  3973. s->ref = &oldscr->script_vars;
  3974. }
  3975. }
  3976. }
  3977. }else{
  3978. ShowWarning("script:callfunc: function not found! [%s]\n",str);
  3979. st->state=END;
  3980. return 1;
  3981. }
  3982. return 0;
  3983. }
  3984. /*==========================================
  3985. * サブルーティンの呼び出し
  3986. *------------------------------------------
  3987. */
  3988. int buildin_callsub(struct script_state *st)
  3989. {
  3990. int pos=conv_num(st,& (st->stack->stack_data[st->start+2]));
  3991. int i,j;
  3992. if(st->stack->stack_data[st->start+2].type != C_POS && st->stack->stack_data[st->start+2].type != C_USERFUNC_POS) {
  3993. ShowError("script: callsub: not label !\n");
  3994. st->state=END;
  3995. return 1;
  3996. } else {
  3997. struct linkdb_node **oldval = st->stack->var_function;
  3998. for(i=st->start+3,j=0;i<st->end;i++,j++)
  3999. push_copy(st->stack,i);
  4000. push_val(st->stack,C_INT,j); // 引数の数をプッシュ
  4001. push_val(st->stack,C_INT,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  4002. push_val(st->stack,C_INT,(int)st->script); // 現在のスクリプトをプッシュ
  4003. push_val(st->stack,C_INT,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  4004. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  4005. st->pos=pos;
  4006. st->stack->defsp=st->start+5+j;
  4007. st->state=GOTO;
  4008. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  4009. // ' 変数の引き継ぎ
  4010. for(i = 0; i < j; i++) {
  4011. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  4012. if( s->type == C_NAME && !s->ref ) {
  4013. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  4014. // '@ 変数の引き継ぎ
  4015. if( name[0] == '.' && name[1] == '@' ) {
  4016. s->ref = oldval;
  4017. }
  4018. }
  4019. }
  4020. }
  4021. return 0;
  4022. }
  4023. /*==========================================
  4024. * 引数の所得
  4025. *------------------------------------------
  4026. */
  4027. int buildin_getarg(struct script_state *st)
  4028. {
  4029. int num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  4030. int max,stsp;
  4031. if( st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO ){
  4032. ShowWarning("script:getarg without callfunc or callsub!\n");
  4033. st->state=END;
  4034. return 1;
  4035. }
  4036. max=conv_num(st,& (st->stack->stack_data[st->stack->defsp-5]));
  4037. stsp=st->stack->defsp - max -5;
  4038. if( num >= max ){
  4039. ShowWarning("script:getarg arg1(%d) out of range(%d) !\n",num,max);
  4040. st->state=END;
  4041. return 1;
  4042. }
  4043. push_copy(st->stack,stsp+num);
  4044. return 0;
  4045. }
  4046. /*==========================================
  4047. * サブルーチン/ユーザー定義関数の終了
  4048. *------------------------------------------
  4049. */
  4050. int buildin_return(struct script_state *st)
  4051. {
  4052. if(st->end>st->start+2){ // 戻り値有り
  4053. struct script_data *sd;
  4054. push_copy(st->stack,st->start+2);
  4055. sd = &st->stack->stack_data[st->stack->sp-1];
  4056. if(sd->type == C_NAME) {
  4057. char *name = str_buf + str_data[sd->u.num&0x00ffffff].str;
  4058. if( name[0] == '.' && name[1] == '@') {
  4059. // '@ 変数を参照渡しにすると危険なので値渡しにする
  4060. get_val(st,sd);
  4061. } else if( name[0] == '.' && !sd->ref) {
  4062. // ' 変数は参照渡しでも良いが、参照元が設定されていないと
  4063. // 元のスクリプトの値を差してしまうので補正する。
  4064. sd->ref = &st->script->script_vars;
  4065. }
  4066. }
  4067. }
  4068. st->state=RETFUNC;
  4069. return 0;
  4070. }
  4071. /*==========================================
  4072. *
  4073. *------------------------------------------
  4074. */
  4075. int buildin_next(struct script_state *st)
  4076. {
  4077. st->state=STOP;
  4078. clif_scriptnext(script_rid2sd(st),st->oid);
  4079. return 0;
  4080. }
  4081. /*==========================================
  4082. *
  4083. *------------------------------------------
  4084. */
  4085. int buildin_close(struct script_state *st)
  4086. {
  4087. st->state=END;
  4088. clif_scriptclose(script_rid2sd(st),st->oid);
  4089. return 0;
  4090. }
  4091. int buildin_close2(struct script_state *st)
  4092. {
  4093. st->state=STOP;
  4094. clif_scriptclose(script_rid2sd(st),st->oid);
  4095. return 0;
  4096. }
  4097. /*==========================================
  4098. *
  4099. *------------------------------------------
  4100. */
  4101. int buildin_menu(struct script_state *st)
  4102. {
  4103. char *buf, *ptr;
  4104. int len,i;
  4105. struct map_session_data *sd = script_rid2sd(st);
  4106. nullpo_retr(0, sd);
  4107. if(sd->state.menu_or_input==0){
  4108. st->state=RERUNLINE;
  4109. sd->state.menu_or_input=1;
  4110. if( (st->end - st->start - 2) % 2 == 1 ) {
  4111. // 引数の数が奇数なのでエラー扱い
  4112. ShowError("buildin_menu: illegal argument count(%d).\n", st->end - st->start - 2);
  4113. sd->state.menu_or_input=0;
  4114. st->state=END;
  4115. return 1;
  4116. }
  4117. for(i=st->start+2,len=0;i<st->end;i+=2){
  4118. conv_str(st,& (st->stack->stack_data[i]));
  4119. len+=(int)strlen(st->stack->stack_data[i].u.str)+1;
  4120. }
  4121. buf=(char *)aMallocA((len+1)*sizeof(char));
  4122. buf[0]=0;
  4123. for(i=st->start+2,len=0;i<st->end;i+=2){
  4124. if( st->stack->stack_data[i].u.str[0] ) {
  4125. strcat(buf,st->stack->stack_data[i].u.str);
  4126. strcat(buf,":");
  4127. }
  4128. }
  4129. ptr = buf;
  4130. sd->npc_menu = 0; //Reuse to store max menu entries. Avoids the need of an extra variable.
  4131. while (ptr && (ptr = strchr(ptr, ':')) != NULL)
  4132. { sd->npc_menu++; ptr++; }
  4133. clif_scriptmenu(sd,st->oid,buf);
  4134. aFree(buf);
  4135. } else if(sd->npc_menu==0xff){ // cancel
  4136. sd->state.menu_or_input=0;
  4137. st->state=END;
  4138. } else { // goto動作
  4139. sd->state.menu_or_input=0;
  4140. if(sd->npc_menu>0){
  4141. //Skip empty menu entries which weren't displayed on the client (blackhole89)
  4142. for(i=st->start+2;i<=(st->start+sd->npc_menu*2) && sd->npc_menu<(st->end-st->start)/2;i+=2) {
  4143. conv_str(st,& (st->stack->stack_data[i])); // we should convert variables to strings before access it [jA1983] [EoE]
  4144. if((int)strlen(st->stack->stack_data[i].u.str) < 1)
  4145. sd->npc_menu++; //Empty selection which wasn't displayed on the client.
  4146. }
  4147. if(sd->npc_menu >= (st->end-st->start)/2) {
  4148. //Invalid selection.
  4149. st->state=END;
  4150. return 0;
  4151. }
  4152. if( st->stack->stack_data[st->start+sd->npc_menu*2+1].type!=C_POS ){
  4153. ShowError("script: menu: not label !\n");
  4154. st->state=END;
  4155. return 1;
  4156. }
  4157. pc_setreg(sd,add_str("@menu"),sd->npc_menu);
  4158. st->pos=conv_num(st,& (st->stack->stack_data[st->start+sd->npc_menu*2+1]));
  4159. st->state=GOTO;
  4160. }
  4161. }
  4162. return 0;
  4163. }
  4164. /*==========================================
  4165. *
  4166. *------------------------------------------
  4167. */
  4168. int buildin_rand(struct script_state *st)
  4169. {
  4170. int range,min,max;
  4171. if (st->end > st->start+3){
  4172. min = conv_num(st,& (st->stack->stack_data[st->start+2]));
  4173. max = conv_num(st,& (st->stack->stack_data[st->start+3]));
  4174. if (max == min){ //Why would someone do this?
  4175. push_val(st->stack,C_INT,min);
  4176. return 0;
  4177. }
  4178. if(max<min){
  4179. int tmp = min;
  4180. min = max;
  4181. max = tmp;
  4182. }
  4183. range = max - min + 1;
  4184. if (range == 0) range = 1;
  4185. push_val(st->stack,C_INT,rand()%range+min);
  4186. } else {
  4187. range = conv_num(st,& (st->stack->stack_data[st->start+2]));
  4188. if (range == 0) range = 1;
  4189. push_val(st->stack,C_INT,rand()%range);
  4190. }
  4191. return 0;
  4192. }
  4193. /*==========================================
  4194. *
  4195. *------------------------------------------
  4196. */
  4197. int buildin_warp(struct script_state *st)
  4198. {
  4199. int x,y;
  4200. char *str;
  4201. struct map_session_data *sd=script_rid2sd(st);
  4202. nullpo_retr(0, sd);
  4203. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  4204. x=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4205. y=conv_num(st,& (st->stack->stack_data[st->start+4]));
  4206. if(strcmp(str,"Random")==0)
  4207. pc_randomwarp(sd,3);
  4208. else if(strcmp(str,"SavePoint")==0){
  4209. pc_setpos(sd,sd->status.save_point.map,
  4210. sd->status.save_point.x,sd->status.save_point.y,3);
  4211. }else if(strcmp(str,"Save")==0){
  4212. pc_setpos(sd,sd->status.save_point.map,
  4213. sd->status.save_point.x,sd->status.save_point.y,3);
  4214. }else
  4215. pc_setpos(sd,mapindex_name2id(str),x,y,0);
  4216. return 0;
  4217. }
  4218. /*==========================================
  4219. * エリア指定ワープ
  4220. *------------------------------------------
  4221. */
  4222. int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4223. {
  4224. int x,y;
  4225. unsigned int map;
  4226. map=va_arg(ap, unsigned int);
  4227. x=va_arg(ap,int);
  4228. y=va_arg(ap,int);
  4229. if(map == 0)
  4230. pc_randomwarp((struct map_session_data *)bl,3);
  4231. else
  4232. pc_setpos((struct map_session_data *)bl,map,x,y,0);
  4233. return 0;
  4234. }
  4235. int buildin_areawarp(struct script_state *st)
  4236. {
  4237. int x,y,m;
  4238. unsigned int index;
  4239. char *str;
  4240. char *mapname;
  4241. int x0,y0,x1,y1;
  4242. mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  4243. x0=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4244. y0=conv_num(st,& (st->stack->stack_data[st->start+4]));
  4245. x1=conv_num(st,& (st->stack->stack_data[st->start+5]));
  4246. y1=conv_num(st,& (st->stack->stack_data[st->start+6]));
  4247. str=conv_str(st,& (st->stack->stack_data[st->start+7]));
  4248. x=conv_num(st,& (st->stack->stack_data[st->start+8]));
  4249. y=conv_num(st,& (st->stack->stack_data[st->start+9]));
  4250. if( (m=map_mapname2mapid(mapname))< 0)
  4251. return 0;
  4252. if(strcmp(str,"Random")==0)
  4253. index = 0;
  4254. else if(!(index=mapindex_name2id(str)))
  4255. return 0;
  4256. map_foreachinarea(buildin_areawarp_sub,
  4257. m,x0,y0,x1,y1,BL_PC, index,x,y );
  4258. return 0;
  4259. }
  4260. /*==========================================
  4261. * warpchar [LuzZza]
  4262. * Useful for warp one player from
  4263. * another player npc-session.
  4264. * Using: warpchar "mapname.gat",x,y,Char_ID;
  4265. *------------------------------------------
  4266. */
  4267. int buildin_warpchar(struct script_state *st)
  4268. {
  4269. int x,y,a,i;
  4270. char *str;
  4271. struct map_session_data *sd, **pl_allsd;
  4272. int users;
  4273. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  4274. x=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4275. y=conv_num(st,& (st->stack->stack_data[st->start+4]));
  4276. a=conv_num(st,& (st->stack->stack_data[st->start+5]));
  4277. pl_allsd = map_getallusers(&users);
  4278. for(i=0; i<users; i++) {
  4279. sd = pl_allsd[i];
  4280. if(sd->status.char_id == a) {
  4281. if(strcmp(str, "Random") == 0)
  4282. pc_randomwarp(sd, 3);
  4283. else if(strcmp(str, "SavePoint") == 0)
  4284. pc_setpos(sd, sd->status.save_point.map,
  4285. sd->status.save_point.x, sd->status.save_point.y, 3);
  4286. else
  4287. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  4288. }
  4289. }
  4290. return 0;
  4291. }
  4292. /*==========================================
  4293. * Warpparty - [Fredzilla]
  4294. * Syntax: warpparty "mapname.gat",x,y,Party_ID;
  4295. *------------------------------------------
  4296. */
  4297. int buildin_warpparty(struct script_state *st)
  4298. {
  4299. int x,y;
  4300. char *str;
  4301. int p_id;
  4302. int i;
  4303. unsigned short mapindex;
  4304. struct map_session_data *pl_sd;
  4305. struct party_data *p=NULL;
  4306. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  4307. x=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4308. y=conv_num(st,& (st->stack->stack_data[st->start+4]));
  4309. p_id=conv_num(st,& (st->stack->stack_data[st->start+5]));
  4310. if(p_id < 1)
  4311. return 0;
  4312. p = party_search(p_id);
  4313. if (!p)
  4314. return 0;
  4315. if(strcmp(str,"Random")==0)
  4316. {
  4317. for (i = 0; i < MAX_PARTY; i++)
  4318. {
  4319. if ((pl_sd = p->data[i].sd))
  4320. {
  4321. if(map[pl_sd->bl.m].flag.nowarp)
  4322. continue;
  4323. pc_randomwarp(pl_sd,3);
  4324. }
  4325. }
  4326. }
  4327. else if(strcmp(str,"SavePointAll")==0)
  4328. {
  4329. for (i = 0; i < MAX_PARTY; i++)
  4330. {
  4331. if ((pl_sd = p->data[i].sd))
  4332. {
  4333. if(map[pl_sd->bl.m].flag.noreturn)
  4334. continue;
  4335. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4336. }
  4337. }
  4338. }
  4339. else if(strcmp(str,"SavePoint")==0)
  4340. {
  4341. pl_sd=script_rid2sd(st);
  4342. if (!pl_sd) return 0;
  4343. mapindex=pl_sd->status.save_point.map;
  4344. x=pl_sd->status.save_point.x;
  4345. y=pl_sd->status.save_point.y;
  4346. for (i = 0; i < MAX_PARTY; i++)
  4347. {
  4348. if ((pl_sd = p->data[i].sd))
  4349. {
  4350. if(map[pl_sd->bl.m].flag.noreturn)
  4351. continue;
  4352. pc_setpos(pl_sd,mapindex,x,y,3);
  4353. }
  4354. }
  4355. }
  4356. else
  4357. {
  4358. mapindex = mapindex_name2id(str);
  4359. if (!mapindex) //Show source of npc error.
  4360. return 1;
  4361. for (i = 0; i < MAX_PARTY; i++)
  4362. {
  4363. if ((pl_sd = p->data[i].sd))
  4364. {
  4365. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4366. continue;
  4367. pc_setpos(pl_sd,mapindex,x,y,3);
  4368. }
  4369. }
  4370. }
  4371. return 0;
  4372. }
  4373. /*==========================================
  4374. * Warpguild - [Fredzilla]
  4375. * Syntax: warpguild "mapname.gat",x,y,Guild_ID;
  4376. *------------------------------------------
  4377. */
  4378. int buildin_warpguild(struct script_state *st)
  4379. {
  4380. int x,y;
  4381. unsigned short mapindex;
  4382. char *str;
  4383. int g;
  4384. int i;
  4385. struct map_session_data *pl_sd, **pl_allsd;
  4386. int users;
  4387. struct map_session_data *sd;
  4388. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  4389. x=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4390. y=conv_num(st,& (st->stack->stack_data[st->start+4]));
  4391. g=conv_num(st,& (st->stack->stack_data[st->start+5]));
  4392. sd=script_rid2sd(st);
  4393. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4394. return 0;
  4395. if(g < 1)
  4396. return 0;
  4397. pl_allsd = map_getallusers(&users);
  4398. if(strcmp(str,"Random")==0)
  4399. {
  4400. for (i = 0; i < users; i++)
  4401. {
  4402. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4403. {
  4404. if(map[pl_sd->bl.m].flag.nowarp)
  4405. continue;
  4406. pc_randomwarp(pl_sd,3);
  4407. }
  4408. }
  4409. }
  4410. else if(strcmp(str,"SavePointAll")==0)
  4411. {
  4412. if(map[sd->bl.m].flag.noreturn)
  4413. return 0;
  4414. for (i = 0; i < users; i++)
  4415. {
  4416. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4417. {
  4418. if(map[pl_sd->bl.m].flag.noreturn)
  4419. continue;
  4420. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4421. }
  4422. }
  4423. }
  4424. else if(strcmp(str,"SavePoint")==0)
  4425. {
  4426. if(map[sd->bl.m].flag.noreturn)
  4427. return 0;
  4428. mapindex=sd->status.save_point.map;
  4429. x=sd->status.save_point.x;
  4430. y=sd->status.save_point.y;
  4431. for (i = 0; i < users; i++)
  4432. {
  4433. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4434. {
  4435. if(map[pl_sd->bl.m].flag.noreturn)
  4436. continue;
  4437. pc_setpos(pl_sd,mapindex,x,y,3);
  4438. }
  4439. }
  4440. }
  4441. else
  4442. {
  4443. mapindex = mapindex_name2id(str);
  4444. for (i = 0; i < users; i++)
  4445. {
  4446. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4447. {
  4448. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4449. continue;
  4450. pc_setpos(pl_sd,mapindex,x,y,3);
  4451. }
  4452. }
  4453. }
  4454. return 0;
  4455. }
  4456. /*==========================================
  4457. *
  4458. *------------------------------------------
  4459. */
  4460. int buildin_heal(struct script_state *st)
  4461. {
  4462. struct map_session_data *sd;
  4463. int hp,sp;
  4464. sd = script_rid2sd(st);
  4465. if (!sd) return 0;
  4466. hp=conv_num(st,& (st->stack->stack_data[st->start+2]));
  4467. sp=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4468. status_heal(&sd->bl, hp, sp, 1);
  4469. return 0;
  4470. }
  4471. /*==========================================
  4472. *
  4473. *------------------------------------------
  4474. */
  4475. int buildin_itemheal(struct script_state *st)
  4476. {
  4477. struct map_session_data *sd;
  4478. int hp,sp;
  4479. hp=conv_num(st,& (st->stack->stack_data[st->start+2]));
  4480. sp=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4481. if(potion_flag==1) {
  4482. potion_hp = hp;
  4483. potion_sp = sp;
  4484. return 0;
  4485. }
  4486. sd = script_rid2sd(st);
  4487. if (!sd) return 0;
  4488. pc_itemheal(sd,sd->itemid,hp,sp);
  4489. return 0;
  4490. }
  4491. /*==========================================
  4492. *
  4493. *------------------------------------------
  4494. */
  4495. int buildin_percentheal(struct script_state *st)
  4496. {
  4497. int hp,sp;
  4498. hp=conv_num(st,& (st->stack->stack_data[st->start+2]));
  4499. sp=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4500. if(potion_flag==1) {
  4501. potion_per_hp = hp;
  4502. potion_per_sp = sp;
  4503. return 0;
  4504. }
  4505. pc_percentheal(script_rid2sd(st),hp,sp);
  4506. return 0;
  4507. }
  4508. /*==========================================
  4509. *
  4510. *------------------------------------------
  4511. */
  4512. int buildin_jobchange(struct script_state *st)
  4513. {
  4514. int job, upper=-1;
  4515. job=conv_num(st,& (st->stack->stack_data[st->start+2]));
  4516. if( st->end>st->start+3 )
  4517. upper=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4518. if ((job >= 0 && job < MAX_PC_CLASS))
  4519. {
  4520. pc_jobchange(script_rid2sd(st),job, upper);
  4521. if(use_irc && irc_announce_jobchange_flag)
  4522. irc_announce_jobchange(script_rid2sd(st));
  4523. }
  4524. return 0;
  4525. }
  4526. /*==========================================
  4527. *
  4528. *------------------------------------------
  4529. */
  4530. int buildin_jobname(struct script_state *st)
  4531. {
  4532. int class_=conv_num(st,& (st->stack->stack_data[st->start+2]));
  4533. push_str(st->stack,C_CONSTSTR,job_name(class_));
  4534. return 0;
  4535. }
  4536. /*==========================================
  4537. *
  4538. *------------------------------------------
  4539. */
  4540. int buildin_input(struct script_state *st)
  4541. {
  4542. struct map_session_data *sd = script_rid2sd(st);
  4543. int num = (st->end>st->start+2)? st->stack->stack_data[st->start+2].u.num: 0;
  4544. char *name = (st->end>st->start+2)? str_buf+str_data[num&0x00ffffff].str: "";
  4545. char postfix=name[strlen(name)-1];
  4546. nullpo_retr(0, sd);
  4547. if(sd->state.menu_or_input){
  4548. sd->state.menu_or_input=0;
  4549. if( postfix=='$' ){
  4550. // 文字列
  4551. if(st->end>st->start+2){ // 引数1個
  4552. set_reg(st,sd,num,name,(void*)sd->npc_str,st->stack->stack_data[st->start+2].ref);
  4553. }else{
  4554. ShowError("buildin_input: string discarded !!\n");
  4555. return 1;
  4556. }
  4557. return 0;
  4558. }
  4559. // commented by Lupus (check Value Number Input fix in clif.c)
  4560. // readded by Yor: set ammount to 0 instead of cancel trade.
  4561. // ** Fix by fritz :X keeps people from abusing old input bugs
  4562. if (sd->npc_amount < 0) { //** If input amount is less then 0
  4563. // clif_tradecancelled(sd); // added "Deal has been cancelled" message by Valaris
  4564. // buildin_close(st); // ** close
  4565. sd->npc_amount = 0;
  4566. } else if ((unsigned int)sd->npc_amount > battle_config.vending_max_value) // new fix by Yor
  4567. sd->npc_amount = battle_config.vending_max_value;
  4568. // 数値
  4569. if(st->end>st->start+2){ // 引数1個
  4570. set_reg(st,sd,num,name,(void*)sd->npc_amount,st->stack->stack_data[st->start+2].ref);
  4571. } else {
  4572. // ragemu互換のため
  4573. //pc_setreg(sd,add_str("l14"),sd->npc_amount);
  4574. }
  4575. return 0;
  4576. }
  4577. //state.menu_or_input = 0
  4578. st->state=RERUNLINE;
  4579. if(postfix=='$')
  4580. clif_scriptinputstr(sd,st->oid);
  4581. else
  4582. clif_scriptinput(sd,st->oid);
  4583. sd->state.menu_or_input=1;
  4584. return 0;
  4585. }
  4586. /*==========================================
  4587. * 変数設定
  4588. *------------------------------------------
  4589. */
  4590. int buildin_set(struct script_state *st)
  4591. {
  4592. struct map_session_data *sd=NULL;
  4593. int num=st->stack->stack_data[st->start+2].u.num;
  4594. char *name=str_buf+str_data[num&0x00ffffff].str;
  4595. char prefix=*name;
  4596. char postfix=name[strlen(name)-1];
  4597. if( st->stack->stack_data[st->start+2].type!=C_NAME ){
  4598. ShowError("script: buildin_set: not name\n");
  4599. return 1;
  4600. }
  4601. if(not_server_variable(prefix))
  4602. sd=script_rid2sd(st);
  4603. if( postfix=='$' ){
  4604. // 文字列
  4605. char *str = conv_str(st,& (st->stack->stack_data[st->start+3]));
  4606. set_reg(st,sd,num,name,(void*)str,st->stack->stack_data[st->start+2].ref);
  4607. }else{
  4608. // 数値
  4609. int val = conv_num(st,& (st->stack->stack_data[st->start+3]));
  4610. set_reg(st,sd,num,name,(void*)val,st->stack->stack_data[st->start+2].ref);
  4611. }
  4612. return 0;
  4613. }
  4614. /*==========================================
  4615. * 配列変数設定
  4616. *------------------------------------------
  4617. */
  4618. int buildin_setarray(struct script_state *st)
  4619. {
  4620. struct map_session_data *sd=NULL;
  4621. int num=st->stack->stack_data[st->start+2].u.num;
  4622. char *name=str_buf+str_data[num&0x00ffffff].str;
  4623. char prefix=*name;
  4624. char postfix=name[strlen(name)-1];
  4625. int i,j;
  4626. if( prefix!='$' && prefix!='@' && prefix!='.'){
  4627. ShowWarning("buildin_setarray: illegal scope !\n");
  4628. return 1;
  4629. }
  4630. if(not_server_variable(prefix))
  4631. sd=script_rid2sd(st);
  4632. for(j=0,i=st->start+3; i<st->end && j<128;i++,j++){
  4633. void *v;
  4634. if( postfix=='$' )
  4635. v=(void*)conv_str(st,& (st->stack->stack_data[i]));
  4636. else
  4637. v=(void*)conv_num(st,& (st->stack->stack_data[i]));
  4638. set_reg(st, sd, num+(j<<24), name, v, st->stack->stack_data[st->start+2].ref);
  4639. }
  4640. return 0;
  4641. }
  4642. /*==========================================
  4643. * 配列変数クリア
  4644. *------------------------------------------
  4645. */
  4646. int buildin_cleararray(struct script_state *st)
  4647. {
  4648. struct map_session_data *sd=NULL;
  4649. int num=st->stack->stack_data[st->start+2].u.num;
  4650. char *name=str_buf+str_data[num&0x00ffffff].str;
  4651. char prefix=*name;
  4652. char postfix=name[strlen(name)-1];
  4653. int sz=conv_num(st,& (st->stack->stack_data[st->start+4]));
  4654. int i;
  4655. void *v;
  4656. if( prefix!='$' && prefix!='@' && prefix!='.'){
  4657. ShowWarning("buildin_cleararray: illegal scope !\n");
  4658. return 1;
  4659. }
  4660. if( not_server_variable(prefix) )
  4661. sd=script_rid2sd(st);
  4662. if( postfix=='$' )
  4663. v=(void*)conv_str(st,& (st->stack->stack_data[st->start+3]));
  4664. else
  4665. v=(void*)conv_num(st,& (st->stack->stack_data[st->start+3]));
  4666. for(i=0;i<sz;i++)
  4667. set_reg(st,sd,num+(i<<24),name,v,st->stack->stack_data[st->start+2].ref);
  4668. return 0;
  4669. }
  4670. /*==========================================
  4671. * 配列変数コピー
  4672. *------------------------------------------
  4673. */
  4674. int buildin_copyarray(struct script_state *st)
  4675. {
  4676. struct map_session_data *sd=NULL;
  4677. int num=st->stack->stack_data[st->start+2].u.num;
  4678. char *name=str_buf+str_data[num&0x00ffffff].str;
  4679. char prefix=*name;
  4680. char postfix=name[strlen(name)-1];
  4681. int num2=st->stack->stack_data[st->start+3].u.num;
  4682. char *name2=str_buf+str_data[num2&0x00ffffff].str;
  4683. char prefix2=*name2;
  4684. char postfix2=name2[strlen(name2)-1];
  4685. int sz=conv_num(st,& (st->stack->stack_data[st->start+4]));
  4686. int i;
  4687. if( prefix!='$' && prefix!='@' && prefix!='.' ){
  4688. printf("buildin_copyarray: illeagal scope !\n");
  4689. return 0;
  4690. }
  4691. if( prefix2!='$' && prefix2!='@' && prefix2!='.' ) {
  4692. printf("buildin_copyarray: illeagal scope !\n");
  4693. return 0;
  4694. }
  4695. if( (postfix=='$' || postfix2=='$') && postfix!=postfix2 ){
  4696. printf("buildin_copyarray: type mismatch !\n");
  4697. return 0;
  4698. }
  4699. if( not_server_variable(prefix) || not_server_variable(prefix2) )
  4700. sd=script_rid2sd(st);
  4701. if((num & 0x00FFFFFF) == (num2 & 0x00FFFFFF) && (num & 0xFF000000) > (num2 & 0xFF000000)) {
  4702. // 同じ配列で、num > num2 の場合大きい方からコピーしないといけない
  4703. for(i=sz-1;i>=0;i--)
  4704. set_reg(
  4705. st,sd,num+(i<<24),name,
  4706. get_val2(st,num2+(i<<24),st->stack->stack_data[st->start+3].ref),
  4707. st->stack->stack_data[st->start+2].ref
  4708. );
  4709. } else {
  4710. for(i=0;i<sz;i++)
  4711. set_reg(
  4712. st,sd,num+(i<<24),name,
  4713. get_val2(st,num2+(i<<24),st->stack->stack_data[st->start+3].ref),
  4714. st->stack->stack_data[st->start+2].ref
  4715. );
  4716. }
  4717. return 0;
  4718. }
  4719. /*==========================================
  4720. * 配列変数のサイズ所得
  4721. *------------------------------------------
  4722. */
  4723. static int getarraysize(struct script_state *st,int num,int postfix,struct linkdb_node** ref)
  4724. {
  4725. 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]
  4726. if(postfix == '$'){
  4727. for(;i<128;i++){
  4728. void *v=get_val2(st,(num & 0x00FFFFFF)+(i<<24),ref);
  4729. if(*((char*)v)) c=i;
  4730. }
  4731. }else{
  4732. for(;i<128;i++){
  4733. void *v=get_val2(st,(num & 0x00FFFFFF)+(i<<24),ref);
  4734. if((int)v) c=i;
  4735. }
  4736. }
  4737. return c+1;
  4738. }
  4739. int buildin_getarraysize(struct script_state *st)
  4740. {
  4741. int num=st->stack->stack_data[st->start+2].u.num;
  4742. char *name=str_buf+str_data[num&0x00ffffff].str;
  4743. char prefix=*name;
  4744. char postfix=name[strlen(name)-1];
  4745. if( prefix!='$' && prefix!='@' && prefix!='.' ){
  4746. ShowWarning("buildin_copyarray: illegal scope !\n");
  4747. push_val(st->stack,C_INT,0);
  4748. return 1;
  4749. }
  4750. push_val(st->stack,C_INT,getarraysize(st,num,postfix,st->stack->stack_data[st->start+2].ref));
  4751. return 0;
  4752. }
  4753. /*==========================================
  4754. * 配列変数から要素削除
  4755. *------------------------------------------
  4756. */
  4757. int buildin_deletearray(struct script_state *st)
  4758. {
  4759. struct map_session_data *sd=NULL;
  4760. int num=st->stack->stack_data[st->start+2].u.num;
  4761. char *name=str_buf+str_data[num&0x00ffffff].str;
  4762. char prefix=*name;
  4763. char postfix=name[strlen(name)-1];
  4764. int count=1;
  4765. int i,sz=getarraysize(st,num,postfix,st->stack->stack_data[st->start+2].ref)-(num>>24)-count+1;
  4766. if( (st->end > st->start+3) )
  4767. count=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4768. if( prefix!='$' && prefix!='@' && prefix!='.' ){
  4769. ShowWarning("buildin_deletearray: illegal scope !\n");
  4770. return 1;
  4771. }
  4772. if( not_server_variable(prefix) )
  4773. sd=script_rid2sd(st);
  4774. for(i=0;i<sz;i++){
  4775. set_reg(
  4776. st,sd,num+(i<<24),name,
  4777. get_val2(st,num+((i+count)<<24),st->stack->stack_data[st->start+2].ref),
  4778. st->stack->stack_data[st->start+2].ref
  4779. );
  4780. }
  4781. if(postfix != '$'){
  4782. for(;i<(128-(num>>24));i++)
  4783. set_reg(st,sd,num+(i<<24),name, 0,st->stack->stack_data[st->start+2].ref);
  4784. } else {
  4785. for(;i<(128-(num>>24));i++)
  4786. set_reg(st,sd,num+(i<<24),name, (void *) "",st->stack->stack_data[st->start+2].ref);
  4787. }
  4788. return 0;
  4789. }
  4790. /*==========================================
  4791. * 指定要素を表す値(キー)を所得する
  4792. *------------------------------------------
  4793. */
  4794. int buildin_getelementofarray(struct script_state *st)
  4795. {
  4796. if( st->stack->stack_data[st->start+2].type==C_NAME ){
  4797. int i=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4798. if(i>127 || i<0){
  4799. ShowWarning("script: getelementofarray (operator[]): param2 illegal number %d\n",i);
  4800. push_val(st->stack,C_INT,0);
  4801. return 1;
  4802. }else{
  4803. push_val2(st->stack,C_NAME,
  4804. (i<<24) | st->stack->stack_data[st->start+2].u.num, st->stack->stack_data[st->start+2].ref );
  4805. }
  4806. }else{
  4807. ShowError("script: getelementofarray (operator[]): param1 not name !\n");
  4808. push_val(st->stack,C_INT,0);
  4809. }
  4810. return 0;
  4811. }
  4812. /*==========================================
  4813. *
  4814. *------------------------------------------
  4815. */
  4816. int buildin_setlook(struct script_state *st)
  4817. {
  4818. int type,val;
  4819. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  4820. val=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4821. pc_changelook(script_rid2sd(st),type,val);
  4822. return 0;
  4823. }
  4824. /*==========================================
  4825. *
  4826. *------------------------------------------
  4827. */
  4828. int buildin_cutin(struct script_state *st)
  4829. {
  4830. int type;
  4831. conv_str(st,& (st->stack->stack_data[st->start+2]));
  4832. type=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4833. clif_cutin(script_rid2sd(st),st->stack->stack_data[st->start+2].u.str,type);
  4834. return 0;
  4835. }
  4836. /*==========================================
  4837. *
  4838. *------------------------------------------
  4839. */
  4840. int buildin_viewpoint(struct script_state *st)
  4841. {
  4842. int type,x,y,id,color;
  4843. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  4844. x=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4845. y=conv_num(st,& (st->stack->stack_data[st->start+4]));
  4846. id=conv_num(st,& (st->stack->stack_data[st->start+5]));
  4847. color=conv_num(st,& (st->stack->stack_data[st->start+6]));
  4848. clif_viewpoint(script_rid2sd(st),st->oid,type,x,y,id,color);
  4849. return 0;
  4850. }
  4851. /*==========================================
  4852. *
  4853. *------------------------------------------
  4854. */
  4855. int buildin_countitem(struct script_state *st)
  4856. {
  4857. int nameid=0,count=0,i;
  4858. struct map_session_data *sd;
  4859. struct script_data *data;
  4860. sd = script_rid2sd(st);
  4861. data=&(st->stack->stack_data[st->start+2]);
  4862. get_val(st,data);
  4863. if( data->type==C_STR || data->type==C_CONSTSTR ){
  4864. const char *name=conv_str(st,data);
  4865. struct item_data *item_data;
  4866. if( (item_data = itemdb_searchname(name)) != NULL)
  4867. nameid=item_data->nameid;
  4868. }else
  4869. nameid=conv_num(st,data);
  4870. if (nameid>=500) //if no such ID then skip this iteration
  4871. for(i=0;i<MAX_INVENTORY;i++){
  4872. if(sd->status.inventory[i].nameid==nameid)
  4873. count+=sd->status.inventory[i].amount;
  4874. }
  4875. else{
  4876. if(battle_config.error_log)
  4877. ShowError("wrong item ID : countitem(%i)\n",nameid);
  4878. push_val(st->stack,C_INT,0);
  4879. return 1;
  4880. }
  4881. push_val(st->stack,C_INT,count);
  4882. return 0;
  4883. }
  4884. /*==========================================
  4885. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4886. * returns number of items that met the conditions
  4887. *------------------------------------------
  4888. */
  4889. int buildin_countitem2(struct script_state *st)
  4890. {
  4891. int nameid=0,count=0,i;
  4892. int iden,ref,attr,c1,c2,c3,c4;
  4893. struct map_session_data *sd;
  4894. struct script_data *data;
  4895. sd = script_rid2sd(st);
  4896. data=&(st->stack->stack_data[st->start+2]);
  4897. get_val(st,data);
  4898. if( data->type==C_STR || data->type==C_CONSTSTR ){
  4899. const char *name=conv_str(st,data);
  4900. struct item_data *item_data;
  4901. if( (item_data = itemdb_searchname(name)) != NULL)
  4902. nameid=item_data->nameid;
  4903. }else
  4904. nameid=conv_num(st,data);
  4905. iden=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4906. ref=conv_num(st,& (st->stack->stack_data[st->start+4]));
  4907. attr=conv_num(st,& (st->stack->stack_data[st->start+5]));
  4908. c1=conv_num(st,& (st->stack->stack_data[st->start+6]));
  4909. c2=conv_num(st,& (st->stack->stack_data[st->start+7]));
  4910. c3=conv_num(st,& (st->stack->stack_data[st->start+8]));
  4911. c4=conv_num(st,& (st->stack->stack_data[st->start+9]));
  4912. if (nameid>=500) //if no such ID then skip this iteration
  4913. for(i=0;i<MAX_INVENTORY;i++){
  4914. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  4915. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  4916. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  4917. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  4918. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  4919. sd->status.inventory[i].card[3]!=c4)
  4920. continue;
  4921. count+=sd->status.inventory[i].amount;
  4922. }
  4923. else{
  4924. if(battle_config.error_log)
  4925. ShowError("wrong item ID : countitem2(%i)\n",nameid);
  4926. push_val(st->stack,C_INT,0);
  4927. return 1;
  4928. }
  4929. push_val(st->stack,C_INT,count);
  4930. return 0;
  4931. }
  4932. /*==========================================
  4933. * 重量チェック
  4934. *------------------------------------------
  4935. */
  4936. int buildin_checkweight(struct script_state *st)
  4937. {
  4938. int nameid=0,amount,i;
  4939. unsigned long weight;
  4940. struct map_session_data *sd;
  4941. struct script_data *data;
  4942. sd = script_rid2sd(st);
  4943. data=&(st->stack->stack_data[st->start+2]);
  4944. get_val(st,data);
  4945. if( data->type==C_STR || data->type==C_CONSTSTR ){
  4946. const char *name=conv_str(st,data);
  4947. struct item_data *item_data = itemdb_searchname(name);
  4948. if( item_data )
  4949. nameid=item_data->nameid;
  4950. }else
  4951. nameid=conv_num(st,data);
  4952. amount=conv_num(st,& (st->stack->stack_data[st->start+3]));
  4953. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4954. push_val(st->stack,C_INT,0);
  4955. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4956. return 1;
  4957. }
  4958. weight = itemdb_weight(nameid)*amount;
  4959. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4960. push_val(st->stack,C_INT,0);
  4961. } else {
  4962. //Check if the inventory ain't full.
  4963. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4964. i = pc_search_inventory(sd,0);
  4965. if (i >= 0) //Empty slot available.
  4966. push_val(st->stack,C_INT,1);
  4967. else //Inventory full
  4968. push_val(st->stack,C_INT,0);
  4969. }
  4970. return 0;
  4971. }
  4972. /*==========================================
  4973. * getitem <item id>,<amount>{,<character ID>};
  4974. * getitem "<item name>",<amount>{,<character ID>};
  4975. *------------------------------------------
  4976. */
  4977. int buildin_getitem(struct script_state *st)
  4978. {
  4979. int nameid,amount,flag = 0;
  4980. struct item it;
  4981. struct map_session_data *sd;
  4982. struct script_data *data;
  4983. data=script_getdata(st,2);
  4984. get_val(st,data);
  4985. if( script_isstring(data) )
  4986. {// "<item name>"
  4987. const char *name=conv_str(st,data);
  4988. struct item_data *item_data = itemdb_searchname(name);
  4989. if( item_data == NULL ){
  4990. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4991. report_src(st);
  4992. return 1; //No item created.
  4993. }
  4994. nameid=item_data->nameid;
  4995. } else if( script_isint(data) )
  4996. {// <item id>
  4997. nameid=conv_num(st,data);
  4998. //Violet Box, Blue Box, etc - random item pick
  4999. if( nameid < 0 ) {
  5000. nameid=itemdb_searchrandomid(-nameid);
  5001. flag = 1;
  5002. }
  5003. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5004. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5005. report_src(st);
  5006. return 1; //No item created.
  5007. }
  5008. } else {
  5009. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5010. report_src(st);
  5011. return 1;
  5012. }
  5013. // <amount>
  5014. if( (amount=conv_num(st, script_getdata(st,3))) <= 0)
  5015. return 0; //return if amount <=0, skip the useles iteration
  5016. memset(&it,0,sizeof(it));
  5017. it.nameid=nameid;
  5018. if(!flag)
  5019. it.identify=1;
  5020. else
  5021. it.identify=itemdb_isidentified(nameid);
  5022. if( script_hasdata(st,4) )
  5023. {// <character ID>
  5024. sd=map_id2sd(conv_num(st,script_getdata(st,4)));
  5025. } else
  5026. {// attached player
  5027. sd=script_rid2sd(st);
  5028. }
  5029. if( sd == NULL ) // no target
  5030. return 0;
  5031. if( pet_create_egg(sd, nameid) )
  5032. amount = 1; //This is a pet!
  5033. else if( (flag=pc_additem(sd,&it,amount)) ){
  5034. clif_additem(sd,0,0,flag);
  5035. if( pc_candrop(sd,&it) )
  5036. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  5037. }
  5038. //Logs items, got from (N)PC scripts [Lupus]
  5039. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  5040. log_pick_pc(sd, "N", nameid, amount, NULL);
  5041. return 0;
  5042. }
  5043. /*==========================================
  5044. *
  5045. *------------------------------------------
  5046. */
  5047. int buildin_getitem2(struct script_state *st)
  5048. {
  5049. int nameid,amount,flag = 0;
  5050. int iden,ref,attr,c1,c2,c3,c4;
  5051. struct item_data *item_data;
  5052. struct item item_tmp;
  5053. struct map_session_data *sd;
  5054. struct script_data *data;
  5055. sd = script_rid2sd(st);
  5056. data=&(st->stack->stack_data[st->start+2]);
  5057. get_val(st,data);
  5058. if( data->type==C_STR || data->type==C_CONSTSTR ){
  5059. const char *name=conv_str(st,data);
  5060. struct item_data *item_data = itemdb_searchname(name);
  5061. nameid=UNKNOWN_ITEM_ID;
  5062. if( item_data )
  5063. nameid=item_data->nameid;
  5064. }else
  5065. nameid=conv_num(st,data);
  5066. amount=conv_num(st,& (st->stack->stack_data[st->start+3]));
  5067. iden=conv_num(st,& (st->stack->stack_data[st->start+4]));
  5068. ref=conv_num(st,& (st->stack->stack_data[st->start+5]));
  5069. attr=conv_num(st,& (st->stack->stack_data[st->start+6]));
  5070. c1=conv_num(st,& (st->stack->stack_data[st->start+7]));
  5071. c2=conv_num(st,& (st->stack->stack_data[st->start+8]));
  5072. c3=conv_num(st,& (st->stack->stack_data[st->start+9]));
  5073. c4=conv_num(st,& (st->stack->stack_data[st->start+10]));
  5074. if( st->end>st->start+11 ) //アイテムを指定したIDに渡す
  5075. sd=map_id2sd(conv_num(st,& (st->stack->stack_data[st->start+11])));
  5076. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  5077. return 0;
  5078. if(nameid<0) { // ランダム
  5079. nameid=itemdb_searchrandomid(-nameid);
  5080. flag = 1;
  5081. }
  5082. if(nameid > 0) {
  5083. memset(&item_tmp,0,sizeof(item_tmp));
  5084. item_data=itemdb_exists(nameid);
  5085. if (item_data == NULL)
  5086. return -1;
  5087. if(item_data->type==4 || item_data->type==5){
  5088. if(ref > 10) ref = 10;
  5089. }
  5090. else if(item_data->type==7) {
  5091. iden = 1;
  5092. ref = 0;
  5093. }
  5094. else {
  5095. iden = 1;
  5096. ref = attr = 0;
  5097. }
  5098. item_tmp.nameid=nameid;
  5099. if(!flag)
  5100. item_tmp.identify=iden;
  5101. else if(item_data->type==4 || item_data->type==5)
  5102. item_tmp.identify=0;
  5103. item_tmp.refine=ref;
  5104. item_tmp.attribute=attr;
  5105. item_tmp.card[0]=c1;
  5106. item_tmp.card[1]=c2;
  5107. item_tmp.card[2]=c3;
  5108. item_tmp.card[3]=c4;
  5109. if((flag = pc_additem(sd,&item_tmp,amount))) {
  5110. clif_additem(sd,0,0,flag);
  5111. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  5112. }
  5113. //Logs items, got from (N)PC scripts [Lupus]
  5114. if(log_config.enable_logs&0x40)
  5115. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  5116. }
  5117. return 0;
  5118. }
  5119. /*==========================================
  5120. * gets an item with someone's name inscribed [Skotlex]
  5121. * getinscribeditem item_num, character_name
  5122. * Returned Qty is always 1, only works on equip-able
  5123. * equipment
  5124. *------------------------------------------
  5125. */
  5126. int buildin_getnameditem(struct script_state *st)
  5127. {
  5128. int nameid;
  5129. struct item item_tmp;
  5130. struct map_session_data *sd, *tsd;
  5131. struct script_data *data;
  5132. sd = script_rid2sd(st);
  5133. if (sd == NULL)
  5134. { //Player not attached!
  5135. push_val(st->stack,C_INT,0);
  5136. return 0;
  5137. }
  5138. data=&(st->stack->stack_data[st->start+2]);
  5139. get_val(st,data);
  5140. if( data->type==C_STR || data->type==C_CONSTSTR ){
  5141. const char *name=conv_str(st,data);
  5142. struct item_data *item_data = itemdb_searchname(name);
  5143. if( item_data == NULL)
  5144. { //Failed
  5145. push_val(st->stack,C_INT,0);
  5146. return 0;
  5147. }
  5148. nameid = item_data->nameid;
  5149. }else
  5150. nameid = conv_num(st,data);
  5151. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5152. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5153. push_val(st->stack,C_INT,0);
  5154. return 0;
  5155. }
  5156. data=&(st->stack->stack_data[st->start+3]);
  5157. get_val(st,data);
  5158. if( data->type==C_STR || data->type==C_CONSTSTR ) //Char Name
  5159. tsd=map_nick2sd(conv_str(st,data));
  5160. else //Char Id was given
  5161. tsd=map_charid2sd(conv_num(st,data));
  5162. if( tsd == NULL )
  5163. { //Failed
  5164. push_val(st->stack,C_INT,0);
  5165. return 0;
  5166. }
  5167. memset(&item_tmp,0,sizeof(item_tmp));
  5168. item_tmp.nameid=nameid;
  5169. item_tmp.amount=1;
  5170. item_tmp.identify=1;
  5171. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5172. item_tmp.card[2]=tsd->status.char_id;
  5173. item_tmp.card[3]=tsd->status.char_id >> 16;
  5174. if(pc_additem(sd,&item_tmp,1)) {
  5175. push_val(st->stack,C_INT,0);
  5176. return 0; //Failed to add item, we will not drop if they don't fit
  5177. }
  5178. //Logs items, got from (N)PC scripts [Lupus]
  5179. if(log_config.enable_logs&0x40)
  5180. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5181. push_val(st->stack,C_INT,1);
  5182. return 0;
  5183. }
  5184. /*==========================================
  5185. * gets a random item ID from an item group [Skotlex]
  5186. * groupranditem group_num
  5187. *------------------------------------------
  5188. */
  5189. int buildin_grouprandomitem(struct script_state *st)
  5190. {
  5191. int group;
  5192. group = conv_num(st,& (st->stack->stack_data[st->start+2]));
  5193. push_val(st->stack, C_INT, itemdb_searchrandomid(group));
  5194. return 0;
  5195. }
  5196. /*==========================================
  5197. *
  5198. *------------------------------------------
  5199. */
  5200. int buildin_makeitem(struct script_state *st)
  5201. {
  5202. int nameid,amount,flag = 0;
  5203. int x,y,m;
  5204. char *mapname;
  5205. struct item item_tmp;
  5206. struct script_data *data;
  5207. data=&(st->stack->stack_data[st->start+2]);
  5208. get_val(st,data);
  5209. if( data->type==C_STR || data->type==C_CONSTSTR ){
  5210. const char *name=conv_str(st,data);
  5211. struct item_data *item_data = itemdb_searchname(name);
  5212. nameid=UNKNOWN_ITEM_ID;
  5213. if( item_data )
  5214. nameid=item_data->nameid;
  5215. }else
  5216. nameid=conv_num(st,data);
  5217. amount=conv_num(st,& (st->stack->stack_data[st->start+3]));
  5218. mapname =conv_str(st,& (st->stack->stack_data[st->start+4]));
  5219. x =conv_num(st,& (st->stack->stack_data[st->start+5]));
  5220. y =conv_num(st,& (st->stack->stack_data[st->start+6]));
  5221. if(strcmp(mapname,"this")==0)
  5222. {
  5223. struct map_session_data *sd;
  5224. sd = script_rid2sd(st);
  5225. if (!sd) return 0; //Failed...
  5226. m=sd->bl.m;
  5227. } else
  5228. m=map_mapname2mapid(mapname);
  5229. if(nameid<0) { // ランダム
  5230. nameid=itemdb_searchrandomid(-nameid);
  5231. flag = 1;
  5232. }
  5233. if(nameid > 0) {
  5234. memset(&item_tmp,0,sizeof(item_tmp));
  5235. item_tmp.nameid=nameid;
  5236. if(!flag)
  5237. item_tmp.identify=1;
  5238. else
  5239. item_tmp.identify=itemdb_isidentified(nameid);
  5240. map_addflooritem(&item_tmp,amount,m,x,y,NULL,NULL,NULL,0);
  5241. }
  5242. return 0;
  5243. }
  5244. /*==========================================
  5245. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5246. *------------------------------------------
  5247. */
  5248. int buildin_delitem(struct script_state *st)
  5249. {
  5250. int nameid=0,amount,i,important_item=0;
  5251. struct map_session_data *sd;
  5252. struct script_data *data;
  5253. sd = script_rid2sd(st);
  5254. data=&(st->stack->stack_data[st->start+2]);
  5255. get_val(st,data);
  5256. if( data->type==C_STR || data->type==C_CONSTSTR ){
  5257. const char *name=conv_str(st,data);
  5258. struct item_data *item_data = itemdb_searchname(name);
  5259. //nameid=UNKNOWN_ITEM_ID;
  5260. if( item_data )
  5261. nameid=item_data->nameid;
  5262. }else
  5263. nameid=conv_num(st,data);
  5264. amount=conv_num(st,& (st->stack->stack_data[st->start+3]));
  5265. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5266. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5267. return 0;
  5268. }
  5269. //1st pass
  5270. //here we won't delete items with CARDS, named items but we count them
  5271. for(i=0;i<MAX_INVENTORY;i++){
  5272. //we don't delete wrong item or equipped item
  5273. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5274. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5275. continue;
  5276. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5277. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5278. sd->status.inventory[i].card[0] == CARD0_PET)
  5279. {
  5280. if (intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5281. continue; //pet couldn't be sent for deletion.
  5282. }
  5283. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5284. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5285. sd->status.inventory[i].card[0] ||
  5286. sd->status.inventory[i].refine) {
  5287. //this is important item, count it (except for pet eggs)
  5288. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5289. important_item++;
  5290. continue;
  5291. }
  5292. if(sd->status.inventory[i].amount>=amount){
  5293. //Logs items, got from (N)PC scripts [Lupus]
  5294. if(log_config.enable_logs&0x40)
  5295. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5296. pc_delitem(sd,i,amount,0);
  5297. return 0; //we deleted exact amount of items. now exit
  5298. } else {
  5299. amount-=sd->status.inventory[i].amount;
  5300. //Logs items, got from (N)PC scripts [Lupus]
  5301. if(log_config.enable_logs&0x40) {
  5302. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5303. }
  5304. //Logs
  5305. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5306. }
  5307. }
  5308. //2nd pass
  5309. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5310. if (important_item>0 && amount>0)
  5311. for(i=0;i<MAX_INVENTORY;i++){
  5312. //we don't delete wrong item
  5313. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5314. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5315. continue;
  5316. if(sd->status.inventory[i].amount>=amount){
  5317. //Logs items, got from (N)PC scripts [Lupus]
  5318. if(log_config.enable_logs&0x40)
  5319. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5320. pc_delitem(sd,i,amount,0);
  5321. return 0; //we deleted exact amount of items. now exit
  5322. } else {
  5323. amount-=sd->status.inventory[i].amount;
  5324. //Logs items, got from (N)PC scripts [Lupus]
  5325. if(log_config.enable_logs&0x40)
  5326. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5327. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5328. }
  5329. }
  5330. return 0;
  5331. }
  5332. /*==========================================
  5333. * advanced version of delitem [modified by Mihilion]
  5334. *------------------------------------------
  5335. */
  5336. int buildin_delitem2(struct script_state *st)
  5337. {
  5338. int nameid=0,amount,i=0;
  5339. int iden,ref,attr,c1,c2,c3,c4;
  5340. struct map_session_data *sd;
  5341. struct script_data *data;
  5342. sd = script_rid2sd(st);
  5343. data=&(st->stack->stack_data[st->start+2]);
  5344. get_val(st,data);
  5345. if( data->type==C_STR || data->type==C_CONSTSTR ){
  5346. const char *name=conv_str(st,data);
  5347. struct item_data *item_data = itemdb_searchname(name);
  5348. //nameid=UNKNOWN_ITEM_ID;
  5349. if( item_data )
  5350. nameid=item_data->nameid;
  5351. }else
  5352. nameid=conv_num(st,data);
  5353. amount=conv_num(st,& (st->stack->stack_data[st->start+3]));
  5354. iden=conv_num(st,& (st->stack->stack_data[st->start+4]));
  5355. ref=conv_num(st,& (st->stack->stack_data[st->start+5]));
  5356. attr=conv_num(st,& (st->stack->stack_data[st->start+6]));
  5357. c1=conv_num(st,& (st->stack->stack_data[st->start+7]));
  5358. c2=conv_num(st,& (st->stack->stack_data[st->start+8]));
  5359. c3=conv_num(st,& (st->stack->stack_data[st->start+9]));
  5360. c4=conv_num(st,& (st->stack->stack_data[st->start+10]));
  5361. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5362. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5363. return 0;
  5364. }
  5365. for(i=0;i<MAX_INVENTORY;i++){
  5366. //we don't delete wrong item or equipped item
  5367. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5368. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5369. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5370. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5371. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5372. sd->status.inventory[i].card[3]!=c4)
  5373. continue;
  5374. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5375. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5376. {
  5377. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5378. continue; //Failed to send delete the pet.
  5379. }
  5380. if(sd->status.inventory[i].amount>=amount){
  5381. //Logs items, got from (N)PC scripts [Lupus]
  5382. if(log_config.enable_logs&0x40)
  5383. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5384. pc_delitem(sd,i,amount,0);
  5385. return 0; //we deleted exact amount of items. now exit
  5386. } else {
  5387. amount-=sd->status.inventory[i].amount;
  5388. //Logs items, got from (N)PC scripts [Lupus]
  5389. if(log_config.enable_logs&0x40)
  5390. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5391. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5392. }
  5393. }
  5394. return 0;
  5395. }
  5396. /*==========================================
  5397. * Enables/Disables use of items while in an NPC [Skotlex]
  5398. *------------------------------------------
  5399. */
  5400. int buildin_enableitemuse(struct script_state *st) {
  5401. struct map_session_data *sd;
  5402. sd=script_rid2sd(st);
  5403. if (sd)
  5404. sd->npc_item_flag = st->oid;
  5405. return 0;
  5406. }
  5407. int buildin_disableitemuse(struct script_state *st) {
  5408. struct map_session_data *sd;
  5409. sd=script_rid2sd(st);
  5410. if (sd)
  5411. sd->npc_item_flag = 0;
  5412. return 0;
  5413. }
  5414. /*==========================================
  5415. *キャラ関係のパラメータ取得
  5416. *------------------------------------------
  5417. */
  5418. int buildin_readparam(struct script_state *st)
  5419. {
  5420. int type;
  5421. struct map_session_data *sd;
  5422. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5423. if( st->end>st->start+3 )
  5424. sd=map_nick2sd(conv_str(st,& (st->stack->stack_data[st->start+3])));
  5425. else
  5426. sd=script_rid2sd(st);
  5427. if(sd==NULL){
  5428. push_val(st->stack,C_INT,-1);
  5429. return 0;
  5430. }
  5431. push_val(st->stack,C_INT,pc_readparam(sd,type));
  5432. return 0;
  5433. }
  5434. /*==========================================
  5435. *キャラ関係のID取得
  5436. *------------------------------------------
  5437. */
  5438. int buildin_getcharid(struct script_state *st)
  5439. {
  5440. int num;
  5441. struct map_session_data *sd;
  5442. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5443. if( st->end>st->start+3 )
  5444. sd=map_nick2sd(conv_str(st,& (st->stack->stack_data[st->start+3])));
  5445. else
  5446. sd=script_rid2sd(st);
  5447. if(sd==NULL || num<0 || num>3){
  5448. push_val(st->stack,C_INT,0); //return 0, according docs
  5449. return 0;
  5450. }
  5451. if(num==0)
  5452. push_val(st->stack,C_INT,sd->status.char_id);
  5453. if(num==1)
  5454. push_val(st->stack,C_INT,sd->status.party_id);
  5455. if(num==2)
  5456. push_val(st->stack,C_INT,sd->status.guild_id);
  5457. if(num==3)
  5458. push_val(st->stack,C_INT,sd->status.account_id);
  5459. return 0;
  5460. }
  5461. /*==========================================
  5462. *指定IDのPT名取得
  5463. *------------------------------------------
  5464. */
  5465. char *buildin_getpartyname_sub(int party_id)
  5466. {
  5467. struct party_data *p;
  5468. p=party_search(party_id);
  5469. if(p!=NULL){
  5470. char *buf;
  5471. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5472. memcpy(buf, p->party.name, NAME_LENGTH-1);
  5473. buf[NAME_LENGTH-1] = '\0';
  5474. return buf;
  5475. }
  5476. return 0;
  5477. }
  5478. int buildin_getpartyname(struct script_state *st)
  5479. {
  5480. char *name;
  5481. int party_id;
  5482. party_id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5483. name=buildin_getpartyname_sub(party_id);
  5484. if(name != NULL)
  5485. push_str(st->stack,C_STR,name);
  5486. else
  5487. push_str(st->stack,C_CONSTSTR,"null");
  5488. return 0;
  5489. }
  5490. /*==========================================
  5491. *指定IDのPT人数とメンバーID取得
  5492. *------------------------------------------
  5493. */
  5494. int buildin_getpartymember(struct script_state *st)
  5495. {
  5496. struct party_data *p;
  5497. int i,j=0,type=0;
  5498. p=party_search(conv_num(st,& (st->stack->stack_data[st->start+2])));
  5499. if( st->end>st->start+3 )
  5500. type=conv_num(st,& (st->stack->stack_data[st->start+3]));
  5501. if(p!=NULL){
  5502. for(i=0;i<MAX_PARTY;i++){
  5503. if(p->party.member[i].account_id){
  5504. switch (type) {
  5505. case 2:
  5506. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5507. break;
  5508. case 1:
  5509. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5510. break;
  5511. default:
  5512. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5513. }
  5514. j++;
  5515. }
  5516. }
  5517. }
  5518. mapreg_setreg(add_str("$@partymembercount"),j);
  5519. return 0;
  5520. }
  5521. /*==========================================
  5522. * Retrieves party leader. if flag is specified,
  5523. * return some of the leader data. Otherwise, return name.
  5524. *------------------------------------------
  5525. */
  5526. int buildin_getpartyleader(struct script_state *st)
  5527. {
  5528. int party_id, type = 0, i=0;
  5529. struct party_data *p;
  5530. party_id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5531. if( st->end>st->start+3 )
  5532. type=conv_num(st,& (st->stack->stack_data[st->start+3]));
  5533. p=party_search(party_id);
  5534. if (p) //Search leader
  5535. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5536. if (!p || i == MAX_PARTY) { //leader not found
  5537. if (type)
  5538. push_val(st->stack,C_INT,-1);
  5539. else
  5540. push_str(st->stack,C_CONSTSTR,"null");
  5541. return 0;
  5542. }
  5543. switch (type) {
  5544. case 1:
  5545. push_val(st->stack,C_INT,p->party.member[i].account_id);
  5546. break;
  5547. case 2:
  5548. push_val(st->stack,C_INT,p->party.member[i].char_id);
  5549. break;
  5550. case 3:
  5551. push_val(st->stack,C_INT,p->party.member[i].class_);
  5552. break;
  5553. case 4:
  5554. push_val(st->stack,C_INT,p->party.member[i].map);
  5555. break;
  5556. case 5:
  5557. push_val(st->stack,C_INT,p->party.member[i].lv);
  5558. break;
  5559. default:
  5560. push_str(st->stack,C_STR,p->party.member[i].name);
  5561. break;
  5562. }
  5563. return 0;
  5564. }
  5565. /*==========================================
  5566. *指定IDのギルド名取得
  5567. *------------------------------------------
  5568. */
  5569. char *buildin_getguildname_sub(int guild_id)
  5570. {
  5571. struct guild *g=NULL;
  5572. g=guild_search(guild_id);
  5573. if(g!=NULL){
  5574. char *buf;
  5575. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5576. memcpy(buf, g->name, NAME_LENGTH-1);
  5577. buf[NAME_LENGTH-1] = '\0';
  5578. return buf;
  5579. }
  5580. return NULL;
  5581. }
  5582. int buildin_getguildname(struct script_state *st)
  5583. {
  5584. char *name;
  5585. int guild_id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5586. name=buildin_getguildname_sub(guild_id);
  5587. if(name != NULL)
  5588. push_str(st->stack,C_STR,name);
  5589. else
  5590. push_str(st->stack,C_CONSTSTR,"null");
  5591. return 0;
  5592. }
  5593. /*==========================================
  5594. *指定IDのGuildMaster名取得
  5595. *------------------------------------------
  5596. */
  5597. char *buildin_getguildmaster_sub(int guild_id)
  5598. {
  5599. struct guild *g=NULL;
  5600. g=guild_search(guild_id);
  5601. if(g!=NULL){
  5602. char *buf;
  5603. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5604. memcpy(buf, g->master, NAME_LENGTH-1);
  5605. buf[NAME_LENGTH-1] = '\0';
  5606. return buf;
  5607. }
  5608. return 0;
  5609. }
  5610. int buildin_getguildmaster(struct script_state *st)
  5611. {
  5612. char *master;
  5613. int guild_id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5614. master=buildin_getguildmaster_sub(guild_id);
  5615. if(master!=0)
  5616. push_str(st->stack,C_STR,master);
  5617. else
  5618. push_str(st->stack,C_CONSTSTR,"null");
  5619. return 0;
  5620. }
  5621. int buildin_getguildmasterid(struct script_state *st)
  5622. {
  5623. char *master;
  5624. struct map_session_data *sd=NULL;
  5625. int guild_id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5626. master=buildin_getguildmaster_sub(guild_id);
  5627. if(master!=0){
  5628. if((sd=map_nick2sd(master)) == NULL){
  5629. push_val(st->stack,C_INT,0);
  5630. return 0;
  5631. }
  5632. push_val(st->stack,C_INT,sd->status.char_id);
  5633. }else{
  5634. push_val(st->stack,C_INT,0);
  5635. }
  5636. return 0;
  5637. }
  5638. /*==========================================
  5639. * キャラクタの名前
  5640. *------------------------------------------
  5641. */
  5642. int buildin_strcharinfo(struct script_state *st)
  5643. {
  5644. struct map_session_data *sd;
  5645. int num;
  5646. char *buf;
  5647. sd=script_rid2sd(st);
  5648. if (!sd) { //Avoid crashing....
  5649. push_str(st->stack,C_CONSTSTR,"");
  5650. return 0;
  5651. }
  5652. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5653. switch(num){
  5654. case 0:
  5655. push_str(st->stack,C_CONSTSTR,sd->status.name);
  5656. break;
  5657. case 1:
  5658. buf=buildin_getpartyname_sub(sd->status.party_id);
  5659. if(buf!=0)
  5660. push_str(st->stack,C_STR,buf);
  5661. else
  5662. push_str(st->stack,C_CONSTSTR,"");
  5663. break;
  5664. case 2:
  5665. buf=buildin_getguildname_sub(sd->status.guild_id);
  5666. if(buf != NULL)
  5667. push_str(st->stack,C_STR,buf);
  5668. else
  5669. push_str(st->stack,C_CONSTSTR,"");
  5670. break;
  5671. default:
  5672. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5673. push_str(st->stack,C_CONSTSTR,"");
  5674. break;
  5675. }
  5676. return 0;
  5677. }
  5678. 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};
  5679. /*==========================================
  5680. * GetEquipID(Pos); Pos: 1-10
  5681. *------------------------------------------
  5682. */
  5683. int buildin_getequipid(struct script_state *st)
  5684. {
  5685. int i,num;
  5686. struct map_session_data *sd;
  5687. struct item_data* item;
  5688. sd=script_rid2sd(st);
  5689. if(sd == NULL)
  5690. {
  5691. ShowError("getequipid: sd == NULL\n");
  5692. return 0;
  5693. }
  5694. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5695. i=pc_checkequip(sd,equip[num-1]);
  5696. if(i >= 0){
  5697. item=sd->inventory_data[i];
  5698. if(item)
  5699. push_val(st->stack,C_INT,item->nameid);
  5700. else
  5701. push_val(st->stack,C_INT,0);
  5702. }else{
  5703. push_val(st->stack,C_INT,-1);
  5704. }
  5705. return 0;
  5706. }
  5707. /*==========================================
  5708. * 装備名文字列(精錬メニュー用)
  5709. *------------------------------------------
  5710. */
  5711. int buildin_getequipname(struct script_state *st)
  5712. {
  5713. int i,num;
  5714. struct map_session_data *sd;
  5715. struct item_data* item;
  5716. char *buf;
  5717. buf=(char *)aMallocA(64*sizeof(char));
  5718. sd=script_rid2sd(st);
  5719. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5720. i=pc_checkequip(sd,equip[num-1]);
  5721. if(i >= 0){
  5722. item=sd->inventory_data[i];
  5723. if(item)
  5724. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5725. else
  5726. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5727. }else{
  5728. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5729. }
  5730. push_str(st->stack,C_STR,buf);
  5731. return 0;
  5732. }
  5733. /*==========================================
  5734. * getbrokenid [Valaris]
  5735. *------------------------------------------
  5736. */
  5737. int buildin_getbrokenid(struct script_state *st)
  5738. {
  5739. int i,num,id=0,brokencounter=0;
  5740. struct map_session_data *sd;
  5741. sd=script_rid2sd(st);
  5742. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5743. for(i=0; i<MAX_INVENTORY; i++) {
  5744. if(sd->status.inventory[i].attribute==1){
  5745. brokencounter++;
  5746. if(num==brokencounter){
  5747. id=sd->status.inventory[i].nameid;
  5748. break;
  5749. }
  5750. }
  5751. }
  5752. push_val(st->stack,C_INT,id);
  5753. return 0;
  5754. }
  5755. /*==========================================
  5756. * repair [Valaris]
  5757. *------------------------------------------
  5758. */
  5759. int buildin_repair(struct script_state *st)
  5760. {
  5761. int i,num;
  5762. int repaircounter=0;
  5763. struct map_session_data *sd;
  5764. sd=script_rid2sd(st);
  5765. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5766. for(i=0; i<MAX_INVENTORY; i++) {
  5767. if(sd->status.inventory[i].attribute==1){
  5768. repaircounter++;
  5769. if(num==repaircounter){
  5770. sd->status.inventory[i].attribute=0;
  5771. clif_equiplist(sd);
  5772. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5773. clif_misceffect(&sd->bl, 3);
  5774. break;
  5775. }
  5776. }
  5777. }
  5778. return 0;
  5779. }
  5780. /*==========================================
  5781. * 装備チェック
  5782. *------------------------------------------
  5783. */
  5784. int buildin_getequipisequiped(struct script_state *st)
  5785. {
  5786. int i,num;
  5787. struct map_session_data *sd;
  5788. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5789. sd=script_rid2sd(st);
  5790. if ((num - 1) >= (sizeof(equip) / sizeof(equip[0])))
  5791. i = -1;
  5792. else
  5793. i=pc_checkequip(sd,equip[num-1]);
  5794. if(i >= 0)
  5795. push_val(st->stack,C_INT,1);
  5796. else
  5797. push_val(st->stack,C_INT,0);
  5798. return 0;
  5799. }
  5800. /*==========================================
  5801. * 装備品精錬可能チェック
  5802. *------------------------------------------
  5803. */
  5804. int buildin_getequipisenableref(struct script_state *st)
  5805. {
  5806. int i,num;
  5807. struct map_session_data *sd;
  5808. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5809. sd=script_rid2sd(st);
  5810. i=pc_checkequip(sd,equip[num-1]);
  5811. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5812. {
  5813. push_val(st->stack,C_INT,1);
  5814. } else {
  5815. push_val(st->stack,C_INT,0);
  5816. }
  5817. return 0;
  5818. }
  5819. /*==========================================
  5820. * 装備品鑑定チェック
  5821. *------------------------------------------
  5822. */
  5823. int buildin_getequipisidentify(struct script_state *st)
  5824. {
  5825. int i,num;
  5826. struct map_session_data *sd;
  5827. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5828. sd=script_rid2sd(st);
  5829. i=pc_checkequip(sd,equip[num-1]);
  5830. if(i >= 0)
  5831. push_val(st->stack,C_INT,sd->status.inventory[i].identify);
  5832. else
  5833. push_val(st->stack,C_INT,0);
  5834. return 0;
  5835. }
  5836. /*==========================================
  5837. * 装備品精錬度
  5838. *------------------------------------------
  5839. */
  5840. int buildin_getequiprefinerycnt(struct script_state *st)
  5841. {
  5842. int i,num;
  5843. struct map_session_data *sd;
  5844. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5845. sd=script_rid2sd(st);
  5846. i=pc_checkequip(sd,equip[num-1]);
  5847. if(i >= 0)
  5848. push_val(st->stack,C_INT,sd->status.inventory[i].refine);
  5849. else
  5850. push_val(st->stack,C_INT,0);
  5851. return 0;
  5852. }
  5853. /*==========================================
  5854. * 装備品武器LV
  5855. *------------------------------------------
  5856. */
  5857. int buildin_getequipweaponlv(struct script_state *st)
  5858. {
  5859. int i,num;
  5860. struct map_session_data *sd;
  5861. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5862. sd=script_rid2sd(st);
  5863. i=pc_checkequip(sd,equip[num-1]);
  5864. if(i >= 0 && sd->inventory_data[i])
  5865. push_val(st->stack,C_INT,sd->inventory_data[i]->wlv);
  5866. else
  5867. push_val(st->stack,C_INT,0);
  5868. return 0;
  5869. }
  5870. /*==========================================
  5871. * 装備品精錬成功率
  5872. *------------------------------------------
  5873. */
  5874. int buildin_getequippercentrefinery(struct script_state *st)
  5875. {
  5876. int i,num;
  5877. struct map_session_data *sd;
  5878. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5879. sd=script_rid2sd(st);
  5880. i=pc_checkequip(sd,equip[num-1]);
  5881. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5882. push_val(st->stack,C_INT,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5883. else
  5884. push_val(st->stack,C_INT,0);
  5885. return 0;
  5886. }
  5887. /*==========================================
  5888. * 精錬成功
  5889. *------------------------------------------
  5890. */
  5891. int buildin_successrefitem(struct script_state *st)
  5892. {
  5893. int i,num,ep;
  5894. struct map_session_data *sd;
  5895. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5896. sd=script_rid2sd(st);
  5897. i=pc_checkequip(sd,equip[num-1]);
  5898. if(i >= 0) {
  5899. ep=sd->status.inventory[i].equip;
  5900. //Logs items, got from (N)PC scripts [Lupus]
  5901. if(log_config.enable_logs&0x40)
  5902. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5903. sd->status.inventory[i].refine++;
  5904. pc_unequipitem(sd,i,2);
  5905. clif_refine(sd->fd,sd,0,i,sd->status.inventory[i].refine);
  5906. clif_delitem(sd,i,1);
  5907. //Logs items, got from (N)PC scripts [Lupus]
  5908. if(log_config.enable_logs&0x40)
  5909. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5910. clif_additem(sd,i,1,0);
  5911. pc_equipitem(sd,i,ep);
  5912. clif_misceffect(&sd->bl,3);
  5913. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5914. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5915. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5916. ){ // Fame point system [DracoRPG]
  5917. switch (sd->inventory_data[i]->wlv){
  5918. case 1:
  5919. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5920. break;
  5921. case 2:
  5922. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5923. break;
  5924. case 3:
  5925. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5926. break;
  5927. }
  5928. }
  5929. }
  5930. return 0;
  5931. }
  5932. /*==========================================
  5933. * 精錬失敗
  5934. *------------------------------------------
  5935. */
  5936. int buildin_failedrefitem(struct script_state *st)
  5937. {
  5938. int i,num;
  5939. struct map_session_data *sd;
  5940. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5941. sd=script_rid2sd(st);
  5942. i=pc_checkequip(sd,equip[num-1]);
  5943. if(i >= 0) {
  5944. //Logs items, got from (N)PC scripts [Lupus]
  5945. if(log_config.enable_logs&0x40)
  5946. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5947. sd->status.inventory[i].refine = 0;
  5948. pc_unequipitem(sd,i,3);
  5949. // 精錬失敗エフェクトのパケット
  5950. clif_refine(sd->fd,sd,1,i,sd->status.inventory[i].refine);
  5951. pc_delitem(sd,i,1,0);
  5952. // 他の人にも失敗を通知
  5953. clif_misceffect(&sd->bl,2);
  5954. }
  5955. return 0;
  5956. }
  5957. /*==========================================
  5958. *
  5959. *------------------------------------------
  5960. */
  5961. int buildin_statusup(struct script_state *st)
  5962. {
  5963. int type;
  5964. struct map_session_data *sd;
  5965. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5966. sd=script_rid2sd(st);
  5967. pc_statusup(sd,type);
  5968. return 0;
  5969. }
  5970. /*==========================================
  5971. *
  5972. *------------------------------------------
  5973. */
  5974. int buildin_statusup2(struct script_state *st)
  5975. {
  5976. int type,val;
  5977. struct map_session_data *sd;
  5978. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  5979. val=conv_num(st,& (st->stack->stack_data[st->start+3]));
  5980. sd=script_rid2sd(st);
  5981. pc_statusup2(sd,type,val);
  5982. return 0;
  5983. }
  5984. /// See 'doc/item_bonus.txt'
  5985. /// bonus <bonus type>,<val1>
  5986. /// bonus2 <bonus type>,<val1>,<val2>
  5987. /// bonus3 <bonus type>,<val1>,<val2>,<val3>
  5988. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>
  5989. int buildin_bonus(struct script_state* st)
  5990. {
  5991. int type;
  5992. int type2;
  5993. int type3;
  5994. int type4;
  5995. int val;
  5996. TBL_PC* sd;
  5997. sd = script_rid2sd(st);
  5998. if( sd == NULL )
  5999. return 0; // no player attached
  6000. type = conv_num(st, script_getdata(st,2));
  6001. switch( script_lastdata(st) ){
  6002. case 3:
  6003. val = conv_num(st, script_getdata(st,3));
  6004. pc_bonus(sd, type, val);
  6005. break;
  6006. case 4:
  6007. type2 = conv_num(st, script_getdata(st,3));
  6008. val = conv_num(st, script_getdata(st,4));
  6009. pc_bonus2(sd, type, type2, val);
  6010. break;
  6011. case 5:
  6012. type2 = conv_num(st, script_getdata(st,3));
  6013. type3 = conv_num(st, script_getdata(st,4));
  6014. val = conv_num(st, script_getdata(st,5));
  6015. pc_bonus3(sd, type, type2, type3, val);
  6016. break;
  6017. case 6:
  6018. type2 = conv_num(st, script_getdata(st,3));
  6019. type3 = conv_num(st, script_getdata(st,4));
  6020. type4 = conv_num(st, script_getdata(st,5));
  6021. val = conv_num(st, script_getdata(st,6));
  6022. pc_bonus4(sd, type, type2, type3, type4, val);
  6023. break;
  6024. default:
  6025. ShowDebug("buildin_bonus: unexpected last data (%d)\n", script_lastdata(st));
  6026. }
  6027. return 0;
  6028. }
  6029. /// Changes the level of a player skill.
  6030. /// skill <skill id>,<level>{,<flag>}
  6031. /// @see pc_skill() for flag
  6032. int buildin_skill(struct script_state* st)
  6033. {
  6034. int id;
  6035. int level;
  6036. int flag = 1;
  6037. TBL_PC* sd;
  6038. sd = script_rid2sd(st);
  6039. if( sd == NULL )
  6040. return 0; // no player attached
  6041. id = conv_num(st, script_getdata(st,2));
  6042. level = conv_num(st, script_getdata(st,3));
  6043. if( script_hasdata(st,4) )
  6044. flag = conv_num(st, script_getdata(st,4));
  6045. pc_skill(sd, id, level, flag);
  6046. return 0;
  6047. }
  6048. /// Changes the level of a player skill.
  6049. /// addtoskill <skill id>,<level>{,<flag>}
  6050. /// @see pc_skill() for flag
  6051. int buildin_addtoskill(struct script_state* st)
  6052. {
  6053. int id;
  6054. int level;
  6055. int flag = 2;
  6056. TBL_PC* sd;
  6057. sd = script_rid2sd(st);
  6058. if( sd == NULL )
  6059. return 0; // no player attached
  6060. id = conv_num(st, script_getdata(st,2));
  6061. level = conv_num(st, script_getdata(st,3));
  6062. if( script_hasdata(st,4) )
  6063. flag = conv_num(st, script_getdata(st,4));
  6064. pc_skill(sd, id, level, flag);
  6065. return 0;
  6066. }
  6067. /// Increases the level of the guild skill.
  6068. /// guildskill <skill id>,<level>
  6069. int buildin_guildskill(struct script_state* st)
  6070. {
  6071. int id;
  6072. int level;
  6073. TBL_PC* sd;
  6074. int i;
  6075. sd = script_rid2sd(st);
  6076. if( sd == NULL )
  6077. return 0; // needs player attached
  6078. id = conv_num(st, script_getdata(st,2));
  6079. level = conv_num(st, script_getdata(st,3));
  6080. for( i=0; i < level; i++ )
  6081. guild_skillup(sd, id);
  6082. return 0;
  6083. }
  6084. /// Returns the level of the player skill.
  6085. /// getskilllv(<skill id>) -> <level>
  6086. int buildin_getskilllv(struct script_state* st)
  6087. {
  6088. int id;
  6089. TBL_PC* sd;
  6090. sd = script_rid2sd(st);
  6091. if( sd == NULL ){
  6092. script_pushint(st, 0);
  6093. return 0; // needs player attached
  6094. }
  6095. id = conv_num(st, script_getdata(st,2));
  6096. script_pushint(st, pc_checkskill(sd,id));
  6097. return 0;
  6098. }
  6099. /// Returns the level of the guild skill.
  6100. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6101. int buildin_getgdskilllv(struct script_state* st)
  6102. {
  6103. int guild_id;
  6104. int skill_id;
  6105. struct guild* g;
  6106. guild_id = conv_num(st, script_getdata(st,2));
  6107. skill_id = conv_num(st, script_getdata(st,3));
  6108. g = guild_search(guild_id);
  6109. if( g == NULL )
  6110. script_pushint(st, -1);
  6111. else
  6112. script_pushint(st, guild_checkskill(g,skill_id));
  6113. return 0;
  6114. /*
  6115. int skill_id;
  6116. TBL_PC* sd;
  6117. struct guild* g = NULL;
  6118. sd = script_rid2sd(st);
  6119. if( sd == NULL )
  6120. return 0; // needs player attached
  6121. skill_id = conv_num(st, script_getdata(st,2));
  6122. if( sd->status.guild_id > 0 )
  6123. g = guild_search(sd->status.guild_id);
  6124. if( g == NULL )
  6125. script_pushint(st, -1);
  6126. else
  6127. script_pushint(st, guild_checkskill(g,skill_id+9999));
  6128. return 0;
  6129. */
  6130. }
  6131. /// Returns the 'basic_skill_check' setting.
  6132. /// basicskillcheck() -> <setting>
  6133. int buildin_basicskillcheck(struct script_state* st)
  6134. {
  6135. script_pushint(st, battle_config.basic_skill_check);
  6136. return 0;
  6137. }
  6138. /// Returns the GM level of the player.
  6139. /// getgmlevel() -> <level>
  6140. int buildin_getgmlevel(struct script_state* st)
  6141. {
  6142. TBL_PC* sd;
  6143. sd = script_rid2sd(st);
  6144. if( sd == NULL ){
  6145. script_pushint(st, 0);
  6146. return 0; // needs player attached
  6147. }
  6148. script_pushint(st, pc_isGM(sd));
  6149. return 0;
  6150. }
  6151. /// Terminates the execution of this script instance.
  6152. /// end
  6153. int buildin_end(struct script_state* st)
  6154. {
  6155. st->state = END;
  6156. return 0;
  6157. }
  6158. /// Checks if the player has that option.
  6159. /// checkoption(<option>) -> <bool>
  6160. int buildin_checkoption(struct script_state* st)
  6161. {
  6162. int option;
  6163. TBL_PC* sd;
  6164. sd = script_rid2sd(st);
  6165. if( sd == NULL )
  6166. return buildin_end(st);// needs player attached
  6167. option = conv_num(st, script_getdata(st,2));
  6168. if( sd->sc.option&option )
  6169. script_pushint(st, 1);
  6170. else
  6171. script_pushint(st, 0);
  6172. return 0;
  6173. }
  6174. /// Checks if the player is in that opt1 state.
  6175. /// checkoption1(<opt1>) -> <bool>
  6176. int buildin_checkoption1(struct script_state* st)
  6177. {
  6178. int opt1;
  6179. TBL_PC* sd;
  6180. sd = script_rid2sd(st);
  6181. if( sd == NULL )
  6182. return buildin_end(st);// needs player attached
  6183. opt1 = conv_num(st, script_getdata(st,2));
  6184. if( sd->sc.opt1 == opt1 )
  6185. script_pushint(st, 1);
  6186. else
  6187. script_pushint(st, 0);
  6188. return 0;
  6189. }
  6190. /// Checks if the player has that opt2.
  6191. /// checkoption2(<opt2>) -> <bool>
  6192. int buildin_checkoption2(struct script_state* st)
  6193. {
  6194. int opt2;
  6195. TBL_PC* sd;
  6196. sd = script_rid2sd(st);
  6197. if( sd == NULL )
  6198. return buildin_end(st);// needs player attached
  6199. opt2 = conv_num(st, script_getdata(st,2));
  6200. if( sd->sc.opt2&opt2 )
  6201. script_pushint(st, 1);
  6202. else
  6203. script_pushint(st, 0);
  6204. return 0;
  6205. }
  6206. /// Changes the option of the player.
  6207. /// setoption <option number>{,<flag>}
  6208. int buildin_setoption(struct script_state* st)
  6209. {
  6210. int option;
  6211. int flag = 1;
  6212. TBL_PC* sd;
  6213. sd = script_rid2sd(st);
  6214. if( sd == NULL )
  6215. return 0;// needs player attached
  6216. option = conv_num(st, script_getdata(st,2));
  6217. if( script_hasdata(st,3) )
  6218. flag = conv_num(st, script_getdata(st,3));
  6219. else if( !option ){// Request to remove everything.
  6220. flag = 0;
  6221. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6222. }
  6223. if( flag ){// Add option
  6224. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6225. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6226. pc_setoption(sd, sd->sc.option|option);
  6227. } else// Remove option
  6228. pc_setoption(sd, sd->sc.option&~option);
  6229. return 0;
  6230. }
  6231. /// Returns if the player has a cart.
  6232. /// checkcart() -> <bool>
  6233. /// @author Valaris
  6234. int buildin_checkcart(struct script_state* st)
  6235. {
  6236. TBL_PC* sd;
  6237. sd = script_rid2sd(st);
  6238. if( sd == NULL )
  6239. return buildin_end(st);// needs player attached
  6240. if( pc_iscarton(sd) )
  6241. script_pushint(st, 1);
  6242. else
  6243. script_pushint(st, 0);
  6244. return 0;
  6245. }
  6246. /// Sets the cart of the player.
  6247. /// setcart {<type>}
  6248. int buildin_setcart(struct script_state* st)
  6249. {
  6250. int type = 1;
  6251. TBL_PC* sd;
  6252. sd = script_rid2sd(st);
  6253. if( sd == NULL )
  6254. return 0;// needs player attached
  6255. if( script_hasdata(st,2) )
  6256. type = conv_num(st, script_getdata(st,2));
  6257. pc_setcart(sd, type);
  6258. return 0;
  6259. }
  6260. /// Returns if the player has a falcon.
  6261. /// checkfalcon() -> <bool>
  6262. /// @author Valaris
  6263. int buildin_checkfalcon(struct script_state* st)
  6264. {
  6265. TBL_PC* sd;
  6266. sd = script_rid2sd(st);
  6267. if( sd == NULL )
  6268. return buildin_end(st);// needs player attached
  6269. if( pc_isfalcon(sd) )
  6270. script_pushint(st, 1);
  6271. else
  6272. script_pushint(st, 0);
  6273. return 0;
  6274. }
  6275. /// Sets if the player has a falcon or not.
  6276. /// setfalcon {<flag>}
  6277. int buildin_setfalcon(struct script_state* st)
  6278. {
  6279. int flag = 1;
  6280. TBL_PC* sd;
  6281. sd = script_rid2sd(st);
  6282. if( sd == NULL )
  6283. return 0;// needs player attached
  6284. if( script_hasdata(st,2) )
  6285. flag = conv_num(st, script_getdata(st,2));
  6286. pc_setfalcon(sd, flag);
  6287. return 0;
  6288. }
  6289. /// Returns if the player is riding.
  6290. /// checkriding() -> <bool>
  6291. /// @author Valaris
  6292. int buildin_checkriding(struct script_state* st)
  6293. {
  6294. TBL_PC* sd;
  6295. sd = script_rid2sd(st);
  6296. if( sd == NULL )
  6297. return buildin_end(st);// needs player attached
  6298. if( pc_isriding(sd) )
  6299. script_pushint(st, 1);
  6300. else
  6301. script_pushint(st, 0);
  6302. return 0;
  6303. }
  6304. /// Sets if the player is riding.
  6305. /// setriding {<flag>}
  6306. int buildin_setriding(struct script_state* st)
  6307. {
  6308. int flag = 1;
  6309. TBL_PC* sd;
  6310. sd = script_rid2sd(st);
  6311. if( sd == NULL )
  6312. return 0;// needs player attached
  6313. if( script_hasdata(st,2) )
  6314. flag = conv_num(st, script_getdata(st,2));
  6315. pc_setriding(sd, flag);
  6316. return 0;
  6317. }
  6318. /// Sets the save point of the player.
  6319. /// save "<map name>",<x>,<y>
  6320. /// savepoint "<map name>",<x>,<y>
  6321. int buildin_savepoint(struct script_state* st)
  6322. {
  6323. int x;
  6324. int y;
  6325. short map;
  6326. char* str;
  6327. TBL_PC* sd;
  6328. sd = script_rid2sd(st);
  6329. if( sd == NULL )
  6330. return 0;// needs player attached
  6331. str = conv_str(st, script_getdata(st,2));
  6332. x = conv_num(st, script_getdata(st,3));
  6333. y = conv_num(st, script_getdata(st,4));
  6334. map = mapindex_name2id(str);
  6335. if( map )
  6336. pc_setsavepoint(sd, map, x, y);
  6337. return 0;
  6338. }
  6339. /*==========================================
  6340. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6341. *------------------------------------------
  6342. */
  6343. int buildin_gettimetick(struct script_state *st) /* Asgard Version */
  6344. {
  6345. int type;
  6346. time_t timer;
  6347. struct tm *t;
  6348. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  6349. switch(type){
  6350. case 2:
  6351. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6352. // from the system clock.)
  6353. push_val(st->stack,C_INT,(int)time(NULL));
  6354. break;
  6355. case 1:
  6356. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6357. time(&timer);
  6358. t=localtime(&timer);
  6359. push_val(st->stack,C_INT,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6360. break;
  6361. case 0:
  6362. default:
  6363. //type 0:(System Ticks)
  6364. push_val(st->stack,C_INT,gettick());
  6365. break;
  6366. }
  6367. return 0;
  6368. }
  6369. /*==========================================
  6370. * GetTime(Type);
  6371. * 1: Sec 2: Min 3: Hour
  6372. * 4: WeekDay 5: MonthDay 6: Month
  6373. * 7: Year
  6374. *------------------------------------------
  6375. */
  6376. int buildin_gettime(struct script_state *st) /* Asgard Version */
  6377. {
  6378. int type;
  6379. time_t timer;
  6380. struct tm *t;
  6381. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  6382. time(&timer);
  6383. t=localtime(&timer);
  6384. switch(type){
  6385. case 1://Sec(0~59)
  6386. push_val(st->stack,C_INT,t->tm_sec);
  6387. break;
  6388. case 2://Min(0~59)
  6389. push_val(st->stack,C_INT,t->tm_min);
  6390. break;
  6391. case 3://Hour(0~23)
  6392. push_val(st->stack,C_INT,t->tm_hour);
  6393. break;
  6394. case 4://WeekDay(0~6)
  6395. push_val(st->stack,C_INT,t->tm_wday);
  6396. break;
  6397. case 5://MonthDay(01~31)
  6398. push_val(st->stack,C_INT,t->tm_mday);
  6399. break;
  6400. case 6://Month(01~12)
  6401. push_val(st->stack,C_INT,t->tm_mon+1);
  6402. break;
  6403. case 7://Year(20xx)
  6404. push_val(st->stack,C_INT,t->tm_year+1900);
  6405. break;
  6406. case 8://Year Day(01~366)
  6407. push_val(st->stack,C_INT,t->tm_yday+1);
  6408. break;
  6409. default://(format error)
  6410. push_val(st->stack,C_INT,-1);
  6411. break;
  6412. }
  6413. return 0;
  6414. }
  6415. /*==========================================
  6416. * GetTimeStr("TimeFMT", Length);
  6417. *------------------------------------------
  6418. */
  6419. int buildin_gettimestr(struct script_state *st)
  6420. {
  6421. char *tmpstr;
  6422. char *fmtstr;
  6423. int maxlen;
  6424. time_t now = time(NULL);
  6425. fmtstr=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6426. maxlen=conv_num(st,& (st->stack->stack_data[st->start+3]));
  6427. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6428. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6429. tmpstr[maxlen]='\0';
  6430. push_str(st->stack,C_STR,tmpstr);
  6431. return 0;
  6432. }
  6433. /*==========================================
  6434. * カプラ倉庫を開く
  6435. *------------------------------------------
  6436. */
  6437. int buildin_openstorage(struct script_state *st)
  6438. {
  6439. storage_storageopen(script_rid2sd(st));
  6440. return 0;
  6441. }
  6442. int buildin_guildopenstorage(struct script_state *st)
  6443. {
  6444. struct map_session_data *sd=script_rid2sd(st);
  6445. int ret;
  6446. ret = storage_guild_storageopen(sd);
  6447. push_val(st->stack,C_INT,ret);
  6448. return 0;
  6449. }
  6450. /*==========================================
  6451. * アイテムによるスキル発動
  6452. *------------------------------------------
  6453. */
  6454. int buildin_itemskill(struct script_state *st)
  6455. {
  6456. int id,lv;
  6457. char *str;
  6458. struct map_session_data *sd=script_rid2sd(st);
  6459. id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  6460. lv=conv_num(st,& (st->stack->stack_data[st->start+3]));
  6461. str=conv_str(st,& (st->stack->stack_data[st->start+4]));
  6462. // 詠唱中にスキルアイテムは使用できない
  6463. if(sd->ud.skilltimer != -1)
  6464. return 0;
  6465. sd->skillitem=id;
  6466. sd->skillitemlv=lv;
  6467. clif_item_skill(sd,id,lv,str);
  6468. return 0;
  6469. }
  6470. /*==========================================
  6471. * アイテム作成
  6472. *------------------------------------------
  6473. */
  6474. int buildin_produce(struct script_state *st)
  6475. {
  6476. int trigger;
  6477. struct map_session_data *sd=script_rid2sd(st);
  6478. trigger=conv_num(st,& (st->stack->stack_data[st->start+2]));
  6479. clif_skill_produce_mix_list(sd, trigger);
  6480. return 0;
  6481. }
  6482. /*==========================================
  6483. * NPCでペット作る
  6484. *------------------------------------------
  6485. */
  6486. int buildin_makepet(struct script_state *st)
  6487. {
  6488. struct map_session_data *sd = script_rid2sd(st);
  6489. struct script_data *data;
  6490. int id,pet_id;
  6491. data=&(st->stack->stack_data[st->start+2]);
  6492. get_val(st,data);
  6493. id=conv_num(st,data);
  6494. pet_id = search_petDB_index(id, PET_CLASS);
  6495. if (pet_id < 0)
  6496. pet_id = search_petDB_index(id, PET_EGG);
  6497. if (pet_id >= 0 && sd) {
  6498. sd->catch_target_class = pet_db[pet_id].class_;
  6499. intif_create_pet(
  6500. sd->status.account_id, sd->status.char_id,
  6501. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6502. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6503. 100, 0, 1, pet_db[pet_id].jname);
  6504. }
  6505. return 0;
  6506. }
  6507. /*==========================================
  6508. * NPCで経験値上げる
  6509. *------------------------------------------
  6510. */
  6511. int buildin_getexp(struct script_state *st)
  6512. {
  6513. struct map_session_data *sd = script_rid2sd(st);
  6514. int base=0,job=0;
  6515. base=conv_num(st,& (st->stack->stack_data[st->start+2]));
  6516. job =conv_num(st,& (st->stack->stack_data[st->start+3]));
  6517. if(base<0 || job<0)
  6518. return 0;
  6519. if(sd)
  6520. pc_gainexp(sd,NULL,base,job);
  6521. return 0;
  6522. }
  6523. /*==========================================
  6524. * Gain guild exp [Celest]
  6525. *------------------------------------------
  6526. */
  6527. int buildin_guildgetexp(struct script_state *st)
  6528. {
  6529. struct map_session_data *sd = script_rid2sd(st);
  6530. int exp;
  6531. exp = conv_num(st,& (st->stack->stack_data[st->start+2]));
  6532. if(exp < 0)
  6533. return 0;
  6534. if(sd && sd->status.guild_id > 0)
  6535. guild_getexp (sd, exp);
  6536. return 0;
  6537. }
  6538. /*==========================================
  6539. * Changes the guild master of a guild [Skotlex]
  6540. *------------------------------------------
  6541. */
  6542. int buildin_guildchangegm(struct script_state *st)
  6543. {
  6544. struct map_session_data *sd;
  6545. int guild_id;
  6546. char *name;
  6547. guild_id = conv_num(st,& (st->stack->stack_data[st->start+2]));
  6548. name = conv_str(st,& (st->stack->stack_data[st->start+3]));
  6549. sd=map_nick2sd(name);
  6550. if (!sd)
  6551. push_val(st->stack,C_INT,0);
  6552. else
  6553. push_val(st->stack,C_INT,guild_gm_change(guild_id, sd));
  6554. return 0;
  6555. }
  6556. /*==========================================
  6557. * モンスター発生
  6558. *------------------------------------------
  6559. */
  6560. int buildin_monster(struct script_state *st)
  6561. {
  6562. int class_,amount,x,y;
  6563. char *str,*map,*event="";
  6564. map =conv_str(st,& (st->stack->stack_data[st->start+2]));
  6565. x =conv_num(st,& (st->stack->stack_data[st->start+3]));
  6566. y =conv_num(st,& (st->stack->stack_data[st->start+4]));
  6567. str =conv_str(st,& (st->stack->stack_data[st->start+5]));
  6568. class_=conv_num(st,& (st->stack->stack_data[st->start+6]));
  6569. amount=conv_num(st,& (st->stack->stack_data[st->start+7]));
  6570. if( st->end>st->start+8 ){
  6571. event=conv_str(st,& (st->stack->stack_data[st->start+8]));
  6572. check_event(st, event);
  6573. }
  6574. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6575. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6576. return 1;
  6577. }
  6578. mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,amount,event);
  6579. return 0;
  6580. }
  6581. /*==========================================
  6582. * モンスター発生
  6583. *------------------------------------------
  6584. */
  6585. int buildin_areamonster(struct script_state *st)
  6586. {
  6587. int class_,amount,x0,y0,x1,y1;
  6588. char *str,*map,*event="";
  6589. map =conv_str(st,& (st->stack->stack_data[st->start+2]));
  6590. x0 =conv_num(st,& (st->stack->stack_data[st->start+3]));
  6591. y0 =conv_num(st,& (st->stack->stack_data[st->start+4]));
  6592. x1 =conv_num(st,& (st->stack->stack_data[st->start+5]));
  6593. y1 =conv_num(st,& (st->stack->stack_data[st->start+6]));
  6594. str =conv_str(st,& (st->stack->stack_data[st->start+7]));
  6595. class_=conv_num(st,& (st->stack->stack_data[st->start+8]));
  6596. amount=conv_num(st,& (st->stack->stack_data[st->start+9]));
  6597. if( st->end>st->start+10 ){
  6598. event=conv_str(st,& (st->stack->stack_data[st->start+10]));
  6599. check_event(st, event);
  6600. }
  6601. mob_once_spawn_area(map_id2sd(st->rid),map,x0,y0,x1,y1,str,class_,amount,event);
  6602. return 0;
  6603. }
  6604. /*==========================================
  6605. * モンスター削除
  6606. *------------------------------------------
  6607. */
  6608. int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6609. {
  6610. TBL_MOB* md = (TBL_MOB*)bl;
  6611. char *event=va_arg(ap,char *);
  6612. int allflag=va_arg(ap,int);
  6613. if(!allflag){
  6614. if(strcmp(event,md->npc_event)==0)
  6615. status_kill(bl);
  6616. }else{
  6617. if(!md->spawn)
  6618. status_kill(bl);
  6619. }
  6620. return 0;
  6621. }
  6622. int buildin_killmonster(struct script_state *st)
  6623. {
  6624. char *mapname,*event;
  6625. int m,allflag=0;
  6626. mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6627. event=conv_str(st,& (st->stack->stack_data[st->start+3]));
  6628. if(strcmp(event,"All")==0)
  6629. allflag = 1;
  6630. else
  6631. check_event(st, event);
  6632. if( (m=map_mapname2mapid(mapname))<0 )
  6633. return 0;
  6634. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6635. return 0;
  6636. }
  6637. int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6638. {
  6639. status_kill(bl);
  6640. return 0;
  6641. }
  6642. int buildin_killmonsterall(struct script_state *st)
  6643. {
  6644. char *mapname;
  6645. int m;
  6646. mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6647. if( (m=map_mapname2mapid(mapname))<0 )
  6648. return 0;
  6649. map_foreachinmap(buildin_killmonsterall_sub,
  6650. m,BL_MOB);
  6651. return 0;
  6652. }
  6653. /*==========================================
  6654. * Creates a clone of a player.
  6655. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6656. *------------------------------------------
  6657. */
  6658. int buildin_clone(struct script_state *st) {
  6659. struct map_session_data *sd, *msd=NULL;
  6660. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6661. unsigned int duration = 0;
  6662. char *map,*event="";
  6663. map=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6664. x=conv_num(st,& (st->stack->stack_data[st->start+3]));
  6665. y=conv_num(st,& (st->stack->stack_data[st->start+4]));
  6666. event=conv_str(st,& (st->stack->stack_data[st->start+5]));
  6667. char_id=conv_num(st,& (st->stack->stack_data[st->start+6]));
  6668. if( st->end>st->start+7 )
  6669. master_id=conv_num(st,& (st->stack->stack_data[st->start+7]));
  6670. if( st->end>st->start+8 )
  6671. mode=conv_num(st,& (st->stack->stack_data[st->start+8]));
  6672. if( st->end>st->start+9 )
  6673. flag=conv_num(st,& (st->stack->stack_data[st->start+9]));
  6674. if( st->end>st->start+10 )
  6675. duration=conv_num(st,& (st->stack->stack_data[st->start+10]));
  6676. check_event(st, event);
  6677. m = map_mapname2mapid(map);
  6678. if (m < 0) return 0;
  6679. sd = map_charid2sd(char_id);
  6680. if (master_id) {
  6681. msd = map_charid2sd(master_id);
  6682. if (msd)
  6683. master_id = msd->bl.id;
  6684. else
  6685. master_id = 0;
  6686. }
  6687. if (sd) //Return ID of newly crafted clone.
  6688. push_val(st->stack,C_INT,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6689. else //Failed to create clone.
  6690. push_val(st->stack,C_INT,0);
  6691. return 0;
  6692. }
  6693. /*==========================================
  6694. * イベント実行
  6695. *------------------------------------------
  6696. */
  6697. int buildin_doevent(struct script_state *st)
  6698. {
  6699. char *event;
  6700. event=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6701. check_event(st, event);
  6702. npc_event(map_id2sd(st->rid),event,0);
  6703. return 0;
  6704. }
  6705. /*==========================================
  6706. * NPC主体イベント実行
  6707. *------------------------------------------
  6708. */
  6709. int buildin_donpcevent(struct script_state *st)
  6710. {
  6711. char *event;
  6712. event=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6713. check_event(st, event);
  6714. npc_event_do(event);
  6715. return 0;
  6716. }
  6717. /*==========================================
  6718. * イベントタイマー追加
  6719. *------------------------------------------
  6720. */
  6721. int buildin_addtimer(struct script_state *st)
  6722. {
  6723. char *event;
  6724. int tick;
  6725. tick=conv_num(st,& (st->stack->stack_data[st->start+2]));
  6726. event=conv_str(st,& (st->stack->stack_data[st->start+3]));
  6727. check_event(st, event);
  6728. pc_addeventtimer(script_rid2sd(st),tick,event);
  6729. return 0;
  6730. }
  6731. /*==========================================
  6732. * イベントタイマー削除
  6733. *------------------------------------------
  6734. */
  6735. int buildin_deltimer(struct script_state *st)
  6736. {
  6737. char *event;
  6738. event=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6739. check_event(st, event);
  6740. pc_deleventtimer(script_rid2sd(st),event);
  6741. return 0;
  6742. }
  6743. /*==========================================
  6744. * イベントタイマーのカウント値追加
  6745. *------------------------------------------
  6746. */
  6747. int buildin_addtimercount(struct script_state *st)
  6748. {
  6749. char *event;
  6750. int tick;
  6751. event=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6752. tick=conv_num(st,& (st->stack->stack_data[st->start+3]));
  6753. check_event(st, event);
  6754. pc_addeventtimercount(script_rid2sd(st),event,tick);
  6755. return 0;
  6756. }
  6757. /*==========================================
  6758. * NPCタイマー初期化
  6759. *------------------------------------------
  6760. */
  6761. int buildin_initnpctimer(struct script_state *st)
  6762. {
  6763. struct npc_data *nd;
  6764. if( st->end > st->start+2 )
  6765. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+2])));
  6766. else
  6767. nd=(struct npc_data *)map_id2bl(st->oid);
  6768. npc_settimerevent_tick(nd,0);
  6769. npc_timerevent_start(nd, st->rid);
  6770. return 0;
  6771. }
  6772. /*==========================================
  6773. * NPCタイマー開始
  6774. *------------------------------------------
  6775. */
  6776. int buildin_startnpctimer(struct script_state *st)
  6777. {
  6778. struct npc_data *nd;
  6779. if( st->end > st->start+2 )
  6780. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+2])));
  6781. else
  6782. nd=(struct npc_data *)map_id2bl(st->oid);
  6783. npc_timerevent_start(nd, st->rid);
  6784. return 0;
  6785. }
  6786. /*==========================================
  6787. * NPCタイマー停止
  6788. *------------------------------------------
  6789. */
  6790. int buildin_stopnpctimer(struct script_state *st)
  6791. {
  6792. struct npc_data *nd;
  6793. if( st->end > st->start+2 )
  6794. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+2])));
  6795. else
  6796. nd=(struct npc_data *)map_id2bl(st->oid);
  6797. npc_timerevent_stop(nd);
  6798. return 0;
  6799. }
  6800. /*==========================================
  6801. * NPCタイマー情報所得
  6802. *------------------------------------------
  6803. */
  6804. int buildin_getnpctimer(struct script_state *st)
  6805. {
  6806. struct npc_data *nd;
  6807. struct map_session_data *sd;
  6808. int type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  6809. int val=0;
  6810. if( st->end > st->start+3 )
  6811. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+3])));
  6812. else
  6813. nd=(struct npc_data *)map_id2bl(st->oid);
  6814. if (!nd || nd->bl.type != BL_NPC)
  6815. {
  6816. push_val(st->stack,C_INT,0);
  6817. if (battle_config.error_log)
  6818. ShowError("getnpctimer: Invalid NPC\n");
  6819. return 1;
  6820. }
  6821. switch(type){
  6822. case 0: val=npc_gettimerevent_tick(nd); break;
  6823. case 1:
  6824. if (nd->u.scr.rid) {
  6825. sd = map_id2sd(nd->u.scr.rid);
  6826. if (!sd) {
  6827. if(battle_config.error_log)
  6828. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6829. break;
  6830. }
  6831. val = (sd->npc_timer_id != -1);
  6832. } else
  6833. val= (nd->u.scr.timerid !=-1);
  6834. break;
  6835. case 2: val= nd->u.scr.timeramount; break;
  6836. }
  6837. push_val(st->stack,C_INT,val);
  6838. return 0;
  6839. }
  6840. /*==========================================
  6841. * NPCタイマー値設定
  6842. *------------------------------------------
  6843. */
  6844. int buildin_setnpctimer(struct script_state *st)
  6845. {
  6846. int tick;
  6847. struct npc_data *nd;
  6848. tick=conv_num(st,& (st->stack->stack_data[st->start+2]));
  6849. if( st->end > st->start+3 )
  6850. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+3])));
  6851. else
  6852. nd=(struct npc_data *)map_id2bl(st->oid);
  6853. npc_settimerevent_tick(nd,tick);
  6854. return 0;
  6855. }
  6856. /*==========================================
  6857. * attaches the player rid to the timer [Celest]
  6858. *------------------------------------------
  6859. */
  6860. int buildin_attachnpctimer(struct script_state *st)
  6861. {
  6862. struct map_session_data *sd;
  6863. struct npc_data *nd;
  6864. nd=(struct npc_data *)map_id2bl(st->oid);
  6865. if( st->end > st->start+2 ) {
  6866. char *name = conv_str(st,& (st->stack->stack_data[st->start+2]));
  6867. sd=map_nick2sd(name);
  6868. } else {
  6869. sd = script_rid2sd(st);
  6870. }
  6871. if (sd==NULL)
  6872. return 0;
  6873. nd->u.scr.rid = sd->bl.id;
  6874. return 0;
  6875. }
  6876. /*==========================================
  6877. * detaches a player rid from the timer [Celest]
  6878. *------------------------------------------
  6879. */
  6880. int buildin_detachnpctimer(struct script_state *st)
  6881. {
  6882. struct npc_data *nd;
  6883. if( st->end > st->start+2 )
  6884. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+2])));
  6885. else
  6886. nd=(struct npc_data *)map_id2bl(st->oid);
  6887. nd->u.scr.rid = 0;
  6888. return 0;
  6889. }
  6890. /*==========================================
  6891. * To avoid "player not attached" script errors, this function is provided,
  6892. * it checks if there is a player attached to the current script. [Skotlex]
  6893. * If no, returns 0, if yes, returns the char_id of the attached player.
  6894. *------------------------------------------
  6895. */
  6896. int buildin_playerattached(struct script_state *st)
  6897. {
  6898. struct map_session_data *sd;
  6899. if (st->rid == 0 || (sd = map_id2sd(st->rid)) == NULL)
  6900. push_val(st->stack,C_INT,0);
  6901. else
  6902. push_val(st->stack,C_INT,st->rid);
  6903. return 0;
  6904. }
  6905. /*==========================================
  6906. * 天の声アナウンス
  6907. *------------------------------------------
  6908. */
  6909. int buildin_announce(struct script_state *st)
  6910. {
  6911. char *str, *color=NULL;
  6912. int flag;
  6913. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6914. flag=conv_num(st,& (st->stack->stack_data[st->start+3]));
  6915. if (st->end>st->start+4)
  6916. color=conv_str(st,& (st->stack->stack_data[st->start+4]));
  6917. if(flag&0x0f){
  6918. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  6919. (struct block_list *)script_rid2sd(st);
  6920. if (color)
  6921. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  6922. else
  6923. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  6924. }else {
  6925. if (color)
  6926. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  6927. else
  6928. intif_GMmessage(str,(int)strlen(str)+1,flag);
  6929. }
  6930. return 0;
  6931. }
  6932. /*==========================================
  6933. * 天の声アナウンス(特定マップ)
  6934. *------------------------------------------
  6935. */
  6936. int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  6937. {
  6938. char *str, *color;
  6939. int len,flag;
  6940. str=va_arg(ap,char *);
  6941. len=va_arg(ap,int);
  6942. flag=va_arg(ap,int);
  6943. color=va_arg(ap,char *);
  6944. if (color)
  6945. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  6946. else
  6947. clif_GMmessage(bl,str,len,flag|3);
  6948. return 0;
  6949. }
  6950. int buildin_mapannounce(struct script_state *st)
  6951. {
  6952. char *mapname,*str, *color=NULL;
  6953. int flag,m;
  6954. mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6955. str=conv_str(st,& (st->stack->stack_data[st->start+3]));
  6956. flag=conv_num(st,& (st->stack->stack_data[st->start+4]));
  6957. if (st->end>st->start+5)
  6958. color=conv_str(st,& (st->stack->stack_data[st->start+5]));
  6959. if( (m=map_mapname2mapid(mapname))<0 )
  6960. return 0;
  6961. map_foreachinmap(buildin_mapannounce_sub,
  6962. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  6963. return 0;
  6964. }
  6965. /*==========================================
  6966. * 天の声アナウンス(特定エリア)
  6967. *------------------------------------------
  6968. */
  6969. int buildin_areaannounce(struct script_state *st)
  6970. {
  6971. char *map,*str,*color=NULL;
  6972. int flag,m;
  6973. int x0,y0,x1,y1;
  6974. map=conv_str(st,& (st->stack->stack_data[st->start+2]));
  6975. x0=conv_num(st,& (st->stack->stack_data[st->start+3]));
  6976. y0=conv_num(st,& (st->stack->stack_data[st->start+4]));
  6977. x1=conv_num(st,& (st->stack->stack_data[st->start+5]));
  6978. y1=conv_num(st,& (st->stack->stack_data[st->start+6]));
  6979. str=conv_str(st,& (st->stack->stack_data[st->start+7]));
  6980. flag=conv_num(st,& (st->stack->stack_data[st->start+8]));
  6981. if (st->end>st->start+9)
  6982. color=conv_str(st,& (st->stack->stack_data[st->start+9]));
  6983. if( (m=map_mapname2mapid(map))<0 )
  6984. return 0;
  6985. map_foreachinarea(buildin_mapannounce_sub,
  6986. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  6987. return 0;
  6988. }
  6989. /*==========================================
  6990. * ユーザー数所得
  6991. *------------------------------------------
  6992. */
  6993. int buildin_getusers(struct script_state *st)
  6994. {
  6995. int flag=conv_num(st,& (st->stack->stack_data[st->start+2]));
  6996. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  6997. int val=0;
  6998. switch(flag&0x07){
  6999. case 0: val=map[bl->m].users; break;
  7000. case 1: val=map_getusers(); break;
  7001. }
  7002. push_val(st->stack,C_INT,val);
  7003. return 0;
  7004. }
  7005. /*==========================================
  7006. * Works like @WHO - displays all online users names in window
  7007. *------------------------------------------
  7008. */
  7009. int buildin_getusersname(struct script_state *st)
  7010. {
  7011. struct map_session_data *sd, *pl_sd = NULL, **pl_allsd;
  7012. int i=0,disp_num=1, users;
  7013. sd = script_rid2sd(st);
  7014. if (!sd) return 0;
  7015. pl_allsd = map_getallusers(&users);
  7016. for (i=0;i<users;i++)
  7017. {
  7018. pl_sd = pl_allsd[i];
  7019. if( !(battle_config.hide_GM_session && pc_isGM(pl_sd)) )
  7020. {
  7021. if((disp_num++)%10==0)
  7022. clif_scriptnext(sd,st->oid);
  7023. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7024. }
  7025. }
  7026. return 0;
  7027. }
  7028. /*==========================================
  7029. * getmapguildusers("mapname.gat",guild ID) Returns the number guild members present on a map [Reddozen]
  7030. *------------------------------------------
  7031. */
  7032. int buildin_getmapguildusers(struct script_state *st) {
  7033. char *str;
  7034. int m, gid;
  7035. int i=0,c=0;
  7036. struct guild *g = NULL;
  7037. str=conv_str(st, &(st->stack->stack_data[st->start+2]));
  7038. gid=conv_num(st, &(st->stack->stack_data[st->start+3]));
  7039. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7040. push_val(st->stack, C_INT, -1);
  7041. return 0;
  7042. }
  7043. g = guild_search(gid);
  7044. if (g){
  7045. for(i = 0; i < g->max_member; i++)
  7046. {
  7047. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7048. c++;
  7049. }
  7050. }
  7051. push_val(st->stack, C_INT, c);
  7052. return 0;
  7053. }
  7054. /*==========================================
  7055. * マップ指定ユーザー数所得
  7056. *------------------------------------------
  7057. */
  7058. int buildin_getmapusers(struct script_state *st)
  7059. {
  7060. char *str;
  7061. int m;
  7062. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7063. if( (m=map_mapname2mapid(str))< 0){
  7064. push_val(st->stack,C_INT,-1);
  7065. return 0;
  7066. }
  7067. push_val(st->stack,C_INT,map[m].users);
  7068. return 0;
  7069. }
  7070. /*==========================================
  7071. * エリア指定ユーザー数所得
  7072. *------------------------------------------
  7073. */
  7074. int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7075. {
  7076. int *users=va_arg(ap,int *);
  7077. (*users)++;
  7078. return 0;
  7079. }
  7080. int buildin_getareausers(struct script_state *st)
  7081. {
  7082. char *str;
  7083. int m,x0,y0,x1,y1,users=0;
  7084. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7085. x0=conv_num(st,& (st->stack->stack_data[st->start+3]));
  7086. y0=conv_num(st,& (st->stack->stack_data[st->start+4]));
  7087. x1=conv_num(st,& (st->stack->stack_data[st->start+5]));
  7088. y1=conv_num(st,& (st->stack->stack_data[st->start+6]));
  7089. if( (m=map_mapname2mapid(str))< 0){
  7090. push_val(st->stack,C_INT,-1);
  7091. return 0;
  7092. }
  7093. map_foreachinarea(buildin_getareausers_sub,
  7094. m,x0,y0,x1,y1,BL_PC,&users);
  7095. push_val(st->stack,C_INT,users);
  7096. return 0;
  7097. }
  7098. /*==========================================
  7099. * エリア指定ドロップアイテム数所得
  7100. *------------------------------------------
  7101. */
  7102. int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7103. {
  7104. int item=va_arg(ap,int);
  7105. int *amount=va_arg(ap,int *);
  7106. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7107. if(drop->item_data.nameid==item)
  7108. (*amount)+=drop->item_data.amount;
  7109. return 0;
  7110. }
  7111. int buildin_getareadropitem(struct script_state *st)
  7112. {
  7113. char *str;
  7114. int m,x0,y0,x1,y1,item,amount=0;
  7115. struct script_data *data;
  7116. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7117. x0=conv_num(st,& (st->stack->stack_data[st->start+3]));
  7118. y0=conv_num(st,& (st->stack->stack_data[st->start+4]));
  7119. x1=conv_num(st,& (st->stack->stack_data[st->start+5]));
  7120. y1=conv_num(st,& (st->stack->stack_data[st->start+6]));
  7121. data=&(st->stack->stack_data[st->start+7]);
  7122. get_val(st,data);
  7123. if( data->type==C_STR || data->type==C_CONSTSTR ){
  7124. const char *name=conv_str(st,data);
  7125. struct item_data *item_data = itemdb_searchname(name);
  7126. item=UNKNOWN_ITEM_ID;
  7127. if( item_data )
  7128. item=item_data->nameid;
  7129. }else
  7130. item=conv_num(st,data);
  7131. if( (m=map_mapname2mapid(str))< 0){
  7132. push_val(st->stack,C_INT,-1);
  7133. return 0;
  7134. }
  7135. map_foreachinarea(buildin_getareadropitem_sub,
  7136. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7137. push_val(st->stack,C_INT,amount);
  7138. return 0;
  7139. }
  7140. /*==========================================
  7141. * NPCの有効化
  7142. *------------------------------------------
  7143. */
  7144. int buildin_enablenpc(struct script_state *st)
  7145. {
  7146. char *str;
  7147. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7148. npc_enable(str,1);
  7149. return 0;
  7150. }
  7151. /*==========================================
  7152. * NPCの無効化
  7153. *------------------------------------------
  7154. */
  7155. int buildin_disablenpc(struct script_state *st)
  7156. {
  7157. char *str;
  7158. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7159. npc_enable(str,0);
  7160. return 0;
  7161. }
  7162. int buildin_enablearena(struct script_state *st) // Added by RoVeRT
  7163. {
  7164. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  7165. struct chat_data *cd;
  7166. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL)
  7167. return 0;
  7168. npc_enable(nd->name,1);
  7169. nd->arenaflag=1;
  7170. if(cd->users>=cd->trigger && cd->npc_event[0])
  7171. npc_timer_event(cd->npc_event);
  7172. return 0;
  7173. }
  7174. int buildin_disablearena(struct script_state *st) // Added by RoVeRT
  7175. {
  7176. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  7177. nd->arenaflag=0;
  7178. return 0;
  7179. }
  7180. /*==========================================
  7181. * 隠れているNPCの表示
  7182. *------------------------------------------
  7183. */
  7184. int buildin_hideoffnpc(struct script_state *st)
  7185. {
  7186. char *str;
  7187. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7188. npc_enable(str,2);
  7189. return 0;
  7190. }
  7191. /*==========================================
  7192. * NPCをハイディング
  7193. *------------------------------------------
  7194. */
  7195. int buildin_hideonnpc(struct script_state *st)
  7196. {
  7197. char *str;
  7198. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7199. npc_enable(str,4);
  7200. return 0;
  7201. }
  7202. /*==========================================
  7203. * 状態異常にかかる
  7204. *------------------------------------------
  7205. */
  7206. int buildin_sc_start(struct script_state *st)
  7207. {
  7208. struct block_list *bl;
  7209. int type,tick,val1,val4=0;
  7210. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  7211. tick=conv_num(st,& (st->stack->stack_data[st->start+3]));
  7212. val1=conv_num(st,& (st->stack->stack_data[st->start+4]));
  7213. if( st->end>st->start+5 ) //指定したキャラを状態異常にする
  7214. bl = map_id2bl(conv_num(st,& (st->stack->stack_data[st->start+5])));
  7215. else
  7216. bl = map_id2bl(st->rid);
  7217. if (potion_flag==1 && potion_target) {
  7218. bl = map_id2bl(potion_target);
  7219. tick/=2; //Thrown potions only last half.
  7220. val4 = 1; //Mark that this was a thrown sc_effect
  7221. }
  7222. if (type >= 0 && type < SC_MAX && val1 && !tick)
  7223. { //When there isn't a duration specified, try to get it from the skill_db
  7224. tick = StatusSkillChangeTable[type];
  7225. if (tick)
  7226. tick = skill_get_time(tick,val1);
  7227. else //Failed to retrieve duration, reset to what it was.
  7228. tick = 0;
  7229. }
  7230. if (bl)
  7231. status_change_start(bl,type,10000,val1,0,0,val4,tick,11);
  7232. return 0;
  7233. }
  7234. /*==========================================
  7235. * 状態異常にかかる(確率指定)
  7236. *------------------------------------------
  7237. */
  7238. int buildin_sc_start2(struct script_state *st)
  7239. {
  7240. struct block_list *bl;
  7241. int type,tick,val1,val4=0,per;
  7242. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  7243. tick=conv_num(st,& (st->stack->stack_data[st->start+3]));
  7244. val1=conv_num(st,& (st->stack->stack_data[st->start+4]));
  7245. per=conv_num(st,& (st->stack->stack_data[st->start+5]));
  7246. if( st->end>st->start+6 ) //指定したキャラを状態異常にする
  7247. bl = map_id2bl(conv_num(st,& (st->stack->stack_data[st->start+6])));
  7248. else
  7249. bl = map_id2bl(st->rid);
  7250. if (type >= 0 && type < SC_MAX && val1 && !tick)
  7251. { //When there isn't a duration specified, try to get it from the skill_db
  7252. tick = StatusSkillChangeTable[type];
  7253. if (tick)
  7254. tick = skill_get_time(tick,val1);
  7255. else //Failed to retrieve duration, reset to what it was.
  7256. tick = 0;
  7257. }
  7258. if (potion_flag==1 && potion_target) {
  7259. bl = map_id2bl(potion_target);
  7260. tick/=2;
  7261. val4 = 1;
  7262. }
  7263. if(bl)
  7264. status_change_start(bl,type,per,val1,0,0,val4,tick,11);
  7265. return 0;
  7266. }
  7267. /*==========================================
  7268. * Starts a SC_ change with the four values passed. [Skotlex]
  7269. * Final optional argument is the ID of player to affect.
  7270. * sc_start4 type, duration, val1, val2, val3, val4, <id>;
  7271. *------------------------------------------
  7272. */
  7273. int buildin_sc_start4(struct script_state *st)
  7274. {
  7275. struct block_list *bl;
  7276. int type,tick,val1,val2,val3,val4;
  7277. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  7278. tick=conv_num(st,& (st->stack->stack_data[st->start+3]));
  7279. val1=conv_num(st,& (st->stack->stack_data[st->start+4]));
  7280. val2=conv_num(st,& (st->stack->stack_data[st->start+5]));
  7281. val3=conv_num(st,& (st->stack->stack_data[st->start+6]));
  7282. val4=conv_num(st,& (st->stack->stack_data[st->start+7]));
  7283. if( st->end>st->start+8 )
  7284. bl = map_id2bl(conv_num(st,& (st->stack->stack_data[st->start+8])));
  7285. else
  7286. bl = map_id2bl(st->rid);
  7287. if (type >= 0 && type < SC_MAX && val1 && !tick)
  7288. { //When there isn't a duration specified, try to get it from the skill_db
  7289. tick = StatusSkillChangeTable[type];
  7290. if (tick)
  7291. tick = skill_get_time(tick,val1);
  7292. else //Failed to retrieve duration, reset to what it was.
  7293. tick = 0;
  7294. }
  7295. if (potion_flag==1 && potion_target) {
  7296. bl = map_id2bl(potion_target);
  7297. tick/=2;
  7298. }
  7299. if (bl)
  7300. status_change_start(bl,type,10000,val1,val2,val3,val4,tick,11);
  7301. return 0;
  7302. }
  7303. /*==========================================
  7304. * 状態異常が直る
  7305. *------------------------------------------
  7306. */
  7307. int buildin_sc_end(struct script_state *st)
  7308. {
  7309. struct block_list *bl;
  7310. int type;
  7311. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  7312. bl = map_id2bl(st->rid);
  7313. if (potion_flag==1 && potion_target)
  7314. bl = map_id2bl(potion_target);
  7315. if (!bl) return 0;
  7316. if (type >= 0)
  7317. status_change_end(bl,type,-1);
  7318. else
  7319. status_change_clear(bl, 2);
  7320. return 0;
  7321. }
  7322. /*==========================================
  7323. * 状態異常耐性を計算した確率を返す
  7324. *------------------------------------------
  7325. */
  7326. int buildin_getscrate(struct script_state *st)
  7327. {
  7328. struct block_list *bl;
  7329. int sc_def=0,type,rate;
  7330. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  7331. rate=conv_num(st,& (st->stack->stack_data[st->start+3]));
  7332. if( st->end>st->start+4 ) //指定したキャラの耐性を計算する
  7333. bl = map_id2bl(conv_num(st,& (st->stack->stack_data[st->start+4])));
  7334. else
  7335. bl = map_id2bl(st->rid);
  7336. if (bl)
  7337. sc_def = status_get_sc_def(bl,type);
  7338. rate = rate*(10000-sc_def)/10000;
  7339. push_val(st->stack,C_INT,rate<0?0:rate);
  7340. return 0;
  7341. }
  7342. /*==========================================
  7343. *
  7344. *------------------------------------------
  7345. */
  7346. int buildin_debugmes(struct script_state *st)
  7347. {
  7348. conv_str(st,& (st->stack->stack_data[st->start+2]));
  7349. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,st->stack->stack_data[st->start+2].u.str);
  7350. return 0;
  7351. }
  7352. /*==========================================
  7353. *捕獲アイテム使用
  7354. *------------------------------------------
  7355. */
  7356. int buildin_catchpet(struct script_state *st)
  7357. {
  7358. int pet_id;
  7359. struct map_session_data *sd;
  7360. pet_id= conv_num(st,& (st->stack->stack_data[st->start+2]));
  7361. sd=script_rid2sd(st);
  7362. pet_catch_process1(sd,pet_id);
  7363. return 0;
  7364. }
  7365. /*==========================================
  7366. * [orn]
  7367. *------------------------------------------
  7368. */
  7369. int buildin_homunculus_evolution(struct script_state *st)
  7370. {
  7371. struct map_session_data *sd;
  7372. sd=script_rid2sd(st);
  7373. if ( sd->hd && sd->hd->homunculusDB->evo_class && sd->hd->homunculus.intimacy > 91000 ) {
  7374. return !merc_hom_evolution(sd->hd) ;
  7375. }
  7376. clif_emotion(&sd->hd->bl, 4) ; //swt
  7377. return 0;
  7378. }
  7379. //These two functions bring the eA MAPID_* class functionality to scripts.
  7380. int buildin_eaclass(struct script_state *st)
  7381. {
  7382. int class_;
  7383. if( st->end>st->start+2 )
  7384. class_ = conv_num(st,& (st->stack->stack_data[st->start+2]));
  7385. else {
  7386. struct map_session_data *sd;
  7387. sd=script_rid2sd(st);
  7388. if (!sd) {
  7389. push_val(st->stack,C_INT, -1);
  7390. return 0;
  7391. }
  7392. class_ = sd->status.class_;
  7393. }
  7394. push_val(st->stack,C_INT, pc_jobid2mapid(class_));
  7395. return 0;
  7396. }
  7397. int buildin_roclass(struct script_state *st)
  7398. {
  7399. int class_ =conv_num(st,& (st->stack->stack_data[st->start+2]));
  7400. int sex;
  7401. if( st->end>st->start+3 )
  7402. sex = conv_num(st,& (st->stack->stack_data[st->start+3]));
  7403. else {
  7404. struct map_session_data *sd;
  7405. if (st->rid && (sd=script_rid2sd(st)))
  7406. sex = sd->status.sex;
  7407. else
  7408. sex = 1; //Just use male when not found.
  7409. }
  7410. push_val(st->stack,C_INT,pc_mapid2jobid(class_, sex));
  7411. return 0;
  7412. }
  7413. /*==========================================
  7414. *携帯卵孵化機使用
  7415. *------------------------------------------
  7416. */
  7417. int buildin_birthpet(struct script_state *st)
  7418. {
  7419. struct map_session_data *sd;
  7420. sd=script_rid2sd(st);
  7421. clif_sendegg(sd);
  7422. return 0;
  7423. }
  7424. /*==========================================
  7425. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7426. *------------------------------------------
  7427. */
  7428. int buildin_resetlvl(struct script_state *st)
  7429. {
  7430. struct map_session_data *sd;
  7431. int type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  7432. sd=script_rid2sd(st);
  7433. pc_resetlvl(sd,type);
  7434. return 0;
  7435. }
  7436. /*==========================================
  7437. * ステータスリセット
  7438. *------------------------------------------
  7439. */
  7440. int buildin_resetstatus(struct script_state *st)
  7441. {
  7442. struct map_session_data *sd;
  7443. sd=script_rid2sd(st);
  7444. pc_resetstate(sd);
  7445. return 0;
  7446. }
  7447. /*==========================================
  7448. * script command resetskill
  7449. *------------------------------------------
  7450. */
  7451. int buildin_resetskill(struct script_state *st)
  7452. {
  7453. struct map_session_data *sd;
  7454. sd=script_rid2sd(st);
  7455. pc_resetskill(sd,1);
  7456. return 0;
  7457. }
  7458. /*==========================================
  7459. * Counts total amount of skill points.
  7460. *------------------------------------------
  7461. */
  7462. int buildin_skillpointcount(struct script_state *st)
  7463. {
  7464. struct map_session_data *sd;
  7465. sd=script_rid2sd(st);
  7466. push_val(st->stack,C_INT,sd->status.skill_point + pc_resetskill(sd,2));
  7467. return 0;
  7468. }
  7469. /*==========================================
  7470. *
  7471. *------------------------------------------
  7472. */
  7473. int buildin_changebase(struct script_state *st)
  7474. {
  7475. struct map_session_data *sd=NULL;
  7476. int vclass;
  7477. if( st->end>st->start+3 )
  7478. sd=map_id2sd(conv_num(st,& (st->stack->stack_data[st->start+3])));
  7479. else
  7480. sd=script_rid2sd(st);
  7481. if(sd == NULL)
  7482. return 0;
  7483. vclass = conv_num(st,& (st->stack->stack_data[st->start+2]));
  7484. if(vclass == JOB_WEDDING)
  7485. {
  7486. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7487. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7488. )
  7489. return 0;
  7490. }
  7491. if(!sd->disguise && vclass != sd->vd.class_) {
  7492. status_set_viewdata(&sd->bl, vclass);
  7493. //Updated client view. Base, Weapon and Cloth Colors.
  7494. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7495. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7496. if (sd->vd.cloth_color)
  7497. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7498. }
  7499. return 0;
  7500. }
  7501. /*==========================================
  7502. * 性別変換
  7503. *------------------------------------------
  7504. */
  7505. int buildin_changesex(struct script_state *st) {
  7506. struct map_session_data *sd = NULL;
  7507. sd = script_rid2sd(st);
  7508. if (sd->status.sex == 0) {
  7509. sd->status.sex = 1;
  7510. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7511. sd->status.class_ -= 1;
  7512. } else if (sd->status.sex == 1) {
  7513. sd->status.sex = 0;
  7514. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7515. sd->status.class_ += 1;
  7516. }
  7517. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  7518. chrif_save(sd,0);
  7519. return 0;
  7520. }
  7521. /*==========================================
  7522. * npcチャット作成
  7523. *------------------------------------------
  7524. */
  7525. int buildin_waitingroom(struct script_state *st)
  7526. {
  7527. char *name,*ev="";
  7528. int limit, trigger = 0,pub=1;
  7529. name=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7530. limit= conv_num(st,& (st->stack->stack_data[st->start+3]));
  7531. if(limit==0)
  7532. pub=3;
  7533. if( (st->end > st->start+5) ){
  7534. struct script_data* data=&(st->stack->stack_data[st->start+5]);
  7535. get_val(st,data);
  7536. if(data->type==C_INT){
  7537. // 新Athena仕様(旧Athena仕様と互換性あり)
  7538. ev=conv_str(st,& (st->stack->stack_data[st->start+4]));
  7539. trigger=conv_num(st,& (st->stack->stack_data[st->start+5]));
  7540. }else{
  7541. // eathena仕様
  7542. trigger=conv_num(st,& (st->stack->stack_data[st->start+4]));
  7543. ev=conv_str(st,& (st->stack->stack_data[st->start+5]));
  7544. }
  7545. }else{
  7546. // 旧Athena仕様
  7547. if( st->end > st->start+4 )
  7548. ev=conv_str(st,& (st->stack->stack_data[st->start+4]));
  7549. }
  7550. chat_createnpcchat( (struct npc_data *)map_id2bl(st->oid),
  7551. limit,pub,trigger,name,(int)strlen(name)+1,ev);
  7552. return 0;
  7553. }
  7554. /*==========================================
  7555. * Works like 'announce' but outputs in the common chat window
  7556. *------------------------------------------
  7557. */
  7558. int buildin_globalmes(struct script_state *st)
  7559. {
  7560. struct block_list *bl = map_id2bl(st->oid);
  7561. struct npc_data *nd = (struct npc_data *)bl;
  7562. char *name=NULL,*mes;
  7563. mes=conv_str(st,& (st->stack->stack_data[st->start+2])); // メッセージの取得
  7564. if(mes==NULL) return 0;
  7565. if(st->end>st->start+3){ // NPC名の取得(123#456)
  7566. name=conv_str(st,& (st->stack->stack_data[st->start+3]));
  7567. } else {
  7568. name=nd->name;
  7569. }
  7570. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7571. return 0;
  7572. }
  7573. /*==========================================
  7574. * npcチャット削除
  7575. *------------------------------------------
  7576. */
  7577. int buildin_delwaitingroom(struct script_state *st)
  7578. {
  7579. struct npc_data *nd;
  7580. if( st->end > st->start+2 )
  7581. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+2])));
  7582. else
  7583. nd=(struct npc_data *)map_id2bl(st->oid);
  7584. chat_deletenpcchat(nd);
  7585. return 0;
  7586. }
  7587. /*==========================================
  7588. * npcチャット全員蹴り出す
  7589. *------------------------------------------
  7590. */
  7591. int buildin_waitingroomkickall(struct script_state *st)
  7592. {
  7593. struct npc_data *nd;
  7594. struct chat_data *cd;
  7595. if( st->end > st->start+2 )
  7596. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+2])));
  7597. else
  7598. nd=(struct npc_data *)map_id2bl(st->oid);
  7599. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL )
  7600. return 0;
  7601. chat_npckickall(cd);
  7602. return 0;
  7603. }
  7604. /*==========================================
  7605. * npcチャットイベント有効化
  7606. *------------------------------------------
  7607. */
  7608. int buildin_enablewaitingroomevent(struct script_state *st)
  7609. {
  7610. struct npc_data *nd;
  7611. struct chat_data *cd;
  7612. if( st->end > st->start+2 )
  7613. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+2])));
  7614. else
  7615. nd=(struct npc_data *)map_id2bl(st->oid);
  7616. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL )
  7617. return 0;
  7618. chat_enableevent(cd);
  7619. return 0;
  7620. }
  7621. /*==========================================
  7622. * npcチャットイベント無効化
  7623. *------------------------------------------
  7624. */
  7625. int buildin_disablewaitingroomevent(struct script_state *st)
  7626. {
  7627. struct npc_data *nd;
  7628. struct chat_data *cd;
  7629. if( st->end > st->start+2 )
  7630. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+2])));
  7631. else
  7632. nd=(struct npc_data *)map_id2bl(st->oid);
  7633. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL )
  7634. return 0;
  7635. chat_disableevent(cd);
  7636. return 0;
  7637. }
  7638. /*==========================================
  7639. * npcチャット状態所得
  7640. *------------------------------------------
  7641. */
  7642. int buildin_getwaitingroomstate(struct script_state *st)
  7643. {
  7644. struct npc_data *nd;
  7645. struct chat_data *cd;
  7646. int val=0,type;
  7647. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  7648. if( st->end > st->start+3 )
  7649. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+3])));
  7650. else
  7651. nd=(struct npc_data *)map_id2bl(st->oid);
  7652. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL ){
  7653. push_val(st->stack,C_INT,-1);
  7654. return 0;
  7655. }
  7656. switch(type){
  7657. case 0: val=cd->users; break;
  7658. case 1: val=cd->limit; break;
  7659. case 2: val=cd->trigger&0x7f; break;
  7660. case 3: val=((cd->trigger&0x80)>0); break;
  7661. case 32: val=(cd->users >= cd->limit); break;
  7662. case 33: val=(cd->users >= cd->trigger); break;
  7663. case 4:
  7664. push_str(st->stack,C_CONSTSTR,cd->title);
  7665. return 0;
  7666. case 5:
  7667. push_str(st->stack,C_CONSTSTR,cd->pass);
  7668. return 0;
  7669. case 16:
  7670. push_str(st->stack,C_CONSTSTR,cd->npc_event);
  7671. return 0;
  7672. }
  7673. push_val(st->stack,C_INT,val);
  7674. return 0;
  7675. }
  7676. /*==========================================
  7677. * チャットメンバー(規定人数)ワープ
  7678. *------------------------------------------
  7679. */
  7680. int buildin_warpwaitingpc(struct script_state *st)
  7681. {
  7682. int x,y,i,n;
  7683. char *str;
  7684. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  7685. struct chat_data *cd;
  7686. struct map_session_data *sd;
  7687. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL )
  7688. return 0;
  7689. n=cd->trigger&0x7f;
  7690. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7691. x=conv_num(st,& (st->stack->stack_data[st->start+3]));
  7692. y=conv_num(st,& (st->stack->stack_data[st->start+4]));
  7693. if( st->end > st->start+5 )
  7694. n=conv_num(st,& (st->stack->stack_data[st->start+5]));
  7695. for(i=0;i<n;i++){
  7696. sd=cd->usersd[0];
  7697. if (!sd) continue; //Broken npc chat room?
  7698. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24),sd->bl.id);
  7699. if(strcmp(str,"Random")==0)
  7700. pc_randomwarp(sd,3);
  7701. else if(strcmp(str,"SavePoint")==0){
  7702. if(map[sd->bl.m].flag.noteleport) // テレポ禁止
  7703. return 0;
  7704. pc_setpos(sd,sd->status.save_point.map,
  7705. sd->status.save_point.x,sd->status.save_point.y,3);
  7706. }else
  7707. pc_setpos(sd,mapindex_name2id(str),x,y,0);
  7708. }
  7709. mapreg_setreg(add_str("$@warpwaitingpcnum"),n);
  7710. return 0;
  7711. }
  7712. /*==========================================
  7713. * RIDのアタッチ
  7714. *------------------------------------------
  7715. */
  7716. int buildin_attachrid(struct script_state *st)
  7717. {
  7718. st->rid=conv_num(st,& (st->stack->stack_data[st->start+2]));
  7719. push_val(st->stack,C_INT, (map_id2sd(st->rid)!=NULL));
  7720. return 0;
  7721. }
  7722. /*==========================================
  7723. * RIDのデタッチ
  7724. *------------------------------------------
  7725. */
  7726. int buildin_detachrid(struct script_state *st)
  7727. {
  7728. st->rid=0;
  7729. return 0;
  7730. }
  7731. /*==========================================
  7732. * 存在チェック
  7733. *------------------------------------------
  7734. */
  7735. int buildin_isloggedin(struct script_state *st)
  7736. {
  7737. push_val(st->stack,C_INT, map_id2sd(
  7738. conv_num(st,& (st->stack->stack_data[st->start+2])) )!=NULL );
  7739. return 0;
  7740. }
  7741. /*==========================================
  7742. *
  7743. *------------------------------------------
  7744. */
  7745. int buildin_setmapflagnosave(struct script_state *st)
  7746. {
  7747. int m,x,y;
  7748. unsigned short mapindex;
  7749. char *str,*str2;
  7750. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7751. str2=conv_str(st,& (st->stack->stack_data[st->start+3]));
  7752. x=conv_num(st,& (st->stack->stack_data[st->start+4]));
  7753. y=conv_num(st,& (st->stack->stack_data[st->start+5]));
  7754. m = map_mapname2mapid(str);
  7755. mapindex = mapindex_name2id(str2);
  7756. if(m >= 0 && mapindex) {
  7757. map[m].flag.nosave=1;
  7758. map[m].save.map=mapindex;
  7759. map[m].save.x=x;
  7760. map[m].save.y=y;
  7761. }
  7762. return 0;
  7763. }
  7764. int buildin_setmapflag(struct script_state *st)
  7765. {
  7766. int m,i;
  7767. char *str;
  7768. char *val=NULL;
  7769. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7770. i=conv_num(st,& (st->stack->stack_data[st->start+3]));
  7771. if(st->end>st->start+4){
  7772. val=conv_str(st,& (st->stack->stack_data[st->start+4]));
  7773. }
  7774. m = map_mapname2mapid(str);
  7775. if(m >= 0) {
  7776. switch(i) {
  7777. case MF_NOMEMO:
  7778. map[m].flag.nomemo=1;
  7779. break;
  7780. case MF_NOTELEPORT:
  7781. map[m].flag.noteleport=1;
  7782. break;
  7783. case MF_NOBRANCH:
  7784. map[m].flag.nobranch=1;
  7785. break;
  7786. case MF_NOPENALTY:
  7787. map[m].flag.noexppenalty=1;
  7788. map[m].flag.nozenypenalty=1;
  7789. break;
  7790. case MF_NOZENYPENALTY:
  7791. map[m].flag.nozenypenalty=1;
  7792. break;
  7793. case MF_PVP:
  7794. map[m].flag.pvp=1;
  7795. break;
  7796. case MF_PVP_NOPARTY:
  7797. map[m].flag.pvp_noparty=1;
  7798. break;
  7799. case MF_PVP_NOGUILD:
  7800. map[m].flag.pvp_noguild=1;
  7801. break;
  7802. case MF_GVG:
  7803. map[m].flag.gvg=1;
  7804. break;
  7805. case MF_GVG_NOPARTY:
  7806. map[m].flag.gvg_noparty=1;
  7807. break;
  7808. case MF_GVG_DUNGEON:
  7809. map[m].flag.gvg_dungeon=1;
  7810. break;
  7811. case MF_GVG_CASTLE:
  7812. map[m].flag.gvg_castle=1;
  7813. break;
  7814. case MF_NOTRADE:
  7815. map[m].flag.notrade=1;
  7816. break;
  7817. case MF_NODROP:
  7818. map[m].flag.nodrop=1;
  7819. break;
  7820. case MF_NOSKILL:
  7821. map[m].flag.noskill=1;
  7822. break;
  7823. case MF_NOWARP:
  7824. map[m].flag.nowarp=1;
  7825. break;
  7826. case MF_NOICEWALL: // [Valaris]
  7827. map[m].flag.noicewall=1;
  7828. break;
  7829. case MF_SNOW: // [Valaris]
  7830. map[m].flag.snow=1;
  7831. break;
  7832. case MF_CLOUDS:
  7833. map[m].flag.clouds=1;
  7834. break;
  7835. case MF_CLOUDS2: // [Valaris]
  7836. map[m].flag.clouds2=1;
  7837. break;
  7838. case MF_FOG: // [Valaris]
  7839. map[m].flag.fog=1;
  7840. break;
  7841. case MF_FIREWORKS:
  7842. map[m].flag.fireworks=1;
  7843. break;
  7844. case MF_SAKURA: // [Valaris]
  7845. map[m].flag.sakura=1;
  7846. break;
  7847. case MF_LEAVES: // [Valaris]
  7848. map[m].flag.leaves=1;
  7849. break;
  7850. case MF_RAIN: // [Valaris]
  7851. map[m].flag.rain=1;
  7852. break;
  7853. case MF_INDOORS: // celest
  7854. map[m].flag.indoors=1;
  7855. break;
  7856. case MF_NIGHTENABLED:
  7857. map[m].flag.nightenabled=1;
  7858. break;
  7859. case MF_NOGO: // celest
  7860. map[m].flag.nogo=1;
  7861. break;
  7862. case MF_NOBASEEXP:
  7863. map[m].flag.nobaseexp=1;
  7864. break;
  7865. case MF_NOJOBEXP:
  7866. map[m].flag.nojobexp=1;
  7867. break;
  7868. case MF_NOMOBLOOT:
  7869. map[m].flag.nomobloot=1;
  7870. break;
  7871. case MF_NOMVPLOOT:
  7872. map[m].flag.nomvploot=1;
  7873. break;
  7874. case MF_NORETURN:
  7875. map[m].flag.noreturn=1;
  7876. break;
  7877. case MF_NOWARPTO:
  7878. map[m].flag.nowarpto=1;
  7879. break;
  7880. case MF_NIGHTMAREDROP:
  7881. map[m].flag.pvp_nightmaredrop=1;
  7882. break;
  7883. case MF_RESTRICTED:
  7884. map[m].flag.restricted=1;
  7885. break;
  7886. case MF_NOCOMMAND:
  7887. map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val);
  7888. break;
  7889. case MF_JEXP:
  7890. map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val);
  7891. break;
  7892. case MF_BEXP:
  7893. map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val);
  7894. break;
  7895. case MF_NOVENDING:
  7896. map[m].flag.novending=1;
  7897. break;
  7898. case MF_LOADEVENT:
  7899. map[m].flag.loadevent=1;
  7900. break;
  7901. case MF_NOCHAT:
  7902. map[m].flag.nochat=1;
  7903. break;
  7904. case MF_PARTYLOCK:
  7905. map[m].flag.partylock=1;
  7906. break;
  7907. case MF_GUILDLOCK:
  7908. map[m].flag.guildlock=1;
  7909. break;
  7910. }
  7911. }
  7912. return 0;
  7913. }
  7914. int buildin_removemapflag(struct script_state *st)
  7915. {
  7916. int m,i;
  7917. char *str;
  7918. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  7919. i=conv_num(st,& (st->stack->stack_data[st->start+3]));
  7920. m = map_mapname2mapid(str);
  7921. if(m >= 0) {
  7922. switch(i) {
  7923. case MF_NOMEMO:
  7924. map[m].flag.nomemo=0;
  7925. break;
  7926. case MF_NOTELEPORT:
  7927. map[m].flag.noteleport=0;
  7928. break;
  7929. case MF_NOSAVE:
  7930. map[m].flag.nosave=0;
  7931. break;
  7932. case MF_NOBRANCH:
  7933. map[m].flag.nobranch=0;
  7934. break;
  7935. case MF_NOPENALTY:
  7936. map[m].flag.noexppenalty=0;
  7937. map[m].flag.nozenypenalty=0;
  7938. break;
  7939. case MF_PVP:
  7940. map[m].flag.pvp=0;
  7941. break;
  7942. case MF_PVP_NOPARTY:
  7943. map[m].flag.pvp_noparty=0;
  7944. break;
  7945. case MF_PVP_NOGUILD:
  7946. map[m].flag.pvp_noguild=0;
  7947. break;
  7948. case MF_GVG:
  7949. map[m].flag.gvg=0;
  7950. break;
  7951. case MF_GVG_NOPARTY:
  7952. map[m].flag.gvg_noparty=0;
  7953. break;
  7954. case MF_GVG_DUNGEON:
  7955. map[m].flag.gvg_dungeon=0;
  7956. break;
  7957. case MF_GVG_CASTLE:
  7958. map[m].flag.gvg_castle=0;
  7959. break;
  7960. case MF_NOZENYPENALTY:
  7961. map[m].flag.nozenypenalty=0;
  7962. break;
  7963. case MF_NOTRADE:
  7964. map[m].flag.notrade=0;
  7965. break;
  7966. case MF_NODROP:
  7967. map[m].flag.nodrop=0;
  7968. break;
  7969. case MF_NOSKILL:
  7970. map[m].flag.noskill=0;
  7971. break;
  7972. case MF_NOWARP:
  7973. map[m].flag.nowarp=0;
  7974. break;
  7975. case MF_NOICEWALL: // [Valaris]
  7976. map[m].flag.noicewall=0;
  7977. break;
  7978. case MF_SNOW: // [Valaris]
  7979. map[m].flag.snow=0;
  7980. break;
  7981. case MF_CLOUDS:
  7982. map[m].flag.clouds=0;
  7983. break;
  7984. case MF_CLOUDS2: // [Valaris]
  7985. map[m].flag.clouds2=0;
  7986. break;
  7987. case MF_FOG: // [Valaris]
  7988. map[m].flag.fog=0;
  7989. break;
  7990. case MF_FIREWORKS:
  7991. map[m].flag.fireworks=0;
  7992. break;
  7993. case MF_SAKURA: // [Valaris]
  7994. map[m].flag.sakura=0;
  7995. break;
  7996. case MF_LEAVES: // [Valaris]
  7997. map[m].flag.leaves=0;
  7998. break;
  7999. case MF_RAIN: // [Valaris]
  8000. map[m].flag.rain=0;
  8001. break;
  8002. case MF_INDOORS: // celest
  8003. map[m].flag.indoors=0;
  8004. break;
  8005. case MF_NIGHTENABLED:
  8006. map[m].flag.nightenabled=0;
  8007. break;
  8008. case MF_NOGO: // celest
  8009. map[m].flag.nogo=0;
  8010. break;
  8011. case MF_NOBASEEXP:
  8012. map[m].flag.nobaseexp=0;
  8013. break;
  8014. case MF_NOJOBEXP:
  8015. map[m].flag.nojobexp=0;
  8016. break;
  8017. case MF_NOMOBLOOT:
  8018. map[m].flag.nomobloot=0;
  8019. break;
  8020. case MF_NOMVPLOOT:
  8021. map[m].flag.nomvploot=0;
  8022. break;
  8023. case MF_NORETURN:
  8024. map[m].flag.noreturn=0;
  8025. break;
  8026. case MF_NOWARPTO:
  8027. map[m].flag.nowarpto=0;
  8028. break;
  8029. case MF_NIGHTMAREDROP:
  8030. map[m].flag.pvp_nightmaredrop=0;
  8031. break;
  8032. case MF_RESTRICTED:
  8033. map[m].flag.restricted=0;
  8034. break;
  8035. case MF_NOCOMMAND:
  8036. map[m].nocommand=0;
  8037. break;
  8038. case MF_JEXP:
  8039. map[m].jexp=100;
  8040. break;
  8041. case MF_BEXP:
  8042. map[m].bexp=100;
  8043. break;
  8044. case MF_NOVENDING:
  8045. map[m].flag.novending=0;
  8046. break;
  8047. case MF_LOADEVENT:
  8048. map[m].flag.loadevent=0;
  8049. break;
  8050. case MF_NOCHAT:
  8051. map[m].flag.nochat=0;
  8052. break;
  8053. case MF_PARTYLOCK:
  8054. map[m].flag.partylock=0;
  8055. break;
  8056. case MF_GUILDLOCK:
  8057. map[m].flag.guildlock=0;
  8058. break;
  8059. }
  8060. }
  8061. return 0;
  8062. }
  8063. int buildin_pvpon(struct script_state *st)
  8064. {
  8065. int m,i,users;
  8066. char *str;
  8067. struct map_session_data *pl_sd=NULL, **pl_allsd;
  8068. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8069. m = map_mapname2mapid(str);
  8070. if(m >= 0 && !map[m].flag.pvp) {
  8071. map[m].flag.pvp = 1;
  8072. clif_send0199(m,1);
  8073. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8074. return 0;
  8075. pl_allsd = map_getallusers(&users);
  8076. for(i=0;i<users;i++)
  8077. {
  8078. if ((pl_sd = pl_allsd[i]) && m == pl_sd->bl.m && pl_sd->pvp_timer == -1)
  8079. {
  8080. pl_sd->pvp_timer=add_timer(gettick()+200,pc_calc_pvprank_timer,pl_sd->bl.id,0);
  8081. pl_sd->pvp_rank=0;
  8082. pl_sd->pvp_lastusers=0;
  8083. pl_sd->pvp_point=5;
  8084. pl_sd->pvp_won = 0;
  8085. pl_sd->pvp_lost = 0;
  8086. }
  8087. }
  8088. }
  8089. return 0;
  8090. }
  8091. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap) {
  8092. TBL_PC* sd = (TBL_PC*)bl;
  8093. clif_pvpset(sd, 0, 0, 2);
  8094. if (sd->pvp_timer != UINT_MAX) {
  8095. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8096. sd->pvp_timer = UINT_MAX;
  8097. }
  8098. return 0;
  8099. }
  8100. int buildin_pvpoff(struct script_state *st)
  8101. {
  8102. int m;
  8103. char *str;
  8104. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8105. m = map_mapname2mapid(str);
  8106. if(m < 0 || !map[m].flag.pvp)
  8107. return 0; //fixed Lupus
  8108. map[m].flag.pvp = 0;
  8109. clif_send0199(m,0);
  8110. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8111. return 0;
  8112. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8113. return 0;
  8114. }
  8115. int buildin_gvgon(struct script_state *st)
  8116. {
  8117. int m;
  8118. char *str;
  8119. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8120. m = map_mapname2mapid(str);
  8121. if(m >= 0 && !map[m].flag.gvg) {
  8122. map[m].flag.gvg = 1;
  8123. clif_send0199(m,3);
  8124. }
  8125. return 0;
  8126. }
  8127. int buildin_gvgoff(struct script_state *st)
  8128. {
  8129. int m;
  8130. char *str;
  8131. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8132. m = map_mapname2mapid(str);
  8133. if(m >= 0 && map[m].flag.gvg) {
  8134. map[m].flag.gvg = 0;
  8135. clif_send0199(m,0);
  8136. }
  8137. return 0;
  8138. }
  8139. /*==========================================
  8140. * Shows an emoticon on top of the player/npc
  8141. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8142. *------------------------------------------
  8143. */
  8144. //Optional second parameter added by [Skotlex]
  8145. int buildin_emotion(struct script_state *st)
  8146. {
  8147. int type;
  8148. int player=0;
  8149. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8150. if(type < 0 || type > 100)
  8151. return 0;
  8152. if( st->end>st->start+3 )
  8153. player=conv_num(st,& (st->stack->stack_data[st->start+3]));
  8154. if (player) {
  8155. struct map_session_data *sd = script_rid2sd(st);
  8156. if (sd)
  8157. clif_emotion(&sd->bl,type);
  8158. } else
  8159. clif_emotion(map_id2bl(st->oid),type);
  8160. return 0;
  8161. }
  8162. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  8163. {
  8164. int m=va_arg(ap,int);
  8165. int g_id=va_arg(ap,int);
  8166. int flag=va_arg(ap,int);
  8167. struct map_session_data *sd = (TBL_PC*)data;
  8168. if(!sd || sd->bl.m != m)
  8169. return 0;
  8170. if(
  8171. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8172. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8173. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8174. )
  8175. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8176. return 1;
  8177. }
  8178. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8179. {
  8180. struct mob_data *md=(struct mob_data *)bl;
  8181. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8182. status_kill(bl);
  8183. return 0;
  8184. }
  8185. int buildin_maprespawnguildid(struct script_state *st)
  8186. {
  8187. char *mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8188. int g_id=conv_num(st,& (st->stack->stack_data[st->start+3]));
  8189. int flag=conv_num(st,& (st->stack->stack_data[st->start+4]));
  8190. int m=map_mapname2mapid(mapname);
  8191. if(m == -1)
  8192. return 0;
  8193. //Catch ALL players (in case some are 'between maps' on execution time)
  8194. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8195. if (flag&4) //Remove script mobs.
  8196. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8197. return 0;
  8198. }
  8199. int buildin_agitstart(struct script_state *st)
  8200. {
  8201. if(agit_flag==1) return 0; // Agit already Start.
  8202. agit_flag=1;
  8203. guild_agit_start();
  8204. return 0;
  8205. }
  8206. int buildin_agitend(struct script_state *st)
  8207. {
  8208. if(agit_flag==0) return 0; // Agit already End.
  8209. agit_flag=0;
  8210. guild_agit_end();
  8211. return 0;
  8212. }
  8213. /*==========================================
  8214. * Returns whether woe is on or off. // choice script
  8215. *------------------------------------------
  8216. */
  8217. int buildin_agitcheck(struct script_state *st)
  8218. {
  8219. push_val(st->stack,C_INT,agit_flag);
  8220. return 0;
  8221. }
  8222. int buildin_flagemblem(struct script_state *st)
  8223. {
  8224. int g_id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8225. if(g_id < 0) return 0;
  8226. // printf("Script.c: [FlagEmblem] GuildID=%d, Emblem=%d.\n", g->guild_id, g->emblem_id);
  8227. ((struct npc_data *)map_id2bl(st->oid))->u.scr.guild_id = g_id;
  8228. return 0;
  8229. }
  8230. int buildin_getcastlename(struct script_state *st)
  8231. {
  8232. char *mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8233. struct guild_castle *gc=NULL;
  8234. int i;
  8235. for(i=0;i<MAX_GUILDCASTLE;i++){
  8236. if( (gc=guild_castle_search(i)) != NULL ){
  8237. if(strcmp(mapname,gc->map_name)==0){
  8238. break;
  8239. }
  8240. }
  8241. }
  8242. if(gc)
  8243. push_str(st->stack,C_CONSTSTR,gc->castle_name);
  8244. else
  8245. push_str(st->stack,C_CONSTSTR,"");
  8246. return 0;
  8247. }
  8248. int buildin_getcastledata(struct script_state *st)
  8249. {
  8250. char *mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8251. int index=conv_num(st,& (st->stack->stack_data[st->start+3]));
  8252. char *event=NULL;
  8253. struct guild_castle *gc;
  8254. int i,j;
  8255. if( st->end>st->start+4 && index==0){
  8256. for(i=0,j=-1;i<MAX_GUILDCASTLE;i++)
  8257. if( (gc=guild_castle_search(i)) != NULL &&
  8258. strcmp(mapname,gc->map_name)==0 )
  8259. j=i;
  8260. if(j>=0){
  8261. event=conv_str(st,& (st->stack->stack_data[st->start+4]));
  8262. check_event(st, event);
  8263. guild_addcastleinfoevent(j,17,event);
  8264. }
  8265. }
  8266. for(i=0;i<MAX_GUILDCASTLE;i++){
  8267. if( (gc=guild_castle_search(i)) != NULL ){
  8268. if(strcmp(mapname,gc->map_name)==0){
  8269. switch(index){
  8270. case 0: for(j=1;j<26;j++) guild_castledataload(gc->castle_id,j); break; // Initialize[AgitInit]
  8271. case 1: push_val(st->stack,C_INT,gc->guild_id); break;
  8272. case 2: push_val(st->stack,C_INT,gc->economy); break;
  8273. case 3: push_val(st->stack,C_INT,gc->defense); break;
  8274. case 4: push_val(st->stack,C_INT,gc->triggerE); break;
  8275. case 5: push_val(st->stack,C_INT,gc->triggerD); break;
  8276. case 6: push_val(st->stack,C_INT,gc->nextTime); break;
  8277. case 7: push_val(st->stack,C_INT,gc->payTime); break;
  8278. case 8: push_val(st->stack,C_INT,gc->createTime); break;
  8279. case 9: push_val(st->stack,C_INT,gc->visibleC); break;
  8280. case 10:
  8281. case 11:
  8282. case 12:
  8283. case 13:
  8284. case 14:
  8285. case 15:
  8286. case 16:
  8287. case 17:
  8288. push_val(st->stack,C_INT,gc->guardian[index-10].visible); break;
  8289. case 18:
  8290. case 19:
  8291. case 20:
  8292. case 21:
  8293. case 22:
  8294. case 23:
  8295. case 24:
  8296. case 25:
  8297. push_val(st->stack,C_INT,gc->guardian[index-18].hp); break;
  8298. default:
  8299. push_val(st->stack,C_INT,0); break;
  8300. }
  8301. return 0;
  8302. }
  8303. }
  8304. }
  8305. push_val(st->stack,C_INT,0);
  8306. return 0;
  8307. }
  8308. int buildin_setcastledata(struct script_state *st)
  8309. {
  8310. char *mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8311. int index=conv_num(st,& (st->stack->stack_data[st->start+3]));
  8312. int value=conv_num(st,& (st->stack->stack_data[st->start+4]));
  8313. struct guild_castle *gc;
  8314. int i;
  8315. for(i=0;i<MAX_GUILDCASTLE;i++){
  8316. if( (gc=guild_castle_search(i)) != NULL ){
  8317. if(strcmp(mapname,gc->map_name)==0){
  8318. // Save Data byself First
  8319. switch(index){
  8320. case 1: gc->guild_id = value; break;
  8321. case 2: gc->economy = value; break;
  8322. case 3: gc->defense = value; break;
  8323. case 4: gc->triggerE = value; break;
  8324. case 5: gc->triggerD = value; break;
  8325. case 6: gc->nextTime = value; break;
  8326. case 7: gc->payTime = value; break;
  8327. case 8: gc->createTime = value; break;
  8328. case 9: gc->visibleC = value; break;
  8329. case 10:
  8330. case 11:
  8331. case 12:
  8332. case 13:
  8333. case 14:
  8334. case 15:
  8335. case 16:
  8336. case 17:
  8337. gc->guardian[index-10].visible = value; break;
  8338. case 18:
  8339. case 19:
  8340. case 20:
  8341. case 21:
  8342. case 22:
  8343. case 23:
  8344. case 24:
  8345. case 25:
  8346. gc->guardian[index-18].hp = value;
  8347. if (gc->guardian[index-18].id)
  8348. { //Update this mob's HP.
  8349. struct block_list *bl = map_id2bl(gc->guardian[index-18].id);
  8350. if (!bl)
  8351. { //Wrong target?
  8352. gc->guardian[index-18].id = 0;
  8353. break;
  8354. }
  8355. if (value < 1) {
  8356. status_kill(bl);
  8357. break;
  8358. }
  8359. status_set_hp(bl, value, 0);
  8360. }
  8361. break;
  8362. default: return 0;
  8363. }
  8364. guild_castledatasave(gc->castle_id,index,value);
  8365. return 0;
  8366. }
  8367. }
  8368. }
  8369. return 0;
  8370. }
  8371. /* =====================================================================
  8372. * ギルド情報を要求する
  8373. * ---------------------------------------------------------------------
  8374. */
  8375. int buildin_requestguildinfo(struct script_state *st)
  8376. {
  8377. int guild_id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8378. char *event=NULL;
  8379. if( st->end>st->start+3 ){
  8380. event=conv_str(st,& (st->stack->stack_data[st->start+3]));
  8381. check_event(st, event);
  8382. }
  8383. if(guild_id>0)
  8384. guild_npc_request_info(guild_id,event);
  8385. return 0;
  8386. }
  8387. /* =====================================================================
  8388. * カードの数を得る
  8389. * ---------------------------------------------------------------------
  8390. */
  8391. int buildin_getequipcardcnt(struct script_state *st)
  8392. {
  8393. int i,num;
  8394. struct map_session_data *sd;
  8395. int c=MAX_SLOTS;
  8396. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8397. sd=script_rid2sd(st);
  8398. i=pc_checkequip(sd,equip[num-1]);
  8399. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8400. {
  8401. push_val(st->stack,C_INT,0);
  8402. return 0;
  8403. }
  8404. do{
  8405. if(sd->status.inventory[i].card[c-1] &&
  8406. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8407. push_val(st->stack,C_INT,(c));
  8408. return 0;
  8409. }
  8410. }while(c--);
  8411. push_val(st->stack,C_INT,0);
  8412. return 0;
  8413. }
  8414. /* ================================================================
  8415. * カード取り外し成功
  8416. * ----------------------------------------------------------------
  8417. */
  8418. int buildin_successremovecards(struct script_state *st)
  8419. {
  8420. int i,j,num,cardflag=0,flag;
  8421. struct map_session_data *sd;
  8422. struct item item_tmp;
  8423. int c=MAX_SLOTS;
  8424. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8425. sd=script_rid2sd(st);
  8426. i=pc_checkequip(sd,equip[num-1]);
  8427. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8428. return 0;
  8429. do{
  8430. if(sd->status.inventory[i].card[c-1] &&
  8431. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8432. cardflag = 1;
  8433. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8434. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8435. item_tmp.attribute=0;
  8436. for (j = 0; j < MAX_SLOTS; j++)
  8437. item_tmp.card[j]=0;
  8438. //Logs items, got from (N)PC scripts [Lupus]
  8439. if(log_config.enable_logs&0x40)
  8440. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8441. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8442. clif_additem(sd,0,0,flag);
  8443. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8444. }
  8445. }
  8446. }while(c--);
  8447. if(cardflag == 1){ // カードを取り除いたアイテム所得
  8448. flag=0;
  8449. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8450. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8451. item_tmp.attribute=sd->status.inventory[i].attribute;
  8452. //Logs items, got from (N)PC scripts [Lupus]
  8453. if(log_config.enable_logs&0x40)
  8454. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8455. for (j = 0; j < MAX_SLOTS; j++)
  8456. item_tmp.card[j]=0;
  8457. pc_delitem(sd,i,1,0);
  8458. //Logs items, got from (N)PC scripts [Lupus]
  8459. if(log_config.enable_logs&0x40)
  8460. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8461. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8462. clif_additem(sd,0,0,flag);
  8463. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8464. }
  8465. clif_misceffect(&sd->bl,3);
  8466. return 0;
  8467. }
  8468. return 0;
  8469. }
  8470. /* ================================================================
  8471. * カード取り外し失敗 slot,type
  8472. * type=0: 両方損失、1:カード損失、2:武具損失、3:損失無し
  8473. * ----------------------------------------------------------------
  8474. */
  8475. int buildin_failedremovecards(struct script_state *st)
  8476. {
  8477. int i,j,num,cardflag=0,flag,typefail;
  8478. struct map_session_data *sd;
  8479. struct item item_tmp;
  8480. int c=MAX_SLOTS;
  8481. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8482. typefail=conv_num(st,& (st->stack->stack_data[st->start+3]));
  8483. sd=script_rid2sd(st);
  8484. i=pc_checkequip(sd,equip[num-1]);
  8485. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8486. return 0;
  8487. do{
  8488. if(sd->status.inventory[i].card[c-1] &&
  8489. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8490. cardflag = 1;
  8491. if(typefail == 2){ // 武具のみ損失なら、カードは受け取らせる
  8492. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8493. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8494. item_tmp.attribute=0;
  8495. for (j = 0; j < MAX_SLOTS; j++)
  8496. item_tmp.card[j]=0;
  8497. //Logs items, got from (N)PC scripts [Lupus]
  8498. if(log_config.enable_logs&0x40)
  8499. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8500. if((flag=pc_additem(sd,&item_tmp,1))){
  8501. clif_additem(sd,0,0,flag);
  8502. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8503. }
  8504. }
  8505. }
  8506. }while(c--);
  8507. if(cardflag == 1){
  8508. if(typefail == 0 || typefail == 2){ // 武具損失
  8509. //Logs items, got from (N)PC scripts [Lupus]
  8510. if(log_config.enable_logs&0x40)
  8511. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8512. pc_delitem(sd,i,1,0);
  8513. clif_misceffect(&sd->bl,2);
  8514. return 0;
  8515. }
  8516. if(typefail == 1){ // カードのみ損失(武具を返す)
  8517. flag=0;
  8518. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8519. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8520. item_tmp.attribute=sd->status.inventory[i].attribute;
  8521. //Logs items, got from (N)PC scripts [Lupus]
  8522. if(log_config.enable_logs&0x40)
  8523. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8524. for (j = 0; j < MAX_SLOTS; j++)
  8525. item_tmp.card[j]=0;
  8526. pc_delitem(sd,i,1,0);
  8527. //Logs items, got from (N)PC scripts [Lupus]
  8528. if(log_config.enable_logs&0x40)
  8529. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8530. if((flag=pc_additem(sd,&item_tmp,1))){
  8531. clif_additem(sd,0,0,flag);
  8532. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8533. }
  8534. }
  8535. clif_misceffect(&sd->bl,2);
  8536. return 0;
  8537. }
  8538. return 0;
  8539. }
  8540. /* ================================================================
  8541. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8542. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8543. * improved by [Lance]
  8544. * ================================================================
  8545. */
  8546. int buildin_mapwarp(struct script_state *st) // Added by RoVeRT
  8547. {
  8548. int x,y,m,check_val=0,check_ID=0,i=0;
  8549. struct guild *g = NULL;
  8550. struct party_data *p = NULL;
  8551. char *str;
  8552. char *mapname;
  8553. unsigned int index;
  8554. mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8555. str=conv_str(st,& (st->stack->stack_data[st->start+3]));
  8556. x=conv_num(st,& (st->stack->stack_data[st->start+4]));
  8557. y=conv_num(st,& (st->stack->stack_data[st->start+5]));
  8558. if(st->end > st->start+7){
  8559. check_val=conv_num(st,& (st->stack->stack_data[st->start+6]));
  8560. check_ID=conv_num(st,& (st->stack->stack_data[st->start+7]));
  8561. }
  8562. if( (m=map_mapname2mapid(mapname))< 0)
  8563. return 0;
  8564. if(!(index=mapindex_name2id(str)))
  8565. return 0;
  8566. switch(check_val){
  8567. case 1:
  8568. g = guild_search(check_ID);
  8569. if (g){
  8570. for( i=0; i < g->max_member; i++)
  8571. {
  8572. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8573. pc_setpos(g->member[i].sd,index,x,y,3);
  8574. }
  8575. }
  8576. }
  8577. break;
  8578. case 2:
  8579. p = party_search(check_ID);
  8580. if(p){
  8581. for(i=0;i<MAX_PARTY; i++){
  8582. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8583. pc_setpos(p->data[i].sd,index,x,y,3);
  8584. }
  8585. }
  8586. }
  8587. break;
  8588. default:
  8589. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8590. break;
  8591. }
  8592. return 0;
  8593. }
  8594. int buildin_cmdothernpc(struct script_state *st) // Added by RoVeRT
  8595. {
  8596. char *npc,*command;
  8597. npc=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8598. command=conv_str(st,& (st->stack->stack_data[st->start+3]));
  8599. npc_command(map_id2sd(st->rid),npc,command);
  8600. return 0;
  8601. }
  8602. int buildin_inittimer(struct script_state *st) // Added by RoVeRT
  8603. {
  8604. // struct npc_data *nd=(struct npc_data*)map_id2bl(st->oid);
  8605. // nd->lastaction=nd->timer=gettick();
  8606. npc_do_ontimer(st->oid, 1);
  8607. return 0;
  8608. }
  8609. int buildin_stoptimer(struct script_state *st) // Added by RoVeRT
  8610. {
  8611. // struct npc_data *nd=(struct npc_data*)map_id2bl(st->oid);
  8612. // nd->lastaction=nd->timer=-1;
  8613. npc_do_ontimer(st->oid, 0);
  8614. return 0;
  8615. }
  8616. int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8617. {
  8618. char *event=va_arg(ap,char *);
  8619. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8620. return 1;
  8621. return 0;
  8622. }
  8623. int buildin_mobcount(struct script_state *st) // Added by RoVeRT
  8624. {
  8625. char *mapname,*event;
  8626. int m;
  8627. mapname=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8628. event=conv_str(st,& (st->stack->stack_data[st->start+3]));
  8629. check_event(st, event);
  8630. if( (m=map_mapname2mapid(mapname))<0 ) {
  8631. push_val(st->stack,C_INT,-1);
  8632. return 0;
  8633. }
  8634. push_val(st->stack,C_INT,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8635. return 0;
  8636. }
  8637. int buildin_marriage(struct script_state *st)
  8638. {
  8639. char *partner=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8640. struct map_session_data *sd=script_rid2sd(st);
  8641. struct map_session_data *p_sd=map_nick2sd(partner);
  8642. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8643. push_val(st->stack,C_INT,0);
  8644. return 0;
  8645. }
  8646. push_val(st->stack,C_INT,1);
  8647. return 0;
  8648. }
  8649. int buildin_wedding_effect(struct script_state *st)
  8650. {
  8651. struct map_session_data *sd=script_rid2sd(st);
  8652. struct block_list *bl;
  8653. if(sd==NULL) {
  8654. bl=map_id2bl(st->oid);
  8655. } else
  8656. bl=&sd->bl;
  8657. clif_wedding_effect(bl);
  8658. return 0;
  8659. }
  8660. int buildin_divorce(struct script_state *st)
  8661. {
  8662. struct map_session_data *sd=script_rid2sd(st);
  8663. if(sd==NULL || pc_divorce(sd) < 0){
  8664. push_val(st->stack,C_INT,0);
  8665. return 0;
  8666. }
  8667. push_val(st->stack,C_INT,1);
  8668. return 0;
  8669. }
  8670. int buildin_ispartneron(struct script_state *st)
  8671. {
  8672. struct map_session_data *sd=script_rid2sd(st);
  8673. struct map_session_data *p_sd=NULL;
  8674. if(sd==NULL || !pc_ismarried(sd) ||
  8675. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8676. push_val(st->stack,C_INT,0);
  8677. return 0;
  8678. }
  8679. push_val(st->stack,C_INT,1);
  8680. return 0;
  8681. }
  8682. int buildin_getpartnerid(struct script_state *st)
  8683. {
  8684. struct map_session_data *sd=script_rid2sd(st);
  8685. if (sd == NULL) {
  8686. push_val(st->stack,C_INT,0);
  8687. return 0;
  8688. }
  8689. push_val(st->stack,C_INT,sd->status.partner_id);
  8690. return 0;
  8691. }
  8692. int buildin_getchildid(struct script_state *st)
  8693. {
  8694. struct map_session_data *sd=script_rid2sd(st);
  8695. if (sd == NULL) {
  8696. push_val(st->stack,C_INT,0);
  8697. return 0;
  8698. }
  8699. push_val(st->stack,C_INT,sd->status.child);
  8700. return 0;
  8701. }
  8702. int buildin_getmotherid(struct script_state *st)
  8703. {
  8704. struct map_session_data *sd=script_rid2sd(st);
  8705. if (sd == NULL) {
  8706. push_val(st->stack,C_INT,0);
  8707. return 0;
  8708. }
  8709. push_val(st->stack,C_INT,sd->status.mother);
  8710. return 0;
  8711. }
  8712. int buildin_getfatherid(struct script_state *st)
  8713. {
  8714. struct map_session_data *sd=script_rid2sd(st);
  8715. if (sd == NULL) {
  8716. push_val(st->stack,C_INT,0);
  8717. return 0;
  8718. }
  8719. push_val(st->stack,C_INT,sd->status.father);
  8720. return 0;
  8721. }
  8722. int buildin_warppartner(struct script_state *st)
  8723. {
  8724. int x,y;
  8725. unsigned short mapindex;
  8726. char *str;
  8727. struct map_session_data *sd=script_rid2sd(st);
  8728. struct map_session_data *p_sd=NULL;
  8729. if(sd==NULL || !pc_ismarried(sd) ||
  8730. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8731. push_val(st->stack,C_INT,0);
  8732. return 0;
  8733. }
  8734. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  8735. x=conv_num(st,& (st->stack->stack_data[st->start+3]));
  8736. y=conv_num(st,& (st->stack->stack_data[st->start+4]));
  8737. mapindex = mapindex_name2id(str);
  8738. if (mapindex) {
  8739. pc_setpos(p_sd,mapindex,x,y,0);
  8740. push_val(st->stack,C_INT,1);
  8741. } else
  8742. push_val(st->stack,C_INT,0);
  8743. return 0;
  8744. }
  8745. /*================================================
  8746. * Script for Displaying MOB Information [Valaris]
  8747. *------------------------------------------------
  8748. */
  8749. int buildin_strmobinfo(struct script_state *st)
  8750. {
  8751. int num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8752. int class_=conv_num(st,& (st->stack->stack_data[st->start+3]));
  8753. if((class_>=0 && class_<=1000) || class_ >2000)
  8754. return 0;
  8755. switch (num) {
  8756. case 1:
  8757. push_str(st->stack,C_CONSTSTR,mob_db(class_)->name);
  8758. break;
  8759. case 2:
  8760. push_str(st->stack,C_CONSTSTR,mob_db(class_)->jname);
  8761. break;
  8762. case 3:
  8763. push_val(st->stack,C_INT,mob_db(class_)->lv);
  8764. break;
  8765. case 4:
  8766. push_val(st->stack,C_INT,mob_db(class_)->status.max_hp);
  8767. break;
  8768. case 5:
  8769. push_val(st->stack,C_INT,mob_db(class_)->status.max_sp);
  8770. break;
  8771. case 6:
  8772. push_val(st->stack,C_INT,mob_db(class_)->base_exp);
  8773. break;
  8774. case 7:
  8775. push_val(st->stack,C_INT,mob_db(class_)->job_exp);
  8776. break;
  8777. default:
  8778. push_val(st->stack,C_INT,0);
  8779. break;
  8780. }
  8781. return 0;
  8782. }
  8783. /*==========================================
  8784. * Summon guardians [Valaris]
  8785. * guardian "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>"}{,<guardian index>};
  8786. *------------------------------------------
  8787. */
  8788. int buildin_guardian(struct script_state *st)
  8789. {
  8790. int class_=0,amount=1,x=0,y=0,guardian=0;
  8791. char *str,*map,*evt="";
  8792. struct script_data *data;
  8793. map =conv_str(st,script_getdata(st,2));
  8794. x =conv_num(st,script_getdata(st,3));
  8795. y =conv_num(st,script_getdata(st,4));
  8796. str =conv_str(st,script_getdata(st,5));
  8797. class_=conv_num(st,script_getdata(st,6));
  8798. amount=conv_num(st,script_getdata(st,7));
  8799. if( script_hasdata(st,9) )
  8800. {// "<event label>",<guardian index>
  8801. evt=conv_str(st,script_getdata(st,8));
  8802. guardian=conv_num(st,script_getdata(st,9));
  8803. } else if( script_hasdata(st,8) ){
  8804. data=script_getdata(st,8);
  8805. get_val(st,data);
  8806. if( script_isstring(data) )
  8807. {// "<event label>"
  8808. evt=conv_str(st,script_getdata(st,8));
  8809. } else if( script_isint(data) )
  8810. {// <guardian index>
  8811. guardian=conv_num(st,script_getdata(st,8));
  8812. } else {
  8813. ShowError("buildin_guardian: invalid data type for argument #8 (%d).", data->type);
  8814. report_src(st);
  8815. return 1;
  8816. }
  8817. }
  8818. check_event(st, evt);
  8819. mob_spawn_guardian(map_id2sd(st->rid),map,x,y,str,class_,amount,evt,guardian);
  8820. return 0;
  8821. }
  8822. /*================================================
  8823. * Script for Displaying Guardian Info [Valaris]
  8824. *------------------------------------------------
  8825. */
  8826. int buildin_guardianinfo(struct script_state *st)
  8827. {
  8828. int guardian=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8829. struct map_session_data *sd=script_rid2sd(st);
  8830. struct guild_castle *gc=guild_mapname2gc(map[sd->bl.m].name);
  8831. if (guardian < 0 || guardian >= MAX_GUARDIANS || gc==NULL)
  8832. {
  8833. push_val(st->stack,C_INT,-1);
  8834. return 0;
  8835. }
  8836. if(gc->guardian[guardian].visible)
  8837. push_val(st->stack,C_INT,gc->guardian[guardian].hp);
  8838. else push_val(st->stack,C_INT,-1);
  8839. return 0;
  8840. }
  8841. /*==========================================
  8842. * IDからItem名
  8843. *------------------------------------------
  8844. */
  8845. int buildin_getitemname(struct script_state *st)
  8846. {
  8847. int item_id=0;
  8848. struct item_data *i_data;
  8849. char *item_name;
  8850. struct script_data *data;
  8851. data=&(st->stack->stack_data[st->start+2]);
  8852. get_val(st,data);
  8853. if( data->type==C_STR || data->type==C_CONSTSTR ){
  8854. const char *name=conv_str(st,data);
  8855. struct item_data *item_data = itemdb_searchname(name);
  8856. if( item_data )
  8857. item_id=item_data->nameid;
  8858. }else
  8859. item_id=conv_num(st,data);
  8860. i_data = itemdb_exists(item_id);
  8861. if (i_data == NULL)
  8862. {
  8863. push_str(st->stack,C_CONSTSTR,"null");
  8864. return 0;
  8865. }
  8866. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8867. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8868. push_str(st->stack,C_STR,item_name);
  8869. return 0;
  8870. }
  8871. /*==========================================
  8872. * Returns number of slots an item has. [Skotlex]
  8873. *------------------------------------------
  8874. */
  8875. int buildin_getitemslots(struct script_state *st)
  8876. {
  8877. int item_id;
  8878. struct item_data *i_data;
  8879. item_id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8880. i_data = itemdb_exists(item_id);
  8881. if (i_data)
  8882. push_val(st->stack,C_INT,i_data->slot);
  8883. else
  8884. push_val(st->stack,C_INT,-1);
  8885. return 0;
  8886. }
  8887. /*==========================================
  8888. * Returns some values of an item [Lupus]
  8889. * Price, Weight, etc...
  8890. getiteminfo(itemID,n), where n
  8891. 0 value_buy;
  8892. 1 value_sell;
  8893. 2 type;
  8894. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8895. if = 0, then monsters don't drop it at all (rare or a quest item)
  8896. if = 10000, then this item is sold in NPC shops only
  8897. 4 sex;
  8898. 5 equip;
  8899. 6 weight;
  8900. 7 atk;
  8901. 8 def;
  8902. 9 range;
  8903. 10 slot;
  8904. 11 look;
  8905. 12 elv;
  8906. 13 wlv;
  8907. *------------------------------------------
  8908. */
  8909. int buildin_getiteminfo(struct script_state *st)
  8910. {
  8911. int item_id,n;
  8912. int *item_arr;
  8913. struct item_data *i_data;
  8914. item_id = conv_num(st,& (st->stack->stack_data[st->start+2]));
  8915. n = conv_num(st,& (st->stack->stack_data[st->start+3]));
  8916. i_data = itemdb_exists(item_id);
  8917. if (i_data && n>=0 && n<14) {
  8918. item_arr = (int*)&i_data->value_buy;
  8919. push_val(st->stack,C_INT,item_arr[n]);
  8920. } else
  8921. push_val(st->stack,C_INT,-1);
  8922. return 0;
  8923. }
  8924. /*==========================================
  8925. * Returns value from equipped item slot n [Lupus]
  8926. getequipcardid(num,slot)
  8927. where
  8928. num = eqip position slot
  8929. slot = 0,1,2,3 (Card Slot N)
  8930. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  8931. it's useful when you want to check item cards or if it's signed
  8932. Useful for such quests as "Sign this refined item with players name" etc
  8933. Hat[0] +4 -> Player's Hat[0] +4
  8934. *------------------------------------------
  8935. */
  8936. int buildin_getequipcardid(struct script_state *st)
  8937. {
  8938. int i,num,slot;
  8939. struct map_session_data *sd;
  8940. num=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8941. slot=conv_num(st,& (st->stack->stack_data[st->start+3]));
  8942. sd=script_rid2sd(st);
  8943. i=pc_checkequip(sd,equip[num-1]);
  8944. if(i >= 0 && slot>=0 && slot<4)
  8945. push_val(st->stack,C_INT,sd->status.inventory[i].card[slot]);
  8946. else
  8947. push_val(st->stack,C_INT,0);
  8948. return 0;
  8949. }
  8950. /*==========================================
  8951. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  8952. *------------------------------------------
  8953. */
  8954. int buildin_petskillbonus(struct script_state *st)
  8955. {
  8956. struct pet_data *pd;
  8957. struct map_session_data *sd=script_rid2sd(st);
  8958. if(sd==NULL || sd->pd==NULL)
  8959. return 0;
  8960. pd=sd->pd;
  8961. if (pd->bonus)
  8962. { //Clear previous bonus
  8963. if (pd->bonus->timer != -1)
  8964. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  8965. } else //init
  8966. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  8967. pd->bonus->type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8968. pd->bonus->val=conv_num(st,& (st->stack->stack_data[st->start+3]));
  8969. pd->bonus->duration=conv_num(st,& (st->stack->stack_data[st->start+4]));
  8970. pd->bonus->delay=conv_num(st,& (st->stack->stack_data[st->start+5]));
  8971. if (pd->state.skillbonus == 1)
  8972. pd->state.skillbonus=0; // waiting state
  8973. // wait for timer to start
  8974. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  8975. pd->bonus->timer=-1;
  8976. else
  8977. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  8978. return 0;
  8979. }
  8980. /*==========================================
  8981. * pet looting [Valaris] //Rewritten by [Skotlex]
  8982. *------------------------------------------
  8983. */
  8984. int buildin_petloot(struct script_state *st)
  8985. {
  8986. int max;
  8987. struct pet_data *pd;
  8988. struct map_session_data *sd=script_rid2sd(st);
  8989. if(sd==NULL || sd->pd==NULL)
  8990. return 0;
  8991. max=conv_num(st,& (st->stack->stack_data[st->start+2]));
  8992. if(max < 1)
  8993. max = 1; //Let'em loot at least 1 item.
  8994. else if (max > MAX_PETLOOT_SIZE)
  8995. max = MAX_PETLOOT_SIZE;
  8996. pd = sd->pd;
  8997. if (pd->loot != NULL)
  8998. { //Release whatever was there already and reallocate memory
  8999. pet_lootitem_drop(pd, pd->msd);
  9000. aFree(pd->loot->item);
  9001. }
  9002. else
  9003. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9004. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9005. pd->loot->max=max;
  9006. pd->loot->count = 0;
  9007. pd->loot->weight = 0;
  9008. return 0;
  9009. }
  9010. /*==========================================
  9011. * PCの所持品情報読み取り
  9012. *------------------------------------------
  9013. */
  9014. int buildin_getinventorylist(struct script_state *st)
  9015. {
  9016. struct map_session_data *sd=script_rid2sd(st);
  9017. unsigned char card_var[NAME_LENGTH];
  9018. int i,j=0,k;
  9019. if(!sd) return 0;
  9020. for(i=0;i<MAX_INVENTORY;i++){
  9021. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9022. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9023. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9024. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9025. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9026. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9027. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9028. for (k = 0; k < MAX_SLOTS; k++)
  9029. {
  9030. sprintf(card_var, "@inventorylist_card%d",k+1);
  9031. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9032. }
  9033. j++;
  9034. }
  9035. }
  9036. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9037. return 0;
  9038. }
  9039. int buildin_getskilllist(struct script_state *st)
  9040. {
  9041. struct map_session_data *sd=script_rid2sd(st);
  9042. int i,j=0;
  9043. if(!sd) return 0;
  9044. for(i=0;i<MAX_SKILL;i++){
  9045. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9046. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9047. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9048. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9049. j++;
  9050. }
  9051. }
  9052. pc_setreg(sd,add_str("@skilllist_count"),j);
  9053. return 0;
  9054. }
  9055. int buildin_clearitem(struct script_state *st)
  9056. {
  9057. struct map_session_data *sd=script_rid2sd(st);
  9058. int i;
  9059. if(sd==NULL) return 0;
  9060. for (i=0; i<MAX_INVENTORY; i++) {
  9061. if (sd->status.inventory[i].amount) {
  9062. //Logs items, got from (N)PC scripts [Lupus]
  9063. if(log_config.enable_logs&0x40)
  9064. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9065. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9066. }
  9067. }
  9068. return 0;
  9069. }
  9070. /*==========================================
  9071. Disguise Player (returns Mob/NPC ID if success, 0 on fail) [Lupus]
  9072. *------------------------------------------
  9073. */
  9074. int buildin_disguise(struct script_state *st)
  9075. {
  9076. struct map_session_data *sd=script_rid2sd(st);
  9077. int id;
  9078. id = conv_num(st,& (st->stack->stack_data[st->start+2]));
  9079. if (!mobdb_checkid(id) && !npcdb_checkid(id)) {
  9080. push_val(st->stack,C_INT,0);
  9081. return 0;
  9082. }
  9083. pc_disguise(sd, id);
  9084. push_val(st->stack,C_INT,id);
  9085. return 0;
  9086. }
  9087. /*==========================================
  9088. Undisguise Player (returns 1 if success, 0 on fail) [Lupus]
  9089. *------------------------------------------
  9090. */
  9091. int buildin_undisguise(struct script_state *st)
  9092. {
  9093. struct map_session_data *sd=script_rid2sd(st);
  9094. if (sd->disguise) {
  9095. pc_disguise(sd, 0);
  9096. push_val(st->stack,C_INT,0);
  9097. } else {
  9098. push_val(st->stack,C_INT,1);
  9099. }
  9100. return 0;
  9101. }
  9102. /*==========================================
  9103. * NPCクラスチェンジ
  9104. * classは変わりたいclass
  9105. * typeは通常0なのかな?
  9106. *------------------------------------------
  9107. */
  9108. int buildin_classchange(struct script_state *st)
  9109. {
  9110. int _class,type;
  9111. struct block_list *bl=map_id2bl(st->oid);
  9112. if(bl==NULL) return 0;
  9113. _class=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9114. type=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9115. clif_class_change(bl,_class,type);
  9116. return 0;
  9117. }
  9118. /*==========================================
  9119. * NPCから発生するエフェクト
  9120. *------------------------------------------
  9121. */
  9122. int buildin_misceffect(struct script_state *st)
  9123. {
  9124. int type;
  9125. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9126. if(st->oid && st->oid != fake_nd->bl.id) {
  9127. struct block_list *bl = map_id2bl(st->oid);
  9128. if (bl)
  9129. clif_misceffect2(bl,type);
  9130. } else{
  9131. struct map_session_data *sd=script_rid2sd(st);
  9132. if(sd)
  9133. clif_misceffect2(&sd->bl,type);
  9134. }
  9135. return 0;
  9136. }
  9137. /*==========================================
  9138. * サウンドエフェクト
  9139. *------------------------------------------
  9140. */
  9141. int buildin_soundeffect(struct script_state *st)
  9142. {
  9143. // Redundn
  9144. struct map_session_data *sd=script_rid2sd(st);
  9145. char *name;
  9146. int type=0;
  9147. name=conv_str(st,& (st->stack->stack_data[st->start+2]));
  9148. type=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9149. if(sd){
  9150. if(!st->rid)
  9151. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9152. else{
  9153. clif_soundeffect(sd,&sd->bl,name,type);
  9154. }
  9155. }
  9156. return 0;
  9157. }
  9158. int soundeffect_sub(struct block_list* bl,va_list ap)
  9159. {
  9160. char *name;
  9161. int type;
  9162. nullpo_retr(0, bl);
  9163. nullpo_retr(0, ap);
  9164. name = va_arg(ap,char *);
  9165. type = va_arg(ap,int);
  9166. clif_soundeffect((struct map_session_data *)bl, bl, name, type);
  9167. return 0;
  9168. }
  9169. int buildin_soundeffectall(struct script_state *st)
  9170. {
  9171. // [Lance] - Improved.
  9172. char *name, *map = NULL;
  9173. struct block_list *bl;
  9174. int type, coverage, x0, y0, x1, y1;
  9175. name=conv_str(st,& (st->stack->stack_data[st->start+2]));
  9176. type=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9177. coverage=conv_num(st,& (st->stack->stack_data[st->start+4]));
  9178. if(!st->rid)
  9179. bl = map_id2bl(st->oid);
  9180. else
  9181. bl = &(script_rid2sd(st)->bl);
  9182. if(bl){
  9183. if(coverage < 23){
  9184. clif_soundeffectall(bl,name,type,coverage);
  9185. }else {
  9186. if(st->end > st->start+9){
  9187. map=conv_str(st,& (st->stack->stack_data[st->start+5]));
  9188. x0 = conv_num(st,& (st->stack->stack_data[st->start+6]));
  9189. y0 = conv_num(st,& (st->stack->stack_data[st->start+7]));
  9190. x1 = conv_num(st,& (st->stack->stack_data[st->start+8]));
  9191. y1 = conv_num(st,& (st->stack->stack_data[st->start+9]));
  9192. map_foreachinarea(soundeffect_sub,map_mapname2mapid(map),x0,y0,x1,y1,BL_PC,name,type);
  9193. } else {
  9194. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9195. }
  9196. }
  9197. }
  9198. return 0;
  9199. }
  9200. /*==========================================
  9201. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9202. *------------------------------------------
  9203. */
  9204. int buildin_petrecovery(struct script_state *st)
  9205. {
  9206. struct pet_data *pd;
  9207. struct map_session_data *sd=script_rid2sd(st);
  9208. if(sd==NULL || sd->pd==NULL)
  9209. return 0;
  9210. pd=sd->pd;
  9211. if (pd->recovery)
  9212. { //Halt previous bonus
  9213. if (pd->recovery->timer != -1)
  9214. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9215. } else //Init
  9216. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9217. pd->recovery->type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9218. pd->recovery->delay=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9219. pd->recovery->timer=-1;
  9220. return 0;
  9221. }
  9222. /*==========================================
  9223. * pet healing [Valaris] //Rewritten by [Skotlex]
  9224. *------------------------------------------
  9225. */
  9226. int buildin_petheal(struct script_state *st)
  9227. {
  9228. struct pet_data *pd;
  9229. struct map_session_data *sd=script_rid2sd(st);
  9230. if(sd==NULL || sd->pd==NULL)
  9231. return 0;
  9232. pd=sd->pd;
  9233. if (pd->s_skill)
  9234. { //Clear previous skill
  9235. if (pd->s_skill->timer != -1)
  9236. {
  9237. if (pd->s_skill->id)
  9238. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9239. else
  9240. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9241. }
  9242. } else //init memory
  9243. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9244. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9245. //Use the lv as the amount to heal
  9246. pd->s_skill->lv=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9247. pd->s_skill->delay=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9248. pd->s_skill->hp=conv_num(st,& (st->stack->stack_data[st->start+4]));
  9249. pd->s_skill->sp=conv_num(st,& (st->stack->stack_data[st->start+5]));
  9250. //Use delay as initial offset to avoid skill/heal exploits
  9251. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9252. pd->s_skill->timer=-1;
  9253. else
  9254. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9255. return 0;
  9256. }
  9257. /*==========================================
  9258. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9259. *------------------------------------------
  9260. */
  9261. int buildin_petskillattack(struct script_state *st)
  9262. {
  9263. struct pet_data *pd;
  9264. struct map_session_data *sd=script_rid2sd(st);
  9265. if(sd==NULL || sd->pd==NULL)
  9266. return 0;
  9267. pd=sd->pd;
  9268. if (pd->a_skill == NULL)
  9269. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9270. pd->a_skill->id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9271. pd->a_skill->lv=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9272. pd->a_skill->div_ = 0;
  9273. pd->a_skill->rate=conv_num(st,& (st->stack->stack_data[st->start+4]));
  9274. pd->a_skill->bonusrate=conv_num(st,& (st->stack->stack_data[st->start+5]));
  9275. return 0;
  9276. }
  9277. /*==========================================
  9278. * pet attack skills [Valaris]
  9279. *------------------------------------------
  9280. */
  9281. int buildin_petskillattack2(struct script_state *st)
  9282. {
  9283. struct pet_data *pd;
  9284. struct map_session_data *sd=script_rid2sd(st);
  9285. if(sd==NULL || sd->pd==NULL)
  9286. return 0;
  9287. pd=sd->pd;
  9288. if (pd->a_skill == NULL)
  9289. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9290. pd->a_skill->id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9291. pd->a_skill->lv=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9292. pd->a_skill->div_ = conv_num(st,& (st->stack->stack_data[st->start+4]));
  9293. pd->a_skill->rate=conv_num(st,& (st->stack->stack_data[st->start+5]));
  9294. pd->a_skill->bonusrate=conv_num(st,& (st->stack->stack_data[st->start+6]));
  9295. return 0;
  9296. }
  9297. /*==========================================
  9298. * pet support skills [Skotlex]
  9299. *------------------------------------------
  9300. */
  9301. int buildin_petskillsupport(struct script_state *st)
  9302. {
  9303. struct pet_data *pd;
  9304. struct map_session_data *sd=script_rid2sd(st);
  9305. if(sd==NULL || sd->pd==NULL)
  9306. return 0;
  9307. pd=sd->pd;
  9308. if (pd->s_skill)
  9309. { //Clear previous skill
  9310. if (pd->s_skill->timer != -1)
  9311. {
  9312. if (pd->s_skill->id)
  9313. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9314. else
  9315. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9316. }
  9317. } else //init memory
  9318. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9319. pd->s_skill->id=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9320. pd->s_skill->lv=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9321. pd->s_skill->delay=conv_num(st,& (st->stack->stack_data[st->start+4]));
  9322. pd->s_skill->hp=conv_num(st,& (st->stack->stack_data[st->start+5]));
  9323. pd->s_skill->sp=conv_num(st,& (st->stack->stack_data[st->start+6]));
  9324. //Use delay as initial offset to avoid skill/heal exploits
  9325. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9326. pd->s_skill->timer=-1;
  9327. else
  9328. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9329. return 0;
  9330. }
  9331. /*==========================================
  9332. * Scripted skill effects [Celest]
  9333. *------------------------------------------
  9334. */
  9335. int buildin_skilleffect(struct script_state *st)
  9336. {
  9337. struct map_session_data *sd;
  9338. int skillid=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9339. int skilllv=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9340. sd=script_rid2sd(st);
  9341. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9342. return 0;
  9343. }
  9344. /*==========================================
  9345. * NPC skill effects [Valaris]
  9346. *------------------------------------------
  9347. */
  9348. int buildin_npcskilleffect(struct script_state *st)
  9349. {
  9350. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9351. int skillid=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9352. int skilllv=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9353. int x=conv_num(st,& (st->stack->stack_data[st->start+4]));
  9354. int y=conv_num(st,& (st->stack->stack_data[st->start+5]));
  9355. clif_skill_poseffect(&nd->bl,skillid,skilllv,x,y,gettick());
  9356. return 0;
  9357. }
  9358. /*==========================================
  9359. * Special effects [Valaris]
  9360. *------------------------------------------
  9361. */
  9362. int buildin_specialeffect(struct script_state *st)
  9363. {
  9364. struct block_list *bl=map_id2bl(st->oid);
  9365. if(bl==NULL)
  9366. return 0;
  9367. clif_specialeffect(bl,conv_num(st,& (st->stack->stack_data[st->start+2])), ((st->end > st->start+3)?conv_num(st,& (st->stack->stack_data[st->start+3])):AREA));
  9368. return 0;
  9369. }
  9370. int buildin_specialeffect2(struct script_state *st)
  9371. {
  9372. struct map_session_data *sd=script_rid2sd(st);
  9373. if(sd==NULL)
  9374. return 0;
  9375. clif_specialeffect(&sd->bl,conv_num(st,& (st->stack->stack_data[st->start+2])), ((st->end > st->start+3)?conv_num(st,& (st->stack->stack_data[st->start+3])):AREA));
  9376. return 0;
  9377. }
  9378. /*==========================================
  9379. * Nude [Valaris]
  9380. *------------------------------------------
  9381. */
  9382. int buildin_nude(struct script_state *st)
  9383. {
  9384. struct map_session_data *sd=script_rid2sd(st);
  9385. int i,calcflag=0;
  9386. if(sd==NULL)
  9387. return 0;
  9388. for(i=0;i<11;i++)
  9389. if(sd->equip_index[i] >= 0) {
  9390. if(!calcflag)
  9391. calcflag=1;
  9392. pc_unequipitem(sd,sd->equip_index[i],2);
  9393. }
  9394. if(calcflag)
  9395. status_calc_pc(sd,0);
  9396. return 0;
  9397. }
  9398. /*==========================================
  9399. * gmcommand [MouseJstr]
  9400. *
  9401. * suggested on the forums...
  9402. * splitted into atcommand & charcommand by [Skotlex]
  9403. *------------------------------------------
  9404. */
  9405. int buildin_atcommand(struct script_state *st)
  9406. {
  9407. struct map_session_data *sd=NULL;
  9408. char *cmd;
  9409. cmd = conv_str(st,& (st->stack->stack_data[st->start+2]));
  9410. if (st->rid)
  9411. sd = script_rid2sd(st);
  9412. if (sd){
  9413. if(cmd[0] != atcommand_symbol){
  9414. cmd += strlen(sd->status.name);
  9415. while(*cmd != atcommand_symbol && *cmd != 0)
  9416. cmd++;
  9417. }
  9418. is_atcommand_sub(sd->fd, sd, cmd, 99);
  9419. } else { //Use a dummy character.
  9420. struct map_session_data dummy_sd;
  9421. struct block_list *bl = NULL;
  9422. memset(&dummy_sd, 0, sizeof(struct map_session_data));
  9423. if (st->oid) bl = map_id2bl(st->oid);
  9424. if (bl) {
  9425. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9426. if (bl->type == BL_NPC)
  9427. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9428. }
  9429. if(cmd[0] != atcommand_symbol){
  9430. cmd += strlen(dummy_sd.status.name);
  9431. while(*cmd != atcommand_symbol && *cmd != 0)
  9432. cmd++;
  9433. }
  9434. is_atcommand_sub(0, &dummy_sd, cmd, 99);
  9435. }
  9436. return 0;
  9437. }
  9438. int buildin_charcommand(struct script_state *st)
  9439. {
  9440. struct map_session_data *sd=NULL;
  9441. char *cmd;
  9442. cmd = conv_str(st,& (st->stack->stack_data[st->start+2]));
  9443. if (st->rid)
  9444. sd = script_rid2sd(st);
  9445. if (sd){
  9446. if(cmd[0] != charcommand_symbol){
  9447. cmd += strlen(sd->status.name);
  9448. while(*cmd != charcommand_symbol && *cmd != 0)
  9449. cmd++;
  9450. }
  9451. is_charcommand_sub(sd->fd, sd, cmd,99);
  9452. } else { //Use a dummy character.
  9453. struct map_session_data dummy_sd;
  9454. struct block_list *bl = NULL;
  9455. memset(&dummy_sd, 0, sizeof(struct map_session_data));
  9456. if (st->oid) bl = map_id2bl(st->oid);
  9457. if (bl) {
  9458. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9459. if (bl->type == BL_NPC)
  9460. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9461. }
  9462. if(cmd[0] != charcommand_symbol){
  9463. cmd += strlen(dummy_sd.status.name);
  9464. while(*cmd != charcommand_symbol && *cmd != 0)
  9465. cmd++;
  9466. }
  9467. is_charcommand_sub(0, &dummy_sd, cmd, 99);
  9468. }
  9469. return 0;
  9470. }
  9471. /*==========================================
  9472. * Displays a message for the player only (like system messages like "you got an apple" )
  9473. *------------------------------------------
  9474. */
  9475. int buildin_dispbottom(struct script_state *st)
  9476. {
  9477. struct map_session_data *sd=script_rid2sd(st);
  9478. char *message;
  9479. message=conv_str(st,& (st->stack->stack_data[st->start+2]));
  9480. if(sd)
  9481. clif_disp_onlyself(sd,message,(int)strlen(message));
  9482. return 0;
  9483. }
  9484. /*==========================================
  9485. * All The Players Full Recovery
  9486. (HP/SP full restore and resurrect if need)
  9487. *------------------------------------------
  9488. */
  9489. int buildin_recovery(struct script_state *st)
  9490. {
  9491. struct map_session_data *sd, **all_sd;
  9492. int i = 0, users;
  9493. all_sd = map_getallusers(&users);
  9494. for (i = 0; i < users; i++)
  9495. {
  9496. sd = all_sd[i];
  9497. if(pc_isdead(sd))
  9498. status_revive(&sd->bl, 100, 100);
  9499. else
  9500. status_percent_heal(&sd->bl, 100, 100);
  9501. clif_displaymessage(sd->fd,"You have been recovered!");
  9502. }
  9503. return 0;
  9504. }
  9505. /*==========================================
  9506. * Get your pet info: getpetinfo(n)
  9507. * n -> 0:pet_id 1:pet_class 2:pet_name
  9508. 3:friendly 4:hungry
  9509. *------------------------------------------
  9510. */
  9511. int buildin_getpetinfo(struct script_state *st)
  9512. {
  9513. struct map_session_data *sd=script_rid2sd(st);
  9514. struct pet_data *pd;
  9515. int type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9516. if(sd && sd->status.pet_id && sd->pd){
  9517. pd = sd->pd;
  9518. switch(type){
  9519. case 0:
  9520. push_val(st->stack,C_INT,sd->status.pet_id);
  9521. break;
  9522. case 1:
  9523. push_val(st->stack,C_INT,pd->pet.class_);
  9524. break;
  9525. case 2:
  9526. if(pd->pet.name)
  9527. push_str(st->stack,C_CONSTSTR,pd->pet.name);
  9528. else
  9529. push_str(st->stack,C_CONSTSTR,"null");
  9530. break;
  9531. case 3:
  9532. push_val(st->stack,C_INT,pd->pet.intimate);
  9533. break;
  9534. case 4:
  9535. push_val(st->stack,C_INT,pd->pet.hungry);
  9536. break;
  9537. default:
  9538. push_val(st->stack,C_INT,0);
  9539. break;
  9540. }
  9541. }else{
  9542. push_val(st->stack,C_INT,0);
  9543. }
  9544. return 0;
  9545. }
  9546. /*==========================================
  9547. * Shows wether your inventory(and equips) contain
  9548. selected card or not.
  9549. checkequipedcard(4001);
  9550. *------------------------------------------
  9551. */
  9552. int buildin_checkequipedcard(struct script_state *st)
  9553. {
  9554. struct map_session_data *sd=script_rid2sd(st);
  9555. int n,i,c=0;
  9556. c=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9557. if(sd){
  9558. for(i=0;i<MAX_INVENTORY;i++){
  9559. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9560. for(n=0;n<MAX_SLOTS;n++){
  9561. if(sd->status.inventory[i].card[n]==c){
  9562. push_val(st->stack,C_INT,1);
  9563. return 0;
  9564. }
  9565. }
  9566. }
  9567. }
  9568. }
  9569. push_val(st->stack,C_INT,0);
  9570. return 0;
  9571. }
  9572. int buildin_jump_zero(struct script_state *st) {
  9573. int sel;
  9574. sel=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9575. if(!sel) {
  9576. int pos;
  9577. if( st->stack->stack_data[st->start+3].type!=C_POS ){
  9578. ShowError("script: jump_zero: not label !\n");
  9579. st->state=END;
  9580. return 0;
  9581. }
  9582. pos=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9583. st->pos=pos;
  9584. st->state=GOTO;
  9585. // printf("script: jump_zero: jumpto : %d\n",pos);
  9586. } else {
  9587. // printf("script: jump_zero: fail\n");
  9588. }
  9589. return 0;
  9590. }
  9591. int buildin_select(struct script_state *st)
  9592. {
  9593. char *buf, *ptr;
  9594. int len,i;
  9595. struct map_session_data *sd;
  9596. sd=script_rid2sd(st);
  9597. nullpo_retr(0, sd);
  9598. if(sd->state.menu_or_input==0){
  9599. st->state=RERUNLINE;
  9600. sd->state.menu_or_input=1;
  9601. for(i=st->start+2,len=16;i<st->end;i++){
  9602. conv_str(st,& (st->stack->stack_data[i]));
  9603. len+=(int)strlen(st->stack->stack_data[i].u.str)+1;
  9604. }
  9605. buf=(char *)aMalloc((len+1)*sizeof(char));
  9606. buf[0]=0;
  9607. for(i=st->start+2,len=0;i<st->end;i++){
  9608. strcat(buf,st->stack->stack_data[i].u.str);
  9609. strcat(buf,":");
  9610. }
  9611. ptr = buf;
  9612. sd->npc_menu = 0; //Reuse to store max menu entries. Avoids the need of an extra variable.
  9613. while (ptr && (ptr = strchr(ptr, ':')) != NULL)
  9614. { sd->npc_menu++; ptr++; }
  9615. clif_scriptmenu(sd,st->oid,buf);
  9616. aFree(buf);
  9617. } else if(sd->npc_menu==0xff){
  9618. sd->state.menu_or_input=0;
  9619. st->state=END;
  9620. } else {
  9621. //Skip empty menu entries which weren't displayed on the client (Skotlex)
  9622. for(i=st->start+2;i< (st->start+2+sd->npc_menu) && sd->npc_menu < (st->end-st->start-2);i++) {
  9623. conv_str(st,& (st->stack->stack_data[i])); // we should convert variables to strings before access it [jA1983] [EoE]
  9624. if((int)strlen(st->stack->stack_data[i].u.str) < 1)
  9625. sd->npc_menu++; //Empty selection which wasn't displayed on the client.
  9626. }
  9627. pc_setreg(sd,add_str("@menu"),sd->npc_menu);
  9628. sd->state.menu_or_input=0;
  9629. push_val(st->stack,C_INT,sd->npc_menu);
  9630. }
  9631. return 0;
  9632. }
  9633. int buildin_prompt(struct script_state *st)
  9634. {
  9635. char *buf, *ptr;
  9636. int len,i;
  9637. struct map_session_data *sd;
  9638. sd=script_rid2sd(st);
  9639. nullpo_retr(0, sd);
  9640. if(sd->state.menu_or_input==0){
  9641. st->state=RERUNLINE;
  9642. sd->state.menu_or_input=1;
  9643. for(i=st->start+2,len=16;i<st->end;i++){
  9644. conv_str(st,& (st->stack->stack_data[i]));
  9645. len+=(int)strlen(st->stack->stack_data[i].u.str)+1;
  9646. }
  9647. buf=(char *)aMalloc((len+1)*sizeof(char));
  9648. buf[0]=0;
  9649. for(i=st->start+2,len=0;i<st->end;i++){
  9650. strcat(buf,st->stack->stack_data[i].u.str);
  9651. strcat(buf,":");
  9652. }
  9653. ptr = buf;
  9654. sd->npc_menu = 0; //Reuse to store max menu entries. Avoids the need of an extra variable.
  9655. while (ptr && (ptr = strchr(ptr, ':')) != NULL)
  9656. { sd->npc_menu++; ptr++; }
  9657. clif_scriptmenu(sd,st->oid,buf);
  9658. aFree(buf);
  9659. } else {
  9660. if(sd->npc_menu != 0xff){
  9661. //Skip empty menu entries which weren't displayed on the client (Skotlex)
  9662. for(i=st->start+2;i< (st->start+2+sd->npc_menu) && sd->npc_menu < (st->end-st->start-2);i++) {
  9663. conv_str(st,& (st->stack->stack_data[i])); // we should convert variables to strings before access it [jA1983] [EoE]
  9664. if((int)strlen(st->stack->stack_data[i].u.str) < 1)
  9665. sd->npc_menu++; //Empty selection which wasn't displayed on the client.
  9666. }
  9667. }
  9668. pc_setreg(sd,add_str("@menu"),sd->npc_menu);
  9669. sd->state.menu_or_input=0;
  9670. push_val(st->stack,C_INT,sd->npc_menu);
  9671. }
  9672. return 0;
  9673. }
  9674. /*==========================================
  9675. * GetMapMobs
  9676. returns mob counts on a set map:
  9677. e.g. GetMapMobs("prontera.gat")
  9678. use "this" - for player's map
  9679. *------------------------------------------
  9680. */
  9681. int buildin_getmapmobs(struct script_state *st)
  9682. {
  9683. char *str=NULL;
  9684. int m=-1,bx,by,i;
  9685. int count=0,c;
  9686. struct block_list *bl;
  9687. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  9688. if(strcmp(str,"this")==0){
  9689. struct map_session_data *sd=script_rid2sd(st);
  9690. if(sd)
  9691. m=sd->bl.m;
  9692. else{
  9693. push_val(st->stack,C_INT,-1);
  9694. return 0;
  9695. }
  9696. }else
  9697. m=map_mapname2mapid(str);
  9698. if(m < 0){
  9699. push_val(st->stack,C_INT,-1);
  9700. return 0;
  9701. }
  9702. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++){
  9703. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++){
  9704. bl = map[m].block_mob[bx+by*map[m].bxs];
  9705. c = map[m].block_mob_count[bx+by*map[m].bxs];
  9706. for(i=0;i<c && bl;i++,bl=bl->next){
  9707. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9708. count++;
  9709. }
  9710. }
  9711. }
  9712. push_val(st->stack,C_INT,count);
  9713. return 0;
  9714. }
  9715. /*==========================================
  9716. * movenpc [MouseJstr]
  9717. *------------------------------------------
  9718. */
  9719. int buildin_movenpc(struct script_state *st)
  9720. {
  9721. TBL_NPC *nd = NULL;
  9722. char *npc;
  9723. int x,y;
  9724. short m;
  9725. npc = conv_str(st,& (st->stack->stack_data[st->start+2]));
  9726. x = conv_num(st,& (st->stack->stack_data[st->start+3]));
  9727. y = conv_num(st,& (st->stack->stack_data[st->start+4]));
  9728. if ((nd = npc_name2id(npc)) == NULL)
  9729. return -1;
  9730. if ((m=nd->bl.m) < 0 || nd->bl.prev == NULL)
  9731. return -1; //Not on a map.
  9732. if (x < 0) x = 0;
  9733. else if (x >= map[m].xs) x = map[m].xs-1;
  9734. if (y < 0) y = 0;
  9735. else if (y >= map[m].ys) y = map[m].ys-1;
  9736. map_foreachinrange(clif_outsight, &nd->bl, AREA_SIZE, BL_PC, &nd->bl);
  9737. map_moveblock(&nd->bl, x, y, gettick());
  9738. map_foreachinrange(clif_insight, &nd->bl, AREA_SIZE, BL_PC, &nd->bl);
  9739. return 0;
  9740. }
  9741. /*==========================================
  9742. * message [MouseJstr]
  9743. *------------------------------------------
  9744. */
  9745. int buildin_message(struct script_state *st)
  9746. {
  9747. struct map_session_data *sd;
  9748. char *msg,*player;
  9749. struct map_session_data *pl_sd = NULL;
  9750. sd = script_rid2sd(st);
  9751. player = conv_str(st,& (st->stack->stack_data[st->start+2]));
  9752. msg = conv_str(st,& (st->stack->stack_data[st->start+3]));
  9753. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9754. return 0;
  9755. clif_displaymessage(pl_sd->fd, msg);
  9756. return 0;
  9757. }
  9758. /*==========================================
  9759. * npctalk (sends message to surrounding
  9760. * area) [Valaris]
  9761. *------------------------------------------
  9762. */
  9763. int buildin_npctalk(struct script_state *st)
  9764. {
  9765. char *str;
  9766. char message[255];
  9767. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9768. str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  9769. if(nd) {
  9770. memcpy(message, nd->name, NAME_LENGTH);
  9771. strcat(message," : ");
  9772. strncat(message,str, 254); //Prevent overflow possibility. [Skotlex]
  9773. clif_message(&(nd->bl), message);
  9774. }
  9775. return 0;
  9776. }
  9777. /*==========================================
  9778. * hasitems (checks to see if player has any
  9779. * items on them, if so will return a 1)
  9780. * [Valaris]
  9781. *------------------------------------------
  9782. */
  9783. int buildin_hasitems(struct script_state *st)
  9784. {
  9785. int i;
  9786. struct map_session_data *sd;
  9787. sd=script_rid2sd(st);
  9788. for(i=0; i<MAX_INVENTORY; i++) {
  9789. if(sd->status.inventory[i].amount && sd->status.inventory[i].nameid!=2364 && sd->status.inventory[i].nameid!=2365) {
  9790. push_val(st->stack,C_INT,1);
  9791. return 0;
  9792. }
  9793. }
  9794. push_val(st->stack,C_INT,0);
  9795. return 0;
  9796. }
  9797. // change npc walkspeed [Valaris]
  9798. int buildin_npcspeed(struct script_state *st)
  9799. {
  9800. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9801. int x=0;
  9802. x=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9803. if(nd) {
  9804. nd->speed=x;
  9805. }
  9806. return 0;
  9807. }
  9808. // make an npc walk to a position [Valaris]
  9809. int buildin_npcwalkto(struct script_state *st)
  9810. {
  9811. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9812. int x=0,y=0;
  9813. x=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9814. y=conv_num(st,& (st->stack->stack_data[st->start+3]));
  9815. if(nd) {
  9816. unit_walktoxy(&nd->bl,x,y,0);
  9817. }
  9818. return 0;
  9819. }
  9820. // stop an npc's movement [Valaris]
  9821. int buildin_npcstop(struct script_state *st)
  9822. {
  9823. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9824. if(nd) {
  9825. unit_stop_walking(&nd->bl,1);
  9826. }
  9827. return 0;
  9828. }
  9829. /*==========================================
  9830. * getlook char info. getlook(arg)
  9831. *------------------------------------------
  9832. */
  9833. int buildin_getlook(struct script_state *st){
  9834. int type,val;
  9835. struct map_session_data *sd;
  9836. sd=script_rid2sd(st);
  9837. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9838. val=-1;
  9839. switch(type){
  9840. case LOOK_HAIR: //1
  9841. val=sd->status.hair;
  9842. break;
  9843. case LOOK_WEAPON: //2
  9844. val=sd->status.weapon;
  9845. break;
  9846. case LOOK_HEAD_BOTTOM: //3
  9847. val=sd->status.head_bottom;
  9848. break;
  9849. case LOOK_HEAD_TOP: //4
  9850. val=sd->status.head_top;
  9851. break;
  9852. case LOOK_HEAD_MID: //5
  9853. val=sd->status.head_mid;
  9854. break;
  9855. case LOOK_HAIR_COLOR: //6
  9856. val=sd->status.hair_color;
  9857. break;
  9858. case LOOK_CLOTHES_COLOR: //7
  9859. val=sd->status.clothes_color;
  9860. break;
  9861. case LOOK_SHIELD: //8
  9862. val=sd->status.shield;
  9863. break;
  9864. case LOOK_SHOES: //9
  9865. break;
  9866. }
  9867. push_val(st->stack,C_INT,val);
  9868. return 0;
  9869. }
  9870. /*==========================================
  9871. * get char save point. argument: 0- map name, 1- x, 2- y
  9872. *------------------------------------------
  9873. */
  9874. int buildin_getsavepoint(struct script_state *st)
  9875. {
  9876. int x,y,type;
  9877. char *mapname;
  9878. struct map_session_data *sd;
  9879. sd=script_rid2sd(st);
  9880. type=conv_num(st,& (st->stack->stack_data[st->start+2]));
  9881. x=sd->status.save_point.x;
  9882. y=sd->status.save_point.y;
  9883. switch(type){
  9884. case 0:
  9885. mapname=(char *) aMallocA((MAP_NAME_LENGTH+1)*sizeof(char));
  9886. memcpy(mapname, mapindex_id2name(sd->status.save_point.map), MAP_NAME_LENGTH);
  9887. mapname[MAP_NAME_LENGTH]='\0';
  9888. push_str(st->stack,C_STR,mapname);
  9889. break;
  9890. case 1:
  9891. push_val(st->stack,C_INT,x);
  9892. break;
  9893. case 2:
  9894. push_val(st->stack,C_INT,y);
  9895. break;
  9896. default:
  9897. push_val(st->stack,C_INT,0);
  9898. break;
  9899. }
  9900. return 0;
  9901. }
  9902. /*==========================================
  9903. * Get position for char/npc/pet/mob objects. Added by Lorky
  9904. *
  9905. * int getMapXY(MapName$,MaxX,MapY,type,[CharName$]);
  9906. * where type:
  9907. * MapName$ - String variable for output map name
  9908. * MapX - Integer variable for output coord X
  9909. * MapY - Integer variable for output coord Y
  9910. * type - type of object
  9911. * 0 - Character coord
  9912. * 1 - NPC coord
  9913. * 2 - Pet coord
  9914. * 3 - Mob coord (not released)
  9915. * 4 - Homun coord
  9916. * CharName$ - Name object. If miss or "this" the current object
  9917. *
  9918. * Return:
  9919. * 0 - success
  9920. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9921. *------------------------------------------
  9922. */
  9923. int buildin_getmapxy(struct script_state *st){
  9924. struct block_list *bl = NULL;
  9925. struct map_session_data *sd=NULL;
  9926. int num;
  9927. char *name;
  9928. char prefix;
  9929. int x,y,type;
  9930. char mapname[MAP_NAME_LENGTH+1];
  9931. memset(mapname, 0, sizeof(mapname));
  9932. if( st->stack->stack_data[st->start+2].type!=C_NAME ){
  9933. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9934. push_val(st->stack,C_INT,-1);
  9935. return 1;
  9936. }
  9937. if( st->stack->stack_data[st->start+3].type!=C_NAME ){
  9938. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9939. push_val(st->stack,C_INT,-1);
  9940. return 1;
  9941. }
  9942. if( st->stack->stack_data[st->start+4].type!=C_NAME ){
  9943. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9944. push_val(st->stack,C_INT,-1);
  9945. return 1;
  9946. }
  9947. //??????????? >>> Possible needly check function parameters on C_STR,C_INT,C_INT <<< ???????????//
  9948. type=conv_num(st,& (st->stack->stack_data[st->start+5]));
  9949. switch (type){
  9950. case 0: //Get Character Position
  9951. if( st->end>st->start+6 )
  9952. sd=map_nick2sd(conv_str(st,& (st->stack->stack_data[st->start+6])));
  9953. else
  9954. sd=script_rid2sd(st);
  9955. if (sd)
  9956. bl = &sd->bl;
  9957. break;
  9958. case 1: //Get NPC Position
  9959. if( st->end > st->start+6 )
  9960. {
  9961. struct npc_data *nd;
  9962. nd=npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+6])));
  9963. if (nd)
  9964. bl = &nd->bl;
  9965. } else //In case the origin is not an npc?
  9966. bl=map_id2bl(st->oid);
  9967. break;
  9968. case 2: //Get Pet Position
  9969. if(st->end>st->start+6)
  9970. sd=map_nick2sd(conv_str(st,& (st->stack->stack_data[st->start+6])));
  9971. else
  9972. sd=script_rid2sd(st);
  9973. if (sd && sd->pd)
  9974. bl = &sd->pd->bl;
  9975. break;
  9976. case 3: //Get Mob Position
  9977. break; //Not supported?
  9978. case 4: //Get Homun Position
  9979. if(st->end>st->start+6)
  9980. sd=map_nick2sd(conv_str(st,& (st->stack->stack_data[st->start+6])));
  9981. else
  9982. sd=script_rid2sd(st);
  9983. if (sd && sd->hd)
  9984. bl = &sd->hd->bl;
  9985. break;
  9986. }
  9987. if (!bl) { //No object found.
  9988. push_val(st->stack,C_INT,-1);
  9989. return 0;
  9990. }
  9991. x= bl->x;
  9992. y= bl->y;
  9993. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9994. //Set MapName$
  9995. num=st->stack->stack_data[st->start+2].u.num;
  9996. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9997. prefix=*name;
  9998. if(not_server_variable(prefix))
  9999. sd=script_rid2sd(st);
  10000. else
  10001. sd=NULL;
  10002. set_reg(st,sd,num,name,(void*)mapname,st->stack->stack_data[st->start+2].ref);
  10003. //Set MapX
  10004. num=st->stack->stack_data[st->start+3].u.num;
  10005. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10006. prefix=*name;
  10007. if(not_server_variable(prefix))
  10008. sd=script_rid2sd(st);
  10009. else
  10010. sd=NULL;
  10011. set_reg(st,sd,num,name,(void*)x,st->stack->stack_data[st->start+3].ref);
  10012. //Set MapY
  10013. num=st->stack->stack_data[st->start+4].u.num;
  10014. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10015. prefix=*name;
  10016. if(not_server_variable(prefix))
  10017. sd=script_rid2sd(st);
  10018. else
  10019. sd=NULL;
  10020. set_reg(st,sd,num,name,(void*)y,st->stack->stack_data[st->start+4].ref);
  10021. //Return Success value
  10022. push_val(st->stack,C_INT,0);
  10023. return 0;
  10024. }
  10025. /*==========================================
  10026. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10027. *------------------------------------------
  10028. */
  10029. int buildin_logmes(struct script_state *st)
  10030. {
  10031. if (log_config.npc <= 0 ) return 0;
  10032. conv_str(st,& (st->stack->stack_data[st->start+2]));
  10033. log_npc(script_rid2sd(st),st->stack->stack_data[st->start+2].u.str);
  10034. return 0;
  10035. }
  10036. int buildin_summon(struct script_state *st)
  10037. {
  10038. int _class, timeout=0;
  10039. char *str,*event="";
  10040. struct map_session_data *sd;
  10041. struct mob_data *md;
  10042. int tick = gettick();
  10043. sd=script_rid2sd(st);
  10044. if (!sd) return 0;
  10045. str =conv_str(st,& (st->stack->stack_data[st->start+2]));
  10046. _class=conv_num(st,& (st->stack->stack_data[st->start+3]));
  10047. if( st->end>st->start+4 )
  10048. timeout=conv_num(st,& (st->stack->stack_data[st->start+4]));
  10049. if( st->end>st->start+5 ){
  10050. event=conv_str(st,& (st->stack->stack_data[st->start+5]));
  10051. check_event(st, event);
  10052. }
  10053. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10054. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10055. if (md) {
  10056. md->master_id=sd->bl.id;
  10057. md->special_state.ai=1;
  10058. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10059. mob_spawn (md); //Now it is ready for spawning.
  10060. clif_misceffect2(&md->bl,344);
  10061. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10062. }
  10063. return 0;
  10064. }
  10065. /*==========================================
  10066. * Checks whether it is daytime/nighttime
  10067. *------------------------------------------
  10068. */
  10069. int buildin_isnight(struct script_state *st)
  10070. {
  10071. push_val(st->stack,C_INT, (night_flag == 1));
  10072. return 0;
  10073. }
  10074. int buildin_isday(struct script_state *st)
  10075. {
  10076. push_val(st->stack,C_INT, (night_flag == 0));
  10077. return 0;
  10078. }
  10079. /*================================================
  10080. * Check whether another item/card has been
  10081. * equipped - used for 2/15's cards patch [celest]
  10082. *------------------------------------------------
  10083. */
  10084. // leave this here, just in case
  10085. #if 0
  10086. int buildin_isequipped(struct script_state *st)
  10087. {
  10088. struct map_session_data *sd;
  10089. int i, j, k, id = 1;
  10090. int ret = -1;
  10091. sd = script_rid2sd(st);
  10092. for (i=0; id!=0; i++) {
  10093. int flag = 0;
  10094. FETCH (i+2, id) else id = 0;
  10095. if (id <= 0)
  10096. continue;
  10097. for (j=0; j<EQI_MAX; j++) {
  10098. int index;
  10099. index = sd->equip_index[j];
  10100. if(index < 0) continue;
  10101. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10102. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10103. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10104. if(!sd->inventory_data[index])
  10105. continue;
  10106. if(itemdb_type(id) != IT_CARD) { //Non card
  10107. if (sd->inventory_data[index]->nameid != id)
  10108. continue;
  10109. flag = 1;
  10110. break;
  10111. } else { //Card
  10112. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10113. continue;
  10114. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10115. if (sd->status.inventory[index].card[k] == id)
  10116. {
  10117. flag = 1;
  10118. break;
  10119. }
  10120. }
  10121. }
  10122. if (flag) break;
  10123. }
  10124. if (ret == -1)
  10125. ret = flag;
  10126. else
  10127. ret &= flag;
  10128. if (!ret) break;
  10129. }
  10130. push_val(st->stack,C_INT,ret);
  10131. return 0;
  10132. }
  10133. #endif
  10134. /*================================================
  10135. * Check how many items/cards in the list are
  10136. * equipped - used for 2/15's cards patch [celest]
  10137. *------------------------------------------------
  10138. */
  10139. int buildin_isequippedcnt(struct script_state *st)
  10140. {
  10141. struct map_session_data *sd;
  10142. int i, j, k, id = 1;
  10143. int ret = 0;
  10144. sd = script_rid2sd(st);
  10145. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10146. push_val(st->stack,C_INT,0);
  10147. return 0;
  10148. }
  10149. for (i=0; id!=0; i++) {
  10150. FETCH (i+2, id) else id = 0;
  10151. if (id <= 0)
  10152. continue;
  10153. for (j=0; j<EQI_MAX; j++) {
  10154. int index;
  10155. index = sd->equip_index[j];
  10156. if(index < 0) continue;
  10157. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10158. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10159. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10160. if(!sd->inventory_data[index])
  10161. continue;
  10162. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10163. if (sd->inventory_data[index]->nameid == id)
  10164. ret+= sd->status.inventory[index].amount;
  10165. } else { //Count cards.
  10166. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10167. continue; //No cards
  10168. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10169. if (sd->status.inventory[index].card[k] == id)
  10170. ret++; //[Lupus]
  10171. }
  10172. }
  10173. }
  10174. }
  10175. push_val(st->stack,C_INT,ret);
  10176. return 0;
  10177. }
  10178. /*================================================
  10179. * Check whether another card has been
  10180. * equipped - used for 2/15's cards patch [celest]
  10181. * -- Items checked cannot be reused in another
  10182. * card set to prevent exploits
  10183. *------------------------------------------------
  10184. */
  10185. int buildin_isequipped(struct script_state *st)
  10186. {
  10187. struct map_session_data *sd;
  10188. int i, j, k, id = 1;
  10189. int index, flag;
  10190. int ret = -1;
  10191. //Original hash to reverse it when full check fails.
  10192. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10193. sd = script_rid2sd(st);
  10194. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10195. push_val(st->stack,C_INT,0);
  10196. return 0;
  10197. }
  10198. setitem_hash = sd->setitem_hash;
  10199. setitem_hash2 = sd->setitem_hash2;
  10200. for (i=0; id!=0; i++)
  10201. {
  10202. FETCH (i+2, id) else id = 0;
  10203. if (id <= 0)
  10204. continue;
  10205. flag = 0;
  10206. for (j=0; j<EQI_MAX; j++)
  10207. {
  10208. index = sd->equip_index[j];
  10209. if(index < 0) continue;
  10210. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10211. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10212. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10213. if(!sd->inventory_data[index])
  10214. continue;
  10215. if (itemdb_type(id) != IT_CARD) {
  10216. if (sd->inventory_data[index]->nameid != id)
  10217. continue;
  10218. flag = 1;
  10219. break;
  10220. } else { //Cards
  10221. if (sd->inventory_data[index]->slot == 0 ||
  10222. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10223. continue;
  10224. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10225. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10226. unsigned int hash = 0;
  10227. if (sd->status.inventory[index].card[k] != id)
  10228. continue;
  10229. hash = 1<<((j<5?j:j-5)*4 + k);
  10230. // check if card is already used by another set
  10231. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10232. continue;
  10233. // We have found a match
  10234. flag = 1;
  10235. // Set hash so this card cannot be used by another
  10236. if (j<5)
  10237. sd->setitem_hash |= hash;
  10238. else
  10239. sd->setitem_hash2 |= hash;
  10240. break;
  10241. }
  10242. }
  10243. if (flag) break; //Card found
  10244. }
  10245. if (ret == -1)
  10246. ret = flag;
  10247. else
  10248. ret &= flag;
  10249. if (!ret) break;
  10250. }
  10251. if (!ret)
  10252. { //When check fails, restore original hash values. [Skotlex]
  10253. sd->setitem_hash = setitem_hash;
  10254. sd->setitem_hash2 = setitem_hash2;
  10255. }
  10256. push_val(st->stack,C_INT,ret);
  10257. return 0;
  10258. }
  10259. /*================================================
  10260. * Check how many given inserted cards in the CURRENT
  10261. * weapon - used for 2/15's cards patch [Lupus]
  10262. *------------------------------------------------
  10263. */
  10264. int buildin_cardscnt(struct script_state *st)
  10265. {
  10266. struct map_session_data *sd;
  10267. int i, k, id = 1;
  10268. int ret = 0;
  10269. int index;
  10270. sd = script_rid2sd(st);
  10271. for (i=0; id!=0; i++) {
  10272. FETCH (i+2, id) else id = 0;
  10273. if (id <= 0)
  10274. continue;
  10275. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10276. if(index < 0) continue;
  10277. if(!sd->inventory_data[index])
  10278. continue;
  10279. if(itemdb_type(id) != IT_CARD) {
  10280. if (sd->inventory_data[index]->nameid == id)
  10281. ret+= sd->status.inventory[index].amount;
  10282. } else {
  10283. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10284. continue;
  10285. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10286. if (sd->status.inventory[index].card[k] == id)
  10287. ret++;
  10288. }
  10289. }
  10290. }
  10291. push_val(st->stack,C_INT,ret);
  10292. // push_val(st->stack,C_INT,current_equip_item_index);
  10293. return 0;
  10294. }
  10295. /*=======================================================
  10296. * Returns the refined number of the current item, or an
  10297. * item with inventory index specified
  10298. *-------------------------------------------------------
  10299. */
  10300. int buildin_getrefine(struct script_state *st)
  10301. {
  10302. struct map_session_data *sd;
  10303. if ((sd = script_rid2sd(st))!= NULL)
  10304. push_val(st->stack, C_INT, sd->status.inventory[current_equip_item_index].refine);
  10305. else
  10306. push_val(st->stack,C_INT,0);
  10307. return 0;
  10308. }
  10309. /*=======================================================
  10310. * Allows 2 Parents to adopt a character as a Baby
  10311. *-------------------------------------------------------
  10312. */
  10313. int buildin_adopt(struct script_state *st)
  10314. {
  10315. int ret;
  10316. char *parent1 = conv_str(st,& (st->stack->stack_data[st->start+2]));
  10317. char *parent2 = conv_str(st,& (st->stack->stack_data[st->start+3]));
  10318. char *child = conv_str(st,& (st->stack->stack_data[st->start+4]));
  10319. struct map_session_data *p1_sd = map_nick2sd(parent1);
  10320. struct map_session_data *p2_sd = map_nick2sd(parent2);
  10321. struct map_session_data *c_sd = map_nick2sd(child);
  10322. if (!p1_sd || !p2_sd || !c_sd ||
  10323. p1_sd->status.base_level < 70 ||
  10324. p2_sd->status.base_level < 70)
  10325. return 0;
  10326. ret = pc_adoption(p1_sd, p2_sd, c_sd);
  10327. push_val(st->stack, C_INT, ret);
  10328. return 0;
  10329. }
  10330. /*=======================================================
  10331. * Day/Night controls
  10332. *-------------------------------------------------------
  10333. */
  10334. int buildin_night(struct script_state *st)
  10335. {
  10336. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10337. return 0;
  10338. }
  10339. int buildin_day(struct script_state *st)
  10340. {
  10341. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10342. return 0;
  10343. }
  10344. //=======================================================
  10345. // Unequip [Spectre]
  10346. //-------------------------------------------------------
  10347. int buildin_unequip(struct script_state *st)
  10348. {
  10349. int i;
  10350. size_t num;
  10351. struct map_session_data *sd;
  10352. num = conv_num(st,& (st->stack->stack_data[st->start+2])) - 1;
  10353. sd=script_rid2sd(st);
  10354. if(sd!=NULL && num<10)
  10355. {
  10356. i=pc_checkequip(sd,equip[num]);
  10357. pc_unequipitem(sd,i,2);
  10358. return 0;
  10359. }
  10360. return 0;
  10361. }
  10362. int buildin_equip(struct script_state *st)
  10363. {
  10364. int nameid=0,i;
  10365. struct map_session_data *sd;
  10366. struct item_data *item_data;
  10367. sd = script_rid2sd(st);
  10368. nameid=conv_num(st,& (st->stack->stack_data[st->start+2]));
  10369. if((item_data = itemdb_exists(nameid)) == NULL)
  10370. {
  10371. if(battle_config.error_log)
  10372. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10373. return 1;
  10374. }
  10375. for(i=0;i<MAX_INVENTORY && sd->status.inventory[i].nameid!=nameid;i++);
  10376. if(i==MAX_INVENTORY) return 0;
  10377. pc_equipitem(sd,i,item_data->equip);
  10378. return 0;
  10379. }
  10380. int buildin_autoequip(struct script_state *st){
  10381. int nameid, flag;
  10382. struct item_data *item_data;
  10383. nameid=conv_num(st,& (st->stack->stack_data[st->start+2]));
  10384. flag=conv_num(st,& (st->stack->stack_data[st->start+3]));
  10385. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10386. item_data->flag.autoequip = flag>0?1:0;
  10387. }
  10388. return 0;
  10389. }
  10390. int buildin_setbattleflag(struct script_state *st){
  10391. char *flag, *value;
  10392. flag = conv_str(st,& (st->stack->stack_data[st->start+2]));
  10393. value = conv_str(st,& (st->stack->stack_data[st->start+3]));
  10394. if (battle_set_value(flag, value) == 0)
  10395. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10396. else
  10397. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10398. return 0;
  10399. }
  10400. int buildin_getbattleflag(struct script_state *st){
  10401. char *flag;
  10402. flag = conv_str(st,& (st->stack->stack_data[st->start+2]));
  10403. push_val(st->stack,C_INT,battle_get_value(flag));
  10404. return 0;
  10405. }
  10406. //=======================================================
  10407. // strlen [Valaris]
  10408. //-------------------------------------------------------
  10409. int buildin_getstrlen(struct script_state *st) {
  10410. char *str = conv_str(st,& (st->stack->stack_data[st->start+2]));
  10411. int len = (str) ? (int)strlen(str) : 0;
  10412. push_val(st->stack,C_INT,len);
  10413. return 0;
  10414. }
  10415. //=======================================================
  10416. // isalpha [Valaris]
  10417. //-------------------------------------------------------
  10418. int buildin_charisalpha(struct script_state *st) {
  10419. char *str=conv_str(st,& (st->stack->stack_data[st->start+2]));
  10420. int pos=conv_num(st,& (st->stack->stack_data[st->start+3]));
  10421. int val = ( str && pos>0 && (unsigned int)pos<strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10422. push_val(st->stack,C_INT,val);
  10423. return 0;
  10424. }
  10425. // [Lance]
  10426. int buildin_fakenpcname(struct script_state *st)
  10427. {
  10428. char *name;
  10429. char *newname;
  10430. int look;
  10431. name = conv_str(st,& (st->stack->stack_data[st->start+2]));
  10432. newname = conv_str(st,& (st->stack->stack_data[st->start+3]));
  10433. look = conv_num(st,& (st->stack->stack_data[st->start+4]));
  10434. if(look > 32767 || look < -32768) {
  10435. ShowError("buildin_fakenpcname: Invalid look value %d\n",look);
  10436. return 1; // Safety measure to prevent runtime errors
  10437. }
  10438. npc_changename(name,newname,(short)look);
  10439. return 0;
  10440. }
  10441. int buildin_atoi(struct script_state *st) {
  10442. char *value;
  10443. value = conv_str(st,& (st->stack->stack_data[st->start+2]));
  10444. push_val(st->stack, C_INT, atoi(value));
  10445. return 0;
  10446. }
  10447. //-----------------------------------------------------------------------//
  10448. // BRING STRSTR TO SCRIPTING ENGINE - LORDALFA START //
  10449. //-----------------------------------------------------------------------//
  10450. int buildin_compare(struct script_state *st) {
  10451. char *message;
  10452. char *cmpstring;
  10453. int j;
  10454. message = conv_str(st,& (st->stack->stack_data[st->start+2]));
  10455. cmpstring = conv_str(st,& (st->stack->stack_data[st->start+3]));
  10456. for (j = 0; message[j]; j++)
  10457. message[j] = TOLOWER(message[j]);
  10458. for (j = 0; cmpstring[j]; j++)
  10459. cmpstring[j] = TOLOWER(cmpstring[j]);
  10460. push_val(st->stack,C_INT,(strstr(message,cmpstring) != NULL));
  10461. return 0;
  10462. }
  10463. //-----------------------------------------------------------------------//
  10464. // BRING STRSTR TO SCRIPTING ENGINE - LORDALFA END //
  10465. //-----------------------------------------------------------------------//
  10466. // [zBuffer] List of mathematics commands --->
  10467. int buildin_sqrt(struct script_state *st){
  10468. double i, a;
  10469. i = conv_num(st, &(st->stack->stack_data[st->start+2]));
  10470. a = sqrt(i);
  10471. push_val(st->stack, C_INT, (int)a);
  10472. return 0;
  10473. }
  10474. int buildin_pow(struct script_state *st){
  10475. double i, a, b;
  10476. a = conv_num(st, &(st->stack->stack_data[st->start+2]));
  10477. b = conv_num(st, &(st->stack->stack_data[st->start+3]));
  10478. i = pow(a,b);
  10479. push_val(st->stack, C_INT, (int)i);
  10480. return 0;
  10481. }
  10482. int buildin_distance(struct script_state *st){
  10483. int x0, y0, x1, y1;
  10484. x0 = conv_num(st, &(st->stack->stack_data[st->start+2]));
  10485. y0 = conv_num(st, &(st->stack->stack_data[st->start+3]));
  10486. x1 = conv_num(st, &(st->stack->stack_data[st->start+4]));
  10487. y1 = conv_num(st, &(st->stack->stack_data[st->start+5]));
  10488. push_val(st->stack, C_INT, distance(x0-x1, y0-y1));
  10489. return 0;
  10490. }
  10491. int buildin_checkcell(struct script_state *st){
  10492. int m;
  10493. char *map = conv_str(st, &(st->stack->stack_data[st->start+2]));
  10494. m = mapindex_name2id(map);
  10495. if(m){
  10496. push_val(st->stack, C_INT, map_getcell(m, conv_num(st, &(st->stack->stack_data[st->start+3])), conv_num(st, &(st->stack->stack_data[st->start+4])),conv_num(st, &(st->stack->stack_data[st->start+5]))));
  10497. } else {
  10498. push_val(st->stack, C_INT, 0);
  10499. }
  10500. return 0;
  10501. }
  10502. // <--- [zBuffer] List of mathematics commands
  10503. // [zBuffer] List of dynamic var commands --->
  10504. void setd_sub(struct script_state *st, struct map_session_data *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10505. {
  10506. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10507. return;
  10508. }
  10509. int buildin_setd(struct script_state *st)
  10510. {
  10511. struct map_session_data *sd=NULL;
  10512. char varname[100], *buffer;
  10513. char *value;
  10514. int elem;
  10515. buffer = conv_str(st, & (st->stack->stack_data[st->start+2]));
  10516. value = conv_str(st, & (st->stack->stack_data[st->start+3]));
  10517. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10518. elem = 0;
  10519. if(st->rid)
  10520. sd = script_rid2sd(st);
  10521. if(varname[strlen(varname)-1] != '$') {
  10522. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10523. } else {
  10524. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10525. }
  10526. return 0;
  10527. }
  10528. int buildin_query_sql(struct script_state *st) {
  10529. #ifndef TXT_ONLY
  10530. char *name = NULL, *query;
  10531. int num, i = 0,j, nb_rows;
  10532. struct { char * dst_var_name; char type; } row[32];
  10533. struct map_session_data *sd = (st->rid)? script_rid2sd(st) : NULL;
  10534. query = conv_str(st,& (st->stack->stack_data[st->start+2]));
  10535. strcpy(tmp_sql, query);
  10536. if(mysql_query(&mmysql_handle,tmp_sql)){
  10537. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  10538. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  10539. push_val(st->stack,C_INT,0);
  10540. return 1;
  10541. }
  10542. // If some data was returned
  10543. if((sql_res = mysql_store_result(&mmysql_handle))){
  10544. // Count the number of rows to store
  10545. nb_rows = mysql_num_fields(sql_res);
  10546. // Can't store more row than variable
  10547. if (nb_rows > st->end - (st->start+3))
  10548. nb_rows = st->end - (st->start+3);
  10549. if (!nb_rows)
  10550. {
  10551. push_val(st->stack,C_INT,0);
  10552. return 0; // Nothing to store
  10553. }
  10554. if (nb_rows > 32)
  10555. {
  10556. ShowWarning("buildin_query_sql: too many rows!\n");
  10557. push_val(st->stack,C_INT,0);
  10558. return 1;
  10559. }
  10560. memset(row, 0, sizeof(row));
  10561. // Verify argument types
  10562. for(j=0; j < nb_rows; j++)
  10563. {
  10564. if(st->stack->stack_data[st->start+3+j].type != C_NAME){
  10565. ShowWarning("buildin_query_sql: Parameter %d is not a variable!\n", j);
  10566. push_val(st->stack,C_INT,0);
  10567. return 0;
  10568. } else {
  10569. // Store type of variable (string = 0/int = 1)
  10570. num=st->stack->stack_data[st->start+3+j].u.num;
  10571. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10572. if(name[strlen(name)-1] != '$') {
  10573. row[j].type = 1;
  10574. }
  10575. row[j].dst_var_name = name;
  10576. }
  10577. }
  10578. // Store data
  10579. while(i<128 && (sql_row = mysql_fetch_row(sql_res))){
  10580. for(j=0; j < nb_rows; j++)
  10581. {
  10582. if (row[j].type == 1)
  10583. setd_sub(st,sd, row[j].dst_var_name, i, (void *)atoi(sql_row[j]),st->stack->stack_data[st->start+3+j].ref);
  10584. else
  10585. setd_sub(st,sd, row[j].dst_var_name, i, (void *)sql_row[j],st->stack->stack_data[st->start+3+j].ref);
  10586. }
  10587. i++;
  10588. }
  10589. // Free data
  10590. mysql_free_result(sql_res);
  10591. }
  10592. push_val(st->stack, C_INT, i);
  10593. #else
  10594. //for TXT version, we always return -1
  10595. push_val(st->stack, C_INT, -1);
  10596. #endif
  10597. return 0;
  10598. }
  10599. //Allows escaping of a given string.
  10600. int buildin_escape_sql(struct script_state *st) {
  10601. char *t_query, *query;
  10602. query = conv_str(st,& (st->stack->stack_data[st->start+2]));
  10603. t_query = aMallocA((strlen(query)*2+1)*sizeof(char));
  10604. jstrescapecpy(t_query,query);
  10605. push_str(st->stack,C_STR,t_query);
  10606. return 0;
  10607. }
  10608. int buildin_getd (struct script_state *st)
  10609. {
  10610. char varname[100], *buffer;
  10611. //struct script_data dat;
  10612. int elem;
  10613. buffer = conv_str(st, & (st->stack->stack_data[st->start+2]));
  10614. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10615. elem = 0;
  10616. /*dat.type=C_NAME;
  10617. dat.u.num=add_str(varname)+(elem<<24);
  10618. get_val(st,&dat);
  10619. if(dat.type == C_INT)
  10620. push_val(st->stack, C_INT, dat.u.num);
  10621. else if(dat.type == C_CONSTSTR){
  10622. buffer = aStrdup((char *)dat.u.str);
  10623. // dat.u.str holds the actual pointer to the data, must be duplicated.
  10624. // It will be freed later. Tested.
  10625. push_str(st->stack, C_STR, buffer);
  10626. }*/
  10627. // Push the 'pointer' so it's more flexible [Lance]
  10628. push_val(st->stack,C_NAME,
  10629. (elem<<24) | add_str(varname));
  10630. return 0;
  10631. }
  10632. // <--- [zBuffer] List of dynamic var commands
  10633. // Pet stat [Lance]
  10634. int buildin_petstat(struct script_state *st){
  10635. struct map_session_data *sd = NULL;
  10636. struct pet_data *pd;
  10637. char *tmp;
  10638. int flag = conv_num(st, & (st->stack->stack_data[st->start+2]));
  10639. sd = script_rid2sd(st);
  10640. if(!sd || !sd->status.pet_id || !sd->pd){
  10641. if(flag == 2)
  10642. push_str(st->stack, C_CONSTSTR, "");
  10643. else
  10644. push_val(st->stack, C_INT, 0);
  10645. return 0;
  10646. }
  10647. pd = sd->pd;
  10648. switch(flag){
  10649. case 1:
  10650. push_val(st->stack, C_INT, (int)pd->pet.class_);
  10651. break;
  10652. case 2:
  10653. tmp = aStrdup(pd->pet.name);
  10654. push_str(st->stack, C_STR, tmp);
  10655. break;
  10656. case 3:
  10657. push_val(st->stack, C_INT, (int)pd->pet.level);
  10658. break;
  10659. case 4:
  10660. push_val(st->stack, C_INT, (int)pd->pet.hungry);
  10661. break;
  10662. case 5:
  10663. push_val(st->stack, C_INT, (int)pd->pet.intimate);
  10664. break;
  10665. default:
  10666. push_val(st->stack, C_INT, 0);
  10667. break;
  10668. }
  10669. return 0;
  10670. }
  10671. int buildin_callshop(struct script_state *st)
  10672. {
  10673. struct map_session_data *sd = NULL;
  10674. struct npc_data *nd;
  10675. char *shopname;
  10676. int flag = 0;
  10677. sd = script_rid2sd(st);
  10678. if (!sd) {
  10679. push_val(st->stack,C_INT,0);
  10680. return 0;
  10681. }
  10682. shopname = conv_str(st, & (st->stack->stack_data[st->start+2]));
  10683. if( st->end>st->start+3 )
  10684. flag = conv_num(st, & (st->stack->stack_data[st->start+3]));
  10685. nd = npc_name2id(shopname);
  10686. if (!nd || nd->bl.type!=BL_NPC || nd->bl.subtype!=SHOP) {
  10687. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)", shopname);
  10688. push_val(st->stack,C_INT,0);
  10689. return 1;
  10690. }
  10691. switch (flag) {
  10692. case 1: //Buy window
  10693. npc_buysellsel(sd,nd->bl.id,0);
  10694. break;
  10695. case 2: //Sell window
  10696. npc_buysellsel(sd,nd->bl.id,1);
  10697. break;
  10698. default: //Show menu
  10699. clif_npcbuysell(sd,nd->bl.id);
  10700. break;
  10701. }
  10702. sd->npc_shopid = nd->bl.id;
  10703. push_val(st->stack,C_INT,1);
  10704. return 0;
  10705. }
  10706. #ifndef MAX_SHOPITEM
  10707. #define MAX_SHOPITEM 100
  10708. #endif
  10709. int buildin_npcshopitem(struct script_state *st)
  10710. {
  10711. struct npc_data *nd= NULL;
  10712. int n = 0;
  10713. int i = 3;
  10714. int amount;
  10715. char* npcname = conv_str(st, & (st->stack->stack_data[st->start + 2]));
  10716. nd = npc_name2id(npcname);
  10717. if(!nd || nd->bl.subtype!=SHOP)
  10718. { //Not found.
  10719. push_val(st->stack,C_INT,0);
  10720. return 0;
  10721. }
  10722. // st->end - 2 = nameid + value # ... / 2 = number of items ... + 1 to end it
  10723. amount = ((st->end-2)/2)+1;
  10724. //Reinsert as realloc could change the pointer address.
  10725. map_deliddb(&nd->bl);
  10726. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10727. sizeof(nd->u.shop_item[0]) * amount);
  10728. map_addiddb(&nd->bl);
  10729. // Reset sell list.
  10730. memset(nd->u.shop_item, 0, sizeof(nd->u.shop_item[0]) * amount);
  10731. n = 0;
  10732. while (st->end > st->start + i) {
  10733. nd->u.shop_item[n].nameid = conv_num(st, & (st->stack->stack_data[st->start+i]));
  10734. i++;
  10735. nd->u.shop_item[n].value = conv_num(st, & (st->stack->stack_data[st->start+i]));
  10736. i++;
  10737. n++;
  10738. }
  10739. return 0;
  10740. }
  10741. int buildin_npcshopadditem(struct script_state *st) {
  10742. struct npc_data *nd=NULL;
  10743. int n = 0;
  10744. int i = 3;
  10745. int amount;
  10746. char* npcname = conv_str(st, & (st->stack->stack_data[st->start+2]));
  10747. nd = npc_name2id(npcname);
  10748. if (!nd || nd->bl.subtype!=SHOP)
  10749. { //Not found.
  10750. push_val(st->stack,C_INT,0);
  10751. return 0;
  10752. }
  10753. amount = ((st->end-2)/2)+1;
  10754. while (nd->u.shop_item[n].nameid && n < MAX_SHOPITEM)
  10755. n++;
  10756. //Reinsert as realloc could change the pointer address.
  10757. map_deliddb(&nd->bl);
  10758. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10759. sizeof(nd->u.shop_item[0]) * (amount+n));
  10760. map_addiddb(&nd->bl);
  10761. while(st->end > st->start + i){
  10762. nd->u.shop_item[n].nameid = conv_num(st, & (st->stack->stack_data[st->start+i]));
  10763. i++;
  10764. nd->u.shop_item[n].value = conv_num(st, & (st->stack->stack_data[st->start+i]));
  10765. i++;
  10766. n++;
  10767. }
  10768. // Marks the last of our stuff..
  10769. nd->u.shop_item[n].value = 0;
  10770. nd->u.shop_item[n].nameid = 0;
  10771. push_val(st->stack,C_INT,1);
  10772. return 0;
  10773. }
  10774. int buildin_npcshopdelitem(struct script_state *st)
  10775. {
  10776. struct npc_data *nd=NULL;
  10777. int n=0;
  10778. int i=3;
  10779. int size = 0;
  10780. char* npcname = conv_str(st, & (st->stack->stack_data[st->start+2]));
  10781. nd = npc_name2id(npcname);
  10782. if (!nd || nd->bl.subtype!=SHOP)
  10783. { //Not found.
  10784. push_val(st->stack,C_INT,0);
  10785. return 0;
  10786. }
  10787. while (nd->u.shop_item[size].nameid)
  10788. size++;
  10789. while (st->end > st->start+i) {
  10790. for(n=0;nd->u.shop_item[n].nameid && n < MAX_SHOPITEM;n++) {
  10791. if (nd->u.shop_item[n].nameid == conv_num(st, & (st->stack->stack_data[st->start+i]))) {
  10792. // We're moving 1 extra empty block. Junk data is eliminated later.
  10793. memmove(&nd->u.shop_item[n], &nd->u.shop_item[n+1], sizeof(nd->u.shop_item[0])*(size-n));
  10794. }
  10795. }
  10796. i++;
  10797. }
  10798. size = 0;
  10799. while (nd->u.shop_item[size].nameid)
  10800. size++;
  10801. //Reinsert as realloc could change the pointer address.
  10802. map_deliddb(&nd->bl);
  10803. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  10804. sizeof(nd->u.shop_item[0]) * (size+1));
  10805. map_addiddb(&nd->bl);
  10806. push_val(st->stack,C_INT,1);
  10807. return 0;
  10808. }
  10809. //Sets a script to attach to an npc.
  10810. int buildin_npcshopattach(struct script_state *st)
  10811. {
  10812. struct npc_data *nd=NULL;
  10813. char* npcname = conv_str(st, & (st->stack->stack_data[st->start+2]));
  10814. int flag = 1;
  10815. if( script_hasdata(st,2) )
  10816. flag = conv_num(st, script_getdata(st,2));
  10817. nd = npc_name2id(npcname);
  10818. if (!nd || nd->bl.subtype!=SHOP)
  10819. { //Not found.
  10820. push_val(st->stack,C_INT,0);
  10821. return 0;
  10822. }
  10823. if (flag)
  10824. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10825. else
  10826. nd->master_nd = NULL;
  10827. push_val(st->stack,C_INT,1);
  10828. return 0;
  10829. }
  10830. /*==========================================
  10831. * Returns some values of an item [Lupus]
  10832. * Price, Weight, etc...
  10833. setiteminfo(itemID,"{new item bonus script}");
  10834. *------------------------------------------
  10835. */
  10836. int buildin_setitemscript(struct script_state *st)
  10837. {
  10838. int item_id;
  10839. char *script;
  10840. struct item_data *i_data;
  10841. item_id = conv_num(st,& (st->stack->stack_data[st->start+2]));
  10842. script = conv_str(st,& (st->stack->stack_data[st->start+3]));
  10843. i_data = itemdb_exists(item_id);
  10844. if (i_data && script!=NULL && script[0]=='{') {
  10845. if(i_data->script!=NULL)
  10846. script_free_code(i_data->script);
  10847. i_data->script = parse_script(script, "script_setitemscript", 0, 0);
  10848. push_val(st->stack,C_INT,1);
  10849. } else
  10850. push_val(st->stack,C_INT,0);
  10851. return 0;
  10852. }
  10853. /* Work In Progress [Lupus]
  10854. int buildin_addmonsterdrop(struct script_state *st)
  10855. {
  10856. int class_,item_id,chance;
  10857. class_=conv_num(st,& (st->stack->stack_data[st->start+2]));
  10858. item_id=conv_num(st,& (st->stack->stack_data[st->start+3]));
  10859. chance=conv_num(st,& (st->stack->stack_data[st->start+4]));
  10860. if(class_>1000 && item_id>500 && chance>0) {
  10861. push_val(st->stack,C_INT,1);
  10862. } else {
  10863. push_val(st->stack,C_INT,0);
  10864. }
  10865. }
  10866. int buildin_delmonsterdrop(struct script_state *st)
  10867. {
  10868. int class_,item_id;
  10869. class_=conv_num(st,& (st->stack->stack_data[st->start+2]));
  10870. item_id=conv_num(st,& (st->stack->stack_data[st->start+3]));
  10871. if(class_>1000 && item_id>500) {
  10872. push_val(st->stack,C_INT,1);
  10873. } else {
  10874. push_val(st->stack,C_INT,0);
  10875. }
  10876. }
  10877. */
  10878. /*==========================================
  10879. * Returns some values of a monster [Lupus]
  10880. * Name, Level, race, size, etc...
  10881. getmonsterinfo(monsterID,queryIndex);
  10882. *------------------------------------------
  10883. */
  10884. int buildin_getmonsterinfo(struct script_state *st)
  10885. {
  10886. struct mob_db *mob;
  10887. int mob_id;
  10888. mob_id = conv_num(st,& (st->stack->stack_data[st->start+2]));
  10889. if (!mobdb_checkid(mob_id)) {
  10890. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i", mob_id);
  10891. push_val(st->stack, C_INT, -1);
  10892. return -1;
  10893. }
  10894. mob = mob_db(mob_id);
  10895. switch ( conv_num(st,& (st->stack->stack_data[st->start+3])) ) {
  10896. case 0: //Name
  10897. push_str(st->stack,C_CONSTSTR,mob->jname);
  10898. break;
  10899. case 1: //Lvl
  10900. push_val(st->stack,C_INT, mob->lv);
  10901. break;
  10902. case 2: //MaxHP
  10903. push_val(st->stack,C_INT, mob->status.max_hp);
  10904. break;
  10905. case 3: //Base EXP
  10906. push_val(st->stack,C_INT, mob->base_exp);
  10907. break;
  10908. case 4: //Job EXP
  10909. push_val(st->stack,C_INT, mob->job_exp);
  10910. break;
  10911. case 5: //Atk1
  10912. push_val(st->stack,C_INT, mob->status.rhw.atk);
  10913. break;
  10914. case 6: //Atk2
  10915. push_val(st->stack,C_INT, mob->status.rhw.atk2);
  10916. break;
  10917. case 7: //Def
  10918. push_val(st->stack,C_INT, mob->status.def);
  10919. break;
  10920. case 8: //Mdef
  10921. push_val(st->stack,C_INT, mob->status.mdef);
  10922. break;
  10923. case 9: //Str
  10924. push_val(st->stack,C_INT, mob->status.str);
  10925. break;
  10926. case 10: //Agi
  10927. push_val(st->stack,C_INT, mob->status.agi);
  10928. break;
  10929. case 11: //Vit
  10930. push_val(st->stack,C_INT, mob->status.vit);
  10931. break;
  10932. case 12: //Int
  10933. push_val(st->stack,C_INT, mob->status.int_);
  10934. break;
  10935. case 13: //Dex
  10936. push_val(st->stack,C_INT, mob->status.dex);
  10937. break;
  10938. case 14: //Luk
  10939. push_val(st->stack,C_INT, mob->status.luk);
  10940. break;
  10941. case 15: //Range
  10942. push_val(st->stack,C_INT, mob->status.rhw.range);
  10943. break;
  10944. case 16: //Range2
  10945. push_val(st->stack,C_INT, mob->range2);
  10946. break;
  10947. case 17: //Range3
  10948. push_val(st->stack,C_INT, mob->range3);
  10949. break;
  10950. case 18: //Size
  10951. push_val(st->stack,C_INT, mob->status.size);
  10952. break;
  10953. case 19: //Race
  10954. push_val(st->stack,C_INT, mob->status.race);
  10955. break;
  10956. case 20: //Element
  10957. push_val(st->stack,C_INT, mob->status.def_ele);
  10958. break;
  10959. case 21: //Mode
  10960. push_val(st->stack,C_INT, mob->status.mode);
  10961. break;
  10962. default: //wrong Index
  10963. push_val(st->stack,C_INT,-1);
  10964. }
  10965. return 0;
  10966. }
  10967. int buildin_checkvending(struct script_state *st){ // check vending [Nab4]
  10968. struct map_session_data *sd = NULL;
  10969. if(st->end > st->start + 2)
  10970. sd = map_nick2sd(conv_str(st,&st->stack->stack_data[st->start+2]));
  10971. else
  10972. sd = script_rid2sd(st);
  10973. if(sd)
  10974. push_val(st->stack, C_INT, (sd->vender_id != 0));
  10975. else
  10976. push_val(st->stack, C_INT, 0);
  10977. return 0;
  10978. }
  10979. int buildin_checkchatting(struct script_state *st){ // check chatting [Marka]
  10980. struct map_session_data *sd = NULL;
  10981. if(st->end > st->start + 2)
  10982. sd = map_nick2sd(conv_str(st,&st->stack->stack_data[st->start+2]));
  10983. else
  10984. sd = script_rid2sd(st);
  10985. if(sd)
  10986. push_val(st->stack, C_INT, (sd->chatID != 0));
  10987. else
  10988. push_val(st->stack, C_INT, 0);
  10989. return 0;
  10990. }
  10991. int axtoi(char *hexStg) {
  10992. int n = 0; // position in string
  10993. int m = 0; // position in digit[] to shift
  10994. int count; // loop index
  10995. int intValue = 0; // integer value of hex string
  10996. int digit[11]; // hold values to convert
  10997. while (n < 10) {
  10998. if (hexStg[n]=='\0')
  10999. break;
  11000. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11001. digit[n] = hexStg[n] & 0x0f; //convert to int
  11002. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11003. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11004. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11005. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11006. else break;
  11007. n++;
  11008. }
  11009. count = n;
  11010. m = n - 1;
  11011. n = 0;
  11012. while(n < count) {
  11013. // digit[n] is value of hex digit at position n
  11014. // (m << 2) is the number of positions to shift
  11015. // OR the bits into return value
  11016. intValue = intValue | (digit[n] << (m << 2));
  11017. m--; // adjust the position to set
  11018. n++; // next digit to process
  11019. }
  11020. return (intValue);
  11021. }
  11022. // [Lance] Hex string to integer converter
  11023. int buildin_axtoi(struct script_state *st)
  11024. {
  11025. char *hex = conv_str(st,& (st->stack->stack_data[st->start+2]));
  11026. push_val(st->stack, C_INT, axtoi(hex));
  11027. return 0;
  11028. }
  11029. // [zBuffer] List of player cont commands --->
  11030. int buildin_rid2name(struct script_state *st){
  11031. struct block_list *bl = NULL;
  11032. int rid = conv_num(st, & (st->stack->stack_data[st->start + 2]));
  11033. if((bl = map_id2bl(rid))){
  11034. switch(bl->type){
  11035. case BL_MOB:
  11036. push_str(st->stack,C_CONSTSTR,((TBL_MOB*)bl)->name);
  11037. break;
  11038. case BL_PC:
  11039. push_str(st->stack,C_CONSTSTR,((TBL_PC*)bl)->status.name);
  11040. break;
  11041. case BL_NPC:
  11042. push_str(st->stack,C_CONSTSTR,((TBL_NPC*)bl)->exname);
  11043. break;
  11044. case BL_PET:
  11045. push_str(st->stack,C_CONSTSTR,((TBL_PET*)bl)->pet.name);
  11046. break;
  11047. case BL_HOM:
  11048. push_str(st->stack,C_CONSTSTR,((TBL_HOM*)bl)->homunculus.name);
  11049. break;
  11050. default:
  11051. ShowError("buildin_rid2name: BL type unknown.\n");
  11052. push_str(st->stack,C_CONSTSTR,"");
  11053. break;
  11054. }
  11055. } else {
  11056. ShowError("buildin_rid2name: invalid RID\n");
  11057. push_str(st->stack,C_CONSTSTR,"(null)");
  11058. }
  11059. return 0;
  11060. }
  11061. int buildin_pcblockmove(struct script_state *st){
  11062. int id, flag;
  11063. struct map_session_data *sd = NULL;
  11064. id = conv_num(st, & (st->stack->stack_data[st->start + 2]));
  11065. flag = conv_num(st, & (st->stack->stack_data[st->start + 3]));
  11066. if(id)
  11067. sd = map_id2sd(id);
  11068. else
  11069. sd = script_rid2sd(st);
  11070. if(sd)
  11071. sd->state.blockedmove = flag > 0;
  11072. return 0;
  11073. }
  11074. int buildin_pcfollow(struct script_state *st) {
  11075. int id, targetid;
  11076. struct map_session_data *sd = NULL;
  11077. id = conv_num(st, & (st->stack->stack_data[st->start + 2]));
  11078. targetid = conv_num(st, & (st->stack->stack_data[st->start + 3]));
  11079. if(id)
  11080. sd = map_id2sd(id);
  11081. else
  11082. sd = script_rid2sd(st);
  11083. if(sd)
  11084. pc_follow(sd, targetid);
  11085. return 0;
  11086. }
  11087. int buildin_pcstopfollow(struct script_state *st) {
  11088. int id;
  11089. struct map_session_data *sd = NULL;
  11090. id = conv_num(st, & (st->stack->stack_data[st->start + 2]));
  11091. if(id)
  11092. sd = map_id2sd(id);
  11093. else
  11094. sd = script_rid2sd(st);
  11095. if(sd)
  11096. pc_stop_following(sd);
  11097. return 0;
  11098. }
  11099. // <--- [zBuffer] List of player cont commands
  11100. // [zBuffer] List of mob control commands --->
  11101. int buildin_mobspawn(struct script_state *st){
  11102. int class_,x,y,id;
  11103. char *str,*map;
  11104. // Who?
  11105. str =conv_str(st,& (st->stack->stack_data[st->start+2]));
  11106. // What?
  11107. class_ =conv_num(st,& (st->stack->stack_data[st->start+3]));
  11108. // Where?
  11109. map =conv_str(st,& (st->stack->stack_data[st->start+4]));
  11110. x =conv_num(st,& (st->stack->stack_data[st->start+5]));
  11111. y =conv_num(st,& (st->stack->stack_data[st->start+6]));
  11112. id = mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,1,"");
  11113. push_val(st->stack,C_INT,id);
  11114. return 0;
  11115. }
  11116. int buildin_mobremove(struct script_state *st) {
  11117. int id;
  11118. struct block_list *bl = NULL;
  11119. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11120. bl = map_id2bl(id);
  11121. if (bl && bl->type == BL_MOB)
  11122. unit_free(bl,0);
  11123. return 0;
  11124. }
  11125. int buildin_getmobdata(struct script_state *st) {
  11126. int num, id;
  11127. char *name;
  11128. struct mob_data *md = NULL;
  11129. struct map_session_data *sd = st->rid?map_id2sd(st->rid):NULL;
  11130. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11131. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || st->stack->stack_data[st->start+3].type!=C_NAME ){
  11132. ShowWarning("buildin_getmobdata: Error in argument!\n");
  11133. return -1;
  11134. }
  11135. num=st->stack->stack_data[st->start+3].u.num;
  11136. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  11137. setd_sub(st,sd,name,0,(void *)(int)md->class_,st->stack->stack_data[st->start+3].ref);
  11138. setd_sub(st,sd,name,1,(void *)(int)md->level,st->stack->stack_data[st->start+3].ref);
  11139. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,st->stack->stack_data[st->start+3].ref);
  11140. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,st->stack->stack_data[st->start+3].ref);
  11141. setd_sub(st,sd,name,4,(void *)(int)md->master_id,st->stack->stack_data[st->start+3].ref);
  11142. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,st->stack->stack_data[st->start+3].ref);
  11143. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,st->stack->stack_data[st->start+3].ref);
  11144. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,st->stack->stack_data[st->start+3].ref);
  11145. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,st->stack->stack_data[st->start+3].ref);
  11146. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,st->stack->stack_data[st->start+3].ref);
  11147. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,st->stack->stack_data[st->start+3].ref);
  11148. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,st->stack->stack_data[st->start+3].ref);
  11149. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,st->stack->stack_data[st->start+3].ref);
  11150. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,st->stack->stack_data[st->start+3].ref);
  11151. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,st->stack->stack_data[st->start+3].ref);
  11152. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,st->stack->stack_data[st->start+3].ref);
  11153. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,st->stack->stack_data[st->start+3].ref);
  11154. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,st->stack->stack_data[st->start+3].ref);
  11155. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,st->stack->stack_data[st->start+3].ref);
  11156. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,st->stack->stack_data[st->start+3].ref);
  11157. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,st->stack->stack_data[st->start+3].ref);
  11158. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,st->stack->stack_data[st->start+3].ref);
  11159. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,st->stack->stack_data[st->start+3].ref);
  11160. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,st->stack->stack_data[st->start+3].ref);
  11161. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,st->stack->stack_data[st->start+3].ref);
  11162. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,st->stack->stack_data[st->start+3].ref);
  11163. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, st->stack->stack_data[st->start+3].ref);
  11164. return 0;
  11165. }
  11166. int buildin_setmobdata(struct script_state *st){
  11167. int id, value, value2;
  11168. struct mob_data *md = NULL;
  11169. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11170. value = conv_num(st, & (st->stack->stack_data[st->start+3]));
  11171. value2 = conv_num(st, & (st->stack->stack_data[st->start+4]));
  11172. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB){
  11173. ShowWarning("buildin_setmobdata: Error in argument!\n");
  11174. return -1;
  11175. }
  11176. switch(value){
  11177. case 0:
  11178. md->class_ = (short)value2;
  11179. break;
  11180. case 1:
  11181. md->level = (unsigned short)value2;
  11182. break;
  11183. case 2:
  11184. md->status.hp = (unsigned int)value2;
  11185. break;
  11186. case 3:
  11187. md->status.max_hp = (unsigned int)value2;
  11188. break;
  11189. case 4:
  11190. md->master_id = value2;
  11191. break;
  11192. case 5:
  11193. md->bl.m = (short)value2;
  11194. break;
  11195. case 6:
  11196. md->bl.x = (short)value2;
  11197. break;
  11198. case 7:
  11199. md->bl.y = (short)value2;
  11200. break;
  11201. case 8:
  11202. md->status.speed = (unsigned short)value2;
  11203. break;
  11204. case 9:
  11205. md->status.mode = (unsigned short)value2;
  11206. break;
  11207. case 10:
  11208. md->special_state.ai = (unsigned int)value2;
  11209. break;
  11210. case 11:
  11211. md->sc.option = (unsigned short)value2;
  11212. break;
  11213. case 12:
  11214. md->vd->sex = (char)value2;
  11215. break;
  11216. case 13:
  11217. md->vd->class_ = (unsigned short)value2;
  11218. break;
  11219. case 14:
  11220. md->vd->hair_style = (unsigned short)value2;
  11221. break;
  11222. case 15:
  11223. md->vd->hair_color = (unsigned short)value2;
  11224. break;
  11225. case 16:
  11226. md->vd->head_bottom = (unsigned short)value2;
  11227. break;
  11228. case 17:
  11229. md->vd->head_mid = (unsigned short)value2;
  11230. break;
  11231. case 18:
  11232. md->vd->head_top = (unsigned short)value2;
  11233. break;
  11234. case 19:
  11235. md->vd->cloth_color = (unsigned short)value2;
  11236. break;
  11237. case 20:
  11238. md->vd->shield = (unsigned short)value2;
  11239. break;
  11240. case 21:
  11241. md->vd->weapon = (unsigned short)value2;
  11242. break;
  11243. case 22:
  11244. md->vd->shield = (unsigned short)value2;
  11245. break;
  11246. case 23:
  11247. md->ud.dir = (unsigned char)value2;
  11248. break;
  11249. case 24:
  11250. md->state.killer = value2>0?1:0;
  11251. break;
  11252. case 25:
  11253. md->callback_flag = (short)value2;
  11254. break;
  11255. case 26:
  11256. md->state.no_random_walk = value2>0?1:0;
  11257. break;
  11258. default:
  11259. ShowError("buildin_setmobdata: argument id is not identified.");
  11260. return -1;
  11261. }
  11262. return 0;
  11263. }
  11264. int buildin_mobassist(struct script_state *st) {
  11265. int id;
  11266. char *target;
  11267. struct mob_data *md = NULL;
  11268. struct block_list *bl = NULL;
  11269. struct unit_data *ud;
  11270. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11271. target = conv_str(st, & (st->stack->stack_data[st->start+3]));
  11272. if((bl =&(map_nick2sd(target)->bl)) || (bl = map_id2bl(atoi(target)))) {
  11273. md = (struct mob_data *)map_id2bl(id);
  11274. if(md && md->bl.type == BL_MOB) {
  11275. ud = unit_bl2ud(bl);
  11276. md->master_id = bl->id;
  11277. md->state.killer = 1;
  11278. mob_convertslave(md);
  11279. if (ud) {
  11280. if (ud->target)
  11281. md->target_id = ud->target;
  11282. else if (ud->skilltarget)
  11283. md->target_id = ud->skilltarget;
  11284. if(md->target_id)
  11285. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  11286. }
  11287. }
  11288. }
  11289. return 0;
  11290. }
  11291. int buildin_mobattach(struct script_state *st){
  11292. int id;
  11293. struct mob_data *md = NULL;
  11294. struct npc_data *nd = NULL;
  11295. char *npcname = NULL;
  11296. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11297. if(st->end > st->start + 3){
  11298. npcname = conv_str(st, & (st->stack->stack_data[st->start+3]));
  11299. }
  11300. if(npcname)
  11301. nd = npc_name2id(npcname);
  11302. else
  11303. nd = (struct npc_data *)map_id2bl(st->oid);
  11304. if(nd)
  11305. if((md = (struct mob_data *)map_id2bl(id)) && md->bl.type == BL_MOB)
  11306. md->nd = nd;
  11307. return 0;
  11308. }
  11309. int buildin_unitwalk(struct script_state *st){
  11310. int id,x,y = 0;
  11311. struct block_list *bl = NULL;
  11312. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11313. x = conv_num(st, & (st->stack->stack_data[st->start+3]));
  11314. if(st->end > st->start+4)
  11315. y = conv_num(st, & (st->stack->stack_data[st->start+4]));
  11316. bl = map_id2bl(id);
  11317. if(bl){
  11318. if(y)
  11319. push_val(st->stack,C_INT,unit_walktoxy(bl,x,y,0)); // We'll use harder calculations.
  11320. else
  11321. push_val(st->stack,C_INT,unit_walktobl(bl,map_id2bl(x),65025,1));
  11322. } else {
  11323. push_val(st->stack,C_INT,0);
  11324. }
  11325. return 0;
  11326. }
  11327. int buildin_unitkill(struct script_state *st){
  11328. struct block_list *bl = map_id2bl(conv_num(st, & (st->stack->stack_data[st->start+2])));
  11329. if(bl)
  11330. status_kill(bl);
  11331. return 0;
  11332. }
  11333. int buildin_unitwarp(struct script_state *st){
  11334. int id,x,y,m = 0;
  11335. char *map;
  11336. struct block_list *bl = NULL;
  11337. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11338. map = conv_str(st, & (st->stack->stack_data[st->start+3]));
  11339. x = conv_num(st, & (st->stack->stack_data[st->start+4]));
  11340. y = conv_num(st, & (st->stack->stack_data[st->start+5]));
  11341. bl = map_id2bl(id);
  11342. m = map_mapname2mapid(map);
  11343. if(m && bl){
  11344. push_val(st->stack,C_INT,unit_warp(bl, m, (short)x, (short)y, 0));
  11345. } else {
  11346. push_val(st->stack,C_INT,0);
  11347. }
  11348. return 0;
  11349. }
  11350. int buildin_unitattack(struct script_state *st) {
  11351. int id = 0, actiontype = 0;
  11352. char *target = NULL;
  11353. struct map_session_data *sd = NULL;
  11354. struct block_list *bl = NULL, *tbl = NULL;
  11355. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11356. target = conv_str(st, & (st->stack->stack_data[st->start+3]));
  11357. if(st->end > st->start + 4)
  11358. actiontype = conv_num(st, & (st->stack->stack_data[st->start+4]));
  11359. sd = map_nick2sd(target);
  11360. if(!sd)
  11361. tbl = map_id2bl(atoi(target));
  11362. else
  11363. tbl = &sd->bl;
  11364. if((bl = map_id2bl(id))){
  11365. switch (bl->type) {
  11366. case BL_PC:
  11367. clif_parse_ActionRequest_sub(((TBL_PC *)bl), actiontype > 0?0x07:0x00, tbl->id, gettick());
  11368. push_val(st->stack,C_INT,1);
  11369. return 0;
  11370. case BL_MOB:
  11371. ((TBL_MOB *)bl)->state.killer = 1;
  11372. ((TBL_MOB *)bl)->target_id = tbl->id;
  11373. break;
  11374. case BL_PET:
  11375. ((TBL_PET *)bl)->target_id = tbl->id;
  11376. break;
  11377. }
  11378. push_val(st->stack,C_INT,unit_walktobl(bl, tbl, 65025, 2));
  11379. } else {
  11380. push_val(st->stack,C_INT,0);
  11381. }
  11382. return 0;
  11383. }
  11384. int buildin_unitstop(struct script_state *st) {
  11385. int id;
  11386. struct block_list *bl = NULL;
  11387. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11388. bl = map_id2bl(id);
  11389. if(bl){
  11390. unit_stop_attack(bl);
  11391. unit_stop_walking(bl,0);
  11392. if(bl->type == BL_MOB)
  11393. ((TBL_MOB *)bl)->target_id = 0;
  11394. }
  11395. return 0;
  11396. }
  11397. int buildin_unittalk(struct script_state *st)
  11398. {
  11399. char *str;
  11400. int id;
  11401. char message[255];
  11402. struct block_list *bl = NULL;
  11403. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11404. str=conv_str(st,& (st->stack->stack_data[st->start+3]));
  11405. bl = map_id2bl(id);
  11406. if(bl) {
  11407. memcpy(message, status_get_name(bl), NAME_LENGTH);
  11408. strcat(message," : ");
  11409. strncat(message,str, 228); //Prevent overflow possibility. [Skotlex]
  11410. clif_message(bl, message);
  11411. if(bl->type == BL_PC)
  11412. clif_displaymessage(((TBL_PC*)bl)->fd, message);
  11413. }
  11414. return 0;
  11415. }
  11416. int buildin_unitemote(struct script_state *st) {
  11417. int id, emo;
  11418. struct block_list *bl= NULL;
  11419. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11420. emo = conv_num(st, & (st->stack->stack_data[st->start+3]));
  11421. if((bl = map_id2bl(id)))
  11422. clif_emotion(bl,emo);
  11423. return 0;
  11424. }
  11425. int buildin_unitdeadsit(struct script_state *st){
  11426. int id, action;
  11427. struct block_list *bl = NULL;
  11428. id = conv_num(st, & (st->stack->stack_data[st->start+2]));
  11429. action = conv_num(st, & (st->stack->stack_data[st->start+3]));
  11430. if((bl = map_id2bl(id))){
  11431. if(action > -1 && action < 4){
  11432. unsigned char buf[61] = "";
  11433. struct view_data *vd = status_get_viewdata(bl);
  11434. if (vd) vd->dead_sit = action;
  11435. WBUFW(buf, 0) = 0x8a;
  11436. WBUFL(buf, 2) = bl->id;
  11437. WBUFB(buf,26) = (unsigned char)action;
  11438. clif_send(buf, 61, bl, AREA);
  11439. }else {
  11440. ShowError("buildin_unitdeadsit: Invalid action.\n");
  11441. report_src(st);
  11442. }
  11443. }else{
  11444. ShowError("buildin_unitdeadsit: Target is not found.\n");
  11445. report_src(st);
  11446. }
  11447. return 0;
  11448. }
  11449. int buildin_unitskilluseid (struct script_state *st)
  11450. {
  11451. int id,skid,sklv;
  11452. struct block_list *bl = NULL;
  11453. id = conv_num(st,& (st->stack->stack_data[st->start+2]));
  11454. skid=conv_num(st,& (st->stack->stack_data[st->start+3]));
  11455. sklv=conv_num(st,& (st->stack->stack_data[st->start+4]));
  11456. if ((bl = map_id2bl(id)))
  11457. unit_skilluse_id(bl,(st->end>st->start+5)?conv_num(st,& (st->stack->stack_data[st->start+5])):bl->id,skid,sklv);
  11458. return 0;
  11459. }
  11460. int buildin_unitskillusepos(struct script_state *st)
  11461. {
  11462. int skid,sklv,x,y,id;
  11463. struct block_list *bl = NULL;
  11464. id = conv_num(st,& (st->stack->stack_data[st->start+2]));
  11465. skid=conv_num(st,& (st->stack->stack_data[st->start+3]));
  11466. sklv=conv_num(st,& (st->stack->stack_data[st->start+4]));
  11467. x=conv_num(st,& (st->stack->stack_data[st->start+5]));
  11468. y=conv_num(st,& (st->stack->stack_data[st->start+6]));
  11469. if ((bl=map_id2bl(id)))
  11470. unit_skilluse_pos(bl,x,y,skid,sklv);
  11471. return 0;
  11472. }
  11473. // <--- [zBuffer] List of mob control commands
  11474. // sleep <mili sec>
  11475. int buildin_sleep(struct script_state *st) {
  11476. int tick = conv_num(st,& (st->stack->stack_data[st->start+2]));
  11477. struct map_session_data *sd = map_id2sd(st->rid);
  11478. if(sd && sd->npc_id == st->oid) {
  11479. sd->npc_id = 0;
  11480. }
  11481. st->rid = 0;
  11482. if(tick <= 0) {
  11483. // 何もしない
  11484. } else if( !st->sleep.tick ) {
  11485. // 初回実行
  11486. st->state = RERUNLINE;
  11487. st->sleep.tick = tick;
  11488. } else {
  11489. // 続行
  11490. st->sleep.tick = 0;
  11491. }
  11492. return 0;
  11493. }
  11494. // sleep2 <mili sec>
  11495. int buildin_sleep2(struct script_state *st) {
  11496. int tick = conv_num(st,& (st->stack->stack_data[st->start+2]));
  11497. if( tick <= 0 ) {
  11498. // 0ms の待機時間を指定された
  11499. push_val(st->stack,C_INT,map_id2sd(st->rid) != NULL);
  11500. } else if( !st->sleep.tick ) {
  11501. // 初回実行時
  11502. st->state = RERUNLINE;
  11503. st->sleep.tick = tick;
  11504. } else {
  11505. push_val(st->stack,C_INT,map_id2sd(st->rid) != NULL);
  11506. st->sleep.tick = 0;
  11507. }
  11508. return 0;
  11509. }
  11510. /*==========================================
  11511. * 指定NPCの全てのsleepを再開する
  11512. *------------------------------------------
  11513. */
  11514. int buildin_awake(struct script_state *st)
  11515. {
  11516. struct npc_data *nd;
  11517. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11518. nd = npc_name2id(conv_str(st,& (st->stack->stack_data[st->start+2])));
  11519. if(nd == NULL)
  11520. return 0;
  11521. while( node ) {
  11522. if( (int)node->key == nd->bl.id) {
  11523. struct script_state *tst = node->data;
  11524. struct map_session_data *sd = map_id2sd(tst->rid);
  11525. if( tst->sleep.timer == -1 ) {
  11526. node = node->next;
  11527. continue;
  11528. }
  11529. if((sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11530. { //Cancel Execution
  11531. tst->state=END;
  11532. tst->rid = 0;
  11533. }
  11534. delete_timer(tst->sleep.timer, run_script_timer);
  11535. node = script_erase_sleepdb(node);
  11536. tst->sleep.timer = -1;
  11537. tst->sleep.tick = 0;
  11538. run_script_main(tst);
  11539. } else {
  11540. node = node->next;
  11541. }
  11542. }
  11543. return 0;
  11544. }
  11545. // getvariableofnpc(<param>, <npc name>);
  11546. int buildin_getvariableofnpc(struct script_state *st)
  11547. {
  11548. if( st->stack->stack_data[st->start+2].type != C_NAME ) {
  11549. // 第一引数が変数名じゃない
  11550. printf("getvariableofnpc: param not name\n");
  11551. push_val(st->stack,C_INT,0);
  11552. } else {
  11553. int num = st->stack->stack_data[st->start+2].u.num;
  11554. char *var_name = str_buf+str_data[num&0x00ffffff].str;
  11555. char *npc_name = conv_str(st,& (st->stack->stack_data[st->start+3]));
  11556. struct npc_data *nd = npc_name2id(npc_name);
  11557. if( var_name[0] != '.' || var_name[1] == '@' ) {
  11558. // ' 変数以外はダメ
  11559. printf("getvariableofnpc: invalid scope %s\n", var_name);
  11560. push_val(st->stack,C_INT,0);
  11561. } else if( nd == NULL || nd->bl.subtype != SCRIPT || !nd->u.scr.script) {
  11562. // NPC が見つからない or SCRIPT以外のNPC
  11563. printf("getvariableofnpc: can't find npc %s\n", npc_name);
  11564. push_val(st->stack,C_INT,0);
  11565. } else {
  11566. push_val2(st->stack,C_NAME,num, &nd->u.scr.script->script_vars );
  11567. }
  11568. }
  11569. return 0;
  11570. }
  11571. // [blackhole89] --->
  11572. // Set a warp portal.
  11573. int buildin_warpportal(struct script_state *st){
  11574. struct skill_unit_group *group;
  11575. unsigned short mapindex;
  11576. long spx,spy,tpx,tpy;
  11577. struct block_list *bl=map_id2bl(st->oid);
  11578. nullpo_retr(0,bl);
  11579. spx=conv_num(st, & (st->stack->stack_data[st->start+2]));
  11580. spy=conv_num(st, & (st->stack->stack_data[st->start+3]));
  11581. mapindex = mapindex_name2id((char*)conv_str(st,& (st->stack->stack_data[st->start+4])));
  11582. printf("mapindex: %d\n",mapindex);
  11583. tpx=conv_num(st, & (st->stack->stack_data[st->start+5]));
  11584. tpy=conv_num(st, & (st->stack->stack_data[st->start+6]));
  11585. if(!mapindex) return 0;
  11586. if((group=skill_unitsetting(bl,AL_WARP,4,spx,spy,1))==NULL) {
  11587. return 0;
  11588. }
  11589. group->val2=(tpx<<16)|tpy;
  11590. group->val3 = mapindex;
  11591. return 0;
  11592. }
  11593. // <-- [blackhole89]