script.c 412 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. //#define DEBUG_DUMP_STACK
  8. #include "../common/cbasetypes.h"
  9. #include "../common/malloc.h"
  10. #include "../common/md5calc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/random.h"
  14. #include "../common/showmsg.h"
  15. #include "../common/strlib.h"
  16. #include "../common/timer.h"
  17. #include "../common/utils.h"
  18. #include "map.h"
  19. #include "path.h"
  20. #include "clif.h"
  21. #include "chrif.h"
  22. #include "itemdb.h"
  23. #include "pc.h"
  24. #include "status.h"
  25. #include "storage.h"
  26. #include "mob.h"
  27. #include "npc.h"
  28. #include "pet.h"
  29. #include "mapreg.h"
  30. #include "homunculus.h"
  31. #include "instance.h"
  32. #include "mercenary.h"
  33. #include "intif.h"
  34. #include "skill.h"
  35. #include "status.h"
  36. #include "chat.h"
  37. #include "battle.h"
  38. #include "battleground.h"
  39. #include "party.h"
  40. #include "guild.h"
  41. #include "atcommand.h"
  42. #include "log.h"
  43. #include "unit.h"
  44. #include "pet.h"
  45. #include "mail.h"
  46. #include "script.h"
  47. #include "quest.h"
  48. #include "elemental.h"
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <math.h>
  53. #ifndef WIN32
  54. #include <sys/time.h>
  55. #endif
  56. #include <time.h>
  57. #include <setjmp.h>
  58. #include <errno.h>
  59. ///////////////////////////////////////////////////////////////////////////////
  60. //## TODO possible enhancements: [FlavioJS]
  61. // - 'callfunc' supporting labels in the current npc "::LabelName"
  62. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  63. // - 'function FuncName;' function declarations reverting to global functions
  64. // if local label isn't found
  65. // - join callfunc and callsub's functionality
  66. // - remove dynamic allocation in add_word()
  67. // - remove GETVALUE / SETVALUE
  68. // - clean up the set_reg / set_val / setd_sub mess
  69. // - detect invalid label references at parse-time
  70. //
  71. // struct script_state* st;
  72. //
  73. /// Returns the script_data at the target index
  74. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  75. /// Returns if the stack contains data at the target index
  76. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  77. /// Returns the index of the last data in the stack
  78. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  79. /// Pushes an int into the stack
  80. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  81. /// Pushes a string into the stack (script engine frees it automatically)
  82. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  83. /// Pushes a copy of a string into the stack
  84. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  85. /// Pushes a constant string into the stack (must never change or be freed)
  86. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  87. /// Pushes a nil into the stack
  88. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  89. /// Pushes a copy of the data in the target index
  90. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  91. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  92. #define script_isint(st,i) data_isint(script_getdata(st,i))
  93. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  94. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  95. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  96. // Note: "top" functions/defines use indexes relative to the top of the stack
  97. // -1 is the index of the data at the top
  98. /// Returns the script_data at the target index relative to the top of the stack
  99. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  100. /// Pushes a copy of the data in the target index relative to the top of the stack
  101. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  102. /// Removes the range of values [start,end[ relative to the top of the stack
  103. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  104. //
  105. // struct script_data* data;
  106. //
  107. /// Returns if the script data is a string
  108. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  109. /// Returns if the script data is an int
  110. #define data_isint(data) ( (data)->type == C_INT )
  111. /// Returns if the script data is a reference
  112. #define data_isreference(data) ( (data)->type == C_NAME )
  113. /// Returns if the script data is a label
  114. #define data_islabel(data) ( (data)->type == C_POS )
  115. /// Returns if the script data is an internal script function label
  116. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  117. /// Returns if this is a reference to a constant
  118. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  119. /// Returns if this a reference to a param
  120. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  121. /// Returns if this a reference to a variable
  122. //##TODO confirm it's C_NAME [FlavioJS]
  123. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  124. /// Returns the unique id of the reference (id and index)
  125. #define reference_getuid(data) ( (data)->u.num )
  126. /// Returns the id of the reference
  127. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  128. /// Returns the array index of the reference
  129. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  130. /// Returns the name of the reference
  131. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  132. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  133. #define reference_getref(data) ( (data)->ref )
  134. /// Returns the value of the constant
  135. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  136. /// Returns the type of param
  137. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  138. /// Composes the uid of a reference from the id and the index
  139. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  140. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  141. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  142. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  143. #define FETCH(n, t) \
  144. if( script_hasdata(st,n) ) \
  145. (t)=script_getnum(st,n);
  146. /// Maximum amount of elements in script arrays
  147. #define SCRIPT_MAX_ARRAYSIZE 128
  148. #define SCRIPT_BLOCK_SIZE 512
  149. enum { LABEL_NEXTLINE=1,LABEL_START };
  150. /// temporary buffer for passing around compiled bytecode
  151. /// @see add_scriptb, set_label, parse_script
  152. static unsigned char* script_buf = NULL;
  153. static int script_pos = 0, script_size = 0;
  154. static inline int GETVALUE(const unsigned char* buf, int i)
  155. {
  156. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  157. }
  158. static inline void SETVALUE(unsigned char* buf, int i, int n)
  159. {
  160. buf[i] = GetByte(n, 0);
  161. buf[i+1] = GetByte(n, 1);
  162. buf[i+2] = GetByte(n, 2);
  163. }
  164. // String buffer structures.
  165. // str_data stores string information
  166. static struct str_data_struct {
  167. enum c_op type;
  168. int str;
  169. int backpatch;
  170. int label;
  171. int (*func)(struct script_state *st);
  172. int val;
  173. int next;
  174. } *str_data = NULL;
  175. static int str_data_size = 0; // size of the data
  176. static int str_num = LABEL_START; // next id to be assigned
  177. // str_buf holds the strings themselves
  178. static char *str_buf;
  179. static int str_size = 0; // size of the buffer
  180. static int str_pos = 0; // next position to be assigned
  181. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  182. #define SCRIPT_HASH_SIZE 1021
  183. int str_hash[SCRIPT_HASH_SIZE];
  184. // Specifies which string hashing method to use
  185. //#define SCRIPT_HASH_DJB2
  186. //#define SCRIPT_HASH_SDBM
  187. #define SCRIPT_HASH_ELF
  188. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  189. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  190. static int parse_options=0;
  191. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  192. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  193. // important buildin function references for usage in scripts
  194. static int buildin_set_ref = 0;
  195. static int buildin_callsub_ref = 0;
  196. static int buildin_callfunc_ref = 0;
  197. static int buildin_getelementofarray_ref = 0;
  198. // Caches compiled autoscript item code.
  199. // Note: This is not cleared when reloading itemdb.
  200. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  201. struct Script_Config script_config = {
  202. 1, // warn_func_mismatch_argtypes
  203. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  204. 0, INT_MAX, // input_min_value/input_max_value
  205. "OnPCDieEvent", //die_event_name
  206. "OnPCKillEvent", //kill_pc_event_name
  207. "OnNPCKillEvent", //kill_mob_event_name
  208. "OnPCLoginEvent", //login_event_name
  209. "OnPCLogoutEvent", //logout_event_name
  210. "OnPCLoadMapEvent", //loadmap_event_name
  211. "OnPCBaseLvUpEvent", //baselvup_event_name
  212. "OnPCJobLvUpEvent", //joblvup_event_name
  213. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  214. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  215. };
  216. static jmp_buf error_jump;
  217. static char* error_msg;
  218. static const char* error_pos;
  219. static int error_report; // if the error should produce output
  220. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  221. // [Eoe / jA 1080, 1081, 1094, 1164]
  222. enum curly_type {
  223. TYPE_NULL = 0,
  224. TYPE_IF,
  225. TYPE_SWITCH,
  226. TYPE_WHILE,
  227. TYPE_FOR,
  228. TYPE_DO,
  229. TYPE_USERFUNC,
  230. TYPE_ARGLIST // function argument list
  231. };
  232. enum e_arglist
  233. {
  234. ARGLIST_UNDEFINED = 0,
  235. ARGLIST_NO_PAREN = 1,
  236. ARGLIST_PAREN = 2,
  237. };
  238. static struct {
  239. struct {
  240. enum curly_type type;
  241. int index;
  242. int count;
  243. int flag;
  244. struct linkdb_node *case_label;
  245. } curly[256]; // 右カッコの情報
  246. int curly_count; // 右カッコの数
  247. int index; // スクリプト内で使用した構文の数
  248. } syntax;
  249. const char* parse_curly_close(const char* p);
  250. const char* parse_syntax_close(const char* p);
  251. const char* parse_syntax_close_sub(const char* p,int* flag);
  252. const char* parse_syntax(const char* p);
  253. static int parse_syntax_for_flag = 0;
  254. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  255. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  256. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  257. int potion_target=0;
  258. c_op get_com(unsigned char *script,int *pos);
  259. int get_num(unsigned char *script,int *pos);
  260. typedef struct script_function {
  261. int (*func)(struct script_state *st);
  262. const char *name;
  263. const char *arg;
  264. } script_function;
  265. extern script_function buildin_func[];
  266. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  267. /*==========================================
  268. * ローカルプロトタイプ宣言 (必要な物のみ)
  269. *------------------------------------------*/
  270. const char* parse_subexpr(const char* p,int limit);
  271. int run_func(struct script_state *st);
  272. enum {
  273. MF_NOMEMO, //0
  274. MF_NOTELEPORT,
  275. MF_NOSAVE,
  276. MF_NOBRANCH,
  277. MF_NOPENALTY,
  278. MF_NOZENYPENALTY,
  279. MF_PVP,
  280. MF_PVP_NOPARTY,
  281. MF_PVP_NOGUILD,
  282. MF_GVG,
  283. MF_GVG_NOPARTY, //10
  284. MF_NOTRADE,
  285. MF_NOSKILL,
  286. MF_NOWARP,
  287. MF_PARTYLOCK,
  288. MF_NOICEWALL,
  289. MF_SNOW,
  290. MF_FOG,
  291. MF_SAKURA,
  292. MF_LEAVES,
  293. /**
  294. * No longer available, keeping here just in case it's back someday. [Ind]
  295. **/
  296. //MF_RAIN, //20
  297. // 21 free
  298. MF_NOGO = 22,
  299. MF_CLOUDS,
  300. MF_CLOUDS2,
  301. MF_FIREWORKS,
  302. MF_GVG_CASTLE,
  303. MF_GVG_DUNGEON,
  304. MF_NIGHTENABLED,
  305. MF_NOBASEEXP,
  306. MF_NOJOBEXP, //30
  307. MF_NOMOBLOOT,
  308. MF_NOMVPLOOT,
  309. MF_NORETURN,
  310. MF_NOWARPTO,
  311. MF_NIGHTMAREDROP,
  312. MF_RESTRICTED,
  313. MF_NOCOMMAND,
  314. MF_NODROP,
  315. MF_JEXP,
  316. MF_BEXP, //40
  317. MF_NOVENDING,
  318. MF_LOADEVENT,
  319. MF_NOCHAT,
  320. MF_NOEXPPENALTY,
  321. MF_GUILDLOCK,
  322. MF_TOWN,
  323. MF_AUTOTRADE,
  324. MF_ALLOWKS,
  325. MF_MONSTER_NOTELEPORT,
  326. MF_PVP_NOCALCRANK, //50
  327. MF_BATTLEGROUND,
  328. MF_RESET
  329. };
  330. const char* script_op2name(int op)
  331. {
  332. #define RETURN_OP_NAME(type) case type: return #type
  333. switch( op )
  334. {
  335. RETURN_OP_NAME(C_NOP);
  336. RETURN_OP_NAME(C_POS);
  337. RETURN_OP_NAME(C_INT);
  338. RETURN_OP_NAME(C_PARAM);
  339. RETURN_OP_NAME(C_FUNC);
  340. RETURN_OP_NAME(C_STR);
  341. RETURN_OP_NAME(C_CONSTSTR);
  342. RETURN_OP_NAME(C_ARG);
  343. RETURN_OP_NAME(C_NAME);
  344. RETURN_OP_NAME(C_EOL);
  345. RETURN_OP_NAME(C_RETINFO);
  346. RETURN_OP_NAME(C_USERFUNC);
  347. RETURN_OP_NAME(C_USERFUNC_POS);
  348. // operators
  349. RETURN_OP_NAME(C_OP3);
  350. RETURN_OP_NAME(C_LOR);
  351. RETURN_OP_NAME(C_LAND);
  352. RETURN_OP_NAME(C_LE);
  353. RETURN_OP_NAME(C_LT);
  354. RETURN_OP_NAME(C_GE);
  355. RETURN_OP_NAME(C_GT);
  356. RETURN_OP_NAME(C_EQ);
  357. RETURN_OP_NAME(C_NE);
  358. RETURN_OP_NAME(C_XOR);
  359. RETURN_OP_NAME(C_OR);
  360. RETURN_OP_NAME(C_AND);
  361. RETURN_OP_NAME(C_ADD);
  362. RETURN_OP_NAME(C_SUB);
  363. RETURN_OP_NAME(C_MUL);
  364. RETURN_OP_NAME(C_DIV);
  365. RETURN_OP_NAME(C_MOD);
  366. RETURN_OP_NAME(C_NEG);
  367. RETURN_OP_NAME(C_LNOT);
  368. RETURN_OP_NAME(C_NOT);
  369. RETURN_OP_NAME(C_R_SHIFT);
  370. RETURN_OP_NAME(C_L_SHIFT);
  371. default:
  372. ShowDebug("script_op2name: unexpected op=%d\n", op);
  373. return "???";
  374. }
  375. #undef RETURN_OP_NAME
  376. }
  377. #ifdef DEBUG_DUMP_STACK
  378. static void script_dump_stack(struct script_state* st)
  379. {
  380. int i;
  381. ShowMessage("\tstart = %d\n", st->start);
  382. ShowMessage("\tend = %d\n", st->end);
  383. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  384. ShowMessage("\tsp = %d\n", st->stack->sp);
  385. for( i = 0; i < st->stack->sp; ++i )
  386. {
  387. struct script_data* data = &st->stack->stack_data[i];
  388. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  389. switch( data->type )
  390. {
  391. case C_INT:
  392. case C_POS:
  393. ShowMessage(" %d\n", data->u.num);
  394. break;
  395. case C_STR:
  396. case C_CONSTSTR:
  397. ShowMessage(" \"%s\"\n", data->u.str);
  398. break;
  399. case C_NAME:
  400. ShowMessage(" \"%s\" (id=%d ref=%p subtype=%s)\n", reference_getname(data), data->u.num, data->ref, script_op2name(str_data[data->u.num].type));
  401. break;
  402. case C_RETINFO:
  403. {
  404. struct script_retinfo* ri = data->u.ri;
  405. ShowMessage(" %p {var_function=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->var_function, ri->script, ri->pos, ri->nargs, ri->defsp);
  406. }
  407. break;
  408. default:
  409. ShowMessage("\n");
  410. break;
  411. }
  412. }
  413. }
  414. #endif
  415. /// Reports on the console the src of a script error.
  416. static void script_reportsrc(struct script_state *st)
  417. {
  418. struct block_list* bl;
  419. if( st->oid == 0 )
  420. return; //Can't report source.
  421. bl = map_id2bl(st->oid);
  422. if( bl == NULL )
  423. return;
  424. switch( bl->type )
  425. {
  426. case BL_NPC:
  427. if( bl->m >= 0 )
  428. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  429. else
  430. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  431. break;
  432. default:
  433. if( bl->m >= 0 )
  434. 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);
  435. else
  436. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  437. break;
  438. }
  439. }
  440. /// Reports on the console information about the script data.
  441. static void script_reportdata(struct script_data* data)
  442. {
  443. if( data == NULL )
  444. return;
  445. switch( data->type )
  446. {
  447. case C_NOP:// no value
  448. ShowDebug("Data: nothing (nil)\n");
  449. break;
  450. case C_INT:// number
  451. ShowDebug("Data: number value=%d\n", data->u.num);
  452. break;
  453. case C_STR:
  454. case C_CONSTSTR:// string
  455. if( data->u.str )
  456. {
  457. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  458. }
  459. else
  460. {
  461. ShowDebug("Data: string value=NULL\n");
  462. }
  463. break;
  464. case C_NAME:// reference
  465. if( reference_tovariable(data) )
  466. {// variable
  467. const char* name = reference_getname(data);
  468. if( not_array_variable(*name) )
  469. ShowDebug("Data: variable name='%s'\n", name);
  470. else
  471. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  472. }
  473. else if( reference_toconstant(data) )
  474. {// constant
  475. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  476. }
  477. else if( reference_toparam(data) )
  478. {// param
  479. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  480. }
  481. else
  482. {// ???
  483. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  484. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  485. }
  486. break;
  487. case C_POS:// label
  488. ShowDebug("Data: label pos=%d\n", data->u.num);
  489. break;
  490. default:
  491. ShowDebug("Data: %s\n", script_op2name(data->type));
  492. break;
  493. }
  494. }
  495. /// Reports on the console information about the current built-in function.
  496. static void script_reportfunc(struct script_state* st)
  497. {
  498. int i, params, id;
  499. struct script_data* data;
  500. if( !script_hasdata(st,0) )
  501. {// no stack
  502. return;
  503. }
  504. data = script_getdata(st,0);
  505. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  506. {// script currently not executing a built-in function or corrupt stack
  507. return;
  508. }
  509. id = reference_getid(data);
  510. params = script_lastdata(st)-1;
  511. if( params > 0 )
  512. {
  513. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  514. for( i = 2; i <= script_lastdata(st); i++ )
  515. {
  516. script_reportdata(script_getdata(st,i));
  517. }
  518. }
  519. else
  520. {
  521. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  522. }
  523. }
  524. /*==========================================
  525. * エラーメッセージ出力
  526. *------------------------------------------*/
  527. static void disp_error_message2(const char *mes,const char *pos,int report)
  528. {
  529. error_msg = aStrdup(mes);
  530. error_pos = pos;
  531. error_report = report;
  532. longjmp( error_jump, 1 );
  533. }
  534. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  535. /// Checks event parameter validity
  536. static void check_event(struct script_state *st, const char *evt)
  537. {
  538. if( evt && evt[0] && !stristr(evt, "::On") )
  539. {
  540. if( npc_event_isspecial(evt) )
  541. {
  542. ; // portable small/large monsters or other attributes
  543. }
  544. else
  545. {
  546. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  547. script_reportsrc(st);
  548. }
  549. }
  550. }
  551. /*==========================================
  552. * Hashes the input string
  553. *------------------------------------------*/
  554. static unsigned int calc_hash(const char* p)
  555. {
  556. unsigned int h;
  557. #if defined(SCRIPT_HASH_DJB2)
  558. h = 5381;
  559. while( *p ) // hash*33 + c
  560. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  561. #elif defined(SCRIPT_HASH_SDBM)
  562. h = 0;
  563. while( *p ) // hash*65599 + c
  564. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  565. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  566. h = 0;
  567. while( *p ){
  568. unsigned int g;
  569. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  570. g = h & 0xF0000000;
  571. if( g )
  572. {
  573. h ^= g >> 24;
  574. h &= ~g;
  575. }
  576. }
  577. #else // athena hash
  578. h = 0;
  579. while( *p )
  580. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  581. #endif
  582. return h % SCRIPT_HASH_SIZE;
  583. }
  584. /*==========================================
  585. * str_data manipulation functions
  586. *------------------------------------------*/
  587. /// Looks up string using the provided id.
  588. const char* get_str(int id)
  589. {
  590. Assert( id >= LABEL_START && id < str_size );
  591. return str_buf+str_data[id].str;
  592. }
  593. /// Returns the uid of the string, or -1.
  594. static int search_str(const char* p)
  595. {
  596. int i;
  597. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  598. if( strcasecmp(get_str(i),p) == 0 )
  599. return i;
  600. return -1;
  601. }
  602. /// Stores a copy of the string and returns its id.
  603. /// If an identical string is already present, returns its id instead.
  604. int add_str(const char* p)
  605. {
  606. int i, h;
  607. int len;
  608. h = calc_hash(p);
  609. if( str_hash[h] == 0 )
  610. {// empty bucket, add new node here
  611. str_hash[h] = str_num;
  612. }
  613. else
  614. {// scan for end of list, or occurence of identical string
  615. for( i = str_hash[h]; ; i = str_data[i].next )
  616. {
  617. if( strcasecmp(get_str(i),p) == 0 )
  618. return i; // string already in list
  619. if( str_data[i].next == 0 )
  620. break; // reached the end
  621. }
  622. // append node to end of list
  623. str_data[i].next = str_num;
  624. }
  625. // grow list if neccessary
  626. if( str_num >= str_data_size )
  627. {
  628. str_data_size += 128;
  629. RECREATE(str_data,struct str_data_struct,str_data_size);
  630. memset(str_data + (str_data_size - 128), '\0', 128);
  631. }
  632. len=(int)strlen(p);
  633. // grow string buffer if neccessary
  634. while( str_pos+len+1 >= str_size )
  635. {
  636. str_size += 256;
  637. RECREATE(str_buf,char,str_size);
  638. memset(str_buf + (str_size - 256), '\0', 256);
  639. }
  640. safestrncpy(str_buf+str_pos, p, len+1);
  641. str_data[str_num].type = C_NOP;
  642. str_data[str_num].str = str_pos;
  643. str_data[str_num].next = 0;
  644. str_data[str_num].func = NULL;
  645. str_data[str_num].backpatch = -1;
  646. str_data[str_num].label = -1;
  647. str_pos += len+1;
  648. return str_num++;
  649. }
  650. /// Appends 1 byte to the script buffer.
  651. static void add_scriptb(int a)
  652. {
  653. if( script_pos+1 >= script_size )
  654. {
  655. script_size += SCRIPT_BLOCK_SIZE;
  656. RECREATE(script_buf,unsigned char,script_size);
  657. }
  658. script_buf[script_pos++] = (uint8)(a);
  659. }
  660. /// Appends a c_op value to the script buffer.
  661. /// The value is variable-length encoded into 8-bit blocks.
  662. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  663. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  664. static void add_scriptc(int a)
  665. {
  666. while( a >= 0x40 )
  667. {
  668. add_scriptb((a&0x3f)|0x40);
  669. a = (a - 0x40) >> 6;
  670. }
  671. add_scriptb(a);
  672. }
  673. /// Appends an integer value to the script buffer.
  674. /// The value is variable-length encoded into 8-bit blocks.
  675. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  676. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  677. static void add_scripti(int a)
  678. {
  679. while( a >= 0x40 )
  680. {
  681. add_scriptb((a&0x3f)|0xc0);
  682. a = (a - 0x40) >> 6;
  683. }
  684. add_scriptb(a|0x80);
  685. }
  686. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  687. ///
  688. /// @param l The id of the str_data entry
  689. // 最大16Mまで
  690. static void add_scriptl(int l)
  691. {
  692. int backpatch = str_data[l].backpatch;
  693. switch(str_data[l].type){
  694. case C_POS:
  695. case C_USERFUNC_POS:
  696. add_scriptc(C_POS);
  697. add_scriptb(str_data[l].label);
  698. add_scriptb(str_data[l].label>>8);
  699. add_scriptb(str_data[l].label>>16);
  700. break;
  701. case C_NOP:
  702. case C_USERFUNC:
  703. // ラベルの可能性があるのでbackpatch用データ埋め込み
  704. add_scriptc(C_NAME);
  705. str_data[l].backpatch = script_pos;
  706. add_scriptb(backpatch);
  707. add_scriptb(backpatch>>8);
  708. add_scriptb(backpatch>>16);
  709. break;
  710. case C_INT:
  711. add_scripti(abs(str_data[l].val));
  712. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  713. add_scriptc(C_NEG);
  714. break;
  715. default: // assume C_NAME
  716. add_scriptc(C_NAME);
  717. add_scriptb(l);
  718. add_scriptb(l>>8);
  719. add_scriptb(l>>16);
  720. break;
  721. }
  722. }
  723. /*==========================================
  724. * ラベルを解決する
  725. *------------------------------------------*/
  726. void set_label(int l,int pos, const char* script_pos)
  727. {
  728. int i,next;
  729. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  730. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  731. disp_error_message("set_label: invalid label name",script_pos);
  732. return;
  733. }
  734. if(str_data[l].label!=-1){
  735. disp_error_message("set_label: dup label ",script_pos);
  736. return;
  737. }
  738. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  739. str_data[l].label=pos;
  740. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  741. next=GETVALUE(script_buf,i);
  742. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  743. SETVALUE(script_buf,i,pos);
  744. i=next;
  745. }
  746. }
  747. /// Skips spaces and/or comments.
  748. const char* skip_space(const char* p)
  749. {
  750. if( p == NULL )
  751. return NULL;
  752. for(;;)
  753. {
  754. while( ISSPACE(*p) )
  755. ++p;
  756. if( *p == '/' && p[1] == '/' )
  757. {// line comment
  758. while(*p && *p!='\n')
  759. ++p;
  760. }
  761. else if( *p == '/' && p[1] == '*' )
  762. {// block comment
  763. p += 2;
  764. for(;;)
  765. {
  766. if( *p == '\0' )
  767. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  768. if( *p == '*' && p[1] == '/' )
  769. {// end of block comment
  770. p += 2;
  771. break;
  772. }
  773. ++p;
  774. }
  775. }
  776. else
  777. break;
  778. }
  779. return p;
  780. }
  781. /// Skips a word.
  782. /// A word consists of undercores and/or alfanumeric characters,
  783. /// and valid variable prefixes/postfixes.
  784. static
  785. const char* skip_word(const char* p)
  786. {
  787. // prefix
  788. switch( *p )
  789. {
  790. case '@':// temporary char variable
  791. ++p; break;
  792. case '#':// account variable
  793. p += ( p[1] == '#' ? 2 : 1 ); break;
  794. case '\'':// instance variable
  795. ++p; break;
  796. case '.':// npc variable
  797. p += ( p[1] == '@' ? 2 : 1 ); break;
  798. case '$':// global variable
  799. p += ( p[1] == '@' ? 2 : 1 ); break;
  800. }
  801. while( ISALNUM(*p) || *p == '_' )
  802. ++p;
  803. // postfix
  804. if( *p == '$' )// string
  805. p++;
  806. return p;
  807. }
  808. /// Adds a word to str_data.
  809. /// @see skip_word
  810. /// @see add_str
  811. static
  812. int add_word(const char* p)
  813. {
  814. char* word;
  815. int len;
  816. int i;
  817. // Check for a word
  818. len = skip_word(p) - p;
  819. if( len == 0 )
  820. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  821. // Duplicate the word
  822. word = (char*)aMalloc(len+1);
  823. memcpy(word, p, len);
  824. word[len] = 0;
  825. // add the word
  826. i = add_str(word);
  827. aFree(word);
  828. return i;
  829. }
  830. /// Parses a function call.
  831. /// The argument list can have parenthesis or not.
  832. /// The number of arguments is checked.
  833. static
  834. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  835. {
  836. const char* p2;
  837. const char* arg=NULL;
  838. int func;
  839. func = add_word(p);
  840. if( str_data[func].type == C_FUNC ){
  841. // buildin function
  842. add_scriptl(func);
  843. add_scriptc(C_ARG);
  844. arg = buildin_func[str_data[func].val].arg;
  845. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  846. // script defined function
  847. add_scriptl(buildin_callsub_ref);
  848. add_scriptc(C_ARG);
  849. add_scriptl(func);
  850. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  851. if( *arg == 0 )
  852. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  853. if( *arg != '*' )
  854. ++arg; // count func as argument
  855. } else {
  856. #ifdef SCRIPT_CALLFUNC_CHECK
  857. const char* name = get_str(func);
  858. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  859. #endif
  860. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  861. #ifdef SCRIPT_CALLFUNC_CHECK
  862. } else {;
  863. add_scriptl(buildin_callfunc_ref);
  864. add_scriptc(C_ARG);
  865. add_scriptc(C_STR);
  866. while( *name ) add_scriptb(*name ++);
  867. add_scriptb(0);
  868. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  869. if( *arg != '*' ) ++ arg;
  870. }
  871. #endif
  872. }
  873. p = skip_word(p);
  874. p = skip_space(p);
  875. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  876. syntax.curly[syntax.curly_count].count = 0;
  877. if( *p == ';' )
  878. {// <func name> ';'
  879. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  880. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  881. {// <func name> '(' ')'
  882. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  883. p = p2;
  884. /*
  885. } else if( 0 && require_paren && *p != '(' )
  886. {// <func name>
  887. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  888. */
  889. } else
  890. {// <func name> <arg list>
  891. if( require_paren ){
  892. if( *p != '(' )
  893. disp_error_message("need '('",p);
  894. ++p; // skip '('
  895. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  896. } else if( *p == '(' ){
  897. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  898. } else {
  899. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  900. }
  901. ++syntax.curly_count;
  902. while( *arg ) {
  903. p2=parse_subexpr(p,-1);
  904. if( p == p2 )
  905. break; // not an argument
  906. if( *arg != '*' )
  907. ++arg; // next argument
  908. p=skip_space(p2);
  909. if( *arg == 0 || *p != ',' )
  910. break; // no more arguments
  911. ++p; // skip comma
  912. }
  913. --syntax.curly_count;
  914. }
  915. if( *arg && *arg != '?' && *arg != '*' )
  916. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  917. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  918. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  919. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  920. if( *p != ')' )
  921. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  922. ++p;
  923. }
  924. add_scriptc(C_FUNC);
  925. return p;
  926. }
  927. /// Processes end of logical script line.
  928. /// @param first When true, only fix up scheduling data is initialized
  929. /// @param p Script position for error reporting in set_label
  930. static void parse_nextline(bool first, const char* p)
  931. {
  932. if( !first )
  933. {
  934. add_scriptc(C_EOL); // mark end of line for stack cleanup
  935. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  936. }
  937. // initialize data for new '-' label fix up scheduling
  938. str_data[LABEL_NEXTLINE].type = C_NOP;
  939. str_data[LABEL_NEXTLINE].backpatch = -1;
  940. str_data[LABEL_NEXTLINE].label = -1;
  941. }
  942. /// Parse a variable assignment using the direct equals operator
  943. /// @param p script position where the function should run from
  944. /// @return NULL if not a variable assignment, the new position otherwise
  945. const char* parse_variable(const char* p)
  946. {
  947. int i, j, word;
  948. c_op type = C_NOP;
  949. const char *p2 = NULL;
  950. const char *var = p;
  951. // skip the variable where applicable
  952. p = skip_word(p);
  953. p = skip_space(p);
  954. if( p == NULL )
  955. {// end of the line or invalid buffer
  956. return NULL;
  957. }
  958. if( *p == '[' )
  959. {// array variable so process the array as appropriate
  960. for( p2 = p, i = 0, j = 1; p; ++ i )
  961. {
  962. if( *p ++ == ']' && --(j) == 0 ) break;
  963. if( *p == '[' ) ++ j;
  964. }
  965. if( !(p = skip_space(p)) )
  966. {// end of line or invalid characters remaining
  967. disp_error_message("Missing right expression or closing bracket for variable.", p);
  968. }
  969. }
  970. if( type == C_NOP &&
  971. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  972. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  973. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  974. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  975. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  976. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  977. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  978. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  979. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  980. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  981. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  982. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  983. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  984. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  985. ) )
  986. {// failed to find a matching operator combination so invalid
  987. return NULL;
  988. }
  989. switch( type )
  990. {
  991. case C_EQ:
  992. {// incremental modifier
  993. p = skip_space( &p[1] );
  994. }
  995. break;
  996. case C_L_SHIFT:
  997. case C_R_SHIFT:
  998. {// left or right shift modifier
  999. p = skip_space( &p[3] );
  1000. }
  1001. break;
  1002. default:
  1003. {// normal incremental command
  1004. p = skip_space( &p[2] );
  1005. }
  1006. }
  1007. if( p == NULL )
  1008. {// end of line or invalid buffer
  1009. return NULL;
  1010. }
  1011. // push the set function onto the stack
  1012. add_scriptl(buildin_set_ref);
  1013. add_scriptc(C_ARG);
  1014. // always append parenthesis to avoid errors
  1015. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1016. syntax.curly[syntax.curly_count].count = 0;
  1017. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1018. // increment the total curly count for the position in the script
  1019. ++ syntax.curly_count;
  1020. // parse the variable currently being modified
  1021. word = add_word(var);
  1022. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1023. {// cannot assign a variable which exists as a function or label
  1024. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1025. }
  1026. if( p2 )
  1027. {// process the variable index
  1028. const char* p3 = NULL;
  1029. // push the getelementofarray method into the stack
  1030. add_scriptl(buildin_getelementofarray_ref);
  1031. add_scriptc(C_ARG);
  1032. add_scriptl(word);
  1033. // process the sub-expression for this assignment
  1034. p3 = parse_subexpr(p2 + 1, 1);
  1035. p3 = skip_space(p3);
  1036. if( *p3 != ']' )
  1037. {// closing parenthesis is required for this script
  1038. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1039. }
  1040. // push the closing function stack operator onto the stack
  1041. add_scriptc(C_FUNC);
  1042. p3 ++;
  1043. }
  1044. else
  1045. {// simply push the variable or value onto the stack
  1046. add_scriptl(word);
  1047. }
  1048. add_scriptc(C_REF);
  1049. if( type == C_ADD_PP || type == C_SUB_PP )
  1050. {// incremental operator for the method
  1051. add_scripti(1);
  1052. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1053. }
  1054. else
  1055. {// process the value as an expression
  1056. p = parse_subexpr(p, -1);
  1057. if( type != C_EQ )
  1058. {// push the type of modifier onto the stack
  1059. add_scriptc(type);
  1060. }
  1061. }
  1062. // decrement the curly count for the position within the script
  1063. -- syntax.curly_count;
  1064. // close the script by appending the function operator
  1065. add_scriptc(C_FUNC);
  1066. // push the buffer from the method
  1067. return p;
  1068. }
  1069. /*==========================================
  1070. * 項の解析
  1071. *------------------------------------------*/
  1072. const char* parse_simpleexpr(const char *p)
  1073. {
  1074. int i;
  1075. p=skip_space(p);
  1076. if(*p==';' || *p==',')
  1077. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  1078. if(*p=='('){
  1079. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1080. ++syntax.curly[i].count;
  1081. p=parse_subexpr(p+1,-1);
  1082. p=skip_space(p);
  1083. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1084. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1085. ){
  1086. if( *p == ',' ){
  1087. syntax.curly[i].flag = ARGLIST_PAREN;
  1088. return p;
  1089. } else
  1090. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1091. }
  1092. if( *p != ')' )
  1093. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  1094. ++p;
  1095. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  1096. char *np;
  1097. i=strtoul(p,&np,0);
  1098. add_scripti(i);
  1099. p=np;
  1100. } else if(*p=='"'){
  1101. add_scriptc(C_STR);
  1102. p++;
  1103. while( *p && *p != '"' ){
  1104. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1105. {
  1106. char buf[8];
  1107. size_t len = skip_escaped_c(p) - p;
  1108. size_t n = sv_unescape_c(buf, p, len);
  1109. if( n != 1 )
  1110. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1111. p += len;
  1112. add_scriptb(*buf);
  1113. continue;
  1114. }
  1115. else if( *p == '\n' )
  1116. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1117. add_scriptb(*p++);
  1118. }
  1119. if(!*p)
  1120. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1121. add_scriptb(0);
  1122. p++; //'"'
  1123. } else {
  1124. int l;
  1125. const char* pv;
  1126. // label , register , function etc
  1127. if(skip_word(p)==p)
  1128. disp_error_message("parse_simpleexpr: unexpected character",p);
  1129. l=add_word(p);
  1130. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1131. return parse_callfunc(p,1,0);
  1132. #ifdef SCRIPT_CALLFUNC_CHECK
  1133. else {
  1134. const char* name = get_str(l);
  1135. if( strdb_get(userfunc_db,name) != NULL ) {
  1136. return parse_callfunc(p,1,1);
  1137. }
  1138. }
  1139. #endif
  1140. if( (pv = parse_variable(p)) )
  1141. {// successfully processed a variable assignment
  1142. return pv;
  1143. }
  1144. p=skip_word(p);
  1145. if( *p == '[' ){
  1146. // array(name[i] => getelementofarray(name,i) )
  1147. add_scriptl(buildin_getelementofarray_ref);
  1148. add_scriptc(C_ARG);
  1149. add_scriptl(l);
  1150. p=parse_subexpr(p+1,-1);
  1151. p=skip_space(p);
  1152. if( *p != ']' )
  1153. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  1154. ++p;
  1155. add_scriptc(C_FUNC);
  1156. }else
  1157. add_scriptl(l);
  1158. }
  1159. return p;
  1160. }
  1161. /*==========================================
  1162. * 式の解析
  1163. *------------------------------------------*/
  1164. const char* parse_subexpr(const char* p,int limit)
  1165. {
  1166. int op,opl,len;
  1167. const char* tmpp;
  1168. p=skip_space(p);
  1169. if(*p=='-'){
  1170. tmpp=skip_space(p+1);
  1171. if(*tmpp==';' || *tmpp==','){
  1172. add_scriptl(LABEL_NEXTLINE);
  1173. p++;
  1174. return p;
  1175. }
  1176. }
  1177. tmpp=p;
  1178. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1179. p=parse_subexpr(p+1,10);
  1180. add_scriptc(op);
  1181. } else
  1182. p=parse_simpleexpr(p);
  1183. p=skip_space(p);
  1184. while((
  1185. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1186. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1187. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1188. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1189. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1190. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1191. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1192. (op=C_AND,opl=6,len=1,*p=='&') ||
  1193. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1194. (op=C_OR,opl=5,len=1,*p=='|') ||
  1195. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1196. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1197. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1198. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1199. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1200. (op=C_GT,opl=3,len=1,*p=='>') ||
  1201. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1202. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1203. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1204. p+=len;
  1205. if(op == C_OP3) {
  1206. p=parse_subexpr(p,-1);
  1207. p=skip_space(p);
  1208. if( *(p++) != ':')
  1209. disp_error_message("parse_subexpr: need ':'", p-1);
  1210. p=parse_subexpr(p,-1);
  1211. } else {
  1212. p=parse_subexpr(p,opl);
  1213. }
  1214. add_scriptc(op);
  1215. p=skip_space(p);
  1216. }
  1217. return p; /* return first untreated operator */
  1218. }
  1219. /*==========================================
  1220. * 式の評価
  1221. *------------------------------------------*/
  1222. const char* parse_expr(const char *p)
  1223. {
  1224. switch(*p){
  1225. case ')': case ';': case ':': case '[': case ']':
  1226. case '}':
  1227. disp_error_message("parse_expr: unexpected char",p);
  1228. }
  1229. p=parse_subexpr(p,-1);
  1230. return p;
  1231. }
  1232. /*==========================================
  1233. * 行の解析
  1234. *------------------------------------------*/
  1235. const char* parse_line(const char* p)
  1236. {
  1237. const char* p2;
  1238. p=skip_space(p);
  1239. if(*p==';') {
  1240. // if(); for(); while(); のために閉じ判定
  1241. p = parse_syntax_close(p + 1);
  1242. return p;
  1243. }
  1244. if(*p==')' && parse_syntax_for_flag)
  1245. return p+1;
  1246. p = skip_space(p);
  1247. if(p[0] == '{') {
  1248. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1249. syntax.curly[syntax.curly_count].count = -1;
  1250. syntax.curly[syntax.curly_count].index = -1;
  1251. syntax.curly_count++;
  1252. return p + 1;
  1253. } else if(p[0] == '}') {
  1254. return parse_curly_close(p);
  1255. }
  1256. // 構文関連の処理
  1257. p2 = parse_syntax(p);
  1258. if(p2 != NULL)
  1259. return p2;
  1260. // attempt to process a variable assignment
  1261. p2 = parse_variable(p);
  1262. if( p2 != NULL )
  1263. {// variable assignment processed so leave the method
  1264. return parse_syntax_close(p2 + 1);
  1265. }
  1266. p = parse_callfunc(p,0,0);
  1267. p = skip_space(p);
  1268. if(parse_syntax_for_flag) {
  1269. if( *p != ')' )
  1270. disp_error_message("parse_line: need ')'",p);
  1271. } else {
  1272. if( *p != ';' )
  1273. disp_error_message("parse_line: need ';'",p);
  1274. }
  1275. // if, for , while の閉じ判定
  1276. p = parse_syntax_close(p+1);
  1277. return p;
  1278. }
  1279. // { ... } の閉じ処理
  1280. const char* parse_curly_close(const char* p)
  1281. {
  1282. if(syntax.curly_count <= 0) {
  1283. disp_error_message("parse_curly_close: unexpected string",p);
  1284. return p + 1;
  1285. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1286. syntax.curly_count--;
  1287. // if, for , while の閉じ判定
  1288. p = parse_syntax_close(p + 1);
  1289. return p;
  1290. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1291. // switch() 閉じ判定
  1292. int pos = syntax.curly_count-1;
  1293. char label[256];
  1294. int l;
  1295. // 一時変数を消す
  1296. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1297. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1298. parse_line(label);
  1299. syntax.curly_count--;
  1300. // 無条件で終了ポインタに移動
  1301. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1302. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1303. parse_line(label);
  1304. syntax.curly_count--;
  1305. // 現在地のラベルを付ける
  1306. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1307. l=add_str(label);
  1308. set_label(l,script_pos, p);
  1309. if(syntax.curly[pos].flag) {
  1310. // default が存在する
  1311. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1312. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1313. parse_line(label);
  1314. syntax.curly_count--;
  1315. }
  1316. // 終了ラベルを付ける
  1317. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1318. l=add_str(label);
  1319. set_label(l,script_pos, p);
  1320. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1321. syntax.curly_count--;
  1322. // if, for , while の閉じ判定
  1323. p = parse_syntax_close(p + 1);
  1324. return p;
  1325. } else {
  1326. disp_error_message("parse_curly_close: unexpected string",p);
  1327. return p + 1;
  1328. }
  1329. }
  1330. // 構文関連の処理
  1331. // break, case, continue, default, do, for, function,
  1332. // if, switch, while をこの内部で処理します。
  1333. const char* parse_syntax(const char* p)
  1334. {
  1335. const char *p2 = skip_word(p);
  1336. switch(*p) {
  1337. case 'B':
  1338. case 'b':
  1339. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1340. // break の処理
  1341. char label[256];
  1342. int pos = syntax.curly_count - 1;
  1343. while(pos >= 0) {
  1344. if(syntax.curly[pos].type == TYPE_DO) {
  1345. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1346. break;
  1347. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1348. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1349. break;
  1350. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1351. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1352. break;
  1353. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1354. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1355. break;
  1356. }
  1357. pos--;
  1358. }
  1359. if(pos < 0) {
  1360. disp_error_message("parse_syntax: unexpected 'break'",p);
  1361. } else {
  1362. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1363. parse_line(label);
  1364. syntax.curly_count--;
  1365. }
  1366. p = skip_space(p2);
  1367. if(*p != ';')
  1368. disp_error_message("parse_syntax: need ';'",p);
  1369. // if, for , while の閉じ判定
  1370. p = parse_syntax_close(p + 1);
  1371. return p;
  1372. }
  1373. break;
  1374. case 'c':
  1375. case 'C':
  1376. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1377. // case の処理
  1378. int pos = syntax.curly_count-1;
  1379. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1380. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1381. return p+1;
  1382. } else {
  1383. char label[256];
  1384. int l,v;
  1385. char *np;
  1386. if(syntax.curly[pos].count != 1) {
  1387. // FALLTHRU 用のジャンプ
  1388. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1389. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1390. parse_line(label);
  1391. syntax.curly_count--;
  1392. // 現在地のラベルを付ける
  1393. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1394. l=add_str(label);
  1395. set_label(l,script_pos, p);
  1396. }
  1397. // switch 判定文
  1398. p = skip_space(p2);
  1399. if(p == p2) {
  1400. disp_error_message("parse_syntax: expect space ' '",p);
  1401. }
  1402. // check whether case label is integer or not
  1403. v = strtol(p,&np,0);
  1404. if(np == p) { //Check for constants
  1405. p2 = skip_word(p);
  1406. v = p2-p; // length of word at p2
  1407. memcpy(label,p,v);
  1408. label[v]='\0';
  1409. if( !script_get_constant(label, &v) )
  1410. disp_error_message("parse_syntax: 'case' label not integer",p);
  1411. p = skip_word(p);
  1412. } else { //Numeric value
  1413. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1414. p++;
  1415. p = skip_word(p);
  1416. if(np != p)
  1417. disp_error_message("parse_syntax: 'case' label not integer",np);
  1418. }
  1419. p = skip_space(p);
  1420. if(*p != ':')
  1421. disp_error_message("parse_syntax: expect ':'",p);
  1422. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1423. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1424. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1425. // 2回parse しないとダメ
  1426. p2 = parse_line(label);
  1427. parse_line(p2);
  1428. syntax.curly_count--;
  1429. if(syntax.curly[pos].count != 1) {
  1430. // FALLTHRU 終了後のラベル
  1431. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1432. l=add_str(label);
  1433. set_label(l,script_pos,p);
  1434. }
  1435. // check duplication of case label [Rayce]
  1436. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1437. disp_error_message("parse_syntax: dup 'case'",p);
  1438. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1439. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1440. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1441. parse_line(label);
  1442. syntax.curly_count--;
  1443. syntax.curly[pos].count++;
  1444. }
  1445. return p + 1;
  1446. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1447. // continue の処理
  1448. char label[256];
  1449. int pos = syntax.curly_count - 1;
  1450. while(pos >= 0) {
  1451. if(syntax.curly[pos].type == TYPE_DO) {
  1452. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1453. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1454. break;
  1455. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1456. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1457. break;
  1458. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1459. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1460. break;
  1461. }
  1462. pos--;
  1463. }
  1464. if(pos < 0) {
  1465. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1466. } else {
  1467. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1468. parse_line(label);
  1469. syntax.curly_count--;
  1470. }
  1471. p = skip_space(p2);
  1472. if(*p != ';')
  1473. disp_error_message("parse_syntax: need ';'",p);
  1474. // if, for , while の閉じ判定
  1475. p = parse_syntax_close(p + 1);
  1476. return p;
  1477. }
  1478. break;
  1479. case 'd':
  1480. case 'D':
  1481. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1482. // switch - default の処理
  1483. int pos = syntax.curly_count-1;
  1484. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1485. disp_error_message("parse_syntax: unexpected 'default'",p);
  1486. } else if(syntax.curly[pos].flag) {
  1487. disp_error_message("parse_syntax: dup 'default'",p);
  1488. } else {
  1489. char label[256];
  1490. int l;
  1491. // 現在地のラベルを付ける
  1492. p = skip_space(p2);
  1493. if(*p != ':') {
  1494. disp_error_message("parse_syntax: need ':'",p);
  1495. }
  1496. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1497. l=add_str(label);
  1498. set_label(l,script_pos,p);
  1499. // 無条件で次のリンクに飛ばす
  1500. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1501. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1502. parse_line(label);
  1503. syntax.curly_count--;
  1504. // default のラベルを付ける
  1505. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1506. l=add_str(label);
  1507. set_label(l,script_pos,p);
  1508. syntax.curly[syntax.curly_count - 1].flag = 1;
  1509. syntax.curly[pos].count++;
  1510. }
  1511. return p + 1;
  1512. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1513. int l;
  1514. char label[256];
  1515. p=skip_space(p2);
  1516. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1517. syntax.curly[syntax.curly_count].count = 1;
  1518. syntax.curly[syntax.curly_count].index = syntax.index++;
  1519. syntax.curly[syntax.curly_count].flag = 0;
  1520. // 現在地のラベル形成する
  1521. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1522. l=add_str(label);
  1523. set_label(l,script_pos,p);
  1524. syntax.curly_count++;
  1525. return p;
  1526. }
  1527. break;
  1528. case 'f':
  1529. case 'F':
  1530. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1531. int l;
  1532. char label[256];
  1533. int pos = syntax.curly_count;
  1534. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1535. syntax.curly[syntax.curly_count].count = 1;
  1536. syntax.curly[syntax.curly_count].index = syntax.index++;
  1537. syntax.curly[syntax.curly_count].flag = 0;
  1538. syntax.curly_count++;
  1539. p=skip_space(p2);
  1540. if(*p != '(')
  1541. disp_error_message("parse_syntax: need '('",p);
  1542. p++;
  1543. // 初期化文を実行する
  1544. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1545. p=parse_line(p);
  1546. syntax.curly_count--;
  1547. // 条件判断開始のラベル形成する
  1548. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1549. l=add_str(label);
  1550. set_label(l,script_pos,p);
  1551. p=skip_space(p);
  1552. if(*p == ';') {
  1553. // for(;;) のパターンなので必ず真
  1554. ;
  1555. } else {
  1556. // 条件が偽なら終了地点に飛ばす
  1557. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1558. add_scriptl(add_str("jump_zero"));
  1559. add_scriptc(C_ARG);
  1560. p=parse_expr(p);
  1561. p=skip_space(p);
  1562. add_scriptl(add_str(label));
  1563. add_scriptc(C_FUNC);
  1564. }
  1565. if(*p != ';')
  1566. disp_error_message("parse_syntax: need ';'",p);
  1567. p++;
  1568. // ループ開始に飛ばす
  1569. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1570. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1571. parse_line(label);
  1572. syntax.curly_count--;
  1573. // 次のループへのラベル形成する
  1574. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1575. l=add_str(label);
  1576. set_label(l,script_pos,p);
  1577. // 次のループに入る時の処理
  1578. // for 最後の ')' を ';' として扱うフラグ
  1579. parse_syntax_for_flag = 1;
  1580. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1581. p=parse_line(p);
  1582. syntax.curly_count--;
  1583. parse_syntax_for_flag = 0;
  1584. // 条件判定処理に飛ばす
  1585. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1586. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1587. parse_line(label);
  1588. syntax.curly_count--;
  1589. // ループ開始のラベル付け
  1590. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1591. l=add_str(label);
  1592. set_label(l,script_pos,p);
  1593. return p;
  1594. }
  1595. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1596. {// internal script function
  1597. const char *func_name;
  1598. func_name = skip_space(p2);
  1599. p = skip_word(func_name);
  1600. if( p == func_name )
  1601. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1602. p2 = skip_space(p);
  1603. if( *p2 == ';' )
  1604. {// function <name> ;
  1605. // function declaration - just register the name
  1606. int l;
  1607. l = add_word(func_name);
  1608. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1609. str_data[l].type = C_USERFUNC;
  1610. else if( str_data[l].type == C_USERFUNC )
  1611. ; // already registered
  1612. else
  1613. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1614. // if, for , while の閉じ判定
  1615. p = parse_syntax_close(p2 + 1);
  1616. return p;
  1617. }
  1618. else if(*p2 == '{')
  1619. {// function <name> <line/block of code>
  1620. char label[256];
  1621. int l;
  1622. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1623. syntax.curly[syntax.curly_count].count = 1;
  1624. syntax.curly[syntax.curly_count].index = syntax.index++;
  1625. syntax.curly[syntax.curly_count].flag = 0;
  1626. ++syntax.curly_count;
  1627. // Jump over the function code
  1628. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1629. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1630. ++syntax.curly_count;
  1631. parse_line(label);
  1632. --syntax.curly_count;
  1633. // Set the position of the function (label)
  1634. l=add_word(func_name);
  1635. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1636. {
  1637. str_data[l].type = C_USERFUNC;
  1638. set_label(l, script_pos, p);
  1639. if( parse_options&SCRIPT_USE_LABEL_DB )
  1640. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1641. }
  1642. else
  1643. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1644. return skip_space(p);
  1645. }
  1646. else
  1647. {
  1648. disp_error_message("expect ';' or '{' at function syntax",p);
  1649. }
  1650. }
  1651. break;
  1652. case 'i':
  1653. case 'I':
  1654. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1655. // if() の処理
  1656. char label[256];
  1657. p=skip_space(p2);
  1658. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1659. disp_error_message("need '('",p);
  1660. }
  1661. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1662. syntax.curly[syntax.curly_count].count = 1;
  1663. syntax.curly[syntax.curly_count].index = syntax.index++;
  1664. syntax.curly[syntax.curly_count].flag = 0;
  1665. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1666. syntax.curly_count++;
  1667. add_scriptl(add_str("jump_zero"));
  1668. add_scriptc(C_ARG);
  1669. p=parse_expr(p);
  1670. p=skip_space(p);
  1671. add_scriptl(add_str(label));
  1672. add_scriptc(C_FUNC);
  1673. return p;
  1674. }
  1675. break;
  1676. case 's':
  1677. case 'S':
  1678. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1679. // switch() の処理
  1680. char label[256];
  1681. p=skip_space(p2);
  1682. if(*p != '(') {
  1683. disp_error_message("need '('",p);
  1684. }
  1685. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1686. syntax.curly[syntax.curly_count].count = 1;
  1687. syntax.curly[syntax.curly_count].index = syntax.index++;
  1688. syntax.curly[syntax.curly_count].flag = 0;
  1689. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1690. syntax.curly_count++;
  1691. add_scriptl(add_str("set"));
  1692. add_scriptc(C_ARG);
  1693. add_scriptl(add_str(label));
  1694. p=parse_expr(p);
  1695. p=skip_space(p);
  1696. if(*p != '{') {
  1697. disp_error_message("parse_syntax: need '{'",p);
  1698. }
  1699. add_scriptc(C_FUNC);
  1700. return p + 1;
  1701. }
  1702. break;
  1703. case 'w':
  1704. case 'W':
  1705. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1706. int l;
  1707. char label[256];
  1708. p=skip_space(p2);
  1709. if(*p != '(') {
  1710. disp_error_message("need '('",p);
  1711. }
  1712. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1713. syntax.curly[syntax.curly_count].count = 1;
  1714. syntax.curly[syntax.curly_count].index = syntax.index++;
  1715. syntax.curly[syntax.curly_count].flag = 0;
  1716. // 条件判断開始のラベル形成する
  1717. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1718. l=add_str(label);
  1719. set_label(l,script_pos,p);
  1720. // 条件が偽なら終了地点に飛ばす
  1721. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1722. syntax.curly_count++;
  1723. add_scriptl(add_str("jump_zero"));
  1724. add_scriptc(C_ARG);
  1725. p=parse_expr(p);
  1726. p=skip_space(p);
  1727. add_scriptl(add_str(label));
  1728. add_scriptc(C_FUNC);
  1729. return p;
  1730. }
  1731. break;
  1732. }
  1733. return NULL;
  1734. }
  1735. const char* parse_syntax_close(const char *p) {
  1736. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1737. int flag;
  1738. do {
  1739. p = parse_syntax_close_sub(p,&flag);
  1740. } while(flag);
  1741. return p;
  1742. }
  1743. // if, for , while , do の閉じ判定
  1744. // flag == 1 : 閉じられた
  1745. // flag == 0 : 閉じられない
  1746. const char* parse_syntax_close_sub(const char* p,int* flag)
  1747. {
  1748. char label[256];
  1749. int pos = syntax.curly_count - 1;
  1750. int l;
  1751. *flag = 1;
  1752. if(syntax.curly_count <= 0) {
  1753. *flag = 0;
  1754. return p;
  1755. } else if(syntax.curly[pos].type == TYPE_IF) {
  1756. const char *bp = p;
  1757. const char *p2;
  1758. // if-block and else-block end is a new line
  1759. parse_nextline(false, p);
  1760. // if 最終場所へ飛ばす
  1761. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1762. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1763. parse_line(label);
  1764. syntax.curly_count--;
  1765. // 現在地のラベルを付ける
  1766. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1767. l=add_str(label);
  1768. set_label(l,script_pos,p);
  1769. syntax.curly[pos].count++;
  1770. p = skip_space(p);
  1771. p2 = skip_word(p);
  1772. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1773. // else or else - if
  1774. p = skip_space(p2);
  1775. p2 = skip_word(p);
  1776. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1777. // else - if
  1778. p=skip_space(p2);
  1779. if(*p != '(') {
  1780. disp_error_message("need '('",p);
  1781. }
  1782. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1783. add_scriptl(add_str("jump_zero"));
  1784. add_scriptc(C_ARG);
  1785. p=parse_expr(p);
  1786. p=skip_space(p);
  1787. add_scriptl(add_str(label));
  1788. add_scriptc(C_FUNC);
  1789. *flag = 0;
  1790. return p;
  1791. } else {
  1792. // else
  1793. if(!syntax.curly[pos].flag) {
  1794. syntax.curly[pos].flag = 1;
  1795. *flag = 0;
  1796. return p;
  1797. }
  1798. }
  1799. }
  1800. // if 閉じ
  1801. syntax.curly_count--;
  1802. // 最終地のラベルを付ける
  1803. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1804. l=add_str(label);
  1805. set_label(l,script_pos,p);
  1806. if(syntax.curly[pos].flag == 1) {
  1807. // このifに対するelseじゃないのでポインタの位置は同じ
  1808. return bp;
  1809. }
  1810. return p;
  1811. } else if(syntax.curly[pos].type == TYPE_DO) {
  1812. int l;
  1813. char label[256];
  1814. const char *p2;
  1815. if(syntax.curly[pos].flag) {
  1816. // 現在地のラベル形成する(continue でここに来る)
  1817. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1818. l=add_str(label);
  1819. set_label(l,script_pos,p);
  1820. }
  1821. // 条件が偽なら終了地点に飛ばす
  1822. p = skip_space(p);
  1823. p2 = skip_word(p);
  1824. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1825. disp_error_message("parse_syntax: need 'while'",p);
  1826. p = skip_space(p2);
  1827. if(*p != '(') {
  1828. disp_error_message("need '('",p);
  1829. }
  1830. // do-block end is a new line
  1831. parse_nextline(false, p);
  1832. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1833. add_scriptl(add_str("jump_zero"));
  1834. add_scriptc(C_ARG);
  1835. p=parse_expr(p);
  1836. p=skip_space(p);
  1837. add_scriptl(add_str(label));
  1838. add_scriptc(C_FUNC);
  1839. // 開始地点に飛ばす
  1840. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1841. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1842. parse_line(label);
  1843. syntax.curly_count--;
  1844. // 条件終了地点のラベル形成する
  1845. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1846. l=add_str(label);
  1847. set_label(l,script_pos,p);
  1848. p = skip_space(p);
  1849. if(*p != ';') {
  1850. disp_error_message("parse_syntax: need ';'",p);
  1851. return p+1;
  1852. }
  1853. p++;
  1854. syntax.curly_count--;
  1855. return p;
  1856. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1857. // for-block end is a new line
  1858. parse_nextline(false, p);
  1859. // 次のループに飛ばす
  1860. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1861. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1862. parse_line(label);
  1863. syntax.curly_count--;
  1864. // for 終了のラベル付け
  1865. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1866. l=add_str(label);
  1867. set_label(l,script_pos,p);
  1868. syntax.curly_count--;
  1869. return p;
  1870. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1871. // while-block end is a new line
  1872. parse_nextline(false, p);
  1873. // while 条件判断へ飛ばす
  1874. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1875. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1876. parse_line(label);
  1877. syntax.curly_count--;
  1878. // while 終了のラベル付け
  1879. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1880. l=add_str(label);
  1881. set_label(l,script_pos,p);
  1882. syntax.curly_count--;
  1883. return p;
  1884. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1885. int pos = syntax.curly_count-1;
  1886. char label[256];
  1887. int l;
  1888. // 戻す
  1889. sprintf(label,"return;");
  1890. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1891. parse_line(label);
  1892. syntax.curly_count--;
  1893. // 現在地のラベルを付ける
  1894. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1895. l=add_str(label);
  1896. set_label(l,script_pos,p);
  1897. syntax.curly_count--;
  1898. return p;
  1899. } else {
  1900. *flag = 0;
  1901. return p;
  1902. }
  1903. }
  1904. /*==========================================
  1905. * 組み込み関数の追加
  1906. *------------------------------------------*/
  1907. static void add_buildin_func(void)
  1908. {
  1909. int i,n;
  1910. const char* p;
  1911. for( i = 0; buildin_func[i].func; i++ )
  1912. {
  1913. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1914. // 'v' - value (either string or int or reference)
  1915. // 's' - string
  1916. // 'i' - int
  1917. // 'r' - reference (of a variable)
  1918. // 'l' - label
  1919. // '?' - one optional parameter
  1920. // '*' - unknown number of optional parameters
  1921. p = buildin_func[i].arg;
  1922. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1923. while( *p == '?' ) ++p;
  1924. if( *p == '*' ) ++p;
  1925. if( *p != 0){
  1926. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1927. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1928. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1929. } else {
  1930. n = add_str(buildin_func[i].name);
  1931. str_data[n].type = C_FUNC;
  1932. str_data[n].val = i;
  1933. str_data[n].func = buildin_func[i].func;
  1934. if( !strcmp(buildin_func[i].name, "set") ) buildin_set_ref = n; else
  1935. if( !strcmp(buildin_func[i].name, "callsub") ) buildin_callsub_ref = n; else
  1936. if( !strcmp(buildin_func[i].name, "callfunc") ) buildin_callfunc_ref = n; else
  1937. if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1938. }
  1939. }
  1940. }
  1941. /// Retrieves the value of a constant.
  1942. bool script_get_constant(const char* name, int* value)
  1943. {
  1944. int n = search_str(name);
  1945. if( n == -1 || str_data[n].type != C_INT )
  1946. {// not found or not a constant
  1947. return false;
  1948. }
  1949. value[0] = str_data[n].val;
  1950. return true;
  1951. }
  1952. /// Creates new constant or parameter with given value.
  1953. void script_set_constant(const char* name, int value, bool isparameter)
  1954. {
  1955. int n = add_str(name);
  1956. if( str_data[n].type == C_NOP )
  1957. {// new
  1958. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1959. str_data[n].val = value;
  1960. }
  1961. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1962. {// existing parameter or constant
  1963. ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%d, new value=%d).\n", ( str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, str_data[n].val, value);
  1964. }
  1965. else
  1966. {// existing name
  1967. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1968. }
  1969. }
  1970. /*==========================================
  1971. * 定数データベースの読み込み
  1972. *------------------------------------------*/
  1973. static void read_constdb(void)
  1974. {
  1975. FILE *fp;
  1976. char line[1024],name[1024],val[1024];
  1977. int type;
  1978. sprintf(line, "%s/const.txt", db_path);
  1979. fp=fopen(line, "r");
  1980. if(fp==NULL){
  1981. ShowError("can't read %s\n", line);
  1982. return ;
  1983. }
  1984. while(fgets(line, sizeof(line), fp))
  1985. {
  1986. if(line[0]=='/' && line[1]=='/')
  1987. continue;
  1988. type=0;
  1989. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1990. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1991. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  1992. }
  1993. }
  1994. fclose(fp);
  1995. }
  1996. /*==========================================
  1997. * エラー表示
  1998. *------------------------------------------*/
  1999. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2000. {
  2001. int i;
  2002. if( p == NULL || !p[0] ) return NULL;
  2003. if( line < 0 )
  2004. StringBuf_Printf(buf, "*% 5d : ", -line);
  2005. else
  2006. StringBuf_Printf(buf, " % 5d : ", line);
  2007. for(i=0;p[i] && p[i] != '\n';i++){
  2008. if(p + i != mark)
  2009. StringBuf_Printf(buf, "%c", p[i]);
  2010. else
  2011. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2012. }
  2013. StringBuf_AppendStr(buf, "\n");
  2014. return p+i+(p[i] == '\n' ? 1 : 0);
  2015. }
  2016. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  2017. {
  2018. // エラーが発生した行を求める
  2019. int j;
  2020. int line = start_line;
  2021. const char *p;
  2022. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2023. StringBuf buf;
  2024. for(p=src;p && *p;line++){
  2025. const char *lineend=strchr(p,'\n');
  2026. if(lineend==NULL || error_pos<lineend){
  2027. break;
  2028. }
  2029. for( j = 0; j < 4; j++ ) {
  2030. linestart[j] = linestart[j+1];
  2031. }
  2032. linestart[4] = p;
  2033. p=lineend+1;
  2034. }
  2035. StringBuf_Init(&buf);
  2036. StringBuf_AppendStr(&buf, "\a\n");
  2037. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  2038. StringBuf_Printf(&buf, " %s\n", error_msg);
  2039. for(j = 0; j < 5; j++ ) {
  2040. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  2041. }
  2042. p = script_print_line(&buf, p, error_pos, -line);
  2043. for(j = 0; j < 5; j++) {
  2044. p = script_print_line(&buf, p, NULL, line + j + 1 );
  2045. }
  2046. ShowError("%s", StringBuf_Value(&buf));
  2047. StringBuf_Destroy(&buf);
  2048. }
  2049. /*==========================================
  2050. * スクリプトの解析
  2051. *------------------------------------------*/
  2052. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2053. {
  2054. const char *p,*tmpp;
  2055. int i;
  2056. struct script_code* code = NULL;
  2057. static int first=1;
  2058. char end;
  2059. bool unresolved_names = false;
  2060. if( src == NULL )
  2061. return NULL;// empty script
  2062. memset(&syntax,0,sizeof(syntax));
  2063. if(first){
  2064. add_buildin_func();
  2065. read_constdb();
  2066. first=0;
  2067. }
  2068. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2069. script_pos=0;
  2070. script_size=SCRIPT_BLOCK_SIZE;
  2071. parse_nextline(true, NULL);
  2072. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2073. if( options&SCRIPT_USE_LABEL_DB )
  2074. db_clear(scriptlabel_db);
  2075. parse_options = options;
  2076. if( setjmp( error_jump ) != 0 ) {
  2077. //Restore program state when script has problems. [from jA]
  2078. int i;
  2079. const int size = ARRAYLENGTH(syntax.curly);
  2080. if( error_report )
  2081. script_error(src,file,line,error_msg,error_pos);
  2082. aFree( error_msg );
  2083. aFree( script_buf );
  2084. script_pos = 0;
  2085. script_size = 0;
  2086. script_buf = NULL;
  2087. for(i=LABEL_START;i<str_num;i++)
  2088. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  2089. for(i=0; i<size; i++)
  2090. linkdb_final(&syntax.curly[i].case_label);
  2091. return NULL;
  2092. }
  2093. parse_syntax_for_flag=0;
  2094. p=src;
  2095. p=skip_space(p);
  2096. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2097. {// does not require brackets around the script
  2098. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2099. {// empty script and can return NULL
  2100. aFree( script_buf );
  2101. script_pos = 0;
  2102. script_size = 0;
  2103. script_buf = NULL;
  2104. return NULL;
  2105. }
  2106. end = '\0';
  2107. }
  2108. else
  2109. {// requires brackets around the script
  2110. if( *p != '{' )
  2111. disp_error_message("not found '{'",p);
  2112. p = skip_space(p+1);
  2113. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2114. {// empty script and can return NULL
  2115. aFree( script_buf );
  2116. script_pos = 0;
  2117. script_size = 0;
  2118. script_buf = NULL;
  2119. return NULL;
  2120. }
  2121. end = '}';
  2122. }
  2123. // clear references of labels, variables and internal functions
  2124. for(i=LABEL_START;i<str_num;i++){
  2125. if(
  2126. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2127. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2128. ){
  2129. str_data[i].type=C_NOP;
  2130. str_data[i].backpatch=-1;
  2131. str_data[i].label=-1;
  2132. }
  2133. }
  2134. while( syntax.curly_count != 0 || *p != end )
  2135. {
  2136. if( *p == '\0' )
  2137. disp_error_message("unexpected end of script",p);
  2138. // labelだけ特殊処理
  2139. tmpp=skip_space(skip_word(p));
  2140. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2141. i=add_word(p);
  2142. set_label(i,script_pos,p);
  2143. if( parse_options&SCRIPT_USE_LABEL_DB )
  2144. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2145. p=tmpp+1;
  2146. p=skip_space(p);
  2147. continue;
  2148. }
  2149. // 他は全部一緒くた
  2150. p=parse_line(p);
  2151. p=skip_space(p);
  2152. parse_nextline(false, p);
  2153. }
  2154. add_scriptc(C_NOP);
  2155. // trim code to size
  2156. script_size = script_pos;
  2157. RECREATE(script_buf,unsigned char,script_pos);
  2158. // default unknown references to variables
  2159. for(i=LABEL_START;i<str_num;i++){
  2160. if(str_data[i].type==C_NOP){
  2161. int j,next;
  2162. str_data[i].type=C_NAME;
  2163. str_data[i].label=i;
  2164. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2165. next=GETVALUE(script_buf,j);
  2166. SETVALUE(script_buf,j,i);
  2167. j=next;
  2168. }
  2169. }
  2170. else if( str_data[i].type == C_USERFUNC )
  2171. {// 'function name;' without follow-up code
  2172. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2173. unresolved_names = true;
  2174. }
  2175. }
  2176. if( unresolved_names )
  2177. {
  2178. disp_error_message("parse_script: unresolved function references", p);
  2179. }
  2180. #ifdef DEBUG_DISP
  2181. for(i=0;i<script_pos;i++){
  2182. if((i&15)==0) ShowMessage("%04x : ",i);
  2183. ShowMessage("%02x ",script_buf[i]);
  2184. if((i&15)==15) ShowMessage("\n");
  2185. }
  2186. ShowMessage("\n");
  2187. #endif
  2188. #ifdef DEBUG_DISASM
  2189. {
  2190. int i = 0,j;
  2191. while(i < script_pos) {
  2192. c_op op = get_com(script_buf,&i);
  2193. ShowMessage("%06x %s", i, script_op2name(op));
  2194. j = i;
  2195. switch(op) {
  2196. case C_INT:
  2197. ShowMessage(" %d", get_num(script_buf,&i));
  2198. break;
  2199. case C_POS:
  2200. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2201. i += 3;
  2202. break;
  2203. case C_NAME:
  2204. j = (*(int*)(script_buf+i)&0xffffff);
  2205. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2206. i += 3;
  2207. break;
  2208. case C_STR:
  2209. j = strlen(script_buf + i);
  2210. ShowMessage(" %s", script_buf + i);
  2211. i += j+1;
  2212. break;
  2213. }
  2214. ShowMessage(CL_CLL"\n");
  2215. }
  2216. }
  2217. #endif
  2218. CREATE(code,struct script_code,1);
  2219. code->script_buf = script_buf;
  2220. code->script_size = script_size;
  2221. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2222. return code;
  2223. }
  2224. /// Returns the player attached to this script, identified by the rid.
  2225. /// If there is no player attached, the script is terminated.
  2226. TBL_PC *script_rid2sd(struct script_state *st)
  2227. {
  2228. TBL_PC *sd=map_id2sd(st->rid);
  2229. if(!sd){
  2230. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2231. script_reportfunc(st);
  2232. script_reportsrc(st);
  2233. st->state = END;
  2234. }
  2235. return sd;
  2236. }
  2237. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2238. ///
  2239. /// @param st Script state
  2240. /// @param data Variable/constant
  2241. void get_val(struct script_state* st, struct script_data* data)
  2242. {
  2243. const char* name;
  2244. char prefix;
  2245. char postfix;
  2246. TBL_PC* sd = NULL;
  2247. if( !data_isreference(data) )
  2248. return;// not a variable/constant
  2249. name = reference_getname(data);
  2250. prefix = name[0];
  2251. postfix = name[strlen(name) - 1];
  2252. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2253. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2254. {
  2255. sd = script_rid2sd(st);
  2256. if( sd == NULL )
  2257. {// needs player attached
  2258. if( postfix == '$' )
  2259. {// string variable
  2260. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2261. data->type = C_CONSTSTR;
  2262. data->u.str = "";
  2263. }
  2264. else
  2265. {// integer variable
  2266. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2267. data->type = C_INT;
  2268. data->u.num = 0;
  2269. }
  2270. return;
  2271. }
  2272. }
  2273. if( postfix == '$' )
  2274. {// string variable
  2275. switch( prefix )
  2276. {
  2277. case '@':
  2278. data->u.str = pc_readregstr(sd, data->u.num);
  2279. break;
  2280. case '$':
  2281. data->u.str = mapreg_readregstr(data->u.num);
  2282. break;
  2283. case '#':
  2284. if( name[1] == '#' )
  2285. data->u.str = pc_readaccountreg2str(sd, name);// global
  2286. else
  2287. data->u.str = pc_readaccountregstr(sd, name);// local
  2288. break;
  2289. case '.':
  2290. {
  2291. struct DBMap* n =
  2292. data->ref ? *data->ref:
  2293. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2294. st->script->script_vars;// npc variable
  2295. if( n )
  2296. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2297. else
  2298. data->u.str = NULL;
  2299. }
  2300. break;
  2301. case '\'':
  2302. if (st->instance_id) {
  2303. data->u.str = (char*)idb_get(instance[st->instance_id].vars,reference_getuid(data));
  2304. } else {
  2305. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2306. data->u.str = NULL;
  2307. }
  2308. break;
  2309. default:
  2310. data->u.str = pc_readglobalreg_str(sd, name);
  2311. break;
  2312. }
  2313. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2314. {// empty string
  2315. data->type = C_CONSTSTR;
  2316. data->u.str = "";
  2317. }
  2318. else
  2319. {// duplicate string
  2320. data->type = C_STR;
  2321. data->u.str = aStrdup(data->u.str);
  2322. }
  2323. }
  2324. else
  2325. {// integer variable
  2326. data->type = C_INT;
  2327. if( reference_toconstant(data) )
  2328. {
  2329. data->u.num = reference_getconstant(data);
  2330. }
  2331. else if( reference_toparam(data) )
  2332. {
  2333. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2334. }
  2335. else
  2336. switch( prefix )
  2337. {
  2338. case '@':
  2339. data->u.num = pc_readreg(sd, data->u.num);
  2340. break;
  2341. case '$':
  2342. data->u.num = mapreg_readreg(data->u.num);
  2343. break;
  2344. case '#':
  2345. if( name[1] == '#' )
  2346. data->u.num = pc_readaccountreg2(sd, name);// global
  2347. else
  2348. data->u.num = pc_readaccountreg(sd, name);// local
  2349. break;
  2350. case '.':
  2351. {
  2352. struct DBMap* n =
  2353. data->ref ? *data->ref:
  2354. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2355. st->script->script_vars;// npc variable
  2356. if( n )
  2357. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2358. else
  2359. data->u.num = 0;
  2360. }
  2361. break;
  2362. case '\'':
  2363. if( st->instance_id )
  2364. data->u.num = (int)idb_iget(instance[st->instance_id].vars,reference_getuid(data));
  2365. else {
  2366. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2367. data->u.num = 0;
  2368. }
  2369. break;
  2370. default:
  2371. data->u.num = pc_readglobalreg(sd, name);
  2372. break;
  2373. }
  2374. }
  2375. return;
  2376. }
  2377. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2378. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2379. /// The value is left in the top of the stack and needs to be removed manually.
  2380. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2381. {
  2382. struct script_data* data;
  2383. push_val2(st->stack, C_NAME, uid, ref);
  2384. data = script_getdatatop(st, -1);
  2385. get_val(st, data);
  2386. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2387. }
  2388. /*==========================================
  2389. * Stores the value of a script variable
  2390. * Return value is 0 on fail, 1 on success.
  2391. *------------------------------------------*/
  2392. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2393. {
  2394. char prefix = name[0];
  2395. if( is_string_variable(name) )
  2396. {// string variable
  2397. const char* str = (const char*)value;
  2398. switch (prefix) {
  2399. case '@':
  2400. return pc_setregstr(sd, num, str);
  2401. case '$':
  2402. return mapreg_setregstr(num, str);
  2403. case '#':
  2404. return (name[1] == '#') ?
  2405. pc_setaccountreg2str(sd, name, str) :
  2406. pc_setaccountregstr(sd, name, str);
  2407. case '.':
  2408. {
  2409. struct DBMap* n;
  2410. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2411. if( n ) {
  2412. idb_remove(n, num);
  2413. if (str[0]) idb_put(n, num, aStrdup(str));
  2414. }
  2415. }
  2416. return 1;
  2417. case '\'':
  2418. if( st->instance_id ) {
  2419. idb_remove(instance[st->instance_id].vars, num);
  2420. if( str[0] ) idb_put(instance[st->instance_id].vars, num, aStrdup(str));
  2421. }
  2422. return 1;
  2423. default:
  2424. return pc_setglobalreg_str(sd, name, str);
  2425. }
  2426. }
  2427. else
  2428. {// integer variable
  2429. int val = (int)__64BPRTSIZE(value);
  2430. if(str_data[num&0x00ffffff].type == C_PARAM)
  2431. {
  2432. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2433. {
  2434. if( st != NULL )
  2435. {
  2436. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2437. script_reportsrc(st);
  2438. st->state = END;
  2439. }
  2440. return 0;
  2441. }
  2442. return 1;
  2443. }
  2444. switch (prefix) {
  2445. case '@':
  2446. return pc_setreg(sd, num, val);
  2447. case '$':
  2448. return mapreg_setreg(num, val);
  2449. case '#':
  2450. return (name[1] == '#') ?
  2451. pc_setaccountreg2(sd, name, val) :
  2452. pc_setaccountreg(sd, name, val);
  2453. case '.':
  2454. {
  2455. struct DBMap* n;
  2456. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2457. if( n ) {
  2458. idb_remove(n, num);
  2459. if( val != 0 )
  2460. idb_iput(n, num, val);
  2461. }
  2462. }
  2463. return 1;
  2464. case '\'':
  2465. if( st->instance_id ) {
  2466. idb_remove(instance[st->instance_id].vars, num);
  2467. if( val != 0 )
  2468. idb_iput(instance[st->instance_id].vars, num, val);
  2469. }
  2470. return 1;
  2471. default:
  2472. return pc_setglobalreg(sd, name, val);
  2473. }
  2474. }
  2475. }
  2476. int set_var(TBL_PC* sd, char* name, void* val)
  2477. {
  2478. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2479. }
  2480. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2481. {
  2482. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2483. }
  2484. /// Converts the data to a string
  2485. const char* conv_str(struct script_state* st, struct script_data* data)
  2486. {
  2487. char* p;
  2488. get_val(st, data);
  2489. if( data_isstring(data) )
  2490. {// nothing to convert
  2491. }
  2492. else if( data_isint(data) )
  2493. {// int -> string
  2494. CREATE(p, char, ITEM_NAME_LENGTH);
  2495. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2496. p[ITEM_NAME_LENGTH-1] = '\0';
  2497. data->type = C_STR;
  2498. data->u.str = p;
  2499. }
  2500. else if( data_isreference(data) )
  2501. {// reference -> string
  2502. //##TODO when does this happen (check get_val) [FlavioJS]
  2503. data->type = C_CONSTSTR;
  2504. data->u.str = reference_getname(data);
  2505. }
  2506. else
  2507. {// unsupported data type
  2508. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2509. script_reportdata(data);
  2510. script_reportsrc(st);
  2511. data->type = C_CONSTSTR;
  2512. data->u.str = "";
  2513. }
  2514. return data->u.str;
  2515. }
  2516. /// Converts the data to an int
  2517. int conv_num(struct script_state* st, struct script_data* data)
  2518. {
  2519. char* p;
  2520. long num;
  2521. get_val(st, data);
  2522. if( data_isint(data) )
  2523. {// nothing to convert
  2524. }
  2525. else if( data_isstring(data) )
  2526. {// string -> int
  2527. // the result does not overflow or underflow, it is capped instead
  2528. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2529. p = data->u.str;
  2530. errno = 0;
  2531. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2532. if( errno == ERANGE
  2533. #if LONG_MAX > INT_MAX
  2534. || num < INT_MIN || num > INT_MAX
  2535. #endif
  2536. )
  2537. {
  2538. if( num <= INT_MIN )
  2539. {
  2540. num = INT_MIN;
  2541. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2542. }
  2543. else//if( num >= INT_MAX )
  2544. {
  2545. num = INT_MAX;
  2546. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2547. }
  2548. script_reportdata(data);
  2549. script_reportsrc(st);
  2550. }
  2551. if( data->type == C_STR )
  2552. aFree(p);
  2553. data->type = C_INT;
  2554. data->u.num = (int)num;
  2555. }
  2556. #if 0
  2557. // FIXME this function is being used to retrieve the position of labels and
  2558. // probably other stuff [FlavioJS]
  2559. else
  2560. {// unsupported data type
  2561. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2562. script_reportdata(data);
  2563. script_reportsrc(st);
  2564. data->type = C_INT;
  2565. data->u.num = 0;
  2566. }
  2567. #endif
  2568. return data->u.num;
  2569. }
  2570. //
  2571. // Stack operations
  2572. //
  2573. /// Increases the size of the stack
  2574. void stack_expand(struct script_stack* stack)
  2575. {
  2576. stack->sp_max += 64;
  2577. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2578. stack->sp_max * sizeof(stack->stack_data[0]) );
  2579. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2580. 64 * sizeof(stack->stack_data[0]) );
  2581. }
  2582. /// Pushes a value into the stack
  2583. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2584. /// Pushes a value into the stack (with reference)
  2585. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2586. {
  2587. if( stack->sp >= stack->sp_max )
  2588. stack_expand(stack);
  2589. stack->stack_data[stack->sp].type = type;
  2590. stack->stack_data[stack->sp].u.num = val;
  2591. stack->stack_data[stack->sp].ref = ref;
  2592. stack->sp++;
  2593. return &stack->stack_data[stack->sp-1];
  2594. }
  2595. /// Pushes a string into the stack
  2596. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2597. {
  2598. if( stack->sp >= stack->sp_max )
  2599. stack_expand(stack);
  2600. stack->stack_data[stack->sp].type = type;
  2601. stack->stack_data[stack->sp].u.str = str;
  2602. stack->stack_data[stack->sp].ref = NULL;
  2603. stack->sp++;
  2604. return &stack->stack_data[stack->sp-1];
  2605. }
  2606. /// Pushes a retinfo into the stack
  2607. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2608. {
  2609. if( stack->sp >= stack->sp_max )
  2610. stack_expand(stack);
  2611. stack->stack_data[stack->sp].type = C_RETINFO;
  2612. stack->stack_data[stack->sp].u.ri = ri;
  2613. stack->stack_data[stack->sp].ref = ref;
  2614. stack->sp++;
  2615. return &stack->stack_data[stack->sp-1];
  2616. }
  2617. /// Pushes a copy of the target position into the stack
  2618. struct script_data* push_copy(struct script_stack* stack, int pos)
  2619. {
  2620. switch( stack->stack_data[pos].type )
  2621. {
  2622. case C_CONSTSTR:
  2623. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2624. break;
  2625. case C_STR:
  2626. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2627. break;
  2628. case C_RETINFO:
  2629. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2630. exit(1);
  2631. break;
  2632. default:
  2633. return push_val2(
  2634. stack,stack->stack_data[pos].type,
  2635. stack->stack_data[pos].u.num,
  2636. stack->stack_data[pos].ref
  2637. );
  2638. break;
  2639. }
  2640. }
  2641. /// Removes the values in indexes [start,end[ from the stack.
  2642. /// Adjusts all stack pointers.
  2643. void pop_stack(struct script_state* st, int start, int end)
  2644. {
  2645. struct script_stack* stack = st->stack;
  2646. struct script_data* data;
  2647. int i;
  2648. if( start < 0 )
  2649. start = 0;
  2650. if( end > stack->sp )
  2651. end = stack->sp;
  2652. if( start >= end )
  2653. return;// nothing to pop
  2654. // free stack elements
  2655. for( i = start; i < end; i++ )
  2656. {
  2657. data = &stack->stack_data[i];
  2658. if( data->type == C_STR )
  2659. aFree(data->u.str);
  2660. if( data->type == C_RETINFO )
  2661. {
  2662. struct script_retinfo* ri = data->u.ri;
  2663. if( ri->var_function )
  2664. script_free_vars(ri->var_function);
  2665. if( data->ref )
  2666. aFree(data->ref);
  2667. aFree(ri);
  2668. }
  2669. data->type = C_NOP;
  2670. }
  2671. // move the rest of the elements
  2672. if( stack->sp > end )
  2673. {
  2674. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2675. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2676. stack->stack_data[i].type = C_NOP;
  2677. }
  2678. // adjust stack pointers
  2679. if( st->start > end ) st->start -= end - start;
  2680. else if( st->start > start ) st->start = start;
  2681. if( st->end > end ) st->end -= end - start;
  2682. else if( st->end > start ) st->end = start;
  2683. if( stack->defsp > end ) stack->defsp -= end - start;
  2684. else if( stack->defsp > start ) stack->defsp = start;
  2685. stack->sp -= end - start;
  2686. }
  2687. ///
  2688. ///
  2689. ///
  2690. /*==========================================
  2691. * スクリプト依存変数、関数依存変数の解放
  2692. *------------------------------------------*/
  2693. void script_free_vars(struct DBMap* storage)
  2694. {
  2695. if( storage )
  2696. {// destroy the storage construct containing the variables
  2697. db_destroy(storage);
  2698. }
  2699. }
  2700. void script_free_code(struct script_code* code)
  2701. {
  2702. script_free_vars( code->script_vars );
  2703. aFree( code->script_buf );
  2704. aFree( code );
  2705. }
  2706. /// Creates a new script state.
  2707. ///
  2708. /// @param script Script code
  2709. /// @param pos Position in the code
  2710. /// @param rid Who is running the script (attached player)
  2711. /// @param oid Where the code is being run (npc 'object')
  2712. /// @return Script state
  2713. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2714. {
  2715. struct script_state* st;
  2716. CREATE(st, struct script_state, 1);
  2717. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2718. st->stack->sp = 0;
  2719. st->stack->sp_max = 64;
  2720. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2721. st->stack->defsp = st->stack->sp;
  2722. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2723. st->state = RUN;
  2724. st->script = script;
  2725. //st->scriptroot = script;
  2726. st->pos = pos;
  2727. st->rid = rid;
  2728. st->oid = oid;
  2729. st->sleep.timer = INVALID_TIMER;
  2730. return st;
  2731. }
  2732. /// Frees a script state.
  2733. ///
  2734. /// @param st Script state
  2735. void script_free_state(struct script_state* st)
  2736. {
  2737. if(st->bk_st)
  2738. {// backup was not restored
  2739. ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  2740. }
  2741. if( st->sleep.timer != INVALID_TIMER )
  2742. delete_timer(st->sleep.timer, run_script_timer);
  2743. script_free_vars(st->stack->var_function);
  2744. pop_stack(st, 0, st->stack->sp);
  2745. aFree(st->stack->stack_data);
  2746. aFree(st->stack);
  2747. st->pos = -1;
  2748. aFree(st);
  2749. }
  2750. //
  2751. // 実行部main
  2752. //
  2753. /*==========================================
  2754. * コマンドの読み取り
  2755. *------------------------------------------*/
  2756. c_op get_com(unsigned char *script,int *pos)
  2757. {
  2758. int i = 0, j = 0;
  2759. if(script[*pos]>=0x80){
  2760. return C_INT;
  2761. }
  2762. while(script[*pos]>=0x40){
  2763. i=script[(*pos)++]<<j;
  2764. j+=6;
  2765. }
  2766. return (c_op)(i+(script[(*pos)++]<<j));
  2767. }
  2768. /*==========================================
  2769. * 数値の所得
  2770. *------------------------------------------*/
  2771. int get_num(unsigned char *script,int *pos)
  2772. {
  2773. int i,j;
  2774. i=0; j=0;
  2775. while(script[*pos]>=0xc0){
  2776. i+=(script[(*pos)++]&0x7f)<<j;
  2777. j+=6;
  2778. }
  2779. return i+((script[(*pos)++]&0x7f)<<j);
  2780. }
  2781. /*==========================================
  2782. * スタックから値を取り出す
  2783. *------------------------------------------*/
  2784. int pop_val(struct script_state* st)
  2785. {
  2786. if(st->stack->sp<=0)
  2787. return 0;
  2788. st->stack->sp--;
  2789. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2790. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2791. return st->stack->stack_data[st->stack->sp].u.num;
  2792. return 0;
  2793. }
  2794. /// Ternary operators
  2795. /// test ? if_true : if_false
  2796. void op_3(struct script_state* st, int op)
  2797. {
  2798. struct script_data* data;
  2799. int flag = 0;
  2800. data = script_getdatatop(st, -3);
  2801. get_val(st, data);
  2802. if( data_isstring(data) )
  2803. flag = data->u.str[0];// "" -> false
  2804. else if( data_isint(data) )
  2805. flag = data->u.num;// 0 -> false
  2806. else
  2807. {
  2808. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2809. script_reportdata(data);
  2810. script_reportsrc(st);
  2811. script_removetop(st, -3, 0);
  2812. script_pushnil(st);
  2813. return;
  2814. }
  2815. if( flag )
  2816. script_pushcopytop(st, -2);
  2817. else
  2818. script_pushcopytop(st, -1);
  2819. script_removetop(st, -4, -1);
  2820. }
  2821. /// Binary string operators
  2822. /// s1 EQ s2 -> i
  2823. /// s1 NE s2 -> i
  2824. /// s1 GT s2 -> i
  2825. /// s1 GE s2 -> i
  2826. /// s1 LT s2 -> i
  2827. /// s1 LE s2 -> i
  2828. /// s1 ADD s2 -> s
  2829. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2830. {
  2831. int a = 0;
  2832. switch(op){
  2833. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2834. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2835. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2836. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2837. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2838. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2839. case C_ADD:
  2840. {
  2841. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2842. strcpy(buf, s1);
  2843. strcat(buf, s2);
  2844. script_pushstr(st, buf);
  2845. return;
  2846. }
  2847. default:
  2848. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2849. script_reportsrc(st);
  2850. script_pushnil(st);
  2851. st->state = END;
  2852. return;
  2853. }
  2854. script_pushint(st,a);
  2855. }
  2856. /// Binary number operators
  2857. /// i OP i -> i
  2858. void op_2num(struct script_state* st, int op, int i1, int i2)
  2859. {
  2860. int ret;
  2861. double ret_double;
  2862. switch( op )
  2863. {
  2864. case C_AND: ret = i1 & i2; break;
  2865. case C_OR: ret = i1 | i2; break;
  2866. case C_XOR: ret = i1 ^ i2; break;
  2867. case C_LAND: ret = (i1 && i2); break;
  2868. case C_LOR: ret = (i1 || i2); break;
  2869. case C_EQ: ret = (i1 == i2); break;
  2870. case C_NE: ret = (i1 != i2); break;
  2871. case C_GT: ret = (i1 > i2); break;
  2872. case C_GE: ret = (i1 >= i2); break;
  2873. case C_LT: ret = (i1 < i2); break;
  2874. case C_LE: ret = (i1 <= i2); break;
  2875. case C_R_SHIFT: ret = i1>>i2; break;
  2876. case C_L_SHIFT: ret = i1<<i2; break;
  2877. case C_DIV:
  2878. case C_MOD:
  2879. if( i2 == 0 )
  2880. {
  2881. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2882. script_reportsrc(st);
  2883. script_pushnil(st);
  2884. st->state = END;
  2885. return;
  2886. }
  2887. else if( op == C_DIV )
  2888. ret = i1 / i2;
  2889. else//if( op == C_MOD )
  2890. ret = i1 % i2;
  2891. break;
  2892. default:
  2893. switch( op )
  2894. {// operators that can overflow/underflow
  2895. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2896. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2897. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2898. default:
  2899. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2900. script_reportsrc(st);
  2901. script_pushnil(st);
  2902. return;
  2903. }
  2904. if( ret_double < (double)INT_MIN )
  2905. {
  2906. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2907. script_reportsrc(st);
  2908. ret = INT_MIN;
  2909. }
  2910. else if( ret_double > (double)INT_MAX )
  2911. {
  2912. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2913. script_reportsrc(st);
  2914. ret = INT_MAX;
  2915. }
  2916. }
  2917. script_pushint(st, ret);
  2918. }
  2919. /// Binary operators
  2920. void op_2(struct script_state *st, int op)
  2921. {
  2922. struct script_data* left, leftref;
  2923. struct script_data* right;
  2924. leftref.type = C_NOP;
  2925. left = script_getdatatop(st, -2);
  2926. right = script_getdatatop(st, -1);
  2927. if (st->op2ref)
  2928. {
  2929. if (data_isreference(left))
  2930. leftref = *left;
  2931. st->op2ref = 0;
  2932. }
  2933. get_val(st, left);
  2934. get_val(st, right);
  2935. // automatic conversions
  2936. switch( op )
  2937. {
  2938. case C_ADD:
  2939. if( data_isint(left) && data_isstring(right) )
  2940. {// convert int-string to string-string
  2941. conv_str(st, left);
  2942. }
  2943. else if( data_isstring(left) && data_isint(right) )
  2944. {// convert string-int to string-string
  2945. conv_str(st, right);
  2946. }
  2947. break;
  2948. }
  2949. if( data_isstring(left) && data_isstring(right) )
  2950. {// ss => op_2str
  2951. op_2str(st, op, left->u.str, right->u.str);
  2952. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  2953. if (leftref.type != C_NOP)
  2954. {
  2955. aFree(left->u.str);
  2956. *left = leftref;
  2957. }
  2958. }
  2959. else if( data_isint(left) && data_isint(right) )
  2960. {// ii => op_2num
  2961. int i1 = left->u.num;
  2962. int i2 = right->u.num;
  2963. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  2964. op_2num(st, op, i1, i2);
  2965. if (leftref.type != C_NOP)
  2966. *left = leftref;
  2967. }
  2968. else
  2969. {// invalid argument
  2970. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2971. script_reportdata(left);
  2972. script_reportdata(right);
  2973. script_reportsrc(st);
  2974. script_removetop(st, -2, 0);
  2975. script_pushnil(st);
  2976. st->state = END;
  2977. }
  2978. }
  2979. /// Unary operators
  2980. /// NEG i -> i
  2981. /// NOT i -> i
  2982. /// LNOT i -> i
  2983. void op_1(struct script_state* st, int op)
  2984. {
  2985. struct script_data* data;
  2986. int i1;
  2987. data = script_getdatatop(st, -1);
  2988. get_val(st, data);
  2989. if( !data_isint(data) )
  2990. {// not a number
  2991. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2992. script_reportdata(data);
  2993. script_reportsrc(st);
  2994. script_pushnil(st);
  2995. st->state = END;
  2996. return;
  2997. }
  2998. i1 = data->u.num;
  2999. script_removetop(st, -1, 0);
  3000. switch( op )
  3001. {
  3002. case C_NEG: i1 = -i1; break;
  3003. case C_NOT: i1 = ~i1; break;
  3004. case C_LNOT: i1 = !i1; break;
  3005. default:
  3006. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3007. script_reportsrc(st);
  3008. script_pushnil(st);
  3009. st->state = END;
  3010. return;
  3011. }
  3012. script_pushint(st, i1);
  3013. }
  3014. /// Checks the type of all arguments passed to a built-in function.
  3015. ///
  3016. /// @param st Script state whose stack arguments should be inspected.
  3017. /// @param func Built-in function for which the arguments are intended.
  3018. static void script_check_buildin_argtype(struct script_state* st, int func)
  3019. {
  3020. char type;
  3021. int idx, invalid = 0;
  3022. script_function* sf = &buildin_func[str_data[func].val];
  3023. for( idx = 2; script_hasdata(st, idx); idx++ )
  3024. {
  3025. struct script_data* data = script_getdata(st, idx);
  3026. type = sf->arg[idx-2];
  3027. if( type == '?' || type == '*' )
  3028. {// optional argument or unknown number of optional parameters ( no types are after this )
  3029. break;
  3030. }
  3031. else if( type == 0 )
  3032. {// more arguments than necessary ( should not happen, as it is checked before )
  3033. ShowWarning("Found more arguments than necessary.\n");
  3034. invalid++;
  3035. break;
  3036. }
  3037. else
  3038. {
  3039. const char* name = NULL;
  3040. if( data_isreference(data) )
  3041. {// get name for variables to determine the type they refer to
  3042. name = reference_getname(data);
  3043. }
  3044. switch( type )
  3045. {
  3046. case 'v':
  3047. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3048. {// variant
  3049. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3050. script_reportdata(data);
  3051. invalid++;
  3052. }
  3053. break;
  3054. case 's':
  3055. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3056. {// string
  3057. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3058. script_reportdata(data);
  3059. invalid++;
  3060. }
  3061. break;
  3062. case 'i':
  3063. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3064. {// int ( params and constants are always int )
  3065. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3066. script_reportdata(data);
  3067. invalid++;
  3068. }
  3069. break;
  3070. case 'r':
  3071. if( !data_isreference(data) )
  3072. {// variables
  3073. ShowWarning("Unexpected type for argument %d. Expected variable.\n", idx-1);
  3074. script_reportdata(data);
  3075. invalid++;
  3076. }
  3077. break;
  3078. case 'l':
  3079. if( !data_islabel(data) && !data_isfunclabel(data) )
  3080. {// label
  3081. ShowWarning("Unexpected type for argument %d. Expected label.\n", idx-1);
  3082. script_reportdata(data);
  3083. invalid++;
  3084. }
  3085. break;
  3086. }
  3087. }
  3088. }
  3089. if(invalid)
  3090. {
  3091. ShowDebug("Function: %s\n", get_str(func));
  3092. script_reportsrc(st);
  3093. }
  3094. }
  3095. /// Executes a buildin command.
  3096. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3097. int run_func(struct script_state *st)
  3098. {
  3099. struct script_data* data;
  3100. int i,start_sp,end_sp,func;
  3101. end_sp = st->stack->sp;// position after the last argument
  3102. for( i = end_sp-1; i > 0 ; --i )
  3103. if( st->stack->stack_data[i].type == C_ARG )
  3104. break;
  3105. if( i == 0 )
  3106. {
  3107. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3108. st->state = END;
  3109. script_reportsrc(st);
  3110. return 1;
  3111. }
  3112. start_sp = i-1;// C_NAME of the command
  3113. st->start = start_sp;
  3114. st->end = end_sp;
  3115. data = &st->stack->stack_data[st->start];
  3116. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  3117. func = data->u.num;
  3118. else
  3119. {
  3120. ShowError("script:run_func: not a buildin command.\n");
  3121. script_reportdata(data);
  3122. script_reportsrc(st);
  3123. st->state = END;
  3124. return 1;
  3125. }
  3126. if( script_config.warn_func_mismatch_argtypes )
  3127. {
  3128. script_check_buildin_argtype(st, func);
  3129. }
  3130. if(str_data[func].func){
  3131. if (str_data[func].func(st)) //Report error
  3132. script_reportsrc(st);
  3133. } else {
  3134. ShowError("script:run_func: '%s' (id=%d type=%s) has no C function. please report this!!!\n", get_str(func), func, script_op2name(str_data[func].type));
  3135. script_reportsrc(st);
  3136. st->state = END;
  3137. }
  3138. // Stack's datum are used when re-running functions [Eoe]
  3139. if( st->state == RERUNLINE )
  3140. return 0;
  3141. pop_stack(st, st->start, st->end);
  3142. if( st->state == RETFUNC )
  3143. {// return from a user-defined function
  3144. struct script_retinfo* ri;
  3145. int olddefsp = st->stack->defsp;
  3146. int nargs;
  3147. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3148. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3149. {
  3150. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3151. script_reportsrc(st);
  3152. st->state = END;
  3153. return 1;
  3154. }
  3155. script_free_vars( st->stack->var_function );
  3156. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3157. nargs = ri->nargs;
  3158. st->pos = ri->pos;
  3159. st->script = ri->script;
  3160. st->stack->var_function = ri->var_function;
  3161. st->stack->defsp = ri->defsp;
  3162. memset(ri, 0, sizeof(struct script_retinfo));
  3163. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3164. st->state = GOTO;
  3165. }
  3166. return 0;
  3167. }
  3168. /*==========================================
  3169. * script execution
  3170. *------------------------------------------*/
  3171. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3172. {
  3173. struct script_state *st;
  3174. if( rootscript == NULL || pos < 0 )
  3175. return;
  3176. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3177. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3178. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3179. st = script_alloc_state(rootscript, pos, rid, oid);
  3180. run_script_main(st);
  3181. }
  3182. void script_stop_sleeptimers(int id)
  3183. {
  3184. struct script_state* st;
  3185. for(;;)
  3186. {
  3187. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3188. if( st == NULL )
  3189. break; // no more sleep timers
  3190. script_free_state(st);
  3191. }
  3192. }
  3193. /*==========================================
  3194. * 指定ノードをsleep_dbから削除
  3195. *------------------------------------------*/
  3196. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3197. {
  3198. struct linkdb_node *retnode;
  3199. if( n == NULL)
  3200. return NULL;
  3201. if( n->prev == NULL )
  3202. sleep_db = n->next;
  3203. else
  3204. n->prev->next = n->next;
  3205. if( n->next )
  3206. n->next->prev = n->prev;
  3207. retnode = n->next;
  3208. aFree( n );
  3209. return retnode; // 次のノードを返す
  3210. }
  3211. /*==========================================
  3212. * sleep用タイマー関数
  3213. *------------------------------------------*/
  3214. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3215. {
  3216. struct script_state *st = (struct script_state *)data;
  3217. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3218. TBL_PC *sd = map_id2sd(st->rid);
  3219. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3220. { //Character mismatch. Cancel execution.
  3221. st->rid = 0;
  3222. st->state = END;
  3223. }
  3224. while( node && st->sleep.timer != INVALID_TIMER ) {
  3225. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3226. script_erase_sleepdb(node);
  3227. st->sleep.timer = INVALID_TIMER;
  3228. break;
  3229. }
  3230. node = node->next;
  3231. }
  3232. if(st->state != RERUNLINE)
  3233. st->sleep.tick = 0;
  3234. run_script_main(st);
  3235. return 0;
  3236. }
  3237. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3238. ///
  3239. /// @param st Script state to detach.
  3240. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3241. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3242. {
  3243. struct map_session_data* sd;
  3244. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3245. {
  3246. sd->st = st->bk_st;
  3247. sd->npc_id = st->bk_npcid;
  3248. /**
  3249. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3250. **/
  3251. #if SECURE_NPCTIMEOUT
  3252. /**
  3253. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3254. **/
  3255. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3256. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3257. sd->npc_idle_timer = INVALID_TIMER;
  3258. }
  3259. #endif
  3260. if(st->bk_st)
  3261. {
  3262. //Remove tag for removal.
  3263. st->bk_st = NULL;
  3264. st->bk_npcid = 0;
  3265. }
  3266. else if(dequeue_event)
  3267. {
  3268. npc_event_dequeue(sd);
  3269. }
  3270. }
  3271. else if(st->bk_st)
  3272. {// rid was set to 0, before detaching the script state
  3273. ShowError("script_detach_state: Found previous script state without attached player (rid=%d, oid=%d, state=%d, bk_npcid=%d)\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3274. script_reportsrc(st->bk_st);
  3275. script_free_state(st->bk_st);
  3276. st->bk_st = NULL;
  3277. }
  3278. }
  3279. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3280. ///
  3281. /// @param st Script state to attach.
  3282. static void script_attach_state(struct script_state* st)
  3283. {
  3284. struct map_session_data* sd;
  3285. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3286. {
  3287. if(st!=sd->st)
  3288. {
  3289. if(st->bk_st)
  3290. {// there is already a backup
  3291. ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3292. }
  3293. st->bk_st = sd->st;
  3294. st->bk_npcid = sd->npc_id;
  3295. }
  3296. sd->st = st;
  3297. sd->npc_id = st->oid;
  3298. /**
  3299. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3300. **/
  3301. #if SECURE_NPCTIMEOUT
  3302. if( sd->npc_idle_timer == INVALID_TIMER )
  3303. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3304. sd->npc_idle_tick = gettick();
  3305. #endif
  3306. }
  3307. }
  3308. /*==========================================
  3309. * スクリプトの実行メイン部分
  3310. *------------------------------------------*/
  3311. void run_script_main(struct script_state *st)
  3312. {
  3313. int cmdcount=script_config.check_cmdcount;
  3314. int gotocount=script_config.check_gotocount;
  3315. TBL_PC *sd;
  3316. struct script_stack *stack=st->stack;
  3317. struct npc_data *nd;
  3318. script_attach_state(st);
  3319. nd = map_id2nd(st->oid);
  3320. if( nd && map[nd->bl.m].instance_id > 0 )
  3321. st->instance_id = map[nd->bl.m].instance_id;
  3322. if(st->state == RERUNLINE) {
  3323. run_func(st);
  3324. if(st->state == GOTO)
  3325. st->state = RUN;
  3326. } else if(st->state != END)
  3327. st->state = RUN;
  3328. while(st->state == RUN)
  3329. {
  3330. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3331. switch(c){
  3332. case C_EOL:
  3333. if( stack->defsp > stack->sp )
  3334. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3335. else
  3336. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3337. break;
  3338. case C_INT:
  3339. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3340. break;
  3341. case C_POS:
  3342. case C_NAME:
  3343. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3344. st->pos+=3;
  3345. break;
  3346. case C_ARG:
  3347. push_val(stack,c,0);
  3348. break;
  3349. case C_STR:
  3350. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3351. while(st->script->script_buf[st->pos++]);
  3352. break;
  3353. case C_FUNC:
  3354. run_func(st);
  3355. if(st->state==GOTO){
  3356. st->state = RUN;
  3357. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3358. ShowError("run_script: infinity loop !\n");
  3359. script_reportsrc(st);
  3360. st->state=END;
  3361. }
  3362. }
  3363. break;
  3364. case C_REF:
  3365. st->op2ref = 1;
  3366. break;
  3367. case C_NEG:
  3368. case C_NOT:
  3369. case C_LNOT:
  3370. op_1(st ,c);
  3371. break;
  3372. case C_ADD:
  3373. case C_SUB:
  3374. case C_MUL:
  3375. case C_DIV:
  3376. case C_MOD:
  3377. case C_EQ:
  3378. case C_NE:
  3379. case C_GT:
  3380. case C_GE:
  3381. case C_LT:
  3382. case C_LE:
  3383. case C_AND:
  3384. case C_OR:
  3385. case C_XOR:
  3386. case C_LAND:
  3387. case C_LOR:
  3388. case C_R_SHIFT:
  3389. case C_L_SHIFT:
  3390. op_2(st, c);
  3391. break;
  3392. case C_OP3:
  3393. op_3(st, c);
  3394. break;
  3395. case C_NOP:
  3396. st->state=END;
  3397. break;
  3398. default:
  3399. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3400. st->state=END;
  3401. break;
  3402. }
  3403. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3404. ShowError("run_script: infinity loop !\n");
  3405. script_reportsrc(st);
  3406. st->state=END;
  3407. }
  3408. }
  3409. if(st->sleep.tick > 0) {
  3410. //Restore previous script
  3411. script_detach_state(st, false);
  3412. //Delay execution
  3413. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3414. st->sleep.charid = sd?sd->status.char_id:0;
  3415. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3416. run_script_timer, st->sleep.charid, (intptr_t)st);
  3417. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3418. }
  3419. else if(st->state != END && st->rid){
  3420. //Resume later (st is already attached to player).
  3421. if(st->bk_st) {
  3422. ShowWarning("Unable to restore stack! Double continuation!\n");
  3423. //Report BOTH scripts to see if that can help somehow.
  3424. ShowDebug("Previous script (lost):\n");
  3425. script_reportsrc(st->bk_st);
  3426. ShowDebug("Current script:\n");
  3427. script_reportsrc(st);
  3428. script_free_state(st->bk_st);
  3429. st->bk_st = NULL;
  3430. }
  3431. } else {
  3432. //Dispose of script.
  3433. if ((sd = map_id2sd(st->rid))!=NULL)
  3434. { //Restore previous stack and save char.
  3435. if(sd->state.using_fake_npc){
  3436. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3437. sd->state.using_fake_npc = 0;
  3438. }
  3439. //Restore previous script if any.
  3440. script_detach_state(st, true);
  3441. if (sd->state.reg_dirty&2)
  3442. intif_saveregistry(sd,2);
  3443. if (sd->state.reg_dirty&1)
  3444. intif_saveregistry(sd,1);
  3445. }
  3446. script_free_state(st);
  3447. st = NULL;
  3448. }
  3449. }
  3450. int script_config_read(char *cfgName)
  3451. {
  3452. int i;
  3453. char line[1024],w1[1024],w2[1024];
  3454. FILE *fp;
  3455. fp=fopen(cfgName,"r");
  3456. if(fp==NULL){
  3457. ShowError("File not found: %s\n", cfgName);
  3458. return 1;
  3459. }
  3460. while(fgets(line, sizeof(line), fp))
  3461. {
  3462. if(line[0] == '/' && line[1] == '/')
  3463. continue;
  3464. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3465. if(i!=2)
  3466. continue;
  3467. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3468. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3469. }
  3470. else if(strcmpi(w1,"check_cmdcount")==0) {
  3471. script_config.check_cmdcount = config_switch(w2);
  3472. }
  3473. else if(strcmpi(w1,"check_gotocount")==0) {
  3474. script_config.check_gotocount = config_switch(w2);
  3475. }
  3476. else if(strcmpi(w1,"input_min_value")==0) {
  3477. script_config.input_min_value = config_switch(w2);
  3478. }
  3479. else if(strcmpi(w1,"input_max_value")==0) {
  3480. script_config.input_max_value = config_switch(w2);
  3481. }
  3482. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3483. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3484. }
  3485. else if(strcmpi(w1,"import")==0){
  3486. script_config_read(w2);
  3487. }
  3488. else {
  3489. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3490. }
  3491. }
  3492. fclose(fp);
  3493. return 0;
  3494. }
  3495. /**
  3496. * @see DBApply
  3497. */
  3498. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3499. {
  3500. struct script_code *code = db_data2ptr(data);
  3501. if (code)
  3502. script_free_code(code);
  3503. return 0;
  3504. }
  3505. void script_run_autobonus(const char *autobonus, int id, int pos)
  3506. {
  3507. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3508. if( script )
  3509. {
  3510. current_equip_item_index = pos;
  3511. run_script(script,0,id,0);
  3512. }
  3513. }
  3514. void script_add_autobonus(const char *autobonus)
  3515. {
  3516. if( strdb_get(autobonus_db, autobonus) == NULL )
  3517. {
  3518. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3519. if( script )
  3520. strdb_put(autobonus_db, autobonus, script);
  3521. }
  3522. }
  3523. /// resets a temporary character array variable to given value
  3524. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3525. {
  3526. int key;
  3527. uint8 idx;
  3528. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3529. {
  3530. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3531. return;
  3532. }
  3533. key = add_str(varname);
  3534. if( is_string_variable(varname) )
  3535. {
  3536. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3537. {
  3538. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3539. }
  3540. }
  3541. else
  3542. {
  3543. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3544. {
  3545. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3546. }
  3547. }
  3548. }
  3549. /// sets a temporary character array variable element idx to given value
  3550. /// @param refcache Pointer to an int variable, which keeps a copy of the reference to varname and must be initialized to 0. Can be NULL if only one element is set.
  3551. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3552. {
  3553. int key;
  3554. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3555. {
  3556. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3557. return;
  3558. }
  3559. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3560. {
  3561. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3562. return;
  3563. }
  3564. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3565. if( is_string_variable(varname) )
  3566. {
  3567. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3568. }
  3569. else
  3570. {
  3571. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3572. }
  3573. if( refcache )
  3574. {// save to avoid repeated add_str calls
  3575. refcache[0] = key;
  3576. }
  3577. }
  3578. /*==========================================
  3579. * 終了
  3580. *------------------------------------------*/
  3581. int do_final_script()
  3582. {
  3583. #ifdef DEBUG_HASH
  3584. if (battle_config.etc_log)
  3585. {
  3586. FILE *fp = fopen("hash_dump.txt","wt");
  3587. if(fp) {
  3588. int i,count[SCRIPT_HASH_SIZE];
  3589. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3590. int n=0;
  3591. int min=INT_MAX,max=0,zero=0;
  3592. double mean=0.0f;
  3593. double median=0.0f;
  3594. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3595. memset(count, 0, sizeof(count));
  3596. fprintf(fp,"num : hash : data_name\n");
  3597. fprintf(fp,"---------------------------------------------------------------\n");
  3598. for(i=LABEL_START; i<str_num; i++) {
  3599. unsigned int h = calc_hash(get_str(i));
  3600. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3601. ++count[h];
  3602. }
  3603. fprintf(fp,"--------------------\n\n");
  3604. memset(count2, 0, sizeof(count2));
  3605. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3606. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3607. if(min > count[i])
  3608. min = count[i]; // minimun count of collision
  3609. if(max < count[i])
  3610. max = count[i]; // maximun count of collision
  3611. if(count[i] == 0)
  3612. zero++;
  3613. ++count2[count[i]];
  3614. }
  3615. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3616. for( i=min; i <= max; ++i ){
  3617. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3618. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3619. }
  3620. for( i=min; i <= max; ++i ){
  3621. n += count2[i];
  3622. if( n*2 >= SCRIPT_HASH_SIZE )
  3623. {
  3624. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3625. median = (i+i+1)/2.0f;
  3626. else
  3627. median = i;
  3628. break;
  3629. }
  3630. }
  3631. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3632. fclose(fp);
  3633. }
  3634. }
  3635. #endif
  3636. mapreg_final();
  3637. db_destroy(scriptlabel_db);
  3638. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3639. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3640. if(sleep_db) {
  3641. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3642. while(n) {
  3643. struct script_state *st = (struct script_state *)n->data;
  3644. script_free_state(st);
  3645. n = n->next;
  3646. }
  3647. linkdb_final(&sleep_db);
  3648. }
  3649. if (str_data)
  3650. aFree(str_data);
  3651. if (str_buf)
  3652. aFree(str_buf);
  3653. return 0;
  3654. }
  3655. /*==========================================
  3656. * 初期化
  3657. *------------------------------------------*/
  3658. int do_init_script()
  3659. {
  3660. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3661. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3662. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3663. mapreg_init();
  3664. return 0;
  3665. }
  3666. int script_reload() {
  3667. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3668. db_clear(scriptlabel_db);
  3669. // @commands (script based)
  3670. // Clear bindings
  3671. memset(atcmd_binding,0,sizeof(atcmd_binding));
  3672. if(sleep_db) {
  3673. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3674. while(n) {
  3675. struct script_state *st = (struct script_state *)n->data;
  3676. script_free_state(st);
  3677. n = n->next;
  3678. }
  3679. linkdb_final(&sleep_db);
  3680. }
  3681. mapreg_reload();
  3682. return 0;
  3683. }
  3684. //-----------------------------------------------------------------------------
  3685. // buildin functions
  3686. //
  3687. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3688. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3689. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3690. /////////////////////////////////////////////////////////////////////
  3691. // NPC interaction
  3692. //
  3693. /// Appends a message to the npc dialog.
  3694. /// If a dialog doesn't exist yet, one is created.
  3695. ///
  3696. /// mes "<message>";
  3697. BUILDIN_FUNC(mes)
  3698. {
  3699. TBL_PC* sd = script_rid2sd(st);
  3700. if( sd == NULL )
  3701. return 0;
  3702. if( !script_hasdata(st, 3) )
  3703. {// only a single line detected in the script
  3704. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3705. }
  3706. else
  3707. {// parse multiple lines as they exist
  3708. int i;
  3709. for( i = 2; script_hasdata(st, i); i++ )
  3710. {
  3711. // send the message to the client
  3712. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3713. }
  3714. }
  3715. return 0;
  3716. }
  3717. /// Displays the button 'next' in the npc dialog.
  3718. /// The dialog text is cleared and the script continues when the button is pressed.
  3719. ///
  3720. /// next;
  3721. BUILDIN_FUNC(next)
  3722. {
  3723. TBL_PC* sd;
  3724. sd = script_rid2sd(st);
  3725. if( sd == NULL )
  3726. return 0;
  3727. st->state = STOP;
  3728. clif_scriptnext(sd, st->oid);
  3729. return 0;
  3730. }
  3731. /// Ends the script and displays the button 'close' on the npc dialog.
  3732. /// The dialog is closed when the button is pressed.
  3733. ///
  3734. /// close;
  3735. BUILDIN_FUNC(close)
  3736. {
  3737. TBL_PC* sd;
  3738. sd = script_rid2sd(st);
  3739. if( sd == NULL )
  3740. return 0;
  3741. st->state = END;
  3742. clif_scriptclose(sd, st->oid);
  3743. return 0;
  3744. }
  3745. /// Displays the button 'close' on the npc dialog.
  3746. /// The dialog is closed and the script continues when the button is pressed.
  3747. ///
  3748. /// close2;
  3749. BUILDIN_FUNC(close2)
  3750. {
  3751. TBL_PC* sd;
  3752. sd = script_rid2sd(st);
  3753. if( sd == NULL )
  3754. return 0;
  3755. st->state = STOP;
  3756. clif_scriptclose(sd, st->oid);
  3757. return 0;
  3758. }
  3759. /// Counts the number of valid and total number of options in 'str'
  3760. /// If max_count > 0 the counting stops when that valid option is reached
  3761. /// total is incremented for each option (NULL is supported)
  3762. static int menu_countoptions(const char* str, int max_count, int* total)
  3763. {
  3764. int count = 0;
  3765. int bogus_total;
  3766. if( total == NULL )
  3767. total = &bogus_total;
  3768. ++(*total);
  3769. // initial empty options
  3770. while( *str == ':' )
  3771. {
  3772. ++str;
  3773. ++(*total);
  3774. }
  3775. // count menu options
  3776. while( *str != '\0' )
  3777. {
  3778. ++count;
  3779. --max_count;
  3780. if( max_count == 0 )
  3781. break;
  3782. while( *str != ':' && *str != '\0' )
  3783. ++str;
  3784. while( *str == ':' )
  3785. {
  3786. ++str;
  3787. ++(*total);
  3788. }
  3789. }
  3790. return count;
  3791. }
  3792. /// Displays a menu with options and goes to the target label.
  3793. /// The script is stopped if cancel is pressed.
  3794. /// Options with no text are not displayed in the client.
  3795. ///
  3796. /// Options can be grouped together, separated by the character ':' in the text:
  3797. /// ex: menu "A:B:C",L_target;
  3798. /// All these options go to the specified target label.
  3799. ///
  3800. /// The index of the selected option is put in the variable @menu.
  3801. /// Indexes start with 1 and are consistent with grouped and empty options.
  3802. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3803. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3804. ///
  3805. /// NOTE: the client closes the npc dialog when cancel is pressed
  3806. ///
  3807. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3808. BUILDIN_FUNC(menu)
  3809. {
  3810. int i;
  3811. const char* text;
  3812. TBL_PC* sd;
  3813. sd = script_rid2sd(st);
  3814. if( sd == NULL )
  3815. return 0;
  3816. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3817. if( sd->state.menu_or_input == 0 )
  3818. {
  3819. struct StringBuf buf;
  3820. struct script_data* data;
  3821. if( script_lastdata(st) % 2 == 0 )
  3822. {// argument count is not even (1st argument is at index 2)
  3823. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3824. st->state = END;
  3825. return 1;
  3826. }
  3827. StringBuf_Init(&buf);
  3828. sd->npc_menu = 0;
  3829. for( i = 2; i < script_lastdata(st); i += 2 )
  3830. {
  3831. // menu options
  3832. text = script_getstr(st, i);
  3833. // target label
  3834. data = script_getdata(st, i+1);
  3835. if( !data_islabel(data) )
  3836. {// not a label
  3837. StringBuf_Destroy(&buf);
  3838. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3839. script_reportdata(data);
  3840. st->state = END;
  3841. return 1;
  3842. }
  3843. // append option(s)
  3844. if( text[0] == '\0' )
  3845. continue;// empty string, ignore
  3846. if( sd->npc_menu > 0 )
  3847. StringBuf_AppendStr(&buf, ":");
  3848. StringBuf_AppendStr(&buf, text);
  3849. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3850. }
  3851. st->state = RERUNLINE;
  3852. sd->state.menu_or_input = 1;
  3853. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3854. StringBuf_Destroy(&buf);
  3855. if( sd->npc_menu >= 0xff )
  3856. {// client supports only up to 254 entries; 0 is not used and 255 is reserved for cancel; excess entries are displayed but cause 'uint8' overflow
  3857. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3858. script_reportsrc(st);
  3859. }
  3860. }
  3861. else if( sd->npc_menu == 0xff )
  3862. {// Cancel was pressed
  3863. sd->state.menu_or_input = 0;
  3864. st->state = END;
  3865. }
  3866. else
  3867. {// goto target label
  3868. int menu = 0;
  3869. sd->state.menu_or_input = 0;
  3870. if( sd->npc_menu <= 0 )
  3871. {
  3872. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3873. st->state = END;
  3874. return 1;
  3875. }
  3876. // get target label
  3877. for( i = 2; i < script_lastdata(st); i += 2 )
  3878. {
  3879. text = script_getstr(st, i);
  3880. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3881. if( sd->npc_menu <= 0 )
  3882. break;// entry found
  3883. }
  3884. if( sd->npc_menu > 0 )
  3885. {// Invalid selection
  3886. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3887. st->state = END;
  3888. return 1;
  3889. }
  3890. if( !data_islabel(script_getdata(st, i + 1)) )
  3891. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3892. ShowError("script:menu: unexpected data in label argument\n");
  3893. script_reportdata(script_getdata(st, i + 1));
  3894. st->state = END;
  3895. return 1;
  3896. }
  3897. pc_setreg(sd, add_str("@menu"), menu);
  3898. st->pos = script_getnum(st, i + 1);
  3899. st->state = GOTO;
  3900. }
  3901. return 0;
  3902. }
  3903. /// Displays a menu with options and returns the selected option.
  3904. /// Behaves like 'menu' without the target labels.
  3905. ///
  3906. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3907. ///
  3908. /// @see menu
  3909. BUILDIN_FUNC(select)
  3910. {
  3911. int i;
  3912. const char* text;
  3913. TBL_PC* sd;
  3914. sd = script_rid2sd(st);
  3915. if( sd == NULL )
  3916. return 0;
  3917. if( sd->state.menu_or_input == 0 )
  3918. {
  3919. struct StringBuf buf;
  3920. StringBuf_Init(&buf);
  3921. sd->npc_menu = 0;
  3922. for( i = 2; i <= script_lastdata(st); ++i )
  3923. {
  3924. text = script_getstr(st, i);
  3925. if( sd->npc_menu > 0 )
  3926. StringBuf_AppendStr(&buf, ":");
  3927. StringBuf_AppendStr(&buf, text);
  3928. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3929. }
  3930. st->state = RERUNLINE;
  3931. sd->state.menu_or_input = 1;
  3932. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3933. StringBuf_Destroy(&buf);
  3934. if( sd->npc_menu >= 0xff )
  3935. {
  3936. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3937. script_reportsrc(st);
  3938. }
  3939. }
  3940. else if( sd->npc_menu == 0xff )
  3941. {// Cancel was pressed
  3942. sd->state.menu_or_input = 0;
  3943. st->state = END;
  3944. }
  3945. else
  3946. {// return selected option
  3947. int menu = 0;
  3948. sd->state.menu_or_input = 0;
  3949. for( i = 2; i <= script_lastdata(st); ++i )
  3950. {
  3951. text = script_getstr(st, i);
  3952. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3953. if( sd->npc_menu <= 0 )
  3954. break;// entry found
  3955. }
  3956. pc_setreg(sd, add_str("@menu"), menu);
  3957. script_pushint(st, menu);
  3958. st->state = RUN;
  3959. }
  3960. return 0;
  3961. }
  3962. /// Displays a menu with options and returns the selected option.
  3963. /// Behaves like 'menu' without the target labels, except when cancel is
  3964. /// pressed.
  3965. /// When cancel is pressed, the script continues and 255 is returned.
  3966. ///
  3967. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3968. ///
  3969. /// @see menu
  3970. BUILDIN_FUNC(prompt)
  3971. {
  3972. int i;
  3973. const char *text;
  3974. TBL_PC* sd;
  3975. sd = script_rid2sd(st);
  3976. if( sd == NULL )
  3977. return 0;
  3978. if( sd->state.menu_or_input == 0 )
  3979. {
  3980. struct StringBuf buf;
  3981. StringBuf_Init(&buf);
  3982. sd->npc_menu = 0;
  3983. for( i = 2; i <= script_lastdata(st); ++i )
  3984. {
  3985. text = script_getstr(st, i);
  3986. if( sd->npc_menu > 0 )
  3987. StringBuf_AppendStr(&buf, ":");
  3988. StringBuf_AppendStr(&buf, text);
  3989. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3990. }
  3991. st->state = RERUNLINE;
  3992. sd->state.menu_or_input = 1;
  3993. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3994. StringBuf_Destroy(&buf);
  3995. if( sd->npc_menu >= 0xff )
  3996. {
  3997. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3998. script_reportsrc(st);
  3999. }
  4000. }
  4001. else if( sd->npc_menu == 0xff )
  4002. {// Cancel was pressed
  4003. sd->state.menu_or_input = 0;
  4004. pc_setreg(sd, add_str("@menu"), 0xff);
  4005. script_pushint(st, 0xff);
  4006. st->state = RUN;
  4007. }
  4008. else
  4009. {// return selected option
  4010. int menu = 0;
  4011. sd->state.menu_or_input = 0;
  4012. for( i = 2; i <= script_lastdata(st); ++i )
  4013. {
  4014. text = script_getstr(st, i);
  4015. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4016. if( sd->npc_menu <= 0 )
  4017. break;// entry found
  4018. }
  4019. pc_setreg(sd, add_str("@menu"), menu);
  4020. script_pushint(st, menu);
  4021. st->state = RUN;
  4022. }
  4023. return 0;
  4024. }
  4025. /////////////////////////////////////////////////////////////////////
  4026. // ...
  4027. //
  4028. /// Jumps to the target script label.
  4029. ///
  4030. /// goto <label>;
  4031. BUILDIN_FUNC(goto)
  4032. {
  4033. if( !data_islabel(script_getdata(st,2)) )
  4034. {
  4035. ShowError("script:goto: not a label\n");
  4036. script_reportdata(script_getdata(st,2));
  4037. st->state = END;
  4038. return 1;
  4039. }
  4040. st->pos = script_getnum(st,2);
  4041. st->state = GOTO;
  4042. return 0;
  4043. }
  4044. /*==========================================
  4045. * user-defined function call
  4046. *------------------------------------------*/
  4047. BUILDIN_FUNC(callfunc)
  4048. {
  4049. int i, j;
  4050. struct script_retinfo* ri;
  4051. struct script_code* scr;
  4052. const char* str = script_getstr(st,2);
  4053. DBMap **ref = NULL;
  4054. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4055. if( !scr )
  4056. {
  4057. ShowError("script:callfunc: function not found! [%s]\n", str);
  4058. st->state = END;
  4059. return 1;
  4060. }
  4061. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4062. {
  4063. struct script_data* data = push_copy(st->stack,i);
  4064. if( data_isreference(data) && !data->ref )
  4065. {
  4066. const char* name = reference_getname(data);
  4067. if( name[0] == '.' ) {
  4068. if ( !ref ) {
  4069. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4070. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4071. }
  4072. data->ref = ref;
  4073. }
  4074. }
  4075. }
  4076. CREATE(ri, struct script_retinfo, 1);
  4077. ri->script = st->script;// script code
  4078. ri->var_function = st->stack->var_function;// scope variables
  4079. ri->pos = st->pos;// script location
  4080. ri->nargs = j;// argument count
  4081. ri->defsp = st->stack->defsp;// default stack pointer
  4082. push_retinfo(st->stack, ri, ref);
  4083. st->pos = 0;
  4084. st->script = scr;
  4085. st->stack->defsp = st->stack->sp;
  4086. st->state = GOTO;
  4087. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4088. return 0;
  4089. }
  4090. /*==========================================
  4091. * subroutine call
  4092. *------------------------------------------*/
  4093. BUILDIN_FUNC(callsub)
  4094. {
  4095. int i,j;
  4096. struct script_retinfo* ri;
  4097. int pos = script_getnum(st,2);
  4098. DBMap **ref = NULL;
  4099. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4100. {
  4101. ShowError("script:callsub: argument is not a label\n");
  4102. script_reportdata(script_getdata(st,2));
  4103. st->state = END;
  4104. return 1;
  4105. }
  4106. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4107. {
  4108. struct script_data* data = push_copy(st->stack,i);
  4109. if( data_isreference(data) && !data->ref )
  4110. {
  4111. const char* name = reference_getname(data);
  4112. if( name[0] == '.' && name[1] == '@' ) {
  4113. if ( !ref ) {
  4114. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4115. ref[0] = st->stack->var_function;
  4116. }
  4117. data->ref = ref;
  4118. }
  4119. }
  4120. }
  4121. CREATE(ri, struct script_retinfo, 1);
  4122. ri->script = st->script;// script code
  4123. ri->var_function = st->stack->var_function;// scope variables
  4124. ri->pos = st->pos;// script location
  4125. ri->nargs = j;// argument count
  4126. ri->defsp = st->stack->defsp;// default stack pointer
  4127. push_retinfo(st->stack, ri, ref);
  4128. st->pos = pos;
  4129. st->stack->defsp = st->stack->sp;
  4130. st->state = GOTO;
  4131. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4132. return 0;
  4133. }
  4134. /// Retrieves an argument provided to callfunc/callsub.
  4135. /// If the argument doesn't exist
  4136. ///
  4137. /// getarg(<index>{,<default_value>}) -> <value>
  4138. BUILDIN_FUNC(getarg)
  4139. {
  4140. struct script_retinfo* ri;
  4141. int idx;
  4142. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4143. {
  4144. ShowError("script:getarg: no callfunc or callsub!\n");
  4145. st->state = END;
  4146. return 1;
  4147. }
  4148. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4149. idx = script_getnum(st,2);
  4150. if( idx >= 0 && idx < ri->nargs )
  4151. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4152. else if( script_hasdata(st,3) )
  4153. script_pushcopy(st, 3);
  4154. else
  4155. {
  4156. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4157. st->state = END;
  4158. return 1;
  4159. }
  4160. return 0;
  4161. }
  4162. /// Returns from the current function, optionaly returning a value from the functions.
  4163. /// Don't use outside script functions.
  4164. ///
  4165. /// return;
  4166. /// return <value>;
  4167. BUILDIN_FUNC(return)
  4168. {
  4169. if( script_hasdata(st,2) )
  4170. {// return value
  4171. struct script_data* data;
  4172. script_pushcopy(st, 2);
  4173. data = script_getdatatop(st, -1);
  4174. if( data_isreference(data) )
  4175. {
  4176. const char* name = reference_getname(data);
  4177. if( name[0] == '.' && name[1] == '@' )
  4178. {// scope variable
  4179. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4180. get_val(st, data);// current scope, convert to value
  4181. }
  4182. else if( name[0] == '.' && !data->ref )
  4183. {// script variable, link to current script
  4184. data->ref = &st->script->script_vars;
  4185. }
  4186. }
  4187. }
  4188. else
  4189. {// no return value
  4190. script_pushnil(st);
  4191. }
  4192. st->state = RETFUNC;
  4193. return 0;
  4194. }
  4195. /// Returns a random number from 0 to <range>-1.
  4196. /// Or returns a random number from <min> to <max>.
  4197. /// If <min> is greater than <max>, their numbers are switched.
  4198. /// rand(<range>) -> <int>
  4199. /// rand(<min>,<max>) -> <int>
  4200. BUILDIN_FUNC(rand)
  4201. {
  4202. int range;
  4203. int min;
  4204. int max;
  4205. if( script_hasdata(st,3) )
  4206. {// min,max
  4207. min = script_getnum(st,2);
  4208. max = script_getnum(st,3);
  4209. if( max < min )
  4210. swap(min, max);
  4211. range = max - min + 1;
  4212. }
  4213. else
  4214. {// range
  4215. min = 0;
  4216. range = script_getnum(st,2);
  4217. }
  4218. if( range <= 1 )
  4219. script_pushint(st, min);
  4220. else
  4221. script_pushint(st, rnd()%range + min);
  4222. return 0;
  4223. }
  4224. /*==========================================
  4225. *
  4226. *------------------------------------------*/
  4227. BUILDIN_FUNC(warp)
  4228. {
  4229. int ret;
  4230. int x,y;
  4231. const char* str;
  4232. TBL_PC* sd;
  4233. sd = script_rid2sd(st);
  4234. if( sd == NULL )
  4235. return 0;
  4236. str = script_getstr(st,2);
  4237. x = script_getnum(st,3);
  4238. y = script_getnum(st,4);
  4239. if(strcmp(str,"Random")==0)
  4240. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4241. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4242. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4243. else
  4244. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4245. if( ret ) {
  4246. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4247. script_reportsrc(st);
  4248. }
  4249. return 0;
  4250. }
  4251. /*==========================================
  4252. * エリア指定ワープ
  4253. *------------------------------------------*/
  4254. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4255. {
  4256. int x,y;
  4257. unsigned int map;
  4258. map=va_arg(ap, unsigned int);
  4259. x=va_arg(ap,int);
  4260. y=va_arg(ap,int);
  4261. if(map == 0)
  4262. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4263. else
  4264. pc_setpos((TBL_PC *)bl,map,x,y,CLR_OUTSIGHT);
  4265. return 0;
  4266. }
  4267. BUILDIN_FUNC(areawarp)
  4268. {
  4269. int x,y,m;
  4270. unsigned int index;
  4271. const char *str;
  4272. const char *mapname;
  4273. int x0,y0,x1,y1;
  4274. mapname=script_getstr(st,2);
  4275. x0=script_getnum(st,3);
  4276. y0=script_getnum(st,4);
  4277. x1=script_getnum(st,5);
  4278. y1=script_getnum(st,6);
  4279. str=script_getstr(st,7);
  4280. x=script_getnum(st,8);
  4281. y=script_getnum(st,9);
  4282. if( (m=map_mapname2mapid(mapname))< 0)
  4283. return 0;
  4284. if(strcmp(str,"Random")==0)
  4285. index = 0;
  4286. else if(!(index=mapindex_name2id(str)))
  4287. return 0;
  4288. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  4289. return 0;
  4290. }
  4291. /*==========================================
  4292. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4293. *------------------------------------------*/
  4294. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4295. {
  4296. int hp, sp;
  4297. hp = va_arg(ap, int);
  4298. sp = va_arg(ap, int);
  4299. pc_percentheal((TBL_PC *)bl,hp,sp);
  4300. return 0;
  4301. }
  4302. BUILDIN_FUNC(areapercentheal)
  4303. {
  4304. int hp,sp,m;
  4305. const char *mapname;
  4306. int x0,y0,x1,y1;
  4307. mapname=script_getstr(st,2);
  4308. x0=script_getnum(st,3);
  4309. y0=script_getnum(st,4);
  4310. x1=script_getnum(st,5);
  4311. y1=script_getnum(st,6);
  4312. hp=script_getnum(st,7);
  4313. sp=script_getnum(st,8);
  4314. if( (m=map_mapname2mapid(mapname))< 0)
  4315. return 0;
  4316. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4317. return 0;
  4318. }
  4319. /*==========================================
  4320. * warpchar [LuzZza]
  4321. * Useful for warp one player from
  4322. * another player npc-session.
  4323. * Using: warpchar "mapname",x,y,Char_ID;
  4324. *------------------------------------------*/
  4325. BUILDIN_FUNC(warpchar)
  4326. {
  4327. int x,y,a;
  4328. const char *str;
  4329. TBL_PC *sd;
  4330. str=script_getstr(st,2);
  4331. x=script_getnum(st,3);
  4332. y=script_getnum(st,4);
  4333. a=script_getnum(st,5);
  4334. sd = map_charid2sd(a);
  4335. if( sd == NULL )
  4336. return 0;
  4337. if(strcmp(str, "Random") == 0)
  4338. pc_randomwarp(sd, CLR_TELEPORT);
  4339. else
  4340. if(strcmp(str, "SavePoint") == 0)
  4341. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4342. else
  4343. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4344. return 0;
  4345. }
  4346. /*==========================================
  4347. * Warpparty - [Fredzilla] [Paradox924X]
  4348. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4349. * If 'from_mapname' is specified, only the party members on that map will be warped
  4350. *------------------------------------------*/
  4351. BUILDIN_FUNC(warpparty)
  4352. {
  4353. TBL_PC *sd = NULL;
  4354. TBL_PC *pl_sd;
  4355. struct party_data* p;
  4356. int type;
  4357. int mapindex;
  4358. int i;
  4359. const char* str = script_getstr(st,2);
  4360. int x = script_getnum(st,3);
  4361. int y = script_getnum(st,4);
  4362. int p_id = script_getnum(st,5);
  4363. const char* str2 = NULL;
  4364. if ( script_hasdata(st,6) )
  4365. str2 = script_getstr(st,6);
  4366. p = party_search(p_id);
  4367. if(!p)
  4368. return 0;
  4369. type = ( strcmp(str,"Random")==0 ) ? 0
  4370. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4371. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4372. : ( strcmp(str,"Leader")==0 ) ? 3
  4373. : 4;
  4374. switch (type)
  4375. {
  4376. case 3:
  4377. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4378. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4379. return 0;
  4380. pl_sd = p->data[i].sd;
  4381. mapindex = pl_sd->mapindex;
  4382. x = pl_sd->bl.x;
  4383. y = pl_sd->bl.y;
  4384. break;
  4385. case 4:
  4386. mapindex = mapindex_name2id(str);
  4387. break;
  4388. case 2:
  4389. //"SavePoint" uses save point of the currently attached player
  4390. if (( sd = script_rid2sd(st) ) == NULL )
  4391. return 0;
  4392. default:
  4393. mapindex = 0;
  4394. break;
  4395. }
  4396. for (i = 0; i < MAX_PARTY; i++)
  4397. {
  4398. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4399. continue;
  4400. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4401. continue;
  4402. if( pc_isdead(pl_sd) )
  4403. continue;
  4404. switch( type )
  4405. {
  4406. case 0: // Random
  4407. if(!map[pl_sd->bl.m].flag.nowarp)
  4408. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4409. break;
  4410. case 1: // SavePointAll
  4411. if(!map[pl_sd->bl.m].flag.noreturn)
  4412. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4413. break;
  4414. case 2: // SavePoint
  4415. if(!map[pl_sd->bl.m].flag.noreturn)
  4416. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4417. break;
  4418. case 3: // Leader
  4419. case 4: // m,x,y
  4420. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4421. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4422. break;
  4423. }
  4424. }
  4425. return 0;
  4426. }
  4427. /*==========================================
  4428. * Warpguild - [Fredzilla]
  4429. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4430. *------------------------------------------*/
  4431. BUILDIN_FUNC(warpguild)
  4432. {
  4433. TBL_PC *sd = NULL;
  4434. TBL_PC *pl_sd;
  4435. struct guild* g;
  4436. struct s_mapiterator* iter;
  4437. int type;
  4438. const char* str = script_getstr(st,2);
  4439. int x = script_getnum(st,3);
  4440. int y = script_getnum(st,4);
  4441. int gid = script_getnum(st,5);
  4442. g = guild_search(gid);
  4443. if( g == NULL )
  4444. return 0;
  4445. type = ( strcmp(str,"Random")==0 ) ? 0
  4446. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4447. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4448. : 3;
  4449. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4450. {// "SavePoint" uses save point of the currently attached player
  4451. return 0;
  4452. }
  4453. iter = mapit_getallusers();
  4454. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4455. {
  4456. if( pl_sd->status.guild_id != gid )
  4457. continue;
  4458. switch( type )
  4459. {
  4460. case 0: // Random
  4461. if(!map[pl_sd->bl.m].flag.nowarp)
  4462. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4463. break;
  4464. case 1: // SavePointAll
  4465. if(!map[pl_sd->bl.m].flag.noreturn)
  4466. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4467. break;
  4468. case 2: // SavePoint
  4469. if(!map[pl_sd->bl.m].flag.noreturn)
  4470. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4471. break;
  4472. case 3: // m,x,y
  4473. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4474. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4475. break;
  4476. }
  4477. }
  4478. mapit_free(iter);
  4479. return 0;
  4480. }
  4481. /*==========================================
  4482. *
  4483. *------------------------------------------*/
  4484. BUILDIN_FUNC(heal)
  4485. {
  4486. TBL_PC *sd;
  4487. int hp,sp;
  4488. sd = script_rid2sd(st);
  4489. if (!sd) return 0;
  4490. hp=script_getnum(st,2);
  4491. sp=script_getnum(st,3);
  4492. status_heal(&sd->bl, hp, sp, 1);
  4493. return 0;
  4494. }
  4495. /*==========================================
  4496. *
  4497. *------------------------------------------*/
  4498. BUILDIN_FUNC(itemheal)
  4499. {
  4500. TBL_PC *sd;
  4501. int hp,sp;
  4502. hp=script_getnum(st,2);
  4503. sp=script_getnum(st,3);
  4504. if(potion_flag==1) {
  4505. potion_hp = hp;
  4506. potion_sp = sp;
  4507. return 0;
  4508. }
  4509. sd = script_rid2sd(st);
  4510. if (!sd) return 0;
  4511. pc_itemheal(sd,sd->itemid,hp,sp);
  4512. return 0;
  4513. }
  4514. /*==========================================
  4515. *
  4516. *------------------------------------------*/
  4517. BUILDIN_FUNC(percentheal)
  4518. {
  4519. int hp,sp;
  4520. TBL_PC* sd;
  4521. hp=script_getnum(st,2);
  4522. sp=script_getnum(st,3);
  4523. if(potion_flag==1) {
  4524. potion_per_hp = hp;
  4525. potion_per_sp = sp;
  4526. return 0;
  4527. }
  4528. sd = script_rid2sd(st);
  4529. if( sd == NULL )
  4530. return 0;
  4531. pc_percentheal(sd,hp,sp);
  4532. return 0;
  4533. }
  4534. /*==========================================
  4535. *
  4536. *------------------------------------------*/
  4537. BUILDIN_FUNC(jobchange)
  4538. {
  4539. int job, upper=-1;
  4540. job=script_getnum(st,2);
  4541. if( script_hasdata(st,3) )
  4542. upper=script_getnum(st,3);
  4543. if (pcdb_checkid(job))
  4544. {
  4545. TBL_PC* sd;
  4546. sd = script_rid2sd(st);
  4547. if( sd == NULL )
  4548. return 0;
  4549. pc_jobchange(sd, job, upper);
  4550. }
  4551. return 0;
  4552. }
  4553. /*==========================================
  4554. *
  4555. *------------------------------------------*/
  4556. BUILDIN_FUNC(jobname)
  4557. {
  4558. int class_=script_getnum(st,2);
  4559. script_pushconststr(st, (char*)job_name(class_));
  4560. return 0;
  4561. }
  4562. /// Get input from the player.
  4563. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4564. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4565. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4566. /// shorter than 'min' and 0 otherwise.
  4567. ///
  4568. /// input(<var>{,<min>{,<max>}}) -> <int>
  4569. BUILDIN_FUNC(input)
  4570. {
  4571. TBL_PC* sd;
  4572. struct script_data* data;
  4573. int uid;
  4574. const char* name;
  4575. int min;
  4576. int max;
  4577. sd = script_rid2sd(st);
  4578. if( sd == NULL )
  4579. return 0;
  4580. data = script_getdata(st,2);
  4581. if( !data_isreference(data) ){
  4582. ShowError("script:input: not a variable\n");
  4583. script_reportdata(data);
  4584. st->state = END;
  4585. return 1;
  4586. }
  4587. uid = reference_getuid(data);
  4588. name = reference_getname(data);
  4589. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4590. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4591. if( !sd->state.menu_or_input )
  4592. { // first invocation, display npc input box
  4593. sd->state.menu_or_input = 1;
  4594. st->state = RERUNLINE;
  4595. if( is_string_variable(name) )
  4596. clif_scriptinputstr(sd,st->oid);
  4597. else
  4598. clif_scriptinput(sd,st->oid);
  4599. }
  4600. else
  4601. { // take received text/value and store it in the designated variable
  4602. sd->state.menu_or_input = 0;
  4603. if( is_string_variable(name) )
  4604. {
  4605. int len = (int)strlen(sd->npc_str);
  4606. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4607. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4608. }
  4609. else
  4610. {
  4611. int amount = sd->npc_amount;
  4612. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4613. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4614. }
  4615. st->state = RUN;
  4616. }
  4617. return 0;
  4618. }
  4619. // declare the copyarray method here for future reference
  4620. BUILDIN_FUNC(copyarray);
  4621. /// Sets the value of a variable.
  4622. /// The value is converted to the type of the variable.
  4623. ///
  4624. /// set(<variable>,<value>) -> <variable>
  4625. BUILDIN_FUNC(set)
  4626. {
  4627. TBL_PC* sd = NULL;
  4628. struct script_data* data;
  4629. //struct script_data* datavalue;
  4630. int num;
  4631. const char* name;
  4632. char prefix;
  4633. data = script_getdata(st,2);
  4634. //datavalue = script_getdata(st,3);
  4635. if( !data_isreference(data) )
  4636. {
  4637. ShowError("script:set: not a variable\n");
  4638. script_reportdata(script_getdata(st,2));
  4639. st->state = END;
  4640. return 1;
  4641. }
  4642. num = reference_getuid(data);
  4643. name = reference_getname(data);
  4644. prefix = *name;
  4645. if( not_server_variable(prefix) )
  4646. {
  4647. sd = script_rid2sd(st);
  4648. if( sd == NULL )
  4649. {
  4650. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4651. return 0;
  4652. }
  4653. }
  4654. #if 0
  4655. if( data_isreference(datavalue) )
  4656. {// the value being referenced is a variable
  4657. const char* namevalue = reference_getname(datavalue);
  4658. if( !not_array_variable(*namevalue) )
  4659. {// array variable being copied into another array variable
  4660. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  4661. {// player must be attached in order to copy a player variable
  4662. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  4663. return 0;
  4664. }
  4665. if( is_string_variable(namevalue) != is_string_variable(name) )
  4666. {// non-matching array value types
  4667. ShowWarning("script:set: two array variables do not match in type.\n");
  4668. return 0;
  4669. }
  4670. // push the maximum number of array values to the stack
  4671. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  4672. // call the copy array method directly
  4673. return buildin_copyarray(st);
  4674. }
  4675. }
  4676. #endif
  4677. if( is_string_variable(name) )
  4678. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4679. else
  4680. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4681. // return a copy of the variable reference
  4682. script_pushcopy(st,2);
  4683. return 0;
  4684. }
  4685. /////////////////////////////////////////////////////////////////////
  4686. /// Array variables
  4687. ///
  4688. /// Returns the size of the specified array
  4689. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  4690. {
  4691. int32 ret = idx;
  4692. if( isstring )
  4693. {
  4694. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4695. {
  4696. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4697. if( str && *str )
  4698. ret = idx + 1;
  4699. script_removetop(st, -1, 0);
  4700. }
  4701. }
  4702. else
  4703. {
  4704. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4705. {
  4706. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  4707. if( num )
  4708. ret = idx + 1;
  4709. script_removetop(st, -1, 0);
  4710. }
  4711. }
  4712. return ret;
  4713. }
  4714. /// Sets values of an array, from the starting index.
  4715. /// ex: setarray arr[1],1,2,3;
  4716. ///
  4717. /// setarray <array variable>,<value1>{,<value2>...};
  4718. BUILDIN_FUNC(setarray)
  4719. {
  4720. struct script_data* data;
  4721. const char* name;
  4722. int32 start;
  4723. int32 end;
  4724. int32 id;
  4725. int32 i;
  4726. TBL_PC* sd = NULL;
  4727. data = script_getdata(st, 2);
  4728. if( !data_isreference(data) )
  4729. {
  4730. ShowError("script:setarray: not a variable\n");
  4731. script_reportdata(data);
  4732. st->state = END;
  4733. return 1;// not a variable
  4734. }
  4735. id = reference_getid(data);
  4736. start = reference_getindex(data);
  4737. name = reference_getname(data);
  4738. if( not_array_variable(*name) )
  4739. {
  4740. ShowError("script:setarray: illegal scope\n");
  4741. script_reportdata(data);
  4742. st->state = END;
  4743. return 1;// not supported
  4744. }
  4745. if( not_server_variable(*name) )
  4746. {
  4747. sd = script_rid2sd(st);
  4748. if( sd == NULL )
  4749. return 0;// no player attached
  4750. }
  4751. end = start + script_lastdata(st) - 2;
  4752. if( end > SCRIPT_MAX_ARRAYSIZE )
  4753. end = SCRIPT_MAX_ARRAYSIZE;
  4754. if( is_string_variable(name) )
  4755. {// string array
  4756. for( i = 3; start < end; ++start, ++i )
  4757. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4758. }
  4759. else
  4760. {// int array
  4761. for( i = 3; start < end; ++start, ++i )
  4762. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  4763. }
  4764. return 0;
  4765. }
  4766. /// Sets count values of an array, from the starting index.
  4767. /// ex: cleararray arr[0],0,1;
  4768. ///
  4769. /// cleararray <array variable>,<value>,<count>;
  4770. BUILDIN_FUNC(cleararray)
  4771. {
  4772. struct script_data* data;
  4773. const char* name;
  4774. int32 start;
  4775. int32 end;
  4776. int32 id;
  4777. void* v;
  4778. TBL_PC* sd = NULL;
  4779. data = script_getdata(st, 2);
  4780. if( !data_isreference(data) )
  4781. {
  4782. ShowError("script:cleararray: not a variable\n");
  4783. script_reportdata(data);
  4784. st->state = END;
  4785. return 1;// not a variable
  4786. }
  4787. id = reference_getid(data);
  4788. start = reference_getindex(data);
  4789. name = reference_getname(data);
  4790. if( not_array_variable(*name) )
  4791. {
  4792. ShowError("script:cleararray: illegal scope\n");
  4793. script_reportdata(data);
  4794. st->state = END;
  4795. return 1;// not supported
  4796. }
  4797. if( not_server_variable(*name) )
  4798. {
  4799. sd = script_rid2sd(st);
  4800. if( sd == NULL )
  4801. return 0;// no player attached
  4802. }
  4803. if( is_string_variable(name) )
  4804. v = (void*)script_getstr(st, 3);
  4805. else
  4806. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  4807. end = start + script_getnum(st, 4);
  4808. if( end > SCRIPT_MAX_ARRAYSIZE )
  4809. end = SCRIPT_MAX_ARRAYSIZE;
  4810. for( ; start < end; ++start )
  4811. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4812. return 0;
  4813. }
  4814. /// Copies data from one array to another.
  4815. /// ex: copyarray arr[0],arr[2],2;
  4816. ///
  4817. /// copyarray <destination array variable>,<source array variable>,<count>;
  4818. BUILDIN_FUNC(copyarray)
  4819. {
  4820. struct script_data* data1;
  4821. struct script_data* data2;
  4822. const char* name1;
  4823. const char* name2;
  4824. int32 idx1;
  4825. int32 idx2;
  4826. int32 id1;
  4827. int32 id2;
  4828. void* v;
  4829. int32 i;
  4830. int32 count;
  4831. TBL_PC* sd = NULL;
  4832. data1 = script_getdata(st, 2);
  4833. data2 = script_getdata(st, 3);
  4834. if( !data_isreference(data1) || !data_isreference(data2) )
  4835. {
  4836. ShowError("script:copyarray: not a variable\n");
  4837. script_reportdata(data1);
  4838. script_reportdata(data2);
  4839. st->state = END;
  4840. return 1;// not a variable
  4841. }
  4842. id1 = reference_getid(data1);
  4843. id2 = reference_getid(data2);
  4844. idx1 = reference_getindex(data1);
  4845. idx2 = reference_getindex(data2);
  4846. name1 = reference_getname(data1);
  4847. name2 = reference_getname(data2);
  4848. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4849. {
  4850. ShowError("script:copyarray: illegal scope\n");
  4851. script_reportdata(data1);
  4852. script_reportdata(data2);
  4853. st->state = END;
  4854. return 1;// not supported
  4855. }
  4856. if( is_string_variable(name1) != is_string_variable(name2) )
  4857. {
  4858. ShowError("script:copyarray: type mismatch\n");
  4859. script_reportdata(data1);
  4860. script_reportdata(data2);
  4861. st->state = END;
  4862. return 1;// data type mismatch
  4863. }
  4864. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4865. {
  4866. sd = script_rid2sd(st);
  4867. if( sd == NULL )
  4868. return 0;// no player attached
  4869. }
  4870. count = script_getnum(st, 4);
  4871. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  4872. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  4873. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4874. return 0;// nothing to copy
  4875. if( id1 == id2 && idx1 > idx2 )
  4876. {// destination might be overlapping the source - copy in reverse order
  4877. for( i = count - 1; i >= 0; --i )
  4878. {
  4879. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4880. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4881. script_removetop(st, -1, 0);
  4882. }
  4883. }
  4884. else
  4885. {// normal copy
  4886. for( i = 0; i < count; ++i )
  4887. {
  4888. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  4889. {
  4890. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4891. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4892. script_removetop(st, -1, 0);
  4893. }
  4894. else// out of range - assume ""/0
  4895. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4896. }
  4897. }
  4898. return 0;
  4899. }
  4900. /// Returns the size of the array.
  4901. /// Assumes that everything before the starting index exists.
  4902. /// ex: getarraysize(arr[3])
  4903. ///
  4904. /// getarraysize(<array variable>) -> <int>
  4905. BUILDIN_FUNC(getarraysize)
  4906. {
  4907. struct script_data* data;
  4908. const char* name;
  4909. data = script_getdata(st, 2);
  4910. if( !data_isreference(data) )
  4911. {
  4912. ShowError("script:getarraysize: not a variable\n");
  4913. script_reportdata(data);
  4914. script_pushnil(st);
  4915. st->state = END;
  4916. return 1;// not a variable
  4917. }
  4918. name = reference_getname(data);
  4919. if( not_array_variable(*name) )
  4920. {
  4921. ShowError("script:getarraysize: illegal scope\n");
  4922. script_reportdata(data);
  4923. script_pushnil(st);
  4924. st->state = END;
  4925. return 1;// not supported
  4926. }
  4927. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4928. return 0;
  4929. }
  4930. /// Deletes count or all the elements in an array, from the starting index.
  4931. /// ex: deletearray arr[4],2;
  4932. ///
  4933. /// deletearray <array variable>;
  4934. /// deletearray <array variable>,<count>;
  4935. BUILDIN_FUNC(deletearray)
  4936. {
  4937. struct script_data* data;
  4938. const char* name;
  4939. int start;
  4940. int end;
  4941. int id;
  4942. TBL_PC *sd = NULL;
  4943. data = script_getdata(st, 2);
  4944. if( !data_isreference(data) )
  4945. {
  4946. ShowError("script:deletearray: not a variable\n");
  4947. script_reportdata(data);
  4948. st->state = END;
  4949. return 1;// not a variable
  4950. }
  4951. id = reference_getid(data);
  4952. start = reference_getindex(data);
  4953. name = reference_getname(data);
  4954. if( not_array_variable(*name) )
  4955. {
  4956. ShowError("script:deletearray: illegal scope\n");
  4957. script_reportdata(data);
  4958. st->state = END;
  4959. return 1;// not supported
  4960. }
  4961. if( not_server_variable(*name) )
  4962. {
  4963. sd = script_rid2sd(st);
  4964. if( sd == NULL )
  4965. return 0;// no player attached
  4966. }
  4967. end = SCRIPT_MAX_ARRAYSIZE;
  4968. if( start >= end )
  4969. return 0;// nothing to free
  4970. if( script_hasdata(st,3) )
  4971. {
  4972. int count = script_getnum(st, 3);
  4973. if( count > end - start )
  4974. count = end - start;
  4975. if( count <= 0 )
  4976. return 0;// nothing to free
  4977. // move rest of the elements backward
  4978. for( ; start + count < end; ++start )
  4979. {
  4980. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4981. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4982. script_removetop(st, -1, 0);
  4983. }
  4984. }
  4985. // clear the rest of the array
  4986. if( is_string_variable(name) )
  4987. {
  4988. for( ; start < end; ++start )
  4989. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4990. }
  4991. else
  4992. {
  4993. for( ; start < end; ++start )
  4994. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4995. }
  4996. return 0;
  4997. }
  4998. /// Returns a reference to the target index of the array variable.
  4999. /// Equivalent to var[index].
  5000. ///
  5001. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5002. BUILDIN_FUNC(getelementofarray)
  5003. {
  5004. struct script_data* data;
  5005. const char* name;
  5006. int32 id;
  5007. int i;
  5008. data = script_getdata(st, 2);
  5009. if( !data_isreference(data) )
  5010. {
  5011. ShowError("script:getelementofarray: not a variable\n");
  5012. script_reportdata(data);
  5013. script_pushnil(st);
  5014. st->state = END;
  5015. return 1;// not a variable
  5016. }
  5017. id = reference_getid(data);
  5018. name = reference_getname(data);
  5019. if( not_array_variable(*name) )
  5020. {
  5021. ShowError("script:getelementofarray: illegal scope\n");
  5022. script_reportdata(data);
  5023. script_pushnil(st);
  5024. st->state = END;
  5025. return 1;// not supported
  5026. }
  5027. i = script_getnum(st, 3);
  5028. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5029. {
  5030. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5031. script_reportdata(data);
  5032. script_pushnil(st);
  5033. st->state = END;
  5034. return 1;// out of range
  5035. }
  5036. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5037. return 0;
  5038. }
  5039. /////////////////////////////////////////////////////////////////////
  5040. /// ...
  5041. ///
  5042. /*==========================================
  5043. *
  5044. *------------------------------------------*/
  5045. BUILDIN_FUNC(setlook)
  5046. {
  5047. int type,val;
  5048. TBL_PC* sd;
  5049. type=script_getnum(st,2);
  5050. val=script_getnum(st,3);
  5051. sd = script_rid2sd(st);
  5052. if( sd == NULL )
  5053. return 0;
  5054. pc_changelook(sd,type,val);
  5055. return 0;
  5056. }
  5057. BUILDIN_FUNC(changelook)
  5058. { // As setlook but only client side
  5059. int type,val;
  5060. TBL_PC* sd;
  5061. type=script_getnum(st,2);
  5062. val=script_getnum(st,3);
  5063. sd = script_rid2sd(st);
  5064. if( sd == NULL )
  5065. return 0;
  5066. clif_changelook(&sd->bl,type,val);
  5067. return 0;
  5068. }
  5069. /*==========================================
  5070. *
  5071. *------------------------------------------*/
  5072. BUILDIN_FUNC(cutin)
  5073. {
  5074. TBL_PC* sd;
  5075. sd = script_rid2sd(st);
  5076. if( sd == NULL )
  5077. return 0;
  5078. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5079. return 0;
  5080. }
  5081. /*==========================================
  5082. *
  5083. *------------------------------------------*/
  5084. BUILDIN_FUNC(viewpoint)
  5085. {
  5086. int type,x,y,id,color;
  5087. TBL_PC* sd;
  5088. type=script_getnum(st,2);
  5089. x=script_getnum(st,3);
  5090. y=script_getnum(st,4);
  5091. id=script_getnum(st,5);
  5092. color=script_getnum(st,6);
  5093. sd = script_rid2sd(st);
  5094. if( sd == NULL )
  5095. return 0;
  5096. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5097. return 0;
  5098. }
  5099. /*==========================================
  5100. *
  5101. *------------------------------------------*/
  5102. BUILDIN_FUNC(countitem)
  5103. {
  5104. int nameid, i;
  5105. int count = 0;
  5106. struct item_data* id = NULL;
  5107. struct script_data* data;
  5108. TBL_PC* sd = script_rid2sd(st);
  5109. if (!sd) {
  5110. script_pushint(st,0);
  5111. return 0;
  5112. }
  5113. data = script_getdata(st,2);
  5114. get_val(st, data); // convert into value in case of a variable
  5115. if( data_isstring(data) )
  5116. {// item name
  5117. id = itemdb_searchname(conv_str(st, data));
  5118. }
  5119. else
  5120. {// item id
  5121. id = itemdb_exists(conv_num(st, data));
  5122. }
  5123. if( id == NULL )
  5124. {
  5125. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5126. script_pushint(st,0);
  5127. return 1;
  5128. }
  5129. nameid = id->nameid;
  5130. for(i = 0; i < MAX_INVENTORY; i++)
  5131. if(sd->status.inventory[i].nameid == nameid)
  5132. count += sd->status.inventory[i].amount;
  5133. script_pushint(st,count);
  5134. return 0;
  5135. }
  5136. /*==========================================
  5137. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5138. * returns number of items that meet the conditions
  5139. *------------------------------------------*/
  5140. BUILDIN_FUNC(countitem2)
  5141. {
  5142. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5143. int count = 0;
  5144. int i;
  5145. struct item_data* id = NULL;
  5146. struct script_data* data;
  5147. TBL_PC* sd = script_rid2sd(st);
  5148. if (!sd) {
  5149. script_pushint(st,0);
  5150. return 0;
  5151. }
  5152. data = script_getdata(st,2);
  5153. get_val(st, data); // convert into value in case of a variable
  5154. if( data_isstring(data) )
  5155. {// item name
  5156. id = itemdb_searchname(conv_str(st, data));
  5157. }
  5158. else
  5159. {// item id
  5160. id = itemdb_exists(conv_num(st, data));
  5161. }
  5162. if( id == NULL )
  5163. {
  5164. ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5165. script_pushint(st,0);
  5166. return 1;
  5167. }
  5168. nameid = id->nameid;
  5169. iden = script_getnum(st,3);
  5170. ref = script_getnum(st,4);
  5171. attr = script_getnum(st,5);
  5172. c1 = (short)script_getnum(st,6);
  5173. c2 = (short)script_getnum(st,7);
  5174. c3 = (short)script_getnum(st,8);
  5175. c4 = (short)script_getnum(st,9);
  5176. for(i = 0; i < MAX_INVENTORY; i++)
  5177. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5178. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5179. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5180. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5181. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5182. sd->status.inventory[i].card[3] == c4
  5183. )
  5184. count += sd->status.inventory[i].amount;
  5185. script_pushint(st,count);
  5186. return 0;
  5187. }
  5188. /*==========================================
  5189. * 重量チェック
  5190. *------------------------------------------*/
  5191. BUILDIN_FUNC(checkweight)
  5192. {
  5193. int nameid, amount, slots;
  5194. unsigned int weight;
  5195. struct item_data* id = NULL;
  5196. struct map_session_data* sd;
  5197. struct script_data* data;
  5198. if( ( sd = script_rid2sd(st) ) == NULL )
  5199. {
  5200. return 0;
  5201. }
  5202. data = script_getdata(st,2);
  5203. get_val(st, data); // convert into value in case of a variable
  5204. if( data_isstring(data) )
  5205. {// item name
  5206. id = itemdb_searchname(conv_str(st, data));
  5207. }
  5208. else
  5209. {// item id
  5210. id = itemdb_exists(conv_num(st, data));
  5211. }
  5212. if( id == NULL )
  5213. {
  5214. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5215. script_pushint(st,0);
  5216. return 1;
  5217. }
  5218. nameid = id->nameid;
  5219. amount = script_getnum(st,3);
  5220. if( amount < 1 )
  5221. {
  5222. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5223. script_pushint(st,0);
  5224. return 1;
  5225. }
  5226. weight = itemdb_weight(nameid)*amount;
  5227. if( weight + sd->weight > sd->max_weight )
  5228. {// too heavy
  5229. script_pushint(st,0);
  5230. return 0;
  5231. }
  5232. switch( pc_checkadditem(sd, nameid, amount) )
  5233. {
  5234. case ADDITEM_EXIST:
  5235. // item is already in inventory, but there is still space for the requested amount
  5236. break;
  5237. case ADDITEM_NEW:
  5238. slots = pc_inventoryblank(sd);
  5239. if( itemdb_isstackable(nameid) )
  5240. {// stackable
  5241. if( slots < 1 )
  5242. {
  5243. script_pushint(st,0);
  5244. return 0;
  5245. }
  5246. }
  5247. else
  5248. {// non-stackable
  5249. if( slots < amount )
  5250. {
  5251. script_pushint(st,0);
  5252. return 0;
  5253. }
  5254. }
  5255. break;
  5256. case ADDITEM_OVERAMOUNT:
  5257. script_pushint(st,0);
  5258. return 0;
  5259. }
  5260. script_pushint(st,1);
  5261. return 0;
  5262. }
  5263. /*==========================================
  5264. * getitem <item id>,<amount>{,<account ID>};
  5265. * getitem "<item name>",<amount>{,<account ID>};
  5266. *------------------------------------------*/
  5267. BUILDIN_FUNC(getitem)
  5268. {
  5269. int nameid,amount,get_count,i,flag = 0;
  5270. struct item it;
  5271. TBL_PC *sd;
  5272. struct script_data *data;
  5273. data=script_getdata(st,2);
  5274. get_val(st,data);
  5275. if( data_isstring(data) )
  5276. {// "<item name>"
  5277. const char *name=conv_str(st,data);
  5278. struct item_data *item_data = itemdb_searchname(name);
  5279. if( item_data == NULL ){
  5280. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5281. return 1; //No item created.
  5282. }
  5283. nameid=item_data->nameid;
  5284. } else if( data_isint(data) )
  5285. {// <item id>
  5286. nameid=conv_num(st,data);
  5287. //Violet Box, Blue Box, etc - random item pick
  5288. if( nameid < 0 ) {
  5289. nameid = -nameid;
  5290. flag = 1;
  5291. }
  5292. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5293. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5294. return 1; //No item created.
  5295. }
  5296. } else {
  5297. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5298. return 1;
  5299. }
  5300. // <amount>
  5301. if( (amount=script_getnum(st,3)) <= 0)
  5302. return 0; //return if amount <=0, skip the useles iteration
  5303. memset(&it,0,sizeof(it));
  5304. it.nameid=nameid;
  5305. if(!flag)
  5306. it.identify=1;
  5307. else
  5308. it.identify=itemdb_isidentified(nameid);
  5309. if( script_hasdata(st,4) )
  5310. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5311. else
  5312. sd=script_rid2sd(st); // Attached player
  5313. if( sd == NULL ) // no target
  5314. return 0;
  5315. //Check if it's stackable.
  5316. if (!itemdb_isstackable(nameid))
  5317. get_count = 1;
  5318. else
  5319. get_count = amount;
  5320. for (i = 0; i < amount; i += get_count)
  5321. {
  5322. // if not pet egg
  5323. if (!pet_create_egg(sd, nameid))
  5324. {
  5325. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5326. {
  5327. clif_additem(sd, 0, 0, flag);
  5328. if( pc_candrop(sd,&it) )
  5329. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5330. }
  5331. }
  5332. }
  5333. return 0;
  5334. }
  5335. /*==========================================
  5336. *
  5337. *------------------------------------------*/
  5338. BUILDIN_FUNC(getitem2)
  5339. {
  5340. int nameid,amount,get_count,i,flag = 0;
  5341. int iden,ref,attr,c1,c2,c3,c4;
  5342. struct item_data *item_data;
  5343. struct item item_tmp;
  5344. TBL_PC *sd;
  5345. struct script_data *data;
  5346. if( script_hasdata(st,11) )
  5347. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5348. else
  5349. sd=script_rid2sd(st); // Attached player
  5350. if( sd == NULL ) // no target
  5351. return 0;
  5352. data=script_getdata(st,2);
  5353. get_val(st,data);
  5354. if( data_isstring(data) ){
  5355. const char *name=conv_str(st,data);
  5356. struct item_data *item_data = itemdb_searchname(name);
  5357. if( item_data )
  5358. nameid=item_data->nameid;
  5359. else
  5360. nameid=UNKNOWN_ITEM_ID;
  5361. }else
  5362. nameid=conv_num(st,data);
  5363. amount=script_getnum(st,3);
  5364. iden=script_getnum(st,4);
  5365. ref=script_getnum(st,5);
  5366. attr=script_getnum(st,6);
  5367. c1=(short)script_getnum(st,7);
  5368. c2=(short)script_getnum(st,8);
  5369. c3=(short)script_getnum(st,9);
  5370. c4=(short)script_getnum(st,10);
  5371. if(nameid<0) { // ランダム
  5372. nameid = -nameid;
  5373. flag = 1;
  5374. }
  5375. if(nameid > 0) {
  5376. memset(&item_tmp,0,sizeof(item_tmp));
  5377. item_data=itemdb_exists(nameid);
  5378. if (item_data == NULL)
  5379. return -1;
  5380. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5381. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5382. }
  5383. else if(item_data->type==IT_PETEGG) {
  5384. iden = 1;
  5385. ref = 0;
  5386. }
  5387. else {
  5388. iden = 1;
  5389. ref = attr = 0;
  5390. }
  5391. item_tmp.nameid=nameid;
  5392. if(!flag)
  5393. item_tmp.identify=iden;
  5394. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5395. item_tmp.identify=0;
  5396. item_tmp.refine=ref;
  5397. item_tmp.attribute=attr;
  5398. item_tmp.card[0]=(short)c1;
  5399. item_tmp.card[1]=(short)c2;
  5400. item_tmp.card[2]=(short)c3;
  5401. item_tmp.card[3]=(short)c4;
  5402. //Check if it's stackable.
  5403. if (!itemdb_isstackable(nameid))
  5404. get_count = 1;
  5405. else
  5406. get_count = amount;
  5407. for (i = 0; i < amount; i += get_count)
  5408. {
  5409. // if not pet egg
  5410. if (!pet_create_egg(sd, nameid))
  5411. {
  5412. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5413. {
  5414. clif_additem(sd, 0, 0, flag);
  5415. if( pc_candrop(sd,&item_tmp) )
  5416. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5417. }
  5418. }
  5419. }
  5420. }
  5421. return 0;
  5422. }
  5423. /*==========================================
  5424. * rentitem <item id>,<seconds>
  5425. * rentitem "<item name>",<seconds>
  5426. *------------------------------------------*/
  5427. BUILDIN_FUNC(rentitem)
  5428. {
  5429. struct map_session_data *sd;
  5430. struct script_data *data;
  5431. struct item it;
  5432. int seconds;
  5433. int nameid = 0, flag;
  5434. data = script_getdata(st,2);
  5435. get_val(st,data);
  5436. if( (sd = script_rid2sd(st)) == NULL )
  5437. return 0;
  5438. if( data_isstring(data) )
  5439. {
  5440. const char *name = conv_str(st,data);
  5441. struct item_data *itd = itemdb_searchname(name);
  5442. if( itd == NULL )
  5443. {
  5444. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5445. return 1;
  5446. }
  5447. nameid = itd->nameid;
  5448. }
  5449. else if( data_isint(data) )
  5450. {
  5451. nameid = conv_num(st,data);
  5452. if( nameid <= 0 || !itemdb_exists(nameid) )
  5453. {
  5454. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5455. return 1;
  5456. }
  5457. }
  5458. else
  5459. {
  5460. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5461. return 1;
  5462. }
  5463. seconds = script_getnum(st,3);
  5464. memset(&it, 0, sizeof(it));
  5465. it.nameid = nameid;
  5466. it.identify = 1;
  5467. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5468. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5469. {
  5470. clif_additem(sd, 0, 0, flag);
  5471. return 1;
  5472. }
  5473. return 0;
  5474. }
  5475. /*==========================================
  5476. * gets an item with someone's name inscribed [Skotlex]
  5477. * getinscribeditem item_num, character_name
  5478. * Returned Qty is always 1, only works on equip-able
  5479. * equipment
  5480. *------------------------------------------*/
  5481. BUILDIN_FUNC(getnameditem)
  5482. {
  5483. int nameid;
  5484. struct item item_tmp;
  5485. TBL_PC *sd, *tsd;
  5486. struct script_data *data;
  5487. sd = script_rid2sd(st);
  5488. if (sd == NULL)
  5489. { //Player not attached!
  5490. script_pushint(st,0);
  5491. return 0;
  5492. }
  5493. data=script_getdata(st,2);
  5494. get_val(st,data);
  5495. if( data_isstring(data) ){
  5496. const char *name=conv_str(st,data);
  5497. struct item_data *item_data = itemdb_searchname(name);
  5498. if( item_data == NULL)
  5499. { //Failed
  5500. script_pushint(st,0);
  5501. return 0;
  5502. }
  5503. nameid = item_data->nameid;
  5504. }else
  5505. nameid = conv_num(st,data);
  5506. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5507. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5508. script_pushint(st,0);
  5509. return 0;
  5510. }
  5511. data=script_getdata(st,3);
  5512. get_val(st,data);
  5513. if( data_isstring(data) ) //Char Name
  5514. tsd=map_nick2sd(conv_str(st,data));
  5515. else //Char Id was given
  5516. tsd=map_charid2sd(conv_num(st,data));
  5517. if( tsd == NULL )
  5518. { //Failed
  5519. script_pushint(st,0);
  5520. return 0;
  5521. }
  5522. memset(&item_tmp,0,sizeof(item_tmp));
  5523. item_tmp.nameid=nameid;
  5524. item_tmp.amount=1;
  5525. item_tmp.identify=1;
  5526. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5527. item_tmp.card[2]=tsd->status.char_id;
  5528. item_tmp.card[3]=tsd->status.char_id >> 16;
  5529. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5530. script_pushint(st,0);
  5531. return 0; //Failed to add item, we will not drop if they don't fit
  5532. }
  5533. script_pushint(st,1);
  5534. return 0;
  5535. }
  5536. /*==========================================
  5537. * gets a random item ID from an item group [Skotlex]
  5538. * groupranditem group_num
  5539. *------------------------------------------*/
  5540. BUILDIN_FUNC(grouprandomitem)
  5541. {
  5542. int group;
  5543. group = script_getnum(st,2);
  5544. script_pushint(st,itemdb_searchrandomid(group));
  5545. return 0;
  5546. }
  5547. /*==========================================
  5548. *
  5549. *------------------------------------------*/
  5550. BUILDIN_FUNC(makeitem)
  5551. {
  5552. int nameid,amount,flag = 0;
  5553. int x,y,m;
  5554. const char *mapname;
  5555. struct item item_tmp;
  5556. struct script_data *data;
  5557. data=script_getdata(st,2);
  5558. get_val(st,data);
  5559. if( data_isstring(data) ){
  5560. const char *name=conv_str(st,data);
  5561. struct item_data *item_data = itemdb_searchname(name);
  5562. if( item_data )
  5563. nameid=item_data->nameid;
  5564. else
  5565. nameid=UNKNOWN_ITEM_ID;
  5566. }else
  5567. nameid=conv_num(st,data);
  5568. amount=script_getnum(st,3);
  5569. mapname =script_getstr(st,4);
  5570. x =script_getnum(st,5);
  5571. y =script_getnum(st,6);
  5572. if(strcmp(mapname,"this")==0)
  5573. {
  5574. TBL_PC *sd;
  5575. sd = script_rid2sd(st);
  5576. if (!sd) return 0; //Failed...
  5577. m=sd->bl.m;
  5578. } else
  5579. m=map_mapname2mapid(mapname);
  5580. if(nameid<0) { // ランダム
  5581. nameid = -nameid;
  5582. flag = 1;
  5583. }
  5584. if(nameid > 0) {
  5585. memset(&item_tmp,0,sizeof(item_tmp));
  5586. item_tmp.nameid=nameid;
  5587. if(!flag)
  5588. item_tmp.identify=1;
  5589. else
  5590. item_tmp.identify=itemdb_isidentified(nameid);
  5591. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5592. }
  5593. return 0;
  5594. }
  5595. /// Counts / deletes the current item given by idx.
  5596. /// Used by buildin_delitem_search
  5597. /// Relies on all input data being already fully valid.
  5598. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5599. {
  5600. int delamount;
  5601. struct item* inv = &sd->status.inventory[idx];
  5602. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5603. if( delete_items )
  5604. {
  5605. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5606. {// delete associated pet
  5607. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5608. }
  5609. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5610. }
  5611. amount[0]-= delamount;
  5612. }
  5613. /// Searches for item(s) and checks, if there is enough of them.
  5614. /// Used by delitem and delitem2
  5615. /// Relies on all input data being already fully valid.
  5616. /// @param exact_match will also match item attributes and cards, not just name id
  5617. /// @return true when all items could be deleted, false when there were not enough items to delete
  5618. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5619. {
  5620. bool delete_items = false;
  5621. int i, amount, important;
  5622. struct item* inv;
  5623. // prefer always non-equipped items
  5624. it->equip = 0;
  5625. // when searching for nameid only, prefer additionally
  5626. if( !exact_match )
  5627. {
  5628. // non-refined items
  5629. it->refine = 0;
  5630. // card-less items
  5631. memset(it->card, 0, sizeof(it->card));
  5632. }
  5633. for(;;)
  5634. {
  5635. amount = it->amount;
  5636. important = 0;
  5637. // 1st pass -- less important items / exact match
  5638. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5639. {
  5640. inv = &sd->status.inventory[i];
  5641. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5642. {// wrong/invalid item
  5643. continue;
  5644. }
  5645. if( inv->equip != it->equip || inv->refine != it->refine )
  5646. {// not matching attributes
  5647. important++;
  5648. continue;
  5649. }
  5650. if( exact_match )
  5651. {
  5652. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5653. {// not matching exact attributes
  5654. continue;
  5655. }
  5656. }
  5657. else
  5658. {
  5659. if( sd->inventory_data[i]->type == IT_PETEGG )
  5660. {
  5661. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5662. {// pet which cannot be deleted
  5663. continue;
  5664. }
  5665. }
  5666. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  5667. {// named/carded item
  5668. important++;
  5669. continue;
  5670. }
  5671. }
  5672. // count / delete item
  5673. buildin_delitem_delete(sd, i, &amount, delete_items);
  5674. }
  5675. // 2nd pass -- any matching item
  5676. if( amount == 0 || important == 0 )
  5677. {// either everything was already consumed or no items were skipped
  5678. ;
  5679. }
  5680. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5681. {
  5682. inv = &sd->status.inventory[i];
  5683. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5684. {// wrong/invalid item
  5685. continue;
  5686. }
  5687. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  5688. {// pet which cannot be deleted
  5689. continue;
  5690. }
  5691. if( exact_match )
  5692. {
  5693. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5694. {// not matching attributes
  5695. continue;
  5696. }
  5697. }
  5698. // count / delete item
  5699. buildin_delitem_delete(sd, i, &amount, delete_items);
  5700. }
  5701. if( amount )
  5702. {// not enough items
  5703. return false;
  5704. }
  5705. else if( delete_items )
  5706. {// we are done with the work
  5707. return true;
  5708. }
  5709. else
  5710. {// get rid of the items now
  5711. delete_items = true;
  5712. }
  5713. }
  5714. }
  5715. /// Deletes items from the target/attached player.
  5716. /// Prioritizes ordinary items.
  5717. ///
  5718. /// delitem <item id>,<amount>{,<account id>}
  5719. /// delitem "<item name>",<amount>{,<account id>}
  5720. BUILDIN_FUNC(delitem)
  5721. {
  5722. TBL_PC *sd;
  5723. struct item it;
  5724. struct script_data *data;
  5725. if( script_hasdata(st,4) )
  5726. {
  5727. int account_id = script_getnum(st,4);
  5728. sd = map_id2sd(account_id); // <account id>
  5729. if( sd == NULL )
  5730. {
  5731. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5732. st->state = END;
  5733. return 1;
  5734. }
  5735. }
  5736. else
  5737. {
  5738. sd = script_rid2sd(st);// attached player
  5739. if( sd == NULL )
  5740. return 0;
  5741. }
  5742. data = script_getdata(st,2);
  5743. get_val(st,data);
  5744. if( data_isstring(data) )
  5745. {
  5746. const char* item_name = conv_str(st,data);
  5747. struct item_data* id = itemdb_searchname(item_name);
  5748. if( id == NULL )
  5749. {
  5750. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5751. st->state = END;
  5752. return 1;
  5753. }
  5754. it.nameid = id->nameid;// "<item name>"
  5755. }
  5756. else
  5757. {
  5758. it.nameid = conv_num(st,data);// <item id>
  5759. if( !itemdb_exists( it.nameid ) )
  5760. {
  5761. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5762. st->state = END;
  5763. return 1;
  5764. }
  5765. }
  5766. it.amount=script_getnum(st,3);
  5767. if( it.amount <= 0 )
  5768. return 0;// nothing to do
  5769. if( buildin_delitem_search(sd, &it, false) )
  5770. {// success
  5771. return 0;
  5772. }
  5773. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5774. st->state = END;
  5775. clif_scriptclose(sd, st->oid);
  5776. return 1;
  5777. }
  5778. /// Deletes items from the target/attached player.
  5779. ///
  5780. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5781. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5782. BUILDIN_FUNC(delitem2)
  5783. {
  5784. TBL_PC *sd;
  5785. struct item it;
  5786. struct script_data *data;
  5787. if( script_hasdata(st,11) )
  5788. {
  5789. int account_id = script_getnum(st,11);
  5790. sd = map_id2sd(account_id); // <account id>
  5791. if( sd == NULL )
  5792. {
  5793. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5794. st->state = END;
  5795. return 1;
  5796. }
  5797. }
  5798. else
  5799. {
  5800. sd = script_rid2sd(st);// attached player
  5801. if( sd == NULL )
  5802. return 0;
  5803. }
  5804. data = script_getdata(st,2);
  5805. get_val(st,data);
  5806. if( data_isstring(data) )
  5807. {
  5808. const char* item_name = conv_str(st,data);
  5809. struct item_data* id = itemdb_searchname(item_name);
  5810. if( id == NULL )
  5811. {
  5812. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5813. st->state = END;
  5814. return 1;
  5815. }
  5816. it.nameid = id->nameid;// "<item name>"
  5817. }
  5818. else
  5819. {
  5820. it.nameid = conv_num(st,data);// <item id>
  5821. if( !itemdb_exists( it.nameid ) )
  5822. {
  5823. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5824. st->state = END;
  5825. return 1;
  5826. }
  5827. }
  5828. it.amount=script_getnum(st,3);
  5829. it.identify=script_getnum(st,4);
  5830. it.refine=script_getnum(st,5);
  5831. it.attribute=script_getnum(st,6);
  5832. it.card[0]=(short)script_getnum(st,7);
  5833. it.card[1]=(short)script_getnum(st,8);
  5834. it.card[2]=(short)script_getnum(st,9);
  5835. it.card[3]=(short)script_getnum(st,10);
  5836. if( it.amount <= 0 )
  5837. return 0;// nothing to do
  5838. if( buildin_delitem_search(sd, &it, true) )
  5839. {// success
  5840. return 0;
  5841. }
  5842. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5843. st->state = END;
  5844. clif_scriptclose(sd, st->oid);
  5845. return 1;
  5846. }
  5847. /*==========================================
  5848. * Enables/Disables use of items while in an NPC [Skotlex]
  5849. *------------------------------------------*/
  5850. BUILDIN_FUNC(enableitemuse)
  5851. {
  5852. TBL_PC *sd;
  5853. sd=script_rid2sd(st);
  5854. if (sd)
  5855. sd->npc_item_flag = st->oid;
  5856. return 0;
  5857. }
  5858. BUILDIN_FUNC(disableitemuse)
  5859. {
  5860. TBL_PC *sd;
  5861. sd=script_rid2sd(st);
  5862. if (sd)
  5863. sd->npc_item_flag = 0;
  5864. return 0;
  5865. }
  5866. /*==========================================
  5867. *キャラ関係のパラメータ取得
  5868. *------------------------------------------*/
  5869. BUILDIN_FUNC(readparam)
  5870. {
  5871. int type;
  5872. TBL_PC *sd;
  5873. type=script_getnum(st,2);
  5874. if( script_hasdata(st,3) )
  5875. sd=map_nick2sd(script_getstr(st,3));
  5876. else
  5877. sd=script_rid2sd(st);
  5878. if(sd==NULL){
  5879. script_pushint(st,-1);
  5880. return 0;
  5881. }
  5882. script_pushint(st,pc_readparam(sd,type));
  5883. return 0;
  5884. }
  5885. /*==========================================
  5886. *キャラ関係のID取得
  5887. *------------------------------------------*/
  5888. BUILDIN_FUNC(getcharid)
  5889. {
  5890. int num;
  5891. TBL_PC *sd;
  5892. num = script_getnum(st,2);
  5893. if( script_hasdata(st,3) )
  5894. sd=map_nick2sd(script_getstr(st,3));
  5895. else
  5896. sd=script_rid2sd(st);
  5897. if(sd==NULL){
  5898. script_pushint(st,0); //return 0, according docs
  5899. return 0;
  5900. }
  5901. switch( num ) {
  5902. case 0: script_pushint(st,sd->status.char_id); break;
  5903. case 1: script_pushint(st,sd->status.party_id); break;
  5904. case 2: script_pushint(st,sd->status.guild_id); break;
  5905. case 3: script_pushint(st,sd->status.account_id); break;
  5906. case 4: script_pushint(st,sd->bg_id); break;
  5907. default:
  5908. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5909. script_pushint(st,0);
  5910. break;
  5911. }
  5912. return 0;
  5913. }
  5914. /*==========================================
  5915. * returns the GID of an NPC
  5916. *------------------------------------------*/
  5917. BUILDIN_FUNC(getnpcid)
  5918. {
  5919. int num = script_getnum(st,2);
  5920. struct npc_data* nd = NULL;
  5921. if( script_hasdata(st,3) )
  5922. {// unique npc name
  5923. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  5924. {
  5925. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  5926. script_pushint(st,0);
  5927. return 1;
  5928. }
  5929. }
  5930. switch (num) {
  5931. case 0:
  5932. script_pushint(st,nd ? nd->bl.id : st->oid);
  5933. break;
  5934. default:
  5935. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  5936. script_pushint(st,0);
  5937. return 1;
  5938. }
  5939. return 0;
  5940. }
  5941. /*==========================================
  5942. *指定IDのPT名取得
  5943. *------------------------------------------*/
  5944. BUILDIN_FUNC(getpartyname)
  5945. {
  5946. int party_id;
  5947. struct party_data* p;
  5948. party_id = script_getnum(st,2);
  5949. if( ( p = party_search(party_id) ) != NULL )
  5950. {
  5951. script_pushstrcopy(st,p->party.name);
  5952. }
  5953. else
  5954. {
  5955. script_pushconststr(st,"null");
  5956. }
  5957. return 0;
  5958. }
  5959. /*==========================================
  5960. *指定IDのPT人数とメンバーID取得
  5961. *------------------------------------------*/
  5962. BUILDIN_FUNC(getpartymember)
  5963. {
  5964. struct party_data *p;
  5965. int i,j=0,type=0;
  5966. p=party_search(script_getnum(st,2));
  5967. if( script_hasdata(st,3) )
  5968. type=script_getnum(st,3);
  5969. if(p!=NULL){
  5970. for(i=0;i<MAX_PARTY;i++){
  5971. if(p->party.member[i].account_id){
  5972. switch (type) {
  5973. case 2:
  5974. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  5975. break;
  5976. case 1:
  5977. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  5978. break;
  5979. default:
  5980. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  5981. }
  5982. j++;
  5983. }
  5984. }
  5985. }
  5986. mapreg_setreg(add_str("$@partymembercount"),j);
  5987. return 0;
  5988. }
  5989. /*==========================================
  5990. * Retrieves party leader. if flag is specified,
  5991. * return some of the leader data. Otherwise, return name.
  5992. *------------------------------------------*/
  5993. BUILDIN_FUNC(getpartyleader)
  5994. {
  5995. int party_id, type = 0, i=0;
  5996. struct party_data *p;
  5997. party_id=script_getnum(st,2);
  5998. if( script_hasdata(st,3) )
  5999. type=script_getnum(st,3);
  6000. p=party_search(party_id);
  6001. if (p) //Search leader
  6002. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6003. if (!p || i == MAX_PARTY) { //leader not found
  6004. if (type)
  6005. script_pushint(st,-1);
  6006. else
  6007. script_pushconststr(st,"null");
  6008. return 0;
  6009. }
  6010. switch (type) {
  6011. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6012. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6013. case 3: script_pushint(st,p->party.member[i].class_); break;
  6014. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6015. case 5: script_pushint(st,p->party.member[i].lv); break;
  6016. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6017. }
  6018. return 0;
  6019. }
  6020. /*==========================================
  6021. *指定IDのギルド名取得
  6022. *------------------------------------------*/
  6023. BUILDIN_FUNC(getguildname)
  6024. {
  6025. int guild_id;
  6026. struct guild* g;
  6027. guild_id = script_getnum(st,2);
  6028. if( ( g = guild_search(guild_id) ) != NULL )
  6029. {
  6030. script_pushstrcopy(st,g->name);
  6031. }
  6032. else
  6033. {
  6034. script_pushconststr(st,"null");
  6035. }
  6036. return 0;
  6037. }
  6038. /*==========================================
  6039. *指定IDのGuildMaster名取得
  6040. *------------------------------------------*/
  6041. BUILDIN_FUNC(getguildmaster)
  6042. {
  6043. int guild_id;
  6044. struct guild* g;
  6045. guild_id = script_getnum(st,2);
  6046. if( ( g = guild_search(guild_id) ) != NULL )
  6047. {
  6048. script_pushstrcopy(st,g->member[0].name);
  6049. }
  6050. else
  6051. {
  6052. script_pushconststr(st,"null");
  6053. }
  6054. return 0;
  6055. }
  6056. BUILDIN_FUNC(getguildmasterid)
  6057. {
  6058. int guild_id;
  6059. struct guild* g;
  6060. guild_id = script_getnum(st,2);
  6061. if( ( g = guild_search(guild_id) ) != NULL )
  6062. {
  6063. script_pushint(st,g->member[0].char_id);
  6064. }
  6065. else
  6066. {
  6067. script_pushint(st,0);
  6068. }
  6069. return 0;
  6070. }
  6071. /*==========================================
  6072. * キャラクタの名前
  6073. *------------------------------------------*/
  6074. BUILDIN_FUNC(strcharinfo)
  6075. {
  6076. TBL_PC *sd;
  6077. int num;
  6078. struct guild* g;
  6079. struct party_data* p;
  6080. sd=script_rid2sd(st);
  6081. if (!sd) { //Avoid crashing....
  6082. script_pushconststr(st,"");
  6083. return 0;
  6084. }
  6085. num=script_getnum(st,2);
  6086. switch(num){
  6087. case 0:
  6088. script_pushstrcopy(st,sd->status.name);
  6089. break;
  6090. case 1:
  6091. if( ( p = party_search(sd->status.party_id) ) != NULL )
  6092. {
  6093. script_pushstrcopy(st,p->party.name);
  6094. }
  6095. else
  6096. {
  6097. script_pushconststr(st,"");
  6098. }
  6099. break;
  6100. case 2:
  6101. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  6102. {
  6103. script_pushstrcopy(st,g->name);
  6104. }
  6105. else
  6106. {
  6107. script_pushconststr(st,"");
  6108. }
  6109. break;
  6110. case 3:
  6111. script_pushconststr(st,map[sd->bl.m].name);
  6112. break;
  6113. default:
  6114. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6115. script_pushconststr(st,"");
  6116. break;
  6117. }
  6118. return 0;
  6119. }
  6120. /*==========================================
  6121. * 呼び出し元のNPC情報を取得する
  6122. *------------------------------------------*/
  6123. BUILDIN_FUNC(strnpcinfo)
  6124. {
  6125. TBL_NPC* nd;
  6126. int num;
  6127. char *buf,*name=NULL;
  6128. nd = map_id2nd(st->oid);
  6129. if (!nd) {
  6130. script_pushconststr(st, "");
  6131. return 0;
  6132. }
  6133. num = script_getnum(st,2);
  6134. switch(num){
  6135. case 0: // display name
  6136. name = aStrdup(nd->name);
  6137. break;
  6138. case 1: // visible part of display name
  6139. if((buf = strchr(nd->name,'#')) != NULL)
  6140. {
  6141. name = aStrdup(nd->name);
  6142. name[buf - nd->name] = 0;
  6143. } else // Return the name, there is no '#' present
  6144. name = aStrdup(nd->name);
  6145. break;
  6146. case 2: // # fragment
  6147. if((buf = strchr(nd->name,'#')) != NULL)
  6148. name = aStrdup(buf+1);
  6149. break;
  6150. case 3: // unique name
  6151. name = aStrdup(nd->exname);
  6152. break;
  6153. case 4: // map name
  6154. name = aStrdup(map[nd->bl.m].name);
  6155. break;
  6156. }
  6157. if(name)
  6158. script_pushstr(st, name);
  6159. else
  6160. script_pushconststr(st, "");
  6161. return 0;
  6162. }
  6163. // aegis->athena slot position conversion table
  6164. static unsigned int equip[] = {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};
  6165. /*==========================================
  6166. * GetEquipID(Pos); Pos: 1-10
  6167. *------------------------------------------*/
  6168. BUILDIN_FUNC(getequipid)
  6169. {
  6170. int i, num;
  6171. TBL_PC* sd;
  6172. struct item_data* item;
  6173. sd = script_rid2sd(st);
  6174. if( sd == NULL )
  6175. return 0;
  6176. num = script_getnum(st,2) - 1;
  6177. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6178. {
  6179. script_pushint(st,-1);
  6180. return 0;
  6181. }
  6182. // get inventory position of item
  6183. i = pc_checkequip(sd,equip[num]);
  6184. if( i < 0 )
  6185. {
  6186. script_pushint(st,-1);
  6187. return 0;
  6188. }
  6189. item = sd->inventory_data[i];
  6190. if( item != 0 )
  6191. script_pushint(st,item->nameid);
  6192. else
  6193. script_pushint(st,0);
  6194. return 0;
  6195. }
  6196. /*==========================================
  6197. * 装備名文字列(精錬メニュー用)
  6198. *------------------------------------------*/
  6199. BUILDIN_FUNC(getequipname)
  6200. {
  6201. int i, num;
  6202. TBL_PC* sd;
  6203. struct item_data* item;
  6204. sd = script_rid2sd(st);
  6205. if( sd == NULL )
  6206. return 0;
  6207. num = script_getnum(st,2) - 1;
  6208. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6209. {
  6210. script_pushconststr(st,"");
  6211. return 0;
  6212. }
  6213. // get inventory position of item
  6214. i = pc_checkequip(sd,equip[num]);
  6215. if( i < 0 )
  6216. {
  6217. script_pushint(st,-1);
  6218. return 0;
  6219. }
  6220. item = sd->inventory_data[i];
  6221. if( item != 0 )
  6222. script_pushstrcopy(st,item->jname);
  6223. else
  6224. script_pushconststr(st,"");
  6225. return 0;
  6226. }
  6227. /*==========================================
  6228. * getbrokenid [Valaris]
  6229. *------------------------------------------*/
  6230. BUILDIN_FUNC(getbrokenid)
  6231. {
  6232. int i,num,id=0,brokencounter=0;
  6233. TBL_PC *sd;
  6234. sd = script_rid2sd(st);
  6235. if( sd == NULL )
  6236. return 0;
  6237. num=script_getnum(st,2);
  6238. for(i=0; i<MAX_INVENTORY; i++) {
  6239. if(sd->status.inventory[i].attribute){
  6240. brokencounter++;
  6241. if(num==brokencounter){
  6242. id=sd->status.inventory[i].nameid;
  6243. break;
  6244. }
  6245. }
  6246. }
  6247. script_pushint(st,id);
  6248. return 0;
  6249. }
  6250. /*==========================================
  6251. * repair [Valaris]
  6252. *------------------------------------------*/
  6253. BUILDIN_FUNC(repair)
  6254. {
  6255. int i,num;
  6256. int repaircounter=0;
  6257. TBL_PC *sd;
  6258. sd = script_rid2sd(st);
  6259. if( sd == NULL )
  6260. return 0;
  6261. num=script_getnum(st,2);
  6262. for(i=0; i<MAX_INVENTORY; i++) {
  6263. if(sd->status.inventory[i].attribute){
  6264. repaircounter++;
  6265. if(num==repaircounter){
  6266. sd->status.inventory[i].attribute=0;
  6267. clif_equiplist(sd);
  6268. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6269. clif_misceffect(&sd->bl, 3);
  6270. break;
  6271. }
  6272. }
  6273. }
  6274. return 0;
  6275. }
  6276. /*==========================================
  6277. * 装備チェック
  6278. *------------------------------------------*/
  6279. BUILDIN_FUNC(getequipisequiped)
  6280. {
  6281. int i = -1,num;
  6282. TBL_PC *sd;
  6283. num = script_getnum(st,2);
  6284. sd = script_rid2sd(st);
  6285. if( sd == NULL )
  6286. return 0;
  6287. if (num > 0 && num <= ARRAYLENGTH(equip))
  6288. i=pc_checkequip(sd,equip[num-1]);
  6289. if(i >= 0)
  6290. script_pushint(st,1);
  6291. else
  6292. script_pushint(st,0);
  6293. return 0;
  6294. }
  6295. /*==========================================
  6296. * 装備品精錬可能チェック
  6297. *------------------------------------------*/
  6298. BUILDIN_FUNC(getequipisenableref)
  6299. {
  6300. int i = -1,num;
  6301. TBL_PC *sd;
  6302. num = script_getnum(st,2);
  6303. sd = script_rid2sd(st);
  6304. if( sd == NULL )
  6305. return 0;
  6306. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6307. i = pc_checkequip(sd,equip[num-1]);
  6308. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6309. script_pushint(st,1);
  6310. else
  6311. script_pushint(st,0);
  6312. return 0;
  6313. }
  6314. /*==========================================
  6315. * 装備品鑑定チェック
  6316. *------------------------------------------*/
  6317. BUILDIN_FUNC(getequipisidentify)
  6318. {
  6319. int i = -1,num;
  6320. TBL_PC *sd;
  6321. num = script_getnum(st,2);
  6322. sd = script_rid2sd(st);
  6323. if( sd == NULL )
  6324. return 0;
  6325. if (num > 0 && num <= ARRAYLENGTH(equip))
  6326. i=pc_checkequip(sd,equip[num-1]);
  6327. if(i >= 0)
  6328. script_pushint(st,sd->status.inventory[i].identify);
  6329. else
  6330. script_pushint(st,0);
  6331. return 0;
  6332. }
  6333. /*==========================================
  6334. * 装備品精錬度
  6335. *------------------------------------------*/
  6336. BUILDIN_FUNC(getequiprefinerycnt)
  6337. {
  6338. int i = -1,num;
  6339. TBL_PC *sd;
  6340. num = script_getnum(st,2);
  6341. sd = script_rid2sd(st);
  6342. if( sd == NULL )
  6343. return 0;
  6344. if (num > 0 && num <= ARRAYLENGTH(equip))
  6345. i=pc_checkequip(sd,equip[num-1]);
  6346. if(i >= 0)
  6347. script_pushint(st,sd->status.inventory[i].refine);
  6348. else
  6349. script_pushint(st,0);
  6350. return 0;
  6351. }
  6352. /*==========================================
  6353. * 装備品武器LV
  6354. *------------------------------------------*/
  6355. BUILDIN_FUNC(getequipweaponlv)
  6356. {
  6357. int i = -1,num;
  6358. TBL_PC *sd;
  6359. num = script_getnum(st,2);
  6360. sd = script_rid2sd(st);
  6361. if( sd == NULL )
  6362. return 0;
  6363. if (num > 0 && num <= ARRAYLENGTH(equip))
  6364. i=pc_checkequip(sd,equip[num-1]);
  6365. if(i >= 0 && sd->inventory_data[i])
  6366. script_pushint(st,sd->inventory_data[i]->wlv);
  6367. else
  6368. script_pushint(st,0);
  6369. return 0;
  6370. }
  6371. /*==========================================
  6372. * 装備品精錬成功率
  6373. *------------------------------------------*/
  6374. BUILDIN_FUNC(getequippercentrefinery)
  6375. {
  6376. int i = -1,num;
  6377. TBL_PC *sd;
  6378. num = script_getnum(st,2);
  6379. sd = script_rid2sd(st);
  6380. if( sd == NULL )
  6381. return 0;
  6382. if (num > 0 && num <= ARRAYLENGTH(equip))
  6383. i=pc_checkequip(sd,equip[num-1]);
  6384. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6385. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6386. else
  6387. script_pushint(st,0);
  6388. return 0;
  6389. }
  6390. /*==========================================
  6391. * 精錬成功
  6392. *------------------------------------------*/
  6393. BUILDIN_FUNC(successrefitem)
  6394. {
  6395. int i=-1,num,ep;
  6396. TBL_PC *sd;
  6397. num = script_getnum(st,2);
  6398. sd = script_rid2sd(st);
  6399. if( sd == NULL )
  6400. return 0;
  6401. if (num > 0 && num <= ARRAYLENGTH(equip))
  6402. i=pc_checkequip(sd,equip[num-1]);
  6403. if(i >= 0) {
  6404. ep=sd->status.inventory[i].equip;
  6405. //Logs items, got from (N)PC scripts [Lupus]
  6406. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6407. sd->status.inventory[i].refine++;
  6408. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6409. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6410. clif_delitem(sd,i,1,3);
  6411. //Logs items, got from (N)PC scripts [Lupus]
  6412. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6413. clif_additem(sd,i,1,0);
  6414. pc_equipitem(sd,i,ep);
  6415. clif_misceffect(&sd->bl,3);
  6416. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6417. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6418. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6419. ){ // Fame point system [DracoRPG]
  6420. switch (sd->inventory_data[i]->wlv){
  6421. case 1:
  6422. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6423. break;
  6424. case 2:
  6425. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6426. break;
  6427. case 3:
  6428. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6429. break;
  6430. }
  6431. }
  6432. }
  6433. return 0;
  6434. }
  6435. /*==========================================
  6436. * 精錬失敗
  6437. *------------------------------------------*/
  6438. BUILDIN_FUNC(failedrefitem)
  6439. {
  6440. int i=-1,num;
  6441. TBL_PC *sd;
  6442. num = script_getnum(st,2);
  6443. sd = script_rid2sd(st);
  6444. if( sd == NULL )
  6445. return 0;
  6446. if (num > 0 && num <= ARRAYLENGTH(equip))
  6447. i=pc_checkequip(sd,equip[num-1]);
  6448. if(i >= 0) {
  6449. sd->status.inventory[i].refine = 0;
  6450. pc_unequipitem(sd,i,3);
  6451. // 精錬失敗エフェクトのパケット
  6452. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6453. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6454. // 他の人にも失敗を通知
  6455. clif_misceffect(&sd->bl,2);
  6456. }
  6457. return 0;
  6458. }
  6459. /*==========================================
  6460. * Downgrades an Equipment Part by -1 . [Masao]
  6461. *------------------------------------------*/
  6462. BUILDIN_FUNC(downrefitem)
  6463. {
  6464. int i = -1,num,ep;
  6465. TBL_PC *sd;
  6466. num = script_getnum(st,2);
  6467. sd = script_rid2sd(st);
  6468. if( sd == NULL )
  6469. return 0;
  6470. if (num > 0 && num <= ARRAYLENGTH(equip))
  6471. i = pc_checkequip(sd,equip[num-1]);
  6472. if(i >= 0) {
  6473. ep = sd->status.inventory[i].equip;
  6474. //Logs items, got from (N)PC scripts [Lupus]
  6475. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6476. sd->status.inventory[i].refine++;
  6477. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6478. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine = sd->status.inventory[i].refine - 2);
  6479. clif_delitem(sd,i,1,3);
  6480. //Logs items, got from (N)PC scripts [Lupus]
  6481. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6482. clif_additem(sd,i,1,0);
  6483. pc_equipitem(sd,i,ep);
  6484. clif_misceffect(&sd->bl,2);
  6485. }
  6486. return 0;
  6487. }
  6488. /*==========================================
  6489. *
  6490. *------------------------------------------*/
  6491. BUILDIN_FUNC(statusup)
  6492. {
  6493. int type;
  6494. TBL_PC *sd;
  6495. type=script_getnum(st,2);
  6496. sd = script_rid2sd(st);
  6497. if( sd == NULL )
  6498. return 0;
  6499. pc_statusup(sd,type);
  6500. return 0;
  6501. }
  6502. /*==========================================
  6503. *
  6504. *------------------------------------------*/
  6505. BUILDIN_FUNC(statusup2)
  6506. {
  6507. int type,val;
  6508. TBL_PC *sd;
  6509. type=script_getnum(st,2);
  6510. val=script_getnum(st,3);
  6511. sd = script_rid2sd(st);
  6512. if( sd == NULL )
  6513. return 0;
  6514. pc_statusup2(sd,type,val);
  6515. return 0;
  6516. }
  6517. /// See 'doc/item_bonus.txt'
  6518. ///
  6519. /// bonus <bonus type>,<val1>;
  6520. /// bonus2 <bonus type>,<val1>,<val2>;
  6521. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6522. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6523. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6524. BUILDIN_FUNC(bonus)
  6525. {
  6526. int type;
  6527. int val1;
  6528. int val2 = 0;
  6529. int val3 = 0;
  6530. int val4 = 0;
  6531. int val5 = 0;
  6532. TBL_PC* sd;
  6533. sd = script_rid2sd(st);
  6534. if( sd == NULL )
  6535. return 0; // no player attached
  6536. type = script_getnum(st,2);
  6537. switch( type ) {
  6538. case SP_AUTOSPELL:
  6539. case SP_AUTOSPELL_WHENHIT:
  6540. case SP_AUTOSPELL_ONSKILL:
  6541. case SP_SKILL_ATK:
  6542. case SP_SKILL_HEAL:
  6543. case SP_SKILL_HEAL2:
  6544. case SP_ADD_SKILL_BLOW:
  6545. case SP_CASTRATE:
  6546. case SP_ADDEFF_ONSKILL:
  6547. case SP_SKILL_USE_SP_RATE:
  6548. case SP_SKILL_COOLDOWN:
  6549. case SP_SKILL_FIXEDCAST:
  6550. case SP_SKILL_VARIABLECAST:
  6551. case SP_SKILL_USE_SP:
  6552. // these bonuses support skill names
  6553. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6554. break;
  6555. default:
  6556. val1 = script_getnum(st,3);
  6557. break;
  6558. }
  6559. switch( script_lastdata(st)-2 ) {
  6560. case 1:
  6561. pc_bonus(sd, type, val1);
  6562. break;
  6563. case 2:
  6564. val2 = script_getnum(st,4);
  6565. pc_bonus2(sd, type, val1, val2);
  6566. break;
  6567. case 3:
  6568. val2 = script_getnum(st,4);
  6569. val3 = script_getnum(st,5);
  6570. pc_bonus3(sd, type, val1, val2, val3);
  6571. break;
  6572. case 4:
  6573. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6574. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6575. else
  6576. val2 = script_getnum(st,4);
  6577. val3 = script_getnum(st,5);
  6578. val4 = script_getnum(st,6);
  6579. pc_bonus4(sd, type, val1, val2, val3, val4);
  6580. break;
  6581. case 5:
  6582. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6583. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6584. else
  6585. val2 = script_getnum(st,4);
  6586. val3 = script_getnum(st,5);
  6587. val4 = script_getnum(st,6);
  6588. val5 = script_getnum(st,7);
  6589. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6590. break;
  6591. default:
  6592. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6593. break;
  6594. }
  6595. return 0;
  6596. }
  6597. BUILDIN_FUNC(autobonus)
  6598. {
  6599. unsigned int dur;
  6600. short rate;
  6601. short atk_type = 0;
  6602. TBL_PC* sd;
  6603. const char *bonus_script, *other_script = NULL;
  6604. sd = script_rid2sd(st);
  6605. if( sd == NULL )
  6606. return 0; // no player attached
  6607. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6608. return 0;
  6609. rate = script_getnum(st,3);
  6610. dur = script_getnum(st,4);
  6611. bonus_script = script_getstr(st,2);
  6612. if( !rate || !dur || !bonus_script )
  6613. return 0;
  6614. if( script_hasdata(st,5) )
  6615. atk_type = script_getnum(st,5);
  6616. if( script_hasdata(st,6) )
  6617. other_script = script_getstr(st,6);
  6618. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6619. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6620. {
  6621. script_add_autobonus(bonus_script);
  6622. if( other_script )
  6623. script_add_autobonus(other_script);
  6624. }
  6625. return 0;
  6626. }
  6627. BUILDIN_FUNC(autobonus2)
  6628. {
  6629. unsigned int dur;
  6630. short rate;
  6631. short atk_type = 0;
  6632. TBL_PC* sd;
  6633. const char *bonus_script, *other_script = NULL;
  6634. sd = script_rid2sd(st);
  6635. if( sd == NULL )
  6636. return 0; // no player attached
  6637. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6638. return 0;
  6639. rate = script_getnum(st,3);
  6640. dur = script_getnum(st,4);
  6641. bonus_script = script_getstr(st,2);
  6642. if( !rate || !dur || !bonus_script )
  6643. return 0;
  6644. if( script_hasdata(st,5) )
  6645. atk_type = script_getnum(st,5);
  6646. if( script_hasdata(st,6) )
  6647. other_script = script_getstr(st,6);
  6648. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6649. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6650. {
  6651. script_add_autobonus(bonus_script);
  6652. if( other_script )
  6653. script_add_autobonus(other_script);
  6654. }
  6655. return 0;
  6656. }
  6657. BUILDIN_FUNC(autobonus3)
  6658. {
  6659. unsigned int dur;
  6660. short rate,atk_type;
  6661. TBL_PC* sd;
  6662. const char *bonus_script, *other_script = NULL;
  6663. sd = script_rid2sd(st);
  6664. if( sd == NULL )
  6665. return 0; // no player attached
  6666. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6667. return 0;
  6668. rate = script_getnum(st,3);
  6669. dur = script_getnum(st,4);
  6670. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6671. bonus_script = script_getstr(st,2);
  6672. if( !rate || !dur || !atk_type || !bonus_script )
  6673. return 0;
  6674. if( script_hasdata(st,6) )
  6675. other_script = script_getstr(st,6);
  6676. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6677. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6678. {
  6679. script_add_autobonus(bonus_script);
  6680. if( other_script )
  6681. script_add_autobonus(other_script);
  6682. }
  6683. return 0;
  6684. }
  6685. /// Changes the level of a player skill.
  6686. /// <flag> defaults to 1
  6687. /// <flag>=0 : set the level of the skill
  6688. /// <flag>=1 : set the temporary level of the skill
  6689. /// <flag>=2 : add to the level of the skill
  6690. ///
  6691. /// skill <skill id>,<level>,<flag>
  6692. /// skill <skill id>,<level>
  6693. /// skill "<skill name>",<level>,<flag>
  6694. /// skill "<skill name>",<level>
  6695. BUILDIN_FUNC(skill)
  6696. {
  6697. int id;
  6698. int level;
  6699. int flag = 1;
  6700. TBL_PC* sd;
  6701. sd = script_rid2sd(st);
  6702. if( sd == NULL )
  6703. return 0;// no player attached, report source
  6704. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6705. level = script_getnum(st,3);
  6706. if( script_hasdata(st,4) )
  6707. flag = script_getnum(st,4);
  6708. pc_skill(sd, id, level, flag);
  6709. return 0;
  6710. }
  6711. /// Changes the level of a player skill.
  6712. /// like skill, but <flag> defaults to 2
  6713. ///
  6714. /// addtoskill <skill id>,<amount>,<flag>
  6715. /// addtoskill <skill id>,<amount>
  6716. /// addtoskill "<skill name>",<amount>,<flag>
  6717. /// addtoskill "<skill name>",<amount>
  6718. ///
  6719. /// @see skill
  6720. BUILDIN_FUNC(addtoskill)
  6721. {
  6722. int id;
  6723. int level;
  6724. int flag = 2;
  6725. TBL_PC* sd;
  6726. sd = script_rid2sd(st);
  6727. if( sd == NULL )
  6728. return 0;// no player attached, report source
  6729. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6730. level = script_getnum(st,3);
  6731. if( script_hasdata(st,4) )
  6732. flag = script_getnum(st,4);
  6733. pc_skill(sd, id, level, flag);
  6734. return 0;
  6735. }
  6736. /// Increases the level of a guild skill.
  6737. ///
  6738. /// guildskill <skill id>,<amount>;
  6739. /// guildskill "<skill name>",<amount>;
  6740. BUILDIN_FUNC(guildskill)
  6741. {
  6742. int id;
  6743. int level;
  6744. TBL_PC* sd;
  6745. int i;
  6746. sd = script_rid2sd(st);
  6747. if( sd == NULL )
  6748. return 0;// no player attached, report source
  6749. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6750. level = script_getnum(st,3);
  6751. for( i=0; i < level; i++ )
  6752. guild_skillup(sd, id);
  6753. return 0;
  6754. }
  6755. /// Returns the level of the player skill.
  6756. ///
  6757. /// getskilllv(<skill id>) -> <level>
  6758. /// getskilllv("<skill name>") -> <level>
  6759. BUILDIN_FUNC(getskilllv)
  6760. {
  6761. int id;
  6762. TBL_PC* sd;
  6763. sd = script_rid2sd(st);
  6764. if( sd == NULL )
  6765. return 0;// no player attached, report source
  6766. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6767. script_pushint(st, pc_checkskill(sd,id));
  6768. return 0;
  6769. }
  6770. /// Returns the level of the guild skill.
  6771. ///
  6772. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6773. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6774. BUILDIN_FUNC(getgdskilllv)
  6775. {
  6776. int guild_id;
  6777. int skill_id;
  6778. struct guild* g;
  6779. guild_id = script_getnum(st,2);
  6780. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6781. g = guild_search(guild_id);
  6782. if( g == NULL )
  6783. script_pushint(st, -1);
  6784. else
  6785. script_pushint(st, guild_checkskill(g,skill_id));
  6786. return 0;
  6787. }
  6788. /// Returns the 'basic_skill_check' setting.
  6789. /// This config determines if the server checks the skill level of NV_BASIC
  6790. /// before allowing the basic actions.
  6791. ///
  6792. /// basicskillcheck() -> <bool>
  6793. BUILDIN_FUNC(basicskillcheck)
  6794. {
  6795. script_pushint(st, battle_config.basic_skill_check);
  6796. return 0;
  6797. }
  6798. /// Returns the GM level of the player.
  6799. ///
  6800. /// getgmlevel() -> <level>
  6801. BUILDIN_FUNC(getgmlevel)
  6802. {
  6803. TBL_PC* sd;
  6804. sd = script_rid2sd(st);
  6805. if( sd == NULL )
  6806. return 0;// no player attached, report source
  6807. script_pushint(st, pc_get_group_level(sd));
  6808. return 0;
  6809. }
  6810. /// Returns the group ID of the player.
  6811. ///
  6812. /// getgroupid() -> <int>
  6813. BUILDIN_FUNC(getgroupid)
  6814. {
  6815. TBL_PC* sd;
  6816. sd = script_rid2sd(st);
  6817. if (sd == NULL)
  6818. return 1; // no player attached, report source
  6819. script_pushint(st, pc_get_group_id(sd));
  6820. return 0;
  6821. }
  6822. /// Terminates the execution of this script instance.
  6823. ///
  6824. /// end
  6825. BUILDIN_FUNC(end)
  6826. {
  6827. st->state = END;
  6828. return 0;
  6829. }
  6830. /// Checks if the player has that effect state (option).
  6831. ///
  6832. /// checkoption(<option>) -> <bool>
  6833. BUILDIN_FUNC(checkoption)
  6834. {
  6835. int option;
  6836. TBL_PC* sd;
  6837. sd = script_rid2sd(st);
  6838. if( sd == NULL )
  6839. return 0;// no player attached, report source
  6840. option = script_getnum(st,2);
  6841. if( sd->sc.option&option )
  6842. script_pushint(st, 1);
  6843. else
  6844. script_pushint(st, 0);
  6845. return 0;
  6846. }
  6847. /// Checks if the player is in that body state (opt1).
  6848. ///
  6849. /// checkoption1(<opt1>) -> <bool>
  6850. BUILDIN_FUNC(checkoption1)
  6851. {
  6852. int opt1;
  6853. TBL_PC* sd;
  6854. sd = script_rid2sd(st);
  6855. if( sd == NULL )
  6856. return 0;// no player attached, report source
  6857. opt1 = script_getnum(st,2);
  6858. if( sd->sc.opt1 == opt1 )
  6859. script_pushint(st, 1);
  6860. else
  6861. script_pushint(st, 0);
  6862. return 0;
  6863. }
  6864. /// Checks if the player has that health state (opt2).
  6865. ///
  6866. /// checkoption2(<opt2>) -> <bool>
  6867. BUILDIN_FUNC(checkoption2)
  6868. {
  6869. int opt2;
  6870. TBL_PC* sd;
  6871. sd = script_rid2sd(st);
  6872. if( sd == NULL )
  6873. return 0;// no player attached, report source
  6874. opt2 = script_getnum(st,2);
  6875. if( sd->sc.opt2&opt2 )
  6876. script_pushint(st, 1);
  6877. else
  6878. script_pushint(st, 0);
  6879. return 0;
  6880. }
  6881. /// Changes the effect state (option) of the player.
  6882. /// <flag> defaults to 1
  6883. /// <flag>=0 : removes the option
  6884. /// <flag>=other : adds the option
  6885. ///
  6886. /// setoption <option>,<flag>;
  6887. /// setoption <option>;
  6888. BUILDIN_FUNC(setoption)
  6889. {
  6890. int option;
  6891. int flag = 1;
  6892. TBL_PC* sd;
  6893. sd = script_rid2sd(st);
  6894. if( sd == NULL )
  6895. return 0;// no player attached, report source
  6896. option = script_getnum(st,2);
  6897. if( script_hasdata(st,3) )
  6898. flag = script_getnum(st,3);
  6899. else if( !option ){// Request to remove everything.
  6900. flag = 0;
  6901. option = OPTION_FALCON|OPTION_RIDING;
  6902. #ifndef NEW_CARTS
  6903. option |= OPTION_CART;
  6904. #endif
  6905. }
  6906. if( flag ){// Add option
  6907. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6908. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6909. pc_setoption(sd, sd->sc.option|option);
  6910. } else// Remove option
  6911. pc_setoption(sd, sd->sc.option&~option);
  6912. return 0;
  6913. }
  6914. /// Returns if the player has a cart.
  6915. ///
  6916. /// checkcart() -> <bool>
  6917. ///
  6918. /// @author Valaris
  6919. BUILDIN_FUNC(checkcart)
  6920. {
  6921. TBL_PC* sd;
  6922. sd = script_rid2sd(st);
  6923. if( sd == NULL )
  6924. return 0;// no player attached, report source
  6925. if( pc_iscarton(sd) )
  6926. script_pushint(st, 1);
  6927. else
  6928. script_pushint(st, 0);
  6929. return 0;
  6930. }
  6931. /// Sets the cart of the player.
  6932. /// <type> defaults to 1
  6933. /// <type>=0 : removes the cart
  6934. /// <type>=1 : Normal cart
  6935. /// <type>=2 : Wooden cart
  6936. /// <type>=3 : Covered cart with flowers and ferns
  6937. /// <type>=4 : Wooden cart with a Panda doll on the back
  6938. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6939. ///
  6940. /// setcart <type>;
  6941. /// setcart;
  6942. BUILDIN_FUNC(setcart)
  6943. {
  6944. int type = 1;
  6945. TBL_PC* sd;
  6946. sd = script_rid2sd(st);
  6947. if( sd == NULL )
  6948. return 0;// no player attached, report source
  6949. if( script_hasdata(st,2) )
  6950. type = script_getnum(st,2);
  6951. pc_setcart(sd, type);
  6952. return 0;
  6953. }
  6954. /// Returns if the player has a falcon.
  6955. ///
  6956. /// checkfalcon() -> <bool>
  6957. ///
  6958. /// @author Valaris
  6959. BUILDIN_FUNC(checkfalcon)
  6960. {
  6961. TBL_PC* sd;
  6962. sd = script_rid2sd(st);
  6963. if( sd == NULL )
  6964. return 0;// no player attached, report source
  6965. if( pc_isfalcon(sd) )
  6966. script_pushint(st, 1);
  6967. else
  6968. script_pushint(st, 0);
  6969. return 0;
  6970. }
  6971. /// Sets if the player has a falcon or not.
  6972. /// <flag> defaults to 1
  6973. ///
  6974. /// setfalcon <flag>;
  6975. /// setfalcon;
  6976. BUILDIN_FUNC(setfalcon)
  6977. {
  6978. int flag = 1;
  6979. TBL_PC* sd;
  6980. sd = script_rid2sd(st);
  6981. if( sd == NULL )
  6982. return 0;// no player attached, report source
  6983. if( script_hasdata(st,2) )
  6984. flag = script_getnum(st,2);
  6985. pc_setfalcon(sd, flag);
  6986. return 0;
  6987. }
  6988. /// Returns if the player is riding.
  6989. ///
  6990. /// checkriding() -> <bool>
  6991. ///
  6992. /// @author Valaris
  6993. BUILDIN_FUNC(checkriding)
  6994. {
  6995. TBL_PC* sd;
  6996. sd = script_rid2sd(st);
  6997. if( sd == NULL )
  6998. return 0;// no player attached, report source
  6999. if( pc_isriding(sd) || pc_isridingwug(sd) || pc_isridingdragon(sd) )
  7000. script_pushint(st, 1);
  7001. else
  7002. script_pushint(st, 0);
  7003. return 0;
  7004. }
  7005. /// Sets if the player is riding.
  7006. /// <flag> defaults to 1
  7007. ///
  7008. /// setriding <flag>;
  7009. /// setriding;
  7010. BUILDIN_FUNC(setriding)
  7011. {
  7012. int flag = 1;
  7013. TBL_PC* sd;
  7014. sd = script_rid2sd(st);
  7015. if( sd == NULL )
  7016. return 0;// no player attached, report source
  7017. if( script_hasdata(st,2) )
  7018. flag = script_getnum(st,2);
  7019. pc_setriding(sd, flag);
  7020. return 0;
  7021. }
  7022. /// Returns if the player has a warg.
  7023. ///
  7024. /// checkwug() -> <bool>
  7025. ///
  7026. BUILDIN_FUNC(checkwug)
  7027. {
  7028. TBL_PC* sd;
  7029. sd = script_rid2sd(st);
  7030. if( sd == NULL )
  7031. return 0;// no player attached, report source
  7032. if( pc_iswug(sd) )
  7033. script_pushint(st, 1);
  7034. else
  7035. script_pushint(st, 0);
  7036. return 0;
  7037. }
  7038. /// Returns if the player is wearing MADO Gear.
  7039. ///
  7040. /// checkmadogear() -> <bool>
  7041. ///
  7042. BUILDIN_FUNC(checkmadogear)
  7043. {
  7044. TBL_PC* sd;
  7045. sd = script_rid2sd(st);
  7046. if( sd == NULL )
  7047. return 0;// no player attached, report source
  7048. if( pc_ismadogear(sd) )
  7049. script_pushint(st, 1);
  7050. else
  7051. script_pushint(st, 0);
  7052. return 0;
  7053. }
  7054. /// Sets if the player is riding MADO Gear.
  7055. /// <flag> defaults to 1
  7056. ///
  7057. /// setmadogear <flag>;
  7058. /// setmadogear;
  7059. BUILDIN_FUNC(setmadogear)
  7060. {
  7061. int flag = 1;
  7062. TBL_PC* sd;
  7063. sd = script_rid2sd(st);
  7064. if( sd == NULL )
  7065. return 0;// no player attached, report source
  7066. if( script_hasdata(st,2) )
  7067. flag = script_getnum(st,2);
  7068. pc_setmadogear(sd, flag);
  7069. return 0;
  7070. }
  7071. /// Sets the save point of the player.
  7072. ///
  7073. /// save "<map name>",<x>,<y>
  7074. /// savepoint "<map name>",<x>,<y>
  7075. BUILDIN_FUNC(savepoint)
  7076. {
  7077. int x;
  7078. int y;
  7079. short map;
  7080. const char* str;
  7081. TBL_PC* sd;
  7082. sd = script_rid2sd(st);
  7083. if( sd == NULL )
  7084. return 0;// no player attached, report source
  7085. str = script_getstr(st, 2);
  7086. x = script_getnum(st,3);
  7087. y = script_getnum(st,4);
  7088. map = mapindex_name2id(str);
  7089. if( map )
  7090. pc_setsavepoint(sd, map, x, y);
  7091. return 0;
  7092. }
  7093. /*==========================================
  7094. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  7095. *------------------------------------------*/
  7096. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  7097. {
  7098. int type;
  7099. time_t timer;
  7100. struct tm *t;
  7101. type=script_getnum(st,2);
  7102. switch(type){
  7103. case 2:
  7104. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  7105. // from the system clock.)
  7106. script_pushint(st,(int)time(NULL));
  7107. break;
  7108. case 1:
  7109. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  7110. time(&timer);
  7111. t=localtime(&timer);
  7112. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  7113. break;
  7114. case 0:
  7115. default:
  7116. //type 0:(System Ticks)
  7117. script_pushint(st,gettick());
  7118. break;
  7119. }
  7120. return 0;
  7121. }
  7122. /*==========================================
  7123. * GetTime(Type);
  7124. * 1: Sec 2: Min 3: Hour
  7125. * 4: WeekDay 5: MonthDay 6: Month
  7126. * 7: Year
  7127. *------------------------------------------*/
  7128. BUILDIN_FUNC(gettime) /* Asgard Version */
  7129. {
  7130. int type;
  7131. time_t timer;
  7132. struct tm *t;
  7133. type=script_getnum(st,2);
  7134. time(&timer);
  7135. t=localtime(&timer);
  7136. switch(type){
  7137. case 1://Sec(0~59)
  7138. script_pushint(st,t->tm_sec);
  7139. break;
  7140. case 2://Min(0~59)
  7141. script_pushint(st,t->tm_min);
  7142. break;
  7143. case 3://Hour(0~23)
  7144. script_pushint(st,t->tm_hour);
  7145. break;
  7146. case 4://WeekDay(0~6)
  7147. script_pushint(st,t->tm_wday);
  7148. break;
  7149. case 5://MonthDay(01~31)
  7150. script_pushint(st,t->tm_mday);
  7151. break;
  7152. case 6://Month(01~12)
  7153. script_pushint(st,t->tm_mon+1);
  7154. break;
  7155. case 7://Year(20xx)
  7156. script_pushint(st,t->tm_year+1900);
  7157. break;
  7158. case 8://Year Day(01~366)
  7159. script_pushint(st,t->tm_yday+1);
  7160. break;
  7161. default://(format error)
  7162. script_pushint(st,-1);
  7163. break;
  7164. }
  7165. return 0;
  7166. }
  7167. /*==========================================
  7168. * GetTimeStr("TimeFMT", Length);
  7169. *------------------------------------------*/
  7170. BUILDIN_FUNC(gettimestr)
  7171. {
  7172. char *tmpstr;
  7173. const char *fmtstr;
  7174. int maxlen;
  7175. time_t now = time(NULL);
  7176. fmtstr=script_getstr(st,2);
  7177. maxlen=script_getnum(st,3);
  7178. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  7179. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7180. tmpstr[maxlen]='\0';
  7181. script_pushstr(st,tmpstr);
  7182. return 0;
  7183. }
  7184. /*==========================================
  7185. * カプラ倉庫を開く
  7186. *------------------------------------------*/
  7187. BUILDIN_FUNC(openstorage)
  7188. {
  7189. TBL_PC* sd;
  7190. sd = script_rid2sd(st);
  7191. if( sd == NULL )
  7192. return 0;
  7193. storage_storageopen(sd);
  7194. return 0;
  7195. }
  7196. BUILDIN_FUNC(guildopenstorage)
  7197. {
  7198. TBL_PC* sd;
  7199. int ret;
  7200. sd = script_rid2sd(st);
  7201. if( sd == NULL )
  7202. return 0;
  7203. ret = storage_guild_storageopen(sd);
  7204. script_pushint(st,ret);
  7205. return 0;
  7206. }
  7207. /*==========================================
  7208. * アイテムによるスキル発動
  7209. *------------------------------------------*/
  7210. /// itemskill <skill id>,<level>
  7211. /// itemskill "<skill name>",<level>
  7212. BUILDIN_FUNC(itemskill)
  7213. {
  7214. int id;
  7215. int lv;
  7216. TBL_PC* sd;
  7217. sd = script_rid2sd(st);
  7218. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  7219. return 0;
  7220. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7221. lv = script_getnum(st,3);
  7222. sd->skillitem=id;
  7223. sd->skillitemlv=lv;
  7224. clif_item_skill(sd,id,lv);
  7225. return 0;
  7226. }
  7227. /*==========================================
  7228. * アイテム作成
  7229. *------------------------------------------*/
  7230. BUILDIN_FUNC(produce)
  7231. {
  7232. int trigger;
  7233. TBL_PC* sd;
  7234. sd = script_rid2sd(st);
  7235. if( sd == NULL )
  7236. return 0;
  7237. trigger=script_getnum(st,2);
  7238. clif_skill_produce_mix_list(sd, -1, trigger);
  7239. return 0;
  7240. }
  7241. /*==========================================
  7242. *
  7243. *------------------------------------------*/
  7244. BUILDIN_FUNC(cooking)
  7245. {
  7246. int trigger;
  7247. TBL_PC* sd;
  7248. sd = script_rid2sd(st);
  7249. if( sd == NULL )
  7250. return 0;
  7251. trigger=script_getnum(st,2);
  7252. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  7253. return 0;
  7254. }
  7255. /*==========================================
  7256. * NPCでペット作る
  7257. *------------------------------------------*/
  7258. BUILDIN_FUNC(makepet)
  7259. {
  7260. TBL_PC* sd;
  7261. int id,pet_id;
  7262. id=script_getnum(st,2);
  7263. sd = script_rid2sd(st);
  7264. if( sd == NULL )
  7265. return 0;
  7266. pet_id = search_petDB_index(id, PET_CLASS);
  7267. if (pet_id < 0)
  7268. pet_id = search_petDB_index(id, PET_EGG);
  7269. if (pet_id >= 0 && sd) {
  7270. sd->catch_target_class = pet_db[pet_id].class_;
  7271. intif_create_pet(
  7272. sd->status.account_id, sd->status.char_id,
  7273. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7274. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7275. 100, 0, 1, pet_db[pet_id].jname);
  7276. }
  7277. return 0;
  7278. }
  7279. /*==========================================
  7280. * NPCで経験値上げる
  7281. *------------------------------------------*/
  7282. BUILDIN_FUNC(getexp)
  7283. {
  7284. TBL_PC* sd;
  7285. int base=0,job=0;
  7286. double bonus;
  7287. sd = script_rid2sd(st);
  7288. if( sd == NULL )
  7289. return 0;
  7290. base=script_getnum(st,2);
  7291. job =script_getnum(st,3);
  7292. if(base<0 || job<0)
  7293. return 0;
  7294. // bonus for npc-given exp
  7295. bonus = battle_config.quest_exp_rate / 100.;
  7296. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7297. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7298. pc_gainexp(sd, NULL, base, job, true);
  7299. return 0;
  7300. }
  7301. /*==========================================
  7302. * Gain guild exp [Celest]
  7303. *------------------------------------------*/
  7304. BUILDIN_FUNC(guildgetexp)
  7305. {
  7306. TBL_PC* sd;
  7307. int exp;
  7308. sd = script_rid2sd(st);
  7309. if( sd == NULL )
  7310. return 0;
  7311. exp = script_getnum(st,2);
  7312. if(exp < 0)
  7313. return 0;
  7314. if(sd && sd->status.guild_id > 0)
  7315. guild_getexp (sd, exp);
  7316. return 0;
  7317. }
  7318. /*==========================================
  7319. * Changes the guild master of a guild [Skotlex]
  7320. *------------------------------------------*/
  7321. BUILDIN_FUNC(guildchangegm)
  7322. {
  7323. TBL_PC *sd;
  7324. int guild_id;
  7325. const char *name;
  7326. guild_id = script_getnum(st,2);
  7327. name = script_getstr(st,3);
  7328. sd=map_nick2sd(name);
  7329. if (!sd)
  7330. script_pushint(st,0);
  7331. else
  7332. script_pushint(st,guild_gm_change(guild_id, sd));
  7333. return 0;
  7334. }
  7335. /*==========================================
  7336. * モンスター発生
  7337. *------------------------------------------*/
  7338. BUILDIN_FUNC(monster)
  7339. {
  7340. const char* mapn = script_getstr(st,2);
  7341. int x = script_getnum(st,3);
  7342. int y = script_getnum(st,4);
  7343. const char* str = script_getstr(st,5);
  7344. int class_ = script_getnum(st,6);
  7345. int amount = script_getnum(st,7);
  7346. const char* event = "";
  7347. struct map_session_data* sd;
  7348. int m;
  7349. if( script_hasdata(st,8) )
  7350. {
  7351. event = script_getstr(st,8);
  7352. check_event(st, event);
  7353. }
  7354. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7355. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7356. return 1;
  7357. }
  7358. sd = map_id2sd(st->rid);
  7359. if( sd && strcmp(mapn,"this") == 0 )
  7360. m = sd->bl.m;
  7361. else
  7362. {
  7363. m = map_mapname2mapid(mapn);
  7364. if( map[m].flag.src4instance && st->instance_id )
  7365. { // Try to redirect to the instance map, not the src map
  7366. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7367. {
  7368. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7369. return 1;
  7370. }
  7371. }
  7372. }
  7373. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  7374. return 0;
  7375. }
  7376. /*==========================================
  7377. * Request List of Monster Drops
  7378. *------------------------------------------*/
  7379. BUILDIN_FUNC(getmobdrops)
  7380. {
  7381. int class_ = script_getnum(st,2);
  7382. int i, j = 0;
  7383. struct mob_db *mob;
  7384. if( !mobdb_checkid(class_) )
  7385. {
  7386. script_pushint(st, 0);
  7387. return 0;
  7388. }
  7389. mob = mob_db(class_);
  7390. for( i = 0; i < MAX_MOB_DROP; i++ )
  7391. {
  7392. if( mob->dropitem[i].nameid < 1 )
  7393. continue;
  7394. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7395. continue;
  7396. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7397. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7398. j++;
  7399. }
  7400. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7401. script_pushint(st, 1);
  7402. return 0;
  7403. }
  7404. /*==========================================
  7405. * モンスター発生
  7406. *------------------------------------------*/
  7407. BUILDIN_FUNC(areamonster)
  7408. {
  7409. const char* mapn = script_getstr(st,2);
  7410. int x0 = script_getnum(st,3);
  7411. int y0 = script_getnum(st,4);
  7412. int x1 = script_getnum(st,5);
  7413. int y1 = script_getnum(st,6);
  7414. const char* str = script_getstr(st,7);
  7415. int class_ = script_getnum(st,8);
  7416. int amount = script_getnum(st,9);
  7417. const char* event = "";
  7418. struct map_session_data* sd;
  7419. int m;
  7420. if( script_hasdata(st,10) )
  7421. {
  7422. event = script_getstr(st,10);
  7423. check_event(st, event);
  7424. }
  7425. sd = map_id2sd(st->rid);
  7426. if( sd && strcmp(mapn,"this") == 0 )
  7427. m = sd->bl.m;
  7428. else
  7429. {
  7430. m = map_mapname2mapid(mapn);
  7431. if( map[m].flag.src4instance && st->instance_id )
  7432. { // Try to redirect to the instance map, not the src map
  7433. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7434. {
  7435. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7436. return 1;
  7437. }
  7438. }
  7439. }
  7440. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  7441. return 0;
  7442. }
  7443. /*==========================================
  7444. * モンスター削除
  7445. *------------------------------------------*/
  7446. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7447. { //same fix but with killmonster instead - stripping events from mobs.
  7448. TBL_MOB* md = (TBL_MOB*)bl;
  7449. char *event=va_arg(ap,char *);
  7450. int allflag=va_arg(ap,int);
  7451. md->state.npc_killmonster = 1;
  7452. if(!allflag){
  7453. if(strcmp(event,md->npc_event)==0)
  7454. status_kill(bl);
  7455. }else{
  7456. if(!md->spawn)
  7457. status_kill(bl);
  7458. }
  7459. md->state.npc_killmonster = 0;
  7460. return 0;
  7461. }
  7462. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7463. {
  7464. TBL_MOB* md = (TBL_MOB*)bl;
  7465. char *event=va_arg(ap,char *);
  7466. int allflag=va_arg(ap,int);
  7467. if(!allflag){
  7468. if(strcmp(event,md->npc_event)==0)
  7469. status_kill(bl);
  7470. }else{
  7471. if(!md->spawn)
  7472. status_kill(bl);
  7473. }
  7474. return 0;
  7475. }
  7476. BUILDIN_FUNC(killmonster)
  7477. {
  7478. const char *mapname,*event;
  7479. int m,allflag=0;
  7480. mapname=script_getstr(st,2);
  7481. event=script_getstr(st,3);
  7482. if(strcmp(event,"All")==0)
  7483. allflag = 1;
  7484. else
  7485. check_event(st, event);
  7486. if( (m=map_mapname2mapid(mapname))<0 )
  7487. return 0;
  7488. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7489. return 0;
  7490. if( script_hasdata(st,4) ) {
  7491. if ( script_getnum(st,4) == 1 ) {
  7492. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7493. return 0;
  7494. }
  7495. }
  7496. map_freeblock_lock();
  7497. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7498. map_freeblock_unlock();
  7499. return 0;
  7500. }
  7501. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7502. { //Strips the event from the mob if it's killed the old method.
  7503. struct mob_data *md;
  7504. md = BL_CAST(BL_MOB, bl);
  7505. if (md->npc_event[0])
  7506. md->npc_event[0] = 0;
  7507. status_kill(bl);
  7508. return 0;
  7509. }
  7510. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7511. {
  7512. status_kill(bl);
  7513. return 0;
  7514. }
  7515. BUILDIN_FUNC(killmonsterall)
  7516. {
  7517. const char *mapname;
  7518. int m;
  7519. mapname=script_getstr(st,2);
  7520. if( (m = map_mapname2mapid(mapname))<0 )
  7521. return 0;
  7522. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7523. return 0;
  7524. if( script_hasdata(st,3) ) {
  7525. if ( script_getnum(st,3) == 1 ) {
  7526. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7527. return 0;
  7528. }
  7529. }
  7530. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7531. return 0;
  7532. }
  7533. /*==========================================
  7534. * Creates a clone of a player.
  7535. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7536. *------------------------------------------*/
  7537. BUILDIN_FUNC(clone)
  7538. {
  7539. TBL_PC *sd, *msd=NULL;
  7540. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7541. unsigned int duration = 0;
  7542. const char *map,*event="";
  7543. map=script_getstr(st,2);
  7544. x=script_getnum(st,3);
  7545. y=script_getnum(st,4);
  7546. event=script_getstr(st,5);
  7547. char_id=script_getnum(st,6);
  7548. if( script_hasdata(st,7) )
  7549. master_id=script_getnum(st,7);
  7550. if( script_hasdata(st,8) )
  7551. mode=script_getnum(st,8);
  7552. if( script_hasdata(st,9) )
  7553. flag=script_getnum(st,9);
  7554. if( script_hasdata(st,10) )
  7555. duration=script_getnum(st,10);
  7556. check_event(st, event);
  7557. m = map_mapname2mapid(map);
  7558. if (m < 0) return 0;
  7559. sd = map_charid2sd(char_id);
  7560. if (master_id) {
  7561. msd = map_charid2sd(master_id);
  7562. if (msd)
  7563. master_id = msd->bl.id;
  7564. else
  7565. master_id = 0;
  7566. }
  7567. if (sd) //Return ID of newly crafted clone.
  7568. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7569. else //Failed to create clone.
  7570. script_pushint(st,0);
  7571. return 0;
  7572. }
  7573. /*==========================================
  7574. * イベント実行
  7575. *------------------------------------------*/
  7576. BUILDIN_FUNC(doevent)
  7577. {
  7578. const char* event = script_getstr(st,2);
  7579. struct map_session_data* sd;
  7580. if( ( sd = script_rid2sd(st) ) == NULL )
  7581. {
  7582. return 0;
  7583. }
  7584. check_event(st, event);
  7585. npc_event(sd, event, 0);
  7586. return 0;
  7587. }
  7588. /*==========================================
  7589. * NPC主体イベント実行
  7590. *------------------------------------------*/
  7591. BUILDIN_FUNC(donpcevent)
  7592. {
  7593. const char* event = script_getstr(st,2);
  7594. check_event(st, event);
  7595. npc_event_do(event);
  7596. return 0;
  7597. }
  7598. /// for Aegis compatibility
  7599. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7600. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7601. {
  7602. const char* npc = script_getstr(st,2);
  7603. const char* command = script_getstr(st,3);
  7604. char event[EVENT_NAME_LENGTH];
  7605. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  7606. check_event(st, event);
  7607. npc_event_do(event);
  7608. return 0;
  7609. }
  7610. /*==========================================
  7611. * イベントタイマー追加
  7612. *------------------------------------------*/
  7613. BUILDIN_FUNC(addtimer)
  7614. {
  7615. int tick = script_getnum(st,2);
  7616. const char* event = script_getstr(st, 3);
  7617. TBL_PC* sd;
  7618. check_event(st, event);
  7619. sd = script_rid2sd(st);
  7620. if( sd == NULL )
  7621. return 0;
  7622. pc_addeventtimer(sd,tick,event);
  7623. return 0;
  7624. }
  7625. /*==========================================
  7626. * イベントタイマー削除
  7627. *------------------------------------------*/
  7628. BUILDIN_FUNC(deltimer)
  7629. {
  7630. const char *event;
  7631. TBL_PC* sd;
  7632. event=script_getstr(st, 2);
  7633. sd = script_rid2sd(st);
  7634. if( sd == NULL )
  7635. return 0;
  7636. check_event(st, event);
  7637. pc_deleventtimer(sd,event);
  7638. return 0;
  7639. }
  7640. /*==========================================
  7641. * イベントタイマーのカウント値追加
  7642. *------------------------------------------*/
  7643. BUILDIN_FUNC(addtimercount)
  7644. {
  7645. const char *event;
  7646. int tick;
  7647. TBL_PC* sd;
  7648. event=script_getstr(st, 2);
  7649. tick=script_getnum(st,3);
  7650. sd = script_rid2sd(st);
  7651. if( sd == NULL )
  7652. return 0;
  7653. check_event(st, event);
  7654. pc_addeventtimercount(sd,event,tick);
  7655. return 0;
  7656. }
  7657. /*==========================================
  7658. * NPCタイマー初期化
  7659. *------------------------------------------*/
  7660. BUILDIN_FUNC(initnpctimer)
  7661. {
  7662. struct npc_data *nd;
  7663. int flag = 0;
  7664. if( script_hasdata(st,3) )
  7665. { //Two arguments: NPC name and attach flag.
  7666. nd = npc_name2id(script_getstr(st, 2));
  7667. flag = script_getnum(st,3);
  7668. }
  7669. else if( script_hasdata(st,2) )
  7670. { //Check if argument is numeric (flag) or string (npc name)
  7671. struct script_data *data;
  7672. data = script_getdata(st,2);
  7673. get_val(st,data);
  7674. if( data_isstring(data) ) //NPC name
  7675. nd = npc_name2id(conv_str(st, data));
  7676. else if( data_isint(data) ) //Flag
  7677. {
  7678. nd = (struct npc_data *)map_id2bl(st->oid);
  7679. flag = conv_num(st,data);
  7680. }
  7681. else
  7682. {
  7683. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7684. return 1;
  7685. }
  7686. }
  7687. else
  7688. nd = (struct npc_data *)map_id2bl(st->oid);
  7689. if( !nd )
  7690. return 0;
  7691. if( flag ) //Attach
  7692. {
  7693. TBL_PC* sd = script_rid2sd(st);
  7694. if( sd == NULL )
  7695. return 0;
  7696. nd->u.scr.rid = sd->bl.id;
  7697. }
  7698. npc_settimerevent_tick(nd,0);
  7699. npc_timerevent_start(nd, st->rid);
  7700. return 0;
  7701. }
  7702. /*==========================================
  7703. * NPCタイマー開始
  7704. *------------------------------------------*/
  7705. BUILDIN_FUNC(startnpctimer)
  7706. {
  7707. struct npc_data *nd;
  7708. int flag = 0;
  7709. if( script_hasdata(st,3) )
  7710. { //Two arguments: NPC name and attach flag.
  7711. nd = npc_name2id(script_getstr(st, 2));
  7712. flag = script_getnum(st,3);
  7713. }
  7714. else if( script_hasdata(st,2) )
  7715. { //Check if argument is numeric (flag) or string (npc name)
  7716. struct script_data *data;
  7717. data = script_getdata(st,2);
  7718. get_val(st,data);
  7719. if( data_isstring(data) ) //NPC name
  7720. nd = npc_name2id(conv_str(st, data));
  7721. else if( data_isint(data) ) //Flag
  7722. {
  7723. nd = (struct npc_data *)map_id2bl(st->oid);
  7724. flag = conv_num(st,data);
  7725. }
  7726. else
  7727. {
  7728. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7729. return 1;
  7730. }
  7731. }
  7732. else
  7733. nd=(struct npc_data *)map_id2bl(st->oid);
  7734. if( !nd )
  7735. return 0;
  7736. if( flag ) //Attach
  7737. {
  7738. TBL_PC* sd = script_rid2sd(st);
  7739. if( sd == NULL )
  7740. return 0;
  7741. nd->u.scr.rid = sd->bl.id;
  7742. }
  7743. npc_timerevent_start(nd, st->rid);
  7744. return 0;
  7745. }
  7746. /*==========================================
  7747. * NPCタイマー停止
  7748. *------------------------------------------*/
  7749. BUILDIN_FUNC(stopnpctimer)
  7750. {
  7751. struct npc_data *nd;
  7752. int flag = 0;
  7753. if( script_hasdata(st,3) )
  7754. { //Two arguments: NPC name and attach flag.
  7755. nd = npc_name2id(script_getstr(st, 2));
  7756. flag = script_getnum(st,3);
  7757. }
  7758. else if( script_hasdata(st,2) )
  7759. { //Check if argument is numeric (flag) or string (npc name)
  7760. struct script_data *data;
  7761. data = script_getdata(st,2);
  7762. get_val(st,data);
  7763. if( data_isstring(data) ) //NPC name
  7764. nd = npc_name2id(conv_str(st, data));
  7765. else if( data_isint(data) ) //Flag
  7766. {
  7767. nd = (struct npc_data *)map_id2bl(st->oid);
  7768. flag = conv_num(st,data);
  7769. }
  7770. else
  7771. {
  7772. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7773. return 1;
  7774. }
  7775. }
  7776. else
  7777. nd=(struct npc_data *)map_id2bl(st->oid);
  7778. if( !nd )
  7779. return 0;
  7780. if( flag ) //Detach
  7781. nd->u.scr.rid = 0;
  7782. npc_timerevent_stop(nd);
  7783. return 0;
  7784. }
  7785. /*==========================================
  7786. * NPCタイマー情報所得
  7787. *------------------------------------------*/
  7788. BUILDIN_FUNC(getnpctimer)
  7789. {
  7790. struct npc_data *nd;
  7791. TBL_PC *sd;
  7792. int type = script_getnum(st,2);
  7793. int val = 0;
  7794. if( script_hasdata(st,3) )
  7795. nd = npc_name2id(script_getstr(st,3));
  7796. else
  7797. nd = (struct npc_data *)map_id2bl(st->oid);
  7798. if( !nd || nd->bl.type != BL_NPC )
  7799. {
  7800. script_pushint(st,0);
  7801. ShowError("getnpctimer: Invalid NPC.\n");
  7802. return 1;
  7803. }
  7804. switch( type )
  7805. {
  7806. case 0: val = npc_gettimerevent_tick(nd); break;
  7807. case 1:
  7808. if( nd->u.scr.rid )
  7809. {
  7810. sd = map_id2sd(nd->u.scr.rid);
  7811. if( !sd )
  7812. {
  7813. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7814. break;
  7815. }
  7816. val = (sd->npc_timer_id != INVALID_TIMER);
  7817. }
  7818. else
  7819. val = (nd->u.scr.timerid != INVALID_TIMER);
  7820. break;
  7821. case 2: val = nd->u.scr.timeramount; break;
  7822. }
  7823. script_pushint(st,val);
  7824. return 0;
  7825. }
  7826. /*==========================================
  7827. * NPCタイマー値設定
  7828. *------------------------------------------*/
  7829. BUILDIN_FUNC(setnpctimer)
  7830. {
  7831. int tick;
  7832. struct npc_data *nd;
  7833. tick = script_getnum(st,2);
  7834. if( script_hasdata(st,3) )
  7835. nd = npc_name2id(script_getstr(st,3));
  7836. else
  7837. nd = (struct npc_data *)map_id2bl(st->oid);
  7838. if( !nd || nd->bl.type != BL_NPC )
  7839. {
  7840. script_pushint(st,1);
  7841. ShowError("setnpctimer: Invalid NPC.\n");
  7842. return 1;
  7843. }
  7844. npc_settimerevent_tick(nd,tick);
  7845. script_pushint(st,0);
  7846. return 0;
  7847. }
  7848. /*==========================================
  7849. * attaches the player rid to the timer [Celest]
  7850. *------------------------------------------*/
  7851. BUILDIN_FUNC(attachnpctimer)
  7852. {
  7853. TBL_PC *sd;
  7854. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7855. if( !nd || nd->bl.type != BL_NPC )
  7856. {
  7857. script_pushint(st,1);
  7858. ShowError("setnpctimer: Invalid NPC.\n");
  7859. return 1;
  7860. }
  7861. if( script_hasdata(st,2) )
  7862. sd = map_nick2sd(script_getstr(st,2));
  7863. else
  7864. sd = script_rid2sd(st);
  7865. if( !sd )
  7866. {
  7867. script_pushint(st,1);
  7868. ShowWarning("attachnpctimer: Invalid player.\n");
  7869. return 1;
  7870. }
  7871. nd->u.scr.rid = sd->bl.id;
  7872. script_pushint(st,0);
  7873. return 0;
  7874. }
  7875. /*==========================================
  7876. * detaches a player rid from the timer [Celest]
  7877. *------------------------------------------*/
  7878. BUILDIN_FUNC(detachnpctimer)
  7879. {
  7880. struct npc_data *nd;
  7881. if( script_hasdata(st,2) )
  7882. nd = npc_name2id(script_getstr(st,2));
  7883. else
  7884. nd = (struct npc_data *)map_id2bl(st->oid);
  7885. if( !nd || nd->bl.type != BL_NPC )
  7886. {
  7887. script_pushint(st,1);
  7888. ShowError("detachnpctimer: Invalid NPC.\n");
  7889. return 1;
  7890. }
  7891. nd->u.scr.rid = 0;
  7892. script_pushint(st,0);
  7893. return 0;
  7894. }
  7895. /*==========================================
  7896. * To avoid "player not attached" script errors, this function is provided,
  7897. * it checks if there is a player attached to the current script. [Skotlex]
  7898. * If no, returns 0, if yes, returns the account_id of the attached player.
  7899. *------------------------------------------*/
  7900. BUILDIN_FUNC(playerattached)
  7901. {
  7902. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7903. script_pushint(st,0);
  7904. else
  7905. script_pushint(st,st->rid);
  7906. return 0;
  7907. }
  7908. /*==========================================
  7909. * 天の声アナウンス
  7910. *------------------------------------------*/
  7911. BUILDIN_FUNC(announce)
  7912. {
  7913. const char *mes = script_getstr(st,2);
  7914. int flag = script_getnum(st,3);
  7915. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7916. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7917. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7918. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7919. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7920. if (flag&0x0f) // Broadcast source or broadcast region defined
  7921. {
  7922. send_target target;
  7923. struct block_list *bl = (flag&0x08) ? map_id2bl(st->oid) : (struct block_list *)script_rid2sd(st); // If bc_npc flag is set, use NPC as broadcast source
  7924. if (bl == NULL)
  7925. return 0;
  7926. flag &= 0x07;
  7927. target = (flag == 1) ? ALL_SAMEMAP :
  7928. (flag == 2) ? AREA :
  7929. (flag == 3) ? SELF :
  7930. ALL_CLIENT;
  7931. if (fontColor)
  7932. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7933. else
  7934. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7935. }
  7936. else
  7937. {
  7938. if (fontColor)
  7939. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7940. else
  7941. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7942. }
  7943. return 0;
  7944. }
  7945. /*==========================================
  7946. * 天の声アナウンス(特定マップ)
  7947. *------------------------------------------*/
  7948. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7949. {
  7950. char *mes = va_arg(ap, char *);
  7951. int len = va_arg(ap, int);
  7952. int type = va_arg(ap, int);
  7953. char *fontColor = va_arg(ap, char *);
  7954. short fontType = (short)va_arg(ap, int);
  7955. short fontSize = (short)va_arg(ap, int);
  7956. short fontAlign = (short)va_arg(ap, int);
  7957. short fontY = (short)va_arg(ap, int);
  7958. if (fontColor)
  7959. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7960. else
  7961. clif_broadcast(bl, mes, len, type, SELF);
  7962. return 0;
  7963. }
  7964. BUILDIN_FUNC(mapannounce)
  7965. {
  7966. const char *mapname = script_getstr(st,2);
  7967. const char *mes = script_getstr(st,3);
  7968. int flag = script_getnum(st,4);
  7969. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7970. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7971. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7972. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7973. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7974. int m;
  7975. if ((m = map_mapname2mapid(mapname)) < 0)
  7976. return 0;
  7977. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7978. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7979. return 0;
  7980. }
  7981. /*==========================================
  7982. * 天の声アナウンス(特定エリア)
  7983. *------------------------------------------*/
  7984. BUILDIN_FUNC(areaannounce)
  7985. {
  7986. const char *mapname = script_getstr(st,2);
  7987. int x0 = script_getnum(st,3);
  7988. int y0 = script_getnum(st,4);
  7989. int x1 = script_getnum(st,5);
  7990. int y1 = script_getnum(st,6);
  7991. const char *mes = script_getstr(st,7);
  7992. int flag = script_getnum(st,8);
  7993. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7994. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7995. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7996. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7997. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7998. int m;
  7999. if ((m = map_mapname2mapid(mapname)) < 0)
  8000. return 0;
  8001. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8002. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8003. return 0;
  8004. }
  8005. /*==========================================
  8006. * ユーザー数所得
  8007. *------------------------------------------*/
  8008. BUILDIN_FUNC(getusers)
  8009. {
  8010. int flag, val = 0;
  8011. struct map_session_data* sd;
  8012. struct block_list* bl = NULL;
  8013. flag = script_getnum(st,2);
  8014. switch(flag&0x07)
  8015. {
  8016. case 0:
  8017. if(flag&0x8)
  8018. {// npc
  8019. bl = map_id2bl(st->oid);
  8020. }
  8021. else if((sd = script_rid2sd(st))!=NULL)
  8022. {// pc
  8023. bl = &sd->bl;
  8024. }
  8025. if(bl)
  8026. {
  8027. val = map[bl->m].users;
  8028. }
  8029. break;
  8030. case 1:
  8031. val = map_getusers();
  8032. break;
  8033. default:
  8034. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8035. script_pushint(st,0);
  8036. return 1;
  8037. }
  8038. script_pushint(st,val);
  8039. return 0;
  8040. }
  8041. /*==========================================
  8042. * Works like @WHO - displays all online users names in window
  8043. *------------------------------------------*/
  8044. BUILDIN_FUNC(getusersname)
  8045. {
  8046. TBL_PC *sd, *pl_sd;
  8047. int disp_num=1, group_level = 0;
  8048. struct s_mapiterator* iter;
  8049. sd = script_rid2sd(st);
  8050. if (!sd) return 0;
  8051. group_level = pc_get_group_level(sd);
  8052. iter = mapit_getallusers();
  8053. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  8054. {
  8055. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  8056. continue; // skip hidden sessions
  8057. if((disp_num++)%10==0)
  8058. clif_scriptnext(sd,st->oid);
  8059. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  8060. }
  8061. mapit_free(iter);
  8062. return 0;
  8063. }
  8064. /*==========================================
  8065. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  8066. *------------------------------------------*/
  8067. BUILDIN_FUNC(getmapguildusers)
  8068. {
  8069. const char *str;
  8070. int m, gid;
  8071. int i=0,c=0;
  8072. struct guild *g = NULL;
  8073. str=script_getstr(st,2);
  8074. gid=script_getnum(st,3);
  8075. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  8076. script_pushint(st,-1);
  8077. return 0;
  8078. }
  8079. g = guild_search(gid);
  8080. if (g){
  8081. for(i = 0; i < g->max_member; i++)
  8082. {
  8083. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  8084. c++;
  8085. }
  8086. }
  8087. script_pushint(st,c);
  8088. return 0;
  8089. }
  8090. /*==========================================
  8091. * マップ指定ユーザー数所得
  8092. *------------------------------------------*/
  8093. BUILDIN_FUNC(getmapusers)
  8094. {
  8095. const char *str;
  8096. int m;
  8097. str=script_getstr(st,2);
  8098. if( (m=map_mapname2mapid(str))< 0){
  8099. script_pushint(st,-1);
  8100. return 0;
  8101. }
  8102. script_pushint(st,map[m].users);
  8103. return 0;
  8104. }
  8105. /*==========================================
  8106. * エリア指定ユーザー数所得
  8107. *------------------------------------------*/
  8108. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  8109. {
  8110. int *users=va_arg(ap,int *);
  8111. (*users)++;
  8112. return 0;
  8113. }
  8114. BUILDIN_FUNC(getareausers)
  8115. {
  8116. const char *str;
  8117. int m,x0,y0,x1,y1,users=0;
  8118. str=script_getstr(st,2);
  8119. x0=script_getnum(st,3);
  8120. y0=script_getnum(st,4);
  8121. x1=script_getnum(st,5);
  8122. y1=script_getnum(st,6);
  8123. if( (m=map_mapname2mapid(str))< 0){
  8124. script_pushint(st,-1);
  8125. return 0;
  8126. }
  8127. map_foreachinarea(buildin_getareausers_sub,
  8128. m,x0,y0,x1,y1,BL_PC,&users);
  8129. script_pushint(st,users);
  8130. return 0;
  8131. }
  8132. /*==========================================
  8133. * エリア指定ドロップアイテム数所得
  8134. *------------------------------------------*/
  8135. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  8136. {
  8137. int item=va_arg(ap,int);
  8138. int *amount=va_arg(ap,int *);
  8139. struct flooritem_data *drop=(struct flooritem_data *)bl;
  8140. if(drop->item_data.nameid==item)
  8141. (*amount)+=drop->item_data.amount;
  8142. return 0;
  8143. }
  8144. BUILDIN_FUNC(getareadropitem)
  8145. {
  8146. const char *str;
  8147. int m,x0,y0,x1,y1,item,amount=0;
  8148. struct script_data *data;
  8149. str=script_getstr(st,2);
  8150. x0=script_getnum(st,3);
  8151. y0=script_getnum(st,4);
  8152. x1=script_getnum(st,5);
  8153. y1=script_getnum(st,6);
  8154. data=script_getdata(st,7);
  8155. get_val(st,data);
  8156. if( data_isstring(data) ){
  8157. const char *name=conv_str(st,data);
  8158. struct item_data *item_data = itemdb_searchname(name);
  8159. item=UNKNOWN_ITEM_ID;
  8160. if( item_data )
  8161. item=item_data->nameid;
  8162. }else
  8163. item=conv_num(st,data);
  8164. if( (m=map_mapname2mapid(str))< 0){
  8165. script_pushint(st,-1);
  8166. return 0;
  8167. }
  8168. map_foreachinarea(buildin_getareadropitem_sub,
  8169. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  8170. script_pushint(st,amount);
  8171. return 0;
  8172. }
  8173. /*==========================================
  8174. * NPCの有効化
  8175. *------------------------------------------*/
  8176. BUILDIN_FUNC(enablenpc)
  8177. {
  8178. const char *str;
  8179. str=script_getstr(st,2);
  8180. npc_enable(str,1);
  8181. return 0;
  8182. }
  8183. /*==========================================
  8184. * NPCの無効化
  8185. *------------------------------------------*/
  8186. BUILDIN_FUNC(disablenpc)
  8187. {
  8188. const char *str;
  8189. str=script_getstr(st,2);
  8190. npc_enable(str,0);
  8191. return 0;
  8192. }
  8193. /*==========================================
  8194. * 隠れているNPCの表示
  8195. *------------------------------------------*/
  8196. BUILDIN_FUNC(hideoffnpc)
  8197. {
  8198. const char *str;
  8199. str=script_getstr(st,2);
  8200. npc_enable(str,2);
  8201. return 0;
  8202. }
  8203. /*==========================================
  8204. * NPCをハイディング
  8205. *------------------------------------------*/
  8206. BUILDIN_FUNC(hideonnpc)
  8207. {
  8208. const char *str;
  8209. str=script_getstr(st,2);
  8210. npc_enable(str,4);
  8211. return 0;
  8212. }
  8213. /// Starts a status effect on the target unit or on the attached player.
  8214. ///
  8215. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  8216. BUILDIN_FUNC(sc_start)
  8217. {
  8218. struct block_list* bl;
  8219. enum sc_type type;
  8220. int tick;
  8221. int val1;
  8222. int val4 = 0;
  8223. type = (sc_type)script_getnum(st,2);
  8224. tick = script_getnum(st,3);
  8225. val1 = script_getnum(st,4);
  8226. if( script_hasdata(st,5) )
  8227. bl = map_id2bl(script_getnum(st,5));
  8228. else
  8229. bl = map_id2bl(st->rid);
  8230. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8231. {// When there isn't a duration specified, try to get it from the skill_db
  8232. tick = skill_get_time(status_sc2skill(type), val1);
  8233. }
  8234. if( potion_flag == 1 && potion_target )
  8235. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8236. bl = map_id2bl(potion_target);
  8237. tick /= 2;// Thrown potions only last half.
  8238. val4 = 1;// Mark that this was a thrown sc_effect
  8239. }
  8240. if( bl )
  8241. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  8242. return 0;
  8243. }
  8244. /// Starts a status effect on the target unit or on the attached player.
  8245. ///
  8246. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  8247. BUILDIN_FUNC(sc_start2)
  8248. {
  8249. struct block_list* bl;
  8250. enum sc_type type;
  8251. int tick;
  8252. int val1;
  8253. int val4 = 0;
  8254. int rate;
  8255. type = (sc_type)script_getnum(st,2);
  8256. tick = script_getnum(st,3);
  8257. val1 = script_getnum(st,4);
  8258. rate = script_getnum(st,5);
  8259. if( script_hasdata(st,6) )
  8260. bl = map_id2bl(script_getnum(st,6));
  8261. else
  8262. bl = map_id2bl(st->rid);
  8263. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8264. {// When there isn't a duration specified, try to get it from the skill_db
  8265. tick = skill_get_time(status_sc2skill(type), val1);
  8266. }
  8267. if( potion_flag == 1 && potion_target )
  8268. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8269. bl = map_id2bl(potion_target);
  8270. tick /= 2;// Thrown potions only last half.
  8271. val4 = 1;// Mark that this was a thrown sc_effect
  8272. }
  8273. if( bl )
  8274. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  8275. return 0;
  8276. }
  8277. /// Starts a status effect on the target unit or on the attached player.
  8278. ///
  8279. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  8280. BUILDIN_FUNC(sc_start4)
  8281. {
  8282. struct block_list* bl;
  8283. enum sc_type type;
  8284. int tick;
  8285. int val1;
  8286. int val2;
  8287. int val3;
  8288. int val4;
  8289. type = (sc_type)script_getnum(st,2);
  8290. tick = script_getnum(st,3);
  8291. val1 = script_getnum(st,4);
  8292. val2 = script_getnum(st,5);
  8293. val3 = script_getnum(st,6);
  8294. val4 = script_getnum(st,7);
  8295. if( script_hasdata(st,8) )
  8296. bl = map_id2bl(script_getnum(st,8));
  8297. else
  8298. bl = map_id2bl(st->rid);
  8299. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8300. {// When there isn't a duration specified, try to get it from the skill_db
  8301. tick = skill_get_time(status_sc2skill(type), val1);
  8302. }
  8303. if( potion_flag == 1 && potion_target )
  8304. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8305. bl = map_id2bl(potion_target);
  8306. tick /= 2;// Thrown potions only last half.
  8307. }
  8308. if( bl )
  8309. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  8310. return 0;
  8311. }
  8312. /// Ends one or all status effects on the target unit or on the attached player.
  8313. ///
  8314. /// sc_end <effect_id>{,<unit_id>};
  8315. BUILDIN_FUNC(sc_end)
  8316. {
  8317. struct block_list* bl;
  8318. int type;
  8319. type = script_getnum(st,2);
  8320. if( script_hasdata(st,3) )
  8321. bl = map_id2bl(script_getnum(st,3));
  8322. else
  8323. bl = map_id2bl(st->rid);
  8324. if( potion_flag==1 && potion_target )
  8325. {//##TODO how does this work [FlavioJS]
  8326. bl = map_id2bl(potion_target);
  8327. }
  8328. if( !bl ) return 0;
  8329. if( type >= 0 && type < SC_MAX )
  8330. {
  8331. struct status_change *sc = status_get_sc(bl);
  8332. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  8333. if (!sce) return 0;
  8334. //This should help status_change_end force disabling the SC in case it has no limit.
  8335. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8336. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8337. } else
  8338. status_change_clear(bl, 2);// remove all effects
  8339. return 0;
  8340. }
  8341. /*==========================================
  8342. * 状態異常耐性を計算した確率を返す
  8343. *------------------------------------------*/
  8344. BUILDIN_FUNC(getscrate)
  8345. {
  8346. struct block_list *bl;
  8347. int type,rate;
  8348. type=script_getnum(st,2);
  8349. rate=script_getnum(st,3);
  8350. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  8351. bl = map_id2bl(script_getnum(st,4));
  8352. else
  8353. bl = map_id2bl(st->rid);
  8354. if (bl)
  8355. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  8356. script_pushint(st,rate);
  8357. return 0;
  8358. }
  8359. /*==========================================
  8360. * getstatus <type>{, <info>};
  8361. *------------------------------------------*/
  8362. BUILDIN_FUNC(getstatus)
  8363. {
  8364. int id, type;
  8365. struct map_session_data* sd = script_rid2sd(st);
  8366. if( sd == NULL )
  8367. {// no player attached
  8368. return 0;
  8369. }
  8370. id = script_getnum(st, 2);
  8371. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8372. if( id <= SC_NONE || id >= SC_MAX )
  8373. {// invalid status type given
  8374. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8375. return 0;
  8376. }
  8377. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8378. {// no status is active
  8379. script_pushint(st, 0);
  8380. return 0;
  8381. }
  8382. switch( type )
  8383. {
  8384. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8385. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8386. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8387. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8388. case 5:
  8389. {
  8390. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8391. if( timer )
  8392. {// return the amount of time remaining
  8393. script_pushint(st, timer->tick - gettick());
  8394. }
  8395. }
  8396. break;
  8397. default: script_pushint(st, 1); break;
  8398. }
  8399. return 0;
  8400. }
  8401. /*==========================================
  8402. *
  8403. *------------------------------------------*/
  8404. BUILDIN_FUNC(debugmes)
  8405. {
  8406. const char *str;
  8407. str=script_getstr(st,2);
  8408. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8409. return 0;
  8410. }
  8411. /*==========================================
  8412. *捕獲アイテム使用
  8413. *------------------------------------------*/
  8414. BUILDIN_FUNC(catchpet)
  8415. {
  8416. int pet_id;
  8417. TBL_PC *sd;
  8418. pet_id= script_getnum(st,2);
  8419. sd=script_rid2sd(st);
  8420. if( sd == NULL )
  8421. return 0;
  8422. pet_catch_process1(sd,pet_id);
  8423. return 0;
  8424. }
  8425. /*==========================================
  8426. * [orn]
  8427. *------------------------------------------*/
  8428. BUILDIN_FUNC(homunculus_evolution)
  8429. {
  8430. TBL_PC *sd;
  8431. sd=script_rid2sd(st);
  8432. if( sd == NULL )
  8433. return 0;
  8434. if(merc_is_hom_active(sd->hd))
  8435. {
  8436. if (sd->hd->homunculus.intimacy > 91000)
  8437. merc_hom_evolution(sd->hd);
  8438. else
  8439. clif_emotion(&sd->hd->bl, E_SWT);
  8440. }
  8441. return 0;
  8442. }
  8443. /*==========================================
  8444. * [Xantara]
  8445. *------------------------------------------*/
  8446. BUILDIN_FUNC(homunculus_mutate)
  8447. {
  8448. int homun_id, m_class, m_id;
  8449. int homun_array[5] = {6048,6049,6050,6051,6052};
  8450. TBL_PC *sd;
  8451. sd = script_rid2sd(st);
  8452. if( sd == NULL )
  8453. return 0;
  8454. if(script_hasdata(st,2))
  8455. homun_id = script_getnum(st,2);
  8456. else
  8457. homun_id = homun_array[rnd() % 5];
  8458. if(merc_is_hom_active(sd->hd)) {
  8459. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8460. m_id = hom_class2mapid(homun_id);
  8461. if ( m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 )
  8462. hom_mutate(sd->hd, homun_id);
  8463. else
  8464. clif_emotion(&sd->hd->bl, E_SWT);
  8465. }
  8466. return 0;
  8467. }
  8468. // [Zephyrus]
  8469. BUILDIN_FUNC(homunculus_shuffle)
  8470. {
  8471. TBL_PC *sd;
  8472. sd=script_rid2sd(st);
  8473. if( sd == NULL )
  8474. return 0;
  8475. if(merc_is_hom_active(sd->hd))
  8476. merc_hom_shuffle(sd->hd);
  8477. return 0;
  8478. }
  8479. //These two functions bring the eA MAPID_* class functionality to scripts.
  8480. BUILDIN_FUNC(eaclass)
  8481. {
  8482. int class_;
  8483. if( script_hasdata(st,2) )
  8484. class_ = script_getnum(st,2);
  8485. else {
  8486. TBL_PC *sd;
  8487. sd=script_rid2sd(st);
  8488. if (!sd) {
  8489. script_pushint(st,-1);
  8490. return 0;
  8491. }
  8492. class_ = sd->status.class_;
  8493. }
  8494. script_pushint(st,pc_jobid2mapid(class_));
  8495. return 0;
  8496. }
  8497. BUILDIN_FUNC(roclass)
  8498. {
  8499. int class_ =script_getnum(st,2);
  8500. int sex;
  8501. if( script_hasdata(st,3) )
  8502. sex = script_getnum(st,3);
  8503. else {
  8504. TBL_PC *sd;
  8505. if (st->rid && (sd=script_rid2sd(st)))
  8506. sex = sd->status.sex;
  8507. else
  8508. sex = 1; //Just use male when not found.
  8509. }
  8510. script_pushint(st,pc_mapid2jobid(class_, sex));
  8511. return 0;
  8512. }
  8513. /*==========================================
  8514. * Tells client to open a hatching window, used for pet incubator
  8515. *------------------------------------------*/
  8516. BUILDIN_FUNC(birthpet)
  8517. {
  8518. TBL_PC *sd;
  8519. sd=script_rid2sd(st);
  8520. if( sd == NULL )
  8521. return 0;
  8522. if( sd->status.pet_id )
  8523. {// do not send egg list, when you already have a pet
  8524. return 0;
  8525. }
  8526. clif_sendegg(sd);
  8527. return 0;
  8528. }
  8529. /*==========================================
  8530. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8531. *------------------------------------------*/
  8532. BUILDIN_FUNC(resetlvl)
  8533. {
  8534. TBL_PC *sd;
  8535. int type=script_getnum(st,2);
  8536. sd=script_rid2sd(st);
  8537. if( sd == NULL )
  8538. return 0;
  8539. pc_resetlvl(sd,type);
  8540. return 0;
  8541. }
  8542. /*==========================================
  8543. * ステータスリセット
  8544. *------------------------------------------*/
  8545. BUILDIN_FUNC(resetstatus)
  8546. {
  8547. TBL_PC *sd;
  8548. sd=script_rid2sd(st);
  8549. pc_resetstate(sd);
  8550. return 0;
  8551. }
  8552. /*==========================================
  8553. * script command resetskill
  8554. *------------------------------------------*/
  8555. BUILDIN_FUNC(resetskill)
  8556. {
  8557. TBL_PC *sd;
  8558. sd=script_rid2sd(st);
  8559. pc_resetskill(sd,1);
  8560. return 0;
  8561. }
  8562. /*==========================================
  8563. * Counts total amount of skill points.
  8564. *------------------------------------------*/
  8565. BUILDIN_FUNC(skillpointcount)
  8566. {
  8567. TBL_PC *sd;
  8568. sd=script_rid2sd(st);
  8569. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8570. return 0;
  8571. }
  8572. /*==========================================
  8573. *
  8574. *------------------------------------------*/
  8575. BUILDIN_FUNC(changebase)
  8576. {
  8577. TBL_PC *sd=NULL;
  8578. int vclass;
  8579. if( script_hasdata(st,3) )
  8580. sd=map_id2sd(script_getnum(st,3));
  8581. else
  8582. sd=script_rid2sd(st);
  8583. if(sd == NULL)
  8584. return 0;
  8585. vclass = script_getnum(st,2);
  8586. if(vclass == JOB_WEDDING)
  8587. {
  8588. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8589. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8590. )
  8591. return 0;
  8592. }
  8593. if(!sd->disguise && vclass != sd->vd.class_) {
  8594. status_set_viewdata(&sd->bl, vclass);
  8595. //Updated client view. Base, Weapon and Cloth Colors.
  8596. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8597. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8598. if (sd->vd.cloth_color)
  8599. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8600. clif_skillinfoblock(sd);
  8601. }
  8602. return 0;
  8603. }
  8604. /*==========================================
  8605. * 性別変換
  8606. *------------------------------------------*/
  8607. BUILDIN_FUNC(changesex)
  8608. {
  8609. int i;
  8610. TBL_PC *sd = NULL;
  8611. sd = script_rid2sd(st);
  8612. pc_resetskill(sd,4);
  8613. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  8614. for( i=0; i<EQI_MAX; i++ )
  8615. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  8616. chrif_changesex(sd);
  8617. return 0;
  8618. }
  8619. /*==========================================
  8620. * Works like 'announce' but outputs in the common chat window
  8621. *------------------------------------------*/
  8622. BUILDIN_FUNC(globalmes)
  8623. {
  8624. struct block_list *bl = map_id2bl(st->oid);
  8625. struct npc_data *nd = (struct npc_data *)bl;
  8626. const char *name=NULL,*mes;
  8627. mes=script_getstr(st,2); // メッセージの取得
  8628. if(mes==NULL) return 0;
  8629. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8630. name=script_getstr(st,3);
  8631. } else {
  8632. name=nd->name;
  8633. }
  8634. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8635. return 0;
  8636. }
  8637. /////////////////////////////////////////////////////////////////////
  8638. // NPC waiting room (chat room)
  8639. //
  8640. /// Creates a waiting room (chat room) for this npc.
  8641. ///
  8642. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  8643. BUILDIN_FUNC(waitingroom)
  8644. {
  8645. struct npc_data* nd;
  8646. int pub = 1;
  8647. const char* title = script_getstr(st, 2);
  8648. int limit = script_getnum(st, 3);
  8649. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  8650. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  8651. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  8652. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  8653. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  8654. nd = (struct npc_data *)map_id2bl(st->oid);
  8655. if( nd != NULL )
  8656. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  8657. return 0;
  8658. }
  8659. /// Removes the waiting room of the current or target npc.
  8660. ///
  8661. /// delwaitingroom "<npc_name>";
  8662. /// delwaitingroom;
  8663. BUILDIN_FUNC(delwaitingroom)
  8664. {
  8665. struct npc_data* nd;
  8666. if( script_hasdata(st,2) )
  8667. nd = npc_name2id(script_getstr(st, 2));
  8668. else
  8669. nd = (struct npc_data *)map_id2bl(st->oid);
  8670. if( nd != NULL )
  8671. chat_deletenpcchat(nd);
  8672. return 0;
  8673. }
  8674. /// Kicks all the players from the waiting room of the current or target npc.
  8675. ///
  8676. /// kickwaitingroomall "<npc_name>";
  8677. /// kickwaitingroomall;
  8678. BUILDIN_FUNC(waitingroomkickall)
  8679. {
  8680. struct npc_data* nd;
  8681. struct chat_data* cd;
  8682. if( script_hasdata(st,2) )
  8683. nd = npc_name2id(script_getstr(st,2));
  8684. else
  8685. nd = (struct npc_data *)map_id2bl(st->oid);
  8686. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8687. chat_npckickall(cd);
  8688. return 0;
  8689. }
  8690. /// Enables the waiting room event of the current or target npc.
  8691. ///
  8692. /// enablewaitingroomevent "<npc_name>";
  8693. /// enablewaitingroomevent;
  8694. BUILDIN_FUNC(enablewaitingroomevent)
  8695. {
  8696. struct npc_data* nd;
  8697. struct chat_data* cd;
  8698. if( script_hasdata(st,2) )
  8699. nd = npc_name2id(script_getstr(st, 2));
  8700. else
  8701. nd = (struct npc_data *)map_id2bl(st->oid);
  8702. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8703. chat_enableevent(cd);
  8704. return 0;
  8705. }
  8706. /// Disables the waiting room event of the current or target npc.
  8707. ///
  8708. /// disablewaitingroomevent "<npc_name>";
  8709. /// disablewaitingroomevent;
  8710. BUILDIN_FUNC(disablewaitingroomevent)
  8711. {
  8712. struct npc_data *nd;
  8713. struct chat_data *cd;
  8714. if( script_hasdata(st,2) )
  8715. nd = npc_name2id(script_getstr(st, 2));
  8716. else
  8717. nd = (struct npc_data *)map_id2bl(st->oid);
  8718. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8719. chat_disableevent(cd);
  8720. return 0;
  8721. }
  8722. /// Returns info on the waiting room of the current or target npc.
  8723. /// Returns -1 if the type unknown
  8724. /// <type>=0 : current number of users
  8725. /// <type>=1 : maximum number of users allowed
  8726. /// <type>=2 : the number of users that trigger the event
  8727. /// <type>=3 : if the trigger is disabled
  8728. /// <type>=4 : the title of the waiting room
  8729. /// <type>=5 : the password of the waiting room
  8730. /// <type>=16 : the name of the waiting room event
  8731. /// <type>=32 : if the waiting room is full
  8732. /// <type>=33 : if there are enough users to trigger the event
  8733. ///
  8734. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8735. /// getwaitingroomstate(<type>) -> <info>
  8736. BUILDIN_FUNC(getwaitingroomstate)
  8737. {
  8738. struct npc_data *nd;
  8739. struct chat_data *cd;
  8740. int type;
  8741. type = script_getnum(st,2);
  8742. if( script_hasdata(st,3) )
  8743. nd = npc_name2id(script_getstr(st, 3));
  8744. else
  8745. nd = (struct npc_data *)map_id2bl(st->oid);
  8746. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8747. {
  8748. script_pushint(st, -1);
  8749. return 0;
  8750. }
  8751. switch(type)
  8752. {
  8753. case 0: script_pushint(st, cd->users); break;
  8754. case 1: script_pushint(st, cd->limit); break;
  8755. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8756. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8757. case 4: script_pushstrcopy(st, cd->title); break;
  8758. case 5: script_pushstrcopy(st, cd->pass); break;
  8759. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8760. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8761. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8762. default: script_pushint(st, -1); break;
  8763. }
  8764. return 0;
  8765. }
  8766. /// Warps the trigger or target amount of players to the target map and position.
  8767. /// Players are automatically removed from the waiting room.
  8768. /// Those waiting the longest will get warped first.
  8769. /// The target map can be "Random" for a random position in the current map,
  8770. /// and "SavePoint" for the savepoint map+position.
  8771. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8772. ///
  8773. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8774. /// The total number of teleported players is put into $@warpwaitingpcnum
  8775. ///
  8776. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8777. /// warpwaitingpc "<map name>",<x>,<y>;
  8778. BUILDIN_FUNC(warpwaitingpc)
  8779. {
  8780. int x;
  8781. int y;
  8782. int i;
  8783. int n;
  8784. const char* map_name;
  8785. struct npc_data* nd;
  8786. struct chat_data* cd;
  8787. TBL_PC* sd;
  8788. nd = (struct npc_data *)map_id2bl(st->oid);
  8789. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8790. return 0;
  8791. map_name = script_getstr(st,2);
  8792. x = script_getnum(st,3);
  8793. y = script_getnum(st,4);
  8794. n = cd->trigger&0x7f;
  8795. if( script_hasdata(st,5) )
  8796. n = script_getnum(st,5);
  8797. for( i = 0; i < n && cd->users > 0; i++ )
  8798. {
  8799. sd = cd->usersd[0];
  8800. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  8801. {// can't teleport on this map
  8802. break;
  8803. }
  8804. if( cd->zeny )
  8805. {// fee set
  8806. if( (uint32)sd->status.zeny < cd->zeny )
  8807. {// no zeny to cover set fee
  8808. break;
  8809. }
  8810. pc_payzeny(sd, cd->zeny);
  8811. }
  8812. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  8813. if( strcmp(map_name,"Random") == 0 )
  8814. pc_randomwarp(sd,CLR_TELEPORT);
  8815. else if( strcmp(map_name,"SavePoint") == 0 )
  8816. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  8817. else
  8818. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  8819. }
  8820. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8821. return 0;
  8822. }
  8823. /////////////////////////////////////////////////////////////////////
  8824. // ...
  8825. //
  8826. /// Detaches a character from a script.
  8827. ///
  8828. /// @param st Script state to detach the character from.
  8829. static void script_detach_rid(struct script_state* st)
  8830. {
  8831. if(st->rid)
  8832. {
  8833. script_detach_state(st, false);
  8834. st->rid = 0;
  8835. }
  8836. }
  8837. /*==========================================
  8838. * RIDのアタッチ
  8839. *------------------------------------------*/
  8840. BUILDIN_FUNC(attachrid)
  8841. {
  8842. int rid = script_getnum(st,2);
  8843. struct map_session_data* sd;
  8844. if ((sd = map_id2sd(rid))!=NULL) {
  8845. script_detach_rid(st);
  8846. st->rid = rid;
  8847. script_attach_state(st);
  8848. script_pushint(st,1);
  8849. } else
  8850. script_pushint(st,0);
  8851. return 0;
  8852. }
  8853. /*==========================================
  8854. * RIDのデタッチ
  8855. *------------------------------------------*/
  8856. BUILDIN_FUNC(detachrid)
  8857. {
  8858. script_detach_rid(st);
  8859. return 0;
  8860. }
  8861. /*==========================================
  8862. * 存在チェック
  8863. *------------------------------------------*/
  8864. BUILDIN_FUNC(isloggedin)
  8865. {
  8866. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8867. if (script_hasdata(st,3) && sd &&
  8868. sd->status.char_id != script_getnum(st,3))
  8869. sd = NULL;
  8870. push_val(st->stack,C_INT,sd!=NULL);
  8871. return 0;
  8872. }
  8873. /*==========================================
  8874. *
  8875. *------------------------------------------*/
  8876. BUILDIN_FUNC(setmapflagnosave)
  8877. {
  8878. int m,x,y;
  8879. unsigned short mapindex;
  8880. const char *str,*str2;
  8881. str=script_getstr(st,2);
  8882. str2=script_getstr(st,3);
  8883. x=script_getnum(st,4);
  8884. y=script_getnum(st,5);
  8885. m = map_mapname2mapid(str);
  8886. mapindex = mapindex_name2id(str2);
  8887. if(m >= 0 && mapindex) {
  8888. map[m].flag.nosave=1;
  8889. map[m].save.map=mapindex;
  8890. map[m].save.x=x;
  8891. map[m].save.y=y;
  8892. }
  8893. return 0;
  8894. }
  8895. BUILDIN_FUNC(getmapflag)
  8896. {
  8897. int m,i;
  8898. const char *str;
  8899. str=script_getstr(st,2);
  8900. i=script_getnum(st,3);
  8901. m = map_mapname2mapid(str);
  8902. if(m >= 0) {
  8903. switch(i) {
  8904. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8905. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8906. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  8907. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8908. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8909. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8910. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8911. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8912. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8913. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8914. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8915. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8916. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8917. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8918. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8919. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8920. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8921. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8922. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8923. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8924. /**
  8925. * No longer available, keeping here just in case it's back someday. [Ind]
  8926. **/
  8927. //case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8928. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8929. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8930. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8931. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8932. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8933. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8934. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8935. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8936. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8937. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8938. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8939. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8940. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8941. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8942. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8943. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8944. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8945. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8946. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8947. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8948. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8949. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8950. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  8951. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8952. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8953. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8954. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8955. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8956. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8957. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8958. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  8959. }
  8960. }
  8961. return 0;
  8962. }
  8963. BUILDIN_FUNC(setmapflag)
  8964. {
  8965. int m,i;
  8966. const char *str;
  8967. const char *val=NULL;
  8968. str=script_getstr(st,2);
  8969. i=script_getnum(st,3);
  8970. if(script_hasdata(st,4)){
  8971. val=script_getstr(st,4);
  8972. }
  8973. m = map_mapname2mapid(str);
  8974. if(m >= 0) {
  8975. switch(i) {
  8976. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  8977. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  8978. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  8979. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  8980. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  8981. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  8982. case MF_PVP: map[m].flag.pvp = 1; break;
  8983. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  8984. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  8985. case MF_GVG: map[m].flag.gvg = 1; break;
  8986. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  8987. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  8988. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  8989. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  8990. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  8991. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  8992. case MF_SNOW: map[m].flag.snow = 1; break;
  8993. case MF_FOG: map[m].flag.fog = 1; break;
  8994. case MF_SAKURA: map[m].flag.sakura = 1; break;
  8995. case MF_LEAVES: map[m].flag.leaves = 1; break;
  8996. /**
  8997. * No longer available, keeping here just in case it's back someday. [Ind]
  8998. **/
  8999. //case MF_RAIN: map[m].flag.rain = 1; break;
  9000. case MF_NOGO: map[m].flag.nogo = 1; break;
  9001. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  9002. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  9003. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  9004. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  9005. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  9006. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  9007. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  9008. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  9009. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  9010. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  9011. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  9012. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  9013. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  9014. case MF_RESTRICTED:
  9015. map[m].zone |= 1<<((int)atoi(val)+1);
  9016. map[m].flag.restricted=1;
  9017. break;
  9018. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  9019. case MF_NODROP: map[m].flag.nodrop = 1; break;
  9020. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  9021. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  9022. case MF_NOVENDING: map[m].flag.novending = 1; break;
  9023. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  9024. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  9025. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  9026. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  9027. case MF_TOWN: map[m].flag.town = 1; break;
  9028. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  9029. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  9030. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  9031. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  9032. case MF_BATTLEGROUND: map[m].flag.battleground = (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  9033. case MF_RESET: map[m].flag.reset = 1; break;
  9034. }
  9035. }
  9036. return 0;
  9037. }
  9038. BUILDIN_FUNC(removemapflag)
  9039. {
  9040. int m,i;
  9041. const char *str;
  9042. const char *val=NULL;
  9043. str=script_getstr(st,2);
  9044. i=script_getnum(st,3);
  9045. if(script_hasdata(st,4)){
  9046. val=script_getstr(st,4);
  9047. }
  9048. m = map_mapname2mapid(str);
  9049. if(m >= 0) {
  9050. switch(i) {
  9051. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  9052. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  9053. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  9054. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  9055. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  9056. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  9057. case MF_PVP: map[m].flag.pvp = 0; break;
  9058. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  9059. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  9060. case MF_GVG: map[m].flag.gvg = 0; break;
  9061. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  9062. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  9063. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  9064. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  9065. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  9066. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  9067. case MF_SNOW: map[m].flag.snow = 0; break;
  9068. case MF_FOG: map[m].flag.fog = 0; break;
  9069. case MF_SAKURA: map[m].flag.sakura = 0; break;
  9070. case MF_LEAVES: map[m].flag.leaves = 0; break;
  9071. /**
  9072. * No longer available, keeping here just in case it's back someday. [Ind]
  9073. **/
  9074. //case MF_RAIN: map[m].flag.rain = 0; break;
  9075. case MF_NOGO: map[m].flag.nogo = 0; break;
  9076. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  9077. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  9078. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  9079. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  9080. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  9081. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  9082. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  9083. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  9084. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  9085. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  9086. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  9087. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  9088. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  9089. case MF_RESTRICTED:
  9090. map[m].zone ^= 1<<((int)atoi(val)+1);
  9091. if (map[m].zone == 0){
  9092. map[m].flag.restricted=0;
  9093. }
  9094. break;
  9095. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  9096. case MF_NODROP: map[m].flag.nodrop = 0; break;
  9097. case MF_JEXP: map[m].jexp = 0; break;
  9098. case MF_BEXP: map[m].bexp = 0; break;
  9099. case MF_NOVENDING: map[m].flag.novending = 0; break;
  9100. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  9101. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  9102. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  9103. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  9104. case MF_TOWN: map[m].flag.town = 0; break;
  9105. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  9106. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  9107. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  9108. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  9109. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  9110. case MF_RESET: map[m].flag.reset = 0; break;
  9111. }
  9112. }
  9113. return 0;
  9114. }
  9115. BUILDIN_FUNC(pvpon)
  9116. {
  9117. int m;
  9118. const char *str;
  9119. TBL_PC* sd = NULL;
  9120. struct s_mapiterator* iter;
  9121. str = script_getstr(st,2);
  9122. m = map_mapname2mapid(str);
  9123. if( m < 0 || map[m].flag.pvp )
  9124. return 0; // nothing to do
  9125. map[m].flag.pvp = 1;
  9126. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  9127. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  9128. return 0;
  9129. iter = mapit_getallusers();
  9130. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9131. {
  9132. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  9133. continue; // not applicable
  9134. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  9135. sd->pvp_rank = 0;
  9136. sd->pvp_lastusers = 0;
  9137. sd->pvp_point = 5;
  9138. sd->pvp_won = 0;
  9139. sd->pvp_lost = 0;
  9140. }
  9141. mapit_free(iter);
  9142. return 0;
  9143. }
  9144. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  9145. {
  9146. TBL_PC* sd = (TBL_PC*)bl;
  9147. clif_pvpset(sd, 0, 0, 2);
  9148. if (sd->pvp_timer != INVALID_TIMER) {
  9149. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  9150. sd->pvp_timer = INVALID_TIMER;
  9151. }
  9152. return 0;
  9153. }
  9154. BUILDIN_FUNC(pvpoff)
  9155. {
  9156. int m;
  9157. const char *str;
  9158. str=script_getstr(st,2);
  9159. m = map_mapname2mapid(str);
  9160. if(m < 0 || !map[m].flag.pvp)
  9161. return 0; //fixed Lupus
  9162. map[m].flag.pvp = 0;
  9163. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9164. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  9165. return 0;
  9166. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  9167. return 0;
  9168. }
  9169. BUILDIN_FUNC(gvgon)
  9170. {
  9171. int m;
  9172. const char *str;
  9173. str=script_getstr(st,2);
  9174. m = map_mapname2mapid(str);
  9175. if(m >= 0 && !map[m].flag.gvg) {
  9176. map[m].flag.gvg = 1;
  9177. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9178. }
  9179. return 0;
  9180. }
  9181. BUILDIN_FUNC(gvgoff)
  9182. {
  9183. int m;
  9184. const char *str;
  9185. str=script_getstr(st,2);
  9186. m = map_mapname2mapid(str);
  9187. if(m >= 0 && map[m].flag.gvg) {
  9188. map[m].flag.gvg = 0;
  9189. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9190. }
  9191. return 0;
  9192. }
  9193. /*==========================================
  9194. * Shows an emoticon on top of the player/npc
  9195. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  9196. *------------------------------------------*/
  9197. //Optional second parameter added by [Skotlex]
  9198. BUILDIN_FUNC(emotion)
  9199. {
  9200. int type;
  9201. int player=0;
  9202. type=script_getnum(st,2);
  9203. if(type < 0 || type > 100)
  9204. return 0;
  9205. if( script_hasdata(st,3) )
  9206. player=script_getnum(st,3);
  9207. if (player) {
  9208. TBL_PC *sd = NULL;
  9209. if( script_hasdata(st,4) )
  9210. sd = map_nick2sd(script_getstr(st,4));
  9211. else
  9212. sd = script_rid2sd(st);
  9213. if (sd)
  9214. clif_emotion(&sd->bl,type);
  9215. } else
  9216. if( script_hasdata(st,4) )
  9217. {
  9218. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9219. if(nd)
  9220. clif_emotion(&nd->bl,type);
  9221. }
  9222. else
  9223. clif_emotion(map_id2bl(st->oid),type);
  9224. return 0;
  9225. }
  9226. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  9227. {
  9228. int m=va_arg(ap,int);
  9229. int g_id=va_arg(ap,int);
  9230. int flag=va_arg(ap,int);
  9231. if(!sd || sd->bl.m != m)
  9232. return 0;
  9233. if(
  9234. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  9235. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  9236. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  9237. )
  9238. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  9239. return 1;
  9240. }
  9241. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  9242. {
  9243. struct mob_data *md=(struct mob_data *)bl;
  9244. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  9245. status_kill(bl);
  9246. return 0;
  9247. }
  9248. BUILDIN_FUNC(maprespawnguildid)
  9249. {
  9250. const char *mapname=script_getstr(st,2);
  9251. int g_id=script_getnum(st,3);
  9252. int flag=script_getnum(st,4);
  9253. int m=map_mapname2mapid(mapname);
  9254. if(m == -1)
  9255. return 0;
  9256. //Catch ALL players (in case some are 'between maps' on execution time)
  9257. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  9258. if (flag&4) //Remove script mobs.
  9259. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  9260. return 0;
  9261. }
  9262. BUILDIN_FUNC(agitstart)
  9263. {
  9264. if(agit_flag==1) return 0; // Agit already Start.
  9265. agit_flag=1;
  9266. guild_agit_start();
  9267. return 0;
  9268. }
  9269. BUILDIN_FUNC(agitend)
  9270. {
  9271. if(agit_flag==0) return 0; // Agit already End.
  9272. agit_flag=0;
  9273. guild_agit_end();
  9274. return 0;
  9275. }
  9276. BUILDIN_FUNC(agitstart2)
  9277. {
  9278. if(agit2_flag==1) return 0; // Agit2 already Start.
  9279. agit2_flag=1;
  9280. guild_agit2_start();
  9281. return 0;
  9282. }
  9283. BUILDIN_FUNC(agitend2)
  9284. {
  9285. if(agit2_flag==0) return 0; // Agit2 already End.
  9286. agit2_flag=0;
  9287. guild_agit2_end();
  9288. return 0;
  9289. }
  9290. /*==========================================
  9291. * Returns whether woe is on or off. // choice script
  9292. *------------------------------------------*/
  9293. BUILDIN_FUNC(agitcheck)
  9294. {
  9295. script_pushint(st,agit_flag);
  9296. return 0;
  9297. }
  9298. /*==========================================
  9299. * Returns whether woese is on or off. // choice script
  9300. *------------------------------------------*/
  9301. BUILDIN_FUNC(agitcheck2)
  9302. {
  9303. script_pushint(st,agit2_flag);
  9304. return 0;
  9305. }
  9306. /// Sets the guild_id of this npc.
  9307. ///
  9308. /// flagemblem <guild_id>;
  9309. BUILDIN_FUNC(flagemblem)
  9310. {
  9311. TBL_NPC* nd;
  9312. int g_id=script_getnum(st,2);
  9313. if(g_id < 0) return 0;
  9314. nd = (TBL_NPC*)map_id2nd(st->oid);
  9315. if( nd == NULL )
  9316. {
  9317. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  9318. }
  9319. else if( nd->subtype != SCRIPT )
  9320. {
  9321. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  9322. }
  9323. else
  9324. {
  9325. nd->u.scr.guild_id = g_id;
  9326. clif_guild_emblem_area(&nd->bl);
  9327. }
  9328. return 0;
  9329. }
  9330. BUILDIN_FUNC(getcastlename)
  9331. {
  9332. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9333. struct guild_castle* gc = guild_mapname2gc(mapname);
  9334. const char* name = (gc) ? gc->castle_name : "";
  9335. script_pushstrcopy(st,name);
  9336. return 0;
  9337. }
  9338. BUILDIN_FUNC(getcastledata)
  9339. {
  9340. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9341. int index = script_getnum(st,3);
  9342. struct guild_castle *gc = guild_mapname2gc(mapname);
  9343. if (gc == NULL) {
  9344. script_pushint(st,0);
  9345. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9346. return 1;
  9347. }
  9348. switch (index) {
  9349. case 1:
  9350. script_pushint(st,gc->guild_id); break;
  9351. case 2:
  9352. script_pushint(st,gc->economy); break;
  9353. case 3:
  9354. script_pushint(st,gc->defense); break;
  9355. case 4:
  9356. script_pushint(st,gc->triggerE); break;
  9357. case 5:
  9358. script_pushint(st,gc->triggerD); break;
  9359. case 6:
  9360. script_pushint(st,gc->nextTime); break;
  9361. case 7:
  9362. script_pushint(st,gc->payTime); break;
  9363. case 8:
  9364. script_pushint(st,gc->createTime); break;
  9365. case 9:
  9366. script_pushint(st,gc->visibleC); break;
  9367. default:
  9368. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  9369. script_pushint(st,gc->guardian[index-10].visible);
  9370. break;
  9371. }
  9372. script_pushint(st,0);
  9373. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9374. return 1;
  9375. }
  9376. return 0;
  9377. }
  9378. BUILDIN_FUNC(setcastledata)
  9379. {
  9380. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9381. int index = script_getnum(st,3);
  9382. int value = script_getnum(st,4);
  9383. struct guild_castle *gc = guild_mapname2gc(mapname);
  9384. if (gc == NULL) {
  9385. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9386. return 1;
  9387. }
  9388. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  9389. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9390. return 1;
  9391. }
  9392. guild_castledatasave(gc->castle_id, index, value);
  9393. return 0;
  9394. }
  9395. /* =====================================================================
  9396. * ギルド情報を要求する
  9397. * ---------------------------------------------------------------------*/
  9398. BUILDIN_FUNC(requestguildinfo)
  9399. {
  9400. int guild_id=script_getnum(st,2);
  9401. const char *event=NULL;
  9402. if( script_hasdata(st,3) ){
  9403. event=script_getstr(st,3);
  9404. check_event(st, event);
  9405. }
  9406. if(guild_id>0)
  9407. guild_npc_request_info(guild_id,event);
  9408. return 0;
  9409. }
  9410. /// Returns the number of cards that have been compounded onto the specified equipped item.
  9411. /// getequipcardcnt(<equipment slot>);
  9412. BUILDIN_FUNC(getequipcardcnt)
  9413. {
  9414. int i=-1,j,num;
  9415. TBL_PC *sd;
  9416. int count;
  9417. num=script_getnum(st,2);
  9418. sd=script_rid2sd(st);
  9419. if (num > 0 && num <= ARRAYLENGTH(equip))
  9420. i=pc_checkequip(sd,equip[num-1]);
  9421. if (i < 0 || !sd->inventory_data[i]) {
  9422. script_pushint(st,0);
  9423. return 0;
  9424. }
  9425. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9426. {
  9427. script_pushint(st,0);
  9428. return 0;
  9429. }
  9430. count = 0;
  9431. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  9432. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  9433. count++;
  9434. script_pushint(st,count);
  9435. return 0;
  9436. }
  9437. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  9438. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  9439. /// successremovecards <slot>;
  9440. BUILDIN_FUNC(successremovecards)
  9441. {
  9442. int i=-1,j,c,cardflag=0;
  9443. TBL_PC* sd = script_rid2sd(st);
  9444. int num = script_getnum(st,2);
  9445. if (num > 0 && num <= ARRAYLENGTH(equip))
  9446. i=pc_checkequip(sd,equip[num-1]);
  9447. if (i < 0 || !sd->inventory_data[i]) {
  9448. return 0;
  9449. }
  9450. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9451. return 0;
  9452. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9453. {
  9454. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9455. {// extract this card from the item
  9456. int flag;
  9457. struct item item_tmp;
  9458. cardflag = 1;
  9459. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9460. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9461. item_tmp.attribute=0,item_tmp.expire_time=0;
  9462. for (j = 0; j < MAX_SLOTS; j++)
  9463. item_tmp.card[j]=0;
  9464. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // 持てないならドロップ
  9465. clif_additem(sd,0,0,flag);
  9466. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9467. }
  9468. }
  9469. }
  9470. if(cardflag == 1)
  9471. { // カードを取り除いたアイテム所得
  9472. int flag;
  9473. struct item item_tmp;
  9474. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9475. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9476. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9477. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9478. item_tmp.card[j]=0;
  9479. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9480. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9481. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  9482. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // もてないならドロップ
  9483. clif_additem(sd,0,0,flag);
  9484. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9485. }
  9486. clif_misceffect(&sd->bl,3);
  9487. }
  9488. return 0;
  9489. }
  9490. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  9491. /// failedremovecards <slot>, <type>;
  9492. /// <type>=0 : will destroy both the item and the cards.
  9493. /// <type>=1 : will keep the item, but destroy the cards.
  9494. /// <type>=2 : will keep the cards, but destroy the item.
  9495. /// <type>=? : will just display the failure effect.
  9496. BUILDIN_FUNC(failedremovecards)
  9497. {
  9498. int i=-1,j,c,cardflag=0;
  9499. TBL_PC* sd = script_rid2sd(st);
  9500. int num = script_getnum(st,2);
  9501. int typefail = script_getnum(st,3);
  9502. if (num > 0 && num <= ARRAYLENGTH(equip))
  9503. i=pc_checkequip(sd,equip[num-1]);
  9504. if (i < 0 || !sd->inventory_data[i])
  9505. return 0;
  9506. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9507. return 0;
  9508. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9509. {
  9510. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9511. {
  9512. cardflag = 1;
  9513. if(typefail == 2)
  9514. {// add cards to inventory, clear
  9515. int flag;
  9516. struct item item_tmp;
  9517. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9518. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9519. item_tmp.attribute=0,item_tmp.expire_time=0;
  9520. for (j = 0; j < MAX_SLOTS; j++)
  9521. item_tmp.card[j]=0;
  9522. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  9523. clif_additem(sd,0,0,flag);
  9524. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9525. }
  9526. }
  9527. }
  9528. }
  9529. if(cardflag == 1)
  9530. {
  9531. if(typefail == 0 || typefail == 2){ // 武具損失
  9532. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  9533. }
  9534. if(typefail == 1){ // カードのみ損失(武具を返す)
  9535. int flag;
  9536. struct item item_tmp;
  9537. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9538. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9539. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9540. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9541. item_tmp.card[j]=0;
  9542. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9543. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9544. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  9545. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  9546. clif_additem(sd,0,0,flag);
  9547. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9548. }
  9549. }
  9550. clif_misceffect(&sd->bl,2);
  9551. }
  9552. return 0;
  9553. }
  9554. /* ================================================================
  9555. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  9556. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  9557. * improved by [Lance]
  9558. * ================================================================*/
  9559. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  9560. {
  9561. int x,y,m,check_val=0,check_ID=0,i=0;
  9562. struct guild *g = NULL;
  9563. struct party_data *p = NULL;
  9564. const char *str;
  9565. const char *mapname;
  9566. unsigned int index;
  9567. mapname=script_getstr(st,2);
  9568. str=script_getstr(st,3);
  9569. x=script_getnum(st,4);
  9570. y=script_getnum(st,5);
  9571. if(script_hasdata(st,7)){
  9572. check_val=script_getnum(st,6);
  9573. check_ID=script_getnum(st,7);
  9574. }
  9575. if((m=map_mapname2mapid(mapname))< 0)
  9576. return 0;
  9577. if(!(index=mapindex_name2id(str)))
  9578. return 0;
  9579. switch(check_val){
  9580. case 1:
  9581. g = guild_search(check_ID);
  9582. if (g){
  9583. for( i=0; i < g->max_member; i++)
  9584. {
  9585. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9586. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  9587. }
  9588. }
  9589. }
  9590. break;
  9591. case 2:
  9592. p = party_search(check_ID);
  9593. if(p){
  9594. for(i=0;i<MAX_PARTY; i++){
  9595. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9596. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  9597. }
  9598. }
  9599. }
  9600. break;
  9601. default:
  9602. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  9603. break;
  9604. }
  9605. return 0;
  9606. }
  9607. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9608. {
  9609. char *event=va_arg(ap,char *);
  9610. struct mob_data *md = ((struct mob_data *)bl);
  9611. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  9612. return 1;
  9613. return 0;
  9614. }
  9615. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9616. {
  9617. const char *mapname,*event;
  9618. int m;
  9619. mapname=script_getstr(st,2);
  9620. event=script_getstr(st,3);
  9621. if( strcmp(event, "all") == 0 )
  9622. event = NULL;
  9623. else
  9624. check_event(st, event);
  9625. if( strcmp(mapname, "this") == 0 ) {
  9626. struct map_session_data *sd = script_rid2sd(st);
  9627. if( sd )
  9628. m = sd->bl.m;
  9629. else {
  9630. script_pushint(st,-1);
  9631. return 0;
  9632. }
  9633. }
  9634. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9635. script_pushint(st,-1);
  9636. return 0;
  9637. }
  9638. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9639. {
  9640. script_pushint(st,-1);
  9641. return 0;
  9642. }
  9643. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9644. return 0;
  9645. }
  9646. BUILDIN_FUNC(marriage)
  9647. {
  9648. const char *partner=script_getstr(st,2);
  9649. TBL_PC *sd=script_rid2sd(st);
  9650. TBL_PC *p_sd=map_nick2sd(partner);
  9651. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9652. script_pushint(st,0);
  9653. return 0;
  9654. }
  9655. script_pushint(st,1);
  9656. return 0;
  9657. }
  9658. BUILDIN_FUNC(wedding_effect)
  9659. {
  9660. TBL_PC *sd=script_rid2sd(st);
  9661. struct block_list *bl;
  9662. if(sd==NULL) {
  9663. bl=map_id2bl(st->oid);
  9664. } else
  9665. bl=&sd->bl;
  9666. clif_wedding_effect(bl);
  9667. return 0;
  9668. }
  9669. BUILDIN_FUNC(divorce)
  9670. {
  9671. TBL_PC *sd=script_rid2sd(st);
  9672. if(sd==NULL || pc_divorce(sd) < 0){
  9673. script_pushint(st,0);
  9674. return 0;
  9675. }
  9676. script_pushint(st,1);
  9677. return 0;
  9678. }
  9679. BUILDIN_FUNC(ispartneron)
  9680. {
  9681. TBL_PC *sd=script_rid2sd(st);
  9682. if(sd==NULL || !pc_ismarried(sd) ||
  9683. map_charid2sd(sd->status.partner_id) == NULL) {
  9684. script_pushint(st,0);
  9685. return 0;
  9686. }
  9687. script_pushint(st,1);
  9688. return 0;
  9689. }
  9690. BUILDIN_FUNC(getpartnerid)
  9691. {
  9692. TBL_PC *sd=script_rid2sd(st);
  9693. if (sd == NULL) {
  9694. script_pushint(st,0);
  9695. return 0;
  9696. }
  9697. script_pushint(st,sd->status.partner_id);
  9698. return 0;
  9699. }
  9700. BUILDIN_FUNC(getchildid)
  9701. {
  9702. TBL_PC *sd=script_rid2sd(st);
  9703. if (sd == NULL) {
  9704. script_pushint(st,0);
  9705. return 0;
  9706. }
  9707. script_pushint(st,sd->status.child);
  9708. return 0;
  9709. }
  9710. BUILDIN_FUNC(getmotherid)
  9711. {
  9712. TBL_PC *sd=script_rid2sd(st);
  9713. if (sd == NULL) {
  9714. script_pushint(st,0);
  9715. return 0;
  9716. }
  9717. script_pushint(st,sd->status.mother);
  9718. return 0;
  9719. }
  9720. BUILDIN_FUNC(getfatherid)
  9721. {
  9722. TBL_PC *sd=script_rid2sd(st);
  9723. if (sd == NULL) {
  9724. script_pushint(st,0);
  9725. return 0;
  9726. }
  9727. script_pushint(st,sd->status.father);
  9728. return 0;
  9729. }
  9730. BUILDIN_FUNC(warppartner)
  9731. {
  9732. int x,y;
  9733. unsigned short mapindex;
  9734. const char *str;
  9735. TBL_PC *sd=script_rid2sd(st);
  9736. TBL_PC *p_sd=NULL;
  9737. if(sd==NULL || !pc_ismarried(sd) ||
  9738. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9739. script_pushint(st,0);
  9740. return 0;
  9741. }
  9742. str=script_getstr(st,2);
  9743. x=script_getnum(st,3);
  9744. y=script_getnum(st,4);
  9745. mapindex = mapindex_name2id(str);
  9746. if (mapindex) {
  9747. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  9748. script_pushint(st,1);
  9749. } else
  9750. script_pushint(st,0);
  9751. return 0;
  9752. }
  9753. /*================================================
  9754. * Script for Displaying MOB Information [Valaris]
  9755. *------------------------------------------------*/
  9756. BUILDIN_FUNC(strmobinfo)
  9757. {
  9758. int num=script_getnum(st,2);
  9759. int class_=script_getnum(st,3);
  9760. if(!mobdb_checkid(class_))
  9761. {
  9762. script_pushint(st,0);
  9763. return 0;
  9764. }
  9765. switch (num) {
  9766. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9767. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9768. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9769. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9770. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9771. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9772. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9773. default:
  9774. script_pushint(st,0);
  9775. break;
  9776. }
  9777. return 0;
  9778. }
  9779. /*==========================================
  9780. * Summon guardians [Valaris]
  9781. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9782. *------------------------------------------*/
  9783. BUILDIN_FUNC(guardian)
  9784. {
  9785. int class_=0,x=0,y=0,guardian=0;
  9786. const char *str,*map,*evt="";
  9787. struct script_data *data;
  9788. bool has_index = false;
  9789. map =script_getstr(st,2);
  9790. x =script_getnum(st,3);
  9791. y =script_getnum(st,4);
  9792. str =script_getstr(st,5);
  9793. class_=script_getnum(st,6);
  9794. if( script_hasdata(st,8) )
  9795. {// "<event label>",<guardian index>
  9796. evt=script_getstr(st,7);
  9797. guardian=script_getnum(st,8);
  9798. has_index = true;
  9799. } else if( script_hasdata(st,7) ){
  9800. data=script_getdata(st,7);
  9801. get_val(st,data);
  9802. if( data_isstring(data) )
  9803. {// "<event label>"
  9804. evt=script_getstr(st,7);
  9805. } else if( data_isint(data) )
  9806. {// <guardian index>
  9807. guardian=script_getnum(st,7);
  9808. has_index = true;
  9809. } else {
  9810. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9811. script_reportdata(data);
  9812. return 1;
  9813. }
  9814. }
  9815. check_event(st, evt);
  9816. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9817. return 0;
  9818. }
  9819. /*==========================================
  9820. * Invisible Walls [Zephyrus]
  9821. *------------------------------------------*/
  9822. BUILDIN_FUNC(setwall)
  9823. {
  9824. const char *map, *name;
  9825. int x, y, m, size, dir;
  9826. bool shootable;
  9827. map = script_getstr(st,2);
  9828. x = script_getnum(st,3);
  9829. y = script_getnum(st,4);
  9830. size = script_getnum(st,5);
  9831. dir = script_getnum(st,6);
  9832. shootable = script_getnum(st,7);
  9833. name = script_getstr(st,8);
  9834. if( (m = map_mapname2mapid(map)) < 0 )
  9835. return 0; // Invalid Map
  9836. map_iwall_set(m, x, y, size, dir, shootable, name);
  9837. return 0;
  9838. }
  9839. BUILDIN_FUNC(delwall)
  9840. {
  9841. const char *name = script_getstr(st,2);
  9842. map_iwall_remove(name);
  9843. return 0;
  9844. }
  9845. /// Retrieves various information about the specified guardian.
  9846. ///
  9847. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9848. /// type: 0 - whether it is deployed or not
  9849. /// 1 - maximum hp
  9850. /// 2 - current hp
  9851. ///
  9852. BUILDIN_FUNC(guardianinfo)
  9853. {
  9854. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9855. int id = script_getnum(st,3);
  9856. int type = script_getnum(st,4);
  9857. struct guild_castle* gc = guild_mapname2gc(mapname);
  9858. struct mob_data* gd;
  9859. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9860. {
  9861. script_pushint(st,-1);
  9862. return 0;
  9863. }
  9864. if( type == 0 )
  9865. script_pushint(st, gc->guardian[id].visible);
  9866. else
  9867. if( !gc->guardian[id].visible )
  9868. script_pushint(st,-1);
  9869. else
  9870. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9871. script_pushint(st,-1);
  9872. else
  9873. {
  9874. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9875. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9876. else
  9877. script_pushint(st,-1);
  9878. }
  9879. return 0;
  9880. }
  9881. /*==========================================
  9882. * IDからItem名
  9883. *------------------------------------------*/
  9884. BUILDIN_FUNC(getitemname)
  9885. {
  9886. int item_id=0;
  9887. struct item_data *i_data;
  9888. char *item_name;
  9889. struct script_data *data;
  9890. data=script_getdata(st,2);
  9891. get_val(st,data);
  9892. if( data_isstring(data) ){
  9893. const char *name=conv_str(st,data);
  9894. struct item_data *item_data = itemdb_searchname(name);
  9895. if( item_data )
  9896. item_id=item_data->nameid;
  9897. }else
  9898. item_id=conv_num(st,data);
  9899. i_data = itemdb_exists(item_id);
  9900. if (i_data == NULL)
  9901. {
  9902. script_pushconststr(st,"null");
  9903. return 0;
  9904. }
  9905. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  9906. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9907. script_pushstr(st,item_name);
  9908. return 0;
  9909. }
  9910. /*==========================================
  9911. * Returns number of slots an item has. [Skotlex]
  9912. *------------------------------------------*/
  9913. BUILDIN_FUNC(getitemslots)
  9914. {
  9915. int item_id;
  9916. struct item_data *i_data;
  9917. item_id=script_getnum(st,2);
  9918. i_data = itemdb_exists(item_id);
  9919. if (i_data)
  9920. script_pushint(st,i_data->slot);
  9921. else
  9922. script_pushint(st,-1);
  9923. return 0;
  9924. }
  9925. // TODO: add matk here if needed/once we get rid of REMODE
  9926. /*==========================================
  9927. * Returns some values of an item [Lupus]
  9928. * Price, Weight, etc...
  9929. getiteminfo(itemID,n), where n
  9930. 0 value_buy;
  9931. 1 value_sell;
  9932. 2 type;
  9933. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9934. if = 0, then monsters don't drop it at all (rare or a quest item)
  9935. if = -1, then this item is sold in NPC shops only
  9936. 4 sex;
  9937. 5 equip;
  9938. 6 weight;
  9939. 7 atk;
  9940. 8 def;
  9941. 9 range;
  9942. 10 slot;
  9943. 11 look;
  9944. 12 elv;
  9945. 13 wlv;
  9946. 14 view id
  9947. *------------------------------------------*/
  9948. BUILDIN_FUNC(getiteminfo)
  9949. {
  9950. int item_id,n;
  9951. int *item_arr;
  9952. struct item_data *i_data;
  9953. item_id = script_getnum(st,2);
  9954. n = script_getnum(st,3);
  9955. i_data = itemdb_exists(item_id);
  9956. if (i_data && n>=0 && n<=14) {
  9957. item_arr = (int*)&i_data->value_buy;
  9958. script_pushint(st,item_arr[n]);
  9959. } else
  9960. script_pushint(st,-1);
  9961. return 0;
  9962. }
  9963. /*==========================================
  9964. * Set some values of an item [Lupus]
  9965. * Price, Weight, etc...
  9966. setiteminfo(itemID,n,Value), where n
  9967. 0 value_buy;
  9968. 1 value_sell;
  9969. 2 type;
  9970. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9971. if = 0, then monsters don't drop it at all (rare or a quest item)
  9972. if = -1, then this item is sold in NPC shops only
  9973. 4 sex;
  9974. 5 equip;
  9975. 6 weight;
  9976. 7 atk;
  9977. 8 def;
  9978. 9 range;
  9979. 10 slot;
  9980. 11 look;
  9981. 12 elv;
  9982. 13 wlv;
  9983. 14 view id
  9984. * Returns Value or -1 if the wrong field's been set
  9985. *------------------------------------------*/
  9986. BUILDIN_FUNC(setiteminfo)
  9987. {
  9988. int item_id,n,value;
  9989. int *item_arr;
  9990. struct item_data *i_data;
  9991. item_id = script_getnum(st,2);
  9992. n = script_getnum(st,3);
  9993. value = script_getnum(st,4);
  9994. i_data = itemdb_exists(item_id);
  9995. if (i_data && n>=0 && n<=14) {
  9996. item_arr = (int*)&i_data->value_buy;
  9997. item_arr[n] = value;
  9998. script_pushint(st,value);
  9999. } else
  10000. script_pushint(st,-1);
  10001. return 0;
  10002. }
  10003. /*==========================================
  10004. * Returns value from equipped item slot n [Lupus]
  10005. getequipcardid(num,slot)
  10006. where
  10007. num = eqip position slot
  10008. slot = 0,1,2,3 (Card Slot N)
  10009. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  10010. it's useful when you want to check item cards or if it's signed
  10011. Useful for such quests as "Sign this refined item with players name" etc
  10012. Hat[0] +4 -> Player's Hat[0] +4
  10013. *------------------------------------------*/
  10014. BUILDIN_FUNC(getequipcardid)
  10015. {
  10016. int i=-1,num,slot;
  10017. TBL_PC *sd;
  10018. num=script_getnum(st,2);
  10019. slot=script_getnum(st,3);
  10020. sd=script_rid2sd(st);
  10021. if (num > 0 && num <= ARRAYLENGTH(equip))
  10022. i=pc_checkequip(sd,equip[num-1]);
  10023. if(i >= 0 && slot>=0 && slot<4)
  10024. script_pushint(st,sd->status.inventory[i].card[slot]);
  10025. else
  10026. script_pushint(st,0);
  10027. return 0;
  10028. }
  10029. /*==========================================
  10030. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  10031. *------------------------------------------*/
  10032. BUILDIN_FUNC(petskillbonus)
  10033. {
  10034. struct pet_data *pd;
  10035. TBL_PC *sd=script_rid2sd(st);
  10036. if(sd==NULL || sd->pd==NULL)
  10037. return 0;
  10038. pd=sd->pd;
  10039. if (pd->bonus)
  10040. { //Clear previous bonus
  10041. if (pd->bonus->timer != INVALID_TIMER)
  10042. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  10043. } else //init
  10044. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  10045. pd->bonus->type=script_getnum(st,2);
  10046. pd->bonus->val=script_getnum(st,3);
  10047. pd->bonus->duration=script_getnum(st,4);
  10048. pd->bonus->delay=script_getnum(st,5);
  10049. if (pd->state.skillbonus == 1)
  10050. pd->state.skillbonus=0; // waiting state
  10051. // wait for timer to start
  10052. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10053. pd->bonus->timer = INVALID_TIMER;
  10054. else
  10055. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  10056. return 0;
  10057. }
  10058. /*==========================================
  10059. * pet looting [Valaris] //Rewritten by [Skotlex]
  10060. *------------------------------------------*/
  10061. BUILDIN_FUNC(petloot)
  10062. {
  10063. int max;
  10064. struct pet_data *pd;
  10065. TBL_PC *sd=script_rid2sd(st);
  10066. if(sd==NULL || sd->pd==NULL)
  10067. return 0;
  10068. max=script_getnum(st,2);
  10069. if(max < 1)
  10070. max = 1; //Let'em loot at least 1 item.
  10071. else if (max > MAX_PETLOOT_SIZE)
  10072. max = MAX_PETLOOT_SIZE;
  10073. pd = sd->pd;
  10074. if (pd->loot != NULL)
  10075. { //Release whatever was there already and reallocate memory
  10076. pet_lootitem_drop(pd, pd->msd);
  10077. aFree(pd->loot->item);
  10078. }
  10079. else
  10080. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  10081. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  10082. pd->loot->max=max;
  10083. pd->loot->count = 0;
  10084. pd->loot->weight = 0;
  10085. return 0;
  10086. }
  10087. /*==========================================
  10088. * PCの所持品情報読み取り
  10089. *------------------------------------------*/
  10090. BUILDIN_FUNC(getinventorylist)
  10091. {
  10092. TBL_PC *sd=script_rid2sd(st);
  10093. char card_var[NAME_LENGTH];
  10094. int i,j=0,k;
  10095. if(!sd) return 0;
  10096. for(i=0;i<MAX_INVENTORY;i++){
  10097. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  10098. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  10099. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  10100. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  10101. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  10102. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  10103. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  10104. for (k = 0; k < MAX_SLOTS; k++)
  10105. {
  10106. sprintf(card_var, "@inventorylist_card%d",k+1);
  10107. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  10108. }
  10109. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  10110. j++;
  10111. }
  10112. }
  10113. pc_setreg(sd,add_str("@inventorylist_count"),j);
  10114. return 0;
  10115. }
  10116. BUILDIN_FUNC(getskilllist)
  10117. {
  10118. TBL_PC *sd=script_rid2sd(st);
  10119. int i,j=0;
  10120. if(!sd) return 0;
  10121. for(i=0;i<MAX_SKILL;i++){
  10122. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  10123. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  10124. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  10125. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  10126. j++;
  10127. }
  10128. }
  10129. pc_setreg(sd,add_str("@skilllist_count"),j);
  10130. return 0;
  10131. }
  10132. BUILDIN_FUNC(clearitem)
  10133. {
  10134. TBL_PC *sd=script_rid2sd(st);
  10135. int i;
  10136. if(sd==NULL) return 0;
  10137. for (i=0; i<MAX_INVENTORY; i++) {
  10138. if (sd->status.inventory[i].amount) {
  10139. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  10140. }
  10141. }
  10142. return 0;
  10143. }
  10144. /*==========================================
  10145. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  10146. *------------------------------------------*/
  10147. BUILDIN_FUNC(disguise)
  10148. {
  10149. int id;
  10150. TBL_PC* sd = script_rid2sd(st);
  10151. if (sd == NULL) return 0;
  10152. id = script_getnum(st,2);
  10153. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  10154. pc_disguise(sd, id);
  10155. script_pushint(st,id);
  10156. } else
  10157. script_pushint(st,0);
  10158. return 0;
  10159. }
  10160. /*==========================================
  10161. * Undisguise Player (returns 1 if success, 0 on fail)
  10162. *------------------------------------------*/
  10163. BUILDIN_FUNC(undisguise)
  10164. {
  10165. TBL_PC* sd = script_rid2sd(st);
  10166. if (sd == NULL) return 0;
  10167. if (sd->disguise) {
  10168. pc_disguise(sd, 0);
  10169. script_pushint(st,0);
  10170. } else {
  10171. script_pushint(st,1);
  10172. }
  10173. return 0;
  10174. }
  10175. /*==========================================
  10176. * NPCクラスチェンジ
  10177. * classは変わりたいclass
  10178. * typeは通常0なのかな?
  10179. *------------------------------------------*/
  10180. BUILDIN_FUNC(classchange)
  10181. {
  10182. int _class,type;
  10183. struct block_list *bl=map_id2bl(st->oid);
  10184. if(bl==NULL) return 0;
  10185. _class=script_getnum(st,2);
  10186. type=script_getnum(st,3);
  10187. clif_class_change(bl,_class,type);
  10188. return 0;
  10189. }
  10190. /*==========================================
  10191. * NPCから発生するエフェクト
  10192. *------------------------------------------*/
  10193. BUILDIN_FUNC(misceffect)
  10194. {
  10195. int type;
  10196. type=script_getnum(st,2);
  10197. if(st->oid && st->oid != fake_nd->bl.id) {
  10198. struct block_list *bl = map_id2bl(st->oid);
  10199. if (bl)
  10200. clif_specialeffect(bl,type,AREA);
  10201. } else{
  10202. TBL_PC *sd=script_rid2sd(st);
  10203. if(sd)
  10204. clif_specialeffect(&sd->bl,type,AREA);
  10205. }
  10206. return 0;
  10207. }
  10208. /*==========================================
  10209. * Play a BGM on a single client [Rikter/Yommy]
  10210. *------------------------------------------*/
  10211. BUILDIN_FUNC(playBGM)
  10212. {
  10213. const char* name;
  10214. struct map_session_data* sd;
  10215. if( ( sd = script_rid2sd(st) ) != NULL )
  10216. {
  10217. name = script_getstr(st,2);
  10218. clif_playBGM(sd, name);
  10219. }
  10220. return 0;
  10221. }
  10222. static int playBGM_sub(struct block_list* bl,va_list ap)
  10223. {
  10224. const char* name = va_arg(ap,const char*);
  10225. clif_playBGM(BL_CAST(BL_PC, bl), name);
  10226. return 0;
  10227. }
  10228. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  10229. {
  10230. const char* name = va_arg(args, const char*);
  10231. clif_playBGM(sd, name);
  10232. return 0;
  10233. }
  10234. /*==========================================
  10235. * Play a BGM on multiple client [Rikter/Yommy]
  10236. *------------------------------------------*/
  10237. BUILDIN_FUNC(playBGMall)
  10238. {
  10239. const char* name;
  10240. name = script_getstr(st,2);
  10241. if( script_hasdata(st,7) )
  10242. {// specified part of map
  10243. const char* map = script_getstr(st,3);
  10244. int x0 = script_getnum(st,4);
  10245. int y0 = script_getnum(st,5);
  10246. int x1 = script_getnum(st,6);
  10247. int y1 = script_getnum(st,7);
  10248. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  10249. }
  10250. else if( script_hasdata(st,3) )
  10251. {// entire map
  10252. const char* map = script_getstr(st,3);
  10253. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  10254. }
  10255. else
  10256. {// entire server
  10257. map_foreachpc(&playBGM_foreachpc_sub, name);
  10258. }
  10259. return 0;
  10260. }
  10261. /*==========================================
  10262. * サウンドエフェクト
  10263. *------------------------------------------*/
  10264. BUILDIN_FUNC(soundeffect)
  10265. {
  10266. TBL_PC* sd = script_rid2sd(st);
  10267. const char* name = script_getstr(st,2);
  10268. int type = script_getnum(st,3);
  10269. if(sd)
  10270. {
  10271. clif_soundeffect(sd,&sd->bl,name,type);
  10272. }
  10273. return 0;
  10274. }
  10275. int soundeffect_sub(struct block_list* bl,va_list ap)
  10276. {
  10277. char* name = va_arg(ap,char*);
  10278. int type = va_arg(ap,int);
  10279. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  10280. return 0;
  10281. }
  10282. /*==========================================
  10283. * Play a sound effect (.wav) on multiple clients
  10284. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  10285. *------------------------------------------*/
  10286. BUILDIN_FUNC(soundeffectall)
  10287. {
  10288. struct block_list* bl;
  10289. const char* name;
  10290. int type;
  10291. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  10292. if (!bl)
  10293. return 0;
  10294. name = script_getstr(st,2);
  10295. type = script_getnum(st,3);
  10296. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  10297. if(!script_hasdata(st,4))
  10298. { // area around
  10299. clif_soundeffectall(bl, name, type, AREA);
  10300. }
  10301. else
  10302. if(!script_hasdata(st,5))
  10303. { // entire map
  10304. const char* map = script_getstr(st,4);
  10305. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  10306. }
  10307. else
  10308. if(script_hasdata(st,8))
  10309. { // specified part of map
  10310. const char* map = script_getstr(st,4);
  10311. int x0 = script_getnum(st,5);
  10312. int y0 = script_getnum(st,6);
  10313. int x1 = script_getnum(st,7);
  10314. int y1 = script_getnum(st,8);
  10315. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  10316. }
  10317. else
  10318. {
  10319. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  10320. }
  10321. return 0;
  10322. }
  10323. /*==========================================
  10324. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  10325. *------------------------------------------*/
  10326. BUILDIN_FUNC(petrecovery)
  10327. {
  10328. struct pet_data *pd;
  10329. TBL_PC *sd=script_rid2sd(st);
  10330. if(sd==NULL || sd->pd==NULL)
  10331. return 0;
  10332. pd=sd->pd;
  10333. if (pd->recovery)
  10334. { //Halt previous bonus
  10335. if (pd->recovery->timer != INVALID_TIMER)
  10336. delete_timer(pd->recovery->timer, pet_recovery_timer);
  10337. } else //Init
  10338. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  10339. pd->recovery->type = (sc_type)script_getnum(st,2);
  10340. pd->recovery->delay = script_getnum(st,3);
  10341. pd->recovery->timer = INVALID_TIMER;
  10342. return 0;
  10343. }
  10344. /*==========================================
  10345. * pet healing [Valaris] //Rewritten by [Skotlex]
  10346. *------------------------------------------*/
  10347. BUILDIN_FUNC(petheal)
  10348. {
  10349. struct pet_data *pd;
  10350. TBL_PC *sd=script_rid2sd(st);
  10351. if(sd==NULL || sd->pd==NULL)
  10352. return 0;
  10353. pd=sd->pd;
  10354. if (pd->s_skill)
  10355. { //Clear previous skill
  10356. if (pd->s_skill->timer != INVALID_TIMER)
  10357. {
  10358. if (pd->s_skill->id)
  10359. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10360. else
  10361. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10362. }
  10363. } else //init memory
  10364. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10365. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  10366. //Use the lv as the amount to heal
  10367. pd->s_skill->lv=script_getnum(st,2);
  10368. pd->s_skill->delay=script_getnum(st,3);
  10369. pd->s_skill->hp=script_getnum(st,4);
  10370. pd->s_skill->sp=script_getnum(st,5);
  10371. //Use delay as initial offset to avoid skill/heal exploits
  10372. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10373. pd->s_skill->timer = INVALID_TIMER;
  10374. else
  10375. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  10376. return 0;
  10377. }
  10378. /*==========================================
  10379. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  10380. *------------------------------------------*/
  10381. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  10382. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  10383. BUILDIN_FUNC(petskillattack)
  10384. {
  10385. struct pet_data *pd;
  10386. TBL_PC *sd=script_rid2sd(st);
  10387. if(sd==NULL || sd->pd==NULL)
  10388. return 0;
  10389. pd=sd->pd;
  10390. if (pd->a_skill == NULL)
  10391. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10392. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10393. pd->a_skill->lv=script_getnum(st,3);
  10394. pd->a_skill->div_ = 0;
  10395. pd->a_skill->rate=script_getnum(st,4);
  10396. pd->a_skill->bonusrate=script_getnum(st,5);
  10397. return 0;
  10398. }
  10399. /*==========================================
  10400. * pet attack skills [Valaris]
  10401. *------------------------------------------*/
  10402. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  10403. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  10404. BUILDIN_FUNC(petskillattack2)
  10405. {
  10406. struct pet_data *pd;
  10407. TBL_PC *sd=script_rid2sd(st);
  10408. if(sd==NULL || sd->pd==NULL)
  10409. return 0;
  10410. pd=sd->pd;
  10411. if (pd->a_skill == NULL)
  10412. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10413. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10414. pd->a_skill->lv=script_getnum(st,3);
  10415. pd->a_skill->div_ = script_getnum(st,4);
  10416. pd->a_skill->rate=script_getnum(st,5);
  10417. pd->a_skill->bonusrate=script_getnum(st,6);
  10418. return 0;
  10419. }
  10420. /*==========================================
  10421. * pet support skills [Skotlex]
  10422. *------------------------------------------*/
  10423. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  10424. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  10425. BUILDIN_FUNC(petskillsupport)
  10426. {
  10427. struct pet_data *pd;
  10428. TBL_PC *sd=script_rid2sd(st);
  10429. if(sd==NULL || sd->pd==NULL)
  10430. return 0;
  10431. pd=sd->pd;
  10432. if (pd->s_skill)
  10433. { //Clear previous skill
  10434. if (pd->s_skill->timer != INVALID_TIMER)
  10435. {
  10436. if (pd->s_skill->id)
  10437. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10438. else
  10439. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10440. }
  10441. } else //init memory
  10442. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10443. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10444. pd->s_skill->lv=script_getnum(st,3);
  10445. pd->s_skill->delay=script_getnum(st,4);
  10446. pd->s_skill->hp=script_getnum(st,5);
  10447. pd->s_skill->sp=script_getnum(st,6);
  10448. //Use delay as initial offset to avoid skill/heal exploits
  10449. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10450. pd->s_skill->timer = INVALID_TIMER;
  10451. else
  10452. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  10453. return 0;
  10454. }
  10455. /*==========================================
  10456. * Scripted skill effects [Celest]
  10457. *------------------------------------------*/
  10458. /// skilleffect <skill id>,<level>
  10459. /// skilleffect "<skill name>",<level>
  10460. BUILDIN_FUNC(skilleffect)
  10461. {
  10462. TBL_PC *sd;
  10463. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10464. int skilllv=script_getnum(st,3);
  10465. sd=script_rid2sd(st);
  10466. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  10467. return 0;
  10468. }
  10469. /*==========================================
  10470. * NPC skill effects [Valaris]
  10471. *------------------------------------------*/
  10472. /// npcskilleffect <skill id>,<level>,<x>,<y>
  10473. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  10474. BUILDIN_FUNC(npcskilleffect)
  10475. {
  10476. struct block_list *bl= map_id2bl(st->oid);
  10477. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10478. int skilllv=script_getnum(st,3);
  10479. int x=script_getnum(st,4);
  10480. int y=script_getnum(st,5);
  10481. if (bl)
  10482. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  10483. return 0;
  10484. }
  10485. /*==========================================
  10486. * Special effects [Valaris]
  10487. *------------------------------------------*/
  10488. BUILDIN_FUNC(specialeffect)
  10489. {
  10490. struct block_list *bl=map_id2bl(st->oid);
  10491. int type = script_getnum(st,2);
  10492. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10493. if(bl==NULL)
  10494. return 0;
  10495. if( script_hasdata(st,4) )
  10496. {
  10497. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10498. if(nd)
  10499. clif_specialeffect(&nd->bl, type, target);
  10500. }
  10501. else
  10502. {
  10503. if (target == SELF) {
  10504. TBL_PC *sd=script_rid2sd(st);
  10505. if (sd)
  10506. clif_specialeffect_single(bl,type,sd->fd);
  10507. } else {
  10508. clif_specialeffect(bl, type, target);
  10509. }
  10510. }
  10511. return 0;
  10512. }
  10513. BUILDIN_FUNC(specialeffect2)
  10514. {
  10515. TBL_PC *sd=script_rid2sd(st);
  10516. int type = script_getnum(st,2);
  10517. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10518. if( script_hasdata(st,4) )
  10519. sd = map_nick2sd(script_getstr(st,4));
  10520. if (sd)
  10521. clif_specialeffect(&sd->bl, type, target);
  10522. return 0;
  10523. }
  10524. /*==========================================
  10525. * Nude [Valaris]
  10526. *------------------------------------------*/
  10527. BUILDIN_FUNC(nude)
  10528. {
  10529. TBL_PC *sd=script_rid2sd(st);
  10530. int i,calcflag=0;
  10531. if(sd==NULL)
  10532. return 0;
  10533. for(i=0;i<11;i++)
  10534. if(sd->equip_index[i] >= 0) {
  10535. if(!calcflag)
  10536. calcflag=1;
  10537. pc_unequipitem(sd,sd->equip_index[i],2);
  10538. }
  10539. if(calcflag)
  10540. status_calc_pc(sd,0);
  10541. return 0;
  10542. }
  10543. /*==========================================
  10544. * gmcommand [MouseJstr]
  10545. *------------------------------------------*/
  10546. BUILDIN_FUNC(atcommand)
  10547. {
  10548. TBL_PC dummy_sd;
  10549. TBL_PC* sd;
  10550. int fd;
  10551. const char* cmd;
  10552. cmd = script_getstr(st,2);
  10553. if (st->rid) {
  10554. sd = script_rid2sd(st);
  10555. fd = sd->fd;
  10556. } else { //Use a dummy character.
  10557. sd = &dummy_sd;
  10558. fd = 0;
  10559. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10560. if (st->oid)
  10561. {
  10562. struct block_list* bl = map_id2bl(st->oid);
  10563. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10564. if (bl->type == BL_NPC)
  10565. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10566. }
  10567. }
  10568. if (!is_atcommand(fd, sd, cmd, 0)) {
  10569. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  10570. script_reportsrc(st);
  10571. return 1;
  10572. }
  10573. return 0;
  10574. }
  10575. /*==========================================
  10576. * Displays a message for the player only (like system messages like "you got an apple" )
  10577. *------------------------------------------*/
  10578. BUILDIN_FUNC(dispbottom)
  10579. {
  10580. TBL_PC *sd=script_rid2sd(st);
  10581. const char *message;
  10582. message=script_getstr(st,2);
  10583. if(sd)
  10584. clif_disp_onlyself(sd,message,(int)strlen(message));
  10585. return 0;
  10586. }
  10587. /*==========================================
  10588. * All The Players Full Recovery
  10589. * (HP/SP full restore and resurrect if need)
  10590. *------------------------------------------*/
  10591. BUILDIN_FUNC(recovery)
  10592. {
  10593. TBL_PC* sd;
  10594. struct s_mapiterator* iter;
  10595. iter = mapit_getallusers();
  10596. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10597. {
  10598. if(pc_isdead(sd))
  10599. status_revive(&sd->bl, 100, 100);
  10600. else
  10601. status_percent_heal(&sd->bl, 100, 100);
  10602. clif_displaymessage(sd->fd,"You have been recovered!");
  10603. }
  10604. mapit_free(iter);
  10605. return 0;
  10606. }
  10607. /*==========================================
  10608. * Get your pet info: getpetinfo(n)
  10609. * n -> 0:pet_id 1:pet_class 2:pet_name
  10610. * 3:friendly 4:hungry, 5: rename flag.
  10611. *------------------------------------------*/
  10612. BUILDIN_FUNC(getpetinfo)
  10613. {
  10614. TBL_PC *sd=script_rid2sd(st);
  10615. TBL_PET *pd;
  10616. int type=script_getnum(st,2);
  10617. if(!sd || !sd->pd) {
  10618. if (type == 2)
  10619. script_pushconststr(st,"null");
  10620. else
  10621. script_pushint(st,0);
  10622. return 0;
  10623. }
  10624. pd = sd->pd;
  10625. switch(type){
  10626. case 0: script_pushint(st,pd->pet.pet_id); break;
  10627. case 1: script_pushint(st,pd->pet.class_); break;
  10628. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10629. case 3: script_pushint(st,pd->pet.intimate); break;
  10630. case 4: script_pushint(st,pd->pet.hungry); break;
  10631. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10632. default:
  10633. script_pushint(st,0);
  10634. break;
  10635. }
  10636. return 0;
  10637. }
  10638. /*==========================================
  10639. * Get your homunculus info: gethominfo(n)
  10640. * n -> 0:hom_id 1:class 2:name
  10641. * 3:friendly 4:hungry, 5: rename flag.
  10642. * 6: level
  10643. *------------------------------------------*/
  10644. BUILDIN_FUNC(gethominfo)
  10645. {
  10646. TBL_PC *sd=script_rid2sd(st);
  10647. TBL_HOM *hd;
  10648. int type=script_getnum(st,2);
  10649. hd = sd?sd->hd:NULL;
  10650. if(!merc_is_hom_active(hd))
  10651. {
  10652. if (type == 2)
  10653. script_pushconststr(st,"null");
  10654. else
  10655. script_pushint(st,0);
  10656. return 0;
  10657. }
  10658. switch(type){
  10659. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10660. case 1: script_pushint(st,hd->homunculus.class_); break;
  10661. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10662. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10663. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10664. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10665. case 6: script_pushint(st,hd->homunculus.level); break;
  10666. default:
  10667. script_pushint(st,0);
  10668. break;
  10669. }
  10670. return 0;
  10671. }
  10672. /// Retrieves information about character's mercenary
  10673. /// getmercinfo <type>[,<char id>];
  10674. BUILDIN_FUNC(getmercinfo)
  10675. {
  10676. int type, char_id;
  10677. struct map_session_data* sd;
  10678. struct mercenary_data* md;
  10679. type = script_getnum(st,2);
  10680. if( script_hasdata(st,3) )
  10681. {
  10682. char_id = script_getnum(st,3);
  10683. if( ( sd = map_charid2sd(char_id) ) == NULL )
  10684. {
  10685. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  10686. script_pushnil(st);
  10687. return 1;
  10688. }
  10689. }
  10690. else
  10691. {
  10692. if( ( sd = script_rid2sd(st) ) == NULL )
  10693. {
  10694. script_pushnil(st);
  10695. return 0;
  10696. }
  10697. }
  10698. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  10699. switch( type )
  10700. {
  10701. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  10702. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  10703. case 2:
  10704. if( md )
  10705. script_pushstrcopy(st,md->db->name);
  10706. else
  10707. script_pushconststr(st,"");
  10708. break;
  10709. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  10710. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  10711. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  10712. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  10713. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  10714. default:
  10715. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  10716. script_pushnil(st);
  10717. return 1;
  10718. }
  10719. return 0;
  10720. }
  10721. /*==========================================
  10722. * Shows wether your inventory(and equips) contain
  10723. selected card or not.
  10724. checkequipedcard(4001);
  10725. *------------------------------------------*/
  10726. BUILDIN_FUNC(checkequipedcard)
  10727. {
  10728. TBL_PC *sd=script_rid2sd(st);
  10729. if(sd){
  10730. int n,i,c=0;
  10731. c=script_getnum(st,2);
  10732. for(i=0;i<MAX_INVENTORY;i++){
  10733. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10734. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10735. continue;
  10736. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10737. if(sd->status.inventory[i].card[n]==c){
  10738. script_pushint(st,1);
  10739. return 0;
  10740. }
  10741. }
  10742. }
  10743. }
  10744. }
  10745. script_pushint(st,0);
  10746. return 0;
  10747. }
  10748. BUILDIN_FUNC(jump_zero)
  10749. {
  10750. int sel;
  10751. sel=script_getnum(st,2);
  10752. if(!sel) {
  10753. int pos;
  10754. if( !data_islabel(script_getdata(st,3)) ){
  10755. ShowError("script: jump_zero: not label !\n");
  10756. st->state=END;
  10757. return 1;
  10758. }
  10759. pos=script_getnum(st,3);
  10760. st->pos=pos;
  10761. st->state=GOTO;
  10762. }
  10763. return 0;
  10764. }
  10765. /*==========================================
  10766. * movenpc [MouseJstr]
  10767. *------------------------------------------*/
  10768. BUILDIN_FUNC(movenpc)
  10769. {
  10770. TBL_NPC *nd = NULL;
  10771. const char *npc;
  10772. int x,y;
  10773. npc = script_getstr(st,2);
  10774. x = script_getnum(st,3);
  10775. y = script_getnum(st,4);
  10776. if ((nd = npc_name2id(npc)) == NULL)
  10777. return -1;
  10778. if (script_hasdata(st,5))
  10779. nd->ud.dir = script_getnum(st,5) % 8;
  10780. npc_movenpc(nd, x, y);
  10781. return 0;
  10782. }
  10783. /*==========================================
  10784. * message [MouseJstr]
  10785. *------------------------------------------*/
  10786. BUILDIN_FUNC(message)
  10787. {
  10788. const char *msg,*player;
  10789. TBL_PC *pl_sd = NULL;
  10790. player = script_getstr(st,2);
  10791. msg = script_getstr(st,3);
  10792. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10793. return 0;
  10794. clif_displaymessage(pl_sd->fd, msg);
  10795. return 0;
  10796. }
  10797. /*==========================================
  10798. * npctalk (sends message to surrounding area)
  10799. *------------------------------------------*/
  10800. BUILDIN_FUNC(npctalk)
  10801. {
  10802. const char* str;
  10803. char name[NAME_LENGTH], message[256];
  10804. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10805. str = script_getstr(st,2);
  10806. if(nd)
  10807. {
  10808. safestrncpy(name, nd->name, sizeof(name));
  10809. strtok(name, "#"); // discard extra name identifier if present
  10810. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  10811. clif_message(&nd->bl, message);
  10812. }
  10813. return 0;
  10814. }
  10815. // change npc walkspeed [Valaris]
  10816. BUILDIN_FUNC(npcspeed)
  10817. {
  10818. struct npc_data* nd;
  10819. int speed;
  10820. speed = script_getnum(st,2);
  10821. nd =(struct npc_data *)map_id2bl(st->oid);
  10822. if( nd )
  10823. {
  10824. nd->speed = speed;
  10825. nd->ud.state.speed_changed = 1;
  10826. }
  10827. return 0;
  10828. }
  10829. // make an npc walk to a position [Valaris]
  10830. BUILDIN_FUNC(npcwalkto)
  10831. {
  10832. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10833. int x=0,y=0;
  10834. x=script_getnum(st,2);
  10835. y=script_getnum(st,3);
  10836. if(nd) {
  10837. unit_walktoxy(&nd->bl,x,y,0);
  10838. }
  10839. return 0;
  10840. }
  10841. // stop an npc's movement [Valaris]
  10842. BUILDIN_FUNC(npcstop)
  10843. {
  10844. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10845. if(nd) {
  10846. unit_stop_walking(&nd->bl,1|4);
  10847. }
  10848. return 0;
  10849. }
  10850. /*==========================================
  10851. * getlook char info. getlook(arg)
  10852. *------------------------------------------*/
  10853. BUILDIN_FUNC(getlook)
  10854. {
  10855. int type,val;
  10856. TBL_PC *sd;
  10857. sd=script_rid2sd(st);
  10858. type=script_getnum(st,2);
  10859. val=-1;
  10860. switch(type) {
  10861. case LOOK_HAIR: val=sd->status.hair; break; //1
  10862. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10863. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10864. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10865. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10866. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10867. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10868. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10869. case LOOK_SHOES: break; //9
  10870. }
  10871. script_pushint(st,val);
  10872. return 0;
  10873. }
  10874. /*==========================================
  10875. * get char save point. argument: 0- map name, 1- x, 2- y
  10876. *------------------------------------------*/
  10877. BUILDIN_FUNC(getsavepoint)
  10878. {
  10879. TBL_PC* sd;
  10880. int type;
  10881. sd = script_rid2sd(st);
  10882. if (sd == NULL) {
  10883. script_pushint(st,0);
  10884. return 0;
  10885. }
  10886. type = script_getnum(st,2);
  10887. switch(type) {
  10888. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10889. case 1: script_pushint(st,sd->status.save_point.x); break;
  10890. case 2: script_pushint(st,sd->status.save_point.y); break;
  10891. default:
  10892. script_pushint(st,0);
  10893. break;
  10894. }
  10895. return 0;
  10896. }
  10897. /*==========================================
  10898. * Get position for char/npc/pet/mob objects. Added by Lorky
  10899. *
  10900. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10901. * where type:
  10902. * MapName$ - String variable for output map name
  10903. * MapX - Integer variable for output coord X
  10904. * MapY - Integer variable for output coord Y
  10905. * type - type of object
  10906. * 0 - Character coord
  10907. * 1 - NPC coord
  10908. * 2 - Pet coord
  10909. * 3 - Mob coord (not released)
  10910. * 4 - Homun coord
  10911. * 5 - Mercenary coord
  10912. * 6 - Elemental coord
  10913. * CharName$ - Name object. If miss or "this" the current object
  10914. *
  10915. * Return:
  10916. * 0 - success
  10917. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10918. *------------------------------------------*/
  10919. BUILDIN_FUNC(getmapxy)
  10920. {
  10921. struct block_list *bl = NULL;
  10922. TBL_PC *sd=NULL;
  10923. int num;
  10924. const char *name;
  10925. char prefix;
  10926. int x,y,type;
  10927. char mapname[MAP_NAME_LENGTH];
  10928. if( !data_isreference(script_getdata(st,2)) ){
  10929. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10930. script_pushint(st,-1);
  10931. return 1;
  10932. }
  10933. if( !data_isreference(script_getdata(st,3)) ){
  10934. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10935. script_pushint(st,-1);
  10936. return 1;
  10937. }
  10938. if( !data_isreference(script_getdata(st,4)) ){
  10939. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10940. script_pushint(st,-1);
  10941. return 1;
  10942. }
  10943. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10944. type=script_getnum(st,5);
  10945. switch (type){
  10946. case 0: //Get Character Position
  10947. if( script_hasdata(st,6) )
  10948. sd=map_nick2sd(script_getstr(st,6));
  10949. else
  10950. sd=script_rid2sd(st);
  10951. if (sd)
  10952. bl = &sd->bl;
  10953. break;
  10954. case 1: //Get NPC Position
  10955. if( script_hasdata(st,6) )
  10956. {
  10957. struct npc_data *nd;
  10958. nd=npc_name2id(script_getstr(st,6));
  10959. if (nd)
  10960. bl = &nd->bl;
  10961. } else //In case the origin is not an npc?
  10962. bl=map_id2bl(st->oid);
  10963. break;
  10964. case 2: //Get Pet Position
  10965. if(script_hasdata(st,6))
  10966. sd=map_nick2sd(script_getstr(st,6));
  10967. else
  10968. sd=script_rid2sd(st);
  10969. if (sd && sd->pd)
  10970. bl = &sd->pd->bl;
  10971. break;
  10972. case 3: //Get Mob Position
  10973. break; //Not supported?
  10974. case 4: //Get Homun Position
  10975. if(script_hasdata(st,6))
  10976. sd=map_nick2sd(script_getstr(st,6));
  10977. else
  10978. sd=script_rid2sd(st);
  10979. if (sd && sd->hd)
  10980. bl = &sd->hd->bl;
  10981. break;
  10982. case 5: //Get Mercenary Position
  10983. if(script_hasdata(st,6))
  10984. sd=map_nick2sd(script_getstr(st,6));
  10985. else
  10986. sd=script_rid2sd(st);
  10987. if (sd && sd->md)
  10988. bl = &sd->md->bl;
  10989. break;
  10990. case 6: //Get Elemental Position
  10991. if(script_hasdata(st,6))
  10992. sd=map_nick2sd(script_getstr(st,6));
  10993. else
  10994. sd=script_rid2sd(st);
  10995. if (sd && sd->ed)
  10996. bl = &sd->ed->bl;
  10997. break;
  10998. default:
  10999. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  11000. script_pushint(st,-1);
  11001. return 1;
  11002. }
  11003. if (!bl) { //No object found.
  11004. script_pushint(st,-1);
  11005. return 0;
  11006. }
  11007. x= bl->x;
  11008. y= bl->y;
  11009. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  11010. //Set MapName$
  11011. num=st->stack->stack_data[st->start+2].u.num;
  11012. name=get_str(num&0x00ffffff);
  11013. prefix=*name;
  11014. if(not_server_variable(prefix))
  11015. sd=script_rid2sd(st);
  11016. else
  11017. sd=NULL;
  11018. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  11019. //Set MapX
  11020. num=st->stack->stack_data[st->start+3].u.num;
  11021. name=get_str(num&0x00ffffff);
  11022. prefix=*name;
  11023. if(not_server_variable(prefix))
  11024. sd=script_rid2sd(st);
  11025. else
  11026. sd=NULL;
  11027. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  11028. //Set MapY
  11029. num=st->stack->stack_data[st->start+4].u.num;
  11030. name=get_str(num&0x00ffffff);
  11031. prefix=*name;
  11032. if(not_server_variable(prefix))
  11033. sd=script_rid2sd(st);
  11034. else
  11035. sd=NULL;
  11036. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  11037. //Return Success value
  11038. script_pushint(st,0);
  11039. return 0;
  11040. }
  11041. /*==========================================
  11042. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  11043. *------------------------------------------*/
  11044. BUILDIN_FUNC(logmes)
  11045. {
  11046. const char *str;
  11047. TBL_PC* sd;
  11048. sd = script_rid2sd(st);
  11049. if( sd == NULL )
  11050. return 1;
  11051. str = script_getstr(st,2);
  11052. log_npc(sd,str);
  11053. return 0;
  11054. }
  11055. BUILDIN_FUNC(summon)
  11056. {
  11057. int _class, timeout=0;
  11058. const char *str,*event="";
  11059. TBL_PC *sd;
  11060. struct mob_data *md;
  11061. int tick = gettick();
  11062. sd=script_rid2sd(st);
  11063. if (!sd) return 0;
  11064. str =script_getstr(st,2);
  11065. _class=script_getnum(st,3);
  11066. if( script_hasdata(st,4) )
  11067. timeout=script_getnum(st,4);
  11068. if( script_hasdata(st,5) ){
  11069. event=script_getstr(st,5);
  11070. check_event(st, event);
  11071. }
  11072. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  11073. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  11074. if (md) {
  11075. md->master_id=sd->bl.id;
  11076. md->special_state.ai=1;
  11077. if( md->deletetimer != INVALID_TIMER )
  11078. delete_timer(md->deletetimer, mob_timer_delete);
  11079. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  11080. mob_spawn (md); //Now it is ready for spawning.
  11081. clif_specialeffect(&md->bl,344,AREA);
  11082. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  11083. }
  11084. return 0;
  11085. }
  11086. /*==========================================
  11087. * Checks whether it is daytime/nighttime
  11088. *------------------------------------------*/
  11089. BUILDIN_FUNC(isnight)
  11090. {
  11091. script_pushint(st,(night_flag == 1));
  11092. return 0;
  11093. }
  11094. BUILDIN_FUNC(isday)
  11095. {
  11096. script_pushint(st,(night_flag == 0));
  11097. return 0;
  11098. }
  11099. /*================================================
  11100. * Check how many items/cards in the list are
  11101. * equipped - used for 2/15's cards patch [celest]
  11102. *------------------------------------------------*/
  11103. BUILDIN_FUNC(isequippedcnt)
  11104. {
  11105. TBL_PC *sd;
  11106. int i, j, k, id = 1;
  11107. int ret = 0;
  11108. sd = script_rid2sd(st);
  11109. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11110. script_pushint(st,0);
  11111. return 0;
  11112. }
  11113. for (i=0; id!=0; i++) {
  11114. FETCH (i+2, id) else id = 0;
  11115. if (id <= 0)
  11116. continue;
  11117. for (j=0; j<EQI_MAX; j++) {
  11118. int index;
  11119. index = sd->equip_index[j];
  11120. if(index < 0) continue;
  11121. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11122. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11123. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11124. if(!sd->inventory_data[index])
  11125. continue;
  11126. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  11127. if (sd->inventory_data[index]->nameid == id)
  11128. ret+= sd->status.inventory[index].amount;
  11129. } else { //Count cards.
  11130. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11131. continue; //No cards
  11132. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11133. if (sd->status.inventory[index].card[k] == id)
  11134. ret++; //[Lupus]
  11135. }
  11136. }
  11137. }
  11138. }
  11139. script_pushint(st,ret);
  11140. return 0;
  11141. }
  11142. /*================================================
  11143. * Check whether another card has been
  11144. * equipped - used for 2/15's cards patch [celest]
  11145. * -- Items checked cannot be reused in another
  11146. * card set to prevent exploits
  11147. *------------------------------------------------*/
  11148. BUILDIN_FUNC(isequipped)
  11149. {
  11150. TBL_PC *sd;
  11151. int i, j, k, id = 1;
  11152. int index, flag;
  11153. int ret = -1;
  11154. //Original hash to reverse it when full check fails.
  11155. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  11156. sd = script_rid2sd(st);
  11157. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11158. script_pushint(st,0);
  11159. return 0;
  11160. }
  11161. setitem_hash = sd->bonus.setitem_hash;
  11162. setitem_hash2 = sd->bonus.setitem_hash2;
  11163. for (i=0; id!=0; i++) {
  11164. FETCH (i+2, id) else id = 0;
  11165. if (id <= 0)
  11166. continue;
  11167. flag = 0;
  11168. for (j=0; j<EQI_MAX; j++) {
  11169. index = sd->equip_index[j];
  11170. if(index < 0) continue;
  11171. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11172. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11173. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11174. if(!sd->inventory_data[index])
  11175. continue;
  11176. if (itemdb_type(id) != IT_CARD) {
  11177. if (sd->inventory_data[index]->nameid != id)
  11178. continue;
  11179. flag = 1;
  11180. break;
  11181. } else { //Cards
  11182. if (sd->inventory_data[index]->slot == 0 ||
  11183. itemdb_isspecial(sd->status.inventory[index].card[0]))
  11184. continue;
  11185. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  11186. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  11187. unsigned int hash = 0;
  11188. if (sd->status.inventory[index].card[k] != id)
  11189. continue;
  11190. hash = 1<<((j<5?j:j-5)*4 + k);
  11191. // check if card is already used by another set
  11192. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  11193. continue;
  11194. // We have found a match
  11195. flag = 1;
  11196. // Set hash so this card cannot be used by another
  11197. if (j<5)
  11198. sd->bonus.setitem_hash |= hash;
  11199. else
  11200. sd->bonus.setitem_hash2 |= hash;
  11201. break;
  11202. }
  11203. }
  11204. if (flag) break; //Card found
  11205. }
  11206. if (ret == -1)
  11207. ret = flag;
  11208. else
  11209. ret &= flag;
  11210. if (!ret) break;
  11211. }
  11212. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  11213. sd->bonus.setitem_hash = setitem_hash;
  11214. sd->bonus.setitem_hash2 = setitem_hash2;
  11215. }
  11216. script_pushint(st,ret);
  11217. return 0;
  11218. }
  11219. /*================================================
  11220. * Check how many given inserted cards in the CURRENT
  11221. * weapon - used for 2/15's cards patch [Lupus]
  11222. *------------------------------------------------*/
  11223. BUILDIN_FUNC(cardscnt)
  11224. {
  11225. TBL_PC *sd;
  11226. int i, k, id = 1;
  11227. int ret = 0;
  11228. int index;
  11229. sd = script_rid2sd(st);
  11230. for (i=0; id!=0; i++) {
  11231. FETCH (i+2, id) else id = 0;
  11232. if (id <= 0)
  11233. continue;
  11234. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  11235. if(index < 0) continue;
  11236. if(!sd->inventory_data[index])
  11237. continue;
  11238. if(itemdb_type(id) != IT_CARD) {
  11239. if (sd->inventory_data[index]->nameid == id)
  11240. ret+= sd->status.inventory[index].amount;
  11241. } else {
  11242. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11243. continue;
  11244. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11245. if (sd->status.inventory[index].card[k] == id)
  11246. ret++;
  11247. }
  11248. }
  11249. }
  11250. script_pushint(st,ret);
  11251. // script_pushint(st,current_equip_item_index);
  11252. return 0;
  11253. }
  11254. /*=======================================================
  11255. * Returns the refined number of the current item, or an
  11256. * item with inventory index specified
  11257. *-------------------------------------------------------*/
  11258. BUILDIN_FUNC(getrefine)
  11259. {
  11260. TBL_PC *sd;
  11261. if ((sd = script_rid2sd(st))!= NULL)
  11262. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  11263. else
  11264. script_pushint(st,0);
  11265. return 0;
  11266. }
  11267. /*=======================================================
  11268. * Day/Night controls
  11269. *-------------------------------------------------------*/
  11270. BUILDIN_FUNC(night)
  11271. {
  11272. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  11273. return 0;
  11274. }
  11275. BUILDIN_FUNC(day)
  11276. {
  11277. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  11278. return 0;
  11279. }
  11280. //=======================================================
  11281. // Unequip [Spectre]
  11282. //-------------------------------------------------------
  11283. BUILDIN_FUNC(unequip)
  11284. {
  11285. int i;
  11286. size_t num;
  11287. TBL_PC *sd;
  11288. num = script_getnum(st,2);
  11289. sd = script_rid2sd(st);
  11290. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  11291. {
  11292. i = pc_checkequip(sd,equip[num-1]);
  11293. if (i >= 0)
  11294. pc_unequipitem(sd,i,1|2);
  11295. }
  11296. return 0;
  11297. }
  11298. BUILDIN_FUNC(equip)
  11299. {
  11300. int nameid=0,i;
  11301. TBL_PC *sd;
  11302. struct item_data *item_data;
  11303. sd = script_rid2sd(st);
  11304. nameid=script_getnum(st,2);
  11305. if((item_data = itemdb_exists(nameid)) == NULL)
  11306. {
  11307. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  11308. return 1;
  11309. }
  11310. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  11311. if( i < MAX_INVENTORY )
  11312. pc_equipitem(sd,i,item_data->equip);
  11313. return 0;
  11314. }
  11315. BUILDIN_FUNC(autoequip)
  11316. {
  11317. int nameid, flag;
  11318. struct item_data *item_data;
  11319. nameid=script_getnum(st,2);
  11320. flag=script_getnum(st,3);
  11321. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  11322. {
  11323. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  11324. return 1;
  11325. }
  11326. if( !itemdb_isequip2(item_data) )
  11327. {
  11328. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  11329. return 1;
  11330. }
  11331. item_data->flag.autoequip = flag>0?1:0;
  11332. return 0;
  11333. }
  11334. BUILDIN_FUNC(setbattleflag)
  11335. {
  11336. const char *flag, *value;
  11337. flag = script_getstr(st,2);
  11338. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  11339. if (battle_set_value(flag, value) == 0)
  11340. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  11341. else
  11342. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  11343. return 0;
  11344. }
  11345. BUILDIN_FUNC(getbattleflag)
  11346. {
  11347. const char *flag;
  11348. flag = script_getstr(st,2);
  11349. script_pushint(st,battle_get_value(flag));
  11350. return 0;
  11351. }
  11352. //=======================================================
  11353. // strlen [Valaris]
  11354. //-------------------------------------------------------
  11355. BUILDIN_FUNC(getstrlen)
  11356. {
  11357. const char *str = script_getstr(st,2);
  11358. int len = (str) ? (int)strlen(str) : 0;
  11359. script_pushint(st,len);
  11360. return 0;
  11361. }
  11362. //=======================================================
  11363. // isalpha [Valaris]
  11364. //-------------------------------------------------------
  11365. BUILDIN_FUNC(charisalpha)
  11366. {
  11367. const char *str=script_getstr(st,2);
  11368. int pos=script_getnum(st,3);
  11369. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  11370. script_pushint(st,val);
  11371. return 0;
  11372. }
  11373. //=======================================================
  11374. // charisupper <str>, <index>
  11375. //-------------------------------------------------------
  11376. BUILDIN_FUNC(charisupper)
  11377. {
  11378. const char *str = script_getstr(st,2);
  11379. int pos = script_getnum(st,3);
  11380. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  11381. script_pushint(st,val);
  11382. return 0;
  11383. }
  11384. //=======================================================
  11385. // charislower <str>, <index>
  11386. //-------------------------------------------------------
  11387. BUILDIN_FUNC(charislower)
  11388. {
  11389. const char *str = script_getstr(st,2);
  11390. int pos = script_getnum(st,3);
  11391. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  11392. script_pushint(st,val);
  11393. return 0;
  11394. }
  11395. //=======================================================
  11396. // charat <str>, <index>
  11397. //-------------------------------------------------------
  11398. BUILDIN_FUNC(charat)
  11399. {
  11400. const char *str = script_getstr(st,2);
  11401. int pos = script_getnum(st,3);
  11402. char *output;
  11403. output = (char*)aMalloc(2*sizeof(char));
  11404. output[0] = '\0';
  11405. if(str && pos >= 0 && (unsigned int)pos < strlen(str))
  11406. sprintf(output, "%c", str[pos]);
  11407. script_pushstr(st, output);
  11408. return 0;
  11409. }
  11410. //=======================================================
  11411. // setchar <string>, <char>, <index>
  11412. //-------------------------------------------------------
  11413. BUILDIN_FUNC(setchar)
  11414. {
  11415. const char *str = script_getstr(st,2);
  11416. const char *c = script_getstr(st,3);
  11417. int index = script_getnum(st,4);
  11418. char *output = aStrdup(str);
  11419. if(index >= 0 && index < strlen(output))
  11420. output[index] = *c;
  11421. script_pushstr(st, output);
  11422. return 0;
  11423. }
  11424. //=======================================================
  11425. // insertchar <string>, <char>, <index>
  11426. //-------------------------------------------------------
  11427. BUILDIN_FUNC(insertchar)
  11428. {
  11429. const char *str = script_getstr(st,2);
  11430. const char *c = script_getstr(st,3);
  11431. int index = script_getnum(st,4);
  11432. char *output;
  11433. size_t len = strlen(str);
  11434. if(index < 0)
  11435. index = 0;
  11436. else if(index > len)
  11437. index = len;
  11438. output = (char*)aMalloc(len + 2);
  11439. memcpy(output, str, index);
  11440. output[index] = c[0];
  11441. memcpy(&output[index+1], &str[index], len - index);
  11442. output[len+1] = '\0';
  11443. script_pushstr(st, output);
  11444. return 0;
  11445. }
  11446. //=======================================================
  11447. // delchar <string>, <index>
  11448. //-------------------------------------------------------
  11449. BUILDIN_FUNC(delchar)
  11450. {
  11451. const char *str = script_getstr(st,2);
  11452. int index = script_getnum(st,3);
  11453. char *output;
  11454. size_t len = strlen(str);
  11455. if(index < 0 || index > len) {
  11456. //return original
  11457. output = aStrdup(str);
  11458. script_pushstr(st, output);
  11459. return 0;
  11460. }
  11461. output = (char*)aMalloc(len);
  11462. memcpy(output, str, index);
  11463. memcpy(&output[index], &str[index+1], len - index);
  11464. script_pushstr(st, output);
  11465. return 0;
  11466. }
  11467. //=======================================================
  11468. // strtoupper <str>
  11469. //-------------------------------------------------------
  11470. BUILDIN_FUNC(strtoupper)
  11471. {
  11472. const char *str = script_getstr(st,2);
  11473. char *output = aStrdup(str);
  11474. char *cursor = output;
  11475. while (*cursor != '\0') {
  11476. *cursor = TOUPPER(*cursor);
  11477. cursor++;
  11478. }
  11479. script_pushstr(st, output);
  11480. return 0;
  11481. }
  11482. //=======================================================
  11483. // strtolower <str>
  11484. //-------------------------------------------------------
  11485. BUILDIN_FUNC(strtolower)
  11486. {
  11487. const char *str = script_getstr(st,2);
  11488. char *output = aStrdup(str);
  11489. char *cursor = output;
  11490. while (*cursor != '\0') {
  11491. *cursor = TOLOWER(*cursor);
  11492. cursor++;
  11493. }
  11494. script_pushstr(st, output);
  11495. return 0;
  11496. }
  11497. //=======================================================
  11498. // substr <str>, <start>, <end>
  11499. //-------------------------------------------------------
  11500. BUILDIN_FUNC(substr)
  11501. {
  11502. const char *str = script_getstr(st,2);
  11503. char *output;
  11504. int start = script_getnum(st,3);
  11505. int end = script_getnum(st,4);
  11506. int len = 0;
  11507. if(start >= 0 && end < strlen(str) && start <= end) {
  11508. len = end - start + 1;
  11509. output = (char*)aMalloc(len + 1);
  11510. memcpy(output, &str[start], len);
  11511. } else
  11512. output = (char*)aMalloc(1);
  11513. output[len] = '\0';
  11514. script_pushstr(st, output);
  11515. return 0;
  11516. }
  11517. //=======================================================
  11518. // explode <dest_string_array>, <str>, <delimiter>
  11519. // Note: delimiter is limited to 1 char
  11520. //-------------------------------------------------------
  11521. BUILDIN_FUNC(explode)
  11522. {
  11523. struct script_data* data = script_getdata(st, 2);
  11524. const char *str = script_getstr(st,3);
  11525. const char delimiter = script_getstr(st, 4)[0];
  11526. int32 id;
  11527. size_t len = strlen(str);
  11528. int i = 0, j = 0;
  11529. int start;
  11530. char *temp;
  11531. const char* name;
  11532. TBL_PC* sd = NULL;
  11533. temp = (char*)aMalloc(len + 1);
  11534. if( !data_isreference(data) )
  11535. {
  11536. ShowError("script:explode: not a variable\n");
  11537. script_reportdata(data);
  11538. st->state = END;
  11539. return 1;// not a variable
  11540. }
  11541. id = reference_getid(data);
  11542. start = reference_getindex(data);
  11543. name = reference_getname(data);
  11544. if( not_array_variable(*name) )
  11545. {
  11546. ShowError("script:explode: illegal scope\n");
  11547. script_reportdata(data);
  11548. st->state = END;
  11549. return 1;// not supported
  11550. }
  11551. if( !is_string_variable(name) )
  11552. {
  11553. ShowError("script:explode: not string array\n");
  11554. script_reportdata(data);
  11555. st->state = END;
  11556. return 1;// data type mismatch
  11557. }
  11558. if( not_server_variable(*name) )
  11559. {
  11560. sd = script_rid2sd(st);
  11561. if( sd == NULL )
  11562. return 0;// no player attached
  11563. }
  11564. while(str[i] != '\0') {
  11565. if(str[i] == delimiter && start < 127) { //break at delimiter but ignore after reaching last array index
  11566. temp[j] = '\0';
  11567. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  11568. j = 0;
  11569. ++i;
  11570. } else {
  11571. temp[j++] = str[i++];
  11572. }
  11573. }
  11574. //set last string
  11575. temp[j] = '\0';
  11576. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  11577. aFree(temp);
  11578. return 0;
  11579. }
  11580. //=======================================================
  11581. // implode <string_array>
  11582. // implode <string_array>, <glue>
  11583. //-------------------------------------------------------
  11584. BUILDIN_FUNC(implode)
  11585. {
  11586. struct script_data* data = script_getdata(st, 2);
  11587. const char *glue = NULL, *name, *temp;
  11588. int32 glue_len = 0, array_size, id;
  11589. size_t len = 0;
  11590. int i, k = 0;
  11591. TBL_PC* sd = NULL;
  11592. char *output;
  11593. if( !data_isreference(data) )
  11594. {
  11595. ShowError("script:implode: not a variable\n");
  11596. script_reportdata(data);
  11597. st->state = END;
  11598. return 1;// not a variable
  11599. }
  11600. id = reference_getid(data);
  11601. name = reference_getname(data);
  11602. if( not_array_variable(*name) )
  11603. {
  11604. ShowError("script:implode: illegal scope\n");
  11605. script_reportdata(data);
  11606. st->state = END;
  11607. return 1;// not supported
  11608. }
  11609. if( !is_string_variable(name) )
  11610. {
  11611. ShowError("script:implode: not string array\n");
  11612. script_reportdata(data);
  11613. st->state = END;
  11614. return 1;// data type mismatch
  11615. }
  11616. if( not_server_variable(*name) )
  11617. {
  11618. sd = script_rid2sd(st);
  11619. if( sd == NULL )
  11620. return 0;// no player attached
  11621. }
  11622. //count chars
  11623. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  11624. if(array_size == -1) //empty array check (AmsTaff)
  11625. {
  11626. ShowWarning("script:implode: array length = 0\n");
  11627. output = (char*)aMalloc(sizeof(char)*5);
  11628. sprintf(output,"%s","NULL");
  11629. } else {
  11630. for(i = 0; i <= array_size; ++i) {
  11631. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11632. len += strlen(temp);
  11633. script_removetop(st, -1, 0);
  11634. }
  11635. //allocate mem
  11636. if( script_hasdata(st,3) ) {
  11637. glue = script_getstr(st,3);
  11638. glue_len = strlen(glue);
  11639. len += glue_len * (array_size);
  11640. }
  11641. output = (char*)aMalloc(len + 1);
  11642. //build output
  11643. for(i = 0; i < array_size; ++i) {
  11644. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11645. len = strlen(temp);
  11646. memcpy(&output[k], temp, len);
  11647. k += len;
  11648. if(glue_len != 0) {
  11649. memcpy(&output[k], glue, glue_len);
  11650. k += glue_len;
  11651. }
  11652. script_removetop(st, -1, 0);
  11653. }
  11654. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  11655. len = strlen(temp);
  11656. memcpy(&output[k], temp, len);
  11657. k += len;
  11658. script_removetop(st, -1, 0);
  11659. output[k] = '\0';
  11660. }
  11661. script_pushstr(st, output);
  11662. return 0;
  11663. }
  11664. //=======================================================
  11665. // sprintf(<format>, ...);
  11666. // Implements C sprintf, except format %n. The resulting string is
  11667. // returned, instead of being saved in variable by reference.
  11668. //-------------------------------------------------------
  11669. BUILDIN_FUNC(sprintf)
  11670. {
  11671. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  11672. const char* format;
  11673. char* p;
  11674. char* q;
  11675. char* buf = NULL;
  11676. char* buf2 = NULL;
  11677. struct script_data* data;
  11678. StringBuf final_buf;
  11679. // Fetch init data
  11680. format = script_getstr(st, 2);
  11681. argc = script_lastdata(st)-2;
  11682. len = strlen(format);
  11683. // Skip parsing, where no parsing is required.
  11684. if(len==0){
  11685. script_pushconststr(st,"");
  11686. return 0;
  11687. }
  11688. // Pessimistic alloc
  11689. CREATE(buf, char, len+1);
  11690. // Need not be parsed, just solve stuff like %%.
  11691. if(argc==0){
  11692. memcpy(buf,format,len+1);
  11693. script_pushstrcopy(st, buf);
  11694. aFree(buf);
  11695. return 0;
  11696. }
  11697. safestrncpy(buf, format, len+1);
  11698. // Issue sprintf for each parameter
  11699. StringBuf_Init(&final_buf);
  11700. q = buf;
  11701. while((p = strchr(q, '%'))!=NULL){
  11702. if(p!=q){
  11703. len = p-q+1;
  11704. if(buf2_len<len){
  11705. RECREATE(buf2, char, len);
  11706. buf2_len = len;
  11707. }
  11708. safestrncpy(buf2, q, len);
  11709. StringBuf_AppendStr(&final_buf, buf2);
  11710. q = p;
  11711. }
  11712. p = q+1;
  11713. if(*p=='%'){ // %%
  11714. StringBuf_AppendStr(&final_buf, "%");
  11715. q+=2;
  11716. continue;
  11717. }
  11718. if(*p=='n'){ // %n
  11719. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  11720. script_reportsrc(st);
  11721. q+=2;
  11722. continue;
  11723. }
  11724. if(arg>=argc){
  11725. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  11726. if(buf) aFree(buf);
  11727. if(buf2) aFree(buf2);
  11728. StringBuf_Destroy(&final_buf);
  11729. script_pushconststr(st,"");
  11730. return 1;
  11731. }
  11732. if((p = strchr(q+1, '%'))==NULL){
  11733. p = strchr(q, 0); // EOS
  11734. }
  11735. len = p-q+1;
  11736. if(buf2_len<len){
  11737. RECREATE(buf2, char, len);
  11738. buf2_len = len;
  11739. }
  11740. safestrncpy(buf2, q, len);
  11741. q = p;
  11742. // Note: This assumes the passed value being the correct
  11743. // type to the current format specifier. If not, the server
  11744. // probably crashes or returns anything else, than expected,
  11745. // but it would behave in normal code the same way so it's
  11746. // the scripter's responsibility.
  11747. data = script_getdata(st, arg+3);
  11748. if(data_isstring(data)){ // String
  11749. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11750. }else if(data_isint(data)){ // Number
  11751. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11752. }else if(data_isreference(data)){ // Variable
  11753. char* name = reference_getname(data);
  11754. if(name[strlen(name)-1]=='$'){ // var Str
  11755. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11756. }else{ // var Int
  11757. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11758. }
  11759. }else{ // Unsupported type
  11760. ShowError("buildin_sprintf: Unknown argument type!\n");
  11761. if(buf) aFree(buf);
  11762. if(buf2) aFree(buf2);
  11763. StringBuf_Destroy(&final_buf);
  11764. script_pushconststr(st,"");
  11765. return 1;
  11766. }
  11767. arg++;
  11768. }
  11769. // Append anything left
  11770. if(*q){
  11771. StringBuf_AppendStr(&final_buf, q);
  11772. }
  11773. // Passed more, than needed
  11774. if(arg<argc){
  11775. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  11776. script_reportsrc(st);
  11777. }
  11778. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  11779. if(buf) aFree(buf);
  11780. if(buf2) aFree(buf2);
  11781. StringBuf_Destroy(&final_buf);
  11782. return 0;
  11783. }
  11784. //=======================================================
  11785. // sscanf(<str>, <format>, ...);
  11786. // Implements C sscanf.
  11787. //-------------------------------------------------------
  11788. BUILDIN_FUNC(sscanf){
  11789. unsigned int argc, arg = 0, len;
  11790. struct script_data* data;
  11791. struct map_session_data* sd = NULL;
  11792. const char* str;
  11793. const char* format;
  11794. const char* p;
  11795. const char* q;
  11796. char* buf = NULL;
  11797. char* buf_p;
  11798. char* ref_str = NULL;
  11799. int ref_int;
  11800. // Get data
  11801. str = script_getstr(st, 2);
  11802. format = script_getstr(st, 3);
  11803. argc = script_lastdata(st)-3;
  11804. len = strlen(format);
  11805. CREATE(buf, char, len*2+1);
  11806. // Issue sscanf for each parameter
  11807. *buf = 0;
  11808. q = format;
  11809. while((p = strchr(q, '%'))){
  11810. if(p!=q){
  11811. strncat(buf, q, (size_t)(p-q));
  11812. q = p;
  11813. }
  11814. p = q+1;
  11815. if(*p=='*' || *p=='%'){ // Skip
  11816. strncat(buf, q, 2);
  11817. q+=2;
  11818. continue;
  11819. }
  11820. if(arg>=argc){
  11821. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  11822. script_pushint(st, -1);
  11823. if(buf) aFree(buf);
  11824. if(ref_str) aFree(ref_str);
  11825. return 1;
  11826. }
  11827. if((p = strchr(q+1, '%'))==NULL){
  11828. p = strchr(q, 0); // EOS
  11829. }
  11830. len = p-q;
  11831. strncat(buf, q, len);
  11832. q = p;
  11833. // Validate output
  11834. data = script_getdata(st, arg+4);
  11835. if(!data_isreference(data) || !reference_tovariable(data)){
  11836. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  11837. script_pushint(st, -1);
  11838. if(buf) aFree(buf);
  11839. if(ref_str) aFree(ref_str);
  11840. return 1;
  11841. }
  11842. buf_p = reference_getname(data);
  11843. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  11844. script_pushint(st, -1);
  11845. if(buf) aFree(buf);
  11846. if(ref_str) aFree(ref_str);
  11847. return 0;
  11848. }
  11849. // Save value if any
  11850. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  11851. if(ref_str==NULL){
  11852. CREATE(ref_str, char, strlen(str)+1);
  11853. }
  11854. if(sscanf(str, buf, ref_str)==0){
  11855. break;
  11856. }
  11857. set_reg(st, sd, add_str(buf_p), buf_p, (void *)(ref_str), reference_getref(data));
  11858. }else{ // Number
  11859. if(sscanf(str, buf, &ref_int)==0){
  11860. break;
  11861. }
  11862. set_reg(st, sd, add_str(buf_p), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  11863. }
  11864. arg++;
  11865. // Disable used format (%... -> %*...)
  11866. buf_p = strchr(buf, 0);
  11867. memmove(buf_p-len+2, buf_p-len+1, len);
  11868. *(buf_p-len+1) = '*';
  11869. }
  11870. // Passed more, than needed
  11871. if(arg<argc){
  11872. ShowWarning("buildin_sscanf: Unused arguments passed.\n");
  11873. script_reportsrc(st);
  11874. }
  11875. script_pushint(st, arg);
  11876. if(buf) aFree(buf);
  11877. if(ref_str) aFree(ref_str);
  11878. return 0;
  11879. }
  11880. //=======================================================
  11881. // strpos(<haystack>, <needle>)
  11882. // strpos(<haystack>, <needle>, <offset>)
  11883. //
  11884. // Implements PHP style strpos. Adapted from code from
  11885. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  11886. //-------------------------------------------------------
  11887. BUILDIN_FUNC(strpos) {
  11888. const char *haystack = script_getstr(st,2);
  11889. const char *needle = script_getstr(st,3);
  11890. int i;
  11891. size_t len;
  11892. if( script_hasdata(st,4) )
  11893. i = script_getnum(st,4);
  11894. else
  11895. i = 0;
  11896. if ( strlen(needle) == 0 ) {
  11897. script_pushint(st, -1);
  11898. return 0;
  11899. }
  11900. len = strlen(haystack);
  11901. for ( ; i < len; ++i ) {
  11902. if ( haystack[i] == *needle ) {
  11903. // matched starting char -- loop through remaining chars
  11904. const char *h, *n;
  11905. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  11906. if ( *h != *n ) {
  11907. break;
  11908. }
  11909. }
  11910. if ( !*n ) { // matched all of 'needle' to null termination
  11911. script_pushint(st, i);
  11912. return 0;
  11913. }
  11914. }
  11915. }
  11916. script_pushint(st, -1);
  11917. return 0;
  11918. }
  11919. //===============================================================
  11920. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  11921. //
  11922. // Note: Finds all instances of <search> in <input> and replaces
  11923. // with <replace>. If specified will only replace as many
  11924. // instances as specified in <count>. By default will be case
  11925. // sensitive.
  11926. //---------------------------------------------------------------
  11927. BUILDIN_FUNC(replacestr)
  11928. {
  11929. const char *input = script_getstr(st, 2);
  11930. const char *find = script_getstr(st, 3);
  11931. const char *replace = script_getstr(st, 4);
  11932. size_t inputlen = strlen(input);
  11933. size_t findlen = strlen(find);
  11934. struct StringBuf output;
  11935. bool usecase = true;
  11936. int count = 0;
  11937. int numFinds = 0;
  11938. int i = 0, f = 0;
  11939. if(findlen == 0) {
  11940. ShowError("script:replacestr: Invalid search length.\n");
  11941. st->state = END;
  11942. return 1;
  11943. }
  11944. if(script_hasdata(st, 5)) {
  11945. if(script_isint(st,5))
  11946. usecase = script_getnum(st, 5) != 0;
  11947. else {
  11948. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  11949. st->state = END;
  11950. return 1;
  11951. }
  11952. }
  11953. if(script_hasdata(st, 6)) {
  11954. if(script_isint(st,6))
  11955. count = script_getnum(st, 6);
  11956. else {
  11957. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  11958. st->state = END;
  11959. return 1;
  11960. }
  11961. }
  11962. StringBuf_Init(&output);
  11963. for(; i < inputlen; i++) {
  11964. if(count && count == numFinds) { //found enough, stop looking
  11965. break;
  11966. }
  11967. for(f = 0; f <= findlen; f++) {
  11968. if(f == findlen) { //complete match
  11969. numFinds++;
  11970. StringBuf_AppendStr(&output, replace);
  11971. i += findlen - 1;
  11972. break;
  11973. } else {
  11974. if(usecase) {
  11975. if((i + f) > inputlen || input[i + f] != find[f]) {
  11976. StringBuf_Printf(&output, "%c", input[i]);
  11977. break;
  11978. }
  11979. } else {
  11980. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11981. StringBuf_Printf(&output, "%c", input[i]);
  11982. break;
  11983. }
  11984. }
  11985. }
  11986. }
  11987. }
  11988. //append excess after enough found
  11989. if(i < inputlen)
  11990. StringBuf_AppendStr(&output, &(input[i]));
  11991. script_pushstrcopy(st, StringBuf_Value(&output));
  11992. StringBuf_Destroy(&output);
  11993. return 0;
  11994. }
  11995. //========================================================
  11996. // countstr <input>, <search>{, <usecase>}
  11997. //
  11998. // Note: Counts the number of times <search> occurs in
  11999. // <input>. By default will be case sensitive.
  12000. //--------------------------------------------------------
  12001. BUILDIN_FUNC(countstr)
  12002. {
  12003. const char *input = script_getstr(st, 2);
  12004. const char *find = script_getstr(st, 3);
  12005. size_t inputlen = strlen(input);
  12006. size_t findlen = strlen(find);
  12007. bool usecase = true;
  12008. int numFinds = 0;
  12009. int i = 0, f = 0;
  12010. if(findlen == 0) {
  12011. ShowError("script:countstr: Invalid search length.\n");
  12012. st->state = END;
  12013. return 1;
  12014. }
  12015. if(script_hasdata(st, 4)) {
  12016. if(script_isint(st,4))
  12017. usecase = script_getnum(st, 4) != 0;
  12018. else {
  12019. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  12020. st->state = END;
  12021. return 1;
  12022. }
  12023. }
  12024. for(; i < inputlen; i++) {
  12025. for(f = 0; f <= findlen; f++) {
  12026. if(f == findlen) { //complete match
  12027. numFinds++;
  12028. i += findlen - 1;
  12029. break;
  12030. } else {
  12031. if(usecase) {
  12032. if((i + f) > inputlen || input[i + f] != find[f]) {
  12033. break;
  12034. }
  12035. } else {
  12036. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12037. break;
  12038. }
  12039. }
  12040. }
  12041. }
  12042. }
  12043. script_pushint(st, numFinds);
  12044. return 0;
  12045. }
  12046. /// Changes the display name and/or display class of the npc.
  12047. /// Returns 0 is successful, 1 if the npc does not exist.
  12048. ///
  12049. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  12050. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  12051. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  12052. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  12053. BUILDIN_FUNC(setnpcdisplay)
  12054. {
  12055. const char* name;
  12056. const char* newname = NULL;
  12057. int class_ = -1, size = -1;
  12058. struct script_data* data;
  12059. struct npc_data* nd;
  12060. name = script_getstr(st,2);
  12061. data = script_getdata(st,3);
  12062. if( script_hasdata(st,4) )
  12063. class_ = script_getnum(st,4);
  12064. if( script_hasdata(st,5) )
  12065. size = script_getnum(st,5);
  12066. get_val(st, data);
  12067. if( data_isstring(data) )
  12068. newname = conv_str(st,data);
  12069. else if( data_isint(data) )
  12070. class_ = conv_num(st,data);
  12071. else
  12072. {
  12073. ShowError("script:setnpcdisplay: expected a string or number\n");
  12074. script_reportdata(data);
  12075. return 1;
  12076. }
  12077. nd = npc_name2id(name);
  12078. if( nd == NULL )
  12079. {// not found
  12080. script_pushint(st,1);
  12081. return 0;
  12082. }
  12083. // update npc
  12084. if( newname )
  12085. npc_setdisplayname(nd, newname);
  12086. if( size != -1 && size != (int)nd->size )
  12087. nd->size = size;
  12088. else
  12089. size = -1;
  12090. if( class_ != -1 && nd->class_ != class_ )
  12091. npc_setclass(nd, class_);
  12092. else if( size != -1 )
  12093. { // Required to update the visual size
  12094. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  12095. clif_spawn(&nd->bl);
  12096. }
  12097. script_pushint(st,0);
  12098. return 0;
  12099. }
  12100. BUILDIN_FUNC(atoi)
  12101. {
  12102. const char *value;
  12103. value = script_getstr(st,2);
  12104. script_pushint(st,atoi(value));
  12105. return 0;
  12106. }
  12107. // case-insensitive substring search [lordalfa]
  12108. BUILDIN_FUNC(compare)
  12109. {
  12110. const char *message;
  12111. const char *cmpstring;
  12112. message = script_getstr(st,2);
  12113. cmpstring = script_getstr(st,3);
  12114. script_pushint(st,(stristr(message,cmpstring) != NULL));
  12115. return 0;
  12116. }
  12117. // [zBuffer] List of mathematics commands --->
  12118. BUILDIN_FUNC(sqrt)
  12119. {
  12120. double i, a;
  12121. i = script_getnum(st,2);
  12122. a = sqrt(i);
  12123. script_pushint(st,(int)a);
  12124. return 0;
  12125. }
  12126. BUILDIN_FUNC(pow)
  12127. {
  12128. double i, a, b;
  12129. a = script_getnum(st,2);
  12130. b = script_getnum(st,3);
  12131. i = pow(a,b);
  12132. script_pushint(st,(int)i);
  12133. return 0;
  12134. }
  12135. BUILDIN_FUNC(distance)
  12136. {
  12137. int x0, y0, x1, y1;
  12138. x0 = script_getnum(st,2);
  12139. y0 = script_getnum(st,3);
  12140. x1 = script_getnum(st,4);
  12141. y1 = script_getnum(st,5);
  12142. script_pushint(st,distance_xy(x0,y0,x1,y1));
  12143. return 0;
  12144. }
  12145. // <--- [zBuffer] List of mathematics commands
  12146. BUILDIN_FUNC(md5)
  12147. {
  12148. const char *tmpstr;
  12149. char *md5str;
  12150. tmpstr = script_getstr(st,2);
  12151. md5str = (char *)aMalloc((32+1)*sizeof(char));
  12152. MD5_String(tmpstr, md5str);
  12153. script_pushstr(st, md5str);
  12154. return 0;
  12155. }
  12156. // [zBuffer] List of dynamic var commands --->
  12157. BUILDIN_FUNC(setd)
  12158. {
  12159. TBL_PC *sd=NULL;
  12160. char varname[100];
  12161. const char *buffer;
  12162. int elem;
  12163. buffer = script_getstr(st, 2);
  12164. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  12165. elem = 0;
  12166. if( not_server_variable(*varname) )
  12167. {
  12168. sd = script_rid2sd(st);
  12169. if( sd == NULL )
  12170. {
  12171. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  12172. return 0;
  12173. }
  12174. }
  12175. if( is_string_variable(varname) ) {
  12176. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  12177. } else {
  12178. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  12179. }
  12180. return 0;
  12181. }
  12182. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  12183. {
  12184. int i, j;
  12185. TBL_PC* sd = NULL;
  12186. const char* query;
  12187. struct script_data* data;
  12188. const char* name;
  12189. int max_rows = SCRIPT_MAX_ARRAYSIZE;// maximum number of rows
  12190. int num_vars;
  12191. int num_cols;
  12192. // check target variables
  12193. for( i = 3; script_hasdata(st,i); ++i )
  12194. {
  12195. data = script_getdata(st, i);
  12196. if( data_isreference(data) && reference_tovariable(data) )
  12197. {// it's a variable
  12198. name = reference_getname(data);
  12199. if( not_server_variable(*name) && sd == NULL )
  12200. {// requires a player
  12201. sd = script_rid2sd(st);
  12202. if( sd == NULL )
  12203. {// no player attached
  12204. script_reportdata(data);
  12205. st->state = END;
  12206. return 1;
  12207. }
  12208. }
  12209. if( not_array_variable(*name) )
  12210. max_rows = 1;// not an array, limit to one row
  12211. }
  12212. else
  12213. {
  12214. ShowError("script:query_sql: not a variable\n");
  12215. script_reportdata(data);
  12216. st->state = END;
  12217. return 1;
  12218. }
  12219. }
  12220. num_vars = i - 3;
  12221. // Execute the query
  12222. query = script_getstr(st,2);
  12223. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  12224. {
  12225. Sql_ShowDebug(handle);
  12226. script_pushint(st, 0);
  12227. return 1;
  12228. }
  12229. if( Sql_NumRows(handle) == 0 )
  12230. {// No data received
  12231. Sql_FreeResult(handle);
  12232. script_pushint(st, 0);
  12233. return 0;
  12234. }
  12235. // Count the number of columns to store
  12236. num_cols = Sql_NumColumns(handle);
  12237. if( num_vars < num_cols )
  12238. {
  12239. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  12240. script_reportsrc(st);
  12241. }
  12242. else if( num_vars > num_cols )
  12243. {
  12244. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  12245. script_reportsrc(st);
  12246. }
  12247. // Store data
  12248. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  12249. {
  12250. for( j = 0; j < num_vars; ++j )
  12251. {
  12252. char* str = NULL;
  12253. if( j < num_cols )
  12254. Sql_GetData(handle, j, &str, NULL);
  12255. data = script_getdata(st, j+3);
  12256. name = reference_getname(data);
  12257. if( is_string_variable(name) )
  12258. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  12259. else
  12260. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  12261. }
  12262. }
  12263. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  12264. {
  12265. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  12266. script_reportsrc(st);
  12267. }
  12268. // Free data
  12269. Sql_FreeResult(handle);
  12270. script_pushint(st, i);
  12271. return 0;
  12272. }
  12273. BUILDIN_FUNC(query_sql)
  12274. {
  12275. return buildin_query_sql_sub(st, mmysql_handle);
  12276. }
  12277. BUILDIN_FUNC(query_logsql)
  12278. {
  12279. if( !log_config.sql_logs )
  12280. {// logmysql_handle == NULL
  12281. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  12282. script_pushint(st,-1);
  12283. return 1;
  12284. }
  12285. return buildin_query_sql_sub(st, logmysql_handle);
  12286. }
  12287. //Allows escaping of a given string.
  12288. BUILDIN_FUNC(escape_sql)
  12289. {
  12290. const char *str;
  12291. char *esc_str;
  12292. size_t len;
  12293. str = script_getstr(st,2);
  12294. len = strlen(str);
  12295. esc_str = (char*)aMalloc(len*2+1);
  12296. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  12297. script_pushstr(st, esc_str);
  12298. return 0;
  12299. }
  12300. BUILDIN_FUNC(getd)
  12301. {
  12302. char varname[100];
  12303. const char *buffer;
  12304. int elem;
  12305. buffer = script_getstr(st, 2);
  12306. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  12307. elem = 0;
  12308. // Push the 'pointer' so it's more flexible [Lance]
  12309. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  12310. return 0;
  12311. }
  12312. // <--- [zBuffer] List of dynamic var commands
  12313. // Pet stat [Lance]
  12314. BUILDIN_FUNC(petstat)
  12315. {
  12316. TBL_PC *sd = NULL;
  12317. struct pet_data *pd;
  12318. int flag = script_getnum(st,2);
  12319. sd = script_rid2sd(st);
  12320. if(!sd || !sd->status.pet_id || !sd->pd){
  12321. if(flag == 2)
  12322. script_pushconststr(st, "");
  12323. else
  12324. script_pushint(st,0);
  12325. return 0;
  12326. }
  12327. pd = sd->pd;
  12328. switch(flag){
  12329. case 1: script_pushint(st,(int)pd->pet.class_); break;
  12330. case 2: script_pushstrcopy(st, pd->pet.name); break;
  12331. case 3: script_pushint(st,(int)pd->pet.level); break;
  12332. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  12333. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  12334. default:
  12335. script_pushint(st,0);
  12336. break;
  12337. }
  12338. return 0;
  12339. }
  12340. BUILDIN_FUNC(callshop)
  12341. {
  12342. TBL_PC *sd = NULL;
  12343. struct npc_data *nd;
  12344. const char *shopname;
  12345. int flag = 0;
  12346. sd = script_rid2sd(st);
  12347. if (!sd) {
  12348. script_pushint(st,0);
  12349. return 0;
  12350. }
  12351. shopname = script_getstr(st, 2);
  12352. if( script_hasdata(st,3) )
  12353. flag = script_getnum(st,3);
  12354. nd = npc_name2id(shopname);
  12355. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  12356. {
  12357. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  12358. script_pushint(st,0);
  12359. return 1;
  12360. }
  12361. if( nd->subtype == SHOP )
  12362. {
  12363. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  12364. sd->state.callshop = 1;
  12365. switch( flag )
  12366. {
  12367. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  12368. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  12369. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  12370. }
  12371. }
  12372. else
  12373. clif_cashshop_show(sd, nd);
  12374. sd->npc_shopid = nd->bl.id;
  12375. script_pushint(st,1);
  12376. return 0;
  12377. }
  12378. BUILDIN_FUNC(npcshopitem)
  12379. {
  12380. const char* npcname = script_getstr(st, 2);
  12381. struct npc_data* nd = npc_name2id(npcname);
  12382. int n, i;
  12383. int amount;
  12384. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12385. { //Not found.
  12386. script_pushint(st,0);
  12387. return 0;
  12388. }
  12389. // get the count of new entries
  12390. amount = (script_lastdata(st)-2)/2;
  12391. // generate new shop item list
  12392. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  12393. for( n = 0, i = 3; n < amount; n++, i+=2 )
  12394. {
  12395. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12396. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12397. }
  12398. nd->u.shop.count = n;
  12399. script_pushint(st,1);
  12400. return 0;
  12401. }
  12402. BUILDIN_FUNC(npcshopadditem)
  12403. {
  12404. const char* npcname = script_getstr(st,2);
  12405. struct npc_data* nd = npc_name2id(npcname);
  12406. int n, i;
  12407. int amount;
  12408. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12409. { //Not found.
  12410. script_pushint(st,0);
  12411. return 0;
  12412. }
  12413. // get the count of new entries
  12414. amount = (script_lastdata(st)-2)/2;
  12415. // append new items to existing shop item list
  12416. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  12417. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  12418. {
  12419. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12420. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12421. }
  12422. nd->u.shop.count = n;
  12423. script_pushint(st,1);
  12424. return 0;
  12425. }
  12426. BUILDIN_FUNC(npcshopdelitem)
  12427. {
  12428. const char* npcname = script_getstr(st,2);
  12429. struct npc_data* nd = npc_name2id(npcname);
  12430. unsigned int nameid;
  12431. int n, i;
  12432. int amount;
  12433. int size;
  12434. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12435. { //Not found.
  12436. script_pushint(st,0);
  12437. return 0;
  12438. }
  12439. amount = script_lastdata(st)-2;
  12440. size = nd->u.shop.count;
  12441. // remove specified items from the shop item list
  12442. for( i = 3; i < 3 + amount; i++ )
  12443. {
  12444. nameid = script_getnum(st,i);
  12445. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  12446. if( n < size )
  12447. {
  12448. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  12449. size--;
  12450. }
  12451. }
  12452. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  12453. nd->u.shop.count = size;
  12454. script_pushint(st,1);
  12455. return 0;
  12456. }
  12457. //Sets a script to attach to a shop npc.
  12458. BUILDIN_FUNC(npcshopattach)
  12459. {
  12460. const char* npcname = script_getstr(st,2);
  12461. struct npc_data* nd = npc_name2id(npcname);
  12462. int flag = 1;
  12463. if( script_hasdata(st,3) )
  12464. flag = script_getnum(st,3);
  12465. if( !nd || nd->subtype != SHOP )
  12466. { //Not found.
  12467. script_pushint(st,0);
  12468. return 0;
  12469. }
  12470. if (flag)
  12471. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  12472. else
  12473. nd->master_nd = NULL;
  12474. script_pushint(st,1);
  12475. return 0;
  12476. }
  12477. /*==========================================
  12478. * Returns some values of an item [Lupus]
  12479. * Price, Weight, etc...
  12480. setitemscript(itemID,"{new item bonus script}",[n]);
  12481. Where n:
  12482. 0 - script
  12483. 1 - Equip script
  12484. 2 - Unequip script
  12485. *------------------------------------------*/
  12486. BUILDIN_FUNC(setitemscript)
  12487. {
  12488. int item_id,n=0;
  12489. const char *script;
  12490. struct item_data *i_data;
  12491. struct script_code **dstscript;
  12492. item_id = script_getnum(st,2);
  12493. script = script_getstr(st,3);
  12494. if( script_hasdata(st,4) )
  12495. n=script_getnum(st,4);
  12496. i_data = itemdb_exists(item_id);
  12497. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  12498. script_pushint(st,0);
  12499. return 0;
  12500. }
  12501. switch (n) {
  12502. case 2:
  12503. dstscript = &i_data->unequip_script;
  12504. break;
  12505. case 1:
  12506. dstscript = &i_data->equip_script;
  12507. break;
  12508. default:
  12509. dstscript = &i_data->script;
  12510. break;
  12511. }
  12512. if(*dstscript)
  12513. script_free_code(*dstscript);
  12514. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  12515. script_pushint(st,1);
  12516. return 0;
  12517. }
  12518. /* Work In Progress [Lupus]
  12519. BUILDIN_FUNC(addmonsterdrop)
  12520. {
  12521. int class_,item_id,chance;
  12522. class_=script_getnum(st,2);
  12523. item_id=script_getnum(st,3);
  12524. chance=script_getnum(st,4);
  12525. if(class_>1000 && item_id>500 && chance>0) {
  12526. script_pushint(st,1);
  12527. } else {
  12528. script_pushint(st,0);
  12529. }
  12530. }
  12531. BUILDIN_FUNC(delmonsterdrop)
  12532. {
  12533. int class_,item_id;
  12534. class_=script_getnum(st,2);
  12535. item_id=script_getnum(st,3);
  12536. if(class_>1000 && item_id>500) {
  12537. script_pushint(st,1);
  12538. } else {
  12539. script_pushint(st,0);
  12540. }
  12541. }
  12542. */
  12543. /*==========================================
  12544. * Returns some values of a monster [Lupus]
  12545. * Name, Level, race, size, etc...
  12546. getmonsterinfo(monsterID,queryIndex);
  12547. *------------------------------------------*/
  12548. BUILDIN_FUNC(getmonsterinfo)
  12549. {
  12550. struct mob_db *mob;
  12551. int mob_id;
  12552. mob_id = script_getnum(st,2);
  12553. if (!mobdb_checkid(mob_id)) {
  12554. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  12555. if ( !script_getnum(st,3) ) //requested a string
  12556. script_pushconststr(st,"null");
  12557. else
  12558. script_pushint(st,-1);
  12559. return -1;
  12560. }
  12561. mob = mob_db(mob_id);
  12562. switch ( script_getnum(st,3) ) {
  12563. case 0: script_pushstrcopy(st,mob->jname); break;
  12564. case 1: script_pushint(st,mob->lv); break;
  12565. case 2: script_pushint(st,mob->status.max_hp); break;
  12566. case 3: script_pushint(st,mob->base_exp); break;
  12567. case 4: script_pushint(st,mob->job_exp); break;
  12568. case 5: script_pushint(st,mob->status.rhw.atk); break;
  12569. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  12570. case 7: script_pushint(st,mob->status.def); break;
  12571. case 8: script_pushint(st,mob->status.mdef); break;
  12572. case 9: script_pushint(st,mob->status.str); break;
  12573. case 10: script_pushint(st,mob->status.agi); break;
  12574. case 11: script_pushint(st,mob->status.vit); break;
  12575. case 12: script_pushint(st,mob->status.int_); break;
  12576. case 13: script_pushint(st,mob->status.dex); break;
  12577. case 14: script_pushint(st,mob->status.luk); break;
  12578. case 15: script_pushint(st,mob->status.rhw.range); break;
  12579. case 16: script_pushint(st,mob->range2); break;
  12580. case 17: script_pushint(st,mob->range3); break;
  12581. case 18: script_pushint(st,mob->status.size); break;
  12582. case 19: script_pushint(st,mob->status.race); break;
  12583. case 20: script_pushint(st,mob->status.def_ele); break;
  12584. case 21: script_pushint(st,mob->status.mode); break;
  12585. case 22: script_pushint(st,mob->mexp); break;
  12586. default: script_pushint(st,-1); //wrong Index
  12587. }
  12588. return 0;
  12589. }
  12590. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  12591. {
  12592. TBL_PC *sd = NULL;
  12593. if(script_hasdata(st,2))
  12594. sd = map_nick2sd(script_getstr(st,2));
  12595. else
  12596. sd = script_rid2sd(st);
  12597. if(sd)
  12598. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  12599. else
  12600. script_pushint(st,0);
  12601. return 0;
  12602. }
  12603. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  12604. {
  12605. TBL_PC *sd = NULL;
  12606. if(script_hasdata(st,2))
  12607. sd = map_nick2sd(script_getstr(st,2));
  12608. else
  12609. sd = script_rid2sd(st);
  12610. if(sd)
  12611. script_pushint(st,(sd->chatID != 0));
  12612. else
  12613. script_pushint(st,0);
  12614. return 0;
  12615. }
  12616. BUILDIN_FUNC(searchitem)
  12617. {
  12618. struct script_data* data = script_getdata(st, 2);
  12619. const char *itemname = script_getstr(st,3);
  12620. struct item_data *items[MAX_SEARCH];
  12621. int count;
  12622. char* name;
  12623. int32 start;
  12624. int32 id;
  12625. int32 i;
  12626. TBL_PC* sd = NULL;
  12627. if ((items[0] = itemdb_exists(atoi(itemname))))
  12628. count = 1;
  12629. else {
  12630. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  12631. if (count > MAX_SEARCH) count = MAX_SEARCH;
  12632. }
  12633. if (!count) {
  12634. script_pushint(st, 0);
  12635. return 0;
  12636. }
  12637. if( !data_isreference(data) )
  12638. {
  12639. ShowError("script:searchitem: not a variable\n");
  12640. script_reportdata(data);
  12641. st->state = END;
  12642. return 1;// not a variable
  12643. }
  12644. id = reference_getid(data);
  12645. start = reference_getindex(data);
  12646. name = reference_getname(data);
  12647. if( not_array_variable(*name) )
  12648. {
  12649. ShowError("script:searchitem: illegal scope\n");
  12650. script_reportdata(data);
  12651. st->state = END;
  12652. return 1;// not supported
  12653. }
  12654. if( not_server_variable(*name) )
  12655. {
  12656. sd = script_rid2sd(st);
  12657. if( sd == NULL )
  12658. return 0;// no player attached
  12659. }
  12660. if( is_string_variable(name) )
  12661. {// string array
  12662. ShowError("script:searchitem: not an integer array reference\n");
  12663. script_reportdata(data);
  12664. st->state = END;
  12665. return 1;// not supported
  12666. }
  12667. for( i = 0; i < count; ++start, ++i )
  12668. {// Set array
  12669. void* v = (void*)__64BPRTSIZE(items[i]->nameid);
  12670. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  12671. }
  12672. script_pushint(st, count);
  12673. return 0;
  12674. }
  12675. int axtoi(const char *hexStg)
  12676. {
  12677. int n = 0; // position in string
  12678. int m = 0; // position in digit[] to shift
  12679. int count; // loop index
  12680. int intValue = 0; // integer value of hex string
  12681. int digit[11]; // hold values to convert
  12682. while (n < 10) {
  12683. if (hexStg[n]=='\0')
  12684. break;
  12685. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  12686. digit[n] = hexStg[n] & 0x0f; //convert to int
  12687. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  12688. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12689. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  12690. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12691. else break;
  12692. n++;
  12693. }
  12694. count = n;
  12695. m = n - 1;
  12696. n = 0;
  12697. while(n < count) {
  12698. // digit[n] is value of hex digit at position n
  12699. // (m << 2) is the number of positions to shift
  12700. // OR the bits into return value
  12701. intValue = intValue | (digit[n] << (m << 2));
  12702. m--; // adjust the position to set
  12703. n++; // next digit to process
  12704. }
  12705. return (intValue);
  12706. }
  12707. // [Lance] Hex string to integer converter
  12708. BUILDIN_FUNC(axtoi)
  12709. {
  12710. const char *hex = script_getstr(st,2);
  12711. script_pushint(st,axtoi(hex));
  12712. return 0;
  12713. }
  12714. // [zBuffer] List of player cont commands --->
  12715. BUILDIN_FUNC(rid2name)
  12716. {
  12717. struct block_list *bl = NULL;
  12718. int rid = script_getnum(st,2);
  12719. if((bl = map_id2bl(rid)))
  12720. {
  12721. switch(bl->type) {
  12722. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  12723. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  12724. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  12725. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  12726. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  12727. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  12728. default:
  12729. ShowError("buildin_rid2name: BL type unknown.\n");
  12730. script_pushconststr(st,"");
  12731. break;
  12732. }
  12733. } else {
  12734. ShowError("buildin_rid2name: invalid RID\n");
  12735. script_pushconststr(st,"(null)");
  12736. }
  12737. return 0;
  12738. }
  12739. BUILDIN_FUNC(pcblockmove)
  12740. {
  12741. int id, flag;
  12742. TBL_PC *sd = NULL;
  12743. id = script_getnum(st,2);
  12744. flag = script_getnum(st,3);
  12745. if(id)
  12746. sd = map_id2sd(id);
  12747. else
  12748. sd = script_rid2sd(st);
  12749. if(sd)
  12750. sd->state.blockedmove = flag > 0;
  12751. return 0;
  12752. }
  12753. BUILDIN_FUNC(pcfollow)
  12754. {
  12755. int id, targetid;
  12756. TBL_PC *sd = NULL;
  12757. id = script_getnum(st,2);
  12758. targetid = script_getnum(st,3);
  12759. if(id)
  12760. sd = map_id2sd(id);
  12761. else
  12762. sd = script_rid2sd(st);
  12763. if(sd)
  12764. pc_follow(sd, targetid);
  12765. return 0;
  12766. }
  12767. BUILDIN_FUNC(pcstopfollow)
  12768. {
  12769. int id;
  12770. TBL_PC *sd = NULL;
  12771. id = script_getnum(st,2);
  12772. if(id)
  12773. sd = map_id2sd(id);
  12774. else
  12775. sd = script_rid2sd(st);
  12776. if(sd)
  12777. pc_stop_following(sd);
  12778. return 0;
  12779. }
  12780. // <--- [zBuffer] List of player cont commands
  12781. // [zBuffer] List of mob control commands --->
  12782. //## TODO always return if the request/whatever was successfull [FlavioJS]
  12783. /// Makes the unit walk to target position or map
  12784. /// Returns if it was successfull
  12785. ///
  12786. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  12787. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  12788. BUILDIN_FUNC(unitwalk)
  12789. {
  12790. struct block_list* bl;
  12791. bl = map_id2bl(script_getnum(st,2));
  12792. if( bl == NULL )
  12793. {
  12794. script_pushint(st, 0);
  12795. }
  12796. else if( script_hasdata(st,4) )
  12797. {
  12798. int x = script_getnum(st,3);
  12799. int y = script_getnum(st,4);
  12800. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  12801. }
  12802. else
  12803. {
  12804. int map_id = script_getnum(st,3);
  12805. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  12806. }
  12807. return 0;
  12808. }
  12809. /// Kills the unit
  12810. ///
  12811. /// unitkill <unit_id>;
  12812. BUILDIN_FUNC(unitkill)
  12813. {
  12814. struct block_list* bl = map_id2bl(script_getnum(st,2));
  12815. if( bl != NULL )
  12816. status_kill(bl);
  12817. return 0;
  12818. }
  12819. /// Warps the unit to the target position in the target map
  12820. /// Returns if it was successfull
  12821. ///
  12822. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  12823. BUILDIN_FUNC(unitwarp)
  12824. {
  12825. int unit_id;
  12826. int map;
  12827. short x;
  12828. short y;
  12829. struct block_list* bl;
  12830. const char *mapname;
  12831. unit_id = script_getnum(st,2);
  12832. mapname = script_getstr(st, 3);
  12833. x = (short)script_getnum(st,4);
  12834. y = (short)script_getnum(st,5);
  12835. if (!unit_id) //Warp the script's runner
  12836. bl = map_id2bl(st->rid);
  12837. else
  12838. bl = map_id2bl(unit_id);
  12839. if( strcmp(mapname,"this") == 0 )
  12840. map = bl?bl->m:-1;
  12841. else
  12842. map = map_mapname2mapid(mapname);
  12843. if( map >= 0 && bl != NULL )
  12844. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  12845. else
  12846. script_pushint(st, 0);
  12847. return 0;
  12848. }
  12849. /// Makes the unit attack the target.
  12850. /// If the unit is a player and <action type> is not 0, it does a continuous
  12851. /// attack instead of a single attack.
  12852. /// Returns if the request was successfull.
  12853. ///
  12854. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  12855. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  12856. BUILDIN_FUNC(unitattack)
  12857. {
  12858. struct block_list* unit_bl;
  12859. struct block_list* target_bl = NULL;
  12860. struct script_data* data;
  12861. int actiontype = 0;
  12862. // get unit
  12863. unit_bl = map_id2bl(script_getnum(st,2));
  12864. if( unit_bl == NULL ) {
  12865. script_pushint(st, 0);
  12866. return 0;
  12867. }
  12868. data = script_getdata(st, 3);
  12869. get_val(st, data);
  12870. if( data_isstring(data) )
  12871. {
  12872. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  12873. if( sd != NULL )
  12874. target_bl = &sd->bl;
  12875. } else
  12876. target_bl = map_id2bl(conv_num(st, data));
  12877. // request the attack
  12878. if( target_bl == NULL )
  12879. {
  12880. script_pushint(st, 0);
  12881. return 0;
  12882. }
  12883. // get actiontype
  12884. if( script_hasdata(st,4) )
  12885. actiontype = script_getnum(st,4);
  12886. switch( unit_bl->type )
  12887. {
  12888. case BL_PC:
  12889. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  12890. script_pushint(st, 1);
  12891. return 0;
  12892. case BL_MOB:
  12893. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  12894. break;
  12895. case BL_PET:
  12896. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  12897. break;
  12898. default:
  12899. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  12900. script_pushint(st, 0);
  12901. return 1;
  12902. }
  12903. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  12904. return 0;
  12905. }
  12906. /// Makes the unit stop attacking and moving
  12907. ///
  12908. /// unitstop <unit_id>;
  12909. BUILDIN_FUNC(unitstop)
  12910. {
  12911. int unit_id;
  12912. struct block_list* bl;
  12913. unit_id = script_getnum(st,2);
  12914. bl = map_id2bl(unit_id);
  12915. if( bl != NULL )
  12916. {
  12917. unit_stop_attack(bl);
  12918. unit_stop_walking(bl,4);
  12919. if( bl->type == BL_MOB )
  12920. ((TBL_MOB*)bl)->target_id = 0;
  12921. }
  12922. return 0;
  12923. }
  12924. /// Makes the unit say the message
  12925. ///
  12926. /// unittalk <unit_id>,"<message>";
  12927. BUILDIN_FUNC(unittalk)
  12928. {
  12929. int unit_id;
  12930. const char* message;
  12931. struct block_list* bl;
  12932. unit_id = script_getnum(st,2);
  12933. message = script_getstr(st, 3);
  12934. bl = map_id2bl(unit_id);
  12935. if( bl != NULL )
  12936. {
  12937. struct StringBuf sbuf;
  12938. StringBuf_Init(&sbuf);
  12939. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  12940. clif_message(bl, StringBuf_Value(&sbuf));
  12941. if( bl->type == BL_PC )
  12942. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  12943. StringBuf_Destroy(&sbuf);
  12944. }
  12945. return 0;
  12946. }
  12947. /// Makes the unit do an emotion
  12948. ///
  12949. /// unitemote <unit_id>,<emotion>;
  12950. ///
  12951. /// @see e_* in const.txt
  12952. BUILDIN_FUNC(unitemote)
  12953. {
  12954. int unit_id;
  12955. int emotion;
  12956. struct block_list* bl;
  12957. unit_id = script_getnum(st,2);
  12958. emotion = script_getnum(st,3);
  12959. bl = map_id2bl(unit_id);
  12960. if( bl != NULL )
  12961. clif_emotion(bl, emotion);
  12962. return 0;
  12963. }
  12964. /// Makes the unit cast the skill on the target or self if no target is specified
  12965. ///
  12966. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  12967. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  12968. BUILDIN_FUNC(unitskilluseid)
  12969. {
  12970. int unit_id;
  12971. int skill_id;
  12972. int skill_lv;
  12973. int target_id;
  12974. struct block_list* bl;
  12975. unit_id = script_getnum(st,2);
  12976. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12977. skill_lv = script_getnum(st,4);
  12978. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  12979. bl = map_id2bl(unit_id);
  12980. if( bl != NULL )
  12981. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  12982. return 0;
  12983. }
  12984. /// Makes the unit cast the skill on the target position.
  12985. ///
  12986. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  12987. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  12988. BUILDIN_FUNC(unitskillusepos)
  12989. {
  12990. int unit_id;
  12991. int skill_id;
  12992. int skill_lv;
  12993. int skill_x;
  12994. int skill_y;
  12995. struct block_list* bl;
  12996. unit_id = script_getnum(st,2);
  12997. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12998. skill_lv = script_getnum(st,4);
  12999. skill_x = script_getnum(st,5);
  13000. skill_y = script_getnum(st,6);
  13001. bl = map_id2bl(unit_id);
  13002. if( bl != NULL )
  13003. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  13004. return 0;
  13005. }
  13006. // <--- [zBuffer] List of mob control commands
  13007. /// Pauses the execution of the script, detaching the player
  13008. ///
  13009. /// sleep <mili seconds>;
  13010. BUILDIN_FUNC(sleep)
  13011. {
  13012. int ticks;
  13013. ticks = script_getnum(st,2);
  13014. // detach the player
  13015. script_detach_rid(st);
  13016. if( ticks <= 0 )
  13017. {// do nothing
  13018. }
  13019. else if( st->sleep.tick == 0 )
  13020. {// sleep for the target amount of time
  13021. st->state = RERUNLINE;
  13022. st->sleep.tick = ticks;
  13023. }
  13024. else
  13025. {// sleep time is over
  13026. st->state = RUN;
  13027. st->sleep.tick = 0;
  13028. }
  13029. return 0;
  13030. }
  13031. /// Pauses the execution of the script, keeping the player attached
  13032. /// Returns if a player is still attached
  13033. ///
  13034. /// sleep2(<mili secconds>) -> <bool>
  13035. BUILDIN_FUNC(sleep2)
  13036. {
  13037. int ticks;
  13038. ticks = script_getnum(st,2);
  13039. if( ticks <= 0 )
  13040. {// do nothing
  13041. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13042. }
  13043. else if( !st->sleep.tick )
  13044. {// sleep for the target amount of time
  13045. st->state = RERUNLINE;
  13046. st->sleep.tick = ticks;
  13047. }
  13048. else
  13049. {// sleep time is over
  13050. st->state = RUN;
  13051. st->sleep.tick = 0;
  13052. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13053. }
  13054. return 0;
  13055. }
  13056. /// Awakes all the sleep timers of the target npc
  13057. ///
  13058. /// awake "<npc name>";
  13059. BUILDIN_FUNC(awake)
  13060. {
  13061. struct npc_data* nd;
  13062. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  13063. nd = npc_name2id(script_getstr(st, 2));
  13064. if( nd == NULL ) {
  13065. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  13066. return 1;
  13067. }
  13068. while( node )
  13069. {
  13070. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  13071. {// sleep timer for the npc
  13072. struct script_state* tst = (struct script_state*)node->data;
  13073. TBL_PC* sd = map_id2sd(tst->rid);
  13074. if( tst->sleep.timer == INVALID_TIMER )
  13075. {// already awake ???
  13076. node = node->next;
  13077. continue;
  13078. }
  13079. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  13080. {// char not online anymore / another char of the same account is online - Cancel execution
  13081. tst->state = END;
  13082. tst->rid = 0;
  13083. }
  13084. delete_timer(tst->sleep.timer, run_script_timer);
  13085. node = script_erase_sleepdb(node);
  13086. tst->sleep.timer = INVALID_TIMER;
  13087. if(tst->state != RERUNLINE)
  13088. tst->sleep.tick = 0;
  13089. run_script_main(tst);
  13090. }
  13091. else
  13092. {
  13093. node = node->next;
  13094. }
  13095. }
  13096. return 0;
  13097. }
  13098. /// Returns a reference to a variable of the target NPC.
  13099. /// Returns 0 if an error occurs.
  13100. ///
  13101. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  13102. BUILDIN_FUNC(getvariableofnpc)
  13103. {
  13104. struct script_data* data;
  13105. const char* name;
  13106. struct npc_data* nd;
  13107. data = script_getdata(st,2);
  13108. if( !data_isreference(data) )
  13109. {// Not a reference (aka varaible name)
  13110. ShowError("script:getvariableofnpc: not a variable\n");
  13111. script_reportdata(data);
  13112. script_pushnil(st);
  13113. st->state = END;
  13114. return 1;
  13115. }
  13116. name = reference_getname(data);
  13117. if( *name != '.' || name[1] == '@' )
  13118. {// not a npc variable
  13119. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  13120. script_reportdata(data);
  13121. script_pushnil(st);
  13122. st->state = END;
  13123. return 1;
  13124. }
  13125. nd = npc_name2id(script_getstr(st,3));
  13126. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  13127. {// NPC not found or has no script
  13128. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  13129. script_pushnil(st);
  13130. st->state = END;
  13131. return 1;
  13132. }
  13133. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  13134. return 0;
  13135. }
  13136. /// Opens a warp portal.
  13137. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  13138. ///
  13139. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  13140. ///
  13141. /// @author blackhole89
  13142. BUILDIN_FUNC(warpportal)
  13143. {
  13144. int spx;
  13145. int spy;
  13146. unsigned short mapindex;
  13147. int tpx;
  13148. int tpy;
  13149. struct skill_unit_group* group;
  13150. struct block_list* bl;
  13151. bl = map_id2bl(st->oid);
  13152. if( bl == NULL )
  13153. {
  13154. ShowError("script:warpportal: npc is needed\n");
  13155. return 1;
  13156. }
  13157. spx = script_getnum(st,2);
  13158. spy = script_getnum(st,3);
  13159. mapindex = mapindex_name2id(script_getstr(st, 4));
  13160. tpx = script_getnum(st,5);
  13161. tpy = script_getnum(st,6);
  13162. if( mapindex == 0 )
  13163. return 0;// map not found
  13164. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  13165. if( group == NULL )
  13166. return 0;// failed
  13167. group->val2 = (tpx<<16) | tpy;
  13168. group->val3 = mapindex;
  13169. return 0;
  13170. }
  13171. BUILDIN_FUNC(openmail)
  13172. {
  13173. TBL_PC* sd;
  13174. sd = script_rid2sd(st);
  13175. if( sd == NULL )
  13176. return 0;
  13177. mail_openmail(sd);
  13178. return 0;
  13179. }
  13180. BUILDIN_FUNC(openauction)
  13181. {
  13182. TBL_PC* sd;
  13183. sd = script_rid2sd(st);
  13184. if( sd == NULL )
  13185. return 0;
  13186. clif_Auction_openwindow(sd);
  13187. return 0;
  13188. }
  13189. /// Retrieves the value of the specified flag of the specified cell.
  13190. ///
  13191. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  13192. ///
  13193. /// @see cell_chk* constants in const.txt for the types
  13194. BUILDIN_FUNC(checkcell)
  13195. {
  13196. int m = map_mapname2mapid(script_getstr(st,2));
  13197. int x = script_getnum(st,3);
  13198. int y = script_getnum(st,4);
  13199. cell_chk type = (cell_chk)script_getnum(st,5);
  13200. script_pushint(st, map_getcell(m, x, y, type));
  13201. return 0;
  13202. }
  13203. /// Modifies flags of cells in the specified area.
  13204. ///
  13205. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  13206. ///
  13207. /// @see cell_* constants in const.txt for the types
  13208. BUILDIN_FUNC(setcell)
  13209. {
  13210. int m = map_mapname2mapid(script_getstr(st,2));
  13211. int x1 = script_getnum(st,3);
  13212. int y1 = script_getnum(st,4);
  13213. int x2 = script_getnum(st,5);
  13214. int y2 = script_getnum(st,6);
  13215. cell_t type = (cell_t)script_getnum(st,7);
  13216. bool flag = (bool)script_getnum(st,8);
  13217. int x,y;
  13218. if( x1 > x2 ) swap(x1,x2);
  13219. if( y1 > y2 ) swap(y1,y2);
  13220. for( y = y1; y <= y2; ++y )
  13221. for( x = x1; x <= x2; ++x )
  13222. map_setcell(m, x, y, type, flag);
  13223. return 0;
  13224. }
  13225. /*==========================================
  13226. * Mercenary Commands
  13227. *------------------------------------------*/
  13228. BUILDIN_FUNC(mercenary_create)
  13229. {
  13230. struct map_session_data *sd;
  13231. int class_, contract_time;
  13232. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  13233. return 0;
  13234. class_ = script_getnum(st,2);
  13235. if( !merc_class(class_) )
  13236. return 0;
  13237. contract_time = script_getnum(st,3);
  13238. merc_create(sd, class_, contract_time);
  13239. return 0;
  13240. }
  13241. BUILDIN_FUNC(mercenary_heal)
  13242. {
  13243. struct map_session_data *sd = script_rid2sd(st);
  13244. int hp, sp;
  13245. if( sd == NULL || sd->md == NULL )
  13246. return 0;
  13247. hp = script_getnum(st,2);
  13248. sp = script_getnum(st,3);
  13249. status_heal(&sd->md->bl, hp, sp, 0);
  13250. return 0;
  13251. }
  13252. BUILDIN_FUNC(mercenary_sc_start)
  13253. {
  13254. struct map_session_data *sd = script_rid2sd(st);
  13255. enum sc_type type;
  13256. int tick, val1;
  13257. if( sd == NULL || sd->md == NULL )
  13258. return 0;
  13259. type = (sc_type)script_getnum(st,2);
  13260. tick = script_getnum(st,3);
  13261. val1 = script_getnum(st,4);
  13262. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  13263. return 0;
  13264. }
  13265. BUILDIN_FUNC(mercenary_get_calls)
  13266. {
  13267. struct map_session_data *sd = script_rid2sd(st);
  13268. int guild;
  13269. if( sd == NULL )
  13270. return 0;
  13271. guild = script_getnum(st,2);
  13272. switch( guild )
  13273. {
  13274. case ARCH_MERC_GUILD:
  13275. script_pushint(st,sd->status.arch_calls);
  13276. break;
  13277. case SPEAR_MERC_GUILD:
  13278. script_pushint(st,sd->status.spear_calls);
  13279. break;
  13280. case SWORD_MERC_GUILD:
  13281. script_pushint(st,sd->status.sword_calls);
  13282. break;
  13283. default:
  13284. script_pushint(st,0);
  13285. break;
  13286. }
  13287. return 0;
  13288. }
  13289. BUILDIN_FUNC(mercenary_set_calls)
  13290. {
  13291. struct map_session_data *sd = script_rid2sd(st);
  13292. int guild, value, *calls;
  13293. if( sd == NULL )
  13294. return 0;
  13295. guild = script_getnum(st,2);
  13296. value = script_getnum(st,3);
  13297. switch( guild )
  13298. {
  13299. case ARCH_MERC_GUILD:
  13300. calls = &sd->status.arch_calls;
  13301. break;
  13302. case SPEAR_MERC_GUILD:
  13303. calls = &sd->status.spear_calls;
  13304. break;
  13305. case SWORD_MERC_GUILD:
  13306. calls = &sd->status.sword_calls;
  13307. break;
  13308. default:
  13309. return 0; // Invalid Guild
  13310. }
  13311. *calls += value;
  13312. *calls = cap_value(*calls, 0, INT_MAX);
  13313. return 0;
  13314. }
  13315. BUILDIN_FUNC(mercenary_get_faith)
  13316. {
  13317. struct map_session_data *sd = script_rid2sd(st);
  13318. int guild;
  13319. if( sd == NULL )
  13320. return 0;
  13321. guild = script_getnum(st,2);
  13322. switch( guild )
  13323. {
  13324. case ARCH_MERC_GUILD:
  13325. script_pushint(st,sd->status.arch_faith);
  13326. break;
  13327. case SPEAR_MERC_GUILD:
  13328. script_pushint(st,sd->status.spear_faith);
  13329. break;
  13330. case SWORD_MERC_GUILD:
  13331. script_pushint(st,sd->status.sword_faith);
  13332. break;
  13333. default:
  13334. script_pushint(st,0);
  13335. break;
  13336. }
  13337. return 0;
  13338. }
  13339. BUILDIN_FUNC(mercenary_set_faith)
  13340. {
  13341. struct map_session_data *sd = script_rid2sd(st);
  13342. int guild, value, *calls;
  13343. if( sd == NULL )
  13344. return 0;
  13345. guild = script_getnum(st,2);
  13346. value = script_getnum(st,3);
  13347. switch( guild )
  13348. {
  13349. case ARCH_MERC_GUILD:
  13350. calls = &sd->status.arch_faith;
  13351. break;
  13352. case SPEAR_MERC_GUILD:
  13353. calls = &sd->status.spear_faith;
  13354. break;
  13355. case SWORD_MERC_GUILD:
  13356. calls = &sd->status.sword_faith;
  13357. break;
  13358. default:
  13359. return 0; // Invalid Guild
  13360. }
  13361. *calls += value;
  13362. *calls = cap_value(*calls, 0, INT_MAX);
  13363. if( mercenary_get_guild(sd->md) == guild )
  13364. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  13365. return 0;
  13366. }
  13367. /*------------------------------------------
  13368. * Book Reading
  13369. *------------------------------------------*/
  13370. BUILDIN_FUNC(readbook)
  13371. {
  13372. struct map_session_data *sd;
  13373. int book_id, page;
  13374. if( (sd = script_rid2sd(st)) == NULL )
  13375. return 0;
  13376. book_id = script_getnum(st,2);
  13377. page = script_getnum(st,3);
  13378. clif_readbook(sd->fd, book_id, page);
  13379. return 0;
  13380. }
  13381. /******************
  13382. Questlog script commands
  13383. *******************/
  13384. BUILDIN_FUNC(setquest)
  13385. {
  13386. TBL_PC * sd = script_rid2sd(st);
  13387. quest_add(sd, script_getnum(st, 2));
  13388. return 0;
  13389. }
  13390. BUILDIN_FUNC(erasequest)
  13391. {
  13392. TBL_PC * sd = script_rid2sd(st);
  13393. quest_delete(sd, script_getnum(st, 2));
  13394. return 0;
  13395. }
  13396. BUILDIN_FUNC(completequest)
  13397. {
  13398. TBL_PC * sd = script_rid2sd(st);
  13399. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  13400. return 0;
  13401. }
  13402. BUILDIN_FUNC(changequest)
  13403. {
  13404. TBL_PC * sd = script_rid2sd(st);
  13405. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  13406. return 0;
  13407. }
  13408. BUILDIN_FUNC(checkquest)
  13409. {
  13410. TBL_PC * sd = script_rid2sd(st);
  13411. quest_check_type type = HAVEQUEST;
  13412. if( script_hasdata(st, 3) )
  13413. type = (quest_check_type)script_getnum(st, 3);
  13414. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  13415. return 0;
  13416. }
  13417. BUILDIN_FUNC(showevent)
  13418. {
  13419. TBL_PC *sd = script_rid2sd(st);
  13420. struct npc_data *nd = map_id2nd(st->oid);
  13421. int state, color;
  13422. if( sd == NULL || nd == NULL )
  13423. return 0;
  13424. state = script_getnum(st, 2);
  13425. color = script_getnum(st, 3);
  13426. if( color < 0 || color > 3 )
  13427. color = 0; // set default color
  13428. clif_quest_show_event(sd, &nd->bl, state, color);
  13429. return 0;
  13430. }
  13431. /*==========================================
  13432. * BattleGround System
  13433. *------------------------------------------*/
  13434. BUILDIN_FUNC(waitingroom2bg)
  13435. {
  13436. struct npc_data *nd;
  13437. struct chat_data *cd;
  13438. const char *map_name, *ev = "", *dev = "";
  13439. int x, y, i, mapindex = 0, bg_id, n;
  13440. struct map_session_data *sd;
  13441. if( script_hasdata(st,7) )
  13442. nd = npc_name2id(script_getstr(st,7));
  13443. else
  13444. nd = (struct npc_data *)map_id2bl(st->oid);
  13445. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  13446. {
  13447. script_pushint(st,0);
  13448. return 0;
  13449. }
  13450. map_name = script_getstr(st,2);
  13451. if( strcmp(map_name,"-") != 0 )
  13452. {
  13453. mapindex = mapindex_name2id(map_name);
  13454. if( mapindex == 0 )
  13455. { // Invalid Map
  13456. script_pushint(st,0);
  13457. return 0;
  13458. }
  13459. }
  13460. x = script_getnum(st,3);
  13461. y = script_getnum(st,4);
  13462. ev = script_getstr(st,5); // Logout Event
  13463. dev = script_getstr(st,6); // Die Event
  13464. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  13465. { // Creation failed
  13466. script_pushint(st,0);
  13467. return 0;
  13468. }
  13469. n = cd->users;
  13470. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  13471. {
  13472. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  13473. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  13474. else
  13475. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  13476. }
  13477. mapreg_setreg(add_str("$@arenamembersnum"), i);
  13478. script_pushint(st,bg_id);
  13479. return 0;
  13480. }
  13481. BUILDIN_FUNC(waitingroom2bg_single)
  13482. {
  13483. const char* map_name;
  13484. struct npc_data *nd;
  13485. struct chat_data *cd;
  13486. struct map_session_data *sd;
  13487. int x, y, mapindex, bg_id;
  13488. bg_id = script_getnum(st,2);
  13489. map_name = script_getstr(st,3);
  13490. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13491. return 0; // Invalid Map
  13492. x = script_getnum(st,4);
  13493. y = script_getnum(st,5);
  13494. nd = npc_name2id(script_getstr(st,6));
  13495. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  13496. return 0;
  13497. if( (sd = cd->usersd[0]) == NULL )
  13498. return 0;
  13499. if( bg_team_join(bg_id, sd) )
  13500. {
  13501. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  13502. script_pushint(st,1);
  13503. }
  13504. else
  13505. script_pushint(st,0);
  13506. return 0;
  13507. }
  13508. BUILDIN_FUNC(bg_team_setxy)
  13509. {
  13510. struct battleground_data *bg;
  13511. int bg_id;
  13512. bg_id = script_getnum(st,2);
  13513. if( (bg = bg_team_search(bg_id)) == NULL )
  13514. return 0;
  13515. bg->x = script_getnum(st,3);
  13516. bg->y = script_getnum(st,4);
  13517. return 0;
  13518. }
  13519. BUILDIN_FUNC(bg_warp)
  13520. {
  13521. int x, y, mapindex, bg_id;
  13522. const char* map_name;
  13523. bg_id = script_getnum(st,2);
  13524. map_name = script_getstr(st,3);
  13525. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13526. return 0; // Invalid Map
  13527. x = script_getnum(st,4);
  13528. y = script_getnum(st,5);
  13529. bg_team_warp(bg_id, mapindex, x, y);
  13530. return 0;
  13531. }
  13532. BUILDIN_FUNC(bg_monster)
  13533. {
  13534. int class_ = 0, x = 0, y = 0, bg_id = 0;
  13535. const char *str,*map, *evt="";
  13536. bg_id = script_getnum(st,2);
  13537. map = script_getstr(st,3);
  13538. x = script_getnum(st,4);
  13539. y = script_getnum(st,5);
  13540. str = script_getstr(st,6);
  13541. class_ = script_getnum(st,7);
  13542. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  13543. check_event(st, evt);
  13544. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  13545. return 0;
  13546. }
  13547. BUILDIN_FUNC(bg_monster_set_team)
  13548. {
  13549. struct mob_data *md;
  13550. struct block_list *mbl;
  13551. int id = script_getnum(st,2),
  13552. bg_id = script_getnum(st,3);
  13553. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  13554. return 0;
  13555. md = (TBL_MOB *)mbl;
  13556. md->bg_id = bg_id;
  13557. mob_stop_attack(md);
  13558. mob_stop_walking(md, 0);
  13559. md->target_id = md->attacked_id = 0;
  13560. clif_charnameack(0, &md->bl);
  13561. return 0;
  13562. }
  13563. BUILDIN_FUNC(bg_leave)
  13564. {
  13565. struct map_session_data *sd = script_rid2sd(st);
  13566. if( sd == NULL || !sd->bg_id )
  13567. return 0;
  13568. bg_team_leave(sd,0);
  13569. return 0;
  13570. }
  13571. BUILDIN_FUNC(bg_destroy)
  13572. {
  13573. int bg_id = script_getnum(st,2);
  13574. bg_team_delete(bg_id);
  13575. return 0;
  13576. }
  13577. BUILDIN_FUNC(bg_getareausers)
  13578. {
  13579. const char *str;
  13580. int m, x0, y0, x1, y1, bg_id;
  13581. int i = 0, c = 0;
  13582. struct battleground_data *bg = NULL;
  13583. struct map_session_data *sd;
  13584. bg_id = script_getnum(st,2);
  13585. str = script_getstr(st,3);
  13586. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  13587. {
  13588. script_pushint(st,0);
  13589. return 0;
  13590. }
  13591. x0 = script_getnum(st,4);
  13592. y0 = script_getnum(st,5);
  13593. x1 = script_getnum(st,6);
  13594. y1 = script_getnum(st,7);
  13595. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  13596. {
  13597. if( (sd = bg->members[i].sd) == NULL )
  13598. continue;
  13599. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  13600. continue;
  13601. c++;
  13602. }
  13603. script_pushint(st,c);
  13604. return 0;
  13605. }
  13606. BUILDIN_FUNC(bg_updatescore)
  13607. {
  13608. const char *str;
  13609. int m;
  13610. str = script_getstr(st,2);
  13611. if( (m = map_mapname2mapid(str)) < 0 )
  13612. return 0;
  13613. map[m].bgscore_lion = script_getnum(st,3);
  13614. map[m].bgscore_eagle = script_getnum(st,4);
  13615. clif_bg_updatescore(m);
  13616. return 0;
  13617. }
  13618. BUILDIN_FUNC(bg_get_data)
  13619. {
  13620. struct battleground_data *bg;
  13621. int bg_id = script_getnum(st,2),
  13622. type = script_getnum(st,3);
  13623. if( (bg = bg_team_search(bg_id)) == NULL )
  13624. {
  13625. script_pushint(st,0);
  13626. return 0;
  13627. }
  13628. switch( type )
  13629. {
  13630. case 0: script_pushint(st, bg->count); break;
  13631. default:
  13632. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  13633. break;
  13634. }
  13635. return 0;
  13636. }
  13637. /*==========================================
  13638. * Instancing Script Commands
  13639. *------------------------------------------*/
  13640. BUILDIN_FUNC(instance_create)
  13641. {
  13642. const char *name;
  13643. int party_id, res;
  13644. name = script_getstr(st, 2);
  13645. party_id = script_getnum(st, 3);
  13646. res = instance_create(party_id, name);
  13647. if( res == -4 ) // Already exists
  13648. {
  13649. script_pushint(st, -1);
  13650. return 0;
  13651. }
  13652. else if( res < 0 )
  13653. {
  13654. const char *err;
  13655. switch(res)
  13656. {
  13657. case -3: err = "No free instances"; break;
  13658. case -2: err = "Invalid party ID"; break;
  13659. case -1: err = "Invalid type"; break;
  13660. default: err = "Unknown"; break;
  13661. }
  13662. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  13663. script_pushint(st, -2);
  13664. return 0;
  13665. }
  13666. script_pushint(st, res);
  13667. return 0;
  13668. }
  13669. BUILDIN_FUNC(instance_destroy)
  13670. {
  13671. int instance_id;
  13672. struct map_session_data *sd;
  13673. struct party_data *p;
  13674. if( script_hasdata(st, 2) )
  13675. instance_id = script_getnum(st, 2);
  13676. else if( st->instance_id )
  13677. instance_id = st->instance_id;
  13678. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13679. instance_id = p->instance_id;
  13680. else return 0;
  13681. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13682. {
  13683. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  13684. return 0;
  13685. }
  13686. instance_destroy(instance_id);
  13687. return 0;
  13688. }
  13689. BUILDIN_FUNC(instance_attachmap)
  13690. {
  13691. const char *name;
  13692. int m;
  13693. int instance_id;
  13694. bool usebasename = false;
  13695. name = script_getstr(st,2);
  13696. instance_id = script_getnum(st,3);
  13697. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  13698. usebasename = true;
  13699. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  13700. {
  13701. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  13702. script_pushconststr(st, "");
  13703. return 0;
  13704. }
  13705. script_pushconststr(st, map[m].name);
  13706. return 0;
  13707. }
  13708. BUILDIN_FUNC(instance_detachmap)
  13709. {
  13710. struct map_session_data *sd;
  13711. struct party_data *p;
  13712. const char *str;
  13713. int m, instance_id;
  13714. str = script_getstr(st, 2);
  13715. if( script_hasdata(st, 3) )
  13716. instance_id = script_getnum(st, 3);
  13717. else if( st->instance_id )
  13718. instance_id = st->instance_id;
  13719. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13720. instance_id = p->instance_id;
  13721. else return 0;
  13722. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  13723. {
  13724. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  13725. return 0;
  13726. }
  13727. instance_del_map(m);
  13728. return 0;
  13729. }
  13730. BUILDIN_FUNC(instance_attach)
  13731. {
  13732. int instance_id;
  13733. instance_id = script_getnum(st, 2);
  13734. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13735. return 0;
  13736. st->instance_id = instance_id;
  13737. return 0;
  13738. }
  13739. BUILDIN_FUNC(instance_id)
  13740. {
  13741. int type, instance_id;
  13742. struct map_session_data *sd;
  13743. struct party_data *p;
  13744. if( script_hasdata(st, 2) )
  13745. {
  13746. type = script_getnum(st, 2);
  13747. if( type == 0 )
  13748. instance_id = st->instance_id;
  13749. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  13750. instance_id = p->instance_id;
  13751. else
  13752. instance_id = 0;
  13753. }
  13754. else
  13755. instance_id = st->instance_id;
  13756. script_pushint(st, instance_id);
  13757. return 0;
  13758. }
  13759. BUILDIN_FUNC(instance_set_timeout)
  13760. {
  13761. int progress_timeout, idle_timeout;
  13762. int instance_id;
  13763. struct map_session_data *sd;
  13764. struct party_data *p;
  13765. progress_timeout = script_getnum(st, 2);
  13766. idle_timeout = script_getnum(st, 3);
  13767. if( script_hasdata(st, 4) )
  13768. instance_id = script_getnum(st, 4);
  13769. else if( st->instance_id )
  13770. instance_id = st->instance_id;
  13771. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13772. instance_id = p->instance_id;
  13773. else return 0;
  13774. if( instance_id > 0 )
  13775. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  13776. return 0;
  13777. }
  13778. BUILDIN_FUNC(instance_init)
  13779. {
  13780. int instance_id = script_getnum(st, 2);
  13781. if( instance[instance_id].state != INSTANCE_IDLE )
  13782. {
  13783. ShowError("instance_init: instance already initialized.\n");
  13784. return 0;
  13785. }
  13786. instance_init(instance_id);
  13787. return 0;
  13788. }
  13789. BUILDIN_FUNC(instance_announce)
  13790. {
  13791. int instance_id = script_getnum(st,2);
  13792. const char *mes = script_getstr(st,3);
  13793. int flag = script_getnum(st,4);
  13794. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  13795. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  13796. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  13797. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  13798. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  13799. int i;
  13800. struct map_session_data *sd;
  13801. struct party_data *p;
  13802. if( instance_id == 0 )
  13803. {
  13804. if( st->instance_id )
  13805. instance_id = st->instance_id;
  13806. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13807. instance_id = p->instance_id;
  13808. else return 0;
  13809. }
  13810. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13811. return 0;
  13812. for( i = 0; i < instance[instance_id].num_map; i++ )
  13813. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  13814. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  13815. return 0;
  13816. }
  13817. BUILDIN_FUNC(instance_npcname)
  13818. {
  13819. const char *str;
  13820. int instance_id = 0;
  13821. struct map_session_data *sd;
  13822. struct party_data *p;
  13823. struct npc_data *nd;
  13824. str = script_getstr(st, 2);
  13825. if( script_hasdata(st, 3) )
  13826. instance_id = script_getnum(st, 3);
  13827. else if( st->instance_id )
  13828. instance_id = st->instance_id;
  13829. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13830. instance_id = p->instance_id;
  13831. if( instance_id && (nd = npc_name2id(str)) != NULL )
  13832. {
  13833. static char npcname[NAME_LENGTH];
  13834. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  13835. script_pushconststr(st,npcname);
  13836. }
  13837. else
  13838. {
  13839. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  13840. st->state = END;
  13841. return 1;
  13842. }
  13843. return 0;
  13844. }
  13845. BUILDIN_FUNC(has_instance)
  13846. {
  13847. struct map_session_data *sd;
  13848. struct party_data *p;
  13849. const char *str;
  13850. int m, instance_id = 0;
  13851. str = script_getstr(st, 2);
  13852. if( script_hasdata(st, 3) )
  13853. instance_id = script_getnum(st, 3);
  13854. else if( st->instance_id )
  13855. instance_id = st->instance_id;
  13856. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13857. instance_id = p->instance_id;
  13858. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  13859. {
  13860. script_pushconststr(st, "");
  13861. return 0;
  13862. }
  13863. script_pushconststr(st, map[m].name);
  13864. return 0;
  13865. }
  13866. BUILDIN_FUNC(instance_warpall)
  13867. {
  13868. struct map_session_data *pl_sd;
  13869. int m, i, instance_id;
  13870. const char *mapn;
  13871. int x, y;
  13872. unsigned short mapindex;
  13873. struct party_data *p = NULL;
  13874. mapn = script_getstr(st,2);
  13875. x = script_getnum(st,3);
  13876. y = script_getnum(st,4);
  13877. if( script_hasdata(st,5) )
  13878. instance_id = script_getnum(st,5);
  13879. else if( st->instance_id )
  13880. instance_id = st->instance_id;
  13881. else if( (pl_sd = script_rid2sd(st)) != NULL && pl_sd->status.party_id && (p = party_search(pl_sd->status.party_id)) != NULL && p->instance_id )
  13882. instance_id = p->instance_id;
  13883. else return 0;
  13884. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  13885. return 0;
  13886. if( !(p = party_search(instance[instance_id].party_id)) )
  13887. return 0;
  13888. mapindex = map_id2index(m);
  13889. for( i = 0; i < MAX_PARTY; i++ )
  13890. if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == st->instance_id ) pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  13891. return 0;
  13892. }
  13893. /*==========================================
  13894. * instance_check_party [malufett]
  13895. * Values:
  13896. * party_id : Party ID of the invoking character. [Required Parameter]
  13897. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  13898. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  13899. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  13900. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  13901. * Example 2: instance_check_party (getcharid(1),1,1,99);
  13902. *------------------------------------------*/
  13903. BUILDIN_FUNC(instance_check_party)
  13904. {
  13905. struct map_session_data *pl_sd;
  13906. int amount, min, max, i, party_id, c = 0;
  13907. struct party_data *p = NULL;
  13908. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  13909. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  13910. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  13911. if( min < 1 || min > MAX_LEVEL){
  13912. ShowError("instance_check_party: Invalid min level, %d\n", min);
  13913. return 0;
  13914. }else if( max < 1 || max > MAX_LEVEL){
  13915. ShowError("instance_check_party: Invalid max level, %d\n", max);
  13916. return 0;
  13917. }
  13918. if( script_hasdata(st,2) )
  13919. party_id = script_getnum(st,2);
  13920. else return 0;
  13921. if( !(p = party_search(party_id)) ){
  13922. script_pushint(st, 0); // Returns false if party does not exist.
  13923. return 0;
  13924. }
  13925. for( i = 0; i < MAX_PARTY; i++ )
  13926. if( (pl_sd = p->data[i].sd) )
  13927. if(map_id2bl(pl_sd->bl.id)){
  13928. if(pl_sd->status.base_level < min){
  13929. script_pushint(st, 0);
  13930. return 0;
  13931. }else if(pl_sd->status.base_level > max){
  13932. script_pushint(st, 0);
  13933. return 0;
  13934. }
  13935. c++;
  13936. }
  13937. if(c < amount){
  13938. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  13939. }else
  13940. script_pushint(st, 1);
  13941. return 0;
  13942. }
  13943. /*==========================================
  13944. * Custom Fonts
  13945. *------------------------------------------*/
  13946. BUILDIN_FUNC(setfont)
  13947. {
  13948. struct map_session_data *sd = script_rid2sd(st);
  13949. int font = script_getnum(st,2);
  13950. if( sd == NULL )
  13951. return 0;
  13952. if( sd->user_font != font )
  13953. sd->user_font = font;
  13954. else
  13955. sd->user_font = 0;
  13956. clif_font(sd);
  13957. return 0;
  13958. }
  13959. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  13960. {
  13961. TBL_MOB* md = (TBL_MOB*)bl;
  13962. struct block_list *tbl;
  13963. int mobid = va_arg(ap,int);
  13964. int skillid = va_arg(ap,int);
  13965. int skilllv = va_arg(ap,int);
  13966. int casttime = va_arg(ap,int);
  13967. int cancel = va_arg(ap,int);
  13968. int emotion = va_arg(ap,int);
  13969. int target = va_arg(ap,int);
  13970. if( md->class_ != mobid )
  13971. return 0;
  13972. // 0:self, 1:target, 2:master, default:random
  13973. switch( target )
  13974. {
  13975. case 0: tbl = map_id2bl(md->bl.id); break;
  13976. case 1: tbl = map_id2bl(md->target_id); break;
  13977. case 2: tbl = map_id2bl(md->master_id); break;
  13978. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  13979. }
  13980. if( !tbl )
  13981. return 0;
  13982. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  13983. unit_skillcastcancel(bl,0);
  13984. if( skill_get_casttype(skillid) == CAST_GROUND )
  13985. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  13986. else
  13987. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  13988. clif_emotion(&md->bl, emotion);
  13989. return 0;
  13990. }
  13991. /*==========================================
  13992. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  13993. *------------------------------------------*/
  13994. BUILDIN_FUNC(areamobuseskill)
  13995. {
  13996. struct block_list center;
  13997. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  13998. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  13999. {
  14000. ShowError("areamobuseskill: invalid map name.\n");
  14001. return 0;
  14002. }
  14003. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  14004. return 0;
  14005. center.m = m;
  14006. center.x = script_getnum(st,3);
  14007. center.y = script_getnum(st,4);
  14008. range = script_getnum(st,5);
  14009. mobid = script_getnum(st,6);
  14010. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  14011. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  14012. skilllv = battle_config.mob_max_skilllvl;
  14013. casttime = script_getnum(st,9);
  14014. cancel = script_getnum(st,10);
  14015. emotion = script_getnum(st,11);
  14016. target = script_getnum(st,12);
  14017. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  14018. return 0;
  14019. }
  14020. BUILDIN_FUNC(progressbar)
  14021. {
  14022. struct map_session_data * sd = script_rid2sd(st);
  14023. const char * color;
  14024. unsigned int second;
  14025. if( !st || !sd )
  14026. return 0;
  14027. st->state = STOP;
  14028. color = script_getstr(st,2);
  14029. second = script_getnum(st,3);
  14030. sd->progressbar.npc_id = st->oid;
  14031. sd->progressbar.timeout = gettick() + second*1000;
  14032. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  14033. return 0;
  14034. }
  14035. BUILDIN_FUNC(pushpc)
  14036. {
  14037. int direction, cells, dx, dy;
  14038. struct map_session_data* sd;
  14039. if((sd = script_rid2sd(st))==NULL)
  14040. {
  14041. return 0;
  14042. }
  14043. direction = script_getnum(st,2);
  14044. cells = script_getnum(st,3);
  14045. if(direction<0 || direction>7)
  14046. {
  14047. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  14048. script_reportsrc(st);
  14049. direction%= 8; // trim spin-over
  14050. }
  14051. if(!cells)
  14052. {// zero distance
  14053. return 0;
  14054. }
  14055. else if(cells<0)
  14056. {// pushing backwards
  14057. direction = (direction+4)%8; // turn around
  14058. cells = -cells;
  14059. }
  14060. dx = dirx[direction];
  14061. dy = diry[direction];
  14062. unit_blown(&sd->bl, dx, dy, cells, 0);
  14063. return 0;
  14064. }
  14065. /// Invokes buying store preparation window
  14066. /// buyingstore <slots>;
  14067. BUILDIN_FUNC(buyingstore)
  14068. {
  14069. struct map_session_data* sd;
  14070. if( ( sd = script_rid2sd(st) ) == NULL )
  14071. {
  14072. return 0;
  14073. }
  14074. buyingstore_setup(sd, script_getnum(st,2));
  14075. return 0;
  14076. }
  14077. /// Invokes search store info window
  14078. /// searchstores <uses>,<effect>;
  14079. BUILDIN_FUNC(searchstores)
  14080. {
  14081. unsigned short effect;
  14082. unsigned int uses;
  14083. struct map_session_data* sd;
  14084. if( ( sd = script_rid2sd(st) ) == NULL )
  14085. {
  14086. return 0;
  14087. }
  14088. uses = script_getnum(st,2);
  14089. effect = script_getnum(st,3);
  14090. if( !uses )
  14091. {
  14092. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  14093. return 1;
  14094. }
  14095. if( effect > 1 )
  14096. {
  14097. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  14098. return 1;
  14099. }
  14100. searchstore_open(sd, uses, effect);
  14101. return 0;
  14102. }
  14103. /// Displays a number as large digital clock.
  14104. /// showdigit <value>[,<type>];
  14105. BUILDIN_FUNC(showdigit)
  14106. {
  14107. unsigned int type = 0;
  14108. int value;
  14109. struct map_session_data* sd;
  14110. if( ( sd = script_rid2sd(st) ) == NULL )
  14111. {
  14112. return 0;
  14113. }
  14114. value = script_getnum(st,2);
  14115. if( script_hasdata(st,3) )
  14116. {
  14117. type = script_getnum(st,3);
  14118. if( type > 3 )
  14119. {
  14120. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  14121. return 1;
  14122. }
  14123. }
  14124. clif_showdigit(sd, (unsigned char)type, value);
  14125. return 0;
  14126. }
  14127. /**
  14128. * Rune Knight
  14129. **/
  14130. BUILDIN_FUNC(makerune) {
  14131. TBL_PC* sd;
  14132. if( (sd = script_rid2sd(st)) == NULL )
  14133. return 0;
  14134. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  14135. sd->itemid = script_getnum(st,2);
  14136. return 0;
  14137. }
  14138. /**
  14139. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  14140. **/
  14141. BUILDIN_FUNC(checkdragon) {
  14142. TBL_PC* sd;
  14143. if( (sd = script_rid2sd(st)) == NULL )
  14144. return 0;
  14145. if( pc_isridingdragon(sd) )
  14146. script_pushint(st,1);
  14147. else
  14148. script_pushint(st,0);
  14149. return 0;
  14150. }
  14151. /**
  14152. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  14153. * - Toggles the dragon on a RK if he can mount;
  14154. * @param Color - when not provided uses the green dragon;
  14155. * - 1 : Green Dragon
  14156. * - 2 : Brown Dragon
  14157. * - 3 : Gray Dragon
  14158. * - 4 : Blue Dragon
  14159. * - 5 : Red Dragon
  14160. **/
  14161. BUILDIN_FUNC(setdragon) {
  14162. TBL_PC* sd;
  14163. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  14164. unsigned int option = OPTION_DRAGON1;
  14165. if( (sd = script_rid2sd(st)) == NULL )
  14166. return 0;
  14167. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  14168. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  14169. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  14170. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  14171. script_pushint(st,1);
  14172. } else {//Not mounted; Mount now.
  14173. if( color ) {
  14174. option = ( color == 1 ? OPTION_DRAGON1 :
  14175. color == 2 ? OPTION_DRAGON2 :
  14176. color == 3 ? OPTION_DRAGON3 :
  14177. color == 4 ? OPTION_DRAGON4 :
  14178. color == 5 ? OPTION_DRAGON5 : 0);
  14179. if( !option ) {
  14180. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  14181. option = OPTION_DRAGON1;
  14182. }
  14183. }
  14184. pc_setoption(sd, sd->sc.option|option);
  14185. script_pushint(st,1);
  14186. }
  14187. return 0;
  14188. }
  14189. /**
  14190. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  14191. **/
  14192. BUILDIN_FUNC(ismounting) {
  14193. TBL_PC* sd;
  14194. if( (sd = script_rid2sd(st)) == NULL )
  14195. return 0;
  14196. if( sd->sc.option&OPTION_MOUNTING )
  14197. script_pushint(st,1);
  14198. else
  14199. script_pushint(st,0);
  14200. return 0;
  14201. }
  14202. /**
  14203. * setmounting() returns 1 on success or 0 otherwise
  14204. * - Toggles new mounts on a player when he can mount
  14205. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  14206. * - Will unmount the player is he is already mounting
  14207. **/
  14208. BUILDIN_FUNC(setmounting) {
  14209. TBL_PC* sd;
  14210. if( (sd = script_rid2sd(st)) == NULL )
  14211. return 0;
  14212. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  14213. script_pushint(st,0);//can't mount with one of these
  14214. else {
  14215. if( sd->sc.option&OPTION_MOUNTING )
  14216. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  14217. else
  14218. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  14219. script_pushint(st,1);//in both cases, return 1.
  14220. }
  14221. return 0;
  14222. }
  14223. /**
  14224. * Retrieves quantity of arguments provided to callfunc/callsub.
  14225. * getargcount() -> amount of arguments received in a function
  14226. **/
  14227. BUILDIN_FUNC(getargcount) {
  14228. struct script_retinfo* ri;
  14229. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  14230. ShowError("script:getargcount: used out of function or callsub label!\n");
  14231. st->state = END;
  14232. return 1;
  14233. }
  14234. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  14235. script_pushint(st, ri->nargs);
  14236. return 0;
  14237. }
  14238. /**
  14239. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  14240. **/
  14241. BUILDIN_FUNC(is_function) {
  14242. const char* str = script_getstr(st,2);
  14243. if( strdb_exists(userfunc_db, str) )
  14244. script_pushint(st,1);
  14245. else
  14246. script_pushint(st,0);
  14247. return 0;
  14248. }
  14249. /**
  14250. * get_revision() -> retrieves the current svn revision (if available)
  14251. **/
  14252. BUILDIN_FUNC(get_revision) {
  14253. const char * revision;
  14254. if ( (revision = get_svn_revision()) != 0 )
  14255. script_pushint(st,atoi(revision));
  14256. else
  14257. script_pushint(st,-1);//unknown
  14258. return 0;
  14259. }
  14260. /**
  14261. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  14262. **/
  14263. BUILDIN_FUNC(freeloop) {
  14264. if( script_getnum(st,2) )
  14265. st->freeloop = 1;
  14266. else
  14267. st->freeloop = 0;
  14268. script_pushint(st, st->freeloop);
  14269. return 0;
  14270. }
  14271. /**
  14272. * @commands (script based)
  14273. **/
  14274. BUILDIN_FUNC(bindatcmd)
  14275. {
  14276. const char* atcmd;
  14277. const char* eventName;
  14278. int i = 0, level = 0, level2 = 0;
  14279. atcmd = script_getstr(st,2);
  14280. eventName = script_getstr(st,3);
  14281. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  14282. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  14283. // check if event is already binded
  14284. ARR_FIND(0, MAX_ATCMD_BINDINGS, i, strcmp(atcmd_binding[i].command,atcmd) == 0);
  14285. if( i < MAX_ATCMD_BINDINGS )
  14286. {
  14287. safestrncpy(atcmd_binding[i].npc_event, eventName, 50);
  14288. atcmd_binding[i].level = level;
  14289. atcmd_binding[i].level2 = level2;
  14290. }
  14291. else
  14292. { // make new binding
  14293. ARR_FIND(0, MAX_ATCMD_BINDINGS, i, atcmd_binding[i].command[0] == '\0');
  14294. if( i < MAX_ATCMD_BINDINGS )
  14295. {
  14296. safestrncpy(atcmd_binding[i].command, atcmd, 50);
  14297. safestrncpy(atcmd_binding[i].npc_event, eventName, 50);
  14298. atcmd_binding[i].level = level;
  14299. atcmd_binding[i].level2 = level2;
  14300. }
  14301. }
  14302. return 0;
  14303. }
  14304. BUILDIN_FUNC(unbindatcmd)
  14305. {
  14306. const char* atcmd;
  14307. int i = 0;
  14308. atcmd = script_getstr(st, 2);
  14309. ARR_FIND(0, MAX_ATCMD_BINDINGS, i, strcmp(atcmd_binding[i].command, atcmd) == 0);
  14310. if( i < MAX_ATCMD_BINDINGS )
  14311. memset(&atcmd_binding[i],0,sizeof(atcmd_binding[0]));
  14312. return 0;
  14313. }
  14314. BUILDIN_FUNC(useatcmd)
  14315. {
  14316. TBL_PC dummy_sd;
  14317. TBL_PC* sd;
  14318. int fd;
  14319. const char* cmd;
  14320. cmd = script_getstr(st,2);
  14321. if( st->rid )
  14322. {
  14323. sd = script_rid2sd(st);
  14324. fd = sd->fd;
  14325. }
  14326. else
  14327. { // Use a dummy character.
  14328. sd = &dummy_sd;
  14329. fd = 0;
  14330. memset(&dummy_sd, 0, sizeof(TBL_PC));
  14331. if( st->oid )
  14332. {
  14333. struct block_list* bl = map_id2bl(st->oid);
  14334. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  14335. if( bl->type == BL_NPC )
  14336. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  14337. }
  14338. }
  14339. // compatibility with previous implementation (deprecated!)
  14340. if( cmd[0] != atcommand_symbol )
  14341. {
  14342. cmd += strlen(sd->status.name);
  14343. while( *cmd != atcommand_symbol && *cmd != 0 )
  14344. cmd++;
  14345. }
  14346. is_atcommand(fd, sd, cmd, 1);
  14347. return 0;
  14348. }
  14349. BUILDIN_FUNC(checkre)
  14350. {
  14351. int num;
  14352. num=script_getnum(st,2);
  14353. switch(num){
  14354. case 0:
  14355. #ifdef RENEWAL
  14356. script_pushint(st, 1);
  14357. #endif
  14358. break;
  14359. case 1:
  14360. #ifdef RENEWAL_CAST
  14361. script_pushint(st, 1);
  14362. #endif
  14363. break;
  14364. case 2:
  14365. #ifdef RENEWAL_DROP
  14366. script_pushint(st, 1);
  14367. #endif
  14368. break;
  14369. case 3:
  14370. #ifdef RENEWAL_EXP
  14371. script_pushint(st, 1);
  14372. #endif
  14373. break;
  14374. case 4:
  14375. #ifdef RENEWAL_LVDMG
  14376. script_pushint(st, 1);
  14377. #endif
  14378. break;
  14379. case 5:
  14380. #ifdef RENEWAL_CAST_VMIN
  14381. script_pushint(st, 1);
  14382. #endif
  14383. break;
  14384. case 6:
  14385. #ifdef RENEWAL_EDP
  14386. script_pushint(st, 1);
  14387. #endif
  14388. break;
  14389. case 7:
  14390. #ifdef RENEWAL_ASPD
  14391. script_pushint(st, 1);
  14392. #endif
  14393. break;
  14394. default:
  14395. ShowWarning("buildin_checkre: unknown parameter.\n");
  14396. break;
  14397. }
  14398. script_pushint(st, 0);
  14399. return 0;
  14400. }
  14401. // declarations that were supposed to be exported from npc_chat.c
  14402. #ifdef PCRE_SUPPORT
  14403. BUILDIN_FUNC(defpattern);
  14404. BUILDIN_FUNC(activatepset);
  14405. BUILDIN_FUNC(deactivatepset);
  14406. BUILDIN_FUNC(deletepset);
  14407. #endif
  14408. /// script command definitions
  14409. /// for an explanation on args, see add_buildin_func
  14410. struct script_function buildin_func[] = {
  14411. // NPC interaction
  14412. BUILDIN_DEF(mes,"s*"),
  14413. BUILDIN_DEF(next,""),
  14414. BUILDIN_DEF(close,""),
  14415. BUILDIN_DEF(close2,""),
  14416. BUILDIN_DEF(menu,"sl*"),
  14417. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  14418. BUILDIN_DEF(prompt,"s*"),
  14419. //
  14420. BUILDIN_DEF(goto,"l"),
  14421. BUILDIN_DEF(callsub,"l*"),
  14422. BUILDIN_DEF(callfunc,"s*"),
  14423. BUILDIN_DEF(return,"?"),
  14424. BUILDIN_DEF(getarg,"i?"),
  14425. BUILDIN_DEF(jobchange,"i?"),
  14426. BUILDIN_DEF(jobname,"i"),
  14427. BUILDIN_DEF(input,"r??"),
  14428. BUILDIN_DEF(warp,"sii"),
  14429. BUILDIN_DEF(areawarp,"siiiisii"),
  14430. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  14431. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  14432. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  14433. BUILDIN_DEF(setlook,"ii"),
  14434. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  14435. BUILDIN_DEF(set,"rv"),
  14436. BUILDIN_DEF(setarray,"rv*"),
  14437. BUILDIN_DEF(cleararray,"rvi"),
  14438. BUILDIN_DEF(copyarray,"rri"),
  14439. BUILDIN_DEF(getarraysize,"r"),
  14440. BUILDIN_DEF(deletearray,"r?"),
  14441. BUILDIN_DEF(getelementofarray,"ri"),
  14442. BUILDIN_DEF(getitem,"vi?"),
  14443. BUILDIN_DEF(rentitem,"vi"),
  14444. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  14445. BUILDIN_DEF(getnameditem,"vv"),
  14446. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  14447. BUILDIN_DEF(makeitem,"visii"),
  14448. BUILDIN_DEF(delitem,"vi?"),
  14449. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  14450. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  14451. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  14452. BUILDIN_DEF(cutin,"si"),
  14453. BUILDIN_DEF(viewpoint,"iiiii"),
  14454. BUILDIN_DEF(heal,"ii"),
  14455. BUILDIN_DEF(itemheal,"ii"),
  14456. BUILDIN_DEF(percentheal,"ii"),
  14457. BUILDIN_DEF(rand,"i?"),
  14458. BUILDIN_DEF(countitem,"v"),
  14459. BUILDIN_DEF(countitem2,"viiiiiii"),
  14460. BUILDIN_DEF(checkweight,"vi"),
  14461. BUILDIN_DEF(readparam,"i?"),
  14462. BUILDIN_DEF(getcharid,"i?"),
  14463. BUILDIN_DEF(getnpcid,"i?"),
  14464. BUILDIN_DEF(getpartyname,"i"),
  14465. BUILDIN_DEF(getpartymember,"i?"),
  14466. BUILDIN_DEF(getpartyleader,"i?"),
  14467. BUILDIN_DEF(getguildname,"i"),
  14468. BUILDIN_DEF(getguildmaster,"i"),
  14469. BUILDIN_DEF(getguildmasterid,"i"),
  14470. BUILDIN_DEF(strcharinfo,"i"),
  14471. BUILDIN_DEF(strnpcinfo,"i"),
  14472. BUILDIN_DEF(getequipid,"i"),
  14473. BUILDIN_DEF(getequipname,"i"),
  14474. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  14475. BUILDIN_DEF(repair,"i"), // [Valaris]
  14476. BUILDIN_DEF(getequipisequiped,"i"),
  14477. BUILDIN_DEF(getequipisenableref,"i"),
  14478. BUILDIN_DEF(getequipisidentify,"i"),
  14479. BUILDIN_DEF(getequiprefinerycnt,"i"),
  14480. BUILDIN_DEF(getequipweaponlv,"i"),
  14481. BUILDIN_DEF(getequippercentrefinery,"i"),
  14482. BUILDIN_DEF(successrefitem,"i"),
  14483. BUILDIN_DEF(failedrefitem,"i"),
  14484. BUILDIN_DEF(downrefitem,"i"),
  14485. BUILDIN_DEF(statusup,"i"),
  14486. BUILDIN_DEF(statusup2,"ii"),
  14487. BUILDIN_DEF(bonus,"iv"),
  14488. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  14489. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  14490. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  14491. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  14492. BUILDIN_DEF(autobonus,"sii??"),
  14493. BUILDIN_DEF(autobonus2,"sii??"),
  14494. BUILDIN_DEF(autobonus3,"siiv?"),
  14495. BUILDIN_DEF(skill,"vi?"),
  14496. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  14497. BUILDIN_DEF(guildskill,"vi"),
  14498. BUILDIN_DEF(getskilllv,"v"),
  14499. BUILDIN_DEF(getgdskilllv,"iv"),
  14500. BUILDIN_DEF(basicskillcheck,""),
  14501. BUILDIN_DEF(getgmlevel,""),
  14502. BUILDIN_DEF(getgroupid,""),
  14503. BUILDIN_DEF(end,""),
  14504. BUILDIN_DEF(checkoption,"i"),
  14505. BUILDIN_DEF(setoption,"i?"),
  14506. BUILDIN_DEF(setcart,"?"),
  14507. BUILDIN_DEF(checkcart,""),
  14508. BUILDIN_DEF(setfalcon,"?"),
  14509. BUILDIN_DEF(checkfalcon,""),
  14510. BUILDIN_DEF(setriding,"?"),
  14511. BUILDIN_DEF(checkriding,""),
  14512. BUILDIN_DEF(checkwug,""),
  14513. BUILDIN_DEF(checkmadogear,""),
  14514. BUILDIN_DEF(setmadogear,"?"),
  14515. BUILDIN_DEF2(savepoint,"save","sii"),
  14516. BUILDIN_DEF(savepoint,"sii"),
  14517. BUILDIN_DEF(gettimetick,"i"),
  14518. BUILDIN_DEF(gettime,"i"),
  14519. BUILDIN_DEF(gettimestr,"si"),
  14520. BUILDIN_DEF(openstorage,""),
  14521. BUILDIN_DEF(guildopenstorage,""),
  14522. BUILDIN_DEF(itemskill,"vi"),
  14523. BUILDIN_DEF(produce,"i"),
  14524. BUILDIN_DEF(cooking,"i"),
  14525. BUILDIN_DEF(monster,"siisii?"),
  14526. BUILDIN_DEF(getmobdrops,"i"),
  14527. BUILDIN_DEF(areamonster,"siiiisii?"),
  14528. BUILDIN_DEF(killmonster,"ss?"),
  14529. BUILDIN_DEF(killmonsterall,"s?"),
  14530. BUILDIN_DEF(clone,"siisi????"),
  14531. BUILDIN_DEF(doevent,"s"),
  14532. BUILDIN_DEF(donpcevent,"s"),
  14533. BUILDIN_DEF(cmdothernpc,"ss"),
  14534. BUILDIN_DEF(addtimer,"is"),
  14535. BUILDIN_DEF(deltimer,"s"),
  14536. BUILDIN_DEF(addtimercount,"si"),
  14537. BUILDIN_DEF(initnpctimer,"??"),
  14538. BUILDIN_DEF(stopnpctimer,"??"),
  14539. BUILDIN_DEF(startnpctimer,"??"),
  14540. BUILDIN_DEF(setnpctimer,"i?"),
  14541. BUILDIN_DEF(getnpctimer,"i?"),
  14542. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  14543. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  14544. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  14545. BUILDIN_DEF(announce,"si?????"),
  14546. BUILDIN_DEF(mapannounce,"ssi?????"),
  14547. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  14548. BUILDIN_DEF(getusers,"i"),
  14549. BUILDIN_DEF(getmapguildusers,"si"),
  14550. BUILDIN_DEF(getmapusers,"s"),
  14551. BUILDIN_DEF(getareausers,"siiii"),
  14552. BUILDIN_DEF(getareadropitem,"siiiiv"),
  14553. BUILDIN_DEF(enablenpc,"s"),
  14554. BUILDIN_DEF(disablenpc,"s"),
  14555. BUILDIN_DEF(hideoffnpc,"s"),
  14556. BUILDIN_DEF(hideonnpc,"s"),
  14557. BUILDIN_DEF(sc_start,"iii?"),
  14558. BUILDIN_DEF(sc_start2,"iiii?"),
  14559. BUILDIN_DEF(sc_start4,"iiiiii?"),
  14560. BUILDIN_DEF(sc_end,"i?"),
  14561. BUILDIN_DEF(getstatus, "i?"),
  14562. BUILDIN_DEF(getscrate,"ii?"),
  14563. BUILDIN_DEF(debugmes,"s"),
  14564. BUILDIN_DEF2(catchpet,"pet","i"),
  14565. BUILDIN_DEF2(birthpet,"bpet",""),
  14566. BUILDIN_DEF(resetlvl,"i"),
  14567. BUILDIN_DEF(resetstatus,""),
  14568. BUILDIN_DEF(resetskill,""),
  14569. BUILDIN_DEF(skillpointcount,""),
  14570. BUILDIN_DEF(changebase,"i?"),
  14571. BUILDIN_DEF(changesex,""),
  14572. BUILDIN_DEF(waitingroom,"si?????"),
  14573. BUILDIN_DEF(delwaitingroom,"?"),
  14574. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  14575. BUILDIN_DEF(enablewaitingroomevent,"?"),
  14576. BUILDIN_DEF(disablewaitingroomevent,"?"),
  14577. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  14578. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  14579. BUILDIN_DEF(getwaitingroomstate,"i?"),
  14580. BUILDIN_DEF(warpwaitingpc,"sii?"),
  14581. BUILDIN_DEF(attachrid,"i"),
  14582. BUILDIN_DEF(detachrid,""),
  14583. BUILDIN_DEF(isloggedin,"i?"),
  14584. BUILDIN_DEF(setmapflagnosave,"ssii"),
  14585. BUILDIN_DEF(getmapflag,"si"),
  14586. BUILDIN_DEF(setmapflag,"si?"),
  14587. BUILDIN_DEF(removemapflag,"si?"),
  14588. BUILDIN_DEF(pvpon,"s"),
  14589. BUILDIN_DEF(pvpoff,"s"),
  14590. BUILDIN_DEF(gvgon,"s"),
  14591. BUILDIN_DEF(gvgoff,"s"),
  14592. BUILDIN_DEF(emotion,"i??"),
  14593. BUILDIN_DEF(maprespawnguildid,"sii"),
  14594. BUILDIN_DEF(agitstart,""), // <Agit>
  14595. BUILDIN_DEF(agitend,""),
  14596. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  14597. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  14598. BUILDIN_DEF(getcastlename,"s"),
  14599. BUILDIN_DEF(getcastledata,"si"),
  14600. BUILDIN_DEF(setcastledata,"sii"),
  14601. BUILDIN_DEF(requestguildinfo,"i?"),
  14602. BUILDIN_DEF(getequipcardcnt,"i"),
  14603. BUILDIN_DEF(successremovecards,"i"),
  14604. BUILDIN_DEF(failedremovecards,"ii"),
  14605. BUILDIN_DEF(marriage,"s"),
  14606. BUILDIN_DEF2(wedding_effect,"wedding",""),
  14607. BUILDIN_DEF(divorce,""),
  14608. BUILDIN_DEF(ispartneron,""),
  14609. BUILDIN_DEF(getpartnerid,""),
  14610. BUILDIN_DEF(getchildid,""),
  14611. BUILDIN_DEF(getmotherid,""),
  14612. BUILDIN_DEF(getfatherid,""),
  14613. BUILDIN_DEF(warppartner,"sii"),
  14614. BUILDIN_DEF(getitemname,"v"),
  14615. BUILDIN_DEF(getitemslots,"i"),
  14616. BUILDIN_DEF(makepet,"i"),
  14617. BUILDIN_DEF(getexp,"ii"),
  14618. BUILDIN_DEF(getinventorylist,""),
  14619. BUILDIN_DEF(getskilllist,""),
  14620. BUILDIN_DEF(clearitem,""),
  14621. BUILDIN_DEF(classchange,"ii"),
  14622. BUILDIN_DEF(misceffect,"i"),
  14623. BUILDIN_DEF(playBGM,"s"),
  14624. BUILDIN_DEF(playBGMall,"s?????"),
  14625. BUILDIN_DEF(soundeffect,"si"),
  14626. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  14627. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  14628. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  14629. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  14630. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  14631. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  14632. BUILDIN_DEF(petloot,"i"), // [Valaris]
  14633. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  14634. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  14635. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  14636. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  14637. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  14638. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  14639. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  14640. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  14641. BUILDIN_DEF(nude,""), // nude command [Valaris]
  14642. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  14643. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  14644. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  14645. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  14646. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  14647. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  14648. BUILDIN_DEF(mobcount,"ss"),
  14649. BUILDIN_DEF(getlook,"i"),
  14650. BUILDIN_DEF(getsavepoint,"i"),
  14651. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  14652. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  14653. BUILDIN_DEF(npcstop,""), // [Valaris]
  14654. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  14655. BUILDIN_DEF(checkoption1,"i"),
  14656. BUILDIN_DEF(checkoption2,"i"),
  14657. BUILDIN_DEF(guildgetexp,"i"),
  14658. BUILDIN_DEF(guildchangegm,"is"),
  14659. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  14660. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  14661. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  14662. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  14663. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  14664. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  14665. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  14666. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  14667. BUILDIN_DEF(night,""), // sets the server to night time
  14668. BUILDIN_DEF(day,""), // sets the server to day time
  14669. #ifdef PCRE_SUPPORT
  14670. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  14671. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  14672. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  14673. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  14674. #endif
  14675. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  14676. BUILDIN_DEF(getusersname,""),
  14677. BUILDIN_DEF(recovery,""),
  14678. BUILDIN_DEF(getpetinfo,"i"),
  14679. BUILDIN_DEF(gethominfo,"i"),
  14680. BUILDIN_DEF(getmercinfo,"i?"),
  14681. BUILDIN_DEF(checkequipedcard,"i"),
  14682. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  14683. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  14684. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  14685. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  14686. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  14687. BUILDIN_DEF(charat,"si"),
  14688. BUILDIN_DEF(setchar,"ssi"),
  14689. BUILDIN_DEF(insertchar,"ssi"),
  14690. BUILDIN_DEF(delchar,"si"),
  14691. BUILDIN_DEF(strtoupper,"s"),
  14692. BUILDIN_DEF(strtolower,"s"),
  14693. BUILDIN_DEF(charisupper, "si"),
  14694. BUILDIN_DEF(charislower, "si"),
  14695. BUILDIN_DEF(substr,"sii"),
  14696. BUILDIN_DEF(explode, "rss"),
  14697. BUILDIN_DEF(implode, "r?"),
  14698. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  14699. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  14700. BUILDIN_DEF(strpos,"ss?"),
  14701. BUILDIN_DEF(replacestr,"sss??"),
  14702. BUILDIN_DEF(countstr,"ss?"),
  14703. BUILDIN_DEF(setnpcdisplay,"sv??"),
  14704. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  14705. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  14706. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  14707. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  14708. // [zBuffer] List of mathematics commands --->
  14709. BUILDIN_DEF(sqrt,"i"),
  14710. BUILDIN_DEF(pow,"ii"),
  14711. BUILDIN_DEF(distance,"iiii"),
  14712. // <--- [zBuffer] List of mathematics commands
  14713. BUILDIN_DEF(md5,"s"),
  14714. // [zBuffer] List of dynamic var commands --->
  14715. BUILDIN_DEF(getd,"s"),
  14716. BUILDIN_DEF(setd,"sv"),
  14717. // <--- [zBuffer] List of dynamic var commands
  14718. BUILDIN_DEF(petstat,"i"),
  14719. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  14720. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  14721. BUILDIN_DEF(npcshopadditem,"sii*"),
  14722. BUILDIN_DEF(npcshopdelitem,"si*"),
  14723. BUILDIN_DEF(npcshopattach,"s?"),
  14724. BUILDIN_DEF(equip,"i"),
  14725. BUILDIN_DEF(autoequip,"ii"),
  14726. BUILDIN_DEF(setbattleflag,"si"),
  14727. BUILDIN_DEF(getbattleflag,"s"),
  14728. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  14729. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  14730. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  14731. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  14732. BUILDIN_DEF(axtoi,"s"),
  14733. BUILDIN_DEF(query_sql,"s*"),
  14734. BUILDIN_DEF(query_logsql,"s*"),
  14735. BUILDIN_DEF(escape_sql,"v"),
  14736. BUILDIN_DEF(atoi,"s"),
  14737. // [zBuffer] List of player cont commands --->
  14738. BUILDIN_DEF(rid2name,"i"),
  14739. BUILDIN_DEF(pcfollow,"ii"),
  14740. BUILDIN_DEF(pcstopfollow,"i"),
  14741. BUILDIN_DEF(pcblockmove,"ii"),
  14742. // <--- [zBuffer] List of player cont commands
  14743. // [zBuffer] List of mob control commands --->
  14744. BUILDIN_DEF(unitwalk,"ii?"),
  14745. BUILDIN_DEF(unitkill,"i"),
  14746. BUILDIN_DEF(unitwarp,"isii"),
  14747. BUILDIN_DEF(unitattack,"iv?"),
  14748. BUILDIN_DEF(unitstop,"i"),
  14749. BUILDIN_DEF(unittalk,"is"),
  14750. BUILDIN_DEF(unitemote,"ii"),
  14751. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  14752. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  14753. // <--- [zBuffer] List of mob control commands
  14754. BUILDIN_DEF(sleep,"i"),
  14755. BUILDIN_DEF(sleep2,"i"),
  14756. BUILDIN_DEF(awake,"s"),
  14757. BUILDIN_DEF(getvariableofnpc,"rs"),
  14758. BUILDIN_DEF(warpportal,"iisii"),
  14759. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  14760. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  14761. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  14762. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  14763. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  14764. BUILDIN_DEF(checkvending,"?"),
  14765. BUILDIN_DEF(checkchatting,"?"),
  14766. BUILDIN_DEF(openmail,""),
  14767. BUILDIN_DEF(openauction,""),
  14768. BUILDIN_DEF(checkcell,"siii"),
  14769. BUILDIN_DEF(setcell,"siiiiii"),
  14770. BUILDIN_DEF(setwall,"siiiiis"),
  14771. BUILDIN_DEF(delwall,"s"),
  14772. BUILDIN_DEF(searchitem,"rs"),
  14773. BUILDIN_DEF(mercenary_create,"ii"),
  14774. BUILDIN_DEF(mercenary_heal,"ii"),
  14775. BUILDIN_DEF(mercenary_sc_start,"iii"),
  14776. BUILDIN_DEF(mercenary_get_calls,"i"),
  14777. BUILDIN_DEF(mercenary_get_faith,"i"),
  14778. BUILDIN_DEF(mercenary_set_calls,"ii"),
  14779. BUILDIN_DEF(mercenary_set_faith,"ii"),
  14780. BUILDIN_DEF(readbook,"ii"),
  14781. BUILDIN_DEF(setfont,"i"),
  14782. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  14783. BUILDIN_DEF(progressbar,"si"),
  14784. BUILDIN_DEF(pushpc,"ii"),
  14785. BUILDIN_DEF(buyingstore,"i"),
  14786. BUILDIN_DEF(searchstores,"ii"),
  14787. BUILDIN_DEF(showdigit,"i?"),
  14788. // WoE SE
  14789. BUILDIN_DEF(agitstart2,""),
  14790. BUILDIN_DEF(agitend2,""),
  14791. BUILDIN_DEF(agitcheck2,""),
  14792. // BattleGround
  14793. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  14794. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  14795. BUILDIN_DEF(bg_team_setxy,"iii"),
  14796. BUILDIN_DEF(bg_warp,"isii"),
  14797. BUILDIN_DEF(bg_monster,"isiisi?"),
  14798. BUILDIN_DEF(bg_monster_set_team,"ii"),
  14799. BUILDIN_DEF(bg_leave,""),
  14800. BUILDIN_DEF(bg_destroy,"i"),
  14801. BUILDIN_DEF(areapercentheal,"siiiiii"),
  14802. BUILDIN_DEF(bg_get_data,"ii"),
  14803. BUILDIN_DEF(bg_getareausers,"isiiii"),
  14804. BUILDIN_DEF(bg_updatescore,"sii"),
  14805. // Instancing
  14806. BUILDIN_DEF(instance_create,"si"),
  14807. BUILDIN_DEF(instance_destroy,"?"),
  14808. BUILDIN_DEF(instance_attachmap,"si?"),
  14809. BUILDIN_DEF(instance_detachmap,"s?"),
  14810. BUILDIN_DEF(instance_attach,"i"),
  14811. BUILDIN_DEF(instance_id,"?"),
  14812. BUILDIN_DEF(instance_set_timeout,"ii?"),
  14813. BUILDIN_DEF(instance_init,"i"),
  14814. BUILDIN_DEF(instance_announce,"isi?????"),
  14815. BUILDIN_DEF(instance_npcname,"s?"),
  14816. BUILDIN_DEF(has_instance,"s?"),
  14817. BUILDIN_DEF(instance_warpall,"sii?"),
  14818. BUILDIN_DEF(instance_check_party,"i???"),
  14819. /**
  14820. * 3rd-related
  14821. **/
  14822. BUILDIN_DEF(makerune,"i"),
  14823. BUILDIN_DEF(checkdragon,""),//[Ind]
  14824. BUILDIN_DEF(setdragon,"?"),//[Ind]
  14825. BUILDIN_DEF(ismounting,""),//[Ind]
  14826. BUILDIN_DEF(setmounting,""),//[Ind]
  14827. BUILDIN_DEF(checkre,"i"),
  14828. /**
  14829. * rAthena and beyond!
  14830. **/
  14831. BUILDIN_DEF(getargcount,""),
  14832. BUILDIN_DEF(is_function,"s"),
  14833. BUILDIN_DEF(get_revision,""),
  14834. BUILDIN_DEF(freeloop,"i"),
  14835. /**
  14836. * @commands (script based)
  14837. **/
  14838. BUILDIN_DEF(bindatcmd, "ss??"),
  14839. BUILDIN_DEF(unbindatcmd, "s"),
  14840. BUILDIN_DEF(useatcmd, "s"),
  14841. //Quest Log System [Inkfish]
  14842. BUILDIN_DEF(setquest, "i"),
  14843. BUILDIN_DEF(erasequest, "i"),
  14844. BUILDIN_DEF(completequest, "i"),
  14845. BUILDIN_DEF(checkquest, "i?"),
  14846. BUILDIN_DEF(changequest, "ii"),
  14847. BUILDIN_DEF(showevent, "ii"),
  14848. {NULL,NULL,NULL},
  14849. };