script.c 363 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944
  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/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/timer.h"
  16. #include "../common/utils.h"
  17. #include "map.h"
  18. #include "path.h"
  19. #include "clif.h"
  20. #include "chrif.h"
  21. #include "itemdb.h"
  22. #include "pc.h"
  23. #include "status.h"
  24. #include "storage.h"
  25. #include "mob.h"
  26. #include "npc.h"
  27. #include "pet.h"
  28. #include "mapreg.h"
  29. #include "homunculus.h"
  30. #include "instance.h"
  31. #include "mercenary.h"
  32. #include "intif.h"
  33. #include "skill.h"
  34. #include "status.h"
  35. #include "chat.h"
  36. #include "battle.h"
  37. #include "battleground.h"
  38. #include "party.h"
  39. #include "guild.h"
  40. #include "atcommand.h"
  41. #include "log.h"
  42. #include "unit.h"
  43. #include "pet.h"
  44. #include "mail.h"
  45. #include "script.h"
  46. #include "quest.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <math.h>
  51. #ifndef WIN32
  52. #include <sys/time.h>
  53. #endif
  54. #include <time.h>
  55. #include <setjmp.h>
  56. #include <errno.h>
  57. ///////////////////////////////////////////////////////////////////////////////
  58. //## TODO possible enhancements: [FlavioJS]
  59. // - 'callfunc' supporting labels in the current npc "::LabelName"
  60. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  61. // - 'function FuncName;' function declarations reverting to global functions
  62. // if local label isn't found
  63. // - join callfunc and callsub's functionality
  64. // - remove dynamic allocation in add_word()
  65. // - remove GETVALUE / SETVALUE
  66. // - clean up the set_reg / set_val / setd_sub mess
  67. //
  68. // struct script_state* st;
  69. //
  70. /// Returns the script_data at the target index
  71. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  72. /// Returns if the stack contains data at the target index
  73. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  74. /// Returns the index of the last data in the stack
  75. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  76. /// Pushes an int into the stack
  77. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  78. /// Pushes a string into the stack (script engine frees it automatically)
  79. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  80. /// Pushes a copy of a string into the stack
  81. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  82. /// Pushes a constant string into the stack (must never change or be freed)
  83. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  84. /// Pushes a nil into the stack
  85. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  86. /// Pushes a copy of the data in the target index
  87. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  88. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  89. #define script_isint(st,i) data_isint(script_getdata(st,i))
  90. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  91. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  92. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  93. // Note: "top" functions/defines use indexes relative to the top of the stack
  94. // -1 is the index of the data at the top
  95. /// Returns the script_data at the target index relative to the top of the stack
  96. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  97. /// Pushes a copy of the data in the target index relative to the top of the stack
  98. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  99. /// Removes the range of values [start,end[ relative to the top of the stack
  100. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  101. //
  102. // struct script_data* data;
  103. //
  104. /// Returns if the script data is a string
  105. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  106. /// Returns if the script data is an int
  107. #define data_isint(data) ( (data)->type == C_INT )
  108. /// Returns if the script data is a reference
  109. #define data_isreference(data) ( (data)->type == C_NAME )
  110. /// Returns if the script data is a label
  111. #define data_islabel(data) ( (data)->type == C_POS )
  112. /// Returns if the script data is an internal script function label
  113. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  114. /// Returns if this is a reference to a constant
  115. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  116. /// Returns if this a reference to a param
  117. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  118. /// Returns if this a reference to a variable
  119. //##TODO confirm it's C_NAME [FlavioJS]
  120. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  121. /// Returns the unique id of the reference (id and index)
  122. #define reference_getuid(data) ( (data)->u.num )
  123. /// Returns the id of the reference
  124. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  125. /// Returns the array index of the reference
  126. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  127. /// Returns the name of the reference
  128. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  129. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  130. #define reference_getref(data) ( (data)->ref )
  131. /// Returns the value of the constant
  132. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  133. /// Returns the type of param
  134. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  135. /// Composes the uid of a reference from the id and the index
  136. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  137. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  138. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  139. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  140. #define FETCH(n, t) \
  141. if( script_hasdata(st,n) ) \
  142. (t)=script_getnum(st,n);
  143. /// Maximum amount of elements in script arrays
  144. #define SCRIPT_MAX_ARRAYSIZE 128
  145. #define SCRIPT_BLOCK_SIZE 512
  146. enum { LABEL_NEXTLINE=1,LABEL_START };
  147. /// temporary buffer for passing around compiled bytecode
  148. /// @see add_scriptb, set_label, parse_script
  149. static unsigned char* script_buf = NULL;
  150. static int script_pos = 0, script_size = 0;
  151. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  152. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  153. // String buffer structures.
  154. // str_data stores string information
  155. static struct str_data_struct {
  156. enum c_op type;
  157. int str;
  158. int backpatch;
  159. int label;
  160. int (*func)(struct script_state *st);
  161. int val;
  162. int next;
  163. } *str_data = NULL;
  164. static int str_data_size = 0; // size of the data
  165. static int str_num = LABEL_START; // next id to be assigned
  166. // str_buf holds the strings themselves
  167. static char *str_buf;
  168. static int str_size = 0; // size of the buffer
  169. static int str_pos = 0; // next position to be assigned
  170. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  171. #define SCRIPT_HASH_SIZE 1021
  172. int str_hash[SCRIPT_HASH_SIZE];
  173. // Specifies which string hashing method to use
  174. //#define SCRIPT_HASH_DJB2
  175. //#define SCRIPT_HASH_SDBM
  176. #define SCRIPT_HASH_ELF
  177. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  178. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  179. static int parse_options=0;
  180. DBMap* script_get_label_db(){ return scriptlabel_db; }
  181. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  182. // Caches compiled autoscript item code.
  183. // Note: This is not cleared when reloading itemdb.
  184. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  185. struct Script_Config script_config = {
  186. 1, // warn_func_mismatch_argtypes
  187. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  188. 0, INT_MAX, // input_min_value/input_max_value
  189. "OnPCDieEvent", //die_event_name
  190. "OnPCKillEvent", //kill_pc_event_name
  191. "OnNPCKillEvent", //kill_mob_event_name
  192. "OnPCLoginEvent", //login_event_name
  193. "OnPCLogoutEvent", //logout_event_name
  194. "OnPCLoadMapEvent", //loadmap_event_name
  195. "OnPCBaseLvUpEvent", //baselvup_event_name
  196. "OnPCJobLvUpEvent", //joblvup_event_name
  197. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  198. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  199. };
  200. static jmp_buf error_jump;
  201. static char* error_msg;
  202. static const char* error_pos;
  203. static int error_report; // if the error should produce output
  204. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  205. // [Eoe / jA 1080, 1081, 1094, 1164]
  206. enum curly_type {
  207. TYPE_NULL = 0,
  208. TYPE_IF,
  209. TYPE_SWITCH,
  210. TYPE_WHILE,
  211. TYPE_FOR,
  212. TYPE_DO,
  213. TYPE_USERFUNC,
  214. TYPE_ARGLIST // function argument list
  215. };
  216. #define ARGLIST_UNDEFINED 0
  217. #define ARGLIST_NO_PAREN 1
  218. #define ARGLIST_PAREN 2
  219. static struct {
  220. struct {
  221. enum curly_type type;
  222. int index;
  223. int count;
  224. int flag;
  225. struct linkdb_node *case_label;
  226. } curly[256]; // 右カッコの情報
  227. int curly_count; // 右カッコの数
  228. int index; // スクリプト内で使用した構文の数
  229. } syntax;
  230. const char* parse_curly_close(const char* p);
  231. const char* parse_syntax_close(const char* p);
  232. const char* parse_syntax_close_sub(const char* p,int* flag);
  233. const char* parse_syntax(const char* p);
  234. static int parse_syntax_for_flag = 0;
  235. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  236. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  237. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  238. int potion_target=0;
  239. c_op get_com(unsigned char *script,int *pos);
  240. int get_num(unsigned char *script,int *pos);
  241. typedef struct script_function {
  242. int (*func)(struct script_state *st);
  243. const char *name;
  244. const char *arg;
  245. } script_function;
  246. extern script_function buildin_func[];
  247. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  248. /*==========================================
  249. * ローカルプロトタイプ宣言 (必要な物のみ)
  250. *------------------------------------------*/
  251. const char* parse_subexpr(const char* p,int limit);
  252. int run_func(struct script_state *st);
  253. enum {
  254. MF_NOMEMO, //0
  255. MF_NOTELEPORT,
  256. MF_NOSAVE,
  257. MF_NOBRANCH,
  258. MF_NOPENALTY,
  259. MF_NOZENYPENALTY,
  260. MF_PVP,
  261. MF_PVP_NOPARTY,
  262. MF_PVP_NOGUILD,
  263. MF_GVG,
  264. MF_GVG_NOPARTY, //10
  265. MF_NOTRADE,
  266. MF_NOSKILL,
  267. MF_NOWARP,
  268. MF_PARTYLOCK,
  269. MF_NOICEWALL,
  270. MF_SNOW,
  271. MF_FOG,
  272. MF_SAKURA,
  273. MF_LEAVES,
  274. MF_RAIN, //20
  275. // 21 free
  276. MF_NOGO = 22,
  277. MF_CLOUDS,
  278. MF_CLOUDS2,
  279. MF_FIREWORKS,
  280. MF_GVG_CASTLE,
  281. MF_GVG_DUNGEON,
  282. MF_NIGHTENABLED,
  283. MF_NOBASEEXP,
  284. MF_NOJOBEXP, //30
  285. MF_NOMOBLOOT,
  286. MF_NOMVPLOOT,
  287. MF_NORETURN,
  288. MF_NOWARPTO,
  289. MF_NIGHTMAREDROP,
  290. MF_RESTRICTED,
  291. MF_NOCOMMAND,
  292. MF_NODROP,
  293. MF_JEXP,
  294. MF_BEXP, //40
  295. MF_NOVENDING,
  296. MF_LOADEVENT,
  297. MF_NOCHAT,
  298. MF_NOEXPPENALTY,
  299. MF_GUILDLOCK,
  300. MF_TOWN,
  301. MF_AUTOTRADE,
  302. MF_ALLOWKS,
  303. MF_MONSTER_NOTELEPORT,
  304. MF_PVP_NOCALCRANK, //50
  305. MF_BATTLEGROUND
  306. };
  307. const char* script_op2name(int op)
  308. {
  309. #define RETURN_OP_NAME(type) case type: return #type
  310. switch( op )
  311. {
  312. RETURN_OP_NAME(C_NOP);
  313. RETURN_OP_NAME(C_POS);
  314. RETURN_OP_NAME(C_INT);
  315. RETURN_OP_NAME(C_PARAM);
  316. RETURN_OP_NAME(C_FUNC);
  317. RETURN_OP_NAME(C_STR);
  318. RETURN_OP_NAME(C_CONSTSTR);
  319. RETURN_OP_NAME(C_ARG);
  320. RETURN_OP_NAME(C_NAME);
  321. RETURN_OP_NAME(C_EOL);
  322. RETURN_OP_NAME(C_RETINFO);
  323. RETURN_OP_NAME(C_USERFUNC);
  324. RETURN_OP_NAME(C_USERFUNC_POS);
  325. // operators
  326. RETURN_OP_NAME(C_OP3);
  327. RETURN_OP_NAME(C_LOR);
  328. RETURN_OP_NAME(C_LAND);
  329. RETURN_OP_NAME(C_LE);
  330. RETURN_OP_NAME(C_LT);
  331. RETURN_OP_NAME(C_GE);
  332. RETURN_OP_NAME(C_GT);
  333. RETURN_OP_NAME(C_EQ);
  334. RETURN_OP_NAME(C_NE);
  335. RETURN_OP_NAME(C_XOR);
  336. RETURN_OP_NAME(C_OR);
  337. RETURN_OP_NAME(C_AND);
  338. RETURN_OP_NAME(C_ADD);
  339. RETURN_OP_NAME(C_SUB);
  340. RETURN_OP_NAME(C_MUL);
  341. RETURN_OP_NAME(C_DIV);
  342. RETURN_OP_NAME(C_MOD);
  343. RETURN_OP_NAME(C_NEG);
  344. RETURN_OP_NAME(C_LNOT);
  345. RETURN_OP_NAME(C_NOT);
  346. RETURN_OP_NAME(C_R_SHIFT);
  347. RETURN_OP_NAME(C_L_SHIFT);
  348. default:
  349. ShowDebug("script_op2name: unexpected op=%d\n", op);
  350. return "???";
  351. }
  352. #undef RETURN_OP_NAME
  353. }
  354. #ifdef DEBUG_DUMP_STACK
  355. static void script_dump_stack(struct script_state* st)
  356. {
  357. int i;
  358. ShowMessage("\tstart = %d\n", st->start);
  359. ShowMessage("\tend = %d\n", st->end);
  360. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  361. ShowMessage("\tsp = %d\n", st->stack->sp);
  362. for( i = 0; i < st->stack->sp; ++i )
  363. {
  364. struct script_data* data = &st->stack->stack_data[i];
  365. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  366. switch( data->type )
  367. {
  368. case C_INT:
  369. case C_POS:
  370. ShowMessage(" %d\n", data->u.num);
  371. break;
  372. case C_STR:
  373. case C_CONSTSTR:
  374. ShowMessage(" \"%s\"\n", data->u.str);
  375. break;
  376. case C_NAME:
  377. 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));
  378. break;
  379. case C_RETINFO:
  380. {
  381. struct script_retinfo* ri = data->u.ri;
  382. 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);
  383. }
  384. break;
  385. default:
  386. ShowMessage("\n");
  387. break;
  388. }
  389. }
  390. }
  391. #endif
  392. /// Reports on the console the src of a script error.
  393. static void script_reportsrc(struct script_state *st)
  394. {
  395. struct block_list* bl;
  396. if( st->oid == 0 )
  397. return; //Can't report source.
  398. bl = map_id2bl(st->oid);
  399. if( bl == NULL )
  400. return;
  401. switch( bl->type )
  402. {
  403. case BL_NPC:
  404. if( bl->m >= 0 )
  405. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  406. else
  407. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  408. break;
  409. default:
  410. if( bl->m >= 0 )
  411. 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);
  412. else
  413. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  414. break;
  415. }
  416. }
  417. /// Reports on the console information about the script data.
  418. static void script_reportdata(struct script_data* data)
  419. {
  420. if( data == NULL )
  421. return;
  422. switch( data->type )
  423. {
  424. case C_NOP:// no value
  425. ShowDebug("Data: nothing (nil)\n");
  426. break;
  427. case C_INT:// number
  428. ShowDebug("Data: number value=%d\n", data->u.num);
  429. break;
  430. case C_STR:
  431. case C_CONSTSTR:// string
  432. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  433. break;
  434. case C_NAME:// reference
  435. if( reference_tovariable(data) )
  436. {// variable
  437. const char* name = reference_getname(data);
  438. if( not_array_variable(*name) )
  439. ShowDebug("Data: variable name='%s'\n", name);
  440. else
  441. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  442. }
  443. else if( reference_toconstant(data) )
  444. {// constant
  445. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  446. }
  447. else if( reference_toparam(data) )
  448. {// param
  449. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  450. }
  451. else
  452. {// ???
  453. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  454. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  455. }
  456. break;
  457. case C_POS:// label
  458. ShowDebug("Data: label pos=%d\n", data->u.num);
  459. break;
  460. default:
  461. ShowDebug("Data: %s\n", script_op2name(data->type));
  462. break;
  463. }
  464. }
  465. /// Reports on the console information about the current built-in function.
  466. static void script_reportfunc(struct script_state* st)
  467. {
  468. int i, params, id;
  469. struct script_data* data;
  470. if( !script_hasdata(st,0) )
  471. {// no stack
  472. return;
  473. }
  474. data = script_getdata(st,0);
  475. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  476. {// script currently not executing a built-in function or corrupt stack
  477. return;
  478. }
  479. id = reference_getid(data);
  480. params = script_lastdata(st)-1;
  481. if( params > 0 )
  482. {
  483. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  484. for( i = 2; i <= script_lastdata(st); i++ )
  485. {
  486. script_reportdata(script_getdata(st,i));
  487. }
  488. }
  489. else
  490. {
  491. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  492. }
  493. }
  494. /*==========================================
  495. * エラーメッセージ出力
  496. *------------------------------------------*/
  497. static void disp_error_message2(const char *mes,const char *pos,int report)
  498. {
  499. error_msg = aStrdup(mes);
  500. error_pos = pos;
  501. error_report = report;
  502. longjmp( error_jump, 1 );
  503. }
  504. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  505. /// Checks event parameter validity
  506. static void check_event(struct script_state *st, const char *evt)
  507. {
  508. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  509. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  510. script_reportsrc(st);
  511. }
  512. }
  513. /*==========================================
  514. * Hashes the input string
  515. *------------------------------------------*/
  516. static unsigned int calc_hash(const char* p)
  517. {
  518. unsigned int h;
  519. #if defined(SCRIPT_HASH_DJB2)
  520. h = 5381;
  521. while( *p ) // hash*33 + c
  522. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  523. #elif defined(SCRIPT_HASH_SDBM)
  524. h = 0;
  525. while( *p ) // hash*65599 + c
  526. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  527. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  528. h = 0;
  529. while( *p ){
  530. unsigned int g;
  531. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  532. g = h & 0xF0000000;
  533. if( g )
  534. {
  535. h ^= g >> 24;
  536. h &= ~g;
  537. }
  538. }
  539. #else // athena hash
  540. h = 0;
  541. while( *p )
  542. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  543. #endif
  544. return h % SCRIPT_HASH_SIZE;
  545. }
  546. /*==========================================
  547. * str_data manipulation functions
  548. *------------------------------------------*/
  549. /// Looks up string using the provided id.
  550. const char* get_str(int id)
  551. {
  552. Assert( id >= LABEL_START && id < str_size );
  553. return str_buf+str_data[id].str;
  554. }
  555. /// Returns the uid of the string, or -1.
  556. static int search_str(const char* p)
  557. {
  558. int i;
  559. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  560. if( strcasecmp(get_str(i),p) == 0 )
  561. return i;
  562. return -1;
  563. }
  564. /// Stores a copy of the string and returns its id.
  565. /// If an identical string is already present, returns its id instead.
  566. int add_str(const char* p)
  567. {
  568. int i, h;
  569. int len;
  570. h = calc_hash(p);
  571. if( str_hash[h] == 0 )
  572. {// empty bucket, add new node here
  573. str_hash[h] = str_num;
  574. }
  575. else
  576. {// scan for end of list, or occurence of identical string
  577. for( i = str_hash[h]; ; i = str_data[i].next )
  578. {
  579. if( strcasecmp(get_str(i),p) == 0 )
  580. return i; // string already in list
  581. if( str_data[i].next == 0 )
  582. break; // reached the end
  583. }
  584. // append node to end of list
  585. str_data[i].next = str_num;
  586. }
  587. // grow list if neccessary
  588. if( str_num >= str_data_size )
  589. {
  590. str_data_size += 128;
  591. RECREATE(str_data,struct str_data_struct,str_data_size);
  592. memset(str_data + (str_data_size - 128), '\0', 128);
  593. }
  594. len=(int)strlen(p);
  595. // grow string buffer if neccessary
  596. while( str_pos+len+1 >= str_size )
  597. {
  598. str_size += 256;
  599. RECREATE(str_buf,char,str_size);
  600. memset(str_buf + (str_size - 256), '\0', 256);
  601. }
  602. safestrncpy(str_buf+str_pos, p, len+1);
  603. str_data[str_num].type = C_NOP;
  604. str_data[str_num].str = str_pos;
  605. str_data[str_num].next = 0;
  606. str_data[str_num].func = NULL;
  607. str_data[str_num].backpatch = -1;
  608. str_data[str_num].label = -1;
  609. str_pos += len+1;
  610. return str_num++;
  611. }
  612. /// Appends 1 byte to the script buffer.
  613. static void add_scriptb(int a)
  614. {
  615. if( script_pos+1 >= script_size )
  616. {
  617. script_size += SCRIPT_BLOCK_SIZE;
  618. RECREATE(script_buf,unsigned char,script_size);
  619. }
  620. script_buf[script_pos++] = (uint8)(a);
  621. }
  622. /// Appends a c_op value to the script buffer.
  623. /// The value is variable-length encoded into 8-bit blocks.
  624. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  625. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  626. static void add_scriptc(int a)
  627. {
  628. while( a >= 0x40 )
  629. {
  630. add_scriptb((a&0x3f)|0x40);
  631. a = (a - 0x40) >> 6;
  632. }
  633. add_scriptb(a);
  634. }
  635. /// Appends an integer value to the script buffer.
  636. /// The value is variable-length encoded into 8-bit blocks.
  637. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  638. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  639. static void add_scripti(int a)
  640. {
  641. while( a >= 0x40 )
  642. {
  643. add_scriptb((a&0x3f)|0xc0);
  644. a = (a - 0x40) >> 6;
  645. }
  646. add_scriptb(a|0x80);
  647. }
  648. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  649. ///
  650. /// @param l The id of the str_data entry
  651. // 最大16Mまで
  652. static void add_scriptl(int l)
  653. {
  654. int backpatch = str_data[l].backpatch;
  655. switch(str_data[l].type){
  656. case C_POS:
  657. case C_USERFUNC_POS:
  658. add_scriptc(C_POS);
  659. add_scriptb(str_data[l].label);
  660. add_scriptb(str_data[l].label>>8);
  661. add_scriptb(str_data[l].label>>16);
  662. break;
  663. case C_NOP:
  664. case C_USERFUNC:
  665. // ラベルの可能性があるのでbackpatch用データ埋め込み
  666. add_scriptc(C_NAME);
  667. str_data[l].backpatch = script_pos;
  668. add_scriptb(backpatch);
  669. add_scriptb(backpatch>>8);
  670. add_scriptb(backpatch>>16);
  671. break;
  672. case C_INT:
  673. add_scripti(abs(str_data[l].val));
  674. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  675. add_scriptc(C_NEG);
  676. break;
  677. default: // assume C_NAME
  678. add_scriptc(C_NAME);
  679. add_scriptb(l);
  680. add_scriptb(l>>8);
  681. add_scriptb(l>>16);
  682. break;
  683. }
  684. }
  685. /*==========================================
  686. * ラベルを解決する
  687. *------------------------------------------*/
  688. void set_label(int l,int pos, const char* script_pos)
  689. {
  690. int i,next;
  691. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  692. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  693. disp_error_message("set_label: invalid label name",script_pos);
  694. return;
  695. }
  696. if(str_data[l].label!=-1){
  697. disp_error_message("set_label: dup label ",script_pos);
  698. return;
  699. }
  700. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  701. str_data[l].label=pos;
  702. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  703. next=GETVALUE(script_buf,i);
  704. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  705. SETVALUE(script_buf,i,pos);
  706. i=next;
  707. }
  708. }
  709. /// Skips spaces and/or comments.
  710. const char* skip_space(const char* p)
  711. {
  712. if( p == NULL )
  713. return NULL;
  714. for(;;)
  715. {
  716. while( ISSPACE(*p) )
  717. ++p;
  718. if( *p == '/' && p[1] == '/' )
  719. {// line comment
  720. while(*p && *p!='\n')
  721. ++p;
  722. }
  723. else if( *p == '/' && p[1] == '*' )
  724. {// block comment
  725. p += 2;
  726. for(;;)
  727. {
  728. if( *p == '\0' )
  729. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  730. if( *p == '*' && p[1] == '/' )
  731. {// end of block comment
  732. p += 2;
  733. break;
  734. }
  735. ++p;
  736. }
  737. }
  738. else
  739. break;
  740. }
  741. return p;
  742. }
  743. /// Skips a word.
  744. /// A word consists of undercores and/or alfanumeric characters,
  745. /// and valid variable prefixes/postfixes.
  746. static
  747. const char* skip_word(const char* p)
  748. {
  749. // prefix
  750. switch( *p )
  751. {
  752. case '@':// temporary char variable
  753. ++p; break;
  754. case '#':// account variable
  755. p += ( p[1] == '#' ? 2 : 1 ); break;
  756. case '\'':// instance variable
  757. ++p; break;
  758. case '.':// npc variable
  759. p += ( p[1] == '@' ? 2 : 1 ); break;
  760. case '$':// global variable
  761. p += ( p[1] == '@' ? 2 : 1 ); break;
  762. }
  763. while( ISALNUM(*p) || *p == '_' )
  764. ++p;
  765. // postfix
  766. if( *p == '$' )// string
  767. p++;
  768. return p;
  769. }
  770. /// Adds a word to str_data.
  771. /// @see skip_word
  772. /// @see add_str
  773. static
  774. int add_word(const char* p)
  775. {
  776. char* word;
  777. int len;
  778. int i;
  779. // Check for a word
  780. len = skip_word(p) - p;
  781. if( len == 0 )
  782. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  783. // Duplicate the word
  784. word = aMalloc(len+1);
  785. memcpy(word, p, len);
  786. word[len] = 0;
  787. // add the word
  788. i = add_str(word);
  789. aFree(word);
  790. return i;
  791. }
  792. /// Parses a function call.
  793. /// The argument list can have parenthesis or not.
  794. /// The number of arguments is checked.
  795. static
  796. const char* parse_callfunc(const char* p, int require_paren)
  797. {
  798. const char* p2;
  799. const char* arg=NULL;
  800. int func;
  801. func = add_word(p);
  802. if( str_data[func].type == C_FUNC ){
  803. // buildin function
  804. add_scriptl(func);
  805. add_scriptc(C_ARG);
  806. arg = buildin_func[str_data[func].val].arg;
  807. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  808. // script defined function
  809. int callsub = search_str("callsub");
  810. add_scriptl(callsub);
  811. add_scriptc(C_ARG);
  812. add_scriptl(func);
  813. arg = buildin_func[str_data[callsub].val].arg;
  814. if( *arg == 0 )
  815. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  816. if( *arg != '*' )
  817. ++arg; // count func as argument
  818. } else
  819. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  820. p = skip_word(p);
  821. p = skip_space(p);
  822. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  823. syntax.curly[syntax.curly_count].count = 0;
  824. if( *p == ';' )
  825. {// <func name> ';'
  826. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  827. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  828. {// <func name> '(' ')'
  829. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  830. p = p2;
  831. /*
  832. } else if( 0 && require_paren && *p != '(' )
  833. {// <func name>
  834. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  835. */
  836. } else
  837. {// <func name> <arg list>
  838. if( require_paren ){
  839. if( *p != '(' )
  840. disp_error_message("need '('",p);
  841. ++p; // skip '('
  842. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  843. } else if( *p == '(' ){
  844. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  845. } else {
  846. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  847. }
  848. ++syntax.curly_count;
  849. while( *arg ) {
  850. p2=parse_subexpr(p,-1);
  851. if( p == p2 )
  852. break; // not an argument
  853. if( *arg != '*' )
  854. ++arg; // next argument
  855. p=skip_space(p2);
  856. if( *arg == 0 || *p != ',' )
  857. break; // no more arguments
  858. ++p; // skip comma
  859. }
  860. --syntax.curly_count;
  861. }
  862. if( *arg && *arg != '?' && *arg != '*' )
  863. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  864. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  865. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  866. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  867. if( *p != ')' )
  868. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  869. ++p;
  870. }
  871. add_scriptc(C_FUNC);
  872. return p;
  873. }
  874. /*==========================================
  875. * 項の解析
  876. *------------------------------------------*/
  877. const char* parse_simpleexpr(const char *p)
  878. {
  879. int i;
  880. p=skip_space(p);
  881. if(*p==';' || *p==',')
  882. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  883. if(*p=='('){
  884. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  885. ++syntax.curly[i].count;
  886. p=parse_subexpr(p+1,-1);
  887. p=skip_space(p);
  888. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  889. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  890. ){
  891. if( *p == ',' ){
  892. syntax.curly[i].flag = ARGLIST_PAREN;
  893. return p;
  894. } else
  895. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  896. }
  897. if( *p != ')' )
  898. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  899. ++p;
  900. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  901. char *np;
  902. i=strtoul(p,&np,0);
  903. add_scripti(i);
  904. p=np;
  905. } else if(*p=='"'){
  906. add_scriptc(C_STR);
  907. p++;
  908. while( *p && *p != '"' ){
  909. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  910. {
  911. char buf[8];
  912. size_t len = skip_escaped_c(p) - p;
  913. size_t n = sv_unescape_c(buf, p, len);
  914. if( n != 1 )
  915. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  916. p += len;
  917. add_scriptb(*buf);
  918. continue;
  919. }
  920. else if( *p == '\n' )
  921. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  922. add_scriptb(*p++);
  923. }
  924. if(!*p)
  925. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  926. add_scriptb(0);
  927. p++; //'"'
  928. } else {
  929. int l;
  930. // label , register , function etc
  931. if(skip_word(p)==p)
  932. disp_error_message("parse_simpleexpr: unexpected character",p);
  933. l=add_word(p);
  934. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  935. return parse_callfunc(p,1);
  936. p=skip_word(p);
  937. if( *p == '[' ){
  938. // array(name[i] => getelementofarray(name,i) )
  939. add_scriptl(search_str("getelementofarray"));
  940. add_scriptc(C_ARG);
  941. add_scriptl(l);
  942. p=parse_subexpr(p+1,-1);
  943. p=skip_space(p);
  944. if( *p != ']' )
  945. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  946. ++p;
  947. add_scriptc(C_FUNC);
  948. }else
  949. add_scriptl(l);
  950. }
  951. return p;
  952. }
  953. /*==========================================
  954. * 式の解析
  955. *------------------------------------------*/
  956. const char* parse_subexpr(const char* p,int limit)
  957. {
  958. int op,opl,len;
  959. const char* tmpp;
  960. p=skip_space(p);
  961. if(*p=='-'){
  962. tmpp=skip_space(p+1);
  963. if(*tmpp==';' || *tmpp==','){
  964. add_scriptl(LABEL_NEXTLINE);
  965. p++;
  966. return p;
  967. }
  968. }
  969. tmpp=p;
  970. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  971. p=parse_subexpr(p+1,10);
  972. add_scriptc(op);
  973. } else
  974. p=parse_simpleexpr(p);
  975. p=skip_space(p);
  976. while((
  977. (op=C_OP3,opl=0,len=1,*p=='?') ||
  978. (op=C_ADD,opl=8,len=1,*p=='+') ||
  979. (op=C_SUB,opl=8,len=1,*p=='-') ||
  980. (op=C_MUL,opl=9,len=1,*p=='*') ||
  981. (op=C_DIV,opl=9,len=1,*p=='/') ||
  982. (op=C_MOD,opl=9,len=1,*p=='%') ||
  983. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  984. (op=C_AND,opl=6,len=1,*p=='&') ||
  985. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  986. (op=C_OR,opl=5,len=1,*p=='|') ||
  987. (op=C_XOR,opl=4,len=1,*p=='^') ||
  988. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  989. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  990. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  991. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  992. (op=C_GT,opl=3,len=1,*p=='>') ||
  993. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  994. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  995. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  996. p+=len;
  997. if(op == C_OP3) {
  998. p=parse_subexpr(p,-1);
  999. p=skip_space(p);
  1000. if( *(p++) != ':')
  1001. disp_error_message("parse_subexpr: need ':'", p-1);
  1002. p=parse_subexpr(p,-1);
  1003. } else {
  1004. p=parse_subexpr(p,opl);
  1005. }
  1006. add_scriptc(op);
  1007. p=skip_space(p);
  1008. }
  1009. return p; /* return first untreated operator */
  1010. }
  1011. /*==========================================
  1012. * 式の評価
  1013. *------------------------------------------*/
  1014. const char* parse_expr(const char *p)
  1015. {
  1016. switch(*p){
  1017. case ')': case ';': case ':': case '[': case ']':
  1018. case '}':
  1019. disp_error_message("parse_expr: unexpected char",p);
  1020. }
  1021. p=parse_subexpr(p,-1);
  1022. return p;
  1023. }
  1024. /*==========================================
  1025. * 行の解析
  1026. *------------------------------------------*/
  1027. const char* parse_line(const char* p)
  1028. {
  1029. const char* p2;
  1030. p=skip_space(p);
  1031. if(*p==';') {
  1032. // if(); for(); while(); のために閉じ判定
  1033. p = parse_syntax_close(p + 1);
  1034. return p;
  1035. }
  1036. if(*p==')' && parse_syntax_for_flag)
  1037. return p+1;
  1038. p = skip_space(p);
  1039. if(p[0] == '{') {
  1040. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1041. syntax.curly[syntax.curly_count].count = -1;
  1042. syntax.curly[syntax.curly_count].index = -1;
  1043. syntax.curly_count++;
  1044. return p + 1;
  1045. } else if(p[0] == '}') {
  1046. return parse_curly_close(p);
  1047. }
  1048. // 構文関連の処理
  1049. p2 = parse_syntax(p);
  1050. if(p2 != NULL)
  1051. return p2;
  1052. p = parse_callfunc(p,0);
  1053. p = skip_space(p);
  1054. if(parse_syntax_for_flag) {
  1055. if( *p != ')' )
  1056. disp_error_message("parse_line: need ')'",p);
  1057. } else {
  1058. if( *p != ';' )
  1059. disp_error_message("parse_line: need ';'",p);
  1060. }
  1061. // if, for , while の閉じ判定
  1062. p = parse_syntax_close(p+1);
  1063. return p;
  1064. }
  1065. // { ... } の閉じ処理
  1066. const char* parse_curly_close(const char* p)
  1067. {
  1068. if(syntax.curly_count <= 0) {
  1069. disp_error_message("parse_curly_close: unexpected string",p);
  1070. return p + 1;
  1071. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1072. syntax.curly_count--;
  1073. // if, for , while の閉じ判定
  1074. p = parse_syntax_close(p + 1);
  1075. return p;
  1076. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1077. // switch() 閉じ判定
  1078. int pos = syntax.curly_count-1;
  1079. char label[256];
  1080. int l;
  1081. // 一時変数を消す
  1082. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1083. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1084. parse_line(label);
  1085. syntax.curly_count--;
  1086. // 無条件で終了ポインタに移動
  1087. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1088. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1089. parse_line(label);
  1090. syntax.curly_count--;
  1091. // 現在地のラベルを付ける
  1092. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1093. l=add_str(label);
  1094. set_label(l,script_pos, p);
  1095. if(syntax.curly[pos].flag) {
  1096. // default が存在する
  1097. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1098. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1099. parse_line(label);
  1100. syntax.curly_count--;
  1101. }
  1102. // 終了ラベルを付ける
  1103. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1104. l=add_str(label);
  1105. set_label(l,script_pos, p);
  1106. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1107. syntax.curly_count--;
  1108. return p+1;
  1109. } else {
  1110. disp_error_message("parse_curly_close: unexpected string",p);
  1111. return p + 1;
  1112. }
  1113. }
  1114. // 構文関連の処理
  1115. // break, case, continue, default, do, for, function,
  1116. // if, switch, while をこの内部で処理します。
  1117. const char* parse_syntax(const char* p)
  1118. {
  1119. const char *p2 = skip_word(p);
  1120. switch(*p) {
  1121. case 'B':
  1122. case 'b':
  1123. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1124. // break の処理
  1125. char label[256];
  1126. int pos = syntax.curly_count - 1;
  1127. while(pos >= 0) {
  1128. if(syntax.curly[pos].type == TYPE_DO) {
  1129. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1130. break;
  1131. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1132. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1133. break;
  1134. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1135. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1136. break;
  1137. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1138. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1139. break;
  1140. }
  1141. pos--;
  1142. }
  1143. if(pos < 0) {
  1144. disp_error_message("parse_syntax: unexpected 'break'",p);
  1145. } else {
  1146. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1147. parse_line(label);
  1148. syntax.curly_count--;
  1149. }
  1150. p = skip_space(p2);
  1151. if(*p != ';')
  1152. disp_error_message("parse_syntax: need ';'",p);
  1153. // if, for , while の閉じ判定
  1154. p = parse_syntax_close(p + 1);
  1155. return p;
  1156. }
  1157. break;
  1158. case 'c':
  1159. case 'C':
  1160. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1161. // case の処理
  1162. int pos = syntax.curly_count-1;
  1163. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1164. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1165. return p+1;
  1166. } else {
  1167. char label[256];
  1168. int l,v;
  1169. char *np;
  1170. if(syntax.curly[pos].count != 1) {
  1171. // FALLTHRU 用のジャンプ
  1172. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1173. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1174. parse_line(label);
  1175. syntax.curly_count--;
  1176. // 現在地のラベルを付ける
  1177. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1178. l=add_str(label);
  1179. set_label(l,script_pos, p);
  1180. }
  1181. // switch 判定文
  1182. p = skip_space(p2);
  1183. if(p == p2) {
  1184. disp_error_message("parse_syntax: expect space ' '",p);
  1185. }
  1186. // check whether case label is integer or not
  1187. v = strtol(p,&np,0);
  1188. if(np == p) { //Check for constants
  1189. p2 = skip_word(p);
  1190. v = p2-p; // length of word at p2
  1191. memcpy(label,p,v);
  1192. label[v]='\0';
  1193. v = search_str(label);
  1194. if (v < 0 || str_data[v].type != C_INT)
  1195. disp_error_message("parse_syntax: 'case' label not integer",p);
  1196. v = str_data[v].val;
  1197. p = skip_word(p);
  1198. } else { //Numeric value
  1199. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1200. p++;
  1201. p = skip_word(p);
  1202. if(np != p)
  1203. disp_error_message("parse_syntax: 'case' label not integer",np);
  1204. }
  1205. p = skip_space(p);
  1206. if(*p != ':')
  1207. disp_error_message("parse_syntax: expect ':'",p);
  1208. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1209. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1210. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1211. // 2回parse しないとダメ
  1212. p2 = parse_line(label);
  1213. parse_line(p2);
  1214. syntax.curly_count--;
  1215. if(syntax.curly[pos].count != 1) {
  1216. // FALLTHRU 終了後のラベル
  1217. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1218. l=add_str(label);
  1219. set_label(l,script_pos,p);
  1220. }
  1221. // check duplication of case label [Rayce]
  1222. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1223. disp_error_message("parse_syntax: dup 'case'",p);
  1224. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1225. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1226. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1227. parse_line(label);
  1228. syntax.curly_count--;
  1229. syntax.curly[pos].count++;
  1230. }
  1231. return p + 1;
  1232. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1233. // continue の処理
  1234. char label[256];
  1235. int pos = syntax.curly_count - 1;
  1236. while(pos >= 0) {
  1237. if(syntax.curly[pos].type == TYPE_DO) {
  1238. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1239. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1240. break;
  1241. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1242. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1243. break;
  1244. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1245. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1246. break;
  1247. }
  1248. pos--;
  1249. }
  1250. if(pos < 0) {
  1251. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1252. } else {
  1253. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1254. parse_line(label);
  1255. syntax.curly_count--;
  1256. }
  1257. p = skip_space(p2);
  1258. if(*p != ';')
  1259. disp_error_message("parse_syntax: need ';'",p);
  1260. // if, for , while の閉じ判定
  1261. p = parse_syntax_close(p + 1);
  1262. return p;
  1263. }
  1264. break;
  1265. case 'd':
  1266. case 'D':
  1267. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1268. // switch - default の処理
  1269. int pos = syntax.curly_count-1;
  1270. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1271. disp_error_message("parse_syntax: unexpected 'default'",p);
  1272. } else if(syntax.curly[pos].flag) {
  1273. disp_error_message("parse_syntax: dup 'default'",p);
  1274. } else {
  1275. char label[256];
  1276. int l;
  1277. // 現在地のラベルを付ける
  1278. p = skip_space(p2);
  1279. if(*p != ':') {
  1280. disp_error_message("parse_syntax: need ':'",p);
  1281. }
  1282. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1283. l=add_str(label);
  1284. set_label(l,script_pos,p);
  1285. // 無条件で次のリンクに飛ばす
  1286. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1287. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1288. parse_line(label);
  1289. syntax.curly_count--;
  1290. // default のラベルを付ける
  1291. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1292. l=add_str(label);
  1293. set_label(l,script_pos,p);
  1294. syntax.curly[syntax.curly_count - 1].flag = 1;
  1295. syntax.curly[pos].count++;
  1296. }
  1297. return p + 1;
  1298. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1299. int l;
  1300. char label[256];
  1301. p=skip_space(p2);
  1302. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1303. syntax.curly[syntax.curly_count].count = 1;
  1304. syntax.curly[syntax.curly_count].index = syntax.index++;
  1305. syntax.curly[syntax.curly_count].flag = 0;
  1306. // 現在地のラベル形成する
  1307. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1308. l=add_str(label);
  1309. set_label(l,script_pos,p);
  1310. syntax.curly_count++;
  1311. return p;
  1312. }
  1313. break;
  1314. case 'f':
  1315. case 'F':
  1316. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1317. int l;
  1318. char label[256];
  1319. int pos = syntax.curly_count;
  1320. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1321. syntax.curly[syntax.curly_count].count = 1;
  1322. syntax.curly[syntax.curly_count].index = syntax.index++;
  1323. syntax.curly[syntax.curly_count].flag = 0;
  1324. syntax.curly_count++;
  1325. p=skip_space(p2);
  1326. if(*p != '(')
  1327. disp_error_message("parse_syntax: need '('",p);
  1328. p++;
  1329. // 初期化文を実行する
  1330. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1331. p=parse_line(p);
  1332. syntax.curly_count--;
  1333. // 条件判断開始のラベル形成する
  1334. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1335. l=add_str(label);
  1336. set_label(l,script_pos,p);
  1337. p=skip_space(p);
  1338. if(*p == ';') {
  1339. // for(;;) のパターンなので必ず真
  1340. ;
  1341. } else {
  1342. // 条件が偽なら終了地点に飛ばす
  1343. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1344. add_scriptl(add_str("jump_zero"));
  1345. add_scriptc(C_ARG);
  1346. p=parse_expr(p);
  1347. p=skip_space(p);
  1348. add_scriptl(add_str(label));
  1349. add_scriptc(C_FUNC);
  1350. }
  1351. if(*p != ';')
  1352. disp_error_message("parse_syntax: need ';'",p);
  1353. p++;
  1354. // ループ開始に飛ばす
  1355. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1356. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1357. parse_line(label);
  1358. syntax.curly_count--;
  1359. // 次のループへのラベル形成する
  1360. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1361. l=add_str(label);
  1362. set_label(l,script_pos,p);
  1363. // 次のループに入る時の処理
  1364. // for 最後の ')' を ';' として扱うフラグ
  1365. parse_syntax_for_flag = 1;
  1366. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1367. p=parse_line(p);
  1368. syntax.curly_count--;
  1369. parse_syntax_for_flag = 0;
  1370. // 条件判定処理に飛ばす
  1371. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1372. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1373. parse_line(label);
  1374. syntax.curly_count--;
  1375. // ループ開始のラベル付け
  1376. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1377. l=add_str(label);
  1378. set_label(l,script_pos,p);
  1379. return p;
  1380. }
  1381. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1382. {// internal script function
  1383. const char *func_name;
  1384. func_name = skip_space(p2);
  1385. p = skip_word(func_name);
  1386. if( p == func_name )
  1387. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1388. p2 = skip_space(p);
  1389. if( *p2 == ';' )
  1390. {// function <name> ;
  1391. // function declaration - just register the name
  1392. int l;
  1393. l = add_word(func_name);
  1394. if( str_data[l].type == C_NOP )// set type only if the name did not exist before
  1395. str_data[l].type = C_USERFUNC;
  1396. // if, for , while の閉じ判定
  1397. p = parse_syntax_close(p2 + 1);
  1398. return p; }
  1399. else if(*p2 == '{')
  1400. {// function <name> <line/block of code>
  1401. char label[256];
  1402. int l;
  1403. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1404. syntax.curly[syntax.curly_count].count = 1;
  1405. syntax.curly[syntax.curly_count].index = syntax.index++;
  1406. syntax.curly[syntax.curly_count].flag = 0;
  1407. ++syntax.curly_count;
  1408. // Jump over the function code
  1409. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1410. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1411. ++syntax.curly_count;
  1412. parse_line(label);
  1413. --syntax.curly_count;
  1414. // Set the position of the function (label)
  1415. l=add_word(func_name);
  1416. if( str_data[l].type == C_NOP )// set type only if the name did not exist before
  1417. str_data[l].type = C_USERFUNC;
  1418. set_label(l, script_pos, p);
  1419. if( parse_options&SCRIPT_USE_LABEL_DB )
  1420. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1421. return skip_space(p);
  1422. }
  1423. else
  1424. {
  1425. disp_error_message("expect ';' or '{' at function syntax",p);
  1426. }
  1427. }
  1428. break;
  1429. case 'i':
  1430. case 'I':
  1431. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1432. // if() の処理
  1433. char label[256];
  1434. p=skip_space(p2);
  1435. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1436. disp_error_message("need '('",p);
  1437. }
  1438. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1439. syntax.curly[syntax.curly_count].count = 1;
  1440. syntax.curly[syntax.curly_count].index = syntax.index++;
  1441. syntax.curly[syntax.curly_count].flag = 0;
  1442. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1443. syntax.curly_count++;
  1444. add_scriptl(add_str("jump_zero"));
  1445. add_scriptc(C_ARG);
  1446. p=parse_expr(p);
  1447. p=skip_space(p);
  1448. add_scriptl(add_str(label));
  1449. add_scriptc(C_FUNC);
  1450. return p;
  1451. }
  1452. break;
  1453. case 's':
  1454. case 'S':
  1455. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1456. // switch() の処理
  1457. char label[256];
  1458. p=skip_space(p2);
  1459. if(*p != '(') {
  1460. disp_error_message("need '('",p);
  1461. }
  1462. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1463. syntax.curly[syntax.curly_count].count = 1;
  1464. syntax.curly[syntax.curly_count].index = syntax.index++;
  1465. syntax.curly[syntax.curly_count].flag = 0;
  1466. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1467. syntax.curly_count++;
  1468. add_scriptl(add_str("set"));
  1469. add_scriptc(C_ARG);
  1470. add_scriptl(add_str(label));
  1471. p=parse_expr(p);
  1472. p=skip_space(p);
  1473. if(*p != '{') {
  1474. disp_error_message("parse_syntax: need '{'",p);
  1475. }
  1476. add_scriptc(C_FUNC);
  1477. return p + 1;
  1478. }
  1479. break;
  1480. case 'w':
  1481. case 'W':
  1482. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1483. int l;
  1484. char label[256];
  1485. p=skip_space(p2);
  1486. if(*p != '(') {
  1487. disp_error_message("need '('",p);
  1488. }
  1489. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1490. syntax.curly[syntax.curly_count].count = 1;
  1491. syntax.curly[syntax.curly_count].index = syntax.index++;
  1492. syntax.curly[syntax.curly_count].flag = 0;
  1493. // 条件判断開始のラベル形成する
  1494. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1495. l=add_str(label);
  1496. set_label(l,script_pos,p);
  1497. // 条件が偽なら終了地点に飛ばす
  1498. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1499. syntax.curly_count++;
  1500. add_scriptl(add_str("jump_zero"));
  1501. add_scriptc(C_ARG);
  1502. p=parse_expr(p);
  1503. p=skip_space(p);
  1504. add_scriptl(add_str(label));
  1505. add_scriptc(C_FUNC);
  1506. return p;
  1507. }
  1508. break;
  1509. }
  1510. return NULL;
  1511. }
  1512. const char* parse_syntax_close(const char *p) {
  1513. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1514. int flag;
  1515. do {
  1516. p = parse_syntax_close_sub(p,&flag);
  1517. } while(flag);
  1518. return p;
  1519. }
  1520. // if, for , while , do の閉じ判定
  1521. // flag == 1 : 閉じられた
  1522. // flag == 0 : 閉じられない
  1523. const char* parse_syntax_close_sub(const char* p,int* flag)
  1524. {
  1525. char label[256];
  1526. int pos = syntax.curly_count - 1;
  1527. int l;
  1528. *flag = 1;
  1529. if(syntax.curly_count <= 0) {
  1530. *flag = 0;
  1531. return p;
  1532. } else if(syntax.curly[pos].type == TYPE_IF) {
  1533. const char *bp = p;
  1534. const char *p2;
  1535. // if 最終場所へ飛ばす
  1536. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1537. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1538. parse_line(label);
  1539. syntax.curly_count--;
  1540. // 現在地のラベルを付ける
  1541. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1542. l=add_str(label);
  1543. set_label(l,script_pos,p);
  1544. syntax.curly[pos].count++;
  1545. p = skip_space(p);
  1546. p2 = skip_word(p);
  1547. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1548. // else or else - if
  1549. p = skip_space(p2);
  1550. p2 = skip_word(p);
  1551. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1552. // else - if
  1553. p=skip_space(p2);
  1554. if(*p != '(') {
  1555. disp_error_message("need '('",p);
  1556. }
  1557. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1558. add_scriptl(add_str("jump_zero"));
  1559. add_scriptc(C_ARG);
  1560. p=parse_expr(p);
  1561. p=skip_space(p);
  1562. add_scriptl(add_str(label));
  1563. add_scriptc(C_FUNC);
  1564. *flag = 0;
  1565. return p;
  1566. } else {
  1567. // else
  1568. if(!syntax.curly[pos].flag) {
  1569. syntax.curly[pos].flag = 1;
  1570. *flag = 0;
  1571. return p;
  1572. }
  1573. }
  1574. }
  1575. // if 閉じ
  1576. syntax.curly_count--;
  1577. // 最終地のラベルを付ける
  1578. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1579. l=add_str(label);
  1580. set_label(l,script_pos,p);
  1581. if(syntax.curly[pos].flag == 1) {
  1582. // このifに対するelseじゃないのでポインタの位置は同じ
  1583. return bp;
  1584. }
  1585. return p;
  1586. } else if(syntax.curly[pos].type == TYPE_DO) {
  1587. int l;
  1588. char label[256];
  1589. const char *p2;
  1590. if(syntax.curly[pos].flag) {
  1591. // 現在地のラベル形成する(continue でここに来る)
  1592. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1593. l=add_str(label);
  1594. set_label(l,script_pos,p);
  1595. }
  1596. // 条件が偽なら終了地点に飛ばす
  1597. p = skip_space(p);
  1598. p2 = skip_word(p);
  1599. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1600. disp_error_message("parse_syntax: need 'while'",p);
  1601. p = skip_space(p2);
  1602. if(*p != '(') {
  1603. disp_error_message("need '('",p);
  1604. }
  1605. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1606. add_scriptl(add_str("jump_zero"));
  1607. add_scriptc(C_ARG);
  1608. p=parse_expr(p);
  1609. p=skip_space(p);
  1610. add_scriptl(add_str(label));
  1611. add_scriptc(C_FUNC);
  1612. // 開始地点に飛ばす
  1613. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1614. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1615. parse_line(label);
  1616. syntax.curly_count--;
  1617. // 条件終了地点のラベル形成する
  1618. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1619. l=add_str(label);
  1620. set_label(l,script_pos,p);
  1621. p = skip_space(p);
  1622. if(*p != ';') {
  1623. disp_error_message("parse_syntax: need ';'",p);
  1624. return p+1;
  1625. }
  1626. p++;
  1627. syntax.curly_count--;
  1628. return p;
  1629. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1630. // 次のループに飛ばす
  1631. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1632. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1633. parse_line(label);
  1634. syntax.curly_count--;
  1635. // for 終了のラベル付け
  1636. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1637. l=add_str(label);
  1638. set_label(l,script_pos,p);
  1639. syntax.curly_count--;
  1640. return p;
  1641. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1642. // while 条件判断へ飛ばす
  1643. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1644. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1645. parse_line(label);
  1646. syntax.curly_count--;
  1647. // while 終了のラベル付け
  1648. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1649. l=add_str(label);
  1650. set_label(l,script_pos,p);
  1651. syntax.curly_count--;
  1652. return p;
  1653. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1654. int pos = syntax.curly_count-1;
  1655. char label[256];
  1656. int l;
  1657. // 戻す
  1658. sprintf(label,"return;");
  1659. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1660. parse_line(label);
  1661. syntax.curly_count--;
  1662. // 現在地のラベルを付ける
  1663. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1664. l=add_str(label);
  1665. set_label(l,script_pos,p);
  1666. syntax.curly_count--;
  1667. return p;
  1668. } else {
  1669. *flag = 0;
  1670. return p;
  1671. }
  1672. }
  1673. /*==========================================
  1674. * 組み込み関数の追加
  1675. *------------------------------------------*/
  1676. static void add_buildin_func(void)
  1677. {
  1678. int i,n;
  1679. const char* p;
  1680. for( i = 0; buildin_func[i].func; i++ )
  1681. {
  1682. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1683. // 'v' - value (either string or int or reference)
  1684. // 's' - string
  1685. // 'i' - int
  1686. // 'r' - reference (of a variable)
  1687. // 'l' - label
  1688. // '?' - one optional parameter
  1689. // '*' - unknown number of optional parameters
  1690. p = buildin_func[i].arg;
  1691. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1692. while( *p == '?' ) ++p;
  1693. if( *p == '*' ) ++p;
  1694. if( *p != 0){
  1695. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1696. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1697. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1698. } else {
  1699. n = add_str(buildin_func[i].name);
  1700. str_data[n].type = C_FUNC;
  1701. str_data[n].val = i;
  1702. str_data[n].func = buildin_func[i].func;
  1703. }
  1704. }
  1705. }
  1706. /*==========================================
  1707. * 定数データベースの読み込み
  1708. *------------------------------------------*/
  1709. static void read_constdb(void)
  1710. {
  1711. FILE *fp;
  1712. char line[1024],name[1024],val[1024];
  1713. int n,type;
  1714. sprintf(line, "%s/const.txt", db_path);
  1715. fp=fopen(line, "r");
  1716. if(fp==NULL){
  1717. ShowError("can't read %s\n", line);
  1718. return ;
  1719. }
  1720. while(fgets(line, sizeof(line), fp))
  1721. {
  1722. if(line[0]=='/' && line[1]=='/')
  1723. continue;
  1724. type=0;
  1725. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1726. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1727. n=add_str(name);
  1728. if(type==0)
  1729. str_data[n].type=C_INT;
  1730. else
  1731. str_data[n].type=C_PARAM;
  1732. str_data[n].val= (int)strtol(val,NULL,0);
  1733. }
  1734. }
  1735. fclose(fp);
  1736. }
  1737. /*==========================================
  1738. * エラー表示
  1739. *------------------------------------------*/
  1740. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1741. {
  1742. int i;
  1743. if( p == NULL || !p[0] ) return NULL;
  1744. if( line < 0 )
  1745. StringBuf_Printf(buf, "*% 5d : ", -line);
  1746. else
  1747. StringBuf_Printf(buf, " % 5d : ", line);
  1748. for(i=0;p[i] && p[i] != '\n';i++){
  1749. if(p + i != mark)
  1750. StringBuf_Printf(buf, "%c", p[i]);
  1751. else
  1752. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1753. }
  1754. StringBuf_AppendStr(buf, "\n");
  1755. return p+i+(p[i] == '\n' ? 1 : 0);
  1756. }
  1757. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1758. {
  1759. // エラーが発生した行を求める
  1760. int j;
  1761. int line = start_line;
  1762. const char *p;
  1763. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1764. StringBuf buf;
  1765. for(p=src;p && *p;line++){
  1766. const char *lineend=strchr(p,'\n');
  1767. if(lineend==NULL || error_pos<lineend){
  1768. break;
  1769. }
  1770. for( j = 0; j < 4; j++ ) {
  1771. linestart[j] = linestart[j+1];
  1772. }
  1773. linestart[4] = p;
  1774. p=lineend+1;
  1775. }
  1776. StringBuf_Init(&buf);
  1777. StringBuf_AppendStr(&buf, "\a\n");
  1778. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1779. StringBuf_Printf(&buf, " %s\n", error_msg);
  1780. for(j = 0; j < 5; j++ ) {
  1781. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1782. }
  1783. p = script_print_line(&buf, p, error_pos, -line);
  1784. for(j = 0; j < 5; j++) {
  1785. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1786. }
  1787. ShowError("%s", StringBuf_Value(&buf));
  1788. StringBuf_Destroy(&buf);
  1789. }
  1790. /*==========================================
  1791. * スクリプトの解析
  1792. *------------------------------------------*/
  1793. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1794. {
  1795. const char *p,*tmpp;
  1796. int i;
  1797. struct script_code* code = NULL;
  1798. static int first=1;
  1799. char end;
  1800. if( src == NULL )
  1801. return NULL;// empty script
  1802. memset(&syntax,0,sizeof(syntax));
  1803. if(first){
  1804. add_buildin_func();
  1805. read_constdb();
  1806. first=0;
  1807. }
  1808. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1809. script_pos=0;
  1810. script_size=SCRIPT_BLOCK_SIZE;
  1811. str_data[LABEL_NEXTLINE].type=C_NOP;
  1812. str_data[LABEL_NEXTLINE].backpatch=-1;
  1813. str_data[LABEL_NEXTLINE].label=-1;
  1814. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1815. if( options&SCRIPT_USE_LABEL_DB )
  1816. scriptlabel_db->clear(scriptlabel_db, NULL);
  1817. parse_options = options;
  1818. if( setjmp( error_jump ) != 0 ) {
  1819. //Restore program state when script has problems. [from jA]
  1820. int i;
  1821. const int size = ARRAYLENGTH(syntax.curly);
  1822. if( error_report )
  1823. script_error(src,file,line,error_msg,error_pos);
  1824. aFree( error_msg );
  1825. aFree( script_buf );
  1826. script_pos = 0;
  1827. script_size = 0;
  1828. script_buf = NULL;
  1829. for(i=LABEL_START;i<str_num;i++)
  1830. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1831. for(i=0; i<size; i++)
  1832. linkdb_final(&syntax.curly[i].case_label);
  1833. return NULL;
  1834. }
  1835. parse_syntax_for_flag=0;
  1836. p=src;
  1837. p=skip_space(p);
  1838. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1839. {// does not require brackets around the script
  1840. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1841. {// empty script and can return NULL
  1842. aFree( script_buf );
  1843. script_pos = 0;
  1844. script_size = 0;
  1845. script_buf = NULL;
  1846. return NULL;
  1847. }
  1848. end = '\0';
  1849. }
  1850. else
  1851. {// requires brackets around the script
  1852. if( *p != '{' )
  1853. disp_error_message("not found '{'",p);
  1854. p = skip_space(p+1);
  1855. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1856. {// empty script and can return NULL
  1857. aFree( script_buf );
  1858. script_pos = 0;
  1859. script_size = 0;
  1860. script_buf = NULL;
  1861. return NULL;
  1862. }
  1863. end = '}';
  1864. }
  1865. // clear references of labels, variables and internal functions
  1866. for(i=LABEL_START;i<str_num;i++){
  1867. if(
  1868. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1869. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1870. ){
  1871. str_data[i].type=C_NOP;
  1872. str_data[i].backpatch=-1;
  1873. str_data[i].label=-1;
  1874. }
  1875. }
  1876. while( syntax.curly_count != 0 || *p != end )
  1877. {
  1878. if( *p == '\0' )
  1879. disp_error_message("unexpected end of script",p);
  1880. // labelだけ特殊処理
  1881. tmpp=skip_space(skip_word(p));
  1882. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1883. i=add_word(p);
  1884. set_label(i,script_pos,p);
  1885. if( parse_options&SCRIPT_USE_LABEL_DB )
  1886. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1887. p=tmpp+1;
  1888. p=skip_space(p);
  1889. continue;
  1890. }
  1891. // 他は全部一緒くた
  1892. p=parse_line(p);
  1893. p=skip_space(p);
  1894. add_scriptc(C_EOL);
  1895. set_label(LABEL_NEXTLINE,script_pos,p);
  1896. str_data[LABEL_NEXTLINE].type=C_NOP;
  1897. str_data[LABEL_NEXTLINE].backpatch=-1;
  1898. str_data[LABEL_NEXTLINE].label=-1;
  1899. }
  1900. add_scriptc(C_NOP);
  1901. // trim code to size
  1902. script_size = script_pos;
  1903. RECREATE(script_buf,unsigned char,script_pos);
  1904. // default unknown references to variables
  1905. for(i=LABEL_START;i<str_num;i++){
  1906. if(str_data[i].type==C_NOP){
  1907. int j,next;
  1908. str_data[i].type=C_NAME;
  1909. str_data[i].label=i;
  1910. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1911. next=GETVALUE(script_buf,j);
  1912. SETVALUE(script_buf,j,i);
  1913. j=next;
  1914. }
  1915. }
  1916. }
  1917. #ifdef DEBUG_DISP
  1918. for(i=0;i<script_pos;i++){
  1919. if((i&15)==0) printf("%04x : ",i);
  1920. ShowMessage("%02x ",script_buf[i]);
  1921. if((i&15)==15) printf("\n");
  1922. }
  1923. ShowMessage("\n");
  1924. #endif
  1925. #ifdef DEBUG_DISASM
  1926. {
  1927. int i = 0,j;
  1928. while(i < script_pos) {
  1929. c_op op = get_com(script_buf,&i);
  1930. ShowMessage("%06x %s", i, script_op2name(op));
  1931. j = i;
  1932. switch(op) {
  1933. case C_INT:
  1934. ShowMessage(" %d", get_num(script_buf,&i));
  1935. break;
  1936. case C_POS:
  1937. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  1938. i += 3;
  1939. break;
  1940. case C_NAME:
  1941. j = (*(int*)(script_buf+i)&0xffffff);
  1942. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  1943. i += 3;
  1944. break;
  1945. case C_STR:
  1946. j = strlen(script_buf + i);
  1947. ShowMessage(" %s", script_buf + i);
  1948. i += j+1;
  1949. break;
  1950. }
  1951. ShowMessage(CL_CLL"\n");
  1952. }
  1953. }
  1954. #endif
  1955. CREATE(code,struct script_code,1);
  1956. code->script_buf = script_buf;
  1957. code->script_size = script_size;
  1958. code->script_vars = NULL;
  1959. return code;
  1960. }
  1961. /// Returns the player attached to this script, identified by the rid.
  1962. /// If there is no player attached, the script is terminated.
  1963. TBL_PC *script_rid2sd(struct script_state *st)
  1964. {
  1965. TBL_PC *sd=map_id2sd(st->rid);
  1966. if(!sd){
  1967. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1968. script_reportfunc(st);
  1969. script_reportsrc(st);
  1970. st->state = END;
  1971. }
  1972. return sd;
  1973. }
  1974. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1975. ///
  1976. /// @param st Script state
  1977. /// @param data Variable/constant
  1978. void get_val(struct script_state* st, struct script_data* data)
  1979. {
  1980. const char* name;
  1981. char prefix;
  1982. char postfix;
  1983. TBL_PC* sd = NULL;
  1984. if( !data_isreference(data) )
  1985. return;// not a variable/constant
  1986. name = reference_getname(data);
  1987. prefix = name[0];
  1988. postfix = name[strlen(name) - 1];
  1989. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1990. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1991. {
  1992. sd = script_rid2sd(st);
  1993. if( sd == NULL )
  1994. {// needs player attached
  1995. if( postfix == '$' )
  1996. {// string variable
  1997. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1998. data->type = C_CONSTSTR;
  1999. data->u.str = "";
  2000. }
  2001. else
  2002. {// integer variable
  2003. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2004. data->type = C_INT;
  2005. data->u.num = 0;
  2006. }
  2007. return;
  2008. }
  2009. }
  2010. if( postfix == '$' )
  2011. {// string variable
  2012. switch( prefix )
  2013. {
  2014. case '@':
  2015. data->u.str = pc_readregstr(sd, data->u.num);
  2016. break;
  2017. case '$':
  2018. data->u.str = mapreg_readregstr(data->u.num);
  2019. break;
  2020. case '#':
  2021. if( name[1] == '#' )
  2022. data->u.str = pc_readaccountreg2str(sd, name);// global
  2023. else
  2024. data->u.str = pc_readaccountregstr(sd, name);// local
  2025. break;
  2026. case '.':
  2027. {
  2028. struct linkdb_node** n =
  2029. data->ref ? data->ref:
  2030. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2031. &st->script->script_vars;// npc variable
  2032. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2033. }
  2034. break;
  2035. case '\'':
  2036. {
  2037. struct linkdb_node** n = NULL;
  2038. if( st->instance_id )
  2039. n = &instance[st->instance_id].svar;
  2040. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2041. }
  2042. break;
  2043. default:
  2044. data->u.str = pc_readglobalreg_str(sd, name);
  2045. break;
  2046. }
  2047. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2048. {// empty string
  2049. data->type = C_CONSTSTR;
  2050. data->u.str = "";
  2051. }
  2052. else
  2053. {// duplicate string
  2054. data->type = C_STR;
  2055. data->u.str = aStrdup(data->u.str);
  2056. }
  2057. }
  2058. else
  2059. {// integer variable
  2060. data->type = C_INT;
  2061. if( reference_toconstant(data) )
  2062. {
  2063. data->u.num = reference_getconstant(data);
  2064. }
  2065. else if( reference_toparam(data) )
  2066. {
  2067. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2068. }
  2069. else
  2070. switch( prefix )
  2071. {
  2072. case '@':
  2073. data->u.num = pc_readreg(sd, data->u.num);
  2074. break;
  2075. case '$':
  2076. data->u.num = mapreg_readreg(data->u.num);
  2077. break;
  2078. case '#':
  2079. if( name[1] == '#' )
  2080. data->u.num = pc_readaccountreg2(sd, name);// global
  2081. else
  2082. data->u.num = pc_readaccountreg(sd, name);// local
  2083. break;
  2084. case '.':
  2085. {
  2086. struct linkdb_node** n =
  2087. data->ref ? data->ref:
  2088. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2089. &st->script->script_vars;// npc variable
  2090. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2091. }
  2092. break;
  2093. case '\'':
  2094. {
  2095. struct linkdb_node** n = NULL;
  2096. if( st->instance_id )
  2097. n = &instance[st->instance_id].ivar;
  2098. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2099. }
  2100. break;
  2101. default:
  2102. data->u.num = pc_readglobalreg(sd, name);
  2103. break;
  2104. }
  2105. }
  2106. return;
  2107. }
  2108. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2109. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2110. /// The value is left in the top of the stack and needs to be removed manually.
  2111. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2112. {
  2113. struct script_data* data;
  2114. push_val2(st->stack, C_NAME, uid, ref);
  2115. data = script_getdatatop(st, -1);
  2116. get_val(st, data);
  2117. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2118. }
  2119. /*==========================================
  2120. * Stores the value of a script variable
  2121. * Return value is 0 on fail, 1 on success.
  2122. *------------------------------------------*/
  2123. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2124. {
  2125. char prefix = name[0];
  2126. if( is_string_variable(name) )
  2127. {// string variable
  2128. const char* str = (const char*)value;
  2129. switch (prefix) {
  2130. case '@':
  2131. return pc_setregstr(sd, num, str);
  2132. case '$':
  2133. return mapreg_setregstr(num, str);
  2134. case '#':
  2135. return (name[1] == '#') ?
  2136. pc_setaccountreg2str(sd, name, str) :
  2137. pc_setaccountregstr(sd, name, str);
  2138. case '.': {
  2139. char* p;
  2140. struct linkdb_node** n;
  2141. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2142. p = (char*)linkdb_erase(n, (void*)num);
  2143. if (p) aFree(p);
  2144. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2145. }
  2146. return 1;
  2147. case '\'': {
  2148. char *p;
  2149. struct linkdb_node** n = NULL;
  2150. if( st->instance_id )
  2151. n = &instance[st->instance_id].svar;
  2152. p = (char*)linkdb_erase(n, (void*)num);
  2153. if (p) aFree(p);
  2154. if( str[0] ) linkdb_insert(n, (void*)num, aStrdup(str));
  2155. }
  2156. return 1;
  2157. default:
  2158. return pc_setglobalreg_str(sd, name, str);
  2159. }
  2160. }
  2161. else
  2162. {// integer variable
  2163. int val = (int)value;
  2164. if(str_data[num&0x00ffffff].type == C_PARAM)
  2165. {
  2166. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2167. {
  2168. if( st != NULL )
  2169. {
  2170. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2171. script_reportsrc(st);
  2172. st->state = END;
  2173. }
  2174. return 0;
  2175. }
  2176. return 1;
  2177. }
  2178. switch (prefix) {
  2179. case '@':
  2180. return pc_setreg(sd, num, val);
  2181. case '$':
  2182. return mapreg_setreg(num, val);
  2183. case '#':
  2184. return (name[1] == '#') ?
  2185. pc_setaccountreg2(sd, name, val) :
  2186. pc_setaccountreg(sd, name, val);
  2187. case '.': {
  2188. struct linkdb_node** n;
  2189. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2190. if (val == 0)
  2191. linkdb_erase(n, (void*)num);
  2192. else
  2193. linkdb_replace(n, (void*)num, (void*)val);
  2194. }
  2195. return 1;
  2196. case '\'':
  2197. {
  2198. struct linkdb_node** n = NULL;
  2199. if( st->instance_id )
  2200. n = &instance[st->instance_id].ivar;
  2201. if( val == 0 )
  2202. linkdb_erase(n, (void*)num);
  2203. else
  2204. linkdb_replace(n, (void*)num, (void*)val);
  2205. return 1;
  2206. }
  2207. default:
  2208. return pc_setglobalreg(sd, name, val);
  2209. }
  2210. }
  2211. }
  2212. int set_var(TBL_PC* sd, char* name, void* val)
  2213. {
  2214. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2215. }
  2216. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2217. {
  2218. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2219. }
  2220. /// Converts the data to a string
  2221. const char* conv_str(struct script_state* st, struct script_data* data)
  2222. {
  2223. char* p;
  2224. get_val(st, data);
  2225. if( data_isstring(data) )
  2226. {// nothing to convert
  2227. }
  2228. else if( data_isint(data) )
  2229. {// int -> string
  2230. CREATE(p, char, ITEM_NAME_LENGTH);
  2231. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2232. p[ITEM_NAME_LENGTH-1] = '\0';
  2233. data->type = C_STR;
  2234. data->u.str = p;
  2235. }
  2236. else if( data_isreference(data) )
  2237. {// reference -> string
  2238. //##TODO when does this happen (check get_val) [FlavioJS]
  2239. data->type = C_CONSTSTR;
  2240. data->u.str = reference_getname(data);
  2241. }
  2242. else
  2243. {// unsupported data type
  2244. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2245. script_reportdata(data);
  2246. script_reportsrc(st);
  2247. data->type = C_CONSTSTR;
  2248. data->u.str = "";
  2249. }
  2250. return data->u.str;
  2251. }
  2252. /// Converts the data to an int
  2253. int conv_num(struct script_state* st, struct script_data* data)
  2254. {
  2255. char* p;
  2256. long num;
  2257. get_val(st, data);
  2258. if( data_isint(data) )
  2259. {// nothing to convert
  2260. }
  2261. else if( data_isstring(data) )
  2262. {// string -> int
  2263. // the result does not overflow or underflow, it is capped instead
  2264. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2265. p = data->u.str;
  2266. errno = 0;
  2267. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2268. if( errno == ERANGE
  2269. #if LONG_MAX > INT_MAX
  2270. || num < INT_MIN || num > INT_MAX
  2271. #endif
  2272. )
  2273. {
  2274. if( num <= INT_MIN )
  2275. {
  2276. num = INT_MIN;
  2277. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2278. }
  2279. else//if( num >= INT_MAX )
  2280. {
  2281. num = INT_MAX;
  2282. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2283. }
  2284. script_reportdata(data);
  2285. script_reportsrc(st);
  2286. }
  2287. if( data->type == C_STR )
  2288. aFree(p);
  2289. data->type = C_INT;
  2290. data->u.num = (int)num;
  2291. }
  2292. #if 0
  2293. // FIXME this function is being used to retrieve the position of labels and
  2294. // probably other stuff [FlavioJS]
  2295. else
  2296. {// unsupported data type
  2297. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2298. script_reportdata(data);
  2299. script_reportsrc(st);
  2300. data->type = C_INT;
  2301. data->u.num = 0;
  2302. }
  2303. #endif
  2304. return data->u.num;
  2305. }
  2306. //
  2307. // Stack operations
  2308. //
  2309. /// Increases the size of the stack
  2310. void stack_expand(struct script_stack* stack)
  2311. {
  2312. stack->sp_max += 64;
  2313. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2314. stack->sp_max * sizeof(stack->stack_data[0]) );
  2315. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2316. 64 * sizeof(stack->stack_data[0]) );
  2317. }
  2318. /// Pushes a value into the stack
  2319. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2320. /// Pushes a value into the stack (with reference)
  2321. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2322. {
  2323. if( stack->sp >= stack->sp_max )
  2324. stack_expand(stack);
  2325. stack->stack_data[stack->sp].type = type;
  2326. stack->stack_data[stack->sp].u.num = val;
  2327. stack->stack_data[stack->sp].ref = ref;
  2328. stack->sp++;
  2329. return &stack->stack_data[stack->sp-1];
  2330. }
  2331. /// Pushes a string into the stack
  2332. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2333. {
  2334. if( stack->sp >= stack->sp_max )
  2335. stack_expand(stack);
  2336. stack->stack_data[stack->sp].type = type;
  2337. stack->stack_data[stack->sp].u.str = str;
  2338. stack->stack_data[stack->sp].ref = NULL;
  2339. stack->sp++;
  2340. return &stack->stack_data[stack->sp-1];
  2341. }
  2342. /// Pushes a retinfo into the stack
  2343. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2344. {
  2345. if( stack->sp >= stack->sp_max )
  2346. stack_expand(stack);
  2347. stack->stack_data[stack->sp].type = C_RETINFO;
  2348. stack->stack_data[stack->sp].u.ri = ri;
  2349. stack->stack_data[stack->sp].ref = NULL;
  2350. stack->sp++;
  2351. return &stack->stack_data[stack->sp-1];
  2352. }
  2353. /// Pushes a copy of the target position into the stack
  2354. struct script_data* push_copy(struct script_stack* stack, int pos)
  2355. {
  2356. switch( stack->stack_data[pos].type )
  2357. {
  2358. case C_CONSTSTR:
  2359. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2360. break;
  2361. case C_STR:
  2362. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2363. break;
  2364. case C_RETINFO:
  2365. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2366. exit(1);
  2367. break;
  2368. default:
  2369. return push_val2(
  2370. stack,stack->stack_data[pos].type,
  2371. stack->stack_data[pos].u.num,
  2372. stack->stack_data[pos].ref
  2373. );
  2374. break;
  2375. }
  2376. }
  2377. /// Removes the values in indexes [start,end[ from the stack.
  2378. /// Adjusts all stack pointers.
  2379. void pop_stack(struct script_state* st, int start, int end)
  2380. {
  2381. struct script_stack* stack = st->stack;
  2382. struct script_data* data;
  2383. int i;
  2384. if( start < 0 )
  2385. start = 0;
  2386. if( end > stack->sp )
  2387. end = stack->sp;
  2388. if( start >= end )
  2389. return;// nothing to pop
  2390. // free stack elements
  2391. for( i = start; i < end; i++ )
  2392. {
  2393. data = &stack->stack_data[i];
  2394. if( data->type == C_STR )
  2395. aFree(data->u.str);
  2396. if( data->type == C_RETINFO )
  2397. {
  2398. struct script_retinfo* ri = data->u.ri;
  2399. if( ri->var_function )
  2400. {
  2401. script_free_vars(ri->var_function);
  2402. aFree(ri->var_function);
  2403. }
  2404. aFree(ri);
  2405. }
  2406. data->type = C_NOP;
  2407. }
  2408. // move the rest of the elements
  2409. if( stack->sp > end )
  2410. {
  2411. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2412. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2413. stack->stack_data[i].type = C_NOP;
  2414. }
  2415. // adjust stack pointers
  2416. if( st->start > end ) st->start -= end - start;
  2417. else if( st->start > start ) st->start = start;
  2418. if( st->end > end ) st->end -= end - start;
  2419. else if( st->end > start ) st->end = start;
  2420. if( stack->defsp > end ) stack->defsp -= end - start;
  2421. else if( stack->defsp > start ) stack->defsp = start;
  2422. stack->sp -= end - start;
  2423. }
  2424. ///
  2425. ///
  2426. ///
  2427. /*==========================================
  2428. * スクリプト依存変数、関数依存変数の解放
  2429. *------------------------------------------*/
  2430. void script_free_vars(struct linkdb_node **node)
  2431. {
  2432. struct linkdb_node* n = *node;
  2433. while( n != NULL)
  2434. {
  2435. const char* name = get_str((int)(n->key)&0x00ffffff);
  2436. if( is_string_variable(name) )
  2437. aFree(n->data); // 文字型変数なので、データ削除
  2438. n = n->next;
  2439. }
  2440. linkdb_final( node );
  2441. }
  2442. void script_free_code(struct script_code* code)
  2443. {
  2444. script_free_vars( &code->script_vars );
  2445. aFree( code->script_buf );
  2446. aFree( code );
  2447. }
  2448. /// Creates a new script state.
  2449. ///
  2450. /// @param script Script code
  2451. /// @param pos Position in the code
  2452. /// @param rid Who is running the script (attached player)
  2453. /// @param oid Where the code is being run (npc 'object')
  2454. /// @return Script state
  2455. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2456. {
  2457. struct script_state* st;
  2458. CREATE(st, struct script_state, 1);
  2459. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2460. st->stack->sp = 0;
  2461. st->stack->sp_max = 64;
  2462. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2463. st->stack->defsp = st->stack->sp;
  2464. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2465. st->state = RUN;
  2466. st->script = script;
  2467. //st->scriptroot = script;
  2468. st->pos = pos;
  2469. st->rid = rid;
  2470. st->oid = oid;
  2471. st->sleep.timer = INVALID_TIMER;
  2472. return st;
  2473. }
  2474. /// Frees a script state.
  2475. ///
  2476. /// @param st Script state
  2477. void script_free_state(struct script_state* st)
  2478. {
  2479. if(st->bk_st)
  2480. {// backup was not restored
  2481. 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);
  2482. }
  2483. if( st->sleep.timer != INVALID_TIMER )
  2484. delete_timer(st->sleep.timer, run_script_timer);
  2485. script_free_vars(st->stack->var_function);
  2486. aFree(st->stack->var_function);
  2487. pop_stack(st, 0, st->stack->sp);
  2488. aFree(st->stack->stack_data);
  2489. aFree(st->stack);
  2490. st->pos = -1;
  2491. aFree(st);
  2492. }
  2493. //
  2494. // 実行部main
  2495. //
  2496. /*==========================================
  2497. * コマンドの読み取り
  2498. *------------------------------------------*/
  2499. c_op get_com(unsigned char *script,int *pos)
  2500. {
  2501. int i = 0, j = 0;
  2502. if(script[*pos]>=0x80){
  2503. return C_INT;
  2504. }
  2505. while(script[*pos]>=0x40){
  2506. i=script[(*pos)++]<<j;
  2507. j+=6;
  2508. }
  2509. return (c_op)(i+(script[(*pos)++]<<j));
  2510. }
  2511. /*==========================================
  2512. * 数値の所得
  2513. *------------------------------------------*/
  2514. int get_num(unsigned char *script,int *pos)
  2515. {
  2516. int i,j;
  2517. i=0; j=0;
  2518. while(script[*pos]>=0xc0){
  2519. i+=(script[(*pos)++]&0x7f)<<j;
  2520. j+=6;
  2521. }
  2522. return i+((script[(*pos)++]&0x7f)<<j);
  2523. }
  2524. /*==========================================
  2525. * スタックから値を取り出す
  2526. *------------------------------------------*/
  2527. int pop_val(struct script_state* st)
  2528. {
  2529. if(st->stack->sp<=0)
  2530. return 0;
  2531. st->stack->sp--;
  2532. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2533. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2534. return st->stack->stack_data[st->stack->sp].u.num;
  2535. return 0;
  2536. }
  2537. /// Ternary operators
  2538. /// test ? if_true : if_false
  2539. void op_3(struct script_state* st, int op)
  2540. {
  2541. struct script_data* data;
  2542. int flag = 0;
  2543. data = script_getdatatop(st, -3);
  2544. get_val(st, data);
  2545. if( data_isstring(data) )
  2546. flag = data->u.str[0];// "" -> false
  2547. else if( data_isint(data) )
  2548. flag = data->u.num;// 0 -> false
  2549. else
  2550. {
  2551. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2552. script_reportdata(data);
  2553. script_reportsrc(st);
  2554. script_removetop(st, -3, 0);
  2555. script_pushnil(st);
  2556. return;
  2557. }
  2558. if( flag )
  2559. script_pushcopytop(st, -2);
  2560. else
  2561. script_pushcopytop(st, -1);
  2562. script_removetop(st, -4, -1);
  2563. }
  2564. /// Binary string operators
  2565. /// s1 EQ s2 -> i
  2566. /// s1 NE s2 -> i
  2567. /// s1 GT s2 -> i
  2568. /// s1 GE s2 -> i
  2569. /// s1 LT s2 -> i
  2570. /// s1 LE s2 -> i
  2571. /// s1 ADD s2 -> s
  2572. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2573. {
  2574. int a = 0;
  2575. switch(op){
  2576. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2577. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2578. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2579. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2580. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2581. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2582. case C_ADD:
  2583. {
  2584. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2585. strcpy(buf, s1);
  2586. strcat(buf, s2);
  2587. script_pushstr(st, buf);
  2588. return;
  2589. }
  2590. default:
  2591. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2592. script_reportsrc(st);
  2593. script_pushnil(st);
  2594. st->state = END;
  2595. return;
  2596. }
  2597. script_pushint(st,a);
  2598. }
  2599. /// Binary number operators
  2600. /// i OP i -> i
  2601. void op_2num(struct script_state* st, int op, int i1, int i2)
  2602. {
  2603. int ret;
  2604. double ret_double;
  2605. switch( op )
  2606. {
  2607. case C_AND: ret = i1 & i2; break;
  2608. case C_OR: ret = i1 | i2; break;
  2609. case C_XOR: ret = i1 ^ i2; break;
  2610. case C_LAND: ret = (i1 && i2); break;
  2611. case C_LOR: ret = (i1 || i2); break;
  2612. case C_EQ: ret = (i1 == i2); break;
  2613. case C_NE: ret = (i1 != i2); break;
  2614. case C_GT: ret = (i1 > i2); break;
  2615. case C_GE: ret = (i1 >= i2); break;
  2616. case C_LT: ret = (i1 < i2); break;
  2617. case C_LE: ret = (i1 <= i2); break;
  2618. case C_R_SHIFT: ret = i1>>i2; break;
  2619. case C_L_SHIFT: ret = i1<<i2; break;
  2620. case C_DIV:
  2621. case C_MOD:
  2622. if( i2 == 0 )
  2623. {
  2624. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2625. script_reportsrc(st);
  2626. script_pushnil(st);
  2627. st->state = END;
  2628. return;
  2629. }
  2630. else if( op == C_DIV )
  2631. ret = i1 / i2;
  2632. else//if( op == C_MOD )
  2633. ret = i1 % i2;
  2634. break;
  2635. default:
  2636. switch( op )
  2637. {// operators that can overflow/underflow
  2638. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2639. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2640. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2641. default:
  2642. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2643. script_reportsrc(st);
  2644. script_pushnil(st);
  2645. return;
  2646. }
  2647. if( ret_double < (double)INT_MIN )
  2648. {
  2649. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2650. script_reportsrc(st);
  2651. ret = INT_MIN;
  2652. }
  2653. else if( ret_double > (double)INT_MAX )
  2654. {
  2655. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2656. script_reportsrc(st);
  2657. ret = INT_MAX;
  2658. }
  2659. }
  2660. script_pushint(st, ret);
  2661. }
  2662. /// Binary operators
  2663. void op_2(struct script_state *st, int op)
  2664. {
  2665. struct script_data* left;
  2666. struct script_data* right;
  2667. left = script_getdatatop(st, -2);
  2668. right = script_getdatatop(st, -1);
  2669. get_val(st, left);
  2670. get_val(st, right);
  2671. // automatic conversions
  2672. switch( op )
  2673. {
  2674. case C_ADD:
  2675. if( data_isint(left) && data_isstring(right) )
  2676. {// convert int-string to string-string
  2677. conv_str(st, left);
  2678. }
  2679. else if( data_isstring(left) && data_isint(right) )
  2680. {// convert string-int to string-string
  2681. conv_str(st, right);
  2682. }
  2683. break;
  2684. }
  2685. if( data_isstring(left) && data_isstring(right) )
  2686. {// ss => op_2str
  2687. op_2str(st, op, left->u.str, right->u.str);
  2688. script_removetop(st, -3, -1);// pop the two values before the top one
  2689. }
  2690. else if( data_isint(left) && data_isint(right) )
  2691. {// ii => op_2num
  2692. int i1 = left->u.num;
  2693. int i2 = right->u.num;
  2694. script_removetop(st, -2, 0);
  2695. op_2num(st, op, i1, i2);
  2696. }
  2697. else
  2698. {// invalid argument
  2699. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2700. script_reportdata(left);
  2701. script_reportdata(right);
  2702. script_reportsrc(st);
  2703. script_removetop(st, -2, 0);
  2704. script_pushnil(st);
  2705. st->state = END;
  2706. }
  2707. }
  2708. /// Unary operators
  2709. /// NEG i -> i
  2710. /// NOT i -> i
  2711. /// LNOT i -> i
  2712. void op_1(struct script_state* st, int op)
  2713. {
  2714. struct script_data* data;
  2715. int i1;
  2716. data = script_getdatatop(st, -1);
  2717. get_val(st, data);
  2718. if( !data_isint(data) )
  2719. {// not a number
  2720. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2721. script_reportdata(data);
  2722. script_reportsrc(st);
  2723. script_pushnil(st);
  2724. st->state = END;
  2725. return;
  2726. }
  2727. i1 = data->u.num;
  2728. script_removetop(st, -1, 0);
  2729. switch( op )
  2730. {
  2731. case C_NEG: i1 = -i1; break;
  2732. case C_NOT: i1 = ~i1; break;
  2733. case C_LNOT: i1 = !i1; break;
  2734. default:
  2735. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2736. script_reportsrc(st);
  2737. script_pushnil(st);
  2738. st->state = END;
  2739. return;
  2740. }
  2741. script_pushint(st, i1);
  2742. }
  2743. /// Checks the type of all arguments passed to a built-in function.
  2744. ///
  2745. /// @param st Script state whose stack arguments should be inspected.
  2746. /// @param func Built-in function for which the arguments are intended.
  2747. static void script_check_buildin_argtype(struct script_state* st, int func)
  2748. {
  2749. char type;
  2750. int idx, invalid = 0;
  2751. script_function* sf = &buildin_func[str_data[func].val];
  2752. for( idx = 2; script_hasdata(st, idx); idx++ )
  2753. {
  2754. struct script_data* data = script_getdata(st, idx);
  2755. type = sf->arg[idx-2];
  2756. if( type == '?' || type == '*' )
  2757. {// optional argument or unknown number of optional parameters ( no types are after this )
  2758. break;
  2759. }
  2760. else if( type == 0 )
  2761. {// more arguments than necessary ( should not happen, as it is checked before )
  2762. ShowWarning("Found more arguments than necessary.\n");
  2763. invalid++;
  2764. break;
  2765. }
  2766. else
  2767. {
  2768. const char* name = NULL;
  2769. if( data_isreference(data) )
  2770. {// get name for variables to determine the type they refer to
  2771. name = reference_getname(data);
  2772. }
  2773. switch( type )
  2774. {
  2775. case 'v':
  2776. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  2777. {// variant
  2778. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  2779. script_reportdata(data);
  2780. invalid++;
  2781. }
  2782. break;
  2783. case 's':
  2784. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  2785. {// string
  2786. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  2787. script_reportdata(data);
  2788. invalid++;
  2789. }
  2790. break;
  2791. case 'i':
  2792. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  2793. {// int ( params and constants are always int )
  2794. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  2795. script_reportdata(data);
  2796. invalid++;
  2797. }
  2798. break;
  2799. case 'r':
  2800. if( !data_isreference(data) )
  2801. {// variables
  2802. ShowWarning("Unexpected type for argument %d. Expected variable.\n", idx-1);
  2803. script_reportdata(data);
  2804. invalid++;
  2805. }
  2806. break;
  2807. case 'l':
  2808. if( !data_islabel(data) && !data_isfunclabel(data) )
  2809. {// label
  2810. ShowWarning("Unexpected type for argument %d. Expected label.\n", idx-1);
  2811. script_reportdata(data);
  2812. invalid++;
  2813. }
  2814. break;
  2815. }
  2816. }
  2817. }
  2818. if(invalid)
  2819. {
  2820. ShowDebug("Function: %s\n", get_str(func));
  2821. script_reportsrc(st);
  2822. }
  2823. }
  2824. /// Executes a buildin command.
  2825. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2826. int run_func(struct script_state *st)
  2827. {
  2828. struct script_data* data;
  2829. int i,start_sp,end_sp,func;
  2830. end_sp = st->stack->sp;// position after the last argument
  2831. for( i = end_sp-1; i > 0 ; --i )
  2832. if( st->stack->stack_data[i].type == C_ARG )
  2833. break;
  2834. if( i == 0 )
  2835. {
  2836. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2837. st->state = END;
  2838. script_reportsrc(st);
  2839. return 1;
  2840. }
  2841. start_sp = i-1;// C_NAME of the command
  2842. st->start = start_sp;
  2843. st->end = end_sp;
  2844. data = &st->stack->stack_data[st->start];
  2845. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2846. func = data->u.num;
  2847. else
  2848. {
  2849. ShowError("script:run_func: not a buildin command.\n");
  2850. script_reportdata(data);
  2851. script_reportsrc(st);
  2852. st->state = END;
  2853. return 1;
  2854. }
  2855. if( script_config.warn_func_mismatch_argtypes )
  2856. {
  2857. script_check_buildin_argtype(st, func);
  2858. }
  2859. if(str_data[func].func){
  2860. if (str_data[func].func(st)) //Report error
  2861. script_reportsrc(st);
  2862. } else {
  2863. 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));
  2864. script_reportsrc(st);
  2865. st->state = END;
  2866. }
  2867. // Stack's datum are used when re-running functions [Eoe]
  2868. if( st->state == RERUNLINE )
  2869. return 0;
  2870. pop_stack(st, st->start, st->end);
  2871. if( st->state == RETFUNC )
  2872. {// return from a user-defined function
  2873. struct script_retinfo* ri;
  2874. int olddefsp = st->stack->defsp;
  2875. int nargs;
  2876. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2877. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2878. {
  2879. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  2880. script_reportsrc(st);
  2881. st->state = END;
  2882. return 1;
  2883. }
  2884. script_free_vars( st->stack->var_function );
  2885. aFree(st->stack->var_function);
  2886. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  2887. nargs = ri->nargs;
  2888. st->pos = ri->pos;
  2889. st->script = ri->script;
  2890. st->stack->var_function = ri->var_function;
  2891. st->stack->defsp = ri->defsp;
  2892. memset(ri, 0, sizeof(struct script_retinfo));
  2893. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  2894. st->state = GOTO;
  2895. }
  2896. return 0;
  2897. }
  2898. /*==========================================
  2899. * script execution
  2900. *------------------------------------------*/
  2901. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2902. {
  2903. struct script_state *st;
  2904. if( rootscript == NULL || pos < 0 )
  2905. return;
  2906. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  2907. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  2908. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  2909. st = script_alloc_state(rootscript, pos, rid, oid);
  2910. run_script_main(st);
  2911. }
  2912. void script_stop_sleeptimers(int id)
  2913. {
  2914. struct script_state* st;
  2915. for(;;)
  2916. {
  2917. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2918. if( st == NULL )
  2919. break; // no more sleep timers
  2920. script_free_state(st);
  2921. }
  2922. }
  2923. /*==========================================
  2924. * 指定ノードをsleep_dbから削除
  2925. *------------------------------------------*/
  2926. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2927. {
  2928. struct linkdb_node *retnode;
  2929. if( n == NULL)
  2930. return NULL;
  2931. if( n->prev == NULL )
  2932. sleep_db = n->next;
  2933. else
  2934. n->prev->next = n->next;
  2935. if( n->next )
  2936. n->next->prev = n->prev;
  2937. retnode = n->next;
  2938. aFree( n );
  2939. return retnode; // 次のノードを返す
  2940. }
  2941. /*==========================================
  2942. * sleep用タイマー関数
  2943. *------------------------------------------*/
  2944. int run_script_timer(int tid, unsigned int tick, int id, intptr data)
  2945. {
  2946. struct script_state *st = (struct script_state *)data;
  2947. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2948. TBL_PC *sd = map_id2sd(st->rid);
  2949. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2950. { //Character mismatch. Cancel execution.
  2951. st->rid = 0;
  2952. st->state = END;
  2953. }
  2954. while( node && st->sleep.timer != -1 ) {
  2955. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2956. script_erase_sleepdb(node);
  2957. st->sleep.timer = INVALID_TIMER;
  2958. break;
  2959. }
  2960. node = node->next;
  2961. }
  2962. if(st->state != RERUNLINE)
  2963. st->sleep.tick = 0;
  2964. run_script_main(st);
  2965. return 0;
  2966. }
  2967. /// Detaches script state from possibly attached character and restores it's previous script if any.
  2968. ///
  2969. /// @param st Script state to detach.
  2970. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  2971. static void script_detach_state(struct script_state* st, bool dequeue_event)
  2972. {
  2973. struct map_session_data* sd;
  2974. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  2975. {
  2976. sd->st = st->bk_st;
  2977. sd->npc_id = st->bk_npcid;
  2978. if(st->bk_st)
  2979. {
  2980. //Remove tag for removal.
  2981. st->bk_st = NULL;
  2982. st->bk_npcid = 0;
  2983. }
  2984. else if(dequeue_event)
  2985. {
  2986. npc_event_dequeue(sd);
  2987. }
  2988. }
  2989. else if(st->bk_st)
  2990. {// rid was set to 0, before detaching the script state
  2991. 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);
  2992. script_reportsrc(st->bk_st);
  2993. script_free_state(st->bk_st);
  2994. st->bk_st = NULL;
  2995. }
  2996. }
  2997. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  2998. ///
  2999. /// @param st Script state to attach.
  3000. static void script_attach_state(struct script_state* st)
  3001. {
  3002. struct map_session_data* sd;
  3003. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3004. {
  3005. if(st!=sd->st)
  3006. {
  3007. if(st->bk_st)
  3008. {// there is already a backup
  3009. 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);
  3010. }
  3011. st->bk_st = sd->st;
  3012. st->bk_npcid = sd->npc_id;
  3013. }
  3014. sd->st = st;
  3015. sd->npc_id = st->oid;
  3016. }
  3017. }
  3018. /*==========================================
  3019. * スクリプトの実行メイン部分
  3020. *------------------------------------------*/
  3021. void run_script_main(struct script_state *st)
  3022. {
  3023. int cmdcount=script_config.check_cmdcount;
  3024. int gotocount=script_config.check_gotocount;
  3025. TBL_PC *sd;
  3026. struct script_stack *stack=st->stack;
  3027. struct npc_data *nd;
  3028. script_attach_state(st);
  3029. nd = map_id2nd(st->oid);
  3030. if( nd && map[nd->bl.m].instance_id > 0 )
  3031. st->instance_id = map[nd->bl.m].instance_id;
  3032. if(st->state == RERUNLINE) {
  3033. run_func(st);
  3034. if(st->state == GOTO)
  3035. st->state = RUN;
  3036. } else if(st->state != END)
  3037. st->state = RUN;
  3038. while(st->state == RUN)
  3039. {
  3040. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3041. switch(c){
  3042. case C_EOL:
  3043. if( stack->defsp > stack->sp )
  3044. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3045. else
  3046. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3047. break;
  3048. case C_INT:
  3049. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3050. break;
  3051. case C_POS:
  3052. case C_NAME:
  3053. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3054. st->pos+=3;
  3055. break;
  3056. case C_ARG:
  3057. push_val(stack,c,0);
  3058. break;
  3059. case C_STR:
  3060. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3061. while(st->script->script_buf[st->pos++]);
  3062. break;
  3063. case C_FUNC:
  3064. run_func(st);
  3065. if(st->state==GOTO){
  3066. st->state = RUN;
  3067. if( gotocount>0 && (--gotocount)<=0 ){
  3068. ShowError("run_script: infinity loop !\n");
  3069. script_reportsrc(st);
  3070. st->state=END;
  3071. }
  3072. }
  3073. break;
  3074. case C_NEG:
  3075. case C_NOT:
  3076. case C_LNOT:
  3077. op_1(st ,c);
  3078. break;
  3079. case C_ADD:
  3080. case C_SUB:
  3081. case C_MUL:
  3082. case C_DIV:
  3083. case C_MOD:
  3084. case C_EQ:
  3085. case C_NE:
  3086. case C_GT:
  3087. case C_GE:
  3088. case C_LT:
  3089. case C_LE:
  3090. case C_AND:
  3091. case C_OR:
  3092. case C_XOR:
  3093. case C_LAND:
  3094. case C_LOR:
  3095. case C_R_SHIFT:
  3096. case C_L_SHIFT:
  3097. op_2(st, c);
  3098. break;
  3099. case C_OP3:
  3100. op_3(st, c);
  3101. break;
  3102. case C_NOP:
  3103. st->state=END;
  3104. break;
  3105. default:
  3106. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3107. st->state=END;
  3108. break;
  3109. }
  3110. if( cmdcount>0 && (--cmdcount)<=0 ){
  3111. ShowError("run_script: infinity loop !\n");
  3112. script_reportsrc(st);
  3113. st->state=END;
  3114. }
  3115. }
  3116. if(st->sleep.tick > 0) {
  3117. //Restore previous script
  3118. script_detach_state(st, false);
  3119. //Delay execution
  3120. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3121. st->sleep.charid = sd?sd->status.char_id:0;
  3122. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3123. run_script_timer, st->sleep.charid, (intptr)st);
  3124. linkdb_insert(&sleep_db, (void*)st->oid, st);
  3125. }
  3126. else if(st->state != END && st->rid){
  3127. //Resume later (st is already attached to player).
  3128. if(st->bk_st) {
  3129. ShowWarning("Unable to restore stack! Double continuation!\n");
  3130. //Report BOTH scripts to see if that can help somehow.
  3131. ShowDebug("Previous script (lost):\n");
  3132. script_reportsrc(st->bk_st);
  3133. ShowDebug("Current script:\n");
  3134. script_reportsrc(st);
  3135. script_free_state(st->bk_st);
  3136. st->bk_st = NULL;
  3137. }
  3138. } else {
  3139. //Dispose of script.
  3140. if ((sd = map_id2sd(st->rid))!=NULL)
  3141. { //Restore previous stack and save char.
  3142. if(sd->state.using_fake_npc){
  3143. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3144. sd->state.using_fake_npc = 0;
  3145. }
  3146. //Restore previous script if any.
  3147. script_detach_state(st, true);
  3148. if (sd->state.reg_dirty&2)
  3149. intif_saveregistry(sd,2);
  3150. if (sd->state.reg_dirty&1)
  3151. intif_saveregistry(sd,1);
  3152. }
  3153. script_free_state(st);
  3154. st = NULL;
  3155. }
  3156. }
  3157. int script_config_read(char *cfgName)
  3158. {
  3159. int i;
  3160. char line[1024],w1[1024],w2[1024];
  3161. FILE *fp;
  3162. fp=fopen(cfgName,"r");
  3163. if(fp==NULL){
  3164. ShowError("file not found: [%s]\n", cfgName);
  3165. return 1;
  3166. }
  3167. while(fgets(line, sizeof(line), fp))
  3168. {
  3169. if(line[0] == '/' && line[1] == '/')
  3170. continue;
  3171. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3172. if(i!=2)
  3173. continue;
  3174. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3175. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3176. }
  3177. else if(strcmpi(w1,"check_cmdcount")==0) {
  3178. script_config.check_cmdcount = config_switch(w2);
  3179. }
  3180. else if(strcmpi(w1,"check_gotocount")==0) {
  3181. script_config.check_gotocount = config_switch(w2);
  3182. }
  3183. else if(strcmpi(w1,"input_min_value")==0) {
  3184. script_config.input_min_value = config_switch(w2);
  3185. }
  3186. else if(strcmpi(w1,"input_max_value")==0) {
  3187. script_config.input_max_value = config_switch(w2);
  3188. }
  3189. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3190. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3191. }
  3192. else if(strcmpi(w1,"import")==0){
  3193. script_config_read(w2);
  3194. }
  3195. }
  3196. fclose(fp);
  3197. return 0;
  3198. }
  3199. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3200. {
  3201. struct script_code *code = (struct script_code *)data;
  3202. if(code){
  3203. script_free_vars( &code->script_vars );
  3204. aFree( code->script_buf );
  3205. aFree( code );
  3206. }
  3207. return 0;
  3208. }
  3209. static int do_final_autobonus_sub (DBKey key,void *data,va_list ap)
  3210. {
  3211. struct script_code *script = (struct script_code *)data;
  3212. if( script )
  3213. script_free_code(script);
  3214. return 0;
  3215. }
  3216. void script_run_autobonus(const char *autobonus, int id, int pos)
  3217. {
  3218. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3219. if( script )
  3220. {
  3221. current_equip_item_index = pos;
  3222. run_script(script,0,id,0);
  3223. }
  3224. }
  3225. void script_add_autobonus(const char *autobonus)
  3226. {
  3227. if( strdb_get(autobonus_db, autobonus) == NULL )
  3228. {
  3229. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3230. if( script )
  3231. strdb_put(autobonus_db, autobonus, script);
  3232. }
  3233. }
  3234. /*==========================================
  3235. * 終了
  3236. *------------------------------------------*/
  3237. int do_final_script()
  3238. {
  3239. #ifdef DEBUG_HASH
  3240. if (battle_config.etc_log)
  3241. {
  3242. FILE *fp = fopen("hash_dump.txt","wt");
  3243. if(fp) {
  3244. int i,count[SCRIPT_HASH_SIZE];
  3245. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3246. int n=0;
  3247. int min=INT_MAX,max=0,zero=0;
  3248. double mean=0.0f;
  3249. double median=0.0f;
  3250. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3251. memset(count, 0, sizeof(count));
  3252. fprintf(fp,"num : hash : data_name\n");
  3253. fprintf(fp,"---------------------------------------------------------------\n");
  3254. for(i=LABEL_START; i<str_num; i++) {
  3255. unsigned int h = calc_hash(get_str(i));
  3256. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3257. ++count[h];
  3258. }
  3259. fprintf(fp,"--------------------\n\n");
  3260. memset(count2, 0, sizeof(count2));
  3261. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3262. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3263. if(min > count[i])
  3264. min = count[i]; // minimun count of collision
  3265. if(max < count[i])
  3266. max = count[i]; // maximun count of collision
  3267. if(count[i] == 0)
  3268. zero++;
  3269. ++count2[count[i]];
  3270. }
  3271. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3272. for( i=min; i <= max; ++i ){
  3273. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3274. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3275. }
  3276. for( i=min; i <= max; ++i ){
  3277. n += count2[i];
  3278. if( n*2 >= SCRIPT_HASH_SIZE )
  3279. {
  3280. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3281. median = (i+i+1)/2.0f;
  3282. else
  3283. median = i;
  3284. break;
  3285. }
  3286. }
  3287. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3288. fclose(fp);
  3289. }
  3290. }
  3291. #endif
  3292. mapreg_final();
  3293. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3294. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3295. autobonus_db->destroy(autobonus_db, do_final_autobonus_sub);
  3296. if(sleep_db) {
  3297. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3298. while(n) {
  3299. struct script_state *st = (struct script_state *)n->data;
  3300. script_free_state(st);
  3301. n = n->next;
  3302. }
  3303. linkdb_final(&sleep_db);
  3304. }
  3305. if (str_data)
  3306. aFree(str_data);
  3307. if (str_buf)
  3308. aFree(str_buf);
  3309. return 0;
  3310. }
  3311. /*==========================================
  3312. * 初期化
  3313. *------------------------------------------*/
  3314. int do_init_script()
  3315. {
  3316. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3317. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3318. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3319. mapreg_init();
  3320. return 0;
  3321. }
  3322. int script_reload()
  3323. {
  3324. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3325. scriptlabel_db->clear(scriptlabel_db, NULL);
  3326. if(sleep_db) {
  3327. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3328. while(n) {
  3329. struct script_state *st = (struct script_state *)n->data;
  3330. script_free_state(st);
  3331. n = n->next;
  3332. }
  3333. linkdb_final(&sleep_db);
  3334. }
  3335. mapreg_reload();
  3336. return 0;
  3337. }
  3338. //-----------------------------------------------------------------------------
  3339. // buildin functions
  3340. //
  3341. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3342. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3343. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3344. /////////////////////////////////////////////////////////////////////
  3345. // NPC interaction
  3346. //
  3347. /// Appends a message to the npc dialog.
  3348. /// If a dialog doesn't exist yet, one is created.
  3349. ///
  3350. /// mes "<message>";
  3351. BUILDIN_FUNC(mes)
  3352. {
  3353. TBL_PC* sd = script_rid2sd(st);
  3354. if( sd == NULL )
  3355. return 0;
  3356. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3357. return 0;
  3358. }
  3359. /// Displays the button 'next' in the npc dialog.
  3360. /// The dialog text is cleared and the script continues when the button is pressed.
  3361. ///
  3362. /// next;
  3363. BUILDIN_FUNC(next)
  3364. {
  3365. TBL_PC* sd;
  3366. sd = script_rid2sd(st);
  3367. if( sd == NULL )
  3368. return 0;
  3369. st->state = STOP;
  3370. clif_scriptnext(sd, st->oid);
  3371. return 0;
  3372. }
  3373. /// Ends the script and displays the button 'close' on the npc dialog.
  3374. /// The dialog is closed when the button is pressed.
  3375. ///
  3376. /// close;
  3377. BUILDIN_FUNC(close)
  3378. {
  3379. TBL_PC* sd;
  3380. sd = script_rid2sd(st);
  3381. if( sd == NULL )
  3382. return 0;
  3383. st->state = END;
  3384. clif_scriptclose(sd, st->oid);
  3385. return 0;
  3386. }
  3387. /// Displays the button 'close' on the npc dialog.
  3388. /// The dialog is closed and the script continues when the button is pressed.
  3389. ///
  3390. /// close2;
  3391. BUILDIN_FUNC(close2)
  3392. {
  3393. TBL_PC* sd;
  3394. sd = script_rid2sd(st);
  3395. if( sd == NULL )
  3396. return 0;
  3397. st->state = STOP;
  3398. clif_scriptclose(sd, st->oid);
  3399. return 0;
  3400. }
  3401. /// Counts the number of valid and total number of options in 'str'
  3402. /// If max_count > 0 the counting stops when that valid option is reached
  3403. /// total is incremented for each option (NULL is supported)
  3404. static int menu_countoptions(const char* str, int max_count, int* total)
  3405. {
  3406. int count = 0;
  3407. int bogus_total;
  3408. if( total == NULL )
  3409. total = &bogus_total;
  3410. ++(*total);
  3411. // initial empty options
  3412. while( *str == ':' )
  3413. {
  3414. ++str;
  3415. ++(*total);
  3416. }
  3417. // count menu options
  3418. while( *str != '\0' )
  3419. {
  3420. ++count;
  3421. --max_count;
  3422. if( max_count == 0 )
  3423. break;
  3424. while( *str != ':' && *str != '\0' )
  3425. ++str;
  3426. while( *str == ':' )
  3427. {
  3428. ++str;
  3429. ++(*total);
  3430. }
  3431. }
  3432. return count;
  3433. }
  3434. /// Displays a menu with options and goes to the target label.
  3435. /// The script is stopped if cancel is pressed.
  3436. /// Options with no text are not displayed in the client.
  3437. ///
  3438. /// Options can be grouped together, separated by the character ':' in the text:
  3439. /// ex: menu "A:B:C",L_target;
  3440. /// All these options go to the specified target label.
  3441. ///
  3442. /// The index of the selected option is put in the variable @menu.
  3443. /// Indexes start with 1 and are consistent with grouped and empty options.
  3444. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3445. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3446. ///
  3447. /// NOTE: the client closes the npc dialog when cancel is pressed
  3448. ///
  3449. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3450. BUILDIN_FUNC(menu)
  3451. {
  3452. int i;
  3453. const char* text;
  3454. TBL_PC* sd;
  3455. sd = script_rid2sd(st);
  3456. if( sd == NULL )
  3457. return 0;
  3458. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3459. if( sd->state.menu_or_input == 0 )
  3460. {
  3461. struct StringBuf buf;
  3462. struct script_data* data;
  3463. if( script_lastdata(st) % 2 == 0 )
  3464. {// argument count is not even (1st argument is at index 2)
  3465. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3466. st->state = END;
  3467. return 1;
  3468. }
  3469. StringBuf_Init(&buf);
  3470. sd->npc_menu = 0;
  3471. for( i = 2; i < script_lastdata(st); i += 2 )
  3472. {
  3473. // menu options
  3474. text = script_getstr(st, i);
  3475. // target label
  3476. data = script_getdata(st, i+1);
  3477. if( !data_islabel(data) )
  3478. {// not a label
  3479. StringBuf_Destroy(&buf);
  3480. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3481. script_reportdata(data);
  3482. st->state = END;
  3483. return 1;
  3484. }
  3485. // append option(s)
  3486. if( text[0] == '\0' )
  3487. continue;// empty string, ignore
  3488. if( sd->npc_menu > 0 )
  3489. StringBuf_AppendStr(&buf, ":");
  3490. StringBuf_AppendStr(&buf, text);
  3491. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3492. }
  3493. st->state = RERUNLINE;
  3494. sd->state.menu_or_input = 1;
  3495. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3496. StringBuf_Destroy(&buf);
  3497. if( sd->npc_menu >= 0xff )
  3498. {// 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
  3499. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3500. script_reportsrc(st);
  3501. }
  3502. }
  3503. else if( sd->npc_menu == 0xff )
  3504. {// Cancel was pressed
  3505. sd->state.menu_or_input = 0;
  3506. st->state = END;
  3507. }
  3508. else
  3509. {// goto target label
  3510. int menu = 0;
  3511. sd->state.menu_or_input = 0;
  3512. if( sd->npc_menu <= 0 )
  3513. {
  3514. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3515. st->state = END;
  3516. return 1;
  3517. }
  3518. // get target label
  3519. for( i = 2; i < script_lastdata(st); i += 2 )
  3520. {
  3521. text = script_getstr(st, i);
  3522. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3523. if( sd->npc_menu <= 0 )
  3524. break;// entry found
  3525. }
  3526. if( sd->npc_menu > 0 )
  3527. {// Invalid selection
  3528. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3529. st->state = END;
  3530. return 1;
  3531. }
  3532. if( !data_islabel(script_getdata(st, i + 1)) )
  3533. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3534. ShowError("script:menu: unexpected data in label argument\n");
  3535. script_reportdata(script_getdata(st, i + 1));
  3536. st->state = END;
  3537. return 1;
  3538. }
  3539. pc_setreg(sd, add_str("@menu"), menu);
  3540. st->pos = script_getnum(st, i + 1);
  3541. st->state = GOTO;
  3542. }
  3543. return 0;
  3544. }
  3545. /// Displays a menu with options and returns the selected option.
  3546. /// Behaves like 'menu' without the target labels.
  3547. ///
  3548. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3549. ///
  3550. /// @see menu
  3551. BUILDIN_FUNC(select)
  3552. {
  3553. int i;
  3554. const char* text;
  3555. TBL_PC* sd;
  3556. sd = script_rid2sd(st);
  3557. if( sd == NULL )
  3558. return 0;
  3559. if( sd->state.menu_or_input == 0 )
  3560. {
  3561. struct StringBuf buf;
  3562. StringBuf_Init(&buf);
  3563. sd->npc_menu = 0;
  3564. for( i = 2; i <= script_lastdata(st); ++i )
  3565. {
  3566. text = script_getstr(st, i);
  3567. if( sd->npc_menu > 0 )
  3568. StringBuf_AppendStr(&buf, ":");
  3569. StringBuf_AppendStr(&buf, text);
  3570. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3571. }
  3572. st->state = RERUNLINE;
  3573. sd->state.menu_or_input = 1;
  3574. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3575. StringBuf_Destroy(&buf);
  3576. if( sd->npc_menu >= 0xff )
  3577. {
  3578. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3579. script_reportsrc(st);
  3580. }
  3581. }
  3582. else if( sd->npc_menu == 0xff )
  3583. {// Cancel was pressed
  3584. sd->state.menu_or_input = 0;
  3585. st->state = END;
  3586. }
  3587. else
  3588. {// return selected option
  3589. int menu = 0;
  3590. sd->state.menu_or_input = 0;
  3591. for( i = 2; i <= script_lastdata(st); ++i )
  3592. {
  3593. text = script_getstr(st, i);
  3594. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3595. if( sd->npc_menu <= 0 )
  3596. break;// entry found
  3597. }
  3598. pc_setreg(sd, add_str("@menu"), menu);
  3599. script_pushint(st, menu);
  3600. st->state = RUN;
  3601. }
  3602. return 0;
  3603. }
  3604. /// Displays a menu with options and returns the selected option.
  3605. /// Behaves like 'menu' without the target labels, except when cancel is
  3606. /// pressed.
  3607. /// When cancel is pressed, the script continues and 255 is returned.
  3608. ///
  3609. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3610. ///
  3611. /// @see menu
  3612. BUILDIN_FUNC(prompt)
  3613. {
  3614. int i;
  3615. const char *text;
  3616. TBL_PC* sd;
  3617. sd = script_rid2sd(st);
  3618. if( sd == NULL )
  3619. return 0;
  3620. if( sd->state.menu_or_input == 0 )
  3621. {
  3622. struct StringBuf buf;
  3623. StringBuf_Init(&buf);
  3624. sd->npc_menu = 0;
  3625. for( i = 2; i <= script_lastdata(st); ++i )
  3626. {
  3627. text = script_getstr(st, i);
  3628. if( sd->npc_menu > 0 )
  3629. StringBuf_AppendStr(&buf, ":");
  3630. StringBuf_AppendStr(&buf, text);
  3631. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3632. }
  3633. st->state = RERUNLINE;
  3634. sd->state.menu_or_input = 1;
  3635. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3636. StringBuf_Destroy(&buf);
  3637. if( sd->npc_menu >= 0xff )
  3638. {
  3639. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3640. script_reportsrc(st);
  3641. }
  3642. }
  3643. else if( sd->npc_menu == 0xff )
  3644. {// Cancel was pressed
  3645. sd->state.menu_or_input = 0;
  3646. pc_setreg(sd, add_str("@menu"), 0xff);
  3647. script_pushint(st, 0xff);
  3648. st->state = RUN;
  3649. }
  3650. else
  3651. {// return selected option
  3652. int menu = 0;
  3653. sd->state.menu_or_input = 0;
  3654. for( i = 2; i <= script_lastdata(st); ++i )
  3655. {
  3656. text = script_getstr(st, i);
  3657. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3658. if( sd->npc_menu <= 0 )
  3659. break;// entry found
  3660. }
  3661. pc_setreg(sd, add_str("@menu"), menu);
  3662. script_pushint(st, menu);
  3663. st->state = RUN;
  3664. }
  3665. return 0;
  3666. }
  3667. /////////////////////////////////////////////////////////////////////
  3668. // ...
  3669. //
  3670. /// Jumps to the target script label.
  3671. ///
  3672. /// goto <label>;
  3673. BUILDIN_FUNC(goto)
  3674. {
  3675. if( !data_islabel(script_getdata(st,2)) )
  3676. {
  3677. ShowError("script:goto: not a label\n");
  3678. script_reportdata(script_getdata(st,2));
  3679. st->state = END;
  3680. return 1;
  3681. }
  3682. st->pos = script_getnum(st,2);
  3683. st->state = GOTO;
  3684. return 0;
  3685. }
  3686. /*==========================================
  3687. * user-defined function call
  3688. *------------------------------------------*/
  3689. BUILDIN_FUNC(callfunc)
  3690. {
  3691. int i, j;
  3692. struct script_retinfo* ri;
  3693. struct script_code* scr;
  3694. const char* str = script_getstr(st,2);
  3695. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3696. if( !scr )
  3697. {
  3698. ShowError("script:callfunc: function not found! [%s]\n", str);
  3699. st->state = END;
  3700. return 1;
  3701. }
  3702. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3703. {
  3704. struct script_data* data = push_copy(st->stack,i);
  3705. if( data_isreference(data) && !data->ref )
  3706. {
  3707. const char* name = reference_getname(data);
  3708. if( name[0] == '.' && name[1] == '@' )
  3709. data->ref = st->stack->var_function;
  3710. else if( name[0] == '.' )
  3711. data->ref = &st->script->script_vars;
  3712. }
  3713. }
  3714. CREATE(ri, struct script_retinfo, 1);
  3715. ri->script = st->script;// script code
  3716. ri->var_function = st->stack->var_function;// scope variables
  3717. ri->pos = st->pos;// script location
  3718. ri->nargs = j;// argument count
  3719. ri->defsp = st->stack->defsp;// default stack pointer
  3720. push_retinfo(st->stack, ri);
  3721. st->pos = 0;
  3722. st->script = scr;
  3723. st->stack->defsp = st->stack->sp;
  3724. st->state = GOTO;
  3725. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3726. return 0;
  3727. }
  3728. /*==========================================
  3729. * subroutine call
  3730. *------------------------------------------*/
  3731. BUILDIN_FUNC(callsub)
  3732. {
  3733. int i,j;
  3734. struct script_retinfo* ri;
  3735. int pos = script_getnum(st,2);
  3736. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3737. {
  3738. ShowError("script:callsub: argument is not a label\n");
  3739. script_reportdata(script_getdata(st,2));
  3740. st->state = END;
  3741. return 1;
  3742. }
  3743. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3744. {
  3745. struct script_data* data = push_copy(st->stack,i);
  3746. if( data_isreference(data) && !data->ref )
  3747. {
  3748. const char* name = reference_getname(data);
  3749. if( name[0] == '.' && name[1] == '@' )
  3750. data->ref = st->stack->var_function;
  3751. }
  3752. }
  3753. CREATE(ri, struct script_retinfo, 1);
  3754. ri->script = st->script;// script code
  3755. ri->var_function = st->stack->var_function;// scope variables
  3756. ri->pos = st->pos;// script location
  3757. ri->nargs = j;// argument count
  3758. ri->defsp = st->stack->defsp;// default stack pointer
  3759. push_retinfo(st->stack, ri);
  3760. st->pos = pos;
  3761. st->stack->defsp = st->stack->sp;
  3762. st->state = GOTO;
  3763. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3764. return 0;
  3765. }
  3766. /// Retrieves an argument provided to callfunc/callsub.
  3767. /// If the argument doesn't exist
  3768. ///
  3769. /// getarg(<index>{,<default_value>}) -> <value>
  3770. BUILDIN_FUNC(getarg)
  3771. {
  3772. struct script_retinfo* ri;
  3773. int idx;
  3774. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3775. {
  3776. ShowError("script:getarg: no callfunc or callsub!\n");
  3777. st->state = END;
  3778. return 1;
  3779. }
  3780. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3781. idx = script_getnum(st,2);
  3782. if( idx >= 0 && idx < ri->nargs )
  3783. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3784. else if( script_hasdata(st,3) )
  3785. script_pushcopy(st, 3);
  3786. else
  3787. {
  3788. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3789. st->state = END;
  3790. return 1;
  3791. }
  3792. return 0;
  3793. }
  3794. /// Returns from the current function, optionaly returning a value from the functions.
  3795. /// Don't use outside script functions.
  3796. ///
  3797. /// return;
  3798. /// return <value>;
  3799. BUILDIN_FUNC(return)
  3800. {
  3801. if( script_hasdata(st,2) )
  3802. {// return value
  3803. struct script_data* data;
  3804. script_pushcopy(st, 2);
  3805. data = script_getdatatop(st, -1);
  3806. if( data_isreference(data) )
  3807. {
  3808. const char* name = reference_getname(data);
  3809. if( name[0] == '.' && name[1] == '@' )
  3810. {// scope variable
  3811. if( !data->ref || data->ref == st->stack->var_function )
  3812. get_val(st, data);// current scope, convert to value
  3813. }
  3814. else if( name[0] == '.' && !data->ref )
  3815. {// script variable, link to current script
  3816. data->ref = &st->script->script_vars;
  3817. }
  3818. }
  3819. }
  3820. else
  3821. {// no return value
  3822. script_pushnil(st);
  3823. }
  3824. st->state = RETFUNC;
  3825. return 0;
  3826. }
  3827. /// Returns a random number from 0 to <range>-1.
  3828. /// Or returns a random number from <min> to <max>.
  3829. /// If <min> is greater than <max>, their numbers are switched.
  3830. /// rand(<range>) -> <int>
  3831. /// rand(<min>,<max>) -> <int>
  3832. BUILDIN_FUNC(rand)
  3833. {
  3834. int range;
  3835. int min;
  3836. int max;
  3837. if( script_hasdata(st,3) )
  3838. {// min,max
  3839. min = script_getnum(st,2);
  3840. max = script_getnum(st,3);
  3841. if( max < min )
  3842. swap(min, max);
  3843. range = max - min + 1;
  3844. }
  3845. else
  3846. {// range
  3847. min = 0;
  3848. range = script_getnum(st,2);
  3849. }
  3850. if( range <= 1 )
  3851. script_pushint(st, min);
  3852. else
  3853. script_pushint(st, rand()%range + min);
  3854. return 0;
  3855. }
  3856. /*==========================================
  3857. *
  3858. *------------------------------------------*/
  3859. BUILDIN_FUNC(warp)
  3860. {
  3861. int ret;
  3862. int x,y;
  3863. const char* str;
  3864. TBL_PC* sd;
  3865. sd = script_rid2sd(st);
  3866. if( sd == NULL )
  3867. return 0;
  3868. str = script_getstr(st,2);
  3869. x = script_getnum(st,3);
  3870. y = script_getnum(st,4);
  3871. if(strcmp(str,"Random")==0)
  3872. ret = pc_randomwarp(sd,CLR_TELEPORT);
  3873. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3874. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  3875. else
  3876. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  3877. if( ret ) {
  3878. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3879. script_reportsrc(st);
  3880. }
  3881. return 0;
  3882. }
  3883. /*==========================================
  3884. * エリア指定ワープ
  3885. *------------------------------------------*/
  3886. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3887. {
  3888. int x,y;
  3889. unsigned int map;
  3890. map=va_arg(ap, unsigned int);
  3891. x=va_arg(ap,int);
  3892. y=va_arg(ap,int);
  3893. if(map == 0)
  3894. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  3895. else
  3896. pc_setpos((TBL_PC *)bl,map,x,y,CLR_OUTSIGHT);
  3897. return 0;
  3898. }
  3899. BUILDIN_FUNC(areawarp)
  3900. {
  3901. int x,y,m;
  3902. unsigned int index;
  3903. const char *str;
  3904. const char *mapname;
  3905. int x0,y0,x1,y1;
  3906. mapname=script_getstr(st,2);
  3907. x0=script_getnum(st,3);
  3908. y0=script_getnum(st,4);
  3909. x1=script_getnum(st,5);
  3910. y1=script_getnum(st,6);
  3911. str=script_getstr(st,7);
  3912. x=script_getnum(st,8);
  3913. y=script_getnum(st,9);
  3914. if( (m=map_mapname2mapid(mapname))< 0)
  3915. return 0;
  3916. if(strcmp(str,"Random")==0)
  3917. index = 0;
  3918. else if(!(index=mapindex_name2id(str)))
  3919. return 0;
  3920. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  3921. return 0;
  3922. }
  3923. /*==========================================
  3924. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  3925. *------------------------------------------*/
  3926. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  3927. {
  3928. int hp, sp;
  3929. hp = va_arg(ap, int);
  3930. sp = va_arg(ap, int);
  3931. pc_percentheal((TBL_PC *)bl,hp,sp);
  3932. return 0;
  3933. }
  3934. BUILDIN_FUNC(areapercentheal)
  3935. {
  3936. int hp,sp,m;
  3937. const char *mapname;
  3938. int x0,y0,x1,y1;
  3939. mapname=script_getstr(st,2);
  3940. x0=script_getnum(st,3);
  3941. y0=script_getnum(st,4);
  3942. x1=script_getnum(st,5);
  3943. y1=script_getnum(st,6);
  3944. hp=script_getnum(st,7);
  3945. sp=script_getnum(st,8);
  3946. if( (m=map_mapname2mapid(mapname))< 0)
  3947. return 0;
  3948. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  3949. return 0;
  3950. }
  3951. /*==========================================
  3952. * warpchar [LuzZza]
  3953. * Useful for warp one player from
  3954. * another player npc-session.
  3955. * Using: warpchar "mapname",x,y,Char_ID;
  3956. *------------------------------------------*/
  3957. BUILDIN_FUNC(warpchar)
  3958. {
  3959. int x,y,a;
  3960. const char *str;
  3961. TBL_PC *sd;
  3962. str=script_getstr(st,2);
  3963. x=script_getnum(st,3);
  3964. y=script_getnum(st,4);
  3965. a=script_getnum(st,5);
  3966. sd = map_charid2sd(a);
  3967. if( sd == NULL )
  3968. return 0;
  3969. if(strcmp(str, "Random") == 0)
  3970. pc_randomwarp(sd, CLR_TELEPORT);
  3971. else
  3972. if(strcmp(str, "SavePoint") == 0)
  3973. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  3974. else
  3975. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  3976. return 0;
  3977. }
  3978. /*==========================================
  3979. * Warpparty - [Fredzilla] [Paradox924X]
  3980. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  3981. * If 'from_mapname' is specified, only the party members on that map will be warped
  3982. *------------------------------------------*/
  3983. BUILDIN_FUNC(warpparty)
  3984. {
  3985. TBL_PC *sd;
  3986. TBL_PC *pl_sd;
  3987. struct party_data* p;
  3988. int type;
  3989. int mapindex;
  3990. int i, j;
  3991. const char* str = script_getstr(st,2);
  3992. int x = script_getnum(st,3);
  3993. int y = script_getnum(st,4);
  3994. int p_id = script_getnum(st,5);
  3995. const char* str2 = NULL;
  3996. if ( script_hasdata(st,6) )
  3997. str2 = script_getstr(st,6);
  3998. sd=script_rid2sd(st);
  3999. if( sd == NULL )
  4000. return 0;
  4001. p = party_search(p_id);
  4002. if(!p)
  4003. return 0;
  4004. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4005. return 0;
  4006. type = ( strcmp(str,"Random")==0 ) ? 0
  4007. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4008. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4009. : ( strcmp(str,"Leader")==0 ) ? 3
  4010. : 4;
  4011. for (i = 0; i < MAX_PARTY; i++)
  4012. {
  4013. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4014. continue;
  4015. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4016. continue;
  4017. if( pc_isdead(pl_sd) )
  4018. continue;
  4019. switch( type )
  4020. {
  4021. case 0: // Random
  4022. if(!map[pl_sd->bl.m].flag.nowarp)
  4023. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4024. break;
  4025. case 1: // SavePointAll
  4026. if(!map[pl_sd->bl.m].flag.noreturn)
  4027. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4028. break;
  4029. case 2: // SavePoint
  4030. if(!map[pl_sd->bl.m].flag.noreturn)
  4031. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4032. break;
  4033. case 3: // Leader
  4034. for(j = 0; j < MAX_PARTY && !p->party.member[j].leader; j++);
  4035. if (j == MAX_PARTY || !p->data[j].sd) //Leader not found / not online
  4036. return 0;
  4037. mapindex = p->data[j].sd->mapindex;
  4038. x = p->data[j].sd->bl.x;
  4039. y = p->data[j].sd->bl.y;
  4040. for (j = 0; j < MAX_PARTY; j++)
  4041. {
  4042. pl_sd = p->data[j].sd;
  4043. if (!pl_sd)
  4044. continue;
  4045. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4046. continue;
  4047. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4048. }
  4049. break;
  4050. case 4: // m,x,y
  4051. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4052. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4053. break;
  4054. }
  4055. }
  4056. return 0;
  4057. }
  4058. /*==========================================
  4059. * Warpguild - [Fredzilla]
  4060. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4061. *------------------------------------------*/
  4062. BUILDIN_FUNC(warpguild)
  4063. {
  4064. TBL_PC *sd;
  4065. TBL_PC *pl_sd;
  4066. struct guild* g;
  4067. struct s_mapiterator* iter;
  4068. int type;
  4069. const char* str = script_getstr(st,2);
  4070. int x = script_getnum(st,3);
  4071. int y = script_getnum(st,4);
  4072. int gid = script_getnum(st,5);
  4073. sd=script_rid2sd(st);
  4074. if( sd == NULL )
  4075. return 0;
  4076. g = guild_search(gid);
  4077. if( g == NULL )
  4078. return 0;
  4079. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4080. return 0;
  4081. type = ( strcmp(str,"Random")==0 ) ? 0
  4082. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4083. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4084. : 3;
  4085. iter = mapit_getallusers();
  4086. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4087. {
  4088. if( pl_sd->status.guild_id != gid )
  4089. continue;
  4090. switch( type )
  4091. {
  4092. case 0: // Random
  4093. if(!map[pl_sd->bl.m].flag.nowarp)
  4094. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4095. break;
  4096. case 1: // SavePointAll
  4097. if(!map[pl_sd->bl.m].flag.noreturn)
  4098. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4099. break;
  4100. case 2: // SavePoint
  4101. if(!map[pl_sd->bl.m].flag.noreturn)
  4102. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4103. break;
  4104. case 3: // m,x,y
  4105. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4106. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4107. break;
  4108. }
  4109. }
  4110. mapit_free(iter);
  4111. return 0;
  4112. }
  4113. /*==========================================
  4114. *
  4115. *------------------------------------------*/
  4116. BUILDIN_FUNC(heal)
  4117. {
  4118. TBL_PC *sd;
  4119. int hp,sp;
  4120. sd = script_rid2sd(st);
  4121. if (!sd) return 0;
  4122. hp=script_getnum(st,2);
  4123. sp=script_getnum(st,3);
  4124. status_heal(&sd->bl, hp, sp, 1);
  4125. return 0;
  4126. }
  4127. /*==========================================
  4128. *
  4129. *------------------------------------------*/
  4130. BUILDIN_FUNC(itemheal)
  4131. {
  4132. TBL_PC *sd;
  4133. int hp,sp;
  4134. hp=script_getnum(st,2);
  4135. sp=script_getnum(st,3);
  4136. if(potion_flag==1) {
  4137. potion_hp = hp;
  4138. potion_sp = sp;
  4139. return 0;
  4140. }
  4141. sd = script_rid2sd(st);
  4142. if (!sd) return 0;
  4143. pc_itemheal(sd,sd->itemid,hp,sp);
  4144. return 0;
  4145. }
  4146. /*==========================================
  4147. *
  4148. *------------------------------------------*/
  4149. BUILDIN_FUNC(percentheal)
  4150. {
  4151. int hp,sp;
  4152. TBL_PC* sd;
  4153. hp=script_getnum(st,2);
  4154. sp=script_getnum(st,3);
  4155. if(potion_flag==1) {
  4156. potion_per_hp = hp;
  4157. potion_per_sp = sp;
  4158. return 0;
  4159. }
  4160. sd = script_rid2sd(st);
  4161. if( sd == NULL )
  4162. return 0;
  4163. pc_percentheal(sd,hp,sp);
  4164. return 0;
  4165. }
  4166. /*==========================================
  4167. *
  4168. *------------------------------------------*/
  4169. BUILDIN_FUNC(jobchange)
  4170. {
  4171. int job, upper=-1;
  4172. job=script_getnum(st,2);
  4173. if( script_hasdata(st,3) )
  4174. upper=script_getnum(st,3);
  4175. if (pcdb_checkid(job))
  4176. {
  4177. TBL_PC* sd;
  4178. sd = script_rid2sd(st);
  4179. if( sd == NULL )
  4180. return 0;
  4181. pc_jobchange(sd, job, upper);
  4182. }
  4183. return 0;
  4184. }
  4185. /*==========================================
  4186. *
  4187. *------------------------------------------*/
  4188. BUILDIN_FUNC(jobname)
  4189. {
  4190. int class_=script_getnum(st,2);
  4191. script_pushconststr(st,job_name(class_));
  4192. return 0;
  4193. }
  4194. /// Get input from the player.
  4195. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4196. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4197. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4198. /// shorter than 'min' and 0 otherwise.
  4199. ///
  4200. /// input(<var>{,<min>{,<max>}}) -> <int>
  4201. BUILDIN_FUNC(input)
  4202. {
  4203. TBL_PC* sd;
  4204. struct script_data* data;
  4205. int uid;
  4206. const char* name;
  4207. int min;
  4208. int max;
  4209. sd = script_rid2sd(st);
  4210. if( sd == NULL )
  4211. return 0;
  4212. data = script_getdata(st,2);
  4213. if( !data_isreference(data) ){
  4214. ShowError("script:input: not a variable\n");
  4215. script_reportdata(data);
  4216. st->state = END;
  4217. return 1;
  4218. }
  4219. uid = reference_getuid(data);
  4220. name = reference_getname(data);
  4221. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4222. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4223. if( !sd->state.menu_or_input )
  4224. { // first invocation, display npc input box
  4225. sd->state.menu_or_input = 1;
  4226. st->state = RERUNLINE;
  4227. if( is_string_variable(name) )
  4228. clif_scriptinputstr(sd,st->oid);
  4229. else
  4230. clif_scriptinput(sd,st->oid);
  4231. }
  4232. else
  4233. { // take received text/value and store it in the designated variable
  4234. sd->state.menu_or_input = 0;
  4235. if( is_string_variable(name) )
  4236. {
  4237. int len = (int)strlen(sd->npc_str);
  4238. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4239. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4240. }
  4241. else
  4242. {
  4243. int amount = sd->npc_amount;
  4244. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4245. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4246. }
  4247. st->state = RUN;
  4248. }
  4249. return 0;
  4250. }
  4251. /// Sets the value of a variable.
  4252. /// The value is converted to the type of the variable.
  4253. ///
  4254. /// set(<variable>,<value>) -> <variable>
  4255. BUILDIN_FUNC(set)
  4256. {
  4257. TBL_PC* sd = NULL;
  4258. struct script_data* data;
  4259. int num;
  4260. const char* name;
  4261. char prefix;
  4262. data = script_getdata(st,2);
  4263. if( !data_isreference(data) )
  4264. {
  4265. ShowError("script:set: not a variable\n");
  4266. script_reportdata(script_getdata(st,2));
  4267. st->state = END;
  4268. return 1;
  4269. }
  4270. num = reference_getuid(data);
  4271. name = reference_getname(data);
  4272. prefix = *name;
  4273. if( not_server_variable(prefix) )
  4274. {
  4275. sd = script_rid2sd(st);
  4276. if( sd == NULL )
  4277. {
  4278. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4279. return 0;
  4280. }
  4281. }
  4282. if( is_string_variable(name) )
  4283. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4284. else
  4285. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4286. // return a copy of the variable reference
  4287. script_pushcopy(st,2);
  4288. return 0;
  4289. }
  4290. /////////////////////////////////////////////////////////////////////
  4291. /// Array variables
  4292. ///
  4293. /// Returns the size of the specified array
  4294. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4295. {
  4296. int32 ret = idx;
  4297. if( isstring )
  4298. {
  4299. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4300. {
  4301. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4302. if( str && *str )
  4303. ret = idx + 1;
  4304. script_removetop(st, -1, 0);
  4305. }
  4306. }
  4307. else
  4308. {
  4309. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4310. {
  4311. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4312. if( num )
  4313. ret = idx + 1;
  4314. script_removetop(st, -1, 0);
  4315. }
  4316. }
  4317. return ret;
  4318. }
  4319. /// Sets values of an array, from the starting index.
  4320. /// ex: setarray arr[1],1,2,3;
  4321. ///
  4322. /// setarray <array variable>,<value1>{,<value2>...};
  4323. BUILDIN_FUNC(setarray)
  4324. {
  4325. struct script_data* data;
  4326. const char* name;
  4327. int32 start;
  4328. int32 end;
  4329. int32 id;
  4330. int32 i;
  4331. TBL_PC* sd = NULL;
  4332. data = script_getdata(st, 2);
  4333. if( !data_isreference(data) )
  4334. {
  4335. ShowError("script:setarray: not a variable\n");
  4336. script_reportdata(data);
  4337. st->state = END;
  4338. return 1;// not a variable
  4339. }
  4340. id = reference_getid(data);
  4341. start = reference_getindex(data);
  4342. name = reference_getname(data);
  4343. if( not_array_variable(*name) )
  4344. {
  4345. ShowError("script:setarray: illegal scope\n");
  4346. script_reportdata(data);
  4347. st->state = END;
  4348. return 1;// not supported
  4349. }
  4350. if( not_server_variable(*name) )
  4351. {
  4352. sd = script_rid2sd(st);
  4353. if( sd == NULL )
  4354. return 0;// no player attached
  4355. }
  4356. end = start + script_lastdata(st) - 2;
  4357. if( end >= SCRIPT_MAX_ARRAYSIZE )
  4358. end = SCRIPT_MAX_ARRAYSIZE-1;
  4359. if( is_string_variable(name) )
  4360. {// string array
  4361. for( i = 3; start < end; ++start, ++i )
  4362. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4363. }
  4364. else
  4365. {// int array
  4366. for( i = 3; start < end; ++start, ++i )
  4367. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4368. }
  4369. return 0;
  4370. }
  4371. /// Sets count values of an array, from the starting index.
  4372. /// ex: cleararray arr[0],0,1;
  4373. ///
  4374. /// cleararray <array variable>,<value>,<count>;
  4375. BUILDIN_FUNC(cleararray)
  4376. {
  4377. struct script_data* data;
  4378. const char* name;
  4379. int32 start;
  4380. int32 end;
  4381. int32 id;
  4382. void* v;
  4383. TBL_PC* sd = NULL;
  4384. data = script_getdata(st, 2);
  4385. if( !data_isreference(data) )
  4386. {
  4387. ShowError("script:cleararray: not a variable\n");
  4388. script_reportdata(data);
  4389. st->state = END;
  4390. return 1;// not a variable
  4391. }
  4392. id = reference_getid(data);
  4393. start = reference_getindex(data);
  4394. name = reference_getname(data);
  4395. if( not_array_variable(*name) )
  4396. {
  4397. ShowError("script:cleararray: illegal scope\n");
  4398. script_reportdata(data);
  4399. st->state = END;
  4400. return 1;// not supported
  4401. }
  4402. if( not_server_variable(*name) )
  4403. {
  4404. sd = script_rid2sd(st);
  4405. if( sd == NULL )
  4406. return 0;// no player attached
  4407. }
  4408. if( is_string_variable(name) )
  4409. v = (void*)script_getstr(st, 3);
  4410. else
  4411. v = (void*)script_getnum(st, 3);
  4412. end = start + script_getnum(st, 4);
  4413. if( end >= SCRIPT_MAX_ARRAYSIZE )
  4414. end = SCRIPT_MAX_ARRAYSIZE-1;
  4415. for( ; start <= end; ++start )
  4416. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4417. return 0;
  4418. }
  4419. /// Copies data from one array to another.
  4420. /// ex: copyarray arr[0],arr[2],2;
  4421. ///
  4422. /// copyarray <destination array variable>,<source array variable>,<count>;
  4423. BUILDIN_FUNC(copyarray)
  4424. {
  4425. struct script_data* data1;
  4426. struct script_data* data2;
  4427. const char* name1;
  4428. const char* name2;
  4429. int32 idx1;
  4430. int32 idx2;
  4431. int32 id1;
  4432. int32 id2;
  4433. void* v;
  4434. int32 i;
  4435. int32 count;
  4436. TBL_PC* sd = NULL;
  4437. data1 = script_getdata(st, 2);
  4438. data2 = script_getdata(st, 3);
  4439. if( !data_isreference(data1) || !data_isreference(data2) )
  4440. {
  4441. ShowError("script:copyarray: not a variable\n");
  4442. script_reportdata(data1);
  4443. script_reportdata(data2);
  4444. st->state = END;
  4445. return 1;// not a variable
  4446. }
  4447. id1 = reference_getid(data1);
  4448. id2 = reference_getid(data2);
  4449. idx1 = reference_getindex(data1);
  4450. idx2 = reference_getindex(data2);
  4451. name1 = reference_getname(data1);
  4452. name2 = reference_getname(data2);
  4453. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4454. {
  4455. ShowError("script:copyarray: illegal scope\n");
  4456. script_reportdata(data1);
  4457. script_reportdata(data2);
  4458. st->state = END;
  4459. return 1;// not supported
  4460. }
  4461. if( is_string_variable(name1) != is_string_variable(name2) )
  4462. {
  4463. ShowError("script:copyarray: type mismatch\n");
  4464. script_reportdata(data1);
  4465. script_reportdata(data2);
  4466. st->state = END;
  4467. return 1;// data type mismatch
  4468. }
  4469. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4470. {
  4471. sd = script_rid2sd(st);
  4472. if( sd == NULL )
  4473. return 0;// no player attached
  4474. }
  4475. count = script_getnum(st, 4);
  4476. if( count >= SCRIPT_MAX_ARRAYSIZE - idx1 )
  4477. count = (SCRIPT_MAX_ARRAYSIZE-1) - idx1;
  4478. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4479. return 0;// nothing to copy
  4480. if( id1 == id2 && idx1 > idx2 )
  4481. {// destination might be overlapping the source - copy in reverse order
  4482. for( i = count - 1; i >= 0; --i )
  4483. {
  4484. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4485. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4486. script_removetop(st, -1, 0);
  4487. }
  4488. }
  4489. else
  4490. {// normal copy
  4491. for( i = 0; i < count; ++i )
  4492. {
  4493. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  4494. {
  4495. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4496. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4497. script_removetop(st, -1, 0);
  4498. }
  4499. else// out of range - assume ""/0
  4500. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4501. }
  4502. }
  4503. return 0;
  4504. }
  4505. /// Returns the size of the array.
  4506. /// Assumes that everything before the starting index exists.
  4507. /// ex: getarraysize(arr[3])
  4508. ///
  4509. /// getarraysize(<array variable>) -> <int>
  4510. BUILDIN_FUNC(getarraysize)
  4511. {
  4512. struct script_data* data;
  4513. const char* name;
  4514. data = script_getdata(st, 2);
  4515. if( !data_isreference(data) )
  4516. {
  4517. ShowError("script:getarraysize: not a variable\n");
  4518. script_reportdata(data);
  4519. script_pushnil(st);
  4520. st->state = END;
  4521. return 1;// not a variable
  4522. }
  4523. name = reference_getname(data);
  4524. if( not_array_variable(*name) )
  4525. {
  4526. ShowError("script:getarraysize: illegal scope\n");
  4527. script_reportdata(data);
  4528. script_pushnil(st);
  4529. st->state = END;
  4530. return 1;// not supported
  4531. }
  4532. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4533. return 0;
  4534. }
  4535. /// Deletes count or all the elements in an array, from the starting index.
  4536. /// ex: deletearray arr[4],2;
  4537. ///
  4538. /// deletearray <array variable>;
  4539. /// deletearray <array variable>,<count>;
  4540. BUILDIN_FUNC(deletearray)
  4541. {
  4542. struct script_data* data;
  4543. const char* name;
  4544. int start;
  4545. int end;
  4546. int id;
  4547. TBL_PC *sd = NULL;
  4548. data = script_getdata(st, 2);
  4549. if( !data_isreference(data) )
  4550. {
  4551. ShowError("script:deletearray: not a variable\n");
  4552. script_reportdata(data);
  4553. st->state = END;
  4554. return 1;// not a variable
  4555. }
  4556. id = reference_getid(data);
  4557. start = reference_getindex(data);
  4558. name = reference_getname(data);
  4559. if( not_array_variable(*name) )
  4560. {
  4561. ShowError("script:deletearray: illegal scope\n");
  4562. script_reportdata(data);
  4563. st->state = END;
  4564. return 1;// not supported
  4565. }
  4566. if( not_server_variable(*name) )
  4567. {
  4568. sd = script_rid2sd(st);
  4569. if( sd == NULL )
  4570. return 0;// no player attached
  4571. }
  4572. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4573. if( start >= end )
  4574. return 0;// nothing to free
  4575. if( script_hasdata(st,3) )
  4576. {
  4577. int count = script_getnum(st, 3);
  4578. if( count > end - start )
  4579. count = end - start;
  4580. if( count <= 0 )
  4581. return 0;// nothing to free
  4582. // move rest of the elements backward
  4583. for( ; start + count < end; ++start )
  4584. {
  4585. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4586. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4587. script_removetop(st, -1, 0);
  4588. }
  4589. }
  4590. // clear the rest of the array
  4591. if( is_string_variable(name) )
  4592. {
  4593. for( ; start < end; ++start )
  4594. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4595. }
  4596. else
  4597. {
  4598. for( ; start < end; ++start )
  4599. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4600. }
  4601. return 0;
  4602. }
  4603. /// Returns a reference to the target index of the array variable.
  4604. /// Equivalent to var[index].
  4605. ///
  4606. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4607. BUILDIN_FUNC(getelementofarray)
  4608. {
  4609. struct script_data* data;
  4610. const char* name;
  4611. int32 id;
  4612. int i;
  4613. data = script_getdata(st, 2);
  4614. if( !data_isreference(data) )
  4615. {
  4616. ShowError("script:getelementofarray: not a variable\n");
  4617. script_reportdata(data);
  4618. script_pushnil(st);
  4619. st->state = END;
  4620. return 1;// not a variable
  4621. }
  4622. id = reference_getid(data);
  4623. name = reference_getname(data);
  4624. if( not_array_variable(*name) )
  4625. {
  4626. ShowError("script:getelementofarray: illegal scope\n");
  4627. script_reportdata(data);
  4628. script_pushnil(st);
  4629. st->state = END;
  4630. return 1;// not supported
  4631. }
  4632. i = script_getnum(st, 3);
  4633. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  4634. {
  4635. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4636. script_reportdata(data);
  4637. script_pushnil(st);
  4638. st->state = END;
  4639. return 1;// out of range
  4640. }
  4641. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4642. return 0;
  4643. }
  4644. /////////////////////////////////////////////////////////////////////
  4645. /// ...
  4646. ///
  4647. /*==========================================
  4648. *
  4649. *------------------------------------------*/
  4650. BUILDIN_FUNC(setlook)
  4651. {
  4652. int type,val;
  4653. TBL_PC* sd;
  4654. type=script_getnum(st,2);
  4655. val=script_getnum(st,3);
  4656. sd = script_rid2sd(st);
  4657. if( sd == NULL )
  4658. return 0;
  4659. pc_changelook(sd,type,val);
  4660. return 0;
  4661. }
  4662. BUILDIN_FUNC(changelook)
  4663. { // As setlook but only client side
  4664. int type,val;
  4665. TBL_PC* sd;
  4666. type=script_getnum(st,2);
  4667. val=script_getnum(st,3);
  4668. sd = script_rid2sd(st);
  4669. if( sd == NULL )
  4670. return 0;
  4671. clif_changelook(&sd->bl,type,val);
  4672. return 0;
  4673. }
  4674. /*==========================================
  4675. *
  4676. *------------------------------------------*/
  4677. BUILDIN_FUNC(cutin)
  4678. {
  4679. TBL_PC* sd;
  4680. sd = script_rid2sd(st);
  4681. if( sd == NULL )
  4682. return 0;
  4683. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4684. return 0;
  4685. }
  4686. /*==========================================
  4687. *
  4688. *------------------------------------------*/
  4689. BUILDIN_FUNC(viewpoint)
  4690. {
  4691. int type,x,y,id,color;
  4692. TBL_PC* sd;
  4693. type=script_getnum(st,2);
  4694. x=script_getnum(st,3);
  4695. y=script_getnum(st,4);
  4696. id=script_getnum(st,5);
  4697. color=script_getnum(st,6);
  4698. sd = script_rid2sd(st);
  4699. if( sd == NULL )
  4700. return 0;
  4701. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4702. return 0;
  4703. }
  4704. /*==========================================
  4705. *
  4706. *------------------------------------------*/
  4707. BUILDIN_FUNC(countitem)
  4708. {
  4709. int nameid, i;
  4710. int count = 0;
  4711. struct script_data* data;
  4712. TBL_PC* sd = script_rid2sd(st);
  4713. if (!sd) {
  4714. script_pushint(st,0);
  4715. return 0;
  4716. }
  4717. data = script_getdata(st,2);
  4718. get_val(st,data);
  4719. if( data_isstring(data) ) {
  4720. const char* name = conv_str(st,data);
  4721. struct item_data* item_data;
  4722. if((item_data = itemdb_searchname(name)) != NULL)
  4723. nameid = item_data->nameid;
  4724. else
  4725. nameid = 0;
  4726. } else
  4727. nameid = conv_num(st,data);
  4728. if (nameid < 500) {
  4729. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4730. script_reportsrc(st);
  4731. script_pushint(st,0);
  4732. return 1;
  4733. }
  4734. for(i = 0; i < MAX_INVENTORY; i++)
  4735. if(sd->status.inventory[i].nameid == nameid)
  4736. count += sd->status.inventory[i].amount;
  4737. script_pushint(st,count);
  4738. return 0;
  4739. }
  4740. /*==========================================
  4741. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4742. * returns number of items that meet the conditions
  4743. *------------------------------------------*/
  4744. BUILDIN_FUNC(countitem2)
  4745. {
  4746. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4747. int count = 0;
  4748. int i;
  4749. struct script_data* data;
  4750. TBL_PC* sd = script_rid2sd(st);
  4751. if (!sd) {
  4752. script_pushint(st,0);
  4753. return 0;
  4754. }
  4755. data = script_getdata(st,2);
  4756. get_val(st,data);
  4757. if( data_isstring(data) ) {
  4758. const char* name = conv_str(st,data);
  4759. struct item_data* item_data;
  4760. if((item_data = itemdb_searchname(name)) != NULL)
  4761. nameid = item_data->nameid;
  4762. else
  4763. nameid = 0;
  4764. } else
  4765. nameid = conv_num(st,data);
  4766. iden = script_getnum(st,3);
  4767. ref = script_getnum(st,4);
  4768. attr = script_getnum(st,5);
  4769. c1 = (short)script_getnum(st,6);
  4770. c2 = (short)script_getnum(st,7);
  4771. c3 = (short)script_getnum(st,8);
  4772. c4 = (short)script_getnum(st,9);
  4773. if (nameid < 500) {
  4774. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4775. script_pushint(st,0);
  4776. return 1;
  4777. }
  4778. for(i = 0; i < MAX_INVENTORY; i++)
  4779. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4780. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4781. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4782. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4783. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4784. sd->status.inventory[i].card[3] == c4
  4785. )
  4786. count += sd->status.inventory[i].amount;
  4787. script_pushint(st,count);
  4788. return 0;
  4789. }
  4790. /*==========================================
  4791. * 重量チェック
  4792. *------------------------------------------*/
  4793. BUILDIN_FUNC(checkweight)
  4794. {
  4795. int nameid=0,amount,i;
  4796. unsigned long weight;
  4797. TBL_PC *sd;
  4798. struct script_data *data;
  4799. sd = script_rid2sd(st);
  4800. if( sd == NULL )
  4801. return 0;
  4802. data=script_getdata(st,2);
  4803. get_val(st,data);
  4804. if( data_isstring(data) ){
  4805. const char *name=conv_str(st,data);
  4806. struct item_data *item_data = itemdb_searchname(name);
  4807. if( item_data )
  4808. nameid=item_data->nameid;
  4809. }else
  4810. nameid=conv_num(st,data);
  4811. amount=script_getnum(st,3);
  4812. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4813. script_pushint(st,0);
  4814. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4815. return 1;
  4816. }
  4817. weight = itemdb_weight(nameid)*amount;
  4818. if( amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight )
  4819. script_pushint(st,0);
  4820. else if( itemdb_isstackable(nameid) )
  4821. {
  4822. if( (i = pc_search_inventory(sd,nameid)) >= 0 )
  4823. script_pushint(st,amount + sd->status.inventory[i].amount > MAX_AMOUNT ? 0 : 1);
  4824. else
  4825. script_pushint(st,pc_search_inventory(sd,0) >= 0 ? 1 : 0);
  4826. }
  4827. else
  4828. {
  4829. for( i = 0; i < MAX_INVENTORY && amount; ++i )
  4830. if( sd->status.inventory[i].nameid == 0 )
  4831. amount--;
  4832. script_pushint(st,amount ? 0 : 1);
  4833. }
  4834. return 0;
  4835. }
  4836. /*==========================================
  4837. * getitem <item id>,<amount>{,<character ID>};
  4838. * getitem "<item name>",<amount>{,<character ID>};
  4839. *------------------------------------------*/
  4840. BUILDIN_FUNC(getitem)
  4841. {
  4842. int nameid,amount,get_count,i,flag = 0;
  4843. struct item it;
  4844. TBL_PC *sd;
  4845. struct script_data *data;
  4846. data=script_getdata(st,2);
  4847. get_val(st,data);
  4848. if( data_isstring(data) )
  4849. {// "<item name>"
  4850. const char *name=conv_str(st,data);
  4851. struct item_data *item_data = itemdb_searchname(name);
  4852. if( item_data == NULL ){
  4853. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4854. return 1; //No item created.
  4855. }
  4856. nameid=item_data->nameid;
  4857. } else if( data_isint(data) )
  4858. {// <item id>
  4859. nameid=conv_num(st,data);
  4860. //Violet Box, Blue Box, etc - random item pick
  4861. if( nameid < 0 ) {
  4862. nameid=itemdb_searchrandomid(-nameid);
  4863. flag = 1;
  4864. }
  4865. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4866. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4867. return 1; //No item created.
  4868. }
  4869. } else {
  4870. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4871. return 1;
  4872. }
  4873. // <amount>
  4874. if( (amount=script_getnum(st,3)) <= 0)
  4875. return 0; //return if amount <=0, skip the useles iteration
  4876. memset(&it,0,sizeof(it));
  4877. it.nameid=nameid;
  4878. if(!flag)
  4879. it.identify=1;
  4880. else
  4881. it.identify=itemdb_isidentified(nameid);
  4882. if( script_hasdata(st,4) )
  4883. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4884. else
  4885. sd=script_rid2sd(st); // Attached player
  4886. if( sd == NULL ) // no target
  4887. return 0;
  4888. //Check if it's stackable.
  4889. if (!itemdb_isstackable(nameid))
  4890. get_count = 1;
  4891. else
  4892. get_count = amount;
  4893. for (i = 0; i < amount; i += get_count)
  4894. {
  4895. // if not pet egg
  4896. if (!pet_create_egg(sd, nameid))
  4897. {
  4898. if ((flag = pc_additem(sd, &it, get_count)))
  4899. {
  4900. clif_additem(sd, 0, 0, flag);
  4901. if( pc_candrop(sd,&it) )
  4902. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4903. }
  4904. }
  4905. }
  4906. //Logs items, got from (N)PC scripts [Lupus]
  4907. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4908. log_pick_pc(sd, "N", nameid, amount, NULL);
  4909. return 0;
  4910. }
  4911. /*==========================================
  4912. *
  4913. *------------------------------------------*/
  4914. BUILDIN_FUNC(getitem2)
  4915. {
  4916. int nameid,amount,get_count,i,flag = 0;
  4917. int iden,ref,attr,c1,c2,c3,c4;
  4918. struct item_data *item_data;
  4919. struct item item_tmp;
  4920. TBL_PC *sd;
  4921. struct script_data *data;
  4922. if( script_hasdata(st,11) )
  4923. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4924. else
  4925. sd=script_rid2sd(st); // Attached player
  4926. if( sd == NULL ) // no target
  4927. return 0;
  4928. data=script_getdata(st,2);
  4929. get_val(st,data);
  4930. if( data_isstring(data) ){
  4931. const char *name=conv_str(st,data);
  4932. struct item_data *item_data = itemdb_searchname(name);
  4933. if( item_data )
  4934. nameid=item_data->nameid;
  4935. else
  4936. nameid=UNKNOWN_ITEM_ID;
  4937. }else
  4938. nameid=conv_num(st,data);
  4939. amount=script_getnum(st,3);
  4940. iden=script_getnum(st,4);
  4941. ref=script_getnum(st,5);
  4942. attr=script_getnum(st,6);
  4943. c1=(short)script_getnum(st,7);
  4944. c2=(short)script_getnum(st,8);
  4945. c3=(short)script_getnum(st,9);
  4946. c4=(short)script_getnum(st,10);
  4947. if(nameid<0) { // ランダム
  4948. nameid=itemdb_searchrandomid(-nameid);
  4949. flag = 1;
  4950. }
  4951. if(nameid > 0) {
  4952. memset(&item_tmp,0,sizeof(item_tmp));
  4953. item_data=itemdb_exists(nameid);
  4954. if (item_data == NULL)
  4955. return -1;
  4956. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4957. if(ref > 10) ref = 10;
  4958. }
  4959. else if(item_data->type==IT_PETEGG) {
  4960. iden = 1;
  4961. ref = 0;
  4962. }
  4963. else {
  4964. iden = 1;
  4965. ref = attr = 0;
  4966. }
  4967. item_tmp.nameid=nameid;
  4968. if(!flag)
  4969. item_tmp.identify=iden;
  4970. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4971. item_tmp.identify=0;
  4972. item_tmp.refine=ref;
  4973. item_tmp.attribute=attr;
  4974. item_tmp.card[0]=(short)c1;
  4975. item_tmp.card[1]=(short)c2;
  4976. item_tmp.card[2]=(short)c3;
  4977. item_tmp.card[3]=(short)c4;
  4978. //Check if it's stackable.
  4979. if (!itemdb_isstackable(nameid))
  4980. get_count = 1;
  4981. else
  4982. get_count = amount;
  4983. for (i = 0; i < amount; i += get_count)
  4984. {
  4985. // if not pet egg
  4986. if (!pet_create_egg(sd, nameid))
  4987. {
  4988. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  4989. {
  4990. clif_additem(sd, 0, 0, flag);
  4991. if( pc_candrop(sd,&item_tmp) )
  4992. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4993. }
  4994. }
  4995. }
  4996. //Logs items, got from (N)PC scripts [Lupus]
  4997. if(log_config.enable_logs&0x40)
  4998. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4999. }
  5000. return 0;
  5001. }
  5002. /*==========================================
  5003. * rentitem <item id>,<seconds>
  5004. * rentitem "<item name>",<seconds>
  5005. *------------------------------------------*/
  5006. BUILDIN_FUNC(rentitem)
  5007. {
  5008. struct map_session_data *sd;
  5009. struct script_data *data;
  5010. struct item it;
  5011. int seconds;
  5012. int nameid = 0, flag;
  5013. data = script_getdata(st,2);
  5014. get_val(st,data);
  5015. if( (sd = script_rid2sd(st)) == NULL )
  5016. return 0;
  5017. if( data_isstring(data) )
  5018. {
  5019. const char *name = conv_str(st,data);
  5020. struct item_data *itd = itemdb_searchname(name);
  5021. if( itd == NULL )
  5022. {
  5023. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5024. return 1;
  5025. }
  5026. nameid = itd->nameid;
  5027. }
  5028. else if( data_isint(data) )
  5029. {
  5030. nameid = conv_num(st,data);
  5031. if( nameid <= 0 || !itemdb_exists(nameid) )
  5032. {
  5033. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5034. return 1;
  5035. }
  5036. }
  5037. else
  5038. {
  5039. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5040. return 1;
  5041. }
  5042. seconds = script_getnum(st,3);
  5043. memset(&it, 0, sizeof(it));
  5044. it.nameid = nameid;
  5045. it.identify = 1;
  5046. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5047. if( (flag = pc_additem(sd, &it, 1)) )
  5048. {
  5049. clif_additem(sd, 0, 0, flag);
  5050. return 1;
  5051. }
  5052. clif_rental_time(sd->fd, nameid, seconds);
  5053. pc_inventory_rental_add(sd, seconds);
  5054. if( log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS )
  5055. log_pick_pc(sd, "N", nameid, 1, NULL);
  5056. return 0;
  5057. }
  5058. /*==========================================
  5059. * gets an item with someone's name inscribed [Skotlex]
  5060. * getinscribeditem item_num, character_name
  5061. * Returned Qty is always 1, only works on equip-able
  5062. * equipment
  5063. *------------------------------------------*/
  5064. BUILDIN_FUNC(getnameditem)
  5065. {
  5066. int nameid;
  5067. struct item item_tmp;
  5068. TBL_PC *sd, *tsd;
  5069. struct script_data *data;
  5070. sd = script_rid2sd(st);
  5071. if (sd == NULL)
  5072. { //Player not attached!
  5073. script_pushint(st,0);
  5074. return 0;
  5075. }
  5076. data=script_getdata(st,2);
  5077. get_val(st,data);
  5078. if( data_isstring(data) ){
  5079. const char *name=conv_str(st,data);
  5080. struct item_data *item_data = itemdb_searchname(name);
  5081. if( item_data == NULL)
  5082. { //Failed
  5083. script_pushint(st,0);
  5084. return 0;
  5085. }
  5086. nameid = item_data->nameid;
  5087. }else
  5088. nameid = conv_num(st,data);
  5089. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5090. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5091. script_pushint(st,0);
  5092. return 0;
  5093. }
  5094. data=script_getdata(st,3);
  5095. get_val(st,data);
  5096. if( data_isstring(data) ) //Char Name
  5097. tsd=map_nick2sd(conv_str(st,data));
  5098. else //Char Id was given
  5099. tsd=map_charid2sd(conv_num(st,data));
  5100. if( tsd == NULL )
  5101. { //Failed
  5102. script_pushint(st,0);
  5103. return 0;
  5104. }
  5105. memset(&item_tmp,0,sizeof(item_tmp));
  5106. item_tmp.nameid=nameid;
  5107. item_tmp.amount=1;
  5108. item_tmp.identify=1;
  5109. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5110. item_tmp.card[2]=tsd->status.char_id;
  5111. item_tmp.card[3]=tsd->status.char_id >> 16;
  5112. if(pc_additem(sd,&item_tmp,1)) {
  5113. script_pushint(st,0);
  5114. return 0; //Failed to add item, we will not drop if they don't fit
  5115. }
  5116. //Logs items, got from (N)PC scripts [Lupus]
  5117. if(log_config.enable_logs&0x40)
  5118. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5119. script_pushint(st,1);
  5120. return 0;
  5121. }
  5122. /*==========================================
  5123. * gets a random item ID from an item group [Skotlex]
  5124. * groupranditem group_num
  5125. *------------------------------------------*/
  5126. BUILDIN_FUNC(grouprandomitem)
  5127. {
  5128. int group;
  5129. group = script_getnum(st,2);
  5130. script_pushint(st,itemdb_searchrandomid(group));
  5131. return 0;
  5132. }
  5133. /*==========================================
  5134. *
  5135. *------------------------------------------*/
  5136. BUILDIN_FUNC(makeitem)
  5137. {
  5138. int nameid,amount,flag = 0;
  5139. int x,y,m;
  5140. const char *mapname;
  5141. struct item item_tmp;
  5142. struct script_data *data;
  5143. data=script_getdata(st,2);
  5144. get_val(st,data);
  5145. if( data_isstring(data) ){
  5146. const char *name=conv_str(st,data);
  5147. struct item_data *item_data = itemdb_searchname(name);
  5148. if( item_data )
  5149. nameid=item_data->nameid;
  5150. else
  5151. nameid=UNKNOWN_ITEM_ID;
  5152. }else
  5153. nameid=conv_num(st,data);
  5154. amount=script_getnum(st,3);
  5155. mapname =script_getstr(st,4);
  5156. x =script_getnum(st,5);
  5157. y =script_getnum(st,6);
  5158. if(strcmp(mapname,"this")==0)
  5159. {
  5160. TBL_PC *sd;
  5161. sd = script_rid2sd(st);
  5162. if (!sd) return 0; //Failed...
  5163. m=sd->bl.m;
  5164. } else
  5165. m=map_mapname2mapid(mapname);
  5166. if(nameid<0) { // ランダム
  5167. nameid=itemdb_searchrandomid(-nameid);
  5168. flag = 1;
  5169. }
  5170. if(nameid > 0) {
  5171. memset(&item_tmp,0,sizeof(item_tmp));
  5172. item_tmp.nameid=nameid;
  5173. if(!flag)
  5174. item_tmp.identify=1;
  5175. else
  5176. item_tmp.identify=itemdb_isidentified(nameid);
  5177. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5178. }
  5179. return 0;
  5180. }
  5181. /// Deletes items from the target/attached player.
  5182. /// Prioritizes ordinary items.
  5183. ///
  5184. /// delitem <item id>,<amount>{,<account id>}
  5185. /// delitem "<item name>",<amount>{,<account id>}
  5186. BUILDIN_FUNC(delitem)
  5187. {
  5188. int nameid=0,amount,i,important_item=0;
  5189. TBL_PC *sd;
  5190. struct script_data *data;
  5191. if( script_hasdata(st,4) )
  5192. {
  5193. int account_id = script_getnum(st,4);
  5194. sd = map_id2sd(account_id); // <account id>
  5195. if( sd == NULL )
  5196. {
  5197. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5198. st->state = END;
  5199. return 1;
  5200. }
  5201. }
  5202. else
  5203. {
  5204. sd = script_rid2sd(st);// attached player
  5205. if( sd == NULL )
  5206. return 0;
  5207. }
  5208. data = script_getdata(st,2);
  5209. get_val(st,data);
  5210. if( data_isstring(data) )
  5211. {
  5212. const char* item_name = conv_str(st,data);
  5213. struct item_data* id = itemdb_searchname(item_name);
  5214. if( id == NULL )
  5215. {
  5216. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5217. st->state = END;
  5218. return 1;
  5219. }
  5220. nameid = id->nameid;// "<item name>"
  5221. }
  5222. else
  5223. nameid = conv_num(st,data);// <item id>
  5224. amount=script_getnum(st,3);
  5225. if( amount <= 0 )
  5226. return 0;// nothing to do
  5227. //1st pass
  5228. //here we won't delete items with CARDS, named items but we count them
  5229. for(i=0;i<MAX_INVENTORY;i++){
  5230. //we don't delete wrong item or equipped item
  5231. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5232. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5233. continue;
  5234. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5235. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5236. sd->status.inventory[i].card[0] == CARD0_PET)
  5237. {
  5238. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5239. continue; //pet couldn't be sent for deletion.
  5240. } else
  5241. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5242. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5243. sd->status.inventory[i].card[0] ||
  5244. sd->status.inventory[i].refine) {
  5245. //this is important item, count it (except for pet eggs)
  5246. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5247. important_item++;
  5248. continue;
  5249. }
  5250. if(sd->status.inventory[i].amount>=amount){
  5251. //Logs items, got from (N)PC scripts [Lupus]
  5252. if(log_config.enable_logs&0x40)
  5253. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5254. pc_delitem(sd,i,amount,0,0);
  5255. return 0; //we deleted exact amount of items. now exit
  5256. } else {
  5257. amount-=sd->status.inventory[i].amount;
  5258. //Logs items, got from (N)PC scripts [Lupus]
  5259. if(log_config.enable_logs&0x40) {
  5260. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5261. }
  5262. //Logs
  5263. pc_delitem(sd,i,sd->status.inventory[i].amount,0,0);
  5264. }
  5265. }
  5266. //2nd pass
  5267. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5268. if (important_item>0 && amount>0)
  5269. for(i=0;i<MAX_INVENTORY;i++){
  5270. //we don't delete wrong item
  5271. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5272. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5273. continue;
  5274. if(sd->status.inventory[i].amount>=amount){
  5275. //Logs items, got from (N)PC scripts [Lupus]
  5276. if(log_config.enable_logs&0x40)
  5277. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5278. pc_delitem(sd,i,amount,0,0);
  5279. return 0; //we deleted exact amount of items. now exit
  5280. } else {
  5281. amount-=sd->status.inventory[i].amount;
  5282. //Logs items, got from (N)PC scripts [Lupus]
  5283. if(log_config.enable_logs&0x40)
  5284. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5285. pc_delitem(sd,i,sd->status.inventory[i].amount,0,0);
  5286. }
  5287. }
  5288. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5289. st->state = END;
  5290. clif_scriptclose(sd, st->oid);
  5291. return 1;
  5292. }
  5293. /// Deletes items from the target/attached player.
  5294. ///
  5295. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5296. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5297. BUILDIN_FUNC(delitem2)
  5298. {
  5299. int nameid=0,amount,i=0;
  5300. int iden,ref,attr,c1,c2,c3,c4;
  5301. TBL_PC *sd;
  5302. struct script_data *data;
  5303. if( script_hasdata(st,11) )
  5304. {
  5305. int account_id = script_getnum(st,11);
  5306. sd = map_id2sd(account_id); // <account id>
  5307. if( sd == NULL )
  5308. {
  5309. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5310. st->state = END;
  5311. return 1;
  5312. }
  5313. }
  5314. else
  5315. {
  5316. sd = script_rid2sd(st);// attached player
  5317. if( sd == NULL )
  5318. return 0;
  5319. }
  5320. data = script_getdata(st,2);
  5321. get_val(st,data);
  5322. if( data_isstring(data) )
  5323. {
  5324. const char* item_name = conv_str(st,data);
  5325. struct item_data* id = itemdb_searchname(item_name);
  5326. if( id == NULL )
  5327. {
  5328. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5329. st->state = END;
  5330. return 1;
  5331. }
  5332. nameid = id->nameid;// "<item name>"
  5333. }
  5334. else
  5335. nameid = conv_num(st,data);// <item id>
  5336. amount=script_getnum(st,3);
  5337. iden=script_getnum(st,4);
  5338. ref=script_getnum(st,5);
  5339. attr=script_getnum(st,6);
  5340. c1=(short)script_getnum(st,7);
  5341. c2=(short)script_getnum(st,8);
  5342. c3=(short)script_getnum(st,9);
  5343. c4=(short)script_getnum(st,10);
  5344. if( amount <= 0 )
  5345. return 0;// nothing to do
  5346. for(i=0;i<MAX_INVENTORY;i++){
  5347. //we don't delete wrong item or equipped item
  5348. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5349. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5350. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5351. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5352. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5353. sd->status.inventory[i].card[3]!=c4)
  5354. continue;
  5355. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5356. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5357. {
  5358. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5359. continue; //Failed to send delete the pet.
  5360. }
  5361. if(sd->status.inventory[i].amount>=amount){
  5362. //Logs items, got from (N)PC scripts [Lupus]
  5363. if(log_config.enable_logs&0x40)
  5364. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5365. pc_delitem(sd,i,amount,0,0);
  5366. return 0; //we deleted exact amount of items. now exit
  5367. } else {
  5368. amount-=sd->status.inventory[i].amount;
  5369. //Logs items, got from (N)PC scripts [Lupus]
  5370. if(log_config.enable_logs&0x40)
  5371. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5372. pc_delitem(sd,i,sd->status.inventory[i].amount,0,0);
  5373. }
  5374. }
  5375. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5376. st->state = END;
  5377. clif_scriptclose(sd, st->oid);
  5378. return 1;
  5379. }
  5380. /*==========================================
  5381. * Enables/Disables use of items while in an NPC [Skotlex]
  5382. *------------------------------------------*/
  5383. BUILDIN_FUNC(enableitemuse)
  5384. {
  5385. TBL_PC *sd;
  5386. sd=script_rid2sd(st);
  5387. if (sd)
  5388. sd->npc_item_flag = st->oid;
  5389. return 0;
  5390. }
  5391. BUILDIN_FUNC(disableitemuse)
  5392. {
  5393. TBL_PC *sd;
  5394. sd=script_rid2sd(st);
  5395. if (sd)
  5396. sd->npc_item_flag = 0;
  5397. return 0;
  5398. }
  5399. /*==========================================
  5400. *キャラ関係のパラメータ取得
  5401. *------------------------------------------*/
  5402. BUILDIN_FUNC(readparam)
  5403. {
  5404. int type;
  5405. TBL_PC *sd;
  5406. type=script_getnum(st,2);
  5407. if( script_hasdata(st,3) )
  5408. sd=map_nick2sd(script_getstr(st,3));
  5409. else
  5410. sd=script_rid2sd(st);
  5411. if(sd==NULL){
  5412. script_pushint(st,-1);
  5413. return 0;
  5414. }
  5415. script_pushint(st,pc_readparam(sd,type));
  5416. return 0;
  5417. }
  5418. /*==========================================
  5419. *キャラ関係のID取得
  5420. *------------------------------------------*/
  5421. BUILDIN_FUNC(getcharid)
  5422. {
  5423. int num;
  5424. TBL_PC *sd;
  5425. num = script_getnum(st,2);
  5426. if( script_hasdata(st,3) )
  5427. sd=map_nick2sd(script_getstr(st,3));
  5428. else
  5429. sd=script_rid2sd(st);
  5430. if(sd==NULL){
  5431. script_pushint(st,0); //return 0, according docs
  5432. return 0;
  5433. }
  5434. switch( num ) {
  5435. case 0: script_pushint(st,sd->status.char_id); break;
  5436. case 1: script_pushint(st,sd->status.party_id); break;
  5437. case 2: script_pushint(st,sd->status.guild_id); break;
  5438. case 3: script_pushint(st,sd->status.account_id); break;
  5439. case 4: script_pushint(st,sd->state.bg_id); break;
  5440. default:
  5441. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5442. script_pushint(st,0);
  5443. break;
  5444. }
  5445. return 0;
  5446. }
  5447. /*==========================================
  5448. *指定IDのPT名取得
  5449. *------------------------------------------*/
  5450. BUILDIN_FUNC(getpartyname)
  5451. {
  5452. int party_id;
  5453. struct party_data* p;
  5454. party_id = script_getnum(st,2);
  5455. if( ( p = party_search(party_id) ) != NULL )
  5456. {
  5457. script_pushstrcopy(st,p->party.name);
  5458. }
  5459. else
  5460. {
  5461. script_pushconststr(st,"null");
  5462. }
  5463. return 0;
  5464. }
  5465. /*==========================================
  5466. *指定IDのPT人数とメンバーID取得
  5467. *------------------------------------------*/
  5468. BUILDIN_FUNC(getpartymember)
  5469. {
  5470. struct party_data *p;
  5471. int i,j=0,type=0;
  5472. p=party_search(script_getnum(st,2));
  5473. if( script_hasdata(st,3) )
  5474. type=script_getnum(st,3);
  5475. if(p!=NULL){
  5476. for(i=0;i<MAX_PARTY;i++){
  5477. if(p->party.member[i].account_id){
  5478. switch (type) {
  5479. case 2:
  5480. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5481. break;
  5482. case 1:
  5483. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5484. break;
  5485. default:
  5486. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5487. }
  5488. j++;
  5489. }
  5490. }
  5491. }
  5492. mapreg_setreg(add_str("$@partymembercount"),j);
  5493. return 0;
  5494. }
  5495. /*==========================================
  5496. * Retrieves party leader. if flag is specified,
  5497. * return some of the leader data. Otherwise, return name.
  5498. *------------------------------------------*/
  5499. BUILDIN_FUNC(getpartyleader)
  5500. {
  5501. int party_id, type = 0, i=0;
  5502. struct party_data *p;
  5503. party_id=script_getnum(st,2);
  5504. if( script_hasdata(st,3) )
  5505. type=script_getnum(st,3);
  5506. p=party_search(party_id);
  5507. if (p) //Search leader
  5508. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5509. if (!p || i == MAX_PARTY) { //leader not found
  5510. if (type)
  5511. script_pushint(st,-1);
  5512. else
  5513. script_pushconststr(st,"null");
  5514. return 0;
  5515. }
  5516. switch (type) {
  5517. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5518. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5519. case 3: script_pushint(st,p->party.member[i].class_); break;
  5520. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5521. case 5: script_pushint(st,p->party.member[i].lv); break;
  5522. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5523. }
  5524. return 0;
  5525. }
  5526. /*==========================================
  5527. *指定IDのギルド名取得
  5528. *------------------------------------------*/
  5529. BUILDIN_FUNC(getguildname)
  5530. {
  5531. int guild_id;
  5532. struct guild* g;
  5533. guild_id = script_getnum(st,2);
  5534. if( ( g = guild_search(guild_id) ) != NULL )
  5535. {
  5536. script_pushstrcopy(st,g->name);
  5537. }
  5538. else
  5539. {
  5540. script_pushconststr(st,"null");
  5541. }
  5542. return 0;
  5543. }
  5544. /*==========================================
  5545. *指定IDのGuildMaster名取得
  5546. *------------------------------------------*/
  5547. BUILDIN_FUNC(getguildmaster)
  5548. {
  5549. int guild_id;
  5550. struct guild* g;
  5551. guild_id = script_getnum(st,2);
  5552. if( ( g = guild_search(guild_id) ) != NULL )
  5553. {
  5554. script_pushstrcopy(st,g->member[0].name);
  5555. }
  5556. else
  5557. {
  5558. script_pushconststr(st,"null");
  5559. }
  5560. return 0;
  5561. }
  5562. BUILDIN_FUNC(getguildmasterid)
  5563. {
  5564. int guild_id;
  5565. struct guild* g;
  5566. guild_id = script_getnum(st,2);
  5567. if( ( g = guild_search(guild_id) ) != NULL )
  5568. {
  5569. script_pushint(st,g->member[0].char_id);
  5570. }
  5571. else
  5572. {
  5573. script_pushint(st,0);
  5574. }
  5575. return 0;
  5576. }
  5577. /*==========================================
  5578. * キャラクタの名前
  5579. *------------------------------------------*/
  5580. BUILDIN_FUNC(strcharinfo)
  5581. {
  5582. TBL_PC *sd;
  5583. int num;
  5584. struct guild* g;
  5585. struct party_data* p;
  5586. sd=script_rid2sd(st);
  5587. if (!sd) { //Avoid crashing....
  5588. script_pushconststr(st,"");
  5589. return 0;
  5590. }
  5591. num=script_getnum(st,2);
  5592. switch(num){
  5593. case 0:
  5594. script_pushstrcopy(st,sd->status.name);
  5595. break;
  5596. case 1:
  5597. if( ( p = party_search(sd->status.party_id) ) != NULL )
  5598. {
  5599. script_pushstrcopy(st,p->party.name);
  5600. }
  5601. else
  5602. {
  5603. script_pushconststr(st,"");
  5604. }
  5605. break;
  5606. case 2:
  5607. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  5608. {
  5609. script_pushstrcopy(st,g->name);
  5610. }
  5611. else
  5612. {
  5613. script_pushconststr(st,"");
  5614. }
  5615. break;
  5616. case 3:
  5617. script_pushconststr(st,map[sd->bl.m].name);
  5618. break;
  5619. default:
  5620. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5621. script_pushconststr(st,"");
  5622. break;
  5623. }
  5624. return 0;
  5625. }
  5626. /*==========================================
  5627. * 呼び出し元のNPC情報を取得する
  5628. *------------------------------------------*/
  5629. BUILDIN_FUNC(strnpcinfo)
  5630. {
  5631. TBL_NPC* nd;
  5632. int num;
  5633. char *buf,*name=NULL;
  5634. nd = map_id2nd(st->oid);
  5635. if (!nd) {
  5636. script_pushconststr(st, "");
  5637. return 0;
  5638. }
  5639. num = script_getnum(st,2);
  5640. switch(num){
  5641. case 0: // display name
  5642. name = aStrdup(nd->name);
  5643. break;
  5644. case 1: // visible part of display name
  5645. if((buf = strchr(nd->name,'#')) != NULL)
  5646. {
  5647. name = aStrdup(nd->name);
  5648. name[buf - nd->name] = 0;
  5649. } else // Return the name, there is no '#' present
  5650. name = aStrdup(nd->name);
  5651. break;
  5652. case 2: // # fragment
  5653. if((buf = strchr(nd->name,'#')) != NULL)
  5654. name = aStrdup(buf+1);
  5655. break;
  5656. case 3: // unique name
  5657. name = aStrdup(nd->exname);
  5658. break;
  5659. }
  5660. if(name)
  5661. script_pushstr(st, name);
  5662. else
  5663. script_pushconststr(st, "");
  5664. return 0;
  5665. }
  5666. // aegis->athena slot position conversion table
  5667. 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};
  5668. /*==========================================
  5669. * GetEquipID(Pos); Pos: 1-10
  5670. *------------------------------------------*/
  5671. BUILDIN_FUNC(getequipid)
  5672. {
  5673. int i, num;
  5674. TBL_PC* sd;
  5675. struct item_data* item;
  5676. sd = script_rid2sd(st);
  5677. if( sd == NULL )
  5678. return 0;
  5679. num = script_getnum(st,2) - 1;
  5680. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5681. {
  5682. script_pushint(st,-1);
  5683. return 0;
  5684. }
  5685. // get inventory position of item
  5686. i = pc_checkequip(sd,equip[num]);
  5687. if( i < 0 )
  5688. {
  5689. script_pushint(st,-1);
  5690. return 0;
  5691. }
  5692. item = sd->inventory_data[i];
  5693. if( item != 0 )
  5694. script_pushint(st,item->nameid);
  5695. else
  5696. script_pushint(st,0);
  5697. return 0;
  5698. }
  5699. /*==========================================
  5700. * 装備名文字列(精錬メニュー用)
  5701. *------------------------------------------*/
  5702. BUILDIN_FUNC(getequipname)
  5703. {
  5704. int i, num;
  5705. TBL_PC* sd;
  5706. struct item_data* item;
  5707. sd = script_rid2sd(st);
  5708. if( sd == NULL )
  5709. return 0;
  5710. num = script_getnum(st,2) - 1;
  5711. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5712. {
  5713. script_pushconststr(st,"");
  5714. return 0;
  5715. }
  5716. // get inventory position of item
  5717. i = pc_checkequip(sd,equip[num]);
  5718. if( i < 0 )
  5719. {
  5720. script_pushint(st,-1);
  5721. return 0;
  5722. }
  5723. item = sd->inventory_data[i];
  5724. if( item != 0 )
  5725. script_pushstrcopy(st,item->jname);
  5726. else
  5727. script_pushconststr(st,"");
  5728. return 0;
  5729. }
  5730. /*==========================================
  5731. * getbrokenid [Valaris]
  5732. *------------------------------------------*/
  5733. BUILDIN_FUNC(getbrokenid)
  5734. {
  5735. int i,num,id=0,brokencounter=0;
  5736. TBL_PC *sd;
  5737. sd = script_rid2sd(st);
  5738. if( sd == NULL )
  5739. return 0;
  5740. num=script_getnum(st,2);
  5741. for(i=0; i<MAX_INVENTORY; i++) {
  5742. if(sd->status.inventory[i].attribute){
  5743. brokencounter++;
  5744. if(num==brokencounter){
  5745. id=sd->status.inventory[i].nameid;
  5746. break;
  5747. }
  5748. }
  5749. }
  5750. script_pushint(st,id);
  5751. return 0;
  5752. }
  5753. /*==========================================
  5754. * repair [Valaris]
  5755. *------------------------------------------*/
  5756. BUILDIN_FUNC(repair)
  5757. {
  5758. int i,num;
  5759. int repaircounter=0;
  5760. TBL_PC *sd;
  5761. sd = script_rid2sd(st);
  5762. if( sd == NULL )
  5763. return 0;
  5764. num=script_getnum(st,2);
  5765. for(i=0; i<MAX_INVENTORY; i++) {
  5766. if(sd->status.inventory[i].attribute){
  5767. repaircounter++;
  5768. if(num==repaircounter){
  5769. sd->status.inventory[i].attribute=0;
  5770. clif_equiplist(sd);
  5771. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5772. clif_misceffect(&sd->bl, 3);
  5773. break;
  5774. }
  5775. }
  5776. }
  5777. return 0;
  5778. }
  5779. /*==========================================
  5780. * 装備チェック
  5781. *------------------------------------------*/
  5782. BUILDIN_FUNC(getequipisequiped)
  5783. {
  5784. int i=-1,num;
  5785. TBL_PC *sd;
  5786. num=script_getnum(st,2);
  5787. sd = script_rid2sd(st);
  5788. if( sd == NULL )
  5789. return 0;
  5790. if (num > 0 && num <= ARRAYLENGTH(equip))
  5791. i=pc_checkequip(sd,equip[num-1]);
  5792. if(i >= 0)
  5793. script_pushint(st,1);
  5794. else
  5795. script_pushint(st,0);
  5796. return 0;
  5797. }
  5798. /*==========================================
  5799. * 装備品精錬可能チェック
  5800. *------------------------------------------*/
  5801. BUILDIN_FUNC(getequipisenableref)
  5802. {
  5803. int i=-1,num;
  5804. TBL_PC *sd;
  5805. num=script_getnum(st,2);
  5806. sd = script_rid2sd(st);
  5807. if( sd == NULL )
  5808. return 0;
  5809. if( num > 0 && num <= ARRAYLENGTH(equip) )
  5810. i = pc_checkequip(sd,equip[num-1]);
  5811. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  5812. script_pushint(st,1);
  5813. else
  5814. script_pushint(st,0);
  5815. return 0;
  5816. }
  5817. /*==========================================
  5818. * 装備品鑑定チェック
  5819. *------------------------------------------*/
  5820. BUILDIN_FUNC(getequipisidentify)
  5821. {
  5822. int i=-1,num;
  5823. TBL_PC *sd;
  5824. num=script_getnum(st,2);
  5825. sd = script_rid2sd(st);
  5826. if( sd == NULL )
  5827. return 0;
  5828. if (num > 0 && num <= ARRAYLENGTH(equip))
  5829. i=pc_checkequip(sd,equip[num-1]);
  5830. if(i >= 0)
  5831. script_pushint(st,sd->status.inventory[i].identify);
  5832. else
  5833. script_pushint(st,0);
  5834. return 0;
  5835. }
  5836. /*==========================================
  5837. * 装備品精錬度
  5838. *------------------------------------------*/
  5839. BUILDIN_FUNC(getequiprefinerycnt)
  5840. {
  5841. int i=-1,num;
  5842. TBL_PC *sd;
  5843. num=script_getnum(st,2);
  5844. sd = script_rid2sd(st);
  5845. if( sd == NULL )
  5846. return 0;
  5847. if (num > 0 && num <= ARRAYLENGTH(equip))
  5848. i=pc_checkequip(sd,equip[num-1]);
  5849. if(i >= 0)
  5850. script_pushint(st,sd->status.inventory[i].refine);
  5851. else
  5852. script_pushint(st,0);
  5853. return 0;
  5854. }
  5855. /*==========================================
  5856. * 装備品武器LV
  5857. *------------------------------------------*/
  5858. BUILDIN_FUNC(getequipweaponlv)
  5859. {
  5860. int i=-1,num;
  5861. TBL_PC *sd;
  5862. num=script_getnum(st,2);
  5863. sd = script_rid2sd(st);
  5864. if( sd == NULL )
  5865. return 0;
  5866. if (num > 0 && num <= ARRAYLENGTH(equip))
  5867. i=pc_checkequip(sd,equip[num-1]);
  5868. if(i >= 0 && sd->inventory_data[i])
  5869. script_pushint(st,sd->inventory_data[i]->wlv);
  5870. else
  5871. script_pushint(st,0);
  5872. return 0;
  5873. }
  5874. /*==========================================
  5875. * 装備品精錬成功率
  5876. *------------------------------------------*/
  5877. BUILDIN_FUNC(getequippercentrefinery)
  5878. {
  5879. int i=-1,num;
  5880. TBL_PC *sd;
  5881. num=script_getnum(st,2);
  5882. sd = script_rid2sd(st);
  5883. if( sd == NULL )
  5884. return 0;
  5885. if (num > 0 && num <= ARRAYLENGTH(equip))
  5886. i=pc_checkequip(sd,equip[num-1]);
  5887. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5888. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5889. else
  5890. script_pushint(st,0);
  5891. return 0;
  5892. }
  5893. /*==========================================
  5894. * 精錬成功
  5895. *------------------------------------------*/
  5896. BUILDIN_FUNC(successrefitem)
  5897. {
  5898. int i=-1,num,ep;
  5899. TBL_PC *sd;
  5900. num=script_getnum(st,2);
  5901. sd = script_rid2sd(st);
  5902. if( sd == NULL )
  5903. return 0;
  5904. if (num > 0 && num <= ARRAYLENGTH(equip))
  5905. i=pc_checkequip(sd,equip[num-1]);
  5906. if(i >= 0) {
  5907. ep=sd->status.inventory[i].equip;
  5908. //Logs items, got from (N)PC scripts [Lupus]
  5909. if(log_config.enable_logs&0x40)
  5910. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5911. sd->status.inventory[i].refine++;
  5912. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  5913. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5914. clif_delitem(sd,i,1,3);
  5915. //Logs items, got from (N)PC scripts [Lupus]
  5916. if(log_config.enable_logs&0x40)
  5917. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5918. clif_additem(sd,i,1,0);
  5919. pc_equipitem(sd,i,ep);
  5920. clif_misceffect(&sd->bl,3);
  5921. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5922. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5923. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5924. ){ // Fame point system [DracoRPG]
  5925. switch (sd->inventory_data[i]->wlv){
  5926. case 1:
  5927. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5928. break;
  5929. case 2:
  5930. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5931. break;
  5932. case 3:
  5933. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5934. break;
  5935. }
  5936. }
  5937. }
  5938. return 0;
  5939. }
  5940. /*==========================================
  5941. * 精錬失敗
  5942. *------------------------------------------*/
  5943. BUILDIN_FUNC(failedrefitem)
  5944. {
  5945. int i=-1,num;
  5946. TBL_PC *sd;
  5947. num=script_getnum(st,2);
  5948. sd = script_rid2sd(st);
  5949. if( sd == NULL )
  5950. return 0;
  5951. if (num > 0 && num <= ARRAYLENGTH(equip))
  5952. i=pc_checkequip(sd,equip[num-1]);
  5953. if(i >= 0) {
  5954. //Logs items, got from (N)PC scripts [Lupus]
  5955. if(log_config.enable_logs&0x40)
  5956. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5957. sd->status.inventory[i].refine = 0;
  5958. pc_unequipitem(sd,i,3);
  5959. // 精錬失敗エフェクトのパケット
  5960. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5961. pc_delitem(sd,i,1,0,2);
  5962. // 他の人にも失敗を通知
  5963. clif_misceffect(&sd->bl,2);
  5964. }
  5965. return 0;
  5966. }
  5967. /*==========================================
  5968. *
  5969. *------------------------------------------*/
  5970. BUILDIN_FUNC(statusup)
  5971. {
  5972. int type;
  5973. TBL_PC *sd;
  5974. type=script_getnum(st,2);
  5975. sd = script_rid2sd(st);
  5976. if( sd == NULL )
  5977. return 0;
  5978. pc_statusup(sd,type);
  5979. return 0;
  5980. }
  5981. /*==========================================
  5982. *
  5983. *------------------------------------------*/
  5984. BUILDIN_FUNC(statusup2)
  5985. {
  5986. int type,val;
  5987. TBL_PC *sd;
  5988. type=script_getnum(st,2);
  5989. val=script_getnum(st,3);
  5990. sd = script_rid2sd(st);
  5991. if( sd == NULL )
  5992. return 0;
  5993. pc_statusup2(sd,type,val);
  5994. return 0;
  5995. }
  5996. /// See 'doc/item_bonus.txt'
  5997. ///
  5998. /// bonus <bonus type>,<val1>;
  5999. /// bonus2 <bonus type>,<val1>,<val2>;
  6000. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6001. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6002. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6003. BUILDIN_FUNC(bonus)
  6004. {
  6005. int type;
  6006. int val1;
  6007. int val2 = 0;
  6008. int val3 = 0;
  6009. int val4 = 0;
  6010. int val5 = 0;
  6011. TBL_PC* sd;
  6012. sd = script_rid2sd(st);
  6013. if( sd == NULL )
  6014. return 0; // no player attached
  6015. type = script_getnum(st,2);
  6016. switch( type )
  6017. {
  6018. case SP_AUTOSPELL:
  6019. case SP_AUTOSPELL_WHENHIT:
  6020. case SP_AUTOSPELL_ONSKILL:
  6021. case SP_SKILL_ATK:
  6022. case SP_SKILL_HEAL:
  6023. case SP_SKILL_HEAL2:
  6024. case SP_ADD_SKILL_BLOW:
  6025. case SP_CASTRATE:
  6026. case SP_ADDEFF_ONSKILL:
  6027. // these bonuses support skill names
  6028. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6029. break;
  6030. default:
  6031. val1 = script_getnum(st,3);
  6032. break;
  6033. }
  6034. switch( script_lastdata(st)-2 )
  6035. {
  6036. case 1:
  6037. pc_bonus(sd, type, val1);
  6038. break;
  6039. case 2:
  6040. val2 = script_getnum(st,4);
  6041. pc_bonus2(sd, type, val1, val2);
  6042. break;
  6043. case 3:
  6044. val2 = script_getnum(st,4);
  6045. val3 = script_getnum(st,5);
  6046. pc_bonus3(sd, type, val1, val2, val3);
  6047. break;
  6048. case 4:
  6049. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6050. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6051. else
  6052. val2 = script_getnum(st,4);
  6053. val3 = script_getnum(st,5);
  6054. val4 = script_getnum(st,6);
  6055. pc_bonus4(sd, type, val1, val2, val3, val4);
  6056. break;
  6057. case 5:
  6058. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6059. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6060. else
  6061. val2 = script_getnum(st,4);
  6062. val3 = script_getnum(st,5);
  6063. val4 = script_getnum(st,6);
  6064. val5 = script_getnum(st,7);
  6065. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6066. break;
  6067. default:
  6068. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6069. break;
  6070. }
  6071. return 0;
  6072. }
  6073. BUILDIN_FUNC(autobonus)
  6074. {
  6075. unsigned int dur;
  6076. short rate;
  6077. short atk_type = 0;
  6078. TBL_PC* sd;
  6079. const char *bonus_script, *other_script = NULL;
  6080. sd = script_rid2sd(st);
  6081. if( sd == NULL )
  6082. return 0; // no player attached
  6083. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6084. return 0;
  6085. rate = script_getnum(st,3);
  6086. dur = script_getnum(st,4);
  6087. bonus_script = script_getstr(st,2);
  6088. if( !rate || !dur || !bonus_script )
  6089. return 0;
  6090. if( script_hasdata(st,5) )
  6091. atk_type = script_getnum(st,5);
  6092. if( script_hasdata(st,6) )
  6093. other_script = script_getstr(st,6);
  6094. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6095. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6096. {
  6097. script_add_autobonus(bonus_script);
  6098. if( other_script )
  6099. script_add_autobonus(other_script);
  6100. }
  6101. return 0;
  6102. }
  6103. BUILDIN_FUNC(autobonus2)
  6104. {
  6105. unsigned int dur;
  6106. short rate;
  6107. short atk_type = 0;
  6108. TBL_PC* sd;
  6109. const char *bonus_script, *other_script = NULL;
  6110. sd = script_rid2sd(st);
  6111. if( sd == NULL )
  6112. return 0; // no player attached
  6113. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6114. return 0;
  6115. rate = script_getnum(st,3);
  6116. dur = script_getnum(st,4);
  6117. bonus_script = script_getstr(st,2);
  6118. if( !rate || !dur || !bonus_script )
  6119. return 0;
  6120. if( script_hasdata(st,5) )
  6121. atk_type = script_getnum(st,5);
  6122. if( script_hasdata(st,6) )
  6123. other_script = script_getstr(st,6);
  6124. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6125. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6126. {
  6127. script_add_autobonus(bonus_script);
  6128. if( other_script )
  6129. script_add_autobonus(other_script);
  6130. }
  6131. return 0;
  6132. }
  6133. BUILDIN_FUNC(autobonus3)
  6134. {
  6135. unsigned int dur;
  6136. short rate,atk_type;
  6137. TBL_PC* sd;
  6138. const char *bonus_script, *other_script = NULL;
  6139. sd = script_rid2sd(st);
  6140. if( sd == NULL )
  6141. return 0; // no player attached
  6142. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6143. return 0;
  6144. rate = script_getnum(st,3);
  6145. dur = script_getnum(st,4);
  6146. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6147. bonus_script = script_getstr(st,2);
  6148. if( !rate || !dur || !atk_type || !bonus_script )
  6149. return 0;
  6150. if( script_hasdata(st,6) )
  6151. other_script = script_getstr(st,6);
  6152. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6153. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6154. {
  6155. script_add_autobonus(bonus_script);
  6156. if( other_script )
  6157. script_add_autobonus(other_script);
  6158. }
  6159. return 0;
  6160. }
  6161. /// Changes the level of a player skill.
  6162. /// <flag> defaults to 1
  6163. /// <flag>=0 : set the level of the skill
  6164. /// <flag>=1 : set the temporary level of the skill
  6165. /// <flag>=2 : add to the level of the skill
  6166. ///
  6167. /// skill <skill id>,<level>,<flag>
  6168. /// skill <skill id>,<level>
  6169. /// skill "<skill name>",<level>,<flag>
  6170. /// skill "<skill name>",<level>
  6171. BUILDIN_FUNC(skill)
  6172. {
  6173. int id;
  6174. int level;
  6175. int flag = 1;
  6176. TBL_PC* sd;
  6177. sd = script_rid2sd(st);
  6178. if( sd == NULL )
  6179. return 0;// no player attached, report source
  6180. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6181. level = script_getnum(st,3);
  6182. if( script_hasdata(st,4) )
  6183. flag = script_getnum(st,4);
  6184. pc_skill(sd, id, level, flag);
  6185. return 0;
  6186. }
  6187. /// Changes the level of a player skill.
  6188. /// like skill, but <flag> defaults to 2
  6189. ///
  6190. /// addtoskill <skill id>,<amount>,<flag>
  6191. /// addtoskill <skill id>,<amount>
  6192. /// addtoskill "<skill name>",<amount>,<flag>
  6193. /// addtoskill "<skill name>",<amount>
  6194. ///
  6195. /// @see skill
  6196. BUILDIN_FUNC(addtoskill)
  6197. {
  6198. int id;
  6199. int level;
  6200. int flag = 2;
  6201. TBL_PC* sd;
  6202. sd = script_rid2sd(st);
  6203. if( sd == NULL )
  6204. return 0;// no player attached, report source
  6205. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6206. level = script_getnum(st,3);
  6207. if( script_hasdata(st,4) )
  6208. flag = script_getnum(st,4);
  6209. pc_skill(sd, id, level, flag);
  6210. return 0;
  6211. }
  6212. /// Increases the level of a guild skill.
  6213. ///
  6214. /// guildskill <skill id>,<amount>;
  6215. /// guildskill "<skill name>",<amount>;
  6216. BUILDIN_FUNC(guildskill)
  6217. {
  6218. int id;
  6219. int level;
  6220. TBL_PC* sd;
  6221. int i;
  6222. sd = script_rid2sd(st);
  6223. if( sd == NULL )
  6224. return 0;// no player attached, report source
  6225. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6226. level = script_getnum(st,3);
  6227. for( i=0; i < level; i++ )
  6228. guild_skillup(sd, id);
  6229. return 0;
  6230. }
  6231. /// Returns the level of the player skill.
  6232. ///
  6233. /// getskilllv(<skill id>) -> <level>
  6234. /// getskilllv("<skill name>") -> <level>
  6235. BUILDIN_FUNC(getskilllv)
  6236. {
  6237. int id;
  6238. TBL_PC* sd;
  6239. sd = script_rid2sd(st);
  6240. if( sd == NULL )
  6241. return 0;// no player attached, report source
  6242. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6243. script_pushint(st, pc_checkskill(sd,id));
  6244. return 0;
  6245. }
  6246. /// Returns the level of the guild skill.
  6247. ///
  6248. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6249. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6250. BUILDIN_FUNC(getgdskilllv)
  6251. {
  6252. int guild_id;
  6253. int skill_id;
  6254. struct guild* g;
  6255. guild_id = script_getnum(st,2);
  6256. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6257. g = guild_search(guild_id);
  6258. if( g == NULL )
  6259. script_pushint(st, -1);
  6260. else
  6261. script_pushint(st, guild_checkskill(g,skill_id));
  6262. return 0;
  6263. }
  6264. /// Returns the 'basic_skill_check' setting.
  6265. /// This config determines if the server checks the skill level of NV_BASIC
  6266. /// before allowing the basic actions.
  6267. ///
  6268. /// basicskillcheck() -> <bool>
  6269. BUILDIN_FUNC(basicskillcheck)
  6270. {
  6271. script_pushint(st, battle_config.basic_skill_check);
  6272. return 0;
  6273. }
  6274. /// Returns the GM level of the player.
  6275. ///
  6276. /// getgmlevel() -> <level>
  6277. BUILDIN_FUNC(getgmlevel)
  6278. {
  6279. TBL_PC* sd;
  6280. sd = script_rid2sd(st);
  6281. if( sd == NULL )
  6282. return 0;// no player attached, report source
  6283. script_pushint(st, pc_isGM(sd));
  6284. return 0;
  6285. }
  6286. /// Terminates the execution of this script instance.
  6287. ///
  6288. /// end
  6289. BUILDIN_FUNC(end)
  6290. {
  6291. st->state = END;
  6292. return 0;
  6293. }
  6294. /// Checks if the player has that effect state (option).
  6295. ///
  6296. /// checkoption(<option>) -> <bool>
  6297. BUILDIN_FUNC(checkoption)
  6298. {
  6299. int option;
  6300. TBL_PC* sd;
  6301. sd = script_rid2sd(st);
  6302. if( sd == NULL )
  6303. return 0;// no player attached, report source
  6304. option = script_getnum(st,2);
  6305. if( sd->sc.option&option )
  6306. script_pushint(st, 1);
  6307. else
  6308. script_pushint(st, 0);
  6309. return 0;
  6310. }
  6311. /// Checks if the player is in that body state (opt1).
  6312. ///
  6313. /// checkoption1(<opt1>) -> <bool>
  6314. BUILDIN_FUNC(checkoption1)
  6315. {
  6316. int opt1;
  6317. TBL_PC* sd;
  6318. sd = script_rid2sd(st);
  6319. if( sd == NULL )
  6320. return 0;// no player attached, report source
  6321. opt1 = script_getnum(st,2);
  6322. if( sd->sc.opt1 == opt1 )
  6323. script_pushint(st, 1);
  6324. else
  6325. script_pushint(st, 0);
  6326. return 0;
  6327. }
  6328. /// Checks if the player has that health state (opt2).
  6329. ///
  6330. /// checkoption2(<opt2>) -> <bool>
  6331. BUILDIN_FUNC(checkoption2)
  6332. {
  6333. int opt2;
  6334. TBL_PC* sd;
  6335. sd = script_rid2sd(st);
  6336. if( sd == NULL )
  6337. return 0;// no player attached, report source
  6338. opt2 = script_getnum(st,2);
  6339. if( sd->sc.opt2&opt2 )
  6340. script_pushint(st, 1);
  6341. else
  6342. script_pushint(st, 0);
  6343. return 0;
  6344. }
  6345. /// Changes the effect state (option) of the player.
  6346. /// <flag> defaults to 1
  6347. /// <flag>=0 : removes the option
  6348. /// <flag>=other : adds the option
  6349. ///
  6350. /// setoption <option>,<flag>;
  6351. /// setoption <option>;
  6352. BUILDIN_FUNC(setoption)
  6353. {
  6354. int option;
  6355. int flag = 1;
  6356. TBL_PC* sd;
  6357. sd = script_rid2sd(st);
  6358. if( sd == NULL )
  6359. return 0;// no player attached, report source
  6360. option = script_getnum(st,2);
  6361. if( script_hasdata(st,3) )
  6362. flag = script_getnum(st,3);
  6363. else if( !option ){// Request to remove everything.
  6364. flag = 0;
  6365. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6366. }
  6367. if( flag ){// Add option
  6368. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6369. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6370. pc_setoption(sd, sd->sc.option|option);
  6371. } else// Remove option
  6372. pc_setoption(sd, sd->sc.option&~option);
  6373. return 0;
  6374. }
  6375. /// Returns if the player has a cart.
  6376. ///
  6377. /// checkcart() -> <bool>
  6378. ///
  6379. /// @author Valaris
  6380. BUILDIN_FUNC(checkcart)
  6381. {
  6382. TBL_PC* sd;
  6383. sd = script_rid2sd(st);
  6384. if( sd == NULL )
  6385. return 0;// no player attached, report source
  6386. if( pc_iscarton(sd) )
  6387. script_pushint(st, 1);
  6388. else
  6389. script_pushint(st, 0);
  6390. return 0;
  6391. }
  6392. /// Sets the cart of the player.
  6393. /// <type> defaults to 1
  6394. /// <type>=0 : removes the cart
  6395. /// <type>=1 : Normal cart
  6396. /// <type>=2 : Wooden cart
  6397. /// <type>=3 : Covered cart with flowers and ferns
  6398. /// <type>=4 : Wooden cart with a Panda doll on the back
  6399. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6400. ///
  6401. /// setcart <type>;
  6402. /// setcart;
  6403. BUILDIN_FUNC(setcart)
  6404. {
  6405. int type = 1;
  6406. TBL_PC* sd;
  6407. sd = script_rid2sd(st);
  6408. if( sd == NULL )
  6409. return 0;// no player attached, report source
  6410. if( script_hasdata(st,2) )
  6411. type = script_getnum(st,2);
  6412. pc_setcart(sd, type);
  6413. return 0;
  6414. }
  6415. /// Returns if the player has a falcon.
  6416. ///
  6417. /// checkfalcon() -> <bool>
  6418. ///
  6419. /// @author Valaris
  6420. BUILDIN_FUNC(checkfalcon)
  6421. {
  6422. TBL_PC* sd;
  6423. sd = script_rid2sd(st);
  6424. if( sd == NULL )
  6425. return 0;// no player attached, report source
  6426. if( pc_isfalcon(sd) )
  6427. script_pushint(st, 1);
  6428. else
  6429. script_pushint(st, 0);
  6430. return 0;
  6431. }
  6432. /// Sets if the player has a falcon or not.
  6433. /// <flag> defaults to 1
  6434. ///
  6435. /// setfalcon <flag>;
  6436. /// setfalcon;
  6437. BUILDIN_FUNC(setfalcon)
  6438. {
  6439. int flag = 1;
  6440. TBL_PC* sd;
  6441. sd = script_rid2sd(st);
  6442. if( sd == NULL )
  6443. return 0;// no player attached, report source
  6444. if( script_hasdata(st,2) )
  6445. flag = script_getnum(st,2);
  6446. pc_setfalcon(sd, flag);
  6447. return 0;
  6448. }
  6449. /// Returns if the player is riding.
  6450. ///
  6451. /// checkriding() -> <bool>
  6452. ///
  6453. /// @author Valaris
  6454. BUILDIN_FUNC(checkriding)
  6455. {
  6456. TBL_PC* sd;
  6457. sd = script_rid2sd(st);
  6458. if( sd == NULL )
  6459. return 0;// no player attached, report source
  6460. if( pc_isriding(sd) )
  6461. script_pushint(st, 1);
  6462. else
  6463. script_pushint(st, 0);
  6464. return 0;
  6465. }
  6466. /// Sets if the player is riding.
  6467. /// <flag> defaults to 1
  6468. ///
  6469. /// setriding <flag>;
  6470. /// setriding;
  6471. BUILDIN_FUNC(setriding)
  6472. {
  6473. int flag = 1;
  6474. TBL_PC* sd;
  6475. sd = script_rid2sd(st);
  6476. if( sd == NULL )
  6477. return 0;// no player attached, report source
  6478. if( script_hasdata(st,2) )
  6479. flag = script_getnum(st,2);
  6480. pc_setriding(sd, flag);
  6481. return 0;
  6482. }
  6483. /// Sets the save point of the player.
  6484. ///
  6485. /// save "<map name>",<x>,<y>
  6486. /// savepoint "<map name>",<x>,<y>
  6487. BUILDIN_FUNC(savepoint)
  6488. {
  6489. int x;
  6490. int y;
  6491. short map;
  6492. const char* str;
  6493. TBL_PC* sd;
  6494. sd = script_rid2sd(st);
  6495. if( sd == NULL )
  6496. return 0;// no player attached, report source
  6497. str = script_getstr(st, 2);
  6498. x = script_getnum(st,3);
  6499. y = script_getnum(st,4);
  6500. map = mapindex_name2id(str);
  6501. if( map )
  6502. pc_setsavepoint(sd, map, x, y);
  6503. return 0;
  6504. }
  6505. /*==========================================
  6506. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6507. *------------------------------------------*/
  6508. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6509. {
  6510. int type;
  6511. time_t timer;
  6512. struct tm *t;
  6513. type=script_getnum(st,2);
  6514. switch(type){
  6515. case 2:
  6516. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6517. // from the system clock.)
  6518. script_pushint(st,(int)time(NULL));
  6519. break;
  6520. case 1:
  6521. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6522. time(&timer);
  6523. t=localtime(&timer);
  6524. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6525. break;
  6526. case 0:
  6527. default:
  6528. //type 0:(System Ticks)
  6529. script_pushint(st,gettick());
  6530. break;
  6531. }
  6532. return 0;
  6533. }
  6534. /*==========================================
  6535. * GetTime(Type);
  6536. * 1: Sec 2: Min 3: Hour
  6537. * 4: WeekDay 5: MonthDay 6: Month
  6538. * 7: Year
  6539. *------------------------------------------*/
  6540. BUILDIN_FUNC(gettime) /* Asgard Version */
  6541. {
  6542. int type;
  6543. time_t timer;
  6544. struct tm *t;
  6545. type=script_getnum(st,2);
  6546. time(&timer);
  6547. t=localtime(&timer);
  6548. switch(type){
  6549. case 1://Sec(0~59)
  6550. script_pushint(st,t->tm_sec);
  6551. break;
  6552. case 2://Min(0~59)
  6553. script_pushint(st,t->tm_min);
  6554. break;
  6555. case 3://Hour(0~23)
  6556. script_pushint(st,t->tm_hour);
  6557. break;
  6558. case 4://WeekDay(0~6)
  6559. script_pushint(st,t->tm_wday);
  6560. break;
  6561. case 5://MonthDay(01~31)
  6562. script_pushint(st,t->tm_mday);
  6563. break;
  6564. case 6://Month(01~12)
  6565. script_pushint(st,t->tm_mon+1);
  6566. break;
  6567. case 7://Year(20xx)
  6568. script_pushint(st,t->tm_year+1900);
  6569. break;
  6570. case 8://Year Day(01~366)
  6571. script_pushint(st,t->tm_yday+1);
  6572. break;
  6573. default://(format error)
  6574. script_pushint(st,-1);
  6575. break;
  6576. }
  6577. return 0;
  6578. }
  6579. /*==========================================
  6580. * GetTimeStr("TimeFMT", Length);
  6581. *------------------------------------------*/
  6582. BUILDIN_FUNC(gettimestr)
  6583. {
  6584. char *tmpstr;
  6585. const char *fmtstr;
  6586. int maxlen;
  6587. time_t now = time(NULL);
  6588. fmtstr=script_getstr(st,2);
  6589. maxlen=script_getnum(st,3);
  6590. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6591. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6592. tmpstr[maxlen]='\0';
  6593. script_pushstr(st,tmpstr);
  6594. return 0;
  6595. }
  6596. /*==========================================
  6597. * カプラ倉庫を開く
  6598. *------------------------------------------*/
  6599. BUILDIN_FUNC(openstorage)
  6600. {
  6601. TBL_PC* sd;
  6602. sd = script_rid2sd(st);
  6603. if( sd == NULL )
  6604. return 0;
  6605. storage_storageopen(sd);
  6606. return 0;
  6607. }
  6608. BUILDIN_FUNC(guildopenstorage)
  6609. {
  6610. TBL_PC* sd;
  6611. int ret;
  6612. sd = script_rid2sd(st);
  6613. if( sd == NULL )
  6614. return 0;
  6615. ret = storage_guild_storageopen(sd);
  6616. script_pushint(st,ret);
  6617. return 0;
  6618. }
  6619. /*==========================================
  6620. * アイテムによるスキル発動
  6621. *------------------------------------------*/
  6622. /// itemskill <skill id>,<level>
  6623. /// itemskill "<skill name>",<level>
  6624. BUILDIN_FUNC(itemskill)
  6625. {
  6626. int id;
  6627. int lv;
  6628. TBL_PC* sd;
  6629. sd = script_rid2sd(st);
  6630. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6631. return 0;
  6632. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6633. lv = script_getnum(st,3);
  6634. sd->skillitem=id;
  6635. sd->skillitemlv=lv;
  6636. clif_item_skill(sd,id,lv);
  6637. return 0;
  6638. }
  6639. /*==========================================
  6640. * アイテム作成
  6641. *------------------------------------------*/
  6642. BUILDIN_FUNC(produce)
  6643. {
  6644. int trigger;
  6645. TBL_PC* sd;
  6646. sd = script_rid2sd(st);
  6647. if( sd == NULL )
  6648. return 0;
  6649. trigger=script_getnum(st,2);
  6650. clif_skill_produce_mix_list(sd, trigger);
  6651. return 0;
  6652. }
  6653. /*==========================================
  6654. *
  6655. *------------------------------------------*/
  6656. BUILDIN_FUNC(cooking)
  6657. {
  6658. int trigger;
  6659. TBL_PC* sd;
  6660. sd = script_rid2sd(st);
  6661. if( sd == NULL )
  6662. return 0;
  6663. trigger=script_getnum(st,2);
  6664. clif_cooking_list(sd, trigger);
  6665. return 0;
  6666. }
  6667. /*==========================================
  6668. * NPCでペット作る
  6669. *------------------------------------------*/
  6670. BUILDIN_FUNC(makepet)
  6671. {
  6672. TBL_PC* sd;
  6673. int id,pet_id;
  6674. id=script_getnum(st,2);
  6675. sd = script_rid2sd(st);
  6676. if( sd == NULL )
  6677. return 0;
  6678. pet_id = search_petDB_index(id, PET_CLASS);
  6679. if (pet_id < 0)
  6680. pet_id = search_petDB_index(id, PET_EGG);
  6681. if (pet_id >= 0 && sd) {
  6682. sd->catch_target_class = pet_db[pet_id].class_;
  6683. intif_create_pet(
  6684. sd->status.account_id, sd->status.char_id,
  6685. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6686. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6687. 100, 0, 1, pet_db[pet_id].jname);
  6688. }
  6689. return 0;
  6690. }
  6691. /*==========================================
  6692. * NPCで経験値上げる
  6693. *------------------------------------------*/
  6694. BUILDIN_FUNC(getexp)
  6695. {
  6696. TBL_PC* sd;
  6697. int base=0,job=0;
  6698. double bonus;
  6699. sd = script_rid2sd(st);
  6700. if( sd == NULL )
  6701. return 0;
  6702. base=script_getnum(st,2);
  6703. job =script_getnum(st,3);
  6704. if(base<0 || job<0)
  6705. return 0;
  6706. // bonus for npc-given exp
  6707. bonus = battle_config.quest_exp_rate / 100.;
  6708. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6709. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6710. pc_gainexp(sd, NULL, base, job, true);
  6711. return 0;
  6712. }
  6713. /*==========================================
  6714. * Gain guild exp [Celest]
  6715. *------------------------------------------*/
  6716. BUILDIN_FUNC(guildgetexp)
  6717. {
  6718. TBL_PC* sd;
  6719. int exp;
  6720. sd = script_rid2sd(st);
  6721. if( sd == NULL )
  6722. return 0;
  6723. exp = script_getnum(st,2);
  6724. if(exp < 0)
  6725. return 0;
  6726. if(sd && sd->status.guild_id > 0)
  6727. guild_getexp (sd, exp);
  6728. return 0;
  6729. }
  6730. /*==========================================
  6731. * Changes the guild master of a guild [Skotlex]
  6732. *------------------------------------------*/
  6733. BUILDIN_FUNC(guildchangegm)
  6734. {
  6735. TBL_PC *sd;
  6736. int guild_id;
  6737. const char *name;
  6738. guild_id = script_getnum(st,2);
  6739. name = script_getstr(st,3);
  6740. sd=map_nick2sd(name);
  6741. if (!sd)
  6742. script_pushint(st,0);
  6743. else
  6744. script_pushint(st,guild_gm_change(guild_id, sd));
  6745. return 0;
  6746. }
  6747. /*==========================================
  6748. * モンスター発生
  6749. *------------------------------------------*/
  6750. BUILDIN_FUNC(monster)
  6751. {
  6752. const char* mapn = script_getstr(st,2);
  6753. int x = script_getnum(st,3);
  6754. int y = script_getnum(st,4);
  6755. const char* str = script_getstr(st,5);
  6756. int class_ = script_getnum(st,6);
  6757. int amount = script_getnum(st,7);
  6758. const char* event = "";
  6759. struct map_session_data* sd;
  6760. int m;
  6761. if( script_hasdata(st,8) )
  6762. {
  6763. event = script_getstr(st,8);
  6764. check_event(st, event);
  6765. }
  6766. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6767. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6768. return 1;
  6769. }
  6770. sd = map_id2sd(st->rid);
  6771. if( sd && strcmp(mapn,"this") == 0 )
  6772. m = sd->bl.m;
  6773. else
  6774. {
  6775. m = map_mapname2mapid(mapn);
  6776. if( map[m].flag.src4instance && st->instance_id )
  6777. { // Try to redirect to the instance map, not the src map
  6778. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6779. {
  6780. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6781. return 1;
  6782. }
  6783. }
  6784. }
  6785. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6786. return 0;
  6787. }
  6788. /*==========================================
  6789. * Request List of Monster Drops
  6790. *------------------------------------------*/
  6791. BUILDIN_FUNC(getmobdrops)
  6792. {
  6793. int class_ = script_getnum(st,2);
  6794. int i, j = 0;
  6795. struct mob_db *mob;
  6796. if( !mobdb_checkid(class_) )
  6797. {
  6798. script_pushint(st, 0);
  6799. return 0;
  6800. }
  6801. mob = mob_db(class_);
  6802. for( i = 0; i < MAX_MOB_DROP; i++ )
  6803. {
  6804. if( mob->dropitem[i].nameid < 1 )
  6805. continue;
  6806. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  6807. continue;
  6808. mapreg_setreg(add_str("$@MobDrop_item") + (j<<24), mob->dropitem[i].nameid);
  6809. mapreg_setreg(add_str("$@MobDrop_rate") + (j<<24), mob->dropitem[i].p);
  6810. j++;
  6811. }
  6812. mapreg_setreg(add_str("$@MobDrop_count"), j);
  6813. script_pushint(st, 1);
  6814. return 0;
  6815. }
  6816. /*==========================================
  6817. * モンスター発生
  6818. *------------------------------------------*/
  6819. BUILDIN_FUNC(areamonster)
  6820. {
  6821. const char* mapn = script_getstr(st,2);
  6822. int x0 = script_getnum(st,3);
  6823. int y0 = script_getnum(st,4);
  6824. int x1 = script_getnum(st,5);
  6825. int y1 = script_getnum(st,6);
  6826. const char* str = script_getstr(st,7);
  6827. int class_ = script_getnum(st,8);
  6828. int amount = script_getnum(st,9);
  6829. const char* event = "";
  6830. struct map_session_data* sd;
  6831. int m;
  6832. if( script_hasdata(st,10) )
  6833. {
  6834. event = script_getstr(st,10);
  6835. check_event(st, event);
  6836. }
  6837. sd = map_id2sd(st->rid);
  6838. if( sd && strcmp(mapn,"this") == 0 )
  6839. m = sd->bl.m;
  6840. else
  6841. {
  6842. m = map_mapname2mapid(mapn);
  6843. if( map[m].flag.src4instance && st->instance_id )
  6844. { // Try to redirect to the instance map, not the src map
  6845. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6846. {
  6847. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6848. return 1;
  6849. }
  6850. }
  6851. }
  6852. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6853. return 0;
  6854. }
  6855. /*==========================================
  6856. * モンスター削除
  6857. *------------------------------------------*/
  6858. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  6859. { //same fix but with killmonster instead - stripping events from mobs.
  6860. TBL_MOB* md = (TBL_MOB*)bl;
  6861. char *event=va_arg(ap,char *);
  6862. int allflag=va_arg(ap,int);
  6863. md->state.npc_killmonster = 1;
  6864. if(!allflag){
  6865. if(strcmp(event,md->npc_event)==0)
  6866. status_kill(bl);
  6867. }else{
  6868. if(!md->spawn)
  6869. status_kill(bl);
  6870. }
  6871. md->state.npc_killmonster = 0;
  6872. return 0;
  6873. }
  6874. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6875. {
  6876. TBL_MOB* md = (TBL_MOB*)bl;
  6877. char *event=va_arg(ap,char *);
  6878. int allflag=va_arg(ap,int);
  6879. if(!allflag){
  6880. if(strcmp(event,md->npc_event)==0)
  6881. status_kill(bl);
  6882. }else{
  6883. if(!md->spawn)
  6884. status_kill(bl);
  6885. }
  6886. return 0;
  6887. }
  6888. BUILDIN_FUNC(killmonster)
  6889. {
  6890. const char *mapname,*event;
  6891. int m,allflag=0;
  6892. mapname=script_getstr(st,2);
  6893. event=script_getstr(st,3);
  6894. if(strcmp(event,"All")==0)
  6895. allflag = 1;
  6896. else
  6897. check_event(st, event);
  6898. if( (m=map_mapname2mapid(mapname))<0 )
  6899. return 0;
  6900. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6901. return 0;
  6902. if( script_hasdata(st,4) ) {
  6903. if ( script_getnum(st,4) == 1 ) {
  6904. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6905. return 0;
  6906. }
  6907. }
  6908. map_freeblock_lock();
  6909. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  6910. map_freeblock_unlock();
  6911. return 0;
  6912. }
  6913. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  6914. { //Strips the event from the mob if it's killed the old method.
  6915. struct mob_data *md;
  6916. md = BL_CAST(BL_MOB, bl);
  6917. if (md->npc_event[0])
  6918. md->npc_event[0] = 0;
  6919. status_kill(bl);
  6920. return 0;
  6921. }
  6922. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6923. {
  6924. status_kill(bl);
  6925. return 0;
  6926. }
  6927. BUILDIN_FUNC(killmonsterall)
  6928. {
  6929. const char *mapname;
  6930. int m;
  6931. mapname=script_getstr(st,2);
  6932. if( (m = map_mapname2mapid(mapname))<0 )
  6933. return 0;
  6934. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6935. return 0;
  6936. if( script_hasdata(st,3) ) {
  6937. if ( script_getnum(st,3) == 1 ) {
  6938. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  6939. return 0;
  6940. }
  6941. }
  6942. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  6943. return 0;
  6944. }
  6945. /*==========================================
  6946. * Creates a clone of a player.
  6947. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6948. *------------------------------------------*/
  6949. BUILDIN_FUNC(clone)
  6950. {
  6951. TBL_PC *sd, *msd=NULL;
  6952. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6953. unsigned int duration = 0;
  6954. const char *map,*event="";
  6955. map=script_getstr(st,2);
  6956. x=script_getnum(st,3);
  6957. y=script_getnum(st,4);
  6958. event=script_getstr(st,5);
  6959. char_id=script_getnum(st,6);
  6960. if( script_hasdata(st,7) )
  6961. master_id=script_getnum(st,7);
  6962. if( script_hasdata(st,8) )
  6963. mode=script_getnum(st,8);
  6964. if( script_hasdata(st,9) )
  6965. flag=script_getnum(st,9);
  6966. if( script_hasdata(st,10) )
  6967. duration=script_getnum(st,10);
  6968. check_event(st, event);
  6969. m = map_mapname2mapid(map);
  6970. if (m < 0) return 0;
  6971. sd = map_charid2sd(char_id);
  6972. if (master_id) {
  6973. msd = map_charid2sd(master_id);
  6974. if (msd)
  6975. master_id = msd->bl.id;
  6976. else
  6977. master_id = 0;
  6978. }
  6979. if (sd) //Return ID of newly crafted clone.
  6980. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6981. else //Failed to create clone.
  6982. script_pushint(st,0);
  6983. return 0;
  6984. }
  6985. /*==========================================
  6986. * イベント実行
  6987. *------------------------------------------*/
  6988. BUILDIN_FUNC(doevent)
  6989. {
  6990. const char* event = script_getstr(st,2);
  6991. struct map_session_data* sd;
  6992. if( ( sd = script_rid2sd(st) ) == NULL )
  6993. {
  6994. return 0;
  6995. }
  6996. check_event(st, event);
  6997. npc_event(sd, event, 0);
  6998. return 0;
  6999. }
  7000. /*==========================================
  7001. * NPC主体イベント実行
  7002. *------------------------------------------*/
  7003. BUILDIN_FUNC(donpcevent)
  7004. {
  7005. const char* event = script_getstr(st,2);
  7006. check_event(st, event);
  7007. npc_event_do(event);
  7008. return 0;
  7009. }
  7010. /// for Aegis compatibility
  7011. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7012. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7013. {
  7014. const char* npc = script_getstr(st,2);
  7015. const char* command = script_getstr(st,3);
  7016. char event[51];
  7017. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  7018. check_event(st, event);
  7019. npc_event_do(event);
  7020. return 0;
  7021. }
  7022. /*==========================================
  7023. * イベントタイマー追加
  7024. *------------------------------------------*/
  7025. BUILDIN_FUNC(addtimer)
  7026. {
  7027. int tick = script_getnum(st,2);
  7028. const char* event = script_getstr(st, 3);
  7029. TBL_PC* sd;
  7030. check_event(st, event);
  7031. sd = script_rid2sd(st);
  7032. if( sd == NULL )
  7033. return 0;
  7034. pc_addeventtimer(sd,tick,event);
  7035. return 0;
  7036. }
  7037. /*==========================================
  7038. * イベントタイマー削除
  7039. *------------------------------------------*/
  7040. BUILDIN_FUNC(deltimer)
  7041. {
  7042. const char *event;
  7043. TBL_PC* sd;
  7044. event=script_getstr(st, 2);
  7045. sd = script_rid2sd(st);
  7046. if( sd == NULL )
  7047. return 0;
  7048. check_event(st, event);
  7049. pc_deleventtimer(sd,event);
  7050. return 0;
  7051. }
  7052. /*==========================================
  7053. * イベントタイマーのカウント値追加
  7054. *------------------------------------------*/
  7055. BUILDIN_FUNC(addtimercount)
  7056. {
  7057. const char *event;
  7058. int tick;
  7059. TBL_PC* sd;
  7060. event=script_getstr(st, 2);
  7061. tick=script_getnum(st,3);
  7062. sd = script_rid2sd(st);
  7063. if( sd == NULL )
  7064. return 0;
  7065. check_event(st, event);
  7066. pc_addeventtimercount(sd,event,tick);
  7067. return 0;
  7068. }
  7069. /*==========================================
  7070. * NPCタイマー初期化
  7071. *------------------------------------------*/
  7072. BUILDIN_FUNC(initnpctimer)
  7073. {
  7074. struct npc_data *nd;
  7075. int flag = 0;
  7076. if( script_hasdata(st,3) )
  7077. { //Two arguments: NPC name and attach flag.
  7078. nd = npc_name2id(script_getstr(st, 2));
  7079. flag = script_getnum(st,3);
  7080. }
  7081. else if( script_hasdata(st,2) )
  7082. { //Check if argument is numeric (flag) or string (npc name)
  7083. struct script_data *data;
  7084. data = script_getdata(st,2);
  7085. get_val(st,data);
  7086. if( data_isstring(data) ) //NPC name
  7087. nd = npc_name2id(conv_str(st, data));
  7088. else if( data_isint(data) ) //Flag
  7089. {
  7090. nd = (struct npc_data *)map_id2bl(st->oid);
  7091. flag = conv_num(st,data);
  7092. }
  7093. else
  7094. {
  7095. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7096. return 1;
  7097. }
  7098. }
  7099. else
  7100. nd = (struct npc_data *)map_id2bl(st->oid);
  7101. if( !nd )
  7102. return 0;
  7103. if( flag ) //Attach
  7104. {
  7105. TBL_PC* sd = script_rid2sd(st);
  7106. if( sd == NULL )
  7107. return 0;
  7108. nd->u.scr.rid = sd->bl.id;
  7109. }
  7110. npc_settimerevent_tick(nd,0);
  7111. npc_timerevent_start(nd, st->rid);
  7112. return 0;
  7113. }
  7114. /*==========================================
  7115. * NPCタイマー開始
  7116. *------------------------------------------*/
  7117. BUILDIN_FUNC(startnpctimer)
  7118. {
  7119. struct npc_data *nd;
  7120. int flag = 0;
  7121. if( script_hasdata(st,3) )
  7122. { //Two arguments: NPC name and attach flag.
  7123. nd = npc_name2id(script_getstr(st, 2));
  7124. flag = script_getnum(st,3);
  7125. }
  7126. else if( script_hasdata(st,2) )
  7127. { //Check if argument is numeric (flag) or string (npc name)
  7128. struct script_data *data;
  7129. data = script_getdata(st,2);
  7130. get_val(st,data);
  7131. if( data_isstring(data) ) //NPC name
  7132. nd = npc_name2id(conv_str(st, data));
  7133. else if( data_isint(data) ) //Flag
  7134. {
  7135. nd = (struct npc_data *)map_id2bl(st->oid);
  7136. flag = conv_num(st,data);
  7137. }
  7138. else
  7139. {
  7140. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7141. return 1;
  7142. }
  7143. }
  7144. else
  7145. nd=(struct npc_data *)map_id2bl(st->oid);
  7146. if( !nd )
  7147. return 0;
  7148. if( flag ) //Attach
  7149. {
  7150. TBL_PC* sd = script_rid2sd(st);
  7151. if( sd == NULL )
  7152. return 0;
  7153. nd->u.scr.rid = sd->bl.id;
  7154. }
  7155. npc_timerevent_start(nd, st->rid);
  7156. return 0;
  7157. }
  7158. /*==========================================
  7159. * NPCタイマー停止
  7160. *------------------------------------------*/
  7161. BUILDIN_FUNC(stopnpctimer)
  7162. {
  7163. struct npc_data *nd;
  7164. int flag = 0;
  7165. if( script_hasdata(st,3) )
  7166. { //Two arguments: NPC name and attach flag.
  7167. nd = npc_name2id(script_getstr(st, 2));
  7168. flag = script_getnum(st,3);
  7169. }
  7170. else if( script_hasdata(st,2) )
  7171. { //Check if argument is numeric (flag) or string (npc name)
  7172. struct script_data *data;
  7173. data = script_getdata(st,2);
  7174. get_val(st,data);
  7175. if( data_isstring(data) ) //NPC name
  7176. nd = npc_name2id(conv_str(st, data));
  7177. else if( data_isint(data) ) //Flag
  7178. {
  7179. nd = (struct npc_data *)map_id2bl(st->oid);
  7180. flag = conv_num(st,data);
  7181. }
  7182. else
  7183. {
  7184. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7185. return 1;
  7186. }
  7187. }
  7188. else
  7189. nd=(struct npc_data *)map_id2bl(st->oid);
  7190. if( !nd )
  7191. return 0;
  7192. if( flag ) //Detach
  7193. nd->u.scr.rid = 0;
  7194. npc_timerevent_stop(nd);
  7195. return 0;
  7196. }
  7197. /*==========================================
  7198. * NPCタイマー情報所得
  7199. *------------------------------------------*/
  7200. BUILDIN_FUNC(getnpctimer)
  7201. {
  7202. struct npc_data *nd;
  7203. TBL_PC *sd;
  7204. int type = script_getnum(st,2);
  7205. int val = 0;
  7206. if( script_hasdata(st,3) )
  7207. nd = npc_name2id(script_getstr(st,3));
  7208. else
  7209. nd = (struct npc_data *)map_id2bl(st->oid);
  7210. if( !nd || nd->bl.type != BL_NPC )
  7211. {
  7212. script_pushint(st,0);
  7213. ShowError("getnpctimer: Invalid NPC.\n");
  7214. return 1;
  7215. }
  7216. switch( type )
  7217. {
  7218. case 0: val = npc_gettimerevent_tick(nd); break;
  7219. case 1:
  7220. if( nd->u.scr.rid )
  7221. {
  7222. sd = map_id2sd(nd->u.scr.rid);
  7223. if( !sd )
  7224. {
  7225. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7226. break;
  7227. }
  7228. val = (sd->npc_timer_id != -1);
  7229. }
  7230. else
  7231. val = (nd->u.scr.timerid !=-1);
  7232. break;
  7233. case 2: val = nd->u.scr.timeramount; break;
  7234. }
  7235. script_pushint(st,val);
  7236. return 0;
  7237. }
  7238. /*==========================================
  7239. * NPCタイマー値設定
  7240. *------------------------------------------*/
  7241. BUILDIN_FUNC(setnpctimer)
  7242. {
  7243. int tick;
  7244. struct npc_data *nd;
  7245. tick = script_getnum(st,2);
  7246. if( script_hasdata(st,3) )
  7247. nd = npc_name2id(script_getstr(st,3));
  7248. else
  7249. nd = (struct npc_data *)map_id2bl(st->oid);
  7250. if( !nd || nd->bl.type != BL_NPC )
  7251. {
  7252. script_pushint(st,1);
  7253. ShowError("setnpctimer: Invalid NPC.\n");
  7254. return 1;
  7255. }
  7256. npc_settimerevent_tick(nd,tick);
  7257. script_pushint(st,0);
  7258. return 0;
  7259. }
  7260. /*==========================================
  7261. * attaches the player rid to the timer [Celest]
  7262. *------------------------------------------*/
  7263. BUILDIN_FUNC(attachnpctimer)
  7264. {
  7265. TBL_PC *sd;
  7266. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7267. if( !nd || nd->bl.type != BL_NPC )
  7268. {
  7269. script_pushint(st,1);
  7270. ShowError("setnpctimer: Invalid NPC.\n");
  7271. return 1;
  7272. }
  7273. if( script_hasdata(st,2) )
  7274. sd = map_nick2sd(script_getstr(st,2));
  7275. else
  7276. sd = script_rid2sd(st);
  7277. if( !sd )
  7278. {
  7279. script_pushint(st,1);
  7280. ShowWarning("attachnpctimer: Invalid player.\n");
  7281. return 1;
  7282. }
  7283. nd->u.scr.rid = sd->bl.id;
  7284. script_pushint(st,0);
  7285. return 0;
  7286. }
  7287. /*==========================================
  7288. * detaches a player rid from the timer [Celest]
  7289. *------------------------------------------*/
  7290. BUILDIN_FUNC(detachnpctimer)
  7291. {
  7292. struct npc_data *nd;
  7293. if( script_hasdata(st,2) )
  7294. nd = npc_name2id(script_getstr(st,2));
  7295. else
  7296. nd = (struct npc_data *)map_id2bl(st->oid);
  7297. if( !nd || nd->bl.type != BL_NPC )
  7298. {
  7299. script_pushint(st,1);
  7300. ShowError("detachnpctimer: Invalid NPC.\n");
  7301. return 1;
  7302. }
  7303. nd->u.scr.rid = 0;
  7304. script_pushint(st,0);
  7305. return 0;
  7306. }
  7307. /*==========================================
  7308. * To avoid "player not attached" script errors, this function is provided,
  7309. * it checks if there is a player attached to the current script. [Skotlex]
  7310. * If no, returns 0, if yes, returns the account_id of the attached player.
  7311. *------------------------------------------*/
  7312. BUILDIN_FUNC(playerattached)
  7313. {
  7314. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7315. script_pushint(st,0);
  7316. else
  7317. script_pushint(st,st->rid);
  7318. return 0;
  7319. }
  7320. /*==========================================
  7321. * 天の声アナウンス
  7322. *------------------------------------------*/
  7323. BUILDIN_FUNC(announce)
  7324. {
  7325. const char *mes = script_getstr(st,2);
  7326. int flag = script_getnum(st,3);
  7327. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7328. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7329. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7330. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7331. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7332. if (flag&0x0f) // Broadcast source or broadcast region defined
  7333. {
  7334. send_target target;
  7335. 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
  7336. if (bl == NULL)
  7337. return 0;
  7338. flag &= 0x07;
  7339. target = (flag == 1) ? ALL_SAMEMAP :
  7340. (flag == 2) ? AREA :
  7341. (flag == 3) ? SELF :
  7342. ALL_CLIENT;
  7343. if (fontColor)
  7344. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7345. else
  7346. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7347. }
  7348. else
  7349. {
  7350. if (fontColor)
  7351. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7352. else
  7353. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7354. }
  7355. return 0;
  7356. }
  7357. /*==========================================
  7358. * 天の声アナウンス(特定マップ)
  7359. *------------------------------------------*/
  7360. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7361. {
  7362. char *mes = va_arg(ap, char *);
  7363. int len = va_arg(ap, int);
  7364. int type = va_arg(ap, int);
  7365. char *fontColor = va_arg(ap, char *);
  7366. short fontType = (short)va_arg(ap, int);
  7367. short fontSize = (short)va_arg(ap, int);
  7368. short fontAlign = (short)va_arg(ap, int);
  7369. short fontY = (short)va_arg(ap, int);
  7370. if (fontColor)
  7371. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7372. else
  7373. clif_broadcast(bl, mes, len, type, SELF);
  7374. return 0;
  7375. }
  7376. BUILDIN_FUNC(mapannounce)
  7377. {
  7378. const char *mapname = script_getstr(st,2);
  7379. const char *mes = script_getstr(st,3);
  7380. int flag = script_getnum(st,4);
  7381. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7382. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7383. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7384. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7385. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7386. int m;
  7387. if ((m = map_mapname2mapid(mapname)) < 0)
  7388. return 0;
  7389. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7390. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7391. return 0;
  7392. }
  7393. /*==========================================
  7394. * 天の声アナウンス(特定エリア)
  7395. *------------------------------------------*/
  7396. BUILDIN_FUNC(areaannounce)
  7397. {
  7398. const char *mapname = script_getstr(st,2);
  7399. int x0 = script_getnum(st,3);
  7400. int y0 = script_getnum(st,4);
  7401. int x1 = script_getnum(st,5);
  7402. int y1 = script_getnum(st,6);
  7403. const char *mes = script_getstr(st,7);
  7404. int flag = script_getnum(st,8);
  7405. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7406. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7407. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7408. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7409. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7410. int m;
  7411. if ((m = map_mapname2mapid(mapname)) < 0)
  7412. return 0;
  7413. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7414. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7415. return 0;
  7416. }
  7417. /*==========================================
  7418. * ユーザー数所得
  7419. *------------------------------------------*/
  7420. BUILDIN_FUNC(getusers)
  7421. {
  7422. int flag, val = 0;
  7423. struct map_session_data* sd;
  7424. struct block_list* bl = NULL;
  7425. flag = script_getnum(st,2);
  7426. switch(flag&0x07)
  7427. {
  7428. case 0:
  7429. if(flag&0x8)
  7430. {// npc
  7431. bl = map_id2bl(st->oid);
  7432. }
  7433. else if((sd = script_rid2sd(st))!=NULL)
  7434. {// pc
  7435. bl = &sd->bl;
  7436. }
  7437. if(bl)
  7438. {
  7439. val = map[bl->m].users;
  7440. }
  7441. break;
  7442. case 1:
  7443. val = map_getusers();
  7444. break;
  7445. default:
  7446. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  7447. script_pushint(st,0);
  7448. return 1;
  7449. }
  7450. script_pushint(st,val);
  7451. return 0;
  7452. }
  7453. /*==========================================
  7454. * Works like @WHO - displays all online users names in window
  7455. *------------------------------------------*/
  7456. BUILDIN_FUNC(getusersname)
  7457. {
  7458. TBL_PC *sd, *pl_sd;
  7459. int disp_num=1;
  7460. struct s_mapiterator* iter;
  7461. sd = script_rid2sd(st);
  7462. if (!sd) return 0;
  7463. iter = mapit_getallusers();
  7464. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7465. {
  7466. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7467. continue; // skip hidden GMs
  7468. if((disp_num++)%10==0)
  7469. clif_scriptnext(sd,st->oid);
  7470. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7471. }
  7472. mapit_free(iter);
  7473. return 0;
  7474. }
  7475. /*==========================================
  7476. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7477. *------------------------------------------*/
  7478. BUILDIN_FUNC(getmapguildusers)
  7479. {
  7480. const char *str;
  7481. int m, gid;
  7482. int i=0,c=0;
  7483. struct guild *g = NULL;
  7484. str=script_getstr(st,2);
  7485. gid=script_getnum(st,3);
  7486. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7487. script_pushint(st,-1);
  7488. return 0;
  7489. }
  7490. g = guild_search(gid);
  7491. if (g){
  7492. for(i = 0; i < g->max_member; i++)
  7493. {
  7494. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7495. c++;
  7496. }
  7497. }
  7498. script_pushint(st,c);
  7499. return 0;
  7500. }
  7501. /*==========================================
  7502. * マップ指定ユーザー数所得
  7503. *------------------------------------------*/
  7504. BUILDIN_FUNC(getmapusers)
  7505. {
  7506. const char *str;
  7507. int m;
  7508. str=script_getstr(st,2);
  7509. if( (m=map_mapname2mapid(str))< 0){
  7510. script_pushint(st,-1);
  7511. return 0;
  7512. }
  7513. script_pushint(st,map[m].users);
  7514. return 0;
  7515. }
  7516. /*==========================================
  7517. * エリア指定ユーザー数所得
  7518. *------------------------------------------*/
  7519. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7520. {
  7521. int *users=va_arg(ap,int *);
  7522. (*users)++;
  7523. return 0;
  7524. }
  7525. BUILDIN_FUNC(getareausers)
  7526. {
  7527. const char *str;
  7528. int m,x0,y0,x1,y1,users=0;
  7529. str=script_getstr(st,2);
  7530. x0=script_getnum(st,3);
  7531. y0=script_getnum(st,4);
  7532. x1=script_getnum(st,5);
  7533. y1=script_getnum(st,6);
  7534. if( (m=map_mapname2mapid(str))< 0){
  7535. script_pushint(st,-1);
  7536. return 0;
  7537. }
  7538. map_foreachinarea(buildin_getareausers_sub,
  7539. m,x0,y0,x1,y1,BL_PC,&users);
  7540. script_pushint(st,users);
  7541. return 0;
  7542. }
  7543. /*==========================================
  7544. * エリア指定ドロップアイテム数所得
  7545. *------------------------------------------*/
  7546. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7547. {
  7548. int item=va_arg(ap,int);
  7549. int *amount=va_arg(ap,int *);
  7550. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7551. if(drop->item_data.nameid==item)
  7552. (*amount)+=drop->item_data.amount;
  7553. return 0;
  7554. }
  7555. BUILDIN_FUNC(getareadropitem)
  7556. {
  7557. const char *str;
  7558. int m,x0,y0,x1,y1,item,amount=0;
  7559. struct script_data *data;
  7560. str=script_getstr(st,2);
  7561. x0=script_getnum(st,3);
  7562. y0=script_getnum(st,4);
  7563. x1=script_getnum(st,5);
  7564. y1=script_getnum(st,6);
  7565. data=script_getdata(st,7);
  7566. get_val(st,data);
  7567. if( data_isstring(data) ){
  7568. const char *name=conv_str(st,data);
  7569. struct item_data *item_data = itemdb_searchname(name);
  7570. item=UNKNOWN_ITEM_ID;
  7571. if( item_data )
  7572. item=item_data->nameid;
  7573. }else
  7574. item=conv_num(st,data);
  7575. if( (m=map_mapname2mapid(str))< 0){
  7576. script_pushint(st,-1);
  7577. return 0;
  7578. }
  7579. map_foreachinarea(buildin_getareadropitem_sub,
  7580. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7581. script_pushint(st,amount);
  7582. return 0;
  7583. }
  7584. /*==========================================
  7585. * NPCの有効化
  7586. *------------------------------------------*/
  7587. BUILDIN_FUNC(enablenpc)
  7588. {
  7589. const char *str;
  7590. str=script_getstr(st,2);
  7591. npc_enable(str,1);
  7592. return 0;
  7593. }
  7594. /*==========================================
  7595. * NPCの無効化
  7596. *------------------------------------------*/
  7597. BUILDIN_FUNC(disablenpc)
  7598. {
  7599. const char *str;
  7600. str=script_getstr(st,2);
  7601. npc_enable(str,0);
  7602. return 0;
  7603. }
  7604. /*==========================================
  7605. * 隠れているNPCの表示
  7606. *------------------------------------------*/
  7607. BUILDIN_FUNC(hideoffnpc)
  7608. {
  7609. const char *str;
  7610. str=script_getstr(st,2);
  7611. npc_enable(str,2);
  7612. return 0;
  7613. }
  7614. /*==========================================
  7615. * NPCをハイディング
  7616. *------------------------------------------*/
  7617. BUILDIN_FUNC(hideonnpc)
  7618. {
  7619. const char *str;
  7620. str=script_getstr(st,2);
  7621. npc_enable(str,4);
  7622. return 0;
  7623. }
  7624. /// Starts a status effect on the target unit or on the attached player.
  7625. ///
  7626. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7627. BUILDIN_FUNC(sc_start)
  7628. {
  7629. struct block_list* bl;
  7630. enum sc_type type;
  7631. int tick;
  7632. int val1;
  7633. int val4 = 0;
  7634. type = (sc_type)script_getnum(st,2);
  7635. tick = script_getnum(st,3);
  7636. val1 = script_getnum(st,4);
  7637. if( script_hasdata(st,5) )
  7638. bl = map_id2bl(script_getnum(st,5));
  7639. else
  7640. bl = map_id2bl(st->rid);
  7641. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7642. {// When there isn't a duration specified, try to get it from the skill_db
  7643. tick = skill_get_time(status_sc2skill(type), val1);
  7644. }
  7645. if( potion_flag == 1 && potion_target )
  7646. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7647. bl = map_id2bl(potion_target);
  7648. tick /= 2;// Thrown potions only last half.
  7649. val4 = 1;// Mark that this was a thrown sc_effect
  7650. }
  7651. if( bl )
  7652. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7653. return 0;
  7654. }
  7655. /// Starts a status effect on the target unit or on the attached player.
  7656. ///
  7657. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7658. BUILDIN_FUNC(sc_start2)
  7659. {
  7660. struct block_list* bl;
  7661. enum sc_type type;
  7662. int tick;
  7663. int val1;
  7664. int val4 = 0;
  7665. int rate;
  7666. type = (sc_type)script_getnum(st,2);
  7667. tick = script_getnum(st,3);
  7668. val1 = script_getnum(st,4);
  7669. rate = script_getnum(st,5);
  7670. if( script_hasdata(st,6) )
  7671. bl = map_id2bl(script_getnum(st,6));
  7672. else
  7673. bl = map_id2bl(st->rid);
  7674. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7675. {// When there isn't a duration specified, try to get it from the skill_db
  7676. tick = skill_get_time(status_sc2skill(type), val1);
  7677. }
  7678. if( potion_flag == 1 && potion_target )
  7679. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7680. bl = map_id2bl(potion_target);
  7681. tick /= 2;// Thrown potions only last half.
  7682. val4 = 1;// Mark that this was a thrown sc_effect
  7683. }
  7684. if( bl )
  7685. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7686. return 0;
  7687. }
  7688. /// Starts a status effect on the target unit or on the attached player.
  7689. ///
  7690. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7691. BUILDIN_FUNC(sc_start4)
  7692. {
  7693. struct block_list* bl;
  7694. enum sc_type type;
  7695. int tick;
  7696. int val1;
  7697. int val2;
  7698. int val3;
  7699. int val4;
  7700. type = (sc_type)script_getnum(st,2);
  7701. tick = script_getnum(st,3);
  7702. val1 = script_getnum(st,4);
  7703. val2 = script_getnum(st,5);
  7704. val3 = script_getnum(st,6);
  7705. val4 = script_getnum(st,7);
  7706. if( script_hasdata(st,8) )
  7707. bl = map_id2bl(script_getnum(st,8));
  7708. else
  7709. bl = map_id2bl(st->rid);
  7710. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7711. {// When there isn't a duration specified, try to get it from the skill_db
  7712. tick = skill_get_time(status_sc2skill(type), val1);
  7713. }
  7714. if( potion_flag == 1 && potion_target )
  7715. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7716. bl = map_id2bl(potion_target);
  7717. tick /= 2;// Thrown potions only last half.
  7718. }
  7719. if( bl )
  7720. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7721. return 0;
  7722. }
  7723. /// Ends one or all status effects on the target unit or on the attached player.
  7724. ///
  7725. /// sc_end <effect_id>{,<unit_id>};
  7726. BUILDIN_FUNC(sc_end)
  7727. {
  7728. struct block_list* bl;
  7729. int type;
  7730. type = script_getnum(st,2);
  7731. if( script_hasdata(st,3) )
  7732. bl = map_id2bl(script_getnum(st,3));
  7733. else
  7734. bl = map_id2bl(st->rid);
  7735. if( potion_flag==1 && potion_target )
  7736. {//##TODO how does this work [FlavioJS]
  7737. bl = map_id2bl(potion_target);
  7738. }
  7739. if( !bl ) return 0;
  7740. if( type >= 0 && type < SC_MAX )
  7741. {
  7742. struct status_change *sc = status_get_sc(bl);
  7743. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7744. if (!sce) return 0;
  7745. //This should help status_change_end force disabling the SC in case it has no limit.
  7746. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7747. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7748. } else
  7749. status_change_clear(bl, 2);// remove all effects
  7750. return 0;
  7751. }
  7752. /*==========================================
  7753. * 状態異常耐性を計算した確率を返す
  7754. *------------------------------------------*/
  7755. BUILDIN_FUNC(getscrate)
  7756. {
  7757. struct block_list *bl;
  7758. int type,rate;
  7759. type=script_getnum(st,2);
  7760. rate=script_getnum(st,3);
  7761. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7762. bl = map_id2bl(script_getnum(st,4));
  7763. else
  7764. bl = map_id2bl(st->rid);
  7765. if (bl)
  7766. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7767. script_pushint(st,rate);
  7768. return 0;
  7769. }
  7770. /*==========================================
  7771. *
  7772. *------------------------------------------*/
  7773. BUILDIN_FUNC(debugmes)
  7774. {
  7775. const char *str;
  7776. str=script_getstr(st,2);
  7777. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7778. return 0;
  7779. }
  7780. /*==========================================
  7781. *捕獲アイテム使用
  7782. *------------------------------------------*/
  7783. BUILDIN_FUNC(catchpet)
  7784. {
  7785. int pet_id;
  7786. TBL_PC *sd;
  7787. pet_id= script_getnum(st,2);
  7788. sd=script_rid2sd(st);
  7789. if( sd == NULL )
  7790. return 0;
  7791. pet_catch_process1(sd,pet_id);
  7792. return 0;
  7793. }
  7794. /*==========================================
  7795. * [orn]
  7796. *------------------------------------------*/
  7797. BUILDIN_FUNC(homunculus_evolution)
  7798. {
  7799. TBL_PC *sd;
  7800. sd=script_rid2sd(st);
  7801. if( sd == NULL )
  7802. return 0;
  7803. if(merc_is_hom_active(sd->hd))
  7804. {
  7805. if (sd->hd->homunculus.intimacy > 91000)
  7806. merc_hom_evolution(sd->hd);
  7807. else
  7808. clif_emotion(&sd->hd->bl, E_SWT);
  7809. }
  7810. return 0;
  7811. }
  7812. // [Zephyrus]
  7813. BUILDIN_FUNC(homunculus_shuffle)
  7814. {
  7815. TBL_PC *sd;
  7816. sd=script_rid2sd(st);
  7817. if( sd == NULL )
  7818. return 0;
  7819. if(merc_is_hom_active(sd->hd))
  7820. merc_hom_shuffle(sd->hd);
  7821. return 0;
  7822. }
  7823. //These two functions bring the eA MAPID_* class functionality to scripts.
  7824. BUILDIN_FUNC(eaclass)
  7825. {
  7826. int class_;
  7827. if( script_hasdata(st,2) )
  7828. class_ = script_getnum(st,2);
  7829. else {
  7830. TBL_PC *sd;
  7831. sd=script_rid2sd(st);
  7832. if (!sd) {
  7833. script_pushint(st,-1);
  7834. return 0;
  7835. }
  7836. class_ = sd->status.class_;
  7837. }
  7838. script_pushint(st,pc_jobid2mapid(class_));
  7839. return 0;
  7840. }
  7841. BUILDIN_FUNC(roclass)
  7842. {
  7843. int class_ =script_getnum(st,2);
  7844. int sex;
  7845. if( script_hasdata(st,3) )
  7846. sex = script_getnum(st,3);
  7847. else {
  7848. TBL_PC *sd;
  7849. if (st->rid && (sd=script_rid2sd(st)))
  7850. sex = sd->status.sex;
  7851. else
  7852. sex = 1; //Just use male when not found.
  7853. }
  7854. script_pushint(st,pc_mapid2jobid(class_, sex));
  7855. return 0;
  7856. }
  7857. /*==========================================
  7858. *携帯卵孵化機使用
  7859. *------------------------------------------*/
  7860. BUILDIN_FUNC(birthpet)
  7861. {
  7862. TBL_PC *sd;
  7863. sd=script_rid2sd(st);
  7864. if( sd == NULL )
  7865. return 0;
  7866. clif_sendegg(sd);
  7867. return 0;
  7868. }
  7869. /*==========================================
  7870. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7871. *------------------------------------------*/
  7872. BUILDIN_FUNC(resetlvl)
  7873. {
  7874. TBL_PC *sd;
  7875. int type=script_getnum(st,2);
  7876. sd=script_rid2sd(st);
  7877. if( sd == NULL )
  7878. return 0;
  7879. pc_resetlvl(sd,type);
  7880. return 0;
  7881. }
  7882. /*==========================================
  7883. * ステータスリセット
  7884. *------------------------------------------*/
  7885. BUILDIN_FUNC(resetstatus)
  7886. {
  7887. TBL_PC *sd;
  7888. sd=script_rid2sd(st);
  7889. pc_resetstate(sd);
  7890. return 0;
  7891. }
  7892. /*==========================================
  7893. * script command resetskill
  7894. *------------------------------------------*/
  7895. BUILDIN_FUNC(resetskill)
  7896. {
  7897. TBL_PC *sd;
  7898. sd=script_rid2sd(st);
  7899. pc_resetskill(sd,1);
  7900. return 0;
  7901. }
  7902. /*==========================================
  7903. * Counts total amount of skill points.
  7904. *------------------------------------------*/
  7905. BUILDIN_FUNC(skillpointcount)
  7906. {
  7907. TBL_PC *sd;
  7908. sd=script_rid2sd(st);
  7909. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7910. return 0;
  7911. }
  7912. /*==========================================
  7913. *
  7914. *------------------------------------------*/
  7915. BUILDIN_FUNC(changebase)
  7916. {
  7917. TBL_PC *sd=NULL;
  7918. int vclass;
  7919. if( script_hasdata(st,3) )
  7920. sd=map_id2sd(script_getnum(st,3));
  7921. else
  7922. sd=script_rid2sd(st);
  7923. if(sd == NULL)
  7924. return 0;
  7925. vclass = script_getnum(st,2);
  7926. if(vclass == JOB_WEDDING)
  7927. {
  7928. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7929. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7930. )
  7931. return 0;
  7932. }
  7933. if(!sd->disguise && vclass != sd->vd.class_) {
  7934. status_set_viewdata(&sd->bl, vclass);
  7935. //Updated client view. Base, Weapon and Cloth Colors.
  7936. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7937. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7938. if (sd->vd.cloth_color)
  7939. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7940. clif_skillinfoblock(sd);
  7941. }
  7942. return 0;
  7943. }
  7944. /*==========================================
  7945. * 性別変換
  7946. *------------------------------------------*/
  7947. BUILDIN_FUNC(changesex)
  7948. {
  7949. TBL_PC *sd = NULL;
  7950. sd = script_rid2sd(st);
  7951. chrif_changesex(sd);
  7952. return 0;
  7953. }
  7954. /*==========================================
  7955. * Works like 'announce' but outputs in the common chat window
  7956. *------------------------------------------*/
  7957. BUILDIN_FUNC(globalmes)
  7958. {
  7959. struct block_list *bl = map_id2bl(st->oid);
  7960. struct npc_data *nd = (struct npc_data *)bl;
  7961. const char *name=NULL,*mes;
  7962. mes=script_getstr(st,2); // メッセージの取得
  7963. if(mes==NULL) return 0;
  7964. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7965. name=script_getstr(st,3);
  7966. } else {
  7967. name=nd->name;
  7968. }
  7969. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7970. return 0;
  7971. }
  7972. /////////////////////////////////////////////////////////////////////
  7973. // NPC waiting room (chat room)
  7974. //
  7975. /// Creates a waiting room (chat room) for this npc.
  7976. ///
  7977. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7978. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7979. /// waitingroom "<title>",<limit>,"<event>";
  7980. /// waitingroom "<title>",<limit>;
  7981. BUILDIN_FUNC(waitingroom)
  7982. {
  7983. struct npc_data* nd;
  7984. const char* title;
  7985. const char* ev = "";
  7986. int limit;
  7987. int trigger = 0;
  7988. int pub = 1;
  7989. title = script_getstr(st, 2);
  7990. limit = script_getnum(st, 3);
  7991. if( script_hasdata(st,5) )
  7992. {
  7993. struct script_data* last = script_getdata(st, 5);
  7994. get_val(st, last);
  7995. if( data_isstring(last) )
  7996. {// ,<trigger>,"<event>"
  7997. trigger = script_getnum(st, 4);
  7998. ev = script_getstr(st, 5);
  7999. }
  8000. else
  8001. {// ,"<event>",<trigger>
  8002. ev = script_getstr(st, 4);
  8003. trigger = script_getnum(st,5);
  8004. }
  8005. }
  8006. else if( script_hasdata(st,4) )
  8007. {// ,"<event>"
  8008. ev = script_getstr(st, 4);
  8009. trigger = limit;
  8010. }
  8011. nd = (struct npc_data *)map_id2bl(st->oid);
  8012. if( nd != NULL )
  8013. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  8014. return 0;
  8015. }
  8016. /// Removes the waiting room of the current or target npc.
  8017. ///
  8018. /// delwaitingroom "<npc_name>";
  8019. /// delwaitingroom;
  8020. BUILDIN_FUNC(delwaitingroom)
  8021. {
  8022. struct npc_data* nd;
  8023. if( script_hasdata(st,2) )
  8024. nd = npc_name2id(script_getstr(st, 2));
  8025. else
  8026. nd = (struct npc_data *)map_id2bl(st->oid);
  8027. if( nd != NULL )
  8028. chat_deletenpcchat(nd);
  8029. return 0;
  8030. }
  8031. /// Kicks all the players from the waiting room of the current or target npc.
  8032. ///
  8033. /// kickwaitingroomall "<npc_name>";
  8034. /// kickwaitingroomall;
  8035. BUILDIN_FUNC(waitingroomkickall)
  8036. {
  8037. struct npc_data* nd;
  8038. struct chat_data* cd;
  8039. if( script_hasdata(st,2) )
  8040. nd = npc_name2id(script_getstr(st,2));
  8041. else
  8042. nd = (struct npc_data *)map_id2bl(st->oid);
  8043. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8044. chat_npckickall(cd);
  8045. return 0;
  8046. }
  8047. /// Enables the waiting room event of the current or target npc.
  8048. ///
  8049. /// enablewaitingroomevent "<npc_name>";
  8050. /// enablewaitingroomevent;
  8051. BUILDIN_FUNC(enablewaitingroomevent)
  8052. {
  8053. struct npc_data* nd;
  8054. struct chat_data* cd;
  8055. if( script_hasdata(st,2) )
  8056. nd = npc_name2id(script_getstr(st, 2));
  8057. else
  8058. nd = (struct npc_data *)map_id2bl(st->oid);
  8059. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8060. chat_enableevent(cd);
  8061. return 0;
  8062. }
  8063. /// Disables the waiting room event of the current or target npc.
  8064. ///
  8065. /// disablewaitingroomevent "<npc_name>";
  8066. /// disablewaitingroomevent;
  8067. BUILDIN_FUNC(disablewaitingroomevent)
  8068. {
  8069. struct npc_data *nd;
  8070. struct chat_data *cd;
  8071. if( script_hasdata(st,2) )
  8072. nd = npc_name2id(script_getstr(st, 2));
  8073. else
  8074. nd = (struct npc_data *)map_id2bl(st->oid);
  8075. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8076. chat_disableevent(cd);
  8077. return 0;
  8078. }
  8079. /// Returns info on the waiting room of the current or target npc.
  8080. /// Returns -1 if the type unknown
  8081. /// <type>=0 : current number of users
  8082. /// <type>=1 : maximum number of users allowed
  8083. /// <type>=2 : the number of users that trigger the event
  8084. /// <type>=3 : if the trigger is disabled
  8085. /// <type>=4 : the title of the waiting room
  8086. /// <type>=5 : the password of the waiting room
  8087. /// <type>=16 : the name of the waiting room event
  8088. /// <type>=32 : if the waiting room is full
  8089. /// <type>=33 : if there are enough users to trigger the event
  8090. ///
  8091. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8092. /// getwaitingroomstate(<type>) -> <info>
  8093. BUILDIN_FUNC(getwaitingroomstate)
  8094. {
  8095. struct npc_data *nd;
  8096. struct chat_data *cd;
  8097. int type;
  8098. type = script_getnum(st,2);
  8099. if( script_hasdata(st,3) )
  8100. nd = npc_name2id(script_getstr(st, 3));
  8101. else
  8102. nd = (struct npc_data *)map_id2bl(st->oid);
  8103. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8104. {
  8105. script_pushint(st, -1);
  8106. return 0;
  8107. }
  8108. switch(type)
  8109. {
  8110. case 0: script_pushint(st, cd->users); break;
  8111. case 1: script_pushint(st, cd->limit); break;
  8112. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8113. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8114. case 4: script_pushstrcopy(st, cd->title); break;
  8115. case 5: script_pushstrcopy(st, cd->pass); break;
  8116. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8117. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8118. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8119. default: script_pushint(st, -1); break;
  8120. }
  8121. return 0;
  8122. }
  8123. /// Warps the trigger or target amount of players to the target map and position.
  8124. /// Players are automatically removed from the waiting room.
  8125. /// Those waiting the longest will get warped first.
  8126. /// The target map can be "Random" for a random position in the current map,
  8127. /// and "SavePoint" for the savepoint map+position.
  8128. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8129. ///
  8130. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8131. /// The total number of teleported players is put into $@warpwaitingpcnum
  8132. ///
  8133. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8134. /// warpwaitingpc "<map name>",<x>,<y>;
  8135. BUILDIN_FUNC(warpwaitingpc)
  8136. {
  8137. int x;
  8138. int y;
  8139. int i;
  8140. int n;
  8141. const char* map_name;
  8142. struct npc_data* nd;
  8143. struct chat_data* cd;
  8144. TBL_PC* sd;
  8145. nd = (struct npc_data *)map_id2bl(st->oid);
  8146. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8147. return 0;
  8148. map_name = script_getstr(st,2);
  8149. x = script_getnum(st,3);
  8150. y = script_getnum(st,4);
  8151. n = cd->trigger&0x7f;
  8152. if( script_hasdata(st,5) )
  8153. n = script_getnum(st,5);
  8154. for( i = 0; i < n && cd->users > 0; i++ )
  8155. {
  8156. sd = cd->usersd[0];
  8157. if( sd == NULL )
  8158. {
  8159. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  8160. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  8161. continue;// Broken npc chat room?
  8162. }
  8163. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  8164. if( strcmp(map_name,"Random") == 0 )
  8165. pc_randomwarp(sd,CLR_TELEPORT);
  8166. else if( strcmp(map_name,"SavePoint") == 0 )
  8167. {
  8168. if( map[sd->bl.m].flag.noteleport )
  8169. return 0;// can't teleport on this map
  8170. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  8171. }
  8172. else
  8173. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  8174. }
  8175. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8176. return 0;
  8177. }
  8178. /////////////////////////////////////////////////////////////////////
  8179. // ...
  8180. //
  8181. /// Detaches a character from a script.
  8182. ///
  8183. /// @param st Script state to detach the character from.
  8184. static void script_detach_rid(struct script_state* st)
  8185. {
  8186. if(st->rid)
  8187. {
  8188. script_detach_state(st, false);
  8189. st->rid = 0;
  8190. }
  8191. }
  8192. /*==========================================
  8193. * RIDのアタッチ
  8194. *------------------------------------------*/
  8195. BUILDIN_FUNC(attachrid)
  8196. {
  8197. int rid = script_getnum(st,2);
  8198. struct map_session_data* sd;
  8199. if ((sd = map_id2sd(rid))!=NULL) {
  8200. script_detach_rid(st);
  8201. st->rid = rid;
  8202. script_attach_state(st);
  8203. script_pushint(st,1);
  8204. } else
  8205. script_pushint(st,0);
  8206. return 0;
  8207. }
  8208. /*==========================================
  8209. * RIDのデタッチ
  8210. *------------------------------------------*/
  8211. BUILDIN_FUNC(detachrid)
  8212. {
  8213. script_detach_rid(st);
  8214. return 0;
  8215. }
  8216. /*==========================================
  8217. * 存在チェック
  8218. *------------------------------------------*/
  8219. BUILDIN_FUNC(isloggedin)
  8220. {
  8221. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8222. if (script_hasdata(st,3) && sd &&
  8223. sd->status.char_id != script_getnum(st,3))
  8224. sd = NULL;
  8225. push_val(st->stack,C_INT,sd!=NULL);
  8226. return 0;
  8227. }
  8228. /*==========================================
  8229. *
  8230. *------------------------------------------*/
  8231. BUILDIN_FUNC(setmapflagnosave)
  8232. {
  8233. int m,x,y;
  8234. unsigned short mapindex;
  8235. const char *str,*str2;
  8236. str=script_getstr(st,2);
  8237. str2=script_getstr(st,3);
  8238. x=script_getnum(st,4);
  8239. y=script_getnum(st,5);
  8240. m = map_mapname2mapid(str);
  8241. mapindex = mapindex_name2id(str2);
  8242. if(m >= 0 && mapindex) {
  8243. map[m].flag.nosave=1;
  8244. map[m].save.map=mapindex;
  8245. map[m].save.x=x;
  8246. map[m].save.y=y;
  8247. }
  8248. return 0;
  8249. }
  8250. BUILDIN_FUNC(getmapflag)
  8251. {
  8252. int m,i;
  8253. const char *str;
  8254. str=script_getstr(st,2);
  8255. i=script_getnum(st,3);
  8256. m = map_mapname2mapid(str);
  8257. if(m >= 0) {
  8258. switch(i) {
  8259. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8260. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8261. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8262. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8263. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8264. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8265. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8266. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8267. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8268. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8269. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8270. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8271. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8272. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8273. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8274. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8275. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8276. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8277. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8278. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8279. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8280. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8281. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8282. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8283. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8284. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8285. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8286. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8287. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8288. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8289. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8290. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8291. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8292. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8293. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8294. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8295. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8296. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8297. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8298. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8299. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8300. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8301. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8302. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8303. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8304. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8305. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8306. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8307. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8308. }
  8309. }
  8310. return 0;
  8311. }
  8312. BUILDIN_FUNC(setmapflag)
  8313. {
  8314. int m,i;
  8315. const char *str;
  8316. const char *val=NULL;
  8317. str=script_getstr(st,2);
  8318. i=script_getnum(st,3);
  8319. if(script_hasdata(st,4)){
  8320. val=script_getstr(st,4);
  8321. }
  8322. m = map_mapname2mapid(str);
  8323. if(m >= 0) {
  8324. switch(i) {
  8325. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  8326. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  8327. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  8328. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  8329. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  8330. case MF_PVP: map[m].flag.pvp=1; break;
  8331. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  8332. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  8333. case MF_GVG: map[m].flag.gvg=1; break;
  8334. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  8335. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  8336. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  8337. case MF_NOTRADE: map[m].flag.notrade=1; break;
  8338. case MF_NODROP: map[m].flag.nodrop=1; break;
  8339. case MF_NOSKILL: map[m].flag.noskill=1; break;
  8340. case MF_NOWARP: map[m].flag.nowarp=1; break;
  8341. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  8342. case MF_SNOW: map[m].flag.snow=1; break;
  8343. case MF_CLOUDS: map[m].flag.clouds=1; break;
  8344. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  8345. case MF_FOG: map[m].flag.fog=1; break;
  8346. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  8347. case MF_SAKURA: map[m].flag.sakura=1; break;
  8348. case MF_LEAVES: map[m].flag.leaves=1; break;
  8349. case MF_RAIN: map[m].flag.rain=1; break;
  8350. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  8351. case MF_NOGO: map[m].flag.nogo=1; break;
  8352. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  8353. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  8354. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  8355. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  8356. case MF_NORETURN: map[m].flag.noreturn=1; break;
  8357. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  8358. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  8359. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  8360. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8361. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8362. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8363. case MF_NOVENDING: map[m].flag.novending=1; break;
  8364. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  8365. case MF_NOCHAT: map[m].flag.nochat=1; break;
  8366. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  8367. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  8368. case MF_TOWN: map[m].flag.town=1; break;
  8369. case MF_AUTOTRADE: map[m].flag.autotrade=1; break;
  8370. case MF_ALLOWKS: map[m].flag.allowks=1; break;
  8371. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport=1; break;
  8372. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank=1; break;
  8373. case MF_BATTLEGROUND: map[m].flag.battleground = (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  8374. }
  8375. }
  8376. return 0;
  8377. }
  8378. BUILDIN_FUNC(removemapflag)
  8379. {
  8380. int m,i;
  8381. const char *str;
  8382. str=script_getstr(st,2);
  8383. i=script_getnum(st,3);
  8384. m = map_mapname2mapid(str);
  8385. if(m >= 0) {
  8386. switch(i) {
  8387. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  8388. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  8389. case MF_NOSAVE: map[m].flag.nosave=0; break;
  8390. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  8391. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  8392. case MF_PVP: map[m].flag.pvp=0; break;
  8393. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  8394. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  8395. case MF_GVG: map[m].flag.gvg=0; break;
  8396. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8397. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8398. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8399. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8400. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8401. case MF_NODROP: map[m].flag.nodrop=0; break;
  8402. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8403. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8404. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8405. case MF_SNOW: map[m].flag.snow=0; break;
  8406. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8407. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8408. case MF_FOG: map[m].flag.fog=0; break;
  8409. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8410. case MF_SAKURA: map[m].flag.sakura=0; break;
  8411. case MF_LEAVES: map[m].flag.leaves=0; break;
  8412. case MF_RAIN: map[m].flag.rain=0; break;
  8413. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8414. case MF_NOGO: map[m].flag.nogo=0; break;
  8415. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8416. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8417. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8418. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8419. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8420. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8421. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8422. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8423. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8424. case MF_JEXP: map[m].jexp=100; break;
  8425. case MF_BEXP: map[m].bexp=100; break;
  8426. case MF_NOVENDING: map[m].flag.novending=0; break;
  8427. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8428. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8429. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8430. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8431. case MF_TOWN: map[m].flag.town=0; break;
  8432. case MF_AUTOTRADE: map[m].flag.autotrade=0; break;
  8433. case MF_ALLOWKS: map[m].flag.allowks=0; break;
  8434. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport=0; break;
  8435. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank=0; break;
  8436. case MF_BATTLEGROUND: map[m].flag.battleground=0; break;
  8437. }
  8438. }
  8439. return 0;
  8440. }
  8441. BUILDIN_FUNC(pvpon)
  8442. {
  8443. int m;
  8444. const char *str;
  8445. TBL_PC* sd = NULL;
  8446. struct s_mapiterator* iter;
  8447. str = script_getstr(st,2);
  8448. m = map_mapname2mapid(str);
  8449. if( m < 0 || map[m].flag.pvp )
  8450. return 0; // nothing to do
  8451. map[m].flag.pvp = 1;
  8452. clif_send0199(m,1);
  8453. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8454. return 0;
  8455. iter = mapit_getallusers();
  8456. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8457. {
  8458. if( sd->bl.m != m || sd->pvp_timer != -1 )
  8459. continue; // not applicable
  8460. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8461. sd->pvp_rank = 0;
  8462. sd->pvp_lastusers = 0;
  8463. sd->pvp_point = 5;
  8464. sd->pvp_won = 0;
  8465. sd->pvp_lost = 0;
  8466. }
  8467. mapit_free(iter);
  8468. return 0;
  8469. }
  8470. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8471. {
  8472. TBL_PC* sd = (TBL_PC*)bl;
  8473. clif_pvpset(sd, 0, 0, 2);
  8474. if (sd->pvp_timer != -1) {
  8475. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8476. sd->pvp_timer = INVALID_TIMER;
  8477. }
  8478. return 0;
  8479. }
  8480. BUILDIN_FUNC(pvpoff)
  8481. {
  8482. int m;
  8483. const char *str;
  8484. str=script_getstr(st,2);
  8485. m = map_mapname2mapid(str);
  8486. if(m < 0 || !map[m].flag.pvp)
  8487. return 0; //fixed Lupus
  8488. map[m].flag.pvp = 0;
  8489. clif_send0199(m,0);
  8490. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8491. return 0;
  8492. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8493. return 0;
  8494. }
  8495. BUILDIN_FUNC(gvgon)
  8496. {
  8497. int m;
  8498. const char *str;
  8499. str=script_getstr(st,2);
  8500. m = map_mapname2mapid(str);
  8501. if(m >= 0 && !map[m].flag.gvg) {
  8502. map[m].flag.gvg = 1;
  8503. clif_send0199(m,3);
  8504. }
  8505. return 0;
  8506. }
  8507. BUILDIN_FUNC(gvgoff)
  8508. {
  8509. int m;
  8510. const char *str;
  8511. str=script_getstr(st,2);
  8512. m = map_mapname2mapid(str);
  8513. if(m >= 0 && map[m].flag.gvg) {
  8514. map[m].flag.gvg = 0;
  8515. clif_send0199(m,0);
  8516. }
  8517. return 0;
  8518. }
  8519. /*==========================================
  8520. * Shows an emoticon on top of the player/npc
  8521. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  8522. *------------------------------------------*/
  8523. //Optional second parameter added by [Skotlex]
  8524. BUILDIN_FUNC(emotion)
  8525. {
  8526. int type;
  8527. int player=0;
  8528. type=script_getnum(st,2);
  8529. if(type < 0 || type > 100)
  8530. return 0;
  8531. if( script_hasdata(st,3) )
  8532. player=script_getnum(st,3);
  8533. if (player) {
  8534. TBL_PC *sd = NULL;
  8535. if( script_hasdata(st,4) )
  8536. sd = map_nick2sd(script_getstr(st,4));
  8537. else
  8538. sd = script_rid2sd(st);
  8539. if (sd)
  8540. clif_emotion(&sd->bl,type);
  8541. } else
  8542. if( script_hasdata(st,4) )
  8543. {
  8544. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  8545. if(nd)
  8546. clif_emotion(&nd->bl,type);
  8547. }
  8548. else
  8549. clif_emotion(map_id2bl(st->oid),type);
  8550. return 0;
  8551. }
  8552. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8553. {
  8554. int m=va_arg(ap,int);
  8555. int g_id=va_arg(ap,int);
  8556. int flag=va_arg(ap,int);
  8557. if(!sd || sd->bl.m != m)
  8558. return 0;
  8559. if(
  8560. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8561. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8562. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8563. )
  8564. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  8565. return 1;
  8566. }
  8567. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8568. {
  8569. struct mob_data *md=(struct mob_data *)bl;
  8570. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8571. status_kill(bl);
  8572. return 0;
  8573. }
  8574. BUILDIN_FUNC(maprespawnguildid)
  8575. {
  8576. const char *mapname=script_getstr(st,2);
  8577. int g_id=script_getnum(st,3);
  8578. int flag=script_getnum(st,4);
  8579. int m=map_mapname2mapid(mapname);
  8580. if(m == -1)
  8581. return 0;
  8582. //Catch ALL players (in case some are 'between maps' on execution time)
  8583. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8584. if (flag&4) //Remove script mobs.
  8585. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8586. return 0;
  8587. }
  8588. BUILDIN_FUNC(agitstart)
  8589. {
  8590. if(agit_flag==1) return 0; // Agit already Start.
  8591. agit_flag=1;
  8592. guild_agit_start();
  8593. return 0;
  8594. }
  8595. BUILDIN_FUNC(agitend)
  8596. {
  8597. if(agit_flag==0) return 0; // Agit already End.
  8598. agit_flag=0;
  8599. guild_agit_end();
  8600. return 0;
  8601. }
  8602. BUILDIN_FUNC(agitstart2)
  8603. {
  8604. if(agit2_flag==1) return 0; // Agit2 already Start.
  8605. agit2_flag=1;
  8606. guild_agit2_start();
  8607. return 0;
  8608. }
  8609. BUILDIN_FUNC(agitend2)
  8610. {
  8611. if(agit2_flag==0) return 0; // Agit2 already End.
  8612. agit2_flag=0;
  8613. guild_agit2_end();
  8614. return 0;
  8615. }
  8616. /*==========================================
  8617. * Returns whether woe is on or off. // choice script
  8618. *------------------------------------------*/
  8619. BUILDIN_FUNC(agitcheck)
  8620. {
  8621. script_pushint(st,agit_flag);
  8622. return 0;
  8623. }
  8624. /*==========================================
  8625. * Returns whether woese is on or off. // choice script
  8626. *------------------------------------------*/
  8627. BUILDIN_FUNC(agitcheck2)
  8628. {
  8629. script_pushint(st,agit2_flag);
  8630. return 0;
  8631. }
  8632. /// Sets the guild_id of this npc.
  8633. ///
  8634. /// flagemblem <guild_id>;
  8635. BUILDIN_FUNC(flagemblem)
  8636. {
  8637. TBL_NPC* nd;
  8638. int g_id=script_getnum(st,2);
  8639. if(g_id < 0) return 0;
  8640. nd = (TBL_NPC*)map_id2nd(st->oid);
  8641. if( nd == NULL )
  8642. {
  8643. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8644. }
  8645. else if( nd->subtype != SCRIPT )
  8646. {
  8647. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8648. }
  8649. else
  8650. {
  8651. nd->u.scr.guild_id = g_id;
  8652. clif_guild_emblem_area(&nd->bl);
  8653. }
  8654. return 0;
  8655. }
  8656. BUILDIN_FUNC(getcastlename)
  8657. {
  8658. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8659. struct guild_castle* gc = guild_mapname2gc(mapname);
  8660. const char* name = (gc) ? gc->castle_name : "";
  8661. script_pushstrcopy(st,name);
  8662. return 0;
  8663. }
  8664. BUILDIN_FUNC(getcastledata)
  8665. {
  8666. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8667. int index = script_getnum(st,3);
  8668. struct guild_castle* gc = guild_mapname2gc(mapname);
  8669. if(script_hasdata(st,4) && index==0 && gc) {
  8670. const char* event = script_getstr(st,4);
  8671. check_event(st, event);
  8672. guild_addcastleinfoevent(gc->castle_id,17,event);
  8673. }
  8674. if(gc){
  8675. switch(index){
  8676. case 0: {
  8677. int i;
  8678. for(i=1;i<18;i++) // Initialize[AgitInit]
  8679. guild_castledataload(gc->castle_id,i);
  8680. } break;
  8681. case 1:
  8682. script_pushint(st,gc->guild_id); break;
  8683. case 2:
  8684. script_pushint(st,gc->economy); break;
  8685. case 3:
  8686. script_pushint(st,gc->defense); break;
  8687. case 4:
  8688. script_pushint(st,gc->triggerE); break;
  8689. case 5:
  8690. script_pushint(st,gc->triggerD); break;
  8691. case 6:
  8692. script_pushint(st,gc->nextTime); break;
  8693. case 7:
  8694. script_pushint(st,gc->payTime); break;
  8695. case 8:
  8696. script_pushint(st,gc->createTime); break;
  8697. case 9:
  8698. script_pushint(st,gc->visibleC); break;
  8699. case 10:
  8700. case 11:
  8701. case 12:
  8702. case 13:
  8703. case 14:
  8704. case 15:
  8705. case 16:
  8706. case 17:
  8707. script_pushint(st,gc->guardian[index-10].visible); break;
  8708. default:
  8709. script_pushint(st,0); break;
  8710. }
  8711. return 0;
  8712. }
  8713. script_pushint(st,0);
  8714. return 0;
  8715. }
  8716. BUILDIN_FUNC(setcastledata)
  8717. {
  8718. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8719. int index = script_getnum(st,3);
  8720. int value = script_getnum(st,4);
  8721. struct guild_castle* gc = guild_mapname2gc(mapname);
  8722. if(gc) {
  8723. // Save Data byself First
  8724. switch(index){
  8725. case 1:
  8726. gc->guild_id = value; break;
  8727. case 2:
  8728. gc->economy = value; break;
  8729. case 3:
  8730. gc->defense = value; break;
  8731. case 4:
  8732. gc->triggerE = value; break;
  8733. case 5:
  8734. gc->triggerD = value; break;
  8735. case 6:
  8736. gc->nextTime = value; break;
  8737. case 7:
  8738. gc->payTime = value; break;
  8739. case 8:
  8740. gc->createTime = value; break;
  8741. case 9:
  8742. gc->visibleC = value; break;
  8743. case 10:
  8744. case 11:
  8745. case 12:
  8746. case 13:
  8747. case 14:
  8748. case 15:
  8749. case 16:
  8750. case 17:
  8751. gc->guardian[index-10].visible = value; break;
  8752. default:
  8753. return 0;
  8754. }
  8755. guild_castledatasave(gc->castle_id,index,value);
  8756. }
  8757. return 0;
  8758. }
  8759. /* =====================================================================
  8760. * ギルド情報を要求する
  8761. * ---------------------------------------------------------------------*/
  8762. BUILDIN_FUNC(requestguildinfo)
  8763. {
  8764. int guild_id=script_getnum(st,2);
  8765. const char *event=NULL;
  8766. if( script_hasdata(st,3) ){
  8767. event=script_getstr(st,3);
  8768. check_event(st, event);
  8769. }
  8770. if(guild_id>0)
  8771. guild_npc_request_info(guild_id,event);
  8772. return 0;
  8773. }
  8774. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8775. /// getequipcardcnt(<equipment slot>);
  8776. BUILDIN_FUNC(getequipcardcnt)
  8777. {
  8778. int i=-1,j,num;
  8779. TBL_PC *sd;
  8780. int count;
  8781. num=script_getnum(st,2);
  8782. sd=script_rid2sd(st);
  8783. if (num > 0 && num <= ARRAYLENGTH(equip))
  8784. i=pc_checkequip(sd,equip[num-1]);
  8785. if (i < 0 || !sd->inventory_data[i]) {
  8786. script_pushint(st,0);
  8787. return 0;
  8788. }
  8789. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8790. {
  8791. script_pushint(st,0);
  8792. return 0;
  8793. }
  8794. count = 0;
  8795. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  8796. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8797. count++;
  8798. script_pushint(st,count);
  8799. return 0;
  8800. }
  8801. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8802. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8803. /// successremovecards <slot>;
  8804. BUILDIN_FUNC(successremovecards)
  8805. {
  8806. int i=-1,j,c,cardflag=0;
  8807. TBL_PC* sd = script_rid2sd(st);
  8808. int num = script_getnum(st,2);
  8809. if (num > 0 && num <= ARRAYLENGTH(equip))
  8810. i=pc_checkequip(sd,equip[num-1]);
  8811. if (i < 0 || !sd->inventory_data[i]) {
  8812. return 0;
  8813. }
  8814. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8815. return 0;
  8816. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8817. {
  8818. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8819. {// extract this card from the item
  8820. int flag;
  8821. struct item item_tmp;
  8822. cardflag = 1;
  8823. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8824. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8825. item_tmp.attribute=0,item_tmp.expire_time=0;
  8826. for (j = 0; j < MAX_SLOTS; j++)
  8827. item_tmp.card[j]=0;
  8828. //Logs items, got from (N)PC scripts [Lupus]
  8829. if(log_config.enable_logs&0x40)
  8830. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8831. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8832. clif_additem(sd,0,0,flag);
  8833. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8834. }
  8835. }
  8836. }
  8837. if(cardflag == 1)
  8838. { // カードを取り除いたアイテム所得
  8839. int flag;
  8840. struct item item_tmp;
  8841. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8842. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8843. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8844. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  8845. item_tmp.card[j]=0;
  8846. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  8847. item_tmp.card[j]=sd->status.inventory[i].card[j];
  8848. //Logs items, got from (N)PC scripts [Lupus]
  8849. if(log_config.enable_logs&0x40)
  8850. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8851. pc_delitem(sd,i,1,0,3);
  8852. //Logs items, got from (N)PC scripts [Lupus]
  8853. if(log_config.enable_logs&0x40)
  8854. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8855. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8856. clif_additem(sd,0,0,flag);
  8857. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8858. }
  8859. clif_misceffect(&sd->bl,3);
  8860. }
  8861. return 0;
  8862. }
  8863. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8864. /// failedremovecards <slot>, <type>;
  8865. /// <type>=0 : will destroy both the item and the cards.
  8866. /// <type>=1 : will keep the item, but destroy the cards.
  8867. /// <type>=2 : will keep the cards, but destroy the item.
  8868. /// <type>=? : will just display the failure effect.
  8869. BUILDIN_FUNC(failedremovecards)
  8870. {
  8871. int i=-1,j,c,cardflag=0;
  8872. TBL_PC* sd = script_rid2sd(st);
  8873. int num = script_getnum(st,2);
  8874. int typefail = script_getnum(st,3);
  8875. if (num > 0 && num <= ARRAYLENGTH(equip))
  8876. i=pc_checkequip(sd,equip[num-1]);
  8877. if (i < 0 || !sd->inventory_data[i])
  8878. return 0;
  8879. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8880. return 0;
  8881. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8882. {
  8883. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8884. {
  8885. cardflag = 1;
  8886. if(typefail == 2)
  8887. {// add cards to inventory, clear
  8888. int flag;
  8889. struct item item_tmp;
  8890. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8891. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8892. item_tmp.attribute=0,item_tmp.expire_time=0;
  8893. for (j = 0; j < MAX_SLOTS; j++)
  8894. item_tmp.card[j]=0;
  8895. //Logs items, got from (N)PC scripts [Lupus]
  8896. if(log_config.enable_logs&0x40)
  8897. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8898. if((flag=pc_additem(sd,&item_tmp,1))){
  8899. clif_additem(sd,0,0,flag);
  8900. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8901. }
  8902. }
  8903. }
  8904. }
  8905. if(cardflag == 1)
  8906. {
  8907. if(typefail == 0 || typefail == 2){ // 武具損失
  8908. //Logs items, got from (N)PC scripts [Lupus]
  8909. if(log_config.enable_logs&0x40)
  8910. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8911. pc_delitem(sd,i,1,0,2);
  8912. }
  8913. if(typefail == 1){ // カードのみ損失(武具を返す)
  8914. int flag;
  8915. struct item item_tmp;
  8916. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8917. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8918. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8919. //Logs items, got from (N)PC scripts [Lupus]
  8920. if(log_config.enable_logs&0x40)
  8921. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8922. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  8923. item_tmp.card[j]=0;
  8924. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  8925. item_tmp.card[j]=sd->status.inventory[i].card[j];
  8926. pc_delitem(sd,i,1,0,2);
  8927. //Logs items, got from (N)PC scripts [Lupus]
  8928. if(log_config.enable_logs&0x40)
  8929. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8930. if((flag=pc_additem(sd,&item_tmp,1))){
  8931. clif_additem(sd,0,0,flag);
  8932. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8933. }
  8934. }
  8935. clif_misceffect(&sd->bl,2);
  8936. }
  8937. return 0;
  8938. }
  8939. /* ================================================================
  8940. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8941. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8942. * improved by [Lance]
  8943. * ================================================================*/
  8944. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8945. {
  8946. int x,y,m,check_val=0,check_ID=0,i=0;
  8947. struct guild *g = NULL;
  8948. struct party_data *p = NULL;
  8949. const char *str;
  8950. const char *mapname;
  8951. unsigned int index;
  8952. mapname=script_getstr(st,2);
  8953. str=script_getstr(st,3);
  8954. x=script_getnum(st,4);
  8955. y=script_getnum(st,5);
  8956. if(script_hasdata(st,7)){
  8957. check_val=script_getnum(st,6);
  8958. check_ID=script_getnum(st,7);
  8959. }
  8960. if((m=map_mapname2mapid(mapname))< 0)
  8961. return 0;
  8962. if(!(index=mapindex_name2id(str)))
  8963. return 0;
  8964. switch(check_val){
  8965. case 1:
  8966. g = guild_search(check_ID);
  8967. if (g){
  8968. for( i=0; i < g->max_member; i++)
  8969. {
  8970. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8971. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  8972. }
  8973. }
  8974. }
  8975. break;
  8976. case 2:
  8977. p = party_search(check_ID);
  8978. if(p){
  8979. for(i=0;i<MAX_PARTY; i++){
  8980. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8981. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  8982. }
  8983. }
  8984. }
  8985. break;
  8986. default:
  8987. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8988. break;
  8989. }
  8990. return 0;
  8991. }
  8992. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8993. {
  8994. char *event=va_arg(ap,char *);
  8995. struct mob_data *md = ((struct mob_data *)bl);
  8996. if(strcmp(event,md->npc_event)==0 && md->status.hp > 0)
  8997. return 1;
  8998. return 0;
  8999. }
  9000. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9001. {
  9002. const char *mapname,*event;
  9003. int m;
  9004. mapname=script_getstr(st,2);
  9005. event=script_getstr(st,3);
  9006. check_event(st, event);
  9007. if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9008. script_pushint(st,-1);
  9009. return 0;
  9010. }
  9011. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9012. {
  9013. script_pushint(st,-1);
  9014. return 0;
  9015. }
  9016. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9017. return 0;
  9018. }
  9019. BUILDIN_FUNC(marriage)
  9020. {
  9021. const char *partner=script_getstr(st,2);
  9022. TBL_PC *sd=script_rid2sd(st);
  9023. TBL_PC *p_sd=map_nick2sd(partner);
  9024. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9025. script_pushint(st,0);
  9026. return 0;
  9027. }
  9028. script_pushint(st,1);
  9029. return 0;
  9030. }
  9031. BUILDIN_FUNC(wedding_effect)
  9032. {
  9033. TBL_PC *sd=script_rid2sd(st);
  9034. struct block_list *bl;
  9035. if(sd==NULL) {
  9036. bl=map_id2bl(st->oid);
  9037. } else
  9038. bl=&sd->bl;
  9039. clif_wedding_effect(bl);
  9040. return 0;
  9041. }
  9042. BUILDIN_FUNC(divorce)
  9043. {
  9044. TBL_PC *sd=script_rid2sd(st);
  9045. if(sd==NULL || pc_divorce(sd) < 0){
  9046. script_pushint(st,0);
  9047. return 0;
  9048. }
  9049. script_pushint(st,1);
  9050. return 0;
  9051. }
  9052. BUILDIN_FUNC(ispartneron)
  9053. {
  9054. TBL_PC *sd=script_rid2sd(st);
  9055. if(sd==NULL || !pc_ismarried(sd) ||
  9056. map_charid2sd(sd->status.partner_id) == NULL) {
  9057. script_pushint(st,0);
  9058. return 0;
  9059. }
  9060. script_pushint(st,1);
  9061. return 0;
  9062. }
  9063. BUILDIN_FUNC(getpartnerid)
  9064. {
  9065. TBL_PC *sd=script_rid2sd(st);
  9066. if (sd == NULL) {
  9067. script_pushint(st,0);
  9068. return 0;
  9069. }
  9070. script_pushint(st,sd->status.partner_id);
  9071. return 0;
  9072. }
  9073. BUILDIN_FUNC(getchildid)
  9074. {
  9075. TBL_PC *sd=script_rid2sd(st);
  9076. if (sd == NULL) {
  9077. script_pushint(st,0);
  9078. return 0;
  9079. }
  9080. script_pushint(st,sd->status.child);
  9081. return 0;
  9082. }
  9083. BUILDIN_FUNC(getmotherid)
  9084. {
  9085. TBL_PC *sd=script_rid2sd(st);
  9086. if (sd == NULL) {
  9087. script_pushint(st,0);
  9088. return 0;
  9089. }
  9090. script_pushint(st,sd->status.mother);
  9091. return 0;
  9092. }
  9093. BUILDIN_FUNC(getfatherid)
  9094. {
  9095. TBL_PC *sd=script_rid2sd(st);
  9096. if (sd == NULL) {
  9097. script_pushint(st,0);
  9098. return 0;
  9099. }
  9100. script_pushint(st,sd->status.father);
  9101. return 0;
  9102. }
  9103. BUILDIN_FUNC(warppartner)
  9104. {
  9105. int x,y;
  9106. unsigned short mapindex;
  9107. const char *str;
  9108. TBL_PC *sd=script_rid2sd(st);
  9109. TBL_PC *p_sd=NULL;
  9110. if(sd==NULL || !pc_ismarried(sd) ||
  9111. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9112. script_pushint(st,0);
  9113. return 0;
  9114. }
  9115. str=script_getstr(st,2);
  9116. x=script_getnum(st,3);
  9117. y=script_getnum(st,4);
  9118. mapindex = mapindex_name2id(str);
  9119. if (mapindex) {
  9120. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  9121. script_pushint(st,1);
  9122. } else
  9123. script_pushint(st,0);
  9124. return 0;
  9125. }
  9126. /*================================================
  9127. * Script for Displaying MOB Information [Valaris]
  9128. *------------------------------------------------*/
  9129. BUILDIN_FUNC(strmobinfo)
  9130. {
  9131. int num=script_getnum(st,2);
  9132. int class_=script_getnum(st,3);
  9133. if(!mobdb_checkid(class_))
  9134. {
  9135. script_pushint(st,0);
  9136. return 0;
  9137. }
  9138. switch (num) {
  9139. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9140. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9141. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9142. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9143. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9144. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9145. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9146. default:
  9147. script_pushint(st,0);
  9148. break;
  9149. }
  9150. return 0;
  9151. }
  9152. /*==========================================
  9153. * Summon guardians [Valaris]
  9154. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9155. *------------------------------------------*/
  9156. BUILDIN_FUNC(guardian)
  9157. {
  9158. int class_=0,x=0,y=0,guardian=0;
  9159. const char *str,*map,*evt="";
  9160. struct script_data *data;
  9161. bool has_index = false;
  9162. map =script_getstr(st,2);
  9163. x =script_getnum(st,3);
  9164. y =script_getnum(st,4);
  9165. str =script_getstr(st,5);
  9166. class_=script_getnum(st,6);
  9167. if( script_hasdata(st,8) )
  9168. {// "<event label>",<guardian index>
  9169. evt=script_getstr(st,7);
  9170. guardian=script_getnum(st,8);
  9171. has_index = true;
  9172. } else if( script_hasdata(st,7) ){
  9173. data=script_getdata(st,7);
  9174. get_val(st,data);
  9175. if( data_isstring(data) )
  9176. {// "<event label>"
  9177. evt=script_getstr(st,7);
  9178. } else if( data_isint(data) )
  9179. {// <guardian index>
  9180. guardian=script_getnum(st,7);
  9181. has_index = true;
  9182. } else {
  9183. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9184. script_reportdata(data);
  9185. return 1;
  9186. }
  9187. }
  9188. check_event(st, evt);
  9189. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9190. return 0;
  9191. }
  9192. /*==========================================
  9193. * Invisible Walls [Zephyrus]
  9194. *------------------------------------------*/
  9195. BUILDIN_FUNC(setwall)
  9196. {
  9197. const char *map, *name;
  9198. int x, y, m, size, dir;
  9199. bool shootable;
  9200. map = script_getstr(st,2);
  9201. x = script_getnum(st,3);
  9202. y = script_getnum(st,4);
  9203. size = script_getnum(st,5);
  9204. dir = script_getnum(st,6);
  9205. shootable = script_getnum(st,7);
  9206. name = script_getstr(st,8);
  9207. if( (m = map_mapname2mapid(map)) < 0 )
  9208. return 0; // Invalid Map
  9209. map_iwall_set(m, x, y, size, dir, shootable, name);
  9210. return 0;
  9211. }
  9212. BUILDIN_FUNC(delwall)
  9213. {
  9214. const char *name = script_getstr(st,2);
  9215. map_iwall_remove(name);
  9216. return 0;
  9217. }
  9218. /// Retrieves various information about the specified guardian.
  9219. ///
  9220. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9221. /// type: 0 - whether it is deployed or not
  9222. /// 1 - maximum hp
  9223. /// 2 - current hp
  9224. ///
  9225. BUILDIN_FUNC(guardianinfo)
  9226. {
  9227. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9228. int id = script_getnum(st,3);
  9229. int type = script_getnum(st,4);
  9230. struct guild_castle* gc = guild_mapname2gc(mapname);
  9231. struct mob_data* gd;
  9232. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9233. {
  9234. script_pushint(st,-1);
  9235. return 0;
  9236. }
  9237. if( type == 0 )
  9238. script_pushint(st, gc->guardian[id].visible);
  9239. else
  9240. if( !gc->guardian[id].visible )
  9241. script_pushint(st,-1);
  9242. else
  9243. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9244. script_pushint(st,-1);
  9245. else
  9246. {
  9247. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9248. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9249. else
  9250. script_pushint(st,-1);
  9251. }
  9252. return 0;
  9253. }
  9254. /*==========================================
  9255. * IDからItem名
  9256. *------------------------------------------*/
  9257. BUILDIN_FUNC(getitemname)
  9258. {
  9259. int item_id=0;
  9260. struct item_data *i_data;
  9261. char *item_name;
  9262. struct script_data *data;
  9263. data=script_getdata(st,2);
  9264. get_val(st,data);
  9265. if( data_isstring(data) ){
  9266. const char *name=conv_str(st,data);
  9267. struct item_data *item_data = itemdb_searchname(name);
  9268. if( item_data )
  9269. item_id=item_data->nameid;
  9270. }else
  9271. item_id=conv_num(st,data);
  9272. i_data = itemdb_exists(item_id);
  9273. if (i_data == NULL)
  9274. {
  9275. script_pushconststr(st,"null");
  9276. return 0;
  9277. }
  9278. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9279. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9280. script_pushstr(st,item_name);
  9281. return 0;
  9282. }
  9283. /*==========================================
  9284. * Returns number of slots an item has. [Skotlex]
  9285. *------------------------------------------*/
  9286. BUILDIN_FUNC(getitemslots)
  9287. {
  9288. int item_id;
  9289. struct item_data *i_data;
  9290. item_id=script_getnum(st,2);
  9291. i_data = itemdb_exists(item_id);
  9292. if (i_data)
  9293. script_pushint(st,i_data->slot);
  9294. else
  9295. script_pushint(st,-1);
  9296. return 0;
  9297. }
  9298. /*==========================================
  9299. * Returns some values of an item [Lupus]
  9300. * Price, Weight, etc...
  9301. getiteminfo(itemID,n), where n
  9302. 0 value_buy;
  9303. 1 value_sell;
  9304. 2 type;
  9305. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9306. if = 0, then monsters don't drop it at all (rare or a quest item)
  9307. if = 10000, then this item is sold in NPC shops only
  9308. 4 sex;
  9309. 5 equip;
  9310. 6 weight;
  9311. 7 atk;
  9312. 8 def;
  9313. 9 range;
  9314. 10 slot;
  9315. 11 look;
  9316. 12 elv;
  9317. 13 wlv;
  9318. 14 view id
  9319. *------------------------------------------*/
  9320. BUILDIN_FUNC(getiteminfo)
  9321. {
  9322. int item_id,n;
  9323. int *item_arr;
  9324. struct item_data *i_data;
  9325. item_id = script_getnum(st,2);
  9326. n = script_getnum(st,3);
  9327. i_data = itemdb_exists(item_id);
  9328. if (i_data && n>=0 && n<=14) {
  9329. item_arr = (int*)&i_data->value_buy;
  9330. script_pushint(st,item_arr[n]);
  9331. } else
  9332. script_pushint(st,-1);
  9333. return 0;
  9334. }
  9335. /*==========================================
  9336. * Set some values of an item [Lupus]
  9337. * Price, Weight, etc...
  9338. setiteminfo(itemID,n,Value), where n
  9339. 0 value_buy;
  9340. 1 value_sell;
  9341. 2 type;
  9342. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9343. if = 0, then monsters don't drop it at all (rare or a quest item)
  9344. if = 10000, then this item is sold in NPC shops only
  9345. 4 sex;
  9346. 5 equip;
  9347. 6 weight;
  9348. 7 atk;
  9349. 8 def;
  9350. 9 range;
  9351. 10 slot;
  9352. 11 look;
  9353. 12 elv;
  9354. 13 wlv;
  9355. 14 view id
  9356. * Returns Value or -1 if the wrong field's been set
  9357. *------------------------------------------*/
  9358. BUILDIN_FUNC(setiteminfo)
  9359. {
  9360. int item_id,n,value;
  9361. int *item_arr;
  9362. struct item_data *i_data;
  9363. item_id = script_getnum(st,2);
  9364. n = script_getnum(st,3);
  9365. value = script_getnum(st,4);
  9366. i_data = itemdb_exists(item_id);
  9367. if (i_data && n>=0 && n<=14) {
  9368. item_arr = (int*)&i_data->value_buy;
  9369. item_arr[n] = value;
  9370. script_pushint(st,value);
  9371. } else
  9372. script_pushint(st,-1);
  9373. return 0;
  9374. }
  9375. /*==========================================
  9376. * Returns value from equipped item slot n [Lupus]
  9377. getequipcardid(num,slot)
  9378. where
  9379. num = eqip position slot
  9380. slot = 0,1,2,3 (Card Slot N)
  9381. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9382. it's useful when you want to check item cards or if it's signed
  9383. Useful for such quests as "Sign this refined item with players name" etc
  9384. Hat[0] +4 -> Player's Hat[0] +4
  9385. *------------------------------------------*/
  9386. BUILDIN_FUNC(getequipcardid)
  9387. {
  9388. int i=-1,num,slot;
  9389. TBL_PC *sd;
  9390. num=script_getnum(st,2);
  9391. slot=script_getnum(st,3);
  9392. sd=script_rid2sd(st);
  9393. if (num > 0 && num <= ARRAYLENGTH(equip))
  9394. i=pc_checkequip(sd,equip[num-1]);
  9395. if(i >= 0 && slot>=0 && slot<4)
  9396. script_pushint(st,sd->status.inventory[i].card[slot]);
  9397. else
  9398. script_pushint(st,0);
  9399. return 0;
  9400. }
  9401. /*==========================================
  9402. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9403. *------------------------------------------*/
  9404. BUILDIN_FUNC(petskillbonus)
  9405. {
  9406. struct pet_data *pd;
  9407. TBL_PC *sd=script_rid2sd(st);
  9408. if(sd==NULL || sd->pd==NULL)
  9409. return 0;
  9410. pd=sd->pd;
  9411. if (pd->bonus)
  9412. { //Clear previous bonus
  9413. if (pd->bonus->timer != -1)
  9414. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9415. } else //init
  9416. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9417. pd->bonus->type=script_getnum(st,2);
  9418. pd->bonus->val=script_getnum(st,3);
  9419. pd->bonus->duration=script_getnum(st,4);
  9420. pd->bonus->delay=script_getnum(st,5);
  9421. if (pd->state.skillbonus == 1)
  9422. pd->state.skillbonus=0; // waiting state
  9423. // wait for timer to start
  9424. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9425. pd->bonus->timer = INVALID_TIMER;
  9426. else
  9427. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9428. return 0;
  9429. }
  9430. /*==========================================
  9431. * pet looting [Valaris] //Rewritten by [Skotlex]
  9432. *------------------------------------------*/
  9433. BUILDIN_FUNC(petloot)
  9434. {
  9435. int max;
  9436. struct pet_data *pd;
  9437. TBL_PC *sd=script_rid2sd(st);
  9438. if(sd==NULL || sd->pd==NULL)
  9439. return 0;
  9440. max=script_getnum(st,2);
  9441. if(max < 1)
  9442. max = 1; //Let'em loot at least 1 item.
  9443. else if (max > MAX_PETLOOT_SIZE)
  9444. max = MAX_PETLOOT_SIZE;
  9445. pd = sd->pd;
  9446. if (pd->loot != NULL)
  9447. { //Release whatever was there already and reallocate memory
  9448. pet_lootitem_drop(pd, pd->msd);
  9449. aFree(pd->loot->item);
  9450. }
  9451. else
  9452. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9453. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9454. pd->loot->max=max;
  9455. pd->loot->count = 0;
  9456. pd->loot->weight = 0;
  9457. return 0;
  9458. }
  9459. /*==========================================
  9460. * PCの所持品情報読み取り
  9461. *------------------------------------------*/
  9462. BUILDIN_FUNC(getinventorylist)
  9463. {
  9464. TBL_PC *sd=script_rid2sd(st);
  9465. char card_var[NAME_LENGTH];
  9466. int i,j=0,k;
  9467. if(!sd) return 0;
  9468. for(i=0;i<MAX_INVENTORY;i++){
  9469. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9470. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9471. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9472. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9473. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9474. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9475. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9476. for (k = 0; k < MAX_SLOTS; k++)
  9477. {
  9478. sprintf(card_var, "@inventorylist_card%d",k+1);
  9479. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9480. }
  9481. pc_setreg(sd,add_str("@inventorylist_expire")+(j<<24),sd->status.inventory[i].expire_time);
  9482. j++;
  9483. }
  9484. }
  9485. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9486. return 0;
  9487. }
  9488. BUILDIN_FUNC(getskilllist)
  9489. {
  9490. TBL_PC *sd=script_rid2sd(st);
  9491. int i,j=0;
  9492. if(!sd) return 0;
  9493. for(i=0;i<MAX_SKILL;i++){
  9494. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9495. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9496. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9497. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9498. j++;
  9499. }
  9500. }
  9501. pc_setreg(sd,add_str("@skilllist_count"),j);
  9502. return 0;
  9503. }
  9504. BUILDIN_FUNC(clearitem)
  9505. {
  9506. TBL_PC *sd=script_rid2sd(st);
  9507. int i;
  9508. if(sd==NULL) return 0;
  9509. for (i=0; i<MAX_INVENTORY; i++) {
  9510. if (sd->status.inventory[i].amount) {
  9511. //Logs items, got from (N)PC scripts [Lupus]
  9512. if(log_config.enable_logs&0x40)
  9513. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9514. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0);
  9515. }
  9516. }
  9517. return 0;
  9518. }
  9519. /*==========================================
  9520. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9521. *------------------------------------------*/
  9522. BUILDIN_FUNC(disguise)
  9523. {
  9524. int id;
  9525. TBL_PC* sd = script_rid2sd(st);
  9526. if (sd == NULL) return 0;
  9527. id = script_getnum(st,2);
  9528. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9529. pc_disguise(sd, id);
  9530. script_pushint(st,id);
  9531. } else
  9532. script_pushint(st,0);
  9533. return 0;
  9534. }
  9535. /*==========================================
  9536. * Undisguise Player (returns 1 if success, 0 on fail)
  9537. *------------------------------------------*/
  9538. BUILDIN_FUNC(undisguise)
  9539. {
  9540. TBL_PC* sd = script_rid2sd(st);
  9541. if (sd == NULL) return 0;
  9542. if (sd->disguise) {
  9543. pc_disguise(sd, 0);
  9544. script_pushint(st,0);
  9545. } else {
  9546. script_pushint(st,1);
  9547. }
  9548. return 0;
  9549. }
  9550. /*==========================================
  9551. * NPCクラスチェンジ
  9552. * classは変わりたいclass
  9553. * typeは通常0なのかな?
  9554. *------------------------------------------*/
  9555. BUILDIN_FUNC(classchange)
  9556. {
  9557. int _class,type;
  9558. struct block_list *bl=map_id2bl(st->oid);
  9559. if(bl==NULL) return 0;
  9560. _class=script_getnum(st,2);
  9561. type=script_getnum(st,3);
  9562. clif_class_change(bl,_class,type);
  9563. return 0;
  9564. }
  9565. /*==========================================
  9566. * NPCから発生するエフェクト
  9567. *------------------------------------------*/
  9568. BUILDIN_FUNC(misceffect)
  9569. {
  9570. int type;
  9571. type=script_getnum(st,2);
  9572. if(st->oid && st->oid != fake_nd->bl.id) {
  9573. struct block_list *bl = map_id2bl(st->oid);
  9574. if (bl)
  9575. clif_specialeffect(bl,type,AREA);
  9576. } else{
  9577. TBL_PC *sd=script_rid2sd(st);
  9578. if(sd)
  9579. clif_specialeffect(&sd->bl,type,AREA);
  9580. }
  9581. return 0;
  9582. }
  9583. /*==========================================
  9584. * Play a BGM on a single client [Rikter/Yommy]
  9585. *------------------------------------------*/
  9586. BUILDIN_FUNC(playBGM)
  9587. {
  9588. const char* name;
  9589. struct map_session_data* sd;
  9590. if( ( sd = script_rid2sd(st) ) != NULL )
  9591. {
  9592. name = script_getstr(st,2);
  9593. clif_playBGM(sd, name);
  9594. }
  9595. return 0;
  9596. }
  9597. static int playBGM_sub(struct block_list* bl,va_list ap)
  9598. {
  9599. const char* name = va_arg(ap,const char*);
  9600. clif_playBGM(BL_CAST(BL_PC, bl), name);
  9601. return 0;
  9602. }
  9603. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  9604. {
  9605. const char* name = va_arg(args, const char*);
  9606. clif_playBGM(sd, name);
  9607. return 0;
  9608. }
  9609. /*==========================================
  9610. * Play a BGM on multiple client [Rikter/Yommy]
  9611. *------------------------------------------*/
  9612. BUILDIN_FUNC(playBGMall)
  9613. {
  9614. const char* name;
  9615. name = script_getstr(st,2);
  9616. if( script_hasdata(st,7) )
  9617. {// specified part of map
  9618. const char* map = script_getstr(st,3);
  9619. int x0 = script_getnum(st,4);
  9620. int y0 = script_getnum(st,5);
  9621. int x1 = script_getnum(st,6);
  9622. int y1 = script_getnum(st,7);
  9623. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  9624. }
  9625. else if( script_hasdata(st,3) )
  9626. {// entire map
  9627. const char* map = script_getstr(st,3);
  9628. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  9629. }
  9630. else
  9631. {// entire server
  9632. map_foreachpc(&playBGM_foreachpc_sub, name);
  9633. }
  9634. return 0;
  9635. }
  9636. /*==========================================
  9637. * サウンドエフェクト
  9638. *------------------------------------------*/
  9639. BUILDIN_FUNC(soundeffect)
  9640. {
  9641. TBL_PC* sd = script_rid2sd(st);
  9642. const char* name = script_getstr(st,2);
  9643. int type = script_getnum(st,3);
  9644. if(sd)
  9645. {
  9646. clif_soundeffect(sd,&sd->bl,name,type);
  9647. }
  9648. return 0;
  9649. }
  9650. int soundeffect_sub(struct block_list* bl,va_list ap)
  9651. {
  9652. char* name = va_arg(ap,char*);
  9653. int type = va_arg(ap,int);
  9654. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9655. return 0;
  9656. }
  9657. /*==========================================
  9658. * Play a sound effect (.wav) on multiple clients
  9659. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9660. *------------------------------------------*/
  9661. BUILDIN_FUNC(soundeffectall)
  9662. {
  9663. struct block_list* bl;
  9664. const char* name;
  9665. int type;
  9666. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9667. if (!bl)
  9668. return 0;
  9669. name = script_getstr(st,2);
  9670. type = script_getnum(st,3);
  9671. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9672. if(!script_hasdata(st,4))
  9673. { // area around
  9674. clif_soundeffectall(bl, name, type, AREA);
  9675. }
  9676. else
  9677. if(!script_hasdata(st,5))
  9678. { // entire map
  9679. const char* map = script_getstr(st,4);
  9680. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9681. }
  9682. else
  9683. if(script_hasdata(st,8))
  9684. { // specified part of map
  9685. const char* map = script_getstr(st,4);
  9686. int x0 = script_getnum(st,5);
  9687. int y0 = script_getnum(st,6);
  9688. int x1 = script_getnum(st,7);
  9689. int y1 = script_getnum(st,8);
  9690. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9691. }
  9692. else
  9693. {
  9694. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9695. }
  9696. return 0;
  9697. }
  9698. /*==========================================
  9699. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9700. *------------------------------------------*/
  9701. BUILDIN_FUNC(petrecovery)
  9702. {
  9703. struct pet_data *pd;
  9704. TBL_PC *sd=script_rid2sd(st);
  9705. if(sd==NULL || sd->pd==NULL)
  9706. return 0;
  9707. pd=sd->pd;
  9708. if (pd->recovery)
  9709. { //Halt previous bonus
  9710. if (pd->recovery->timer != -1)
  9711. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9712. } else //Init
  9713. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9714. pd->recovery->type = (sc_type)script_getnum(st,2);
  9715. pd->recovery->delay = script_getnum(st,3);
  9716. pd->recovery->timer = INVALID_TIMER;
  9717. return 0;
  9718. }
  9719. /*==========================================
  9720. * pet healing [Valaris] //Rewritten by [Skotlex]
  9721. *------------------------------------------*/
  9722. BUILDIN_FUNC(petheal)
  9723. {
  9724. struct pet_data *pd;
  9725. TBL_PC *sd=script_rid2sd(st);
  9726. if(sd==NULL || sd->pd==NULL)
  9727. return 0;
  9728. pd=sd->pd;
  9729. if (pd->s_skill)
  9730. { //Clear previous skill
  9731. if (pd->s_skill->timer != -1)
  9732. {
  9733. if (pd->s_skill->id)
  9734. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9735. else
  9736. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9737. }
  9738. } else //init memory
  9739. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9740. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9741. //Use the lv as the amount to heal
  9742. pd->s_skill->lv=script_getnum(st,2);
  9743. pd->s_skill->delay=script_getnum(st,3);
  9744. pd->s_skill->hp=script_getnum(st,4);
  9745. pd->s_skill->sp=script_getnum(st,5);
  9746. //Use delay as initial offset to avoid skill/heal exploits
  9747. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9748. pd->s_skill->timer = INVALID_TIMER;
  9749. else
  9750. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9751. return 0;
  9752. }
  9753. /*==========================================
  9754. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9755. *------------------------------------------*/
  9756. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  9757. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  9758. BUILDIN_FUNC(petskillattack)
  9759. {
  9760. struct pet_data *pd;
  9761. TBL_PC *sd=script_rid2sd(st);
  9762. if(sd==NULL || sd->pd==NULL)
  9763. return 0;
  9764. pd=sd->pd;
  9765. if (pd->a_skill == NULL)
  9766. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9767. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9768. pd->a_skill->lv=script_getnum(st,3);
  9769. pd->a_skill->div_ = 0;
  9770. pd->a_skill->rate=script_getnum(st,4);
  9771. pd->a_skill->bonusrate=script_getnum(st,5);
  9772. return 0;
  9773. }
  9774. /*==========================================
  9775. * pet attack skills [Valaris]
  9776. *------------------------------------------*/
  9777. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  9778. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  9779. BUILDIN_FUNC(petskillattack2)
  9780. {
  9781. struct pet_data *pd;
  9782. TBL_PC *sd=script_rid2sd(st);
  9783. if(sd==NULL || sd->pd==NULL)
  9784. return 0;
  9785. pd=sd->pd;
  9786. if (pd->a_skill == NULL)
  9787. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9788. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9789. pd->a_skill->lv=script_getnum(st,3);
  9790. pd->a_skill->div_ = script_getnum(st,4);
  9791. pd->a_skill->rate=script_getnum(st,5);
  9792. pd->a_skill->bonusrate=script_getnum(st,6);
  9793. return 0;
  9794. }
  9795. /*==========================================
  9796. * pet support skills [Skotlex]
  9797. *------------------------------------------*/
  9798. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  9799. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  9800. BUILDIN_FUNC(petskillsupport)
  9801. {
  9802. struct pet_data *pd;
  9803. TBL_PC *sd=script_rid2sd(st);
  9804. if(sd==NULL || sd->pd==NULL)
  9805. return 0;
  9806. pd=sd->pd;
  9807. if (pd->s_skill)
  9808. { //Clear previous skill
  9809. if (pd->s_skill->timer != -1)
  9810. {
  9811. if (pd->s_skill->id)
  9812. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9813. else
  9814. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9815. }
  9816. } else //init memory
  9817. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9818. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9819. pd->s_skill->lv=script_getnum(st,3);
  9820. pd->s_skill->delay=script_getnum(st,4);
  9821. pd->s_skill->hp=script_getnum(st,5);
  9822. pd->s_skill->sp=script_getnum(st,6);
  9823. //Use delay as initial offset to avoid skill/heal exploits
  9824. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9825. pd->s_skill->timer = INVALID_TIMER;
  9826. else
  9827. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9828. return 0;
  9829. }
  9830. /*==========================================
  9831. * Scripted skill effects [Celest]
  9832. *------------------------------------------*/
  9833. /// skilleffect <skill id>,<level>
  9834. /// skilleffect "<skill name>",<level>
  9835. BUILDIN_FUNC(skilleffect)
  9836. {
  9837. TBL_PC *sd;
  9838. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9839. int skilllv=script_getnum(st,3);
  9840. sd=script_rid2sd(st);
  9841. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9842. return 0;
  9843. }
  9844. /*==========================================
  9845. * NPC skill effects [Valaris]
  9846. *------------------------------------------*/
  9847. /// npcskilleffect <skill id>,<level>,<x>,<y>
  9848. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  9849. BUILDIN_FUNC(npcskilleffect)
  9850. {
  9851. struct block_list *bl= map_id2bl(st->oid);
  9852. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9853. int skilllv=script_getnum(st,3);
  9854. int x=script_getnum(st,4);
  9855. int y=script_getnum(st,5);
  9856. if (bl)
  9857. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9858. return 0;
  9859. }
  9860. /*==========================================
  9861. * Special effects [Valaris]
  9862. *------------------------------------------*/
  9863. BUILDIN_FUNC(specialeffect)
  9864. {
  9865. struct block_list *bl=map_id2bl(st->oid);
  9866. int type = script_getnum(st,2);
  9867. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9868. if(bl==NULL)
  9869. return 0;
  9870. if( script_hasdata(st,4) )
  9871. {
  9872. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9873. if(nd)
  9874. clif_specialeffect(&nd->bl, type, target);
  9875. }
  9876. else
  9877. clif_specialeffect(bl, type, target);
  9878. return 0;
  9879. }
  9880. BUILDIN_FUNC(specialeffect2)
  9881. {
  9882. TBL_PC *sd=script_rid2sd(st);
  9883. int type = script_getnum(st,2);
  9884. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9885. if( script_hasdata(st,4) )
  9886. sd = map_nick2sd(script_getstr(st,4));
  9887. if (sd)
  9888. clif_specialeffect(&sd->bl, type, target);
  9889. return 0;
  9890. }
  9891. /*==========================================
  9892. * Nude [Valaris]
  9893. *------------------------------------------*/
  9894. BUILDIN_FUNC(nude)
  9895. {
  9896. TBL_PC *sd=script_rid2sd(st);
  9897. int i,calcflag=0;
  9898. if(sd==NULL)
  9899. return 0;
  9900. for(i=0;i<11;i++)
  9901. if(sd->equip_index[i] >= 0) {
  9902. if(!calcflag)
  9903. calcflag=1;
  9904. pc_unequipitem(sd,sd->equip_index[i],2);
  9905. }
  9906. if(calcflag)
  9907. status_calc_pc(sd,0);
  9908. return 0;
  9909. }
  9910. /*==========================================
  9911. * gmcommand [MouseJstr]
  9912. *
  9913. * suggested on the forums...
  9914. * splitted into atcommand & charcommand by [Skotlex]
  9915. *------------------------------------------*/
  9916. BUILDIN_FUNC(atcommand)
  9917. {
  9918. TBL_PC dummy_sd;
  9919. TBL_PC* sd;
  9920. int fd;
  9921. const char* cmd;
  9922. cmd = script_getstr(st,2);
  9923. if (st->rid) {
  9924. sd = script_rid2sd(st);
  9925. fd = sd->fd;
  9926. } else { //Use a dummy character.
  9927. sd = &dummy_sd;
  9928. fd = 0;
  9929. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9930. if (st->oid)
  9931. {
  9932. struct block_list* bl = map_id2bl(st->oid);
  9933. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9934. if (bl->type == BL_NPC)
  9935. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9936. }
  9937. }
  9938. // compatibility with previous implementation (deprecated!)
  9939. if(cmd[0] != atcommand_symbol)
  9940. {
  9941. cmd += strlen(sd->status.name);
  9942. while(*cmd != atcommand_symbol && *cmd != 0)
  9943. cmd++;
  9944. }
  9945. is_atcommand(fd, sd, cmd, 0);
  9946. return 0;
  9947. }
  9948. BUILDIN_FUNC(charcommand)
  9949. {
  9950. TBL_PC dummy_sd;
  9951. TBL_PC* sd;
  9952. int fd;
  9953. const char* cmd;
  9954. cmd = script_getstr(st,2);
  9955. if (st->rid) {
  9956. sd = script_rid2sd(st);
  9957. fd = sd->fd;
  9958. } else { //Use a dummy character.
  9959. sd = &dummy_sd;
  9960. fd = 0;
  9961. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9962. if (st->oid)
  9963. {
  9964. struct block_list* bl = map_id2bl(st->oid);
  9965. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9966. if (bl->type == BL_NPC)
  9967. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9968. }
  9969. }
  9970. if (*cmd != charcommand_symbol) {
  9971. ShowWarning("script: buildin_charcommand: No '#' symbol!\n");
  9972. script_reportsrc(st);
  9973. return 1;
  9974. }
  9975. is_atcommand(fd, sd, cmd, 0);
  9976. return 0;
  9977. }
  9978. /*==========================================
  9979. * Displays a message for the player only (like system messages like "you got an apple" )
  9980. *------------------------------------------*/
  9981. BUILDIN_FUNC(dispbottom)
  9982. {
  9983. TBL_PC *sd=script_rid2sd(st);
  9984. const char *message;
  9985. message=script_getstr(st,2);
  9986. if(sd)
  9987. clif_disp_onlyself(sd,message,(int)strlen(message));
  9988. return 0;
  9989. }
  9990. /*==========================================
  9991. * All The Players Full Recovery
  9992. * (HP/SP full restore and resurrect if need)
  9993. *------------------------------------------*/
  9994. BUILDIN_FUNC(recovery)
  9995. {
  9996. TBL_PC* sd;
  9997. struct s_mapiterator* iter;
  9998. iter = mapit_getallusers();
  9999. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10000. {
  10001. if(pc_isdead(sd))
  10002. status_revive(&sd->bl, 100, 100);
  10003. else
  10004. status_percent_heal(&sd->bl, 100, 100);
  10005. clif_displaymessage(sd->fd,"You have been recovered!");
  10006. }
  10007. mapit_free(iter);
  10008. return 0;
  10009. }
  10010. /*==========================================
  10011. * Get your pet info: getpetinfo(n)
  10012. * n -> 0:pet_id 1:pet_class 2:pet_name
  10013. * 3:friendly 4:hungry, 5: rename flag.
  10014. *------------------------------------------*/
  10015. BUILDIN_FUNC(getpetinfo)
  10016. {
  10017. TBL_PC *sd=script_rid2sd(st);
  10018. TBL_PET *pd;
  10019. int type=script_getnum(st,2);
  10020. if(!sd || !sd->pd) {
  10021. if (type == 2)
  10022. script_pushconststr(st,"null");
  10023. else
  10024. script_pushint(st,0);
  10025. return 0;
  10026. }
  10027. pd = sd->pd;
  10028. switch(type){
  10029. case 0: script_pushint(st,pd->pet.pet_id); break;
  10030. case 1: script_pushint(st,pd->pet.class_); break;
  10031. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10032. case 3: script_pushint(st,pd->pet.intimate); break;
  10033. case 4: script_pushint(st,pd->pet.hungry); break;
  10034. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10035. default:
  10036. script_pushint(st,0);
  10037. break;
  10038. }
  10039. return 0;
  10040. }
  10041. /*==========================================
  10042. * Get your homunculus info: gethominfo(n)
  10043. * n -> 0:hom_id 1:class 2:name
  10044. * 3:friendly 4:hungry, 5: rename flag.
  10045. * 6: level
  10046. *------------------------------------------*/
  10047. BUILDIN_FUNC(gethominfo)
  10048. {
  10049. TBL_PC *sd=script_rid2sd(st);
  10050. TBL_HOM *hd;
  10051. int type=script_getnum(st,2);
  10052. hd = sd?sd->hd:NULL;
  10053. if(!merc_is_hom_active(hd))
  10054. {
  10055. if (type == 2)
  10056. script_pushconststr(st,"null");
  10057. else
  10058. script_pushint(st,0);
  10059. return 0;
  10060. }
  10061. switch(type){
  10062. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10063. case 1: script_pushint(st,hd->homunculus.class_); break;
  10064. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10065. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10066. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10067. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10068. case 6: script_pushint(st,hd->homunculus.level); break;
  10069. default:
  10070. script_pushint(st,0);
  10071. break;
  10072. }
  10073. return 0;
  10074. }
  10075. /*==========================================
  10076. * Shows wether your inventory(and equips) contain
  10077. selected card or not.
  10078. checkequipedcard(4001);
  10079. *------------------------------------------*/
  10080. BUILDIN_FUNC(checkequipedcard)
  10081. {
  10082. TBL_PC *sd=script_rid2sd(st);
  10083. int n,i,c=0;
  10084. c=script_getnum(st,2);
  10085. if(sd){
  10086. for(i=0;i<MAX_INVENTORY;i++){
  10087. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10088. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10089. continue;
  10090. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10091. if(sd->status.inventory[i].card[n]==c){
  10092. script_pushint(st,1);
  10093. return 0;
  10094. }
  10095. }
  10096. }
  10097. }
  10098. }
  10099. script_pushint(st,0);
  10100. return 0;
  10101. }
  10102. BUILDIN_FUNC(jump_zero)
  10103. {
  10104. int sel;
  10105. sel=script_getnum(st,2);
  10106. if(!sel) {
  10107. int pos;
  10108. if( !data_islabel(script_getdata(st,3)) ){
  10109. ShowError("script: jump_zero: not label !\n");
  10110. st->state=END;
  10111. return 1;
  10112. }
  10113. pos=script_getnum(st,3);
  10114. st->pos=pos;
  10115. st->state=GOTO;
  10116. // printf("script: jump_zero: jumpto : %d\n",pos);
  10117. } else {
  10118. // printf("script: jump_zero: fail\n");
  10119. }
  10120. return 0;
  10121. }
  10122. /*==========================================
  10123. * GetMapMobs
  10124. returns mob counts on a set map:
  10125. e.g. GetMapMobs("prontera")
  10126. use "this" - for player's map
  10127. *------------------------------------------*/
  10128. BUILDIN_FUNC(getmapmobs)
  10129. {
  10130. const char *str=NULL;
  10131. int m=-1,bx,by;
  10132. int count=0;
  10133. struct block_list *bl;
  10134. str=script_getstr(st,2);
  10135. if(strcmp(str,"this")==0){
  10136. TBL_PC *sd=script_rid2sd(st);
  10137. if(sd)
  10138. m=sd->bl.m;
  10139. else{
  10140. script_pushint(st,-1);
  10141. return 0;
  10142. }
  10143. }else
  10144. m=map_mapname2mapid(str);
  10145. if(m < 0){
  10146. script_pushint(st,-1);
  10147. return 0;
  10148. }
  10149. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  10150. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  10151. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  10152. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  10153. count++;
  10154. script_pushint(st,count);
  10155. return 0;
  10156. }
  10157. /*==========================================
  10158. * movenpc [MouseJstr]
  10159. *------------------------------------------*/
  10160. BUILDIN_FUNC(movenpc)
  10161. {
  10162. TBL_NPC *nd = NULL;
  10163. const char *npc;
  10164. int x,y;
  10165. npc = script_getstr(st,2);
  10166. x = script_getnum(st,3);
  10167. y = script_getnum(st,4);
  10168. if ((nd = npc_name2id(npc)) == NULL)
  10169. return -1;
  10170. npc_movenpc(nd, x, y);
  10171. return 0;
  10172. }
  10173. /*==========================================
  10174. * message [MouseJstr]
  10175. *------------------------------------------*/
  10176. BUILDIN_FUNC(message)
  10177. {
  10178. const char *msg,*player;
  10179. TBL_PC *pl_sd = NULL;
  10180. player = script_getstr(st,2);
  10181. msg = script_getstr(st,3);
  10182. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10183. return 0;
  10184. clif_displaymessage(pl_sd->fd, msg);
  10185. return 0;
  10186. }
  10187. /*==========================================
  10188. * npctalk (sends message to surrounding area)
  10189. *------------------------------------------*/
  10190. BUILDIN_FUNC(npctalk)
  10191. {
  10192. const char* str;
  10193. char message[255];
  10194. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10195. str = script_getstr(st,2);
  10196. if(nd) {
  10197. memcpy(message, nd->name, NAME_LENGTH);
  10198. strtok(message, "#"); // discard extra name identifier if present
  10199. strcat(message, " : ");
  10200. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  10201. clif_message(&(nd->bl), message);
  10202. }
  10203. return 0;
  10204. }
  10205. // change npc walkspeed [Valaris]
  10206. BUILDIN_FUNC(npcspeed)
  10207. {
  10208. struct npc_data* nd;
  10209. int speed;
  10210. speed = script_getnum(st,2);
  10211. nd =(struct npc_data *)map_id2bl(st->oid);
  10212. if( nd )
  10213. {
  10214. nd->speed = speed;
  10215. nd->ud.state.speed_changed = 1;
  10216. }
  10217. return 0;
  10218. }
  10219. // make an npc walk to a position [Valaris]
  10220. BUILDIN_FUNC(npcwalkto)
  10221. {
  10222. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10223. int x=0,y=0;
  10224. x=script_getnum(st,2);
  10225. y=script_getnum(st,3);
  10226. if(nd) {
  10227. unit_walktoxy(&nd->bl,x,y,0);
  10228. }
  10229. return 0;
  10230. }
  10231. // stop an npc's movement [Valaris]
  10232. BUILDIN_FUNC(npcstop)
  10233. {
  10234. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10235. if(nd) {
  10236. unit_stop_walking(&nd->bl,1|4);
  10237. }
  10238. return 0;
  10239. }
  10240. /*==========================================
  10241. * getlook char info. getlook(arg)
  10242. *------------------------------------------*/
  10243. BUILDIN_FUNC(getlook)
  10244. {
  10245. int type,val;
  10246. TBL_PC *sd;
  10247. sd=script_rid2sd(st);
  10248. type=script_getnum(st,2);
  10249. val=-1;
  10250. switch(type) {
  10251. case LOOK_HAIR: val=sd->status.hair; break; //1
  10252. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10253. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10254. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10255. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10256. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10257. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10258. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10259. case LOOK_SHOES: break; //9
  10260. }
  10261. script_pushint(st,val);
  10262. return 0;
  10263. }
  10264. /*==========================================
  10265. * get char save point. argument: 0- map name, 1- x, 2- y
  10266. *------------------------------------------*/
  10267. BUILDIN_FUNC(getsavepoint)
  10268. {
  10269. TBL_PC* sd;
  10270. int type;
  10271. sd = script_rid2sd(st);
  10272. if (sd == NULL) {
  10273. script_pushint(st,0);
  10274. return 0;
  10275. }
  10276. type = script_getnum(st,2);
  10277. switch(type) {
  10278. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10279. case 1: script_pushint(st,sd->status.save_point.x); break;
  10280. case 2: script_pushint(st,sd->status.save_point.y); break;
  10281. default:
  10282. script_pushint(st,0);
  10283. break;
  10284. }
  10285. return 0;
  10286. }
  10287. /*==========================================
  10288. * Get position for char/npc/pet/mob objects. Added by Lorky
  10289. *
  10290. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10291. * where type:
  10292. * MapName$ - String variable for output map name
  10293. * MapX - Integer variable for output coord X
  10294. * MapY - Integer variable for output coord Y
  10295. * type - type of object
  10296. * 0 - Character coord
  10297. * 1 - NPC coord
  10298. * 2 - Pet coord
  10299. * 3 - Mob coord (not released)
  10300. * 4 - Homun coord
  10301. * CharName$ - Name object. If miss or "this" the current object
  10302. *
  10303. * Return:
  10304. * 0 - success
  10305. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10306. *------------------------------------------*/
  10307. BUILDIN_FUNC(getmapxy)
  10308. {
  10309. struct block_list *bl = NULL;
  10310. TBL_PC *sd=NULL;
  10311. int num;
  10312. const char *name;
  10313. char prefix;
  10314. int x,y,type;
  10315. char mapname[MAP_NAME_LENGTH];
  10316. if( !data_isreference(script_getdata(st,2)) ){
  10317. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10318. script_pushint(st,-1);
  10319. return 1;
  10320. }
  10321. if( !data_isreference(script_getdata(st,3)) ){
  10322. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10323. script_pushint(st,-1);
  10324. return 1;
  10325. }
  10326. if( !data_isreference(script_getdata(st,4)) ){
  10327. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10328. script_pushint(st,-1);
  10329. return 1;
  10330. }
  10331. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10332. type=script_getnum(st,5);
  10333. switch (type){
  10334. case 0: //Get Character Position
  10335. if( script_hasdata(st,6) )
  10336. sd=map_nick2sd(script_getstr(st,6));
  10337. else
  10338. sd=script_rid2sd(st);
  10339. if (sd)
  10340. bl = &sd->bl;
  10341. break;
  10342. case 1: //Get NPC Position
  10343. if( script_hasdata(st,6) )
  10344. {
  10345. struct npc_data *nd;
  10346. nd=npc_name2id(script_getstr(st,6));
  10347. if (nd)
  10348. bl = &nd->bl;
  10349. } else //In case the origin is not an npc?
  10350. bl=map_id2bl(st->oid);
  10351. break;
  10352. case 2: //Get Pet Position
  10353. if(script_hasdata(st,6))
  10354. sd=map_nick2sd(script_getstr(st,6));
  10355. else
  10356. sd=script_rid2sd(st);
  10357. if (sd && sd->pd)
  10358. bl = &sd->pd->bl;
  10359. break;
  10360. case 3: //Get Mob Position
  10361. break; //Not supported?
  10362. case 4: //Get Homun Position
  10363. if(script_hasdata(st,6))
  10364. sd=map_nick2sd(script_getstr(st,6));
  10365. else
  10366. sd=script_rid2sd(st);
  10367. if (sd && sd->hd)
  10368. bl = &sd->hd->bl;
  10369. break;
  10370. default:
  10371. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  10372. script_pushint(st,-1);
  10373. return 1;
  10374. }
  10375. if (!bl) { //No object found.
  10376. script_pushint(st,-1);
  10377. return 0;
  10378. }
  10379. x= bl->x;
  10380. y= bl->y;
  10381. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10382. //Set MapName$
  10383. num=st->stack->stack_data[st->start+2].u.num;
  10384. name=get_str(num&0x00ffffff);
  10385. prefix=*name;
  10386. if(not_server_variable(prefix))
  10387. sd=script_rid2sd(st);
  10388. else
  10389. sd=NULL;
  10390. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10391. //Set MapX
  10392. num=st->stack->stack_data[st->start+3].u.num;
  10393. name=get_str(num&0x00ffffff);
  10394. prefix=*name;
  10395. if(not_server_variable(prefix))
  10396. sd=script_rid2sd(st);
  10397. else
  10398. sd=NULL;
  10399. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10400. //Set MapY
  10401. num=st->stack->stack_data[st->start+4].u.num;
  10402. name=get_str(num&0x00ffffff);
  10403. prefix=*name;
  10404. if(not_server_variable(prefix))
  10405. sd=script_rid2sd(st);
  10406. else
  10407. sd=NULL;
  10408. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10409. //Return Success value
  10410. script_pushint(st,0);
  10411. return 0;
  10412. }
  10413. /*==========================================
  10414. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10415. *------------------------------------------*/
  10416. BUILDIN_FUNC(logmes)
  10417. {
  10418. const char *str;
  10419. TBL_PC* sd;
  10420. if( log_config.npc <= 0 )
  10421. return 0;
  10422. sd = script_rid2sd(st);
  10423. if( sd == NULL )
  10424. return 1;
  10425. str = script_getstr(st,2);
  10426. log_npc(sd,str);
  10427. return 0;
  10428. }
  10429. BUILDIN_FUNC(summon)
  10430. {
  10431. int _class, timeout=0;
  10432. const char *str,*event="";
  10433. TBL_PC *sd;
  10434. struct mob_data *md;
  10435. int tick = gettick();
  10436. sd=script_rid2sd(st);
  10437. if (!sd) return 0;
  10438. str =script_getstr(st,2);
  10439. _class=script_getnum(st,3);
  10440. if( script_hasdata(st,4) )
  10441. timeout=script_getnum(st,4);
  10442. if( script_hasdata(st,5) ){
  10443. event=script_getstr(st,5);
  10444. check_event(st, event);
  10445. }
  10446. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10447. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10448. if (md) {
  10449. md->master_id=sd->bl.id;
  10450. md->special_state.ai=1;
  10451. if( md->deletetimer != INVALID_TIMER )
  10452. delete_timer(md->deletetimer, mob_timer_delete);
  10453. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10454. mob_spawn (md); //Now it is ready for spawning.
  10455. clif_specialeffect(&md->bl,344,AREA);
  10456. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10457. }
  10458. return 0;
  10459. }
  10460. /*==========================================
  10461. * Checks whether it is daytime/nighttime
  10462. *------------------------------------------*/
  10463. BUILDIN_FUNC(isnight)
  10464. {
  10465. script_pushint(st,(night_flag == 1));
  10466. return 0;
  10467. }
  10468. BUILDIN_FUNC(isday)
  10469. {
  10470. script_pushint(st,(night_flag == 0));
  10471. return 0;
  10472. }
  10473. /*================================================
  10474. * Check how many items/cards in the list are
  10475. * equipped - used for 2/15's cards patch [celest]
  10476. *------------------------------------------------*/
  10477. BUILDIN_FUNC(isequippedcnt)
  10478. {
  10479. TBL_PC *sd;
  10480. int i, j, k, id = 1;
  10481. int ret = 0;
  10482. sd = script_rid2sd(st);
  10483. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10484. script_pushint(st,0);
  10485. return 0;
  10486. }
  10487. for (i=0; id!=0; i++) {
  10488. FETCH (i+2, id) else id = 0;
  10489. if (id <= 0)
  10490. continue;
  10491. for (j=0; j<EQI_MAX; j++) {
  10492. int index;
  10493. index = sd->equip_index[j];
  10494. if(index < 0) continue;
  10495. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10496. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10497. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10498. if(!sd->inventory_data[index])
  10499. continue;
  10500. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10501. if (sd->inventory_data[index]->nameid == id)
  10502. ret+= sd->status.inventory[index].amount;
  10503. } else { //Count cards.
  10504. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10505. continue; //No cards
  10506. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10507. if (sd->status.inventory[index].card[k] == id)
  10508. ret++; //[Lupus]
  10509. }
  10510. }
  10511. }
  10512. }
  10513. script_pushint(st,ret);
  10514. return 0;
  10515. }
  10516. /*================================================
  10517. * Check whether another card has been
  10518. * equipped - used for 2/15's cards patch [celest]
  10519. * -- Items checked cannot be reused in another
  10520. * card set to prevent exploits
  10521. *------------------------------------------------*/
  10522. BUILDIN_FUNC(isequipped)
  10523. {
  10524. TBL_PC *sd;
  10525. int i, j, k, id = 1;
  10526. int index, flag;
  10527. int ret = -1;
  10528. //Original hash to reverse it when full check fails.
  10529. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10530. sd = script_rid2sd(st);
  10531. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10532. script_pushint(st,0);
  10533. return 0;
  10534. }
  10535. setitem_hash = sd->setitem_hash;
  10536. setitem_hash2 = sd->setitem_hash2;
  10537. for (i=0; id!=0; i++)
  10538. {
  10539. FETCH (i+2, id) else id = 0;
  10540. if (id <= 0)
  10541. continue;
  10542. flag = 0;
  10543. for (j=0; j<EQI_MAX; j++)
  10544. {
  10545. index = sd->equip_index[j];
  10546. if(index < 0) continue;
  10547. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10548. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10549. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10550. if(!sd->inventory_data[index])
  10551. continue;
  10552. if (itemdb_type(id) != IT_CARD) {
  10553. if (sd->inventory_data[index]->nameid != id)
  10554. continue;
  10555. flag = 1;
  10556. break;
  10557. } else { //Cards
  10558. if (sd->inventory_data[index]->slot == 0 ||
  10559. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10560. continue;
  10561. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10562. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10563. unsigned int hash = 0;
  10564. if (sd->status.inventory[index].card[k] != id)
  10565. continue;
  10566. hash = 1<<((j<5?j:j-5)*4 + k);
  10567. // check if card is already used by another set
  10568. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10569. continue;
  10570. // We have found a match
  10571. flag = 1;
  10572. // Set hash so this card cannot be used by another
  10573. if (j<5)
  10574. sd->setitem_hash |= hash;
  10575. else
  10576. sd->setitem_hash2 |= hash;
  10577. break;
  10578. }
  10579. }
  10580. if (flag) break; //Card found
  10581. }
  10582. if (ret == -1)
  10583. ret = flag;
  10584. else
  10585. ret &= flag;
  10586. if (!ret) break;
  10587. }
  10588. if (!ret)
  10589. { //When check fails, restore original hash values. [Skotlex]
  10590. sd->setitem_hash = setitem_hash;
  10591. sd->setitem_hash2 = setitem_hash2;
  10592. }
  10593. script_pushint(st,ret);
  10594. return 0;
  10595. }
  10596. /*================================================
  10597. * Check how many given inserted cards in the CURRENT
  10598. * weapon - used for 2/15's cards patch [Lupus]
  10599. *------------------------------------------------*/
  10600. BUILDIN_FUNC(cardscnt)
  10601. {
  10602. TBL_PC *sd;
  10603. int i, k, id = 1;
  10604. int ret = 0;
  10605. int index;
  10606. sd = script_rid2sd(st);
  10607. for (i=0; id!=0; i++) {
  10608. FETCH (i+2, id) else id = 0;
  10609. if (id <= 0)
  10610. continue;
  10611. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10612. if(index < 0) continue;
  10613. if(!sd->inventory_data[index])
  10614. continue;
  10615. if(itemdb_type(id) != IT_CARD) {
  10616. if (sd->inventory_data[index]->nameid == id)
  10617. ret+= sd->status.inventory[index].amount;
  10618. } else {
  10619. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10620. continue;
  10621. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10622. if (sd->status.inventory[index].card[k] == id)
  10623. ret++;
  10624. }
  10625. }
  10626. }
  10627. script_pushint(st,ret);
  10628. // script_pushint(st,current_equip_item_index);
  10629. return 0;
  10630. }
  10631. /*=======================================================
  10632. * Returns the refined number of the current item, or an
  10633. * item with inventory index specified
  10634. *-------------------------------------------------------*/
  10635. BUILDIN_FUNC(getrefine)
  10636. {
  10637. TBL_PC *sd;
  10638. if ((sd = script_rid2sd(st))!= NULL)
  10639. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10640. else
  10641. script_pushint(st,0);
  10642. return 0;
  10643. }
  10644. /*=======================================================
  10645. * Day/Night controls
  10646. *-------------------------------------------------------*/
  10647. BUILDIN_FUNC(night)
  10648. {
  10649. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10650. return 0;
  10651. }
  10652. BUILDIN_FUNC(day)
  10653. {
  10654. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10655. return 0;
  10656. }
  10657. //=======================================================
  10658. // Unequip [Spectre]
  10659. //-------------------------------------------------------
  10660. BUILDIN_FUNC(unequip)
  10661. {
  10662. int i;
  10663. size_t num;
  10664. TBL_PC *sd;
  10665. num = script_getnum(st,2);
  10666. sd = script_rid2sd(st);
  10667. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10668. {
  10669. i = pc_checkequip(sd,equip[num-1]);
  10670. if (i >= 0)
  10671. pc_unequipitem(sd,i,1|2);
  10672. }
  10673. return 0;
  10674. }
  10675. BUILDIN_FUNC(equip)
  10676. {
  10677. int nameid=0,i;
  10678. TBL_PC *sd;
  10679. struct item_data *item_data;
  10680. sd = script_rid2sd(st);
  10681. nameid=script_getnum(st,2);
  10682. if((item_data = itemdb_exists(nameid)) == NULL)
  10683. {
  10684. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10685. return 1;
  10686. }
  10687. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10688. if( i < MAX_INVENTORY )
  10689. pc_equipitem(sd,i,item_data->equip);
  10690. return 0;
  10691. }
  10692. BUILDIN_FUNC(autoequip)
  10693. {
  10694. int nameid, flag;
  10695. struct item_data *item_data;
  10696. nameid=script_getnum(st,2);
  10697. flag=script_getnum(st,3);
  10698. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10699. item_data->flag.autoequip = flag>0?1:0;
  10700. }
  10701. return 0;
  10702. }
  10703. BUILDIN_FUNC(setbattleflag)
  10704. {
  10705. const char *flag, *value;
  10706. flag = script_getstr(st,2);
  10707. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  10708. if (battle_set_value(flag, value) == 0)
  10709. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10710. else
  10711. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10712. return 0;
  10713. }
  10714. BUILDIN_FUNC(getbattleflag)
  10715. {
  10716. const char *flag;
  10717. flag = script_getstr(st,2);
  10718. script_pushint(st,battle_get_value(flag));
  10719. return 0;
  10720. }
  10721. //=======================================================
  10722. // strlen [Valaris]
  10723. //-------------------------------------------------------
  10724. BUILDIN_FUNC(getstrlen)
  10725. {
  10726. const char *str = script_getstr(st,2);
  10727. int len = (str) ? (int)strlen(str) : 0;
  10728. script_pushint(st,len);
  10729. return 0;
  10730. }
  10731. //=======================================================
  10732. // isalpha [Valaris]
  10733. //-------------------------------------------------------
  10734. BUILDIN_FUNC(charisalpha)
  10735. {
  10736. const char *str=script_getstr(st,2);
  10737. int pos=script_getnum(st,3);
  10738. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10739. script_pushint(st,val);
  10740. return 0;
  10741. }
  10742. /// Changes the display name and/or display class of the npc.
  10743. /// Returns 0 is successful, 1 if the npc does not exist.
  10744. ///
  10745. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  10746. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10747. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10748. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10749. BUILDIN_FUNC(setnpcdisplay)
  10750. {
  10751. const char* name;
  10752. const char* newname = NULL;
  10753. int class_ = -1, size = -1;
  10754. struct script_data* data;
  10755. struct npc_data* nd;
  10756. name = script_getstr(st,2);
  10757. data = script_getdata(st,3);
  10758. if( script_hasdata(st,4) )
  10759. class_ = script_getnum(st,4);
  10760. if( script_hasdata(st,5) )
  10761. size = script_getnum(st,5);
  10762. get_val(st, data);
  10763. if( data_isstring(data) )
  10764. newname = conv_str(st,data);
  10765. else if( data_isint(data) )
  10766. class_ = conv_num(st,data);
  10767. else
  10768. {
  10769. ShowError("script:setnpcdisplay: expected a string or number\n");
  10770. script_reportdata(data);
  10771. return 1;
  10772. }
  10773. nd = npc_name2id(name);
  10774. if( nd == NULL )
  10775. {// not found
  10776. script_pushint(st,1);
  10777. return 0;
  10778. }
  10779. // update npc
  10780. if( newname )
  10781. npc_setdisplayname(nd, newname);
  10782. if( size != -1 && size != (int)nd->size )
  10783. nd->size = size;
  10784. else
  10785. size = -1;
  10786. if( class_ != -1 && nd->class_ != class_ )
  10787. npc_setclass(nd, class_);
  10788. else if( size != -1 )
  10789. { // Required to update the visual size
  10790. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  10791. clif_spawn(&nd->bl);
  10792. }
  10793. script_pushint(st,0);
  10794. return 0;
  10795. }
  10796. BUILDIN_FUNC(atoi)
  10797. {
  10798. const char *value;
  10799. value = script_getstr(st,2);
  10800. script_pushint(st,atoi(value));
  10801. return 0;
  10802. }
  10803. // case-insensitive substring search [lordalfa]
  10804. BUILDIN_FUNC(compare)
  10805. {
  10806. const char *message;
  10807. const char *cmpstring;
  10808. message = script_getstr(st,2);
  10809. cmpstring = script_getstr(st,3);
  10810. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10811. return 0;
  10812. }
  10813. // [zBuffer] List of mathematics commands --->
  10814. BUILDIN_FUNC(sqrt)
  10815. {
  10816. double i, a;
  10817. i = script_getnum(st,2);
  10818. a = sqrt(i);
  10819. script_pushint(st,(int)a);
  10820. return 0;
  10821. }
  10822. BUILDIN_FUNC(pow)
  10823. {
  10824. double i, a, b;
  10825. a = script_getnum(st,2);
  10826. b = script_getnum(st,3);
  10827. i = pow(a,b);
  10828. script_pushint(st,(int)i);
  10829. return 0;
  10830. }
  10831. BUILDIN_FUNC(distance)
  10832. {
  10833. int x0, y0, x1, y1;
  10834. x0 = script_getnum(st,2);
  10835. y0 = script_getnum(st,3);
  10836. x1 = script_getnum(st,4);
  10837. y1 = script_getnum(st,5);
  10838. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10839. return 0;
  10840. }
  10841. // <--- [zBuffer] List of mathematics commands
  10842. BUILDIN_FUNC(md5)
  10843. {
  10844. const char *tmpstr;
  10845. char *md5str;
  10846. tmpstr = script_getstr(st,2);
  10847. md5str = (char *)aMallocA((32+1)*sizeof(char));
  10848. MD5_String(tmpstr, md5str);
  10849. script_pushstr(st, md5str);
  10850. return 0;
  10851. }
  10852. // [zBuffer] List of dynamic var commands --->
  10853. BUILDIN_FUNC(setd)
  10854. {
  10855. TBL_PC *sd=NULL;
  10856. char varname[100];
  10857. const char *buffer;
  10858. int elem;
  10859. buffer = script_getstr(st, 2);
  10860. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10861. elem = 0;
  10862. if( not_server_variable(*varname) )
  10863. {
  10864. sd = script_rid2sd(st);
  10865. if( sd == NULL )
  10866. {
  10867. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10868. return 0;
  10869. }
  10870. }
  10871. if( is_string_variable(varname) ) {
  10872. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  10873. } else {
  10874. setd_sub(st, sd, varname, elem, (void *)script_getnum(st, 3), NULL);
  10875. }
  10876. return 0;
  10877. }
  10878. #ifndef TXT_ONLY
  10879. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10880. {
  10881. int i, j;
  10882. TBL_PC* sd = NULL;
  10883. const char* query;
  10884. struct script_data* data;
  10885. const char* name;
  10886. int max_rows = SCRIPT_MAX_ARRAYSIZE;// maximum number of rows
  10887. int num_vars;
  10888. int num_cols;
  10889. // check target variables
  10890. for( i = 3; script_hasdata(st,i); ++i )
  10891. {
  10892. data = script_getdata(st, i);
  10893. if( data_isreference(data) && reference_tovariable(data) )
  10894. {// it's a variable
  10895. name = reference_getname(data);
  10896. if( not_server_variable(*name) && sd == NULL )
  10897. {// requires a player
  10898. sd = script_rid2sd(st);
  10899. if( sd == NULL )
  10900. {// no player attached
  10901. script_reportdata(data);
  10902. st->state = END;
  10903. return 1;
  10904. }
  10905. }
  10906. if( not_array_variable(*name) )
  10907. max_rows = 1;// not an array, limit to one row
  10908. }
  10909. else
  10910. {
  10911. ShowError("script:query_sql: not a variable\n");
  10912. script_reportdata(data);
  10913. st->state = END;
  10914. return 1;
  10915. }
  10916. }
  10917. num_vars = i - 3;
  10918. // Execute the query
  10919. query = script_getstr(st,2);
  10920. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10921. {
  10922. Sql_ShowDebug(handle);
  10923. script_pushint(st, 0);
  10924. return 1;
  10925. }
  10926. if( Sql_NumRows(handle) == 0 )
  10927. {// No data received
  10928. Sql_FreeResult(handle);
  10929. script_pushint(st, 0);
  10930. return 0;
  10931. }
  10932. // Count the number of columns to store
  10933. num_cols = Sql_NumColumns(handle);
  10934. if( num_vars < num_cols )
  10935. {
  10936. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10937. script_reportsrc(st);
  10938. }
  10939. else if( num_vars > num_cols )
  10940. {
  10941. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10942. script_reportsrc(st);
  10943. }
  10944. // Store data
  10945. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10946. {
  10947. for( j = 0; j < num_vars; ++j )
  10948. {
  10949. char* str = NULL;
  10950. if( j < num_cols )
  10951. Sql_GetData(handle, j, &str, NULL);
  10952. data = script_getdata(st, j+3);
  10953. name = reference_getname(data);
  10954. if( is_string_variable(name) )
  10955. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10956. else
  10957. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10958. }
  10959. }
  10960. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10961. {
  10962. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10963. script_reportsrc(st);
  10964. }
  10965. // Free data
  10966. Sql_FreeResult(handle);
  10967. script_pushint(st, i);
  10968. return 0;
  10969. }
  10970. #endif
  10971. BUILDIN_FUNC(query_sql)
  10972. {
  10973. #ifndef TXT_ONLY
  10974. return buildin_query_sql_sub(st, mmysql_handle);
  10975. #else
  10976. //for TXT version, we always return -1
  10977. script_pushint(st,-1);
  10978. return 0;
  10979. #endif
  10980. }
  10981. BUILDIN_FUNC(query_logsql)
  10982. {
  10983. #ifndef TXT_ONLY
  10984. return buildin_query_sql_sub(st, logmysql_handle);
  10985. #else
  10986. //for TXT version, we always return -1
  10987. script_pushint(st,-1);
  10988. return 0;
  10989. #endif
  10990. }
  10991. //Allows escaping of a given string.
  10992. BUILDIN_FUNC(escape_sql)
  10993. {
  10994. const char *str;
  10995. char *esc_str;
  10996. size_t len;
  10997. str = script_getstr(st,2);
  10998. len = strlen(str);
  10999. esc_str = (char*)aMallocA(len*2+1);
  11000. #if defined(TXT_ONLY)
  11001. jstrescapecpy(esc_str, str);
  11002. #else
  11003. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  11004. #endif
  11005. script_pushstr(st, esc_str);
  11006. return 0;
  11007. }
  11008. BUILDIN_FUNC(getd)
  11009. {
  11010. char varname[100];
  11011. const char *buffer;
  11012. int elem;
  11013. buffer = script_getstr(st, 2);
  11014. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  11015. elem = 0;
  11016. // Push the 'pointer' so it's more flexible [Lance]
  11017. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  11018. return 0;
  11019. }
  11020. // <--- [zBuffer] List of dynamic var commands
  11021. // Pet stat [Lance]
  11022. BUILDIN_FUNC(petstat)
  11023. {
  11024. TBL_PC *sd = NULL;
  11025. struct pet_data *pd;
  11026. int flag = script_getnum(st,2);
  11027. sd = script_rid2sd(st);
  11028. if(!sd || !sd->status.pet_id || !sd->pd){
  11029. if(flag == 2)
  11030. script_pushconststr(st, "");
  11031. else
  11032. script_pushint(st,0);
  11033. return 0;
  11034. }
  11035. pd = sd->pd;
  11036. switch(flag){
  11037. case 1: script_pushint(st,(int)pd->pet.class_); break;
  11038. case 2: script_pushstrcopy(st, pd->pet.name); break;
  11039. case 3: script_pushint(st,(int)pd->pet.level); break;
  11040. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  11041. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  11042. default:
  11043. script_pushint(st,0);
  11044. break;
  11045. }
  11046. return 0;
  11047. }
  11048. BUILDIN_FUNC(callshop)
  11049. {
  11050. TBL_PC *sd = NULL;
  11051. struct npc_data *nd;
  11052. const char *shopname;
  11053. int flag = 0;
  11054. sd = script_rid2sd(st);
  11055. if (!sd) {
  11056. script_pushint(st,0);
  11057. return 0;
  11058. }
  11059. shopname = script_getstr(st, 2);
  11060. if( script_hasdata(st,3) )
  11061. flag = script_getnum(st,3);
  11062. nd = npc_name2id(shopname);
  11063. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  11064. {
  11065. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  11066. script_pushint(st,0);
  11067. return 1;
  11068. }
  11069. if( nd->subtype == SHOP )
  11070. {
  11071. switch( flag )
  11072. {
  11073. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  11074. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  11075. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  11076. }
  11077. }
  11078. else
  11079. clif_cashshop_show(sd, nd);
  11080. sd->npc_shopid = nd->bl.id;
  11081. script_pushint(st,1);
  11082. return 0;
  11083. }
  11084. BUILDIN_FUNC(npcshopitem)
  11085. {
  11086. const char* npcname = script_getstr(st, 2);
  11087. struct npc_data* nd = npc_name2id(npcname);
  11088. int n, i;
  11089. int amount;
  11090. if( !nd || nd->subtype != SHOP )
  11091. { //Not found.
  11092. script_pushint(st,0);
  11093. return 0;
  11094. }
  11095. // get the count of new entries
  11096. amount = (script_lastdata(st)-2)/2;
  11097. // generate new shop item list
  11098. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  11099. for( n = 0, i = 3; n < amount; n++, i+=2 )
  11100. {
  11101. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  11102. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  11103. }
  11104. nd->u.shop.count = n;
  11105. script_pushint(st,1);
  11106. return 0;
  11107. }
  11108. BUILDIN_FUNC(npcshopadditem)
  11109. {
  11110. const char* npcname = script_getstr(st,2);
  11111. struct npc_data* nd = npc_name2id(npcname);
  11112. int n, i;
  11113. int amount;
  11114. if( !nd || nd->subtype != SHOP )
  11115. { //Not found.
  11116. script_pushint(st,0);
  11117. return 0;
  11118. }
  11119. // get the count of new entries
  11120. amount = (script_lastdata(st)-2)/2;
  11121. // append new items to existing shop item list
  11122. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  11123. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  11124. {
  11125. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  11126. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  11127. }
  11128. nd->u.shop.count = n;
  11129. script_pushint(st,1);
  11130. return 0;
  11131. }
  11132. BUILDIN_FUNC(npcshopdelitem)
  11133. {
  11134. const char* npcname = script_getstr(st,2);
  11135. struct npc_data* nd = npc_name2id(npcname);
  11136. unsigned int nameid;
  11137. int n, i;
  11138. int amount;
  11139. int size;
  11140. if( !nd || nd->subtype != SHOP )
  11141. { //Not found.
  11142. script_pushint(st,0);
  11143. return 0;
  11144. }
  11145. amount = script_lastdata(st)-2;
  11146. size = nd->u.shop.count;
  11147. // remove specified items from the shop item list
  11148. for( i = 3; i < 3 + amount; i++ )
  11149. {
  11150. nameid = script_getnum(st,i);
  11151. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  11152. if( n < size )
  11153. {
  11154. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  11155. size--;
  11156. }
  11157. }
  11158. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  11159. nd->u.shop.count = size;
  11160. script_pushint(st,1);
  11161. return 0;
  11162. }
  11163. //Sets a script to attach to a shop npc.
  11164. BUILDIN_FUNC(npcshopattach)
  11165. {
  11166. const char* npcname = script_getstr(st,2);
  11167. struct npc_data* nd = npc_name2id(npcname);
  11168. int flag = 1;
  11169. if( script_hasdata(st,3) )
  11170. flag = script_getnum(st,3);
  11171. if( !nd || nd->subtype != SHOP )
  11172. { //Not found.
  11173. script_pushint(st,0);
  11174. return 0;
  11175. }
  11176. if (flag)
  11177. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  11178. else
  11179. nd->master_nd = NULL;
  11180. script_pushint(st,1);
  11181. return 0;
  11182. }
  11183. /*==========================================
  11184. * Returns some values of an item [Lupus]
  11185. * Price, Weight, etc...
  11186. setitemscript(itemID,"{new item bonus script}",[n]);
  11187. Where n:
  11188. 0 - script
  11189. 1 - Equip script
  11190. 2 - Unequip script
  11191. *------------------------------------------*/
  11192. BUILDIN_FUNC(setitemscript)
  11193. {
  11194. int item_id,n=0;
  11195. const char *script;
  11196. struct item_data *i_data;
  11197. struct script_code **dstscript;
  11198. item_id = script_getnum(st,2);
  11199. script = script_getstr(st,3);
  11200. if( script_hasdata(st,4) )
  11201. n=script_getnum(st,4);
  11202. i_data = itemdb_exists(item_id);
  11203. if (!i_data || script==NULL || script[0]!='{') {
  11204. script_pushint(st,0);
  11205. return 0;
  11206. }
  11207. switch (n) {
  11208. case 2:
  11209. dstscript = &i_data->unequip_script;
  11210. break;
  11211. case 1:
  11212. dstscript = &i_data->equip_script;
  11213. break;
  11214. default:
  11215. dstscript = &i_data->script;
  11216. break;
  11217. }
  11218. if(*dstscript)
  11219. script_free_code(*dstscript);
  11220. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11221. script_pushint(st,1);
  11222. return 0;
  11223. }
  11224. /* Work In Progress [Lupus]
  11225. BUILDIN_FUNC(addmonsterdrop)
  11226. {
  11227. int class_,item_id,chance;
  11228. class_=script_getnum(st,2);
  11229. item_id=script_getnum(st,3);
  11230. chance=script_getnum(st,4);
  11231. if(class_>1000 && item_id>500 && chance>0) {
  11232. script_pushint(st,1);
  11233. } else {
  11234. script_pushint(st,0);
  11235. }
  11236. }
  11237. BUILDIN_FUNC(delmonsterdrop)
  11238. {
  11239. int class_,item_id;
  11240. class_=script_getnum(st,2);
  11241. item_id=script_getnum(st,3);
  11242. if(class_>1000 && item_id>500) {
  11243. script_pushint(st,1);
  11244. } else {
  11245. script_pushint(st,0);
  11246. }
  11247. }
  11248. */
  11249. /*==========================================
  11250. * Returns some values of a monster [Lupus]
  11251. * Name, Level, race, size, etc...
  11252. getmonsterinfo(monsterID,queryIndex);
  11253. *------------------------------------------*/
  11254. BUILDIN_FUNC(getmonsterinfo)
  11255. {
  11256. struct mob_db *mob;
  11257. int mob_id;
  11258. mob_id = script_getnum(st,2);
  11259. if (!mobdb_checkid(mob_id)) {
  11260. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  11261. if ( !script_getnum(st,3) ) //requested a string
  11262. script_pushconststr(st,"null");
  11263. else
  11264. script_pushint(st,-1);
  11265. return -1;
  11266. }
  11267. mob = mob_db(mob_id);
  11268. switch ( script_getnum(st,3) ) {
  11269. case 0: script_pushstrcopy(st,mob->jname); break;
  11270. case 1: script_pushint(st,mob->lv); break;
  11271. case 2: script_pushint(st,mob->status.max_hp); break;
  11272. case 3: script_pushint(st,mob->base_exp); break;
  11273. case 4: script_pushint(st,mob->job_exp); break;
  11274. case 5: script_pushint(st,mob->status.rhw.atk); break;
  11275. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  11276. case 7: script_pushint(st,mob->status.def); break;
  11277. case 8: script_pushint(st,mob->status.mdef); break;
  11278. case 9: script_pushint(st,mob->status.str); break;
  11279. case 10: script_pushint(st,mob->status.agi); break;
  11280. case 11: script_pushint(st,mob->status.vit); break;
  11281. case 12: script_pushint(st,mob->status.int_); break;
  11282. case 13: script_pushint(st,mob->status.dex); break;
  11283. case 14: script_pushint(st,mob->status.luk); break;
  11284. case 15: script_pushint(st,mob->status.rhw.range); break;
  11285. case 16: script_pushint(st,mob->range2); break;
  11286. case 17: script_pushint(st,mob->range3); break;
  11287. case 18: script_pushint(st,mob->status.size); break;
  11288. case 19: script_pushint(st,mob->status.race); break;
  11289. case 20: script_pushint(st,mob->status.def_ele); break;
  11290. case 21: script_pushint(st,mob->status.mode); break;
  11291. default: script_pushint(st,-1); //wrong Index
  11292. }
  11293. return 0;
  11294. }
  11295. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11296. {
  11297. TBL_PC *sd = NULL;
  11298. if(script_hasdata(st,2))
  11299. sd = map_nick2sd(script_getstr(st,2));
  11300. else
  11301. sd = script_rid2sd(st);
  11302. if(sd)
  11303. script_pushint(st,(sd->vender_id != 0));
  11304. else
  11305. script_pushint(st,0);
  11306. return 0;
  11307. }
  11308. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11309. {
  11310. TBL_PC *sd = NULL;
  11311. if(script_hasdata(st,2))
  11312. sd = map_nick2sd(script_getstr(st,2));
  11313. else
  11314. sd = script_rid2sd(st);
  11315. if(sd)
  11316. script_pushint(st,(sd->chatID != 0));
  11317. else
  11318. script_pushint(st,0);
  11319. return 0;
  11320. }
  11321. BUILDIN_FUNC(searchitem)
  11322. {
  11323. struct script_data* data = script_getdata(st, 2);
  11324. const char *itemname = script_getstr(st,3);
  11325. struct item_data *items[MAX_SEARCH];
  11326. int count;
  11327. char* name;
  11328. int32 start;
  11329. int32 id;
  11330. int32 i;
  11331. TBL_PC* sd = NULL;
  11332. if ((items[0] = itemdb_exists(atoi(itemname))))
  11333. count = 1;
  11334. else {
  11335. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  11336. if (count > MAX_SEARCH) count = MAX_SEARCH;
  11337. }
  11338. if (!count) {
  11339. script_pushint(st, 0);
  11340. return 0;
  11341. }
  11342. if( !data_isreference(data) )
  11343. {
  11344. ShowError("script:searchitem: not a variable\n");
  11345. script_reportdata(data);
  11346. st->state = END;
  11347. return 1;// not a variable
  11348. }
  11349. id = reference_getid(data);
  11350. start = reference_getindex(data);
  11351. name = reference_getname(data);
  11352. if( not_array_variable(*name) )
  11353. {
  11354. ShowError("script:searchitem: illegal scope\n");
  11355. script_reportdata(data);
  11356. st->state = END;
  11357. return 1;// not supported
  11358. }
  11359. if( not_server_variable(*name) )
  11360. {
  11361. sd = script_rid2sd(st);
  11362. if( sd == NULL )
  11363. return 0;// no player attached
  11364. }
  11365. if( is_string_variable(name) )
  11366. {// string array
  11367. ShowError("script:searchitem: not an integer array reference\n");
  11368. script_reportdata(data);
  11369. st->state = END;
  11370. return 1;// not supported
  11371. }
  11372. for( i = 0; i < count; ++start, ++i )
  11373. {// Set array
  11374. void* v = (void*)items[i]->nameid;
  11375. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  11376. }
  11377. script_pushint(st, count);
  11378. return 0;
  11379. }
  11380. int axtoi(const char *hexStg)
  11381. {
  11382. int n = 0; // position in string
  11383. int m = 0; // position in digit[] to shift
  11384. int count; // loop index
  11385. int intValue = 0; // integer value of hex string
  11386. int digit[11]; // hold values to convert
  11387. while (n < 10) {
  11388. if (hexStg[n]=='\0')
  11389. break;
  11390. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11391. digit[n] = hexStg[n] & 0x0f; //convert to int
  11392. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11393. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11394. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11395. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11396. else break;
  11397. n++;
  11398. }
  11399. count = n;
  11400. m = n - 1;
  11401. n = 0;
  11402. while(n < count) {
  11403. // digit[n] is value of hex digit at position n
  11404. // (m << 2) is the number of positions to shift
  11405. // OR the bits into return value
  11406. intValue = intValue | (digit[n] << (m << 2));
  11407. m--; // adjust the position to set
  11408. n++; // next digit to process
  11409. }
  11410. return (intValue);
  11411. }
  11412. // [Lance] Hex string to integer converter
  11413. BUILDIN_FUNC(axtoi)
  11414. {
  11415. const char *hex = script_getstr(st,2);
  11416. script_pushint(st,axtoi(hex));
  11417. return 0;
  11418. }
  11419. // [zBuffer] List of player cont commands --->
  11420. BUILDIN_FUNC(rid2name)
  11421. {
  11422. struct block_list *bl = NULL;
  11423. int rid = script_getnum(st,2);
  11424. if((bl = map_id2bl(rid)))
  11425. {
  11426. switch(bl->type) {
  11427. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  11428. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  11429. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  11430. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  11431. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  11432. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  11433. default:
  11434. ShowError("buildin_rid2name: BL type unknown.\n");
  11435. script_pushconststr(st,"");
  11436. break;
  11437. }
  11438. } else {
  11439. ShowError("buildin_rid2name: invalid RID\n");
  11440. script_pushconststr(st,"(null)");
  11441. }
  11442. return 0;
  11443. }
  11444. BUILDIN_FUNC(pcblockmove)
  11445. {
  11446. int id, flag;
  11447. TBL_PC *sd = NULL;
  11448. id = script_getnum(st,2);
  11449. flag = script_getnum(st,3);
  11450. if(id)
  11451. sd = map_id2sd(id);
  11452. else
  11453. sd = script_rid2sd(st);
  11454. if(sd)
  11455. sd->state.blockedmove = flag > 0;
  11456. return 0;
  11457. }
  11458. BUILDIN_FUNC(pcfollow)
  11459. {
  11460. int id, targetid;
  11461. TBL_PC *sd = NULL;
  11462. id = script_getnum(st,2);
  11463. targetid = script_getnum(st,3);
  11464. if(id)
  11465. sd = map_id2sd(id);
  11466. else
  11467. sd = script_rid2sd(st);
  11468. if(sd)
  11469. pc_follow(sd, targetid);
  11470. return 0;
  11471. }
  11472. BUILDIN_FUNC(pcstopfollow)
  11473. {
  11474. int id;
  11475. TBL_PC *sd = NULL;
  11476. id = script_getnum(st,2);
  11477. if(id)
  11478. sd = map_id2sd(id);
  11479. else
  11480. sd = script_rid2sd(st);
  11481. if(sd)
  11482. pc_stop_following(sd);
  11483. return 0;
  11484. }
  11485. // <--- [zBuffer] List of player cont commands
  11486. // [zBuffer] List of mob control commands --->
  11487. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11488. /// Makes the unit walk to target position or map
  11489. /// Returns if it was successfull
  11490. ///
  11491. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11492. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11493. BUILDIN_FUNC(unitwalk)
  11494. {
  11495. struct block_list* bl;
  11496. bl = map_id2bl(script_getnum(st,2));
  11497. if( bl == NULL )
  11498. {
  11499. script_pushint(st, 0);
  11500. }
  11501. else if( script_hasdata(st,4) )
  11502. {
  11503. int x = script_getnum(st,3);
  11504. int y = script_getnum(st,4);
  11505. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11506. }
  11507. else
  11508. {
  11509. int map_id = script_getnum(st,3);
  11510. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11511. }
  11512. return 0;
  11513. }
  11514. /// Kills the unit
  11515. ///
  11516. /// unitkill <unit_id>;
  11517. BUILDIN_FUNC(unitkill)
  11518. {
  11519. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11520. if( bl != NULL )
  11521. status_kill(bl);
  11522. return 0;
  11523. }
  11524. /// Warps the unit to the target position in the target map
  11525. /// Returns if it was successfull
  11526. ///
  11527. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11528. BUILDIN_FUNC(unitwarp)
  11529. {
  11530. int unit_id;
  11531. int map;
  11532. short x;
  11533. short y;
  11534. struct block_list* bl;
  11535. unit_id = script_getnum(st,2);
  11536. map = map_mapname2mapid(script_getstr(st, 3));
  11537. x = (short)script_getnum(st,4);
  11538. y = (short)script_getnum(st,5);
  11539. bl = map_id2bl(unit_id);
  11540. if( map >= 0 && bl != NULL )
  11541. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  11542. else
  11543. script_pushint(st, 0);
  11544. return 0;
  11545. }
  11546. /// Makes the unit attack the target.
  11547. /// If the unit is a player and <action type> is not 0, it does a continuous
  11548. /// attack instead of a single attack.
  11549. /// Returns if the request was successfull.
  11550. ///
  11551. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11552. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11553. BUILDIN_FUNC(unitattack)
  11554. {
  11555. struct block_list* unit_bl;
  11556. struct block_list* target_bl = NULL;
  11557. struct script_data* data;
  11558. int actiontype = 0;
  11559. // get unit
  11560. unit_bl = map_id2bl(script_getnum(st,2));
  11561. if( unit_bl == NULL ) {
  11562. script_pushint(st, 0);
  11563. return 0;
  11564. }
  11565. data = script_getdata(st, 3);
  11566. get_val(st, data);
  11567. if( data_isstring(data) )
  11568. {
  11569. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11570. if( sd != NULL )
  11571. target_bl = &sd->bl;
  11572. } else
  11573. target_bl = map_id2bl(conv_num(st, data));
  11574. // request the attack
  11575. if( target_bl == NULL )
  11576. {
  11577. script_pushint(st, 0);
  11578. return 0;
  11579. }
  11580. // get actiontype
  11581. if( script_hasdata(st,4) )
  11582. actiontype = script_getnum(st,4);
  11583. switch( unit_bl->type )
  11584. {
  11585. case BL_PC:
  11586. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11587. script_pushint(st, 1);
  11588. return 0;
  11589. case BL_MOB:
  11590. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11591. break;
  11592. case BL_PET:
  11593. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11594. break;
  11595. default:
  11596. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11597. script_pushint(st, 0);
  11598. return 1;
  11599. }
  11600. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11601. return 0;
  11602. }
  11603. /// Makes the unit stop attacking and moving
  11604. ///
  11605. /// unitstop <unit_id>;
  11606. BUILDIN_FUNC(unitstop)
  11607. {
  11608. int unit_id;
  11609. struct block_list* bl;
  11610. unit_id = script_getnum(st,2);
  11611. bl = map_id2bl(unit_id);
  11612. if( bl != NULL )
  11613. {
  11614. unit_stop_attack(bl);
  11615. unit_stop_walking(bl,4);
  11616. if( bl->type == BL_MOB )
  11617. ((TBL_MOB*)bl)->target_id = 0;
  11618. }
  11619. return 0;
  11620. }
  11621. /// Makes the unit say the message
  11622. ///
  11623. /// unittalk <unit_id>,"<message>";
  11624. BUILDIN_FUNC(unittalk)
  11625. {
  11626. int unit_id;
  11627. const char* message;
  11628. struct block_list* bl;
  11629. unit_id = script_getnum(st,2);
  11630. message = script_getstr(st, 3);
  11631. bl = map_id2bl(unit_id);
  11632. if( bl != NULL )
  11633. {
  11634. struct StringBuf sbuf;
  11635. StringBuf_Init(&sbuf);
  11636. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11637. clif_message(bl, StringBuf_Value(&sbuf));
  11638. if( bl->type == BL_PC )
  11639. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11640. StringBuf_Destroy(&sbuf);
  11641. }
  11642. return 0;
  11643. }
  11644. /// Makes the unit do an emotion
  11645. ///
  11646. /// unitemote <unit_id>,<emotion>;
  11647. ///
  11648. /// @see e_* in const.txt
  11649. BUILDIN_FUNC(unitemote)
  11650. {
  11651. int unit_id;
  11652. int emotion;
  11653. struct block_list* bl;
  11654. unit_id = script_getnum(st,2);
  11655. emotion = script_getnum(st,3);
  11656. bl = map_id2bl(unit_id);
  11657. if( bl != NULL )
  11658. clif_emotion(bl, emotion);
  11659. return 0;
  11660. }
  11661. /// Makes the unit cast the skill on the target or self if no target is specified
  11662. ///
  11663. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11664. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  11665. BUILDIN_FUNC(unitskilluseid)
  11666. {
  11667. int unit_id;
  11668. int skill_id;
  11669. int skill_lv;
  11670. int target_id;
  11671. struct block_list* bl;
  11672. unit_id = script_getnum(st,2);
  11673. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11674. skill_lv = script_getnum(st,4);
  11675. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11676. bl = map_id2bl(unit_id);
  11677. if( bl != NULL )
  11678. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11679. return 0;
  11680. }
  11681. /// Makes the unit cast the skill on the target position.
  11682. ///
  11683. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11684. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  11685. BUILDIN_FUNC(unitskillusepos)
  11686. {
  11687. int unit_id;
  11688. int skill_id;
  11689. int skill_lv;
  11690. int skill_x;
  11691. int skill_y;
  11692. struct block_list* bl;
  11693. unit_id = script_getnum(st,2);
  11694. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11695. skill_lv = script_getnum(st,4);
  11696. skill_x = script_getnum(st,5);
  11697. skill_y = script_getnum(st,6);
  11698. bl = map_id2bl(unit_id);
  11699. if( bl != NULL )
  11700. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11701. return 0;
  11702. }
  11703. // <--- [zBuffer] List of mob control commands
  11704. /// Pauses the execution of the script, detaching the player
  11705. ///
  11706. /// sleep <mili seconds>;
  11707. BUILDIN_FUNC(sleep)
  11708. {
  11709. int ticks;
  11710. ticks = script_getnum(st,2);
  11711. // detach the player
  11712. script_detach_rid(st);
  11713. if( ticks <= 0 )
  11714. {// do nothing
  11715. }
  11716. else if( st->sleep.tick == 0 )
  11717. {// sleep for the target amount of time
  11718. st->state = RERUNLINE;
  11719. st->sleep.tick = ticks;
  11720. }
  11721. else
  11722. {// sleep time is over
  11723. st->state = RUN;
  11724. st->sleep.tick = 0;
  11725. }
  11726. return 0;
  11727. }
  11728. /// Pauses the execution of the script, keeping the player attached
  11729. /// Returns if a player is still attached
  11730. ///
  11731. /// sleep2(<mili secconds>) -> <bool>
  11732. BUILDIN_FUNC(sleep2)
  11733. {
  11734. int ticks;
  11735. ticks = script_getnum(st,2);
  11736. if( ticks <= 0 )
  11737. {// do nothing
  11738. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11739. }
  11740. else if( !st->sleep.tick )
  11741. {// sleep for the target amount of time
  11742. st->state = RERUNLINE;
  11743. st->sleep.tick = ticks;
  11744. }
  11745. else
  11746. {// sleep time is over
  11747. st->state = RUN;
  11748. st->sleep.tick = 0;
  11749. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11750. }
  11751. return 0;
  11752. }
  11753. /// Awakes all the sleep timers of the target npc
  11754. ///
  11755. /// awake "<npc name>";
  11756. BUILDIN_FUNC(awake)
  11757. {
  11758. struct npc_data* nd;
  11759. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11760. nd = npc_name2id(script_getstr(st, 2));
  11761. if( nd == NULL ) {
  11762. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  11763. return 1;
  11764. }
  11765. while( node )
  11766. {
  11767. if( (int)node->key == nd->bl.id )
  11768. {// sleep timer for the npc
  11769. struct script_state* tst = (struct script_state*)node->data;
  11770. TBL_PC* sd = map_id2sd(tst->rid);
  11771. if( tst->sleep.timer == -1 )
  11772. {// already awake ???
  11773. node = node->next;
  11774. continue;
  11775. }
  11776. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11777. {// char not online anymore / another char of the same account is online - Cancel execution
  11778. tst->state = END;
  11779. tst->rid = 0;
  11780. }
  11781. delete_timer(tst->sleep.timer, run_script_timer);
  11782. node = script_erase_sleepdb(node);
  11783. tst->sleep.timer = INVALID_TIMER;
  11784. if(tst->state != RERUNLINE)
  11785. tst->sleep.tick = 0;
  11786. run_script_main(tst);
  11787. }
  11788. else
  11789. {
  11790. node = node->next;
  11791. }
  11792. }
  11793. return 0;
  11794. }
  11795. /// Returns a reference to a variable of the target NPC.
  11796. /// Returns 0 if an error occurs.
  11797. ///
  11798. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11799. BUILDIN_FUNC(getvariableofnpc)
  11800. {
  11801. struct script_data* data;
  11802. const char* name;
  11803. struct npc_data* nd;
  11804. data = script_getdata(st,2);
  11805. if( !data_isreference(data) )
  11806. {// Not a reference (aka varaible name)
  11807. ShowError("script:getvariableofnpc: not a variable\n");
  11808. script_reportdata(data);
  11809. script_pushnil(st);
  11810. st->state = END;
  11811. return 1;
  11812. }
  11813. name = reference_getname(data);
  11814. if( *name != '.' || name[1] == '@' )
  11815. {// not a npc variable
  11816. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11817. script_reportdata(data);
  11818. script_pushnil(st);
  11819. st->state = END;
  11820. return 1;
  11821. }
  11822. nd = npc_name2id(script_getstr(st,3));
  11823. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11824. {// NPC not found or has no script
  11825. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11826. script_pushnil(st);
  11827. st->state = END;
  11828. return 1;
  11829. }
  11830. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11831. return 0;
  11832. }
  11833. /// Opens a warp portal.
  11834. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11835. ///
  11836. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11837. ///
  11838. /// @author blackhole89
  11839. BUILDIN_FUNC(warpportal)
  11840. {
  11841. int spx;
  11842. int spy;
  11843. unsigned short mapindex;
  11844. int tpx;
  11845. int tpy;
  11846. struct skill_unit_group* group;
  11847. struct block_list* bl;
  11848. bl = map_id2bl(st->oid);
  11849. if( bl == NULL )
  11850. {
  11851. ShowError("script:warpportal: npc is needed\n");
  11852. return 1;
  11853. }
  11854. spx = script_getnum(st,2);
  11855. spy = script_getnum(st,3);
  11856. mapindex = mapindex_name2id(script_getstr(st, 4));
  11857. tpx = script_getnum(st,5);
  11858. tpy = script_getnum(st,6);
  11859. if( mapindex == 0 )
  11860. return 0;// map not found
  11861. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11862. if( group == NULL )
  11863. return 0;// failed
  11864. group->val2 = (tpx<<16) | tpy;
  11865. group->val3 = mapindex;
  11866. return 0;
  11867. }
  11868. BUILDIN_FUNC(openmail)
  11869. {
  11870. TBL_PC* sd;
  11871. sd = script_rid2sd(st);
  11872. if( sd == NULL )
  11873. return 0;
  11874. #ifndef TXT_ONLY
  11875. mail_openmail(sd);
  11876. #endif
  11877. return 0;
  11878. }
  11879. BUILDIN_FUNC(openauction)
  11880. {
  11881. TBL_PC* sd;
  11882. sd = script_rid2sd(st);
  11883. if( sd == NULL )
  11884. return 0;
  11885. #ifndef TXT_ONLY
  11886. clif_Auction_openwindow(sd);
  11887. #endif
  11888. return 0;
  11889. }
  11890. /// Retrieves the value of the specified flag of the specified cell.
  11891. ///
  11892. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11893. ///
  11894. /// @see cell_chk* constants in const.txt for the types
  11895. BUILDIN_FUNC(checkcell)
  11896. {
  11897. int m = map_mapname2mapid(script_getstr(st,2));
  11898. int x = script_getnum(st,3);
  11899. int y = script_getnum(st,4);
  11900. cell_chk type = (cell_chk)script_getnum(st,5);
  11901. script_pushint(st, map_getcell(m, x, y, type));
  11902. return 0;
  11903. }
  11904. /// Modifies flags of cells in the specified area.
  11905. ///
  11906. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11907. ///
  11908. /// @see cell_* constants in const.txt for the types
  11909. BUILDIN_FUNC(setcell)
  11910. {
  11911. int m = map_mapname2mapid(script_getstr(st,2));
  11912. int x1 = script_getnum(st,3);
  11913. int y1 = script_getnum(st,4);
  11914. int x2 = script_getnum(st,5);
  11915. int y2 = script_getnum(st,6);
  11916. cell_t type = (cell_t)script_getnum(st,7);
  11917. bool flag = (bool)script_getnum(st,8);
  11918. int x,y;
  11919. if( x1 > x2 ) swap(x1,x2);
  11920. if( y1 > y2 ) swap(y1,y2);
  11921. for( y = y1; y <= y2; ++y )
  11922. for( x = x1; x <= x2; ++x )
  11923. map_setcell(m, x, y, type, flag);
  11924. return 0;
  11925. }
  11926. /*==========================================
  11927. * Mercenary Commands
  11928. *------------------------------------------*/
  11929. BUILDIN_FUNC(mercenary_create)
  11930. {
  11931. struct map_session_data *sd;
  11932. int class_, contract_time;
  11933. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  11934. return 0;
  11935. class_ = script_getnum(st,2);
  11936. if( !merc_class(class_) )
  11937. return 0;
  11938. contract_time = script_getnum(st,3);
  11939. merc_create(sd, class_, contract_time);
  11940. return 0;
  11941. }
  11942. BUILDIN_FUNC(mercenary_heal)
  11943. {
  11944. struct map_session_data *sd = script_rid2sd(st);
  11945. int hp, sp;
  11946. if( sd == NULL || sd->md == NULL )
  11947. return 0;
  11948. hp = script_getnum(st,2);
  11949. sp = script_getnum(st,3);
  11950. status_heal(&sd->md->bl, hp, sp, 0);
  11951. return 0;
  11952. }
  11953. BUILDIN_FUNC(mercenary_sc_start)
  11954. {
  11955. struct map_session_data *sd = script_rid2sd(st);
  11956. enum sc_type type;
  11957. int tick, val1;
  11958. if( sd == NULL || sd->md == NULL )
  11959. return 0;
  11960. type = (sc_type)script_getnum(st,2);
  11961. tick = script_getnum(st,3);
  11962. val1 = script_getnum(st,4);
  11963. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  11964. return 0;
  11965. }
  11966. BUILDIN_FUNC(mercenary_get_calls)
  11967. {
  11968. struct map_session_data *sd = script_rid2sd(st);
  11969. int guild;
  11970. if( sd == NULL )
  11971. return 0;
  11972. guild = script_getnum(st,2);
  11973. switch( guild )
  11974. {
  11975. case ARCH_MERC_GUILD:
  11976. script_pushint(st,sd->status.arch_calls);
  11977. break;
  11978. case SPEAR_MERC_GUILD:
  11979. script_pushint(st,sd->status.spear_calls);
  11980. break;
  11981. case SWORD_MERC_GUILD:
  11982. script_pushint(st,sd->status.sword_calls);
  11983. break;
  11984. default:
  11985. script_pushint(st,0);
  11986. break;
  11987. }
  11988. return 0;
  11989. }
  11990. BUILDIN_FUNC(mercenary_set_calls)
  11991. {
  11992. struct map_session_data *sd = script_rid2sd(st);
  11993. int guild, value, *calls;
  11994. if( sd == NULL )
  11995. return 0;
  11996. guild = script_getnum(st,2);
  11997. value = script_getnum(st,3);
  11998. switch( guild )
  11999. {
  12000. case ARCH_MERC_GUILD:
  12001. calls = &sd->status.arch_calls;
  12002. break;
  12003. case SPEAR_MERC_GUILD:
  12004. calls = &sd->status.spear_calls;
  12005. break;
  12006. case SWORD_MERC_GUILD:
  12007. calls = &sd->status.sword_calls;
  12008. break;
  12009. default:
  12010. return 0; // Invalid Guild
  12011. }
  12012. *calls += value;
  12013. *calls = cap_value(*calls, 0, INT_MAX);
  12014. return 0;
  12015. }
  12016. BUILDIN_FUNC(mercenary_get_faith)
  12017. {
  12018. struct map_session_data *sd = script_rid2sd(st);
  12019. int guild;
  12020. if( sd == NULL )
  12021. return 0;
  12022. guild = script_getnum(st,2);
  12023. switch( guild )
  12024. {
  12025. case ARCH_MERC_GUILD:
  12026. script_pushint(st,sd->status.arch_faith);
  12027. break;
  12028. case SPEAR_MERC_GUILD:
  12029. script_pushint(st,sd->status.spear_faith);
  12030. break;
  12031. case SWORD_MERC_GUILD:
  12032. script_pushint(st,sd->status.sword_faith);
  12033. break;
  12034. default:
  12035. script_pushint(st,0);
  12036. break;
  12037. }
  12038. return 0;
  12039. }
  12040. BUILDIN_FUNC(mercenary_set_faith)
  12041. {
  12042. struct map_session_data *sd = script_rid2sd(st);
  12043. int guild, value, *calls;
  12044. if( sd == NULL )
  12045. return 0;
  12046. guild = script_getnum(st,2);
  12047. value = script_getnum(st,3);
  12048. switch( guild )
  12049. {
  12050. case ARCH_MERC_GUILD:
  12051. calls = &sd->status.arch_faith;
  12052. break;
  12053. case SPEAR_MERC_GUILD:
  12054. calls = &sd->status.spear_faith;
  12055. break;
  12056. case SWORD_MERC_GUILD:
  12057. calls = &sd->status.sword_faith;
  12058. break;
  12059. default:
  12060. return 0; // Invalid Guild
  12061. }
  12062. *calls += value;
  12063. *calls = cap_value(*calls, 0, INT_MAX);
  12064. if( mercenary_get_guild(sd->md) == guild )
  12065. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  12066. return 0;
  12067. }
  12068. /*------------------------------------------
  12069. * Book Reading
  12070. *------------------------------------------*/
  12071. BUILDIN_FUNC(readbook)
  12072. {
  12073. struct map_session_data *sd;
  12074. int book_id, page;
  12075. if( (sd = script_rid2sd(st)) == NULL )
  12076. return 0;
  12077. book_id = script_getnum(st,2);
  12078. page = script_getnum(st,3);
  12079. clif_readbook(sd->fd, book_id, page);
  12080. return 0;
  12081. }
  12082. /******************
  12083. Questlog script commands
  12084. *******************/
  12085. BUILDIN_FUNC(setquest)
  12086. {
  12087. TBL_PC * sd = script_rid2sd(st);
  12088. quest_add(sd, script_getnum(st, 2));
  12089. return 0;
  12090. }
  12091. BUILDIN_FUNC(erasequest)
  12092. {
  12093. TBL_PC * sd = script_rid2sd(st);
  12094. quest_delete(sd, script_getnum(st, 2));
  12095. return 0;
  12096. }
  12097. BUILDIN_FUNC(completequest)
  12098. {
  12099. TBL_PC * sd = script_rid2sd(st);
  12100. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  12101. return 0;
  12102. }
  12103. BUILDIN_FUNC(changequest)
  12104. {
  12105. TBL_PC * sd = script_rid2sd(st);
  12106. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  12107. return 0;
  12108. }
  12109. BUILDIN_FUNC(checkquest)
  12110. {
  12111. TBL_PC * sd = script_rid2sd(st);
  12112. quest_check_type type = HAVEQUEST;
  12113. if( script_hasdata(st, 3) )
  12114. type = (quest_check_type)script_getnum(st, 3);
  12115. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  12116. return 0;
  12117. }
  12118. BUILDIN_FUNC(showevent)
  12119. {
  12120. TBL_PC *sd = script_rid2sd(st);
  12121. struct npc_data *nd = map_id2nd(st->oid);
  12122. int state, color;
  12123. if( sd == NULL || nd == NULL )
  12124. return 0;
  12125. state = script_getnum(st, 2);
  12126. color = script_getnum(st, 3);
  12127. if( color < 0 || color > 4 )
  12128. color = 0; // set default color
  12129. clif_quest_show_event(sd, &nd->bl, state, color);
  12130. return 0;
  12131. }
  12132. /*==========================================
  12133. * BattleGround System
  12134. *------------------------------------------*/
  12135. BUILDIN_FUNC(waitingroom2bg)
  12136. {
  12137. struct npc_data *nd;
  12138. struct chat_data *cd;
  12139. const char *map_name, *ev = "", *dev = "";
  12140. int x, y, i, mapindex = 0, bg_id, n;
  12141. struct map_session_data *sd;
  12142. if( script_hasdata(st,7) )
  12143. nd = npc_name2id(script_getstr(st,7));
  12144. else
  12145. nd = (struct npc_data *)map_id2bl(st->oid);
  12146. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  12147. {
  12148. script_pushint(st,0);
  12149. return 0;
  12150. }
  12151. map_name = script_getstr(st,2);
  12152. if( strcmp(map_name,"-") != 0 )
  12153. {
  12154. mapindex = mapindex_name2id(map_name);
  12155. if( mapindex == 0 )
  12156. { // Invalid Map
  12157. script_pushint(st,0);
  12158. return 0;
  12159. }
  12160. }
  12161. x = script_getnum(st,3);
  12162. y = script_getnum(st,4);
  12163. ev = script_getstr(st,5); // Logout Event
  12164. dev = script_getstr(st,6); // Die Event
  12165. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  12166. { // Creation failed
  12167. script_pushint(st,0);
  12168. return 0;
  12169. }
  12170. n = cd->users;
  12171. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  12172. {
  12173. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  12174. mapreg_setreg(add_str("$@arenamembers") + (i<<24), sd->bl.id);
  12175. else
  12176. mapreg_setreg(add_str("$@arenamembers") + (i<<24), 0);
  12177. }
  12178. mapreg_setreg(add_str("$@arenamembersnum"), i);
  12179. script_pushint(st,bg_id);
  12180. return 0;
  12181. }
  12182. BUILDIN_FUNC(waitingroom2bg_single)
  12183. {
  12184. const char* map_name;
  12185. struct npc_data *nd;
  12186. struct chat_data *cd;
  12187. struct map_session_data *sd;
  12188. int x, y, mapindex, bg_id;
  12189. bg_id = script_getnum(st,2);
  12190. map_name = script_getstr(st,3);
  12191. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12192. return 0; // Invalid Map
  12193. x = script_getnum(st,4);
  12194. y = script_getnum(st,5);
  12195. nd = npc_name2id(script_getstr(st,6));
  12196. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  12197. return 0;
  12198. if( (sd = cd->usersd[0]) == NULL )
  12199. return 0;
  12200. if( bg_team_join(bg_id, sd) )
  12201. {
  12202. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  12203. script_pushint(st,1);
  12204. }
  12205. else
  12206. script_pushint(st,0);
  12207. return 0;
  12208. }
  12209. BUILDIN_FUNC(bg_team_setxy)
  12210. {
  12211. struct battleground_data *bg;
  12212. int bg_id;
  12213. bg_id = script_getnum(st,2);
  12214. if( (bg = bg_team_search(bg_id)) == NULL )
  12215. return 0;
  12216. bg->x = script_getnum(st,3);
  12217. bg->y = script_getnum(st,4);
  12218. return 0;
  12219. }
  12220. BUILDIN_FUNC(bg_warp)
  12221. {
  12222. int x, y, mapindex, bg_id;
  12223. const char* map_name;
  12224. bg_id = script_getnum(st,2);
  12225. map_name = script_getstr(st,3);
  12226. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12227. return 0; // Invalid Map
  12228. x = script_getnum(st,4);
  12229. y = script_getnum(st,5);
  12230. bg_team_warp(bg_id, mapindex, x, y);
  12231. return 0;
  12232. }
  12233. BUILDIN_FUNC(bg_monster)
  12234. {
  12235. int class_ = 0, x = 0, y = 0, bg_id = 0;
  12236. const char *str,*map, *evt="";
  12237. bg_id = script_getnum(st,2);
  12238. map = script_getstr(st,3);
  12239. x = script_getnum(st,4);
  12240. y = script_getnum(st,5);
  12241. str = script_getstr(st,6);
  12242. class_ = script_getnum(st,7);
  12243. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  12244. check_event(st, evt);
  12245. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  12246. return 0;
  12247. }
  12248. BUILDIN_FUNC(bg_monster_set_team)
  12249. {
  12250. struct mob_data *md;
  12251. struct block_list *mbl;
  12252. int id = script_getnum(st,2),
  12253. bg_id = script_getnum(st,3);
  12254. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  12255. return 0;
  12256. md = (TBL_MOB *)mbl;
  12257. md->state.bg_id = bg_id;
  12258. mob_stop_attack(md);
  12259. mob_stop_walking(md, 0);
  12260. md->target_id = md->attacked_id = 0;
  12261. clif_charnameack(0, &md->bl);
  12262. return 0;
  12263. }
  12264. BUILDIN_FUNC(bg_leave)
  12265. {
  12266. struct map_session_data *sd = script_rid2sd(st);
  12267. if( sd == NULL || !sd->state.bg_id )
  12268. return 0;
  12269. bg_team_leave(sd,0);
  12270. return 0;
  12271. }
  12272. BUILDIN_FUNC(bg_destroy)
  12273. {
  12274. int bg_id = script_getnum(st,2);
  12275. bg_team_delete(bg_id);
  12276. return 0;
  12277. }
  12278. BUILDIN_FUNC(bg_getareausers)
  12279. {
  12280. const char *str;
  12281. int m, x0, y0, x1, y1, bg_id;
  12282. int i = 0, c = 0;
  12283. struct battleground_data *bg = NULL;
  12284. struct map_session_data *sd;
  12285. bg_id = script_getnum(st,2);
  12286. str = script_getstr(st,3);
  12287. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  12288. {
  12289. script_pushint(st,0);
  12290. return 0;
  12291. }
  12292. x0 = script_getnum(st,4);
  12293. y0 = script_getnum(st,5);
  12294. x1 = script_getnum(st,6);
  12295. y1 = script_getnum(st,7);
  12296. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  12297. {
  12298. if( (sd = bg->members[i].sd) == NULL )
  12299. continue;
  12300. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  12301. continue;
  12302. c++;
  12303. }
  12304. script_pushint(st,c);
  12305. return 0;
  12306. }
  12307. BUILDIN_FUNC(bg_updatescore)
  12308. {
  12309. const char *str;
  12310. int m;
  12311. str = script_getstr(st,2);
  12312. if( (m = map_mapname2mapid(str)) < 0 )
  12313. return 0;
  12314. map[m].bgscore_lion = script_getnum(st,3);
  12315. map[m].bgscore_eagle = script_getnum(st,4);
  12316. clif_bg_updatescore(m);
  12317. return 0;
  12318. }
  12319. BUILDIN_FUNC(bg_get_data)
  12320. {
  12321. struct battleground_data *bg;
  12322. int bg_id = script_getnum(st,2),
  12323. type = script_getnum(st,3);
  12324. if( (bg = bg_team_search(bg_id)) == NULL )
  12325. {
  12326. script_pushint(st,0);
  12327. return 0;
  12328. }
  12329. switch( type )
  12330. {
  12331. case 0: script_pushint(st, bg->count); break;
  12332. default:
  12333. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  12334. break;
  12335. }
  12336. return 0;
  12337. }
  12338. /*==========================================
  12339. * Instancing Script Commands
  12340. *------------------------------------------*/
  12341. BUILDIN_FUNC(instance_create)
  12342. {
  12343. const char *name;
  12344. int party_id, res;
  12345. name = script_getstr(st, 2);
  12346. party_id = script_getnum(st, 3);
  12347. res = instance_create(party_id, name);
  12348. if( res == -4 ) // Already exists
  12349. {
  12350. script_pushint(st, -1);
  12351. return 0;
  12352. }
  12353. else if( res < 0 )
  12354. {
  12355. char *err;
  12356. switch(res)
  12357. {
  12358. case -3: err = "No free instances"; break;
  12359. case -2: err = "Missing parameter"; break;
  12360. case -1: err = "Invalid type"; break;
  12361. default: err = "Unknown"; break;
  12362. }
  12363. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  12364. script_pushint(st, -2);
  12365. return 0;
  12366. }
  12367. script_pushint(st, res);
  12368. return 0;
  12369. }
  12370. BUILDIN_FUNC(instance_destroy)
  12371. {
  12372. int instance_id;
  12373. struct map_session_data *sd;
  12374. struct party_data *p;
  12375. if( script_hasdata(st, 2) )
  12376. instance_id = script_getnum(st, 2);
  12377. else if( st->instance_id )
  12378. instance_id = st->instance_id;
  12379. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12380. instance_id = p->instance_id;
  12381. else return 0;
  12382. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12383. {
  12384. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  12385. return 0;
  12386. }
  12387. instance_destroy(instance_id);
  12388. return 0;
  12389. }
  12390. BUILDIN_FUNC(instance_attachmap)
  12391. {
  12392. const char *name;
  12393. int m;
  12394. int instance_id;
  12395. bool usebasename = false;
  12396. name = script_getstr(st,2);
  12397. instance_id = script_getnum(st,3);
  12398. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  12399. usebasename = true;
  12400. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  12401. {
  12402. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  12403. script_pushconststr(st, "");
  12404. return 0;
  12405. }
  12406. script_pushconststr(st, map[m].name);
  12407. return 0;
  12408. }
  12409. BUILDIN_FUNC(instance_detachmap)
  12410. {
  12411. struct map_session_data *sd;
  12412. struct party_data *p;
  12413. const char *str;
  12414. int m, instance_id;
  12415. str = script_getstr(st, 2);
  12416. if( script_hasdata(st, 3) )
  12417. instance_id = script_getnum(st, 3);
  12418. else if( st->instance_id )
  12419. instance_id = st->instance_id;
  12420. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12421. instance_id = p->instance_id;
  12422. else return 0;
  12423. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  12424. {
  12425. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  12426. return 0;
  12427. }
  12428. instance_del_map(m);
  12429. return 0;
  12430. }
  12431. BUILDIN_FUNC(instance_attach)
  12432. {
  12433. int instance_id;
  12434. instance_id = script_getnum(st, 2);
  12435. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12436. return 0;
  12437. st->instance_id = instance_id;
  12438. return 0;
  12439. }
  12440. BUILDIN_FUNC(instance_id)
  12441. {
  12442. int type, instance_id;
  12443. struct map_session_data *sd;
  12444. struct party_data *p;
  12445. if( script_hasdata(st, 2) )
  12446. {
  12447. type = script_getnum(st, 2);
  12448. if( type == 0 )
  12449. instance_id = st->instance_id;
  12450. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  12451. instance_id = p->instance_id;
  12452. else
  12453. instance_id = 0;
  12454. }
  12455. else
  12456. instance_id = st->instance_id;
  12457. script_pushint(st, instance_id);
  12458. return 0;
  12459. }
  12460. BUILDIN_FUNC(instance_set_timeout)
  12461. {
  12462. int progress_timeout, idle_timeout;
  12463. int instance_id;
  12464. struct map_session_data *sd;
  12465. struct party_data *p;
  12466. progress_timeout = script_getnum(st, 2);
  12467. idle_timeout = script_getnum(st, 3);
  12468. if( script_hasdata(st, 4) )
  12469. instance_id = script_getnum(st, 4);
  12470. else if( st->instance_id )
  12471. instance_id = st->instance_id;
  12472. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12473. instance_id = p->instance_id;
  12474. else return 0;
  12475. if( instance_id > 0 )
  12476. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  12477. return 0;
  12478. }
  12479. BUILDIN_FUNC(instance_init)
  12480. {
  12481. int instance_id = script_getnum(st, 2);
  12482. if( instance[instance_id].state != INSTANCE_IDLE )
  12483. {
  12484. ShowError("instance_init: instance already initialized.\n");
  12485. return 0;
  12486. }
  12487. instance_init(instance_id);
  12488. return 0;
  12489. }
  12490. BUILDIN_FUNC(instance_announce)
  12491. {
  12492. int instance_id = script_getnum(st,2);
  12493. const char *mes = script_getstr(st,3);
  12494. int flag = script_getnum(st,4);
  12495. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  12496. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  12497. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  12498. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  12499. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  12500. int i;
  12501. struct map_session_data *sd;
  12502. struct party_data *p;
  12503. if( instance_id == 0 )
  12504. {
  12505. if( st->instance_id )
  12506. instance_id = st->instance_id;
  12507. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12508. instance_id = p->instance_id;
  12509. else return 0;
  12510. }
  12511. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12512. return 0;
  12513. for( i = 0; i < instance[instance_id].num_map; i++ )
  12514. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  12515. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  12516. return 0;
  12517. }
  12518. BUILDIN_FUNC(instance_npcname)
  12519. {
  12520. const char *str;
  12521. int instance_id = 0;
  12522. struct map_session_data *sd;
  12523. struct party_data *p;
  12524. struct npc_data *nd;
  12525. str = script_getstr(st, 2);
  12526. if( script_hasdata(st, 3) )
  12527. instance_id = script_getnum(st, 3);
  12528. else if( st->instance_id )
  12529. instance_id = st->instance_id;
  12530. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12531. instance_id = p->instance_id;
  12532. if( instance_id && (nd = npc_name2id(str)) != NULL )
  12533. {
  12534. static char npcname[NAME_LENGTH];
  12535. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  12536. script_pushconststr(st,npcname);
  12537. }
  12538. else
  12539. {
  12540. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  12541. st->state = END;
  12542. return 1;
  12543. }
  12544. return 0;
  12545. }
  12546. BUILDIN_FUNC(has_instance)
  12547. {
  12548. struct map_session_data *sd;
  12549. struct party_data *p;
  12550. const char *str;
  12551. int m, instance_id = 0;
  12552. str = script_getstr(st, 2);
  12553. if( script_hasdata(st, 3) )
  12554. instance_id = script_getnum(st, 3);
  12555. else if( st->instance_id )
  12556. instance_id = st->instance_id;
  12557. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12558. instance_id = p->instance_id;
  12559. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  12560. {
  12561. script_pushconststr(st, "");
  12562. return 0;
  12563. }
  12564. script_pushconststr(st, map[m].name);
  12565. return 0;
  12566. }
  12567. BUILDIN_FUNC(instance_warpall)
  12568. {
  12569. struct map_session_data *pl_sd;
  12570. int m, i, instance_id;
  12571. const char *mapn;
  12572. int x, y;
  12573. unsigned short mapindex;
  12574. struct party_data *p = NULL;
  12575. mapn = script_getstr(st,2);
  12576. x = script_getnum(st,3);
  12577. y = script_getnum(st,4);
  12578. if( script_hasdata(st,5) )
  12579. instance_id = script_getnum(st,5);
  12580. else if( st->instance_id )
  12581. instance_id = st->instance_id;
  12582. 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 )
  12583. instance_id = p->instance_id;
  12584. else return 0;
  12585. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  12586. return 0;
  12587. if( !(p = party_search(instance[instance_id].party_id)) )
  12588. return 0;
  12589. mapindex = map_id2index(m);
  12590. for( i = 0; i < MAX_PARTY; i++ )
  12591. 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);
  12592. return 0;
  12593. }
  12594. /*==========================================
  12595. * Custom Fonts
  12596. *------------------------------------------*/
  12597. BUILDIN_FUNC(setfont)
  12598. {
  12599. struct map_session_data *sd = script_rid2sd(st);
  12600. int font = script_getnum(st,2);
  12601. if( sd == NULL )
  12602. return 0;
  12603. if( sd->state.user_font != font )
  12604. sd->state.user_font = font;
  12605. else
  12606. sd->state.user_font = 0;
  12607. clif_font(sd);
  12608. return 0;
  12609. }
  12610. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  12611. {
  12612. TBL_MOB* md = (TBL_MOB*)bl;
  12613. struct block_list *tbl;
  12614. int mobid = va_arg(ap,int);
  12615. int skillid = va_arg(ap,int);
  12616. int skilllv = va_arg(ap,int);
  12617. int casttime = va_arg(ap,int);
  12618. int cancel = va_arg(ap,int);
  12619. int emotion = va_arg(ap,int);
  12620. int target = va_arg(ap,int);
  12621. if( md->class_ != mobid )
  12622. return 0;
  12623. if( md->ud.skilltimer != -1 ) // Cancel the casting skill.
  12624. unit_skillcastcancel(bl,0);
  12625. // 0:self, 1:target, 2:master, default:random
  12626. switch( target )
  12627. {
  12628. case 0: tbl = map_id2bl(md->bl.id); break;
  12629. case 1: tbl = map_id2bl(md->target_id); break;
  12630. case 2: tbl = map_id2bl(md->master_id); break;
  12631. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  12632. }
  12633. if( !tbl )
  12634. return 0;
  12635. if( skill_get_casttype(skillid) == CAST_GROUND )
  12636. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  12637. else
  12638. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  12639. clif_emotion(&md->bl, emotion);
  12640. return 0;
  12641. }
  12642. /*==========================================
  12643. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  12644. *------------------------------------------*/
  12645. BUILDIN_FUNC(areamobuseskill)
  12646. {
  12647. struct block_list center;
  12648. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  12649. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  12650. {
  12651. ShowError("areamobuseskill: invalid map name.\n");
  12652. return 0;
  12653. }
  12654. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  12655. return 0;
  12656. center.m = m;
  12657. center.x = script_getnum(st,3);
  12658. center.y = script_getnum(st,4);
  12659. range = script_getnum(st,5);
  12660. mobid = script_getnum(st,6);
  12661. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  12662. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  12663. skilllv = battle_config.mob_max_skilllvl;
  12664. casttime = script_getnum(st,9);
  12665. cancel = script_getnum(st,10);
  12666. emotion = script_getnum(st,11);
  12667. target = script_getnum(st,12);
  12668. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  12669. return 0;
  12670. }
  12671. BUILDIN_FUNC(progressbar)
  12672. {
  12673. #if PACKETVER >= 20080318
  12674. struct map_session_data * sd = script_rid2sd(st);
  12675. const char * color;
  12676. unsigned int second;
  12677. if( !st || !sd )
  12678. return 0;
  12679. st->state = STOP;
  12680. color = script_getstr(st,2);
  12681. second = script_getnum(st,3);
  12682. sd->progressbar.npc_id = st->oid;
  12683. sd->progressbar.timeout = gettick() + second*1000;
  12684. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  12685. #endif
  12686. return 0;
  12687. }
  12688. BUILDIN_FUNC(pushpc)
  12689. {
  12690. int direction, cells, dx, dy;
  12691. struct map_session_data* sd;
  12692. if((sd = script_rid2sd(st))==NULL)
  12693. {
  12694. return 0;
  12695. }
  12696. direction = script_getnum(st,2);
  12697. cells = script_getnum(st,3);
  12698. if(direction<0 || direction>7)
  12699. {
  12700. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  12701. script_reportsrc(st);
  12702. direction%= 8; // trim spin-over
  12703. }
  12704. if(!cells)
  12705. {// zero distance
  12706. return 0;
  12707. }
  12708. else if(cells<0)
  12709. {// pushing backwards
  12710. direction = (direction+4)%8; // turn around
  12711. cells = -cells;
  12712. }
  12713. dx = dirx[direction];
  12714. dy = diry[direction];
  12715. unit_blown(&sd->bl, dx, dy, cells, 0);
  12716. return 0;
  12717. }
  12718. // declarations that were supposed to be exported from npc_chat.c
  12719. #ifdef PCRE_SUPPORT
  12720. BUILDIN_FUNC(defpattern);
  12721. BUILDIN_FUNC(activatepset);
  12722. BUILDIN_FUNC(deactivatepset);
  12723. BUILDIN_FUNC(deletepset);
  12724. #endif
  12725. /// script command definitions
  12726. /// for an explanation on args, see add_buildin_func
  12727. struct script_function buildin_func[] = {
  12728. // NPC interaction
  12729. BUILDIN_DEF(mes,"s"),
  12730. BUILDIN_DEF(next,""),
  12731. BUILDIN_DEF(close,""),
  12732. BUILDIN_DEF(close2,""),
  12733. BUILDIN_DEF(menu,"sl*"),
  12734. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  12735. BUILDIN_DEF(prompt,"s*"),
  12736. //
  12737. BUILDIN_DEF(goto,"l"),
  12738. BUILDIN_DEF(callsub,"l*"),
  12739. BUILDIN_DEF(callfunc,"s*"),
  12740. BUILDIN_DEF(return,"?"),
  12741. BUILDIN_DEF(getarg,"i?"),
  12742. BUILDIN_DEF(jobchange,"i?"),
  12743. BUILDIN_DEF(jobname,"i"),
  12744. BUILDIN_DEF(input,"r??"),
  12745. BUILDIN_DEF(warp,"sii"),
  12746. BUILDIN_DEF(areawarp,"siiiisii"),
  12747. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  12748. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  12749. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  12750. BUILDIN_DEF(setlook,"ii"),
  12751. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  12752. BUILDIN_DEF(set,"rv"),
  12753. BUILDIN_DEF(setarray,"rv*"),
  12754. BUILDIN_DEF(cleararray,"rvi"),
  12755. BUILDIN_DEF(copyarray,"rri"),
  12756. BUILDIN_DEF(getarraysize,"r"),
  12757. BUILDIN_DEF(deletearray,"r?"),
  12758. BUILDIN_DEF(getelementofarray,"ri"),
  12759. BUILDIN_DEF(getitem,"vi?"),
  12760. BUILDIN_DEF(rentitem,"vi"),
  12761. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  12762. BUILDIN_DEF(getnameditem,"vv"),
  12763. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  12764. BUILDIN_DEF(makeitem,"visii"),
  12765. BUILDIN_DEF(delitem,"vi?"),
  12766. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  12767. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  12768. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  12769. BUILDIN_DEF(cutin,"si"),
  12770. BUILDIN_DEF(viewpoint,"iiiii"),
  12771. BUILDIN_DEF(heal,"ii"),
  12772. BUILDIN_DEF(itemheal,"ii"),
  12773. BUILDIN_DEF(percentheal,"ii"),
  12774. BUILDIN_DEF(rand,"i?"),
  12775. BUILDIN_DEF(countitem,"v"),
  12776. BUILDIN_DEF(countitem2,"viiiiiii"),
  12777. BUILDIN_DEF(checkweight,"vi"),
  12778. BUILDIN_DEF(readparam,"i?"),
  12779. BUILDIN_DEF(getcharid,"i?"),
  12780. BUILDIN_DEF(getpartyname,"i"),
  12781. BUILDIN_DEF(getpartymember,"i?"),
  12782. BUILDIN_DEF(getpartyleader,"i?"),
  12783. BUILDIN_DEF(getguildname,"i"),
  12784. BUILDIN_DEF(getguildmaster,"i"),
  12785. BUILDIN_DEF(getguildmasterid,"i"),
  12786. BUILDIN_DEF(strcharinfo,"i"),
  12787. BUILDIN_DEF(strnpcinfo,"i"),
  12788. BUILDIN_DEF(getequipid,"i"),
  12789. BUILDIN_DEF(getequipname,"i"),
  12790. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  12791. BUILDIN_DEF(repair,"i"), // [Valaris]
  12792. BUILDIN_DEF(getequipisequiped,"i"),
  12793. BUILDIN_DEF(getequipisenableref,"i"),
  12794. BUILDIN_DEF(getequipisidentify,"i"),
  12795. BUILDIN_DEF(getequiprefinerycnt,"i"),
  12796. BUILDIN_DEF(getequipweaponlv,"i"),
  12797. BUILDIN_DEF(getequippercentrefinery,"i"),
  12798. BUILDIN_DEF(successrefitem,"i"),
  12799. BUILDIN_DEF(failedrefitem,"i"),
  12800. BUILDIN_DEF(statusup,"i"),
  12801. BUILDIN_DEF(statusup2,"ii"),
  12802. BUILDIN_DEF(bonus,"iv"),
  12803. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  12804. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  12805. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  12806. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  12807. BUILDIN_DEF(autobonus,"sii??"),
  12808. BUILDIN_DEF(autobonus2,"sii??"),
  12809. BUILDIN_DEF(autobonus3,"siiv?"),
  12810. BUILDIN_DEF(skill,"vi?"),
  12811. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  12812. BUILDIN_DEF(guildskill,"vi"),
  12813. BUILDIN_DEF(getskilllv,"v"),
  12814. BUILDIN_DEF(getgdskilllv,"iv"),
  12815. BUILDIN_DEF(basicskillcheck,""),
  12816. BUILDIN_DEF(getgmlevel,""),
  12817. BUILDIN_DEF(end,""),
  12818. BUILDIN_DEF(checkoption,"i"),
  12819. BUILDIN_DEF(setoption,"i?"),
  12820. BUILDIN_DEF(setcart,"?"),
  12821. BUILDIN_DEF(checkcart,""),
  12822. BUILDIN_DEF(setfalcon,"?"),
  12823. BUILDIN_DEF(checkfalcon,""),
  12824. BUILDIN_DEF(setriding,"?"),
  12825. BUILDIN_DEF(checkriding,""),
  12826. BUILDIN_DEF2(savepoint,"save","sii"),
  12827. BUILDIN_DEF(savepoint,"sii"),
  12828. BUILDIN_DEF(gettimetick,"i"),
  12829. BUILDIN_DEF(gettime,"i"),
  12830. BUILDIN_DEF(gettimestr,"si"),
  12831. BUILDIN_DEF(openstorage,""),
  12832. BUILDIN_DEF(guildopenstorage,""),
  12833. BUILDIN_DEF(itemskill,"vi"),
  12834. BUILDIN_DEF(produce,"i"),
  12835. BUILDIN_DEF(cooking,"i"),
  12836. BUILDIN_DEF(monster,"siisii?"),
  12837. BUILDIN_DEF(getmobdrops,"i"),
  12838. BUILDIN_DEF(areamonster,"siiiisii?"),
  12839. BUILDIN_DEF(killmonster,"ss?"),
  12840. BUILDIN_DEF(killmonsterall,"s?"),
  12841. BUILDIN_DEF(clone,"siisi????"),
  12842. BUILDIN_DEF(doevent,"s"),
  12843. BUILDIN_DEF(donpcevent,"s"),
  12844. BUILDIN_DEF(cmdothernpc,"ss"),
  12845. BUILDIN_DEF(addtimer,"is"),
  12846. BUILDIN_DEF(deltimer,"s"),
  12847. BUILDIN_DEF(addtimercount,"si"),
  12848. BUILDIN_DEF(initnpctimer,"??"),
  12849. BUILDIN_DEF(stopnpctimer,"??"),
  12850. BUILDIN_DEF(startnpctimer,"??"),
  12851. BUILDIN_DEF(setnpctimer,"i?"),
  12852. BUILDIN_DEF(getnpctimer,"i?"),
  12853. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  12854. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  12855. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  12856. BUILDIN_DEF(announce,"si?????"),
  12857. BUILDIN_DEF(mapannounce,"ssi?????"),
  12858. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  12859. BUILDIN_DEF(getusers,"i"),
  12860. BUILDIN_DEF(getmapguildusers,"si"),
  12861. BUILDIN_DEF(getmapusers,"s"),
  12862. BUILDIN_DEF(getareausers,"siiii"),
  12863. BUILDIN_DEF(getareadropitem,"siiiiv"),
  12864. BUILDIN_DEF(enablenpc,"s"),
  12865. BUILDIN_DEF(disablenpc,"s"),
  12866. BUILDIN_DEF(hideoffnpc,"s"),
  12867. BUILDIN_DEF(hideonnpc,"s"),
  12868. BUILDIN_DEF(sc_start,"iii?"),
  12869. BUILDIN_DEF(sc_start2,"iiii?"),
  12870. BUILDIN_DEF(sc_start4,"iiiiii?"),
  12871. BUILDIN_DEF(sc_end,"i?"),
  12872. BUILDIN_DEF(getscrate,"ii?"),
  12873. BUILDIN_DEF(debugmes,"s"),
  12874. BUILDIN_DEF2(catchpet,"pet","i"),
  12875. BUILDIN_DEF2(birthpet,"bpet",""),
  12876. BUILDIN_DEF(resetlvl,"i"),
  12877. BUILDIN_DEF(resetstatus,""),
  12878. BUILDIN_DEF(resetskill,""),
  12879. BUILDIN_DEF(skillpointcount,""),
  12880. BUILDIN_DEF(changebase,"i?"),
  12881. BUILDIN_DEF(changesex,""),
  12882. BUILDIN_DEF(waitingroom,"si??"),
  12883. BUILDIN_DEF(delwaitingroom,"?"),
  12884. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  12885. BUILDIN_DEF(enablewaitingroomevent,"?"),
  12886. BUILDIN_DEF(disablewaitingroomevent,"?"),
  12887. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  12888. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  12889. BUILDIN_DEF(getwaitingroomstate,"i?"),
  12890. BUILDIN_DEF(warpwaitingpc,"sii?"),
  12891. BUILDIN_DEF(attachrid,"i"),
  12892. BUILDIN_DEF(detachrid,""),
  12893. BUILDIN_DEF(isloggedin,"i?"),
  12894. BUILDIN_DEF(setmapflagnosave,"ssii"),
  12895. BUILDIN_DEF(getmapflag,"si"),
  12896. BUILDIN_DEF(setmapflag,"si?"),
  12897. BUILDIN_DEF(removemapflag,"si"),
  12898. BUILDIN_DEF(pvpon,"s"),
  12899. BUILDIN_DEF(pvpoff,"s"),
  12900. BUILDIN_DEF(gvgon,"s"),
  12901. BUILDIN_DEF(gvgoff,"s"),
  12902. BUILDIN_DEF(emotion,"i??"),
  12903. BUILDIN_DEF(maprespawnguildid,"sii"),
  12904. BUILDIN_DEF(agitstart,""), // <Agit>
  12905. BUILDIN_DEF(agitend,""),
  12906. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  12907. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  12908. BUILDIN_DEF(getcastlename,"s"),
  12909. BUILDIN_DEF(getcastledata,"si?"),
  12910. BUILDIN_DEF(setcastledata,"sii"),
  12911. BUILDIN_DEF(requestguildinfo,"i?"),
  12912. BUILDIN_DEF(getequipcardcnt,"i"),
  12913. BUILDIN_DEF(successremovecards,"i"),
  12914. BUILDIN_DEF(failedremovecards,"ii"),
  12915. BUILDIN_DEF(marriage,"s"),
  12916. BUILDIN_DEF2(wedding_effect,"wedding",""),
  12917. BUILDIN_DEF(divorce,""),
  12918. BUILDIN_DEF(ispartneron,""),
  12919. BUILDIN_DEF(getpartnerid,""),
  12920. BUILDIN_DEF(getchildid,""),
  12921. BUILDIN_DEF(getmotherid,""),
  12922. BUILDIN_DEF(getfatherid,""),
  12923. BUILDIN_DEF(warppartner,"sii"),
  12924. BUILDIN_DEF(getitemname,"v"),
  12925. BUILDIN_DEF(getitemslots,"i"),
  12926. BUILDIN_DEF(makepet,"i"),
  12927. BUILDIN_DEF(getexp,"ii"),
  12928. BUILDIN_DEF(getinventorylist,""),
  12929. BUILDIN_DEF(getskilllist,""),
  12930. BUILDIN_DEF(clearitem,""),
  12931. BUILDIN_DEF(classchange,"ii"),
  12932. BUILDIN_DEF(misceffect,"i"),
  12933. BUILDIN_DEF(playBGM,"s"),
  12934. BUILDIN_DEF(playBGMall,"s?????"),
  12935. BUILDIN_DEF(soundeffect,"si"),
  12936. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  12937. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  12938. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  12939. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  12940. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  12941. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  12942. BUILDIN_DEF(petloot,"i"), // [Valaris]
  12943. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  12944. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  12945. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  12946. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  12947. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  12948. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  12949. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  12950. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  12951. BUILDIN_DEF(nude,""), // nude command [Valaris]
  12952. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  12953. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  12954. BUILDIN_DEF(charcommand,"s"), // [MouseJstr]
  12955. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  12956. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  12957. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  12958. BUILDIN_DEF(mobcount,"ss"),
  12959. BUILDIN_DEF(getlook,"i"),
  12960. BUILDIN_DEF(getsavepoint,"i"),
  12961. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  12962. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  12963. BUILDIN_DEF(npcstop,""), // [Valaris]
  12964. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  12965. BUILDIN_DEF(checkoption1,"i"),
  12966. BUILDIN_DEF(checkoption2,"i"),
  12967. BUILDIN_DEF(guildgetexp,"i"),
  12968. BUILDIN_DEF(guildchangegm,"is"),
  12969. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  12970. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  12971. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  12972. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  12973. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  12974. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  12975. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  12976. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  12977. BUILDIN_DEF(night,""), // sets the server to night time
  12978. BUILDIN_DEF(day,""), // sets the server to day time
  12979. #ifdef PCRE_SUPPORT
  12980. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  12981. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  12982. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  12983. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  12984. #endif
  12985. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  12986. BUILDIN_DEF(getusersname,""),
  12987. BUILDIN_DEF(recovery,""),
  12988. BUILDIN_DEF(getpetinfo,"i"),
  12989. BUILDIN_DEF(gethominfo,"i"),
  12990. BUILDIN_DEF(checkequipedcard,"i"),
  12991. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  12992. BUILDIN_DEF(globalmes,"s?"),
  12993. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  12994. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  12995. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  12996. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  12997. BUILDIN_DEF(setnpcdisplay,"sv??"),
  12998. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  12999. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  13000. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  13001. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  13002. // [zBuffer] List of mathematics commands --->
  13003. BUILDIN_DEF(sqrt,"i"),
  13004. BUILDIN_DEF(pow,"ii"),
  13005. BUILDIN_DEF(distance,"iiii"),
  13006. // <--- [zBuffer] List of mathematics commands
  13007. BUILDIN_DEF(md5,"s"),
  13008. // [zBuffer] List of dynamic var commands --->
  13009. BUILDIN_DEF(getd,"s"),
  13010. BUILDIN_DEF(setd,"sv"),
  13011. // <--- [zBuffer] List of dynamic var commands
  13012. BUILDIN_DEF(petstat,"i"),
  13013. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  13014. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  13015. BUILDIN_DEF(npcshopadditem,"sii*"),
  13016. BUILDIN_DEF(npcshopdelitem,"si*"),
  13017. BUILDIN_DEF(npcshopattach,"s?"),
  13018. BUILDIN_DEF(equip,"i"),
  13019. BUILDIN_DEF(autoequip,"ii"),
  13020. BUILDIN_DEF(setbattleflag,"si"),
  13021. BUILDIN_DEF(getbattleflag,"s"),
  13022. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  13023. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  13024. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  13025. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  13026. BUILDIN_DEF(axtoi,"s"),
  13027. BUILDIN_DEF(query_sql,"s*"),
  13028. BUILDIN_DEF(query_logsql,"s*"),
  13029. BUILDIN_DEF(escape_sql,"s"),
  13030. BUILDIN_DEF(atoi,"s"),
  13031. // [zBuffer] List of player cont commands --->
  13032. BUILDIN_DEF(rid2name,"i"),
  13033. BUILDIN_DEF(pcfollow,"ii"),
  13034. BUILDIN_DEF(pcstopfollow,"i"),
  13035. BUILDIN_DEF(pcblockmove,"ii"),
  13036. // <--- [zBuffer] List of player cont commands
  13037. // [zBuffer] List of mob control commands --->
  13038. BUILDIN_DEF(unitwalk,"ii?"),
  13039. BUILDIN_DEF(unitkill,"i"),
  13040. BUILDIN_DEF(unitwarp,"isii"),
  13041. BUILDIN_DEF(unitattack,"iv?"),
  13042. BUILDIN_DEF(unitstop,"i"),
  13043. BUILDIN_DEF(unittalk,"is"),
  13044. BUILDIN_DEF(unitemote,"ii"),
  13045. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  13046. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  13047. // <--- [zBuffer] List of mob control commands
  13048. BUILDIN_DEF(sleep,"i"),
  13049. BUILDIN_DEF(sleep2,"i"),
  13050. BUILDIN_DEF(awake,"s"),
  13051. BUILDIN_DEF(getvariableofnpc,"rs"),
  13052. BUILDIN_DEF(warpportal,"iisii"),
  13053. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  13054. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  13055. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  13056. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  13057. BUILDIN_DEF(checkvending,"?"),
  13058. BUILDIN_DEF(checkchatting,"?"),
  13059. BUILDIN_DEF(openmail,""),
  13060. BUILDIN_DEF(openauction,""),
  13061. BUILDIN_DEF(checkcell,"siii"),
  13062. BUILDIN_DEF(setcell,"siiiiii"),
  13063. BUILDIN_DEF(setwall,"siiiiis"),
  13064. BUILDIN_DEF(delwall,"s"),
  13065. BUILDIN_DEF(searchitem,"rs"),
  13066. BUILDIN_DEF(mercenary_create,"ii"),
  13067. BUILDIN_DEF(mercenary_heal,"ii"),
  13068. BUILDIN_DEF(mercenary_sc_start,"iii"),
  13069. BUILDIN_DEF(mercenary_get_calls,"i"),
  13070. BUILDIN_DEF(mercenary_get_faith,"i"),
  13071. BUILDIN_DEF(mercenary_set_calls,"ii"),
  13072. BUILDIN_DEF(mercenary_set_faith,"ii"),
  13073. BUILDIN_DEF(readbook,"ii"),
  13074. BUILDIN_DEF(setfont,"i"),
  13075. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  13076. BUILDIN_DEF(progressbar,"si"),
  13077. BUILDIN_DEF(pushpc,"ii"),
  13078. // WoE SE
  13079. BUILDIN_DEF(agitstart2,""),
  13080. BUILDIN_DEF(agitend2,""),
  13081. BUILDIN_DEF(agitcheck2,""),
  13082. // BattleGround
  13083. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  13084. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  13085. BUILDIN_DEF(bg_team_setxy,"iii"),
  13086. BUILDIN_DEF(bg_warp,"isii"),
  13087. BUILDIN_DEF(bg_monster,"isiisi?"),
  13088. BUILDIN_DEF(bg_monster_set_team,"ii"),
  13089. BUILDIN_DEF(bg_leave,""),
  13090. BUILDIN_DEF(bg_destroy,"i"),
  13091. BUILDIN_DEF(areapercentheal,"siiiiii"),
  13092. BUILDIN_DEF(bg_get_data,"ii"),
  13093. BUILDIN_DEF(bg_getareausers,"isiiii"),
  13094. BUILDIN_DEF(bg_updatescore,"sii"),
  13095. // Instancing
  13096. BUILDIN_DEF(instance_create,"si"),
  13097. BUILDIN_DEF(instance_destroy,"?"),
  13098. BUILDIN_DEF(instance_attachmap,"si?"),
  13099. BUILDIN_DEF(instance_detachmap,"s?"),
  13100. BUILDIN_DEF(instance_attach,"i"),
  13101. BUILDIN_DEF(instance_id,"?"),
  13102. BUILDIN_DEF(instance_set_timeout,"ii?"),
  13103. BUILDIN_DEF(instance_init,"i"),
  13104. BUILDIN_DEF(instance_announce,"isi?????"),
  13105. BUILDIN_DEF(instance_npcname,"s?"),
  13106. BUILDIN_DEF(has_instance,"s?"),
  13107. BUILDIN_DEF(instance_warpall,"sii?"),
  13108. //Quest Log System [Inkfish]
  13109. BUILDIN_DEF(setquest, "i"),
  13110. BUILDIN_DEF(erasequest, "i"),
  13111. BUILDIN_DEF(completequest, "i"),
  13112. BUILDIN_DEF(checkquest, "i?"),
  13113. BUILDIN_DEF(changequest, "ii"),
  13114. BUILDIN_DEF(showevent, "ii"),
  13115. {NULL,NULL,NULL},
  13116. };