script.c 397 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319
  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. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  852. p = skip_word(p);
  853. p = skip_space(p);
  854. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  855. syntax.curly[syntax.curly_count].count = 0;
  856. if( *p == ';' )
  857. {// <func name> ';'
  858. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  859. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  860. {// <func name> '(' ')'
  861. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  862. p = p2;
  863. /*
  864. } else if( 0 && require_paren && *p != '(' )
  865. {// <func name>
  866. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  867. */
  868. } else
  869. {// <func name> <arg list>
  870. if( require_paren ){
  871. if( *p != '(' )
  872. disp_error_message("need '('",p);
  873. ++p; // skip '('
  874. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  875. } else if( *p == '(' ){
  876. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  877. } else {
  878. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  879. }
  880. ++syntax.curly_count;
  881. while( *arg ) {
  882. p2=parse_subexpr(p,-1);
  883. if( p == p2 )
  884. break; // not an argument
  885. if( *arg != '*' )
  886. ++arg; // next argument
  887. p=skip_space(p2);
  888. if( *arg == 0 || *p != ',' )
  889. break; // no more arguments
  890. ++p; // skip comma
  891. }
  892. --syntax.curly_count;
  893. }
  894. if( *arg && *arg != '?' && *arg != '*' )
  895. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  896. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  897. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  898. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  899. if( *p != ')' )
  900. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  901. ++p;
  902. }
  903. add_scriptc(C_FUNC);
  904. return p;
  905. }
  906. /// Processes end of logical script line.
  907. /// @param first When true, only fix up scheduling data is initialized
  908. /// @param p Script position for error reporting in set_label
  909. static void parse_nextline(bool first, const char* p)
  910. {
  911. if( !first )
  912. {
  913. add_scriptc(C_EOL); // mark end of line for stack cleanup
  914. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  915. }
  916. // initialize data for new '-' label fix up scheduling
  917. str_data[LABEL_NEXTLINE].type = C_NOP;
  918. str_data[LABEL_NEXTLINE].backpatch = -1;
  919. str_data[LABEL_NEXTLINE].label = -1;
  920. }
  921. /*==========================================
  922. * 項の解析
  923. *------------------------------------------*/
  924. const char* parse_simpleexpr(const char *p)
  925. {
  926. int i;
  927. p=skip_space(p);
  928. if(*p==';' || *p==',')
  929. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  930. if(*p=='('){
  931. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  932. ++syntax.curly[i].count;
  933. p=parse_subexpr(p+1,-1);
  934. p=skip_space(p);
  935. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  936. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  937. ){
  938. if( *p == ',' ){
  939. syntax.curly[i].flag = ARGLIST_PAREN;
  940. return p;
  941. } else
  942. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  943. }
  944. if( *p != ')' )
  945. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  946. ++p;
  947. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  948. char *np;
  949. i=strtoul(p,&np,0);
  950. add_scripti(i);
  951. p=np;
  952. } else if(*p=='"'){
  953. add_scriptc(C_STR);
  954. p++;
  955. while( *p && *p != '"' ){
  956. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  957. {
  958. char buf[8];
  959. size_t len = skip_escaped_c(p) - p;
  960. size_t n = sv_unescape_c(buf, p, len);
  961. if( n != 1 )
  962. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  963. p += len;
  964. add_scriptb(*buf);
  965. continue;
  966. }
  967. else if( *p == '\n' )
  968. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  969. add_scriptb(*p++);
  970. }
  971. if(!*p)
  972. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  973. add_scriptb(0);
  974. p++; //'"'
  975. } else {
  976. int l;
  977. // label , register , function etc
  978. if(skip_word(p)==p)
  979. disp_error_message("parse_simpleexpr: unexpected character",p);
  980. l=add_word(p);
  981. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  982. return parse_callfunc(p,1);
  983. p=skip_word(p);
  984. if( *p == '[' ){
  985. // array(name[i] => getelementofarray(name,i) )
  986. add_scriptl(search_str("getelementofarray"));
  987. add_scriptc(C_ARG);
  988. add_scriptl(l);
  989. p=parse_subexpr(p+1,-1);
  990. p=skip_space(p);
  991. if( *p != ']' )
  992. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  993. ++p;
  994. add_scriptc(C_FUNC);
  995. }else
  996. add_scriptl(l);
  997. }
  998. return p;
  999. }
  1000. /*==========================================
  1001. * 式の解析
  1002. *------------------------------------------*/
  1003. const char* parse_subexpr(const char* p,int limit)
  1004. {
  1005. int op,opl,len;
  1006. const char* tmpp;
  1007. p=skip_space(p);
  1008. if(*p=='-'){
  1009. tmpp=skip_space(p+1);
  1010. if(*tmpp==';' || *tmpp==','){
  1011. add_scriptl(LABEL_NEXTLINE);
  1012. p++;
  1013. return p;
  1014. }
  1015. }
  1016. tmpp=p;
  1017. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1018. p=parse_subexpr(p+1,10);
  1019. add_scriptc(op);
  1020. } else
  1021. p=parse_simpleexpr(p);
  1022. p=skip_space(p);
  1023. while((
  1024. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1025. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1026. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1027. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1028. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1029. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1030. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1031. (op=C_AND,opl=6,len=1,*p=='&') ||
  1032. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1033. (op=C_OR,opl=5,len=1,*p=='|') ||
  1034. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1035. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1036. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1037. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1038. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1039. (op=C_GT,opl=3,len=1,*p=='>') ||
  1040. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1041. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1042. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1043. p+=len;
  1044. if(op == C_OP3) {
  1045. p=parse_subexpr(p,-1);
  1046. p=skip_space(p);
  1047. if( *(p++) != ':')
  1048. disp_error_message("parse_subexpr: need ':'", p-1);
  1049. p=parse_subexpr(p,-1);
  1050. } else {
  1051. p=parse_subexpr(p,opl);
  1052. }
  1053. add_scriptc(op);
  1054. p=skip_space(p);
  1055. }
  1056. return p; /* return first untreated operator */
  1057. }
  1058. /*==========================================
  1059. * 式の評価
  1060. *------------------------------------------*/
  1061. const char* parse_expr(const char *p)
  1062. {
  1063. switch(*p){
  1064. case ')': case ';': case ':': case '[': case ']':
  1065. case '}':
  1066. disp_error_message("parse_expr: unexpected char",p);
  1067. }
  1068. p=parse_subexpr(p,-1);
  1069. return p;
  1070. }
  1071. /*==========================================
  1072. * 行の解析
  1073. *------------------------------------------*/
  1074. const char* parse_line(const char* p)
  1075. {
  1076. const char* p2;
  1077. p=skip_space(p);
  1078. if(*p==';') {
  1079. // if(); for(); while(); のために閉じ判定
  1080. p = parse_syntax_close(p + 1);
  1081. return p;
  1082. }
  1083. if(*p==')' && parse_syntax_for_flag)
  1084. return p+1;
  1085. p = skip_space(p);
  1086. if(p[0] == '{') {
  1087. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1088. syntax.curly[syntax.curly_count].count = -1;
  1089. syntax.curly[syntax.curly_count].index = -1;
  1090. syntax.curly_count++;
  1091. return p + 1;
  1092. } else if(p[0] == '}') {
  1093. return parse_curly_close(p);
  1094. }
  1095. // 構文関連の処理
  1096. p2 = parse_syntax(p);
  1097. if(p2 != NULL)
  1098. return p2;
  1099. p = parse_callfunc(p,0);
  1100. p = skip_space(p);
  1101. if(parse_syntax_for_flag) {
  1102. if( *p != ')' )
  1103. disp_error_message("parse_line: need ')'",p);
  1104. } else {
  1105. if( *p != ';' )
  1106. disp_error_message("parse_line: need ';'",p);
  1107. }
  1108. // if, for , while の閉じ判定
  1109. p = parse_syntax_close(p+1);
  1110. return p;
  1111. }
  1112. // { ... } の閉じ処理
  1113. const char* parse_curly_close(const char* p)
  1114. {
  1115. if(syntax.curly_count <= 0) {
  1116. disp_error_message("parse_curly_close: unexpected string",p);
  1117. return p + 1;
  1118. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1119. syntax.curly_count--;
  1120. // if, for , while の閉じ判定
  1121. p = parse_syntax_close(p + 1);
  1122. return p;
  1123. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1124. // switch() 閉じ判定
  1125. int pos = syntax.curly_count-1;
  1126. char label[256];
  1127. int l;
  1128. // 一時変数を消す
  1129. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1130. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1131. parse_line(label);
  1132. syntax.curly_count--;
  1133. // 無条件で終了ポインタに移動
  1134. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1135. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1136. parse_line(label);
  1137. syntax.curly_count--;
  1138. // 現在地のラベルを付ける
  1139. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1140. l=add_str(label);
  1141. set_label(l,script_pos, p);
  1142. if(syntax.curly[pos].flag) {
  1143. // default が存在する
  1144. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1145. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1146. parse_line(label);
  1147. syntax.curly_count--;
  1148. }
  1149. // 終了ラベルを付ける
  1150. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1151. l=add_str(label);
  1152. set_label(l,script_pos, p);
  1153. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1154. syntax.curly_count--;
  1155. // if, for , while の閉じ判定
  1156. p = parse_syntax_close(p + 1);
  1157. return p;
  1158. } else {
  1159. disp_error_message("parse_curly_close: unexpected string",p);
  1160. return p + 1;
  1161. }
  1162. }
  1163. // 構文関連の処理
  1164. // break, case, continue, default, do, for, function,
  1165. // if, switch, while をこの内部で処理します。
  1166. const char* parse_syntax(const char* p)
  1167. {
  1168. const char *p2 = skip_word(p);
  1169. switch(*p) {
  1170. case 'B':
  1171. case 'b':
  1172. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1173. // break の処理
  1174. char label[256];
  1175. int pos = syntax.curly_count - 1;
  1176. while(pos >= 0) {
  1177. if(syntax.curly[pos].type == TYPE_DO) {
  1178. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1179. break;
  1180. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1181. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1182. break;
  1183. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1184. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1185. break;
  1186. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1187. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1188. break;
  1189. }
  1190. pos--;
  1191. }
  1192. if(pos < 0) {
  1193. disp_error_message("parse_syntax: unexpected 'break'",p);
  1194. } else {
  1195. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1196. parse_line(label);
  1197. syntax.curly_count--;
  1198. }
  1199. p = skip_space(p2);
  1200. if(*p != ';')
  1201. disp_error_message("parse_syntax: need ';'",p);
  1202. // if, for , while の閉じ判定
  1203. p = parse_syntax_close(p + 1);
  1204. return p;
  1205. }
  1206. break;
  1207. case 'c':
  1208. case 'C':
  1209. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1210. // case の処理
  1211. int pos = syntax.curly_count-1;
  1212. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1213. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1214. return p+1;
  1215. } else {
  1216. char label[256];
  1217. int l,v;
  1218. char *np;
  1219. if(syntax.curly[pos].count != 1) {
  1220. // FALLTHRU 用のジャンプ
  1221. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1222. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1223. parse_line(label);
  1224. syntax.curly_count--;
  1225. // 現在地のラベルを付ける
  1226. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1227. l=add_str(label);
  1228. set_label(l,script_pos, p);
  1229. }
  1230. // switch 判定文
  1231. p = skip_space(p2);
  1232. if(p == p2) {
  1233. disp_error_message("parse_syntax: expect space ' '",p);
  1234. }
  1235. // check whether case label is integer or not
  1236. v = strtol(p,&np,0);
  1237. if(np == p) { //Check for constants
  1238. p2 = skip_word(p);
  1239. v = p2-p; // length of word at p2
  1240. memcpy(label,p,v);
  1241. label[v]='\0';
  1242. if( !script_get_constant(label, &v) )
  1243. disp_error_message("parse_syntax: 'case' label not integer",p);
  1244. p = skip_word(p);
  1245. } else { //Numeric value
  1246. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1247. p++;
  1248. p = skip_word(p);
  1249. if(np != p)
  1250. disp_error_message("parse_syntax: 'case' label not integer",np);
  1251. }
  1252. p = skip_space(p);
  1253. if(*p != ':')
  1254. disp_error_message("parse_syntax: expect ':'",p);
  1255. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1256. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1257. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1258. // 2回parse しないとダメ
  1259. p2 = parse_line(label);
  1260. parse_line(p2);
  1261. syntax.curly_count--;
  1262. if(syntax.curly[pos].count != 1) {
  1263. // FALLTHRU 終了後のラベル
  1264. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1265. l=add_str(label);
  1266. set_label(l,script_pos,p);
  1267. }
  1268. // check duplication of case label [Rayce]
  1269. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1270. disp_error_message("parse_syntax: dup 'case'",p);
  1271. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1272. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1273. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1274. parse_line(label);
  1275. syntax.curly_count--;
  1276. syntax.curly[pos].count++;
  1277. }
  1278. return p + 1;
  1279. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1280. // continue の処理
  1281. char label[256];
  1282. int pos = syntax.curly_count - 1;
  1283. while(pos >= 0) {
  1284. if(syntax.curly[pos].type == TYPE_DO) {
  1285. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1286. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1287. break;
  1288. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1289. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1290. break;
  1291. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1292. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1293. break;
  1294. }
  1295. pos--;
  1296. }
  1297. if(pos < 0) {
  1298. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1299. } else {
  1300. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1301. parse_line(label);
  1302. syntax.curly_count--;
  1303. }
  1304. p = skip_space(p2);
  1305. if(*p != ';')
  1306. disp_error_message("parse_syntax: need ';'",p);
  1307. // if, for , while の閉じ判定
  1308. p = parse_syntax_close(p + 1);
  1309. return p;
  1310. }
  1311. break;
  1312. case 'd':
  1313. case 'D':
  1314. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1315. // switch - default の処理
  1316. int pos = syntax.curly_count-1;
  1317. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1318. disp_error_message("parse_syntax: unexpected 'default'",p);
  1319. } else if(syntax.curly[pos].flag) {
  1320. disp_error_message("parse_syntax: dup 'default'",p);
  1321. } else {
  1322. char label[256];
  1323. int l;
  1324. // 現在地のラベルを付ける
  1325. p = skip_space(p2);
  1326. if(*p != ':') {
  1327. disp_error_message("parse_syntax: need ':'",p);
  1328. }
  1329. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1330. l=add_str(label);
  1331. set_label(l,script_pos,p);
  1332. // 無条件で次のリンクに飛ばす
  1333. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1334. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1335. parse_line(label);
  1336. syntax.curly_count--;
  1337. // default のラベルを付ける
  1338. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1339. l=add_str(label);
  1340. set_label(l,script_pos,p);
  1341. syntax.curly[syntax.curly_count - 1].flag = 1;
  1342. syntax.curly[pos].count++;
  1343. }
  1344. return p + 1;
  1345. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1346. int l;
  1347. char label[256];
  1348. p=skip_space(p2);
  1349. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1350. syntax.curly[syntax.curly_count].count = 1;
  1351. syntax.curly[syntax.curly_count].index = syntax.index++;
  1352. syntax.curly[syntax.curly_count].flag = 0;
  1353. // 現在地のラベル形成する
  1354. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1355. l=add_str(label);
  1356. set_label(l,script_pos,p);
  1357. syntax.curly_count++;
  1358. return p;
  1359. }
  1360. break;
  1361. case 'f':
  1362. case 'F':
  1363. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1364. int l;
  1365. char label[256];
  1366. int pos = syntax.curly_count;
  1367. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1368. syntax.curly[syntax.curly_count].count = 1;
  1369. syntax.curly[syntax.curly_count].index = syntax.index++;
  1370. syntax.curly[syntax.curly_count].flag = 0;
  1371. syntax.curly_count++;
  1372. p=skip_space(p2);
  1373. if(*p != '(')
  1374. disp_error_message("parse_syntax: need '('",p);
  1375. p++;
  1376. // 初期化文を実行する
  1377. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1378. p=parse_line(p);
  1379. syntax.curly_count--;
  1380. // 条件判断開始のラベル形成する
  1381. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1382. l=add_str(label);
  1383. set_label(l,script_pos,p);
  1384. p=skip_space(p);
  1385. if(*p == ';') {
  1386. // for(;;) のパターンなので必ず真
  1387. ;
  1388. } else {
  1389. // 条件が偽なら終了地点に飛ばす
  1390. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1391. add_scriptl(add_str("jump_zero"));
  1392. add_scriptc(C_ARG);
  1393. p=parse_expr(p);
  1394. p=skip_space(p);
  1395. add_scriptl(add_str(label));
  1396. add_scriptc(C_FUNC);
  1397. }
  1398. if(*p != ';')
  1399. disp_error_message("parse_syntax: need ';'",p);
  1400. p++;
  1401. // ループ開始に飛ばす
  1402. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1403. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1404. parse_line(label);
  1405. syntax.curly_count--;
  1406. // 次のループへのラベル形成する
  1407. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1408. l=add_str(label);
  1409. set_label(l,script_pos,p);
  1410. // 次のループに入る時の処理
  1411. // for 最後の ')' を ';' として扱うフラグ
  1412. parse_syntax_for_flag = 1;
  1413. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1414. p=parse_line(p);
  1415. syntax.curly_count--;
  1416. parse_syntax_for_flag = 0;
  1417. // 条件判定処理に飛ばす
  1418. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1419. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1420. parse_line(label);
  1421. syntax.curly_count--;
  1422. // ループ開始のラベル付け
  1423. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1424. l=add_str(label);
  1425. set_label(l,script_pos,p);
  1426. return p;
  1427. }
  1428. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1429. {// internal script function
  1430. const char *func_name;
  1431. func_name = skip_space(p2);
  1432. p = skip_word(func_name);
  1433. if( p == func_name )
  1434. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1435. p2 = skip_space(p);
  1436. if( *p2 == ';' )
  1437. {// function <name> ;
  1438. // function declaration - just register the name
  1439. int l;
  1440. l = add_word(func_name);
  1441. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1442. str_data[l].type = C_USERFUNC;
  1443. else if( str_data[l].type == C_USERFUNC )
  1444. ; // already registered
  1445. else
  1446. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1447. // if, for , while の閉じ判定
  1448. p = parse_syntax_close(p2 + 1);
  1449. return p;
  1450. }
  1451. else if(*p2 == '{')
  1452. {// function <name> <line/block of code>
  1453. char label[256];
  1454. int l;
  1455. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1456. syntax.curly[syntax.curly_count].count = 1;
  1457. syntax.curly[syntax.curly_count].index = syntax.index++;
  1458. syntax.curly[syntax.curly_count].flag = 0;
  1459. ++syntax.curly_count;
  1460. // Jump over the function code
  1461. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1462. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1463. ++syntax.curly_count;
  1464. parse_line(label);
  1465. --syntax.curly_count;
  1466. // Set the position of the function (label)
  1467. l=add_word(func_name);
  1468. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1469. {
  1470. str_data[l].type = C_USERFUNC;
  1471. set_label(l, script_pos, p);
  1472. if( parse_options&SCRIPT_USE_LABEL_DB )
  1473. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1474. }
  1475. else
  1476. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1477. return skip_space(p);
  1478. }
  1479. else
  1480. {
  1481. disp_error_message("expect ';' or '{' at function syntax",p);
  1482. }
  1483. }
  1484. break;
  1485. case 'i':
  1486. case 'I':
  1487. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1488. // if() の処理
  1489. char label[256];
  1490. p=skip_space(p2);
  1491. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1492. disp_error_message("need '('",p);
  1493. }
  1494. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1495. syntax.curly[syntax.curly_count].count = 1;
  1496. syntax.curly[syntax.curly_count].index = syntax.index++;
  1497. syntax.curly[syntax.curly_count].flag = 0;
  1498. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1499. syntax.curly_count++;
  1500. add_scriptl(add_str("jump_zero"));
  1501. add_scriptc(C_ARG);
  1502. p=parse_expr(p);
  1503. p=skip_space(p);
  1504. add_scriptl(add_str(label));
  1505. add_scriptc(C_FUNC);
  1506. return p;
  1507. }
  1508. break;
  1509. case 's':
  1510. case 'S':
  1511. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1512. // switch() の処理
  1513. char label[256];
  1514. p=skip_space(p2);
  1515. if(*p != '(') {
  1516. disp_error_message("need '('",p);
  1517. }
  1518. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1519. syntax.curly[syntax.curly_count].count = 1;
  1520. syntax.curly[syntax.curly_count].index = syntax.index++;
  1521. syntax.curly[syntax.curly_count].flag = 0;
  1522. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1523. syntax.curly_count++;
  1524. add_scriptl(add_str("set"));
  1525. add_scriptc(C_ARG);
  1526. add_scriptl(add_str(label));
  1527. p=parse_expr(p);
  1528. p=skip_space(p);
  1529. if(*p != '{') {
  1530. disp_error_message("parse_syntax: need '{'",p);
  1531. }
  1532. add_scriptc(C_FUNC);
  1533. return p + 1;
  1534. }
  1535. break;
  1536. case 'w':
  1537. case 'W':
  1538. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1539. int l;
  1540. char label[256];
  1541. p=skip_space(p2);
  1542. if(*p != '(') {
  1543. disp_error_message("need '('",p);
  1544. }
  1545. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1546. syntax.curly[syntax.curly_count].count = 1;
  1547. syntax.curly[syntax.curly_count].index = syntax.index++;
  1548. syntax.curly[syntax.curly_count].flag = 0;
  1549. // 条件判断開始のラベル形成する
  1550. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1551. l=add_str(label);
  1552. set_label(l,script_pos,p);
  1553. // 条件が偽なら終了地点に飛ばす
  1554. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1555. syntax.curly_count++;
  1556. add_scriptl(add_str("jump_zero"));
  1557. add_scriptc(C_ARG);
  1558. p=parse_expr(p);
  1559. p=skip_space(p);
  1560. add_scriptl(add_str(label));
  1561. add_scriptc(C_FUNC);
  1562. return p;
  1563. }
  1564. break;
  1565. }
  1566. return NULL;
  1567. }
  1568. const char* parse_syntax_close(const char *p) {
  1569. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1570. int flag;
  1571. do {
  1572. p = parse_syntax_close_sub(p,&flag);
  1573. } while(flag);
  1574. return p;
  1575. }
  1576. // if, for , while , do の閉じ判定
  1577. // flag == 1 : 閉じられた
  1578. // flag == 0 : 閉じられない
  1579. const char* parse_syntax_close_sub(const char* p,int* flag)
  1580. {
  1581. char label[256];
  1582. int pos = syntax.curly_count - 1;
  1583. int l;
  1584. *flag = 1;
  1585. if(syntax.curly_count <= 0) {
  1586. *flag = 0;
  1587. return p;
  1588. } else if(syntax.curly[pos].type == TYPE_IF) {
  1589. const char *bp = p;
  1590. const char *p2;
  1591. // if-block and else-block end is a new line
  1592. parse_nextline(false, p);
  1593. // if 最終場所へ飛ばす
  1594. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1595. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1596. parse_line(label);
  1597. syntax.curly_count--;
  1598. // 現在地のラベルを付ける
  1599. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1600. l=add_str(label);
  1601. set_label(l,script_pos,p);
  1602. syntax.curly[pos].count++;
  1603. p = skip_space(p);
  1604. p2 = skip_word(p);
  1605. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1606. // else or else - if
  1607. p = skip_space(p2);
  1608. p2 = skip_word(p);
  1609. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1610. // else - if
  1611. p=skip_space(p2);
  1612. if(*p != '(') {
  1613. disp_error_message("need '('",p);
  1614. }
  1615. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1616. add_scriptl(add_str("jump_zero"));
  1617. add_scriptc(C_ARG);
  1618. p=parse_expr(p);
  1619. p=skip_space(p);
  1620. add_scriptl(add_str(label));
  1621. add_scriptc(C_FUNC);
  1622. *flag = 0;
  1623. return p;
  1624. } else {
  1625. // else
  1626. if(!syntax.curly[pos].flag) {
  1627. syntax.curly[pos].flag = 1;
  1628. *flag = 0;
  1629. return p;
  1630. }
  1631. }
  1632. }
  1633. // if 閉じ
  1634. syntax.curly_count--;
  1635. // 最終地のラベルを付ける
  1636. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1637. l=add_str(label);
  1638. set_label(l,script_pos,p);
  1639. if(syntax.curly[pos].flag == 1) {
  1640. // このifに対するelseじゃないのでポインタの位置は同じ
  1641. return bp;
  1642. }
  1643. return p;
  1644. } else if(syntax.curly[pos].type == TYPE_DO) {
  1645. int l;
  1646. char label[256];
  1647. const char *p2;
  1648. if(syntax.curly[pos].flag) {
  1649. // 現在地のラベル形成する(continue でここに来る)
  1650. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1651. l=add_str(label);
  1652. set_label(l,script_pos,p);
  1653. }
  1654. // 条件が偽なら終了地点に飛ばす
  1655. p = skip_space(p);
  1656. p2 = skip_word(p);
  1657. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1658. disp_error_message("parse_syntax: need 'while'",p);
  1659. p = skip_space(p2);
  1660. if(*p != '(') {
  1661. disp_error_message("need '('",p);
  1662. }
  1663. // do-block end is a new line
  1664. parse_nextline(false, p);
  1665. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1666. add_scriptl(add_str("jump_zero"));
  1667. add_scriptc(C_ARG);
  1668. p=parse_expr(p);
  1669. p=skip_space(p);
  1670. add_scriptl(add_str(label));
  1671. add_scriptc(C_FUNC);
  1672. // 開始地点に飛ばす
  1673. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1674. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1675. parse_line(label);
  1676. syntax.curly_count--;
  1677. // 条件終了地点のラベル形成する
  1678. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1679. l=add_str(label);
  1680. set_label(l,script_pos,p);
  1681. p = skip_space(p);
  1682. if(*p != ';') {
  1683. disp_error_message("parse_syntax: need ';'",p);
  1684. return p+1;
  1685. }
  1686. p++;
  1687. syntax.curly_count--;
  1688. return p;
  1689. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1690. // for-block end is a new line
  1691. parse_nextline(false, p);
  1692. // 次のループに飛ばす
  1693. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1694. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1695. parse_line(label);
  1696. syntax.curly_count--;
  1697. // for 終了のラベル付け
  1698. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1699. l=add_str(label);
  1700. set_label(l,script_pos,p);
  1701. syntax.curly_count--;
  1702. return p;
  1703. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1704. // while-block end is a new line
  1705. parse_nextline(false, p);
  1706. // while 条件判断へ飛ばす
  1707. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1708. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1709. parse_line(label);
  1710. syntax.curly_count--;
  1711. // while 終了のラベル付け
  1712. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1713. l=add_str(label);
  1714. set_label(l,script_pos,p);
  1715. syntax.curly_count--;
  1716. return p;
  1717. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1718. int pos = syntax.curly_count-1;
  1719. char label[256];
  1720. int l;
  1721. // 戻す
  1722. sprintf(label,"return;");
  1723. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1724. parse_line(label);
  1725. syntax.curly_count--;
  1726. // 現在地のラベルを付ける
  1727. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1728. l=add_str(label);
  1729. set_label(l,script_pos,p);
  1730. syntax.curly_count--;
  1731. return p;
  1732. } else {
  1733. *flag = 0;
  1734. return p;
  1735. }
  1736. }
  1737. /*==========================================
  1738. * 組み込み関数の追加
  1739. *------------------------------------------*/
  1740. static void add_buildin_func(void)
  1741. {
  1742. int i,n;
  1743. const char* p;
  1744. for( i = 0; buildin_func[i].func; i++ )
  1745. {
  1746. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1747. // 'v' - value (either string or int or reference)
  1748. // 's' - string
  1749. // 'i' - int
  1750. // 'r' - reference (of a variable)
  1751. // 'l' - label
  1752. // '?' - one optional parameter
  1753. // '*' - unknown number of optional parameters
  1754. p = buildin_func[i].arg;
  1755. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1756. while( *p == '?' ) ++p;
  1757. if( *p == '*' ) ++p;
  1758. if( *p != 0){
  1759. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1760. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1761. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1762. } else {
  1763. n = add_str(buildin_func[i].name);
  1764. str_data[n].type = C_FUNC;
  1765. str_data[n].val = i;
  1766. str_data[n].func = buildin_func[i].func;
  1767. }
  1768. }
  1769. }
  1770. /// Retrieves the value of a constant.
  1771. bool script_get_constant(const char* name, int* value)
  1772. {
  1773. int n = search_str(name);
  1774. if( n == -1 || str_data[n].type != C_INT )
  1775. {// not found or not a constant
  1776. return false;
  1777. }
  1778. value[0] = str_data[n].val;
  1779. return true;
  1780. }
  1781. /// Creates new constant or parameter with given value.
  1782. void script_set_constant(const char* name, int value, bool isparameter)
  1783. {
  1784. int n = add_str(name);
  1785. if( str_data[n].type == C_NOP )
  1786. {// new
  1787. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1788. str_data[n].val = value;
  1789. }
  1790. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1791. {// existing parameter or constant
  1792. 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);
  1793. }
  1794. else
  1795. {// existing name
  1796. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1797. }
  1798. }
  1799. /*==========================================
  1800. * 定数データベースの読み込み
  1801. *------------------------------------------*/
  1802. static void read_constdb(void)
  1803. {
  1804. FILE *fp;
  1805. char line[1024],name[1024],val[1024];
  1806. int type;
  1807. sprintf(line, "%s/const.txt", db_path);
  1808. fp=fopen(line, "r");
  1809. if(fp==NULL){
  1810. ShowError("can't read %s\n", line);
  1811. return ;
  1812. }
  1813. while(fgets(line, sizeof(line), fp))
  1814. {
  1815. if(line[0]=='/' && line[1]=='/')
  1816. continue;
  1817. type=0;
  1818. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1819. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1820. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  1821. }
  1822. }
  1823. fclose(fp);
  1824. }
  1825. /*==========================================
  1826. * エラー表示
  1827. *------------------------------------------*/
  1828. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1829. {
  1830. int i;
  1831. if( p == NULL || !p[0] ) return NULL;
  1832. if( line < 0 )
  1833. StringBuf_Printf(buf, "*% 5d : ", -line);
  1834. else
  1835. StringBuf_Printf(buf, " % 5d : ", line);
  1836. for(i=0;p[i] && p[i] != '\n';i++){
  1837. if(p + i != mark)
  1838. StringBuf_Printf(buf, "%c", p[i]);
  1839. else
  1840. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1841. }
  1842. StringBuf_AppendStr(buf, "\n");
  1843. return p+i+(p[i] == '\n' ? 1 : 0);
  1844. }
  1845. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1846. {
  1847. // エラーが発生した行を求める
  1848. int j;
  1849. int line = start_line;
  1850. const char *p;
  1851. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1852. StringBuf buf;
  1853. for(p=src;p && *p;line++){
  1854. const char *lineend=strchr(p,'\n');
  1855. if(lineend==NULL || error_pos<lineend){
  1856. break;
  1857. }
  1858. for( j = 0; j < 4; j++ ) {
  1859. linestart[j] = linestart[j+1];
  1860. }
  1861. linestart[4] = p;
  1862. p=lineend+1;
  1863. }
  1864. StringBuf_Init(&buf);
  1865. StringBuf_AppendStr(&buf, "\a\n");
  1866. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1867. StringBuf_Printf(&buf, " %s\n", error_msg);
  1868. for(j = 0; j < 5; j++ ) {
  1869. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1870. }
  1871. p = script_print_line(&buf, p, error_pos, -line);
  1872. for(j = 0; j < 5; j++) {
  1873. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1874. }
  1875. ShowError("%s", StringBuf_Value(&buf));
  1876. StringBuf_Destroy(&buf);
  1877. }
  1878. /*==========================================
  1879. * スクリプトの解析
  1880. *------------------------------------------*/
  1881. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1882. {
  1883. const char *p,*tmpp;
  1884. int i;
  1885. struct script_code* code = NULL;
  1886. static int first=1;
  1887. char end;
  1888. bool unresolved_names = false;
  1889. if( src == NULL )
  1890. return NULL;// empty script
  1891. memset(&syntax,0,sizeof(syntax));
  1892. if(first){
  1893. add_buildin_func();
  1894. read_constdb();
  1895. first=0;
  1896. }
  1897. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1898. script_pos=0;
  1899. script_size=SCRIPT_BLOCK_SIZE;
  1900. parse_nextline(true, NULL);
  1901. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1902. if( options&SCRIPT_USE_LABEL_DB )
  1903. db_clear(scriptlabel_db);
  1904. parse_options = options;
  1905. if( setjmp( error_jump ) != 0 ) {
  1906. //Restore program state when script has problems. [from jA]
  1907. int i;
  1908. const int size = ARRAYLENGTH(syntax.curly);
  1909. if( error_report )
  1910. script_error(src,file,line,error_msg,error_pos);
  1911. aFree( error_msg );
  1912. aFree( script_buf );
  1913. script_pos = 0;
  1914. script_size = 0;
  1915. script_buf = NULL;
  1916. for(i=LABEL_START;i<str_num;i++)
  1917. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1918. for(i=0; i<size; i++)
  1919. linkdb_final(&syntax.curly[i].case_label);
  1920. return NULL;
  1921. }
  1922. parse_syntax_for_flag=0;
  1923. p=src;
  1924. p=skip_space(p);
  1925. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1926. {// does not require brackets around the script
  1927. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1928. {// empty script and can return NULL
  1929. aFree( script_buf );
  1930. script_pos = 0;
  1931. script_size = 0;
  1932. script_buf = NULL;
  1933. return NULL;
  1934. }
  1935. end = '\0';
  1936. }
  1937. else
  1938. {// requires brackets around the script
  1939. if( *p != '{' )
  1940. disp_error_message("not found '{'",p);
  1941. p = skip_space(p+1);
  1942. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1943. {// empty script and can return NULL
  1944. aFree( script_buf );
  1945. script_pos = 0;
  1946. script_size = 0;
  1947. script_buf = NULL;
  1948. return NULL;
  1949. }
  1950. end = '}';
  1951. }
  1952. // clear references of labels, variables and internal functions
  1953. for(i=LABEL_START;i<str_num;i++){
  1954. if(
  1955. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1956. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1957. ){
  1958. str_data[i].type=C_NOP;
  1959. str_data[i].backpatch=-1;
  1960. str_data[i].label=-1;
  1961. }
  1962. }
  1963. while( syntax.curly_count != 0 || *p != end )
  1964. {
  1965. if( *p == '\0' )
  1966. disp_error_message("unexpected end of script",p);
  1967. // labelだけ特殊処理
  1968. tmpp=skip_space(skip_word(p));
  1969. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1970. i=add_word(p);
  1971. set_label(i,script_pos,p);
  1972. if( parse_options&SCRIPT_USE_LABEL_DB )
  1973. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  1974. p=tmpp+1;
  1975. p=skip_space(p);
  1976. continue;
  1977. }
  1978. // 他は全部一緒くた
  1979. p=parse_line(p);
  1980. p=skip_space(p);
  1981. parse_nextline(false, p);
  1982. }
  1983. add_scriptc(C_NOP);
  1984. // trim code to size
  1985. script_size = script_pos;
  1986. RECREATE(script_buf,unsigned char,script_pos);
  1987. // default unknown references to variables
  1988. for(i=LABEL_START;i<str_num;i++){
  1989. if(str_data[i].type==C_NOP){
  1990. int j,next;
  1991. str_data[i].type=C_NAME;
  1992. str_data[i].label=i;
  1993. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1994. next=GETVALUE(script_buf,j);
  1995. SETVALUE(script_buf,j,i);
  1996. j=next;
  1997. }
  1998. }
  1999. else if( str_data[i].type == C_USERFUNC )
  2000. {// 'function name;' without follow-up code
  2001. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2002. unresolved_names = true;
  2003. }
  2004. }
  2005. if( unresolved_names )
  2006. {
  2007. disp_error_message("parse_script: unresolved function references", p);
  2008. }
  2009. #ifdef DEBUG_DISP
  2010. for(i=0;i<script_pos;i++){
  2011. if((i&15)==0) ShowMessage("%04x : ",i);
  2012. ShowMessage("%02x ",script_buf[i]);
  2013. if((i&15)==15) ShowMessage("\n");
  2014. }
  2015. ShowMessage("\n");
  2016. #endif
  2017. #ifdef DEBUG_DISASM
  2018. {
  2019. int i = 0,j;
  2020. while(i < script_pos) {
  2021. c_op op = get_com(script_buf,&i);
  2022. ShowMessage("%06x %s", i, script_op2name(op));
  2023. j = i;
  2024. switch(op) {
  2025. case C_INT:
  2026. ShowMessage(" %d", get_num(script_buf,&i));
  2027. break;
  2028. case C_POS:
  2029. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2030. i += 3;
  2031. break;
  2032. case C_NAME:
  2033. j = (*(int*)(script_buf+i)&0xffffff);
  2034. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2035. i += 3;
  2036. break;
  2037. case C_STR:
  2038. j = strlen(script_buf + i);
  2039. ShowMessage(" %s", script_buf + i);
  2040. i += j+1;
  2041. break;
  2042. }
  2043. ShowMessage(CL_CLL"\n");
  2044. }
  2045. }
  2046. #endif
  2047. CREATE(code,struct script_code,1);
  2048. code->script_buf = script_buf;
  2049. code->script_size = script_size;
  2050. code->script_vars = NULL;
  2051. return code;
  2052. }
  2053. /// Returns the player attached to this script, identified by the rid.
  2054. /// If there is no player attached, the script is terminated.
  2055. TBL_PC *script_rid2sd(struct script_state *st)
  2056. {
  2057. TBL_PC *sd=map_id2sd(st->rid);
  2058. if(!sd){
  2059. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2060. script_reportfunc(st);
  2061. script_reportsrc(st);
  2062. st->state = END;
  2063. }
  2064. return sd;
  2065. }
  2066. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2067. ///
  2068. /// @param st Script state
  2069. /// @param data Variable/constant
  2070. void get_val(struct script_state* st, struct script_data* data)
  2071. {
  2072. const char* name;
  2073. char prefix;
  2074. char postfix;
  2075. TBL_PC* sd = NULL;
  2076. if( !data_isreference(data) )
  2077. return;// not a variable/constant
  2078. name = reference_getname(data);
  2079. prefix = name[0];
  2080. postfix = name[strlen(name) - 1];
  2081. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2082. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2083. {
  2084. sd = script_rid2sd(st);
  2085. if( sd == NULL )
  2086. {// needs player attached
  2087. if( postfix == '$' )
  2088. {// string variable
  2089. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2090. data->type = C_CONSTSTR;
  2091. data->u.str = "";
  2092. }
  2093. else
  2094. {// integer variable
  2095. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2096. data->type = C_INT;
  2097. data->u.num = 0;
  2098. }
  2099. return;
  2100. }
  2101. }
  2102. if( postfix == '$' )
  2103. {// string variable
  2104. switch( prefix )
  2105. {
  2106. case '@':
  2107. data->u.str = pc_readregstr(sd, data->u.num);
  2108. break;
  2109. case '$':
  2110. data->u.str = mapreg_readregstr(data->u.num);
  2111. break;
  2112. case '#':
  2113. if( name[1] == '#' )
  2114. data->u.str = pc_readaccountreg2str(sd, name);// global
  2115. else
  2116. data->u.str = pc_readaccountregstr(sd, name);// local
  2117. break;
  2118. case '.':
  2119. {
  2120. struct linkdb_node** n =
  2121. data->ref ? data->ref:
  2122. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2123. &st->script->script_vars;// npc variable
  2124. data->u.str = (char*)linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data)));
  2125. }
  2126. break;
  2127. case '\'':
  2128. {
  2129. struct linkdb_node** n = NULL;
  2130. if (st->instance_id) {
  2131. n = &instance[st->instance_id].svar;
  2132. } else {
  2133. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2134. }
  2135. data->u.str = (char*)linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data)));
  2136. }
  2137. break;
  2138. default:
  2139. data->u.str = pc_readglobalreg_str(sd, name);
  2140. break;
  2141. }
  2142. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2143. {// empty string
  2144. data->type = C_CONSTSTR;
  2145. data->u.str = "";
  2146. }
  2147. else
  2148. {// duplicate string
  2149. data->type = C_STR;
  2150. data->u.str = aStrdup(data->u.str);
  2151. }
  2152. }
  2153. else
  2154. {// integer variable
  2155. data->type = C_INT;
  2156. if( reference_toconstant(data) )
  2157. {
  2158. data->u.num = reference_getconstant(data);
  2159. }
  2160. else if( reference_toparam(data) )
  2161. {
  2162. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2163. }
  2164. else
  2165. switch( prefix )
  2166. {
  2167. case '@':
  2168. data->u.num = pc_readreg(sd, data->u.num);
  2169. break;
  2170. case '$':
  2171. data->u.num = mapreg_readreg(data->u.num);
  2172. break;
  2173. case '#':
  2174. if( name[1] == '#' )
  2175. data->u.num = pc_readaccountreg2(sd, name);// global
  2176. else
  2177. data->u.num = pc_readaccountreg(sd, name);// local
  2178. break;
  2179. case '.':
  2180. {
  2181. struct linkdb_node** n =
  2182. data->ref ? data->ref:
  2183. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2184. &st->script->script_vars;// npc variable
  2185. data->u.num = (int)__64BPRTSIZE(linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data))));
  2186. }
  2187. break;
  2188. case '\'':
  2189. {
  2190. struct linkdb_node** n = NULL;
  2191. if( st->instance_id )
  2192. n = &instance[st->instance_id].ivar;
  2193. data->u.num = (int)__64BPRTSIZE(linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data))));
  2194. }
  2195. break;
  2196. default:
  2197. data->u.num = pc_readglobalreg(sd, name);
  2198. break;
  2199. }
  2200. }
  2201. return;
  2202. }
  2203. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2204. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2205. /// The value is left in the top of the stack and needs to be removed manually.
  2206. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2207. {
  2208. struct script_data* data;
  2209. push_val2(st->stack, C_NAME, uid, ref);
  2210. data = script_getdatatop(st, -1);
  2211. get_val(st, data);
  2212. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2213. }
  2214. /*==========================================
  2215. * Stores the value of a script variable
  2216. * Return value is 0 on fail, 1 on success.
  2217. *------------------------------------------*/
  2218. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2219. {
  2220. char prefix = name[0];
  2221. if( is_string_variable(name) )
  2222. {// string variable
  2223. const char* str = (const char*)value;
  2224. switch (prefix) {
  2225. case '@':
  2226. return pc_setregstr(sd, num, str);
  2227. case '$':
  2228. return mapreg_setregstr(num, str);
  2229. case '#':
  2230. return (name[1] == '#') ?
  2231. pc_setaccountreg2str(sd, name, str) :
  2232. pc_setaccountregstr(sd, name, str);
  2233. case '.': {
  2234. char* p;
  2235. struct linkdb_node** n;
  2236. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2237. p = (char*)linkdb_erase(n, (void*)__64BPRTSIZE(num));
  2238. if (p) aFree(p);
  2239. if (str[0]) linkdb_insert(n, (void*)__64BPRTSIZE(num), aStrdup(str));
  2240. }
  2241. return 1;
  2242. case '\'': {
  2243. char *p;
  2244. struct linkdb_node** n = NULL;
  2245. if( st->instance_id )
  2246. n = &instance[st->instance_id].svar;
  2247. p = (char*)linkdb_erase(n, (void*)__64BPRTSIZE(num));
  2248. if (p) aFree(p);
  2249. if( str[0] ) linkdb_insert(n, (void*)__64BPRTSIZE(num), aStrdup(str));
  2250. }
  2251. return 1;
  2252. default:
  2253. return pc_setglobalreg_str(sd, name, str);
  2254. }
  2255. }
  2256. else
  2257. {// integer variable
  2258. int val = (int)__64BPRTSIZE(value);
  2259. if(str_data[num&0x00ffffff].type == C_PARAM)
  2260. {
  2261. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2262. {
  2263. if( st != NULL )
  2264. {
  2265. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2266. script_reportsrc(st);
  2267. st->state = END;
  2268. }
  2269. return 0;
  2270. }
  2271. return 1;
  2272. }
  2273. switch (prefix) {
  2274. case '@':
  2275. return pc_setreg(sd, num, val);
  2276. case '$':
  2277. return mapreg_setreg(num, val);
  2278. case '#':
  2279. return (name[1] == '#') ?
  2280. pc_setaccountreg2(sd, name, val) :
  2281. pc_setaccountreg(sd, name, val);
  2282. case '.': {
  2283. struct linkdb_node** n;
  2284. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2285. if (val == 0)
  2286. linkdb_erase(n, (void*)__64BPRTSIZE(num));
  2287. else
  2288. linkdb_replace(n, (void*)__64BPRTSIZE(num), (void*)__64BPRTSIZE(val));
  2289. }
  2290. return 1;
  2291. case '\'':
  2292. {
  2293. struct linkdb_node** n = NULL;
  2294. if( st->instance_id )
  2295. n = &instance[st->instance_id].ivar;
  2296. if( val == 0 )
  2297. linkdb_erase(n, (void*)__64BPRTSIZE(num));
  2298. else
  2299. linkdb_replace(n, (void*)__64BPRTSIZE(num), (void*)__64BPRTSIZE(val));
  2300. return 1;
  2301. }
  2302. default:
  2303. return pc_setglobalreg(sd, name, val);
  2304. }
  2305. }
  2306. }
  2307. int set_var(TBL_PC* sd, char* name, void* val)
  2308. {
  2309. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2310. }
  2311. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2312. {
  2313. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2314. }
  2315. /// Converts the data to a string
  2316. const char* conv_str(struct script_state* st, struct script_data* data)
  2317. {
  2318. char* p;
  2319. get_val(st, data);
  2320. if( data_isstring(data) )
  2321. {// nothing to convert
  2322. }
  2323. else if( data_isint(data) )
  2324. {// int -> string
  2325. CREATE(p, char, ITEM_NAME_LENGTH);
  2326. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2327. p[ITEM_NAME_LENGTH-1] = '\0';
  2328. data->type = C_STR;
  2329. data->u.str = p;
  2330. }
  2331. else if( data_isreference(data) )
  2332. {// reference -> string
  2333. //##TODO when does this happen (check get_val) [FlavioJS]
  2334. data->type = C_CONSTSTR;
  2335. data->u.str = reference_getname(data);
  2336. }
  2337. else
  2338. {// unsupported data type
  2339. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2340. script_reportdata(data);
  2341. script_reportsrc(st);
  2342. data->type = C_CONSTSTR;
  2343. data->u.str = "";
  2344. }
  2345. return data->u.str;
  2346. }
  2347. /// Converts the data to an int
  2348. int conv_num(struct script_state* st, struct script_data* data)
  2349. {
  2350. char* p;
  2351. long num;
  2352. get_val(st, data);
  2353. if( data_isint(data) )
  2354. {// nothing to convert
  2355. }
  2356. else if( data_isstring(data) )
  2357. {// string -> int
  2358. // the result does not overflow or underflow, it is capped instead
  2359. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2360. p = data->u.str;
  2361. errno = 0;
  2362. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2363. if( errno == ERANGE
  2364. #if LONG_MAX > INT_MAX
  2365. || num < INT_MIN || num > INT_MAX
  2366. #endif
  2367. )
  2368. {
  2369. if( num <= INT_MIN )
  2370. {
  2371. num = INT_MIN;
  2372. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2373. }
  2374. else//if( num >= INT_MAX )
  2375. {
  2376. num = INT_MAX;
  2377. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2378. }
  2379. script_reportdata(data);
  2380. script_reportsrc(st);
  2381. }
  2382. if( data->type == C_STR )
  2383. aFree(p);
  2384. data->type = C_INT;
  2385. data->u.num = (int)num;
  2386. }
  2387. #if 0
  2388. // FIXME this function is being used to retrieve the position of labels and
  2389. // probably other stuff [FlavioJS]
  2390. else
  2391. {// unsupported data type
  2392. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2393. script_reportdata(data);
  2394. script_reportsrc(st);
  2395. data->type = C_INT;
  2396. data->u.num = 0;
  2397. }
  2398. #endif
  2399. return data->u.num;
  2400. }
  2401. //
  2402. // Stack operations
  2403. //
  2404. /// Increases the size of the stack
  2405. void stack_expand(struct script_stack* stack)
  2406. {
  2407. stack->sp_max += 64;
  2408. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2409. stack->sp_max * sizeof(stack->stack_data[0]) );
  2410. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2411. 64 * sizeof(stack->stack_data[0]) );
  2412. }
  2413. /// Pushes a value into the stack
  2414. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2415. /// Pushes a value into the stack (with reference)
  2416. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2417. {
  2418. if( stack->sp >= stack->sp_max )
  2419. stack_expand(stack);
  2420. stack->stack_data[stack->sp].type = type;
  2421. stack->stack_data[stack->sp].u.num = val;
  2422. stack->stack_data[stack->sp].ref = ref;
  2423. stack->sp++;
  2424. return &stack->stack_data[stack->sp-1];
  2425. }
  2426. /// Pushes a string into the stack
  2427. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2428. {
  2429. if( stack->sp >= stack->sp_max )
  2430. stack_expand(stack);
  2431. stack->stack_data[stack->sp].type = type;
  2432. stack->stack_data[stack->sp].u.str = str;
  2433. stack->stack_data[stack->sp].ref = NULL;
  2434. stack->sp++;
  2435. return &stack->stack_data[stack->sp-1];
  2436. }
  2437. /// Pushes a retinfo into the stack
  2438. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2439. {
  2440. if( stack->sp >= stack->sp_max )
  2441. stack_expand(stack);
  2442. stack->stack_data[stack->sp].type = C_RETINFO;
  2443. stack->stack_data[stack->sp].u.ri = ri;
  2444. stack->stack_data[stack->sp].ref = NULL;
  2445. stack->sp++;
  2446. return &stack->stack_data[stack->sp-1];
  2447. }
  2448. /// Pushes a copy of the target position into the stack
  2449. struct script_data* push_copy(struct script_stack* stack, int pos)
  2450. {
  2451. switch( stack->stack_data[pos].type )
  2452. {
  2453. case C_CONSTSTR:
  2454. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2455. break;
  2456. case C_STR:
  2457. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2458. break;
  2459. case C_RETINFO:
  2460. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2461. exit(1);
  2462. break;
  2463. default:
  2464. return push_val2(
  2465. stack,stack->stack_data[pos].type,
  2466. stack->stack_data[pos].u.num,
  2467. stack->stack_data[pos].ref
  2468. );
  2469. break;
  2470. }
  2471. }
  2472. /// Removes the values in indexes [start,end[ from the stack.
  2473. /// Adjusts all stack pointers.
  2474. void pop_stack(struct script_state* st, int start, int end)
  2475. {
  2476. struct script_stack* stack = st->stack;
  2477. struct script_data* data;
  2478. int i;
  2479. if( start < 0 )
  2480. start = 0;
  2481. if( end > stack->sp )
  2482. end = stack->sp;
  2483. if( start >= end )
  2484. return;// nothing to pop
  2485. // free stack elements
  2486. for( i = start; i < end; i++ )
  2487. {
  2488. data = &stack->stack_data[i];
  2489. if( data->type == C_STR )
  2490. aFree(data->u.str);
  2491. if( data->type == C_RETINFO )
  2492. {
  2493. struct script_retinfo* ri = data->u.ri;
  2494. if( ri->var_function )
  2495. {
  2496. script_free_vars(ri->var_function);
  2497. aFree(ri->var_function);
  2498. }
  2499. aFree(ri);
  2500. }
  2501. data->type = C_NOP;
  2502. }
  2503. // move the rest of the elements
  2504. if( stack->sp > end )
  2505. {
  2506. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2507. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2508. stack->stack_data[i].type = C_NOP;
  2509. }
  2510. // adjust stack pointers
  2511. if( st->start > end ) st->start -= end - start;
  2512. else if( st->start > start ) st->start = start;
  2513. if( st->end > end ) st->end -= end - start;
  2514. else if( st->end > start ) st->end = start;
  2515. if( stack->defsp > end ) stack->defsp -= end - start;
  2516. else if( stack->defsp > start ) stack->defsp = start;
  2517. stack->sp -= end - start;
  2518. }
  2519. ///
  2520. ///
  2521. ///
  2522. /*==========================================
  2523. * スクリプト依存変数、関数依存変数の解放
  2524. *------------------------------------------*/
  2525. void script_free_vars(struct linkdb_node **node)
  2526. {
  2527. struct linkdb_node* n = *node;
  2528. while( n != NULL)
  2529. {
  2530. const char* name = get_str((int)__64BPRTSIZE((n->key)&0x00ffffff));
  2531. if( is_string_variable(name) )
  2532. aFree(n->data); // 文字型変数なので、データ削除
  2533. n = n->next;
  2534. }
  2535. linkdb_final( node );
  2536. }
  2537. void script_free_code(struct script_code* code)
  2538. {
  2539. script_free_vars( &code->script_vars );
  2540. aFree( code->script_buf );
  2541. aFree( code );
  2542. }
  2543. /// Creates a new script state.
  2544. ///
  2545. /// @param script Script code
  2546. /// @param pos Position in the code
  2547. /// @param rid Who is running the script (attached player)
  2548. /// @param oid Where the code is being run (npc 'object')
  2549. /// @return Script state
  2550. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2551. {
  2552. struct script_state* st;
  2553. CREATE(st, struct script_state, 1);
  2554. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2555. st->stack->sp = 0;
  2556. st->stack->sp_max = 64;
  2557. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2558. st->stack->defsp = st->stack->sp;
  2559. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2560. st->state = RUN;
  2561. st->script = script;
  2562. //st->scriptroot = script;
  2563. st->pos = pos;
  2564. st->rid = rid;
  2565. st->oid = oid;
  2566. st->sleep.timer = INVALID_TIMER;
  2567. return st;
  2568. }
  2569. /// Frees a script state.
  2570. ///
  2571. /// @param st Script state
  2572. void script_free_state(struct script_state* st)
  2573. {
  2574. if(st->bk_st)
  2575. {// backup was not restored
  2576. 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);
  2577. }
  2578. if( st->sleep.timer != INVALID_TIMER )
  2579. delete_timer(st->sleep.timer, run_script_timer);
  2580. script_free_vars(st->stack->var_function);
  2581. aFree(st->stack->var_function);
  2582. pop_stack(st, 0, st->stack->sp);
  2583. aFree(st->stack->stack_data);
  2584. aFree(st->stack);
  2585. st->pos = -1;
  2586. aFree(st);
  2587. }
  2588. //
  2589. // 実行部main
  2590. //
  2591. /*==========================================
  2592. * コマンドの読み取り
  2593. *------------------------------------------*/
  2594. c_op get_com(unsigned char *script,int *pos)
  2595. {
  2596. int i = 0, j = 0;
  2597. if(script[*pos]>=0x80){
  2598. return C_INT;
  2599. }
  2600. while(script[*pos]>=0x40){
  2601. i=script[(*pos)++]<<j;
  2602. j+=6;
  2603. }
  2604. return (c_op)(i+(script[(*pos)++]<<j));
  2605. }
  2606. /*==========================================
  2607. * 数値の所得
  2608. *------------------------------------------*/
  2609. int get_num(unsigned char *script,int *pos)
  2610. {
  2611. int i,j;
  2612. i=0; j=0;
  2613. while(script[*pos]>=0xc0){
  2614. i+=(script[(*pos)++]&0x7f)<<j;
  2615. j+=6;
  2616. }
  2617. return i+((script[(*pos)++]&0x7f)<<j);
  2618. }
  2619. /*==========================================
  2620. * スタックから値を取り出す
  2621. *------------------------------------------*/
  2622. int pop_val(struct script_state* st)
  2623. {
  2624. if(st->stack->sp<=0)
  2625. return 0;
  2626. st->stack->sp--;
  2627. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2628. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2629. return st->stack->stack_data[st->stack->sp].u.num;
  2630. return 0;
  2631. }
  2632. /// Ternary operators
  2633. /// test ? if_true : if_false
  2634. void op_3(struct script_state* st, int op)
  2635. {
  2636. struct script_data* data;
  2637. int flag = 0;
  2638. data = script_getdatatop(st, -3);
  2639. get_val(st, data);
  2640. if( data_isstring(data) )
  2641. flag = data->u.str[0];// "" -> false
  2642. else if( data_isint(data) )
  2643. flag = data->u.num;// 0 -> false
  2644. else
  2645. {
  2646. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2647. script_reportdata(data);
  2648. script_reportsrc(st);
  2649. script_removetop(st, -3, 0);
  2650. script_pushnil(st);
  2651. return;
  2652. }
  2653. if( flag )
  2654. script_pushcopytop(st, -2);
  2655. else
  2656. script_pushcopytop(st, -1);
  2657. script_removetop(st, -4, -1);
  2658. }
  2659. /// Binary string operators
  2660. /// s1 EQ s2 -> i
  2661. /// s1 NE s2 -> i
  2662. /// s1 GT s2 -> i
  2663. /// s1 GE s2 -> i
  2664. /// s1 LT s2 -> i
  2665. /// s1 LE s2 -> i
  2666. /// s1 ADD s2 -> s
  2667. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2668. {
  2669. int a = 0;
  2670. switch(op){
  2671. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2672. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2673. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2674. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2675. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2676. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2677. case C_ADD:
  2678. {
  2679. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2680. strcpy(buf, s1);
  2681. strcat(buf, s2);
  2682. script_pushstr(st, buf);
  2683. return;
  2684. }
  2685. default:
  2686. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2687. script_reportsrc(st);
  2688. script_pushnil(st);
  2689. st->state = END;
  2690. return;
  2691. }
  2692. script_pushint(st,a);
  2693. }
  2694. /// Binary number operators
  2695. /// i OP i -> i
  2696. void op_2num(struct script_state* st, int op, int i1, int i2)
  2697. {
  2698. int ret;
  2699. double ret_double;
  2700. switch( op )
  2701. {
  2702. case C_AND: ret = i1 & i2; break;
  2703. case C_OR: ret = i1 | i2; break;
  2704. case C_XOR: ret = i1 ^ i2; break;
  2705. case C_LAND: ret = (i1 && i2); break;
  2706. case C_LOR: ret = (i1 || i2); break;
  2707. case C_EQ: ret = (i1 == i2); break;
  2708. case C_NE: ret = (i1 != i2); break;
  2709. case C_GT: ret = (i1 > i2); break;
  2710. case C_GE: ret = (i1 >= i2); break;
  2711. case C_LT: ret = (i1 < i2); break;
  2712. case C_LE: ret = (i1 <= i2); break;
  2713. case C_R_SHIFT: ret = i1>>i2; break;
  2714. case C_L_SHIFT: ret = i1<<i2; break;
  2715. case C_DIV:
  2716. case C_MOD:
  2717. if( i2 == 0 )
  2718. {
  2719. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2720. script_reportsrc(st);
  2721. script_pushnil(st);
  2722. st->state = END;
  2723. return;
  2724. }
  2725. else if( op == C_DIV )
  2726. ret = i1 / i2;
  2727. else//if( op == C_MOD )
  2728. ret = i1 % i2;
  2729. break;
  2730. default:
  2731. switch( op )
  2732. {// operators that can overflow/underflow
  2733. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2734. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2735. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2736. default:
  2737. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2738. script_reportsrc(st);
  2739. script_pushnil(st);
  2740. return;
  2741. }
  2742. if( ret_double < (double)INT_MIN )
  2743. {
  2744. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2745. script_reportsrc(st);
  2746. ret = INT_MIN;
  2747. }
  2748. else if( ret_double > (double)INT_MAX )
  2749. {
  2750. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2751. script_reportsrc(st);
  2752. ret = INT_MAX;
  2753. }
  2754. }
  2755. script_pushint(st, ret);
  2756. }
  2757. /// Binary operators
  2758. void op_2(struct script_state *st, int op)
  2759. {
  2760. struct script_data* left;
  2761. struct script_data* right;
  2762. left = script_getdatatop(st, -2);
  2763. right = script_getdatatop(st, -1);
  2764. get_val(st, left);
  2765. get_val(st, right);
  2766. // automatic conversions
  2767. switch( op )
  2768. {
  2769. case C_ADD:
  2770. if( data_isint(left) && data_isstring(right) )
  2771. {// convert int-string to string-string
  2772. conv_str(st, left);
  2773. }
  2774. else if( data_isstring(left) && data_isint(right) )
  2775. {// convert string-int to string-string
  2776. conv_str(st, right);
  2777. }
  2778. break;
  2779. }
  2780. if( data_isstring(left) && data_isstring(right) )
  2781. {// ss => op_2str
  2782. op_2str(st, op, left->u.str, right->u.str);
  2783. script_removetop(st, -3, -1);// pop the two values before the top one
  2784. }
  2785. else if( data_isint(left) && data_isint(right) )
  2786. {// ii => op_2num
  2787. int i1 = left->u.num;
  2788. int i2 = right->u.num;
  2789. script_removetop(st, -2, 0);
  2790. op_2num(st, op, i1, i2);
  2791. }
  2792. else
  2793. {// invalid argument
  2794. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2795. script_reportdata(left);
  2796. script_reportdata(right);
  2797. script_reportsrc(st);
  2798. script_removetop(st, -2, 0);
  2799. script_pushnil(st);
  2800. st->state = END;
  2801. }
  2802. }
  2803. /// Unary operators
  2804. /// NEG i -> i
  2805. /// NOT i -> i
  2806. /// LNOT i -> i
  2807. void op_1(struct script_state* st, int op)
  2808. {
  2809. struct script_data* data;
  2810. int i1;
  2811. data = script_getdatatop(st, -1);
  2812. get_val(st, data);
  2813. if( !data_isint(data) )
  2814. {// not a number
  2815. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2816. script_reportdata(data);
  2817. script_reportsrc(st);
  2818. script_pushnil(st);
  2819. st->state = END;
  2820. return;
  2821. }
  2822. i1 = data->u.num;
  2823. script_removetop(st, -1, 0);
  2824. switch( op )
  2825. {
  2826. case C_NEG: i1 = -i1; break;
  2827. case C_NOT: i1 = ~i1; break;
  2828. case C_LNOT: i1 = !i1; break;
  2829. default:
  2830. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2831. script_reportsrc(st);
  2832. script_pushnil(st);
  2833. st->state = END;
  2834. return;
  2835. }
  2836. script_pushint(st, i1);
  2837. }
  2838. /// Checks the type of all arguments passed to a built-in function.
  2839. ///
  2840. /// @param st Script state whose stack arguments should be inspected.
  2841. /// @param func Built-in function for which the arguments are intended.
  2842. static void script_check_buildin_argtype(struct script_state* st, int func)
  2843. {
  2844. char type;
  2845. int idx, invalid = 0;
  2846. script_function* sf = &buildin_func[str_data[func].val];
  2847. for( idx = 2; script_hasdata(st, idx); idx++ )
  2848. {
  2849. struct script_data* data = script_getdata(st, idx);
  2850. type = sf->arg[idx-2];
  2851. if( type == '?' || type == '*' )
  2852. {// optional argument or unknown number of optional parameters ( no types are after this )
  2853. break;
  2854. }
  2855. else if( type == 0 )
  2856. {// more arguments than necessary ( should not happen, as it is checked before )
  2857. ShowWarning("Found more arguments than necessary.\n");
  2858. invalid++;
  2859. break;
  2860. }
  2861. else
  2862. {
  2863. const char* name = NULL;
  2864. if( data_isreference(data) )
  2865. {// get name for variables to determine the type they refer to
  2866. name = reference_getname(data);
  2867. }
  2868. switch( type )
  2869. {
  2870. case 'v':
  2871. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  2872. {// variant
  2873. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  2874. script_reportdata(data);
  2875. invalid++;
  2876. }
  2877. break;
  2878. case 's':
  2879. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  2880. {// string
  2881. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  2882. script_reportdata(data);
  2883. invalid++;
  2884. }
  2885. break;
  2886. case 'i':
  2887. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  2888. {// int ( params and constants are always int )
  2889. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  2890. script_reportdata(data);
  2891. invalid++;
  2892. }
  2893. break;
  2894. case 'r':
  2895. if( !data_isreference(data) )
  2896. {// variables
  2897. ShowWarning("Unexpected type for argument %d. Expected variable.\n", idx-1);
  2898. script_reportdata(data);
  2899. invalid++;
  2900. }
  2901. break;
  2902. case 'l':
  2903. if( !data_islabel(data) && !data_isfunclabel(data) )
  2904. {// label
  2905. ShowWarning("Unexpected type for argument %d. Expected label.\n", idx-1);
  2906. script_reportdata(data);
  2907. invalid++;
  2908. }
  2909. break;
  2910. }
  2911. }
  2912. }
  2913. if(invalid)
  2914. {
  2915. ShowDebug("Function: %s\n", get_str(func));
  2916. script_reportsrc(st);
  2917. }
  2918. }
  2919. /// Executes a buildin command.
  2920. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2921. int run_func(struct script_state *st)
  2922. {
  2923. struct script_data* data;
  2924. int i,start_sp,end_sp,func;
  2925. end_sp = st->stack->sp;// position after the last argument
  2926. for( i = end_sp-1; i > 0 ; --i )
  2927. if( st->stack->stack_data[i].type == C_ARG )
  2928. break;
  2929. if( i == 0 )
  2930. {
  2931. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2932. st->state = END;
  2933. script_reportsrc(st);
  2934. return 1;
  2935. }
  2936. start_sp = i-1;// C_NAME of the command
  2937. st->start = start_sp;
  2938. st->end = end_sp;
  2939. data = &st->stack->stack_data[st->start];
  2940. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2941. func = data->u.num;
  2942. else
  2943. {
  2944. ShowError("script:run_func: not a buildin command.\n");
  2945. script_reportdata(data);
  2946. script_reportsrc(st);
  2947. st->state = END;
  2948. return 1;
  2949. }
  2950. if( script_config.warn_func_mismatch_argtypes )
  2951. {
  2952. script_check_buildin_argtype(st, func);
  2953. }
  2954. if(str_data[func].func){
  2955. if (str_data[func].func(st)) //Report error
  2956. script_reportsrc(st);
  2957. } else {
  2958. 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));
  2959. script_reportsrc(st);
  2960. st->state = END;
  2961. }
  2962. // Stack's datum are used when re-running functions [Eoe]
  2963. if( st->state == RERUNLINE )
  2964. return 0;
  2965. pop_stack(st, st->start, st->end);
  2966. if( st->state == RETFUNC )
  2967. {// return from a user-defined function
  2968. struct script_retinfo* ri;
  2969. int olddefsp = st->stack->defsp;
  2970. int nargs;
  2971. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2972. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2973. {
  2974. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  2975. script_reportsrc(st);
  2976. st->state = END;
  2977. return 1;
  2978. }
  2979. script_free_vars( st->stack->var_function );
  2980. aFree(st->stack->var_function);
  2981. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  2982. nargs = ri->nargs;
  2983. st->pos = ri->pos;
  2984. st->script = ri->script;
  2985. st->stack->var_function = ri->var_function;
  2986. st->stack->defsp = ri->defsp;
  2987. memset(ri, 0, sizeof(struct script_retinfo));
  2988. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  2989. st->state = GOTO;
  2990. }
  2991. return 0;
  2992. }
  2993. /*==========================================
  2994. * script execution
  2995. *------------------------------------------*/
  2996. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2997. {
  2998. struct script_state *st;
  2999. if( rootscript == NULL || pos < 0 )
  3000. return;
  3001. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3002. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3003. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3004. st = script_alloc_state(rootscript, pos, rid, oid);
  3005. run_script_main(st);
  3006. }
  3007. void script_stop_sleeptimers(int id)
  3008. {
  3009. struct script_state* st;
  3010. for(;;)
  3011. {
  3012. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3013. if( st == NULL )
  3014. break; // no more sleep timers
  3015. script_free_state(st);
  3016. }
  3017. }
  3018. /*==========================================
  3019. * 指定ノードをsleep_dbから削除
  3020. *------------------------------------------*/
  3021. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3022. {
  3023. struct linkdb_node *retnode;
  3024. if( n == NULL)
  3025. return NULL;
  3026. if( n->prev == NULL )
  3027. sleep_db = n->next;
  3028. else
  3029. n->prev->next = n->next;
  3030. if( n->next )
  3031. n->next->prev = n->prev;
  3032. retnode = n->next;
  3033. aFree( n );
  3034. return retnode; // 次のノードを返す
  3035. }
  3036. /*==========================================
  3037. * sleep用タイマー関数
  3038. *------------------------------------------*/
  3039. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3040. {
  3041. struct script_state *st = (struct script_state *)data;
  3042. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3043. TBL_PC *sd = map_id2sd(st->rid);
  3044. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3045. { //Character mismatch. Cancel execution.
  3046. st->rid = 0;
  3047. st->state = END;
  3048. }
  3049. while( node && st->sleep.timer != INVALID_TIMER ) {
  3050. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3051. script_erase_sleepdb(node);
  3052. st->sleep.timer = INVALID_TIMER;
  3053. break;
  3054. }
  3055. node = node->next;
  3056. }
  3057. if(st->state != RERUNLINE)
  3058. st->sleep.tick = 0;
  3059. run_script_main(st);
  3060. return 0;
  3061. }
  3062. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3063. ///
  3064. /// @param st Script state to detach.
  3065. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3066. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3067. {
  3068. struct map_session_data* sd;
  3069. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3070. {
  3071. sd->st = st->bk_st;
  3072. sd->npc_id = st->bk_npcid;
  3073. /**
  3074. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3075. **/
  3076. #if SECURE_NPCTIMEOUT
  3077. /**
  3078. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3079. **/
  3080. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3081. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3082. sd->npc_idle_timer = INVALID_TIMER;
  3083. }
  3084. #endif
  3085. if(st->bk_st)
  3086. {
  3087. //Remove tag for removal.
  3088. st->bk_st = NULL;
  3089. st->bk_npcid = 0;
  3090. }
  3091. else if(dequeue_event)
  3092. {
  3093. npc_event_dequeue(sd);
  3094. }
  3095. }
  3096. else if(st->bk_st)
  3097. {// rid was set to 0, before detaching the script state
  3098. 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);
  3099. script_reportsrc(st->bk_st);
  3100. script_free_state(st->bk_st);
  3101. st->bk_st = NULL;
  3102. }
  3103. }
  3104. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3105. ///
  3106. /// @param st Script state to attach.
  3107. static void script_attach_state(struct script_state* st)
  3108. {
  3109. struct map_session_data* sd;
  3110. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3111. {
  3112. if(st!=sd->st)
  3113. {
  3114. if(st->bk_st)
  3115. {// there is already a backup
  3116. 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);
  3117. }
  3118. st->bk_st = sd->st;
  3119. st->bk_npcid = sd->npc_id;
  3120. }
  3121. sd->st = st;
  3122. sd->npc_id = st->oid;
  3123. /**
  3124. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3125. **/
  3126. #if SECURE_NPCTIMEOUT
  3127. if( sd->npc_idle_timer == INVALID_TIMER )
  3128. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3129. sd->npc_idle_tick = gettick();
  3130. #endif
  3131. }
  3132. }
  3133. /*==========================================
  3134. * スクリプトの実行メイン部分
  3135. *------------------------------------------*/
  3136. void run_script_main(struct script_state *st)
  3137. {
  3138. int cmdcount=script_config.check_cmdcount;
  3139. int gotocount=script_config.check_gotocount;
  3140. TBL_PC *sd;
  3141. struct script_stack *stack=st->stack;
  3142. struct npc_data *nd;
  3143. script_attach_state(st);
  3144. nd = map_id2nd(st->oid);
  3145. if( nd && map[nd->bl.m].instance_id > 0 )
  3146. st->instance_id = map[nd->bl.m].instance_id;
  3147. if(st->state == RERUNLINE) {
  3148. run_func(st);
  3149. if(st->state == GOTO)
  3150. st->state = RUN;
  3151. } else if(st->state != END)
  3152. st->state = RUN;
  3153. while(st->state == RUN)
  3154. {
  3155. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3156. switch(c){
  3157. case C_EOL:
  3158. if( stack->defsp > stack->sp )
  3159. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3160. else
  3161. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3162. break;
  3163. case C_INT:
  3164. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3165. break;
  3166. case C_POS:
  3167. case C_NAME:
  3168. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3169. st->pos+=3;
  3170. break;
  3171. case C_ARG:
  3172. push_val(stack,c,0);
  3173. break;
  3174. case C_STR:
  3175. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3176. while(st->script->script_buf[st->pos++]);
  3177. break;
  3178. case C_FUNC:
  3179. run_func(st);
  3180. if(st->state==GOTO){
  3181. st->state = RUN;
  3182. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3183. ShowError("run_script: infinity loop !\n");
  3184. script_reportsrc(st);
  3185. st->state=END;
  3186. }
  3187. }
  3188. break;
  3189. case C_NEG:
  3190. case C_NOT:
  3191. case C_LNOT:
  3192. op_1(st ,c);
  3193. break;
  3194. case C_ADD:
  3195. case C_SUB:
  3196. case C_MUL:
  3197. case C_DIV:
  3198. case C_MOD:
  3199. case C_EQ:
  3200. case C_NE:
  3201. case C_GT:
  3202. case C_GE:
  3203. case C_LT:
  3204. case C_LE:
  3205. case C_AND:
  3206. case C_OR:
  3207. case C_XOR:
  3208. case C_LAND:
  3209. case C_LOR:
  3210. case C_R_SHIFT:
  3211. case C_L_SHIFT:
  3212. op_2(st, c);
  3213. break;
  3214. case C_OP3:
  3215. op_3(st, c);
  3216. break;
  3217. case C_NOP:
  3218. st->state=END;
  3219. break;
  3220. default:
  3221. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3222. st->state=END;
  3223. break;
  3224. }
  3225. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3226. ShowError("run_script: infinity loop !\n");
  3227. script_reportsrc(st);
  3228. st->state=END;
  3229. }
  3230. }
  3231. if(st->sleep.tick > 0) {
  3232. //Restore previous script
  3233. script_detach_state(st, false);
  3234. //Delay execution
  3235. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3236. st->sleep.charid = sd?sd->status.char_id:0;
  3237. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3238. run_script_timer, st->sleep.charid, (intptr_t)st);
  3239. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3240. }
  3241. else if(st->state != END && st->rid){
  3242. //Resume later (st is already attached to player).
  3243. if(st->bk_st) {
  3244. ShowWarning("Unable to restore stack! Double continuation!\n");
  3245. //Report BOTH scripts to see if that can help somehow.
  3246. ShowDebug("Previous script (lost):\n");
  3247. script_reportsrc(st->bk_st);
  3248. ShowDebug("Current script:\n");
  3249. script_reportsrc(st);
  3250. script_free_state(st->bk_st);
  3251. st->bk_st = NULL;
  3252. }
  3253. } else {
  3254. //Dispose of script.
  3255. if ((sd = map_id2sd(st->rid))!=NULL)
  3256. { //Restore previous stack and save char.
  3257. if(sd->state.using_fake_npc){
  3258. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3259. sd->state.using_fake_npc = 0;
  3260. }
  3261. //Restore previous script if any.
  3262. script_detach_state(st, true);
  3263. if (sd->state.reg_dirty&2)
  3264. intif_saveregistry(sd,2);
  3265. if (sd->state.reg_dirty&1)
  3266. intif_saveregistry(sd,1);
  3267. }
  3268. script_free_state(st);
  3269. st = NULL;
  3270. }
  3271. }
  3272. int script_config_read(char *cfgName)
  3273. {
  3274. int i;
  3275. char line[1024],w1[1024],w2[1024];
  3276. FILE *fp;
  3277. fp=fopen(cfgName,"r");
  3278. if(fp==NULL){
  3279. ShowError("file not found: [%s]\n", cfgName);
  3280. return 1;
  3281. }
  3282. while(fgets(line, sizeof(line), fp))
  3283. {
  3284. if(line[0] == '/' && line[1] == '/')
  3285. continue;
  3286. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3287. if(i!=2)
  3288. continue;
  3289. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3290. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3291. }
  3292. else if(strcmpi(w1,"check_cmdcount")==0) {
  3293. script_config.check_cmdcount = config_switch(w2);
  3294. }
  3295. else if(strcmpi(w1,"check_gotocount")==0) {
  3296. script_config.check_gotocount = config_switch(w2);
  3297. }
  3298. else if(strcmpi(w1,"input_min_value")==0) {
  3299. script_config.input_min_value = config_switch(w2);
  3300. }
  3301. else if(strcmpi(w1,"input_max_value")==0) {
  3302. script_config.input_max_value = config_switch(w2);
  3303. }
  3304. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3305. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3306. }
  3307. else if(strcmpi(w1,"import")==0){
  3308. script_config_read(w2);
  3309. }
  3310. }
  3311. fclose(fp);
  3312. return 0;
  3313. }
  3314. /**
  3315. * @see DBApply
  3316. */
  3317. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3318. {
  3319. struct script_code *code = db_data2ptr(data);
  3320. if (code)
  3321. script_free_code(code);
  3322. return 0;
  3323. }
  3324. void script_run_autobonus(const char *autobonus, int id, int pos)
  3325. {
  3326. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3327. if( script )
  3328. {
  3329. current_equip_item_index = pos;
  3330. run_script(script,0,id,0);
  3331. }
  3332. }
  3333. void script_add_autobonus(const char *autobonus)
  3334. {
  3335. if( strdb_get(autobonus_db, autobonus) == NULL )
  3336. {
  3337. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3338. if( script )
  3339. strdb_put(autobonus_db, autobonus, script);
  3340. }
  3341. }
  3342. /// resets a temporary character array variable to given value
  3343. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3344. {
  3345. int key;
  3346. uint8 idx;
  3347. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3348. {
  3349. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3350. return;
  3351. }
  3352. key = add_str(varname);
  3353. if( is_string_variable(varname) )
  3354. {
  3355. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3356. {
  3357. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3358. }
  3359. }
  3360. else
  3361. {
  3362. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3363. {
  3364. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3365. }
  3366. }
  3367. }
  3368. /// sets a temporary character array variable element idx to given value
  3369. /// @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.
  3370. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3371. {
  3372. int key;
  3373. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3374. {
  3375. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3376. return;
  3377. }
  3378. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3379. {
  3380. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3381. return;
  3382. }
  3383. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3384. if( is_string_variable(varname) )
  3385. {
  3386. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3387. }
  3388. else
  3389. {
  3390. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3391. }
  3392. if( refcache )
  3393. {// save to avoid repeated add_str calls
  3394. refcache[0] = key;
  3395. }
  3396. }
  3397. /*==========================================
  3398. * 終了
  3399. *------------------------------------------*/
  3400. int do_final_script()
  3401. {
  3402. #ifdef DEBUG_HASH
  3403. if (battle_config.etc_log)
  3404. {
  3405. FILE *fp = fopen("hash_dump.txt","wt");
  3406. if(fp) {
  3407. int i,count[SCRIPT_HASH_SIZE];
  3408. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3409. int n=0;
  3410. int min=INT_MAX,max=0,zero=0;
  3411. double mean=0.0f;
  3412. double median=0.0f;
  3413. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3414. memset(count, 0, sizeof(count));
  3415. fprintf(fp,"num : hash : data_name\n");
  3416. fprintf(fp,"---------------------------------------------------------------\n");
  3417. for(i=LABEL_START; i<str_num; i++) {
  3418. unsigned int h = calc_hash(get_str(i));
  3419. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3420. ++count[h];
  3421. }
  3422. fprintf(fp,"--------------------\n\n");
  3423. memset(count2, 0, sizeof(count2));
  3424. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3425. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3426. if(min > count[i])
  3427. min = count[i]; // minimun count of collision
  3428. if(max < count[i])
  3429. max = count[i]; // maximun count of collision
  3430. if(count[i] == 0)
  3431. zero++;
  3432. ++count2[count[i]];
  3433. }
  3434. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3435. for( i=min; i <= max; ++i ){
  3436. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3437. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3438. }
  3439. for( i=min; i <= max; ++i ){
  3440. n += count2[i];
  3441. if( n*2 >= SCRIPT_HASH_SIZE )
  3442. {
  3443. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3444. median = (i+i+1)/2.0f;
  3445. else
  3446. median = i;
  3447. break;
  3448. }
  3449. }
  3450. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3451. fclose(fp);
  3452. }
  3453. }
  3454. #endif
  3455. mapreg_final();
  3456. db_destroy(scriptlabel_db);
  3457. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3458. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3459. if(sleep_db) {
  3460. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3461. while(n) {
  3462. struct script_state *st = (struct script_state *)n->data;
  3463. script_free_state(st);
  3464. n = n->next;
  3465. }
  3466. linkdb_final(&sleep_db);
  3467. }
  3468. if (str_data)
  3469. aFree(str_data);
  3470. if (str_buf)
  3471. aFree(str_buf);
  3472. return 0;
  3473. }
  3474. /*==========================================
  3475. * 初期化
  3476. *------------------------------------------*/
  3477. int do_init_script()
  3478. {
  3479. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3480. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3481. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3482. mapreg_init();
  3483. return 0;
  3484. }
  3485. int script_reload()
  3486. {
  3487. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3488. db_clear(scriptlabel_db);
  3489. if(sleep_db) {
  3490. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3491. while(n) {
  3492. struct script_state *st = (struct script_state *)n->data;
  3493. script_free_state(st);
  3494. n = n->next;
  3495. }
  3496. linkdb_final(&sleep_db);
  3497. }
  3498. mapreg_reload();
  3499. return 0;
  3500. }
  3501. //-----------------------------------------------------------------------------
  3502. // buildin functions
  3503. //
  3504. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3505. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3506. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3507. /////////////////////////////////////////////////////////////////////
  3508. // NPC interaction
  3509. //
  3510. /// Appends a message to the npc dialog.
  3511. /// If a dialog doesn't exist yet, one is created.
  3512. ///
  3513. /// mes "<message>";
  3514. BUILDIN_FUNC(mes)
  3515. {
  3516. TBL_PC* sd = script_rid2sd(st);
  3517. if( sd == NULL )
  3518. return 0;
  3519. if( !script_hasdata(st, 3) )
  3520. {// only a single line detected in the script
  3521. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3522. }
  3523. else
  3524. {// parse multiple lines as they exist
  3525. int i;
  3526. for( i = 2; script_hasdata(st, i); i++ )
  3527. {
  3528. // send the message to the client
  3529. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3530. }
  3531. }
  3532. return 0;
  3533. }
  3534. /// Displays the button 'next' in the npc dialog.
  3535. /// The dialog text is cleared and the script continues when the button is pressed.
  3536. ///
  3537. /// next;
  3538. BUILDIN_FUNC(next)
  3539. {
  3540. TBL_PC* sd;
  3541. sd = script_rid2sd(st);
  3542. if( sd == NULL )
  3543. return 0;
  3544. st->state = STOP;
  3545. clif_scriptnext(sd, st->oid);
  3546. return 0;
  3547. }
  3548. /// Ends the script and displays the button 'close' on the npc dialog.
  3549. /// The dialog is closed when the button is pressed.
  3550. ///
  3551. /// close;
  3552. BUILDIN_FUNC(close)
  3553. {
  3554. TBL_PC* sd;
  3555. sd = script_rid2sd(st);
  3556. if( sd == NULL )
  3557. return 0;
  3558. st->state = END;
  3559. clif_scriptclose(sd, st->oid);
  3560. return 0;
  3561. }
  3562. /// Displays the button 'close' on the npc dialog.
  3563. /// The dialog is closed and the script continues when the button is pressed.
  3564. ///
  3565. /// close2;
  3566. BUILDIN_FUNC(close2)
  3567. {
  3568. TBL_PC* sd;
  3569. sd = script_rid2sd(st);
  3570. if( sd == NULL )
  3571. return 0;
  3572. st->state = STOP;
  3573. clif_scriptclose(sd, st->oid);
  3574. return 0;
  3575. }
  3576. /// Counts the number of valid and total number of options in 'str'
  3577. /// If max_count > 0 the counting stops when that valid option is reached
  3578. /// total is incremented for each option (NULL is supported)
  3579. static int menu_countoptions(const char* str, int max_count, int* total)
  3580. {
  3581. int count = 0;
  3582. int bogus_total;
  3583. if( total == NULL )
  3584. total = &bogus_total;
  3585. ++(*total);
  3586. // initial empty options
  3587. while( *str == ':' )
  3588. {
  3589. ++str;
  3590. ++(*total);
  3591. }
  3592. // count menu options
  3593. while( *str != '\0' )
  3594. {
  3595. ++count;
  3596. --max_count;
  3597. if( max_count == 0 )
  3598. break;
  3599. while( *str != ':' && *str != '\0' )
  3600. ++str;
  3601. while( *str == ':' )
  3602. {
  3603. ++str;
  3604. ++(*total);
  3605. }
  3606. }
  3607. return count;
  3608. }
  3609. /// Displays a menu with options and goes to the target label.
  3610. /// The script is stopped if cancel is pressed.
  3611. /// Options with no text are not displayed in the client.
  3612. ///
  3613. /// Options can be grouped together, separated by the character ':' in the text:
  3614. /// ex: menu "A:B:C",L_target;
  3615. /// All these options go to the specified target label.
  3616. ///
  3617. /// The index of the selected option is put in the variable @menu.
  3618. /// Indexes start with 1 and are consistent with grouped and empty options.
  3619. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3620. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3621. ///
  3622. /// NOTE: the client closes the npc dialog when cancel is pressed
  3623. ///
  3624. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3625. BUILDIN_FUNC(menu)
  3626. {
  3627. int i;
  3628. const char* text;
  3629. TBL_PC* sd;
  3630. sd = script_rid2sd(st);
  3631. if( sd == NULL )
  3632. return 0;
  3633. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3634. if( sd->state.menu_or_input == 0 )
  3635. {
  3636. struct StringBuf buf;
  3637. struct script_data* data;
  3638. if( script_lastdata(st) % 2 == 0 )
  3639. {// argument count is not even (1st argument is at index 2)
  3640. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3641. st->state = END;
  3642. return 1;
  3643. }
  3644. StringBuf_Init(&buf);
  3645. sd->npc_menu = 0;
  3646. for( i = 2; i < script_lastdata(st); i += 2 )
  3647. {
  3648. // menu options
  3649. text = script_getstr(st, i);
  3650. // target label
  3651. data = script_getdata(st, i+1);
  3652. if( !data_islabel(data) )
  3653. {// not a label
  3654. StringBuf_Destroy(&buf);
  3655. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3656. script_reportdata(data);
  3657. st->state = END;
  3658. return 1;
  3659. }
  3660. // append option(s)
  3661. if( text[0] == '\0' )
  3662. continue;// empty string, ignore
  3663. if( sd->npc_menu > 0 )
  3664. StringBuf_AppendStr(&buf, ":");
  3665. StringBuf_AppendStr(&buf, text);
  3666. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3667. }
  3668. st->state = RERUNLINE;
  3669. sd->state.menu_or_input = 1;
  3670. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3671. StringBuf_Destroy(&buf);
  3672. if( sd->npc_menu >= 0xff )
  3673. {// 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
  3674. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3675. script_reportsrc(st);
  3676. }
  3677. }
  3678. else if( sd->npc_menu == 0xff )
  3679. {// Cancel was pressed
  3680. sd->state.menu_or_input = 0;
  3681. st->state = END;
  3682. }
  3683. else
  3684. {// goto target label
  3685. int menu = 0;
  3686. sd->state.menu_or_input = 0;
  3687. if( sd->npc_menu <= 0 )
  3688. {
  3689. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3690. st->state = END;
  3691. return 1;
  3692. }
  3693. // get target label
  3694. for( i = 2; i < script_lastdata(st); i += 2 )
  3695. {
  3696. text = script_getstr(st, i);
  3697. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3698. if( sd->npc_menu <= 0 )
  3699. break;// entry found
  3700. }
  3701. if( sd->npc_menu > 0 )
  3702. {// Invalid selection
  3703. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3704. st->state = END;
  3705. return 1;
  3706. }
  3707. if( !data_islabel(script_getdata(st, i + 1)) )
  3708. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3709. ShowError("script:menu: unexpected data in label argument\n");
  3710. script_reportdata(script_getdata(st, i + 1));
  3711. st->state = END;
  3712. return 1;
  3713. }
  3714. pc_setreg(sd, add_str("@menu"), menu);
  3715. st->pos = script_getnum(st, i + 1);
  3716. st->state = GOTO;
  3717. }
  3718. return 0;
  3719. }
  3720. /// Displays a menu with options and returns the selected option.
  3721. /// Behaves like 'menu' without the target labels.
  3722. ///
  3723. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3724. ///
  3725. /// @see menu
  3726. BUILDIN_FUNC(select)
  3727. {
  3728. int i;
  3729. const char* text;
  3730. TBL_PC* sd;
  3731. sd = script_rid2sd(st);
  3732. if( sd == NULL )
  3733. return 0;
  3734. if( sd->state.menu_or_input == 0 )
  3735. {
  3736. struct StringBuf buf;
  3737. StringBuf_Init(&buf);
  3738. sd->npc_menu = 0;
  3739. for( i = 2; i <= script_lastdata(st); ++i )
  3740. {
  3741. text = script_getstr(st, i);
  3742. if( sd->npc_menu > 0 )
  3743. StringBuf_AppendStr(&buf, ":");
  3744. StringBuf_AppendStr(&buf, text);
  3745. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3746. }
  3747. st->state = RERUNLINE;
  3748. sd->state.menu_or_input = 1;
  3749. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3750. StringBuf_Destroy(&buf);
  3751. if( sd->npc_menu >= 0xff )
  3752. {
  3753. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3754. script_reportsrc(st);
  3755. }
  3756. }
  3757. else if( sd->npc_menu == 0xff )
  3758. {// Cancel was pressed
  3759. sd->state.menu_or_input = 0;
  3760. st->state = END;
  3761. }
  3762. else
  3763. {// return selected option
  3764. int menu = 0;
  3765. sd->state.menu_or_input = 0;
  3766. for( i = 2; i <= script_lastdata(st); ++i )
  3767. {
  3768. text = script_getstr(st, i);
  3769. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3770. if( sd->npc_menu <= 0 )
  3771. break;// entry found
  3772. }
  3773. pc_setreg(sd, add_str("@menu"), menu);
  3774. script_pushint(st, menu);
  3775. st->state = RUN;
  3776. }
  3777. return 0;
  3778. }
  3779. /// Displays a menu with options and returns the selected option.
  3780. /// Behaves like 'menu' without the target labels, except when cancel is
  3781. /// pressed.
  3782. /// When cancel is pressed, the script continues and 255 is returned.
  3783. ///
  3784. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3785. ///
  3786. /// @see menu
  3787. BUILDIN_FUNC(prompt)
  3788. {
  3789. int i;
  3790. const char *text;
  3791. TBL_PC* sd;
  3792. sd = script_rid2sd(st);
  3793. if( sd == NULL )
  3794. return 0;
  3795. if( sd->state.menu_or_input == 0 )
  3796. {
  3797. struct StringBuf buf;
  3798. StringBuf_Init(&buf);
  3799. sd->npc_menu = 0;
  3800. for( i = 2; i <= script_lastdata(st); ++i )
  3801. {
  3802. text = script_getstr(st, i);
  3803. if( sd->npc_menu > 0 )
  3804. StringBuf_AppendStr(&buf, ":");
  3805. StringBuf_AppendStr(&buf, text);
  3806. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3807. }
  3808. st->state = RERUNLINE;
  3809. sd->state.menu_or_input = 1;
  3810. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3811. StringBuf_Destroy(&buf);
  3812. if( sd->npc_menu >= 0xff )
  3813. {
  3814. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3815. script_reportsrc(st);
  3816. }
  3817. }
  3818. else if( sd->npc_menu == 0xff )
  3819. {// Cancel was pressed
  3820. sd->state.menu_or_input = 0;
  3821. pc_setreg(sd, add_str("@menu"), 0xff);
  3822. script_pushint(st, 0xff);
  3823. st->state = RUN;
  3824. }
  3825. else
  3826. {// return selected option
  3827. int menu = 0;
  3828. sd->state.menu_or_input = 0;
  3829. for( i = 2; i <= script_lastdata(st); ++i )
  3830. {
  3831. text = script_getstr(st, i);
  3832. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3833. if( sd->npc_menu <= 0 )
  3834. break;// entry found
  3835. }
  3836. pc_setreg(sd, add_str("@menu"), menu);
  3837. script_pushint(st, menu);
  3838. st->state = RUN;
  3839. }
  3840. return 0;
  3841. }
  3842. /////////////////////////////////////////////////////////////////////
  3843. // ...
  3844. //
  3845. /// Jumps to the target script label.
  3846. ///
  3847. /// goto <label>;
  3848. BUILDIN_FUNC(goto)
  3849. {
  3850. if( !data_islabel(script_getdata(st,2)) )
  3851. {
  3852. ShowError("script:goto: not a label\n");
  3853. script_reportdata(script_getdata(st,2));
  3854. st->state = END;
  3855. return 1;
  3856. }
  3857. st->pos = script_getnum(st,2);
  3858. st->state = GOTO;
  3859. return 0;
  3860. }
  3861. /*==========================================
  3862. * user-defined function call
  3863. *------------------------------------------*/
  3864. BUILDIN_FUNC(callfunc)
  3865. {
  3866. int i, j;
  3867. struct script_retinfo* ri;
  3868. struct script_code* scr;
  3869. const char* str = script_getstr(st,2);
  3870. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3871. if( !scr )
  3872. {
  3873. ShowError("script:callfunc: function not found! [%s]\n", str);
  3874. st->state = END;
  3875. return 1;
  3876. }
  3877. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3878. {
  3879. struct script_data* data = push_copy(st->stack,i);
  3880. if( data_isreference(data) && !data->ref )
  3881. {
  3882. const char* name = reference_getname(data);
  3883. if( name[0] == '.' && name[1] == '@' )
  3884. data->ref = st->stack->var_function;
  3885. else if( name[0] == '.' )
  3886. data->ref = &st->script->script_vars;
  3887. }
  3888. }
  3889. CREATE(ri, struct script_retinfo, 1);
  3890. ri->script = st->script;// script code
  3891. ri->var_function = st->stack->var_function;// scope variables
  3892. ri->pos = st->pos;// script location
  3893. ri->nargs = j;// argument count
  3894. ri->defsp = st->stack->defsp;// default stack pointer
  3895. push_retinfo(st->stack, ri);
  3896. st->pos = 0;
  3897. st->script = scr;
  3898. st->stack->defsp = st->stack->sp;
  3899. st->state = GOTO;
  3900. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3901. return 0;
  3902. }
  3903. /*==========================================
  3904. * subroutine call
  3905. *------------------------------------------*/
  3906. BUILDIN_FUNC(callsub)
  3907. {
  3908. int i,j;
  3909. struct script_retinfo* ri;
  3910. int pos = script_getnum(st,2);
  3911. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3912. {
  3913. ShowError("script:callsub: argument is not a label\n");
  3914. script_reportdata(script_getdata(st,2));
  3915. st->state = END;
  3916. return 1;
  3917. }
  3918. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3919. {
  3920. struct script_data* data = push_copy(st->stack,i);
  3921. if( data_isreference(data) && !data->ref )
  3922. {
  3923. const char* name = reference_getname(data);
  3924. if( name[0] == '.' && name[1] == '@' )
  3925. data->ref = st->stack->var_function;
  3926. }
  3927. }
  3928. CREATE(ri, struct script_retinfo, 1);
  3929. ri->script = st->script;// script code
  3930. ri->var_function = st->stack->var_function;// scope variables
  3931. ri->pos = st->pos;// script location
  3932. ri->nargs = j;// argument count
  3933. ri->defsp = st->stack->defsp;// default stack pointer
  3934. push_retinfo(st->stack, ri);
  3935. st->pos = pos;
  3936. st->stack->defsp = st->stack->sp;
  3937. st->state = GOTO;
  3938. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3939. return 0;
  3940. }
  3941. /// Retrieves an argument provided to callfunc/callsub.
  3942. /// If the argument doesn't exist
  3943. ///
  3944. /// getarg(<index>{,<default_value>}) -> <value>
  3945. BUILDIN_FUNC(getarg)
  3946. {
  3947. struct script_retinfo* ri;
  3948. int idx;
  3949. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3950. {
  3951. ShowError("script:getarg: no callfunc or callsub!\n");
  3952. st->state = END;
  3953. return 1;
  3954. }
  3955. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3956. idx = script_getnum(st,2);
  3957. if( idx >= 0 && idx < ri->nargs )
  3958. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3959. else if( script_hasdata(st,3) )
  3960. script_pushcopy(st, 3);
  3961. else
  3962. {
  3963. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3964. st->state = END;
  3965. return 1;
  3966. }
  3967. return 0;
  3968. }
  3969. /// Returns from the current function, optionaly returning a value from the functions.
  3970. /// Don't use outside script functions.
  3971. ///
  3972. /// return;
  3973. /// return <value>;
  3974. BUILDIN_FUNC(return)
  3975. {
  3976. if( script_hasdata(st,2) )
  3977. {// return value
  3978. struct script_data* data;
  3979. script_pushcopy(st, 2);
  3980. data = script_getdatatop(st, -1);
  3981. if( data_isreference(data) )
  3982. {
  3983. const char* name = reference_getname(data);
  3984. if( name[0] == '.' && name[1] == '@' )
  3985. {// scope variable
  3986. if( !data->ref || data->ref == st->stack->var_function )
  3987. get_val(st, data);// current scope, convert to value
  3988. }
  3989. else if( name[0] == '.' && !data->ref )
  3990. {// script variable, link to current script
  3991. data->ref = &st->script->script_vars;
  3992. }
  3993. }
  3994. }
  3995. else
  3996. {// no return value
  3997. script_pushnil(st);
  3998. }
  3999. st->state = RETFUNC;
  4000. return 0;
  4001. }
  4002. /// Returns a random number from 0 to <range>-1.
  4003. /// Or returns a random number from <min> to <max>.
  4004. /// If <min> is greater than <max>, their numbers are switched.
  4005. /// rand(<range>) -> <int>
  4006. /// rand(<min>,<max>) -> <int>
  4007. BUILDIN_FUNC(rand)
  4008. {
  4009. int range;
  4010. int min;
  4011. int max;
  4012. if( script_hasdata(st,3) )
  4013. {// min,max
  4014. min = script_getnum(st,2);
  4015. max = script_getnum(st,3);
  4016. if( max < min )
  4017. swap(min, max);
  4018. range = max - min + 1;
  4019. }
  4020. else
  4021. {// range
  4022. min = 0;
  4023. range = script_getnum(st,2);
  4024. }
  4025. if( range <= 1 )
  4026. script_pushint(st, min);
  4027. else
  4028. script_pushint(st, rnd()%range + min);
  4029. return 0;
  4030. }
  4031. /*==========================================
  4032. *
  4033. *------------------------------------------*/
  4034. BUILDIN_FUNC(warp)
  4035. {
  4036. int ret;
  4037. int x,y;
  4038. const char* str;
  4039. TBL_PC* sd;
  4040. sd = script_rid2sd(st);
  4041. if( sd == NULL )
  4042. return 0;
  4043. str = script_getstr(st,2);
  4044. x = script_getnum(st,3);
  4045. y = script_getnum(st,4);
  4046. if(strcmp(str,"Random")==0)
  4047. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4048. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4049. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4050. else
  4051. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4052. if( ret ) {
  4053. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4054. script_reportsrc(st);
  4055. }
  4056. return 0;
  4057. }
  4058. /*==========================================
  4059. * エリア指定ワープ
  4060. *------------------------------------------*/
  4061. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4062. {
  4063. int x,y;
  4064. unsigned int map;
  4065. map=va_arg(ap, unsigned int);
  4066. x=va_arg(ap,int);
  4067. y=va_arg(ap,int);
  4068. if(map == 0)
  4069. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4070. else
  4071. pc_setpos((TBL_PC *)bl,map,x,y,CLR_OUTSIGHT);
  4072. return 0;
  4073. }
  4074. BUILDIN_FUNC(areawarp)
  4075. {
  4076. int x,y,m;
  4077. unsigned int index;
  4078. const char *str;
  4079. const char *mapname;
  4080. int x0,y0,x1,y1;
  4081. mapname=script_getstr(st,2);
  4082. x0=script_getnum(st,3);
  4083. y0=script_getnum(st,4);
  4084. x1=script_getnum(st,5);
  4085. y1=script_getnum(st,6);
  4086. str=script_getstr(st,7);
  4087. x=script_getnum(st,8);
  4088. y=script_getnum(st,9);
  4089. if( (m=map_mapname2mapid(mapname))< 0)
  4090. return 0;
  4091. if(strcmp(str,"Random")==0)
  4092. index = 0;
  4093. else if(!(index=mapindex_name2id(str)))
  4094. return 0;
  4095. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  4096. return 0;
  4097. }
  4098. /*==========================================
  4099. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4100. *------------------------------------------*/
  4101. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4102. {
  4103. int hp, sp;
  4104. hp = va_arg(ap, int);
  4105. sp = va_arg(ap, int);
  4106. pc_percentheal((TBL_PC *)bl,hp,sp);
  4107. return 0;
  4108. }
  4109. BUILDIN_FUNC(areapercentheal)
  4110. {
  4111. int hp,sp,m;
  4112. const char *mapname;
  4113. int x0,y0,x1,y1;
  4114. mapname=script_getstr(st,2);
  4115. x0=script_getnum(st,3);
  4116. y0=script_getnum(st,4);
  4117. x1=script_getnum(st,5);
  4118. y1=script_getnum(st,6);
  4119. hp=script_getnum(st,7);
  4120. sp=script_getnum(st,8);
  4121. if( (m=map_mapname2mapid(mapname))< 0)
  4122. return 0;
  4123. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4124. return 0;
  4125. }
  4126. /*==========================================
  4127. * warpchar [LuzZza]
  4128. * Useful for warp one player from
  4129. * another player npc-session.
  4130. * Using: warpchar "mapname",x,y,Char_ID;
  4131. *------------------------------------------*/
  4132. BUILDIN_FUNC(warpchar)
  4133. {
  4134. int x,y,a;
  4135. const char *str;
  4136. TBL_PC *sd;
  4137. str=script_getstr(st,2);
  4138. x=script_getnum(st,3);
  4139. y=script_getnum(st,4);
  4140. a=script_getnum(st,5);
  4141. sd = map_charid2sd(a);
  4142. if( sd == NULL )
  4143. return 0;
  4144. if(strcmp(str, "Random") == 0)
  4145. pc_randomwarp(sd, CLR_TELEPORT);
  4146. else
  4147. if(strcmp(str, "SavePoint") == 0)
  4148. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4149. else
  4150. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4151. return 0;
  4152. }
  4153. /*==========================================
  4154. * Warpparty - [Fredzilla] [Paradox924X]
  4155. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4156. * If 'from_mapname' is specified, only the party members on that map will be warped
  4157. *------------------------------------------*/
  4158. BUILDIN_FUNC(warpparty)
  4159. {
  4160. TBL_PC *sd = NULL;
  4161. TBL_PC *pl_sd;
  4162. struct party_data* p;
  4163. int type;
  4164. int mapindex;
  4165. int i;
  4166. const char* str = script_getstr(st,2);
  4167. int x = script_getnum(st,3);
  4168. int y = script_getnum(st,4);
  4169. int p_id = script_getnum(st,5);
  4170. const char* str2 = NULL;
  4171. if ( script_hasdata(st,6) )
  4172. str2 = script_getstr(st,6);
  4173. p = party_search(p_id);
  4174. if(!p)
  4175. return 0;
  4176. type = ( strcmp(str,"Random")==0 ) ? 0
  4177. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4178. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4179. : ( strcmp(str,"Leader")==0 ) ? 3
  4180. : 4;
  4181. switch (type)
  4182. {
  4183. case 3:
  4184. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4185. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4186. return 0;
  4187. pl_sd = p->data[i].sd;
  4188. mapindex = pl_sd->mapindex;
  4189. x = pl_sd->bl.x;
  4190. y = pl_sd->bl.y;
  4191. break;
  4192. case 4:
  4193. mapindex = mapindex_name2id(str);
  4194. break;
  4195. case 2:
  4196. //"SavePoint" uses save point of the currently attached player
  4197. if (( sd = script_rid2sd(st) ) == NULL )
  4198. return 0;
  4199. default:
  4200. mapindex = 0;
  4201. break;
  4202. }
  4203. for (i = 0; i < MAX_PARTY; i++)
  4204. {
  4205. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4206. continue;
  4207. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4208. continue;
  4209. if( pc_isdead(pl_sd) )
  4210. continue;
  4211. switch( type )
  4212. {
  4213. case 0: // Random
  4214. if(!map[pl_sd->bl.m].flag.nowarp)
  4215. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4216. break;
  4217. case 1: // SavePointAll
  4218. if(!map[pl_sd->bl.m].flag.noreturn)
  4219. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4220. break;
  4221. case 2: // SavePoint
  4222. if(!map[pl_sd->bl.m].flag.noreturn)
  4223. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4224. break;
  4225. case 3: // Leader
  4226. case 4: // m,x,y
  4227. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4228. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4229. break;
  4230. }
  4231. }
  4232. return 0;
  4233. }
  4234. /*==========================================
  4235. * Warpguild - [Fredzilla]
  4236. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4237. *------------------------------------------*/
  4238. BUILDIN_FUNC(warpguild)
  4239. {
  4240. TBL_PC *sd = NULL;
  4241. TBL_PC *pl_sd;
  4242. struct guild* g;
  4243. struct s_mapiterator* iter;
  4244. int type;
  4245. const char* str = script_getstr(st,2);
  4246. int x = script_getnum(st,3);
  4247. int y = script_getnum(st,4);
  4248. int gid = script_getnum(st,5);
  4249. g = guild_search(gid);
  4250. if( g == NULL )
  4251. return 0;
  4252. type = ( strcmp(str,"Random")==0 ) ? 0
  4253. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4254. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4255. : 3;
  4256. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4257. {// "SavePoint" uses save point of the currently attached player
  4258. return 0;
  4259. }
  4260. iter = mapit_getallusers();
  4261. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4262. {
  4263. if( pl_sd->status.guild_id != gid )
  4264. continue;
  4265. switch( type )
  4266. {
  4267. case 0: // Random
  4268. if(!map[pl_sd->bl.m].flag.nowarp)
  4269. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4270. break;
  4271. case 1: // SavePointAll
  4272. if(!map[pl_sd->bl.m].flag.noreturn)
  4273. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4274. break;
  4275. case 2: // SavePoint
  4276. if(!map[pl_sd->bl.m].flag.noreturn)
  4277. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4278. break;
  4279. case 3: // m,x,y
  4280. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4281. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4282. break;
  4283. }
  4284. }
  4285. mapit_free(iter);
  4286. return 0;
  4287. }
  4288. /*==========================================
  4289. *
  4290. *------------------------------------------*/
  4291. BUILDIN_FUNC(heal)
  4292. {
  4293. TBL_PC *sd;
  4294. int hp,sp;
  4295. sd = script_rid2sd(st);
  4296. if (!sd) return 0;
  4297. hp=script_getnum(st,2);
  4298. sp=script_getnum(st,3);
  4299. status_heal(&sd->bl, hp, sp, 1);
  4300. return 0;
  4301. }
  4302. /*==========================================
  4303. *
  4304. *------------------------------------------*/
  4305. BUILDIN_FUNC(itemheal)
  4306. {
  4307. TBL_PC *sd;
  4308. int hp,sp;
  4309. hp=script_getnum(st,2);
  4310. sp=script_getnum(st,3);
  4311. if(potion_flag==1) {
  4312. potion_hp = hp;
  4313. potion_sp = sp;
  4314. return 0;
  4315. }
  4316. sd = script_rid2sd(st);
  4317. if (!sd) return 0;
  4318. pc_itemheal(sd,sd->itemid,hp,sp);
  4319. return 0;
  4320. }
  4321. /*==========================================
  4322. *
  4323. *------------------------------------------*/
  4324. BUILDIN_FUNC(percentheal)
  4325. {
  4326. int hp,sp;
  4327. TBL_PC* sd;
  4328. hp=script_getnum(st,2);
  4329. sp=script_getnum(st,3);
  4330. if(potion_flag==1) {
  4331. potion_per_hp = hp;
  4332. potion_per_sp = sp;
  4333. return 0;
  4334. }
  4335. sd = script_rid2sd(st);
  4336. if( sd == NULL )
  4337. return 0;
  4338. pc_percentheal(sd,hp,sp);
  4339. return 0;
  4340. }
  4341. /*==========================================
  4342. *
  4343. *------------------------------------------*/
  4344. BUILDIN_FUNC(jobchange)
  4345. {
  4346. int job, upper=-1;
  4347. job=script_getnum(st,2);
  4348. if( script_hasdata(st,3) )
  4349. upper=script_getnum(st,3);
  4350. if (pcdb_checkid(job))
  4351. {
  4352. TBL_PC* sd;
  4353. sd = script_rid2sd(st);
  4354. if( sd == NULL )
  4355. return 0;
  4356. pc_jobchange(sd, job, upper);
  4357. }
  4358. return 0;
  4359. }
  4360. /*==========================================
  4361. *
  4362. *------------------------------------------*/
  4363. BUILDIN_FUNC(jobname)
  4364. {
  4365. int class_=script_getnum(st,2);
  4366. script_pushconststr(st, (char*)job_name(class_));
  4367. return 0;
  4368. }
  4369. /// Get input from the player.
  4370. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4371. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4372. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4373. /// shorter than 'min' and 0 otherwise.
  4374. ///
  4375. /// input(<var>{,<min>{,<max>}}) -> <int>
  4376. BUILDIN_FUNC(input)
  4377. {
  4378. TBL_PC* sd;
  4379. struct script_data* data;
  4380. int uid;
  4381. const char* name;
  4382. int min;
  4383. int max;
  4384. sd = script_rid2sd(st);
  4385. if( sd == NULL )
  4386. return 0;
  4387. data = script_getdata(st,2);
  4388. if( !data_isreference(data) ){
  4389. ShowError("script:input: not a variable\n");
  4390. script_reportdata(data);
  4391. st->state = END;
  4392. return 1;
  4393. }
  4394. uid = reference_getuid(data);
  4395. name = reference_getname(data);
  4396. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4397. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4398. if( !sd->state.menu_or_input )
  4399. { // first invocation, display npc input box
  4400. sd->state.menu_or_input = 1;
  4401. st->state = RERUNLINE;
  4402. if( is_string_variable(name) )
  4403. clif_scriptinputstr(sd,st->oid);
  4404. else
  4405. clif_scriptinput(sd,st->oid);
  4406. }
  4407. else
  4408. { // take received text/value and store it in the designated variable
  4409. sd->state.menu_or_input = 0;
  4410. if( is_string_variable(name) )
  4411. {
  4412. int len = (int)strlen(sd->npc_str);
  4413. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4414. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4415. }
  4416. else
  4417. {
  4418. int amount = sd->npc_amount;
  4419. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4420. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4421. }
  4422. st->state = RUN;
  4423. }
  4424. return 0;
  4425. }
  4426. /// Sets the value of a variable.
  4427. /// The value is converted to the type of the variable.
  4428. ///
  4429. /// set(<variable>,<value>) -> <variable>
  4430. BUILDIN_FUNC(set)
  4431. {
  4432. TBL_PC* sd = NULL;
  4433. struct script_data* data;
  4434. int num;
  4435. const char* name;
  4436. char prefix;
  4437. data = script_getdata(st,2);
  4438. if( !data_isreference(data) )
  4439. {
  4440. ShowError("script:set: not a variable\n");
  4441. script_reportdata(script_getdata(st,2));
  4442. st->state = END;
  4443. return 1;
  4444. }
  4445. num = reference_getuid(data);
  4446. name = reference_getname(data);
  4447. prefix = *name;
  4448. if( not_server_variable(prefix) )
  4449. {
  4450. sd = script_rid2sd(st);
  4451. if( sd == NULL )
  4452. {
  4453. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4454. return 0;
  4455. }
  4456. }
  4457. if( is_string_variable(name) )
  4458. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4459. else
  4460. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4461. // return a copy of the variable reference
  4462. script_pushcopy(st,2);
  4463. return 0;
  4464. }
  4465. /////////////////////////////////////////////////////////////////////
  4466. /// Array variables
  4467. ///
  4468. /// Returns the size of the specified array
  4469. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4470. {
  4471. int32 ret = idx;
  4472. if( isstring )
  4473. {
  4474. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4475. {
  4476. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4477. if( str && *str )
  4478. ret = idx + 1;
  4479. script_removetop(st, -1, 0);
  4480. }
  4481. }
  4482. else
  4483. {
  4484. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4485. {
  4486. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  4487. if( num )
  4488. ret = idx + 1;
  4489. script_removetop(st, -1, 0);
  4490. }
  4491. }
  4492. return ret;
  4493. }
  4494. /// Sets values of an array, from the starting index.
  4495. /// ex: setarray arr[1],1,2,3;
  4496. ///
  4497. /// setarray <array variable>,<value1>{,<value2>...};
  4498. BUILDIN_FUNC(setarray)
  4499. {
  4500. struct script_data* data;
  4501. const char* name;
  4502. int32 start;
  4503. int32 end;
  4504. int32 id;
  4505. int32 i;
  4506. TBL_PC* sd = NULL;
  4507. data = script_getdata(st, 2);
  4508. if( !data_isreference(data) )
  4509. {
  4510. ShowError("script:setarray: not a variable\n");
  4511. script_reportdata(data);
  4512. st->state = END;
  4513. return 1;// not a variable
  4514. }
  4515. id = reference_getid(data);
  4516. start = reference_getindex(data);
  4517. name = reference_getname(data);
  4518. if( not_array_variable(*name) )
  4519. {
  4520. ShowError("script:setarray: illegal scope\n");
  4521. script_reportdata(data);
  4522. st->state = END;
  4523. return 1;// not supported
  4524. }
  4525. if( not_server_variable(*name) )
  4526. {
  4527. sd = script_rid2sd(st);
  4528. if( sd == NULL )
  4529. return 0;// no player attached
  4530. }
  4531. end = start + script_lastdata(st) - 2;
  4532. if( end > SCRIPT_MAX_ARRAYSIZE )
  4533. end = SCRIPT_MAX_ARRAYSIZE;
  4534. if( is_string_variable(name) )
  4535. {// string array
  4536. for( i = 3; start < end; ++start, ++i )
  4537. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4538. }
  4539. else
  4540. {// int array
  4541. for( i = 3; start < end; ++start, ++i )
  4542. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  4543. }
  4544. return 0;
  4545. }
  4546. /// Sets count values of an array, from the starting index.
  4547. /// ex: cleararray arr[0],0,1;
  4548. ///
  4549. /// cleararray <array variable>,<value>,<count>;
  4550. BUILDIN_FUNC(cleararray)
  4551. {
  4552. struct script_data* data;
  4553. const char* name;
  4554. int32 start;
  4555. int32 end;
  4556. int32 id;
  4557. void* v;
  4558. TBL_PC* sd = NULL;
  4559. data = script_getdata(st, 2);
  4560. if( !data_isreference(data) )
  4561. {
  4562. ShowError("script:cleararray: not a variable\n");
  4563. script_reportdata(data);
  4564. st->state = END;
  4565. return 1;// not a variable
  4566. }
  4567. id = reference_getid(data);
  4568. start = reference_getindex(data);
  4569. name = reference_getname(data);
  4570. if( not_array_variable(*name) )
  4571. {
  4572. ShowError("script:cleararray: illegal scope\n");
  4573. script_reportdata(data);
  4574. st->state = END;
  4575. return 1;// not supported
  4576. }
  4577. if( not_server_variable(*name) )
  4578. {
  4579. sd = script_rid2sd(st);
  4580. if( sd == NULL )
  4581. return 0;// no player attached
  4582. }
  4583. if( is_string_variable(name) )
  4584. v = (void*)script_getstr(st, 3);
  4585. else
  4586. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  4587. end = start + script_getnum(st, 4);
  4588. if( end > SCRIPT_MAX_ARRAYSIZE )
  4589. end = SCRIPT_MAX_ARRAYSIZE;
  4590. for( ; start < end; ++start )
  4591. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4592. return 0;
  4593. }
  4594. /// Copies data from one array to another.
  4595. /// ex: copyarray arr[0],arr[2],2;
  4596. ///
  4597. /// copyarray <destination array variable>,<source array variable>,<count>;
  4598. BUILDIN_FUNC(copyarray)
  4599. {
  4600. struct script_data* data1;
  4601. struct script_data* data2;
  4602. const char* name1;
  4603. const char* name2;
  4604. int32 idx1;
  4605. int32 idx2;
  4606. int32 id1;
  4607. int32 id2;
  4608. void* v;
  4609. int32 i;
  4610. int32 count;
  4611. TBL_PC* sd = NULL;
  4612. data1 = script_getdata(st, 2);
  4613. data2 = script_getdata(st, 3);
  4614. if( !data_isreference(data1) || !data_isreference(data2) )
  4615. {
  4616. ShowError("script:copyarray: not a variable\n");
  4617. script_reportdata(data1);
  4618. script_reportdata(data2);
  4619. st->state = END;
  4620. return 1;// not a variable
  4621. }
  4622. id1 = reference_getid(data1);
  4623. id2 = reference_getid(data2);
  4624. idx1 = reference_getindex(data1);
  4625. idx2 = reference_getindex(data2);
  4626. name1 = reference_getname(data1);
  4627. name2 = reference_getname(data2);
  4628. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4629. {
  4630. ShowError("script:copyarray: illegal scope\n");
  4631. script_reportdata(data1);
  4632. script_reportdata(data2);
  4633. st->state = END;
  4634. return 1;// not supported
  4635. }
  4636. if( is_string_variable(name1) != is_string_variable(name2) )
  4637. {
  4638. ShowError("script:copyarray: type mismatch\n");
  4639. script_reportdata(data1);
  4640. script_reportdata(data2);
  4641. st->state = END;
  4642. return 1;// data type mismatch
  4643. }
  4644. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4645. {
  4646. sd = script_rid2sd(st);
  4647. if( sd == NULL )
  4648. return 0;// no player attached
  4649. }
  4650. count = script_getnum(st, 4);
  4651. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  4652. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  4653. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4654. return 0;// nothing to copy
  4655. if( id1 == id2 && idx1 > idx2 )
  4656. {// destination might be overlapping the source - copy in reverse order
  4657. for( i = count - 1; i >= 0; --i )
  4658. {
  4659. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4660. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4661. script_removetop(st, -1, 0);
  4662. }
  4663. }
  4664. else
  4665. {// normal copy
  4666. for( i = 0; i < count; ++i )
  4667. {
  4668. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  4669. {
  4670. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4671. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4672. script_removetop(st, -1, 0);
  4673. }
  4674. else// out of range - assume ""/0
  4675. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4676. }
  4677. }
  4678. return 0;
  4679. }
  4680. /// Returns the size of the array.
  4681. /// Assumes that everything before the starting index exists.
  4682. /// ex: getarraysize(arr[3])
  4683. ///
  4684. /// getarraysize(<array variable>) -> <int>
  4685. BUILDIN_FUNC(getarraysize)
  4686. {
  4687. struct script_data* data;
  4688. const char* name;
  4689. data = script_getdata(st, 2);
  4690. if( !data_isreference(data) )
  4691. {
  4692. ShowError("script:getarraysize: not a variable\n");
  4693. script_reportdata(data);
  4694. script_pushnil(st);
  4695. st->state = END;
  4696. return 1;// not a variable
  4697. }
  4698. name = reference_getname(data);
  4699. if( not_array_variable(*name) )
  4700. {
  4701. ShowError("script:getarraysize: illegal scope\n");
  4702. script_reportdata(data);
  4703. script_pushnil(st);
  4704. st->state = END;
  4705. return 1;// not supported
  4706. }
  4707. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4708. return 0;
  4709. }
  4710. /// Deletes count or all the elements in an array, from the starting index.
  4711. /// ex: deletearray arr[4],2;
  4712. ///
  4713. /// deletearray <array variable>;
  4714. /// deletearray <array variable>,<count>;
  4715. BUILDIN_FUNC(deletearray)
  4716. {
  4717. struct script_data* data;
  4718. const char* name;
  4719. int start;
  4720. int end;
  4721. int id;
  4722. TBL_PC *sd = NULL;
  4723. data = script_getdata(st, 2);
  4724. if( !data_isreference(data) )
  4725. {
  4726. ShowError("script:deletearray: not a variable\n");
  4727. script_reportdata(data);
  4728. st->state = END;
  4729. return 1;// not a variable
  4730. }
  4731. id = reference_getid(data);
  4732. start = reference_getindex(data);
  4733. name = reference_getname(data);
  4734. if( not_array_variable(*name) )
  4735. {
  4736. ShowError("script:deletearray: illegal scope\n");
  4737. script_reportdata(data);
  4738. st->state = END;
  4739. return 1;// not supported
  4740. }
  4741. if( not_server_variable(*name) )
  4742. {
  4743. sd = script_rid2sd(st);
  4744. if( sd == NULL )
  4745. return 0;// no player attached
  4746. }
  4747. end = SCRIPT_MAX_ARRAYSIZE;
  4748. if( start >= end )
  4749. return 0;// nothing to free
  4750. if( script_hasdata(st,3) )
  4751. {
  4752. int count = script_getnum(st, 3);
  4753. if( count > end - start )
  4754. count = end - start;
  4755. if( count <= 0 )
  4756. return 0;// nothing to free
  4757. // move rest of the elements backward
  4758. for( ; start + count < end; ++start )
  4759. {
  4760. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4761. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4762. script_removetop(st, -1, 0);
  4763. }
  4764. }
  4765. // clear the rest of the array
  4766. if( is_string_variable(name) )
  4767. {
  4768. for( ; start < end; ++start )
  4769. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4770. }
  4771. else
  4772. {
  4773. for( ; start < end; ++start )
  4774. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4775. }
  4776. return 0;
  4777. }
  4778. /// Returns a reference to the target index of the array variable.
  4779. /// Equivalent to var[index].
  4780. ///
  4781. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4782. BUILDIN_FUNC(getelementofarray)
  4783. {
  4784. struct script_data* data;
  4785. const char* name;
  4786. int32 id;
  4787. int i;
  4788. data = script_getdata(st, 2);
  4789. if( !data_isreference(data) )
  4790. {
  4791. ShowError("script:getelementofarray: not a variable\n");
  4792. script_reportdata(data);
  4793. script_pushnil(st);
  4794. st->state = END;
  4795. return 1;// not a variable
  4796. }
  4797. id = reference_getid(data);
  4798. name = reference_getname(data);
  4799. if( not_array_variable(*name) )
  4800. {
  4801. ShowError("script:getelementofarray: illegal scope\n");
  4802. script_reportdata(data);
  4803. script_pushnil(st);
  4804. st->state = END;
  4805. return 1;// not supported
  4806. }
  4807. i = script_getnum(st, 3);
  4808. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  4809. {
  4810. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4811. script_reportdata(data);
  4812. script_pushnil(st);
  4813. st->state = END;
  4814. return 1;// out of range
  4815. }
  4816. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4817. return 0;
  4818. }
  4819. /////////////////////////////////////////////////////////////////////
  4820. /// ...
  4821. ///
  4822. /*==========================================
  4823. *
  4824. *------------------------------------------*/
  4825. BUILDIN_FUNC(setlook)
  4826. {
  4827. int type,val;
  4828. TBL_PC* sd;
  4829. type=script_getnum(st,2);
  4830. val=script_getnum(st,3);
  4831. sd = script_rid2sd(st);
  4832. if( sd == NULL )
  4833. return 0;
  4834. pc_changelook(sd,type,val);
  4835. return 0;
  4836. }
  4837. BUILDIN_FUNC(changelook)
  4838. { // As setlook but only client side
  4839. int type,val;
  4840. TBL_PC* sd;
  4841. type=script_getnum(st,2);
  4842. val=script_getnum(st,3);
  4843. sd = script_rid2sd(st);
  4844. if( sd == NULL )
  4845. return 0;
  4846. clif_changelook(&sd->bl,type,val);
  4847. return 0;
  4848. }
  4849. /*==========================================
  4850. *
  4851. *------------------------------------------*/
  4852. BUILDIN_FUNC(cutin)
  4853. {
  4854. TBL_PC* sd;
  4855. sd = script_rid2sd(st);
  4856. if( sd == NULL )
  4857. return 0;
  4858. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4859. return 0;
  4860. }
  4861. /*==========================================
  4862. *
  4863. *------------------------------------------*/
  4864. BUILDIN_FUNC(viewpoint)
  4865. {
  4866. int type,x,y,id,color;
  4867. TBL_PC* sd;
  4868. type=script_getnum(st,2);
  4869. x=script_getnum(st,3);
  4870. y=script_getnum(st,4);
  4871. id=script_getnum(st,5);
  4872. color=script_getnum(st,6);
  4873. sd = script_rid2sd(st);
  4874. if( sd == NULL )
  4875. return 0;
  4876. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4877. return 0;
  4878. }
  4879. /*==========================================
  4880. *
  4881. *------------------------------------------*/
  4882. BUILDIN_FUNC(countitem)
  4883. {
  4884. int nameid, i;
  4885. int count = 0;
  4886. struct item_data* id = NULL;
  4887. struct script_data* data;
  4888. TBL_PC* sd = script_rid2sd(st);
  4889. if (!sd) {
  4890. script_pushint(st,0);
  4891. return 0;
  4892. }
  4893. data = script_getdata(st,2);
  4894. get_val(st, data); // convert into value in case of a variable
  4895. if( data_isstring(data) )
  4896. {// item name
  4897. id = itemdb_searchname(conv_str(st, data));
  4898. }
  4899. else
  4900. {// item id
  4901. id = itemdb_exists(conv_num(st, data));
  4902. }
  4903. if( id == NULL )
  4904. {
  4905. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4906. script_pushint(st,0);
  4907. return 1;
  4908. }
  4909. nameid = id->nameid;
  4910. for(i = 0; i < MAX_INVENTORY; i++)
  4911. if(sd->status.inventory[i].nameid == nameid)
  4912. count += sd->status.inventory[i].amount;
  4913. script_pushint(st,count);
  4914. return 0;
  4915. }
  4916. /*==========================================
  4917. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4918. * returns number of items that meet the conditions
  4919. *------------------------------------------*/
  4920. BUILDIN_FUNC(countitem2)
  4921. {
  4922. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4923. int count = 0;
  4924. int i;
  4925. struct item_data* id = NULL;
  4926. struct script_data* data;
  4927. TBL_PC* sd = script_rid2sd(st);
  4928. if (!sd) {
  4929. script_pushint(st,0);
  4930. return 0;
  4931. }
  4932. data = script_getdata(st,2);
  4933. get_val(st, data); // convert into value in case of a variable
  4934. if( data_isstring(data) )
  4935. {// item name
  4936. id = itemdb_searchname(conv_str(st, data));
  4937. }
  4938. else
  4939. {// item id
  4940. id = itemdb_exists(conv_num(st, data));
  4941. }
  4942. if( id == NULL )
  4943. {
  4944. ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4945. script_pushint(st,0);
  4946. return 1;
  4947. }
  4948. nameid = id->nameid;
  4949. iden = script_getnum(st,3);
  4950. ref = script_getnum(st,4);
  4951. attr = script_getnum(st,5);
  4952. c1 = (short)script_getnum(st,6);
  4953. c2 = (short)script_getnum(st,7);
  4954. c3 = (short)script_getnum(st,8);
  4955. c4 = (short)script_getnum(st,9);
  4956. for(i = 0; i < MAX_INVENTORY; i++)
  4957. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4958. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4959. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4960. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4961. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4962. sd->status.inventory[i].card[3] == c4
  4963. )
  4964. count += sd->status.inventory[i].amount;
  4965. script_pushint(st,count);
  4966. return 0;
  4967. }
  4968. /*==========================================
  4969. * 重量チェック
  4970. *------------------------------------------*/
  4971. BUILDIN_FUNC(checkweight)
  4972. {
  4973. int nameid, amount, slots;
  4974. unsigned int weight;
  4975. struct item_data* id = NULL;
  4976. struct map_session_data* sd;
  4977. struct script_data* data;
  4978. if( ( sd = script_rid2sd(st) ) == NULL )
  4979. {
  4980. return 0;
  4981. }
  4982. data = script_getdata(st,2);
  4983. get_val(st, data); // convert into value in case of a variable
  4984. if( data_isstring(data) )
  4985. {// item name
  4986. id = itemdb_searchname(conv_str(st, data));
  4987. }
  4988. else
  4989. {// item id
  4990. id = itemdb_exists(conv_num(st, data));
  4991. }
  4992. if( id == NULL )
  4993. {
  4994. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4995. script_pushint(st,0);
  4996. return 1;
  4997. }
  4998. nameid = id->nameid;
  4999. amount = script_getnum(st,3);
  5000. if( amount < 1 )
  5001. {
  5002. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5003. script_pushint(st,0);
  5004. return 1;
  5005. }
  5006. weight = itemdb_weight(nameid)*amount;
  5007. if( weight + sd->weight > sd->max_weight )
  5008. {// too heavy
  5009. script_pushint(st,0);
  5010. return 0;
  5011. }
  5012. switch( pc_checkadditem(sd, nameid, amount) )
  5013. {
  5014. case ADDITEM_EXIST:
  5015. // item is already in inventory, but there is still space for the requested amount
  5016. break;
  5017. case ADDITEM_NEW:
  5018. slots = pc_inventoryblank(sd);
  5019. if( itemdb_isstackable(nameid) )
  5020. {// stackable
  5021. if( slots < 1 )
  5022. {
  5023. script_pushint(st,0);
  5024. return 0;
  5025. }
  5026. }
  5027. else
  5028. {// non-stackable
  5029. if( slots < amount )
  5030. {
  5031. script_pushint(st,0);
  5032. return 0;
  5033. }
  5034. }
  5035. break;
  5036. case ADDITEM_OVERAMOUNT:
  5037. script_pushint(st,0);
  5038. return 0;
  5039. }
  5040. script_pushint(st,1);
  5041. return 0;
  5042. }
  5043. /*==========================================
  5044. * getitem <item id>,<amount>{,<account ID>};
  5045. * getitem "<item name>",<amount>{,<account ID>};
  5046. *------------------------------------------*/
  5047. BUILDIN_FUNC(getitem)
  5048. {
  5049. int nameid,amount,get_count,i,flag = 0;
  5050. struct item it;
  5051. TBL_PC *sd;
  5052. struct script_data *data;
  5053. data=script_getdata(st,2);
  5054. get_val(st,data);
  5055. if( data_isstring(data) )
  5056. {// "<item name>"
  5057. const char *name=conv_str(st,data);
  5058. struct item_data *item_data = itemdb_searchname(name);
  5059. if( item_data == NULL ){
  5060. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5061. return 1; //No item created.
  5062. }
  5063. nameid=item_data->nameid;
  5064. } else if( data_isint(data) )
  5065. {// <item id>
  5066. nameid=conv_num(st,data);
  5067. //Violet Box, Blue Box, etc - random item pick
  5068. if( nameid < 0 ) {
  5069. nameid = -nameid;
  5070. flag = 1;
  5071. }
  5072. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5073. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5074. return 1; //No item created.
  5075. }
  5076. } else {
  5077. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5078. return 1;
  5079. }
  5080. // <amount>
  5081. if( (amount=script_getnum(st,3)) <= 0)
  5082. return 0; //return if amount <=0, skip the useles iteration
  5083. memset(&it,0,sizeof(it));
  5084. it.nameid=nameid;
  5085. if(!flag)
  5086. it.identify=1;
  5087. else
  5088. it.identify=itemdb_isidentified(nameid);
  5089. if( script_hasdata(st,4) )
  5090. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5091. else
  5092. sd=script_rid2sd(st); // Attached player
  5093. if( sd == NULL ) // no target
  5094. return 0;
  5095. //Check if it's stackable.
  5096. if (!itemdb_isstackable(nameid))
  5097. get_count = 1;
  5098. else
  5099. get_count = amount;
  5100. for (i = 0; i < amount; i += get_count)
  5101. {
  5102. // if not pet egg
  5103. if (!pet_create_egg(sd, nameid))
  5104. {
  5105. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5106. {
  5107. clif_additem(sd, 0, 0, flag);
  5108. if( pc_candrop(sd,&it) )
  5109. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5110. }
  5111. }
  5112. }
  5113. return 0;
  5114. }
  5115. /*==========================================
  5116. *
  5117. *------------------------------------------*/
  5118. BUILDIN_FUNC(getitem2)
  5119. {
  5120. int nameid,amount,get_count,i,flag = 0;
  5121. int iden,ref,attr,c1,c2,c3,c4;
  5122. struct item_data *item_data;
  5123. struct item item_tmp;
  5124. TBL_PC *sd;
  5125. struct script_data *data;
  5126. if( script_hasdata(st,11) )
  5127. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5128. else
  5129. sd=script_rid2sd(st); // Attached player
  5130. if( sd == NULL ) // no target
  5131. return 0;
  5132. data=script_getdata(st,2);
  5133. get_val(st,data);
  5134. if( data_isstring(data) ){
  5135. const char *name=conv_str(st,data);
  5136. struct item_data *item_data = itemdb_searchname(name);
  5137. if( item_data )
  5138. nameid=item_data->nameid;
  5139. else
  5140. nameid=UNKNOWN_ITEM_ID;
  5141. }else
  5142. nameid=conv_num(st,data);
  5143. amount=script_getnum(st,3);
  5144. iden=script_getnum(st,4);
  5145. ref=script_getnum(st,5);
  5146. attr=script_getnum(st,6);
  5147. c1=(short)script_getnum(st,7);
  5148. c2=(short)script_getnum(st,8);
  5149. c3=(short)script_getnum(st,9);
  5150. c4=(short)script_getnum(st,10);
  5151. if(nameid<0) { // ランダム
  5152. nameid = -nameid;
  5153. flag = 1;
  5154. }
  5155. if(nameid > 0) {
  5156. memset(&item_tmp,0,sizeof(item_tmp));
  5157. item_data=itemdb_exists(nameid);
  5158. if (item_data == NULL)
  5159. return -1;
  5160. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5161. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5162. }
  5163. else if(item_data->type==IT_PETEGG) {
  5164. iden = 1;
  5165. ref = 0;
  5166. }
  5167. else {
  5168. iden = 1;
  5169. ref = attr = 0;
  5170. }
  5171. item_tmp.nameid=nameid;
  5172. if(!flag)
  5173. item_tmp.identify=iden;
  5174. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5175. item_tmp.identify=0;
  5176. item_tmp.refine=ref;
  5177. item_tmp.attribute=attr;
  5178. item_tmp.card[0]=(short)c1;
  5179. item_tmp.card[1]=(short)c2;
  5180. item_tmp.card[2]=(short)c3;
  5181. item_tmp.card[3]=(short)c4;
  5182. //Check if it's stackable.
  5183. if (!itemdb_isstackable(nameid))
  5184. get_count = 1;
  5185. else
  5186. get_count = amount;
  5187. for (i = 0; i < amount; i += get_count)
  5188. {
  5189. // if not pet egg
  5190. if (!pet_create_egg(sd, nameid))
  5191. {
  5192. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5193. {
  5194. clif_additem(sd, 0, 0, flag);
  5195. if( pc_candrop(sd,&item_tmp) )
  5196. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5197. }
  5198. }
  5199. }
  5200. }
  5201. return 0;
  5202. }
  5203. /*==========================================
  5204. * rentitem <item id>,<seconds>
  5205. * rentitem "<item name>",<seconds>
  5206. *------------------------------------------*/
  5207. BUILDIN_FUNC(rentitem)
  5208. {
  5209. struct map_session_data *sd;
  5210. struct script_data *data;
  5211. struct item it;
  5212. int seconds;
  5213. int nameid = 0, flag;
  5214. data = script_getdata(st,2);
  5215. get_val(st,data);
  5216. if( (sd = script_rid2sd(st)) == NULL )
  5217. return 0;
  5218. if( data_isstring(data) )
  5219. {
  5220. const char *name = conv_str(st,data);
  5221. struct item_data *itd = itemdb_searchname(name);
  5222. if( itd == NULL )
  5223. {
  5224. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5225. return 1;
  5226. }
  5227. nameid = itd->nameid;
  5228. }
  5229. else if( data_isint(data) )
  5230. {
  5231. nameid = conv_num(st,data);
  5232. if( nameid <= 0 || !itemdb_exists(nameid) )
  5233. {
  5234. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5235. return 1;
  5236. }
  5237. }
  5238. else
  5239. {
  5240. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5241. return 1;
  5242. }
  5243. seconds = script_getnum(st,3);
  5244. memset(&it, 0, sizeof(it));
  5245. it.nameid = nameid;
  5246. it.identify = 1;
  5247. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5248. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5249. {
  5250. clif_additem(sd, 0, 0, flag);
  5251. return 1;
  5252. }
  5253. clif_rental_time(sd->fd, nameid, seconds);
  5254. pc_inventory_rental_add(sd, seconds);
  5255. return 0;
  5256. }
  5257. /*==========================================
  5258. * gets an item with someone's name inscribed [Skotlex]
  5259. * getinscribeditem item_num, character_name
  5260. * Returned Qty is always 1, only works on equip-able
  5261. * equipment
  5262. *------------------------------------------*/
  5263. BUILDIN_FUNC(getnameditem)
  5264. {
  5265. int nameid;
  5266. struct item item_tmp;
  5267. TBL_PC *sd, *tsd;
  5268. struct script_data *data;
  5269. sd = script_rid2sd(st);
  5270. if (sd == NULL)
  5271. { //Player not attached!
  5272. script_pushint(st,0);
  5273. return 0;
  5274. }
  5275. data=script_getdata(st,2);
  5276. get_val(st,data);
  5277. if( data_isstring(data) ){
  5278. const char *name=conv_str(st,data);
  5279. struct item_data *item_data = itemdb_searchname(name);
  5280. if( item_data == NULL)
  5281. { //Failed
  5282. script_pushint(st,0);
  5283. return 0;
  5284. }
  5285. nameid = item_data->nameid;
  5286. }else
  5287. nameid = conv_num(st,data);
  5288. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5289. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5290. script_pushint(st,0);
  5291. return 0;
  5292. }
  5293. data=script_getdata(st,3);
  5294. get_val(st,data);
  5295. if( data_isstring(data) ) //Char Name
  5296. tsd=map_nick2sd(conv_str(st,data));
  5297. else //Char Id was given
  5298. tsd=map_charid2sd(conv_num(st,data));
  5299. if( tsd == NULL )
  5300. { //Failed
  5301. script_pushint(st,0);
  5302. return 0;
  5303. }
  5304. memset(&item_tmp,0,sizeof(item_tmp));
  5305. item_tmp.nameid=nameid;
  5306. item_tmp.amount=1;
  5307. item_tmp.identify=1;
  5308. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5309. item_tmp.card[2]=tsd->status.char_id;
  5310. item_tmp.card[3]=tsd->status.char_id >> 16;
  5311. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5312. script_pushint(st,0);
  5313. return 0; //Failed to add item, we will not drop if they don't fit
  5314. }
  5315. script_pushint(st,1);
  5316. return 0;
  5317. }
  5318. /*==========================================
  5319. * gets a random item ID from an item group [Skotlex]
  5320. * groupranditem group_num
  5321. *------------------------------------------*/
  5322. BUILDIN_FUNC(grouprandomitem)
  5323. {
  5324. int group;
  5325. group = script_getnum(st,2);
  5326. script_pushint(st,itemdb_searchrandomid(group));
  5327. return 0;
  5328. }
  5329. /*==========================================
  5330. *
  5331. *------------------------------------------*/
  5332. BUILDIN_FUNC(makeitem)
  5333. {
  5334. int nameid,amount,flag = 0;
  5335. int x,y,m;
  5336. const char *mapname;
  5337. struct item item_tmp;
  5338. struct script_data *data;
  5339. data=script_getdata(st,2);
  5340. get_val(st,data);
  5341. if( data_isstring(data) ){
  5342. const char *name=conv_str(st,data);
  5343. struct item_data *item_data = itemdb_searchname(name);
  5344. if( item_data )
  5345. nameid=item_data->nameid;
  5346. else
  5347. nameid=UNKNOWN_ITEM_ID;
  5348. }else
  5349. nameid=conv_num(st,data);
  5350. amount=script_getnum(st,3);
  5351. mapname =script_getstr(st,4);
  5352. x =script_getnum(st,5);
  5353. y =script_getnum(st,6);
  5354. if(strcmp(mapname,"this")==0)
  5355. {
  5356. TBL_PC *sd;
  5357. sd = script_rid2sd(st);
  5358. if (!sd) return 0; //Failed...
  5359. m=sd->bl.m;
  5360. } else
  5361. m=map_mapname2mapid(mapname);
  5362. if(nameid<0) { // ランダム
  5363. nameid = -nameid;
  5364. flag = 1;
  5365. }
  5366. if(nameid > 0) {
  5367. memset(&item_tmp,0,sizeof(item_tmp));
  5368. item_tmp.nameid=nameid;
  5369. if(!flag)
  5370. item_tmp.identify=1;
  5371. else
  5372. item_tmp.identify=itemdb_isidentified(nameid);
  5373. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5374. }
  5375. return 0;
  5376. }
  5377. /// Counts / deletes the current item given by idx.
  5378. /// Used by buildin_delitem_search
  5379. /// Relies on all input data being already fully valid.
  5380. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5381. {
  5382. int delamount;
  5383. struct item* inv = &sd->status.inventory[idx];
  5384. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5385. if( delete_items )
  5386. {
  5387. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5388. {// delete associated pet
  5389. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5390. }
  5391. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5392. }
  5393. amount[0]-= delamount;
  5394. }
  5395. /// Searches for item(s) and checks, if there is enough of them.
  5396. /// Used by delitem and delitem2
  5397. /// Relies on all input data being already fully valid.
  5398. /// @param exact_match will also match item attributes and cards, not just name id
  5399. /// @return true when all items could be deleted, false when there were not enough items to delete
  5400. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5401. {
  5402. bool delete_items = false;
  5403. int i, amount, important;
  5404. struct item* inv;
  5405. // prefer always non-equipped items
  5406. it->equip = 0;
  5407. // when searching for nameid only, prefer additionally
  5408. if( !exact_match )
  5409. {
  5410. // non-refined items
  5411. it->refine = 0;
  5412. // card-less items
  5413. memset(it->card, 0, sizeof(it->card));
  5414. }
  5415. for(;;)
  5416. {
  5417. amount = it->amount;
  5418. important = 0;
  5419. // 1st pass -- less important items / exact match
  5420. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5421. {
  5422. inv = &sd->status.inventory[i];
  5423. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5424. {// wrong/invalid item
  5425. continue;
  5426. }
  5427. if( inv->equip != it->equip || inv->refine != it->refine )
  5428. {// not matching attributes
  5429. important++;
  5430. continue;
  5431. }
  5432. if( exact_match )
  5433. {
  5434. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5435. {// not matching exact attributes
  5436. continue;
  5437. }
  5438. }
  5439. else
  5440. {
  5441. if( sd->inventory_data[i]->type == IT_PETEGG )
  5442. {
  5443. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5444. {// pet which cannot be deleted
  5445. continue;
  5446. }
  5447. }
  5448. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  5449. {// named/carded item
  5450. important++;
  5451. continue;
  5452. }
  5453. }
  5454. // count / delete item
  5455. buildin_delitem_delete(sd, i, &amount, delete_items);
  5456. }
  5457. // 2nd pass -- any matching item
  5458. if( amount == 0 || important == 0 )
  5459. {// either everything was already consumed or no items were skipped
  5460. ;
  5461. }
  5462. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5463. {
  5464. inv = &sd->status.inventory[i];
  5465. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5466. {// wrong/invalid item
  5467. continue;
  5468. }
  5469. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  5470. {// pet which cannot be deleted
  5471. continue;
  5472. }
  5473. if( exact_match )
  5474. {
  5475. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5476. {// not matching attributes
  5477. continue;
  5478. }
  5479. }
  5480. // count / delete item
  5481. buildin_delitem_delete(sd, i, &amount, delete_items);
  5482. }
  5483. if( amount )
  5484. {// not enough items
  5485. return false;
  5486. }
  5487. else if( delete_items )
  5488. {// we are done with the work
  5489. return true;
  5490. }
  5491. else
  5492. {// get rid of the items now
  5493. delete_items = true;
  5494. }
  5495. }
  5496. }
  5497. /// Deletes items from the target/attached player.
  5498. /// Prioritizes ordinary items.
  5499. ///
  5500. /// delitem <item id>,<amount>{,<account id>}
  5501. /// delitem "<item name>",<amount>{,<account id>}
  5502. BUILDIN_FUNC(delitem)
  5503. {
  5504. TBL_PC *sd;
  5505. struct item it;
  5506. struct script_data *data;
  5507. if( script_hasdata(st,4) )
  5508. {
  5509. int account_id = script_getnum(st,4);
  5510. sd = map_id2sd(account_id); // <account id>
  5511. if( sd == NULL )
  5512. {
  5513. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5514. st->state = END;
  5515. return 1;
  5516. }
  5517. }
  5518. else
  5519. {
  5520. sd = script_rid2sd(st);// attached player
  5521. if( sd == NULL )
  5522. return 0;
  5523. }
  5524. data = script_getdata(st,2);
  5525. get_val(st,data);
  5526. if( data_isstring(data) )
  5527. {
  5528. const char* item_name = conv_str(st,data);
  5529. struct item_data* id = itemdb_searchname(item_name);
  5530. if( id == NULL )
  5531. {
  5532. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5533. st->state = END;
  5534. return 1;
  5535. }
  5536. it.nameid = id->nameid;// "<item name>"
  5537. }
  5538. else
  5539. {
  5540. it.nameid = conv_num(st,data);// <item id>
  5541. if( !itemdb_exists( it.nameid ) )
  5542. {
  5543. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5544. st->state = END;
  5545. return 1;
  5546. }
  5547. }
  5548. it.amount=script_getnum(st,3);
  5549. if( it.amount <= 0 )
  5550. return 0;// nothing to do
  5551. if( buildin_delitem_search(sd, &it, false) )
  5552. {// success
  5553. return 0;
  5554. }
  5555. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5556. st->state = END;
  5557. clif_scriptclose(sd, st->oid);
  5558. return 1;
  5559. }
  5560. /// Deletes items from the target/attached player.
  5561. ///
  5562. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5563. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5564. BUILDIN_FUNC(delitem2)
  5565. {
  5566. TBL_PC *sd;
  5567. struct item it;
  5568. struct script_data *data;
  5569. if( script_hasdata(st,11) )
  5570. {
  5571. int account_id = script_getnum(st,11);
  5572. sd = map_id2sd(account_id); // <account id>
  5573. if( sd == NULL )
  5574. {
  5575. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5576. st->state = END;
  5577. return 1;
  5578. }
  5579. }
  5580. else
  5581. {
  5582. sd = script_rid2sd(st);// attached player
  5583. if( sd == NULL )
  5584. return 0;
  5585. }
  5586. data = script_getdata(st,2);
  5587. get_val(st,data);
  5588. if( data_isstring(data) )
  5589. {
  5590. const char* item_name = conv_str(st,data);
  5591. struct item_data* id = itemdb_searchname(item_name);
  5592. if( id == NULL )
  5593. {
  5594. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5595. st->state = END;
  5596. return 1;
  5597. }
  5598. it.nameid = id->nameid;// "<item name>"
  5599. }
  5600. else
  5601. {
  5602. it.nameid = conv_num(st,data);// <item id>
  5603. if( !itemdb_exists( it.nameid ) )
  5604. {
  5605. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5606. st->state = END;
  5607. return 1;
  5608. }
  5609. }
  5610. it.amount=script_getnum(st,3);
  5611. it.identify=script_getnum(st,4);
  5612. it.refine=script_getnum(st,5);
  5613. it.attribute=script_getnum(st,6);
  5614. it.card[0]=(short)script_getnum(st,7);
  5615. it.card[1]=(short)script_getnum(st,8);
  5616. it.card[2]=(short)script_getnum(st,9);
  5617. it.card[3]=(short)script_getnum(st,10);
  5618. if( it.amount <= 0 )
  5619. return 0;// nothing to do
  5620. if( buildin_delitem_search(sd, &it, true) )
  5621. {// success
  5622. return 0;
  5623. }
  5624. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5625. st->state = END;
  5626. clif_scriptclose(sd, st->oid);
  5627. return 1;
  5628. }
  5629. /*==========================================
  5630. * Enables/Disables use of items while in an NPC [Skotlex]
  5631. *------------------------------------------*/
  5632. BUILDIN_FUNC(enableitemuse)
  5633. {
  5634. TBL_PC *sd;
  5635. sd=script_rid2sd(st);
  5636. if (sd)
  5637. sd->npc_item_flag = st->oid;
  5638. return 0;
  5639. }
  5640. BUILDIN_FUNC(disableitemuse)
  5641. {
  5642. TBL_PC *sd;
  5643. sd=script_rid2sd(st);
  5644. if (sd)
  5645. sd->npc_item_flag = 0;
  5646. return 0;
  5647. }
  5648. /*==========================================
  5649. *キャラ関係のパラメータ取得
  5650. *------------------------------------------*/
  5651. BUILDIN_FUNC(readparam)
  5652. {
  5653. int type;
  5654. TBL_PC *sd;
  5655. type=script_getnum(st,2);
  5656. if( script_hasdata(st,3) )
  5657. sd=map_nick2sd(script_getstr(st,3));
  5658. else
  5659. sd=script_rid2sd(st);
  5660. if(sd==NULL){
  5661. script_pushint(st,-1);
  5662. return 0;
  5663. }
  5664. script_pushint(st,pc_readparam(sd,type));
  5665. return 0;
  5666. }
  5667. /*==========================================
  5668. *キャラ関係のID取得
  5669. *------------------------------------------*/
  5670. BUILDIN_FUNC(getcharid)
  5671. {
  5672. int num;
  5673. TBL_PC *sd;
  5674. num = script_getnum(st,2);
  5675. if( script_hasdata(st,3) )
  5676. sd=map_nick2sd(script_getstr(st,3));
  5677. else
  5678. sd=script_rid2sd(st);
  5679. if(sd==NULL){
  5680. script_pushint(st,0); //return 0, according docs
  5681. return 0;
  5682. }
  5683. switch( num ) {
  5684. case 0: script_pushint(st,sd->status.char_id); break;
  5685. case 1: script_pushint(st,sd->status.party_id); break;
  5686. case 2: script_pushint(st,sd->status.guild_id); break;
  5687. case 3: script_pushint(st,sd->status.account_id); break;
  5688. case 4: script_pushint(st,sd->bg_id); break;
  5689. default:
  5690. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5691. script_pushint(st,0);
  5692. break;
  5693. }
  5694. return 0;
  5695. }
  5696. /*==========================================
  5697. * returns the GID of an NPC
  5698. *------------------------------------------*/
  5699. BUILDIN_FUNC(getnpcid)
  5700. {
  5701. int num = script_getnum(st,2);
  5702. struct npc_data* nd = NULL;
  5703. if( script_hasdata(st,3) )
  5704. {// unique npc name
  5705. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  5706. {
  5707. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  5708. script_pushint(st,0);
  5709. return 1;
  5710. }
  5711. }
  5712. switch (num) {
  5713. case 0:
  5714. script_pushint(st,nd ? nd->bl.id : st->oid);
  5715. break;
  5716. default:
  5717. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  5718. script_pushint(st,0);
  5719. return 1;
  5720. }
  5721. return 0;
  5722. }
  5723. /*==========================================
  5724. *指定IDのPT名取得
  5725. *------------------------------------------*/
  5726. BUILDIN_FUNC(getpartyname)
  5727. {
  5728. int party_id;
  5729. struct party_data* p;
  5730. party_id = script_getnum(st,2);
  5731. if( ( p = party_search(party_id) ) != NULL )
  5732. {
  5733. script_pushstrcopy(st,p->party.name);
  5734. }
  5735. else
  5736. {
  5737. script_pushconststr(st,"null");
  5738. }
  5739. return 0;
  5740. }
  5741. /*==========================================
  5742. *指定IDのPT人数とメンバーID取得
  5743. *------------------------------------------*/
  5744. BUILDIN_FUNC(getpartymember)
  5745. {
  5746. struct party_data *p;
  5747. int i,j=0,type=0;
  5748. p=party_search(script_getnum(st,2));
  5749. if( script_hasdata(st,3) )
  5750. type=script_getnum(st,3);
  5751. if(p!=NULL){
  5752. for(i=0;i<MAX_PARTY;i++){
  5753. if(p->party.member[i].account_id){
  5754. switch (type) {
  5755. case 2:
  5756. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  5757. break;
  5758. case 1:
  5759. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  5760. break;
  5761. default:
  5762. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  5763. }
  5764. j++;
  5765. }
  5766. }
  5767. }
  5768. mapreg_setreg(add_str("$@partymembercount"),j);
  5769. return 0;
  5770. }
  5771. /*==========================================
  5772. * Retrieves party leader. if flag is specified,
  5773. * return some of the leader data. Otherwise, return name.
  5774. *------------------------------------------*/
  5775. BUILDIN_FUNC(getpartyleader)
  5776. {
  5777. int party_id, type = 0, i=0;
  5778. struct party_data *p;
  5779. party_id=script_getnum(st,2);
  5780. if( script_hasdata(st,3) )
  5781. type=script_getnum(st,3);
  5782. p=party_search(party_id);
  5783. if (p) //Search leader
  5784. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5785. if (!p || i == MAX_PARTY) { //leader not found
  5786. if (type)
  5787. script_pushint(st,-1);
  5788. else
  5789. script_pushconststr(st,"null");
  5790. return 0;
  5791. }
  5792. switch (type) {
  5793. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5794. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5795. case 3: script_pushint(st,p->party.member[i].class_); break;
  5796. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5797. case 5: script_pushint(st,p->party.member[i].lv); break;
  5798. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5799. }
  5800. return 0;
  5801. }
  5802. /*==========================================
  5803. *指定IDのギルド名取得
  5804. *------------------------------------------*/
  5805. BUILDIN_FUNC(getguildname)
  5806. {
  5807. int guild_id;
  5808. struct guild* g;
  5809. guild_id = script_getnum(st,2);
  5810. if( ( g = guild_search(guild_id) ) != NULL )
  5811. {
  5812. script_pushstrcopy(st,g->name);
  5813. }
  5814. else
  5815. {
  5816. script_pushconststr(st,"null");
  5817. }
  5818. return 0;
  5819. }
  5820. /*==========================================
  5821. *指定IDのGuildMaster名取得
  5822. *------------------------------------------*/
  5823. BUILDIN_FUNC(getguildmaster)
  5824. {
  5825. int guild_id;
  5826. struct guild* g;
  5827. guild_id = script_getnum(st,2);
  5828. if( ( g = guild_search(guild_id) ) != NULL )
  5829. {
  5830. script_pushstrcopy(st,g->member[0].name);
  5831. }
  5832. else
  5833. {
  5834. script_pushconststr(st,"null");
  5835. }
  5836. return 0;
  5837. }
  5838. BUILDIN_FUNC(getguildmasterid)
  5839. {
  5840. int guild_id;
  5841. struct guild* g;
  5842. guild_id = script_getnum(st,2);
  5843. if( ( g = guild_search(guild_id) ) != NULL )
  5844. {
  5845. script_pushint(st,g->member[0].char_id);
  5846. }
  5847. else
  5848. {
  5849. script_pushint(st,0);
  5850. }
  5851. return 0;
  5852. }
  5853. /*==========================================
  5854. * キャラクタの名前
  5855. *------------------------------------------*/
  5856. BUILDIN_FUNC(strcharinfo)
  5857. {
  5858. TBL_PC *sd;
  5859. int num;
  5860. struct guild* g;
  5861. struct party_data* p;
  5862. sd=script_rid2sd(st);
  5863. if (!sd) { //Avoid crashing....
  5864. script_pushconststr(st,"");
  5865. return 0;
  5866. }
  5867. num=script_getnum(st,2);
  5868. switch(num){
  5869. case 0:
  5870. script_pushstrcopy(st,sd->status.name);
  5871. break;
  5872. case 1:
  5873. if( ( p = party_search(sd->status.party_id) ) != NULL )
  5874. {
  5875. script_pushstrcopy(st,p->party.name);
  5876. }
  5877. else
  5878. {
  5879. script_pushconststr(st,"");
  5880. }
  5881. break;
  5882. case 2:
  5883. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  5884. {
  5885. script_pushstrcopy(st,g->name);
  5886. }
  5887. else
  5888. {
  5889. script_pushconststr(st,"");
  5890. }
  5891. break;
  5892. case 3:
  5893. script_pushconststr(st,map[sd->bl.m].name);
  5894. break;
  5895. default:
  5896. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5897. script_pushconststr(st,"");
  5898. break;
  5899. }
  5900. return 0;
  5901. }
  5902. /*==========================================
  5903. * 呼び出し元のNPC情報を取得する
  5904. *------------------------------------------*/
  5905. BUILDIN_FUNC(strnpcinfo)
  5906. {
  5907. TBL_NPC* nd;
  5908. int num;
  5909. char *buf,*name=NULL;
  5910. nd = map_id2nd(st->oid);
  5911. if (!nd) {
  5912. script_pushconststr(st, "");
  5913. return 0;
  5914. }
  5915. num = script_getnum(st,2);
  5916. switch(num){
  5917. case 0: // display name
  5918. name = aStrdup(nd->name);
  5919. break;
  5920. case 1: // visible part of display name
  5921. if((buf = strchr(nd->name,'#')) != NULL)
  5922. {
  5923. name = aStrdup(nd->name);
  5924. name[buf - nd->name] = 0;
  5925. } else // Return the name, there is no '#' present
  5926. name = aStrdup(nd->name);
  5927. break;
  5928. case 2: // # fragment
  5929. if((buf = strchr(nd->name,'#')) != NULL)
  5930. name = aStrdup(buf+1);
  5931. break;
  5932. case 3: // unique name
  5933. name = aStrdup(nd->exname);
  5934. break;
  5935. case 4: // map name
  5936. name = aStrdup(map[nd->bl.m].name);
  5937. break;
  5938. }
  5939. if(name)
  5940. script_pushstr(st, name);
  5941. else
  5942. script_pushconststr(st, "");
  5943. return 0;
  5944. }
  5945. // aegis->athena slot position conversion table
  5946. 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};
  5947. /*==========================================
  5948. * GetEquipID(Pos); Pos: 1-10
  5949. *------------------------------------------*/
  5950. BUILDIN_FUNC(getequipid)
  5951. {
  5952. int i, num;
  5953. TBL_PC* sd;
  5954. struct item_data* item;
  5955. sd = script_rid2sd(st);
  5956. if( sd == NULL )
  5957. return 0;
  5958. num = script_getnum(st,2) - 1;
  5959. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5960. {
  5961. script_pushint(st,-1);
  5962. return 0;
  5963. }
  5964. // get inventory position of item
  5965. i = pc_checkequip(sd,equip[num]);
  5966. if( i < 0 )
  5967. {
  5968. script_pushint(st,-1);
  5969. return 0;
  5970. }
  5971. item = sd->inventory_data[i];
  5972. if( item != 0 )
  5973. script_pushint(st,item->nameid);
  5974. else
  5975. script_pushint(st,0);
  5976. return 0;
  5977. }
  5978. /*==========================================
  5979. * 装備名文字列(精錬メニュー用)
  5980. *------------------------------------------*/
  5981. BUILDIN_FUNC(getequipname)
  5982. {
  5983. int i, num;
  5984. TBL_PC* sd;
  5985. struct item_data* item;
  5986. sd = script_rid2sd(st);
  5987. if( sd == NULL )
  5988. return 0;
  5989. num = script_getnum(st,2) - 1;
  5990. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5991. {
  5992. script_pushconststr(st,"");
  5993. return 0;
  5994. }
  5995. // get inventory position of item
  5996. i = pc_checkequip(sd,equip[num]);
  5997. if( i < 0 )
  5998. {
  5999. script_pushint(st,-1);
  6000. return 0;
  6001. }
  6002. item = sd->inventory_data[i];
  6003. if( item != 0 )
  6004. script_pushstrcopy(st,item->jname);
  6005. else
  6006. script_pushconststr(st,"");
  6007. return 0;
  6008. }
  6009. /*==========================================
  6010. * getbrokenid [Valaris]
  6011. *------------------------------------------*/
  6012. BUILDIN_FUNC(getbrokenid)
  6013. {
  6014. int i,num,id=0,brokencounter=0;
  6015. TBL_PC *sd;
  6016. sd = script_rid2sd(st);
  6017. if( sd == NULL )
  6018. return 0;
  6019. num=script_getnum(st,2);
  6020. for(i=0; i<MAX_INVENTORY; i++) {
  6021. if(sd->status.inventory[i].attribute){
  6022. brokencounter++;
  6023. if(num==brokencounter){
  6024. id=sd->status.inventory[i].nameid;
  6025. break;
  6026. }
  6027. }
  6028. }
  6029. script_pushint(st,id);
  6030. return 0;
  6031. }
  6032. /*==========================================
  6033. * repair [Valaris]
  6034. *------------------------------------------*/
  6035. BUILDIN_FUNC(repair)
  6036. {
  6037. int i,num;
  6038. int repaircounter=0;
  6039. TBL_PC *sd;
  6040. sd = script_rid2sd(st);
  6041. if( sd == NULL )
  6042. return 0;
  6043. num=script_getnum(st,2);
  6044. for(i=0; i<MAX_INVENTORY; i++) {
  6045. if(sd->status.inventory[i].attribute){
  6046. repaircounter++;
  6047. if(num==repaircounter){
  6048. sd->status.inventory[i].attribute=0;
  6049. clif_equiplist(sd);
  6050. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6051. clif_misceffect(&sd->bl, 3);
  6052. break;
  6053. }
  6054. }
  6055. }
  6056. return 0;
  6057. }
  6058. /*==========================================
  6059. * 装備チェック
  6060. *------------------------------------------*/
  6061. BUILDIN_FUNC(getequipisequiped)
  6062. {
  6063. int i=-1,num;
  6064. TBL_PC *sd;
  6065. num=script_getnum(st,2);
  6066. sd = script_rid2sd(st);
  6067. if( sd == NULL )
  6068. return 0;
  6069. if (num > 0 && num <= ARRAYLENGTH(equip))
  6070. i=pc_checkequip(sd,equip[num-1]);
  6071. if(i >= 0)
  6072. script_pushint(st,1);
  6073. else
  6074. script_pushint(st,0);
  6075. return 0;
  6076. }
  6077. /*==========================================
  6078. * 装備品精錬可能チェック
  6079. *------------------------------------------*/
  6080. BUILDIN_FUNC(getequipisenableref)
  6081. {
  6082. int i=-1,num;
  6083. TBL_PC *sd;
  6084. num=script_getnum(st,2);
  6085. sd = script_rid2sd(st);
  6086. if( sd == NULL )
  6087. return 0;
  6088. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6089. i = pc_checkequip(sd,equip[num-1]);
  6090. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6091. script_pushint(st,1);
  6092. else
  6093. script_pushint(st,0);
  6094. return 0;
  6095. }
  6096. /*==========================================
  6097. * 装備品鑑定チェック
  6098. *------------------------------------------*/
  6099. BUILDIN_FUNC(getequipisidentify)
  6100. {
  6101. int i=-1,num;
  6102. TBL_PC *sd;
  6103. num=script_getnum(st,2);
  6104. sd = script_rid2sd(st);
  6105. if( sd == NULL )
  6106. return 0;
  6107. if (num > 0 && num <= ARRAYLENGTH(equip))
  6108. i=pc_checkequip(sd,equip[num-1]);
  6109. if(i >= 0)
  6110. script_pushint(st,sd->status.inventory[i].identify);
  6111. else
  6112. script_pushint(st,0);
  6113. return 0;
  6114. }
  6115. /*==========================================
  6116. * 装備品精錬度
  6117. *------------------------------------------*/
  6118. BUILDIN_FUNC(getequiprefinerycnt)
  6119. {
  6120. int i=-1,num;
  6121. TBL_PC *sd;
  6122. num=script_getnum(st,2);
  6123. sd = script_rid2sd(st);
  6124. if( sd == NULL )
  6125. return 0;
  6126. if (num > 0 && num <= ARRAYLENGTH(equip))
  6127. i=pc_checkequip(sd,equip[num-1]);
  6128. if(i >= 0)
  6129. script_pushint(st,sd->status.inventory[i].refine);
  6130. else
  6131. script_pushint(st,0);
  6132. return 0;
  6133. }
  6134. /*==========================================
  6135. * 装備品武器LV
  6136. *------------------------------------------*/
  6137. BUILDIN_FUNC(getequipweaponlv)
  6138. {
  6139. int i=-1,num;
  6140. TBL_PC *sd;
  6141. num=script_getnum(st,2);
  6142. sd = script_rid2sd(st);
  6143. if( sd == NULL )
  6144. return 0;
  6145. if (num > 0 && num <= ARRAYLENGTH(equip))
  6146. i=pc_checkequip(sd,equip[num-1]);
  6147. if(i >= 0 && sd->inventory_data[i])
  6148. script_pushint(st,sd->inventory_data[i]->wlv);
  6149. else
  6150. script_pushint(st,0);
  6151. return 0;
  6152. }
  6153. /*==========================================
  6154. * 装備品精錬成功率
  6155. *------------------------------------------*/
  6156. BUILDIN_FUNC(getequippercentrefinery)
  6157. {
  6158. int i=-1,num;
  6159. TBL_PC *sd;
  6160. num=script_getnum(st,2);
  6161. sd = script_rid2sd(st);
  6162. if( sd == NULL )
  6163. return 0;
  6164. if (num > 0 && num <= ARRAYLENGTH(equip))
  6165. i=pc_checkequip(sd,equip[num-1]);
  6166. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6167. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6168. else
  6169. script_pushint(st,0);
  6170. return 0;
  6171. }
  6172. /*==========================================
  6173. * 精錬成功
  6174. *------------------------------------------*/
  6175. BUILDIN_FUNC(successrefitem)
  6176. {
  6177. int i=-1,num,ep;
  6178. TBL_PC *sd;
  6179. num=script_getnum(st,2);
  6180. sd = script_rid2sd(st);
  6181. if( sd == NULL )
  6182. return 0;
  6183. if (num > 0 && num <= ARRAYLENGTH(equip))
  6184. i=pc_checkequip(sd,equip[num-1]);
  6185. if(i >= 0) {
  6186. ep=sd->status.inventory[i].equip;
  6187. //Logs items, got from (N)PC scripts [Lupus]
  6188. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6189. sd->status.inventory[i].refine++;
  6190. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6191. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6192. clif_delitem(sd,i,1,3);
  6193. //Logs items, got from (N)PC scripts [Lupus]
  6194. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6195. clif_additem(sd,i,1,0);
  6196. pc_equipitem(sd,i,ep);
  6197. clif_misceffect(&sd->bl,3);
  6198. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6199. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6200. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6201. ){ // Fame point system [DracoRPG]
  6202. switch (sd->inventory_data[i]->wlv){
  6203. case 1:
  6204. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6205. break;
  6206. case 2:
  6207. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6208. break;
  6209. case 3:
  6210. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6211. break;
  6212. }
  6213. }
  6214. }
  6215. return 0;
  6216. }
  6217. /*==========================================
  6218. * 精錬失敗
  6219. *------------------------------------------*/
  6220. BUILDIN_FUNC(failedrefitem)
  6221. {
  6222. int i=-1,num;
  6223. TBL_PC *sd;
  6224. num=script_getnum(st,2);
  6225. sd = script_rid2sd(st);
  6226. if( sd == NULL )
  6227. return 0;
  6228. if (num > 0 && num <= ARRAYLENGTH(equip))
  6229. i=pc_checkequip(sd,equip[num-1]);
  6230. if(i >= 0) {
  6231. sd->status.inventory[i].refine = 0;
  6232. pc_unequipitem(sd,i,3);
  6233. // 精錬失敗エフェクトのパケット
  6234. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6235. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6236. // 他の人にも失敗を通知
  6237. clif_misceffect(&sd->bl,2);
  6238. }
  6239. return 0;
  6240. }
  6241. /*==========================================
  6242. *
  6243. *------------------------------------------*/
  6244. BUILDIN_FUNC(statusup)
  6245. {
  6246. int type;
  6247. TBL_PC *sd;
  6248. type=script_getnum(st,2);
  6249. sd = script_rid2sd(st);
  6250. if( sd == NULL )
  6251. return 0;
  6252. pc_statusup(sd,type);
  6253. return 0;
  6254. }
  6255. /*==========================================
  6256. *
  6257. *------------------------------------------*/
  6258. BUILDIN_FUNC(statusup2)
  6259. {
  6260. int type,val;
  6261. TBL_PC *sd;
  6262. type=script_getnum(st,2);
  6263. val=script_getnum(st,3);
  6264. sd = script_rid2sd(st);
  6265. if( sd == NULL )
  6266. return 0;
  6267. pc_statusup2(sd,type,val);
  6268. return 0;
  6269. }
  6270. /// See 'doc/item_bonus.txt'
  6271. ///
  6272. /// bonus <bonus type>,<val1>;
  6273. /// bonus2 <bonus type>,<val1>,<val2>;
  6274. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6275. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6276. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6277. BUILDIN_FUNC(bonus)
  6278. {
  6279. int type;
  6280. int val1;
  6281. int val2 = 0;
  6282. int val3 = 0;
  6283. int val4 = 0;
  6284. int val5 = 0;
  6285. TBL_PC* sd;
  6286. sd = script_rid2sd(st);
  6287. if( sd == NULL )
  6288. return 0; // no player attached
  6289. type = script_getnum(st,2);
  6290. switch( type )
  6291. {
  6292. case SP_AUTOSPELL:
  6293. case SP_AUTOSPELL_WHENHIT:
  6294. case SP_AUTOSPELL_ONSKILL:
  6295. case SP_SKILL_ATK:
  6296. case SP_SKILL_HEAL:
  6297. case SP_SKILL_HEAL2:
  6298. case SP_ADD_SKILL_BLOW:
  6299. case SP_CASTRATE:
  6300. case SP_ADDEFF_ONSKILL:
  6301. // these bonuses support skill names
  6302. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6303. break;
  6304. default:
  6305. val1 = script_getnum(st,3);
  6306. break;
  6307. }
  6308. switch( script_lastdata(st)-2 )
  6309. {
  6310. case 1:
  6311. pc_bonus(sd, type, val1);
  6312. break;
  6313. case 2:
  6314. val2 = script_getnum(st,4);
  6315. pc_bonus2(sd, type, val1, val2);
  6316. break;
  6317. case 3:
  6318. val2 = script_getnum(st,4);
  6319. val3 = script_getnum(st,5);
  6320. pc_bonus3(sd, type, val1, val2, val3);
  6321. break;
  6322. case 4:
  6323. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6324. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6325. else
  6326. val2 = script_getnum(st,4);
  6327. val3 = script_getnum(st,5);
  6328. val4 = script_getnum(st,6);
  6329. pc_bonus4(sd, type, val1, val2, val3, val4);
  6330. break;
  6331. case 5:
  6332. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6333. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6334. else
  6335. val2 = script_getnum(st,4);
  6336. val3 = script_getnum(st,5);
  6337. val4 = script_getnum(st,6);
  6338. val5 = script_getnum(st,7);
  6339. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6340. break;
  6341. default:
  6342. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6343. break;
  6344. }
  6345. return 0;
  6346. }
  6347. BUILDIN_FUNC(autobonus)
  6348. {
  6349. unsigned int dur;
  6350. short rate;
  6351. short atk_type = 0;
  6352. TBL_PC* sd;
  6353. const char *bonus_script, *other_script = NULL;
  6354. sd = script_rid2sd(st);
  6355. if( sd == NULL )
  6356. return 0; // no player attached
  6357. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6358. return 0;
  6359. rate = script_getnum(st,3);
  6360. dur = script_getnum(st,4);
  6361. bonus_script = script_getstr(st,2);
  6362. if( !rate || !dur || !bonus_script )
  6363. return 0;
  6364. if( script_hasdata(st,5) )
  6365. atk_type = script_getnum(st,5);
  6366. if( script_hasdata(st,6) )
  6367. other_script = script_getstr(st,6);
  6368. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6369. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6370. {
  6371. script_add_autobonus(bonus_script);
  6372. if( other_script )
  6373. script_add_autobonus(other_script);
  6374. }
  6375. return 0;
  6376. }
  6377. BUILDIN_FUNC(autobonus2)
  6378. {
  6379. unsigned int dur;
  6380. short rate;
  6381. short atk_type = 0;
  6382. TBL_PC* sd;
  6383. const char *bonus_script, *other_script = NULL;
  6384. sd = script_rid2sd(st);
  6385. if( sd == NULL )
  6386. return 0; // no player attached
  6387. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6388. return 0;
  6389. rate = script_getnum(st,3);
  6390. dur = script_getnum(st,4);
  6391. bonus_script = script_getstr(st,2);
  6392. if( !rate || !dur || !bonus_script )
  6393. return 0;
  6394. if( script_hasdata(st,5) )
  6395. atk_type = script_getnum(st,5);
  6396. if( script_hasdata(st,6) )
  6397. other_script = script_getstr(st,6);
  6398. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6399. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6400. {
  6401. script_add_autobonus(bonus_script);
  6402. if( other_script )
  6403. script_add_autobonus(other_script);
  6404. }
  6405. return 0;
  6406. }
  6407. BUILDIN_FUNC(autobonus3)
  6408. {
  6409. unsigned int dur;
  6410. short rate,atk_type;
  6411. TBL_PC* sd;
  6412. const char *bonus_script, *other_script = NULL;
  6413. sd = script_rid2sd(st);
  6414. if( sd == NULL )
  6415. return 0; // no player attached
  6416. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6417. return 0;
  6418. rate = script_getnum(st,3);
  6419. dur = script_getnum(st,4);
  6420. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6421. bonus_script = script_getstr(st,2);
  6422. if( !rate || !dur || !atk_type || !bonus_script )
  6423. return 0;
  6424. if( script_hasdata(st,6) )
  6425. other_script = script_getstr(st,6);
  6426. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6427. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6428. {
  6429. script_add_autobonus(bonus_script);
  6430. if( other_script )
  6431. script_add_autobonus(other_script);
  6432. }
  6433. return 0;
  6434. }
  6435. /// Changes the level of a player skill.
  6436. /// <flag> defaults to 1
  6437. /// <flag>=0 : set the level of the skill
  6438. /// <flag>=1 : set the temporary level of the skill
  6439. /// <flag>=2 : add to the level of the skill
  6440. ///
  6441. /// skill <skill id>,<level>,<flag>
  6442. /// skill <skill id>,<level>
  6443. /// skill "<skill name>",<level>,<flag>
  6444. /// skill "<skill name>",<level>
  6445. BUILDIN_FUNC(skill)
  6446. {
  6447. int id;
  6448. int level;
  6449. int flag = 1;
  6450. TBL_PC* sd;
  6451. sd = script_rid2sd(st);
  6452. if( sd == NULL )
  6453. return 0;// no player attached, report source
  6454. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6455. level = script_getnum(st,3);
  6456. if( script_hasdata(st,4) )
  6457. flag = script_getnum(st,4);
  6458. pc_skill(sd, id, level, flag);
  6459. return 0;
  6460. }
  6461. /// Changes the level of a player skill.
  6462. /// like skill, but <flag> defaults to 2
  6463. ///
  6464. /// addtoskill <skill id>,<amount>,<flag>
  6465. /// addtoskill <skill id>,<amount>
  6466. /// addtoskill "<skill name>",<amount>,<flag>
  6467. /// addtoskill "<skill name>",<amount>
  6468. ///
  6469. /// @see skill
  6470. BUILDIN_FUNC(addtoskill)
  6471. {
  6472. int id;
  6473. int level;
  6474. int flag = 2;
  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. /// Increases the level of a guild skill.
  6487. ///
  6488. /// guildskill <skill id>,<amount>;
  6489. /// guildskill "<skill name>",<amount>;
  6490. BUILDIN_FUNC(guildskill)
  6491. {
  6492. int id;
  6493. int level;
  6494. TBL_PC* sd;
  6495. int i;
  6496. sd = script_rid2sd(st);
  6497. if( sd == NULL )
  6498. return 0;// no player attached, report source
  6499. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6500. level = script_getnum(st,3);
  6501. for( i=0; i < level; i++ )
  6502. guild_skillup(sd, id);
  6503. return 0;
  6504. }
  6505. /// Returns the level of the player skill.
  6506. ///
  6507. /// getskilllv(<skill id>) -> <level>
  6508. /// getskilllv("<skill name>") -> <level>
  6509. BUILDIN_FUNC(getskilllv)
  6510. {
  6511. int id;
  6512. TBL_PC* sd;
  6513. sd = script_rid2sd(st);
  6514. if( sd == NULL )
  6515. return 0;// no player attached, report source
  6516. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6517. script_pushint(st, pc_checkskill(sd,id));
  6518. return 0;
  6519. }
  6520. /// Returns the level of the guild skill.
  6521. ///
  6522. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6523. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6524. BUILDIN_FUNC(getgdskilllv)
  6525. {
  6526. int guild_id;
  6527. int skill_id;
  6528. struct guild* g;
  6529. guild_id = script_getnum(st,2);
  6530. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6531. g = guild_search(guild_id);
  6532. if( g == NULL )
  6533. script_pushint(st, -1);
  6534. else
  6535. script_pushint(st, guild_checkskill(g,skill_id));
  6536. return 0;
  6537. }
  6538. /// Returns the 'basic_skill_check' setting.
  6539. /// This config determines if the server checks the skill level of NV_BASIC
  6540. /// before allowing the basic actions.
  6541. ///
  6542. /// basicskillcheck() -> <bool>
  6543. BUILDIN_FUNC(basicskillcheck)
  6544. {
  6545. script_pushint(st, battle_config.basic_skill_check);
  6546. return 0;
  6547. }
  6548. /// Returns the GM level of the player.
  6549. ///
  6550. /// getgmlevel() -> <level>
  6551. BUILDIN_FUNC(getgmlevel)
  6552. {
  6553. TBL_PC* sd;
  6554. sd = script_rid2sd(st);
  6555. if( sd == NULL )
  6556. return 0;// no player attached, report source
  6557. script_pushint(st, pc_get_group_level(sd));
  6558. return 0;
  6559. }
  6560. /// Returns the group ID of the player.
  6561. ///
  6562. /// getgroupid() -> <int>
  6563. BUILDIN_FUNC(getgroupid)
  6564. {
  6565. TBL_PC* sd;
  6566. sd = script_rid2sd(st);
  6567. if (sd == NULL)
  6568. return 1; // no player attached, report source
  6569. script_pushint(st, pc_get_group_id(sd));
  6570. return 0;
  6571. }
  6572. /// Terminates the execution of this script instance.
  6573. ///
  6574. /// end
  6575. BUILDIN_FUNC(end)
  6576. {
  6577. st->state = END;
  6578. return 0;
  6579. }
  6580. /// Checks if the player has that effect state (option).
  6581. ///
  6582. /// checkoption(<option>) -> <bool>
  6583. BUILDIN_FUNC(checkoption)
  6584. {
  6585. int option;
  6586. TBL_PC* sd;
  6587. sd = script_rid2sd(st);
  6588. if( sd == NULL )
  6589. return 0;// no player attached, report source
  6590. option = script_getnum(st,2);
  6591. if( sd->sc.option&option )
  6592. script_pushint(st, 1);
  6593. else
  6594. script_pushint(st, 0);
  6595. return 0;
  6596. }
  6597. /// Checks if the player is in that body state (opt1).
  6598. ///
  6599. /// checkoption1(<opt1>) -> <bool>
  6600. BUILDIN_FUNC(checkoption1)
  6601. {
  6602. int opt1;
  6603. TBL_PC* sd;
  6604. sd = script_rid2sd(st);
  6605. if( sd == NULL )
  6606. return 0;// no player attached, report source
  6607. opt1 = script_getnum(st,2);
  6608. if( sd->sc.opt1 == opt1 )
  6609. script_pushint(st, 1);
  6610. else
  6611. script_pushint(st, 0);
  6612. return 0;
  6613. }
  6614. /// Checks if the player has that health state (opt2).
  6615. ///
  6616. /// checkoption2(<opt2>) -> <bool>
  6617. BUILDIN_FUNC(checkoption2)
  6618. {
  6619. int opt2;
  6620. TBL_PC* sd;
  6621. sd = script_rid2sd(st);
  6622. if( sd == NULL )
  6623. return 0;// no player attached, report source
  6624. opt2 = script_getnum(st,2);
  6625. if( sd->sc.opt2&opt2 )
  6626. script_pushint(st, 1);
  6627. else
  6628. script_pushint(st, 0);
  6629. return 0;
  6630. }
  6631. /// Changes the effect state (option) of the player.
  6632. /// <flag> defaults to 1
  6633. /// <flag>=0 : removes the option
  6634. /// <flag>=other : adds the option
  6635. ///
  6636. /// setoption <option>,<flag>;
  6637. /// setoption <option>;
  6638. BUILDIN_FUNC(setoption)
  6639. {
  6640. int option;
  6641. int flag = 1;
  6642. TBL_PC* sd;
  6643. sd = script_rid2sd(st);
  6644. if( sd == NULL )
  6645. return 0;// no player attached, report source
  6646. option = script_getnum(st,2);
  6647. if( script_hasdata(st,3) )
  6648. flag = script_getnum(st,3);
  6649. else if( !option ){// Request to remove everything.
  6650. flag = 0;
  6651. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6652. }
  6653. if( flag ){// Add option
  6654. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6655. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6656. else if ( option&OPTION_MOUNTING && pc_cant_newmount(sd) )
  6657. option &= ~OPTION_MOUNTING;
  6658. pc_setoption(sd, sd->sc.option|option);
  6659. } else// Remove option
  6660. pc_setoption(sd, sd->sc.option&~option);
  6661. return 0;
  6662. }
  6663. /// Returns if the player has a cart.
  6664. ///
  6665. /// checkcart() -> <bool>
  6666. ///
  6667. /// @author Valaris
  6668. BUILDIN_FUNC(checkcart)
  6669. {
  6670. TBL_PC* sd;
  6671. sd = script_rid2sd(st);
  6672. if( sd == NULL )
  6673. return 0;// no player attached, report source
  6674. if( pc_iscarton(sd) )
  6675. script_pushint(st, 1);
  6676. else
  6677. script_pushint(st, 0);
  6678. return 0;
  6679. }
  6680. /// Sets the cart of the player.
  6681. /// <type> defaults to 1
  6682. /// <type>=0 : removes the cart
  6683. /// <type>=1 : Normal cart
  6684. /// <type>=2 : Wooden cart
  6685. /// <type>=3 : Covered cart with flowers and ferns
  6686. /// <type>=4 : Wooden cart with a Panda doll on the back
  6687. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6688. ///
  6689. /// setcart <type>;
  6690. /// setcart;
  6691. BUILDIN_FUNC(setcart)
  6692. {
  6693. int type = 1;
  6694. TBL_PC* sd;
  6695. sd = script_rid2sd(st);
  6696. if( sd == NULL )
  6697. return 0;// no player attached, report source
  6698. if( script_hasdata(st,2) )
  6699. type = script_getnum(st,2);
  6700. pc_setcart(sd, type);
  6701. return 0;
  6702. }
  6703. /// Returns if the player has a falcon.
  6704. ///
  6705. /// checkfalcon() -> <bool>
  6706. ///
  6707. /// @author Valaris
  6708. BUILDIN_FUNC(checkfalcon)
  6709. {
  6710. TBL_PC* sd;
  6711. sd = script_rid2sd(st);
  6712. if( sd == NULL )
  6713. return 0;// no player attached, report source
  6714. if( pc_isfalcon(sd) )
  6715. script_pushint(st, 1);
  6716. else
  6717. script_pushint(st, 0);
  6718. return 0;
  6719. }
  6720. /// Sets if the player has a falcon or not.
  6721. /// <flag> defaults to 1
  6722. ///
  6723. /// setfalcon <flag>;
  6724. /// setfalcon;
  6725. BUILDIN_FUNC(setfalcon)
  6726. {
  6727. int flag = 1;
  6728. TBL_PC* sd;
  6729. sd = script_rid2sd(st);
  6730. if( sd == NULL )
  6731. return 0;// no player attached, report source
  6732. if( script_hasdata(st,2) )
  6733. flag = script_getnum(st,2);
  6734. pc_setfalcon(sd, flag);
  6735. return 0;
  6736. }
  6737. /// Returns if the player is riding.
  6738. ///
  6739. /// checkriding() -> <bool>
  6740. ///
  6741. /// @author Valaris
  6742. BUILDIN_FUNC(checkriding)
  6743. {
  6744. TBL_PC* sd;
  6745. sd = script_rid2sd(st);
  6746. if( sd == NULL )
  6747. return 0;// no player attached, report source
  6748. if( pc_isriding(sd) || sd->sc.option&OPTION_MOUNTING )
  6749. script_pushint(st, 1);
  6750. else
  6751. script_pushint(st, 0);
  6752. return 0;
  6753. }
  6754. /// Sets if the player is riding.
  6755. /// <flag> defaults to 1
  6756. ///
  6757. /// setriding <flag>;
  6758. /// setriding;
  6759. BUILDIN_FUNC(setriding)
  6760. {
  6761. int flag = 1;
  6762. TBL_PC* sd;
  6763. sd = script_rid2sd(st);
  6764. if( sd == NULL )
  6765. return 0;// no player attached, report source
  6766. if( script_hasdata(st,2) )
  6767. flag = script_getnum(st,2);
  6768. pc_setriding(sd, flag);
  6769. return 0;
  6770. }
  6771. /// Sets the save point of the player.
  6772. ///
  6773. /// save "<map name>",<x>,<y>
  6774. /// savepoint "<map name>",<x>,<y>
  6775. BUILDIN_FUNC(savepoint)
  6776. {
  6777. int x;
  6778. int y;
  6779. short map;
  6780. const char* str;
  6781. TBL_PC* sd;
  6782. sd = script_rid2sd(st);
  6783. if( sd == NULL )
  6784. return 0;// no player attached, report source
  6785. str = script_getstr(st, 2);
  6786. x = script_getnum(st,3);
  6787. y = script_getnum(st,4);
  6788. map = mapindex_name2id(str);
  6789. if( map )
  6790. pc_setsavepoint(sd, map, x, y);
  6791. return 0;
  6792. }
  6793. /*==========================================
  6794. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6795. *------------------------------------------*/
  6796. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6797. {
  6798. int type;
  6799. time_t timer;
  6800. struct tm *t;
  6801. type=script_getnum(st,2);
  6802. switch(type){
  6803. case 2:
  6804. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6805. // from the system clock.)
  6806. script_pushint(st,(int)time(NULL));
  6807. break;
  6808. case 1:
  6809. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6810. time(&timer);
  6811. t=localtime(&timer);
  6812. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6813. break;
  6814. case 0:
  6815. default:
  6816. //type 0:(System Ticks)
  6817. script_pushint(st,gettick());
  6818. break;
  6819. }
  6820. return 0;
  6821. }
  6822. /*==========================================
  6823. * GetTime(Type);
  6824. * 1: Sec 2: Min 3: Hour
  6825. * 4: WeekDay 5: MonthDay 6: Month
  6826. * 7: Year
  6827. *------------------------------------------*/
  6828. BUILDIN_FUNC(gettime) /* Asgard Version */
  6829. {
  6830. int type;
  6831. time_t timer;
  6832. struct tm *t;
  6833. type=script_getnum(st,2);
  6834. time(&timer);
  6835. t=localtime(&timer);
  6836. switch(type){
  6837. case 1://Sec(0~59)
  6838. script_pushint(st,t->tm_sec);
  6839. break;
  6840. case 2://Min(0~59)
  6841. script_pushint(st,t->tm_min);
  6842. break;
  6843. case 3://Hour(0~23)
  6844. script_pushint(st,t->tm_hour);
  6845. break;
  6846. case 4://WeekDay(0~6)
  6847. script_pushint(st,t->tm_wday);
  6848. break;
  6849. case 5://MonthDay(01~31)
  6850. script_pushint(st,t->tm_mday);
  6851. break;
  6852. case 6://Month(01~12)
  6853. script_pushint(st,t->tm_mon+1);
  6854. break;
  6855. case 7://Year(20xx)
  6856. script_pushint(st,t->tm_year+1900);
  6857. break;
  6858. case 8://Year Day(01~366)
  6859. script_pushint(st,t->tm_yday+1);
  6860. break;
  6861. default://(format error)
  6862. script_pushint(st,-1);
  6863. break;
  6864. }
  6865. return 0;
  6866. }
  6867. /*==========================================
  6868. * GetTimeStr("TimeFMT", Length);
  6869. *------------------------------------------*/
  6870. BUILDIN_FUNC(gettimestr)
  6871. {
  6872. char *tmpstr;
  6873. const char *fmtstr;
  6874. int maxlen;
  6875. time_t now = time(NULL);
  6876. fmtstr=script_getstr(st,2);
  6877. maxlen=script_getnum(st,3);
  6878. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  6879. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6880. tmpstr[maxlen]='\0';
  6881. script_pushstr(st,tmpstr);
  6882. return 0;
  6883. }
  6884. /*==========================================
  6885. * カプラ倉庫を開く
  6886. *------------------------------------------*/
  6887. BUILDIN_FUNC(openstorage)
  6888. {
  6889. TBL_PC* sd;
  6890. sd = script_rid2sd(st);
  6891. if( sd == NULL )
  6892. return 0;
  6893. storage_storageopen(sd);
  6894. return 0;
  6895. }
  6896. BUILDIN_FUNC(guildopenstorage)
  6897. {
  6898. TBL_PC* sd;
  6899. int ret;
  6900. sd = script_rid2sd(st);
  6901. if( sd == NULL )
  6902. return 0;
  6903. ret = storage_guild_storageopen(sd);
  6904. script_pushint(st,ret);
  6905. return 0;
  6906. }
  6907. /*==========================================
  6908. * アイテムによるスキル発動
  6909. *------------------------------------------*/
  6910. /// itemskill <skill id>,<level>
  6911. /// itemskill "<skill name>",<level>
  6912. BUILDIN_FUNC(itemskill)
  6913. {
  6914. int id;
  6915. int lv;
  6916. TBL_PC* sd;
  6917. sd = script_rid2sd(st);
  6918. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6919. return 0;
  6920. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6921. lv = script_getnum(st,3);
  6922. sd->skillitem=id;
  6923. sd->skillitemlv=lv;
  6924. clif_item_skill(sd,id,lv);
  6925. return 0;
  6926. }
  6927. /*==========================================
  6928. * アイテム作成
  6929. *------------------------------------------*/
  6930. BUILDIN_FUNC(produce)
  6931. {
  6932. int trigger;
  6933. TBL_PC* sd;
  6934. sd = script_rid2sd(st);
  6935. if( sd == NULL )
  6936. return 0;
  6937. trigger=script_getnum(st,2);
  6938. clif_skill_produce_mix_list(sd, -1, trigger);
  6939. return 0;
  6940. }
  6941. /*==========================================
  6942. *
  6943. *------------------------------------------*/
  6944. BUILDIN_FUNC(cooking)
  6945. {
  6946. int trigger;
  6947. TBL_PC* sd;
  6948. sd = script_rid2sd(st);
  6949. if( sd == NULL )
  6950. return 0;
  6951. trigger=script_getnum(st,2);
  6952. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  6953. return 0;
  6954. }
  6955. /*==========================================
  6956. * NPCでペット作る
  6957. *------------------------------------------*/
  6958. BUILDIN_FUNC(makepet)
  6959. {
  6960. TBL_PC* sd;
  6961. int id,pet_id;
  6962. id=script_getnum(st,2);
  6963. sd = script_rid2sd(st);
  6964. if( sd == NULL )
  6965. return 0;
  6966. pet_id = search_petDB_index(id, PET_CLASS);
  6967. if (pet_id < 0)
  6968. pet_id = search_petDB_index(id, PET_EGG);
  6969. if (pet_id >= 0 && sd) {
  6970. sd->catch_target_class = pet_db[pet_id].class_;
  6971. intif_create_pet(
  6972. sd->status.account_id, sd->status.char_id,
  6973. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6974. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6975. 100, 0, 1, pet_db[pet_id].jname);
  6976. }
  6977. return 0;
  6978. }
  6979. /*==========================================
  6980. * NPCで経験値上げる
  6981. *------------------------------------------*/
  6982. BUILDIN_FUNC(getexp)
  6983. {
  6984. TBL_PC* sd;
  6985. int base=0,job=0;
  6986. double bonus;
  6987. sd = script_rid2sd(st);
  6988. if( sd == NULL )
  6989. return 0;
  6990. base=script_getnum(st,2);
  6991. job =script_getnum(st,3);
  6992. if(base<0 || job<0)
  6993. return 0;
  6994. // bonus for npc-given exp
  6995. bonus = battle_config.quest_exp_rate / 100.;
  6996. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6997. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6998. pc_gainexp(sd, NULL, base, job, true);
  6999. return 0;
  7000. }
  7001. /*==========================================
  7002. * Gain guild exp [Celest]
  7003. *------------------------------------------*/
  7004. BUILDIN_FUNC(guildgetexp)
  7005. {
  7006. TBL_PC* sd;
  7007. int exp;
  7008. sd = script_rid2sd(st);
  7009. if( sd == NULL )
  7010. return 0;
  7011. exp = script_getnum(st,2);
  7012. if(exp < 0)
  7013. return 0;
  7014. if(sd && sd->status.guild_id > 0)
  7015. guild_getexp (sd, exp);
  7016. return 0;
  7017. }
  7018. /*==========================================
  7019. * Changes the guild master of a guild [Skotlex]
  7020. *------------------------------------------*/
  7021. BUILDIN_FUNC(guildchangegm)
  7022. {
  7023. TBL_PC *sd;
  7024. int guild_id;
  7025. const char *name;
  7026. guild_id = script_getnum(st,2);
  7027. name = script_getstr(st,3);
  7028. sd=map_nick2sd(name);
  7029. if (!sd)
  7030. script_pushint(st,0);
  7031. else
  7032. script_pushint(st,guild_gm_change(guild_id, sd));
  7033. return 0;
  7034. }
  7035. /*==========================================
  7036. * モンスター発生
  7037. *------------------------------------------*/
  7038. BUILDIN_FUNC(monster)
  7039. {
  7040. const char* mapn = script_getstr(st,2);
  7041. int x = script_getnum(st,3);
  7042. int y = script_getnum(st,4);
  7043. const char* str = script_getstr(st,5);
  7044. int class_ = script_getnum(st,6);
  7045. int amount = script_getnum(st,7);
  7046. const char* event = "";
  7047. struct map_session_data* sd;
  7048. int m;
  7049. if( script_hasdata(st,8) )
  7050. {
  7051. event = script_getstr(st,8);
  7052. check_event(st, event);
  7053. }
  7054. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7055. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7056. return 1;
  7057. }
  7058. sd = map_id2sd(st->rid);
  7059. if( sd && strcmp(mapn,"this") == 0 )
  7060. m = sd->bl.m;
  7061. else
  7062. {
  7063. m = map_mapname2mapid(mapn);
  7064. if( map[m].flag.src4instance && st->instance_id )
  7065. { // Try to redirect to the instance map, not the src map
  7066. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7067. {
  7068. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7069. return 1;
  7070. }
  7071. }
  7072. }
  7073. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  7074. return 0;
  7075. }
  7076. /*==========================================
  7077. * Request List of Monster Drops
  7078. *------------------------------------------*/
  7079. BUILDIN_FUNC(getmobdrops)
  7080. {
  7081. int class_ = script_getnum(st,2);
  7082. int i, j = 0;
  7083. struct mob_db *mob;
  7084. if( !mobdb_checkid(class_) )
  7085. {
  7086. script_pushint(st, 0);
  7087. return 0;
  7088. }
  7089. mob = mob_db(class_);
  7090. for( i = 0; i < MAX_MOB_DROP; i++ )
  7091. {
  7092. if( mob->dropitem[i].nameid < 1 )
  7093. continue;
  7094. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7095. continue;
  7096. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7097. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7098. j++;
  7099. }
  7100. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7101. script_pushint(st, 1);
  7102. return 0;
  7103. }
  7104. /*==========================================
  7105. * モンスター発生
  7106. *------------------------------------------*/
  7107. BUILDIN_FUNC(areamonster)
  7108. {
  7109. const char* mapn = script_getstr(st,2);
  7110. int x0 = script_getnum(st,3);
  7111. int y0 = script_getnum(st,4);
  7112. int x1 = script_getnum(st,5);
  7113. int y1 = script_getnum(st,6);
  7114. const char* str = script_getstr(st,7);
  7115. int class_ = script_getnum(st,8);
  7116. int amount = script_getnum(st,9);
  7117. const char* event = "";
  7118. struct map_session_data* sd;
  7119. int m;
  7120. if( script_hasdata(st,10) )
  7121. {
  7122. event = script_getstr(st,10);
  7123. check_event(st, event);
  7124. }
  7125. sd = map_id2sd(st->rid);
  7126. if( sd && strcmp(mapn,"this") == 0 )
  7127. m = sd->bl.m;
  7128. else
  7129. {
  7130. m = map_mapname2mapid(mapn);
  7131. if( map[m].flag.src4instance && st->instance_id )
  7132. { // Try to redirect to the instance map, not the src map
  7133. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7134. {
  7135. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7136. return 1;
  7137. }
  7138. }
  7139. }
  7140. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  7141. return 0;
  7142. }
  7143. /*==========================================
  7144. * モンスター削除
  7145. *------------------------------------------*/
  7146. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7147. { //same fix but with killmonster instead - stripping events from mobs.
  7148. TBL_MOB* md = (TBL_MOB*)bl;
  7149. char *event=va_arg(ap,char *);
  7150. int allflag=va_arg(ap,int);
  7151. md->state.npc_killmonster = 1;
  7152. if(!allflag){
  7153. if(strcmp(event,md->npc_event)==0)
  7154. status_kill(bl);
  7155. }else{
  7156. if(!md->spawn)
  7157. status_kill(bl);
  7158. }
  7159. md->state.npc_killmonster = 0;
  7160. return 0;
  7161. }
  7162. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7163. {
  7164. TBL_MOB* md = (TBL_MOB*)bl;
  7165. char *event=va_arg(ap,char *);
  7166. int allflag=va_arg(ap,int);
  7167. if(!allflag){
  7168. if(strcmp(event,md->npc_event)==0)
  7169. status_kill(bl);
  7170. }else{
  7171. if(!md->spawn)
  7172. status_kill(bl);
  7173. }
  7174. return 0;
  7175. }
  7176. BUILDIN_FUNC(killmonster)
  7177. {
  7178. const char *mapname,*event;
  7179. int m,allflag=0;
  7180. mapname=script_getstr(st,2);
  7181. event=script_getstr(st,3);
  7182. if(strcmp(event,"All")==0)
  7183. allflag = 1;
  7184. else
  7185. check_event(st, event);
  7186. if( (m=map_mapname2mapid(mapname))<0 )
  7187. return 0;
  7188. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7189. return 0;
  7190. if( script_hasdata(st,4) ) {
  7191. if ( script_getnum(st,4) == 1 ) {
  7192. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7193. return 0;
  7194. }
  7195. }
  7196. map_freeblock_lock();
  7197. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7198. map_freeblock_unlock();
  7199. return 0;
  7200. }
  7201. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7202. { //Strips the event from the mob if it's killed the old method.
  7203. struct mob_data *md;
  7204. md = BL_CAST(BL_MOB, bl);
  7205. if (md->npc_event[0])
  7206. md->npc_event[0] = 0;
  7207. status_kill(bl);
  7208. return 0;
  7209. }
  7210. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7211. {
  7212. status_kill(bl);
  7213. return 0;
  7214. }
  7215. BUILDIN_FUNC(killmonsterall)
  7216. {
  7217. const char *mapname;
  7218. int m;
  7219. mapname=script_getstr(st,2);
  7220. if( (m = map_mapname2mapid(mapname))<0 )
  7221. return 0;
  7222. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7223. return 0;
  7224. if( script_hasdata(st,3) ) {
  7225. if ( script_getnum(st,3) == 1 ) {
  7226. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7227. return 0;
  7228. }
  7229. }
  7230. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7231. return 0;
  7232. }
  7233. /*==========================================
  7234. * Creates a clone of a player.
  7235. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7236. *------------------------------------------*/
  7237. BUILDIN_FUNC(clone)
  7238. {
  7239. TBL_PC *sd, *msd=NULL;
  7240. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7241. unsigned int duration = 0;
  7242. const char *map,*event="";
  7243. map=script_getstr(st,2);
  7244. x=script_getnum(st,3);
  7245. y=script_getnum(st,4);
  7246. event=script_getstr(st,5);
  7247. char_id=script_getnum(st,6);
  7248. if( script_hasdata(st,7) )
  7249. master_id=script_getnum(st,7);
  7250. if( script_hasdata(st,8) )
  7251. mode=script_getnum(st,8);
  7252. if( script_hasdata(st,9) )
  7253. flag=script_getnum(st,9);
  7254. if( script_hasdata(st,10) )
  7255. duration=script_getnum(st,10);
  7256. check_event(st, event);
  7257. m = map_mapname2mapid(map);
  7258. if (m < 0) return 0;
  7259. sd = map_charid2sd(char_id);
  7260. if (master_id) {
  7261. msd = map_charid2sd(master_id);
  7262. if (msd)
  7263. master_id = msd->bl.id;
  7264. else
  7265. master_id = 0;
  7266. }
  7267. if (sd) //Return ID of newly crafted clone.
  7268. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7269. else //Failed to create clone.
  7270. script_pushint(st,0);
  7271. return 0;
  7272. }
  7273. /*==========================================
  7274. * イベント実行
  7275. *------------------------------------------*/
  7276. BUILDIN_FUNC(doevent)
  7277. {
  7278. const char* event = script_getstr(st,2);
  7279. struct map_session_data* sd;
  7280. if( ( sd = script_rid2sd(st) ) == NULL )
  7281. {
  7282. return 0;
  7283. }
  7284. check_event(st, event);
  7285. npc_event(sd, event, 0);
  7286. return 0;
  7287. }
  7288. /*==========================================
  7289. * NPC主体イベント実行
  7290. *------------------------------------------*/
  7291. BUILDIN_FUNC(donpcevent)
  7292. {
  7293. const char* event = script_getstr(st,2);
  7294. check_event(st, event);
  7295. npc_event_do(event);
  7296. return 0;
  7297. }
  7298. /// for Aegis compatibility
  7299. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7300. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7301. {
  7302. const char* npc = script_getstr(st,2);
  7303. const char* command = script_getstr(st,3);
  7304. char event[EVENT_NAME_LENGTH];
  7305. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  7306. check_event(st, event);
  7307. npc_event_do(event);
  7308. return 0;
  7309. }
  7310. /*==========================================
  7311. * イベントタイマー追加
  7312. *------------------------------------------*/
  7313. BUILDIN_FUNC(addtimer)
  7314. {
  7315. int tick = script_getnum(st,2);
  7316. const char* event = script_getstr(st, 3);
  7317. TBL_PC* sd;
  7318. check_event(st, event);
  7319. sd = script_rid2sd(st);
  7320. if( sd == NULL )
  7321. return 0;
  7322. pc_addeventtimer(sd,tick,event);
  7323. return 0;
  7324. }
  7325. /*==========================================
  7326. * イベントタイマー削除
  7327. *------------------------------------------*/
  7328. BUILDIN_FUNC(deltimer)
  7329. {
  7330. const char *event;
  7331. TBL_PC* sd;
  7332. event=script_getstr(st, 2);
  7333. sd = script_rid2sd(st);
  7334. if( sd == NULL )
  7335. return 0;
  7336. check_event(st, event);
  7337. pc_deleventtimer(sd,event);
  7338. return 0;
  7339. }
  7340. /*==========================================
  7341. * イベントタイマーのカウント値追加
  7342. *------------------------------------------*/
  7343. BUILDIN_FUNC(addtimercount)
  7344. {
  7345. const char *event;
  7346. int tick;
  7347. TBL_PC* sd;
  7348. event=script_getstr(st, 2);
  7349. tick=script_getnum(st,3);
  7350. sd = script_rid2sd(st);
  7351. if( sd == NULL )
  7352. return 0;
  7353. check_event(st, event);
  7354. pc_addeventtimercount(sd,event,tick);
  7355. return 0;
  7356. }
  7357. /*==========================================
  7358. * NPCタイマー初期化
  7359. *------------------------------------------*/
  7360. BUILDIN_FUNC(initnpctimer)
  7361. {
  7362. struct npc_data *nd;
  7363. int flag = 0;
  7364. if( script_hasdata(st,3) )
  7365. { //Two arguments: NPC name and attach flag.
  7366. nd = npc_name2id(script_getstr(st, 2));
  7367. flag = script_getnum(st,3);
  7368. }
  7369. else if( script_hasdata(st,2) )
  7370. { //Check if argument is numeric (flag) or string (npc name)
  7371. struct script_data *data;
  7372. data = script_getdata(st,2);
  7373. get_val(st,data);
  7374. if( data_isstring(data) ) //NPC name
  7375. nd = npc_name2id(conv_str(st, data));
  7376. else if( data_isint(data) ) //Flag
  7377. {
  7378. nd = (struct npc_data *)map_id2bl(st->oid);
  7379. flag = conv_num(st,data);
  7380. }
  7381. else
  7382. {
  7383. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7384. return 1;
  7385. }
  7386. }
  7387. else
  7388. nd = (struct npc_data *)map_id2bl(st->oid);
  7389. if( !nd )
  7390. return 0;
  7391. if( flag ) //Attach
  7392. {
  7393. TBL_PC* sd = script_rid2sd(st);
  7394. if( sd == NULL )
  7395. return 0;
  7396. nd->u.scr.rid = sd->bl.id;
  7397. }
  7398. npc_settimerevent_tick(nd,0);
  7399. npc_timerevent_start(nd, st->rid);
  7400. return 0;
  7401. }
  7402. /*==========================================
  7403. * NPCタイマー開始
  7404. *------------------------------------------*/
  7405. BUILDIN_FUNC(startnpctimer)
  7406. {
  7407. struct npc_data *nd;
  7408. int flag = 0;
  7409. if( script_hasdata(st,3) )
  7410. { //Two arguments: NPC name and attach flag.
  7411. nd = npc_name2id(script_getstr(st, 2));
  7412. flag = script_getnum(st,3);
  7413. }
  7414. else if( script_hasdata(st,2) )
  7415. { //Check if argument is numeric (flag) or string (npc name)
  7416. struct script_data *data;
  7417. data = script_getdata(st,2);
  7418. get_val(st,data);
  7419. if( data_isstring(data) ) //NPC name
  7420. nd = npc_name2id(conv_str(st, data));
  7421. else if( data_isint(data) ) //Flag
  7422. {
  7423. nd = (struct npc_data *)map_id2bl(st->oid);
  7424. flag = conv_num(st,data);
  7425. }
  7426. else
  7427. {
  7428. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7429. return 1;
  7430. }
  7431. }
  7432. else
  7433. nd=(struct npc_data *)map_id2bl(st->oid);
  7434. if( !nd )
  7435. return 0;
  7436. if( flag ) //Attach
  7437. {
  7438. TBL_PC* sd = script_rid2sd(st);
  7439. if( sd == NULL )
  7440. return 0;
  7441. nd->u.scr.rid = sd->bl.id;
  7442. }
  7443. npc_timerevent_start(nd, st->rid);
  7444. return 0;
  7445. }
  7446. /*==========================================
  7447. * NPCタイマー停止
  7448. *------------------------------------------*/
  7449. BUILDIN_FUNC(stopnpctimer)
  7450. {
  7451. struct npc_data *nd;
  7452. int flag = 0;
  7453. if( script_hasdata(st,3) )
  7454. { //Two arguments: NPC name and attach flag.
  7455. nd = npc_name2id(script_getstr(st, 2));
  7456. flag = script_getnum(st,3);
  7457. }
  7458. else if( script_hasdata(st,2) )
  7459. { //Check if argument is numeric (flag) or string (npc name)
  7460. struct script_data *data;
  7461. data = script_getdata(st,2);
  7462. get_val(st,data);
  7463. if( data_isstring(data) ) //NPC name
  7464. nd = npc_name2id(conv_str(st, data));
  7465. else if( data_isint(data) ) //Flag
  7466. {
  7467. nd = (struct npc_data *)map_id2bl(st->oid);
  7468. flag = conv_num(st,data);
  7469. }
  7470. else
  7471. {
  7472. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7473. return 1;
  7474. }
  7475. }
  7476. else
  7477. nd=(struct npc_data *)map_id2bl(st->oid);
  7478. if( !nd )
  7479. return 0;
  7480. if( flag ) //Detach
  7481. nd->u.scr.rid = 0;
  7482. npc_timerevent_stop(nd);
  7483. return 0;
  7484. }
  7485. /*==========================================
  7486. * NPCタイマー情報所得
  7487. *------------------------------------------*/
  7488. BUILDIN_FUNC(getnpctimer)
  7489. {
  7490. struct npc_data *nd;
  7491. TBL_PC *sd;
  7492. int type = script_getnum(st,2);
  7493. int val = 0;
  7494. if( script_hasdata(st,3) )
  7495. nd = npc_name2id(script_getstr(st,3));
  7496. else
  7497. nd = (struct npc_data *)map_id2bl(st->oid);
  7498. if( !nd || nd->bl.type != BL_NPC )
  7499. {
  7500. script_pushint(st,0);
  7501. ShowError("getnpctimer: Invalid NPC.\n");
  7502. return 1;
  7503. }
  7504. switch( type )
  7505. {
  7506. case 0: val = npc_gettimerevent_tick(nd); break;
  7507. case 1:
  7508. if( nd->u.scr.rid )
  7509. {
  7510. sd = map_id2sd(nd->u.scr.rid);
  7511. if( !sd )
  7512. {
  7513. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7514. break;
  7515. }
  7516. val = (sd->npc_timer_id != INVALID_TIMER);
  7517. }
  7518. else
  7519. val = (nd->u.scr.timerid != INVALID_TIMER);
  7520. break;
  7521. case 2: val = nd->u.scr.timeramount; break;
  7522. }
  7523. script_pushint(st,val);
  7524. return 0;
  7525. }
  7526. /*==========================================
  7527. * NPCタイマー値設定
  7528. *------------------------------------------*/
  7529. BUILDIN_FUNC(setnpctimer)
  7530. {
  7531. int tick;
  7532. struct npc_data *nd;
  7533. tick = script_getnum(st,2);
  7534. if( script_hasdata(st,3) )
  7535. nd = npc_name2id(script_getstr(st,3));
  7536. else
  7537. nd = (struct npc_data *)map_id2bl(st->oid);
  7538. if( !nd || nd->bl.type != BL_NPC )
  7539. {
  7540. script_pushint(st,1);
  7541. ShowError("setnpctimer: Invalid NPC.\n");
  7542. return 1;
  7543. }
  7544. npc_settimerevent_tick(nd,tick);
  7545. script_pushint(st,0);
  7546. return 0;
  7547. }
  7548. /*==========================================
  7549. * attaches the player rid to the timer [Celest]
  7550. *------------------------------------------*/
  7551. BUILDIN_FUNC(attachnpctimer)
  7552. {
  7553. TBL_PC *sd;
  7554. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7555. if( !nd || nd->bl.type != BL_NPC )
  7556. {
  7557. script_pushint(st,1);
  7558. ShowError("setnpctimer: Invalid NPC.\n");
  7559. return 1;
  7560. }
  7561. if( script_hasdata(st,2) )
  7562. sd = map_nick2sd(script_getstr(st,2));
  7563. else
  7564. sd = script_rid2sd(st);
  7565. if( !sd )
  7566. {
  7567. script_pushint(st,1);
  7568. ShowWarning("attachnpctimer: Invalid player.\n");
  7569. return 1;
  7570. }
  7571. nd->u.scr.rid = sd->bl.id;
  7572. script_pushint(st,0);
  7573. return 0;
  7574. }
  7575. /*==========================================
  7576. * detaches a player rid from the timer [Celest]
  7577. *------------------------------------------*/
  7578. BUILDIN_FUNC(detachnpctimer)
  7579. {
  7580. struct npc_data *nd;
  7581. if( script_hasdata(st,2) )
  7582. nd = npc_name2id(script_getstr(st,2));
  7583. else
  7584. nd = (struct npc_data *)map_id2bl(st->oid);
  7585. if( !nd || nd->bl.type != BL_NPC )
  7586. {
  7587. script_pushint(st,1);
  7588. ShowError("detachnpctimer: Invalid NPC.\n");
  7589. return 1;
  7590. }
  7591. nd->u.scr.rid = 0;
  7592. script_pushint(st,0);
  7593. return 0;
  7594. }
  7595. /*==========================================
  7596. * To avoid "player not attached" script errors, this function is provided,
  7597. * it checks if there is a player attached to the current script. [Skotlex]
  7598. * If no, returns 0, if yes, returns the account_id of the attached player.
  7599. *------------------------------------------*/
  7600. BUILDIN_FUNC(playerattached)
  7601. {
  7602. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7603. script_pushint(st,0);
  7604. else
  7605. script_pushint(st,st->rid);
  7606. return 0;
  7607. }
  7608. /*==========================================
  7609. * 天の声アナウンス
  7610. *------------------------------------------*/
  7611. BUILDIN_FUNC(announce)
  7612. {
  7613. const char *mes = script_getstr(st,2);
  7614. int flag = script_getnum(st,3);
  7615. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7616. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7617. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7618. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7619. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7620. if (flag&0x0f) // Broadcast source or broadcast region defined
  7621. {
  7622. send_target target;
  7623. 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
  7624. if (bl == NULL)
  7625. return 0;
  7626. flag &= 0x07;
  7627. target = (flag == 1) ? ALL_SAMEMAP :
  7628. (flag == 2) ? AREA :
  7629. (flag == 3) ? SELF :
  7630. ALL_CLIENT;
  7631. if (fontColor)
  7632. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7633. else
  7634. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7635. }
  7636. else
  7637. {
  7638. if (fontColor)
  7639. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7640. else
  7641. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7642. }
  7643. return 0;
  7644. }
  7645. /*==========================================
  7646. * 天の声アナウンス(特定マップ)
  7647. *------------------------------------------*/
  7648. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7649. {
  7650. char *mes = va_arg(ap, char *);
  7651. int len = va_arg(ap, int);
  7652. int type = va_arg(ap, int);
  7653. char *fontColor = va_arg(ap, char *);
  7654. short fontType = (short)va_arg(ap, int);
  7655. short fontSize = (short)va_arg(ap, int);
  7656. short fontAlign = (short)va_arg(ap, int);
  7657. short fontY = (short)va_arg(ap, int);
  7658. if (fontColor)
  7659. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7660. else
  7661. clif_broadcast(bl, mes, len, type, SELF);
  7662. return 0;
  7663. }
  7664. BUILDIN_FUNC(mapannounce)
  7665. {
  7666. const char *mapname = script_getstr(st,2);
  7667. const char *mes = script_getstr(st,3);
  7668. int flag = script_getnum(st,4);
  7669. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7670. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7671. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7672. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7673. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7674. int m;
  7675. if ((m = map_mapname2mapid(mapname)) < 0)
  7676. return 0;
  7677. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7678. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7679. return 0;
  7680. }
  7681. /*==========================================
  7682. * 天の声アナウンス(特定エリア)
  7683. *------------------------------------------*/
  7684. BUILDIN_FUNC(areaannounce)
  7685. {
  7686. const char *mapname = script_getstr(st,2);
  7687. int x0 = script_getnum(st,3);
  7688. int y0 = script_getnum(st,4);
  7689. int x1 = script_getnum(st,5);
  7690. int y1 = script_getnum(st,6);
  7691. const char *mes = script_getstr(st,7);
  7692. int flag = script_getnum(st,8);
  7693. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7694. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7695. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7696. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7697. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7698. int m;
  7699. if ((m = map_mapname2mapid(mapname)) < 0)
  7700. return 0;
  7701. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7702. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7703. return 0;
  7704. }
  7705. /*==========================================
  7706. * ユーザー数所得
  7707. *------------------------------------------*/
  7708. BUILDIN_FUNC(getusers)
  7709. {
  7710. int flag, val = 0;
  7711. struct map_session_data* sd;
  7712. struct block_list* bl = NULL;
  7713. flag = script_getnum(st,2);
  7714. switch(flag&0x07)
  7715. {
  7716. case 0:
  7717. if(flag&0x8)
  7718. {// npc
  7719. bl = map_id2bl(st->oid);
  7720. }
  7721. else if((sd = script_rid2sd(st))!=NULL)
  7722. {// pc
  7723. bl = &sd->bl;
  7724. }
  7725. if(bl)
  7726. {
  7727. val = map[bl->m].users;
  7728. }
  7729. break;
  7730. case 1:
  7731. val = map_getusers();
  7732. break;
  7733. default:
  7734. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  7735. script_pushint(st,0);
  7736. return 1;
  7737. }
  7738. script_pushint(st,val);
  7739. return 0;
  7740. }
  7741. /*==========================================
  7742. * Works like @WHO - displays all online users names in window
  7743. *------------------------------------------*/
  7744. BUILDIN_FUNC(getusersname)
  7745. {
  7746. TBL_PC *sd, *pl_sd;
  7747. int disp_num=1, group_level = 0;
  7748. struct s_mapiterator* iter;
  7749. sd = script_rid2sd(st);
  7750. if (!sd) return 0;
  7751. group_level = pc_get_group_level(sd);
  7752. iter = mapit_getallusers();
  7753. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7754. {
  7755. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  7756. continue; // skip hidden sessions
  7757. if((disp_num++)%10==0)
  7758. clif_scriptnext(sd,st->oid);
  7759. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7760. }
  7761. mapit_free(iter);
  7762. return 0;
  7763. }
  7764. /*==========================================
  7765. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7766. *------------------------------------------*/
  7767. BUILDIN_FUNC(getmapguildusers)
  7768. {
  7769. const char *str;
  7770. int m, gid;
  7771. int i=0,c=0;
  7772. struct guild *g = NULL;
  7773. str=script_getstr(st,2);
  7774. gid=script_getnum(st,3);
  7775. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7776. script_pushint(st,-1);
  7777. return 0;
  7778. }
  7779. g = guild_search(gid);
  7780. if (g){
  7781. for(i = 0; i < g->max_member; i++)
  7782. {
  7783. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7784. c++;
  7785. }
  7786. }
  7787. script_pushint(st,c);
  7788. return 0;
  7789. }
  7790. /*==========================================
  7791. * マップ指定ユーザー数所得
  7792. *------------------------------------------*/
  7793. BUILDIN_FUNC(getmapusers)
  7794. {
  7795. const char *str;
  7796. int m;
  7797. str=script_getstr(st,2);
  7798. if( (m=map_mapname2mapid(str))< 0){
  7799. script_pushint(st,-1);
  7800. return 0;
  7801. }
  7802. script_pushint(st,map[m].users);
  7803. return 0;
  7804. }
  7805. /*==========================================
  7806. * エリア指定ユーザー数所得
  7807. *------------------------------------------*/
  7808. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7809. {
  7810. int *users=va_arg(ap,int *);
  7811. (*users)++;
  7812. return 0;
  7813. }
  7814. BUILDIN_FUNC(getareausers)
  7815. {
  7816. const char *str;
  7817. int m,x0,y0,x1,y1,users=0;
  7818. str=script_getstr(st,2);
  7819. x0=script_getnum(st,3);
  7820. y0=script_getnum(st,4);
  7821. x1=script_getnum(st,5);
  7822. y1=script_getnum(st,6);
  7823. if( (m=map_mapname2mapid(str))< 0){
  7824. script_pushint(st,-1);
  7825. return 0;
  7826. }
  7827. map_foreachinarea(buildin_getareausers_sub,
  7828. m,x0,y0,x1,y1,BL_PC,&users);
  7829. script_pushint(st,users);
  7830. return 0;
  7831. }
  7832. /*==========================================
  7833. * エリア指定ドロップアイテム数所得
  7834. *------------------------------------------*/
  7835. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7836. {
  7837. int item=va_arg(ap,int);
  7838. int *amount=va_arg(ap,int *);
  7839. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7840. if(drop->item_data.nameid==item)
  7841. (*amount)+=drop->item_data.amount;
  7842. return 0;
  7843. }
  7844. BUILDIN_FUNC(getareadropitem)
  7845. {
  7846. const char *str;
  7847. int m,x0,y0,x1,y1,item,amount=0;
  7848. struct script_data *data;
  7849. str=script_getstr(st,2);
  7850. x0=script_getnum(st,3);
  7851. y0=script_getnum(st,4);
  7852. x1=script_getnum(st,5);
  7853. y1=script_getnum(st,6);
  7854. data=script_getdata(st,7);
  7855. get_val(st,data);
  7856. if( data_isstring(data) ){
  7857. const char *name=conv_str(st,data);
  7858. struct item_data *item_data = itemdb_searchname(name);
  7859. item=UNKNOWN_ITEM_ID;
  7860. if( item_data )
  7861. item=item_data->nameid;
  7862. }else
  7863. item=conv_num(st,data);
  7864. if( (m=map_mapname2mapid(str))< 0){
  7865. script_pushint(st,-1);
  7866. return 0;
  7867. }
  7868. map_foreachinarea(buildin_getareadropitem_sub,
  7869. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7870. script_pushint(st,amount);
  7871. return 0;
  7872. }
  7873. /*==========================================
  7874. * NPCの有効化
  7875. *------------------------------------------*/
  7876. BUILDIN_FUNC(enablenpc)
  7877. {
  7878. const char *str;
  7879. str=script_getstr(st,2);
  7880. npc_enable(str,1);
  7881. return 0;
  7882. }
  7883. /*==========================================
  7884. * NPCの無効化
  7885. *------------------------------------------*/
  7886. BUILDIN_FUNC(disablenpc)
  7887. {
  7888. const char *str;
  7889. str=script_getstr(st,2);
  7890. npc_enable(str,0);
  7891. return 0;
  7892. }
  7893. /*==========================================
  7894. * 隠れているNPCの表示
  7895. *------------------------------------------*/
  7896. BUILDIN_FUNC(hideoffnpc)
  7897. {
  7898. const char *str;
  7899. str=script_getstr(st,2);
  7900. npc_enable(str,2);
  7901. return 0;
  7902. }
  7903. /*==========================================
  7904. * NPCをハイディング
  7905. *------------------------------------------*/
  7906. BUILDIN_FUNC(hideonnpc)
  7907. {
  7908. const char *str;
  7909. str=script_getstr(st,2);
  7910. npc_enable(str,4);
  7911. return 0;
  7912. }
  7913. /// Starts a status effect on the target unit or on the attached player.
  7914. ///
  7915. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7916. BUILDIN_FUNC(sc_start)
  7917. {
  7918. struct block_list* bl;
  7919. enum sc_type type;
  7920. int tick;
  7921. int val1;
  7922. int val4 = 0;
  7923. type = (sc_type)script_getnum(st,2);
  7924. tick = script_getnum(st,3);
  7925. val1 = script_getnum(st,4);
  7926. if( script_hasdata(st,5) )
  7927. bl = map_id2bl(script_getnum(st,5));
  7928. else
  7929. bl = map_id2bl(st->rid);
  7930. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7931. {// When there isn't a duration specified, try to get it from the skill_db
  7932. tick = skill_get_time(status_sc2skill(type), val1);
  7933. }
  7934. if( potion_flag == 1 && potion_target )
  7935. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7936. bl = map_id2bl(potion_target);
  7937. tick /= 2;// Thrown potions only last half.
  7938. val4 = 1;// Mark that this was a thrown sc_effect
  7939. }
  7940. if( bl )
  7941. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7942. return 0;
  7943. }
  7944. /// Starts a status effect on the target unit or on the attached player.
  7945. ///
  7946. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7947. BUILDIN_FUNC(sc_start2)
  7948. {
  7949. struct block_list* bl;
  7950. enum sc_type type;
  7951. int tick;
  7952. int val1;
  7953. int val4 = 0;
  7954. int rate;
  7955. type = (sc_type)script_getnum(st,2);
  7956. tick = script_getnum(st,3);
  7957. val1 = script_getnum(st,4);
  7958. rate = script_getnum(st,5);
  7959. if( script_hasdata(st,6) )
  7960. bl = map_id2bl(script_getnum(st,6));
  7961. else
  7962. bl = map_id2bl(st->rid);
  7963. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7964. {// When there isn't a duration specified, try to get it from the skill_db
  7965. tick = skill_get_time(status_sc2skill(type), val1);
  7966. }
  7967. if( potion_flag == 1 && potion_target )
  7968. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7969. bl = map_id2bl(potion_target);
  7970. tick /= 2;// Thrown potions only last half.
  7971. val4 = 1;// Mark that this was a thrown sc_effect
  7972. }
  7973. if( bl )
  7974. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7975. return 0;
  7976. }
  7977. /// Starts a status effect on the target unit or on the attached player.
  7978. ///
  7979. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7980. BUILDIN_FUNC(sc_start4)
  7981. {
  7982. struct block_list* bl;
  7983. enum sc_type type;
  7984. int tick;
  7985. int val1;
  7986. int val2;
  7987. int val3;
  7988. int val4;
  7989. type = (sc_type)script_getnum(st,2);
  7990. tick = script_getnum(st,3);
  7991. val1 = script_getnum(st,4);
  7992. val2 = script_getnum(st,5);
  7993. val3 = script_getnum(st,6);
  7994. val4 = script_getnum(st,7);
  7995. if( script_hasdata(st,8) )
  7996. bl = map_id2bl(script_getnum(st,8));
  7997. else
  7998. bl = map_id2bl(st->rid);
  7999. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8000. {// When there isn't a duration specified, try to get it from the skill_db
  8001. tick = skill_get_time(status_sc2skill(type), val1);
  8002. }
  8003. if( potion_flag == 1 && potion_target )
  8004. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8005. bl = map_id2bl(potion_target);
  8006. tick /= 2;// Thrown potions only last half.
  8007. }
  8008. if( bl )
  8009. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  8010. return 0;
  8011. }
  8012. /// Ends one or all status effects on the target unit or on the attached player.
  8013. ///
  8014. /// sc_end <effect_id>{,<unit_id>};
  8015. BUILDIN_FUNC(sc_end)
  8016. {
  8017. struct block_list* bl;
  8018. int type;
  8019. type = script_getnum(st,2);
  8020. if( script_hasdata(st,3) )
  8021. bl = map_id2bl(script_getnum(st,3));
  8022. else
  8023. bl = map_id2bl(st->rid);
  8024. if( potion_flag==1 && potion_target )
  8025. {//##TODO how does this work [FlavioJS]
  8026. bl = map_id2bl(potion_target);
  8027. }
  8028. if( !bl ) return 0;
  8029. if( type >= 0 && type < SC_MAX )
  8030. {
  8031. struct status_change *sc = status_get_sc(bl);
  8032. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  8033. if (!sce) return 0;
  8034. //This should help status_change_end force disabling the SC in case it has no limit.
  8035. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8036. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8037. } else
  8038. status_change_clear(bl, 2);// remove all effects
  8039. return 0;
  8040. }
  8041. /*==========================================
  8042. * 状態異常耐性を計算した確率を返す
  8043. *------------------------------------------*/
  8044. BUILDIN_FUNC(getscrate)
  8045. {
  8046. struct block_list *bl;
  8047. int type,rate;
  8048. type=script_getnum(st,2);
  8049. rate=script_getnum(st,3);
  8050. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  8051. bl = map_id2bl(script_getnum(st,4));
  8052. else
  8053. bl = map_id2bl(st->rid);
  8054. if (bl)
  8055. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  8056. script_pushint(st,rate);
  8057. return 0;
  8058. }
  8059. /*==========================================
  8060. * getstatus <type>{, <info>};
  8061. *------------------------------------------*/
  8062. BUILDIN_FUNC(getstatus)
  8063. {
  8064. int id, type;
  8065. struct map_session_data* sd = script_rid2sd(st);
  8066. if( sd == NULL )
  8067. {// no player attached
  8068. return 0;
  8069. }
  8070. id = script_getnum(st, 2);
  8071. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8072. if( id <= SC_NONE || id >= SC_MAX )
  8073. {// invalid status type given
  8074. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8075. return 0;
  8076. }
  8077. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8078. {// no status is active
  8079. script_pushint(st, 0);
  8080. return 0;
  8081. }
  8082. switch( type )
  8083. {
  8084. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8085. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8086. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8087. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8088. case 5:
  8089. {
  8090. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8091. if( timer )
  8092. {// return the amount of time remaining
  8093. script_pushint(st, timer->tick - gettick());
  8094. }
  8095. }
  8096. break;
  8097. default: script_pushint(st, 1); break;
  8098. }
  8099. return 0;
  8100. }
  8101. /*==========================================
  8102. *
  8103. *------------------------------------------*/
  8104. BUILDIN_FUNC(debugmes)
  8105. {
  8106. const char *str;
  8107. str=script_getstr(st,2);
  8108. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8109. return 0;
  8110. }
  8111. /*==========================================
  8112. *捕獲アイテム使用
  8113. *------------------------------------------*/
  8114. BUILDIN_FUNC(catchpet)
  8115. {
  8116. int pet_id;
  8117. TBL_PC *sd;
  8118. pet_id= script_getnum(st,2);
  8119. sd=script_rid2sd(st);
  8120. if( sd == NULL )
  8121. return 0;
  8122. pet_catch_process1(sd,pet_id);
  8123. return 0;
  8124. }
  8125. /*==========================================
  8126. * [orn]
  8127. *------------------------------------------*/
  8128. BUILDIN_FUNC(homunculus_evolution)
  8129. {
  8130. TBL_PC *sd;
  8131. sd=script_rid2sd(st);
  8132. if( sd == NULL )
  8133. return 0;
  8134. if(merc_is_hom_active(sd->hd))
  8135. {
  8136. if (sd->hd->homunculus.intimacy > 91000)
  8137. merc_hom_evolution(sd->hd);
  8138. else
  8139. clif_emotion(&sd->hd->bl, E_SWT);
  8140. }
  8141. return 0;
  8142. }
  8143. // [Zephyrus]
  8144. BUILDIN_FUNC(homunculus_shuffle)
  8145. {
  8146. TBL_PC *sd;
  8147. sd=script_rid2sd(st);
  8148. if( sd == NULL )
  8149. return 0;
  8150. if(merc_is_hom_active(sd->hd))
  8151. merc_hom_shuffle(sd->hd);
  8152. return 0;
  8153. }
  8154. //These two functions bring the eA MAPID_* class functionality to scripts.
  8155. BUILDIN_FUNC(eaclass)
  8156. {
  8157. int class_;
  8158. if( script_hasdata(st,2) )
  8159. class_ = script_getnum(st,2);
  8160. else {
  8161. TBL_PC *sd;
  8162. sd=script_rid2sd(st);
  8163. if (!sd) {
  8164. script_pushint(st,-1);
  8165. return 0;
  8166. }
  8167. class_ = sd->status.class_;
  8168. }
  8169. script_pushint(st,pc_jobid2mapid(class_));
  8170. return 0;
  8171. }
  8172. BUILDIN_FUNC(roclass)
  8173. {
  8174. int class_ =script_getnum(st,2);
  8175. int sex;
  8176. if( script_hasdata(st,3) )
  8177. sex = script_getnum(st,3);
  8178. else {
  8179. TBL_PC *sd;
  8180. if (st->rid && (sd=script_rid2sd(st)))
  8181. sex = sd->status.sex;
  8182. else
  8183. sex = 1; //Just use male when not found.
  8184. }
  8185. script_pushint(st,pc_mapid2jobid(class_, sex));
  8186. return 0;
  8187. }
  8188. /*==========================================
  8189. * Tells client to open a hatching window, used for pet incubator
  8190. *------------------------------------------*/
  8191. BUILDIN_FUNC(birthpet)
  8192. {
  8193. TBL_PC *sd;
  8194. sd=script_rid2sd(st);
  8195. if( sd == NULL )
  8196. return 0;
  8197. if( sd->status.pet_id )
  8198. {// do not send egg list, when you already have a pet
  8199. return 0;
  8200. }
  8201. clif_sendegg(sd);
  8202. return 0;
  8203. }
  8204. /*==========================================
  8205. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8206. *------------------------------------------*/
  8207. BUILDIN_FUNC(resetlvl)
  8208. {
  8209. TBL_PC *sd;
  8210. int type=script_getnum(st,2);
  8211. sd=script_rid2sd(st);
  8212. if( sd == NULL )
  8213. return 0;
  8214. pc_resetlvl(sd,type);
  8215. return 0;
  8216. }
  8217. /*==========================================
  8218. * ステータスリセット
  8219. *------------------------------------------*/
  8220. BUILDIN_FUNC(resetstatus)
  8221. {
  8222. TBL_PC *sd;
  8223. sd=script_rid2sd(st);
  8224. pc_resetstate(sd);
  8225. return 0;
  8226. }
  8227. /*==========================================
  8228. * script command resetskill
  8229. *------------------------------------------*/
  8230. BUILDIN_FUNC(resetskill)
  8231. {
  8232. TBL_PC *sd;
  8233. sd=script_rid2sd(st);
  8234. pc_resetskill(sd,1);
  8235. return 0;
  8236. }
  8237. /*==========================================
  8238. * Counts total amount of skill points.
  8239. *------------------------------------------*/
  8240. BUILDIN_FUNC(skillpointcount)
  8241. {
  8242. TBL_PC *sd;
  8243. sd=script_rid2sd(st);
  8244. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8245. return 0;
  8246. }
  8247. /*==========================================
  8248. *
  8249. *------------------------------------------*/
  8250. BUILDIN_FUNC(changebase)
  8251. {
  8252. TBL_PC *sd=NULL;
  8253. int vclass;
  8254. if( script_hasdata(st,3) )
  8255. sd=map_id2sd(script_getnum(st,3));
  8256. else
  8257. sd=script_rid2sd(st);
  8258. if(sd == NULL)
  8259. return 0;
  8260. vclass = script_getnum(st,2);
  8261. if(vclass == JOB_WEDDING)
  8262. {
  8263. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8264. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8265. )
  8266. return 0;
  8267. }
  8268. if(!sd->disguise && vclass != sd->vd.class_) {
  8269. status_set_viewdata(&sd->bl, vclass);
  8270. //Updated client view. Base, Weapon and Cloth Colors.
  8271. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8272. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8273. if (sd->vd.cloth_color)
  8274. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8275. clif_skillinfoblock(sd);
  8276. }
  8277. return 0;
  8278. }
  8279. /*==========================================
  8280. * 性別変換
  8281. *------------------------------------------*/
  8282. BUILDIN_FUNC(changesex)
  8283. {
  8284. TBL_PC *sd = NULL;
  8285. sd = script_rid2sd(st);
  8286. pc_resetskill(sd,4);
  8287. chrif_changesex(sd);
  8288. return 0;
  8289. }
  8290. /*==========================================
  8291. * Works like 'announce' but outputs in the common chat window
  8292. *------------------------------------------*/
  8293. BUILDIN_FUNC(globalmes)
  8294. {
  8295. struct block_list *bl = map_id2bl(st->oid);
  8296. struct npc_data *nd = (struct npc_data *)bl;
  8297. const char *name=NULL,*mes;
  8298. mes=script_getstr(st,2); // メッセージの取得
  8299. if(mes==NULL) return 0;
  8300. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8301. name=script_getstr(st,3);
  8302. } else {
  8303. name=nd->name;
  8304. }
  8305. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8306. return 0;
  8307. }
  8308. /////////////////////////////////////////////////////////////////////
  8309. // NPC waiting room (chat room)
  8310. //
  8311. /// Creates a waiting room (chat room) for this npc.
  8312. ///
  8313. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  8314. BUILDIN_FUNC(waitingroom)
  8315. {
  8316. struct npc_data* nd;
  8317. int pub = 1;
  8318. const char* title = script_getstr(st, 2);
  8319. int limit = script_getnum(st, 3);
  8320. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  8321. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  8322. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  8323. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  8324. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  8325. nd = (struct npc_data *)map_id2bl(st->oid);
  8326. if( nd != NULL )
  8327. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  8328. return 0;
  8329. }
  8330. /// Removes the waiting room of the current or target npc.
  8331. ///
  8332. /// delwaitingroom "<npc_name>";
  8333. /// delwaitingroom;
  8334. BUILDIN_FUNC(delwaitingroom)
  8335. {
  8336. struct npc_data* nd;
  8337. if( script_hasdata(st,2) )
  8338. nd = npc_name2id(script_getstr(st, 2));
  8339. else
  8340. nd = (struct npc_data *)map_id2bl(st->oid);
  8341. if( nd != NULL )
  8342. chat_deletenpcchat(nd);
  8343. return 0;
  8344. }
  8345. /// Kicks all the players from the waiting room of the current or target npc.
  8346. ///
  8347. /// kickwaitingroomall "<npc_name>";
  8348. /// kickwaitingroomall;
  8349. BUILDIN_FUNC(waitingroomkickall)
  8350. {
  8351. struct npc_data* nd;
  8352. struct chat_data* cd;
  8353. if( script_hasdata(st,2) )
  8354. nd = npc_name2id(script_getstr(st,2));
  8355. else
  8356. nd = (struct npc_data *)map_id2bl(st->oid);
  8357. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8358. chat_npckickall(cd);
  8359. return 0;
  8360. }
  8361. /// Enables the waiting room event of the current or target npc.
  8362. ///
  8363. /// enablewaitingroomevent "<npc_name>";
  8364. /// enablewaitingroomevent;
  8365. BUILDIN_FUNC(enablewaitingroomevent)
  8366. {
  8367. struct npc_data* nd;
  8368. struct chat_data* cd;
  8369. if( script_hasdata(st,2) )
  8370. nd = npc_name2id(script_getstr(st, 2));
  8371. else
  8372. nd = (struct npc_data *)map_id2bl(st->oid);
  8373. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8374. chat_enableevent(cd);
  8375. return 0;
  8376. }
  8377. /// Disables the waiting room event of the current or target npc.
  8378. ///
  8379. /// disablewaitingroomevent "<npc_name>";
  8380. /// disablewaitingroomevent;
  8381. BUILDIN_FUNC(disablewaitingroomevent)
  8382. {
  8383. struct npc_data *nd;
  8384. struct chat_data *cd;
  8385. if( script_hasdata(st,2) )
  8386. nd = npc_name2id(script_getstr(st, 2));
  8387. else
  8388. nd = (struct npc_data *)map_id2bl(st->oid);
  8389. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8390. chat_disableevent(cd);
  8391. return 0;
  8392. }
  8393. /// Returns info on the waiting room of the current or target npc.
  8394. /// Returns -1 if the type unknown
  8395. /// <type>=0 : current number of users
  8396. /// <type>=1 : maximum number of users allowed
  8397. /// <type>=2 : the number of users that trigger the event
  8398. /// <type>=3 : if the trigger is disabled
  8399. /// <type>=4 : the title of the waiting room
  8400. /// <type>=5 : the password of the waiting room
  8401. /// <type>=16 : the name of the waiting room event
  8402. /// <type>=32 : if the waiting room is full
  8403. /// <type>=33 : if there are enough users to trigger the event
  8404. ///
  8405. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8406. /// getwaitingroomstate(<type>) -> <info>
  8407. BUILDIN_FUNC(getwaitingroomstate)
  8408. {
  8409. struct npc_data *nd;
  8410. struct chat_data *cd;
  8411. int type;
  8412. type = script_getnum(st,2);
  8413. if( script_hasdata(st,3) )
  8414. nd = npc_name2id(script_getstr(st, 3));
  8415. else
  8416. nd = (struct npc_data *)map_id2bl(st->oid);
  8417. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8418. {
  8419. script_pushint(st, -1);
  8420. return 0;
  8421. }
  8422. switch(type)
  8423. {
  8424. case 0: script_pushint(st, cd->users); break;
  8425. case 1: script_pushint(st, cd->limit); break;
  8426. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8427. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8428. case 4: script_pushstrcopy(st, cd->title); break;
  8429. case 5: script_pushstrcopy(st, cd->pass); break;
  8430. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8431. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8432. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8433. default: script_pushint(st, -1); break;
  8434. }
  8435. return 0;
  8436. }
  8437. /// Warps the trigger or target amount of players to the target map and position.
  8438. /// Players are automatically removed from the waiting room.
  8439. /// Those waiting the longest will get warped first.
  8440. /// The target map can be "Random" for a random position in the current map,
  8441. /// and "SavePoint" for the savepoint map+position.
  8442. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8443. ///
  8444. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8445. /// The total number of teleported players is put into $@warpwaitingpcnum
  8446. ///
  8447. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8448. /// warpwaitingpc "<map name>",<x>,<y>;
  8449. BUILDIN_FUNC(warpwaitingpc)
  8450. {
  8451. int x;
  8452. int y;
  8453. int i;
  8454. int n;
  8455. const char* map_name;
  8456. struct npc_data* nd;
  8457. struct chat_data* cd;
  8458. TBL_PC* sd;
  8459. nd = (struct npc_data *)map_id2bl(st->oid);
  8460. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8461. return 0;
  8462. map_name = script_getstr(st,2);
  8463. x = script_getnum(st,3);
  8464. y = script_getnum(st,4);
  8465. n = cd->trigger&0x7f;
  8466. if( script_hasdata(st,5) )
  8467. n = script_getnum(st,5);
  8468. for( i = 0; i < n && cd->users > 0; i++ )
  8469. {
  8470. sd = cd->usersd[0];
  8471. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  8472. {// can't teleport on this map
  8473. break;
  8474. }
  8475. if( cd->zeny )
  8476. {// fee set
  8477. if( (uint32)sd->status.zeny < cd->zeny )
  8478. {// no zeny to cover set fee
  8479. break;
  8480. }
  8481. pc_payzeny(sd, cd->zeny);
  8482. }
  8483. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  8484. if( strcmp(map_name,"Random") == 0 )
  8485. pc_randomwarp(sd,CLR_TELEPORT);
  8486. else if( strcmp(map_name,"SavePoint") == 0 )
  8487. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  8488. else
  8489. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  8490. }
  8491. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8492. return 0;
  8493. }
  8494. /////////////////////////////////////////////////////////////////////
  8495. // ...
  8496. //
  8497. /// Detaches a character from a script.
  8498. ///
  8499. /// @param st Script state to detach the character from.
  8500. static void script_detach_rid(struct script_state* st)
  8501. {
  8502. if(st->rid)
  8503. {
  8504. script_detach_state(st, false);
  8505. st->rid = 0;
  8506. }
  8507. }
  8508. /*==========================================
  8509. * RIDのアタッチ
  8510. *------------------------------------------*/
  8511. BUILDIN_FUNC(attachrid)
  8512. {
  8513. int rid = script_getnum(st,2);
  8514. struct map_session_data* sd;
  8515. if ((sd = map_id2sd(rid))!=NULL) {
  8516. script_detach_rid(st);
  8517. st->rid = rid;
  8518. script_attach_state(st);
  8519. script_pushint(st,1);
  8520. } else
  8521. script_pushint(st,0);
  8522. return 0;
  8523. }
  8524. /*==========================================
  8525. * RIDのデタッチ
  8526. *------------------------------------------*/
  8527. BUILDIN_FUNC(detachrid)
  8528. {
  8529. script_detach_rid(st);
  8530. return 0;
  8531. }
  8532. /*==========================================
  8533. * 存在チェック
  8534. *------------------------------------------*/
  8535. BUILDIN_FUNC(isloggedin)
  8536. {
  8537. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8538. if (script_hasdata(st,3) && sd &&
  8539. sd->status.char_id != script_getnum(st,3))
  8540. sd = NULL;
  8541. push_val(st->stack,C_INT,sd!=NULL);
  8542. return 0;
  8543. }
  8544. /*==========================================
  8545. *
  8546. *------------------------------------------*/
  8547. BUILDIN_FUNC(setmapflagnosave)
  8548. {
  8549. int m,x,y;
  8550. unsigned short mapindex;
  8551. const char *str,*str2;
  8552. str=script_getstr(st,2);
  8553. str2=script_getstr(st,3);
  8554. x=script_getnum(st,4);
  8555. y=script_getnum(st,5);
  8556. m = map_mapname2mapid(str);
  8557. mapindex = mapindex_name2id(str2);
  8558. if(m >= 0 && mapindex) {
  8559. map[m].flag.nosave=1;
  8560. map[m].save.map=mapindex;
  8561. map[m].save.x=x;
  8562. map[m].save.y=y;
  8563. }
  8564. return 0;
  8565. }
  8566. BUILDIN_FUNC(getmapflag)
  8567. {
  8568. int m,i;
  8569. const char *str;
  8570. str=script_getstr(st,2);
  8571. i=script_getnum(st,3);
  8572. m = map_mapname2mapid(str);
  8573. if(m >= 0) {
  8574. switch(i) {
  8575. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8576. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8577. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  8578. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8579. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8580. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8581. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8582. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8583. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8584. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8585. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8586. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8587. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8588. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8589. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8590. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8591. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8592. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8593. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8594. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8595. /**
  8596. * No longer available, keeping here just in case it's back someday. [Ind]
  8597. **/
  8598. //case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8599. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8600. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8601. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8602. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8603. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8604. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8605. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8606. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8607. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8608. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8609. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8610. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8611. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8612. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8613. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8614. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8615. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8616. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8617. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8618. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8619. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8620. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8621. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  8622. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8623. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8624. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8625. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8626. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8627. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8628. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8629. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  8630. }
  8631. }
  8632. return 0;
  8633. }
  8634. BUILDIN_FUNC(setmapflag)
  8635. {
  8636. int m,i;
  8637. const char *str;
  8638. const char *val=NULL;
  8639. str=script_getstr(st,2);
  8640. i=script_getnum(st,3);
  8641. if(script_hasdata(st,4)){
  8642. val=script_getstr(st,4);
  8643. }
  8644. m = map_mapname2mapid(str);
  8645. if(m >= 0) {
  8646. switch(i) {
  8647. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  8648. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  8649. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  8650. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  8651. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  8652. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  8653. case MF_PVP: map[m].flag.pvp = 1; break;
  8654. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  8655. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  8656. case MF_GVG: map[m].flag.gvg = 1; break;
  8657. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  8658. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  8659. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  8660. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  8661. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  8662. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  8663. case MF_SNOW: map[m].flag.snow = 1; break;
  8664. case MF_FOG: map[m].flag.fog = 1; break;
  8665. case MF_SAKURA: map[m].flag.sakura = 1; break;
  8666. case MF_LEAVES: map[m].flag.leaves = 1; break;
  8667. /**
  8668. * No longer available, keeping here just in case it's back someday. [Ind]
  8669. **/
  8670. //case MF_RAIN: map[m].flag.rain = 1; break;
  8671. case MF_NOGO: map[m].flag.nogo = 1; break;
  8672. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  8673. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  8674. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  8675. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  8676. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  8677. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  8678. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  8679. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  8680. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  8681. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  8682. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  8683. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  8684. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  8685. case MF_RESTRICTED:
  8686. map[m].zone |= 1<<((int)atoi(val)+1);
  8687. map[m].flag.restricted=1;
  8688. break;
  8689. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8690. case MF_NODROP: map[m].flag.nodrop = 1; break;
  8691. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8692. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8693. case MF_NOVENDING: map[m].flag.novending = 1; break;
  8694. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  8695. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  8696. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  8697. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  8698. case MF_TOWN: map[m].flag.town = 1; break;
  8699. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  8700. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  8701. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  8702. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  8703. case MF_BATTLEGROUND: map[m].flag.battleground = (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  8704. case MF_RESET: map[m].flag.reset = 1; break;
  8705. }
  8706. }
  8707. return 0;
  8708. }
  8709. BUILDIN_FUNC(removemapflag)
  8710. {
  8711. int m,i;
  8712. const char *str;
  8713. const char *val=NULL;
  8714. str=script_getstr(st,2);
  8715. i=script_getnum(st,3);
  8716. if(script_hasdata(st,4)){
  8717. val=script_getstr(st,4);
  8718. }
  8719. m = map_mapname2mapid(str);
  8720. if(m >= 0) {
  8721. switch(i) {
  8722. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  8723. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  8724. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  8725. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  8726. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  8727. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  8728. case MF_PVP: map[m].flag.pvp = 0; break;
  8729. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  8730. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  8731. case MF_GVG: map[m].flag.gvg = 0; break;
  8732. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  8733. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  8734. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  8735. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  8736. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  8737. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  8738. case MF_SNOW: map[m].flag.snow = 0; break;
  8739. case MF_FOG: map[m].flag.fog = 0; break;
  8740. case MF_SAKURA: map[m].flag.sakura = 0; break;
  8741. case MF_LEAVES: map[m].flag.leaves = 0; break;
  8742. /**
  8743. * No longer available, keeping here just in case it's back someday. [Ind]
  8744. **/
  8745. //case MF_RAIN: map[m].flag.rain = 0; break;
  8746. case MF_NOGO: map[m].flag.nogo = 0; break;
  8747. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  8748. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  8749. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  8750. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  8751. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  8752. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  8753. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  8754. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  8755. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  8756. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  8757. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  8758. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  8759. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  8760. case MF_RESTRICTED:
  8761. map[m].zone ^= 1<<((int)atoi(val)+1);
  8762. if (map[m].zone == 0){
  8763. map[m].flag.restricted=0;
  8764. }
  8765. break;
  8766. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  8767. case MF_NODROP: map[m].flag.nodrop = 0; break;
  8768. case MF_JEXP: map[m].jexp = 0; break;
  8769. case MF_BEXP: map[m].bexp = 0; break;
  8770. case MF_NOVENDING: map[m].flag.novending = 0; break;
  8771. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  8772. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  8773. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  8774. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  8775. case MF_TOWN: map[m].flag.town = 0; break;
  8776. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  8777. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  8778. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  8779. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  8780. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  8781. case MF_RESET: map[m].flag.reset = 0; break;
  8782. }
  8783. }
  8784. return 0;
  8785. }
  8786. BUILDIN_FUNC(pvpon)
  8787. {
  8788. int m;
  8789. const char *str;
  8790. TBL_PC* sd = NULL;
  8791. struct s_mapiterator* iter;
  8792. str = script_getstr(st,2);
  8793. m = map_mapname2mapid(str);
  8794. if( m < 0 || map[m].flag.pvp )
  8795. return 0; // nothing to do
  8796. map[m].flag.pvp = 1;
  8797. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  8798. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8799. return 0;
  8800. iter = mapit_getallusers();
  8801. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8802. {
  8803. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  8804. continue; // not applicable
  8805. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8806. sd->pvp_rank = 0;
  8807. sd->pvp_lastusers = 0;
  8808. sd->pvp_point = 5;
  8809. sd->pvp_won = 0;
  8810. sd->pvp_lost = 0;
  8811. }
  8812. mapit_free(iter);
  8813. return 0;
  8814. }
  8815. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8816. {
  8817. TBL_PC* sd = (TBL_PC*)bl;
  8818. clif_pvpset(sd, 0, 0, 2);
  8819. if (sd->pvp_timer != INVALID_TIMER) {
  8820. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8821. sd->pvp_timer = INVALID_TIMER;
  8822. }
  8823. return 0;
  8824. }
  8825. BUILDIN_FUNC(pvpoff)
  8826. {
  8827. int m;
  8828. const char *str;
  8829. str=script_getstr(st,2);
  8830. m = map_mapname2mapid(str);
  8831. if(m < 0 || !map[m].flag.pvp)
  8832. return 0; //fixed Lupus
  8833. map[m].flag.pvp = 0;
  8834. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8835. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8836. return 0;
  8837. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8838. return 0;
  8839. }
  8840. BUILDIN_FUNC(gvgon)
  8841. {
  8842. int m;
  8843. const char *str;
  8844. str=script_getstr(st,2);
  8845. m = map_mapname2mapid(str);
  8846. if(m >= 0 && !map[m].flag.gvg) {
  8847. map[m].flag.gvg = 1;
  8848. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  8849. }
  8850. return 0;
  8851. }
  8852. BUILDIN_FUNC(gvgoff)
  8853. {
  8854. int m;
  8855. const char *str;
  8856. str=script_getstr(st,2);
  8857. m = map_mapname2mapid(str);
  8858. if(m >= 0 && map[m].flag.gvg) {
  8859. map[m].flag.gvg = 0;
  8860. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8861. }
  8862. return 0;
  8863. }
  8864. /*==========================================
  8865. * Shows an emoticon on top of the player/npc
  8866. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  8867. *------------------------------------------*/
  8868. //Optional second parameter added by [Skotlex]
  8869. BUILDIN_FUNC(emotion)
  8870. {
  8871. int type;
  8872. int player=0;
  8873. type=script_getnum(st,2);
  8874. if(type < 0 || type > 100)
  8875. return 0;
  8876. if( script_hasdata(st,3) )
  8877. player=script_getnum(st,3);
  8878. if (player) {
  8879. TBL_PC *sd = NULL;
  8880. if( script_hasdata(st,4) )
  8881. sd = map_nick2sd(script_getstr(st,4));
  8882. else
  8883. sd = script_rid2sd(st);
  8884. if (sd)
  8885. clif_emotion(&sd->bl,type);
  8886. } else
  8887. if( script_hasdata(st,4) )
  8888. {
  8889. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  8890. if(nd)
  8891. clif_emotion(&nd->bl,type);
  8892. }
  8893. else
  8894. clif_emotion(map_id2bl(st->oid),type);
  8895. return 0;
  8896. }
  8897. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8898. {
  8899. int m=va_arg(ap,int);
  8900. int g_id=va_arg(ap,int);
  8901. int flag=va_arg(ap,int);
  8902. if(!sd || sd->bl.m != m)
  8903. return 0;
  8904. if(
  8905. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8906. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8907. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8908. )
  8909. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  8910. return 1;
  8911. }
  8912. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8913. {
  8914. struct mob_data *md=(struct mob_data *)bl;
  8915. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8916. status_kill(bl);
  8917. return 0;
  8918. }
  8919. BUILDIN_FUNC(maprespawnguildid)
  8920. {
  8921. const char *mapname=script_getstr(st,2);
  8922. int g_id=script_getnum(st,3);
  8923. int flag=script_getnum(st,4);
  8924. int m=map_mapname2mapid(mapname);
  8925. if(m == -1)
  8926. return 0;
  8927. //Catch ALL players (in case some are 'between maps' on execution time)
  8928. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8929. if (flag&4) //Remove script mobs.
  8930. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8931. return 0;
  8932. }
  8933. BUILDIN_FUNC(agitstart)
  8934. {
  8935. if(agit_flag==1) return 0; // Agit already Start.
  8936. agit_flag=1;
  8937. guild_agit_start();
  8938. return 0;
  8939. }
  8940. BUILDIN_FUNC(agitend)
  8941. {
  8942. if(agit_flag==0) return 0; // Agit already End.
  8943. agit_flag=0;
  8944. guild_agit_end();
  8945. return 0;
  8946. }
  8947. BUILDIN_FUNC(agitstart2)
  8948. {
  8949. if(agit2_flag==1) return 0; // Agit2 already Start.
  8950. agit2_flag=1;
  8951. guild_agit2_start();
  8952. return 0;
  8953. }
  8954. BUILDIN_FUNC(agitend2)
  8955. {
  8956. if(agit2_flag==0) return 0; // Agit2 already End.
  8957. agit2_flag=0;
  8958. guild_agit2_end();
  8959. return 0;
  8960. }
  8961. /*==========================================
  8962. * Returns whether woe is on or off. // choice script
  8963. *------------------------------------------*/
  8964. BUILDIN_FUNC(agitcheck)
  8965. {
  8966. script_pushint(st,agit_flag);
  8967. return 0;
  8968. }
  8969. /*==========================================
  8970. * Returns whether woese is on or off. // choice script
  8971. *------------------------------------------*/
  8972. BUILDIN_FUNC(agitcheck2)
  8973. {
  8974. script_pushint(st,agit2_flag);
  8975. return 0;
  8976. }
  8977. /// Sets the guild_id of this npc.
  8978. ///
  8979. /// flagemblem <guild_id>;
  8980. BUILDIN_FUNC(flagemblem)
  8981. {
  8982. TBL_NPC* nd;
  8983. int g_id=script_getnum(st,2);
  8984. if(g_id < 0) return 0;
  8985. nd = (TBL_NPC*)map_id2nd(st->oid);
  8986. if( nd == NULL )
  8987. {
  8988. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8989. }
  8990. else if( nd->subtype != SCRIPT )
  8991. {
  8992. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8993. }
  8994. else
  8995. {
  8996. nd->u.scr.guild_id = g_id;
  8997. clif_guild_emblem_area(&nd->bl);
  8998. }
  8999. return 0;
  9000. }
  9001. BUILDIN_FUNC(getcastlename)
  9002. {
  9003. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9004. struct guild_castle* gc = guild_mapname2gc(mapname);
  9005. const char* name = (gc) ? gc->castle_name : "";
  9006. script_pushstrcopy(st,name);
  9007. return 0;
  9008. }
  9009. BUILDIN_FUNC(getcastledata)
  9010. {
  9011. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9012. int index = script_getnum(st,3);
  9013. struct guild_castle *gc = guild_mapname2gc(mapname);
  9014. if (gc == NULL) {
  9015. script_pushint(st,0);
  9016. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9017. return 1;
  9018. }
  9019. switch (index) {
  9020. case 1:
  9021. script_pushint(st,gc->guild_id); break;
  9022. case 2:
  9023. script_pushint(st,gc->economy); break;
  9024. case 3:
  9025. script_pushint(st,gc->defense); break;
  9026. case 4:
  9027. script_pushint(st,gc->triggerE); break;
  9028. case 5:
  9029. script_pushint(st,gc->triggerD); break;
  9030. case 6:
  9031. script_pushint(st,gc->nextTime); break;
  9032. case 7:
  9033. script_pushint(st,gc->payTime); break;
  9034. case 8:
  9035. script_pushint(st,gc->createTime); break;
  9036. case 9:
  9037. script_pushint(st,gc->visibleC); break;
  9038. default:
  9039. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  9040. script_pushint(st,gc->guardian[index-10].visible);
  9041. break;
  9042. }
  9043. script_pushint(st,0);
  9044. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9045. return 1;
  9046. }
  9047. return 0;
  9048. }
  9049. BUILDIN_FUNC(setcastledata)
  9050. {
  9051. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9052. int index = script_getnum(st,3);
  9053. int value = script_getnum(st,4);
  9054. struct guild_castle *gc = guild_mapname2gc(mapname);
  9055. if (gc == NULL) {
  9056. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9057. return 1;
  9058. }
  9059. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  9060. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9061. return 1;
  9062. }
  9063. guild_castledatasave(gc->castle_id, index, value);
  9064. return 0;
  9065. }
  9066. /* =====================================================================
  9067. * ギルド情報を要求する
  9068. * ---------------------------------------------------------------------*/
  9069. BUILDIN_FUNC(requestguildinfo)
  9070. {
  9071. int guild_id=script_getnum(st,2);
  9072. const char *event=NULL;
  9073. if( script_hasdata(st,3) ){
  9074. event=script_getstr(st,3);
  9075. check_event(st, event);
  9076. }
  9077. if(guild_id>0)
  9078. guild_npc_request_info(guild_id,event);
  9079. return 0;
  9080. }
  9081. /// Returns the number of cards that have been compounded onto the specified equipped item.
  9082. /// getequipcardcnt(<equipment slot>);
  9083. BUILDIN_FUNC(getequipcardcnt)
  9084. {
  9085. int i=-1,j,num;
  9086. TBL_PC *sd;
  9087. int count;
  9088. num=script_getnum(st,2);
  9089. sd=script_rid2sd(st);
  9090. if (num > 0 && num <= ARRAYLENGTH(equip))
  9091. i=pc_checkequip(sd,equip[num-1]);
  9092. if (i < 0 || !sd->inventory_data[i]) {
  9093. script_pushint(st,0);
  9094. return 0;
  9095. }
  9096. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9097. {
  9098. script_pushint(st,0);
  9099. return 0;
  9100. }
  9101. count = 0;
  9102. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  9103. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  9104. count++;
  9105. script_pushint(st,count);
  9106. return 0;
  9107. }
  9108. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  9109. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  9110. /// successremovecards <slot>;
  9111. BUILDIN_FUNC(successremovecards)
  9112. {
  9113. int i=-1,j,c,cardflag=0;
  9114. TBL_PC* sd = script_rid2sd(st);
  9115. int num = script_getnum(st,2);
  9116. if (num > 0 && num <= ARRAYLENGTH(equip))
  9117. i=pc_checkequip(sd,equip[num-1]);
  9118. if (i < 0 || !sd->inventory_data[i]) {
  9119. return 0;
  9120. }
  9121. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9122. return 0;
  9123. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9124. {
  9125. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9126. {// extract this card from the item
  9127. int flag;
  9128. struct item item_tmp;
  9129. cardflag = 1;
  9130. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9131. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9132. item_tmp.attribute=0,item_tmp.expire_time=0;
  9133. for (j = 0; j < MAX_SLOTS; j++)
  9134. item_tmp.card[j]=0;
  9135. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // 持てないならドロップ
  9136. clif_additem(sd,0,0,flag);
  9137. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9138. }
  9139. }
  9140. }
  9141. if(cardflag == 1)
  9142. { // カードを取り除いたアイテム所得
  9143. int flag;
  9144. struct item item_tmp;
  9145. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9146. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9147. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9148. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9149. item_tmp.card[j]=0;
  9150. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9151. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9152. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  9153. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // もてないならドロップ
  9154. clif_additem(sd,0,0,flag);
  9155. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9156. }
  9157. clif_misceffect(&sd->bl,3);
  9158. }
  9159. return 0;
  9160. }
  9161. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  9162. /// failedremovecards <slot>, <type>;
  9163. /// <type>=0 : will destroy both the item and the cards.
  9164. /// <type>=1 : will keep the item, but destroy the cards.
  9165. /// <type>=2 : will keep the cards, but destroy the item.
  9166. /// <type>=? : will just display the failure effect.
  9167. BUILDIN_FUNC(failedremovecards)
  9168. {
  9169. int i=-1,j,c,cardflag=0;
  9170. TBL_PC* sd = script_rid2sd(st);
  9171. int num = script_getnum(st,2);
  9172. int typefail = script_getnum(st,3);
  9173. if (num > 0 && num <= ARRAYLENGTH(equip))
  9174. i=pc_checkequip(sd,equip[num-1]);
  9175. if (i < 0 || !sd->inventory_data[i])
  9176. return 0;
  9177. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9178. return 0;
  9179. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9180. {
  9181. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9182. {
  9183. cardflag = 1;
  9184. if(typefail == 2)
  9185. {// add cards to inventory, clear
  9186. int flag;
  9187. struct item item_tmp;
  9188. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9189. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9190. item_tmp.attribute=0,item_tmp.expire_time=0;
  9191. for (j = 0; j < MAX_SLOTS; j++)
  9192. item_tmp.card[j]=0;
  9193. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  9194. clif_additem(sd,0,0,flag);
  9195. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9196. }
  9197. }
  9198. }
  9199. }
  9200. if(cardflag == 1)
  9201. {
  9202. if(typefail == 0 || typefail == 2){ // 武具損失
  9203. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  9204. }
  9205. if(typefail == 1){ // カードのみ損失(武具を返す)
  9206. int flag;
  9207. struct item item_tmp;
  9208. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9209. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9210. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9211. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9212. item_tmp.card[j]=0;
  9213. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9214. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9215. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  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. clif_misceffect(&sd->bl,2);
  9222. }
  9223. return 0;
  9224. }
  9225. /* ================================================================
  9226. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  9227. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  9228. * improved by [Lance]
  9229. * ================================================================*/
  9230. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  9231. {
  9232. int x,y,m,check_val=0,check_ID=0,i=0;
  9233. struct guild *g = NULL;
  9234. struct party_data *p = NULL;
  9235. const char *str;
  9236. const char *mapname;
  9237. unsigned int index;
  9238. mapname=script_getstr(st,2);
  9239. str=script_getstr(st,3);
  9240. x=script_getnum(st,4);
  9241. y=script_getnum(st,5);
  9242. if(script_hasdata(st,7)){
  9243. check_val=script_getnum(st,6);
  9244. check_ID=script_getnum(st,7);
  9245. }
  9246. if((m=map_mapname2mapid(mapname))< 0)
  9247. return 0;
  9248. if(!(index=mapindex_name2id(str)))
  9249. return 0;
  9250. switch(check_val){
  9251. case 1:
  9252. g = guild_search(check_ID);
  9253. if (g){
  9254. for( i=0; i < g->max_member; i++)
  9255. {
  9256. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9257. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  9258. }
  9259. }
  9260. }
  9261. break;
  9262. case 2:
  9263. p = party_search(check_ID);
  9264. if(p){
  9265. for(i=0;i<MAX_PARTY; i++){
  9266. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9267. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  9268. }
  9269. }
  9270. }
  9271. break;
  9272. default:
  9273. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  9274. break;
  9275. }
  9276. return 0;
  9277. }
  9278. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9279. {
  9280. char *event=va_arg(ap,char *);
  9281. struct mob_data *md = ((struct mob_data *)bl);
  9282. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  9283. return 1;
  9284. return 0;
  9285. }
  9286. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9287. {
  9288. const char *mapname,*event;
  9289. int m;
  9290. mapname=script_getstr(st,2);
  9291. event=script_getstr(st,3);
  9292. if( strcmp(event, "all") == 0 )
  9293. event = NULL;
  9294. else
  9295. check_event(st, event);
  9296. if( strcmp(mapname, "this") == 0 ) {
  9297. struct map_session_data *sd = script_rid2sd(st);
  9298. if( sd )
  9299. m = sd->bl.m;
  9300. else {
  9301. script_pushint(st,-1);
  9302. return 0;
  9303. }
  9304. }
  9305. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9306. script_pushint(st,-1);
  9307. return 0;
  9308. }
  9309. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9310. {
  9311. script_pushint(st,-1);
  9312. return 0;
  9313. }
  9314. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9315. return 0;
  9316. }
  9317. BUILDIN_FUNC(marriage)
  9318. {
  9319. const char *partner=script_getstr(st,2);
  9320. TBL_PC *sd=script_rid2sd(st);
  9321. TBL_PC *p_sd=map_nick2sd(partner);
  9322. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9323. script_pushint(st,0);
  9324. return 0;
  9325. }
  9326. script_pushint(st,1);
  9327. return 0;
  9328. }
  9329. BUILDIN_FUNC(wedding_effect)
  9330. {
  9331. TBL_PC *sd=script_rid2sd(st);
  9332. struct block_list *bl;
  9333. if(sd==NULL) {
  9334. bl=map_id2bl(st->oid);
  9335. } else
  9336. bl=&sd->bl;
  9337. clif_wedding_effect(bl);
  9338. return 0;
  9339. }
  9340. BUILDIN_FUNC(divorce)
  9341. {
  9342. TBL_PC *sd=script_rid2sd(st);
  9343. if(sd==NULL || pc_divorce(sd) < 0){
  9344. script_pushint(st,0);
  9345. return 0;
  9346. }
  9347. script_pushint(st,1);
  9348. return 0;
  9349. }
  9350. BUILDIN_FUNC(ispartneron)
  9351. {
  9352. TBL_PC *sd=script_rid2sd(st);
  9353. if(sd==NULL || !pc_ismarried(sd) ||
  9354. map_charid2sd(sd->status.partner_id) == NULL) {
  9355. script_pushint(st,0);
  9356. return 0;
  9357. }
  9358. script_pushint(st,1);
  9359. return 0;
  9360. }
  9361. BUILDIN_FUNC(getpartnerid)
  9362. {
  9363. TBL_PC *sd=script_rid2sd(st);
  9364. if (sd == NULL) {
  9365. script_pushint(st,0);
  9366. return 0;
  9367. }
  9368. script_pushint(st,sd->status.partner_id);
  9369. return 0;
  9370. }
  9371. BUILDIN_FUNC(getchildid)
  9372. {
  9373. TBL_PC *sd=script_rid2sd(st);
  9374. if (sd == NULL) {
  9375. script_pushint(st,0);
  9376. return 0;
  9377. }
  9378. script_pushint(st,sd->status.child);
  9379. return 0;
  9380. }
  9381. BUILDIN_FUNC(getmotherid)
  9382. {
  9383. TBL_PC *sd=script_rid2sd(st);
  9384. if (sd == NULL) {
  9385. script_pushint(st,0);
  9386. return 0;
  9387. }
  9388. script_pushint(st,sd->status.mother);
  9389. return 0;
  9390. }
  9391. BUILDIN_FUNC(getfatherid)
  9392. {
  9393. TBL_PC *sd=script_rid2sd(st);
  9394. if (sd == NULL) {
  9395. script_pushint(st,0);
  9396. return 0;
  9397. }
  9398. script_pushint(st,sd->status.father);
  9399. return 0;
  9400. }
  9401. BUILDIN_FUNC(warppartner)
  9402. {
  9403. int x,y;
  9404. unsigned short mapindex;
  9405. const char *str;
  9406. TBL_PC *sd=script_rid2sd(st);
  9407. TBL_PC *p_sd=NULL;
  9408. if(sd==NULL || !pc_ismarried(sd) ||
  9409. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9410. script_pushint(st,0);
  9411. return 0;
  9412. }
  9413. str=script_getstr(st,2);
  9414. x=script_getnum(st,3);
  9415. y=script_getnum(st,4);
  9416. mapindex = mapindex_name2id(str);
  9417. if (mapindex) {
  9418. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  9419. script_pushint(st,1);
  9420. } else
  9421. script_pushint(st,0);
  9422. return 0;
  9423. }
  9424. /*================================================
  9425. * Script for Displaying MOB Information [Valaris]
  9426. *------------------------------------------------*/
  9427. BUILDIN_FUNC(strmobinfo)
  9428. {
  9429. int num=script_getnum(st,2);
  9430. int class_=script_getnum(st,3);
  9431. if(!mobdb_checkid(class_))
  9432. {
  9433. script_pushint(st,0);
  9434. return 0;
  9435. }
  9436. switch (num) {
  9437. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9438. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9439. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9440. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9441. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9442. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9443. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9444. default:
  9445. script_pushint(st,0);
  9446. break;
  9447. }
  9448. return 0;
  9449. }
  9450. /*==========================================
  9451. * Summon guardians [Valaris]
  9452. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9453. *------------------------------------------*/
  9454. BUILDIN_FUNC(guardian)
  9455. {
  9456. int class_=0,x=0,y=0,guardian=0;
  9457. const char *str,*map,*evt="";
  9458. struct script_data *data;
  9459. bool has_index = false;
  9460. map =script_getstr(st,2);
  9461. x =script_getnum(st,3);
  9462. y =script_getnum(st,4);
  9463. str =script_getstr(st,5);
  9464. class_=script_getnum(st,6);
  9465. if( script_hasdata(st,8) )
  9466. {// "<event label>",<guardian index>
  9467. evt=script_getstr(st,7);
  9468. guardian=script_getnum(st,8);
  9469. has_index = true;
  9470. } else if( script_hasdata(st,7) ){
  9471. data=script_getdata(st,7);
  9472. get_val(st,data);
  9473. if( data_isstring(data) )
  9474. {// "<event label>"
  9475. evt=script_getstr(st,7);
  9476. } else if( data_isint(data) )
  9477. {// <guardian index>
  9478. guardian=script_getnum(st,7);
  9479. has_index = true;
  9480. } else {
  9481. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9482. script_reportdata(data);
  9483. return 1;
  9484. }
  9485. }
  9486. check_event(st, evt);
  9487. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9488. return 0;
  9489. }
  9490. /*==========================================
  9491. * Invisible Walls [Zephyrus]
  9492. *------------------------------------------*/
  9493. BUILDIN_FUNC(setwall)
  9494. {
  9495. const char *map, *name;
  9496. int x, y, m, size, dir;
  9497. bool shootable;
  9498. map = script_getstr(st,2);
  9499. x = script_getnum(st,3);
  9500. y = script_getnum(st,4);
  9501. size = script_getnum(st,5);
  9502. dir = script_getnum(st,6);
  9503. shootable = script_getnum(st,7);
  9504. name = script_getstr(st,8);
  9505. if( (m = map_mapname2mapid(map)) < 0 )
  9506. return 0; // Invalid Map
  9507. map_iwall_set(m, x, y, size, dir, shootable, name);
  9508. return 0;
  9509. }
  9510. BUILDIN_FUNC(delwall)
  9511. {
  9512. const char *name = script_getstr(st,2);
  9513. map_iwall_remove(name);
  9514. return 0;
  9515. }
  9516. /// Retrieves various information about the specified guardian.
  9517. ///
  9518. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9519. /// type: 0 - whether it is deployed or not
  9520. /// 1 - maximum hp
  9521. /// 2 - current hp
  9522. ///
  9523. BUILDIN_FUNC(guardianinfo)
  9524. {
  9525. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9526. int id = script_getnum(st,3);
  9527. int type = script_getnum(st,4);
  9528. struct guild_castle* gc = guild_mapname2gc(mapname);
  9529. struct mob_data* gd;
  9530. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9531. {
  9532. script_pushint(st,-1);
  9533. return 0;
  9534. }
  9535. if( type == 0 )
  9536. script_pushint(st, gc->guardian[id].visible);
  9537. else
  9538. if( !gc->guardian[id].visible )
  9539. script_pushint(st,-1);
  9540. else
  9541. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9542. script_pushint(st,-1);
  9543. else
  9544. {
  9545. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9546. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9547. else
  9548. script_pushint(st,-1);
  9549. }
  9550. return 0;
  9551. }
  9552. /*==========================================
  9553. * IDからItem名
  9554. *------------------------------------------*/
  9555. BUILDIN_FUNC(getitemname)
  9556. {
  9557. int item_id=0;
  9558. struct item_data *i_data;
  9559. char *item_name;
  9560. struct script_data *data;
  9561. data=script_getdata(st,2);
  9562. get_val(st,data);
  9563. if( data_isstring(data) ){
  9564. const char *name=conv_str(st,data);
  9565. struct item_data *item_data = itemdb_searchname(name);
  9566. if( item_data )
  9567. item_id=item_data->nameid;
  9568. }else
  9569. item_id=conv_num(st,data);
  9570. i_data = itemdb_exists(item_id);
  9571. if (i_data == NULL)
  9572. {
  9573. script_pushconststr(st,"null");
  9574. return 0;
  9575. }
  9576. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  9577. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9578. script_pushstr(st,item_name);
  9579. return 0;
  9580. }
  9581. /*==========================================
  9582. * Returns number of slots an item has. [Skotlex]
  9583. *------------------------------------------*/
  9584. BUILDIN_FUNC(getitemslots)
  9585. {
  9586. int item_id;
  9587. struct item_data *i_data;
  9588. item_id=script_getnum(st,2);
  9589. i_data = itemdb_exists(item_id);
  9590. if (i_data)
  9591. script_pushint(st,i_data->slot);
  9592. else
  9593. script_pushint(st,-1);
  9594. return 0;
  9595. }
  9596. // TODO: add matk here if needed/once we get rid of REMODE
  9597. /*==========================================
  9598. * Returns some values of an item [Lupus]
  9599. * Price, Weight, etc...
  9600. getiteminfo(itemID,n), where n
  9601. 0 value_buy;
  9602. 1 value_sell;
  9603. 2 type;
  9604. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9605. if = 0, then monsters don't drop it at all (rare or a quest item)
  9606. if = -1, then this item is sold in NPC shops only
  9607. 4 sex;
  9608. 5 equip;
  9609. 6 weight;
  9610. 7 atk;
  9611. 8 def;
  9612. 9 range;
  9613. 10 slot;
  9614. 11 look;
  9615. 12 elv;
  9616. 13 wlv;
  9617. 14 view id
  9618. *------------------------------------------*/
  9619. BUILDIN_FUNC(getiteminfo)
  9620. {
  9621. int item_id,n;
  9622. int *item_arr;
  9623. struct item_data *i_data;
  9624. item_id = script_getnum(st,2);
  9625. n = script_getnum(st,3);
  9626. i_data = itemdb_exists(item_id);
  9627. if (i_data && n>=0 && n<=14) {
  9628. item_arr = (int*)&i_data->value_buy;
  9629. script_pushint(st,item_arr[n]);
  9630. } else
  9631. script_pushint(st,-1);
  9632. return 0;
  9633. }
  9634. /*==========================================
  9635. * Set some values of an item [Lupus]
  9636. * Price, Weight, etc...
  9637. setiteminfo(itemID,n,Value), where n
  9638. 0 value_buy;
  9639. 1 value_sell;
  9640. 2 type;
  9641. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9642. if = 0, then monsters don't drop it at all (rare or a quest item)
  9643. if = -1, then this item is sold in NPC shops only
  9644. 4 sex;
  9645. 5 equip;
  9646. 6 weight;
  9647. 7 atk;
  9648. 8 def;
  9649. 9 range;
  9650. 10 slot;
  9651. 11 look;
  9652. 12 elv;
  9653. 13 wlv;
  9654. 14 view id
  9655. * Returns Value or -1 if the wrong field's been set
  9656. *------------------------------------------*/
  9657. BUILDIN_FUNC(setiteminfo)
  9658. {
  9659. int item_id,n,value;
  9660. int *item_arr;
  9661. struct item_data *i_data;
  9662. item_id = script_getnum(st,2);
  9663. n = script_getnum(st,3);
  9664. value = script_getnum(st,4);
  9665. i_data = itemdb_exists(item_id);
  9666. if (i_data && n>=0 && n<=14) {
  9667. item_arr = (int*)&i_data->value_buy;
  9668. item_arr[n] = value;
  9669. script_pushint(st,value);
  9670. } else
  9671. script_pushint(st,-1);
  9672. return 0;
  9673. }
  9674. /*==========================================
  9675. * Returns value from equipped item slot n [Lupus]
  9676. getequipcardid(num,slot)
  9677. where
  9678. num = eqip position slot
  9679. slot = 0,1,2,3 (Card Slot N)
  9680. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9681. it's useful when you want to check item cards or if it's signed
  9682. Useful for such quests as "Sign this refined item with players name" etc
  9683. Hat[0] +4 -> Player's Hat[0] +4
  9684. *------------------------------------------*/
  9685. BUILDIN_FUNC(getequipcardid)
  9686. {
  9687. int i=-1,num,slot;
  9688. TBL_PC *sd;
  9689. num=script_getnum(st,2);
  9690. slot=script_getnum(st,3);
  9691. sd=script_rid2sd(st);
  9692. if (num > 0 && num <= ARRAYLENGTH(equip))
  9693. i=pc_checkequip(sd,equip[num-1]);
  9694. if(i >= 0 && slot>=0 && slot<4)
  9695. script_pushint(st,sd->status.inventory[i].card[slot]);
  9696. else
  9697. script_pushint(st,0);
  9698. return 0;
  9699. }
  9700. /*==========================================
  9701. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9702. *------------------------------------------*/
  9703. BUILDIN_FUNC(petskillbonus)
  9704. {
  9705. struct pet_data *pd;
  9706. TBL_PC *sd=script_rid2sd(st);
  9707. if(sd==NULL || sd->pd==NULL)
  9708. return 0;
  9709. pd=sd->pd;
  9710. if (pd->bonus)
  9711. { //Clear previous bonus
  9712. if (pd->bonus->timer != INVALID_TIMER)
  9713. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9714. } else //init
  9715. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9716. pd->bonus->type=script_getnum(st,2);
  9717. pd->bonus->val=script_getnum(st,3);
  9718. pd->bonus->duration=script_getnum(st,4);
  9719. pd->bonus->delay=script_getnum(st,5);
  9720. if (pd->state.skillbonus == 1)
  9721. pd->state.skillbonus=0; // waiting state
  9722. // wait for timer to start
  9723. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9724. pd->bonus->timer = INVALID_TIMER;
  9725. else
  9726. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9727. return 0;
  9728. }
  9729. /*==========================================
  9730. * pet looting [Valaris] //Rewritten by [Skotlex]
  9731. *------------------------------------------*/
  9732. BUILDIN_FUNC(petloot)
  9733. {
  9734. int max;
  9735. struct pet_data *pd;
  9736. TBL_PC *sd=script_rid2sd(st);
  9737. if(sd==NULL || sd->pd==NULL)
  9738. return 0;
  9739. max=script_getnum(st,2);
  9740. if(max < 1)
  9741. max = 1; //Let'em loot at least 1 item.
  9742. else if (max > MAX_PETLOOT_SIZE)
  9743. max = MAX_PETLOOT_SIZE;
  9744. pd = sd->pd;
  9745. if (pd->loot != NULL)
  9746. { //Release whatever was there already and reallocate memory
  9747. pet_lootitem_drop(pd, pd->msd);
  9748. aFree(pd->loot->item);
  9749. }
  9750. else
  9751. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9752. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9753. pd->loot->max=max;
  9754. pd->loot->count = 0;
  9755. pd->loot->weight = 0;
  9756. return 0;
  9757. }
  9758. /*==========================================
  9759. * PCの所持品情報読み取り
  9760. *------------------------------------------*/
  9761. BUILDIN_FUNC(getinventorylist)
  9762. {
  9763. TBL_PC *sd=script_rid2sd(st);
  9764. char card_var[NAME_LENGTH];
  9765. int i,j=0,k;
  9766. if(!sd) return 0;
  9767. for(i=0;i<MAX_INVENTORY;i++){
  9768. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9769. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  9770. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  9771. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  9772. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  9773. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  9774. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  9775. for (k = 0; k < MAX_SLOTS; k++)
  9776. {
  9777. sprintf(card_var, "@inventorylist_card%d",k+1);
  9778. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  9779. }
  9780. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  9781. j++;
  9782. }
  9783. }
  9784. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9785. return 0;
  9786. }
  9787. BUILDIN_FUNC(getskilllist)
  9788. {
  9789. TBL_PC *sd=script_rid2sd(st);
  9790. int i,j=0;
  9791. if(!sd) return 0;
  9792. for(i=0;i<MAX_SKILL;i++){
  9793. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9794. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  9795. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  9796. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  9797. j++;
  9798. }
  9799. }
  9800. pc_setreg(sd,add_str("@skilllist_count"),j);
  9801. return 0;
  9802. }
  9803. BUILDIN_FUNC(clearitem)
  9804. {
  9805. TBL_PC *sd=script_rid2sd(st);
  9806. int i;
  9807. if(sd==NULL) return 0;
  9808. for (i=0; i<MAX_INVENTORY; i++) {
  9809. if (sd->status.inventory[i].amount) {
  9810. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  9811. }
  9812. }
  9813. return 0;
  9814. }
  9815. /*==========================================
  9816. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9817. *------------------------------------------*/
  9818. BUILDIN_FUNC(disguise)
  9819. {
  9820. int id;
  9821. TBL_PC* sd = script_rid2sd(st);
  9822. if (sd == NULL) return 0;
  9823. id = script_getnum(st,2);
  9824. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9825. pc_disguise(sd, id);
  9826. script_pushint(st,id);
  9827. } else
  9828. script_pushint(st,0);
  9829. return 0;
  9830. }
  9831. /*==========================================
  9832. * Undisguise Player (returns 1 if success, 0 on fail)
  9833. *------------------------------------------*/
  9834. BUILDIN_FUNC(undisguise)
  9835. {
  9836. TBL_PC* sd = script_rid2sd(st);
  9837. if (sd == NULL) return 0;
  9838. if (sd->disguise) {
  9839. pc_disguise(sd, 0);
  9840. script_pushint(st,0);
  9841. } else {
  9842. script_pushint(st,1);
  9843. }
  9844. return 0;
  9845. }
  9846. /*==========================================
  9847. * NPCクラスチェンジ
  9848. * classは変わりたいclass
  9849. * typeは通常0なのかな?
  9850. *------------------------------------------*/
  9851. BUILDIN_FUNC(classchange)
  9852. {
  9853. int _class,type;
  9854. struct block_list *bl=map_id2bl(st->oid);
  9855. if(bl==NULL) return 0;
  9856. _class=script_getnum(st,2);
  9857. type=script_getnum(st,3);
  9858. clif_class_change(bl,_class,type);
  9859. return 0;
  9860. }
  9861. /*==========================================
  9862. * NPCから発生するエフェクト
  9863. *------------------------------------------*/
  9864. BUILDIN_FUNC(misceffect)
  9865. {
  9866. int type;
  9867. type=script_getnum(st,2);
  9868. if(st->oid && st->oid != fake_nd->bl.id) {
  9869. struct block_list *bl = map_id2bl(st->oid);
  9870. if (bl)
  9871. clif_specialeffect(bl,type,AREA);
  9872. } else{
  9873. TBL_PC *sd=script_rid2sd(st);
  9874. if(sd)
  9875. clif_specialeffect(&sd->bl,type,AREA);
  9876. }
  9877. return 0;
  9878. }
  9879. /*==========================================
  9880. * Play a BGM on a single client [Rikter/Yommy]
  9881. *------------------------------------------*/
  9882. BUILDIN_FUNC(playBGM)
  9883. {
  9884. const char* name;
  9885. struct map_session_data* sd;
  9886. if( ( sd = script_rid2sd(st) ) != NULL )
  9887. {
  9888. name = script_getstr(st,2);
  9889. clif_playBGM(sd, name);
  9890. }
  9891. return 0;
  9892. }
  9893. static int playBGM_sub(struct block_list* bl,va_list ap)
  9894. {
  9895. const char* name = va_arg(ap,const char*);
  9896. clif_playBGM(BL_CAST(BL_PC, bl), name);
  9897. return 0;
  9898. }
  9899. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  9900. {
  9901. const char* name = va_arg(args, const char*);
  9902. clif_playBGM(sd, name);
  9903. return 0;
  9904. }
  9905. /*==========================================
  9906. * Play a BGM on multiple client [Rikter/Yommy]
  9907. *------------------------------------------*/
  9908. BUILDIN_FUNC(playBGMall)
  9909. {
  9910. const char* name;
  9911. name = script_getstr(st,2);
  9912. if( script_hasdata(st,7) )
  9913. {// specified part of map
  9914. const char* map = script_getstr(st,3);
  9915. int x0 = script_getnum(st,4);
  9916. int y0 = script_getnum(st,5);
  9917. int x1 = script_getnum(st,6);
  9918. int y1 = script_getnum(st,7);
  9919. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  9920. }
  9921. else if( script_hasdata(st,3) )
  9922. {// entire map
  9923. const char* map = script_getstr(st,3);
  9924. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  9925. }
  9926. else
  9927. {// entire server
  9928. map_foreachpc(&playBGM_foreachpc_sub, name);
  9929. }
  9930. return 0;
  9931. }
  9932. /*==========================================
  9933. * サウンドエフェクト
  9934. *------------------------------------------*/
  9935. BUILDIN_FUNC(soundeffect)
  9936. {
  9937. TBL_PC* sd = script_rid2sd(st);
  9938. const char* name = script_getstr(st,2);
  9939. int type = script_getnum(st,3);
  9940. if(sd)
  9941. {
  9942. clif_soundeffect(sd,&sd->bl,name,type);
  9943. }
  9944. return 0;
  9945. }
  9946. int soundeffect_sub(struct block_list* bl,va_list ap)
  9947. {
  9948. char* name = va_arg(ap,char*);
  9949. int type = va_arg(ap,int);
  9950. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9951. return 0;
  9952. }
  9953. /*==========================================
  9954. * Play a sound effect (.wav) on multiple clients
  9955. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9956. *------------------------------------------*/
  9957. BUILDIN_FUNC(soundeffectall)
  9958. {
  9959. struct block_list* bl;
  9960. const char* name;
  9961. int type;
  9962. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9963. if (!bl)
  9964. return 0;
  9965. name = script_getstr(st,2);
  9966. type = script_getnum(st,3);
  9967. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9968. if(!script_hasdata(st,4))
  9969. { // area around
  9970. clif_soundeffectall(bl, name, type, AREA);
  9971. }
  9972. else
  9973. if(!script_hasdata(st,5))
  9974. { // entire map
  9975. const char* map = script_getstr(st,4);
  9976. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9977. }
  9978. else
  9979. if(script_hasdata(st,8))
  9980. { // specified part of map
  9981. const char* map = script_getstr(st,4);
  9982. int x0 = script_getnum(st,5);
  9983. int y0 = script_getnum(st,6);
  9984. int x1 = script_getnum(st,7);
  9985. int y1 = script_getnum(st,8);
  9986. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9987. }
  9988. else
  9989. {
  9990. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9991. }
  9992. return 0;
  9993. }
  9994. /*==========================================
  9995. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9996. *------------------------------------------*/
  9997. BUILDIN_FUNC(petrecovery)
  9998. {
  9999. struct pet_data *pd;
  10000. TBL_PC *sd=script_rid2sd(st);
  10001. if(sd==NULL || sd->pd==NULL)
  10002. return 0;
  10003. pd=sd->pd;
  10004. if (pd->recovery)
  10005. { //Halt previous bonus
  10006. if (pd->recovery->timer != INVALID_TIMER)
  10007. delete_timer(pd->recovery->timer, pet_recovery_timer);
  10008. } else //Init
  10009. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  10010. pd->recovery->type = (sc_type)script_getnum(st,2);
  10011. pd->recovery->delay = script_getnum(st,3);
  10012. pd->recovery->timer = INVALID_TIMER;
  10013. return 0;
  10014. }
  10015. /*==========================================
  10016. * pet healing [Valaris] //Rewritten by [Skotlex]
  10017. *------------------------------------------*/
  10018. BUILDIN_FUNC(petheal)
  10019. {
  10020. struct pet_data *pd;
  10021. TBL_PC *sd=script_rid2sd(st);
  10022. if(sd==NULL || sd->pd==NULL)
  10023. return 0;
  10024. pd=sd->pd;
  10025. if (pd->s_skill)
  10026. { //Clear previous skill
  10027. if (pd->s_skill->timer != INVALID_TIMER)
  10028. {
  10029. if (pd->s_skill->id)
  10030. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10031. else
  10032. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10033. }
  10034. } else //init memory
  10035. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10036. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  10037. //Use the lv as the amount to heal
  10038. pd->s_skill->lv=script_getnum(st,2);
  10039. pd->s_skill->delay=script_getnum(st,3);
  10040. pd->s_skill->hp=script_getnum(st,4);
  10041. pd->s_skill->sp=script_getnum(st,5);
  10042. //Use delay as initial offset to avoid skill/heal exploits
  10043. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10044. pd->s_skill->timer = INVALID_TIMER;
  10045. else
  10046. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  10047. return 0;
  10048. }
  10049. /*==========================================
  10050. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  10051. *------------------------------------------*/
  10052. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  10053. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  10054. BUILDIN_FUNC(petskillattack)
  10055. {
  10056. struct pet_data *pd;
  10057. TBL_PC *sd=script_rid2sd(st);
  10058. if(sd==NULL || sd->pd==NULL)
  10059. return 0;
  10060. pd=sd->pd;
  10061. if (pd->a_skill == NULL)
  10062. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10063. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10064. pd->a_skill->lv=script_getnum(st,3);
  10065. pd->a_skill->div_ = 0;
  10066. pd->a_skill->rate=script_getnum(st,4);
  10067. pd->a_skill->bonusrate=script_getnum(st,5);
  10068. return 0;
  10069. }
  10070. /*==========================================
  10071. * pet attack skills [Valaris]
  10072. *------------------------------------------*/
  10073. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  10074. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  10075. BUILDIN_FUNC(petskillattack2)
  10076. {
  10077. struct pet_data *pd;
  10078. TBL_PC *sd=script_rid2sd(st);
  10079. if(sd==NULL || sd->pd==NULL)
  10080. return 0;
  10081. pd=sd->pd;
  10082. if (pd->a_skill == NULL)
  10083. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10084. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10085. pd->a_skill->lv=script_getnum(st,3);
  10086. pd->a_skill->div_ = script_getnum(st,4);
  10087. pd->a_skill->rate=script_getnum(st,5);
  10088. pd->a_skill->bonusrate=script_getnum(st,6);
  10089. return 0;
  10090. }
  10091. /*==========================================
  10092. * pet support skills [Skotlex]
  10093. *------------------------------------------*/
  10094. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  10095. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  10096. BUILDIN_FUNC(petskillsupport)
  10097. {
  10098. struct pet_data *pd;
  10099. TBL_PC *sd=script_rid2sd(st);
  10100. if(sd==NULL || sd->pd==NULL)
  10101. return 0;
  10102. pd=sd->pd;
  10103. if (pd->s_skill)
  10104. { //Clear previous skill
  10105. if (pd->s_skill->timer != INVALID_TIMER)
  10106. {
  10107. if (pd->s_skill->id)
  10108. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10109. else
  10110. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10111. }
  10112. } else //init memory
  10113. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10114. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10115. pd->s_skill->lv=script_getnum(st,3);
  10116. pd->s_skill->delay=script_getnum(st,4);
  10117. pd->s_skill->hp=script_getnum(st,5);
  10118. pd->s_skill->sp=script_getnum(st,6);
  10119. //Use delay as initial offset to avoid skill/heal exploits
  10120. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10121. pd->s_skill->timer = INVALID_TIMER;
  10122. else
  10123. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  10124. return 0;
  10125. }
  10126. /*==========================================
  10127. * Scripted skill effects [Celest]
  10128. *------------------------------------------*/
  10129. /// skilleffect <skill id>,<level>
  10130. /// skilleffect "<skill name>",<level>
  10131. BUILDIN_FUNC(skilleffect)
  10132. {
  10133. TBL_PC *sd;
  10134. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10135. int skilllv=script_getnum(st,3);
  10136. sd=script_rid2sd(st);
  10137. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  10138. return 0;
  10139. }
  10140. /*==========================================
  10141. * NPC skill effects [Valaris]
  10142. *------------------------------------------*/
  10143. /// npcskilleffect <skill id>,<level>,<x>,<y>
  10144. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  10145. BUILDIN_FUNC(npcskilleffect)
  10146. {
  10147. struct block_list *bl= map_id2bl(st->oid);
  10148. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10149. int skilllv=script_getnum(st,3);
  10150. int x=script_getnum(st,4);
  10151. int y=script_getnum(st,5);
  10152. if (bl)
  10153. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  10154. return 0;
  10155. }
  10156. /*==========================================
  10157. * Special effects [Valaris]
  10158. *------------------------------------------*/
  10159. BUILDIN_FUNC(specialeffect)
  10160. {
  10161. struct block_list *bl=map_id2bl(st->oid);
  10162. int type = script_getnum(st,2);
  10163. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10164. if(bl==NULL)
  10165. return 0;
  10166. if( script_hasdata(st,4) )
  10167. {
  10168. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10169. if(nd)
  10170. clif_specialeffect(&nd->bl, type, target);
  10171. }
  10172. else
  10173. {
  10174. if (target == SELF) {
  10175. TBL_PC *sd=script_rid2sd(st);
  10176. if (sd)
  10177. clif_specialeffect_single(bl,type,sd->fd);
  10178. } else {
  10179. clif_specialeffect(bl, type, target);
  10180. }
  10181. }
  10182. return 0;
  10183. }
  10184. BUILDIN_FUNC(specialeffect2)
  10185. {
  10186. TBL_PC *sd=script_rid2sd(st);
  10187. int type = script_getnum(st,2);
  10188. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10189. if( script_hasdata(st,4) )
  10190. sd = map_nick2sd(script_getstr(st,4));
  10191. if (sd)
  10192. clif_specialeffect(&sd->bl, type, target);
  10193. return 0;
  10194. }
  10195. /*==========================================
  10196. * Nude [Valaris]
  10197. *------------------------------------------*/
  10198. BUILDIN_FUNC(nude)
  10199. {
  10200. TBL_PC *sd=script_rid2sd(st);
  10201. int i,calcflag=0;
  10202. if(sd==NULL)
  10203. return 0;
  10204. for(i=0;i<11;i++)
  10205. if(sd->equip_index[i] >= 0) {
  10206. if(!calcflag)
  10207. calcflag=1;
  10208. pc_unequipitem(sd,sd->equip_index[i],2);
  10209. }
  10210. if(calcflag)
  10211. status_calc_pc(sd,0);
  10212. return 0;
  10213. }
  10214. /*==========================================
  10215. * gmcommand [MouseJstr]
  10216. *------------------------------------------*/
  10217. BUILDIN_FUNC(atcommand)
  10218. {
  10219. TBL_PC dummy_sd;
  10220. TBL_PC* sd;
  10221. int fd;
  10222. const char* cmd;
  10223. cmd = script_getstr(st,2);
  10224. if (st->rid) {
  10225. sd = script_rid2sd(st);
  10226. fd = sd->fd;
  10227. } else { //Use a dummy character.
  10228. sd = &dummy_sd;
  10229. fd = 0;
  10230. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10231. if (st->oid)
  10232. {
  10233. struct block_list* bl = map_id2bl(st->oid);
  10234. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10235. if (bl->type == BL_NPC)
  10236. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10237. }
  10238. }
  10239. if (!is_atcommand(fd, sd, cmd, 0)) {
  10240. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  10241. script_reportsrc(st);
  10242. return 1;
  10243. }
  10244. return 0;
  10245. }
  10246. /*==========================================
  10247. * Displays a message for the player only (like system messages like "you got an apple" )
  10248. *------------------------------------------*/
  10249. BUILDIN_FUNC(dispbottom)
  10250. {
  10251. TBL_PC *sd=script_rid2sd(st);
  10252. const char *message;
  10253. message=script_getstr(st,2);
  10254. if(sd)
  10255. clif_disp_onlyself(sd,message,(int)strlen(message));
  10256. return 0;
  10257. }
  10258. /*==========================================
  10259. * All The Players Full Recovery
  10260. * (HP/SP full restore and resurrect if need)
  10261. *------------------------------------------*/
  10262. BUILDIN_FUNC(recovery)
  10263. {
  10264. TBL_PC* sd;
  10265. struct s_mapiterator* iter;
  10266. iter = mapit_getallusers();
  10267. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10268. {
  10269. if(pc_isdead(sd))
  10270. status_revive(&sd->bl, 100, 100);
  10271. else
  10272. status_percent_heal(&sd->bl, 100, 100);
  10273. clif_displaymessage(sd->fd,"You have been recovered!");
  10274. }
  10275. mapit_free(iter);
  10276. return 0;
  10277. }
  10278. /*==========================================
  10279. * Get your pet info: getpetinfo(n)
  10280. * n -> 0:pet_id 1:pet_class 2:pet_name
  10281. * 3:friendly 4:hungry, 5: rename flag.
  10282. *------------------------------------------*/
  10283. BUILDIN_FUNC(getpetinfo)
  10284. {
  10285. TBL_PC *sd=script_rid2sd(st);
  10286. TBL_PET *pd;
  10287. int type=script_getnum(st,2);
  10288. if(!sd || !sd->pd) {
  10289. if (type == 2)
  10290. script_pushconststr(st,"null");
  10291. else
  10292. script_pushint(st,0);
  10293. return 0;
  10294. }
  10295. pd = sd->pd;
  10296. switch(type){
  10297. case 0: script_pushint(st,pd->pet.pet_id); break;
  10298. case 1: script_pushint(st,pd->pet.class_); break;
  10299. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10300. case 3: script_pushint(st,pd->pet.intimate); break;
  10301. case 4: script_pushint(st,pd->pet.hungry); break;
  10302. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10303. default:
  10304. script_pushint(st,0);
  10305. break;
  10306. }
  10307. return 0;
  10308. }
  10309. /*==========================================
  10310. * Get your homunculus info: gethominfo(n)
  10311. * n -> 0:hom_id 1:class 2:name
  10312. * 3:friendly 4:hungry, 5: rename flag.
  10313. * 6: level
  10314. *------------------------------------------*/
  10315. BUILDIN_FUNC(gethominfo)
  10316. {
  10317. TBL_PC *sd=script_rid2sd(st);
  10318. TBL_HOM *hd;
  10319. int type=script_getnum(st,2);
  10320. hd = sd?sd->hd:NULL;
  10321. if(!merc_is_hom_active(hd))
  10322. {
  10323. if (type == 2)
  10324. script_pushconststr(st,"null");
  10325. else
  10326. script_pushint(st,0);
  10327. return 0;
  10328. }
  10329. switch(type){
  10330. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10331. case 1: script_pushint(st,hd->homunculus.class_); break;
  10332. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10333. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10334. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10335. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10336. case 6: script_pushint(st,hd->homunculus.level); break;
  10337. default:
  10338. script_pushint(st,0);
  10339. break;
  10340. }
  10341. return 0;
  10342. }
  10343. /// Retrieves information about character's mercenary
  10344. /// getmercinfo <type>[,<char id>];
  10345. BUILDIN_FUNC(getmercinfo)
  10346. {
  10347. int type, char_id;
  10348. struct map_session_data* sd;
  10349. struct mercenary_data* md;
  10350. type = script_getnum(st,2);
  10351. if( script_hasdata(st,3) )
  10352. {
  10353. char_id = script_getnum(st,3);
  10354. if( ( sd = map_charid2sd(char_id) ) == NULL )
  10355. {
  10356. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  10357. script_pushnil(st);
  10358. return 1;
  10359. }
  10360. }
  10361. else
  10362. {
  10363. if( ( sd = script_rid2sd(st) ) == NULL )
  10364. {
  10365. script_pushnil(st);
  10366. return 0;
  10367. }
  10368. }
  10369. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  10370. switch( type )
  10371. {
  10372. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  10373. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  10374. case 2:
  10375. if( md )
  10376. script_pushstrcopy(st,md->db->name);
  10377. else
  10378. script_pushconststr(st,"");
  10379. break;
  10380. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  10381. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  10382. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  10383. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  10384. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  10385. default:
  10386. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  10387. script_pushnil(st);
  10388. return 1;
  10389. }
  10390. return 0;
  10391. }
  10392. /*==========================================
  10393. * Shows wether your inventory(and equips) contain
  10394. selected card or not.
  10395. checkequipedcard(4001);
  10396. *------------------------------------------*/
  10397. BUILDIN_FUNC(checkequipedcard)
  10398. {
  10399. TBL_PC *sd=script_rid2sd(st);
  10400. if(sd){
  10401. int n,i,c=0;
  10402. c=script_getnum(st,2);
  10403. for(i=0;i<MAX_INVENTORY;i++){
  10404. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10405. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10406. continue;
  10407. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10408. if(sd->status.inventory[i].card[n]==c){
  10409. script_pushint(st,1);
  10410. return 0;
  10411. }
  10412. }
  10413. }
  10414. }
  10415. }
  10416. script_pushint(st,0);
  10417. return 0;
  10418. }
  10419. BUILDIN_FUNC(jump_zero)
  10420. {
  10421. int sel;
  10422. sel=script_getnum(st,2);
  10423. if(!sel) {
  10424. int pos;
  10425. if( !data_islabel(script_getdata(st,3)) ){
  10426. ShowError("script: jump_zero: not label !\n");
  10427. st->state=END;
  10428. return 1;
  10429. }
  10430. pos=script_getnum(st,3);
  10431. st->pos=pos;
  10432. st->state=GOTO;
  10433. }
  10434. return 0;
  10435. }
  10436. /*==========================================
  10437. * movenpc [MouseJstr]
  10438. *------------------------------------------*/
  10439. BUILDIN_FUNC(movenpc)
  10440. {
  10441. TBL_NPC *nd = NULL;
  10442. const char *npc;
  10443. int x,y;
  10444. npc = script_getstr(st,2);
  10445. x = script_getnum(st,3);
  10446. y = script_getnum(st,4);
  10447. if ((nd = npc_name2id(npc)) == NULL)
  10448. return -1;
  10449. if (script_hasdata(st,5))
  10450. nd->ud.dir = script_getnum(st,5) % 8;
  10451. npc_movenpc(nd, x, y);
  10452. return 0;
  10453. }
  10454. /*==========================================
  10455. * message [MouseJstr]
  10456. *------------------------------------------*/
  10457. BUILDIN_FUNC(message)
  10458. {
  10459. const char *msg,*player;
  10460. TBL_PC *pl_sd = NULL;
  10461. player = script_getstr(st,2);
  10462. msg = script_getstr(st,3);
  10463. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10464. return 0;
  10465. clif_displaymessage(pl_sd->fd, msg);
  10466. return 0;
  10467. }
  10468. /*==========================================
  10469. * npctalk (sends message to surrounding area)
  10470. *------------------------------------------*/
  10471. BUILDIN_FUNC(npctalk)
  10472. {
  10473. const char* str;
  10474. char name[NAME_LENGTH], message[256];
  10475. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10476. str = script_getstr(st,2);
  10477. if(nd)
  10478. {
  10479. safestrncpy(name, nd->name, sizeof(name));
  10480. strtok(name, "#"); // discard extra name identifier if present
  10481. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  10482. clif_message(&nd->bl, message);
  10483. }
  10484. return 0;
  10485. }
  10486. // change npc walkspeed [Valaris]
  10487. BUILDIN_FUNC(npcspeed)
  10488. {
  10489. struct npc_data* nd;
  10490. int speed;
  10491. speed = script_getnum(st,2);
  10492. nd =(struct npc_data *)map_id2bl(st->oid);
  10493. if( nd )
  10494. {
  10495. nd->speed = speed;
  10496. nd->ud.state.speed_changed = 1;
  10497. }
  10498. return 0;
  10499. }
  10500. // make an npc walk to a position [Valaris]
  10501. BUILDIN_FUNC(npcwalkto)
  10502. {
  10503. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10504. int x=0,y=0;
  10505. x=script_getnum(st,2);
  10506. y=script_getnum(st,3);
  10507. if(nd) {
  10508. unit_walktoxy(&nd->bl,x,y,0);
  10509. }
  10510. return 0;
  10511. }
  10512. // stop an npc's movement [Valaris]
  10513. BUILDIN_FUNC(npcstop)
  10514. {
  10515. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10516. if(nd) {
  10517. unit_stop_walking(&nd->bl,1|4);
  10518. }
  10519. return 0;
  10520. }
  10521. /*==========================================
  10522. * getlook char info. getlook(arg)
  10523. *------------------------------------------*/
  10524. BUILDIN_FUNC(getlook)
  10525. {
  10526. int type,val;
  10527. TBL_PC *sd;
  10528. sd=script_rid2sd(st);
  10529. type=script_getnum(st,2);
  10530. val=-1;
  10531. switch(type) {
  10532. case LOOK_HAIR: val=sd->status.hair; break; //1
  10533. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10534. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10535. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10536. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10537. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10538. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10539. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10540. case LOOK_SHOES: break; //9
  10541. }
  10542. script_pushint(st,val);
  10543. return 0;
  10544. }
  10545. /*==========================================
  10546. * get char save point. argument: 0- map name, 1- x, 2- y
  10547. *------------------------------------------*/
  10548. BUILDIN_FUNC(getsavepoint)
  10549. {
  10550. TBL_PC* sd;
  10551. int type;
  10552. sd = script_rid2sd(st);
  10553. if (sd == NULL) {
  10554. script_pushint(st,0);
  10555. return 0;
  10556. }
  10557. type = script_getnum(st,2);
  10558. switch(type) {
  10559. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10560. case 1: script_pushint(st,sd->status.save_point.x); break;
  10561. case 2: script_pushint(st,sd->status.save_point.y); break;
  10562. default:
  10563. script_pushint(st,0);
  10564. break;
  10565. }
  10566. return 0;
  10567. }
  10568. /*==========================================
  10569. * Get position for char/npc/pet/mob objects. Added by Lorky
  10570. *
  10571. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10572. * where type:
  10573. * MapName$ - String variable for output map name
  10574. * MapX - Integer variable for output coord X
  10575. * MapY - Integer variable for output coord Y
  10576. * type - type of object
  10577. * 0 - Character coord
  10578. * 1 - NPC coord
  10579. * 2 - Pet coord
  10580. * 3 - Mob coord (not released)
  10581. * 4 - Homun coord
  10582. * CharName$ - Name object. If miss or "this" the current object
  10583. *
  10584. * Return:
  10585. * 0 - success
  10586. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10587. *------------------------------------------*/
  10588. BUILDIN_FUNC(getmapxy)
  10589. {
  10590. struct block_list *bl = NULL;
  10591. TBL_PC *sd=NULL;
  10592. int num;
  10593. const char *name;
  10594. char prefix;
  10595. int x,y,type;
  10596. char mapname[MAP_NAME_LENGTH];
  10597. if( !data_isreference(script_getdata(st,2)) ){
  10598. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10599. script_pushint(st,-1);
  10600. return 1;
  10601. }
  10602. if( !data_isreference(script_getdata(st,3)) ){
  10603. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10604. script_pushint(st,-1);
  10605. return 1;
  10606. }
  10607. if( !data_isreference(script_getdata(st,4)) ){
  10608. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10609. script_pushint(st,-1);
  10610. return 1;
  10611. }
  10612. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10613. type=script_getnum(st,5);
  10614. switch (type){
  10615. case 0: //Get Character Position
  10616. if( script_hasdata(st,6) )
  10617. sd=map_nick2sd(script_getstr(st,6));
  10618. else
  10619. sd=script_rid2sd(st);
  10620. if (sd)
  10621. bl = &sd->bl;
  10622. break;
  10623. case 1: //Get NPC Position
  10624. if( script_hasdata(st,6) )
  10625. {
  10626. struct npc_data *nd;
  10627. nd=npc_name2id(script_getstr(st,6));
  10628. if (nd)
  10629. bl = &nd->bl;
  10630. } else //In case the origin is not an npc?
  10631. bl=map_id2bl(st->oid);
  10632. break;
  10633. case 2: //Get Pet Position
  10634. if(script_hasdata(st,6))
  10635. sd=map_nick2sd(script_getstr(st,6));
  10636. else
  10637. sd=script_rid2sd(st);
  10638. if (sd && sd->pd)
  10639. bl = &sd->pd->bl;
  10640. break;
  10641. case 3: //Get Mob Position
  10642. break; //Not supported?
  10643. case 4: //Get Homun Position
  10644. if(script_hasdata(st,6))
  10645. sd=map_nick2sd(script_getstr(st,6));
  10646. else
  10647. sd=script_rid2sd(st);
  10648. if (sd && sd->hd)
  10649. bl = &sd->hd->bl;
  10650. break;
  10651. default:
  10652. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  10653. script_pushint(st,-1);
  10654. return 1;
  10655. }
  10656. if (!bl) { //No object found.
  10657. script_pushint(st,-1);
  10658. return 0;
  10659. }
  10660. x= bl->x;
  10661. y= bl->y;
  10662. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10663. //Set MapName$
  10664. num=st->stack->stack_data[st->start+2].u.num;
  10665. name=get_str(num&0x00ffffff);
  10666. prefix=*name;
  10667. if(not_server_variable(prefix))
  10668. sd=script_rid2sd(st);
  10669. else
  10670. sd=NULL;
  10671. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10672. //Set MapX
  10673. num=st->stack->stack_data[st->start+3].u.num;
  10674. name=get_str(num&0x00ffffff);
  10675. prefix=*name;
  10676. if(not_server_variable(prefix))
  10677. sd=script_rid2sd(st);
  10678. else
  10679. sd=NULL;
  10680. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  10681. //Set MapY
  10682. num=st->stack->stack_data[st->start+4].u.num;
  10683. name=get_str(num&0x00ffffff);
  10684. prefix=*name;
  10685. if(not_server_variable(prefix))
  10686. sd=script_rid2sd(st);
  10687. else
  10688. sd=NULL;
  10689. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  10690. //Return Success value
  10691. script_pushint(st,0);
  10692. return 0;
  10693. }
  10694. /*==========================================
  10695. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10696. *------------------------------------------*/
  10697. BUILDIN_FUNC(logmes)
  10698. {
  10699. const char *str;
  10700. TBL_PC* sd;
  10701. sd = script_rid2sd(st);
  10702. if( sd == NULL )
  10703. return 1;
  10704. str = script_getstr(st,2);
  10705. log_npc(sd,str);
  10706. return 0;
  10707. }
  10708. BUILDIN_FUNC(summon)
  10709. {
  10710. int _class, timeout=0;
  10711. const char *str,*event="";
  10712. TBL_PC *sd;
  10713. struct mob_data *md;
  10714. int tick = gettick();
  10715. sd=script_rid2sd(st);
  10716. if (!sd) return 0;
  10717. str =script_getstr(st,2);
  10718. _class=script_getnum(st,3);
  10719. if( script_hasdata(st,4) )
  10720. timeout=script_getnum(st,4);
  10721. if( script_hasdata(st,5) ){
  10722. event=script_getstr(st,5);
  10723. check_event(st, event);
  10724. }
  10725. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10726. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10727. if (md) {
  10728. md->master_id=sd->bl.id;
  10729. md->special_state.ai=1;
  10730. if( md->deletetimer != INVALID_TIMER )
  10731. delete_timer(md->deletetimer, mob_timer_delete);
  10732. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10733. mob_spawn (md); //Now it is ready for spawning.
  10734. clif_specialeffect(&md->bl,344,AREA);
  10735. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10736. }
  10737. return 0;
  10738. }
  10739. /*==========================================
  10740. * Checks whether it is daytime/nighttime
  10741. *------------------------------------------*/
  10742. BUILDIN_FUNC(isnight)
  10743. {
  10744. script_pushint(st,(night_flag == 1));
  10745. return 0;
  10746. }
  10747. BUILDIN_FUNC(isday)
  10748. {
  10749. script_pushint(st,(night_flag == 0));
  10750. return 0;
  10751. }
  10752. /*================================================
  10753. * Check how many items/cards in the list are
  10754. * equipped - used for 2/15's cards patch [celest]
  10755. *------------------------------------------------*/
  10756. BUILDIN_FUNC(isequippedcnt)
  10757. {
  10758. TBL_PC *sd;
  10759. int i, j, k, id = 1;
  10760. int ret = 0;
  10761. sd = script_rid2sd(st);
  10762. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10763. script_pushint(st,0);
  10764. return 0;
  10765. }
  10766. for (i=0; id!=0; i++) {
  10767. FETCH (i+2, id) else id = 0;
  10768. if (id <= 0)
  10769. continue;
  10770. for (j=0; j<EQI_MAX; j++) {
  10771. int index;
  10772. index = sd->equip_index[j];
  10773. if(index < 0) continue;
  10774. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10775. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10776. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10777. if(!sd->inventory_data[index])
  10778. continue;
  10779. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10780. if (sd->inventory_data[index]->nameid == id)
  10781. ret+= sd->status.inventory[index].amount;
  10782. } else { //Count cards.
  10783. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10784. continue; //No cards
  10785. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10786. if (sd->status.inventory[index].card[k] == id)
  10787. ret++; //[Lupus]
  10788. }
  10789. }
  10790. }
  10791. }
  10792. script_pushint(st,ret);
  10793. return 0;
  10794. }
  10795. /*================================================
  10796. * Check whether another card has been
  10797. * equipped - used for 2/15's cards patch [celest]
  10798. * -- Items checked cannot be reused in another
  10799. * card set to prevent exploits
  10800. *------------------------------------------------*/
  10801. BUILDIN_FUNC(isequipped)
  10802. {
  10803. TBL_PC *sd;
  10804. int i, j, k, id = 1;
  10805. int index, flag;
  10806. int ret = -1;
  10807. //Original hash to reverse it when full check fails.
  10808. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10809. sd = script_rid2sd(st);
  10810. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10811. script_pushint(st,0);
  10812. return 0;
  10813. }
  10814. setitem_hash = sd->setitem_hash;
  10815. setitem_hash2 = sd->setitem_hash2;
  10816. for (i=0; id!=0; i++)
  10817. {
  10818. FETCH (i+2, id) else id = 0;
  10819. if (id <= 0)
  10820. continue;
  10821. flag = 0;
  10822. for (j=0; j<EQI_MAX; j++)
  10823. {
  10824. index = sd->equip_index[j];
  10825. if(index < 0) continue;
  10826. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10827. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10828. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10829. if(!sd->inventory_data[index])
  10830. continue;
  10831. if (itemdb_type(id) != IT_CARD) {
  10832. if (sd->inventory_data[index]->nameid != id)
  10833. continue;
  10834. flag = 1;
  10835. break;
  10836. } else { //Cards
  10837. if (sd->inventory_data[index]->slot == 0 ||
  10838. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10839. continue;
  10840. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10841. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10842. unsigned int hash = 0;
  10843. if (sd->status.inventory[index].card[k] != id)
  10844. continue;
  10845. hash = 1<<((j<5?j:j-5)*4 + k);
  10846. // check if card is already used by another set
  10847. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10848. continue;
  10849. // We have found a match
  10850. flag = 1;
  10851. // Set hash so this card cannot be used by another
  10852. if (j<5)
  10853. sd->setitem_hash |= hash;
  10854. else
  10855. sd->setitem_hash2 |= hash;
  10856. break;
  10857. }
  10858. }
  10859. if (flag) break; //Card found
  10860. }
  10861. if (ret == -1)
  10862. ret = flag;
  10863. else
  10864. ret &= flag;
  10865. if (!ret) break;
  10866. }
  10867. if (!ret)
  10868. { //When check fails, restore original hash values. [Skotlex]
  10869. sd->setitem_hash = setitem_hash;
  10870. sd->setitem_hash2 = setitem_hash2;
  10871. }
  10872. script_pushint(st,ret);
  10873. return 0;
  10874. }
  10875. /*================================================
  10876. * Check how many given inserted cards in the CURRENT
  10877. * weapon - used for 2/15's cards patch [Lupus]
  10878. *------------------------------------------------*/
  10879. BUILDIN_FUNC(cardscnt)
  10880. {
  10881. TBL_PC *sd;
  10882. int i, k, id = 1;
  10883. int ret = 0;
  10884. int index;
  10885. sd = script_rid2sd(st);
  10886. for (i=0; id!=0; i++) {
  10887. FETCH (i+2, id) else id = 0;
  10888. if (id <= 0)
  10889. continue;
  10890. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10891. if(index < 0) continue;
  10892. if(!sd->inventory_data[index])
  10893. continue;
  10894. if(itemdb_type(id) != IT_CARD) {
  10895. if (sd->inventory_data[index]->nameid == id)
  10896. ret+= sd->status.inventory[index].amount;
  10897. } else {
  10898. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10899. continue;
  10900. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10901. if (sd->status.inventory[index].card[k] == id)
  10902. ret++;
  10903. }
  10904. }
  10905. }
  10906. script_pushint(st,ret);
  10907. // script_pushint(st,current_equip_item_index);
  10908. return 0;
  10909. }
  10910. /*=======================================================
  10911. * Returns the refined number of the current item, or an
  10912. * item with inventory index specified
  10913. *-------------------------------------------------------*/
  10914. BUILDIN_FUNC(getrefine)
  10915. {
  10916. TBL_PC *sd;
  10917. if ((sd = script_rid2sd(st))!= NULL)
  10918. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10919. else
  10920. script_pushint(st,0);
  10921. return 0;
  10922. }
  10923. /*=======================================================
  10924. * Day/Night controls
  10925. *-------------------------------------------------------*/
  10926. BUILDIN_FUNC(night)
  10927. {
  10928. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10929. return 0;
  10930. }
  10931. BUILDIN_FUNC(day)
  10932. {
  10933. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10934. return 0;
  10935. }
  10936. //=======================================================
  10937. // Unequip [Spectre]
  10938. //-------------------------------------------------------
  10939. BUILDIN_FUNC(unequip)
  10940. {
  10941. int i;
  10942. size_t num;
  10943. TBL_PC *sd;
  10944. num = script_getnum(st,2);
  10945. sd = script_rid2sd(st);
  10946. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10947. {
  10948. i = pc_checkequip(sd,equip[num-1]);
  10949. if (i >= 0)
  10950. pc_unequipitem(sd,i,1|2);
  10951. }
  10952. return 0;
  10953. }
  10954. BUILDIN_FUNC(equip)
  10955. {
  10956. int nameid=0,i;
  10957. TBL_PC *sd;
  10958. struct item_data *item_data;
  10959. sd = script_rid2sd(st);
  10960. nameid=script_getnum(st,2);
  10961. if((item_data = itemdb_exists(nameid)) == NULL)
  10962. {
  10963. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10964. return 1;
  10965. }
  10966. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10967. if( i < MAX_INVENTORY )
  10968. pc_equipitem(sd,i,item_data->equip);
  10969. return 0;
  10970. }
  10971. BUILDIN_FUNC(autoequip)
  10972. {
  10973. int nameid, flag;
  10974. struct item_data *item_data;
  10975. nameid=script_getnum(st,2);
  10976. flag=script_getnum(st,3);
  10977. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  10978. {
  10979. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  10980. return 1;
  10981. }
  10982. if( !itemdb_isequip2(item_data) )
  10983. {
  10984. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  10985. return 1;
  10986. }
  10987. item_data->flag.autoequip = flag>0?1:0;
  10988. return 0;
  10989. }
  10990. BUILDIN_FUNC(setbattleflag)
  10991. {
  10992. const char *flag, *value;
  10993. flag = script_getstr(st,2);
  10994. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  10995. if (battle_set_value(flag, value) == 0)
  10996. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10997. else
  10998. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10999. return 0;
  11000. }
  11001. BUILDIN_FUNC(getbattleflag)
  11002. {
  11003. const char *flag;
  11004. flag = script_getstr(st,2);
  11005. script_pushint(st,battle_get_value(flag));
  11006. return 0;
  11007. }
  11008. //=======================================================
  11009. // strlen [Valaris]
  11010. //-------------------------------------------------------
  11011. BUILDIN_FUNC(getstrlen)
  11012. {
  11013. const char *str = script_getstr(st,2);
  11014. int len = (str) ? (int)strlen(str) : 0;
  11015. script_pushint(st,len);
  11016. return 0;
  11017. }
  11018. //=======================================================
  11019. // isalpha [Valaris]
  11020. //-------------------------------------------------------
  11021. BUILDIN_FUNC(charisalpha)
  11022. {
  11023. const char *str=script_getstr(st,2);
  11024. int pos=script_getnum(st,3);
  11025. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  11026. script_pushint(st,val);
  11027. return 0;
  11028. }
  11029. //=======================================================
  11030. // charisupper <str>, <index>
  11031. //-------------------------------------------------------
  11032. BUILDIN_FUNC(charisupper)
  11033. {
  11034. const char *str = script_getstr(st,2);
  11035. int pos = script_getnum(st,3);
  11036. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  11037. script_pushint(st,val);
  11038. return 0;
  11039. }
  11040. //=======================================================
  11041. // charislower <str>, <index>
  11042. //-------------------------------------------------------
  11043. BUILDIN_FUNC(charislower)
  11044. {
  11045. const char *str = script_getstr(st,2);
  11046. int pos = script_getnum(st,3);
  11047. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  11048. script_pushint(st,val);
  11049. return 0;
  11050. }
  11051. //=======================================================
  11052. // charat <str>, <index>
  11053. //-------------------------------------------------------
  11054. BUILDIN_FUNC(charat)
  11055. {
  11056. const char *str = script_getstr(st,2);
  11057. int pos = script_getnum(st,3);
  11058. char *output;
  11059. output = (char*)aMalloc(2*sizeof(char));
  11060. output[0] = '\0';
  11061. if(str && pos >= 0 && (unsigned int)pos < strlen(str))
  11062. sprintf(output, "%c", str[pos]);
  11063. script_pushstr(st, output);
  11064. return 0;
  11065. }
  11066. //=======================================================
  11067. // setchar <string>, <char>, <index>
  11068. //-------------------------------------------------------
  11069. BUILDIN_FUNC(setchar)
  11070. {
  11071. const char *str = script_getstr(st,2);
  11072. const char *c = script_getstr(st,3);
  11073. int index = script_getnum(st,4);
  11074. char *output = aStrdup(str);
  11075. if(index >= 0 && index < strlen(output))
  11076. output[index] = *c;
  11077. script_pushstr(st, output);
  11078. return 0;
  11079. }
  11080. //=======================================================
  11081. // insertchar <string>, <char>, <index>
  11082. //-------------------------------------------------------
  11083. BUILDIN_FUNC(insertchar)
  11084. {
  11085. const char *str = script_getstr(st,2);
  11086. const char *c = script_getstr(st,3);
  11087. int index = script_getnum(st,4);
  11088. char *output;
  11089. size_t len = strlen(str);
  11090. if(index < 0)
  11091. index = 0;
  11092. else if(index > len)
  11093. index = len;
  11094. output = (char*)aMalloc(len + 2);
  11095. memcpy(output, str, index);
  11096. output[index] = c[0];
  11097. memcpy(&output[index+1], &str[index], len - index);
  11098. output[len+1] = '\0';
  11099. script_pushstr(st, output);
  11100. return 0;
  11101. }
  11102. //=======================================================
  11103. // delchar <string>, <index>
  11104. //-------------------------------------------------------
  11105. BUILDIN_FUNC(delchar)
  11106. {
  11107. const char *str = script_getstr(st,2);
  11108. int index = script_getnum(st,3);
  11109. char *output;
  11110. size_t len = strlen(str);
  11111. if(index < 0 || index > len) {
  11112. //return original
  11113. output = aStrdup(str);
  11114. script_pushstr(st, output);
  11115. return 0;
  11116. }
  11117. output = (char*)aMalloc(len);
  11118. memcpy(output, str, index);
  11119. memcpy(&output[index], &str[index+1], len - index);
  11120. script_pushstr(st, output);
  11121. return 0;
  11122. }
  11123. //=======================================================
  11124. // strtoupper <str>
  11125. //-------------------------------------------------------
  11126. BUILDIN_FUNC(strtoupper)
  11127. {
  11128. const char *str = script_getstr(st,2);
  11129. char *output = aStrdup(str);
  11130. char *cursor = output;
  11131. while (*cursor != '\0') {
  11132. *cursor = TOUPPER(*cursor);
  11133. cursor++;
  11134. }
  11135. script_pushstr(st, output);
  11136. return 0;
  11137. }
  11138. //=======================================================
  11139. // strtolower <str>
  11140. //-------------------------------------------------------
  11141. BUILDIN_FUNC(strtolower)
  11142. {
  11143. const char *str = script_getstr(st,2);
  11144. char *output = aStrdup(str);
  11145. char *cursor = output;
  11146. while (*cursor != '\0') {
  11147. *cursor = TOLOWER(*cursor);
  11148. cursor++;
  11149. }
  11150. script_pushstr(st, output);
  11151. return 0;
  11152. }
  11153. //=======================================================
  11154. // substr <str>, <start>, <end>
  11155. //-------------------------------------------------------
  11156. BUILDIN_FUNC(substr)
  11157. {
  11158. const char *str = script_getstr(st,2);
  11159. char *output;
  11160. int start = script_getnum(st,3);
  11161. int end = script_getnum(st,4);
  11162. int len = 0;
  11163. if(start >= 0 && end < strlen(str) && start <= end) {
  11164. len = end - start + 1;
  11165. output = (char*)aMalloc(len + 1);
  11166. memcpy(output, &str[start], len);
  11167. } else
  11168. output = (char*)aMalloc(1);
  11169. output[len] = '\0';
  11170. script_pushstr(st, output);
  11171. return 0;
  11172. }
  11173. //=======================================================
  11174. // explode <dest_string_array>, <str>, <delimiter>
  11175. // Note: delimiter is limited to 1 char
  11176. //-------------------------------------------------------
  11177. BUILDIN_FUNC(explode)
  11178. {
  11179. struct script_data* data = script_getdata(st, 2);
  11180. const char *str = script_getstr(st,3);
  11181. const char delimiter = script_getstr(st, 4)[0];
  11182. int32 id;
  11183. size_t len = strlen(str);
  11184. int i = 0, j = 0;
  11185. int start;
  11186. char *temp;
  11187. const char* name;
  11188. TBL_PC* sd = NULL;
  11189. temp = (char*)aMalloc(len + 1);
  11190. if( !data_isreference(data) )
  11191. {
  11192. ShowError("script:explode: not a variable\n");
  11193. script_reportdata(data);
  11194. st->state = END;
  11195. return 1;// not a variable
  11196. }
  11197. id = reference_getid(data);
  11198. start = reference_getindex(data);
  11199. name = reference_getname(data);
  11200. if( not_array_variable(*name) )
  11201. {
  11202. ShowError("script:explode: illegal scope\n");
  11203. script_reportdata(data);
  11204. st->state = END;
  11205. return 1;// not supported
  11206. }
  11207. if( !is_string_variable(name) )
  11208. {
  11209. ShowError("script:explode: not string array\n");
  11210. script_reportdata(data);
  11211. st->state = END;
  11212. return 1;// data type mismatch
  11213. }
  11214. if( not_server_variable(*name) )
  11215. {
  11216. sd = script_rid2sd(st);
  11217. if( sd == NULL )
  11218. return 0;// no player attached
  11219. }
  11220. while(str[i] != '\0') {
  11221. if(str[i] == delimiter && start < 127) { //break at delimiter but ignore after reaching last array index
  11222. temp[j] = '\0';
  11223. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  11224. j = 0;
  11225. ++i;
  11226. } else {
  11227. temp[j++] = str[i++];
  11228. }
  11229. }
  11230. //set last string
  11231. temp[j] = '\0';
  11232. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  11233. aFree(temp);
  11234. return 0;
  11235. }
  11236. //=======================================================
  11237. // implode <string_array>
  11238. // implode <string_array>, <glue>
  11239. //-------------------------------------------------------
  11240. BUILDIN_FUNC(implode)
  11241. {
  11242. struct script_data* data = script_getdata(st, 2);
  11243. const char *glue = NULL, *name, *temp;
  11244. int32 glue_len = 0, array_size, id;
  11245. size_t len = 0;
  11246. int i, k = 0;
  11247. TBL_PC* sd = NULL;
  11248. char *output;
  11249. if( !data_isreference(data) )
  11250. {
  11251. ShowError("script:implode: not a variable\n");
  11252. script_reportdata(data);
  11253. st->state = END;
  11254. return 1;// not a variable
  11255. }
  11256. id = reference_getid(data);
  11257. name = reference_getname(data);
  11258. if( not_array_variable(*name) )
  11259. {
  11260. ShowError("script:implode: illegal scope\n");
  11261. script_reportdata(data);
  11262. st->state = END;
  11263. return 1;// not supported
  11264. }
  11265. if( !is_string_variable(name) )
  11266. {
  11267. ShowError("script:implode: not string array\n");
  11268. script_reportdata(data);
  11269. st->state = END;
  11270. return 1;// data type mismatch
  11271. }
  11272. if( not_server_variable(*name) )
  11273. {
  11274. sd = script_rid2sd(st);
  11275. if( sd == NULL )
  11276. return 0;// no player attached
  11277. }
  11278. //count chars
  11279. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  11280. if(array_size == -1) //empty array check (AmsTaff)
  11281. {
  11282. ShowWarning("script:implode: array length = 0\n");
  11283. output = (char*)aMalloc(sizeof(char)*5);
  11284. sprintf(output,"%s","NULL");
  11285. } else {
  11286. for(i = 0; i <= array_size; ++i) {
  11287. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11288. len += strlen(temp);
  11289. script_removetop(st, -1, 0);
  11290. }
  11291. //allocate mem
  11292. if( script_hasdata(st,3) ) {
  11293. glue = script_getstr(st,3);
  11294. glue_len = strlen(glue);
  11295. len += glue_len * (array_size);
  11296. }
  11297. output = (char*)aMalloc(len + 1);
  11298. //build output
  11299. for(i = 0; i < array_size; ++i) {
  11300. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11301. len = strlen(temp);
  11302. memcpy(&output[k], temp, len);
  11303. k += len;
  11304. if(glue_len != 0) {
  11305. memcpy(&output[k], glue, glue_len);
  11306. k += glue_len;
  11307. }
  11308. script_removetop(st, -1, 0);
  11309. }
  11310. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  11311. len = strlen(temp);
  11312. memcpy(&output[k], temp, len);
  11313. k += len;
  11314. script_removetop(st, -1, 0);
  11315. output[k] = '\0';
  11316. }
  11317. script_pushstr(st, output);
  11318. return 0;
  11319. }
  11320. //=======================================================
  11321. // sprintf(<format>, ...);
  11322. // Implements C sprintf, except format %n. The resulting string is
  11323. // returned, instead of being saved in variable by reference.
  11324. //-------------------------------------------------------
  11325. BUILDIN_FUNC(sprintf)
  11326. {
  11327. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  11328. const char* format;
  11329. char* p;
  11330. char* q;
  11331. char* buf = NULL;
  11332. char* buf2 = NULL;
  11333. struct script_data* data;
  11334. StringBuf final_buf;
  11335. // Fetch init data
  11336. format = script_getstr(st, 2);
  11337. argc = script_lastdata(st)-2;
  11338. len = strlen(format);
  11339. // Skip parsing, where no parsing is required.
  11340. if(len==0){
  11341. script_pushconststr(st,"");
  11342. return 0;
  11343. }
  11344. // Pessimistic alloc
  11345. CREATE(buf, char, len+1);
  11346. // Need not be parsed, just solve stuff like %%.
  11347. if(argc==0){
  11348. memcpy(buf,format,len+1);
  11349. script_pushstrcopy(st, buf);
  11350. aFree(buf);
  11351. return 0;
  11352. }
  11353. safestrncpy(buf, format, len+1);
  11354. // Issue sprintf for each parameter
  11355. StringBuf_Init(&final_buf);
  11356. q = buf;
  11357. while((p = strchr(q, '%'))!=NULL){
  11358. if(p!=q){
  11359. len = p-q+1;
  11360. if(buf2_len<len){
  11361. RECREATE(buf2, char, len);
  11362. buf2_len = len;
  11363. }
  11364. safestrncpy(buf2, q, len);
  11365. StringBuf_AppendStr(&final_buf, buf2);
  11366. q = p;
  11367. }
  11368. p = q+1;
  11369. if(*p=='%'){ // %%
  11370. StringBuf_AppendStr(&final_buf, "%");
  11371. q+=2;
  11372. continue;
  11373. }
  11374. if(*p=='n'){ // %n
  11375. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  11376. script_reportsrc(st);
  11377. q+=2;
  11378. continue;
  11379. }
  11380. if(arg>=argc){
  11381. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  11382. if(buf) aFree(buf);
  11383. if(buf2) aFree(buf2);
  11384. StringBuf_Destroy(&final_buf);
  11385. script_pushconststr(st,"");
  11386. return 1;
  11387. }
  11388. if((p = strchr(q+1, '%'))==NULL){
  11389. p = strchr(q, 0); // EOS
  11390. }
  11391. len = p-q+1;
  11392. if(buf2_len<len){
  11393. RECREATE(buf2, char, len);
  11394. buf2_len = len;
  11395. }
  11396. safestrncpy(buf2, q, len);
  11397. q = p;
  11398. // Note: This assumes the passed value being the correct
  11399. // type to the current format specifier. If not, the server
  11400. // probably crashes or returns anything else, than expected,
  11401. // but it would behave in normal code the same way so it's
  11402. // the scripter's responsibility.
  11403. data = script_getdata(st, arg+3);
  11404. if(data_isstring(data)){ // String
  11405. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11406. }else if(data_isint(data)){ // Number
  11407. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11408. }else if(data_isreference(data)){ // Variable
  11409. char* name = reference_getname(data);
  11410. if(name[strlen(name)-1]=='$'){ // var Str
  11411. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11412. }else{ // var Int
  11413. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11414. }
  11415. }else{ // Unsupported type
  11416. ShowError("buildin_sprintf: Unknown argument type!\n");
  11417. if(buf) aFree(buf);
  11418. if(buf2) aFree(buf2);
  11419. StringBuf_Destroy(&final_buf);
  11420. script_pushconststr(st,"");
  11421. return 1;
  11422. }
  11423. arg++;
  11424. }
  11425. // Append anything left
  11426. if(*q){
  11427. StringBuf_AppendStr(&final_buf, q);
  11428. }
  11429. // Passed more, than needed
  11430. if(arg<argc){
  11431. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  11432. script_reportsrc(st);
  11433. }
  11434. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  11435. if(buf) aFree(buf);
  11436. if(buf2) aFree(buf2);
  11437. StringBuf_Destroy(&final_buf);
  11438. return 0;
  11439. }
  11440. //=======================================================
  11441. // sscanf(<str>, <format>, ...);
  11442. // Implements C sscanf.
  11443. //-------------------------------------------------------
  11444. BUILDIN_FUNC(sscanf){
  11445. unsigned int argc, arg = 0, len;
  11446. struct script_data* data;
  11447. struct map_session_data* sd = NULL;
  11448. const char* str;
  11449. const char* format;
  11450. const char* p;
  11451. const char* q;
  11452. char* buf = NULL;
  11453. char* buf_p;
  11454. char* ref_str = NULL;
  11455. int ref_int;
  11456. // Get data
  11457. str = script_getstr(st, 2);
  11458. format = script_getstr(st, 3);
  11459. argc = script_lastdata(st)-3;
  11460. len = strlen(format);
  11461. CREATE(buf, char, len*2+1);
  11462. // Issue sscanf for each parameter
  11463. *buf = 0;
  11464. q = format;
  11465. while((p = strchr(q, '%'))){
  11466. if(p!=q){
  11467. strncat(buf, q, (size_t)(p-q));
  11468. q = p;
  11469. }
  11470. p = q+1;
  11471. if(*p=='*' || *p=='%'){ // Skip
  11472. strncat(buf, q, 2);
  11473. q+=2;
  11474. continue;
  11475. }
  11476. if(arg>=argc){
  11477. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  11478. script_pushint(st, -1);
  11479. if(buf) aFree(buf);
  11480. if(ref_str) aFree(ref_str);
  11481. return 1;
  11482. }
  11483. if((p = strchr(q+1, '%'))==NULL){
  11484. p = strchr(q, 0); // EOS
  11485. }
  11486. len = p-q;
  11487. strncat(buf, q, len);
  11488. q = p;
  11489. // Validate output
  11490. data = script_getdata(st, arg+4);
  11491. if(!data_isreference(data) || !reference_tovariable(data)){
  11492. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  11493. script_pushint(st, -1);
  11494. if(buf) aFree(buf);
  11495. if(ref_str) aFree(ref_str);
  11496. return 1;
  11497. }
  11498. buf_p = reference_getname(data);
  11499. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  11500. script_pushint(st, -1);
  11501. if(buf) aFree(buf);
  11502. if(ref_str) aFree(ref_str);
  11503. return 0;
  11504. }
  11505. // Save value if any
  11506. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  11507. if(ref_str==NULL){
  11508. CREATE(ref_str, char, strlen(str)+1);
  11509. }
  11510. if(sscanf(str, buf, ref_str)==0){
  11511. break;
  11512. }
  11513. set_reg(st, sd, add_str(buf_p), buf_p, (void *)(ref_str), reference_getref(data));
  11514. }else{ // Number
  11515. if(sscanf(str, buf, &ref_int)==0){
  11516. break;
  11517. }
  11518. set_reg(st, sd, add_str(buf_p), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  11519. }
  11520. arg++;
  11521. // Disable used format (%... -> %*...)
  11522. buf_p = strchr(buf, 0);
  11523. memmove(buf_p-len+2, buf_p-len+1, len);
  11524. *(buf_p-len+1) = '*';
  11525. }
  11526. // Passed more, than needed
  11527. if(arg<argc){
  11528. ShowWarning("buildin_sscanf: Unused arguments passed.\n");
  11529. script_reportsrc(st);
  11530. }
  11531. script_pushint(st, arg);
  11532. if(buf) aFree(buf);
  11533. if(ref_str) aFree(ref_str);
  11534. return 0;
  11535. }
  11536. //=======================================================
  11537. // strpos(<haystack>, <needle>)
  11538. // strpos(<haystack>, <needle>, <offset>)
  11539. //
  11540. // Implements PHP style strpos. Adapted from code from
  11541. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  11542. //-------------------------------------------------------
  11543. BUILDIN_FUNC(strpos) {
  11544. const char *haystack = script_getstr(st,2);
  11545. const char *needle = script_getstr(st,3);
  11546. int i;
  11547. size_t len;
  11548. if( script_hasdata(st,4) )
  11549. i = script_getnum(st,4);
  11550. else
  11551. i = 0;
  11552. if ( strlen(needle) == 0 ) {
  11553. script_pushint(st, -1);
  11554. return 0;
  11555. }
  11556. len = strlen(haystack);
  11557. for ( ; i < len; ++i ) {
  11558. if ( haystack[i] == *needle ) {
  11559. // matched starting char -- loop through remaining chars
  11560. const char *h, *n;
  11561. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  11562. if ( *h != *n ) {
  11563. break;
  11564. }
  11565. }
  11566. if ( !*n ) { // matched all of 'needle' to null termination
  11567. script_pushint(st, i);
  11568. return 0;
  11569. }
  11570. }
  11571. }
  11572. script_pushint(st, -1);
  11573. return 0;
  11574. }
  11575. //===============================================================
  11576. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  11577. //
  11578. // Note: Finds all instances of <search> in <input> and replaces
  11579. // with <replace>. If specified will only replace as many
  11580. // instances as specified in <count>. By default will be case
  11581. // sensitive.
  11582. //---------------------------------------------------------------
  11583. BUILDIN_FUNC(replacestr)
  11584. {
  11585. const char *input = script_getstr(st, 2);
  11586. const char *find = script_getstr(st, 3);
  11587. const char *replace = script_getstr(st, 4);
  11588. size_t inputlen = strlen(input);
  11589. size_t findlen = strlen(find);
  11590. struct StringBuf output;
  11591. bool usecase = true;
  11592. int count = 0;
  11593. int numFinds = 0;
  11594. int i = 0, f = 0;
  11595. if(findlen == 0) {
  11596. ShowError("script:replacestr: Invalid search length.\n");
  11597. st->state = END;
  11598. return 1;
  11599. }
  11600. if(script_hasdata(st, 5)) {
  11601. if(script_isint(st,5))
  11602. usecase = script_getnum(st, 5) != 0;
  11603. else {
  11604. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  11605. st->state = END;
  11606. return 1;
  11607. }
  11608. }
  11609. if(script_hasdata(st, 6)) {
  11610. if(script_isint(st,6))
  11611. count = script_getnum(st, 6);
  11612. else {
  11613. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  11614. st->state = END;
  11615. return 1;
  11616. }
  11617. }
  11618. StringBuf_Init(&output);
  11619. for(; i < inputlen; i++) {
  11620. if(count && count == numFinds) { //found enough, stop looking
  11621. break;
  11622. }
  11623. for(f = 0; f <= findlen; f++) {
  11624. if(f == findlen) { //complete match
  11625. numFinds++;
  11626. StringBuf_AppendStr(&output, replace);
  11627. i += findlen - 1;
  11628. break;
  11629. } else {
  11630. if(usecase) {
  11631. if((i + f) > inputlen || input[i + f] != find[f]) {
  11632. StringBuf_Printf(&output, "%c", input[i]);
  11633. break;
  11634. }
  11635. } else {
  11636. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11637. StringBuf_Printf(&output, "%c", input[i]);
  11638. break;
  11639. }
  11640. }
  11641. }
  11642. }
  11643. }
  11644. //append excess after enough found
  11645. if(i < inputlen)
  11646. StringBuf_AppendStr(&output, &(input[i]));
  11647. script_pushstrcopy(st, StringBuf_Value(&output));
  11648. StringBuf_Destroy(&output);
  11649. return 0;
  11650. }
  11651. //========================================================
  11652. // countstr <input>, <search>{, <usecase>}
  11653. //
  11654. // Note: Counts the number of times <search> occurs in
  11655. // <input>. By default will be case sensitive.
  11656. //--------------------------------------------------------
  11657. BUILDIN_FUNC(countstr)
  11658. {
  11659. const char *input = script_getstr(st, 2);
  11660. const char *find = script_getstr(st, 3);
  11661. size_t inputlen = strlen(input);
  11662. size_t findlen = strlen(find);
  11663. bool usecase = true;
  11664. int numFinds = 0;
  11665. int i = 0, f = 0;
  11666. if(findlen == 0) {
  11667. ShowError("script:countstr: Invalid search length.\n");
  11668. st->state = END;
  11669. return 1;
  11670. }
  11671. if(script_hasdata(st, 4)) {
  11672. if(script_isint(st,4))
  11673. usecase = script_getnum(st, 4) != 0;
  11674. else {
  11675. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  11676. st->state = END;
  11677. return 1;
  11678. }
  11679. }
  11680. for(; i < inputlen; i++) {
  11681. for(f = 0; f <= findlen; f++) {
  11682. if(f == findlen) { //complete match
  11683. numFinds++;
  11684. i += findlen - 1;
  11685. break;
  11686. } else {
  11687. if(usecase) {
  11688. if((i + f) > inputlen || input[i + f] != find[f]) {
  11689. break;
  11690. }
  11691. } else {
  11692. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11693. break;
  11694. }
  11695. }
  11696. }
  11697. }
  11698. }
  11699. script_pushint(st, numFinds);
  11700. return 0;
  11701. }
  11702. /// Changes the display name and/or display class of the npc.
  11703. /// Returns 0 is successful, 1 if the npc does not exist.
  11704. ///
  11705. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  11706. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  11707. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  11708. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  11709. BUILDIN_FUNC(setnpcdisplay)
  11710. {
  11711. const char* name;
  11712. const char* newname = NULL;
  11713. int class_ = -1, size = -1;
  11714. struct script_data* data;
  11715. struct npc_data* nd;
  11716. name = script_getstr(st,2);
  11717. data = script_getdata(st,3);
  11718. if( script_hasdata(st,4) )
  11719. class_ = script_getnum(st,4);
  11720. if( script_hasdata(st,5) )
  11721. size = script_getnum(st,5);
  11722. get_val(st, data);
  11723. if( data_isstring(data) )
  11724. newname = conv_str(st,data);
  11725. else if( data_isint(data) )
  11726. class_ = conv_num(st,data);
  11727. else
  11728. {
  11729. ShowError("script:setnpcdisplay: expected a string or number\n");
  11730. script_reportdata(data);
  11731. return 1;
  11732. }
  11733. nd = npc_name2id(name);
  11734. if( nd == NULL )
  11735. {// not found
  11736. script_pushint(st,1);
  11737. return 0;
  11738. }
  11739. // update npc
  11740. if( newname )
  11741. npc_setdisplayname(nd, newname);
  11742. if( size != -1 && size != (int)nd->size )
  11743. nd->size = size;
  11744. else
  11745. size = -1;
  11746. if( class_ != -1 && nd->class_ != class_ )
  11747. npc_setclass(nd, class_);
  11748. else if( size != -1 )
  11749. { // Required to update the visual size
  11750. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  11751. clif_spawn(&nd->bl);
  11752. }
  11753. script_pushint(st,0);
  11754. return 0;
  11755. }
  11756. BUILDIN_FUNC(atoi)
  11757. {
  11758. const char *value;
  11759. value = script_getstr(st,2);
  11760. script_pushint(st,atoi(value));
  11761. return 0;
  11762. }
  11763. // case-insensitive substring search [lordalfa]
  11764. BUILDIN_FUNC(compare)
  11765. {
  11766. const char *message;
  11767. const char *cmpstring;
  11768. message = script_getstr(st,2);
  11769. cmpstring = script_getstr(st,3);
  11770. script_pushint(st,(stristr(message,cmpstring) != NULL));
  11771. return 0;
  11772. }
  11773. // [zBuffer] List of mathematics commands --->
  11774. BUILDIN_FUNC(sqrt)
  11775. {
  11776. double i, a;
  11777. i = script_getnum(st,2);
  11778. a = sqrt(i);
  11779. script_pushint(st,(int)a);
  11780. return 0;
  11781. }
  11782. BUILDIN_FUNC(pow)
  11783. {
  11784. double i, a, b;
  11785. a = script_getnum(st,2);
  11786. b = script_getnum(st,3);
  11787. i = pow(a,b);
  11788. script_pushint(st,(int)i);
  11789. return 0;
  11790. }
  11791. BUILDIN_FUNC(distance)
  11792. {
  11793. int x0, y0, x1, y1;
  11794. x0 = script_getnum(st,2);
  11795. y0 = script_getnum(st,3);
  11796. x1 = script_getnum(st,4);
  11797. y1 = script_getnum(st,5);
  11798. script_pushint(st,distance_xy(x0,y0,x1,y1));
  11799. return 0;
  11800. }
  11801. // <--- [zBuffer] List of mathematics commands
  11802. BUILDIN_FUNC(md5)
  11803. {
  11804. const char *tmpstr;
  11805. char *md5str;
  11806. tmpstr = script_getstr(st,2);
  11807. md5str = (char *)aMalloc((32+1)*sizeof(char));
  11808. MD5_String(tmpstr, md5str);
  11809. script_pushstr(st, md5str);
  11810. return 0;
  11811. }
  11812. // [zBuffer] List of dynamic var commands --->
  11813. BUILDIN_FUNC(setd)
  11814. {
  11815. TBL_PC *sd=NULL;
  11816. char varname[100];
  11817. const char *buffer;
  11818. int elem;
  11819. buffer = script_getstr(st, 2);
  11820. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  11821. elem = 0;
  11822. if( not_server_variable(*varname) )
  11823. {
  11824. sd = script_rid2sd(st);
  11825. if( sd == NULL )
  11826. {
  11827. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  11828. return 0;
  11829. }
  11830. }
  11831. if( is_string_variable(varname) ) {
  11832. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  11833. } else {
  11834. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  11835. }
  11836. return 0;
  11837. }
  11838. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  11839. {
  11840. int i, j;
  11841. TBL_PC* sd = NULL;
  11842. const char* query;
  11843. struct script_data* data;
  11844. const char* name;
  11845. int max_rows = SCRIPT_MAX_ARRAYSIZE;// maximum number of rows
  11846. int num_vars;
  11847. int num_cols;
  11848. // check target variables
  11849. for( i = 3; script_hasdata(st,i); ++i )
  11850. {
  11851. data = script_getdata(st, i);
  11852. if( data_isreference(data) && reference_tovariable(data) )
  11853. {// it's a variable
  11854. name = reference_getname(data);
  11855. if( not_server_variable(*name) && sd == NULL )
  11856. {// requires a player
  11857. sd = script_rid2sd(st);
  11858. if( sd == NULL )
  11859. {// no player attached
  11860. script_reportdata(data);
  11861. st->state = END;
  11862. return 1;
  11863. }
  11864. }
  11865. if( not_array_variable(*name) )
  11866. max_rows = 1;// not an array, limit to one row
  11867. }
  11868. else
  11869. {
  11870. ShowError("script:query_sql: not a variable\n");
  11871. script_reportdata(data);
  11872. st->state = END;
  11873. return 1;
  11874. }
  11875. }
  11876. num_vars = i - 3;
  11877. // Execute the query
  11878. query = script_getstr(st,2);
  11879. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  11880. {
  11881. Sql_ShowDebug(handle);
  11882. script_pushint(st, 0);
  11883. return 1;
  11884. }
  11885. if( Sql_NumRows(handle) == 0 )
  11886. {// No data received
  11887. Sql_FreeResult(handle);
  11888. script_pushint(st, 0);
  11889. return 0;
  11890. }
  11891. // Count the number of columns to store
  11892. num_cols = Sql_NumColumns(handle);
  11893. if( num_vars < num_cols )
  11894. {
  11895. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  11896. script_reportsrc(st);
  11897. }
  11898. else if( num_vars > num_cols )
  11899. {
  11900. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  11901. script_reportsrc(st);
  11902. }
  11903. // Store data
  11904. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  11905. {
  11906. for( j = 0; j < num_vars; ++j )
  11907. {
  11908. char* str = NULL;
  11909. if( j < num_cols )
  11910. Sql_GetData(handle, j, &str, NULL);
  11911. data = script_getdata(st, j+3);
  11912. name = reference_getname(data);
  11913. if( is_string_variable(name) )
  11914. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  11915. else
  11916. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  11917. }
  11918. }
  11919. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  11920. {
  11921. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  11922. script_reportsrc(st);
  11923. }
  11924. // Free data
  11925. Sql_FreeResult(handle);
  11926. script_pushint(st, i);
  11927. return 0;
  11928. }
  11929. BUILDIN_FUNC(query_sql)
  11930. {
  11931. return buildin_query_sql_sub(st, mmysql_handle);
  11932. }
  11933. BUILDIN_FUNC(query_logsql)
  11934. {
  11935. if( !log_config.sql_logs )
  11936. {// logmysql_handle == NULL
  11937. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  11938. script_pushint(st,-1);
  11939. return 1;
  11940. }
  11941. return buildin_query_sql_sub(st, logmysql_handle);
  11942. }
  11943. //Allows escaping of a given string.
  11944. BUILDIN_FUNC(escape_sql)
  11945. {
  11946. const char *str;
  11947. char *esc_str;
  11948. size_t len;
  11949. str = script_getstr(st,2);
  11950. len = strlen(str);
  11951. esc_str = (char*)aMalloc(len*2+1);
  11952. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  11953. script_pushstr(st, esc_str);
  11954. return 0;
  11955. }
  11956. BUILDIN_FUNC(getd)
  11957. {
  11958. char varname[100];
  11959. const char *buffer;
  11960. int elem;
  11961. buffer = script_getstr(st, 2);
  11962. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  11963. elem = 0;
  11964. // Push the 'pointer' so it's more flexible [Lance]
  11965. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  11966. return 0;
  11967. }
  11968. // <--- [zBuffer] List of dynamic var commands
  11969. // Pet stat [Lance]
  11970. BUILDIN_FUNC(petstat)
  11971. {
  11972. TBL_PC *sd = NULL;
  11973. struct pet_data *pd;
  11974. int flag = script_getnum(st,2);
  11975. sd = script_rid2sd(st);
  11976. if(!sd || !sd->status.pet_id || !sd->pd){
  11977. if(flag == 2)
  11978. script_pushconststr(st, "");
  11979. else
  11980. script_pushint(st,0);
  11981. return 0;
  11982. }
  11983. pd = sd->pd;
  11984. switch(flag){
  11985. case 1: script_pushint(st,(int)pd->pet.class_); break;
  11986. case 2: script_pushstrcopy(st, pd->pet.name); break;
  11987. case 3: script_pushint(st,(int)pd->pet.level); break;
  11988. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  11989. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  11990. default:
  11991. script_pushint(st,0);
  11992. break;
  11993. }
  11994. return 0;
  11995. }
  11996. BUILDIN_FUNC(callshop)
  11997. {
  11998. TBL_PC *sd = NULL;
  11999. struct npc_data *nd;
  12000. const char *shopname;
  12001. int flag = 0;
  12002. sd = script_rid2sd(st);
  12003. if (!sd) {
  12004. script_pushint(st,0);
  12005. return 0;
  12006. }
  12007. shopname = script_getstr(st, 2);
  12008. if( script_hasdata(st,3) )
  12009. flag = script_getnum(st,3);
  12010. nd = npc_name2id(shopname);
  12011. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  12012. {
  12013. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  12014. script_pushint(st,0);
  12015. return 1;
  12016. }
  12017. if( nd->subtype == SHOP )
  12018. {
  12019. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  12020. sd->state.callshop = 1;
  12021. switch( flag )
  12022. {
  12023. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  12024. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  12025. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  12026. }
  12027. }
  12028. else
  12029. clif_cashshop_show(sd, nd);
  12030. sd->npc_shopid = nd->bl.id;
  12031. script_pushint(st,1);
  12032. return 0;
  12033. }
  12034. BUILDIN_FUNC(npcshopitem)
  12035. {
  12036. const char* npcname = script_getstr(st, 2);
  12037. struct npc_data* nd = npc_name2id(npcname);
  12038. int n, i;
  12039. int amount;
  12040. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12041. { //Not found.
  12042. script_pushint(st,0);
  12043. return 0;
  12044. }
  12045. // get the count of new entries
  12046. amount = (script_lastdata(st)-2)/2;
  12047. // generate new shop item list
  12048. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  12049. for( n = 0, i = 3; n < amount; n++, i+=2 )
  12050. {
  12051. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12052. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12053. }
  12054. nd->u.shop.count = n;
  12055. script_pushint(st,1);
  12056. return 0;
  12057. }
  12058. BUILDIN_FUNC(npcshopadditem)
  12059. {
  12060. const char* npcname = script_getstr(st,2);
  12061. struct npc_data* nd = npc_name2id(npcname);
  12062. int n, i;
  12063. int amount;
  12064. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12065. { //Not found.
  12066. script_pushint(st,0);
  12067. return 0;
  12068. }
  12069. // get the count of new entries
  12070. amount = (script_lastdata(st)-2)/2;
  12071. // append new items to existing shop item list
  12072. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  12073. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  12074. {
  12075. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12076. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12077. }
  12078. nd->u.shop.count = n;
  12079. script_pushint(st,1);
  12080. return 0;
  12081. }
  12082. BUILDIN_FUNC(npcshopdelitem)
  12083. {
  12084. const char* npcname = script_getstr(st,2);
  12085. struct npc_data* nd = npc_name2id(npcname);
  12086. unsigned int nameid;
  12087. int n, i;
  12088. int amount;
  12089. int size;
  12090. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12091. { //Not found.
  12092. script_pushint(st,0);
  12093. return 0;
  12094. }
  12095. amount = script_lastdata(st)-2;
  12096. size = nd->u.shop.count;
  12097. // remove specified items from the shop item list
  12098. for( i = 3; i < 3 + amount; i++ )
  12099. {
  12100. nameid = script_getnum(st,i);
  12101. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  12102. if( n < size )
  12103. {
  12104. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  12105. size--;
  12106. }
  12107. }
  12108. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  12109. nd->u.shop.count = size;
  12110. script_pushint(st,1);
  12111. return 0;
  12112. }
  12113. //Sets a script to attach to a shop npc.
  12114. BUILDIN_FUNC(npcshopattach)
  12115. {
  12116. const char* npcname = script_getstr(st,2);
  12117. struct npc_data* nd = npc_name2id(npcname);
  12118. int flag = 1;
  12119. if( script_hasdata(st,3) )
  12120. flag = script_getnum(st,3);
  12121. if( !nd || nd->subtype != SHOP )
  12122. { //Not found.
  12123. script_pushint(st,0);
  12124. return 0;
  12125. }
  12126. if (flag)
  12127. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  12128. else
  12129. nd->master_nd = NULL;
  12130. script_pushint(st,1);
  12131. return 0;
  12132. }
  12133. /*==========================================
  12134. * Returns some values of an item [Lupus]
  12135. * Price, Weight, etc...
  12136. setitemscript(itemID,"{new item bonus script}",[n]);
  12137. Where n:
  12138. 0 - script
  12139. 1 - Equip script
  12140. 2 - Unequip script
  12141. *------------------------------------------*/
  12142. BUILDIN_FUNC(setitemscript)
  12143. {
  12144. int item_id,n=0;
  12145. const char *script;
  12146. struct item_data *i_data;
  12147. struct script_code **dstscript;
  12148. item_id = script_getnum(st,2);
  12149. script = script_getstr(st,3);
  12150. if( script_hasdata(st,4) )
  12151. n=script_getnum(st,4);
  12152. i_data = itemdb_exists(item_id);
  12153. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  12154. script_pushint(st,0);
  12155. return 0;
  12156. }
  12157. switch (n) {
  12158. case 2:
  12159. dstscript = &i_data->unequip_script;
  12160. break;
  12161. case 1:
  12162. dstscript = &i_data->equip_script;
  12163. break;
  12164. default:
  12165. dstscript = &i_data->script;
  12166. break;
  12167. }
  12168. if(*dstscript)
  12169. script_free_code(*dstscript);
  12170. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  12171. script_pushint(st,1);
  12172. return 0;
  12173. }
  12174. /* Work In Progress [Lupus]
  12175. BUILDIN_FUNC(addmonsterdrop)
  12176. {
  12177. int class_,item_id,chance;
  12178. class_=script_getnum(st,2);
  12179. item_id=script_getnum(st,3);
  12180. chance=script_getnum(st,4);
  12181. if(class_>1000 && item_id>500 && chance>0) {
  12182. script_pushint(st,1);
  12183. } else {
  12184. script_pushint(st,0);
  12185. }
  12186. }
  12187. BUILDIN_FUNC(delmonsterdrop)
  12188. {
  12189. int class_,item_id;
  12190. class_=script_getnum(st,2);
  12191. item_id=script_getnum(st,3);
  12192. if(class_>1000 && item_id>500) {
  12193. script_pushint(st,1);
  12194. } else {
  12195. script_pushint(st,0);
  12196. }
  12197. }
  12198. */
  12199. /*==========================================
  12200. * Returns some values of a monster [Lupus]
  12201. * Name, Level, race, size, etc...
  12202. getmonsterinfo(monsterID,queryIndex);
  12203. *------------------------------------------*/
  12204. BUILDIN_FUNC(getmonsterinfo)
  12205. {
  12206. struct mob_db *mob;
  12207. int mob_id;
  12208. mob_id = script_getnum(st,2);
  12209. if (!mobdb_checkid(mob_id)) {
  12210. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  12211. if ( !script_getnum(st,3) ) //requested a string
  12212. script_pushconststr(st,"null");
  12213. else
  12214. script_pushint(st,-1);
  12215. return -1;
  12216. }
  12217. mob = mob_db(mob_id);
  12218. switch ( script_getnum(st,3) ) {
  12219. case 0: script_pushstrcopy(st,mob->jname); break;
  12220. case 1: script_pushint(st,mob->lv); break;
  12221. case 2: script_pushint(st,mob->status.max_hp); break;
  12222. case 3: script_pushint(st,mob->base_exp); break;
  12223. case 4: script_pushint(st,mob->job_exp); break;
  12224. case 5: script_pushint(st,mob->status.rhw.atk); break;
  12225. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  12226. case 7: script_pushint(st,mob->status.def); break;
  12227. case 8: script_pushint(st,mob->status.mdef); break;
  12228. case 9: script_pushint(st,mob->status.str); break;
  12229. case 10: script_pushint(st,mob->status.agi); break;
  12230. case 11: script_pushint(st,mob->status.vit); break;
  12231. case 12: script_pushint(st,mob->status.int_); break;
  12232. case 13: script_pushint(st,mob->status.dex); break;
  12233. case 14: script_pushint(st,mob->status.luk); break;
  12234. case 15: script_pushint(st,mob->status.rhw.range); break;
  12235. case 16: script_pushint(st,mob->range2); break;
  12236. case 17: script_pushint(st,mob->range3); break;
  12237. case 18: script_pushint(st,mob->status.size); break;
  12238. case 19: script_pushint(st,mob->status.race); break;
  12239. case 20: script_pushint(st,mob->status.def_ele); break;
  12240. case 21: script_pushint(st,mob->status.mode); break;
  12241. case 22: script_pushint(st,mob->mexp); break;
  12242. default: script_pushint(st,-1); //wrong Index
  12243. }
  12244. return 0;
  12245. }
  12246. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  12247. {
  12248. TBL_PC *sd = NULL;
  12249. if(script_hasdata(st,2))
  12250. sd = map_nick2sd(script_getstr(st,2));
  12251. else
  12252. sd = script_rid2sd(st);
  12253. if(sd)
  12254. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  12255. else
  12256. script_pushint(st,0);
  12257. return 0;
  12258. }
  12259. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  12260. {
  12261. TBL_PC *sd = NULL;
  12262. if(script_hasdata(st,2))
  12263. sd = map_nick2sd(script_getstr(st,2));
  12264. else
  12265. sd = script_rid2sd(st);
  12266. if(sd)
  12267. script_pushint(st,(sd->chatID != 0));
  12268. else
  12269. script_pushint(st,0);
  12270. return 0;
  12271. }
  12272. BUILDIN_FUNC(searchitem)
  12273. {
  12274. struct script_data* data = script_getdata(st, 2);
  12275. const char *itemname = script_getstr(st,3);
  12276. struct item_data *items[MAX_SEARCH];
  12277. int count;
  12278. char* name;
  12279. int32 start;
  12280. int32 id;
  12281. int32 i;
  12282. TBL_PC* sd = NULL;
  12283. if ((items[0] = itemdb_exists(atoi(itemname))))
  12284. count = 1;
  12285. else {
  12286. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  12287. if (count > MAX_SEARCH) count = MAX_SEARCH;
  12288. }
  12289. if (!count) {
  12290. script_pushint(st, 0);
  12291. return 0;
  12292. }
  12293. if( !data_isreference(data) )
  12294. {
  12295. ShowError("script:searchitem: not a variable\n");
  12296. script_reportdata(data);
  12297. st->state = END;
  12298. return 1;// not a variable
  12299. }
  12300. id = reference_getid(data);
  12301. start = reference_getindex(data);
  12302. name = reference_getname(data);
  12303. if( not_array_variable(*name) )
  12304. {
  12305. ShowError("script:searchitem: illegal scope\n");
  12306. script_reportdata(data);
  12307. st->state = END;
  12308. return 1;// not supported
  12309. }
  12310. if( not_server_variable(*name) )
  12311. {
  12312. sd = script_rid2sd(st);
  12313. if( sd == NULL )
  12314. return 0;// no player attached
  12315. }
  12316. if( is_string_variable(name) )
  12317. {// string array
  12318. ShowError("script:searchitem: not an integer array reference\n");
  12319. script_reportdata(data);
  12320. st->state = END;
  12321. return 1;// not supported
  12322. }
  12323. for( i = 0; i < count; ++start, ++i )
  12324. {// Set array
  12325. void* v = (void*)__64BPRTSIZE(items[i]->nameid);
  12326. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  12327. }
  12328. script_pushint(st, count);
  12329. return 0;
  12330. }
  12331. int axtoi(const char *hexStg)
  12332. {
  12333. int n = 0; // position in string
  12334. int m = 0; // position in digit[] to shift
  12335. int count; // loop index
  12336. int intValue = 0; // integer value of hex string
  12337. int digit[11]; // hold values to convert
  12338. while (n < 10) {
  12339. if (hexStg[n]=='\0')
  12340. break;
  12341. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  12342. digit[n] = hexStg[n] & 0x0f; //convert to int
  12343. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  12344. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12345. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  12346. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12347. else break;
  12348. n++;
  12349. }
  12350. count = n;
  12351. m = n - 1;
  12352. n = 0;
  12353. while(n < count) {
  12354. // digit[n] is value of hex digit at position n
  12355. // (m << 2) is the number of positions to shift
  12356. // OR the bits into return value
  12357. intValue = intValue | (digit[n] << (m << 2));
  12358. m--; // adjust the position to set
  12359. n++; // next digit to process
  12360. }
  12361. return (intValue);
  12362. }
  12363. // [Lance] Hex string to integer converter
  12364. BUILDIN_FUNC(axtoi)
  12365. {
  12366. const char *hex = script_getstr(st,2);
  12367. script_pushint(st,axtoi(hex));
  12368. return 0;
  12369. }
  12370. // [zBuffer] List of player cont commands --->
  12371. BUILDIN_FUNC(rid2name)
  12372. {
  12373. struct block_list *bl = NULL;
  12374. int rid = script_getnum(st,2);
  12375. if((bl = map_id2bl(rid)))
  12376. {
  12377. switch(bl->type) {
  12378. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  12379. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  12380. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  12381. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  12382. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  12383. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  12384. default:
  12385. ShowError("buildin_rid2name: BL type unknown.\n");
  12386. script_pushconststr(st,"");
  12387. break;
  12388. }
  12389. } else {
  12390. ShowError("buildin_rid2name: invalid RID\n");
  12391. script_pushconststr(st,"(null)");
  12392. }
  12393. return 0;
  12394. }
  12395. BUILDIN_FUNC(pcblockmove)
  12396. {
  12397. int id, flag;
  12398. TBL_PC *sd = NULL;
  12399. id = script_getnum(st,2);
  12400. flag = script_getnum(st,3);
  12401. if(id)
  12402. sd = map_id2sd(id);
  12403. else
  12404. sd = script_rid2sd(st);
  12405. if(sd)
  12406. sd->state.blockedmove = flag > 0;
  12407. return 0;
  12408. }
  12409. BUILDIN_FUNC(pcfollow)
  12410. {
  12411. int id, targetid;
  12412. TBL_PC *sd = NULL;
  12413. id = script_getnum(st,2);
  12414. targetid = script_getnum(st,3);
  12415. if(id)
  12416. sd = map_id2sd(id);
  12417. else
  12418. sd = script_rid2sd(st);
  12419. if(sd)
  12420. pc_follow(sd, targetid);
  12421. return 0;
  12422. }
  12423. BUILDIN_FUNC(pcstopfollow)
  12424. {
  12425. int id;
  12426. TBL_PC *sd = NULL;
  12427. id = script_getnum(st,2);
  12428. if(id)
  12429. sd = map_id2sd(id);
  12430. else
  12431. sd = script_rid2sd(st);
  12432. if(sd)
  12433. pc_stop_following(sd);
  12434. return 0;
  12435. }
  12436. // <--- [zBuffer] List of player cont commands
  12437. // [zBuffer] List of mob control commands --->
  12438. //## TODO always return if the request/whatever was successfull [FlavioJS]
  12439. /// Makes the unit walk to target position or map
  12440. /// Returns if it was successfull
  12441. ///
  12442. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  12443. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  12444. BUILDIN_FUNC(unitwalk)
  12445. {
  12446. struct block_list* bl;
  12447. bl = map_id2bl(script_getnum(st,2));
  12448. if( bl == NULL )
  12449. {
  12450. script_pushint(st, 0);
  12451. }
  12452. else if( script_hasdata(st,4) )
  12453. {
  12454. int x = script_getnum(st,3);
  12455. int y = script_getnum(st,4);
  12456. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  12457. }
  12458. else
  12459. {
  12460. int map_id = script_getnum(st,3);
  12461. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  12462. }
  12463. return 0;
  12464. }
  12465. /// Kills the unit
  12466. ///
  12467. /// unitkill <unit_id>;
  12468. BUILDIN_FUNC(unitkill)
  12469. {
  12470. struct block_list* bl = map_id2bl(script_getnum(st,2));
  12471. if( bl != NULL )
  12472. status_kill(bl);
  12473. return 0;
  12474. }
  12475. /// Warps the unit to the target position in the target map
  12476. /// Returns if it was successfull
  12477. ///
  12478. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  12479. BUILDIN_FUNC(unitwarp)
  12480. {
  12481. int unit_id;
  12482. int map;
  12483. short x;
  12484. short y;
  12485. struct block_list* bl;
  12486. const char *mapname;
  12487. unit_id = script_getnum(st,2);
  12488. mapname = script_getstr(st, 3);
  12489. x = (short)script_getnum(st,4);
  12490. y = (short)script_getnum(st,5);
  12491. if (!unit_id) //Warp the script's runner
  12492. bl = map_id2bl(st->rid);
  12493. else
  12494. bl = map_id2bl(unit_id);
  12495. if( strcmp(mapname,"this") == 0 )
  12496. map = bl?bl->m:-1;
  12497. else
  12498. map = map_mapname2mapid(mapname);
  12499. if( map >= 0 && bl != NULL )
  12500. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  12501. else
  12502. script_pushint(st, 0);
  12503. return 0;
  12504. }
  12505. /// Makes the unit attack the target.
  12506. /// If the unit is a player and <action type> is not 0, it does a continuous
  12507. /// attack instead of a single attack.
  12508. /// Returns if the request was successfull.
  12509. ///
  12510. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  12511. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  12512. BUILDIN_FUNC(unitattack)
  12513. {
  12514. struct block_list* unit_bl;
  12515. struct block_list* target_bl = NULL;
  12516. struct script_data* data;
  12517. int actiontype = 0;
  12518. // get unit
  12519. unit_bl = map_id2bl(script_getnum(st,2));
  12520. if( unit_bl == NULL ) {
  12521. script_pushint(st, 0);
  12522. return 0;
  12523. }
  12524. data = script_getdata(st, 3);
  12525. get_val(st, data);
  12526. if( data_isstring(data) )
  12527. {
  12528. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  12529. if( sd != NULL )
  12530. target_bl = &sd->bl;
  12531. } else
  12532. target_bl = map_id2bl(conv_num(st, data));
  12533. // request the attack
  12534. if( target_bl == NULL )
  12535. {
  12536. script_pushint(st, 0);
  12537. return 0;
  12538. }
  12539. // get actiontype
  12540. if( script_hasdata(st,4) )
  12541. actiontype = script_getnum(st,4);
  12542. switch( unit_bl->type )
  12543. {
  12544. case BL_PC:
  12545. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  12546. script_pushint(st, 1);
  12547. return 0;
  12548. case BL_MOB:
  12549. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  12550. break;
  12551. case BL_PET:
  12552. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  12553. break;
  12554. default:
  12555. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  12556. script_pushint(st, 0);
  12557. return 1;
  12558. }
  12559. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  12560. return 0;
  12561. }
  12562. /// Makes the unit stop attacking and moving
  12563. ///
  12564. /// unitstop <unit_id>;
  12565. BUILDIN_FUNC(unitstop)
  12566. {
  12567. int unit_id;
  12568. struct block_list* bl;
  12569. unit_id = script_getnum(st,2);
  12570. bl = map_id2bl(unit_id);
  12571. if( bl != NULL )
  12572. {
  12573. unit_stop_attack(bl);
  12574. unit_stop_walking(bl,4);
  12575. if( bl->type == BL_MOB )
  12576. ((TBL_MOB*)bl)->target_id = 0;
  12577. }
  12578. return 0;
  12579. }
  12580. /// Makes the unit say the message
  12581. ///
  12582. /// unittalk <unit_id>,"<message>";
  12583. BUILDIN_FUNC(unittalk)
  12584. {
  12585. int unit_id;
  12586. const char* message;
  12587. struct block_list* bl;
  12588. unit_id = script_getnum(st,2);
  12589. message = script_getstr(st, 3);
  12590. bl = map_id2bl(unit_id);
  12591. if( bl != NULL )
  12592. {
  12593. struct StringBuf sbuf;
  12594. StringBuf_Init(&sbuf);
  12595. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  12596. clif_message(bl, StringBuf_Value(&sbuf));
  12597. if( bl->type == BL_PC )
  12598. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  12599. StringBuf_Destroy(&sbuf);
  12600. }
  12601. return 0;
  12602. }
  12603. /// Makes the unit do an emotion
  12604. ///
  12605. /// unitemote <unit_id>,<emotion>;
  12606. ///
  12607. /// @see e_* in const.txt
  12608. BUILDIN_FUNC(unitemote)
  12609. {
  12610. int unit_id;
  12611. int emotion;
  12612. struct block_list* bl;
  12613. unit_id = script_getnum(st,2);
  12614. emotion = script_getnum(st,3);
  12615. bl = map_id2bl(unit_id);
  12616. if( bl != NULL )
  12617. clif_emotion(bl, emotion);
  12618. return 0;
  12619. }
  12620. /// Makes the unit cast the skill on the target or self if no target is specified
  12621. ///
  12622. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  12623. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  12624. BUILDIN_FUNC(unitskilluseid)
  12625. {
  12626. int unit_id;
  12627. int skill_id;
  12628. int skill_lv;
  12629. int target_id;
  12630. struct block_list* bl;
  12631. unit_id = script_getnum(st,2);
  12632. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12633. skill_lv = script_getnum(st,4);
  12634. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  12635. bl = map_id2bl(unit_id);
  12636. if( bl != NULL )
  12637. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  12638. return 0;
  12639. }
  12640. /// Makes the unit cast the skill on the target position.
  12641. ///
  12642. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  12643. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  12644. BUILDIN_FUNC(unitskillusepos)
  12645. {
  12646. int unit_id;
  12647. int skill_id;
  12648. int skill_lv;
  12649. int skill_x;
  12650. int skill_y;
  12651. struct block_list* bl;
  12652. unit_id = script_getnum(st,2);
  12653. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12654. skill_lv = script_getnum(st,4);
  12655. skill_x = script_getnum(st,5);
  12656. skill_y = script_getnum(st,6);
  12657. bl = map_id2bl(unit_id);
  12658. if( bl != NULL )
  12659. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  12660. return 0;
  12661. }
  12662. // <--- [zBuffer] List of mob control commands
  12663. /// Pauses the execution of the script, detaching the player
  12664. ///
  12665. /// sleep <mili seconds>;
  12666. BUILDIN_FUNC(sleep)
  12667. {
  12668. int ticks;
  12669. ticks = script_getnum(st,2);
  12670. // detach the player
  12671. script_detach_rid(st);
  12672. if( ticks <= 0 )
  12673. {// do nothing
  12674. }
  12675. else if( st->sleep.tick == 0 )
  12676. {// sleep for the target amount of time
  12677. st->state = RERUNLINE;
  12678. st->sleep.tick = ticks;
  12679. }
  12680. else
  12681. {// sleep time is over
  12682. st->state = RUN;
  12683. st->sleep.tick = 0;
  12684. }
  12685. return 0;
  12686. }
  12687. /// Pauses the execution of the script, keeping the player attached
  12688. /// Returns if a player is still attached
  12689. ///
  12690. /// sleep2(<mili secconds>) -> <bool>
  12691. BUILDIN_FUNC(sleep2)
  12692. {
  12693. int ticks;
  12694. ticks = script_getnum(st,2);
  12695. if( ticks <= 0 )
  12696. {// do nothing
  12697. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12698. }
  12699. else if( !st->sleep.tick )
  12700. {// sleep for the target amount of time
  12701. st->state = RERUNLINE;
  12702. st->sleep.tick = ticks;
  12703. }
  12704. else
  12705. {// sleep time is over
  12706. st->state = RUN;
  12707. st->sleep.tick = 0;
  12708. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12709. }
  12710. return 0;
  12711. }
  12712. /// Awakes all the sleep timers of the target npc
  12713. ///
  12714. /// awake "<npc name>";
  12715. BUILDIN_FUNC(awake)
  12716. {
  12717. struct npc_data* nd;
  12718. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  12719. nd = npc_name2id(script_getstr(st, 2));
  12720. if( nd == NULL ) {
  12721. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  12722. return 1;
  12723. }
  12724. while( node )
  12725. {
  12726. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  12727. {// sleep timer for the npc
  12728. struct script_state* tst = (struct script_state*)node->data;
  12729. TBL_PC* sd = map_id2sd(tst->rid);
  12730. if( tst->sleep.timer == INVALID_TIMER )
  12731. {// already awake ???
  12732. node = node->next;
  12733. continue;
  12734. }
  12735. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  12736. {// char not online anymore / another char of the same account is online - Cancel execution
  12737. tst->state = END;
  12738. tst->rid = 0;
  12739. }
  12740. delete_timer(tst->sleep.timer, run_script_timer);
  12741. node = script_erase_sleepdb(node);
  12742. tst->sleep.timer = INVALID_TIMER;
  12743. if(tst->state != RERUNLINE)
  12744. tst->sleep.tick = 0;
  12745. run_script_main(tst);
  12746. }
  12747. else
  12748. {
  12749. node = node->next;
  12750. }
  12751. }
  12752. return 0;
  12753. }
  12754. /// Returns a reference to a variable of the target NPC.
  12755. /// Returns 0 if an error occurs.
  12756. ///
  12757. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  12758. BUILDIN_FUNC(getvariableofnpc)
  12759. {
  12760. struct script_data* data;
  12761. const char* name;
  12762. struct npc_data* nd;
  12763. data = script_getdata(st,2);
  12764. if( !data_isreference(data) )
  12765. {// Not a reference (aka varaible name)
  12766. ShowError("script:getvariableofnpc: not a variable\n");
  12767. script_reportdata(data);
  12768. script_pushnil(st);
  12769. st->state = END;
  12770. return 1;
  12771. }
  12772. name = reference_getname(data);
  12773. if( *name != '.' || name[1] == '@' )
  12774. {// not a npc variable
  12775. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  12776. script_reportdata(data);
  12777. script_pushnil(st);
  12778. st->state = END;
  12779. return 1;
  12780. }
  12781. nd = npc_name2id(script_getstr(st,3));
  12782. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  12783. {// NPC not found or has no script
  12784. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  12785. script_pushnil(st);
  12786. st->state = END;
  12787. return 1;
  12788. }
  12789. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  12790. return 0;
  12791. }
  12792. /// Opens a warp portal.
  12793. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  12794. ///
  12795. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  12796. ///
  12797. /// @author blackhole89
  12798. BUILDIN_FUNC(warpportal)
  12799. {
  12800. int spx;
  12801. int spy;
  12802. unsigned short mapindex;
  12803. int tpx;
  12804. int tpy;
  12805. struct skill_unit_group* group;
  12806. struct block_list* bl;
  12807. bl = map_id2bl(st->oid);
  12808. if( bl == NULL )
  12809. {
  12810. ShowError("script:warpportal: npc is needed\n");
  12811. return 1;
  12812. }
  12813. spx = script_getnum(st,2);
  12814. spy = script_getnum(st,3);
  12815. mapindex = mapindex_name2id(script_getstr(st, 4));
  12816. tpx = script_getnum(st,5);
  12817. tpy = script_getnum(st,6);
  12818. if( mapindex == 0 )
  12819. return 0;// map not found
  12820. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  12821. if( group == NULL )
  12822. return 0;// failed
  12823. group->val2 = (tpx<<16) | tpy;
  12824. group->val3 = mapindex;
  12825. return 0;
  12826. }
  12827. BUILDIN_FUNC(openmail)
  12828. {
  12829. TBL_PC* sd;
  12830. sd = script_rid2sd(st);
  12831. if( sd == NULL )
  12832. return 0;
  12833. mail_openmail(sd);
  12834. return 0;
  12835. }
  12836. BUILDIN_FUNC(openauction)
  12837. {
  12838. TBL_PC* sd;
  12839. sd = script_rid2sd(st);
  12840. if( sd == NULL )
  12841. return 0;
  12842. clif_Auction_openwindow(sd);
  12843. return 0;
  12844. }
  12845. /// Retrieves the value of the specified flag of the specified cell.
  12846. ///
  12847. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  12848. ///
  12849. /// @see cell_chk* constants in const.txt for the types
  12850. BUILDIN_FUNC(checkcell)
  12851. {
  12852. int m = map_mapname2mapid(script_getstr(st,2));
  12853. int x = script_getnum(st,3);
  12854. int y = script_getnum(st,4);
  12855. cell_chk type = (cell_chk)script_getnum(st,5);
  12856. script_pushint(st, map_getcell(m, x, y, type));
  12857. return 0;
  12858. }
  12859. /// Modifies flags of cells in the specified area.
  12860. ///
  12861. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  12862. ///
  12863. /// @see cell_* constants in const.txt for the types
  12864. BUILDIN_FUNC(setcell)
  12865. {
  12866. int m = map_mapname2mapid(script_getstr(st,2));
  12867. int x1 = script_getnum(st,3);
  12868. int y1 = script_getnum(st,4);
  12869. int x2 = script_getnum(st,5);
  12870. int y2 = script_getnum(st,6);
  12871. cell_t type = (cell_t)script_getnum(st,7);
  12872. bool flag = (bool)script_getnum(st,8);
  12873. int x,y;
  12874. if( x1 > x2 ) swap(x1,x2);
  12875. if( y1 > y2 ) swap(y1,y2);
  12876. for( y = y1; y <= y2; ++y )
  12877. for( x = x1; x <= x2; ++x )
  12878. map_setcell(m, x, y, type, flag);
  12879. return 0;
  12880. }
  12881. /*==========================================
  12882. * Mercenary Commands
  12883. *------------------------------------------*/
  12884. BUILDIN_FUNC(mercenary_create)
  12885. {
  12886. struct map_session_data *sd;
  12887. int class_, contract_time;
  12888. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  12889. return 0;
  12890. class_ = script_getnum(st,2);
  12891. if( !merc_class(class_) )
  12892. return 0;
  12893. contract_time = script_getnum(st,3);
  12894. merc_create(sd, class_, contract_time);
  12895. return 0;
  12896. }
  12897. BUILDIN_FUNC(mercenary_heal)
  12898. {
  12899. struct map_session_data *sd = script_rid2sd(st);
  12900. int hp, sp;
  12901. if( sd == NULL || sd->md == NULL )
  12902. return 0;
  12903. hp = script_getnum(st,2);
  12904. sp = script_getnum(st,3);
  12905. status_heal(&sd->md->bl, hp, sp, 0);
  12906. return 0;
  12907. }
  12908. BUILDIN_FUNC(mercenary_sc_start)
  12909. {
  12910. struct map_session_data *sd = script_rid2sd(st);
  12911. enum sc_type type;
  12912. int tick, val1;
  12913. if( sd == NULL || sd->md == NULL )
  12914. return 0;
  12915. type = (sc_type)script_getnum(st,2);
  12916. tick = script_getnum(st,3);
  12917. val1 = script_getnum(st,4);
  12918. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  12919. return 0;
  12920. }
  12921. BUILDIN_FUNC(mercenary_get_calls)
  12922. {
  12923. struct map_session_data *sd = script_rid2sd(st);
  12924. int guild;
  12925. if( sd == NULL )
  12926. return 0;
  12927. guild = script_getnum(st,2);
  12928. switch( guild )
  12929. {
  12930. case ARCH_MERC_GUILD:
  12931. script_pushint(st,sd->status.arch_calls);
  12932. break;
  12933. case SPEAR_MERC_GUILD:
  12934. script_pushint(st,sd->status.spear_calls);
  12935. break;
  12936. case SWORD_MERC_GUILD:
  12937. script_pushint(st,sd->status.sword_calls);
  12938. break;
  12939. default:
  12940. script_pushint(st,0);
  12941. break;
  12942. }
  12943. return 0;
  12944. }
  12945. BUILDIN_FUNC(mercenary_set_calls)
  12946. {
  12947. struct map_session_data *sd = script_rid2sd(st);
  12948. int guild, value, *calls;
  12949. if( sd == NULL )
  12950. return 0;
  12951. guild = script_getnum(st,2);
  12952. value = script_getnum(st,3);
  12953. switch( guild )
  12954. {
  12955. case ARCH_MERC_GUILD:
  12956. calls = &sd->status.arch_calls;
  12957. break;
  12958. case SPEAR_MERC_GUILD:
  12959. calls = &sd->status.spear_calls;
  12960. break;
  12961. case SWORD_MERC_GUILD:
  12962. calls = &sd->status.sword_calls;
  12963. break;
  12964. default:
  12965. return 0; // Invalid Guild
  12966. }
  12967. *calls += value;
  12968. *calls = cap_value(*calls, 0, INT_MAX);
  12969. return 0;
  12970. }
  12971. BUILDIN_FUNC(mercenary_get_faith)
  12972. {
  12973. struct map_session_data *sd = script_rid2sd(st);
  12974. int guild;
  12975. if( sd == NULL )
  12976. return 0;
  12977. guild = script_getnum(st,2);
  12978. switch( guild )
  12979. {
  12980. case ARCH_MERC_GUILD:
  12981. script_pushint(st,sd->status.arch_faith);
  12982. break;
  12983. case SPEAR_MERC_GUILD:
  12984. script_pushint(st,sd->status.spear_faith);
  12985. break;
  12986. case SWORD_MERC_GUILD:
  12987. script_pushint(st,sd->status.sword_faith);
  12988. break;
  12989. default:
  12990. script_pushint(st,0);
  12991. break;
  12992. }
  12993. return 0;
  12994. }
  12995. BUILDIN_FUNC(mercenary_set_faith)
  12996. {
  12997. struct map_session_data *sd = script_rid2sd(st);
  12998. int guild, value, *calls;
  12999. if( sd == NULL )
  13000. return 0;
  13001. guild = script_getnum(st,2);
  13002. value = script_getnum(st,3);
  13003. switch( guild )
  13004. {
  13005. case ARCH_MERC_GUILD:
  13006. calls = &sd->status.arch_faith;
  13007. break;
  13008. case SPEAR_MERC_GUILD:
  13009. calls = &sd->status.spear_faith;
  13010. break;
  13011. case SWORD_MERC_GUILD:
  13012. calls = &sd->status.sword_faith;
  13013. break;
  13014. default:
  13015. return 0; // Invalid Guild
  13016. }
  13017. *calls += value;
  13018. *calls = cap_value(*calls, 0, INT_MAX);
  13019. if( mercenary_get_guild(sd->md) == guild )
  13020. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  13021. return 0;
  13022. }
  13023. /*------------------------------------------
  13024. * Book Reading
  13025. *------------------------------------------*/
  13026. BUILDIN_FUNC(readbook)
  13027. {
  13028. struct map_session_data *sd;
  13029. int book_id, page;
  13030. if( (sd = script_rid2sd(st)) == NULL )
  13031. return 0;
  13032. book_id = script_getnum(st,2);
  13033. page = script_getnum(st,3);
  13034. clif_readbook(sd->fd, book_id, page);
  13035. return 0;
  13036. }
  13037. /******************
  13038. Questlog script commands
  13039. *******************/
  13040. BUILDIN_FUNC(setquest)
  13041. {
  13042. TBL_PC * sd = script_rid2sd(st);
  13043. quest_add(sd, script_getnum(st, 2));
  13044. return 0;
  13045. }
  13046. BUILDIN_FUNC(erasequest)
  13047. {
  13048. TBL_PC * sd = script_rid2sd(st);
  13049. quest_delete(sd, script_getnum(st, 2));
  13050. return 0;
  13051. }
  13052. BUILDIN_FUNC(completequest)
  13053. {
  13054. TBL_PC * sd = script_rid2sd(st);
  13055. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  13056. return 0;
  13057. }
  13058. BUILDIN_FUNC(changequest)
  13059. {
  13060. TBL_PC * sd = script_rid2sd(st);
  13061. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  13062. return 0;
  13063. }
  13064. BUILDIN_FUNC(checkquest)
  13065. {
  13066. TBL_PC * sd = script_rid2sd(st);
  13067. quest_check_type type = HAVEQUEST;
  13068. if( script_hasdata(st, 3) )
  13069. type = (quest_check_type)script_getnum(st, 3);
  13070. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  13071. return 0;
  13072. }
  13073. BUILDIN_FUNC(showevent)
  13074. {
  13075. TBL_PC *sd = script_rid2sd(st);
  13076. struct npc_data *nd = map_id2nd(st->oid);
  13077. int state, color;
  13078. if( sd == NULL || nd == NULL )
  13079. return 0;
  13080. state = script_getnum(st, 2);
  13081. color = script_getnum(st, 3);
  13082. if( color < 0 || color > 3 )
  13083. color = 0; // set default color
  13084. clif_quest_show_event(sd, &nd->bl, state, color);
  13085. return 0;
  13086. }
  13087. /*==========================================
  13088. * BattleGround System
  13089. *------------------------------------------*/
  13090. BUILDIN_FUNC(waitingroom2bg)
  13091. {
  13092. struct npc_data *nd;
  13093. struct chat_data *cd;
  13094. const char *map_name, *ev = "", *dev = "";
  13095. int x, y, i, mapindex = 0, bg_id, n;
  13096. struct map_session_data *sd;
  13097. if( script_hasdata(st,7) )
  13098. nd = npc_name2id(script_getstr(st,7));
  13099. else
  13100. nd = (struct npc_data *)map_id2bl(st->oid);
  13101. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  13102. {
  13103. script_pushint(st,0);
  13104. return 0;
  13105. }
  13106. map_name = script_getstr(st,2);
  13107. if( strcmp(map_name,"-") != 0 )
  13108. {
  13109. mapindex = mapindex_name2id(map_name);
  13110. if( mapindex == 0 )
  13111. { // Invalid Map
  13112. script_pushint(st,0);
  13113. return 0;
  13114. }
  13115. }
  13116. x = script_getnum(st,3);
  13117. y = script_getnum(st,4);
  13118. ev = script_getstr(st,5); // Logout Event
  13119. dev = script_getstr(st,6); // Die Event
  13120. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  13121. { // Creation failed
  13122. script_pushint(st,0);
  13123. return 0;
  13124. }
  13125. n = cd->users;
  13126. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  13127. {
  13128. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  13129. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  13130. else
  13131. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  13132. }
  13133. mapreg_setreg(add_str("$@arenamembersnum"), i);
  13134. script_pushint(st,bg_id);
  13135. return 0;
  13136. }
  13137. BUILDIN_FUNC(waitingroom2bg_single)
  13138. {
  13139. const char* map_name;
  13140. struct npc_data *nd;
  13141. struct chat_data *cd;
  13142. struct map_session_data *sd;
  13143. int x, y, mapindex, bg_id;
  13144. bg_id = script_getnum(st,2);
  13145. map_name = script_getstr(st,3);
  13146. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13147. return 0; // Invalid Map
  13148. x = script_getnum(st,4);
  13149. y = script_getnum(st,5);
  13150. nd = npc_name2id(script_getstr(st,6));
  13151. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  13152. return 0;
  13153. if( (sd = cd->usersd[0]) == NULL )
  13154. return 0;
  13155. if( bg_team_join(bg_id, sd) )
  13156. {
  13157. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  13158. script_pushint(st,1);
  13159. }
  13160. else
  13161. script_pushint(st,0);
  13162. return 0;
  13163. }
  13164. BUILDIN_FUNC(bg_team_setxy)
  13165. {
  13166. struct battleground_data *bg;
  13167. int bg_id;
  13168. bg_id = script_getnum(st,2);
  13169. if( (bg = bg_team_search(bg_id)) == NULL )
  13170. return 0;
  13171. bg->x = script_getnum(st,3);
  13172. bg->y = script_getnum(st,4);
  13173. return 0;
  13174. }
  13175. BUILDIN_FUNC(bg_warp)
  13176. {
  13177. int x, y, mapindex, bg_id;
  13178. const char* map_name;
  13179. bg_id = script_getnum(st,2);
  13180. map_name = script_getstr(st,3);
  13181. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13182. return 0; // Invalid Map
  13183. x = script_getnum(st,4);
  13184. y = script_getnum(st,5);
  13185. bg_team_warp(bg_id, mapindex, x, y);
  13186. return 0;
  13187. }
  13188. BUILDIN_FUNC(bg_monster)
  13189. {
  13190. int class_ = 0, x = 0, y = 0, bg_id = 0;
  13191. const char *str,*map, *evt="";
  13192. bg_id = script_getnum(st,2);
  13193. map = script_getstr(st,3);
  13194. x = script_getnum(st,4);
  13195. y = script_getnum(st,5);
  13196. str = script_getstr(st,6);
  13197. class_ = script_getnum(st,7);
  13198. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  13199. check_event(st, evt);
  13200. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  13201. return 0;
  13202. }
  13203. BUILDIN_FUNC(bg_monster_set_team)
  13204. {
  13205. struct mob_data *md;
  13206. struct block_list *mbl;
  13207. int id = script_getnum(st,2),
  13208. bg_id = script_getnum(st,3);
  13209. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  13210. return 0;
  13211. md = (TBL_MOB *)mbl;
  13212. md->bg_id = bg_id;
  13213. mob_stop_attack(md);
  13214. mob_stop_walking(md, 0);
  13215. md->target_id = md->attacked_id = 0;
  13216. clif_charnameack(0, &md->bl);
  13217. return 0;
  13218. }
  13219. BUILDIN_FUNC(bg_leave)
  13220. {
  13221. struct map_session_data *sd = script_rid2sd(st);
  13222. if( sd == NULL || !sd->bg_id )
  13223. return 0;
  13224. bg_team_leave(sd,0);
  13225. return 0;
  13226. }
  13227. BUILDIN_FUNC(bg_destroy)
  13228. {
  13229. int bg_id = script_getnum(st,2);
  13230. bg_team_delete(bg_id);
  13231. return 0;
  13232. }
  13233. BUILDIN_FUNC(bg_getareausers)
  13234. {
  13235. const char *str;
  13236. int m, x0, y0, x1, y1, bg_id;
  13237. int i = 0, c = 0;
  13238. struct battleground_data *bg = NULL;
  13239. struct map_session_data *sd;
  13240. bg_id = script_getnum(st,2);
  13241. str = script_getstr(st,3);
  13242. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  13243. {
  13244. script_pushint(st,0);
  13245. return 0;
  13246. }
  13247. x0 = script_getnum(st,4);
  13248. y0 = script_getnum(st,5);
  13249. x1 = script_getnum(st,6);
  13250. y1 = script_getnum(st,7);
  13251. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  13252. {
  13253. if( (sd = bg->members[i].sd) == NULL )
  13254. continue;
  13255. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  13256. continue;
  13257. c++;
  13258. }
  13259. script_pushint(st,c);
  13260. return 0;
  13261. }
  13262. BUILDIN_FUNC(bg_updatescore)
  13263. {
  13264. const char *str;
  13265. int m;
  13266. str = script_getstr(st,2);
  13267. if( (m = map_mapname2mapid(str)) < 0 )
  13268. return 0;
  13269. map[m].bgscore_lion = script_getnum(st,3);
  13270. map[m].bgscore_eagle = script_getnum(st,4);
  13271. clif_bg_updatescore(m);
  13272. return 0;
  13273. }
  13274. BUILDIN_FUNC(bg_get_data)
  13275. {
  13276. struct battleground_data *bg;
  13277. int bg_id = script_getnum(st,2),
  13278. type = script_getnum(st,3);
  13279. if( (bg = bg_team_search(bg_id)) == NULL )
  13280. {
  13281. script_pushint(st,0);
  13282. return 0;
  13283. }
  13284. switch( type )
  13285. {
  13286. case 0: script_pushint(st, bg->count); break;
  13287. default:
  13288. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  13289. break;
  13290. }
  13291. return 0;
  13292. }
  13293. /*==========================================
  13294. * Instancing Script Commands
  13295. *------------------------------------------*/
  13296. BUILDIN_FUNC(instance_create)
  13297. {
  13298. const char *name;
  13299. int party_id, res;
  13300. name = script_getstr(st, 2);
  13301. party_id = script_getnum(st, 3);
  13302. res = instance_create(party_id, name);
  13303. if( res == -4 ) // Already exists
  13304. {
  13305. script_pushint(st, -1);
  13306. return 0;
  13307. }
  13308. else if( res < 0 )
  13309. {
  13310. const char *err;
  13311. switch(res)
  13312. {
  13313. case -3: err = "No free instances"; break;
  13314. case -2: err = "Invalid party ID"; break;
  13315. case -1: err = "Invalid type"; break;
  13316. default: err = "Unknown"; break;
  13317. }
  13318. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  13319. script_pushint(st, -2);
  13320. return 0;
  13321. }
  13322. script_pushint(st, res);
  13323. return 0;
  13324. }
  13325. BUILDIN_FUNC(instance_destroy)
  13326. {
  13327. int instance_id;
  13328. struct map_session_data *sd;
  13329. struct party_data *p;
  13330. if( script_hasdata(st, 2) )
  13331. instance_id = script_getnum(st, 2);
  13332. else if( st->instance_id )
  13333. instance_id = st->instance_id;
  13334. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13335. instance_id = p->instance_id;
  13336. else return 0;
  13337. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13338. {
  13339. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  13340. return 0;
  13341. }
  13342. instance_destroy(instance_id);
  13343. return 0;
  13344. }
  13345. BUILDIN_FUNC(instance_attachmap)
  13346. {
  13347. const char *name;
  13348. int m;
  13349. int instance_id;
  13350. bool usebasename = false;
  13351. name = script_getstr(st,2);
  13352. instance_id = script_getnum(st,3);
  13353. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  13354. usebasename = true;
  13355. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  13356. {
  13357. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  13358. script_pushconststr(st, "");
  13359. return 0;
  13360. }
  13361. script_pushconststr(st, map[m].name);
  13362. return 0;
  13363. }
  13364. BUILDIN_FUNC(instance_detachmap)
  13365. {
  13366. struct map_session_data *sd;
  13367. struct party_data *p;
  13368. const char *str;
  13369. int m, instance_id;
  13370. str = script_getstr(st, 2);
  13371. if( script_hasdata(st, 3) )
  13372. instance_id = script_getnum(st, 3);
  13373. else if( st->instance_id )
  13374. instance_id = st->instance_id;
  13375. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13376. instance_id = p->instance_id;
  13377. else return 0;
  13378. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  13379. {
  13380. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  13381. return 0;
  13382. }
  13383. instance_del_map(m);
  13384. return 0;
  13385. }
  13386. BUILDIN_FUNC(instance_attach)
  13387. {
  13388. int instance_id;
  13389. instance_id = script_getnum(st, 2);
  13390. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13391. return 0;
  13392. st->instance_id = instance_id;
  13393. return 0;
  13394. }
  13395. BUILDIN_FUNC(instance_id)
  13396. {
  13397. int type, instance_id;
  13398. struct map_session_data *sd;
  13399. struct party_data *p;
  13400. if( script_hasdata(st, 2) )
  13401. {
  13402. type = script_getnum(st, 2);
  13403. if( type == 0 )
  13404. instance_id = st->instance_id;
  13405. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  13406. instance_id = p->instance_id;
  13407. else
  13408. instance_id = 0;
  13409. }
  13410. else
  13411. instance_id = st->instance_id;
  13412. script_pushint(st, instance_id);
  13413. return 0;
  13414. }
  13415. BUILDIN_FUNC(instance_set_timeout)
  13416. {
  13417. int progress_timeout, idle_timeout;
  13418. int instance_id;
  13419. struct map_session_data *sd;
  13420. struct party_data *p;
  13421. progress_timeout = script_getnum(st, 2);
  13422. idle_timeout = script_getnum(st, 3);
  13423. if( script_hasdata(st, 4) )
  13424. instance_id = script_getnum(st, 4);
  13425. else if( st->instance_id )
  13426. instance_id = st->instance_id;
  13427. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13428. instance_id = p->instance_id;
  13429. else return 0;
  13430. if( instance_id > 0 )
  13431. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  13432. return 0;
  13433. }
  13434. BUILDIN_FUNC(instance_init)
  13435. {
  13436. int instance_id = script_getnum(st, 2);
  13437. if( instance[instance_id].state != INSTANCE_IDLE )
  13438. {
  13439. ShowError("instance_init: instance already initialized.\n");
  13440. return 0;
  13441. }
  13442. instance_init(instance_id);
  13443. return 0;
  13444. }
  13445. BUILDIN_FUNC(instance_announce)
  13446. {
  13447. int instance_id = script_getnum(st,2);
  13448. const char *mes = script_getstr(st,3);
  13449. int flag = script_getnum(st,4);
  13450. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  13451. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  13452. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  13453. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  13454. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  13455. int i;
  13456. struct map_session_data *sd;
  13457. struct party_data *p;
  13458. if( instance_id == 0 )
  13459. {
  13460. if( st->instance_id )
  13461. instance_id = st->instance_id;
  13462. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13463. instance_id = p->instance_id;
  13464. else return 0;
  13465. }
  13466. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13467. return 0;
  13468. for( i = 0; i < instance[instance_id].num_map; i++ )
  13469. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  13470. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  13471. return 0;
  13472. }
  13473. BUILDIN_FUNC(instance_npcname)
  13474. {
  13475. const char *str;
  13476. int instance_id = 0;
  13477. struct map_session_data *sd;
  13478. struct party_data *p;
  13479. struct npc_data *nd;
  13480. str = script_getstr(st, 2);
  13481. if( script_hasdata(st, 3) )
  13482. instance_id = script_getnum(st, 3);
  13483. else 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. if( instance_id && (nd = npc_name2id(str)) != NULL )
  13488. {
  13489. static char npcname[NAME_LENGTH];
  13490. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  13491. script_pushconststr(st,npcname);
  13492. }
  13493. else
  13494. {
  13495. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  13496. st->state = END;
  13497. return 1;
  13498. }
  13499. return 0;
  13500. }
  13501. BUILDIN_FUNC(has_instance)
  13502. {
  13503. struct map_session_data *sd;
  13504. struct party_data *p;
  13505. const char *str;
  13506. int m, instance_id = 0;
  13507. str = script_getstr(st, 2);
  13508. if( script_hasdata(st, 3) )
  13509. instance_id = script_getnum(st, 3);
  13510. else if( st->instance_id )
  13511. instance_id = st->instance_id;
  13512. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13513. instance_id = p->instance_id;
  13514. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  13515. {
  13516. script_pushconststr(st, "");
  13517. return 0;
  13518. }
  13519. script_pushconststr(st, map[m].name);
  13520. return 0;
  13521. }
  13522. BUILDIN_FUNC(instance_warpall)
  13523. {
  13524. struct map_session_data *pl_sd;
  13525. int m, i, instance_id;
  13526. const char *mapn;
  13527. int x, y;
  13528. unsigned short mapindex;
  13529. struct party_data *p = NULL;
  13530. mapn = script_getstr(st,2);
  13531. x = script_getnum(st,3);
  13532. y = script_getnum(st,4);
  13533. if( script_hasdata(st,5) )
  13534. instance_id = script_getnum(st,5);
  13535. else if( st->instance_id )
  13536. instance_id = st->instance_id;
  13537. 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 )
  13538. instance_id = p->instance_id;
  13539. else return 0;
  13540. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  13541. return 0;
  13542. if( !(p = party_search(instance[instance_id].party_id)) )
  13543. return 0;
  13544. mapindex = map_id2index(m);
  13545. for( i = 0; i < MAX_PARTY; i++ )
  13546. 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);
  13547. return 0;
  13548. }
  13549. /*==========================================
  13550. * Custom Fonts
  13551. *------------------------------------------*/
  13552. BUILDIN_FUNC(setfont)
  13553. {
  13554. struct map_session_data *sd = script_rid2sd(st);
  13555. int font = script_getnum(st,2);
  13556. if( sd == NULL )
  13557. return 0;
  13558. if( sd->user_font != font )
  13559. sd->user_font = font;
  13560. else
  13561. sd->user_font = 0;
  13562. clif_font(sd);
  13563. return 0;
  13564. }
  13565. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  13566. {
  13567. TBL_MOB* md = (TBL_MOB*)bl;
  13568. struct block_list *tbl;
  13569. int mobid = va_arg(ap,int);
  13570. int skillid = va_arg(ap,int);
  13571. int skilllv = va_arg(ap,int);
  13572. int casttime = va_arg(ap,int);
  13573. int cancel = va_arg(ap,int);
  13574. int emotion = va_arg(ap,int);
  13575. int target = va_arg(ap,int);
  13576. if( md->class_ != mobid )
  13577. return 0;
  13578. // 0:self, 1:target, 2:master, default:random
  13579. switch( target )
  13580. {
  13581. case 0: tbl = map_id2bl(md->bl.id); break;
  13582. case 1: tbl = map_id2bl(md->target_id); break;
  13583. case 2: tbl = map_id2bl(md->master_id); break;
  13584. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  13585. }
  13586. if( !tbl )
  13587. return 0;
  13588. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  13589. unit_skillcastcancel(bl,0);
  13590. if( skill_get_casttype(skillid) == CAST_GROUND )
  13591. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  13592. else
  13593. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  13594. clif_emotion(&md->bl, emotion);
  13595. return 0;
  13596. }
  13597. /*==========================================
  13598. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  13599. *------------------------------------------*/
  13600. BUILDIN_FUNC(areamobuseskill)
  13601. {
  13602. struct block_list center;
  13603. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  13604. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  13605. {
  13606. ShowError("areamobuseskill: invalid map name.\n");
  13607. return 0;
  13608. }
  13609. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  13610. return 0;
  13611. center.m = m;
  13612. center.x = script_getnum(st,3);
  13613. center.y = script_getnum(st,4);
  13614. range = script_getnum(st,5);
  13615. mobid = script_getnum(st,6);
  13616. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  13617. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  13618. skilllv = battle_config.mob_max_skilllvl;
  13619. casttime = script_getnum(st,9);
  13620. cancel = script_getnum(st,10);
  13621. emotion = script_getnum(st,11);
  13622. target = script_getnum(st,12);
  13623. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  13624. return 0;
  13625. }
  13626. BUILDIN_FUNC(progressbar)
  13627. {
  13628. struct map_session_data * sd = script_rid2sd(st);
  13629. const char * color;
  13630. unsigned int second;
  13631. if( !st || !sd )
  13632. return 0;
  13633. st->state = STOP;
  13634. color = script_getstr(st,2);
  13635. second = script_getnum(st,3);
  13636. sd->progressbar.npc_id = st->oid;
  13637. sd->progressbar.timeout = gettick() + second*1000;
  13638. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  13639. return 0;
  13640. }
  13641. BUILDIN_FUNC(pushpc)
  13642. {
  13643. int direction, cells, dx, dy;
  13644. struct map_session_data* sd;
  13645. if((sd = script_rid2sd(st))==NULL)
  13646. {
  13647. return 0;
  13648. }
  13649. direction = script_getnum(st,2);
  13650. cells = script_getnum(st,3);
  13651. if(direction<0 || direction>7)
  13652. {
  13653. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  13654. script_reportsrc(st);
  13655. direction%= 8; // trim spin-over
  13656. }
  13657. if(!cells)
  13658. {// zero distance
  13659. return 0;
  13660. }
  13661. else if(cells<0)
  13662. {// pushing backwards
  13663. direction = (direction+4)%8; // turn around
  13664. cells = -cells;
  13665. }
  13666. dx = dirx[direction];
  13667. dy = diry[direction];
  13668. unit_blown(&sd->bl, dx, dy, cells, 0);
  13669. return 0;
  13670. }
  13671. /// Invokes buying store preparation window
  13672. /// buyingstore <slots>;
  13673. BUILDIN_FUNC(buyingstore)
  13674. {
  13675. struct map_session_data* sd;
  13676. if( ( sd = script_rid2sd(st) ) == NULL )
  13677. {
  13678. return 0;
  13679. }
  13680. buyingstore_setup(sd, script_getnum(st,2));
  13681. return 0;
  13682. }
  13683. /// Invokes search store info window
  13684. /// searchstores <uses>,<effect>;
  13685. BUILDIN_FUNC(searchstores)
  13686. {
  13687. unsigned short effect;
  13688. unsigned int uses;
  13689. struct map_session_data* sd;
  13690. if( ( sd = script_rid2sd(st) ) == NULL )
  13691. {
  13692. return 0;
  13693. }
  13694. uses = script_getnum(st,2);
  13695. effect = script_getnum(st,3);
  13696. if( !uses )
  13697. {
  13698. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  13699. return 1;
  13700. }
  13701. if( effect > 1 )
  13702. {
  13703. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  13704. return 1;
  13705. }
  13706. searchstore_open(sd, uses, effect);
  13707. return 0;
  13708. }
  13709. /// Displays a number as large digital clock.
  13710. /// showdigit <value>[,<type>];
  13711. BUILDIN_FUNC(showdigit)
  13712. {
  13713. unsigned int type = 0;
  13714. int value;
  13715. struct map_session_data* sd;
  13716. if( ( sd = script_rid2sd(st) ) == NULL )
  13717. {
  13718. return 0;
  13719. }
  13720. value = script_getnum(st,2);
  13721. if( script_hasdata(st,3) )
  13722. {
  13723. type = script_getnum(st,3);
  13724. if( type > 3 )
  13725. {
  13726. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  13727. return 1;
  13728. }
  13729. }
  13730. clif_showdigit(sd, (unsigned char)type, value);
  13731. return 0;
  13732. }
  13733. /**
  13734. * Rune Knight
  13735. **/
  13736. BUILDIN_FUNC(makerune) {
  13737. TBL_PC* sd;
  13738. if( (sd = script_rid2sd(st)) == NULL )
  13739. return 0;
  13740. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  13741. sd->itemid = script_getnum(st,2);
  13742. return 0;
  13743. }
  13744. /**
  13745. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  13746. **/
  13747. BUILDIN_FUNC(checkdragon) {
  13748. TBL_PC* sd;
  13749. if( (sd = script_rid2sd(st)) == NULL )
  13750. return 0;
  13751. if( sd->sc.option&OPTION_DRAGON )
  13752. script_pushint(st,1);
  13753. else
  13754. script_pushint(st,0);
  13755. return 0;
  13756. }
  13757. /**
  13758. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  13759. * - Toggles the dragon on a RK if he can mount;
  13760. * @param Color - when not provided uses the green dragon;
  13761. * - 1 : Green Dragon
  13762. * - 2 : Brown Dragon
  13763. * - 3 : Gray Dragon
  13764. * - 4 : Blue Dragon
  13765. * - 5 : Red Dragon
  13766. **/
  13767. BUILDIN_FUNC(setdragon) {
  13768. TBL_PC* sd;
  13769. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  13770. unsigned int option = OPTION_DRAGON1;
  13771. if( (sd = script_rid2sd(st)) == NULL )
  13772. return 0;
  13773. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  13774. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  13775. else if ( sd->sc.option&OPTION_DRAGON ) {//Is mounted; release
  13776. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  13777. script_pushint(st,1);
  13778. } else {//Not mounted; Mount now.
  13779. if( color ) {
  13780. option = ( color == 1 ? OPTION_DRAGON1 :
  13781. color == 2 ? OPTION_DRAGON2 :
  13782. color == 3 ? OPTION_DRAGON3 :
  13783. color == 4 ? OPTION_DRAGON4 :
  13784. color == 5 ? OPTION_DRAGON5 : 0);
  13785. if( !option ) {
  13786. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  13787. option = OPTION_DRAGON1;
  13788. }
  13789. }
  13790. pc_setoption(sd, sd->sc.option|option);
  13791. script_pushint(st,1);
  13792. }
  13793. return 0;
  13794. }
  13795. /**
  13796. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  13797. **/
  13798. BUILDIN_FUNC(ismounting) {
  13799. TBL_PC* sd;
  13800. if( (sd = script_rid2sd(st)) == NULL )
  13801. return 0;
  13802. if( sd->sc.option&OPTION_MOUNTING )
  13803. script_pushint(st,1);
  13804. else
  13805. script_pushint(st,0);
  13806. return 0;
  13807. }
  13808. /**
  13809. * setmounting() returns 1 on success or 0 otherwise
  13810. * - Toggles new mounts on a player when he can mount
  13811. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  13812. * - Will unmount the player is he is already mounting
  13813. **/
  13814. BUILDIN_FUNC(setmounting) {
  13815. TBL_PC* sd;
  13816. if( (sd = script_rid2sd(st)) == NULL )
  13817. return 0;
  13818. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) || pc_cant_newmount(sd) )
  13819. script_pushint(st,0);//can't mount with one of these
  13820. else {
  13821. if( sd->sc.option&OPTION_MOUNTING )
  13822. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  13823. else
  13824. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  13825. script_pushint(st,1);//in both cases, return 1.
  13826. }
  13827. return 0;
  13828. }
  13829. /**
  13830. * Retrieves quantity of arguments provided to callfunc/callsub.
  13831. * getargcount() -> amount of arguments received in a function
  13832. **/
  13833. BUILDIN_FUNC(getargcount) {
  13834. struct script_retinfo* ri;
  13835. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  13836. ShowError("script:getargcount: used out of function or callsub label!\n");
  13837. st->state = END;
  13838. return 1;
  13839. }
  13840. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  13841. script_pushint(st, ri->nargs);
  13842. return 0;
  13843. }
  13844. /**
  13845. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  13846. **/
  13847. BUILDIN_FUNC(is_function) {
  13848. const char* str = script_getstr(st,2);
  13849. if( ((struct script_code*)strdb_get(userfunc_db, str)) != NULL )
  13850. script_pushint(st,1);
  13851. else
  13852. script_pushint(st,0);
  13853. return 0;
  13854. }
  13855. /**
  13856. * get_revision() -> retrieves the current svn revision (if available)
  13857. **/
  13858. BUILDIN_FUNC(get_revision) {
  13859. const char * revision;
  13860. if ( (revision = get_svn_revision()) != 0 )
  13861. script_pushstrcopy(st,revision);
  13862. else
  13863. script_pushconststr(st,"Unknown");
  13864. return 0;
  13865. }
  13866. /**
  13867. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  13868. **/
  13869. BUILDIN_FUNC(freeloop) {
  13870. if( script_getnum(st,2) )
  13871. st->freeloop = 1;
  13872. else
  13873. st->freeloop = 0;
  13874. script_pushint(st, st->freeloop);
  13875. return 0;
  13876. }
  13877. // declarations that were supposed to be exported from npc_chat.c
  13878. #ifdef PCRE_SUPPORT
  13879. BUILDIN_FUNC(defpattern);
  13880. BUILDIN_FUNC(activatepset);
  13881. BUILDIN_FUNC(deactivatepset);
  13882. BUILDIN_FUNC(deletepset);
  13883. #endif
  13884. /// script command definitions
  13885. /// for an explanation on args, see add_buildin_func
  13886. struct script_function buildin_func[] = {
  13887. // NPC interaction
  13888. BUILDIN_DEF(mes,"s*"),
  13889. BUILDIN_DEF(next,""),
  13890. BUILDIN_DEF(close,""),
  13891. BUILDIN_DEF(close2,""),
  13892. BUILDIN_DEF(menu,"sl*"),
  13893. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  13894. BUILDIN_DEF(prompt,"s*"),
  13895. //
  13896. BUILDIN_DEF(goto,"l"),
  13897. BUILDIN_DEF(callsub,"l*"),
  13898. BUILDIN_DEF(callfunc,"s*"),
  13899. BUILDIN_DEF(return,"?"),
  13900. BUILDIN_DEF(getarg,"i?"),
  13901. BUILDIN_DEF(jobchange,"i?"),
  13902. BUILDIN_DEF(jobname,"i"),
  13903. BUILDIN_DEF(input,"r??"),
  13904. BUILDIN_DEF(warp,"sii"),
  13905. BUILDIN_DEF(areawarp,"siiiisii"),
  13906. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  13907. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  13908. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  13909. BUILDIN_DEF(setlook,"ii"),
  13910. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  13911. BUILDIN_DEF(set,"rv"),
  13912. BUILDIN_DEF(setarray,"rv*"),
  13913. BUILDIN_DEF(cleararray,"rvi"),
  13914. BUILDIN_DEF(copyarray,"rri"),
  13915. BUILDIN_DEF(getarraysize,"r"),
  13916. BUILDIN_DEF(deletearray,"r?"),
  13917. BUILDIN_DEF(getelementofarray,"ri"),
  13918. BUILDIN_DEF(getitem,"vi?"),
  13919. BUILDIN_DEF(rentitem,"vi"),
  13920. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  13921. BUILDIN_DEF(getnameditem,"vv"),
  13922. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  13923. BUILDIN_DEF(makeitem,"visii"),
  13924. BUILDIN_DEF(delitem,"vi?"),
  13925. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  13926. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  13927. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  13928. BUILDIN_DEF(cutin,"si"),
  13929. BUILDIN_DEF(viewpoint,"iiiii"),
  13930. BUILDIN_DEF(heal,"ii"),
  13931. BUILDIN_DEF(itemheal,"ii"),
  13932. BUILDIN_DEF(percentheal,"ii"),
  13933. BUILDIN_DEF(rand,"i?"),
  13934. BUILDIN_DEF(countitem,"v"),
  13935. BUILDIN_DEF(countitem2,"viiiiiii"),
  13936. BUILDIN_DEF(checkweight,"vi"),
  13937. BUILDIN_DEF(readparam,"i?"),
  13938. BUILDIN_DEF(getcharid,"i?"),
  13939. BUILDIN_DEF(getnpcid,"i?"),
  13940. BUILDIN_DEF(getpartyname,"i"),
  13941. BUILDIN_DEF(getpartymember,"i?"),
  13942. BUILDIN_DEF(getpartyleader,"i?"),
  13943. BUILDIN_DEF(getguildname,"i"),
  13944. BUILDIN_DEF(getguildmaster,"i"),
  13945. BUILDIN_DEF(getguildmasterid,"i"),
  13946. BUILDIN_DEF(strcharinfo,"i"),
  13947. BUILDIN_DEF(strnpcinfo,"i"),
  13948. BUILDIN_DEF(getequipid,"i"),
  13949. BUILDIN_DEF(getequipname,"i"),
  13950. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  13951. BUILDIN_DEF(repair,"i"), // [Valaris]
  13952. BUILDIN_DEF(getequipisequiped,"i"),
  13953. BUILDIN_DEF(getequipisenableref,"i"),
  13954. BUILDIN_DEF(getequipisidentify,"i"),
  13955. BUILDIN_DEF(getequiprefinerycnt,"i"),
  13956. BUILDIN_DEF(getequipweaponlv,"i"),
  13957. BUILDIN_DEF(getequippercentrefinery,"i"),
  13958. BUILDIN_DEF(successrefitem,"i"),
  13959. BUILDIN_DEF(failedrefitem,"i"),
  13960. BUILDIN_DEF(statusup,"i"),
  13961. BUILDIN_DEF(statusup2,"ii"),
  13962. BUILDIN_DEF(bonus,"iv"),
  13963. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  13964. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  13965. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  13966. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  13967. BUILDIN_DEF(autobonus,"sii??"),
  13968. BUILDIN_DEF(autobonus2,"sii??"),
  13969. BUILDIN_DEF(autobonus3,"siiv?"),
  13970. BUILDIN_DEF(skill,"vi?"),
  13971. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  13972. BUILDIN_DEF(guildskill,"vi"),
  13973. BUILDIN_DEF(getskilllv,"v"),
  13974. BUILDIN_DEF(getgdskilllv,"iv"),
  13975. BUILDIN_DEF(basicskillcheck,""),
  13976. BUILDIN_DEF(getgmlevel,""),
  13977. BUILDIN_DEF(getgroupid,""),
  13978. BUILDIN_DEF(end,""),
  13979. BUILDIN_DEF(checkoption,"i"),
  13980. BUILDIN_DEF(setoption,"i?"),
  13981. BUILDIN_DEF(setcart,"?"),
  13982. BUILDIN_DEF(checkcart,""),
  13983. BUILDIN_DEF(setfalcon,"?"),
  13984. BUILDIN_DEF(checkfalcon,""),
  13985. BUILDIN_DEF(setriding,"?"),
  13986. BUILDIN_DEF(checkriding,""),
  13987. BUILDIN_DEF2(savepoint,"save","sii"),
  13988. BUILDIN_DEF(savepoint,"sii"),
  13989. BUILDIN_DEF(gettimetick,"i"),
  13990. BUILDIN_DEF(gettime,"i"),
  13991. BUILDIN_DEF(gettimestr,"si"),
  13992. BUILDIN_DEF(openstorage,""),
  13993. BUILDIN_DEF(guildopenstorage,""),
  13994. BUILDIN_DEF(itemskill,"vi"),
  13995. BUILDIN_DEF(produce,"i"),
  13996. BUILDIN_DEF(cooking,"i"),
  13997. BUILDIN_DEF(monster,"siisii?"),
  13998. BUILDIN_DEF(getmobdrops,"i"),
  13999. BUILDIN_DEF(areamonster,"siiiisii?"),
  14000. BUILDIN_DEF(killmonster,"ss?"),
  14001. BUILDIN_DEF(killmonsterall,"s?"),
  14002. BUILDIN_DEF(clone,"siisi????"),
  14003. BUILDIN_DEF(doevent,"s"),
  14004. BUILDIN_DEF(donpcevent,"s"),
  14005. BUILDIN_DEF(cmdothernpc,"ss"),
  14006. BUILDIN_DEF(addtimer,"is"),
  14007. BUILDIN_DEF(deltimer,"s"),
  14008. BUILDIN_DEF(addtimercount,"si"),
  14009. BUILDIN_DEF(initnpctimer,"??"),
  14010. BUILDIN_DEF(stopnpctimer,"??"),
  14011. BUILDIN_DEF(startnpctimer,"??"),
  14012. BUILDIN_DEF(setnpctimer,"i?"),
  14013. BUILDIN_DEF(getnpctimer,"i?"),
  14014. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  14015. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  14016. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  14017. BUILDIN_DEF(announce,"si?????"),
  14018. BUILDIN_DEF(mapannounce,"ssi?????"),
  14019. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  14020. BUILDIN_DEF(getusers,"i"),
  14021. BUILDIN_DEF(getmapguildusers,"si"),
  14022. BUILDIN_DEF(getmapusers,"s"),
  14023. BUILDIN_DEF(getareausers,"siiii"),
  14024. BUILDIN_DEF(getareadropitem,"siiiiv"),
  14025. BUILDIN_DEF(enablenpc,"s"),
  14026. BUILDIN_DEF(disablenpc,"s"),
  14027. BUILDIN_DEF(hideoffnpc,"s"),
  14028. BUILDIN_DEF(hideonnpc,"s"),
  14029. BUILDIN_DEF(sc_start,"iii?"),
  14030. BUILDIN_DEF(sc_start2,"iiii?"),
  14031. BUILDIN_DEF(sc_start4,"iiiiii?"),
  14032. BUILDIN_DEF(sc_end,"i?"),
  14033. BUILDIN_DEF(getstatus, "i?"),
  14034. BUILDIN_DEF(getscrate,"ii?"),
  14035. BUILDIN_DEF(debugmes,"s"),
  14036. BUILDIN_DEF2(catchpet,"pet","i"),
  14037. BUILDIN_DEF2(birthpet,"bpet",""),
  14038. BUILDIN_DEF(resetlvl,"i"),
  14039. BUILDIN_DEF(resetstatus,""),
  14040. BUILDIN_DEF(resetskill,""),
  14041. BUILDIN_DEF(skillpointcount,""),
  14042. BUILDIN_DEF(changebase,"i?"),
  14043. BUILDIN_DEF(changesex,""),
  14044. BUILDIN_DEF(waitingroom,"si?????"),
  14045. BUILDIN_DEF(delwaitingroom,"?"),
  14046. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  14047. BUILDIN_DEF(enablewaitingroomevent,"?"),
  14048. BUILDIN_DEF(disablewaitingroomevent,"?"),
  14049. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  14050. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  14051. BUILDIN_DEF(getwaitingroomstate,"i?"),
  14052. BUILDIN_DEF(warpwaitingpc,"sii?"),
  14053. BUILDIN_DEF(attachrid,"i"),
  14054. BUILDIN_DEF(detachrid,""),
  14055. BUILDIN_DEF(isloggedin,"i?"),
  14056. BUILDIN_DEF(setmapflagnosave,"ssii"),
  14057. BUILDIN_DEF(getmapflag,"si"),
  14058. BUILDIN_DEF(setmapflag,"si?"),
  14059. BUILDIN_DEF(removemapflag,"si?"),
  14060. BUILDIN_DEF(pvpon,"s"),
  14061. BUILDIN_DEF(pvpoff,"s"),
  14062. BUILDIN_DEF(gvgon,"s"),
  14063. BUILDIN_DEF(gvgoff,"s"),
  14064. BUILDIN_DEF(emotion,"i??"),
  14065. BUILDIN_DEF(maprespawnguildid,"sii"),
  14066. BUILDIN_DEF(agitstart,""), // <Agit>
  14067. BUILDIN_DEF(agitend,""),
  14068. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  14069. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  14070. BUILDIN_DEF(getcastlename,"s"),
  14071. BUILDIN_DEF(getcastledata,"si"),
  14072. BUILDIN_DEF(setcastledata,"sii"),
  14073. BUILDIN_DEF(requestguildinfo,"i?"),
  14074. BUILDIN_DEF(getequipcardcnt,"i"),
  14075. BUILDIN_DEF(successremovecards,"i"),
  14076. BUILDIN_DEF(failedremovecards,"ii"),
  14077. BUILDIN_DEF(marriage,"s"),
  14078. BUILDIN_DEF2(wedding_effect,"wedding",""),
  14079. BUILDIN_DEF(divorce,""),
  14080. BUILDIN_DEF(ispartneron,""),
  14081. BUILDIN_DEF(getpartnerid,""),
  14082. BUILDIN_DEF(getchildid,""),
  14083. BUILDIN_DEF(getmotherid,""),
  14084. BUILDIN_DEF(getfatherid,""),
  14085. BUILDIN_DEF(warppartner,"sii"),
  14086. BUILDIN_DEF(getitemname,"v"),
  14087. BUILDIN_DEF(getitemslots,"i"),
  14088. BUILDIN_DEF(makepet,"i"),
  14089. BUILDIN_DEF(getexp,"ii"),
  14090. BUILDIN_DEF(getinventorylist,""),
  14091. BUILDIN_DEF(getskilllist,""),
  14092. BUILDIN_DEF(clearitem,""),
  14093. BUILDIN_DEF(classchange,"ii"),
  14094. BUILDIN_DEF(misceffect,"i"),
  14095. BUILDIN_DEF(playBGM,"s"),
  14096. BUILDIN_DEF(playBGMall,"s?????"),
  14097. BUILDIN_DEF(soundeffect,"si"),
  14098. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  14099. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  14100. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  14101. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  14102. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  14103. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  14104. BUILDIN_DEF(petloot,"i"), // [Valaris]
  14105. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  14106. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  14107. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  14108. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  14109. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  14110. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  14111. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  14112. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  14113. BUILDIN_DEF(nude,""), // nude command [Valaris]
  14114. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  14115. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  14116. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  14117. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  14118. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  14119. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  14120. BUILDIN_DEF(mobcount,"ss"),
  14121. BUILDIN_DEF(getlook,"i"),
  14122. BUILDIN_DEF(getsavepoint,"i"),
  14123. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  14124. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  14125. BUILDIN_DEF(npcstop,""), // [Valaris]
  14126. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  14127. BUILDIN_DEF(checkoption1,"i"),
  14128. BUILDIN_DEF(checkoption2,"i"),
  14129. BUILDIN_DEF(guildgetexp,"i"),
  14130. BUILDIN_DEF(guildchangegm,"is"),
  14131. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  14132. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  14133. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  14134. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  14135. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  14136. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  14137. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  14138. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  14139. BUILDIN_DEF(night,""), // sets the server to night time
  14140. BUILDIN_DEF(day,""), // sets the server to day time
  14141. #ifdef PCRE_SUPPORT
  14142. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  14143. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  14144. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  14145. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  14146. #endif
  14147. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  14148. BUILDIN_DEF(getusersname,""),
  14149. BUILDIN_DEF(recovery,""),
  14150. BUILDIN_DEF(getpetinfo,"i"),
  14151. BUILDIN_DEF(gethominfo,"i"),
  14152. BUILDIN_DEF(getmercinfo,"i?"),
  14153. BUILDIN_DEF(checkequipedcard,"i"),
  14154. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  14155. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  14156. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  14157. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  14158. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  14159. BUILDIN_DEF(charat,"si"),
  14160. BUILDIN_DEF(setchar,"ssi"),
  14161. BUILDIN_DEF(insertchar,"ssi"),
  14162. BUILDIN_DEF(delchar,"si"),
  14163. BUILDIN_DEF(strtoupper,"s"),
  14164. BUILDIN_DEF(strtolower,"s"),
  14165. BUILDIN_DEF(charisupper, "si"),
  14166. BUILDIN_DEF(charislower, "si"),
  14167. BUILDIN_DEF(substr,"sii"),
  14168. BUILDIN_DEF(explode, "rss"),
  14169. BUILDIN_DEF(implode, "r?"),
  14170. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  14171. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  14172. BUILDIN_DEF(strpos,"ss?"),
  14173. BUILDIN_DEF(replacestr,"sss??"),
  14174. BUILDIN_DEF(countstr,"ss?"),
  14175. BUILDIN_DEF(setnpcdisplay,"sv??"),
  14176. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  14177. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  14178. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  14179. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  14180. // [zBuffer] List of mathematics commands --->
  14181. BUILDIN_DEF(sqrt,"i"),
  14182. BUILDIN_DEF(pow,"ii"),
  14183. BUILDIN_DEF(distance,"iiii"),
  14184. // <--- [zBuffer] List of mathematics commands
  14185. BUILDIN_DEF(md5,"s"),
  14186. // [zBuffer] List of dynamic var commands --->
  14187. BUILDIN_DEF(getd,"s"),
  14188. BUILDIN_DEF(setd,"sv"),
  14189. // <--- [zBuffer] List of dynamic var commands
  14190. BUILDIN_DEF(petstat,"i"),
  14191. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  14192. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  14193. BUILDIN_DEF(npcshopadditem,"sii*"),
  14194. BUILDIN_DEF(npcshopdelitem,"si*"),
  14195. BUILDIN_DEF(npcshopattach,"s?"),
  14196. BUILDIN_DEF(equip,"i"),
  14197. BUILDIN_DEF(autoequip,"ii"),
  14198. BUILDIN_DEF(setbattleflag,"si"),
  14199. BUILDIN_DEF(getbattleflag,"s"),
  14200. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  14201. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  14202. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  14203. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  14204. BUILDIN_DEF(axtoi,"s"),
  14205. BUILDIN_DEF(query_sql,"s*"),
  14206. BUILDIN_DEF(query_logsql,"s*"),
  14207. BUILDIN_DEF(escape_sql,"s"),
  14208. BUILDIN_DEF(atoi,"s"),
  14209. // [zBuffer] List of player cont commands --->
  14210. BUILDIN_DEF(rid2name,"i"),
  14211. BUILDIN_DEF(pcfollow,"ii"),
  14212. BUILDIN_DEF(pcstopfollow,"i"),
  14213. BUILDIN_DEF(pcblockmove,"ii"),
  14214. // <--- [zBuffer] List of player cont commands
  14215. // [zBuffer] List of mob control commands --->
  14216. BUILDIN_DEF(unitwalk,"ii?"),
  14217. BUILDIN_DEF(unitkill,"i"),
  14218. BUILDIN_DEF(unitwarp,"isii"),
  14219. BUILDIN_DEF(unitattack,"iv?"),
  14220. BUILDIN_DEF(unitstop,"i"),
  14221. BUILDIN_DEF(unittalk,"is"),
  14222. BUILDIN_DEF(unitemote,"ii"),
  14223. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  14224. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  14225. // <--- [zBuffer] List of mob control commands
  14226. BUILDIN_DEF(sleep,"i"),
  14227. BUILDIN_DEF(sleep2,"i"),
  14228. BUILDIN_DEF(awake,"s"),
  14229. BUILDIN_DEF(getvariableofnpc,"rs"),
  14230. BUILDIN_DEF(warpportal,"iisii"),
  14231. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  14232. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  14233. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  14234. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  14235. BUILDIN_DEF(checkvending,"?"),
  14236. BUILDIN_DEF(checkchatting,"?"),
  14237. BUILDIN_DEF(openmail,""),
  14238. BUILDIN_DEF(openauction,""),
  14239. BUILDIN_DEF(checkcell,"siii"),
  14240. BUILDIN_DEF(setcell,"siiiiii"),
  14241. BUILDIN_DEF(setwall,"siiiiis"),
  14242. BUILDIN_DEF(delwall,"s"),
  14243. BUILDIN_DEF(searchitem,"rs"),
  14244. BUILDIN_DEF(mercenary_create,"ii"),
  14245. BUILDIN_DEF(mercenary_heal,"ii"),
  14246. BUILDIN_DEF(mercenary_sc_start,"iii"),
  14247. BUILDIN_DEF(mercenary_get_calls,"i"),
  14248. BUILDIN_DEF(mercenary_get_faith,"i"),
  14249. BUILDIN_DEF(mercenary_set_calls,"ii"),
  14250. BUILDIN_DEF(mercenary_set_faith,"ii"),
  14251. BUILDIN_DEF(readbook,"ii"),
  14252. BUILDIN_DEF(setfont,"i"),
  14253. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  14254. BUILDIN_DEF(progressbar,"si"),
  14255. BUILDIN_DEF(pushpc,"ii"),
  14256. BUILDIN_DEF(buyingstore,"i"),
  14257. BUILDIN_DEF(searchstores,"ii"),
  14258. BUILDIN_DEF(showdigit,"i?"),
  14259. // WoE SE
  14260. BUILDIN_DEF(agitstart2,""),
  14261. BUILDIN_DEF(agitend2,""),
  14262. BUILDIN_DEF(agitcheck2,""),
  14263. // BattleGround
  14264. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  14265. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  14266. BUILDIN_DEF(bg_team_setxy,"iii"),
  14267. BUILDIN_DEF(bg_warp,"isii"),
  14268. BUILDIN_DEF(bg_monster,"isiisi?"),
  14269. BUILDIN_DEF(bg_monster_set_team,"ii"),
  14270. BUILDIN_DEF(bg_leave,""),
  14271. BUILDIN_DEF(bg_destroy,"i"),
  14272. BUILDIN_DEF(areapercentheal,"siiiiii"),
  14273. BUILDIN_DEF(bg_get_data,"ii"),
  14274. BUILDIN_DEF(bg_getareausers,"isiiii"),
  14275. BUILDIN_DEF(bg_updatescore,"sii"),
  14276. // Instancing
  14277. BUILDIN_DEF(instance_create,"si"),
  14278. BUILDIN_DEF(instance_destroy,"?"),
  14279. BUILDIN_DEF(instance_attachmap,"si?"),
  14280. BUILDIN_DEF(instance_detachmap,"s?"),
  14281. BUILDIN_DEF(instance_attach,"i"),
  14282. BUILDIN_DEF(instance_id,"?"),
  14283. BUILDIN_DEF(instance_set_timeout,"ii?"),
  14284. BUILDIN_DEF(instance_init,"i"),
  14285. BUILDIN_DEF(instance_announce,"isi?????"),
  14286. BUILDIN_DEF(instance_npcname,"s?"),
  14287. BUILDIN_DEF(has_instance,"s?"),
  14288. BUILDIN_DEF(instance_warpall,"sii?"),
  14289. /**
  14290. * 3rd-related
  14291. **/
  14292. BUILDIN_DEF(makerune,"i"),
  14293. BUILDIN_DEF(checkdragon,""),//[Ind]
  14294. BUILDIN_DEF(setdragon,"?"),//[Ind]
  14295. BUILDIN_DEF(ismounting,""),//[Ind]
  14296. BUILDIN_DEF(setmounting,""),//[Ind]
  14297. /**
  14298. * rAthena and beyond!
  14299. **/
  14300. BUILDIN_DEF(getargcount,""),
  14301. BUILDIN_DEF(is_function,"s"),
  14302. BUILDIN_DEF(get_revision,""),
  14303. BUILDIN_DEF(freeloop,"i"),
  14304. //Quest Log System [Inkfish]
  14305. BUILDIN_DEF(setquest, "i"),
  14306. BUILDIN_DEF(erasequest, "i"),
  14307. BUILDIN_DEF(completequest, "i"),
  14308. BUILDIN_DEF(checkquest, "i?"),
  14309. BUILDIN_DEF(changequest, "ii"),
  14310. BUILDIN_DEF(showevent, "ii"),
  14311. {NULL,NULL,NULL},
  14312. };