script.c 363 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941
  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. #define SCRIPT_BLOCK_SIZE 512
  144. enum { LABEL_NEXTLINE=1,LABEL_START };
  145. /// temporary buffer for passing around compiled bytecode
  146. /// @see add_scriptb, set_label, parse_script
  147. static unsigned char* script_buf = NULL;
  148. static int script_pos = 0, script_size = 0;
  149. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  150. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  151. // String buffer structures.
  152. // str_data stores string information
  153. static struct str_data_struct {
  154. enum c_op type;
  155. int str;
  156. int backpatch;
  157. int label;
  158. int (*func)(struct script_state *st);
  159. int val;
  160. int next;
  161. } *str_data = NULL;
  162. static int str_data_size = 0; // size of the data
  163. static int str_num = LABEL_START; // next id to be assigned
  164. // str_buf holds the strings themselves
  165. static char *str_buf;
  166. static int str_size = 0; // size of the buffer
  167. static int str_pos = 0; // next position to be assigned
  168. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  169. #define SCRIPT_HASH_SIZE 1021
  170. int str_hash[SCRIPT_HASH_SIZE];
  171. // Specifies which string hashing method to use
  172. //#define SCRIPT_HASH_DJB2
  173. //#define SCRIPT_HASH_SDBM
  174. #define SCRIPT_HASH_ELF
  175. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  176. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  177. static int parse_options=0;
  178. DBMap* script_get_label_db(){ return scriptlabel_db; }
  179. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  180. // Caches compiled autoscript item code.
  181. // Note: This is not cleared when reloading itemdb.
  182. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  183. struct Script_Config script_config = {
  184. 1, // warn_func_mismatch_argtypes
  185. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  186. 0, INT_MAX, // input_min_value/input_max_value
  187. "OnPCDieEvent", //die_event_name
  188. "OnPCKillEvent", //kill_pc_event_name
  189. "OnNPCKillEvent", //kill_mob_event_name
  190. "OnPCLoginEvent", //login_event_name
  191. "OnPCLogoutEvent", //logout_event_name
  192. "OnPCLoadMapEvent", //loadmap_event_name
  193. "OnPCBaseLvUpEvent", //baselvup_event_name
  194. "OnPCJobLvUpEvent", //joblvup_event_name
  195. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  196. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  197. };
  198. static jmp_buf error_jump;
  199. static char* error_msg;
  200. static const char* error_pos;
  201. static int error_report; // if the error should produce output
  202. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  203. // [Eoe / jA 1080, 1081, 1094, 1164]
  204. enum curly_type {
  205. TYPE_NULL = 0,
  206. TYPE_IF,
  207. TYPE_SWITCH,
  208. TYPE_WHILE,
  209. TYPE_FOR,
  210. TYPE_DO,
  211. TYPE_USERFUNC,
  212. TYPE_ARGLIST // function argument list
  213. };
  214. #define ARGLIST_UNDEFINED 0
  215. #define ARGLIST_NO_PAREN 1
  216. #define ARGLIST_PAREN 2
  217. static struct {
  218. struct {
  219. enum curly_type type;
  220. int index;
  221. int count;
  222. int flag;
  223. struct linkdb_node *case_label;
  224. } curly[256]; // 右カッコの情報
  225. int curly_count; // 右カッコの数
  226. int index; // スクリプト内で使用した構文の数
  227. } syntax;
  228. const char* parse_curly_close(const char* p);
  229. const char* parse_syntax_close(const char* p);
  230. const char* parse_syntax_close_sub(const char* p,int* flag);
  231. const char* parse_syntax(const char* p);
  232. static int parse_syntax_for_flag = 0;
  233. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  234. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  235. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  236. int potion_target=0;
  237. c_op get_com(unsigned char *script,int *pos);
  238. int get_num(unsigned char *script,int *pos);
  239. typedef struct script_function {
  240. int (*func)(struct script_state *st);
  241. const char *name;
  242. const char *arg;
  243. } script_function;
  244. extern script_function buildin_func[];
  245. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  246. /*==========================================
  247. * ローカルプロトタイプ宣言 (必要な物のみ)
  248. *------------------------------------------*/
  249. const char* parse_subexpr(const char* p,int limit);
  250. int run_func(struct script_state *st);
  251. enum {
  252. MF_NOMEMO, //0
  253. MF_NOTELEPORT,
  254. MF_NOSAVE,
  255. MF_NOBRANCH,
  256. MF_NOPENALTY,
  257. MF_NOZENYPENALTY,
  258. MF_PVP,
  259. MF_PVP_NOPARTY,
  260. MF_PVP_NOGUILD,
  261. MF_GVG,
  262. MF_GVG_NOPARTY, //10
  263. MF_NOTRADE,
  264. MF_NOSKILL,
  265. MF_NOWARP,
  266. MF_PARTYLOCK,
  267. MF_NOICEWALL,
  268. MF_SNOW,
  269. MF_FOG,
  270. MF_SAKURA,
  271. MF_LEAVES,
  272. MF_RAIN, //20
  273. // 21 free
  274. MF_NOGO = 22,
  275. MF_CLOUDS,
  276. MF_CLOUDS2,
  277. MF_FIREWORKS,
  278. MF_GVG_CASTLE,
  279. MF_GVG_DUNGEON,
  280. MF_NIGHTENABLED,
  281. MF_NOBASEEXP,
  282. MF_NOJOBEXP, //30
  283. MF_NOMOBLOOT,
  284. MF_NOMVPLOOT,
  285. MF_NORETURN,
  286. MF_NOWARPTO,
  287. MF_NIGHTMAREDROP,
  288. MF_RESTRICTED,
  289. MF_NOCOMMAND,
  290. MF_NODROP,
  291. MF_JEXP,
  292. MF_BEXP, //40
  293. MF_NOVENDING,
  294. MF_LOADEVENT,
  295. MF_NOCHAT,
  296. MF_NOEXPPENALTY,
  297. MF_GUILDLOCK,
  298. MF_TOWN,
  299. MF_AUTOTRADE,
  300. MF_ALLOWKS,
  301. MF_MONSTER_NOTELEPORT,
  302. MF_PVP_NOCALCRANK, //50
  303. MF_BATTLEGROUND
  304. };
  305. const char* script_op2name(int op)
  306. {
  307. #define RETURN_OP_NAME(type) case type: return #type
  308. switch( op )
  309. {
  310. RETURN_OP_NAME(C_NOP);
  311. RETURN_OP_NAME(C_POS);
  312. RETURN_OP_NAME(C_INT);
  313. RETURN_OP_NAME(C_PARAM);
  314. RETURN_OP_NAME(C_FUNC);
  315. RETURN_OP_NAME(C_STR);
  316. RETURN_OP_NAME(C_CONSTSTR);
  317. RETURN_OP_NAME(C_ARG);
  318. RETURN_OP_NAME(C_NAME);
  319. RETURN_OP_NAME(C_EOL);
  320. RETURN_OP_NAME(C_RETINFO);
  321. RETURN_OP_NAME(C_USERFUNC);
  322. RETURN_OP_NAME(C_USERFUNC_POS);
  323. // operators
  324. RETURN_OP_NAME(C_OP3);
  325. RETURN_OP_NAME(C_LOR);
  326. RETURN_OP_NAME(C_LAND);
  327. RETURN_OP_NAME(C_LE);
  328. RETURN_OP_NAME(C_LT);
  329. RETURN_OP_NAME(C_GE);
  330. RETURN_OP_NAME(C_GT);
  331. RETURN_OP_NAME(C_EQ);
  332. RETURN_OP_NAME(C_NE);
  333. RETURN_OP_NAME(C_XOR);
  334. RETURN_OP_NAME(C_OR);
  335. RETURN_OP_NAME(C_AND);
  336. RETURN_OP_NAME(C_ADD);
  337. RETURN_OP_NAME(C_SUB);
  338. RETURN_OP_NAME(C_MUL);
  339. RETURN_OP_NAME(C_DIV);
  340. RETURN_OP_NAME(C_MOD);
  341. RETURN_OP_NAME(C_NEG);
  342. RETURN_OP_NAME(C_LNOT);
  343. RETURN_OP_NAME(C_NOT);
  344. RETURN_OP_NAME(C_R_SHIFT);
  345. RETURN_OP_NAME(C_L_SHIFT);
  346. default:
  347. ShowDebug("script_op2name: unexpected op=%d\n", op);
  348. return "???";
  349. }
  350. #undef RETURN_OP_NAME
  351. }
  352. #ifdef DEBUG_DUMP_STACK
  353. static void script_dump_stack(struct script_state* st)
  354. {
  355. int i;
  356. ShowMessage("\tstart = %d\n", st->start);
  357. ShowMessage("\tend = %d\n", st->end);
  358. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  359. ShowMessage("\tsp = %d\n", st->stack->sp);
  360. for( i = 0; i < st->stack->sp; ++i )
  361. {
  362. struct script_data* data = &st->stack->stack_data[i];
  363. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  364. switch( data->type )
  365. {
  366. case C_INT:
  367. case C_POS:
  368. ShowMessage(" %d\n", data->u.num);
  369. break;
  370. case C_STR:
  371. case C_CONSTSTR:
  372. ShowMessage(" \"%s\"\n", data->u.str);
  373. break;
  374. case C_NAME:
  375. 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));
  376. break;
  377. case C_RETINFO:
  378. {
  379. struct script_retinfo* ri = data->u.ri;
  380. 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);
  381. }
  382. break;
  383. default:
  384. ShowMessage("\n");
  385. break;
  386. }
  387. }
  388. }
  389. #endif
  390. /// Reports on the console the src of a script error.
  391. static void script_reportsrc(struct script_state *st)
  392. {
  393. struct block_list* bl;
  394. if( st->oid == 0 )
  395. return; //Can't report source.
  396. bl = map_id2bl(st->oid);
  397. if( bl == NULL )
  398. return;
  399. switch( bl->type )
  400. {
  401. case BL_NPC:
  402. if( bl->m >= 0 )
  403. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  404. else
  405. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  406. break;
  407. default:
  408. if( bl->m >= 0 )
  409. 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);
  410. else
  411. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  412. break;
  413. }
  414. }
  415. /// Reports on the console information about the script data.
  416. static void script_reportdata(struct script_data* data)
  417. {
  418. if( data == NULL )
  419. return;
  420. switch( data->type )
  421. {
  422. case C_NOP:// no value
  423. ShowDebug("Data: nothing (nil)\n");
  424. break;
  425. case C_INT:// number
  426. ShowDebug("Data: number value=%d\n", data->u.num);
  427. break;
  428. case C_STR:
  429. case C_CONSTSTR:// string
  430. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  431. break;
  432. case C_NAME:// reference
  433. if( reference_tovariable(data) )
  434. {// variable
  435. const char* name = reference_getname(data);
  436. if( not_array_variable(*name) )
  437. ShowDebug("Data: variable name='%s'\n", name);
  438. else
  439. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  440. }
  441. else if( reference_toconstant(data) )
  442. {// constant
  443. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  444. }
  445. else if( reference_toparam(data) )
  446. {// param
  447. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  448. }
  449. else
  450. {// ???
  451. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  452. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  453. }
  454. break;
  455. case C_POS:// label
  456. ShowDebug("Data: label pos=%d\n", data->u.num);
  457. break;
  458. default:
  459. ShowDebug("Data: %s\n", script_op2name(data->type));
  460. break;
  461. }
  462. }
  463. /// Reports on the console information about the current built-in function.
  464. static void script_reportfunc(struct script_state* st)
  465. {
  466. int i, params, id;
  467. struct script_data* data;
  468. if( !script_hasdata(st,0) )
  469. {// no stack
  470. return;
  471. }
  472. data = script_getdata(st,0);
  473. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  474. {// script currently not executing a built-in function or corrupt stack
  475. return;
  476. }
  477. id = reference_getid(data);
  478. params = script_lastdata(st)-1;
  479. if( params > 0 )
  480. {
  481. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  482. for( i = 2; i <= script_lastdata(st); i++ )
  483. {
  484. script_reportdata(script_getdata(st,i));
  485. }
  486. }
  487. else
  488. {
  489. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  490. }
  491. }
  492. /*==========================================
  493. * エラーメッセージ出力
  494. *------------------------------------------*/
  495. static void disp_error_message2(const char *mes,const char *pos,int report)
  496. {
  497. error_msg = aStrdup(mes);
  498. error_pos = pos;
  499. error_report = report;
  500. longjmp( error_jump, 1 );
  501. }
  502. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  503. /// Checks event parameter validity
  504. static void check_event(struct script_state *st, const char *evt)
  505. {
  506. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  507. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  508. script_reportsrc(st);
  509. }
  510. }
  511. /*==========================================
  512. * Hashes the input string
  513. *------------------------------------------*/
  514. static unsigned int calc_hash(const char* p)
  515. {
  516. unsigned int h;
  517. #if defined(SCRIPT_HASH_DJB2)
  518. h = 5381;
  519. while( *p ) // hash*33 + c
  520. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  521. #elif defined(SCRIPT_HASH_SDBM)
  522. h = 0;
  523. while( *p ) // hash*65599 + c
  524. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  525. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  526. h = 0;
  527. while( *p ){
  528. unsigned int g;
  529. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  530. g = h & 0xF0000000;
  531. if( g )
  532. {
  533. h ^= g >> 24;
  534. h &= ~g;
  535. }
  536. }
  537. #else // athena hash
  538. h = 0;
  539. while( *p )
  540. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  541. #endif
  542. return h % SCRIPT_HASH_SIZE;
  543. }
  544. /*==========================================
  545. * str_data manipulation functions
  546. *------------------------------------------*/
  547. /// Looks up string using the provided id.
  548. const char* get_str(int id)
  549. {
  550. Assert( id >= LABEL_START && id < str_size );
  551. return str_buf+str_data[id].str;
  552. }
  553. /// Returns the uid of the string, or -1.
  554. static int search_str(const char* p)
  555. {
  556. int i;
  557. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  558. if( strcasecmp(get_str(i),p) == 0 )
  559. return i;
  560. return -1;
  561. }
  562. /// Stores a copy of the string and returns its id.
  563. /// If an identical string is already present, returns its id instead.
  564. int add_str(const char* p)
  565. {
  566. int i, h;
  567. int len;
  568. h = calc_hash(p);
  569. if( str_hash[h] == 0 )
  570. {// empty bucket, add new node here
  571. str_hash[h] = str_num;
  572. }
  573. else
  574. {// scan for end of list, or occurence of identical string
  575. for( i = str_hash[h]; ; i = str_data[i].next )
  576. {
  577. if( strcasecmp(get_str(i),p) == 0 )
  578. return i; // string already in list
  579. if( str_data[i].next == 0 )
  580. break; // reached the end
  581. }
  582. // append node to end of list
  583. str_data[i].next = str_num;
  584. }
  585. // grow list if neccessary
  586. if( str_num >= str_data_size )
  587. {
  588. str_data_size += 128;
  589. RECREATE(str_data,struct str_data_struct,str_data_size);
  590. memset(str_data + (str_data_size - 128), '\0', 128);
  591. }
  592. len=(int)strlen(p);
  593. // grow string buffer if neccessary
  594. while( str_pos+len+1 >= str_size )
  595. {
  596. str_size += 256;
  597. RECREATE(str_buf,char,str_size);
  598. memset(str_buf + (str_size - 256), '\0', 256);
  599. }
  600. safestrncpy(str_buf+str_pos, p, len+1);
  601. str_data[str_num].type = C_NOP;
  602. str_data[str_num].str = str_pos;
  603. str_data[str_num].next = 0;
  604. str_data[str_num].func = NULL;
  605. str_data[str_num].backpatch = -1;
  606. str_data[str_num].label = -1;
  607. str_pos += len+1;
  608. return str_num++;
  609. }
  610. /// Appends 1 byte to the script buffer.
  611. static void add_scriptb(int a)
  612. {
  613. if( script_pos+1 >= script_size )
  614. {
  615. script_size += SCRIPT_BLOCK_SIZE;
  616. RECREATE(script_buf,unsigned char,script_size);
  617. }
  618. script_buf[script_pos++] = (uint8)(a);
  619. }
  620. /// Appends a c_op value to the script buffer.
  621. /// The value is variable-length encoded into 8-bit blocks.
  622. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  623. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  624. static void add_scriptc(int a)
  625. {
  626. while( a >= 0x40 )
  627. {
  628. add_scriptb((a&0x3f)|0x40);
  629. a = (a - 0x40) >> 6;
  630. }
  631. add_scriptb(a);
  632. }
  633. /// Appends an integer value to the script buffer.
  634. /// The value is variable-length encoded into 8-bit blocks.
  635. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  636. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  637. static void add_scripti(int a)
  638. {
  639. while( a >= 0x40 )
  640. {
  641. add_scriptb((a&0x3f)|0xc0);
  642. a = (a - 0x40) >> 6;
  643. }
  644. add_scriptb(a|0x80);
  645. }
  646. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  647. ///
  648. /// @param l The id of the str_data entry
  649. // 最大16Mまで
  650. static void add_scriptl(int l)
  651. {
  652. int backpatch = str_data[l].backpatch;
  653. switch(str_data[l].type){
  654. case C_POS:
  655. case C_USERFUNC_POS:
  656. add_scriptc(C_POS);
  657. add_scriptb(str_data[l].label);
  658. add_scriptb(str_data[l].label>>8);
  659. add_scriptb(str_data[l].label>>16);
  660. break;
  661. case C_NOP:
  662. case C_USERFUNC:
  663. // ラベルの可能性があるのでbackpatch用データ埋め込み
  664. add_scriptc(C_NAME);
  665. str_data[l].backpatch = script_pos;
  666. add_scriptb(backpatch);
  667. add_scriptb(backpatch>>8);
  668. add_scriptb(backpatch>>16);
  669. break;
  670. case C_INT:
  671. add_scripti(abs(str_data[l].val));
  672. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  673. add_scriptc(C_NEG);
  674. break;
  675. default: // assume C_NAME
  676. add_scriptc(C_NAME);
  677. add_scriptb(l);
  678. add_scriptb(l>>8);
  679. add_scriptb(l>>16);
  680. break;
  681. }
  682. }
  683. /*==========================================
  684. * ラベルを解決する
  685. *------------------------------------------*/
  686. void set_label(int l,int pos, const char* script_pos)
  687. {
  688. int i,next;
  689. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  690. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  691. disp_error_message("set_label: invalid label name",script_pos);
  692. return;
  693. }
  694. if(str_data[l].label!=-1){
  695. disp_error_message("set_label: dup label ",script_pos);
  696. return;
  697. }
  698. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  699. str_data[l].label=pos;
  700. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  701. next=GETVALUE(script_buf,i);
  702. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  703. SETVALUE(script_buf,i,pos);
  704. i=next;
  705. }
  706. }
  707. /// Skips spaces and/or comments.
  708. const char* skip_space(const char* p)
  709. {
  710. if( p == NULL )
  711. return NULL;
  712. for(;;)
  713. {
  714. while( ISSPACE(*p) )
  715. ++p;
  716. if( *p == '/' && p[1] == '/' )
  717. {// line comment
  718. while(*p && *p!='\n')
  719. ++p;
  720. }
  721. else if( *p == '/' && p[1] == '*' )
  722. {// block comment
  723. p += 2;
  724. for(;;)
  725. {
  726. if( *p == '\0' )
  727. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  728. if( *p == '*' && p[1] == '/' )
  729. {// end of block comment
  730. p += 2;
  731. break;
  732. }
  733. ++p;
  734. }
  735. }
  736. else
  737. break;
  738. }
  739. return p;
  740. }
  741. /// Skips a word.
  742. /// A word consists of undercores and/or alfanumeric characters,
  743. /// and valid variable prefixes/postfixes.
  744. static
  745. const char* skip_word(const char* p)
  746. {
  747. // prefix
  748. switch( *p )
  749. {
  750. case '@':// temporary char variable
  751. ++p; break;
  752. case '#':// account variable
  753. p += ( p[1] == '#' ? 2 : 1 ); break;
  754. case '\'':// instance variable
  755. ++p; break;
  756. case '.':// npc variable
  757. p += ( p[1] == '@' ? 2 : 1 ); break;
  758. case '$':// global variable
  759. p += ( p[1] == '@' ? 2 : 1 ); break;
  760. }
  761. while( ISALNUM(*p) || *p == '_' )
  762. ++p;
  763. // postfix
  764. if( *p == '$' )// string
  765. p++;
  766. return p;
  767. }
  768. /// Adds a word to str_data.
  769. /// @see skip_word
  770. /// @see add_str
  771. static
  772. int add_word(const char* p)
  773. {
  774. char* word;
  775. int len;
  776. int i;
  777. // Check for a word
  778. len = skip_word(p) - p;
  779. if( len == 0 )
  780. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  781. // Duplicate the word
  782. word = aMalloc(len+1);
  783. memcpy(word, p, len);
  784. word[len] = 0;
  785. // add the word
  786. i = add_str(word);
  787. aFree(word);
  788. return i;
  789. }
  790. /// Parses a function call.
  791. /// The argument list can have parenthesis or not.
  792. /// The number of arguments is checked.
  793. static
  794. const char* parse_callfunc(const char* p, int require_paren)
  795. {
  796. const char* p2;
  797. const char* arg=NULL;
  798. int func;
  799. func = add_word(p);
  800. if( str_data[func].type == C_FUNC ){
  801. // buildin function
  802. add_scriptl(func);
  803. add_scriptc(C_ARG);
  804. arg = buildin_func[str_data[func].val].arg;
  805. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  806. // script defined function
  807. int callsub = search_str("callsub");
  808. add_scriptl(callsub);
  809. add_scriptc(C_ARG);
  810. add_scriptl(func);
  811. arg = buildin_func[str_data[callsub].val].arg;
  812. if( *arg == 0 )
  813. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  814. if( *arg != '*' )
  815. ++arg; // count func as argument
  816. } else
  817. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  818. p = skip_word(p);
  819. p = skip_space(p);
  820. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  821. syntax.curly[syntax.curly_count].count = 0;
  822. if( *p == ';' )
  823. {// <func name> ';'
  824. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  825. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  826. {// <func name> '(' ')'
  827. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  828. p = p2;
  829. /*
  830. } else if( 0 && require_paren && *p != '(' )
  831. {// <func name>
  832. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  833. */
  834. } else
  835. {// <func name> <arg list>
  836. if( require_paren ){
  837. if( *p != '(' )
  838. disp_error_message("need '('",p);
  839. ++p; // skip '('
  840. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  841. } else if( *p == '(' ){
  842. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  843. } else {
  844. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  845. }
  846. ++syntax.curly_count;
  847. while( *arg ) {
  848. p2=parse_subexpr(p,-1);
  849. if( p == p2 )
  850. break; // not an argument
  851. if( *arg != '*' )
  852. ++arg; // next argument
  853. p=skip_space(p2);
  854. if( *arg == 0 || *p != ',' )
  855. break; // no more arguments
  856. ++p; // skip comma
  857. }
  858. --syntax.curly_count;
  859. }
  860. if( *arg && *arg != '?' && *arg != '*' )
  861. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  862. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  863. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  864. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  865. if( *p != ')' )
  866. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  867. ++p;
  868. }
  869. add_scriptc(C_FUNC);
  870. return p;
  871. }
  872. /*==========================================
  873. * 項の解析
  874. *------------------------------------------*/
  875. const char* parse_simpleexpr(const char *p)
  876. {
  877. int i;
  878. p=skip_space(p);
  879. if(*p==';' || *p==',')
  880. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  881. if(*p=='('){
  882. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  883. ++syntax.curly[i].count;
  884. p=parse_subexpr(p+1,-1);
  885. p=skip_space(p);
  886. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  887. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  888. ){
  889. if( *p == ',' ){
  890. syntax.curly[i].flag = ARGLIST_PAREN;
  891. return p;
  892. } else
  893. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  894. }
  895. if( *p != ')' )
  896. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  897. ++p;
  898. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  899. char *np;
  900. i=strtoul(p,&np,0);
  901. add_scripti(i);
  902. p=np;
  903. } else if(*p=='"'){
  904. add_scriptc(C_STR);
  905. p++;
  906. while( *p && *p != '"' ){
  907. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  908. {
  909. char buf[8];
  910. size_t len = skip_escaped_c(p) - p;
  911. size_t n = sv_unescape_c(buf, p, len);
  912. if( n != 1 )
  913. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  914. p += len;
  915. add_scriptb(*buf);
  916. continue;
  917. }
  918. else if( *p == '\n' )
  919. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  920. add_scriptb(*p++);
  921. }
  922. if(!*p)
  923. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  924. add_scriptb(0);
  925. p++; //'"'
  926. } else {
  927. int l;
  928. // label , register , function etc
  929. if(skip_word(p)==p)
  930. disp_error_message("parse_simpleexpr: unexpected character",p);
  931. l=add_word(p);
  932. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  933. return parse_callfunc(p,1);
  934. p=skip_word(p);
  935. if( *p == '[' ){
  936. // array(name[i] => getelementofarray(name,i) )
  937. add_scriptl(search_str("getelementofarray"));
  938. add_scriptc(C_ARG);
  939. add_scriptl(l);
  940. p=parse_subexpr(p+1,-1);
  941. p=skip_space(p);
  942. if( *p != ']' )
  943. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  944. ++p;
  945. add_scriptc(C_FUNC);
  946. }else
  947. add_scriptl(l);
  948. }
  949. return p;
  950. }
  951. /*==========================================
  952. * 式の解析
  953. *------------------------------------------*/
  954. const char* parse_subexpr(const char* p,int limit)
  955. {
  956. int op,opl,len;
  957. const char* tmpp;
  958. p=skip_space(p);
  959. if(*p=='-'){
  960. tmpp=skip_space(p+1);
  961. if(*tmpp==';' || *tmpp==','){
  962. add_scriptl(LABEL_NEXTLINE);
  963. p++;
  964. return p;
  965. }
  966. }
  967. tmpp=p;
  968. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  969. p=parse_subexpr(p+1,10);
  970. add_scriptc(op);
  971. } else
  972. p=parse_simpleexpr(p);
  973. p=skip_space(p);
  974. while((
  975. (op=C_OP3,opl=0,len=1,*p=='?') ||
  976. (op=C_ADD,opl=8,len=1,*p=='+') ||
  977. (op=C_SUB,opl=8,len=1,*p=='-') ||
  978. (op=C_MUL,opl=9,len=1,*p=='*') ||
  979. (op=C_DIV,opl=9,len=1,*p=='/') ||
  980. (op=C_MOD,opl=9,len=1,*p=='%') ||
  981. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  982. (op=C_AND,opl=6,len=1,*p=='&') ||
  983. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  984. (op=C_OR,opl=5,len=1,*p=='|') ||
  985. (op=C_XOR,opl=4,len=1,*p=='^') ||
  986. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  987. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  988. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  989. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  990. (op=C_GT,opl=3,len=1,*p=='>') ||
  991. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  992. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  993. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  994. p+=len;
  995. if(op == C_OP3) {
  996. p=parse_subexpr(p,-1);
  997. p=skip_space(p);
  998. if( *(p++) != ':')
  999. disp_error_message("parse_subexpr: need ':'", p-1);
  1000. p=parse_subexpr(p,-1);
  1001. } else {
  1002. p=parse_subexpr(p,opl);
  1003. }
  1004. add_scriptc(op);
  1005. p=skip_space(p);
  1006. }
  1007. return p; /* return first untreated operator */
  1008. }
  1009. /*==========================================
  1010. * 式の評価
  1011. *------------------------------------------*/
  1012. const char* parse_expr(const char *p)
  1013. {
  1014. switch(*p){
  1015. case ')': case ';': case ':': case '[': case ']':
  1016. case '}':
  1017. disp_error_message("parse_expr: unexpected char",p);
  1018. }
  1019. p=parse_subexpr(p,-1);
  1020. return p;
  1021. }
  1022. /*==========================================
  1023. * 行の解析
  1024. *------------------------------------------*/
  1025. const char* parse_line(const char* p)
  1026. {
  1027. const char* p2;
  1028. p=skip_space(p);
  1029. if(*p==';') {
  1030. // if(); for(); while(); のために閉じ判定
  1031. p = parse_syntax_close(p + 1);
  1032. return p;
  1033. }
  1034. if(*p==')' && parse_syntax_for_flag)
  1035. return p+1;
  1036. p = skip_space(p);
  1037. if(p[0] == '{') {
  1038. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1039. syntax.curly[syntax.curly_count].count = -1;
  1040. syntax.curly[syntax.curly_count].index = -1;
  1041. syntax.curly_count++;
  1042. return p + 1;
  1043. } else if(p[0] == '}') {
  1044. return parse_curly_close(p);
  1045. }
  1046. // 構文関連の処理
  1047. p2 = parse_syntax(p);
  1048. if(p2 != NULL)
  1049. return p2;
  1050. p = parse_callfunc(p,0);
  1051. p = skip_space(p);
  1052. if(parse_syntax_for_flag) {
  1053. if( *p != ')' )
  1054. disp_error_message("parse_line: need ')'",p);
  1055. } else {
  1056. if( *p != ';' )
  1057. disp_error_message("parse_line: need ';'",p);
  1058. }
  1059. // if, for , while の閉じ判定
  1060. p = parse_syntax_close(p+1);
  1061. return p;
  1062. }
  1063. // { ... } の閉じ処理
  1064. const char* parse_curly_close(const char* p)
  1065. {
  1066. if(syntax.curly_count <= 0) {
  1067. disp_error_message("parse_curly_close: unexpected string",p);
  1068. return p + 1;
  1069. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1070. syntax.curly_count--;
  1071. // if, for , while の閉じ判定
  1072. p = parse_syntax_close(p + 1);
  1073. return p;
  1074. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1075. // switch() 閉じ判定
  1076. int pos = syntax.curly_count-1;
  1077. char label[256];
  1078. int l;
  1079. // 一時変数を消す
  1080. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1081. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1082. parse_line(label);
  1083. syntax.curly_count--;
  1084. // 無条件で終了ポインタに移動
  1085. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1086. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1087. parse_line(label);
  1088. syntax.curly_count--;
  1089. // 現在地のラベルを付ける
  1090. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1091. l=add_str(label);
  1092. set_label(l,script_pos, p);
  1093. if(syntax.curly[pos].flag) {
  1094. // default が存在する
  1095. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1096. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1097. parse_line(label);
  1098. syntax.curly_count--;
  1099. }
  1100. // 終了ラベルを付ける
  1101. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1102. l=add_str(label);
  1103. set_label(l,script_pos, p);
  1104. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1105. syntax.curly_count--;
  1106. return p+1;
  1107. } else {
  1108. disp_error_message("parse_curly_close: unexpected string",p);
  1109. return p + 1;
  1110. }
  1111. }
  1112. // 構文関連の処理
  1113. // break, case, continue, default, do, for, function,
  1114. // if, switch, while をこの内部で処理します。
  1115. const char* parse_syntax(const char* p)
  1116. {
  1117. const char *p2 = skip_word(p);
  1118. switch(*p) {
  1119. case 'B':
  1120. case 'b':
  1121. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1122. // break の処理
  1123. char label[256];
  1124. int pos = syntax.curly_count - 1;
  1125. while(pos >= 0) {
  1126. if(syntax.curly[pos].type == TYPE_DO) {
  1127. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1128. break;
  1129. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1130. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1131. break;
  1132. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1133. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1134. break;
  1135. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1136. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1137. break;
  1138. }
  1139. pos--;
  1140. }
  1141. if(pos < 0) {
  1142. disp_error_message("parse_syntax: unexpected 'break'",p);
  1143. } else {
  1144. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1145. parse_line(label);
  1146. syntax.curly_count--;
  1147. }
  1148. p = skip_space(p2);
  1149. if(*p != ';')
  1150. disp_error_message("parse_syntax: need ';'",p);
  1151. // if, for , while の閉じ判定
  1152. p = parse_syntax_close(p + 1);
  1153. return p;
  1154. }
  1155. break;
  1156. case 'c':
  1157. case 'C':
  1158. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1159. // case の処理
  1160. int pos = syntax.curly_count-1;
  1161. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1162. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1163. return p+1;
  1164. } else {
  1165. char label[256];
  1166. int l,v;
  1167. char *np;
  1168. if(syntax.curly[pos].count != 1) {
  1169. // FALLTHRU 用のジャンプ
  1170. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1171. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1172. parse_line(label);
  1173. syntax.curly_count--;
  1174. // 現在地のラベルを付ける
  1175. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1176. l=add_str(label);
  1177. set_label(l,script_pos, p);
  1178. }
  1179. // switch 判定文
  1180. p = skip_space(p2);
  1181. if(p == p2) {
  1182. disp_error_message("parse_syntax: expect space ' '",p);
  1183. }
  1184. // check whether case label is integer or not
  1185. v = strtol(p,&np,0);
  1186. if(np == p) { //Check for constants
  1187. p2 = skip_word(p);
  1188. v = p2-p; // length of word at p2
  1189. memcpy(label,p,v);
  1190. label[v]='\0';
  1191. v = search_str(label);
  1192. if (v < 0 || str_data[v].type != C_INT)
  1193. disp_error_message("parse_syntax: 'case' label not integer",p);
  1194. v = str_data[v].val;
  1195. p = skip_word(p);
  1196. } else { //Numeric value
  1197. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1198. p++;
  1199. p = skip_word(p);
  1200. if(np != p)
  1201. disp_error_message("parse_syntax: 'case' label not integer",np);
  1202. }
  1203. p = skip_space(p);
  1204. if(*p != ':')
  1205. disp_error_message("parse_syntax: expect ':'",p);
  1206. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1207. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1208. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1209. // 2回parse しないとダメ
  1210. p2 = parse_line(label);
  1211. parse_line(p2);
  1212. syntax.curly_count--;
  1213. if(syntax.curly[pos].count != 1) {
  1214. // FALLTHRU 終了後のラベル
  1215. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1216. l=add_str(label);
  1217. set_label(l,script_pos,p);
  1218. }
  1219. // check duplication of case label [Rayce]
  1220. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1221. disp_error_message("parse_syntax: dup 'case'",p);
  1222. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1223. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1224. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1225. parse_line(label);
  1226. syntax.curly_count--;
  1227. syntax.curly[pos].count++;
  1228. }
  1229. return p + 1;
  1230. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1231. // continue の処理
  1232. char label[256];
  1233. int pos = syntax.curly_count - 1;
  1234. while(pos >= 0) {
  1235. if(syntax.curly[pos].type == TYPE_DO) {
  1236. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1237. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1238. break;
  1239. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1240. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1241. break;
  1242. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1243. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1244. break;
  1245. }
  1246. pos--;
  1247. }
  1248. if(pos < 0) {
  1249. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1250. } else {
  1251. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1252. parse_line(label);
  1253. syntax.curly_count--;
  1254. }
  1255. p = skip_space(p2);
  1256. if(*p != ';')
  1257. disp_error_message("parse_syntax: need ';'",p);
  1258. // if, for , while の閉じ判定
  1259. p = parse_syntax_close(p + 1);
  1260. return p;
  1261. }
  1262. break;
  1263. case 'd':
  1264. case 'D':
  1265. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1266. // switch - default の処理
  1267. int pos = syntax.curly_count-1;
  1268. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1269. disp_error_message("parse_syntax: unexpected 'default'",p);
  1270. } else if(syntax.curly[pos].flag) {
  1271. disp_error_message("parse_syntax: dup 'default'",p);
  1272. } else {
  1273. char label[256];
  1274. int l;
  1275. // 現在地のラベルを付ける
  1276. p = skip_space(p2);
  1277. if(*p != ':') {
  1278. disp_error_message("parse_syntax: need ':'",p);
  1279. }
  1280. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1281. l=add_str(label);
  1282. set_label(l,script_pos,p);
  1283. // 無条件で次のリンクに飛ばす
  1284. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1285. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1286. parse_line(label);
  1287. syntax.curly_count--;
  1288. // default のラベルを付ける
  1289. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1290. l=add_str(label);
  1291. set_label(l,script_pos,p);
  1292. syntax.curly[syntax.curly_count - 1].flag = 1;
  1293. syntax.curly[pos].count++;
  1294. }
  1295. return p + 1;
  1296. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1297. int l;
  1298. char label[256];
  1299. p=skip_space(p2);
  1300. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1301. syntax.curly[syntax.curly_count].count = 1;
  1302. syntax.curly[syntax.curly_count].index = syntax.index++;
  1303. syntax.curly[syntax.curly_count].flag = 0;
  1304. // 現在地のラベル形成する
  1305. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1306. l=add_str(label);
  1307. set_label(l,script_pos,p);
  1308. syntax.curly_count++;
  1309. return p;
  1310. }
  1311. break;
  1312. case 'f':
  1313. case 'F':
  1314. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1315. int l;
  1316. char label[256];
  1317. int pos = syntax.curly_count;
  1318. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1319. syntax.curly[syntax.curly_count].count = 1;
  1320. syntax.curly[syntax.curly_count].index = syntax.index++;
  1321. syntax.curly[syntax.curly_count].flag = 0;
  1322. syntax.curly_count++;
  1323. p=skip_space(p2);
  1324. if(*p != '(')
  1325. disp_error_message("parse_syntax: need '('",p);
  1326. p++;
  1327. // 初期化文を実行する
  1328. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1329. p=parse_line(p);
  1330. syntax.curly_count--;
  1331. // 条件判断開始のラベル形成する
  1332. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1333. l=add_str(label);
  1334. set_label(l,script_pos,p);
  1335. p=skip_space(p);
  1336. if(*p == ';') {
  1337. // for(;;) のパターンなので必ず真
  1338. ;
  1339. } else {
  1340. // 条件が偽なら終了地点に飛ばす
  1341. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1342. add_scriptl(add_str("jump_zero"));
  1343. add_scriptc(C_ARG);
  1344. p=parse_expr(p);
  1345. p=skip_space(p);
  1346. add_scriptl(add_str(label));
  1347. add_scriptc(C_FUNC);
  1348. }
  1349. if(*p != ';')
  1350. disp_error_message("parse_syntax: need ';'",p);
  1351. p++;
  1352. // ループ開始に飛ばす
  1353. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1354. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1355. parse_line(label);
  1356. syntax.curly_count--;
  1357. // 次のループへのラベル形成する
  1358. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1359. l=add_str(label);
  1360. set_label(l,script_pos,p);
  1361. // 次のループに入る時の処理
  1362. // for 最後の ')' を ';' として扱うフラグ
  1363. parse_syntax_for_flag = 1;
  1364. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1365. p=parse_line(p);
  1366. syntax.curly_count--;
  1367. parse_syntax_for_flag = 0;
  1368. // 条件判定処理に飛ばす
  1369. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1370. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1371. parse_line(label);
  1372. syntax.curly_count--;
  1373. // ループ開始のラベル付け
  1374. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1375. l=add_str(label);
  1376. set_label(l,script_pos,p);
  1377. return p;
  1378. }
  1379. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1380. {// internal script function
  1381. const char *func_name;
  1382. func_name = skip_space(p2);
  1383. p = skip_word(func_name);
  1384. if( p == func_name )
  1385. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1386. p2 = skip_space(p);
  1387. if( *p2 == ';' )
  1388. {// function <name> ;
  1389. // function declaration - just register the name
  1390. int l;
  1391. l = add_word(func_name);
  1392. if( str_data[l].type == C_NOP )// set type only if the name did not exist before
  1393. str_data[l].type = C_USERFUNC;
  1394. // if, for , while の閉じ判定
  1395. p = parse_syntax_close(p2 + 1);
  1396. return p; }
  1397. else if(*p2 == '{')
  1398. {// function <name> <line/block of code>
  1399. char label[256];
  1400. int l;
  1401. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1402. syntax.curly[syntax.curly_count].count = 1;
  1403. syntax.curly[syntax.curly_count].index = syntax.index++;
  1404. syntax.curly[syntax.curly_count].flag = 0;
  1405. ++syntax.curly_count;
  1406. // Jump over the function code
  1407. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1408. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1409. ++syntax.curly_count;
  1410. parse_line(label);
  1411. --syntax.curly_count;
  1412. // Set the position of the function (label)
  1413. l=add_word(func_name);
  1414. if( str_data[l].type == C_NOP )// set type only if the name did not exist before
  1415. str_data[l].type = C_USERFUNC;
  1416. set_label(l, script_pos, p);
  1417. if( parse_options&SCRIPT_USE_LABEL_DB )
  1418. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1419. return skip_space(p);
  1420. }
  1421. else
  1422. {
  1423. disp_error_message("expect ';' or '{' at function syntax",p);
  1424. }
  1425. }
  1426. break;
  1427. case 'i':
  1428. case 'I':
  1429. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1430. // if() の処理
  1431. char label[256];
  1432. p=skip_space(p2);
  1433. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1434. disp_error_message("need '('",p);
  1435. }
  1436. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1437. syntax.curly[syntax.curly_count].count = 1;
  1438. syntax.curly[syntax.curly_count].index = syntax.index++;
  1439. syntax.curly[syntax.curly_count].flag = 0;
  1440. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1441. syntax.curly_count++;
  1442. add_scriptl(add_str("jump_zero"));
  1443. add_scriptc(C_ARG);
  1444. p=parse_expr(p);
  1445. p=skip_space(p);
  1446. add_scriptl(add_str(label));
  1447. add_scriptc(C_FUNC);
  1448. return p;
  1449. }
  1450. break;
  1451. case 's':
  1452. case 'S':
  1453. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1454. // switch() の処理
  1455. char label[256];
  1456. p=skip_space(p2);
  1457. if(*p != '(') {
  1458. disp_error_message("need '('",p);
  1459. }
  1460. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1461. syntax.curly[syntax.curly_count].count = 1;
  1462. syntax.curly[syntax.curly_count].index = syntax.index++;
  1463. syntax.curly[syntax.curly_count].flag = 0;
  1464. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1465. syntax.curly_count++;
  1466. add_scriptl(add_str("set"));
  1467. add_scriptc(C_ARG);
  1468. add_scriptl(add_str(label));
  1469. p=parse_expr(p);
  1470. p=skip_space(p);
  1471. if(*p != '{') {
  1472. disp_error_message("parse_syntax: need '{'",p);
  1473. }
  1474. add_scriptc(C_FUNC);
  1475. return p + 1;
  1476. }
  1477. break;
  1478. case 'w':
  1479. case 'W':
  1480. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1481. int l;
  1482. char label[256];
  1483. p=skip_space(p2);
  1484. if(*p != '(') {
  1485. disp_error_message("need '('",p);
  1486. }
  1487. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1488. syntax.curly[syntax.curly_count].count = 1;
  1489. syntax.curly[syntax.curly_count].index = syntax.index++;
  1490. syntax.curly[syntax.curly_count].flag = 0;
  1491. // 条件判断開始のラベル形成する
  1492. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1493. l=add_str(label);
  1494. set_label(l,script_pos,p);
  1495. // 条件が偽なら終了地点に飛ばす
  1496. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1497. syntax.curly_count++;
  1498. add_scriptl(add_str("jump_zero"));
  1499. add_scriptc(C_ARG);
  1500. p=parse_expr(p);
  1501. p=skip_space(p);
  1502. add_scriptl(add_str(label));
  1503. add_scriptc(C_FUNC);
  1504. return p;
  1505. }
  1506. break;
  1507. }
  1508. return NULL;
  1509. }
  1510. const char* parse_syntax_close(const char *p) {
  1511. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1512. int flag;
  1513. do {
  1514. p = parse_syntax_close_sub(p,&flag);
  1515. } while(flag);
  1516. return p;
  1517. }
  1518. // if, for , while , do の閉じ判定
  1519. // flag == 1 : 閉じられた
  1520. // flag == 0 : 閉じられない
  1521. const char* parse_syntax_close_sub(const char* p,int* flag)
  1522. {
  1523. char label[256];
  1524. int pos = syntax.curly_count - 1;
  1525. int l;
  1526. *flag = 1;
  1527. if(syntax.curly_count <= 0) {
  1528. *flag = 0;
  1529. return p;
  1530. } else if(syntax.curly[pos].type == TYPE_IF) {
  1531. const char *bp = p;
  1532. const char *p2;
  1533. // if 最終場所へ飛ばす
  1534. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1535. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1536. parse_line(label);
  1537. syntax.curly_count--;
  1538. // 現在地のラベルを付ける
  1539. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1540. l=add_str(label);
  1541. set_label(l,script_pos,p);
  1542. syntax.curly[pos].count++;
  1543. p = skip_space(p);
  1544. p2 = skip_word(p);
  1545. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1546. // else or else - if
  1547. p = skip_space(p2);
  1548. p2 = skip_word(p);
  1549. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1550. // else - if
  1551. p=skip_space(p2);
  1552. if(*p != '(') {
  1553. disp_error_message("need '('",p);
  1554. }
  1555. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1556. add_scriptl(add_str("jump_zero"));
  1557. add_scriptc(C_ARG);
  1558. p=parse_expr(p);
  1559. p=skip_space(p);
  1560. add_scriptl(add_str(label));
  1561. add_scriptc(C_FUNC);
  1562. *flag = 0;
  1563. return p;
  1564. } else {
  1565. // else
  1566. if(!syntax.curly[pos].flag) {
  1567. syntax.curly[pos].flag = 1;
  1568. *flag = 0;
  1569. return p;
  1570. }
  1571. }
  1572. }
  1573. // if 閉じ
  1574. syntax.curly_count--;
  1575. // 最終地のラベルを付ける
  1576. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1577. l=add_str(label);
  1578. set_label(l,script_pos,p);
  1579. if(syntax.curly[pos].flag == 1) {
  1580. // このifに対するelseじゃないのでポインタの位置は同じ
  1581. return bp;
  1582. }
  1583. return p;
  1584. } else if(syntax.curly[pos].type == TYPE_DO) {
  1585. int l;
  1586. char label[256];
  1587. const char *p2;
  1588. if(syntax.curly[pos].flag) {
  1589. // 現在地のラベル形成する(continue でここに来る)
  1590. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1591. l=add_str(label);
  1592. set_label(l,script_pos,p);
  1593. }
  1594. // 条件が偽なら終了地点に飛ばす
  1595. p = skip_space(p);
  1596. p2 = skip_word(p);
  1597. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1598. disp_error_message("parse_syntax: need 'while'",p);
  1599. p = skip_space(p2);
  1600. if(*p != '(') {
  1601. disp_error_message("need '('",p);
  1602. }
  1603. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1604. add_scriptl(add_str("jump_zero"));
  1605. add_scriptc(C_ARG);
  1606. p=parse_expr(p);
  1607. p=skip_space(p);
  1608. add_scriptl(add_str(label));
  1609. add_scriptc(C_FUNC);
  1610. // 開始地点に飛ばす
  1611. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1612. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1613. parse_line(label);
  1614. syntax.curly_count--;
  1615. // 条件終了地点のラベル形成する
  1616. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1617. l=add_str(label);
  1618. set_label(l,script_pos,p);
  1619. p = skip_space(p);
  1620. if(*p != ';') {
  1621. disp_error_message("parse_syntax: need ';'",p);
  1622. return p+1;
  1623. }
  1624. p++;
  1625. syntax.curly_count--;
  1626. return p;
  1627. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1628. // 次のループに飛ばす
  1629. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1630. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1631. parse_line(label);
  1632. syntax.curly_count--;
  1633. // for 終了のラベル付け
  1634. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1635. l=add_str(label);
  1636. set_label(l,script_pos,p);
  1637. syntax.curly_count--;
  1638. return p;
  1639. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1640. // while 条件判断へ飛ばす
  1641. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1642. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1643. parse_line(label);
  1644. syntax.curly_count--;
  1645. // while 終了のラベル付け
  1646. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1647. l=add_str(label);
  1648. set_label(l,script_pos,p);
  1649. syntax.curly_count--;
  1650. return p;
  1651. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1652. int pos = syntax.curly_count-1;
  1653. char label[256];
  1654. int l;
  1655. // 戻す
  1656. sprintf(label,"return;");
  1657. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1658. parse_line(label);
  1659. syntax.curly_count--;
  1660. // 現在地のラベルを付ける
  1661. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1662. l=add_str(label);
  1663. set_label(l,script_pos,p);
  1664. syntax.curly_count--;
  1665. return p;
  1666. } else {
  1667. *flag = 0;
  1668. return p;
  1669. }
  1670. }
  1671. /*==========================================
  1672. * 組み込み関数の追加
  1673. *------------------------------------------*/
  1674. static void add_buildin_func(void)
  1675. {
  1676. int i,n;
  1677. const char* p;
  1678. for( i = 0; buildin_func[i].func; i++ )
  1679. {
  1680. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1681. // 'v' - value (either string or int or reference)
  1682. // 's' - string
  1683. // 'i' - int
  1684. // 'r' - reference (of a variable)
  1685. // 'l' - label
  1686. // '?' - one optional parameter
  1687. // '*' - unknown number of optional parameters
  1688. p = buildin_func[i].arg;
  1689. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1690. while( *p == '?' ) ++p;
  1691. if( *p == '*' ) ++p;
  1692. if( *p != 0){
  1693. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1694. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1695. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1696. } else {
  1697. n = add_str(buildin_func[i].name);
  1698. str_data[n].type = C_FUNC;
  1699. str_data[n].val = i;
  1700. str_data[n].func = buildin_func[i].func;
  1701. }
  1702. }
  1703. }
  1704. /*==========================================
  1705. * 定数データベースの読み込み
  1706. *------------------------------------------*/
  1707. static void read_constdb(void)
  1708. {
  1709. FILE *fp;
  1710. char line[1024],name[1024],val[1024];
  1711. int n,type;
  1712. sprintf(line, "%s/const.txt", db_path);
  1713. fp=fopen(line, "r");
  1714. if(fp==NULL){
  1715. ShowError("can't read %s\n", line);
  1716. return ;
  1717. }
  1718. while(fgets(line, sizeof(line), fp))
  1719. {
  1720. if(line[0]=='/' && line[1]=='/')
  1721. continue;
  1722. type=0;
  1723. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1724. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1725. n=add_str(name);
  1726. if(type==0)
  1727. str_data[n].type=C_INT;
  1728. else
  1729. str_data[n].type=C_PARAM;
  1730. str_data[n].val= (int)strtol(val,NULL,0);
  1731. }
  1732. }
  1733. fclose(fp);
  1734. }
  1735. /*==========================================
  1736. * エラー表示
  1737. *------------------------------------------*/
  1738. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1739. {
  1740. int i;
  1741. if( p == NULL || !p[0] ) return NULL;
  1742. if( line < 0 )
  1743. StringBuf_Printf(buf, "*% 5d : ", -line);
  1744. else
  1745. StringBuf_Printf(buf, " % 5d : ", line);
  1746. for(i=0;p[i] && p[i] != '\n';i++){
  1747. if(p + i != mark)
  1748. StringBuf_Printf(buf, "%c", p[i]);
  1749. else
  1750. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1751. }
  1752. StringBuf_AppendStr(buf, "\n");
  1753. return p+i+(p[i] == '\n' ? 1 : 0);
  1754. }
  1755. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1756. {
  1757. // エラーが発生した行を求める
  1758. int j;
  1759. int line = start_line;
  1760. const char *p;
  1761. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1762. StringBuf buf;
  1763. for(p=src;p && *p;line++){
  1764. const char *lineend=strchr(p,'\n');
  1765. if(lineend==NULL || error_pos<lineend){
  1766. break;
  1767. }
  1768. for( j = 0; j < 4; j++ ) {
  1769. linestart[j] = linestart[j+1];
  1770. }
  1771. linestart[4] = p;
  1772. p=lineend+1;
  1773. }
  1774. StringBuf_Init(&buf);
  1775. StringBuf_AppendStr(&buf, "\a\n");
  1776. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1777. StringBuf_Printf(&buf, " %s\n", error_msg);
  1778. for(j = 0; j < 5; j++ ) {
  1779. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1780. }
  1781. p = script_print_line(&buf, p, error_pos, -line);
  1782. for(j = 0; j < 5; j++) {
  1783. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1784. }
  1785. ShowError("%s", StringBuf_Value(&buf));
  1786. StringBuf_Destroy(&buf);
  1787. }
  1788. /*==========================================
  1789. * スクリプトの解析
  1790. *------------------------------------------*/
  1791. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1792. {
  1793. const char *p,*tmpp;
  1794. int i;
  1795. struct script_code* code = NULL;
  1796. static int first=1;
  1797. char end;
  1798. if( src == NULL )
  1799. return NULL;// empty script
  1800. memset(&syntax,0,sizeof(syntax));
  1801. if(first){
  1802. add_buildin_func();
  1803. read_constdb();
  1804. first=0;
  1805. }
  1806. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1807. script_pos=0;
  1808. script_size=SCRIPT_BLOCK_SIZE;
  1809. str_data[LABEL_NEXTLINE].type=C_NOP;
  1810. str_data[LABEL_NEXTLINE].backpatch=-1;
  1811. str_data[LABEL_NEXTLINE].label=-1;
  1812. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1813. if( options&SCRIPT_USE_LABEL_DB )
  1814. scriptlabel_db->clear(scriptlabel_db, NULL);
  1815. parse_options = options;
  1816. if( setjmp( error_jump ) != 0 ) {
  1817. //Restore program state when script has problems. [from jA]
  1818. int i;
  1819. const int size = ARRAYLENGTH(syntax.curly);
  1820. if( error_report )
  1821. script_error(src,file,line,error_msg,error_pos);
  1822. aFree( error_msg );
  1823. aFree( script_buf );
  1824. script_pos = 0;
  1825. script_size = 0;
  1826. script_buf = NULL;
  1827. for(i=LABEL_START;i<str_num;i++)
  1828. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1829. for(i=0; i<size; i++)
  1830. linkdb_final(&syntax.curly[i].case_label);
  1831. return NULL;
  1832. }
  1833. parse_syntax_for_flag=0;
  1834. p=src;
  1835. p=skip_space(p);
  1836. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1837. {// does not require brackets around the script
  1838. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1839. {// empty script and can return NULL
  1840. aFree( script_buf );
  1841. script_pos = 0;
  1842. script_size = 0;
  1843. script_buf = NULL;
  1844. return NULL;
  1845. }
  1846. end = '\0';
  1847. }
  1848. else
  1849. {// requires brackets around the script
  1850. if( *p != '{' )
  1851. disp_error_message("not found '{'",p);
  1852. p = skip_space(p+1);
  1853. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1854. {// empty script and can return NULL
  1855. aFree( script_buf );
  1856. script_pos = 0;
  1857. script_size = 0;
  1858. script_buf = NULL;
  1859. return NULL;
  1860. }
  1861. end = '}';
  1862. }
  1863. // clear references of labels, variables and internal functions
  1864. for(i=LABEL_START;i<str_num;i++){
  1865. if(
  1866. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1867. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1868. ){
  1869. str_data[i].type=C_NOP;
  1870. str_data[i].backpatch=-1;
  1871. str_data[i].label=-1;
  1872. }
  1873. }
  1874. while( syntax.curly_count != 0 || *p != end )
  1875. {
  1876. if( *p == '\0' )
  1877. disp_error_message("unexpected end of script",p);
  1878. // labelだけ特殊処理
  1879. tmpp=skip_space(skip_word(p));
  1880. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1881. i=add_word(p);
  1882. set_label(i,script_pos,p);
  1883. if( parse_options&SCRIPT_USE_LABEL_DB )
  1884. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1885. p=tmpp+1;
  1886. p=skip_space(p);
  1887. continue;
  1888. }
  1889. // 他は全部一緒くた
  1890. p=parse_line(p);
  1891. p=skip_space(p);
  1892. add_scriptc(C_EOL);
  1893. set_label(LABEL_NEXTLINE,script_pos,p);
  1894. str_data[LABEL_NEXTLINE].type=C_NOP;
  1895. str_data[LABEL_NEXTLINE].backpatch=-1;
  1896. str_data[LABEL_NEXTLINE].label=-1;
  1897. }
  1898. add_scriptc(C_NOP);
  1899. // trim code to size
  1900. script_size = script_pos;
  1901. RECREATE(script_buf,unsigned char,script_pos);
  1902. // default unknown references to variables
  1903. for(i=LABEL_START;i<str_num;i++){
  1904. if(str_data[i].type==C_NOP){
  1905. int j,next;
  1906. str_data[i].type=C_NAME;
  1907. str_data[i].label=i;
  1908. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1909. next=GETVALUE(script_buf,j);
  1910. SETVALUE(script_buf,j,i);
  1911. j=next;
  1912. }
  1913. }
  1914. }
  1915. #ifdef DEBUG_DISP
  1916. for(i=0;i<script_pos;i++){
  1917. if((i&15)==0) printf("%04x : ",i);
  1918. ShowMessage("%02x ",script_buf[i]);
  1919. if((i&15)==15) printf("\n");
  1920. }
  1921. ShowMessage("\n");
  1922. #endif
  1923. #ifdef DEBUG_DISASM
  1924. {
  1925. int i = 0,j;
  1926. while(i < script_pos) {
  1927. c_op op = get_com(script_buf,&i);
  1928. ShowMessage("%06x %s", i, script_op2name(op));
  1929. j = i;
  1930. switch(op) {
  1931. case C_INT:
  1932. ShowMessage(" %d", get_num(script_buf,&i));
  1933. break;
  1934. case C_POS:
  1935. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  1936. i += 3;
  1937. break;
  1938. case C_NAME:
  1939. j = (*(int*)(script_buf+i)&0xffffff);
  1940. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  1941. i += 3;
  1942. break;
  1943. case C_STR:
  1944. j = strlen(script_buf + i);
  1945. ShowMessage(" %s", script_buf + i);
  1946. i += j+1;
  1947. break;
  1948. }
  1949. ShowMessage(CL_CLL"\n");
  1950. }
  1951. }
  1952. #endif
  1953. CREATE(code,struct script_code,1);
  1954. code->script_buf = script_buf;
  1955. code->script_size = script_size;
  1956. code->script_vars = NULL;
  1957. return code;
  1958. }
  1959. /// Returns the player attached to this script, identified by the rid.
  1960. /// If there is no player attached, the script is terminated.
  1961. TBL_PC *script_rid2sd(struct script_state *st)
  1962. {
  1963. TBL_PC *sd=map_id2sd(st->rid);
  1964. if(!sd){
  1965. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1966. script_reportfunc(st);
  1967. script_reportsrc(st);
  1968. st->state = END;
  1969. }
  1970. return sd;
  1971. }
  1972. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1973. ///
  1974. /// @param st Script state
  1975. /// @param data Variable/constant
  1976. void get_val(struct script_state* st, struct script_data* data)
  1977. {
  1978. const char* name;
  1979. char prefix;
  1980. char postfix;
  1981. TBL_PC* sd = NULL;
  1982. if( !data_isreference(data) )
  1983. return;// not a variable/constant
  1984. name = reference_getname(data);
  1985. prefix = name[0];
  1986. postfix = name[strlen(name) - 1];
  1987. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1988. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1989. {
  1990. sd = script_rid2sd(st);
  1991. if( sd == NULL )
  1992. {// needs player attached
  1993. if( postfix == '$' )
  1994. {// string variable
  1995. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1996. data->type = C_CONSTSTR;
  1997. data->u.str = "";
  1998. }
  1999. else
  2000. {// integer variable
  2001. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2002. data->type = C_INT;
  2003. data->u.num = 0;
  2004. }
  2005. return;
  2006. }
  2007. }
  2008. if( postfix == '$' )
  2009. {// string variable
  2010. switch( prefix )
  2011. {
  2012. case '@':
  2013. data->u.str = pc_readregstr(sd, data->u.num);
  2014. break;
  2015. case '$':
  2016. data->u.str = mapreg_readregstr(data->u.num);
  2017. break;
  2018. case '#':
  2019. if( name[1] == '#' )
  2020. data->u.str = pc_readaccountreg2str(sd, name);// global
  2021. else
  2022. data->u.str = pc_readaccountregstr(sd, name);// local
  2023. break;
  2024. case '.':
  2025. {
  2026. struct linkdb_node** n =
  2027. data->ref ? data->ref:
  2028. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2029. &st->script->script_vars;// npc variable
  2030. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2031. }
  2032. break;
  2033. case '\'':
  2034. {
  2035. struct linkdb_node** n = NULL;
  2036. if( st->instance_id )
  2037. n = &instance[st->instance_id].svar;
  2038. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2039. }
  2040. break;
  2041. default:
  2042. data->u.str = pc_readglobalreg_str(sd, name);
  2043. break;
  2044. }
  2045. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2046. {// empty string
  2047. data->type = C_CONSTSTR;
  2048. data->u.str = "";
  2049. }
  2050. else
  2051. {// duplicate string
  2052. data->type = C_STR;
  2053. data->u.str = aStrdup(data->u.str);
  2054. }
  2055. }
  2056. else
  2057. {// integer variable
  2058. data->type = C_INT;
  2059. if( reference_toconstant(data) )
  2060. {
  2061. data->u.num = reference_getconstant(data);
  2062. }
  2063. else if( reference_toparam(data) )
  2064. {
  2065. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2066. }
  2067. else
  2068. switch( prefix )
  2069. {
  2070. case '@':
  2071. data->u.num = pc_readreg(sd, data->u.num);
  2072. break;
  2073. case '$':
  2074. data->u.num = mapreg_readreg(data->u.num);
  2075. break;
  2076. case '#':
  2077. if( name[1] == '#' )
  2078. data->u.num = pc_readaccountreg2(sd, name);// global
  2079. else
  2080. data->u.num = pc_readaccountreg(sd, name);// local
  2081. break;
  2082. case '.':
  2083. {
  2084. struct linkdb_node** n =
  2085. data->ref ? data->ref:
  2086. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2087. &st->script->script_vars;// npc variable
  2088. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2089. }
  2090. break;
  2091. case '\'':
  2092. {
  2093. struct linkdb_node** n = NULL;
  2094. if( st->instance_id )
  2095. n = &instance[st->instance_id].ivar;
  2096. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2097. }
  2098. break;
  2099. default:
  2100. data->u.num = pc_readglobalreg(sd, name);
  2101. break;
  2102. }
  2103. }
  2104. return;
  2105. }
  2106. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2107. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2108. /// The value is left in the top of the stack and needs to be removed manually.
  2109. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2110. {
  2111. struct script_data* data;
  2112. push_val2(st->stack, C_NAME, uid, ref);
  2113. data = script_getdatatop(st, -1);
  2114. get_val(st, data);
  2115. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2116. }
  2117. /*==========================================
  2118. * Stores the value of a script variable
  2119. * Return value is 0 on fail, 1 on success.
  2120. *------------------------------------------*/
  2121. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2122. {
  2123. char prefix = name[0];
  2124. if( is_string_variable(name) )
  2125. {// string variable
  2126. const char* str = (const char*)value;
  2127. switch (prefix) {
  2128. case '@':
  2129. return pc_setregstr(sd, num, str);
  2130. case '$':
  2131. return mapreg_setregstr(num, str);
  2132. case '#':
  2133. return (name[1] == '#') ?
  2134. pc_setaccountreg2str(sd, name, str) :
  2135. pc_setaccountregstr(sd, name, str);
  2136. case '.': {
  2137. char* p;
  2138. struct linkdb_node** n;
  2139. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2140. p = (char*)linkdb_erase(n, (void*)num);
  2141. if (p) aFree(p);
  2142. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2143. }
  2144. return 1;
  2145. case '\'': {
  2146. char *p;
  2147. struct linkdb_node** n = NULL;
  2148. if( st->instance_id )
  2149. n = &instance[st->instance_id].svar;
  2150. p = (char*)linkdb_erase(n, (void*)num);
  2151. if (p) aFree(p);
  2152. if( str[0] ) linkdb_insert(n, (void*)num, aStrdup(str));
  2153. }
  2154. return 1;
  2155. default:
  2156. return pc_setglobalreg_str(sd, name, str);
  2157. }
  2158. }
  2159. else
  2160. {// integer variable
  2161. int val = (int)value;
  2162. if(str_data[num&0x00ffffff].type == C_PARAM)
  2163. {
  2164. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2165. {
  2166. if( st != NULL )
  2167. {
  2168. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2169. script_reportsrc(st);
  2170. st->state = END;
  2171. }
  2172. return 0;
  2173. }
  2174. return 1;
  2175. }
  2176. switch (prefix) {
  2177. case '@':
  2178. return pc_setreg(sd, num, val);
  2179. case '$':
  2180. return mapreg_setreg(num, val);
  2181. case '#':
  2182. return (name[1] == '#') ?
  2183. pc_setaccountreg2(sd, name, val) :
  2184. pc_setaccountreg(sd, name, val);
  2185. case '.': {
  2186. struct linkdb_node** n;
  2187. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2188. if (val == 0)
  2189. linkdb_erase(n, (void*)num);
  2190. else
  2191. linkdb_replace(n, (void*)num, (void*)val);
  2192. }
  2193. return 1;
  2194. case '\'':
  2195. {
  2196. struct linkdb_node** n = NULL;
  2197. if( st->instance_id )
  2198. n = &instance[st->instance_id].ivar;
  2199. if( val == 0 )
  2200. linkdb_erase(n, (void*)num);
  2201. else
  2202. linkdb_replace(n, (void*)num, (void*)val);
  2203. return 1;
  2204. }
  2205. default:
  2206. return pc_setglobalreg(sd, name, val);
  2207. }
  2208. }
  2209. }
  2210. int set_var(TBL_PC* sd, char* name, void* val)
  2211. {
  2212. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2213. }
  2214. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2215. {
  2216. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2217. }
  2218. /// Converts the data to a string
  2219. const char* conv_str(struct script_state* st, struct script_data* data)
  2220. {
  2221. char* p;
  2222. get_val(st, data);
  2223. if( data_isstring(data) )
  2224. {// nothing to convert
  2225. }
  2226. else if( data_isint(data) )
  2227. {// int -> string
  2228. CREATE(p, char, ITEM_NAME_LENGTH);
  2229. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2230. p[ITEM_NAME_LENGTH-1] = '\0';
  2231. data->type = C_STR;
  2232. data->u.str = p;
  2233. }
  2234. else if( data_isreference(data) )
  2235. {// reference -> string
  2236. //##TODO when does this happen (check get_val) [FlavioJS]
  2237. data->type = C_CONSTSTR;
  2238. data->u.str = reference_getname(data);
  2239. }
  2240. else
  2241. {// unsupported data type
  2242. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2243. script_reportdata(data);
  2244. script_reportsrc(st);
  2245. data->type = C_CONSTSTR;
  2246. data->u.str = "";
  2247. }
  2248. return data->u.str;
  2249. }
  2250. /// Converts the data to an int
  2251. int conv_num(struct script_state* st, struct script_data* data)
  2252. {
  2253. char* p;
  2254. long num;
  2255. get_val(st, data);
  2256. if( data_isint(data) )
  2257. {// nothing to convert
  2258. }
  2259. else if( data_isstring(data) )
  2260. {// string -> int
  2261. // the result does not overflow or underflow, it is capped instead
  2262. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2263. p = data->u.str;
  2264. errno = 0;
  2265. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2266. if( errno == ERANGE
  2267. #if LONG_MAX > INT_MAX
  2268. || num < INT_MIN || num > INT_MAX
  2269. #endif
  2270. )
  2271. {
  2272. if( num <= INT_MIN )
  2273. {
  2274. num = INT_MIN;
  2275. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2276. }
  2277. else//if( num >= INT_MAX )
  2278. {
  2279. num = INT_MAX;
  2280. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2281. }
  2282. script_reportdata(data);
  2283. script_reportsrc(st);
  2284. }
  2285. if( data->type == C_STR )
  2286. aFree(p);
  2287. data->type = C_INT;
  2288. data->u.num = (int)num;
  2289. }
  2290. #if 0
  2291. // FIXME this function is being used to retrieve the position of labels and
  2292. // probably other stuff [FlavioJS]
  2293. else
  2294. {// unsupported data type
  2295. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2296. script_reportdata(data);
  2297. script_reportsrc(st);
  2298. data->type = C_INT;
  2299. data->u.num = 0;
  2300. }
  2301. #endif
  2302. return data->u.num;
  2303. }
  2304. //
  2305. // Stack operations
  2306. //
  2307. /// Increases the size of the stack
  2308. void stack_expand(struct script_stack* stack)
  2309. {
  2310. stack->sp_max += 64;
  2311. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2312. stack->sp_max * sizeof(stack->stack_data[0]) );
  2313. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2314. 64 * sizeof(stack->stack_data[0]) );
  2315. }
  2316. /// Pushes a value into the stack
  2317. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2318. /// Pushes a value into the stack (with reference)
  2319. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2320. {
  2321. if( stack->sp >= stack->sp_max )
  2322. stack_expand(stack);
  2323. stack->stack_data[stack->sp].type = type;
  2324. stack->stack_data[stack->sp].u.num = val;
  2325. stack->stack_data[stack->sp].ref = ref;
  2326. stack->sp++;
  2327. return &stack->stack_data[stack->sp-1];
  2328. }
  2329. /// Pushes a string into the stack
  2330. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2331. {
  2332. if( stack->sp >= stack->sp_max )
  2333. stack_expand(stack);
  2334. stack->stack_data[stack->sp].type = type;
  2335. stack->stack_data[stack->sp].u.str = str;
  2336. stack->stack_data[stack->sp].ref = NULL;
  2337. stack->sp++;
  2338. return &stack->stack_data[stack->sp-1];
  2339. }
  2340. /// Pushes a retinfo into the stack
  2341. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2342. {
  2343. if( stack->sp >= stack->sp_max )
  2344. stack_expand(stack);
  2345. stack->stack_data[stack->sp].type = C_RETINFO;
  2346. stack->stack_data[stack->sp].u.ri = ri;
  2347. stack->stack_data[stack->sp].ref = NULL;
  2348. stack->sp++;
  2349. return &stack->stack_data[stack->sp-1];
  2350. }
  2351. /// Pushes a copy of the target position into the stack
  2352. struct script_data* push_copy(struct script_stack* stack, int pos)
  2353. {
  2354. switch( stack->stack_data[pos].type )
  2355. {
  2356. case C_CONSTSTR:
  2357. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2358. break;
  2359. case C_STR:
  2360. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2361. break;
  2362. case C_RETINFO:
  2363. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2364. exit(1);
  2365. break;
  2366. default:
  2367. return push_val2(
  2368. stack,stack->stack_data[pos].type,
  2369. stack->stack_data[pos].u.num,
  2370. stack->stack_data[pos].ref
  2371. );
  2372. break;
  2373. }
  2374. }
  2375. /// Removes the values in indexes [start,end[ from the stack.
  2376. /// Adjusts all stack pointers.
  2377. void pop_stack(struct script_state* st, int start, int end)
  2378. {
  2379. struct script_stack* stack = st->stack;
  2380. struct script_data* data;
  2381. int i;
  2382. if( start < 0 )
  2383. start = 0;
  2384. if( end > stack->sp )
  2385. end = stack->sp;
  2386. if( start >= end )
  2387. return;// nothing to pop
  2388. // free stack elements
  2389. for( i = start; i < end; i++ )
  2390. {
  2391. data = &stack->stack_data[i];
  2392. if( data->type == C_STR )
  2393. aFree(data->u.str);
  2394. if( data->type == C_RETINFO )
  2395. {
  2396. struct script_retinfo* ri = data->u.ri;
  2397. if( ri->var_function )
  2398. {
  2399. script_free_vars(ri->var_function);
  2400. aFree(ri->var_function);
  2401. }
  2402. aFree(ri);
  2403. }
  2404. data->type = C_NOP;
  2405. }
  2406. // move the rest of the elements
  2407. if( stack->sp > end )
  2408. {
  2409. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2410. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2411. stack->stack_data[i].type = C_NOP;
  2412. }
  2413. // adjust stack pointers
  2414. if( st->start > end ) st->start -= end - start;
  2415. else if( st->start > start ) st->start = start;
  2416. if( st->end > end ) st->end -= end - start;
  2417. else if( st->end > start ) st->end = start;
  2418. if( stack->defsp > end ) stack->defsp -= end - start;
  2419. else if( stack->defsp > start ) stack->defsp = start;
  2420. stack->sp -= end - start;
  2421. }
  2422. ///
  2423. ///
  2424. ///
  2425. /*==========================================
  2426. * スクリプト依存変数、関数依存変数の解放
  2427. *------------------------------------------*/
  2428. void script_free_vars(struct linkdb_node **node)
  2429. {
  2430. struct linkdb_node* n = *node;
  2431. while( n != NULL)
  2432. {
  2433. const char* name = get_str((int)(n->key)&0x00ffffff);
  2434. if( is_string_variable(name) )
  2435. aFree(n->data); // 文字型変数なので、データ削除
  2436. n = n->next;
  2437. }
  2438. linkdb_final( node );
  2439. }
  2440. void script_free_code(struct script_code* code)
  2441. {
  2442. script_free_vars( &code->script_vars );
  2443. aFree( code->script_buf );
  2444. aFree( code );
  2445. }
  2446. /// Creates a new script state.
  2447. ///
  2448. /// @param script Script code
  2449. /// @param pos Position in the code
  2450. /// @param rid Who is running the script (attached player)
  2451. /// @param oid Where the code is being run (npc 'object')
  2452. /// @return Script state
  2453. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2454. {
  2455. struct script_state* st;
  2456. CREATE(st, struct script_state, 1);
  2457. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2458. st->stack->sp = 0;
  2459. st->stack->sp_max = 64;
  2460. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2461. st->stack->defsp = st->stack->sp;
  2462. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2463. st->state = RUN;
  2464. st->script = script;
  2465. //st->scriptroot = script;
  2466. st->pos = pos;
  2467. st->rid = rid;
  2468. st->oid = oid;
  2469. st->sleep.timer = INVALID_TIMER;
  2470. return st;
  2471. }
  2472. /// Frees a script state.
  2473. ///
  2474. /// @param st Script state
  2475. void script_free_state(struct script_state* st)
  2476. {
  2477. if(st->bk_st)
  2478. {// backup was not restored
  2479. 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);
  2480. }
  2481. if( st->sleep.timer != INVALID_TIMER )
  2482. delete_timer(st->sleep.timer, run_script_timer);
  2483. script_free_vars(st->stack->var_function);
  2484. aFree(st->stack->var_function);
  2485. pop_stack(st, 0, st->stack->sp);
  2486. aFree(st->stack->stack_data);
  2487. aFree(st->stack);
  2488. st->pos = -1;
  2489. aFree(st);
  2490. }
  2491. //
  2492. // 実行部main
  2493. //
  2494. /*==========================================
  2495. * コマンドの読み取り
  2496. *------------------------------------------*/
  2497. c_op get_com(unsigned char *script,int *pos)
  2498. {
  2499. int i = 0, j = 0;
  2500. if(script[*pos]>=0x80){
  2501. return C_INT;
  2502. }
  2503. while(script[*pos]>=0x40){
  2504. i=script[(*pos)++]<<j;
  2505. j+=6;
  2506. }
  2507. return (c_op)(i+(script[(*pos)++]<<j));
  2508. }
  2509. /*==========================================
  2510. * 数値の所得
  2511. *------------------------------------------*/
  2512. int get_num(unsigned char *script,int *pos)
  2513. {
  2514. int i,j;
  2515. i=0; j=0;
  2516. while(script[*pos]>=0xc0){
  2517. i+=(script[(*pos)++]&0x7f)<<j;
  2518. j+=6;
  2519. }
  2520. return i+((script[(*pos)++]&0x7f)<<j);
  2521. }
  2522. /*==========================================
  2523. * スタックから値を取り出す
  2524. *------------------------------------------*/
  2525. int pop_val(struct script_state* st)
  2526. {
  2527. if(st->stack->sp<=0)
  2528. return 0;
  2529. st->stack->sp--;
  2530. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2531. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2532. return st->stack->stack_data[st->stack->sp].u.num;
  2533. return 0;
  2534. }
  2535. /// Ternary operators
  2536. /// test ? if_true : if_false
  2537. void op_3(struct script_state* st, int op)
  2538. {
  2539. struct script_data* data;
  2540. int flag = 0;
  2541. data = script_getdatatop(st, -3);
  2542. get_val(st, data);
  2543. if( data_isstring(data) )
  2544. flag = data->u.str[0];// "" -> false
  2545. else if( data_isint(data) )
  2546. flag = data->u.num;// 0 -> false
  2547. else
  2548. {
  2549. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2550. script_reportdata(data);
  2551. script_reportsrc(st);
  2552. script_removetop(st, -3, 0);
  2553. script_pushnil(st);
  2554. return;
  2555. }
  2556. if( flag )
  2557. script_pushcopytop(st, -2);
  2558. else
  2559. script_pushcopytop(st, -1);
  2560. script_removetop(st, -4, -1);
  2561. }
  2562. /// Binary string operators
  2563. /// s1 EQ s2 -> i
  2564. /// s1 NE s2 -> i
  2565. /// s1 GT s2 -> i
  2566. /// s1 GE s2 -> i
  2567. /// s1 LT s2 -> i
  2568. /// s1 LE s2 -> i
  2569. /// s1 ADD s2 -> s
  2570. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2571. {
  2572. int a = 0;
  2573. switch(op){
  2574. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2575. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2576. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2577. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2578. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2579. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2580. case C_ADD:
  2581. {
  2582. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2583. strcpy(buf, s1);
  2584. strcat(buf, s2);
  2585. script_pushstr(st, buf);
  2586. return;
  2587. }
  2588. default:
  2589. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2590. script_reportsrc(st);
  2591. script_pushnil(st);
  2592. st->state = END;
  2593. return;
  2594. }
  2595. script_pushint(st,a);
  2596. }
  2597. /// Binary number operators
  2598. /// i OP i -> i
  2599. void op_2num(struct script_state* st, int op, int i1, int i2)
  2600. {
  2601. int ret;
  2602. double ret_double;
  2603. switch( op )
  2604. {
  2605. case C_AND: ret = i1 & i2; break;
  2606. case C_OR: ret = i1 | i2; break;
  2607. case C_XOR: ret = i1 ^ i2; break;
  2608. case C_LAND: ret = (i1 && i2); break;
  2609. case C_LOR: ret = (i1 || i2); break;
  2610. case C_EQ: ret = (i1 == i2); break;
  2611. case C_NE: ret = (i1 != i2); break;
  2612. case C_GT: ret = (i1 > i2); break;
  2613. case C_GE: ret = (i1 >= i2); break;
  2614. case C_LT: ret = (i1 < i2); break;
  2615. case C_LE: ret = (i1 <= i2); break;
  2616. case C_R_SHIFT: ret = i1>>i2; break;
  2617. case C_L_SHIFT: ret = i1<<i2; break;
  2618. case C_DIV:
  2619. case C_MOD:
  2620. if( i2 == 0 )
  2621. {
  2622. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2623. script_reportsrc(st);
  2624. script_pushnil(st);
  2625. st->state = END;
  2626. return;
  2627. }
  2628. else if( op == C_DIV )
  2629. ret = i1 / i2;
  2630. else//if( op == C_MOD )
  2631. ret = i1 % i2;
  2632. break;
  2633. default:
  2634. switch( op )
  2635. {// operators that can overflow/underflow
  2636. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2637. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2638. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2639. default:
  2640. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2641. script_reportsrc(st);
  2642. script_pushnil(st);
  2643. return;
  2644. }
  2645. if( ret_double < (double)INT_MIN )
  2646. {
  2647. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2648. script_reportsrc(st);
  2649. ret = INT_MIN;
  2650. }
  2651. else if( ret_double > (double)INT_MAX )
  2652. {
  2653. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2654. script_reportsrc(st);
  2655. ret = INT_MAX;
  2656. }
  2657. }
  2658. script_pushint(st, ret);
  2659. }
  2660. /// Binary operators
  2661. void op_2(struct script_state *st, int op)
  2662. {
  2663. struct script_data* left;
  2664. struct script_data* right;
  2665. left = script_getdatatop(st, -2);
  2666. right = script_getdatatop(st, -1);
  2667. get_val(st, left);
  2668. get_val(st, right);
  2669. // automatic conversions
  2670. switch( op )
  2671. {
  2672. case C_ADD:
  2673. if( data_isint(left) && data_isstring(right) )
  2674. {// convert int-string to string-string
  2675. conv_str(st, left);
  2676. }
  2677. else if( data_isstring(left) && data_isint(right) )
  2678. {// convert string-int to string-string
  2679. conv_str(st, right);
  2680. }
  2681. break;
  2682. }
  2683. if( data_isstring(left) && data_isstring(right) )
  2684. {// ss => op_2str
  2685. op_2str(st, op, left->u.str, right->u.str);
  2686. script_removetop(st, -3, -1);// pop the two values before the top one
  2687. }
  2688. else if( data_isint(left) && data_isint(right) )
  2689. {// ii => op_2num
  2690. int i1 = left->u.num;
  2691. int i2 = right->u.num;
  2692. script_removetop(st, -2, 0);
  2693. op_2num(st, op, i1, i2);
  2694. }
  2695. else
  2696. {// invalid argument
  2697. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2698. script_reportdata(left);
  2699. script_reportdata(right);
  2700. script_reportsrc(st);
  2701. script_removetop(st, -2, 0);
  2702. script_pushnil(st);
  2703. st->state = END;
  2704. }
  2705. }
  2706. /// Unary operators
  2707. /// NEG i -> i
  2708. /// NOT i -> i
  2709. /// LNOT i -> i
  2710. void op_1(struct script_state* st, int op)
  2711. {
  2712. struct script_data* data;
  2713. int i1;
  2714. data = script_getdatatop(st, -1);
  2715. get_val(st, data);
  2716. if( !data_isint(data) )
  2717. {// not a number
  2718. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2719. script_reportdata(data);
  2720. script_reportsrc(st);
  2721. script_pushnil(st);
  2722. st->state = END;
  2723. return;
  2724. }
  2725. i1 = data->u.num;
  2726. script_removetop(st, -1, 0);
  2727. switch( op )
  2728. {
  2729. case C_NEG: i1 = -i1; break;
  2730. case C_NOT: i1 = ~i1; break;
  2731. case C_LNOT: i1 = !i1; break;
  2732. default:
  2733. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2734. script_reportsrc(st);
  2735. script_pushnil(st);
  2736. st->state = END;
  2737. return;
  2738. }
  2739. script_pushint(st, i1);
  2740. }
  2741. /// Checks the type of all arguments passed to a built-in function.
  2742. ///
  2743. /// @param st Script state whose stack arguments should be inspected.
  2744. /// @param func Built-in function for which the arguments are intended.
  2745. static void script_check_buildin_argtype(struct script_state* st, int func)
  2746. {
  2747. char type;
  2748. int idx, invalid = 0;
  2749. script_function* sf = &buildin_func[str_data[func].val];
  2750. for( idx = 2; script_hasdata(st, idx); idx++ )
  2751. {
  2752. struct script_data* data = script_getdata(st, idx);
  2753. type = sf->arg[idx-2];
  2754. if( type == '?' || type == '*' )
  2755. {// optional argument or unknown number of optional parameters ( no types are after this )
  2756. break;
  2757. }
  2758. else if( type == 0 )
  2759. {// more arguments than necessary ( should not happen, as it is checked before )
  2760. ShowWarning("Found more arguments than necessary.\n");
  2761. invalid++;
  2762. break;
  2763. }
  2764. else
  2765. {
  2766. const char* name = NULL;
  2767. if( data_isreference(data) )
  2768. {// get name for variables to determine the type they refer to
  2769. name = reference_getname(data);
  2770. }
  2771. switch( type )
  2772. {
  2773. case 'v':
  2774. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  2775. {// variant
  2776. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  2777. script_reportdata(data);
  2778. invalid++;
  2779. }
  2780. break;
  2781. case 's':
  2782. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  2783. {// string
  2784. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  2785. script_reportdata(data);
  2786. invalid++;
  2787. }
  2788. break;
  2789. case 'i':
  2790. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  2791. {// int ( params and constants are always int )
  2792. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  2793. script_reportdata(data);
  2794. invalid++;
  2795. }
  2796. break;
  2797. case 'r':
  2798. if( !data_isreference(data) )
  2799. {// variables
  2800. ShowWarning("Unexpected type for argument %d. Expected variable.\n", idx-1);
  2801. script_reportdata(data);
  2802. invalid++;
  2803. }
  2804. break;
  2805. case 'l':
  2806. if( !data_islabel(data) && !data_isfunclabel(data) )
  2807. {// label
  2808. ShowWarning("Unexpected type for argument %d. Expected label.\n", idx-1);
  2809. script_reportdata(data);
  2810. invalid++;
  2811. }
  2812. break;
  2813. }
  2814. }
  2815. }
  2816. if(invalid)
  2817. {
  2818. ShowDebug("Function: %s\n", get_str(func));
  2819. script_reportsrc(st);
  2820. }
  2821. }
  2822. /// Executes a buildin command.
  2823. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2824. int run_func(struct script_state *st)
  2825. {
  2826. struct script_data* data;
  2827. int i,start_sp,end_sp,func;
  2828. end_sp = st->stack->sp;// position after the last argument
  2829. for( i = end_sp-1; i > 0 ; --i )
  2830. if( st->stack->stack_data[i].type == C_ARG )
  2831. break;
  2832. if( i == 0 )
  2833. {
  2834. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2835. st->state = END;
  2836. script_reportsrc(st);
  2837. return 1;
  2838. }
  2839. start_sp = i-1;// C_NAME of the command
  2840. st->start = start_sp;
  2841. st->end = end_sp;
  2842. data = &st->stack->stack_data[st->start];
  2843. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2844. func = data->u.num;
  2845. else
  2846. {
  2847. ShowError("script:run_func: not a buildin command.\n");
  2848. script_reportdata(data);
  2849. script_reportsrc(st);
  2850. st->state = END;
  2851. return 1;
  2852. }
  2853. if( script_config.warn_func_mismatch_argtypes )
  2854. {
  2855. script_check_buildin_argtype(st, func);
  2856. }
  2857. if(str_data[func].func){
  2858. if (str_data[func].func(st)) //Report error
  2859. script_reportsrc(st);
  2860. } else {
  2861. 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));
  2862. script_reportsrc(st);
  2863. st->state = END;
  2864. }
  2865. // Stack's datum are used when re-running functions [Eoe]
  2866. if( st->state == RERUNLINE )
  2867. return 0;
  2868. pop_stack(st, st->start, st->end);
  2869. if( st->state == RETFUNC )
  2870. {// return from a user-defined function
  2871. struct script_retinfo* ri;
  2872. int olddefsp = st->stack->defsp;
  2873. int nargs;
  2874. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2875. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2876. {
  2877. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  2878. script_reportsrc(st);
  2879. st->state = END;
  2880. return 1;
  2881. }
  2882. script_free_vars( st->stack->var_function );
  2883. aFree(st->stack->var_function);
  2884. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  2885. nargs = ri->nargs;
  2886. st->pos = ri->pos;
  2887. st->script = ri->script;
  2888. st->stack->var_function = ri->var_function;
  2889. st->stack->defsp = ri->defsp;
  2890. memset(ri, 0, sizeof(struct script_retinfo));
  2891. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  2892. st->state = GOTO;
  2893. }
  2894. return 0;
  2895. }
  2896. /*==========================================
  2897. * script execution
  2898. *------------------------------------------*/
  2899. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2900. {
  2901. struct script_state *st;
  2902. if( rootscript == NULL || pos < 0 )
  2903. return;
  2904. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  2905. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  2906. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  2907. st = script_alloc_state(rootscript, pos, rid, oid);
  2908. run_script_main(st);
  2909. }
  2910. void script_stop_sleeptimers(int id)
  2911. {
  2912. struct script_state* st;
  2913. for(;;)
  2914. {
  2915. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2916. if( st == NULL )
  2917. break; // no more sleep timers
  2918. script_free_state(st);
  2919. }
  2920. }
  2921. /*==========================================
  2922. * 指定ノードをsleep_dbから削除
  2923. *------------------------------------------*/
  2924. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2925. {
  2926. struct linkdb_node *retnode;
  2927. if( n == NULL)
  2928. return NULL;
  2929. if( n->prev == NULL )
  2930. sleep_db = n->next;
  2931. else
  2932. n->prev->next = n->next;
  2933. if( n->next )
  2934. n->next->prev = n->prev;
  2935. retnode = n->next;
  2936. aFree( n );
  2937. return retnode; // 次のノードを返す
  2938. }
  2939. /*==========================================
  2940. * sleep用タイマー関数
  2941. *------------------------------------------*/
  2942. int run_script_timer(int tid, unsigned int tick, int id, intptr data)
  2943. {
  2944. struct script_state *st = (struct script_state *)data;
  2945. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2946. TBL_PC *sd = map_id2sd(st->rid);
  2947. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2948. { //Character mismatch. Cancel execution.
  2949. st->rid = 0;
  2950. st->state = END;
  2951. }
  2952. while( node && st->sleep.timer != -1 ) {
  2953. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2954. script_erase_sleepdb(node);
  2955. st->sleep.timer = INVALID_TIMER;
  2956. break;
  2957. }
  2958. node = node->next;
  2959. }
  2960. if(st->state != RERUNLINE)
  2961. st->sleep.tick = 0;
  2962. run_script_main(st);
  2963. return 0;
  2964. }
  2965. /// Detaches script state from possibly attached character and restores it's previous script if any.
  2966. ///
  2967. /// @param st Script state to detach.
  2968. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  2969. static void script_detach_state(struct script_state* st, bool dequeue_event)
  2970. {
  2971. struct map_session_data* sd;
  2972. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  2973. {
  2974. sd->st = st->bk_st;
  2975. sd->npc_id = st->bk_npcid;
  2976. if(st->bk_st)
  2977. {
  2978. //Remove tag for removal.
  2979. st->bk_st = NULL;
  2980. st->bk_npcid = 0;
  2981. }
  2982. else if(dequeue_event)
  2983. {
  2984. npc_event_dequeue(sd);
  2985. }
  2986. }
  2987. else if(st->bk_st)
  2988. {// rid was set to 0, before detaching the script state
  2989. 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);
  2990. script_reportsrc(st->bk_st);
  2991. script_free_state(st->bk_st);
  2992. st->bk_st = NULL;
  2993. }
  2994. }
  2995. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  2996. ///
  2997. /// @param st Script state to attach.
  2998. static void script_attach_state(struct script_state* st)
  2999. {
  3000. struct map_session_data* sd;
  3001. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3002. {
  3003. if(st!=sd->st)
  3004. {
  3005. if(st->bk_st)
  3006. {// there is already a backup
  3007. 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);
  3008. }
  3009. st->bk_st = sd->st;
  3010. st->bk_npcid = sd->npc_id;
  3011. }
  3012. sd->st = st;
  3013. sd->npc_id = st->oid;
  3014. }
  3015. }
  3016. /*==========================================
  3017. * スクリプトの実行メイン部分
  3018. *------------------------------------------*/
  3019. void run_script_main(struct script_state *st)
  3020. {
  3021. int cmdcount=script_config.check_cmdcount;
  3022. int gotocount=script_config.check_gotocount;
  3023. TBL_PC *sd;
  3024. struct script_stack *stack=st->stack;
  3025. struct npc_data *nd;
  3026. script_attach_state(st);
  3027. nd = map_id2nd(st->oid);
  3028. if( nd && map[nd->bl.m].instance_id > 0 )
  3029. st->instance_id = map[nd->bl.m].instance_id;
  3030. if(st->state == RERUNLINE) {
  3031. run_func(st);
  3032. if(st->state == GOTO)
  3033. st->state = RUN;
  3034. } else if(st->state != END)
  3035. st->state = RUN;
  3036. while(st->state == RUN)
  3037. {
  3038. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3039. switch(c){
  3040. case C_EOL:
  3041. if( stack->defsp > stack->sp )
  3042. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3043. else
  3044. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3045. break;
  3046. case C_INT:
  3047. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3048. break;
  3049. case C_POS:
  3050. case C_NAME:
  3051. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3052. st->pos+=3;
  3053. break;
  3054. case C_ARG:
  3055. push_val(stack,c,0);
  3056. break;
  3057. case C_STR:
  3058. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3059. while(st->script->script_buf[st->pos++]);
  3060. break;
  3061. case C_FUNC:
  3062. run_func(st);
  3063. if(st->state==GOTO){
  3064. st->state = RUN;
  3065. if( gotocount>0 && (--gotocount)<=0 ){
  3066. ShowError("run_script: infinity loop !\n");
  3067. script_reportsrc(st);
  3068. st->state=END;
  3069. }
  3070. }
  3071. break;
  3072. case C_NEG:
  3073. case C_NOT:
  3074. case C_LNOT:
  3075. op_1(st ,c);
  3076. break;
  3077. case C_ADD:
  3078. case C_SUB:
  3079. case C_MUL:
  3080. case C_DIV:
  3081. case C_MOD:
  3082. case C_EQ:
  3083. case C_NE:
  3084. case C_GT:
  3085. case C_GE:
  3086. case C_LT:
  3087. case C_LE:
  3088. case C_AND:
  3089. case C_OR:
  3090. case C_XOR:
  3091. case C_LAND:
  3092. case C_LOR:
  3093. case C_R_SHIFT:
  3094. case C_L_SHIFT:
  3095. op_2(st, c);
  3096. break;
  3097. case C_OP3:
  3098. op_3(st, c);
  3099. break;
  3100. case C_NOP:
  3101. st->state=END;
  3102. break;
  3103. default:
  3104. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3105. st->state=END;
  3106. break;
  3107. }
  3108. if( cmdcount>0 && (--cmdcount)<=0 ){
  3109. ShowError("run_script: infinity loop !\n");
  3110. script_reportsrc(st);
  3111. st->state=END;
  3112. }
  3113. }
  3114. if(st->sleep.tick > 0) {
  3115. //Restore previous script
  3116. script_detach_state(st, false);
  3117. //Delay execution
  3118. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3119. st->sleep.charid = sd?sd->status.char_id:0;
  3120. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3121. run_script_timer, st->sleep.charid, (intptr)st);
  3122. linkdb_insert(&sleep_db, (void*)st->oid, st);
  3123. }
  3124. else if(st->state != END && st->rid){
  3125. //Resume later (st is already attached to player).
  3126. if(st->bk_st) {
  3127. ShowWarning("Unable to restore stack! Double continuation!\n");
  3128. //Report BOTH scripts to see if that can help somehow.
  3129. ShowDebug("Previous script (lost):\n");
  3130. script_reportsrc(st->bk_st);
  3131. ShowDebug("Current script:\n");
  3132. script_reportsrc(st);
  3133. script_free_state(st->bk_st);
  3134. st->bk_st = NULL;
  3135. }
  3136. } else {
  3137. //Dispose of script.
  3138. if ((sd = map_id2sd(st->rid))!=NULL)
  3139. { //Restore previous stack and save char.
  3140. if(sd->state.using_fake_npc){
  3141. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3142. sd->state.using_fake_npc = 0;
  3143. }
  3144. //Restore previous script if any.
  3145. script_detach_state(st, true);
  3146. if (sd->state.reg_dirty&2)
  3147. intif_saveregistry(sd,2);
  3148. if (sd->state.reg_dirty&1)
  3149. intif_saveregistry(sd,1);
  3150. }
  3151. script_free_state(st);
  3152. st = NULL;
  3153. }
  3154. }
  3155. int script_config_read(char *cfgName)
  3156. {
  3157. int i;
  3158. char line[1024],w1[1024],w2[1024];
  3159. FILE *fp;
  3160. fp=fopen(cfgName,"r");
  3161. if(fp==NULL){
  3162. ShowError("file not found: [%s]\n", cfgName);
  3163. return 1;
  3164. }
  3165. while(fgets(line, sizeof(line), fp))
  3166. {
  3167. if(line[0] == '/' && line[1] == '/')
  3168. continue;
  3169. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3170. if(i!=2)
  3171. continue;
  3172. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3173. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3174. }
  3175. else if(strcmpi(w1,"check_cmdcount")==0) {
  3176. script_config.check_cmdcount = config_switch(w2);
  3177. }
  3178. else if(strcmpi(w1,"check_gotocount")==0) {
  3179. script_config.check_gotocount = config_switch(w2);
  3180. }
  3181. else if(strcmpi(w1,"input_min_value")==0) {
  3182. script_config.input_min_value = config_switch(w2);
  3183. }
  3184. else if(strcmpi(w1,"input_max_value")==0) {
  3185. script_config.input_max_value = config_switch(w2);
  3186. }
  3187. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3188. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3189. }
  3190. else if(strcmpi(w1,"import")==0){
  3191. script_config_read(w2);
  3192. }
  3193. }
  3194. fclose(fp);
  3195. return 0;
  3196. }
  3197. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3198. {
  3199. struct script_code *code = (struct script_code *)data;
  3200. if(code){
  3201. script_free_vars( &code->script_vars );
  3202. aFree( code->script_buf );
  3203. aFree( code );
  3204. }
  3205. return 0;
  3206. }
  3207. static int do_final_autobonus_sub (DBKey key,void *data,va_list ap)
  3208. {
  3209. struct script_code *script = (struct script_code *)data;
  3210. if( script )
  3211. script_free_code(script);
  3212. return 0;
  3213. }
  3214. void script_run_autobonus(const char *autobonus, int id, int pos)
  3215. {
  3216. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3217. if( script )
  3218. {
  3219. current_equip_item_index = pos;
  3220. run_script(script,0,id,0);
  3221. }
  3222. }
  3223. void script_add_autobonus(const char *autobonus)
  3224. {
  3225. if( strdb_get(autobonus_db, autobonus) == NULL )
  3226. {
  3227. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3228. if( script )
  3229. strdb_put(autobonus_db, autobonus, script);
  3230. }
  3231. }
  3232. /*==========================================
  3233. * 終了
  3234. *------------------------------------------*/
  3235. int do_final_script()
  3236. {
  3237. #ifdef DEBUG_HASH
  3238. if (battle_config.etc_log)
  3239. {
  3240. FILE *fp = fopen("hash_dump.txt","wt");
  3241. if(fp) {
  3242. int i,count[SCRIPT_HASH_SIZE];
  3243. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3244. int n=0;
  3245. int min=INT_MAX,max=0,zero=0;
  3246. double mean=0.0f;
  3247. double median=0.0f;
  3248. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3249. memset(count, 0, sizeof(count));
  3250. fprintf(fp,"num : hash : data_name\n");
  3251. fprintf(fp,"---------------------------------------------------------------\n");
  3252. for(i=LABEL_START; i<str_num; i++) {
  3253. unsigned int h = calc_hash(get_str(i));
  3254. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3255. ++count[h];
  3256. }
  3257. fprintf(fp,"--------------------\n\n");
  3258. memset(count2, 0, sizeof(count2));
  3259. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3260. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3261. if(min > count[i])
  3262. min = count[i]; // minimun count of collision
  3263. if(max < count[i])
  3264. max = count[i]; // maximun count of collision
  3265. if(count[i] == 0)
  3266. zero++;
  3267. ++count2[count[i]];
  3268. }
  3269. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3270. for( i=min; i <= max; ++i ){
  3271. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3272. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3273. }
  3274. for( i=min; i <= max; ++i ){
  3275. n += count2[i];
  3276. if( n*2 >= SCRIPT_HASH_SIZE )
  3277. {
  3278. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3279. median = (i+i+1)/2.0f;
  3280. else
  3281. median = i;
  3282. break;
  3283. }
  3284. }
  3285. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3286. fclose(fp);
  3287. }
  3288. }
  3289. #endif
  3290. mapreg_final();
  3291. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3292. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3293. autobonus_db->destroy(autobonus_db, do_final_autobonus_sub);
  3294. if(sleep_db) {
  3295. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3296. while(n) {
  3297. struct script_state *st = (struct script_state *)n->data;
  3298. script_free_state(st);
  3299. n = n->next;
  3300. }
  3301. linkdb_final(&sleep_db);
  3302. }
  3303. if (str_data)
  3304. aFree(str_data);
  3305. if (str_buf)
  3306. aFree(str_buf);
  3307. return 0;
  3308. }
  3309. /*==========================================
  3310. * 初期化
  3311. *------------------------------------------*/
  3312. int do_init_script()
  3313. {
  3314. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3315. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3316. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3317. mapreg_init();
  3318. return 0;
  3319. }
  3320. int script_reload()
  3321. {
  3322. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3323. scriptlabel_db->clear(scriptlabel_db, NULL);
  3324. if(sleep_db) {
  3325. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3326. while(n) {
  3327. struct script_state *st = (struct script_state *)n->data;
  3328. script_free_state(st);
  3329. n = n->next;
  3330. }
  3331. linkdb_final(&sleep_db);
  3332. }
  3333. mapreg_reload();
  3334. return 0;
  3335. }
  3336. //-----------------------------------------------------------------------------
  3337. // buildin functions
  3338. //
  3339. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3340. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3341. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3342. /////////////////////////////////////////////////////////////////////
  3343. // NPC interaction
  3344. //
  3345. /// Appends a message to the npc dialog.
  3346. /// If a dialog doesn't exist yet, one is created.
  3347. ///
  3348. /// mes "<message>";
  3349. BUILDIN_FUNC(mes)
  3350. {
  3351. TBL_PC* sd = script_rid2sd(st);
  3352. if( sd == NULL )
  3353. return 0;
  3354. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3355. return 0;
  3356. }
  3357. /// Displays the button 'next' in the npc dialog.
  3358. /// The dialog text is cleared and the script continues when the button is pressed.
  3359. ///
  3360. /// next;
  3361. BUILDIN_FUNC(next)
  3362. {
  3363. TBL_PC* sd;
  3364. sd = script_rid2sd(st);
  3365. if( sd == NULL )
  3366. return 0;
  3367. st->state = STOP;
  3368. clif_scriptnext(sd, st->oid);
  3369. return 0;
  3370. }
  3371. /// Ends the script and displays the button 'close' on the npc dialog.
  3372. /// The dialog is closed when the button is pressed.
  3373. ///
  3374. /// close;
  3375. BUILDIN_FUNC(close)
  3376. {
  3377. TBL_PC* sd;
  3378. sd = script_rid2sd(st);
  3379. if( sd == NULL )
  3380. return 0;
  3381. st->state = END;
  3382. clif_scriptclose(sd, st->oid);
  3383. return 0;
  3384. }
  3385. /// Displays the button 'close' on the npc dialog.
  3386. /// The dialog is closed and the script continues when the button is pressed.
  3387. ///
  3388. /// close2;
  3389. BUILDIN_FUNC(close2)
  3390. {
  3391. TBL_PC* sd;
  3392. sd = script_rid2sd(st);
  3393. if( sd == NULL )
  3394. return 0;
  3395. st->state = STOP;
  3396. clif_scriptclose(sd, st->oid);
  3397. return 0;
  3398. }
  3399. /// Counts the number of valid and total number of options in 'str'
  3400. /// If max_count > 0 the counting stops when that valid option is reached
  3401. /// total is incremented for each option (NULL is supported)
  3402. static int menu_countoptions(const char* str, int max_count, int* total)
  3403. {
  3404. int count = 0;
  3405. int bogus_total;
  3406. if( total == NULL )
  3407. total = &bogus_total;
  3408. ++(*total);
  3409. // initial empty options
  3410. while( *str == ':' )
  3411. {
  3412. ++str;
  3413. ++(*total);
  3414. }
  3415. // count menu options
  3416. while( *str != '\0' )
  3417. {
  3418. ++count;
  3419. --max_count;
  3420. if( max_count == 0 )
  3421. break;
  3422. while( *str != ':' && *str != '\0' )
  3423. ++str;
  3424. while( *str == ':' )
  3425. {
  3426. ++str;
  3427. ++(*total);
  3428. }
  3429. }
  3430. return count;
  3431. }
  3432. /// Displays a menu with options and goes to the target label.
  3433. /// The script is stopped if cancel is pressed.
  3434. /// Options with no text are not displayed in the client.
  3435. ///
  3436. /// Options can be grouped together, separated by the character ':' in the text:
  3437. /// ex: menu "A:B:C",L_target;
  3438. /// All these options go to the specified target label.
  3439. ///
  3440. /// The index of the selected option is put in the variable @menu.
  3441. /// Indexes start with 1 and are consistent with grouped and empty options.
  3442. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3443. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3444. ///
  3445. /// NOTE: the client closes the npc dialog when cancel is pressed
  3446. ///
  3447. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3448. BUILDIN_FUNC(menu)
  3449. {
  3450. int i;
  3451. const char* text;
  3452. TBL_PC* sd;
  3453. sd = script_rid2sd(st);
  3454. if( sd == NULL )
  3455. return 0;
  3456. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3457. if( sd->state.menu_or_input == 0 )
  3458. {
  3459. struct StringBuf buf;
  3460. struct script_data* data;
  3461. if( script_lastdata(st) % 2 == 0 )
  3462. {// argument count is not even (1st argument is at index 2)
  3463. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3464. st->state = END;
  3465. return 1;
  3466. }
  3467. StringBuf_Init(&buf);
  3468. sd->npc_menu = 0;
  3469. for( i = 2; i < script_lastdata(st); i += 2 )
  3470. {
  3471. // menu options
  3472. text = script_getstr(st, i);
  3473. // target label
  3474. data = script_getdata(st, i+1);
  3475. if( !data_islabel(data) )
  3476. {// not a label
  3477. StringBuf_Destroy(&buf);
  3478. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3479. script_reportdata(data);
  3480. st->state = END;
  3481. return 1;
  3482. }
  3483. // append option(s)
  3484. if( text[0] == '\0' )
  3485. continue;// empty string, ignore
  3486. if( sd->npc_menu > 0 )
  3487. StringBuf_AppendStr(&buf, ":");
  3488. StringBuf_AppendStr(&buf, text);
  3489. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3490. }
  3491. st->state = RERUNLINE;
  3492. sd->state.menu_or_input = 1;
  3493. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3494. StringBuf_Destroy(&buf);
  3495. if( sd->npc_menu >= 0xff )
  3496. {// 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
  3497. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3498. script_reportsrc(st);
  3499. }
  3500. }
  3501. else if( sd->npc_menu == 0xff )
  3502. {// Cancel was pressed
  3503. sd->state.menu_or_input = 0;
  3504. st->state = END;
  3505. }
  3506. else
  3507. {// goto target label
  3508. int menu = 0;
  3509. sd->state.menu_or_input = 0;
  3510. if( sd->npc_menu <= 0 )
  3511. {
  3512. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3513. st->state = END;
  3514. return 1;
  3515. }
  3516. // get target label
  3517. for( i = 2; i < script_lastdata(st); i += 2 )
  3518. {
  3519. text = script_getstr(st, i);
  3520. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3521. if( sd->npc_menu <= 0 )
  3522. break;// entry found
  3523. }
  3524. if( sd->npc_menu > 0 )
  3525. {// Invalid selection
  3526. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3527. st->state = END;
  3528. return 1;
  3529. }
  3530. if( !data_islabel(script_getdata(st, i + 1)) )
  3531. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3532. ShowError("script:menu: unexpected data in label argument\n");
  3533. script_reportdata(script_getdata(st, i + 1));
  3534. st->state = END;
  3535. return 1;
  3536. }
  3537. pc_setreg(sd, add_str("@menu"), menu);
  3538. st->pos = script_getnum(st, i + 1);
  3539. st->state = GOTO;
  3540. }
  3541. return 0;
  3542. }
  3543. /// Displays a menu with options and returns the selected option.
  3544. /// Behaves like 'menu' without the target labels.
  3545. ///
  3546. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3547. ///
  3548. /// @see menu
  3549. BUILDIN_FUNC(select)
  3550. {
  3551. int i;
  3552. const char* text;
  3553. TBL_PC* sd;
  3554. sd = script_rid2sd(st);
  3555. if( sd == NULL )
  3556. return 0;
  3557. if( sd->state.menu_or_input == 0 )
  3558. {
  3559. struct StringBuf buf;
  3560. StringBuf_Init(&buf);
  3561. sd->npc_menu = 0;
  3562. for( i = 2; i <= script_lastdata(st); ++i )
  3563. {
  3564. text = script_getstr(st, i);
  3565. if( sd->npc_menu > 0 )
  3566. StringBuf_AppendStr(&buf, ":");
  3567. StringBuf_AppendStr(&buf, text);
  3568. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3569. }
  3570. st->state = RERUNLINE;
  3571. sd->state.menu_or_input = 1;
  3572. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3573. StringBuf_Destroy(&buf);
  3574. if( sd->npc_menu >= 0xff )
  3575. {
  3576. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3577. script_reportsrc(st);
  3578. }
  3579. }
  3580. else if( sd->npc_menu == 0xff )
  3581. {// Cancel was pressed
  3582. sd->state.menu_or_input = 0;
  3583. st->state = END;
  3584. }
  3585. else
  3586. {// return selected option
  3587. int menu = 0;
  3588. sd->state.menu_or_input = 0;
  3589. for( i = 2; i <= script_lastdata(st); ++i )
  3590. {
  3591. text = script_getstr(st, i);
  3592. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3593. if( sd->npc_menu <= 0 )
  3594. break;// entry found
  3595. }
  3596. pc_setreg(sd, add_str("@menu"), menu);
  3597. script_pushint(st, menu);
  3598. st->state = RUN;
  3599. }
  3600. return 0;
  3601. }
  3602. /// Displays a menu with options and returns the selected option.
  3603. /// Behaves like 'menu' without the target labels, except when cancel is
  3604. /// pressed.
  3605. /// When cancel is pressed, the script continues and 255 is returned.
  3606. ///
  3607. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3608. ///
  3609. /// @see menu
  3610. BUILDIN_FUNC(prompt)
  3611. {
  3612. int i;
  3613. const char *text;
  3614. TBL_PC* sd;
  3615. sd = script_rid2sd(st);
  3616. if( sd == NULL )
  3617. return 0;
  3618. if( sd->state.menu_or_input == 0 )
  3619. {
  3620. struct StringBuf buf;
  3621. StringBuf_Init(&buf);
  3622. sd->npc_menu = 0;
  3623. for( i = 2; i <= script_lastdata(st); ++i )
  3624. {
  3625. text = script_getstr(st, i);
  3626. if( sd->npc_menu > 0 )
  3627. StringBuf_AppendStr(&buf, ":");
  3628. StringBuf_AppendStr(&buf, text);
  3629. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3630. }
  3631. st->state = RERUNLINE;
  3632. sd->state.menu_or_input = 1;
  3633. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3634. StringBuf_Destroy(&buf);
  3635. if( sd->npc_menu >= 0xff )
  3636. {
  3637. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3638. script_reportsrc(st);
  3639. }
  3640. }
  3641. else if( sd->npc_menu == 0xff )
  3642. {// Cancel was pressed
  3643. sd->state.menu_or_input = 0;
  3644. pc_setreg(sd, add_str("@menu"), 0xff);
  3645. script_pushint(st, 0xff);
  3646. st->state = RUN;
  3647. }
  3648. else
  3649. {// return selected option
  3650. int menu = 0;
  3651. sd->state.menu_or_input = 0;
  3652. for( i = 2; i <= script_lastdata(st); ++i )
  3653. {
  3654. text = script_getstr(st, i);
  3655. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3656. if( sd->npc_menu <= 0 )
  3657. break;// entry found
  3658. }
  3659. pc_setreg(sd, add_str("@menu"), menu);
  3660. script_pushint(st, menu);
  3661. st->state = RUN;
  3662. }
  3663. return 0;
  3664. }
  3665. /////////////////////////////////////////////////////////////////////
  3666. // ...
  3667. //
  3668. /// Jumps to the target script label.
  3669. ///
  3670. /// goto <label>;
  3671. BUILDIN_FUNC(goto)
  3672. {
  3673. if( !data_islabel(script_getdata(st,2)) )
  3674. {
  3675. ShowError("script:goto: not a label\n");
  3676. script_reportdata(script_getdata(st,2));
  3677. st->state = END;
  3678. return 1;
  3679. }
  3680. st->pos = script_getnum(st,2);
  3681. st->state = GOTO;
  3682. return 0;
  3683. }
  3684. /*==========================================
  3685. * user-defined function call
  3686. *------------------------------------------*/
  3687. BUILDIN_FUNC(callfunc)
  3688. {
  3689. int i, j;
  3690. struct script_retinfo* ri;
  3691. struct script_code* scr;
  3692. const char* str = script_getstr(st,2);
  3693. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3694. if( !scr )
  3695. {
  3696. ShowError("script:callfunc: function not found! [%s]\n", str);
  3697. st->state = END;
  3698. return 1;
  3699. }
  3700. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3701. {
  3702. struct script_data* data = push_copy(st->stack,i);
  3703. if( data_isreference(data) && !data->ref )
  3704. {
  3705. const char* name = reference_getname(data);
  3706. if( name[0] == '.' && name[1] == '@' )
  3707. data->ref = st->stack->var_function;
  3708. else if( name[0] == '.' )
  3709. data->ref = &st->script->script_vars;
  3710. }
  3711. }
  3712. CREATE(ri, struct script_retinfo, 1);
  3713. ri->script = st->script;// script code
  3714. ri->var_function = st->stack->var_function;// scope variables
  3715. ri->pos = st->pos;// script location
  3716. ri->nargs = j;// argument count
  3717. ri->defsp = st->stack->defsp;// default stack pointer
  3718. push_retinfo(st->stack, ri);
  3719. st->pos = 0;
  3720. st->script = scr;
  3721. st->stack->defsp = st->stack->sp;
  3722. st->state = GOTO;
  3723. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3724. return 0;
  3725. }
  3726. /*==========================================
  3727. * subroutine call
  3728. *------------------------------------------*/
  3729. BUILDIN_FUNC(callsub)
  3730. {
  3731. int i,j;
  3732. struct script_retinfo* ri;
  3733. int pos = script_getnum(st,2);
  3734. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3735. {
  3736. ShowError("script:callsub: argument is not a label\n");
  3737. script_reportdata(script_getdata(st,2));
  3738. st->state = END;
  3739. return 1;
  3740. }
  3741. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3742. {
  3743. struct script_data* data = push_copy(st->stack,i);
  3744. if( data_isreference(data) && !data->ref )
  3745. {
  3746. const char* name = reference_getname(data);
  3747. if( name[0] == '.' && name[1] == '@' )
  3748. data->ref = st->stack->var_function;
  3749. }
  3750. }
  3751. CREATE(ri, struct script_retinfo, 1);
  3752. ri->script = st->script;// script code
  3753. ri->var_function = st->stack->var_function;// scope variables
  3754. ri->pos = st->pos;// script location
  3755. ri->nargs = j;// argument count
  3756. ri->defsp = st->stack->defsp;// default stack pointer
  3757. push_retinfo(st->stack, ri);
  3758. st->pos = pos;
  3759. st->stack->defsp = st->stack->sp;
  3760. st->state = GOTO;
  3761. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3762. return 0;
  3763. }
  3764. /// Retrieves an argument provided to callfunc/callsub.
  3765. /// If the argument doesn't exist
  3766. ///
  3767. /// getarg(<index>{,<default_value>}) -> <value>
  3768. BUILDIN_FUNC(getarg)
  3769. {
  3770. struct script_retinfo* ri;
  3771. int idx;
  3772. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3773. {
  3774. ShowError("script:getarg: no callfunc or callsub!\n");
  3775. st->state = END;
  3776. return 1;
  3777. }
  3778. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3779. idx = script_getnum(st,2);
  3780. if( idx >= 0 && idx < ri->nargs )
  3781. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3782. else if( script_hasdata(st,3) )
  3783. script_pushcopy(st, 3);
  3784. else
  3785. {
  3786. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3787. st->state = END;
  3788. return 1;
  3789. }
  3790. return 0;
  3791. }
  3792. /// Returns from the current function, optionaly returning a value from the functions.
  3793. /// Don't use outside script functions.
  3794. ///
  3795. /// return;
  3796. /// return <value>;
  3797. BUILDIN_FUNC(return)
  3798. {
  3799. if( script_hasdata(st,2) )
  3800. {// return value
  3801. struct script_data* data;
  3802. script_pushcopy(st, 2);
  3803. data = script_getdatatop(st, -1);
  3804. if( data_isreference(data) )
  3805. {
  3806. const char* name = reference_getname(data);
  3807. if( name[0] == '.' && name[1] == '@' )
  3808. {// scope variable
  3809. if( !data->ref || data->ref == st->stack->var_function )
  3810. get_val(st, data);// current scope, convert to value
  3811. }
  3812. else if( name[0] == '.' && !data->ref )
  3813. {// script variable, link to current script
  3814. data->ref = &st->script->script_vars;
  3815. }
  3816. }
  3817. }
  3818. else
  3819. {// no return value
  3820. script_pushnil(st);
  3821. }
  3822. st->state = RETFUNC;
  3823. return 0;
  3824. }
  3825. /// Returns a random number from 0 to <range>-1.
  3826. /// Or returns a random number from <min> to <max>.
  3827. /// If <min> is greater than <max>, their numbers are switched.
  3828. /// rand(<range>) -> <int>
  3829. /// rand(<min>,<max>) -> <int>
  3830. BUILDIN_FUNC(rand)
  3831. {
  3832. int range;
  3833. int min;
  3834. int max;
  3835. if( script_hasdata(st,3) )
  3836. {// min,max
  3837. min = script_getnum(st,2);
  3838. max = script_getnum(st,3);
  3839. if( max < min )
  3840. swap(min, max);
  3841. range = max - min + 1;
  3842. }
  3843. else
  3844. {// range
  3845. min = 0;
  3846. range = script_getnum(st,2);
  3847. }
  3848. if( range <= 1 )
  3849. script_pushint(st, min);
  3850. else
  3851. script_pushint(st, rand()%range + min);
  3852. return 0;
  3853. }
  3854. /*==========================================
  3855. *
  3856. *------------------------------------------*/
  3857. BUILDIN_FUNC(warp)
  3858. {
  3859. int ret;
  3860. int x,y;
  3861. const char* str;
  3862. TBL_PC* sd;
  3863. sd = script_rid2sd(st);
  3864. if( sd == NULL )
  3865. return 0;
  3866. str = script_getstr(st,2);
  3867. x = script_getnum(st,3);
  3868. y = script_getnum(st,4);
  3869. if(strcmp(str,"Random")==0)
  3870. ret = pc_randomwarp(sd,CLR_TELEPORT);
  3871. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3872. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  3873. else
  3874. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  3875. if( ret ) {
  3876. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3877. script_reportsrc(st);
  3878. }
  3879. return 0;
  3880. }
  3881. /*==========================================
  3882. * エリア指定ワープ
  3883. *------------------------------------------*/
  3884. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3885. {
  3886. int x,y;
  3887. unsigned int map;
  3888. map=va_arg(ap, unsigned int);
  3889. x=va_arg(ap,int);
  3890. y=va_arg(ap,int);
  3891. if(map == 0)
  3892. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  3893. else
  3894. pc_setpos((TBL_PC *)bl,map,x,y,CLR_OUTSIGHT);
  3895. return 0;
  3896. }
  3897. BUILDIN_FUNC(areawarp)
  3898. {
  3899. int x,y,m;
  3900. unsigned int index;
  3901. const char *str;
  3902. const char *mapname;
  3903. int x0,y0,x1,y1;
  3904. mapname=script_getstr(st,2);
  3905. x0=script_getnum(st,3);
  3906. y0=script_getnum(st,4);
  3907. x1=script_getnum(st,5);
  3908. y1=script_getnum(st,6);
  3909. str=script_getstr(st,7);
  3910. x=script_getnum(st,8);
  3911. y=script_getnum(st,9);
  3912. if( (m=map_mapname2mapid(mapname))< 0)
  3913. return 0;
  3914. if(strcmp(str,"Random")==0)
  3915. index = 0;
  3916. else if(!(index=mapindex_name2id(str)))
  3917. return 0;
  3918. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  3919. return 0;
  3920. }
  3921. /*==========================================
  3922. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  3923. *------------------------------------------*/
  3924. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  3925. {
  3926. int hp, sp;
  3927. hp = va_arg(ap, int);
  3928. sp = va_arg(ap, int);
  3929. pc_percentheal((TBL_PC *)bl,hp,sp);
  3930. return 0;
  3931. }
  3932. BUILDIN_FUNC(areapercentheal)
  3933. {
  3934. int hp,sp,m;
  3935. const char *mapname;
  3936. int x0,y0,x1,y1;
  3937. mapname=script_getstr(st,2);
  3938. x0=script_getnum(st,3);
  3939. y0=script_getnum(st,4);
  3940. x1=script_getnum(st,5);
  3941. y1=script_getnum(st,6);
  3942. hp=script_getnum(st,7);
  3943. sp=script_getnum(st,8);
  3944. if( (m=map_mapname2mapid(mapname))< 0)
  3945. return 0;
  3946. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  3947. return 0;
  3948. }
  3949. /*==========================================
  3950. * warpchar [LuzZza]
  3951. * Useful for warp one player from
  3952. * another player npc-session.
  3953. * Using: warpchar "mapname",x,y,Char_ID;
  3954. *------------------------------------------*/
  3955. BUILDIN_FUNC(warpchar)
  3956. {
  3957. int x,y,a;
  3958. const char *str;
  3959. TBL_PC *sd;
  3960. str=script_getstr(st,2);
  3961. x=script_getnum(st,3);
  3962. y=script_getnum(st,4);
  3963. a=script_getnum(st,5);
  3964. sd = map_charid2sd(a);
  3965. if( sd == NULL )
  3966. return 0;
  3967. if(strcmp(str, "Random") == 0)
  3968. pc_randomwarp(sd, CLR_TELEPORT);
  3969. else
  3970. if(strcmp(str, "SavePoint") == 0)
  3971. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  3972. else
  3973. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  3974. return 0;
  3975. }
  3976. /*==========================================
  3977. * Warpparty - [Fredzilla] [Paradox924X]
  3978. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  3979. * If 'from_mapname' is specified, only the party members on that map will be warped
  3980. *------------------------------------------*/
  3981. BUILDIN_FUNC(warpparty)
  3982. {
  3983. TBL_PC *sd;
  3984. TBL_PC *pl_sd;
  3985. struct party_data* p;
  3986. int type;
  3987. int mapindex;
  3988. int i, j;
  3989. const char* str = script_getstr(st,2);
  3990. int x = script_getnum(st,3);
  3991. int y = script_getnum(st,4);
  3992. int p_id = script_getnum(st,5);
  3993. const char* str2 = NULL;
  3994. if ( script_hasdata(st,6) )
  3995. str2 = script_getstr(st,6);
  3996. sd=script_rid2sd(st);
  3997. if( sd == NULL )
  3998. return 0;
  3999. p = party_search(p_id);
  4000. if(!p)
  4001. return 0;
  4002. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4003. return 0;
  4004. type = ( strcmp(str,"Random")==0 ) ? 0
  4005. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4006. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4007. : ( strcmp(str,"Leader")==0 ) ? 3
  4008. : 4;
  4009. for (i = 0; i < MAX_PARTY; i++)
  4010. {
  4011. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4012. continue;
  4013. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4014. continue;
  4015. if( pc_isdead(pl_sd) )
  4016. continue;
  4017. switch( type )
  4018. {
  4019. case 0: // Random
  4020. if(!map[pl_sd->bl.m].flag.nowarp)
  4021. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4022. break;
  4023. case 1: // SavePointAll
  4024. if(!map[pl_sd->bl.m].flag.noreturn)
  4025. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4026. break;
  4027. case 2: // SavePoint
  4028. if(!map[pl_sd->bl.m].flag.noreturn)
  4029. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4030. break;
  4031. case 3: // Leader
  4032. for(j = 0; j < MAX_PARTY && !p->party.member[j].leader; j++);
  4033. if (j == MAX_PARTY || !p->data[j].sd) //Leader not found / not online
  4034. return 0;
  4035. mapindex = p->data[j].sd->mapindex;
  4036. x = p->data[j].sd->bl.x;
  4037. y = p->data[j].sd->bl.y;
  4038. for (j = 0; j < MAX_PARTY; j++)
  4039. {
  4040. pl_sd = p->data[j].sd;
  4041. if (!pl_sd)
  4042. continue;
  4043. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4044. continue;
  4045. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4046. }
  4047. break;
  4048. case 4: // m,x,y
  4049. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4050. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4051. break;
  4052. }
  4053. }
  4054. return 0;
  4055. }
  4056. /*==========================================
  4057. * Warpguild - [Fredzilla]
  4058. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4059. *------------------------------------------*/
  4060. BUILDIN_FUNC(warpguild)
  4061. {
  4062. TBL_PC *sd;
  4063. TBL_PC *pl_sd;
  4064. struct guild* g;
  4065. struct s_mapiterator* iter;
  4066. int type;
  4067. const char* str = script_getstr(st,2);
  4068. int x = script_getnum(st,3);
  4069. int y = script_getnum(st,4);
  4070. int gid = script_getnum(st,5);
  4071. sd=script_rid2sd(st);
  4072. if( sd == NULL )
  4073. return 0;
  4074. g = guild_search(gid);
  4075. if( g == NULL )
  4076. return 0;
  4077. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4078. return 0;
  4079. type = ( strcmp(str,"Random")==0 ) ? 0
  4080. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4081. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4082. : 3;
  4083. iter = mapit_getallusers();
  4084. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4085. {
  4086. if( pl_sd->status.guild_id != gid )
  4087. continue;
  4088. switch( type )
  4089. {
  4090. case 0: // Random
  4091. if(!map[pl_sd->bl.m].flag.nowarp)
  4092. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4093. break;
  4094. case 1: // SavePointAll
  4095. if(!map[pl_sd->bl.m].flag.noreturn)
  4096. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4097. break;
  4098. case 2: // SavePoint
  4099. if(!map[pl_sd->bl.m].flag.noreturn)
  4100. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4101. break;
  4102. case 3: // m,x,y
  4103. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4104. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4105. break;
  4106. }
  4107. }
  4108. mapit_free(iter);
  4109. return 0;
  4110. }
  4111. /*==========================================
  4112. *
  4113. *------------------------------------------*/
  4114. BUILDIN_FUNC(heal)
  4115. {
  4116. TBL_PC *sd;
  4117. int hp,sp;
  4118. sd = script_rid2sd(st);
  4119. if (!sd) return 0;
  4120. hp=script_getnum(st,2);
  4121. sp=script_getnum(st,3);
  4122. status_heal(&sd->bl, hp, sp, 1);
  4123. return 0;
  4124. }
  4125. /*==========================================
  4126. *
  4127. *------------------------------------------*/
  4128. BUILDIN_FUNC(itemheal)
  4129. {
  4130. TBL_PC *sd;
  4131. int hp,sp;
  4132. hp=script_getnum(st,2);
  4133. sp=script_getnum(st,3);
  4134. if(potion_flag==1) {
  4135. potion_hp = hp;
  4136. potion_sp = sp;
  4137. return 0;
  4138. }
  4139. sd = script_rid2sd(st);
  4140. if (!sd) return 0;
  4141. pc_itemheal(sd,sd->itemid,hp,sp);
  4142. return 0;
  4143. }
  4144. /*==========================================
  4145. *
  4146. *------------------------------------------*/
  4147. BUILDIN_FUNC(percentheal)
  4148. {
  4149. int hp,sp;
  4150. TBL_PC* sd;
  4151. hp=script_getnum(st,2);
  4152. sp=script_getnum(st,3);
  4153. if(potion_flag==1) {
  4154. potion_per_hp = hp;
  4155. potion_per_sp = sp;
  4156. return 0;
  4157. }
  4158. sd = script_rid2sd(st);
  4159. if( sd == NULL )
  4160. return 0;
  4161. pc_percentheal(sd,hp,sp);
  4162. return 0;
  4163. }
  4164. /*==========================================
  4165. *
  4166. *------------------------------------------*/
  4167. BUILDIN_FUNC(jobchange)
  4168. {
  4169. int job, upper=-1;
  4170. job=script_getnum(st,2);
  4171. if( script_hasdata(st,3) )
  4172. upper=script_getnum(st,3);
  4173. if (pcdb_checkid(job))
  4174. {
  4175. TBL_PC* sd;
  4176. sd = script_rid2sd(st);
  4177. if( sd == NULL )
  4178. return 0;
  4179. pc_jobchange(sd, job, upper);
  4180. }
  4181. return 0;
  4182. }
  4183. /*==========================================
  4184. *
  4185. *------------------------------------------*/
  4186. BUILDIN_FUNC(jobname)
  4187. {
  4188. int class_=script_getnum(st,2);
  4189. script_pushconststr(st,job_name(class_));
  4190. return 0;
  4191. }
  4192. /// Get input from the player.
  4193. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4194. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4195. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4196. /// shorter than 'min' and 0 otherwise.
  4197. ///
  4198. /// input(<var>{,<min>{,<max>}}) -> <int>
  4199. BUILDIN_FUNC(input)
  4200. {
  4201. TBL_PC* sd;
  4202. struct script_data* data;
  4203. int uid;
  4204. const char* name;
  4205. int min;
  4206. int max;
  4207. sd = script_rid2sd(st);
  4208. if( sd == NULL )
  4209. return 0;
  4210. data = script_getdata(st,2);
  4211. if( !data_isreference(data) ){
  4212. ShowError("script:input: not a variable\n");
  4213. script_reportdata(data);
  4214. st->state = END;
  4215. return 1;
  4216. }
  4217. uid = reference_getuid(data);
  4218. name = reference_getname(data);
  4219. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4220. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4221. if( !sd->state.menu_or_input )
  4222. { // first invocation, display npc input box
  4223. sd->state.menu_or_input = 1;
  4224. st->state = RERUNLINE;
  4225. if( is_string_variable(name) )
  4226. clif_scriptinputstr(sd,st->oid);
  4227. else
  4228. clif_scriptinput(sd,st->oid);
  4229. }
  4230. else
  4231. { // take received text/value and store it in the designated variable
  4232. sd->state.menu_or_input = 0;
  4233. if( is_string_variable(name) )
  4234. {
  4235. int len = (int)strlen(sd->npc_str);
  4236. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4237. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4238. }
  4239. else
  4240. {
  4241. int amount = sd->npc_amount;
  4242. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4243. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4244. }
  4245. st->state = RUN;
  4246. }
  4247. return 0;
  4248. }
  4249. /// Sets the value of a variable.
  4250. /// The value is converted to the type of the variable.
  4251. ///
  4252. /// set(<variable>,<value>) -> <variable>
  4253. BUILDIN_FUNC(set)
  4254. {
  4255. TBL_PC* sd = NULL;
  4256. struct script_data* data;
  4257. int num;
  4258. const char* name;
  4259. char prefix;
  4260. data = script_getdata(st,2);
  4261. if( !data_isreference(data) )
  4262. {
  4263. ShowError("script:set: not a variable\n");
  4264. script_reportdata(script_getdata(st,2));
  4265. st->state = END;
  4266. return 1;
  4267. }
  4268. num = reference_getuid(data);
  4269. name = reference_getname(data);
  4270. prefix = *name;
  4271. if( not_server_variable(prefix) )
  4272. {
  4273. sd = script_rid2sd(st);
  4274. if( sd == NULL )
  4275. {
  4276. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4277. return 0;
  4278. }
  4279. }
  4280. if( is_string_variable(name) )
  4281. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4282. else
  4283. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4284. // return a copy of the variable reference
  4285. script_pushcopy(st,2);
  4286. return 0;
  4287. }
  4288. /////////////////////////////////////////////////////////////////////
  4289. /// Array variables
  4290. ///
  4291. /// Returns the size of the specified array
  4292. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4293. {
  4294. int32 ret = idx;
  4295. if( isstring )
  4296. {
  4297. for( ; idx < 128; ++idx )
  4298. {
  4299. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4300. if( str && *str )
  4301. ret = idx + 1;
  4302. script_removetop(st, -1, 0);
  4303. }
  4304. }
  4305. else
  4306. {
  4307. for( ; idx < 128; ++idx )
  4308. {
  4309. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4310. if( num )
  4311. ret = idx + 1;
  4312. script_removetop(st, -1, 0);
  4313. }
  4314. }
  4315. return ret;
  4316. }
  4317. /// Sets values of an array, from the starting index.
  4318. /// ex: setarray arr[1],1,2,3;
  4319. ///
  4320. /// setarray <array variable>,<value1>{,<value2>...};
  4321. BUILDIN_FUNC(setarray)
  4322. {
  4323. struct script_data* data;
  4324. const char* name;
  4325. int32 start;
  4326. int32 end;
  4327. int32 id;
  4328. int32 i;
  4329. TBL_PC* sd = NULL;
  4330. data = script_getdata(st, 2);
  4331. if( !data_isreference(data) )
  4332. {
  4333. ShowError("script:setarray: not a variable\n");
  4334. script_reportdata(data);
  4335. st->state = END;
  4336. return 1;// not a variable
  4337. }
  4338. id = reference_getid(data);
  4339. start = reference_getindex(data);
  4340. name = reference_getname(data);
  4341. if( not_array_variable(*name) )
  4342. {
  4343. ShowError("script:setarray: illegal scope\n");
  4344. script_reportdata(data);
  4345. st->state = END;
  4346. return 1;// not supported
  4347. }
  4348. if( not_server_variable(*name) )
  4349. {
  4350. sd = script_rid2sd(st);
  4351. if( sd == NULL )
  4352. return 0;// no player attached
  4353. }
  4354. end = start + script_lastdata(st) - 2;
  4355. if( end > 127 )
  4356. end = 127;
  4357. if( is_string_variable(name) )
  4358. {// string array
  4359. for( i = 3; start < end; ++start, ++i )
  4360. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4361. }
  4362. else
  4363. {// int array
  4364. for( i = 3; start < end; ++start, ++i )
  4365. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4366. }
  4367. return 0;
  4368. }
  4369. /// Sets count values of an array, from the starting index.
  4370. /// ex: cleararray arr[0],0,1;
  4371. ///
  4372. /// cleararray <array variable>,<value>,<count>;
  4373. BUILDIN_FUNC(cleararray)
  4374. {
  4375. struct script_data* data;
  4376. const char* name;
  4377. int32 start;
  4378. int32 end;
  4379. int32 id;
  4380. void* v;
  4381. TBL_PC* sd = NULL;
  4382. data = script_getdata(st, 2);
  4383. if( !data_isreference(data) )
  4384. {
  4385. ShowError("script:cleararray: not a variable\n");
  4386. script_reportdata(data);
  4387. st->state = END;
  4388. return 1;// not a variable
  4389. }
  4390. id = reference_getid(data);
  4391. start = reference_getindex(data);
  4392. name = reference_getname(data);
  4393. if( not_array_variable(*name) )
  4394. {
  4395. ShowError("script:cleararray: illegal scope\n");
  4396. script_reportdata(data);
  4397. st->state = END;
  4398. return 1;// not supported
  4399. }
  4400. if( not_server_variable(*name) )
  4401. {
  4402. sd = script_rid2sd(st);
  4403. if( sd == NULL )
  4404. return 0;// no player attached
  4405. }
  4406. if( is_string_variable(name) )
  4407. v = (void*)script_getstr(st, 3);
  4408. else
  4409. v = (void*)script_getnum(st, 3);
  4410. end = start + script_getnum(st, 4);
  4411. if( end > 127 )
  4412. end = 127;
  4413. for( ; start <= end; ++start )
  4414. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4415. return 0;
  4416. }
  4417. /// Copies data from one array to another.
  4418. /// ex: copyarray arr[0],arr[2],2;
  4419. ///
  4420. /// copyarray <destination array variable>,<source array variable>,<count>;
  4421. BUILDIN_FUNC(copyarray)
  4422. {
  4423. struct script_data* data1;
  4424. struct script_data* data2;
  4425. const char* name1;
  4426. const char* name2;
  4427. int32 idx1;
  4428. int32 idx2;
  4429. int32 id1;
  4430. int32 id2;
  4431. void* v;
  4432. int32 i;
  4433. int32 count;
  4434. TBL_PC* sd = NULL;
  4435. data1 = script_getdata(st, 2);
  4436. data2 = script_getdata(st, 3);
  4437. if( !data_isreference(data1) || !data_isreference(data2) )
  4438. {
  4439. ShowError("script:copyarray: not a variable\n");
  4440. script_reportdata(data1);
  4441. script_reportdata(data2);
  4442. st->state = END;
  4443. return 1;// not a variable
  4444. }
  4445. id1 = reference_getid(data1);
  4446. id2 = reference_getid(data2);
  4447. idx1 = reference_getindex(data1);
  4448. idx2 = reference_getindex(data2);
  4449. name1 = reference_getname(data1);
  4450. name2 = reference_getname(data2);
  4451. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4452. {
  4453. ShowError("script:copyarray: illegal scope\n");
  4454. script_reportdata(data1);
  4455. script_reportdata(data2);
  4456. st->state = END;
  4457. return 1;// not supported
  4458. }
  4459. if( is_string_variable(name1) != is_string_variable(name2) )
  4460. {
  4461. ShowError("script:copyarray: type mismatch\n");
  4462. script_reportdata(data1);
  4463. script_reportdata(data2);
  4464. st->state = END;
  4465. return 1;// data type mismatch
  4466. }
  4467. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4468. {
  4469. sd = script_rid2sd(st);
  4470. if( sd == NULL )
  4471. return 0;// no player attached
  4472. }
  4473. count = script_getnum(st, 4);
  4474. if( count > 128 - idx1 )
  4475. count = 128 - idx1;
  4476. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4477. return 0;// nothing to copy
  4478. if( id1 == id2 && idx1 > idx2 )
  4479. {// destination might be overlapping the source - copy in reverse order
  4480. for( i = count - 1; i >= 0; --i )
  4481. {
  4482. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4483. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4484. script_removetop(st, -1, 0);
  4485. }
  4486. }
  4487. else
  4488. {// normal copy
  4489. for( i = 0; i < count; ++i )
  4490. {
  4491. if( idx2 + i < 128 )
  4492. {
  4493. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4494. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4495. script_removetop(st, -1, 0);
  4496. }
  4497. else// out of range - assume ""/0
  4498. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4499. }
  4500. }
  4501. return 0;
  4502. }
  4503. /// Returns the size of the array.
  4504. /// Assumes that everything before the starting index exists.
  4505. /// ex: getarraysize(arr[3])
  4506. ///
  4507. /// getarraysize(<array variable>) -> <int>
  4508. BUILDIN_FUNC(getarraysize)
  4509. {
  4510. struct script_data* data;
  4511. const char* name;
  4512. data = script_getdata(st, 2);
  4513. if( !data_isreference(data) )
  4514. {
  4515. ShowError("script:getarraysize: not a variable\n");
  4516. script_reportdata(data);
  4517. script_pushnil(st);
  4518. st->state = END;
  4519. return 1;// not a variable
  4520. }
  4521. name = reference_getname(data);
  4522. if( not_array_variable(*name) )
  4523. {
  4524. ShowError("script:getarraysize: illegal scope\n");
  4525. script_reportdata(data);
  4526. script_pushnil(st);
  4527. st->state = END;
  4528. return 1;// not supported
  4529. }
  4530. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4531. return 0;
  4532. }
  4533. /// Deletes count or all the elements in an array, from the starting index.
  4534. /// ex: deletearray arr[4],2;
  4535. ///
  4536. /// deletearray <array variable>;
  4537. /// deletearray <array variable>,<count>;
  4538. BUILDIN_FUNC(deletearray)
  4539. {
  4540. struct script_data* data;
  4541. const char* name;
  4542. int start;
  4543. int end;
  4544. int id;
  4545. TBL_PC *sd = NULL;
  4546. data = script_getdata(st, 2);
  4547. if( !data_isreference(data) )
  4548. {
  4549. ShowError("script:deletearray: not a variable\n");
  4550. script_reportdata(data);
  4551. st->state = END;
  4552. return 1;// not a variable
  4553. }
  4554. id = reference_getid(data);
  4555. start = reference_getindex(data);
  4556. name = reference_getname(data);
  4557. if( not_array_variable(*name) )
  4558. {
  4559. ShowError("script:deletearray: illegal scope\n");
  4560. script_reportdata(data);
  4561. st->state = END;
  4562. return 1;// not supported
  4563. }
  4564. if( not_server_variable(*name) )
  4565. {
  4566. sd = script_rid2sd(st);
  4567. if( sd == NULL )
  4568. return 0;// no player attached
  4569. }
  4570. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4571. if( start >= end )
  4572. return 0;// nothing to free
  4573. if( script_hasdata(st,3) )
  4574. {
  4575. int count = script_getnum(st, 3);
  4576. if( count > end - start )
  4577. count = end - start;
  4578. if( count <= 0 )
  4579. return 0;// nothing to free
  4580. // move rest of the elements backward
  4581. for( ; start + count < end; ++start )
  4582. {
  4583. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4584. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4585. script_removetop(st, -1, 0);
  4586. }
  4587. }
  4588. // clear the rest of the array
  4589. if( is_string_variable(name) )
  4590. {
  4591. for( ; start < end; ++start )
  4592. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4593. }
  4594. else
  4595. {
  4596. for( ; start < end; ++start )
  4597. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4598. }
  4599. return 0;
  4600. }
  4601. /// Returns a reference to the target index of the array variable.
  4602. /// Equivalent to var[index].
  4603. ///
  4604. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4605. BUILDIN_FUNC(getelementofarray)
  4606. {
  4607. struct script_data* data;
  4608. const char* name;
  4609. int32 id;
  4610. int i;
  4611. data = script_getdata(st, 2);
  4612. if( !data_isreference(data) )
  4613. {
  4614. ShowError("script:getelementofarray: not a variable\n");
  4615. script_reportdata(data);
  4616. script_pushnil(st);
  4617. st->state = END;
  4618. return 1;// not a variable
  4619. }
  4620. id = reference_getid(data);
  4621. name = reference_getname(data);
  4622. if( not_array_variable(*name) )
  4623. {
  4624. ShowError("script:getelementofarray: illegal scope\n");
  4625. script_reportdata(data);
  4626. script_pushnil(st);
  4627. st->state = END;
  4628. return 1;// not supported
  4629. }
  4630. i = script_getnum(st, 3);
  4631. if( i < 0 || i >= 128 )
  4632. {
  4633. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4634. script_reportdata(data);
  4635. script_pushnil(st);
  4636. st->state = END;
  4637. return 1;// out of range
  4638. }
  4639. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4640. return 0;
  4641. }
  4642. /////////////////////////////////////////////////////////////////////
  4643. /// ...
  4644. ///
  4645. /*==========================================
  4646. *
  4647. *------------------------------------------*/
  4648. BUILDIN_FUNC(setlook)
  4649. {
  4650. int type,val;
  4651. TBL_PC* sd;
  4652. type=script_getnum(st,2);
  4653. val=script_getnum(st,3);
  4654. sd = script_rid2sd(st);
  4655. if( sd == NULL )
  4656. return 0;
  4657. pc_changelook(sd,type,val);
  4658. return 0;
  4659. }
  4660. BUILDIN_FUNC(changelook)
  4661. { // As setlook but only client side
  4662. int type,val;
  4663. TBL_PC* sd;
  4664. type=script_getnum(st,2);
  4665. val=script_getnum(st,3);
  4666. sd = script_rid2sd(st);
  4667. if( sd == NULL )
  4668. return 0;
  4669. clif_changelook(&sd->bl,type,val);
  4670. return 0;
  4671. }
  4672. /*==========================================
  4673. *
  4674. *------------------------------------------*/
  4675. BUILDIN_FUNC(cutin)
  4676. {
  4677. TBL_PC* sd;
  4678. sd = script_rid2sd(st);
  4679. if( sd == NULL )
  4680. return 0;
  4681. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4682. return 0;
  4683. }
  4684. /*==========================================
  4685. *
  4686. *------------------------------------------*/
  4687. BUILDIN_FUNC(viewpoint)
  4688. {
  4689. int type,x,y,id,color;
  4690. TBL_PC* sd;
  4691. type=script_getnum(st,2);
  4692. x=script_getnum(st,3);
  4693. y=script_getnum(st,4);
  4694. id=script_getnum(st,5);
  4695. color=script_getnum(st,6);
  4696. sd = script_rid2sd(st);
  4697. if( sd == NULL )
  4698. return 0;
  4699. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4700. return 0;
  4701. }
  4702. /*==========================================
  4703. *
  4704. *------------------------------------------*/
  4705. BUILDIN_FUNC(countitem)
  4706. {
  4707. int nameid, i;
  4708. int count = 0;
  4709. struct script_data* data;
  4710. TBL_PC* sd = script_rid2sd(st);
  4711. if (!sd) {
  4712. script_pushint(st,0);
  4713. return 0;
  4714. }
  4715. data = script_getdata(st,2);
  4716. get_val(st,data);
  4717. if( data_isstring(data) ) {
  4718. const char* name = conv_str(st,data);
  4719. struct item_data* item_data;
  4720. if((item_data = itemdb_searchname(name)) != NULL)
  4721. nameid = item_data->nameid;
  4722. else
  4723. nameid = 0;
  4724. } else
  4725. nameid = conv_num(st,data);
  4726. if (nameid < 500) {
  4727. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4728. script_reportsrc(st);
  4729. script_pushint(st,0);
  4730. return 1;
  4731. }
  4732. for(i = 0; i < MAX_INVENTORY; i++)
  4733. if(sd->status.inventory[i].nameid == nameid)
  4734. count += sd->status.inventory[i].amount;
  4735. script_pushint(st,count);
  4736. return 0;
  4737. }
  4738. /*==========================================
  4739. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4740. * returns number of items that meet the conditions
  4741. *------------------------------------------*/
  4742. BUILDIN_FUNC(countitem2)
  4743. {
  4744. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4745. int count = 0;
  4746. int i;
  4747. struct script_data* data;
  4748. TBL_PC* sd = script_rid2sd(st);
  4749. if (!sd) {
  4750. script_pushint(st,0);
  4751. return 0;
  4752. }
  4753. data = script_getdata(st,2);
  4754. get_val(st,data);
  4755. if( data_isstring(data) ) {
  4756. const char* name = conv_str(st,data);
  4757. struct item_data* item_data;
  4758. if((item_data = itemdb_searchname(name)) != NULL)
  4759. nameid = item_data->nameid;
  4760. else
  4761. nameid = 0;
  4762. } else
  4763. nameid = conv_num(st,data);
  4764. iden = script_getnum(st,3);
  4765. ref = script_getnum(st,4);
  4766. attr = script_getnum(st,5);
  4767. c1 = (short)script_getnum(st,6);
  4768. c2 = (short)script_getnum(st,7);
  4769. c3 = (short)script_getnum(st,8);
  4770. c4 = (short)script_getnum(st,9);
  4771. if (nameid < 500) {
  4772. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4773. script_pushint(st,0);
  4774. return 1;
  4775. }
  4776. for(i = 0; i < MAX_INVENTORY; i++)
  4777. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4778. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4779. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4780. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4781. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4782. sd->status.inventory[i].card[3] == c4
  4783. )
  4784. count += sd->status.inventory[i].amount;
  4785. script_pushint(st,count);
  4786. return 0;
  4787. }
  4788. /*==========================================
  4789. * 重量チェック
  4790. *------------------------------------------*/
  4791. BUILDIN_FUNC(checkweight)
  4792. {
  4793. int nameid=0,amount,i;
  4794. unsigned long weight;
  4795. TBL_PC *sd;
  4796. struct script_data *data;
  4797. sd = script_rid2sd(st);
  4798. if( sd == NULL )
  4799. return 0;
  4800. data=script_getdata(st,2);
  4801. get_val(st,data);
  4802. if( data_isstring(data) ){
  4803. const char *name=conv_str(st,data);
  4804. struct item_data *item_data = itemdb_searchname(name);
  4805. if( item_data )
  4806. nameid=item_data->nameid;
  4807. }else
  4808. nameid=conv_num(st,data);
  4809. amount=script_getnum(st,3);
  4810. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4811. script_pushint(st,0);
  4812. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4813. return 1;
  4814. }
  4815. weight = itemdb_weight(nameid)*amount;
  4816. if( amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight )
  4817. script_pushint(st,0);
  4818. else if( itemdb_isstackable(nameid) )
  4819. {
  4820. if( (i = pc_search_inventory(sd,nameid)) >= 0 )
  4821. script_pushint(st,amount + sd->status.inventory[i].amount > MAX_AMOUNT ? 0 : 1);
  4822. else
  4823. script_pushint(st,pc_search_inventory(sd,0) >= 0 ? 1 : 0);
  4824. }
  4825. else
  4826. {
  4827. for( i = 0; i < MAX_INVENTORY && amount; ++i )
  4828. if( sd->status.inventory[i].nameid == 0 )
  4829. amount--;
  4830. script_pushint(st,amount ? 0 : 1);
  4831. }
  4832. return 0;
  4833. }
  4834. /*==========================================
  4835. * getitem <item id>,<amount>{,<character ID>};
  4836. * getitem "<item name>",<amount>{,<character ID>};
  4837. *------------------------------------------*/
  4838. BUILDIN_FUNC(getitem)
  4839. {
  4840. int nameid,amount,get_count,i,flag = 0;
  4841. struct item it;
  4842. TBL_PC *sd;
  4843. struct script_data *data;
  4844. data=script_getdata(st,2);
  4845. get_val(st,data);
  4846. if( data_isstring(data) )
  4847. {// "<item name>"
  4848. const char *name=conv_str(st,data);
  4849. struct item_data *item_data = itemdb_searchname(name);
  4850. if( item_data == NULL ){
  4851. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4852. return 1; //No item created.
  4853. }
  4854. nameid=item_data->nameid;
  4855. } else if( data_isint(data) )
  4856. {// <item id>
  4857. nameid=conv_num(st,data);
  4858. //Violet Box, Blue Box, etc - random item pick
  4859. if( nameid < 0 ) {
  4860. nameid=itemdb_searchrandomid(-nameid);
  4861. flag = 1;
  4862. }
  4863. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4864. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4865. return 1; //No item created.
  4866. }
  4867. } else {
  4868. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4869. return 1;
  4870. }
  4871. // <amount>
  4872. if( (amount=script_getnum(st,3)) <= 0)
  4873. return 0; //return if amount <=0, skip the useles iteration
  4874. memset(&it,0,sizeof(it));
  4875. it.nameid=nameid;
  4876. if(!flag)
  4877. it.identify=1;
  4878. else
  4879. it.identify=itemdb_isidentified(nameid);
  4880. if( script_hasdata(st,4) )
  4881. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4882. else
  4883. sd=script_rid2sd(st); // Attached player
  4884. if( sd == NULL ) // no target
  4885. return 0;
  4886. //Check if it's stackable.
  4887. if (!itemdb_isstackable(nameid))
  4888. get_count = 1;
  4889. else
  4890. get_count = amount;
  4891. for (i = 0; i < amount; i += get_count)
  4892. {
  4893. // if not pet egg
  4894. if (!pet_create_egg(sd, nameid))
  4895. {
  4896. if ((flag = pc_additem(sd, &it, get_count)))
  4897. {
  4898. clif_additem(sd, 0, 0, flag);
  4899. if( pc_candrop(sd,&it) )
  4900. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4901. }
  4902. }
  4903. }
  4904. //Logs items, got from (N)PC scripts [Lupus]
  4905. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4906. log_pick_pc(sd, "N", nameid, amount, NULL);
  4907. return 0;
  4908. }
  4909. /*==========================================
  4910. *
  4911. *------------------------------------------*/
  4912. BUILDIN_FUNC(getitem2)
  4913. {
  4914. int nameid,amount,get_count,i,flag = 0;
  4915. int iden,ref,attr,c1,c2,c3,c4;
  4916. struct item_data *item_data;
  4917. struct item item_tmp;
  4918. TBL_PC *sd;
  4919. struct script_data *data;
  4920. if( script_hasdata(st,11) )
  4921. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4922. else
  4923. sd=script_rid2sd(st); // Attached player
  4924. if( sd == NULL ) // no target
  4925. return 0;
  4926. data=script_getdata(st,2);
  4927. get_val(st,data);
  4928. if( data_isstring(data) ){
  4929. const char *name=conv_str(st,data);
  4930. struct item_data *item_data = itemdb_searchname(name);
  4931. if( item_data )
  4932. nameid=item_data->nameid;
  4933. else
  4934. nameid=UNKNOWN_ITEM_ID;
  4935. }else
  4936. nameid=conv_num(st,data);
  4937. amount=script_getnum(st,3);
  4938. iden=script_getnum(st,4);
  4939. ref=script_getnum(st,5);
  4940. attr=script_getnum(st,6);
  4941. c1=(short)script_getnum(st,7);
  4942. c2=(short)script_getnum(st,8);
  4943. c3=(short)script_getnum(st,9);
  4944. c4=(short)script_getnum(st,10);
  4945. if(nameid<0) { // ランダム
  4946. nameid=itemdb_searchrandomid(-nameid);
  4947. flag = 1;
  4948. }
  4949. if(nameid > 0) {
  4950. memset(&item_tmp,0,sizeof(item_tmp));
  4951. item_data=itemdb_exists(nameid);
  4952. if (item_data == NULL)
  4953. return -1;
  4954. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4955. if(ref > 10) ref = 10;
  4956. }
  4957. else if(item_data->type==IT_PETEGG) {
  4958. iden = 1;
  4959. ref = 0;
  4960. }
  4961. else {
  4962. iden = 1;
  4963. ref = attr = 0;
  4964. }
  4965. item_tmp.nameid=nameid;
  4966. if(!flag)
  4967. item_tmp.identify=iden;
  4968. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4969. item_tmp.identify=0;
  4970. item_tmp.refine=ref;
  4971. item_tmp.attribute=attr;
  4972. item_tmp.card[0]=(short)c1;
  4973. item_tmp.card[1]=(short)c2;
  4974. item_tmp.card[2]=(short)c3;
  4975. item_tmp.card[3]=(short)c4;
  4976. //Check if it's stackable.
  4977. if (!itemdb_isstackable(nameid))
  4978. get_count = 1;
  4979. else
  4980. get_count = amount;
  4981. for (i = 0; i < amount; i += get_count)
  4982. {
  4983. // if not pet egg
  4984. if (!pet_create_egg(sd, nameid))
  4985. {
  4986. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  4987. {
  4988. clif_additem(sd, 0, 0, flag);
  4989. if( pc_candrop(sd,&item_tmp) )
  4990. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4991. }
  4992. }
  4993. }
  4994. //Logs items, got from (N)PC scripts [Lupus]
  4995. if(log_config.enable_logs&0x40)
  4996. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4997. }
  4998. return 0;
  4999. }
  5000. /*==========================================
  5001. * rentitem <item id>,<seconds>
  5002. * rentitem "<item name>",<seconds>
  5003. *------------------------------------------*/
  5004. BUILDIN_FUNC(rentitem)
  5005. {
  5006. struct map_session_data *sd;
  5007. struct script_data *data;
  5008. struct item it;
  5009. int seconds;
  5010. int nameid = 0, flag;
  5011. data = script_getdata(st,2);
  5012. get_val(st,data);
  5013. if( (sd = script_rid2sd(st)) == NULL )
  5014. return 0;
  5015. if( data_isstring(data) )
  5016. {
  5017. const char *name = conv_str(st,data);
  5018. struct item_data *itd = itemdb_searchname(name);
  5019. if( itd == NULL )
  5020. {
  5021. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5022. return 1;
  5023. }
  5024. nameid = itd->nameid;
  5025. }
  5026. else if( data_isint(data) )
  5027. {
  5028. nameid = conv_num(st,data);
  5029. if( nameid <= 0 || !itemdb_exists(nameid) )
  5030. {
  5031. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5032. return 1;
  5033. }
  5034. }
  5035. else
  5036. {
  5037. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5038. return 1;
  5039. }
  5040. seconds = script_getnum(st,3);
  5041. memset(&it, 0, sizeof(it));
  5042. it.nameid = nameid;
  5043. it.identify = 1;
  5044. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5045. if( (flag = pc_additem(sd, &it, 1)) )
  5046. {
  5047. clif_additem(sd, 0, 0, flag);
  5048. return 1;
  5049. }
  5050. clif_rental_time(sd->fd, nameid, seconds);
  5051. pc_inventory_rental_add(sd, seconds);
  5052. if( log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS )
  5053. log_pick_pc(sd, "N", nameid, 1, NULL);
  5054. return 0;
  5055. }
  5056. /*==========================================
  5057. * gets an item with someone's name inscribed [Skotlex]
  5058. * getinscribeditem item_num, character_name
  5059. * Returned Qty is always 1, only works on equip-able
  5060. * equipment
  5061. *------------------------------------------*/
  5062. BUILDIN_FUNC(getnameditem)
  5063. {
  5064. int nameid;
  5065. struct item item_tmp;
  5066. TBL_PC *sd, *tsd;
  5067. struct script_data *data;
  5068. sd = script_rid2sd(st);
  5069. if (sd == NULL)
  5070. { //Player not attached!
  5071. script_pushint(st,0);
  5072. return 0;
  5073. }
  5074. data=script_getdata(st,2);
  5075. get_val(st,data);
  5076. if( data_isstring(data) ){
  5077. const char *name=conv_str(st,data);
  5078. struct item_data *item_data = itemdb_searchname(name);
  5079. if( item_data == NULL)
  5080. { //Failed
  5081. script_pushint(st,0);
  5082. return 0;
  5083. }
  5084. nameid = item_data->nameid;
  5085. }else
  5086. nameid = conv_num(st,data);
  5087. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5088. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5089. script_pushint(st,0);
  5090. return 0;
  5091. }
  5092. data=script_getdata(st,3);
  5093. get_val(st,data);
  5094. if( data_isstring(data) ) //Char Name
  5095. tsd=map_nick2sd(conv_str(st,data));
  5096. else //Char Id was given
  5097. tsd=map_charid2sd(conv_num(st,data));
  5098. if( tsd == NULL )
  5099. { //Failed
  5100. script_pushint(st,0);
  5101. return 0;
  5102. }
  5103. memset(&item_tmp,0,sizeof(item_tmp));
  5104. item_tmp.nameid=nameid;
  5105. item_tmp.amount=1;
  5106. item_tmp.identify=1;
  5107. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5108. item_tmp.card[2]=tsd->status.char_id;
  5109. item_tmp.card[3]=tsd->status.char_id >> 16;
  5110. if(pc_additem(sd,&item_tmp,1)) {
  5111. script_pushint(st,0);
  5112. return 0; //Failed to add item, we will not drop if they don't fit
  5113. }
  5114. //Logs items, got from (N)PC scripts [Lupus]
  5115. if(log_config.enable_logs&0x40)
  5116. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5117. script_pushint(st,1);
  5118. return 0;
  5119. }
  5120. /*==========================================
  5121. * gets a random item ID from an item group [Skotlex]
  5122. * groupranditem group_num
  5123. *------------------------------------------*/
  5124. BUILDIN_FUNC(grouprandomitem)
  5125. {
  5126. int group;
  5127. group = script_getnum(st,2);
  5128. script_pushint(st,itemdb_searchrandomid(group));
  5129. return 0;
  5130. }
  5131. /*==========================================
  5132. *
  5133. *------------------------------------------*/
  5134. BUILDIN_FUNC(makeitem)
  5135. {
  5136. int nameid,amount,flag = 0;
  5137. int x,y,m;
  5138. const char *mapname;
  5139. struct item item_tmp;
  5140. struct script_data *data;
  5141. data=script_getdata(st,2);
  5142. get_val(st,data);
  5143. if( data_isstring(data) ){
  5144. const char *name=conv_str(st,data);
  5145. struct item_data *item_data = itemdb_searchname(name);
  5146. if( item_data )
  5147. nameid=item_data->nameid;
  5148. else
  5149. nameid=UNKNOWN_ITEM_ID;
  5150. }else
  5151. nameid=conv_num(st,data);
  5152. amount=script_getnum(st,3);
  5153. mapname =script_getstr(st,4);
  5154. x =script_getnum(st,5);
  5155. y =script_getnum(st,6);
  5156. if(strcmp(mapname,"this")==0)
  5157. {
  5158. TBL_PC *sd;
  5159. sd = script_rid2sd(st);
  5160. if (!sd) return 0; //Failed...
  5161. m=sd->bl.m;
  5162. } else
  5163. m=map_mapname2mapid(mapname);
  5164. if(nameid<0) { // ランダム
  5165. nameid=itemdb_searchrandomid(-nameid);
  5166. flag = 1;
  5167. }
  5168. if(nameid > 0) {
  5169. memset(&item_tmp,0,sizeof(item_tmp));
  5170. item_tmp.nameid=nameid;
  5171. if(!flag)
  5172. item_tmp.identify=1;
  5173. else
  5174. item_tmp.identify=itemdb_isidentified(nameid);
  5175. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5176. }
  5177. return 0;
  5178. }
  5179. /// Deletes items from the target/attached player.
  5180. /// Prioritizes ordinary items.
  5181. ///
  5182. /// delitem <item id>,<amount>{,<account id>}
  5183. /// delitem "<item name>",<amount>{,<account id>}
  5184. BUILDIN_FUNC(delitem)
  5185. {
  5186. int nameid=0,amount,i,important_item=0;
  5187. TBL_PC *sd;
  5188. struct script_data *data;
  5189. if( script_hasdata(st,4) )
  5190. {
  5191. int account_id = script_getnum(st,4);
  5192. sd = map_id2sd(account_id); // <account id>
  5193. if( sd == NULL )
  5194. {
  5195. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5196. st->state = END;
  5197. return 1;
  5198. }
  5199. }
  5200. else
  5201. {
  5202. sd = script_rid2sd(st);// attached player
  5203. if( sd == NULL )
  5204. return 0;
  5205. }
  5206. data = script_getdata(st,2);
  5207. get_val(st,data);
  5208. if( data_isstring(data) )
  5209. {
  5210. const char* item_name = conv_str(st,data);
  5211. struct item_data* id = itemdb_searchname(item_name);
  5212. if( id == NULL )
  5213. {
  5214. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5215. st->state = END;
  5216. return 1;
  5217. }
  5218. nameid = id->nameid;// "<item name>"
  5219. }
  5220. else
  5221. nameid = conv_num(st,data);// <item id>
  5222. amount=script_getnum(st,3);
  5223. if( amount <= 0 )
  5224. return 0;// nothing to do
  5225. //1st pass
  5226. //here we won't delete items with CARDS, named items but we count them
  5227. for(i=0;i<MAX_INVENTORY;i++){
  5228. //we don't delete wrong item or equipped item
  5229. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5230. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5231. continue;
  5232. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5233. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5234. sd->status.inventory[i].card[0] == CARD0_PET)
  5235. {
  5236. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5237. continue; //pet couldn't be sent for deletion.
  5238. } else
  5239. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5240. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5241. sd->status.inventory[i].card[0] ||
  5242. sd->status.inventory[i].refine) {
  5243. //this is important item, count it (except for pet eggs)
  5244. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5245. important_item++;
  5246. continue;
  5247. }
  5248. if(sd->status.inventory[i].amount>=amount){
  5249. //Logs items, got from (N)PC scripts [Lupus]
  5250. if(log_config.enable_logs&0x40)
  5251. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5252. pc_delitem(sd,i,amount,0,0);
  5253. return 0; //we deleted exact amount of items. now exit
  5254. } else {
  5255. amount-=sd->status.inventory[i].amount;
  5256. //Logs items, got from (N)PC scripts [Lupus]
  5257. if(log_config.enable_logs&0x40) {
  5258. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5259. }
  5260. //Logs
  5261. pc_delitem(sd,i,sd->status.inventory[i].amount,0,0);
  5262. }
  5263. }
  5264. //2nd pass
  5265. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5266. if (important_item>0 && amount>0)
  5267. for(i=0;i<MAX_INVENTORY;i++){
  5268. //we don't delete wrong item
  5269. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5270. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5271. continue;
  5272. if(sd->status.inventory[i].amount>=amount){
  5273. //Logs items, got from (N)PC scripts [Lupus]
  5274. if(log_config.enable_logs&0x40)
  5275. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5276. pc_delitem(sd,i,amount,0,0);
  5277. return 0; //we deleted exact amount of items. now exit
  5278. } else {
  5279. amount-=sd->status.inventory[i].amount;
  5280. //Logs items, got from (N)PC scripts [Lupus]
  5281. if(log_config.enable_logs&0x40)
  5282. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5283. pc_delitem(sd,i,sd->status.inventory[i].amount,0,0);
  5284. }
  5285. }
  5286. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5287. st->state = END;
  5288. clif_scriptclose(sd, st->oid);
  5289. return 1;
  5290. }
  5291. /// Deletes items from the target/attached player.
  5292. ///
  5293. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5294. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5295. BUILDIN_FUNC(delitem2)
  5296. {
  5297. int nameid=0,amount,i=0;
  5298. int iden,ref,attr,c1,c2,c3,c4;
  5299. TBL_PC *sd;
  5300. struct script_data *data;
  5301. if( script_hasdata(st,11) )
  5302. {
  5303. int account_id = script_getnum(st,11);
  5304. sd = map_id2sd(account_id); // <account id>
  5305. if( sd == NULL )
  5306. {
  5307. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5308. st->state = END;
  5309. return 1;
  5310. }
  5311. }
  5312. else
  5313. {
  5314. sd = script_rid2sd(st);// attached player
  5315. if( sd == NULL )
  5316. return 0;
  5317. }
  5318. data = script_getdata(st,2);
  5319. get_val(st,data);
  5320. if( data_isstring(data) )
  5321. {
  5322. const char* item_name = conv_str(st,data);
  5323. struct item_data* id = itemdb_searchname(item_name);
  5324. if( id == NULL )
  5325. {
  5326. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5327. st->state = END;
  5328. return 1;
  5329. }
  5330. nameid = id->nameid;// "<item name>"
  5331. }
  5332. else
  5333. nameid = conv_num(st,data);// <item id>
  5334. amount=script_getnum(st,3);
  5335. iden=script_getnum(st,4);
  5336. ref=script_getnum(st,5);
  5337. attr=script_getnum(st,6);
  5338. c1=(short)script_getnum(st,7);
  5339. c2=(short)script_getnum(st,8);
  5340. c3=(short)script_getnum(st,9);
  5341. c4=(short)script_getnum(st,10);
  5342. if( amount <= 0 )
  5343. return 0;// nothing to do
  5344. for(i=0;i<MAX_INVENTORY;i++){
  5345. //we don't delete wrong item or equipped item
  5346. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5347. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5348. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5349. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5350. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5351. sd->status.inventory[i].card[3]!=c4)
  5352. continue;
  5353. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5354. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5355. {
  5356. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5357. continue; //Failed to send delete the pet.
  5358. }
  5359. if(sd->status.inventory[i].amount>=amount){
  5360. //Logs items, got from (N)PC scripts [Lupus]
  5361. if(log_config.enable_logs&0x40)
  5362. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5363. pc_delitem(sd,i,amount,0,0);
  5364. return 0; //we deleted exact amount of items. now exit
  5365. } else {
  5366. amount-=sd->status.inventory[i].amount;
  5367. //Logs items, got from (N)PC scripts [Lupus]
  5368. if(log_config.enable_logs&0x40)
  5369. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5370. pc_delitem(sd,i,sd->status.inventory[i].amount,0,0);
  5371. }
  5372. }
  5373. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5374. st->state = END;
  5375. clif_scriptclose(sd, st->oid);
  5376. return 1;
  5377. }
  5378. /*==========================================
  5379. * Enables/Disables use of items while in an NPC [Skotlex]
  5380. *------------------------------------------*/
  5381. BUILDIN_FUNC(enableitemuse)
  5382. {
  5383. TBL_PC *sd;
  5384. sd=script_rid2sd(st);
  5385. if (sd)
  5386. sd->npc_item_flag = st->oid;
  5387. return 0;
  5388. }
  5389. BUILDIN_FUNC(disableitemuse)
  5390. {
  5391. TBL_PC *sd;
  5392. sd=script_rid2sd(st);
  5393. if (sd)
  5394. sd->npc_item_flag = 0;
  5395. return 0;
  5396. }
  5397. /*==========================================
  5398. *キャラ関係のパラメータ取得
  5399. *------------------------------------------*/
  5400. BUILDIN_FUNC(readparam)
  5401. {
  5402. int type;
  5403. TBL_PC *sd;
  5404. type=script_getnum(st,2);
  5405. if( script_hasdata(st,3) )
  5406. sd=map_nick2sd(script_getstr(st,3));
  5407. else
  5408. sd=script_rid2sd(st);
  5409. if(sd==NULL){
  5410. script_pushint(st,-1);
  5411. return 0;
  5412. }
  5413. script_pushint(st,pc_readparam(sd,type));
  5414. return 0;
  5415. }
  5416. /*==========================================
  5417. *キャラ関係のID取得
  5418. *------------------------------------------*/
  5419. BUILDIN_FUNC(getcharid)
  5420. {
  5421. int num;
  5422. TBL_PC *sd;
  5423. num = script_getnum(st,2);
  5424. if( script_hasdata(st,3) )
  5425. sd=map_nick2sd(script_getstr(st,3));
  5426. else
  5427. sd=script_rid2sd(st);
  5428. if(sd==NULL){
  5429. script_pushint(st,0); //return 0, according docs
  5430. return 0;
  5431. }
  5432. switch( num ) {
  5433. case 0: script_pushint(st,sd->status.char_id); break;
  5434. case 1: script_pushint(st,sd->status.party_id); break;
  5435. case 2: script_pushint(st,sd->status.guild_id); break;
  5436. case 3: script_pushint(st,sd->status.account_id); break;
  5437. case 4: script_pushint(st,sd->state.bg_id); break;
  5438. default:
  5439. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5440. script_pushint(st,0);
  5441. break;
  5442. }
  5443. return 0;
  5444. }
  5445. /*==========================================
  5446. *指定IDのPT名取得
  5447. *------------------------------------------*/
  5448. BUILDIN_FUNC(getpartyname)
  5449. {
  5450. int party_id;
  5451. struct party_data* p;
  5452. party_id = script_getnum(st,2);
  5453. if( ( p = party_search(party_id) ) != NULL )
  5454. {
  5455. script_pushstrcopy(st,p->party.name);
  5456. }
  5457. else
  5458. {
  5459. script_pushconststr(st,"null");
  5460. }
  5461. return 0;
  5462. }
  5463. /*==========================================
  5464. *指定IDのPT人数とメンバーID取得
  5465. *------------------------------------------*/
  5466. BUILDIN_FUNC(getpartymember)
  5467. {
  5468. struct party_data *p;
  5469. int i,j=0,type=0;
  5470. p=party_search(script_getnum(st,2));
  5471. if( script_hasdata(st,3) )
  5472. type=script_getnum(st,3);
  5473. if(p!=NULL){
  5474. for(i=0;i<MAX_PARTY;i++){
  5475. if(p->party.member[i].account_id){
  5476. switch (type) {
  5477. case 2:
  5478. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5479. break;
  5480. case 1:
  5481. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5482. break;
  5483. default:
  5484. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5485. }
  5486. j++;
  5487. }
  5488. }
  5489. }
  5490. mapreg_setreg(add_str("$@partymembercount"),j);
  5491. return 0;
  5492. }
  5493. /*==========================================
  5494. * Retrieves party leader. if flag is specified,
  5495. * return some of the leader data. Otherwise, return name.
  5496. *------------------------------------------*/
  5497. BUILDIN_FUNC(getpartyleader)
  5498. {
  5499. int party_id, type = 0, i=0;
  5500. struct party_data *p;
  5501. party_id=script_getnum(st,2);
  5502. if( script_hasdata(st,3) )
  5503. type=script_getnum(st,3);
  5504. p=party_search(party_id);
  5505. if (p) //Search leader
  5506. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5507. if (!p || i == MAX_PARTY) { //leader not found
  5508. if (type)
  5509. script_pushint(st,-1);
  5510. else
  5511. script_pushconststr(st,"null");
  5512. return 0;
  5513. }
  5514. switch (type) {
  5515. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5516. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5517. case 3: script_pushint(st,p->party.member[i].class_); break;
  5518. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5519. case 5: script_pushint(st,p->party.member[i].lv); break;
  5520. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5521. }
  5522. return 0;
  5523. }
  5524. /*==========================================
  5525. *指定IDのギルド名取得
  5526. *------------------------------------------*/
  5527. BUILDIN_FUNC(getguildname)
  5528. {
  5529. int guild_id;
  5530. struct guild* g;
  5531. guild_id = script_getnum(st,2);
  5532. if( ( g = guild_search(guild_id) ) != NULL )
  5533. {
  5534. script_pushstrcopy(st,g->name);
  5535. }
  5536. else
  5537. {
  5538. script_pushconststr(st,"null");
  5539. }
  5540. return 0;
  5541. }
  5542. /*==========================================
  5543. *指定IDのGuildMaster名取得
  5544. *------------------------------------------*/
  5545. BUILDIN_FUNC(getguildmaster)
  5546. {
  5547. int guild_id;
  5548. struct guild* g;
  5549. guild_id = script_getnum(st,2);
  5550. if( ( g = guild_search(guild_id) ) != NULL )
  5551. {
  5552. script_pushstrcopy(st,g->member[0].name);
  5553. }
  5554. else
  5555. {
  5556. script_pushconststr(st,"null");
  5557. }
  5558. return 0;
  5559. }
  5560. BUILDIN_FUNC(getguildmasterid)
  5561. {
  5562. int guild_id;
  5563. struct guild* g;
  5564. guild_id = script_getnum(st,2);
  5565. if( ( g = guild_search(guild_id) ) != NULL )
  5566. {
  5567. script_pushint(st,g->member[0].char_id);
  5568. }
  5569. else
  5570. {
  5571. script_pushint(st,0);
  5572. }
  5573. return 0;
  5574. }
  5575. /*==========================================
  5576. * キャラクタの名前
  5577. *------------------------------------------*/
  5578. BUILDIN_FUNC(strcharinfo)
  5579. {
  5580. TBL_PC *sd;
  5581. int num;
  5582. struct guild* g;
  5583. struct party_data* p;
  5584. sd=script_rid2sd(st);
  5585. if (!sd) { //Avoid crashing....
  5586. script_pushconststr(st,"");
  5587. return 0;
  5588. }
  5589. num=script_getnum(st,2);
  5590. switch(num){
  5591. case 0:
  5592. script_pushstrcopy(st,sd->status.name);
  5593. break;
  5594. case 1:
  5595. if( ( p = party_search(sd->status.party_id) ) != NULL )
  5596. {
  5597. script_pushstrcopy(st,p->party.name);
  5598. }
  5599. else
  5600. {
  5601. script_pushconststr(st,"");
  5602. }
  5603. break;
  5604. case 2:
  5605. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  5606. {
  5607. script_pushstrcopy(st,g->name);
  5608. }
  5609. else
  5610. {
  5611. script_pushconststr(st,"");
  5612. }
  5613. break;
  5614. case 3:
  5615. script_pushconststr(st,map[sd->bl.m].name);
  5616. break;
  5617. default:
  5618. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5619. script_pushconststr(st,"");
  5620. break;
  5621. }
  5622. return 0;
  5623. }
  5624. /*==========================================
  5625. * 呼び出し元のNPC情報を取得する
  5626. *------------------------------------------*/
  5627. BUILDIN_FUNC(strnpcinfo)
  5628. {
  5629. TBL_NPC* nd;
  5630. int num;
  5631. char *buf,*name=NULL;
  5632. nd = map_id2nd(st->oid);
  5633. if (!nd) {
  5634. script_pushconststr(st, "");
  5635. return 0;
  5636. }
  5637. num = script_getnum(st,2);
  5638. switch(num){
  5639. case 0: // display name
  5640. name = aStrdup(nd->name);
  5641. break;
  5642. case 1: // visible part of display name
  5643. if((buf = strchr(nd->name,'#')) != NULL)
  5644. {
  5645. name = aStrdup(nd->name);
  5646. name[buf - nd->name] = 0;
  5647. } else // Return the name, there is no '#' present
  5648. name = aStrdup(nd->name);
  5649. break;
  5650. case 2: // # fragment
  5651. if((buf = strchr(nd->name,'#')) != NULL)
  5652. name = aStrdup(buf+1);
  5653. break;
  5654. case 3: // unique name
  5655. name = aStrdup(nd->exname);
  5656. break;
  5657. }
  5658. if(name)
  5659. script_pushstr(st, name);
  5660. else
  5661. script_pushconststr(st, "");
  5662. return 0;
  5663. }
  5664. // aegis->athena slot position conversion table
  5665. 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};
  5666. /*==========================================
  5667. * GetEquipID(Pos); Pos: 1-10
  5668. *------------------------------------------*/
  5669. BUILDIN_FUNC(getequipid)
  5670. {
  5671. int i, num;
  5672. TBL_PC* sd;
  5673. struct item_data* item;
  5674. sd = script_rid2sd(st);
  5675. if( sd == NULL )
  5676. return 0;
  5677. num = script_getnum(st,2) - 1;
  5678. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5679. {
  5680. script_pushint(st,-1);
  5681. return 0;
  5682. }
  5683. // get inventory position of item
  5684. i = pc_checkequip(sd,equip[num]);
  5685. if( i < 0 )
  5686. {
  5687. script_pushint(st,-1);
  5688. return 0;
  5689. }
  5690. item = sd->inventory_data[i];
  5691. if( item != 0 )
  5692. script_pushint(st,item->nameid);
  5693. else
  5694. script_pushint(st,0);
  5695. return 0;
  5696. }
  5697. /*==========================================
  5698. * 装備名文字列(精錬メニュー用)
  5699. *------------------------------------------*/
  5700. BUILDIN_FUNC(getequipname)
  5701. {
  5702. int i, num;
  5703. TBL_PC* sd;
  5704. struct item_data* item;
  5705. sd = script_rid2sd(st);
  5706. if( sd == NULL )
  5707. return 0;
  5708. num = script_getnum(st,2) - 1;
  5709. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5710. {
  5711. script_pushconststr(st,"");
  5712. return 0;
  5713. }
  5714. // get inventory position of item
  5715. i = pc_checkequip(sd,equip[num]);
  5716. if( i < 0 )
  5717. {
  5718. script_pushint(st,-1);
  5719. return 0;
  5720. }
  5721. item = sd->inventory_data[i];
  5722. if( item != 0 )
  5723. script_pushstrcopy(st,item->jname);
  5724. else
  5725. script_pushconststr(st,"");
  5726. return 0;
  5727. }
  5728. /*==========================================
  5729. * getbrokenid [Valaris]
  5730. *------------------------------------------*/
  5731. BUILDIN_FUNC(getbrokenid)
  5732. {
  5733. int i,num,id=0,brokencounter=0;
  5734. TBL_PC *sd;
  5735. sd = script_rid2sd(st);
  5736. if( sd == NULL )
  5737. return 0;
  5738. num=script_getnum(st,2);
  5739. for(i=0; i<MAX_INVENTORY; i++) {
  5740. if(sd->status.inventory[i].attribute){
  5741. brokencounter++;
  5742. if(num==brokencounter){
  5743. id=sd->status.inventory[i].nameid;
  5744. break;
  5745. }
  5746. }
  5747. }
  5748. script_pushint(st,id);
  5749. return 0;
  5750. }
  5751. /*==========================================
  5752. * repair [Valaris]
  5753. *------------------------------------------*/
  5754. BUILDIN_FUNC(repair)
  5755. {
  5756. int i,num;
  5757. int repaircounter=0;
  5758. TBL_PC *sd;
  5759. sd = script_rid2sd(st);
  5760. if( sd == NULL )
  5761. return 0;
  5762. num=script_getnum(st,2);
  5763. for(i=0; i<MAX_INVENTORY; i++) {
  5764. if(sd->status.inventory[i].attribute){
  5765. repaircounter++;
  5766. if(num==repaircounter){
  5767. sd->status.inventory[i].attribute=0;
  5768. clif_equiplist(sd);
  5769. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5770. clif_misceffect(&sd->bl, 3);
  5771. break;
  5772. }
  5773. }
  5774. }
  5775. return 0;
  5776. }
  5777. /*==========================================
  5778. * 装備チェック
  5779. *------------------------------------------*/
  5780. BUILDIN_FUNC(getequipisequiped)
  5781. {
  5782. int i=-1,num;
  5783. TBL_PC *sd;
  5784. num=script_getnum(st,2);
  5785. sd = script_rid2sd(st);
  5786. if( sd == NULL )
  5787. return 0;
  5788. if (num > 0 && num <= ARRAYLENGTH(equip))
  5789. i=pc_checkequip(sd,equip[num-1]);
  5790. if(i >= 0)
  5791. script_pushint(st,1);
  5792. else
  5793. script_pushint(st,0);
  5794. return 0;
  5795. }
  5796. /*==========================================
  5797. * 装備品精錬可能チェック
  5798. *------------------------------------------*/
  5799. BUILDIN_FUNC(getequipisenableref)
  5800. {
  5801. int i=-1,num;
  5802. TBL_PC *sd;
  5803. num=script_getnum(st,2);
  5804. sd = script_rid2sd(st);
  5805. if( sd == NULL )
  5806. return 0;
  5807. if( num > 0 && num <= ARRAYLENGTH(equip) )
  5808. i = pc_checkequip(sd,equip[num-1]);
  5809. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  5810. script_pushint(st,1);
  5811. else
  5812. script_pushint(st,0);
  5813. return 0;
  5814. }
  5815. /*==========================================
  5816. * 装備品鑑定チェック
  5817. *------------------------------------------*/
  5818. BUILDIN_FUNC(getequipisidentify)
  5819. {
  5820. int i=-1,num;
  5821. TBL_PC *sd;
  5822. num=script_getnum(st,2);
  5823. sd = script_rid2sd(st);
  5824. if( sd == NULL )
  5825. return 0;
  5826. if (num > 0 && num <= ARRAYLENGTH(equip))
  5827. i=pc_checkequip(sd,equip[num-1]);
  5828. if(i >= 0)
  5829. script_pushint(st,sd->status.inventory[i].identify);
  5830. else
  5831. script_pushint(st,0);
  5832. return 0;
  5833. }
  5834. /*==========================================
  5835. * 装備品精錬度
  5836. *------------------------------------------*/
  5837. BUILDIN_FUNC(getequiprefinerycnt)
  5838. {
  5839. int i=-1,num;
  5840. TBL_PC *sd;
  5841. num=script_getnum(st,2);
  5842. sd = script_rid2sd(st);
  5843. if( sd == NULL )
  5844. return 0;
  5845. if (num > 0 && num <= ARRAYLENGTH(equip))
  5846. i=pc_checkequip(sd,equip[num-1]);
  5847. if(i >= 0)
  5848. script_pushint(st,sd->status.inventory[i].refine);
  5849. else
  5850. script_pushint(st,0);
  5851. return 0;
  5852. }
  5853. /*==========================================
  5854. * 装備品武器LV
  5855. *------------------------------------------*/
  5856. BUILDIN_FUNC(getequipweaponlv)
  5857. {
  5858. int i=-1,num;
  5859. TBL_PC *sd;
  5860. num=script_getnum(st,2);
  5861. sd = script_rid2sd(st);
  5862. if( sd == NULL )
  5863. return 0;
  5864. if (num > 0 && num <= ARRAYLENGTH(equip))
  5865. i=pc_checkequip(sd,equip[num-1]);
  5866. if(i >= 0 && sd->inventory_data[i])
  5867. script_pushint(st,sd->inventory_data[i]->wlv);
  5868. else
  5869. script_pushint(st,0);
  5870. return 0;
  5871. }
  5872. /*==========================================
  5873. * 装備品精錬成功率
  5874. *------------------------------------------*/
  5875. BUILDIN_FUNC(getequippercentrefinery)
  5876. {
  5877. int i=-1,num;
  5878. TBL_PC *sd;
  5879. num=script_getnum(st,2);
  5880. sd = script_rid2sd(st);
  5881. if( sd == NULL )
  5882. return 0;
  5883. if (num > 0 && num <= ARRAYLENGTH(equip))
  5884. i=pc_checkequip(sd,equip[num-1]);
  5885. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5886. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5887. else
  5888. script_pushint(st,0);
  5889. return 0;
  5890. }
  5891. /*==========================================
  5892. * 精錬成功
  5893. *------------------------------------------*/
  5894. BUILDIN_FUNC(successrefitem)
  5895. {
  5896. int i=-1,num,ep;
  5897. TBL_PC *sd;
  5898. num=script_getnum(st,2);
  5899. sd = script_rid2sd(st);
  5900. if( sd == NULL )
  5901. return 0;
  5902. if (num > 0 && num <= ARRAYLENGTH(equip))
  5903. i=pc_checkequip(sd,equip[num-1]);
  5904. if(i >= 0) {
  5905. ep=sd->status.inventory[i].equip;
  5906. //Logs items, got from (N)PC scripts [Lupus]
  5907. if(log_config.enable_logs&0x40)
  5908. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5909. sd->status.inventory[i].refine++;
  5910. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  5911. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5912. clif_delitem(sd,i,1,3);
  5913. //Logs items, got from (N)PC scripts [Lupus]
  5914. if(log_config.enable_logs&0x40)
  5915. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5916. clif_additem(sd,i,1,0);
  5917. pc_equipitem(sd,i,ep);
  5918. clif_misceffect(&sd->bl,3);
  5919. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5920. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5921. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5922. ){ // Fame point system [DracoRPG]
  5923. switch (sd->inventory_data[i]->wlv){
  5924. case 1:
  5925. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5926. break;
  5927. case 2:
  5928. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5929. break;
  5930. case 3:
  5931. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5932. break;
  5933. }
  5934. }
  5935. }
  5936. return 0;
  5937. }
  5938. /*==========================================
  5939. * 精錬失敗
  5940. *------------------------------------------*/
  5941. BUILDIN_FUNC(failedrefitem)
  5942. {
  5943. int i=-1,num;
  5944. TBL_PC *sd;
  5945. num=script_getnum(st,2);
  5946. sd = script_rid2sd(st);
  5947. if( sd == NULL )
  5948. return 0;
  5949. if (num > 0 && num <= ARRAYLENGTH(equip))
  5950. i=pc_checkequip(sd,equip[num-1]);
  5951. if(i >= 0) {
  5952. //Logs items, got from (N)PC scripts [Lupus]
  5953. if(log_config.enable_logs&0x40)
  5954. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5955. sd->status.inventory[i].refine = 0;
  5956. pc_unequipitem(sd,i,3);
  5957. // 精錬失敗エフェクトのパケット
  5958. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5959. pc_delitem(sd,i,1,0,2);
  5960. // 他の人にも失敗を通知
  5961. clif_misceffect(&sd->bl,2);
  5962. }
  5963. return 0;
  5964. }
  5965. /*==========================================
  5966. *
  5967. *------------------------------------------*/
  5968. BUILDIN_FUNC(statusup)
  5969. {
  5970. int type;
  5971. TBL_PC *sd;
  5972. type=script_getnum(st,2);
  5973. sd = script_rid2sd(st);
  5974. if( sd == NULL )
  5975. return 0;
  5976. pc_statusup(sd,type);
  5977. return 0;
  5978. }
  5979. /*==========================================
  5980. *
  5981. *------------------------------------------*/
  5982. BUILDIN_FUNC(statusup2)
  5983. {
  5984. int type,val;
  5985. TBL_PC *sd;
  5986. type=script_getnum(st,2);
  5987. val=script_getnum(st,3);
  5988. sd = script_rid2sd(st);
  5989. if( sd == NULL )
  5990. return 0;
  5991. pc_statusup2(sd,type,val);
  5992. return 0;
  5993. }
  5994. /// See 'doc/item_bonus.txt'
  5995. ///
  5996. /// bonus <bonus type>,<val1>;
  5997. /// bonus2 <bonus type>,<val1>,<val2>;
  5998. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5999. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6000. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6001. BUILDIN_FUNC(bonus)
  6002. {
  6003. int type;
  6004. int val1;
  6005. int val2 = 0;
  6006. int val3 = 0;
  6007. int val4 = 0;
  6008. int val5 = 0;
  6009. TBL_PC* sd;
  6010. sd = script_rid2sd(st);
  6011. if( sd == NULL )
  6012. return 0; // no player attached
  6013. type = script_getnum(st,2);
  6014. switch( type )
  6015. {
  6016. case SP_AUTOSPELL:
  6017. case SP_AUTOSPELL_WHENHIT:
  6018. case SP_AUTOSPELL_ONSKILL:
  6019. case SP_SKILL_ATK:
  6020. case SP_SKILL_HEAL:
  6021. case SP_SKILL_HEAL2:
  6022. case SP_ADD_SKILL_BLOW:
  6023. case SP_CASTRATE:
  6024. case SP_ADDEFF_ONSKILL:
  6025. // these bonuses support skill names
  6026. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6027. break;
  6028. default:
  6029. val1 = script_getnum(st,3);
  6030. break;
  6031. }
  6032. switch( script_lastdata(st)-2 )
  6033. {
  6034. case 1:
  6035. pc_bonus(sd, type, val1);
  6036. break;
  6037. case 2:
  6038. val2 = script_getnum(st,4);
  6039. pc_bonus2(sd, type, val1, val2);
  6040. break;
  6041. case 3:
  6042. val2 = script_getnum(st,4);
  6043. val3 = script_getnum(st,5);
  6044. pc_bonus3(sd, type, val1, val2, val3);
  6045. break;
  6046. case 4:
  6047. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6048. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6049. else
  6050. val2 = script_getnum(st,4);
  6051. val3 = script_getnum(st,5);
  6052. val4 = script_getnum(st,6);
  6053. pc_bonus4(sd, type, val1, val2, val3, val4);
  6054. break;
  6055. case 5:
  6056. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6057. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6058. else
  6059. val2 = script_getnum(st,4);
  6060. val3 = script_getnum(st,5);
  6061. val4 = script_getnum(st,6);
  6062. val5 = script_getnum(st,7);
  6063. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6064. break;
  6065. default:
  6066. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6067. break;
  6068. }
  6069. return 0;
  6070. }
  6071. BUILDIN_FUNC(autobonus)
  6072. {
  6073. unsigned int dur;
  6074. short rate;
  6075. short atk_type = 0;
  6076. TBL_PC* sd;
  6077. const char *bonus_script, *other_script = NULL;
  6078. sd = script_rid2sd(st);
  6079. if( sd == NULL )
  6080. return 0; // no player attached
  6081. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6082. return 0;
  6083. rate = script_getnum(st,3);
  6084. dur = script_getnum(st,4);
  6085. bonus_script = script_getstr(st,2);
  6086. if( !rate || !dur || !bonus_script )
  6087. return 0;
  6088. if( script_hasdata(st,5) )
  6089. atk_type = script_getnum(st,5);
  6090. if( script_hasdata(st,6) )
  6091. other_script = script_getstr(st,6);
  6092. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6093. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6094. {
  6095. script_add_autobonus(bonus_script);
  6096. if( other_script )
  6097. script_add_autobonus(other_script);
  6098. }
  6099. return 0;
  6100. }
  6101. BUILDIN_FUNC(autobonus2)
  6102. {
  6103. unsigned int dur;
  6104. short rate;
  6105. short atk_type = 0;
  6106. TBL_PC* sd;
  6107. const char *bonus_script, *other_script = NULL;
  6108. sd = script_rid2sd(st);
  6109. if( sd == NULL )
  6110. return 0; // no player attached
  6111. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6112. return 0;
  6113. rate = script_getnum(st,3);
  6114. dur = script_getnum(st,4);
  6115. bonus_script = script_getstr(st,2);
  6116. if( !rate || !dur || !bonus_script )
  6117. return 0;
  6118. if( script_hasdata(st,5) )
  6119. atk_type = script_getnum(st,5);
  6120. if( script_hasdata(st,6) )
  6121. other_script = script_getstr(st,6);
  6122. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6123. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6124. {
  6125. script_add_autobonus(bonus_script);
  6126. if( other_script )
  6127. script_add_autobonus(other_script);
  6128. }
  6129. return 0;
  6130. }
  6131. BUILDIN_FUNC(autobonus3)
  6132. {
  6133. unsigned int dur;
  6134. short rate,atk_type;
  6135. TBL_PC* sd;
  6136. const char *bonus_script, *other_script = NULL;
  6137. sd = script_rid2sd(st);
  6138. if( sd == NULL )
  6139. return 0; // no player attached
  6140. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6141. return 0;
  6142. rate = script_getnum(st,3);
  6143. dur = script_getnum(st,4);
  6144. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6145. bonus_script = script_getstr(st,2);
  6146. if( !rate || !dur || !atk_type || !bonus_script )
  6147. return 0;
  6148. if( script_hasdata(st,6) )
  6149. other_script = script_getstr(st,6);
  6150. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6151. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6152. {
  6153. script_add_autobonus(bonus_script);
  6154. if( other_script )
  6155. script_add_autobonus(other_script);
  6156. }
  6157. return 0;
  6158. }
  6159. /// Changes the level of a player skill.
  6160. /// <flag> defaults to 1
  6161. /// <flag>=0 : set the level of the skill
  6162. /// <flag>=1 : set the temporary level of the skill
  6163. /// <flag>=2 : add to the level of the skill
  6164. ///
  6165. /// skill <skill id>,<level>,<flag>
  6166. /// skill <skill id>,<level>
  6167. /// skill "<skill name>",<level>,<flag>
  6168. /// skill "<skill name>",<level>
  6169. BUILDIN_FUNC(skill)
  6170. {
  6171. int id;
  6172. int level;
  6173. int flag = 1;
  6174. TBL_PC* sd;
  6175. sd = script_rid2sd(st);
  6176. if( sd == NULL )
  6177. return 0;// no player attached, report source
  6178. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6179. level = script_getnum(st,3);
  6180. if( script_hasdata(st,4) )
  6181. flag = script_getnum(st,4);
  6182. pc_skill(sd, id, level, flag);
  6183. return 0;
  6184. }
  6185. /// Changes the level of a player skill.
  6186. /// like skill, but <flag> defaults to 2
  6187. ///
  6188. /// addtoskill <skill id>,<amount>,<flag>
  6189. /// addtoskill <skill id>,<amount>
  6190. /// addtoskill "<skill name>",<amount>,<flag>
  6191. /// addtoskill "<skill name>",<amount>
  6192. ///
  6193. /// @see skill
  6194. BUILDIN_FUNC(addtoskill)
  6195. {
  6196. int id;
  6197. int level;
  6198. int flag = 2;
  6199. TBL_PC* sd;
  6200. sd = script_rid2sd(st);
  6201. if( sd == NULL )
  6202. return 0;// no player attached, report source
  6203. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6204. level = script_getnum(st,3);
  6205. if( script_hasdata(st,4) )
  6206. flag = script_getnum(st,4);
  6207. pc_skill(sd, id, level, flag);
  6208. return 0;
  6209. }
  6210. /// Increases the level of a guild skill.
  6211. ///
  6212. /// guildskill <skill id>,<amount>;
  6213. /// guildskill "<skill name>",<amount>;
  6214. BUILDIN_FUNC(guildskill)
  6215. {
  6216. int id;
  6217. int level;
  6218. TBL_PC* sd;
  6219. int i;
  6220. sd = script_rid2sd(st);
  6221. if( sd == NULL )
  6222. return 0;// no player attached, report source
  6223. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6224. level = script_getnum(st,3);
  6225. for( i=0; i < level; i++ )
  6226. guild_skillup(sd, id);
  6227. return 0;
  6228. }
  6229. /// Returns the level of the player skill.
  6230. ///
  6231. /// getskilllv(<skill id>) -> <level>
  6232. /// getskilllv("<skill name>") -> <level>
  6233. BUILDIN_FUNC(getskilllv)
  6234. {
  6235. int id;
  6236. TBL_PC* sd;
  6237. sd = script_rid2sd(st);
  6238. if( sd == NULL )
  6239. return 0;// no player attached, report source
  6240. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6241. script_pushint(st, pc_checkskill(sd,id));
  6242. return 0;
  6243. }
  6244. /// Returns the level of the guild skill.
  6245. ///
  6246. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6247. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6248. BUILDIN_FUNC(getgdskilllv)
  6249. {
  6250. int guild_id;
  6251. int skill_id;
  6252. struct guild* g;
  6253. guild_id = script_getnum(st,2);
  6254. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6255. g = guild_search(guild_id);
  6256. if( g == NULL )
  6257. script_pushint(st, -1);
  6258. else
  6259. script_pushint(st, guild_checkskill(g,skill_id));
  6260. return 0;
  6261. }
  6262. /// Returns the 'basic_skill_check' setting.
  6263. /// This config determines if the server checks the skill level of NV_BASIC
  6264. /// before allowing the basic actions.
  6265. ///
  6266. /// basicskillcheck() -> <bool>
  6267. BUILDIN_FUNC(basicskillcheck)
  6268. {
  6269. script_pushint(st, battle_config.basic_skill_check);
  6270. return 0;
  6271. }
  6272. /// Returns the GM level of the player.
  6273. ///
  6274. /// getgmlevel() -> <level>
  6275. BUILDIN_FUNC(getgmlevel)
  6276. {
  6277. TBL_PC* sd;
  6278. sd = script_rid2sd(st);
  6279. if( sd == NULL )
  6280. return 0;// no player attached, report source
  6281. script_pushint(st, pc_isGM(sd));
  6282. return 0;
  6283. }
  6284. /// Terminates the execution of this script instance.
  6285. ///
  6286. /// end
  6287. BUILDIN_FUNC(end)
  6288. {
  6289. st->state = END;
  6290. return 0;
  6291. }
  6292. /// Checks if the player has that effect state (option).
  6293. ///
  6294. /// checkoption(<option>) -> <bool>
  6295. BUILDIN_FUNC(checkoption)
  6296. {
  6297. int option;
  6298. TBL_PC* sd;
  6299. sd = script_rid2sd(st);
  6300. if( sd == NULL )
  6301. return 0;// no player attached, report source
  6302. option = script_getnum(st,2);
  6303. if( sd->sc.option&option )
  6304. script_pushint(st, 1);
  6305. else
  6306. script_pushint(st, 0);
  6307. return 0;
  6308. }
  6309. /// Checks if the player is in that body state (opt1).
  6310. ///
  6311. /// checkoption1(<opt1>) -> <bool>
  6312. BUILDIN_FUNC(checkoption1)
  6313. {
  6314. int opt1;
  6315. TBL_PC* sd;
  6316. sd = script_rid2sd(st);
  6317. if( sd == NULL )
  6318. return 0;// no player attached, report source
  6319. opt1 = script_getnum(st,2);
  6320. if( sd->sc.opt1 == opt1 )
  6321. script_pushint(st, 1);
  6322. else
  6323. script_pushint(st, 0);
  6324. return 0;
  6325. }
  6326. /// Checks if the player has that health state (opt2).
  6327. ///
  6328. /// checkoption2(<opt2>) -> <bool>
  6329. BUILDIN_FUNC(checkoption2)
  6330. {
  6331. int opt2;
  6332. TBL_PC* sd;
  6333. sd = script_rid2sd(st);
  6334. if( sd == NULL )
  6335. return 0;// no player attached, report source
  6336. opt2 = script_getnum(st,2);
  6337. if( sd->sc.opt2&opt2 )
  6338. script_pushint(st, 1);
  6339. else
  6340. script_pushint(st, 0);
  6341. return 0;
  6342. }
  6343. /// Changes the effect state (option) of the player.
  6344. /// <flag> defaults to 1
  6345. /// <flag>=0 : removes the option
  6346. /// <flag>=other : adds the option
  6347. ///
  6348. /// setoption <option>,<flag>;
  6349. /// setoption <option>;
  6350. BUILDIN_FUNC(setoption)
  6351. {
  6352. int option;
  6353. int flag = 1;
  6354. TBL_PC* sd;
  6355. sd = script_rid2sd(st);
  6356. if( sd == NULL )
  6357. return 0;// no player attached, report source
  6358. option = script_getnum(st,2);
  6359. if( script_hasdata(st,3) )
  6360. flag = script_getnum(st,3);
  6361. else if( !option ){// Request to remove everything.
  6362. flag = 0;
  6363. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6364. }
  6365. if( flag ){// Add option
  6366. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6367. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6368. pc_setoption(sd, sd->sc.option|option);
  6369. } else// Remove option
  6370. pc_setoption(sd, sd->sc.option&~option);
  6371. return 0;
  6372. }
  6373. /// Returns if the player has a cart.
  6374. ///
  6375. /// checkcart() -> <bool>
  6376. ///
  6377. /// @author Valaris
  6378. BUILDIN_FUNC(checkcart)
  6379. {
  6380. TBL_PC* sd;
  6381. sd = script_rid2sd(st);
  6382. if( sd == NULL )
  6383. return 0;// no player attached, report source
  6384. if( pc_iscarton(sd) )
  6385. script_pushint(st, 1);
  6386. else
  6387. script_pushint(st, 0);
  6388. return 0;
  6389. }
  6390. /// Sets the cart of the player.
  6391. /// <type> defaults to 1
  6392. /// <type>=0 : removes the cart
  6393. /// <type>=1 : Normal cart
  6394. /// <type>=2 : Wooden cart
  6395. /// <type>=3 : Covered cart with flowers and ferns
  6396. /// <type>=4 : Wooden cart with a Panda doll on the back
  6397. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6398. ///
  6399. /// setcart <type>;
  6400. /// setcart;
  6401. BUILDIN_FUNC(setcart)
  6402. {
  6403. int type = 1;
  6404. TBL_PC* sd;
  6405. sd = script_rid2sd(st);
  6406. if( sd == NULL )
  6407. return 0;// no player attached, report source
  6408. if( script_hasdata(st,2) )
  6409. type = script_getnum(st,2);
  6410. pc_setcart(sd, type);
  6411. return 0;
  6412. }
  6413. /// Returns if the player has a falcon.
  6414. ///
  6415. /// checkfalcon() -> <bool>
  6416. ///
  6417. /// @author Valaris
  6418. BUILDIN_FUNC(checkfalcon)
  6419. {
  6420. TBL_PC* sd;
  6421. sd = script_rid2sd(st);
  6422. if( sd == NULL )
  6423. return 0;// no player attached, report source
  6424. if( pc_isfalcon(sd) )
  6425. script_pushint(st, 1);
  6426. else
  6427. script_pushint(st, 0);
  6428. return 0;
  6429. }
  6430. /// Sets if the player has a falcon or not.
  6431. /// <flag> defaults to 1
  6432. ///
  6433. /// setfalcon <flag>;
  6434. /// setfalcon;
  6435. BUILDIN_FUNC(setfalcon)
  6436. {
  6437. int flag = 1;
  6438. TBL_PC* sd;
  6439. sd = script_rid2sd(st);
  6440. if( sd == NULL )
  6441. return 0;// no player attached, report source
  6442. if( script_hasdata(st,2) )
  6443. flag = script_getnum(st,2);
  6444. pc_setfalcon(sd, flag);
  6445. return 0;
  6446. }
  6447. /// Returns if the player is riding.
  6448. ///
  6449. /// checkriding() -> <bool>
  6450. ///
  6451. /// @author Valaris
  6452. BUILDIN_FUNC(checkriding)
  6453. {
  6454. TBL_PC* sd;
  6455. sd = script_rid2sd(st);
  6456. if( sd == NULL )
  6457. return 0;// no player attached, report source
  6458. if( pc_isriding(sd) )
  6459. script_pushint(st, 1);
  6460. else
  6461. script_pushint(st, 0);
  6462. return 0;
  6463. }
  6464. /// Sets if the player is riding.
  6465. /// <flag> defaults to 1
  6466. ///
  6467. /// setriding <flag>;
  6468. /// setriding;
  6469. BUILDIN_FUNC(setriding)
  6470. {
  6471. int flag = 1;
  6472. TBL_PC* sd;
  6473. sd = script_rid2sd(st);
  6474. if( sd == NULL )
  6475. return 0;// no player attached, report source
  6476. if( script_hasdata(st,2) )
  6477. flag = script_getnum(st,2);
  6478. pc_setriding(sd, flag);
  6479. return 0;
  6480. }
  6481. /// Sets the save point of the player.
  6482. ///
  6483. /// save "<map name>",<x>,<y>
  6484. /// savepoint "<map name>",<x>,<y>
  6485. BUILDIN_FUNC(savepoint)
  6486. {
  6487. int x;
  6488. int y;
  6489. short map;
  6490. const char* str;
  6491. TBL_PC* sd;
  6492. sd = script_rid2sd(st);
  6493. if( sd == NULL )
  6494. return 0;// no player attached, report source
  6495. str = script_getstr(st, 2);
  6496. x = script_getnum(st,3);
  6497. y = script_getnum(st,4);
  6498. map = mapindex_name2id(str);
  6499. if( map )
  6500. pc_setsavepoint(sd, map, x, y);
  6501. return 0;
  6502. }
  6503. /*==========================================
  6504. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6505. *------------------------------------------*/
  6506. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6507. {
  6508. int type;
  6509. time_t timer;
  6510. struct tm *t;
  6511. type=script_getnum(st,2);
  6512. switch(type){
  6513. case 2:
  6514. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6515. // from the system clock.)
  6516. script_pushint(st,(int)time(NULL));
  6517. break;
  6518. case 1:
  6519. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6520. time(&timer);
  6521. t=localtime(&timer);
  6522. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6523. break;
  6524. case 0:
  6525. default:
  6526. //type 0:(System Ticks)
  6527. script_pushint(st,gettick());
  6528. break;
  6529. }
  6530. return 0;
  6531. }
  6532. /*==========================================
  6533. * GetTime(Type);
  6534. * 1: Sec 2: Min 3: Hour
  6535. * 4: WeekDay 5: MonthDay 6: Month
  6536. * 7: Year
  6537. *------------------------------------------*/
  6538. BUILDIN_FUNC(gettime) /* Asgard Version */
  6539. {
  6540. int type;
  6541. time_t timer;
  6542. struct tm *t;
  6543. type=script_getnum(st,2);
  6544. time(&timer);
  6545. t=localtime(&timer);
  6546. switch(type){
  6547. case 1://Sec(0~59)
  6548. script_pushint(st,t->tm_sec);
  6549. break;
  6550. case 2://Min(0~59)
  6551. script_pushint(st,t->tm_min);
  6552. break;
  6553. case 3://Hour(0~23)
  6554. script_pushint(st,t->tm_hour);
  6555. break;
  6556. case 4://WeekDay(0~6)
  6557. script_pushint(st,t->tm_wday);
  6558. break;
  6559. case 5://MonthDay(01~31)
  6560. script_pushint(st,t->tm_mday);
  6561. break;
  6562. case 6://Month(01~12)
  6563. script_pushint(st,t->tm_mon+1);
  6564. break;
  6565. case 7://Year(20xx)
  6566. script_pushint(st,t->tm_year+1900);
  6567. break;
  6568. case 8://Year Day(01~366)
  6569. script_pushint(st,t->tm_yday+1);
  6570. break;
  6571. default://(format error)
  6572. script_pushint(st,-1);
  6573. break;
  6574. }
  6575. return 0;
  6576. }
  6577. /*==========================================
  6578. * GetTimeStr("TimeFMT", Length);
  6579. *------------------------------------------*/
  6580. BUILDIN_FUNC(gettimestr)
  6581. {
  6582. char *tmpstr;
  6583. const char *fmtstr;
  6584. int maxlen;
  6585. time_t now = time(NULL);
  6586. fmtstr=script_getstr(st,2);
  6587. maxlen=script_getnum(st,3);
  6588. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6589. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6590. tmpstr[maxlen]='\0';
  6591. script_pushstr(st,tmpstr);
  6592. return 0;
  6593. }
  6594. /*==========================================
  6595. * カプラ倉庫を開く
  6596. *------------------------------------------*/
  6597. BUILDIN_FUNC(openstorage)
  6598. {
  6599. TBL_PC* sd;
  6600. sd = script_rid2sd(st);
  6601. if( sd == NULL )
  6602. return 0;
  6603. storage_storageopen(sd);
  6604. return 0;
  6605. }
  6606. BUILDIN_FUNC(guildopenstorage)
  6607. {
  6608. TBL_PC* sd;
  6609. int ret;
  6610. sd = script_rid2sd(st);
  6611. if( sd == NULL )
  6612. return 0;
  6613. ret = storage_guild_storageopen(sd);
  6614. script_pushint(st,ret);
  6615. return 0;
  6616. }
  6617. /*==========================================
  6618. * アイテムによるスキル発動
  6619. *------------------------------------------*/
  6620. /// itemskill <skill id>,<level>
  6621. /// itemskill "<skill name>",<level>
  6622. BUILDIN_FUNC(itemskill)
  6623. {
  6624. int id;
  6625. int lv;
  6626. TBL_PC* sd;
  6627. sd = script_rid2sd(st);
  6628. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6629. return 0;
  6630. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6631. lv = script_getnum(st,3);
  6632. sd->skillitem=id;
  6633. sd->skillitemlv=lv;
  6634. clif_item_skill(sd,id,lv);
  6635. return 0;
  6636. }
  6637. /*==========================================
  6638. * アイテム作成
  6639. *------------------------------------------*/
  6640. BUILDIN_FUNC(produce)
  6641. {
  6642. int trigger;
  6643. TBL_PC* sd;
  6644. sd = script_rid2sd(st);
  6645. if( sd == NULL )
  6646. return 0;
  6647. trigger=script_getnum(st,2);
  6648. clif_skill_produce_mix_list(sd, trigger);
  6649. return 0;
  6650. }
  6651. /*==========================================
  6652. *
  6653. *------------------------------------------*/
  6654. BUILDIN_FUNC(cooking)
  6655. {
  6656. int trigger;
  6657. TBL_PC* sd;
  6658. sd = script_rid2sd(st);
  6659. if( sd == NULL )
  6660. return 0;
  6661. trigger=script_getnum(st,2);
  6662. clif_cooking_list(sd, trigger);
  6663. return 0;
  6664. }
  6665. /*==========================================
  6666. * NPCでペット作る
  6667. *------------------------------------------*/
  6668. BUILDIN_FUNC(makepet)
  6669. {
  6670. TBL_PC* sd;
  6671. int id,pet_id;
  6672. id=script_getnum(st,2);
  6673. sd = script_rid2sd(st);
  6674. if( sd == NULL )
  6675. return 0;
  6676. pet_id = search_petDB_index(id, PET_CLASS);
  6677. if (pet_id < 0)
  6678. pet_id = search_petDB_index(id, PET_EGG);
  6679. if (pet_id >= 0 && sd) {
  6680. sd->catch_target_class = pet_db[pet_id].class_;
  6681. intif_create_pet(
  6682. sd->status.account_id, sd->status.char_id,
  6683. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6684. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6685. 100, 0, 1, pet_db[pet_id].jname);
  6686. }
  6687. return 0;
  6688. }
  6689. /*==========================================
  6690. * NPCで経験値上げる
  6691. *------------------------------------------*/
  6692. BUILDIN_FUNC(getexp)
  6693. {
  6694. TBL_PC* sd;
  6695. int base=0,job=0;
  6696. double bonus;
  6697. sd = script_rid2sd(st);
  6698. if( sd == NULL )
  6699. return 0;
  6700. base=script_getnum(st,2);
  6701. job =script_getnum(st,3);
  6702. if(base<0 || job<0)
  6703. return 0;
  6704. // bonus for npc-given exp
  6705. bonus = battle_config.quest_exp_rate / 100.;
  6706. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6707. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6708. pc_gainexp(sd, NULL, base, job, true);
  6709. return 0;
  6710. }
  6711. /*==========================================
  6712. * Gain guild exp [Celest]
  6713. *------------------------------------------*/
  6714. BUILDIN_FUNC(guildgetexp)
  6715. {
  6716. TBL_PC* sd;
  6717. int exp;
  6718. sd = script_rid2sd(st);
  6719. if( sd == NULL )
  6720. return 0;
  6721. exp = script_getnum(st,2);
  6722. if(exp < 0)
  6723. return 0;
  6724. if(sd && sd->status.guild_id > 0)
  6725. guild_getexp (sd, exp);
  6726. return 0;
  6727. }
  6728. /*==========================================
  6729. * Changes the guild master of a guild [Skotlex]
  6730. *------------------------------------------*/
  6731. BUILDIN_FUNC(guildchangegm)
  6732. {
  6733. TBL_PC *sd;
  6734. int guild_id;
  6735. const char *name;
  6736. guild_id = script_getnum(st,2);
  6737. name = script_getstr(st,3);
  6738. sd=map_nick2sd(name);
  6739. if (!sd)
  6740. script_pushint(st,0);
  6741. else
  6742. script_pushint(st,guild_gm_change(guild_id, sd));
  6743. return 0;
  6744. }
  6745. /*==========================================
  6746. * モンスター発生
  6747. *------------------------------------------*/
  6748. BUILDIN_FUNC(monster)
  6749. {
  6750. const char* mapn = script_getstr(st,2);
  6751. int x = script_getnum(st,3);
  6752. int y = script_getnum(st,4);
  6753. const char* str = script_getstr(st,5);
  6754. int class_ = script_getnum(st,6);
  6755. int amount = script_getnum(st,7);
  6756. const char* event = "";
  6757. struct map_session_data* sd;
  6758. int m;
  6759. if( script_hasdata(st,8) )
  6760. {
  6761. event = script_getstr(st,8);
  6762. check_event(st, event);
  6763. }
  6764. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6765. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6766. return 1;
  6767. }
  6768. sd = map_id2sd(st->rid);
  6769. if( sd && strcmp(mapn,"this") == 0 )
  6770. m = sd->bl.m;
  6771. else
  6772. {
  6773. m = map_mapname2mapid(mapn);
  6774. if( map[m].flag.src4instance && st->instance_id )
  6775. { // Try to redirect to the instance map, not the src map
  6776. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6777. {
  6778. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6779. return 1;
  6780. }
  6781. }
  6782. }
  6783. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6784. return 0;
  6785. }
  6786. /*==========================================
  6787. * Request List of Monster Drops
  6788. *------------------------------------------*/
  6789. BUILDIN_FUNC(getmobdrops)
  6790. {
  6791. int class_ = script_getnum(st,2);
  6792. int i, j = 0;
  6793. struct mob_db *mob;
  6794. if( !mobdb_checkid(class_) )
  6795. {
  6796. script_pushint(st, 0);
  6797. return 0;
  6798. }
  6799. mob = mob_db(class_);
  6800. for( i = 0; i < MAX_MOB_DROP; i++ )
  6801. {
  6802. if( mob->dropitem[i].nameid < 1 )
  6803. continue;
  6804. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  6805. continue;
  6806. mapreg_setreg(add_str("$@MobDrop_item") + (j<<24), mob->dropitem[i].nameid);
  6807. mapreg_setreg(add_str("$@MobDrop_rate") + (j<<24), mob->dropitem[i].p);
  6808. j++;
  6809. }
  6810. mapreg_setreg(add_str("$@MobDrop_count"), j);
  6811. script_pushint(st, 1);
  6812. return 0;
  6813. }
  6814. /*==========================================
  6815. * モンスター発生
  6816. *------------------------------------------*/
  6817. BUILDIN_FUNC(areamonster)
  6818. {
  6819. const char* mapn = script_getstr(st,2);
  6820. int x0 = script_getnum(st,3);
  6821. int y0 = script_getnum(st,4);
  6822. int x1 = script_getnum(st,5);
  6823. int y1 = script_getnum(st,6);
  6824. const char* str = script_getstr(st,7);
  6825. int class_ = script_getnum(st,8);
  6826. int amount = script_getnum(st,9);
  6827. const char* event = "";
  6828. struct map_session_data* sd;
  6829. int m;
  6830. if( script_hasdata(st,10) )
  6831. {
  6832. event = script_getstr(st,10);
  6833. check_event(st, event);
  6834. }
  6835. sd = map_id2sd(st->rid);
  6836. if( sd && strcmp(mapn,"this") == 0 )
  6837. m = sd->bl.m;
  6838. else
  6839. {
  6840. m = map_mapname2mapid(mapn);
  6841. if( map[m].flag.src4instance && st->instance_id )
  6842. { // Try to redirect to the instance map, not the src map
  6843. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6844. {
  6845. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6846. return 1;
  6847. }
  6848. }
  6849. }
  6850. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6851. return 0;
  6852. }
  6853. /*==========================================
  6854. * モンスター削除
  6855. *------------------------------------------*/
  6856. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  6857. { //same fix but with killmonster instead - stripping events from mobs.
  6858. TBL_MOB* md = (TBL_MOB*)bl;
  6859. char *event=va_arg(ap,char *);
  6860. int allflag=va_arg(ap,int);
  6861. md->state.npc_killmonster = 1;
  6862. if(!allflag){
  6863. if(strcmp(event,md->npc_event)==0)
  6864. status_kill(bl);
  6865. }else{
  6866. if(!md->spawn)
  6867. status_kill(bl);
  6868. }
  6869. md->state.npc_killmonster = 0;
  6870. return 0;
  6871. }
  6872. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6873. {
  6874. TBL_MOB* md = (TBL_MOB*)bl;
  6875. char *event=va_arg(ap,char *);
  6876. int allflag=va_arg(ap,int);
  6877. if(!allflag){
  6878. if(strcmp(event,md->npc_event)==0)
  6879. status_kill(bl);
  6880. }else{
  6881. if(!md->spawn)
  6882. status_kill(bl);
  6883. }
  6884. return 0;
  6885. }
  6886. BUILDIN_FUNC(killmonster)
  6887. {
  6888. const char *mapname,*event;
  6889. int m,allflag=0;
  6890. mapname=script_getstr(st,2);
  6891. event=script_getstr(st,3);
  6892. if(strcmp(event,"All")==0)
  6893. allflag = 1;
  6894. else
  6895. check_event(st, event);
  6896. if( (m=map_mapname2mapid(mapname))<0 )
  6897. return 0;
  6898. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6899. return 0;
  6900. if( script_hasdata(st,4) ) {
  6901. if ( script_getnum(st,4) == 1 ) {
  6902. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6903. return 0;
  6904. }
  6905. }
  6906. map_freeblock_lock();
  6907. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  6908. map_freeblock_unlock();
  6909. return 0;
  6910. }
  6911. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  6912. { //Strips the event from the mob if it's killed the old method.
  6913. struct mob_data *md;
  6914. md = BL_CAST(BL_MOB, bl);
  6915. if (md->npc_event[0])
  6916. md->npc_event[0] = 0;
  6917. status_kill(bl);
  6918. return 0;
  6919. }
  6920. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6921. {
  6922. status_kill(bl);
  6923. return 0;
  6924. }
  6925. BUILDIN_FUNC(killmonsterall)
  6926. {
  6927. const char *mapname;
  6928. int m;
  6929. mapname=script_getstr(st,2);
  6930. if( (m = map_mapname2mapid(mapname))<0 )
  6931. return 0;
  6932. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6933. return 0;
  6934. if( script_hasdata(st,3) ) {
  6935. if ( script_getnum(st,3) == 1 ) {
  6936. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  6937. return 0;
  6938. }
  6939. }
  6940. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  6941. return 0;
  6942. }
  6943. /*==========================================
  6944. * Creates a clone of a player.
  6945. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6946. *------------------------------------------*/
  6947. BUILDIN_FUNC(clone)
  6948. {
  6949. TBL_PC *sd, *msd=NULL;
  6950. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6951. unsigned int duration = 0;
  6952. const char *map,*event="";
  6953. map=script_getstr(st,2);
  6954. x=script_getnum(st,3);
  6955. y=script_getnum(st,4);
  6956. event=script_getstr(st,5);
  6957. char_id=script_getnum(st,6);
  6958. if( script_hasdata(st,7) )
  6959. master_id=script_getnum(st,7);
  6960. if( script_hasdata(st,8) )
  6961. mode=script_getnum(st,8);
  6962. if( script_hasdata(st,9) )
  6963. flag=script_getnum(st,9);
  6964. if( script_hasdata(st,10) )
  6965. duration=script_getnum(st,10);
  6966. check_event(st, event);
  6967. m = map_mapname2mapid(map);
  6968. if (m < 0) return 0;
  6969. sd = map_charid2sd(char_id);
  6970. if (master_id) {
  6971. msd = map_charid2sd(master_id);
  6972. if (msd)
  6973. master_id = msd->bl.id;
  6974. else
  6975. master_id = 0;
  6976. }
  6977. if (sd) //Return ID of newly crafted clone.
  6978. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6979. else //Failed to create clone.
  6980. script_pushint(st,0);
  6981. return 0;
  6982. }
  6983. /*==========================================
  6984. * イベント実行
  6985. *------------------------------------------*/
  6986. BUILDIN_FUNC(doevent)
  6987. {
  6988. const char* event = script_getstr(st,2);
  6989. struct map_session_data* sd;
  6990. if( ( sd = script_rid2sd(st) ) == NULL )
  6991. {
  6992. return 0;
  6993. }
  6994. check_event(st, event);
  6995. npc_event(sd, event, 0);
  6996. return 0;
  6997. }
  6998. /*==========================================
  6999. * NPC主体イベント実行
  7000. *------------------------------------------*/
  7001. BUILDIN_FUNC(donpcevent)
  7002. {
  7003. const char* event = script_getstr(st,2);
  7004. check_event(st, event);
  7005. npc_event_do(event);
  7006. return 0;
  7007. }
  7008. /// for Aegis compatibility
  7009. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7010. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7011. {
  7012. const char* npc = script_getstr(st,2);
  7013. const char* command = script_getstr(st,3);
  7014. char event[51];
  7015. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  7016. check_event(st, event);
  7017. npc_event_do(event);
  7018. return 0;
  7019. }
  7020. /*==========================================
  7021. * イベントタイマー追加
  7022. *------------------------------------------*/
  7023. BUILDIN_FUNC(addtimer)
  7024. {
  7025. int tick = script_getnum(st,2);
  7026. const char* event = script_getstr(st, 3);
  7027. TBL_PC* sd;
  7028. check_event(st, event);
  7029. sd = script_rid2sd(st);
  7030. if( sd == NULL )
  7031. return 0;
  7032. pc_addeventtimer(sd,tick,event);
  7033. return 0;
  7034. }
  7035. /*==========================================
  7036. * イベントタイマー削除
  7037. *------------------------------------------*/
  7038. BUILDIN_FUNC(deltimer)
  7039. {
  7040. const char *event;
  7041. TBL_PC* sd;
  7042. event=script_getstr(st, 2);
  7043. sd = script_rid2sd(st);
  7044. if( sd == NULL )
  7045. return 0;
  7046. check_event(st, event);
  7047. pc_deleventtimer(sd,event);
  7048. return 0;
  7049. }
  7050. /*==========================================
  7051. * イベントタイマーのカウント値追加
  7052. *------------------------------------------*/
  7053. BUILDIN_FUNC(addtimercount)
  7054. {
  7055. const char *event;
  7056. int tick;
  7057. TBL_PC* sd;
  7058. event=script_getstr(st, 2);
  7059. tick=script_getnum(st,3);
  7060. sd = script_rid2sd(st);
  7061. if( sd == NULL )
  7062. return 0;
  7063. check_event(st, event);
  7064. pc_addeventtimercount(sd,event,tick);
  7065. return 0;
  7066. }
  7067. /*==========================================
  7068. * NPCタイマー初期化
  7069. *------------------------------------------*/
  7070. BUILDIN_FUNC(initnpctimer)
  7071. {
  7072. struct npc_data *nd;
  7073. int flag = 0;
  7074. if( script_hasdata(st,3) )
  7075. { //Two arguments: NPC name and attach flag.
  7076. nd = npc_name2id(script_getstr(st, 2));
  7077. flag = script_getnum(st,3);
  7078. }
  7079. else if( script_hasdata(st,2) )
  7080. { //Check if argument is numeric (flag) or string (npc name)
  7081. struct script_data *data;
  7082. data = script_getdata(st,2);
  7083. get_val(st,data);
  7084. if( data_isstring(data) ) //NPC name
  7085. nd = npc_name2id(conv_str(st, data));
  7086. else if( data_isint(data) ) //Flag
  7087. {
  7088. nd = (struct npc_data *)map_id2bl(st->oid);
  7089. flag = conv_num(st,data);
  7090. }
  7091. else
  7092. {
  7093. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7094. return 1;
  7095. }
  7096. }
  7097. else
  7098. nd = (struct npc_data *)map_id2bl(st->oid);
  7099. if( !nd )
  7100. return 0;
  7101. if( flag ) //Attach
  7102. {
  7103. TBL_PC* sd = script_rid2sd(st);
  7104. if( sd == NULL )
  7105. return 0;
  7106. nd->u.scr.rid = sd->bl.id;
  7107. }
  7108. npc_settimerevent_tick(nd,0);
  7109. npc_timerevent_start(nd, st->rid);
  7110. return 0;
  7111. }
  7112. /*==========================================
  7113. * NPCタイマー開始
  7114. *------------------------------------------*/
  7115. BUILDIN_FUNC(startnpctimer)
  7116. {
  7117. struct npc_data *nd;
  7118. int flag = 0;
  7119. if( script_hasdata(st,3) )
  7120. { //Two arguments: NPC name and attach flag.
  7121. nd = npc_name2id(script_getstr(st, 2));
  7122. flag = script_getnum(st,3);
  7123. }
  7124. else if( script_hasdata(st,2) )
  7125. { //Check if argument is numeric (flag) or string (npc name)
  7126. struct script_data *data;
  7127. data = script_getdata(st,2);
  7128. get_val(st,data);
  7129. if( data_isstring(data) ) //NPC name
  7130. nd = npc_name2id(conv_str(st, data));
  7131. else if( data_isint(data) ) //Flag
  7132. {
  7133. nd = (struct npc_data *)map_id2bl(st->oid);
  7134. flag = conv_num(st,data);
  7135. }
  7136. else
  7137. {
  7138. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7139. return 1;
  7140. }
  7141. }
  7142. else
  7143. nd=(struct npc_data *)map_id2bl(st->oid);
  7144. if( !nd )
  7145. return 0;
  7146. if( flag ) //Attach
  7147. {
  7148. TBL_PC* sd = script_rid2sd(st);
  7149. if( sd == NULL )
  7150. return 0;
  7151. nd->u.scr.rid = sd->bl.id;
  7152. }
  7153. npc_timerevent_start(nd, st->rid);
  7154. return 0;
  7155. }
  7156. /*==========================================
  7157. * NPCタイマー停止
  7158. *------------------------------------------*/
  7159. BUILDIN_FUNC(stopnpctimer)
  7160. {
  7161. struct npc_data *nd;
  7162. int flag = 0;
  7163. if( script_hasdata(st,3) )
  7164. { //Two arguments: NPC name and attach flag.
  7165. nd = npc_name2id(script_getstr(st, 2));
  7166. flag = script_getnum(st,3);
  7167. }
  7168. else if( script_hasdata(st,2) )
  7169. { //Check if argument is numeric (flag) or string (npc name)
  7170. struct script_data *data;
  7171. data = script_getdata(st,2);
  7172. get_val(st,data);
  7173. if( data_isstring(data) ) //NPC name
  7174. nd = npc_name2id(conv_str(st, data));
  7175. else if( data_isint(data) ) //Flag
  7176. {
  7177. nd = (struct npc_data *)map_id2bl(st->oid);
  7178. flag = conv_num(st,data);
  7179. }
  7180. else
  7181. {
  7182. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7183. return 1;
  7184. }
  7185. }
  7186. else
  7187. nd=(struct npc_data *)map_id2bl(st->oid);
  7188. if( !nd )
  7189. return 0;
  7190. if( flag ) //Detach
  7191. nd->u.scr.rid = 0;
  7192. npc_timerevent_stop(nd);
  7193. return 0;
  7194. }
  7195. /*==========================================
  7196. * NPCタイマー情報所得
  7197. *------------------------------------------*/
  7198. BUILDIN_FUNC(getnpctimer)
  7199. {
  7200. struct npc_data *nd;
  7201. TBL_PC *sd;
  7202. int type = script_getnum(st,2);
  7203. int val = 0;
  7204. if( script_hasdata(st,3) )
  7205. nd = npc_name2id(script_getstr(st,3));
  7206. else
  7207. nd = (struct npc_data *)map_id2bl(st->oid);
  7208. if( !nd || nd->bl.type != BL_NPC )
  7209. {
  7210. script_pushint(st,0);
  7211. ShowError("getnpctimer: Invalid NPC.\n");
  7212. return 1;
  7213. }
  7214. switch( type )
  7215. {
  7216. case 0: val = npc_gettimerevent_tick(nd); break;
  7217. case 1:
  7218. if( nd->u.scr.rid )
  7219. {
  7220. sd = map_id2sd(nd->u.scr.rid);
  7221. if( !sd )
  7222. {
  7223. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7224. break;
  7225. }
  7226. val = (sd->npc_timer_id != -1);
  7227. }
  7228. else
  7229. val = (nd->u.scr.timerid !=-1);
  7230. break;
  7231. case 2: val = nd->u.scr.timeramount; break;
  7232. }
  7233. script_pushint(st,val);
  7234. return 0;
  7235. }
  7236. /*==========================================
  7237. * NPCタイマー値設定
  7238. *------------------------------------------*/
  7239. BUILDIN_FUNC(setnpctimer)
  7240. {
  7241. int tick;
  7242. struct npc_data *nd;
  7243. tick = script_getnum(st,2);
  7244. if( script_hasdata(st,3) )
  7245. nd = npc_name2id(script_getstr(st,3));
  7246. else
  7247. nd = (struct npc_data *)map_id2bl(st->oid);
  7248. if( !nd || nd->bl.type != BL_NPC )
  7249. {
  7250. script_pushint(st,1);
  7251. ShowError("setnpctimer: Invalid NPC.\n");
  7252. return 1;
  7253. }
  7254. npc_settimerevent_tick(nd,tick);
  7255. script_pushint(st,0);
  7256. return 0;
  7257. }
  7258. /*==========================================
  7259. * attaches the player rid to the timer [Celest]
  7260. *------------------------------------------*/
  7261. BUILDIN_FUNC(attachnpctimer)
  7262. {
  7263. TBL_PC *sd;
  7264. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7265. if( !nd || nd->bl.type != BL_NPC )
  7266. {
  7267. script_pushint(st,1);
  7268. ShowError("setnpctimer: Invalid NPC.\n");
  7269. return 1;
  7270. }
  7271. if( script_hasdata(st,2) )
  7272. sd = map_nick2sd(script_getstr(st,2));
  7273. else
  7274. sd = script_rid2sd(st);
  7275. if( !sd )
  7276. {
  7277. script_pushint(st,1);
  7278. ShowWarning("attachnpctimer: Invalid player.\n");
  7279. return 1;
  7280. }
  7281. nd->u.scr.rid = sd->bl.id;
  7282. script_pushint(st,0);
  7283. return 0;
  7284. }
  7285. /*==========================================
  7286. * detaches a player rid from the timer [Celest]
  7287. *------------------------------------------*/
  7288. BUILDIN_FUNC(detachnpctimer)
  7289. {
  7290. struct npc_data *nd;
  7291. if( script_hasdata(st,2) )
  7292. nd = npc_name2id(script_getstr(st,2));
  7293. else
  7294. nd = (struct npc_data *)map_id2bl(st->oid);
  7295. if( !nd || nd->bl.type != BL_NPC )
  7296. {
  7297. script_pushint(st,1);
  7298. ShowError("detachnpctimer: Invalid NPC.\n");
  7299. return 1;
  7300. }
  7301. nd->u.scr.rid = 0;
  7302. script_pushint(st,0);
  7303. return 0;
  7304. }
  7305. /*==========================================
  7306. * To avoid "player not attached" script errors, this function is provided,
  7307. * it checks if there is a player attached to the current script. [Skotlex]
  7308. * If no, returns 0, if yes, returns the account_id of the attached player.
  7309. *------------------------------------------*/
  7310. BUILDIN_FUNC(playerattached)
  7311. {
  7312. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7313. script_pushint(st,0);
  7314. else
  7315. script_pushint(st,st->rid);
  7316. return 0;
  7317. }
  7318. /*==========================================
  7319. * 天の声アナウンス
  7320. *------------------------------------------*/
  7321. BUILDIN_FUNC(announce)
  7322. {
  7323. const char *mes = script_getstr(st,2);
  7324. int flag = script_getnum(st,3);
  7325. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7326. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7327. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7328. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7329. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7330. if (flag&0x0f) // Broadcast source or broadcast region defined
  7331. {
  7332. send_target target;
  7333. 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
  7334. if (bl == NULL)
  7335. return 0;
  7336. flag &= 0x07;
  7337. target = (flag == 1) ? ALL_SAMEMAP :
  7338. (flag == 2) ? AREA :
  7339. (flag == 3) ? SELF :
  7340. ALL_CLIENT;
  7341. if (fontColor)
  7342. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7343. else
  7344. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7345. }
  7346. else
  7347. {
  7348. if (fontColor)
  7349. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7350. else
  7351. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7352. }
  7353. return 0;
  7354. }
  7355. /*==========================================
  7356. * 天の声アナウンス(特定マップ)
  7357. *------------------------------------------*/
  7358. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7359. {
  7360. char *mes = va_arg(ap, char *);
  7361. int len = va_arg(ap, int);
  7362. int type = va_arg(ap, int);
  7363. char *fontColor = va_arg(ap, char *);
  7364. short fontType = (short)va_arg(ap, int);
  7365. short fontSize = (short)va_arg(ap, int);
  7366. short fontAlign = (short)va_arg(ap, int);
  7367. short fontY = (short)va_arg(ap, int);
  7368. if (fontColor)
  7369. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7370. else
  7371. clif_broadcast(bl, mes, len, type, SELF);
  7372. return 0;
  7373. }
  7374. BUILDIN_FUNC(mapannounce)
  7375. {
  7376. const char *mapname = script_getstr(st,2);
  7377. const char *mes = script_getstr(st,3);
  7378. int flag = script_getnum(st,4);
  7379. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7380. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7381. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7382. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7383. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7384. int m;
  7385. if ((m = map_mapname2mapid(mapname)) < 0)
  7386. return 0;
  7387. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7388. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7389. return 0;
  7390. }
  7391. /*==========================================
  7392. * 天の声アナウンス(特定エリア)
  7393. *------------------------------------------*/
  7394. BUILDIN_FUNC(areaannounce)
  7395. {
  7396. const char *mapname = script_getstr(st,2);
  7397. int x0 = script_getnum(st,3);
  7398. int y0 = script_getnum(st,4);
  7399. int x1 = script_getnum(st,5);
  7400. int y1 = script_getnum(st,6);
  7401. const char *mes = script_getstr(st,7);
  7402. int flag = script_getnum(st,8);
  7403. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7404. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7405. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7406. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7407. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7408. int m;
  7409. if ((m = map_mapname2mapid(mapname)) < 0)
  7410. return 0;
  7411. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7412. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7413. return 0;
  7414. }
  7415. /*==========================================
  7416. * ユーザー数所得
  7417. *------------------------------------------*/
  7418. BUILDIN_FUNC(getusers)
  7419. {
  7420. int flag, val = 0;
  7421. struct map_session_data* sd;
  7422. struct block_list* bl = NULL;
  7423. flag = script_getnum(st,2);
  7424. switch(flag&0x07)
  7425. {
  7426. case 0:
  7427. if(flag&0x8)
  7428. {// npc
  7429. bl = map_id2bl(st->oid);
  7430. }
  7431. else if((sd = script_rid2sd(st))!=NULL)
  7432. {// pc
  7433. bl = &sd->bl;
  7434. }
  7435. if(bl)
  7436. {
  7437. val = map[bl->m].users;
  7438. }
  7439. break;
  7440. case 1:
  7441. val = map_getusers();
  7442. break;
  7443. default:
  7444. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  7445. script_pushint(st,0);
  7446. return 1;
  7447. }
  7448. script_pushint(st,val);
  7449. return 0;
  7450. }
  7451. /*==========================================
  7452. * Works like @WHO - displays all online users names in window
  7453. *------------------------------------------*/
  7454. BUILDIN_FUNC(getusersname)
  7455. {
  7456. TBL_PC *sd, *pl_sd;
  7457. int disp_num=1;
  7458. struct s_mapiterator* iter;
  7459. sd = script_rid2sd(st);
  7460. if (!sd) return 0;
  7461. iter = mapit_getallusers();
  7462. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7463. {
  7464. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7465. continue; // skip hidden GMs
  7466. if((disp_num++)%10==0)
  7467. clif_scriptnext(sd,st->oid);
  7468. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7469. }
  7470. mapit_free(iter);
  7471. return 0;
  7472. }
  7473. /*==========================================
  7474. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7475. *------------------------------------------*/
  7476. BUILDIN_FUNC(getmapguildusers)
  7477. {
  7478. const char *str;
  7479. int m, gid;
  7480. int i=0,c=0;
  7481. struct guild *g = NULL;
  7482. str=script_getstr(st,2);
  7483. gid=script_getnum(st,3);
  7484. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7485. script_pushint(st,-1);
  7486. return 0;
  7487. }
  7488. g = guild_search(gid);
  7489. if (g){
  7490. for(i = 0; i < g->max_member; i++)
  7491. {
  7492. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7493. c++;
  7494. }
  7495. }
  7496. script_pushint(st,c);
  7497. return 0;
  7498. }
  7499. /*==========================================
  7500. * マップ指定ユーザー数所得
  7501. *------------------------------------------*/
  7502. BUILDIN_FUNC(getmapusers)
  7503. {
  7504. const char *str;
  7505. int m;
  7506. str=script_getstr(st,2);
  7507. if( (m=map_mapname2mapid(str))< 0){
  7508. script_pushint(st,-1);
  7509. return 0;
  7510. }
  7511. script_pushint(st,map[m].users);
  7512. return 0;
  7513. }
  7514. /*==========================================
  7515. * エリア指定ユーザー数所得
  7516. *------------------------------------------*/
  7517. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7518. {
  7519. int *users=va_arg(ap,int *);
  7520. (*users)++;
  7521. return 0;
  7522. }
  7523. BUILDIN_FUNC(getareausers)
  7524. {
  7525. const char *str;
  7526. int m,x0,y0,x1,y1,users=0;
  7527. str=script_getstr(st,2);
  7528. x0=script_getnum(st,3);
  7529. y0=script_getnum(st,4);
  7530. x1=script_getnum(st,5);
  7531. y1=script_getnum(st,6);
  7532. if( (m=map_mapname2mapid(str))< 0){
  7533. script_pushint(st,-1);
  7534. return 0;
  7535. }
  7536. map_foreachinarea(buildin_getareausers_sub,
  7537. m,x0,y0,x1,y1,BL_PC,&users);
  7538. script_pushint(st,users);
  7539. return 0;
  7540. }
  7541. /*==========================================
  7542. * エリア指定ドロップアイテム数所得
  7543. *------------------------------------------*/
  7544. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7545. {
  7546. int item=va_arg(ap,int);
  7547. int *amount=va_arg(ap,int *);
  7548. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7549. if(drop->item_data.nameid==item)
  7550. (*amount)+=drop->item_data.amount;
  7551. return 0;
  7552. }
  7553. BUILDIN_FUNC(getareadropitem)
  7554. {
  7555. const char *str;
  7556. int m,x0,y0,x1,y1,item,amount=0;
  7557. struct script_data *data;
  7558. str=script_getstr(st,2);
  7559. x0=script_getnum(st,3);
  7560. y0=script_getnum(st,4);
  7561. x1=script_getnum(st,5);
  7562. y1=script_getnum(st,6);
  7563. data=script_getdata(st,7);
  7564. get_val(st,data);
  7565. if( data_isstring(data) ){
  7566. const char *name=conv_str(st,data);
  7567. struct item_data *item_data = itemdb_searchname(name);
  7568. item=UNKNOWN_ITEM_ID;
  7569. if( item_data )
  7570. item=item_data->nameid;
  7571. }else
  7572. item=conv_num(st,data);
  7573. if( (m=map_mapname2mapid(str))< 0){
  7574. script_pushint(st,-1);
  7575. return 0;
  7576. }
  7577. map_foreachinarea(buildin_getareadropitem_sub,
  7578. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7579. script_pushint(st,amount);
  7580. return 0;
  7581. }
  7582. /*==========================================
  7583. * NPCの有効化
  7584. *------------------------------------------*/
  7585. BUILDIN_FUNC(enablenpc)
  7586. {
  7587. const char *str;
  7588. str=script_getstr(st,2);
  7589. npc_enable(str,1);
  7590. return 0;
  7591. }
  7592. /*==========================================
  7593. * NPCの無効化
  7594. *------------------------------------------*/
  7595. BUILDIN_FUNC(disablenpc)
  7596. {
  7597. const char *str;
  7598. str=script_getstr(st,2);
  7599. npc_enable(str,0);
  7600. return 0;
  7601. }
  7602. /*==========================================
  7603. * 隠れているNPCの表示
  7604. *------------------------------------------*/
  7605. BUILDIN_FUNC(hideoffnpc)
  7606. {
  7607. const char *str;
  7608. str=script_getstr(st,2);
  7609. npc_enable(str,2);
  7610. return 0;
  7611. }
  7612. /*==========================================
  7613. * NPCをハイディング
  7614. *------------------------------------------*/
  7615. BUILDIN_FUNC(hideonnpc)
  7616. {
  7617. const char *str;
  7618. str=script_getstr(st,2);
  7619. npc_enable(str,4);
  7620. return 0;
  7621. }
  7622. /// Starts a status effect on the target unit or on the attached player.
  7623. ///
  7624. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7625. BUILDIN_FUNC(sc_start)
  7626. {
  7627. struct block_list* bl;
  7628. enum sc_type type;
  7629. int tick;
  7630. int val1;
  7631. int val4 = 0;
  7632. type = (sc_type)script_getnum(st,2);
  7633. tick = script_getnum(st,3);
  7634. val1 = script_getnum(st,4);
  7635. if( script_hasdata(st,5) )
  7636. bl = map_id2bl(script_getnum(st,5));
  7637. else
  7638. bl = map_id2bl(st->rid);
  7639. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7640. {// When there isn't a duration specified, try to get it from the skill_db
  7641. tick = skill_get_time(status_sc2skill(type), val1);
  7642. }
  7643. if( potion_flag == 1 && potion_target )
  7644. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7645. bl = map_id2bl(potion_target);
  7646. tick /= 2;// Thrown potions only last half.
  7647. val4 = 1;// Mark that this was a thrown sc_effect
  7648. }
  7649. if( bl )
  7650. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7651. return 0;
  7652. }
  7653. /// Starts a status effect on the target unit or on the attached player.
  7654. ///
  7655. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7656. BUILDIN_FUNC(sc_start2)
  7657. {
  7658. struct block_list* bl;
  7659. enum sc_type type;
  7660. int tick;
  7661. int val1;
  7662. int val4 = 0;
  7663. int rate;
  7664. type = (sc_type)script_getnum(st,2);
  7665. tick = script_getnum(st,3);
  7666. val1 = script_getnum(st,4);
  7667. rate = script_getnum(st,5);
  7668. if( script_hasdata(st,6) )
  7669. bl = map_id2bl(script_getnum(st,6));
  7670. else
  7671. bl = map_id2bl(st->rid);
  7672. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7673. {// When there isn't a duration specified, try to get it from the skill_db
  7674. tick = skill_get_time(status_sc2skill(type), val1);
  7675. }
  7676. if( potion_flag == 1 && potion_target )
  7677. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7678. bl = map_id2bl(potion_target);
  7679. tick /= 2;// Thrown potions only last half.
  7680. val4 = 1;// Mark that this was a thrown sc_effect
  7681. }
  7682. if( bl )
  7683. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7684. return 0;
  7685. }
  7686. /// Starts a status effect on the target unit or on the attached player.
  7687. ///
  7688. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7689. BUILDIN_FUNC(sc_start4)
  7690. {
  7691. struct block_list* bl;
  7692. enum sc_type type;
  7693. int tick;
  7694. int val1;
  7695. int val2;
  7696. int val3;
  7697. int val4;
  7698. type = (sc_type)script_getnum(st,2);
  7699. tick = script_getnum(st,3);
  7700. val1 = script_getnum(st,4);
  7701. val2 = script_getnum(st,5);
  7702. val3 = script_getnum(st,6);
  7703. val4 = script_getnum(st,7);
  7704. if( script_hasdata(st,8) )
  7705. bl = map_id2bl(script_getnum(st,8));
  7706. else
  7707. bl = map_id2bl(st->rid);
  7708. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7709. {// When there isn't a duration specified, try to get it from the skill_db
  7710. tick = skill_get_time(status_sc2skill(type), val1);
  7711. }
  7712. if( potion_flag == 1 && potion_target )
  7713. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7714. bl = map_id2bl(potion_target);
  7715. tick /= 2;// Thrown potions only last half.
  7716. }
  7717. if( bl )
  7718. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7719. return 0;
  7720. }
  7721. /// Ends one or all status effects on the target unit or on the attached player.
  7722. ///
  7723. /// sc_end <effect_id>{,<unit_id>};
  7724. BUILDIN_FUNC(sc_end)
  7725. {
  7726. struct block_list* bl;
  7727. int type;
  7728. type = script_getnum(st,2);
  7729. if( script_hasdata(st,3) )
  7730. bl = map_id2bl(script_getnum(st,3));
  7731. else
  7732. bl = map_id2bl(st->rid);
  7733. if( potion_flag==1 && potion_target )
  7734. {//##TODO how does this work [FlavioJS]
  7735. bl = map_id2bl(potion_target);
  7736. }
  7737. if( !bl ) return 0;
  7738. if( type >= 0 && type < SC_MAX )
  7739. {
  7740. struct status_change *sc = status_get_sc(bl);
  7741. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7742. if (!sce) return 0;
  7743. //This should help status_change_end force disabling the SC in case it has no limit.
  7744. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7745. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7746. } else
  7747. status_change_clear(bl, 2);// remove all effects
  7748. return 0;
  7749. }
  7750. /*==========================================
  7751. * 状態異常耐性を計算した確率を返す
  7752. *------------------------------------------*/
  7753. BUILDIN_FUNC(getscrate)
  7754. {
  7755. struct block_list *bl;
  7756. int type,rate;
  7757. type=script_getnum(st,2);
  7758. rate=script_getnum(st,3);
  7759. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7760. bl = map_id2bl(script_getnum(st,4));
  7761. else
  7762. bl = map_id2bl(st->rid);
  7763. if (bl)
  7764. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7765. script_pushint(st,rate);
  7766. return 0;
  7767. }
  7768. /*==========================================
  7769. *
  7770. *------------------------------------------*/
  7771. BUILDIN_FUNC(debugmes)
  7772. {
  7773. const char *str;
  7774. str=script_getstr(st,2);
  7775. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7776. return 0;
  7777. }
  7778. /*==========================================
  7779. *捕獲アイテム使用
  7780. *------------------------------------------*/
  7781. BUILDIN_FUNC(catchpet)
  7782. {
  7783. int pet_id;
  7784. TBL_PC *sd;
  7785. pet_id= script_getnum(st,2);
  7786. sd=script_rid2sd(st);
  7787. if( sd == NULL )
  7788. return 0;
  7789. pet_catch_process1(sd,pet_id);
  7790. return 0;
  7791. }
  7792. /*==========================================
  7793. * [orn]
  7794. *------------------------------------------*/
  7795. BUILDIN_FUNC(homunculus_evolution)
  7796. {
  7797. TBL_PC *sd;
  7798. sd=script_rid2sd(st);
  7799. if( sd == NULL )
  7800. return 0;
  7801. if(merc_is_hom_active(sd->hd))
  7802. {
  7803. if (sd->hd->homunculus.intimacy > 91000)
  7804. merc_hom_evolution(sd->hd);
  7805. else
  7806. clif_emotion(&sd->hd->bl, E_SWT);
  7807. }
  7808. return 0;
  7809. }
  7810. // [Zephyrus]
  7811. BUILDIN_FUNC(homunculus_shuffle)
  7812. {
  7813. TBL_PC *sd;
  7814. sd=script_rid2sd(st);
  7815. if( sd == NULL )
  7816. return 0;
  7817. if(merc_is_hom_active(sd->hd))
  7818. merc_hom_shuffle(sd->hd);
  7819. return 0;
  7820. }
  7821. //These two functions bring the eA MAPID_* class functionality to scripts.
  7822. BUILDIN_FUNC(eaclass)
  7823. {
  7824. int class_;
  7825. if( script_hasdata(st,2) )
  7826. class_ = script_getnum(st,2);
  7827. else {
  7828. TBL_PC *sd;
  7829. sd=script_rid2sd(st);
  7830. if (!sd) {
  7831. script_pushint(st,-1);
  7832. return 0;
  7833. }
  7834. class_ = sd->status.class_;
  7835. }
  7836. script_pushint(st,pc_jobid2mapid(class_));
  7837. return 0;
  7838. }
  7839. BUILDIN_FUNC(roclass)
  7840. {
  7841. int class_ =script_getnum(st,2);
  7842. int sex;
  7843. if( script_hasdata(st,3) )
  7844. sex = script_getnum(st,3);
  7845. else {
  7846. TBL_PC *sd;
  7847. if (st->rid && (sd=script_rid2sd(st)))
  7848. sex = sd->status.sex;
  7849. else
  7850. sex = 1; //Just use male when not found.
  7851. }
  7852. script_pushint(st,pc_mapid2jobid(class_, sex));
  7853. return 0;
  7854. }
  7855. /*==========================================
  7856. *携帯卵孵化機使用
  7857. *------------------------------------------*/
  7858. BUILDIN_FUNC(birthpet)
  7859. {
  7860. TBL_PC *sd;
  7861. sd=script_rid2sd(st);
  7862. if( sd == NULL )
  7863. return 0;
  7864. clif_sendegg(sd);
  7865. return 0;
  7866. }
  7867. /*==========================================
  7868. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7869. *------------------------------------------*/
  7870. BUILDIN_FUNC(resetlvl)
  7871. {
  7872. TBL_PC *sd;
  7873. int type=script_getnum(st,2);
  7874. sd=script_rid2sd(st);
  7875. if( sd == NULL )
  7876. return 0;
  7877. pc_resetlvl(sd,type);
  7878. return 0;
  7879. }
  7880. /*==========================================
  7881. * ステータスリセット
  7882. *------------------------------------------*/
  7883. BUILDIN_FUNC(resetstatus)
  7884. {
  7885. TBL_PC *sd;
  7886. sd=script_rid2sd(st);
  7887. pc_resetstate(sd);
  7888. return 0;
  7889. }
  7890. /*==========================================
  7891. * script command resetskill
  7892. *------------------------------------------*/
  7893. BUILDIN_FUNC(resetskill)
  7894. {
  7895. TBL_PC *sd;
  7896. sd=script_rid2sd(st);
  7897. pc_resetskill(sd,1);
  7898. return 0;
  7899. }
  7900. /*==========================================
  7901. * Counts total amount of skill points.
  7902. *------------------------------------------*/
  7903. BUILDIN_FUNC(skillpointcount)
  7904. {
  7905. TBL_PC *sd;
  7906. sd=script_rid2sd(st);
  7907. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7908. return 0;
  7909. }
  7910. /*==========================================
  7911. *
  7912. *------------------------------------------*/
  7913. BUILDIN_FUNC(changebase)
  7914. {
  7915. TBL_PC *sd=NULL;
  7916. int vclass;
  7917. if( script_hasdata(st,3) )
  7918. sd=map_id2sd(script_getnum(st,3));
  7919. else
  7920. sd=script_rid2sd(st);
  7921. if(sd == NULL)
  7922. return 0;
  7923. vclass = script_getnum(st,2);
  7924. if(vclass == JOB_WEDDING)
  7925. {
  7926. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7927. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7928. )
  7929. return 0;
  7930. }
  7931. if(!sd->disguise && vclass != sd->vd.class_) {
  7932. status_set_viewdata(&sd->bl, vclass);
  7933. //Updated client view. Base, Weapon and Cloth Colors.
  7934. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7935. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7936. if (sd->vd.cloth_color)
  7937. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7938. clif_skillinfoblock(sd);
  7939. }
  7940. return 0;
  7941. }
  7942. /*==========================================
  7943. * 性別変換
  7944. *------------------------------------------*/
  7945. BUILDIN_FUNC(changesex)
  7946. {
  7947. TBL_PC *sd = NULL;
  7948. sd = script_rid2sd(st);
  7949. chrif_changesex(sd);
  7950. return 0;
  7951. }
  7952. /*==========================================
  7953. * Works like 'announce' but outputs in the common chat window
  7954. *------------------------------------------*/
  7955. BUILDIN_FUNC(globalmes)
  7956. {
  7957. struct block_list *bl = map_id2bl(st->oid);
  7958. struct npc_data *nd = (struct npc_data *)bl;
  7959. const char *name=NULL,*mes;
  7960. mes=script_getstr(st,2); // メッセージの取得
  7961. if(mes==NULL) return 0;
  7962. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7963. name=script_getstr(st,3);
  7964. } else {
  7965. name=nd->name;
  7966. }
  7967. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7968. return 0;
  7969. }
  7970. /////////////////////////////////////////////////////////////////////
  7971. // NPC waiting room (chat room)
  7972. //
  7973. /// Creates a waiting room (chat room) for this npc.
  7974. ///
  7975. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7976. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7977. /// waitingroom "<title>",<limit>,"<event>";
  7978. /// waitingroom "<title>",<limit>;
  7979. BUILDIN_FUNC(waitingroom)
  7980. {
  7981. struct npc_data* nd;
  7982. const char* title;
  7983. const char* ev = "";
  7984. int limit;
  7985. int trigger = 0;
  7986. int pub = 1;
  7987. title = script_getstr(st, 2);
  7988. limit = script_getnum(st, 3);
  7989. if( script_hasdata(st,5) )
  7990. {
  7991. struct script_data* last = script_getdata(st, 5);
  7992. get_val(st, last);
  7993. if( data_isstring(last) )
  7994. {// ,<trigger>,"<event>"
  7995. trigger = script_getnum(st, 4);
  7996. ev = script_getstr(st, 5);
  7997. }
  7998. else
  7999. {// ,"<event>",<trigger>
  8000. ev = script_getstr(st, 4);
  8001. trigger = script_getnum(st,5);
  8002. }
  8003. }
  8004. else if( script_hasdata(st,4) )
  8005. {// ,"<event>"
  8006. ev = script_getstr(st, 4);
  8007. trigger = limit;
  8008. }
  8009. nd = (struct npc_data *)map_id2bl(st->oid);
  8010. if( nd != NULL )
  8011. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  8012. return 0;
  8013. }
  8014. /// Removes the waiting room of the current or target npc.
  8015. ///
  8016. /// delwaitingroom "<npc_name>";
  8017. /// delwaitingroom;
  8018. BUILDIN_FUNC(delwaitingroom)
  8019. {
  8020. struct npc_data* nd;
  8021. if( script_hasdata(st,2) )
  8022. nd = npc_name2id(script_getstr(st, 2));
  8023. else
  8024. nd = (struct npc_data *)map_id2bl(st->oid);
  8025. if( nd != NULL )
  8026. chat_deletenpcchat(nd);
  8027. return 0;
  8028. }
  8029. /// Kicks all the players from the waiting room of the current or target npc.
  8030. ///
  8031. /// kickwaitingroomall "<npc_name>";
  8032. /// kickwaitingroomall;
  8033. BUILDIN_FUNC(waitingroomkickall)
  8034. {
  8035. struct npc_data* nd;
  8036. struct chat_data* cd;
  8037. if( script_hasdata(st,2) )
  8038. nd = npc_name2id(script_getstr(st,2));
  8039. else
  8040. nd = (struct npc_data *)map_id2bl(st->oid);
  8041. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8042. chat_npckickall(cd);
  8043. return 0;
  8044. }
  8045. /// Enables the waiting room event of the current or target npc.
  8046. ///
  8047. /// enablewaitingroomevent "<npc_name>";
  8048. /// enablewaitingroomevent;
  8049. BUILDIN_FUNC(enablewaitingroomevent)
  8050. {
  8051. struct npc_data* nd;
  8052. struct chat_data* cd;
  8053. if( script_hasdata(st,2) )
  8054. nd = npc_name2id(script_getstr(st, 2));
  8055. else
  8056. nd = (struct npc_data *)map_id2bl(st->oid);
  8057. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8058. chat_enableevent(cd);
  8059. return 0;
  8060. }
  8061. /// Disables the waiting room event of the current or target npc.
  8062. ///
  8063. /// disablewaitingroomevent "<npc_name>";
  8064. /// disablewaitingroomevent;
  8065. BUILDIN_FUNC(disablewaitingroomevent)
  8066. {
  8067. struct npc_data *nd;
  8068. struct chat_data *cd;
  8069. if( script_hasdata(st,2) )
  8070. nd = npc_name2id(script_getstr(st, 2));
  8071. else
  8072. nd = (struct npc_data *)map_id2bl(st->oid);
  8073. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8074. chat_disableevent(cd);
  8075. return 0;
  8076. }
  8077. /// Returns info on the waiting room of the current or target npc.
  8078. /// Returns -1 if the type unknown
  8079. /// <type>=0 : current number of users
  8080. /// <type>=1 : maximum number of users allowed
  8081. /// <type>=2 : the number of users that trigger the event
  8082. /// <type>=3 : if the trigger is disabled
  8083. /// <type>=4 : the title of the waiting room
  8084. /// <type>=5 : the password of the waiting room
  8085. /// <type>=16 : the name of the waiting room event
  8086. /// <type>=32 : if the waiting room is full
  8087. /// <type>=33 : if there are enough users to trigger the event
  8088. ///
  8089. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8090. /// getwaitingroomstate(<type>) -> <info>
  8091. BUILDIN_FUNC(getwaitingroomstate)
  8092. {
  8093. struct npc_data *nd;
  8094. struct chat_data *cd;
  8095. int type;
  8096. type = script_getnum(st,2);
  8097. if( script_hasdata(st,3) )
  8098. nd = npc_name2id(script_getstr(st, 3));
  8099. else
  8100. nd = (struct npc_data *)map_id2bl(st->oid);
  8101. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8102. {
  8103. script_pushint(st, -1);
  8104. return 0;
  8105. }
  8106. switch(type)
  8107. {
  8108. case 0: script_pushint(st, cd->users); break;
  8109. case 1: script_pushint(st, cd->limit); break;
  8110. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8111. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8112. case 4: script_pushstrcopy(st, cd->title); break;
  8113. case 5: script_pushstrcopy(st, cd->pass); break;
  8114. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8115. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8116. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8117. default: script_pushint(st, -1); break;
  8118. }
  8119. return 0;
  8120. }
  8121. /// Warps the trigger or target amount of players to the target map and position.
  8122. /// Players are automatically removed from the waiting room.
  8123. /// Those waiting the longest will get warped first.
  8124. /// The target map can be "Random" for a random position in the current map,
  8125. /// and "SavePoint" for the savepoint map+position.
  8126. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8127. ///
  8128. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8129. /// The total number of teleported players is put into $@warpwaitingpcnum
  8130. ///
  8131. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8132. /// warpwaitingpc "<map name>",<x>,<y>;
  8133. BUILDIN_FUNC(warpwaitingpc)
  8134. {
  8135. int x;
  8136. int y;
  8137. int i;
  8138. int n;
  8139. const char* map_name;
  8140. struct npc_data* nd;
  8141. struct chat_data* cd;
  8142. TBL_PC* sd;
  8143. nd = (struct npc_data *)map_id2bl(st->oid);
  8144. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8145. return 0;
  8146. map_name = script_getstr(st,2);
  8147. x = script_getnum(st,3);
  8148. y = script_getnum(st,4);
  8149. n = cd->trigger&0x7f;
  8150. if( script_hasdata(st,5) )
  8151. n = script_getnum(st,5);
  8152. for( i = 0; i < n && cd->users > 0; i++ )
  8153. {
  8154. sd = cd->usersd[0];
  8155. if( sd == NULL )
  8156. {
  8157. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  8158. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  8159. continue;// Broken npc chat room?
  8160. }
  8161. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  8162. if( strcmp(map_name,"Random") == 0 )
  8163. pc_randomwarp(sd,CLR_TELEPORT);
  8164. else if( strcmp(map_name,"SavePoint") == 0 )
  8165. {
  8166. if( map[sd->bl.m].flag.noteleport )
  8167. return 0;// can't teleport on this map
  8168. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  8169. }
  8170. else
  8171. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  8172. }
  8173. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8174. return 0;
  8175. }
  8176. /////////////////////////////////////////////////////////////////////
  8177. // ...
  8178. //
  8179. /// Detaches a character from a script.
  8180. ///
  8181. /// @param st Script state to detach the character from.
  8182. static void script_detach_rid(struct script_state* st)
  8183. {
  8184. if(st->rid)
  8185. {
  8186. script_detach_state(st, false);
  8187. st->rid = 0;
  8188. }
  8189. }
  8190. /*==========================================
  8191. * RIDのアタッチ
  8192. *------------------------------------------*/
  8193. BUILDIN_FUNC(attachrid)
  8194. {
  8195. int rid = script_getnum(st,2);
  8196. struct map_session_data* sd;
  8197. if ((sd = map_id2sd(rid))!=NULL) {
  8198. script_detach_rid(st);
  8199. st->rid = rid;
  8200. script_attach_state(st);
  8201. script_pushint(st,1);
  8202. } else
  8203. script_pushint(st,0);
  8204. return 0;
  8205. }
  8206. /*==========================================
  8207. * RIDのデタッチ
  8208. *------------------------------------------*/
  8209. BUILDIN_FUNC(detachrid)
  8210. {
  8211. script_detach_rid(st);
  8212. return 0;
  8213. }
  8214. /*==========================================
  8215. * 存在チェック
  8216. *------------------------------------------*/
  8217. BUILDIN_FUNC(isloggedin)
  8218. {
  8219. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8220. if (script_hasdata(st,3) && sd &&
  8221. sd->status.char_id != script_getnum(st,3))
  8222. sd = NULL;
  8223. push_val(st->stack,C_INT,sd!=NULL);
  8224. return 0;
  8225. }
  8226. /*==========================================
  8227. *
  8228. *------------------------------------------*/
  8229. BUILDIN_FUNC(setmapflagnosave)
  8230. {
  8231. int m,x,y;
  8232. unsigned short mapindex;
  8233. const char *str,*str2;
  8234. str=script_getstr(st,2);
  8235. str2=script_getstr(st,3);
  8236. x=script_getnum(st,4);
  8237. y=script_getnum(st,5);
  8238. m = map_mapname2mapid(str);
  8239. mapindex = mapindex_name2id(str2);
  8240. if(m >= 0 && mapindex) {
  8241. map[m].flag.nosave=1;
  8242. map[m].save.map=mapindex;
  8243. map[m].save.x=x;
  8244. map[m].save.y=y;
  8245. }
  8246. return 0;
  8247. }
  8248. BUILDIN_FUNC(getmapflag)
  8249. {
  8250. int m,i;
  8251. const char *str;
  8252. str=script_getstr(st,2);
  8253. i=script_getnum(st,3);
  8254. m = map_mapname2mapid(str);
  8255. if(m >= 0) {
  8256. switch(i) {
  8257. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8258. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8259. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8260. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8261. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8262. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8263. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8264. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8265. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8266. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8267. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8268. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8269. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8270. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8271. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8272. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8273. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8274. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8275. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8276. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8277. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8278. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8279. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8280. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8281. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8282. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8283. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8284. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8285. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8286. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8287. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8288. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8289. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8290. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8291. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8292. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8293. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8294. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8295. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8296. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8297. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8298. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8299. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8300. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8301. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8302. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8303. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8304. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8305. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8306. }
  8307. }
  8308. return 0;
  8309. }
  8310. BUILDIN_FUNC(setmapflag)
  8311. {
  8312. int m,i;
  8313. const char *str;
  8314. const char *val=NULL;
  8315. str=script_getstr(st,2);
  8316. i=script_getnum(st,3);
  8317. if(script_hasdata(st,4)){
  8318. val=script_getstr(st,4);
  8319. }
  8320. m = map_mapname2mapid(str);
  8321. if(m >= 0) {
  8322. switch(i) {
  8323. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  8324. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  8325. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  8326. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  8327. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  8328. case MF_PVP: map[m].flag.pvp=1; break;
  8329. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  8330. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  8331. case MF_GVG: map[m].flag.gvg=1; break;
  8332. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  8333. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  8334. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  8335. case MF_NOTRADE: map[m].flag.notrade=1; break;
  8336. case MF_NODROP: map[m].flag.nodrop=1; break;
  8337. case MF_NOSKILL: map[m].flag.noskill=1; break;
  8338. case MF_NOWARP: map[m].flag.nowarp=1; break;
  8339. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  8340. case MF_SNOW: map[m].flag.snow=1; break;
  8341. case MF_CLOUDS: map[m].flag.clouds=1; break;
  8342. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  8343. case MF_FOG: map[m].flag.fog=1; break;
  8344. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  8345. case MF_SAKURA: map[m].flag.sakura=1; break;
  8346. case MF_LEAVES: map[m].flag.leaves=1; break;
  8347. case MF_RAIN: map[m].flag.rain=1; break;
  8348. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  8349. case MF_NOGO: map[m].flag.nogo=1; break;
  8350. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  8351. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  8352. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  8353. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  8354. case MF_NORETURN: map[m].flag.noreturn=1; break;
  8355. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  8356. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  8357. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  8358. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8359. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8360. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8361. case MF_NOVENDING: map[m].flag.novending=1; break;
  8362. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  8363. case MF_NOCHAT: map[m].flag.nochat=1; break;
  8364. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  8365. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  8366. case MF_TOWN: map[m].flag.town=1; break;
  8367. case MF_AUTOTRADE: map[m].flag.autotrade=1; break;
  8368. case MF_ALLOWKS: map[m].flag.allowks=1; break;
  8369. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport=1; break;
  8370. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank=1; break;
  8371. case MF_BATTLEGROUND: map[m].flag.battleground = (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  8372. }
  8373. }
  8374. return 0;
  8375. }
  8376. BUILDIN_FUNC(removemapflag)
  8377. {
  8378. int m,i;
  8379. const char *str;
  8380. str=script_getstr(st,2);
  8381. i=script_getnum(st,3);
  8382. m = map_mapname2mapid(str);
  8383. if(m >= 0) {
  8384. switch(i) {
  8385. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  8386. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  8387. case MF_NOSAVE: map[m].flag.nosave=0; break;
  8388. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  8389. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  8390. case MF_PVP: map[m].flag.pvp=0; break;
  8391. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  8392. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  8393. case MF_GVG: map[m].flag.gvg=0; break;
  8394. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8395. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8396. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8397. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8398. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8399. case MF_NODROP: map[m].flag.nodrop=0; break;
  8400. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8401. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8402. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8403. case MF_SNOW: map[m].flag.snow=0; break;
  8404. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8405. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8406. case MF_FOG: map[m].flag.fog=0; break;
  8407. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8408. case MF_SAKURA: map[m].flag.sakura=0; break;
  8409. case MF_LEAVES: map[m].flag.leaves=0; break;
  8410. case MF_RAIN: map[m].flag.rain=0; break;
  8411. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8412. case MF_NOGO: map[m].flag.nogo=0; break;
  8413. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8414. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8415. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8416. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8417. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8418. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8419. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8420. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8421. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8422. case MF_JEXP: map[m].jexp=100; break;
  8423. case MF_BEXP: map[m].bexp=100; break;
  8424. case MF_NOVENDING: map[m].flag.novending=0; break;
  8425. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8426. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8427. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8428. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8429. case MF_TOWN: map[m].flag.town=0; break;
  8430. case MF_AUTOTRADE: map[m].flag.autotrade=0; break;
  8431. case MF_ALLOWKS: map[m].flag.allowks=0; break;
  8432. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport=0; break;
  8433. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank=0; break;
  8434. case MF_BATTLEGROUND: map[m].flag.battleground=0; break;
  8435. }
  8436. }
  8437. return 0;
  8438. }
  8439. BUILDIN_FUNC(pvpon)
  8440. {
  8441. int m;
  8442. const char *str;
  8443. TBL_PC* sd = NULL;
  8444. struct s_mapiterator* iter;
  8445. str = script_getstr(st,2);
  8446. m = map_mapname2mapid(str);
  8447. if( m < 0 || map[m].flag.pvp )
  8448. return 0; // nothing to do
  8449. map[m].flag.pvp = 1;
  8450. clif_send0199(m,1);
  8451. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8452. return 0;
  8453. iter = mapit_getallusers();
  8454. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8455. {
  8456. if( sd->bl.m != m || sd->pvp_timer != -1 )
  8457. continue; // not applicable
  8458. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8459. sd->pvp_rank = 0;
  8460. sd->pvp_lastusers = 0;
  8461. sd->pvp_point = 5;
  8462. sd->pvp_won = 0;
  8463. sd->pvp_lost = 0;
  8464. }
  8465. mapit_free(iter);
  8466. return 0;
  8467. }
  8468. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8469. {
  8470. TBL_PC* sd = (TBL_PC*)bl;
  8471. clif_pvpset(sd, 0, 0, 2);
  8472. if (sd->pvp_timer != -1) {
  8473. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8474. sd->pvp_timer = INVALID_TIMER;
  8475. }
  8476. return 0;
  8477. }
  8478. BUILDIN_FUNC(pvpoff)
  8479. {
  8480. int m;
  8481. const char *str;
  8482. str=script_getstr(st,2);
  8483. m = map_mapname2mapid(str);
  8484. if(m < 0 || !map[m].flag.pvp)
  8485. return 0; //fixed Lupus
  8486. map[m].flag.pvp = 0;
  8487. clif_send0199(m,0);
  8488. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8489. return 0;
  8490. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8491. return 0;
  8492. }
  8493. BUILDIN_FUNC(gvgon)
  8494. {
  8495. int m;
  8496. const char *str;
  8497. str=script_getstr(st,2);
  8498. m = map_mapname2mapid(str);
  8499. if(m >= 0 && !map[m].flag.gvg) {
  8500. map[m].flag.gvg = 1;
  8501. clif_send0199(m,3);
  8502. }
  8503. return 0;
  8504. }
  8505. BUILDIN_FUNC(gvgoff)
  8506. {
  8507. int m;
  8508. const char *str;
  8509. str=script_getstr(st,2);
  8510. m = map_mapname2mapid(str);
  8511. if(m >= 0 && map[m].flag.gvg) {
  8512. map[m].flag.gvg = 0;
  8513. clif_send0199(m,0);
  8514. }
  8515. return 0;
  8516. }
  8517. /*==========================================
  8518. * Shows an emoticon on top of the player/npc
  8519. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  8520. *------------------------------------------*/
  8521. //Optional second parameter added by [Skotlex]
  8522. BUILDIN_FUNC(emotion)
  8523. {
  8524. int type;
  8525. int player=0;
  8526. type=script_getnum(st,2);
  8527. if(type < 0 || type > 100)
  8528. return 0;
  8529. if( script_hasdata(st,3) )
  8530. player=script_getnum(st,3);
  8531. if (player) {
  8532. TBL_PC *sd = NULL;
  8533. if( script_hasdata(st,4) )
  8534. sd = map_nick2sd(script_getstr(st,4));
  8535. else
  8536. sd = script_rid2sd(st);
  8537. if (sd)
  8538. clif_emotion(&sd->bl,type);
  8539. } else
  8540. if( script_hasdata(st,4) )
  8541. {
  8542. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  8543. if(nd)
  8544. clif_emotion(&nd->bl,type);
  8545. }
  8546. else
  8547. clif_emotion(map_id2bl(st->oid),type);
  8548. return 0;
  8549. }
  8550. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8551. {
  8552. int m=va_arg(ap,int);
  8553. int g_id=va_arg(ap,int);
  8554. int flag=va_arg(ap,int);
  8555. if(!sd || sd->bl.m != m)
  8556. return 0;
  8557. if(
  8558. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8559. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8560. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8561. )
  8562. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  8563. return 1;
  8564. }
  8565. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8566. {
  8567. struct mob_data *md=(struct mob_data *)bl;
  8568. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8569. status_kill(bl);
  8570. return 0;
  8571. }
  8572. BUILDIN_FUNC(maprespawnguildid)
  8573. {
  8574. const char *mapname=script_getstr(st,2);
  8575. int g_id=script_getnum(st,3);
  8576. int flag=script_getnum(st,4);
  8577. int m=map_mapname2mapid(mapname);
  8578. if(m == -1)
  8579. return 0;
  8580. //Catch ALL players (in case some are 'between maps' on execution time)
  8581. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8582. if (flag&4) //Remove script mobs.
  8583. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8584. return 0;
  8585. }
  8586. BUILDIN_FUNC(agitstart)
  8587. {
  8588. if(agit_flag==1) return 0; // Agit already Start.
  8589. agit_flag=1;
  8590. guild_agit_start();
  8591. return 0;
  8592. }
  8593. BUILDIN_FUNC(agitend)
  8594. {
  8595. if(agit_flag==0) return 0; // Agit already End.
  8596. agit_flag=0;
  8597. guild_agit_end();
  8598. return 0;
  8599. }
  8600. BUILDIN_FUNC(agitstart2)
  8601. {
  8602. if(agit2_flag==1) return 0; // Agit2 already Start.
  8603. agit2_flag=1;
  8604. guild_agit2_start();
  8605. return 0;
  8606. }
  8607. BUILDIN_FUNC(agitend2)
  8608. {
  8609. if(agit2_flag==0) return 0; // Agit2 already End.
  8610. agit2_flag=0;
  8611. guild_agit2_end();
  8612. return 0;
  8613. }
  8614. /*==========================================
  8615. * Returns whether woe is on or off. // choice script
  8616. *------------------------------------------*/
  8617. BUILDIN_FUNC(agitcheck)
  8618. {
  8619. script_pushint(st,agit_flag);
  8620. return 0;
  8621. }
  8622. /*==========================================
  8623. * Returns whether woese is on or off. // choice script
  8624. *------------------------------------------*/
  8625. BUILDIN_FUNC(agitcheck2)
  8626. {
  8627. script_pushint(st,agit2_flag);
  8628. return 0;
  8629. }
  8630. /// Sets the guild_id of this npc.
  8631. ///
  8632. /// flagemblem <guild_id>;
  8633. BUILDIN_FUNC(flagemblem)
  8634. {
  8635. TBL_NPC* nd;
  8636. int g_id=script_getnum(st,2);
  8637. if(g_id < 0) return 0;
  8638. nd = (TBL_NPC*)map_id2nd(st->oid);
  8639. if( nd == NULL )
  8640. {
  8641. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8642. }
  8643. else if( nd->subtype != SCRIPT )
  8644. {
  8645. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8646. }
  8647. else
  8648. {
  8649. nd->u.scr.guild_id = g_id;
  8650. clif_guild_emblem_area(&nd->bl);
  8651. }
  8652. return 0;
  8653. }
  8654. BUILDIN_FUNC(getcastlename)
  8655. {
  8656. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8657. struct guild_castle* gc = guild_mapname2gc(mapname);
  8658. const char* name = (gc) ? gc->castle_name : "";
  8659. script_pushstrcopy(st,name);
  8660. return 0;
  8661. }
  8662. BUILDIN_FUNC(getcastledata)
  8663. {
  8664. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8665. int index = script_getnum(st,3);
  8666. struct guild_castle* gc = guild_mapname2gc(mapname);
  8667. if(script_hasdata(st,4) && index==0 && gc) {
  8668. const char* event = script_getstr(st,4);
  8669. check_event(st, event);
  8670. guild_addcastleinfoevent(gc->castle_id,17,event);
  8671. }
  8672. if(gc){
  8673. switch(index){
  8674. case 0: {
  8675. int i;
  8676. for(i=1;i<18;i++) // Initialize[AgitInit]
  8677. guild_castledataload(gc->castle_id,i);
  8678. } break;
  8679. case 1:
  8680. script_pushint(st,gc->guild_id); break;
  8681. case 2:
  8682. script_pushint(st,gc->economy); break;
  8683. case 3:
  8684. script_pushint(st,gc->defense); break;
  8685. case 4:
  8686. script_pushint(st,gc->triggerE); break;
  8687. case 5:
  8688. script_pushint(st,gc->triggerD); break;
  8689. case 6:
  8690. script_pushint(st,gc->nextTime); break;
  8691. case 7:
  8692. script_pushint(st,gc->payTime); break;
  8693. case 8:
  8694. script_pushint(st,gc->createTime); break;
  8695. case 9:
  8696. script_pushint(st,gc->visibleC); break;
  8697. case 10:
  8698. case 11:
  8699. case 12:
  8700. case 13:
  8701. case 14:
  8702. case 15:
  8703. case 16:
  8704. case 17:
  8705. script_pushint(st,gc->guardian[index-10].visible); break;
  8706. default:
  8707. script_pushint(st,0); break;
  8708. }
  8709. return 0;
  8710. }
  8711. script_pushint(st,0);
  8712. return 0;
  8713. }
  8714. BUILDIN_FUNC(setcastledata)
  8715. {
  8716. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8717. int index = script_getnum(st,3);
  8718. int value = script_getnum(st,4);
  8719. struct guild_castle* gc = guild_mapname2gc(mapname);
  8720. if(gc) {
  8721. // Save Data byself First
  8722. switch(index){
  8723. case 1:
  8724. gc->guild_id = value; break;
  8725. case 2:
  8726. gc->economy = value; break;
  8727. case 3:
  8728. gc->defense = value; break;
  8729. case 4:
  8730. gc->triggerE = value; break;
  8731. case 5:
  8732. gc->triggerD = value; break;
  8733. case 6:
  8734. gc->nextTime = value; break;
  8735. case 7:
  8736. gc->payTime = value; break;
  8737. case 8:
  8738. gc->createTime = value; break;
  8739. case 9:
  8740. gc->visibleC = value; break;
  8741. case 10:
  8742. case 11:
  8743. case 12:
  8744. case 13:
  8745. case 14:
  8746. case 15:
  8747. case 16:
  8748. case 17:
  8749. gc->guardian[index-10].visible = value; break;
  8750. default:
  8751. return 0;
  8752. }
  8753. guild_castledatasave(gc->castle_id,index,value);
  8754. }
  8755. return 0;
  8756. }
  8757. /* =====================================================================
  8758. * ギルド情報を要求する
  8759. * ---------------------------------------------------------------------*/
  8760. BUILDIN_FUNC(requestguildinfo)
  8761. {
  8762. int guild_id=script_getnum(st,2);
  8763. const char *event=NULL;
  8764. if( script_hasdata(st,3) ){
  8765. event=script_getstr(st,3);
  8766. check_event(st, event);
  8767. }
  8768. if(guild_id>0)
  8769. guild_npc_request_info(guild_id,event);
  8770. return 0;
  8771. }
  8772. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8773. /// getequipcardcnt(<equipment slot>);
  8774. BUILDIN_FUNC(getequipcardcnt)
  8775. {
  8776. int i=-1,j,num;
  8777. TBL_PC *sd;
  8778. int count;
  8779. num=script_getnum(st,2);
  8780. sd=script_rid2sd(st);
  8781. if (num > 0 && num <= ARRAYLENGTH(equip))
  8782. i=pc_checkequip(sd,equip[num-1]);
  8783. if (i < 0 || !sd->inventory_data[i]) {
  8784. script_pushint(st,0);
  8785. return 0;
  8786. }
  8787. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8788. {
  8789. script_pushint(st,0);
  8790. return 0;
  8791. }
  8792. count = 0;
  8793. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  8794. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8795. count++;
  8796. script_pushint(st,count);
  8797. return 0;
  8798. }
  8799. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8800. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8801. /// successremovecards <slot>;
  8802. BUILDIN_FUNC(successremovecards)
  8803. {
  8804. int i=-1,j,c,cardflag=0;
  8805. TBL_PC* sd = script_rid2sd(st);
  8806. int num = script_getnum(st,2);
  8807. if (num > 0 && num <= ARRAYLENGTH(equip))
  8808. i=pc_checkequip(sd,equip[num-1]);
  8809. if (i < 0 || !sd->inventory_data[i]) {
  8810. return 0;
  8811. }
  8812. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8813. return 0;
  8814. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8815. {
  8816. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8817. {// extract this card from the item
  8818. int flag;
  8819. struct item item_tmp;
  8820. cardflag = 1;
  8821. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8822. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8823. item_tmp.attribute=0,item_tmp.expire_time=0;
  8824. for (j = 0; j < MAX_SLOTS; j++)
  8825. item_tmp.card[j]=0;
  8826. //Logs items, got from (N)PC scripts [Lupus]
  8827. if(log_config.enable_logs&0x40)
  8828. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8829. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8830. clif_additem(sd,0,0,flag);
  8831. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8832. }
  8833. }
  8834. }
  8835. if(cardflag == 1)
  8836. { // カードを取り除いたアイテム所得
  8837. int flag;
  8838. struct item item_tmp;
  8839. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8840. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8841. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8842. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  8843. item_tmp.card[j]=0;
  8844. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  8845. item_tmp.card[j]=sd->status.inventory[i].card[j];
  8846. //Logs items, got from (N)PC scripts [Lupus]
  8847. if(log_config.enable_logs&0x40)
  8848. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8849. pc_delitem(sd,i,1,0,3);
  8850. //Logs items, got from (N)PC scripts [Lupus]
  8851. if(log_config.enable_logs&0x40)
  8852. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8853. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8854. clif_additem(sd,0,0,flag);
  8855. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8856. }
  8857. clif_misceffect(&sd->bl,3);
  8858. }
  8859. return 0;
  8860. }
  8861. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8862. /// failedremovecards <slot>, <type>;
  8863. /// <type>=0 : will destroy both the item and the cards.
  8864. /// <type>=1 : will keep the item, but destroy the cards.
  8865. /// <type>=2 : will keep the cards, but destroy the item.
  8866. /// <type>=? : will just display the failure effect.
  8867. BUILDIN_FUNC(failedremovecards)
  8868. {
  8869. int i=-1,j,c,cardflag=0;
  8870. TBL_PC* sd = script_rid2sd(st);
  8871. int num = script_getnum(st,2);
  8872. int typefail = script_getnum(st,3);
  8873. if (num > 0 && num <= ARRAYLENGTH(equip))
  8874. i=pc_checkequip(sd,equip[num-1]);
  8875. if (i < 0 || !sd->inventory_data[i])
  8876. return 0;
  8877. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8878. return 0;
  8879. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8880. {
  8881. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8882. {
  8883. cardflag = 1;
  8884. if(typefail == 2)
  8885. {// add cards to inventory, clear
  8886. int flag;
  8887. struct item item_tmp;
  8888. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8889. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8890. item_tmp.attribute=0,item_tmp.expire_time=0;
  8891. for (j = 0; j < MAX_SLOTS; j++)
  8892. item_tmp.card[j]=0;
  8893. //Logs items, got from (N)PC scripts [Lupus]
  8894. if(log_config.enable_logs&0x40)
  8895. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8896. if((flag=pc_additem(sd,&item_tmp,1))){
  8897. clif_additem(sd,0,0,flag);
  8898. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8899. }
  8900. }
  8901. }
  8902. }
  8903. if(cardflag == 1)
  8904. {
  8905. if(typefail == 0 || typefail == 2){ // 武具損失
  8906. //Logs items, got from (N)PC scripts [Lupus]
  8907. if(log_config.enable_logs&0x40)
  8908. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8909. pc_delitem(sd,i,1,0,2);
  8910. }
  8911. if(typefail == 1){ // カードのみ損失(武具を返す)
  8912. int flag;
  8913. struct item item_tmp;
  8914. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8915. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8916. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8917. //Logs items, got from (N)PC scripts [Lupus]
  8918. if(log_config.enable_logs&0x40)
  8919. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8920. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  8921. item_tmp.card[j]=0;
  8922. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  8923. item_tmp.card[j]=sd->status.inventory[i].card[j];
  8924. pc_delitem(sd,i,1,0,2);
  8925. //Logs items, got from (N)PC scripts [Lupus]
  8926. if(log_config.enable_logs&0x40)
  8927. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8928. if((flag=pc_additem(sd,&item_tmp,1))){
  8929. clif_additem(sd,0,0,flag);
  8930. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8931. }
  8932. }
  8933. clif_misceffect(&sd->bl,2);
  8934. }
  8935. return 0;
  8936. }
  8937. /* ================================================================
  8938. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8939. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8940. * improved by [Lance]
  8941. * ================================================================*/
  8942. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8943. {
  8944. int x,y,m,check_val=0,check_ID=0,i=0;
  8945. struct guild *g = NULL;
  8946. struct party_data *p = NULL;
  8947. const char *str;
  8948. const char *mapname;
  8949. unsigned int index;
  8950. mapname=script_getstr(st,2);
  8951. str=script_getstr(st,3);
  8952. x=script_getnum(st,4);
  8953. y=script_getnum(st,5);
  8954. if(script_hasdata(st,7)){
  8955. check_val=script_getnum(st,6);
  8956. check_ID=script_getnum(st,7);
  8957. }
  8958. if((m=map_mapname2mapid(mapname))< 0)
  8959. return 0;
  8960. if(!(index=mapindex_name2id(str)))
  8961. return 0;
  8962. switch(check_val){
  8963. case 1:
  8964. g = guild_search(check_ID);
  8965. if (g){
  8966. for( i=0; i < g->max_member; i++)
  8967. {
  8968. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8969. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  8970. }
  8971. }
  8972. }
  8973. break;
  8974. case 2:
  8975. p = party_search(check_ID);
  8976. if(p){
  8977. for(i=0;i<MAX_PARTY; i++){
  8978. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8979. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  8980. }
  8981. }
  8982. }
  8983. break;
  8984. default:
  8985. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8986. break;
  8987. }
  8988. return 0;
  8989. }
  8990. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8991. {
  8992. char *event=va_arg(ap,char *);
  8993. struct mob_data *md = ((struct mob_data *)bl);
  8994. if(strcmp(event,md->npc_event)==0 && md->status.hp > 0)
  8995. return 1;
  8996. return 0;
  8997. }
  8998. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8999. {
  9000. const char *mapname,*event;
  9001. int m;
  9002. mapname=script_getstr(st,2);
  9003. event=script_getstr(st,3);
  9004. check_event(st, event);
  9005. if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9006. script_pushint(st,-1);
  9007. return 0;
  9008. }
  9009. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9010. {
  9011. script_pushint(st,-1);
  9012. return 0;
  9013. }
  9014. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9015. return 0;
  9016. }
  9017. BUILDIN_FUNC(marriage)
  9018. {
  9019. const char *partner=script_getstr(st,2);
  9020. TBL_PC *sd=script_rid2sd(st);
  9021. TBL_PC *p_sd=map_nick2sd(partner);
  9022. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9023. script_pushint(st,0);
  9024. return 0;
  9025. }
  9026. script_pushint(st,1);
  9027. return 0;
  9028. }
  9029. BUILDIN_FUNC(wedding_effect)
  9030. {
  9031. TBL_PC *sd=script_rid2sd(st);
  9032. struct block_list *bl;
  9033. if(sd==NULL) {
  9034. bl=map_id2bl(st->oid);
  9035. } else
  9036. bl=&sd->bl;
  9037. clif_wedding_effect(bl);
  9038. return 0;
  9039. }
  9040. BUILDIN_FUNC(divorce)
  9041. {
  9042. TBL_PC *sd=script_rid2sd(st);
  9043. if(sd==NULL || pc_divorce(sd) < 0){
  9044. script_pushint(st,0);
  9045. return 0;
  9046. }
  9047. script_pushint(st,1);
  9048. return 0;
  9049. }
  9050. BUILDIN_FUNC(ispartneron)
  9051. {
  9052. TBL_PC *sd=script_rid2sd(st);
  9053. if(sd==NULL || !pc_ismarried(sd) ||
  9054. map_charid2sd(sd->status.partner_id) == NULL) {
  9055. script_pushint(st,0);
  9056. return 0;
  9057. }
  9058. script_pushint(st,1);
  9059. return 0;
  9060. }
  9061. BUILDIN_FUNC(getpartnerid)
  9062. {
  9063. TBL_PC *sd=script_rid2sd(st);
  9064. if (sd == NULL) {
  9065. script_pushint(st,0);
  9066. return 0;
  9067. }
  9068. script_pushint(st,sd->status.partner_id);
  9069. return 0;
  9070. }
  9071. BUILDIN_FUNC(getchildid)
  9072. {
  9073. TBL_PC *sd=script_rid2sd(st);
  9074. if (sd == NULL) {
  9075. script_pushint(st,0);
  9076. return 0;
  9077. }
  9078. script_pushint(st,sd->status.child);
  9079. return 0;
  9080. }
  9081. BUILDIN_FUNC(getmotherid)
  9082. {
  9083. TBL_PC *sd=script_rid2sd(st);
  9084. if (sd == NULL) {
  9085. script_pushint(st,0);
  9086. return 0;
  9087. }
  9088. script_pushint(st,sd->status.mother);
  9089. return 0;
  9090. }
  9091. BUILDIN_FUNC(getfatherid)
  9092. {
  9093. TBL_PC *sd=script_rid2sd(st);
  9094. if (sd == NULL) {
  9095. script_pushint(st,0);
  9096. return 0;
  9097. }
  9098. script_pushint(st,sd->status.father);
  9099. return 0;
  9100. }
  9101. BUILDIN_FUNC(warppartner)
  9102. {
  9103. int x,y;
  9104. unsigned short mapindex;
  9105. const char *str;
  9106. TBL_PC *sd=script_rid2sd(st);
  9107. TBL_PC *p_sd=NULL;
  9108. if(sd==NULL || !pc_ismarried(sd) ||
  9109. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9110. script_pushint(st,0);
  9111. return 0;
  9112. }
  9113. str=script_getstr(st,2);
  9114. x=script_getnum(st,3);
  9115. y=script_getnum(st,4);
  9116. mapindex = mapindex_name2id(str);
  9117. if (mapindex) {
  9118. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  9119. script_pushint(st,1);
  9120. } else
  9121. script_pushint(st,0);
  9122. return 0;
  9123. }
  9124. /*================================================
  9125. * Script for Displaying MOB Information [Valaris]
  9126. *------------------------------------------------*/
  9127. BUILDIN_FUNC(strmobinfo)
  9128. {
  9129. int num=script_getnum(st,2);
  9130. int class_=script_getnum(st,3);
  9131. if(!mobdb_checkid(class_))
  9132. {
  9133. script_pushint(st,0);
  9134. return 0;
  9135. }
  9136. switch (num) {
  9137. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9138. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9139. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9140. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9141. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9142. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9143. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9144. default:
  9145. script_pushint(st,0);
  9146. break;
  9147. }
  9148. return 0;
  9149. }
  9150. /*==========================================
  9151. * Summon guardians [Valaris]
  9152. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9153. *------------------------------------------*/
  9154. BUILDIN_FUNC(guardian)
  9155. {
  9156. int class_=0,x=0,y=0,guardian=0;
  9157. const char *str,*map,*evt="";
  9158. struct script_data *data;
  9159. bool has_index = false;
  9160. map =script_getstr(st,2);
  9161. x =script_getnum(st,3);
  9162. y =script_getnum(st,4);
  9163. str =script_getstr(st,5);
  9164. class_=script_getnum(st,6);
  9165. if( script_hasdata(st,8) )
  9166. {// "<event label>",<guardian index>
  9167. evt=script_getstr(st,7);
  9168. guardian=script_getnum(st,8);
  9169. has_index = true;
  9170. } else if( script_hasdata(st,7) ){
  9171. data=script_getdata(st,7);
  9172. get_val(st,data);
  9173. if( data_isstring(data) )
  9174. {// "<event label>"
  9175. evt=script_getstr(st,7);
  9176. } else if( data_isint(data) )
  9177. {// <guardian index>
  9178. guardian=script_getnum(st,7);
  9179. has_index = true;
  9180. } else {
  9181. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9182. script_reportdata(data);
  9183. return 1;
  9184. }
  9185. }
  9186. check_event(st, evt);
  9187. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9188. return 0;
  9189. }
  9190. /*==========================================
  9191. * Invisible Walls [Zephyrus]
  9192. *------------------------------------------*/
  9193. BUILDIN_FUNC(setwall)
  9194. {
  9195. const char *map, *name;
  9196. int x, y, m, size, dir;
  9197. bool shootable;
  9198. map = script_getstr(st,2);
  9199. x = script_getnum(st,3);
  9200. y = script_getnum(st,4);
  9201. size = script_getnum(st,5);
  9202. dir = script_getnum(st,6);
  9203. shootable = script_getnum(st,7);
  9204. name = script_getstr(st,8);
  9205. if( (m = map_mapname2mapid(map)) < 0 )
  9206. return 0; // Invalid Map
  9207. map_iwall_set(m, x, y, size, dir, shootable, name);
  9208. return 0;
  9209. }
  9210. BUILDIN_FUNC(delwall)
  9211. {
  9212. const char *name = script_getstr(st,2);
  9213. map_iwall_remove(name);
  9214. return 0;
  9215. }
  9216. /// Retrieves various information about the specified guardian.
  9217. ///
  9218. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9219. /// type: 0 - whether it is deployed or not
  9220. /// 1 - maximum hp
  9221. /// 2 - current hp
  9222. ///
  9223. BUILDIN_FUNC(guardianinfo)
  9224. {
  9225. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9226. int id = script_getnum(st,3);
  9227. int type = script_getnum(st,4);
  9228. struct guild_castle* gc = guild_mapname2gc(mapname);
  9229. struct mob_data* gd;
  9230. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9231. {
  9232. script_pushint(st,-1);
  9233. return 0;
  9234. }
  9235. if( type == 0 )
  9236. script_pushint(st, gc->guardian[id].visible);
  9237. else
  9238. if( !gc->guardian[id].visible )
  9239. script_pushint(st,-1);
  9240. else
  9241. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9242. script_pushint(st,-1);
  9243. else
  9244. {
  9245. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9246. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9247. else
  9248. script_pushint(st,-1);
  9249. }
  9250. return 0;
  9251. }
  9252. /*==========================================
  9253. * IDからItem名
  9254. *------------------------------------------*/
  9255. BUILDIN_FUNC(getitemname)
  9256. {
  9257. int item_id=0;
  9258. struct item_data *i_data;
  9259. char *item_name;
  9260. struct script_data *data;
  9261. data=script_getdata(st,2);
  9262. get_val(st,data);
  9263. if( data_isstring(data) ){
  9264. const char *name=conv_str(st,data);
  9265. struct item_data *item_data = itemdb_searchname(name);
  9266. if( item_data )
  9267. item_id=item_data->nameid;
  9268. }else
  9269. item_id=conv_num(st,data);
  9270. i_data = itemdb_exists(item_id);
  9271. if (i_data == NULL)
  9272. {
  9273. script_pushconststr(st,"null");
  9274. return 0;
  9275. }
  9276. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9277. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9278. script_pushstr(st,item_name);
  9279. return 0;
  9280. }
  9281. /*==========================================
  9282. * Returns number of slots an item has. [Skotlex]
  9283. *------------------------------------------*/
  9284. BUILDIN_FUNC(getitemslots)
  9285. {
  9286. int item_id;
  9287. struct item_data *i_data;
  9288. item_id=script_getnum(st,2);
  9289. i_data = itemdb_exists(item_id);
  9290. if (i_data)
  9291. script_pushint(st,i_data->slot);
  9292. else
  9293. script_pushint(st,-1);
  9294. return 0;
  9295. }
  9296. /*==========================================
  9297. * Returns some values of an item [Lupus]
  9298. * Price, Weight, etc...
  9299. getiteminfo(itemID,n), where n
  9300. 0 value_buy;
  9301. 1 value_sell;
  9302. 2 type;
  9303. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9304. if = 0, then monsters don't drop it at all (rare or a quest item)
  9305. if = 10000, then this item is sold in NPC shops only
  9306. 4 sex;
  9307. 5 equip;
  9308. 6 weight;
  9309. 7 atk;
  9310. 8 def;
  9311. 9 range;
  9312. 10 slot;
  9313. 11 look;
  9314. 12 elv;
  9315. 13 wlv;
  9316. 14 view id
  9317. *------------------------------------------*/
  9318. BUILDIN_FUNC(getiteminfo)
  9319. {
  9320. int item_id,n;
  9321. int *item_arr;
  9322. struct item_data *i_data;
  9323. item_id = script_getnum(st,2);
  9324. n = script_getnum(st,3);
  9325. i_data = itemdb_exists(item_id);
  9326. if (i_data && n>=0 && n<=14) {
  9327. item_arr = (int*)&i_data->value_buy;
  9328. script_pushint(st,item_arr[n]);
  9329. } else
  9330. script_pushint(st,-1);
  9331. return 0;
  9332. }
  9333. /*==========================================
  9334. * Set some values of an item [Lupus]
  9335. * Price, Weight, etc...
  9336. setiteminfo(itemID,n,Value), where n
  9337. 0 value_buy;
  9338. 1 value_sell;
  9339. 2 type;
  9340. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9341. if = 0, then monsters don't drop it at all (rare or a quest item)
  9342. if = 10000, then this item is sold in NPC shops only
  9343. 4 sex;
  9344. 5 equip;
  9345. 6 weight;
  9346. 7 atk;
  9347. 8 def;
  9348. 9 range;
  9349. 10 slot;
  9350. 11 look;
  9351. 12 elv;
  9352. 13 wlv;
  9353. 14 view id
  9354. * Returns Value or -1 if the wrong field's been set
  9355. *------------------------------------------*/
  9356. BUILDIN_FUNC(setiteminfo)
  9357. {
  9358. int item_id,n,value;
  9359. int *item_arr;
  9360. struct item_data *i_data;
  9361. item_id = script_getnum(st,2);
  9362. n = script_getnum(st,3);
  9363. value = script_getnum(st,4);
  9364. i_data = itemdb_exists(item_id);
  9365. if (i_data && n>=0 && n<=14) {
  9366. item_arr = (int*)&i_data->value_buy;
  9367. item_arr[n] = value;
  9368. script_pushint(st,value);
  9369. } else
  9370. script_pushint(st,-1);
  9371. return 0;
  9372. }
  9373. /*==========================================
  9374. * Returns value from equipped item slot n [Lupus]
  9375. getequipcardid(num,slot)
  9376. where
  9377. num = eqip position slot
  9378. slot = 0,1,2,3 (Card Slot N)
  9379. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9380. it's useful when you want to check item cards or if it's signed
  9381. Useful for such quests as "Sign this refined item with players name" etc
  9382. Hat[0] +4 -> Player's Hat[0] +4
  9383. *------------------------------------------*/
  9384. BUILDIN_FUNC(getequipcardid)
  9385. {
  9386. int i=-1,num,slot;
  9387. TBL_PC *sd;
  9388. num=script_getnum(st,2);
  9389. slot=script_getnum(st,3);
  9390. sd=script_rid2sd(st);
  9391. if (num > 0 && num <= ARRAYLENGTH(equip))
  9392. i=pc_checkequip(sd,equip[num-1]);
  9393. if(i >= 0 && slot>=0 && slot<4)
  9394. script_pushint(st,sd->status.inventory[i].card[slot]);
  9395. else
  9396. script_pushint(st,0);
  9397. return 0;
  9398. }
  9399. /*==========================================
  9400. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9401. *------------------------------------------*/
  9402. BUILDIN_FUNC(petskillbonus)
  9403. {
  9404. struct pet_data *pd;
  9405. TBL_PC *sd=script_rid2sd(st);
  9406. if(sd==NULL || sd->pd==NULL)
  9407. return 0;
  9408. pd=sd->pd;
  9409. if (pd->bonus)
  9410. { //Clear previous bonus
  9411. if (pd->bonus->timer != -1)
  9412. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9413. } else //init
  9414. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9415. pd->bonus->type=script_getnum(st,2);
  9416. pd->bonus->val=script_getnum(st,3);
  9417. pd->bonus->duration=script_getnum(st,4);
  9418. pd->bonus->delay=script_getnum(st,5);
  9419. if (pd->state.skillbonus == 1)
  9420. pd->state.skillbonus=0; // waiting state
  9421. // wait for timer to start
  9422. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9423. pd->bonus->timer = INVALID_TIMER;
  9424. else
  9425. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9426. return 0;
  9427. }
  9428. /*==========================================
  9429. * pet looting [Valaris] //Rewritten by [Skotlex]
  9430. *------------------------------------------*/
  9431. BUILDIN_FUNC(petloot)
  9432. {
  9433. int max;
  9434. struct pet_data *pd;
  9435. TBL_PC *sd=script_rid2sd(st);
  9436. if(sd==NULL || sd->pd==NULL)
  9437. return 0;
  9438. max=script_getnum(st,2);
  9439. if(max < 1)
  9440. max = 1; //Let'em loot at least 1 item.
  9441. else if (max > MAX_PETLOOT_SIZE)
  9442. max = MAX_PETLOOT_SIZE;
  9443. pd = sd->pd;
  9444. if (pd->loot != NULL)
  9445. { //Release whatever was there already and reallocate memory
  9446. pet_lootitem_drop(pd, pd->msd);
  9447. aFree(pd->loot->item);
  9448. }
  9449. else
  9450. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9451. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9452. pd->loot->max=max;
  9453. pd->loot->count = 0;
  9454. pd->loot->weight = 0;
  9455. return 0;
  9456. }
  9457. /*==========================================
  9458. * PCの所持品情報読み取り
  9459. *------------------------------------------*/
  9460. BUILDIN_FUNC(getinventorylist)
  9461. {
  9462. TBL_PC *sd=script_rid2sd(st);
  9463. char card_var[NAME_LENGTH];
  9464. int i,j=0,k;
  9465. if(!sd) return 0;
  9466. for(i=0;i<MAX_INVENTORY;i++){
  9467. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9468. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9469. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9470. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9471. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9472. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9473. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9474. for (k = 0; k < MAX_SLOTS; k++)
  9475. {
  9476. sprintf(card_var, "@inventorylist_card%d",k+1);
  9477. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9478. }
  9479. pc_setreg(sd,add_str("@inventorylist_expire")+(j<<24),sd->status.inventory[i].expire_time);
  9480. j++;
  9481. }
  9482. }
  9483. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9484. return 0;
  9485. }
  9486. BUILDIN_FUNC(getskilllist)
  9487. {
  9488. TBL_PC *sd=script_rid2sd(st);
  9489. int i,j=0;
  9490. if(!sd) return 0;
  9491. for(i=0;i<MAX_SKILL;i++){
  9492. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9493. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9494. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9495. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9496. j++;
  9497. }
  9498. }
  9499. pc_setreg(sd,add_str("@skilllist_count"),j);
  9500. return 0;
  9501. }
  9502. BUILDIN_FUNC(clearitem)
  9503. {
  9504. TBL_PC *sd=script_rid2sd(st);
  9505. int i;
  9506. if(sd==NULL) return 0;
  9507. for (i=0; i<MAX_INVENTORY; i++) {
  9508. if (sd->status.inventory[i].amount) {
  9509. //Logs items, got from (N)PC scripts [Lupus]
  9510. if(log_config.enable_logs&0x40)
  9511. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9512. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0);
  9513. }
  9514. }
  9515. return 0;
  9516. }
  9517. /*==========================================
  9518. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9519. *------------------------------------------*/
  9520. BUILDIN_FUNC(disguise)
  9521. {
  9522. int id;
  9523. TBL_PC* sd = script_rid2sd(st);
  9524. if (sd == NULL) return 0;
  9525. id = script_getnum(st,2);
  9526. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9527. pc_disguise(sd, id);
  9528. script_pushint(st,id);
  9529. } else
  9530. script_pushint(st,0);
  9531. return 0;
  9532. }
  9533. /*==========================================
  9534. * Undisguise Player (returns 1 if success, 0 on fail)
  9535. *------------------------------------------*/
  9536. BUILDIN_FUNC(undisguise)
  9537. {
  9538. TBL_PC* sd = script_rid2sd(st);
  9539. if (sd == NULL) return 0;
  9540. if (sd->disguise) {
  9541. pc_disguise(sd, 0);
  9542. script_pushint(st,0);
  9543. } else {
  9544. script_pushint(st,1);
  9545. }
  9546. return 0;
  9547. }
  9548. /*==========================================
  9549. * NPCクラスチェンジ
  9550. * classは変わりたいclass
  9551. * typeは通常0なのかな?
  9552. *------------------------------------------*/
  9553. BUILDIN_FUNC(classchange)
  9554. {
  9555. int _class,type;
  9556. struct block_list *bl=map_id2bl(st->oid);
  9557. if(bl==NULL) return 0;
  9558. _class=script_getnum(st,2);
  9559. type=script_getnum(st,3);
  9560. clif_class_change(bl,_class,type);
  9561. return 0;
  9562. }
  9563. /*==========================================
  9564. * NPCから発生するエフェクト
  9565. *------------------------------------------*/
  9566. BUILDIN_FUNC(misceffect)
  9567. {
  9568. int type;
  9569. type=script_getnum(st,2);
  9570. if(st->oid && st->oid != fake_nd->bl.id) {
  9571. struct block_list *bl = map_id2bl(st->oid);
  9572. if (bl)
  9573. clif_specialeffect(bl,type,AREA);
  9574. } else{
  9575. TBL_PC *sd=script_rid2sd(st);
  9576. if(sd)
  9577. clif_specialeffect(&sd->bl,type,AREA);
  9578. }
  9579. return 0;
  9580. }
  9581. /*==========================================
  9582. * Play a BGM on a single client [Rikter/Yommy]
  9583. *------------------------------------------*/
  9584. BUILDIN_FUNC(playBGM)
  9585. {
  9586. const char* name;
  9587. struct map_session_data* sd;
  9588. if( ( sd = script_rid2sd(st) ) != NULL )
  9589. {
  9590. name = script_getstr(st,2);
  9591. clif_playBGM(sd, name);
  9592. }
  9593. return 0;
  9594. }
  9595. static int playBGM_sub(struct block_list* bl,va_list ap)
  9596. {
  9597. const char* name = va_arg(ap,const char*);
  9598. clif_playBGM(BL_CAST(BL_PC, bl), name);
  9599. return 0;
  9600. }
  9601. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  9602. {
  9603. const char* name = va_arg(args, const char*);
  9604. clif_playBGM(sd, name);
  9605. return 0;
  9606. }
  9607. /*==========================================
  9608. * Play a BGM on multiple client [Rikter/Yommy]
  9609. *------------------------------------------*/
  9610. BUILDIN_FUNC(playBGMall)
  9611. {
  9612. const char* name;
  9613. name = script_getstr(st,2);
  9614. if( script_hasdata(st,7) )
  9615. {// specified part of map
  9616. const char* map = script_getstr(st,3);
  9617. int x0 = script_getnum(st,4);
  9618. int y0 = script_getnum(st,5);
  9619. int x1 = script_getnum(st,6);
  9620. int y1 = script_getnum(st,7);
  9621. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  9622. }
  9623. else if( script_hasdata(st,3) )
  9624. {// entire map
  9625. const char* map = script_getstr(st,3);
  9626. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  9627. }
  9628. else
  9629. {// entire server
  9630. map_foreachpc(&playBGM_foreachpc_sub, name);
  9631. }
  9632. return 0;
  9633. }
  9634. /*==========================================
  9635. * サウンドエフェクト
  9636. *------------------------------------------*/
  9637. BUILDIN_FUNC(soundeffect)
  9638. {
  9639. TBL_PC* sd = script_rid2sd(st);
  9640. const char* name = script_getstr(st,2);
  9641. int type = script_getnum(st,3);
  9642. if(sd)
  9643. {
  9644. clif_soundeffect(sd,&sd->bl,name,type);
  9645. }
  9646. return 0;
  9647. }
  9648. int soundeffect_sub(struct block_list* bl,va_list ap)
  9649. {
  9650. char* name = va_arg(ap,char*);
  9651. int type = va_arg(ap,int);
  9652. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9653. return 0;
  9654. }
  9655. /*==========================================
  9656. * Play a sound effect (.wav) on multiple clients
  9657. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9658. *------------------------------------------*/
  9659. BUILDIN_FUNC(soundeffectall)
  9660. {
  9661. struct block_list* bl;
  9662. const char* name;
  9663. int type;
  9664. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9665. if (!bl)
  9666. return 0;
  9667. name = script_getstr(st,2);
  9668. type = script_getnum(st,3);
  9669. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9670. if(!script_hasdata(st,4))
  9671. { // area around
  9672. clif_soundeffectall(bl, name, type, AREA);
  9673. }
  9674. else
  9675. if(!script_hasdata(st,5))
  9676. { // entire map
  9677. const char* map = script_getstr(st,4);
  9678. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9679. }
  9680. else
  9681. if(script_hasdata(st,8))
  9682. { // specified part of map
  9683. const char* map = script_getstr(st,4);
  9684. int x0 = script_getnum(st,5);
  9685. int y0 = script_getnum(st,6);
  9686. int x1 = script_getnum(st,7);
  9687. int y1 = script_getnum(st,8);
  9688. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9689. }
  9690. else
  9691. {
  9692. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9693. }
  9694. return 0;
  9695. }
  9696. /*==========================================
  9697. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9698. *------------------------------------------*/
  9699. BUILDIN_FUNC(petrecovery)
  9700. {
  9701. struct pet_data *pd;
  9702. TBL_PC *sd=script_rid2sd(st);
  9703. if(sd==NULL || sd->pd==NULL)
  9704. return 0;
  9705. pd=sd->pd;
  9706. if (pd->recovery)
  9707. { //Halt previous bonus
  9708. if (pd->recovery->timer != -1)
  9709. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9710. } else //Init
  9711. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9712. pd->recovery->type = (sc_type)script_getnum(st,2);
  9713. pd->recovery->delay = script_getnum(st,3);
  9714. pd->recovery->timer = INVALID_TIMER;
  9715. return 0;
  9716. }
  9717. /*==========================================
  9718. * pet healing [Valaris] //Rewritten by [Skotlex]
  9719. *------------------------------------------*/
  9720. BUILDIN_FUNC(petheal)
  9721. {
  9722. struct pet_data *pd;
  9723. TBL_PC *sd=script_rid2sd(st);
  9724. if(sd==NULL || sd->pd==NULL)
  9725. return 0;
  9726. pd=sd->pd;
  9727. if (pd->s_skill)
  9728. { //Clear previous skill
  9729. if (pd->s_skill->timer != -1)
  9730. {
  9731. if (pd->s_skill->id)
  9732. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9733. else
  9734. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9735. }
  9736. } else //init memory
  9737. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9738. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9739. //Use the lv as the amount to heal
  9740. pd->s_skill->lv=script_getnum(st,2);
  9741. pd->s_skill->delay=script_getnum(st,3);
  9742. pd->s_skill->hp=script_getnum(st,4);
  9743. pd->s_skill->sp=script_getnum(st,5);
  9744. //Use delay as initial offset to avoid skill/heal exploits
  9745. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9746. pd->s_skill->timer = INVALID_TIMER;
  9747. else
  9748. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9749. return 0;
  9750. }
  9751. /*==========================================
  9752. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9753. *------------------------------------------*/
  9754. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  9755. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  9756. BUILDIN_FUNC(petskillattack)
  9757. {
  9758. struct pet_data *pd;
  9759. TBL_PC *sd=script_rid2sd(st);
  9760. if(sd==NULL || sd->pd==NULL)
  9761. return 0;
  9762. pd=sd->pd;
  9763. if (pd->a_skill == NULL)
  9764. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9765. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9766. pd->a_skill->lv=script_getnum(st,3);
  9767. pd->a_skill->div_ = 0;
  9768. pd->a_skill->rate=script_getnum(st,4);
  9769. pd->a_skill->bonusrate=script_getnum(st,5);
  9770. return 0;
  9771. }
  9772. /*==========================================
  9773. * pet attack skills [Valaris]
  9774. *------------------------------------------*/
  9775. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  9776. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  9777. BUILDIN_FUNC(petskillattack2)
  9778. {
  9779. struct pet_data *pd;
  9780. TBL_PC *sd=script_rid2sd(st);
  9781. if(sd==NULL || sd->pd==NULL)
  9782. return 0;
  9783. pd=sd->pd;
  9784. if (pd->a_skill == NULL)
  9785. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9786. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9787. pd->a_skill->lv=script_getnum(st,3);
  9788. pd->a_skill->div_ = script_getnum(st,4);
  9789. pd->a_skill->rate=script_getnum(st,5);
  9790. pd->a_skill->bonusrate=script_getnum(st,6);
  9791. return 0;
  9792. }
  9793. /*==========================================
  9794. * pet support skills [Skotlex]
  9795. *------------------------------------------*/
  9796. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  9797. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  9798. BUILDIN_FUNC(petskillsupport)
  9799. {
  9800. struct pet_data *pd;
  9801. TBL_PC *sd=script_rid2sd(st);
  9802. if(sd==NULL || sd->pd==NULL)
  9803. return 0;
  9804. pd=sd->pd;
  9805. if (pd->s_skill)
  9806. { //Clear previous skill
  9807. if (pd->s_skill->timer != -1)
  9808. {
  9809. if (pd->s_skill->id)
  9810. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9811. else
  9812. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9813. }
  9814. } else //init memory
  9815. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9816. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9817. pd->s_skill->lv=script_getnum(st,3);
  9818. pd->s_skill->delay=script_getnum(st,4);
  9819. pd->s_skill->hp=script_getnum(st,5);
  9820. pd->s_skill->sp=script_getnum(st,6);
  9821. //Use delay as initial offset to avoid skill/heal exploits
  9822. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9823. pd->s_skill->timer = INVALID_TIMER;
  9824. else
  9825. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9826. return 0;
  9827. }
  9828. /*==========================================
  9829. * Scripted skill effects [Celest]
  9830. *------------------------------------------*/
  9831. /// skilleffect <skill id>,<level>
  9832. /// skilleffect "<skill name>",<level>
  9833. BUILDIN_FUNC(skilleffect)
  9834. {
  9835. TBL_PC *sd;
  9836. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9837. int skilllv=script_getnum(st,3);
  9838. sd=script_rid2sd(st);
  9839. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9840. return 0;
  9841. }
  9842. /*==========================================
  9843. * NPC skill effects [Valaris]
  9844. *------------------------------------------*/
  9845. /// npcskilleffect <skill id>,<level>,<x>,<y>
  9846. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  9847. BUILDIN_FUNC(npcskilleffect)
  9848. {
  9849. struct block_list *bl= map_id2bl(st->oid);
  9850. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9851. int skilllv=script_getnum(st,3);
  9852. int x=script_getnum(st,4);
  9853. int y=script_getnum(st,5);
  9854. if (bl)
  9855. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9856. return 0;
  9857. }
  9858. /*==========================================
  9859. * Special effects [Valaris]
  9860. *------------------------------------------*/
  9861. BUILDIN_FUNC(specialeffect)
  9862. {
  9863. struct block_list *bl=map_id2bl(st->oid);
  9864. int type = script_getnum(st,2);
  9865. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9866. if(bl==NULL)
  9867. return 0;
  9868. if( script_hasdata(st,4) )
  9869. {
  9870. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9871. if(nd)
  9872. clif_specialeffect(&nd->bl, type, target);
  9873. }
  9874. else
  9875. clif_specialeffect(bl, type, target);
  9876. return 0;
  9877. }
  9878. BUILDIN_FUNC(specialeffect2)
  9879. {
  9880. TBL_PC *sd=script_rid2sd(st);
  9881. int type = script_getnum(st,2);
  9882. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9883. if( script_hasdata(st,4) )
  9884. sd = map_nick2sd(script_getstr(st,4));
  9885. if (sd)
  9886. clif_specialeffect(&sd->bl, type, target);
  9887. return 0;
  9888. }
  9889. /*==========================================
  9890. * Nude [Valaris]
  9891. *------------------------------------------*/
  9892. BUILDIN_FUNC(nude)
  9893. {
  9894. TBL_PC *sd=script_rid2sd(st);
  9895. int i,calcflag=0;
  9896. if(sd==NULL)
  9897. return 0;
  9898. for(i=0;i<11;i++)
  9899. if(sd->equip_index[i] >= 0) {
  9900. if(!calcflag)
  9901. calcflag=1;
  9902. pc_unequipitem(sd,sd->equip_index[i],2);
  9903. }
  9904. if(calcflag)
  9905. status_calc_pc(sd,0);
  9906. return 0;
  9907. }
  9908. /*==========================================
  9909. * gmcommand [MouseJstr]
  9910. *
  9911. * suggested on the forums...
  9912. * splitted into atcommand & charcommand by [Skotlex]
  9913. *------------------------------------------*/
  9914. BUILDIN_FUNC(atcommand)
  9915. {
  9916. TBL_PC dummy_sd;
  9917. TBL_PC* sd;
  9918. int fd;
  9919. const char* cmd;
  9920. cmd = script_getstr(st,2);
  9921. if (st->rid) {
  9922. sd = script_rid2sd(st);
  9923. fd = sd->fd;
  9924. } else { //Use a dummy character.
  9925. sd = &dummy_sd;
  9926. fd = 0;
  9927. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9928. if (st->oid)
  9929. {
  9930. struct block_list* bl = map_id2bl(st->oid);
  9931. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9932. if (bl->type == BL_NPC)
  9933. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9934. }
  9935. }
  9936. // compatibility with previous implementation (deprecated!)
  9937. if(cmd[0] != atcommand_symbol)
  9938. {
  9939. cmd += strlen(sd->status.name);
  9940. while(*cmd != atcommand_symbol && *cmd != 0)
  9941. cmd++;
  9942. }
  9943. is_atcommand(fd, sd, cmd, 0);
  9944. return 0;
  9945. }
  9946. BUILDIN_FUNC(charcommand)
  9947. {
  9948. TBL_PC dummy_sd;
  9949. TBL_PC* sd;
  9950. int fd;
  9951. const char* cmd;
  9952. cmd = script_getstr(st,2);
  9953. if (st->rid) {
  9954. sd = script_rid2sd(st);
  9955. fd = sd->fd;
  9956. } else { //Use a dummy character.
  9957. sd = &dummy_sd;
  9958. fd = 0;
  9959. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9960. if (st->oid)
  9961. {
  9962. struct block_list* bl = map_id2bl(st->oid);
  9963. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9964. if (bl->type == BL_NPC)
  9965. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9966. }
  9967. }
  9968. if (*cmd != charcommand_symbol) {
  9969. ShowWarning("script: buildin_charcommand: No '#' symbol!\n");
  9970. script_reportsrc(st);
  9971. return 1;
  9972. }
  9973. is_atcommand(fd, sd, cmd, 0);
  9974. return 0;
  9975. }
  9976. /*==========================================
  9977. * Displays a message for the player only (like system messages like "you got an apple" )
  9978. *------------------------------------------*/
  9979. BUILDIN_FUNC(dispbottom)
  9980. {
  9981. TBL_PC *sd=script_rid2sd(st);
  9982. const char *message;
  9983. message=script_getstr(st,2);
  9984. if(sd)
  9985. clif_disp_onlyself(sd,message,(int)strlen(message));
  9986. return 0;
  9987. }
  9988. /*==========================================
  9989. * All The Players Full Recovery
  9990. * (HP/SP full restore and resurrect if need)
  9991. *------------------------------------------*/
  9992. BUILDIN_FUNC(recovery)
  9993. {
  9994. TBL_PC* sd;
  9995. struct s_mapiterator* iter;
  9996. iter = mapit_getallusers();
  9997. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9998. {
  9999. if(pc_isdead(sd))
  10000. status_revive(&sd->bl, 100, 100);
  10001. else
  10002. status_percent_heal(&sd->bl, 100, 100);
  10003. clif_displaymessage(sd->fd,"You have been recovered!");
  10004. }
  10005. mapit_free(iter);
  10006. return 0;
  10007. }
  10008. /*==========================================
  10009. * Get your pet info: getpetinfo(n)
  10010. * n -> 0:pet_id 1:pet_class 2:pet_name
  10011. * 3:friendly 4:hungry, 5: rename flag.
  10012. *------------------------------------------*/
  10013. BUILDIN_FUNC(getpetinfo)
  10014. {
  10015. TBL_PC *sd=script_rid2sd(st);
  10016. TBL_PET *pd;
  10017. int type=script_getnum(st,2);
  10018. if(!sd || !sd->pd) {
  10019. if (type == 2)
  10020. script_pushconststr(st,"null");
  10021. else
  10022. script_pushint(st,0);
  10023. return 0;
  10024. }
  10025. pd = sd->pd;
  10026. switch(type){
  10027. case 0: script_pushint(st,pd->pet.pet_id); break;
  10028. case 1: script_pushint(st,pd->pet.class_); break;
  10029. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10030. case 3: script_pushint(st,pd->pet.intimate); break;
  10031. case 4: script_pushint(st,pd->pet.hungry); break;
  10032. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10033. default:
  10034. script_pushint(st,0);
  10035. break;
  10036. }
  10037. return 0;
  10038. }
  10039. /*==========================================
  10040. * Get your homunculus info: gethominfo(n)
  10041. * n -> 0:hom_id 1:class 2:name
  10042. * 3:friendly 4:hungry, 5: rename flag.
  10043. * 6: level
  10044. *------------------------------------------*/
  10045. BUILDIN_FUNC(gethominfo)
  10046. {
  10047. TBL_PC *sd=script_rid2sd(st);
  10048. TBL_HOM *hd;
  10049. int type=script_getnum(st,2);
  10050. hd = sd?sd->hd:NULL;
  10051. if(!merc_is_hom_active(hd))
  10052. {
  10053. if (type == 2)
  10054. script_pushconststr(st,"null");
  10055. else
  10056. script_pushint(st,0);
  10057. return 0;
  10058. }
  10059. switch(type){
  10060. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10061. case 1: script_pushint(st,hd->homunculus.class_); break;
  10062. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10063. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10064. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10065. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10066. case 6: script_pushint(st,hd->homunculus.level); break;
  10067. default:
  10068. script_pushint(st,0);
  10069. break;
  10070. }
  10071. return 0;
  10072. }
  10073. /*==========================================
  10074. * Shows wether your inventory(and equips) contain
  10075. selected card or not.
  10076. checkequipedcard(4001);
  10077. *------------------------------------------*/
  10078. BUILDIN_FUNC(checkequipedcard)
  10079. {
  10080. TBL_PC *sd=script_rid2sd(st);
  10081. int n,i,c=0;
  10082. c=script_getnum(st,2);
  10083. if(sd){
  10084. for(i=0;i<MAX_INVENTORY;i++){
  10085. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10086. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10087. continue;
  10088. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10089. if(sd->status.inventory[i].card[n]==c){
  10090. script_pushint(st,1);
  10091. return 0;
  10092. }
  10093. }
  10094. }
  10095. }
  10096. }
  10097. script_pushint(st,0);
  10098. return 0;
  10099. }
  10100. BUILDIN_FUNC(jump_zero)
  10101. {
  10102. int sel;
  10103. sel=script_getnum(st,2);
  10104. if(!sel) {
  10105. int pos;
  10106. if( !data_islabel(script_getdata(st,3)) ){
  10107. ShowError("script: jump_zero: not label !\n");
  10108. st->state=END;
  10109. return 1;
  10110. }
  10111. pos=script_getnum(st,3);
  10112. st->pos=pos;
  10113. st->state=GOTO;
  10114. // printf("script: jump_zero: jumpto : %d\n",pos);
  10115. } else {
  10116. // printf("script: jump_zero: fail\n");
  10117. }
  10118. return 0;
  10119. }
  10120. /*==========================================
  10121. * GetMapMobs
  10122. returns mob counts on a set map:
  10123. e.g. GetMapMobs("prontera")
  10124. use "this" - for player's map
  10125. *------------------------------------------*/
  10126. BUILDIN_FUNC(getmapmobs)
  10127. {
  10128. const char *str=NULL;
  10129. int m=-1,bx,by;
  10130. int count=0;
  10131. struct block_list *bl;
  10132. str=script_getstr(st,2);
  10133. if(strcmp(str,"this")==0){
  10134. TBL_PC *sd=script_rid2sd(st);
  10135. if(sd)
  10136. m=sd->bl.m;
  10137. else{
  10138. script_pushint(st,-1);
  10139. return 0;
  10140. }
  10141. }else
  10142. m=map_mapname2mapid(str);
  10143. if(m < 0){
  10144. script_pushint(st,-1);
  10145. return 0;
  10146. }
  10147. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  10148. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  10149. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  10150. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  10151. count++;
  10152. script_pushint(st,count);
  10153. return 0;
  10154. }
  10155. /*==========================================
  10156. * movenpc [MouseJstr]
  10157. *------------------------------------------*/
  10158. BUILDIN_FUNC(movenpc)
  10159. {
  10160. TBL_NPC *nd = NULL;
  10161. const char *npc;
  10162. int x,y;
  10163. npc = script_getstr(st,2);
  10164. x = script_getnum(st,3);
  10165. y = script_getnum(st,4);
  10166. if ((nd = npc_name2id(npc)) == NULL)
  10167. return -1;
  10168. npc_movenpc(nd, x, y);
  10169. return 0;
  10170. }
  10171. /*==========================================
  10172. * message [MouseJstr]
  10173. *------------------------------------------*/
  10174. BUILDIN_FUNC(message)
  10175. {
  10176. const char *msg,*player;
  10177. TBL_PC *pl_sd = NULL;
  10178. player = script_getstr(st,2);
  10179. msg = script_getstr(st,3);
  10180. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10181. return 0;
  10182. clif_displaymessage(pl_sd->fd, msg);
  10183. return 0;
  10184. }
  10185. /*==========================================
  10186. * npctalk (sends message to surrounding area)
  10187. *------------------------------------------*/
  10188. BUILDIN_FUNC(npctalk)
  10189. {
  10190. const char* str;
  10191. char message[255];
  10192. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10193. str = script_getstr(st,2);
  10194. if(nd) {
  10195. memcpy(message, nd->name, NAME_LENGTH);
  10196. strtok(message, "#"); // discard extra name identifier if present
  10197. strcat(message, " : ");
  10198. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  10199. clif_message(&(nd->bl), message);
  10200. }
  10201. return 0;
  10202. }
  10203. // change npc walkspeed [Valaris]
  10204. BUILDIN_FUNC(npcspeed)
  10205. {
  10206. struct npc_data* nd;
  10207. int speed;
  10208. speed = script_getnum(st,2);
  10209. nd =(struct npc_data *)map_id2bl(st->oid);
  10210. if( nd )
  10211. {
  10212. nd->speed = speed;
  10213. nd->ud.state.speed_changed = 1;
  10214. }
  10215. return 0;
  10216. }
  10217. // make an npc walk to a position [Valaris]
  10218. BUILDIN_FUNC(npcwalkto)
  10219. {
  10220. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10221. int x=0,y=0;
  10222. x=script_getnum(st,2);
  10223. y=script_getnum(st,3);
  10224. if(nd) {
  10225. unit_walktoxy(&nd->bl,x,y,0);
  10226. }
  10227. return 0;
  10228. }
  10229. // stop an npc's movement [Valaris]
  10230. BUILDIN_FUNC(npcstop)
  10231. {
  10232. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10233. if(nd) {
  10234. unit_stop_walking(&nd->bl,1|4);
  10235. }
  10236. return 0;
  10237. }
  10238. /*==========================================
  10239. * getlook char info. getlook(arg)
  10240. *------------------------------------------*/
  10241. BUILDIN_FUNC(getlook)
  10242. {
  10243. int type,val;
  10244. TBL_PC *sd;
  10245. sd=script_rid2sd(st);
  10246. type=script_getnum(st,2);
  10247. val=-1;
  10248. switch(type) {
  10249. case LOOK_HAIR: val=sd->status.hair; break; //1
  10250. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10251. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10252. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10253. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10254. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10255. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10256. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10257. case LOOK_SHOES: break; //9
  10258. }
  10259. script_pushint(st,val);
  10260. return 0;
  10261. }
  10262. /*==========================================
  10263. * get char save point. argument: 0- map name, 1- x, 2- y
  10264. *------------------------------------------*/
  10265. BUILDIN_FUNC(getsavepoint)
  10266. {
  10267. TBL_PC* sd;
  10268. int type;
  10269. sd = script_rid2sd(st);
  10270. if (sd == NULL) {
  10271. script_pushint(st,0);
  10272. return 0;
  10273. }
  10274. type = script_getnum(st,2);
  10275. switch(type) {
  10276. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10277. case 1: script_pushint(st,sd->status.save_point.x); break;
  10278. case 2: script_pushint(st,sd->status.save_point.y); break;
  10279. default:
  10280. script_pushint(st,0);
  10281. break;
  10282. }
  10283. return 0;
  10284. }
  10285. /*==========================================
  10286. * Get position for char/npc/pet/mob objects. Added by Lorky
  10287. *
  10288. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10289. * where type:
  10290. * MapName$ - String variable for output map name
  10291. * MapX - Integer variable for output coord X
  10292. * MapY - Integer variable for output coord Y
  10293. * type - type of object
  10294. * 0 - Character coord
  10295. * 1 - NPC coord
  10296. * 2 - Pet coord
  10297. * 3 - Mob coord (not released)
  10298. * 4 - Homun coord
  10299. * CharName$ - Name object. If miss or "this" the current object
  10300. *
  10301. * Return:
  10302. * 0 - success
  10303. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10304. *------------------------------------------*/
  10305. BUILDIN_FUNC(getmapxy)
  10306. {
  10307. struct block_list *bl = NULL;
  10308. TBL_PC *sd=NULL;
  10309. int num;
  10310. const char *name;
  10311. char prefix;
  10312. int x,y,type;
  10313. char mapname[MAP_NAME_LENGTH];
  10314. if( !data_isreference(script_getdata(st,2)) ){
  10315. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10316. script_pushint(st,-1);
  10317. return 1;
  10318. }
  10319. if( !data_isreference(script_getdata(st,3)) ){
  10320. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10321. script_pushint(st,-1);
  10322. return 1;
  10323. }
  10324. if( !data_isreference(script_getdata(st,4)) ){
  10325. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10326. script_pushint(st,-1);
  10327. return 1;
  10328. }
  10329. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10330. type=script_getnum(st,5);
  10331. switch (type){
  10332. case 0: //Get Character Position
  10333. if( script_hasdata(st,6) )
  10334. sd=map_nick2sd(script_getstr(st,6));
  10335. else
  10336. sd=script_rid2sd(st);
  10337. if (sd)
  10338. bl = &sd->bl;
  10339. break;
  10340. case 1: //Get NPC Position
  10341. if( script_hasdata(st,6) )
  10342. {
  10343. struct npc_data *nd;
  10344. nd=npc_name2id(script_getstr(st,6));
  10345. if (nd)
  10346. bl = &nd->bl;
  10347. } else //In case the origin is not an npc?
  10348. bl=map_id2bl(st->oid);
  10349. break;
  10350. case 2: //Get Pet Position
  10351. if(script_hasdata(st,6))
  10352. sd=map_nick2sd(script_getstr(st,6));
  10353. else
  10354. sd=script_rid2sd(st);
  10355. if (sd && sd->pd)
  10356. bl = &sd->pd->bl;
  10357. break;
  10358. case 3: //Get Mob Position
  10359. break; //Not supported?
  10360. case 4: //Get Homun Position
  10361. if(script_hasdata(st,6))
  10362. sd=map_nick2sd(script_getstr(st,6));
  10363. else
  10364. sd=script_rid2sd(st);
  10365. if (sd && sd->hd)
  10366. bl = &sd->hd->bl;
  10367. break;
  10368. default:
  10369. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  10370. script_pushint(st,-1);
  10371. return 1;
  10372. }
  10373. if (!bl) { //No object found.
  10374. script_pushint(st,-1);
  10375. return 0;
  10376. }
  10377. x= bl->x;
  10378. y= bl->y;
  10379. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10380. //Set MapName$
  10381. num=st->stack->stack_data[st->start+2].u.num;
  10382. name=get_str(num&0x00ffffff);
  10383. prefix=*name;
  10384. if(not_server_variable(prefix))
  10385. sd=script_rid2sd(st);
  10386. else
  10387. sd=NULL;
  10388. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10389. //Set MapX
  10390. num=st->stack->stack_data[st->start+3].u.num;
  10391. name=get_str(num&0x00ffffff);
  10392. prefix=*name;
  10393. if(not_server_variable(prefix))
  10394. sd=script_rid2sd(st);
  10395. else
  10396. sd=NULL;
  10397. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10398. //Set MapY
  10399. num=st->stack->stack_data[st->start+4].u.num;
  10400. name=get_str(num&0x00ffffff);
  10401. prefix=*name;
  10402. if(not_server_variable(prefix))
  10403. sd=script_rid2sd(st);
  10404. else
  10405. sd=NULL;
  10406. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10407. //Return Success value
  10408. script_pushint(st,0);
  10409. return 0;
  10410. }
  10411. /*==========================================
  10412. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10413. *------------------------------------------*/
  10414. BUILDIN_FUNC(logmes)
  10415. {
  10416. const char *str;
  10417. TBL_PC* sd;
  10418. if( log_config.npc <= 0 )
  10419. return 0;
  10420. sd = script_rid2sd(st);
  10421. if( sd == NULL )
  10422. return 1;
  10423. str = script_getstr(st,2);
  10424. log_npc(sd,str);
  10425. return 0;
  10426. }
  10427. BUILDIN_FUNC(summon)
  10428. {
  10429. int _class, timeout=0;
  10430. const char *str,*event="";
  10431. TBL_PC *sd;
  10432. struct mob_data *md;
  10433. int tick = gettick();
  10434. sd=script_rid2sd(st);
  10435. if (!sd) return 0;
  10436. str =script_getstr(st,2);
  10437. _class=script_getnum(st,3);
  10438. if( script_hasdata(st,4) )
  10439. timeout=script_getnum(st,4);
  10440. if( script_hasdata(st,5) ){
  10441. event=script_getstr(st,5);
  10442. check_event(st, event);
  10443. }
  10444. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10445. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10446. if (md) {
  10447. md->master_id=sd->bl.id;
  10448. md->special_state.ai=1;
  10449. if( md->deletetimer != INVALID_TIMER )
  10450. delete_timer(md->deletetimer, mob_timer_delete);
  10451. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10452. mob_spawn (md); //Now it is ready for spawning.
  10453. clif_specialeffect(&md->bl,344,AREA);
  10454. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10455. }
  10456. return 0;
  10457. }
  10458. /*==========================================
  10459. * Checks whether it is daytime/nighttime
  10460. *------------------------------------------*/
  10461. BUILDIN_FUNC(isnight)
  10462. {
  10463. script_pushint(st,(night_flag == 1));
  10464. return 0;
  10465. }
  10466. BUILDIN_FUNC(isday)
  10467. {
  10468. script_pushint(st,(night_flag == 0));
  10469. return 0;
  10470. }
  10471. /*================================================
  10472. * Check how many items/cards in the list are
  10473. * equipped - used for 2/15's cards patch [celest]
  10474. *------------------------------------------------*/
  10475. BUILDIN_FUNC(isequippedcnt)
  10476. {
  10477. TBL_PC *sd;
  10478. int i, j, k, id = 1;
  10479. int ret = 0;
  10480. sd = script_rid2sd(st);
  10481. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10482. script_pushint(st,0);
  10483. return 0;
  10484. }
  10485. for (i=0; id!=0; i++) {
  10486. FETCH (i+2, id) else id = 0;
  10487. if (id <= 0)
  10488. continue;
  10489. for (j=0; j<EQI_MAX; j++) {
  10490. int index;
  10491. index = sd->equip_index[j];
  10492. if(index < 0) continue;
  10493. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10494. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10495. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10496. if(!sd->inventory_data[index])
  10497. continue;
  10498. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10499. if (sd->inventory_data[index]->nameid == id)
  10500. ret+= sd->status.inventory[index].amount;
  10501. } else { //Count cards.
  10502. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10503. continue; //No cards
  10504. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10505. if (sd->status.inventory[index].card[k] == id)
  10506. ret++; //[Lupus]
  10507. }
  10508. }
  10509. }
  10510. }
  10511. script_pushint(st,ret);
  10512. return 0;
  10513. }
  10514. /*================================================
  10515. * Check whether another card has been
  10516. * equipped - used for 2/15's cards patch [celest]
  10517. * -- Items checked cannot be reused in another
  10518. * card set to prevent exploits
  10519. *------------------------------------------------*/
  10520. BUILDIN_FUNC(isequipped)
  10521. {
  10522. TBL_PC *sd;
  10523. int i, j, k, id = 1;
  10524. int index, flag;
  10525. int ret = -1;
  10526. //Original hash to reverse it when full check fails.
  10527. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10528. sd = script_rid2sd(st);
  10529. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10530. script_pushint(st,0);
  10531. return 0;
  10532. }
  10533. setitem_hash = sd->setitem_hash;
  10534. setitem_hash2 = sd->setitem_hash2;
  10535. for (i=0; id!=0; i++)
  10536. {
  10537. FETCH (i+2, id) else id = 0;
  10538. if (id <= 0)
  10539. continue;
  10540. flag = 0;
  10541. for (j=0; j<EQI_MAX; j++)
  10542. {
  10543. index = sd->equip_index[j];
  10544. if(index < 0) continue;
  10545. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10546. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10547. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10548. if(!sd->inventory_data[index])
  10549. continue;
  10550. if (itemdb_type(id) != IT_CARD) {
  10551. if (sd->inventory_data[index]->nameid != id)
  10552. continue;
  10553. flag = 1;
  10554. break;
  10555. } else { //Cards
  10556. if (sd->inventory_data[index]->slot == 0 ||
  10557. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10558. continue;
  10559. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10560. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10561. unsigned int hash = 0;
  10562. if (sd->status.inventory[index].card[k] != id)
  10563. continue;
  10564. hash = 1<<((j<5?j:j-5)*4 + k);
  10565. // check if card is already used by another set
  10566. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10567. continue;
  10568. // We have found a match
  10569. flag = 1;
  10570. // Set hash so this card cannot be used by another
  10571. if (j<5)
  10572. sd->setitem_hash |= hash;
  10573. else
  10574. sd->setitem_hash2 |= hash;
  10575. break;
  10576. }
  10577. }
  10578. if (flag) break; //Card found
  10579. }
  10580. if (ret == -1)
  10581. ret = flag;
  10582. else
  10583. ret &= flag;
  10584. if (!ret) break;
  10585. }
  10586. if (!ret)
  10587. { //When check fails, restore original hash values. [Skotlex]
  10588. sd->setitem_hash = setitem_hash;
  10589. sd->setitem_hash2 = setitem_hash2;
  10590. }
  10591. script_pushint(st,ret);
  10592. return 0;
  10593. }
  10594. /*================================================
  10595. * Check how many given inserted cards in the CURRENT
  10596. * weapon - used for 2/15's cards patch [Lupus]
  10597. *------------------------------------------------*/
  10598. BUILDIN_FUNC(cardscnt)
  10599. {
  10600. TBL_PC *sd;
  10601. int i, k, id = 1;
  10602. int ret = 0;
  10603. int index;
  10604. sd = script_rid2sd(st);
  10605. for (i=0; id!=0; i++) {
  10606. FETCH (i+2, id) else id = 0;
  10607. if (id <= 0)
  10608. continue;
  10609. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10610. if(index < 0) continue;
  10611. if(!sd->inventory_data[index])
  10612. continue;
  10613. if(itemdb_type(id) != IT_CARD) {
  10614. if (sd->inventory_data[index]->nameid == id)
  10615. ret+= sd->status.inventory[index].amount;
  10616. } else {
  10617. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10618. continue;
  10619. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10620. if (sd->status.inventory[index].card[k] == id)
  10621. ret++;
  10622. }
  10623. }
  10624. }
  10625. script_pushint(st,ret);
  10626. // script_pushint(st,current_equip_item_index);
  10627. return 0;
  10628. }
  10629. /*=======================================================
  10630. * Returns the refined number of the current item, or an
  10631. * item with inventory index specified
  10632. *-------------------------------------------------------*/
  10633. BUILDIN_FUNC(getrefine)
  10634. {
  10635. TBL_PC *sd;
  10636. if ((sd = script_rid2sd(st))!= NULL)
  10637. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10638. else
  10639. script_pushint(st,0);
  10640. return 0;
  10641. }
  10642. /*=======================================================
  10643. * Day/Night controls
  10644. *-------------------------------------------------------*/
  10645. BUILDIN_FUNC(night)
  10646. {
  10647. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10648. return 0;
  10649. }
  10650. BUILDIN_FUNC(day)
  10651. {
  10652. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10653. return 0;
  10654. }
  10655. //=======================================================
  10656. // Unequip [Spectre]
  10657. //-------------------------------------------------------
  10658. BUILDIN_FUNC(unequip)
  10659. {
  10660. int i;
  10661. size_t num;
  10662. TBL_PC *sd;
  10663. num = script_getnum(st,2);
  10664. sd = script_rid2sd(st);
  10665. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10666. {
  10667. i = pc_checkequip(sd,equip[num-1]);
  10668. if (i >= 0)
  10669. pc_unequipitem(sd,i,1|2);
  10670. }
  10671. return 0;
  10672. }
  10673. BUILDIN_FUNC(equip)
  10674. {
  10675. int nameid=0,i;
  10676. TBL_PC *sd;
  10677. struct item_data *item_data;
  10678. sd = script_rid2sd(st);
  10679. nameid=script_getnum(st,2);
  10680. if((item_data = itemdb_exists(nameid)) == NULL)
  10681. {
  10682. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10683. return 1;
  10684. }
  10685. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10686. if( i < MAX_INVENTORY )
  10687. pc_equipitem(sd,i,item_data->equip);
  10688. return 0;
  10689. }
  10690. BUILDIN_FUNC(autoequip)
  10691. {
  10692. int nameid, flag;
  10693. struct item_data *item_data;
  10694. nameid=script_getnum(st,2);
  10695. flag=script_getnum(st,3);
  10696. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10697. item_data->flag.autoequip = flag>0?1:0;
  10698. }
  10699. return 0;
  10700. }
  10701. BUILDIN_FUNC(setbattleflag)
  10702. {
  10703. const char *flag, *value;
  10704. flag = script_getstr(st,2);
  10705. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  10706. if (battle_set_value(flag, value) == 0)
  10707. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10708. else
  10709. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10710. return 0;
  10711. }
  10712. BUILDIN_FUNC(getbattleflag)
  10713. {
  10714. const char *flag;
  10715. flag = script_getstr(st,2);
  10716. script_pushint(st,battle_get_value(flag));
  10717. return 0;
  10718. }
  10719. //=======================================================
  10720. // strlen [Valaris]
  10721. //-------------------------------------------------------
  10722. BUILDIN_FUNC(getstrlen)
  10723. {
  10724. const char *str = script_getstr(st,2);
  10725. int len = (str) ? (int)strlen(str) : 0;
  10726. script_pushint(st,len);
  10727. return 0;
  10728. }
  10729. //=======================================================
  10730. // isalpha [Valaris]
  10731. //-------------------------------------------------------
  10732. BUILDIN_FUNC(charisalpha)
  10733. {
  10734. const char *str=script_getstr(st,2);
  10735. int pos=script_getnum(st,3);
  10736. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10737. script_pushint(st,val);
  10738. return 0;
  10739. }
  10740. /// Changes the display name and/or display class of the npc.
  10741. /// Returns 0 is successful, 1 if the npc does not exist.
  10742. ///
  10743. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  10744. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10745. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10746. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10747. BUILDIN_FUNC(setnpcdisplay)
  10748. {
  10749. const char* name;
  10750. const char* newname = NULL;
  10751. int class_ = -1, size = -1;
  10752. struct script_data* data;
  10753. struct npc_data* nd;
  10754. name = script_getstr(st,2);
  10755. data = script_getdata(st,3);
  10756. if( script_hasdata(st,4) )
  10757. class_ = script_getnum(st,4);
  10758. if( script_hasdata(st,5) )
  10759. size = script_getnum(st,5);
  10760. get_val(st, data);
  10761. if( data_isstring(data) )
  10762. newname = conv_str(st,data);
  10763. else if( data_isint(data) )
  10764. class_ = conv_num(st,data);
  10765. else
  10766. {
  10767. ShowError("script:setnpcdisplay: expected a string or number\n");
  10768. script_reportdata(data);
  10769. return 1;
  10770. }
  10771. nd = npc_name2id(name);
  10772. if( nd == NULL )
  10773. {// not found
  10774. script_pushint(st,1);
  10775. return 0;
  10776. }
  10777. // update npc
  10778. if( newname )
  10779. npc_setdisplayname(nd, newname);
  10780. if( size != -1 && size != (int)nd->size )
  10781. nd->size = size;
  10782. else
  10783. size = -1;
  10784. if( class_ != -1 && nd->class_ != class_ )
  10785. npc_setclass(nd, class_);
  10786. else if( size != -1 )
  10787. { // Required to update the visual size
  10788. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  10789. clif_spawn(&nd->bl);
  10790. }
  10791. script_pushint(st,0);
  10792. return 0;
  10793. }
  10794. BUILDIN_FUNC(atoi)
  10795. {
  10796. const char *value;
  10797. value = script_getstr(st,2);
  10798. script_pushint(st,atoi(value));
  10799. return 0;
  10800. }
  10801. // case-insensitive substring search [lordalfa]
  10802. BUILDIN_FUNC(compare)
  10803. {
  10804. const char *message;
  10805. const char *cmpstring;
  10806. message = script_getstr(st,2);
  10807. cmpstring = script_getstr(st,3);
  10808. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10809. return 0;
  10810. }
  10811. // [zBuffer] List of mathematics commands --->
  10812. BUILDIN_FUNC(sqrt)
  10813. {
  10814. double i, a;
  10815. i = script_getnum(st,2);
  10816. a = sqrt(i);
  10817. script_pushint(st,(int)a);
  10818. return 0;
  10819. }
  10820. BUILDIN_FUNC(pow)
  10821. {
  10822. double i, a, b;
  10823. a = script_getnum(st,2);
  10824. b = script_getnum(st,3);
  10825. i = pow(a,b);
  10826. script_pushint(st,(int)i);
  10827. return 0;
  10828. }
  10829. BUILDIN_FUNC(distance)
  10830. {
  10831. int x0, y0, x1, y1;
  10832. x0 = script_getnum(st,2);
  10833. y0 = script_getnum(st,3);
  10834. x1 = script_getnum(st,4);
  10835. y1 = script_getnum(st,5);
  10836. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10837. return 0;
  10838. }
  10839. // <--- [zBuffer] List of mathematics commands
  10840. BUILDIN_FUNC(md5)
  10841. {
  10842. const char *tmpstr;
  10843. char *md5str;
  10844. tmpstr = script_getstr(st,2);
  10845. md5str = (char *)aMallocA((32+1)*sizeof(char));
  10846. MD5_String(tmpstr, md5str);
  10847. script_pushstr(st, md5str);
  10848. return 0;
  10849. }
  10850. // [zBuffer] List of dynamic var commands --->
  10851. BUILDIN_FUNC(setd)
  10852. {
  10853. TBL_PC *sd=NULL;
  10854. char varname[100];
  10855. const char *buffer;
  10856. int elem;
  10857. buffer = script_getstr(st, 2);
  10858. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10859. elem = 0;
  10860. if( not_server_variable(*varname) )
  10861. {
  10862. sd = script_rid2sd(st);
  10863. if( sd == NULL )
  10864. {
  10865. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10866. return 0;
  10867. }
  10868. }
  10869. if( is_string_variable(varname) ) {
  10870. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  10871. } else {
  10872. setd_sub(st, sd, varname, elem, (void *)script_getnum(st, 3), NULL);
  10873. }
  10874. return 0;
  10875. }
  10876. #ifndef TXT_ONLY
  10877. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10878. {
  10879. int i, j;
  10880. TBL_PC* sd = NULL;
  10881. const char* query;
  10882. struct script_data* data;
  10883. const char* name;
  10884. int max_rows = 128;// maximum number of rows
  10885. int num_vars;
  10886. int num_cols;
  10887. // check target variables
  10888. for( i = 3; script_hasdata(st,i); ++i )
  10889. {
  10890. data = script_getdata(st, i);
  10891. if( data_isreference(data) && reference_tovariable(data) )
  10892. {// it's a variable
  10893. name = reference_getname(data);
  10894. if( not_server_variable(*name) && sd == NULL )
  10895. {// requires a player
  10896. sd = script_rid2sd(st);
  10897. if( sd == NULL )
  10898. {// no player attached
  10899. script_reportdata(data);
  10900. st->state = END;
  10901. return 1;
  10902. }
  10903. }
  10904. if( not_array_variable(*name) )
  10905. max_rows = 1;// not an array, limit to one row
  10906. }
  10907. else
  10908. {
  10909. ShowError("script:query_sql: not a variable\n");
  10910. script_reportdata(data);
  10911. st->state = END;
  10912. return 1;
  10913. }
  10914. }
  10915. num_vars = i - 3;
  10916. // Execute the query
  10917. query = script_getstr(st,2);
  10918. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10919. {
  10920. Sql_ShowDebug(handle);
  10921. script_pushint(st, 0);
  10922. return 1;
  10923. }
  10924. if( Sql_NumRows(handle) == 0 )
  10925. {// No data received
  10926. Sql_FreeResult(handle);
  10927. script_pushint(st, 0);
  10928. return 0;
  10929. }
  10930. // Count the number of columns to store
  10931. num_cols = Sql_NumColumns(handle);
  10932. if( num_vars < num_cols )
  10933. {
  10934. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10935. script_reportsrc(st);
  10936. }
  10937. else if( num_vars > num_cols )
  10938. {
  10939. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10940. script_reportsrc(st);
  10941. }
  10942. // Store data
  10943. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10944. {
  10945. for( j = 0; j < num_vars; ++j )
  10946. {
  10947. char* str = NULL;
  10948. if( j < num_cols )
  10949. Sql_GetData(handle, j, &str, NULL);
  10950. data = script_getdata(st, j+3);
  10951. name = reference_getname(data);
  10952. if( is_string_variable(name) )
  10953. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10954. else
  10955. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10956. }
  10957. }
  10958. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10959. {
  10960. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10961. script_reportsrc(st);
  10962. }
  10963. // Free data
  10964. Sql_FreeResult(handle);
  10965. script_pushint(st, i);
  10966. return 0;
  10967. }
  10968. #endif
  10969. BUILDIN_FUNC(query_sql)
  10970. {
  10971. #ifndef TXT_ONLY
  10972. return buildin_query_sql_sub(st, mmysql_handle);
  10973. #else
  10974. //for TXT version, we always return -1
  10975. script_pushint(st,-1);
  10976. return 0;
  10977. #endif
  10978. }
  10979. BUILDIN_FUNC(query_logsql)
  10980. {
  10981. #ifndef TXT_ONLY
  10982. return buildin_query_sql_sub(st, logmysql_handle);
  10983. #else
  10984. //for TXT version, we always return -1
  10985. script_pushint(st,-1);
  10986. return 0;
  10987. #endif
  10988. }
  10989. //Allows escaping of a given string.
  10990. BUILDIN_FUNC(escape_sql)
  10991. {
  10992. const char *str;
  10993. char *esc_str;
  10994. size_t len;
  10995. str = script_getstr(st,2);
  10996. len = strlen(str);
  10997. esc_str = (char*)aMallocA(len*2+1);
  10998. #if defined(TXT_ONLY)
  10999. jstrescapecpy(esc_str, str);
  11000. #else
  11001. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  11002. #endif
  11003. script_pushstr(st, esc_str);
  11004. return 0;
  11005. }
  11006. BUILDIN_FUNC(getd)
  11007. {
  11008. char varname[100];
  11009. const char *buffer;
  11010. int elem;
  11011. buffer = script_getstr(st, 2);
  11012. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  11013. elem = 0;
  11014. // Push the 'pointer' so it's more flexible [Lance]
  11015. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  11016. return 0;
  11017. }
  11018. // <--- [zBuffer] List of dynamic var commands
  11019. // Pet stat [Lance]
  11020. BUILDIN_FUNC(petstat)
  11021. {
  11022. TBL_PC *sd = NULL;
  11023. struct pet_data *pd;
  11024. int flag = script_getnum(st,2);
  11025. sd = script_rid2sd(st);
  11026. if(!sd || !sd->status.pet_id || !sd->pd){
  11027. if(flag == 2)
  11028. script_pushconststr(st, "");
  11029. else
  11030. script_pushint(st,0);
  11031. return 0;
  11032. }
  11033. pd = sd->pd;
  11034. switch(flag){
  11035. case 1: script_pushint(st,(int)pd->pet.class_); break;
  11036. case 2: script_pushstrcopy(st, pd->pet.name); break;
  11037. case 3: script_pushint(st,(int)pd->pet.level); break;
  11038. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  11039. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  11040. default:
  11041. script_pushint(st,0);
  11042. break;
  11043. }
  11044. return 0;
  11045. }
  11046. BUILDIN_FUNC(callshop)
  11047. {
  11048. TBL_PC *sd = NULL;
  11049. struct npc_data *nd;
  11050. const char *shopname;
  11051. int flag = 0;
  11052. sd = script_rid2sd(st);
  11053. if (!sd) {
  11054. script_pushint(st,0);
  11055. return 0;
  11056. }
  11057. shopname = script_getstr(st, 2);
  11058. if( script_hasdata(st,3) )
  11059. flag = script_getnum(st,3);
  11060. nd = npc_name2id(shopname);
  11061. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  11062. {
  11063. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  11064. script_pushint(st,0);
  11065. return 1;
  11066. }
  11067. if( nd->subtype == SHOP )
  11068. {
  11069. switch( flag )
  11070. {
  11071. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  11072. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  11073. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  11074. }
  11075. }
  11076. else
  11077. clif_cashshop_show(sd, nd);
  11078. sd->npc_shopid = nd->bl.id;
  11079. script_pushint(st,1);
  11080. return 0;
  11081. }
  11082. BUILDIN_FUNC(npcshopitem)
  11083. {
  11084. const char* npcname = script_getstr(st, 2);
  11085. struct npc_data* nd = npc_name2id(npcname);
  11086. int n, i;
  11087. int amount;
  11088. if( !nd || nd->subtype != SHOP )
  11089. { //Not found.
  11090. script_pushint(st,0);
  11091. return 0;
  11092. }
  11093. // get the count of new entries
  11094. amount = (script_lastdata(st)-2)/2;
  11095. // generate new shop item list
  11096. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  11097. for( n = 0, i = 3; n < amount; n++, i+=2 )
  11098. {
  11099. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  11100. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  11101. }
  11102. nd->u.shop.count = n;
  11103. script_pushint(st,1);
  11104. return 0;
  11105. }
  11106. BUILDIN_FUNC(npcshopadditem)
  11107. {
  11108. const char* npcname = script_getstr(st,2);
  11109. struct npc_data* nd = npc_name2id(npcname);
  11110. int n, i;
  11111. int amount;
  11112. if( !nd || nd->subtype != SHOP )
  11113. { //Not found.
  11114. script_pushint(st,0);
  11115. return 0;
  11116. }
  11117. // get the count of new entries
  11118. amount = (script_lastdata(st)-2)/2;
  11119. // append new items to existing shop item list
  11120. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  11121. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  11122. {
  11123. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  11124. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  11125. }
  11126. nd->u.shop.count = n;
  11127. script_pushint(st,1);
  11128. return 0;
  11129. }
  11130. BUILDIN_FUNC(npcshopdelitem)
  11131. {
  11132. const char* npcname = script_getstr(st,2);
  11133. struct npc_data* nd = npc_name2id(npcname);
  11134. unsigned int nameid;
  11135. int n, i;
  11136. int amount;
  11137. int size;
  11138. if( !nd || nd->subtype != SHOP )
  11139. { //Not found.
  11140. script_pushint(st,0);
  11141. return 0;
  11142. }
  11143. amount = script_lastdata(st)-2;
  11144. size = nd->u.shop.count;
  11145. // remove specified items from the shop item list
  11146. for( i = 3; i < 3 + amount; i++ )
  11147. {
  11148. nameid = script_getnum(st,i);
  11149. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  11150. if( n < size )
  11151. {
  11152. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  11153. size--;
  11154. }
  11155. }
  11156. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  11157. nd->u.shop.count = size;
  11158. script_pushint(st,1);
  11159. return 0;
  11160. }
  11161. //Sets a script to attach to a shop npc.
  11162. BUILDIN_FUNC(npcshopattach)
  11163. {
  11164. const char* npcname = script_getstr(st,2);
  11165. struct npc_data* nd = npc_name2id(npcname);
  11166. int flag = 1;
  11167. if( script_hasdata(st,3) )
  11168. flag = script_getnum(st,3);
  11169. if( !nd || nd->subtype != SHOP )
  11170. { //Not found.
  11171. script_pushint(st,0);
  11172. return 0;
  11173. }
  11174. if (flag)
  11175. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  11176. else
  11177. nd->master_nd = NULL;
  11178. script_pushint(st,1);
  11179. return 0;
  11180. }
  11181. /*==========================================
  11182. * Returns some values of an item [Lupus]
  11183. * Price, Weight, etc...
  11184. setitemscript(itemID,"{new item bonus script}",[n]);
  11185. Where n:
  11186. 0 - script
  11187. 1 - Equip script
  11188. 2 - Unequip script
  11189. *------------------------------------------*/
  11190. BUILDIN_FUNC(setitemscript)
  11191. {
  11192. int item_id,n=0;
  11193. const char *script;
  11194. struct item_data *i_data;
  11195. struct script_code **dstscript;
  11196. item_id = script_getnum(st,2);
  11197. script = script_getstr(st,3);
  11198. if( script_hasdata(st,4) )
  11199. n=script_getnum(st,4);
  11200. i_data = itemdb_exists(item_id);
  11201. if (!i_data || script==NULL || script[0]!='{') {
  11202. script_pushint(st,0);
  11203. return 0;
  11204. }
  11205. switch (n) {
  11206. case 2:
  11207. dstscript = &i_data->unequip_script;
  11208. break;
  11209. case 1:
  11210. dstscript = &i_data->equip_script;
  11211. break;
  11212. default:
  11213. dstscript = &i_data->script;
  11214. break;
  11215. }
  11216. if(*dstscript)
  11217. script_free_code(*dstscript);
  11218. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11219. script_pushint(st,1);
  11220. return 0;
  11221. }
  11222. /* Work In Progress [Lupus]
  11223. BUILDIN_FUNC(addmonsterdrop)
  11224. {
  11225. int class_,item_id,chance;
  11226. class_=script_getnum(st,2);
  11227. item_id=script_getnum(st,3);
  11228. chance=script_getnum(st,4);
  11229. if(class_>1000 && item_id>500 && chance>0) {
  11230. script_pushint(st,1);
  11231. } else {
  11232. script_pushint(st,0);
  11233. }
  11234. }
  11235. BUILDIN_FUNC(delmonsterdrop)
  11236. {
  11237. int class_,item_id;
  11238. class_=script_getnum(st,2);
  11239. item_id=script_getnum(st,3);
  11240. if(class_>1000 && item_id>500) {
  11241. script_pushint(st,1);
  11242. } else {
  11243. script_pushint(st,0);
  11244. }
  11245. }
  11246. */
  11247. /*==========================================
  11248. * Returns some values of a monster [Lupus]
  11249. * Name, Level, race, size, etc...
  11250. getmonsterinfo(monsterID,queryIndex);
  11251. *------------------------------------------*/
  11252. BUILDIN_FUNC(getmonsterinfo)
  11253. {
  11254. struct mob_db *mob;
  11255. int mob_id;
  11256. mob_id = script_getnum(st,2);
  11257. if (!mobdb_checkid(mob_id)) {
  11258. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  11259. if ( !script_getnum(st,3) ) //requested a string
  11260. script_pushconststr(st,"null");
  11261. else
  11262. script_pushint(st,-1);
  11263. return -1;
  11264. }
  11265. mob = mob_db(mob_id);
  11266. switch ( script_getnum(st,3) ) {
  11267. case 0: script_pushstrcopy(st,mob->jname); break;
  11268. case 1: script_pushint(st,mob->lv); break;
  11269. case 2: script_pushint(st,mob->status.max_hp); break;
  11270. case 3: script_pushint(st,mob->base_exp); break;
  11271. case 4: script_pushint(st,mob->job_exp); break;
  11272. case 5: script_pushint(st,mob->status.rhw.atk); break;
  11273. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  11274. case 7: script_pushint(st,mob->status.def); break;
  11275. case 8: script_pushint(st,mob->status.mdef); break;
  11276. case 9: script_pushint(st,mob->status.str); break;
  11277. case 10: script_pushint(st,mob->status.agi); break;
  11278. case 11: script_pushint(st,mob->status.vit); break;
  11279. case 12: script_pushint(st,mob->status.int_); break;
  11280. case 13: script_pushint(st,mob->status.dex); break;
  11281. case 14: script_pushint(st,mob->status.luk); break;
  11282. case 15: script_pushint(st,mob->status.rhw.range); break;
  11283. case 16: script_pushint(st,mob->range2); break;
  11284. case 17: script_pushint(st,mob->range3); break;
  11285. case 18: script_pushint(st,mob->status.size); break;
  11286. case 19: script_pushint(st,mob->status.race); break;
  11287. case 20: script_pushint(st,mob->status.def_ele); break;
  11288. case 21: script_pushint(st,mob->status.mode); break;
  11289. default: script_pushint(st,-1); //wrong Index
  11290. }
  11291. return 0;
  11292. }
  11293. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11294. {
  11295. TBL_PC *sd = NULL;
  11296. if(script_hasdata(st,2))
  11297. sd = map_nick2sd(script_getstr(st,2));
  11298. else
  11299. sd = script_rid2sd(st);
  11300. if(sd)
  11301. script_pushint(st,(sd->vender_id != 0));
  11302. else
  11303. script_pushint(st,0);
  11304. return 0;
  11305. }
  11306. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11307. {
  11308. TBL_PC *sd = NULL;
  11309. if(script_hasdata(st,2))
  11310. sd = map_nick2sd(script_getstr(st,2));
  11311. else
  11312. sd = script_rid2sd(st);
  11313. if(sd)
  11314. script_pushint(st,(sd->chatID != 0));
  11315. else
  11316. script_pushint(st,0);
  11317. return 0;
  11318. }
  11319. BUILDIN_FUNC(searchitem)
  11320. {
  11321. struct script_data* data = script_getdata(st, 2);
  11322. const char *itemname = script_getstr(st,3);
  11323. struct item_data *items[MAX_SEARCH];
  11324. int count;
  11325. char* name;
  11326. int32 start;
  11327. int32 id;
  11328. int32 i;
  11329. TBL_PC* sd = NULL;
  11330. if ((items[0] = itemdb_exists(atoi(itemname))))
  11331. count = 1;
  11332. else {
  11333. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  11334. if (count > MAX_SEARCH) count = MAX_SEARCH;
  11335. }
  11336. if (!count) {
  11337. script_pushint(st, 0);
  11338. return 0;
  11339. }
  11340. if( !data_isreference(data) )
  11341. {
  11342. ShowError("script:searchitem: not a variable\n");
  11343. script_reportdata(data);
  11344. st->state = END;
  11345. return 1;// not a variable
  11346. }
  11347. id = reference_getid(data);
  11348. start = reference_getindex(data);
  11349. name = reference_getname(data);
  11350. if( not_array_variable(*name) )
  11351. {
  11352. ShowError("script:searchitem: illegal scope\n");
  11353. script_reportdata(data);
  11354. st->state = END;
  11355. return 1;// not supported
  11356. }
  11357. if( not_server_variable(*name) )
  11358. {
  11359. sd = script_rid2sd(st);
  11360. if( sd == NULL )
  11361. return 0;// no player attached
  11362. }
  11363. if( is_string_variable(name) )
  11364. {// string array
  11365. ShowError("script:searchitem: not an integer array reference\n");
  11366. script_reportdata(data);
  11367. st->state = END;
  11368. return 1;// not supported
  11369. }
  11370. for( i = 0; i < count; ++start, ++i )
  11371. {// Set array
  11372. void* v = (void*)items[i]->nameid;
  11373. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  11374. }
  11375. script_pushint(st, count);
  11376. return 0;
  11377. }
  11378. int axtoi(const char *hexStg)
  11379. {
  11380. int n = 0; // position in string
  11381. int m = 0; // position in digit[] to shift
  11382. int count; // loop index
  11383. int intValue = 0; // integer value of hex string
  11384. int digit[11]; // hold values to convert
  11385. while (n < 10) {
  11386. if (hexStg[n]=='\0')
  11387. break;
  11388. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11389. digit[n] = hexStg[n] & 0x0f; //convert to int
  11390. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11391. digit[n] = (hexStg[n] & 0x0f) + 9; //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 break;
  11395. n++;
  11396. }
  11397. count = n;
  11398. m = n - 1;
  11399. n = 0;
  11400. while(n < count) {
  11401. // digit[n] is value of hex digit at position n
  11402. // (m << 2) is the number of positions to shift
  11403. // OR the bits into return value
  11404. intValue = intValue | (digit[n] << (m << 2));
  11405. m--; // adjust the position to set
  11406. n++; // next digit to process
  11407. }
  11408. return (intValue);
  11409. }
  11410. // [Lance] Hex string to integer converter
  11411. BUILDIN_FUNC(axtoi)
  11412. {
  11413. const char *hex = script_getstr(st,2);
  11414. script_pushint(st,axtoi(hex));
  11415. return 0;
  11416. }
  11417. // [zBuffer] List of player cont commands --->
  11418. BUILDIN_FUNC(rid2name)
  11419. {
  11420. struct block_list *bl = NULL;
  11421. int rid = script_getnum(st,2);
  11422. if((bl = map_id2bl(rid)))
  11423. {
  11424. switch(bl->type) {
  11425. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  11426. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  11427. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  11428. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  11429. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  11430. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  11431. default:
  11432. ShowError("buildin_rid2name: BL type unknown.\n");
  11433. script_pushconststr(st,"");
  11434. break;
  11435. }
  11436. } else {
  11437. ShowError("buildin_rid2name: invalid RID\n");
  11438. script_pushconststr(st,"(null)");
  11439. }
  11440. return 0;
  11441. }
  11442. BUILDIN_FUNC(pcblockmove)
  11443. {
  11444. int id, flag;
  11445. TBL_PC *sd = NULL;
  11446. id = script_getnum(st,2);
  11447. flag = script_getnum(st,3);
  11448. if(id)
  11449. sd = map_id2sd(id);
  11450. else
  11451. sd = script_rid2sd(st);
  11452. if(sd)
  11453. sd->state.blockedmove = flag > 0;
  11454. return 0;
  11455. }
  11456. BUILDIN_FUNC(pcfollow)
  11457. {
  11458. int id, targetid;
  11459. TBL_PC *sd = NULL;
  11460. id = script_getnum(st,2);
  11461. targetid = script_getnum(st,3);
  11462. if(id)
  11463. sd = map_id2sd(id);
  11464. else
  11465. sd = script_rid2sd(st);
  11466. if(sd)
  11467. pc_follow(sd, targetid);
  11468. return 0;
  11469. }
  11470. BUILDIN_FUNC(pcstopfollow)
  11471. {
  11472. int id;
  11473. TBL_PC *sd = NULL;
  11474. id = script_getnum(st,2);
  11475. if(id)
  11476. sd = map_id2sd(id);
  11477. else
  11478. sd = script_rid2sd(st);
  11479. if(sd)
  11480. pc_stop_following(sd);
  11481. return 0;
  11482. }
  11483. // <--- [zBuffer] List of player cont commands
  11484. // [zBuffer] List of mob control commands --->
  11485. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11486. /// Makes the unit walk to target position or map
  11487. /// Returns if it was successfull
  11488. ///
  11489. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11490. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11491. BUILDIN_FUNC(unitwalk)
  11492. {
  11493. struct block_list* bl;
  11494. bl = map_id2bl(script_getnum(st,2));
  11495. if( bl == NULL )
  11496. {
  11497. script_pushint(st, 0);
  11498. }
  11499. else if( script_hasdata(st,4) )
  11500. {
  11501. int x = script_getnum(st,3);
  11502. int y = script_getnum(st,4);
  11503. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11504. }
  11505. else
  11506. {
  11507. int map_id = script_getnum(st,3);
  11508. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11509. }
  11510. return 0;
  11511. }
  11512. /// Kills the unit
  11513. ///
  11514. /// unitkill <unit_id>;
  11515. BUILDIN_FUNC(unitkill)
  11516. {
  11517. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11518. if( bl != NULL )
  11519. status_kill(bl);
  11520. return 0;
  11521. }
  11522. /// Warps the unit to the target position in the target map
  11523. /// Returns if it was successfull
  11524. ///
  11525. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11526. BUILDIN_FUNC(unitwarp)
  11527. {
  11528. int unit_id;
  11529. int map;
  11530. short x;
  11531. short y;
  11532. struct block_list* bl;
  11533. unit_id = script_getnum(st,2);
  11534. map = map_mapname2mapid(script_getstr(st, 3));
  11535. x = (short)script_getnum(st,4);
  11536. y = (short)script_getnum(st,5);
  11537. bl = map_id2bl(unit_id);
  11538. if( map >= 0 && bl != NULL )
  11539. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  11540. else
  11541. script_pushint(st, 0);
  11542. return 0;
  11543. }
  11544. /// Makes the unit attack the target.
  11545. /// If the unit is a player and <action type> is not 0, it does a continuous
  11546. /// attack instead of a single attack.
  11547. /// Returns if the request was successfull.
  11548. ///
  11549. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11550. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11551. BUILDIN_FUNC(unitattack)
  11552. {
  11553. struct block_list* unit_bl;
  11554. struct block_list* target_bl = NULL;
  11555. struct script_data* data;
  11556. int actiontype = 0;
  11557. // get unit
  11558. unit_bl = map_id2bl(script_getnum(st,2));
  11559. if( unit_bl == NULL ) {
  11560. script_pushint(st, 0);
  11561. return 0;
  11562. }
  11563. data = script_getdata(st, 3);
  11564. get_val(st, data);
  11565. if( data_isstring(data) )
  11566. {
  11567. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11568. if( sd != NULL )
  11569. target_bl = &sd->bl;
  11570. } else
  11571. target_bl = map_id2bl(conv_num(st, data));
  11572. // request the attack
  11573. if( target_bl == NULL )
  11574. {
  11575. script_pushint(st, 0);
  11576. return 0;
  11577. }
  11578. // get actiontype
  11579. if( script_hasdata(st,4) )
  11580. actiontype = script_getnum(st,4);
  11581. switch( unit_bl->type )
  11582. {
  11583. case BL_PC:
  11584. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11585. script_pushint(st, 1);
  11586. return 0;
  11587. case BL_MOB:
  11588. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11589. break;
  11590. case BL_PET:
  11591. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11592. break;
  11593. default:
  11594. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11595. script_pushint(st, 0);
  11596. return 1;
  11597. }
  11598. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11599. return 0;
  11600. }
  11601. /// Makes the unit stop attacking and moving
  11602. ///
  11603. /// unitstop <unit_id>;
  11604. BUILDIN_FUNC(unitstop)
  11605. {
  11606. int unit_id;
  11607. struct block_list* bl;
  11608. unit_id = script_getnum(st,2);
  11609. bl = map_id2bl(unit_id);
  11610. if( bl != NULL )
  11611. {
  11612. unit_stop_attack(bl);
  11613. unit_stop_walking(bl,4);
  11614. if( bl->type == BL_MOB )
  11615. ((TBL_MOB*)bl)->target_id = 0;
  11616. }
  11617. return 0;
  11618. }
  11619. /// Makes the unit say the message
  11620. ///
  11621. /// unittalk <unit_id>,"<message>";
  11622. BUILDIN_FUNC(unittalk)
  11623. {
  11624. int unit_id;
  11625. const char* message;
  11626. struct block_list* bl;
  11627. unit_id = script_getnum(st,2);
  11628. message = script_getstr(st, 3);
  11629. bl = map_id2bl(unit_id);
  11630. if( bl != NULL )
  11631. {
  11632. struct StringBuf sbuf;
  11633. StringBuf_Init(&sbuf);
  11634. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11635. clif_message(bl, StringBuf_Value(&sbuf));
  11636. if( bl->type == BL_PC )
  11637. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11638. StringBuf_Destroy(&sbuf);
  11639. }
  11640. return 0;
  11641. }
  11642. /// Makes the unit do an emotion
  11643. ///
  11644. /// unitemote <unit_id>,<emotion>;
  11645. ///
  11646. /// @see e_* in const.txt
  11647. BUILDIN_FUNC(unitemote)
  11648. {
  11649. int unit_id;
  11650. int emotion;
  11651. struct block_list* bl;
  11652. unit_id = script_getnum(st,2);
  11653. emotion = script_getnum(st,3);
  11654. bl = map_id2bl(unit_id);
  11655. if( bl != NULL )
  11656. clif_emotion(bl, emotion);
  11657. return 0;
  11658. }
  11659. /// Makes the unit cast the skill on the target or self if no target is specified
  11660. ///
  11661. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11662. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  11663. BUILDIN_FUNC(unitskilluseid)
  11664. {
  11665. int unit_id;
  11666. int skill_id;
  11667. int skill_lv;
  11668. int target_id;
  11669. struct block_list* bl;
  11670. unit_id = script_getnum(st,2);
  11671. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11672. skill_lv = script_getnum(st,4);
  11673. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11674. bl = map_id2bl(unit_id);
  11675. if( bl != NULL )
  11676. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11677. return 0;
  11678. }
  11679. /// Makes the unit cast the skill on the target position.
  11680. ///
  11681. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11682. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  11683. BUILDIN_FUNC(unitskillusepos)
  11684. {
  11685. int unit_id;
  11686. int skill_id;
  11687. int skill_lv;
  11688. int skill_x;
  11689. int skill_y;
  11690. struct block_list* bl;
  11691. unit_id = script_getnum(st,2);
  11692. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11693. skill_lv = script_getnum(st,4);
  11694. skill_x = script_getnum(st,5);
  11695. skill_y = script_getnum(st,6);
  11696. bl = map_id2bl(unit_id);
  11697. if( bl != NULL )
  11698. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11699. return 0;
  11700. }
  11701. // <--- [zBuffer] List of mob control commands
  11702. /// Pauses the execution of the script, detaching the player
  11703. ///
  11704. /// sleep <mili seconds>;
  11705. BUILDIN_FUNC(sleep)
  11706. {
  11707. int ticks;
  11708. ticks = script_getnum(st,2);
  11709. // detach the player
  11710. script_detach_rid(st);
  11711. if( ticks <= 0 )
  11712. {// do nothing
  11713. }
  11714. else if( st->sleep.tick == 0 )
  11715. {// sleep for the target amount of time
  11716. st->state = RERUNLINE;
  11717. st->sleep.tick = ticks;
  11718. }
  11719. else
  11720. {// sleep time is over
  11721. st->state = RUN;
  11722. st->sleep.tick = 0;
  11723. }
  11724. return 0;
  11725. }
  11726. /// Pauses the execution of the script, keeping the player attached
  11727. /// Returns if a player is still attached
  11728. ///
  11729. /// sleep2(<mili secconds>) -> <bool>
  11730. BUILDIN_FUNC(sleep2)
  11731. {
  11732. int ticks;
  11733. ticks = script_getnum(st,2);
  11734. if( ticks <= 0 )
  11735. {// do nothing
  11736. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11737. }
  11738. else if( !st->sleep.tick )
  11739. {// sleep for the target amount of time
  11740. st->state = RERUNLINE;
  11741. st->sleep.tick = ticks;
  11742. }
  11743. else
  11744. {// sleep time is over
  11745. st->state = RUN;
  11746. st->sleep.tick = 0;
  11747. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11748. }
  11749. return 0;
  11750. }
  11751. /// Awakes all the sleep timers of the target npc
  11752. ///
  11753. /// awake "<npc name>";
  11754. BUILDIN_FUNC(awake)
  11755. {
  11756. struct npc_data* nd;
  11757. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11758. nd = npc_name2id(script_getstr(st, 2));
  11759. if( nd == NULL ) {
  11760. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  11761. return 1;
  11762. }
  11763. while( node )
  11764. {
  11765. if( (int)node->key == nd->bl.id )
  11766. {// sleep timer for the npc
  11767. struct script_state* tst = (struct script_state*)node->data;
  11768. TBL_PC* sd = map_id2sd(tst->rid);
  11769. if( tst->sleep.timer == -1 )
  11770. {// already awake ???
  11771. node = node->next;
  11772. continue;
  11773. }
  11774. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11775. {// char not online anymore / another char of the same account is online - Cancel execution
  11776. tst->state = END;
  11777. tst->rid = 0;
  11778. }
  11779. delete_timer(tst->sleep.timer, run_script_timer);
  11780. node = script_erase_sleepdb(node);
  11781. tst->sleep.timer = INVALID_TIMER;
  11782. if(tst->state != RERUNLINE)
  11783. tst->sleep.tick = 0;
  11784. run_script_main(tst);
  11785. }
  11786. else
  11787. {
  11788. node = node->next;
  11789. }
  11790. }
  11791. return 0;
  11792. }
  11793. /// Returns a reference to a variable of the target NPC.
  11794. /// Returns 0 if an error occurs.
  11795. ///
  11796. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11797. BUILDIN_FUNC(getvariableofnpc)
  11798. {
  11799. struct script_data* data;
  11800. const char* name;
  11801. struct npc_data* nd;
  11802. data = script_getdata(st,2);
  11803. if( !data_isreference(data) )
  11804. {// Not a reference (aka varaible name)
  11805. ShowError("script:getvariableofnpc: not a variable\n");
  11806. script_reportdata(data);
  11807. script_pushnil(st);
  11808. st->state = END;
  11809. return 1;
  11810. }
  11811. name = reference_getname(data);
  11812. if( *name != '.' || name[1] == '@' )
  11813. {// not a npc variable
  11814. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11815. script_reportdata(data);
  11816. script_pushnil(st);
  11817. st->state = END;
  11818. return 1;
  11819. }
  11820. nd = npc_name2id(script_getstr(st,3));
  11821. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11822. {// NPC not found or has no script
  11823. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11824. script_pushnil(st);
  11825. st->state = END;
  11826. return 1;
  11827. }
  11828. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11829. return 0;
  11830. }
  11831. /// Opens a warp portal.
  11832. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11833. ///
  11834. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11835. ///
  11836. /// @author blackhole89
  11837. BUILDIN_FUNC(warpportal)
  11838. {
  11839. int spx;
  11840. int spy;
  11841. unsigned short mapindex;
  11842. int tpx;
  11843. int tpy;
  11844. struct skill_unit_group* group;
  11845. struct block_list* bl;
  11846. bl = map_id2bl(st->oid);
  11847. if( bl == NULL )
  11848. {
  11849. ShowError("script:warpportal: npc is needed\n");
  11850. return 1;
  11851. }
  11852. spx = script_getnum(st,2);
  11853. spy = script_getnum(st,3);
  11854. mapindex = mapindex_name2id(script_getstr(st, 4));
  11855. tpx = script_getnum(st,5);
  11856. tpy = script_getnum(st,6);
  11857. if( mapindex == 0 )
  11858. return 0;// map not found
  11859. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11860. if( group == NULL )
  11861. return 0;// failed
  11862. group->val2 = (tpx<<16) | tpy;
  11863. group->val3 = mapindex;
  11864. return 0;
  11865. }
  11866. BUILDIN_FUNC(openmail)
  11867. {
  11868. TBL_PC* sd;
  11869. sd = script_rid2sd(st);
  11870. if( sd == NULL )
  11871. return 0;
  11872. #ifndef TXT_ONLY
  11873. mail_openmail(sd);
  11874. #endif
  11875. return 0;
  11876. }
  11877. BUILDIN_FUNC(openauction)
  11878. {
  11879. TBL_PC* sd;
  11880. sd = script_rid2sd(st);
  11881. if( sd == NULL )
  11882. return 0;
  11883. #ifndef TXT_ONLY
  11884. clif_Auction_openwindow(sd);
  11885. #endif
  11886. return 0;
  11887. }
  11888. /// Retrieves the value of the specified flag of the specified cell.
  11889. ///
  11890. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11891. ///
  11892. /// @see cell_chk* constants in const.txt for the types
  11893. BUILDIN_FUNC(checkcell)
  11894. {
  11895. int m = map_mapname2mapid(script_getstr(st,2));
  11896. int x = script_getnum(st,3);
  11897. int y = script_getnum(st,4);
  11898. cell_chk type = (cell_chk)script_getnum(st,5);
  11899. script_pushint(st, map_getcell(m, x, y, type));
  11900. return 0;
  11901. }
  11902. /// Modifies flags of cells in the specified area.
  11903. ///
  11904. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11905. ///
  11906. /// @see cell_* constants in const.txt for the types
  11907. BUILDIN_FUNC(setcell)
  11908. {
  11909. int m = map_mapname2mapid(script_getstr(st,2));
  11910. int x1 = script_getnum(st,3);
  11911. int y1 = script_getnum(st,4);
  11912. int x2 = script_getnum(st,5);
  11913. int y2 = script_getnum(st,6);
  11914. cell_t type = (cell_t)script_getnum(st,7);
  11915. bool flag = (bool)script_getnum(st,8);
  11916. int x,y;
  11917. if( x1 > x2 ) swap(x1,x2);
  11918. if( y1 > y2 ) swap(y1,y2);
  11919. for( y = y1; y <= y2; ++y )
  11920. for( x = x1; x <= x2; ++x )
  11921. map_setcell(m, x, y, type, flag);
  11922. return 0;
  11923. }
  11924. /*==========================================
  11925. * Mercenary Commands
  11926. *------------------------------------------*/
  11927. BUILDIN_FUNC(mercenary_create)
  11928. {
  11929. struct map_session_data *sd;
  11930. int class_, contract_time;
  11931. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  11932. return 0;
  11933. class_ = script_getnum(st,2);
  11934. if( !merc_class(class_) )
  11935. return 0;
  11936. contract_time = script_getnum(st,3);
  11937. merc_create(sd, class_, contract_time);
  11938. return 0;
  11939. }
  11940. BUILDIN_FUNC(mercenary_heal)
  11941. {
  11942. struct map_session_data *sd = script_rid2sd(st);
  11943. int hp, sp;
  11944. if( sd == NULL || sd->md == NULL )
  11945. return 0;
  11946. hp = script_getnum(st,2);
  11947. sp = script_getnum(st,3);
  11948. status_heal(&sd->md->bl, hp, sp, 0);
  11949. return 0;
  11950. }
  11951. BUILDIN_FUNC(mercenary_sc_start)
  11952. {
  11953. struct map_session_data *sd = script_rid2sd(st);
  11954. enum sc_type type;
  11955. int tick, val1;
  11956. if( sd == NULL || sd->md == NULL )
  11957. return 0;
  11958. type = (sc_type)script_getnum(st,2);
  11959. tick = script_getnum(st,3);
  11960. val1 = script_getnum(st,4);
  11961. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  11962. return 0;
  11963. }
  11964. BUILDIN_FUNC(mercenary_get_calls)
  11965. {
  11966. struct map_session_data *sd = script_rid2sd(st);
  11967. int guild;
  11968. if( sd == NULL )
  11969. return 0;
  11970. guild = script_getnum(st,2);
  11971. switch( guild )
  11972. {
  11973. case ARCH_MERC_GUILD:
  11974. script_pushint(st,sd->status.arch_calls);
  11975. break;
  11976. case SPEAR_MERC_GUILD:
  11977. script_pushint(st,sd->status.spear_calls);
  11978. break;
  11979. case SWORD_MERC_GUILD:
  11980. script_pushint(st,sd->status.sword_calls);
  11981. break;
  11982. default:
  11983. script_pushint(st,0);
  11984. break;
  11985. }
  11986. return 0;
  11987. }
  11988. BUILDIN_FUNC(mercenary_set_calls)
  11989. {
  11990. struct map_session_data *sd = script_rid2sd(st);
  11991. int guild, value, *calls;
  11992. if( sd == NULL )
  11993. return 0;
  11994. guild = script_getnum(st,2);
  11995. value = script_getnum(st,3);
  11996. switch( guild )
  11997. {
  11998. case ARCH_MERC_GUILD:
  11999. calls = &sd->status.arch_calls;
  12000. break;
  12001. case SPEAR_MERC_GUILD:
  12002. calls = &sd->status.spear_calls;
  12003. break;
  12004. case SWORD_MERC_GUILD:
  12005. calls = &sd->status.sword_calls;
  12006. break;
  12007. default:
  12008. return 0; // Invalid Guild
  12009. }
  12010. *calls += value;
  12011. *calls = cap_value(*calls, 0, INT_MAX);
  12012. return 0;
  12013. }
  12014. BUILDIN_FUNC(mercenary_get_faith)
  12015. {
  12016. struct map_session_data *sd = script_rid2sd(st);
  12017. int guild;
  12018. if( sd == NULL )
  12019. return 0;
  12020. guild = script_getnum(st,2);
  12021. switch( guild )
  12022. {
  12023. case ARCH_MERC_GUILD:
  12024. script_pushint(st,sd->status.arch_faith);
  12025. break;
  12026. case SPEAR_MERC_GUILD:
  12027. script_pushint(st,sd->status.spear_faith);
  12028. break;
  12029. case SWORD_MERC_GUILD:
  12030. script_pushint(st,sd->status.sword_faith);
  12031. break;
  12032. default:
  12033. script_pushint(st,0);
  12034. break;
  12035. }
  12036. return 0;
  12037. }
  12038. BUILDIN_FUNC(mercenary_set_faith)
  12039. {
  12040. struct map_session_data *sd = script_rid2sd(st);
  12041. int guild, value, *calls;
  12042. if( sd == NULL )
  12043. return 0;
  12044. guild = script_getnum(st,2);
  12045. value = script_getnum(st,3);
  12046. switch( guild )
  12047. {
  12048. case ARCH_MERC_GUILD:
  12049. calls = &sd->status.arch_faith;
  12050. break;
  12051. case SPEAR_MERC_GUILD:
  12052. calls = &sd->status.spear_faith;
  12053. break;
  12054. case SWORD_MERC_GUILD:
  12055. calls = &sd->status.sword_faith;
  12056. break;
  12057. default:
  12058. return 0; // Invalid Guild
  12059. }
  12060. *calls += value;
  12061. *calls = cap_value(*calls, 0, INT_MAX);
  12062. if( mercenary_get_guild(sd->md) == guild )
  12063. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  12064. return 0;
  12065. }
  12066. /*------------------------------------------
  12067. * Book Reading
  12068. *------------------------------------------*/
  12069. BUILDIN_FUNC(readbook)
  12070. {
  12071. struct map_session_data *sd;
  12072. int book_id, page;
  12073. if( (sd = script_rid2sd(st)) == NULL )
  12074. return 0;
  12075. book_id = script_getnum(st,2);
  12076. page = script_getnum(st,3);
  12077. clif_readbook(sd->fd, book_id, page);
  12078. return 0;
  12079. }
  12080. /******************
  12081. Questlog script commands
  12082. *******************/
  12083. BUILDIN_FUNC(setquest)
  12084. {
  12085. TBL_PC * sd = script_rid2sd(st);
  12086. quest_add(sd, script_getnum(st, 2));
  12087. return 0;
  12088. }
  12089. BUILDIN_FUNC(erasequest)
  12090. {
  12091. TBL_PC * sd = script_rid2sd(st);
  12092. quest_delete(sd, script_getnum(st, 2));
  12093. return 0;
  12094. }
  12095. BUILDIN_FUNC(completequest)
  12096. {
  12097. TBL_PC * sd = script_rid2sd(st);
  12098. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  12099. return 0;
  12100. }
  12101. BUILDIN_FUNC(changequest)
  12102. {
  12103. TBL_PC * sd = script_rid2sd(st);
  12104. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  12105. return 0;
  12106. }
  12107. BUILDIN_FUNC(checkquest)
  12108. {
  12109. TBL_PC * sd = script_rid2sd(st);
  12110. quest_check_type type = HAVEQUEST;
  12111. if( script_hasdata(st, 3) )
  12112. type = (quest_check_type)script_getnum(st, 3);
  12113. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  12114. return 0;
  12115. }
  12116. BUILDIN_FUNC(showevent)
  12117. {
  12118. TBL_PC *sd = script_rid2sd(st);
  12119. struct npc_data *nd = map_id2nd(st->oid);
  12120. int state, color;
  12121. if( sd == NULL || nd == NULL )
  12122. return 0;
  12123. state = script_getnum(st, 2);
  12124. color = script_getnum(st, 3);
  12125. if( color < 0 || color > 4 )
  12126. color = 0; // set default color
  12127. clif_quest_show_event(sd, &nd->bl, state, color);
  12128. return 0;
  12129. }
  12130. /*==========================================
  12131. * BattleGround System
  12132. *------------------------------------------*/
  12133. BUILDIN_FUNC(waitingroom2bg)
  12134. {
  12135. struct npc_data *nd;
  12136. struct chat_data *cd;
  12137. const char *map_name, *ev = "", *dev = "";
  12138. int x, y, i, mapindex = 0, bg_id, n;
  12139. struct map_session_data *sd;
  12140. if( script_hasdata(st,7) )
  12141. nd = npc_name2id(script_getstr(st,7));
  12142. else
  12143. nd = (struct npc_data *)map_id2bl(st->oid);
  12144. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  12145. {
  12146. script_pushint(st,0);
  12147. return 0;
  12148. }
  12149. map_name = script_getstr(st,2);
  12150. if( strcmp(map_name,"-") != 0 )
  12151. {
  12152. mapindex = mapindex_name2id(map_name);
  12153. if( mapindex == 0 )
  12154. { // Invalid Map
  12155. script_pushint(st,0);
  12156. return 0;
  12157. }
  12158. }
  12159. x = script_getnum(st,3);
  12160. y = script_getnum(st,4);
  12161. ev = script_getstr(st,5); // Logout Event
  12162. dev = script_getstr(st,6); // Die Event
  12163. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  12164. { // Creation failed
  12165. script_pushint(st,0);
  12166. return 0;
  12167. }
  12168. n = cd->users;
  12169. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  12170. {
  12171. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  12172. mapreg_setreg(add_str("$@arenamembers") + (i<<24), sd->bl.id);
  12173. else
  12174. mapreg_setreg(add_str("$@arenamembers") + (i<<24), 0);
  12175. }
  12176. mapreg_setreg(add_str("$@arenamembersnum"), i);
  12177. script_pushint(st,bg_id);
  12178. return 0;
  12179. }
  12180. BUILDIN_FUNC(waitingroom2bg_single)
  12181. {
  12182. const char* map_name;
  12183. struct npc_data *nd;
  12184. struct chat_data *cd;
  12185. struct map_session_data *sd;
  12186. int x, y, mapindex, bg_id;
  12187. bg_id = script_getnum(st,2);
  12188. map_name = script_getstr(st,3);
  12189. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12190. return 0; // Invalid Map
  12191. x = script_getnum(st,4);
  12192. y = script_getnum(st,5);
  12193. nd = npc_name2id(script_getstr(st,6));
  12194. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  12195. return 0;
  12196. if( (sd = cd->usersd[0]) == NULL )
  12197. return 0;
  12198. if( bg_team_join(bg_id, sd) )
  12199. {
  12200. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  12201. script_pushint(st,1);
  12202. }
  12203. else
  12204. script_pushint(st,0);
  12205. return 0;
  12206. }
  12207. BUILDIN_FUNC(bg_team_setxy)
  12208. {
  12209. struct battleground_data *bg;
  12210. int bg_id;
  12211. bg_id = script_getnum(st,2);
  12212. if( (bg = bg_team_search(bg_id)) == NULL )
  12213. return 0;
  12214. bg->x = script_getnum(st,3);
  12215. bg->y = script_getnum(st,4);
  12216. return 0;
  12217. }
  12218. BUILDIN_FUNC(bg_warp)
  12219. {
  12220. int x, y, mapindex, bg_id;
  12221. const char* map_name;
  12222. bg_id = script_getnum(st,2);
  12223. map_name = script_getstr(st,3);
  12224. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12225. return 0; // Invalid Map
  12226. x = script_getnum(st,4);
  12227. y = script_getnum(st,5);
  12228. bg_team_warp(bg_id, mapindex, x, y);
  12229. return 0;
  12230. }
  12231. BUILDIN_FUNC(bg_monster)
  12232. {
  12233. int class_ = 0, x = 0, y = 0, bg_id = 0;
  12234. const char *str,*map, *evt="";
  12235. bg_id = script_getnum(st,2);
  12236. map = script_getstr(st,3);
  12237. x = script_getnum(st,4);
  12238. y = script_getnum(st,5);
  12239. str = script_getstr(st,6);
  12240. class_ = script_getnum(st,7);
  12241. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  12242. check_event(st, evt);
  12243. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  12244. return 0;
  12245. }
  12246. BUILDIN_FUNC(bg_monster_set_team)
  12247. {
  12248. struct mob_data *md;
  12249. struct block_list *mbl;
  12250. int id = script_getnum(st,2),
  12251. bg_id = script_getnum(st,3);
  12252. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  12253. return 0;
  12254. md = (TBL_MOB *)mbl;
  12255. md->state.bg_id = bg_id;
  12256. mob_stop_attack(md);
  12257. mob_stop_walking(md, 0);
  12258. md->target_id = md->attacked_id = 0;
  12259. clif_charnameack(0, &md->bl);
  12260. return 0;
  12261. }
  12262. BUILDIN_FUNC(bg_leave)
  12263. {
  12264. struct map_session_data *sd = script_rid2sd(st);
  12265. if( sd == NULL || !sd->state.bg_id )
  12266. return 0;
  12267. bg_team_leave(sd,0);
  12268. return 0;
  12269. }
  12270. BUILDIN_FUNC(bg_destroy)
  12271. {
  12272. int bg_id = script_getnum(st,2);
  12273. bg_team_delete(bg_id);
  12274. return 0;
  12275. }
  12276. BUILDIN_FUNC(bg_getareausers)
  12277. {
  12278. const char *str;
  12279. int m, x0, y0, x1, y1, bg_id;
  12280. int i = 0, c = 0;
  12281. struct battleground_data *bg = NULL;
  12282. struct map_session_data *sd;
  12283. bg_id = script_getnum(st,2);
  12284. str = script_getstr(st,3);
  12285. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  12286. {
  12287. script_pushint(st,0);
  12288. return 0;
  12289. }
  12290. x0 = script_getnum(st,4);
  12291. y0 = script_getnum(st,5);
  12292. x1 = script_getnum(st,6);
  12293. y1 = script_getnum(st,7);
  12294. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  12295. {
  12296. if( (sd = bg->members[i].sd) == NULL )
  12297. continue;
  12298. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  12299. continue;
  12300. c++;
  12301. }
  12302. script_pushint(st,c);
  12303. return 0;
  12304. }
  12305. BUILDIN_FUNC(bg_updatescore)
  12306. {
  12307. const char *str;
  12308. int m;
  12309. str = script_getstr(st,2);
  12310. if( (m = map_mapname2mapid(str)) < 0 )
  12311. return 0;
  12312. map[m].bgscore_lion = script_getnum(st,3);
  12313. map[m].bgscore_eagle = script_getnum(st,4);
  12314. clif_bg_updatescore(m);
  12315. return 0;
  12316. }
  12317. BUILDIN_FUNC(bg_get_data)
  12318. {
  12319. struct battleground_data *bg;
  12320. int bg_id = script_getnum(st,2),
  12321. type = script_getnum(st,3);
  12322. if( (bg = bg_team_search(bg_id)) == NULL )
  12323. {
  12324. script_pushint(st,0);
  12325. return 0;
  12326. }
  12327. switch( type )
  12328. {
  12329. case 0: script_pushint(st, bg->count); break;
  12330. default:
  12331. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  12332. break;
  12333. }
  12334. return 0;
  12335. }
  12336. /*==========================================
  12337. * Instancing Script Commands
  12338. *------------------------------------------*/
  12339. BUILDIN_FUNC(instance_create)
  12340. {
  12341. const char *name;
  12342. int party_id, res;
  12343. name = script_getstr(st, 2);
  12344. party_id = script_getnum(st, 3);
  12345. res = instance_create(party_id, name);
  12346. if( res == -4 ) // Already exists
  12347. {
  12348. script_pushint(st, -1);
  12349. return 0;
  12350. }
  12351. else if( res < 0 )
  12352. {
  12353. char *err;
  12354. switch(res)
  12355. {
  12356. case -3: err = "No free instances"; break;
  12357. case -2: err = "Missing parameter"; break;
  12358. case -1: err = "Invalid type"; break;
  12359. default: err = "Unknown"; break;
  12360. }
  12361. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  12362. script_pushint(st, -2);
  12363. return 0;
  12364. }
  12365. script_pushint(st, res);
  12366. return 0;
  12367. }
  12368. BUILDIN_FUNC(instance_destroy)
  12369. {
  12370. int instance_id;
  12371. struct map_session_data *sd;
  12372. struct party_data *p;
  12373. if( script_hasdata(st, 2) )
  12374. instance_id = script_getnum(st, 2);
  12375. else if( st->instance_id )
  12376. instance_id = st->instance_id;
  12377. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12378. instance_id = p->instance_id;
  12379. else return 0;
  12380. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12381. {
  12382. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  12383. return 0;
  12384. }
  12385. instance_destroy(instance_id);
  12386. return 0;
  12387. }
  12388. BUILDIN_FUNC(instance_attachmap)
  12389. {
  12390. const char *name;
  12391. int m;
  12392. int instance_id;
  12393. bool usebasename = false;
  12394. name = script_getstr(st,2);
  12395. instance_id = script_getnum(st,3);
  12396. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  12397. usebasename = true;
  12398. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  12399. {
  12400. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  12401. script_pushconststr(st, "");
  12402. return 0;
  12403. }
  12404. script_pushconststr(st, map[m].name);
  12405. return 0;
  12406. }
  12407. BUILDIN_FUNC(instance_detachmap)
  12408. {
  12409. struct map_session_data *sd;
  12410. struct party_data *p;
  12411. const char *str;
  12412. int m, instance_id;
  12413. str = script_getstr(st, 2);
  12414. if( script_hasdata(st, 3) )
  12415. instance_id = script_getnum(st, 3);
  12416. else if( st->instance_id )
  12417. instance_id = st->instance_id;
  12418. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12419. instance_id = p->instance_id;
  12420. else return 0;
  12421. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  12422. {
  12423. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  12424. return 0;
  12425. }
  12426. instance_del_map(m);
  12427. return 0;
  12428. }
  12429. BUILDIN_FUNC(instance_attach)
  12430. {
  12431. int instance_id;
  12432. instance_id = script_getnum(st, 2);
  12433. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12434. return 0;
  12435. st->instance_id = instance_id;
  12436. return 0;
  12437. }
  12438. BUILDIN_FUNC(instance_id)
  12439. {
  12440. int type, instance_id;
  12441. struct map_session_data *sd;
  12442. struct party_data *p;
  12443. if( script_hasdata(st, 2) )
  12444. {
  12445. type = script_getnum(st, 2);
  12446. if( type == 0 )
  12447. instance_id = st->instance_id;
  12448. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  12449. instance_id = p->instance_id;
  12450. else
  12451. instance_id = 0;
  12452. }
  12453. else
  12454. instance_id = st->instance_id;
  12455. script_pushint(st, instance_id);
  12456. return 0;
  12457. }
  12458. BUILDIN_FUNC(instance_set_timeout)
  12459. {
  12460. int progress_timeout, idle_timeout;
  12461. int instance_id;
  12462. struct map_session_data *sd;
  12463. struct party_data *p;
  12464. progress_timeout = script_getnum(st, 2);
  12465. idle_timeout = script_getnum(st, 3);
  12466. if( script_hasdata(st, 4) )
  12467. instance_id = script_getnum(st, 4);
  12468. else if( st->instance_id )
  12469. instance_id = st->instance_id;
  12470. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12471. instance_id = p->instance_id;
  12472. else return 0;
  12473. if( instance_id > 0 )
  12474. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  12475. return 0;
  12476. }
  12477. BUILDIN_FUNC(instance_init)
  12478. {
  12479. int instance_id = script_getnum(st, 2);
  12480. if( instance[instance_id].state != INSTANCE_IDLE )
  12481. {
  12482. ShowError("instance_init: instance already initialized.\n");
  12483. return 0;
  12484. }
  12485. instance_init(instance_id);
  12486. return 0;
  12487. }
  12488. BUILDIN_FUNC(instance_announce)
  12489. {
  12490. int instance_id = script_getnum(st,2);
  12491. const char *mes = script_getstr(st,3);
  12492. int flag = script_getnum(st,4);
  12493. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  12494. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  12495. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  12496. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  12497. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  12498. int i;
  12499. struct map_session_data *sd;
  12500. struct party_data *p;
  12501. if( instance_id == 0 )
  12502. {
  12503. if( st->instance_id )
  12504. instance_id = st->instance_id;
  12505. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12506. instance_id = p->instance_id;
  12507. else return 0;
  12508. }
  12509. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12510. return 0;
  12511. for( i = 0; i < instance[instance_id].num_map; i++ )
  12512. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  12513. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  12514. return 0;
  12515. }
  12516. BUILDIN_FUNC(instance_npcname)
  12517. {
  12518. const char *str;
  12519. int instance_id = 0;
  12520. struct map_session_data *sd;
  12521. struct party_data *p;
  12522. struct npc_data *nd;
  12523. str = script_getstr(st, 2);
  12524. if( script_hasdata(st, 3) )
  12525. instance_id = script_getnum(st, 3);
  12526. else if( st->instance_id )
  12527. instance_id = st->instance_id;
  12528. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12529. instance_id = p->instance_id;
  12530. if( instance_id && (nd = npc_name2id(str)) != NULL )
  12531. {
  12532. static char npcname[NAME_LENGTH];
  12533. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  12534. script_pushconststr(st,npcname);
  12535. }
  12536. else
  12537. {
  12538. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  12539. st->state = END;
  12540. return 1;
  12541. }
  12542. return 0;
  12543. }
  12544. BUILDIN_FUNC(has_instance)
  12545. {
  12546. struct map_session_data *sd;
  12547. struct party_data *p;
  12548. const char *str;
  12549. int m, instance_id = 0;
  12550. str = script_getstr(st, 2);
  12551. if( script_hasdata(st, 3) )
  12552. instance_id = script_getnum(st, 3);
  12553. else if( st->instance_id )
  12554. instance_id = st->instance_id;
  12555. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12556. instance_id = p->instance_id;
  12557. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  12558. {
  12559. script_pushconststr(st, "");
  12560. return 0;
  12561. }
  12562. script_pushconststr(st, map[m].name);
  12563. return 0;
  12564. }
  12565. BUILDIN_FUNC(instance_warpall)
  12566. {
  12567. struct map_session_data *pl_sd;
  12568. int m, i, instance_id;
  12569. const char *mapn;
  12570. int x, y;
  12571. unsigned short mapindex;
  12572. struct party_data *p = NULL;
  12573. mapn = script_getstr(st,2);
  12574. x = script_getnum(st,3);
  12575. y = script_getnum(st,4);
  12576. if( script_hasdata(st,5) )
  12577. instance_id = script_getnum(st,5);
  12578. else if( st->instance_id )
  12579. instance_id = st->instance_id;
  12580. 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 )
  12581. instance_id = p->instance_id;
  12582. else return 0;
  12583. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  12584. return 0;
  12585. if( !(p = party_search(instance[instance_id].party_id)) )
  12586. return 0;
  12587. mapindex = map_id2index(m);
  12588. for( i = 0; i < MAX_PARTY; i++ )
  12589. 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);
  12590. return 0;
  12591. }
  12592. /*==========================================
  12593. * Custom Fonts
  12594. *------------------------------------------*/
  12595. BUILDIN_FUNC(setfont)
  12596. {
  12597. struct map_session_data *sd = script_rid2sd(st);
  12598. int font = script_getnum(st,2);
  12599. if( sd == NULL )
  12600. return 0;
  12601. if( sd->state.user_font != font )
  12602. sd->state.user_font = font;
  12603. else
  12604. sd->state.user_font = 0;
  12605. clif_font(sd);
  12606. return 0;
  12607. }
  12608. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  12609. {
  12610. TBL_MOB* md = (TBL_MOB*)bl;
  12611. struct block_list *tbl;
  12612. int mobid = va_arg(ap,int);
  12613. int skillid = va_arg(ap,int);
  12614. int skilllv = va_arg(ap,int);
  12615. int casttime = va_arg(ap,int);
  12616. int cancel = va_arg(ap,int);
  12617. int emotion = va_arg(ap,int);
  12618. int target = va_arg(ap,int);
  12619. if( md->class_ != mobid )
  12620. return 0;
  12621. if( md->ud.skilltimer != -1 ) // Cancel the casting skill.
  12622. unit_skillcastcancel(bl,0);
  12623. // 0:self, 1:target, 2:master, default:random
  12624. switch( target )
  12625. {
  12626. case 0: tbl = map_id2bl(md->bl.id); break;
  12627. case 1: tbl = map_id2bl(md->target_id); break;
  12628. case 2: tbl = map_id2bl(md->master_id); break;
  12629. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  12630. }
  12631. if( !tbl )
  12632. return 0;
  12633. if( skill_get_casttype(skillid) == CAST_GROUND )
  12634. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  12635. else
  12636. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  12637. clif_emotion(&md->bl, emotion);
  12638. return 0;
  12639. }
  12640. /*==========================================
  12641. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  12642. *------------------------------------------*/
  12643. BUILDIN_FUNC(areamobuseskill)
  12644. {
  12645. struct block_list center;
  12646. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  12647. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  12648. {
  12649. ShowError("areamobuseskill: invalid map name.\n");
  12650. return 0;
  12651. }
  12652. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  12653. return 0;
  12654. center.m = m;
  12655. center.x = script_getnum(st,3);
  12656. center.y = script_getnum(st,4);
  12657. range = script_getnum(st,5);
  12658. mobid = script_getnum(st,6);
  12659. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  12660. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  12661. skilllv = battle_config.mob_max_skilllvl;
  12662. casttime = script_getnum(st,9);
  12663. cancel = script_getnum(st,10);
  12664. emotion = script_getnum(st,11);
  12665. target = script_getnum(st,12);
  12666. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  12667. return 0;
  12668. }
  12669. BUILDIN_FUNC(progressbar)
  12670. {
  12671. #if PACKETVER >= 20080318
  12672. struct map_session_data * sd = script_rid2sd(st);
  12673. const char * color;
  12674. unsigned int second;
  12675. if( !st || !sd )
  12676. return 0;
  12677. st->state = STOP;
  12678. color = script_getstr(st,2);
  12679. second = script_getnum(st,3);
  12680. sd->progressbar.npc_id = st->oid;
  12681. sd->progressbar.timeout = gettick() + second*1000;
  12682. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  12683. #endif
  12684. return 0;
  12685. }
  12686. BUILDIN_FUNC(pushpc)
  12687. {
  12688. int direction, cells, dx, dy;
  12689. struct map_session_data* sd;
  12690. if((sd = script_rid2sd(st))==NULL)
  12691. {
  12692. return 0;
  12693. }
  12694. direction = script_getnum(st,2);
  12695. cells = script_getnum(st,3);
  12696. if(direction<0 || direction>7)
  12697. {
  12698. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  12699. script_reportsrc(st);
  12700. direction%= 8; // trim spin-over
  12701. }
  12702. if(!cells)
  12703. {// zero distance
  12704. return 0;
  12705. }
  12706. else if(cells<0)
  12707. {// pushing backwards
  12708. direction = (direction+4)%8; // turn around
  12709. cells = -cells;
  12710. }
  12711. dx = dirx[direction];
  12712. dy = diry[direction];
  12713. unit_blown(&sd->bl, dx, dy, cells, 0);
  12714. return 0;
  12715. }
  12716. // declarations that were supposed to be exported from npc_chat.c
  12717. #ifdef PCRE_SUPPORT
  12718. BUILDIN_FUNC(defpattern);
  12719. BUILDIN_FUNC(activatepset);
  12720. BUILDIN_FUNC(deactivatepset);
  12721. BUILDIN_FUNC(deletepset);
  12722. #endif
  12723. /// script command definitions
  12724. /// for an explanation on args, see add_buildin_func
  12725. struct script_function buildin_func[] = {
  12726. // NPC interaction
  12727. BUILDIN_DEF(mes,"s"),
  12728. BUILDIN_DEF(next,""),
  12729. BUILDIN_DEF(close,""),
  12730. BUILDIN_DEF(close2,""),
  12731. BUILDIN_DEF(menu,"sl*"),
  12732. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  12733. BUILDIN_DEF(prompt,"s*"),
  12734. //
  12735. BUILDIN_DEF(goto,"l"),
  12736. BUILDIN_DEF(callsub,"l*"),
  12737. BUILDIN_DEF(callfunc,"s*"),
  12738. BUILDIN_DEF(return,"?"),
  12739. BUILDIN_DEF(getarg,"i?"),
  12740. BUILDIN_DEF(jobchange,"i?"),
  12741. BUILDIN_DEF(jobname,"i"),
  12742. BUILDIN_DEF(input,"r??"),
  12743. BUILDIN_DEF(warp,"sii"),
  12744. BUILDIN_DEF(areawarp,"siiiisii"),
  12745. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  12746. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  12747. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  12748. BUILDIN_DEF(setlook,"ii"),
  12749. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  12750. BUILDIN_DEF(set,"rv"),
  12751. BUILDIN_DEF(setarray,"rv*"),
  12752. BUILDIN_DEF(cleararray,"rvi"),
  12753. BUILDIN_DEF(copyarray,"rri"),
  12754. BUILDIN_DEF(getarraysize,"r"),
  12755. BUILDIN_DEF(deletearray,"r?"),
  12756. BUILDIN_DEF(getelementofarray,"ri"),
  12757. BUILDIN_DEF(getitem,"vi?"),
  12758. BUILDIN_DEF(rentitem,"vi"),
  12759. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  12760. BUILDIN_DEF(getnameditem,"vv"),
  12761. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  12762. BUILDIN_DEF(makeitem,"visii"),
  12763. BUILDIN_DEF(delitem,"vi?"),
  12764. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  12765. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  12766. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  12767. BUILDIN_DEF(cutin,"si"),
  12768. BUILDIN_DEF(viewpoint,"iiiii"),
  12769. BUILDIN_DEF(heal,"ii"),
  12770. BUILDIN_DEF(itemheal,"ii"),
  12771. BUILDIN_DEF(percentheal,"ii"),
  12772. BUILDIN_DEF(rand,"i?"),
  12773. BUILDIN_DEF(countitem,"v"),
  12774. BUILDIN_DEF(countitem2,"viiiiiii"),
  12775. BUILDIN_DEF(checkweight,"vi"),
  12776. BUILDIN_DEF(readparam,"i?"),
  12777. BUILDIN_DEF(getcharid,"i?"),
  12778. BUILDIN_DEF(getpartyname,"i"),
  12779. BUILDIN_DEF(getpartymember,"i?"),
  12780. BUILDIN_DEF(getpartyleader,"i?"),
  12781. BUILDIN_DEF(getguildname,"i"),
  12782. BUILDIN_DEF(getguildmaster,"i"),
  12783. BUILDIN_DEF(getguildmasterid,"i"),
  12784. BUILDIN_DEF(strcharinfo,"i"),
  12785. BUILDIN_DEF(strnpcinfo,"i"),
  12786. BUILDIN_DEF(getequipid,"i"),
  12787. BUILDIN_DEF(getequipname,"i"),
  12788. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  12789. BUILDIN_DEF(repair,"i"), // [Valaris]
  12790. BUILDIN_DEF(getequipisequiped,"i"),
  12791. BUILDIN_DEF(getequipisenableref,"i"),
  12792. BUILDIN_DEF(getequipisidentify,"i"),
  12793. BUILDIN_DEF(getequiprefinerycnt,"i"),
  12794. BUILDIN_DEF(getequipweaponlv,"i"),
  12795. BUILDIN_DEF(getequippercentrefinery,"i"),
  12796. BUILDIN_DEF(successrefitem,"i"),
  12797. BUILDIN_DEF(failedrefitem,"i"),
  12798. BUILDIN_DEF(statusup,"i"),
  12799. BUILDIN_DEF(statusup2,"ii"),
  12800. BUILDIN_DEF(bonus,"iv"),
  12801. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  12802. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  12803. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  12804. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  12805. BUILDIN_DEF(autobonus,"sii??"),
  12806. BUILDIN_DEF(autobonus2,"sii??"),
  12807. BUILDIN_DEF(autobonus3,"siiv?"),
  12808. BUILDIN_DEF(skill,"vi?"),
  12809. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  12810. BUILDIN_DEF(guildskill,"vi"),
  12811. BUILDIN_DEF(getskilllv,"v"),
  12812. BUILDIN_DEF(getgdskilllv,"iv"),
  12813. BUILDIN_DEF(basicskillcheck,""),
  12814. BUILDIN_DEF(getgmlevel,""),
  12815. BUILDIN_DEF(end,""),
  12816. BUILDIN_DEF(checkoption,"i"),
  12817. BUILDIN_DEF(setoption,"i?"),
  12818. BUILDIN_DEF(setcart,"?"),
  12819. BUILDIN_DEF(checkcart,""),
  12820. BUILDIN_DEF(setfalcon,"?"),
  12821. BUILDIN_DEF(checkfalcon,""),
  12822. BUILDIN_DEF(setriding,"?"),
  12823. BUILDIN_DEF(checkriding,""),
  12824. BUILDIN_DEF2(savepoint,"save","sii"),
  12825. BUILDIN_DEF(savepoint,"sii"),
  12826. BUILDIN_DEF(gettimetick,"i"),
  12827. BUILDIN_DEF(gettime,"i"),
  12828. BUILDIN_DEF(gettimestr,"si"),
  12829. BUILDIN_DEF(openstorage,""),
  12830. BUILDIN_DEF(guildopenstorage,""),
  12831. BUILDIN_DEF(itemskill,"vi"),
  12832. BUILDIN_DEF(produce,"i"),
  12833. BUILDIN_DEF(cooking,"i"),
  12834. BUILDIN_DEF(monster,"siisii?"),
  12835. BUILDIN_DEF(getmobdrops,"i"),
  12836. BUILDIN_DEF(areamonster,"siiiisii?"),
  12837. BUILDIN_DEF(killmonster,"ss?"),
  12838. BUILDIN_DEF(killmonsterall,"s?"),
  12839. BUILDIN_DEF(clone,"siisi????"),
  12840. BUILDIN_DEF(doevent,"s"),
  12841. BUILDIN_DEF(donpcevent,"s"),
  12842. BUILDIN_DEF(cmdothernpc,"ss"),
  12843. BUILDIN_DEF(addtimer,"is"),
  12844. BUILDIN_DEF(deltimer,"s"),
  12845. BUILDIN_DEF(addtimercount,"si"),
  12846. BUILDIN_DEF(initnpctimer,"??"),
  12847. BUILDIN_DEF(stopnpctimer,"??"),
  12848. BUILDIN_DEF(startnpctimer,"??"),
  12849. BUILDIN_DEF(setnpctimer,"i?"),
  12850. BUILDIN_DEF(getnpctimer,"i?"),
  12851. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  12852. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  12853. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  12854. BUILDIN_DEF(announce,"si?????"),
  12855. BUILDIN_DEF(mapannounce,"ssi?????"),
  12856. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  12857. BUILDIN_DEF(getusers,"i"),
  12858. BUILDIN_DEF(getmapguildusers,"si"),
  12859. BUILDIN_DEF(getmapusers,"s"),
  12860. BUILDIN_DEF(getareausers,"siiii"),
  12861. BUILDIN_DEF(getareadropitem,"siiiiv"),
  12862. BUILDIN_DEF(enablenpc,"s"),
  12863. BUILDIN_DEF(disablenpc,"s"),
  12864. BUILDIN_DEF(hideoffnpc,"s"),
  12865. BUILDIN_DEF(hideonnpc,"s"),
  12866. BUILDIN_DEF(sc_start,"iii?"),
  12867. BUILDIN_DEF(sc_start2,"iiii?"),
  12868. BUILDIN_DEF(sc_start4,"iiiiii?"),
  12869. BUILDIN_DEF(sc_end,"i?"),
  12870. BUILDIN_DEF(getscrate,"ii?"),
  12871. BUILDIN_DEF(debugmes,"s"),
  12872. BUILDIN_DEF2(catchpet,"pet","i"),
  12873. BUILDIN_DEF2(birthpet,"bpet",""),
  12874. BUILDIN_DEF(resetlvl,"i"),
  12875. BUILDIN_DEF(resetstatus,""),
  12876. BUILDIN_DEF(resetskill,""),
  12877. BUILDIN_DEF(skillpointcount,""),
  12878. BUILDIN_DEF(changebase,"i?"),
  12879. BUILDIN_DEF(changesex,""),
  12880. BUILDIN_DEF(waitingroom,"si??"),
  12881. BUILDIN_DEF(delwaitingroom,"?"),
  12882. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  12883. BUILDIN_DEF(enablewaitingroomevent,"?"),
  12884. BUILDIN_DEF(disablewaitingroomevent,"?"),
  12885. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  12886. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  12887. BUILDIN_DEF(getwaitingroomstate,"i?"),
  12888. BUILDIN_DEF(warpwaitingpc,"sii?"),
  12889. BUILDIN_DEF(attachrid,"i"),
  12890. BUILDIN_DEF(detachrid,""),
  12891. BUILDIN_DEF(isloggedin,"i?"),
  12892. BUILDIN_DEF(setmapflagnosave,"ssii"),
  12893. BUILDIN_DEF(getmapflag,"si"),
  12894. BUILDIN_DEF(setmapflag,"si?"),
  12895. BUILDIN_DEF(removemapflag,"si"),
  12896. BUILDIN_DEF(pvpon,"s"),
  12897. BUILDIN_DEF(pvpoff,"s"),
  12898. BUILDIN_DEF(gvgon,"s"),
  12899. BUILDIN_DEF(gvgoff,"s"),
  12900. BUILDIN_DEF(emotion,"i??"),
  12901. BUILDIN_DEF(maprespawnguildid,"sii"),
  12902. BUILDIN_DEF(agitstart,""), // <Agit>
  12903. BUILDIN_DEF(agitend,""),
  12904. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  12905. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  12906. BUILDIN_DEF(getcastlename,"s"),
  12907. BUILDIN_DEF(getcastledata,"si?"),
  12908. BUILDIN_DEF(setcastledata,"sii"),
  12909. BUILDIN_DEF(requestguildinfo,"i?"),
  12910. BUILDIN_DEF(getequipcardcnt,"i"),
  12911. BUILDIN_DEF(successremovecards,"i"),
  12912. BUILDIN_DEF(failedremovecards,"ii"),
  12913. BUILDIN_DEF(marriage,"s"),
  12914. BUILDIN_DEF2(wedding_effect,"wedding",""),
  12915. BUILDIN_DEF(divorce,""),
  12916. BUILDIN_DEF(ispartneron,""),
  12917. BUILDIN_DEF(getpartnerid,""),
  12918. BUILDIN_DEF(getchildid,""),
  12919. BUILDIN_DEF(getmotherid,""),
  12920. BUILDIN_DEF(getfatherid,""),
  12921. BUILDIN_DEF(warppartner,"sii"),
  12922. BUILDIN_DEF(getitemname,"v"),
  12923. BUILDIN_DEF(getitemslots,"i"),
  12924. BUILDIN_DEF(makepet,"i"),
  12925. BUILDIN_DEF(getexp,"ii"),
  12926. BUILDIN_DEF(getinventorylist,""),
  12927. BUILDIN_DEF(getskilllist,""),
  12928. BUILDIN_DEF(clearitem,""),
  12929. BUILDIN_DEF(classchange,"ii"),
  12930. BUILDIN_DEF(misceffect,"i"),
  12931. BUILDIN_DEF(playBGM,"s"),
  12932. BUILDIN_DEF(playBGMall,"s?????"),
  12933. BUILDIN_DEF(soundeffect,"si"),
  12934. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  12935. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  12936. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  12937. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  12938. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  12939. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  12940. BUILDIN_DEF(petloot,"i"), // [Valaris]
  12941. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  12942. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  12943. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  12944. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  12945. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  12946. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  12947. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  12948. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  12949. BUILDIN_DEF(nude,""), // nude command [Valaris]
  12950. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  12951. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  12952. BUILDIN_DEF(charcommand,"s"), // [MouseJstr]
  12953. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  12954. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  12955. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  12956. BUILDIN_DEF(mobcount,"ss"),
  12957. BUILDIN_DEF(getlook,"i"),
  12958. BUILDIN_DEF(getsavepoint,"i"),
  12959. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  12960. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  12961. BUILDIN_DEF(npcstop,""), // [Valaris]
  12962. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  12963. BUILDIN_DEF(checkoption1,"i"),
  12964. BUILDIN_DEF(checkoption2,"i"),
  12965. BUILDIN_DEF(guildgetexp,"i"),
  12966. BUILDIN_DEF(guildchangegm,"is"),
  12967. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  12968. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  12969. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  12970. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  12971. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  12972. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  12973. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  12974. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  12975. BUILDIN_DEF(night,""), // sets the server to night time
  12976. BUILDIN_DEF(day,""), // sets the server to day time
  12977. #ifdef PCRE_SUPPORT
  12978. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  12979. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  12980. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  12981. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  12982. #endif
  12983. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  12984. BUILDIN_DEF(getusersname,""),
  12985. BUILDIN_DEF(recovery,""),
  12986. BUILDIN_DEF(getpetinfo,"i"),
  12987. BUILDIN_DEF(gethominfo,"i"),
  12988. BUILDIN_DEF(checkequipedcard,"i"),
  12989. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  12990. BUILDIN_DEF(globalmes,"s?"),
  12991. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  12992. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  12993. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  12994. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  12995. BUILDIN_DEF(setnpcdisplay,"sv??"),
  12996. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  12997. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  12998. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  12999. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  13000. // [zBuffer] List of mathematics commands --->
  13001. BUILDIN_DEF(sqrt,"i"),
  13002. BUILDIN_DEF(pow,"ii"),
  13003. BUILDIN_DEF(distance,"iiii"),
  13004. // <--- [zBuffer] List of mathematics commands
  13005. BUILDIN_DEF(md5,"s"),
  13006. // [zBuffer] List of dynamic var commands --->
  13007. BUILDIN_DEF(getd,"s"),
  13008. BUILDIN_DEF(setd,"sv"),
  13009. // <--- [zBuffer] List of dynamic var commands
  13010. BUILDIN_DEF(petstat,"i"),
  13011. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  13012. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  13013. BUILDIN_DEF(npcshopadditem,"sii*"),
  13014. BUILDIN_DEF(npcshopdelitem,"si*"),
  13015. BUILDIN_DEF(npcshopattach,"s?"),
  13016. BUILDIN_DEF(equip,"i"),
  13017. BUILDIN_DEF(autoequip,"ii"),
  13018. BUILDIN_DEF(setbattleflag,"si"),
  13019. BUILDIN_DEF(getbattleflag,"s"),
  13020. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  13021. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  13022. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  13023. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  13024. BUILDIN_DEF(axtoi,"s"),
  13025. BUILDIN_DEF(query_sql,"s*"),
  13026. BUILDIN_DEF(query_logsql,"s*"),
  13027. BUILDIN_DEF(escape_sql,"s"),
  13028. BUILDIN_DEF(atoi,"s"),
  13029. // [zBuffer] List of player cont commands --->
  13030. BUILDIN_DEF(rid2name,"i"),
  13031. BUILDIN_DEF(pcfollow,"ii"),
  13032. BUILDIN_DEF(pcstopfollow,"i"),
  13033. BUILDIN_DEF(pcblockmove,"ii"),
  13034. // <--- [zBuffer] List of player cont commands
  13035. // [zBuffer] List of mob control commands --->
  13036. BUILDIN_DEF(unitwalk,"ii?"),
  13037. BUILDIN_DEF(unitkill,"i"),
  13038. BUILDIN_DEF(unitwarp,"isii"),
  13039. BUILDIN_DEF(unitattack,"iv?"),
  13040. BUILDIN_DEF(unitstop,"i"),
  13041. BUILDIN_DEF(unittalk,"is"),
  13042. BUILDIN_DEF(unitemote,"ii"),
  13043. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  13044. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  13045. // <--- [zBuffer] List of mob control commands
  13046. BUILDIN_DEF(sleep,"i"),
  13047. BUILDIN_DEF(sleep2,"i"),
  13048. BUILDIN_DEF(awake,"s"),
  13049. BUILDIN_DEF(getvariableofnpc,"rs"),
  13050. BUILDIN_DEF(warpportal,"iisii"),
  13051. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  13052. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  13053. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  13054. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  13055. BUILDIN_DEF(checkvending,"?"),
  13056. BUILDIN_DEF(checkchatting,"?"),
  13057. BUILDIN_DEF(openmail,""),
  13058. BUILDIN_DEF(openauction,""),
  13059. BUILDIN_DEF(checkcell,"siii"),
  13060. BUILDIN_DEF(setcell,"siiiiii"),
  13061. BUILDIN_DEF(setwall,"siiiiis"),
  13062. BUILDIN_DEF(delwall,"s"),
  13063. BUILDIN_DEF(searchitem,"rs"),
  13064. BUILDIN_DEF(mercenary_create,"ii"),
  13065. BUILDIN_DEF(mercenary_heal,"ii"),
  13066. BUILDIN_DEF(mercenary_sc_start,"iii"),
  13067. BUILDIN_DEF(mercenary_get_calls,"i"),
  13068. BUILDIN_DEF(mercenary_get_faith,"i"),
  13069. BUILDIN_DEF(mercenary_set_calls,"ii"),
  13070. BUILDIN_DEF(mercenary_set_faith,"ii"),
  13071. BUILDIN_DEF(readbook,"ii"),
  13072. BUILDIN_DEF(setfont,"i"),
  13073. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  13074. BUILDIN_DEF(progressbar,"si"),
  13075. BUILDIN_DEF(pushpc,"ii"),
  13076. // WoE SE
  13077. BUILDIN_DEF(agitstart2,""),
  13078. BUILDIN_DEF(agitend2,""),
  13079. BUILDIN_DEF(agitcheck2,""),
  13080. // BattleGround
  13081. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  13082. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  13083. BUILDIN_DEF(bg_team_setxy,"iii"),
  13084. BUILDIN_DEF(bg_warp,"isii"),
  13085. BUILDIN_DEF(bg_monster,"isiisi?"),
  13086. BUILDIN_DEF(bg_monster_set_team,"ii"),
  13087. BUILDIN_DEF(bg_leave,""),
  13088. BUILDIN_DEF(bg_destroy,"i"),
  13089. BUILDIN_DEF(areapercentheal,"siiiiii"),
  13090. BUILDIN_DEF(bg_get_data,"ii"),
  13091. BUILDIN_DEF(bg_getareausers,"isiiii"),
  13092. BUILDIN_DEF(bg_updatescore,"sii"),
  13093. // Instancing
  13094. BUILDIN_DEF(instance_create,"si"),
  13095. BUILDIN_DEF(instance_destroy,"?"),
  13096. BUILDIN_DEF(instance_attachmap,"si?"),
  13097. BUILDIN_DEF(instance_detachmap,"s?"),
  13098. BUILDIN_DEF(instance_attach,"i"),
  13099. BUILDIN_DEF(instance_id,"?"),
  13100. BUILDIN_DEF(instance_set_timeout,"ii?"),
  13101. BUILDIN_DEF(instance_init,"i"),
  13102. BUILDIN_DEF(instance_announce,"isi?????"),
  13103. BUILDIN_DEF(instance_npcname,"s?"),
  13104. BUILDIN_DEF(has_instance,"s?"),
  13105. BUILDIN_DEF(instance_warpall,"sii?"),
  13106. //Quest Log System [Inkfish]
  13107. BUILDIN_DEF(setquest, "i"),
  13108. BUILDIN_DEF(erasequest, "i"),
  13109. BUILDIN_DEF(completequest, "i"),
  13110. BUILDIN_DEF(checkquest, "i?"),
  13111. BUILDIN_DEF(changequest, "ii"),
  13112. BUILDIN_DEF(showevent, "ii"),
  13113. {NULL,NULL,NULL},
  13114. };