script.c 402 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528
  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 <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <math.h>
  52. #ifndef WIN32
  53. #include <sys/time.h>
  54. #endif
  55. #include <time.h>
  56. #include <setjmp.h>
  57. #include <errno.h>
  58. ///////////////////////////////////////////////////////////////////////////////
  59. //## TODO possible enhancements: [FlavioJS]
  60. // - 'callfunc' supporting labels in the current npc "::LabelName"
  61. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  62. // - 'function FuncName;' function declarations reverting to global functions
  63. // if local label isn't found
  64. // - join callfunc and callsub's functionality
  65. // - remove dynamic allocation in add_word()
  66. // - remove GETVALUE / SETVALUE
  67. // - clean up the set_reg / set_val / setd_sub mess
  68. // - detect invalid label references at parse-time
  69. //
  70. // struct script_state* st;
  71. //
  72. /// Returns the script_data at the target index
  73. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  74. /// Returns if the stack contains data at the target index
  75. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  76. /// Returns the index of the last data in the stack
  77. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  78. /// Pushes an int into the stack
  79. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  80. /// Pushes a string into the stack (script engine frees it automatically)
  81. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  82. /// Pushes a copy of a string into the stack
  83. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  84. /// Pushes a constant string into the stack (must never change or be freed)
  85. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  86. /// Pushes a nil into the stack
  87. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  88. /// Pushes a copy of the data in the target index
  89. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  90. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  91. #define script_isint(st,i) data_isint(script_getdata(st,i))
  92. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  93. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  94. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  95. // Note: "top" functions/defines use indexes relative to the top of the stack
  96. // -1 is the index of the data at the top
  97. /// Returns the script_data at the target index relative to the top of the stack
  98. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  99. /// Pushes a copy of the data in the target index relative to the top of the stack
  100. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  101. /// Removes the range of values [start,end[ relative to the top of the stack
  102. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  103. //
  104. // struct script_data* data;
  105. //
  106. /// Returns if the script data is a string
  107. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  108. /// Returns if the script data is an int
  109. #define data_isint(data) ( (data)->type == C_INT )
  110. /// Returns if the script data is a reference
  111. #define data_isreference(data) ( (data)->type == C_NAME )
  112. /// Returns if the script data is a label
  113. #define data_islabel(data) ( (data)->type == C_POS )
  114. /// Returns if the script data is an internal script function label
  115. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  116. /// Returns if this is a reference to a constant
  117. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  118. /// Returns if this a reference to a param
  119. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  120. /// Returns if this a reference to a variable
  121. //##TODO confirm it's C_NAME [FlavioJS]
  122. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  123. /// Returns the unique id of the reference (id and index)
  124. #define reference_getuid(data) ( (data)->u.num )
  125. /// Returns the id of the reference
  126. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  127. /// Returns the array index of the reference
  128. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  129. /// Returns the name of the reference
  130. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  131. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  132. #define reference_getref(data) ( (data)->ref )
  133. /// Returns the value of the constant
  134. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  135. /// Returns the type of param
  136. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  137. /// Composes the uid of a reference from the id and the index
  138. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  139. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  140. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  141. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  142. #define FETCH(n, t) \
  143. if( script_hasdata(st,n) ) \
  144. (t)=script_getnum(st,n);
  145. /// Maximum amount of elements in script arrays
  146. #define SCRIPT_MAX_ARRAYSIZE 128
  147. #define SCRIPT_BLOCK_SIZE 512
  148. enum { LABEL_NEXTLINE=1,LABEL_START };
  149. /// temporary buffer for passing around compiled bytecode
  150. /// @see add_scriptb, set_label, parse_script
  151. static unsigned char* script_buf = NULL;
  152. static int script_pos = 0, script_size = 0;
  153. static inline int GETVALUE(const unsigned char* buf, int i)
  154. {
  155. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  156. }
  157. static inline void SETVALUE(unsigned char* buf, int i, int n)
  158. {
  159. buf[i] = GetByte(n, 0);
  160. buf[i+1] = GetByte(n, 1);
  161. buf[i+2] = GetByte(n, 2);
  162. }
  163. // String buffer structures.
  164. // str_data stores string information
  165. static struct str_data_struct {
  166. enum c_op type;
  167. int str;
  168. int backpatch;
  169. int label;
  170. int (*func)(struct script_state *st);
  171. int val;
  172. int next;
  173. } *str_data = NULL;
  174. static int str_data_size = 0; // size of the data
  175. static int str_num = LABEL_START; // next id to be assigned
  176. // str_buf holds the strings themselves
  177. static char *str_buf;
  178. static int str_size = 0; // size of the buffer
  179. static int str_pos = 0; // next position to be assigned
  180. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  181. #define SCRIPT_HASH_SIZE 1021
  182. int str_hash[SCRIPT_HASH_SIZE];
  183. // Specifies which string hashing method to use
  184. //#define SCRIPT_HASH_DJB2
  185. //#define SCRIPT_HASH_SDBM
  186. #define SCRIPT_HASH_ELF
  187. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  188. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  189. static int parse_options=0;
  190. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  191. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  192. // important buildin function references for usage in scripts
  193. static int buildin_set_ref = 0;
  194. static int buildin_callsub_ref = 0;
  195. static int buildin_callfunc_ref = 0;
  196. static int buildin_getelementofarray_ref = 0;
  197. // Caches compiled autoscript item code.
  198. // Note: This is not cleared when reloading itemdb.
  199. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  200. struct Script_Config script_config = {
  201. 1, // warn_func_mismatch_argtypes
  202. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  203. 0, INT_MAX, // input_min_value/input_max_value
  204. "OnPCDieEvent", //die_event_name
  205. "OnPCKillEvent", //kill_pc_event_name
  206. "OnNPCKillEvent", //kill_mob_event_name
  207. "OnPCLoginEvent", //login_event_name
  208. "OnPCLogoutEvent", //logout_event_name
  209. "OnPCLoadMapEvent", //loadmap_event_name
  210. "OnPCBaseLvUpEvent", //baselvup_event_name
  211. "OnPCJobLvUpEvent", //joblvup_event_name
  212. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  213. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  214. };
  215. static jmp_buf error_jump;
  216. static char* error_msg;
  217. static const char* error_pos;
  218. static int error_report; // if the error should produce output
  219. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  220. // [Eoe / jA 1080, 1081, 1094, 1164]
  221. enum curly_type {
  222. TYPE_NULL = 0,
  223. TYPE_IF,
  224. TYPE_SWITCH,
  225. TYPE_WHILE,
  226. TYPE_FOR,
  227. TYPE_DO,
  228. TYPE_USERFUNC,
  229. TYPE_ARGLIST // function argument list
  230. };
  231. enum e_arglist
  232. {
  233. ARGLIST_UNDEFINED = 0,
  234. ARGLIST_NO_PAREN = 1,
  235. ARGLIST_PAREN = 2,
  236. };
  237. static struct {
  238. struct {
  239. enum curly_type type;
  240. int index;
  241. int count;
  242. int flag;
  243. struct linkdb_node *case_label;
  244. } curly[256]; // 右カッコの情報
  245. int curly_count; // 右カッコの数
  246. int index; // スクリプト内で使用した構文の数
  247. } syntax;
  248. const char* parse_curly_close(const char* p);
  249. const char* parse_syntax_close(const char* p);
  250. const char* parse_syntax_close_sub(const char* p,int* flag);
  251. const char* parse_syntax(const char* p);
  252. static int parse_syntax_for_flag = 0;
  253. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  254. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  255. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  256. int potion_target=0;
  257. c_op get_com(unsigned char *script,int *pos);
  258. int get_num(unsigned char *script,int *pos);
  259. typedef struct script_function {
  260. int (*func)(struct script_state *st);
  261. const char *name;
  262. const char *arg;
  263. } script_function;
  264. extern script_function buildin_func[];
  265. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  266. /*==========================================
  267. * ローカルプロトタイプ宣言 (必要な物のみ)
  268. *------------------------------------------*/
  269. const char* parse_subexpr(const char* p,int limit);
  270. int run_func(struct script_state *st);
  271. enum {
  272. MF_NOMEMO, //0
  273. MF_NOTELEPORT,
  274. MF_NOSAVE,
  275. MF_NOBRANCH,
  276. MF_NOPENALTY,
  277. MF_NOZENYPENALTY,
  278. MF_PVP,
  279. MF_PVP_NOPARTY,
  280. MF_PVP_NOGUILD,
  281. MF_GVG,
  282. MF_GVG_NOPARTY, //10
  283. MF_NOTRADE,
  284. MF_NOSKILL,
  285. MF_NOWARP,
  286. MF_PARTYLOCK,
  287. MF_NOICEWALL,
  288. MF_SNOW,
  289. MF_FOG,
  290. MF_SAKURA,
  291. MF_LEAVES,
  292. /**
  293. * No longer available, keeping here just in case it's back someday. [Ind]
  294. **/
  295. //MF_RAIN, //20
  296. // 21 free
  297. MF_NOGO = 22,
  298. MF_CLOUDS,
  299. MF_CLOUDS2,
  300. MF_FIREWORKS,
  301. MF_GVG_CASTLE,
  302. MF_GVG_DUNGEON,
  303. MF_NIGHTENABLED,
  304. MF_NOBASEEXP,
  305. MF_NOJOBEXP, //30
  306. MF_NOMOBLOOT,
  307. MF_NOMVPLOOT,
  308. MF_NORETURN,
  309. MF_NOWARPTO,
  310. MF_NIGHTMAREDROP,
  311. MF_RESTRICTED,
  312. MF_NOCOMMAND,
  313. MF_NODROP,
  314. MF_JEXP,
  315. MF_BEXP, //40
  316. MF_NOVENDING,
  317. MF_LOADEVENT,
  318. MF_NOCHAT,
  319. MF_NOEXPPENALTY,
  320. MF_GUILDLOCK,
  321. MF_TOWN,
  322. MF_AUTOTRADE,
  323. MF_ALLOWKS,
  324. MF_MONSTER_NOTELEPORT,
  325. MF_PVP_NOCALCRANK, //50
  326. MF_BATTLEGROUND,
  327. MF_RESET
  328. };
  329. const char* script_op2name(int op)
  330. {
  331. #define RETURN_OP_NAME(type) case type: return #type
  332. switch( op )
  333. {
  334. RETURN_OP_NAME(C_NOP);
  335. RETURN_OP_NAME(C_POS);
  336. RETURN_OP_NAME(C_INT);
  337. RETURN_OP_NAME(C_PARAM);
  338. RETURN_OP_NAME(C_FUNC);
  339. RETURN_OP_NAME(C_STR);
  340. RETURN_OP_NAME(C_CONSTSTR);
  341. RETURN_OP_NAME(C_ARG);
  342. RETURN_OP_NAME(C_NAME);
  343. RETURN_OP_NAME(C_EOL);
  344. RETURN_OP_NAME(C_RETINFO);
  345. RETURN_OP_NAME(C_USERFUNC);
  346. RETURN_OP_NAME(C_USERFUNC_POS);
  347. // operators
  348. RETURN_OP_NAME(C_OP3);
  349. RETURN_OP_NAME(C_LOR);
  350. RETURN_OP_NAME(C_LAND);
  351. RETURN_OP_NAME(C_LE);
  352. RETURN_OP_NAME(C_LT);
  353. RETURN_OP_NAME(C_GE);
  354. RETURN_OP_NAME(C_GT);
  355. RETURN_OP_NAME(C_EQ);
  356. RETURN_OP_NAME(C_NE);
  357. RETURN_OP_NAME(C_XOR);
  358. RETURN_OP_NAME(C_OR);
  359. RETURN_OP_NAME(C_AND);
  360. RETURN_OP_NAME(C_ADD);
  361. RETURN_OP_NAME(C_SUB);
  362. RETURN_OP_NAME(C_MUL);
  363. RETURN_OP_NAME(C_DIV);
  364. RETURN_OP_NAME(C_MOD);
  365. RETURN_OP_NAME(C_NEG);
  366. RETURN_OP_NAME(C_LNOT);
  367. RETURN_OP_NAME(C_NOT);
  368. RETURN_OP_NAME(C_R_SHIFT);
  369. RETURN_OP_NAME(C_L_SHIFT);
  370. default:
  371. ShowDebug("script_op2name: unexpected op=%d\n", op);
  372. return "???";
  373. }
  374. #undef RETURN_OP_NAME
  375. }
  376. #ifdef DEBUG_DUMP_STACK
  377. static void script_dump_stack(struct script_state* st)
  378. {
  379. int i;
  380. ShowMessage("\tstart = %d\n", st->start);
  381. ShowMessage("\tend = %d\n", st->end);
  382. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  383. ShowMessage("\tsp = %d\n", st->stack->sp);
  384. for( i = 0; i < st->stack->sp; ++i )
  385. {
  386. struct script_data* data = &st->stack->stack_data[i];
  387. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  388. switch( data->type )
  389. {
  390. case C_INT:
  391. case C_POS:
  392. ShowMessage(" %d\n", data->u.num);
  393. break;
  394. case C_STR:
  395. case C_CONSTSTR:
  396. ShowMessage(" \"%s\"\n", data->u.str);
  397. break;
  398. case C_NAME:
  399. 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));
  400. break;
  401. case C_RETINFO:
  402. {
  403. struct script_retinfo* ri = data->u.ri;
  404. 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);
  405. }
  406. break;
  407. default:
  408. ShowMessage("\n");
  409. break;
  410. }
  411. }
  412. }
  413. #endif
  414. /// Reports on the console the src of a script error.
  415. static void script_reportsrc(struct script_state *st)
  416. {
  417. struct block_list* bl;
  418. if( st->oid == 0 )
  419. return; //Can't report source.
  420. bl = map_id2bl(st->oid);
  421. if( bl == NULL )
  422. return;
  423. switch( bl->type )
  424. {
  425. case BL_NPC:
  426. if( bl->m >= 0 )
  427. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  428. else
  429. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  430. break;
  431. default:
  432. if( bl->m >= 0 )
  433. 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);
  434. else
  435. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  436. break;
  437. }
  438. }
  439. /// Reports on the console information about the script data.
  440. static void script_reportdata(struct script_data* data)
  441. {
  442. if( data == NULL )
  443. return;
  444. switch( data->type )
  445. {
  446. case C_NOP:// no value
  447. ShowDebug("Data: nothing (nil)\n");
  448. break;
  449. case C_INT:// number
  450. ShowDebug("Data: number value=%d\n", data->u.num);
  451. break;
  452. case C_STR:
  453. case C_CONSTSTR:// string
  454. if( data->u.str )
  455. {
  456. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  457. }
  458. else
  459. {
  460. ShowDebug("Data: string value=NULL\n");
  461. }
  462. break;
  463. case C_NAME:// reference
  464. if( reference_tovariable(data) )
  465. {// variable
  466. const char* name = reference_getname(data);
  467. if( not_array_variable(*name) )
  468. ShowDebug("Data: variable name='%s'\n", name);
  469. else
  470. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  471. }
  472. else if( reference_toconstant(data) )
  473. {// constant
  474. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  475. }
  476. else if( reference_toparam(data) )
  477. {// param
  478. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  479. }
  480. else
  481. {// ???
  482. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  483. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  484. }
  485. break;
  486. case C_POS:// label
  487. ShowDebug("Data: label pos=%d\n", data->u.num);
  488. break;
  489. default:
  490. ShowDebug("Data: %s\n", script_op2name(data->type));
  491. break;
  492. }
  493. }
  494. /// Reports on the console information about the current built-in function.
  495. static void script_reportfunc(struct script_state* st)
  496. {
  497. int i, params, id;
  498. struct script_data* data;
  499. if( !script_hasdata(st,0) )
  500. {// no stack
  501. return;
  502. }
  503. data = script_getdata(st,0);
  504. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  505. {// script currently not executing a built-in function or corrupt stack
  506. return;
  507. }
  508. id = reference_getid(data);
  509. params = script_lastdata(st)-1;
  510. if( params > 0 )
  511. {
  512. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  513. for( i = 2; i <= script_lastdata(st); i++ )
  514. {
  515. script_reportdata(script_getdata(st,i));
  516. }
  517. }
  518. else
  519. {
  520. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  521. }
  522. }
  523. /*==========================================
  524. * エラーメッセージ出力
  525. *------------------------------------------*/
  526. static void disp_error_message2(const char *mes,const char *pos,int report)
  527. {
  528. error_msg = aStrdup(mes);
  529. error_pos = pos;
  530. error_report = report;
  531. longjmp( error_jump, 1 );
  532. }
  533. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  534. /// Checks event parameter validity
  535. static void check_event(struct script_state *st, const char *evt)
  536. {
  537. if( evt && evt[0] && !stristr(evt, "::On") )
  538. {
  539. if( npc_event_isspecial(evt) )
  540. {
  541. ; // portable small/large monsters or other attributes
  542. }
  543. else
  544. {
  545. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  546. script_reportsrc(st);
  547. }
  548. }
  549. }
  550. /*==========================================
  551. * Hashes the input string
  552. *------------------------------------------*/
  553. static unsigned int calc_hash(const char* p)
  554. {
  555. unsigned int h;
  556. #if defined(SCRIPT_HASH_DJB2)
  557. h = 5381;
  558. while( *p ) // hash*33 + c
  559. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  560. #elif defined(SCRIPT_HASH_SDBM)
  561. h = 0;
  562. while( *p ) // hash*65599 + c
  563. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  564. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  565. h = 0;
  566. while( *p ){
  567. unsigned int g;
  568. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  569. g = h & 0xF0000000;
  570. if( g )
  571. {
  572. h ^= g >> 24;
  573. h &= ~g;
  574. }
  575. }
  576. #else // athena hash
  577. h = 0;
  578. while( *p )
  579. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  580. #endif
  581. return h % SCRIPT_HASH_SIZE;
  582. }
  583. /*==========================================
  584. * str_data manipulation functions
  585. *------------------------------------------*/
  586. /// Looks up string using the provided id.
  587. const char* get_str(int id)
  588. {
  589. Assert( id >= LABEL_START && id < str_size );
  590. return str_buf+str_data[id].str;
  591. }
  592. /// Returns the uid of the string, or -1.
  593. static int search_str(const char* p)
  594. {
  595. int i;
  596. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  597. if( strcasecmp(get_str(i),p) == 0 )
  598. return i;
  599. return -1;
  600. }
  601. /// Stores a copy of the string and returns its id.
  602. /// If an identical string is already present, returns its id instead.
  603. int add_str(const char* p)
  604. {
  605. int i, h;
  606. int len;
  607. h = calc_hash(p);
  608. if( str_hash[h] == 0 )
  609. {// empty bucket, add new node here
  610. str_hash[h] = str_num;
  611. }
  612. else
  613. {// scan for end of list, or occurence of identical string
  614. for( i = str_hash[h]; ; i = str_data[i].next )
  615. {
  616. if( strcasecmp(get_str(i),p) == 0 )
  617. return i; // string already in list
  618. if( str_data[i].next == 0 )
  619. break; // reached the end
  620. }
  621. // append node to end of list
  622. str_data[i].next = str_num;
  623. }
  624. // grow list if neccessary
  625. if( str_num >= str_data_size )
  626. {
  627. str_data_size += 128;
  628. RECREATE(str_data,struct str_data_struct,str_data_size);
  629. memset(str_data + (str_data_size - 128), '\0', 128);
  630. }
  631. len=(int)strlen(p);
  632. // grow string buffer if neccessary
  633. while( str_pos+len+1 >= str_size )
  634. {
  635. str_size += 256;
  636. RECREATE(str_buf,char,str_size);
  637. memset(str_buf + (str_size - 256), '\0', 256);
  638. }
  639. safestrncpy(str_buf+str_pos, p, len+1);
  640. str_data[str_num].type = C_NOP;
  641. str_data[str_num].str = str_pos;
  642. str_data[str_num].next = 0;
  643. str_data[str_num].func = NULL;
  644. str_data[str_num].backpatch = -1;
  645. str_data[str_num].label = -1;
  646. str_pos += len+1;
  647. return str_num++;
  648. }
  649. /// Appends 1 byte to the script buffer.
  650. static void add_scriptb(int a)
  651. {
  652. if( script_pos+1 >= script_size )
  653. {
  654. script_size += SCRIPT_BLOCK_SIZE;
  655. RECREATE(script_buf,unsigned char,script_size);
  656. }
  657. script_buf[script_pos++] = (uint8)(a);
  658. }
  659. /// Appends a c_op value to the script buffer.
  660. /// The value is variable-length encoded into 8-bit blocks.
  661. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  662. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  663. static void add_scriptc(int a)
  664. {
  665. while( a >= 0x40 )
  666. {
  667. add_scriptb((a&0x3f)|0x40);
  668. a = (a - 0x40) >> 6;
  669. }
  670. add_scriptb(a);
  671. }
  672. /// Appends an integer value to the script buffer.
  673. /// The value is variable-length encoded into 8-bit blocks.
  674. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  675. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  676. static void add_scripti(int a)
  677. {
  678. while( a >= 0x40 )
  679. {
  680. add_scriptb((a&0x3f)|0xc0);
  681. a = (a - 0x40) >> 6;
  682. }
  683. add_scriptb(a|0x80);
  684. }
  685. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  686. ///
  687. /// @param l The id of the str_data entry
  688. // 最大16Mまで
  689. static void add_scriptl(int l)
  690. {
  691. int backpatch = str_data[l].backpatch;
  692. switch(str_data[l].type){
  693. case C_POS:
  694. case C_USERFUNC_POS:
  695. add_scriptc(C_POS);
  696. add_scriptb(str_data[l].label);
  697. add_scriptb(str_data[l].label>>8);
  698. add_scriptb(str_data[l].label>>16);
  699. break;
  700. case C_NOP:
  701. case C_USERFUNC:
  702. // ラベルの可能性があるのでbackpatch用データ埋め込み
  703. add_scriptc(C_NAME);
  704. str_data[l].backpatch = script_pos;
  705. add_scriptb(backpatch);
  706. add_scriptb(backpatch>>8);
  707. add_scriptb(backpatch>>16);
  708. break;
  709. case C_INT:
  710. add_scripti(abs(str_data[l].val));
  711. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  712. add_scriptc(C_NEG);
  713. break;
  714. default: // assume C_NAME
  715. add_scriptc(C_NAME);
  716. add_scriptb(l);
  717. add_scriptb(l>>8);
  718. add_scriptb(l>>16);
  719. break;
  720. }
  721. }
  722. /*==========================================
  723. * ラベルを解決する
  724. *------------------------------------------*/
  725. void set_label(int l,int pos, const char* script_pos)
  726. {
  727. int i,next;
  728. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  729. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  730. disp_error_message("set_label: invalid label name",script_pos);
  731. return;
  732. }
  733. if(str_data[l].label!=-1){
  734. disp_error_message("set_label: dup label ",script_pos);
  735. return;
  736. }
  737. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  738. str_data[l].label=pos;
  739. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  740. next=GETVALUE(script_buf,i);
  741. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  742. SETVALUE(script_buf,i,pos);
  743. i=next;
  744. }
  745. }
  746. /// Skips spaces and/or comments.
  747. const char* skip_space(const char* p)
  748. {
  749. if( p == NULL )
  750. return NULL;
  751. for(;;)
  752. {
  753. while( ISSPACE(*p) )
  754. ++p;
  755. if( *p == '/' && p[1] == '/' )
  756. {// line comment
  757. while(*p && *p!='\n')
  758. ++p;
  759. }
  760. else if( *p == '/' && p[1] == '*' )
  761. {// block comment
  762. p += 2;
  763. for(;;)
  764. {
  765. if( *p == '\0' )
  766. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  767. if( *p == '*' && p[1] == '/' )
  768. {// end of block comment
  769. p += 2;
  770. break;
  771. }
  772. ++p;
  773. }
  774. }
  775. else
  776. break;
  777. }
  778. return p;
  779. }
  780. /// Skips a word.
  781. /// A word consists of undercores and/or alfanumeric characters,
  782. /// and valid variable prefixes/postfixes.
  783. static
  784. const char* skip_word(const char* p)
  785. {
  786. // prefix
  787. switch( *p )
  788. {
  789. case '@':// temporary char variable
  790. ++p; break;
  791. case '#':// account variable
  792. p += ( p[1] == '#' ? 2 : 1 ); break;
  793. case '\'':// instance variable
  794. ++p; break;
  795. case '.':// npc variable
  796. p += ( p[1] == '@' ? 2 : 1 ); break;
  797. case '$':// global variable
  798. p += ( p[1] == '@' ? 2 : 1 ); break;
  799. }
  800. while( ISALNUM(*p) || *p == '_' )
  801. ++p;
  802. // postfix
  803. if( *p == '$' )// string
  804. p++;
  805. return p;
  806. }
  807. /// Adds a word to str_data.
  808. /// @see skip_word
  809. /// @see add_str
  810. static
  811. int add_word(const char* p)
  812. {
  813. char* word;
  814. int len;
  815. int i;
  816. // Check for a word
  817. len = skip_word(p) - p;
  818. if( len == 0 )
  819. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  820. // Duplicate the word
  821. word = (char*)aMalloc(len+1);
  822. memcpy(word, p, len);
  823. word[len] = 0;
  824. // add the word
  825. i = add_str(word);
  826. aFree(word);
  827. return i;
  828. }
  829. /// Parses a function call.
  830. /// The argument list can have parenthesis or not.
  831. /// The number of arguments is checked.
  832. static
  833. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  834. {
  835. const char* p2;
  836. const char* arg=NULL;
  837. int func;
  838. func = add_word(p);
  839. if( str_data[func].type == C_FUNC ){
  840. // buildin function
  841. add_scriptl(func);
  842. add_scriptc(C_ARG);
  843. arg = buildin_func[str_data[func].val].arg;
  844. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  845. // script defined function
  846. add_scriptl(buildin_callsub_ref);
  847. add_scriptc(C_ARG);
  848. add_scriptl(func);
  849. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  850. if( *arg == 0 )
  851. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  852. if( *arg != '*' )
  853. ++arg; // count func as argument
  854. } else {
  855. #ifdef SCRIPT_CALLFUNC_CHECK
  856. const char* name = get_str(func);
  857. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  858. #endif
  859. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  860. #ifdef SCRIPT_CALLFUNC_CHECK
  861. } else {;
  862. add_scriptl(buildin_callfunc_ref);
  863. add_scriptc(C_ARG);
  864. add_scriptc(C_STR);
  865. while( *name ) add_scriptb(*name ++);
  866. add_scriptb(0);
  867. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  868. if( *arg != '*' ) ++ arg;
  869. }
  870. #endif
  871. }
  872. p = skip_word(p);
  873. p = skip_space(p);
  874. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  875. syntax.curly[syntax.curly_count].count = 0;
  876. if( *p == ';' )
  877. {// <func name> ';'
  878. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  879. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  880. {// <func name> '(' ')'
  881. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  882. p = p2;
  883. /*
  884. } else if( 0 && require_paren && *p != '(' )
  885. {// <func name>
  886. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  887. */
  888. } else
  889. {// <func name> <arg list>
  890. if( require_paren ){
  891. if( *p != '(' )
  892. disp_error_message("need '('",p);
  893. ++p; // skip '('
  894. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  895. } else if( *p == '(' ){
  896. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  897. } else {
  898. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  899. }
  900. ++syntax.curly_count;
  901. while( *arg ) {
  902. p2=parse_subexpr(p,-1);
  903. if( p == p2 )
  904. break; // not an argument
  905. if( *arg != '*' )
  906. ++arg; // next argument
  907. p=skip_space(p2);
  908. if( *arg == 0 || *p != ',' )
  909. break; // no more arguments
  910. ++p; // skip comma
  911. }
  912. --syntax.curly_count;
  913. }
  914. if( *arg && *arg != '?' && *arg != '*' )
  915. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  916. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  917. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  918. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  919. if( *p != ')' )
  920. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  921. ++p;
  922. }
  923. add_scriptc(C_FUNC);
  924. return p;
  925. }
  926. /// Processes end of logical script line.
  927. /// @param first When true, only fix up scheduling data is initialized
  928. /// @param p Script position for error reporting in set_label
  929. static void parse_nextline(bool first, const char* p)
  930. {
  931. if( !first )
  932. {
  933. add_scriptc(C_EOL); // mark end of line for stack cleanup
  934. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  935. }
  936. // initialize data for new '-' label fix up scheduling
  937. str_data[LABEL_NEXTLINE].type = C_NOP;
  938. str_data[LABEL_NEXTLINE].backpatch = -1;
  939. str_data[LABEL_NEXTLINE].label = -1;
  940. }
  941. /// Parse a variable assignment using the direct equals operator
  942. /// @param p script position where the function should run from
  943. /// @return NULL if not a variable assignment, the new position otherwise
  944. const char* parse_variable(const char* p)
  945. {
  946. int i, j, word;
  947. c_op type = C_NOP;
  948. const char *p2 = NULL;
  949. const char *var = p;
  950. // skip the variable where applicable
  951. p = skip_word(p);
  952. p = skip_space(p);
  953. if( p == NULL )
  954. {// end of the line or invalid buffer
  955. return NULL;
  956. }
  957. if( *p == '[' )
  958. {// array variable so process the array as appropriate
  959. for( p2 = p, i = 0, j = 1; p; ++ i )
  960. {
  961. if( *p ++ == ']' && --(j) == 0 ) break;
  962. if( *p == '[' ) ++ j;
  963. }
  964. if( !(p = skip_space(p)) )
  965. {// end of line or invalid characters remaining
  966. disp_error_message("Missing right expression or closing bracket for variable.", p);
  967. }
  968. }
  969. if( type == C_NOP &&
  970. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  971. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  972. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  973. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  974. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  975. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  976. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  977. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  978. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  979. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  980. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  981. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  982. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  983. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  984. ) )
  985. {// failed to find a matching operator combination so invalid
  986. return NULL;
  987. }
  988. switch( type )
  989. {
  990. case C_EQ:
  991. {// incremental modifier
  992. p = skip_space( &p[1] );
  993. }
  994. break;
  995. case C_L_SHIFT:
  996. case C_R_SHIFT:
  997. {// left or right shift modifier
  998. p = skip_space( &p[3] );
  999. }
  1000. break;
  1001. default:
  1002. {// normal incremental command
  1003. p = skip_space( &p[2] );
  1004. }
  1005. }
  1006. if( p == NULL )
  1007. {// end of line or invalid buffer
  1008. return NULL;
  1009. }
  1010. // push the set function onto the stack
  1011. add_scriptl(buildin_set_ref);
  1012. add_scriptc(C_ARG);
  1013. // always append parenthesis to avoid errors
  1014. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1015. syntax.curly[syntax.curly_count].count = 0;
  1016. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1017. // increment the total curly count for the position in the script
  1018. ++ syntax.curly_count;
  1019. // parse the variable currently being modified
  1020. word = add_word(var);
  1021. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1022. {// cannot assign a variable which exists as a function or label
  1023. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1024. }
  1025. if( p2 )
  1026. {// process the variable index
  1027. const char* p3 = NULL;
  1028. // push the getelementofarray method into the stack
  1029. add_scriptl(buildin_getelementofarray_ref);
  1030. add_scriptc(C_ARG);
  1031. add_scriptl(word);
  1032. // process the sub-expression for this assignment
  1033. p3 = parse_subexpr(p2 + 1, 1);
  1034. p3 = skip_space(p3);
  1035. if( *p3 != ']' )
  1036. {// closing parenthesis is required for this script
  1037. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1038. }
  1039. // push the closing function stack operator onto the stack
  1040. add_scriptc(C_FUNC);
  1041. p3 ++;
  1042. }
  1043. else
  1044. {// simply push the variable or value onto the stack
  1045. add_scriptl(word);
  1046. }
  1047. if( type == C_ADD_PP || type == C_SUB_PP )
  1048. {// incremental operator for the method
  1049. add_scripti(1);
  1050. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1051. }
  1052. else
  1053. {// process the value as an expression
  1054. p = parse_subexpr(p, -1);
  1055. if( type != C_EQ )
  1056. {// push the type of modifier onto the stack
  1057. add_scriptc(type);
  1058. }
  1059. }
  1060. // decrement the curly count for the position within the script
  1061. -- syntax.curly_count;
  1062. // close the script by appending the function operator
  1063. add_scriptc(C_FUNC);
  1064. // push the buffer from the method
  1065. return p;
  1066. }
  1067. /*==========================================
  1068. * 項の解析
  1069. *------------------------------------------*/
  1070. const char* parse_simpleexpr(const char *p)
  1071. {
  1072. int i;
  1073. p=skip_space(p);
  1074. if(*p==';' || *p==',')
  1075. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  1076. if(*p=='('){
  1077. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1078. ++syntax.curly[i].count;
  1079. p=parse_subexpr(p+1,-1);
  1080. p=skip_space(p);
  1081. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1082. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1083. ){
  1084. if( *p == ',' ){
  1085. syntax.curly[i].flag = ARGLIST_PAREN;
  1086. return p;
  1087. } else
  1088. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1089. }
  1090. if( *p != ')' )
  1091. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  1092. ++p;
  1093. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  1094. char *np;
  1095. i=strtoul(p,&np,0);
  1096. add_scripti(i);
  1097. p=np;
  1098. } else if(*p=='"'){
  1099. add_scriptc(C_STR);
  1100. p++;
  1101. while( *p && *p != '"' ){
  1102. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1103. {
  1104. char buf[8];
  1105. size_t len = skip_escaped_c(p) - p;
  1106. size_t n = sv_unescape_c(buf, p, len);
  1107. if( n != 1 )
  1108. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1109. p += len;
  1110. add_scriptb(*buf);
  1111. continue;
  1112. }
  1113. else if( *p == '\n' )
  1114. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1115. add_scriptb(*p++);
  1116. }
  1117. if(!*p)
  1118. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1119. add_scriptb(0);
  1120. p++; //'"'
  1121. } else {
  1122. int l;
  1123. const char* pv;
  1124. // label , register , function etc
  1125. if(skip_word(p)==p)
  1126. disp_error_message("parse_simpleexpr: unexpected character",p);
  1127. l=add_word(p);
  1128. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1129. return parse_callfunc(p,1,0);
  1130. #ifdef SCRIPT_CALLFUNC_CHECK
  1131. else {
  1132. const char* name = get_str(l);
  1133. if( strdb_get(userfunc_db,name) != NULL ) {
  1134. return parse_callfunc(p,1,1);
  1135. }
  1136. }
  1137. #endif
  1138. if( (pv = parse_variable(p)) )
  1139. {// successfully processed a variable assignment
  1140. return pv;
  1141. }
  1142. p=skip_word(p);
  1143. if( *p == '[' ){
  1144. // array(name[i] => getelementofarray(name,i) )
  1145. add_scriptl(buildin_getelementofarray_ref);
  1146. add_scriptc(C_ARG);
  1147. add_scriptl(l);
  1148. p=parse_subexpr(p+1,-1);
  1149. p=skip_space(p);
  1150. if( *p != ']' )
  1151. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  1152. ++p;
  1153. add_scriptc(C_FUNC);
  1154. }else
  1155. add_scriptl(l);
  1156. }
  1157. return p;
  1158. }
  1159. /*==========================================
  1160. * 式の解析
  1161. *------------------------------------------*/
  1162. const char* parse_subexpr(const char* p,int limit)
  1163. {
  1164. int op,opl,len;
  1165. const char* tmpp;
  1166. p=skip_space(p);
  1167. if(*p=='-'){
  1168. tmpp=skip_space(p+1);
  1169. if(*tmpp==';' || *tmpp==','){
  1170. add_scriptl(LABEL_NEXTLINE);
  1171. p++;
  1172. return p;
  1173. }
  1174. }
  1175. tmpp=p;
  1176. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1177. p=parse_subexpr(p+1,10);
  1178. add_scriptc(op);
  1179. } else
  1180. p=parse_simpleexpr(p);
  1181. p=skip_space(p);
  1182. while((
  1183. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1184. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1185. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1186. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1187. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1188. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1189. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1190. (op=C_AND,opl=6,len=1,*p=='&') ||
  1191. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1192. (op=C_OR,opl=5,len=1,*p=='|') ||
  1193. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1194. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1195. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1196. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1197. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1198. (op=C_GT,opl=3,len=1,*p=='>') ||
  1199. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1200. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1201. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1202. p+=len;
  1203. if(op == C_OP3) {
  1204. p=parse_subexpr(p,-1);
  1205. p=skip_space(p);
  1206. if( *(p++) != ':')
  1207. disp_error_message("parse_subexpr: need ':'", p-1);
  1208. p=parse_subexpr(p,-1);
  1209. } else {
  1210. p=parse_subexpr(p,opl);
  1211. }
  1212. add_scriptc(op);
  1213. p=skip_space(p);
  1214. }
  1215. return p; /* return first untreated operator */
  1216. }
  1217. /*==========================================
  1218. * 式の評価
  1219. *------------------------------------------*/
  1220. const char* parse_expr(const char *p)
  1221. {
  1222. switch(*p){
  1223. case ')': case ';': case ':': case '[': case ']':
  1224. case '}':
  1225. disp_error_message("parse_expr: unexpected char",p);
  1226. }
  1227. p=parse_subexpr(p,-1);
  1228. return p;
  1229. }
  1230. /*==========================================
  1231. * 行の解析
  1232. *------------------------------------------*/
  1233. const char* parse_line(const char* p)
  1234. {
  1235. const char* p2;
  1236. p=skip_space(p);
  1237. if(*p==';') {
  1238. // if(); for(); while(); のために閉じ判定
  1239. p = parse_syntax_close(p + 1);
  1240. return p;
  1241. }
  1242. if(*p==')' && parse_syntax_for_flag)
  1243. return p+1;
  1244. p = skip_space(p);
  1245. if(p[0] == '{') {
  1246. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1247. syntax.curly[syntax.curly_count].count = -1;
  1248. syntax.curly[syntax.curly_count].index = -1;
  1249. syntax.curly_count++;
  1250. return p + 1;
  1251. } else if(p[0] == '}') {
  1252. return parse_curly_close(p);
  1253. }
  1254. // 構文関連の処理
  1255. p2 = parse_syntax(p);
  1256. if(p2 != NULL)
  1257. return p2;
  1258. // attempt to process a variable assignment
  1259. p2 = parse_variable(p);
  1260. if( p2 != NULL )
  1261. {// variable assignment processed so leave the method
  1262. return parse_syntax_close(p2 + 1);
  1263. }
  1264. p = parse_callfunc(p,0,0);
  1265. p = skip_space(p);
  1266. if(parse_syntax_for_flag) {
  1267. if( *p != ')' )
  1268. disp_error_message("parse_line: need ')'",p);
  1269. } else {
  1270. if( *p != ';' )
  1271. disp_error_message("parse_line: need ';'",p);
  1272. }
  1273. // if, for , while の閉じ判定
  1274. p = parse_syntax_close(p+1);
  1275. return p;
  1276. }
  1277. // { ... } の閉じ処理
  1278. const char* parse_curly_close(const char* p)
  1279. {
  1280. if(syntax.curly_count <= 0) {
  1281. disp_error_message("parse_curly_close: unexpected string",p);
  1282. return p + 1;
  1283. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1284. syntax.curly_count--;
  1285. // if, for , while の閉じ判定
  1286. p = parse_syntax_close(p + 1);
  1287. return p;
  1288. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1289. // switch() 閉じ判定
  1290. int pos = syntax.curly_count-1;
  1291. char label[256];
  1292. int l;
  1293. // 一時変数を消す
  1294. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1295. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1296. parse_line(label);
  1297. syntax.curly_count--;
  1298. // 無条件で終了ポインタに移動
  1299. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1300. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1301. parse_line(label);
  1302. syntax.curly_count--;
  1303. // 現在地のラベルを付ける
  1304. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1305. l=add_str(label);
  1306. set_label(l,script_pos, p);
  1307. if(syntax.curly[pos].flag) {
  1308. // default が存在する
  1309. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1310. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1311. parse_line(label);
  1312. syntax.curly_count--;
  1313. }
  1314. // 終了ラベルを付ける
  1315. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1316. l=add_str(label);
  1317. set_label(l,script_pos, p);
  1318. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1319. syntax.curly_count--;
  1320. // if, for , while の閉じ判定
  1321. p = parse_syntax_close(p + 1);
  1322. return p;
  1323. } else {
  1324. disp_error_message("parse_curly_close: unexpected string",p);
  1325. return p + 1;
  1326. }
  1327. }
  1328. // 構文関連の処理
  1329. // break, case, continue, default, do, for, function,
  1330. // if, switch, while をこの内部で処理します。
  1331. const char* parse_syntax(const char* p)
  1332. {
  1333. const char *p2 = skip_word(p);
  1334. switch(*p) {
  1335. case 'B':
  1336. case 'b':
  1337. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1338. // break の処理
  1339. char label[256];
  1340. int pos = syntax.curly_count - 1;
  1341. while(pos >= 0) {
  1342. if(syntax.curly[pos].type == TYPE_DO) {
  1343. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1344. break;
  1345. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1346. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1347. break;
  1348. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1349. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1350. break;
  1351. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1352. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1353. break;
  1354. }
  1355. pos--;
  1356. }
  1357. if(pos < 0) {
  1358. disp_error_message("parse_syntax: unexpected 'break'",p);
  1359. } else {
  1360. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1361. parse_line(label);
  1362. syntax.curly_count--;
  1363. }
  1364. p = skip_space(p2);
  1365. if(*p != ';')
  1366. disp_error_message("parse_syntax: need ';'",p);
  1367. // if, for , while の閉じ判定
  1368. p = parse_syntax_close(p + 1);
  1369. return p;
  1370. }
  1371. break;
  1372. case 'c':
  1373. case 'C':
  1374. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1375. // case の処理
  1376. int pos = syntax.curly_count-1;
  1377. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1378. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1379. return p+1;
  1380. } else {
  1381. char label[256];
  1382. int l,v;
  1383. char *np;
  1384. if(syntax.curly[pos].count != 1) {
  1385. // FALLTHRU 用のジャンプ
  1386. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1387. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1388. parse_line(label);
  1389. syntax.curly_count--;
  1390. // 現在地のラベルを付ける
  1391. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1392. l=add_str(label);
  1393. set_label(l,script_pos, p);
  1394. }
  1395. // switch 判定文
  1396. p = skip_space(p2);
  1397. if(p == p2) {
  1398. disp_error_message("parse_syntax: expect space ' '",p);
  1399. }
  1400. // check whether case label is integer or not
  1401. v = strtol(p,&np,0);
  1402. if(np == p) { //Check for constants
  1403. p2 = skip_word(p);
  1404. v = p2-p; // length of word at p2
  1405. memcpy(label,p,v);
  1406. label[v]='\0';
  1407. if( !script_get_constant(label, &v) )
  1408. disp_error_message("parse_syntax: 'case' label not integer",p);
  1409. p = skip_word(p);
  1410. } else { //Numeric value
  1411. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1412. p++;
  1413. p = skip_word(p);
  1414. if(np != p)
  1415. disp_error_message("parse_syntax: 'case' label not integer",np);
  1416. }
  1417. p = skip_space(p);
  1418. if(*p != ':')
  1419. disp_error_message("parse_syntax: expect ':'",p);
  1420. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1421. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1422. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1423. // 2回parse しないとダメ
  1424. p2 = parse_line(label);
  1425. parse_line(p2);
  1426. syntax.curly_count--;
  1427. if(syntax.curly[pos].count != 1) {
  1428. // FALLTHRU 終了後のラベル
  1429. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1430. l=add_str(label);
  1431. set_label(l,script_pos,p);
  1432. }
  1433. // check duplication of case label [Rayce]
  1434. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1435. disp_error_message("parse_syntax: dup 'case'",p);
  1436. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1437. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1438. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1439. parse_line(label);
  1440. syntax.curly_count--;
  1441. syntax.curly[pos].count++;
  1442. }
  1443. return p + 1;
  1444. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1445. // continue の処理
  1446. char label[256];
  1447. int pos = syntax.curly_count - 1;
  1448. while(pos >= 0) {
  1449. if(syntax.curly[pos].type == TYPE_DO) {
  1450. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1451. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1452. break;
  1453. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1454. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1455. break;
  1456. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1457. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1458. break;
  1459. }
  1460. pos--;
  1461. }
  1462. if(pos < 0) {
  1463. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1464. } else {
  1465. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1466. parse_line(label);
  1467. syntax.curly_count--;
  1468. }
  1469. p = skip_space(p2);
  1470. if(*p != ';')
  1471. disp_error_message("parse_syntax: need ';'",p);
  1472. // if, for , while の閉じ判定
  1473. p = parse_syntax_close(p + 1);
  1474. return p;
  1475. }
  1476. break;
  1477. case 'd':
  1478. case 'D':
  1479. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1480. // switch - default の処理
  1481. int pos = syntax.curly_count-1;
  1482. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1483. disp_error_message("parse_syntax: unexpected 'default'",p);
  1484. } else if(syntax.curly[pos].flag) {
  1485. disp_error_message("parse_syntax: dup 'default'",p);
  1486. } else {
  1487. char label[256];
  1488. int l;
  1489. // 現在地のラベルを付ける
  1490. p = skip_space(p2);
  1491. if(*p != ':') {
  1492. disp_error_message("parse_syntax: need ':'",p);
  1493. }
  1494. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1495. l=add_str(label);
  1496. set_label(l,script_pos,p);
  1497. // 無条件で次のリンクに飛ばす
  1498. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1499. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1500. parse_line(label);
  1501. syntax.curly_count--;
  1502. // default のラベルを付ける
  1503. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1504. l=add_str(label);
  1505. set_label(l,script_pos,p);
  1506. syntax.curly[syntax.curly_count - 1].flag = 1;
  1507. syntax.curly[pos].count++;
  1508. }
  1509. return p + 1;
  1510. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1511. int l;
  1512. char label[256];
  1513. p=skip_space(p2);
  1514. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1515. syntax.curly[syntax.curly_count].count = 1;
  1516. syntax.curly[syntax.curly_count].index = syntax.index++;
  1517. syntax.curly[syntax.curly_count].flag = 0;
  1518. // 現在地のラベル形成する
  1519. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1520. l=add_str(label);
  1521. set_label(l,script_pos,p);
  1522. syntax.curly_count++;
  1523. return p;
  1524. }
  1525. break;
  1526. case 'f':
  1527. case 'F':
  1528. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1529. int l;
  1530. char label[256];
  1531. int pos = syntax.curly_count;
  1532. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1533. syntax.curly[syntax.curly_count].count = 1;
  1534. syntax.curly[syntax.curly_count].index = syntax.index++;
  1535. syntax.curly[syntax.curly_count].flag = 0;
  1536. syntax.curly_count++;
  1537. p=skip_space(p2);
  1538. if(*p != '(')
  1539. disp_error_message("parse_syntax: need '('",p);
  1540. p++;
  1541. // 初期化文を実行する
  1542. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1543. p=parse_line(p);
  1544. syntax.curly_count--;
  1545. // 条件判断開始のラベル形成する
  1546. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1547. l=add_str(label);
  1548. set_label(l,script_pos,p);
  1549. p=skip_space(p);
  1550. if(*p == ';') {
  1551. // for(;;) のパターンなので必ず真
  1552. ;
  1553. } else {
  1554. // 条件が偽なら終了地点に飛ばす
  1555. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1556. add_scriptl(add_str("jump_zero"));
  1557. add_scriptc(C_ARG);
  1558. p=parse_expr(p);
  1559. p=skip_space(p);
  1560. add_scriptl(add_str(label));
  1561. add_scriptc(C_FUNC);
  1562. }
  1563. if(*p != ';')
  1564. disp_error_message("parse_syntax: need ';'",p);
  1565. p++;
  1566. // ループ開始に飛ばす
  1567. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1568. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1569. parse_line(label);
  1570. syntax.curly_count--;
  1571. // 次のループへのラベル形成する
  1572. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1573. l=add_str(label);
  1574. set_label(l,script_pos,p);
  1575. // 次のループに入る時の処理
  1576. // for 最後の ')' を ';' として扱うフラグ
  1577. parse_syntax_for_flag = 1;
  1578. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1579. p=parse_line(p);
  1580. syntax.curly_count--;
  1581. parse_syntax_for_flag = 0;
  1582. // 条件判定処理に飛ばす
  1583. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1584. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1585. parse_line(label);
  1586. syntax.curly_count--;
  1587. // ループ開始のラベル付け
  1588. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1589. l=add_str(label);
  1590. set_label(l,script_pos,p);
  1591. return p;
  1592. }
  1593. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1594. {// internal script function
  1595. const char *func_name;
  1596. func_name = skip_space(p2);
  1597. p = skip_word(func_name);
  1598. if( p == func_name )
  1599. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1600. p2 = skip_space(p);
  1601. if( *p2 == ';' )
  1602. {// function <name> ;
  1603. // function declaration - just register the name
  1604. int l;
  1605. l = add_word(func_name);
  1606. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1607. str_data[l].type = C_USERFUNC;
  1608. else if( str_data[l].type == C_USERFUNC )
  1609. ; // already registered
  1610. else
  1611. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1612. // if, for , while の閉じ判定
  1613. p = parse_syntax_close(p2 + 1);
  1614. return p;
  1615. }
  1616. else if(*p2 == '{')
  1617. {// function <name> <line/block of code>
  1618. char label[256];
  1619. int l;
  1620. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1621. syntax.curly[syntax.curly_count].count = 1;
  1622. syntax.curly[syntax.curly_count].index = syntax.index++;
  1623. syntax.curly[syntax.curly_count].flag = 0;
  1624. ++syntax.curly_count;
  1625. // Jump over the function code
  1626. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1627. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1628. ++syntax.curly_count;
  1629. parse_line(label);
  1630. --syntax.curly_count;
  1631. // Set the position of the function (label)
  1632. l=add_word(func_name);
  1633. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1634. {
  1635. str_data[l].type = C_USERFUNC;
  1636. set_label(l, script_pos, p);
  1637. if( parse_options&SCRIPT_USE_LABEL_DB )
  1638. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1639. }
  1640. else
  1641. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1642. return skip_space(p);
  1643. }
  1644. else
  1645. {
  1646. disp_error_message("expect ';' or '{' at function syntax",p);
  1647. }
  1648. }
  1649. break;
  1650. case 'i':
  1651. case 'I':
  1652. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1653. // if() の処理
  1654. char label[256];
  1655. p=skip_space(p2);
  1656. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1657. disp_error_message("need '('",p);
  1658. }
  1659. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1660. syntax.curly[syntax.curly_count].count = 1;
  1661. syntax.curly[syntax.curly_count].index = syntax.index++;
  1662. syntax.curly[syntax.curly_count].flag = 0;
  1663. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1664. syntax.curly_count++;
  1665. add_scriptl(add_str("jump_zero"));
  1666. add_scriptc(C_ARG);
  1667. p=parse_expr(p);
  1668. p=skip_space(p);
  1669. add_scriptl(add_str(label));
  1670. add_scriptc(C_FUNC);
  1671. return p;
  1672. }
  1673. break;
  1674. case 's':
  1675. case 'S':
  1676. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1677. // switch() の処理
  1678. char label[256];
  1679. p=skip_space(p2);
  1680. if(*p != '(') {
  1681. disp_error_message("need '('",p);
  1682. }
  1683. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1684. syntax.curly[syntax.curly_count].count = 1;
  1685. syntax.curly[syntax.curly_count].index = syntax.index++;
  1686. syntax.curly[syntax.curly_count].flag = 0;
  1687. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1688. syntax.curly_count++;
  1689. add_scriptl(add_str("set"));
  1690. add_scriptc(C_ARG);
  1691. add_scriptl(add_str(label));
  1692. p=parse_expr(p);
  1693. p=skip_space(p);
  1694. if(*p != '{') {
  1695. disp_error_message("parse_syntax: need '{'",p);
  1696. }
  1697. add_scriptc(C_FUNC);
  1698. return p + 1;
  1699. }
  1700. break;
  1701. case 'w':
  1702. case 'W':
  1703. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1704. int l;
  1705. char label[256];
  1706. p=skip_space(p2);
  1707. if(*p != '(') {
  1708. disp_error_message("need '('",p);
  1709. }
  1710. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1711. syntax.curly[syntax.curly_count].count = 1;
  1712. syntax.curly[syntax.curly_count].index = syntax.index++;
  1713. syntax.curly[syntax.curly_count].flag = 0;
  1714. // 条件判断開始のラベル形成する
  1715. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1716. l=add_str(label);
  1717. set_label(l,script_pos,p);
  1718. // 条件が偽なら終了地点に飛ばす
  1719. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1720. syntax.curly_count++;
  1721. add_scriptl(add_str("jump_zero"));
  1722. add_scriptc(C_ARG);
  1723. p=parse_expr(p);
  1724. p=skip_space(p);
  1725. add_scriptl(add_str(label));
  1726. add_scriptc(C_FUNC);
  1727. return p;
  1728. }
  1729. break;
  1730. }
  1731. return NULL;
  1732. }
  1733. const char* parse_syntax_close(const char *p) {
  1734. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1735. int flag;
  1736. do {
  1737. p = parse_syntax_close_sub(p,&flag);
  1738. } while(flag);
  1739. return p;
  1740. }
  1741. // if, for , while , do の閉じ判定
  1742. // flag == 1 : 閉じられた
  1743. // flag == 0 : 閉じられない
  1744. const char* parse_syntax_close_sub(const char* p,int* flag)
  1745. {
  1746. char label[256];
  1747. int pos = syntax.curly_count - 1;
  1748. int l;
  1749. *flag = 1;
  1750. if(syntax.curly_count <= 0) {
  1751. *flag = 0;
  1752. return p;
  1753. } else if(syntax.curly[pos].type == TYPE_IF) {
  1754. const char *bp = p;
  1755. const char *p2;
  1756. // if-block and else-block end is a new line
  1757. parse_nextline(false, p);
  1758. // if 最終場所へ飛ばす
  1759. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1760. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1761. parse_line(label);
  1762. syntax.curly_count--;
  1763. // 現在地のラベルを付ける
  1764. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1765. l=add_str(label);
  1766. set_label(l,script_pos,p);
  1767. syntax.curly[pos].count++;
  1768. p = skip_space(p);
  1769. p2 = skip_word(p);
  1770. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1771. // else or else - if
  1772. p = skip_space(p2);
  1773. p2 = skip_word(p);
  1774. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1775. // else - if
  1776. p=skip_space(p2);
  1777. if(*p != '(') {
  1778. disp_error_message("need '('",p);
  1779. }
  1780. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1781. add_scriptl(add_str("jump_zero"));
  1782. add_scriptc(C_ARG);
  1783. p=parse_expr(p);
  1784. p=skip_space(p);
  1785. add_scriptl(add_str(label));
  1786. add_scriptc(C_FUNC);
  1787. *flag = 0;
  1788. return p;
  1789. } else {
  1790. // else
  1791. if(!syntax.curly[pos].flag) {
  1792. syntax.curly[pos].flag = 1;
  1793. *flag = 0;
  1794. return p;
  1795. }
  1796. }
  1797. }
  1798. // if 閉じ
  1799. syntax.curly_count--;
  1800. // 最終地のラベルを付ける
  1801. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1802. l=add_str(label);
  1803. set_label(l,script_pos,p);
  1804. if(syntax.curly[pos].flag == 1) {
  1805. // このifに対するelseじゃないのでポインタの位置は同じ
  1806. return bp;
  1807. }
  1808. return p;
  1809. } else if(syntax.curly[pos].type == TYPE_DO) {
  1810. int l;
  1811. char label[256];
  1812. const char *p2;
  1813. if(syntax.curly[pos].flag) {
  1814. // 現在地のラベル形成する(continue でここに来る)
  1815. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1816. l=add_str(label);
  1817. set_label(l,script_pos,p);
  1818. }
  1819. // 条件が偽なら終了地点に飛ばす
  1820. p = skip_space(p);
  1821. p2 = skip_word(p);
  1822. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1823. disp_error_message("parse_syntax: need 'while'",p);
  1824. p = skip_space(p2);
  1825. if(*p != '(') {
  1826. disp_error_message("need '('",p);
  1827. }
  1828. // do-block end is a new line
  1829. parse_nextline(false, p);
  1830. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1831. add_scriptl(add_str("jump_zero"));
  1832. add_scriptc(C_ARG);
  1833. p=parse_expr(p);
  1834. p=skip_space(p);
  1835. add_scriptl(add_str(label));
  1836. add_scriptc(C_FUNC);
  1837. // 開始地点に飛ばす
  1838. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1839. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1840. parse_line(label);
  1841. syntax.curly_count--;
  1842. // 条件終了地点のラベル形成する
  1843. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1844. l=add_str(label);
  1845. set_label(l,script_pos,p);
  1846. p = skip_space(p);
  1847. if(*p != ';') {
  1848. disp_error_message("parse_syntax: need ';'",p);
  1849. return p+1;
  1850. }
  1851. p++;
  1852. syntax.curly_count--;
  1853. return p;
  1854. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1855. // for-block end is a new line
  1856. parse_nextline(false, p);
  1857. // 次のループに飛ばす
  1858. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1859. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1860. parse_line(label);
  1861. syntax.curly_count--;
  1862. // for 終了のラベル付け
  1863. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1864. l=add_str(label);
  1865. set_label(l,script_pos,p);
  1866. syntax.curly_count--;
  1867. return p;
  1868. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1869. // while-block end is a new line
  1870. parse_nextline(false, p);
  1871. // while 条件判断へ飛ばす
  1872. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1873. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1874. parse_line(label);
  1875. syntax.curly_count--;
  1876. // while 終了のラベル付け
  1877. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1878. l=add_str(label);
  1879. set_label(l,script_pos,p);
  1880. syntax.curly_count--;
  1881. return p;
  1882. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1883. int pos = syntax.curly_count-1;
  1884. char label[256];
  1885. int l;
  1886. // 戻す
  1887. sprintf(label,"return;");
  1888. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1889. parse_line(label);
  1890. syntax.curly_count--;
  1891. // 現在地のラベルを付ける
  1892. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1893. l=add_str(label);
  1894. set_label(l,script_pos,p);
  1895. syntax.curly_count--;
  1896. return p;
  1897. } else {
  1898. *flag = 0;
  1899. return p;
  1900. }
  1901. }
  1902. /*==========================================
  1903. * 組み込み関数の追加
  1904. *------------------------------------------*/
  1905. static void add_buildin_func(void)
  1906. {
  1907. int i,n;
  1908. const char* p;
  1909. for( i = 0; buildin_func[i].func; i++ )
  1910. {
  1911. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1912. // 'v' - value (either string or int or reference)
  1913. // 's' - string
  1914. // 'i' - int
  1915. // 'r' - reference (of a variable)
  1916. // 'l' - label
  1917. // '?' - one optional parameter
  1918. // '*' - unknown number of optional parameters
  1919. p = buildin_func[i].arg;
  1920. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1921. while( *p == '?' ) ++p;
  1922. if( *p == '*' ) ++p;
  1923. if( *p != 0){
  1924. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1925. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1926. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1927. } else {
  1928. n = add_str(buildin_func[i].name);
  1929. str_data[n].type = C_FUNC;
  1930. str_data[n].val = i;
  1931. str_data[n].func = buildin_func[i].func;
  1932. if( !strcmp(buildin_func[i].name, "set") ) buildin_set_ref = n; else
  1933. if( !strcmp(buildin_func[i].name, "callsub") ) buildin_callsub_ref = n; else
  1934. if( !strcmp(buildin_func[i].name, "callfunc") ) buildin_callfunc_ref = n; else
  1935. if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1936. }
  1937. }
  1938. }
  1939. /// Retrieves the value of a constant.
  1940. bool script_get_constant(const char* name, int* value)
  1941. {
  1942. int n = search_str(name);
  1943. if( n == -1 || str_data[n].type != C_INT )
  1944. {// not found or not a constant
  1945. return false;
  1946. }
  1947. value[0] = str_data[n].val;
  1948. return true;
  1949. }
  1950. /// Creates new constant or parameter with given value.
  1951. void script_set_constant(const char* name, int value, bool isparameter)
  1952. {
  1953. int n = add_str(name);
  1954. if( str_data[n].type == C_NOP )
  1955. {// new
  1956. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1957. str_data[n].val = value;
  1958. }
  1959. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1960. {// existing parameter or constant
  1961. 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);
  1962. }
  1963. else
  1964. {// existing name
  1965. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1966. }
  1967. }
  1968. /*==========================================
  1969. * 定数データベースの読み込み
  1970. *------------------------------------------*/
  1971. static void read_constdb(void)
  1972. {
  1973. FILE *fp;
  1974. char line[1024],name[1024],val[1024];
  1975. int type;
  1976. sprintf(line, "%s/const.txt", db_path);
  1977. fp=fopen(line, "r");
  1978. if(fp==NULL){
  1979. ShowError("can't read %s\n", line);
  1980. return ;
  1981. }
  1982. while(fgets(line, sizeof(line), fp))
  1983. {
  1984. if(line[0]=='/' && line[1]=='/')
  1985. continue;
  1986. type=0;
  1987. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1988. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1989. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  1990. }
  1991. }
  1992. fclose(fp);
  1993. }
  1994. /*==========================================
  1995. * エラー表示
  1996. *------------------------------------------*/
  1997. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1998. {
  1999. int i;
  2000. if( p == NULL || !p[0] ) return NULL;
  2001. if( line < 0 )
  2002. StringBuf_Printf(buf, "*% 5d : ", -line);
  2003. else
  2004. StringBuf_Printf(buf, " % 5d : ", line);
  2005. for(i=0;p[i] && p[i] != '\n';i++){
  2006. if(p + i != mark)
  2007. StringBuf_Printf(buf, "%c", p[i]);
  2008. else
  2009. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2010. }
  2011. StringBuf_AppendStr(buf, "\n");
  2012. return p+i+(p[i] == '\n' ? 1 : 0);
  2013. }
  2014. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  2015. {
  2016. // エラーが発生した行を求める
  2017. int j;
  2018. int line = start_line;
  2019. const char *p;
  2020. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2021. StringBuf buf;
  2022. for(p=src;p && *p;line++){
  2023. const char *lineend=strchr(p,'\n');
  2024. if(lineend==NULL || error_pos<lineend){
  2025. break;
  2026. }
  2027. for( j = 0; j < 4; j++ ) {
  2028. linestart[j] = linestart[j+1];
  2029. }
  2030. linestart[4] = p;
  2031. p=lineend+1;
  2032. }
  2033. StringBuf_Init(&buf);
  2034. StringBuf_AppendStr(&buf, "\a\n");
  2035. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  2036. StringBuf_Printf(&buf, " %s\n", error_msg);
  2037. for(j = 0; j < 5; j++ ) {
  2038. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  2039. }
  2040. p = script_print_line(&buf, p, error_pos, -line);
  2041. for(j = 0; j < 5; j++) {
  2042. p = script_print_line(&buf, p, NULL, line + j + 1 );
  2043. }
  2044. ShowError("%s", StringBuf_Value(&buf));
  2045. StringBuf_Destroy(&buf);
  2046. }
  2047. /*==========================================
  2048. * スクリプトの解析
  2049. *------------------------------------------*/
  2050. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2051. {
  2052. const char *p,*tmpp;
  2053. int i;
  2054. struct script_code* code = NULL;
  2055. static int first=1;
  2056. char end;
  2057. bool unresolved_names = false;
  2058. if( src == NULL )
  2059. return NULL;// empty script
  2060. memset(&syntax,0,sizeof(syntax));
  2061. if(first){
  2062. add_buildin_func();
  2063. read_constdb();
  2064. first=0;
  2065. }
  2066. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2067. script_pos=0;
  2068. script_size=SCRIPT_BLOCK_SIZE;
  2069. parse_nextline(true, NULL);
  2070. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2071. if( options&SCRIPT_USE_LABEL_DB )
  2072. db_clear(scriptlabel_db);
  2073. parse_options = options;
  2074. if( setjmp( error_jump ) != 0 ) {
  2075. //Restore program state when script has problems. [from jA]
  2076. int i;
  2077. const int size = ARRAYLENGTH(syntax.curly);
  2078. if( error_report )
  2079. script_error(src,file,line,error_msg,error_pos);
  2080. aFree( error_msg );
  2081. aFree( script_buf );
  2082. script_pos = 0;
  2083. script_size = 0;
  2084. script_buf = NULL;
  2085. for(i=LABEL_START;i<str_num;i++)
  2086. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  2087. for(i=0; i<size; i++)
  2088. linkdb_final(&syntax.curly[i].case_label);
  2089. return NULL;
  2090. }
  2091. parse_syntax_for_flag=0;
  2092. p=src;
  2093. p=skip_space(p);
  2094. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2095. {// does not require brackets around the script
  2096. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2097. {// empty script and can return NULL
  2098. aFree( script_buf );
  2099. script_pos = 0;
  2100. script_size = 0;
  2101. script_buf = NULL;
  2102. return NULL;
  2103. }
  2104. end = '\0';
  2105. }
  2106. else
  2107. {// requires brackets around the script
  2108. if( *p != '{' )
  2109. disp_error_message("not found '{'",p);
  2110. p = skip_space(p+1);
  2111. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2112. {// empty script and can return NULL
  2113. aFree( script_buf );
  2114. script_pos = 0;
  2115. script_size = 0;
  2116. script_buf = NULL;
  2117. return NULL;
  2118. }
  2119. end = '}';
  2120. }
  2121. // clear references of labels, variables and internal functions
  2122. for(i=LABEL_START;i<str_num;i++){
  2123. if(
  2124. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2125. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2126. ){
  2127. str_data[i].type=C_NOP;
  2128. str_data[i].backpatch=-1;
  2129. str_data[i].label=-1;
  2130. }
  2131. }
  2132. while( syntax.curly_count != 0 || *p != end )
  2133. {
  2134. if( *p == '\0' )
  2135. disp_error_message("unexpected end of script",p);
  2136. // labelだけ特殊処理
  2137. tmpp=skip_space(skip_word(p));
  2138. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2139. i=add_word(p);
  2140. set_label(i,script_pos,p);
  2141. if( parse_options&SCRIPT_USE_LABEL_DB )
  2142. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2143. p=tmpp+1;
  2144. p=skip_space(p);
  2145. continue;
  2146. }
  2147. // 他は全部一緒くた
  2148. p=parse_line(p);
  2149. p=skip_space(p);
  2150. parse_nextline(false, p);
  2151. }
  2152. add_scriptc(C_NOP);
  2153. // trim code to size
  2154. script_size = script_pos;
  2155. RECREATE(script_buf,unsigned char,script_pos);
  2156. // default unknown references to variables
  2157. for(i=LABEL_START;i<str_num;i++){
  2158. if(str_data[i].type==C_NOP){
  2159. int j,next;
  2160. str_data[i].type=C_NAME;
  2161. str_data[i].label=i;
  2162. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2163. next=GETVALUE(script_buf,j);
  2164. SETVALUE(script_buf,j,i);
  2165. j=next;
  2166. }
  2167. }
  2168. else if( str_data[i].type == C_USERFUNC )
  2169. {// 'function name;' without follow-up code
  2170. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2171. unresolved_names = true;
  2172. }
  2173. }
  2174. if( unresolved_names )
  2175. {
  2176. disp_error_message("parse_script: unresolved function references", p);
  2177. }
  2178. #ifdef DEBUG_DISP
  2179. for(i=0;i<script_pos;i++){
  2180. if((i&15)==0) ShowMessage("%04x : ",i);
  2181. ShowMessage("%02x ",script_buf[i]);
  2182. if((i&15)==15) ShowMessage("\n");
  2183. }
  2184. ShowMessage("\n");
  2185. #endif
  2186. #ifdef DEBUG_DISASM
  2187. {
  2188. int i = 0,j;
  2189. while(i < script_pos) {
  2190. c_op op = get_com(script_buf,&i);
  2191. ShowMessage("%06x %s", i, script_op2name(op));
  2192. j = i;
  2193. switch(op) {
  2194. case C_INT:
  2195. ShowMessage(" %d", get_num(script_buf,&i));
  2196. break;
  2197. case C_POS:
  2198. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2199. i += 3;
  2200. break;
  2201. case C_NAME:
  2202. j = (*(int*)(script_buf+i)&0xffffff);
  2203. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2204. i += 3;
  2205. break;
  2206. case C_STR:
  2207. j = strlen(script_buf + i);
  2208. ShowMessage(" %s", script_buf + i);
  2209. i += j+1;
  2210. break;
  2211. }
  2212. ShowMessage(CL_CLL"\n");
  2213. }
  2214. }
  2215. #endif
  2216. CREATE(code,struct script_code,1);
  2217. code->script_buf = script_buf;
  2218. code->script_size = script_size;
  2219. code->script_vars = NULL;
  2220. return code;
  2221. }
  2222. /// Returns the player attached to this script, identified by the rid.
  2223. /// If there is no player attached, the script is terminated.
  2224. TBL_PC *script_rid2sd(struct script_state *st)
  2225. {
  2226. TBL_PC *sd=map_id2sd(st->rid);
  2227. if(!sd){
  2228. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2229. script_reportfunc(st);
  2230. script_reportsrc(st);
  2231. st->state = END;
  2232. }
  2233. return sd;
  2234. }
  2235. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2236. ///
  2237. /// @param st Script state
  2238. /// @param data Variable/constant
  2239. void get_val(struct script_state* st, struct script_data* data)
  2240. {
  2241. const char* name;
  2242. char prefix;
  2243. char postfix;
  2244. TBL_PC* sd = NULL;
  2245. if( !data_isreference(data) )
  2246. return;// not a variable/constant
  2247. name = reference_getname(data);
  2248. prefix = name[0];
  2249. postfix = name[strlen(name) - 1];
  2250. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2251. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2252. {
  2253. sd = script_rid2sd(st);
  2254. if( sd == NULL )
  2255. {// needs player attached
  2256. if( postfix == '$' )
  2257. {// string variable
  2258. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2259. data->type = C_CONSTSTR;
  2260. data->u.str = "";
  2261. }
  2262. else
  2263. {// integer variable
  2264. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2265. data->type = C_INT;
  2266. data->u.num = 0;
  2267. }
  2268. return;
  2269. }
  2270. }
  2271. if( postfix == '$' )
  2272. {// string variable
  2273. switch( prefix )
  2274. {
  2275. case '@':
  2276. data->u.str = pc_readregstr(sd, data->u.num);
  2277. break;
  2278. case '$':
  2279. data->u.str = mapreg_readregstr(data->u.num);
  2280. break;
  2281. case '#':
  2282. if( name[1] == '#' )
  2283. data->u.str = pc_readaccountreg2str(sd, name);// global
  2284. else
  2285. data->u.str = pc_readaccountregstr(sd, name);// local
  2286. break;
  2287. case '.':
  2288. {
  2289. struct DBMap* n =
  2290. data->ref ? *data->ref:
  2291. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2292. st->script->script_vars;// npc variable
  2293. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2294. }
  2295. break;
  2296. case '\'':
  2297. if (st->instance_id) {
  2298. data->u.str = (char*)idb_get(instance[st->instance_id].vars,reference_getuid(data));
  2299. } else {
  2300. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2301. data->u.str = NULL;
  2302. }
  2303. break;
  2304. default:
  2305. data->u.str = pc_readglobalreg_str(sd, name);
  2306. break;
  2307. }
  2308. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2309. {// empty string
  2310. data->type = C_CONSTSTR;
  2311. data->u.str = "";
  2312. }
  2313. else
  2314. {// duplicate string
  2315. data->type = C_STR;
  2316. data->u.str = aStrdup(data->u.str);
  2317. }
  2318. }
  2319. else
  2320. {// integer variable
  2321. data->type = C_INT;
  2322. if( reference_toconstant(data) )
  2323. {
  2324. data->u.num = reference_getconstant(data);
  2325. }
  2326. else if( reference_toparam(data) )
  2327. {
  2328. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2329. }
  2330. else
  2331. switch( prefix )
  2332. {
  2333. case '@':
  2334. data->u.num = pc_readreg(sd, data->u.num);
  2335. break;
  2336. case '$':
  2337. data->u.num = mapreg_readreg(data->u.num);
  2338. break;
  2339. case '#':
  2340. if( name[1] == '#' )
  2341. data->u.num = pc_readaccountreg2(sd, name);// global
  2342. else
  2343. data->u.num = pc_readaccountreg(sd, name);// local
  2344. break;
  2345. case '.':
  2346. {
  2347. struct DBMap* n =
  2348. data->ref ? *data->ref:
  2349. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2350. st->script->script_vars;// npc variable
  2351. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2352. }
  2353. break;
  2354. case '\'':
  2355. if( st->instance_id )
  2356. data->u.num = (int)idb_iget(instance[st->instance_id].vars,reference_getuid(data));
  2357. else {
  2358. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2359. data->u.num = 0;
  2360. }
  2361. break;
  2362. default:
  2363. data->u.num = pc_readglobalreg(sd, name);
  2364. break;
  2365. }
  2366. }
  2367. return;
  2368. }
  2369. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2370. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2371. /// The value is left in the top of the stack and needs to be removed manually.
  2372. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2373. {
  2374. struct script_data* data;
  2375. push_val2(st->stack, C_NAME, uid, ref);
  2376. data = script_getdatatop(st, -1);
  2377. get_val(st, data);
  2378. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2379. }
  2380. /*==========================================
  2381. * Stores the value of a script variable
  2382. * Return value is 0 on fail, 1 on success.
  2383. *------------------------------------------*/
  2384. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2385. {
  2386. char prefix = name[0];
  2387. if( is_string_variable(name) )
  2388. {// string variable
  2389. const char* str = (const char*)value;
  2390. switch (prefix) {
  2391. case '@':
  2392. return pc_setregstr(sd, num, str);
  2393. case '$':
  2394. return mapreg_setregstr(num, str);
  2395. case '#':
  2396. return (name[1] == '#') ?
  2397. pc_setaccountreg2str(sd, name, str) :
  2398. pc_setaccountregstr(sd, name, str);
  2399. case '.':
  2400. {
  2401. struct DBMap* n;
  2402. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2403. if( n ) {
  2404. idb_remove(n, num);
  2405. if (str[0]) idb_put(n, num, aStrdup(str));
  2406. }
  2407. }
  2408. return 1;
  2409. case '\'':
  2410. if( st->instance_id ) {
  2411. idb_remove(instance[st->instance_id].vars, num);
  2412. if( str[0] ) idb_put(instance[st->instance_id].vars, num, aStrdup(str));
  2413. }
  2414. return 1;
  2415. default:
  2416. return pc_setglobalreg_str(sd, name, str);
  2417. }
  2418. }
  2419. else
  2420. {// integer variable
  2421. int val = (int)__64BPRTSIZE(value);
  2422. if(str_data[num&0x00ffffff].type == C_PARAM)
  2423. {
  2424. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2425. {
  2426. if( st != NULL )
  2427. {
  2428. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2429. script_reportsrc(st);
  2430. st->state = END;
  2431. }
  2432. return 0;
  2433. }
  2434. return 1;
  2435. }
  2436. switch (prefix) {
  2437. case '@':
  2438. return pc_setreg(sd, num, val);
  2439. case '$':
  2440. return mapreg_setreg(num, val);
  2441. case '#':
  2442. return (name[1] == '#') ?
  2443. pc_setaccountreg2(sd, name, val) :
  2444. pc_setaccountreg(sd, name, val);
  2445. case '.':
  2446. {
  2447. struct DBMap* n;
  2448. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2449. if( n ) {
  2450. idb_remove(n, num);
  2451. if( val != 0 )
  2452. idb_iput(n, num, val);
  2453. }
  2454. }
  2455. return 1;
  2456. case '\'':
  2457. if( st->instance_id ) {
  2458. idb_remove(instance[st->instance_id].vars, num);
  2459. if( val != 0 )
  2460. idb_iput(instance[st->instance_id].vars, num, val);
  2461. }
  2462. return 1;
  2463. default:
  2464. return pc_setglobalreg(sd, name, val);
  2465. }
  2466. }
  2467. }
  2468. int set_var(TBL_PC* sd, char* name, void* val)
  2469. {
  2470. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2471. }
  2472. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2473. {
  2474. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2475. }
  2476. /// Converts the data to a string
  2477. const char* conv_str(struct script_state* st, struct script_data* data)
  2478. {
  2479. char* p;
  2480. get_val(st, data);
  2481. if( data_isstring(data) )
  2482. {// nothing to convert
  2483. }
  2484. else if( data_isint(data) )
  2485. {// int -> string
  2486. CREATE(p, char, ITEM_NAME_LENGTH);
  2487. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2488. p[ITEM_NAME_LENGTH-1] = '\0';
  2489. data->type = C_STR;
  2490. data->u.str = p;
  2491. }
  2492. else if( data_isreference(data) )
  2493. {// reference -> string
  2494. //##TODO when does this happen (check get_val) [FlavioJS]
  2495. data->type = C_CONSTSTR;
  2496. data->u.str = reference_getname(data);
  2497. }
  2498. else
  2499. {// unsupported data type
  2500. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2501. script_reportdata(data);
  2502. script_reportsrc(st);
  2503. data->type = C_CONSTSTR;
  2504. data->u.str = "";
  2505. }
  2506. return data->u.str;
  2507. }
  2508. /// Converts the data to an int
  2509. int conv_num(struct script_state* st, struct script_data* data)
  2510. {
  2511. char* p;
  2512. long num;
  2513. get_val(st, data);
  2514. if( data_isint(data) )
  2515. {// nothing to convert
  2516. }
  2517. else if( data_isstring(data) )
  2518. {// string -> int
  2519. // the result does not overflow or underflow, it is capped instead
  2520. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2521. p = data->u.str;
  2522. errno = 0;
  2523. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2524. if( errno == ERANGE
  2525. #if LONG_MAX > INT_MAX
  2526. || num < INT_MIN || num > INT_MAX
  2527. #endif
  2528. )
  2529. {
  2530. if( num <= INT_MIN )
  2531. {
  2532. num = INT_MIN;
  2533. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2534. }
  2535. else//if( num >= INT_MAX )
  2536. {
  2537. num = INT_MAX;
  2538. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2539. }
  2540. script_reportdata(data);
  2541. script_reportsrc(st);
  2542. }
  2543. if( data->type == C_STR )
  2544. aFree(p);
  2545. data->type = C_INT;
  2546. data->u.num = (int)num;
  2547. }
  2548. #if 0
  2549. // FIXME this function is being used to retrieve the position of labels and
  2550. // probably other stuff [FlavioJS]
  2551. else
  2552. {// unsupported data type
  2553. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2554. script_reportdata(data);
  2555. script_reportsrc(st);
  2556. data->type = C_INT;
  2557. data->u.num = 0;
  2558. }
  2559. #endif
  2560. return data->u.num;
  2561. }
  2562. //
  2563. // Stack operations
  2564. //
  2565. /// Increases the size of the stack
  2566. void stack_expand(struct script_stack* stack)
  2567. {
  2568. stack->sp_max += 64;
  2569. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2570. stack->sp_max * sizeof(stack->stack_data[0]) );
  2571. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2572. 64 * sizeof(stack->stack_data[0]) );
  2573. }
  2574. /// Pushes a value into the stack
  2575. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2576. /// Pushes a value into the stack (with reference)
  2577. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2578. {
  2579. if( stack->sp >= stack->sp_max )
  2580. stack_expand(stack);
  2581. stack->stack_data[stack->sp].type = type;
  2582. stack->stack_data[stack->sp].u.num = val;
  2583. stack->stack_data[stack->sp].ref = ref;
  2584. stack->sp++;
  2585. return &stack->stack_data[stack->sp-1];
  2586. }
  2587. /// Pushes a string into the stack
  2588. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2589. {
  2590. if( stack->sp >= stack->sp_max )
  2591. stack_expand(stack);
  2592. stack->stack_data[stack->sp].type = type;
  2593. stack->stack_data[stack->sp].u.str = str;
  2594. stack->stack_data[stack->sp].ref = NULL;
  2595. stack->sp++;
  2596. return &stack->stack_data[stack->sp-1];
  2597. }
  2598. /// Pushes a retinfo into the stack
  2599. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2600. {
  2601. if( stack->sp >= stack->sp_max )
  2602. stack_expand(stack);
  2603. stack->stack_data[stack->sp].type = C_RETINFO;
  2604. stack->stack_data[stack->sp].u.ri = ri;
  2605. stack->stack_data[stack->sp].ref = NULL;
  2606. stack->sp++;
  2607. return &stack->stack_data[stack->sp-1];
  2608. }
  2609. /// Pushes a copy of the target position into the stack
  2610. struct script_data* push_copy(struct script_stack* stack, int pos)
  2611. {
  2612. switch( stack->stack_data[pos].type )
  2613. {
  2614. case C_CONSTSTR:
  2615. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2616. break;
  2617. case C_STR:
  2618. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2619. break;
  2620. case C_RETINFO:
  2621. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2622. exit(1);
  2623. break;
  2624. default:
  2625. return push_val2(
  2626. stack,stack->stack_data[pos].type,
  2627. stack->stack_data[pos].u.num,
  2628. stack->stack_data[pos].ref
  2629. );
  2630. break;
  2631. }
  2632. }
  2633. /// Removes the values in indexes [start,end[ from the stack.
  2634. /// Adjusts all stack pointers.
  2635. void pop_stack(struct script_state* st, int start, int end)
  2636. {
  2637. struct script_stack* stack = st->stack;
  2638. struct script_data* data;
  2639. int i;
  2640. if( start < 0 )
  2641. start = 0;
  2642. if( end > stack->sp )
  2643. end = stack->sp;
  2644. if( start >= end )
  2645. return;// nothing to pop
  2646. // free stack elements
  2647. for( i = start; i < end; i++ )
  2648. {
  2649. data = &stack->stack_data[i];
  2650. if( data->type == C_STR )
  2651. aFree(data->u.str);
  2652. if( data->type == C_RETINFO )
  2653. {
  2654. struct script_retinfo* ri = data->u.ri;
  2655. if( ri->var_function )
  2656. script_free_vars(ri->var_function);
  2657. aFree(ri);
  2658. }
  2659. data->type = C_NOP;
  2660. }
  2661. // move the rest of the elements
  2662. if( stack->sp > end )
  2663. {
  2664. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2665. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2666. stack->stack_data[i].type = C_NOP;
  2667. }
  2668. // adjust stack pointers
  2669. if( st->start > end ) st->start -= end - start;
  2670. else if( st->start > start ) st->start = start;
  2671. if( st->end > end ) st->end -= end - start;
  2672. else if( st->end > start ) st->end = start;
  2673. if( stack->defsp > end ) stack->defsp -= end - start;
  2674. else if( stack->defsp > start ) stack->defsp = start;
  2675. stack->sp -= end - start;
  2676. }
  2677. ///
  2678. ///
  2679. ///
  2680. /*==========================================
  2681. * スクリプト依存変数、関数依存変数の解放
  2682. *------------------------------------------*/
  2683. void script_free_vars(struct DBMap* storage)
  2684. {
  2685. if( storage )
  2686. {// destroy the storage construct containing the variables
  2687. db_destroy(storage);
  2688. }
  2689. }
  2690. void script_free_code(struct script_code* code)
  2691. {
  2692. script_free_vars( code->script_vars );
  2693. aFree( code->script_buf );
  2694. aFree( code );
  2695. }
  2696. /// Creates a new script state.
  2697. ///
  2698. /// @param script Script code
  2699. /// @param pos Position in the code
  2700. /// @param rid Who is running the script (attached player)
  2701. /// @param oid Where the code is being run (npc 'object')
  2702. /// @return Script state
  2703. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2704. {
  2705. struct script_state* st;
  2706. CREATE(st, struct script_state, 1);
  2707. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2708. st->stack->sp = 0;
  2709. st->stack->sp_max = 64;
  2710. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2711. st->stack->defsp = st->stack->sp;
  2712. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2713. st->state = RUN;
  2714. st->script = script;
  2715. //st->scriptroot = script;
  2716. st->pos = pos;
  2717. st->rid = rid;
  2718. st->oid = oid;
  2719. st->sleep.timer = INVALID_TIMER;
  2720. return st;
  2721. }
  2722. /// Frees a script state.
  2723. ///
  2724. /// @param st Script state
  2725. void script_free_state(struct script_state* st)
  2726. {
  2727. if(st->bk_st)
  2728. {// backup was not restored
  2729. 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);
  2730. }
  2731. if( st->sleep.timer != INVALID_TIMER )
  2732. delete_timer(st->sleep.timer, run_script_timer);
  2733. script_free_vars(st->stack->var_function);
  2734. pop_stack(st, 0, st->stack->sp);
  2735. aFree(st->stack->stack_data);
  2736. aFree(st->stack);
  2737. st->pos = -1;
  2738. aFree(st);
  2739. }
  2740. //
  2741. // 実行部main
  2742. //
  2743. /*==========================================
  2744. * コマンドの読み取り
  2745. *------------------------------------------*/
  2746. c_op get_com(unsigned char *script,int *pos)
  2747. {
  2748. int i = 0, j = 0;
  2749. if(script[*pos]>=0x80){
  2750. return C_INT;
  2751. }
  2752. while(script[*pos]>=0x40){
  2753. i=script[(*pos)++]<<j;
  2754. j+=6;
  2755. }
  2756. return (c_op)(i+(script[(*pos)++]<<j));
  2757. }
  2758. /*==========================================
  2759. * 数値の所得
  2760. *------------------------------------------*/
  2761. int get_num(unsigned char *script,int *pos)
  2762. {
  2763. int i,j;
  2764. i=0; j=0;
  2765. while(script[*pos]>=0xc0){
  2766. i+=(script[(*pos)++]&0x7f)<<j;
  2767. j+=6;
  2768. }
  2769. return i+((script[(*pos)++]&0x7f)<<j);
  2770. }
  2771. /*==========================================
  2772. * スタックから値を取り出す
  2773. *------------------------------------------*/
  2774. int pop_val(struct script_state* st)
  2775. {
  2776. if(st->stack->sp<=0)
  2777. return 0;
  2778. st->stack->sp--;
  2779. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2780. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2781. return st->stack->stack_data[st->stack->sp].u.num;
  2782. return 0;
  2783. }
  2784. /// Ternary operators
  2785. /// test ? if_true : if_false
  2786. void op_3(struct script_state* st, int op)
  2787. {
  2788. struct script_data* data;
  2789. int flag = 0;
  2790. data = script_getdatatop(st, -3);
  2791. get_val(st, data);
  2792. if( data_isstring(data) )
  2793. flag = data->u.str[0];// "" -> false
  2794. else if( data_isint(data) )
  2795. flag = data->u.num;// 0 -> false
  2796. else
  2797. {
  2798. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2799. script_reportdata(data);
  2800. script_reportsrc(st);
  2801. script_removetop(st, -3, 0);
  2802. script_pushnil(st);
  2803. return;
  2804. }
  2805. if( flag )
  2806. script_pushcopytop(st, -2);
  2807. else
  2808. script_pushcopytop(st, -1);
  2809. script_removetop(st, -4, -1);
  2810. }
  2811. /// Binary string operators
  2812. /// s1 EQ s2 -> i
  2813. /// s1 NE s2 -> i
  2814. /// s1 GT s2 -> i
  2815. /// s1 GE s2 -> i
  2816. /// s1 LT s2 -> i
  2817. /// s1 LE s2 -> i
  2818. /// s1 ADD s2 -> s
  2819. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2820. {
  2821. int a = 0;
  2822. switch(op){
  2823. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2824. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2825. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2826. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2827. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2828. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2829. case C_ADD:
  2830. {
  2831. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2832. strcpy(buf, s1);
  2833. strcat(buf, s2);
  2834. script_pushstr(st, buf);
  2835. return;
  2836. }
  2837. default:
  2838. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2839. script_reportsrc(st);
  2840. script_pushnil(st);
  2841. st->state = END;
  2842. return;
  2843. }
  2844. script_pushint(st,a);
  2845. }
  2846. /// Binary number operators
  2847. /// i OP i -> i
  2848. void op_2num(struct script_state* st, int op, int i1, int i2)
  2849. {
  2850. int ret;
  2851. double ret_double;
  2852. switch( op )
  2853. {
  2854. case C_AND: ret = i1 & i2; break;
  2855. case C_OR: ret = i1 | i2; break;
  2856. case C_XOR: ret = i1 ^ i2; break;
  2857. case C_LAND: ret = (i1 && i2); break;
  2858. case C_LOR: ret = (i1 || i2); break;
  2859. case C_EQ: ret = (i1 == i2); break;
  2860. case C_NE: ret = (i1 != i2); break;
  2861. case C_GT: ret = (i1 > i2); break;
  2862. case C_GE: ret = (i1 >= i2); break;
  2863. case C_LT: ret = (i1 < i2); break;
  2864. case C_LE: ret = (i1 <= i2); break;
  2865. case C_R_SHIFT: ret = i1>>i2; break;
  2866. case C_L_SHIFT: ret = i1<<i2; break;
  2867. case C_DIV:
  2868. case C_MOD:
  2869. if( i2 == 0 )
  2870. {
  2871. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2872. script_reportsrc(st);
  2873. script_pushnil(st);
  2874. st->state = END;
  2875. return;
  2876. }
  2877. else if( op == C_DIV )
  2878. ret = i1 / i2;
  2879. else//if( op == C_MOD )
  2880. ret = i1 % i2;
  2881. break;
  2882. default:
  2883. switch( op )
  2884. {// operators that can overflow/underflow
  2885. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2886. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2887. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2888. default:
  2889. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2890. script_reportsrc(st);
  2891. script_pushnil(st);
  2892. return;
  2893. }
  2894. if( ret_double < (double)INT_MIN )
  2895. {
  2896. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2897. script_reportsrc(st);
  2898. ret = INT_MIN;
  2899. }
  2900. else if( ret_double > (double)INT_MAX )
  2901. {
  2902. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2903. script_reportsrc(st);
  2904. ret = INT_MAX;
  2905. }
  2906. }
  2907. script_pushint(st, ret);
  2908. }
  2909. /// Binary operators
  2910. void op_2(struct script_state *st, int op)
  2911. {
  2912. struct script_data* left;
  2913. struct script_data* right;
  2914. left = script_getdatatop(st, -2);
  2915. right = script_getdatatop(st, -1);
  2916. get_val(st, left);
  2917. get_val(st, right);
  2918. // automatic conversions
  2919. switch( op )
  2920. {
  2921. case C_ADD:
  2922. if( data_isint(left) && data_isstring(right) )
  2923. {// convert int-string to string-string
  2924. conv_str(st, left);
  2925. }
  2926. else if( data_isstring(left) && data_isint(right) )
  2927. {// convert string-int to string-string
  2928. conv_str(st, right);
  2929. }
  2930. break;
  2931. }
  2932. if( data_isstring(left) && data_isstring(right) )
  2933. {// ss => op_2str
  2934. op_2str(st, op, left->u.str, right->u.str);
  2935. script_removetop(st, -3, -1);// pop the two values before the top one
  2936. }
  2937. else if( data_isint(left) && data_isint(right) )
  2938. {// ii => op_2num
  2939. int i1 = left->u.num;
  2940. int i2 = right->u.num;
  2941. script_removetop(st, -2, 0);
  2942. op_2num(st, op, i1, i2);
  2943. }
  2944. else
  2945. {// invalid argument
  2946. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2947. script_reportdata(left);
  2948. script_reportdata(right);
  2949. script_reportsrc(st);
  2950. script_removetop(st, -2, 0);
  2951. script_pushnil(st);
  2952. st->state = END;
  2953. }
  2954. }
  2955. /// Unary operators
  2956. /// NEG i -> i
  2957. /// NOT i -> i
  2958. /// LNOT i -> i
  2959. void op_1(struct script_state* st, int op)
  2960. {
  2961. struct script_data* data;
  2962. int i1;
  2963. data = script_getdatatop(st, -1);
  2964. get_val(st, data);
  2965. if( !data_isint(data) )
  2966. {// not a number
  2967. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2968. script_reportdata(data);
  2969. script_reportsrc(st);
  2970. script_pushnil(st);
  2971. st->state = END;
  2972. return;
  2973. }
  2974. i1 = data->u.num;
  2975. script_removetop(st, -1, 0);
  2976. switch( op )
  2977. {
  2978. case C_NEG: i1 = -i1; break;
  2979. case C_NOT: i1 = ~i1; break;
  2980. case C_LNOT: i1 = !i1; break;
  2981. default:
  2982. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2983. script_reportsrc(st);
  2984. script_pushnil(st);
  2985. st->state = END;
  2986. return;
  2987. }
  2988. script_pushint(st, i1);
  2989. }
  2990. /// Checks the type of all arguments passed to a built-in function.
  2991. ///
  2992. /// @param st Script state whose stack arguments should be inspected.
  2993. /// @param func Built-in function for which the arguments are intended.
  2994. static void script_check_buildin_argtype(struct script_state* st, int func)
  2995. {
  2996. char type;
  2997. int idx, invalid = 0;
  2998. script_function* sf = &buildin_func[str_data[func].val];
  2999. for( idx = 2; script_hasdata(st, idx); idx++ )
  3000. {
  3001. struct script_data* data = script_getdata(st, idx);
  3002. type = sf->arg[idx-2];
  3003. if( type == '?' || type == '*' )
  3004. {// optional argument or unknown number of optional parameters ( no types are after this )
  3005. break;
  3006. }
  3007. else if( type == 0 )
  3008. {// more arguments than necessary ( should not happen, as it is checked before )
  3009. ShowWarning("Found more arguments than necessary.\n");
  3010. invalid++;
  3011. break;
  3012. }
  3013. else
  3014. {
  3015. const char* name = NULL;
  3016. if( data_isreference(data) )
  3017. {// get name for variables to determine the type they refer to
  3018. name = reference_getname(data);
  3019. }
  3020. switch( type )
  3021. {
  3022. case 'v':
  3023. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3024. {// variant
  3025. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3026. script_reportdata(data);
  3027. invalid++;
  3028. }
  3029. break;
  3030. case 's':
  3031. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3032. {// string
  3033. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3034. script_reportdata(data);
  3035. invalid++;
  3036. }
  3037. break;
  3038. case 'i':
  3039. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3040. {// int ( params and constants are always int )
  3041. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3042. script_reportdata(data);
  3043. invalid++;
  3044. }
  3045. break;
  3046. case 'r':
  3047. if( !data_isreference(data) )
  3048. {// variables
  3049. ShowWarning("Unexpected type for argument %d. Expected variable.\n", idx-1);
  3050. script_reportdata(data);
  3051. invalid++;
  3052. }
  3053. break;
  3054. case 'l':
  3055. if( !data_islabel(data) && !data_isfunclabel(data) )
  3056. {// label
  3057. ShowWarning("Unexpected type for argument %d. Expected label.\n", idx-1);
  3058. script_reportdata(data);
  3059. invalid++;
  3060. }
  3061. break;
  3062. }
  3063. }
  3064. }
  3065. if(invalid)
  3066. {
  3067. ShowDebug("Function: %s\n", get_str(func));
  3068. script_reportsrc(st);
  3069. }
  3070. }
  3071. /// Executes a buildin command.
  3072. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3073. int run_func(struct script_state *st)
  3074. {
  3075. struct script_data* data;
  3076. int i,start_sp,end_sp,func;
  3077. end_sp = st->stack->sp;// position after the last argument
  3078. for( i = end_sp-1; i > 0 ; --i )
  3079. if( st->stack->stack_data[i].type == C_ARG )
  3080. break;
  3081. if( i == 0 )
  3082. {
  3083. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3084. st->state = END;
  3085. script_reportsrc(st);
  3086. return 1;
  3087. }
  3088. start_sp = i-1;// C_NAME of the command
  3089. st->start = start_sp;
  3090. st->end = end_sp;
  3091. data = &st->stack->stack_data[st->start];
  3092. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  3093. func = data->u.num;
  3094. else
  3095. {
  3096. ShowError("script:run_func: not a buildin command.\n");
  3097. script_reportdata(data);
  3098. script_reportsrc(st);
  3099. st->state = END;
  3100. return 1;
  3101. }
  3102. if( script_config.warn_func_mismatch_argtypes )
  3103. {
  3104. script_check_buildin_argtype(st, func);
  3105. }
  3106. if(str_data[func].func){
  3107. if (str_data[func].func(st)) //Report error
  3108. script_reportsrc(st);
  3109. } else {
  3110. 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));
  3111. script_reportsrc(st);
  3112. st->state = END;
  3113. }
  3114. // Stack's datum are used when re-running functions [Eoe]
  3115. if( st->state == RERUNLINE )
  3116. return 0;
  3117. pop_stack(st, st->start, st->end);
  3118. if( st->state == RETFUNC )
  3119. {// return from a user-defined function
  3120. struct script_retinfo* ri;
  3121. int olddefsp = st->stack->defsp;
  3122. int nargs;
  3123. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3124. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3125. {
  3126. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3127. script_reportsrc(st);
  3128. st->state = END;
  3129. return 1;
  3130. }
  3131. script_free_vars( st->stack->var_function );
  3132. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3133. nargs = ri->nargs;
  3134. st->pos = ri->pos;
  3135. st->script = ri->script;
  3136. st->stack->var_function = ri->var_function;
  3137. st->stack->defsp = ri->defsp;
  3138. memset(ri, 0, sizeof(struct script_retinfo));
  3139. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3140. st->state = GOTO;
  3141. }
  3142. return 0;
  3143. }
  3144. /*==========================================
  3145. * script execution
  3146. *------------------------------------------*/
  3147. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3148. {
  3149. struct script_state *st;
  3150. if( rootscript == NULL || pos < 0 )
  3151. return;
  3152. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3153. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3154. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3155. st = script_alloc_state(rootscript, pos, rid, oid);
  3156. run_script_main(st);
  3157. }
  3158. void script_stop_sleeptimers(int id)
  3159. {
  3160. struct script_state* st;
  3161. for(;;)
  3162. {
  3163. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3164. if( st == NULL )
  3165. break; // no more sleep timers
  3166. script_free_state(st);
  3167. }
  3168. }
  3169. /*==========================================
  3170. * 指定ノードをsleep_dbから削除
  3171. *------------------------------------------*/
  3172. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3173. {
  3174. struct linkdb_node *retnode;
  3175. if( n == NULL)
  3176. return NULL;
  3177. if( n->prev == NULL )
  3178. sleep_db = n->next;
  3179. else
  3180. n->prev->next = n->next;
  3181. if( n->next )
  3182. n->next->prev = n->prev;
  3183. retnode = n->next;
  3184. aFree( n );
  3185. return retnode; // 次のノードを返す
  3186. }
  3187. /*==========================================
  3188. * sleep用タイマー関数
  3189. *------------------------------------------*/
  3190. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3191. {
  3192. struct script_state *st = (struct script_state *)data;
  3193. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3194. TBL_PC *sd = map_id2sd(st->rid);
  3195. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3196. { //Character mismatch. Cancel execution.
  3197. st->rid = 0;
  3198. st->state = END;
  3199. }
  3200. while( node && st->sleep.timer != INVALID_TIMER ) {
  3201. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3202. script_erase_sleepdb(node);
  3203. st->sleep.timer = INVALID_TIMER;
  3204. break;
  3205. }
  3206. node = node->next;
  3207. }
  3208. if(st->state != RERUNLINE)
  3209. st->sleep.tick = 0;
  3210. run_script_main(st);
  3211. return 0;
  3212. }
  3213. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3214. ///
  3215. /// @param st Script state to detach.
  3216. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3217. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3218. {
  3219. struct map_session_data* sd;
  3220. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3221. {
  3222. sd->st = st->bk_st;
  3223. sd->npc_id = st->bk_npcid;
  3224. /**
  3225. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3226. **/
  3227. #if SECURE_NPCTIMEOUT
  3228. /**
  3229. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3230. **/
  3231. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3232. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3233. sd->npc_idle_timer = INVALID_TIMER;
  3234. }
  3235. #endif
  3236. if(st->bk_st)
  3237. {
  3238. //Remove tag for removal.
  3239. st->bk_st = NULL;
  3240. st->bk_npcid = 0;
  3241. }
  3242. else if(dequeue_event)
  3243. {
  3244. npc_event_dequeue(sd);
  3245. }
  3246. }
  3247. else if(st->bk_st)
  3248. {// rid was set to 0, before detaching the script state
  3249. 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);
  3250. script_reportsrc(st->bk_st);
  3251. script_free_state(st->bk_st);
  3252. st->bk_st = NULL;
  3253. }
  3254. }
  3255. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3256. ///
  3257. /// @param st Script state to attach.
  3258. static void script_attach_state(struct script_state* st)
  3259. {
  3260. struct map_session_data* sd;
  3261. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3262. {
  3263. if(st!=sd->st)
  3264. {
  3265. if(st->bk_st)
  3266. {// there is already a backup
  3267. 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);
  3268. }
  3269. st->bk_st = sd->st;
  3270. st->bk_npcid = sd->npc_id;
  3271. }
  3272. sd->st = st;
  3273. sd->npc_id = st->oid;
  3274. /**
  3275. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3276. **/
  3277. #if SECURE_NPCTIMEOUT
  3278. if( sd->npc_idle_timer == INVALID_TIMER )
  3279. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3280. sd->npc_idle_tick = gettick();
  3281. #endif
  3282. }
  3283. }
  3284. /*==========================================
  3285. * スクリプトの実行メイン部分
  3286. *------------------------------------------*/
  3287. void run_script_main(struct script_state *st)
  3288. {
  3289. int cmdcount=script_config.check_cmdcount;
  3290. int gotocount=script_config.check_gotocount;
  3291. TBL_PC *sd;
  3292. struct script_stack *stack=st->stack;
  3293. struct npc_data *nd;
  3294. script_attach_state(st);
  3295. nd = map_id2nd(st->oid);
  3296. if( nd && map[nd->bl.m].instance_id > 0 )
  3297. st->instance_id = map[nd->bl.m].instance_id;
  3298. if(st->state == RERUNLINE) {
  3299. run_func(st);
  3300. if(st->state == GOTO)
  3301. st->state = RUN;
  3302. } else if(st->state != END)
  3303. st->state = RUN;
  3304. while(st->state == RUN)
  3305. {
  3306. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3307. switch(c){
  3308. case C_EOL:
  3309. if( stack->defsp > stack->sp )
  3310. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3311. else
  3312. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3313. break;
  3314. case C_INT:
  3315. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3316. break;
  3317. case C_POS:
  3318. case C_NAME:
  3319. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3320. st->pos+=3;
  3321. break;
  3322. case C_ARG:
  3323. push_val(stack,c,0);
  3324. break;
  3325. case C_STR:
  3326. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3327. while(st->script->script_buf[st->pos++]);
  3328. break;
  3329. case C_FUNC:
  3330. run_func(st);
  3331. if(st->state==GOTO){
  3332. st->state = RUN;
  3333. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3334. ShowError("run_script: infinity loop !\n");
  3335. script_reportsrc(st);
  3336. st->state=END;
  3337. }
  3338. }
  3339. break;
  3340. case C_NEG:
  3341. case C_NOT:
  3342. case C_LNOT:
  3343. op_1(st ,c);
  3344. break;
  3345. case C_ADD:
  3346. case C_SUB:
  3347. case C_MUL:
  3348. case C_DIV:
  3349. case C_MOD:
  3350. case C_EQ:
  3351. case C_NE:
  3352. case C_GT:
  3353. case C_GE:
  3354. case C_LT:
  3355. case C_LE:
  3356. case C_AND:
  3357. case C_OR:
  3358. case C_XOR:
  3359. case C_LAND:
  3360. case C_LOR:
  3361. case C_R_SHIFT:
  3362. case C_L_SHIFT:
  3363. op_2(st, c);
  3364. break;
  3365. case C_OP3:
  3366. op_3(st, c);
  3367. break;
  3368. case C_NOP:
  3369. st->state=END;
  3370. break;
  3371. default:
  3372. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3373. st->state=END;
  3374. break;
  3375. }
  3376. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3377. ShowError("run_script: infinity loop !\n");
  3378. script_reportsrc(st);
  3379. st->state=END;
  3380. }
  3381. }
  3382. if(st->sleep.tick > 0) {
  3383. //Restore previous script
  3384. script_detach_state(st, false);
  3385. //Delay execution
  3386. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3387. st->sleep.charid = sd?sd->status.char_id:0;
  3388. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3389. run_script_timer, st->sleep.charid, (intptr_t)st);
  3390. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3391. }
  3392. else if(st->state != END && st->rid){
  3393. //Resume later (st is already attached to player).
  3394. if(st->bk_st) {
  3395. ShowWarning("Unable to restore stack! Double continuation!\n");
  3396. //Report BOTH scripts to see if that can help somehow.
  3397. ShowDebug("Previous script (lost):\n");
  3398. script_reportsrc(st->bk_st);
  3399. ShowDebug("Current script:\n");
  3400. script_reportsrc(st);
  3401. script_free_state(st->bk_st);
  3402. st->bk_st = NULL;
  3403. }
  3404. } else {
  3405. //Dispose of script.
  3406. if ((sd = map_id2sd(st->rid))!=NULL)
  3407. { //Restore previous stack and save char.
  3408. if(sd->state.using_fake_npc){
  3409. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3410. sd->state.using_fake_npc = 0;
  3411. }
  3412. //Restore previous script if any.
  3413. script_detach_state(st, true);
  3414. if (sd->state.reg_dirty&2)
  3415. intif_saveregistry(sd,2);
  3416. if (sd->state.reg_dirty&1)
  3417. intif_saveregistry(sd,1);
  3418. }
  3419. script_free_state(st);
  3420. st = NULL;
  3421. }
  3422. }
  3423. int script_config_read(char *cfgName)
  3424. {
  3425. int i;
  3426. char line[1024],w1[1024],w2[1024];
  3427. FILE *fp;
  3428. fp=fopen(cfgName,"r");
  3429. if(fp==NULL){
  3430. ShowError("file not found: [%s]\n", cfgName);
  3431. return 1;
  3432. }
  3433. while(fgets(line, sizeof(line), fp))
  3434. {
  3435. if(line[0] == '/' && line[1] == '/')
  3436. continue;
  3437. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3438. if(i!=2)
  3439. continue;
  3440. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3441. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3442. }
  3443. else if(strcmpi(w1,"check_cmdcount")==0) {
  3444. script_config.check_cmdcount = config_switch(w2);
  3445. }
  3446. else if(strcmpi(w1,"check_gotocount")==0) {
  3447. script_config.check_gotocount = config_switch(w2);
  3448. }
  3449. else if(strcmpi(w1,"input_min_value")==0) {
  3450. script_config.input_min_value = config_switch(w2);
  3451. }
  3452. else if(strcmpi(w1,"input_max_value")==0) {
  3453. script_config.input_max_value = config_switch(w2);
  3454. }
  3455. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3456. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3457. }
  3458. else if(strcmpi(w1,"import")==0){
  3459. script_config_read(w2);
  3460. }
  3461. }
  3462. fclose(fp);
  3463. return 0;
  3464. }
  3465. /**
  3466. * @see DBApply
  3467. */
  3468. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3469. {
  3470. struct script_code *code = db_data2ptr(data);
  3471. if (code)
  3472. script_free_code(code);
  3473. return 0;
  3474. }
  3475. void script_run_autobonus(const char *autobonus, int id, int pos)
  3476. {
  3477. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3478. if( script )
  3479. {
  3480. current_equip_item_index = pos;
  3481. run_script(script,0,id,0);
  3482. }
  3483. }
  3484. void script_add_autobonus(const char *autobonus)
  3485. {
  3486. if( strdb_get(autobonus_db, autobonus) == NULL )
  3487. {
  3488. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3489. if( script )
  3490. strdb_put(autobonus_db, autobonus, script);
  3491. }
  3492. }
  3493. /// resets a temporary character array variable to given value
  3494. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3495. {
  3496. int key;
  3497. uint8 idx;
  3498. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3499. {
  3500. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3501. return;
  3502. }
  3503. key = add_str(varname);
  3504. if( is_string_variable(varname) )
  3505. {
  3506. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3507. {
  3508. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3509. }
  3510. }
  3511. else
  3512. {
  3513. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3514. {
  3515. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3516. }
  3517. }
  3518. }
  3519. /// sets a temporary character array variable element idx to given value
  3520. /// @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.
  3521. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3522. {
  3523. int key;
  3524. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3525. {
  3526. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3527. return;
  3528. }
  3529. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3530. {
  3531. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3532. return;
  3533. }
  3534. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3535. if( is_string_variable(varname) )
  3536. {
  3537. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3538. }
  3539. else
  3540. {
  3541. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3542. }
  3543. if( refcache )
  3544. {// save to avoid repeated add_str calls
  3545. refcache[0] = key;
  3546. }
  3547. }
  3548. /*==========================================
  3549. * 終了
  3550. *------------------------------------------*/
  3551. int do_final_script()
  3552. {
  3553. #ifdef DEBUG_HASH
  3554. if (battle_config.etc_log)
  3555. {
  3556. FILE *fp = fopen("hash_dump.txt","wt");
  3557. if(fp) {
  3558. int i,count[SCRIPT_HASH_SIZE];
  3559. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3560. int n=0;
  3561. int min=INT_MAX,max=0,zero=0;
  3562. double mean=0.0f;
  3563. double median=0.0f;
  3564. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3565. memset(count, 0, sizeof(count));
  3566. fprintf(fp,"num : hash : data_name\n");
  3567. fprintf(fp,"---------------------------------------------------------------\n");
  3568. for(i=LABEL_START; i<str_num; i++) {
  3569. unsigned int h = calc_hash(get_str(i));
  3570. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3571. ++count[h];
  3572. }
  3573. fprintf(fp,"--------------------\n\n");
  3574. memset(count2, 0, sizeof(count2));
  3575. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3576. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3577. if(min > count[i])
  3578. min = count[i]; // minimun count of collision
  3579. if(max < count[i])
  3580. max = count[i]; // maximun count of collision
  3581. if(count[i] == 0)
  3582. zero++;
  3583. ++count2[count[i]];
  3584. }
  3585. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3586. for( i=min; i <= max; ++i ){
  3587. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3588. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3589. }
  3590. for( i=min; i <= max; ++i ){
  3591. n += count2[i];
  3592. if( n*2 >= SCRIPT_HASH_SIZE )
  3593. {
  3594. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3595. median = (i+i+1)/2.0f;
  3596. else
  3597. median = i;
  3598. break;
  3599. }
  3600. }
  3601. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3602. fclose(fp);
  3603. }
  3604. }
  3605. #endif
  3606. mapreg_final();
  3607. db_destroy(scriptlabel_db);
  3608. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3609. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3610. if(sleep_db) {
  3611. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3612. while(n) {
  3613. struct script_state *st = (struct script_state *)n->data;
  3614. script_free_state(st);
  3615. n = n->next;
  3616. }
  3617. linkdb_final(&sleep_db);
  3618. }
  3619. if (str_data)
  3620. aFree(str_data);
  3621. if (str_buf)
  3622. aFree(str_buf);
  3623. return 0;
  3624. }
  3625. /*==========================================
  3626. * 初期化
  3627. *------------------------------------------*/
  3628. int do_init_script()
  3629. {
  3630. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3631. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3632. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3633. mapreg_init();
  3634. return 0;
  3635. }
  3636. int script_reload()
  3637. {
  3638. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3639. db_clear(scriptlabel_db);
  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. mapreg_reload();
  3650. return 0;
  3651. }
  3652. //-----------------------------------------------------------------------------
  3653. // buildin functions
  3654. //
  3655. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3656. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3657. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3658. /////////////////////////////////////////////////////////////////////
  3659. // NPC interaction
  3660. //
  3661. /// Appends a message to the npc dialog.
  3662. /// If a dialog doesn't exist yet, one is created.
  3663. ///
  3664. /// mes "<message>";
  3665. BUILDIN_FUNC(mes)
  3666. {
  3667. TBL_PC* sd = script_rid2sd(st);
  3668. if( sd == NULL )
  3669. return 0;
  3670. if( !script_hasdata(st, 3) )
  3671. {// only a single line detected in the script
  3672. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3673. }
  3674. else
  3675. {// parse multiple lines as they exist
  3676. int i;
  3677. for( i = 2; script_hasdata(st, i); i++ )
  3678. {
  3679. // send the message to the client
  3680. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3681. }
  3682. }
  3683. return 0;
  3684. }
  3685. /// Displays the button 'next' in the npc dialog.
  3686. /// The dialog text is cleared and the script continues when the button is pressed.
  3687. ///
  3688. /// next;
  3689. BUILDIN_FUNC(next)
  3690. {
  3691. TBL_PC* sd;
  3692. sd = script_rid2sd(st);
  3693. if( sd == NULL )
  3694. return 0;
  3695. st->state = STOP;
  3696. clif_scriptnext(sd, st->oid);
  3697. return 0;
  3698. }
  3699. /// Ends the script and displays the button 'close' on the npc dialog.
  3700. /// The dialog is closed when the button is pressed.
  3701. ///
  3702. /// close;
  3703. BUILDIN_FUNC(close)
  3704. {
  3705. TBL_PC* sd;
  3706. sd = script_rid2sd(st);
  3707. if( sd == NULL )
  3708. return 0;
  3709. st->state = END;
  3710. clif_scriptclose(sd, st->oid);
  3711. return 0;
  3712. }
  3713. /// Displays the button 'close' on the npc dialog.
  3714. /// The dialog is closed and the script continues when the button is pressed.
  3715. ///
  3716. /// close2;
  3717. BUILDIN_FUNC(close2)
  3718. {
  3719. TBL_PC* sd;
  3720. sd = script_rid2sd(st);
  3721. if( sd == NULL )
  3722. return 0;
  3723. st->state = STOP;
  3724. clif_scriptclose(sd, st->oid);
  3725. return 0;
  3726. }
  3727. /// Counts the number of valid and total number of options in 'str'
  3728. /// If max_count > 0 the counting stops when that valid option is reached
  3729. /// total is incremented for each option (NULL is supported)
  3730. static int menu_countoptions(const char* str, int max_count, int* total)
  3731. {
  3732. int count = 0;
  3733. int bogus_total;
  3734. if( total == NULL )
  3735. total = &bogus_total;
  3736. ++(*total);
  3737. // initial empty options
  3738. while( *str == ':' )
  3739. {
  3740. ++str;
  3741. ++(*total);
  3742. }
  3743. // count menu options
  3744. while( *str != '\0' )
  3745. {
  3746. ++count;
  3747. --max_count;
  3748. if( max_count == 0 )
  3749. break;
  3750. while( *str != ':' && *str != '\0' )
  3751. ++str;
  3752. while( *str == ':' )
  3753. {
  3754. ++str;
  3755. ++(*total);
  3756. }
  3757. }
  3758. return count;
  3759. }
  3760. /// Displays a menu with options and goes to the target label.
  3761. /// The script is stopped if cancel is pressed.
  3762. /// Options with no text are not displayed in the client.
  3763. ///
  3764. /// Options can be grouped together, separated by the character ':' in the text:
  3765. /// ex: menu "A:B:C",L_target;
  3766. /// All these options go to the specified target label.
  3767. ///
  3768. /// The index of the selected option is put in the variable @menu.
  3769. /// Indexes start with 1 and are consistent with grouped and empty options.
  3770. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3771. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3772. ///
  3773. /// NOTE: the client closes the npc dialog when cancel is pressed
  3774. ///
  3775. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3776. BUILDIN_FUNC(menu)
  3777. {
  3778. int i;
  3779. const char* text;
  3780. TBL_PC* sd;
  3781. sd = script_rid2sd(st);
  3782. if( sd == NULL )
  3783. return 0;
  3784. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3785. if( sd->state.menu_or_input == 0 )
  3786. {
  3787. struct StringBuf buf;
  3788. struct script_data* data;
  3789. if( script_lastdata(st) % 2 == 0 )
  3790. {// argument count is not even (1st argument is at index 2)
  3791. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3792. st->state = END;
  3793. return 1;
  3794. }
  3795. StringBuf_Init(&buf);
  3796. sd->npc_menu = 0;
  3797. for( i = 2; i < script_lastdata(st); i += 2 )
  3798. {
  3799. // menu options
  3800. text = script_getstr(st, i);
  3801. // target label
  3802. data = script_getdata(st, i+1);
  3803. if( !data_islabel(data) )
  3804. {// not a label
  3805. StringBuf_Destroy(&buf);
  3806. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3807. script_reportdata(data);
  3808. st->state = END;
  3809. return 1;
  3810. }
  3811. // append option(s)
  3812. if( text[0] == '\0' )
  3813. continue;// empty string, ignore
  3814. if( sd->npc_menu > 0 )
  3815. StringBuf_AppendStr(&buf, ":");
  3816. StringBuf_AppendStr(&buf, text);
  3817. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3818. }
  3819. st->state = RERUNLINE;
  3820. sd->state.menu_or_input = 1;
  3821. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3822. StringBuf_Destroy(&buf);
  3823. if( sd->npc_menu >= 0xff )
  3824. {// 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
  3825. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3826. script_reportsrc(st);
  3827. }
  3828. }
  3829. else if( sd->npc_menu == 0xff )
  3830. {// Cancel was pressed
  3831. sd->state.menu_or_input = 0;
  3832. st->state = END;
  3833. }
  3834. else
  3835. {// goto target label
  3836. int menu = 0;
  3837. sd->state.menu_or_input = 0;
  3838. if( sd->npc_menu <= 0 )
  3839. {
  3840. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3841. st->state = END;
  3842. return 1;
  3843. }
  3844. // get target label
  3845. for( i = 2; i < script_lastdata(st); i += 2 )
  3846. {
  3847. text = script_getstr(st, i);
  3848. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3849. if( sd->npc_menu <= 0 )
  3850. break;// entry found
  3851. }
  3852. if( sd->npc_menu > 0 )
  3853. {// Invalid selection
  3854. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3855. st->state = END;
  3856. return 1;
  3857. }
  3858. if( !data_islabel(script_getdata(st, i + 1)) )
  3859. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3860. ShowError("script:menu: unexpected data in label argument\n");
  3861. script_reportdata(script_getdata(st, i + 1));
  3862. st->state = END;
  3863. return 1;
  3864. }
  3865. pc_setreg(sd, add_str("@menu"), menu);
  3866. st->pos = script_getnum(st, i + 1);
  3867. st->state = GOTO;
  3868. }
  3869. return 0;
  3870. }
  3871. /// Displays a menu with options and returns the selected option.
  3872. /// Behaves like 'menu' without the target labels.
  3873. ///
  3874. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3875. ///
  3876. /// @see menu
  3877. BUILDIN_FUNC(select)
  3878. {
  3879. int i;
  3880. const char* text;
  3881. TBL_PC* sd;
  3882. sd = script_rid2sd(st);
  3883. if( sd == NULL )
  3884. return 0;
  3885. if( sd->state.menu_or_input == 0 )
  3886. {
  3887. struct StringBuf buf;
  3888. StringBuf_Init(&buf);
  3889. sd->npc_menu = 0;
  3890. for( i = 2; i <= script_lastdata(st); ++i )
  3891. {
  3892. text = script_getstr(st, i);
  3893. if( sd->npc_menu > 0 )
  3894. StringBuf_AppendStr(&buf, ":");
  3895. StringBuf_AppendStr(&buf, text);
  3896. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3897. }
  3898. st->state = RERUNLINE;
  3899. sd->state.menu_or_input = 1;
  3900. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3901. StringBuf_Destroy(&buf);
  3902. if( sd->npc_menu >= 0xff )
  3903. {
  3904. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3905. script_reportsrc(st);
  3906. }
  3907. }
  3908. else if( sd->npc_menu == 0xff )
  3909. {// Cancel was pressed
  3910. sd->state.menu_or_input = 0;
  3911. st->state = END;
  3912. }
  3913. else
  3914. {// return selected option
  3915. int menu = 0;
  3916. sd->state.menu_or_input = 0;
  3917. for( i = 2; i <= script_lastdata(st); ++i )
  3918. {
  3919. text = script_getstr(st, i);
  3920. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3921. if( sd->npc_menu <= 0 )
  3922. break;// entry found
  3923. }
  3924. pc_setreg(sd, add_str("@menu"), menu);
  3925. script_pushint(st, menu);
  3926. st->state = RUN;
  3927. }
  3928. return 0;
  3929. }
  3930. /// Displays a menu with options and returns the selected option.
  3931. /// Behaves like 'menu' without the target labels, except when cancel is
  3932. /// pressed.
  3933. /// When cancel is pressed, the script continues and 255 is returned.
  3934. ///
  3935. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3936. ///
  3937. /// @see menu
  3938. BUILDIN_FUNC(prompt)
  3939. {
  3940. int i;
  3941. const char *text;
  3942. TBL_PC* sd;
  3943. sd = script_rid2sd(st);
  3944. if( sd == NULL )
  3945. return 0;
  3946. if( sd->state.menu_or_input == 0 )
  3947. {
  3948. struct StringBuf buf;
  3949. StringBuf_Init(&buf);
  3950. sd->npc_menu = 0;
  3951. for( i = 2; i <= script_lastdata(st); ++i )
  3952. {
  3953. text = script_getstr(st, i);
  3954. if( sd->npc_menu > 0 )
  3955. StringBuf_AppendStr(&buf, ":");
  3956. StringBuf_AppendStr(&buf, text);
  3957. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3958. }
  3959. st->state = RERUNLINE;
  3960. sd->state.menu_or_input = 1;
  3961. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3962. StringBuf_Destroy(&buf);
  3963. if( sd->npc_menu >= 0xff )
  3964. {
  3965. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3966. script_reportsrc(st);
  3967. }
  3968. }
  3969. else if( sd->npc_menu == 0xff )
  3970. {// Cancel was pressed
  3971. sd->state.menu_or_input = 0;
  3972. pc_setreg(sd, add_str("@menu"), 0xff);
  3973. script_pushint(st, 0xff);
  3974. st->state = RUN;
  3975. }
  3976. else
  3977. {// return selected option
  3978. int menu = 0;
  3979. sd->state.menu_or_input = 0;
  3980. for( i = 2; i <= script_lastdata(st); ++i )
  3981. {
  3982. text = script_getstr(st, i);
  3983. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3984. if( sd->npc_menu <= 0 )
  3985. break;// entry found
  3986. }
  3987. pc_setreg(sd, add_str("@menu"), menu);
  3988. script_pushint(st, menu);
  3989. st->state = RUN;
  3990. }
  3991. return 0;
  3992. }
  3993. /////////////////////////////////////////////////////////////////////
  3994. // ...
  3995. //
  3996. /// Jumps to the target script label.
  3997. ///
  3998. /// goto <label>;
  3999. BUILDIN_FUNC(goto)
  4000. {
  4001. if( !data_islabel(script_getdata(st,2)) )
  4002. {
  4003. ShowError("script:goto: not a label\n");
  4004. script_reportdata(script_getdata(st,2));
  4005. st->state = END;
  4006. return 1;
  4007. }
  4008. st->pos = script_getnum(st,2);
  4009. st->state = GOTO;
  4010. return 0;
  4011. }
  4012. /*==========================================
  4013. * user-defined function call
  4014. *------------------------------------------*/
  4015. BUILDIN_FUNC(callfunc)
  4016. {
  4017. int i, j;
  4018. struct script_retinfo* ri;
  4019. struct script_code* scr;
  4020. const char* str = script_getstr(st,2);
  4021. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4022. if( !scr )
  4023. {
  4024. ShowError("script:callfunc: function not found! [%s]\n", str);
  4025. st->state = END;
  4026. return 1;
  4027. }
  4028. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4029. {
  4030. struct script_data* data = push_copy(st->stack,i);
  4031. if( data_isreference(data) && !data->ref )
  4032. {
  4033. const char* name = reference_getname(data);
  4034. if( name[0] == '.' && name[1] == '@' )
  4035. data->ref = &st->stack->var_function;
  4036. else if( name[0] == '.' )
  4037. data->ref = &st->script->script_vars;
  4038. }
  4039. }
  4040. CREATE(ri, struct script_retinfo, 1);
  4041. ri->script = st->script;// script code
  4042. ri->var_function = st->stack->var_function;// scope variables
  4043. ri->pos = st->pos;// script location
  4044. ri->nargs = j;// argument count
  4045. ri->defsp = st->stack->defsp;// default stack pointer
  4046. push_retinfo(st->stack, ri);
  4047. st->pos = 0;
  4048. st->script = scr;
  4049. st->stack->defsp = st->stack->sp;
  4050. st->state = GOTO;
  4051. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4052. return 0;
  4053. }
  4054. /*==========================================
  4055. * subroutine call
  4056. *------------------------------------------*/
  4057. BUILDIN_FUNC(callsub)
  4058. {
  4059. int i,j;
  4060. struct script_retinfo* ri;
  4061. int pos = script_getnum(st,2);
  4062. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4063. {
  4064. ShowError("script:callsub: argument is not a label\n");
  4065. script_reportdata(script_getdata(st,2));
  4066. st->state = END;
  4067. return 1;
  4068. }
  4069. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4070. {
  4071. struct script_data* data = push_copy(st->stack,i);
  4072. if( data_isreference(data) && !data->ref )
  4073. {
  4074. const char* name = reference_getname(data);
  4075. if( name[0] == '.' && name[1] == '@' )
  4076. data->ref = &st->stack->var_function;
  4077. }
  4078. }
  4079. CREATE(ri, struct script_retinfo, 1);
  4080. ri->script = st->script;// script code
  4081. ri->var_function = st->stack->var_function;// scope variables
  4082. ri->pos = st->pos;// script location
  4083. ri->nargs = j;// argument count
  4084. ri->defsp = st->stack->defsp;// default stack pointer
  4085. push_retinfo(st->stack, ri);
  4086. st->pos = pos;
  4087. st->stack->defsp = st->stack->sp;
  4088. st->state = GOTO;
  4089. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4090. return 0;
  4091. }
  4092. /// Retrieves an argument provided to callfunc/callsub.
  4093. /// If the argument doesn't exist
  4094. ///
  4095. /// getarg(<index>{,<default_value>}) -> <value>
  4096. BUILDIN_FUNC(getarg)
  4097. {
  4098. struct script_retinfo* ri;
  4099. int idx;
  4100. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4101. {
  4102. ShowError("script:getarg: no callfunc or callsub!\n");
  4103. st->state = END;
  4104. return 1;
  4105. }
  4106. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4107. idx = script_getnum(st,2);
  4108. if( idx >= 0 && idx < ri->nargs )
  4109. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4110. else if( script_hasdata(st,3) )
  4111. script_pushcopy(st, 3);
  4112. else
  4113. {
  4114. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4115. st->state = END;
  4116. return 1;
  4117. }
  4118. return 0;
  4119. }
  4120. /// Returns from the current function, optionaly returning a value from the functions.
  4121. /// Don't use outside script functions.
  4122. ///
  4123. /// return;
  4124. /// return <value>;
  4125. BUILDIN_FUNC(return)
  4126. {
  4127. if( script_hasdata(st,2) )
  4128. {// return value
  4129. struct script_data* data;
  4130. script_pushcopy(st, 2);
  4131. data = script_getdatatop(st, -1);
  4132. if( data_isreference(data) )
  4133. {
  4134. const char* name = reference_getname(data);
  4135. if( name[0] == '.' && name[1] == '@' )
  4136. {// scope variable
  4137. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4138. get_val(st, data);// current scope, convert to value
  4139. }
  4140. else if( name[0] == '.' && !data->ref )
  4141. {// script variable, link to current script
  4142. data->ref = &st->script->script_vars;
  4143. }
  4144. }
  4145. }
  4146. else
  4147. {// no return value
  4148. script_pushnil(st);
  4149. }
  4150. st->state = RETFUNC;
  4151. return 0;
  4152. }
  4153. /// Returns a random number from 0 to <range>-1.
  4154. /// Or returns a random number from <min> to <max>.
  4155. /// If <min> is greater than <max>, their numbers are switched.
  4156. /// rand(<range>) -> <int>
  4157. /// rand(<min>,<max>) -> <int>
  4158. BUILDIN_FUNC(rand)
  4159. {
  4160. int range;
  4161. int min;
  4162. int max;
  4163. if( script_hasdata(st,3) )
  4164. {// min,max
  4165. min = script_getnum(st,2);
  4166. max = script_getnum(st,3);
  4167. if( max < min )
  4168. swap(min, max);
  4169. range = max - min + 1;
  4170. }
  4171. else
  4172. {// range
  4173. min = 0;
  4174. range = script_getnum(st,2);
  4175. }
  4176. if( range <= 1 )
  4177. script_pushint(st, min);
  4178. else
  4179. script_pushint(st, rnd()%range + min);
  4180. return 0;
  4181. }
  4182. /*==========================================
  4183. *
  4184. *------------------------------------------*/
  4185. BUILDIN_FUNC(warp)
  4186. {
  4187. int ret;
  4188. int x,y;
  4189. const char* str;
  4190. TBL_PC* sd;
  4191. sd = script_rid2sd(st);
  4192. if( sd == NULL )
  4193. return 0;
  4194. str = script_getstr(st,2);
  4195. x = script_getnum(st,3);
  4196. y = script_getnum(st,4);
  4197. if(strcmp(str,"Random")==0)
  4198. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4199. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4200. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4201. else
  4202. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4203. if( ret ) {
  4204. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4205. script_reportsrc(st);
  4206. }
  4207. return 0;
  4208. }
  4209. /*==========================================
  4210. * エリア指定ワープ
  4211. *------------------------------------------*/
  4212. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4213. {
  4214. int x,y;
  4215. unsigned int map;
  4216. map=va_arg(ap, unsigned int);
  4217. x=va_arg(ap,int);
  4218. y=va_arg(ap,int);
  4219. if(map == 0)
  4220. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4221. else
  4222. pc_setpos((TBL_PC *)bl,map,x,y,CLR_OUTSIGHT);
  4223. return 0;
  4224. }
  4225. BUILDIN_FUNC(areawarp)
  4226. {
  4227. int x,y,m;
  4228. unsigned int index;
  4229. const char *str;
  4230. const char *mapname;
  4231. int x0,y0,x1,y1;
  4232. mapname=script_getstr(st,2);
  4233. x0=script_getnum(st,3);
  4234. y0=script_getnum(st,4);
  4235. x1=script_getnum(st,5);
  4236. y1=script_getnum(st,6);
  4237. str=script_getstr(st,7);
  4238. x=script_getnum(st,8);
  4239. y=script_getnum(st,9);
  4240. if( (m=map_mapname2mapid(mapname))< 0)
  4241. return 0;
  4242. if(strcmp(str,"Random")==0)
  4243. index = 0;
  4244. else if(!(index=mapindex_name2id(str)))
  4245. return 0;
  4246. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  4247. return 0;
  4248. }
  4249. /*==========================================
  4250. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4251. *------------------------------------------*/
  4252. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4253. {
  4254. int hp, sp;
  4255. hp = va_arg(ap, int);
  4256. sp = va_arg(ap, int);
  4257. pc_percentheal((TBL_PC *)bl,hp,sp);
  4258. return 0;
  4259. }
  4260. BUILDIN_FUNC(areapercentheal)
  4261. {
  4262. int hp,sp,m;
  4263. const char *mapname;
  4264. int x0,y0,x1,y1;
  4265. mapname=script_getstr(st,2);
  4266. x0=script_getnum(st,3);
  4267. y0=script_getnum(st,4);
  4268. x1=script_getnum(st,5);
  4269. y1=script_getnum(st,6);
  4270. hp=script_getnum(st,7);
  4271. sp=script_getnum(st,8);
  4272. if( (m=map_mapname2mapid(mapname))< 0)
  4273. return 0;
  4274. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4275. return 0;
  4276. }
  4277. /*==========================================
  4278. * warpchar [LuzZza]
  4279. * Useful for warp one player from
  4280. * another player npc-session.
  4281. * Using: warpchar "mapname",x,y,Char_ID;
  4282. *------------------------------------------*/
  4283. BUILDIN_FUNC(warpchar)
  4284. {
  4285. int x,y,a;
  4286. const char *str;
  4287. TBL_PC *sd;
  4288. str=script_getstr(st,2);
  4289. x=script_getnum(st,3);
  4290. y=script_getnum(st,4);
  4291. a=script_getnum(st,5);
  4292. sd = map_charid2sd(a);
  4293. if( sd == NULL )
  4294. return 0;
  4295. if(strcmp(str, "Random") == 0)
  4296. pc_randomwarp(sd, CLR_TELEPORT);
  4297. else
  4298. if(strcmp(str, "SavePoint") == 0)
  4299. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4300. else
  4301. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4302. return 0;
  4303. }
  4304. /*==========================================
  4305. * Warpparty - [Fredzilla] [Paradox924X]
  4306. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4307. * If 'from_mapname' is specified, only the party members on that map will be warped
  4308. *------------------------------------------*/
  4309. BUILDIN_FUNC(warpparty)
  4310. {
  4311. TBL_PC *sd = NULL;
  4312. TBL_PC *pl_sd;
  4313. struct party_data* p;
  4314. int type;
  4315. int mapindex;
  4316. int i;
  4317. const char* str = script_getstr(st,2);
  4318. int x = script_getnum(st,3);
  4319. int y = script_getnum(st,4);
  4320. int p_id = script_getnum(st,5);
  4321. const char* str2 = NULL;
  4322. if ( script_hasdata(st,6) )
  4323. str2 = script_getstr(st,6);
  4324. p = party_search(p_id);
  4325. if(!p)
  4326. return 0;
  4327. type = ( strcmp(str,"Random")==0 ) ? 0
  4328. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4329. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4330. : ( strcmp(str,"Leader")==0 ) ? 3
  4331. : 4;
  4332. switch (type)
  4333. {
  4334. case 3:
  4335. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4336. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4337. return 0;
  4338. pl_sd = p->data[i].sd;
  4339. mapindex = pl_sd->mapindex;
  4340. x = pl_sd->bl.x;
  4341. y = pl_sd->bl.y;
  4342. break;
  4343. case 4:
  4344. mapindex = mapindex_name2id(str);
  4345. break;
  4346. case 2:
  4347. //"SavePoint" uses save point of the currently attached player
  4348. if (( sd = script_rid2sd(st) ) == NULL )
  4349. return 0;
  4350. default:
  4351. mapindex = 0;
  4352. break;
  4353. }
  4354. for (i = 0; i < MAX_PARTY; i++)
  4355. {
  4356. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4357. continue;
  4358. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4359. continue;
  4360. if( pc_isdead(pl_sd) )
  4361. continue;
  4362. switch( type )
  4363. {
  4364. case 0: // Random
  4365. if(!map[pl_sd->bl.m].flag.nowarp)
  4366. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4367. break;
  4368. case 1: // SavePointAll
  4369. if(!map[pl_sd->bl.m].flag.noreturn)
  4370. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4371. break;
  4372. case 2: // SavePoint
  4373. if(!map[pl_sd->bl.m].flag.noreturn)
  4374. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4375. break;
  4376. case 3: // Leader
  4377. case 4: // m,x,y
  4378. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4379. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4380. break;
  4381. }
  4382. }
  4383. return 0;
  4384. }
  4385. /*==========================================
  4386. * Warpguild - [Fredzilla]
  4387. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4388. *------------------------------------------*/
  4389. BUILDIN_FUNC(warpguild)
  4390. {
  4391. TBL_PC *sd = NULL;
  4392. TBL_PC *pl_sd;
  4393. struct guild* g;
  4394. struct s_mapiterator* iter;
  4395. int type;
  4396. const char* str = script_getstr(st,2);
  4397. int x = script_getnum(st,3);
  4398. int y = script_getnum(st,4);
  4399. int gid = script_getnum(st,5);
  4400. g = guild_search(gid);
  4401. if( g == NULL )
  4402. return 0;
  4403. type = ( strcmp(str,"Random")==0 ) ? 0
  4404. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4405. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4406. : 3;
  4407. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4408. {// "SavePoint" uses save point of the currently attached player
  4409. return 0;
  4410. }
  4411. iter = mapit_getallusers();
  4412. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4413. {
  4414. if( pl_sd->status.guild_id != gid )
  4415. continue;
  4416. switch( type )
  4417. {
  4418. case 0: // Random
  4419. if(!map[pl_sd->bl.m].flag.nowarp)
  4420. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4421. break;
  4422. case 1: // SavePointAll
  4423. if(!map[pl_sd->bl.m].flag.noreturn)
  4424. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4425. break;
  4426. case 2: // SavePoint
  4427. if(!map[pl_sd->bl.m].flag.noreturn)
  4428. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4429. break;
  4430. case 3: // m,x,y
  4431. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4432. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4433. break;
  4434. }
  4435. }
  4436. mapit_free(iter);
  4437. return 0;
  4438. }
  4439. /*==========================================
  4440. *
  4441. *------------------------------------------*/
  4442. BUILDIN_FUNC(heal)
  4443. {
  4444. TBL_PC *sd;
  4445. int hp,sp;
  4446. sd = script_rid2sd(st);
  4447. if (!sd) return 0;
  4448. hp=script_getnum(st,2);
  4449. sp=script_getnum(st,3);
  4450. status_heal(&sd->bl, hp, sp, 1);
  4451. return 0;
  4452. }
  4453. /*==========================================
  4454. *
  4455. *------------------------------------------*/
  4456. BUILDIN_FUNC(itemheal)
  4457. {
  4458. TBL_PC *sd;
  4459. int hp,sp;
  4460. hp=script_getnum(st,2);
  4461. sp=script_getnum(st,3);
  4462. if(potion_flag==1) {
  4463. potion_hp = hp;
  4464. potion_sp = sp;
  4465. return 0;
  4466. }
  4467. sd = script_rid2sd(st);
  4468. if (!sd) return 0;
  4469. pc_itemheal(sd,sd->itemid,hp,sp);
  4470. return 0;
  4471. }
  4472. /*==========================================
  4473. *
  4474. *------------------------------------------*/
  4475. BUILDIN_FUNC(percentheal)
  4476. {
  4477. int hp,sp;
  4478. TBL_PC* sd;
  4479. hp=script_getnum(st,2);
  4480. sp=script_getnum(st,3);
  4481. if(potion_flag==1) {
  4482. potion_per_hp = hp;
  4483. potion_per_sp = sp;
  4484. return 0;
  4485. }
  4486. sd = script_rid2sd(st);
  4487. if( sd == NULL )
  4488. return 0;
  4489. pc_percentheal(sd,hp,sp);
  4490. return 0;
  4491. }
  4492. /*==========================================
  4493. *
  4494. *------------------------------------------*/
  4495. BUILDIN_FUNC(jobchange)
  4496. {
  4497. int job, upper=-1;
  4498. job=script_getnum(st,2);
  4499. if( script_hasdata(st,3) )
  4500. upper=script_getnum(st,3);
  4501. if (pcdb_checkid(job))
  4502. {
  4503. TBL_PC* sd;
  4504. sd = script_rid2sd(st);
  4505. if( sd == NULL )
  4506. return 0;
  4507. pc_jobchange(sd, job, upper);
  4508. }
  4509. return 0;
  4510. }
  4511. /*==========================================
  4512. *
  4513. *------------------------------------------*/
  4514. BUILDIN_FUNC(jobname)
  4515. {
  4516. int class_=script_getnum(st,2);
  4517. script_pushconststr(st, (char*)job_name(class_));
  4518. return 0;
  4519. }
  4520. /// Get input from the player.
  4521. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4522. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4523. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4524. /// shorter than 'min' and 0 otherwise.
  4525. ///
  4526. /// input(<var>{,<min>{,<max>}}) -> <int>
  4527. BUILDIN_FUNC(input)
  4528. {
  4529. TBL_PC* sd;
  4530. struct script_data* data;
  4531. int uid;
  4532. const char* name;
  4533. int min;
  4534. int max;
  4535. sd = script_rid2sd(st);
  4536. if( sd == NULL )
  4537. return 0;
  4538. data = script_getdata(st,2);
  4539. if( !data_isreference(data) ){
  4540. ShowError("script:input: not a variable\n");
  4541. script_reportdata(data);
  4542. st->state = END;
  4543. return 1;
  4544. }
  4545. uid = reference_getuid(data);
  4546. name = reference_getname(data);
  4547. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4548. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4549. if( !sd->state.menu_or_input )
  4550. { // first invocation, display npc input box
  4551. sd->state.menu_or_input = 1;
  4552. st->state = RERUNLINE;
  4553. if( is_string_variable(name) )
  4554. clif_scriptinputstr(sd,st->oid);
  4555. else
  4556. clif_scriptinput(sd,st->oid);
  4557. }
  4558. else
  4559. { // take received text/value and store it in the designated variable
  4560. sd->state.menu_or_input = 0;
  4561. if( is_string_variable(name) )
  4562. {
  4563. int len = (int)strlen(sd->npc_str);
  4564. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4565. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4566. }
  4567. else
  4568. {
  4569. int amount = sd->npc_amount;
  4570. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4571. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4572. }
  4573. st->state = RUN;
  4574. }
  4575. return 0;
  4576. }
  4577. // declare the copyarray method here for future reference
  4578. BUILDIN_FUNC(copyarray);
  4579. /// Sets the value of a variable.
  4580. /// The value is converted to the type of the variable.
  4581. ///
  4582. /// set(<variable>,<value>) -> <variable>
  4583. BUILDIN_FUNC(set)
  4584. {
  4585. TBL_PC* sd = NULL;
  4586. struct script_data* data;
  4587. struct script_data* datavalue;
  4588. int num;
  4589. const char* name;
  4590. char prefix;
  4591. data = script_getdata(st,2);
  4592. datavalue = script_getdata(st,3);
  4593. if( !data_isreference(data) )
  4594. {
  4595. ShowError("script:set: not a variable\n");
  4596. script_reportdata(script_getdata(st,2));
  4597. st->state = END;
  4598. return 1;
  4599. }
  4600. num = reference_getuid(data);
  4601. name = reference_getname(data);
  4602. prefix = *name;
  4603. if( not_server_variable(prefix) )
  4604. {
  4605. sd = script_rid2sd(st);
  4606. if( sd == NULL )
  4607. {
  4608. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4609. return 0;
  4610. }
  4611. }
  4612. if( data_isreference(datavalue) )
  4613. {// the value being referenced is a variable
  4614. const char* namevalue = reference_getname(datavalue);
  4615. if( !not_array_variable(*namevalue) )
  4616. {// array variable being copied into another array variable
  4617. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  4618. {// player must be attached in order to copy a player variable
  4619. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  4620. return 0;
  4621. }
  4622. if( is_string_variable(namevalue) != is_string_variable(name) )
  4623. {// non-matching array value types
  4624. ShowWarning("script:set: two array variables do not match in type.\n");
  4625. return 0;
  4626. }
  4627. // push the maximum number of array values to the stack
  4628. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  4629. // call the copy array method directly
  4630. return buildin_copyarray(st);
  4631. }
  4632. }
  4633. if( is_string_variable(name) )
  4634. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4635. else
  4636. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4637. // return a copy of the variable reference
  4638. script_pushcopy(st,2);
  4639. return 0;
  4640. }
  4641. /////////////////////////////////////////////////////////////////////
  4642. /// Array variables
  4643. ///
  4644. /// Returns the size of the specified array
  4645. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  4646. {
  4647. int32 ret = idx;
  4648. if( isstring )
  4649. {
  4650. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4651. {
  4652. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4653. if( str && *str )
  4654. ret = idx + 1;
  4655. script_removetop(st, -1, 0);
  4656. }
  4657. }
  4658. else
  4659. {
  4660. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4661. {
  4662. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  4663. if( num )
  4664. ret = idx + 1;
  4665. script_removetop(st, -1, 0);
  4666. }
  4667. }
  4668. return ret;
  4669. }
  4670. /// Sets values of an array, from the starting index.
  4671. /// ex: setarray arr[1],1,2,3;
  4672. ///
  4673. /// setarray <array variable>,<value1>{,<value2>...};
  4674. BUILDIN_FUNC(setarray)
  4675. {
  4676. struct script_data* data;
  4677. const char* name;
  4678. int32 start;
  4679. int32 end;
  4680. int32 id;
  4681. int32 i;
  4682. TBL_PC* sd = NULL;
  4683. data = script_getdata(st, 2);
  4684. if( !data_isreference(data) )
  4685. {
  4686. ShowError("script:setarray: not a variable\n");
  4687. script_reportdata(data);
  4688. st->state = END;
  4689. return 1;// not a variable
  4690. }
  4691. id = reference_getid(data);
  4692. start = reference_getindex(data);
  4693. name = reference_getname(data);
  4694. if( not_array_variable(*name) )
  4695. {
  4696. ShowError("script:setarray: illegal scope\n");
  4697. script_reportdata(data);
  4698. st->state = END;
  4699. return 1;// not supported
  4700. }
  4701. if( not_server_variable(*name) )
  4702. {
  4703. sd = script_rid2sd(st);
  4704. if( sd == NULL )
  4705. return 0;// no player attached
  4706. }
  4707. end = start + script_lastdata(st) - 2;
  4708. if( end > SCRIPT_MAX_ARRAYSIZE )
  4709. end = SCRIPT_MAX_ARRAYSIZE;
  4710. if( is_string_variable(name) )
  4711. {// string array
  4712. for( i = 3; start < end; ++start, ++i )
  4713. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4714. }
  4715. else
  4716. {// int array
  4717. for( i = 3; start < end; ++start, ++i )
  4718. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  4719. }
  4720. return 0;
  4721. }
  4722. /// Sets count values of an array, from the starting index.
  4723. /// ex: cleararray arr[0],0,1;
  4724. ///
  4725. /// cleararray <array variable>,<value>,<count>;
  4726. BUILDIN_FUNC(cleararray)
  4727. {
  4728. struct script_data* data;
  4729. const char* name;
  4730. int32 start;
  4731. int32 end;
  4732. int32 id;
  4733. void* v;
  4734. TBL_PC* sd = NULL;
  4735. data = script_getdata(st, 2);
  4736. if( !data_isreference(data) )
  4737. {
  4738. ShowError("script:cleararray: not a variable\n");
  4739. script_reportdata(data);
  4740. st->state = END;
  4741. return 1;// not a variable
  4742. }
  4743. id = reference_getid(data);
  4744. start = reference_getindex(data);
  4745. name = reference_getname(data);
  4746. if( not_array_variable(*name) )
  4747. {
  4748. ShowError("script:cleararray: illegal scope\n");
  4749. script_reportdata(data);
  4750. st->state = END;
  4751. return 1;// not supported
  4752. }
  4753. if( not_server_variable(*name) )
  4754. {
  4755. sd = script_rid2sd(st);
  4756. if( sd == NULL )
  4757. return 0;// no player attached
  4758. }
  4759. if( is_string_variable(name) )
  4760. v = (void*)script_getstr(st, 3);
  4761. else
  4762. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  4763. end = start + script_getnum(st, 4);
  4764. if( end > SCRIPT_MAX_ARRAYSIZE )
  4765. end = SCRIPT_MAX_ARRAYSIZE;
  4766. for( ; start < end; ++start )
  4767. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4768. return 0;
  4769. }
  4770. /// Copies data from one array to another.
  4771. /// ex: copyarray arr[0],arr[2],2;
  4772. ///
  4773. /// copyarray <destination array variable>,<source array variable>,<count>;
  4774. BUILDIN_FUNC(copyarray)
  4775. {
  4776. struct script_data* data1;
  4777. struct script_data* data2;
  4778. const char* name1;
  4779. const char* name2;
  4780. int32 idx1;
  4781. int32 idx2;
  4782. int32 id1;
  4783. int32 id2;
  4784. void* v;
  4785. int32 i;
  4786. int32 count;
  4787. TBL_PC* sd = NULL;
  4788. data1 = script_getdata(st, 2);
  4789. data2 = script_getdata(st, 3);
  4790. if( !data_isreference(data1) || !data_isreference(data2) )
  4791. {
  4792. ShowError("script:copyarray: not a variable\n");
  4793. script_reportdata(data1);
  4794. script_reportdata(data2);
  4795. st->state = END;
  4796. return 1;// not a variable
  4797. }
  4798. id1 = reference_getid(data1);
  4799. id2 = reference_getid(data2);
  4800. idx1 = reference_getindex(data1);
  4801. idx2 = reference_getindex(data2);
  4802. name1 = reference_getname(data1);
  4803. name2 = reference_getname(data2);
  4804. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4805. {
  4806. ShowError("script:copyarray: illegal scope\n");
  4807. script_reportdata(data1);
  4808. script_reportdata(data2);
  4809. st->state = END;
  4810. return 1;// not supported
  4811. }
  4812. if( is_string_variable(name1) != is_string_variable(name2) )
  4813. {
  4814. ShowError("script:copyarray: type mismatch\n");
  4815. script_reportdata(data1);
  4816. script_reportdata(data2);
  4817. st->state = END;
  4818. return 1;// data type mismatch
  4819. }
  4820. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4821. {
  4822. sd = script_rid2sd(st);
  4823. if( sd == NULL )
  4824. return 0;// no player attached
  4825. }
  4826. count = script_getnum(st, 4);
  4827. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  4828. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  4829. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4830. return 0;// nothing to copy
  4831. if( id1 == id2 && idx1 > idx2 )
  4832. {// destination might be overlapping the source - copy in reverse order
  4833. for( i = count - 1; i >= 0; --i )
  4834. {
  4835. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4836. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4837. script_removetop(st, -1, 0);
  4838. }
  4839. }
  4840. else
  4841. {// normal copy
  4842. for( i = 0; i < count; ++i )
  4843. {
  4844. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  4845. {
  4846. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4847. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4848. script_removetop(st, -1, 0);
  4849. }
  4850. else// out of range - assume ""/0
  4851. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4852. }
  4853. }
  4854. return 0;
  4855. }
  4856. /// Returns the size of the array.
  4857. /// Assumes that everything before the starting index exists.
  4858. /// ex: getarraysize(arr[3])
  4859. ///
  4860. /// getarraysize(<array variable>) -> <int>
  4861. BUILDIN_FUNC(getarraysize)
  4862. {
  4863. struct script_data* data;
  4864. const char* name;
  4865. data = script_getdata(st, 2);
  4866. if( !data_isreference(data) )
  4867. {
  4868. ShowError("script:getarraysize: not a variable\n");
  4869. script_reportdata(data);
  4870. script_pushnil(st);
  4871. st->state = END;
  4872. return 1;// not a variable
  4873. }
  4874. name = reference_getname(data);
  4875. if( not_array_variable(*name) )
  4876. {
  4877. ShowError("script:getarraysize: illegal scope\n");
  4878. script_reportdata(data);
  4879. script_pushnil(st);
  4880. st->state = END;
  4881. return 1;// not supported
  4882. }
  4883. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4884. return 0;
  4885. }
  4886. /// Deletes count or all the elements in an array, from the starting index.
  4887. /// ex: deletearray arr[4],2;
  4888. ///
  4889. /// deletearray <array variable>;
  4890. /// deletearray <array variable>,<count>;
  4891. BUILDIN_FUNC(deletearray)
  4892. {
  4893. struct script_data* data;
  4894. const char* name;
  4895. int start;
  4896. int end;
  4897. int id;
  4898. TBL_PC *sd = NULL;
  4899. data = script_getdata(st, 2);
  4900. if( !data_isreference(data) )
  4901. {
  4902. ShowError("script:deletearray: not a variable\n");
  4903. script_reportdata(data);
  4904. st->state = END;
  4905. return 1;// not a variable
  4906. }
  4907. id = reference_getid(data);
  4908. start = reference_getindex(data);
  4909. name = reference_getname(data);
  4910. if( not_array_variable(*name) )
  4911. {
  4912. ShowError("script:deletearray: illegal scope\n");
  4913. script_reportdata(data);
  4914. st->state = END;
  4915. return 1;// not supported
  4916. }
  4917. if( not_server_variable(*name) )
  4918. {
  4919. sd = script_rid2sd(st);
  4920. if( sd == NULL )
  4921. return 0;// no player attached
  4922. }
  4923. end = SCRIPT_MAX_ARRAYSIZE;
  4924. if( start >= end )
  4925. return 0;// nothing to free
  4926. if( script_hasdata(st,3) )
  4927. {
  4928. int count = script_getnum(st, 3);
  4929. if( count > end - start )
  4930. count = end - start;
  4931. if( count <= 0 )
  4932. return 0;// nothing to free
  4933. // move rest of the elements backward
  4934. for( ; start + count < end; ++start )
  4935. {
  4936. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4937. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4938. script_removetop(st, -1, 0);
  4939. }
  4940. }
  4941. // clear the rest of the array
  4942. if( is_string_variable(name) )
  4943. {
  4944. for( ; start < end; ++start )
  4945. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4946. }
  4947. else
  4948. {
  4949. for( ; start < end; ++start )
  4950. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4951. }
  4952. return 0;
  4953. }
  4954. /// Returns a reference to the target index of the array variable.
  4955. /// Equivalent to var[index].
  4956. ///
  4957. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4958. BUILDIN_FUNC(getelementofarray)
  4959. {
  4960. struct script_data* data;
  4961. const char* name;
  4962. int32 id;
  4963. int i;
  4964. data = script_getdata(st, 2);
  4965. if( !data_isreference(data) )
  4966. {
  4967. ShowError("script:getelementofarray: not a variable\n");
  4968. script_reportdata(data);
  4969. script_pushnil(st);
  4970. st->state = END;
  4971. return 1;// not a variable
  4972. }
  4973. id = reference_getid(data);
  4974. name = reference_getname(data);
  4975. if( not_array_variable(*name) )
  4976. {
  4977. ShowError("script:getelementofarray: illegal scope\n");
  4978. script_reportdata(data);
  4979. script_pushnil(st);
  4980. st->state = END;
  4981. return 1;// not supported
  4982. }
  4983. i = script_getnum(st, 3);
  4984. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  4985. {
  4986. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4987. script_reportdata(data);
  4988. script_pushnil(st);
  4989. st->state = END;
  4990. return 1;// out of range
  4991. }
  4992. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4993. return 0;
  4994. }
  4995. /////////////////////////////////////////////////////////////////////
  4996. /// ...
  4997. ///
  4998. /*==========================================
  4999. *
  5000. *------------------------------------------*/
  5001. BUILDIN_FUNC(setlook)
  5002. {
  5003. int type,val;
  5004. TBL_PC* sd;
  5005. type=script_getnum(st,2);
  5006. val=script_getnum(st,3);
  5007. sd = script_rid2sd(st);
  5008. if( sd == NULL )
  5009. return 0;
  5010. pc_changelook(sd,type,val);
  5011. return 0;
  5012. }
  5013. BUILDIN_FUNC(changelook)
  5014. { // As setlook but only client side
  5015. int type,val;
  5016. TBL_PC* sd;
  5017. type=script_getnum(st,2);
  5018. val=script_getnum(st,3);
  5019. sd = script_rid2sd(st);
  5020. if( sd == NULL )
  5021. return 0;
  5022. clif_changelook(&sd->bl,type,val);
  5023. return 0;
  5024. }
  5025. /*==========================================
  5026. *
  5027. *------------------------------------------*/
  5028. BUILDIN_FUNC(cutin)
  5029. {
  5030. TBL_PC* sd;
  5031. sd = script_rid2sd(st);
  5032. if( sd == NULL )
  5033. return 0;
  5034. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5035. return 0;
  5036. }
  5037. /*==========================================
  5038. *
  5039. *------------------------------------------*/
  5040. BUILDIN_FUNC(viewpoint)
  5041. {
  5042. int type,x,y,id,color;
  5043. TBL_PC* sd;
  5044. type=script_getnum(st,2);
  5045. x=script_getnum(st,3);
  5046. y=script_getnum(st,4);
  5047. id=script_getnum(st,5);
  5048. color=script_getnum(st,6);
  5049. sd = script_rid2sd(st);
  5050. if( sd == NULL )
  5051. return 0;
  5052. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5053. return 0;
  5054. }
  5055. /*==========================================
  5056. *
  5057. *------------------------------------------*/
  5058. BUILDIN_FUNC(countitem)
  5059. {
  5060. int nameid, i;
  5061. int count = 0;
  5062. struct item_data* id = NULL;
  5063. struct script_data* data;
  5064. TBL_PC* sd = script_rid2sd(st);
  5065. if (!sd) {
  5066. script_pushint(st,0);
  5067. return 0;
  5068. }
  5069. data = script_getdata(st,2);
  5070. get_val(st, data); // convert into value in case of a variable
  5071. if( data_isstring(data) )
  5072. {// item name
  5073. id = itemdb_searchname(conv_str(st, data));
  5074. }
  5075. else
  5076. {// item id
  5077. id = itemdb_exists(conv_num(st, data));
  5078. }
  5079. if( id == NULL )
  5080. {
  5081. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5082. script_pushint(st,0);
  5083. return 1;
  5084. }
  5085. nameid = id->nameid;
  5086. for(i = 0; i < MAX_INVENTORY; i++)
  5087. if(sd->status.inventory[i].nameid == nameid)
  5088. count += sd->status.inventory[i].amount;
  5089. script_pushint(st,count);
  5090. return 0;
  5091. }
  5092. /*==========================================
  5093. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5094. * returns number of items that meet the conditions
  5095. *------------------------------------------*/
  5096. BUILDIN_FUNC(countitem2)
  5097. {
  5098. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5099. int count = 0;
  5100. int i;
  5101. struct item_data* id = NULL;
  5102. struct script_data* data;
  5103. TBL_PC* sd = script_rid2sd(st);
  5104. if (!sd) {
  5105. script_pushint(st,0);
  5106. return 0;
  5107. }
  5108. data = script_getdata(st,2);
  5109. get_val(st, data); // convert into value in case of a variable
  5110. if( data_isstring(data) )
  5111. {// item name
  5112. id = itemdb_searchname(conv_str(st, data));
  5113. }
  5114. else
  5115. {// item id
  5116. id = itemdb_exists(conv_num(st, data));
  5117. }
  5118. if( id == NULL )
  5119. {
  5120. ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5121. script_pushint(st,0);
  5122. return 1;
  5123. }
  5124. nameid = id->nameid;
  5125. iden = script_getnum(st,3);
  5126. ref = script_getnum(st,4);
  5127. attr = script_getnum(st,5);
  5128. c1 = (short)script_getnum(st,6);
  5129. c2 = (short)script_getnum(st,7);
  5130. c3 = (short)script_getnum(st,8);
  5131. c4 = (short)script_getnum(st,9);
  5132. for(i = 0; i < MAX_INVENTORY; i++)
  5133. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5134. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5135. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5136. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5137. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5138. sd->status.inventory[i].card[3] == c4
  5139. )
  5140. count += sd->status.inventory[i].amount;
  5141. script_pushint(st,count);
  5142. return 0;
  5143. }
  5144. /*==========================================
  5145. * 重量チェック
  5146. *------------------------------------------*/
  5147. BUILDIN_FUNC(checkweight)
  5148. {
  5149. int nameid, amount, slots;
  5150. unsigned int weight;
  5151. struct item_data* id = NULL;
  5152. struct map_session_data* sd;
  5153. struct script_data* data;
  5154. if( ( sd = script_rid2sd(st) ) == NULL )
  5155. {
  5156. return 0;
  5157. }
  5158. data = script_getdata(st,2);
  5159. get_val(st, data); // convert into value in case of a variable
  5160. if( data_isstring(data) )
  5161. {// item name
  5162. id = itemdb_searchname(conv_str(st, data));
  5163. }
  5164. else
  5165. {// item id
  5166. id = itemdb_exists(conv_num(st, data));
  5167. }
  5168. if( id == NULL )
  5169. {
  5170. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5171. script_pushint(st,0);
  5172. return 1;
  5173. }
  5174. nameid = id->nameid;
  5175. amount = script_getnum(st,3);
  5176. if( amount < 1 )
  5177. {
  5178. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5179. script_pushint(st,0);
  5180. return 1;
  5181. }
  5182. weight = itemdb_weight(nameid)*amount;
  5183. if( weight + sd->weight > sd->max_weight )
  5184. {// too heavy
  5185. script_pushint(st,0);
  5186. return 0;
  5187. }
  5188. switch( pc_checkadditem(sd, nameid, amount) )
  5189. {
  5190. case ADDITEM_EXIST:
  5191. // item is already in inventory, but there is still space for the requested amount
  5192. break;
  5193. case ADDITEM_NEW:
  5194. slots = pc_inventoryblank(sd);
  5195. if( itemdb_isstackable(nameid) )
  5196. {// stackable
  5197. if( slots < 1 )
  5198. {
  5199. script_pushint(st,0);
  5200. return 0;
  5201. }
  5202. }
  5203. else
  5204. {// non-stackable
  5205. if( slots < amount )
  5206. {
  5207. script_pushint(st,0);
  5208. return 0;
  5209. }
  5210. }
  5211. break;
  5212. case ADDITEM_OVERAMOUNT:
  5213. script_pushint(st,0);
  5214. return 0;
  5215. }
  5216. script_pushint(st,1);
  5217. return 0;
  5218. }
  5219. /*==========================================
  5220. * getitem <item id>,<amount>{,<account ID>};
  5221. * getitem "<item name>",<amount>{,<account ID>};
  5222. *------------------------------------------*/
  5223. BUILDIN_FUNC(getitem)
  5224. {
  5225. int nameid,amount,get_count,i,flag = 0;
  5226. struct item it;
  5227. TBL_PC *sd;
  5228. struct script_data *data;
  5229. data=script_getdata(st,2);
  5230. get_val(st,data);
  5231. if( data_isstring(data) )
  5232. {// "<item name>"
  5233. const char *name=conv_str(st,data);
  5234. struct item_data *item_data = itemdb_searchname(name);
  5235. if( item_data == NULL ){
  5236. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5237. return 1; //No item created.
  5238. }
  5239. nameid=item_data->nameid;
  5240. } else if( data_isint(data) )
  5241. {// <item id>
  5242. nameid=conv_num(st,data);
  5243. //Violet Box, Blue Box, etc - random item pick
  5244. if( nameid < 0 ) {
  5245. nameid = -nameid;
  5246. flag = 1;
  5247. }
  5248. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5249. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5250. return 1; //No item created.
  5251. }
  5252. } else {
  5253. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5254. return 1;
  5255. }
  5256. // <amount>
  5257. if( (amount=script_getnum(st,3)) <= 0)
  5258. return 0; //return if amount <=0, skip the useles iteration
  5259. memset(&it,0,sizeof(it));
  5260. it.nameid=nameid;
  5261. if(!flag)
  5262. it.identify=1;
  5263. else
  5264. it.identify=itemdb_isidentified(nameid);
  5265. if( script_hasdata(st,4) )
  5266. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5267. else
  5268. sd=script_rid2sd(st); // Attached player
  5269. if( sd == NULL ) // no target
  5270. return 0;
  5271. //Check if it's stackable.
  5272. if (!itemdb_isstackable(nameid))
  5273. get_count = 1;
  5274. else
  5275. get_count = amount;
  5276. for (i = 0; i < amount; i += get_count)
  5277. {
  5278. // if not pet egg
  5279. if (!pet_create_egg(sd, nameid))
  5280. {
  5281. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5282. {
  5283. clif_additem(sd, 0, 0, flag);
  5284. if( pc_candrop(sd,&it) )
  5285. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5286. }
  5287. }
  5288. }
  5289. return 0;
  5290. }
  5291. /*==========================================
  5292. *
  5293. *------------------------------------------*/
  5294. BUILDIN_FUNC(getitem2)
  5295. {
  5296. int nameid,amount,get_count,i,flag = 0;
  5297. int iden,ref,attr,c1,c2,c3,c4;
  5298. struct item_data *item_data;
  5299. struct item item_tmp;
  5300. TBL_PC *sd;
  5301. struct script_data *data;
  5302. if( script_hasdata(st,11) )
  5303. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5304. else
  5305. sd=script_rid2sd(st); // Attached player
  5306. if( sd == NULL ) // no target
  5307. return 0;
  5308. data=script_getdata(st,2);
  5309. get_val(st,data);
  5310. if( data_isstring(data) ){
  5311. const char *name=conv_str(st,data);
  5312. struct item_data *item_data = itemdb_searchname(name);
  5313. if( item_data )
  5314. nameid=item_data->nameid;
  5315. else
  5316. nameid=UNKNOWN_ITEM_ID;
  5317. }else
  5318. nameid=conv_num(st,data);
  5319. amount=script_getnum(st,3);
  5320. iden=script_getnum(st,4);
  5321. ref=script_getnum(st,5);
  5322. attr=script_getnum(st,6);
  5323. c1=(short)script_getnum(st,7);
  5324. c2=(short)script_getnum(st,8);
  5325. c3=(short)script_getnum(st,9);
  5326. c4=(short)script_getnum(st,10);
  5327. if(nameid<0) { // ランダム
  5328. nameid = -nameid;
  5329. flag = 1;
  5330. }
  5331. if(nameid > 0) {
  5332. memset(&item_tmp,0,sizeof(item_tmp));
  5333. item_data=itemdb_exists(nameid);
  5334. if (item_data == NULL)
  5335. return -1;
  5336. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5337. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5338. }
  5339. else if(item_data->type==IT_PETEGG) {
  5340. iden = 1;
  5341. ref = 0;
  5342. }
  5343. else {
  5344. iden = 1;
  5345. ref = attr = 0;
  5346. }
  5347. item_tmp.nameid=nameid;
  5348. if(!flag)
  5349. item_tmp.identify=iden;
  5350. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5351. item_tmp.identify=0;
  5352. item_tmp.refine=ref;
  5353. item_tmp.attribute=attr;
  5354. item_tmp.card[0]=(short)c1;
  5355. item_tmp.card[1]=(short)c2;
  5356. item_tmp.card[2]=(short)c3;
  5357. item_tmp.card[3]=(short)c4;
  5358. //Check if it's stackable.
  5359. if (!itemdb_isstackable(nameid))
  5360. get_count = 1;
  5361. else
  5362. get_count = amount;
  5363. for (i = 0; i < amount; i += get_count)
  5364. {
  5365. // if not pet egg
  5366. if (!pet_create_egg(sd, nameid))
  5367. {
  5368. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5369. {
  5370. clif_additem(sd, 0, 0, flag);
  5371. if( pc_candrop(sd,&item_tmp) )
  5372. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5373. }
  5374. }
  5375. }
  5376. }
  5377. return 0;
  5378. }
  5379. /*==========================================
  5380. * rentitem <item id>,<seconds>
  5381. * rentitem "<item name>",<seconds>
  5382. *------------------------------------------*/
  5383. BUILDIN_FUNC(rentitem)
  5384. {
  5385. struct map_session_data *sd;
  5386. struct script_data *data;
  5387. struct item it;
  5388. int seconds;
  5389. int nameid = 0, flag;
  5390. data = script_getdata(st,2);
  5391. get_val(st,data);
  5392. if( (sd = script_rid2sd(st)) == NULL )
  5393. return 0;
  5394. if( data_isstring(data) )
  5395. {
  5396. const char *name = conv_str(st,data);
  5397. struct item_data *itd = itemdb_searchname(name);
  5398. if( itd == NULL )
  5399. {
  5400. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5401. return 1;
  5402. }
  5403. nameid = itd->nameid;
  5404. }
  5405. else if( data_isint(data) )
  5406. {
  5407. nameid = conv_num(st,data);
  5408. if( nameid <= 0 || !itemdb_exists(nameid) )
  5409. {
  5410. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5411. return 1;
  5412. }
  5413. }
  5414. else
  5415. {
  5416. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5417. return 1;
  5418. }
  5419. seconds = script_getnum(st,3);
  5420. memset(&it, 0, sizeof(it));
  5421. it.nameid = nameid;
  5422. it.identify = 1;
  5423. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5424. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5425. {
  5426. clif_additem(sd, 0, 0, flag);
  5427. return 1;
  5428. }
  5429. clif_rental_time(sd->fd, nameid, seconds);
  5430. pc_inventory_rental_add(sd, seconds);
  5431. return 0;
  5432. }
  5433. /*==========================================
  5434. * gets an item with someone's name inscribed [Skotlex]
  5435. * getinscribeditem item_num, character_name
  5436. * Returned Qty is always 1, only works on equip-able
  5437. * equipment
  5438. *------------------------------------------*/
  5439. BUILDIN_FUNC(getnameditem)
  5440. {
  5441. int nameid;
  5442. struct item item_tmp;
  5443. TBL_PC *sd, *tsd;
  5444. struct script_data *data;
  5445. sd = script_rid2sd(st);
  5446. if (sd == NULL)
  5447. { //Player not attached!
  5448. script_pushint(st,0);
  5449. return 0;
  5450. }
  5451. data=script_getdata(st,2);
  5452. get_val(st,data);
  5453. if( data_isstring(data) ){
  5454. const char *name=conv_str(st,data);
  5455. struct item_data *item_data = itemdb_searchname(name);
  5456. if( item_data == NULL)
  5457. { //Failed
  5458. script_pushint(st,0);
  5459. return 0;
  5460. }
  5461. nameid = item_data->nameid;
  5462. }else
  5463. nameid = conv_num(st,data);
  5464. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5465. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5466. script_pushint(st,0);
  5467. return 0;
  5468. }
  5469. data=script_getdata(st,3);
  5470. get_val(st,data);
  5471. if( data_isstring(data) ) //Char Name
  5472. tsd=map_nick2sd(conv_str(st,data));
  5473. else //Char Id was given
  5474. tsd=map_charid2sd(conv_num(st,data));
  5475. if( tsd == NULL )
  5476. { //Failed
  5477. script_pushint(st,0);
  5478. return 0;
  5479. }
  5480. memset(&item_tmp,0,sizeof(item_tmp));
  5481. item_tmp.nameid=nameid;
  5482. item_tmp.amount=1;
  5483. item_tmp.identify=1;
  5484. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5485. item_tmp.card[2]=tsd->status.char_id;
  5486. item_tmp.card[3]=tsd->status.char_id >> 16;
  5487. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5488. script_pushint(st,0);
  5489. return 0; //Failed to add item, we will not drop if they don't fit
  5490. }
  5491. script_pushint(st,1);
  5492. return 0;
  5493. }
  5494. /*==========================================
  5495. * gets a random item ID from an item group [Skotlex]
  5496. * groupranditem group_num
  5497. *------------------------------------------*/
  5498. BUILDIN_FUNC(grouprandomitem)
  5499. {
  5500. int group;
  5501. group = script_getnum(st,2);
  5502. script_pushint(st,itemdb_searchrandomid(group));
  5503. return 0;
  5504. }
  5505. /*==========================================
  5506. *
  5507. *------------------------------------------*/
  5508. BUILDIN_FUNC(makeitem)
  5509. {
  5510. int nameid,amount,flag = 0;
  5511. int x,y,m;
  5512. const char *mapname;
  5513. struct item item_tmp;
  5514. struct script_data *data;
  5515. data=script_getdata(st,2);
  5516. get_val(st,data);
  5517. if( data_isstring(data) ){
  5518. const char *name=conv_str(st,data);
  5519. struct item_data *item_data = itemdb_searchname(name);
  5520. if( item_data )
  5521. nameid=item_data->nameid;
  5522. else
  5523. nameid=UNKNOWN_ITEM_ID;
  5524. }else
  5525. nameid=conv_num(st,data);
  5526. amount=script_getnum(st,3);
  5527. mapname =script_getstr(st,4);
  5528. x =script_getnum(st,5);
  5529. y =script_getnum(st,6);
  5530. if(strcmp(mapname,"this")==0)
  5531. {
  5532. TBL_PC *sd;
  5533. sd = script_rid2sd(st);
  5534. if (!sd) return 0; //Failed...
  5535. m=sd->bl.m;
  5536. } else
  5537. m=map_mapname2mapid(mapname);
  5538. if(nameid<0) { // ランダム
  5539. nameid = -nameid;
  5540. flag = 1;
  5541. }
  5542. if(nameid > 0) {
  5543. memset(&item_tmp,0,sizeof(item_tmp));
  5544. item_tmp.nameid=nameid;
  5545. if(!flag)
  5546. item_tmp.identify=1;
  5547. else
  5548. item_tmp.identify=itemdb_isidentified(nameid);
  5549. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5550. }
  5551. return 0;
  5552. }
  5553. /// Counts / deletes the current item given by idx.
  5554. /// Used by buildin_delitem_search
  5555. /// Relies on all input data being already fully valid.
  5556. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5557. {
  5558. int delamount;
  5559. struct item* inv = &sd->status.inventory[idx];
  5560. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5561. if( delete_items )
  5562. {
  5563. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5564. {// delete associated pet
  5565. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5566. }
  5567. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5568. }
  5569. amount[0]-= delamount;
  5570. }
  5571. /// Searches for item(s) and checks, if there is enough of them.
  5572. /// Used by delitem and delitem2
  5573. /// Relies on all input data being already fully valid.
  5574. /// @param exact_match will also match item attributes and cards, not just name id
  5575. /// @return true when all items could be deleted, false when there were not enough items to delete
  5576. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5577. {
  5578. bool delete_items = false;
  5579. int i, amount, important;
  5580. struct item* inv;
  5581. // prefer always non-equipped items
  5582. it->equip = 0;
  5583. // when searching for nameid only, prefer additionally
  5584. if( !exact_match )
  5585. {
  5586. // non-refined items
  5587. it->refine = 0;
  5588. // card-less items
  5589. memset(it->card, 0, sizeof(it->card));
  5590. }
  5591. for(;;)
  5592. {
  5593. amount = it->amount;
  5594. important = 0;
  5595. // 1st pass -- less important items / exact match
  5596. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5597. {
  5598. inv = &sd->status.inventory[i];
  5599. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5600. {// wrong/invalid item
  5601. continue;
  5602. }
  5603. if( inv->equip != it->equip || inv->refine != it->refine )
  5604. {// not matching attributes
  5605. important++;
  5606. continue;
  5607. }
  5608. if( exact_match )
  5609. {
  5610. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5611. {// not matching exact attributes
  5612. continue;
  5613. }
  5614. }
  5615. else
  5616. {
  5617. if( sd->inventory_data[i]->type == IT_PETEGG )
  5618. {
  5619. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5620. {// pet which cannot be deleted
  5621. continue;
  5622. }
  5623. }
  5624. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  5625. {// named/carded item
  5626. important++;
  5627. continue;
  5628. }
  5629. }
  5630. // count / delete item
  5631. buildin_delitem_delete(sd, i, &amount, delete_items);
  5632. }
  5633. // 2nd pass -- any matching item
  5634. if( amount == 0 || important == 0 )
  5635. {// either everything was already consumed or no items were skipped
  5636. ;
  5637. }
  5638. else 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( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  5646. {// pet which cannot be deleted
  5647. continue;
  5648. }
  5649. if( exact_match )
  5650. {
  5651. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5652. {// not matching attributes
  5653. continue;
  5654. }
  5655. }
  5656. // count / delete item
  5657. buildin_delitem_delete(sd, i, &amount, delete_items);
  5658. }
  5659. if( amount )
  5660. {// not enough items
  5661. return false;
  5662. }
  5663. else if( delete_items )
  5664. {// we are done with the work
  5665. return true;
  5666. }
  5667. else
  5668. {// get rid of the items now
  5669. delete_items = true;
  5670. }
  5671. }
  5672. }
  5673. /// Deletes items from the target/attached player.
  5674. /// Prioritizes ordinary items.
  5675. ///
  5676. /// delitem <item id>,<amount>{,<account id>}
  5677. /// delitem "<item name>",<amount>{,<account id>}
  5678. BUILDIN_FUNC(delitem)
  5679. {
  5680. TBL_PC *sd;
  5681. struct item it;
  5682. struct script_data *data;
  5683. if( script_hasdata(st,4) )
  5684. {
  5685. int account_id = script_getnum(st,4);
  5686. sd = map_id2sd(account_id); // <account id>
  5687. if( sd == NULL )
  5688. {
  5689. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5690. st->state = END;
  5691. return 1;
  5692. }
  5693. }
  5694. else
  5695. {
  5696. sd = script_rid2sd(st);// attached player
  5697. if( sd == NULL )
  5698. return 0;
  5699. }
  5700. data = script_getdata(st,2);
  5701. get_val(st,data);
  5702. if( data_isstring(data) )
  5703. {
  5704. const char* item_name = conv_str(st,data);
  5705. struct item_data* id = itemdb_searchname(item_name);
  5706. if( id == NULL )
  5707. {
  5708. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5709. st->state = END;
  5710. return 1;
  5711. }
  5712. it.nameid = id->nameid;// "<item name>"
  5713. }
  5714. else
  5715. {
  5716. it.nameid = conv_num(st,data);// <item id>
  5717. if( !itemdb_exists( it.nameid ) )
  5718. {
  5719. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5720. st->state = END;
  5721. return 1;
  5722. }
  5723. }
  5724. it.amount=script_getnum(st,3);
  5725. if( it.amount <= 0 )
  5726. return 0;// nothing to do
  5727. if( buildin_delitem_search(sd, &it, false) )
  5728. {// success
  5729. return 0;
  5730. }
  5731. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5732. st->state = END;
  5733. clif_scriptclose(sd, st->oid);
  5734. return 1;
  5735. }
  5736. /// Deletes items from the target/attached player.
  5737. ///
  5738. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5739. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5740. BUILDIN_FUNC(delitem2)
  5741. {
  5742. TBL_PC *sd;
  5743. struct item it;
  5744. struct script_data *data;
  5745. if( script_hasdata(st,11) )
  5746. {
  5747. int account_id = script_getnum(st,11);
  5748. sd = map_id2sd(account_id); // <account id>
  5749. if( sd == NULL )
  5750. {
  5751. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5752. st->state = END;
  5753. return 1;
  5754. }
  5755. }
  5756. else
  5757. {
  5758. sd = script_rid2sd(st);// attached player
  5759. if( sd == NULL )
  5760. return 0;
  5761. }
  5762. data = script_getdata(st,2);
  5763. get_val(st,data);
  5764. if( data_isstring(data) )
  5765. {
  5766. const char* item_name = conv_str(st,data);
  5767. struct item_data* id = itemdb_searchname(item_name);
  5768. if( id == NULL )
  5769. {
  5770. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5771. st->state = END;
  5772. return 1;
  5773. }
  5774. it.nameid = id->nameid;// "<item name>"
  5775. }
  5776. else
  5777. {
  5778. it.nameid = conv_num(st,data);// <item id>
  5779. if( !itemdb_exists( it.nameid ) )
  5780. {
  5781. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5782. st->state = END;
  5783. return 1;
  5784. }
  5785. }
  5786. it.amount=script_getnum(st,3);
  5787. it.identify=script_getnum(st,4);
  5788. it.refine=script_getnum(st,5);
  5789. it.attribute=script_getnum(st,6);
  5790. it.card[0]=(short)script_getnum(st,7);
  5791. it.card[1]=(short)script_getnum(st,8);
  5792. it.card[2]=(short)script_getnum(st,9);
  5793. it.card[3]=(short)script_getnum(st,10);
  5794. if( it.amount <= 0 )
  5795. return 0;// nothing to do
  5796. if( buildin_delitem_search(sd, &it, true) )
  5797. {// success
  5798. return 0;
  5799. }
  5800. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5801. st->state = END;
  5802. clif_scriptclose(sd, st->oid);
  5803. return 1;
  5804. }
  5805. /*==========================================
  5806. * Enables/Disables use of items while in an NPC [Skotlex]
  5807. *------------------------------------------*/
  5808. BUILDIN_FUNC(enableitemuse)
  5809. {
  5810. TBL_PC *sd;
  5811. sd=script_rid2sd(st);
  5812. if (sd)
  5813. sd->npc_item_flag = st->oid;
  5814. return 0;
  5815. }
  5816. BUILDIN_FUNC(disableitemuse)
  5817. {
  5818. TBL_PC *sd;
  5819. sd=script_rid2sd(st);
  5820. if (sd)
  5821. sd->npc_item_flag = 0;
  5822. return 0;
  5823. }
  5824. /*==========================================
  5825. *キャラ関係のパラメータ取得
  5826. *------------------------------------------*/
  5827. BUILDIN_FUNC(readparam)
  5828. {
  5829. int type;
  5830. TBL_PC *sd;
  5831. type=script_getnum(st,2);
  5832. if( script_hasdata(st,3) )
  5833. sd=map_nick2sd(script_getstr(st,3));
  5834. else
  5835. sd=script_rid2sd(st);
  5836. if(sd==NULL){
  5837. script_pushint(st,-1);
  5838. return 0;
  5839. }
  5840. script_pushint(st,pc_readparam(sd,type));
  5841. return 0;
  5842. }
  5843. /*==========================================
  5844. *キャラ関係のID取得
  5845. *------------------------------------------*/
  5846. BUILDIN_FUNC(getcharid)
  5847. {
  5848. int num;
  5849. TBL_PC *sd;
  5850. num = script_getnum(st,2);
  5851. if( script_hasdata(st,3) )
  5852. sd=map_nick2sd(script_getstr(st,3));
  5853. else
  5854. sd=script_rid2sd(st);
  5855. if(sd==NULL){
  5856. script_pushint(st,0); //return 0, according docs
  5857. return 0;
  5858. }
  5859. switch( num ) {
  5860. case 0: script_pushint(st,sd->status.char_id); break;
  5861. case 1: script_pushint(st,sd->status.party_id); break;
  5862. case 2: script_pushint(st,sd->status.guild_id); break;
  5863. case 3: script_pushint(st,sd->status.account_id); break;
  5864. case 4: script_pushint(st,sd->bg_id); break;
  5865. default:
  5866. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5867. script_pushint(st,0);
  5868. break;
  5869. }
  5870. return 0;
  5871. }
  5872. /*==========================================
  5873. * returns the GID of an NPC
  5874. *------------------------------------------*/
  5875. BUILDIN_FUNC(getnpcid)
  5876. {
  5877. int num = script_getnum(st,2);
  5878. struct npc_data* nd = NULL;
  5879. if( script_hasdata(st,3) )
  5880. {// unique npc name
  5881. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  5882. {
  5883. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  5884. script_pushint(st,0);
  5885. return 1;
  5886. }
  5887. }
  5888. switch (num) {
  5889. case 0:
  5890. script_pushint(st,nd ? nd->bl.id : st->oid);
  5891. break;
  5892. default:
  5893. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  5894. script_pushint(st,0);
  5895. return 1;
  5896. }
  5897. return 0;
  5898. }
  5899. /*==========================================
  5900. *指定IDのPT名取得
  5901. *------------------------------------------*/
  5902. BUILDIN_FUNC(getpartyname)
  5903. {
  5904. int party_id;
  5905. struct party_data* p;
  5906. party_id = script_getnum(st,2);
  5907. if( ( p = party_search(party_id) ) != NULL )
  5908. {
  5909. script_pushstrcopy(st,p->party.name);
  5910. }
  5911. else
  5912. {
  5913. script_pushconststr(st,"null");
  5914. }
  5915. return 0;
  5916. }
  5917. /*==========================================
  5918. *指定IDのPT人数とメンバーID取得
  5919. *------------------------------------------*/
  5920. BUILDIN_FUNC(getpartymember)
  5921. {
  5922. struct party_data *p;
  5923. int i,j=0,type=0;
  5924. p=party_search(script_getnum(st,2));
  5925. if( script_hasdata(st,3) )
  5926. type=script_getnum(st,3);
  5927. if(p!=NULL){
  5928. for(i=0;i<MAX_PARTY;i++){
  5929. if(p->party.member[i].account_id){
  5930. switch (type) {
  5931. case 2:
  5932. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  5933. break;
  5934. case 1:
  5935. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  5936. break;
  5937. default:
  5938. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  5939. }
  5940. j++;
  5941. }
  5942. }
  5943. }
  5944. mapreg_setreg(add_str("$@partymembercount"),j);
  5945. return 0;
  5946. }
  5947. /*==========================================
  5948. * Retrieves party leader. if flag is specified,
  5949. * return some of the leader data. Otherwise, return name.
  5950. *------------------------------------------*/
  5951. BUILDIN_FUNC(getpartyleader)
  5952. {
  5953. int party_id, type = 0, i=0;
  5954. struct party_data *p;
  5955. party_id=script_getnum(st,2);
  5956. if( script_hasdata(st,3) )
  5957. type=script_getnum(st,3);
  5958. p=party_search(party_id);
  5959. if (p) //Search leader
  5960. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5961. if (!p || i == MAX_PARTY) { //leader not found
  5962. if (type)
  5963. script_pushint(st,-1);
  5964. else
  5965. script_pushconststr(st,"null");
  5966. return 0;
  5967. }
  5968. switch (type) {
  5969. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5970. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5971. case 3: script_pushint(st,p->party.member[i].class_); break;
  5972. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5973. case 5: script_pushint(st,p->party.member[i].lv); break;
  5974. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5975. }
  5976. return 0;
  5977. }
  5978. /*==========================================
  5979. *指定IDのギルド名取得
  5980. *------------------------------------------*/
  5981. BUILDIN_FUNC(getguildname)
  5982. {
  5983. int guild_id;
  5984. struct guild* g;
  5985. guild_id = script_getnum(st,2);
  5986. if( ( g = guild_search(guild_id) ) != NULL )
  5987. {
  5988. script_pushstrcopy(st,g->name);
  5989. }
  5990. else
  5991. {
  5992. script_pushconststr(st,"null");
  5993. }
  5994. return 0;
  5995. }
  5996. /*==========================================
  5997. *指定IDのGuildMaster名取得
  5998. *------------------------------------------*/
  5999. BUILDIN_FUNC(getguildmaster)
  6000. {
  6001. int guild_id;
  6002. struct guild* g;
  6003. guild_id = script_getnum(st,2);
  6004. if( ( g = guild_search(guild_id) ) != NULL )
  6005. {
  6006. script_pushstrcopy(st,g->member[0].name);
  6007. }
  6008. else
  6009. {
  6010. script_pushconststr(st,"null");
  6011. }
  6012. return 0;
  6013. }
  6014. BUILDIN_FUNC(getguildmasterid)
  6015. {
  6016. int guild_id;
  6017. struct guild* g;
  6018. guild_id = script_getnum(st,2);
  6019. if( ( g = guild_search(guild_id) ) != NULL )
  6020. {
  6021. script_pushint(st,g->member[0].char_id);
  6022. }
  6023. else
  6024. {
  6025. script_pushint(st,0);
  6026. }
  6027. return 0;
  6028. }
  6029. /*==========================================
  6030. * キャラクタの名前
  6031. *------------------------------------------*/
  6032. BUILDIN_FUNC(strcharinfo)
  6033. {
  6034. TBL_PC *sd;
  6035. int num;
  6036. struct guild* g;
  6037. struct party_data* p;
  6038. sd=script_rid2sd(st);
  6039. if (!sd) { //Avoid crashing....
  6040. script_pushconststr(st,"");
  6041. return 0;
  6042. }
  6043. num=script_getnum(st,2);
  6044. switch(num){
  6045. case 0:
  6046. script_pushstrcopy(st,sd->status.name);
  6047. break;
  6048. case 1:
  6049. if( ( p = party_search(sd->status.party_id) ) != NULL )
  6050. {
  6051. script_pushstrcopy(st,p->party.name);
  6052. }
  6053. else
  6054. {
  6055. script_pushconststr(st,"");
  6056. }
  6057. break;
  6058. case 2:
  6059. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  6060. {
  6061. script_pushstrcopy(st,g->name);
  6062. }
  6063. else
  6064. {
  6065. script_pushconststr(st,"");
  6066. }
  6067. break;
  6068. case 3:
  6069. script_pushconststr(st,map[sd->bl.m].name);
  6070. break;
  6071. default:
  6072. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6073. script_pushconststr(st,"");
  6074. break;
  6075. }
  6076. return 0;
  6077. }
  6078. /*==========================================
  6079. * 呼び出し元のNPC情報を取得する
  6080. *------------------------------------------*/
  6081. BUILDIN_FUNC(strnpcinfo)
  6082. {
  6083. TBL_NPC* nd;
  6084. int num;
  6085. char *buf,*name=NULL;
  6086. nd = map_id2nd(st->oid);
  6087. if (!nd) {
  6088. script_pushconststr(st, "");
  6089. return 0;
  6090. }
  6091. num = script_getnum(st,2);
  6092. switch(num){
  6093. case 0: // display name
  6094. name = aStrdup(nd->name);
  6095. break;
  6096. case 1: // visible part of display name
  6097. if((buf = strchr(nd->name,'#')) != NULL)
  6098. {
  6099. name = aStrdup(nd->name);
  6100. name[buf - nd->name] = 0;
  6101. } else // Return the name, there is no '#' present
  6102. name = aStrdup(nd->name);
  6103. break;
  6104. case 2: // # fragment
  6105. if((buf = strchr(nd->name,'#')) != NULL)
  6106. name = aStrdup(buf+1);
  6107. break;
  6108. case 3: // unique name
  6109. name = aStrdup(nd->exname);
  6110. break;
  6111. case 4: // map name
  6112. name = aStrdup(map[nd->bl.m].name);
  6113. break;
  6114. }
  6115. if(name)
  6116. script_pushstr(st, name);
  6117. else
  6118. script_pushconststr(st, "");
  6119. return 0;
  6120. }
  6121. // aegis->athena slot position conversion table
  6122. 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};
  6123. /*==========================================
  6124. * GetEquipID(Pos); Pos: 1-10
  6125. *------------------------------------------*/
  6126. BUILDIN_FUNC(getequipid)
  6127. {
  6128. int i, num;
  6129. TBL_PC* sd;
  6130. struct item_data* item;
  6131. sd = script_rid2sd(st);
  6132. if( sd == NULL )
  6133. return 0;
  6134. num = script_getnum(st,2) - 1;
  6135. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6136. {
  6137. script_pushint(st,-1);
  6138. return 0;
  6139. }
  6140. // get inventory position of item
  6141. i = pc_checkequip(sd,equip[num]);
  6142. if( i < 0 )
  6143. {
  6144. script_pushint(st,-1);
  6145. return 0;
  6146. }
  6147. item = sd->inventory_data[i];
  6148. if( item != 0 )
  6149. script_pushint(st,item->nameid);
  6150. else
  6151. script_pushint(st,0);
  6152. return 0;
  6153. }
  6154. /*==========================================
  6155. * 装備名文字列(精錬メニュー用)
  6156. *------------------------------------------*/
  6157. BUILDIN_FUNC(getequipname)
  6158. {
  6159. int i, num;
  6160. TBL_PC* sd;
  6161. struct item_data* item;
  6162. sd = script_rid2sd(st);
  6163. if( sd == NULL )
  6164. return 0;
  6165. num = script_getnum(st,2) - 1;
  6166. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6167. {
  6168. script_pushconststr(st,"");
  6169. return 0;
  6170. }
  6171. // get inventory position of item
  6172. i = pc_checkequip(sd,equip[num]);
  6173. if( i < 0 )
  6174. {
  6175. script_pushint(st,-1);
  6176. return 0;
  6177. }
  6178. item = sd->inventory_data[i];
  6179. if( item != 0 )
  6180. script_pushstrcopy(st,item->jname);
  6181. else
  6182. script_pushconststr(st,"");
  6183. return 0;
  6184. }
  6185. /*==========================================
  6186. * getbrokenid [Valaris]
  6187. *------------------------------------------*/
  6188. BUILDIN_FUNC(getbrokenid)
  6189. {
  6190. int i,num,id=0,brokencounter=0;
  6191. TBL_PC *sd;
  6192. sd = script_rid2sd(st);
  6193. if( sd == NULL )
  6194. return 0;
  6195. num=script_getnum(st,2);
  6196. for(i=0; i<MAX_INVENTORY; i++) {
  6197. if(sd->status.inventory[i].attribute){
  6198. brokencounter++;
  6199. if(num==brokencounter){
  6200. id=sd->status.inventory[i].nameid;
  6201. break;
  6202. }
  6203. }
  6204. }
  6205. script_pushint(st,id);
  6206. return 0;
  6207. }
  6208. /*==========================================
  6209. * repair [Valaris]
  6210. *------------------------------------------*/
  6211. BUILDIN_FUNC(repair)
  6212. {
  6213. int i,num;
  6214. int repaircounter=0;
  6215. TBL_PC *sd;
  6216. sd = script_rid2sd(st);
  6217. if( sd == NULL )
  6218. return 0;
  6219. num=script_getnum(st,2);
  6220. for(i=0; i<MAX_INVENTORY; i++) {
  6221. if(sd->status.inventory[i].attribute){
  6222. repaircounter++;
  6223. if(num==repaircounter){
  6224. sd->status.inventory[i].attribute=0;
  6225. clif_equiplist(sd);
  6226. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6227. clif_misceffect(&sd->bl, 3);
  6228. break;
  6229. }
  6230. }
  6231. }
  6232. return 0;
  6233. }
  6234. /*==========================================
  6235. * 装備チェック
  6236. *------------------------------------------*/
  6237. BUILDIN_FUNC(getequipisequiped)
  6238. {
  6239. int i=-1,num;
  6240. TBL_PC *sd;
  6241. num=script_getnum(st,2);
  6242. sd = script_rid2sd(st);
  6243. if( sd == NULL )
  6244. return 0;
  6245. if (num > 0 && num <= ARRAYLENGTH(equip))
  6246. i=pc_checkequip(sd,equip[num-1]);
  6247. if(i >= 0)
  6248. script_pushint(st,1);
  6249. else
  6250. script_pushint(st,0);
  6251. return 0;
  6252. }
  6253. /*==========================================
  6254. * 装備品精錬可能チェック
  6255. *------------------------------------------*/
  6256. BUILDIN_FUNC(getequipisenableref)
  6257. {
  6258. int i=-1,num;
  6259. TBL_PC *sd;
  6260. num=script_getnum(st,2);
  6261. sd = script_rid2sd(st);
  6262. if( sd == NULL )
  6263. return 0;
  6264. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6265. i = pc_checkequip(sd,equip[num-1]);
  6266. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6267. script_pushint(st,1);
  6268. else
  6269. script_pushint(st,0);
  6270. return 0;
  6271. }
  6272. /*==========================================
  6273. * 装備品鑑定チェック
  6274. *------------------------------------------*/
  6275. BUILDIN_FUNC(getequipisidentify)
  6276. {
  6277. int i=-1,num;
  6278. TBL_PC *sd;
  6279. num=script_getnum(st,2);
  6280. sd = script_rid2sd(st);
  6281. if( sd == NULL )
  6282. return 0;
  6283. if (num > 0 && num <= ARRAYLENGTH(equip))
  6284. i=pc_checkequip(sd,equip[num-1]);
  6285. if(i >= 0)
  6286. script_pushint(st,sd->status.inventory[i].identify);
  6287. else
  6288. script_pushint(st,0);
  6289. return 0;
  6290. }
  6291. /*==========================================
  6292. * 装備品精錬度
  6293. *------------------------------------------*/
  6294. BUILDIN_FUNC(getequiprefinerycnt)
  6295. {
  6296. int i=-1,num;
  6297. TBL_PC *sd;
  6298. num=script_getnum(st,2);
  6299. sd = script_rid2sd(st);
  6300. if( sd == NULL )
  6301. return 0;
  6302. if (num > 0 && num <= ARRAYLENGTH(equip))
  6303. i=pc_checkequip(sd,equip[num-1]);
  6304. if(i >= 0)
  6305. script_pushint(st,sd->status.inventory[i].refine);
  6306. else
  6307. script_pushint(st,0);
  6308. return 0;
  6309. }
  6310. /*==========================================
  6311. * 装備品武器LV
  6312. *------------------------------------------*/
  6313. BUILDIN_FUNC(getequipweaponlv)
  6314. {
  6315. int i=-1,num;
  6316. TBL_PC *sd;
  6317. num=script_getnum(st,2);
  6318. sd = script_rid2sd(st);
  6319. if( sd == NULL )
  6320. return 0;
  6321. if (num > 0 && num <= ARRAYLENGTH(equip))
  6322. i=pc_checkequip(sd,equip[num-1]);
  6323. if(i >= 0 && sd->inventory_data[i])
  6324. script_pushint(st,sd->inventory_data[i]->wlv);
  6325. else
  6326. script_pushint(st,0);
  6327. return 0;
  6328. }
  6329. /*==========================================
  6330. * 装備品精錬成功率
  6331. *------------------------------------------*/
  6332. BUILDIN_FUNC(getequippercentrefinery)
  6333. {
  6334. int i=-1,num;
  6335. TBL_PC *sd;
  6336. num=script_getnum(st,2);
  6337. sd = script_rid2sd(st);
  6338. if( sd == NULL )
  6339. return 0;
  6340. if (num > 0 && num <= ARRAYLENGTH(equip))
  6341. i=pc_checkequip(sd,equip[num-1]);
  6342. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6343. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6344. else
  6345. script_pushint(st,0);
  6346. return 0;
  6347. }
  6348. /*==========================================
  6349. * 精錬成功
  6350. *------------------------------------------*/
  6351. BUILDIN_FUNC(successrefitem)
  6352. {
  6353. int i=-1,num,ep;
  6354. TBL_PC *sd;
  6355. num=script_getnum(st,2);
  6356. sd = script_rid2sd(st);
  6357. if( sd == NULL )
  6358. return 0;
  6359. if (num > 0 && num <= ARRAYLENGTH(equip))
  6360. i=pc_checkequip(sd,equip[num-1]);
  6361. if(i >= 0) {
  6362. ep=sd->status.inventory[i].equip;
  6363. //Logs items, got from (N)PC scripts [Lupus]
  6364. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6365. sd->status.inventory[i].refine++;
  6366. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6367. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6368. clif_delitem(sd,i,1,3);
  6369. //Logs items, got from (N)PC scripts [Lupus]
  6370. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6371. clif_additem(sd,i,1,0);
  6372. pc_equipitem(sd,i,ep);
  6373. clif_misceffect(&sd->bl,3);
  6374. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6375. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6376. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6377. ){ // Fame point system [DracoRPG]
  6378. switch (sd->inventory_data[i]->wlv){
  6379. case 1:
  6380. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6381. break;
  6382. case 2:
  6383. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6384. break;
  6385. case 3:
  6386. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6387. break;
  6388. }
  6389. }
  6390. }
  6391. return 0;
  6392. }
  6393. /*==========================================
  6394. * 精錬失敗
  6395. *------------------------------------------*/
  6396. BUILDIN_FUNC(failedrefitem)
  6397. {
  6398. int i=-1,num;
  6399. TBL_PC *sd;
  6400. num=script_getnum(st,2);
  6401. sd = script_rid2sd(st);
  6402. if( sd == NULL )
  6403. return 0;
  6404. if (num > 0 && num <= ARRAYLENGTH(equip))
  6405. i=pc_checkequip(sd,equip[num-1]);
  6406. if(i >= 0) {
  6407. sd->status.inventory[i].refine = 0;
  6408. pc_unequipitem(sd,i,3);
  6409. // 精錬失敗エフェクトのパケット
  6410. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6411. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6412. // 他の人にも失敗を通知
  6413. clif_misceffect(&sd->bl,2);
  6414. }
  6415. return 0;
  6416. }
  6417. /*==========================================
  6418. *
  6419. *------------------------------------------*/
  6420. BUILDIN_FUNC(statusup)
  6421. {
  6422. int type;
  6423. TBL_PC *sd;
  6424. type=script_getnum(st,2);
  6425. sd = script_rid2sd(st);
  6426. if( sd == NULL )
  6427. return 0;
  6428. pc_statusup(sd,type);
  6429. return 0;
  6430. }
  6431. /*==========================================
  6432. *
  6433. *------------------------------------------*/
  6434. BUILDIN_FUNC(statusup2)
  6435. {
  6436. int type,val;
  6437. TBL_PC *sd;
  6438. type=script_getnum(st,2);
  6439. val=script_getnum(st,3);
  6440. sd = script_rid2sd(st);
  6441. if( sd == NULL )
  6442. return 0;
  6443. pc_statusup2(sd,type,val);
  6444. return 0;
  6445. }
  6446. /// See 'doc/item_bonus.txt'
  6447. ///
  6448. /// bonus <bonus type>,<val1>;
  6449. /// bonus2 <bonus type>,<val1>,<val2>;
  6450. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6451. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6452. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6453. BUILDIN_FUNC(bonus)
  6454. {
  6455. int type;
  6456. int val1;
  6457. int val2 = 0;
  6458. int val3 = 0;
  6459. int val4 = 0;
  6460. int val5 = 0;
  6461. TBL_PC* sd;
  6462. sd = script_rid2sd(st);
  6463. if( sd == NULL )
  6464. return 0; // no player attached
  6465. type = script_getnum(st,2);
  6466. switch( type )
  6467. {
  6468. case SP_AUTOSPELL:
  6469. case SP_AUTOSPELL_WHENHIT:
  6470. case SP_AUTOSPELL_ONSKILL:
  6471. case SP_SKILL_ATK:
  6472. case SP_SKILL_HEAL:
  6473. case SP_SKILL_HEAL2:
  6474. case SP_ADD_SKILL_BLOW:
  6475. case SP_CASTRATE:
  6476. case SP_ADDEFF_ONSKILL:
  6477. // these bonuses support skill names
  6478. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6479. break;
  6480. default:
  6481. val1 = script_getnum(st,3);
  6482. break;
  6483. }
  6484. switch( script_lastdata(st)-2 )
  6485. {
  6486. case 1:
  6487. pc_bonus(sd, type, val1);
  6488. break;
  6489. case 2:
  6490. val2 = script_getnum(st,4);
  6491. pc_bonus2(sd, type, val1, val2);
  6492. break;
  6493. case 3:
  6494. val2 = script_getnum(st,4);
  6495. val3 = script_getnum(st,5);
  6496. pc_bonus3(sd, type, val1, val2, val3);
  6497. break;
  6498. case 4:
  6499. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6500. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6501. else
  6502. val2 = script_getnum(st,4);
  6503. val3 = script_getnum(st,5);
  6504. val4 = script_getnum(st,6);
  6505. pc_bonus4(sd, type, val1, val2, val3, val4);
  6506. break;
  6507. case 5:
  6508. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6509. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6510. else
  6511. val2 = script_getnum(st,4);
  6512. val3 = script_getnum(st,5);
  6513. val4 = script_getnum(st,6);
  6514. val5 = script_getnum(st,7);
  6515. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6516. break;
  6517. default:
  6518. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6519. break;
  6520. }
  6521. return 0;
  6522. }
  6523. BUILDIN_FUNC(autobonus)
  6524. {
  6525. unsigned int dur;
  6526. short rate;
  6527. short atk_type = 0;
  6528. TBL_PC* sd;
  6529. const char *bonus_script, *other_script = NULL;
  6530. sd = script_rid2sd(st);
  6531. if( sd == NULL )
  6532. return 0; // no player attached
  6533. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6534. return 0;
  6535. rate = script_getnum(st,3);
  6536. dur = script_getnum(st,4);
  6537. bonus_script = script_getstr(st,2);
  6538. if( !rate || !dur || !bonus_script )
  6539. return 0;
  6540. if( script_hasdata(st,5) )
  6541. atk_type = script_getnum(st,5);
  6542. if( script_hasdata(st,6) )
  6543. other_script = script_getstr(st,6);
  6544. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6545. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6546. {
  6547. script_add_autobonus(bonus_script);
  6548. if( other_script )
  6549. script_add_autobonus(other_script);
  6550. }
  6551. return 0;
  6552. }
  6553. BUILDIN_FUNC(autobonus2)
  6554. {
  6555. unsigned int dur;
  6556. short rate;
  6557. short atk_type = 0;
  6558. TBL_PC* sd;
  6559. const char *bonus_script, *other_script = NULL;
  6560. sd = script_rid2sd(st);
  6561. if( sd == NULL )
  6562. return 0; // no player attached
  6563. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6564. return 0;
  6565. rate = script_getnum(st,3);
  6566. dur = script_getnum(st,4);
  6567. bonus_script = script_getstr(st,2);
  6568. if( !rate || !dur || !bonus_script )
  6569. return 0;
  6570. if( script_hasdata(st,5) )
  6571. atk_type = script_getnum(st,5);
  6572. if( script_hasdata(st,6) )
  6573. other_script = script_getstr(st,6);
  6574. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6575. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6576. {
  6577. script_add_autobonus(bonus_script);
  6578. if( other_script )
  6579. script_add_autobonus(other_script);
  6580. }
  6581. return 0;
  6582. }
  6583. BUILDIN_FUNC(autobonus3)
  6584. {
  6585. unsigned int dur;
  6586. short rate,atk_type;
  6587. TBL_PC* sd;
  6588. const char *bonus_script, *other_script = NULL;
  6589. sd = script_rid2sd(st);
  6590. if( sd == NULL )
  6591. return 0; // no player attached
  6592. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6593. return 0;
  6594. rate = script_getnum(st,3);
  6595. dur = script_getnum(st,4);
  6596. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6597. bonus_script = script_getstr(st,2);
  6598. if( !rate || !dur || !atk_type || !bonus_script )
  6599. return 0;
  6600. if( script_hasdata(st,6) )
  6601. other_script = script_getstr(st,6);
  6602. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6603. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6604. {
  6605. script_add_autobonus(bonus_script);
  6606. if( other_script )
  6607. script_add_autobonus(other_script);
  6608. }
  6609. return 0;
  6610. }
  6611. /// Changes the level of a player skill.
  6612. /// <flag> defaults to 1
  6613. /// <flag>=0 : set the level of the skill
  6614. /// <flag>=1 : set the temporary level of the skill
  6615. /// <flag>=2 : add to the level of the skill
  6616. ///
  6617. /// skill <skill id>,<level>,<flag>
  6618. /// skill <skill id>,<level>
  6619. /// skill "<skill name>",<level>,<flag>
  6620. /// skill "<skill name>",<level>
  6621. BUILDIN_FUNC(skill)
  6622. {
  6623. int id;
  6624. int level;
  6625. int flag = 1;
  6626. TBL_PC* sd;
  6627. sd = script_rid2sd(st);
  6628. if( sd == NULL )
  6629. return 0;// no player attached, report source
  6630. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6631. level = script_getnum(st,3);
  6632. if( script_hasdata(st,4) )
  6633. flag = script_getnum(st,4);
  6634. pc_skill(sd, id, level, flag);
  6635. return 0;
  6636. }
  6637. /// Changes the level of a player skill.
  6638. /// like skill, but <flag> defaults to 2
  6639. ///
  6640. /// addtoskill <skill id>,<amount>,<flag>
  6641. /// addtoskill <skill id>,<amount>
  6642. /// addtoskill "<skill name>",<amount>,<flag>
  6643. /// addtoskill "<skill name>",<amount>
  6644. ///
  6645. /// @see skill
  6646. BUILDIN_FUNC(addtoskill)
  6647. {
  6648. int id;
  6649. int level;
  6650. int flag = 2;
  6651. TBL_PC* sd;
  6652. sd = script_rid2sd(st);
  6653. if( sd == NULL )
  6654. return 0;// no player attached, report source
  6655. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6656. level = script_getnum(st,3);
  6657. if( script_hasdata(st,4) )
  6658. flag = script_getnum(st,4);
  6659. pc_skill(sd, id, level, flag);
  6660. return 0;
  6661. }
  6662. /// Increases the level of a guild skill.
  6663. ///
  6664. /// guildskill <skill id>,<amount>;
  6665. /// guildskill "<skill name>",<amount>;
  6666. BUILDIN_FUNC(guildskill)
  6667. {
  6668. int id;
  6669. int level;
  6670. TBL_PC* sd;
  6671. int i;
  6672. sd = script_rid2sd(st);
  6673. if( sd == NULL )
  6674. return 0;// no player attached, report source
  6675. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6676. level = script_getnum(st,3);
  6677. for( i=0; i < level; i++ )
  6678. guild_skillup(sd, id);
  6679. return 0;
  6680. }
  6681. /// Returns the level of the player skill.
  6682. ///
  6683. /// getskilllv(<skill id>) -> <level>
  6684. /// getskilllv("<skill name>") -> <level>
  6685. BUILDIN_FUNC(getskilllv)
  6686. {
  6687. int id;
  6688. TBL_PC* sd;
  6689. sd = script_rid2sd(st);
  6690. if( sd == NULL )
  6691. return 0;// no player attached, report source
  6692. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6693. script_pushint(st, pc_checkskill(sd,id));
  6694. return 0;
  6695. }
  6696. /// Returns the level of the guild skill.
  6697. ///
  6698. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6699. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6700. BUILDIN_FUNC(getgdskilllv)
  6701. {
  6702. int guild_id;
  6703. int skill_id;
  6704. struct guild* g;
  6705. guild_id = script_getnum(st,2);
  6706. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6707. g = guild_search(guild_id);
  6708. if( g == NULL )
  6709. script_pushint(st, -1);
  6710. else
  6711. script_pushint(st, guild_checkskill(g,skill_id));
  6712. return 0;
  6713. }
  6714. /// Returns the 'basic_skill_check' setting.
  6715. /// This config determines if the server checks the skill level of NV_BASIC
  6716. /// before allowing the basic actions.
  6717. ///
  6718. /// basicskillcheck() -> <bool>
  6719. BUILDIN_FUNC(basicskillcheck)
  6720. {
  6721. script_pushint(st, battle_config.basic_skill_check);
  6722. return 0;
  6723. }
  6724. /// Returns the GM level of the player.
  6725. ///
  6726. /// getgmlevel() -> <level>
  6727. BUILDIN_FUNC(getgmlevel)
  6728. {
  6729. TBL_PC* sd;
  6730. sd = script_rid2sd(st);
  6731. if( sd == NULL )
  6732. return 0;// no player attached, report source
  6733. script_pushint(st, pc_get_group_level(sd));
  6734. return 0;
  6735. }
  6736. /// Returns the group ID of the player.
  6737. ///
  6738. /// getgroupid() -> <int>
  6739. BUILDIN_FUNC(getgroupid)
  6740. {
  6741. TBL_PC* sd;
  6742. sd = script_rid2sd(st);
  6743. if (sd == NULL)
  6744. return 1; // no player attached, report source
  6745. script_pushint(st, pc_get_group_id(sd));
  6746. return 0;
  6747. }
  6748. /// Terminates the execution of this script instance.
  6749. ///
  6750. /// end
  6751. BUILDIN_FUNC(end)
  6752. {
  6753. st->state = END;
  6754. return 0;
  6755. }
  6756. /// Checks if the player has that effect state (option).
  6757. ///
  6758. /// checkoption(<option>) -> <bool>
  6759. BUILDIN_FUNC(checkoption)
  6760. {
  6761. int option;
  6762. TBL_PC* sd;
  6763. sd = script_rid2sd(st);
  6764. if( sd == NULL )
  6765. return 0;// no player attached, report source
  6766. option = script_getnum(st,2);
  6767. if( sd->sc.option&option )
  6768. script_pushint(st, 1);
  6769. else
  6770. script_pushint(st, 0);
  6771. return 0;
  6772. }
  6773. /// Checks if the player is in that body state (opt1).
  6774. ///
  6775. /// checkoption1(<opt1>) -> <bool>
  6776. BUILDIN_FUNC(checkoption1)
  6777. {
  6778. int opt1;
  6779. TBL_PC* sd;
  6780. sd = script_rid2sd(st);
  6781. if( sd == NULL )
  6782. return 0;// no player attached, report source
  6783. opt1 = script_getnum(st,2);
  6784. if( sd->sc.opt1 == opt1 )
  6785. script_pushint(st, 1);
  6786. else
  6787. script_pushint(st, 0);
  6788. return 0;
  6789. }
  6790. /// Checks if the player has that health state (opt2).
  6791. ///
  6792. /// checkoption2(<opt2>) -> <bool>
  6793. BUILDIN_FUNC(checkoption2)
  6794. {
  6795. int opt2;
  6796. TBL_PC* sd;
  6797. sd = script_rid2sd(st);
  6798. if( sd == NULL )
  6799. return 0;// no player attached, report source
  6800. opt2 = script_getnum(st,2);
  6801. if( sd->sc.opt2&opt2 )
  6802. script_pushint(st, 1);
  6803. else
  6804. script_pushint(st, 0);
  6805. return 0;
  6806. }
  6807. /// Changes the effect state (option) of the player.
  6808. /// <flag> defaults to 1
  6809. /// <flag>=0 : removes the option
  6810. /// <flag>=other : adds the option
  6811. ///
  6812. /// setoption <option>,<flag>;
  6813. /// setoption <option>;
  6814. BUILDIN_FUNC(setoption)
  6815. {
  6816. int option;
  6817. int flag = 1;
  6818. TBL_PC* sd;
  6819. sd = script_rid2sd(st);
  6820. if( sd == NULL )
  6821. return 0;// no player attached, report source
  6822. option = script_getnum(st,2);
  6823. if( script_hasdata(st,3) )
  6824. flag = script_getnum(st,3);
  6825. else if( !option ){// Request to remove everything.
  6826. flag = 0;
  6827. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6828. }
  6829. if( flag ){// Add option
  6830. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6831. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6832. pc_setoption(sd, sd->sc.option|option);
  6833. } else// Remove option
  6834. pc_setoption(sd, sd->sc.option&~option);
  6835. return 0;
  6836. }
  6837. /// Returns if the player has a cart.
  6838. ///
  6839. /// checkcart() -> <bool>
  6840. ///
  6841. /// @author Valaris
  6842. BUILDIN_FUNC(checkcart)
  6843. {
  6844. TBL_PC* sd;
  6845. sd = script_rid2sd(st);
  6846. if( sd == NULL )
  6847. return 0;// no player attached, report source
  6848. if( pc_iscarton(sd) )
  6849. script_pushint(st, 1);
  6850. else
  6851. script_pushint(st, 0);
  6852. return 0;
  6853. }
  6854. /// Sets the cart of the player.
  6855. /// <type> defaults to 1
  6856. /// <type>=0 : removes the cart
  6857. /// <type>=1 : Normal cart
  6858. /// <type>=2 : Wooden cart
  6859. /// <type>=3 : Covered cart with flowers and ferns
  6860. /// <type>=4 : Wooden cart with a Panda doll on the back
  6861. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6862. ///
  6863. /// setcart <type>;
  6864. /// setcart;
  6865. BUILDIN_FUNC(setcart)
  6866. {
  6867. int type = 1;
  6868. TBL_PC* sd;
  6869. sd = script_rid2sd(st);
  6870. if( sd == NULL )
  6871. return 0;// no player attached, report source
  6872. if( script_hasdata(st,2) )
  6873. type = script_getnum(st,2);
  6874. pc_setcart(sd, type);
  6875. return 0;
  6876. }
  6877. /// Returns if the player has a falcon.
  6878. ///
  6879. /// checkfalcon() -> <bool>
  6880. ///
  6881. /// @author Valaris
  6882. BUILDIN_FUNC(checkfalcon)
  6883. {
  6884. TBL_PC* sd;
  6885. sd = script_rid2sd(st);
  6886. if( sd == NULL )
  6887. return 0;// no player attached, report source
  6888. if( pc_isfalcon(sd) )
  6889. script_pushint(st, 1);
  6890. else
  6891. script_pushint(st, 0);
  6892. return 0;
  6893. }
  6894. /// Sets if the player has a falcon or not.
  6895. /// <flag> defaults to 1
  6896. ///
  6897. /// setfalcon <flag>;
  6898. /// setfalcon;
  6899. BUILDIN_FUNC(setfalcon)
  6900. {
  6901. int flag = 1;
  6902. TBL_PC* sd;
  6903. sd = script_rid2sd(st);
  6904. if( sd == NULL )
  6905. return 0;// no player attached, report source
  6906. if( script_hasdata(st,2) )
  6907. flag = script_getnum(st,2);
  6908. pc_setfalcon(sd, flag);
  6909. return 0;
  6910. }
  6911. /// Returns if the player is riding.
  6912. ///
  6913. /// checkriding() -> <bool>
  6914. ///
  6915. /// @author Valaris
  6916. BUILDIN_FUNC(checkriding)
  6917. {
  6918. TBL_PC* sd;
  6919. sd = script_rid2sd(st);
  6920. if( sd == NULL )
  6921. return 0;// no player attached, report source
  6922. if( pc_isriding(sd) || sd->sc.option&OPTION_MOUNTING )
  6923. script_pushint(st, 1);
  6924. else
  6925. script_pushint(st, 0);
  6926. return 0;
  6927. }
  6928. /// Sets if the player is riding.
  6929. /// <flag> defaults to 1
  6930. ///
  6931. /// setriding <flag>;
  6932. /// setriding;
  6933. BUILDIN_FUNC(setriding)
  6934. {
  6935. int flag = 1;
  6936. TBL_PC* sd;
  6937. sd = script_rid2sd(st);
  6938. if( sd == NULL )
  6939. return 0;// no player attached, report source
  6940. if( script_hasdata(st,2) )
  6941. flag = script_getnum(st,2);
  6942. pc_setriding(sd, flag);
  6943. return 0;
  6944. }
  6945. /// Sets the save point of the player.
  6946. ///
  6947. /// save "<map name>",<x>,<y>
  6948. /// savepoint "<map name>",<x>,<y>
  6949. BUILDIN_FUNC(savepoint)
  6950. {
  6951. int x;
  6952. int y;
  6953. short map;
  6954. const char* str;
  6955. TBL_PC* sd;
  6956. sd = script_rid2sd(st);
  6957. if( sd == NULL )
  6958. return 0;// no player attached, report source
  6959. str = script_getstr(st, 2);
  6960. x = script_getnum(st,3);
  6961. y = script_getnum(st,4);
  6962. map = mapindex_name2id(str);
  6963. if( map )
  6964. pc_setsavepoint(sd, map, x, y);
  6965. return 0;
  6966. }
  6967. /*==========================================
  6968. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6969. *------------------------------------------*/
  6970. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6971. {
  6972. int type;
  6973. time_t timer;
  6974. struct tm *t;
  6975. type=script_getnum(st,2);
  6976. switch(type){
  6977. case 2:
  6978. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6979. // from the system clock.)
  6980. script_pushint(st,(int)time(NULL));
  6981. break;
  6982. case 1:
  6983. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6984. time(&timer);
  6985. t=localtime(&timer);
  6986. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6987. break;
  6988. case 0:
  6989. default:
  6990. //type 0:(System Ticks)
  6991. script_pushint(st,gettick());
  6992. break;
  6993. }
  6994. return 0;
  6995. }
  6996. /*==========================================
  6997. * GetTime(Type);
  6998. * 1: Sec 2: Min 3: Hour
  6999. * 4: WeekDay 5: MonthDay 6: Month
  7000. * 7: Year
  7001. *------------------------------------------*/
  7002. BUILDIN_FUNC(gettime) /* Asgard Version */
  7003. {
  7004. int type;
  7005. time_t timer;
  7006. struct tm *t;
  7007. type=script_getnum(st,2);
  7008. time(&timer);
  7009. t=localtime(&timer);
  7010. switch(type){
  7011. case 1://Sec(0~59)
  7012. script_pushint(st,t->tm_sec);
  7013. break;
  7014. case 2://Min(0~59)
  7015. script_pushint(st,t->tm_min);
  7016. break;
  7017. case 3://Hour(0~23)
  7018. script_pushint(st,t->tm_hour);
  7019. break;
  7020. case 4://WeekDay(0~6)
  7021. script_pushint(st,t->tm_wday);
  7022. break;
  7023. case 5://MonthDay(01~31)
  7024. script_pushint(st,t->tm_mday);
  7025. break;
  7026. case 6://Month(01~12)
  7027. script_pushint(st,t->tm_mon+1);
  7028. break;
  7029. case 7://Year(20xx)
  7030. script_pushint(st,t->tm_year+1900);
  7031. break;
  7032. case 8://Year Day(01~366)
  7033. script_pushint(st,t->tm_yday+1);
  7034. break;
  7035. default://(format error)
  7036. script_pushint(st,-1);
  7037. break;
  7038. }
  7039. return 0;
  7040. }
  7041. /*==========================================
  7042. * GetTimeStr("TimeFMT", Length);
  7043. *------------------------------------------*/
  7044. BUILDIN_FUNC(gettimestr)
  7045. {
  7046. char *tmpstr;
  7047. const char *fmtstr;
  7048. int maxlen;
  7049. time_t now = time(NULL);
  7050. fmtstr=script_getstr(st,2);
  7051. maxlen=script_getnum(st,3);
  7052. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  7053. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7054. tmpstr[maxlen]='\0';
  7055. script_pushstr(st,tmpstr);
  7056. return 0;
  7057. }
  7058. /*==========================================
  7059. * カプラ倉庫を開く
  7060. *------------------------------------------*/
  7061. BUILDIN_FUNC(openstorage)
  7062. {
  7063. TBL_PC* sd;
  7064. sd = script_rid2sd(st);
  7065. if( sd == NULL )
  7066. return 0;
  7067. storage_storageopen(sd);
  7068. return 0;
  7069. }
  7070. BUILDIN_FUNC(guildopenstorage)
  7071. {
  7072. TBL_PC* sd;
  7073. int ret;
  7074. sd = script_rid2sd(st);
  7075. if( sd == NULL )
  7076. return 0;
  7077. ret = storage_guild_storageopen(sd);
  7078. script_pushint(st,ret);
  7079. return 0;
  7080. }
  7081. /*==========================================
  7082. * アイテムによるスキル発動
  7083. *------------------------------------------*/
  7084. /// itemskill <skill id>,<level>
  7085. /// itemskill "<skill name>",<level>
  7086. BUILDIN_FUNC(itemskill)
  7087. {
  7088. int id;
  7089. int lv;
  7090. TBL_PC* sd;
  7091. sd = script_rid2sd(st);
  7092. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  7093. return 0;
  7094. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7095. lv = script_getnum(st,3);
  7096. sd->skillitem=id;
  7097. sd->skillitemlv=lv;
  7098. clif_item_skill(sd,id,lv);
  7099. return 0;
  7100. }
  7101. /*==========================================
  7102. * アイテム作成
  7103. *------------------------------------------*/
  7104. BUILDIN_FUNC(produce)
  7105. {
  7106. int trigger;
  7107. TBL_PC* sd;
  7108. sd = script_rid2sd(st);
  7109. if( sd == NULL )
  7110. return 0;
  7111. trigger=script_getnum(st,2);
  7112. clif_skill_produce_mix_list(sd, -1, trigger);
  7113. return 0;
  7114. }
  7115. /*==========================================
  7116. *
  7117. *------------------------------------------*/
  7118. BUILDIN_FUNC(cooking)
  7119. {
  7120. int trigger;
  7121. TBL_PC* sd;
  7122. sd = script_rid2sd(st);
  7123. if( sd == NULL )
  7124. return 0;
  7125. trigger=script_getnum(st,2);
  7126. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  7127. return 0;
  7128. }
  7129. /*==========================================
  7130. * NPCでペット作る
  7131. *------------------------------------------*/
  7132. BUILDIN_FUNC(makepet)
  7133. {
  7134. TBL_PC* sd;
  7135. int id,pet_id;
  7136. id=script_getnum(st,2);
  7137. sd = script_rid2sd(st);
  7138. if( sd == NULL )
  7139. return 0;
  7140. pet_id = search_petDB_index(id, PET_CLASS);
  7141. if (pet_id < 0)
  7142. pet_id = search_petDB_index(id, PET_EGG);
  7143. if (pet_id >= 0 && sd) {
  7144. sd->catch_target_class = pet_db[pet_id].class_;
  7145. intif_create_pet(
  7146. sd->status.account_id, sd->status.char_id,
  7147. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7148. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7149. 100, 0, 1, pet_db[pet_id].jname);
  7150. }
  7151. return 0;
  7152. }
  7153. /*==========================================
  7154. * NPCで経験値上げる
  7155. *------------------------------------------*/
  7156. BUILDIN_FUNC(getexp)
  7157. {
  7158. TBL_PC* sd;
  7159. int base=0,job=0;
  7160. double bonus;
  7161. sd = script_rid2sd(st);
  7162. if( sd == NULL )
  7163. return 0;
  7164. base=script_getnum(st,2);
  7165. job =script_getnum(st,3);
  7166. if(base<0 || job<0)
  7167. return 0;
  7168. // bonus for npc-given exp
  7169. bonus = battle_config.quest_exp_rate / 100.;
  7170. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7171. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7172. pc_gainexp(sd, NULL, base, job, true);
  7173. return 0;
  7174. }
  7175. /*==========================================
  7176. * Gain guild exp [Celest]
  7177. *------------------------------------------*/
  7178. BUILDIN_FUNC(guildgetexp)
  7179. {
  7180. TBL_PC* sd;
  7181. int exp;
  7182. sd = script_rid2sd(st);
  7183. if( sd == NULL )
  7184. return 0;
  7185. exp = script_getnum(st,2);
  7186. if(exp < 0)
  7187. return 0;
  7188. if(sd && sd->status.guild_id > 0)
  7189. guild_getexp (sd, exp);
  7190. return 0;
  7191. }
  7192. /*==========================================
  7193. * Changes the guild master of a guild [Skotlex]
  7194. *------------------------------------------*/
  7195. BUILDIN_FUNC(guildchangegm)
  7196. {
  7197. TBL_PC *sd;
  7198. int guild_id;
  7199. const char *name;
  7200. guild_id = script_getnum(st,2);
  7201. name = script_getstr(st,3);
  7202. sd=map_nick2sd(name);
  7203. if (!sd)
  7204. script_pushint(st,0);
  7205. else
  7206. script_pushint(st,guild_gm_change(guild_id, sd));
  7207. return 0;
  7208. }
  7209. /*==========================================
  7210. * モンスター発生
  7211. *------------------------------------------*/
  7212. BUILDIN_FUNC(monster)
  7213. {
  7214. const char* mapn = script_getstr(st,2);
  7215. int x = script_getnum(st,3);
  7216. int y = script_getnum(st,4);
  7217. const char* str = script_getstr(st,5);
  7218. int class_ = script_getnum(st,6);
  7219. int amount = script_getnum(st,7);
  7220. const char* event = "";
  7221. struct map_session_data* sd;
  7222. int m;
  7223. if( script_hasdata(st,8) )
  7224. {
  7225. event = script_getstr(st,8);
  7226. check_event(st, event);
  7227. }
  7228. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7229. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7230. return 1;
  7231. }
  7232. sd = map_id2sd(st->rid);
  7233. if( sd && strcmp(mapn,"this") == 0 )
  7234. m = sd->bl.m;
  7235. else
  7236. {
  7237. m = map_mapname2mapid(mapn);
  7238. if( map[m].flag.src4instance && st->instance_id )
  7239. { // Try to redirect to the instance map, not the src map
  7240. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7241. {
  7242. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7243. return 1;
  7244. }
  7245. }
  7246. }
  7247. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  7248. return 0;
  7249. }
  7250. /*==========================================
  7251. * Request List of Monster Drops
  7252. *------------------------------------------*/
  7253. BUILDIN_FUNC(getmobdrops)
  7254. {
  7255. int class_ = script_getnum(st,2);
  7256. int i, j = 0;
  7257. struct mob_db *mob;
  7258. if( !mobdb_checkid(class_) )
  7259. {
  7260. script_pushint(st, 0);
  7261. return 0;
  7262. }
  7263. mob = mob_db(class_);
  7264. for( i = 0; i < MAX_MOB_DROP; i++ )
  7265. {
  7266. if( mob->dropitem[i].nameid < 1 )
  7267. continue;
  7268. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7269. continue;
  7270. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7271. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7272. j++;
  7273. }
  7274. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7275. script_pushint(st, 1);
  7276. return 0;
  7277. }
  7278. /*==========================================
  7279. * モンスター発生
  7280. *------------------------------------------*/
  7281. BUILDIN_FUNC(areamonster)
  7282. {
  7283. const char* mapn = script_getstr(st,2);
  7284. int x0 = script_getnum(st,3);
  7285. int y0 = script_getnum(st,4);
  7286. int x1 = script_getnum(st,5);
  7287. int y1 = script_getnum(st,6);
  7288. const char* str = script_getstr(st,7);
  7289. int class_ = script_getnum(st,8);
  7290. int amount = script_getnum(st,9);
  7291. const char* event = "";
  7292. struct map_session_data* sd;
  7293. int m;
  7294. if( script_hasdata(st,10) )
  7295. {
  7296. event = script_getstr(st,10);
  7297. check_event(st, event);
  7298. }
  7299. sd = map_id2sd(st->rid);
  7300. if( sd && strcmp(mapn,"this") == 0 )
  7301. m = sd->bl.m;
  7302. else
  7303. {
  7304. m = map_mapname2mapid(mapn);
  7305. if( map[m].flag.src4instance && st->instance_id )
  7306. { // Try to redirect to the instance map, not the src map
  7307. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7308. {
  7309. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7310. return 1;
  7311. }
  7312. }
  7313. }
  7314. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  7315. return 0;
  7316. }
  7317. /*==========================================
  7318. * モンスター削除
  7319. *------------------------------------------*/
  7320. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7321. { //same fix but with killmonster instead - stripping events from mobs.
  7322. TBL_MOB* md = (TBL_MOB*)bl;
  7323. char *event=va_arg(ap,char *);
  7324. int allflag=va_arg(ap,int);
  7325. md->state.npc_killmonster = 1;
  7326. if(!allflag){
  7327. if(strcmp(event,md->npc_event)==0)
  7328. status_kill(bl);
  7329. }else{
  7330. if(!md->spawn)
  7331. status_kill(bl);
  7332. }
  7333. md->state.npc_killmonster = 0;
  7334. return 0;
  7335. }
  7336. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7337. {
  7338. TBL_MOB* md = (TBL_MOB*)bl;
  7339. char *event=va_arg(ap,char *);
  7340. int allflag=va_arg(ap,int);
  7341. if(!allflag){
  7342. if(strcmp(event,md->npc_event)==0)
  7343. status_kill(bl);
  7344. }else{
  7345. if(!md->spawn)
  7346. status_kill(bl);
  7347. }
  7348. return 0;
  7349. }
  7350. BUILDIN_FUNC(killmonster)
  7351. {
  7352. const char *mapname,*event;
  7353. int m,allflag=0;
  7354. mapname=script_getstr(st,2);
  7355. event=script_getstr(st,3);
  7356. if(strcmp(event,"All")==0)
  7357. allflag = 1;
  7358. else
  7359. check_event(st, event);
  7360. if( (m=map_mapname2mapid(mapname))<0 )
  7361. return 0;
  7362. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7363. return 0;
  7364. if( script_hasdata(st,4) ) {
  7365. if ( script_getnum(st,4) == 1 ) {
  7366. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7367. return 0;
  7368. }
  7369. }
  7370. map_freeblock_lock();
  7371. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7372. map_freeblock_unlock();
  7373. return 0;
  7374. }
  7375. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7376. { //Strips the event from the mob if it's killed the old method.
  7377. struct mob_data *md;
  7378. md = BL_CAST(BL_MOB, bl);
  7379. if (md->npc_event[0])
  7380. md->npc_event[0] = 0;
  7381. status_kill(bl);
  7382. return 0;
  7383. }
  7384. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7385. {
  7386. status_kill(bl);
  7387. return 0;
  7388. }
  7389. BUILDIN_FUNC(killmonsterall)
  7390. {
  7391. const char *mapname;
  7392. int m;
  7393. mapname=script_getstr(st,2);
  7394. if( (m = map_mapname2mapid(mapname))<0 )
  7395. return 0;
  7396. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7397. return 0;
  7398. if( script_hasdata(st,3) ) {
  7399. if ( script_getnum(st,3) == 1 ) {
  7400. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7401. return 0;
  7402. }
  7403. }
  7404. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7405. return 0;
  7406. }
  7407. /*==========================================
  7408. * Creates a clone of a player.
  7409. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7410. *------------------------------------------*/
  7411. BUILDIN_FUNC(clone)
  7412. {
  7413. TBL_PC *sd, *msd=NULL;
  7414. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7415. unsigned int duration = 0;
  7416. const char *map,*event="";
  7417. map=script_getstr(st,2);
  7418. x=script_getnum(st,3);
  7419. y=script_getnum(st,4);
  7420. event=script_getstr(st,5);
  7421. char_id=script_getnum(st,6);
  7422. if( script_hasdata(st,7) )
  7423. master_id=script_getnum(st,7);
  7424. if( script_hasdata(st,8) )
  7425. mode=script_getnum(st,8);
  7426. if( script_hasdata(st,9) )
  7427. flag=script_getnum(st,9);
  7428. if( script_hasdata(st,10) )
  7429. duration=script_getnum(st,10);
  7430. check_event(st, event);
  7431. m = map_mapname2mapid(map);
  7432. if (m < 0) return 0;
  7433. sd = map_charid2sd(char_id);
  7434. if (master_id) {
  7435. msd = map_charid2sd(master_id);
  7436. if (msd)
  7437. master_id = msd->bl.id;
  7438. else
  7439. master_id = 0;
  7440. }
  7441. if (sd) //Return ID of newly crafted clone.
  7442. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7443. else //Failed to create clone.
  7444. script_pushint(st,0);
  7445. return 0;
  7446. }
  7447. /*==========================================
  7448. * イベント実行
  7449. *------------------------------------------*/
  7450. BUILDIN_FUNC(doevent)
  7451. {
  7452. const char* event = script_getstr(st,2);
  7453. struct map_session_data* sd;
  7454. if( ( sd = script_rid2sd(st) ) == NULL )
  7455. {
  7456. return 0;
  7457. }
  7458. check_event(st, event);
  7459. npc_event(sd, event, 0);
  7460. return 0;
  7461. }
  7462. /*==========================================
  7463. * NPC主体イベント実行
  7464. *------------------------------------------*/
  7465. BUILDIN_FUNC(donpcevent)
  7466. {
  7467. const char* event = script_getstr(st,2);
  7468. check_event(st, event);
  7469. npc_event_do(event);
  7470. return 0;
  7471. }
  7472. /// for Aegis compatibility
  7473. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7474. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7475. {
  7476. const char* npc = script_getstr(st,2);
  7477. const char* command = script_getstr(st,3);
  7478. char event[EVENT_NAME_LENGTH];
  7479. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  7480. check_event(st, event);
  7481. npc_event_do(event);
  7482. return 0;
  7483. }
  7484. /*==========================================
  7485. * イベントタイマー追加
  7486. *------------------------------------------*/
  7487. BUILDIN_FUNC(addtimer)
  7488. {
  7489. int tick = script_getnum(st,2);
  7490. const char* event = script_getstr(st, 3);
  7491. TBL_PC* sd;
  7492. check_event(st, event);
  7493. sd = script_rid2sd(st);
  7494. if( sd == NULL )
  7495. return 0;
  7496. pc_addeventtimer(sd,tick,event);
  7497. return 0;
  7498. }
  7499. /*==========================================
  7500. * イベントタイマー削除
  7501. *------------------------------------------*/
  7502. BUILDIN_FUNC(deltimer)
  7503. {
  7504. const char *event;
  7505. TBL_PC* sd;
  7506. event=script_getstr(st, 2);
  7507. sd = script_rid2sd(st);
  7508. if( sd == NULL )
  7509. return 0;
  7510. check_event(st, event);
  7511. pc_deleventtimer(sd,event);
  7512. return 0;
  7513. }
  7514. /*==========================================
  7515. * イベントタイマーのカウント値追加
  7516. *------------------------------------------*/
  7517. BUILDIN_FUNC(addtimercount)
  7518. {
  7519. const char *event;
  7520. int tick;
  7521. TBL_PC* sd;
  7522. event=script_getstr(st, 2);
  7523. tick=script_getnum(st,3);
  7524. sd = script_rid2sd(st);
  7525. if( sd == NULL )
  7526. return 0;
  7527. check_event(st, event);
  7528. pc_addeventtimercount(sd,event,tick);
  7529. return 0;
  7530. }
  7531. /*==========================================
  7532. * NPCタイマー初期化
  7533. *------------------------------------------*/
  7534. BUILDIN_FUNC(initnpctimer)
  7535. {
  7536. struct npc_data *nd;
  7537. int flag = 0;
  7538. if( script_hasdata(st,3) )
  7539. { //Two arguments: NPC name and attach flag.
  7540. nd = npc_name2id(script_getstr(st, 2));
  7541. flag = script_getnum(st,3);
  7542. }
  7543. else if( script_hasdata(st,2) )
  7544. { //Check if argument is numeric (flag) or string (npc name)
  7545. struct script_data *data;
  7546. data = script_getdata(st,2);
  7547. get_val(st,data);
  7548. if( data_isstring(data) ) //NPC name
  7549. nd = npc_name2id(conv_str(st, data));
  7550. else if( data_isint(data) ) //Flag
  7551. {
  7552. nd = (struct npc_data *)map_id2bl(st->oid);
  7553. flag = conv_num(st,data);
  7554. }
  7555. else
  7556. {
  7557. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7558. return 1;
  7559. }
  7560. }
  7561. else
  7562. nd = (struct npc_data *)map_id2bl(st->oid);
  7563. if( !nd )
  7564. return 0;
  7565. if( flag ) //Attach
  7566. {
  7567. TBL_PC* sd = script_rid2sd(st);
  7568. if( sd == NULL )
  7569. return 0;
  7570. nd->u.scr.rid = sd->bl.id;
  7571. }
  7572. npc_settimerevent_tick(nd,0);
  7573. npc_timerevent_start(nd, st->rid);
  7574. return 0;
  7575. }
  7576. /*==========================================
  7577. * NPCタイマー開始
  7578. *------------------------------------------*/
  7579. BUILDIN_FUNC(startnpctimer)
  7580. {
  7581. struct npc_data *nd;
  7582. int flag = 0;
  7583. if( script_hasdata(st,3) )
  7584. { //Two arguments: NPC name and attach flag.
  7585. nd = npc_name2id(script_getstr(st, 2));
  7586. flag = script_getnum(st,3);
  7587. }
  7588. else if( script_hasdata(st,2) )
  7589. { //Check if argument is numeric (flag) or string (npc name)
  7590. struct script_data *data;
  7591. data = script_getdata(st,2);
  7592. get_val(st,data);
  7593. if( data_isstring(data) ) //NPC name
  7594. nd = npc_name2id(conv_str(st, data));
  7595. else if( data_isint(data) ) //Flag
  7596. {
  7597. nd = (struct npc_data *)map_id2bl(st->oid);
  7598. flag = conv_num(st,data);
  7599. }
  7600. else
  7601. {
  7602. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7603. return 1;
  7604. }
  7605. }
  7606. else
  7607. nd=(struct npc_data *)map_id2bl(st->oid);
  7608. if( !nd )
  7609. return 0;
  7610. if( flag ) //Attach
  7611. {
  7612. TBL_PC* sd = script_rid2sd(st);
  7613. if( sd == NULL )
  7614. return 0;
  7615. nd->u.scr.rid = sd->bl.id;
  7616. }
  7617. npc_timerevent_start(nd, st->rid);
  7618. return 0;
  7619. }
  7620. /*==========================================
  7621. * NPCタイマー停止
  7622. *------------------------------------------*/
  7623. BUILDIN_FUNC(stopnpctimer)
  7624. {
  7625. struct npc_data *nd;
  7626. int flag = 0;
  7627. if( script_hasdata(st,3) )
  7628. { //Two arguments: NPC name and attach flag.
  7629. nd = npc_name2id(script_getstr(st, 2));
  7630. flag = script_getnum(st,3);
  7631. }
  7632. else if( script_hasdata(st,2) )
  7633. { //Check if argument is numeric (flag) or string (npc name)
  7634. struct script_data *data;
  7635. data = script_getdata(st,2);
  7636. get_val(st,data);
  7637. if( data_isstring(data) ) //NPC name
  7638. nd = npc_name2id(conv_str(st, data));
  7639. else if( data_isint(data) ) //Flag
  7640. {
  7641. nd = (struct npc_data *)map_id2bl(st->oid);
  7642. flag = conv_num(st,data);
  7643. }
  7644. else
  7645. {
  7646. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7647. return 1;
  7648. }
  7649. }
  7650. else
  7651. nd=(struct npc_data *)map_id2bl(st->oid);
  7652. if( !nd )
  7653. return 0;
  7654. if( flag ) //Detach
  7655. nd->u.scr.rid = 0;
  7656. npc_timerevent_stop(nd);
  7657. return 0;
  7658. }
  7659. /*==========================================
  7660. * NPCタイマー情報所得
  7661. *------------------------------------------*/
  7662. BUILDIN_FUNC(getnpctimer)
  7663. {
  7664. struct npc_data *nd;
  7665. TBL_PC *sd;
  7666. int type = script_getnum(st,2);
  7667. int val = 0;
  7668. if( script_hasdata(st,3) )
  7669. nd = npc_name2id(script_getstr(st,3));
  7670. else
  7671. nd = (struct npc_data *)map_id2bl(st->oid);
  7672. if( !nd || nd->bl.type != BL_NPC )
  7673. {
  7674. script_pushint(st,0);
  7675. ShowError("getnpctimer: Invalid NPC.\n");
  7676. return 1;
  7677. }
  7678. switch( type )
  7679. {
  7680. case 0: val = npc_gettimerevent_tick(nd); break;
  7681. case 1:
  7682. if( nd->u.scr.rid )
  7683. {
  7684. sd = map_id2sd(nd->u.scr.rid);
  7685. if( !sd )
  7686. {
  7687. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7688. break;
  7689. }
  7690. val = (sd->npc_timer_id != INVALID_TIMER);
  7691. }
  7692. else
  7693. val = (nd->u.scr.timerid != INVALID_TIMER);
  7694. break;
  7695. case 2: val = nd->u.scr.timeramount; break;
  7696. }
  7697. script_pushint(st,val);
  7698. return 0;
  7699. }
  7700. /*==========================================
  7701. * NPCタイマー値設定
  7702. *------------------------------------------*/
  7703. BUILDIN_FUNC(setnpctimer)
  7704. {
  7705. int tick;
  7706. struct npc_data *nd;
  7707. tick = script_getnum(st,2);
  7708. if( script_hasdata(st,3) )
  7709. nd = npc_name2id(script_getstr(st,3));
  7710. else
  7711. nd = (struct npc_data *)map_id2bl(st->oid);
  7712. if( !nd || nd->bl.type != BL_NPC )
  7713. {
  7714. script_pushint(st,1);
  7715. ShowError("setnpctimer: Invalid NPC.\n");
  7716. return 1;
  7717. }
  7718. npc_settimerevent_tick(nd,tick);
  7719. script_pushint(st,0);
  7720. return 0;
  7721. }
  7722. /*==========================================
  7723. * attaches the player rid to the timer [Celest]
  7724. *------------------------------------------*/
  7725. BUILDIN_FUNC(attachnpctimer)
  7726. {
  7727. TBL_PC *sd;
  7728. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7729. if( !nd || nd->bl.type != BL_NPC )
  7730. {
  7731. script_pushint(st,1);
  7732. ShowError("setnpctimer: Invalid NPC.\n");
  7733. return 1;
  7734. }
  7735. if( script_hasdata(st,2) )
  7736. sd = map_nick2sd(script_getstr(st,2));
  7737. else
  7738. sd = script_rid2sd(st);
  7739. if( !sd )
  7740. {
  7741. script_pushint(st,1);
  7742. ShowWarning("attachnpctimer: Invalid player.\n");
  7743. return 1;
  7744. }
  7745. nd->u.scr.rid = sd->bl.id;
  7746. script_pushint(st,0);
  7747. return 0;
  7748. }
  7749. /*==========================================
  7750. * detaches a player rid from the timer [Celest]
  7751. *------------------------------------------*/
  7752. BUILDIN_FUNC(detachnpctimer)
  7753. {
  7754. struct npc_data *nd;
  7755. if( script_hasdata(st,2) )
  7756. nd = npc_name2id(script_getstr(st,2));
  7757. else
  7758. nd = (struct npc_data *)map_id2bl(st->oid);
  7759. if( !nd || nd->bl.type != BL_NPC )
  7760. {
  7761. script_pushint(st,1);
  7762. ShowError("detachnpctimer: Invalid NPC.\n");
  7763. return 1;
  7764. }
  7765. nd->u.scr.rid = 0;
  7766. script_pushint(st,0);
  7767. return 0;
  7768. }
  7769. /*==========================================
  7770. * To avoid "player not attached" script errors, this function is provided,
  7771. * it checks if there is a player attached to the current script. [Skotlex]
  7772. * If no, returns 0, if yes, returns the account_id of the attached player.
  7773. *------------------------------------------*/
  7774. BUILDIN_FUNC(playerattached)
  7775. {
  7776. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7777. script_pushint(st,0);
  7778. else
  7779. script_pushint(st,st->rid);
  7780. return 0;
  7781. }
  7782. /*==========================================
  7783. * 天の声アナウンス
  7784. *------------------------------------------*/
  7785. BUILDIN_FUNC(announce)
  7786. {
  7787. const char *mes = script_getstr(st,2);
  7788. int flag = script_getnum(st,3);
  7789. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7790. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7791. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7792. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7793. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7794. if (flag&0x0f) // Broadcast source or broadcast region defined
  7795. {
  7796. send_target target;
  7797. 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
  7798. if (bl == NULL)
  7799. return 0;
  7800. flag &= 0x07;
  7801. target = (flag == 1) ? ALL_SAMEMAP :
  7802. (flag == 2) ? AREA :
  7803. (flag == 3) ? SELF :
  7804. ALL_CLIENT;
  7805. if (fontColor)
  7806. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7807. else
  7808. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7809. }
  7810. else
  7811. {
  7812. if (fontColor)
  7813. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7814. else
  7815. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7816. }
  7817. return 0;
  7818. }
  7819. /*==========================================
  7820. * 天の声アナウンス(特定マップ)
  7821. *------------------------------------------*/
  7822. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7823. {
  7824. char *mes = va_arg(ap, char *);
  7825. int len = va_arg(ap, int);
  7826. int type = va_arg(ap, int);
  7827. char *fontColor = va_arg(ap, char *);
  7828. short fontType = (short)va_arg(ap, int);
  7829. short fontSize = (short)va_arg(ap, int);
  7830. short fontAlign = (short)va_arg(ap, int);
  7831. short fontY = (short)va_arg(ap, int);
  7832. if (fontColor)
  7833. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7834. else
  7835. clif_broadcast(bl, mes, len, type, SELF);
  7836. return 0;
  7837. }
  7838. BUILDIN_FUNC(mapannounce)
  7839. {
  7840. const char *mapname = script_getstr(st,2);
  7841. const char *mes = script_getstr(st,3);
  7842. int flag = script_getnum(st,4);
  7843. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7844. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7845. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7846. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7847. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7848. int m;
  7849. if ((m = map_mapname2mapid(mapname)) < 0)
  7850. return 0;
  7851. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7852. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7853. return 0;
  7854. }
  7855. /*==========================================
  7856. * 天の声アナウンス(特定エリア)
  7857. *------------------------------------------*/
  7858. BUILDIN_FUNC(areaannounce)
  7859. {
  7860. const char *mapname = script_getstr(st,2);
  7861. int x0 = script_getnum(st,3);
  7862. int y0 = script_getnum(st,4);
  7863. int x1 = script_getnum(st,5);
  7864. int y1 = script_getnum(st,6);
  7865. const char *mes = script_getstr(st,7);
  7866. int flag = script_getnum(st,8);
  7867. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7868. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7869. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7870. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7871. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7872. int m;
  7873. if ((m = map_mapname2mapid(mapname)) < 0)
  7874. return 0;
  7875. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7876. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7877. return 0;
  7878. }
  7879. /*==========================================
  7880. * ユーザー数所得
  7881. *------------------------------------------*/
  7882. BUILDIN_FUNC(getusers)
  7883. {
  7884. int flag, val = 0;
  7885. struct map_session_data* sd;
  7886. struct block_list* bl = NULL;
  7887. flag = script_getnum(st,2);
  7888. switch(flag&0x07)
  7889. {
  7890. case 0:
  7891. if(flag&0x8)
  7892. {// npc
  7893. bl = map_id2bl(st->oid);
  7894. }
  7895. else if((sd = script_rid2sd(st))!=NULL)
  7896. {// pc
  7897. bl = &sd->bl;
  7898. }
  7899. if(bl)
  7900. {
  7901. val = map[bl->m].users;
  7902. }
  7903. break;
  7904. case 1:
  7905. val = map_getusers();
  7906. break;
  7907. default:
  7908. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  7909. script_pushint(st,0);
  7910. return 1;
  7911. }
  7912. script_pushint(st,val);
  7913. return 0;
  7914. }
  7915. /*==========================================
  7916. * Works like @WHO - displays all online users names in window
  7917. *------------------------------------------*/
  7918. BUILDIN_FUNC(getusersname)
  7919. {
  7920. TBL_PC *sd, *pl_sd;
  7921. int disp_num=1, group_level = 0;
  7922. struct s_mapiterator* iter;
  7923. sd = script_rid2sd(st);
  7924. if (!sd) return 0;
  7925. group_level = pc_get_group_level(sd);
  7926. iter = mapit_getallusers();
  7927. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7928. {
  7929. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  7930. continue; // skip hidden sessions
  7931. if((disp_num++)%10==0)
  7932. clif_scriptnext(sd,st->oid);
  7933. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7934. }
  7935. mapit_free(iter);
  7936. return 0;
  7937. }
  7938. /*==========================================
  7939. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7940. *------------------------------------------*/
  7941. BUILDIN_FUNC(getmapguildusers)
  7942. {
  7943. const char *str;
  7944. int m, gid;
  7945. int i=0,c=0;
  7946. struct guild *g = NULL;
  7947. str=script_getstr(st,2);
  7948. gid=script_getnum(st,3);
  7949. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7950. script_pushint(st,-1);
  7951. return 0;
  7952. }
  7953. g = guild_search(gid);
  7954. if (g){
  7955. for(i = 0; i < g->max_member; i++)
  7956. {
  7957. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7958. c++;
  7959. }
  7960. }
  7961. script_pushint(st,c);
  7962. return 0;
  7963. }
  7964. /*==========================================
  7965. * マップ指定ユーザー数所得
  7966. *------------------------------------------*/
  7967. BUILDIN_FUNC(getmapusers)
  7968. {
  7969. const char *str;
  7970. int m;
  7971. str=script_getstr(st,2);
  7972. if( (m=map_mapname2mapid(str))< 0){
  7973. script_pushint(st,-1);
  7974. return 0;
  7975. }
  7976. script_pushint(st,map[m].users);
  7977. return 0;
  7978. }
  7979. /*==========================================
  7980. * エリア指定ユーザー数所得
  7981. *------------------------------------------*/
  7982. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7983. {
  7984. int *users=va_arg(ap,int *);
  7985. (*users)++;
  7986. return 0;
  7987. }
  7988. BUILDIN_FUNC(getareausers)
  7989. {
  7990. const char *str;
  7991. int m,x0,y0,x1,y1,users=0;
  7992. str=script_getstr(st,2);
  7993. x0=script_getnum(st,3);
  7994. y0=script_getnum(st,4);
  7995. x1=script_getnum(st,5);
  7996. y1=script_getnum(st,6);
  7997. if( (m=map_mapname2mapid(str))< 0){
  7998. script_pushint(st,-1);
  7999. return 0;
  8000. }
  8001. map_foreachinarea(buildin_getareausers_sub,
  8002. m,x0,y0,x1,y1,BL_PC,&users);
  8003. script_pushint(st,users);
  8004. return 0;
  8005. }
  8006. /*==========================================
  8007. * エリア指定ドロップアイテム数所得
  8008. *------------------------------------------*/
  8009. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  8010. {
  8011. int item=va_arg(ap,int);
  8012. int *amount=va_arg(ap,int *);
  8013. struct flooritem_data *drop=(struct flooritem_data *)bl;
  8014. if(drop->item_data.nameid==item)
  8015. (*amount)+=drop->item_data.amount;
  8016. return 0;
  8017. }
  8018. BUILDIN_FUNC(getareadropitem)
  8019. {
  8020. const char *str;
  8021. int m,x0,y0,x1,y1,item,amount=0;
  8022. struct script_data *data;
  8023. str=script_getstr(st,2);
  8024. x0=script_getnum(st,3);
  8025. y0=script_getnum(st,4);
  8026. x1=script_getnum(st,5);
  8027. y1=script_getnum(st,6);
  8028. data=script_getdata(st,7);
  8029. get_val(st,data);
  8030. if( data_isstring(data) ){
  8031. const char *name=conv_str(st,data);
  8032. struct item_data *item_data = itemdb_searchname(name);
  8033. item=UNKNOWN_ITEM_ID;
  8034. if( item_data )
  8035. item=item_data->nameid;
  8036. }else
  8037. item=conv_num(st,data);
  8038. if( (m=map_mapname2mapid(str))< 0){
  8039. script_pushint(st,-1);
  8040. return 0;
  8041. }
  8042. map_foreachinarea(buildin_getareadropitem_sub,
  8043. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  8044. script_pushint(st,amount);
  8045. return 0;
  8046. }
  8047. /*==========================================
  8048. * NPCの有効化
  8049. *------------------------------------------*/
  8050. BUILDIN_FUNC(enablenpc)
  8051. {
  8052. const char *str;
  8053. str=script_getstr(st,2);
  8054. npc_enable(str,1);
  8055. return 0;
  8056. }
  8057. /*==========================================
  8058. * NPCの無効化
  8059. *------------------------------------------*/
  8060. BUILDIN_FUNC(disablenpc)
  8061. {
  8062. const char *str;
  8063. str=script_getstr(st,2);
  8064. npc_enable(str,0);
  8065. return 0;
  8066. }
  8067. /*==========================================
  8068. * 隠れているNPCの表示
  8069. *------------------------------------------*/
  8070. BUILDIN_FUNC(hideoffnpc)
  8071. {
  8072. const char *str;
  8073. str=script_getstr(st,2);
  8074. npc_enable(str,2);
  8075. return 0;
  8076. }
  8077. /*==========================================
  8078. * NPCをハイディング
  8079. *------------------------------------------*/
  8080. BUILDIN_FUNC(hideonnpc)
  8081. {
  8082. const char *str;
  8083. str=script_getstr(st,2);
  8084. npc_enable(str,4);
  8085. return 0;
  8086. }
  8087. /// Starts a status effect on the target unit or on the attached player.
  8088. ///
  8089. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  8090. BUILDIN_FUNC(sc_start)
  8091. {
  8092. struct block_list* bl;
  8093. enum sc_type type;
  8094. int tick;
  8095. int val1;
  8096. int val4 = 0;
  8097. type = (sc_type)script_getnum(st,2);
  8098. tick = script_getnum(st,3);
  8099. val1 = script_getnum(st,4);
  8100. if( script_hasdata(st,5) )
  8101. bl = map_id2bl(script_getnum(st,5));
  8102. else
  8103. bl = map_id2bl(st->rid);
  8104. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8105. {// When there isn't a duration specified, try to get it from the skill_db
  8106. tick = skill_get_time(status_sc2skill(type), val1);
  8107. }
  8108. if( potion_flag == 1 && potion_target )
  8109. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8110. bl = map_id2bl(potion_target);
  8111. tick /= 2;// Thrown potions only last half.
  8112. val4 = 1;// Mark that this was a thrown sc_effect
  8113. }
  8114. if( bl )
  8115. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  8116. return 0;
  8117. }
  8118. /// Starts a status effect on the target unit or on the attached player.
  8119. ///
  8120. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  8121. BUILDIN_FUNC(sc_start2)
  8122. {
  8123. struct block_list* bl;
  8124. enum sc_type type;
  8125. int tick;
  8126. int val1;
  8127. int val4 = 0;
  8128. int rate;
  8129. type = (sc_type)script_getnum(st,2);
  8130. tick = script_getnum(st,3);
  8131. val1 = script_getnum(st,4);
  8132. rate = script_getnum(st,5);
  8133. if( script_hasdata(st,6) )
  8134. bl = map_id2bl(script_getnum(st,6));
  8135. else
  8136. bl = map_id2bl(st->rid);
  8137. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8138. {// When there isn't a duration specified, try to get it from the skill_db
  8139. tick = skill_get_time(status_sc2skill(type), val1);
  8140. }
  8141. if( potion_flag == 1 && potion_target )
  8142. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8143. bl = map_id2bl(potion_target);
  8144. tick /= 2;// Thrown potions only last half.
  8145. val4 = 1;// Mark that this was a thrown sc_effect
  8146. }
  8147. if( bl )
  8148. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  8149. return 0;
  8150. }
  8151. /// Starts a status effect on the target unit or on the attached player.
  8152. ///
  8153. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  8154. BUILDIN_FUNC(sc_start4)
  8155. {
  8156. struct block_list* bl;
  8157. enum sc_type type;
  8158. int tick;
  8159. int val1;
  8160. int val2;
  8161. int val3;
  8162. int val4;
  8163. type = (sc_type)script_getnum(st,2);
  8164. tick = script_getnum(st,3);
  8165. val1 = script_getnum(st,4);
  8166. val2 = script_getnum(st,5);
  8167. val3 = script_getnum(st,6);
  8168. val4 = script_getnum(st,7);
  8169. if( script_hasdata(st,8) )
  8170. bl = map_id2bl(script_getnum(st,8));
  8171. else
  8172. bl = map_id2bl(st->rid);
  8173. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8174. {// When there isn't a duration specified, try to get it from the skill_db
  8175. tick = skill_get_time(status_sc2skill(type), val1);
  8176. }
  8177. if( potion_flag == 1 && potion_target )
  8178. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8179. bl = map_id2bl(potion_target);
  8180. tick /= 2;// Thrown potions only last half.
  8181. }
  8182. if( bl )
  8183. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  8184. return 0;
  8185. }
  8186. /// Ends one or all status effects on the target unit or on the attached player.
  8187. ///
  8188. /// sc_end <effect_id>{,<unit_id>};
  8189. BUILDIN_FUNC(sc_end)
  8190. {
  8191. struct block_list* bl;
  8192. int type;
  8193. type = script_getnum(st,2);
  8194. if( script_hasdata(st,3) )
  8195. bl = map_id2bl(script_getnum(st,3));
  8196. else
  8197. bl = map_id2bl(st->rid);
  8198. if( potion_flag==1 && potion_target )
  8199. {//##TODO how does this work [FlavioJS]
  8200. bl = map_id2bl(potion_target);
  8201. }
  8202. if( !bl ) return 0;
  8203. if( type >= 0 && type < SC_MAX )
  8204. {
  8205. struct status_change *sc = status_get_sc(bl);
  8206. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  8207. if (!sce) return 0;
  8208. //This should help status_change_end force disabling the SC in case it has no limit.
  8209. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8210. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8211. } else
  8212. status_change_clear(bl, 2);// remove all effects
  8213. return 0;
  8214. }
  8215. /*==========================================
  8216. * 状態異常耐性を計算した確率を返す
  8217. *------------------------------------------*/
  8218. BUILDIN_FUNC(getscrate)
  8219. {
  8220. struct block_list *bl;
  8221. int type,rate;
  8222. type=script_getnum(st,2);
  8223. rate=script_getnum(st,3);
  8224. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  8225. bl = map_id2bl(script_getnum(st,4));
  8226. else
  8227. bl = map_id2bl(st->rid);
  8228. if (bl)
  8229. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  8230. script_pushint(st,rate);
  8231. return 0;
  8232. }
  8233. /*==========================================
  8234. * getstatus <type>{, <info>};
  8235. *------------------------------------------*/
  8236. BUILDIN_FUNC(getstatus)
  8237. {
  8238. int id, type;
  8239. struct map_session_data* sd = script_rid2sd(st);
  8240. if( sd == NULL )
  8241. {// no player attached
  8242. return 0;
  8243. }
  8244. id = script_getnum(st, 2);
  8245. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8246. if( id <= SC_NONE || id >= SC_MAX )
  8247. {// invalid status type given
  8248. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8249. return 0;
  8250. }
  8251. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8252. {// no status is active
  8253. script_pushint(st, 0);
  8254. return 0;
  8255. }
  8256. switch( type )
  8257. {
  8258. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8259. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8260. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8261. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8262. case 5:
  8263. {
  8264. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8265. if( timer )
  8266. {// return the amount of time remaining
  8267. script_pushint(st, timer->tick - gettick());
  8268. }
  8269. }
  8270. break;
  8271. default: script_pushint(st, 1); break;
  8272. }
  8273. return 0;
  8274. }
  8275. /*==========================================
  8276. *
  8277. *------------------------------------------*/
  8278. BUILDIN_FUNC(debugmes)
  8279. {
  8280. const char *str;
  8281. str=script_getstr(st,2);
  8282. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8283. return 0;
  8284. }
  8285. /*==========================================
  8286. *捕獲アイテム使用
  8287. *------------------------------------------*/
  8288. BUILDIN_FUNC(catchpet)
  8289. {
  8290. int pet_id;
  8291. TBL_PC *sd;
  8292. pet_id= script_getnum(st,2);
  8293. sd=script_rid2sd(st);
  8294. if( sd == NULL )
  8295. return 0;
  8296. pet_catch_process1(sd,pet_id);
  8297. return 0;
  8298. }
  8299. /*==========================================
  8300. * [orn]
  8301. *------------------------------------------*/
  8302. BUILDIN_FUNC(homunculus_evolution)
  8303. {
  8304. TBL_PC *sd;
  8305. sd=script_rid2sd(st);
  8306. if( sd == NULL )
  8307. return 0;
  8308. if(merc_is_hom_active(sd->hd))
  8309. {
  8310. if (sd->hd->homunculus.intimacy > 91000)
  8311. merc_hom_evolution(sd->hd);
  8312. else
  8313. clif_emotion(&sd->hd->bl, E_SWT);
  8314. }
  8315. return 0;
  8316. }
  8317. // [Zephyrus]
  8318. BUILDIN_FUNC(homunculus_shuffle)
  8319. {
  8320. TBL_PC *sd;
  8321. sd=script_rid2sd(st);
  8322. if( sd == NULL )
  8323. return 0;
  8324. if(merc_is_hom_active(sd->hd))
  8325. merc_hom_shuffle(sd->hd);
  8326. return 0;
  8327. }
  8328. //These two functions bring the eA MAPID_* class functionality to scripts.
  8329. BUILDIN_FUNC(eaclass)
  8330. {
  8331. int class_;
  8332. if( script_hasdata(st,2) )
  8333. class_ = script_getnum(st,2);
  8334. else {
  8335. TBL_PC *sd;
  8336. sd=script_rid2sd(st);
  8337. if (!sd) {
  8338. script_pushint(st,-1);
  8339. return 0;
  8340. }
  8341. class_ = sd->status.class_;
  8342. }
  8343. script_pushint(st,pc_jobid2mapid(class_));
  8344. return 0;
  8345. }
  8346. BUILDIN_FUNC(roclass)
  8347. {
  8348. int class_ =script_getnum(st,2);
  8349. int sex;
  8350. if( script_hasdata(st,3) )
  8351. sex = script_getnum(st,3);
  8352. else {
  8353. TBL_PC *sd;
  8354. if (st->rid && (sd=script_rid2sd(st)))
  8355. sex = sd->status.sex;
  8356. else
  8357. sex = 1; //Just use male when not found.
  8358. }
  8359. script_pushint(st,pc_mapid2jobid(class_, sex));
  8360. return 0;
  8361. }
  8362. /*==========================================
  8363. * Tells client to open a hatching window, used for pet incubator
  8364. *------------------------------------------*/
  8365. BUILDIN_FUNC(birthpet)
  8366. {
  8367. TBL_PC *sd;
  8368. sd=script_rid2sd(st);
  8369. if( sd == NULL )
  8370. return 0;
  8371. if( sd->status.pet_id )
  8372. {// do not send egg list, when you already have a pet
  8373. return 0;
  8374. }
  8375. clif_sendegg(sd);
  8376. return 0;
  8377. }
  8378. /*==========================================
  8379. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8380. *------------------------------------------*/
  8381. BUILDIN_FUNC(resetlvl)
  8382. {
  8383. TBL_PC *sd;
  8384. int type=script_getnum(st,2);
  8385. sd=script_rid2sd(st);
  8386. if( sd == NULL )
  8387. return 0;
  8388. pc_resetlvl(sd,type);
  8389. return 0;
  8390. }
  8391. /*==========================================
  8392. * ステータスリセット
  8393. *------------------------------------------*/
  8394. BUILDIN_FUNC(resetstatus)
  8395. {
  8396. TBL_PC *sd;
  8397. sd=script_rid2sd(st);
  8398. pc_resetstate(sd);
  8399. return 0;
  8400. }
  8401. /*==========================================
  8402. * script command resetskill
  8403. *------------------------------------------*/
  8404. BUILDIN_FUNC(resetskill)
  8405. {
  8406. TBL_PC *sd;
  8407. sd=script_rid2sd(st);
  8408. pc_resetskill(sd,1);
  8409. return 0;
  8410. }
  8411. /*==========================================
  8412. * Counts total amount of skill points.
  8413. *------------------------------------------*/
  8414. BUILDIN_FUNC(skillpointcount)
  8415. {
  8416. TBL_PC *sd;
  8417. sd=script_rid2sd(st);
  8418. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8419. return 0;
  8420. }
  8421. /*==========================================
  8422. *
  8423. *------------------------------------------*/
  8424. BUILDIN_FUNC(changebase)
  8425. {
  8426. TBL_PC *sd=NULL;
  8427. int vclass;
  8428. if( script_hasdata(st,3) )
  8429. sd=map_id2sd(script_getnum(st,3));
  8430. else
  8431. sd=script_rid2sd(st);
  8432. if(sd == NULL)
  8433. return 0;
  8434. vclass = script_getnum(st,2);
  8435. if(vclass == JOB_WEDDING)
  8436. {
  8437. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8438. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8439. )
  8440. return 0;
  8441. }
  8442. if(!sd->disguise && vclass != sd->vd.class_) {
  8443. status_set_viewdata(&sd->bl, vclass);
  8444. //Updated client view. Base, Weapon and Cloth Colors.
  8445. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8446. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8447. if (sd->vd.cloth_color)
  8448. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8449. clif_skillinfoblock(sd);
  8450. }
  8451. return 0;
  8452. }
  8453. /*==========================================
  8454. * 性別変換
  8455. *------------------------------------------*/
  8456. BUILDIN_FUNC(changesex)
  8457. {
  8458. TBL_PC *sd = NULL;
  8459. sd = script_rid2sd(st);
  8460. pc_resetskill(sd,4);
  8461. chrif_changesex(sd);
  8462. return 0;
  8463. }
  8464. /*==========================================
  8465. * Works like 'announce' but outputs in the common chat window
  8466. *------------------------------------------*/
  8467. BUILDIN_FUNC(globalmes)
  8468. {
  8469. struct block_list *bl = map_id2bl(st->oid);
  8470. struct npc_data *nd = (struct npc_data *)bl;
  8471. const char *name=NULL,*mes;
  8472. mes=script_getstr(st,2); // メッセージの取得
  8473. if(mes==NULL) return 0;
  8474. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8475. name=script_getstr(st,3);
  8476. } else {
  8477. name=nd->name;
  8478. }
  8479. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8480. return 0;
  8481. }
  8482. /////////////////////////////////////////////////////////////////////
  8483. // NPC waiting room (chat room)
  8484. //
  8485. /// Creates a waiting room (chat room) for this npc.
  8486. ///
  8487. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  8488. BUILDIN_FUNC(waitingroom)
  8489. {
  8490. struct npc_data* nd;
  8491. int pub = 1;
  8492. const char* title = script_getstr(st, 2);
  8493. int limit = script_getnum(st, 3);
  8494. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  8495. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  8496. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  8497. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  8498. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  8499. nd = (struct npc_data *)map_id2bl(st->oid);
  8500. if( nd != NULL )
  8501. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  8502. return 0;
  8503. }
  8504. /// Removes the waiting room of the current or target npc.
  8505. ///
  8506. /// delwaitingroom "<npc_name>";
  8507. /// delwaitingroom;
  8508. BUILDIN_FUNC(delwaitingroom)
  8509. {
  8510. struct npc_data* nd;
  8511. if( script_hasdata(st,2) )
  8512. nd = npc_name2id(script_getstr(st, 2));
  8513. else
  8514. nd = (struct npc_data *)map_id2bl(st->oid);
  8515. if( nd != NULL )
  8516. chat_deletenpcchat(nd);
  8517. return 0;
  8518. }
  8519. /// Kicks all the players from the waiting room of the current or target npc.
  8520. ///
  8521. /// kickwaitingroomall "<npc_name>";
  8522. /// kickwaitingroomall;
  8523. BUILDIN_FUNC(waitingroomkickall)
  8524. {
  8525. struct npc_data* nd;
  8526. struct chat_data* cd;
  8527. if( script_hasdata(st,2) )
  8528. nd = npc_name2id(script_getstr(st,2));
  8529. else
  8530. nd = (struct npc_data *)map_id2bl(st->oid);
  8531. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8532. chat_npckickall(cd);
  8533. return 0;
  8534. }
  8535. /// Enables the waiting room event of the current or target npc.
  8536. ///
  8537. /// enablewaitingroomevent "<npc_name>";
  8538. /// enablewaitingroomevent;
  8539. BUILDIN_FUNC(enablewaitingroomevent)
  8540. {
  8541. struct npc_data* nd;
  8542. struct chat_data* cd;
  8543. if( script_hasdata(st,2) )
  8544. nd = npc_name2id(script_getstr(st, 2));
  8545. else
  8546. nd = (struct npc_data *)map_id2bl(st->oid);
  8547. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8548. chat_enableevent(cd);
  8549. return 0;
  8550. }
  8551. /// Disables the waiting room event of the current or target npc.
  8552. ///
  8553. /// disablewaitingroomevent "<npc_name>";
  8554. /// disablewaitingroomevent;
  8555. BUILDIN_FUNC(disablewaitingroomevent)
  8556. {
  8557. struct npc_data *nd;
  8558. struct chat_data *cd;
  8559. if( script_hasdata(st,2) )
  8560. nd = npc_name2id(script_getstr(st, 2));
  8561. else
  8562. nd = (struct npc_data *)map_id2bl(st->oid);
  8563. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8564. chat_disableevent(cd);
  8565. return 0;
  8566. }
  8567. /// Returns info on the waiting room of the current or target npc.
  8568. /// Returns -1 if the type unknown
  8569. /// <type>=0 : current number of users
  8570. /// <type>=1 : maximum number of users allowed
  8571. /// <type>=2 : the number of users that trigger the event
  8572. /// <type>=3 : if the trigger is disabled
  8573. /// <type>=4 : the title of the waiting room
  8574. /// <type>=5 : the password of the waiting room
  8575. /// <type>=16 : the name of the waiting room event
  8576. /// <type>=32 : if the waiting room is full
  8577. /// <type>=33 : if there are enough users to trigger the event
  8578. ///
  8579. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8580. /// getwaitingroomstate(<type>) -> <info>
  8581. BUILDIN_FUNC(getwaitingroomstate)
  8582. {
  8583. struct npc_data *nd;
  8584. struct chat_data *cd;
  8585. int type;
  8586. type = script_getnum(st,2);
  8587. if( script_hasdata(st,3) )
  8588. nd = npc_name2id(script_getstr(st, 3));
  8589. else
  8590. nd = (struct npc_data *)map_id2bl(st->oid);
  8591. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8592. {
  8593. script_pushint(st, -1);
  8594. return 0;
  8595. }
  8596. switch(type)
  8597. {
  8598. case 0: script_pushint(st, cd->users); break;
  8599. case 1: script_pushint(st, cd->limit); break;
  8600. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8601. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8602. case 4: script_pushstrcopy(st, cd->title); break;
  8603. case 5: script_pushstrcopy(st, cd->pass); break;
  8604. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8605. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8606. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8607. default: script_pushint(st, -1); break;
  8608. }
  8609. return 0;
  8610. }
  8611. /// Warps the trigger or target amount of players to the target map and position.
  8612. /// Players are automatically removed from the waiting room.
  8613. /// Those waiting the longest will get warped first.
  8614. /// The target map can be "Random" for a random position in the current map,
  8615. /// and "SavePoint" for the savepoint map+position.
  8616. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8617. ///
  8618. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8619. /// The total number of teleported players is put into $@warpwaitingpcnum
  8620. ///
  8621. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8622. /// warpwaitingpc "<map name>",<x>,<y>;
  8623. BUILDIN_FUNC(warpwaitingpc)
  8624. {
  8625. int x;
  8626. int y;
  8627. int i;
  8628. int n;
  8629. const char* map_name;
  8630. struct npc_data* nd;
  8631. struct chat_data* cd;
  8632. TBL_PC* sd;
  8633. nd = (struct npc_data *)map_id2bl(st->oid);
  8634. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8635. return 0;
  8636. map_name = script_getstr(st,2);
  8637. x = script_getnum(st,3);
  8638. y = script_getnum(st,4);
  8639. n = cd->trigger&0x7f;
  8640. if( script_hasdata(st,5) )
  8641. n = script_getnum(st,5);
  8642. for( i = 0; i < n && cd->users > 0; i++ )
  8643. {
  8644. sd = cd->usersd[0];
  8645. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  8646. {// can't teleport on this map
  8647. break;
  8648. }
  8649. if( cd->zeny )
  8650. {// fee set
  8651. if( (uint32)sd->status.zeny < cd->zeny )
  8652. {// no zeny to cover set fee
  8653. break;
  8654. }
  8655. pc_payzeny(sd, cd->zeny);
  8656. }
  8657. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  8658. if( strcmp(map_name,"Random") == 0 )
  8659. pc_randomwarp(sd,CLR_TELEPORT);
  8660. else if( strcmp(map_name,"SavePoint") == 0 )
  8661. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  8662. else
  8663. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  8664. }
  8665. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8666. return 0;
  8667. }
  8668. /////////////////////////////////////////////////////////////////////
  8669. // ...
  8670. //
  8671. /// Detaches a character from a script.
  8672. ///
  8673. /// @param st Script state to detach the character from.
  8674. static void script_detach_rid(struct script_state* st)
  8675. {
  8676. if(st->rid)
  8677. {
  8678. script_detach_state(st, false);
  8679. st->rid = 0;
  8680. }
  8681. }
  8682. /*==========================================
  8683. * RIDのアタッチ
  8684. *------------------------------------------*/
  8685. BUILDIN_FUNC(attachrid)
  8686. {
  8687. int rid = script_getnum(st,2);
  8688. struct map_session_data* sd;
  8689. if ((sd = map_id2sd(rid))!=NULL) {
  8690. script_detach_rid(st);
  8691. st->rid = rid;
  8692. script_attach_state(st);
  8693. script_pushint(st,1);
  8694. } else
  8695. script_pushint(st,0);
  8696. return 0;
  8697. }
  8698. /*==========================================
  8699. * RIDのデタッチ
  8700. *------------------------------------------*/
  8701. BUILDIN_FUNC(detachrid)
  8702. {
  8703. script_detach_rid(st);
  8704. return 0;
  8705. }
  8706. /*==========================================
  8707. * 存在チェック
  8708. *------------------------------------------*/
  8709. BUILDIN_FUNC(isloggedin)
  8710. {
  8711. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8712. if (script_hasdata(st,3) && sd &&
  8713. sd->status.char_id != script_getnum(st,3))
  8714. sd = NULL;
  8715. push_val(st->stack,C_INT,sd!=NULL);
  8716. return 0;
  8717. }
  8718. /*==========================================
  8719. *
  8720. *------------------------------------------*/
  8721. BUILDIN_FUNC(setmapflagnosave)
  8722. {
  8723. int m,x,y;
  8724. unsigned short mapindex;
  8725. const char *str,*str2;
  8726. str=script_getstr(st,2);
  8727. str2=script_getstr(st,3);
  8728. x=script_getnum(st,4);
  8729. y=script_getnum(st,5);
  8730. m = map_mapname2mapid(str);
  8731. mapindex = mapindex_name2id(str2);
  8732. if(m >= 0 && mapindex) {
  8733. map[m].flag.nosave=1;
  8734. map[m].save.map=mapindex;
  8735. map[m].save.x=x;
  8736. map[m].save.y=y;
  8737. }
  8738. return 0;
  8739. }
  8740. BUILDIN_FUNC(getmapflag)
  8741. {
  8742. int m,i;
  8743. const char *str;
  8744. str=script_getstr(st,2);
  8745. i=script_getnum(st,3);
  8746. m = map_mapname2mapid(str);
  8747. if(m >= 0) {
  8748. switch(i) {
  8749. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8750. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8751. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  8752. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8753. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8754. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8755. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8756. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8757. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8758. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8759. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8760. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8761. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8762. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8763. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8764. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8765. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8766. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8767. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8768. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8769. /**
  8770. * No longer available, keeping here just in case it's back someday. [Ind]
  8771. **/
  8772. //case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8773. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8774. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8775. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8776. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8777. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8778. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8779. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8780. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8781. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8782. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8783. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8784. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8785. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8786. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8787. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8788. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8789. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8790. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8791. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8792. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8793. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8794. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8795. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  8796. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8797. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8798. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8799. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8800. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8801. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8802. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8803. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  8804. }
  8805. }
  8806. return 0;
  8807. }
  8808. BUILDIN_FUNC(setmapflag)
  8809. {
  8810. int m,i;
  8811. const char *str;
  8812. const char *val=NULL;
  8813. str=script_getstr(st,2);
  8814. i=script_getnum(st,3);
  8815. if(script_hasdata(st,4)){
  8816. val=script_getstr(st,4);
  8817. }
  8818. m = map_mapname2mapid(str);
  8819. if(m >= 0) {
  8820. switch(i) {
  8821. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  8822. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  8823. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  8824. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  8825. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  8826. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  8827. case MF_PVP: map[m].flag.pvp = 1; break;
  8828. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  8829. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  8830. case MF_GVG: map[m].flag.gvg = 1; break;
  8831. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  8832. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  8833. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  8834. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  8835. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  8836. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  8837. case MF_SNOW: map[m].flag.snow = 1; break;
  8838. case MF_FOG: map[m].flag.fog = 1; break;
  8839. case MF_SAKURA: map[m].flag.sakura = 1; break;
  8840. case MF_LEAVES: map[m].flag.leaves = 1; break;
  8841. /**
  8842. * No longer available, keeping here just in case it's back someday. [Ind]
  8843. **/
  8844. //case MF_RAIN: map[m].flag.rain = 1; break;
  8845. case MF_NOGO: map[m].flag.nogo = 1; break;
  8846. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  8847. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  8848. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  8849. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  8850. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  8851. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  8852. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  8853. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  8854. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  8855. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  8856. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  8857. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  8858. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  8859. case MF_RESTRICTED:
  8860. map[m].zone |= 1<<((int)atoi(val)+1);
  8861. map[m].flag.restricted=1;
  8862. break;
  8863. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8864. case MF_NODROP: map[m].flag.nodrop = 1; break;
  8865. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8866. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8867. case MF_NOVENDING: map[m].flag.novending = 1; break;
  8868. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  8869. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  8870. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  8871. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  8872. case MF_TOWN: map[m].flag.town = 1; break;
  8873. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  8874. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  8875. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  8876. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  8877. case MF_BATTLEGROUND: map[m].flag.battleground = (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  8878. case MF_RESET: map[m].flag.reset = 1; break;
  8879. }
  8880. }
  8881. return 0;
  8882. }
  8883. BUILDIN_FUNC(removemapflag)
  8884. {
  8885. int m,i;
  8886. const char *str;
  8887. const char *val=NULL;
  8888. str=script_getstr(st,2);
  8889. i=script_getnum(st,3);
  8890. if(script_hasdata(st,4)){
  8891. val=script_getstr(st,4);
  8892. }
  8893. m = map_mapname2mapid(str);
  8894. if(m >= 0) {
  8895. switch(i) {
  8896. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  8897. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  8898. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  8899. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  8900. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  8901. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  8902. case MF_PVP: map[m].flag.pvp = 0; break;
  8903. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  8904. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  8905. case MF_GVG: map[m].flag.gvg = 0; break;
  8906. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  8907. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  8908. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  8909. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  8910. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  8911. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  8912. case MF_SNOW: map[m].flag.snow = 0; break;
  8913. case MF_FOG: map[m].flag.fog = 0; break;
  8914. case MF_SAKURA: map[m].flag.sakura = 0; break;
  8915. case MF_LEAVES: map[m].flag.leaves = 0; break;
  8916. /**
  8917. * No longer available, keeping here just in case it's back someday. [Ind]
  8918. **/
  8919. //case MF_RAIN: map[m].flag.rain = 0; break;
  8920. case MF_NOGO: map[m].flag.nogo = 0; break;
  8921. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  8922. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  8923. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  8924. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  8925. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  8926. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  8927. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  8928. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  8929. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  8930. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  8931. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  8932. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  8933. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  8934. case MF_RESTRICTED:
  8935. map[m].zone ^= 1<<((int)atoi(val)+1);
  8936. if (map[m].zone == 0){
  8937. map[m].flag.restricted=0;
  8938. }
  8939. break;
  8940. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  8941. case MF_NODROP: map[m].flag.nodrop = 0; break;
  8942. case MF_JEXP: map[m].jexp = 0; break;
  8943. case MF_BEXP: map[m].bexp = 0; break;
  8944. case MF_NOVENDING: map[m].flag.novending = 0; break;
  8945. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  8946. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  8947. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  8948. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  8949. case MF_TOWN: map[m].flag.town = 0; break;
  8950. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  8951. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  8952. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  8953. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  8954. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  8955. case MF_RESET: map[m].flag.reset = 0; break;
  8956. }
  8957. }
  8958. return 0;
  8959. }
  8960. BUILDIN_FUNC(pvpon)
  8961. {
  8962. int m;
  8963. const char *str;
  8964. TBL_PC* sd = NULL;
  8965. struct s_mapiterator* iter;
  8966. str = script_getstr(st,2);
  8967. m = map_mapname2mapid(str);
  8968. if( m < 0 || map[m].flag.pvp )
  8969. return 0; // nothing to do
  8970. map[m].flag.pvp = 1;
  8971. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  8972. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8973. return 0;
  8974. iter = mapit_getallusers();
  8975. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8976. {
  8977. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  8978. continue; // not applicable
  8979. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8980. sd->pvp_rank = 0;
  8981. sd->pvp_lastusers = 0;
  8982. sd->pvp_point = 5;
  8983. sd->pvp_won = 0;
  8984. sd->pvp_lost = 0;
  8985. }
  8986. mapit_free(iter);
  8987. return 0;
  8988. }
  8989. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8990. {
  8991. TBL_PC* sd = (TBL_PC*)bl;
  8992. clif_pvpset(sd, 0, 0, 2);
  8993. if (sd->pvp_timer != INVALID_TIMER) {
  8994. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8995. sd->pvp_timer = INVALID_TIMER;
  8996. }
  8997. return 0;
  8998. }
  8999. BUILDIN_FUNC(pvpoff)
  9000. {
  9001. int m;
  9002. const char *str;
  9003. str=script_getstr(st,2);
  9004. m = map_mapname2mapid(str);
  9005. if(m < 0 || !map[m].flag.pvp)
  9006. return 0; //fixed Lupus
  9007. map[m].flag.pvp = 0;
  9008. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9009. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  9010. return 0;
  9011. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  9012. return 0;
  9013. }
  9014. BUILDIN_FUNC(gvgon)
  9015. {
  9016. int m;
  9017. const char *str;
  9018. str=script_getstr(st,2);
  9019. m = map_mapname2mapid(str);
  9020. if(m >= 0 && !map[m].flag.gvg) {
  9021. map[m].flag.gvg = 1;
  9022. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9023. }
  9024. return 0;
  9025. }
  9026. BUILDIN_FUNC(gvgoff)
  9027. {
  9028. int m;
  9029. const char *str;
  9030. str=script_getstr(st,2);
  9031. m = map_mapname2mapid(str);
  9032. if(m >= 0 && map[m].flag.gvg) {
  9033. map[m].flag.gvg = 0;
  9034. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9035. }
  9036. return 0;
  9037. }
  9038. /*==========================================
  9039. * Shows an emoticon on top of the player/npc
  9040. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  9041. *------------------------------------------*/
  9042. //Optional second parameter added by [Skotlex]
  9043. BUILDIN_FUNC(emotion)
  9044. {
  9045. int type;
  9046. int player=0;
  9047. type=script_getnum(st,2);
  9048. if(type < 0 || type > 100)
  9049. return 0;
  9050. if( script_hasdata(st,3) )
  9051. player=script_getnum(st,3);
  9052. if (player) {
  9053. TBL_PC *sd = NULL;
  9054. if( script_hasdata(st,4) )
  9055. sd = map_nick2sd(script_getstr(st,4));
  9056. else
  9057. sd = script_rid2sd(st);
  9058. if (sd)
  9059. clif_emotion(&sd->bl,type);
  9060. } else
  9061. if( script_hasdata(st,4) )
  9062. {
  9063. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9064. if(nd)
  9065. clif_emotion(&nd->bl,type);
  9066. }
  9067. else
  9068. clif_emotion(map_id2bl(st->oid),type);
  9069. return 0;
  9070. }
  9071. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  9072. {
  9073. int m=va_arg(ap,int);
  9074. int g_id=va_arg(ap,int);
  9075. int flag=va_arg(ap,int);
  9076. if(!sd || sd->bl.m != m)
  9077. return 0;
  9078. if(
  9079. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  9080. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  9081. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  9082. )
  9083. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  9084. return 1;
  9085. }
  9086. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  9087. {
  9088. struct mob_data *md=(struct mob_data *)bl;
  9089. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  9090. status_kill(bl);
  9091. return 0;
  9092. }
  9093. BUILDIN_FUNC(maprespawnguildid)
  9094. {
  9095. const char *mapname=script_getstr(st,2);
  9096. int g_id=script_getnum(st,3);
  9097. int flag=script_getnum(st,4);
  9098. int m=map_mapname2mapid(mapname);
  9099. if(m == -1)
  9100. return 0;
  9101. //Catch ALL players (in case some are 'between maps' on execution time)
  9102. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  9103. if (flag&4) //Remove script mobs.
  9104. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  9105. return 0;
  9106. }
  9107. BUILDIN_FUNC(agitstart)
  9108. {
  9109. if(agit_flag==1) return 0; // Agit already Start.
  9110. agit_flag=1;
  9111. guild_agit_start();
  9112. return 0;
  9113. }
  9114. BUILDIN_FUNC(agitend)
  9115. {
  9116. if(agit_flag==0) return 0; // Agit already End.
  9117. agit_flag=0;
  9118. guild_agit_end();
  9119. return 0;
  9120. }
  9121. BUILDIN_FUNC(agitstart2)
  9122. {
  9123. if(agit2_flag==1) return 0; // Agit2 already Start.
  9124. agit2_flag=1;
  9125. guild_agit2_start();
  9126. return 0;
  9127. }
  9128. BUILDIN_FUNC(agitend2)
  9129. {
  9130. if(agit2_flag==0) return 0; // Agit2 already End.
  9131. agit2_flag=0;
  9132. guild_agit2_end();
  9133. return 0;
  9134. }
  9135. /*==========================================
  9136. * Returns whether woe is on or off. // choice script
  9137. *------------------------------------------*/
  9138. BUILDIN_FUNC(agitcheck)
  9139. {
  9140. script_pushint(st,agit_flag);
  9141. return 0;
  9142. }
  9143. /*==========================================
  9144. * Returns whether woese is on or off. // choice script
  9145. *------------------------------------------*/
  9146. BUILDIN_FUNC(agitcheck2)
  9147. {
  9148. script_pushint(st,agit2_flag);
  9149. return 0;
  9150. }
  9151. /// Sets the guild_id of this npc.
  9152. ///
  9153. /// flagemblem <guild_id>;
  9154. BUILDIN_FUNC(flagemblem)
  9155. {
  9156. TBL_NPC* nd;
  9157. int g_id=script_getnum(st,2);
  9158. if(g_id < 0) return 0;
  9159. nd = (TBL_NPC*)map_id2nd(st->oid);
  9160. if( nd == NULL )
  9161. {
  9162. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  9163. }
  9164. else if( nd->subtype != SCRIPT )
  9165. {
  9166. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  9167. }
  9168. else
  9169. {
  9170. nd->u.scr.guild_id = g_id;
  9171. clif_guild_emblem_area(&nd->bl);
  9172. }
  9173. return 0;
  9174. }
  9175. BUILDIN_FUNC(getcastlename)
  9176. {
  9177. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9178. struct guild_castle* gc = guild_mapname2gc(mapname);
  9179. const char* name = (gc) ? gc->castle_name : "";
  9180. script_pushstrcopy(st,name);
  9181. return 0;
  9182. }
  9183. BUILDIN_FUNC(getcastledata)
  9184. {
  9185. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9186. int index = script_getnum(st,3);
  9187. struct guild_castle *gc = guild_mapname2gc(mapname);
  9188. if (gc == NULL) {
  9189. script_pushint(st,0);
  9190. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9191. return 1;
  9192. }
  9193. switch (index) {
  9194. case 1:
  9195. script_pushint(st,gc->guild_id); break;
  9196. case 2:
  9197. script_pushint(st,gc->economy); break;
  9198. case 3:
  9199. script_pushint(st,gc->defense); break;
  9200. case 4:
  9201. script_pushint(st,gc->triggerE); break;
  9202. case 5:
  9203. script_pushint(st,gc->triggerD); break;
  9204. case 6:
  9205. script_pushint(st,gc->nextTime); break;
  9206. case 7:
  9207. script_pushint(st,gc->payTime); break;
  9208. case 8:
  9209. script_pushint(st,gc->createTime); break;
  9210. case 9:
  9211. script_pushint(st,gc->visibleC); break;
  9212. default:
  9213. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  9214. script_pushint(st,gc->guardian[index-10].visible);
  9215. break;
  9216. }
  9217. script_pushint(st,0);
  9218. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9219. return 1;
  9220. }
  9221. return 0;
  9222. }
  9223. BUILDIN_FUNC(setcastledata)
  9224. {
  9225. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9226. int index = script_getnum(st,3);
  9227. int value = script_getnum(st,4);
  9228. struct guild_castle *gc = guild_mapname2gc(mapname);
  9229. if (gc == NULL) {
  9230. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9231. return 1;
  9232. }
  9233. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  9234. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9235. return 1;
  9236. }
  9237. guild_castledatasave(gc->castle_id, index, value);
  9238. return 0;
  9239. }
  9240. /* =====================================================================
  9241. * ギルド情報を要求する
  9242. * ---------------------------------------------------------------------*/
  9243. BUILDIN_FUNC(requestguildinfo)
  9244. {
  9245. int guild_id=script_getnum(st,2);
  9246. const char *event=NULL;
  9247. if( script_hasdata(st,3) ){
  9248. event=script_getstr(st,3);
  9249. check_event(st, event);
  9250. }
  9251. if(guild_id>0)
  9252. guild_npc_request_info(guild_id,event);
  9253. return 0;
  9254. }
  9255. /// Returns the number of cards that have been compounded onto the specified equipped item.
  9256. /// getequipcardcnt(<equipment slot>);
  9257. BUILDIN_FUNC(getequipcardcnt)
  9258. {
  9259. int i=-1,j,num;
  9260. TBL_PC *sd;
  9261. int count;
  9262. num=script_getnum(st,2);
  9263. sd=script_rid2sd(st);
  9264. if (num > 0 && num <= ARRAYLENGTH(equip))
  9265. i=pc_checkequip(sd,equip[num-1]);
  9266. if (i < 0 || !sd->inventory_data[i]) {
  9267. script_pushint(st,0);
  9268. return 0;
  9269. }
  9270. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9271. {
  9272. script_pushint(st,0);
  9273. return 0;
  9274. }
  9275. count = 0;
  9276. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  9277. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  9278. count++;
  9279. script_pushint(st,count);
  9280. return 0;
  9281. }
  9282. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  9283. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  9284. /// successremovecards <slot>;
  9285. BUILDIN_FUNC(successremovecards)
  9286. {
  9287. int i=-1,j,c,cardflag=0;
  9288. TBL_PC* sd = script_rid2sd(st);
  9289. int num = script_getnum(st,2);
  9290. if (num > 0 && num <= ARRAYLENGTH(equip))
  9291. i=pc_checkequip(sd,equip[num-1]);
  9292. if (i < 0 || !sd->inventory_data[i]) {
  9293. return 0;
  9294. }
  9295. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9296. return 0;
  9297. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9298. {
  9299. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9300. {// extract this card from the item
  9301. int flag;
  9302. struct item item_tmp;
  9303. cardflag = 1;
  9304. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9305. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9306. item_tmp.attribute=0,item_tmp.expire_time=0;
  9307. for (j = 0; j < MAX_SLOTS; j++)
  9308. item_tmp.card[j]=0;
  9309. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // 持てないならドロップ
  9310. clif_additem(sd,0,0,flag);
  9311. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9312. }
  9313. }
  9314. }
  9315. if(cardflag == 1)
  9316. { // カードを取り除いたアイテム所得
  9317. int flag;
  9318. struct item item_tmp;
  9319. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9320. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9321. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9322. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9323. item_tmp.card[j]=0;
  9324. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9325. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9326. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  9327. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // もてないならドロップ
  9328. clif_additem(sd,0,0,flag);
  9329. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9330. }
  9331. clif_misceffect(&sd->bl,3);
  9332. }
  9333. return 0;
  9334. }
  9335. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  9336. /// failedremovecards <slot>, <type>;
  9337. /// <type>=0 : will destroy both the item and the cards.
  9338. /// <type>=1 : will keep the item, but destroy the cards.
  9339. /// <type>=2 : will keep the cards, but destroy the item.
  9340. /// <type>=? : will just display the failure effect.
  9341. BUILDIN_FUNC(failedremovecards)
  9342. {
  9343. int i=-1,j,c,cardflag=0;
  9344. TBL_PC* sd = script_rid2sd(st);
  9345. int num = script_getnum(st,2);
  9346. int typefail = script_getnum(st,3);
  9347. if (num > 0 && num <= ARRAYLENGTH(equip))
  9348. i=pc_checkequip(sd,equip[num-1]);
  9349. if (i < 0 || !sd->inventory_data[i])
  9350. return 0;
  9351. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9352. return 0;
  9353. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9354. {
  9355. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9356. {
  9357. cardflag = 1;
  9358. if(typefail == 2)
  9359. {// add cards to inventory, clear
  9360. int flag;
  9361. struct item item_tmp;
  9362. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9363. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9364. item_tmp.attribute=0,item_tmp.expire_time=0;
  9365. for (j = 0; j < MAX_SLOTS; j++)
  9366. item_tmp.card[j]=0;
  9367. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  9368. clif_additem(sd,0,0,flag);
  9369. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9370. }
  9371. }
  9372. }
  9373. }
  9374. if(cardflag == 1)
  9375. {
  9376. if(typefail == 0 || typefail == 2){ // 武具損失
  9377. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  9378. }
  9379. if(typefail == 1){ // カードのみ損失(武具を返す)
  9380. int flag;
  9381. struct item item_tmp;
  9382. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9383. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9384. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9385. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9386. item_tmp.card[j]=0;
  9387. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9388. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9389. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  9390. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  9391. clif_additem(sd,0,0,flag);
  9392. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9393. }
  9394. }
  9395. clif_misceffect(&sd->bl,2);
  9396. }
  9397. return 0;
  9398. }
  9399. /* ================================================================
  9400. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  9401. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  9402. * improved by [Lance]
  9403. * ================================================================*/
  9404. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  9405. {
  9406. int x,y,m,check_val=0,check_ID=0,i=0;
  9407. struct guild *g = NULL;
  9408. struct party_data *p = NULL;
  9409. const char *str;
  9410. const char *mapname;
  9411. unsigned int index;
  9412. mapname=script_getstr(st,2);
  9413. str=script_getstr(st,3);
  9414. x=script_getnum(st,4);
  9415. y=script_getnum(st,5);
  9416. if(script_hasdata(st,7)){
  9417. check_val=script_getnum(st,6);
  9418. check_ID=script_getnum(st,7);
  9419. }
  9420. if((m=map_mapname2mapid(mapname))< 0)
  9421. return 0;
  9422. if(!(index=mapindex_name2id(str)))
  9423. return 0;
  9424. switch(check_val){
  9425. case 1:
  9426. g = guild_search(check_ID);
  9427. if (g){
  9428. for( i=0; i < g->max_member; i++)
  9429. {
  9430. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9431. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  9432. }
  9433. }
  9434. }
  9435. break;
  9436. case 2:
  9437. p = party_search(check_ID);
  9438. if(p){
  9439. for(i=0;i<MAX_PARTY; i++){
  9440. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9441. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  9442. }
  9443. }
  9444. }
  9445. break;
  9446. default:
  9447. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  9448. break;
  9449. }
  9450. return 0;
  9451. }
  9452. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9453. {
  9454. char *event=va_arg(ap,char *);
  9455. struct mob_data *md = ((struct mob_data *)bl);
  9456. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  9457. return 1;
  9458. return 0;
  9459. }
  9460. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9461. {
  9462. const char *mapname,*event;
  9463. int m;
  9464. mapname=script_getstr(st,2);
  9465. event=script_getstr(st,3);
  9466. if( strcmp(event, "all") == 0 )
  9467. event = NULL;
  9468. else
  9469. check_event(st, event);
  9470. if( strcmp(mapname, "this") == 0 ) {
  9471. struct map_session_data *sd = script_rid2sd(st);
  9472. if( sd )
  9473. m = sd->bl.m;
  9474. else {
  9475. script_pushint(st,-1);
  9476. return 0;
  9477. }
  9478. }
  9479. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9480. script_pushint(st,-1);
  9481. return 0;
  9482. }
  9483. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9484. {
  9485. script_pushint(st,-1);
  9486. return 0;
  9487. }
  9488. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9489. return 0;
  9490. }
  9491. BUILDIN_FUNC(marriage)
  9492. {
  9493. const char *partner=script_getstr(st,2);
  9494. TBL_PC *sd=script_rid2sd(st);
  9495. TBL_PC *p_sd=map_nick2sd(partner);
  9496. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9497. script_pushint(st,0);
  9498. return 0;
  9499. }
  9500. script_pushint(st,1);
  9501. return 0;
  9502. }
  9503. BUILDIN_FUNC(wedding_effect)
  9504. {
  9505. TBL_PC *sd=script_rid2sd(st);
  9506. struct block_list *bl;
  9507. if(sd==NULL) {
  9508. bl=map_id2bl(st->oid);
  9509. } else
  9510. bl=&sd->bl;
  9511. clif_wedding_effect(bl);
  9512. return 0;
  9513. }
  9514. BUILDIN_FUNC(divorce)
  9515. {
  9516. TBL_PC *sd=script_rid2sd(st);
  9517. if(sd==NULL || pc_divorce(sd) < 0){
  9518. script_pushint(st,0);
  9519. return 0;
  9520. }
  9521. script_pushint(st,1);
  9522. return 0;
  9523. }
  9524. BUILDIN_FUNC(ispartneron)
  9525. {
  9526. TBL_PC *sd=script_rid2sd(st);
  9527. if(sd==NULL || !pc_ismarried(sd) ||
  9528. map_charid2sd(sd->status.partner_id) == NULL) {
  9529. script_pushint(st,0);
  9530. return 0;
  9531. }
  9532. script_pushint(st,1);
  9533. return 0;
  9534. }
  9535. BUILDIN_FUNC(getpartnerid)
  9536. {
  9537. TBL_PC *sd=script_rid2sd(st);
  9538. if (sd == NULL) {
  9539. script_pushint(st,0);
  9540. return 0;
  9541. }
  9542. script_pushint(st,sd->status.partner_id);
  9543. return 0;
  9544. }
  9545. BUILDIN_FUNC(getchildid)
  9546. {
  9547. TBL_PC *sd=script_rid2sd(st);
  9548. if (sd == NULL) {
  9549. script_pushint(st,0);
  9550. return 0;
  9551. }
  9552. script_pushint(st,sd->status.child);
  9553. return 0;
  9554. }
  9555. BUILDIN_FUNC(getmotherid)
  9556. {
  9557. TBL_PC *sd=script_rid2sd(st);
  9558. if (sd == NULL) {
  9559. script_pushint(st,0);
  9560. return 0;
  9561. }
  9562. script_pushint(st,sd->status.mother);
  9563. return 0;
  9564. }
  9565. BUILDIN_FUNC(getfatherid)
  9566. {
  9567. TBL_PC *sd=script_rid2sd(st);
  9568. if (sd == NULL) {
  9569. script_pushint(st,0);
  9570. return 0;
  9571. }
  9572. script_pushint(st,sd->status.father);
  9573. return 0;
  9574. }
  9575. BUILDIN_FUNC(warppartner)
  9576. {
  9577. int x,y;
  9578. unsigned short mapindex;
  9579. const char *str;
  9580. TBL_PC *sd=script_rid2sd(st);
  9581. TBL_PC *p_sd=NULL;
  9582. if(sd==NULL || !pc_ismarried(sd) ||
  9583. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9584. script_pushint(st,0);
  9585. return 0;
  9586. }
  9587. str=script_getstr(st,2);
  9588. x=script_getnum(st,3);
  9589. y=script_getnum(st,4);
  9590. mapindex = mapindex_name2id(str);
  9591. if (mapindex) {
  9592. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  9593. script_pushint(st,1);
  9594. } else
  9595. script_pushint(st,0);
  9596. return 0;
  9597. }
  9598. /*================================================
  9599. * Script for Displaying MOB Information [Valaris]
  9600. *------------------------------------------------*/
  9601. BUILDIN_FUNC(strmobinfo)
  9602. {
  9603. int num=script_getnum(st,2);
  9604. int class_=script_getnum(st,3);
  9605. if(!mobdb_checkid(class_))
  9606. {
  9607. script_pushint(st,0);
  9608. return 0;
  9609. }
  9610. switch (num) {
  9611. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9612. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9613. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9614. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9615. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9616. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9617. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9618. default:
  9619. script_pushint(st,0);
  9620. break;
  9621. }
  9622. return 0;
  9623. }
  9624. /*==========================================
  9625. * Summon guardians [Valaris]
  9626. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9627. *------------------------------------------*/
  9628. BUILDIN_FUNC(guardian)
  9629. {
  9630. int class_=0,x=0,y=0,guardian=0;
  9631. const char *str,*map,*evt="";
  9632. struct script_data *data;
  9633. bool has_index = false;
  9634. map =script_getstr(st,2);
  9635. x =script_getnum(st,3);
  9636. y =script_getnum(st,4);
  9637. str =script_getstr(st,5);
  9638. class_=script_getnum(st,6);
  9639. if( script_hasdata(st,8) )
  9640. {// "<event label>",<guardian index>
  9641. evt=script_getstr(st,7);
  9642. guardian=script_getnum(st,8);
  9643. has_index = true;
  9644. } else if( script_hasdata(st,7) ){
  9645. data=script_getdata(st,7);
  9646. get_val(st,data);
  9647. if( data_isstring(data) )
  9648. {// "<event label>"
  9649. evt=script_getstr(st,7);
  9650. } else if( data_isint(data) )
  9651. {// <guardian index>
  9652. guardian=script_getnum(st,7);
  9653. has_index = true;
  9654. } else {
  9655. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9656. script_reportdata(data);
  9657. return 1;
  9658. }
  9659. }
  9660. check_event(st, evt);
  9661. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9662. return 0;
  9663. }
  9664. /*==========================================
  9665. * Invisible Walls [Zephyrus]
  9666. *------------------------------------------*/
  9667. BUILDIN_FUNC(setwall)
  9668. {
  9669. const char *map, *name;
  9670. int x, y, m, size, dir;
  9671. bool shootable;
  9672. map = script_getstr(st,2);
  9673. x = script_getnum(st,3);
  9674. y = script_getnum(st,4);
  9675. size = script_getnum(st,5);
  9676. dir = script_getnum(st,6);
  9677. shootable = script_getnum(st,7);
  9678. name = script_getstr(st,8);
  9679. if( (m = map_mapname2mapid(map)) < 0 )
  9680. return 0; // Invalid Map
  9681. map_iwall_set(m, x, y, size, dir, shootable, name);
  9682. return 0;
  9683. }
  9684. BUILDIN_FUNC(delwall)
  9685. {
  9686. const char *name = script_getstr(st,2);
  9687. map_iwall_remove(name);
  9688. return 0;
  9689. }
  9690. /// Retrieves various information about the specified guardian.
  9691. ///
  9692. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9693. /// type: 0 - whether it is deployed or not
  9694. /// 1 - maximum hp
  9695. /// 2 - current hp
  9696. ///
  9697. BUILDIN_FUNC(guardianinfo)
  9698. {
  9699. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9700. int id = script_getnum(st,3);
  9701. int type = script_getnum(st,4);
  9702. struct guild_castle* gc = guild_mapname2gc(mapname);
  9703. struct mob_data* gd;
  9704. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9705. {
  9706. script_pushint(st,-1);
  9707. return 0;
  9708. }
  9709. if( type == 0 )
  9710. script_pushint(st, gc->guardian[id].visible);
  9711. else
  9712. if( !gc->guardian[id].visible )
  9713. script_pushint(st,-1);
  9714. else
  9715. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9716. script_pushint(st,-1);
  9717. else
  9718. {
  9719. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9720. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9721. else
  9722. script_pushint(st,-1);
  9723. }
  9724. return 0;
  9725. }
  9726. /*==========================================
  9727. * IDからItem名
  9728. *------------------------------------------*/
  9729. BUILDIN_FUNC(getitemname)
  9730. {
  9731. int item_id=0;
  9732. struct item_data *i_data;
  9733. char *item_name;
  9734. struct script_data *data;
  9735. data=script_getdata(st,2);
  9736. get_val(st,data);
  9737. if( data_isstring(data) ){
  9738. const char *name=conv_str(st,data);
  9739. struct item_data *item_data = itemdb_searchname(name);
  9740. if( item_data )
  9741. item_id=item_data->nameid;
  9742. }else
  9743. item_id=conv_num(st,data);
  9744. i_data = itemdb_exists(item_id);
  9745. if (i_data == NULL)
  9746. {
  9747. script_pushconststr(st,"null");
  9748. return 0;
  9749. }
  9750. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  9751. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9752. script_pushstr(st,item_name);
  9753. return 0;
  9754. }
  9755. /*==========================================
  9756. * Returns number of slots an item has. [Skotlex]
  9757. *------------------------------------------*/
  9758. BUILDIN_FUNC(getitemslots)
  9759. {
  9760. int item_id;
  9761. struct item_data *i_data;
  9762. item_id=script_getnum(st,2);
  9763. i_data = itemdb_exists(item_id);
  9764. if (i_data)
  9765. script_pushint(st,i_data->slot);
  9766. else
  9767. script_pushint(st,-1);
  9768. return 0;
  9769. }
  9770. // TODO: add matk here if needed/once we get rid of REMODE
  9771. /*==========================================
  9772. * Returns some values of an item [Lupus]
  9773. * Price, Weight, etc...
  9774. getiteminfo(itemID,n), where n
  9775. 0 value_buy;
  9776. 1 value_sell;
  9777. 2 type;
  9778. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9779. if = 0, then monsters don't drop it at all (rare or a quest item)
  9780. if = -1, then this item is sold in NPC shops only
  9781. 4 sex;
  9782. 5 equip;
  9783. 6 weight;
  9784. 7 atk;
  9785. 8 def;
  9786. 9 range;
  9787. 10 slot;
  9788. 11 look;
  9789. 12 elv;
  9790. 13 wlv;
  9791. 14 view id
  9792. *------------------------------------------*/
  9793. BUILDIN_FUNC(getiteminfo)
  9794. {
  9795. int item_id,n;
  9796. int *item_arr;
  9797. struct item_data *i_data;
  9798. item_id = script_getnum(st,2);
  9799. n = script_getnum(st,3);
  9800. i_data = itemdb_exists(item_id);
  9801. if (i_data && n>=0 && n<=14) {
  9802. item_arr = (int*)&i_data->value_buy;
  9803. script_pushint(st,item_arr[n]);
  9804. } else
  9805. script_pushint(st,-1);
  9806. return 0;
  9807. }
  9808. /*==========================================
  9809. * Set some values of an item [Lupus]
  9810. * Price, Weight, etc...
  9811. setiteminfo(itemID,n,Value), where n
  9812. 0 value_buy;
  9813. 1 value_sell;
  9814. 2 type;
  9815. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9816. if = 0, then monsters don't drop it at all (rare or a quest item)
  9817. if = -1, then this item is sold in NPC shops only
  9818. 4 sex;
  9819. 5 equip;
  9820. 6 weight;
  9821. 7 atk;
  9822. 8 def;
  9823. 9 range;
  9824. 10 slot;
  9825. 11 look;
  9826. 12 elv;
  9827. 13 wlv;
  9828. 14 view id
  9829. * Returns Value or -1 if the wrong field's been set
  9830. *------------------------------------------*/
  9831. BUILDIN_FUNC(setiteminfo)
  9832. {
  9833. int item_id,n,value;
  9834. int *item_arr;
  9835. struct item_data *i_data;
  9836. item_id = script_getnum(st,2);
  9837. n = script_getnum(st,3);
  9838. value = script_getnum(st,4);
  9839. i_data = itemdb_exists(item_id);
  9840. if (i_data && n>=0 && n<=14) {
  9841. item_arr = (int*)&i_data->value_buy;
  9842. item_arr[n] = value;
  9843. script_pushint(st,value);
  9844. } else
  9845. script_pushint(st,-1);
  9846. return 0;
  9847. }
  9848. /*==========================================
  9849. * Returns value from equipped item slot n [Lupus]
  9850. getequipcardid(num,slot)
  9851. where
  9852. num = eqip position slot
  9853. slot = 0,1,2,3 (Card Slot N)
  9854. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9855. it's useful when you want to check item cards or if it's signed
  9856. Useful for such quests as "Sign this refined item with players name" etc
  9857. Hat[0] +4 -> Player's Hat[0] +4
  9858. *------------------------------------------*/
  9859. BUILDIN_FUNC(getequipcardid)
  9860. {
  9861. int i=-1,num,slot;
  9862. TBL_PC *sd;
  9863. num=script_getnum(st,2);
  9864. slot=script_getnum(st,3);
  9865. sd=script_rid2sd(st);
  9866. if (num > 0 && num <= ARRAYLENGTH(equip))
  9867. i=pc_checkequip(sd,equip[num-1]);
  9868. if(i >= 0 && slot>=0 && slot<4)
  9869. script_pushint(st,sd->status.inventory[i].card[slot]);
  9870. else
  9871. script_pushint(st,0);
  9872. return 0;
  9873. }
  9874. /*==========================================
  9875. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9876. *------------------------------------------*/
  9877. BUILDIN_FUNC(petskillbonus)
  9878. {
  9879. struct pet_data *pd;
  9880. TBL_PC *sd=script_rid2sd(st);
  9881. if(sd==NULL || sd->pd==NULL)
  9882. return 0;
  9883. pd=sd->pd;
  9884. if (pd->bonus)
  9885. { //Clear previous bonus
  9886. if (pd->bonus->timer != INVALID_TIMER)
  9887. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9888. } else //init
  9889. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9890. pd->bonus->type=script_getnum(st,2);
  9891. pd->bonus->val=script_getnum(st,3);
  9892. pd->bonus->duration=script_getnum(st,4);
  9893. pd->bonus->delay=script_getnum(st,5);
  9894. if (pd->state.skillbonus == 1)
  9895. pd->state.skillbonus=0; // waiting state
  9896. // wait for timer to start
  9897. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9898. pd->bonus->timer = INVALID_TIMER;
  9899. else
  9900. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9901. return 0;
  9902. }
  9903. /*==========================================
  9904. * pet looting [Valaris] //Rewritten by [Skotlex]
  9905. *------------------------------------------*/
  9906. BUILDIN_FUNC(petloot)
  9907. {
  9908. int max;
  9909. struct pet_data *pd;
  9910. TBL_PC *sd=script_rid2sd(st);
  9911. if(sd==NULL || sd->pd==NULL)
  9912. return 0;
  9913. max=script_getnum(st,2);
  9914. if(max < 1)
  9915. max = 1; //Let'em loot at least 1 item.
  9916. else if (max > MAX_PETLOOT_SIZE)
  9917. max = MAX_PETLOOT_SIZE;
  9918. pd = sd->pd;
  9919. if (pd->loot != NULL)
  9920. { //Release whatever was there already and reallocate memory
  9921. pet_lootitem_drop(pd, pd->msd);
  9922. aFree(pd->loot->item);
  9923. }
  9924. else
  9925. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9926. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9927. pd->loot->max=max;
  9928. pd->loot->count = 0;
  9929. pd->loot->weight = 0;
  9930. return 0;
  9931. }
  9932. /*==========================================
  9933. * PCの所持品情報読み取り
  9934. *------------------------------------------*/
  9935. BUILDIN_FUNC(getinventorylist)
  9936. {
  9937. TBL_PC *sd=script_rid2sd(st);
  9938. char card_var[NAME_LENGTH];
  9939. int i,j=0,k;
  9940. if(!sd) return 0;
  9941. for(i=0;i<MAX_INVENTORY;i++){
  9942. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9943. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  9944. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  9945. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  9946. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  9947. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  9948. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  9949. for (k = 0; k < MAX_SLOTS; k++)
  9950. {
  9951. sprintf(card_var, "@inventorylist_card%d",k+1);
  9952. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  9953. }
  9954. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  9955. j++;
  9956. }
  9957. }
  9958. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9959. return 0;
  9960. }
  9961. BUILDIN_FUNC(getskilllist)
  9962. {
  9963. TBL_PC *sd=script_rid2sd(st);
  9964. int i,j=0;
  9965. if(!sd) return 0;
  9966. for(i=0;i<MAX_SKILL;i++){
  9967. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9968. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  9969. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  9970. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  9971. j++;
  9972. }
  9973. }
  9974. pc_setreg(sd,add_str("@skilllist_count"),j);
  9975. return 0;
  9976. }
  9977. BUILDIN_FUNC(clearitem)
  9978. {
  9979. TBL_PC *sd=script_rid2sd(st);
  9980. int i;
  9981. if(sd==NULL) return 0;
  9982. for (i=0; i<MAX_INVENTORY; i++) {
  9983. if (sd->status.inventory[i].amount) {
  9984. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  9985. }
  9986. }
  9987. return 0;
  9988. }
  9989. /*==========================================
  9990. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9991. *------------------------------------------*/
  9992. BUILDIN_FUNC(disguise)
  9993. {
  9994. int id;
  9995. TBL_PC* sd = script_rid2sd(st);
  9996. if (sd == NULL) return 0;
  9997. id = script_getnum(st,2);
  9998. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9999. pc_disguise(sd, id);
  10000. script_pushint(st,id);
  10001. } else
  10002. script_pushint(st,0);
  10003. return 0;
  10004. }
  10005. /*==========================================
  10006. * Undisguise Player (returns 1 if success, 0 on fail)
  10007. *------------------------------------------*/
  10008. BUILDIN_FUNC(undisguise)
  10009. {
  10010. TBL_PC* sd = script_rid2sd(st);
  10011. if (sd == NULL) return 0;
  10012. if (sd->disguise) {
  10013. pc_disguise(sd, 0);
  10014. script_pushint(st,0);
  10015. } else {
  10016. script_pushint(st,1);
  10017. }
  10018. return 0;
  10019. }
  10020. /*==========================================
  10021. * NPCクラスチェンジ
  10022. * classは変わりたいclass
  10023. * typeは通常0なのかな?
  10024. *------------------------------------------*/
  10025. BUILDIN_FUNC(classchange)
  10026. {
  10027. int _class,type;
  10028. struct block_list *bl=map_id2bl(st->oid);
  10029. if(bl==NULL) return 0;
  10030. _class=script_getnum(st,2);
  10031. type=script_getnum(st,3);
  10032. clif_class_change(bl,_class,type);
  10033. return 0;
  10034. }
  10035. /*==========================================
  10036. * NPCから発生するエフェクト
  10037. *------------------------------------------*/
  10038. BUILDIN_FUNC(misceffect)
  10039. {
  10040. int type;
  10041. type=script_getnum(st,2);
  10042. if(st->oid && st->oid != fake_nd->bl.id) {
  10043. struct block_list *bl = map_id2bl(st->oid);
  10044. if (bl)
  10045. clif_specialeffect(bl,type,AREA);
  10046. } else{
  10047. TBL_PC *sd=script_rid2sd(st);
  10048. if(sd)
  10049. clif_specialeffect(&sd->bl,type,AREA);
  10050. }
  10051. return 0;
  10052. }
  10053. /*==========================================
  10054. * Play a BGM on a single client [Rikter/Yommy]
  10055. *------------------------------------------*/
  10056. BUILDIN_FUNC(playBGM)
  10057. {
  10058. const char* name;
  10059. struct map_session_data* sd;
  10060. if( ( sd = script_rid2sd(st) ) != NULL )
  10061. {
  10062. name = script_getstr(st,2);
  10063. clif_playBGM(sd, name);
  10064. }
  10065. return 0;
  10066. }
  10067. static int playBGM_sub(struct block_list* bl,va_list ap)
  10068. {
  10069. const char* name = va_arg(ap,const char*);
  10070. clif_playBGM(BL_CAST(BL_PC, bl), name);
  10071. return 0;
  10072. }
  10073. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  10074. {
  10075. const char* name = va_arg(args, const char*);
  10076. clif_playBGM(sd, name);
  10077. return 0;
  10078. }
  10079. /*==========================================
  10080. * Play a BGM on multiple client [Rikter/Yommy]
  10081. *------------------------------------------*/
  10082. BUILDIN_FUNC(playBGMall)
  10083. {
  10084. const char* name;
  10085. name = script_getstr(st,2);
  10086. if( script_hasdata(st,7) )
  10087. {// specified part of map
  10088. const char* map = script_getstr(st,3);
  10089. int x0 = script_getnum(st,4);
  10090. int y0 = script_getnum(st,5);
  10091. int x1 = script_getnum(st,6);
  10092. int y1 = script_getnum(st,7);
  10093. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  10094. }
  10095. else if( script_hasdata(st,3) )
  10096. {// entire map
  10097. const char* map = script_getstr(st,3);
  10098. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  10099. }
  10100. else
  10101. {// entire server
  10102. map_foreachpc(&playBGM_foreachpc_sub, name);
  10103. }
  10104. return 0;
  10105. }
  10106. /*==========================================
  10107. * サウンドエフェクト
  10108. *------------------------------------------*/
  10109. BUILDIN_FUNC(soundeffect)
  10110. {
  10111. TBL_PC* sd = script_rid2sd(st);
  10112. const char* name = script_getstr(st,2);
  10113. int type = script_getnum(st,3);
  10114. if(sd)
  10115. {
  10116. clif_soundeffect(sd,&sd->bl,name,type);
  10117. }
  10118. return 0;
  10119. }
  10120. int soundeffect_sub(struct block_list* bl,va_list ap)
  10121. {
  10122. char* name = va_arg(ap,char*);
  10123. int type = va_arg(ap,int);
  10124. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  10125. return 0;
  10126. }
  10127. /*==========================================
  10128. * Play a sound effect (.wav) on multiple clients
  10129. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  10130. *------------------------------------------*/
  10131. BUILDIN_FUNC(soundeffectall)
  10132. {
  10133. struct block_list* bl;
  10134. const char* name;
  10135. int type;
  10136. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  10137. if (!bl)
  10138. return 0;
  10139. name = script_getstr(st,2);
  10140. type = script_getnum(st,3);
  10141. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  10142. if(!script_hasdata(st,4))
  10143. { // area around
  10144. clif_soundeffectall(bl, name, type, AREA);
  10145. }
  10146. else
  10147. if(!script_hasdata(st,5))
  10148. { // entire map
  10149. const char* map = script_getstr(st,4);
  10150. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  10151. }
  10152. else
  10153. if(script_hasdata(st,8))
  10154. { // specified part of map
  10155. const char* map = script_getstr(st,4);
  10156. int x0 = script_getnum(st,5);
  10157. int y0 = script_getnum(st,6);
  10158. int x1 = script_getnum(st,7);
  10159. int y1 = script_getnum(st,8);
  10160. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  10161. }
  10162. else
  10163. {
  10164. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  10165. }
  10166. return 0;
  10167. }
  10168. /*==========================================
  10169. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  10170. *------------------------------------------*/
  10171. BUILDIN_FUNC(petrecovery)
  10172. {
  10173. struct pet_data *pd;
  10174. TBL_PC *sd=script_rid2sd(st);
  10175. if(sd==NULL || sd->pd==NULL)
  10176. return 0;
  10177. pd=sd->pd;
  10178. if (pd->recovery)
  10179. { //Halt previous bonus
  10180. if (pd->recovery->timer != INVALID_TIMER)
  10181. delete_timer(pd->recovery->timer, pet_recovery_timer);
  10182. } else //Init
  10183. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  10184. pd->recovery->type = (sc_type)script_getnum(st,2);
  10185. pd->recovery->delay = script_getnum(st,3);
  10186. pd->recovery->timer = INVALID_TIMER;
  10187. return 0;
  10188. }
  10189. /*==========================================
  10190. * pet healing [Valaris] //Rewritten by [Skotlex]
  10191. *------------------------------------------*/
  10192. BUILDIN_FUNC(petheal)
  10193. {
  10194. struct pet_data *pd;
  10195. TBL_PC *sd=script_rid2sd(st);
  10196. if(sd==NULL || sd->pd==NULL)
  10197. return 0;
  10198. pd=sd->pd;
  10199. if (pd->s_skill)
  10200. { //Clear previous skill
  10201. if (pd->s_skill->timer != INVALID_TIMER)
  10202. {
  10203. if (pd->s_skill->id)
  10204. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10205. else
  10206. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10207. }
  10208. } else //init memory
  10209. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10210. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  10211. //Use the lv as the amount to heal
  10212. pd->s_skill->lv=script_getnum(st,2);
  10213. pd->s_skill->delay=script_getnum(st,3);
  10214. pd->s_skill->hp=script_getnum(st,4);
  10215. pd->s_skill->sp=script_getnum(st,5);
  10216. //Use delay as initial offset to avoid skill/heal exploits
  10217. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10218. pd->s_skill->timer = INVALID_TIMER;
  10219. else
  10220. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  10221. return 0;
  10222. }
  10223. /*==========================================
  10224. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  10225. *------------------------------------------*/
  10226. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  10227. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  10228. BUILDIN_FUNC(petskillattack)
  10229. {
  10230. struct pet_data *pd;
  10231. TBL_PC *sd=script_rid2sd(st);
  10232. if(sd==NULL || sd->pd==NULL)
  10233. return 0;
  10234. pd=sd->pd;
  10235. if (pd->a_skill == NULL)
  10236. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10237. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10238. pd->a_skill->lv=script_getnum(st,3);
  10239. pd->a_skill->div_ = 0;
  10240. pd->a_skill->rate=script_getnum(st,4);
  10241. pd->a_skill->bonusrate=script_getnum(st,5);
  10242. return 0;
  10243. }
  10244. /*==========================================
  10245. * pet attack skills [Valaris]
  10246. *------------------------------------------*/
  10247. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  10248. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  10249. BUILDIN_FUNC(petskillattack2)
  10250. {
  10251. struct pet_data *pd;
  10252. TBL_PC *sd=script_rid2sd(st);
  10253. if(sd==NULL || sd->pd==NULL)
  10254. return 0;
  10255. pd=sd->pd;
  10256. if (pd->a_skill == NULL)
  10257. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10258. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10259. pd->a_skill->lv=script_getnum(st,3);
  10260. pd->a_skill->div_ = script_getnum(st,4);
  10261. pd->a_skill->rate=script_getnum(st,5);
  10262. pd->a_skill->bonusrate=script_getnum(st,6);
  10263. return 0;
  10264. }
  10265. /*==========================================
  10266. * pet support skills [Skotlex]
  10267. *------------------------------------------*/
  10268. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  10269. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  10270. BUILDIN_FUNC(petskillsupport)
  10271. {
  10272. struct pet_data *pd;
  10273. TBL_PC *sd=script_rid2sd(st);
  10274. if(sd==NULL || sd->pd==NULL)
  10275. return 0;
  10276. pd=sd->pd;
  10277. if (pd->s_skill)
  10278. { //Clear previous skill
  10279. if (pd->s_skill->timer != INVALID_TIMER)
  10280. {
  10281. if (pd->s_skill->id)
  10282. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10283. else
  10284. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10285. }
  10286. } else //init memory
  10287. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10288. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10289. pd->s_skill->lv=script_getnum(st,3);
  10290. pd->s_skill->delay=script_getnum(st,4);
  10291. pd->s_skill->hp=script_getnum(st,5);
  10292. pd->s_skill->sp=script_getnum(st,6);
  10293. //Use delay as initial offset to avoid skill/heal exploits
  10294. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10295. pd->s_skill->timer = INVALID_TIMER;
  10296. else
  10297. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  10298. return 0;
  10299. }
  10300. /*==========================================
  10301. * Scripted skill effects [Celest]
  10302. *------------------------------------------*/
  10303. /// skilleffect <skill id>,<level>
  10304. /// skilleffect "<skill name>",<level>
  10305. BUILDIN_FUNC(skilleffect)
  10306. {
  10307. TBL_PC *sd;
  10308. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10309. int skilllv=script_getnum(st,3);
  10310. sd=script_rid2sd(st);
  10311. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  10312. return 0;
  10313. }
  10314. /*==========================================
  10315. * NPC skill effects [Valaris]
  10316. *------------------------------------------*/
  10317. /// npcskilleffect <skill id>,<level>,<x>,<y>
  10318. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  10319. BUILDIN_FUNC(npcskilleffect)
  10320. {
  10321. struct block_list *bl= map_id2bl(st->oid);
  10322. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10323. int skilllv=script_getnum(st,3);
  10324. int x=script_getnum(st,4);
  10325. int y=script_getnum(st,5);
  10326. if (bl)
  10327. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  10328. return 0;
  10329. }
  10330. /*==========================================
  10331. * Special effects [Valaris]
  10332. *------------------------------------------*/
  10333. BUILDIN_FUNC(specialeffect)
  10334. {
  10335. struct block_list *bl=map_id2bl(st->oid);
  10336. int type = script_getnum(st,2);
  10337. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10338. if(bl==NULL)
  10339. return 0;
  10340. if( script_hasdata(st,4) )
  10341. {
  10342. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10343. if(nd)
  10344. clif_specialeffect(&nd->bl, type, target);
  10345. }
  10346. else
  10347. {
  10348. if (target == SELF) {
  10349. TBL_PC *sd=script_rid2sd(st);
  10350. if (sd)
  10351. clif_specialeffect_single(bl,type,sd->fd);
  10352. } else {
  10353. clif_specialeffect(bl, type, target);
  10354. }
  10355. }
  10356. return 0;
  10357. }
  10358. BUILDIN_FUNC(specialeffect2)
  10359. {
  10360. TBL_PC *sd=script_rid2sd(st);
  10361. int type = script_getnum(st,2);
  10362. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10363. if( script_hasdata(st,4) )
  10364. sd = map_nick2sd(script_getstr(st,4));
  10365. if (sd)
  10366. clif_specialeffect(&sd->bl, type, target);
  10367. return 0;
  10368. }
  10369. /*==========================================
  10370. * Nude [Valaris]
  10371. *------------------------------------------*/
  10372. BUILDIN_FUNC(nude)
  10373. {
  10374. TBL_PC *sd=script_rid2sd(st);
  10375. int i,calcflag=0;
  10376. if(sd==NULL)
  10377. return 0;
  10378. for(i=0;i<11;i++)
  10379. if(sd->equip_index[i] >= 0) {
  10380. if(!calcflag)
  10381. calcflag=1;
  10382. pc_unequipitem(sd,sd->equip_index[i],2);
  10383. }
  10384. if(calcflag)
  10385. status_calc_pc(sd,0);
  10386. return 0;
  10387. }
  10388. /*==========================================
  10389. * gmcommand [MouseJstr]
  10390. *------------------------------------------*/
  10391. BUILDIN_FUNC(atcommand)
  10392. {
  10393. TBL_PC dummy_sd;
  10394. TBL_PC* sd;
  10395. int fd;
  10396. const char* cmd;
  10397. cmd = script_getstr(st,2);
  10398. if (st->rid) {
  10399. sd = script_rid2sd(st);
  10400. fd = sd->fd;
  10401. } else { //Use a dummy character.
  10402. sd = &dummy_sd;
  10403. fd = 0;
  10404. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10405. if (st->oid)
  10406. {
  10407. struct block_list* bl = map_id2bl(st->oid);
  10408. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10409. if (bl->type == BL_NPC)
  10410. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10411. }
  10412. }
  10413. if (!is_atcommand(fd, sd, cmd, 0)) {
  10414. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  10415. script_reportsrc(st);
  10416. return 1;
  10417. }
  10418. return 0;
  10419. }
  10420. /*==========================================
  10421. * Displays a message for the player only (like system messages like "you got an apple" )
  10422. *------------------------------------------*/
  10423. BUILDIN_FUNC(dispbottom)
  10424. {
  10425. TBL_PC *sd=script_rid2sd(st);
  10426. const char *message;
  10427. message=script_getstr(st,2);
  10428. if(sd)
  10429. clif_disp_onlyself(sd,message,(int)strlen(message));
  10430. return 0;
  10431. }
  10432. /*==========================================
  10433. * All The Players Full Recovery
  10434. * (HP/SP full restore and resurrect if need)
  10435. *------------------------------------------*/
  10436. BUILDIN_FUNC(recovery)
  10437. {
  10438. TBL_PC* sd;
  10439. struct s_mapiterator* iter;
  10440. iter = mapit_getallusers();
  10441. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10442. {
  10443. if(pc_isdead(sd))
  10444. status_revive(&sd->bl, 100, 100);
  10445. else
  10446. status_percent_heal(&sd->bl, 100, 100);
  10447. clif_displaymessage(sd->fd,"You have been recovered!");
  10448. }
  10449. mapit_free(iter);
  10450. return 0;
  10451. }
  10452. /*==========================================
  10453. * Get your pet info: getpetinfo(n)
  10454. * n -> 0:pet_id 1:pet_class 2:pet_name
  10455. * 3:friendly 4:hungry, 5: rename flag.
  10456. *------------------------------------------*/
  10457. BUILDIN_FUNC(getpetinfo)
  10458. {
  10459. TBL_PC *sd=script_rid2sd(st);
  10460. TBL_PET *pd;
  10461. int type=script_getnum(st,2);
  10462. if(!sd || !sd->pd) {
  10463. if (type == 2)
  10464. script_pushconststr(st,"null");
  10465. else
  10466. script_pushint(st,0);
  10467. return 0;
  10468. }
  10469. pd = sd->pd;
  10470. switch(type){
  10471. case 0: script_pushint(st,pd->pet.pet_id); break;
  10472. case 1: script_pushint(st,pd->pet.class_); break;
  10473. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10474. case 3: script_pushint(st,pd->pet.intimate); break;
  10475. case 4: script_pushint(st,pd->pet.hungry); break;
  10476. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10477. default:
  10478. script_pushint(st,0);
  10479. break;
  10480. }
  10481. return 0;
  10482. }
  10483. /*==========================================
  10484. * Get your homunculus info: gethominfo(n)
  10485. * n -> 0:hom_id 1:class 2:name
  10486. * 3:friendly 4:hungry, 5: rename flag.
  10487. * 6: level
  10488. *------------------------------------------*/
  10489. BUILDIN_FUNC(gethominfo)
  10490. {
  10491. TBL_PC *sd=script_rid2sd(st);
  10492. TBL_HOM *hd;
  10493. int type=script_getnum(st,2);
  10494. hd = sd?sd->hd:NULL;
  10495. if(!merc_is_hom_active(hd))
  10496. {
  10497. if (type == 2)
  10498. script_pushconststr(st,"null");
  10499. else
  10500. script_pushint(st,0);
  10501. return 0;
  10502. }
  10503. switch(type){
  10504. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10505. case 1: script_pushint(st,hd->homunculus.class_); break;
  10506. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10507. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10508. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10509. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10510. case 6: script_pushint(st,hd->homunculus.level); break;
  10511. default:
  10512. script_pushint(st,0);
  10513. break;
  10514. }
  10515. return 0;
  10516. }
  10517. /// Retrieves information about character's mercenary
  10518. /// getmercinfo <type>[,<char id>];
  10519. BUILDIN_FUNC(getmercinfo)
  10520. {
  10521. int type, char_id;
  10522. struct map_session_data* sd;
  10523. struct mercenary_data* md;
  10524. type = script_getnum(st,2);
  10525. if( script_hasdata(st,3) )
  10526. {
  10527. char_id = script_getnum(st,3);
  10528. if( ( sd = map_charid2sd(char_id) ) == NULL )
  10529. {
  10530. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  10531. script_pushnil(st);
  10532. return 1;
  10533. }
  10534. }
  10535. else
  10536. {
  10537. if( ( sd = script_rid2sd(st) ) == NULL )
  10538. {
  10539. script_pushnil(st);
  10540. return 0;
  10541. }
  10542. }
  10543. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  10544. switch( type )
  10545. {
  10546. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  10547. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  10548. case 2:
  10549. if( md )
  10550. script_pushstrcopy(st,md->db->name);
  10551. else
  10552. script_pushconststr(st,"");
  10553. break;
  10554. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  10555. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  10556. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  10557. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  10558. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  10559. default:
  10560. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  10561. script_pushnil(st);
  10562. return 1;
  10563. }
  10564. return 0;
  10565. }
  10566. /*==========================================
  10567. * Shows wether your inventory(and equips) contain
  10568. selected card or not.
  10569. checkequipedcard(4001);
  10570. *------------------------------------------*/
  10571. BUILDIN_FUNC(checkequipedcard)
  10572. {
  10573. TBL_PC *sd=script_rid2sd(st);
  10574. if(sd){
  10575. int n,i,c=0;
  10576. c=script_getnum(st,2);
  10577. for(i=0;i<MAX_INVENTORY;i++){
  10578. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10579. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10580. continue;
  10581. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10582. if(sd->status.inventory[i].card[n]==c){
  10583. script_pushint(st,1);
  10584. return 0;
  10585. }
  10586. }
  10587. }
  10588. }
  10589. }
  10590. script_pushint(st,0);
  10591. return 0;
  10592. }
  10593. BUILDIN_FUNC(jump_zero)
  10594. {
  10595. int sel;
  10596. sel=script_getnum(st,2);
  10597. if(!sel) {
  10598. int pos;
  10599. if( !data_islabel(script_getdata(st,3)) ){
  10600. ShowError("script: jump_zero: not label !\n");
  10601. st->state=END;
  10602. return 1;
  10603. }
  10604. pos=script_getnum(st,3);
  10605. st->pos=pos;
  10606. st->state=GOTO;
  10607. }
  10608. return 0;
  10609. }
  10610. /*==========================================
  10611. * movenpc [MouseJstr]
  10612. *------------------------------------------*/
  10613. BUILDIN_FUNC(movenpc)
  10614. {
  10615. TBL_NPC *nd = NULL;
  10616. const char *npc;
  10617. int x,y;
  10618. npc = script_getstr(st,2);
  10619. x = script_getnum(st,3);
  10620. y = script_getnum(st,4);
  10621. if ((nd = npc_name2id(npc)) == NULL)
  10622. return -1;
  10623. if (script_hasdata(st,5))
  10624. nd->ud.dir = script_getnum(st,5) % 8;
  10625. npc_movenpc(nd, x, y);
  10626. return 0;
  10627. }
  10628. /*==========================================
  10629. * message [MouseJstr]
  10630. *------------------------------------------*/
  10631. BUILDIN_FUNC(message)
  10632. {
  10633. const char *msg,*player;
  10634. TBL_PC *pl_sd = NULL;
  10635. player = script_getstr(st,2);
  10636. msg = script_getstr(st,3);
  10637. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10638. return 0;
  10639. clif_displaymessage(pl_sd->fd, msg);
  10640. return 0;
  10641. }
  10642. /*==========================================
  10643. * npctalk (sends message to surrounding area)
  10644. *------------------------------------------*/
  10645. BUILDIN_FUNC(npctalk)
  10646. {
  10647. const char* str;
  10648. char name[NAME_LENGTH], message[256];
  10649. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10650. str = script_getstr(st,2);
  10651. if(nd)
  10652. {
  10653. safestrncpy(name, nd->name, sizeof(name));
  10654. strtok(name, "#"); // discard extra name identifier if present
  10655. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  10656. clif_message(&nd->bl, message);
  10657. }
  10658. return 0;
  10659. }
  10660. // change npc walkspeed [Valaris]
  10661. BUILDIN_FUNC(npcspeed)
  10662. {
  10663. struct npc_data* nd;
  10664. int speed;
  10665. speed = script_getnum(st,2);
  10666. nd =(struct npc_data *)map_id2bl(st->oid);
  10667. if( nd )
  10668. {
  10669. nd->speed = speed;
  10670. nd->ud.state.speed_changed = 1;
  10671. }
  10672. return 0;
  10673. }
  10674. // make an npc walk to a position [Valaris]
  10675. BUILDIN_FUNC(npcwalkto)
  10676. {
  10677. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10678. int x=0,y=0;
  10679. x=script_getnum(st,2);
  10680. y=script_getnum(st,3);
  10681. if(nd) {
  10682. unit_walktoxy(&nd->bl,x,y,0);
  10683. }
  10684. return 0;
  10685. }
  10686. // stop an npc's movement [Valaris]
  10687. BUILDIN_FUNC(npcstop)
  10688. {
  10689. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10690. if(nd) {
  10691. unit_stop_walking(&nd->bl,1|4);
  10692. }
  10693. return 0;
  10694. }
  10695. /*==========================================
  10696. * getlook char info. getlook(arg)
  10697. *------------------------------------------*/
  10698. BUILDIN_FUNC(getlook)
  10699. {
  10700. int type,val;
  10701. TBL_PC *sd;
  10702. sd=script_rid2sd(st);
  10703. type=script_getnum(st,2);
  10704. val=-1;
  10705. switch(type) {
  10706. case LOOK_HAIR: val=sd->status.hair; break; //1
  10707. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10708. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10709. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10710. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10711. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10712. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10713. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10714. case LOOK_SHOES: break; //9
  10715. }
  10716. script_pushint(st,val);
  10717. return 0;
  10718. }
  10719. /*==========================================
  10720. * get char save point. argument: 0- map name, 1- x, 2- y
  10721. *------------------------------------------*/
  10722. BUILDIN_FUNC(getsavepoint)
  10723. {
  10724. TBL_PC* sd;
  10725. int type;
  10726. sd = script_rid2sd(st);
  10727. if (sd == NULL) {
  10728. script_pushint(st,0);
  10729. return 0;
  10730. }
  10731. type = script_getnum(st,2);
  10732. switch(type) {
  10733. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10734. case 1: script_pushint(st,sd->status.save_point.x); break;
  10735. case 2: script_pushint(st,sd->status.save_point.y); break;
  10736. default:
  10737. script_pushint(st,0);
  10738. break;
  10739. }
  10740. return 0;
  10741. }
  10742. /*==========================================
  10743. * Get position for char/npc/pet/mob objects. Added by Lorky
  10744. *
  10745. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10746. * where type:
  10747. * MapName$ - String variable for output map name
  10748. * MapX - Integer variable for output coord X
  10749. * MapY - Integer variable for output coord Y
  10750. * type - type of object
  10751. * 0 - Character coord
  10752. * 1 - NPC coord
  10753. * 2 - Pet coord
  10754. * 3 - Mob coord (not released)
  10755. * 4 - Homun coord
  10756. * CharName$ - Name object. If miss or "this" the current object
  10757. *
  10758. * Return:
  10759. * 0 - success
  10760. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10761. *------------------------------------------*/
  10762. BUILDIN_FUNC(getmapxy)
  10763. {
  10764. struct block_list *bl = NULL;
  10765. TBL_PC *sd=NULL;
  10766. int num;
  10767. const char *name;
  10768. char prefix;
  10769. int x,y,type;
  10770. char mapname[MAP_NAME_LENGTH];
  10771. if( !data_isreference(script_getdata(st,2)) ){
  10772. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10773. script_pushint(st,-1);
  10774. return 1;
  10775. }
  10776. if( !data_isreference(script_getdata(st,3)) ){
  10777. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10778. script_pushint(st,-1);
  10779. return 1;
  10780. }
  10781. if( !data_isreference(script_getdata(st,4)) ){
  10782. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10783. script_pushint(st,-1);
  10784. return 1;
  10785. }
  10786. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10787. type=script_getnum(st,5);
  10788. switch (type){
  10789. case 0: //Get Character Position
  10790. if( script_hasdata(st,6) )
  10791. sd=map_nick2sd(script_getstr(st,6));
  10792. else
  10793. sd=script_rid2sd(st);
  10794. if (sd)
  10795. bl = &sd->bl;
  10796. break;
  10797. case 1: //Get NPC Position
  10798. if( script_hasdata(st,6) )
  10799. {
  10800. struct npc_data *nd;
  10801. nd=npc_name2id(script_getstr(st,6));
  10802. if (nd)
  10803. bl = &nd->bl;
  10804. } else //In case the origin is not an npc?
  10805. bl=map_id2bl(st->oid);
  10806. break;
  10807. case 2: //Get Pet Position
  10808. if(script_hasdata(st,6))
  10809. sd=map_nick2sd(script_getstr(st,6));
  10810. else
  10811. sd=script_rid2sd(st);
  10812. if (sd && sd->pd)
  10813. bl = &sd->pd->bl;
  10814. break;
  10815. case 3: //Get Mob Position
  10816. break; //Not supported?
  10817. case 4: //Get Homun Position
  10818. if(script_hasdata(st,6))
  10819. sd=map_nick2sd(script_getstr(st,6));
  10820. else
  10821. sd=script_rid2sd(st);
  10822. if (sd && sd->hd)
  10823. bl = &sd->hd->bl;
  10824. break;
  10825. default:
  10826. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  10827. script_pushint(st,-1);
  10828. return 1;
  10829. }
  10830. if (!bl) { //No object found.
  10831. script_pushint(st,-1);
  10832. return 0;
  10833. }
  10834. x= bl->x;
  10835. y= bl->y;
  10836. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10837. //Set MapName$
  10838. num=st->stack->stack_data[st->start+2].u.num;
  10839. name=get_str(num&0x00ffffff);
  10840. prefix=*name;
  10841. if(not_server_variable(prefix))
  10842. sd=script_rid2sd(st);
  10843. else
  10844. sd=NULL;
  10845. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10846. //Set MapX
  10847. num=st->stack->stack_data[st->start+3].u.num;
  10848. name=get_str(num&0x00ffffff);
  10849. prefix=*name;
  10850. if(not_server_variable(prefix))
  10851. sd=script_rid2sd(st);
  10852. else
  10853. sd=NULL;
  10854. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  10855. //Set MapY
  10856. num=st->stack->stack_data[st->start+4].u.num;
  10857. name=get_str(num&0x00ffffff);
  10858. prefix=*name;
  10859. if(not_server_variable(prefix))
  10860. sd=script_rid2sd(st);
  10861. else
  10862. sd=NULL;
  10863. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  10864. //Return Success value
  10865. script_pushint(st,0);
  10866. return 0;
  10867. }
  10868. /*==========================================
  10869. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10870. *------------------------------------------*/
  10871. BUILDIN_FUNC(logmes)
  10872. {
  10873. const char *str;
  10874. TBL_PC* sd;
  10875. sd = script_rid2sd(st);
  10876. if( sd == NULL )
  10877. return 1;
  10878. str = script_getstr(st,2);
  10879. log_npc(sd,str);
  10880. return 0;
  10881. }
  10882. BUILDIN_FUNC(summon)
  10883. {
  10884. int _class, timeout=0;
  10885. const char *str,*event="";
  10886. TBL_PC *sd;
  10887. struct mob_data *md;
  10888. int tick = gettick();
  10889. sd=script_rid2sd(st);
  10890. if (!sd) return 0;
  10891. str =script_getstr(st,2);
  10892. _class=script_getnum(st,3);
  10893. if( script_hasdata(st,4) )
  10894. timeout=script_getnum(st,4);
  10895. if( script_hasdata(st,5) ){
  10896. event=script_getstr(st,5);
  10897. check_event(st, event);
  10898. }
  10899. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10900. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10901. if (md) {
  10902. md->master_id=sd->bl.id;
  10903. md->special_state.ai=1;
  10904. if( md->deletetimer != INVALID_TIMER )
  10905. delete_timer(md->deletetimer, mob_timer_delete);
  10906. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10907. mob_spawn (md); //Now it is ready for spawning.
  10908. clif_specialeffect(&md->bl,344,AREA);
  10909. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10910. }
  10911. return 0;
  10912. }
  10913. /*==========================================
  10914. * Checks whether it is daytime/nighttime
  10915. *------------------------------------------*/
  10916. BUILDIN_FUNC(isnight)
  10917. {
  10918. script_pushint(st,(night_flag == 1));
  10919. return 0;
  10920. }
  10921. BUILDIN_FUNC(isday)
  10922. {
  10923. script_pushint(st,(night_flag == 0));
  10924. return 0;
  10925. }
  10926. /*================================================
  10927. * Check how many items/cards in the list are
  10928. * equipped - used for 2/15's cards patch [celest]
  10929. *------------------------------------------------*/
  10930. BUILDIN_FUNC(isequippedcnt)
  10931. {
  10932. TBL_PC *sd;
  10933. int i, j, k, id = 1;
  10934. int ret = 0;
  10935. sd = script_rid2sd(st);
  10936. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10937. script_pushint(st,0);
  10938. return 0;
  10939. }
  10940. for (i=0; id!=0; i++) {
  10941. FETCH (i+2, id) else id = 0;
  10942. if (id <= 0)
  10943. continue;
  10944. for (j=0; j<EQI_MAX; j++) {
  10945. int index;
  10946. index = sd->equip_index[j];
  10947. if(index < 0) continue;
  10948. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10949. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10950. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10951. if(!sd->inventory_data[index])
  10952. continue;
  10953. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10954. if (sd->inventory_data[index]->nameid == id)
  10955. ret+= sd->status.inventory[index].amount;
  10956. } else { //Count cards.
  10957. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10958. continue; //No cards
  10959. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10960. if (sd->status.inventory[index].card[k] == id)
  10961. ret++; //[Lupus]
  10962. }
  10963. }
  10964. }
  10965. }
  10966. script_pushint(st,ret);
  10967. return 0;
  10968. }
  10969. /*================================================
  10970. * Check whether another card has been
  10971. * equipped - used for 2/15's cards patch [celest]
  10972. * -- Items checked cannot be reused in another
  10973. * card set to prevent exploits
  10974. *------------------------------------------------*/
  10975. BUILDIN_FUNC(isequipped)
  10976. {
  10977. TBL_PC *sd;
  10978. int i, j, k, id = 1;
  10979. int index, flag;
  10980. int ret = -1;
  10981. //Original hash to reverse it when full check fails.
  10982. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10983. sd = script_rid2sd(st);
  10984. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10985. script_pushint(st,0);
  10986. return 0;
  10987. }
  10988. setitem_hash = sd->setitem_hash;
  10989. setitem_hash2 = sd->setitem_hash2;
  10990. for (i=0; id!=0; i++)
  10991. {
  10992. FETCH (i+2, id) else id = 0;
  10993. if (id <= 0)
  10994. continue;
  10995. flag = 0;
  10996. for (j=0; j<EQI_MAX; j++)
  10997. {
  10998. index = sd->equip_index[j];
  10999. if(index < 0) continue;
  11000. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11001. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11002. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11003. if(!sd->inventory_data[index])
  11004. continue;
  11005. if (itemdb_type(id) != IT_CARD) {
  11006. if (sd->inventory_data[index]->nameid != id)
  11007. continue;
  11008. flag = 1;
  11009. break;
  11010. } else { //Cards
  11011. if (sd->inventory_data[index]->slot == 0 ||
  11012. itemdb_isspecial(sd->status.inventory[index].card[0]))
  11013. continue;
  11014. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  11015. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  11016. unsigned int hash = 0;
  11017. if (sd->status.inventory[index].card[k] != id)
  11018. continue;
  11019. hash = 1<<((j<5?j:j-5)*4 + k);
  11020. // check if card is already used by another set
  11021. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  11022. continue;
  11023. // We have found a match
  11024. flag = 1;
  11025. // Set hash so this card cannot be used by another
  11026. if (j<5)
  11027. sd->setitem_hash |= hash;
  11028. else
  11029. sd->setitem_hash2 |= hash;
  11030. break;
  11031. }
  11032. }
  11033. if (flag) break; //Card found
  11034. }
  11035. if (ret == -1)
  11036. ret = flag;
  11037. else
  11038. ret &= flag;
  11039. if (!ret) break;
  11040. }
  11041. if (!ret)
  11042. { //When check fails, restore original hash values. [Skotlex]
  11043. sd->setitem_hash = setitem_hash;
  11044. sd->setitem_hash2 = setitem_hash2;
  11045. }
  11046. script_pushint(st,ret);
  11047. return 0;
  11048. }
  11049. /*================================================
  11050. * Check how many given inserted cards in the CURRENT
  11051. * weapon - used for 2/15's cards patch [Lupus]
  11052. *------------------------------------------------*/
  11053. BUILDIN_FUNC(cardscnt)
  11054. {
  11055. TBL_PC *sd;
  11056. int i, k, id = 1;
  11057. int ret = 0;
  11058. int index;
  11059. sd = script_rid2sd(st);
  11060. for (i=0; id!=0; i++) {
  11061. FETCH (i+2, id) else id = 0;
  11062. if (id <= 0)
  11063. continue;
  11064. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  11065. if(index < 0) continue;
  11066. if(!sd->inventory_data[index])
  11067. continue;
  11068. if(itemdb_type(id) != IT_CARD) {
  11069. if (sd->inventory_data[index]->nameid == id)
  11070. ret+= sd->status.inventory[index].amount;
  11071. } else {
  11072. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11073. continue;
  11074. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11075. if (sd->status.inventory[index].card[k] == id)
  11076. ret++;
  11077. }
  11078. }
  11079. }
  11080. script_pushint(st,ret);
  11081. // script_pushint(st,current_equip_item_index);
  11082. return 0;
  11083. }
  11084. /*=======================================================
  11085. * Returns the refined number of the current item, or an
  11086. * item with inventory index specified
  11087. *-------------------------------------------------------*/
  11088. BUILDIN_FUNC(getrefine)
  11089. {
  11090. TBL_PC *sd;
  11091. if ((sd = script_rid2sd(st))!= NULL)
  11092. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  11093. else
  11094. script_pushint(st,0);
  11095. return 0;
  11096. }
  11097. /*=======================================================
  11098. * Day/Night controls
  11099. *-------------------------------------------------------*/
  11100. BUILDIN_FUNC(night)
  11101. {
  11102. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  11103. return 0;
  11104. }
  11105. BUILDIN_FUNC(day)
  11106. {
  11107. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  11108. return 0;
  11109. }
  11110. //=======================================================
  11111. // Unequip [Spectre]
  11112. //-------------------------------------------------------
  11113. BUILDIN_FUNC(unequip)
  11114. {
  11115. int i;
  11116. size_t num;
  11117. TBL_PC *sd;
  11118. num = script_getnum(st,2);
  11119. sd = script_rid2sd(st);
  11120. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  11121. {
  11122. i = pc_checkequip(sd,equip[num-1]);
  11123. if (i >= 0)
  11124. pc_unequipitem(sd,i,1|2);
  11125. }
  11126. return 0;
  11127. }
  11128. BUILDIN_FUNC(equip)
  11129. {
  11130. int nameid=0,i;
  11131. TBL_PC *sd;
  11132. struct item_data *item_data;
  11133. sd = script_rid2sd(st);
  11134. nameid=script_getnum(st,2);
  11135. if((item_data = itemdb_exists(nameid)) == NULL)
  11136. {
  11137. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  11138. return 1;
  11139. }
  11140. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  11141. if( i < MAX_INVENTORY )
  11142. pc_equipitem(sd,i,item_data->equip);
  11143. return 0;
  11144. }
  11145. BUILDIN_FUNC(autoequip)
  11146. {
  11147. int nameid, flag;
  11148. struct item_data *item_data;
  11149. nameid=script_getnum(st,2);
  11150. flag=script_getnum(st,3);
  11151. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  11152. {
  11153. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  11154. return 1;
  11155. }
  11156. if( !itemdb_isequip2(item_data) )
  11157. {
  11158. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  11159. return 1;
  11160. }
  11161. item_data->flag.autoequip = flag>0?1:0;
  11162. return 0;
  11163. }
  11164. BUILDIN_FUNC(setbattleflag)
  11165. {
  11166. const char *flag, *value;
  11167. flag = script_getstr(st,2);
  11168. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  11169. if (battle_set_value(flag, value) == 0)
  11170. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  11171. else
  11172. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  11173. return 0;
  11174. }
  11175. BUILDIN_FUNC(getbattleflag)
  11176. {
  11177. const char *flag;
  11178. flag = script_getstr(st,2);
  11179. script_pushint(st,battle_get_value(flag));
  11180. return 0;
  11181. }
  11182. //=======================================================
  11183. // strlen [Valaris]
  11184. //-------------------------------------------------------
  11185. BUILDIN_FUNC(getstrlen)
  11186. {
  11187. const char *str = script_getstr(st,2);
  11188. int len = (str) ? (int)strlen(str) : 0;
  11189. script_pushint(st,len);
  11190. return 0;
  11191. }
  11192. //=======================================================
  11193. // isalpha [Valaris]
  11194. //-------------------------------------------------------
  11195. BUILDIN_FUNC(charisalpha)
  11196. {
  11197. const char *str=script_getstr(st,2);
  11198. int pos=script_getnum(st,3);
  11199. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  11200. script_pushint(st,val);
  11201. return 0;
  11202. }
  11203. //=======================================================
  11204. // charisupper <str>, <index>
  11205. //-------------------------------------------------------
  11206. BUILDIN_FUNC(charisupper)
  11207. {
  11208. const char *str = script_getstr(st,2);
  11209. int pos = script_getnum(st,3);
  11210. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  11211. script_pushint(st,val);
  11212. return 0;
  11213. }
  11214. //=======================================================
  11215. // charislower <str>, <index>
  11216. //-------------------------------------------------------
  11217. BUILDIN_FUNC(charislower)
  11218. {
  11219. const char *str = script_getstr(st,2);
  11220. int pos = script_getnum(st,3);
  11221. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  11222. script_pushint(st,val);
  11223. return 0;
  11224. }
  11225. //=======================================================
  11226. // charat <str>, <index>
  11227. //-------------------------------------------------------
  11228. BUILDIN_FUNC(charat)
  11229. {
  11230. const char *str = script_getstr(st,2);
  11231. int pos = script_getnum(st,3);
  11232. char *output;
  11233. output = (char*)aMalloc(2*sizeof(char));
  11234. output[0] = '\0';
  11235. if(str && pos >= 0 && (unsigned int)pos < strlen(str))
  11236. sprintf(output, "%c", str[pos]);
  11237. script_pushstr(st, output);
  11238. return 0;
  11239. }
  11240. //=======================================================
  11241. // setchar <string>, <char>, <index>
  11242. //-------------------------------------------------------
  11243. BUILDIN_FUNC(setchar)
  11244. {
  11245. const char *str = script_getstr(st,2);
  11246. const char *c = script_getstr(st,3);
  11247. int index = script_getnum(st,4);
  11248. char *output = aStrdup(str);
  11249. if(index >= 0 && index < strlen(output))
  11250. output[index] = *c;
  11251. script_pushstr(st, output);
  11252. return 0;
  11253. }
  11254. //=======================================================
  11255. // insertchar <string>, <char>, <index>
  11256. //-------------------------------------------------------
  11257. BUILDIN_FUNC(insertchar)
  11258. {
  11259. const char *str = script_getstr(st,2);
  11260. const char *c = script_getstr(st,3);
  11261. int index = script_getnum(st,4);
  11262. char *output;
  11263. size_t len = strlen(str);
  11264. if(index < 0)
  11265. index = 0;
  11266. else if(index > len)
  11267. index = len;
  11268. output = (char*)aMalloc(len + 2);
  11269. memcpy(output, str, index);
  11270. output[index] = c[0];
  11271. memcpy(&output[index+1], &str[index], len - index);
  11272. output[len+1] = '\0';
  11273. script_pushstr(st, output);
  11274. return 0;
  11275. }
  11276. //=======================================================
  11277. // delchar <string>, <index>
  11278. //-------------------------------------------------------
  11279. BUILDIN_FUNC(delchar)
  11280. {
  11281. const char *str = script_getstr(st,2);
  11282. int index = script_getnum(st,3);
  11283. char *output;
  11284. size_t len = strlen(str);
  11285. if(index < 0 || index > len) {
  11286. //return original
  11287. output = aStrdup(str);
  11288. script_pushstr(st, output);
  11289. return 0;
  11290. }
  11291. output = (char*)aMalloc(len);
  11292. memcpy(output, str, index);
  11293. memcpy(&output[index], &str[index+1], len - index);
  11294. script_pushstr(st, output);
  11295. return 0;
  11296. }
  11297. //=======================================================
  11298. // strtoupper <str>
  11299. //-------------------------------------------------------
  11300. BUILDIN_FUNC(strtoupper)
  11301. {
  11302. const char *str = script_getstr(st,2);
  11303. char *output = aStrdup(str);
  11304. char *cursor = output;
  11305. while (*cursor != '\0') {
  11306. *cursor = TOUPPER(*cursor);
  11307. cursor++;
  11308. }
  11309. script_pushstr(st, output);
  11310. return 0;
  11311. }
  11312. //=======================================================
  11313. // strtolower <str>
  11314. //-------------------------------------------------------
  11315. BUILDIN_FUNC(strtolower)
  11316. {
  11317. const char *str = script_getstr(st,2);
  11318. char *output = aStrdup(str);
  11319. char *cursor = output;
  11320. while (*cursor != '\0') {
  11321. *cursor = TOLOWER(*cursor);
  11322. cursor++;
  11323. }
  11324. script_pushstr(st, output);
  11325. return 0;
  11326. }
  11327. //=======================================================
  11328. // substr <str>, <start>, <end>
  11329. //-------------------------------------------------------
  11330. BUILDIN_FUNC(substr)
  11331. {
  11332. const char *str = script_getstr(st,2);
  11333. char *output;
  11334. int start = script_getnum(st,3);
  11335. int end = script_getnum(st,4);
  11336. int len = 0;
  11337. if(start >= 0 && end < strlen(str) && start <= end) {
  11338. len = end - start + 1;
  11339. output = (char*)aMalloc(len + 1);
  11340. memcpy(output, &str[start], len);
  11341. } else
  11342. output = (char*)aMalloc(1);
  11343. output[len] = '\0';
  11344. script_pushstr(st, output);
  11345. return 0;
  11346. }
  11347. //=======================================================
  11348. // explode <dest_string_array>, <str>, <delimiter>
  11349. // Note: delimiter is limited to 1 char
  11350. //-------------------------------------------------------
  11351. BUILDIN_FUNC(explode)
  11352. {
  11353. struct script_data* data = script_getdata(st, 2);
  11354. const char *str = script_getstr(st,3);
  11355. const char delimiter = script_getstr(st, 4)[0];
  11356. int32 id;
  11357. size_t len = strlen(str);
  11358. int i = 0, j = 0;
  11359. int start;
  11360. char *temp;
  11361. const char* name;
  11362. TBL_PC* sd = NULL;
  11363. temp = (char*)aMalloc(len + 1);
  11364. if( !data_isreference(data) )
  11365. {
  11366. ShowError("script:explode: not a variable\n");
  11367. script_reportdata(data);
  11368. st->state = END;
  11369. return 1;// not a variable
  11370. }
  11371. id = reference_getid(data);
  11372. start = reference_getindex(data);
  11373. name = reference_getname(data);
  11374. if( not_array_variable(*name) )
  11375. {
  11376. ShowError("script:explode: illegal scope\n");
  11377. script_reportdata(data);
  11378. st->state = END;
  11379. return 1;// not supported
  11380. }
  11381. if( !is_string_variable(name) )
  11382. {
  11383. ShowError("script:explode: not string array\n");
  11384. script_reportdata(data);
  11385. st->state = END;
  11386. return 1;// data type mismatch
  11387. }
  11388. if( not_server_variable(*name) )
  11389. {
  11390. sd = script_rid2sd(st);
  11391. if( sd == NULL )
  11392. return 0;// no player attached
  11393. }
  11394. while(str[i] != '\0') {
  11395. if(str[i] == delimiter && start < 127) { //break at delimiter but ignore after reaching last array index
  11396. temp[j] = '\0';
  11397. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  11398. j = 0;
  11399. ++i;
  11400. } else {
  11401. temp[j++] = str[i++];
  11402. }
  11403. }
  11404. //set last string
  11405. temp[j] = '\0';
  11406. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  11407. aFree(temp);
  11408. return 0;
  11409. }
  11410. //=======================================================
  11411. // implode <string_array>
  11412. // implode <string_array>, <glue>
  11413. //-------------------------------------------------------
  11414. BUILDIN_FUNC(implode)
  11415. {
  11416. struct script_data* data = script_getdata(st, 2);
  11417. const char *glue = NULL, *name, *temp;
  11418. int32 glue_len = 0, array_size, id;
  11419. size_t len = 0;
  11420. int i, k = 0;
  11421. TBL_PC* sd = NULL;
  11422. char *output;
  11423. if( !data_isreference(data) )
  11424. {
  11425. ShowError("script:implode: not a variable\n");
  11426. script_reportdata(data);
  11427. st->state = END;
  11428. return 1;// not a variable
  11429. }
  11430. id = reference_getid(data);
  11431. name = reference_getname(data);
  11432. if( not_array_variable(*name) )
  11433. {
  11434. ShowError("script:implode: illegal scope\n");
  11435. script_reportdata(data);
  11436. st->state = END;
  11437. return 1;// not supported
  11438. }
  11439. if( !is_string_variable(name) )
  11440. {
  11441. ShowError("script:implode: not string array\n");
  11442. script_reportdata(data);
  11443. st->state = END;
  11444. return 1;// data type mismatch
  11445. }
  11446. if( not_server_variable(*name) )
  11447. {
  11448. sd = script_rid2sd(st);
  11449. if( sd == NULL )
  11450. return 0;// no player attached
  11451. }
  11452. //count chars
  11453. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  11454. if(array_size == -1) //empty array check (AmsTaff)
  11455. {
  11456. ShowWarning("script:implode: array length = 0\n");
  11457. output = (char*)aMalloc(sizeof(char)*5);
  11458. sprintf(output,"%s","NULL");
  11459. } else {
  11460. for(i = 0; i <= array_size; ++i) {
  11461. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11462. len += strlen(temp);
  11463. script_removetop(st, -1, 0);
  11464. }
  11465. //allocate mem
  11466. if( script_hasdata(st,3) ) {
  11467. glue = script_getstr(st,3);
  11468. glue_len = strlen(glue);
  11469. len += glue_len * (array_size);
  11470. }
  11471. output = (char*)aMalloc(len + 1);
  11472. //build output
  11473. for(i = 0; i < array_size; ++i) {
  11474. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11475. len = strlen(temp);
  11476. memcpy(&output[k], temp, len);
  11477. k += len;
  11478. if(glue_len != 0) {
  11479. memcpy(&output[k], glue, glue_len);
  11480. k += glue_len;
  11481. }
  11482. script_removetop(st, -1, 0);
  11483. }
  11484. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  11485. len = strlen(temp);
  11486. memcpy(&output[k], temp, len);
  11487. k += len;
  11488. script_removetop(st, -1, 0);
  11489. output[k] = '\0';
  11490. }
  11491. script_pushstr(st, output);
  11492. return 0;
  11493. }
  11494. //=======================================================
  11495. // sprintf(<format>, ...);
  11496. // Implements C sprintf, except format %n. The resulting string is
  11497. // returned, instead of being saved in variable by reference.
  11498. //-------------------------------------------------------
  11499. BUILDIN_FUNC(sprintf)
  11500. {
  11501. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  11502. const char* format;
  11503. char* p;
  11504. char* q;
  11505. char* buf = NULL;
  11506. char* buf2 = NULL;
  11507. struct script_data* data;
  11508. StringBuf final_buf;
  11509. // Fetch init data
  11510. format = script_getstr(st, 2);
  11511. argc = script_lastdata(st)-2;
  11512. len = strlen(format);
  11513. // Skip parsing, where no parsing is required.
  11514. if(len==0){
  11515. script_pushconststr(st,"");
  11516. return 0;
  11517. }
  11518. // Pessimistic alloc
  11519. CREATE(buf, char, len+1);
  11520. // Need not be parsed, just solve stuff like %%.
  11521. if(argc==0){
  11522. memcpy(buf,format,len+1);
  11523. script_pushstrcopy(st, buf);
  11524. aFree(buf);
  11525. return 0;
  11526. }
  11527. safestrncpy(buf, format, len+1);
  11528. // Issue sprintf for each parameter
  11529. StringBuf_Init(&final_buf);
  11530. q = buf;
  11531. while((p = strchr(q, '%'))!=NULL){
  11532. if(p!=q){
  11533. len = p-q+1;
  11534. if(buf2_len<len){
  11535. RECREATE(buf2, char, len);
  11536. buf2_len = len;
  11537. }
  11538. safestrncpy(buf2, q, len);
  11539. StringBuf_AppendStr(&final_buf, buf2);
  11540. q = p;
  11541. }
  11542. p = q+1;
  11543. if(*p=='%'){ // %%
  11544. StringBuf_AppendStr(&final_buf, "%");
  11545. q+=2;
  11546. continue;
  11547. }
  11548. if(*p=='n'){ // %n
  11549. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  11550. script_reportsrc(st);
  11551. q+=2;
  11552. continue;
  11553. }
  11554. if(arg>=argc){
  11555. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  11556. if(buf) aFree(buf);
  11557. if(buf2) aFree(buf2);
  11558. StringBuf_Destroy(&final_buf);
  11559. script_pushconststr(st,"");
  11560. return 1;
  11561. }
  11562. if((p = strchr(q+1, '%'))==NULL){
  11563. p = strchr(q, 0); // EOS
  11564. }
  11565. len = p-q+1;
  11566. if(buf2_len<len){
  11567. RECREATE(buf2, char, len);
  11568. buf2_len = len;
  11569. }
  11570. safestrncpy(buf2, q, len);
  11571. q = p;
  11572. // Note: This assumes the passed value being the correct
  11573. // type to the current format specifier. If not, the server
  11574. // probably crashes or returns anything else, than expected,
  11575. // but it would behave in normal code the same way so it's
  11576. // the scripter's responsibility.
  11577. data = script_getdata(st, arg+3);
  11578. if(data_isstring(data)){ // String
  11579. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11580. }else if(data_isint(data)){ // Number
  11581. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11582. }else if(data_isreference(data)){ // Variable
  11583. char* name = reference_getname(data);
  11584. if(name[strlen(name)-1]=='$'){ // var Str
  11585. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11586. }else{ // var Int
  11587. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11588. }
  11589. }else{ // Unsupported type
  11590. ShowError("buildin_sprintf: Unknown argument type!\n");
  11591. if(buf) aFree(buf);
  11592. if(buf2) aFree(buf2);
  11593. StringBuf_Destroy(&final_buf);
  11594. script_pushconststr(st,"");
  11595. return 1;
  11596. }
  11597. arg++;
  11598. }
  11599. // Append anything left
  11600. if(*q){
  11601. StringBuf_AppendStr(&final_buf, q);
  11602. }
  11603. // Passed more, than needed
  11604. if(arg<argc){
  11605. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  11606. script_reportsrc(st);
  11607. }
  11608. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  11609. if(buf) aFree(buf);
  11610. if(buf2) aFree(buf2);
  11611. StringBuf_Destroy(&final_buf);
  11612. return 0;
  11613. }
  11614. //=======================================================
  11615. // sscanf(<str>, <format>, ...);
  11616. // Implements C sscanf.
  11617. //-------------------------------------------------------
  11618. BUILDIN_FUNC(sscanf){
  11619. unsigned int argc, arg = 0, len;
  11620. struct script_data* data;
  11621. struct map_session_data* sd = NULL;
  11622. const char* str;
  11623. const char* format;
  11624. const char* p;
  11625. const char* q;
  11626. char* buf = NULL;
  11627. char* buf_p;
  11628. char* ref_str = NULL;
  11629. int ref_int;
  11630. // Get data
  11631. str = script_getstr(st, 2);
  11632. format = script_getstr(st, 3);
  11633. argc = script_lastdata(st)-3;
  11634. len = strlen(format);
  11635. CREATE(buf, char, len*2+1);
  11636. // Issue sscanf for each parameter
  11637. *buf = 0;
  11638. q = format;
  11639. while((p = strchr(q, '%'))){
  11640. if(p!=q){
  11641. strncat(buf, q, (size_t)(p-q));
  11642. q = p;
  11643. }
  11644. p = q+1;
  11645. if(*p=='*' || *p=='%'){ // Skip
  11646. strncat(buf, q, 2);
  11647. q+=2;
  11648. continue;
  11649. }
  11650. if(arg>=argc){
  11651. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  11652. script_pushint(st, -1);
  11653. if(buf) aFree(buf);
  11654. if(ref_str) aFree(ref_str);
  11655. return 1;
  11656. }
  11657. if((p = strchr(q+1, '%'))==NULL){
  11658. p = strchr(q, 0); // EOS
  11659. }
  11660. len = p-q;
  11661. strncat(buf, q, len);
  11662. q = p;
  11663. // Validate output
  11664. data = script_getdata(st, arg+4);
  11665. if(!data_isreference(data) || !reference_tovariable(data)){
  11666. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  11667. script_pushint(st, -1);
  11668. if(buf) aFree(buf);
  11669. if(ref_str) aFree(ref_str);
  11670. return 1;
  11671. }
  11672. buf_p = reference_getname(data);
  11673. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  11674. script_pushint(st, -1);
  11675. if(buf) aFree(buf);
  11676. if(ref_str) aFree(ref_str);
  11677. return 0;
  11678. }
  11679. // Save value if any
  11680. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  11681. if(ref_str==NULL){
  11682. CREATE(ref_str, char, strlen(str)+1);
  11683. }
  11684. if(sscanf(str, buf, ref_str)==0){
  11685. break;
  11686. }
  11687. set_reg(st, sd, add_str(buf_p), buf_p, (void *)(ref_str), reference_getref(data));
  11688. }else{ // Number
  11689. if(sscanf(str, buf, &ref_int)==0){
  11690. break;
  11691. }
  11692. set_reg(st, sd, add_str(buf_p), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  11693. }
  11694. arg++;
  11695. // Disable used format (%... -> %*...)
  11696. buf_p = strchr(buf, 0);
  11697. memmove(buf_p-len+2, buf_p-len+1, len);
  11698. *(buf_p-len+1) = '*';
  11699. }
  11700. // Passed more, than needed
  11701. if(arg<argc){
  11702. ShowWarning("buildin_sscanf: Unused arguments passed.\n");
  11703. script_reportsrc(st);
  11704. }
  11705. script_pushint(st, arg);
  11706. if(buf) aFree(buf);
  11707. if(ref_str) aFree(ref_str);
  11708. return 0;
  11709. }
  11710. //=======================================================
  11711. // strpos(<haystack>, <needle>)
  11712. // strpos(<haystack>, <needle>, <offset>)
  11713. //
  11714. // Implements PHP style strpos. Adapted from code from
  11715. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  11716. //-------------------------------------------------------
  11717. BUILDIN_FUNC(strpos) {
  11718. const char *haystack = script_getstr(st,2);
  11719. const char *needle = script_getstr(st,3);
  11720. int i;
  11721. size_t len;
  11722. if( script_hasdata(st,4) )
  11723. i = script_getnum(st,4);
  11724. else
  11725. i = 0;
  11726. if ( strlen(needle) == 0 ) {
  11727. script_pushint(st, -1);
  11728. return 0;
  11729. }
  11730. len = strlen(haystack);
  11731. for ( ; i < len; ++i ) {
  11732. if ( haystack[i] == *needle ) {
  11733. // matched starting char -- loop through remaining chars
  11734. const char *h, *n;
  11735. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  11736. if ( *h != *n ) {
  11737. break;
  11738. }
  11739. }
  11740. if ( !*n ) { // matched all of 'needle' to null termination
  11741. script_pushint(st, i);
  11742. return 0;
  11743. }
  11744. }
  11745. }
  11746. script_pushint(st, -1);
  11747. return 0;
  11748. }
  11749. //===============================================================
  11750. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  11751. //
  11752. // Note: Finds all instances of <search> in <input> and replaces
  11753. // with <replace>. If specified will only replace as many
  11754. // instances as specified in <count>. By default will be case
  11755. // sensitive.
  11756. //---------------------------------------------------------------
  11757. BUILDIN_FUNC(replacestr)
  11758. {
  11759. const char *input = script_getstr(st, 2);
  11760. const char *find = script_getstr(st, 3);
  11761. const char *replace = script_getstr(st, 4);
  11762. size_t inputlen = strlen(input);
  11763. size_t findlen = strlen(find);
  11764. struct StringBuf output;
  11765. bool usecase = true;
  11766. int count = 0;
  11767. int numFinds = 0;
  11768. int i = 0, f = 0;
  11769. if(findlen == 0) {
  11770. ShowError("script:replacestr: Invalid search length.\n");
  11771. st->state = END;
  11772. return 1;
  11773. }
  11774. if(script_hasdata(st, 5)) {
  11775. if(script_isint(st,5))
  11776. usecase = script_getnum(st, 5) != 0;
  11777. else {
  11778. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  11779. st->state = END;
  11780. return 1;
  11781. }
  11782. }
  11783. if(script_hasdata(st, 6)) {
  11784. if(script_isint(st,6))
  11785. count = script_getnum(st, 6);
  11786. else {
  11787. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  11788. st->state = END;
  11789. return 1;
  11790. }
  11791. }
  11792. StringBuf_Init(&output);
  11793. for(; i < inputlen; i++) {
  11794. if(count && count == numFinds) { //found enough, stop looking
  11795. break;
  11796. }
  11797. for(f = 0; f <= findlen; f++) {
  11798. if(f == findlen) { //complete match
  11799. numFinds++;
  11800. StringBuf_AppendStr(&output, replace);
  11801. i += findlen - 1;
  11802. break;
  11803. } else {
  11804. if(usecase) {
  11805. if((i + f) > inputlen || input[i + f] != find[f]) {
  11806. StringBuf_Printf(&output, "%c", input[i]);
  11807. break;
  11808. }
  11809. } else {
  11810. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11811. StringBuf_Printf(&output, "%c", input[i]);
  11812. break;
  11813. }
  11814. }
  11815. }
  11816. }
  11817. }
  11818. //append excess after enough found
  11819. if(i < inputlen)
  11820. StringBuf_AppendStr(&output, &(input[i]));
  11821. script_pushstrcopy(st, StringBuf_Value(&output));
  11822. StringBuf_Destroy(&output);
  11823. return 0;
  11824. }
  11825. //========================================================
  11826. // countstr <input>, <search>{, <usecase>}
  11827. //
  11828. // Note: Counts the number of times <search> occurs in
  11829. // <input>. By default will be case sensitive.
  11830. //--------------------------------------------------------
  11831. BUILDIN_FUNC(countstr)
  11832. {
  11833. const char *input = script_getstr(st, 2);
  11834. const char *find = script_getstr(st, 3);
  11835. size_t inputlen = strlen(input);
  11836. size_t findlen = strlen(find);
  11837. bool usecase = true;
  11838. int numFinds = 0;
  11839. int i = 0, f = 0;
  11840. if(findlen == 0) {
  11841. ShowError("script:countstr: Invalid search length.\n");
  11842. st->state = END;
  11843. return 1;
  11844. }
  11845. if(script_hasdata(st, 4)) {
  11846. if(script_isint(st,4))
  11847. usecase = script_getnum(st, 4) != 0;
  11848. else {
  11849. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  11850. st->state = END;
  11851. return 1;
  11852. }
  11853. }
  11854. for(; i < inputlen; i++) {
  11855. for(f = 0; f <= findlen; f++) {
  11856. if(f == findlen) { //complete match
  11857. numFinds++;
  11858. i += findlen - 1;
  11859. break;
  11860. } else {
  11861. if(usecase) {
  11862. if((i + f) > inputlen || input[i + f] != find[f]) {
  11863. break;
  11864. }
  11865. } else {
  11866. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11867. break;
  11868. }
  11869. }
  11870. }
  11871. }
  11872. }
  11873. script_pushint(st, numFinds);
  11874. return 0;
  11875. }
  11876. /// Changes the display name and/or display class of the npc.
  11877. /// Returns 0 is successful, 1 if the npc does not exist.
  11878. ///
  11879. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  11880. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  11881. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  11882. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  11883. BUILDIN_FUNC(setnpcdisplay)
  11884. {
  11885. const char* name;
  11886. const char* newname = NULL;
  11887. int class_ = -1, size = -1;
  11888. struct script_data* data;
  11889. struct npc_data* nd;
  11890. name = script_getstr(st,2);
  11891. data = script_getdata(st,3);
  11892. if( script_hasdata(st,4) )
  11893. class_ = script_getnum(st,4);
  11894. if( script_hasdata(st,5) )
  11895. size = script_getnum(st,5);
  11896. get_val(st, data);
  11897. if( data_isstring(data) )
  11898. newname = conv_str(st,data);
  11899. else if( data_isint(data) )
  11900. class_ = conv_num(st,data);
  11901. else
  11902. {
  11903. ShowError("script:setnpcdisplay: expected a string or number\n");
  11904. script_reportdata(data);
  11905. return 1;
  11906. }
  11907. nd = npc_name2id(name);
  11908. if( nd == NULL )
  11909. {// not found
  11910. script_pushint(st,1);
  11911. return 0;
  11912. }
  11913. // update npc
  11914. if( newname )
  11915. npc_setdisplayname(nd, newname);
  11916. if( size != -1 && size != (int)nd->size )
  11917. nd->size = size;
  11918. else
  11919. size = -1;
  11920. if( class_ != -1 && nd->class_ != class_ )
  11921. npc_setclass(nd, class_);
  11922. else if( size != -1 )
  11923. { // Required to update the visual size
  11924. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  11925. clif_spawn(&nd->bl);
  11926. }
  11927. script_pushint(st,0);
  11928. return 0;
  11929. }
  11930. BUILDIN_FUNC(atoi)
  11931. {
  11932. const char *value;
  11933. value = script_getstr(st,2);
  11934. script_pushint(st,atoi(value));
  11935. return 0;
  11936. }
  11937. // case-insensitive substring search [lordalfa]
  11938. BUILDIN_FUNC(compare)
  11939. {
  11940. const char *message;
  11941. const char *cmpstring;
  11942. message = script_getstr(st,2);
  11943. cmpstring = script_getstr(st,3);
  11944. script_pushint(st,(stristr(message,cmpstring) != NULL));
  11945. return 0;
  11946. }
  11947. // [zBuffer] List of mathematics commands --->
  11948. BUILDIN_FUNC(sqrt)
  11949. {
  11950. double i, a;
  11951. i = script_getnum(st,2);
  11952. a = sqrt(i);
  11953. script_pushint(st,(int)a);
  11954. return 0;
  11955. }
  11956. BUILDIN_FUNC(pow)
  11957. {
  11958. double i, a, b;
  11959. a = script_getnum(st,2);
  11960. b = script_getnum(st,3);
  11961. i = pow(a,b);
  11962. script_pushint(st,(int)i);
  11963. return 0;
  11964. }
  11965. BUILDIN_FUNC(distance)
  11966. {
  11967. int x0, y0, x1, y1;
  11968. x0 = script_getnum(st,2);
  11969. y0 = script_getnum(st,3);
  11970. x1 = script_getnum(st,4);
  11971. y1 = script_getnum(st,5);
  11972. script_pushint(st,distance_xy(x0,y0,x1,y1));
  11973. return 0;
  11974. }
  11975. // <--- [zBuffer] List of mathematics commands
  11976. BUILDIN_FUNC(md5)
  11977. {
  11978. const char *tmpstr;
  11979. char *md5str;
  11980. tmpstr = script_getstr(st,2);
  11981. md5str = (char *)aMalloc((32+1)*sizeof(char));
  11982. MD5_String(tmpstr, md5str);
  11983. script_pushstr(st, md5str);
  11984. return 0;
  11985. }
  11986. // [zBuffer] List of dynamic var commands --->
  11987. BUILDIN_FUNC(setd)
  11988. {
  11989. TBL_PC *sd=NULL;
  11990. char varname[100];
  11991. const char *buffer;
  11992. int elem;
  11993. buffer = script_getstr(st, 2);
  11994. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  11995. elem = 0;
  11996. if( not_server_variable(*varname) )
  11997. {
  11998. sd = script_rid2sd(st);
  11999. if( sd == NULL )
  12000. {
  12001. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  12002. return 0;
  12003. }
  12004. }
  12005. if( is_string_variable(varname) ) {
  12006. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  12007. } else {
  12008. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  12009. }
  12010. return 0;
  12011. }
  12012. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  12013. {
  12014. int i, j;
  12015. TBL_PC* sd = NULL;
  12016. const char* query;
  12017. struct script_data* data;
  12018. const char* name;
  12019. int max_rows = SCRIPT_MAX_ARRAYSIZE;// maximum number of rows
  12020. int num_vars;
  12021. int num_cols;
  12022. // check target variables
  12023. for( i = 3; script_hasdata(st,i); ++i )
  12024. {
  12025. data = script_getdata(st, i);
  12026. if( data_isreference(data) && reference_tovariable(data) )
  12027. {// it's a variable
  12028. name = reference_getname(data);
  12029. if( not_server_variable(*name) && sd == NULL )
  12030. {// requires a player
  12031. sd = script_rid2sd(st);
  12032. if( sd == NULL )
  12033. {// no player attached
  12034. script_reportdata(data);
  12035. st->state = END;
  12036. return 1;
  12037. }
  12038. }
  12039. if( not_array_variable(*name) )
  12040. max_rows = 1;// not an array, limit to one row
  12041. }
  12042. else
  12043. {
  12044. ShowError("script:query_sql: not a variable\n");
  12045. script_reportdata(data);
  12046. st->state = END;
  12047. return 1;
  12048. }
  12049. }
  12050. num_vars = i - 3;
  12051. // Execute the query
  12052. query = script_getstr(st,2);
  12053. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  12054. {
  12055. Sql_ShowDebug(handle);
  12056. script_pushint(st, 0);
  12057. return 1;
  12058. }
  12059. if( Sql_NumRows(handle) == 0 )
  12060. {// No data received
  12061. Sql_FreeResult(handle);
  12062. script_pushint(st, 0);
  12063. return 0;
  12064. }
  12065. // Count the number of columns to store
  12066. num_cols = Sql_NumColumns(handle);
  12067. if( num_vars < num_cols )
  12068. {
  12069. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  12070. script_reportsrc(st);
  12071. }
  12072. else if( num_vars > num_cols )
  12073. {
  12074. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  12075. script_reportsrc(st);
  12076. }
  12077. // Store data
  12078. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  12079. {
  12080. for( j = 0; j < num_vars; ++j )
  12081. {
  12082. char* str = NULL;
  12083. if( j < num_cols )
  12084. Sql_GetData(handle, j, &str, NULL);
  12085. data = script_getdata(st, j+3);
  12086. name = reference_getname(data);
  12087. if( is_string_variable(name) )
  12088. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  12089. else
  12090. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  12091. }
  12092. }
  12093. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  12094. {
  12095. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  12096. script_reportsrc(st);
  12097. }
  12098. // Free data
  12099. Sql_FreeResult(handle);
  12100. script_pushint(st, i);
  12101. return 0;
  12102. }
  12103. BUILDIN_FUNC(query_sql)
  12104. {
  12105. return buildin_query_sql_sub(st, mmysql_handle);
  12106. }
  12107. BUILDIN_FUNC(query_logsql)
  12108. {
  12109. if( !log_config.sql_logs )
  12110. {// logmysql_handle == NULL
  12111. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  12112. script_pushint(st,-1);
  12113. return 1;
  12114. }
  12115. return buildin_query_sql_sub(st, logmysql_handle);
  12116. }
  12117. //Allows escaping of a given string.
  12118. BUILDIN_FUNC(escape_sql)
  12119. {
  12120. const char *str;
  12121. char *esc_str;
  12122. size_t len;
  12123. str = script_getstr(st,2);
  12124. len = strlen(str);
  12125. esc_str = (char*)aMalloc(len*2+1);
  12126. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  12127. script_pushstr(st, esc_str);
  12128. return 0;
  12129. }
  12130. BUILDIN_FUNC(getd)
  12131. {
  12132. char varname[100];
  12133. const char *buffer;
  12134. int elem;
  12135. buffer = script_getstr(st, 2);
  12136. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  12137. elem = 0;
  12138. // Push the 'pointer' so it's more flexible [Lance]
  12139. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  12140. return 0;
  12141. }
  12142. // <--- [zBuffer] List of dynamic var commands
  12143. // Pet stat [Lance]
  12144. BUILDIN_FUNC(petstat)
  12145. {
  12146. TBL_PC *sd = NULL;
  12147. struct pet_data *pd;
  12148. int flag = script_getnum(st,2);
  12149. sd = script_rid2sd(st);
  12150. if(!sd || !sd->status.pet_id || !sd->pd){
  12151. if(flag == 2)
  12152. script_pushconststr(st, "");
  12153. else
  12154. script_pushint(st,0);
  12155. return 0;
  12156. }
  12157. pd = sd->pd;
  12158. switch(flag){
  12159. case 1: script_pushint(st,(int)pd->pet.class_); break;
  12160. case 2: script_pushstrcopy(st, pd->pet.name); break;
  12161. case 3: script_pushint(st,(int)pd->pet.level); break;
  12162. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  12163. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  12164. default:
  12165. script_pushint(st,0);
  12166. break;
  12167. }
  12168. return 0;
  12169. }
  12170. BUILDIN_FUNC(callshop)
  12171. {
  12172. TBL_PC *sd = NULL;
  12173. struct npc_data *nd;
  12174. const char *shopname;
  12175. int flag = 0;
  12176. sd = script_rid2sd(st);
  12177. if (!sd) {
  12178. script_pushint(st,0);
  12179. return 0;
  12180. }
  12181. shopname = script_getstr(st, 2);
  12182. if( script_hasdata(st,3) )
  12183. flag = script_getnum(st,3);
  12184. nd = npc_name2id(shopname);
  12185. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  12186. {
  12187. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  12188. script_pushint(st,0);
  12189. return 1;
  12190. }
  12191. if( nd->subtype == SHOP )
  12192. {
  12193. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  12194. sd->state.callshop = 1;
  12195. switch( flag )
  12196. {
  12197. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  12198. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  12199. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  12200. }
  12201. }
  12202. else
  12203. clif_cashshop_show(sd, nd);
  12204. sd->npc_shopid = nd->bl.id;
  12205. script_pushint(st,1);
  12206. return 0;
  12207. }
  12208. BUILDIN_FUNC(npcshopitem)
  12209. {
  12210. const char* npcname = script_getstr(st, 2);
  12211. struct npc_data* nd = npc_name2id(npcname);
  12212. int n, i;
  12213. int amount;
  12214. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12215. { //Not found.
  12216. script_pushint(st,0);
  12217. return 0;
  12218. }
  12219. // get the count of new entries
  12220. amount = (script_lastdata(st)-2)/2;
  12221. // generate new shop item list
  12222. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  12223. for( n = 0, i = 3; n < amount; n++, i+=2 )
  12224. {
  12225. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12226. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12227. }
  12228. nd->u.shop.count = n;
  12229. script_pushint(st,1);
  12230. return 0;
  12231. }
  12232. BUILDIN_FUNC(npcshopadditem)
  12233. {
  12234. const char* npcname = script_getstr(st,2);
  12235. struct npc_data* nd = npc_name2id(npcname);
  12236. int n, i;
  12237. int amount;
  12238. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12239. { //Not found.
  12240. script_pushint(st,0);
  12241. return 0;
  12242. }
  12243. // get the count of new entries
  12244. amount = (script_lastdata(st)-2)/2;
  12245. // append new items to existing shop item list
  12246. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  12247. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  12248. {
  12249. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12250. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12251. }
  12252. nd->u.shop.count = n;
  12253. script_pushint(st,1);
  12254. return 0;
  12255. }
  12256. BUILDIN_FUNC(npcshopdelitem)
  12257. {
  12258. const char* npcname = script_getstr(st,2);
  12259. struct npc_data* nd = npc_name2id(npcname);
  12260. unsigned int nameid;
  12261. int n, i;
  12262. int amount;
  12263. int size;
  12264. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12265. { //Not found.
  12266. script_pushint(st,0);
  12267. return 0;
  12268. }
  12269. amount = script_lastdata(st)-2;
  12270. size = nd->u.shop.count;
  12271. // remove specified items from the shop item list
  12272. for( i = 3; i < 3 + amount; i++ )
  12273. {
  12274. nameid = script_getnum(st,i);
  12275. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  12276. if( n < size )
  12277. {
  12278. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  12279. size--;
  12280. }
  12281. }
  12282. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  12283. nd->u.shop.count = size;
  12284. script_pushint(st,1);
  12285. return 0;
  12286. }
  12287. //Sets a script to attach to a shop npc.
  12288. BUILDIN_FUNC(npcshopattach)
  12289. {
  12290. const char* npcname = script_getstr(st,2);
  12291. struct npc_data* nd = npc_name2id(npcname);
  12292. int flag = 1;
  12293. if( script_hasdata(st,3) )
  12294. flag = script_getnum(st,3);
  12295. if( !nd || nd->subtype != SHOP )
  12296. { //Not found.
  12297. script_pushint(st,0);
  12298. return 0;
  12299. }
  12300. if (flag)
  12301. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  12302. else
  12303. nd->master_nd = NULL;
  12304. script_pushint(st,1);
  12305. return 0;
  12306. }
  12307. /*==========================================
  12308. * Returns some values of an item [Lupus]
  12309. * Price, Weight, etc...
  12310. setitemscript(itemID,"{new item bonus script}",[n]);
  12311. Where n:
  12312. 0 - script
  12313. 1 - Equip script
  12314. 2 - Unequip script
  12315. *------------------------------------------*/
  12316. BUILDIN_FUNC(setitemscript)
  12317. {
  12318. int item_id,n=0;
  12319. const char *script;
  12320. struct item_data *i_data;
  12321. struct script_code **dstscript;
  12322. item_id = script_getnum(st,2);
  12323. script = script_getstr(st,3);
  12324. if( script_hasdata(st,4) )
  12325. n=script_getnum(st,4);
  12326. i_data = itemdb_exists(item_id);
  12327. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  12328. script_pushint(st,0);
  12329. return 0;
  12330. }
  12331. switch (n) {
  12332. case 2:
  12333. dstscript = &i_data->unequip_script;
  12334. break;
  12335. case 1:
  12336. dstscript = &i_data->equip_script;
  12337. break;
  12338. default:
  12339. dstscript = &i_data->script;
  12340. break;
  12341. }
  12342. if(*dstscript)
  12343. script_free_code(*dstscript);
  12344. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  12345. script_pushint(st,1);
  12346. return 0;
  12347. }
  12348. /* Work In Progress [Lupus]
  12349. BUILDIN_FUNC(addmonsterdrop)
  12350. {
  12351. int class_,item_id,chance;
  12352. class_=script_getnum(st,2);
  12353. item_id=script_getnum(st,3);
  12354. chance=script_getnum(st,4);
  12355. if(class_>1000 && item_id>500 && chance>0) {
  12356. script_pushint(st,1);
  12357. } else {
  12358. script_pushint(st,0);
  12359. }
  12360. }
  12361. BUILDIN_FUNC(delmonsterdrop)
  12362. {
  12363. int class_,item_id;
  12364. class_=script_getnum(st,2);
  12365. item_id=script_getnum(st,3);
  12366. if(class_>1000 && item_id>500) {
  12367. script_pushint(st,1);
  12368. } else {
  12369. script_pushint(st,0);
  12370. }
  12371. }
  12372. */
  12373. /*==========================================
  12374. * Returns some values of a monster [Lupus]
  12375. * Name, Level, race, size, etc...
  12376. getmonsterinfo(monsterID,queryIndex);
  12377. *------------------------------------------*/
  12378. BUILDIN_FUNC(getmonsterinfo)
  12379. {
  12380. struct mob_db *mob;
  12381. int mob_id;
  12382. mob_id = script_getnum(st,2);
  12383. if (!mobdb_checkid(mob_id)) {
  12384. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  12385. if ( !script_getnum(st,3) ) //requested a string
  12386. script_pushconststr(st,"null");
  12387. else
  12388. script_pushint(st,-1);
  12389. return -1;
  12390. }
  12391. mob = mob_db(mob_id);
  12392. switch ( script_getnum(st,3) ) {
  12393. case 0: script_pushstrcopy(st,mob->jname); break;
  12394. case 1: script_pushint(st,mob->lv); break;
  12395. case 2: script_pushint(st,mob->status.max_hp); break;
  12396. case 3: script_pushint(st,mob->base_exp); break;
  12397. case 4: script_pushint(st,mob->job_exp); break;
  12398. case 5: script_pushint(st,mob->status.rhw.atk); break;
  12399. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  12400. case 7: script_pushint(st,mob->status.def); break;
  12401. case 8: script_pushint(st,mob->status.mdef); break;
  12402. case 9: script_pushint(st,mob->status.str); break;
  12403. case 10: script_pushint(st,mob->status.agi); break;
  12404. case 11: script_pushint(st,mob->status.vit); break;
  12405. case 12: script_pushint(st,mob->status.int_); break;
  12406. case 13: script_pushint(st,mob->status.dex); break;
  12407. case 14: script_pushint(st,mob->status.luk); break;
  12408. case 15: script_pushint(st,mob->status.rhw.range); break;
  12409. case 16: script_pushint(st,mob->range2); break;
  12410. case 17: script_pushint(st,mob->range3); break;
  12411. case 18: script_pushint(st,mob->status.size); break;
  12412. case 19: script_pushint(st,mob->status.race); break;
  12413. case 20: script_pushint(st,mob->status.def_ele); break;
  12414. case 21: script_pushint(st,mob->status.mode); break;
  12415. case 22: script_pushint(st,mob->mexp); break;
  12416. default: script_pushint(st,-1); //wrong Index
  12417. }
  12418. return 0;
  12419. }
  12420. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  12421. {
  12422. TBL_PC *sd = NULL;
  12423. if(script_hasdata(st,2))
  12424. sd = map_nick2sd(script_getstr(st,2));
  12425. else
  12426. sd = script_rid2sd(st);
  12427. if(sd)
  12428. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  12429. else
  12430. script_pushint(st,0);
  12431. return 0;
  12432. }
  12433. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  12434. {
  12435. TBL_PC *sd = NULL;
  12436. if(script_hasdata(st,2))
  12437. sd = map_nick2sd(script_getstr(st,2));
  12438. else
  12439. sd = script_rid2sd(st);
  12440. if(sd)
  12441. script_pushint(st,(sd->chatID != 0));
  12442. else
  12443. script_pushint(st,0);
  12444. return 0;
  12445. }
  12446. BUILDIN_FUNC(searchitem)
  12447. {
  12448. struct script_data* data = script_getdata(st, 2);
  12449. const char *itemname = script_getstr(st,3);
  12450. struct item_data *items[MAX_SEARCH];
  12451. int count;
  12452. char* name;
  12453. int32 start;
  12454. int32 id;
  12455. int32 i;
  12456. TBL_PC* sd = NULL;
  12457. if ((items[0] = itemdb_exists(atoi(itemname))))
  12458. count = 1;
  12459. else {
  12460. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  12461. if (count > MAX_SEARCH) count = MAX_SEARCH;
  12462. }
  12463. if (!count) {
  12464. script_pushint(st, 0);
  12465. return 0;
  12466. }
  12467. if( !data_isreference(data) )
  12468. {
  12469. ShowError("script:searchitem: not a variable\n");
  12470. script_reportdata(data);
  12471. st->state = END;
  12472. return 1;// not a variable
  12473. }
  12474. id = reference_getid(data);
  12475. start = reference_getindex(data);
  12476. name = reference_getname(data);
  12477. if( not_array_variable(*name) )
  12478. {
  12479. ShowError("script:searchitem: illegal scope\n");
  12480. script_reportdata(data);
  12481. st->state = END;
  12482. return 1;// not supported
  12483. }
  12484. if( not_server_variable(*name) )
  12485. {
  12486. sd = script_rid2sd(st);
  12487. if( sd == NULL )
  12488. return 0;// no player attached
  12489. }
  12490. if( is_string_variable(name) )
  12491. {// string array
  12492. ShowError("script:searchitem: not an integer array reference\n");
  12493. script_reportdata(data);
  12494. st->state = END;
  12495. return 1;// not supported
  12496. }
  12497. for( i = 0; i < count; ++start, ++i )
  12498. {// Set array
  12499. void* v = (void*)__64BPRTSIZE(items[i]->nameid);
  12500. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  12501. }
  12502. script_pushint(st, count);
  12503. return 0;
  12504. }
  12505. int axtoi(const char *hexStg)
  12506. {
  12507. int n = 0; // position in string
  12508. int m = 0; // position in digit[] to shift
  12509. int count; // loop index
  12510. int intValue = 0; // integer value of hex string
  12511. int digit[11]; // hold values to convert
  12512. while (n < 10) {
  12513. if (hexStg[n]=='\0')
  12514. break;
  12515. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  12516. digit[n] = hexStg[n] & 0x0f; //convert to int
  12517. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  12518. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12519. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  12520. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12521. else break;
  12522. n++;
  12523. }
  12524. count = n;
  12525. m = n - 1;
  12526. n = 0;
  12527. while(n < count) {
  12528. // digit[n] is value of hex digit at position n
  12529. // (m << 2) is the number of positions to shift
  12530. // OR the bits into return value
  12531. intValue = intValue | (digit[n] << (m << 2));
  12532. m--; // adjust the position to set
  12533. n++; // next digit to process
  12534. }
  12535. return (intValue);
  12536. }
  12537. // [Lance] Hex string to integer converter
  12538. BUILDIN_FUNC(axtoi)
  12539. {
  12540. const char *hex = script_getstr(st,2);
  12541. script_pushint(st,axtoi(hex));
  12542. return 0;
  12543. }
  12544. // [zBuffer] List of player cont commands --->
  12545. BUILDIN_FUNC(rid2name)
  12546. {
  12547. struct block_list *bl = NULL;
  12548. int rid = script_getnum(st,2);
  12549. if((bl = map_id2bl(rid)))
  12550. {
  12551. switch(bl->type) {
  12552. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  12553. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  12554. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  12555. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  12556. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  12557. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  12558. default:
  12559. ShowError("buildin_rid2name: BL type unknown.\n");
  12560. script_pushconststr(st,"");
  12561. break;
  12562. }
  12563. } else {
  12564. ShowError("buildin_rid2name: invalid RID\n");
  12565. script_pushconststr(st,"(null)");
  12566. }
  12567. return 0;
  12568. }
  12569. BUILDIN_FUNC(pcblockmove)
  12570. {
  12571. int id, flag;
  12572. TBL_PC *sd = NULL;
  12573. id = script_getnum(st,2);
  12574. flag = script_getnum(st,3);
  12575. if(id)
  12576. sd = map_id2sd(id);
  12577. else
  12578. sd = script_rid2sd(st);
  12579. if(sd)
  12580. sd->state.blockedmove = flag > 0;
  12581. return 0;
  12582. }
  12583. BUILDIN_FUNC(pcfollow)
  12584. {
  12585. int id, targetid;
  12586. TBL_PC *sd = NULL;
  12587. id = script_getnum(st,2);
  12588. targetid = script_getnum(st,3);
  12589. if(id)
  12590. sd = map_id2sd(id);
  12591. else
  12592. sd = script_rid2sd(st);
  12593. if(sd)
  12594. pc_follow(sd, targetid);
  12595. return 0;
  12596. }
  12597. BUILDIN_FUNC(pcstopfollow)
  12598. {
  12599. int id;
  12600. TBL_PC *sd = NULL;
  12601. id = script_getnum(st,2);
  12602. if(id)
  12603. sd = map_id2sd(id);
  12604. else
  12605. sd = script_rid2sd(st);
  12606. if(sd)
  12607. pc_stop_following(sd);
  12608. return 0;
  12609. }
  12610. // <--- [zBuffer] List of player cont commands
  12611. // [zBuffer] List of mob control commands --->
  12612. //## TODO always return if the request/whatever was successfull [FlavioJS]
  12613. /// Makes the unit walk to target position or map
  12614. /// Returns if it was successfull
  12615. ///
  12616. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  12617. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  12618. BUILDIN_FUNC(unitwalk)
  12619. {
  12620. struct block_list* bl;
  12621. bl = map_id2bl(script_getnum(st,2));
  12622. if( bl == NULL )
  12623. {
  12624. script_pushint(st, 0);
  12625. }
  12626. else if( script_hasdata(st,4) )
  12627. {
  12628. int x = script_getnum(st,3);
  12629. int y = script_getnum(st,4);
  12630. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  12631. }
  12632. else
  12633. {
  12634. int map_id = script_getnum(st,3);
  12635. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  12636. }
  12637. return 0;
  12638. }
  12639. /// Kills the unit
  12640. ///
  12641. /// unitkill <unit_id>;
  12642. BUILDIN_FUNC(unitkill)
  12643. {
  12644. struct block_list* bl = map_id2bl(script_getnum(st,2));
  12645. if( bl != NULL )
  12646. status_kill(bl);
  12647. return 0;
  12648. }
  12649. /// Warps the unit to the target position in the target map
  12650. /// Returns if it was successfull
  12651. ///
  12652. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  12653. BUILDIN_FUNC(unitwarp)
  12654. {
  12655. int unit_id;
  12656. int map;
  12657. short x;
  12658. short y;
  12659. struct block_list* bl;
  12660. const char *mapname;
  12661. unit_id = script_getnum(st,2);
  12662. mapname = script_getstr(st, 3);
  12663. x = (short)script_getnum(st,4);
  12664. y = (short)script_getnum(st,5);
  12665. if (!unit_id) //Warp the script's runner
  12666. bl = map_id2bl(st->rid);
  12667. else
  12668. bl = map_id2bl(unit_id);
  12669. if( strcmp(mapname,"this") == 0 )
  12670. map = bl?bl->m:-1;
  12671. else
  12672. map = map_mapname2mapid(mapname);
  12673. if( map >= 0 && bl != NULL )
  12674. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  12675. else
  12676. script_pushint(st, 0);
  12677. return 0;
  12678. }
  12679. /// Makes the unit attack the target.
  12680. /// If the unit is a player and <action type> is not 0, it does a continuous
  12681. /// attack instead of a single attack.
  12682. /// Returns if the request was successfull.
  12683. ///
  12684. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  12685. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  12686. BUILDIN_FUNC(unitattack)
  12687. {
  12688. struct block_list* unit_bl;
  12689. struct block_list* target_bl = NULL;
  12690. struct script_data* data;
  12691. int actiontype = 0;
  12692. // get unit
  12693. unit_bl = map_id2bl(script_getnum(st,2));
  12694. if( unit_bl == NULL ) {
  12695. script_pushint(st, 0);
  12696. return 0;
  12697. }
  12698. data = script_getdata(st, 3);
  12699. get_val(st, data);
  12700. if( data_isstring(data) )
  12701. {
  12702. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  12703. if( sd != NULL )
  12704. target_bl = &sd->bl;
  12705. } else
  12706. target_bl = map_id2bl(conv_num(st, data));
  12707. // request the attack
  12708. if( target_bl == NULL )
  12709. {
  12710. script_pushint(st, 0);
  12711. return 0;
  12712. }
  12713. // get actiontype
  12714. if( script_hasdata(st,4) )
  12715. actiontype = script_getnum(st,4);
  12716. switch( unit_bl->type )
  12717. {
  12718. case BL_PC:
  12719. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  12720. script_pushint(st, 1);
  12721. return 0;
  12722. case BL_MOB:
  12723. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  12724. break;
  12725. case BL_PET:
  12726. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  12727. break;
  12728. default:
  12729. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  12730. script_pushint(st, 0);
  12731. return 1;
  12732. }
  12733. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  12734. return 0;
  12735. }
  12736. /// Makes the unit stop attacking and moving
  12737. ///
  12738. /// unitstop <unit_id>;
  12739. BUILDIN_FUNC(unitstop)
  12740. {
  12741. int unit_id;
  12742. struct block_list* bl;
  12743. unit_id = script_getnum(st,2);
  12744. bl = map_id2bl(unit_id);
  12745. if( bl != NULL )
  12746. {
  12747. unit_stop_attack(bl);
  12748. unit_stop_walking(bl,4);
  12749. if( bl->type == BL_MOB )
  12750. ((TBL_MOB*)bl)->target_id = 0;
  12751. }
  12752. return 0;
  12753. }
  12754. /// Makes the unit say the message
  12755. ///
  12756. /// unittalk <unit_id>,"<message>";
  12757. BUILDIN_FUNC(unittalk)
  12758. {
  12759. int unit_id;
  12760. const char* message;
  12761. struct block_list* bl;
  12762. unit_id = script_getnum(st,2);
  12763. message = script_getstr(st, 3);
  12764. bl = map_id2bl(unit_id);
  12765. if( bl != NULL )
  12766. {
  12767. struct StringBuf sbuf;
  12768. StringBuf_Init(&sbuf);
  12769. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  12770. clif_message(bl, StringBuf_Value(&sbuf));
  12771. if( bl->type == BL_PC )
  12772. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  12773. StringBuf_Destroy(&sbuf);
  12774. }
  12775. return 0;
  12776. }
  12777. /// Makes the unit do an emotion
  12778. ///
  12779. /// unitemote <unit_id>,<emotion>;
  12780. ///
  12781. /// @see e_* in const.txt
  12782. BUILDIN_FUNC(unitemote)
  12783. {
  12784. int unit_id;
  12785. int emotion;
  12786. struct block_list* bl;
  12787. unit_id = script_getnum(st,2);
  12788. emotion = script_getnum(st,3);
  12789. bl = map_id2bl(unit_id);
  12790. if( bl != NULL )
  12791. clif_emotion(bl, emotion);
  12792. return 0;
  12793. }
  12794. /// Makes the unit cast the skill on the target or self if no target is specified
  12795. ///
  12796. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  12797. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  12798. BUILDIN_FUNC(unitskilluseid)
  12799. {
  12800. int unit_id;
  12801. int skill_id;
  12802. int skill_lv;
  12803. int target_id;
  12804. struct block_list* bl;
  12805. unit_id = script_getnum(st,2);
  12806. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12807. skill_lv = script_getnum(st,4);
  12808. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  12809. bl = map_id2bl(unit_id);
  12810. if( bl != NULL )
  12811. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  12812. return 0;
  12813. }
  12814. /// Makes the unit cast the skill on the target position.
  12815. ///
  12816. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  12817. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  12818. BUILDIN_FUNC(unitskillusepos)
  12819. {
  12820. int unit_id;
  12821. int skill_id;
  12822. int skill_lv;
  12823. int skill_x;
  12824. int skill_y;
  12825. struct block_list* bl;
  12826. unit_id = script_getnum(st,2);
  12827. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12828. skill_lv = script_getnum(st,4);
  12829. skill_x = script_getnum(st,5);
  12830. skill_y = script_getnum(st,6);
  12831. bl = map_id2bl(unit_id);
  12832. if( bl != NULL )
  12833. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  12834. return 0;
  12835. }
  12836. // <--- [zBuffer] List of mob control commands
  12837. /// Pauses the execution of the script, detaching the player
  12838. ///
  12839. /// sleep <mili seconds>;
  12840. BUILDIN_FUNC(sleep)
  12841. {
  12842. int ticks;
  12843. ticks = script_getnum(st,2);
  12844. // detach the player
  12845. script_detach_rid(st);
  12846. if( ticks <= 0 )
  12847. {// do nothing
  12848. }
  12849. else if( st->sleep.tick == 0 )
  12850. {// sleep for the target amount of time
  12851. st->state = RERUNLINE;
  12852. st->sleep.tick = ticks;
  12853. }
  12854. else
  12855. {// sleep time is over
  12856. st->state = RUN;
  12857. st->sleep.tick = 0;
  12858. }
  12859. return 0;
  12860. }
  12861. /// Pauses the execution of the script, keeping the player attached
  12862. /// Returns if a player is still attached
  12863. ///
  12864. /// sleep2(<mili secconds>) -> <bool>
  12865. BUILDIN_FUNC(sleep2)
  12866. {
  12867. int ticks;
  12868. ticks = script_getnum(st,2);
  12869. if( ticks <= 0 )
  12870. {// do nothing
  12871. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12872. }
  12873. else if( !st->sleep.tick )
  12874. {// sleep for the target amount of time
  12875. st->state = RERUNLINE;
  12876. st->sleep.tick = ticks;
  12877. }
  12878. else
  12879. {// sleep time is over
  12880. st->state = RUN;
  12881. st->sleep.tick = 0;
  12882. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12883. }
  12884. return 0;
  12885. }
  12886. /// Awakes all the sleep timers of the target npc
  12887. ///
  12888. /// awake "<npc name>";
  12889. BUILDIN_FUNC(awake)
  12890. {
  12891. struct npc_data* nd;
  12892. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  12893. nd = npc_name2id(script_getstr(st, 2));
  12894. if( nd == NULL ) {
  12895. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  12896. return 1;
  12897. }
  12898. while( node )
  12899. {
  12900. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  12901. {// sleep timer for the npc
  12902. struct script_state* tst = (struct script_state*)node->data;
  12903. TBL_PC* sd = map_id2sd(tst->rid);
  12904. if( tst->sleep.timer == INVALID_TIMER )
  12905. {// already awake ???
  12906. node = node->next;
  12907. continue;
  12908. }
  12909. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  12910. {// char not online anymore / another char of the same account is online - Cancel execution
  12911. tst->state = END;
  12912. tst->rid = 0;
  12913. }
  12914. delete_timer(tst->sleep.timer, run_script_timer);
  12915. node = script_erase_sleepdb(node);
  12916. tst->sleep.timer = INVALID_TIMER;
  12917. if(tst->state != RERUNLINE)
  12918. tst->sleep.tick = 0;
  12919. run_script_main(tst);
  12920. }
  12921. else
  12922. {
  12923. node = node->next;
  12924. }
  12925. }
  12926. return 0;
  12927. }
  12928. /// Returns a reference to a variable of the target NPC.
  12929. /// Returns 0 if an error occurs.
  12930. ///
  12931. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  12932. BUILDIN_FUNC(getvariableofnpc)
  12933. {
  12934. struct script_data* data;
  12935. const char* name;
  12936. struct npc_data* nd;
  12937. data = script_getdata(st,2);
  12938. if( !data_isreference(data) )
  12939. {// Not a reference (aka varaible name)
  12940. ShowError("script:getvariableofnpc: not a variable\n");
  12941. script_reportdata(data);
  12942. script_pushnil(st);
  12943. st->state = END;
  12944. return 1;
  12945. }
  12946. name = reference_getname(data);
  12947. if( *name != '.' || name[1] == '@' )
  12948. {// not a npc variable
  12949. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  12950. script_reportdata(data);
  12951. script_pushnil(st);
  12952. st->state = END;
  12953. return 1;
  12954. }
  12955. nd = npc_name2id(script_getstr(st,3));
  12956. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  12957. {// NPC not found or has no script
  12958. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  12959. script_pushnil(st);
  12960. st->state = END;
  12961. return 1;
  12962. }
  12963. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  12964. return 0;
  12965. }
  12966. /// Opens a warp portal.
  12967. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  12968. ///
  12969. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  12970. ///
  12971. /// @author blackhole89
  12972. BUILDIN_FUNC(warpportal)
  12973. {
  12974. int spx;
  12975. int spy;
  12976. unsigned short mapindex;
  12977. int tpx;
  12978. int tpy;
  12979. struct skill_unit_group* group;
  12980. struct block_list* bl;
  12981. bl = map_id2bl(st->oid);
  12982. if( bl == NULL )
  12983. {
  12984. ShowError("script:warpportal: npc is needed\n");
  12985. return 1;
  12986. }
  12987. spx = script_getnum(st,2);
  12988. spy = script_getnum(st,3);
  12989. mapindex = mapindex_name2id(script_getstr(st, 4));
  12990. tpx = script_getnum(st,5);
  12991. tpy = script_getnum(st,6);
  12992. if( mapindex == 0 )
  12993. return 0;// map not found
  12994. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  12995. if( group == NULL )
  12996. return 0;// failed
  12997. group->val2 = (tpx<<16) | tpy;
  12998. group->val3 = mapindex;
  12999. return 0;
  13000. }
  13001. BUILDIN_FUNC(openmail)
  13002. {
  13003. TBL_PC* sd;
  13004. sd = script_rid2sd(st);
  13005. if( sd == NULL )
  13006. return 0;
  13007. mail_openmail(sd);
  13008. return 0;
  13009. }
  13010. BUILDIN_FUNC(openauction)
  13011. {
  13012. TBL_PC* sd;
  13013. sd = script_rid2sd(st);
  13014. if( sd == NULL )
  13015. return 0;
  13016. clif_Auction_openwindow(sd);
  13017. return 0;
  13018. }
  13019. /// Retrieves the value of the specified flag of the specified cell.
  13020. ///
  13021. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  13022. ///
  13023. /// @see cell_chk* constants in const.txt for the types
  13024. BUILDIN_FUNC(checkcell)
  13025. {
  13026. int m = map_mapname2mapid(script_getstr(st,2));
  13027. int x = script_getnum(st,3);
  13028. int y = script_getnum(st,4);
  13029. cell_chk type = (cell_chk)script_getnum(st,5);
  13030. script_pushint(st, map_getcell(m, x, y, type));
  13031. return 0;
  13032. }
  13033. /// Modifies flags of cells in the specified area.
  13034. ///
  13035. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  13036. ///
  13037. /// @see cell_* constants in const.txt for the types
  13038. BUILDIN_FUNC(setcell)
  13039. {
  13040. int m = map_mapname2mapid(script_getstr(st,2));
  13041. int x1 = script_getnum(st,3);
  13042. int y1 = script_getnum(st,4);
  13043. int x2 = script_getnum(st,5);
  13044. int y2 = script_getnum(st,6);
  13045. cell_t type = (cell_t)script_getnum(st,7);
  13046. bool flag = (bool)script_getnum(st,8);
  13047. int x,y;
  13048. if( x1 > x2 ) swap(x1,x2);
  13049. if( y1 > y2 ) swap(y1,y2);
  13050. for( y = y1; y <= y2; ++y )
  13051. for( x = x1; x <= x2; ++x )
  13052. map_setcell(m, x, y, type, flag);
  13053. return 0;
  13054. }
  13055. /*==========================================
  13056. * Mercenary Commands
  13057. *------------------------------------------*/
  13058. BUILDIN_FUNC(mercenary_create)
  13059. {
  13060. struct map_session_data *sd;
  13061. int class_, contract_time;
  13062. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  13063. return 0;
  13064. class_ = script_getnum(st,2);
  13065. if( !merc_class(class_) )
  13066. return 0;
  13067. contract_time = script_getnum(st,3);
  13068. merc_create(sd, class_, contract_time);
  13069. return 0;
  13070. }
  13071. BUILDIN_FUNC(mercenary_heal)
  13072. {
  13073. struct map_session_data *sd = script_rid2sd(st);
  13074. int hp, sp;
  13075. if( sd == NULL || sd->md == NULL )
  13076. return 0;
  13077. hp = script_getnum(st,2);
  13078. sp = script_getnum(st,3);
  13079. status_heal(&sd->md->bl, hp, sp, 0);
  13080. return 0;
  13081. }
  13082. BUILDIN_FUNC(mercenary_sc_start)
  13083. {
  13084. struct map_session_data *sd = script_rid2sd(st);
  13085. enum sc_type type;
  13086. int tick, val1;
  13087. if( sd == NULL || sd->md == NULL )
  13088. return 0;
  13089. type = (sc_type)script_getnum(st,2);
  13090. tick = script_getnum(st,3);
  13091. val1 = script_getnum(st,4);
  13092. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  13093. return 0;
  13094. }
  13095. BUILDIN_FUNC(mercenary_get_calls)
  13096. {
  13097. struct map_session_data *sd = script_rid2sd(st);
  13098. int guild;
  13099. if( sd == NULL )
  13100. return 0;
  13101. guild = script_getnum(st,2);
  13102. switch( guild )
  13103. {
  13104. case ARCH_MERC_GUILD:
  13105. script_pushint(st,sd->status.arch_calls);
  13106. break;
  13107. case SPEAR_MERC_GUILD:
  13108. script_pushint(st,sd->status.spear_calls);
  13109. break;
  13110. case SWORD_MERC_GUILD:
  13111. script_pushint(st,sd->status.sword_calls);
  13112. break;
  13113. default:
  13114. script_pushint(st,0);
  13115. break;
  13116. }
  13117. return 0;
  13118. }
  13119. BUILDIN_FUNC(mercenary_set_calls)
  13120. {
  13121. struct map_session_data *sd = script_rid2sd(st);
  13122. int guild, value, *calls;
  13123. if( sd == NULL )
  13124. return 0;
  13125. guild = script_getnum(st,2);
  13126. value = script_getnum(st,3);
  13127. switch( guild )
  13128. {
  13129. case ARCH_MERC_GUILD:
  13130. calls = &sd->status.arch_calls;
  13131. break;
  13132. case SPEAR_MERC_GUILD:
  13133. calls = &sd->status.spear_calls;
  13134. break;
  13135. case SWORD_MERC_GUILD:
  13136. calls = &sd->status.sword_calls;
  13137. break;
  13138. default:
  13139. return 0; // Invalid Guild
  13140. }
  13141. *calls += value;
  13142. *calls = cap_value(*calls, 0, INT_MAX);
  13143. return 0;
  13144. }
  13145. BUILDIN_FUNC(mercenary_get_faith)
  13146. {
  13147. struct map_session_data *sd = script_rid2sd(st);
  13148. int guild;
  13149. if( sd == NULL )
  13150. return 0;
  13151. guild = script_getnum(st,2);
  13152. switch( guild )
  13153. {
  13154. case ARCH_MERC_GUILD:
  13155. script_pushint(st,sd->status.arch_faith);
  13156. break;
  13157. case SPEAR_MERC_GUILD:
  13158. script_pushint(st,sd->status.spear_faith);
  13159. break;
  13160. case SWORD_MERC_GUILD:
  13161. script_pushint(st,sd->status.sword_faith);
  13162. break;
  13163. default:
  13164. script_pushint(st,0);
  13165. break;
  13166. }
  13167. return 0;
  13168. }
  13169. BUILDIN_FUNC(mercenary_set_faith)
  13170. {
  13171. struct map_session_data *sd = script_rid2sd(st);
  13172. int guild, value, *calls;
  13173. if( sd == NULL )
  13174. return 0;
  13175. guild = script_getnum(st,2);
  13176. value = script_getnum(st,3);
  13177. switch( guild )
  13178. {
  13179. case ARCH_MERC_GUILD:
  13180. calls = &sd->status.arch_faith;
  13181. break;
  13182. case SPEAR_MERC_GUILD:
  13183. calls = &sd->status.spear_faith;
  13184. break;
  13185. case SWORD_MERC_GUILD:
  13186. calls = &sd->status.sword_faith;
  13187. break;
  13188. default:
  13189. return 0; // Invalid Guild
  13190. }
  13191. *calls += value;
  13192. *calls = cap_value(*calls, 0, INT_MAX);
  13193. if( mercenary_get_guild(sd->md) == guild )
  13194. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  13195. return 0;
  13196. }
  13197. /*------------------------------------------
  13198. * Book Reading
  13199. *------------------------------------------*/
  13200. BUILDIN_FUNC(readbook)
  13201. {
  13202. struct map_session_data *sd;
  13203. int book_id, page;
  13204. if( (sd = script_rid2sd(st)) == NULL )
  13205. return 0;
  13206. book_id = script_getnum(st,2);
  13207. page = script_getnum(st,3);
  13208. clif_readbook(sd->fd, book_id, page);
  13209. return 0;
  13210. }
  13211. /******************
  13212. Questlog script commands
  13213. *******************/
  13214. BUILDIN_FUNC(setquest)
  13215. {
  13216. TBL_PC * sd = script_rid2sd(st);
  13217. quest_add(sd, script_getnum(st, 2));
  13218. return 0;
  13219. }
  13220. BUILDIN_FUNC(erasequest)
  13221. {
  13222. TBL_PC * sd = script_rid2sd(st);
  13223. quest_delete(sd, script_getnum(st, 2));
  13224. return 0;
  13225. }
  13226. BUILDIN_FUNC(completequest)
  13227. {
  13228. TBL_PC * sd = script_rid2sd(st);
  13229. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  13230. return 0;
  13231. }
  13232. BUILDIN_FUNC(changequest)
  13233. {
  13234. TBL_PC * sd = script_rid2sd(st);
  13235. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  13236. return 0;
  13237. }
  13238. BUILDIN_FUNC(checkquest)
  13239. {
  13240. TBL_PC * sd = script_rid2sd(st);
  13241. quest_check_type type = HAVEQUEST;
  13242. if( script_hasdata(st, 3) )
  13243. type = (quest_check_type)script_getnum(st, 3);
  13244. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  13245. return 0;
  13246. }
  13247. BUILDIN_FUNC(showevent)
  13248. {
  13249. TBL_PC *sd = script_rid2sd(st);
  13250. struct npc_data *nd = map_id2nd(st->oid);
  13251. int state, color;
  13252. if( sd == NULL || nd == NULL )
  13253. return 0;
  13254. state = script_getnum(st, 2);
  13255. color = script_getnum(st, 3);
  13256. if( color < 0 || color > 3 )
  13257. color = 0; // set default color
  13258. clif_quest_show_event(sd, &nd->bl, state, color);
  13259. return 0;
  13260. }
  13261. /*==========================================
  13262. * BattleGround System
  13263. *------------------------------------------*/
  13264. BUILDIN_FUNC(waitingroom2bg)
  13265. {
  13266. struct npc_data *nd;
  13267. struct chat_data *cd;
  13268. const char *map_name, *ev = "", *dev = "";
  13269. int x, y, i, mapindex = 0, bg_id, n;
  13270. struct map_session_data *sd;
  13271. if( script_hasdata(st,7) )
  13272. nd = npc_name2id(script_getstr(st,7));
  13273. else
  13274. nd = (struct npc_data *)map_id2bl(st->oid);
  13275. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  13276. {
  13277. script_pushint(st,0);
  13278. return 0;
  13279. }
  13280. map_name = script_getstr(st,2);
  13281. if( strcmp(map_name,"-") != 0 )
  13282. {
  13283. mapindex = mapindex_name2id(map_name);
  13284. if( mapindex == 0 )
  13285. { // Invalid Map
  13286. script_pushint(st,0);
  13287. return 0;
  13288. }
  13289. }
  13290. x = script_getnum(st,3);
  13291. y = script_getnum(st,4);
  13292. ev = script_getstr(st,5); // Logout Event
  13293. dev = script_getstr(st,6); // Die Event
  13294. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  13295. { // Creation failed
  13296. script_pushint(st,0);
  13297. return 0;
  13298. }
  13299. n = cd->users;
  13300. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  13301. {
  13302. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  13303. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  13304. else
  13305. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  13306. }
  13307. mapreg_setreg(add_str("$@arenamembersnum"), i);
  13308. script_pushint(st,bg_id);
  13309. return 0;
  13310. }
  13311. BUILDIN_FUNC(waitingroom2bg_single)
  13312. {
  13313. const char* map_name;
  13314. struct npc_data *nd;
  13315. struct chat_data *cd;
  13316. struct map_session_data *sd;
  13317. int x, y, mapindex, bg_id;
  13318. bg_id = script_getnum(st,2);
  13319. map_name = script_getstr(st,3);
  13320. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13321. return 0; // Invalid Map
  13322. x = script_getnum(st,4);
  13323. y = script_getnum(st,5);
  13324. nd = npc_name2id(script_getstr(st,6));
  13325. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  13326. return 0;
  13327. if( (sd = cd->usersd[0]) == NULL )
  13328. return 0;
  13329. if( bg_team_join(bg_id, sd) )
  13330. {
  13331. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  13332. script_pushint(st,1);
  13333. }
  13334. else
  13335. script_pushint(st,0);
  13336. return 0;
  13337. }
  13338. BUILDIN_FUNC(bg_team_setxy)
  13339. {
  13340. struct battleground_data *bg;
  13341. int bg_id;
  13342. bg_id = script_getnum(st,2);
  13343. if( (bg = bg_team_search(bg_id)) == NULL )
  13344. return 0;
  13345. bg->x = script_getnum(st,3);
  13346. bg->y = script_getnum(st,4);
  13347. return 0;
  13348. }
  13349. BUILDIN_FUNC(bg_warp)
  13350. {
  13351. int x, y, mapindex, bg_id;
  13352. const char* map_name;
  13353. bg_id = script_getnum(st,2);
  13354. map_name = script_getstr(st,3);
  13355. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13356. return 0; // Invalid Map
  13357. x = script_getnum(st,4);
  13358. y = script_getnum(st,5);
  13359. bg_team_warp(bg_id, mapindex, x, y);
  13360. return 0;
  13361. }
  13362. BUILDIN_FUNC(bg_monster)
  13363. {
  13364. int class_ = 0, x = 0, y = 0, bg_id = 0;
  13365. const char *str,*map, *evt="";
  13366. bg_id = script_getnum(st,2);
  13367. map = script_getstr(st,3);
  13368. x = script_getnum(st,4);
  13369. y = script_getnum(st,5);
  13370. str = script_getstr(st,6);
  13371. class_ = script_getnum(st,7);
  13372. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  13373. check_event(st, evt);
  13374. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  13375. return 0;
  13376. }
  13377. BUILDIN_FUNC(bg_monster_set_team)
  13378. {
  13379. struct mob_data *md;
  13380. struct block_list *mbl;
  13381. int id = script_getnum(st,2),
  13382. bg_id = script_getnum(st,3);
  13383. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  13384. return 0;
  13385. md = (TBL_MOB *)mbl;
  13386. md->bg_id = bg_id;
  13387. mob_stop_attack(md);
  13388. mob_stop_walking(md, 0);
  13389. md->target_id = md->attacked_id = 0;
  13390. clif_charnameack(0, &md->bl);
  13391. return 0;
  13392. }
  13393. BUILDIN_FUNC(bg_leave)
  13394. {
  13395. struct map_session_data *sd = script_rid2sd(st);
  13396. if( sd == NULL || !sd->bg_id )
  13397. return 0;
  13398. bg_team_leave(sd,0);
  13399. return 0;
  13400. }
  13401. BUILDIN_FUNC(bg_destroy)
  13402. {
  13403. int bg_id = script_getnum(st,2);
  13404. bg_team_delete(bg_id);
  13405. return 0;
  13406. }
  13407. BUILDIN_FUNC(bg_getareausers)
  13408. {
  13409. const char *str;
  13410. int m, x0, y0, x1, y1, bg_id;
  13411. int i = 0, c = 0;
  13412. struct battleground_data *bg = NULL;
  13413. struct map_session_data *sd;
  13414. bg_id = script_getnum(st,2);
  13415. str = script_getstr(st,3);
  13416. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  13417. {
  13418. script_pushint(st,0);
  13419. return 0;
  13420. }
  13421. x0 = script_getnum(st,4);
  13422. y0 = script_getnum(st,5);
  13423. x1 = script_getnum(st,6);
  13424. y1 = script_getnum(st,7);
  13425. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  13426. {
  13427. if( (sd = bg->members[i].sd) == NULL )
  13428. continue;
  13429. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  13430. continue;
  13431. c++;
  13432. }
  13433. script_pushint(st,c);
  13434. return 0;
  13435. }
  13436. BUILDIN_FUNC(bg_updatescore)
  13437. {
  13438. const char *str;
  13439. int m;
  13440. str = script_getstr(st,2);
  13441. if( (m = map_mapname2mapid(str)) < 0 )
  13442. return 0;
  13443. map[m].bgscore_lion = script_getnum(st,3);
  13444. map[m].bgscore_eagle = script_getnum(st,4);
  13445. clif_bg_updatescore(m);
  13446. return 0;
  13447. }
  13448. BUILDIN_FUNC(bg_get_data)
  13449. {
  13450. struct battleground_data *bg;
  13451. int bg_id = script_getnum(st,2),
  13452. type = script_getnum(st,3);
  13453. if( (bg = bg_team_search(bg_id)) == NULL )
  13454. {
  13455. script_pushint(st,0);
  13456. return 0;
  13457. }
  13458. switch( type )
  13459. {
  13460. case 0: script_pushint(st, bg->count); break;
  13461. default:
  13462. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  13463. break;
  13464. }
  13465. return 0;
  13466. }
  13467. /*==========================================
  13468. * Instancing Script Commands
  13469. *------------------------------------------*/
  13470. BUILDIN_FUNC(instance_create)
  13471. {
  13472. const char *name;
  13473. int party_id, res;
  13474. name = script_getstr(st, 2);
  13475. party_id = script_getnum(st, 3);
  13476. res = instance_create(party_id, name);
  13477. if( res == -4 ) // Already exists
  13478. {
  13479. script_pushint(st, -1);
  13480. return 0;
  13481. }
  13482. else if( res < 0 )
  13483. {
  13484. const char *err;
  13485. switch(res)
  13486. {
  13487. case -3: err = "No free instances"; break;
  13488. case -2: err = "Invalid party ID"; break;
  13489. case -1: err = "Invalid type"; break;
  13490. default: err = "Unknown"; break;
  13491. }
  13492. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  13493. script_pushint(st, -2);
  13494. return 0;
  13495. }
  13496. script_pushint(st, res);
  13497. return 0;
  13498. }
  13499. BUILDIN_FUNC(instance_destroy)
  13500. {
  13501. int instance_id;
  13502. struct map_session_data *sd;
  13503. struct party_data *p;
  13504. if( script_hasdata(st, 2) )
  13505. instance_id = script_getnum(st, 2);
  13506. else if( st->instance_id )
  13507. instance_id = st->instance_id;
  13508. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13509. instance_id = p->instance_id;
  13510. else return 0;
  13511. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13512. {
  13513. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  13514. return 0;
  13515. }
  13516. instance_destroy(instance_id);
  13517. return 0;
  13518. }
  13519. BUILDIN_FUNC(instance_attachmap)
  13520. {
  13521. const char *name;
  13522. int m;
  13523. int instance_id;
  13524. bool usebasename = false;
  13525. name = script_getstr(st,2);
  13526. instance_id = script_getnum(st,3);
  13527. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  13528. usebasename = true;
  13529. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  13530. {
  13531. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  13532. script_pushconststr(st, "");
  13533. return 0;
  13534. }
  13535. script_pushconststr(st, map[m].name);
  13536. return 0;
  13537. }
  13538. BUILDIN_FUNC(instance_detachmap)
  13539. {
  13540. struct map_session_data *sd;
  13541. struct party_data *p;
  13542. const char *str;
  13543. int m, instance_id;
  13544. str = script_getstr(st, 2);
  13545. if( script_hasdata(st, 3) )
  13546. instance_id = script_getnum(st, 3);
  13547. else if( st->instance_id )
  13548. instance_id = st->instance_id;
  13549. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13550. instance_id = p->instance_id;
  13551. else return 0;
  13552. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  13553. {
  13554. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  13555. return 0;
  13556. }
  13557. instance_del_map(m);
  13558. return 0;
  13559. }
  13560. BUILDIN_FUNC(instance_attach)
  13561. {
  13562. int instance_id;
  13563. instance_id = script_getnum(st, 2);
  13564. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13565. return 0;
  13566. st->instance_id = instance_id;
  13567. return 0;
  13568. }
  13569. BUILDIN_FUNC(instance_id)
  13570. {
  13571. int type, instance_id;
  13572. struct map_session_data *sd;
  13573. struct party_data *p;
  13574. if( script_hasdata(st, 2) )
  13575. {
  13576. type = script_getnum(st, 2);
  13577. if( type == 0 )
  13578. instance_id = st->instance_id;
  13579. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  13580. instance_id = p->instance_id;
  13581. else
  13582. instance_id = 0;
  13583. }
  13584. else
  13585. instance_id = st->instance_id;
  13586. script_pushint(st, instance_id);
  13587. return 0;
  13588. }
  13589. BUILDIN_FUNC(instance_set_timeout)
  13590. {
  13591. int progress_timeout, idle_timeout;
  13592. int instance_id;
  13593. struct map_session_data *sd;
  13594. struct party_data *p;
  13595. progress_timeout = script_getnum(st, 2);
  13596. idle_timeout = script_getnum(st, 3);
  13597. if( script_hasdata(st, 4) )
  13598. instance_id = script_getnum(st, 4);
  13599. else if( st->instance_id )
  13600. instance_id = st->instance_id;
  13601. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13602. instance_id = p->instance_id;
  13603. else return 0;
  13604. if( instance_id > 0 )
  13605. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  13606. return 0;
  13607. }
  13608. BUILDIN_FUNC(instance_init)
  13609. {
  13610. int instance_id = script_getnum(st, 2);
  13611. if( instance[instance_id].state != INSTANCE_IDLE )
  13612. {
  13613. ShowError("instance_init: instance already initialized.\n");
  13614. return 0;
  13615. }
  13616. instance_init(instance_id);
  13617. return 0;
  13618. }
  13619. BUILDIN_FUNC(instance_announce)
  13620. {
  13621. int instance_id = script_getnum(st,2);
  13622. const char *mes = script_getstr(st,3);
  13623. int flag = script_getnum(st,4);
  13624. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  13625. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  13626. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  13627. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  13628. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  13629. int i;
  13630. struct map_session_data *sd;
  13631. struct party_data *p;
  13632. if( instance_id == 0 )
  13633. {
  13634. if( st->instance_id )
  13635. instance_id = st->instance_id;
  13636. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13637. instance_id = p->instance_id;
  13638. else return 0;
  13639. }
  13640. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13641. return 0;
  13642. for( i = 0; i < instance[instance_id].num_map; i++ )
  13643. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  13644. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  13645. return 0;
  13646. }
  13647. BUILDIN_FUNC(instance_npcname)
  13648. {
  13649. const char *str;
  13650. int instance_id = 0;
  13651. struct map_session_data *sd;
  13652. struct party_data *p;
  13653. struct npc_data *nd;
  13654. str = script_getstr(st, 2);
  13655. if( script_hasdata(st, 3) )
  13656. instance_id = script_getnum(st, 3);
  13657. else if( st->instance_id )
  13658. instance_id = st->instance_id;
  13659. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13660. instance_id = p->instance_id;
  13661. if( instance_id && (nd = npc_name2id(str)) != NULL )
  13662. {
  13663. static char npcname[NAME_LENGTH];
  13664. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  13665. script_pushconststr(st,npcname);
  13666. }
  13667. else
  13668. {
  13669. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  13670. st->state = END;
  13671. return 1;
  13672. }
  13673. return 0;
  13674. }
  13675. BUILDIN_FUNC(has_instance)
  13676. {
  13677. struct map_session_data *sd;
  13678. struct party_data *p;
  13679. const char *str;
  13680. int m, instance_id = 0;
  13681. str = script_getstr(st, 2);
  13682. if( script_hasdata(st, 3) )
  13683. instance_id = script_getnum(st, 3);
  13684. else if( st->instance_id )
  13685. instance_id = st->instance_id;
  13686. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13687. instance_id = p->instance_id;
  13688. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  13689. {
  13690. script_pushconststr(st, "");
  13691. return 0;
  13692. }
  13693. script_pushconststr(st, map[m].name);
  13694. return 0;
  13695. }
  13696. BUILDIN_FUNC(instance_warpall)
  13697. {
  13698. struct map_session_data *pl_sd;
  13699. int m, i, instance_id;
  13700. const char *mapn;
  13701. int x, y;
  13702. unsigned short mapindex;
  13703. struct party_data *p = NULL;
  13704. mapn = script_getstr(st,2);
  13705. x = script_getnum(st,3);
  13706. y = script_getnum(st,4);
  13707. if( script_hasdata(st,5) )
  13708. instance_id = script_getnum(st,5);
  13709. else if( st->instance_id )
  13710. instance_id = st->instance_id;
  13711. 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 )
  13712. instance_id = p->instance_id;
  13713. else return 0;
  13714. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  13715. return 0;
  13716. if( !(p = party_search(instance[instance_id].party_id)) )
  13717. return 0;
  13718. mapindex = map_id2index(m);
  13719. for( i = 0; i < MAX_PARTY; i++ )
  13720. 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);
  13721. return 0;
  13722. }
  13723. /*==========================================
  13724. * Custom Fonts
  13725. *------------------------------------------*/
  13726. BUILDIN_FUNC(setfont)
  13727. {
  13728. struct map_session_data *sd = script_rid2sd(st);
  13729. int font = script_getnum(st,2);
  13730. if( sd == NULL )
  13731. return 0;
  13732. if( sd->user_font != font )
  13733. sd->user_font = font;
  13734. else
  13735. sd->user_font = 0;
  13736. clif_font(sd);
  13737. return 0;
  13738. }
  13739. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  13740. {
  13741. TBL_MOB* md = (TBL_MOB*)bl;
  13742. struct block_list *tbl;
  13743. int mobid = va_arg(ap,int);
  13744. int skillid = va_arg(ap,int);
  13745. int skilllv = va_arg(ap,int);
  13746. int casttime = va_arg(ap,int);
  13747. int cancel = va_arg(ap,int);
  13748. int emotion = va_arg(ap,int);
  13749. int target = va_arg(ap,int);
  13750. if( md->class_ != mobid )
  13751. return 0;
  13752. // 0:self, 1:target, 2:master, default:random
  13753. switch( target )
  13754. {
  13755. case 0: tbl = map_id2bl(md->bl.id); break;
  13756. case 1: tbl = map_id2bl(md->target_id); break;
  13757. case 2: tbl = map_id2bl(md->master_id); break;
  13758. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  13759. }
  13760. if( !tbl )
  13761. return 0;
  13762. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  13763. unit_skillcastcancel(bl,0);
  13764. if( skill_get_casttype(skillid) == CAST_GROUND )
  13765. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  13766. else
  13767. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  13768. clif_emotion(&md->bl, emotion);
  13769. return 0;
  13770. }
  13771. /*==========================================
  13772. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  13773. *------------------------------------------*/
  13774. BUILDIN_FUNC(areamobuseskill)
  13775. {
  13776. struct block_list center;
  13777. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  13778. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  13779. {
  13780. ShowError("areamobuseskill: invalid map name.\n");
  13781. return 0;
  13782. }
  13783. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  13784. return 0;
  13785. center.m = m;
  13786. center.x = script_getnum(st,3);
  13787. center.y = script_getnum(st,4);
  13788. range = script_getnum(st,5);
  13789. mobid = script_getnum(st,6);
  13790. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  13791. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  13792. skilllv = battle_config.mob_max_skilllvl;
  13793. casttime = script_getnum(st,9);
  13794. cancel = script_getnum(st,10);
  13795. emotion = script_getnum(st,11);
  13796. target = script_getnum(st,12);
  13797. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  13798. return 0;
  13799. }
  13800. BUILDIN_FUNC(progressbar)
  13801. {
  13802. struct map_session_data * sd = script_rid2sd(st);
  13803. const char * color;
  13804. unsigned int second;
  13805. if( !st || !sd )
  13806. return 0;
  13807. st->state = STOP;
  13808. color = script_getstr(st,2);
  13809. second = script_getnum(st,3);
  13810. sd->progressbar.npc_id = st->oid;
  13811. sd->progressbar.timeout = gettick() + second*1000;
  13812. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  13813. return 0;
  13814. }
  13815. BUILDIN_FUNC(pushpc)
  13816. {
  13817. int direction, cells, dx, dy;
  13818. struct map_session_data* sd;
  13819. if((sd = script_rid2sd(st))==NULL)
  13820. {
  13821. return 0;
  13822. }
  13823. direction = script_getnum(st,2);
  13824. cells = script_getnum(st,3);
  13825. if(direction<0 || direction>7)
  13826. {
  13827. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  13828. script_reportsrc(st);
  13829. direction%= 8; // trim spin-over
  13830. }
  13831. if(!cells)
  13832. {// zero distance
  13833. return 0;
  13834. }
  13835. else if(cells<0)
  13836. {// pushing backwards
  13837. direction = (direction+4)%8; // turn around
  13838. cells = -cells;
  13839. }
  13840. dx = dirx[direction];
  13841. dy = diry[direction];
  13842. unit_blown(&sd->bl, dx, dy, cells, 0);
  13843. return 0;
  13844. }
  13845. /// Invokes buying store preparation window
  13846. /// buyingstore <slots>;
  13847. BUILDIN_FUNC(buyingstore)
  13848. {
  13849. struct map_session_data* sd;
  13850. if( ( sd = script_rid2sd(st) ) == NULL )
  13851. {
  13852. return 0;
  13853. }
  13854. buyingstore_setup(sd, script_getnum(st,2));
  13855. return 0;
  13856. }
  13857. /// Invokes search store info window
  13858. /// searchstores <uses>,<effect>;
  13859. BUILDIN_FUNC(searchstores)
  13860. {
  13861. unsigned short effect;
  13862. unsigned int uses;
  13863. struct map_session_data* sd;
  13864. if( ( sd = script_rid2sd(st) ) == NULL )
  13865. {
  13866. return 0;
  13867. }
  13868. uses = script_getnum(st,2);
  13869. effect = script_getnum(st,3);
  13870. if( !uses )
  13871. {
  13872. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  13873. return 1;
  13874. }
  13875. if( effect > 1 )
  13876. {
  13877. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  13878. return 1;
  13879. }
  13880. searchstore_open(sd, uses, effect);
  13881. return 0;
  13882. }
  13883. /// Displays a number as large digital clock.
  13884. /// showdigit <value>[,<type>];
  13885. BUILDIN_FUNC(showdigit)
  13886. {
  13887. unsigned int type = 0;
  13888. int value;
  13889. struct map_session_data* sd;
  13890. if( ( sd = script_rid2sd(st) ) == NULL )
  13891. {
  13892. return 0;
  13893. }
  13894. value = script_getnum(st,2);
  13895. if( script_hasdata(st,3) )
  13896. {
  13897. type = script_getnum(st,3);
  13898. if( type > 3 )
  13899. {
  13900. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  13901. return 1;
  13902. }
  13903. }
  13904. clif_showdigit(sd, (unsigned char)type, value);
  13905. return 0;
  13906. }
  13907. /**
  13908. * Rune Knight
  13909. **/
  13910. BUILDIN_FUNC(makerune) {
  13911. TBL_PC* sd;
  13912. if( (sd = script_rid2sd(st)) == NULL )
  13913. return 0;
  13914. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  13915. sd->itemid = script_getnum(st,2);
  13916. return 0;
  13917. }
  13918. /**
  13919. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  13920. **/
  13921. BUILDIN_FUNC(checkdragon) {
  13922. TBL_PC* sd;
  13923. if( (sd = script_rid2sd(st)) == NULL )
  13924. return 0;
  13925. if( sd->sc.option&OPTION_DRAGON )
  13926. script_pushint(st,1);
  13927. else
  13928. script_pushint(st,0);
  13929. return 0;
  13930. }
  13931. /**
  13932. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  13933. * - Toggles the dragon on a RK if he can mount;
  13934. * @param Color - when not provided uses the green dragon;
  13935. * - 1 : Green Dragon
  13936. * - 2 : Brown Dragon
  13937. * - 3 : Gray Dragon
  13938. * - 4 : Blue Dragon
  13939. * - 5 : Red Dragon
  13940. **/
  13941. BUILDIN_FUNC(setdragon) {
  13942. TBL_PC* sd;
  13943. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  13944. unsigned int option = OPTION_DRAGON1;
  13945. if( (sd = script_rid2sd(st)) == NULL )
  13946. return 0;
  13947. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  13948. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  13949. else if ( sd->sc.option&OPTION_DRAGON ) {//Is mounted; release
  13950. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  13951. script_pushint(st,1);
  13952. } else {//Not mounted; Mount now.
  13953. if( color ) {
  13954. option = ( color == 1 ? OPTION_DRAGON1 :
  13955. color == 2 ? OPTION_DRAGON2 :
  13956. color == 3 ? OPTION_DRAGON3 :
  13957. color == 4 ? OPTION_DRAGON4 :
  13958. color == 5 ? OPTION_DRAGON5 : 0);
  13959. if( !option ) {
  13960. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  13961. option = OPTION_DRAGON1;
  13962. }
  13963. }
  13964. pc_setoption(sd, sd->sc.option|option);
  13965. script_pushint(st,1);
  13966. }
  13967. return 0;
  13968. }
  13969. /**
  13970. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  13971. **/
  13972. BUILDIN_FUNC(ismounting) {
  13973. TBL_PC* sd;
  13974. if( (sd = script_rid2sd(st)) == NULL )
  13975. return 0;
  13976. if( sd->sc.option&OPTION_MOUNTING )
  13977. script_pushint(st,1);
  13978. else
  13979. script_pushint(st,0);
  13980. return 0;
  13981. }
  13982. /**
  13983. * setmounting() returns 1 on success or 0 otherwise
  13984. * - Toggles new mounts on a player when he can mount
  13985. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  13986. * - Will unmount the player is he is already mounting
  13987. **/
  13988. BUILDIN_FUNC(setmounting) {
  13989. TBL_PC* sd;
  13990. if( (sd = script_rid2sd(st)) == NULL )
  13991. return 0;
  13992. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  13993. script_pushint(st,0);//can't mount with one of these
  13994. else {
  13995. if( sd->sc.option&OPTION_MOUNTING )
  13996. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  13997. else
  13998. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  13999. script_pushint(st,1);//in both cases, return 1.
  14000. }
  14001. return 0;
  14002. }
  14003. /**
  14004. * Retrieves quantity of arguments provided to callfunc/callsub.
  14005. * getargcount() -> amount of arguments received in a function
  14006. **/
  14007. BUILDIN_FUNC(getargcount) {
  14008. struct script_retinfo* ri;
  14009. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  14010. ShowError("script:getargcount: used out of function or callsub label!\n");
  14011. st->state = END;
  14012. return 1;
  14013. }
  14014. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  14015. script_pushint(st, ri->nargs);
  14016. return 0;
  14017. }
  14018. /**
  14019. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  14020. **/
  14021. BUILDIN_FUNC(is_function) {
  14022. const char* str = script_getstr(st,2);
  14023. if( strdb_exists(userfunc_db, str) )
  14024. script_pushint(st,1);
  14025. else
  14026. script_pushint(st,0);
  14027. return 0;
  14028. }
  14029. /**
  14030. * get_revision() -> retrieves the current svn revision (if available)
  14031. **/
  14032. BUILDIN_FUNC(get_revision) {
  14033. const char * revision;
  14034. if ( (revision = get_svn_revision()) != 0 )
  14035. script_pushint(st,atoi(revision));
  14036. else
  14037. script_pushint(st,-1);//unknown
  14038. return 0;
  14039. }
  14040. /**
  14041. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  14042. **/
  14043. BUILDIN_FUNC(freeloop) {
  14044. if( script_getnum(st,2) )
  14045. st->freeloop = 1;
  14046. else
  14047. st->freeloop = 0;
  14048. script_pushint(st, st->freeloop);
  14049. return 0;
  14050. }
  14051. // declarations that were supposed to be exported from npc_chat.c
  14052. #ifdef PCRE_SUPPORT
  14053. BUILDIN_FUNC(defpattern);
  14054. BUILDIN_FUNC(activatepset);
  14055. BUILDIN_FUNC(deactivatepset);
  14056. BUILDIN_FUNC(deletepset);
  14057. #endif
  14058. /// script command definitions
  14059. /// for an explanation on args, see add_buildin_func
  14060. struct script_function buildin_func[] = {
  14061. // NPC interaction
  14062. BUILDIN_DEF(mes,"s*"),
  14063. BUILDIN_DEF(next,""),
  14064. BUILDIN_DEF(close,""),
  14065. BUILDIN_DEF(close2,""),
  14066. BUILDIN_DEF(menu,"sl*"),
  14067. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  14068. BUILDIN_DEF(prompt,"s*"),
  14069. //
  14070. BUILDIN_DEF(goto,"l"),
  14071. BUILDIN_DEF(callsub,"l*"),
  14072. BUILDIN_DEF(callfunc,"s*"),
  14073. BUILDIN_DEF(return,"?"),
  14074. BUILDIN_DEF(getarg,"i?"),
  14075. BUILDIN_DEF(jobchange,"i?"),
  14076. BUILDIN_DEF(jobname,"i"),
  14077. BUILDIN_DEF(input,"r??"),
  14078. BUILDIN_DEF(warp,"sii"),
  14079. BUILDIN_DEF(areawarp,"siiiisii"),
  14080. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  14081. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  14082. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  14083. BUILDIN_DEF(setlook,"ii"),
  14084. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  14085. BUILDIN_DEF(set,"rv"),
  14086. BUILDIN_DEF(setarray,"rv*"),
  14087. BUILDIN_DEF(cleararray,"rvi"),
  14088. BUILDIN_DEF(copyarray,"rri"),
  14089. BUILDIN_DEF(getarraysize,"r"),
  14090. BUILDIN_DEF(deletearray,"r?"),
  14091. BUILDIN_DEF(getelementofarray,"ri"),
  14092. BUILDIN_DEF(getitem,"vi?"),
  14093. BUILDIN_DEF(rentitem,"vi"),
  14094. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  14095. BUILDIN_DEF(getnameditem,"vv"),
  14096. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  14097. BUILDIN_DEF(makeitem,"visii"),
  14098. BUILDIN_DEF(delitem,"vi?"),
  14099. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  14100. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  14101. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  14102. BUILDIN_DEF(cutin,"si"),
  14103. BUILDIN_DEF(viewpoint,"iiiii"),
  14104. BUILDIN_DEF(heal,"ii"),
  14105. BUILDIN_DEF(itemheal,"ii"),
  14106. BUILDIN_DEF(percentheal,"ii"),
  14107. BUILDIN_DEF(rand,"i?"),
  14108. BUILDIN_DEF(countitem,"v"),
  14109. BUILDIN_DEF(countitem2,"viiiiiii"),
  14110. BUILDIN_DEF(checkweight,"vi"),
  14111. BUILDIN_DEF(readparam,"i?"),
  14112. BUILDIN_DEF(getcharid,"i?"),
  14113. BUILDIN_DEF(getnpcid,"i?"),
  14114. BUILDIN_DEF(getpartyname,"i"),
  14115. BUILDIN_DEF(getpartymember,"i?"),
  14116. BUILDIN_DEF(getpartyleader,"i?"),
  14117. BUILDIN_DEF(getguildname,"i"),
  14118. BUILDIN_DEF(getguildmaster,"i"),
  14119. BUILDIN_DEF(getguildmasterid,"i"),
  14120. BUILDIN_DEF(strcharinfo,"i"),
  14121. BUILDIN_DEF(strnpcinfo,"i"),
  14122. BUILDIN_DEF(getequipid,"i"),
  14123. BUILDIN_DEF(getequipname,"i"),
  14124. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  14125. BUILDIN_DEF(repair,"i"), // [Valaris]
  14126. BUILDIN_DEF(getequipisequiped,"i"),
  14127. BUILDIN_DEF(getequipisenableref,"i"),
  14128. BUILDIN_DEF(getequipisidentify,"i"),
  14129. BUILDIN_DEF(getequiprefinerycnt,"i"),
  14130. BUILDIN_DEF(getequipweaponlv,"i"),
  14131. BUILDIN_DEF(getequippercentrefinery,"i"),
  14132. BUILDIN_DEF(successrefitem,"i"),
  14133. BUILDIN_DEF(failedrefitem,"i"),
  14134. BUILDIN_DEF(statusup,"i"),
  14135. BUILDIN_DEF(statusup2,"ii"),
  14136. BUILDIN_DEF(bonus,"iv"),
  14137. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  14138. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  14139. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  14140. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  14141. BUILDIN_DEF(autobonus,"sii??"),
  14142. BUILDIN_DEF(autobonus2,"sii??"),
  14143. BUILDIN_DEF(autobonus3,"siiv?"),
  14144. BUILDIN_DEF(skill,"vi?"),
  14145. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  14146. BUILDIN_DEF(guildskill,"vi"),
  14147. BUILDIN_DEF(getskilllv,"v"),
  14148. BUILDIN_DEF(getgdskilllv,"iv"),
  14149. BUILDIN_DEF(basicskillcheck,""),
  14150. BUILDIN_DEF(getgmlevel,""),
  14151. BUILDIN_DEF(getgroupid,""),
  14152. BUILDIN_DEF(end,""),
  14153. BUILDIN_DEF(checkoption,"i"),
  14154. BUILDIN_DEF(setoption,"i?"),
  14155. BUILDIN_DEF(setcart,"?"),
  14156. BUILDIN_DEF(checkcart,""),
  14157. BUILDIN_DEF(setfalcon,"?"),
  14158. BUILDIN_DEF(checkfalcon,""),
  14159. BUILDIN_DEF(setriding,"?"),
  14160. BUILDIN_DEF(checkriding,""),
  14161. BUILDIN_DEF2(savepoint,"save","sii"),
  14162. BUILDIN_DEF(savepoint,"sii"),
  14163. BUILDIN_DEF(gettimetick,"i"),
  14164. BUILDIN_DEF(gettime,"i"),
  14165. BUILDIN_DEF(gettimestr,"si"),
  14166. BUILDIN_DEF(openstorage,""),
  14167. BUILDIN_DEF(guildopenstorage,""),
  14168. BUILDIN_DEF(itemskill,"vi"),
  14169. BUILDIN_DEF(produce,"i"),
  14170. BUILDIN_DEF(cooking,"i"),
  14171. BUILDIN_DEF(monster,"siisii?"),
  14172. BUILDIN_DEF(getmobdrops,"i"),
  14173. BUILDIN_DEF(areamonster,"siiiisii?"),
  14174. BUILDIN_DEF(killmonster,"ss?"),
  14175. BUILDIN_DEF(killmonsterall,"s?"),
  14176. BUILDIN_DEF(clone,"siisi????"),
  14177. BUILDIN_DEF(doevent,"s"),
  14178. BUILDIN_DEF(donpcevent,"s"),
  14179. BUILDIN_DEF(cmdothernpc,"ss"),
  14180. BUILDIN_DEF(addtimer,"is"),
  14181. BUILDIN_DEF(deltimer,"s"),
  14182. BUILDIN_DEF(addtimercount,"si"),
  14183. BUILDIN_DEF(initnpctimer,"??"),
  14184. BUILDIN_DEF(stopnpctimer,"??"),
  14185. BUILDIN_DEF(startnpctimer,"??"),
  14186. BUILDIN_DEF(setnpctimer,"i?"),
  14187. BUILDIN_DEF(getnpctimer,"i?"),
  14188. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  14189. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  14190. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  14191. BUILDIN_DEF(announce,"si?????"),
  14192. BUILDIN_DEF(mapannounce,"ssi?????"),
  14193. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  14194. BUILDIN_DEF(getusers,"i"),
  14195. BUILDIN_DEF(getmapguildusers,"si"),
  14196. BUILDIN_DEF(getmapusers,"s"),
  14197. BUILDIN_DEF(getareausers,"siiii"),
  14198. BUILDIN_DEF(getareadropitem,"siiiiv"),
  14199. BUILDIN_DEF(enablenpc,"s"),
  14200. BUILDIN_DEF(disablenpc,"s"),
  14201. BUILDIN_DEF(hideoffnpc,"s"),
  14202. BUILDIN_DEF(hideonnpc,"s"),
  14203. BUILDIN_DEF(sc_start,"iii?"),
  14204. BUILDIN_DEF(sc_start2,"iiii?"),
  14205. BUILDIN_DEF(sc_start4,"iiiiii?"),
  14206. BUILDIN_DEF(sc_end,"i?"),
  14207. BUILDIN_DEF(getstatus, "i?"),
  14208. BUILDIN_DEF(getscrate,"ii?"),
  14209. BUILDIN_DEF(debugmes,"s"),
  14210. BUILDIN_DEF2(catchpet,"pet","i"),
  14211. BUILDIN_DEF2(birthpet,"bpet",""),
  14212. BUILDIN_DEF(resetlvl,"i"),
  14213. BUILDIN_DEF(resetstatus,""),
  14214. BUILDIN_DEF(resetskill,""),
  14215. BUILDIN_DEF(skillpointcount,""),
  14216. BUILDIN_DEF(changebase,"i?"),
  14217. BUILDIN_DEF(changesex,""),
  14218. BUILDIN_DEF(waitingroom,"si?????"),
  14219. BUILDIN_DEF(delwaitingroom,"?"),
  14220. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  14221. BUILDIN_DEF(enablewaitingroomevent,"?"),
  14222. BUILDIN_DEF(disablewaitingroomevent,"?"),
  14223. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  14224. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  14225. BUILDIN_DEF(getwaitingroomstate,"i?"),
  14226. BUILDIN_DEF(warpwaitingpc,"sii?"),
  14227. BUILDIN_DEF(attachrid,"i"),
  14228. BUILDIN_DEF(detachrid,""),
  14229. BUILDIN_DEF(isloggedin,"i?"),
  14230. BUILDIN_DEF(setmapflagnosave,"ssii"),
  14231. BUILDIN_DEF(getmapflag,"si"),
  14232. BUILDIN_DEF(setmapflag,"si?"),
  14233. BUILDIN_DEF(removemapflag,"si?"),
  14234. BUILDIN_DEF(pvpon,"s"),
  14235. BUILDIN_DEF(pvpoff,"s"),
  14236. BUILDIN_DEF(gvgon,"s"),
  14237. BUILDIN_DEF(gvgoff,"s"),
  14238. BUILDIN_DEF(emotion,"i??"),
  14239. BUILDIN_DEF(maprespawnguildid,"sii"),
  14240. BUILDIN_DEF(agitstart,""), // <Agit>
  14241. BUILDIN_DEF(agitend,""),
  14242. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  14243. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  14244. BUILDIN_DEF(getcastlename,"s"),
  14245. BUILDIN_DEF(getcastledata,"si"),
  14246. BUILDIN_DEF(setcastledata,"sii"),
  14247. BUILDIN_DEF(requestguildinfo,"i?"),
  14248. BUILDIN_DEF(getequipcardcnt,"i"),
  14249. BUILDIN_DEF(successremovecards,"i"),
  14250. BUILDIN_DEF(failedremovecards,"ii"),
  14251. BUILDIN_DEF(marriage,"s"),
  14252. BUILDIN_DEF2(wedding_effect,"wedding",""),
  14253. BUILDIN_DEF(divorce,""),
  14254. BUILDIN_DEF(ispartneron,""),
  14255. BUILDIN_DEF(getpartnerid,""),
  14256. BUILDIN_DEF(getchildid,""),
  14257. BUILDIN_DEF(getmotherid,""),
  14258. BUILDIN_DEF(getfatherid,""),
  14259. BUILDIN_DEF(warppartner,"sii"),
  14260. BUILDIN_DEF(getitemname,"v"),
  14261. BUILDIN_DEF(getitemslots,"i"),
  14262. BUILDIN_DEF(makepet,"i"),
  14263. BUILDIN_DEF(getexp,"ii"),
  14264. BUILDIN_DEF(getinventorylist,""),
  14265. BUILDIN_DEF(getskilllist,""),
  14266. BUILDIN_DEF(clearitem,""),
  14267. BUILDIN_DEF(classchange,"ii"),
  14268. BUILDIN_DEF(misceffect,"i"),
  14269. BUILDIN_DEF(playBGM,"s"),
  14270. BUILDIN_DEF(playBGMall,"s?????"),
  14271. BUILDIN_DEF(soundeffect,"si"),
  14272. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  14273. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  14274. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  14275. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  14276. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  14277. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  14278. BUILDIN_DEF(petloot,"i"), // [Valaris]
  14279. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  14280. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  14281. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  14282. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  14283. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  14284. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  14285. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  14286. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  14287. BUILDIN_DEF(nude,""), // nude command [Valaris]
  14288. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  14289. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  14290. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  14291. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  14292. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  14293. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  14294. BUILDIN_DEF(mobcount,"ss"),
  14295. BUILDIN_DEF(getlook,"i"),
  14296. BUILDIN_DEF(getsavepoint,"i"),
  14297. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  14298. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  14299. BUILDIN_DEF(npcstop,""), // [Valaris]
  14300. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  14301. BUILDIN_DEF(checkoption1,"i"),
  14302. BUILDIN_DEF(checkoption2,"i"),
  14303. BUILDIN_DEF(guildgetexp,"i"),
  14304. BUILDIN_DEF(guildchangegm,"is"),
  14305. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  14306. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  14307. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  14308. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  14309. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  14310. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  14311. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  14312. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  14313. BUILDIN_DEF(night,""), // sets the server to night time
  14314. BUILDIN_DEF(day,""), // sets the server to day time
  14315. #ifdef PCRE_SUPPORT
  14316. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  14317. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  14318. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  14319. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  14320. #endif
  14321. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  14322. BUILDIN_DEF(getusersname,""),
  14323. BUILDIN_DEF(recovery,""),
  14324. BUILDIN_DEF(getpetinfo,"i"),
  14325. BUILDIN_DEF(gethominfo,"i"),
  14326. BUILDIN_DEF(getmercinfo,"i?"),
  14327. BUILDIN_DEF(checkequipedcard,"i"),
  14328. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  14329. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  14330. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  14331. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  14332. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  14333. BUILDIN_DEF(charat,"si"),
  14334. BUILDIN_DEF(setchar,"ssi"),
  14335. BUILDIN_DEF(insertchar,"ssi"),
  14336. BUILDIN_DEF(delchar,"si"),
  14337. BUILDIN_DEF(strtoupper,"s"),
  14338. BUILDIN_DEF(strtolower,"s"),
  14339. BUILDIN_DEF(charisupper, "si"),
  14340. BUILDIN_DEF(charislower, "si"),
  14341. BUILDIN_DEF(substr,"sii"),
  14342. BUILDIN_DEF(explode, "rss"),
  14343. BUILDIN_DEF(implode, "r?"),
  14344. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  14345. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  14346. BUILDIN_DEF(strpos,"ss?"),
  14347. BUILDIN_DEF(replacestr,"sss??"),
  14348. BUILDIN_DEF(countstr,"ss?"),
  14349. BUILDIN_DEF(setnpcdisplay,"sv??"),
  14350. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  14351. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  14352. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  14353. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  14354. // [zBuffer] List of mathematics commands --->
  14355. BUILDIN_DEF(sqrt,"i"),
  14356. BUILDIN_DEF(pow,"ii"),
  14357. BUILDIN_DEF(distance,"iiii"),
  14358. // <--- [zBuffer] List of mathematics commands
  14359. BUILDIN_DEF(md5,"s"),
  14360. // [zBuffer] List of dynamic var commands --->
  14361. BUILDIN_DEF(getd,"s"),
  14362. BUILDIN_DEF(setd,"sv"),
  14363. // <--- [zBuffer] List of dynamic var commands
  14364. BUILDIN_DEF(petstat,"i"),
  14365. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  14366. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  14367. BUILDIN_DEF(npcshopadditem,"sii*"),
  14368. BUILDIN_DEF(npcshopdelitem,"si*"),
  14369. BUILDIN_DEF(npcshopattach,"s?"),
  14370. BUILDIN_DEF(equip,"i"),
  14371. BUILDIN_DEF(autoequip,"ii"),
  14372. BUILDIN_DEF(setbattleflag,"si"),
  14373. BUILDIN_DEF(getbattleflag,"s"),
  14374. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  14375. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  14376. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  14377. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  14378. BUILDIN_DEF(axtoi,"s"),
  14379. BUILDIN_DEF(query_sql,"s*"),
  14380. BUILDIN_DEF(query_logsql,"s*"),
  14381. BUILDIN_DEF(escape_sql,"v"),
  14382. BUILDIN_DEF(atoi,"s"),
  14383. // [zBuffer] List of player cont commands --->
  14384. BUILDIN_DEF(rid2name,"i"),
  14385. BUILDIN_DEF(pcfollow,"ii"),
  14386. BUILDIN_DEF(pcstopfollow,"i"),
  14387. BUILDIN_DEF(pcblockmove,"ii"),
  14388. // <--- [zBuffer] List of player cont commands
  14389. // [zBuffer] List of mob control commands --->
  14390. BUILDIN_DEF(unitwalk,"ii?"),
  14391. BUILDIN_DEF(unitkill,"i"),
  14392. BUILDIN_DEF(unitwarp,"isii"),
  14393. BUILDIN_DEF(unitattack,"iv?"),
  14394. BUILDIN_DEF(unitstop,"i"),
  14395. BUILDIN_DEF(unittalk,"is"),
  14396. BUILDIN_DEF(unitemote,"ii"),
  14397. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  14398. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  14399. // <--- [zBuffer] List of mob control commands
  14400. BUILDIN_DEF(sleep,"i"),
  14401. BUILDIN_DEF(sleep2,"i"),
  14402. BUILDIN_DEF(awake,"s"),
  14403. BUILDIN_DEF(getvariableofnpc,"rs"),
  14404. BUILDIN_DEF(warpportal,"iisii"),
  14405. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  14406. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  14407. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  14408. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  14409. BUILDIN_DEF(checkvending,"?"),
  14410. BUILDIN_DEF(checkchatting,"?"),
  14411. BUILDIN_DEF(openmail,""),
  14412. BUILDIN_DEF(openauction,""),
  14413. BUILDIN_DEF(checkcell,"siii"),
  14414. BUILDIN_DEF(setcell,"siiiiii"),
  14415. BUILDIN_DEF(setwall,"siiiiis"),
  14416. BUILDIN_DEF(delwall,"s"),
  14417. BUILDIN_DEF(searchitem,"rs"),
  14418. BUILDIN_DEF(mercenary_create,"ii"),
  14419. BUILDIN_DEF(mercenary_heal,"ii"),
  14420. BUILDIN_DEF(mercenary_sc_start,"iii"),
  14421. BUILDIN_DEF(mercenary_get_calls,"i"),
  14422. BUILDIN_DEF(mercenary_get_faith,"i"),
  14423. BUILDIN_DEF(mercenary_set_calls,"ii"),
  14424. BUILDIN_DEF(mercenary_set_faith,"ii"),
  14425. BUILDIN_DEF(readbook,"ii"),
  14426. BUILDIN_DEF(setfont,"i"),
  14427. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  14428. BUILDIN_DEF(progressbar,"si"),
  14429. BUILDIN_DEF(pushpc,"ii"),
  14430. BUILDIN_DEF(buyingstore,"i"),
  14431. BUILDIN_DEF(searchstores,"ii"),
  14432. BUILDIN_DEF(showdigit,"i?"),
  14433. // WoE SE
  14434. BUILDIN_DEF(agitstart2,""),
  14435. BUILDIN_DEF(agitend2,""),
  14436. BUILDIN_DEF(agitcheck2,""),
  14437. // BattleGround
  14438. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  14439. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  14440. BUILDIN_DEF(bg_team_setxy,"iii"),
  14441. BUILDIN_DEF(bg_warp,"isii"),
  14442. BUILDIN_DEF(bg_monster,"isiisi?"),
  14443. BUILDIN_DEF(bg_monster_set_team,"ii"),
  14444. BUILDIN_DEF(bg_leave,""),
  14445. BUILDIN_DEF(bg_destroy,"i"),
  14446. BUILDIN_DEF(areapercentheal,"siiiiii"),
  14447. BUILDIN_DEF(bg_get_data,"ii"),
  14448. BUILDIN_DEF(bg_getareausers,"isiiii"),
  14449. BUILDIN_DEF(bg_updatescore,"sii"),
  14450. // Instancing
  14451. BUILDIN_DEF(instance_create,"si"),
  14452. BUILDIN_DEF(instance_destroy,"?"),
  14453. BUILDIN_DEF(instance_attachmap,"si?"),
  14454. BUILDIN_DEF(instance_detachmap,"s?"),
  14455. BUILDIN_DEF(instance_attach,"i"),
  14456. BUILDIN_DEF(instance_id,"?"),
  14457. BUILDIN_DEF(instance_set_timeout,"ii?"),
  14458. BUILDIN_DEF(instance_init,"i"),
  14459. BUILDIN_DEF(instance_announce,"isi?????"),
  14460. BUILDIN_DEF(instance_npcname,"s?"),
  14461. BUILDIN_DEF(has_instance,"s?"),
  14462. BUILDIN_DEF(instance_warpall,"sii?"),
  14463. /**
  14464. * 3rd-related
  14465. **/
  14466. BUILDIN_DEF(makerune,"i"),
  14467. BUILDIN_DEF(checkdragon,""),//[Ind]
  14468. BUILDIN_DEF(setdragon,"?"),//[Ind]
  14469. BUILDIN_DEF(ismounting,""),//[Ind]
  14470. BUILDIN_DEF(setmounting,""),//[Ind]
  14471. /**
  14472. * rAthena and beyond!
  14473. **/
  14474. BUILDIN_DEF(getargcount,""),
  14475. BUILDIN_DEF(is_function,"s"),
  14476. BUILDIN_DEF(get_revision,""),
  14477. BUILDIN_DEF(freeloop,"i"),
  14478. //Quest Log System [Inkfish]
  14479. BUILDIN_DEF(setquest, "i"),
  14480. BUILDIN_DEF(erasequest, "i"),
  14481. BUILDIN_DEF(completequest, "i"),
  14482. BUILDIN_DEF(checkquest, "i?"),
  14483. BUILDIN_DEF(changequest, "ii"),
  14484. BUILDIN_DEF(showevent, "ii"),
  14485. {NULL,NULL,NULL},
  14486. };