script.c 397 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. //#define DEBUG_DUMP_STACK
  8. #include "../common/cbasetypes.h"
  9. #include "../common/malloc.h"
  10. #include "../common/md5calc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/random.h"
  14. #include "../common/showmsg.h"
  15. #include "../common/strlib.h"
  16. #include "../common/timer.h"
  17. #include "../common/utils.h"
  18. #include "map.h"
  19. #include "path.h"
  20. #include "clif.h"
  21. #include "chrif.h"
  22. #include "itemdb.h"
  23. #include "pc.h"
  24. #include "status.h"
  25. #include "storage.h"
  26. #include "mob.h"
  27. #include "npc.h"
  28. #include "pet.h"
  29. #include "mapreg.h"
  30. #include "homunculus.h"
  31. #include "instance.h"
  32. #include "mercenary.h"
  33. #include "intif.h"
  34. #include "skill.h"
  35. #include "status.h"
  36. #include "chat.h"
  37. #include "battle.h"
  38. #include "battleground.h"
  39. #include "party.h"
  40. #include "guild.h"
  41. #include "atcommand.h"
  42. #include "log.h"
  43. #include "unit.h"
  44. #include "pet.h"
  45. #include "mail.h"
  46. #include "script.h"
  47. #include "quest.h"
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <math.h>
  52. #ifndef WIN32
  53. #include <sys/time.h>
  54. #endif
  55. #include <time.h>
  56. #include <setjmp.h>
  57. #include <errno.h>
  58. ///////////////////////////////////////////////////////////////////////////////
  59. //## TODO possible enhancements: [FlavioJS]
  60. // - 'callfunc' supporting labels in the current npc "::LabelName"
  61. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  62. // - 'function FuncName;' function declarations reverting to global functions
  63. // if local label isn't found
  64. // - join callfunc and callsub's functionality
  65. // - remove dynamic allocation in add_word()
  66. // - remove GETVALUE / SETVALUE
  67. // - clean up the set_reg / set_val / setd_sub mess
  68. // - detect invalid label references at parse-time
  69. //
  70. // struct script_state* st;
  71. //
  72. /// Returns the script_data at the target index
  73. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  74. /// Returns if the stack contains data at the target index
  75. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  76. /// Returns the index of the last data in the stack
  77. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  78. /// Pushes an int into the stack
  79. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  80. /// Pushes a string into the stack (script engine frees it automatically)
  81. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  82. /// Pushes a copy of a string into the stack
  83. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  84. /// Pushes a constant string into the stack (must never change or be freed)
  85. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  86. /// Pushes a nil into the stack
  87. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  88. /// Pushes a copy of the data in the target index
  89. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  90. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  91. #define script_isint(st,i) data_isint(script_getdata(st,i))
  92. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  93. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  94. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  95. // Note: "top" functions/defines use indexes relative to the top of the stack
  96. // -1 is the index of the data at the top
  97. /// Returns the script_data at the target index relative to the top of the stack
  98. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  99. /// Pushes a copy of the data in the target index relative to the top of the stack
  100. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  101. /// Removes the range of values [start,end[ relative to the top of the stack
  102. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  103. //
  104. // struct script_data* data;
  105. //
  106. /// Returns if the script data is a string
  107. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  108. /// Returns if the script data is an int
  109. #define data_isint(data) ( (data)->type == C_INT )
  110. /// Returns if the script data is a reference
  111. #define data_isreference(data) ( (data)->type == C_NAME )
  112. /// Returns if the script data is a label
  113. #define data_islabel(data) ( (data)->type == C_POS )
  114. /// Returns if the script data is an internal script function label
  115. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  116. /// Returns if this is a reference to a constant
  117. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  118. /// Returns if this a reference to a param
  119. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  120. /// Returns if this a reference to a variable
  121. //##TODO confirm it's C_NAME [FlavioJS]
  122. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  123. /// Returns the unique id of the reference (id and index)
  124. #define reference_getuid(data) ( (data)->u.num )
  125. /// Returns the id of the reference
  126. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  127. /// Returns the array index of the reference
  128. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  129. /// Returns the name of the reference
  130. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  131. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  132. #define reference_getref(data) ( (data)->ref )
  133. /// Returns the value of the constant
  134. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  135. /// Returns the type of param
  136. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  137. /// Composes the uid of a reference from the id and the index
  138. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  139. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  140. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  141. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  142. #define FETCH(n, t) \
  143. if( script_hasdata(st,n) ) \
  144. (t)=script_getnum(st,n);
  145. /// Maximum amount of elements in script arrays
  146. #define SCRIPT_MAX_ARRAYSIZE 128
  147. #define SCRIPT_BLOCK_SIZE 512
  148. enum { LABEL_NEXTLINE=1,LABEL_START };
  149. /// temporary buffer for passing around compiled bytecode
  150. /// @see add_scriptb, set_label, parse_script
  151. static unsigned char* script_buf = NULL;
  152. static int script_pos = 0, script_size = 0;
  153. static inline int GETVALUE(const unsigned char* buf, int i)
  154. {
  155. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  156. }
  157. static inline void SETVALUE(unsigned char* buf, int i, int n)
  158. {
  159. buf[i] = GetByte(n, 0);
  160. buf[i+1] = GetByte(n, 1);
  161. buf[i+2] = GetByte(n, 2);
  162. }
  163. // String buffer structures.
  164. // str_data stores string information
  165. static struct str_data_struct {
  166. enum c_op type;
  167. int str;
  168. int backpatch;
  169. int label;
  170. int (*func)(struct script_state *st);
  171. int val;
  172. int next;
  173. } *str_data = NULL;
  174. static int str_data_size = 0; // size of the data
  175. static int str_num = LABEL_START; // next id to be assigned
  176. // str_buf holds the strings themselves
  177. static char *str_buf;
  178. static int str_size = 0; // size of the buffer
  179. static int str_pos = 0; // next position to be assigned
  180. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  181. #define SCRIPT_HASH_SIZE 1021
  182. int str_hash[SCRIPT_HASH_SIZE];
  183. // Specifies which string hashing method to use
  184. //#define SCRIPT_HASH_DJB2
  185. //#define SCRIPT_HASH_SDBM
  186. #define SCRIPT_HASH_ELF
  187. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  188. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  189. static int parse_options=0;
  190. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  191. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  192. // Caches compiled autoscript item code.
  193. // Note: This is not cleared when reloading itemdb.
  194. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  195. struct Script_Config script_config = {
  196. 1, // warn_func_mismatch_argtypes
  197. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  198. 0, INT_MAX, // input_min_value/input_max_value
  199. "OnPCDieEvent", //die_event_name
  200. "OnPCKillEvent", //kill_pc_event_name
  201. "OnNPCKillEvent", //kill_mob_event_name
  202. "OnPCLoginEvent", //login_event_name
  203. "OnPCLogoutEvent", //logout_event_name
  204. "OnPCLoadMapEvent", //loadmap_event_name
  205. "OnPCBaseLvUpEvent", //baselvup_event_name
  206. "OnPCJobLvUpEvent", //joblvup_event_name
  207. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  208. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  209. };
  210. static jmp_buf error_jump;
  211. static char* error_msg;
  212. static const char* error_pos;
  213. static int error_report; // if the error should produce output
  214. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  215. // [Eoe / jA 1080, 1081, 1094, 1164]
  216. enum curly_type {
  217. TYPE_NULL = 0,
  218. TYPE_IF,
  219. TYPE_SWITCH,
  220. TYPE_WHILE,
  221. TYPE_FOR,
  222. TYPE_DO,
  223. TYPE_USERFUNC,
  224. TYPE_ARGLIST // function argument list
  225. };
  226. enum e_arglist
  227. {
  228. ARGLIST_UNDEFINED = 0,
  229. ARGLIST_NO_PAREN = 1,
  230. ARGLIST_PAREN = 2,
  231. };
  232. static struct {
  233. struct {
  234. enum curly_type type;
  235. int index;
  236. int count;
  237. int flag;
  238. struct linkdb_node *case_label;
  239. } curly[256]; // 右カッコの情報
  240. int curly_count; // 右カッコの数
  241. int index; // スクリプト内で使用した構文の数
  242. } syntax;
  243. const char* parse_curly_close(const char* p);
  244. const char* parse_syntax_close(const char* p);
  245. const char* parse_syntax_close_sub(const char* p,int* flag);
  246. const char* parse_syntax(const char* p);
  247. static int parse_syntax_for_flag = 0;
  248. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  249. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  250. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  251. int potion_target=0;
  252. c_op get_com(unsigned char *script,int *pos);
  253. int get_num(unsigned char *script,int *pos);
  254. typedef struct script_function {
  255. int (*func)(struct script_state *st);
  256. const char *name;
  257. const char *arg;
  258. } script_function;
  259. extern script_function buildin_func[];
  260. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  261. /*==========================================
  262. * ローカルプロトタイプ宣言 (必要な物のみ)
  263. *------------------------------------------*/
  264. const char* parse_subexpr(const char* p,int limit);
  265. int run_func(struct script_state *st);
  266. enum {
  267. MF_NOMEMO, //0
  268. MF_NOTELEPORT,
  269. MF_NOSAVE,
  270. MF_NOBRANCH,
  271. MF_NOPENALTY,
  272. MF_NOZENYPENALTY,
  273. MF_PVP,
  274. MF_PVP_NOPARTY,
  275. MF_PVP_NOGUILD,
  276. MF_GVG,
  277. MF_GVG_NOPARTY, //10
  278. MF_NOTRADE,
  279. MF_NOSKILL,
  280. MF_NOWARP,
  281. MF_PARTYLOCK,
  282. MF_NOICEWALL,
  283. MF_SNOW,
  284. MF_FOG,
  285. MF_SAKURA,
  286. MF_LEAVES,
  287. /**
  288. * No longer available, keeping here just in case it's back someday. [Ind]
  289. **/
  290. //MF_RAIN, //20
  291. // 21 free
  292. MF_NOGO = 22,
  293. MF_CLOUDS,
  294. MF_CLOUDS2,
  295. MF_FIREWORKS,
  296. MF_GVG_CASTLE,
  297. MF_GVG_DUNGEON,
  298. MF_NIGHTENABLED,
  299. MF_NOBASEEXP,
  300. MF_NOJOBEXP, //30
  301. MF_NOMOBLOOT,
  302. MF_NOMVPLOOT,
  303. MF_NORETURN,
  304. MF_NOWARPTO,
  305. MF_NIGHTMAREDROP,
  306. MF_RESTRICTED,
  307. MF_NOCOMMAND,
  308. MF_NODROP,
  309. MF_JEXP,
  310. MF_BEXP, //40
  311. MF_NOVENDING,
  312. MF_LOADEVENT,
  313. MF_NOCHAT,
  314. MF_NOEXPPENALTY,
  315. MF_GUILDLOCK,
  316. MF_TOWN,
  317. MF_AUTOTRADE,
  318. MF_ALLOWKS,
  319. MF_MONSTER_NOTELEPORT,
  320. MF_PVP_NOCALCRANK, //50
  321. MF_BATTLEGROUND,
  322. MF_RESET
  323. };
  324. const char* script_op2name(int op)
  325. {
  326. #define RETURN_OP_NAME(type) case type: return #type
  327. switch( op )
  328. {
  329. RETURN_OP_NAME(C_NOP);
  330. RETURN_OP_NAME(C_POS);
  331. RETURN_OP_NAME(C_INT);
  332. RETURN_OP_NAME(C_PARAM);
  333. RETURN_OP_NAME(C_FUNC);
  334. RETURN_OP_NAME(C_STR);
  335. RETURN_OP_NAME(C_CONSTSTR);
  336. RETURN_OP_NAME(C_ARG);
  337. RETURN_OP_NAME(C_NAME);
  338. RETURN_OP_NAME(C_EOL);
  339. RETURN_OP_NAME(C_RETINFO);
  340. RETURN_OP_NAME(C_USERFUNC);
  341. RETURN_OP_NAME(C_USERFUNC_POS);
  342. // operators
  343. RETURN_OP_NAME(C_OP3);
  344. RETURN_OP_NAME(C_LOR);
  345. RETURN_OP_NAME(C_LAND);
  346. RETURN_OP_NAME(C_LE);
  347. RETURN_OP_NAME(C_LT);
  348. RETURN_OP_NAME(C_GE);
  349. RETURN_OP_NAME(C_GT);
  350. RETURN_OP_NAME(C_EQ);
  351. RETURN_OP_NAME(C_NE);
  352. RETURN_OP_NAME(C_XOR);
  353. RETURN_OP_NAME(C_OR);
  354. RETURN_OP_NAME(C_AND);
  355. RETURN_OP_NAME(C_ADD);
  356. RETURN_OP_NAME(C_SUB);
  357. RETURN_OP_NAME(C_MUL);
  358. RETURN_OP_NAME(C_DIV);
  359. RETURN_OP_NAME(C_MOD);
  360. RETURN_OP_NAME(C_NEG);
  361. RETURN_OP_NAME(C_LNOT);
  362. RETURN_OP_NAME(C_NOT);
  363. RETURN_OP_NAME(C_R_SHIFT);
  364. RETURN_OP_NAME(C_L_SHIFT);
  365. default:
  366. ShowDebug("script_op2name: unexpected op=%d\n", op);
  367. return "???";
  368. }
  369. #undef RETURN_OP_NAME
  370. }
  371. #ifdef DEBUG_DUMP_STACK
  372. static void script_dump_stack(struct script_state* st)
  373. {
  374. int i;
  375. ShowMessage("\tstart = %d\n", st->start);
  376. ShowMessage("\tend = %d\n", st->end);
  377. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  378. ShowMessage("\tsp = %d\n", st->stack->sp);
  379. for( i = 0; i < st->stack->sp; ++i )
  380. {
  381. struct script_data* data = &st->stack->stack_data[i];
  382. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  383. switch( data->type )
  384. {
  385. case C_INT:
  386. case C_POS:
  387. ShowMessage(" %d\n", data->u.num);
  388. break;
  389. case C_STR:
  390. case C_CONSTSTR:
  391. ShowMessage(" \"%s\"\n", data->u.str);
  392. break;
  393. case C_NAME:
  394. 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));
  395. break;
  396. case C_RETINFO:
  397. {
  398. struct script_retinfo* ri = data->u.ri;
  399. 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);
  400. }
  401. break;
  402. default:
  403. ShowMessage("\n");
  404. break;
  405. }
  406. }
  407. }
  408. #endif
  409. /// Reports on the console the src of a script error.
  410. static void script_reportsrc(struct script_state *st)
  411. {
  412. struct block_list* bl;
  413. if( st->oid == 0 )
  414. return; //Can't report source.
  415. bl = map_id2bl(st->oid);
  416. if( bl == NULL )
  417. return;
  418. switch( bl->type )
  419. {
  420. case BL_NPC:
  421. if( bl->m >= 0 )
  422. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  423. else
  424. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  425. break;
  426. default:
  427. if( bl->m >= 0 )
  428. 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);
  429. else
  430. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  431. break;
  432. }
  433. }
  434. /// Reports on the console information about the script data.
  435. static void script_reportdata(struct script_data* data)
  436. {
  437. if( data == NULL )
  438. return;
  439. switch( data->type )
  440. {
  441. case C_NOP:// no value
  442. ShowDebug("Data: nothing (nil)\n");
  443. break;
  444. case C_INT:// number
  445. ShowDebug("Data: number value=%d\n", data->u.num);
  446. break;
  447. case C_STR:
  448. case C_CONSTSTR:// string
  449. if( data->u.str )
  450. {
  451. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  452. }
  453. else
  454. {
  455. ShowDebug("Data: string value=NULL\n");
  456. }
  457. break;
  458. case C_NAME:// reference
  459. if( reference_tovariable(data) )
  460. {// variable
  461. const char* name = reference_getname(data);
  462. if( not_array_variable(*name) )
  463. ShowDebug("Data: variable name='%s'\n", name);
  464. else
  465. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  466. }
  467. else if( reference_toconstant(data) )
  468. {// constant
  469. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  470. }
  471. else if( reference_toparam(data) )
  472. {// param
  473. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  474. }
  475. else
  476. {// ???
  477. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  478. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  479. }
  480. break;
  481. case C_POS:// label
  482. ShowDebug("Data: label pos=%d\n", data->u.num);
  483. break;
  484. default:
  485. ShowDebug("Data: %s\n", script_op2name(data->type));
  486. break;
  487. }
  488. }
  489. /// Reports on the console information about the current built-in function.
  490. static void script_reportfunc(struct script_state* st)
  491. {
  492. int i, params, id;
  493. struct script_data* data;
  494. if( !script_hasdata(st,0) )
  495. {// no stack
  496. return;
  497. }
  498. data = script_getdata(st,0);
  499. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  500. {// script currently not executing a built-in function or corrupt stack
  501. return;
  502. }
  503. id = reference_getid(data);
  504. params = script_lastdata(st)-1;
  505. if( params > 0 )
  506. {
  507. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  508. for( i = 2; i <= script_lastdata(st); i++ )
  509. {
  510. script_reportdata(script_getdata(st,i));
  511. }
  512. }
  513. else
  514. {
  515. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  516. }
  517. }
  518. /*==========================================
  519. * エラーメッセージ出力
  520. *------------------------------------------*/
  521. static void disp_error_message2(const char *mes,const char *pos,int report)
  522. {
  523. error_msg = aStrdup(mes);
  524. error_pos = pos;
  525. error_report = report;
  526. longjmp( error_jump, 1 );
  527. }
  528. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  529. /// Checks event parameter validity
  530. static void check_event(struct script_state *st, const char *evt)
  531. {
  532. if( evt && evt[0] && !stristr(evt, "::On") )
  533. {
  534. if( npc_event_isspecial(evt) )
  535. {
  536. ; // portable small/large monsters or other attributes
  537. }
  538. else
  539. {
  540. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  541. script_reportsrc(st);
  542. }
  543. }
  544. }
  545. /*==========================================
  546. * Hashes the input string
  547. *------------------------------------------*/
  548. static unsigned int calc_hash(const char* p)
  549. {
  550. unsigned int h;
  551. #if defined(SCRIPT_HASH_DJB2)
  552. h = 5381;
  553. while( *p ) // hash*33 + c
  554. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  555. #elif defined(SCRIPT_HASH_SDBM)
  556. h = 0;
  557. while( *p ) // hash*65599 + c
  558. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  559. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  560. h = 0;
  561. while( *p ){
  562. unsigned int g;
  563. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  564. g = h & 0xF0000000;
  565. if( g )
  566. {
  567. h ^= g >> 24;
  568. h &= ~g;
  569. }
  570. }
  571. #else // athena hash
  572. h = 0;
  573. while( *p )
  574. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  575. #endif
  576. return h % SCRIPT_HASH_SIZE;
  577. }
  578. /*==========================================
  579. * str_data manipulation functions
  580. *------------------------------------------*/
  581. /// Looks up string using the provided id.
  582. const char* get_str(int id)
  583. {
  584. Assert( id >= LABEL_START && id < str_size );
  585. return str_buf+str_data[id].str;
  586. }
  587. /// Returns the uid of the string, or -1.
  588. static int search_str(const char* p)
  589. {
  590. int i;
  591. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  592. if( strcasecmp(get_str(i),p) == 0 )
  593. return i;
  594. return -1;
  595. }
  596. /// Stores a copy of the string and returns its id.
  597. /// If an identical string is already present, returns its id instead.
  598. int add_str(const char* p)
  599. {
  600. int i, h;
  601. int len;
  602. h = calc_hash(p);
  603. if( str_hash[h] == 0 )
  604. {// empty bucket, add new node here
  605. str_hash[h] = str_num;
  606. }
  607. else
  608. {// scan for end of list, or occurence of identical string
  609. for( i = str_hash[h]; ; i = str_data[i].next )
  610. {
  611. if( strcasecmp(get_str(i),p) == 0 )
  612. return i; // string already in list
  613. if( str_data[i].next == 0 )
  614. break; // reached the end
  615. }
  616. // append node to end of list
  617. str_data[i].next = str_num;
  618. }
  619. // grow list if neccessary
  620. if( str_num >= str_data_size )
  621. {
  622. str_data_size += 128;
  623. RECREATE(str_data,struct str_data_struct,str_data_size);
  624. memset(str_data + (str_data_size - 128), '\0', 128);
  625. }
  626. len=(int)strlen(p);
  627. // grow string buffer if neccessary
  628. while( str_pos+len+1 >= str_size )
  629. {
  630. str_size += 256;
  631. RECREATE(str_buf,char,str_size);
  632. memset(str_buf + (str_size - 256), '\0', 256);
  633. }
  634. safestrncpy(str_buf+str_pos, p, len+1);
  635. str_data[str_num].type = C_NOP;
  636. str_data[str_num].str = str_pos;
  637. str_data[str_num].next = 0;
  638. str_data[str_num].func = NULL;
  639. str_data[str_num].backpatch = -1;
  640. str_data[str_num].label = -1;
  641. str_pos += len+1;
  642. return str_num++;
  643. }
  644. /// Appends 1 byte to the script buffer.
  645. static void add_scriptb(int a)
  646. {
  647. if( script_pos+1 >= script_size )
  648. {
  649. script_size += SCRIPT_BLOCK_SIZE;
  650. RECREATE(script_buf,unsigned char,script_size);
  651. }
  652. script_buf[script_pos++] = (uint8)(a);
  653. }
  654. /// Appends a c_op value to the script buffer.
  655. /// The value is variable-length encoded into 8-bit blocks.
  656. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  657. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  658. static void add_scriptc(int a)
  659. {
  660. while( a >= 0x40 )
  661. {
  662. add_scriptb((a&0x3f)|0x40);
  663. a = (a - 0x40) >> 6;
  664. }
  665. add_scriptb(a);
  666. }
  667. /// Appends an integer value to the script buffer.
  668. /// The value is variable-length encoded into 8-bit blocks.
  669. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  670. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  671. static void add_scripti(int a)
  672. {
  673. while( a >= 0x40 )
  674. {
  675. add_scriptb((a&0x3f)|0xc0);
  676. a = (a - 0x40) >> 6;
  677. }
  678. add_scriptb(a|0x80);
  679. }
  680. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  681. ///
  682. /// @param l The id of the str_data entry
  683. // 最大16Mまで
  684. static void add_scriptl(int l)
  685. {
  686. int backpatch = str_data[l].backpatch;
  687. switch(str_data[l].type){
  688. case C_POS:
  689. case C_USERFUNC_POS:
  690. add_scriptc(C_POS);
  691. add_scriptb(str_data[l].label);
  692. add_scriptb(str_data[l].label>>8);
  693. add_scriptb(str_data[l].label>>16);
  694. break;
  695. case C_NOP:
  696. case C_USERFUNC:
  697. // ラベルの可能性があるのでbackpatch用データ埋め込み
  698. add_scriptc(C_NAME);
  699. str_data[l].backpatch = script_pos;
  700. add_scriptb(backpatch);
  701. add_scriptb(backpatch>>8);
  702. add_scriptb(backpatch>>16);
  703. break;
  704. case C_INT:
  705. add_scripti(abs(str_data[l].val));
  706. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  707. add_scriptc(C_NEG);
  708. break;
  709. default: // assume C_NAME
  710. add_scriptc(C_NAME);
  711. add_scriptb(l);
  712. add_scriptb(l>>8);
  713. add_scriptb(l>>16);
  714. break;
  715. }
  716. }
  717. /*==========================================
  718. * ラベルを解決する
  719. *------------------------------------------*/
  720. void set_label(int l,int pos, const char* script_pos)
  721. {
  722. int i,next;
  723. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  724. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  725. disp_error_message("set_label: invalid label name",script_pos);
  726. return;
  727. }
  728. if(str_data[l].label!=-1){
  729. disp_error_message("set_label: dup label ",script_pos);
  730. return;
  731. }
  732. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  733. str_data[l].label=pos;
  734. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  735. next=GETVALUE(script_buf,i);
  736. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  737. SETVALUE(script_buf,i,pos);
  738. i=next;
  739. }
  740. }
  741. /// Skips spaces and/or comments.
  742. const char* skip_space(const char* p)
  743. {
  744. if( p == NULL )
  745. return NULL;
  746. for(;;)
  747. {
  748. while( ISSPACE(*p) )
  749. ++p;
  750. if( *p == '/' && p[1] == '/' )
  751. {// line comment
  752. while(*p && *p!='\n')
  753. ++p;
  754. }
  755. else if( *p == '/' && p[1] == '*' )
  756. {// block comment
  757. p += 2;
  758. for(;;)
  759. {
  760. if( *p == '\0' )
  761. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  762. if( *p == '*' && p[1] == '/' )
  763. {// end of block comment
  764. p += 2;
  765. break;
  766. }
  767. ++p;
  768. }
  769. }
  770. else
  771. break;
  772. }
  773. return p;
  774. }
  775. /// Skips a word.
  776. /// A word consists of undercores and/or alfanumeric characters,
  777. /// and valid variable prefixes/postfixes.
  778. static
  779. const char* skip_word(const char* p)
  780. {
  781. // prefix
  782. switch( *p )
  783. {
  784. case '@':// temporary char variable
  785. ++p; break;
  786. case '#':// account variable
  787. p += ( p[1] == '#' ? 2 : 1 ); break;
  788. case '\'':// instance variable
  789. ++p; break;
  790. case '.':// npc variable
  791. p += ( p[1] == '@' ? 2 : 1 ); break;
  792. case '$':// global variable
  793. p += ( p[1] == '@' ? 2 : 1 ); break;
  794. }
  795. while( ISALNUM(*p) || *p == '_' )
  796. ++p;
  797. // postfix
  798. if( *p == '$' )// string
  799. p++;
  800. return p;
  801. }
  802. /// Adds a word to str_data.
  803. /// @see skip_word
  804. /// @see add_str
  805. static
  806. int add_word(const char* p)
  807. {
  808. char* word;
  809. int len;
  810. int i;
  811. // Check for a word
  812. len = skip_word(p) - p;
  813. if( len == 0 )
  814. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  815. // Duplicate the word
  816. word = (char*)aMalloc(len+1);
  817. memcpy(word, p, len);
  818. word[len] = 0;
  819. // add the word
  820. i = add_str(word);
  821. aFree(word);
  822. return i;
  823. }
  824. /// Parses a function call.
  825. /// The argument list can have parenthesis or not.
  826. /// The number of arguments is checked.
  827. static
  828. const char* parse_callfunc(const char* p, int require_paren)
  829. {
  830. const char* p2;
  831. const char* arg=NULL;
  832. int func;
  833. func = add_word(p);
  834. if( str_data[func].type == C_FUNC ){
  835. // buildin function
  836. add_scriptl(func);
  837. add_scriptc(C_ARG);
  838. arg = buildin_func[str_data[func].val].arg;
  839. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  840. // script defined function
  841. int callsub = search_str("callsub");
  842. add_scriptl(callsub);
  843. add_scriptc(C_ARG);
  844. add_scriptl(func);
  845. arg = buildin_func[str_data[callsub].val].arg;
  846. if( *arg == 0 )
  847. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  848. if( *arg != '*' )
  849. ++arg; // count func as argument
  850. } else {
  851. #ifdef SCRIPT_CALLFUNC_CHECK
  852. const char* name = get_str(func);
  853. if( strdb_get(userfunc_db, name) == NULL ) {
  854. #endif
  855. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  856. #ifdef SCRIPT_CALLFUNC_CHECK
  857. } else {
  858. int callfunc = search_str("callfunc");
  859. add_scriptl(callfunc);
  860. add_scriptc(C_ARG);
  861. add_scriptc(C_STR);
  862. while( *name ) add_scriptb(*name ++);
  863. add_scriptb(0);
  864. arg = buildin_func[str_data[callfunc].val].arg;
  865. if( *arg != '*' ) ++ arg;
  866. }
  867. #endif
  868. }
  869. p = skip_word(p);
  870. p = skip_space(p);
  871. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  872. syntax.curly[syntax.curly_count].count = 0;
  873. if( *p == ';' )
  874. {// <func name> ';'
  875. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  876. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  877. {// <func name> '(' ')'
  878. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  879. p = p2;
  880. /*
  881. } else if( 0 && require_paren && *p != '(' )
  882. {// <func name>
  883. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  884. */
  885. } else
  886. {// <func name> <arg list>
  887. if( require_paren ){
  888. if( *p != '(' )
  889. disp_error_message("need '('",p);
  890. ++p; // skip '('
  891. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  892. } else if( *p == '(' ){
  893. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  894. } else {
  895. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  896. }
  897. ++syntax.curly_count;
  898. while( *arg ) {
  899. p2=parse_subexpr(p,-1);
  900. if( p == p2 )
  901. break; // not an argument
  902. if( *arg != '*' )
  903. ++arg; // next argument
  904. p=skip_space(p2);
  905. if( *arg == 0 || *p != ',' )
  906. break; // no more arguments
  907. ++p; // skip comma
  908. }
  909. --syntax.curly_count;
  910. }
  911. if( *arg && *arg != '?' && *arg != '*' )
  912. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  913. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  914. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  915. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  916. if( *p != ')' )
  917. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  918. ++p;
  919. }
  920. add_scriptc(C_FUNC);
  921. return p;
  922. }
  923. /// Processes end of logical script line.
  924. /// @param first When true, only fix up scheduling data is initialized
  925. /// @param p Script position for error reporting in set_label
  926. static void parse_nextline(bool first, const char* p)
  927. {
  928. if( !first )
  929. {
  930. add_scriptc(C_EOL); // mark end of line for stack cleanup
  931. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  932. }
  933. // initialize data for new '-' label fix up scheduling
  934. str_data[LABEL_NEXTLINE].type = C_NOP;
  935. str_data[LABEL_NEXTLINE].backpatch = -1;
  936. str_data[LABEL_NEXTLINE].label = -1;
  937. }
  938. /*==========================================
  939. * 項の解析
  940. *------------------------------------------*/
  941. const char* parse_simpleexpr(const char *p)
  942. {
  943. int i;
  944. p=skip_space(p);
  945. if(*p==';' || *p==',')
  946. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  947. if(*p=='('){
  948. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  949. ++syntax.curly[i].count;
  950. p=parse_subexpr(p+1,-1);
  951. p=skip_space(p);
  952. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  953. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  954. ){
  955. if( *p == ',' ){
  956. syntax.curly[i].flag = ARGLIST_PAREN;
  957. return p;
  958. } else
  959. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  960. }
  961. if( *p != ')' )
  962. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  963. ++p;
  964. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  965. char *np;
  966. i=strtoul(p,&np,0);
  967. add_scripti(i);
  968. p=np;
  969. } else if(*p=='"'){
  970. add_scriptc(C_STR);
  971. p++;
  972. while( *p && *p != '"' ){
  973. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  974. {
  975. char buf[8];
  976. size_t len = skip_escaped_c(p) - p;
  977. size_t n = sv_unescape_c(buf, p, len);
  978. if( n != 1 )
  979. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  980. p += len;
  981. add_scriptb(*buf);
  982. continue;
  983. }
  984. else if( *p == '\n' )
  985. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  986. add_scriptb(*p++);
  987. }
  988. if(!*p)
  989. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  990. add_scriptb(0);
  991. p++; //'"'
  992. } else {
  993. int l;
  994. // label , register , function etc
  995. if(skip_word(p)==p)
  996. disp_error_message("parse_simpleexpr: unexpected character",p);
  997. l=add_word(p);
  998. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  999. return parse_callfunc(p,1);
  1000. #ifdef SCRIPT_CALLFUNC_CHECK
  1001. else {
  1002. const char* name = get_str(l);
  1003. if( strdb_get(userfunc_db,name) != NULL ) {
  1004. return parse_callfunc(p,1);
  1005. }
  1006. }
  1007. #endif
  1008. p=skip_word(p);
  1009. if( *p == '[' ){
  1010. // array(name[i] => getelementofarray(name,i) )
  1011. add_scriptl(search_str("getelementofarray"));
  1012. add_scriptc(C_ARG);
  1013. add_scriptl(l);
  1014. p=parse_subexpr(p+1,-1);
  1015. p=skip_space(p);
  1016. if( *p != ']' )
  1017. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  1018. ++p;
  1019. add_scriptc(C_FUNC);
  1020. }else
  1021. add_scriptl(l);
  1022. }
  1023. return p;
  1024. }
  1025. /*==========================================
  1026. * 式の解析
  1027. *------------------------------------------*/
  1028. const char* parse_subexpr(const char* p,int limit)
  1029. {
  1030. int op,opl,len;
  1031. const char* tmpp;
  1032. p=skip_space(p);
  1033. if(*p=='-'){
  1034. tmpp=skip_space(p+1);
  1035. if(*tmpp==';' || *tmpp==','){
  1036. add_scriptl(LABEL_NEXTLINE);
  1037. p++;
  1038. return p;
  1039. }
  1040. }
  1041. tmpp=p;
  1042. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1043. p=parse_subexpr(p+1,10);
  1044. add_scriptc(op);
  1045. } else
  1046. p=parse_simpleexpr(p);
  1047. p=skip_space(p);
  1048. while((
  1049. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1050. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1051. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1052. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1053. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1054. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1055. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1056. (op=C_AND,opl=6,len=1,*p=='&') ||
  1057. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1058. (op=C_OR,opl=5,len=1,*p=='|') ||
  1059. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1060. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1061. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1062. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1063. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1064. (op=C_GT,opl=3,len=1,*p=='>') ||
  1065. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1066. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1067. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1068. p+=len;
  1069. if(op == C_OP3) {
  1070. p=parse_subexpr(p,-1);
  1071. p=skip_space(p);
  1072. if( *(p++) != ':')
  1073. disp_error_message("parse_subexpr: need ':'", p-1);
  1074. p=parse_subexpr(p,-1);
  1075. } else {
  1076. p=parse_subexpr(p,opl);
  1077. }
  1078. add_scriptc(op);
  1079. p=skip_space(p);
  1080. }
  1081. return p; /* return first untreated operator */
  1082. }
  1083. /*==========================================
  1084. * 式の評価
  1085. *------------------------------------------*/
  1086. const char* parse_expr(const char *p)
  1087. {
  1088. switch(*p){
  1089. case ')': case ';': case ':': case '[': case ']':
  1090. case '}':
  1091. disp_error_message("parse_expr: unexpected char",p);
  1092. }
  1093. p=parse_subexpr(p,-1);
  1094. return p;
  1095. }
  1096. /*==========================================
  1097. * 行の解析
  1098. *------------------------------------------*/
  1099. const char* parse_line(const char* p)
  1100. {
  1101. const char* p2;
  1102. p=skip_space(p);
  1103. if(*p==';') {
  1104. // if(); for(); while(); のために閉じ判定
  1105. p = parse_syntax_close(p + 1);
  1106. return p;
  1107. }
  1108. if(*p==')' && parse_syntax_for_flag)
  1109. return p+1;
  1110. p = skip_space(p);
  1111. if(p[0] == '{') {
  1112. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1113. syntax.curly[syntax.curly_count].count = -1;
  1114. syntax.curly[syntax.curly_count].index = -1;
  1115. syntax.curly_count++;
  1116. return p + 1;
  1117. } else if(p[0] == '}') {
  1118. return parse_curly_close(p);
  1119. }
  1120. // 構文関連の処理
  1121. p2 = parse_syntax(p);
  1122. if(p2 != NULL)
  1123. return p2;
  1124. p = parse_callfunc(p,0);
  1125. p = skip_space(p);
  1126. if(parse_syntax_for_flag) {
  1127. if( *p != ')' )
  1128. disp_error_message("parse_line: need ')'",p);
  1129. } else {
  1130. if( *p != ';' )
  1131. disp_error_message("parse_line: need ';'",p);
  1132. }
  1133. // if, for , while の閉じ判定
  1134. p = parse_syntax_close(p+1);
  1135. return p;
  1136. }
  1137. // { ... } の閉じ処理
  1138. const char* parse_curly_close(const char* p)
  1139. {
  1140. if(syntax.curly_count <= 0) {
  1141. disp_error_message("parse_curly_close: unexpected string",p);
  1142. return p + 1;
  1143. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1144. syntax.curly_count--;
  1145. // if, for , while の閉じ判定
  1146. p = parse_syntax_close(p + 1);
  1147. return p;
  1148. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1149. // switch() 閉じ判定
  1150. int pos = syntax.curly_count-1;
  1151. char label[256];
  1152. int l;
  1153. // 一時変数を消す
  1154. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1155. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1156. parse_line(label);
  1157. syntax.curly_count--;
  1158. // 無条件で終了ポインタに移動
  1159. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1160. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1161. parse_line(label);
  1162. syntax.curly_count--;
  1163. // 現在地のラベルを付ける
  1164. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1165. l=add_str(label);
  1166. set_label(l,script_pos, p);
  1167. if(syntax.curly[pos].flag) {
  1168. // default が存在する
  1169. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1170. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1171. parse_line(label);
  1172. syntax.curly_count--;
  1173. }
  1174. // 終了ラベルを付ける
  1175. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1176. l=add_str(label);
  1177. set_label(l,script_pos, p);
  1178. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1179. syntax.curly_count--;
  1180. // if, for , while の閉じ判定
  1181. p = parse_syntax_close(p + 1);
  1182. return p;
  1183. } else {
  1184. disp_error_message("parse_curly_close: unexpected string",p);
  1185. return p + 1;
  1186. }
  1187. }
  1188. // 構文関連の処理
  1189. // break, case, continue, default, do, for, function,
  1190. // if, switch, while をこの内部で処理します。
  1191. const char* parse_syntax(const char* p)
  1192. {
  1193. const char *p2 = skip_word(p);
  1194. switch(*p) {
  1195. case 'B':
  1196. case 'b':
  1197. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1198. // break の処理
  1199. char label[256];
  1200. int pos = syntax.curly_count - 1;
  1201. while(pos >= 0) {
  1202. if(syntax.curly[pos].type == TYPE_DO) {
  1203. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1204. break;
  1205. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1206. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1207. break;
  1208. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1209. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1210. break;
  1211. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1212. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1213. break;
  1214. }
  1215. pos--;
  1216. }
  1217. if(pos < 0) {
  1218. disp_error_message("parse_syntax: unexpected 'break'",p);
  1219. } else {
  1220. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1221. parse_line(label);
  1222. syntax.curly_count--;
  1223. }
  1224. p = skip_space(p2);
  1225. if(*p != ';')
  1226. disp_error_message("parse_syntax: need ';'",p);
  1227. // if, for , while の閉じ判定
  1228. p = parse_syntax_close(p + 1);
  1229. return p;
  1230. }
  1231. break;
  1232. case 'c':
  1233. case 'C':
  1234. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1235. // case の処理
  1236. int pos = syntax.curly_count-1;
  1237. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1238. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1239. return p+1;
  1240. } else {
  1241. char label[256];
  1242. int l,v;
  1243. char *np;
  1244. if(syntax.curly[pos].count != 1) {
  1245. // FALLTHRU 用のジャンプ
  1246. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1247. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1248. parse_line(label);
  1249. syntax.curly_count--;
  1250. // 現在地のラベルを付ける
  1251. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1252. l=add_str(label);
  1253. set_label(l,script_pos, p);
  1254. }
  1255. // switch 判定文
  1256. p = skip_space(p2);
  1257. if(p == p2) {
  1258. disp_error_message("parse_syntax: expect space ' '",p);
  1259. }
  1260. // check whether case label is integer or not
  1261. v = strtol(p,&np,0);
  1262. if(np == p) { //Check for constants
  1263. p2 = skip_word(p);
  1264. v = p2-p; // length of word at p2
  1265. memcpy(label,p,v);
  1266. label[v]='\0';
  1267. if( !script_get_constant(label, &v) )
  1268. disp_error_message("parse_syntax: 'case' label not integer",p);
  1269. p = skip_word(p);
  1270. } else { //Numeric value
  1271. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1272. p++;
  1273. p = skip_word(p);
  1274. if(np != p)
  1275. disp_error_message("parse_syntax: 'case' label not integer",np);
  1276. }
  1277. p = skip_space(p);
  1278. if(*p != ':')
  1279. disp_error_message("parse_syntax: expect ':'",p);
  1280. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1281. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1282. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1283. // 2回parse しないとダメ
  1284. p2 = parse_line(label);
  1285. parse_line(p2);
  1286. syntax.curly_count--;
  1287. if(syntax.curly[pos].count != 1) {
  1288. // FALLTHRU 終了後のラベル
  1289. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1290. l=add_str(label);
  1291. set_label(l,script_pos,p);
  1292. }
  1293. // check duplication of case label [Rayce]
  1294. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1295. disp_error_message("parse_syntax: dup 'case'",p);
  1296. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1297. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1298. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1299. parse_line(label);
  1300. syntax.curly_count--;
  1301. syntax.curly[pos].count++;
  1302. }
  1303. return p + 1;
  1304. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1305. // continue の処理
  1306. char label[256];
  1307. int pos = syntax.curly_count - 1;
  1308. while(pos >= 0) {
  1309. if(syntax.curly[pos].type == TYPE_DO) {
  1310. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1311. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1312. break;
  1313. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1314. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1315. break;
  1316. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1317. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1318. break;
  1319. }
  1320. pos--;
  1321. }
  1322. if(pos < 0) {
  1323. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1324. } else {
  1325. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1326. parse_line(label);
  1327. syntax.curly_count--;
  1328. }
  1329. p = skip_space(p2);
  1330. if(*p != ';')
  1331. disp_error_message("parse_syntax: need ';'",p);
  1332. // if, for , while の閉じ判定
  1333. p = parse_syntax_close(p + 1);
  1334. return p;
  1335. }
  1336. break;
  1337. case 'd':
  1338. case 'D':
  1339. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1340. // switch - default の処理
  1341. int pos = syntax.curly_count-1;
  1342. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1343. disp_error_message("parse_syntax: unexpected 'default'",p);
  1344. } else if(syntax.curly[pos].flag) {
  1345. disp_error_message("parse_syntax: dup 'default'",p);
  1346. } else {
  1347. char label[256];
  1348. int l;
  1349. // 現在地のラベルを付ける
  1350. p = skip_space(p2);
  1351. if(*p != ':') {
  1352. disp_error_message("parse_syntax: need ':'",p);
  1353. }
  1354. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1355. l=add_str(label);
  1356. set_label(l,script_pos,p);
  1357. // 無条件で次のリンクに飛ばす
  1358. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1359. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1360. parse_line(label);
  1361. syntax.curly_count--;
  1362. // default のラベルを付ける
  1363. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1364. l=add_str(label);
  1365. set_label(l,script_pos,p);
  1366. syntax.curly[syntax.curly_count - 1].flag = 1;
  1367. syntax.curly[pos].count++;
  1368. }
  1369. return p + 1;
  1370. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1371. int l;
  1372. char label[256];
  1373. p=skip_space(p2);
  1374. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1375. syntax.curly[syntax.curly_count].count = 1;
  1376. syntax.curly[syntax.curly_count].index = syntax.index++;
  1377. syntax.curly[syntax.curly_count].flag = 0;
  1378. // 現在地のラベル形成する
  1379. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1380. l=add_str(label);
  1381. set_label(l,script_pos,p);
  1382. syntax.curly_count++;
  1383. return p;
  1384. }
  1385. break;
  1386. case 'f':
  1387. case 'F':
  1388. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1389. int l;
  1390. char label[256];
  1391. int pos = syntax.curly_count;
  1392. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1393. syntax.curly[syntax.curly_count].count = 1;
  1394. syntax.curly[syntax.curly_count].index = syntax.index++;
  1395. syntax.curly[syntax.curly_count].flag = 0;
  1396. syntax.curly_count++;
  1397. p=skip_space(p2);
  1398. if(*p != '(')
  1399. disp_error_message("parse_syntax: need '('",p);
  1400. p++;
  1401. // 初期化文を実行する
  1402. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1403. p=parse_line(p);
  1404. syntax.curly_count--;
  1405. // 条件判断開始のラベル形成する
  1406. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1407. l=add_str(label);
  1408. set_label(l,script_pos,p);
  1409. p=skip_space(p);
  1410. if(*p == ';') {
  1411. // for(;;) のパターンなので必ず真
  1412. ;
  1413. } else {
  1414. // 条件が偽なら終了地点に飛ばす
  1415. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1416. add_scriptl(add_str("jump_zero"));
  1417. add_scriptc(C_ARG);
  1418. p=parse_expr(p);
  1419. p=skip_space(p);
  1420. add_scriptl(add_str(label));
  1421. add_scriptc(C_FUNC);
  1422. }
  1423. if(*p != ';')
  1424. disp_error_message("parse_syntax: need ';'",p);
  1425. p++;
  1426. // ループ開始に飛ばす
  1427. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1428. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1429. parse_line(label);
  1430. syntax.curly_count--;
  1431. // 次のループへのラベル形成する
  1432. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1433. l=add_str(label);
  1434. set_label(l,script_pos,p);
  1435. // 次のループに入る時の処理
  1436. // for 最後の ')' を ';' として扱うフラグ
  1437. parse_syntax_for_flag = 1;
  1438. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1439. p=parse_line(p);
  1440. syntax.curly_count--;
  1441. parse_syntax_for_flag = 0;
  1442. // 条件判定処理に飛ばす
  1443. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1444. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1445. parse_line(label);
  1446. syntax.curly_count--;
  1447. // ループ開始のラベル付け
  1448. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1449. l=add_str(label);
  1450. set_label(l,script_pos,p);
  1451. return p;
  1452. }
  1453. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1454. {// internal script function
  1455. const char *func_name;
  1456. func_name = skip_space(p2);
  1457. p = skip_word(func_name);
  1458. if( p == func_name )
  1459. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1460. p2 = skip_space(p);
  1461. if( *p2 == ';' )
  1462. {// function <name> ;
  1463. // function declaration - just register the name
  1464. int l;
  1465. l = add_word(func_name);
  1466. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1467. str_data[l].type = C_USERFUNC;
  1468. else if( str_data[l].type == C_USERFUNC )
  1469. ; // already registered
  1470. else
  1471. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1472. // if, for , while の閉じ判定
  1473. p = parse_syntax_close(p2 + 1);
  1474. return p;
  1475. }
  1476. else if(*p2 == '{')
  1477. {// function <name> <line/block of code>
  1478. char label[256];
  1479. int l;
  1480. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1481. syntax.curly[syntax.curly_count].count = 1;
  1482. syntax.curly[syntax.curly_count].index = syntax.index++;
  1483. syntax.curly[syntax.curly_count].flag = 0;
  1484. ++syntax.curly_count;
  1485. // Jump over the function code
  1486. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1487. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1488. ++syntax.curly_count;
  1489. parse_line(label);
  1490. --syntax.curly_count;
  1491. // Set the position of the function (label)
  1492. l=add_word(func_name);
  1493. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1494. {
  1495. str_data[l].type = C_USERFUNC;
  1496. set_label(l, script_pos, p);
  1497. if( parse_options&SCRIPT_USE_LABEL_DB )
  1498. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1499. }
  1500. else
  1501. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1502. return skip_space(p);
  1503. }
  1504. else
  1505. {
  1506. disp_error_message("expect ';' or '{' at function syntax",p);
  1507. }
  1508. }
  1509. break;
  1510. case 'i':
  1511. case 'I':
  1512. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1513. // if() の処理
  1514. char label[256];
  1515. p=skip_space(p2);
  1516. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1517. disp_error_message("need '('",p);
  1518. }
  1519. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1520. syntax.curly[syntax.curly_count].count = 1;
  1521. syntax.curly[syntax.curly_count].index = syntax.index++;
  1522. syntax.curly[syntax.curly_count].flag = 0;
  1523. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1524. syntax.curly_count++;
  1525. add_scriptl(add_str("jump_zero"));
  1526. add_scriptc(C_ARG);
  1527. p=parse_expr(p);
  1528. p=skip_space(p);
  1529. add_scriptl(add_str(label));
  1530. add_scriptc(C_FUNC);
  1531. return p;
  1532. }
  1533. break;
  1534. case 's':
  1535. case 'S':
  1536. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1537. // switch() の処理
  1538. char label[256];
  1539. p=skip_space(p2);
  1540. if(*p != '(') {
  1541. disp_error_message("need '('",p);
  1542. }
  1543. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1544. syntax.curly[syntax.curly_count].count = 1;
  1545. syntax.curly[syntax.curly_count].index = syntax.index++;
  1546. syntax.curly[syntax.curly_count].flag = 0;
  1547. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1548. syntax.curly_count++;
  1549. add_scriptl(add_str("set"));
  1550. add_scriptc(C_ARG);
  1551. add_scriptl(add_str(label));
  1552. p=parse_expr(p);
  1553. p=skip_space(p);
  1554. if(*p != '{') {
  1555. disp_error_message("parse_syntax: need '{'",p);
  1556. }
  1557. add_scriptc(C_FUNC);
  1558. return p + 1;
  1559. }
  1560. break;
  1561. case 'w':
  1562. case 'W':
  1563. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1564. int l;
  1565. char label[256];
  1566. p=skip_space(p2);
  1567. if(*p != '(') {
  1568. disp_error_message("need '('",p);
  1569. }
  1570. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1571. syntax.curly[syntax.curly_count].count = 1;
  1572. syntax.curly[syntax.curly_count].index = syntax.index++;
  1573. syntax.curly[syntax.curly_count].flag = 0;
  1574. // 条件判断開始のラベル形成する
  1575. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1576. l=add_str(label);
  1577. set_label(l,script_pos,p);
  1578. // 条件が偽なら終了地点に飛ばす
  1579. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1580. syntax.curly_count++;
  1581. add_scriptl(add_str("jump_zero"));
  1582. add_scriptc(C_ARG);
  1583. p=parse_expr(p);
  1584. p=skip_space(p);
  1585. add_scriptl(add_str(label));
  1586. add_scriptc(C_FUNC);
  1587. return p;
  1588. }
  1589. break;
  1590. }
  1591. return NULL;
  1592. }
  1593. const char* parse_syntax_close(const char *p) {
  1594. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1595. int flag;
  1596. do {
  1597. p = parse_syntax_close_sub(p,&flag);
  1598. } while(flag);
  1599. return p;
  1600. }
  1601. // if, for , while , do の閉じ判定
  1602. // flag == 1 : 閉じられた
  1603. // flag == 0 : 閉じられない
  1604. const char* parse_syntax_close_sub(const char* p,int* flag)
  1605. {
  1606. char label[256];
  1607. int pos = syntax.curly_count - 1;
  1608. int l;
  1609. *flag = 1;
  1610. if(syntax.curly_count <= 0) {
  1611. *flag = 0;
  1612. return p;
  1613. } else if(syntax.curly[pos].type == TYPE_IF) {
  1614. const char *bp = p;
  1615. const char *p2;
  1616. // if-block and else-block end is a new line
  1617. parse_nextline(false, p);
  1618. // if 最終場所へ飛ばす
  1619. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1620. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1621. parse_line(label);
  1622. syntax.curly_count--;
  1623. // 現在地のラベルを付ける
  1624. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1625. l=add_str(label);
  1626. set_label(l,script_pos,p);
  1627. syntax.curly[pos].count++;
  1628. p = skip_space(p);
  1629. p2 = skip_word(p);
  1630. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1631. // else or else - if
  1632. p = skip_space(p2);
  1633. p2 = skip_word(p);
  1634. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1635. // else - if
  1636. p=skip_space(p2);
  1637. if(*p != '(') {
  1638. disp_error_message("need '('",p);
  1639. }
  1640. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1641. add_scriptl(add_str("jump_zero"));
  1642. add_scriptc(C_ARG);
  1643. p=parse_expr(p);
  1644. p=skip_space(p);
  1645. add_scriptl(add_str(label));
  1646. add_scriptc(C_FUNC);
  1647. *flag = 0;
  1648. return p;
  1649. } else {
  1650. // else
  1651. if(!syntax.curly[pos].flag) {
  1652. syntax.curly[pos].flag = 1;
  1653. *flag = 0;
  1654. return p;
  1655. }
  1656. }
  1657. }
  1658. // if 閉じ
  1659. syntax.curly_count--;
  1660. // 最終地のラベルを付ける
  1661. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1662. l=add_str(label);
  1663. set_label(l,script_pos,p);
  1664. if(syntax.curly[pos].flag == 1) {
  1665. // このifに対するelseじゃないのでポインタの位置は同じ
  1666. return bp;
  1667. }
  1668. return p;
  1669. } else if(syntax.curly[pos].type == TYPE_DO) {
  1670. int l;
  1671. char label[256];
  1672. const char *p2;
  1673. if(syntax.curly[pos].flag) {
  1674. // 現在地のラベル形成する(continue でここに来る)
  1675. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1676. l=add_str(label);
  1677. set_label(l,script_pos,p);
  1678. }
  1679. // 条件が偽なら終了地点に飛ばす
  1680. p = skip_space(p);
  1681. p2 = skip_word(p);
  1682. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1683. disp_error_message("parse_syntax: need 'while'",p);
  1684. p = skip_space(p2);
  1685. if(*p != '(') {
  1686. disp_error_message("need '('",p);
  1687. }
  1688. // do-block end is a new line
  1689. parse_nextline(false, p);
  1690. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1691. add_scriptl(add_str("jump_zero"));
  1692. add_scriptc(C_ARG);
  1693. p=parse_expr(p);
  1694. p=skip_space(p);
  1695. add_scriptl(add_str(label));
  1696. add_scriptc(C_FUNC);
  1697. // 開始地点に飛ばす
  1698. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1699. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1700. parse_line(label);
  1701. syntax.curly_count--;
  1702. // 条件終了地点のラベル形成する
  1703. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1704. l=add_str(label);
  1705. set_label(l,script_pos,p);
  1706. p = skip_space(p);
  1707. if(*p != ';') {
  1708. disp_error_message("parse_syntax: need ';'",p);
  1709. return p+1;
  1710. }
  1711. p++;
  1712. syntax.curly_count--;
  1713. return p;
  1714. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1715. // for-block end is a new line
  1716. parse_nextline(false, p);
  1717. // 次のループに飛ばす
  1718. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1719. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1720. parse_line(label);
  1721. syntax.curly_count--;
  1722. // for 終了のラベル付け
  1723. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1724. l=add_str(label);
  1725. set_label(l,script_pos,p);
  1726. syntax.curly_count--;
  1727. return p;
  1728. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1729. // while-block end is a new line
  1730. parse_nextline(false, p);
  1731. // while 条件判断へ飛ばす
  1732. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1733. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1734. parse_line(label);
  1735. syntax.curly_count--;
  1736. // while 終了のラベル付け
  1737. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1738. l=add_str(label);
  1739. set_label(l,script_pos,p);
  1740. syntax.curly_count--;
  1741. return p;
  1742. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1743. int pos = syntax.curly_count-1;
  1744. char label[256];
  1745. int l;
  1746. // 戻す
  1747. sprintf(label,"return;");
  1748. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1749. parse_line(label);
  1750. syntax.curly_count--;
  1751. // 現在地のラベルを付ける
  1752. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1753. l=add_str(label);
  1754. set_label(l,script_pos,p);
  1755. syntax.curly_count--;
  1756. return p;
  1757. } else {
  1758. *flag = 0;
  1759. return p;
  1760. }
  1761. }
  1762. /*==========================================
  1763. * 組み込み関数の追加
  1764. *------------------------------------------*/
  1765. static void add_buildin_func(void)
  1766. {
  1767. int i,n;
  1768. const char* p;
  1769. for( i = 0; buildin_func[i].func; i++ )
  1770. {
  1771. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1772. // 'v' - value (either string or int or reference)
  1773. // 's' - string
  1774. // 'i' - int
  1775. // 'r' - reference (of a variable)
  1776. // 'l' - label
  1777. // '?' - one optional parameter
  1778. // '*' - unknown number of optional parameters
  1779. p = buildin_func[i].arg;
  1780. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1781. while( *p == '?' ) ++p;
  1782. if( *p == '*' ) ++p;
  1783. if( *p != 0){
  1784. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1785. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1786. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1787. } else {
  1788. n = add_str(buildin_func[i].name);
  1789. str_data[n].type = C_FUNC;
  1790. str_data[n].val = i;
  1791. str_data[n].func = buildin_func[i].func;
  1792. }
  1793. }
  1794. }
  1795. /// Retrieves the value of a constant.
  1796. bool script_get_constant(const char* name, int* value)
  1797. {
  1798. int n = search_str(name);
  1799. if( n == -1 || str_data[n].type != C_INT )
  1800. {// not found or not a constant
  1801. return false;
  1802. }
  1803. value[0] = str_data[n].val;
  1804. return true;
  1805. }
  1806. /// Creates new constant or parameter with given value.
  1807. void script_set_constant(const char* name, int value, bool isparameter)
  1808. {
  1809. int n = add_str(name);
  1810. if( str_data[n].type == C_NOP )
  1811. {// new
  1812. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1813. str_data[n].val = value;
  1814. }
  1815. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1816. {// existing parameter or constant
  1817. ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%d, new value=%d).\n", ( str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, str_data[n].val, value);
  1818. }
  1819. else
  1820. {// existing name
  1821. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1822. }
  1823. }
  1824. /*==========================================
  1825. * 定数データベースの読み込み
  1826. *------------------------------------------*/
  1827. static void read_constdb(void)
  1828. {
  1829. FILE *fp;
  1830. char line[1024],name[1024],val[1024];
  1831. int type;
  1832. sprintf(line, "%s/const.txt", db_path);
  1833. fp=fopen(line, "r");
  1834. if(fp==NULL){
  1835. ShowError("can't read %s\n", line);
  1836. return ;
  1837. }
  1838. while(fgets(line, sizeof(line), fp))
  1839. {
  1840. if(line[0]=='/' && line[1]=='/')
  1841. continue;
  1842. type=0;
  1843. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1844. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1845. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  1846. }
  1847. }
  1848. fclose(fp);
  1849. }
  1850. /*==========================================
  1851. * エラー表示
  1852. *------------------------------------------*/
  1853. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1854. {
  1855. int i;
  1856. if( p == NULL || !p[0] ) return NULL;
  1857. if( line < 0 )
  1858. StringBuf_Printf(buf, "*% 5d : ", -line);
  1859. else
  1860. StringBuf_Printf(buf, " % 5d : ", line);
  1861. for(i=0;p[i] && p[i] != '\n';i++){
  1862. if(p + i != mark)
  1863. StringBuf_Printf(buf, "%c", p[i]);
  1864. else
  1865. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1866. }
  1867. StringBuf_AppendStr(buf, "\n");
  1868. return p+i+(p[i] == '\n' ? 1 : 0);
  1869. }
  1870. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1871. {
  1872. // エラーが発生した行を求める
  1873. int j;
  1874. int line = start_line;
  1875. const char *p;
  1876. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1877. StringBuf buf;
  1878. for(p=src;p && *p;line++){
  1879. const char *lineend=strchr(p,'\n');
  1880. if(lineend==NULL || error_pos<lineend){
  1881. break;
  1882. }
  1883. for( j = 0; j < 4; j++ ) {
  1884. linestart[j] = linestart[j+1];
  1885. }
  1886. linestart[4] = p;
  1887. p=lineend+1;
  1888. }
  1889. StringBuf_Init(&buf);
  1890. StringBuf_AppendStr(&buf, "\a\n");
  1891. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1892. StringBuf_Printf(&buf, " %s\n", error_msg);
  1893. for(j = 0; j < 5; j++ ) {
  1894. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1895. }
  1896. p = script_print_line(&buf, p, error_pos, -line);
  1897. for(j = 0; j < 5; j++) {
  1898. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1899. }
  1900. ShowError("%s", StringBuf_Value(&buf));
  1901. StringBuf_Destroy(&buf);
  1902. }
  1903. /*==========================================
  1904. * スクリプトの解析
  1905. *------------------------------------------*/
  1906. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1907. {
  1908. const char *p,*tmpp;
  1909. int i;
  1910. struct script_code* code = NULL;
  1911. static int first=1;
  1912. char end;
  1913. bool unresolved_names = false;
  1914. if( src == NULL )
  1915. return NULL;// empty script
  1916. memset(&syntax,0,sizeof(syntax));
  1917. if(first){
  1918. add_buildin_func();
  1919. read_constdb();
  1920. first=0;
  1921. }
  1922. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1923. script_pos=0;
  1924. script_size=SCRIPT_BLOCK_SIZE;
  1925. parse_nextline(true, NULL);
  1926. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1927. if( options&SCRIPT_USE_LABEL_DB )
  1928. db_clear(scriptlabel_db);
  1929. parse_options = options;
  1930. if( setjmp( error_jump ) != 0 ) {
  1931. //Restore program state when script has problems. [from jA]
  1932. int i;
  1933. const int size = ARRAYLENGTH(syntax.curly);
  1934. if( error_report )
  1935. script_error(src,file,line,error_msg,error_pos);
  1936. aFree( error_msg );
  1937. aFree( script_buf );
  1938. script_pos = 0;
  1939. script_size = 0;
  1940. script_buf = NULL;
  1941. for(i=LABEL_START;i<str_num;i++)
  1942. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1943. for(i=0; i<size; i++)
  1944. linkdb_final(&syntax.curly[i].case_label);
  1945. return NULL;
  1946. }
  1947. parse_syntax_for_flag=0;
  1948. p=src;
  1949. p=skip_space(p);
  1950. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1951. {// does not require brackets around the script
  1952. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1953. {// empty script and can return NULL
  1954. aFree( script_buf );
  1955. script_pos = 0;
  1956. script_size = 0;
  1957. script_buf = NULL;
  1958. return NULL;
  1959. }
  1960. end = '\0';
  1961. }
  1962. else
  1963. {// requires brackets around the script
  1964. if( *p != '{' )
  1965. disp_error_message("not found '{'",p);
  1966. p = skip_space(p+1);
  1967. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1968. {// empty script and can return NULL
  1969. aFree( script_buf );
  1970. script_pos = 0;
  1971. script_size = 0;
  1972. script_buf = NULL;
  1973. return NULL;
  1974. }
  1975. end = '}';
  1976. }
  1977. // clear references of labels, variables and internal functions
  1978. for(i=LABEL_START;i<str_num;i++){
  1979. if(
  1980. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1981. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1982. ){
  1983. str_data[i].type=C_NOP;
  1984. str_data[i].backpatch=-1;
  1985. str_data[i].label=-1;
  1986. }
  1987. }
  1988. while( syntax.curly_count != 0 || *p != end )
  1989. {
  1990. if( *p == '\0' )
  1991. disp_error_message("unexpected end of script",p);
  1992. // labelだけ特殊処理
  1993. tmpp=skip_space(skip_word(p));
  1994. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1995. i=add_word(p);
  1996. set_label(i,script_pos,p);
  1997. if( parse_options&SCRIPT_USE_LABEL_DB )
  1998. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  1999. p=tmpp+1;
  2000. p=skip_space(p);
  2001. continue;
  2002. }
  2003. // 他は全部一緒くた
  2004. p=parse_line(p);
  2005. p=skip_space(p);
  2006. parse_nextline(false, p);
  2007. }
  2008. add_scriptc(C_NOP);
  2009. // trim code to size
  2010. script_size = script_pos;
  2011. RECREATE(script_buf,unsigned char,script_pos);
  2012. // default unknown references to variables
  2013. for(i=LABEL_START;i<str_num;i++){
  2014. if(str_data[i].type==C_NOP){
  2015. int j,next;
  2016. str_data[i].type=C_NAME;
  2017. str_data[i].label=i;
  2018. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2019. next=GETVALUE(script_buf,j);
  2020. SETVALUE(script_buf,j,i);
  2021. j=next;
  2022. }
  2023. }
  2024. else if( str_data[i].type == C_USERFUNC )
  2025. {// 'function name;' without follow-up code
  2026. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2027. unresolved_names = true;
  2028. }
  2029. }
  2030. if( unresolved_names )
  2031. {
  2032. disp_error_message("parse_script: unresolved function references", p);
  2033. }
  2034. #ifdef DEBUG_DISP
  2035. for(i=0;i<script_pos;i++){
  2036. if((i&15)==0) ShowMessage("%04x : ",i);
  2037. ShowMessage("%02x ",script_buf[i]);
  2038. if((i&15)==15) ShowMessage("\n");
  2039. }
  2040. ShowMessage("\n");
  2041. #endif
  2042. #ifdef DEBUG_DISASM
  2043. {
  2044. int i = 0,j;
  2045. while(i < script_pos) {
  2046. c_op op = get_com(script_buf,&i);
  2047. ShowMessage("%06x %s", i, script_op2name(op));
  2048. j = i;
  2049. switch(op) {
  2050. case C_INT:
  2051. ShowMessage(" %d", get_num(script_buf,&i));
  2052. break;
  2053. case C_POS:
  2054. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2055. i += 3;
  2056. break;
  2057. case C_NAME:
  2058. j = (*(int*)(script_buf+i)&0xffffff);
  2059. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2060. i += 3;
  2061. break;
  2062. case C_STR:
  2063. j = strlen(script_buf + i);
  2064. ShowMessage(" %s", script_buf + i);
  2065. i += j+1;
  2066. break;
  2067. }
  2068. ShowMessage(CL_CLL"\n");
  2069. }
  2070. }
  2071. #endif
  2072. CREATE(code,struct script_code,1);
  2073. code->script_buf = script_buf;
  2074. code->script_size = script_size;
  2075. code->script_vars = NULL;
  2076. return code;
  2077. }
  2078. /// Returns the player attached to this script, identified by the rid.
  2079. /// If there is no player attached, the script is terminated.
  2080. TBL_PC *script_rid2sd(struct script_state *st)
  2081. {
  2082. TBL_PC *sd=map_id2sd(st->rid);
  2083. if(!sd){
  2084. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2085. script_reportfunc(st);
  2086. script_reportsrc(st);
  2087. st->state = END;
  2088. }
  2089. return sd;
  2090. }
  2091. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2092. ///
  2093. /// @param st Script state
  2094. /// @param data Variable/constant
  2095. void get_val(struct script_state* st, struct script_data* data)
  2096. {
  2097. const char* name;
  2098. char prefix;
  2099. char postfix;
  2100. TBL_PC* sd = NULL;
  2101. if( !data_isreference(data) )
  2102. return;// not a variable/constant
  2103. name = reference_getname(data);
  2104. prefix = name[0];
  2105. postfix = name[strlen(name) - 1];
  2106. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2107. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2108. {
  2109. sd = script_rid2sd(st);
  2110. if( sd == NULL )
  2111. {// needs player attached
  2112. if( postfix == '$' )
  2113. {// string variable
  2114. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2115. data->type = C_CONSTSTR;
  2116. data->u.str = "";
  2117. }
  2118. else
  2119. {// integer variable
  2120. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2121. data->type = C_INT;
  2122. data->u.num = 0;
  2123. }
  2124. return;
  2125. }
  2126. }
  2127. if( postfix == '$' )
  2128. {// string variable
  2129. switch( prefix )
  2130. {
  2131. case '@':
  2132. data->u.str = pc_readregstr(sd, data->u.num);
  2133. break;
  2134. case '$':
  2135. data->u.str = mapreg_readregstr(data->u.num);
  2136. break;
  2137. case '#':
  2138. if( name[1] == '#' )
  2139. data->u.str = pc_readaccountreg2str(sd, name);// global
  2140. else
  2141. data->u.str = pc_readaccountregstr(sd, name);// local
  2142. break;
  2143. case '.':
  2144. {
  2145. struct linkdb_node** n =
  2146. data->ref ? data->ref:
  2147. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2148. &st->script->script_vars;// npc variable
  2149. data->u.str = (char*)linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data)));
  2150. }
  2151. break;
  2152. case '\'':
  2153. {
  2154. struct linkdb_node** n = NULL;
  2155. if (st->instance_id) {
  2156. n = &instance[st->instance_id].svar;
  2157. } else {
  2158. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2159. }
  2160. data->u.str = (char*)linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data)));
  2161. }
  2162. break;
  2163. default:
  2164. data->u.str = pc_readglobalreg_str(sd, name);
  2165. break;
  2166. }
  2167. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2168. {// empty string
  2169. data->type = C_CONSTSTR;
  2170. data->u.str = "";
  2171. }
  2172. else
  2173. {// duplicate string
  2174. data->type = C_STR;
  2175. data->u.str = aStrdup(data->u.str);
  2176. }
  2177. }
  2178. else
  2179. {// integer variable
  2180. data->type = C_INT;
  2181. if( reference_toconstant(data) )
  2182. {
  2183. data->u.num = reference_getconstant(data);
  2184. }
  2185. else if( reference_toparam(data) )
  2186. {
  2187. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2188. }
  2189. else
  2190. switch( prefix )
  2191. {
  2192. case '@':
  2193. data->u.num = pc_readreg(sd, data->u.num);
  2194. break;
  2195. case '$':
  2196. data->u.num = mapreg_readreg(data->u.num);
  2197. break;
  2198. case '#':
  2199. if( name[1] == '#' )
  2200. data->u.num = pc_readaccountreg2(sd, name);// global
  2201. else
  2202. data->u.num = pc_readaccountreg(sd, name);// local
  2203. break;
  2204. case '.':
  2205. {
  2206. struct linkdb_node** n =
  2207. data->ref ? data->ref:
  2208. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2209. &st->script->script_vars;// npc variable
  2210. data->u.num = (int)__64BPRTSIZE(linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data))));
  2211. }
  2212. break;
  2213. case '\'':
  2214. {
  2215. struct linkdb_node** n = NULL;
  2216. if( st->instance_id )
  2217. n = &instance[st->instance_id].ivar;
  2218. data->u.num = (int)__64BPRTSIZE(linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data))));
  2219. }
  2220. break;
  2221. default:
  2222. data->u.num = pc_readglobalreg(sd, name);
  2223. break;
  2224. }
  2225. }
  2226. return;
  2227. }
  2228. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2229. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2230. /// The value is left in the top of the stack and needs to be removed manually.
  2231. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2232. {
  2233. struct script_data* data;
  2234. push_val2(st->stack, C_NAME, uid, ref);
  2235. data = script_getdatatop(st, -1);
  2236. get_val(st, data);
  2237. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2238. }
  2239. /*==========================================
  2240. * Stores the value of a script variable
  2241. * Return value is 0 on fail, 1 on success.
  2242. *------------------------------------------*/
  2243. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2244. {
  2245. char prefix = name[0];
  2246. if( is_string_variable(name) )
  2247. {// string variable
  2248. const char* str = (const char*)value;
  2249. switch (prefix) {
  2250. case '@':
  2251. return pc_setregstr(sd, num, str);
  2252. case '$':
  2253. return mapreg_setregstr(num, str);
  2254. case '#':
  2255. return (name[1] == '#') ?
  2256. pc_setaccountreg2str(sd, name, str) :
  2257. pc_setaccountregstr(sd, name, str);
  2258. case '.': {
  2259. char* p;
  2260. struct linkdb_node** n;
  2261. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2262. p = (char*)linkdb_erase(n, (void*)__64BPRTSIZE(num));
  2263. if (p) aFree(p);
  2264. if (str[0]) linkdb_insert(n, (void*)__64BPRTSIZE(num), aStrdup(str));
  2265. }
  2266. return 1;
  2267. case '\'': {
  2268. char *p;
  2269. struct linkdb_node** n = NULL;
  2270. if( st->instance_id )
  2271. n = &instance[st->instance_id].svar;
  2272. p = (char*)linkdb_erase(n, (void*)__64BPRTSIZE(num));
  2273. if (p) aFree(p);
  2274. if( str[0] ) linkdb_insert(n, (void*)__64BPRTSIZE(num), aStrdup(str));
  2275. }
  2276. return 1;
  2277. default:
  2278. return pc_setglobalreg_str(sd, name, str);
  2279. }
  2280. }
  2281. else
  2282. {// integer variable
  2283. int val = (int)__64BPRTSIZE(value);
  2284. if(str_data[num&0x00ffffff].type == C_PARAM)
  2285. {
  2286. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2287. {
  2288. if( st != NULL )
  2289. {
  2290. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2291. script_reportsrc(st);
  2292. st->state = END;
  2293. }
  2294. return 0;
  2295. }
  2296. return 1;
  2297. }
  2298. switch (prefix) {
  2299. case '@':
  2300. return pc_setreg(sd, num, val);
  2301. case '$':
  2302. return mapreg_setreg(num, val);
  2303. case '#':
  2304. return (name[1] == '#') ?
  2305. pc_setaccountreg2(sd, name, val) :
  2306. pc_setaccountreg(sd, name, val);
  2307. case '.': {
  2308. struct linkdb_node** n;
  2309. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2310. if (val == 0)
  2311. linkdb_erase(n, (void*)__64BPRTSIZE(num));
  2312. else
  2313. linkdb_replace(n, (void*)__64BPRTSIZE(num), (void*)__64BPRTSIZE(val));
  2314. }
  2315. return 1;
  2316. case '\'':
  2317. {
  2318. struct linkdb_node** n = NULL;
  2319. if( st->instance_id )
  2320. n = &instance[st->instance_id].ivar;
  2321. if( val == 0 )
  2322. linkdb_erase(n, (void*)__64BPRTSIZE(num));
  2323. else
  2324. linkdb_replace(n, (void*)__64BPRTSIZE(num), (void*)__64BPRTSIZE(val));
  2325. return 1;
  2326. }
  2327. default:
  2328. return pc_setglobalreg(sd, name, val);
  2329. }
  2330. }
  2331. }
  2332. int set_var(TBL_PC* sd, char* name, void* val)
  2333. {
  2334. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2335. }
  2336. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2337. {
  2338. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2339. }
  2340. /// Converts the data to a string
  2341. const char* conv_str(struct script_state* st, struct script_data* data)
  2342. {
  2343. char* p;
  2344. get_val(st, data);
  2345. if( data_isstring(data) )
  2346. {// nothing to convert
  2347. }
  2348. else if( data_isint(data) )
  2349. {// int -> string
  2350. CREATE(p, char, ITEM_NAME_LENGTH);
  2351. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2352. p[ITEM_NAME_LENGTH-1] = '\0';
  2353. data->type = C_STR;
  2354. data->u.str = p;
  2355. }
  2356. else if( data_isreference(data) )
  2357. {// reference -> string
  2358. //##TODO when does this happen (check get_val) [FlavioJS]
  2359. data->type = C_CONSTSTR;
  2360. data->u.str = reference_getname(data);
  2361. }
  2362. else
  2363. {// unsupported data type
  2364. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2365. script_reportdata(data);
  2366. script_reportsrc(st);
  2367. data->type = C_CONSTSTR;
  2368. data->u.str = "";
  2369. }
  2370. return data->u.str;
  2371. }
  2372. /// Converts the data to an int
  2373. int conv_num(struct script_state* st, struct script_data* data)
  2374. {
  2375. char* p;
  2376. long num;
  2377. get_val(st, data);
  2378. if( data_isint(data) )
  2379. {// nothing to convert
  2380. }
  2381. else if( data_isstring(data) )
  2382. {// string -> int
  2383. // the result does not overflow or underflow, it is capped instead
  2384. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2385. p = data->u.str;
  2386. errno = 0;
  2387. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2388. if( errno == ERANGE
  2389. #if LONG_MAX > INT_MAX
  2390. || num < INT_MIN || num > INT_MAX
  2391. #endif
  2392. )
  2393. {
  2394. if( num <= INT_MIN )
  2395. {
  2396. num = INT_MIN;
  2397. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2398. }
  2399. else//if( num >= INT_MAX )
  2400. {
  2401. num = INT_MAX;
  2402. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2403. }
  2404. script_reportdata(data);
  2405. script_reportsrc(st);
  2406. }
  2407. if( data->type == C_STR )
  2408. aFree(p);
  2409. data->type = C_INT;
  2410. data->u.num = (int)num;
  2411. }
  2412. #if 0
  2413. // FIXME this function is being used to retrieve the position of labels and
  2414. // probably other stuff [FlavioJS]
  2415. else
  2416. {// unsupported data type
  2417. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2418. script_reportdata(data);
  2419. script_reportsrc(st);
  2420. data->type = C_INT;
  2421. data->u.num = 0;
  2422. }
  2423. #endif
  2424. return data->u.num;
  2425. }
  2426. //
  2427. // Stack operations
  2428. //
  2429. /// Increases the size of the stack
  2430. void stack_expand(struct script_stack* stack)
  2431. {
  2432. stack->sp_max += 64;
  2433. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2434. stack->sp_max * sizeof(stack->stack_data[0]) );
  2435. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2436. 64 * sizeof(stack->stack_data[0]) );
  2437. }
  2438. /// Pushes a value into the stack
  2439. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2440. /// Pushes a value into the stack (with reference)
  2441. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2442. {
  2443. if( stack->sp >= stack->sp_max )
  2444. stack_expand(stack);
  2445. stack->stack_data[stack->sp].type = type;
  2446. stack->stack_data[stack->sp].u.num = val;
  2447. stack->stack_data[stack->sp].ref = ref;
  2448. stack->sp++;
  2449. return &stack->stack_data[stack->sp-1];
  2450. }
  2451. /// Pushes a string into the stack
  2452. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2453. {
  2454. if( stack->sp >= stack->sp_max )
  2455. stack_expand(stack);
  2456. stack->stack_data[stack->sp].type = type;
  2457. stack->stack_data[stack->sp].u.str = str;
  2458. stack->stack_data[stack->sp].ref = NULL;
  2459. stack->sp++;
  2460. return &stack->stack_data[stack->sp-1];
  2461. }
  2462. /// Pushes a retinfo into the stack
  2463. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2464. {
  2465. if( stack->sp >= stack->sp_max )
  2466. stack_expand(stack);
  2467. stack->stack_data[stack->sp].type = C_RETINFO;
  2468. stack->stack_data[stack->sp].u.ri = ri;
  2469. stack->stack_data[stack->sp].ref = NULL;
  2470. stack->sp++;
  2471. return &stack->stack_data[stack->sp-1];
  2472. }
  2473. /// Pushes a copy of the target position into the stack
  2474. struct script_data* push_copy(struct script_stack* stack, int pos)
  2475. {
  2476. switch( stack->stack_data[pos].type )
  2477. {
  2478. case C_CONSTSTR:
  2479. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2480. break;
  2481. case C_STR:
  2482. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2483. break;
  2484. case C_RETINFO:
  2485. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2486. exit(1);
  2487. break;
  2488. default:
  2489. return push_val2(
  2490. stack,stack->stack_data[pos].type,
  2491. stack->stack_data[pos].u.num,
  2492. stack->stack_data[pos].ref
  2493. );
  2494. break;
  2495. }
  2496. }
  2497. /// Removes the values in indexes [start,end[ from the stack.
  2498. /// Adjusts all stack pointers.
  2499. void pop_stack(struct script_state* st, int start, int end)
  2500. {
  2501. struct script_stack* stack = st->stack;
  2502. struct script_data* data;
  2503. int i;
  2504. if( start < 0 )
  2505. start = 0;
  2506. if( end > stack->sp )
  2507. end = stack->sp;
  2508. if( start >= end )
  2509. return;// nothing to pop
  2510. // free stack elements
  2511. for( i = start; i < end; i++ )
  2512. {
  2513. data = &stack->stack_data[i];
  2514. if( data->type == C_STR )
  2515. aFree(data->u.str);
  2516. if( data->type == C_RETINFO )
  2517. {
  2518. struct script_retinfo* ri = data->u.ri;
  2519. if( ri->var_function )
  2520. {
  2521. script_free_vars(ri->var_function);
  2522. aFree(ri->var_function);
  2523. }
  2524. aFree(ri);
  2525. }
  2526. data->type = C_NOP;
  2527. }
  2528. // move the rest of the elements
  2529. if( stack->sp > end )
  2530. {
  2531. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2532. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2533. stack->stack_data[i].type = C_NOP;
  2534. }
  2535. // adjust stack pointers
  2536. if( st->start > end ) st->start -= end - start;
  2537. else if( st->start > start ) st->start = start;
  2538. if( st->end > end ) st->end -= end - start;
  2539. else if( st->end > start ) st->end = start;
  2540. if( stack->defsp > end ) stack->defsp -= end - start;
  2541. else if( stack->defsp > start ) stack->defsp = start;
  2542. stack->sp -= end - start;
  2543. }
  2544. ///
  2545. ///
  2546. ///
  2547. /*==========================================
  2548. * スクリプト依存変数、関数依存変数の解放
  2549. *------------------------------------------*/
  2550. void script_free_vars(struct linkdb_node **node)
  2551. {
  2552. struct linkdb_node* n = *node;
  2553. while( n != NULL)
  2554. {
  2555. const char* name = get_str((int)__64BPRTSIZE((n->key)&0x00ffffff));
  2556. if( is_string_variable(name) )
  2557. aFree(n->data); // 文字型変数なので、データ削除
  2558. n = n->next;
  2559. }
  2560. linkdb_final( node );
  2561. }
  2562. void script_free_code(struct script_code* code)
  2563. {
  2564. script_free_vars( &code->script_vars );
  2565. aFree( code->script_buf );
  2566. aFree( code );
  2567. }
  2568. /// Creates a new script state.
  2569. ///
  2570. /// @param script Script code
  2571. /// @param pos Position in the code
  2572. /// @param rid Who is running the script (attached player)
  2573. /// @param oid Where the code is being run (npc 'object')
  2574. /// @return Script state
  2575. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2576. {
  2577. struct script_state* st;
  2578. CREATE(st, struct script_state, 1);
  2579. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2580. st->stack->sp = 0;
  2581. st->stack->sp_max = 64;
  2582. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2583. st->stack->defsp = st->stack->sp;
  2584. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2585. st->state = RUN;
  2586. st->script = script;
  2587. //st->scriptroot = script;
  2588. st->pos = pos;
  2589. st->rid = rid;
  2590. st->oid = oid;
  2591. st->sleep.timer = INVALID_TIMER;
  2592. return st;
  2593. }
  2594. /// Frees a script state.
  2595. ///
  2596. /// @param st Script state
  2597. void script_free_state(struct script_state* st)
  2598. {
  2599. if(st->bk_st)
  2600. {// backup was not restored
  2601. 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);
  2602. }
  2603. if( st->sleep.timer != INVALID_TIMER )
  2604. delete_timer(st->sleep.timer, run_script_timer);
  2605. script_free_vars(st->stack->var_function);
  2606. aFree(st->stack->var_function);
  2607. pop_stack(st, 0, st->stack->sp);
  2608. aFree(st->stack->stack_data);
  2609. aFree(st->stack);
  2610. st->pos = -1;
  2611. aFree(st);
  2612. }
  2613. //
  2614. // 実行部main
  2615. //
  2616. /*==========================================
  2617. * コマンドの読み取り
  2618. *------------------------------------------*/
  2619. c_op get_com(unsigned char *script,int *pos)
  2620. {
  2621. int i = 0, j = 0;
  2622. if(script[*pos]>=0x80){
  2623. return C_INT;
  2624. }
  2625. while(script[*pos]>=0x40){
  2626. i=script[(*pos)++]<<j;
  2627. j+=6;
  2628. }
  2629. return (c_op)(i+(script[(*pos)++]<<j));
  2630. }
  2631. /*==========================================
  2632. * 数値の所得
  2633. *------------------------------------------*/
  2634. int get_num(unsigned char *script,int *pos)
  2635. {
  2636. int i,j;
  2637. i=0; j=0;
  2638. while(script[*pos]>=0xc0){
  2639. i+=(script[(*pos)++]&0x7f)<<j;
  2640. j+=6;
  2641. }
  2642. return i+((script[(*pos)++]&0x7f)<<j);
  2643. }
  2644. /*==========================================
  2645. * スタックから値を取り出す
  2646. *------------------------------------------*/
  2647. int pop_val(struct script_state* st)
  2648. {
  2649. if(st->stack->sp<=0)
  2650. return 0;
  2651. st->stack->sp--;
  2652. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2653. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2654. return st->stack->stack_data[st->stack->sp].u.num;
  2655. return 0;
  2656. }
  2657. /// Ternary operators
  2658. /// test ? if_true : if_false
  2659. void op_3(struct script_state* st, int op)
  2660. {
  2661. struct script_data* data;
  2662. int flag = 0;
  2663. data = script_getdatatop(st, -3);
  2664. get_val(st, data);
  2665. if( data_isstring(data) )
  2666. flag = data->u.str[0];// "" -> false
  2667. else if( data_isint(data) )
  2668. flag = data->u.num;// 0 -> false
  2669. else
  2670. {
  2671. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2672. script_reportdata(data);
  2673. script_reportsrc(st);
  2674. script_removetop(st, -3, 0);
  2675. script_pushnil(st);
  2676. return;
  2677. }
  2678. if( flag )
  2679. script_pushcopytop(st, -2);
  2680. else
  2681. script_pushcopytop(st, -1);
  2682. script_removetop(st, -4, -1);
  2683. }
  2684. /// Binary string operators
  2685. /// s1 EQ s2 -> i
  2686. /// s1 NE s2 -> i
  2687. /// s1 GT s2 -> i
  2688. /// s1 GE s2 -> i
  2689. /// s1 LT s2 -> i
  2690. /// s1 LE s2 -> i
  2691. /// s1 ADD s2 -> s
  2692. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2693. {
  2694. int a = 0;
  2695. switch(op){
  2696. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2697. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2698. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2699. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2700. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2701. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2702. case C_ADD:
  2703. {
  2704. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2705. strcpy(buf, s1);
  2706. strcat(buf, s2);
  2707. script_pushstr(st, buf);
  2708. return;
  2709. }
  2710. default:
  2711. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2712. script_reportsrc(st);
  2713. script_pushnil(st);
  2714. st->state = END;
  2715. return;
  2716. }
  2717. script_pushint(st,a);
  2718. }
  2719. /// Binary number operators
  2720. /// i OP i -> i
  2721. void op_2num(struct script_state* st, int op, int i1, int i2)
  2722. {
  2723. int ret;
  2724. double ret_double;
  2725. switch( op )
  2726. {
  2727. case C_AND: ret = i1 & i2; break;
  2728. case C_OR: ret = i1 | i2; break;
  2729. case C_XOR: ret = i1 ^ i2; break;
  2730. case C_LAND: ret = (i1 && i2); break;
  2731. case C_LOR: ret = (i1 || i2); break;
  2732. case C_EQ: ret = (i1 == i2); break;
  2733. case C_NE: ret = (i1 != i2); break;
  2734. case C_GT: ret = (i1 > i2); break;
  2735. case C_GE: ret = (i1 >= i2); break;
  2736. case C_LT: ret = (i1 < i2); break;
  2737. case C_LE: ret = (i1 <= i2); break;
  2738. case C_R_SHIFT: ret = i1>>i2; break;
  2739. case C_L_SHIFT: ret = i1<<i2; break;
  2740. case C_DIV:
  2741. case C_MOD:
  2742. if( i2 == 0 )
  2743. {
  2744. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2745. script_reportsrc(st);
  2746. script_pushnil(st);
  2747. st->state = END;
  2748. return;
  2749. }
  2750. else if( op == C_DIV )
  2751. ret = i1 / i2;
  2752. else//if( op == C_MOD )
  2753. ret = i1 % i2;
  2754. break;
  2755. default:
  2756. switch( op )
  2757. {// operators that can overflow/underflow
  2758. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2759. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2760. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2761. default:
  2762. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2763. script_reportsrc(st);
  2764. script_pushnil(st);
  2765. return;
  2766. }
  2767. if( ret_double < (double)INT_MIN )
  2768. {
  2769. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2770. script_reportsrc(st);
  2771. ret = INT_MIN;
  2772. }
  2773. else if( ret_double > (double)INT_MAX )
  2774. {
  2775. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2776. script_reportsrc(st);
  2777. ret = INT_MAX;
  2778. }
  2779. }
  2780. script_pushint(st, ret);
  2781. }
  2782. /// Binary operators
  2783. void op_2(struct script_state *st, int op)
  2784. {
  2785. struct script_data* left;
  2786. struct script_data* right;
  2787. left = script_getdatatop(st, -2);
  2788. right = script_getdatatop(st, -1);
  2789. get_val(st, left);
  2790. get_val(st, right);
  2791. // automatic conversions
  2792. switch( op )
  2793. {
  2794. case C_ADD:
  2795. if( data_isint(left) && data_isstring(right) )
  2796. {// convert int-string to string-string
  2797. conv_str(st, left);
  2798. }
  2799. else if( data_isstring(left) && data_isint(right) )
  2800. {// convert string-int to string-string
  2801. conv_str(st, right);
  2802. }
  2803. break;
  2804. }
  2805. if( data_isstring(left) && data_isstring(right) )
  2806. {// ss => op_2str
  2807. op_2str(st, op, left->u.str, right->u.str);
  2808. script_removetop(st, -3, -1);// pop the two values before the top one
  2809. }
  2810. else if( data_isint(left) && data_isint(right) )
  2811. {// ii => op_2num
  2812. int i1 = left->u.num;
  2813. int i2 = right->u.num;
  2814. script_removetop(st, -2, 0);
  2815. op_2num(st, op, i1, i2);
  2816. }
  2817. else
  2818. {// invalid argument
  2819. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2820. script_reportdata(left);
  2821. script_reportdata(right);
  2822. script_reportsrc(st);
  2823. script_removetop(st, -2, 0);
  2824. script_pushnil(st);
  2825. st->state = END;
  2826. }
  2827. }
  2828. /// Unary operators
  2829. /// NEG i -> i
  2830. /// NOT i -> i
  2831. /// LNOT i -> i
  2832. void op_1(struct script_state* st, int op)
  2833. {
  2834. struct script_data* data;
  2835. int i1;
  2836. data = script_getdatatop(st, -1);
  2837. get_val(st, data);
  2838. if( !data_isint(data) )
  2839. {// not a number
  2840. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2841. script_reportdata(data);
  2842. script_reportsrc(st);
  2843. script_pushnil(st);
  2844. st->state = END;
  2845. return;
  2846. }
  2847. i1 = data->u.num;
  2848. script_removetop(st, -1, 0);
  2849. switch( op )
  2850. {
  2851. case C_NEG: i1 = -i1; break;
  2852. case C_NOT: i1 = ~i1; break;
  2853. case C_LNOT: i1 = !i1; break;
  2854. default:
  2855. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2856. script_reportsrc(st);
  2857. script_pushnil(st);
  2858. st->state = END;
  2859. return;
  2860. }
  2861. script_pushint(st, i1);
  2862. }
  2863. /// Checks the type of all arguments passed to a built-in function.
  2864. ///
  2865. /// @param st Script state whose stack arguments should be inspected.
  2866. /// @param func Built-in function for which the arguments are intended.
  2867. static void script_check_buildin_argtype(struct script_state* st, int func)
  2868. {
  2869. char type;
  2870. int idx, invalid = 0;
  2871. script_function* sf = &buildin_func[str_data[func].val];
  2872. for( idx = 2; script_hasdata(st, idx); idx++ )
  2873. {
  2874. struct script_data* data = script_getdata(st, idx);
  2875. type = sf->arg[idx-2];
  2876. if( type == '?' || type == '*' )
  2877. {// optional argument or unknown number of optional parameters ( no types are after this )
  2878. break;
  2879. }
  2880. else if( type == 0 )
  2881. {// more arguments than necessary ( should not happen, as it is checked before )
  2882. ShowWarning("Found more arguments than necessary.\n");
  2883. invalid++;
  2884. break;
  2885. }
  2886. else
  2887. {
  2888. const char* name = NULL;
  2889. if( data_isreference(data) )
  2890. {// get name for variables to determine the type they refer to
  2891. name = reference_getname(data);
  2892. }
  2893. switch( type )
  2894. {
  2895. case 'v':
  2896. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  2897. {// variant
  2898. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  2899. script_reportdata(data);
  2900. invalid++;
  2901. }
  2902. break;
  2903. case 's':
  2904. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  2905. {// string
  2906. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  2907. script_reportdata(data);
  2908. invalid++;
  2909. }
  2910. break;
  2911. case 'i':
  2912. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  2913. {// int ( params and constants are always int )
  2914. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  2915. script_reportdata(data);
  2916. invalid++;
  2917. }
  2918. break;
  2919. case 'r':
  2920. if( !data_isreference(data) )
  2921. {// variables
  2922. ShowWarning("Unexpected type for argument %d. Expected variable.\n", idx-1);
  2923. script_reportdata(data);
  2924. invalid++;
  2925. }
  2926. break;
  2927. case 'l':
  2928. if( !data_islabel(data) && !data_isfunclabel(data) )
  2929. {// label
  2930. ShowWarning("Unexpected type for argument %d. Expected label.\n", idx-1);
  2931. script_reportdata(data);
  2932. invalid++;
  2933. }
  2934. break;
  2935. }
  2936. }
  2937. }
  2938. if(invalid)
  2939. {
  2940. ShowDebug("Function: %s\n", get_str(func));
  2941. script_reportsrc(st);
  2942. }
  2943. }
  2944. /// Executes a buildin command.
  2945. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2946. int run_func(struct script_state *st)
  2947. {
  2948. struct script_data* data;
  2949. int i,start_sp,end_sp,func;
  2950. end_sp = st->stack->sp;// position after the last argument
  2951. for( i = end_sp-1; i > 0 ; --i )
  2952. if( st->stack->stack_data[i].type == C_ARG )
  2953. break;
  2954. if( i == 0 )
  2955. {
  2956. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2957. st->state = END;
  2958. script_reportsrc(st);
  2959. return 1;
  2960. }
  2961. start_sp = i-1;// C_NAME of the command
  2962. st->start = start_sp;
  2963. st->end = end_sp;
  2964. data = &st->stack->stack_data[st->start];
  2965. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2966. func = data->u.num;
  2967. else
  2968. {
  2969. ShowError("script:run_func: not a buildin command.\n");
  2970. script_reportdata(data);
  2971. script_reportsrc(st);
  2972. st->state = END;
  2973. return 1;
  2974. }
  2975. if( script_config.warn_func_mismatch_argtypes )
  2976. {
  2977. script_check_buildin_argtype(st, func);
  2978. }
  2979. if(str_data[func].func){
  2980. if (str_data[func].func(st)) //Report error
  2981. script_reportsrc(st);
  2982. } else {
  2983. 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));
  2984. script_reportsrc(st);
  2985. st->state = END;
  2986. }
  2987. // Stack's datum are used when re-running functions [Eoe]
  2988. if( st->state == RERUNLINE )
  2989. return 0;
  2990. pop_stack(st, st->start, st->end);
  2991. if( st->state == RETFUNC )
  2992. {// return from a user-defined function
  2993. struct script_retinfo* ri;
  2994. int olddefsp = st->stack->defsp;
  2995. int nargs;
  2996. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2997. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2998. {
  2999. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3000. script_reportsrc(st);
  3001. st->state = END;
  3002. return 1;
  3003. }
  3004. script_free_vars( st->stack->var_function );
  3005. aFree(st->stack->var_function);
  3006. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3007. nargs = ri->nargs;
  3008. st->pos = ri->pos;
  3009. st->script = ri->script;
  3010. st->stack->var_function = ri->var_function;
  3011. st->stack->defsp = ri->defsp;
  3012. memset(ri, 0, sizeof(struct script_retinfo));
  3013. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3014. st->state = GOTO;
  3015. }
  3016. return 0;
  3017. }
  3018. /*==========================================
  3019. * script execution
  3020. *------------------------------------------*/
  3021. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3022. {
  3023. struct script_state *st;
  3024. if( rootscript == NULL || pos < 0 )
  3025. return;
  3026. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3027. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3028. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3029. st = script_alloc_state(rootscript, pos, rid, oid);
  3030. run_script_main(st);
  3031. }
  3032. void script_stop_sleeptimers(int id)
  3033. {
  3034. struct script_state* st;
  3035. for(;;)
  3036. {
  3037. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3038. if( st == NULL )
  3039. break; // no more sleep timers
  3040. script_free_state(st);
  3041. }
  3042. }
  3043. /*==========================================
  3044. * 指定ノードをsleep_dbから削除
  3045. *------------------------------------------*/
  3046. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3047. {
  3048. struct linkdb_node *retnode;
  3049. if( n == NULL)
  3050. return NULL;
  3051. if( n->prev == NULL )
  3052. sleep_db = n->next;
  3053. else
  3054. n->prev->next = n->next;
  3055. if( n->next )
  3056. n->next->prev = n->prev;
  3057. retnode = n->next;
  3058. aFree( n );
  3059. return retnode; // 次のノードを返す
  3060. }
  3061. /*==========================================
  3062. * sleep用タイマー関数
  3063. *------------------------------------------*/
  3064. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3065. {
  3066. struct script_state *st = (struct script_state *)data;
  3067. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3068. TBL_PC *sd = map_id2sd(st->rid);
  3069. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3070. { //Character mismatch. Cancel execution.
  3071. st->rid = 0;
  3072. st->state = END;
  3073. }
  3074. while( node && st->sleep.timer != INVALID_TIMER ) {
  3075. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3076. script_erase_sleepdb(node);
  3077. st->sleep.timer = INVALID_TIMER;
  3078. break;
  3079. }
  3080. node = node->next;
  3081. }
  3082. if(st->state != RERUNLINE)
  3083. st->sleep.tick = 0;
  3084. run_script_main(st);
  3085. return 0;
  3086. }
  3087. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3088. ///
  3089. /// @param st Script state to detach.
  3090. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3091. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3092. {
  3093. struct map_session_data* sd;
  3094. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3095. {
  3096. sd->st = st->bk_st;
  3097. sd->npc_id = st->bk_npcid;
  3098. /**
  3099. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3100. **/
  3101. #if SECURE_NPCTIMEOUT
  3102. /**
  3103. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3104. **/
  3105. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3106. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3107. sd->npc_idle_timer = INVALID_TIMER;
  3108. }
  3109. #endif
  3110. if(st->bk_st)
  3111. {
  3112. //Remove tag for removal.
  3113. st->bk_st = NULL;
  3114. st->bk_npcid = 0;
  3115. }
  3116. else if(dequeue_event)
  3117. {
  3118. npc_event_dequeue(sd);
  3119. }
  3120. }
  3121. else if(st->bk_st)
  3122. {// rid was set to 0, before detaching the script state
  3123. 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);
  3124. script_reportsrc(st->bk_st);
  3125. script_free_state(st->bk_st);
  3126. st->bk_st = NULL;
  3127. }
  3128. }
  3129. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3130. ///
  3131. /// @param st Script state to attach.
  3132. static void script_attach_state(struct script_state* st)
  3133. {
  3134. struct map_session_data* sd;
  3135. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3136. {
  3137. if(st!=sd->st)
  3138. {
  3139. if(st->bk_st)
  3140. {// there is already a backup
  3141. 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);
  3142. }
  3143. st->bk_st = sd->st;
  3144. st->bk_npcid = sd->npc_id;
  3145. }
  3146. sd->st = st;
  3147. sd->npc_id = st->oid;
  3148. /**
  3149. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3150. **/
  3151. #if SECURE_NPCTIMEOUT
  3152. if( sd->npc_idle_timer == INVALID_TIMER )
  3153. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3154. sd->npc_idle_tick = gettick();
  3155. #endif
  3156. }
  3157. }
  3158. /*==========================================
  3159. * スクリプトの実行メイン部分
  3160. *------------------------------------------*/
  3161. void run_script_main(struct script_state *st)
  3162. {
  3163. int cmdcount=script_config.check_cmdcount;
  3164. int gotocount=script_config.check_gotocount;
  3165. TBL_PC *sd;
  3166. struct script_stack *stack=st->stack;
  3167. struct npc_data *nd;
  3168. script_attach_state(st);
  3169. nd = map_id2nd(st->oid);
  3170. if( nd && map[nd->bl.m].instance_id > 0 )
  3171. st->instance_id = map[nd->bl.m].instance_id;
  3172. if(st->state == RERUNLINE) {
  3173. run_func(st);
  3174. if(st->state == GOTO)
  3175. st->state = RUN;
  3176. } else if(st->state != END)
  3177. st->state = RUN;
  3178. while(st->state == RUN)
  3179. {
  3180. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3181. switch(c){
  3182. case C_EOL:
  3183. if( stack->defsp > stack->sp )
  3184. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3185. else
  3186. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3187. break;
  3188. case C_INT:
  3189. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3190. break;
  3191. case C_POS:
  3192. case C_NAME:
  3193. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3194. st->pos+=3;
  3195. break;
  3196. case C_ARG:
  3197. push_val(stack,c,0);
  3198. break;
  3199. case C_STR:
  3200. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3201. while(st->script->script_buf[st->pos++]);
  3202. break;
  3203. case C_FUNC:
  3204. run_func(st);
  3205. if(st->state==GOTO){
  3206. st->state = RUN;
  3207. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3208. ShowError("run_script: infinity loop !\n");
  3209. script_reportsrc(st);
  3210. st->state=END;
  3211. }
  3212. }
  3213. break;
  3214. case C_NEG:
  3215. case C_NOT:
  3216. case C_LNOT:
  3217. op_1(st ,c);
  3218. break;
  3219. case C_ADD:
  3220. case C_SUB:
  3221. case C_MUL:
  3222. case C_DIV:
  3223. case C_MOD:
  3224. case C_EQ:
  3225. case C_NE:
  3226. case C_GT:
  3227. case C_GE:
  3228. case C_LT:
  3229. case C_LE:
  3230. case C_AND:
  3231. case C_OR:
  3232. case C_XOR:
  3233. case C_LAND:
  3234. case C_LOR:
  3235. case C_R_SHIFT:
  3236. case C_L_SHIFT:
  3237. op_2(st, c);
  3238. break;
  3239. case C_OP3:
  3240. op_3(st, c);
  3241. break;
  3242. case C_NOP:
  3243. st->state=END;
  3244. break;
  3245. default:
  3246. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3247. st->state=END;
  3248. break;
  3249. }
  3250. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3251. ShowError("run_script: infinity loop !\n");
  3252. script_reportsrc(st);
  3253. st->state=END;
  3254. }
  3255. }
  3256. if(st->sleep.tick > 0) {
  3257. //Restore previous script
  3258. script_detach_state(st, false);
  3259. //Delay execution
  3260. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3261. st->sleep.charid = sd?sd->status.char_id:0;
  3262. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3263. run_script_timer, st->sleep.charid, (intptr_t)st);
  3264. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3265. }
  3266. else if(st->state != END && st->rid){
  3267. //Resume later (st is already attached to player).
  3268. if(st->bk_st) {
  3269. ShowWarning("Unable to restore stack! Double continuation!\n");
  3270. //Report BOTH scripts to see if that can help somehow.
  3271. ShowDebug("Previous script (lost):\n");
  3272. script_reportsrc(st->bk_st);
  3273. ShowDebug("Current script:\n");
  3274. script_reportsrc(st);
  3275. script_free_state(st->bk_st);
  3276. st->bk_st = NULL;
  3277. }
  3278. } else {
  3279. //Dispose of script.
  3280. if ((sd = map_id2sd(st->rid))!=NULL)
  3281. { //Restore previous stack and save char.
  3282. if(sd->state.using_fake_npc){
  3283. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3284. sd->state.using_fake_npc = 0;
  3285. }
  3286. //Restore previous script if any.
  3287. script_detach_state(st, true);
  3288. if (sd->state.reg_dirty&2)
  3289. intif_saveregistry(sd,2);
  3290. if (sd->state.reg_dirty&1)
  3291. intif_saveregistry(sd,1);
  3292. }
  3293. script_free_state(st);
  3294. st = NULL;
  3295. }
  3296. }
  3297. int script_config_read(char *cfgName)
  3298. {
  3299. int i;
  3300. char line[1024],w1[1024],w2[1024];
  3301. FILE *fp;
  3302. fp=fopen(cfgName,"r");
  3303. if(fp==NULL){
  3304. ShowError("file not found: [%s]\n", cfgName);
  3305. return 1;
  3306. }
  3307. while(fgets(line, sizeof(line), fp))
  3308. {
  3309. if(line[0] == '/' && line[1] == '/')
  3310. continue;
  3311. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3312. if(i!=2)
  3313. continue;
  3314. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3315. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3316. }
  3317. else if(strcmpi(w1,"check_cmdcount")==0) {
  3318. script_config.check_cmdcount = config_switch(w2);
  3319. }
  3320. else if(strcmpi(w1,"check_gotocount")==0) {
  3321. script_config.check_gotocount = config_switch(w2);
  3322. }
  3323. else if(strcmpi(w1,"input_min_value")==0) {
  3324. script_config.input_min_value = config_switch(w2);
  3325. }
  3326. else if(strcmpi(w1,"input_max_value")==0) {
  3327. script_config.input_max_value = config_switch(w2);
  3328. }
  3329. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3330. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3331. }
  3332. else if(strcmpi(w1,"import")==0){
  3333. script_config_read(w2);
  3334. }
  3335. }
  3336. fclose(fp);
  3337. return 0;
  3338. }
  3339. /**
  3340. * @see DBApply
  3341. */
  3342. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3343. {
  3344. struct script_code *code = db_data2ptr(data);
  3345. if (code)
  3346. script_free_code(code);
  3347. return 0;
  3348. }
  3349. void script_run_autobonus(const char *autobonus, int id, int pos)
  3350. {
  3351. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3352. if( script )
  3353. {
  3354. current_equip_item_index = pos;
  3355. run_script(script,0,id,0);
  3356. }
  3357. }
  3358. void script_add_autobonus(const char *autobonus)
  3359. {
  3360. if( strdb_get(autobonus_db, autobonus) == NULL )
  3361. {
  3362. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3363. if( script )
  3364. strdb_put(autobonus_db, autobonus, script);
  3365. }
  3366. }
  3367. /// resets a temporary character array variable to given value
  3368. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3369. {
  3370. int key;
  3371. uint8 idx;
  3372. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3373. {
  3374. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3375. return;
  3376. }
  3377. key = add_str(varname);
  3378. if( is_string_variable(varname) )
  3379. {
  3380. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3381. {
  3382. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3383. }
  3384. }
  3385. else
  3386. {
  3387. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3388. {
  3389. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3390. }
  3391. }
  3392. }
  3393. /// sets a temporary character array variable element idx to given value
  3394. /// @param refcache Pointer to an int variable, which keeps a copy of the reference to varname and must be initialized to 0. Can be NULL if only one element is set.
  3395. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3396. {
  3397. int key;
  3398. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3399. {
  3400. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3401. return;
  3402. }
  3403. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3404. {
  3405. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3406. return;
  3407. }
  3408. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3409. if( is_string_variable(varname) )
  3410. {
  3411. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3412. }
  3413. else
  3414. {
  3415. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3416. }
  3417. if( refcache )
  3418. {// save to avoid repeated add_str calls
  3419. refcache[0] = key;
  3420. }
  3421. }
  3422. /*==========================================
  3423. * 終了
  3424. *------------------------------------------*/
  3425. int do_final_script()
  3426. {
  3427. #ifdef DEBUG_HASH
  3428. if (battle_config.etc_log)
  3429. {
  3430. FILE *fp = fopen("hash_dump.txt","wt");
  3431. if(fp) {
  3432. int i,count[SCRIPT_HASH_SIZE];
  3433. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3434. int n=0;
  3435. int min=INT_MAX,max=0,zero=0;
  3436. double mean=0.0f;
  3437. double median=0.0f;
  3438. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3439. memset(count, 0, sizeof(count));
  3440. fprintf(fp,"num : hash : data_name\n");
  3441. fprintf(fp,"---------------------------------------------------------------\n");
  3442. for(i=LABEL_START; i<str_num; i++) {
  3443. unsigned int h = calc_hash(get_str(i));
  3444. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3445. ++count[h];
  3446. }
  3447. fprintf(fp,"--------------------\n\n");
  3448. memset(count2, 0, sizeof(count2));
  3449. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3450. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3451. if(min > count[i])
  3452. min = count[i]; // minimun count of collision
  3453. if(max < count[i])
  3454. max = count[i]; // maximun count of collision
  3455. if(count[i] == 0)
  3456. zero++;
  3457. ++count2[count[i]];
  3458. }
  3459. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3460. for( i=min; i <= max; ++i ){
  3461. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3462. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3463. }
  3464. for( i=min; i <= max; ++i ){
  3465. n += count2[i];
  3466. if( n*2 >= SCRIPT_HASH_SIZE )
  3467. {
  3468. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3469. median = (i+i+1)/2.0f;
  3470. else
  3471. median = i;
  3472. break;
  3473. }
  3474. }
  3475. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3476. fclose(fp);
  3477. }
  3478. }
  3479. #endif
  3480. mapreg_final();
  3481. db_destroy(scriptlabel_db);
  3482. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3483. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3484. if(sleep_db) {
  3485. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3486. while(n) {
  3487. struct script_state *st = (struct script_state *)n->data;
  3488. script_free_state(st);
  3489. n = n->next;
  3490. }
  3491. linkdb_final(&sleep_db);
  3492. }
  3493. if (str_data)
  3494. aFree(str_data);
  3495. if (str_buf)
  3496. aFree(str_buf);
  3497. return 0;
  3498. }
  3499. /*==========================================
  3500. * 初期化
  3501. *------------------------------------------*/
  3502. int do_init_script()
  3503. {
  3504. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3505. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3506. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3507. mapreg_init();
  3508. return 0;
  3509. }
  3510. int script_reload()
  3511. {
  3512. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3513. db_clear(scriptlabel_db);
  3514. if(sleep_db) {
  3515. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3516. while(n) {
  3517. struct script_state *st = (struct script_state *)n->data;
  3518. script_free_state(st);
  3519. n = n->next;
  3520. }
  3521. linkdb_final(&sleep_db);
  3522. }
  3523. mapreg_reload();
  3524. return 0;
  3525. }
  3526. //-----------------------------------------------------------------------------
  3527. // buildin functions
  3528. //
  3529. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3530. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3531. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3532. /////////////////////////////////////////////////////////////////////
  3533. // NPC interaction
  3534. //
  3535. /// Appends a message to the npc dialog.
  3536. /// If a dialog doesn't exist yet, one is created.
  3537. ///
  3538. /// mes "<message>";
  3539. BUILDIN_FUNC(mes)
  3540. {
  3541. TBL_PC* sd = script_rid2sd(st);
  3542. if( sd == NULL )
  3543. return 0;
  3544. if( !script_hasdata(st, 3) )
  3545. {// only a single line detected in the script
  3546. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3547. }
  3548. else
  3549. {// parse multiple lines as they exist
  3550. int i;
  3551. for( i = 2; script_hasdata(st, i); i++ )
  3552. {
  3553. // send the message to the client
  3554. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3555. }
  3556. }
  3557. return 0;
  3558. }
  3559. /// Displays the button 'next' in the npc dialog.
  3560. /// The dialog text is cleared and the script continues when the button is pressed.
  3561. ///
  3562. /// next;
  3563. BUILDIN_FUNC(next)
  3564. {
  3565. TBL_PC* sd;
  3566. sd = script_rid2sd(st);
  3567. if( sd == NULL )
  3568. return 0;
  3569. st->state = STOP;
  3570. clif_scriptnext(sd, st->oid);
  3571. return 0;
  3572. }
  3573. /// Ends the script and displays the button 'close' on the npc dialog.
  3574. /// The dialog is closed when the button is pressed.
  3575. ///
  3576. /// close;
  3577. BUILDIN_FUNC(close)
  3578. {
  3579. TBL_PC* sd;
  3580. sd = script_rid2sd(st);
  3581. if( sd == NULL )
  3582. return 0;
  3583. st->state = END;
  3584. clif_scriptclose(sd, st->oid);
  3585. return 0;
  3586. }
  3587. /// Displays the button 'close' on the npc dialog.
  3588. /// The dialog is closed and the script continues when the button is pressed.
  3589. ///
  3590. /// close2;
  3591. BUILDIN_FUNC(close2)
  3592. {
  3593. TBL_PC* sd;
  3594. sd = script_rid2sd(st);
  3595. if( sd == NULL )
  3596. return 0;
  3597. st->state = STOP;
  3598. clif_scriptclose(sd, st->oid);
  3599. return 0;
  3600. }
  3601. /// Counts the number of valid and total number of options in 'str'
  3602. /// If max_count > 0 the counting stops when that valid option is reached
  3603. /// total is incremented for each option (NULL is supported)
  3604. static int menu_countoptions(const char* str, int max_count, int* total)
  3605. {
  3606. int count = 0;
  3607. int bogus_total;
  3608. if( total == NULL )
  3609. total = &bogus_total;
  3610. ++(*total);
  3611. // initial empty options
  3612. while( *str == ':' )
  3613. {
  3614. ++str;
  3615. ++(*total);
  3616. }
  3617. // count menu options
  3618. while( *str != '\0' )
  3619. {
  3620. ++count;
  3621. --max_count;
  3622. if( max_count == 0 )
  3623. break;
  3624. while( *str != ':' && *str != '\0' )
  3625. ++str;
  3626. while( *str == ':' )
  3627. {
  3628. ++str;
  3629. ++(*total);
  3630. }
  3631. }
  3632. return count;
  3633. }
  3634. /// Displays a menu with options and goes to the target label.
  3635. /// The script is stopped if cancel is pressed.
  3636. /// Options with no text are not displayed in the client.
  3637. ///
  3638. /// Options can be grouped together, separated by the character ':' in the text:
  3639. /// ex: menu "A:B:C",L_target;
  3640. /// All these options go to the specified target label.
  3641. ///
  3642. /// The index of the selected option is put in the variable @menu.
  3643. /// Indexes start with 1 and are consistent with grouped and empty options.
  3644. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3645. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3646. ///
  3647. /// NOTE: the client closes the npc dialog when cancel is pressed
  3648. ///
  3649. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3650. BUILDIN_FUNC(menu)
  3651. {
  3652. int i;
  3653. const char* text;
  3654. TBL_PC* sd;
  3655. sd = script_rid2sd(st);
  3656. if( sd == NULL )
  3657. return 0;
  3658. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3659. if( sd->state.menu_or_input == 0 )
  3660. {
  3661. struct StringBuf buf;
  3662. struct script_data* data;
  3663. if( script_lastdata(st) % 2 == 0 )
  3664. {// argument count is not even (1st argument is at index 2)
  3665. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3666. st->state = END;
  3667. return 1;
  3668. }
  3669. StringBuf_Init(&buf);
  3670. sd->npc_menu = 0;
  3671. for( i = 2; i < script_lastdata(st); i += 2 )
  3672. {
  3673. // menu options
  3674. text = script_getstr(st, i);
  3675. // target label
  3676. data = script_getdata(st, i+1);
  3677. if( !data_islabel(data) )
  3678. {// not a label
  3679. StringBuf_Destroy(&buf);
  3680. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3681. script_reportdata(data);
  3682. st->state = END;
  3683. return 1;
  3684. }
  3685. // append option(s)
  3686. if( text[0] == '\0' )
  3687. continue;// empty string, ignore
  3688. if( sd->npc_menu > 0 )
  3689. StringBuf_AppendStr(&buf, ":");
  3690. StringBuf_AppendStr(&buf, text);
  3691. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3692. }
  3693. st->state = RERUNLINE;
  3694. sd->state.menu_or_input = 1;
  3695. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3696. StringBuf_Destroy(&buf);
  3697. if( sd->npc_menu >= 0xff )
  3698. {// 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
  3699. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3700. script_reportsrc(st);
  3701. }
  3702. }
  3703. else if( sd->npc_menu == 0xff )
  3704. {// Cancel was pressed
  3705. sd->state.menu_or_input = 0;
  3706. st->state = END;
  3707. }
  3708. else
  3709. {// goto target label
  3710. int menu = 0;
  3711. sd->state.menu_or_input = 0;
  3712. if( sd->npc_menu <= 0 )
  3713. {
  3714. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3715. st->state = END;
  3716. return 1;
  3717. }
  3718. // get target label
  3719. for( i = 2; i < script_lastdata(st); i += 2 )
  3720. {
  3721. text = script_getstr(st, i);
  3722. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3723. if( sd->npc_menu <= 0 )
  3724. break;// entry found
  3725. }
  3726. if( sd->npc_menu > 0 )
  3727. {// Invalid selection
  3728. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3729. st->state = END;
  3730. return 1;
  3731. }
  3732. if( !data_islabel(script_getdata(st, i + 1)) )
  3733. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3734. ShowError("script:menu: unexpected data in label argument\n");
  3735. script_reportdata(script_getdata(st, i + 1));
  3736. st->state = END;
  3737. return 1;
  3738. }
  3739. pc_setreg(sd, add_str("@menu"), menu);
  3740. st->pos = script_getnum(st, i + 1);
  3741. st->state = GOTO;
  3742. }
  3743. return 0;
  3744. }
  3745. /// Displays a menu with options and returns the selected option.
  3746. /// Behaves like 'menu' without the target labels.
  3747. ///
  3748. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3749. ///
  3750. /// @see menu
  3751. BUILDIN_FUNC(select)
  3752. {
  3753. int i;
  3754. const char* text;
  3755. TBL_PC* sd;
  3756. sd = script_rid2sd(st);
  3757. if( sd == NULL )
  3758. return 0;
  3759. if( sd->state.menu_or_input == 0 )
  3760. {
  3761. struct StringBuf buf;
  3762. StringBuf_Init(&buf);
  3763. sd->npc_menu = 0;
  3764. for( i = 2; i <= script_lastdata(st); ++i )
  3765. {
  3766. text = script_getstr(st, i);
  3767. if( sd->npc_menu > 0 )
  3768. StringBuf_AppendStr(&buf, ":");
  3769. StringBuf_AppendStr(&buf, text);
  3770. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3771. }
  3772. st->state = RERUNLINE;
  3773. sd->state.menu_or_input = 1;
  3774. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3775. StringBuf_Destroy(&buf);
  3776. if( sd->npc_menu >= 0xff )
  3777. {
  3778. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3779. script_reportsrc(st);
  3780. }
  3781. }
  3782. else if( sd->npc_menu == 0xff )
  3783. {// Cancel was pressed
  3784. sd->state.menu_or_input = 0;
  3785. st->state = END;
  3786. }
  3787. else
  3788. {// return selected option
  3789. int menu = 0;
  3790. sd->state.menu_or_input = 0;
  3791. for( i = 2; i <= script_lastdata(st); ++i )
  3792. {
  3793. text = script_getstr(st, i);
  3794. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3795. if( sd->npc_menu <= 0 )
  3796. break;// entry found
  3797. }
  3798. pc_setreg(sd, add_str("@menu"), menu);
  3799. script_pushint(st, menu);
  3800. st->state = RUN;
  3801. }
  3802. return 0;
  3803. }
  3804. /// Displays a menu with options and returns the selected option.
  3805. /// Behaves like 'menu' without the target labels, except when cancel is
  3806. /// pressed.
  3807. /// When cancel is pressed, the script continues and 255 is returned.
  3808. ///
  3809. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3810. ///
  3811. /// @see menu
  3812. BUILDIN_FUNC(prompt)
  3813. {
  3814. int i;
  3815. const char *text;
  3816. TBL_PC* sd;
  3817. sd = script_rid2sd(st);
  3818. if( sd == NULL )
  3819. return 0;
  3820. if( sd->state.menu_or_input == 0 )
  3821. {
  3822. struct StringBuf buf;
  3823. StringBuf_Init(&buf);
  3824. sd->npc_menu = 0;
  3825. for( i = 2; i <= script_lastdata(st); ++i )
  3826. {
  3827. text = script_getstr(st, i);
  3828. if( sd->npc_menu > 0 )
  3829. StringBuf_AppendStr(&buf, ":");
  3830. StringBuf_AppendStr(&buf, text);
  3831. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3832. }
  3833. st->state = RERUNLINE;
  3834. sd->state.menu_or_input = 1;
  3835. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3836. StringBuf_Destroy(&buf);
  3837. if( sd->npc_menu >= 0xff )
  3838. {
  3839. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3840. script_reportsrc(st);
  3841. }
  3842. }
  3843. else if( sd->npc_menu == 0xff )
  3844. {// Cancel was pressed
  3845. sd->state.menu_or_input = 0;
  3846. pc_setreg(sd, add_str("@menu"), 0xff);
  3847. script_pushint(st, 0xff);
  3848. st->state = RUN;
  3849. }
  3850. else
  3851. {// return selected option
  3852. int menu = 0;
  3853. sd->state.menu_or_input = 0;
  3854. for( i = 2; i <= script_lastdata(st); ++i )
  3855. {
  3856. text = script_getstr(st, i);
  3857. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3858. if( sd->npc_menu <= 0 )
  3859. break;// entry found
  3860. }
  3861. pc_setreg(sd, add_str("@menu"), menu);
  3862. script_pushint(st, menu);
  3863. st->state = RUN;
  3864. }
  3865. return 0;
  3866. }
  3867. /////////////////////////////////////////////////////////////////////
  3868. // ...
  3869. //
  3870. /// Jumps to the target script label.
  3871. ///
  3872. /// goto <label>;
  3873. BUILDIN_FUNC(goto)
  3874. {
  3875. if( !data_islabel(script_getdata(st,2)) )
  3876. {
  3877. ShowError("script:goto: not a label\n");
  3878. script_reportdata(script_getdata(st,2));
  3879. st->state = END;
  3880. return 1;
  3881. }
  3882. st->pos = script_getnum(st,2);
  3883. st->state = GOTO;
  3884. return 0;
  3885. }
  3886. /*==========================================
  3887. * user-defined function call
  3888. *------------------------------------------*/
  3889. BUILDIN_FUNC(callfunc)
  3890. {
  3891. int i, j;
  3892. struct script_retinfo* ri;
  3893. struct script_code* scr;
  3894. const char* str = script_getstr(st,2);
  3895. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3896. if( !scr )
  3897. {
  3898. ShowError("script:callfunc: function not found! [%s]\n", str);
  3899. st->state = END;
  3900. return 1;
  3901. }
  3902. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3903. {
  3904. struct script_data* data = push_copy(st->stack,i);
  3905. if( data_isreference(data) && !data->ref )
  3906. {
  3907. const char* name = reference_getname(data);
  3908. if( name[0] == '.' && name[1] == '@' )
  3909. data->ref = st->stack->var_function;
  3910. else if( name[0] == '.' )
  3911. data->ref = &st->script->script_vars;
  3912. }
  3913. }
  3914. CREATE(ri, struct script_retinfo, 1);
  3915. ri->script = st->script;// script code
  3916. ri->var_function = st->stack->var_function;// scope variables
  3917. ri->pos = st->pos;// script location
  3918. ri->nargs = j;// argument count
  3919. ri->defsp = st->stack->defsp;// default stack pointer
  3920. push_retinfo(st->stack, ri);
  3921. st->pos = 0;
  3922. st->script = scr;
  3923. st->stack->defsp = st->stack->sp;
  3924. st->state = GOTO;
  3925. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3926. return 0;
  3927. }
  3928. /*==========================================
  3929. * subroutine call
  3930. *------------------------------------------*/
  3931. BUILDIN_FUNC(callsub)
  3932. {
  3933. int i,j;
  3934. struct script_retinfo* ri;
  3935. int pos = script_getnum(st,2);
  3936. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3937. {
  3938. ShowError("script:callsub: argument is not a label\n");
  3939. script_reportdata(script_getdata(st,2));
  3940. st->state = END;
  3941. return 1;
  3942. }
  3943. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3944. {
  3945. struct script_data* data = push_copy(st->stack,i);
  3946. if( data_isreference(data) && !data->ref )
  3947. {
  3948. const char* name = reference_getname(data);
  3949. if( name[0] == '.' && name[1] == '@' )
  3950. data->ref = st->stack->var_function;
  3951. }
  3952. }
  3953. CREATE(ri, struct script_retinfo, 1);
  3954. ri->script = st->script;// script code
  3955. ri->var_function = st->stack->var_function;// scope variables
  3956. ri->pos = st->pos;// script location
  3957. ri->nargs = j;// argument count
  3958. ri->defsp = st->stack->defsp;// default stack pointer
  3959. push_retinfo(st->stack, ri);
  3960. st->pos = pos;
  3961. st->stack->defsp = st->stack->sp;
  3962. st->state = GOTO;
  3963. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3964. return 0;
  3965. }
  3966. /// Retrieves an argument provided to callfunc/callsub.
  3967. /// If the argument doesn't exist
  3968. ///
  3969. /// getarg(<index>{,<default_value>}) -> <value>
  3970. BUILDIN_FUNC(getarg)
  3971. {
  3972. struct script_retinfo* ri;
  3973. int idx;
  3974. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3975. {
  3976. ShowError("script:getarg: no callfunc or callsub!\n");
  3977. st->state = END;
  3978. return 1;
  3979. }
  3980. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3981. idx = script_getnum(st,2);
  3982. if( idx >= 0 && idx < ri->nargs )
  3983. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3984. else if( script_hasdata(st,3) )
  3985. script_pushcopy(st, 3);
  3986. else
  3987. {
  3988. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3989. st->state = END;
  3990. return 1;
  3991. }
  3992. return 0;
  3993. }
  3994. /// Returns from the current function, optionaly returning a value from the functions.
  3995. /// Don't use outside script functions.
  3996. ///
  3997. /// return;
  3998. /// return <value>;
  3999. BUILDIN_FUNC(return)
  4000. {
  4001. if( script_hasdata(st,2) )
  4002. {// return value
  4003. struct script_data* data;
  4004. script_pushcopy(st, 2);
  4005. data = script_getdatatop(st, -1);
  4006. if( data_isreference(data) )
  4007. {
  4008. const char* name = reference_getname(data);
  4009. if( name[0] == '.' && name[1] == '@' )
  4010. {// scope variable
  4011. if( !data->ref || data->ref == st->stack->var_function )
  4012. get_val(st, data);// current scope, convert to value
  4013. }
  4014. else if( name[0] == '.' && !data->ref )
  4015. {// script variable, link to current script
  4016. data->ref = &st->script->script_vars;
  4017. }
  4018. }
  4019. }
  4020. else
  4021. {// no return value
  4022. script_pushnil(st);
  4023. }
  4024. st->state = RETFUNC;
  4025. return 0;
  4026. }
  4027. /// Returns a random number from 0 to <range>-1.
  4028. /// Or returns a random number from <min> to <max>.
  4029. /// If <min> is greater than <max>, their numbers are switched.
  4030. /// rand(<range>) -> <int>
  4031. /// rand(<min>,<max>) -> <int>
  4032. BUILDIN_FUNC(rand)
  4033. {
  4034. int range;
  4035. int min;
  4036. int max;
  4037. if( script_hasdata(st,3) )
  4038. {// min,max
  4039. min = script_getnum(st,2);
  4040. max = script_getnum(st,3);
  4041. if( max < min )
  4042. swap(min, max);
  4043. range = max - min + 1;
  4044. }
  4045. else
  4046. {// range
  4047. min = 0;
  4048. range = script_getnum(st,2);
  4049. }
  4050. if( range <= 1 )
  4051. script_pushint(st, min);
  4052. else
  4053. script_pushint(st, rnd()%range + min);
  4054. return 0;
  4055. }
  4056. /*==========================================
  4057. *
  4058. *------------------------------------------*/
  4059. BUILDIN_FUNC(warp)
  4060. {
  4061. int ret;
  4062. int x,y;
  4063. const char* str;
  4064. TBL_PC* sd;
  4065. sd = script_rid2sd(st);
  4066. if( sd == NULL )
  4067. return 0;
  4068. str = script_getstr(st,2);
  4069. x = script_getnum(st,3);
  4070. y = script_getnum(st,4);
  4071. if(strcmp(str,"Random")==0)
  4072. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4073. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4074. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4075. else
  4076. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4077. if( ret ) {
  4078. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4079. script_reportsrc(st);
  4080. }
  4081. return 0;
  4082. }
  4083. /*==========================================
  4084. * エリア指定ワープ
  4085. *------------------------------------------*/
  4086. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4087. {
  4088. int x,y;
  4089. unsigned int map;
  4090. map=va_arg(ap, unsigned int);
  4091. x=va_arg(ap,int);
  4092. y=va_arg(ap,int);
  4093. if(map == 0)
  4094. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4095. else
  4096. pc_setpos((TBL_PC *)bl,map,x,y,CLR_OUTSIGHT);
  4097. return 0;
  4098. }
  4099. BUILDIN_FUNC(areawarp)
  4100. {
  4101. int x,y,m;
  4102. unsigned int index;
  4103. const char *str;
  4104. const char *mapname;
  4105. int x0,y0,x1,y1;
  4106. mapname=script_getstr(st,2);
  4107. x0=script_getnum(st,3);
  4108. y0=script_getnum(st,4);
  4109. x1=script_getnum(st,5);
  4110. y1=script_getnum(st,6);
  4111. str=script_getstr(st,7);
  4112. x=script_getnum(st,8);
  4113. y=script_getnum(st,9);
  4114. if( (m=map_mapname2mapid(mapname))< 0)
  4115. return 0;
  4116. if(strcmp(str,"Random")==0)
  4117. index = 0;
  4118. else if(!(index=mapindex_name2id(str)))
  4119. return 0;
  4120. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  4121. return 0;
  4122. }
  4123. /*==========================================
  4124. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4125. *------------------------------------------*/
  4126. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4127. {
  4128. int hp, sp;
  4129. hp = va_arg(ap, int);
  4130. sp = va_arg(ap, int);
  4131. pc_percentheal((TBL_PC *)bl,hp,sp);
  4132. return 0;
  4133. }
  4134. BUILDIN_FUNC(areapercentheal)
  4135. {
  4136. int hp,sp,m;
  4137. const char *mapname;
  4138. int x0,y0,x1,y1;
  4139. mapname=script_getstr(st,2);
  4140. x0=script_getnum(st,3);
  4141. y0=script_getnum(st,4);
  4142. x1=script_getnum(st,5);
  4143. y1=script_getnum(st,6);
  4144. hp=script_getnum(st,7);
  4145. sp=script_getnum(st,8);
  4146. if( (m=map_mapname2mapid(mapname))< 0)
  4147. return 0;
  4148. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4149. return 0;
  4150. }
  4151. /*==========================================
  4152. * warpchar [LuzZza]
  4153. * Useful for warp one player from
  4154. * another player npc-session.
  4155. * Using: warpchar "mapname",x,y,Char_ID;
  4156. *------------------------------------------*/
  4157. BUILDIN_FUNC(warpchar)
  4158. {
  4159. int x,y,a;
  4160. const char *str;
  4161. TBL_PC *sd;
  4162. str=script_getstr(st,2);
  4163. x=script_getnum(st,3);
  4164. y=script_getnum(st,4);
  4165. a=script_getnum(st,5);
  4166. sd = map_charid2sd(a);
  4167. if( sd == NULL )
  4168. return 0;
  4169. if(strcmp(str, "Random") == 0)
  4170. pc_randomwarp(sd, CLR_TELEPORT);
  4171. else
  4172. if(strcmp(str, "SavePoint") == 0)
  4173. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4174. else
  4175. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4176. return 0;
  4177. }
  4178. /*==========================================
  4179. * Warpparty - [Fredzilla] [Paradox924X]
  4180. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4181. * If 'from_mapname' is specified, only the party members on that map will be warped
  4182. *------------------------------------------*/
  4183. BUILDIN_FUNC(warpparty)
  4184. {
  4185. TBL_PC *sd = NULL;
  4186. TBL_PC *pl_sd;
  4187. struct party_data* p;
  4188. int type;
  4189. int mapindex;
  4190. int i;
  4191. const char* str = script_getstr(st,2);
  4192. int x = script_getnum(st,3);
  4193. int y = script_getnum(st,4);
  4194. int p_id = script_getnum(st,5);
  4195. const char* str2 = NULL;
  4196. if ( script_hasdata(st,6) )
  4197. str2 = script_getstr(st,6);
  4198. p = party_search(p_id);
  4199. if(!p)
  4200. return 0;
  4201. type = ( strcmp(str,"Random")==0 ) ? 0
  4202. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4203. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4204. : ( strcmp(str,"Leader")==0 ) ? 3
  4205. : 4;
  4206. switch (type)
  4207. {
  4208. case 3:
  4209. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4210. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4211. return 0;
  4212. pl_sd = p->data[i].sd;
  4213. mapindex = pl_sd->mapindex;
  4214. x = pl_sd->bl.x;
  4215. y = pl_sd->bl.y;
  4216. break;
  4217. case 4:
  4218. mapindex = mapindex_name2id(str);
  4219. break;
  4220. case 2:
  4221. //"SavePoint" uses save point of the currently attached player
  4222. if (( sd = script_rid2sd(st) ) == NULL )
  4223. return 0;
  4224. default:
  4225. mapindex = 0;
  4226. break;
  4227. }
  4228. for (i = 0; i < MAX_PARTY; i++)
  4229. {
  4230. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4231. continue;
  4232. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4233. continue;
  4234. if( pc_isdead(pl_sd) )
  4235. continue;
  4236. switch( type )
  4237. {
  4238. case 0: // Random
  4239. if(!map[pl_sd->bl.m].flag.nowarp)
  4240. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4241. break;
  4242. case 1: // SavePointAll
  4243. if(!map[pl_sd->bl.m].flag.noreturn)
  4244. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4245. break;
  4246. case 2: // SavePoint
  4247. if(!map[pl_sd->bl.m].flag.noreturn)
  4248. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4249. break;
  4250. case 3: // Leader
  4251. case 4: // m,x,y
  4252. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4253. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4254. break;
  4255. }
  4256. }
  4257. return 0;
  4258. }
  4259. /*==========================================
  4260. * Warpguild - [Fredzilla]
  4261. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4262. *------------------------------------------*/
  4263. BUILDIN_FUNC(warpguild)
  4264. {
  4265. TBL_PC *sd = NULL;
  4266. TBL_PC *pl_sd;
  4267. struct guild* g;
  4268. struct s_mapiterator* iter;
  4269. int type;
  4270. const char* str = script_getstr(st,2);
  4271. int x = script_getnum(st,3);
  4272. int y = script_getnum(st,4);
  4273. int gid = script_getnum(st,5);
  4274. g = guild_search(gid);
  4275. if( g == NULL )
  4276. return 0;
  4277. type = ( strcmp(str,"Random")==0 ) ? 0
  4278. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4279. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4280. : 3;
  4281. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4282. {// "SavePoint" uses save point of the currently attached player
  4283. return 0;
  4284. }
  4285. iter = mapit_getallusers();
  4286. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4287. {
  4288. if( pl_sd->status.guild_id != gid )
  4289. continue;
  4290. switch( type )
  4291. {
  4292. case 0: // Random
  4293. if(!map[pl_sd->bl.m].flag.nowarp)
  4294. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4295. break;
  4296. case 1: // SavePointAll
  4297. if(!map[pl_sd->bl.m].flag.noreturn)
  4298. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4299. break;
  4300. case 2: // SavePoint
  4301. if(!map[pl_sd->bl.m].flag.noreturn)
  4302. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4303. break;
  4304. case 3: // m,x,y
  4305. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4306. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4307. break;
  4308. }
  4309. }
  4310. mapit_free(iter);
  4311. return 0;
  4312. }
  4313. /*==========================================
  4314. *
  4315. *------------------------------------------*/
  4316. BUILDIN_FUNC(heal)
  4317. {
  4318. TBL_PC *sd;
  4319. int hp,sp;
  4320. sd = script_rid2sd(st);
  4321. if (!sd) return 0;
  4322. hp=script_getnum(st,2);
  4323. sp=script_getnum(st,3);
  4324. status_heal(&sd->bl, hp, sp, 1);
  4325. return 0;
  4326. }
  4327. /*==========================================
  4328. *
  4329. *------------------------------------------*/
  4330. BUILDIN_FUNC(itemheal)
  4331. {
  4332. TBL_PC *sd;
  4333. int hp,sp;
  4334. hp=script_getnum(st,2);
  4335. sp=script_getnum(st,3);
  4336. if(potion_flag==1) {
  4337. potion_hp = hp;
  4338. potion_sp = sp;
  4339. return 0;
  4340. }
  4341. sd = script_rid2sd(st);
  4342. if (!sd) return 0;
  4343. pc_itemheal(sd,sd->itemid,hp,sp);
  4344. return 0;
  4345. }
  4346. /*==========================================
  4347. *
  4348. *------------------------------------------*/
  4349. BUILDIN_FUNC(percentheal)
  4350. {
  4351. int hp,sp;
  4352. TBL_PC* sd;
  4353. hp=script_getnum(st,2);
  4354. sp=script_getnum(st,3);
  4355. if(potion_flag==1) {
  4356. potion_per_hp = hp;
  4357. potion_per_sp = sp;
  4358. return 0;
  4359. }
  4360. sd = script_rid2sd(st);
  4361. if( sd == NULL )
  4362. return 0;
  4363. pc_percentheal(sd,hp,sp);
  4364. return 0;
  4365. }
  4366. /*==========================================
  4367. *
  4368. *------------------------------------------*/
  4369. BUILDIN_FUNC(jobchange)
  4370. {
  4371. int job, upper=-1;
  4372. job=script_getnum(st,2);
  4373. if( script_hasdata(st,3) )
  4374. upper=script_getnum(st,3);
  4375. if (pcdb_checkid(job))
  4376. {
  4377. TBL_PC* sd;
  4378. sd = script_rid2sd(st);
  4379. if( sd == NULL )
  4380. return 0;
  4381. pc_jobchange(sd, job, upper);
  4382. }
  4383. return 0;
  4384. }
  4385. /*==========================================
  4386. *
  4387. *------------------------------------------*/
  4388. BUILDIN_FUNC(jobname)
  4389. {
  4390. int class_=script_getnum(st,2);
  4391. script_pushconststr(st, (char*)job_name(class_));
  4392. return 0;
  4393. }
  4394. /// Get input from the player.
  4395. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4396. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4397. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4398. /// shorter than 'min' and 0 otherwise.
  4399. ///
  4400. /// input(<var>{,<min>{,<max>}}) -> <int>
  4401. BUILDIN_FUNC(input)
  4402. {
  4403. TBL_PC* sd;
  4404. struct script_data* data;
  4405. int uid;
  4406. const char* name;
  4407. int min;
  4408. int max;
  4409. sd = script_rid2sd(st);
  4410. if( sd == NULL )
  4411. return 0;
  4412. data = script_getdata(st,2);
  4413. if( !data_isreference(data) ){
  4414. ShowError("script:input: not a variable\n");
  4415. script_reportdata(data);
  4416. st->state = END;
  4417. return 1;
  4418. }
  4419. uid = reference_getuid(data);
  4420. name = reference_getname(data);
  4421. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4422. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4423. if( !sd->state.menu_or_input )
  4424. { // first invocation, display npc input box
  4425. sd->state.menu_or_input = 1;
  4426. st->state = RERUNLINE;
  4427. if( is_string_variable(name) )
  4428. clif_scriptinputstr(sd,st->oid);
  4429. else
  4430. clif_scriptinput(sd,st->oid);
  4431. }
  4432. else
  4433. { // take received text/value and store it in the designated variable
  4434. sd->state.menu_or_input = 0;
  4435. if( is_string_variable(name) )
  4436. {
  4437. int len = (int)strlen(sd->npc_str);
  4438. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4439. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4440. }
  4441. else
  4442. {
  4443. int amount = sd->npc_amount;
  4444. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4445. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4446. }
  4447. st->state = RUN;
  4448. }
  4449. return 0;
  4450. }
  4451. /// Sets the value of a variable.
  4452. /// The value is converted to the type of the variable.
  4453. ///
  4454. /// set(<variable>,<value>) -> <variable>
  4455. BUILDIN_FUNC(set)
  4456. {
  4457. TBL_PC* sd = NULL;
  4458. struct script_data* data;
  4459. int num;
  4460. const char* name;
  4461. char prefix;
  4462. data = script_getdata(st,2);
  4463. if( !data_isreference(data) )
  4464. {
  4465. ShowError("script:set: not a variable\n");
  4466. script_reportdata(script_getdata(st,2));
  4467. st->state = END;
  4468. return 1;
  4469. }
  4470. num = reference_getuid(data);
  4471. name = reference_getname(data);
  4472. prefix = *name;
  4473. if( not_server_variable(prefix) )
  4474. {
  4475. sd = script_rid2sd(st);
  4476. if( sd == NULL )
  4477. {
  4478. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4479. return 0;
  4480. }
  4481. }
  4482. if( is_string_variable(name) )
  4483. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4484. else
  4485. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4486. // return a copy of the variable reference
  4487. script_pushcopy(st,2);
  4488. return 0;
  4489. }
  4490. /////////////////////////////////////////////////////////////////////
  4491. /// Array variables
  4492. ///
  4493. /// Returns the size of the specified array
  4494. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4495. {
  4496. int32 ret = idx;
  4497. if( isstring )
  4498. {
  4499. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4500. {
  4501. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4502. if( str && *str )
  4503. ret = idx + 1;
  4504. script_removetop(st, -1, 0);
  4505. }
  4506. }
  4507. else
  4508. {
  4509. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4510. {
  4511. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  4512. if( num )
  4513. ret = idx + 1;
  4514. script_removetop(st, -1, 0);
  4515. }
  4516. }
  4517. return ret;
  4518. }
  4519. /// Sets values of an array, from the starting index.
  4520. /// ex: setarray arr[1],1,2,3;
  4521. ///
  4522. /// setarray <array variable>,<value1>{,<value2>...};
  4523. BUILDIN_FUNC(setarray)
  4524. {
  4525. struct script_data* data;
  4526. const char* name;
  4527. int32 start;
  4528. int32 end;
  4529. int32 id;
  4530. int32 i;
  4531. TBL_PC* sd = NULL;
  4532. data = script_getdata(st, 2);
  4533. if( !data_isreference(data) )
  4534. {
  4535. ShowError("script:setarray: not a variable\n");
  4536. script_reportdata(data);
  4537. st->state = END;
  4538. return 1;// not a variable
  4539. }
  4540. id = reference_getid(data);
  4541. start = reference_getindex(data);
  4542. name = reference_getname(data);
  4543. if( not_array_variable(*name) )
  4544. {
  4545. ShowError("script:setarray: illegal scope\n");
  4546. script_reportdata(data);
  4547. st->state = END;
  4548. return 1;// not supported
  4549. }
  4550. if( not_server_variable(*name) )
  4551. {
  4552. sd = script_rid2sd(st);
  4553. if( sd == NULL )
  4554. return 0;// no player attached
  4555. }
  4556. end = start + script_lastdata(st) - 2;
  4557. if( end > SCRIPT_MAX_ARRAYSIZE )
  4558. end = SCRIPT_MAX_ARRAYSIZE;
  4559. if( is_string_variable(name) )
  4560. {// string array
  4561. for( i = 3; start < end; ++start, ++i )
  4562. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4563. }
  4564. else
  4565. {// int array
  4566. for( i = 3; start < end; ++start, ++i )
  4567. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  4568. }
  4569. return 0;
  4570. }
  4571. /// Sets count values of an array, from the starting index.
  4572. /// ex: cleararray arr[0],0,1;
  4573. ///
  4574. /// cleararray <array variable>,<value>,<count>;
  4575. BUILDIN_FUNC(cleararray)
  4576. {
  4577. struct script_data* data;
  4578. const char* name;
  4579. int32 start;
  4580. int32 end;
  4581. int32 id;
  4582. void* v;
  4583. TBL_PC* sd = NULL;
  4584. data = script_getdata(st, 2);
  4585. if( !data_isreference(data) )
  4586. {
  4587. ShowError("script:cleararray: not a variable\n");
  4588. script_reportdata(data);
  4589. st->state = END;
  4590. return 1;// not a variable
  4591. }
  4592. id = reference_getid(data);
  4593. start = reference_getindex(data);
  4594. name = reference_getname(data);
  4595. if( not_array_variable(*name) )
  4596. {
  4597. ShowError("script:cleararray: illegal scope\n");
  4598. script_reportdata(data);
  4599. st->state = END;
  4600. return 1;// not supported
  4601. }
  4602. if( not_server_variable(*name) )
  4603. {
  4604. sd = script_rid2sd(st);
  4605. if( sd == NULL )
  4606. return 0;// no player attached
  4607. }
  4608. if( is_string_variable(name) )
  4609. v = (void*)script_getstr(st, 3);
  4610. else
  4611. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  4612. end = start + script_getnum(st, 4);
  4613. if( end > SCRIPT_MAX_ARRAYSIZE )
  4614. end = SCRIPT_MAX_ARRAYSIZE;
  4615. for( ; start < end; ++start )
  4616. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4617. return 0;
  4618. }
  4619. /// Copies data from one array to another.
  4620. /// ex: copyarray arr[0],arr[2],2;
  4621. ///
  4622. /// copyarray <destination array variable>,<source array variable>,<count>;
  4623. BUILDIN_FUNC(copyarray)
  4624. {
  4625. struct script_data* data1;
  4626. struct script_data* data2;
  4627. const char* name1;
  4628. const char* name2;
  4629. int32 idx1;
  4630. int32 idx2;
  4631. int32 id1;
  4632. int32 id2;
  4633. void* v;
  4634. int32 i;
  4635. int32 count;
  4636. TBL_PC* sd = NULL;
  4637. data1 = script_getdata(st, 2);
  4638. data2 = script_getdata(st, 3);
  4639. if( !data_isreference(data1) || !data_isreference(data2) )
  4640. {
  4641. ShowError("script:copyarray: not a variable\n");
  4642. script_reportdata(data1);
  4643. script_reportdata(data2);
  4644. st->state = END;
  4645. return 1;// not a variable
  4646. }
  4647. id1 = reference_getid(data1);
  4648. id2 = reference_getid(data2);
  4649. idx1 = reference_getindex(data1);
  4650. idx2 = reference_getindex(data2);
  4651. name1 = reference_getname(data1);
  4652. name2 = reference_getname(data2);
  4653. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4654. {
  4655. ShowError("script:copyarray: illegal scope\n");
  4656. script_reportdata(data1);
  4657. script_reportdata(data2);
  4658. st->state = END;
  4659. return 1;// not supported
  4660. }
  4661. if( is_string_variable(name1) != is_string_variable(name2) )
  4662. {
  4663. ShowError("script:copyarray: type mismatch\n");
  4664. script_reportdata(data1);
  4665. script_reportdata(data2);
  4666. st->state = END;
  4667. return 1;// data type mismatch
  4668. }
  4669. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4670. {
  4671. sd = script_rid2sd(st);
  4672. if( sd == NULL )
  4673. return 0;// no player attached
  4674. }
  4675. count = script_getnum(st, 4);
  4676. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  4677. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  4678. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4679. return 0;// nothing to copy
  4680. if( id1 == id2 && idx1 > idx2 )
  4681. {// destination might be overlapping the source - copy in reverse order
  4682. for( i = count - 1; i >= 0; --i )
  4683. {
  4684. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4685. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4686. script_removetop(st, -1, 0);
  4687. }
  4688. }
  4689. else
  4690. {// normal copy
  4691. for( i = 0; i < count; ++i )
  4692. {
  4693. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  4694. {
  4695. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4696. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4697. script_removetop(st, -1, 0);
  4698. }
  4699. else// out of range - assume ""/0
  4700. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4701. }
  4702. }
  4703. return 0;
  4704. }
  4705. /// Returns the size of the array.
  4706. /// Assumes that everything before the starting index exists.
  4707. /// ex: getarraysize(arr[3])
  4708. ///
  4709. /// getarraysize(<array variable>) -> <int>
  4710. BUILDIN_FUNC(getarraysize)
  4711. {
  4712. struct script_data* data;
  4713. const char* name;
  4714. data = script_getdata(st, 2);
  4715. if( !data_isreference(data) )
  4716. {
  4717. ShowError("script:getarraysize: not a variable\n");
  4718. script_reportdata(data);
  4719. script_pushnil(st);
  4720. st->state = END;
  4721. return 1;// not a variable
  4722. }
  4723. name = reference_getname(data);
  4724. if( not_array_variable(*name) )
  4725. {
  4726. ShowError("script:getarraysize: illegal scope\n");
  4727. script_reportdata(data);
  4728. script_pushnil(st);
  4729. st->state = END;
  4730. return 1;// not supported
  4731. }
  4732. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4733. return 0;
  4734. }
  4735. /// Deletes count or all the elements in an array, from the starting index.
  4736. /// ex: deletearray arr[4],2;
  4737. ///
  4738. /// deletearray <array variable>;
  4739. /// deletearray <array variable>,<count>;
  4740. BUILDIN_FUNC(deletearray)
  4741. {
  4742. struct script_data* data;
  4743. const char* name;
  4744. int start;
  4745. int end;
  4746. int id;
  4747. TBL_PC *sd = NULL;
  4748. data = script_getdata(st, 2);
  4749. if( !data_isreference(data) )
  4750. {
  4751. ShowError("script:deletearray: not a variable\n");
  4752. script_reportdata(data);
  4753. st->state = END;
  4754. return 1;// not a variable
  4755. }
  4756. id = reference_getid(data);
  4757. start = reference_getindex(data);
  4758. name = reference_getname(data);
  4759. if( not_array_variable(*name) )
  4760. {
  4761. ShowError("script:deletearray: illegal scope\n");
  4762. script_reportdata(data);
  4763. st->state = END;
  4764. return 1;// not supported
  4765. }
  4766. if( not_server_variable(*name) )
  4767. {
  4768. sd = script_rid2sd(st);
  4769. if( sd == NULL )
  4770. return 0;// no player attached
  4771. }
  4772. end = SCRIPT_MAX_ARRAYSIZE;
  4773. if( start >= end )
  4774. return 0;// nothing to free
  4775. if( script_hasdata(st,3) )
  4776. {
  4777. int count = script_getnum(st, 3);
  4778. if( count > end - start )
  4779. count = end - start;
  4780. if( count <= 0 )
  4781. return 0;// nothing to free
  4782. // move rest of the elements backward
  4783. for( ; start + count < end; ++start )
  4784. {
  4785. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4786. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4787. script_removetop(st, -1, 0);
  4788. }
  4789. }
  4790. // clear the rest of the array
  4791. if( is_string_variable(name) )
  4792. {
  4793. for( ; start < end; ++start )
  4794. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4795. }
  4796. else
  4797. {
  4798. for( ; start < end; ++start )
  4799. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4800. }
  4801. return 0;
  4802. }
  4803. /// Returns a reference to the target index of the array variable.
  4804. /// Equivalent to var[index].
  4805. ///
  4806. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4807. BUILDIN_FUNC(getelementofarray)
  4808. {
  4809. struct script_data* data;
  4810. const char* name;
  4811. int32 id;
  4812. int i;
  4813. data = script_getdata(st, 2);
  4814. if( !data_isreference(data) )
  4815. {
  4816. ShowError("script:getelementofarray: not a variable\n");
  4817. script_reportdata(data);
  4818. script_pushnil(st);
  4819. st->state = END;
  4820. return 1;// not a variable
  4821. }
  4822. id = reference_getid(data);
  4823. name = reference_getname(data);
  4824. if( not_array_variable(*name) )
  4825. {
  4826. ShowError("script:getelementofarray: illegal scope\n");
  4827. script_reportdata(data);
  4828. script_pushnil(st);
  4829. st->state = END;
  4830. return 1;// not supported
  4831. }
  4832. i = script_getnum(st, 3);
  4833. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  4834. {
  4835. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4836. script_reportdata(data);
  4837. script_pushnil(st);
  4838. st->state = END;
  4839. return 1;// out of range
  4840. }
  4841. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4842. return 0;
  4843. }
  4844. /////////////////////////////////////////////////////////////////////
  4845. /// ...
  4846. ///
  4847. /*==========================================
  4848. *
  4849. *------------------------------------------*/
  4850. BUILDIN_FUNC(setlook)
  4851. {
  4852. int type,val;
  4853. TBL_PC* sd;
  4854. type=script_getnum(st,2);
  4855. val=script_getnum(st,3);
  4856. sd = script_rid2sd(st);
  4857. if( sd == NULL )
  4858. return 0;
  4859. pc_changelook(sd,type,val);
  4860. return 0;
  4861. }
  4862. BUILDIN_FUNC(changelook)
  4863. { // As setlook but only client side
  4864. int type,val;
  4865. TBL_PC* sd;
  4866. type=script_getnum(st,2);
  4867. val=script_getnum(st,3);
  4868. sd = script_rid2sd(st);
  4869. if( sd == NULL )
  4870. return 0;
  4871. clif_changelook(&sd->bl,type,val);
  4872. return 0;
  4873. }
  4874. /*==========================================
  4875. *
  4876. *------------------------------------------*/
  4877. BUILDIN_FUNC(cutin)
  4878. {
  4879. TBL_PC* sd;
  4880. sd = script_rid2sd(st);
  4881. if( sd == NULL )
  4882. return 0;
  4883. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4884. return 0;
  4885. }
  4886. /*==========================================
  4887. *
  4888. *------------------------------------------*/
  4889. BUILDIN_FUNC(viewpoint)
  4890. {
  4891. int type,x,y,id,color;
  4892. TBL_PC* sd;
  4893. type=script_getnum(st,2);
  4894. x=script_getnum(st,3);
  4895. y=script_getnum(st,4);
  4896. id=script_getnum(st,5);
  4897. color=script_getnum(st,6);
  4898. sd = script_rid2sd(st);
  4899. if( sd == NULL )
  4900. return 0;
  4901. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4902. return 0;
  4903. }
  4904. /*==========================================
  4905. *
  4906. *------------------------------------------*/
  4907. BUILDIN_FUNC(countitem)
  4908. {
  4909. int nameid, i;
  4910. int count = 0;
  4911. struct item_data* id = NULL;
  4912. struct script_data* data;
  4913. TBL_PC* sd = script_rid2sd(st);
  4914. if (!sd) {
  4915. script_pushint(st,0);
  4916. return 0;
  4917. }
  4918. data = script_getdata(st,2);
  4919. get_val(st, data); // convert into value in case of a variable
  4920. if( data_isstring(data) )
  4921. {// item name
  4922. id = itemdb_searchname(conv_str(st, data));
  4923. }
  4924. else
  4925. {// item id
  4926. id = itemdb_exists(conv_num(st, data));
  4927. }
  4928. if( id == NULL )
  4929. {
  4930. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4931. script_pushint(st,0);
  4932. return 1;
  4933. }
  4934. nameid = id->nameid;
  4935. for(i = 0; i < MAX_INVENTORY; i++)
  4936. if(sd->status.inventory[i].nameid == nameid)
  4937. count += sd->status.inventory[i].amount;
  4938. script_pushint(st,count);
  4939. return 0;
  4940. }
  4941. /*==========================================
  4942. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4943. * returns number of items that meet the conditions
  4944. *------------------------------------------*/
  4945. BUILDIN_FUNC(countitem2)
  4946. {
  4947. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4948. int count = 0;
  4949. int i;
  4950. struct item_data* id = NULL;
  4951. struct script_data* data;
  4952. TBL_PC* sd = script_rid2sd(st);
  4953. if (!sd) {
  4954. script_pushint(st,0);
  4955. return 0;
  4956. }
  4957. data = script_getdata(st,2);
  4958. get_val(st, data); // convert into value in case of a variable
  4959. if( data_isstring(data) )
  4960. {// item name
  4961. id = itemdb_searchname(conv_str(st, data));
  4962. }
  4963. else
  4964. {// item id
  4965. id = itemdb_exists(conv_num(st, data));
  4966. }
  4967. if( id == NULL )
  4968. {
  4969. ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4970. script_pushint(st,0);
  4971. return 1;
  4972. }
  4973. nameid = id->nameid;
  4974. iden = script_getnum(st,3);
  4975. ref = script_getnum(st,4);
  4976. attr = script_getnum(st,5);
  4977. c1 = (short)script_getnum(st,6);
  4978. c2 = (short)script_getnum(st,7);
  4979. c3 = (short)script_getnum(st,8);
  4980. c4 = (short)script_getnum(st,9);
  4981. for(i = 0; i < MAX_INVENTORY; i++)
  4982. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4983. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4984. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4985. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4986. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4987. sd->status.inventory[i].card[3] == c4
  4988. )
  4989. count += sd->status.inventory[i].amount;
  4990. script_pushint(st,count);
  4991. return 0;
  4992. }
  4993. /*==========================================
  4994. * 重量チェック
  4995. *------------------------------------------*/
  4996. BUILDIN_FUNC(checkweight)
  4997. {
  4998. int nameid, amount, slots;
  4999. unsigned int weight;
  5000. struct item_data* id = NULL;
  5001. struct map_session_data* sd;
  5002. struct script_data* data;
  5003. if( ( sd = script_rid2sd(st) ) == NULL )
  5004. {
  5005. return 0;
  5006. }
  5007. data = script_getdata(st,2);
  5008. get_val(st, data); // convert into value in case of a variable
  5009. if( data_isstring(data) )
  5010. {// item name
  5011. id = itemdb_searchname(conv_str(st, data));
  5012. }
  5013. else
  5014. {// item id
  5015. id = itemdb_exists(conv_num(st, data));
  5016. }
  5017. if( id == NULL )
  5018. {
  5019. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5020. script_pushint(st,0);
  5021. return 1;
  5022. }
  5023. nameid = id->nameid;
  5024. amount = script_getnum(st,3);
  5025. if( amount < 1 )
  5026. {
  5027. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5028. script_pushint(st,0);
  5029. return 1;
  5030. }
  5031. weight = itemdb_weight(nameid)*amount;
  5032. if( weight + sd->weight > sd->max_weight )
  5033. {// too heavy
  5034. script_pushint(st,0);
  5035. return 0;
  5036. }
  5037. switch( pc_checkadditem(sd, nameid, amount) )
  5038. {
  5039. case ADDITEM_EXIST:
  5040. // item is already in inventory, but there is still space for the requested amount
  5041. break;
  5042. case ADDITEM_NEW:
  5043. slots = pc_inventoryblank(sd);
  5044. if( itemdb_isstackable(nameid) )
  5045. {// stackable
  5046. if( slots < 1 )
  5047. {
  5048. script_pushint(st,0);
  5049. return 0;
  5050. }
  5051. }
  5052. else
  5053. {// non-stackable
  5054. if( slots < amount )
  5055. {
  5056. script_pushint(st,0);
  5057. return 0;
  5058. }
  5059. }
  5060. break;
  5061. case ADDITEM_OVERAMOUNT:
  5062. script_pushint(st,0);
  5063. return 0;
  5064. }
  5065. script_pushint(st,1);
  5066. return 0;
  5067. }
  5068. /*==========================================
  5069. * getitem <item id>,<amount>{,<account ID>};
  5070. * getitem "<item name>",<amount>{,<account ID>};
  5071. *------------------------------------------*/
  5072. BUILDIN_FUNC(getitem)
  5073. {
  5074. int nameid,amount,get_count,i,flag = 0;
  5075. struct item it;
  5076. TBL_PC *sd;
  5077. struct script_data *data;
  5078. data=script_getdata(st,2);
  5079. get_val(st,data);
  5080. if( data_isstring(data) )
  5081. {// "<item name>"
  5082. const char *name=conv_str(st,data);
  5083. struct item_data *item_data = itemdb_searchname(name);
  5084. if( item_data == NULL ){
  5085. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5086. return 1; //No item created.
  5087. }
  5088. nameid=item_data->nameid;
  5089. } else if( data_isint(data) )
  5090. {// <item id>
  5091. nameid=conv_num(st,data);
  5092. //Violet Box, Blue Box, etc - random item pick
  5093. if( nameid < 0 ) {
  5094. nameid = -nameid;
  5095. flag = 1;
  5096. }
  5097. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5098. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5099. return 1; //No item created.
  5100. }
  5101. } else {
  5102. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5103. return 1;
  5104. }
  5105. // <amount>
  5106. if( (amount=script_getnum(st,3)) <= 0)
  5107. return 0; //return if amount <=0, skip the useles iteration
  5108. memset(&it,0,sizeof(it));
  5109. it.nameid=nameid;
  5110. if(!flag)
  5111. it.identify=1;
  5112. else
  5113. it.identify=itemdb_isidentified(nameid);
  5114. if( script_hasdata(st,4) )
  5115. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5116. else
  5117. sd=script_rid2sd(st); // Attached player
  5118. if( sd == NULL ) // no target
  5119. return 0;
  5120. //Check if it's stackable.
  5121. if (!itemdb_isstackable(nameid))
  5122. get_count = 1;
  5123. else
  5124. get_count = amount;
  5125. for (i = 0; i < amount; i += get_count)
  5126. {
  5127. // if not pet egg
  5128. if (!pet_create_egg(sd, nameid))
  5129. {
  5130. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5131. {
  5132. clif_additem(sd, 0, 0, flag);
  5133. if( pc_candrop(sd,&it) )
  5134. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5135. }
  5136. }
  5137. }
  5138. return 0;
  5139. }
  5140. /*==========================================
  5141. *
  5142. *------------------------------------------*/
  5143. BUILDIN_FUNC(getitem2)
  5144. {
  5145. int nameid,amount,get_count,i,flag = 0;
  5146. int iden,ref,attr,c1,c2,c3,c4;
  5147. struct item_data *item_data;
  5148. struct item item_tmp;
  5149. TBL_PC *sd;
  5150. struct script_data *data;
  5151. if( script_hasdata(st,11) )
  5152. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5153. else
  5154. sd=script_rid2sd(st); // Attached player
  5155. if( sd == NULL ) // no target
  5156. return 0;
  5157. data=script_getdata(st,2);
  5158. get_val(st,data);
  5159. if( data_isstring(data) ){
  5160. const char *name=conv_str(st,data);
  5161. struct item_data *item_data = itemdb_searchname(name);
  5162. if( item_data )
  5163. nameid=item_data->nameid;
  5164. else
  5165. nameid=UNKNOWN_ITEM_ID;
  5166. }else
  5167. nameid=conv_num(st,data);
  5168. amount=script_getnum(st,3);
  5169. iden=script_getnum(st,4);
  5170. ref=script_getnum(st,5);
  5171. attr=script_getnum(st,6);
  5172. c1=(short)script_getnum(st,7);
  5173. c2=(short)script_getnum(st,8);
  5174. c3=(short)script_getnum(st,9);
  5175. c4=(short)script_getnum(st,10);
  5176. if(nameid<0) { // ランダム
  5177. nameid = -nameid;
  5178. flag = 1;
  5179. }
  5180. if(nameid > 0) {
  5181. memset(&item_tmp,0,sizeof(item_tmp));
  5182. item_data=itemdb_exists(nameid);
  5183. if (item_data == NULL)
  5184. return -1;
  5185. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5186. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5187. }
  5188. else if(item_data->type==IT_PETEGG) {
  5189. iden = 1;
  5190. ref = 0;
  5191. }
  5192. else {
  5193. iden = 1;
  5194. ref = attr = 0;
  5195. }
  5196. item_tmp.nameid=nameid;
  5197. if(!flag)
  5198. item_tmp.identify=iden;
  5199. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5200. item_tmp.identify=0;
  5201. item_tmp.refine=ref;
  5202. item_tmp.attribute=attr;
  5203. item_tmp.card[0]=(short)c1;
  5204. item_tmp.card[1]=(short)c2;
  5205. item_tmp.card[2]=(short)c3;
  5206. item_tmp.card[3]=(short)c4;
  5207. //Check if it's stackable.
  5208. if (!itemdb_isstackable(nameid))
  5209. get_count = 1;
  5210. else
  5211. get_count = amount;
  5212. for (i = 0; i < amount; i += get_count)
  5213. {
  5214. // if not pet egg
  5215. if (!pet_create_egg(sd, nameid))
  5216. {
  5217. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5218. {
  5219. clif_additem(sd, 0, 0, flag);
  5220. if( pc_candrop(sd,&item_tmp) )
  5221. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5222. }
  5223. }
  5224. }
  5225. }
  5226. return 0;
  5227. }
  5228. /*==========================================
  5229. * rentitem <item id>,<seconds>
  5230. * rentitem "<item name>",<seconds>
  5231. *------------------------------------------*/
  5232. BUILDIN_FUNC(rentitem)
  5233. {
  5234. struct map_session_data *sd;
  5235. struct script_data *data;
  5236. struct item it;
  5237. int seconds;
  5238. int nameid = 0, flag;
  5239. data = script_getdata(st,2);
  5240. get_val(st,data);
  5241. if( (sd = script_rid2sd(st)) == NULL )
  5242. return 0;
  5243. if( data_isstring(data) )
  5244. {
  5245. const char *name = conv_str(st,data);
  5246. struct item_data *itd = itemdb_searchname(name);
  5247. if( itd == NULL )
  5248. {
  5249. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5250. return 1;
  5251. }
  5252. nameid = itd->nameid;
  5253. }
  5254. else if( data_isint(data) )
  5255. {
  5256. nameid = conv_num(st,data);
  5257. if( nameid <= 0 || !itemdb_exists(nameid) )
  5258. {
  5259. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5260. return 1;
  5261. }
  5262. }
  5263. else
  5264. {
  5265. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5266. return 1;
  5267. }
  5268. seconds = script_getnum(st,3);
  5269. memset(&it, 0, sizeof(it));
  5270. it.nameid = nameid;
  5271. it.identify = 1;
  5272. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5273. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5274. {
  5275. clif_additem(sd, 0, 0, flag);
  5276. return 1;
  5277. }
  5278. clif_rental_time(sd->fd, nameid, seconds);
  5279. pc_inventory_rental_add(sd, seconds);
  5280. return 0;
  5281. }
  5282. /*==========================================
  5283. * gets an item with someone's name inscribed [Skotlex]
  5284. * getinscribeditem item_num, character_name
  5285. * Returned Qty is always 1, only works on equip-able
  5286. * equipment
  5287. *------------------------------------------*/
  5288. BUILDIN_FUNC(getnameditem)
  5289. {
  5290. int nameid;
  5291. struct item item_tmp;
  5292. TBL_PC *sd, *tsd;
  5293. struct script_data *data;
  5294. sd = script_rid2sd(st);
  5295. if (sd == NULL)
  5296. { //Player not attached!
  5297. script_pushint(st,0);
  5298. return 0;
  5299. }
  5300. data=script_getdata(st,2);
  5301. get_val(st,data);
  5302. if( data_isstring(data) ){
  5303. const char *name=conv_str(st,data);
  5304. struct item_data *item_data = itemdb_searchname(name);
  5305. if( item_data == NULL)
  5306. { //Failed
  5307. script_pushint(st,0);
  5308. return 0;
  5309. }
  5310. nameid = item_data->nameid;
  5311. }else
  5312. nameid = conv_num(st,data);
  5313. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5314. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5315. script_pushint(st,0);
  5316. return 0;
  5317. }
  5318. data=script_getdata(st,3);
  5319. get_val(st,data);
  5320. if( data_isstring(data) ) //Char Name
  5321. tsd=map_nick2sd(conv_str(st,data));
  5322. else //Char Id was given
  5323. tsd=map_charid2sd(conv_num(st,data));
  5324. if( tsd == NULL )
  5325. { //Failed
  5326. script_pushint(st,0);
  5327. return 0;
  5328. }
  5329. memset(&item_tmp,0,sizeof(item_tmp));
  5330. item_tmp.nameid=nameid;
  5331. item_tmp.amount=1;
  5332. item_tmp.identify=1;
  5333. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5334. item_tmp.card[2]=tsd->status.char_id;
  5335. item_tmp.card[3]=tsd->status.char_id >> 16;
  5336. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5337. script_pushint(st,0);
  5338. return 0; //Failed to add item, we will not drop if they don't fit
  5339. }
  5340. script_pushint(st,1);
  5341. return 0;
  5342. }
  5343. /*==========================================
  5344. * gets a random item ID from an item group [Skotlex]
  5345. * groupranditem group_num
  5346. *------------------------------------------*/
  5347. BUILDIN_FUNC(grouprandomitem)
  5348. {
  5349. int group;
  5350. group = script_getnum(st,2);
  5351. script_pushint(st,itemdb_searchrandomid(group));
  5352. return 0;
  5353. }
  5354. /*==========================================
  5355. *
  5356. *------------------------------------------*/
  5357. BUILDIN_FUNC(makeitem)
  5358. {
  5359. int nameid,amount,flag = 0;
  5360. int x,y,m;
  5361. const char *mapname;
  5362. struct item item_tmp;
  5363. struct script_data *data;
  5364. data=script_getdata(st,2);
  5365. get_val(st,data);
  5366. if( data_isstring(data) ){
  5367. const char *name=conv_str(st,data);
  5368. struct item_data *item_data = itemdb_searchname(name);
  5369. if( item_data )
  5370. nameid=item_data->nameid;
  5371. else
  5372. nameid=UNKNOWN_ITEM_ID;
  5373. }else
  5374. nameid=conv_num(st,data);
  5375. amount=script_getnum(st,3);
  5376. mapname =script_getstr(st,4);
  5377. x =script_getnum(st,5);
  5378. y =script_getnum(st,6);
  5379. if(strcmp(mapname,"this")==0)
  5380. {
  5381. TBL_PC *sd;
  5382. sd = script_rid2sd(st);
  5383. if (!sd) return 0; //Failed...
  5384. m=sd->bl.m;
  5385. } else
  5386. m=map_mapname2mapid(mapname);
  5387. if(nameid<0) { // ランダム
  5388. nameid = -nameid;
  5389. flag = 1;
  5390. }
  5391. if(nameid > 0) {
  5392. memset(&item_tmp,0,sizeof(item_tmp));
  5393. item_tmp.nameid=nameid;
  5394. if(!flag)
  5395. item_tmp.identify=1;
  5396. else
  5397. item_tmp.identify=itemdb_isidentified(nameid);
  5398. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5399. }
  5400. return 0;
  5401. }
  5402. /// Counts / deletes the current item given by idx.
  5403. /// Used by buildin_delitem_search
  5404. /// Relies on all input data being already fully valid.
  5405. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5406. {
  5407. int delamount;
  5408. struct item* inv = &sd->status.inventory[idx];
  5409. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5410. if( delete_items )
  5411. {
  5412. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5413. {// delete associated pet
  5414. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5415. }
  5416. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5417. }
  5418. amount[0]-= delamount;
  5419. }
  5420. /// Searches for item(s) and checks, if there is enough of them.
  5421. /// Used by delitem and delitem2
  5422. /// Relies on all input data being already fully valid.
  5423. /// @param exact_match will also match item attributes and cards, not just name id
  5424. /// @return true when all items could be deleted, false when there were not enough items to delete
  5425. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5426. {
  5427. bool delete_items = false;
  5428. int i, amount, important;
  5429. struct item* inv;
  5430. // prefer always non-equipped items
  5431. it->equip = 0;
  5432. // when searching for nameid only, prefer additionally
  5433. if( !exact_match )
  5434. {
  5435. // non-refined items
  5436. it->refine = 0;
  5437. // card-less items
  5438. memset(it->card, 0, sizeof(it->card));
  5439. }
  5440. for(;;)
  5441. {
  5442. amount = it->amount;
  5443. important = 0;
  5444. // 1st pass -- less important items / exact match
  5445. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5446. {
  5447. inv = &sd->status.inventory[i];
  5448. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5449. {// wrong/invalid item
  5450. continue;
  5451. }
  5452. if( inv->equip != it->equip || inv->refine != it->refine )
  5453. {// not matching attributes
  5454. important++;
  5455. continue;
  5456. }
  5457. if( exact_match )
  5458. {
  5459. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5460. {// not matching exact attributes
  5461. continue;
  5462. }
  5463. }
  5464. else
  5465. {
  5466. if( sd->inventory_data[i]->type == IT_PETEGG )
  5467. {
  5468. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5469. {// pet which cannot be deleted
  5470. continue;
  5471. }
  5472. }
  5473. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  5474. {// named/carded item
  5475. important++;
  5476. continue;
  5477. }
  5478. }
  5479. // count / delete item
  5480. buildin_delitem_delete(sd, i, &amount, delete_items);
  5481. }
  5482. // 2nd pass -- any matching item
  5483. if( amount == 0 || important == 0 )
  5484. {// either everything was already consumed or no items were skipped
  5485. ;
  5486. }
  5487. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5488. {
  5489. inv = &sd->status.inventory[i];
  5490. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5491. {// wrong/invalid item
  5492. continue;
  5493. }
  5494. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  5495. {// pet which cannot be deleted
  5496. continue;
  5497. }
  5498. if( exact_match )
  5499. {
  5500. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5501. {// not matching attributes
  5502. continue;
  5503. }
  5504. }
  5505. // count / delete item
  5506. buildin_delitem_delete(sd, i, &amount, delete_items);
  5507. }
  5508. if( amount )
  5509. {// not enough items
  5510. return false;
  5511. }
  5512. else if( delete_items )
  5513. {// we are done with the work
  5514. return true;
  5515. }
  5516. else
  5517. {// get rid of the items now
  5518. delete_items = true;
  5519. }
  5520. }
  5521. }
  5522. /// Deletes items from the target/attached player.
  5523. /// Prioritizes ordinary items.
  5524. ///
  5525. /// delitem <item id>,<amount>{,<account id>}
  5526. /// delitem "<item name>",<amount>{,<account id>}
  5527. BUILDIN_FUNC(delitem)
  5528. {
  5529. TBL_PC *sd;
  5530. struct item it;
  5531. struct script_data *data;
  5532. if( script_hasdata(st,4) )
  5533. {
  5534. int account_id = script_getnum(st,4);
  5535. sd = map_id2sd(account_id); // <account id>
  5536. if( sd == NULL )
  5537. {
  5538. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5539. st->state = END;
  5540. return 1;
  5541. }
  5542. }
  5543. else
  5544. {
  5545. sd = script_rid2sd(st);// attached player
  5546. if( sd == NULL )
  5547. return 0;
  5548. }
  5549. data = script_getdata(st,2);
  5550. get_val(st,data);
  5551. if( data_isstring(data) )
  5552. {
  5553. const char* item_name = conv_str(st,data);
  5554. struct item_data* id = itemdb_searchname(item_name);
  5555. if( id == NULL )
  5556. {
  5557. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5558. st->state = END;
  5559. return 1;
  5560. }
  5561. it.nameid = id->nameid;// "<item name>"
  5562. }
  5563. else
  5564. {
  5565. it.nameid = conv_num(st,data);// <item id>
  5566. if( !itemdb_exists( it.nameid ) )
  5567. {
  5568. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5569. st->state = END;
  5570. return 1;
  5571. }
  5572. }
  5573. it.amount=script_getnum(st,3);
  5574. if( it.amount <= 0 )
  5575. return 0;// nothing to do
  5576. if( buildin_delitem_search(sd, &it, false) )
  5577. {// success
  5578. return 0;
  5579. }
  5580. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5581. st->state = END;
  5582. clif_scriptclose(sd, st->oid);
  5583. return 1;
  5584. }
  5585. /// Deletes items from the target/attached player.
  5586. ///
  5587. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5588. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5589. BUILDIN_FUNC(delitem2)
  5590. {
  5591. TBL_PC *sd;
  5592. struct item it;
  5593. struct script_data *data;
  5594. if( script_hasdata(st,11) )
  5595. {
  5596. int account_id = script_getnum(st,11);
  5597. sd = map_id2sd(account_id); // <account id>
  5598. if( sd == NULL )
  5599. {
  5600. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5601. st->state = END;
  5602. return 1;
  5603. }
  5604. }
  5605. else
  5606. {
  5607. sd = script_rid2sd(st);// attached player
  5608. if( sd == NULL )
  5609. return 0;
  5610. }
  5611. data = script_getdata(st,2);
  5612. get_val(st,data);
  5613. if( data_isstring(data) )
  5614. {
  5615. const char* item_name = conv_str(st,data);
  5616. struct item_data* id = itemdb_searchname(item_name);
  5617. if( id == NULL )
  5618. {
  5619. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5620. st->state = END;
  5621. return 1;
  5622. }
  5623. it.nameid = id->nameid;// "<item name>"
  5624. }
  5625. else
  5626. {
  5627. it.nameid = conv_num(st,data);// <item id>
  5628. if( !itemdb_exists( it.nameid ) )
  5629. {
  5630. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5631. st->state = END;
  5632. return 1;
  5633. }
  5634. }
  5635. it.amount=script_getnum(st,3);
  5636. it.identify=script_getnum(st,4);
  5637. it.refine=script_getnum(st,5);
  5638. it.attribute=script_getnum(st,6);
  5639. it.card[0]=(short)script_getnum(st,7);
  5640. it.card[1]=(short)script_getnum(st,8);
  5641. it.card[2]=(short)script_getnum(st,9);
  5642. it.card[3]=(short)script_getnum(st,10);
  5643. if( it.amount <= 0 )
  5644. return 0;// nothing to do
  5645. if( buildin_delitem_search(sd, &it, true) )
  5646. {// success
  5647. return 0;
  5648. }
  5649. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5650. st->state = END;
  5651. clif_scriptclose(sd, st->oid);
  5652. return 1;
  5653. }
  5654. /*==========================================
  5655. * Enables/Disables use of items while in an NPC [Skotlex]
  5656. *------------------------------------------*/
  5657. BUILDIN_FUNC(enableitemuse)
  5658. {
  5659. TBL_PC *sd;
  5660. sd=script_rid2sd(st);
  5661. if (sd)
  5662. sd->npc_item_flag = st->oid;
  5663. return 0;
  5664. }
  5665. BUILDIN_FUNC(disableitemuse)
  5666. {
  5667. TBL_PC *sd;
  5668. sd=script_rid2sd(st);
  5669. if (sd)
  5670. sd->npc_item_flag = 0;
  5671. return 0;
  5672. }
  5673. /*==========================================
  5674. *キャラ関係のパラメータ取得
  5675. *------------------------------------------*/
  5676. BUILDIN_FUNC(readparam)
  5677. {
  5678. int type;
  5679. TBL_PC *sd;
  5680. type=script_getnum(st,2);
  5681. if( script_hasdata(st,3) )
  5682. sd=map_nick2sd(script_getstr(st,3));
  5683. else
  5684. sd=script_rid2sd(st);
  5685. if(sd==NULL){
  5686. script_pushint(st,-1);
  5687. return 0;
  5688. }
  5689. script_pushint(st,pc_readparam(sd,type));
  5690. return 0;
  5691. }
  5692. /*==========================================
  5693. *キャラ関係のID取得
  5694. *------------------------------------------*/
  5695. BUILDIN_FUNC(getcharid)
  5696. {
  5697. int num;
  5698. TBL_PC *sd;
  5699. num = script_getnum(st,2);
  5700. if( script_hasdata(st,3) )
  5701. sd=map_nick2sd(script_getstr(st,3));
  5702. else
  5703. sd=script_rid2sd(st);
  5704. if(sd==NULL){
  5705. script_pushint(st,0); //return 0, according docs
  5706. return 0;
  5707. }
  5708. switch( num ) {
  5709. case 0: script_pushint(st,sd->status.char_id); break;
  5710. case 1: script_pushint(st,sd->status.party_id); break;
  5711. case 2: script_pushint(st,sd->status.guild_id); break;
  5712. case 3: script_pushint(st,sd->status.account_id); break;
  5713. case 4: script_pushint(st,sd->bg_id); break;
  5714. default:
  5715. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5716. script_pushint(st,0);
  5717. break;
  5718. }
  5719. return 0;
  5720. }
  5721. /*==========================================
  5722. * returns the GID of an NPC
  5723. *------------------------------------------*/
  5724. BUILDIN_FUNC(getnpcid)
  5725. {
  5726. int num = script_getnum(st,2);
  5727. struct npc_data* nd = NULL;
  5728. if( script_hasdata(st,3) )
  5729. {// unique npc name
  5730. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  5731. {
  5732. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  5733. script_pushint(st,0);
  5734. return 1;
  5735. }
  5736. }
  5737. switch (num) {
  5738. case 0:
  5739. script_pushint(st,nd ? nd->bl.id : st->oid);
  5740. break;
  5741. default:
  5742. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  5743. script_pushint(st,0);
  5744. return 1;
  5745. }
  5746. return 0;
  5747. }
  5748. /*==========================================
  5749. *指定IDのPT名取得
  5750. *------------------------------------------*/
  5751. BUILDIN_FUNC(getpartyname)
  5752. {
  5753. int party_id;
  5754. struct party_data* p;
  5755. party_id = script_getnum(st,2);
  5756. if( ( p = party_search(party_id) ) != NULL )
  5757. {
  5758. script_pushstrcopy(st,p->party.name);
  5759. }
  5760. else
  5761. {
  5762. script_pushconststr(st,"null");
  5763. }
  5764. return 0;
  5765. }
  5766. /*==========================================
  5767. *指定IDのPT人数とメンバーID取得
  5768. *------------------------------------------*/
  5769. BUILDIN_FUNC(getpartymember)
  5770. {
  5771. struct party_data *p;
  5772. int i,j=0,type=0;
  5773. p=party_search(script_getnum(st,2));
  5774. if( script_hasdata(st,3) )
  5775. type=script_getnum(st,3);
  5776. if(p!=NULL){
  5777. for(i=0;i<MAX_PARTY;i++){
  5778. if(p->party.member[i].account_id){
  5779. switch (type) {
  5780. case 2:
  5781. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  5782. break;
  5783. case 1:
  5784. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  5785. break;
  5786. default:
  5787. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  5788. }
  5789. j++;
  5790. }
  5791. }
  5792. }
  5793. mapreg_setreg(add_str("$@partymembercount"),j);
  5794. return 0;
  5795. }
  5796. /*==========================================
  5797. * Retrieves party leader. if flag is specified,
  5798. * return some of the leader data. Otherwise, return name.
  5799. *------------------------------------------*/
  5800. BUILDIN_FUNC(getpartyleader)
  5801. {
  5802. int party_id, type = 0, i=0;
  5803. struct party_data *p;
  5804. party_id=script_getnum(st,2);
  5805. if( script_hasdata(st,3) )
  5806. type=script_getnum(st,3);
  5807. p=party_search(party_id);
  5808. if (p) //Search leader
  5809. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5810. if (!p || i == MAX_PARTY) { //leader not found
  5811. if (type)
  5812. script_pushint(st,-1);
  5813. else
  5814. script_pushconststr(st,"null");
  5815. return 0;
  5816. }
  5817. switch (type) {
  5818. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5819. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5820. case 3: script_pushint(st,p->party.member[i].class_); break;
  5821. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5822. case 5: script_pushint(st,p->party.member[i].lv); break;
  5823. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5824. }
  5825. return 0;
  5826. }
  5827. /*==========================================
  5828. *指定IDのギルド名取得
  5829. *------------------------------------------*/
  5830. BUILDIN_FUNC(getguildname)
  5831. {
  5832. int guild_id;
  5833. struct guild* g;
  5834. guild_id = script_getnum(st,2);
  5835. if( ( g = guild_search(guild_id) ) != NULL )
  5836. {
  5837. script_pushstrcopy(st,g->name);
  5838. }
  5839. else
  5840. {
  5841. script_pushconststr(st,"null");
  5842. }
  5843. return 0;
  5844. }
  5845. /*==========================================
  5846. *指定IDのGuildMaster名取得
  5847. *------------------------------------------*/
  5848. BUILDIN_FUNC(getguildmaster)
  5849. {
  5850. int guild_id;
  5851. struct guild* g;
  5852. guild_id = script_getnum(st,2);
  5853. if( ( g = guild_search(guild_id) ) != NULL )
  5854. {
  5855. script_pushstrcopy(st,g->member[0].name);
  5856. }
  5857. else
  5858. {
  5859. script_pushconststr(st,"null");
  5860. }
  5861. return 0;
  5862. }
  5863. BUILDIN_FUNC(getguildmasterid)
  5864. {
  5865. int guild_id;
  5866. struct guild* g;
  5867. guild_id = script_getnum(st,2);
  5868. if( ( g = guild_search(guild_id) ) != NULL )
  5869. {
  5870. script_pushint(st,g->member[0].char_id);
  5871. }
  5872. else
  5873. {
  5874. script_pushint(st,0);
  5875. }
  5876. return 0;
  5877. }
  5878. /*==========================================
  5879. * キャラクタの名前
  5880. *------------------------------------------*/
  5881. BUILDIN_FUNC(strcharinfo)
  5882. {
  5883. TBL_PC *sd;
  5884. int num;
  5885. struct guild* g;
  5886. struct party_data* p;
  5887. sd=script_rid2sd(st);
  5888. if (!sd) { //Avoid crashing....
  5889. script_pushconststr(st,"");
  5890. return 0;
  5891. }
  5892. num=script_getnum(st,2);
  5893. switch(num){
  5894. case 0:
  5895. script_pushstrcopy(st,sd->status.name);
  5896. break;
  5897. case 1:
  5898. if( ( p = party_search(sd->status.party_id) ) != NULL )
  5899. {
  5900. script_pushstrcopy(st,p->party.name);
  5901. }
  5902. else
  5903. {
  5904. script_pushconststr(st,"");
  5905. }
  5906. break;
  5907. case 2:
  5908. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  5909. {
  5910. script_pushstrcopy(st,g->name);
  5911. }
  5912. else
  5913. {
  5914. script_pushconststr(st,"");
  5915. }
  5916. break;
  5917. case 3:
  5918. script_pushconststr(st,map[sd->bl.m].name);
  5919. break;
  5920. default:
  5921. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5922. script_pushconststr(st,"");
  5923. break;
  5924. }
  5925. return 0;
  5926. }
  5927. /*==========================================
  5928. * 呼び出し元のNPC情報を取得する
  5929. *------------------------------------------*/
  5930. BUILDIN_FUNC(strnpcinfo)
  5931. {
  5932. TBL_NPC* nd;
  5933. int num;
  5934. char *buf,*name=NULL;
  5935. nd = map_id2nd(st->oid);
  5936. if (!nd) {
  5937. script_pushconststr(st, "");
  5938. return 0;
  5939. }
  5940. num = script_getnum(st,2);
  5941. switch(num){
  5942. case 0: // display name
  5943. name = aStrdup(nd->name);
  5944. break;
  5945. case 1: // visible part of display name
  5946. if((buf = strchr(nd->name,'#')) != NULL)
  5947. {
  5948. name = aStrdup(nd->name);
  5949. name[buf - nd->name] = 0;
  5950. } else // Return the name, there is no '#' present
  5951. name = aStrdup(nd->name);
  5952. break;
  5953. case 2: // # fragment
  5954. if((buf = strchr(nd->name,'#')) != NULL)
  5955. name = aStrdup(buf+1);
  5956. break;
  5957. case 3: // unique name
  5958. name = aStrdup(nd->exname);
  5959. break;
  5960. case 4: // map name
  5961. name = aStrdup(map[nd->bl.m].name);
  5962. break;
  5963. }
  5964. if(name)
  5965. script_pushstr(st, name);
  5966. else
  5967. script_pushconststr(st, "");
  5968. return 0;
  5969. }
  5970. // aegis->athena slot position conversion table
  5971. 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};
  5972. /*==========================================
  5973. * GetEquipID(Pos); Pos: 1-10
  5974. *------------------------------------------*/
  5975. BUILDIN_FUNC(getequipid)
  5976. {
  5977. int i, num;
  5978. TBL_PC* sd;
  5979. struct item_data* item;
  5980. sd = script_rid2sd(st);
  5981. if( sd == NULL )
  5982. return 0;
  5983. num = script_getnum(st,2) - 1;
  5984. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5985. {
  5986. script_pushint(st,-1);
  5987. return 0;
  5988. }
  5989. // get inventory position of item
  5990. i = pc_checkequip(sd,equip[num]);
  5991. if( i < 0 )
  5992. {
  5993. script_pushint(st,-1);
  5994. return 0;
  5995. }
  5996. item = sd->inventory_data[i];
  5997. if( item != 0 )
  5998. script_pushint(st,item->nameid);
  5999. else
  6000. script_pushint(st,0);
  6001. return 0;
  6002. }
  6003. /*==========================================
  6004. * 装備名文字列(精錬メニュー用)
  6005. *------------------------------------------*/
  6006. BUILDIN_FUNC(getequipname)
  6007. {
  6008. int i, num;
  6009. TBL_PC* sd;
  6010. struct item_data* item;
  6011. sd = script_rid2sd(st);
  6012. if( sd == NULL )
  6013. return 0;
  6014. num = script_getnum(st,2) - 1;
  6015. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6016. {
  6017. script_pushconststr(st,"");
  6018. return 0;
  6019. }
  6020. // get inventory position of item
  6021. i = pc_checkequip(sd,equip[num]);
  6022. if( i < 0 )
  6023. {
  6024. script_pushint(st,-1);
  6025. return 0;
  6026. }
  6027. item = sd->inventory_data[i];
  6028. if( item != 0 )
  6029. script_pushstrcopy(st,item->jname);
  6030. else
  6031. script_pushconststr(st,"");
  6032. return 0;
  6033. }
  6034. /*==========================================
  6035. * getbrokenid [Valaris]
  6036. *------------------------------------------*/
  6037. BUILDIN_FUNC(getbrokenid)
  6038. {
  6039. int i,num,id=0,brokencounter=0;
  6040. TBL_PC *sd;
  6041. sd = script_rid2sd(st);
  6042. if( sd == NULL )
  6043. return 0;
  6044. num=script_getnum(st,2);
  6045. for(i=0; i<MAX_INVENTORY; i++) {
  6046. if(sd->status.inventory[i].attribute){
  6047. brokencounter++;
  6048. if(num==brokencounter){
  6049. id=sd->status.inventory[i].nameid;
  6050. break;
  6051. }
  6052. }
  6053. }
  6054. script_pushint(st,id);
  6055. return 0;
  6056. }
  6057. /*==========================================
  6058. * repair [Valaris]
  6059. *------------------------------------------*/
  6060. BUILDIN_FUNC(repair)
  6061. {
  6062. int i,num;
  6063. int repaircounter=0;
  6064. TBL_PC *sd;
  6065. sd = script_rid2sd(st);
  6066. if( sd == NULL )
  6067. return 0;
  6068. num=script_getnum(st,2);
  6069. for(i=0; i<MAX_INVENTORY; i++) {
  6070. if(sd->status.inventory[i].attribute){
  6071. repaircounter++;
  6072. if(num==repaircounter){
  6073. sd->status.inventory[i].attribute=0;
  6074. clif_equiplist(sd);
  6075. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6076. clif_misceffect(&sd->bl, 3);
  6077. break;
  6078. }
  6079. }
  6080. }
  6081. return 0;
  6082. }
  6083. /*==========================================
  6084. * 装備チェック
  6085. *------------------------------------------*/
  6086. BUILDIN_FUNC(getequipisequiped)
  6087. {
  6088. int i=-1,num;
  6089. TBL_PC *sd;
  6090. num=script_getnum(st,2);
  6091. sd = script_rid2sd(st);
  6092. if( sd == NULL )
  6093. return 0;
  6094. if (num > 0 && num <= ARRAYLENGTH(equip))
  6095. i=pc_checkequip(sd,equip[num-1]);
  6096. if(i >= 0)
  6097. script_pushint(st,1);
  6098. else
  6099. script_pushint(st,0);
  6100. return 0;
  6101. }
  6102. /*==========================================
  6103. * 装備品精錬可能チェック
  6104. *------------------------------------------*/
  6105. BUILDIN_FUNC(getequipisenableref)
  6106. {
  6107. int i=-1,num;
  6108. TBL_PC *sd;
  6109. num=script_getnum(st,2);
  6110. sd = script_rid2sd(st);
  6111. if( sd == NULL )
  6112. return 0;
  6113. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6114. i = pc_checkequip(sd,equip[num-1]);
  6115. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6116. script_pushint(st,1);
  6117. else
  6118. script_pushint(st,0);
  6119. return 0;
  6120. }
  6121. /*==========================================
  6122. * 装備品鑑定チェック
  6123. *------------------------------------------*/
  6124. BUILDIN_FUNC(getequipisidentify)
  6125. {
  6126. int i=-1,num;
  6127. TBL_PC *sd;
  6128. num=script_getnum(st,2);
  6129. sd = script_rid2sd(st);
  6130. if( sd == NULL )
  6131. return 0;
  6132. if (num > 0 && num <= ARRAYLENGTH(equip))
  6133. i=pc_checkequip(sd,equip[num-1]);
  6134. if(i >= 0)
  6135. script_pushint(st,sd->status.inventory[i].identify);
  6136. else
  6137. script_pushint(st,0);
  6138. return 0;
  6139. }
  6140. /*==========================================
  6141. * 装備品精錬度
  6142. *------------------------------------------*/
  6143. BUILDIN_FUNC(getequiprefinerycnt)
  6144. {
  6145. int i=-1,num;
  6146. TBL_PC *sd;
  6147. num=script_getnum(st,2);
  6148. sd = script_rid2sd(st);
  6149. if( sd == NULL )
  6150. return 0;
  6151. if (num > 0 && num <= ARRAYLENGTH(equip))
  6152. i=pc_checkequip(sd,equip[num-1]);
  6153. if(i >= 0)
  6154. script_pushint(st,sd->status.inventory[i].refine);
  6155. else
  6156. script_pushint(st,0);
  6157. return 0;
  6158. }
  6159. /*==========================================
  6160. * 装備品武器LV
  6161. *------------------------------------------*/
  6162. BUILDIN_FUNC(getequipweaponlv)
  6163. {
  6164. int i=-1,num;
  6165. TBL_PC *sd;
  6166. num=script_getnum(st,2);
  6167. sd = script_rid2sd(st);
  6168. if( sd == NULL )
  6169. return 0;
  6170. if (num > 0 && num <= ARRAYLENGTH(equip))
  6171. i=pc_checkequip(sd,equip[num-1]);
  6172. if(i >= 0 && sd->inventory_data[i])
  6173. script_pushint(st,sd->inventory_data[i]->wlv);
  6174. else
  6175. script_pushint(st,0);
  6176. return 0;
  6177. }
  6178. /*==========================================
  6179. * 装備品精錬成功率
  6180. *------------------------------------------*/
  6181. BUILDIN_FUNC(getequippercentrefinery)
  6182. {
  6183. int i=-1,num;
  6184. TBL_PC *sd;
  6185. num=script_getnum(st,2);
  6186. sd = script_rid2sd(st);
  6187. if( sd == NULL )
  6188. return 0;
  6189. if (num > 0 && num <= ARRAYLENGTH(equip))
  6190. i=pc_checkequip(sd,equip[num-1]);
  6191. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6192. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6193. else
  6194. script_pushint(st,0);
  6195. return 0;
  6196. }
  6197. /*==========================================
  6198. * 精錬成功
  6199. *------------------------------------------*/
  6200. BUILDIN_FUNC(successrefitem)
  6201. {
  6202. int i=-1,num,ep;
  6203. TBL_PC *sd;
  6204. num=script_getnum(st,2);
  6205. sd = script_rid2sd(st);
  6206. if( sd == NULL )
  6207. return 0;
  6208. if (num > 0 && num <= ARRAYLENGTH(equip))
  6209. i=pc_checkequip(sd,equip[num-1]);
  6210. if(i >= 0) {
  6211. ep=sd->status.inventory[i].equip;
  6212. //Logs items, got from (N)PC scripts [Lupus]
  6213. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6214. sd->status.inventory[i].refine++;
  6215. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6216. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6217. clif_delitem(sd,i,1,3);
  6218. //Logs items, got from (N)PC scripts [Lupus]
  6219. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6220. clif_additem(sd,i,1,0);
  6221. pc_equipitem(sd,i,ep);
  6222. clif_misceffect(&sd->bl,3);
  6223. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6224. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6225. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6226. ){ // Fame point system [DracoRPG]
  6227. switch (sd->inventory_data[i]->wlv){
  6228. case 1:
  6229. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6230. break;
  6231. case 2:
  6232. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6233. break;
  6234. case 3:
  6235. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6236. break;
  6237. }
  6238. }
  6239. }
  6240. return 0;
  6241. }
  6242. /*==========================================
  6243. * 精錬失敗
  6244. *------------------------------------------*/
  6245. BUILDIN_FUNC(failedrefitem)
  6246. {
  6247. int i=-1,num;
  6248. TBL_PC *sd;
  6249. num=script_getnum(st,2);
  6250. sd = script_rid2sd(st);
  6251. if( sd == NULL )
  6252. return 0;
  6253. if (num > 0 && num <= ARRAYLENGTH(equip))
  6254. i=pc_checkequip(sd,equip[num-1]);
  6255. if(i >= 0) {
  6256. sd->status.inventory[i].refine = 0;
  6257. pc_unequipitem(sd,i,3);
  6258. // 精錬失敗エフェクトのパケット
  6259. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6260. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6261. // 他の人にも失敗を通知
  6262. clif_misceffect(&sd->bl,2);
  6263. }
  6264. return 0;
  6265. }
  6266. /*==========================================
  6267. *
  6268. *------------------------------------------*/
  6269. BUILDIN_FUNC(statusup)
  6270. {
  6271. int type;
  6272. TBL_PC *sd;
  6273. type=script_getnum(st,2);
  6274. sd = script_rid2sd(st);
  6275. if( sd == NULL )
  6276. return 0;
  6277. pc_statusup(sd,type);
  6278. return 0;
  6279. }
  6280. /*==========================================
  6281. *
  6282. *------------------------------------------*/
  6283. BUILDIN_FUNC(statusup2)
  6284. {
  6285. int type,val;
  6286. TBL_PC *sd;
  6287. type=script_getnum(st,2);
  6288. val=script_getnum(st,3);
  6289. sd = script_rid2sd(st);
  6290. if( sd == NULL )
  6291. return 0;
  6292. pc_statusup2(sd,type,val);
  6293. return 0;
  6294. }
  6295. /// See 'doc/item_bonus.txt'
  6296. ///
  6297. /// bonus <bonus type>,<val1>;
  6298. /// bonus2 <bonus type>,<val1>,<val2>;
  6299. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6300. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6301. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6302. BUILDIN_FUNC(bonus)
  6303. {
  6304. int type;
  6305. int val1;
  6306. int val2 = 0;
  6307. int val3 = 0;
  6308. int val4 = 0;
  6309. int val5 = 0;
  6310. TBL_PC* sd;
  6311. sd = script_rid2sd(st);
  6312. if( sd == NULL )
  6313. return 0; // no player attached
  6314. type = script_getnum(st,2);
  6315. switch( type )
  6316. {
  6317. case SP_AUTOSPELL:
  6318. case SP_AUTOSPELL_WHENHIT:
  6319. case SP_AUTOSPELL_ONSKILL:
  6320. case SP_SKILL_ATK:
  6321. case SP_SKILL_HEAL:
  6322. case SP_SKILL_HEAL2:
  6323. case SP_ADD_SKILL_BLOW:
  6324. case SP_CASTRATE:
  6325. case SP_ADDEFF_ONSKILL:
  6326. // these bonuses support skill names
  6327. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6328. break;
  6329. default:
  6330. val1 = script_getnum(st,3);
  6331. break;
  6332. }
  6333. switch( script_lastdata(st)-2 )
  6334. {
  6335. case 1:
  6336. pc_bonus(sd, type, val1);
  6337. break;
  6338. case 2:
  6339. val2 = script_getnum(st,4);
  6340. pc_bonus2(sd, type, val1, val2);
  6341. break;
  6342. case 3:
  6343. val2 = script_getnum(st,4);
  6344. val3 = script_getnum(st,5);
  6345. pc_bonus3(sd, type, val1, val2, val3);
  6346. break;
  6347. case 4:
  6348. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6349. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6350. else
  6351. val2 = script_getnum(st,4);
  6352. val3 = script_getnum(st,5);
  6353. val4 = script_getnum(st,6);
  6354. pc_bonus4(sd, type, val1, val2, val3, val4);
  6355. break;
  6356. case 5:
  6357. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6358. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6359. else
  6360. val2 = script_getnum(st,4);
  6361. val3 = script_getnum(st,5);
  6362. val4 = script_getnum(st,6);
  6363. val5 = script_getnum(st,7);
  6364. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6365. break;
  6366. default:
  6367. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6368. break;
  6369. }
  6370. return 0;
  6371. }
  6372. BUILDIN_FUNC(autobonus)
  6373. {
  6374. unsigned int dur;
  6375. short rate;
  6376. short atk_type = 0;
  6377. TBL_PC* sd;
  6378. const char *bonus_script, *other_script = NULL;
  6379. sd = script_rid2sd(st);
  6380. if( sd == NULL )
  6381. return 0; // no player attached
  6382. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6383. return 0;
  6384. rate = script_getnum(st,3);
  6385. dur = script_getnum(st,4);
  6386. bonus_script = script_getstr(st,2);
  6387. if( !rate || !dur || !bonus_script )
  6388. return 0;
  6389. if( script_hasdata(st,5) )
  6390. atk_type = script_getnum(st,5);
  6391. if( script_hasdata(st,6) )
  6392. other_script = script_getstr(st,6);
  6393. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6394. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6395. {
  6396. script_add_autobonus(bonus_script);
  6397. if( other_script )
  6398. script_add_autobonus(other_script);
  6399. }
  6400. return 0;
  6401. }
  6402. BUILDIN_FUNC(autobonus2)
  6403. {
  6404. unsigned int dur;
  6405. short rate;
  6406. short atk_type = 0;
  6407. TBL_PC* sd;
  6408. const char *bonus_script, *other_script = NULL;
  6409. sd = script_rid2sd(st);
  6410. if( sd == NULL )
  6411. return 0; // no player attached
  6412. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6413. return 0;
  6414. rate = script_getnum(st,3);
  6415. dur = script_getnum(st,4);
  6416. bonus_script = script_getstr(st,2);
  6417. if( !rate || !dur || !bonus_script )
  6418. return 0;
  6419. if( script_hasdata(st,5) )
  6420. atk_type = script_getnum(st,5);
  6421. if( script_hasdata(st,6) )
  6422. other_script = script_getstr(st,6);
  6423. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6424. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6425. {
  6426. script_add_autobonus(bonus_script);
  6427. if( other_script )
  6428. script_add_autobonus(other_script);
  6429. }
  6430. return 0;
  6431. }
  6432. BUILDIN_FUNC(autobonus3)
  6433. {
  6434. unsigned int dur;
  6435. short rate,atk_type;
  6436. TBL_PC* sd;
  6437. const char *bonus_script, *other_script = NULL;
  6438. sd = script_rid2sd(st);
  6439. if( sd == NULL )
  6440. return 0; // no player attached
  6441. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6442. return 0;
  6443. rate = script_getnum(st,3);
  6444. dur = script_getnum(st,4);
  6445. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6446. bonus_script = script_getstr(st,2);
  6447. if( !rate || !dur || !atk_type || !bonus_script )
  6448. return 0;
  6449. if( script_hasdata(st,6) )
  6450. other_script = script_getstr(st,6);
  6451. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6452. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6453. {
  6454. script_add_autobonus(bonus_script);
  6455. if( other_script )
  6456. script_add_autobonus(other_script);
  6457. }
  6458. return 0;
  6459. }
  6460. /// Changes the level of a player skill.
  6461. /// <flag> defaults to 1
  6462. /// <flag>=0 : set the level of the skill
  6463. /// <flag>=1 : set the temporary level of the skill
  6464. /// <flag>=2 : add to the level of the skill
  6465. ///
  6466. /// skill <skill id>,<level>,<flag>
  6467. /// skill <skill id>,<level>
  6468. /// skill "<skill name>",<level>,<flag>
  6469. /// skill "<skill name>",<level>
  6470. BUILDIN_FUNC(skill)
  6471. {
  6472. int id;
  6473. int level;
  6474. int flag = 1;
  6475. TBL_PC* sd;
  6476. sd = script_rid2sd(st);
  6477. if( sd == NULL )
  6478. return 0;// no player attached, report source
  6479. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6480. level = script_getnum(st,3);
  6481. if( script_hasdata(st,4) )
  6482. flag = script_getnum(st,4);
  6483. pc_skill(sd, id, level, flag);
  6484. return 0;
  6485. }
  6486. /// Changes the level of a player skill.
  6487. /// like skill, but <flag> defaults to 2
  6488. ///
  6489. /// addtoskill <skill id>,<amount>,<flag>
  6490. /// addtoskill <skill id>,<amount>
  6491. /// addtoskill "<skill name>",<amount>,<flag>
  6492. /// addtoskill "<skill name>",<amount>
  6493. ///
  6494. /// @see skill
  6495. BUILDIN_FUNC(addtoskill)
  6496. {
  6497. int id;
  6498. int level;
  6499. int flag = 2;
  6500. TBL_PC* sd;
  6501. sd = script_rid2sd(st);
  6502. if( sd == NULL )
  6503. return 0;// no player attached, report source
  6504. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6505. level = script_getnum(st,3);
  6506. if( script_hasdata(st,4) )
  6507. flag = script_getnum(st,4);
  6508. pc_skill(sd, id, level, flag);
  6509. return 0;
  6510. }
  6511. /// Increases the level of a guild skill.
  6512. ///
  6513. /// guildskill <skill id>,<amount>;
  6514. /// guildskill "<skill name>",<amount>;
  6515. BUILDIN_FUNC(guildskill)
  6516. {
  6517. int id;
  6518. int level;
  6519. TBL_PC* sd;
  6520. int i;
  6521. sd = script_rid2sd(st);
  6522. if( sd == NULL )
  6523. return 0;// no player attached, report source
  6524. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6525. level = script_getnum(st,3);
  6526. for( i=0; i < level; i++ )
  6527. guild_skillup(sd, id);
  6528. return 0;
  6529. }
  6530. /// Returns the level of the player skill.
  6531. ///
  6532. /// getskilllv(<skill id>) -> <level>
  6533. /// getskilllv("<skill name>") -> <level>
  6534. BUILDIN_FUNC(getskilllv)
  6535. {
  6536. int id;
  6537. TBL_PC* sd;
  6538. sd = script_rid2sd(st);
  6539. if( sd == NULL )
  6540. return 0;// no player attached, report source
  6541. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6542. script_pushint(st, pc_checkskill(sd,id));
  6543. return 0;
  6544. }
  6545. /// Returns the level of the guild skill.
  6546. ///
  6547. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6548. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6549. BUILDIN_FUNC(getgdskilllv)
  6550. {
  6551. int guild_id;
  6552. int skill_id;
  6553. struct guild* g;
  6554. guild_id = script_getnum(st,2);
  6555. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6556. g = guild_search(guild_id);
  6557. if( g == NULL )
  6558. script_pushint(st, -1);
  6559. else
  6560. script_pushint(st, guild_checkskill(g,skill_id));
  6561. return 0;
  6562. }
  6563. /// Returns the 'basic_skill_check' setting.
  6564. /// This config determines if the server checks the skill level of NV_BASIC
  6565. /// before allowing the basic actions.
  6566. ///
  6567. /// basicskillcheck() -> <bool>
  6568. BUILDIN_FUNC(basicskillcheck)
  6569. {
  6570. script_pushint(st, battle_config.basic_skill_check);
  6571. return 0;
  6572. }
  6573. /// Returns the GM level of the player.
  6574. ///
  6575. /// getgmlevel() -> <level>
  6576. BUILDIN_FUNC(getgmlevel)
  6577. {
  6578. TBL_PC* sd;
  6579. sd = script_rid2sd(st);
  6580. if( sd == NULL )
  6581. return 0;// no player attached, report source
  6582. script_pushint(st, pc_get_group_level(sd));
  6583. return 0;
  6584. }
  6585. /// Returns the group ID of the player.
  6586. ///
  6587. /// getgroupid() -> <int>
  6588. BUILDIN_FUNC(getgroupid)
  6589. {
  6590. TBL_PC* sd;
  6591. sd = script_rid2sd(st);
  6592. if (sd == NULL)
  6593. return 1; // no player attached, report source
  6594. script_pushint(st, pc_get_group_id(sd));
  6595. return 0;
  6596. }
  6597. /// Terminates the execution of this script instance.
  6598. ///
  6599. /// end
  6600. BUILDIN_FUNC(end)
  6601. {
  6602. st->state = END;
  6603. return 0;
  6604. }
  6605. /// Checks if the player has that effect state (option).
  6606. ///
  6607. /// checkoption(<option>) -> <bool>
  6608. BUILDIN_FUNC(checkoption)
  6609. {
  6610. int option;
  6611. TBL_PC* sd;
  6612. sd = script_rid2sd(st);
  6613. if( sd == NULL )
  6614. return 0;// no player attached, report source
  6615. option = script_getnum(st,2);
  6616. if( sd->sc.option&option )
  6617. script_pushint(st, 1);
  6618. else
  6619. script_pushint(st, 0);
  6620. return 0;
  6621. }
  6622. /// Checks if the player is in that body state (opt1).
  6623. ///
  6624. /// checkoption1(<opt1>) -> <bool>
  6625. BUILDIN_FUNC(checkoption1)
  6626. {
  6627. int opt1;
  6628. TBL_PC* sd;
  6629. sd = script_rid2sd(st);
  6630. if( sd == NULL )
  6631. return 0;// no player attached, report source
  6632. opt1 = script_getnum(st,2);
  6633. if( sd->sc.opt1 == opt1 )
  6634. script_pushint(st, 1);
  6635. else
  6636. script_pushint(st, 0);
  6637. return 0;
  6638. }
  6639. /// Checks if the player has that health state (opt2).
  6640. ///
  6641. /// checkoption2(<opt2>) -> <bool>
  6642. BUILDIN_FUNC(checkoption2)
  6643. {
  6644. int opt2;
  6645. TBL_PC* sd;
  6646. sd = script_rid2sd(st);
  6647. if( sd == NULL )
  6648. return 0;// no player attached, report source
  6649. opt2 = script_getnum(st,2);
  6650. if( sd->sc.opt2&opt2 )
  6651. script_pushint(st, 1);
  6652. else
  6653. script_pushint(st, 0);
  6654. return 0;
  6655. }
  6656. /// Changes the effect state (option) of the player.
  6657. /// <flag> defaults to 1
  6658. /// <flag>=0 : removes the option
  6659. /// <flag>=other : adds the option
  6660. ///
  6661. /// setoption <option>,<flag>;
  6662. /// setoption <option>;
  6663. BUILDIN_FUNC(setoption)
  6664. {
  6665. int option;
  6666. int flag = 1;
  6667. TBL_PC* sd;
  6668. sd = script_rid2sd(st);
  6669. if( sd == NULL )
  6670. return 0;// no player attached, report source
  6671. option = script_getnum(st,2);
  6672. if( script_hasdata(st,3) )
  6673. flag = script_getnum(st,3);
  6674. else if( !option ){// Request to remove everything.
  6675. flag = 0;
  6676. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6677. }
  6678. if( flag ){// Add option
  6679. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6680. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6681. pc_setoption(sd, sd->sc.option|option);
  6682. } else// Remove option
  6683. pc_setoption(sd, sd->sc.option&~option);
  6684. return 0;
  6685. }
  6686. /// Returns if the player has a cart.
  6687. ///
  6688. /// checkcart() -> <bool>
  6689. ///
  6690. /// @author Valaris
  6691. BUILDIN_FUNC(checkcart)
  6692. {
  6693. TBL_PC* sd;
  6694. sd = script_rid2sd(st);
  6695. if( sd == NULL )
  6696. return 0;// no player attached, report source
  6697. if( pc_iscarton(sd) )
  6698. script_pushint(st, 1);
  6699. else
  6700. script_pushint(st, 0);
  6701. return 0;
  6702. }
  6703. /// Sets the cart of the player.
  6704. /// <type> defaults to 1
  6705. /// <type>=0 : removes the cart
  6706. /// <type>=1 : Normal cart
  6707. /// <type>=2 : Wooden cart
  6708. /// <type>=3 : Covered cart with flowers and ferns
  6709. /// <type>=4 : Wooden cart with a Panda doll on the back
  6710. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6711. ///
  6712. /// setcart <type>;
  6713. /// setcart;
  6714. BUILDIN_FUNC(setcart)
  6715. {
  6716. int type = 1;
  6717. TBL_PC* sd;
  6718. sd = script_rid2sd(st);
  6719. if( sd == NULL )
  6720. return 0;// no player attached, report source
  6721. if( script_hasdata(st,2) )
  6722. type = script_getnum(st,2);
  6723. pc_setcart(sd, type);
  6724. return 0;
  6725. }
  6726. /// Returns if the player has a falcon.
  6727. ///
  6728. /// checkfalcon() -> <bool>
  6729. ///
  6730. /// @author Valaris
  6731. BUILDIN_FUNC(checkfalcon)
  6732. {
  6733. TBL_PC* sd;
  6734. sd = script_rid2sd(st);
  6735. if( sd == NULL )
  6736. return 0;// no player attached, report source
  6737. if( pc_isfalcon(sd) )
  6738. script_pushint(st, 1);
  6739. else
  6740. script_pushint(st, 0);
  6741. return 0;
  6742. }
  6743. /// Sets if the player has a falcon or not.
  6744. /// <flag> defaults to 1
  6745. ///
  6746. /// setfalcon <flag>;
  6747. /// setfalcon;
  6748. BUILDIN_FUNC(setfalcon)
  6749. {
  6750. int flag = 1;
  6751. TBL_PC* sd;
  6752. sd = script_rid2sd(st);
  6753. if( sd == NULL )
  6754. return 0;// no player attached, report source
  6755. if( script_hasdata(st,2) )
  6756. flag = script_getnum(st,2);
  6757. pc_setfalcon(sd, flag);
  6758. return 0;
  6759. }
  6760. /// Returns if the player is riding.
  6761. ///
  6762. /// checkriding() -> <bool>
  6763. ///
  6764. /// @author Valaris
  6765. BUILDIN_FUNC(checkriding)
  6766. {
  6767. TBL_PC* sd;
  6768. sd = script_rid2sd(st);
  6769. if( sd == NULL )
  6770. return 0;// no player attached, report source
  6771. if( pc_isriding(sd) || sd->sc.option&OPTION_MOUNTING )
  6772. script_pushint(st, 1);
  6773. else
  6774. script_pushint(st, 0);
  6775. return 0;
  6776. }
  6777. /// Sets if the player is riding.
  6778. /// <flag> defaults to 1
  6779. ///
  6780. /// setriding <flag>;
  6781. /// setriding;
  6782. BUILDIN_FUNC(setriding)
  6783. {
  6784. int flag = 1;
  6785. TBL_PC* sd;
  6786. sd = script_rid2sd(st);
  6787. if( sd == NULL )
  6788. return 0;// no player attached, report source
  6789. if( script_hasdata(st,2) )
  6790. flag = script_getnum(st,2);
  6791. pc_setriding(sd, flag);
  6792. return 0;
  6793. }
  6794. /// Sets the save point of the player.
  6795. ///
  6796. /// save "<map name>",<x>,<y>
  6797. /// savepoint "<map name>",<x>,<y>
  6798. BUILDIN_FUNC(savepoint)
  6799. {
  6800. int x;
  6801. int y;
  6802. short map;
  6803. const char* str;
  6804. TBL_PC* sd;
  6805. sd = script_rid2sd(st);
  6806. if( sd == NULL )
  6807. return 0;// no player attached, report source
  6808. str = script_getstr(st, 2);
  6809. x = script_getnum(st,3);
  6810. y = script_getnum(st,4);
  6811. map = mapindex_name2id(str);
  6812. if( map )
  6813. pc_setsavepoint(sd, map, x, y);
  6814. return 0;
  6815. }
  6816. /*==========================================
  6817. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6818. *------------------------------------------*/
  6819. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6820. {
  6821. int type;
  6822. time_t timer;
  6823. struct tm *t;
  6824. type=script_getnum(st,2);
  6825. switch(type){
  6826. case 2:
  6827. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6828. // from the system clock.)
  6829. script_pushint(st,(int)time(NULL));
  6830. break;
  6831. case 1:
  6832. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6833. time(&timer);
  6834. t=localtime(&timer);
  6835. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6836. break;
  6837. case 0:
  6838. default:
  6839. //type 0:(System Ticks)
  6840. script_pushint(st,gettick());
  6841. break;
  6842. }
  6843. return 0;
  6844. }
  6845. /*==========================================
  6846. * GetTime(Type);
  6847. * 1: Sec 2: Min 3: Hour
  6848. * 4: WeekDay 5: MonthDay 6: Month
  6849. * 7: Year
  6850. *------------------------------------------*/
  6851. BUILDIN_FUNC(gettime) /* Asgard Version */
  6852. {
  6853. int type;
  6854. time_t timer;
  6855. struct tm *t;
  6856. type=script_getnum(st,2);
  6857. time(&timer);
  6858. t=localtime(&timer);
  6859. switch(type){
  6860. case 1://Sec(0~59)
  6861. script_pushint(st,t->tm_sec);
  6862. break;
  6863. case 2://Min(0~59)
  6864. script_pushint(st,t->tm_min);
  6865. break;
  6866. case 3://Hour(0~23)
  6867. script_pushint(st,t->tm_hour);
  6868. break;
  6869. case 4://WeekDay(0~6)
  6870. script_pushint(st,t->tm_wday);
  6871. break;
  6872. case 5://MonthDay(01~31)
  6873. script_pushint(st,t->tm_mday);
  6874. break;
  6875. case 6://Month(01~12)
  6876. script_pushint(st,t->tm_mon+1);
  6877. break;
  6878. case 7://Year(20xx)
  6879. script_pushint(st,t->tm_year+1900);
  6880. break;
  6881. case 8://Year Day(01~366)
  6882. script_pushint(st,t->tm_yday+1);
  6883. break;
  6884. default://(format error)
  6885. script_pushint(st,-1);
  6886. break;
  6887. }
  6888. return 0;
  6889. }
  6890. /*==========================================
  6891. * GetTimeStr("TimeFMT", Length);
  6892. *------------------------------------------*/
  6893. BUILDIN_FUNC(gettimestr)
  6894. {
  6895. char *tmpstr;
  6896. const char *fmtstr;
  6897. int maxlen;
  6898. time_t now = time(NULL);
  6899. fmtstr=script_getstr(st,2);
  6900. maxlen=script_getnum(st,3);
  6901. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  6902. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6903. tmpstr[maxlen]='\0';
  6904. script_pushstr(st,tmpstr);
  6905. return 0;
  6906. }
  6907. /*==========================================
  6908. * カプラ倉庫を開く
  6909. *------------------------------------------*/
  6910. BUILDIN_FUNC(openstorage)
  6911. {
  6912. TBL_PC* sd;
  6913. sd = script_rid2sd(st);
  6914. if( sd == NULL )
  6915. return 0;
  6916. storage_storageopen(sd);
  6917. return 0;
  6918. }
  6919. BUILDIN_FUNC(guildopenstorage)
  6920. {
  6921. TBL_PC* sd;
  6922. int ret;
  6923. sd = script_rid2sd(st);
  6924. if( sd == NULL )
  6925. return 0;
  6926. ret = storage_guild_storageopen(sd);
  6927. script_pushint(st,ret);
  6928. return 0;
  6929. }
  6930. /*==========================================
  6931. * アイテムによるスキル発動
  6932. *------------------------------------------*/
  6933. /// itemskill <skill id>,<level>
  6934. /// itemskill "<skill name>",<level>
  6935. BUILDIN_FUNC(itemskill)
  6936. {
  6937. int id;
  6938. int lv;
  6939. TBL_PC* sd;
  6940. sd = script_rid2sd(st);
  6941. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6942. return 0;
  6943. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6944. lv = script_getnum(st,3);
  6945. sd->skillitem=id;
  6946. sd->skillitemlv=lv;
  6947. clif_item_skill(sd,id,lv);
  6948. return 0;
  6949. }
  6950. /*==========================================
  6951. * アイテム作成
  6952. *------------------------------------------*/
  6953. BUILDIN_FUNC(produce)
  6954. {
  6955. int trigger;
  6956. TBL_PC* sd;
  6957. sd = script_rid2sd(st);
  6958. if( sd == NULL )
  6959. return 0;
  6960. trigger=script_getnum(st,2);
  6961. clif_skill_produce_mix_list(sd, -1, trigger);
  6962. return 0;
  6963. }
  6964. /*==========================================
  6965. *
  6966. *------------------------------------------*/
  6967. BUILDIN_FUNC(cooking)
  6968. {
  6969. int trigger;
  6970. TBL_PC* sd;
  6971. sd = script_rid2sd(st);
  6972. if( sd == NULL )
  6973. return 0;
  6974. trigger=script_getnum(st,2);
  6975. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  6976. return 0;
  6977. }
  6978. /*==========================================
  6979. * NPCでペット作る
  6980. *------------------------------------------*/
  6981. BUILDIN_FUNC(makepet)
  6982. {
  6983. TBL_PC* sd;
  6984. int id,pet_id;
  6985. id=script_getnum(st,2);
  6986. sd = script_rid2sd(st);
  6987. if( sd == NULL )
  6988. return 0;
  6989. pet_id = search_petDB_index(id, PET_CLASS);
  6990. if (pet_id < 0)
  6991. pet_id = search_petDB_index(id, PET_EGG);
  6992. if (pet_id >= 0 && sd) {
  6993. sd->catch_target_class = pet_db[pet_id].class_;
  6994. intif_create_pet(
  6995. sd->status.account_id, sd->status.char_id,
  6996. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6997. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6998. 100, 0, 1, pet_db[pet_id].jname);
  6999. }
  7000. return 0;
  7001. }
  7002. /*==========================================
  7003. * NPCで経験値上げる
  7004. *------------------------------------------*/
  7005. BUILDIN_FUNC(getexp)
  7006. {
  7007. TBL_PC* sd;
  7008. int base=0,job=0;
  7009. double bonus;
  7010. sd = script_rid2sd(st);
  7011. if( sd == NULL )
  7012. return 0;
  7013. base=script_getnum(st,2);
  7014. job =script_getnum(st,3);
  7015. if(base<0 || job<0)
  7016. return 0;
  7017. // bonus for npc-given exp
  7018. bonus = battle_config.quest_exp_rate / 100.;
  7019. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7020. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7021. pc_gainexp(sd, NULL, base, job, true);
  7022. return 0;
  7023. }
  7024. /*==========================================
  7025. * Gain guild exp [Celest]
  7026. *------------------------------------------*/
  7027. BUILDIN_FUNC(guildgetexp)
  7028. {
  7029. TBL_PC* sd;
  7030. int exp;
  7031. sd = script_rid2sd(st);
  7032. if( sd == NULL )
  7033. return 0;
  7034. exp = script_getnum(st,2);
  7035. if(exp < 0)
  7036. return 0;
  7037. if(sd && sd->status.guild_id > 0)
  7038. guild_getexp (sd, exp);
  7039. return 0;
  7040. }
  7041. /*==========================================
  7042. * Changes the guild master of a guild [Skotlex]
  7043. *------------------------------------------*/
  7044. BUILDIN_FUNC(guildchangegm)
  7045. {
  7046. TBL_PC *sd;
  7047. int guild_id;
  7048. const char *name;
  7049. guild_id = script_getnum(st,2);
  7050. name = script_getstr(st,3);
  7051. sd=map_nick2sd(name);
  7052. if (!sd)
  7053. script_pushint(st,0);
  7054. else
  7055. script_pushint(st,guild_gm_change(guild_id, sd));
  7056. return 0;
  7057. }
  7058. /*==========================================
  7059. * モンスター発生
  7060. *------------------------------------------*/
  7061. BUILDIN_FUNC(monster)
  7062. {
  7063. const char* mapn = script_getstr(st,2);
  7064. int x = script_getnum(st,3);
  7065. int y = script_getnum(st,4);
  7066. const char* str = script_getstr(st,5);
  7067. int class_ = script_getnum(st,6);
  7068. int amount = script_getnum(st,7);
  7069. const char* event = "";
  7070. struct map_session_data* sd;
  7071. int m;
  7072. if( script_hasdata(st,8) )
  7073. {
  7074. event = script_getstr(st,8);
  7075. check_event(st, event);
  7076. }
  7077. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7078. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7079. return 1;
  7080. }
  7081. sd = map_id2sd(st->rid);
  7082. if( sd && strcmp(mapn,"this") == 0 )
  7083. m = sd->bl.m;
  7084. else
  7085. {
  7086. m = map_mapname2mapid(mapn);
  7087. if( map[m].flag.src4instance && st->instance_id )
  7088. { // Try to redirect to the instance map, not the src map
  7089. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7090. {
  7091. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7092. return 1;
  7093. }
  7094. }
  7095. }
  7096. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  7097. return 0;
  7098. }
  7099. /*==========================================
  7100. * Request List of Monster Drops
  7101. *------------------------------------------*/
  7102. BUILDIN_FUNC(getmobdrops)
  7103. {
  7104. int class_ = script_getnum(st,2);
  7105. int i, j = 0;
  7106. struct mob_db *mob;
  7107. if( !mobdb_checkid(class_) )
  7108. {
  7109. script_pushint(st, 0);
  7110. return 0;
  7111. }
  7112. mob = mob_db(class_);
  7113. for( i = 0; i < MAX_MOB_DROP; i++ )
  7114. {
  7115. if( mob->dropitem[i].nameid < 1 )
  7116. continue;
  7117. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7118. continue;
  7119. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7120. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7121. j++;
  7122. }
  7123. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7124. script_pushint(st, 1);
  7125. return 0;
  7126. }
  7127. /*==========================================
  7128. * モンスター発生
  7129. *------------------------------------------*/
  7130. BUILDIN_FUNC(areamonster)
  7131. {
  7132. const char* mapn = script_getstr(st,2);
  7133. int x0 = script_getnum(st,3);
  7134. int y0 = script_getnum(st,4);
  7135. int x1 = script_getnum(st,5);
  7136. int y1 = script_getnum(st,6);
  7137. const char* str = script_getstr(st,7);
  7138. int class_ = script_getnum(st,8);
  7139. int amount = script_getnum(st,9);
  7140. const char* event = "";
  7141. struct map_session_data* sd;
  7142. int m;
  7143. if( script_hasdata(st,10) )
  7144. {
  7145. event = script_getstr(st,10);
  7146. check_event(st, event);
  7147. }
  7148. sd = map_id2sd(st->rid);
  7149. if( sd && strcmp(mapn,"this") == 0 )
  7150. m = sd->bl.m;
  7151. else
  7152. {
  7153. m = map_mapname2mapid(mapn);
  7154. if( map[m].flag.src4instance && st->instance_id )
  7155. { // Try to redirect to the instance map, not the src map
  7156. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7157. {
  7158. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7159. return 1;
  7160. }
  7161. }
  7162. }
  7163. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  7164. return 0;
  7165. }
  7166. /*==========================================
  7167. * モンスター削除
  7168. *------------------------------------------*/
  7169. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7170. { //same fix but with killmonster instead - stripping events from mobs.
  7171. TBL_MOB* md = (TBL_MOB*)bl;
  7172. char *event=va_arg(ap,char *);
  7173. int allflag=va_arg(ap,int);
  7174. md->state.npc_killmonster = 1;
  7175. if(!allflag){
  7176. if(strcmp(event,md->npc_event)==0)
  7177. status_kill(bl);
  7178. }else{
  7179. if(!md->spawn)
  7180. status_kill(bl);
  7181. }
  7182. md->state.npc_killmonster = 0;
  7183. return 0;
  7184. }
  7185. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7186. {
  7187. TBL_MOB* md = (TBL_MOB*)bl;
  7188. char *event=va_arg(ap,char *);
  7189. int allflag=va_arg(ap,int);
  7190. if(!allflag){
  7191. if(strcmp(event,md->npc_event)==0)
  7192. status_kill(bl);
  7193. }else{
  7194. if(!md->spawn)
  7195. status_kill(bl);
  7196. }
  7197. return 0;
  7198. }
  7199. BUILDIN_FUNC(killmonster)
  7200. {
  7201. const char *mapname,*event;
  7202. int m,allflag=0;
  7203. mapname=script_getstr(st,2);
  7204. event=script_getstr(st,3);
  7205. if(strcmp(event,"All")==0)
  7206. allflag = 1;
  7207. else
  7208. check_event(st, event);
  7209. if( (m=map_mapname2mapid(mapname))<0 )
  7210. return 0;
  7211. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7212. return 0;
  7213. if( script_hasdata(st,4) ) {
  7214. if ( script_getnum(st,4) == 1 ) {
  7215. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7216. return 0;
  7217. }
  7218. }
  7219. map_freeblock_lock();
  7220. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7221. map_freeblock_unlock();
  7222. return 0;
  7223. }
  7224. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7225. { //Strips the event from the mob if it's killed the old method.
  7226. struct mob_data *md;
  7227. md = BL_CAST(BL_MOB, bl);
  7228. if (md->npc_event[0])
  7229. md->npc_event[0] = 0;
  7230. status_kill(bl);
  7231. return 0;
  7232. }
  7233. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7234. {
  7235. status_kill(bl);
  7236. return 0;
  7237. }
  7238. BUILDIN_FUNC(killmonsterall)
  7239. {
  7240. const char *mapname;
  7241. int m;
  7242. mapname=script_getstr(st,2);
  7243. if( (m = map_mapname2mapid(mapname))<0 )
  7244. return 0;
  7245. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7246. return 0;
  7247. if( script_hasdata(st,3) ) {
  7248. if ( script_getnum(st,3) == 1 ) {
  7249. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7250. return 0;
  7251. }
  7252. }
  7253. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7254. return 0;
  7255. }
  7256. /*==========================================
  7257. * Creates a clone of a player.
  7258. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7259. *------------------------------------------*/
  7260. BUILDIN_FUNC(clone)
  7261. {
  7262. TBL_PC *sd, *msd=NULL;
  7263. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7264. unsigned int duration = 0;
  7265. const char *map,*event="";
  7266. map=script_getstr(st,2);
  7267. x=script_getnum(st,3);
  7268. y=script_getnum(st,4);
  7269. event=script_getstr(st,5);
  7270. char_id=script_getnum(st,6);
  7271. if( script_hasdata(st,7) )
  7272. master_id=script_getnum(st,7);
  7273. if( script_hasdata(st,8) )
  7274. mode=script_getnum(st,8);
  7275. if( script_hasdata(st,9) )
  7276. flag=script_getnum(st,9);
  7277. if( script_hasdata(st,10) )
  7278. duration=script_getnum(st,10);
  7279. check_event(st, event);
  7280. m = map_mapname2mapid(map);
  7281. if (m < 0) return 0;
  7282. sd = map_charid2sd(char_id);
  7283. if (master_id) {
  7284. msd = map_charid2sd(master_id);
  7285. if (msd)
  7286. master_id = msd->bl.id;
  7287. else
  7288. master_id = 0;
  7289. }
  7290. if (sd) //Return ID of newly crafted clone.
  7291. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7292. else //Failed to create clone.
  7293. script_pushint(st,0);
  7294. return 0;
  7295. }
  7296. /*==========================================
  7297. * イベント実行
  7298. *------------------------------------------*/
  7299. BUILDIN_FUNC(doevent)
  7300. {
  7301. const char* event = script_getstr(st,2);
  7302. struct map_session_data* sd;
  7303. if( ( sd = script_rid2sd(st) ) == NULL )
  7304. {
  7305. return 0;
  7306. }
  7307. check_event(st, event);
  7308. npc_event(sd, event, 0);
  7309. return 0;
  7310. }
  7311. /*==========================================
  7312. * NPC主体イベント実行
  7313. *------------------------------------------*/
  7314. BUILDIN_FUNC(donpcevent)
  7315. {
  7316. const char* event = script_getstr(st,2);
  7317. check_event(st, event);
  7318. npc_event_do(event);
  7319. return 0;
  7320. }
  7321. /// for Aegis compatibility
  7322. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7323. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7324. {
  7325. const char* npc = script_getstr(st,2);
  7326. const char* command = script_getstr(st,3);
  7327. char event[EVENT_NAME_LENGTH];
  7328. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  7329. check_event(st, event);
  7330. npc_event_do(event);
  7331. return 0;
  7332. }
  7333. /*==========================================
  7334. * イベントタイマー追加
  7335. *------------------------------------------*/
  7336. BUILDIN_FUNC(addtimer)
  7337. {
  7338. int tick = script_getnum(st,2);
  7339. const char* event = script_getstr(st, 3);
  7340. TBL_PC* sd;
  7341. check_event(st, event);
  7342. sd = script_rid2sd(st);
  7343. if( sd == NULL )
  7344. return 0;
  7345. pc_addeventtimer(sd,tick,event);
  7346. return 0;
  7347. }
  7348. /*==========================================
  7349. * イベントタイマー削除
  7350. *------------------------------------------*/
  7351. BUILDIN_FUNC(deltimer)
  7352. {
  7353. const char *event;
  7354. TBL_PC* sd;
  7355. event=script_getstr(st, 2);
  7356. sd = script_rid2sd(st);
  7357. if( sd == NULL )
  7358. return 0;
  7359. check_event(st, event);
  7360. pc_deleventtimer(sd,event);
  7361. return 0;
  7362. }
  7363. /*==========================================
  7364. * イベントタイマーのカウント値追加
  7365. *------------------------------------------*/
  7366. BUILDIN_FUNC(addtimercount)
  7367. {
  7368. const char *event;
  7369. int tick;
  7370. TBL_PC* sd;
  7371. event=script_getstr(st, 2);
  7372. tick=script_getnum(st,3);
  7373. sd = script_rid2sd(st);
  7374. if( sd == NULL )
  7375. return 0;
  7376. check_event(st, event);
  7377. pc_addeventtimercount(sd,event,tick);
  7378. return 0;
  7379. }
  7380. /*==========================================
  7381. * NPCタイマー初期化
  7382. *------------------------------------------*/
  7383. BUILDIN_FUNC(initnpctimer)
  7384. {
  7385. struct npc_data *nd;
  7386. int flag = 0;
  7387. if( script_hasdata(st,3) )
  7388. { //Two arguments: NPC name and attach flag.
  7389. nd = npc_name2id(script_getstr(st, 2));
  7390. flag = script_getnum(st,3);
  7391. }
  7392. else if( script_hasdata(st,2) )
  7393. { //Check if argument is numeric (flag) or string (npc name)
  7394. struct script_data *data;
  7395. data = script_getdata(st,2);
  7396. get_val(st,data);
  7397. if( data_isstring(data) ) //NPC name
  7398. nd = npc_name2id(conv_str(st, data));
  7399. else if( data_isint(data) ) //Flag
  7400. {
  7401. nd = (struct npc_data *)map_id2bl(st->oid);
  7402. flag = conv_num(st,data);
  7403. }
  7404. else
  7405. {
  7406. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7407. return 1;
  7408. }
  7409. }
  7410. else
  7411. nd = (struct npc_data *)map_id2bl(st->oid);
  7412. if( !nd )
  7413. return 0;
  7414. if( flag ) //Attach
  7415. {
  7416. TBL_PC* sd = script_rid2sd(st);
  7417. if( sd == NULL )
  7418. return 0;
  7419. nd->u.scr.rid = sd->bl.id;
  7420. }
  7421. npc_settimerevent_tick(nd,0);
  7422. npc_timerevent_start(nd, st->rid);
  7423. return 0;
  7424. }
  7425. /*==========================================
  7426. * NPCタイマー開始
  7427. *------------------------------------------*/
  7428. BUILDIN_FUNC(startnpctimer)
  7429. {
  7430. struct npc_data *nd;
  7431. int flag = 0;
  7432. if( script_hasdata(st,3) )
  7433. { //Two arguments: NPC name and attach flag.
  7434. nd = npc_name2id(script_getstr(st, 2));
  7435. flag = script_getnum(st,3);
  7436. }
  7437. else if( script_hasdata(st,2) )
  7438. { //Check if argument is numeric (flag) or string (npc name)
  7439. struct script_data *data;
  7440. data = script_getdata(st,2);
  7441. get_val(st,data);
  7442. if( data_isstring(data) ) //NPC name
  7443. nd = npc_name2id(conv_str(st, data));
  7444. else if( data_isint(data) ) //Flag
  7445. {
  7446. nd = (struct npc_data *)map_id2bl(st->oid);
  7447. flag = conv_num(st,data);
  7448. }
  7449. else
  7450. {
  7451. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7452. return 1;
  7453. }
  7454. }
  7455. else
  7456. nd=(struct npc_data *)map_id2bl(st->oid);
  7457. if( !nd )
  7458. return 0;
  7459. if( flag ) //Attach
  7460. {
  7461. TBL_PC* sd = script_rid2sd(st);
  7462. if( sd == NULL )
  7463. return 0;
  7464. nd->u.scr.rid = sd->bl.id;
  7465. }
  7466. npc_timerevent_start(nd, st->rid);
  7467. return 0;
  7468. }
  7469. /*==========================================
  7470. * NPCタイマー停止
  7471. *------------------------------------------*/
  7472. BUILDIN_FUNC(stopnpctimer)
  7473. {
  7474. struct npc_data *nd;
  7475. int flag = 0;
  7476. if( script_hasdata(st,3) )
  7477. { //Two arguments: NPC name and attach flag.
  7478. nd = npc_name2id(script_getstr(st, 2));
  7479. flag = script_getnum(st,3);
  7480. }
  7481. else if( script_hasdata(st,2) )
  7482. { //Check if argument is numeric (flag) or string (npc name)
  7483. struct script_data *data;
  7484. data = script_getdata(st,2);
  7485. get_val(st,data);
  7486. if( data_isstring(data) ) //NPC name
  7487. nd = npc_name2id(conv_str(st, data));
  7488. else if( data_isint(data) ) //Flag
  7489. {
  7490. nd = (struct npc_data *)map_id2bl(st->oid);
  7491. flag = conv_num(st,data);
  7492. }
  7493. else
  7494. {
  7495. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7496. return 1;
  7497. }
  7498. }
  7499. else
  7500. nd=(struct npc_data *)map_id2bl(st->oid);
  7501. if( !nd )
  7502. return 0;
  7503. if( flag ) //Detach
  7504. nd->u.scr.rid = 0;
  7505. npc_timerevent_stop(nd);
  7506. return 0;
  7507. }
  7508. /*==========================================
  7509. * NPCタイマー情報所得
  7510. *------------------------------------------*/
  7511. BUILDIN_FUNC(getnpctimer)
  7512. {
  7513. struct npc_data *nd;
  7514. TBL_PC *sd;
  7515. int type = script_getnum(st,2);
  7516. int val = 0;
  7517. if( script_hasdata(st,3) )
  7518. nd = npc_name2id(script_getstr(st,3));
  7519. else
  7520. nd = (struct npc_data *)map_id2bl(st->oid);
  7521. if( !nd || nd->bl.type != BL_NPC )
  7522. {
  7523. script_pushint(st,0);
  7524. ShowError("getnpctimer: Invalid NPC.\n");
  7525. return 1;
  7526. }
  7527. switch( type )
  7528. {
  7529. case 0: val = npc_gettimerevent_tick(nd); break;
  7530. case 1:
  7531. if( nd->u.scr.rid )
  7532. {
  7533. sd = map_id2sd(nd->u.scr.rid);
  7534. if( !sd )
  7535. {
  7536. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7537. break;
  7538. }
  7539. val = (sd->npc_timer_id != INVALID_TIMER);
  7540. }
  7541. else
  7542. val = (nd->u.scr.timerid != INVALID_TIMER);
  7543. break;
  7544. case 2: val = nd->u.scr.timeramount; break;
  7545. }
  7546. script_pushint(st,val);
  7547. return 0;
  7548. }
  7549. /*==========================================
  7550. * NPCタイマー値設定
  7551. *------------------------------------------*/
  7552. BUILDIN_FUNC(setnpctimer)
  7553. {
  7554. int tick;
  7555. struct npc_data *nd;
  7556. tick = script_getnum(st,2);
  7557. if( script_hasdata(st,3) )
  7558. nd = npc_name2id(script_getstr(st,3));
  7559. else
  7560. nd = (struct npc_data *)map_id2bl(st->oid);
  7561. if( !nd || nd->bl.type != BL_NPC )
  7562. {
  7563. script_pushint(st,1);
  7564. ShowError("setnpctimer: Invalid NPC.\n");
  7565. return 1;
  7566. }
  7567. npc_settimerevent_tick(nd,tick);
  7568. script_pushint(st,0);
  7569. return 0;
  7570. }
  7571. /*==========================================
  7572. * attaches the player rid to the timer [Celest]
  7573. *------------------------------------------*/
  7574. BUILDIN_FUNC(attachnpctimer)
  7575. {
  7576. TBL_PC *sd;
  7577. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7578. if( !nd || nd->bl.type != BL_NPC )
  7579. {
  7580. script_pushint(st,1);
  7581. ShowError("setnpctimer: Invalid NPC.\n");
  7582. return 1;
  7583. }
  7584. if( script_hasdata(st,2) )
  7585. sd = map_nick2sd(script_getstr(st,2));
  7586. else
  7587. sd = script_rid2sd(st);
  7588. if( !sd )
  7589. {
  7590. script_pushint(st,1);
  7591. ShowWarning("attachnpctimer: Invalid player.\n");
  7592. return 1;
  7593. }
  7594. nd->u.scr.rid = sd->bl.id;
  7595. script_pushint(st,0);
  7596. return 0;
  7597. }
  7598. /*==========================================
  7599. * detaches a player rid from the timer [Celest]
  7600. *------------------------------------------*/
  7601. BUILDIN_FUNC(detachnpctimer)
  7602. {
  7603. struct npc_data *nd;
  7604. if( script_hasdata(st,2) )
  7605. nd = npc_name2id(script_getstr(st,2));
  7606. else
  7607. nd = (struct npc_data *)map_id2bl(st->oid);
  7608. if( !nd || nd->bl.type != BL_NPC )
  7609. {
  7610. script_pushint(st,1);
  7611. ShowError("detachnpctimer: Invalid NPC.\n");
  7612. return 1;
  7613. }
  7614. nd->u.scr.rid = 0;
  7615. script_pushint(st,0);
  7616. return 0;
  7617. }
  7618. /*==========================================
  7619. * To avoid "player not attached" script errors, this function is provided,
  7620. * it checks if there is a player attached to the current script. [Skotlex]
  7621. * If no, returns 0, if yes, returns the account_id of the attached player.
  7622. *------------------------------------------*/
  7623. BUILDIN_FUNC(playerattached)
  7624. {
  7625. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7626. script_pushint(st,0);
  7627. else
  7628. script_pushint(st,st->rid);
  7629. return 0;
  7630. }
  7631. /*==========================================
  7632. * 天の声アナウンス
  7633. *------------------------------------------*/
  7634. BUILDIN_FUNC(announce)
  7635. {
  7636. const char *mes = script_getstr(st,2);
  7637. int flag = script_getnum(st,3);
  7638. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7639. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7640. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7641. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7642. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7643. if (flag&0x0f) // Broadcast source or broadcast region defined
  7644. {
  7645. send_target target;
  7646. 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
  7647. if (bl == NULL)
  7648. return 0;
  7649. flag &= 0x07;
  7650. target = (flag == 1) ? ALL_SAMEMAP :
  7651. (flag == 2) ? AREA :
  7652. (flag == 3) ? SELF :
  7653. ALL_CLIENT;
  7654. if (fontColor)
  7655. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7656. else
  7657. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7658. }
  7659. else
  7660. {
  7661. if (fontColor)
  7662. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7663. else
  7664. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7665. }
  7666. return 0;
  7667. }
  7668. /*==========================================
  7669. * 天の声アナウンス(特定マップ)
  7670. *------------------------------------------*/
  7671. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7672. {
  7673. char *mes = va_arg(ap, char *);
  7674. int len = va_arg(ap, int);
  7675. int type = va_arg(ap, int);
  7676. char *fontColor = va_arg(ap, char *);
  7677. short fontType = (short)va_arg(ap, int);
  7678. short fontSize = (short)va_arg(ap, int);
  7679. short fontAlign = (short)va_arg(ap, int);
  7680. short fontY = (short)va_arg(ap, int);
  7681. if (fontColor)
  7682. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7683. else
  7684. clif_broadcast(bl, mes, len, type, SELF);
  7685. return 0;
  7686. }
  7687. BUILDIN_FUNC(mapannounce)
  7688. {
  7689. const char *mapname = script_getstr(st,2);
  7690. const char *mes = script_getstr(st,3);
  7691. int flag = script_getnum(st,4);
  7692. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7693. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7694. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7695. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7696. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7697. int m;
  7698. if ((m = map_mapname2mapid(mapname)) < 0)
  7699. return 0;
  7700. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7701. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7702. return 0;
  7703. }
  7704. /*==========================================
  7705. * 天の声アナウンス(特定エリア)
  7706. *------------------------------------------*/
  7707. BUILDIN_FUNC(areaannounce)
  7708. {
  7709. const char *mapname = script_getstr(st,2);
  7710. int x0 = script_getnum(st,3);
  7711. int y0 = script_getnum(st,4);
  7712. int x1 = script_getnum(st,5);
  7713. int y1 = script_getnum(st,6);
  7714. const char *mes = script_getstr(st,7);
  7715. int flag = script_getnum(st,8);
  7716. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7717. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7718. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7719. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7720. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7721. int m;
  7722. if ((m = map_mapname2mapid(mapname)) < 0)
  7723. return 0;
  7724. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7725. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7726. return 0;
  7727. }
  7728. /*==========================================
  7729. * ユーザー数所得
  7730. *------------------------------------------*/
  7731. BUILDIN_FUNC(getusers)
  7732. {
  7733. int flag, val = 0;
  7734. struct map_session_data* sd;
  7735. struct block_list* bl = NULL;
  7736. flag = script_getnum(st,2);
  7737. switch(flag&0x07)
  7738. {
  7739. case 0:
  7740. if(flag&0x8)
  7741. {// npc
  7742. bl = map_id2bl(st->oid);
  7743. }
  7744. else if((sd = script_rid2sd(st))!=NULL)
  7745. {// pc
  7746. bl = &sd->bl;
  7747. }
  7748. if(bl)
  7749. {
  7750. val = map[bl->m].users;
  7751. }
  7752. break;
  7753. case 1:
  7754. val = map_getusers();
  7755. break;
  7756. default:
  7757. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  7758. script_pushint(st,0);
  7759. return 1;
  7760. }
  7761. script_pushint(st,val);
  7762. return 0;
  7763. }
  7764. /*==========================================
  7765. * Works like @WHO - displays all online users names in window
  7766. *------------------------------------------*/
  7767. BUILDIN_FUNC(getusersname)
  7768. {
  7769. TBL_PC *sd, *pl_sd;
  7770. int disp_num=1, group_level = 0;
  7771. struct s_mapiterator* iter;
  7772. sd = script_rid2sd(st);
  7773. if (!sd) return 0;
  7774. group_level = pc_get_group_level(sd);
  7775. iter = mapit_getallusers();
  7776. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7777. {
  7778. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  7779. continue; // skip hidden sessions
  7780. if((disp_num++)%10==0)
  7781. clif_scriptnext(sd,st->oid);
  7782. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7783. }
  7784. mapit_free(iter);
  7785. return 0;
  7786. }
  7787. /*==========================================
  7788. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7789. *------------------------------------------*/
  7790. BUILDIN_FUNC(getmapguildusers)
  7791. {
  7792. const char *str;
  7793. int m, gid;
  7794. int i=0,c=0;
  7795. struct guild *g = NULL;
  7796. str=script_getstr(st,2);
  7797. gid=script_getnum(st,3);
  7798. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7799. script_pushint(st,-1);
  7800. return 0;
  7801. }
  7802. g = guild_search(gid);
  7803. if (g){
  7804. for(i = 0; i < g->max_member; i++)
  7805. {
  7806. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7807. c++;
  7808. }
  7809. }
  7810. script_pushint(st,c);
  7811. return 0;
  7812. }
  7813. /*==========================================
  7814. * マップ指定ユーザー数所得
  7815. *------------------------------------------*/
  7816. BUILDIN_FUNC(getmapusers)
  7817. {
  7818. const char *str;
  7819. int m;
  7820. str=script_getstr(st,2);
  7821. if( (m=map_mapname2mapid(str))< 0){
  7822. script_pushint(st,-1);
  7823. return 0;
  7824. }
  7825. script_pushint(st,map[m].users);
  7826. return 0;
  7827. }
  7828. /*==========================================
  7829. * エリア指定ユーザー数所得
  7830. *------------------------------------------*/
  7831. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7832. {
  7833. int *users=va_arg(ap,int *);
  7834. (*users)++;
  7835. return 0;
  7836. }
  7837. BUILDIN_FUNC(getareausers)
  7838. {
  7839. const char *str;
  7840. int m,x0,y0,x1,y1,users=0;
  7841. str=script_getstr(st,2);
  7842. x0=script_getnum(st,3);
  7843. y0=script_getnum(st,4);
  7844. x1=script_getnum(st,5);
  7845. y1=script_getnum(st,6);
  7846. if( (m=map_mapname2mapid(str))< 0){
  7847. script_pushint(st,-1);
  7848. return 0;
  7849. }
  7850. map_foreachinarea(buildin_getareausers_sub,
  7851. m,x0,y0,x1,y1,BL_PC,&users);
  7852. script_pushint(st,users);
  7853. return 0;
  7854. }
  7855. /*==========================================
  7856. * エリア指定ドロップアイテム数所得
  7857. *------------------------------------------*/
  7858. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7859. {
  7860. int item=va_arg(ap,int);
  7861. int *amount=va_arg(ap,int *);
  7862. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7863. if(drop->item_data.nameid==item)
  7864. (*amount)+=drop->item_data.amount;
  7865. return 0;
  7866. }
  7867. BUILDIN_FUNC(getareadropitem)
  7868. {
  7869. const char *str;
  7870. int m,x0,y0,x1,y1,item,amount=0;
  7871. struct script_data *data;
  7872. str=script_getstr(st,2);
  7873. x0=script_getnum(st,3);
  7874. y0=script_getnum(st,4);
  7875. x1=script_getnum(st,5);
  7876. y1=script_getnum(st,6);
  7877. data=script_getdata(st,7);
  7878. get_val(st,data);
  7879. if( data_isstring(data) ){
  7880. const char *name=conv_str(st,data);
  7881. struct item_data *item_data = itemdb_searchname(name);
  7882. item=UNKNOWN_ITEM_ID;
  7883. if( item_data )
  7884. item=item_data->nameid;
  7885. }else
  7886. item=conv_num(st,data);
  7887. if( (m=map_mapname2mapid(str))< 0){
  7888. script_pushint(st,-1);
  7889. return 0;
  7890. }
  7891. map_foreachinarea(buildin_getareadropitem_sub,
  7892. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7893. script_pushint(st,amount);
  7894. return 0;
  7895. }
  7896. /*==========================================
  7897. * NPCの有効化
  7898. *------------------------------------------*/
  7899. BUILDIN_FUNC(enablenpc)
  7900. {
  7901. const char *str;
  7902. str=script_getstr(st,2);
  7903. npc_enable(str,1);
  7904. return 0;
  7905. }
  7906. /*==========================================
  7907. * NPCの無効化
  7908. *------------------------------------------*/
  7909. BUILDIN_FUNC(disablenpc)
  7910. {
  7911. const char *str;
  7912. str=script_getstr(st,2);
  7913. npc_enable(str,0);
  7914. return 0;
  7915. }
  7916. /*==========================================
  7917. * 隠れているNPCの表示
  7918. *------------------------------------------*/
  7919. BUILDIN_FUNC(hideoffnpc)
  7920. {
  7921. const char *str;
  7922. str=script_getstr(st,2);
  7923. npc_enable(str,2);
  7924. return 0;
  7925. }
  7926. /*==========================================
  7927. * NPCをハイディング
  7928. *------------------------------------------*/
  7929. BUILDIN_FUNC(hideonnpc)
  7930. {
  7931. const char *str;
  7932. str=script_getstr(st,2);
  7933. npc_enable(str,4);
  7934. return 0;
  7935. }
  7936. /// Starts a status effect on the target unit or on the attached player.
  7937. ///
  7938. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7939. BUILDIN_FUNC(sc_start)
  7940. {
  7941. struct block_list* bl;
  7942. enum sc_type type;
  7943. int tick;
  7944. int val1;
  7945. int val4 = 0;
  7946. type = (sc_type)script_getnum(st,2);
  7947. tick = script_getnum(st,3);
  7948. val1 = script_getnum(st,4);
  7949. if( script_hasdata(st,5) )
  7950. bl = map_id2bl(script_getnum(st,5));
  7951. else
  7952. bl = map_id2bl(st->rid);
  7953. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7954. {// When there isn't a duration specified, try to get it from the skill_db
  7955. tick = skill_get_time(status_sc2skill(type), val1);
  7956. }
  7957. if( potion_flag == 1 && potion_target )
  7958. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7959. bl = map_id2bl(potion_target);
  7960. tick /= 2;// Thrown potions only last half.
  7961. val4 = 1;// Mark that this was a thrown sc_effect
  7962. }
  7963. if( bl )
  7964. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7965. return 0;
  7966. }
  7967. /// Starts a status effect on the target unit or on the attached player.
  7968. ///
  7969. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7970. BUILDIN_FUNC(sc_start2)
  7971. {
  7972. struct block_list* bl;
  7973. enum sc_type type;
  7974. int tick;
  7975. int val1;
  7976. int val4 = 0;
  7977. int rate;
  7978. type = (sc_type)script_getnum(st,2);
  7979. tick = script_getnum(st,3);
  7980. val1 = script_getnum(st,4);
  7981. rate = script_getnum(st,5);
  7982. if( script_hasdata(st,6) )
  7983. bl = map_id2bl(script_getnum(st,6));
  7984. else
  7985. bl = map_id2bl(st->rid);
  7986. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7987. {// When there isn't a duration specified, try to get it from the skill_db
  7988. tick = skill_get_time(status_sc2skill(type), val1);
  7989. }
  7990. if( potion_flag == 1 && potion_target )
  7991. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7992. bl = map_id2bl(potion_target);
  7993. tick /= 2;// Thrown potions only last half.
  7994. val4 = 1;// Mark that this was a thrown sc_effect
  7995. }
  7996. if( bl )
  7997. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7998. return 0;
  7999. }
  8000. /// Starts a status effect on the target unit or on the attached player.
  8001. ///
  8002. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  8003. BUILDIN_FUNC(sc_start4)
  8004. {
  8005. struct block_list* bl;
  8006. enum sc_type type;
  8007. int tick;
  8008. int val1;
  8009. int val2;
  8010. int val3;
  8011. int val4;
  8012. type = (sc_type)script_getnum(st,2);
  8013. tick = script_getnum(st,3);
  8014. val1 = script_getnum(st,4);
  8015. val2 = script_getnum(st,5);
  8016. val3 = script_getnum(st,6);
  8017. val4 = script_getnum(st,7);
  8018. if( script_hasdata(st,8) )
  8019. bl = map_id2bl(script_getnum(st,8));
  8020. else
  8021. bl = map_id2bl(st->rid);
  8022. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8023. {// When there isn't a duration specified, try to get it from the skill_db
  8024. tick = skill_get_time(status_sc2skill(type), val1);
  8025. }
  8026. if( potion_flag == 1 && potion_target )
  8027. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8028. bl = map_id2bl(potion_target);
  8029. tick /= 2;// Thrown potions only last half.
  8030. }
  8031. if( bl )
  8032. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  8033. return 0;
  8034. }
  8035. /// Ends one or all status effects on the target unit or on the attached player.
  8036. ///
  8037. /// sc_end <effect_id>{,<unit_id>};
  8038. BUILDIN_FUNC(sc_end)
  8039. {
  8040. struct block_list* bl;
  8041. int type;
  8042. type = script_getnum(st,2);
  8043. if( script_hasdata(st,3) )
  8044. bl = map_id2bl(script_getnum(st,3));
  8045. else
  8046. bl = map_id2bl(st->rid);
  8047. if( potion_flag==1 && potion_target )
  8048. {//##TODO how does this work [FlavioJS]
  8049. bl = map_id2bl(potion_target);
  8050. }
  8051. if( !bl ) return 0;
  8052. if( type >= 0 && type < SC_MAX )
  8053. {
  8054. struct status_change *sc = status_get_sc(bl);
  8055. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  8056. if (!sce) return 0;
  8057. //This should help status_change_end force disabling the SC in case it has no limit.
  8058. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8059. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8060. } else
  8061. status_change_clear(bl, 2);// remove all effects
  8062. return 0;
  8063. }
  8064. /*==========================================
  8065. * 状態異常耐性を計算した確率を返す
  8066. *------------------------------------------*/
  8067. BUILDIN_FUNC(getscrate)
  8068. {
  8069. struct block_list *bl;
  8070. int type,rate;
  8071. type=script_getnum(st,2);
  8072. rate=script_getnum(st,3);
  8073. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  8074. bl = map_id2bl(script_getnum(st,4));
  8075. else
  8076. bl = map_id2bl(st->rid);
  8077. if (bl)
  8078. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  8079. script_pushint(st,rate);
  8080. return 0;
  8081. }
  8082. /*==========================================
  8083. * getstatus <type>{, <info>};
  8084. *------------------------------------------*/
  8085. BUILDIN_FUNC(getstatus)
  8086. {
  8087. int id, type;
  8088. struct map_session_data* sd = script_rid2sd(st);
  8089. if( sd == NULL )
  8090. {// no player attached
  8091. return 0;
  8092. }
  8093. id = script_getnum(st, 2);
  8094. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8095. if( id <= SC_NONE || id >= SC_MAX )
  8096. {// invalid status type given
  8097. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8098. return 0;
  8099. }
  8100. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8101. {// no status is active
  8102. script_pushint(st, 0);
  8103. return 0;
  8104. }
  8105. switch( type )
  8106. {
  8107. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8108. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8109. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8110. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8111. case 5:
  8112. {
  8113. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8114. if( timer )
  8115. {// return the amount of time remaining
  8116. script_pushint(st, timer->tick - gettick());
  8117. }
  8118. }
  8119. break;
  8120. default: script_pushint(st, 1); break;
  8121. }
  8122. return 0;
  8123. }
  8124. /*==========================================
  8125. *
  8126. *------------------------------------------*/
  8127. BUILDIN_FUNC(debugmes)
  8128. {
  8129. const char *str;
  8130. str=script_getstr(st,2);
  8131. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8132. return 0;
  8133. }
  8134. /*==========================================
  8135. *捕獲アイテム使用
  8136. *------------------------------------------*/
  8137. BUILDIN_FUNC(catchpet)
  8138. {
  8139. int pet_id;
  8140. TBL_PC *sd;
  8141. pet_id= script_getnum(st,2);
  8142. sd=script_rid2sd(st);
  8143. if( sd == NULL )
  8144. return 0;
  8145. pet_catch_process1(sd,pet_id);
  8146. return 0;
  8147. }
  8148. /*==========================================
  8149. * [orn]
  8150. *------------------------------------------*/
  8151. BUILDIN_FUNC(homunculus_evolution)
  8152. {
  8153. TBL_PC *sd;
  8154. sd=script_rid2sd(st);
  8155. if( sd == NULL )
  8156. return 0;
  8157. if(merc_is_hom_active(sd->hd))
  8158. {
  8159. if (sd->hd->homunculus.intimacy > 91000)
  8160. merc_hom_evolution(sd->hd);
  8161. else
  8162. clif_emotion(&sd->hd->bl, E_SWT);
  8163. }
  8164. return 0;
  8165. }
  8166. // [Zephyrus]
  8167. BUILDIN_FUNC(homunculus_shuffle)
  8168. {
  8169. TBL_PC *sd;
  8170. sd=script_rid2sd(st);
  8171. if( sd == NULL )
  8172. return 0;
  8173. if(merc_is_hom_active(sd->hd))
  8174. merc_hom_shuffle(sd->hd);
  8175. return 0;
  8176. }
  8177. //These two functions bring the eA MAPID_* class functionality to scripts.
  8178. BUILDIN_FUNC(eaclass)
  8179. {
  8180. int class_;
  8181. if( script_hasdata(st,2) )
  8182. class_ = script_getnum(st,2);
  8183. else {
  8184. TBL_PC *sd;
  8185. sd=script_rid2sd(st);
  8186. if (!sd) {
  8187. script_pushint(st,-1);
  8188. return 0;
  8189. }
  8190. class_ = sd->status.class_;
  8191. }
  8192. script_pushint(st,pc_jobid2mapid(class_));
  8193. return 0;
  8194. }
  8195. BUILDIN_FUNC(roclass)
  8196. {
  8197. int class_ =script_getnum(st,2);
  8198. int sex;
  8199. if( script_hasdata(st,3) )
  8200. sex = script_getnum(st,3);
  8201. else {
  8202. TBL_PC *sd;
  8203. if (st->rid && (sd=script_rid2sd(st)))
  8204. sex = sd->status.sex;
  8205. else
  8206. sex = 1; //Just use male when not found.
  8207. }
  8208. script_pushint(st,pc_mapid2jobid(class_, sex));
  8209. return 0;
  8210. }
  8211. /*==========================================
  8212. * Tells client to open a hatching window, used for pet incubator
  8213. *------------------------------------------*/
  8214. BUILDIN_FUNC(birthpet)
  8215. {
  8216. TBL_PC *sd;
  8217. sd=script_rid2sd(st);
  8218. if( sd == NULL )
  8219. return 0;
  8220. if( sd->status.pet_id )
  8221. {// do not send egg list, when you already have a pet
  8222. return 0;
  8223. }
  8224. clif_sendegg(sd);
  8225. return 0;
  8226. }
  8227. /*==========================================
  8228. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8229. *------------------------------------------*/
  8230. BUILDIN_FUNC(resetlvl)
  8231. {
  8232. TBL_PC *sd;
  8233. int type=script_getnum(st,2);
  8234. sd=script_rid2sd(st);
  8235. if( sd == NULL )
  8236. return 0;
  8237. pc_resetlvl(sd,type);
  8238. return 0;
  8239. }
  8240. /*==========================================
  8241. * ステータスリセット
  8242. *------------------------------------------*/
  8243. BUILDIN_FUNC(resetstatus)
  8244. {
  8245. TBL_PC *sd;
  8246. sd=script_rid2sd(st);
  8247. pc_resetstate(sd);
  8248. return 0;
  8249. }
  8250. /*==========================================
  8251. * script command resetskill
  8252. *------------------------------------------*/
  8253. BUILDIN_FUNC(resetskill)
  8254. {
  8255. TBL_PC *sd;
  8256. sd=script_rid2sd(st);
  8257. pc_resetskill(sd,1);
  8258. return 0;
  8259. }
  8260. /*==========================================
  8261. * Counts total amount of skill points.
  8262. *------------------------------------------*/
  8263. BUILDIN_FUNC(skillpointcount)
  8264. {
  8265. TBL_PC *sd;
  8266. sd=script_rid2sd(st);
  8267. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8268. return 0;
  8269. }
  8270. /*==========================================
  8271. *
  8272. *------------------------------------------*/
  8273. BUILDIN_FUNC(changebase)
  8274. {
  8275. TBL_PC *sd=NULL;
  8276. int vclass;
  8277. if( script_hasdata(st,3) )
  8278. sd=map_id2sd(script_getnum(st,3));
  8279. else
  8280. sd=script_rid2sd(st);
  8281. if(sd == NULL)
  8282. return 0;
  8283. vclass = script_getnum(st,2);
  8284. if(vclass == JOB_WEDDING)
  8285. {
  8286. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8287. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8288. )
  8289. return 0;
  8290. }
  8291. if(!sd->disguise && vclass != sd->vd.class_) {
  8292. status_set_viewdata(&sd->bl, vclass);
  8293. //Updated client view. Base, Weapon and Cloth Colors.
  8294. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8295. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8296. if (sd->vd.cloth_color)
  8297. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8298. clif_skillinfoblock(sd);
  8299. }
  8300. return 0;
  8301. }
  8302. /*==========================================
  8303. * 性別変換
  8304. *------------------------------------------*/
  8305. BUILDIN_FUNC(changesex)
  8306. {
  8307. TBL_PC *sd = NULL;
  8308. sd = script_rid2sd(st);
  8309. pc_resetskill(sd,4);
  8310. chrif_changesex(sd);
  8311. return 0;
  8312. }
  8313. /*==========================================
  8314. * Works like 'announce' but outputs in the common chat window
  8315. *------------------------------------------*/
  8316. BUILDIN_FUNC(globalmes)
  8317. {
  8318. struct block_list *bl = map_id2bl(st->oid);
  8319. struct npc_data *nd = (struct npc_data *)bl;
  8320. const char *name=NULL,*mes;
  8321. mes=script_getstr(st,2); // メッセージの取得
  8322. if(mes==NULL) return 0;
  8323. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8324. name=script_getstr(st,3);
  8325. } else {
  8326. name=nd->name;
  8327. }
  8328. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8329. return 0;
  8330. }
  8331. /////////////////////////////////////////////////////////////////////
  8332. // NPC waiting room (chat room)
  8333. //
  8334. /// Creates a waiting room (chat room) for this npc.
  8335. ///
  8336. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  8337. BUILDIN_FUNC(waitingroom)
  8338. {
  8339. struct npc_data* nd;
  8340. int pub = 1;
  8341. const char* title = script_getstr(st, 2);
  8342. int limit = script_getnum(st, 3);
  8343. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  8344. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  8345. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  8346. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  8347. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  8348. nd = (struct npc_data *)map_id2bl(st->oid);
  8349. if( nd != NULL )
  8350. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  8351. return 0;
  8352. }
  8353. /// Removes the waiting room of the current or target npc.
  8354. ///
  8355. /// delwaitingroom "<npc_name>";
  8356. /// delwaitingroom;
  8357. BUILDIN_FUNC(delwaitingroom)
  8358. {
  8359. struct npc_data* nd;
  8360. if( script_hasdata(st,2) )
  8361. nd = npc_name2id(script_getstr(st, 2));
  8362. else
  8363. nd = (struct npc_data *)map_id2bl(st->oid);
  8364. if( nd != NULL )
  8365. chat_deletenpcchat(nd);
  8366. return 0;
  8367. }
  8368. /// Kicks all the players from the waiting room of the current or target npc.
  8369. ///
  8370. /// kickwaitingroomall "<npc_name>";
  8371. /// kickwaitingroomall;
  8372. BUILDIN_FUNC(waitingroomkickall)
  8373. {
  8374. struct npc_data* nd;
  8375. struct chat_data* cd;
  8376. if( script_hasdata(st,2) )
  8377. nd = npc_name2id(script_getstr(st,2));
  8378. else
  8379. nd = (struct npc_data *)map_id2bl(st->oid);
  8380. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8381. chat_npckickall(cd);
  8382. return 0;
  8383. }
  8384. /// Enables the waiting room event of the current or target npc.
  8385. ///
  8386. /// enablewaitingroomevent "<npc_name>";
  8387. /// enablewaitingroomevent;
  8388. BUILDIN_FUNC(enablewaitingroomevent)
  8389. {
  8390. struct npc_data* nd;
  8391. struct chat_data* cd;
  8392. if( script_hasdata(st,2) )
  8393. nd = npc_name2id(script_getstr(st, 2));
  8394. else
  8395. nd = (struct npc_data *)map_id2bl(st->oid);
  8396. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8397. chat_enableevent(cd);
  8398. return 0;
  8399. }
  8400. /// Disables the waiting room event of the current or target npc.
  8401. ///
  8402. /// disablewaitingroomevent "<npc_name>";
  8403. /// disablewaitingroomevent;
  8404. BUILDIN_FUNC(disablewaitingroomevent)
  8405. {
  8406. struct npc_data *nd;
  8407. struct chat_data *cd;
  8408. if( script_hasdata(st,2) )
  8409. nd = npc_name2id(script_getstr(st, 2));
  8410. else
  8411. nd = (struct npc_data *)map_id2bl(st->oid);
  8412. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8413. chat_disableevent(cd);
  8414. return 0;
  8415. }
  8416. /// Returns info on the waiting room of the current or target npc.
  8417. /// Returns -1 if the type unknown
  8418. /// <type>=0 : current number of users
  8419. /// <type>=1 : maximum number of users allowed
  8420. /// <type>=2 : the number of users that trigger the event
  8421. /// <type>=3 : if the trigger is disabled
  8422. /// <type>=4 : the title of the waiting room
  8423. /// <type>=5 : the password of the waiting room
  8424. /// <type>=16 : the name of the waiting room event
  8425. /// <type>=32 : if the waiting room is full
  8426. /// <type>=33 : if there are enough users to trigger the event
  8427. ///
  8428. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8429. /// getwaitingroomstate(<type>) -> <info>
  8430. BUILDIN_FUNC(getwaitingroomstate)
  8431. {
  8432. struct npc_data *nd;
  8433. struct chat_data *cd;
  8434. int type;
  8435. type = script_getnum(st,2);
  8436. if( script_hasdata(st,3) )
  8437. nd = npc_name2id(script_getstr(st, 3));
  8438. else
  8439. nd = (struct npc_data *)map_id2bl(st->oid);
  8440. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8441. {
  8442. script_pushint(st, -1);
  8443. return 0;
  8444. }
  8445. switch(type)
  8446. {
  8447. case 0: script_pushint(st, cd->users); break;
  8448. case 1: script_pushint(st, cd->limit); break;
  8449. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8450. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8451. case 4: script_pushstrcopy(st, cd->title); break;
  8452. case 5: script_pushstrcopy(st, cd->pass); break;
  8453. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8454. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8455. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8456. default: script_pushint(st, -1); break;
  8457. }
  8458. return 0;
  8459. }
  8460. /// Warps the trigger or target amount of players to the target map and position.
  8461. /// Players are automatically removed from the waiting room.
  8462. /// Those waiting the longest will get warped first.
  8463. /// The target map can be "Random" for a random position in the current map,
  8464. /// and "SavePoint" for the savepoint map+position.
  8465. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8466. ///
  8467. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8468. /// The total number of teleported players is put into $@warpwaitingpcnum
  8469. ///
  8470. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8471. /// warpwaitingpc "<map name>",<x>,<y>;
  8472. BUILDIN_FUNC(warpwaitingpc)
  8473. {
  8474. int x;
  8475. int y;
  8476. int i;
  8477. int n;
  8478. const char* map_name;
  8479. struct npc_data* nd;
  8480. struct chat_data* cd;
  8481. TBL_PC* sd;
  8482. nd = (struct npc_data *)map_id2bl(st->oid);
  8483. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8484. return 0;
  8485. map_name = script_getstr(st,2);
  8486. x = script_getnum(st,3);
  8487. y = script_getnum(st,4);
  8488. n = cd->trigger&0x7f;
  8489. if( script_hasdata(st,5) )
  8490. n = script_getnum(st,5);
  8491. for( i = 0; i < n && cd->users > 0; i++ )
  8492. {
  8493. sd = cd->usersd[0];
  8494. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  8495. {// can't teleport on this map
  8496. break;
  8497. }
  8498. if( cd->zeny )
  8499. {// fee set
  8500. if( (uint32)sd->status.zeny < cd->zeny )
  8501. {// no zeny to cover set fee
  8502. break;
  8503. }
  8504. pc_payzeny(sd, cd->zeny);
  8505. }
  8506. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  8507. if( strcmp(map_name,"Random") == 0 )
  8508. pc_randomwarp(sd,CLR_TELEPORT);
  8509. else if( strcmp(map_name,"SavePoint") == 0 )
  8510. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  8511. else
  8512. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  8513. }
  8514. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8515. return 0;
  8516. }
  8517. /////////////////////////////////////////////////////////////////////
  8518. // ...
  8519. //
  8520. /// Detaches a character from a script.
  8521. ///
  8522. /// @param st Script state to detach the character from.
  8523. static void script_detach_rid(struct script_state* st)
  8524. {
  8525. if(st->rid)
  8526. {
  8527. script_detach_state(st, false);
  8528. st->rid = 0;
  8529. }
  8530. }
  8531. /*==========================================
  8532. * RIDのアタッチ
  8533. *------------------------------------------*/
  8534. BUILDIN_FUNC(attachrid)
  8535. {
  8536. int rid = script_getnum(st,2);
  8537. struct map_session_data* sd;
  8538. if ((sd = map_id2sd(rid))!=NULL) {
  8539. script_detach_rid(st);
  8540. st->rid = rid;
  8541. script_attach_state(st);
  8542. script_pushint(st,1);
  8543. } else
  8544. script_pushint(st,0);
  8545. return 0;
  8546. }
  8547. /*==========================================
  8548. * RIDのデタッチ
  8549. *------------------------------------------*/
  8550. BUILDIN_FUNC(detachrid)
  8551. {
  8552. script_detach_rid(st);
  8553. return 0;
  8554. }
  8555. /*==========================================
  8556. * 存在チェック
  8557. *------------------------------------------*/
  8558. BUILDIN_FUNC(isloggedin)
  8559. {
  8560. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8561. if (script_hasdata(st,3) && sd &&
  8562. sd->status.char_id != script_getnum(st,3))
  8563. sd = NULL;
  8564. push_val(st->stack,C_INT,sd!=NULL);
  8565. return 0;
  8566. }
  8567. /*==========================================
  8568. *
  8569. *------------------------------------------*/
  8570. BUILDIN_FUNC(setmapflagnosave)
  8571. {
  8572. int m,x,y;
  8573. unsigned short mapindex;
  8574. const char *str,*str2;
  8575. str=script_getstr(st,2);
  8576. str2=script_getstr(st,3);
  8577. x=script_getnum(st,4);
  8578. y=script_getnum(st,5);
  8579. m = map_mapname2mapid(str);
  8580. mapindex = mapindex_name2id(str2);
  8581. if(m >= 0 && mapindex) {
  8582. map[m].flag.nosave=1;
  8583. map[m].save.map=mapindex;
  8584. map[m].save.x=x;
  8585. map[m].save.y=y;
  8586. }
  8587. return 0;
  8588. }
  8589. BUILDIN_FUNC(getmapflag)
  8590. {
  8591. int m,i;
  8592. const char *str;
  8593. str=script_getstr(st,2);
  8594. i=script_getnum(st,3);
  8595. m = map_mapname2mapid(str);
  8596. if(m >= 0) {
  8597. switch(i) {
  8598. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8599. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8600. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  8601. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8602. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8603. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8604. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8605. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8606. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8607. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8608. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8609. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8610. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8611. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8612. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8613. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8614. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8615. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8616. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8617. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8618. /**
  8619. * No longer available, keeping here just in case it's back someday. [Ind]
  8620. **/
  8621. //case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8622. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8623. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8624. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8625. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8626. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8627. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8628. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8629. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8630. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8631. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8632. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8633. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8634. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8635. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8636. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8637. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8638. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8639. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8640. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8641. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8642. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8643. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8644. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  8645. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8646. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8647. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8648. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8649. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8650. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8651. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8652. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  8653. }
  8654. }
  8655. return 0;
  8656. }
  8657. BUILDIN_FUNC(setmapflag)
  8658. {
  8659. int m,i;
  8660. const char *str;
  8661. const char *val=NULL;
  8662. str=script_getstr(st,2);
  8663. i=script_getnum(st,3);
  8664. if(script_hasdata(st,4)){
  8665. val=script_getstr(st,4);
  8666. }
  8667. m = map_mapname2mapid(str);
  8668. if(m >= 0) {
  8669. switch(i) {
  8670. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  8671. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  8672. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  8673. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  8674. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  8675. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  8676. case MF_PVP: map[m].flag.pvp = 1; break;
  8677. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  8678. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  8679. case MF_GVG: map[m].flag.gvg = 1; break;
  8680. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  8681. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  8682. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  8683. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  8684. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  8685. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  8686. case MF_SNOW: map[m].flag.snow = 1; break;
  8687. case MF_FOG: map[m].flag.fog = 1; break;
  8688. case MF_SAKURA: map[m].flag.sakura = 1; break;
  8689. case MF_LEAVES: map[m].flag.leaves = 1; break;
  8690. /**
  8691. * No longer available, keeping here just in case it's back someday. [Ind]
  8692. **/
  8693. //case MF_RAIN: map[m].flag.rain = 1; break;
  8694. case MF_NOGO: map[m].flag.nogo = 1; break;
  8695. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  8696. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  8697. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  8698. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  8699. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  8700. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  8701. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  8702. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  8703. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  8704. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  8705. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  8706. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  8707. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  8708. case MF_RESTRICTED:
  8709. map[m].zone |= 1<<((int)atoi(val)+1);
  8710. map[m].flag.restricted=1;
  8711. break;
  8712. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8713. case MF_NODROP: map[m].flag.nodrop = 1; break;
  8714. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8715. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8716. case MF_NOVENDING: map[m].flag.novending = 1; break;
  8717. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  8718. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  8719. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  8720. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  8721. case MF_TOWN: map[m].flag.town = 1; break;
  8722. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  8723. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  8724. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  8725. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  8726. case MF_BATTLEGROUND: map[m].flag.battleground = (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  8727. case MF_RESET: map[m].flag.reset = 1; break;
  8728. }
  8729. }
  8730. return 0;
  8731. }
  8732. BUILDIN_FUNC(removemapflag)
  8733. {
  8734. int m,i;
  8735. const char *str;
  8736. const char *val=NULL;
  8737. str=script_getstr(st,2);
  8738. i=script_getnum(st,3);
  8739. if(script_hasdata(st,4)){
  8740. val=script_getstr(st,4);
  8741. }
  8742. m = map_mapname2mapid(str);
  8743. if(m >= 0) {
  8744. switch(i) {
  8745. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  8746. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  8747. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  8748. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  8749. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  8750. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  8751. case MF_PVP: map[m].flag.pvp = 0; break;
  8752. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  8753. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  8754. case MF_GVG: map[m].flag.gvg = 0; break;
  8755. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  8756. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  8757. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  8758. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  8759. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  8760. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  8761. case MF_SNOW: map[m].flag.snow = 0; break;
  8762. case MF_FOG: map[m].flag.fog = 0; break;
  8763. case MF_SAKURA: map[m].flag.sakura = 0; break;
  8764. case MF_LEAVES: map[m].flag.leaves = 0; break;
  8765. /**
  8766. * No longer available, keeping here just in case it's back someday. [Ind]
  8767. **/
  8768. //case MF_RAIN: map[m].flag.rain = 0; break;
  8769. case MF_NOGO: map[m].flag.nogo = 0; break;
  8770. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  8771. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  8772. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  8773. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  8774. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  8775. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  8776. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  8777. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  8778. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  8779. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  8780. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  8781. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  8782. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  8783. case MF_RESTRICTED:
  8784. map[m].zone ^= 1<<((int)atoi(val)+1);
  8785. if (map[m].zone == 0){
  8786. map[m].flag.restricted=0;
  8787. }
  8788. break;
  8789. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  8790. case MF_NODROP: map[m].flag.nodrop = 0; break;
  8791. case MF_JEXP: map[m].jexp = 0; break;
  8792. case MF_BEXP: map[m].bexp = 0; break;
  8793. case MF_NOVENDING: map[m].flag.novending = 0; break;
  8794. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  8795. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  8796. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  8797. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  8798. case MF_TOWN: map[m].flag.town = 0; break;
  8799. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  8800. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  8801. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  8802. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  8803. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  8804. case MF_RESET: map[m].flag.reset = 0; break;
  8805. }
  8806. }
  8807. return 0;
  8808. }
  8809. BUILDIN_FUNC(pvpon)
  8810. {
  8811. int m;
  8812. const char *str;
  8813. TBL_PC* sd = NULL;
  8814. struct s_mapiterator* iter;
  8815. str = script_getstr(st,2);
  8816. m = map_mapname2mapid(str);
  8817. if( m < 0 || map[m].flag.pvp )
  8818. return 0; // nothing to do
  8819. map[m].flag.pvp = 1;
  8820. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  8821. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8822. return 0;
  8823. iter = mapit_getallusers();
  8824. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8825. {
  8826. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  8827. continue; // not applicable
  8828. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8829. sd->pvp_rank = 0;
  8830. sd->pvp_lastusers = 0;
  8831. sd->pvp_point = 5;
  8832. sd->pvp_won = 0;
  8833. sd->pvp_lost = 0;
  8834. }
  8835. mapit_free(iter);
  8836. return 0;
  8837. }
  8838. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8839. {
  8840. TBL_PC* sd = (TBL_PC*)bl;
  8841. clif_pvpset(sd, 0, 0, 2);
  8842. if (sd->pvp_timer != INVALID_TIMER) {
  8843. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8844. sd->pvp_timer = INVALID_TIMER;
  8845. }
  8846. return 0;
  8847. }
  8848. BUILDIN_FUNC(pvpoff)
  8849. {
  8850. int m;
  8851. const char *str;
  8852. str=script_getstr(st,2);
  8853. m = map_mapname2mapid(str);
  8854. if(m < 0 || !map[m].flag.pvp)
  8855. return 0; //fixed Lupus
  8856. map[m].flag.pvp = 0;
  8857. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8858. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8859. return 0;
  8860. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8861. return 0;
  8862. }
  8863. BUILDIN_FUNC(gvgon)
  8864. {
  8865. int m;
  8866. const char *str;
  8867. str=script_getstr(st,2);
  8868. m = map_mapname2mapid(str);
  8869. if(m >= 0 && !map[m].flag.gvg) {
  8870. map[m].flag.gvg = 1;
  8871. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  8872. }
  8873. return 0;
  8874. }
  8875. BUILDIN_FUNC(gvgoff)
  8876. {
  8877. int m;
  8878. const char *str;
  8879. str=script_getstr(st,2);
  8880. m = map_mapname2mapid(str);
  8881. if(m >= 0 && map[m].flag.gvg) {
  8882. map[m].flag.gvg = 0;
  8883. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8884. }
  8885. return 0;
  8886. }
  8887. /*==========================================
  8888. * Shows an emoticon on top of the player/npc
  8889. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  8890. *------------------------------------------*/
  8891. //Optional second parameter added by [Skotlex]
  8892. BUILDIN_FUNC(emotion)
  8893. {
  8894. int type;
  8895. int player=0;
  8896. type=script_getnum(st,2);
  8897. if(type < 0 || type > 100)
  8898. return 0;
  8899. if( script_hasdata(st,3) )
  8900. player=script_getnum(st,3);
  8901. if (player) {
  8902. TBL_PC *sd = NULL;
  8903. if( script_hasdata(st,4) )
  8904. sd = map_nick2sd(script_getstr(st,4));
  8905. else
  8906. sd = script_rid2sd(st);
  8907. if (sd)
  8908. clif_emotion(&sd->bl,type);
  8909. } else
  8910. if( script_hasdata(st,4) )
  8911. {
  8912. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  8913. if(nd)
  8914. clif_emotion(&nd->bl,type);
  8915. }
  8916. else
  8917. clif_emotion(map_id2bl(st->oid),type);
  8918. return 0;
  8919. }
  8920. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8921. {
  8922. int m=va_arg(ap,int);
  8923. int g_id=va_arg(ap,int);
  8924. int flag=va_arg(ap,int);
  8925. if(!sd || sd->bl.m != m)
  8926. return 0;
  8927. if(
  8928. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8929. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8930. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8931. )
  8932. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  8933. return 1;
  8934. }
  8935. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8936. {
  8937. struct mob_data *md=(struct mob_data *)bl;
  8938. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8939. status_kill(bl);
  8940. return 0;
  8941. }
  8942. BUILDIN_FUNC(maprespawnguildid)
  8943. {
  8944. const char *mapname=script_getstr(st,2);
  8945. int g_id=script_getnum(st,3);
  8946. int flag=script_getnum(st,4);
  8947. int m=map_mapname2mapid(mapname);
  8948. if(m == -1)
  8949. return 0;
  8950. //Catch ALL players (in case some are 'between maps' on execution time)
  8951. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8952. if (flag&4) //Remove script mobs.
  8953. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8954. return 0;
  8955. }
  8956. BUILDIN_FUNC(agitstart)
  8957. {
  8958. if(agit_flag==1) return 0; // Agit already Start.
  8959. agit_flag=1;
  8960. guild_agit_start();
  8961. return 0;
  8962. }
  8963. BUILDIN_FUNC(agitend)
  8964. {
  8965. if(agit_flag==0) return 0; // Agit already End.
  8966. agit_flag=0;
  8967. guild_agit_end();
  8968. return 0;
  8969. }
  8970. BUILDIN_FUNC(agitstart2)
  8971. {
  8972. if(agit2_flag==1) return 0; // Agit2 already Start.
  8973. agit2_flag=1;
  8974. guild_agit2_start();
  8975. return 0;
  8976. }
  8977. BUILDIN_FUNC(agitend2)
  8978. {
  8979. if(agit2_flag==0) return 0; // Agit2 already End.
  8980. agit2_flag=0;
  8981. guild_agit2_end();
  8982. return 0;
  8983. }
  8984. /*==========================================
  8985. * Returns whether woe is on or off. // choice script
  8986. *------------------------------------------*/
  8987. BUILDIN_FUNC(agitcheck)
  8988. {
  8989. script_pushint(st,agit_flag);
  8990. return 0;
  8991. }
  8992. /*==========================================
  8993. * Returns whether woese is on or off. // choice script
  8994. *------------------------------------------*/
  8995. BUILDIN_FUNC(agitcheck2)
  8996. {
  8997. script_pushint(st,agit2_flag);
  8998. return 0;
  8999. }
  9000. /// Sets the guild_id of this npc.
  9001. ///
  9002. /// flagemblem <guild_id>;
  9003. BUILDIN_FUNC(flagemblem)
  9004. {
  9005. TBL_NPC* nd;
  9006. int g_id=script_getnum(st,2);
  9007. if(g_id < 0) return 0;
  9008. nd = (TBL_NPC*)map_id2nd(st->oid);
  9009. if( nd == NULL )
  9010. {
  9011. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  9012. }
  9013. else if( nd->subtype != SCRIPT )
  9014. {
  9015. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  9016. }
  9017. else
  9018. {
  9019. nd->u.scr.guild_id = g_id;
  9020. clif_guild_emblem_area(&nd->bl);
  9021. }
  9022. return 0;
  9023. }
  9024. BUILDIN_FUNC(getcastlename)
  9025. {
  9026. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9027. struct guild_castle* gc = guild_mapname2gc(mapname);
  9028. const char* name = (gc) ? gc->castle_name : "";
  9029. script_pushstrcopy(st,name);
  9030. return 0;
  9031. }
  9032. BUILDIN_FUNC(getcastledata)
  9033. {
  9034. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9035. int index = script_getnum(st,3);
  9036. struct guild_castle *gc = guild_mapname2gc(mapname);
  9037. if (gc == NULL) {
  9038. script_pushint(st,0);
  9039. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9040. return 1;
  9041. }
  9042. switch (index) {
  9043. case 1:
  9044. script_pushint(st,gc->guild_id); break;
  9045. case 2:
  9046. script_pushint(st,gc->economy); break;
  9047. case 3:
  9048. script_pushint(st,gc->defense); break;
  9049. case 4:
  9050. script_pushint(st,gc->triggerE); break;
  9051. case 5:
  9052. script_pushint(st,gc->triggerD); break;
  9053. case 6:
  9054. script_pushint(st,gc->nextTime); break;
  9055. case 7:
  9056. script_pushint(st,gc->payTime); break;
  9057. case 8:
  9058. script_pushint(st,gc->createTime); break;
  9059. case 9:
  9060. script_pushint(st,gc->visibleC); break;
  9061. default:
  9062. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  9063. script_pushint(st,gc->guardian[index-10].visible);
  9064. break;
  9065. }
  9066. script_pushint(st,0);
  9067. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9068. return 1;
  9069. }
  9070. return 0;
  9071. }
  9072. BUILDIN_FUNC(setcastledata)
  9073. {
  9074. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9075. int index = script_getnum(st,3);
  9076. int value = script_getnum(st,4);
  9077. struct guild_castle *gc = guild_mapname2gc(mapname);
  9078. if (gc == NULL) {
  9079. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9080. return 1;
  9081. }
  9082. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  9083. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9084. return 1;
  9085. }
  9086. guild_castledatasave(gc->castle_id, index, value);
  9087. return 0;
  9088. }
  9089. /* =====================================================================
  9090. * ギルド情報を要求する
  9091. * ---------------------------------------------------------------------*/
  9092. BUILDIN_FUNC(requestguildinfo)
  9093. {
  9094. int guild_id=script_getnum(st,2);
  9095. const char *event=NULL;
  9096. if( script_hasdata(st,3) ){
  9097. event=script_getstr(st,3);
  9098. check_event(st, event);
  9099. }
  9100. if(guild_id>0)
  9101. guild_npc_request_info(guild_id,event);
  9102. return 0;
  9103. }
  9104. /// Returns the number of cards that have been compounded onto the specified equipped item.
  9105. /// getequipcardcnt(<equipment slot>);
  9106. BUILDIN_FUNC(getequipcardcnt)
  9107. {
  9108. int i=-1,j,num;
  9109. TBL_PC *sd;
  9110. int count;
  9111. num=script_getnum(st,2);
  9112. sd=script_rid2sd(st);
  9113. if (num > 0 && num <= ARRAYLENGTH(equip))
  9114. i=pc_checkequip(sd,equip[num-1]);
  9115. if (i < 0 || !sd->inventory_data[i]) {
  9116. script_pushint(st,0);
  9117. return 0;
  9118. }
  9119. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9120. {
  9121. script_pushint(st,0);
  9122. return 0;
  9123. }
  9124. count = 0;
  9125. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  9126. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  9127. count++;
  9128. script_pushint(st,count);
  9129. return 0;
  9130. }
  9131. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  9132. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  9133. /// successremovecards <slot>;
  9134. BUILDIN_FUNC(successremovecards)
  9135. {
  9136. int i=-1,j,c,cardflag=0;
  9137. TBL_PC* sd = script_rid2sd(st);
  9138. int num = script_getnum(st,2);
  9139. if (num > 0 && num <= ARRAYLENGTH(equip))
  9140. i=pc_checkequip(sd,equip[num-1]);
  9141. if (i < 0 || !sd->inventory_data[i]) {
  9142. return 0;
  9143. }
  9144. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9145. return 0;
  9146. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9147. {
  9148. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9149. {// extract this card from the item
  9150. int flag;
  9151. struct item item_tmp;
  9152. cardflag = 1;
  9153. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9154. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9155. item_tmp.attribute=0,item_tmp.expire_time=0;
  9156. for (j = 0; j < MAX_SLOTS; j++)
  9157. item_tmp.card[j]=0;
  9158. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // 持てないならドロップ
  9159. clif_additem(sd,0,0,flag);
  9160. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9161. }
  9162. }
  9163. }
  9164. if(cardflag == 1)
  9165. { // カードを取り除いたアイテム所得
  9166. int flag;
  9167. struct item item_tmp;
  9168. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9169. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9170. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9171. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9172. item_tmp.card[j]=0;
  9173. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9174. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9175. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  9176. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // もてないならドロップ
  9177. clif_additem(sd,0,0,flag);
  9178. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9179. }
  9180. clif_misceffect(&sd->bl,3);
  9181. }
  9182. return 0;
  9183. }
  9184. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  9185. /// failedremovecards <slot>, <type>;
  9186. /// <type>=0 : will destroy both the item and the cards.
  9187. /// <type>=1 : will keep the item, but destroy the cards.
  9188. /// <type>=2 : will keep the cards, but destroy the item.
  9189. /// <type>=? : will just display the failure effect.
  9190. BUILDIN_FUNC(failedremovecards)
  9191. {
  9192. int i=-1,j,c,cardflag=0;
  9193. TBL_PC* sd = script_rid2sd(st);
  9194. int num = script_getnum(st,2);
  9195. int typefail = script_getnum(st,3);
  9196. if (num > 0 && num <= ARRAYLENGTH(equip))
  9197. i=pc_checkequip(sd,equip[num-1]);
  9198. if (i < 0 || !sd->inventory_data[i])
  9199. return 0;
  9200. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9201. return 0;
  9202. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9203. {
  9204. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9205. {
  9206. cardflag = 1;
  9207. if(typefail == 2)
  9208. {// add cards to inventory, clear
  9209. int flag;
  9210. struct item item_tmp;
  9211. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9212. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9213. item_tmp.attribute=0,item_tmp.expire_time=0;
  9214. for (j = 0; j < MAX_SLOTS; j++)
  9215. item_tmp.card[j]=0;
  9216. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  9217. clif_additem(sd,0,0,flag);
  9218. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9219. }
  9220. }
  9221. }
  9222. }
  9223. if(cardflag == 1)
  9224. {
  9225. if(typefail == 0 || typefail == 2){ // 武具損失
  9226. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  9227. }
  9228. if(typefail == 1){ // カードのみ損失(武具を返す)
  9229. int flag;
  9230. struct item item_tmp;
  9231. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9232. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9233. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9234. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9235. item_tmp.card[j]=0;
  9236. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9237. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9238. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  9239. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  9240. clif_additem(sd,0,0,flag);
  9241. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9242. }
  9243. }
  9244. clif_misceffect(&sd->bl,2);
  9245. }
  9246. return 0;
  9247. }
  9248. /* ================================================================
  9249. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  9250. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  9251. * improved by [Lance]
  9252. * ================================================================*/
  9253. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  9254. {
  9255. int x,y,m,check_val=0,check_ID=0,i=0;
  9256. struct guild *g = NULL;
  9257. struct party_data *p = NULL;
  9258. const char *str;
  9259. const char *mapname;
  9260. unsigned int index;
  9261. mapname=script_getstr(st,2);
  9262. str=script_getstr(st,3);
  9263. x=script_getnum(st,4);
  9264. y=script_getnum(st,5);
  9265. if(script_hasdata(st,7)){
  9266. check_val=script_getnum(st,6);
  9267. check_ID=script_getnum(st,7);
  9268. }
  9269. if((m=map_mapname2mapid(mapname))< 0)
  9270. return 0;
  9271. if(!(index=mapindex_name2id(str)))
  9272. return 0;
  9273. switch(check_val){
  9274. case 1:
  9275. g = guild_search(check_ID);
  9276. if (g){
  9277. for( i=0; i < g->max_member; i++)
  9278. {
  9279. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9280. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  9281. }
  9282. }
  9283. }
  9284. break;
  9285. case 2:
  9286. p = party_search(check_ID);
  9287. if(p){
  9288. for(i=0;i<MAX_PARTY; i++){
  9289. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9290. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  9291. }
  9292. }
  9293. }
  9294. break;
  9295. default:
  9296. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  9297. break;
  9298. }
  9299. return 0;
  9300. }
  9301. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9302. {
  9303. char *event=va_arg(ap,char *);
  9304. struct mob_data *md = ((struct mob_data *)bl);
  9305. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  9306. return 1;
  9307. return 0;
  9308. }
  9309. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9310. {
  9311. const char *mapname,*event;
  9312. int m;
  9313. mapname=script_getstr(st,2);
  9314. event=script_getstr(st,3);
  9315. if( strcmp(event, "all") == 0 )
  9316. event = NULL;
  9317. else
  9318. check_event(st, event);
  9319. if( strcmp(mapname, "this") == 0 ) {
  9320. struct map_session_data *sd = script_rid2sd(st);
  9321. if( sd )
  9322. m = sd->bl.m;
  9323. else {
  9324. script_pushint(st,-1);
  9325. return 0;
  9326. }
  9327. }
  9328. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9329. script_pushint(st,-1);
  9330. return 0;
  9331. }
  9332. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9333. {
  9334. script_pushint(st,-1);
  9335. return 0;
  9336. }
  9337. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9338. return 0;
  9339. }
  9340. BUILDIN_FUNC(marriage)
  9341. {
  9342. const char *partner=script_getstr(st,2);
  9343. TBL_PC *sd=script_rid2sd(st);
  9344. TBL_PC *p_sd=map_nick2sd(partner);
  9345. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9346. script_pushint(st,0);
  9347. return 0;
  9348. }
  9349. script_pushint(st,1);
  9350. return 0;
  9351. }
  9352. BUILDIN_FUNC(wedding_effect)
  9353. {
  9354. TBL_PC *sd=script_rid2sd(st);
  9355. struct block_list *bl;
  9356. if(sd==NULL) {
  9357. bl=map_id2bl(st->oid);
  9358. } else
  9359. bl=&sd->bl;
  9360. clif_wedding_effect(bl);
  9361. return 0;
  9362. }
  9363. BUILDIN_FUNC(divorce)
  9364. {
  9365. TBL_PC *sd=script_rid2sd(st);
  9366. if(sd==NULL || pc_divorce(sd) < 0){
  9367. script_pushint(st,0);
  9368. return 0;
  9369. }
  9370. script_pushint(st,1);
  9371. return 0;
  9372. }
  9373. BUILDIN_FUNC(ispartneron)
  9374. {
  9375. TBL_PC *sd=script_rid2sd(st);
  9376. if(sd==NULL || !pc_ismarried(sd) ||
  9377. map_charid2sd(sd->status.partner_id) == NULL) {
  9378. script_pushint(st,0);
  9379. return 0;
  9380. }
  9381. script_pushint(st,1);
  9382. return 0;
  9383. }
  9384. BUILDIN_FUNC(getpartnerid)
  9385. {
  9386. TBL_PC *sd=script_rid2sd(st);
  9387. if (sd == NULL) {
  9388. script_pushint(st,0);
  9389. return 0;
  9390. }
  9391. script_pushint(st,sd->status.partner_id);
  9392. return 0;
  9393. }
  9394. BUILDIN_FUNC(getchildid)
  9395. {
  9396. TBL_PC *sd=script_rid2sd(st);
  9397. if (sd == NULL) {
  9398. script_pushint(st,0);
  9399. return 0;
  9400. }
  9401. script_pushint(st,sd->status.child);
  9402. return 0;
  9403. }
  9404. BUILDIN_FUNC(getmotherid)
  9405. {
  9406. TBL_PC *sd=script_rid2sd(st);
  9407. if (sd == NULL) {
  9408. script_pushint(st,0);
  9409. return 0;
  9410. }
  9411. script_pushint(st,sd->status.mother);
  9412. return 0;
  9413. }
  9414. BUILDIN_FUNC(getfatherid)
  9415. {
  9416. TBL_PC *sd=script_rid2sd(st);
  9417. if (sd == NULL) {
  9418. script_pushint(st,0);
  9419. return 0;
  9420. }
  9421. script_pushint(st,sd->status.father);
  9422. return 0;
  9423. }
  9424. BUILDIN_FUNC(warppartner)
  9425. {
  9426. int x,y;
  9427. unsigned short mapindex;
  9428. const char *str;
  9429. TBL_PC *sd=script_rid2sd(st);
  9430. TBL_PC *p_sd=NULL;
  9431. if(sd==NULL || !pc_ismarried(sd) ||
  9432. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9433. script_pushint(st,0);
  9434. return 0;
  9435. }
  9436. str=script_getstr(st,2);
  9437. x=script_getnum(st,3);
  9438. y=script_getnum(st,4);
  9439. mapindex = mapindex_name2id(str);
  9440. if (mapindex) {
  9441. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  9442. script_pushint(st,1);
  9443. } else
  9444. script_pushint(st,0);
  9445. return 0;
  9446. }
  9447. /*================================================
  9448. * Script for Displaying MOB Information [Valaris]
  9449. *------------------------------------------------*/
  9450. BUILDIN_FUNC(strmobinfo)
  9451. {
  9452. int num=script_getnum(st,2);
  9453. int class_=script_getnum(st,3);
  9454. if(!mobdb_checkid(class_))
  9455. {
  9456. script_pushint(st,0);
  9457. return 0;
  9458. }
  9459. switch (num) {
  9460. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9461. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9462. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9463. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9464. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9465. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9466. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9467. default:
  9468. script_pushint(st,0);
  9469. break;
  9470. }
  9471. return 0;
  9472. }
  9473. /*==========================================
  9474. * Summon guardians [Valaris]
  9475. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9476. *------------------------------------------*/
  9477. BUILDIN_FUNC(guardian)
  9478. {
  9479. int class_=0,x=0,y=0,guardian=0;
  9480. const char *str,*map,*evt="";
  9481. struct script_data *data;
  9482. bool has_index = false;
  9483. map =script_getstr(st,2);
  9484. x =script_getnum(st,3);
  9485. y =script_getnum(st,4);
  9486. str =script_getstr(st,5);
  9487. class_=script_getnum(st,6);
  9488. if( script_hasdata(st,8) )
  9489. {// "<event label>",<guardian index>
  9490. evt=script_getstr(st,7);
  9491. guardian=script_getnum(st,8);
  9492. has_index = true;
  9493. } else if( script_hasdata(st,7) ){
  9494. data=script_getdata(st,7);
  9495. get_val(st,data);
  9496. if( data_isstring(data) )
  9497. {// "<event label>"
  9498. evt=script_getstr(st,7);
  9499. } else if( data_isint(data) )
  9500. {// <guardian index>
  9501. guardian=script_getnum(st,7);
  9502. has_index = true;
  9503. } else {
  9504. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9505. script_reportdata(data);
  9506. return 1;
  9507. }
  9508. }
  9509. check_event(st, evt);
  9510. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9511. return 0;
  9512. }
  9513. /*==========================================
  9514. * Invisible Walls [Zephyrus]
  9515. *------------------------------------------*/
  9516. BUILDIN_FUNC(setwall)
  9517. {
  9518. const char *map, *name;
  9519. int x, y, m, size, dir;
  9520. bool shootable;
  9521. map = script_getstr(st,2);
  9522. x = script_getnum(st,3);
  9523. y = script_getnum(st,4);
  9524. size = script_getnum(st,5);
  9525. dir = script_getnum(st,6);
  9526. shootable = script_getnum(st,7);
  9527. name = script_getstr(st,8);
  9528. if( (m = map_mapname2mapid(map)) < 0 )
  9529. return 0; // Invalid Map
  9530. map_iwall_set(m, x, y, size, dir, shootable, name);
  9531. return 0;
  9532. }
  9533. BUILDIN_FUNC(delwall)
  9534. {
  9535. const char *name = script_getstr(st,2);
  9536. map_iwall_remove(name);
  9537. return 0;
  9538. }
  9539. /// Retrieves various information about the specified guardian.
  9540. ///
  9541. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9542. /// type: 0 - whether it is deployed or not
  9543. /// 1 - maximum hp
  9544. /// 2 - current hp
  9545. ///
  9546. BUILDIN_FUNC(guardianinfo)
  9547. {
  9548. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9549. int id = script_getnum(st,3);
  9550. int type = script_getnum(st,4);
  9551. struct guild_castle* gc = guild_mapname2gc(mapname);
  9552. struct mob_data* gd;
  9553. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9554. {
  9555. script_pushint(st,-1);
  9556. return 0;
  9557. }
  9558. if( type == 0 )
  9559. script_pushint(st, gc->guardian[id].visible);
  9560. else
  9561. if( !gc->guardian[id].visible )
  9562. script_pushint(st,-1);
  9563. else
  9564. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9565. script_pushint(st,-1);
  9566. else
  9567. {
  9568. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9569. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9570. else
  9571. script_pushint(st,-1);
  9572. }
  9573. return 0;
  9574. }
  9575. /*==========================================
  9576. * IDからItem名
  9577. *------------------------------------------*/
  9578. BUILDIN_FUNC(getitemname)
  9579. {
  9580. int item_id=0;
  9581. struct item_data *i_data;
  9582. char *item_name;
  9583. struct script_data *data;
  9584. data=script_getdata(st,2);
  9585. get_val(st,data);
  9586. if( data_isstring(data) ){
  9587. const char *name=conv_str(st,data);
  9588. struct item_data *item_data = itemdb_searchname(name);
  9589. if( item_data )
  9590. item_id=item_data->nameid;
  9591. }else
  9592. item_id=conv_num(st,data);
  9593. i_data = itemdb_exists(item_id);
  9594. if (i_data == NULL)
  9595. {
  9596. script_pushconststr(st,"null");
  9597. return 0;
  9598. }
  9599. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  9600. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9601. script_pushstr(st,item_name);
  9602. return 0;
  9603. }
  9604. /*==========================================
  9605. * Returns number of slots an item has. [Skotlex]
  9606. *------------------------------------------*/
  9607. BUILDIN_FUNC(getitemslots)
  9608. {
  9609. int item_id;
  9610. struct item_data *i_data;
  9611. item_id=script_getnum(st,2);
  9612. i_data = itemdb_exists(item_id);
  9613. if (i_data)
  9614. script_pushint(st,i_data->slot);
  9615. else
  9616. script_pushint(st,-1);
  9617. return 0;
  9618. }
  9619. // TODO: add matk here if needed/once we get rid of REMODE
  9620. /*==========================================
  9621. * Returns some values of an item [Lupus]
  9622. * Price, Weight, etc...
  9623. getiteminfo(itemID,n), where n
  9624. 0 value_buy;
  9625. 1 value_sell;
  9626. 2 type;
  9627. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9628. if = 0, then monsters don't drop it at all (rare or a quest item)
  9629. if = -1, then this item is sold in NPC shops only
  9630. 4 sex;
  9631. 5 equip;
  9632. 6 weight;
  9633. 7 atk;
  9634. 8 def;
  9635. 9 range;
  9636. 10 slot;
  9637. 11 look;
  9638. 12 elv;
  9639. 13 wlv;
  9640. 14 view id
  9641. *------------------------------------------*/
  9642. BUILDIN_FUNC(getiteminfo)
  9643. {
  9644. int item_id,n;
  9645. int *item_arr;
  9646. struct item_data *i_data;
  9647. item_id = script_getnum(st,2);
  9648. n = script_getnum(st,3);
  9649. i_data = itemdb_exists(item_id);
  9650. if (i_data && n>=0 && n<=14) {
  9651. item_arr = (int*)&i_data->value_buy;
  9652. script_pushint(st,item_arr[n]);
  9653. } else
  9654. script_pushint(st,-1);
  9655. return 0;
  9656. }
  9657. /*==========================================
  9658. * Set some values of an item [Lupus]
  9659. * Price, Weight, etc...
  9660. setiteminfo(itemID,n,Value), where n
  9661. 0 value_buy;
  9662. 1 value_sell;
  9663. 2 type;
  9664. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9665. if = 0, then monsters don't drop it at all (rare or a quest item)
  9666. if = -1, then this item is sold in NPC shops only
  9667. 4 sex;
  9668. 5 equip;
  9669. 6 weight;
  9670. 7 atk;
  9671. 8 def;
  9672. 9 range;
  9673. 10 slot;
  9674. 11 look;
  9675. 12 elv;
  9676. 13 wlv;
  9677. 14 view id
  9678. * Returns Value or -1 if the wrong field's been set
  9679. *------------------------------------------*/
  9680. BUILDIN_FUNC(setiteminfo)
  9681. {
  9682. int item_id,n,value;
  9683. int *item_arr;
  9684. struct item_data *i_data;
  9685. item_id = script_getnum(st,2);
  9686. n = script_getnum(st,3);
  9687. value = script_getnum(st,4);
  9688. i_data = itemdb_exists(item_id);
  9689. if (i_data && n>=0 && n<=14) {
  9690. item_arr = (int*)&i_data->value_buy;
  9691. item_arr[n] = value;
  9692. script_pushint(st,value);
  9693. } else
  9694. script_pushint(st,-1);
  9695. return 0;
  9696. }
  9697. /*==========================================
  9698. * Returns value from equipped item slot n [Lupus]
  9699. getequipcardid(num,slot)
  9700. where
  9701. num = eqip position slot
  9702. slot = 0,1,2,3 (Card Slot N)
  9703. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9704. it's useful when you want to check item cards or if it's signed
  9705. Useful for such quests as "Sign this refined item with players name" etc
  9706. Hat[0] +4 -> Player's Hat[0] +4
  9707. *------------------------------------------*/
  9708. BUILDIN_FUNC(getequipcardid)
  9709. {
  9710. int i=-1,num,slot;
  9711. TBL_PC *sd;
  9712. num=script_getnum(st,2);
  9713. slot=script_getnum(st,3);
  9714. sd=script_rid2sd(st);
  9715. if (num > 0 && num <= ARRAYLENGTH(equip))
  9716. i=pc_checkequip(sd,equip[num-1]);
  9717. if(i >= 0 && slot>=0 && slot<4)
  9718. script_pushint(st,sd->status.inventory[i].card[slot]);
  9719. else
  9720. script_pushint(st,0);
  9721. return 0;
  9722. }
  9723. /*==========================================
  9724. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9725. *------------------------------------------*/
  9726. BUILDIN_FUNC(petskillbonus)
  9727. {
  9728. struct pet_data *pd;
  9729. TBL_PC *sd=script_rid2sd(st);
  9730. if(sd==NULL || sd->pd==NULL)
  9731. return 0;
  9732. pd=sd->pd;
  9733. if (pd->bonus)
  9734. { //Clear previous bonus
  9735. if (pd->bonus->timer != INVALID_TIMER)
  9736. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9737. } else //init
  9738. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9739. pd->bonus->type=script_getnum(st,2);
  9740. pd->bonus->val=script_getnum(st,3);
  9741. pd->bonus->duration=script_getnum(st,4);
  9742. pd->bonus->delay=script_getnum(st,5);
  9743. if (pd->state.skillbonus == 1)
  9744. pd->state.skillbonus=0; // waiting state
  9745. // wait for timer to start
  9746. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9747. pd->bonus->timer = INVALID_TIMER;
  9748. else
  9749. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9750. return 0;
  9751. }
  9752. /*==========================================
  9753. * pet looting [Valaris] //Rewritten by [Skotlex]
  9754. *------------------------------------------*/
  9755. BUILDIN_FUNC(petloot)
  9756. {
  9757. int max;
  9758. struct pet_data *pd;
  9759. TBL_PC *sd=script_rid2sd(st);
  9760. if(sd==NULL || sd->pd==NULL)
  9761. return 0;
  9762. max=script_getnum(st,2);
  9763. if(max < 1)
  9764. max = 1; //Let'em loot at least 1 item.
  9765. else if (max > MAX_PETLOOT_SIZE)
  9766. max = MAX_PETLOOT_SIZE;
  9767. pd = sd->pd;
  9768. if (pd->loot != NULL)
  9769. { //Release whatever was there already and reallocate memory
  9770. pet_lootitem_drop(pd, pd->msd);
  9771. aFree(pd->loot->item);
  9772. }
  9773. else
  9774. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9775. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9776. pd->loot->max=max;
  9777. pd->loot->count = 0;
  9778. pd->loot->weight = 0;
  9779. return 0;
  9780. }
  9781. /*==========================================
  9782. * PCの所持品情報読み取り
  9783. *------------------------------------------*/
  9784. BUILDIN_FUNC(getinventorylist)
  9785. {
  9786. TBL_PC *sd=script_rid2sd(st);
  9787. char card_var[NAME_LENGTH];
  9788. int i,j=0,k;
  9789. if(!sd) return 0;
  9790. for(i=0;i<MAX_INVENTORY;i++){
  9791. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9792. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  9793. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  9794. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  9795. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  9796. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  9797. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  9798. for (k = 0; k < MAX_SLOTS; k++)
  9799. {
  9800. sprintf(card_var, "@inventorylist_card%d",k+1);
  9801. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  9802. }
  9803. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  9804. j++;
  9805. }
  9806. }
  9807. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9808. return 0;
  9809. }
  9810. BUILDIN_FUNC(getskilllist)
  9811. {
  9812. TBL_PC *sd=script_rid2sd(st);
  9813. int i,j=0;
  9814. if(!sd) return 0;
  9815. for(i=0;i<MAX_SKILL;i++){
  9816. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9817. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  9818. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  9819. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  9820. j++;
  9821. }
  9822. }
  9823. pc_setreg(sd,add_str("@skilllist_count"),j);
  9824. return 0;
  9825. }
  9826. BUILDIN_FUNC(clearitem)
  9827. {
  9828. TBL_PC *sd=script_rid2sd(st);
  9829. int i;
  9830. if(sd==NULL) return 0;
  9831. for (i=0; i<MAX_INVENTORY; i++) {
  9832. if (sd->status.inventory[i].amount) {
  9833. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  9834. }
  9835. }
  9836. return 0;
  9837. }
  9838. /*==========================================
  9839. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9840. *------------------------------------------*/
  9841. BUILDIN_FUNC(disguise)
  9842. {
  9843. int id;
  9844. TBL_PC* sd = script_rid2sd(st);
  9845. if (sd == NULL) return 0;
  9846. id = script_getnum(st,2);
  9847. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9848. pc_disguise(sd, id);
  9849. script_pushint(st,id);
  9850. } else
  9851. script_pushint(st,0);
  9852. return 0;
  9853. }
  9854. /*==========================================
  9855. * Undisguise Player (returns 1 if success, 0 on fail)
  9856. *------------------------------------------*/
  9857. BUILDIN_FUNC(undisguise)
  9858. {
  9859. TBL_PC* sd = script_rid2sd(st);
  9860. if (sd == NULL) return 0;
  9861. if (sd->disguise) {
  9862. pc_disguise(sd, 0);
  9863. script_pushint(st,0);
  9864. } else {
  9865. script_pushint(st,1);
  9866. }
  9867. return 0;
  9868. }
  9869. /*==========================================
  9870. * NPCクラスチェンジ
  9871. * classは変わりたいclass
  9872. * typeは通常0なのかな?
  9873. *------------------------------------------*/
  9874. BUILDIN_FUNC(classchange)
  9875. {
  9876. int _class,type;
  9877. struct block_list *bl=map_id2bl(st->oid);
  9878. if(bl==NULL) return 0;
  9879. _class=script_getnum(st,2);
  9880. type=script_getnum(st,3);
  9881. clif_class_change(bl,_class,type);
  9882. return 0;
  9883. }
  9884. /*==========================================
  9885. * NPCから発生するエフェクト
  9886. *------------------------------------------*/
  9887. BUILDIN_FUNC(misceffect)
  9888. {
  9889. int type;
  9890. type=script_getnum(st,2);
  9891. if(st->oid && st->oid != fake_nd->bl.id) {
  9892. struct block_list *bl = map_id2bl(st->oid);
  9893. if (bl)
  9894. clif_specialeffect(bl,type,AREA);
  9895. } else{
  9896. TBL_PC *sd=script_rid2sd(st);
  9897. if(sd)
  9898. clif_specialeffect(&sd->bl,type,AREA);
  9899. }
  9900. return 0;
  9901. }
  9902. /*==========================================
  9903. * Play a BGM on a single client [Rikter/Yommy]
  9904. *------------------------------------------*/
  9905. BUILDIN_FUNC(playBGM)
  9906. {
  9907. const char* name;
  9908. struct map_session_data* sd;
  9909. if( ( sd = script_rid2sd(st) ) != NULL )
  9910. {
  9911. name = script_getstr(st,2);
  9912. clif_playBGM(sd, name);
  9913. }
  9914. return 0;
  9915. }
  9916. static int playBGM_sub(struct block_list* bl,va_list ap)
  9917. {
  9918. const char* name = va_arg(ap,const char*);
  9919. clif_playBGM(BL_CAST(BL_PC, bl), name);
  9920. return 0;
  9921. }
  9922. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  9923. {
  9924. const char* name = va_arg(args, const char*);
  9925. clif_playBGM(sd, name);
  9926. return 0;
  9927. }
  9928. /*==========================================
  9929. * Play a BGM on multiple client [Rikter/Yommy]
  9930. *------------------------------------------*/
  9931. BUILDIN_FUNC(playBGMall)
  9932. {
  9933. const char* name;
  9934. name = script_getstr(st,2);
  9935. if( script_hasdata(st,7) )
  9936. {// specified part of map
  9937. const char* map = script_getstr(st,3);
  9938. int x0 = script_getnum(st,4);
  9939. int y0 = script_getnum(st,5);
  9940. int x1 = script_getnum(st,6);
  9941. int y1 = script_getnum(st,7);
  9942. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  9943. }
  9944. else if( script_hasdata(st,3) )
  9945. {// entire map
  9946. const char* map = script_getstr(st,3);
  9947. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  9948. }
  9949. else
  9950. {// entire server
  9951. map_foreachpc(&playBGM_foreachpc_sub, name);
  9952. }
  9953. return 0;
  9954. }
  9955. /*==========================================
  9956. * サウンドエフェクト
  9957. *------------------------------------------*/
  9958. BUILDIN_FUNC(soundeffect)
  9959. {
  9960. TBL_PC* sd = script_rid2sd(st);
  9961. const char* name = script_getstr(st,2);
  9962. int type = script_getnum(st,3);
  9963. if(sd)
  9964. {
  9965. clif_soundeffect(sd,&sd->bl,name,type);
  9966. }
  9967. return 0;
  9968. }
  9969. int soundeffect_sub(struct block_list* bl,va_list ap)
  9970. {
  9971. char* name = va_arg(ap,char*);
  9972. int type = va_arg(ap,int);
  9973. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9974. return 0;
  9975. }
  9976. /*==========================================
  9977. * Play a sound effect (.wav) on multiple clients
  9978. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9979. *------------------------------------------*/
  9980. BUILDIN_FUNC(soundeffectall)
  9981. {
  9982. struct block_list* bl;
  9983. const char* name;
  9984. int type;
  9985. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9986. if (!bl)
  9987. return 0;
  9988. name = script_getstr(st,2);
  9989. type = script_getnum(st,3);
  9990. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9991. if(!script_hasdata(st,4))
  9992. { // area around
  9993. clif_soundeffectall(bl, name, type, AREA);
  9994. }
  9995. else
  9996. if(!script_hasdata(st,5))
  9997. { // entire map
  9998. const char* map = script_getstr(st,4);
  9999. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  10000. }
  10001. else
  10002. if(script_hasdata(st,8))
  10003. { // specified part of map
  10004. const char* map = script_getstr(st,4);
  10005. int x0 = script_getnum(st,5);
  10006. int y0 = script_getnum(st,6);
  10007. int x1 = script_getnum(st,7);
  10008. int y1 = script_getnum(st,8);
  10009. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  10010. }
  10011. else
  10012. {
  10013. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  10014. }
  10015. return 0;
  10016. }
  10017. /*==========================================
  10018. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  10019. *------------------------------------------*/
  10020. BUILDIN_FUNC(petrecovery)
  10021. {
  10022. struct pet_data *pd;
  10023. TBL_PC *sd=script_rid2sd(st);
  10024. if(sd==NULL || sd->pd==NULL)
  10025. return 0;
  10026. pd=sd->pd;
  10027. if (pd->recovery)
  10028. { //Halt previous bonus
  10029. if (pd->recovery->timer != INVALID_TIMER)
  10030. delete_timer(pd->recovery->timer, pet_recovery_timer);
  10031. } else //Init
  10032. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  10033. pd->recovery->type = (sc_type)script_getnum(st,2);
  10034. pd->recovery->delay = script_getnum(st,3);
  10035. pd->recovery->timer = INVALID_TIMER;
  10036. return 0;
  10037. }
  10038. /*==========================================
  10039. * pet healing [Valaris] //Rewritten by [Skotlex]
  10040. *------------------------------------------*/
  10041. BUILDIN_FUNC(petheal)
  10042. {
  10043. struct pet_data *pd;
  10044. TBL_PC *sd=script_rid2sd(st);
  10045. if(sd==NULL || sd->pd==NULL)
  10046. return 0;
  10047. pd=sd->pd;
  10048. if (pd->s_skill)
  10049. { //Clear previous skill
  10050. if (pd->s_skill->timer != INVALID_TIMER)
  10051. {
  10052. if (pd->s_skill->id)
  10053. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10054. else
  10055. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10056. }
  10057. } else //init memory
  10058. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10059. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  10060. //Use the lv as the amount to heal
  10061. pd->s_skill->lv=script_getnum(st,2);
  10062. pd->s_skill->delay=script_getnum(st,3);
  10063. pd->s_skill->hp=script_getnum(st,4);
  10064. pd->s_skill->sp=script_getnum(st,5);
  10065. //Use delay as initial offset to avoid skill/heal exploits
  10066. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10067. pd->s_skill->timer = INVALID_TIMER;
  10068. else
  10069. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  10070. return 0;
  10071. }
  10072. /*==========================================
  10073. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  10074. *------------------------------------------*/
  10075. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  10076. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  10077. BUILDIN_FUNC(petskillattack)
  10078. {
  10079. struct pet_data *pd;
  10080. TBL_PC *sd=script_rid2sd(st);
  10081. if(sd==NULL || sd->pd==NULL)
  10082. return 0;
  10083. pd=sd->pd;
  10084. if (pd->a_skill == NULL)
  10085. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10086. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10087. pd->a_skill->lv=script_getnum(st,3);
  10088. pd->a_skill->div_ = 0;
  10089. pd->a_skill->rate=script_getnum(st,4);
  10090. pd->a_skill->bonusrate=script_getnum(st,5);
  10091. return 0;
  10092. }
  10093. /*==========================================
  10094. * pet attack skills [Valaris]
  10095. *------------------------------------------*/
  10096. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  10097. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  10098. BUILDIN_FUNC(petskillattack2)
  10099. {
  10100. struct pet_data *pd;
  10101. TBL_PC *sd=script_rid2sd(st);
  10102. if(sd==NULL || sd->pd==NULL)
  10103. return 0;
  10104. pd=sd->pd;
  10105. if (pd->a_skill == NULL)
  10106. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10107. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10108. pd->a_skill->lv=script_getnum(st,3);
  10109. pd->a_skill->div_ = script_getnum(st,4);
  10110. pd->a_skill->rate=script_getnum(st,5);
  10111. pd->a_skill->bonusrate=script_getnum(st,6);
  10112. return 0;
  10113. }
  10114. /*==========================================
  10115. * pet support skills [Skotlex]
  10116. *------------------------------------------*/
  10117. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  10118. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  10119. BUILDIN_FUNC(petskillsupport)
  10120. {
  10121. struct pet_data *pd;
  10122. TBL_PC *sd=script_rid2sd(st);
  10123. if(sd==NULL || sd->pd==NULL)
  10124. return 0;
  10125. pd=sd->pd;
  10126. if (pd->s_skill)
  10127. { //Clear previous skill
  10128. if (pd->s_skill->timer != INVALID_TIMER)
  10129. {
  10130. if (pd->s_skill->id)
  10131. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10132. else
  10133. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10134. }
  10135. } else //init memory
  10136. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10137. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10138. pd->s_skill->lv=script_getnum(st,3);
  10139. pd->s_skill->delay=script_getnum(st,4);
  10140. pd->s_skill->hp=script_getnum(st,5);
  10141. pd->s_skill->sp=script_getnum(st,6);
  10142. //Use delay as initial offset to avoid skill/heal exploits
  10143. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10144. pd->s_skill->timer = INVALID_TIMER;
  10145. else
  10146. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  10147. return 0;
  10148. }
  10149. /*==========================================
  10150. * Scripted skill effects [Celest]
  10151. *------------------------------------------*/
  10152. /// skilleffect <skill id>,<level>
  10153. /// skilleffect "<skill name>",<level>
  10154. BUILDIN_FUNC(skilleffect)
  10155. {
  10156. TBL_PC *sd;
  10157. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10158. int skilllv=script_getnum(st,3);
  10159. sd=script_rid2sd(st);
  10160. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  10161. return 0;
  10162. }
  10163. /*==========================================
  10164. * NPC skill effects [Valaris]
  10165. *------------------------------------------*/
  10166. /// npcskilleffect <skill id>,<level>,<x>,<y>
  10167. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  10168. BUILDIN_FUNC(npcskilleffect)
  10169. {
  10170. struct block_list *bl= map_id2bl(st->oid);
  10171. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10172. int skilllv=script_getnum(st,3);
  10173. int x=script_getnum(st,4);
  10174. int y=script_getnum(st,5);
  10175. if (bl)
  10176. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  10177. return 0;
  10178. }
  10179. /*==========================================
  10180. * Special effects [Valaris]
  10181. *------------------------------------------*/
  10182. BUILDIN_FUNC(specialeffect)
  10183. {
  10184. struct block_list *bl=map_id2bl(st->oid);
  10185. int type = script_getnum(st,2);
  10186. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10187. if(bl==NULL)
  10188. return 0;
  10189. if( script_hasdata(st,4) )
  10190. {
  10191. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10192. if(nd)
  10193. clif_specialeffect(&nd->bl, type, target);
  10194. }
  10195. else
  10196. {
  10197. if (target == SELF) {
  10198. TBL_PC *sd=script_rid2sd(st);
  10199. if (sd)
  10200. clif_specialeffect_single(bl,type,sd->fd);
  10201. } else {
  10202. clif_specialeffect(bl, type, target);
  10203. }
  10204. }
  10205. return 0;
  10206. }
  10207. BUILDIN_FUNC(specialeffect2)
  10208. {
  10209. TBL_PC *sd=script_rid2sd(st);
  10210. int type = script_getnum(st,2);
  10211. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10212. if( script_hasdata(st,4) )
  10213. sd = map_nick2sd(script_getstr(st,4));
  10214. if (sd)
  10215. clif_specialeffect(&sd->bl, type, target);
  10216. return 0;
  10217. }
  10218. /*==========================================
  10219. * Nude [Valaris]
  10220. *------------------------------------------*/
  10221. BUILDIN_FUNC(nude)
  10222. {
  10223. TBL_PC *sd=script_rid2sd(st);
  10224. int i,calcflag=0;
  10225. if(sd==NULL)
  10226. return 0;
  10227. for(i=0;i<11;i++)
  10228. if(sd->equip_index[i] >= 0) {
  10229. if(!calcflag)
  10230. calcflag=1;
  10231. pc_unequipitem(sd,sd->equip_index[i],2);
  10232. }
  10233. if(calcflag)
  10234. status_calc_pc(sd,0);
  10235. return 0;
  10236. }
  10237. /*==========================================
  10238. * gmcommand [MouseJstr]
  10239. *------------------------------------------*/
  10240. BUILDIN_FUNC(atcommand)
  10241. {
  10242. TBL_PC dummy_sd;
  10243. TBL_PC* sd;
  10244. int fd;
  10245. const char* cmd;
  10246. cmd = script_getstr(st,2);
  10247. if (st->rid) {
  10248. sd = script_rid2sd(st);
  10249. fd = sd->fd;
  10250. } else { //Use a dummy character.
  10251. sd = &dummy_sd;
  10252. fd = 0;
  10253. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10254. if (st->oid)
  10255. {
  10256. struct block_list* bl = map_id2bl(st->oid);
  10257. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10258. if (bl->type == BL_NPC)
  10259. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10260. }
  10261. }
  10262. if (!is_atcommand(fd, sd, cmd, 0)) {
  10263. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  10264. script_reportsrc(st);
  10265. return 1;
  10266. }
  10267. return 0;
  10268. }
  10269. /*==========================================
  10270. * Displays a message for the player only (like system messages like "you got an apple" )
  10271. *------------------------------------------*/
  10272. BUILDIN_FUNC(dispbottom)
  10273. {
  10274. TBL_PC *sd=script_rid2sd(st);
  10275. const char *message;
  10276. message=script_getstr(st,2);
  10277. if(sd)
  10278. clif_disp_onlyself(sd,message,(int)strlen(message));
  10279. return 0;
  10280. }
  10281. /*==========================================
  10282. * All The Players Full Recovery
  10283. * (HP/SP full restore and resurrect if need)
  10284. *------------------------------------------*/
  10285. BUILDIN_FUNC(recovery)
  10286. {
  10287. TBL_PC* sd;
  10288. struct s_mapiterator* iter;
  10289. iter = mapit_getallusers();
  10290. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10291. {
  10292. if(pc_isdead(sd))
  10293. status_revive(&sd->bl, 100, 100);
  10294. else
  10295. status_percent_heal(&sd->bl, 100, 100);
  10296. clif_displaymessage(sd->fd,"You have been recovered!");
  10297. }
  10298. mapit_free(iter);
  10299. return 0;
  10300. }
  10301. /*==========================================
  10302. * Get your pet info: getpetinfo(n)
  10303. * n -> 0:pet_id 1:pet_class 2:pet_name
  10304. * 3:friendly 4:hungry, 5: rename flag.
  10305. *------------------------------------------*/
  10306. BUILDIN_FUNC(getpetinfo)
  10307. {
  10308. TBL_PC *sd=script_rid2sd(st);
  10309. TBL_PET *pd;
  10310. int type=script_getnum(st,2);
  10311. if(!sd || !sd->pd) {
  10312. if (type == 2)
  10313. script_pushconststr(st,"null");
  10314. else
  10315. script_pushint(st,0);
  10316. return 0;
  10317. }
  10318. pd = sd->pd;
  10319. switch(type){
  10320. case 0: script_pushint(st,pd->pet.pet_id); break;
  10321. case 1: script_pushint(st,pd->pet.class_); break;
  10322. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10323. case 3: script_pushint(st,pd->pet.intimate); break;
  10324. case 4: script_pushint(st,pd->pet.hungry); break;
  10325. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10326. default:
  10327. script_pushint(st,0);
  10328. break;
  10329. }
  10330. return 0;
  10331. }
  10332. /*==========================================
  10333. * Get your homunculus info: gethominfo(n)
  10334. * n -> 0:hom_id 1:class 2:name
  10335. * 3:friendly 4:hungry, 5: rename flag.
  10336. * 6: level
  10337. *------------------------------------------*/
  10338. BUILDIN_FUNC(gethominfo)
  10339. {
  10340. TBL_PC *sd=script_rid2sd(st);
  10341. TBL_HOM *hd;
  10342. int type=script_getnum(st,2);
  10343. hd = sd?sd->hd:NULL;
  10344. if(!merc_is_hom_active(hd))
  10345. {
  10346. if (type == 2)
  10347. script_pushconststr(st,"null");
  10348. else
  10349. script_pushint(st,0);
  10350. return 0;
  10351. }
  10352. switch(type){
  10353. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10354. case 1: script_pushint(st,hd->homunculus.class_); break;
  10355. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10356. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10357. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10358. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10359. case 6: script_pushint(st,hd->homunculus.level); break;
  10360. default:
  10361. script_pushint(st,0);
  10362. break;
  10363. }
  10364. return 0;
  10365. }
  10366. /// Retrieves information about character's mercenary
  10367. /// getmercinfo <type>[,<char id>];
  10368. BUILDIN_FUNC(getmercinfo)
  10369. {
  10370. int type, char_id;
  10371. struct map_session_data* sd;
  10372. struct mercenary_data* md;
  10373. type = script_getnum(st,2);
  10374. if( script_hasdata(st,3) )
  10375. {
  10376. char_id = script_getnum(st,3);
  10377. if( ( sd = map_charid2sd(char_id) ) == NULL )
  10378. {
  10379. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  10380. script_pushnil(st);
  10381. return 1;
  10382. }
  10383. }
  10384. else
  10385. {
  10386. if( ( sd = script_rid2sd(st) ) == NULL )
  10387. {
  10388. script_pushnil(st);
  10389. return 0;
  10390. }
  10391. }
  10392. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  10393. switch( type )
  10394. {
  10395. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  10396. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  10397. case 2:
  10398. if( md )
  10399. script_pushstrcopy(st,md->db->name);
  10400. else
  10401. script_pushconststr(st,"");
  10402. break;
  10403. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  10404. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  10405. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  10406. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  10407. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  10408. default:
  10409. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  10410. script_pushnil(st);
  10411. return 1;
  10412. }
  10413. return 0;
  10414. }
  10415. /*==========================================
  10416. * Shows wether your inventory(and equips) contain
  10417. selected card or not.
  10418. checkequipedcard(4001);
  10419. *------------------------------------------*/
  10420. BUILDIN_FUNC(checkequipedcard)
  10421. {
  10422. TBL_PC *sd=script_rid2sd(st);
  10423. if(sd){
  10424. int n,i,c=0;
  10425. c=script_getnum(st,2);
  10426. for(i=0;i<MAX_INVENTORY;i++){
  10427. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10428. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10429. continue;
  10430. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10431. if(sd->status.inventory[i].card[n]==c){
  10432. script_pushint(st,1);
  10433. return 0;
  10434. }
  10435. }
  10436. }
  10437. }
  10438. }
  10439. script_pushint(st,0);
  10440. return 0;
  10441. }
  10442. BUILDIN_FUNC(jump_zero)
  10443. {
  10444. int sel;
  10445. sel=script_getnum(st,2);
  10446. if(!sel) {
  10447. int pos;
  10448. if( !data_islabel(script_getdata(st,3)) ){
  10449. ShowError("script: jump_zero: not label !\n");
  10450. st->state=END;
  10451. return 1;
  10452. }
  10453. pos=script_getnum(st,3);
  10454. st->pos=pos;
  10455. st->state=GOTO;
  10456. }
  10457. return 0;
  10458. }
  10459. /*==========================================
  10460. * movenpc [MouseJstr]
  10461. *------------------------------------------*/
  10462. BUILDIN_FUNC(movenpc)
  10463. {
  10464. TBL_NPC *nd = NULL;
  10465. const char *npc;
  10466. int x,y;
  10467. npc = script_getstr(st,2);
  10468. x = script_getnum(st,3);
  10469. y = script_getnum(st,4);
  10470. if ((nd = npc_name2id(npc)) == NULL)
  10471. return -1;
  10472. if (script_hasdata(st,5))
  10473. nd->ud.dir = script_getnum(st,5) % 8;
  10474. npc_movenpc(nd, x, y);
  10475. return 0;
  10476. }
  10477. /*==========================================
  10478. * message [MouseJstr]
  10479. *------------------------------------------*/
  10480. BUILDIN_FUNC(message)
  10481. {
  10482. const char *msg,*player;
  10483. TBL_PC *pl_sd = NULL;
  10484. player = script_getstr(st,2);
  10485. msg = script_getstr(st,3);
  10486. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10487. return 0;
  10488. clif_displaymessage(pl_sd->fd, msg);
  10489. return 0;
  10490. }
  10491. /*==========================================
  10492. * npctalk (sends message to surrounding area)
  10493. *------------------------------------------*/
  10494. BUILDIN_FUNC(npctalk)
  10495. {
  10496. const char* str;
  10497. char name[NAME_LENGTH], message[256];
  10498. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10499. str = script_getstr(st,2);
  10500. if(nd)
  10501. {
  10502. safestrncpy(name, nd->name, sizeof(name));
  10503. strtok(name, "#"); // discard extra name identifier if present
  10504. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  10505. clif_message(&nd->bl, message);
  10506. }
  10507. return 0;
  10508. }
  10509. // change npc walkspeed [Valaris]
  10510. BUILDIN_FUNC(npcspeed)
  10511. {
  10512. struct npc_data* nd;
  10513. int speed;
  10514. speed = script_getnum(st,2);
  10515. nd =(struct npc_data *)map_id2bl(st->oid);
  10516. if( nd )
  10517. {
  10518. nd->speed = speed;
  10519. nd->ud.state.speed_changed = 1;
  10520. }
  10521. return 0;
  10522. }
  10523. // make an npc walk to a position [Valaris]
  10524. BUILDIN_FUNC(npcwalkto)
  10525. {
  10526. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10527. int x=0,y=0;
  10528. x=script_getnum(st,2);
  10529. y=script_getnum(st,3);
  10530. if(nd) {
  10531. unit_walktoxy(&nd->bl,x,y,0);
  10532. }
  10533. return 0;
  10534. }
  10535. // stop an npc's movement [Valaris]
  10536. BUILDIN_FUNC(npcstop)
  10537. {
  10538. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10539. if(nd) {
  10540. unit_stop_walking(&nd->bl,1|4);
  10541. }
  10542. return 0;
  10543. }
  10544. /*==========================================
  10545. * getlook char info. getlook(arg)
  10546. *------------------------------------------*/
  10547. BUILDIN_FUNC(getlook)
  10548. {
  10549. int type,val;
  10550. TBL_PC *sd;
  10551. sd=script_rid2sd(st);
  10552. type=script_getnum(st,2);
  10553. val=-1;
  10554. switch(type) {
  10555. case LOOK_HAIR: val=sd->status.hair; break; //1
  10556. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10557. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10558. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10559. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10560. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10561. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10562. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10563. case LOOK_SHOES: break; //9
  10564. }
  10565. script_pushint(st,val);
  10566. return 0;
  10567. }
  10568. /*==========================================
  10569. * get char save point. argument: 0- map name, 1- x, 2- y
  10570. *------------------------------------------*/
  10571. BUILDIN_FUNC(getsavepoint)
  10572. {
  10573. TBL_PC* sd;
  10574. int type;
  10575. sd = script_rid2sd(st);
  10576. if (sd == NULL) {
  10577. script_pushint(st,0);
  10578. return 0;
  10579. }
  10580. type = script_getnum(st,2);
  10581. switch(type) {
  10582. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10583. case 1: script_pushint(st,sd->status.save_point.x); break;
  10584. case 2: script_pushint(st,sd->status.save_point.y); break;
  10585. default:
  10586. script_pushint(st,0);
  10587. break;
  10588. }
  10589. return 0;
  10590. }
  10591. /*==========================================
  10592. * Get position for char/npc/pet/mob objects. Added by Lorky
  10593. *
  10594. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10595. * where type:
  10596. * MapName$ - String variable for output map name
  10597. * MapX - Integer variable for output coord X
  10598. * MapY - Integer variable for output coord Y
  10599. * type - type of object
  10600. * 0 - Character coord
  10601. * 1 - NPC coord
  10602. * 2 - Pet coord
  10603. * 3 - Mob coord (not released)
  10604. * 4 - Homun coord
  10605. * CharName$ - Name object. If miss or "this" the current object
  10606. *
  10607. * Return:
  10608. * 0 - success
  10609. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10610. *------------------------------------------*/
  10611. BUILDIN_FUNC(getmapxy)
  10612. {
  10613. struct block_list *bl = NULL;
  10614. TBL_PC *sd=NULL;
  10615. int num;
  10616. const char *name;
  10617. char prefix;
  10618. int x,y,type;
  10619. char mapname[MAP_NAME_LENGTH];
  10620. if( !data_isreference(script_getdata(st,2)) ){
  10621. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10622. script_pushint(st,-1);
  10623. return 1;
  10624. }
  10625. if( !data_isreference(script_getdata(st,3)) ){
  10626. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10627. script_pushint(st,-1);
  10628. return 1;
  10629. }
  10630. if( !data_isreference(script_getdata(st,4)) ){
  10631. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10632. script_pushint(st,-1);
  10633. return 1;
  10634. }
  10635. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10636. type=script_getnum(st,5);
  10637. switch (type){
  10638. case 0: //Get Character Position
  10639. if( script_hasdata(st,6) )
  10640. sd=map_nick2sd(script_getstr(st,6));
  10641. else
  10642. sd=script_rid2sd(st);
  10643. if (sd)
  10644. bl = &sd->bl;
  10645. break;
  10646. case 1: //Get NPC Position
  10647. if( script_hasdata(st,6) )
  10648. {
  10649. struct npc_data *nd;
  10650. nd=npc_name2id(script_getstr(st,6));
  10651. if (nd)
  10652. bl = &nd->bl;
  10653. } else //In case the origin is not an npc?
  10654. bl=map_id2bl(st->oid);
  10655. break;
  10656. case 2: //Get Pet Position
  10657. if(script_hasdata(st,6))
  10658. sd=map_nick2sd(script_getstr(st,6));
  10659. else
  10660. sd=script_rid2sd(st);
  10661. if (sd && sd->pd)
  10662. bl = &sd->pd->bl;
  10663. break;
  10664. case 3: //Get Mob Position
  10665. break; //Not supported?
  10666. case 4: //Get Homun Position
  10667. if(script_hasdata(st,6))
  10668. sd=map_nick2sd(script_getstr(st,6));
  10669. else
  10670. sd=script_rid2sd(st);
  10671. if (sd && sd->hd)
  10672. bl = &sd->hd->bl;
  10673. break;
  10674. default:
  10675. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  10676. script_pushint(st,-1);
  10677. return 1;
  10678. }
  10679. if (!bl) { //No object found.
  10680. script_pushint(st,-1);
  10681. return 0;
  10682. }
  10683. x= bl->x;
  10684. y= bl->y;
  10685. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10686. //Set MapName$
  10687. num=st->stack->stack_data[st->start+2].u.num;
  10688. name=get_str(num&0x00ffffff);
  10689. prefix=*name;
  10690. if(not_server_variable(prefix))
  10691. sd=script_rid2sd(st);
  10692. else
  10693. sd=NULL;
  10694. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10695. //Set MapX
  10696. num=st->stack->stack_data[st->start+3].u.num;
  10697. name=get_str(num&0x00ffffff);
  10698. prefix=*name;
  10699. if(not_server_variable(prefix))
  10700. sd=script_rid2sd(st);
  10701. else
  10702. sd=NULL;
  10703. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  10704. //Set MapY
  10705. num=st->stack->stack_data[st->start+4].u.num;
  10706. name=get_str(num&0x00ffffff);
  10707. prefix=*name;
  10708. if(not_server_variable(prefix))
  10709. sd=script_rid2sd(st);
  10710. else
  10711. sd=NULL;
  10712. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  10713. //Return Success value
  10714. script_pushint(st,0);
  10715. return 0;
  10716. }
  10717. /*==========================================
  10718. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10719. *------------------------------------------*/
  10720. BUILDIN_FUNC(logmes)
  10721. {
  10722. const char *str;
  10723. TBL_PC* sd;
  10724. sd = script_rid2sd(st);
  10725. if( sd == NULL )
  10726. return 1;
  10727. str = script_getstr(st,2);
  10728. log_npc(sd,str);
  10729. return 0;
  10730. }
  10731. BUILDIN_FUNC(summon)
  10732. {
  10733. int _class, timeout=0;
  10734. const char *str,*event="";
  10735. TBL_PC *sd;
  10736. struct mob_data *md;
  10737. int tick = gettick();
  10738. sd=script_rid2sd(st);
  10739. if (!sd) return 0;
  10740. str =script_getstr(st,2);
  10741. _class=script_getnum(st,3);
  10742. if( script_hasdata(st,4) )
  10743. timeout=script_getnum(st,4);
  10744. if( script_hasdata(st,5) ){
  10745. event=script_getstr(st,5);
  10746. check_event(st, event);
  10747. }
  10748. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10749. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10750. if (md) {
  10751. md->master_id=sd->bl.id;
  10752. md->special_state.ai=1;
  10753. if( md->deletetimer != INVALID_TIMER )
  10754. delete_timer(md->deletetimer, mob_timer_delete);
  10755. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10756. mob_spawn (md); //Now it is ready for spawning.
  10757. clif_specialeffect(&md->bl,344,AREA);
  10758. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10759. }
  10760. return 0;
  10761. }
  10762. /*==========================================
  10763. * Checks whether it is daytime/nighttime
  10764. *------------------------------------------*/
  10765. BUILDIN_FUNC(isnight)
  10766. {
  10767. script_pushint(st,(night_flag == 1));
  10768. return 0;
  10769. }
  10770. BUILDIN_FUNC(isday)
  10771. {
  10772. script_pushint(st,(night_flag == 0));
  10773. return 0;
  10774. }
  10775. /*================================================
  10776. * Check how many items/cards in the list are
  10777. * equipped - used for 2/15's cards patch [celest]
  10778. *------------------------------------------------*/
  10779. BUILDIN_FUNC(isequippedcnt)
  10780. {
  10781. TBL_PC *sd;
  10782. int i, j, k, id = 1;
  10783. int ret = 0;
  10784. sd = script_rid2sd(st);
  10785. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10786. script_pushint(st,0);
  10787. return 0;
  10788. }
  10789. for (i=0; id!=0; i++) {
  10790. FETCH (i+2, id) else id = 0;
  10791. if (id <= 0)
  10792. continue;
  10793. for (j=0; j<EQI_MAX; j++) {
  10794. int index;
  10795. index = sd->equip_index[j];
  10796. if(index < 0) continue;
  10797. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10798. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10799. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10800. if(!sd->inventory_data[index])
  10801. continue;
  10802. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10803. if (sd->inventory_data[index]->nameid == id)
  10804. ret+= sd->status.inventory[index].amount;
  10805. } else { //Count cards.
  10806. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10807. continue; //No cards
  10808. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10809. if (sd->status.inventory[index].card[k] == id)
  10810. ret++; //[Lupus]
  10811. }
  10812. }
  10813. }
  10814. }
  10815. script_pushint(st,ret);
  10816. return 0;
  10817. }
  10818. /*================================================
  10819. * Check whether another card has been
  10820. * equipped - used for 2/15's cards patch [celest]
  10821. * -- Items checked cannot be reused in another
  10822. * card set to prevent exploits
  10823. *------------------------------------------------*/
  10824. BUILDIN_FUNC(isequipped)
  10825. {
  10826. TBL_PC *sd;
  10827. int i, j, k, id = 1;
  10828. int index, flag;
  10829. int ret = -1;
  10830. //Original hash to reverse it when full check fails.
  10831. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10832. sd = script_rid2sd(st);
  10833. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10834. script_pushint(st,0);
  10835. return 0;
  10836. }
  10837. setitem_hash = sd->setitem_hash;
  10838. setitem_hash2 = sd->setitem_hash2;
  10839. for (i=0; id!=0; i++)
  10840. {
  10841. FETCH (i+2, id) else id = 0;
  10842. if (id <= 0)
  10843. continue;
  10844. flag = 0;
  10845. for (j=0; j<EQI_MAX; j++)
  10846. {
  10847. index = sd->equip_index[j];
  10848. if(index < 0) continue;
  10849. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10850. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10851. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10852. if(!sd->inventory_data[index])
  10853. continue;
  10854. if (itemdb_type(id) != IT_CARD) {
  10855. if (sd->inventory_data[index]->nameid != id)
  10856. continue;
  10857. flag = 1;
  10858. break;
  10859. } else { //Cards
  10860. if (sd->inventory_data[index]->slot == 0 ||
  10861. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10862. continue;
  10863. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10864. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10865. unsigned int hash = 0;
  10866. if (sd->status.inventory[index].card[k] != id)
  10867. continue;
  10868. hash = 1<<((j<5?j:j-5)*4 + k);
  10869. // check if card is already used by another set
  10870. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10871. continue;
  10872. // We have found a match
  10873. flag = 1;
  10874. // Set hash so this card cannot be used by another
  10875. if (j<5)
  10876. sd->setitem_hash |= hash;
  10877. else
  10878. sd->setitem_hash2 |= hash;
  10879. break;
  10880. }
  10881. }
  10882. if (flag) break; //Card found
  10883. }
  10884. if (ret == -1)
  10885. ret = flag;
  10886. else
  10887. ret &= flag;
  10888. if (!ret) break;
  10889. }
  10890. if (!ret)
  10891. { //When check fails, restore original hash values. [Skotlex]
  10892. sd->setitem_hash = setitem_hash;
  10893. sd->setitem_hash2 = setitem_hash2;
  10894. }
  10895. script_pushint(st,ret);
  10896. return 0;
  10897. }
  10898. /*================================================
  10899. * Check how many given inserted cards in the CURRENT
  10900. * weapon - used for 2/15's cards patch [Lupus]
  10901. *------------------------------------------------*/
  10902. BUILDIN_FUNC(cardscnt)
  10903. {
  10904. TBL_PC *sd;
  10905. int i, k, id = 1;
  10906. int ret = 0;
  10907. int index;
  10908. sd = script_rid2sd(st);
  10909. for (i=0; id!=0; i++) {
  10910. FETCH (i+2, id) else id = 0;
  10911. if (id <= 0)
  10912. continue;
  10913. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10914. if(index < 0) continue;
  10915. if(!sd->inventory_data[index])
  10916. continue;
  10917. if(itemdb_type(id) != IT_CARD) {
  10918. if (sd->inventory_data[index]->nameid == id)
  10919. ret+= sd->status.inventory[index].amount;
  10920. } else {
  10921. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10922. continue;
  10923. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10924. if (sd->status.inventory[index].card[k] == id)
  10925. ret++;
  10926. }
  10927. }
  10928. }
  10929. script_pushint(st,ret);
  10930. // script_pushint(st,current_equip_item_index);
  10931. return 0;
  10932. }
  10933. /*=======================================================
  10934. * Returns the refined number of the current item, or an
  10935. * item with inventory index specified
  10936. *-------------------------------------------------------*/
  10937. BUILDIN_FUNC(getrefine)
  10938. {
  10939. TBL_PC *sd;
  10940. if ((sd = script_rid2sd(st))!= NULL)
  10941. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10942. else
  10943. script_pushint(st,0);
  10944. return 0;
  10945. }
  10946. /*=======================================================
  10947. * Day/Night controls
  10948. *-------------------------------------------------------*/
  10949. BUILDIN_FUNC(night)
  10950. {
  10951. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10952. return 0;
  10953. }
  10954. BUILDIN_FUNC(day)
  10955. {
  10956. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10957. return 0;
  10958. }
  10959. //=======================================================
  10960. // Unequip [Spectre]
  10961. //-------------------------------------------------------
  10962. BUILDIN_FUNC(unequip)
  10963. {
  10964. int i;
  10965. size_t num;
  10966. TBL_PC *sd;
  10967. num = script_getnum(st,2);
  10968. sd = script_rid2sd(st);
  10969. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10970. {
  10971. i = pc_checkequip(sd,equip[num-1]);
  10972. if (i >= 0)
  10973. pc_unequipitem(sd,i,1|2);
  10974. }
  10975. return 0;
  10976. }
  10977. BUILDIN_FUNC(equip)
  10978. {
  10979. int nameid=0,i;
  10980. TBL_PC *sd;
  10981. struct item_data *item_data;
  10982. sd = script_rid2sd(st);
  10983. nameid=script_getnum(st,2);
  10984. if((item_data = itemdb_exists(nameid)) == NULL)
  10985. {
  10986. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10987. return 1;
  10988. }
  10989. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10990. if( i < MAX_INVENTORY )
  10991. pc_equipitem(sd,i,item_data->equip);
  10992. return 0;
  10993. }
  10994. BUILDIN_FUNC(autoequip)
  10995. {
  10996. int nameid, flag;
  10997. struct item_data *item_data;
  10998. nameid=script_getnum(st,2);
  10999. flag=script_getnum(st,3);
  11000. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  11001. {
  11002. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  11003. return 1;
  11004. }
  11005. if( !itemdb_isequip2(item_data) )
  11006. {
  11007. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  11008. return 1;
  11009. }
  11010. item_data->flag.autoequip = flag>0?1:0;
  11011. return 0;
  11012. }
  11013. BUILDIN_FUNC(setbattleflag)
  11014. {
  11015. const char *flag, *value;
  11016. flag = script_getstr(st,2);
  11017. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  11018. if (battle_set_value(flag, value) == 0)
  11019. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  11020. else
  11021. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  11022. return 0;
  11023. }
  11024. BUILDIN_FUNC(getbattleflag)
  11025. {
  11026. const char *flag;
  11027. flag = script_getstr(st,2);
  11028. script_pushint(st,battle_get_value(flag));
  11029. return 0;
  11030. }
  11031. //=======================================================
  11032. // strlen [Valaris]
  11033. //-------------------------------------------------------
  11034. BUILDIN_FUNC(getstrlen)
  11035. {
  11036. const char *str = script_getstr(st,2);
  11037. int len = (str) ? (int)strlen(str) : 0;
  11038. script_pushint(st,len);
  11039. return 0;
  11040. }
  11041. //=======================================================
  11042. // isalpha [Valaris]
  11043. //-------------------------------------------------------
  11044. BUILDIN_FUNC(charisalpha)
  11045. {
  11046. const char *str=script_getstr(st,2);
  11047. int pos=script_getnum(st,3);
  11048. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  11049. script_pushint(st,val);
  11050. return 0;
  11051. }
  11052. //=======================================================
  11053. // charisupper <str>, <index>
  11054. //-------------------------------------------------------
  11055. BUILDIN_FUNC(charisupper)
  11056. {
  11057. const char *str = script_getstr(st,2);
  11058. int pos = script_getnum(st,3);
  11059. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  11060. script_pushint(st,val);
  11061. return 0;
  11062. }
  11063. //=======================================================
  11064. // charislower <str>, <index>
  11065. //-------------------------------------------------------
  11066. BUILDIN_FUNC(charislower)
  11067. {
  11068. const char *str = script_getstr(st,2);
  11069. int pos = script_getnum(st,3);
  11070. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  11071. script_pushint(st,val);
  11072. return 0;
  11073. }
  11074. //=======================================================
  11075. // charat <str>, <index>
  11076. //-------------------------------------------------------
  11077. BUILDIN_FUNC(charat)
  11078. {
  11079. const char *str = script_getstr(st,2);
  11080. int pos = script_getnum(st,3);
  11081. char *output;
  11082. output = (char*)aMalloc(2*sizeof(char));
  11083. output[0] = '\0';
  11084. if(str && pos >= 0 && (unsigned int)pos < strlen(str))
  11085. sprintf(output, "%c", str[pos]);
  11086. script_pushstr(st, output);
  11087. return 0;
  11088. }
  11089. //=======================================================
  11090. // setchar <string>, <char>, <index>
  11091. //-------------------------------------------------------
  11092. BUILDIN_FUNC(setchar)
  11093. {
  11094. const char *str = script_getstr(st,2);
  11095. const char *c = script_getstr(st,3);
  11096. int index = script_getnum(st,4);
  11097. char *output = aStrdup(str);
  11098. if(index >= 0 && index < strlen(output))
  11099. output[index] = *c;
  11100. script_pushstr(st, output);
  11101. return 0;
  11102. }
  11103. //=======================================================
  11104. // insertchar <string>, <char>, <index>
  11105. //-------------------------------------------------------
  11106. BUILDIN_FUNC(insertchar)
  11107. {
  11108. const char *str = script_getstr(st,2);
  11109. const char *c = script_getstr(st,3);
  11110. int index = script_getnum(st,4);
  11111. char *output;
  11112. size_t len = strlen(str);
  11113. if(index < 0)
  11114. index = 0;
  11115. else if(index > len)
  11116. index = len;
  11117. output = (char*)aMalloc(len + 2);
  11118. memcpy(output, str, index);
  11119. output[index] = c[0];
  11120. memcpy(&output[index+1], &str[index], len - index);
  11121. output[len+1] = '\0';
  11122. script_pushstr(st, output);
  11123. return 0;
  11124. }
  11125. //=======================================================
  11126. // delchar <string>, <index>
  11127. //-------------------------------------------------------
  11128. BUILDIN_FUNC(delchar)
  11129. {
  11130. const char *str = script_getstr(st,2);
  11131. int index = script_getnum(st,3);
  11132. char *output;
  11133. size_t len = strlen(str);
  11134. if(index < 0 || index > len) {
  11135. //return original
  11136. output = aStrdup(str);
  11137. script_pushstr(st, output);
  11138. return 0;
  11139. }
  11140. output = (char*)aMalloc(len);
  11141. memcpy(output, str, index);
  11142. memcpy(&output[index], &str[index+1], len - index);
  11143. script_pushstr(st, output);
  11144. return 0;
  11145. }
  11146. //=======================================================
  11147. // strtoupper <str>
  11148. //-------------------------------------------------------
  11149. BUILDIN_FUNC(strtoupper)
  11150. {
  11151. const char *str = script_getstr(st,2);
  11152. char *output = aStrdup(str);
  11153. char *cursor = output;
  11154. while (*cursor != '\0') {
  11155. *cursor = TOUPPER(*cursor);
  11156. cursor++;
  11157. }
  11158. script_pushstr(st, output);
  11159. return 0;
  11160. }
  11161. //=======================================================
  11162. // strtolower <str>
  11163. //-------------------------------------------------------
  11164. BUILDIN_FUNC(strtolower)
  11165. {
  11166. const char *str = script_getstr(st,2);
  11167. char *output = aStrdup(str);
  11168. char *cursor = output;
  11169. while (*cursor != '\0') {
  11170. *cursor = TOLOWER(*cursor);
  11171. cursor++;
  11172. }
  11173. script_pushstr(st, output);
  11174. return 0;
  11175. }
  11176. //=======================================================
  11177. // substr <str>, <start>, <end>
  11178. //-------------------------------------------------------
  11179. BUILDIN_FUNC(substr)
  11180. {
  11181. const char *str = script_getstr(st,2);
  11182. char *output;
  11183. int start = script_getnum(st,3);
  11184. int end = script_getnum(st,4);
  11185. int len = 0;
  11186. if(start >= 0 && end < strlen(str) && start <= end) {
  11187. len = end - start + 1;
  11188. output = (char*)aMalloc(len + 1);
  11189. memcpy(output, &str[start], len);
  11190. } else
  11191. output = (char*)aMalloc(1);
  11192. output[len] = '\0';
  11193. script_pushstr(st, output);
  11194. return 0;
  11195. }
  11196. //=======================================================
  11197. // explode <dest_string_array>, <str>, <delimiter>
  11198. // Note: delimiter is limited to 1 char
  11199. //-------------------------------------------------------
  11200. BUILDIN_FUNC(explode)
  11201. {
  11202. struct script_data* data = script_getdata(st, 2);
  11203. const char *str = script_getstr(st,3);
  11204. const char delimiter = script_getstr(st, 4)[0];
  11205. int32 id;
  11206. size_t len = strlen(str);
  11207. int i = 0, j = 0;
  11208. int start;
  11209. char *temp;
  11210. const char* name;
  11211. TBL_PC* sd = NULL;
  11212. temp = (char*)aMalloc(len + 1);
  11213. if( !data_isreference(data) )
  11214. {
  11215. ShowError("script:explode: not a variable\n");
  11216. script_reportdata(data);
  11217. st->state = END;
  11218. return 1;// not a variable
  11219. }
  11220. id = reference_getid(data);
  11221. start = reference_getindex(data);
  11222. name = reference_getname(data);
  11223. if( not_array_variable(*name) )
  11224. {
  11225. ShowError("script:explode: illegal scope\n");
  11226. script_reportdata(data);
  11227. st->state = END;
  11228. return 1;// not supported
  11229. }
  11230. if( !is_string_variable(name) )
  11231. {
  11232. ShowError("script:explode: not string array\n");
  11233. script_reportdata(data);
  11234. st->state = END;
  11235. return 1;// data type mismatch
  11236. }
  11237. if( not_server_variable(*name) )
  11238. {
  11239. sd = script_rid2sd(st);
  11240. if( sd == NULL )
  11241. return 0;// no player attached
  11242. }
  11243. while(str[i] != '\0') {
  11244. if(str[i] == delimiter && start < 127) { //break at delimiter but ignore after reaching last array index
  11245. temp[j] = '\0';
  11246. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  11247. j = 0;
  11248. ++i;
  11249. } else {
  11250. temp[j++] = str[i++];
  11251. }
  11252. }
  11253. //set last string
  11254. temp[j] = '\0';
  11255. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  11256. aFree(temp);
  11257. return 0;
  11258. }
  11259. //=======================================================
  11260. // implode <string_array>
  11261. // implode <string_array>, <glue>
  11262. //-------------------------------------------------------
  11263. BUILDIN_FUNC(implode)
  11264. {
  11265. struct script_data* data = script_getdata(st, 2);
  11266. const char *glue = NULL, *name, *temp;
  11267. int32 glue_len = 0, array_size, id;
  11268. size_t len = 0;
  11269. int i, k = 0;
  11270. TBL_PC* sd = NULL;
  11271. char *output;
  11272. if( !data_isreference(data) )
  11273. {
  11274. ShowError("script:implode: not a variable\n");
  11275. script_reportdata(data);
  11276. st->state = END;
  11277. return 1;// not a variable
  11278. }
  11279. id = reference_getid(data);
  11280. name = reference_getname(data);
  11281. if( not_array_variable(*name) )
  11282. {
  11283. ShowError("script:implode: illegal scope\n");
  11284. script_reportdata(data);
  11285. st->state = END;
  11286. return 1;// not supported
  11287. }
  11288. if( !is_string_variable(name) )
  11289. {
  11290. ShowError("script:implode: not string array\n");
  11291. script_reportdata(data);
  11292. st->state = END;
  11293. return 1;// data type mismatch
  11294. }
  11295. if( not_server_variable(*name) )
  11296. {
  11297. sd = script_rid2sd(st);
  11298. if( sd == NULL )
  11299. return 0;// no player attached
  11300. }
  11301. //count chars
  11302. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  11303. if(array_size == -1) //empty array check (AmsTaff)
  11304. {
  11305. ShowWarning("script:implode: array length = 0\n");
  11306. output = (char*)aMalloc(sizeof(char)*5);
  11307. sprintf(output,"%s","NULL");
  11308. } else {
  11309. for(i = 0; i <= array_size; ++i) {
  11310. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11311. len += strlen(temp);
  11312. script_removetop(st, -1, 0);
  11313. }
  11314. //allocate mem
  11315. if( script_hasdata(st,3) ) {
  11316. glue = script_getstr(st,3);
  11317. glue_len = strlen(glue);
  11318. len += glue_len * (array_size);
  11319. }
  11320. output = (char*)aMalloc(len + 1);
  11321. //build output
  11322. for(i = 0; i < array_size; ++i) {
  11323. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11324. len = strlen(temp);
  11325. memcpy(&output[k], temp, len);
  11326. k += len;
  11327. if(glue_len != 0) {
  11328. memcpy(&output[k], glue, glue_len);
  11329. k += glue_len;
  11330. }
  11331. script_removetop(st, -1, 0);
  11332. }
  11333. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  11334. len = strlen(temp);
  11335. memcpy(&output[k], temp, len);
  11336. k += len;
  11337. script_removetop(st, -1, 0);
  11338. output[k] = '\0';
  11339. }
  11340. script_pushstr(st, output);
  11341. return 0;
  11342. }
  11343. //=======================================================
  11344. // sprintf(<format>, ...);
  11345. // Implements C sprintf, except format %n. The resulting string is
  11346. // returned, instead of being saved in variable by reference.
  11347. //-------------------------------------------------------
  11348. BUILDIN_FUNC(sprintf)
  11349. {
  11350. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  11351. const char* format;
  11352. char* p;
  11353. char* q;
  11354. char* buf = NULL;
  11355. char* buf2 = NULL;
  11356. struct script_data* data;
  11357. StringBuf final_buf;
  11358. // Fetch init data
  11359. format = script_getstr(st, 2);
  11360. argc = script_lastdata(st)-2;
  11361. len = strlen(format);
  11362. // Skip parsing, where no parsing is required.
  11363. if(len==0){
  11364. script_pushconststr(st,"");
  11365. return 0;
  11366. }
  11367. // Pessimistic alloc
  11368. CREATE(buf, char, len+1);
  11369. // Need not be parsed, just solve stuff like %%.
  11370. if(argc==0){
  11371. memcpy(buf,format,len+1);
  11372. script_pushstrcopy(st, buf);
  11373. aFree(buf);
  11374. return 0;
  11375. }
  11376. safestrncpy(buf, format, len+1);
  11377. // Issue sprintf for each parameter
  11378. StringBuf_Init(&final_buf);
  11379. q = buf;
  11380. while((p = strchr(q, '%'))!=NULL){
  11381. if(p!=q){
  11382. len = p-q+1;
  11383. if(buf2_len<len){
  11384. RECREATE(buf2, char, len);
  11385. buf2_len = len;
  11386. }
  11387. safestrncpy(buf2, q, len);
  11388. StringBuf_AppendStr(&final_buf, buf2);
  11389. q = p;
  11390. }
  11391. p = q+1;
  11392. if(*p=='%'){ // %%
  11393. StringBuf_AppendStr(&final_buf, "%");
  11394. q+=2;
  11395. continue;
  11396. }
  11397. if(*p=='n'){ // %n
  11398. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  11399. script_reportsrc(st);
  11400. q+=2;
  11401. continue;
  11402. }
  11403. if(arg>=argc){
  11404. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  11405. if(buf) aFree(buf);
  11406. if(buf2) aFree(buf2);
  11407. StringBuf_Destroy(&final_buf);
  11408. script_pushconststr(st,"");
  11409. return 1;
  11410. }
  11411. if((p = strchr(q+1, '%'))==NULL){
  11412. p = strchr(q, 0); // EOS
  11413. }
  11414. len = p-q+1;
  11415. if(buf2_len<len){
  11416. RECREATE(buf2, char, len);
  11417. buf2_len = len;
  11418. }
  11419. safestrncpy(buf2, q, len);
  11420. q = p;
  11421. // Note: This assumes the passed value being the correct
  11422. // type to the current format specifier. If not, the server
  11423. // probably crashes or returns anything else, than expected,
  11424. // but it would behave in normal code the same way so it's
  11425. // the scripter's responsibility.
  11426. data = script_getdata(st, arg+3);
  11427. if(data_isstring(data)){ // String
  11428. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11429. }else if(data_isint(data)){ // Number
  11430. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11431. }else if(data_isreference(data)){ // Variable
  11432. char* name = reference_getname(data);
  11433. if(name[strlen(name)-1]=='$'){ // var Str
  11434. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11435. }else{ // var Int
  11436. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11437. }
  11438. }else{ // Unsupported type
  11439. ShowError("buildin_sprintf: Unknown argument type!\n");
  11440. if(buf) aFree(buf);
  11441. if(buf2) aFree(buf2);
  11442. StringBuf_Destroy(&final_buf);
  11443. script_pushconststr(st,"");
  11444. return 1;
  11445. }
  11446. arg++;
  11447. }
  11448. // Append anything left
  11449. if(*q){
  11450. StringBuf_AppendStr(&final_buf, q);
  11451. }
  11452. // Passed more, than needed
  11453. if(arg<argc){
  11454. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  11455. script_reportsrc(st);
  11456. }
  11457. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  11458. if(buf) aFree(buf);
  11459. if(buf2) aFree(buf2);
  11460. StringBuf_Destroy(&final_buf);
  11461. return 0;
  11462. }
  11463. //=======================================================
  11464. // sscanf(<str>, <format>, ...);
  11465. // Implements C sscanf.
  11466. //-------------------------------------------------------
  11467. BUILDIN_FUNC(sscanf){
  11468. unsigned int argc, arg = 0, len;
  11469. struct script_data* data;
  11470. struct map_session_data* sd = NULL;
  11471. const char* str;
  11472. const char* format;
  11473. const char* p;
  11474. const char* q;
  11475. char* buf = NULL;
  11476. char* buf_p;
  11477. char* ref_str = NULL;
  11478. int ref_int;
  11479. // Get data
  11480. str = script_getstr(st, 2);
  11481. format = script_getstr(st, 3);
  11482. argc = script_lastdata(st)-3;
  11483. len = strlen(format);
  11484. CREATE(buf, char, len*2+1);
  11485. // Issue sscanf for each parameter
  11486. *buf = 0;
  11487. q = format;
  11488. while((p = strchr(q, '%'))){
  11489. if(p!=q){
  11490. strncat(buf, q, (size_t)(p-q));
  11491. q = p;
  11492. }
  11493. p = q+1;
  11494. if(*p=='*' || *p=='%'){ // Skip
  11495. strncat(buf, q, 2);
  11496. q+=2;
  11497. continue;
  11498. }
  11499. if(arg>=argc){
  11500. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  11501. script_pushint(st, -1);
  11502. if(buf) aFree(buf);
  11503. if(ref_str) aFree(ref_str);
  11504. return 1;
  11505. }
  11506. if((p = strchr(q+1, '%'))==NULL){
  11507. p = strchr(q, 0); // EOS
  11508. }
  11509. len = p-q;
  11510. strncat(buf, q, len);
  11511. q = p;
  11512. // Validate output
  11513. data = script_getdata(st, arg+4);
  11514. if(!data_isreference(data) || !reference_tovariable(data)){
  11515. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  11516. script_pushint(st, -1);
  11517. if(buf) aFree(buf);
  11518. if(ref_str) aFree(ref_str);
  11519. return 1;
  11520. }
  11521. buf_p = reference_getname(data);
  11522. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  11523. script_pushint(st, -1);
  11524. if(buf) aFree(buf);
  11525. if(ref_str) aFree(ref_str);
  11526. return 0;
  11527. }
  11528. // Save value if any
  11529. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  11530. if(ref_str==NULL){
  11531. CREATE(ref_str, char, strlen(str)+1);
  11532. }
  11533. if(sscanf(str, buf, ref_str)==0){
  11534. break;
  11535. }
  11536. set_reg(st, sd, add_str(buf_p), buf_p, (void *)(ref_str), reference_getref(data));
  11537. }else{ // Number
  11538. if(sscanf(str, buf, &ref_int)==0){
  11539. break;
  11540. }
  11541. set_reg(st, sd, add_str(buf_p), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  11542. }
  11543. arg++;
  11544. // Disable used format (%... -> %*...)
  11545. buf_p = strchr(buf, 0);
  11546. memmove(buf_p-len+2, buf_p-len+1, len);
  11547. *(buf_p-len+1) = '*';
  11548. }
  11549. // Passed more, than needed
  11550. if(arg<argc){
  11551. ShowWarning("buildin_sscanf: Unused arguments passed.\n");
  11552. script_reportsrc(st);
  11553. }
  11554. script_pushint(st, arg);
  11555. if(buf) aFree(buf);
  11556. if(ref_str) aFree(ref_str);
  11557. return 0;
  11558. }
  11559. //=======================================================
  11560. // strpos(<haystack>, <needle>)
  11561. // strpos(<haystack>, <needle>, <offset>)
  11562. //
  11563. // Implements PHP style strpos. Adapted from code from
  11564. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  11565. //-------------------------------------------------------
  11566. BUILDIN_FUNC(strpos) {
  11567. const char *haystack = script_getstr(st,2);
  11568. const char *needle = script_getstr(st,3);
  11569. int i;
  11570. size_t len;
  11571. if( script_hasdata(st,4) )
  11572. i = script_getnum(st,4);
  11573. else
  11574. i = 0;
  11575. if ( strlen(needle) == 0 ) {
  11576. script_pushint(st, -1);
  11577. return 0;
  11578. }
  11579. len = strlen(haystack);
  11580. for ( ; i < len; ++i ) {
  11581. if ( haystack[i] == *needle ) {
  11582. // matched starting char -- loop through remaining chars
  11583. const char *h, *n;
  11584. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  11585. if ( *h != *n ) {
  11586. break;
  11587. }
  11588. }
  11589. if ( !*n ) { // matched all of 'needle' to null termination
  11590. script_pushint(st, i);
  11591. return 0;
  11592. }
  11593. }
  11594. }
  11595. script_pushint(st, -1);
  11596. return 0;
  11597. }
  11598. //===============================================================
  11599. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  11600. //
  11601. // Note: Finds all instances of <search> in <input> and replaces
  11602. // with <replace>. If specified will only replace as many
  11603. // instances as specified in <count>. By default will be case
  11604. // sensitive.
  11605. //---------------------------------------------------------------
  11606. BUILDIN_FUNC(replacestr)
  11607. {
  11608. const char *input = script_getstr(st, 2);
  11609. const char *find = script_getstr(st, 3);
  11610. const char *replace = script_getstr(st, 4);
  11611. size_t inputlen = strlen(input);
  11612. size_t findlen = strlen(find);
  11613. struct StringBuf output;
  11614. bool usecase = true;
  11615. int count = 0;
  11616. int numFinds = 0;
  11617. int i = 0, f = 0;
  11618. if(findlen == 0) {
  11619. ShowError("script:replacestr: Invalid search length.\n");
  11620. st->state = END;
  11621. return 1;
  11622. }
  11623. if(script_hasdata(st, 5)) {
  11624. if(script_isint(st,5))
  11625. usecase = script_getnum(st, 5) != 0;
  11626. else {
  11627. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  11628. st->state = END;
  11629. return 1;
  11630. }
  11631. }
  11632. if(script_hasdata(st, 6)) {
  11633. if(script_isint(st,6))
  11634. count = script_getnum(st, 6);
  11635. else {
  11636. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  11637. st->state = END;
  11638. return 1;
  11639. }
  11640. }
  11641. StringBuf_Init(&output);
  11642. for(; i < inputlen; i++) {
  11643. if(count && count == numFinds) { //found enough, stop looking
  11644. break;
  11645. }
  11646. for(f = 0; f <= findlen; f++) {
  11647. if(f == findlen) { //complete match
  11648. numFinds++;
  11649. StringBuf_AppendStr(&output, replace);
  11650. i += findlen - 1;
  11651. break;
  11652. } else {
  11653. if(usecase) {
  11654. if((i + f) > inputlen || input[i + f] != find[f]) {
  11655. StringBuf_Printf(&output, "%c", input[i]);
  11656. break;
  11657. }
  11658. } else {
  11659. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11660. StringBuf_Printf(&output, "%c", input[i]);
  11661. break;
  11662. }
  11663. }
  11664. }
  11665. }
  11666. }
  11667. //append excess after enough found
  11668. if(i < inputlen)
  11669. StringBuf_AppendStr(&output, &(input[i]));
  11670. script_pushstrcopy(st, StringBuf_Value(&output));
  11671. StringBuf_Destroy(&output);
  11672. return 0;
  11673. }
  11674. //========================================================
  11675. // countstr <input>, <search>{, <usecase>}
  11676. //
  11677. // Note: Counts the number of times <search> occurs in
  11678. // <input>. By default will be case sensitive.
  11679. //--------------------------------------------------------
  11680. BUILDIN_FUNC(countstr)
  11681. {
  11682. const char *input = script_getstr(st, 2);
  11683. const char *find = script_getstr(st, 3);
  11684. size_t inputlen = strlen(input);
  11685. size_t findlen = strlen(find);
  11686. bool usecase = true;
  11687. int numFinds = 0;
  11688. int i = 0, f = 0;
  11689. if(findlen == 0) {
  11690. ShowError("script:countstr: Invalid search length.\n");
  11691. st->state = END;
  11692. return 1;
  11693. }
  11694. if(script_hasdata(st, 4)) {
  11695. if(script_isint(st,4))
  11696. usecase = script_getnum(st, 4) != 0;
  11697. else {
  11698. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  11699. st->state = END;
  11700. return 1;
  11701. }
  11702. }
  11703. for(; i < inputlen; i++) {
  11704. for(f = 0; f <= findlen; f++) {
  11705. if(f == findlen) { //complete match
  11706. numFinds++;
  11707. i += findlen - 1;
  11708. break;
  11709. } else {
  11710. if(usecase) {
  11711. if((i + f) > inputlen || input[i + f] != find[f]) {
  11712. break;
  11713. }
  11714. } else {
  11715. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11716. break;
  11717. }
  11718. }
  11719. }
  11720. }
  11721. }
  11722. script_pushint(st, numFinds);
  11723. return 0;
  11724. }
  11725. /// Changes the display name and/or display class of the npc.
  11726. /// Returns 0 is successful, 1 if the npc does not exist.
  11727. ///
  11728. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  11729. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  11730. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  11731. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  11732. BUILDIN_FUNC(setnpcdisplay)
  11733. {
  11734. const char* name;
  11735. const char* newname = NULL;
  11736. int class_ = -1, size = -1;
  11737. struct script_data* data;
  11738. struct npc_data* nd;
  11739. name = script_getstr(st,2);
  11740. data = script_getdata(st,3);
  11741. if( script_hasdata(st,4) )
  11742. class_ = script_getnum(st,4);
  11743. if( script_hasdata(st,5) )
  11744. size = script_getnum(st,5);
  11745. get_val(st, data);
  11746. if( data_isstring(data) )
  11747. newname = conv_str(st,data);
  11748. else if( data_isint(data) )
  11749. class_ = conv_num(st,data);
  11750. else
  11751. {
  11752. ShowError("script:setnpcdisplay: expected a string or number\n");
  11753. script_reportdata(data);
  11754. return 1;
  11755. }
  11756. nd = npc_name2id(name);
  11757. if( nd == NULL )
  11758. {// not found
  11759. script_pushint(st,1);
  11760. return 0;
  11761. }
  11762. // update npc
  11763. if( newname )
  11764. npc_setdisplayname(nd, newname);
  11765. if( size != -1 && size != (int)nd->size )
  11766. nd->size = size;
  11767. else
  11768. size = -1;
  11769. if( class_ != -1 && nd->class_ != class_ )
  11770. npc_setclass(nd, class_);
  11771. else if( size != -1 )
  11772. { // Required to update the visual size
  11773. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  11774. clif_spawn(&nd->bl);
  11775. }
  11776. script_pushint(st,0);
  11777. return 0;
  11778. }
  11779. BUILDIN_FUNC(atoi)
  11780. {
  11781. const char *value;
  11782. value = script_getstr(st,2);
  11783. script_pushint(st,atoi(value));
  11784. return 0;
  11785. }
  11786. // case-insensitive substring search [lordalfa]
  11787. BUILDIN_FUNC(compare)
  11788. {
  11789. const char *message;
  11790. const char *cmpstring;
  11791. message = script_getstr(st,2);
  11792. cmpstring = script_getstr(st,3);
  11793. script_pushint(st,(stristr(message,cmpstring) != NULL));
  11794. return 0;
  11795. }
  11796. // [zBuffer] List of mathematics commands --->
  11797. BUILDIN_FUNC(sqrt)
  11798. {
  11799. double i, a;
  11800. i = script_getnum(st,2);
  11801. a = sqrt(i);
  11802. script_pushint(st,(int)a);
  11803. return 0;
  11804. }
  11805. BUILDIN_FUNC(pow)
  11806. {
  11807. double i, a, b;
  11808. a = script_getnum(st,2);
  11809. b = script_getnum(st,3);
  11810. i = pow(a,b);
  11811. script_pushint(st,(int)i);
  11812. return 0;
  11813. }
  11814. BUILDIN_FUNC(distance)
  11815. {
  11816. int x0, y0, x1, y1;
  11817. x0 = script_getnum(st,2);
  11818. y0 = script_getnum(st,3);
  11819. x1 = script_getnum(st,4);
  11820. y1 = script_getnum(st,5);
  11821. script_pushint(st,distance_xy(x0,y0,x1,y1));
  11822. return 0;
  11823. }
  11824. // <--- [zBuffer] List of mathematics commands
  11825. BUILDIN_FUNC(md5)
  11826. {
  11827. const char *tmpstr;
  11828. char *md5str;
  11829. tmpstr = script_getstr(st,2);
  11830. md5str = (char *)aMalloc((32+1)*sizeof(char));
  11831. MD5_String(tmpstr, md5str);
  11832. script_pushstr(st, md5str);
  11833. return 0;
  11834. }
  11835. // [zBuffer] List of dynamic var commands --->
  11836. BUILDIN_FUNC(setd)
  11837. {
  11838. TBL_PC *sd=NULL;
  11839. char varname[100];
  11840. const char *buffer;
  11841. int elem;
  11842. buffer = script_getstr(st, 2);
  11843. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  11844. elem = 0;
  11845. if( not_server_variable(*varname) )
  11846. {
  11847. sd = script_rid2sd(st);
  11848. if( sd == NULL )
  11849. {
  11850. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  11851. return 0;
  11852. }
  11853. }
  11854. if( is_string_variable(varname) ) {
  11855. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  11856. } else {
  11857. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  11858. }
  11859. return 0;
  11860. }
  11861. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  11862. {
  11863. int i, j;
  11864. TBL_PC* sd = NULL;
  11865. const char* query;
  11866. struct script_data* data;
  11867. const char* name;
  11868. int max_rows = SCRIPT_MAX_ARRAYSIZE;// maximum number of rows
  11869. int num_vars;
  11870. int num_cols;
  11871. // check target variables
  11872. for( i = 3; script_hasdata(st,i); ++i )
  11873. {
  11874. data = script_getdata(st, i);
  11875. if( data_isreference(data) && reference_tovariable(data) )
  11876. {// it's a variable
  11877. name = reference_getname(data);
  11878. if( not_server_variable(*name) && sd == NULL )
  11879. {// requires a player
  11880. sd = script_rid2sd(st);
  11881. if( sd == NULL )
  11882. {// no player attached
  11883. script_reportdata(data);
  11884. st->state = END;
  11885. return 1;
  11886. }
  11887. }
  11888. if( not_array_variable(*name) )
  11889. max_rows = 1;// not an array, limit to one row
  11890. }
  11891. else
  11892. {
  11893. ShowError("script:query_sql: not a variable\n");
  11894. script_reportdata(data);
  11895. st->state = END;
  11896. return 1;
  11897. }
  11898. }
  11899. num_vars = i - 3;
  11900. // Execute the query
  11901. query = script_getstr(st,2);
  11902. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  11903. {
  11904. Sql_ShowDebug(handle);
  11905. script_pushint(st, 0);
  11906. return 1;
  11907. }
  11908. if( Sql_NumRows(handle) == 0 )
  11909. {// No data received
  11910. Sql_FreeResult(handle);
  11911. script_pushint(st, 0);
  11912. return 0;
  11913. }
  11914. // Count the number of columns to store
  11915. num_cols = Sql_NumColumns(handle);
  11916. if( num_vars < num_cols )
  11917. {
  11918. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  11919. script_reportsrc(st);
  11920. }
  11921. else if( num_vars > num_cols )
  11922. {
  11923. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  11924. script_reportsrc(st);
  11925. }
  11926. // Store data
  11927. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  11928. {
  11929. for( j = 0; j < num_vars; ++j )
  11930. {
  11931. char* str = NULL;
  11932. if( j < num_cols )
  11933. Sql_GetData(handle, j, &str, NULL);
  11934. data = script_getdata(st, j+3);
  11935. name = reference_getname(data);
  11936. if( is_string_variable(name) )
  11937. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  11938. else
  11939. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  11940. }
  11941. }
  11942. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  11943. {
  11944. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  11945. script_reportsrc(st);
  11946. }
  11947. // Free data
  11948. Sql_FreeResult(handle);
  11949. script_pushint(st, i);
  11950. return 0;
  11951. }
  11952. BUILDIN_FUNC(query_sql)
  11953. {
  11954. return buildin_query_sql_sub(st, mmysql_handle);
  11955. }
  11956. BUILDIN_FUNC(query_logsql)
  11957. {
  11958. if( !log_config.sql_logs )
  11959. {// logmysql_handle == NULL
  11960. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  11961. script_pushint(st,-1);
  11962. return 1;
  11963. }
  11964. return buildin_query_sql_sub(st, logmysql_handle);
  11965. }
  11966. //Allows escaping of a given string.
  11967. BUILDIN_FUNC(escape_sql)
  11968. {
  11969. const char *str;
  11970. char *esc_str;
  11971. size_t len;
  11972. str = script_getstr(st,2);
  11973. len = strlen(str);
  11974. esc_str = (char*)aMalloc(len*2+1);
  11975. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  11976. script_pushstr(st, esc_str);
  11977. return 0;
  11978. }
  11979. BUILDIN_FUNC(getd)
  11980. {
  11981. char varname[100];
  11982. const char *buffer;
  11983. int elem;
  11984. buffer = script_getstr(st, 2);
  11985. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  11986. elem = 0;
  11987. // Push the 'pointer' so it's more flexible [Lance]
  11988. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  11989. return 0;
  11990. }
  11991. // <--- [zBuffer] List of dynamic var commands
  11992. // Pet stat [Lance]
  11993. BUILDIN_FUNC(petstat)
  11994. {
  11995. TBL_PC *sd = NULL;
  11996. struct pet_data *pd;
  11997. int flag = script_getnum(st,2);
  11998. sd = script_rid2sd(st);
  11999. if(!sd || !sd->status.pet_id || !sd->pd){
  12000. if(flag == 2)
  12001. script_pushconststr(st, "");
  12002. else
  12003. script_pushint(st,0);
  12004. return 0;
  12005. }
  12006. pd = sd->pd;
  12007. switch(flag){
  12008. case 1: script_pushint(st,(int)pd->pet.class_); break;
  12009. case 2: script_pushstrcopy(st, pd->pet.name); break;
  12010. case 3: script_pushint(st,(int)pd->pet.level); break;
  12011. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  12012. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  12013. default:
  12014. script_pushint(st,0);
  12015. break;
  12016. }
  12017. return 0;
  12018. }
  12019. BUILDIN_FUNC(callshop)
  12020. {
  12021. TBL_PC *sd = NULL;
  12022. struct npc_data *nd;
  12023. const char *shopname;
  12024. int flag = 0;
  12025. sd = script_rid2sd(st);
  12026. if (!sd) {
  12027. script_pushint(st,0);
  12028. return 0;
  12029. }
  12030. shopname = script_getstr(st, 2);
  12031. if( script_hasdata(st,3) )
  12032. flag = script_getnum(st,3);
  12033. nd = npc_name2id(shopname);
  12034. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  12035. {
  12036. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  12037. script_pushint(st,0);
  12038. return 1;
  12039. }
  12040. if( nd->subtype == SHOP )
  12041. {
  12042. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  12043. sd->state.callshop = 1;
  12044. switch( flag )
  12045. {
  12046. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  12047. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  12048. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  12049. }
  12050. }
  12051. else
  12052. clif_cashshop_show(sd, nd);
  12053. sd->npc_shopid = nd->bl.id;
  12054. script_pushint(st,1);
  12055. return 0;
  12056. }
  12057. BUILDIN_FUNC(npcshopitem)
  12058. {
  12059. const char* npcname = script_getstr(st, 2);
  12060. struct npc_data* nd = npc_name2id(npcname);
  12061. int n, i;
  12062. int amount;
  12063. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12064. { //Not found.
  12065. script_pushint(st,0);
  12066. return 0;
  12067. }
  12068. // get the count of new entries
  12069. amount = (script_lastdata(st)-2)/2;
  12070. // generate new shop item list
  12071. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  12072. for( n = 0, i = 3; n < amount; n++, i+=2 )
  12073. {
  12074. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12075. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12076. }
  12077. nd->u.shop.count = n;
  12078. script_pushint(st,1);
  12079. return 0;
  12080. }
  12081. BUILDIN_FUNC(npcshopadditem)
  12082. {
  12083. const char* npcname = script_getstr(st,2);
  12084. struct npc_data* nd = npc_name2id(npcname);
  12085. int n, i;
  12086. int amount;
  12087. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12088. { //Not found.
  12089. script_pushint(st,0);
  12090. return 0;
  12091. }
  12092. // get the count of new entries
  12093. amount = (script_lastdata(st)-2)/2;
  12094. // append new items to existing shop item list
  12095. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  12096. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  12097. {
  12098. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12099. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12100. }
  12101. nd->u.shop.count = n;
  12102. script_pushint(st,1);
  12103. return 0;
  12104. }
  12105. BUILDIN_FUNC(npcshopdelitem)
  12106. {
  12107. const char* npcname = script_getstr(st,2);
  12108. struct npc_data* nd = npc_name2id(npcname);
  12109. unsigned int nameid;
  12110. int n, i;
  12111. int amount;
  12112. int size;
  12113. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12114. { //Not found.
  12115. script_pushint(st,0);
  12116. return 0;
  12117. }
  12118. amount = script_lastdata(st)-2;
  12119. size = nd->u.shop.count;
  12120. // remove specified items from the shop item list
  12121. for( i = 3; i < 3 + amount; i++ )
  12122. {
  12123. nameid = script_getnum(st,i);
  12124. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  12125. if( n < size )
  12126. {
  12127. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  12128. size--;
  12129. }
  12130. }
  12131. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  12132. nd->u.shop.count = size;
  12133. script_pushint(st,1);
  12134. return 0;
  12135. }
  12136. //Sets a script to attach to a shop npc.
  12137. BUILDIN_FUNC(npcshopattach)
  12138. {
  12139. const char* npcname = script_getstr(st,2);
  12140. struct npc_data* nd = npc_name2id(npcname);
  12141. int flag = 1;
  12142. if( script_hasdata(st,3) )
  12143. flag = script_getnum(st,3);
  12144. if( !nd || nd->subtype != SHOP )
  12145. { //Not found.
  12146. script_pushint(st,0);
  12147. return 0;
  12148. }
  12149. if (flag)
  12150. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  12151. else
  12152. nd->master_nd = NULL;
  12153. script_pushint(st,1);
  12154. return 0;
  12155. }
  12156. /*==========================================
  12157. * Returns some values of an item [Lupus]
  12158. * Price, Weight, etc...
  12159. setitemscript(itemID,"{new item bonus script}",[n]);
  12160. Where n:
  12161. 0 - script
  12162. 1 - Equip script
  12163. 2 - Unequip script
  12164. *------------------------------------------*/
  12165. BUILDIN_FUNC(setitemscript)
  12166. {
  12167. int item_id,n=0;
  12168. const char *script;
  12169. struct item_data *i_data;
  12170. struct script_code **dstscript;
  12171. item_id = script_getnum(st,2);
  12172. script = script_getstr(st,3);
  12173. if( script_hasdata(st,4) )
  12174. n=script_getnum(st,4);
  12175. i_data = itemdb_exists(item_id);
  12176. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  12177. script_pushint(st,0);
  12178. return 0;
  12179. }
  12180. switch (n) {
  12181. case 2:
  12182. dstscript = &i_data->unequip_script;
  12183. break;
  12184. case 1:
  12185. dstscript = &i_data->equip_script;
  12186. break;
  12187. default:
  12188. dstscript = &i_data->script;
  12189. break;
  12190. }
  12191. if(*dstscript)
  12192. script_free_code(*dstscript);
  12193. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  12194. script_pushint(st,1);
  12195. return 0;
  12196. }
  12197. /* Work In Progress [Lupus]
  12198. BUILDIN_FUNC(addmonsterdrop)
  12199. {
  12200. int class_,item_id,chance;
  12201. class_=script_getnum(st,2);
  12202. item_id=script_getnum(st,3);
  12203. chance=script_getnum(st,4);
  12204. if(class_>1000 && item_id>500 && chance>0) {
  12205. script_pushint(st,1);
  12206. } else {
  12207. script_pushint(st,0);
  12208. }
  12209. }
  12210. BUILDIN_FUNC(delmonsterdrop)
  12211. {
  12212. int class_,item_id;
  12213. class_=script_getnum(st,2);
  12214. item_id=script_getnum(st,3);
  12215. if(class_>1000 && item_id>500) {
  12216. script_pushint(st,1);
  12217. } else {
  12218. script_pushint(st,0);
  12219. }
  12220. }
  12221. */
  12222. /*==========================================
  12223. * Returns some values of a monster [Lupus]
  12224. * Name, Level, race, size, etc...
  12225. getmonsterinfo(monsterID,queryIndex);
  12226. *------------------------------------------*/
  12227. BUILDIN_FUNC(getmonsterinfo)
  12228. {
  12229. struct mob_db *mob;
  12230. int mob_id;
  12231. mob_id = script_getnum(st,2);
  12232. if (!mobdb_checkid(mob_id)) {
  12233. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  12234. if ( !script_getnum(st,3) ) //requested a string
  12235. script_pushconststr(st,"null");
  12236. else
  12237. script_pushint(st,-1);
  12238. return -1;
  12239. }
  12240. mob = mob_db(mob_id);
  12241. switch ( script_getnum(st,3) ) {
  12242. case 0: script_pushstrcopy(st,mob->jname); break;
  12243. case 1: script_pushint(st,mob->lv); break;
  12244. case 2: script_pushint(st,mob->status.max_hp); break;
  12245. case 3: script_pushint(st,mob->base_exp); break;
  12246. case 4: script_pushint(st,mob->job_exp); break;
  12247. case 5: script_pushint(st,mob->status.rhw.atk); break;
  12248. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  12249. case 7: script_pushint(st,mob->status.def); break;
  12250. case 8: script_pushint(st,mob->status.mdef); break;
  12251. case 9: script_pushint(st,mob->status.str); break;
  12252. case 10: script_pushint(st,mob->status.agi); break;
  12253. case 11: script_pushint(st,mob->status.vit); break;
  12254. case 12: script_pushint(st,mob->status.int_); break;
  12255. case 13: script_pushint(st,mob->status.dex); break;
  12256. case 14: script_pushint(st,mob->status.luk); break;
  12257. case 15: script_pushint(st,mob->status.rhw.range); break;
  12258. case 16: script_pushint(st,mob->range2); break;
  12259. case 17: script_pushint(st,mob->range3); break;
  12260. case 18: script_pushint(st,mob->status.size); break;
  12261. case 19: script_pushint(st,mob->status.race); break;
  12262. case 20: script_pushint(st,mob->status.def_ele); break;
  12263. case 21: script_pushint(st,mob->status.mode); break;
  12264. case 22: script_pushint(st,mob->mexp); break;
  12265. default: script_pushint(st,-1); //wrong Index
  12266. }
  12267. return 0;
  12268. }
  12269. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  12270. {
  12271. TBL_PC *sd = NULL;
  12272. if(script_hasdata(st,2))
  12273. sd = map_nick2sd(script_getstr(st,2));
  12274. else
  12275. sd = script_rid2sd(st);
  12276. if(sd)
  12277. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  12278. else
  12279. script_pushint(st,0);
  12280. return 0;
  12281. }
  12282. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  12283. {
  12284. TBL_PC *sd = NULL;
  12285. if(script_hasdata(st,2))
  12286. sd = map_nick2sd(script_getstr(st,2));
  12287. else
  12288. sd = script_rid2sd(st);
  12289. if(sd)
  12290. script_pushint(st,(sd->chatID != 0));
  12291. else
  12292. script_pushint(st,0);
  12293. return 0;
  12294. }
  12295. BUILDIN_FUNC(searchitem)
  12296. {
  12297. struct script_data* data = script_getdata(st, 2);
  12298. const char *itemname = script_getstr(st,3);
  12299. struct item_data *items[MAX_SEARCH];
  12300. int count;
  12301. char* name;
  12302. int32 start;
  12303. int32 id;
  12304. int32 i;
  12305. TBL_PC* sd = NULL;
  12306. if ((items[0] = itemdb_exists(atoi(itemname))))
  12307. count = 1;
  12308. else {
  12309. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  12310. if (count > MAX_SEARCH) count = MAX_SEARCH;
  12311. }
  12312. if (!count) {
  12313. script_pushint(st, 0);
  12314. return 0;
  12315. }
  12316. if( !data_isreference(data) )
  12317. {
  12318. ShowError("script:searchitem: not a variable\n");
  12319. script_reportdata(data);
  12320. st->state = END;
  12321. return 1;// not a variable
  12322. }
  12323. id = reference_getid(data);
  12324. start = reference_getindex(data);
  12325. name = reference_getname(data);
  12326. if( not_array_variable(*name) )
  12327. {
  12328. ShowError("script:searchitem: illegal scope\n");
  12329. script_reportdata(data);
  12330. st->state = END;
  12331. return 1;// not supported
  12332. }
  12333. if( not_server_variable(*name) )
  12334. {
  12335. sd = script_rid2sd(st);
  12336. if( sd == NULL )
  12337. return 0;// no player attached
  12338. }
  12339. if( is_string_variable(name) )
  12340. {// string array
  12341. ShowError("script:searchitem: not an integer array reference\n");
  12342. script_reportdata(data);
  12343. st->state = END;
  12344. return 1;// not supported
  12345. }
  12346. for( i = 0; i < count; ++start, ++i )
  12347. {// Set array
  12348. void* v = (void*)__64BPRTSIZE(items[i]->nameid);
  12349. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  12350. }
  12351. script_pushint(st, count);
  12352. return 0;
  12353. }
  12354. int axtoi(const char *hexStg)
  12355. {
  12356. int n = 0; // position in string
  12357. int m = 0; // position in digit[] to shift
  12358. int count; // loop index
  12359. int intValue = 0; // integer value of hex string
  12360. int digit[11]; // hold values to convert
  12361. while (n < 10) {
  12362. if (hexStg[n]=='\0')
  12363. break;
  12364. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  12365. digit[n] = hexStg[n] & 0x0f; //convert to int
  12366. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  12367. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12368. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  12369. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12370. else break;
  12371. n++;
  12372. }
  12373. count = n;
  12374. m = n - 1;
  12375. n = 0;
  12376. while(n < count) {
  12377. // digit[n] is value of hex digit at position n
  12378. // (m << 2) is the number of positions to shift
  12379. // OR the bits into return value
  12380. intValue = intValue | (digit[n] << (m << 2));
  12381. m--; // adjust the position to set
  12382. n++; // next digit to process
  12383. }
  12384. return (intValue);
  12385. }
  12386. // [Lance] Hex string to integer converter
  12387. BUILDIN_FUNC(axtoi)
  12388. {
  12389. const char *hex = script_getstr(st,2);
  12390. script_pushint(st,axtoi(hex));
  12391. return 0;
  12392. }
  12393. // [zBuffer] List of player cont commands --->
  12394. BUILDIN_FUNC(rid2name)
  12395. {
  12396. struct block_list *bl = NULL;
  12397. int rid = script_getnum(st,2);
  12398. if((bl = map_id2bl(rid)))
  12399. {
  12400. switch(bl->type) {
  12401. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  12402. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  12403. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  12404. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  12405. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  12406. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  12407. default:
  12408. ShowError("buildin_rid2name: BL type unknown.\n");
  12409. script_pushconststr(st,"");
  12410. break;
  12411. }
  12412. } else {
  12413. ShowError("buildin_rid2name: invalid RID\n");
  12414. script_pushconststr(st,"(null)");
  12415. }
  12416. return 0;
  12417. }
  12418. BUILDIN_FUNC(pcblockmove)
  12419. {
  12420. int id, flag;
  12421. TBL_PC *sd = NULL;
  12422. id = script_getnum(st,2);
  12423. flag = script_getnum(st,3);
  12424. if(id)
  12425. sd = map_id2sd(id);
  12426. else
  12427. sd = script_rid2sd(st);
  12428. if(sd)
  12429. sd->state.blockedmove = flag > 0;
  12430. return 0;
  12431. }
  12432. BUILDIN_FUNC(pcfollow)
  12433. {
  12434. int id, targetid;
  12435. TBL_PC *sd = NULL;
  12436. id = script_getnum(st,2);
  12437. targetid = script_getnum(st,3);
  12438. if(id)
  12439. sd = map_id2sd(id);
  12440. else
  12441. sd = script_rid2sd(st);
  12442. if(sd)
  12443. pc_follow(sd, targetid);
  12444. return 0;
  12445. }
  12446. BUILDIN_FUNC(pcstopfollow)
  12447. {
  12448. int id;
  12449. TBL_PC *sd = NULL;
  12450. id = script_getnum(st,2);
  12451. if(id)
  12452. sd = map_id2sd(id);
  12453. else
  12454. sd = script_rid2sd(st);
  12455. if(sd)
  12456. pc_stop_following(sd);
  12457. return 0;
  12458. }
  12459. // <--- [zBuffer] List of player cont commands
  12460. // [zBuffer] List of mob control commands --->
  12461. //## TODO always return if the request/whatever was successfull [FlavioJS]
  12462. /// Makes the unit walk to target position or map
  12463. /// Returns if it was successfull
  12464. ///
  12465. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  12466. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  12467. BUILDIN_FUNC(unitwalk)
  12468. {
  12469. struct block_list* bl;
  12470. bl = map_id2bl(script_getnum(st,2));
  12471. if( bl == NULL )
  12472. {
  12473. script_pushint(st, 0);
  12474. }
  12475. else if( script_hasdata(st,4) )
  12476. {
  12477. int x = script_getnum(st,3);
  12478. int y = script_getnum(st,4);
  12479. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  12480. }
  12481. else
  12482. {
  12483. int map_id = script_getnum(st,3);
  12484. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  12485. }
  12486. return 0;
  12487. }
  12488. /// Kills the unit
  12489. ///
  12490. /// unitkill <unit_id>;
  12491. BUILDIN_FUNC(unitkill)
  12492. {
  12493. struct block_list* bl = map_id2bl(script_getnum(st,2));
  12494. if( bl != NULL )
  12495. status_kill(bl);
  12496. return 0;
  12497. }
  12498. /// Warps the unit to the target position in the target map
  12499. /// Returns if it was successfull
  12500. ///
  12501. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  12502. BUILDIN_FUNC(unitwarp)
  12503. {
  12504. int unit_id;
  12505. int map;
  12506. short x;
  12507. short y;
  12508. struct block_list* bl;
  12509. const char *mapname;
  12510. unit_id = script_getnum(st,2);
  12511. mapname = script_getstr(st, 3);
  12512. x = (short)script_getnum(st,4);
  12513. y = (short)script_getnum(st,5);
  12514. if (!unit_id) //Warp the script's runner
  12515. bl = map_id2bl(st->rid);
  12516. else
  12517. bl = map_id2bl(unit_id);
  12518. if( strcmp(mapname,"this") == 0 )
  12519. map = bl?bl->m:-1;
  12520. else
  12521. map = map_mapname2mapid(mapname);
  12522. if( map >= 0 && bl != NULL )
  12523. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  12524. else
  12525. script_pushint(st, 0);
  12526. return 0;
  12527. }
  12528. /// Makes the unit attack the target.
  12529. /// If the unit is a player and <action type> is not 0, it does a continuous
  12530. /// attack instead of a single attack.
  12531. /// Returns if the request was successfull.
  12532. ///
  12533. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  12534. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  12535. BUILDIN_FUNC(unitattack)
  12536. {
  12537. struct block_list* unit_bl;
  12538. struct block_list* target_bl = NULL;
  12539. struct script_data* data;
  12540. int actiontype = 0;
  12541. // get unit
  12542. unit_bl = map_id2bl(script_getnum(st,2));
  12543. if( unit_bl == NULL ) {
  12544. script_pushint(st, 0);
  12545. return 0;
  12546. }
  12547. data = script_getdata(st, 3);
  12548. get_val(st, data);
  12549. if( data_isstring(data) )
  12550. {
  12551. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  12552. if( sd != NULL )
  12553. target_bl = &sd->bl;
  12554. } else
  12555. target_bl = map_id2bl(conv_num(st, data));
  12556. // request the attack
  12557. if( target_bl == NULL )
  12558. {
  12559. script_pushint(st, 0);
  12560. return 0;
  12561. }
  12562. // get actiontype
  12563. if( script_hasdata(st,4) )
  12564. actiontype = script_getnum(st,4);
  12565. switch( unit_bl->type )
  12566. {
  12567. case BL_PC:
  12568. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  12569. script_pushint(st, 1);
  12570. return 0;
  12571. case BL_MOB:
  12572. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  12573. break;
  12574. case BL_PET:
  12575. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  12576. break;
  12577. default:
  12578. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  12579. script_pushint(st, 0);
  12580. return 1;
  12581. }
  12582. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  12583. return 0;
  12584. }
  12585. /// Makes the unit stop attacking and moving
  12586. ///
  12587. /// unitstop <unit_id>;
  12588. BUILDIN_FUNC(unitstop)
  12589. {
  12590. int unit_id;
  12591. struct block_list* bl;
  12592. unit_id = script_getnum(st,2);
  12593. bl = map_id2bl(unit_id);
  12594. if( bl != NULL )
  12595. {
  12596. unit_stop_attack(bl);
  12597. unit_stop_walking(bl,4);
  12598. if( bl->type == BL_MOB )
  12599. ((TBL_MOB*)bl)->target_id = 0;
  12600. }
  12601. return 0;
  12602. }
  12603. /// Makes the unit say the message
  12604. ///
  12605. /// unittalk <unit_id>,"<message>";
  12606. BUILDIN_FUNC(unittalk)
  12607. {
  12608. int unit_id;
  12609. const char* message;
  12610. struct block_list* bl;
  12611. unit_id = script_getnum(st,2);
  12612. message = script_getstr(st, 3);
  12613. bl = map_id2bl(unit_id);
  12614. if( bl != NULL )
  12615. {
  12616. struct StringBuf sbuf;
  12617. StringBuf_Init(&sbuf);
  12618. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  12619. clif_message(bl, StringBuf_Value(&sbuf));
  12620. if( bl->type == BL_PC )
  12621. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  12622. StringBuf_Destroy(&sbuf);
  12623. }
  12624. return 0;
  12625. }
  12626. /// Makes the unit do an emotion
  12627. ///
  12628. /// unitemote <unit_id>,<emotion>;
  12629. ///
  12630. /// @see e_* in const.txt
  12631. BUILDIN_FUNC(unitemote)
  12632. {
  12633. int unit_id;
  12634. int emotion;
  12635. struct block_list* bl;
  12636. unit_id = script_getnum(st,2);
  12637. emotion = script_getnum(st,3);
  12638. bl = map_id2bl(unit_id);
  12639. if( bl != NULL )
  12640. clif_emotion(bl, emotion);
  12641. return 0;
  12642. }
  12643. /// Makes the unit cast the skill on the target or self if no target is specified
  12644. ///
  12645. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  12646. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  12647. BUILDIN_FUNC(unitskilluseid)
  12648. {
  12649. int unit_id;
  12650. int skill_id;
  12651. int skill_lv;
  12652. int target_id;
  12653. struct block_list* bl;
  12654. unit_id = script_getnum(st,2);
  12655. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12656. skill_lv = script_getnum(st,4);
  12657. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  12658. bl = map_id2bl(unit_id);
  12659. if( bl != NULL )
  12660. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  12661. return 0;
  12662. }
  12663. /// Makes the unit cast the skill on the target position.
  12664. ///
  12665. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  12666. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  12667. BUILDIN_FUNC(unitskillusepos)
  12668. {
  12669. int unit_id;
  12670. int skill_id;
  12671. int skill_lv;
  12672. int skill_x;
  12673. int skill_y;
  12674. struct block_list* bl;
  12675. unit_id = script_getnum(st,2);
  12676. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12677. skill_lv = script_getnum(st,4);
  12678. skill_x = script_getnum(st,5);
  12679. skill_y = script_getnum(st,6);
  12680. bl = map_id2bl(unit_id);
  12681. if( bl != NULL )
  12682. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  12683. return 0;
  12684. }
  12685. // <--- [zBuffer] List of mob control commands
  12686. /// Pauses the execution of the script, detaching the player
  12687. ///
  12688. /// sleep <mili seconds>;
  12689. BUILDIN_FUNC(sleep)
  12690. {
  12691. int ticks;
  12692. ticks = script_getnum(st,2);
  12693. // detach the player
  12694. script_detach_rid(st);
  12695. if( ticks <= 0 )
  12696. {// do nothing
  12697. }
  12698. else if( st->sleep.tick == 0 )
  12699. {// sleep for the target amount of time
  12700. st->state = RERUNLINE;
  12701. st->sleep.tick = ticks;
  12702. }
  12703. else
  12704. {// sleep time is over
  12705. st->state = RUN;
  12706. st->sleep.tick = 0;
  12707. }
  12708. return 0;
  12709. }
  12710. /// Pauses the execution of the script, keeping the player attached
  12711. /// Returns if a player is still attached
  12712. ///
  12713. /// sleep2(<mili secconds>) -> <bool>
  12714. BUILDIN_FUNC(sleep2)
  12715. {
  12716. int ticks;
  12717. ticks = script_getnum(st,2);
  12718. if( ticks <= 0 )
  12719. {// do nothing
  12720. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12721. }
  12722. else if( !st->sleep.tick )
  12723. {// sleep for the target amount of time
  12724. st->state = RERUNLINE;
  12725. st->sleep.tick = ticks;
  12726. }
  12727. else
  12728. {// sleep time is over
  12729. st->state = RUN;
  12730. st->sleep.tick = 0;
  12731. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12732. }
  12733. return 0;
  12734. }
  12735. /// Awakes all the sleep timers of the target npc
  12736. ///
  12737. /// awake "<npc name>";
  12738. BUILDIN_FUNC(awake)
  12739. {
  12740. struct npc_data* nd;
  12741. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  12742. nd = npc_name2id(script_getstr(st, 2));
  12743. if( nd == NULL ) {
  12744. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  12745. return 1;
  12746. }
  12747. while( node )
  12748. {
  12749. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  12750. {// sleep timer for the npc
  12751. struct script_state* tst = (struct script_state*)node->data;
  12752. TBL_PC* sd = map_id2sd(tst->rid);
  12753. if( tst->sleep.timer == INVALID_TIMER )
  12754. {// already awake ???
  12755. node = node->next;
  12756. continue;
  12757. }
  12758. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  12759. {// char not online anymore / another char of the same account is online - Cancel execution
  12760. tst->state = END;
  12761. tst->rid = 0;
  12762. }
  12763. delete_timer(tst->sleep.timer, run_script_timer);
  12764. node = script_erase_sleepdb(node);
  12765. tst->sleep.timer = INVALID_TIMER;
  12766. if(tst->state != RERUNLINE)
  12767. tst->sleep.tick = 0;
  12768. run_script_main(tst);
  12769. }
  12770. else
  12771. {
  12772. node = node->next;
  12773. }
  12774. }
  12775. return 0;
  12776. }
  12777. /// Returns a reference to a variable of the target NPC.
  12778. /// Returns 0 if an error occurs.
  12779. ///
  12780. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  12781. BUILDIN_FUNC(getvariableofnpc)
  12782. {
  12783. struct script_data* data;
  12784. const char* name;
  12785. struct npc_data* nd;
  12786. data = script_getdata(st,2);
  12787. if( !data_isreference(data) )
  12788. {// Not a reference (aka varaible name)
  12789. ShowError("script:getvariableofnpc: not a variable\n");
  12790. script_reportdata(data);
  12791. script_pushnil(st);
  12792. st->state = END;
  12793. return 1;
  12794. }
  12795. name = reference_getname(data);
  12796. if( *name != '.' || name[1] == '@' )
  12797. {// not a npc variable
  12798. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  12799. script_reportdata(data);
  12800. script_pushnil(st);
  12801. st->state = END;
  12802. return 1;
  12803. }
  12804. nd = npc_name2id(script_getstr(st,3));
  12805. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  12806. {// NPC not found or has no script
  12807. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  12808. script_pushnil(st);
  12809. st->state = END;
  12810. return 1;
  12811. }
  12812. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  12813. return 0;
  12814. }
  12815. /// Opens a warp portal.
  12816. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  12817. ///
  12818. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  12819. ///
  12820. /// @author blackhole89
  12821. BUILDIN_FUNC(warpportal)
  12822. {
  12823. int spx;
  12824. int spy;
  12825. unsigned short mapindex;
  12826. int tpx;
  12827. int tpy;
  12828. struct skill_unit_group* group;
  12829. struct block_list* bl;
  12830. bl = map_id2bl(st->oid);
  12831. if( bl == NULL )
  12832. {
  12833. ShowError("script:warpportal: npc is needed\n");
  12834. return 1;
  12835. }
  12836. spx = script_getnum(st,2);
  12837. spy = script_getnum(st,3);
  12838. mapindex = mapindex_name2id(script_getstr(st, 4));
  12839. tpx = script_getnum(st,5);
  12840. tpy = script_getnum(st,6);
  12841. if( mapindex == 0 )
  12842. return 0;// map not found
  12843. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  12844. if( group == NULL )
  12845. return 0;// failed
  12846. group->val2 = (tpx<<16) | tpy;
  12847. group->val3 = mapindex;
  12848. return 0;
  12849. }
  12850. BUILDIN_FUNC(openmail)
  12851. {
  12852. TBL_PC* sd;
  12853. sd = script_rid2sd(st);
  12854. if( sd == NULL )
  12855. return 0;
  12856. mail_openmail(sd);
  12857. return 0;
  12858. }
  12859. BUILDIN_FUNC(openauction)
  12860. {
  12861. TBL_PC* sd;
  12862. sd = script_rid2sd(st);
  12863. if( sd == NULL )
  12864. return 0;
  12865. clif_Auction_openwindow(sd);
  12866. return 0;
  12867. }
  12868. /// Retrieves the value of the specified flag of the specified cell.
  12869. ///
  12870. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  12871. ///
  12872. /// @see cell_chk* constants in const.txt for the types
  12873. BUILDIN_FUNC(checkcell)
  12874. {
  12875. int m = map_mapname2mapid(script_getstr(st,2));
  12876. int x = script_getnum(st,3);
  12877. int y = script_getnum(st,4);
  12878. cell_chk type = (cell_chk)script_getnum(st,5);
  12879. script_pushint(st, map_getcell(m, x, y, type));
  12880. return 0;
  12881. }
  12882. /// Modifies flags of cells in the specified area.
  12883. ///
  12884. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  12885. ///
  12886. /// @see cell_* constants in const.txt for the types
  12887. BUILDIN_FUNC(setcell)
  12888. {
  12889. int m = map_mapname2mapid(script_getstr(st,2));
  12890. int x1 = script_getnum(st,3);
  12891. int y1 = script_getnum(st,4);
  12892. int x2 = script_getnum(st,5);
  12893. int y2 = script_getnum(st,6);
  12894. cell_t type = (cell_t)script_getnum(st,7);
  12895. bool flag = (bool)script_getnum(st,8);
  12896. int x,y;
  12897. if( x1 > x2 ) swap(x1,x2);
  12898. if( y1 > y2 ) swap(y1,y2);
  12899. for( y = y1; y <= y2; ++y )
  12900. for( x = x1; x <= x2; ++x )
  12901. map_setcell(m, x, y, type, flag);
  12902. return 0;
  12903. }
  12904. /*==========================================
  12905. * Mercenary Commands
  12906. *------------------------------------------*/
  12907. BUILDIN_FUNC(mercenary_create)
  12908. {
  12909. struct map_session_data *sd;
  12910. int class_, contract_time;
  12911. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  12912. return 0;
  12913. class_ = script_getnum(st,2);
  12914. if( !merc_class(class_) )
  12915. return 0;
  12916. contract_time = script_getnum(st,3);
  12917. merc_create(sd, class_, contract_time);
  12918. return 0;
  12919. }
  12920. BUILDIN_FUNC(mercenary_heal)
  12921. {
  12922. struct map_session_data *sd = script_rid2sd(st);
  12923. int hp, sp;
  12924. if( sd == NULL || sd->md == NULL )
  12925. return 0;
  12926. hp = script_getnum(st,2);
  12927. sp = script_getnum(st,3);
  12928. status_heal(&sd->md->bl, hp, sp, 0);
  12929. return 0;
  12930. }
  12931. BUILDIN_FUNC(mercenary_sc_start)
  12932. {
  12933. struct map_session_data *sd = script_rid2sd(st);
  12934. enum sc_type type;
  12935. int tick, val1;
  12936. if( sd == NULL || sd->md == NULL )
  12937. return 0;
  12938. type = (sc_type)script_getnum(st,2);
  12939. tick = script_getnum(st,3);
  12940. val1 = script_getnum(st,4);
  12941. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  12942. return 0;
  12943. }
  12944. BUILDIN_FUNC(mercenary_get_calls)
  12945. {
  12946. struct map_session_data *sd = script_rid2sd(st);
  12947. int guild;
  12948. if( sd == NULL )
  12949. return 0;
  12950. guild = script_getnum(st,2);
  12951. switch( guild )
  12952. {
  12953. case ARCH_MERC_GUILD:
  12954. script_pushint(st,sd->status.arch_calls);
  12955. break;
  12956. case SPEAR_MERC_GUILD:
  12957. script_pushint(st,sd->status.spear_calls);
  12958. break;
  12959. case SWORD_MERC_GUILD:
  12960. script_pushint(st,sd->status.sword_calls);
  12961. break;
  12962. default:
  12963. script_pushint(st,0);
  12964. break;
  12965. }
  12966. return 0;
  12967. }
  12968. BUILDIN_FUNC(mercenary_set_calls)
  12969. {
  12970. struct map_session_data *sd = script_rid2sd(st);
  12971. int guild, value, *calls;
  12972. if( sd == NULL )
  12973. return 0;
  12974. guild = script_getnum(st,2);
  12975. value = script_getnum(st,3);
  12976. switch( guild )
  12977. {
  12978. case ARCH_MERC_GUILD:
  12979. calls = &sd->status.arch_calls;
  12980. break;
  12981. case SPEAR_MERC_GUILD:
  12982. calls = &sd->status.spear_calls;
  12983. break;
  12984. case SWORD_MERC_GUILD:
  12985. calls = &sd->status.sword_calls;
  12986. break;
  12987. default:
  12988. return 0; // Invalid Guild
  12989. }
  12990. *calls += value;
  12991. *calls = cap_value(*calls, 0, INT_MAX);
  12992. return 0;
  12993. }
  12994. BUILDIN_FUNC(mercenary_get_faith)
  12995. {
  12996. struct map_session_data *sd = script_rid2sd(st);
  12997. int guild;
  12998. if( sd == NULL )
  12999. return 0;
  13000. guild = script_getnum(st,2);
  13001. switch( guild )
  13002. {
  13003. case ARCH_MERC_GUILD:
  13004. script_pushint(st,sd->status.arch_faith);
  13005. break;
  13006. case SPEAR_MERC_GUILD:
  13007. script_pushint(st,sd->status.spear_faith);
  13008. break;
  13009. case SWORD_MERC_GUILD:
  13010. script_pushint(st,sd->status.sword_faith);
  13011. break;
  13012. default:
  13013. script_pushint(st,0);
  13014. break;
  13015. }
  13016. return 0;
  13017. }
  13018. BUILDIN_FUNC(mercenary_set_faith)
  13019. {
  13020. struct map_session_data *sd = script_rid2sd(st);
  13021. int guild, value, *calls;
  13022. if( sd == NULL )
  13023. return 0;
  13024. guild = script_getnum(st,2);
  13025. value = script_getnum(st,3);
  13026. switch( guild )
  13027. {
  13028. case ARCH_MERC_GUILD:
  13029. calls = &sd->status.arch_faith;
  13030. break;
  13031. case SPEAR_MERC_GUILD:
  13032. calls = &sd->status.spear_faith;
  13033. break;
  13034. case SWORD_MERC_GUILD:
  13035. calls = &sd->status.sword_faith;
  13036. break;
  13037. default:
  13038. return 0; // Invalid Guild
  13039. }
  13040. *calls += value;
  13041. *calls = cap_value(*calls, 0, INT_MAX);
  13042. if( mercenary_get_guild(sd->md) == guild )
  13043. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  13044. return 0;
  13045. }
  13046. /*------------------------------------------
  13047. * Book Reading
  13048. *------------------------------------------*/
  13049. BUILDIN_FUNC(readbook)
  13050. {
  13051. struct map_session_data *sd;
  13052. int book_id, page;
  13053. if( (sd = script_rid2sd(st)) == NULL )
  13054. return 0;
  13055. book_id = script_getnum(st,2);
  13056. page = script_getnum(st,3);
  13057. clif_readbook(sd->fd, book_id, page);
  13058. return 0;
  13059. }
  13060. /******************
  13061. Questlog script commands
  13062. *******************/
  13063. BUILDIN_FUNC(setquest)
  13064. {
  13065. TBL_PC * sd = script_rid2sd(st);
  13066. quest_add(sd, script_getnum(st, 2));
  13067. return 0;
  13068. }
  13069. BUILDIN_FUNC(erasequest)
  13070. {
  13071. TBL_PC * sd = script_rid2sd(st);
  13072. quest_delete(sd, script_getnum(st, 2));
  13073. return 0;
  13074. }
  13075. BUILDIN_FUNC(completequest)
  13076. {
  13077. TBL_PC * sd = script_rid2sd(st);
  13078. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  13079. return 0;
  13080. }
  13081. BUILDIN_FUNC(changequest)
  13082. {
  13083. TBL_PC * sd = script_rid2sd(st);
  13084. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  13085. return 0;
  13086. }
  13087. BUILDIN_FUNC(checkquest)
  13088. {
  13089. TBL_PC * sd = script_rid2sd(st);
  13090. quest_check_type type = HAVEQUEST;
  13091. if( script_hasdata(st, 3) )
  13092. type = (quest_check_type)script_getnum(st, 3);
  13093. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  13094. return 0;
  13095. }
  13096. BUILDIN_FUNC(showevent)
  13097. {
  13098. TBL_PC *sd = script_rid2sd(st);
  13099. struct npc_data *nd = map_id2nd(st->oid);
  13100. int state, color;
  13101. if( sd == NULL || nd == NULL )
  13102. return 0;
  13103. state = script_getnum(st, 2);
  13104. color = script_getnum(st, 3);
  13105. if( color < 0 || color > 3 )
  13106. color = 0; // set default color
  13107. clif_quest_show_event(sd, &nd->bl, state, color);
  13108. return 0;
  13109. }
  13110. /*==========================================
  13111. * BattleGround System
  13112. *------------------------------------------*/
  13113. BUILDIN_FUNC(waitingroom2bg)
  13114. {
  13115. struct npc_data *nd;
  13116. struct chat_data *cd;
  13117. const char *map_name, *ev = "", *dev = "";
  13118. int x, y, i, mapindex = 0, bg_id, n;
  13119. struct map_session_data *sd;
  13120. if( script_hasdata(st,7) )
  13121. nd = npc_name2id(script_getstr(st,7));
  13122. else
  13123. nd = (struct npc_data *)map_id2bl(st->oid);
  13124. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  13125. {
  13126. script_pushint(st,0);
  13127. return 0;
  13128. }
  13129. map_name = script_getstr(st,2);
  13130. if( strcmp(map_name,"-") != 0 )
  13131. {
  13132. mapindex = mapindex_name2id(map_name);
  13133. if( mapindex == 0 )
  13134. { // Invalid Map
  13135. script_pushint(st,0);
  13136. return 0;
  13137. }
  13138. }
  13139. x = script_getnum(st,3);
  13140. y = script_getnum(st,4);
  13141. ev = script_getstr(st,5); // Logout Event
  13142. dev = script_getstr(st,6); // Die Event
  13143. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  13144. { // Creation failed
  13145. script_pushint(st,0);
  13146. return 0;
  13147. }
  13148. n = cd->users;
  13149. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  13150. {
  13151. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  13152. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  13153. else
  13154. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  13155. }
  13156. mapreg_setreg(add_str("$@arenamembersnum"), i);
  13157. script_pushint(st,bg_id);
  13158. return 0;
  13159. }
  13160. BUILDIN_FUNC(waitingroom2bg_single)
  13161. {
  13162. const char* map_name;
  13163. struct npc_data *nd;
  13164. struct chat_data *cd;
  13165. struct map_session_data *sd;
  13166. int x, y, mapindex, bg_id;
  13167. bg_id = script_getnum(st,2);
  13168. map_name = script_getstr(st,3);
  13169. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13170. return 0; // Invalid Map
  13171. x = script_getnum(st,4);
  13172. y = script_getnum(st,5);
  13173. nd = npc_name2id(script_getstr(st,6));
  13174. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  13175. return 0;
  13176. if( (sd = cd->usersd[0]) == NULL )
  13177. return 0;
  13178. if( bg_team_join(bg_id, sd) )
  13179. {
  13180. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  13181. script_pushint(st,1);
  13182. }
  13183. else
  13184. script_pushint(st,0);
  13185. return 0;
  13186. }
  13187. BUILDIN_FUNC(bg_team_setxy)
  13188. {
  13189. struct battleground_data *bg;
  13190. int bg_id;
  13191. bg_id = script_getnum(st,2);
  13192. if( (bg = bg_team_search(bg_id)) == NULL )
  13193. return 0;
  13194. bg->x = script_getnum(st,3);
  13195. bg->y = script_getnum(st,4);
  13196. return 0;
  13197. }
  13198. BUILDIN_FUNC(bg_warp)
  13199. {
  13200. int x, y, mapindex, bg_id;
  13201. const char* map_name;
  13202. bg_id = script_getnum(st,2);
  13203. map_name = script_getstr(st,3);
  13204. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13205. return 0; // Invalid Map
  13206. x = script_getnum(st,4);
  13207. y = script_getnum(st,5);
  13208. bg_team_warp(bg_id, mapindex, x, y);
  13209. return 0;
  13210. }
  13211. BUILDIN_FUNC(bg_monster)
  13212. {
  13213. int class_ = 0, x = 0, y = 0, bg_id = 0;
  13214. const char *str,*map, *evt="";
  13215. bg_id = script_getnum(st,2);
  13216. map = script_getstr(st,3);
  13217. x = script_getnum(st,4);
  13218. y = script_getnum(st,5);
  13219. str = script_getstr(st,6);
  13220. class_ = script_getnum(st,7);
  13221. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  13222. check_event(st, evt);
  13223. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  13224. return 0;
  13225. }
  13226. BUILDIN_FUNC(bg_monster_set_team)
  13227. {
  13228. struct mob_data *md;
  13229. struct block_list *mbl;
  13230. int id = script_getnum(st,2),
  13231. bg_id = script_getnum(st,3);
  13232. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  13233. return 0;
  13234. md = (TBL_MOB *)mbl;
  13235. md->bg_id = bg_id;
  13236. mob_stop_attack(md);
  13237. mob_stop_walking(md, 0);
  13238. md->target_id = md->attacked_id = 0;
  13239. clif_charnameack(0, &md->bl);
  13240. return 0;
  13241. }
  13242. BUILDIN_FUNC(bg_leave)
  13243. {
  13244. struct map_session_data *sd = script_rid2sd(st);
  13245. if( sd == NULL || !sd->bg_id )
  13246. return 0;
  13247. bg_team_leave(sd,0);
  13248. return 0;
  13249. }
  13250. BUILDIN_FUNC(bg_destroy)
  13251. {
  13252. int bg_id = script_getnum(st,2);
  13253. bg_team_delete(bg_id);
  13254. return 0;
  13255. }
  13256. BUILDIN_FUNC(bg_getareausers)
  13257. {
  13258. const char *str;
  13259. int m, x0, y0, x1, y1, bg_id;
  13260. int i = 0, c = 0;
  13261. struct battleground_data *bg = NULL;
  13262. struct map_session_data *sd;
  13263. bg_id = script_getnum(st,2);
  13264. str = script_getstr(st,3);
  13265. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  13266. {
  13267. script_pushint(st,0);
  13268. return 0;
  13269. }
  13270. x0 = script_getnum(st,4);
  13271. y0 = script_getnum(st,5);
  13272. x1 = script_getnum(st,6);
  13273. y1 = script_getnum(st,7);
  13274. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  13275. {
  13276. if( (sd = bg->members[i].sd) == NULL )
  13277. continue;
  13278. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  13279. continue;
  13280. c++;
  13281. }
  13282. script_pushint(st,c);
  13283. return 0;
  13284. }
  13285. BUILDIN_FUNC(bg_updatescore)
  13286. {
  13287. const char *str;
  13288. int m;
  13289. str = script_getstr(st,2);
  13290. if( (m = map_mapname2mapid(str)) < 0 )
  13291. return 0;
  13292. map[m].bgscore_lion = script_getnum(st,3);
  13293. map[m].bgscore_eagle = script_getnum(st,4);
  13294. clif_bg_updatescore(m);
  13295. return 0;
  13296. }
  13297. BUILDIN_FUNC(bg_get_data)
  13298. {
  13299. struct battleground_data *bg;
  13300. int bg_id = script_getnum(st,2),
  13301. type = script_getnum(st,3);
  13302. if( (bg = bg_team_search(bg_id)) == NULL )
  13303. {
  13304. script_pushint(st,0);
  13305. return 0;
  13306. }
  13307. switch( type )
  13308. {
  13309. case 0: script_pushint(st, bg->count); break;
  13310. default:
  13311. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  13312. break;
  13313. }
  13314. return 0;
  13315. }
  13316. /*==========================================
  13317. * Instancing Script Commands
  13318. *------------------------------------------*/
  13319. BUILDIN_FUNC(instance_create)
  13320. {
  13321. const char *name;
  13322. int party_id, res;
  13323. name = script_getstr(st, 2);
  13324. party_id = script_getnum(st, 3);
  13325. res = instance_create(party_id, name);
  13326. if( res == -4 ) // Already exists
  13327. {
  13328. script_pushint(st, -1);
  13329. return 0;
  13330. }
  13331. else if( res < 0 )
  13332. {
  13333. const char *err;
  13334. switch(res)
  13335. {
  13336. case -3: err = "No free instances"; break;
  13337. case -2: err = "Invalid party ID"; break;
  13338. case -1: err = "Invalid type"; break;
  13339. default: err = "Unknown"; break;
  13340. }
  13341. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  13342. script_pushint(st, -2);
  13343. return 0;
  13344. }
  13345. script_pushint(st, res);
  13346. return 0;
  13347. }
  13348. BUILDIN_FUNC(instance_destroy)
  13349. {
  13350. int instance_id;
  13351. struct map_session_data *sd;
  13352. struct party_data *p;
  13353. if( script_hasdata(st, 2) )
  13354. instance_id = script_getnum(st, 2);
  13355. else if( st->instance_id )
  13356. instance_id = st->instance_id;
  13357. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13358. instance_id = p->instance_id;
  13359. else return 0;
  13360. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13361. {
  13362. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  13363. return 0;
  13364. }
  13365. instance_destroy(instance_id);
  13366. return 0;
  13367. }
  13368. BUILDIN_FUNC(instance_attachmap)
  13369. {
  13370. const char *name;
  13371. int m;
  13372. int instance_id;
  13373. bool usebasename = false;
  13374. name = script_getstr(st,2);
  13375. instance_id = script_getnum(st,3);
  13376. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  13377. usebasename = true;
  13378. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  13379. {
  13380. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  13381. script_pushconststr(st, "");
  13382. return 0;
  13383. }
  13384. script_pushconststr(st, map[m].name);
  13385. return 0;
  13386. }
  13387. BUILDIN_FUNC(instance_detachmap)
  13388. {
  13389. struct map_session_data *sd;
  13390. struct party_data *p;
  13391. const char *str;
  13392. int m, instance_id;
  13393. str = script_getstr(st, 2);
  13394. if( script_hasdata(st, 3) )
  13395. instance_id = script_getnum(st, 3);
  13396. else if( st->instance_id )
  13397. instance_id = st->instance_id;
  13398. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13399. instance_id = p->instance_id;
  13400. else return 0;
  13401. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  13402. {
  13403. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  13404. return 0;
  13405. }
  13406. instance_del_map(m);
  13407. return 0;
  13408. }
  13409. BUILDIN_FUNC(instance_attach)
  13410. {
  13411. int instance_id;
  13412. instance_id = script_getnum(st, 2);
  13413. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13414. return 0;
  13415. st->instance_id = instance_id;
  13416. return 0;
  13417. }
  13418. BUILDIN_FUNC(instance_id)
  13419. {
  13420. int type, instance_id;
  13421. struct map_session_data *sd;
  13422. struct party_data *p;
  13423. if( script_hasdata(st, 2) )
  13424. {
  13425. type = script_getnum(st, 2);
  13426. if( type == 0 )
  13427. instance_id = st->instance_id;
  13428. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  13429. instance_id = p->instance_id;
  13430. else
  13431. instance_id = 0;
  13432. }
  13433. else
  13434. instance_id = st->instance_id;
  13435. script_pushint(st, instance_id);
  13436. return 0;
  13437. }
  13438. BUILDIN_FUNC(instance_set_timeout)
  13439. {
  13440. int progress_timeout, idle_timeout;
  13441. int instance_id;
  13442. struct map_session_data *sd;
  13443. struct party_data *p;
  13444. progress_timeout = script_getnum(st, 2);
  13445. idle_timeout = script_getnum(st, 3);
  13446. if( script_hasdata(st, 4) )
  13447. instance_id = script_getnum(st, 4);
  13448. else if( st->instance_id )
  13449. instance_id = st->instance_id;
  13450. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13451. instance_id = p->instance_id;
  13452. else return 0;
  13453. if( instance_id > 0 )
  13454. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  13455. return 0;
  13456. }
  13457. BUILDIN_FUNC(instance_init)
  13458. {
  13459. int instance_id = script_getnum(st, 2);
  13460. if( instance[instance_id].state != INSTANCE_IDLE )
  13461. {
  13462. ShowError("instance_init: instance already initialized.\n");
  13463. return 0;
  13464. }
  13465. instance_init(instance_id);
  13466. return 0;
  13467. }
  13468. BUILDIN_FUNC(instance_announce)
  13469. {
  13470. int instance_id = script_getnum(st,2);
  13471. const char *mes = script_getstr(st,3);
  13472. int flag = script_getnum(st,4);
  13473. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  13474. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  13475. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  13476. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  13477. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  13478. int i;
  13479. struct map_session_data *sd;
  13480. struct party_data *p;
  13481. if( instance_id == 0 )
  13482. {
  13483. if( st->instance_id )
  13484. instance_id = st->instance_id;
  13485. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13486. instance_id = p->instance_id;
  13487. else return 0;
  13488. }
  13489. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13490. return 0;
  13491. for( i = 0; i < instance[instance_id].num_map; i++ )
  13492. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  13493. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  13494. return 0;
  13495. }
  13496. BUILDIN_FUNC(instance_npcname)
  13497. {
  13498. const char *str;
  13499. int instance_id = 0;
  13500. struct map_session_data *sd;
  13501. struct party_data *p;
  13502. struct npc_data *nd;
  13503. str = script_getstr(st, 2);
  13504. if( script_hasdata(st, 3) )
  13505. instance_id = script_getnum(st, 3);
  13506. else if( st->instance_id )
  13507. instance_id = st->instance_id;
  13508. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13509. instance_id = p->instance_id;
  13510. if( instance_id && (nd = npc_name2id(str)) != NULL )
  13511. {
  13512. static char npcname[NAME_LENGTH];
  13513. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  13514. script_pushconststr(st,npcname);
  13515. }
  13516. else
  13517. {
  13518. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  13519. st->state = END;
  13520. return 1;
  13521. }
  13522. return 0;
  13523. }
  13524. BUILDIN_FUNC(has_instance)
  13525. {
  13526. struct map_session_data *sd;
  13527. struct party_data *p;
  13528. const char *str;
  13529. int m, instance_id = 0;
  13530. str = script_getstr(st, 2);
  13531. if( script_hasdata(st, 3) )
  13532. instance_id = script_getnum(st, 3);
  13533. else if( st->instance_id )
  13534. instance_id = st->instance_id;
  13535. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13536. instance_id = p->instance_id;
  13537. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  13538. {
  13539. script_pushconststr(st, "");
  13540. return 0;
  13541. }
  13542. script_pushconststr(st, map[m].name);
  13543. return 0;
  13544. }
  13545. BUILDIN_FUNC(instance_warpall)
  13546. {
  13547. struct map_session_data *pl_sd;
  13548. int m, i, instance_id;
  13549. const char *mapn;
  13550. int x, y;
  13551. unsigned short mapindex;
  13552. struct party_data *p = NULL;
  13553. mapn = script_getstr(st,2);
  13554. x = script_getnum(st,3);
  13555. y = script_getnum(st,4);
  13556. if( script_hasdata(st,5) )
  13557. instance_id = script_getnum(st,5);
  13558. else if( st->instance_id )
  13559. instance_id = st->instance_id;
  13560. 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 )
  13561. instance_id = p->instance_id;
  13562. else return 0;
  13563. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  13564. return 0;
  13565. if( !(p = party_search(instance[instance_id].party_id)) )
  13566. return 0;
  13567. mapindex = map_id2index(m);
  13568. for( i = 0; i < MAX_PARTY; i++ )
  13569. 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);
  13570. return 0;
  13571. }
  13572. /*==========================================
  13573. * Custom Fonts
  13574. *------------------------------------------*/
  13575. BUILDIN_FUNC(setfont)
  13576. {
  13577. struct map_session_data *sd = script_rid2sd(st);
  13578. int font = script_getnum(st,2);
  13579. if( sd == NULL )
  13580. return 0;
  13581. if( sd->user_font != font )
  13582. sd->user_font = font;
  13583. else
  13584. sd->user_font = 0;
  13585. clif_font(sd);
  13586. return 0;
  13587. }
  13588. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  13589. {
  13590. TBL_MOB* md = (TBL_MOB*)bl;
  13591. struct block_list *tbl;
  13592. int mobid = va_arg(ap,int);
  13593. int skillid = va_arg(ap,int);
  13594. int skilllv = va_arg(ap,int);
  13595. int casttime = va_arg(ap,int);
  13596. int cancel = va_arg(ap,int);
  13597. int emotion = va_arg(ap,int);
  13598. int target = va_arg(ap,int);
  13599. if( md->class_ != mobid )
  13600. return 0;
  13601. // 0:self, 1:target, 2:master, default:random
  13602. switch( target )
  13603. {
  13604. case 0: tbl = map_id2bl(md->bl.id); break;
  13605. case 1: tbl = map_id2bl(md->target_id); break;
  13606. case 2: tbl = map_id2bl(md->master_id); break;
  13607. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  13608. }
  13609. if( !tbl )
  13610. return 0;
  13611. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  13612. unit_skillcastcancel(bl,0);
  13613. if( skill_get_casttype(skillid) == CAST_GROUND )
  13614. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  13615. else
  13616. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  13617. clif_emotion(&md->bl, emotion);
  13618. return 0;
  13619. }
  13620. /*==========================================
  13621. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  13622. *------------------------------------------*/
  13623. BUILDIN_FUNC(areamobuseskill)
  13624. {
  13625. struct block_list center;
  13626. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  13627. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  13628. {
  13629. ShowError("areamobuseskill: invalid map name.\n");
  13630. return 0;
  13631. }
  13632. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  13633. return 0;
  13634. center.m = m;
  13635. center.x = script_getnum(st,3);
  13636. center.y = script_getnum(st,4);
  13637. range = script_getnum(st,5);
  13638. mobid = script_getnum(st,6);
  13639. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  13640. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  13641. skilllv = battle_config.mob_max_skilllvl;
  13642. casttime = script_getnum(st,9);
  13643. cancel = script_getnum(st,10);
  13644. emotion = script_getnum(st,11);
  13645. target = script_getnum(st,12);
  13646. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  13647. return 0;
  13648. }
  13649. BUILDIN_FUNC(progressbar)
  13650. {
  13651. struct map_session_data * sd = script_rid2sd(st);
  13652. const char * color;
  13653. unsigned int second;
  13654. if( !st || !sd )
  13655. return 0;
  13656. st->state = STOP;
  13657. color = script_getstr(st,2);
  13658. second = script_getnum(st,3);
  13659. sd->progressbar.npc_id = st->oid;
  13660. sd->progressbar.timeout = gettick() + second*1000;
  13661. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  13662. return 0;
  13663. }
  13664. BUILDIN_FUNC(pushpc)
  13665. {
  13666. int direction, cells, dx, dy;
  13667. struct map_session_data* sd;
  13668. if((sd = script_rid2sd(st))==NULL)
  13669. {
  13670. return 0;
  13671. }
  13672. direction = script_getnum(st,2);
  13673. cells = script_getnum(st,3);
  13674. if(direction<0 || direction>7)
  13675. {
  13676. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  13677. script_reportsrc(st);
  13678. direction%= 8; // trim spin-over
  13679. }
  13680. if(!cells)
  13681. {// zero distance
  13682. return 0;
  13683. }
  13684. else if(cells<0)
  13685. {// pushing backwards
  13686. direction = (direction+4)%8; // turn around
  13687. cells = -cells;
  13688. }
  13689. dx = dirx[direction];
  13690. dy = diry[direction];
  13691. unit_blown(&sd->bl, dx, dy, cells, 0);
  13692. return 0;
  13693. }
  13694. /// Invokes buying store preparation window
  13695. /// buyingstore <slots>;
  13696. BUILDIN_FUNC(buyingstore)
  13697. {
  13698. struct map_session_data* sd;
  13699. if( ( sd = script_rid2sd(st) ) == NULL )
  13700. {
  13701. return 0;
  13702. }
  13703. buyingstore_setup(sd, script_getnum(st,2));
  13704. return 0;
  13705. }
  13706. /// Invokes search store info window
  13707. /// searchstores <uses>,<effect>;
  13708. BUILDIN_FUNC(searchstores)
  13709. {
  13710. unsigned short effect;
  13711. unsigned int uses;
  13712. struct map_session_data* sd;
  13713. if( ( sd = script_rid2sd(st) ) == NULL )
  13714. {
  13715. return 0;
  13716. }
  13717. uses = script_getnum(st,2);
  13718. effect = script_getnum(st,3);
  13719. if( !uses )
  13720. {
  13721. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  13722. return 1;
  13723. }
  13724. if( effect > 1 )
  13725. {
  13726. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  13727. return 1;
  13728. }
  13729. searchstore_open(sd, uses, effect);
  13730. return 0;
  13731. }
  13732. /// Displays a number as large digital clock.
  13733. /// showdigit <value>[,<type>];
  13734. BUILDIN_FUNC(showdigit)
  13735. {
  13736. unsigned int type = 0;
  13737. int value;
  13738. struct map_session_data* sd;
  13739. if( ( sd = script_rid2sd(st) ) == NULL )
  13740. {
  13741. return 0;
  13742. }
  13743. value = script_getnum(st,2);
  13744. if( script_hasdata(st,3) )
  13745. {
  13746. type = script_getnum(st,3);
  13747. if( type > 3 )
  13748. {
  13749. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  13750. return 1;
  13751. }
  13752. }
  13753. clif_showdigit(sd, (unsigned char)type, value);
  13754. return 0;
  13755. }
  13756. /**
  13757. * Rune Knight
  13758. **/
  13759. BUILDIN_FUNC(makerune) {
  13760. TBL_PC* sd;
  13761. if( (sd = script_rid2sd(st)) == NULL )
  13762. return 0;
  13763. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  13764. sd->itemid = script_getnum(st,2);
  13765. return 0;
  13766. }
  13767. /**
  13768. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  13769. **/
  13770. BUILDIN_FUNC(checkdragon) {
  13771. TBL_PC* sd;
  13772. if( (sd = script_rid2sd(st)) == NULL )
  13773. return 0;
  13774. if( sd->sc.option&OPTION_DRAGON )
  13775. script_pushint(st,1);
  13776. else
  13777. script_pushint(st,0);
  13778. return 0;
  13779. }
  13780. /**
  13781. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  13782. * - Toggles the dragon on a RK if he can mount;
  13783. * @param Color - when not provided uses the green dragon;
  13784. * - 1 : Green Dragon
  13785. * - 2 : Brown Dragon
  13786. * - 3 : Gray Dragon
  13787. * - 4 : Blue Dragon
  13788. * - 5 : Red Dragon
  13789. **/
  13790. BUILDIN_FUNC(setdragon) {
  13791. TBL_PC* sd;
  13792. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  13793. unsigned int option = OPTION_DRAGON1;
  13794. if( (sd = script_rid2sd(st)) == NULL )
  13795. return 0;
  13796. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  13797. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  13798. else if ( sd->sc.option&OPTION_DRAGON ) {//Is mounted; release
  13799. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  13800. script_pushint(st,1);
  13801. } else {//Not mounted; Mount now.
  13802. if( color ) {
  13803. option = ( color == 1 ? OPTION_DRAGON1 :
  13804. color == 2 ? OPTION_DRAGON2 :
  13805. color == 3 ? OPTION_DRAGON3 :
  13806. color == 4 ? OPTION_DRAGON4 :
  13807. color == 5 ? OPTION_DRAGON5 : 0);
  13808. if( !option ) {
  13809. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  13810. option = OPTION_DRAGON1;
  13811. }
  13812. }
  13813. pc_setoption(sd, sd->sc.option|option);
  13814. script_pushint(st,1);
  13815. }
  13816. return 0;
  13817. }
  13818. /**
  13819. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  13820. **/
  13821. BUILDIN_FUNC(ismounting) {
  13822. TBL_PC* sd;
  13823. if( (sd = script_rid2sd(st)) == NULL )
  13824. return 0;
  13825. if( sd->sc.option&OPTION_MOUNTING )
  13826. script_pushint(st,1);
  13827. else
  13828. script_pushint(st,0);
  13829. return 0;
  13830. }
  13831. /**
  13832. * setmounting() returns 1 on success or 0 otherwise
  13833. * - Toggles new mounts on a player when he can mount
  13834. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  13835. * - Will unmount the player is he is already mounting
  13836. **/
  13837. BUILDIN_FUNC(setmounting) {
  13838. TBL_PC* sd;
  13839. if( (sd = script_rid2sd(st)) == NULL )
  13840. return 0;
  13841. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  13842. script_pushint(st,0);//can't mount with one of these
  13843. else {
  13844. if( sd->sc.option&OPTION_MOUNTING )
  13845. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  13846. else
  13847. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  13848. script_pushint(st,1);//in both cases, return 1.
  13849. }
  13850. return 0;
  13851. }
  13852. /**
  13853. * Retrieves quantity of arguments provided to callfunc/callsub.
  13854. * getargcount() -> amount of arguments received in a function
  13855. **/
  13856. BUILDIN_FUNC(getargcount) {
  13857. struct script_retinfo* ri;
  13858. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  13859. ShowError("script:getargcount: used out of function or callsub label!\n");
  13860. st->state = END;
  13861. return 1;
  13862. }
  13863. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  13864. script_pushint(st, ri->nargs);
  13865. return 0;
  13866. }
  13867. /**
  13868. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  13869. **/
  13870. BUILDIN_FUNC(is_function) {
  13871. const char* str = script_getstr(st,2);
  13872. if( strdb_exists(userfunc_db, str) )
  13873. script_pushint(st,1);
  13874. else
  13875. script_pushint(st,0);
  13876. return 0;
  13877. }
  13878. /**
  13879. * get_revision() -> retrieves the current svn revision (if available)
  13880. **/
  13881. BUILDIN_FUNC(get_revision) {
  13882. const char * revision;
  13883. if ( (revision = get_svn_revision()) != 0 )
  13884. script_pushint(st,atoi(revision));
  13885. else
  13886. script_pushint(st,-1);//unknown
  13887. return 0;
  13888. }
  13889. /**
  13890. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  13891. **/
  13892. BUILDIN_FUNC(freeloop) {
  13893. if( script_getnum(st,2) )
  13894. st->freeloop = 1;
  13895. else
  13896. st->freeloop = 0;
  13897. script_pushint(st, st->freeloop);
  13898. return 0;
  13899. }
  13900. // declarations that were supposed to be exported from npc_chat.c
  13901. #ifdef PCRE_SUPPORT
  13902. BUILDIN_FUNC(defpattern);
  13903. BUILDIN_FUNC(activatepset);
  13904. BUILDIN_FUNC(deactivatepset);
  13905. BUILDIN_FUNC(deletepset);
  13906. #endif
  13907. /// script command definitions
  13908. /// for an explanation on args, see add_buildin_func
  13909. struct script_function buildin_func[] = {
  13910. // NPC interaction
  13911. BUILDIN_DEF(mes,"s*"),
  13912. BUILDIN_DEF(next,""),
  13913. BUILDIN_DEF(close,""),
  13914. BUILDIN_DEF(close2,""),
  13915. BUILDIN_DEF(menu,"sl*"),
  13916. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  13917. BUILDIN_DEF(prompt,"s*"),
  13918. //
  13919. BUILDIN_DEF(goto,"l"),
  13920. BUILDIN_DEF(callsub,"l*"),
  13921. BUILDIN_DEF(callfunc,"s*"),
  13922. BUILDIN_DEF(return,"?"),
  13923. BUILDIN_DEF(getarg,"i?"),
  13924. BUILDIN_DEF(jobchange,"i?"),
  13925. BUILDIN_DEF(jobname,"i"),
  13926. BUILDIN_DEF(input,"r??"),
  13927. BUILDIN_DEF(warp,"sii"),
  13928. BUILDIN_DEF(areawarp,"siiiisii"),
  13929. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  13930. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  13931. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  13932. BUILDIN_DEF(setlook,"ii"),
  13933. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  13934. BUILDIN_DEF(set,"rv"),
  13935. BUILDIN_DEF(setarray,"rv*"),
  13936. BUILDIN_DEF(cleararray,"rvi"),
  13937. BUILDIN_DEF(copyarray,"rri"),
  13938. BUILDIN_DEF(getarraysize,"r"),
  13939. BUILDIN_DEF(deletearray,"r?"),
  13940. BUILDIN_DEF(getelementofarray,"ri"),
  13941. BUILDIN_DEF(getitem,"vi?"),
  13942. BUILDIN_DEF(rentitem,"vi"),
  13943. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  13944. BUILDIN_DEF(getnameditem,"vv"),
  13945. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  13946. BUILDIN_DEF(makeitem,"visii"),
  13947. BUILDIN_DEF(delitem,"vi?"),
  13948. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  13949. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  13950. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  13951. BUILDIN_DEF(cutin,"si"),
  13952. BUILDIN_DEF(viewpoint,"iiiii"),
  13953. BUILDIN_DEF(heal,"ii"),
  13954. BUILDIN_DEF(itemheal,"ii"),
  13955. BUILDIN_DEF(percentheal,"ii"),
  13956. BUILDIN_DEF(rand,"i?"),
  13957. BUILDIN_DEF(countitem,"v"),
  13958. BUILDIN_DEF(countitem2,"viiiiiii"),
  13959. BUILDIN_DEF(checkweight,"vi"),
  13960. BUILDIN_DEF(readparam,"i?"),
  13961. BUILDIN_DEF(getcharid,"i?"),
  13962. BUILDIN_DEF(getnpcid,"i?"),
  13963. BUILDIN_DEF(getpartyname,"i"),
  13964. BUILDIN_DEF(getpartymember,"i?"),
  13965. BUILDIN_DEF(getpartyleader,"i?"),
  13966. BUILDIN_DEF(getguildname,"i"),
  13967. BUILDIN_DEF(getguildmaster,"i"),
  13968. BUILDIN_DEF(getguildmasterid,"i"),
  13969. BUILDIN_DEF(strcharinfo,"i"),
  13970. BUILDIN_DEF(strnpcinfo,"i"),
  13971. BUILDIN_DEF(getequipid,"i"),
  13972. BUILDIN_DEF(getequipname,"i"),
  13973. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  13974. BUILDIN_DEF(repair,"i"), // [Valaris]
  13975. BUILDIN_DEF(getequipisequiped,"i"),
  13976. BUILDIN_DEF(getequipisenableref,"i"),
  13977. BUILDIN_DEF(getequipisidentify,"i"),
  13978. BUILDIN_DEF(getequiprefinerycnt,"i"),
  13979. BUILDIN_DEF(getequipweaponlv,"i"),
  13980. BUILDIN_DEF(getequippercentrefinery,"i"),
  13981. BUILDIN_DEF(successrefitem,"i"),
  13982. BUILDIN_DEF(failedrefitem,"i"),
  13983. BUILDIN_DEF(statusup,"i"),
  13984. BUILDIN_DEF(statusup2,"ii"),
  13985. BUILDIN_DEF(bonus,"iv"),
  13986. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  13987. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  13988. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  13989. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  13990. BUILDIN_DEF(autobonus,"sii??"),
  13991. BUILDIN_DEF(autobonus2,"sii??"),
  13992. BUILDIN_DEF(autobonus3,"siiv?"),
  13993. BUILDIN_DEF(skill,"vi?"),
  13994. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  13995. BUILDIN_DEF(guildskill,"vi"),
  13996. BUILDIN_DEF(getskilllv,"v"),
  13997. BUILDIN_DEF(getgdskilllv,"iv"),
  13998. BUILDIN_DEF(basicskillcheck,""),
  13999. BUILDIN_DEF(getgmlevel,""),
  14000. BUILDIN_DEF(getgroupid,""),
  14001. BUILDIN_DEF(end,""),
  14002. BUILDIN_DEF(checkoption,"i"),
  14003. BUILDIN_DEF(setoption,"i?"),
  14004. BUILDIN_DEF(setcart,"?"),
  14005. BUILDIN_DEF(checkcart,""),
  14006. BUILDIN_DEF(setfalcon,"?"),
  14007. BUILDIN_DEF(checkfalcon,""),
  14008. BUILDIN_DEF(setriding,"?"),
  14009. BUILDIN_DEF(checkriding,""),
  14010. BUILDIN_DEF2(savepoint,"save","sii"),
  14011. BUILDIN_DEF(savepoint,"sii"),
  14012. BUILDIN_DEF(gettimetick,"i"),
  14013. BUILDIN_DEF(gettime,"i"),
  14014. BUILDIN_DEF(gettimestr,"si"),
  14015. BUILDIN_DEF(openstorage,""),
  14016. BUILDIN_DEF(guildopenstorage,""),
  14017. BUILDIN_DEF(itemskill,"vi"),
  14018. BUILDIN_DEF(produce,"i"),
  14019. BUILDIN_DEF(cooking,"i"),
  14020. BUILDIN_DEF(monster,"siisii?"),
  14021. BUILDIN_DEF(getmobdrops,"i"),
  14022. BUILDIN_DEF(areamonster,"siiiisii?"),
  14023. BUILDIN_DEF(killmonster,"ss?"),
  14024. BUILDIN_DEF(killmonsterall,"s?"),
  14025. BUILDIN_DEF(clone,"siisi????"),
  14026. BUILDIN_DEF(doevent,"s"),
  14027. BUILDIN_DEF(donpcevent,"s"),
  14028. BUILDIN_DEF(cmdothernpc,"ss"),
  14029. BUILDIN_DEF(addtimer,"is"),
  14030. BUILDIN_DEF(deltimer,"s"),
  14031. BUILDIN_DEF(addtimercount,"si"),
  14032. BUILDIN_DEF(initnpctimer,"??"),
  14033. BUILDIN_DEF(stopnpctimer,"??"),
  14034. BUILDIN_DEF(startnpctimer,"??"),
  14035. BUILDIN_DEF(setnpctimer,"i?"),
  14036. BUILDIN_DEF(getnpctimer,"i?"),
  14037. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  14038. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  14039. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  14040. BUILDIN_DEF(announce,"si?????"),
  14041. BUILDIN_DEF(mapannounce,"ssi?????"),
  14042. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  14043. BUILDIN_DEF(getusers,"i"),
  14044. BUILDIN_DEF(getmapguildusers,"si"),
  14045. BUILDIN_DEF(getmapusers,"s"),
  14046. BUILDIN_DEF(getareausers,"siiii"),
  14047. BUILDIN_DEF(getareadropitem,"siiiiv"),
  14048. BUILDIN_DEF(enablenpc,"s"),
  14049. BUILDIN_DEF(disablenpc,"s"),
  14050. BUILDIN_DEF(hideoffnpc,"s"),
  14051. BUILDIN_DEF(hideonnpc,"s"),
  14052. BUILDIN_DEF(sc_start,"iii?"),
  14053. BUILDIN_DEF(sc_start2,"iiii?"),
  14054. BUILDIN_DEF(sc_start4,"iiiiii?"),
  14055. BUILDIN_DEF(sc_end,"i?"),
  14056. BUILDIN_DEF(getstatus, "i?"),
  14057. BUILDIN_DEF(getscrate,"ii?"),
  14058. BUILDIN_DEF(debugmes,"s"),
  14059. BUILDIN_DEF2(catchpet,"pet","i"),
  14060. BUILDIN_DEF2(birthpet,"bpet",""),
  14061. BUILDIN_DEF(resetlvl,"i"),
  14062. BUILDIN_DEF(resetstatus,""),
  14063. BUILDIN_DEF(resetskill,""),
  14064. BUILDIN_DEF(skillpointcount,""),
  14065. BUILDIN_DEF(changebase,"i?"),
  14066. BUILDIN_DEF(changesex,""),
  14067. BUILDIN_DEF(waitingroom,"si?????"),
  14068. BUILDIN_DEF(delwaitingroom,"?"),
  14069. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  14070. BUILDIN_DEF(enablewaitingroomevent,"?"),
  14071. BUILDIN_DEF(disablewaitingroomevent,"?"),
  14072. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  14073. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  14074. BUILDIN_DEF(getwaitingroomstate,"i?"),
  14075. BUILDIN_DEF(warpwaitingpc,"sii?"),
  14076. BUILDIN_DEF(attachrid,"i"),
  14077. BUILDIN_DEF(detachrid,""),
  14078. BUILDIN_DEF(isloggedin,"i?"),
  14079. BUILDIN_DEF(setmapflagnosave,"ssii"),
  14080. BUILDIN_DEF(getmapflag,"si"),
  14081. BUILDIN_DEF(setmapflag,"si?"),
  14082. BUILDIN_DEF(removemapflag,"si?"),
  14083. BUILDIN_DEF(pvpon,"s"),
  14084. BUILDIN_DEF(pvpoff,"s"),
  14085. BUILDIN_DEF(gvgon,"s"),
  14086. BUILDIN_DEF(gvgoff,"s"),
  14087. BUILDIN_DEF(emotion,"i??"),
  14088. BUILDIN_DEF(maprespawnguildid,"sii"),
  14089. BUILDIN_DEF(agitstart,""), // <Agit>
  14090. BUILDIN_DEF(agitend,""),
  14091. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  14092. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  14093. BUILDIN_DEF(getcastlename,"s"),
  14094. BUILDIN_DEF(getcastledata,"si"),
  14095. BUILDIN_DEF(setcastledata,"sii"),
  14096. BUILDIN_DEF(requestguildinfo,"i?"),
  14097. BUILDIN_DEF(getequipcardcnt,"i"),
  14098. BUILDIN_DEF(successremovecards,"i"),
  14099. BUILDIN_DEF(failedremovecards,"ii"),
  14100. BUILDIN_DEF(marriage,"s"),
  14101. BUILDIN_DEF2(wedding_effect,"wedding",""),
  14102. BUILDIN_DEF(divorce,""),
  14103. BUILDIN_DEF(ispartneron,""),
  14104. BUILDIN_DEF(getpartnerid,""),
  14105. BUILDIN_DEF(getchildid,""),
  14106. BUILDIN_DEF(getmotherid,""),
  14107. BUILDIN_DEF(getfatherid,""),
  14108. BUILDIN_DEF(warppartner,"sii"),
  14109. BUILDIN_DEF(getitemname,"v"),
  14110. BUILDIN_DEF(getitemslots,"i"),
  14111. BUILDIN_DEF(makepet,"i"),
  14112. BUILDIN_DEF(getexp,"ii"),
  14113. BUILDIN_DEF(getinventorylist,""),
  14114. BUILDIN_DEF(getskilllist,""),
  14115. BUILDIN_DEF(clearitem,""),
  14116. BUILDIN_DEF(classchange,"ii"),
  14117. BUILDIN_DEF(misceffect,"i"),
  14118. BUILDIN_DEF(playBGM,"s"),
  14119. BUILDIN_DEF(playBGMall,"s?????"),
  14120. BUILDIN_DEF(soundeffect,"si"),
  14121. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  14122. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  14123. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  14124. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  14125. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  14126. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  14127. BUILDIN_DEF(petloot,"i"), // [Valaris]
  14128. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  14129. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  14130. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  14131. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  14132. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  14133. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  14134. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  14135. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  14136. BUILDIN_DEF(nude,""), // nude command [Valaris]
  14137. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  14138. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  14139. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  14140. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  14141. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  14142. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  14143. BUILDIN_DEF(mobcount,"ss"),
  14144. BUILDIN_DEF(getlook,"i"),
  14145. BUILDIN_DEF(getsavepoint,"i"),
  14146. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  14147. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  14148. BUILDIN_DEF(npcstop,""), // [Valaris]
  14149. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  14150. BUILDIN_DEF(checkoption1,"i"),
  14151. BUILDIN_DEF(checkoption2,"i"),
  14152. BUILDIN_DEF(guildgetexp,"i"),
  14153. BUILDIN_DEF(guildchangegm,"is"),
  14154. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  14155. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  14156. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  14157. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  14158. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  14159. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  14160. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  14161. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  14162. BUILDIN_DEF(night,""), // sets the server to night time
  14163. BUILDIN_DEF(day,""), // sets the server to day time
  14164. #ifdef PCRE_SUPPORT
  14165. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  14166. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  14167. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  14168. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  14169. #endif
  14170. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  14171. BUILDIN_DEF(getusersname,""),
  14172. BUILDIN_DEF(recovery,""),
  14173. BUILDIN_DEF(getpetinfo,"i"),
  14174. BUILDIN_DEF(gethominfo,"i"),
  14175. BUILDIN_DEF(getmercinfo,"i?"),
  14176. BUILDIN_DEF(checkequipedcard,"i"),
  14177. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  14178. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  14179. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  14180. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  14181. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  14182. BUILDIN_DEF(charat,"si"),
  14183. BUILDIN_DEF(setchar,"ssi"),
  14184. BUILDIN_DEF(insertchar,"ssi"),
  14185. BUILDIN_DEF(delchar,"si"),
  14186. BUILDIN_DEF(strtoupper,"s"),
  14187. BUILDIN_DEF(strtolower,"s"),
  14188. BUILDIN_DEF(charisupper, "si"),
  14189. BUILDIN_DEF(charislower, "si"),
  14190. BUILDIN_DEF(substr,"sii"),
  14191. BUILDIN_DEF(explode, "rss"),
  14192. BUILDIN_DEF(implode, "r?"),
  14193. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  14194. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  14195. BUILDIN_DEF(strpos,"ss?"),
  14196. BUILDIN_DEF(replacestr,"sss??"),
  14197. BUILDIN_DEF(countstr,"ss?"),
  14198. BUILDIN_DEF(setnpcdisplay,"sv??"),
  14199. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  14200. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  14201. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  14202. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  14203. // [zBuffer] List of mathematics commands --->
  14204. BUILDIN_DEF(sqrt,"i"),
  14205. BUILDIN_DEF(pow,"ii"),
  14206. BUILDIN_DEF(distance,"iiii"),
  14207. // <--- [zBuffer] List of mathematics commands
  14208. BUILDIN_DEF(md5,"s"),
  14209. // [zBuffer] List of dynamic var commands --->
  14210. BUILDIN_DEF(getd,"s"),
  14211. BUILDIN_DEF(setd,"sv"),
  14212. // <--- [zBuffer] List of dynamic var commands
  14213. BUILDIN_DEF(petstat,"i"),
  14214. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  14215. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  14216. BUILDIN_DEF(npcshopadditem,"sii*"),
  14217. BUILDIN_DEF(npcshopdelitem,"si*"),
  14218. BUILDIN_DEF(npcshopattach,"s?"),
  14219. BUILDIN_DEF(equip,"i"),
  14220. BUILDIN_DEF(autoequip,"ii"),
  14221. BUILDIN_DEF(setbattleflag,"si"),
  14222. BUILDIN_DEF(getbattleflag,"s"),
  14223. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  14224. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  14225. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  14226. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  14227. BUILDIN_DEF(axtoi,"s"),
  14228. BUILDIN_DEF(query_sql,"s*"),
  14229. BUILDIN_DEF(query_logsql,"s*"),
  14230. BUILDIN_DEF(escape_sql,"v"),
  14231. BUILDIN_DEF(atoi,"s"),
  14232. // [zBuffer] List of player cont commands --->
  14233. BUILDIN_DEF(rid2name,"i"),
  14234. BUILDIN_DEF(pcfollow,"ii"),
  14235. BUILDIN_DEF(pcstopfollow,"i"),
  14236. BUILDIN_DEF(pcblockmove,"ii"),
  14237. // <--- [zBuffer] List of player cont commands
  14238. // [zBuffer] List of mob control commands --->
  14239. BUILDIN_DEF(unitwalk,"ii?"),
  14240. BUILDIN_DEF(unitkill,"i"),
  14241. BUILDIN_DEF(unitwarp,"isii"),
  14242. BUILDIN_DEF(unitattack,"iv?"),
  14243. BUILDIN_DEF(unitstop,"i"),
  14244. BUILDIN_DEF(unittalk,"is"),
  14245. BUILDIN_DEF(unitemote,"ii"),
  14246. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  14247. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  14248. // <--- [zBuffer] List of mob control commands
  14249. BUILDIN_DEF(sleep,"i"),
  14250. BUILDIN_DEF(sleep2,"i"),
  14251. BUILDIN_DEF(awake,"s"),
  14252. BUILDIN_DEF(getvariableofnpc,"rs"),
  14253. BUILDIN_DEF(warpportal,"iisii"),
  14254. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  14255. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  14256. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  14257. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  14258. BUILDIN_DEF(checkvending,"?"),
  14259. BUILDIN_DEF(checkchatting,"?"),
  14260. BUILDIN_DEF(openmail,""),
  14261. BUILDIN_DEF(openauction,""),
  14262. BUILDIN_DEF(checkcell,"siii"),
  14263. BUILDIN_DEF(setcell,"siiiiii"),
  14264. BUILDIN_DEF(setwall,"siiiiis"),
  14265. BUILDIN_DEF(delwall,"s"),
  14266. BUILDIN_DEF(searchitem,"rs"),
  14267. BUILDIN_DEF(mercenary_create,"ii"),
  14268. BUILDIN_DEF(mercenary_heal,"ii"),
  14269. BUILDIN_DEF(mercenary_sc_start,"iii"),
  14270. BUILDIN_DEF(mercenary_get_calls,"i"),
  14271. BUILDIN_DEF(mercenary_get_faith,"i"),
  14272. BUILDIN_DEF(mercenary_set_calls,"ii"),
  14273. BUILDIN_DEF(mercenary_set_faith,"ii"),
  14274. BUILDIN_DEF(readbook,"ii"),
  14275. BUILDIN_DEF(setfont,"i"),
  14276. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  14277. BUILDIN_DEF(progressbar,"si"),
  14278. BUILDIN_DEF(pushpc,"ii"),
  14279. BUILDIN_DEF(buyingstore,"i"),
  14280. BUILDIN_DEF(searchstores,"ii"),
  14281. BUILDIN_DEF(showdigit,"i?"),
  14282. // WoE SE
  14283. BUILDIN_DEF(agitstart2,""),
  14284. BUILDIN_DEF(agitend2,""),
  14285. BUILDIN_DEF(agitcheck2,""),
  14286. // BattleGround
  14287. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  14288. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  14289. BUILDIN_DEF(bg_team_setxy,"iii"),
  14290. BUILDIN_DEF(bg_warp,"isii"),
  14291. BUILDIN_DEF(bg_monster,"isiisi?"),
  14292. BUILDIN_DEF(bg_monster_set_team,"ii"),
  14293. BUILDIN_DEF(bg_leave,""),
  14294. BUILDIN_DEF(bg_destroy,"i"),
  14295. BUILDIN_DEF(areapercentheal,"siiiiii"),
  14296. BUILDIN_DEF(bg_get_data,"ii"),
  14297. BUILDIN_DEF(bg_getareausers,"isiiii"),
  14298. BUILDIN_DEF(bg_updatescore,"sii"),
  14299. // Instancing
  14300. BUILDIN_DEF(instance_create,"si"),
  14301. BUILDIN_DEF(instance_destroy,"?"),
  14302. BUILDIN_DEF(instance_attachmap,"si?"),
  14303. BUILDIN_DEF(instance_detachmap,"s?"),
  14304. BUILDIN_DEF(instance_attach,"i"),
  14305. BUILDIN_DEF(instance_id,"?"),
  14306. BUILDIN_DEF(instance_set_timeout,"ii?"),
  14307. BUILDIN_DEF(instance_init,"i"),
  14308. BUILDIN_DEF(instance_announce,"isi?????"),
  14309. BUILDIN_DEF(instance_npcname,"s?"),
  14310. BUILDIN_DEF(has_instance,"s?"),
  14311. BUILDIN_DEF(instance_warpall,"sii?"),
  14312. /**
  14313. * 3rd-related
  14314. **/
  14315. BUILDIN_DEF(makerune,"i"),
  14316. BUILDIN_DEF(checkdragon,""),//[Ind]
  14317. BUILDIN_DEF(setdragon,"?"),//[Ind]
  14318. BUILDIN_DEF(ismounting,""),//[Ind]
  14319. BUILDIN_DEF(setmounting,""),//[Ind]
  14320. /**
  14321. * rAthena and beyond!
  14322. **/
  14323. BUILDIN_DEF(getargcount,""),
  14324. BUILDIN_DEF(is_function,"s"),
  14325. BUILDIN_DEF(get_revision,""),
  14326. BUILDIN_DEF(freeloop,"i"),
  14327. //Quest Log System [Inkfish]
  14328. BUILDIN_DEF(setquest, "i"),
  14329. BUILDIN_DEF(erasequest, "i"),
  14330. BUILDIN_DEF(completequest, "i"),
  14331. BUILDIN_DEF(checkquest, "i?"),
  14332. BUILDIN_DEF(changequest, "ii"),
  14333. BUILDIN_DEF(showevent, "ii"),
  14334. {NULL,NULL,NULL},
  14335. };