script.c 396 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. //#define DEBUG_DUMP_STACK
  8. #include "../common/cbasetypes.h"
  9. #include "../common/malloc.h"
  10. #include "../common/md5calc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/timer.h"
  16. #include "../common/utils.h"
  17. #include "map.h"
  18. #include "path.h"
  19. #include "clif.h"
  20. #include "chrif.h"
  21. #include "itemdb.h"
  22. #include "pc.h"
  23. #include "status.h"
  24. #include "storage.h"
  25. #include "mob.h"
  26. #include "npc.h"
  27. #include "pet.h"
  28. #include "mapreg.h"
  29. #include "homunculus.h"
  30. #include "instance.h"
  31. #include "mercenary.h"
  32. #include "intif.h"
  33. #include "skill.h"
  34. #include "status.h"
  35. #include "chat.h"
  36. #include "battle.h"
  37. #include "battleground.h"
  38. #include "party.h"
  39. #include "guild.h"
  40. #include "atcommand.h"
  41. #include "log.h"
  42. #include "unit.h"
  43. #include "pet.h"
  44. #include "mail.h"
  45. #include "script.h"
  46. #include "quest.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <math.h>
  51. #ifndef WIN32
  52. #include <sys/time.h>
  53. #endif
  54. #include <time.h>
  55. #include <setjmp.h>
  56. #include <errno.h>
  57. ///////////////////////////////////////////////////////////////////////////////
  58. //## TODO possible enhancements: [FlavioJS]
  59. // - 'callfunc' supporting labels in the current npc "::LabelName"
  60. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  61. // - 'function FuncName;' function declarations reverting to global functions
  62. // if local label isn't found
  63. // - join callfunc and callsub's functionality
  64. // - remove dynamic allocation in add_word()
  65. // - remove GETVALUE / SETVALUE
  66. // - clean up the set_reg / set_val / setd_sub mess
  67. // - detect invalid label references at parse-time
  68. //
  69. // struct script_state* st;
  70. //
  71. /// Returns the script_data at the target index
  72. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  73. /// Returns if the stack contains data at the target index
  74. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  75. /// Returns the index of the last data in the stack
  76. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  77. /// Pushes an int into the stack
  78. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  79. /// Pushes a string into the stack (script engine frees it automatically)
  80. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  81. /// Pushes a copy of a string into the stack
  82. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  83. /// Pushes a constant string into the stack (must never change or be freed)
  84. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  85. /// Pushes a nil into the stack
  86. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  87. /// Pushes a copy of the data in the target index
  88. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  89. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  90. #define script_isint(st,i) data_isint(script_getdata(st,i))
  91. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  92. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  93. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  94. // Note: "top" functions/defines use indexes relative to the top of the stack
  95. // -1 is the index of the data at the top
  96. /// Returns the script_data at the target index relative to the top of the stack
  97. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  98. /// Pushes a copy of the data in the target index relative to the top of the stack
  99. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  100. /// Removes the range of values [start,end[ relative to the top of the stack
  101. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  102. //
  103. // struct script_data* data;
  104. //
  105. /// Returns if the script data is a string
  106. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  107. /// Returns if the script data is an int
  108. #define data_isint(data) ( (data)->type == C_INT )
  109. /// Returns if the script data is a reference
  110. #define data_isreference(data) ( (data)->type == C_NAME )
  111. /// Returns if the script data is a label
  112. #define data_islabel(data) ( (data)->type == C_POS )
  113. /// Returns if the script data is an internal script function label
  114. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  115. /// Returns if this is a reference to a constant
  116. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  117. /// Returns if this a reference to a param
  118. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  119. /// Returns if this a reference to a variable
  120. //##TODO confirm it's C_NAME [FlavioJS]
  121. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  122. /// Returns the unique id of the reference (id and index)
  123. #define reference_getuid(data) ( (data)->u.num )
  124. /// Returns the id of the reference
  125. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  126. /// Returns the array index of the reference
  127. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  128. /// Returns the name of the reference
  129. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  130. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  131. #define reference_getref(data) ( (data)->ref )
  132. /// Returns the value of the constant
  133. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  134. /// Returns the type of param
  135. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  136. /// Composes the uid of a reference from the id and the index
  137. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  138. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  139. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  140. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  141. #define FETCH(n, t) \
  142. if( script_hasdata(st,n) ) \
  143. (t)=script_getnum(st,n);
  144. /// Maximum amount of elements in script arrays
  145. #define SCRIPT_MAX_ARRAYSIZE 128
  146. #define SCRIPT_BLOCK_SIZE 512
  147. enum { LABEL_NEXTLINE=1,LABEL_START };
  148. /// temporary buffer for passing around compiled bytecode
  149. /// @see add_scriptb, set_label, parse_script
  150. static unsigned char* script_buf = NULL;
  151. static int script_pos = 0, script_size = 0;
  152. static inline int GETVALUE(const unsigned char* buf, int i)
  153. {
  154. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  155. }
  156. static inline void SETVALUE(unsigned char* buf, int i, int n)
  157. {
  158. buf[i] = GetByte(n, 0);
  159. buf[i+1] = GetByte(n, 1);
  160. buf[i+2] = GetByte(n, 2);
  161. }
  162. // String buffer structures.
  163. // str_data stores string information
  164. static struct str_data_struct {
  165. enum c_op type;
  166. int str;
  167. int backpatch;
  168. int label;
  169. int (*func)(struct script_state *st);
  170. int val;
  171. int next;
  172. } *str_data = NULL;
  173. static int str_data_size = 0; // size of the data
  174. static int str_num = LABEL_START; // next id to be assigned
  175. // str_buf holds the strings themselves
  176. static char *str_buf;
  177. static int str_size = 0; // size of the buffer
  178. static int str_pos = 0; // next position to be assigned
  179. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  180. #define SCRIPT_HASH_SIZE 1021
  181. int str_hash[SCRIPT_HASH_SIZE];
  182. // Specifies which string hashing method to use
  183. //#define SCRIPT_HASH_DJB2
  184. //#define SCRIPT_HASH_SDBM
  185. #define SCRIPT_HASH_ELF
  186. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  187. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  188. static int parse_options=0;
  189. DBMap* script_get_label_db(){ return scriptlabel_db; }
  190. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  191. // Caches compiled autoscript item code.
  192. // Note: This is not cleared when reloading itemdb.
  193. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  194. struct Script_Config script_config = {
  195. 1, // warn_func_mismatch_argtypes
  196. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  197. 0, INT_MAX, // input_min_value/input_max_value
  198. "OnPCDieEvent", //die_event_name
  199. "OnPCKillEvent", //kill_pc_event_name
  200. "OnNPCKillEvent", //kill_mob_event_name
  201. "OnPCLoginEvent", //login_event_name
  202. "OnPCLogoutEvent", //logout_event_name
  203. "OnPCLoadMapEvent", //loadmap_event_name
  204. "OnPCBaseLvUpEvent", //baselvup_event_name
  205. "OnPCJobLvUpEvent", //joblvup_event_name
  206. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  207. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  208. };
  209. static jmp_buf error_jump;
  210. static char* error_msg;
  211. static const char* error_pos;
  212. static int error_report; // if the error should produce output
  213. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  214. // [Eoe / jA 1080, 1081, 1094, 1164]
  215. enum curly_type {
  216. TYPE_NULL = 0,
  217. TYPE_IF,
  218. TYPE_SWITCH,
  219. TYPE_WHILE,
  220. TYPE_FOR,
  221. TYPE_DO,
  222. TYPE_USERFUNC,
  223. TYPE_ARGLIST // function argument list
  224. };
  225. enum e_arglist
  226. {
  227. ARGLIST_UNDEFINED = 0,
  228. ARGLIST_NO_PAREN = 1,
  229. ARGLIST_PAREN = 2,
  230. };
  231. static struct {
  232. struct {
  233. enum curly_type type;
  234. int index;
  235. int count;
  236. int flag;
  237. struct linkdb_node *case_label;
  238. } curly[256]; // 右カッコの情報
  239. int curly_count; // 右カッコの数
  240. int index; // スクリプト内で使用した構文の数
  241. } syntax;
  242. const char* parse_curly_close(const char* p);
  243. const char* parse_syntax_close(const char* p);
  244. const char* parse_syntax_close_sub(const char* p,int* flag);
  245. const char* parse_syntax(const char* p);
  246. static int parse_syntax_for_flag = 0;
  247. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  248. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  249. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  250. int potion_target=0;
  251. c_op get_com(unsigned char *script,int *pos);
  252. int get_num(unsigned char *script,int *pos);
  253. typedef struct script_function {
  254. int (*func)(struct script_state *st);
  255. const char *name;
  256. const char *arg;
  257. } script_function;
  258. extern script_function buildin_func[];
  259. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  260. /*==========================================
  261. * ローカルプロトタイプ宣言 (必要な物のみ)
  262. *------------------------------------------*/
  263. const char* parse_subexpr(const char* p,int limit);
  264. int run_func(struct script_state *st);
  265. enum {
  266. MF_NOMEMO, //0
  267. MF_NOTELEPORT,
  268. MF_NOSAVE,
  269. MF_NOBRANCH,
  270. MF_NOPENALTY,
  271. MF_NOZENYPENALTY,
  272. MF_PVP,
  273. MF_PVP_NOPARTY,
  274. MF_PVP_NOGUILD,
  275. MF_GVG,
  276. MF_GVG_NOPARTY, //10
  277. MF_NOTRADE,
  278. MF_NOSKILL,
  279. MF_NOWARP,
  280. MF_PARTYLOCK,
  281. MF_NOICEWALL,
  282. MF_SNOW,
  283. MF_FOG,
  284. MF_SAKURA,
  285. MF_LEAVES,
  286. /**
  287. * No longer available, keeping here just in case it's back someday. [Ind]
  288. **/
  289. //MF_RAIN, //20
  290. // 21 free
  291. MF_NOGO = 22,
  292. MF_CLOUDS,
  293. MF_CLOUDS2,
  294. MF_FIREWORKS,
  295. MF_GVG_CASTLE,
  296. MF_GVG_DUNGEON,
  297. MF_NIGHTENABLED,
  298. MF_NOBASEEXP,
  299. MF_NOJOBEXP, //30
  300. MF_NOMOBLOOT,
  301. MF_NOMVPLOOT,
  302. MF_NORETURN,
  303. MF_NOWARPTO,
  304. MF_NIGHTMAREDROP,
  305. MF_RESTRICTED,
  306. MF_NOCOMMAND,
  307. MF_NODROP,
  308. MF_JEXP,
  309. MF_BEXP, //40
  310. MF_NOVENDING,
  311. MF_LOADEVENT,
  312. MF_NOCHAT,
  313. MF_NOEXPPENALTY,
  314. MF_GUILDLOCK,
  315. MF_TOWN,
  316. MF_AUTOTRADE,
  317. MF_ALLOWKS,
  318. MF_MONSTER_NOTELEPORT,
  319. MF_PVP_NOCALCRANK, //50
  320. MF_BATTLEGROUND,
  321. MF_RESET
  322. };
  323. const char* script_op2name(int op)
  324. {
  325. #define RETURN_OP_NAME(type) case type: return #type
  326. switch( op )
  327. {
  328. RETURN_OP_NAME(C_NOP);
  329. RETURN_OP_NAME(C_POS);
  330. RETURN_OP_NAME(C_INT);
  331. RETURN_OP_NAME(C_PARAM);
  332. RETURN_OP_NAME(C_FUNC);
  333. RETURN_OP_NAME(C_STR);
  334. RETURN_OP_NAME(C_CONSTSTR);
  335. RETURN_OP_NAME(C_ARG);
  336. RETURN_OP_NAME(C_NAME);
  337. RETURN_OP_NAME(C_EOL);
  338. RETURN_OP_NAME(C_RETINFO);
  339. RETURN_OP_NAME(C_USERFUNC);
  340. RETURN_OP_NAME(C_USERFUNC_POS);
  341. // operators
  342. RETURN_OP_NAME(C_OP3);
  343. RETURN_OP_NAME(C_LOR);
  344. RETURN_OP_NAME(C_LAND);
  345. RETURN_OP_NAME(C_LE);
  346. RETURN_OP_NAME(C_LT);
  347. RETURN_OP_NAME(C_GE);
  348. RETURN_OP_NAME(C_GT);
  349. RETURN_OP_NAME(C_EQ);
  350. RETURN_OP_NAME(C_NE);
  351. RETURN_OP_NAME(C_XOR);
  352. RETURN_OP_NAME(C_OR);
  353. RETURN_OP_NAME(C_AND);
  354. RETURN_OP_NAME(C_ADD);
  355. RETURN_OP_NAME(C_SUB);
  356. RETURN_OP_NAME(C_MUL);
  357. RETURN_OP_NAME(C_DIV);
  358. RETURN_OP_NAME(C_MOD);
  359. RETURN_OP_NAME(C_NEG);
  360. RETURN_OP_NAME(C_LNOT);
  361. RETURN_OP_NAME(C_NOT);
  362. RETURN_OP_NAME(C_R_SHIFT);
  363. RETURN_OP_NAME(C_L_SHIFT);
  364. default:
  365. ShowDebug("script_op2name: unexpected op=%d\n", op);
  366. return "???";
  367. }
  368. #undef RETURN_OP_NAME
  369. }
  370. #ifdef DEBUG_DUMP_STACK
  371. static void script_dump_stack(struct script_state* st)
  372. {
  373. int i;
  374. ShowMessage("\tstart = %d\n", st->start);
  375. ShowMessage("\tend = %d\n", st->end);
  376. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  377. ShowMessage("\tsp = %d\n", st->stack->sp);
  378. for( i = 0; i < st->stack->sp; ++i )
  379. {
  380. struct script_data* data = &st->stack->stack_data[i];
  381. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  382. switch( data->type )
  383. {
  384. case C_INT:
  385. case C_POS:
  386. ShowMessage(" %d\n", data->u.num);
  387. break;
  388. case C_STR:
  389. case C_CONSTSTR:
  390. ShowMessage(" \"%s\"\n", data->u.str);
  391. break;
  392. case C_NAME:
  393. 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));
  394. break;
  395. case C_RETINFO:
  396. {
  397. struct script_retinfo* ri = data->u.ri;
  398. 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);
  399. }
  400. break;
  401. default:
  402. ShowMessage("\n");
  403. break;
  404. }
  405. }
  406. }
  407. #endif
  408. /// Reports on the console the src of a script error.
  409. static void script_reportsrc(struct script_state *st)
  410. {
  411. struct block_list* bl;
  412. if( st->oid == 0 )
  413. return; //Can't report source.
  414. bl = map_id2bl(st->oid);
  415. if( bl == NULL )
  416. return;
  417. switch( bl->type )
  418. {
  419. case BL_NPC:
  420. if( bl->m >= 0 )
  421. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  422. else
  423. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  424. break;
  425. default:
  426. if( bl->m >= 0 )
  427. 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);
  428. else
  429. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  430. break;
  431. }
  432. }
  433. /// Reports on the console information about the script data.
  434. static void script_reportdata(struct script_data* data)
  435. {
  436. if( data == NULL )
  437. return;
  438. switch( data->type )
  439. {
  440. case C_NOP:// no value
  441. ShowDebug("Data: nothing (nil)\n");
  442. break;
  443. case C_INT:// number
  444. ShowDebug("Data: number value=%d\n", data->u.num);
  445. break;
  446. case C_STR:
  447. case C_CONSTSTR:// string
  448. if( data->u.str )
  449. {
  450. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  451. }
  452. else
  453. {
  454. ShowDebug("Data: string value=NULL\n");
  455. }
  456. break;
  457. case C_NAME:// reference
  458. if( reference_tovariable(data) )
  459. {// variable
  460. const char* name = reference_getname(data);
  461. if( not_array_variable(*name) )
  462. ShowDebug("Data: variable name='%s'\n", name);
  463. else
  464. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  465. }
  466. else if( reference_toconstant(data) )
  467. {// constant
  468. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  469. }
  470. else if( reference_toparam(data) )
  471. {// param
  472. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  473. }
  474. else
  475. {// ???
  476. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  477. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  478. }
  479. break;
  480. case C_POS:// label
  481. ShowDebug("Data: label pos=%d\n", data->u.num);
  482. break;
  483. default:
  484. ShowDebug("Data: %s\n", script_op2name(data->type));
  485. break;
  486. }
  487. }
  488. /// Reports on the console information about the current built-in function.
  489. static void script_reportfunc(struct script_state* st)
  490. {
  491. int i, params, id;
  492. struct script_data* data;
  493. if( !script_hasdata(st,0) )
  494. {// no stack
  495. return;
  496. }
  497. data = script_getdata(st,0);
  498. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  499. {// script currently not executing a built-in function or corrupt stack
  500. return;
  501. }
  502. id = reference_getid(data);
  503. params = script_lastdata(st)-1;
  504. if( params > 0 )
  505. {
  506. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  507. for( i = 2; i <= script_lastdata(st); i++ )
  508. {
  509. script_reportdata(script_getdata(st,i));
  510. }
  511. }
  512. else
  513. {
  514. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  515. }
  516. }
  517. /*==========================================
  518. * エラーメッセージ出力
  519. *------------------------------------------*/
  520. static void disp_error_message2(const char *mes,const char *pos,int report)
  521. {
  522. error_msg = aStrdup(mes);
  523. error_pos = pos;
  524. error_report = report;
  525. longjmp( error_jump, 1 );
  526. }
  527. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  528. /// Checks event parameter validity
  529. static void check_event(struct script_state *st, const char *evt)
  530. {
  531. if( evt && evt[0] && !stristr(evt, "::On") )
  532. {
  533. if( npc_event_isspecial(evt) )
  534. {
  535. ; // portable small/large monsters or other attributes
  536. }
  537. else
  538. {
  539. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  540. script_reportsrc(st);
  541. }
  542. }
  543. }
  544. /*==========================================
  545. * Hashes the input string
  546. *------------------------------------------*/
  547. static unsigned int calc_hash(const char* p)
  548. {
  549. unsigned int h;
  550. #if defined(SCRIPT_HASH_DJB2)
  551. h = 5381;
  552. while( *p ) // hash*33 + c
  553. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  554. #elif defined(SCRIPT_HASH_SDBM)
  555. h = 0;
  556. while( *p ) // hash*65599 + c
  557. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  558. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  559. h = 0;
  560. while( *p ){
  561. unsigned int g;
  562. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  563. g = h & 0xF0000000;
  564. if( g )
  565. {
  566. h ^= g >> 24;
  567. h &= ~g;
  568. }
  569. }
  570. #else // athena hash
  571. h = 0;
  572. while( *p )
  573. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  574. #endif
  575. return h % SCRIPT_HASH_SIZE;
  576. }
  577. /*==========================================
  578. * str_data manipulation functions
  579. *------------------------------------------*/
  580. /// Looks up string using the provided id.
  581. const char* get_str(int id)
  582. {
  583. Assert( id >= LABEL_START && id < str_size );
  584. return str_buf+str_data[id].str;
  585. }
  586. /// Returns the uid of the string, or -1.
  587. static int search_str(const char* p)
  588. {
  589. int i;
  590. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  591. if( strcasecmp(get_str(i),p) == 0 )
  592. return i;
  593. return -1;
  594. }
  595. /// Stores a copy of the string and returns its id.
  596. /// If an identical string is already present, returns its id instead.
  597. int add_str(const char* p)
  598. {
  599. int i, h;
  600. int len;
  601. h = calc_hash(p);
  602. if( str_hash[h] == 0 )
  603. {// empty bucket, add new node here
  604. str_hash[h] = str_num;
  605. }
  606. else
  607. {// scan for end of list, or occurence of identical string
  608. for( i = str_hash[h]; ; i = str_data[i].next )
  609. {
  610. if( strcasecmp(get_str(i),p) == 0 )
  611. return i; // string already in list
  612. if( str_data[i].next == 0 )
  613. break; // reached the end
  614. }
  615. // append node to end of list
  616. str_data[i].next = str_num;
  617. }
  618. // grow list if neccessary
  619. if( str_num >= str_data_size )
  620. {
  621. str_data_size += 128;
  622. RECREATE(str_data,struct str_data_struct,str_data_size);
  623. memset(str_data + (str_data_size - 128), '\0', 128);
  624. }
  625. len=(int)strlen(p);
  626. // grow string buffer if neccessary
  627. while( str_pos+len+1 >= str_size )
  628. {
  629. str_size += 256;
  630. RECREATE(str_buf,char,str_size);
  631. memset(str_buf + (str_size - 256), '\0', 256);
  632. }
  633. safestrncpy(str_buf+str_pos, p, len+1);
  634. str_data[str_num].type = C_NOP;
  635. str_data[str_num].str = str_pos;
  636. str_data[str_num].next = 0;
  637. str_data[str_num].func = NULL;
  638. str_data[str_num].backpatch = -1;
  639. str_data[str_num].label = -1;
  640. str_pos += len+1;
  641. return str_num++;
  642. }
  643. /// Appends 1 byte to the script buffer.
  644. static void add_scriptb(int a)
  645. {
  646. if( script_pos+1 >= script_size )
  647. {
  648. script_size += SCRIPT_BLOCK_SIZE;
  649. RECREATE(script_buf,unsigned char,script_size);
  650. }
  651. script_buf[script_pos++] = (uint8)(a);
  652. }
  653. /// Appends a c_op value to the script buffer.
  654. /// The value is variable-length encoded into 8-bit blocks.
  655. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  656. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  657. static void add_scriptc(int a)
  658. {
  659. while( a >= 0x40 )
  660. {
  661. add_scriptb((a&0x3f)|0x40);
  662. a = (a - 0x40) >> 6;
  663. }
  664. add_scriptb(a);
  665. }
  666. /// Appends an integer value to the script buffer.
  667. /// The value is variable-length encoded into 8-bit blocks.
  668. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  669. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  670. static void add_scripti(int a)
  671. {
  672. while( a >= 0x40 )
  673. {
  674. add_scriptb((a&0x3f)|0xc0);
  675. a = (a - 0x40) >> 6;
  676. }
  677. add_scriptb(a|0x80);
  678. }
  679. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  680. ///
  681. /// @param l The id of the str_data entry
  682. // 最大16Mまで
  683. static void add_scriptl(int l)
  684. {
  685. int backpatch = str_data[l].backpatch;
  686. switch(str_data[l].type){
  687. case C_POS:
  688. case C_USERFUNC_POS:
  689. add_scriptc(C_POS);
  690. add_scriptb(str_data[l].label);
  691. add_scriptb(str_data[l].label>>8);
  692. add_scriptb(str_data[l].label>>16);
  693. break;
  694. case C_NOP:
  695. case C_USERFUNC:
  696. // ラベルの可能性があるのでbackpatch用データ埋め込み
  697. add_scriptc(C_NAME);
  698. str_data[l].backpatch = script_pos;
  699. add_scriptb(backpatch);
  700. add_scriptb(backpatch>>8);
  701. add_scriptb(backpatch>>16);
  702. break;
  703. case C_INT:
  704. add_scripti(abs(str_data[l].val));
  705. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  706. add_scriptc(C_NEG);
  707. break;
  708. default: // assume C_NAME
  709. add_scriptc(C_NAME);
  710. add_scriptb(l);
  711. add_scriptb(l>>8);
  712. add_scriptb(l>>16);
  713. break;
  714. }
  715. }
  716. /*==========================================
  717. * ラベルを解決する
  718. *------------------------------------------*/
  719. void set_label(int l,int pos, const char* script_pos)
  720. {
  721. int i,next;
  722. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  723. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  724. disp_error_message("set_label: invalid label name",script_pos);
  725. return;
  726. }
  727. if(str_data[l].label!=-1){
  728. disp_error_message("set_label: dup label ",script_pos);
  729. return;
  730. }
  731. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  732. str_data[l].label=pos;
  733. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  734. next=GETVALUE(script_buf,i);
  735. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  736. SETVALUE(script_buf,i,pos);
  737. i=next;
  738. }
  739. }
  740. /// Skips spaces and/or comments.
  741. const char* skip_space(const char* p)
  742. {
  743. if( p == NULL )
  744. return NULL;
  745. for(;;)
  746. {
  747. while( ISSPACE(*p) )
  748. ++p;
  749. if( *p == '/' && p[1] == '/' )
  750. {// line comment
  751. while(*p && *p!='\n')
  752. ++p;
  753. }
  754. else if( *p == '/' && p[1] == '*' )
  755. {// block comment
  756. p += 2;
  757. for(;;)
  758. {
  759. if( *p == '\0' )
  760. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  761. if( *p == '*' && p[1] == '/' )
  762. {// end of block comment
  763. p += 2;
  764. break;
  765. }
  766. ++p;
  767. }
  768. }
  769. else
  770. break;
  771. }
  772. return p;
  773. }
  774. /// Skips a word.
  775. /// A word consists of undercores and/or alfanumeric characters,
  776. /// and valid variable prefixes/postfixes.
  777. static
  778. const char* skip_word(const char* p)
  779. {
  780. // prefix
  781. switch( *p )
  782. {
  783. case '@':// temporary char variable
  784. ++p; break;
  785. case '#':// account variable
  786. p += ( p[1] == '#' ? 2 : 1 ); break;
  787. case '\'':// instance variable
  788. ++p; break;
  789. case '.':// npc variable
  790. p += ( p[1] == '@' ? 2 : 1 ); break;
  791. case '$':// global variable
  792. p += ( p[1] == '@' ? 2 : 1 ); break;
  793. }
  794. while( ISALNUM(*p) || *p == '_' )
  795. ++p;
  796. // postfix
  797. if( *p == '$' )// string
  798. p++;
  799. return p;
  800. }
  801. /// Adds a word to str_data.
  802. /// @see skip_word
  803. /// @see add_str
  804. static
  805. int add_word(const char* p)
  806. {
  807. char* word;
  808. int len;
  809. int i;
  810. // Check for a word
  811. len = skip_word(p) - p;
  812. if( len == 0 )
  813. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  814. // Duplicate the word
  815. word = (char*)aMalloc(len+1);
  816. memcpy(word, p, len);
  817. word[len] = 0;
  818. // add the word
  819. i = add_str(word);
  820. aFree(word);
  821. return i;
  822. }
  823. /// Parses a function call.
  824. /// The argument list can have parenthesis or not.
  825. /// The number of arguments is checked.
  826. static
  827. const char* parse_callfunc(const char* p, int require_paren)
  828. {
  829. const char* p2;
  830. const char* arg=NULL;
  831. int func;
  832. func = add_word(p);
  833. if( str_data[func].type == C_FUNC ){
  834. // buildin function
  835. add_scriptl(func);
  836. add_scriptc(C_ARG);
  837. arg = buildin_func[str_data[func].val].arg;
  838. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  839. // script defined function
  840. int callsub = search_str("callsub");
  841. add_scriptl(callsub);
  842. add_scriptc(C_ARG);
  843. add_scriptl(func);
  844. arg = buildin_func[str_data[callsub].val].arg;
  845. if( *arg == 0 )
  846. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  847. if( *arg != '*' )
  848. ++arg; // count func as argument
  849. } else
  850. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  851. p = skip_word(p);
  852. p = skip_space(p);
  853. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  854. syntax.curly[syntax.curly_count].count = 0;
  855. if( *p == ';' )
  856. {// <func name> ';'
  857. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  858. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  859. {// <func name> '(' ')'
  860. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  861. p = p2;
  862. /*
  863. } else if( 0 && require_paren && *p != '(' )
  864. {// <func name>
  865. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  866. */
  867. } else
  868. {// <func name> <arg list>
  869. if( require_paren ){
  870. if( *p != '(' )
  871. disp_error_message("need '('",p);
  872. ++p; // skip '('
  873. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  874. } else if( *p == '(' ){
  875. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  876. } else {
  877. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  878. }
  879. ++syntax.curly_count;
  880. while( *arg ) {
  881. p2=parse_subexpr(p,-1);
  882. if( p == p2 )
  883. break; // not an argument
  884. if( *arg != '*' )
  885. ++arg; // next argument
  886. p=skip_space(p2);
  887. if( *arg == 0 || *p != ',' )
  888. break; // no more arguments
  889. ++p; // skip comma
  890. }
  891. --syntax.curly_count;
  892. }
  893. if( *arg && *arg != '?' && *arg != '*' )
  894. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  895. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  896. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  897. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  898. if( *p != ')' )
  899. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  900. ++p;
  901. }
  902. add_scriptc(C_FUNC);
  903. return p;
  904. }
  905. /// Processes end of logical script line.
  906. /// @param first When true, only fix up scheduling data is initialized
  907. /// @param p Script position for error reporting in set_label
  908. static void parse_nextline(bool first, const char* p)
  909. {
  910. if( !first )
  911. {
  912. add_scriptc(C_EOL); // mark end of line for stack cleanup
  913. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  914. }
  915. // initialize data for new '-' label fix up scheduling
  916. str_data[LABEL_NEXTLINE].type = C_NOP;
  917. str_data[LABEL_NEXTLINE].backpatch = -1;
  918. str_data[LABEL_NEXTLINE].label = -1;
  919. }
  920. /*==========================================
  921. * 項の解析
  922. *------------------------------------------*/
  923. const char* parse_simpleexpr(const char *p)
  924. {
  925. int i;
  926. p=skip_space(p);
  927. if(*p==';' || *p==',')
  928. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  929. if(*p=='('){
  930. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  931. ++syntax.curly[i].count;
  932. p=parse_subexpr(p+1,-1);
  933. p=skip_space(p);
  934. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  935. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  936. ){
  937. if( *p == ',' ){
  938. syntax.curly[i].flag = ARGLIST_PAREN;
  939. return p;
  940. } else
  941. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  942. }
  943. if( *p != ')' )
  944. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  945. ++p;
  946. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  947. char *np;
  948. i=strtoul(p,&np,0);
  949. add_scripti(i);
  950. p=np;
  951. } else if(*p=='"'){
  952. add_scriptc(C_STR);
  953. p++;
  954. while( *p && *p != '"' ){
  955. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  956. {
  957. char buf[8];
  958. size_t len = skip_escaped_c(p) - p;
  959. size_t n = sv_unescape_c(buf, p, len);
  960. if( n != 1 )
  961. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  962. p += len;
  963. add_scriptb(*buf);
  964. continue;
  965. }
  966. else if( *p == '\n' )
  967. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  968. add_scriptb(*p++);
  969. }
  970. if(!*p)
  971. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  972. add_scriptb(0);
  973. p++; //'"'
  974. } else {
  975. int l;
  976. // label , register , function etc
  977. if(skip_word(p)==p)
  978. disp_error_message("parse_simpleexpr: unexpected character",p);
  979. l=add_word(p);
  980. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  981. return parse_callfunc(p,1);
  982. p=skip_word(p);
  983. if( *p == '[' ){
  984. // array(name[i] => getelementofarray(name,i) )
  985. add_scriptl(search_str("getelementofarray"));
  986. add_scriptc(C_ARG);
  987. add_scriptl(l);
  988. p=parse_subexpr(p+1,-1);
  989. p=skip_space(p);
  990. if( *p != ']' )
  991. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  992. ++p;
  993. add_scriptc(C_FUNC);
  994. }else
  995. add_scriptl(l);
  996. }
  997. return p;
  998. }
  999. /*==========================================
  1000. * 式の解析
  1001. *------------------------------------------*/
  1002. const char* parse_subexpr(const char* p,int limit)
  1003. {
  1004. int op,opl,len;
  1005. const char* tmpp;
  1006. p=skip_space(p);
  1007. if(*p=='-'){
  1008. tmpp=skip_space(p+1);
  1009. if(*tmpp==';' || *tmpp==','){
  1010. add_scriptl(LABEL_NEXTLINE);
  1011. p++;
  1012. return p;
  1013. }
  1014. }
  1015. tmpp=p;
  1016. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1017. p=parse_subexpr(p+1,10);
  1018. add_scriptc(op);
  1019. } else
  1020. p=parse_simpleexpr(p);
  1021. p=skip_space(p);
  1022. while((
  1023. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1024. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1025. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1026. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1027. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1028. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1029. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1030. (op=C_AND,opl=6,len=1,*p=='&') ||
  1031. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1032. (op=C_OR,opl=5,len=1,*p=='|') ||
  1033. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1034. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1035. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1036. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1037. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1038. (op=C_GT,opl=3,len=1,*p=='>') ||
  1039. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1040. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1041. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1042. p+=len;
  1043. if(op == C_OP3) {
  1044. p=parse_subexpr(p,-1);
  1045. p=skip_space(p);
  1046. if( *(p++) != ':')
  1047. disp_error_message("parse_subexpr: need ':'", p-1);
  1048. p=parse_subexpr(p,-1);
  1049. } else {
  1050. p=parse_subexpr(p,opl);
  1051. }
  1052. add_scriptc(op);
  1053. p=skip_space(p);
  1054. }
  1055. return p; /* return first untreated operator */
  1056. }
  1057. /*==========================================
  1058. * 式の評価
  1059. *------------------------------------------*/
  1060. const char* parse_expr(const char *p)
  1061. {
  1062. switch(*p){
  1063. case ')': case ';': case ':': case '[': case ']':
  1064. case '}':
  1065. disp_error_message("parse_expr: unexpected char",p);
  1066. }
  1067. p=parse_subexpr(p,-1);
  1068. return p;
  1069. }
  1070. /*==========================================
  1071. * 行の解析
  1072. *------------------------------------------*/
  1073. const char* parse_line(const char* p)
  1074. {
  1075. const char* p2;
  1076. p=skip_space(p);
  1077. if(*p==';') {
  1078. // if(); for(); while(); のために閉じ判定
  1079. p = parse_syntax_close(p + 1);
  1080. return p;
  1081. }
  1082. if(*p==')' && parse_syntax_for_flag)
  1083. return p+1;
  1084. p = skip_space(p);
  1085. if(p[0] == '{') {
  1086. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1087. syntax.curly[syntax.curly_count].count = -1;
  1088. syntax.curly[syntax.curly_count].index = -1;
  1089. syntax.curly_count++;
  1090. return p + 1;
  1091. } else if(p[0] == '}') {
  1092. return parse_curly_close(p);
  1093. }
  1094. // 構文関連の処理
  1095. p2 = parse_syntax(p);
  1096. if(p2 != NULL)
  1097. return p2;
  1098. p = parse_callfunc(p,0);
  1099. p = skip_space(p);
  1100. if(parse_syntax_for_flag) {
  1101. if( *p != ')' )
  1102. disp_error_message("parse_line: need ')'",p);
  1103. } else {
  1104. if( *p != ';' )
  1105. disp_error_message("parse_line: need ';'",p);
  1106. }
  1107. // if, for , while の閉じ判定
  1108. p = parse_syntax_close(p+1);
  1109. return p;
  1110. }
  1111. // { ... } の閉じ処理
  1112. const char* parse_curly_close(const char* p)
  1113. {
  1114. if(syntax.curly_count <= 0) {
  1115. disp_error_message("parse_curly_close: unexpected string",p);
  1116. return p + 1;
  1117. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1118. syntax.curly_count--;
  1119. // if, for , while の閉じ判定
  1120. p = parse_syntax_close(p + 1);
  1121. return p;
  1122. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1123. // switch() 閉じ判定
  1124. int pos = syntax.curly_count-1;
  1125. char label[256];
  1126. int l;
  1127. // 一時変数を消す
  1128. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1129. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1130. parse_line(label);
  1131. syntax.curly_count--;
  1132. // 無条件で終了ポインタに移動
  1133. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1134. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1135. parse_line(label);
  1136. syntax.curly_count--;
  1137. // 現在地のラベルを付ける
  1138. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1139. l=add_str(label);
  1140. set_label(l,script_pos, p);
  1141. if(syntax.curly[pos].flag) {
  1142. // default が存在する
  1143. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1144. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1145. parse_line(label);
  1146. syntax.curly_count--;
  1147. }
  1148. // 終了ラベルを付ける
  1149. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1150. l=add_str(label);
  1151. set_label(l,script_pos, p);
  1152. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1153. syntax.curly_count--;
  1154. // if, for , while の閉じ判定
  1155. p = parse_syntax_close(p + 1);
  1156. return p;
  1157. } else {
  1158. disp_error_message("parse_curly_close: unexpected string",p);
  1159. return p + 1;
  1160. }
  1161. }
  1162. // 構文関連の処理
  1163. // break, case, continue, default, do, for, function,
  1164. // if, switch, while をこの内部で処理します。
  1165. const char* parse_syntax(const char* p)
  1166. {
  1167. const char *p2 = skip_word(p);
  1168. switch(*p) {
  1169. case 'B':
  1170. case 'b':
  1171. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1172. // break の処理
  1173. char label[256];
  1174. int pos = syntax.curly_count - 1;
  1175. while(pos >= 0) {
  1176. if(syntax.curly[pos].type == TYPE_DO) {
  1177. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1178. break;
  1179. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1180. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1181. break;
  1182. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1183. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1184. break;
  1185. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1186. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1187. break;
  1188. }
  1189. pos--;
  1190. }
  1191. if(pos < 0) {
  1192. disp_error_message("parse_syntax: unexpected 'break'",p);
  1193. } else {
  1194. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1195. parse_line(label);
  1196. syntax.curly_count--;
  1197. }
  1198. p = skip_space(p2);
  1199. if(*p != ';')
  1200. disp_error_message("parse_syntax: need ';'",p);
  1201. // if, for , while の閉じ判定
  1202. p = parse_syntax_close(p + 1);
  1203. return p;
  1204. }
  1205. break;
  1206. case 'c':
  1207. case 'C':
  1208. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1209. // case の処理
  1210. int pos = syntax.curly_count-1;
  1211. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1212. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1213. return p+1;
  1214. } else {
  1215. char label[256];
  1216. int l,v;
  1217. char *np;
  1218. if(syntax.curly[pos].count != 1) {
  1219. // FALLTHRU 用のジャンプ
  1220. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1221. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1222. parse_line(label);
  1223. syntax.curly_count--;
  1224. // 現在地のラベルを付ける
  1225. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1226. l=add_str(label);
  1227. set_label(l,script_pos, p);
  1228. }
  1229. // switch 判定文
  1230. p = skip_space(p2);
  1231. if(p == p2) {
  1232. disp_error_message("parse_syntax: expect space ' '",p);
  1233. }
  1234. // check whether case label is integer or not
  1235. v = strtol(p,&np,0);
  1236. if(np == p) { //Check for constants
  1237. p2 = skip_word(p);
  1238. v = p2-p; // length of word at p2
  1239. memcpy(label,p,v);
  1240. label[v]='\0';
  1241. if( !script_get_constant(label, &v) )
  1242. disp_error_message("parse_syntax: 'case' label not integer",p);
  1243. p = skip_word(p);
  1244. } else { //Numeric value
  1245. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1246. p++;
  1247. p = skip_word(p);
  1248. if(np != p)
  1249. disp_error_message("parse_syntax: 'case' label not integer",np);
  1250. }
  1251. p = skip_space(p);
  1252. if(*p != ':')
  1253. disp_error_message("parse_syntax: expect ':'",p);
  1254. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1255. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1256. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1257. // 2回parse しないとダメ
  1258. p2 = parse_line(label);
  1259. parse_line(p2);
  1260. syntax.curly_count--;
  1261. if(syntax.curly[pos].count != 1) {
  1262. // FALLTHRU 終了後のラベル
  1263. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1264. l=add_str(label);
  1265. set_label(l,script_pos,p);
  1266. }
  1267. // check duplication of case label [Rayce]
  1268. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1269. disp_error_message("parse_syntax: dup 'case'",p);
  1270. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1271. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1272. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1273. parse_line(label);
  1274. syntax.curly_count--;
  1275. syntax.curly[pos].count++;
  1276. }
  1277. return p + 1;
  1278. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1279. // continue の処理
  1280. char label[256];
  1281. int pos = syntax.curly_count - 1;
  1282. while(pos >= 0) {
  1283. if(syntax.curly[pos].type == TYPE_DO) {
  1284. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1285. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1286. break;
  1287. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1288. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1289. break;
  1290. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1291. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1292. break;
  1293. }
  1294. pos--;
  1295. }
  1296. if(pos < 0) {
  1297. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1298. } else {
  1299. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1300. parse_line(label);
  1301. syntax.curly_count--;
  1302. }
  1303. p = skip_space(p2);
  1304. if(*p != ';')
  1305. disp_error_message("parse_syntax: need ';'",p);
  1306. // if, for , while の閉じ判定
  1307. p = parse_syntax_close(p + 1);
  1308. return p;
  1309. }
  1310. break;
  1311. case 'd':
  1312. case 'D':
  1313. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1314. // switch - default の処理
  1315. int pos = syntax.curly_count-1;
  1316. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1317. disp_error_message("parse_syntax: unexpected 'default'",p);
  1318. } else if(syntax.curly[pos].flag) {
  1319. disp_error_message("parse_syntax: dup 'default'",p);
  1320. } else {
  1321. char label[256];
  1322. int l;
  1323. // 現在地のラベルを付ける
  1324. p = skip_space(p2);
  1325. if(*p != ':') {
  1326. disp_error_message("parse_syntax: need ':'",p);
  1327. }
  1328. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1329. l=add_str(label);
  1330. set_label(l,script_pos,p);
  1331. // 無条件で次のリンクに飛ばす
  1332. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1333. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1334. parse_line(label);
  1335. syntax.curly_count--;
  1336. // default のラベルを付ける
  1337. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1338. l=add_str(label);
  1339. set_label(l,script_pos,p);
  1340. syntax.curly[syntax.curly_count - 1].flag = 1;
  1341. syntax.curly[pos].count++;
  1342. }
  1343. return p + 1;
  1344. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1345. int l;
  1346. char label[256];
  1347. p=skip_space(p2);
  1348. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1349. syntax.curly[syntax.curly_count].count = 1;
  1350. syntax.curly[syntax.curly_count].index = syntax.index++;
  1351. syntax.curly[syntax.curly_count].flag = 0;
  1352. // 現在地のラベル形成する
  1353. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1354. l=add_str(label);
  1355. set_label(l,script_pos,p);
  1356. syntax.curly_count++;
  1357. return p;
  1358. }
  1359. break;
  1360. case 'f':
  1361. case 'F':
  1362. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1363. int l;
  1364. char label[256];
  1365. int pos = syntax.curly_count;
  1366. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1367. syntax.curly[syntax.curly_count].count = 1;
  1368. syntax.curly[syntax.curly_count].index = syntax.index++;
  1369. syntax.curly[syntax.curly_count].flag = 0;
  1370. syntax.curly_count++;
  1371. p=skip_space(p2);
  1372. if(*p != '(')
  1373. disp_error_message("parse_syntax: need '('",p);
  1374. p++;
  1375. // 初期化文を実行する
  1376. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1377. p=parse_line(p);
  1378. syntax.curly_count--;
  1379. // 条件判断開始のラベル形成する
  1380. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1381. l=add_str(label);
  1382. set_label(l,script_pos,p);
  1383. p=skip_space(p);
  1384. if(*p == ';') {
  1385. // for(;;) のパターンなので必ず真
  1386. ;
  1387. } else {
  1388. // 条件が偽なら終了地点に飛ばす
  1389. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1390. add_scriptl(add_str("jump_zero"));
  1391. add_scriptc(C_ARG);
  1392. p=parse_expr(p);
  1393. p=skip_space(p);
  1394. add_scriptl(add_str(label));
  1395. add_scriptc(C_FUNC);
  1396. }
  1397. if(*p != ';')
  1398. disp_error_message("parse_syntax: need ';'",p);
  1399. p++;
  1400. // ループ開始に飛ばす
  1401. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1402. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1403. parse_line(label);
  1404. syntax.curly_count--;
  1405. // 次のループへのラベル形成する
  1406. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1407. l=add_str(label);
  1408. set_label(l,script_pos,p);
  1409. // 次のループに入る時の処理
  1410. // for 最後の ')' を ';' として扱うフラグ
  1411. parse_syntax_for_flag = 1;
  1412. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1413. p=parse_line(p);
  1414. syntax.curly_count--;
  1415. parse_syntax_for_flag = 0;
  1416. // 条件判定処理に飛ばす
  1417. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1418. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1419. parse_line(label);
  1420. syntax.curly_count--;
  1421. // ループ開始のラベル付け
  1422. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1423. l=add_str(label);
  1424. set_label(l,script_pos,p);
  1425. return p;
  1426. }
  1427. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1428. {// internal script function
  1429. const char *func_name;
  1430. func_name = skip_space(p2);
  1431. p = skip_word(func_name);
  1432. if( p == func_name )
  1433. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1434. p2 = skip_space(p);
  1435. if( *p2 == ';' )
  1436. {// function <name> ;
  1437. // function declaration - just register the name
  1438. int l;
  1439. l = add_word(func_name);
  1440. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1441. str_data[l].type = C_USERFUNC;
  1442. else if( str_data[l].type == C_USERFUNC )
  1443. ; // already registered
  1444. else
  1445. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1446. // if, for , while の閉じ判定
  1447. p = parse_syntax_close(p2 + 1);
  1448. return p;
  1449. }
  1450. else if(*p2 == '{')
  1451. {// function <name> <line/block of code>
  1452. char label[256];
  1453. int l;
  1454. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1455. syntax.curly[syntax.curly_count].count = 1;
  1456. syntax.curly[syntax.curly_count].index = syntax.index++;
  1457. syntax.curly[syntax.curly_count].flag = 0;
  1458. ++syntax.curly_count;
  1459. // Jump over the function code
  1460. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1461. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1462. ++syntax.curly_count;
  1463. parse_line(label);
  1464. --syntax.curly_count;
  1465. // Set the position of the function (label)
  1466. l=add_word(func_name);
  1467. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1468. {
  1469. str_data[l].type = C_USERFUNC;
  1470. set_label(l, script_pos, p);
  1471. if( parse_options&SCRIPT_USE_LABEL_DB )
  1472. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1473. }
  1474. else
  1475. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1476. return skip_space(p);
  1477. }
  1478. else
  1479. {
  1480. disp_error_message("expect ';' or '{' at function syntax",p);
  1481. }
  1482. }
  1483. break;
  1484. case 'i':
  1485. case 'I':
  1486. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1487. // if() の処理
  1488. char label[256];
  1489. p=skip_space(p2);
  1490. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1491. disp_error_message("need '('",p);
  1492. }
  1493. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1494. syntax.curly[syntax.curly_count].count = 1;
  1495. syntax.curly[syntax.curly_count].index = syntax.index++;
  1496. syntax.curly[syntax.curly_count].flag = 0;
  1497. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1498. syntax.curly_count++;
  1499. add_scriptl(add_str("jump_zero"));
  1500. add_scriptc(C_ARG);
  1501. p=parse_expr(p);
  1502. p=skip_space(p);
  1503. add_scriptl(add_str(label));
  1504. add_scriptc(C_FUNC);
  1505. return p;
  1506. }
  1507. break;
  1508. case 's':
  1509. case 'S':
  1510. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1511. // switch() の処理
  1512. char label[256];
  1513. p=skip_space(p2);
  1514. if(*p != '(') {
  1515. disp_error_message("need '('",p);
  1516. }
  1517. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1518. syntax.curly[syntax.curly_count].count = 1;
  1519. syntax.curly[syntax.curly_count].index = syntax.index++;
  1520. syntax.curly[syntax.curly_count].flag = 0;
  1521. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1522. syntax.curly_count++;
  1523. add_scriptl(add_str("set"));
  1524. add_scriptc(C_ARG);
  1525. add_scriptl(add_str(label));
  1526. p=parse_expr(p);
  1527. p=skip_space(p);
  1528. if(*p != '{') {
  1529. disp_error_message("parse_syntax: need '{'",p);
  1530. }
  1531. add_scriptc(C_FUNC);
  1532. return p + 1;
  1533. }
  1534. break;
  1535. case 'w':
  1536. case 'W':
  1537. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1538. int l;
  1539. char label[256];
  1540. p=skip_space(p2);
  1541. if(*p != '(') {
  1542. disp_error_message("need '('",p);
  1543. }
  1544. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1545. syntax.curly[syntax.curly_count].count = 1;
  1546. syntax.curly[syntax.curly_count].index = syntax.index++;
  1547. syntax.curly[syntax.curly_count].flag = 0;
  1548. // 条件判断開始のラベル形成する
  1549. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1550. l=add_str(label);
  1551. set_label(l,script_pos,p);
  1552. // 条件が偽なら終了地点に飛ばす
  1553. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1554. syntax.curly_count++;
  1555. add_scriptl(add_str("jump_zero"));
  1556. add_scriptc(C_ARG);
  1557. p=parse_expr(p);
  1558. p=skip_space(p);
  1559. add_scriptl(add_str(label));
  1560. add_scriptc(C_FUNC);
  1561. return p;
  1562. }
  1563. break;
  1564. }
  1565. return NULL;
  1566. }
  1567. const char* parse_syntax_close(const char *p) {
  1568. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1569. int flag;
  1570. do {
  1571. p = parse_syntax_close_sub(p,&flag);
  1572. } while(flag);
  1573. return p;
  1574. }
  1575. // if, for , while , do の閉じ判定
  1576. // flag == 1 : 閉じられた
  1577. // flag == 0 : 閉じられない
  1578. const char* parse_syntax_close_sub(const char* p,int* flag)
  1579. {
  1580. char label[256];
  1581. int pos = syntax.curly_count - 1;
  1582. int l;
  1583. *flag = 1;
  1584. if(syntax.curly_count <= 0) {
  1585. *flag = 0;
  1586. return p;
  1587. } else if(syntax.curly[pos].type == TYPE_IF) {
  1588. const char *bp = p;
  1589. const char *p2;
  1590. // if-block and else-block end is a new line
  1591. parse_nextline(false, p);
  1592. // if 最終場所へ飛ばす
  1593. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1594. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1595. parse_line(label);
  1596. syntax.curly_count--;
  1597. // 現在地のラベルを付ける
  1598. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1599. l=add_str(label);
  1600. set_label(l,script_pos,p);
  1601. syntax.curly[pos].count++;
  1602. p = skip_space(p);
  1603. p2 = skip_word(p);
  1604. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1605. // else or else - if
  1606. p = skip_space(p2);
  1607. p2 = skip_word(p);
  1608. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1609. // else - if
  1610. p=skip_space(p2);
  1611. if(*p != '(') {
  1612. disp_error_message("need '('",p);
  1613. }
  1614. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1615. add_scriptl(add_str("jump_zero"));
  1616. add_scriptc(C_ARG);
  1617. p=parse_expr(p);
  1618. p=skip_space(p);
  1619. add_scriptl(add_str(label));
  1620. add_scriptc(C_FUNC);
  1621. *flag = 0;
  1622. return p;
  1623. } else {
  1624. // else
  1625. if(!syntax.curly[pos].flag) {
  1626. syntax.curly[pos].flag = 1;
  1627. *flag = 0;
  1628. return p;
  1629. }
  1630. }
  1631. }
  1632. // if 閉じ
  1633. syntax.curly_count--;
  1634. // 最終地のラベルを付ける
  1635. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1636. l=add_str(label);
  1637. set_label(l,script_pos,p);
  1638. if(syntax.curly[pos].flag == 1) {
  1639. // このifに対するelseじゃないのでポインタの位置は同じ
  1640. return bp;
  1641. }
  1642. return p;
  1643. } else if(syntax.curly[pos].type == TYPE_DO) {
  1644. int l;
  1645. char label[256];
  1646. const char *p2;
  1647. if(syntax.curly[pos].flag) {
  1648. // 現在地のラベル形成する(continue でここに来る)
  1649. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1650. l=add_str(label);
  1651. set_label(l,script_pos,p);
  1652. }
  1653. // 条件が偽なら終了地点に飛ばす
  1654. p = skip_space(p);
  1655. p2 = skip_word(p);
  1656. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1657. disp_error_message("parse_syntax: need 'while'",p);
  1658. p = skip_space(p2);
  1659. if(*p != '(') {
  1660. disp_error_message("need '('",p);
  1661. }
  1662. // do-block end is a new line
  1663. parse_nextline(false, p);
  1664. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1665. add_scriptl(add_str("jump_zero"));
  1666. add_scriptc(C_ARG);
  1667. p=parse_expr(p);
  1668. p=skip_space(p);
  1669. add_scriptl(add_str(label));
  1670. add_scriptc(C_FUNC);
  1671. // 開始地点に飛ばす
  1672. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1673. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1674. parse_line(label);
  1675. syntax.curly_count--;
  1676. // 条件終了地点のラベル形成する
  1677. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1678. l=add_str(label);
  1679. set_label(l,script_pos,p);
  1680. p = skip_space(p);
  1681. if(*p != ';') {
  1682. disp_error_message("parse_syntax: need ';'",p);
  1683. return p+1;
  1684. }
  1685. p++;
  1686. syntax.curly_count--;
  1687. return p;
  1688. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1689. // for-block end is a new line
  1690. parse_nextline(false, p);
  1691. // 次のループに飛ばす
  1692. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1693. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1694. parse_line(label);
  1695. syntax.curly_count--;
  1696. // for 終了のラベル付け
  1697. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1698. l=add_str(label);
  1699. set_label(l,script_pos,p);
  1700. syntax.curly_count--;
  1701. return p;
  1702. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1703. // while-block end is a new line
  1704. parse_nextline(false, p);
  1705. // while 条件判断へ飛ばす
  1706. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1707. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1708. parse_line(label);
  1709. syntax.curly_count--;
  1710. // while 終了のラベル付け
  1711. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1712. l=add_str(label);
  1713. set_label(l,script_pos,p);
  1714. syntax.curly_count--;
  1715. return p;
  1716. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1717. int pos = syntax.curly_count-1;
  1718. char label[256];
  1719. int l;
  1720. // 戻す
  1721. sprintf(label,"return;");
  1722. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1723. parse_line(label);
  1724. syntax.curly_count--;
  1725. // 現在地のラベルを付ける
  1726. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1727. l=add_str(label);
  1728. set_label(l,script_pos,p);
  1729. syntax.curly_count--;
  1730. return p;
  1731. } else {
  1732. *flag = 0;
  1733. return p;
  1734. }
  1735. }
  1736. /*==========================================
  1737. * 組み込み関数の追加
  1738. *------------------------------------------*/
  1739. static void add_buildin_func(void)
  1740. {
  1741. int i,n;
  1742. const char* p;
  1743. for( i = 0; buildin_func[i].func; i++ )
  1744. {
  1745. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1746. // 'v' - value (either string or int or reference)
  1747. // 's' - string
  1748. // 'i' - int
  1749. // 'r' - reference (of a variable)
  1750. // 'l' - label
  1751. // '?' - one optional parameter
  1752. // '*' - unknown number of optional parameters
  1753. p = buildin_func[i].arg;
  1754. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1755. while( *p == '?' ) ++p;
  1756. if( *p == '*' ) ++p;
  1757. if( *p != 0){
  1758. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1759. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1760. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1761. } else {
  1762. n = add_str(buildin_func[i].name);
  1763. str_data[n].type = C_FUNC;
  1764. str_data[n].val = i;
  1765. str_data[n].func = buildin_func[i].func;
  1766. }
  1767. }
  1768. }
  1769. /// Retrieves the value of a constant.
  1770. bool script_get_constant(const char* name, int* value)
  1771. {
  1772. int n = search_str(name);
  1773. if( n == -1 || str_data[n].type != C_INT )
  1774. {// not found or not a constant
  1775. return false;
  1776. }
  1777. value[0] = str_data[n].val;
  1778. return true;
  1779. }
  1780. /// Creates new constant or parameter with given value.
  1781. void script_set_constant(const char* name, int value, bool isparameter)
  1782. {
  1783. int n = add_str(name);
  1784. if( str_data[n].type == C_NOP )
  1785. {// new
  1786. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1787. str_data[n].val = value;
  1788. }
  1789. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1790. {// existing parameter or constant
  1791. 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);
  1792. }
  1793. else
  1794. {// existing name
  1795. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1796. }
  1797. }
  1798. /*==========================================
  1799. * 定数データベースの読み込み
  1800. *------------------------------------------*/
  1801. static void read_constdb(void)
  1802. {
  1803. FILE *fp;
  1804. char line[1024],name[1024],val[1024];
  1805. int type;
  1806. sprintf(line, "%s/const.txt", db_path);
  1807. fp=fopen(line, "r");
  1808. if(fp==NULL){
  1809. ShowError("can't read %s\n", line);
  1810. return ;
  1811. }
  1812. while(fgets(line, sizeof(line), fp))
  1813. {
  1814. if(line[0]=='/' && line[1]=='/')
  1815. continue;
  1816. type=0;
  1817. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1818. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1819. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  1820. }
  1821. }
  1822. fclose(fp);
  1823. }
  1824. /*==========================================
  1825. * エラー表示
  1826. *------------------------------------------*/
  1827. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1828. {
  1829. int i;
  1830. if( p == NULL || !p[0] ) return NULL;
  1831. if( line < 0 )
  1832. StringBuf_Printf(buf, "*% 5d : ", -line);
  1833. else
  1834. StringBuf_Printf(buf, " % 5d : ", line);
  1835. for(i=0;p[i] && p[i] != '\n';i++){
  1836. if(p + i != mark)
  1837. StringBuf_Printf(buf, "%c", p[i]);
  1838. else
  1839. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1840. }
  1841. StringBuf_AppendStr(buf, "\n");
  1842. return p+i+(p[i] == '\n' ? 1 : 0);
  1843. }
  1844. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1845. {
  1846. // エラーが発生した行を求める
  1847. int j;
  1848. int line = start_line;
  1849. const char *p;
  1850. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1851. StringBuf buf;
  1852. for(p=src;p && *p;line++){
  1853. const char *lineend=strchr(p,'\n');
  1854. if(lineend==NULL || error_pos<lineend){
  1855. break;
  1856. }
  1857. for( j = 0; j < 4; j++ ) {
  1858. linestart[j] = linestart[j+1];
  1859. }
  1860. linestart[4] = p;
  1861. p=lineend+1;
  1862. }
  1863. StringBuf_Init(&buf);
  1864. StringBuf_AppendStr(&buf, "\a\n");
  1865. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1866. StringBuf_Printf(&buf, " %s\n", error_msg);
  1867. for(j = 0; j < 5; j++ ) {
  1868. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1869. }
  1870. p = script_print_line(&buf, p, error_pos, -line);
  1871. for(j = 0; j < 5; j++) {
  1872. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1873. }
  1874. ShowError("%s", StringBuf_Value(&buf));
  1875. StringBuf_Destroy(&buf);
  1876. }
  1877. /*==========================================
  1878. * スクリプトの解析
  1879. *------------------------------------------*/
  1880. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1881. {
  1882. const char *p,*tmpp;
  1883. int i;
  1884. struct script_code* code = NULL;
  1885. static int first=1;
  1886. char end;
  1887. bool unresolved_names = false;
  1888. if( src == NULL )
  1889. return NULL;// empty script
  1890. memset(&syntax,0,sizeof(syntax));
  1891. if(first){
  1892. add_buildin_func();
  1893. read_constdb();
  1894. first=0;
  1895. }
  1896. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1897. script_pos=0;
  1898. script_size=SCRIPT_BLOCK_SIZE;
  1899. parse_nextline(true, NULL);
  1900. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1901. if( options&SCRIPT_USE_LABEL_DB )
  1902. scriptlabel_db->clear(scriptlabel_db, NULL);
  1903. parse_options = options;
  1904. if( setjmp( error_jump ) != 0 ) {
  1905. //Restore program state when script has problems. [from jA]
  1906. int i;
  1907. const int size = ARRAYLENGTH(syntax.curly);
  1908. if( error_report )
  1909. script_error(src,file,line,error_msg,error_pos);
  1910. aFree( error_msg );
  1911. aFree( script_buf );
  1912. script_pos = 0;
  1913. script_size = 0;
  1914. script_buf = NULL;
  1915. for(i=LABEL_START;i<str_num;i++)
  1916. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1917. for(i=0; i<size; i++)
  1918. linkdb_final(&syntax.curly[i].case_label);
  1919. return NULL;
  1920. }
  1921. parse_syntax_for_flag=0;
  1922. p=src;
  1923. p=skip_space(p);
  1924. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1925. {// does not require brackets around the script
  1926. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1927. {// empty script and can return NULL
  1928. aFree( script_buf );
  1929. script_pos = 0;
  1930. script_size = 0;
  1931. script_buf = NULL;
  1932. return NULL;
  1933. }
  1934. end = '\0';
  1935. }
  1936. else
  1937. {// requires brackets around the script
  1938. if( *p != '{' )
  1939. disp_error_message("not found '{'",p);
  1940. p = skip_space(p+1);
  1941. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1942. {// empty script and can return NULL
  1943. aFree( script_buf );
  1944. script_pos = 0;
  1945. script_size = 0;
  1946. script_buf = NULL;
  1947. return NULL;
  1948. }
  1949. end = '}';
  1950. }
  1951. // clear references of labels, variables and internal functions
  1952. for(i=LABEL_START;i<str_num;i++){
  1953. if(
  1954. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1955. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1956. ){
  1957. str_data[i].type=C_NOP;
  1958. str_data[i].backpatch=-1;
  1959. str_data[i].label=-1;
  1960. }
  1961. }
  1962. while( syntax.curly_count != 0 || *p != end )
  1963. {
  1964. if( *p == '\0' )
  1965. disp_error_message("unexpected end of script",p);
  1966. // labelだけ特殊処理
  1967. tmpp=skip_space(skip_word(p));
  1968. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1969. i=add_word(p);
  1970. set_label(i,script_pos,p);
  1971. if( parse_options&SCRIPT_USE_LABEL_DB )
  1972. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1973. p=tmpp+1;
  1974. p=skip_space(p);
  1975. continue;
  1976. }
  1977. // 他は全部一緒くた
  1978. p=parse_line(p);
  1979. p=skip_space(p);
  1980. parse_nextline(false, p);
  1981. }
  1982. add_scriptc(C_NOP);
  1983. // trim code to size
  1984. script_size = script_pos;
  1985. RECREATE(script_buf,unsigned char,script_pos);
  1986. // default unknown references to variables
  1987. for(i=LABEL_START;i<str_num;i++){
  1988. if(str_data[i].type==C_NOP){
  1989. int j,next;
  1990. str_data[i].type=C_NAME;
  1991. str_data[i].label=i;
  1992. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1993. next=GETVALUE(script_buf,j);
  1994. SETVALUE(script_buf,j,i);
  1995. j=next;
  1996. }
  1997. }
  1998. else if( str_data[i].type == C_USERFUNC )
  1999. {// 'function name;' without follow-up code
  2000. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2001. unresolved_names = true;
  2002. }
  2003. }
  2004. if( unresolved_names )
  2005. {
  2006. disp_error_message("parse_script: unresolved function references", p);
  2007. }
  2008. #ifdef DEBUG_DISP
  2009. for(i=0;i<script_pos;i++){
  2010. if((i&15)==0) ShowMessage("%04x : ",i);
  2011. ShowMessage("%02x ",script_buf[i]);
  2012. if((i&15)==15) ShowMessage("\n");
  2013. }
  2014. ShowMessage("\n");
  2015. #endif
  2016. #ifdef DEBUG_DISASM
  2017. {
  2018. int i = 0,j;
  2019. while(i < script_pos) {
  2020. c_op op = get_com(script_buf,&i);
  2021. ShowMessage("%06x %s", i, script_op2name(op));
  2022. j = i;
  2023. switch(op) {
  2024. case C_INT:
  2025. ShowMessage(" %d", get_num(script_buf,&i));
  2026. break;
  2027. case C_POS:
  2028. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2029. i += 3;
  2030. break;
  2031. case C_NAME:
  2032. j = (*(int*)(script_buf+i)&0xffffff);
  2033. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2034. i += 3;
  2035. break;
  2036. case C_STR:
  2037. j = strlen(script_buf + i);
  2038. ShowMessage(" %s", script_buf + i);
  2039. i += j+1;
  2040. break;
  2041. }
  2042. ShowMessage(CL_CLL"\n");
  2043. }
  2044. }
  2045. #endif
  2046. CREATE(code,struct script_code,1);
  2047. code->script_buf = script_buf;
  2048. code->script_size = script_size;
  2049. code->script_vars = NULL;
  2050. return code;
  2051. }
  2052. /// Returns the player attached to this script, identified by the rid.
  2053. /// If there is no player attached, the script is terminated.
  2054. TBL_PC *script_rid2sd(struct script_state *st)
  2055. {
  2056. TBL_PC *sd=map_id2sd(st->rid);
  2057. if(!sd){
  2058. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2059. script_reportfunc(st);
  2060. script_reportsrc(st);
  2061. st->state = END;
  2062. }
  2063. return sd;
  2064. }
  2065. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2066. ///
  2067. /// @param st Script state
  2068. /// @param data Variable/constant
  2069. void get_val(struct script_state* st, struct script_data* data)
  2070. {
  2071. const char* name;
  2072. char prefix;
  2073. char postfix;
  2074. TBL_PC* sd = NULL;
  2075. if( !data_isreference(data) )
  2076. return;// not a variable/constant
  2077. name = reference_getname(data);
  2078. prefix = name[0];
  2079. postfix = name[strlen(name) - 1];
  2080. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2081. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2082. {
  2083. sd = script_rid2sd(st);
  2084. if( sd == NULL )
  2085. {// needs player attached
  2086. if( postfix == '$' )
  2087. {// string variable
  2088. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2089. data->type = C_CONSTSTR;
  2090. data->u.str = "";
  2091. }
  2092. else
  2093. {// integer variable
  2094. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2095. data->type = C_INT;
  2096. data->u.num = 0;
  2097. }
  2098. return;
  2099. }
  2100. }
  2101. if( postfix == '$' )
  2102. {// string variable
  2103. switch( prefix )
  2104. {
  2105. case '@':
  2106. data->u.str = pc_readregstr(sd, data->u.num);
  2107. break;
  2108. case '$':
  2109. data->u.str = mapreg_readregstr(data->u.num);
  2110. break;
  2111. case '#':
  2112. if( name[1] == '#' )
  2113. data->u.str = pc_readaccountreg2str(sd, name);// global
  2114. else
  2115. data->u.str = pc_readaccountregstr(sd, name);// local
  2116. break;
  2117. case '.':
  2118. {
  2119. struct linkdb_node** n =
  2120. data->ref ? data->ref:
  2121. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2122. &st->script->script_vars;// npc variable
  2123. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2124. }
  2125. break;
  2126. case '\'':
  2127. {
  2128. struct linkdb_node** n = NULL;
  2129. if( st->instance_id )
  2130. n = &instance[st->instance_id].svar;
  2131. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2132. }
  2133. break;
  2134. default:
  2135. data->u.str = pc_readglobalreg_str(sd, name);
  2136. break;
  2137. }
  2138. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2139. {// empty string
  2140. data->type = C_CONSTSTR;
  2141. data->u.str = "";
  2142. }
  2143. else
  2144. {// duplicate string
  2145. data->type = C_STR;
  2146. data->u.str = aStrdup(data->u.str);
  2147. }
  2148. }
  2149. else
  2150. {// integer variable
  2151. data->type = C_INT;
  2152. if( reference_toconstant(data) )
  2153. {
  2154. data->u.num = reference_getconstant(data);
  2155. }
  2156. else if( reference_toparam(data) )
  2157. {
  2158. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2159. }
  2160. else
  2161. switch( prefix )
  2162. {
  2163. case '@':
  2164. data->u.num = pc_readreg(sd, data->u.num);
  2165. break;
  2166. case '$':
  2167. data->u.num = mapreg_readreg(data->u.num);
  2168. break;
  2169. case '#':
  2170. if( name[1] == '#' )
  2171. data->u.num = pc_readaccountreg2(sd, name);// global
  2172. else
  2173. data->u.num = pc_readaccountreg(sd, name);// local
  2174. break;
  2175. case '.':
  2176. {
  2177. struct linkdb_node** n =
  2178. data->ref ? data->ref:
  2179. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2180. &st->script->script_vars;// npc variable
  2181. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2182. }
  2183. break;
  2184. case '\'':
  2185. {
  2186. struct linkdb_node** n = NULL;
  2187. if( st->instance_id )
  2188. n = &instance[st->instance_id].ivar;
  2189. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2190. }
  2191. break;
  2192. default:
  2193. data->u.num = pc_readglobalreg(sd, name);
  2194. break;
  2195. }
  2196. }
  2197. return;
  2198. }
  2199. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2200. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2201. /// The value is left in the top of the stack and needs to be removed manually.
  2202. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2203. {
  2204. struct script_data* data;
  2205. push_val2(st->stack, C_NAME, uid, ref);
  2206. data = script_getdatatop(st, -1);
  2207. get_val(st, data);
  2208. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2209. }
  2210. /*==========================================
  2211. * Stores the value of a script variable
  2212. * Return value is 0 on fail, 1 on success.
  2213. *------------------------------------------*/
  2214. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2215. {
  2216. char prefix = name[0];
  2217. if( is_string_variable(name) )
  2218. {// string variable
  2219. const char* str = (const char*)value;
  2220. switch (prefix) {
  2221. case '@':
  2222. return pc_setregstr(sd, num, str);
  2223. case '$':
  2224. return mapreg_setregstr(num, str);
  2225. case '#':
  2226. return (name[1] == '#') ?
  2227. pc_setaccountreg2str(sd, name, str) :
  2228. pc_setaccountregstr(sd, name, str);
  2229. case '.': {
  2230. char* p;
  2231. struct linkdb_node** n;
  2232. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2233. p = (char*)linkdb_erase(n, (void*)num);
  2234. if (p) aFree(p);
  2235. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2236. }
  2237. return 1;
  2238. case '\'': {
  2239. char *p;
  2240. struct linkdb_node** n = NULL;
  2241. if( st->instance_id )
  2242. n = &instance[st->instance_id].svar;
  2243. p = (char*)linkdb_erase(n, (void*)num);
  2244. if (p) aFree(p);
  2245. if( str[0] ) linkdb_insert(n, (void*)num, aStrdup(str));
  2246. }
  2247. return 1;
  2248. default:
  2249. return pc_setglobalreg_str(sd, name, str);
  2250. }
  2251. }
  2252. else
  2253. {// integer variable
  2254. int val = (int)value;
  2255. if(str_data[num&0x00ffffff].type == C_PARAM)
  2256. {
  2257. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2258. {
  2259. if( st != NULL )
  2260. {
  2261. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2262. script_reportsrc(st);
  2263. st->state = END;
  2264. }
  2265. return 0;
  2266. }
  2267. return 1;
  2268. }
  2269. switch (prefix) {
  2270. case '@':
  2271. return pc_setreg(sd, num, val);
  2272. case '$':
  2273. return mapreg_setreg(num, val);
  2274. case '#':
  2275. return (name[1] == '#') ?
  2276. pc_setaccountreg2(sd, name, val) :
  2277. pc_setaccountreg(sd, name, val);
  2278. case '.': {
  2279. struct linkdb_node** n;
  2280. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2281. if (val == 0)
  2282. linkdb_erase(n, (void*)num);
  2283. else
  2284. linkdb_replace(n, (void*)num, (void*)val);
  2285. }
  2286. return 1;
  2287. case '\'':
  2288. {
  2289. struct linkdb_node** n = NULL;
  2290. if( st->instance_id )
  2291. n = &instance[st->instance_id].ivar;
  2292. if( val == 0 )
  2293. linkdb_erase(n, (void*)num);
  2294. else
  2295. linkdb_replace(n, (void*)num, (void*)val);
  2296. return 1;
  2297. }
  2298. default:
  2299. return pc_setglobalreg(sd, name, val);
  2300. }
  2301. }
  2302. }
  2303. int set_var(TBL_PC* sd, char* name, void* val)
  2304. {
  2305. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2306. }
  2307. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2308. {
  2309. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2310. }
  2311. /// Converts the data to a string
  2312. const char* conv_str(struct script_state* st, struct script_data* data)
  2313. {
  2314. char* p;
  2315. get_val(st, data);
  2316. if( data_isstring(data) )
  2317. {// nothing to convert
  2318. }
  2319. else if( data_isint(data) )
  2320. {// int -> string
  2321. CREATE(p, char, ITEM_NAME_LENGTH);
  2322. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2323. p[ITEM_NAME_LENGTH-1] = '\0';
  2324. data->type = C_STR;
  2325. data->u.str = p;
  2326. }
  2327. else if( data_isreference(data) )
  2328. {// reference -> string
  2329. //##TODO when does this happen (check get_val) [FlavioJS]
  2330. data->type = C_CONSTSTR;
  2331. data->u.str = reference_getname(data);
  2332. }
  2333. else
  2334. {// unsupported data type
  2335. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2336. script_reportdata(data);
  2337. script_reportsrc(st);
  2338. data->type = C_CONSTSTR;
  2339. data->u.str = "";
  2340. }
  2341. return data->u.str;
  2342. }
  2343. /// Converts the data to an int
  2344. int conv_num(struct script_state* st, struct script_data* data)
  2345. {
  2346. char* p;
  2347. long num;
  2348. get_val(st, data);
  2349. if( data_isint(data) )
  2350. {// nothing to convert
  2351. }
  2352. else if( data_isstring(data) )
  2353. {// string -> int
  2354. // the result does not overflow or underflow, it is capped instead
  2355. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2356. p = data->u.str;
  2357. errno = 0;
  2358. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2359. if( errno == ERANGE
  2360. #if LONG_MAX > INT_MAX
  2361. || num < INT_MIN || num > INT_MAX
  2362. #endif
  2363. )
  2364. {
  2365. if( num <= INT_MIN )
  2366. {
  2367. num = INT_MIN;
  2368. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2369. }
  2370. else//if( num >= INT_MAX )
  2371. {
  2372. num = INT_MAX;
  2373. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2374. }
  2375. script_reportdata(data);
  2376. script_reportsrc(st);
  2377. }
  2378. if( data->type == C_STR )
  2379. aFree(p);
  2380. data->type = C_INT;
  2381. data->u.num = (int)num;
  2382. }
  2383. #if 0
  2384. // FIXME this function is being used to retrieve the position of labels and
  2385. // probably other stuff [FlavioJS]
  2386. else
  2387. {// unsupported data type
  2388. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2389. script_reportdata(data);
  2390. script_reportsrc(st);
  2391. data->type = C_INT;
  2392. data->u.num = 0;
  2393. }
  2394. #endif
  2395. return data->u.num;
  2396. }
  2397. //
  2398. // Stack operations
  2399. //
  2400. /// Increases the size of the stack
  2401. void stack_expand(struct script_stack* stack)
  2402. {
  2403. stack->sp_max += 64;
  2404. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2405. stack->sp_max * sizeof(stack->stack_data[0]) );
  2406. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2407. 64 * sizeof(stack->stack_data[0]) );
  2408. }
  2409. /// Pushes a value into the stack
  2410. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2411. /// Pushes a value into the stack (with reference)
  2412. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2413. {
  2414. if( stack->sp >= stack->sp_max )
  2415. stack_expand(stack);
  2416. stack->stack_data[stack->sp].type = type;
  2417. stack->stack_data[stack->sp].u.num = val;
  2418. stack->stack_data[stack->sp].ref = ref;
  2419. stack->sp++;
  2420. return &stack->stack_data[stack->sp-1];
  2421. }
  2422. /// Pushes a string into the stack
  2423. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2424. {
  2425. if( stack->sp >= stack->sp_max )
  2426. stack_expand(stack);
  2427. stack->stack_data[stack->sp].type = type;
  2428. stack->stack_data[stack->sp].u.str = str;
  2429. stack->stack_data[stack->sp].ref = NULL;
  2430. stack->sp++;
  2431. return &stack->stack_data[stack->sp-1];
  2432. }
  2433. /// Pushes a retinfo into the stack
  2434. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2435. {
  2436. if( stack->sp >= stack->sp_max )
  2437. stack_expand(stack);
  2438. stack->stack_data[stack->sp].type = C_RETINFO;
  2439. stack->stack_data[stack->sp].u.ri = ri;
  2440. stack->stack_data[stack->sp].ref = NULL;
  2441. stack->sp++;
  2442. return &stack->stack_data[stack->sp-1];
  2443. }
  2444. /// Pushes a copy of the target position into the stack
  2445. struct script_data* push_copy(struct script_stack* stack, int pos)
  2446. {
  2447. switch( stack->stack_data[pos].type )
  2448. {
  2449. case C_CONSTSTR:
  2450. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2451. break;
  2452. case C_STR:
  2453. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2454. break;
  2455. case C_RETINFO:
  2456. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2457. exit(1);
  2458. break;
  2459. default:
  2460. return push_val2(
  2461. stack,stack->stack_data[pos].type,
  2462. stack->stack_data[pos].u.num,
  2463. stack->stack_data[pos].ref
  2464. );
  2465. break;
  2466. }
  2467. }
  2468. /// Removes the values in indexes [start,end[ from the stack.
  2469. /// Adjusts all stack pointers.
  2470. void pop_stack(struct script_state* st, int start, int end)
  2471. {
  2472. struct script_stack* stack = st->stack;
  2473. struct script_data* data;
  2474. int i;
  2475. if( start < 0 )
  2476. start = 0;
  2477. if( end > stack->sp )
  2478. end = stack->sp;
  2479. if( start >= end )
  2480. return;// nothing to pop
  2481. // free stack elements
  2482. for( i = start; i < end; i++ )
  2483. {
  2484. data = &stack->stack_data[i];
  2485. if( data->type == C_STR )
  2486. aFree(data->u.str);
  2487. if( data->type == C_RETINFO )
  2488. {
  2489. struct script_retinfo* ri = data->u.ri;
  2490. if( ri->var_function )
  2491. {
  2492. script_free_vars(ri->var_function);
  2493. aFree(ri->var_function);
  2494. }
  2495. aFree(ri);
  2496. }
  2497. data->type = C_NOP;
  2498. }
  2499. // move the rest of the elements
  2500. if( stack->sp > end )
  2501. {
  2502. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2503. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2504. stack->stack_data[i].type = C_NOP;
  2505. }
  2506. // adjust stack pointers
  2507. if( st->start > end ) st->start -= end - start;
  2508. else if( st->start > start ) st->start = start;
  2509. if( st->end > end ) st->end -= end - start;
  2510. else if( st->end > start ) st->end = start;
  2511. if( stack->defsp > end ) stack->defsp -= end - start;
  2512. else if( stack->defsp > start ) stack->defsp = start;
  2513. stack->sp -= end - start;
  2514. }
  2515. ///
  2516. ///
  2517. ///
  2518. /*==========================================
  2519. * スクリプト依存変数、関数依存変数の解放
  2520. *------------------------------------------*/
  2521. void script_free_vars(struct linkdb_node **node)
  2522. {
  2523. struct linkdb_node* n = *node;
  2524. while( n != NULL)
  2525. {
  2526. const char* name = get_str((int)(n->key)&0x00ffffff);
  2527. if( is_string_variable(name) )
  2528. aFree(n->data); // 文字型変数なので、データ削除
  2529. n = n->next;
  2530. }
  2531. linkdb_final( node );
  2532. }
  2533. void script_free_code(struct script_code* code)
  2534. {
  2535. script_free_vars( &code->script_vars );
  2536. aFree( code->script_buf );
  2537. aFree( code );
  2538. }
  2539. /// Creates a new script state.
  2540. ///
  2541. /// @param script Script code
  2542. /// @param pos Position in the code
  2543. /// @param rid Who is running the script (attached player)
  2544. /// @param oid Where the code is being run (npc 'object')
  2545. /// @return Script state
  2546. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2547. {
  2548. struct script_state* st;
  2549. CREATE(st, struct script_state, 1);
  2550. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2551. st->stack->sp = 0;
  2552. st->stack->sp_max = 64;
  2553. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2554. st->stack->defsp = st->stack->sp;
  2555. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2556. st->state = RUN;
  2557. st->script = script;
  2558. //st->scriptroot = script;
  2559. st->pos = pos;
  2560. st->rid = rid;
  2561. st->oid = oid;
  2562. st->sleep.timer = INVALID_TIMER;
  2563. return st;
  2564. }
  2565. /// Frees a script state.
  2566. ///
  2567. /// @param st Script state
  2568. void script_free_state(struct script_state* st)
  2569. {
  2570. if(st->bk_st)
  2571. {// backup was not restored
  2572. 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);
  2573. }
  2574. if( st->sleep.timer != INVALID_TIMER )
  2575. delete_timer(st->sleep.timer, run_script_timer);
  2576. script_free_vars(st->stack->var_function);
  2577. aFree(st->stack->var_function);
  2578. pop_stack(st, 0, st->stack->sp);
  2579. aFree(st->stack->stack_data);
  2580. aFree(st->stack);
  2581. st->pos = -1;
  2582. aFree(st);
  2583. }
  2584. //
  2585. // 実行部main
  2586. //
  2587. /*==========================================
  2588. * コマンドの読み取り
  2589. *------------------------------------------*/
  2590. c_op get_com(unsigned char *script,int *pos)
  2591. {
  2592. int i = 0, j = 0;
  2593. if(script[*pos]>=0x80){
  2594. return C_INT;
  2595. }
  2596. while(script[*pos]>=0x40){
  2597. i=script[(*pos)++]<<j;
  2598. j+=6;
  2599. }
  2600. return (c_op)(i+(script[(*pos)++]<<j));
  2601. }
  2602. /*==========================================
  2603. * 数値の所得
  2604. *------------------------------------------*/
  2605. int get_num(unsigned char *script,int *pos)
  2606. {
  2607. int i,j;
  2608. i=0; j=0;
  2609. while(script[*pos]>=0xc0){
  2610. i+=(script[(*pos)++]&0x7f)<<j;
  2611. j+=6;
  2612. }
  2613. return i+((script[(*pos)++]&0x7f)<<j);
  2614. }
  2615. /*==========================================
  2616. * スタックから値を取り出す
  2617. *------------------------------------------*/
  2618. int pop_val(struct script_state* st)
  2619. {
  2620. if(st->stack->sp<=0)
  2621. return 0;
  2622. st->stack->sp--;
  2623. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2624. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2625. return st->stack->stack_data[st->stack->sp].u.num;
  2626. return 0;
  2627. }
  2628. /// Ternary operators
  2629. /// test ? if_true : if_false
  2630. void op_3(struct script_state* st, int op)
  2631. {
  2632. struct script_data* data;
  2633. int flag = 0;
  2634. data = script_getdatatop(st, -3);
  2635. get_val(st, data);
  2636. if( data_isstring(data) )
  2637. flag = data->u.str[0];// "" -> false
  2638. else if( data_isint(data) )
  2639. flag = data->u.num;// 0 -> false
  2640. else
  2641. {
  2642. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2643. script_reportdata(data);
  2644. script_reportsrc(st);
  2645. script_removetop(st, -3, 0);
  2646. script_pushnil(st);
  2647. return;
  2648. }
  2649. if( flag )
  2650. script_pushcopytop(st, -2);
  2651. else
  2652. script_pushcopytop(st, -1);
  2653. script_removetop(st, -4, -1);
  2654. }
  2655. /// Binary string operators
  2656. /// s1 EQ s2 -> i
  2657. /// s1 NE s2 -> i
  2658. /// s1 GT s2 -> i
  2659. /// s1 GE s2 -> i
  2660. /// s1 LT s2 -> i
  2661. /// s1 LE s2 -> i
  2662. /// s1 ADD s2 -> s
  2663. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2664. {
  2665. int a = 0;
  2666. switch(op){
  2667. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2668. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2669. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2670. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2671. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2672. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2673. case C_ADD:
  2674. {
  2675. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2676. strcpy(buf, s1);
  2677. strcat(buf, s2);
  2678. script_pushstr(st, buf);
  2679. return;
  2680. }
  2681. default:
  2682. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2683. script_reportsrc(st);
  2684. script_pushnil(st);
  2685. st->state = END;
  2686. return;
  2687. }
  2688. script_pushint(st,a);
  2689. }
  2690. /// Binary number operators
  2691. /// i OP i -> i
  2692. void op_2num(struct script_state* st, int op, int i1, int i2)
  2693. {
  2694. int ret;
  2695. double ret_double;
  2696. switch( op )
  2697. {
  2698. case C_AND: ret = i1 & i2; break;
  2699. case C_OR: ret = i1 | i2; break;
  2700. case C_XOR: ret = i1 ^ i2; break;
  2701. case C_LAND: ret = (i1 && i2); break;
  2702. case C_LOR: ret = (i1 || i2); break;
  2703. case C_EQ: ret = (i1 == i2); break;
  2704. case C_NE: ret = (i1 != i2); break;
  2705. case C_GT: ret = (i1 > i2); break;
  2706. case C_GE: ret = (i1 >= i2); break;
  2707. case C_LT: ret = (i1 < i2); break;
  2708. case C_LE: ret = (i1 <= i2); break;
  2709. case C_R_SHIFT: ret = i1>>i2; break;
  2710. case C_L_SHIFT: ret = i1<<i2; break;
  2711. case C_DIV:
  2712. case C_MOD:
  2713. if( i2 == 0 )
  2714. {
  2715. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2716. script_reportsrc(st);
  2717. script_pushnil(st);
  2718. st->state = END;
  2719. return;
  2720. }
  2721. else if( op == C_DIV )
  2722. ret = i1 / i2;
  2723. else//if( op == C_MOD )
  2724. ret = i1 % i2;
  2725. break;
  2726. default:
  2727. switch( op )
  2728. {// operators that can overflow/underflow
  2729. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2730. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2731. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2732. default:
  2733. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2734. script_reportsrc(st);
  2735. script_pushnil(st);
  2736. return;
  2737. }
  2738. if( ret_double < (double)INT_MIN )
  2739. {
  2740. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2741. script_reportsrc(st);
  2742. ret = INT_MIN;
  2743. }
  2744. else if( ret_double > (double)INT_MAX )
  2745. {
  2746. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2747. script_reportsrc(st);
  2748. ret = INT_MAX;
  2749. }
  2750. }
  2751. script_pushint(st, ret);
  2752. }
  2753. /// Binary operators
  2754. void op_2(struct script_state *st, int op)
  2755. {
  2756. struct script_data* left;
  2757. struct script_data* right;
  2758. left = script_getdatatop(st, -2);
  2759. right = script_getdatatop(st, -1);
  2760. get_val(st, left);
  2761. get_val(st, right);
  2762. // automatic conversions
  2763. switch( op )
  2764. {
  2765. case C_ADD:
  2766. if( data_isint(left) && data_isstring(right) )
  2767. {// convert int-string to string-string
  2768. conv_str(st, left);
  2769. }
  2770. else if( data_isstring(left) && data_isint(right) )
  2771. {// convert string-int to string-string
  2772. conv_str(st, right);
  2773. }
  2774. break;
  2775. }
  2776. if( data_isstring(left) && data_isstring(right) )
  2777. {// ss => op_2str
  2778. op_2str(st, op, left->u.str, right->u.str);
  2779. script_removetop(st, -3, -1);// pop the two values before the top one
  2780. }
  2781. else if( data_isint(left) && data_isint(right) )
  2782. {// ii => op_2num
  2783. int i1 = left->u.num;
  2784. int i2 = right->u.num;
  2785. script_removetop(st, -2, 0);
  2786. op_2num(st, op, i1, i2);
  2787. }
  2788. else
  2789. {// invalid argument
  2790. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2791. script_reportdata(left);
  2792. script_reportdata(right);
  2793. script_reportsrc(st);
  2794. script_removetop(st, -2, 0);
  2795. script_pushnil(st);
  2796. st->state = END;
  2797. }
  2798. }
  2799. /// Unary operators
  2800. /// NEG i -> i
  2801. /// NOT i -> i
  2802. /// LNOT i -> i
  2803. void op_1(struct script_state* st, int op)
  2804. {
  2805. struct script_data* data;
  2806. int i1;
  2807. data = script_getdatatop(st, -1);
  2808. get_val(st, data);
  2809. if( !data_isint(data) )
  2810. {// not a number
  2811. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2812. script_reportdata(data);
  2813. script_reportsrc(st);
  2814. script_pushnil(st);
  2815. st->state = END;
  2816. return;
  2817. }
  2818. i1 = data->u.num;
  2819. script_removetop(st, -1, 0);
  2820. switch( op )
  2821. {
  2822. case C_NEG: i1 = -i1; break;
  2823. case C_NOT: i1 = ~i1; break;
  2824. case C_LNOT: i1 = !i1; break;
  2825. default:
  2826. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2827. script_reportsrc(st);
  2828. script_pushnil(st);
  2829. st->state = END;
  2830. return;
  2831. }
  2832. script_pushint(st, i1);
  2833. }
  2834. /// Checks the type of all arguments passed to a built-in function.
  2835. ///
  2836. /// @param st Script state whose stack arguments should be inspected.
  2837. /// @param func Built-in function for which the arguments are intended.
  2838. static void script_check_buildin_argtype(struct script_state* st, int func)
  2839. {
  2840. char type;
  2841. int idx, invalid = 0;
  2842. script_function* sf = &buildin_func[str_data[func].val];
  2843. for( idx = 2; script_hasdata(st, idx); idx++ )
  2844. {
  2845. struct script_data* data = script_getdata(st, idx);
  2846. type = sf->arg[idx-2];
  2847. if( type == '?' || type == '*' )
  2848. {// optional argument or unknown number of optional parameters ( no types are after this )
  2849. break;
  2850. }
  2851. else if( type == 0 )
  2852. {// more arguments than necessary ( should not happen, as it is checked before )
  2853. ShowWarning("Found more arguments than necessary.\n");
  2854. invalid++;
  2855. break;
  2856. }
  2857. else
  2858. {
  2859. const char* name = NULL;
  2860. if( data_isreference(data) )
  2861. {// get name for variables to determine the type they refer to
  2862. name = reference_getname(data);
  2863. }
  2864. switch( type )
  2865. {
  2866. case 'v':
  2867. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  2868. {// variant
  2869. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  2870. script_reportdata(data);
  2871. invalid++;
  2872. }
  2873. break;
  2874. case 's':
  2875. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  2876. {// string
  2877. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  2878. script_reportdata(data);
  2879. invalid++;
  2880. }
  2881. break;
  2882. case 'i':
  2883. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  2884. {// int ( params and constants are always int )
  2885. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  2886. script_reportdata(data);
  2887. invalid++;
  2888. }
  2889. break;
  2890. case 'r':
  2891. if( !data_isreference(data) )
  2892. {// variables
  2893. ShowWarning("Unexpected type for argument %d. Expected variable.\n", idx-1);
  2894. script_reportdata(data);
  2895. invalid++;
  2896. }
  2897. break;
  2898. case 'l':
  2899. if( !data_islabel(data) && !data_isfunclabel(data) )
  2900. {// label
  2901. ShowWarning("Unexpected type for argument %d. Expected label.\n", idx-1);
  2902. script_reportdata(data);
  2903. invalid++;
  2904. }
  2905. break;
  2906. }
  2907. }
  2908. }
  2909. if(invalid)
  2910. {
  2911. ShowDebug("Function: %s\n", get_str(func));
  2912. script_reportsrc(st);
  2913. }
  2914. }
  2915. /// Executes a buildin command.
  2916. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2917. int run_func(struct script_state *st)
  2918. {
  2919. struct script_data* data;
  2920. int i,start_sp,end_sp,func;
  2921. end_sp = st->stack->sp;// position after the last argument
  2922. for( i = end_sp-1; i > 0 ; --i )
  2923. if( st->stack->stack_data[i].type == C_ARG )
  2924. break;
  2925. if( i == 0 )
  2926. {
  2927. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2928. st->state = END;
  2929. script_reportsrc(st);
  2930. return 1;
  2931. }
  2932. start_sp = i-1;// C_NAME of the command
  2933. st->start = start_sp;
  2934. st->end = end_sp;
  2935. data = &st->stack->stack_data[st->start];
  2936. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2937. func = data->u.num;
  2938. else
  2939. {
  2940. ShowError("script:run_func: not a buildin command.\n");
  2941. script_reportdata(data);
  2942. script_reportsrc(st);
  2943. st->state = END;
  2944. return 1;
  2945. }
  2946. if( script_config.warn_func_mismatch_argtypes )
  2947. {
  2948. script_check_buildin_argtype(st, func);
  2949. }
  2950. if(str_data[func].func){
  2951. if (str_data[func].func(st)) //Report error
  2952. script_reportsrc(st);
  2953. } else {
  2954. 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));
  2955. script_reportsrc(st);
  2956. st->state = END;
  2957. }
  2958. // Stack's datum are used when re-running functions [Eoe]
  2959. if( st->state == RERUNLINE )
  2960. return 0;
  2961. pop_stack(st, st->start, st->end);
  2962. if( st->state == RETFUNC )
  2963. {// return from a user-defined function
  2964. struct script_retinfo* ri;
  2965. int olddefsp = st->stack->defsp;
  2966. int nargs;
  2967. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2968. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2969. {
  2970. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  2971. script_reportsrc(st);
  2972. st->state = END;
  2973. return 1;
  2974. }
  2975. script_free_vars( st->stack->var_function );
  2976. aFree(st->stack->var_function);
  2977. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  2978. nargs = ri->nargs;
  2979. st->pos = ri->pos;
  2980. st->script = ri->script;
  2981. st->stack->var_function = ri->var_function;
  2982. st->stack->defsp = ri->defsp;
  2983. memset(ri, 0, sizeof(struct script_retinfo));
  2984. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  2985. st->state = GOTO;
  2986. }
  2987. return 0;
  2988. }
  2989. /*==========================================
  2990. * script execution
  2991. *------------------------------------------*/
  2992. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2993. {
  2994. struct script_state *st;
  2995. if( rootscript == NULL || pos < 0 )
  2996. return;
  2997. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  2998. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  2999. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3000. st = script_alloc_state(rootscript, pos, rid, oid);
  3001. run_script_main(st);
  3002. }
  3003. void script_stop_sleeptimers(int id)
  3004. {
  3005. struct script_state* st;
  3006. for(;;)
  3007. {
  3008. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  3009. if( st == NULL )
  3010. break; // no more sleep timers
  3011. script_free_state(st);
  3012. }
  3013. }
  3014. /*==========================================
  3015. * 指定ノードをsleep_dbから削除
  3016. *------------------------------------------*/
  3017. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3018. {
  3019. struct linkdb_node *retnode;
  3020. if( n == NULL)
  3021. return NULL;
  3022. if( n->prev == NULL )
  3023. sleep_db = n->next;
  3024. else
  3025. n->prev->next = n->next;
  3026. if( n->next )
  3027. n->next->prev = n->prev;
  3028. retnode = n->next;
  3029. aFree( n );
  3030. return retnode; // 次のノードを返す
  3031. }
  3032. /*==========================================
  3033. * sleep用タイマー関数
  3034. *------------------------------------------*/
  3035. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3036. {
  3037. struct script_state *st = (struct script_state *)data;
  3038. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3039. TBL_PC *sd = map_id2sd(st->rid);
  3040. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3041. { //Character mismatch. Cancel execution.
  3042. st->rid = 0;
  3043. st->state = END;
  3044. }
  3045. while( node && st->sleep.timer != INVALID_TIMER ) {
  3046. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3047. script_erase_sleepdb(node);
  3048. st->sleep.timer = INVALID_TIMER;
  3049. break;
  3050. }
  3051. node = node->next;
  3052. }
  3053. if(st->state != RERUNLINE)
  3054. st->sleep.tick = 0;
  3055. run_script_main(st);
  3056. return 0;
  3057. }
  3058. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3059. ///
  3060. /// @param st Script state to detach.
  3061. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3062. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3063. {
  3064. struct map_session_data* sd;
  3065. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3066. {
  3067. sd->st = st->bk_st;
  3068. sd->npc_id = st->bk_npcid;
  3069. /**
  3070. * For the Secure NPC Timeout option (check RRConfig/Secure.h) [RR]
  3071. **/
  3072. #if SECURE_NPCTIMEOUT
  3073. /**
  3074. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3075. **/
  3076. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3077. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3078. sd->npc_idle_timer = INVALID_TIMER;
  3079. }
  3080. #endif
  3081. if(st->bk_st)
  3082. {
  3083. //Remove tag for removal.
  3084. st->bk_st = NULL;
  3085. st->bk_npcid = 0;
  3086. }
  3087. else if(dequeue_event)
  3088. {
  3089. npc_event_dequeue(sd);
  3090. }
  3091. }
  3092. else if(st->bk_st)
  3093. {// rid was set to 0, before detaching the script state
  3094. 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);
  3095. script_reportsrc(st->bk_st);
  3096. script_free_state(st->bk_st);
  3097. st->bk_st = NULL;
  3098. }
  3099. }
  3100. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3101. ///
  3102. /// @param st Script state to attach.
  3103. static void script_attach_state(struct script_state* st)
  3104. {
  3105. struct map_session_data* sd;
  3106. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3107. {
  3108. if(st!=sd->st)
  3109. {
  3110. if(st->bk_st)
  3111. {// there is already a backup
  3112. 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);
  3113. }
  3114. st->bk_st = sd->st;
  3115. st->bk_npcid = sd->npc_id;
  3116. }
  3117. sd->st = st;
  3118. sd->npc_id = st->oid;
  3119. /**
  3120. * For the Secure NPC Timeout option (check RRConfig/Secure.h) [RR]
  3121. **/
  3122. #if SECURE_NPCTIMEOUT
  3123. if( sd->npc_idle_timer == INVALID_TIMER )
  3124. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3125. sd->npc_idle_tick = gettick();
  3126. #endif
  3127. }
  3128. }
  3129. /*==========================================
  3130. * スクリプトの実行メイン部分
  3131. *------------------------------------------*/
  3132. void run_script_main(struct script_state *st)
  3133. {
  3134. int cmdcount=script_config.check_cmdcount;
  3135. int gotocount=script_config.check_gotocount;
  3136. TBL_PC *sd;
  3137. struct script_stack *stack=st->stack;
  3138. struct npc_data *nd;
  3139. script_attach_state(st);
  3140. nd = map_id2nd(st->oid);
  3141. if( nd && map[nd->bl.m].instance_id > 0 )
  3142. st->instance_id = map[nd->bl.m].instance_id;
  3143. if(st->state == RERUNLINE) {
  3144. run_func(st);
  3145. if(st->state == GOTO)
  3146. st->state = RUN;
  3147. } else if(st->state != END)
  3148. st->state = RUN;
  3149. while(st->state == RUN)
  3150. {
  3151. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3152. switch(c){
  3153. case C_EOL:
  3154. if( stack->defsp > stack->sp )
  3155. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3156. else
  3157. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3158. break;
  3159. case C_INT:
  3160. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3161. break;
  3162. case C_POS:
  3163. case C_NAME:
  3164. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3165. st->pos+=3;
  3166. break;
  3167. case C_ARG:
  3168. push_val(stack,c,0);
  3169. break;
  3170. case C_STR:
  3171. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3172. while(st->script->script_buf[st->pos++]);
  3173. break;
  3174. case C_FUNC:
  3175. run_func(st);
  3176. if(st->state==GOTO){
  3177. st->state = RUN;
  3178. if( gotocount>0 && (--gotocount)<=0 ){
  3179. ShowError("run_script: infinity loop !\n");
  3180. script_reportsrc(st);
  3181. st->state=END;
  3182. }
  3183. }
  3184. break;
  3185. case C_NEG:
  3186. case C_NOT:
  3187. case C_LNOT:
  3188. op_1(st ,c);
  3189. break;
  3190. case C_ADD:
  3191. case C_SUB:
  3192. case C_MUL:
  3193. case C_DIV:
  3194. case C_MOD:
  3195. case C_EQ:
  3196. case C_NE:
  3197. case C_GT:
  3198. case C_GE:
  3199. case C_LT:
  3200. case C_LE:
  3201. case C_AND:
  3202. case C_OR:
  3203. case C_XOR:
  3204. case C_LAND:
  3205. case C_LOR:
  3206. case C_R_SHIFT:
  3207. case C_L_SHIFT:
  3208. op_2(st, c);
  3209. break;
  3210. case C_OP3:
  3211. op_3(st, c);
  3212. break;
  3213. case C_NOP:
  3214. st->state=END;
  3215. break;
  3216. default:
  3217. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3218. st->state=END;
  3219. break;
  3220. }
  3221. if( cmdcount>0 && (--cmdcount)<=0 ){
  3222. ShowError("run_script: infinity loop !\n");
  3223. script_reportsrc(st);
  3224. st->state=END;
  3225. }
  3226. }
  3227. if(st->sleep.tick > 0) {
  3228. //Restore previous script
  3229. script_detach_state(st, false);
  3230. //Delay execution
  3231. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3232. st->sleep.charid = sd?sd->status.char_id:0;
  3233. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3234. run_script_timer, st->sleep.charid, (intptr_t)st);
  3235. linkdb_insert(&sleep_db, (void*)st->oid, st);
  3236. }
  3237. else if(st->state != END && st->rid){
  3238. //Resume later (st is already attached to player).
  3239. if(st->bk_st) {
  3240. ShowWarning("Unable to restore stack! Double continuation!\n");
  3241. //Report BOTH scripts to see if that can help somehow.
  3242. ShowDebug("Previous script (lost):\n");
  3243. script_reportsrc(st->bk_st);
  3244. ShowDebug("Current script:\n");
  3245. script_reportsrc(st);
  3246. script_free_state(st->bk_st);
  3247. st->bk_st = NULL;
  3248. }
  3249. } else {
  3250. //Dispose of script.
  3251. if ((sd = map_id2sd(st->rid))!=NULL)
  3252. { //Restore previous stack and save char.
  3253. if(sd->state.using_fake_npc){
  3254. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3255. sd->state.using_fake_npc = 0;
  3256. }
  3257. //Restore previous script if any.
  3258. script_detach_state(st, true);
  3259. if (sd->state.reg_dirty&2)
  3260. intif_saveregistry(sd,2);
  3261. if (sd->state.reg_dirty&1)
  3262. intif_saveregistry(sd,1);
  3263. }
  3264. script_free_state(st);
  3265. st = NULL;
  3266. }
  3267. }
  3268. int script_config_read(char *cfgName)
  3269. {
  3270. int i;
  3271. char line[1024],w1[1024],w2[1024];
  3272. FILE *fp;
  3273. fp=fopen(cfgName,"r");
  3274. if(fp==NULL){
  3275. ShowError("file not found: [%s]\n", cfgName);
  3276. return 1;
  3277. }
  3278. while(fgets(line, sizeof(line), fp))
  3279. {
  3280. if(line[0] == '/' && line[1] == '/')
  3281. continue;
  3282. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3283. if(i!=2)
  3284. continue;
  3285. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3286. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3287. }
  3288. else if(strcmpi(w1,"check_cmdcount")==0) {
  3289. script_config.check_cmdcount = config_switch(w2);
  3290. }
  3291. else if(strcmpi(w1,"check_gotocount")==0) {
  3292. script_config.check_gotocount = config_switch(w2);
  3293. }
  3294. else if(strcmpi(w1,"input_min_value")==0) {
  3295. script_config.input_min_value = config_switch(w2);
  3296. }
  3297. else if(strcmpi(w1,"input_max_value")==0) {
  3298. script_config.input_max_value = config_switch(w2);
  3299. }
  3300. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3301. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3302. }
  3303. else if(strcmpi(w1,"import")==0){
  3304. script_config_read(w2);
  3305. }
  3306. }
  3307. fclose(fp);
  3308. return 0;
  3309. }
  3310. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3311. {
  3312. struct script_code *code = (struct script_code *)data;
  3313. if(code){
  3314. script_free_vars( &code->script_vars );
  3315. aFree( code->script_buf );
  3316. aFree( code );
  3317. }
  3318. return 0;
  3319. }
  3320. static int do_final_autobonus_sub (DBKey key,void *data,va_list ap)
  3321. {
  3322. struct script_code *script = (struct script_code *)data;
  3323. if( script )
  3324. script_free_code(script);
  3325. return 0;
  3326. }
  3327. void script_run_autobonus(const char *autobonus, int id, int pos)
  3328. {
  3329. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3330. if( script )
  3331. {
  3332. current_equip_item_index = pos;
  3333. run_script(script,0,id,0);
  3334. }
  3335. }
  3336. void script_add_autobonus(const char *autobonus)
  3337. {
  3338. if( strdb_get(autobonus_db, autobonus) == NULL )
  3339. {
  3340. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3341. if( script )
  3342. strdb_put(autobonus_db, autobonus, script);
  3343. }
  3344. }
  3345. /// resets a temporary character array variable to given value
  3346. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3347. {
  3348. int key;
  3349. uint8 idx;
  3350. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3351. {
  3352. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3353. return;
  3354. }
  3355. key = add_str(varname);
  3356. if( is_string_variable(varname) )
  3357. {
  3358. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3359. {
  3360. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3361. }
  3362. }
  3363. else
  3364. {
  3365. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3366. {
  3367. pc_setreg(sd, reference_uid(key, idx), (int)value);
  3368. }
  3369. }
  3370. }
  3371. /// sets a temporary character array variable element idx to given value
  3372. /// @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.
  3373. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3374. {
  3375. int key;
  3376. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3377. {
  3378. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3379. return;
  3380. }
  3381. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3382. {
  3383. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3384. return;
  3385. }
  3386. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3387. if( is_string_variable(varname) )
  3388. {
  3389. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3390. }
  3391. else
  3392. {
  3393. pc_setreg(sd, reference_uid(key, idx), (int)value);
  3394. }
  3395. if( refcache )
  3396. {// save to avoid repeated add_str calls
  3397. refcache[0] = key;
  3398. }
  3399. }
  3400. /*==========================================
  3401. * 終了
  3402. *------------------------------------------*/
  3403. int do_final_script()
  3404. {
  3405. #ifdef DEBUG_HASH
  3406. if (battle_config.etc_log)
  3407. {
  3408. FILE *fp = fopen("hash_dump.txt","wt");
  3409. if(fp) {
  3410. int i,count[SCRIPT_HASH_SIZE];
  3411. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3412. int n=0;
  3413. int min=INT_MAX,max=0,zero=0;
  3414. double mean=0.0f;
  3415. double median=0.0f;
  3416. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3417. memset(count, 0, sizeof(count));
  3418. fprintf(fp,"num : hash : data_name\n");
  3419. fprintf(fp,"---------------------------------------------------------------\n");
  3420. for(i=LABEL_START; i<str_num; i++) {
  3421. unsigned int h = calc_hash(get_str(i));
  3422. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3423. ++count[h];
  3424. }
  3425. fprintf(fp,"--------------------\n\n");
  3426. memset(count2, 0, sizeof(count2));
  3427. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3428. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3429. if(min > count[i])
  3430. min = count[i]; // minimun count of collision
  3431. if(max < count[i])
  3432. max = count[i]; // maximun count of collision
  3433. if(count[i] == 0)
  3434. zero++;
  3435. ++count2[count[i]];
  3436. }
  3437. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3438. for( i=min; i <= max; ++i ){
  3439. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3440. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3441. }
  3442. for( i=min; i <= max; ++i ){
  3443. n += count2[i];
  3444. if( n*2 >= SCRIPT_HASH_SIZE )
  3445. {
  3446. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3447. median = (i+i+1)/2.0f;
  3448. else
  3449. median = i;
  3450. break;
  3451. }
  3452. }
  3453. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3454. fclose(fp);
  3455. }
  3456. }
  3457. #endif
  3458. mapreg_final();
  3459. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3460. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3461. autobonus_db->destroy(autobonus_db, do_final_autobonus_sub);
  3462. if(sleep_db) {
  3463. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3464. while(n) {
  3465. struct script_state *st = (struct script_state *)n->data;
  3466. script_free_state(st);
  3467. n = n->next;
  3468. }
  3469. linkdb_final(&sleep_db);
  3470. }
  3471. if (str_data)
  3472. aFree(str_data);
  3473. if (str_buf)
  3474. aFree(str_buf);
  3475. return 0;
  3476. }
  3477. /*==========================================
  3478. * 初期化
  3479. *------------------------------------------*/
  3480. int do_init_script()
  3481. {
  3482. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3483. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3484. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3485. mapreg_init();
  3486. return 0;
  3487. }
  3488. int script_reload()
  3489. {
  3490. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3491. scriptlabel_db->clear(scriptlabel_db, NULL);
  3492. if(sleep_db) {
  3493. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3494. while(n) {
  3495. struct script_state *st = (struct script_state *)n->data;
  3496. script_free_state(st);
  3497. n = n->next;
  3498. }
  3499. linkdb_final(&sleep_db);
  3500. }
  3501. mapreg_reload();
  3502. return 0;
  3503. }
  3504. //-----------------------------------------------------------------------------
  3505. // buildin functions
  3506. //
  3507. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3508. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3509. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3510. /////////////////////////////////////////////////////////////////////
  3511. // NPC interaction
  3512. //
  3513. /// Appends a message to the npc dialog.
  3514. /// If a dialog doesn't exist yet, one is created.
  3515. ///
  3516. /// mes "<message>";
  3517. BUILDIN_FUNC(mes)
  3518. {
  3519. TBL_PC* sd = script_rid2sd(st);
  3520. if( sd == NULL )
  3521. return 0;
  3522. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3523. return 0;
  3524. }
  3525. /// Displays the button 'next' in the npc dialog.
  3526. /// The dialog text is cleared and the script continues when the button is pressed.
  3527. ///
  3528. /// next;
  3529. BUILDIN_FUNC(next)
  3530. {
  3531. TBL_PC* sd;
  3532. sd = script_rid2sd(st);
  3533. if( sd == NULL )
  3534. return 0;
  3535. st->state = STOP;
  3536. clif_scriptnext(sd, st->oid);
  3537. return 0;
  3538. }
  3539. /// Ends the script and displays the button 'close' on the npc dialog.
  3540. /// The dialog is closed when the button is pressed.
  3541. ///
  3542. /// close;
  3543. BUILDIN_FUNC(close)
  3544. {
  3545. TBL_PC* sd;
  3546. sd = script_rid2sd(st);
  3547. if( sd == NULL )
  3548. return 0;
  3549. st->state = END;
  3550. clif_scriptclose(sd, st->oid);
  3551. return 0;
  3552. }
  3553. /// Displays the button 'close' on the npc dialog.
  3554. /// The dialog is closed and the script continues when the button is pressed.
  3555. ///
  3556. /// close2;
  3557. BUILDIN_FUNC(close2)
  3558. {
  3559. TBL_PC* sd;
  3560. sd = script_rid2sd(st);
  3561. if( sd == NULL )
  3562. return 0;
  3563. st->state = STOP;
  3564. clif_scriptclose(sd, st->oid);
  3565. return 0;
  3566. }
  3567. /// Counts the number of valid and total number of options in 'str'
  3568. /// If max_count > 0 the counting stops when that valid option is reached
  3569. /// total is incremented for each option (NULL is supported)
  3570. static int menu_countoptions(const char* str, int max_count, int* total)
  3571. {
  3572. int count = 0;
  3573. int bogus_total;
  3574. if( total == NULL )
  3575. total = &bogus_total;
  3576. ++(*total);
  3577. // initial empty options
  3578. while( *str == ':' )
  3579. {
  3580. ++str;
  3581. ++(*total);
  3582. }
  3583. // count menu options
  3584. while( *str != '\0' )
  3585. {
  3586. ++count;
  3587. --max_count;
  3588. if( max_count == 0 )
  3589. break;
  3590. while( *str != ':' && *str != '\0' )
  3591. ++str;
  3592. while( *str == ':' )
  3593. {
  3594. ++str;
  3595. ++(*total);
  3596. }
  3597. }
  3598. return count;
  3599. }
  3600. /// Displays a menu with options and goes to the target label.
  3601. /// The script is stopped if cancel is pressed.
  3602. /// Options with no text are not displayed in the client.
  3603. ///
  3604. /// Options can be grouped together, separated by the character ':' in the text:
  3605. /// ex: menu "A:B:C",L_target;
  3606. /// All these options go to the specified target label.
  3607. ///
  3608. /// The index of the selected option is put in the variable @menu.
  3609. /// Indexes start with 1 and are consistent with grouped and empty options.
  3610. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3611. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3612. ///
  3613. /// NOTE: the client closes the npc dialog when cancel is pressed
  3614. ///
  3615. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3616. BUILDIN_FUNC(menu)
  3617. {
  3618. int i;
  3619. const char* text;
  3620. TBL_PC* sd;
  3621. sd = script_rid2sd(st);
  3622. if( sd == NULL )
  3623. return 0;
  3624. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3625. if( sd->state.menu_or_input == 0 )
  3626. {
  3627. struct StringBuf buf;
  3628. struct script_data* data;
  3629. if( script_lastdata(st) % 2 == 0 )
  3630. {// argument count is not even (1st argument is at index 2)
  3631. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3632. st->state = END;
  3633. return 1;
  3634. }
  3635. StringBuf_Init(&buf);
  3636. sd->npc_menu = 0;
  3637. for( i = 2; i < script_lastdata(st); i += 2 )
  3638. {
  3639. // menu options
  3640. text = script_getstr(st, i);
  3641. // target label
  3642. data = script_getdata(st, i+1);
  3643. if( !data_islabel(data) )
  3644. {// not a label
  3645. StringBuf_Destroy(&buf);
  3646. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3647. script_reportdata(data);
  3648. st->state = END;
  3649. return 1;
  3650. }
  3651. // append option(s)
  3652. if( text[0] == '\0' )
  3653. continue;// empty string, ignore
  3654. if( sd->npc_menu > 0 )
  3655. StringBuf_AppendStr(&buf, ":");
  3656. StringBuf_AppendStr(&buf, text);
  3657. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3658. }
  3659. st->state = RERUNLINE;
  3660. sd->state.menu_or_input = 1;
  3661. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3662. StringBuf_Destroy(&buf);
  3663. if( sd->npc_menu >= 0xff )
  3664. {// 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
  3665. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3666. script_reportsrc(st);
  3667. }
  3668. }
  3669. else if( sd->npc_menu == 0xff )
  3670. {// Cancel was pressed
  3671. sd->state.menu_or_input = 0;
  3672. st->state = END;
  3673. }
  3674. else
  3675. {// goto target label
  3676. int menu = 0;
  3677. sd->state.menu_or_input = 0;
  3678. if( sd->npc_menu <= 0 )
  3679. {
  3680. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3681. st->state = END;
  3682. return 1;
  3683. }
  3684. // get target label
  3685. for( i = 2; i < script_lastdata(st); i += 2 )
  3686. {
  3687. text = script_getstr(st, i);
  3688. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3689. if( sd->npc_menu <= 0 )
  3690. break;// entry found
  3691. }
  3692. if( sd->npc_menu > 0 )
  3693. {// Invalid selection
  3694. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3695. st->state = END;
  3696. return 1;
  3697. }
  3698. if( !data_islabel(script_getdata(st, i + 1)) )
  3699. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3700. ShowError("script:menu: unexpected data in label argument\n");
  3701. script_reportdata(script_getdata(st, i + 1));
  3702. st->state = END;
  3703. return 1;
  3704. }
  3705. pc_setreg(sd, add_str("@menu"), menu);
  3706. st->pos = script_getnum(st, i + 1);
  3707. st->state = GOTO;
  3708. }
  3709. return 0;
  3710. }
  3711. /// Displays a menu with options and returns the selected option.
  3712. /// Behaves like 'menu' without the target labels.
  3713. ///
  3714. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3715. ///
  3716. /// @see menu
  3717. BUILDIN_FUNC(select)
  3718. {
  3719. int i;
  3720. const char* text;
  3721. TBL_PC* sd;
  3722. sd = script_rid2sd(st);
  3723. if( sd == NULL )
  3724. return 0;
  3725. if( sd->state.menu_or_input == 0 )
  3726. {
  3727. struct StringBuf buf;
  3728. StringBuf_Init(&buf);
  3729. sd->npc_menu = 0;
  3730. for( i = 2; i <= script_lastdata(st); ++i )
  3731. {
  3732. text = script_getstr(st, i);
  3733. if( sd->npc_menu > 0 )
  3734. StringBuf_AppendStr(&buf, ":");
  3735. StringBuf_AppendStr(&buf, text);
  3736. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3737. }
  3738. st->state = RERUNLINE;
  3739. sd->state.menu_or_input = 1;
  3740. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3741. StringBuf_Destroy(&buf);
  3742. if( sd->npc_menu >= 0xff )
  3743. {
  3744. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3745. script_reportsrc(st);
  3746. }
  3747. }
  3748. else if( sd->npc_menu == 0xff )
  3749. {// Cancel was pressed
  3750. sd->state.menu_or_input = 0;
  3751. st->state = END;
  3752. }
  3753. else
  3754. {// return selected option
  3755. int menu = 0;
  3756. sd->state.menu_or_input = 0;
  3757. for( i = 2; i <= script_lastdata(st); ++i )
  3758. {
  3759. text = script_getstr(st, i);
  3760. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3761. if( sd->npc_menu <= 0 )
  3762. break;// entry found
  3763. }
  3764. pc_setreg(sd, add_str("@menu"), menu);
  3765. script_pushint(st, menu);
  3766. st->state = RUN;
  3767. }
  3768. return 0;
  3769. }
  3770. /// Displays a menu with options and returns the selected option.
  3771. /// Behaves like 'menu' without the target labels, except when cancel is
  3772. /// pressed.
  3773. /// When cancel is pressed, the script continues and 255 is returned.
  3774. ///
  3775. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3776. ///
  3777. /// @see menu
  3778. BUILDIN_FUNC(prompt)
  3779. {
  3780. int i;
  3781. const char *text;
  3782. TBL_PC* sd;
  3783. sd = script_rid2sd(st);
  3784. if( sd == NULL )
  3785. return 0;
  3786. if( sd->state.menu_or_input == 0 )
  3787. {
  3788. struct StringBuf buf;
  3789. StringBuf_Init(&buf);
  3790. sd->npc_menu = 0;
  3791. for( i = 2; i <= script_lastdata(st); ++i )
  3792. {
  3793. text = script_getstr(st, i);
  3794. if( sd->npc_menu > 0 )
  3795. StringBuf_AppendStr(&buf, ":");
  3796. StringBuf_AppendStr(&buf, text);
  3797. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3798. }
  3799. st->state = RERUNLINE;
  3800. sd->state.menu_or_input = 1;
  3801. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3802. StringBuf_Destroy(&buf);
  3803. if( sd->npc_menu >= 0xff )
  3804. {
  3805. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3806. script_reportsrc(st);
  3807. }
  3808. }
  3809. else if( sd->npc_menu == 0xff )
  3810. {// Cancel was pressed
  3811. sd->state.menu_or_input = 0;
  3812. pc_setreg(sd, add_str("@menu"), 0xff);
  3813. script_pushint(st, 0xff);
  3814. st->state = RUN;
  3815. }
  3816. else
  3817. {// return selected option
  3818. int menu = 0;
  3819. sd->state.menu_or_input = 0;
  3820. for( i = 2; i <= script_lastdata(st); ++i )
  3821. {
  3822. text = script_getstr(st, i);
  3823. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3824. if( sd->npc_menu <= 0 )
  3825. break;// entry found
  3826. }
  3827. pc_setreg(sd, add_str("@menu"), menu);
  3828. script_pushint(st, menu);
  3829. st->state = RUN;
  3830. }
  3831. return 0;
  3832. }
  3833. /////////////////////////////////////////////////////////////////////
  3834. // ...
  3835. //
  3836. /// Jumps to the target script label.
  3837. ///
  3838. /// goto <label>;
  3839. BUILDIN_FUNC(goto)
  3840. {
  3841. if( !data_islabel(script_getdata(st,2)) )
  3842. {
  3843. ShowError("script:goto: not a label\n");
  3844. script_reportdata(script_getdata(st,2));
  3845. st->state = END;
  3846. return 1;
  3847. }
  3848. st->pos = script_getnum(st,2);
  3849. st->state = GOTO;
  3850. return 0;
  3851. }
  3852. /*==========================================
  3853. * user-defined function call
  3854. *------------------------------------------*/
  3855. BUILDIN_FUNC(callfunc)
  3856. {
  3857. int i, j;
  3858. struct script_retinfo* ri;
  3859. struct script_code* scr;
  3860. const char* str = script_getstr(st,2);
  3861. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3862. if( !scr )
  3863. {
  3864. ShowError("script:callfunc: function not found! [%s]\n", str);
  3865. st->state = END;
  3866. return 1;
  3867. }
  3868. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3869. {
  3870. struct script_data* data = push_copy(st->stack,i);
  3871. if( data_isreference(data) && !data->ref )
  3872. {
  3873. const char* name = reference_getname(data);
  3874. if( name[0] == '.' && name[1] == '@' )
  3875. data->ref = st->stack->var_function;
  3876. else if( name[0] == '.' )
  3877. data->ref = &st->script->script_vars;
  3878. }
  3879. }
  3880. CREATE(ri, struct script_retinfo, 1);
  3881. ri->script = st->script;// script code
  3882. ri->var_function = st->stack->var_function;// scope variables
  3883. ri->pos = st->pos;// script location
  3884. ri->nargs = j;// argument count
  3885. ri->defsp = st->stack->defsp;// default stack pointer
  3886. push_retinfo(st->stack, ri);
  3887. st->pos = 0;
  3888. st->script = scr;
  3889. st->stack->defsp = st->stack->sp;
  3890. st->state = GOTO;
  3891. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3892. return 0;
  3893. }
  3894. /*==========================================
  3895. * subroutine call
  3896. *------------------------------------------*/
  3897. BUILDIN_FUNC(callsub)
  3898. {
  3899. int i,j;
  3900. struct script_retinfo* ri;
  3901. int pos = script_getnum(st,2);
  3902. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3903. {
  3904. ShowError("script:callsub: argument is not a label\n");
  3905. script_reportdata(script_getdata(st,2));
  3906. st->state = END;
  3907. return 1;
  3908. }
  3909. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3910. {
  3911. struct script_data* data = push_copy(st->stack,i);
  3912. if( data_isreference(data) && !data->ref )
  3913. {
  3914. const char* name = reference_getname(data);
  3915. if( name[0] == '.' && name[1] == '@' )
  3916. data->ref = st->stack->var_function;
  3917. }
  3918. }
  3919. CREATE(ri, struct script_retinfo, 1);
  3920. ri->script = st->script;// script code
  3921. ri->var_function = st->stack->var_function;// scope variables
  3922. ri->pos = st->pos;// script location
  3923. ri->nargs = j;// argument count
  3924. ri->defsp = st->stack->defsp;// default stack pointer
  3925. push_retinfo(st->stack, ri);
  3926. st->pos = pos;
  3927. st->stack->defsp = st->stack->sp;
  3928. st->state = GOTO;
  3929. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3930. return 0;
  3931. }
  3932. /// Retrieves an argument provided to callfunc/callsub.
  3933. /// If the argument doesn't exist
  3934. ///
  3935. /// getarg(<index>{,<default_value>}) -> <value>
  3936. BUILDIN_FUNC(getarg)
  3937. {
  3938. struct script_retinfo* ri;
  3939. int idx;
  3940. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3941. {
  3942. ShowError("script:getarg: no callfunc or callsub!\n");
  3943. st->state = END;
  3944. return 1;
  3945. }
  3946. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3947. idx = script_getnum(st,2);
  3948. if( idx >= 0 && idx < ri->nargs )
  3949. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3950. else if( script_hasdata(st,3) )
  3951. script_pushcopy(st, 3);
  3952. else
  3953. {
  3954. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3955. st->state = END;
  3956. return 1;
  3957. }
  3958. return 0;
  3959. }
  3960. /// Returns from the current function, optionaly returning a value from the functions.
  3961. /// Don't use outside script functions.
  3962. ///
  3963. /// return;
  3964. /// return <value>;
  3965. BUILDIN_FUNC(return)
  3966. {
  3967. if( script_hasdata(st,2) )
  3968. {// return value
  3969. struct script_data* data;
  3970. script_pushcopy(st, 2);
  3971. data = script_getdatatop(st, -1);
  3972. if( data_isreference(data) )
  3973. {
  3974. const char* name = reference_getname(data);
  3975. if( name[0] == '.' && name[1] == '@' )
  3976. {// scope variable
  3977. if( !data->ref || data->ref == st->stack->var_function )
  3978. get_val(st, data);// current scope, convert to value
  3979. }
  3980. else if( name[0] == '.' && !data->ref )
  3981. {// script variable, link to current script
  3982. data->ref = &st->script->script_vars;
  3983. }
  3984. }
  3985. }
  3986. else
  3987. {// no return value
  3988. script_pushnil(st);
  3989. }
  3990. st->state = RETFUNC;
  3991. return 0;
  3992. }
  3993. /// Returns a random number from 0 to <range>-1.
  3994. /// Or returns a random number from <min> to <max>.
  3995. /// If <min> is greater than <max>, their numbers are switched.
  3996. /// rand(<range>) -> <int>
  3997. /// rand(<min>,<max>) -> <int>
  3998. BUILDIN_FUNC(rand)
  3999. {
  4000. int range;
  4001. int min;
  4002. int max;
  4003. if( script_hasdata(st,3) )
  4004. {// min,max
  4005. min = script_getnum(st,2);
  4006. max = script_getnum(st,3);
  4007. if( max < min )
  4008. swap(min, max);
  4009. range = max - min + 1;
  4010. }
  4011. else
  4012. {// range
  4013. min = 0;
  4014. range = script_getnum(st,2);
  4015. }
  4016. if( range <= 1 )
  4017. script_pushint(st, min);
  4018. else
  4019. script_pushint(st, rand()%range + min);
  4020. return 0;
  4021. }
  4022. /*==========================================
  4023. *
  4024. *------------------------------------------*/
  4025. BUILDIN_FUNC(warp)
  4026. {
  4027. int ret;
  4028. int x,y;
  4029. const char* str;
  4030. TBL_PC* sd;
  4031. sd = script_rid2sd(st);
  4032. if( sd == NULL )
  4033. return 0;
  4034. str = script_getstr(st,2);
  4035. x = script_getnum(st,3);
  4036. y = script_getnum(st,4);
  4037. if(strcmp(str,"Random")==0)
  4038. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4039. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4040. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4041. else
  4042. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4043. if( ret ) {
  4044. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4045. script_reportsrc(st);
  4046. }
  4047. return 0;
  4048. }
  4049. /*==========================================
  4050. * エリア指定ワープ
  4051. *------------------------------------------*/
  4052. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4053. {
  4054. int x,y;
  4055. unsigned int map;
  4056. map=va_arg(ap, unsigned int);
  4057. x=va_arg(ap,int);
  4058. y=va_arg(ap,int);
  4059. if(map == 0)
  4060. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4061. else
  4062. pc_setpos((TBL_PC *)bl,map,x,y,CLR_OUTSIGHT);
  4063. return 0;
  4064. }
  4065. BUILDIN_FUNC(areawarp)
  4066. {
  4067. int x,y,m;
  4068. unsigned int index;
  4069. const char *str;
  4070. const char *mapname;
  4071. int x0,y0,x1,y1;
  4072. mapname=script_getstr(st,2);
  4073. x0=script_getnum(st,3);
  4074. y0=script_getnum(st,4);
  4075. x1=script_getnum(st,5);
  4076. y1=script_getnum(st,6);
  4077. str=script_getstr(st,7);
  4078. x=script_getnum(st,8);
  4079. y=script_getnum(st,9);
  4080. if( (m=map_mapname2mapid(mapname))< 0)
  4081. return 0;
  4082. if(strcmp(str,"Random")==0)
  4083. index = 0;
  4084. else if(!(index=mapindex_name2id(str)))
  4085. return 0;
  4086. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  4087. return 0;
  4088. }
  4089. /*==========================================
  4090. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4091. *------------------------------------------*/
  4092. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4093. {
  4094. int hp, sp;
  4095. hp = va_arg(ap, int);
  4096. sp = va_arg(ap, int);
  4097. pc_percentheal((TBL_PC *)bl,hp,sp);
  4098. return 0;
  4099. }
  4100. BUILDIN_FUNC(areapercentheal)
  4101. {
  4102. int hp,sp,m;
  4103. const char *mapname;
  4104. int x0,y0,x1,y1;
  4105. mapname=script_getstr(st,2);
  4106. x0=script_getnum(st,3);
  4107. y0=script_getnum(st,4);
  4108. x1=script_getnum(st,5);
  4109. y1=script_getnum(st,6);
  4110. hp=script_getnum(st,7);
  4111. sp=script_getnum(st,8);
  4112. if( (m=map_mapname2mapid(mapname))< 0)
  4113. return 0;
  4114. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4115. return 0;
  4116. }
  4117. /*==========================================
  4118. * warpchar [LuzZza]
  4119. * Useful for warp one player from
  4120. * another player npc-session.
  4121. * Using: warpchar "mapname",x,y,Char_ID;
  4122. *------------------------------------------*/
  4123. BUILDIN_FUNC(warpchar)
  4124. {
  4125. int x,y,a;
  4126. const char *str;
  4127. TBL_PC *sd;
  4128. str=script_getstr(st,2);
  4129. x=script_getnum(st,3);
  4130. y=script_getnum(st,4);
  4131. a=script_getnum(st,5);
  4132. sd = map_charid2sd(a);
  4133. if( sd == NULL )
  4134. return 0;
  4135. if(strcmp(str, "Random") == 0)
  4136. pc_randomwarp(sd, CLR_TELEPORT);
  4137. else
  4138. if(strcmp(str, "SavePoint") == 0)
  4139. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4140. else
  4141. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4142. return 0;
  4143. }
  4144. /*==========================================
  4145. * Warpparty - [Fredzilla] [Paradox924X]
  4146. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4147. * If 'from_mapname' is specified, only the party members on that map will be warped
  4148. *------------------------------------------*/
  4149. BUILDIN_FUNC(warpparty)
  4150. {
  4151. TBL_PC *sd = NULL;
  4152. TBL_PC *pl_sd;
  4153. struct party_data* p;
  4154. int type;
  4155. int mapindex;
  4156. int i;
  4157. const char* str = script_getstr(st,2);
  4158. int x = script_getnum(st,3);
  4159. int y = script_getnum(st,4);
  4160. int p_id = script_getnum(st,5);
  4161. const char* str2 = NULL;
  4162. if ( script_hasdata(st,6) )
  4163. str2 = script_getstr(st,6);
  4164. p = party_search(p_id);
  4165. if(!p)
  4166. return 0;
  4167. type = ( strcmp(str,"Random")==0 ) ? 0
  4168. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4169. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4170. : ( strcmp(str,"Leader")==0 ) ? 3
  4171. : 4;
  4172. switch (type)
  4173. {
  4174. case 3:
  4175. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4176. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4177. return 0;
  4178. pl_sd = p->data[i].sd;
  4179. mapindex = pl_sd->mapindex;
  4180. x = pl_sd->bl.x;
  4181. y = pl_sd->bl.y;
  4182. break;
  4183. case 4:
  4184. mapindex = mapindex_name2id(str);
  4185. break;
  4186. case 2:
  4187. //"SavePoint" uses save point of the currently attached player
  4188. if (( sd = script_rid2sd(st) ) == NULL )
  4189. return 0;
  4190. default:
  4191. mapindex = 0;
  4192. break;
  4193. }
  4194. for (i = 0; i < MAX_PARTY; i++)
  4195. {
  4196. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4197. continue;
  4198. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4199. continue;
  4200. if( pc_isdead(pl_sd) )
  4201. continue;
  4202. switch( type )
  4203. {
  4204. case 0: // Random
  4205. if(!map[pl_sd->bl.m].flag.nowarp)
  4206. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4207. break;
  4208. case 1: // SavePointAll
  4209. if(!map[pl_sd->bl.m].flag.noreturn)
  4210. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4211. break;
  4212. case 2: // SavePoint
  4213. if(!map[pl_sd->bl.m].flag.noreturn)
  4214. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4215. break;
  4216. case 3: // Leader
  4217. case 4: // m,x,y
  4218. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4219. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4220. break;
  4221. }
  4222. }
  4223. return 0;
  4224. }
  4225. /*==========================================
  4226. * Warpguild - [Fredzilla]
  4227. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4228. *------------------------------------------*/
  4229. BUILDIN_FUNC(warpguild)
  4230. {
  4231. TBL_PC *sd = NULL;
  4232. TBL_PC *pl_sd;
  4233. struct guild* g;
  4234. struct s_mapiterator* iter;
  4235. int type;
  4236. const char* str = script_getstr(st,2);
  4237. int x = script_getnum(st,3);
  4238. int y = script_getnum(st,4);
  4239. int gid = script_getnum(st,5);
  4240. g = guild_search(gid);
  4241. if( g == NULL )
  4242. return 0;
  4243. type = ( strcmp(str,"Random")==0 ) ? 0
  4244. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4245. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4246. : 3;
  4247. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4248. {// "SavePoint" uses save point of the currently attached player
  4249. return 0;
  4250. }
  4251. iter = mapit_getallusers();
  4252. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4253. {
  4254. if( pl_sd->status.guild_id != gid )
  4255. continue;
  4256. switch( type )
  4257. {
  4258. case 0: // Random
  4259. if(!map[pl_sd->bl.m].flag.nowarp)
  4260. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4261. break;
  4262. case 1: // SavePointAll
  4263. if(!map[pl_sd->bl.m].flag.noreturn)
  4264. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4265. break;
  4266. case 2: // SavePoint
  4267. if(!map[pl_sd->bl.m].flag.noreturn)
  4268. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4269. break;
  4270. case 3: // m,x,y
  4271. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4272. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4273. break;
  4274. }
  4275. }
  4276. mapit_free(iter);
  4277. return 0;
  4278. }
  4279. /*==========================================
  4280. *
  4281. *------------------------------------------*/
  4282. BUILDIN_FUNC(heal)
  4283. {
  4284. TBL_PC *sd;
  4285. int hp,sp;
  4286. sd = script_rid2sd(st);
  4287. if (!sd) return 0;
  4288. hp=script_getnum(st,2);
  4289. sp=script_getnum(st,3);
  4290. status_heal(&sd->bl, hp, sp, 1);
  4291. return 0;
  4292. }
  4293. /*==========================================
  4294. *
  4295. *------------------------------------------*/
  4296. BUILDIN_FUNC(itemheal)
  4297. {
  4298. TBL_PC *sd;
  4299. int hp,sp;
  4300. hp=script_getnum(st,2);
  4301. sp=script_getnum(st,3);
  4302. if(potion_flag==1) {
  4303. potion_hp = hp;
  4304. potion_sp = sp;
  4305. return 0;
  4306. }
  4307. sd = script_rid2sd(st);
  4308. if (!sd) return 0;
  4309. pc_itemheal(sd,sd->itemid,hp,sp);
  4310. return 0;
  4311. }
  4312. /*==========================================
  4313. *
  4314. *------------------------------------------*/
  4315. BUILDIN_FUNC(percentheal)
  4316. {
  4317. int hp,sp;
  4318. TBL_PC* sd;
  4319. hp=script_getnum(st,2);
  4320. sp=script_getnum(st,3);
  4321. if(potion_flag==1) {
  4322. potion_per_hp = hp;
  4323. potion_per_sp = sp;
  4324. return 0;
  4325. }
  4326. sd = script_rid2sd(st);
  4327. if( sd == NULL )
  4328. return 0;
  4329. pc_percentheal(sd,hp,sp);
  4330. return 0;
  4331. }
  4332. /*==========================================
  4333. *
  4334. *------------------------------------------*/
  4335. BUILDIN_FUNC(jobchange)
  4336. {
  4337. int job, upper=-1;
  4338. job=script_getnum(st,2);
  4339. if( script_hasdata(st,3) )
  4340. upper=script_getnum(st,3);
  4341. if (pcdb_checkid(job))
  4342. {
  4343. TBL_PC* sd;
  4344. sd = script_rid2sd(st);
  4345. if( sd == NULL )
  4346. return 0;
  4347. pc_jobchange(sd, job, upper);
  4348. }
  4349. return 0;
  4350. }
  4351. /*==========================================
  4352. *
  4353. *------------------------------------------*/
  4354. BUILDIN_FUNC(jobname)
  4355. {
  4356. int class_=script_getnum(st,2);
  4357. script_pushconststr(st, (char*)job_name(class_));
  4358. return 0;
  4359. }
  4360. /// Get input from the player.
  4361. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4362. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4363. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4364. /// shorter than 'min' and 0 otherwise.
  4365. ///
  4366. /// input(<var>{,<min>{,<max>}}) -> <int>
  4367. BUILDIN_FUNC(input)
  4368. {
  4369. TBL_PC* sd;
  4370. struct script_data* data;
  4371. int uid;
  4372. const char* name;
  4373. int min;
  4374. int max;
  4375. sd = script_rid2sd(st);
  4376. if( sd == NULL )
  4377. return 0;
  4378. data = script_getdata(st,2);
  4379. if( !data_isreference(data) ){
  4380. ShowError("script:input: not a variable\n");
  4381. script_reportdata(data);
  4382. st->state = END;
  4383. return 1;
  4384. }
  4385. uid = reference_getuid(data);
  4386. name = reference_getname(data);
  4387. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4388. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4389. if( !sd->state.menu_or_input )
  4390. { // first invocation, display npc input box
  4391. sd->state.menu_or_input = 1;
  4392. st->state = RERUNLINE;
  4393. if( is_string_variable(name) )
  4394. clif_scriptinputstr(sd,st->oid);
  4395. else
  4396. clif_scriptinput(sd,st->oid);
  4397. }
  4398. else
  4399. { // take received text/value and store it in the designated variable
  4400. sd->state.menu_or_input = 0;
  4401. if( is_string_variable(name) )
  4402. {
  4403. int len = (int)strlen(sd->npc_str);
  4404. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4405. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4406. }
  4407. else
  4408. {
  4409. int amount = sd->npc_amount;
  4410. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4411. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4412. }
  4413. st->state = RUN;
  4414. }
  4415. return 0;
  4416. }
  4417. /// Sets the value of a variable.
  4418. /// The value is converted to the type of the variable.
  4419. ///
  4420. /// set(<variable>,<value>) -> <variable>
  4421. BUILDIN_FUNC(set)
  4422. {
  4423. TBL_PC* sd = NULL;
  4424. struct script_data* data;
  4425. int num;
  4426. const char* name;
  4427. char prefix;
  4428. data = script_getdata(st,2);
  4429. if( !data_isreference(data) )
  4430. {
  4431. ShowError("script:set: not a variable\n");
  4432. script_reportdata(script_getdata(st,2));
  4433. st->state = END;
  4434. return 1;
  4435. }
  4436. num = reference_getuid(data);
  4437. name = reference_getname(data);
  4438. prefix = *name;
  4439. if( not_server_variable(prefix) )
  4440. {
  4441. sd = script_rid2sd(st);
  4442. if( sd == NULL )
  4443. {
  4444. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4445. return 0;
  4446. }
  4447. }
  4448. if( is_string_variable(name) )
  4449. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4450. else
  4451. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4452. // return a copy of the variable reference
  4453. script_pushcopy(st,2);
  4454. return 0;
  4455. }
  4456. /////////////////////////////////////////////////////////////////////
  4457. /// Array variables
  4458. ///
  4459. /// Returns the size of the specified array
  4460. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4461. {
  4462. int32 ret = idx;
  4463. if( isstring )
  4464. {
  4465. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4466. {
  4467. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4468. if( str && *str )
  4469. ret = idx + 1;
  4470. script_removetop(st, -1, 0);
  4471. }
  4472. }
  4473. else
  4474. {
  4475. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4476. {
  4477. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4478. if( num )
  4479. ret = idx + 1;
  4480. script_removetop(st, -1, 0);
  4481. }
  4482. }
  4483. return ret;
  4484. }
  4485. /// Sets values of an array, from the starting index.
  4486. /// ex: setarray arr[1],1,2,3;
  4487. ///
  4488. /// setarray <array variable>,<value1>{,<value2>...};
  4489. BUILDIN_FUNC(setarray)
  4490. {
  4491. struct script_data* data;
  4492. const char* name;
  4493. int32 start;
  4494. int32 end;
  4495. int32 id;
  4496. int32 i;
  4497. TBL_PC* sd = NULL;
  4498. data = script_getdata(st, 2);
  4499. if( !data_isreference(data) )
  4500. {
  4501. ShowError("script:setarray: not a variable\n");
  4502. script_reportdata(data);
  4503. st->state = END;
  4504. return 1;// not a variable
  4505. }
  4506. id = reference_getid(data);
  4507. start = reference_getindex(data);
  4508. name = reference_getname(data);
  4509. if( not_array_variable(*name) )
  4510. {
  4511. ShowError("script:setarray: illegal scope\n");
  4512. script_reportdata(data);
  4513. st->state = END;
  4514. return 1;// not supported
  4515. }
  4516. if( not_server_variable(*name) )
  4517. {
  4518. sd = script_rid2sd(st);
  4519. if( sd == NULL )
  4520. return 0;// no player attached
  4521. }
  4522. end = start + script_lastdata(st) - 2;
  4523. if( end > SCRIPT_MAX_ARRAYSIZE )
  4524. end = SCRIPT_MAX_ARRAYSIZE;
  4525. if( is_string_variable(name) )
  4526. {// string array
  4527. for( i = 3; start < end; ++start, ++i )
  4528. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4529. }
  4530. else
  4531. {// int array
  4532. for( i = 3; start < end; ++start, ++i )
  4533. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4534. }
  4535. return 0;
  4536. }
  4537. /// Sets count values of an array, from the starting index.
  4538. /// ex: cleararray arr[0],0,1;
  4539. ///
  4540. /// cleararray <array variable>,<value>,<count>;
  4541. BUILDIN_FUNC(cleararray)
  4542. {
  4543. struct script_data* data;
  4544. const char* name;
  4545. int32 start;
  4546. int32 end;
  4547. int32 id;
  4548. void* v;
  4549. TBL_PC* sd = NULL;
  4550. data = script_getdata(st, 2);
  4551. if( !data_isreference(data) )
  4552. {
  4553. ShowError("script:cleararray: not a variable\n");
  4554. script_reportdata(data);
  4555. st->state = END;
  4556. return 1;// not a variable
  4557. }
  4558. id = reference_getid(data);
  4559. start = reference_getindex(data);
  4560. name = reference_getname(data);
  4561. if( not_array_variable(*name) )
  4562. {
  4563. ShowError("script:cleararray: illegal scope\n");
  4564. script_reportdata(data);
  4565. st->state = END;
  4566. return 1;// not supported
  4567. }
  4568. if( not_server_variable(*name) )
  4569. {
  4570. sd = script_rid2sd(st);
  4571. if( sd == NULL )
  4572. return 0;// no player attached
  4573. }
  4574. if( is_string_variable(name) )
  4575. v = (void*)script_getstr(st, 3);
  4576. else
  4577. v = (void*)script_getnum(st, 3);
  4578. end = start + script_getnum(st, 4);
  4579. if( end > SCRIPT_MAX_ARRAYSIZE )
  4580. end = SCRIPT_MAX_ARRAYSIZE;
  4581. for( ; start < end; ++start )
  4582. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4583. return 0;
  4584. }
  4585. /// Copies data from one array to another.
  4586. /// ex: copyarray arr[0],arr[2],2;
  4587. ///
  4588. /// copyarray <destination array variable>,<source array variable>,<count>;
  4589. BUILDIN_FUNC(copyarray)
  4590. {
  4591. struct script_data* data1;
  4592. struct script_data* data2;
  4593. const char* name1;
  4594. const char* name2;
  4595. int32 idx1;
  4596. int32 idx2;
  4597. int32 id1;
  4598. int32 id2;
  4599. void* v;
  4600. int32 i;
  4601. int32 count;
  4602. TBL_PC* sd = NULL;
  4603. data1 = script_getdata(st, 2);
  4604. data2 = script_getdata(st, 3);
  4605. if( !data_isreference(data1) || !data_isreference(data2) )
  4606. {
  4607. ShowError("script:copyarray: not a variable\n");
  4608. script_reportdata(data1);
  4609. script_reportdata(data2);
  4610. st->state = END;
  4611. return 1;// not a variable
  4612. }
  4613. id1 = reference_getid(data1);
  4614. id2 = reference_getid(data2);
  4615. idx1 = reference_getindex(data1);
  4616. idx2 = reference_getindex(data2);
  4617. name1 = reference_getname(data1);
  4618. name2 = reference_getname(data2);
  4619. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4620. {
  4621. ShowError("script:copyarray: illegal scope\n");
  4622. script_reportdata(data1);
  4623. script_reportdata(data2);
  4624. st->state = END;
  4625. return 1;// not supported
  4626. }
  4627. if( is_string_variable(name1) != is_string_variable(name2) )
  4628. {
  4629. ShowError("script:copyarray: type mismatch\n");
  4630. script_reportdata(data1);
  4631. script_reportdata(data2);
  4632. st->state = END;
  4633. return 1;// data type mismatch
  4634. }
  4635. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4636. {
  4637. sd = script_rid2sd(st);
  4638. if( sd == NULL )
  4639. return 0;// no player attached
  4640. }
  4641. count = script_getnum(st, 4);
  4642. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  4643. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  4644. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4645. return 0;// nothing to copy
  4646. if( id1 == id2 && idx1 > idx2 )
  4647. {// destination might be overlapping the source - copy in reverse order
  4648. for( i = count - 1; i >= 0; --i )
  4649. {
  4650. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4651. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4652. script_removetop(st, -1, 0);
  4653. }
  4654. }
  4655. else
  4656. {// normal copy
  4657. for( i = 0; i < count; ++i )
  4658. {
  4659. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  4660. {
  4661. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4662. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4663. script_removetop(st, -1, 0);
  4664. }
  4665. else// out of range - assume ""/0
  4666. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4667. }
  4668. }
  4669. return 0;
  4670. }
  4671. /// Returns the size of the array.
  4672. /// Assumes that everything before the starting index exists.
  4673. /// ex: getarraysize(arr[3])
  4674. ///
  4675. /// getarraysize(<array variable>) -> <int>
  4676. BUILDIN_FUNC(getarraysize)
  4677. {
  4678. struct script_data* data;
  4679. const char* name;
  4680. data = script_getdata(st, 2);
  4681. if( !data_isreference(data) )
  4682. {
  4683. ShowError("script:getarraysize: not a variable\n");
  4684. script_reportdata(data);
  4685. script_pushnil(st);
  4686. st->state = END;
  4687. return 1;// not a variable
  4688. }
  4689. name = reference_getname(data);
  4690. if( not_array_variable(*name) )
  4691. {
  4692. ShowError("script:getarraysize: illegal scope\n");
  4693. script_reportdata(data);
  4694. script_pushnil(st);
  4695. st->state = END;
  4696. return 1;// not supported
  4697. }
  4698. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4699. return 0;
  4700. }
  4701. /// Deletes count or all the elements in an array, from the starting index.
  4702. /// ex: deletearray arr[4],2;
  4703. ///
  4704. /// deletearray <array variable>;
  4705. /// deletearray <array variable>,<count>;
  4706. BUILDIN_FUNC(deletearray)
  4707. {
  4708. struct script_data* data;
  4709. const char* name;
  4710. int start;
  4711. int end;
  4712. int id;
  4713. TBL_PC *sd = NULL;
  4714. data = script_getdata(st, 2);
  4715. if( !data_isreference(data) )
  4716. {
  4717. ShowError("script:deletearray: not a variable\n");
  4718. script_reportdata(data);
  4719. st->state = END;
  4720. return 1;// not a variable
  4721. }
  4722. id = reference_getid(data);
  4723. start = reference_getindex(data);
  4724. name = reference_getname(data);
  4725. if( not_array_variable(*name) )
  4726. {
  4727. ShowError("script:deletearray: illegal scope\n");
  4728. script_reportdata(data);
  4729. st->state = END;
  4730. return 1;// not supported
  4731. }
  4732. if( not_server_variable(*name) )
  4733. {
  4734. sd = script_rid2sd(st);
  4735. if( sd == NULL )
  4736. return 0;// no player attached
  4737. }
  4738. end = SCRIPT_MAX_ARRAYSIZE;
  4739. if( start >= end )
  4740. return 0;// nothing to free
  4741. if( script_hasdata(st,3) )
  4742. {
  4743. int count = script_getnum(st, 3);
  4744. if( count > end - start )
  4745. count = end - start;
  4746. if( count <= 0 )
  4747. return 0;// nothing to free
  4748. // move rest of the elements backward
  4749. for( ; start + count < end; ++start )
  4750. {
  4751. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4752. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4753. script_removetop(st, -1, 0);
  4754. }
  4755. }
  4756. // clear the rest of the array
  4757. if( is_string_variable(name) )
  4758. {
  4759. for( ; start < end; ++start )
  4760. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4761. }
  4762. else
  4763. {
  4764. for( ; start < end; ++start )
  4765. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4766. }
  4767. return 0;
  4768. }
  4769. /// Returns a reference to the target index of the array variable.
  4770. /// Equivalent to var[index].
  4771. ///
  4772. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4773. BUILDIN_FUNC(getelementofarray)
  4774. {
  4775. struct script_data* data;
  4776. const char* name;
  4777. int32 id;
  4778. int i;
  4779. data = script_getdata(st, 2);
  4780. if( !data_isreference(data) )
  4781. {
  4782. ShowError("script:getelementofarray: not a variable\n");
  4783. script_reportdata(data);
  4784. script_pushnil(st);
  4785. st->state = END;
  4786. return 1;// not a variable
  4787. }
  4788. id = reference_getid(data);
  4789. name = reference_getname(data);
  4790. if( not_array_variable(*name) )
  4791. {
  4792. ShowError("script:getelementofarray: illegal scope\n");
  4793. script_reportdata(data);
  4794. script_pushnil(st);
  4795. st->state = END;
  4796. return 1;// not supported
  4797. }
  4798. i = script_getnum(st, 3);
  4799. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  4800. {
  4801. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4802. script_reportdata(data);
  4803. script_pushnil(st);
  4804. st->state = END;
  4805. return 1;// out of range
  4806. }
  4807. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4808. return 0;
  4809. }
  4810. /////////////////////////////////////////////////////////////////////
  4811. /// ...
  4812. ///
  4813. /*==========================================
  4814. *
  4815. *------------------------------------------*/
  4816. BUILDIN_FUNC(setlook)
  4817. {
  4818. int type,val;
  4819. TBL_PC* sd;
  4820. type=script_getnum(st,2);
  4821. val=script_getnum(st,3);
  4822. sd = script_rid2sd(st);
  4823. if( sd == NULL )
  4824. return 0;
  4825. pc_changelook(sd,type,val);
  4826. return 0;
  4827. }
  4828. BUILDIN_FUNC(changelook)
  4829. { // As setlook but only client side
  4830. int type,val;
  4831. TBL_PC* sd;
  4832. type=script_getnum(st,2);
  4833. val=script_getnum(st,3);
  4834. sd = script_rid2sd(st);
  4835. if( sd == NULL )
  4836. return 0;
  4837. clif_changelook(&sd->bl,type,val);
  4838. return 0;
  4839. }
  4840. /*==========================================
  4841. *
  4842. *------------------------------------------*/
  4843. BUILDIN_FUNC(cutin)
  4844. {
  4845. TBL_PC* sd;
  4846. sd = script_rid2sd(st);
  4847. if( sd == NULL )
  4848. return 0;
  4849. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4850. return 0;
  4851. }
  4852. /*==========================================
  4853. *
  4854. *------------------------------------------*/
  4855. BUILDIN_FUNC(viewpoint)
  4856. {
  4857. int type,x,y,id,color;
  4858. TBL_PC* sd;
  4859. type=script_getnum(st,2);
  4860. x=script_getnum(st,3);
  4861. y=script_getnum(st,4);
  4862. id=script_getnum(st,5);
  4863. color=script_getnum(st,6);
  4864. sd = script_rid2sd(st);
  4865. if( sd == NULL )
  4866. return 0;
  4867. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4868. return 0;
  4869. }
  4870. /*==========================================
  4871. *
  4872. *------------------------------------------*/
  4873. BUILDIN_FUNC(countitem)
  4874. {
  4875. int nameid, i;
  4876. int count = 0;
  4877. struct item_data* id = NULL;
  4878. struct script_data* data;
  4879. TBL_PC* sd = script_rid2sd(st);
  4880. if (!sd) {
  4881. script_pushint(st,0);
  4882. return 0;
  4883. }
  4884. data = script_getdata(st,2);
  4885. get_val(st, data); // convert into value in case of a variable
  4886. if( data_isstring(data) )
  4887. {// item name
  4888. id = itemdb_searchname(conv_str(st, data));
  4889. }
  4890. else
  4891. {// item id
  4892. id = itemdb_exists(conv_num(st, data));
  4893. }
  4894. if( id == NULL )
  4895. {
  4896. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4897. script_pushint(st,0);
  4898. return 1;
  4899. }
  4900. nameid = id->nameid;
  4901. for(i = 0; i < MAX_INVENTORY; i++)
  4902. if(sd->status.inventory[i].nameid == nameid)
  4903. count += sd->status.inventory[i].amount;
  4904. script_pushint(st,count);
  4905. return 0;
  4906. }
  4907. /*==========================================
  4908. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4909. * returns number of items that meet the conditions
  4910. *------------------------------------------*/
  4911. BUILDIN_FUNC(countitem2)
  4912. {
  4913. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4914. int count = 0;
  4915. int i;
  4916. struct item_data* id = NULL;
  4917. struct script_data* data;
  4918. TBL_PC* sd = script_rid2sd(st);
  4919. if (!sd) {
  4920. script_pushint(st,0);
  4921. return 0;
  4922. }
  4923. data = script_getdata(st,2);
  4924. get_val(st, data); // convert into value in case of a variable
  4925. if( data_isstring(data) )
  4926. {// item name
  4927. id = itemdb_searchname(conv_str(st, data));
  4928. }
  4929. else
  4930. {// item id
  4931. id = itemdb_exists(conv_num(st, data));
  4932. }
  4933. if( id == NULL )
  4934. {
  4935. ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4936. script_pushint(st,0);
  4937. return 1;
  4938. }
  4939. nameid = id->nameid;
  4940. iden = script_getnum(st,3);
  4941. ref = script_getnum(st,4);
  4942. attr = script_getnum(st,5);
  4943. c1 = (short)script_getnum(st,6);
  4944. c2 = (short)script_getnum(st,7);
  4945. c3 = (short)script_getnum(st,8);
  4946. c4 = (short)script_getnum(st,9);
  4947. for(i = 0; i < MAX_INVENTORY; i++)
  4948. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4949. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4950. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4951. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4952. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4953. sd->status.inventory[i].card[3] == c4
  4954. )
  4955. count += sd->status.inventory[i].amount;
  4956. script_pushint(st,count);
  4957. return 0;
  4958. }
  4959. /*==========================================
  4960. * 重量チェック
  4961. *------------------------------------------*/
  4962. BUILDIN_FUNC(checkweight)
  4963. {
  4964. int nameid, amount, slots;
  4965. unsigned int weight;
  4966. struct item_data* id = NULL;
  4967. struct map_session_data* sd;
  4968. struct script_data* data;
  4969. if( ( sd = script_rid2sd(st) ) == NULL )
  4970. {
  4971. return 0;
  4972. }
  4973. data = script_getdata(st,2);
  4974. get_val(st, data); // convert into value in case of a variable
  4975. if( data_isstring(data) )
  4976. {// item name
  4977. id = itemdb_searchname(conv_str(st, data));
  4978. }
  4979. else
  4980. {// item id
  4981. id = itemdb_exists(conv_num(st, data));
  4982. }
  4983. if( id == NULL )
  4984. {
  4985. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4986. script_pushint(st,0);
  4987. return 1;
  4988. }
  4989. nameid = id->nameid;
  4990. amount = script_getnum(st,3);
  4991. if( amount < 1 )
  4992. {
  4993. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  4994. script_pushint(st,0);
  4995. return 1;
  4996. }
  4997. weight = itemdb_weight(nameid)*amount;
  4998. if( weight + sd->weight > sd->max_weight )
  4999. {// too heavy
  5000. script_pushint(st,0);
  5001. return 0;
  5002. }
  5003. switch( pc_checkadditem(sd, nameid, amount) )
  5004. {
  5005. case ADDITEM_EXIST:
  5006. // item is already in inventory, but there is still space for the requested amount
  5007. break;
  5008. case ADDITEM_NEW:
  5009. slots = pc_inventoryblank(sd);
  5010. if( itemdb_isstackable(nameid) )
  5011. {// stackable
  5012. if( slots < 1 )
  5013. {
  5014. script_pushint(st,0);
  5015. return 0;
  5016. }
  5017. }
  5018. else
  5019. {// non-stackable
  5020. if( slots < amount )
  5021. {
  5022. script_pushint(st,0);
  5023. return 0;
  5024. }
  5025. }
  5026. break;
  5027. case ADDITEM_OVERAMOUNT:
  5028. script_pushint(st,0);
  5029. return 0;
  5030. }
  5031. script_pushint(st,1);
  5032. return 0;
  5033. }
  5034. /*==========================================
  5035. * getitem <item id>,<amount>{,<account ID>};
  5036. * getitem "<item name>",<amount>{,<account ID>};
  5037. *------------------------------------------*/
  5038. BUILDIN_FUNC(getitem)
  5039. {
  5040. int nameid,amount,get_count,i,flag = 0;
  5041. struct item it;
  5042. TBL_PC *sd;
  5043. struct script_data *data;
  5044. data=script_getdata(st,2);
  5045. get_val(st,data);
  5046. if( data_isstring(data) )
  5047. {// "<item name>"
  5048. const char *name=conv_str(st,data);
  5049. struct item_data *item_data = itemdb_searchname(name);
  5050. if( item_data == NULL ){
  5051. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5052. return 1; //No item created.
  5053. }
  5054. nameid=item_data->nameid;
  5055. } else if( data_isint(data) )
  5056. {// <item id>
  5057. nameid=conv_num(st,data);
  5058. //Violet Box, Blue Box, etc - random item pick
  5059. if( nameid < 0 ) {
  5060. nameid=itemdb_searchrandomid(-nameid);
  5061. flag = 1;
  5062. }
  5063. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5064. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5065. return 1; //No item created.
  5066. }
  5067. } else {
  5068. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5069. return 1;
  5070. }
  5071. // <amount>
  5072. if( (amount=script_getnum(st,3)) <= 0)
  5073. return 0; //return if amount <=0, skip the useles iteration
  5074. memset(&it,0,sizeof(it));
  5075. it.nameid=nameid;
  5076. if(!flag)
  5077. it.identify=1;
  5078. else
  5079. it.identify=itemdb_isidentified(nameid);
  5080. if( script_hasdata(st,4) )
  5081. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5082. else
  5083. sd=script_rid2sd(st); // Attached player
  5084. if( sd == NULL ) // no target
  5085. return 0;
  5086. //Check if it's stackable.
  5087. if (!itemdb_isstackable(nameid))
  5088. get_count = 1;
  5089. else
  5090. get_count = amount;
  5091. for (i = 0; i < amount; i += get_count)
  5092. {
  5093. // if not pet egg
  5094. if (!pet_create_egg(sd, nameid))
  5095. {
  5096. if ((flag = pc_additem(sd, &it, get_count)))
  5097. {
  5098. clif_additem(sd, 0, 0, flag);
  5099. if( pc_candrop(sd,&it) )
  5100. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5101. }
  5102. }
  5103. }
  5104. //Logs items, got from (N)PC scripts [Lupus]
  5105. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  5106. log_pick_pc(sd, "N", nameid, amount, NULL);
  5107. return 0;
  5108. }
  5109. /*==========================================
  5110. *
  5111. *------------------------------------------*/
  5112. BUILDIN_FUNC(getitem2)
  5113. {
  5114. int nameid,amount,get_count,i,flag = 0;
  5115. int iden,ref,attr,c1,c2,c3,c4;
  5116. struct item_data *item_data;
  5117. struct item item_tmp;
  5118. TBL_PC *sd;
  5119. struct script_data *data;
  5120. if( script_hasdata(st,11) )
  5121. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5122. else
  5123. sd=script_rid2sd(st); // Attached player
  5124. if( sd == NULL ) // no target
  5125. return 0;
  5126. data=script_getdata(st,2);
  5127. get_val(st,data);
  5128. if( data_isstring(data) ){
  5129. const char *name=conv_str(st,data);
  5130. struct item_data *item_data = itemdb_searchname(name);
  5131. if( item_data )
  5132. nameid=item_data->nameid;
  5133. else
  5134. nameid=UNKNOWN_ITEM_ID;
  5135. }else
  5136. nameid=conv_num(st,data);
  5137. amount=script_getnum(st,3);
  5138. iden=script_getnum(st,4);
  5139. ref=script_getnum(st,5);
  5140. attr=script_getnum(st,6);
  5141. c1=(short)script_getnum(st,7);
  5142. c2=(short)script_getnum(st,8);
  5143. c3=(short)script_getnum(st,9);
  5144. c4=(short)script_getnum(st,10);
  5145. if(nameid<0) { // ランダム
  5146. nameid=itemdb_searchrandomid(-nameid);
  5147. flag = 1;
  5148. }
  5149. if(nameid > 0) {
  5150. memset(&item_tmp,0,sizeof(item_tmp));
  5151. item_data=itemdb_exists(nameid);
  5152. if (item_data == NULL)
  5153. return -1;
  5154. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5155. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5156. }
  5157. else if(item_data->type==IT_PETEGG) {
  5158. iden = 1;
  5159. ref = 0;
  5160. }
  5161. else {
  5162. iden = 1;
  5163. ref = attr = 0;
  5164. }
  5165. item_tmp.nameid=nameid;
  5166. if(!flag)
  5167. item_tmp.identify=iden;
  5168. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5169. item_tmp.identify=0;
  5170. item_tmp.refine=ref;
  5171. item_tmp.attribute=attr;
  5172. item_tmp.card[0]=(short)c1;
  5173. item_tmp.card[1]=(short)c2;
  5174. item_tmp.card[2]=(short)c3;
  5175. item_tmp.card[3]=(short)c4;
  5176. //Check if it's stackable.
  5177. if (!itemdb_isstackable(nameid))
  5178. get_count = 1;
  5179. else
  5180. get_count = amount;
  5181. for (i = 0; i < amount; i += get_count)
  5182. {
  5183. // if not pet egg
  5184. if (!pet_create_egg(sd, nameid))
  5185. {
  5186. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  5187. {
  5188. clif_additem(sd, 0, 0, flag);
  5189. if( pc_candrop(sd,&item_tmp) )
  5190. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5191. }
  5192. }
  5193. }
  5194. //Logs items, got from (N)PC scripts [Lupus]
  5195. if(log_config.enable_logs&0x40)
  5196. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  5197. }
  5198. return 0;
  5199. }
  5200. /*==========================================
  5201. * rentitem <item id>,<seconds>
  5202. * rentitem "<item name>",<seconds>
  5203. *------------------------------------------*/
  5204. BUILDIN_FUNC(rentitem)
  5205. {
  5206. struct map_session_data *sd;
  5207. struct script_data *data;
  5208. struct item it;
  5209. int seconds;
  5210. int nameid = 0, flag;
  5211. data = script_getdata(st,2);
  5212. get_val(st,data);
  5213. if( (sd = script_rid2sd(st)) == NULL )
  5214. return 0;
  5215. if( data_isstring(data) )
  5216. {
  5217. const char *name = conv_str(st,data);
  5218. struct item_data *itd = itemdb_searchname(name);
  5219. if( itd == NULL )
  5220. {
  5221. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5222. return 1;
  5223. }
  5224. nameid = itd->nameid;
  5225. }
  5226. else if( data_isint(data) )
  5227. {
  5228. nameid = conv_num(st,data);
  5229. if( nameid <= 0 || !itemdb_exists(nameid) )
  5230. {
  5231. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5232. return 1;
  5233. }
  5234. }
  5235. else
  5236. {
  5237. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5238. return 1;
  5239. }
  5240. seconds = script_getnum(st,3);
  5241. memset(&it, 0, sizeof(it));
  5242. it.nameid = nameid;
  5243. it.identify = 1;
  5244. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5245. if( (flag = pc_additem(sd, &it, 1)) )
  5246. {
  5247. clif_additem(sd, 0, 0, flag);
  5248. return 1;
  5249. }
  5250. clif_rental_time(sd->fd, nameid, seconds);
  5251. pc_inventory_rental_add(sd, seconds);
  5252. if( log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS )
  5253. log_pick_pc(sd, "N", nameid, 1, NULL);
  5254. return 0;
  5255. }
  5256. /*==========================================
  5257. * gets an item with someone's name inscribed [Skotlex]
  5258. * getinscribeditem item_num, character_name
  5259. * Returned Qty is always 1, only works on equip-able
  5260. * equipment
  5261. *------------------------------------------*/
  5262. BUILDIN_FUNC(getnameditem)
  5263. {
  5264. int nameid;
  5265. struct item item_tmp;
  5266. TBL_PC *sd, *tsd;
  5267. struct script_data *data;
  5268. sd = script_rid2sd(st);
  5269. if (sd == NULL)
  5270. { //Player not attached!
  5271. script_pushint(st,0);
  5272. return 0;
  5273. }
  5274. data=script_getdata(st,2);
  5275. get_val(st,data);
  5276. if( data_isstring(data) ){
  5277. const char *name=conv_str(st,data);
  5278. struct item_data *item_data = itemdb_searchname(name);
  5279. if( item_data == NULL)
  5280. { //Failed
  5281. script_pushint(st,0);
  5282. return 0;
  5283. }
  5284. nameid = item_data->nameid;
  5285. }else
  5286. nameid = conv_num(st,data);
  5287. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5288. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5289. script_pushint(st,0);
  5290. return 0;
  5291. }
  5292. data=script_getdata(st,3);
  5293. get_val(st,data);
  5294. if( data_isstring(data) ) //Char Name
  5295. tsd=map_nick2sd(conv_str(st,data));
  5296. else //Char Id was given
  5297. tsd=map_charid2sd(conv_num(st,data));
  5298. if( tsd == NULL )
  5299. { //Failed
  5300. script_pushint(st,0);
  5301. return 0;
  5302. }
  5303. memset(&item_tmp,0,sizeof(item_tmp));
  5304. item_tmp.nameid=nameid;
  5305. item_tmp.amount=1;
  5306. item_tmp.identify=1;
  5307. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5308. item_tmp.card[2]=tsd->status.char_id;
  5309. item_tmp.card[3]=tsd->status.char_id >> 16;
  5310. if(pc_additem(sd,&item_tmp,1)) {
  5311. script_pushint(st,0);
  5312. return 0; //Failed to add item, we will not drop if they don't fit
  5313. }
  5314. //Logs items, got from (N)PC scripts [Lupus]
  5315. if(log_config.enable_logs&0x40)
  5316. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5317. script_pushint(st,1);
  5318. return 0;
  5319. }
  5320. /*==========================================
  5321. * gets a random item ID from an item group [Skotlex]
  5322. * groupranditem group_num
  5323. *------------------------------------------*/
  5324. BUILDIN_FUNC(grouprandomitem)
  5325. {
  5326. int group;
  5327. group = script_getnum(st,2);
  5328. script_pushint(st,itemdb_searchrandomid(group));
  5329. return 0;
  5330. }
  5331. /*==========================================
  5332. *
  5333. *------------------------------------------*/
  5334. BUILDIN_FUNC(makeitem)
  5335. {
  5336. int nameid,amount,flag = 0;
  5337. int x,y,m;
  5338. const char *mapname;
  5339. struct item item_tmp;
  5340. struct script_data *data;
  5341. data=script_getdata(st,2);
  5342. get_val(st,data);
  5343. if( data_isstring(data) ){
  5344. const char *name=conv_str(st,data);
  5345. struct item_data *item_data = itemdb_searchname(name);
  5346. if( item_data )
  5347. nameid=item_data->nameid;
  5348. else
  5349. nameid=UNKNOWN_ITEM_ID;
  5350. }else
  5351. nameid=conv_num(st,data);
  5352. amount=script_getnum(st,3);
  5353. mapname =script_getstr(st,4);
  5354. x =script_getnum(st,5);
  5355. y =script_getnum(st,6);
  5356. if(strcmp(mapname,"this")==0)
  5357. {
  5358. TBL_PC *sd;
  5359. sd = script_rid2sd(st);
  5360. if (!sd) return 0; //Failed...
  5361. m=sd->bl.m;
  5362. } else
  5363. m=map_mapname2mapid(mapname);
  5364. if(nameid<0) { // ランダム
  5365. nameid=itemdb_searchrandomid(-nameid);
  5366. flag = 1;
  5367. }
  5368. if(nameid > 0) {
  5369. memset(&item_tmp,0,sizeof(item_tmp));
  5370. item_tmp.nameid=nameid;
  5371. if(!flag)
  5372. item_tmp.identify=1;
  5373. else
  5374. item_tmp.identify=itemdb_isidentified(nameid);
  5375. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5376. }
  5377. return 0;
  5378. }
  5379. /// Counts / deletes the current item given by idx.
  5380. /// Used by buildin_delitem_search
  5381. /// Relies on all input data being already fully valid.
  5382. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5383. {
  5384. int delamount;
  5385. struct item* inv = &sd->status.inventory[idx];
  5386. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5387. if( delete_items )
  5388. {
  5389. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5390. {// delete associated pet
  5391. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5392. }
  5393. //Logs items, got from (N)PC scripts [Lupus]
  5394. if( log_config.enable_logs&0x40 )
  5395. {
  5396. log_pick_pc(sd, "N", inv->nameid, -delamount, inv);
  5397. }
  5398. //Logs
  5399. pc_delitem(sd, idx, delamount, 0, 0);
  5400. }
  5401. amount[0]-= delamount;
  5402. }
  5403. /// Searches for item(s) and checks, if there is enough of them.
  5404. /// Used by delitem and delitem2
  5405. /// Relies on all input data being already fully valid.
  5406. /// @param exact_match will also match item attributes and cards, not just name id
  5407. /// @return true when all items could be deleted, false when there were not enough items to delete
  5408. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5409. {
  5410. bool delete_items = false;
  5411. int i, amount, important;
  5412. struct item* inv;
  5413. // prefer always non-equipped items
  5414. it->equip = 0;
  5415. // when searching for nameid only, prefer additionally
  5416. if( !exact_match )
  5417. {
  5418. // non-refined items
  5419. it->refine = 0;
  5420. // card-less items
  5421. memset(it->card, 0, sizeof(it->card));
  5422. }
  5423. for(;;)
  5424. {
  5425. amount = it->amount;
  5426. important = 0;
  5427. // 1st pass -- less important items / exact match
  5428. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5429. {
  5430. inv = &sd->status.inventory[i];
  5431. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5432. {// wrong/invalid item
  5433. continue;
  5434. }
  5435. if( inv->equip != it->equip || inv->refine != it->refine )
  5436. {// not matching attributes
  5437. important++;
  5438. continue;
  5439. }
  5440. if( exact_match )
  5441. {
  5442. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5443. {// not matching exact attributes
  5444. continue;
  5445. }
  5446. }
  5447. else
  5448. {
  5449. if( sd->inventory_data[i]->type == IT_PETEGG )
  5450. {
  5451. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5452. {// pet which cannot be deleted
  5453. continue;
  5454. }
  5455. }
  5456. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  5457. {// named/carded item
  5458. important++;
  5459. continue;
  5460. }
  5461. }
  5462. // count / delete item
  5463. buildin_delitem_delete(sd, i, &amount, delete_items);
  5464. }
  5465. // 2nd pass -- any matching item
  5466. if( amount == 0 || important == 0 )
  5467. {// either everything was already consumed or no items were skipped
  5468. ;
  5469. }
  5470. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5471. {
  5472. inv = &sd->status.inventory[i];
  5473. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5474. {// wrong/invalid item
  5475. continue;
  5476. }
  5477. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  5478. {// pet which cannot be deleted
  5479. continue;
  5480. }
  5481. if( exact_match )
  5482. {
  5483. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5484. {// not matching attributes
  5485. continue;
  5486. }
  5487. }
  5488. // count / delete item
  5489. buildin_delitem_delete(sd, i, &amount, delete_items);
  5490. }
  5491. if( amount )
  5492. {// not enough items
  5493. return false;
  5494. }
  5495. else if( delete_items )
  5496. {// we are done with the work
  5497. return true;
  5498. }
  5499. else
  5500. {// get rid of the items now
  5501. delete_items = true;
  5502. }
  5503. }
  5504. }
  5505. /// Deletes items from the target/attached player.
  5506. /// Prioritizes ordinary items.
  5507. ///
  5508. /// delitem <item id>,<amount>{,<account id>}
  5509. /// delitem "<item name>",<amount>{,<account id>}
  5510. BUILDIN_FUNC(delitem)
  5511. {
  5512. TBL_PC *sd;
  5513. struct item it;
  5514. struct script_data *data;
  5515. if( script_hasdata(st,4) )
  5516. {
  5517. int account_id = script_getnum(st,4);
  5518. sd = map_id2sd(account_id); // <account id>
  5519. if( sd == NULL )
  5520. {
  5521. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5522. st->state = END;
  5523. return 1;
  5524. }
  5525. }
  5526. else
  5527. {
  5528. sd = script_rid2sd(st);// attached player
  5529. if( sd == NULL )
  5530. return 0;
  5531. }
  5532. data = script_getdata(st,2);
  5533. get_val(st,data);
  5534. if( data_isstring(data) )
  5535. {
  5536. const char* item_name = conv_str(st,data);
  5537. struct item_data* id = itemdb_searchname(item_name);
  5538. if( id == NULL )
  5539. {
  5540. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5541. st->state = END;
  5542. return 1;
  5543. }
  5544. it.nameid = id->nameid;// "<item name>"
  5545. }
  5546. else
  5547. {
  5548. it.nameid = conv_num(st,data);// <item id>
  5549. if( !itemdb_exists( it.nameid ) )
  5550. {
  5551. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5552. st->state = END;
  5553. return 1;
  5554. }
  5555. }
  5556. it.amount=script_getnum(st,3);
  5557. if( it.amount <= 0 )
  5558. return 0;// nothing to do
  5559. if( buildin_delitem_search(sd, &it, false) )
  5560. {// success
  5561. return 0;
  5562. }
  5563. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5564. st->state = END;
  5565. clif_scriptclose(sd, st->oid);
  5566. return 1;
  5567. }
  5568. /// Deletes items from the target/attached player.
  5569. ///
  5570. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5571. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5572. BUILDIN_FUNC(delitem2)
  5573. {
  5574. TBL_PC *sd;
  5575. struct item it;
  5576. struct script_data *data;
  5577. if( script_hasdata(st,11) )
  5578. {
  5579. int account_id = script_getnum(st,11);
  5580. sd = map_id2sd(account_id); // <account id>
  5581. if( sd == NULL )
  5582. {
  5583. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5584. st->state = END;
  5585. return 1;
  5586. }
  5587. }
  5588. else
  5589. {
  5590. sd = script_rid2sd(st);// attached player
  5591. if( sd == NULL )
  5592. return 0;
  5593. }
  5594. data = script_getdata(st,2);
  5595. get_val(st,data);
  5596. if( data_isstring(data) )
  5597. {
  5598. const char* item_name = conv_str(st,data);
  5599. struct item_data* id = itemdb_searchname(item_name);
  5600. if( id == NULL )
  5601. {
  5602. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5603. st->state = END;
  5604. return 1;
  5605. }
  5606. it.nameid = id->nameid;// "<item name>"
  5607. }
  5608. else
  5609. {
  5610. it.nameid = conv_num(st,data);// <item id>
  5611. if( !itemdb_exists( it.nameid ) )
  5612. {
  5613. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5614. st->state = END;
  5615. return 1;
  5616. }
  5617. }
  5618. it.amount=script_getnum(st,3);
  5619. it.identify=script_getnum(st,4);
  5620. it.refine=script_getnum(st,5);
  5621. it.attribute=script_getnum(st,6);
  5622. it.card[0]=(short)script_getnum(st,7);
  5623. it.card[1]=(short)script_getnum(st,8);
  5624. it.card[2]=(short)script_getnum(st,9);
  5625. it.card[3]=(short)script_getnum(st,10);
  5626. if( it.amount <= 0 )
  5627. return 0;// nothing to do
  5628. if( buildin_delitem_search(sd, &it, true) )
  5629. {// success
  5630. return 0;
  5631. }
  5632. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5633. st->state = END;
  5634. clif_scriptclose(sd, st->oid);
  5635. return 1;
  5636. }
  5637. /*==========================================
  5638. * Enables/Disables use of items while in an NPC [Skotlex]
  5639. *------------------------------------------*/
  5640. BUILDIN_FUNC(enableitemuse)
  5641. {
  5642. TBL_PC *sd;
  5643. sd=script_rid2sd(st);
  5644. if (sd)
  5645. sd->npc_item_flag = st->oid;
  5646. return 0;
  5647. }
  5648. BUILDIN_FUNC(disableitemuse)
  5649. {
  5650. TBL_PC *sd;
  5651. sd=script_rid2sd(st);
  5652. if (sd)
  5653. sd->npc_item_flag = 0;
  5654. return 0;
  5655. }
  5656. /*==========================================
  5657. *キャラ関係のパラメータ取得
  5658. *------------------------------------------*/
  5659. BUILDIN_FUNC(readparam)
  5660. {
  5661. int type;
  5662. TBL_PC *sd;
  5663. type=script_getnum(st,2);
  5664. if( script_hasdata(st,3) )
  5665. sd=map_nick2sd(script_getstr(st,3));
  5666. else
  5667. sd=script_rid2sd(st);
  5668. if(sd==NULL){
  5669. script_pushint(st,-1);
  5670. return 0;
  5671. }
  5672. script_pushint(st,pc_readparam(sd,type));
  5673. return 0;
  5674. }
  5675. /*==========================================
  5676. *キャラ関係のID取得
  5677. *------------------------------------------*/
  5678. BUILDIN_FUNC(getcharid)
  5679. {
  5680. int num;
  5681. TBL_PC *sd;
  5682. num = script_getnum(st,2);
  5683. if( script_hasdata(st,3) )
  5684. sd=map_nick2sd(script_getstr(st,3));
  5685. else
  5686. sd=script_rid2sd(st);
  5687. if(sd==NULL){
  5688. script_pushint(st,0); //return 0, according docs
  5689. return 0;
  5690. }
  5691. switch( num ) {
  5692. case 0: script_pushint(st,sd->status.char_id); break;
  5693. case 1: script_pushint(st,sd->status.party_id); break;
  5694. case 2: script_pushint(st,sd->status.guild_id); break;
  5695. case 3: script_pushint(st,sd->status.account_id); break;
  5696. case 4: script_pushint(st,sd->bg_id); break;
  5697. default:
  5698. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5699. script_pushint(st,0);
  5700. break;
  5701. }
  5702. return 0;
  5703. }
  5704. /*==========================================
  5705. *指定IDのPT名取得
  5706. *------------------------------------------*/
  5707. BUILDIN_FUNC(getpartyname)
  5708. {
  5709. int party_id;
  5710. struct party_data* p;
  5711. party_id = script_getnum(st,2);
  5712. if( ( p = party_search(party_id) ) != NULL )
  5713. {
  5714. script_pushstrcopy(st,p->party.name);
  5715. }
  5716. else
  5717. {
  5718. script_pushconststr(st,"null");
  5719. }
  5720. return 0;
  5721. }
  5722. /*==========================================
  5723. *指定IDのPT人数とメンバーID取得
  5724. *------------------------------------------*/
  5725. BUILDIN_FUNC(getpartymember)
  5726. {
  5727. struct party_data *p;
  5728. int i,j=0,type=0;
  5729. p=party_search(script_getnum(st,2));
  5730. if( script_hasdata(st,3) )
  5731. type=script_getnum(st,3);
  5732. if(p!=NULL){
  5733. for(i=0;i<MAX_PARTY;i++){
  5734. if(p->party.member[i].account_id){
  5735. switch (type) {
  5736. case 2:
  5737. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  5738. break;
  5739. case 1:
  5740. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  5741. break;
  5742. default:
  5743. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  5744. }
  5745. j++;
  5746. }
  5747. }
  5748. }
  5749. mapreg_setreg(add_str("$@partymembercount"),j);
  5750. return 0;
  5751. }
  5752. /*==========================================
  5753. * Retrieves party leader. if flag is specified,
  5754. * return some of the leader data. Otherwise, return name.
  5755. *------------------------------------------*/
  5756. BUILDIN_FUNC(getpartyleader)
  5757. {
  5758. int party_id, type = 0, i=0;
  5759. struct party_data *p;
  5760. party_id=script_getnum(st,2);
  5761. if( script_hasdata(st,3) )
  5762. type=script_getnum(st,3);
  5763. p=party_search(party_id);
  5764. if (p) //Search leader
  5765. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5766. if (!p || i == MAX_PARTY) { //leader not found
  5767. if (type)
  5768. script_pushint(st,-1);
  5769. else
  5770. script_pushconststr(st,"null");
  5771. return 0;
  5772. }
  5773. switch (type) {
  5774. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5775. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5776. case 3: script_pushint(st,p->party.member[i].class_); break;
  5777. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5778. case 5: script_pushint(st,p->party.member[i].lv); break;
  5779. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5780. }
  5781. return 0;
  5782. }
  5783. /*==========================================
  5784. *指定IDのギルド名取得
  5785. *------------------------------------------*/
  5786. BUILDIN_FUNC(getguildname)
  5787. {
  5788. int guild_id;
  5789. struct guild* g;
  5790. guild_id = script_getnum(st,2);
  5791. if( ( g = guild_search(guild_id) ) != NULL )
  5792. {
  5793. script_pushstrcopy(st,g->name);
  5794. }
  5795. else
  5796. {
  5797. script_pushconststr(st,"null");
  5798. }
  5799. return 0;
  5800. }
  5801. /*==========================================
  5802. *指定IDのGuildMaster名取得
  5803. *------------------------------------------*/
  5804. BUILDIN_FUNC(getguildmaster)
  5805. {
  5806. int guild_id;
  5807. struct guild* g;
  5808. guild_id = script_getnum(st,2);
  5809. if( ( g = guild_search(guild_id) ) != NULL )
  5810. {
  5811. script_pushstrcopy(st,g->member[0].name);
  5812. }
  5813. else
  5814. {
  5815. script_pushconststr(st,"null");
  5816. }
  5817. return 0;
  5818. }
  5819. BUILDIN_FUNC(getguildmasterid)
  5820. {
  5821. int guild_id;
  5822. struct guild* g;
  5823. guild_id = script_getnum(st,2);
  5824. if( ( g = guild_search(guild_id) ) != NULL )
  5825. {
  5826. script_pushint(st,g->member[0].char_id);
  5827. }
  5828. else
  5829. {
  5830. script_pushint(st,0);
  5831. }
  5832. return 0;
  5833. }
  5834. /*==========================================
  5835. * キャラクタの名前
  5836. *------------------------------------------*/
  5837. BUILDIN_FUNC(strcharinfo)
  5838. {
  5839. TBL_PC *sd;
  5840. int num;
  5841. struct guild* g;
  5842. struct party_data* p;
  5843. sd=script_rid2sd(st);
  5844. if (!sd) { //Avoid crashing....
  5845. script_pushconststr(st,"");
  5846. return 0;
  5847. }
  5848. num=script_getnum(st,2);
  5849. switch(num){
  5850. case 0:
  5851. script_pushstrcopy(st,sd->status.name);
  5852. break;
  5853. case 1:
  5854. if( ( p = party_search(sd->status.party_id) ) != NULL )
  5855. {
  5856. script_pushstrcopy(st,p->party.name);
  5857. }
  5858. else
  5859. {
  5860. script_pushconststr(st,"");
  5861. }
  5862. break;
  5863. case 2:
  5864. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  5865. {
  5866. script_pushstrcopy(st,g->name);
  5867. }
  5868. else
  5869. {
  5870. script_pushconststr(st,"");
  5871. }
  5872. break;
  5873. case 3:
  5874. script_pushconststr(st,map[sd->bl.m].name);
  5875. break;
  5876. default:
  5877. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5878. script_pushconststr(st,"");
  5879. break;
  5880. }
  5881. return 0;
  5882. }
  5883. /*==========================================
  5884. * 呼び出し元のNPC情報を取得する
  5885. *------------------------------------------*/
  5886. BUILDIN_FUNC(strnpcinfo)
  5887. {
  5888. TBL_NPC* nd;
  5889. int num;
  5890. char *buf,*name=NULL;
  5891. nd = map_id2nd(st->oid);
  5892. if (!nd) {
  5893. script_pushconststr(st, "");
  5894. return 0;
  5895. }
  5896. num = script_getnum(st,2);
  5897. switch(num){
  5898. case 0: // display name
  5899. name = aStrdup(nd->name);
  5900. break;
  5901. case 1: // visible part of display name
  5902. if((buf = strchr(nd->name,'#')) != NULL)
  5903. {
  5904. name = aStrdup(nd->name);
  5905. name[buf - nd->name] = 0;
  5906. } else // Return the name, there is no '#' present
  5907. name = aStrdup(nd->name);
  5908. break;
  5909. case 2: // # fragment
  5910. if((buf = strchr(nd->name,'#')) != NULL)
  5911. name = aStrdup(buf+1);
  5912. break;
  5913. case 3: // unique name
  5914. name = aStrdup(nd->exname);
  5915. break;
  5916. case 4: // map name
  5917. name = aStrdup(map[nd->bl.m].name);
  5918. break;
  5919. }
  5920. if(name)
  5921. script_pushstr(st, name);
  5922. else
  5923. script_pushconststr(st, "");
  5924. return 0;
  5925. }
  5926. // aegis->athena slot position conversion table
  5927. 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};
  5928. /*==========================================
  5929. * GetEquipID(Pos); Pos: 1-10
  5930. *------------------------------------------*/
  5931. BUILDIN_FUNC(getequipid)
  5932. {
  5933. int i, num;
  5934. TBL_PC* sd;
  5935. struct item_data* item;
  5936. sd = script_rid2sd(st);
  5937. if( sd == NULL )
  5938. return 0;
  5939. num = script_getnum(st,2) - 1;
  5940. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5941. {
  5942. script_pushint(st,-1);
  5943. return 0;
  5944. }
  5945. // get inventory position of item
  5946. i = pc_checkequip(sd,equip[num]);
  5947. if( i < 0 )
  5948. {
  5949. script_pushint(st,-1);
  5950. return 0;
  5951. }
  5952. item = sd->inventory_data[i];
  5953. if( item != 0 )
  5954. script_pushint(st,item->nameid);
  5955. else
  5956. script_pushint(st,0);
  5957. return 0;
  5958. }
  5959. /*==========================================
  5960. * 装備名文字列(精錬メニュー用)
  5961. *------------------------------------------*/
  5962. BUILDIN_FUNC(getequipname)
  5963. {
  5964. int i, num;
  5965. TBL_PC* sd;
  5966. struct item_data* item;
  5967. sd = script_rid2sd(st);
  5968. if( sd == NULL )
  5969. return 0;
  5970. num = script_getnum(st,2) - 1;
  5971. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5972. {
  5973. script_pushconststr(st,"");
  5974. return 0;
  5975. }
  5976. // get inventory position of item
  5977. i = pc_checkequip(sd,equip[num]);
  5978. if( i < 0 )
  5979. {
  5980. script_pushint(st,-1);
  5981. return 0;
  5982. }
  5983. item = sd->inventory_data[i];
  5984. if( item != 0 )
  5985. script_pushstrcopy(st,item->jname);
  5986. else
  5987. script_pushconststr(st,"");
  5988. return 0;
  5989. }
  5990. /*==========================================
  5991. * getbrokenid [Valaris]
  5992. *------------------------------------------*/
  5993. BUILDIN_FUNC(getbrokenid)
  5994. {
  5995. int i,num,id=0,brokencounter=0;
  5996. TBL_PC *sd;
  5997. sd = script_rid2sd(st);
  5998. if( sd == NULL )
  5999. return 0;
  6000. num=script_getnum(st,2);
  6001. for(i=0; i<MAX_INVENTORY; i++) {
  6002. if(sd->status.inventory[i].attribute){
  6003. brokencounter++;
  6004. if(num==brokencounter){
  6005. id=sd->status.inventory[i].nameid;
  6006. break;
  6007. }
  6008. }
  6009. }
  6010. script_pushint(st,id);
  6011. return 0;
  6012. }
  6013. /*==========================================
  6014. * repair [Valaris]
  6015. *------------------------------------------*/
  6016. BUILDIN_FUNC(repair)
  6017. {
  6018. int i,num;
  6019. int repaircounter=0;
  6020. TBL_PC *sd;
  6021. sd = script_rid2sd(st);
  6022. if( sd == NULL )
  6023. return 0;
  6024. num=script_getnum(st,2);
  6025. for(i=0; i<MAX_INVENTORY; i++) {
  6026. if(sd->status.inventory[i].attribute){
  6027. repaircounter++;
  6028. if(num==repaircounter){
  6029. sd->status.inventory[i].attribute=0;
  6030. clif_equiplist(sd);
  6031. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6032. clif_misceffect(&sd->bl, 3);
  6033. break;
  6034. }
  6035. }
  6036. }
  6037. return 0;
  6038. }
  6039. /*==========================================
  6040. * 装備チェック
  6041. *------------------------------------------*/
  6042. BUILDIN_FUNC(getequipisequiped)
  6043. {
  6044. int i=-1,num;
  6045. TBL_PC *sd;
  6046. num=script_getnum(st,2);
  6047. sd = script_rid2sd(st);
  6048. if( sd == NULL )
  6049. return 0;
  6050. if (num > 0 && num <= ARRAYLENGTH(equip))
  6051. i=pc_checkequip(sd,equip[num-1]);
  6052. if(i >= 0)
  6053. script_pushint(st,1);
  6054. else
  6055. script_pushint(st,0);
  6056. return 0;
  6057. }
  6058. /*==========================================
  6059. * 装備品精錬可能チェック
  6060. *------------------------------------------*/
  6061. BUILDIN_FUNC(getequipisenableref)
  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 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6072. script_pushint(st,1);
  6073. else
  6074. script_pushint(st,0);
  6075. return 0;
  6076. }
  6077. /*==========================================
  6078. * 装備品鑑定チェック
  6079. *------------------------------------------*/
  6080. BUILDIN_FUNC(getequipisidentify)
  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)
  6091. script_pushint(st,sd->status.inventory[i].identify);
  6092. else
  6093. script_pushint(st,0);
  6094. return 0;
  6095. }
  6096. /*==========================================
  6097. * 装備品精錬度
  6098. *------------------------------------------*/
  6099. BUILDIN_FUNC(getequiprefinerycnt)
  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].refine);
  6111. else
  6112. script_pushint(st,0);
  6113. return 0;
  6114. }
  6115. /*==========================================
  6116. * 装備品武器LV
  6117. *------------------------------------------*/
  6118. BUILDIN_FUNC(getequipweaponlv)
  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 && sd->inventory_data[i])
  6129. script_pushint(st,sd->inventory_data[i]->wlv);
  6130. else
  6131. script_pushint(st,0);
  6132. return 0;
  6133. }
  6134. /*==========================================
  6135. * 装備品精錬成功率
  6136. *------------------------------------------*/
  6137. BUILDIN_FUNC(getequippercentrefinery)
  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->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6148. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  6149. else
  6150. script_pushint(st,0);
  6151. return 0;
  6152. }
  6153. /*==========================================
  6154. * 精錬成功
  6155. *------------------------------------------*/
  6156. BUILDIN_FUNC(successrefitem)
  6157. {
  6158. int i=-1,num,ep;
  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) {
  6167. ep=sd->status.inventory[i].equip;
  6168. //Logs items, got from (N)PC scripts [Lupus]
  6169. if(log_config.enable_logs&0x40)
  6170. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6171. sd->status.inventory[i].refine++;
  6172. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6173. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6174. clif_delitem(sd,i,1,3);
  6175. //Logs items, got from (N)PC scripts [Lupus]
  6176. if(log_config.enable_logs&0x40)
  6177. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  6178. clif_additem(sd,i,1,0);
  6179. pc_equipitem(sd,i,ep);
  6180. clif_misceffect(&sd->bl,3);
  6181. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6182. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6183. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6184. ){ // Fame point system [DracoRPG]
  6185. switch (sd->inventory_data[i]->wlv){
  6186. case 1:
  6187. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6188. break;
  6189. case 2:
  6190. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6191. break;
  6192. case 3:
  6193. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6194. break;
  6195. }
  6196. }
  6197. }
  6198. return 0;
  6199. }
  6200. /*==========================================
  6201. * 精錬失敗
  6202. *------------------------------------------*/
  6203. BUILDIN_FUNC(failedrefitem)
  6204. {
  6205. int i=-1,num;
  6206. TBL_PC *sd;
  6207. num=script_getnum(st,2);
  6208. sd = script_rid2sd(st);
  6209. if( sd == NULL )
  6210. return 0;
  6211. if (num > 0 && num <= ARRAYLENGTH(equip))
  6212. i=pc_checkequip(sd,equip[num-1]);
  6213. if(i >= 0) {
  6214. //Logs items, got from (N)PC scripts [Lupus]
  6215. if(log_config.enable_logs&0x40)
  6216. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6217. sd->status.inventory[i].refine = 0;
  6218. pc_unequipitem(sd,i,3);
  6219. // 精錬失敗エフェクトのパケット
  6220. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6221. pc_delitem(sd,i,1,0,2);
  6222. // 他の人にも失敗を通知
  6223. clif_misceffect(&sd->bl,2);
  6224. }
  6225. return 0;
  6226. }
  6227. /*==========================================
  6228. *
  6229. *------------------------------------------*/
  6230. BUILDIN_FUNC(statusup)
  6231. {
  6232. int type;
  6233. TBL_PC *sd;
  6234. type=script_getnum(st,2);
  6235. sd = script_rid2sd(st);
  6236. if( sd == NULL )
  6237. return 0;
  6238. pc_statusup(sd,type);
  6239. return 0;
  6240. }
  6241. /*==========================================
  6242. *
  6243. *------------------------------------------*/
  6244. BUILDIN_FUNC(statusup2)
  6245. {
  6246. int type,val;
  6247. TBL_PC *sd;
  6248. type=script_getnum(st,2);
  6249. val=script_getnum(st,3);
  6250. sd = script_rid2sd(st);
  6251. if( sd == NULL )
  6252. return 0;
  6253. pc_statusup2(sd,type,val);
  6254. return 0;
  6255. }
  6256. /// See 'doc/item_bonus.txt'
  6257. ///
  6258. /// bonus <bonus type>,<val1>;
  6259. /// bonus2 <bonus type>,<val1>,<val2>;
  6260. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6261. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6262. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6263. BUILDIN_FUNC(bonus)
  6264. {
  6265. int type;
  6266. int val1;
  6267. int val2 = 0;
  6268. int val3 = 0;
  6269. int val4 = 0;
  6270. int val5 = 0;
  6271. TBL_PC* sd;
  6272. sd = script_rid2sd(st);
  6273. if( sd == NULL )
  6274. return 0; // no player attached
  6275. type = script_getnum(st,2);
  6276. switch( type )
  6277. {
  6278. case SP_AUTOSPELL:
  6279. case SP_AUTOSPELL_WHENHIT:
  6280. case SP_AUTOSPELL_ONSKILL:
  6281. case SP_SKILL_ATK:
  6282. case SP_SKILL_HEAL:
  6283. case SP_SKILL_HEAL2:
  6284. case SP_ADD_SKILL_BLOW:
  6285. case SP_CASTRATE:
  6286. case SP_ADDEFF_ONSKILL:
  6287. // these bonuses support skill names
  6288. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6289. break;
  6290. default:
  6291. val1 = script_getnum(st,3);
  6292. break;
  6293. }
  6294. switch( script_lastdata(st)-2 )
  6295. {
  6296. case 1:
  6297. pc_bonus(sd, type, val1);
  6298. break;
  6299. case 2:
  6300. val2 = script_getnum(st,4);
  6301. pc_bonus2(sd, type, val1, val2);
  6302. break;
  6303. case 3:
  6304. val2 = script_getnum(st,4);
  6305. val3 = script_getnum(st,5);
  6306. pc_bonus3(sd, type, val1, val2, val3);
  6307. break;
  6308. case 4:
  6309. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6310. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6311. else
  6312. val2 = script_getnum(st,4);
  6313. val3 = script_getnum(st,5);
  6314. val4 = script_getnum(st,6);
  6315. pc_bonus4(sd, type, val1, val2, val3, val4);
  6316. break;
  6317. case 5:
  6318. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6319. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6320. else
  6321. val2 = script_getnum(st,4);
  6322. val3 = script_getnum(st,5);
  6323. val4 = script_getnum(st,6);
  6324. val5 = script_getnum(st,7);
  6325. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6326. break;
  6327. default:
  6328. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6329. break;
  6330. }
  6331. return 0;
  6332. }
  6333. BUILDIN_FUNC(autobonus)
  6334. {
  6335. unsigned int dur;
  6336. short rate;
  6337. short atk_type = 0;
  6338. TBL_PC* sd;
  6339. const char *bonus_script, *other_script = NULL;
  6340. sd = script_rid2sd(st);
  6341. if( sd == NULL )
  6342. return 0; // no player attached
  6343. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6344. return 0;
  6345. rate = script_getnum(st,3);
  6346. dur = script_getnum(st,4);
  6347. bonus_script = script_getstr(st,2);
  6348. if( !rate || !dur || !bonus_script )
  6349. return 0;
  6350. if( script_hasdata(st,5) )
  6351. atk_type = script_getnum(st,5);
  6352. if( script_hasdata(st,6) )
  6353. other_script = script_getstr(st,6);
  6354. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6355. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6356. {
  6357. script_add_autobonus(bonus_script);
  6358. if( other_script )
  6359. script_add_autobonus(other_script);
  6360. }
  6361. return 0;
  6362. }
  6363. BUILDIN_FUNC(autobonus2)
  6364. {
  6365. unsigned int dur;
  6366. short rate;
  6367. short atk_type = 0;
  6368. TBL_PC* sd;
  6369. const char *bonus_script, *other_script = NULL;
  6370. sd = script_rid2sd(st);
  6371. if( sd == NULL )
  6372. return 0; // no player attached
  6373. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6374. return 0;
  6375. rate = script_getnum(st,3);
  6376. dur = script_getnum(st,4);
  6377. bonus_script = script_getstr(st,2);
  6378. if( !rate || !dur || !bonus_script )
  6379. return 0;
  6380. if( script_hasdata(st,5) )
  6381. atk_type = script_getnum(st,5);
  6382. if( script_hasdata(st,6) )
  6383. other_script = script_getstr(st,6);
  6384. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6385. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6386. {
  6387. script_add_autobonus(bonus_script);
  6388. if( other_script )
  6389. script_add_autobonus(other_script);
  6390. }
  6391. return 0;
  6392. }
  6393. BUILDIN_FUNC(autobonus3)
  6394. {
  6395. unsigned int dur;
  6396. short rate,atk_type;
  6397. TBL_PC* sd;
  6398. const char *bonus_script, *other_script = NULL;
  6399. sd = script_rid2sd(st);
  6400. if( sd == NULL )
  6401. return 0; // no player attached
  6402. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6403. return 0;
  6404. rate = script_getnum(st,3);
  6405. dur = script_getnum(st,4);
  6406. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6407. bonus_script = script_getstr(st,2);
  6408. if( !rate || !dur || !atk_type || !bonus_script )
  6409. return 0;
  6410. if( script_hasdata(st,6) )
  6411. other_script = script_getstr(st,6);
  6412. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6413. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6414. {
  6415. script_add_autobonus(bonus_script);
  6416. if( other_script )
  6417. script_add_autobonus(other_script);
  6418. }
  6419. return 0;
  6420. }
  6421. /// Changes the level of a player skill.
  6422. /// <flag> defaults to 1
  6423. /// <flag>=0 : set the level of the skill
  6424. /// <flag>=1 : set the temporary level of the skill
  6425. /// <flag>=2 : add to the level of the skill
  6426. ///
  6427. /// skill <skill id>,<level>,<flag>
  6428. /// skill <skill id>,<level>
  6429. /// skill "<skill name>",<level>,<flag>
  6430. /// skill "<skill name>",<level>
  6431. BUILDIN_FUNC(skill)
  6432. {
  6433. int id;
  6434. int level;
  6435. int flag = 1;
  6436. TBL_PC* sd;
  6437. sd = script_rid2sd(st);
  6438. if( sd == NULL )
  6439. return 0;// no player attached, report source
  6440. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6441. level = script_getnum(st,3);
  6442. if( script_hasdata(st,4) )
  6443. flag = script_getnum(st,4);
  6444. pc_skill(sd, id, level, flag);
  6445. return 0;
  6446. }
  6447. /// Changes the level of a player skill.
  6448. /// like skill, but <flag> defaults to 2
  6449. ///
  6450. /// addtoskill <skill id>,<amount>,<flag>
  6451. /// addtoskill <skill id>,<amount>
  6452. /// addtoskill "<skill name>",<amount>,<flag>
  6453. /// addtoskill "<skill name>",<amount>
  6454. ///
  6455. /// @see skill
  6456. BUILDIN_FUNC(addtoskill)
  6457. {
  6458. int id;
  6459. int level;
  6460. int flag = 2;
  6461. TBL_PC* sd;
  6462. sd = script_rid2sd(st);
  6463. if( sd == NULL )
  6464. return 0;// no player attached, report source
  6465. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6466. level = script_getnum(st,3);
  6467. if( script_hasdata(st,4) )
  6468. flag = script_getnum(st,4);
  6469. pc_skill(sd, id, level, flag);
  6470. return 0;
  6471. }
  6472. /// Increases the level of a guild skill.
  6473. ///
  6474. /// guildskill <skill id>,<amount>;
  6475. /// guildskill "<skill name>",<amount>;
  6476. BUILDIN_FUNC(guildskill)
  6477. {
  6478. int id;
  6479. int level;
  6480. TBL_PC* sd;
  6481. int i;
  6482. sd = script_rid2sd(st);
  6483. if( sd == NULL )
  6484. return 0;// no player attached, report source
  6485. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6486. level = script_getnum(st,3);
  6487. for( i=0; i < level; i++ )
  6488. guild_skillup(sd, id);
  6489. return 0;
  6490. }
  6491. /// Returns the level of the player skill.
  6492. ///
  6493. /// getskilllv(<skill id>) -> <level>
  6494. /// getskilllv("<skill name>") -> <level>
  6495. BUILDIN_FUNC(getskilllv)
  6496. {
  6497. int id;
  6498. TBL_PC* sd;
  6499. sd = script_rid2sd(st);
  6500. if( sd == NULL )
  6501. return 0;// no player attached, report source
  6502. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6503. script_pushint(st, pc_checkskill(sd,id));
  6504. return 0;
  6505. }
  6506. /// Returns the level of the guild skill.
  6507. ///
  6508. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6509. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6510. BUILDIN_FUNC(getgdskilllv)
  6511. {
  6512. int guild_id;
  6513. int skill_id;
  6514. struct guild* g;
  6515. guild_id = script_getnum(st,2);
  6516. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6517. g = guild_search(guild_id);
  6518. if( g == NULL )
  6519. script_pushint(st, -1);
  6520. else
  6521. script_pushint(st, guild_checkskill(g,skill_id));
  6522. return 0;
  6523. }
  6524. /// Returns the 'basic_skill_check' setting.
  6525. /// This config determines if the server checks the skill level of NV_BASIC
  6526. /// before allowing the basic actions.
  6527. ///
  6528. /// basicskillcheck() -> <bool>
  6529. BUILDIN_FUNC(basicskillcheck)
  6530. {
  6531. script_pushint(st, battle_config.basic_skill_check);
  6532. return 0;
  6533. }
  6534. /// Returns the GM level of the player.
  6535. ///
  6536. /// getgmlevel() -> <level>
  6537. BUILDIN_FUNC(getgmlevel)
  6538. {
  6539. TBL_PC* sd;
  6540. sd = script_rid2sd(st);
  6541. if( sd == NULL )
  6542. return 0;// no player attached, report source
  6543. script_pushint(st, pc_isGM(sd));
  6544. return 0;
  6545. }
  6546. /// Terminates the execution of this script instance.
  6547. ///
  6548. /// end
  6549. BUILDIN_FUNC(end)
  6550. {
  6551. st->state = END;
  6552. return 0;
  6553. }
  6554. /// Checks if the player has that effect state (option).
  6555. ///
  6556. /// checkoption(<option>) -> <bool>
  6557. BUILDIN_FUNC(checkoption)
  6558. {
  6559. int option;
  6560. TBL_PC* sd;
  6561. sd = script_rid2sd(st);
  6562. if( sd == NULL )
  6563. return 0;// no player attached, report source
  6564. option = script_getnum(st,2);
  6565. if( sd->sc.option&option )
  6566. script_pushint(st, 1);
  6567. else
  6568. script_pushint(st, 0);
  6569. return 0;
  6570. }
  6571. /// Checks if the player is in that body state (opt1).
  6572. ///
  6573. /// checkoption1(<opt1>) -> <bool>
  6574. BUILDIN_FUNC(checkoption1)
  6575. {
  6576. int opt1;
  6577. TBL_PC* sd;
  6578. sd = script_rid2sd(st);
  6579. if( sd == NULL )
  6580. return 0;// no player attached, report source
  6581. opt1 = script_getnum(st,2);
  6582. if( sd->sc.opt1 == opt1 )
  6583. script_pushint(st, 1);
  6584. else
  6585. script_pushint(st, 0);
  6586. return 0;
  6587. }
  6588. /// Checks if the player has that health state (opt2).
  6589. ///
  6590. /// checkoption2(<opt2>) -> <bool>
  6591. BUILDIN_FUNC(checkoption2)
  6592. {
  6593. int opt2;
  6594. TBL_PC* sd;
  6595. sd = script_rid2sd(st);
  6596. if( sd == NULL )
  6597. return 0;// no player attached, report source
  6598. opt2 = script_getnum(st,2);
  6599. if( sd->sc.opt2&opt2 )
  6600. script_pushint(st, 1);
  6601. else
  6602. script_pushint(st, 0);
  6603. return 0;
  6604. }
  6605. /// Changes the effect state (option) of the player.
  6606. /// <flag> defaults to 1
  6607. /// <flag>=0 : removes the option
  6608. /// <flag>=other : adds the option
  6609. ///
  6610. /// setoption <option>,<flag>;
  6611. /// setoption <option>;
  6612. BUILDIN_FUNC(setoption)
  6613. {
  6614. int option;
  6615. int flag = 1;
  6616. TBL_PC* sd;
  6617. sd = script_rid2sd(st);
  6618. if( sd == NULL )
  6619. return 0;// no player attached, report source
  6620. option = script_getnum(st,2);
  6621. if( script_hasdata(st,3) )
  6622. flag = script_getnum(st,3);
  6623. else if( !option ){// Request to remove everything.
  6624. flag = 0;
  6625. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6626. }
  6627. if( flag ){// Add option
  6628. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6629. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6630. pc_setoption(sd, sd->sc.option|option);
  6631. } else// Remove option
  6632. pc_setoption(sd, sd->sc.option&~option);
  6633. return 0;
  6634. }
  6635. /// Returns if the player has a cart.
  6636. ///
  6637. /// checkcart() -> <bool>
  6638. ///
  6639. /// @author Valaris
  6640. BUILDIN_FUNC(checkcart)
  6641. {
  6642. TBL_PC* sd;
  6643. sd = script_rid2sd(st);
  6644. if( sd == NULL )
  6645. return 0;// no player attached, report source
  6646. if( pc_iscarton(sd) )
  6647. script_pushint(st, 1);
  6648. else
  6649. script_pushint(st, 0);
  6650. return 0;
  6651. }
  6652. /// Sets the cart of the player.
  6653. /// <type> defaults to 1
  6654. /// <type>=0 : removes the cart
  6655. /// <type>=1 : Normal cart
  6656. /// <type>=2 : Wooden cart
  6657. /// <type>=3 : Covered cart with flowers and ferns
  6658. /// <type>=4 : Wooden cart with a Panda doll on the back
  6659. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6660. ///
  6661. /// setcart <type>;
  6662. /// setcart;
  6663. BUILDIN_FUNC(setcart)
  6664. {
  6665. int type = 1;
  6666. TBL_PC* sd;
  6667. sd = script_rid2sd(st);
  6668. if( sd == NULL )
  6669. return 0;// no player attached, report source
  6670. if( script_hasdata(st,2) )
  6671. type = script_getnum(st,2);
  6672. pc_setcart(sd, type);
  6673. return 0;
  6674. }
  6675. /// Returns if the player has a falcon.
  6676. ///
  6677. /// checkfalcon() -> <bool>
  6678. ///
  6679. /// @author Valaris
  6680. BUILDIN_FUNC(checkfalcon)
  6681. {
  6682. TBL_PC* sd;
  6683. sd = script_rid2sd(st);
  6684. if( sd == NULL )
  6685. return 0;// no player attached, report source
  6686. if( pc_isfalcon(sd) )
  6687. script_pushint(st, 1);
  6688. else
  6689. script_pushint(st, 0);
  6690. return 0;
  6691. }
  6692. /// Sets if the player has a falcon or not.
  6693. /// <flag> defaults to 1
  6694. ///
  6695. /// setfalcon <flag>;
  6696. /// setfalcon;
  6697. BUILDIN_FUNC(setfalcon)
  6698. {
  6699. int flag = 1;
  6700. TBL_PC* sd;
  6701. sd = script_rid2sd(st);
  6702. if( sd == NULL )
  6703. return 0;// no player attached, report source
  6704. if( script_hasdata(st,2) )
  6705. flag = script_getnum(st,2);
  6706. pc_setfalcon(sd, flag);
  6707. return 0;
  6708. }
  6709. /// Returns if the player is riding.
  6710. ///
  6711. /// checkriding() -> <bool>
  6712. ///
  6713. /// @author Valaris
  6714. BUILDIN_FUNC(checkriding)
  6715. {
  6716. TBL_PC* sd;
  6717. sd = script_rid2sd(st);
  6718. if( sd == NULL )
  6719. return 0;// no player attached, report source
  6720. if( pc_isriding(sd) || sd->sc.option&OPTION_MOUNTING )
  6721. script_pushint(st, 1);
  6722. else
  6723. script_pushint(st, 0);
  6724. return 0;
  6725. }
  6726. /// Sets if the player is riding.
  6727. /// <flag> defaults to 1
  6728. ///
  6729. /// setriding <flag>;
  6730. /// setriding;
  6731. BUILDIN_FUNC(setriding)
  6732. {
  6733. int flag = 1;
  6734. TBL_PC* sd;
  6735. sd = script_rid2sd(st);
  6736. if( sd == NULL )
  6737. return 0;// no player attached, report source
  6738. if( script_hasdata(st,2) )
  6739. flag = script_getnum(st,2);
  6740. pc_setriding(sd, flag);
  6741. return 0;
  6742. }
  6743. /// Sets the save point of the player.
  6744. ///
  6745. /// save "<map name>",<x>,<y>
  6746. /// savepoint "<map name>",<x>,<y>
  6747. BUILDIN_FUNC(savepoint)
  6748. {
  6749. int x;
  6750. int y;
  6751. short map;
  6752. const char* str;
  6753. TBL_PC* sd;
  6754. sd = script_rid2sd(st);
  6755. if( sd == NULL )
  6756. return 0;// no player attached, report source
  6757. str = script_getstr(st, 2);
  6758. x = script_getnum(st,3);
  6759. y = script_getnum(st,4);
  6760. map = mapindex_name2id(str);
  6761. if( map )
  6762. pc_setsavepoint(sd, map, x, y);
  6763. return 0;
  6764. }
  6765. /*==========================================
  6766. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6767. *------------------------------------------*/
  6768. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6769. {
  6770. int type;
  6771. time_t timer;
  6772. struct tm *t;
  6773. type=script_getnum(st,2);
  6774. switch(type){
  6775. case 2:
  6776. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6777. // from the system clock.)
  6778. script_pushint(st,(int)time(NULL));
  6779. break;
  6780. case 1:
  6781. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6782. time(&timer);
  6783. t=localtime(&timer);
  6784. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6785. break;
  6786. case 0:
  6787. default:
  6788. //type 0:(System Ticks)
  6789. script_pushint(st,gettick());
  6790. break;
  6791. }
  6792. return 0;
  6793. }
  6794. /*==========================================
  6795. * GetTime(Type);
  6796. * 1: Sec 2: Min 3: Hour
  6797. * 4: WeekDay 5: MonthDay 6: Month
  6798. * 7: Year
  6799. *------------------------------------------*/
  6800. BUILDIN_FUNC(gettime) /* Asgard Version */
  6801. {
  6802. int type;
  6803. time_t timer;
  6804. struct tm *t;
  6805. type=script_getnum(st,2);
  6806. time(&timer);
  6807. t=localtime(&timer);
  6808. switch(type){
  6809. case 1://Sec(0~59)
  6810. script_pushint(st,t->tm_sec);
  6811. break;
  6812. case 2://Min(0~59)
  6813. script_pushint(st,t->tm_min);
  6814. break;
  6815. case 3://Hour(0~23)
  6816. script_pushint(st,t->tm_hour);
  6817. break;
  6818. case 4://WeekDay(0~6)
  6819. script_pushint(st,t->tm_wday);
  6820. break;
  6821. case 5://MonthDay(01~31)
  6822. script_pushint(st,t->tm_mday);
  6823. break;
  6824. case 6://Month(01~12)
  6825. script_pushint(st,t->tm_mon+1);
  6826. break;
  6827. case 7://Year(20xx)
  6828. script_pushint(st,t->tm_year+1900);
  6829. break;
  6830. case 8://Year Day(01~366)
  6831. script_pushint(st,t->tm_yday+1);
  6832. break;
  6833. default://(format error)
  6834. script_pushint(st,-1);
  6835. break;
  6836. }
  6837. return 0;
  6838. }
  6839. /*==========================================
  6840. * GetTimeStr("TimeFMT", Length);
  6841. *------------------------------------------*/
  6842. BUILDIN_FUNC(gettimestr)
  6843. {
  6844. char *tmpstr;
  6845. const char *fmtstr;
  6846. int maxlen;
  6847. time_t now = time(NULL);
  6848. fmtstr=script_getstr(st,2);
  6849. maxlen=script_getnum(st,3);
  6850. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6851. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6852. tmpstr[maxlen]='\0';
  6853. script_pushstr(st,tmpstr);
  6854. return 0;
  6855. }
  6856. /*==========================================
  6857. * カプラ倉庫を開く
  6858. *------------------------------------------*/
  6859. BUILDIN_FUNC(openstorage)
  6860. {
  6861. TBL_PC* sd;
  6862. sd = script_rid2sd(st);
  6863. if( sd == NULL )
  6864. return 0;
  6865. storage_storageopen(sd);
  6866. return 0;
  6867. }
  6868. BUILDIN_FUNC(guildopenstorage)
  6869. {
  6870. TBL_PC* sd;
  6871. int ret;
  6872. sd = script_rid2sd(st);
  6873. if( sd == NULL )
  6874. return 0;
  6875. ret = storage_guild_storageopen(sd);
  6876. script_pushint(st,ret);
  6877. return 0;
  6878. }
  6879. /*==========================================
  6880. * アイテムによるスキル発動
  6881. *------------------------------------------*/
  6882. /// itemskill <skill id>,<level>
  6883. /// itemskill "<skill name>",<level>
  6884. BUILDIN_FUNC(itemskill)
  6885. {
  6886. int id;
  6887. int lv;
  6888. TBL_PC* sd;
  6889. sd = script_rid2sd(st);
  6890. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6891. return 0;
  6892. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6893. lv = script_getnum(st,3);
  6894. sd->skillitem=id;
  6895. sd->skillitemlv=lv;
  6896. clif_item_skill(sd,id,lv);
  6897. return 0;
  6898. }
  6899. /*==========================================
  6900. * アイテム作成
  6901. *------------------------------------------*/
  6902. BUILDIN_FUNC(produce)
  6903. {
  6904. int trigger;
  6905. TBL_PC* sd;
  6906. sd = script_rid2sd(st);
  6907. if( sd == NULL )
  6908. return 0;
  6909. trigger=script_getnum(st,2);
  6910. clif_skill_produce_mix_list(sd, -1, trigger);
  6911. return 0;
  6912. }
  6913. /*==========================================
  6914. *
  6915. *------------------------------------------*/
  6916. BUILDIN_FUNC(cooking)
  6917. {
  6918. int trigger;
  6919. TBL_PC* sd;
  6920. sd = script_rid2sd(st);
  6921. if( sd == NULL )
  6922. return 0;
  6923. trigger=script_getnum(st,2);
  6924. clif_cooking_list(sd, trigger);
  6925. return 0;
  6926. }
  6927. /*==========================================
  6928. * NPCでペット作る
  6929. *------------------------------------------*/
  6930. BUILDIN_FUNC(makepet)
  6931. {
  6932. TBL_PC* sd;
  6933. int id,pet_id;
  6934. id=script_getnum(st,2);
  6935. sd = script_rid2sd(st);
  6936. if( sd == NULL )
  6937. return 0;
  6938. pet_id = search_petDB_index(id, PET_CLASS);
  6939. if (pet_id < 0)
  6940. pet_id = search_petDB_index(id, PET_EGG);
  6941. if (pet_id >= 0 && sd) {
  6942. sd->catch_target_class = pet_db[pet_id].class_;
  6943. intif_create_pet(
  6944. sd->status.account_id, sd->status.char_id,
  6945. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6946. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6947. 100, 0, 1, pet_db[pet_id].jname);
  6948. }
  6949. return 0;
  6950. }
  6951. /*==========================================
  6952. * NPCで経験値上げる
  6953. *------------------------------------------*/
  6954. BUILDIN_FUNC(getexp)
  6955. {
  6956. TBL_PC* sd;
  6957. int base=0,job=0;
  6958. double bonus;
  6959. sd = script_rid2sd(st);
  6960. if( sd == NULL )
  6961. return 0;
  6962. base=script_getnum(st,2);
  6963. job =script_getnum(st,3);
  6964. if(base<0 || job<0)
  6965. return 0;
  6966. // bonus for npc-given exp
  6967. bonus = battle_config.quest_exp_rate / 100.;
  6968. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6969. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6970. pc_gainexp(sd, NULL, base, job, true);
  6971. return 0;
  6972. }
  6973. /*==========================================
  6974. * Gain guild exp [Celest]
  6975. *------------------------------------------*/
  6976. BUILDIN_FUNC(guildgetexp)
  6977. {
  6978. TBL_PC* sd;
  6979. int exp;
  6980. sd = script_rid2sd(st);
  6981. if( sd == NULL )
  6982. return 0;
  6983. exp = script_getnum(st,2);
  6984. if(exp < 0)
  6985. return 0;
  6986. if(sd && sd->status.guild_id > 0)
  6987. guild_getexp (sd, exp);
  6988. return 0;
  6989. }
  6990. /*==========================================
  6991. * Changes the guild master of a guild [Skotlex]
  6992. *------------------------------------------*/
  6993. BUILDIN_FUNC(guildchangegm)
  6994. {
  6995. TBL_PC *sd;
  6996. int guild_id;
  6997. const char *name;
  6998. guild_id = script_getnum(st,2);
  6999. name = script_getstr(st,3);
  7000. sd=map_nick2sd(name);
  7001. if (!sd)
  7002. script_pushint(st,0);
  7003. else
  7004. script_pushint(st,guild_gm_change(guild_id, sd));
  7005. return 0;
  7006. }
  7007. /*==========================================
  7008. * モンスター発生
  7009. *------------------------------------------*/
  7010. BUILDIN_FUNC(monster)
  7011. {
  7012. const char* mapn = script_getstr(st,2);
  7013. int x = script_getnum(st,3);
  7014. int y = script_getnum(st,4);
  7015. const char* str = script_getstr(st,5);
  7016. int class_ = script_getnum(st,6);
  7017. int amount = script_getnum(st,7);
  7018. const char* event = "";
  7019. struct map_session_data* sd;
  7020. int m;
  7021. if( script_hasdata(st,8) )
  7022. {
  7023. event = script_getstr(st,8);
  7024. check_event(st, event);
  7025. }
  7026. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7027. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7028. return 1;
  7029. }
  7030. sd = map_id2sd(st->rid);
  7031. if( sd && strcmp(mapn,"this") == 0 )
  7032. m = sd->bl.m;
  7033. else
  7034. {
  7035. m = map_mapname2mapid(mapn);
  7036. if( map[m].flag.src4instance && st->instance_id )
  7037. { // Try to redirect to the instance map, not the src map
  7038. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7039. {
  7040. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7041. return 1;
  7042. }
  7043. }
  7044. }
  7045. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  7046. return 0;
  7047. }
  7048. /*==========================================
  7049. * Request List of Monster Drops
  7050. *------------------------------------------*/
  7051. BUILDIN_FUNC(getmobdrops)
  7052. {
  7053. int class_ = script_getnum(st,2);
  7054. int i, j = 0;
  7055. struct mob_db *mob;
  7056. if( !mobdb_checkid(class_) )
  7057. {
  7058. script_pushint(st, 0);
  7059. return 0;
  7060. }
  7061. mob = mob_db(class_);
  7062. for( i = 0; i < MAX_MOB_DROP; i++ )
  7063. {
  7064. if( mob->dropitem[i].nameid < 1 )
  7065. continue;
  7066. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7067. continue;
  7068. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7069. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7070. j++;
  7071. }
  7072. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7073. script_pushint(st, 1);
  7074. return 0;
  7075. }
  7076. /*==========================================
  7077. * モンスター発生
  7078. *------------------------------------------*/
  7079. BUILDIN_FUNC(areamonster)
  7080. {
  7081. const char* mapn = script_getstr(st,2);
  7082. int x0 = script_getnum(st,3);
  7083. int y0 = script_getnum(st,4);
  7084. int x1 = script_getnum(st,5);
  7085. int y1 = script_getnum(st,6);
  7086. const char* str = script_getstr(st,7);
  7087. int class_ = script_getnum(st,8);
  7088. int amount = script_getnum(st,9);
  7089. const char* event = "";
  7090. struct map_session_data* sd;
  7091. int m;
  7092. if( script_hasdata(st,10) )
  7093. {
  7094. event = script_getstr(st,10);
  7095. check_event(st, event);
  7096. }
  7097. sd = map_id2sd(st->rid);
  7098. if( sd && strcmp(mapn,"this") == 0 )
  7099. m = sd->bl.m;
  7100. else
  7101. {
  7102. m = map_mapname2mapid(mapn);
  7103. if( map[m].flag.src4instance && st->instance_id )
  7104. { // Try to redirect to the instance map, not the src map
  7105. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7106. {
  7107. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7108. return 1;
  7109. }
  7110. }
  7111. }
  7112. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  7113. return 0;
  7114. }
  7115. /*==========================================
  7116. * モンスター削除
  7117. *------------------------------------------*/
  7118. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7119. { //same fix but with killmonster instead - stripping events from mobs.
  7120. TBL_MOB* md = (TBL_MOB*)bl;
  7121. char *event=va_arg(ap,char *);
  7122. int allflag=va_arg(ap,int);
  7123. md->state.npc_killmonster = 1;
  7124. if(!allflag){
  7125. if(strcmp(event,md->npc_event)==0)
  7126. status_kill(bl);
  7127. }else{
  7128. if(!md->spawn)
  7129. status_kill(bl);
  7130. }
  7131. md->state.npc_killmonster = 0;
  7132. return 0;
  7133. }
  7134. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7135. {
  7136. TBL_MOB* md = (TBL_MOB*)bl;
  7137. char *event=va_arg(ap,char *);
  7138. int allflag=va_arg(ap,int);
  7139. if(!allflag){
  7140. if(strcmp(event,md->npc_event)==0)
  7141. status_kill(bl);
  7142. }else{
  7143. if(!md->spawn)
  7144. status_kill(bl);
  7145. }
  7146. return 0;
  7147. }
  7148. BUILDIN_FUNC(killmonster)
  7149. {
  7150. const char *mapname,*event;
  7151. int m,allflag=0;
  7152. mapname=script_getstr(st,2);
  7153. event=script_getstr(st,3);
  7154. if(strcmp(event,"All")==0)
  7155. allflag = 1;
  7156. else
  7157. check_event(st, event);
  7158. if( (m=map_mapname2mapid(mapname))<0 )
  7159. return 0;
  7160. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7161. return 0;
  7162. if( script_hasdata(st,4) ) {
  7163. if ( script_getnum(st,4) == 1 ) {
  7164. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7165. return 0;
  7166. }
  7167. }
  7168. map_freeblock_lock();
  7169. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7170. map_freeblock_unlock();
  7171. return 0;
  7172. }
  7173. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7174. { //Strips the event from the mob if it's killed the old method.
  7175. struct mob_data *md;
  7176. md = BL_CAST(BL_MOB, bl);
  7177. if (md->npc_event[0])
  7178. md->npc_event[0] = 0;
  7179. status_kill(bl);
  7180. return 0;
  7181. }
  7182. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7183. {
  7184. status_kill(bl);
  7185. return 0;
  7186. }
  7187. BUILDIN_FUNC(killmonsterall)
  7188. {
  7189. const char *mapname;
  7190. int m;
  7191. mapname=script_getstr(st,2);
  7192. if( (m = map_mapname2mapid(mapname))<0 )
  7193. return 0;
  7194. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7195. return 0;
  7196. if( script_hasdata(st,3) ) {
  7197. if ( script_getnum(st,3) == 1 ) {
  7198. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7199. return 0;
  7200. }
  7201. }
  7202. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7203. return 0;
  7204. }
  7205. /*==========================================
  7206. * Creates a clone of a player.
  7207. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7208. *------------------------------------------*/
  7209. BUILDIN_FUNC(clone)
  7210. {
  7211. TBL_PC *sd, *msd=NULL;
  7212. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7213. unsigned int duration = 0;
  7214. const char *map,*event="";
  7215. map=script_getstr(st,2);
  7216. x=script_getnum(st,3);
  7217. y=script_getnum(st,4);
  7218. event=script_getstr(st,5);
  7219. char_id=script_getnum(st,6);
  7220. if( script_hasdata(st,7) )
  7221. master_id=script_getnum(st,7);
  7222. if( script_hasdata(st,8) )
  7223. mode=script_getnum(st,8);
  7224. if( script_hasdata(st,9) )
  7225. flag=script_getnum(st,9);
  7226. if( script_hasdata(st,10) )
  7227. duration=script_getnum(st,10);
  7228. check_event(st, event);
  7229. m = map_mapname2mapid(map);
  7230. if (m < 0) return 0;
  7231. sd = map_charid2sd(char_id);
  7232. if (master_id) {
  7233. msd = map_charid2sd(master_id);
  7234. if (msd)
  7235. master_id = msd->bl.id;
  7236. else
  7237. master_id = 0;
  7238. }
  7239. if (sd) //Return ID of newly crafted clone.
  7240. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7241. else //Failed to create clone.
  7242. script_pushint(st,0);
  7243. return 0;
  7244. }
  7245. /*==========================================
  7246. * イベント実行
  7247. *------------------------------------------*/
  7248. BUILDIN_FUNC(doevent)
  7249. {
  7250. const char* event = script_getstr(st,2);
  7251. struct map_session_data* sd;
  7252. if( ( sd = script_rid2sd(st) ) == NULL )
  7253. {
  7254. return 0;
  7255. }
  7256. check_event(st, event);
  7257. npc_event(sd, event, 0);
  7258. return 0;
  7259. }
  7260. /*==========================================
  7261. * NPC主体イベント実行
  7262. *------------------------------------------*/
  7263. BUILDIN_FUNC(donpcevent)
  7264. {
  7265. const char* event = script_getstr(st,2);
  7266. check_event(st, event);
  7267. npc_event_do(event);
  7268. return 0;
  7269. }
  7270. /// for Aegis compatibility
  7271. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7272. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7273. {
  7274. const char* npc = script_getstr(st,2);
  7275. const char* command = script_getstr(st,3);
  7276. char event[EVENT_NAME_LENGTH];
  7277. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  7278. check_event(st, event);
  7279. npc_event_do(event);
  7280. return 0;
  7281. }
  7282. /*==========================================
  7283. * イベントタイマー追加
  7284. *------------------------------------------*/
  7285. BUILDIN_FUNC(addtimer)
  7286. {
  7287. int tick = script_getnum(st,2);
  7288. const char* event = script_getstr(st, 3);
  7289. TBL_PC* sd;
  7290. check_event(st, event);
  7291. sd = script_rid2sd(st);
  7292. if( sd == NULL )
  7293. return 0;
  7294. pc_addeventtimer(sd,tick,event);
  7295. return 0;
  7296. }
  7297. /*==========================================
  7298. * イベントタイマー削除
  7299. *------------------------------------------*/
  7300. BUILDIN_FUNC(deltimer)
  7301. {
  7302. const char *event;
  7303. TBL_PC* sd;
  7304. event=script_getstr(st, 2);
  7305. sd = script_rid2sd(st);
  7306. if( sd == NULL )
  7307. return 0;
  7308. check_event(st, event);
  7309. pc_deleventtimer(sd,event);
  7310. return 0;
  7311. }
  7312. /*==========================================
  7313. * イベントタイマーのカウント値追加
  7314. *------------------------------------------*/
  7315. BUILDIN_FUNC(addtimercount)
  7316. {
  7317. const char *event;
  7318. int tick;
  7319. TBL_PC* sd;
  7320. event=script_getstr(st, 2);
  7321. tick=script_getnum(st,3);
  7322. sd = script_rid2sd(st);
  7323. if( sd == NULL )
  7324. return 0;
  7325. check_event(st, event);
  7326. pc_addeventtimercount(sd,event,tick);
  7327. return 0;
  7328. }
  7329. /*==========================================
  7330. * NPCタイマー初期化
  7331. *------------------------------------------*/
  7332. BUILDIN_FUNC(initnpctimer)
  7333. {
  7334. struct npc_data *nd;
  7335. int flag = 0;
  7336. if( script_hasdata(st,3) )
  7337. { //Two arguments: NPC name and attach flag.
  7338. nd = npc_name2id(script_getstr(st, 2));
  7339. flag = script_getnum(st,3);
  7340. }
  7341. else if( script_hasdata(st,2) )
  7342. { //Check if argument is numeric (flag) or string (npc name)
  7343. struct script_data *data;
  7344. data = script_getdata(st,2);
  7345. get_val(st,data);
  7346. if( data_isstring(data) ) //NPC name
  7347. nd = npc_name2id(conv_str(st, data));
  7348. else if( data_isint(data) ) //Flag
  7349. {
  7350. nd = (struct npc_data *)map_id2bl(st->oid);
  7351. flag = conv_num(st,data);
  7352. }
  7353. else
  7354. {
  7355. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7356. return 1;
  7357. }
  7358. }
  7359. else
  7360. nd = (struct npc_data *)map_id2bl(st->oid);
  7361. if( !nd )
  7362. return 0;
  7363. if( flag ) //Attach
  7364. {
  7365. TBL_PC* sd = script_rid2sd(st);
  7366. if( sd == NULL )
  7367. return 0;
  7368. nd->u.scr.rid = sd->bl.id;
  7369. }
  7370. npc_settimerevent_tick(nd,0);
  7371. npc_timerevent_start(nd, st->rid);
  7372. return 0;
  7373. }
  7374. /*==========================================
  7375. * NPCタイマー開始
  7376. *------------------------------------------*/
  7377. BUILDIN_FUNC(startnpctimer)
  7378. {
  7379. struct npc_data *nd;
  7380. int flag = 0;
  7381. if( script_hasdata(st,3) )
  7382. { //Two arguments: NPC name and attach flag.
  7383. nd = npc_name2id(script_getstr(st, 2));
  7384. flag = script_getnum(st,3);
  7385. }
  7386. else if( script_hasdata(st,2) )
  7387. { //Check if argument is numeric (flag) or string (npc name)
  7388. struct script_data *data;
  7389. data = script_getdata(st,2);
  7390. get_val(st,data);
  7391. if( data_isstring(data) ) //NPC name
  7392. nd = npc_name2id(conv_str(st, data));
  7393. else if( data_isint(data) ) //Flag
  7394. {
  7395. nd = (struct npc_data *)map_id2bl(st->oid);
  7396. flag = conv_num(st,data);
  7397. }
  7398. else
  7399. {
  7400. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7401. return 1;
  7402. }
  7403. }
  7404. else
  7405. nd=(struct npc_data *)map_id2bl(st->oid);
  7406. if( !nd )
  7407. return 0;
  7408. if( flag ) //Attach
  7409. {
  7410. TBL_PC* sd = script_rid2sd(st);
  7411. if( sd == NULL )
  7412. return 0;
  7413. nd->u.scr.rid = sd->bl.id;
  7414. }
  7415. npc_timerevent_start(nd, st->rid);
  7416. return 0;
  7417. }
  7418. /*==========================================
  7419. * NPCタイマー停止
  7420. *------------------------------------------*/
  7421. BUILDIN_FUNC(stopnpctimer)
  7422. {
  7423. struct npc_data *nd;
  7424. int flag = 0;
  7425. if( script_hasdata(st,3) )
  7426. { //Two arguments: NPC name and attach flag.
  7427. nd = npc_name2id(script_getstr(st, 2));
  7428. flag = script_getnum(st,3);
  7429. }
  7430. else if( script_hasdata(st,2) )
  7431. { //Check if argument is numeric (flag) or string (npc name)
  7432. struct script_data *data;
  7433. data = script_getdata(st,2);
  7434. get_val(st,data);
  7435. if( data_isstring(data) ) //NPC name
  7436. nd = npc_name2id(conv_str(st, data));
  7437. else if( data_isint(data) ) //Flag
  7438. {
  7439. nd = (struct npc_data *)map_id2bl(st->oid);
  7440. flag = conv_num(st,data);
  7441. }
  7442. else
  7443. {
  7444. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7445. return 1;
  7446. }
  7447. }
  7448. else
  7449. nd=(struct npc_data *)map_id2bl(st->oid);
  7450. if( !nd )
  7451. return 0;
  7452. if( flag ) //Detach
  7453. nd->u.scr.rid = 0;
  7454. npc_timerevent_stop(nd);
  7455. return 0;
  7456. }
  7457. /*==========================================
  7458. * NPCタイマー情報所得
  7459. *------------------------------------------*/
  7460. BUILDIN_FUNC(getnpctimer)
  7461. {
  7462. struct npc_data *nd;
  7463. TBL_PC *sd;
  7464. int type = script_getnum(st,2);
  7465. int val = 0;
  7466. if( script_hasdata(st,3) )
  7467. nd = npc_name2id(script_getstr(st,3));
  7468. else
  7469. nd = (struct npc_data *)map_id2bl(st->oid);
  7470. if( !nd || nd->bl.type != BL_NPC )
  7471. {
  7472. script_pushint(st,0);
  7473. ShowError("getnpctimer: Invalid NPC.\n");
  7474. return 1;
  7475. }
  7476. switch( type )
  7477. {
  7478. case 0: val = npc_gettimerevent_tick(nd); break;
  7479. case 1:
  7480. if( nd->u.scr.rid )
  7481. {
  7482. sd = map_id2sd(nd->u.scr.rid);
  7483. if( !sd )
  7484. {
  7485. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7486. break;
  7487. }
  7488. val = (sd->npc_timer_id != INVALID_TIMER);
  7489. }
  7490. else
  7491. val = (nd->u.scr.timerid != INVALID_TIMER);
  7492. break;
  7493. case 2: val = nd->u.scr.timeramount; break;
  7494. }
  7495. script_pushint(st,val);
  7496. return 0;
  7497. }
  7498. /*==========================================
  7499. * NPCタイマー値設定
  7500. *------------------------------------------*/
  7501. BUILDIN_FUNC(setnpctimer)
  7502. {
  7503. int tick;
  7504. struct npc_data *nd;
  7505. tick = script_getnum(st,2);
  7506. if( script_hasdata(st,3) )
  7507. nd = npc_name2id(script_getstr(st,3));
  7508. else
  7509. nd = (struct npc_data *)map_id2bl(st->oid);
  7510. if( !nd || nd->bl.type != BL_NPC )
  7511. {
  7512. script_pushint(st,1);
  7513. ShowError("setnpctimer: Invalid NPC.\n");
  7514. return 1;
  7515. }
  7516. npc_settimerevent_tick(nd,tick);
  7517. script_pushint(st,0);
  7518. return 0;
  7519. }
  7520. /*==========================================
  7521. * attaches the player rid to the timer [Celest]
  7522. *------------------------------------------*/
  7523. BUILDIN_FUNC(attachnpctimer)
  7524. {
  7525. TBL_PC *sd;
  7526. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7527. if( !nd || nd->bl.type != BL_NPC )
  7528. {
  7529. script_pushint(st,1);
  7530. ShowError("setnpctimer: Invalid NPC.\n");
  7531. return 1;
  7532. }
  7533. if( script_hasdata(st,2) )
  7534. sd = map_nick2sd(script_getstr(st,2));
  7535. else
  7536. sd = script_rid2sd(st);
  7537. if( !sd )
  7538. {
  7539. script_pushint(st,1);
  7540. ShowWarning("attachnpctimer: Invalid player.\n");
  7541. return 1;
  7542. }
  7543. nd->u.scr.rid = sd->bl.id;
  7544. script_pushint(st,0);
  7545. return 0;
  7546. }
  7547. /*==========================================
  7548. * detaches a player rid from the timer [Celest]
  7549. *------------------------------------------*/
  7550. BUILDIN_FUNC(detachnpctimer)
  7551. {
  7552. struct npc_data *nd;
  7553. if( script_hasdata(st,2) )
  7554. nd = npc_name2id(script_getstr(st,2));
  7555. else
  7556. nd = (struct npc_data *)map_id2bl(st->oid);
  7557. if( !nd || nd->bl.type != BL_NPC )
  7558. {
  7559. script_pushint(st,1);
  7560. ShowError("detachnpctimer: Invalid NPC.\n");
  7561. return 1;
  7562. }
  7563. nd->u.scr.rid = 0;
  7564. script_pushint(st,0);
  7565. return 0;
  7566. }
  7567. /*==========================================
  7568. * To avoid "player not attached" script errors, this function is provided,
  7569. * it checks if there is a player attached to the current script. [Skotlex]
  7570. * If no, returns 0, if yes, returns the account_id of the attached player.
  7571. *------------------------------------------*/
  7572. BUILDIN_FUNC(playerattached)
  7573. {
  7574. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7575. script_pushint(st,0);
  7576. else
  7577. script_pushint(st,st->rid);
  7578. return 0;
  7579. }
  7580. /*==========================================
  7581. * 天の声アナウンス
  7582. *------------------------------------------*/
  7583. BUILDIN_FUNC(announce)
  7584. {
  7585. const char *mes = script_getstr(st,2);
  7586. int flag = script_getnum(st,3);
  7587. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7588. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7589. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7590. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7591. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7592. if (flag&0x0f) // Broadcast source or broadcast region defined
  7593. {
  7594. send_target target;
  7595. 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
  7596. if (bl == NULL)
  7597. return 0;
  7598. flag &= 0x07;
  7599. target = (flag == 1) ? ALL_SAMEMAP :
  7600. (flag == 2) ? AREA :
  7601. (flag == 3) ? SELF :
  7602. ALL_CLIENT;
  7603. if (fontColor)
  7604. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7605. else
  7606. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7607. }
  7608. else
  7609. {
  7610. if (fontColor)
  7611. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7612. else
  7613. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7614. }
  7615. return 0;
  7616. }
  7617. /*==========================================
  7618. * 天の声アナウンス(特定マップ)
  7619. *------------------------------------------*/
  7620. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7621. {
  7622. char *mes = va_arg(ap, char *);
  7623. int len = va_arg(ap, int);
  7624. int type = va_arg(ap, int);
  7625. char *fontColor = va_arg(ap, char *);
  7626. short fontType = (short)va_arg(ap, int);
  7627. short fontSize = (short)va_arg(ap, int);
  7628. short fontAlign = (short)va_arg(ap, int);
  7629. short fontY = (short)va_arg(ap, int);
  7630. if (fontColor)
  7631. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7632. else
  7633. clif_broadcast(bl, mes, len, type, SELF);
  7634. return 0;
  7635. }
  7636. BUILDIN_FUNC(mapannounce)
  7637. {
  7638. const char *mapname = script_getstr(st,2);
  7639. const char *mes = script_getstr(st,3);
  7640. int flag = script_getnum(st,4);
  7641. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7642. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7643. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7644. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7645. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7646. int m;
  7647. if ((m = map_mapname2mapid(mapname)) < 0)
  7648. return 0;
  7649. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7650. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7651. return 0;
  7652. }
  7653. /*==========================================
  7654. * 天の声アナウンス(特定エリア)
  7655. *------------------------------------------*/
  7656. BUILDIN_FUNC(areaannounce)
  7657. {
  7658. const char *mapname = script_getstr(st,2);
  7659. int x0 = script_getnum(st,3);
  7660. int y0 = script_getnum(st,4);
  7661. int x1 = script_getnum(st,5);
  7662. int y1 = script_getnum(st,6);
  7663. const char *mes = script_getstr(st,7);
  7664. int flag = script_getnum(st,8);
  7665. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7666. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7667. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7668. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7669. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7670. int m;
  7671. if ((m = map_mapname2mapid(mapname)) < 0)
  7672. return 0;
  7673. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7674. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7675. return 0;
  7676. }
  7677. /*==========================================
  7678. * ユーザー数所得
  7679. *------------------------------------------*/
  7680. BUILDIN_FUNC(getusers)
  7681. {
  7682. int flag, val = 0;
  7683. struct map_session_data* sd;
  7684. struct block_list* bl = NULL;
  7685. flag = script_getnum(st,2);
  7686. switch(flag&0x07)
  7687. {
  7688. case 0:
  7689. if(flag&0x8)
  7690. {// npc
  7691. bl = map_id2bl(st->oid);
  7692. }
  7693. else if((sd = script_rid2sd(st))!=NULL)
  7694. {// pc
  7695. bl = &sd->bl;
  7696. }
  7697. if(bl)
  7698. {
  7699. val = map[bl->m].users;
  7700. }
  7701. break;
  7702. case 1:
  7703. val = map_getusers();
  7704. break;
  7705. default:
  7706. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  7707. script_pushint(st,0);
  7708. return 1;
  7709. }
  7710. script_pushint(st,val);
  7711. return 0;
  7712. }
  7713. /*==========================================
  7714. * Works like @WHO - displays all online users names in window
  7715. *------------------------------------------*/
  7716. BUILDIN_FUNC(getusersname)
  7717. {
  7718. TBL_PC *sd, *pl_sd;
  7719. int disp_num=1;
  7720. struct s_mapiterator* iter;
  7721. sd = script_rid2sd(st);
  7722. if (!sd) return 0;
  7723. iter = mapit_getallusers();
  7724. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7725. {
  7726. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7727. continue; // skip hidden GMs
  7728. if((disp_num++)%10==0)
  7729. clif_scriptnext(sd,st->oid);
  7730. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7731. }
  7732. mapit_free(iter);
  7733. return 0;
  7734. }
  7735. /*==========================================
  7736. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7737. *------------------------------------------*/
  7738. BUILDIN_FUNC(getmapguildusers)
  7739. {
  7740. const char *str;
  7741. int m, gid;
  7742. int i=0,c=0;
  7743. struct guild *g = NULL;
  7744. str=script_getstr(st,2);
  7745. gid=script_getnum(st,3);
  7746. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7747. script_pushint(st,-1);
  7748. return 0;
  7749. }
  7750. g = guild_search(gid);
  7751. if (g){
  7752. for(i = 0; i < g->max_member; i++)
  7753. {
  7754. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7755. c++;
  7756. }
  7757. }
  7758. script_pushint(st,c);
  7759. return 0;
  7760. }
  7761. /*==========================================
  7762. * マップ指定ユーザー数所得
  7763. *------------------------------------------*/
  7764. BUILDIN_FUNC(getmapusers)
  7765. {
  7766. const char *str;
  7767. int m;
  7768. str=script_getstr(st,2);
  7769. if( (m=map_mapname2mapid(str))< 0){
  7770. script_pushint(st,-1);
  7771. return 0;
  7772. }
  7773. script_pushint(st,map[m].users);
  7774. return 0;
  7775. }
  7776. /*==========================================
  7777. * エリア指定ユーザー数所得
  7778. *------------------------------------------*/
  7779. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7780. {
  7781. int *users=va_arg(ap,int *);
  7782. (*users)++;
  7783. return 0;
  7784. }
  7785. BUILDIN_FUNC(getareausers)
  7786. {
  7787. const char *str;
  7788. int m,x0,y0,x1,y1,users=0;
  7789. str=script_getstr(st,2);
  7790. x0=script_getnum(st,3);
  7791. y0=script_getnum(st,4);
  7792. x1=script_getnum(st,5);
  7793. y1=script_getnum(st,6);
  7794. if( (m=map_mapname2mapid(str))< 0){
  7795. script_pushint(st,-1);
  7796. return 0;
  7797. }
  7798. map_foreachinarea(buildin_getareausers_sub,
  7799. m,x0,y0,x1,y1,BL_PC,&users);
  7800. script_pushint(st,users);
  7801. return 0;
  7802. }
  7803. /*==========================================
  7804. * エリア指定ドロップアイテム数所得
  7805. *------------------------------------------*/
  7806. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7807. {
  7808. int item=va_arg(ap,int);
  7809. int *amount=va_arg(ap,int *);
  7810. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7811. if(drop->item_data.nameid==item)
  7812. (*amount)+=drop->item_data.amount;
  7813. return 0;
  7814. }
  7815. BUILDIN_FUNC(getareadropitem)
  7816. {
  7817. const char *str;
  7818. int m,x0,y0,x1,y1,item,amount=0;
  7819. struct script_data *data;
  7820. str=script_getstr(st,2);
  7821. x0=script_getnum(st,3);
  7822. y0=script_getnum(st,4);
  7823. x1=script_getnum(st,5);
  7824. y1=script_getnum(st,6);
  7825. data=script_getdata(st,7);
  7826. get_val(st,data);
  7827. if( data_isstring(data) ){
  7828. const char *name=conv_str(st,data);
  7829. struct item_data *item_data = itemdb_searchname(name);
  7830. item=UNKNOWN_ITEM_ID;
  7831. if( item_data )
  7832. item=item_data->nameid;
  7833. }else
  7834. item=conv_num(st,data);
  7835. if( (m=map_mapname2mapid(str))< 0){
  7836. script_pushint(st,-1);
  7837. return 0;
  7838. }
  7839. map_foreachinarea(buildin_getareadropitem_sub,
  7840. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7841. script_pushint(st,amount);
  7842. return 0;
  7843. }
  7844. /*==========================================
  7845. * NPCの有効化
  7846. *------------------------------------------*/
  7847. BUILDIN_FUNC(enablenpc)
  7848. {
  7849. const char *str;
  7850. str=script_getstr(st,2);
  7851. npc_enable(str,1);
  7852. return 0;
  7853. }
  7854. /*==========================================
  7855. * NPCの無効化
  7856. *------------------------------------------*/
  7857. BUILDIN_FUNC(disablenpc)
  7858. {
  7859. const char *str;
  7860. str=script_getstr(st,2);
  7861. npc_enable(str,0);
  7862. return 0;
  7863. }
  7864. /*==========================================
  7865. * 隠れているNPCの表示
  7866. *------------------------------------------*/
  7867. BUILDIN_FUNC(hideoffnpc)
  7868. {
  7869. const char *str;
  7870. str=script_getstr(st,2);
  7871. npc_enable(str,2);
  7872. return 0;
  7873. }
  7874. /*==========================================
  7875. * NPCをハイディング
  7876. *------------------------------------------*/
  7877. BUILDIN_FUNC(hideonnpc)
  7878. {
  7879. const char *str;
  7880. str=script_getstr(st,2);
  7881. npc_enable(str,4);
  7882. return 0;
  7883. }
  7884. /// Starts a status effect on the target unit or on the attached player.
  7885. ///
  7886. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7887. BUILDIN_FUNC(sc_start)
  7888. {
  7889. struct block_list* bl;
  7890. enum sc_type type;
  7891. int tick;
  7892. int val1;
  7893. int val4 = 0;
  7894. type = (sc_type)script_getnum(st,2);
  7895. tick = script_getnum(st,3);
  7896. val1 = script_getnum(st,4);
  7897. if( script_hasdata(st,5) )
  7898. bl = map_id2bl(script_getnum(st,5));
  7899. else
  7900. bl = map_id2bl(st->rid);
  7901. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7902. {// When there isn't a duration specified, try to get it from the skill_db
  7903. tick = skill_get_time(status_sc2skill(type), val1);
  7904. }
  7905. if( potion_flag == 1 && potion_target )
  7906. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7907. bl = map_id2bl(potion_target);
  7908. tick /= 2;// Thrown potions only last half.
  7909. val4 = 1;// Mark that this was a thrown sc_effect
  7910. }
  7911. if( bl )
  7912. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7913. return 0;
  7914. }
  7915. /// Starts a status effect on the target unit or on the attached player.
  7916. ///
  7917. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7918. BUILDIN_FUNC(sc_start2)
  7919. {
  7920. struct block_list* bl;
  7921. enum sc_type type;
  7922. int tick;
  7923. int val1;
  7924. int val4 = 0;
  7925. int rate;
  7926. type = (sc_type)script_getnum(st,2);
  7927. tick = script_getnum(st,3);
  7928. val1 = script_getnum(st,4);
  7929. rate = script_getnum(st,5);
  7930. if( script_hasdata(st,6) )
  7931. bl = map_id2bl(script_getnum(st,6));
  7932. else
  7933. bl = map_id2bl(st->rid);
  7934. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7935. {// When there isn't a duration specified, try to get it from the skill_db
  7936. tick = skill_get_time(status_sc2skill(type), val1);
  7937. }
  7938. if( potion_flag == 1 && potion_target )
  7939. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7940. bl = map_id2bl(potion_target);
  7941. tick /= 2;// Thrown potions only last half.
  7942. val4 = 1;// Mark that this was a thrown sc_effect
  7943. }
  7944. if( bl )
  7945. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7946. return 0;
  7947. }
  7948. /// Starts a status effect on the target unit or on the attached player.
  7949. ///
  7950. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7951. BUILDIN_FUNC(sc_start4)
  7952. {
  7953. struct block_list* bl;
  7954. enum sc_type type;
  7955. int tick;
  7956. int val1;
  7957. int val2;
  7958. int val3;
  7959. int val4;
  7960. type = (sc_type)script_getnum(st,2);
  7961. tick = script_getnum(st,3);
  7962. val1 = script_getnum(st,4);
  7963. val2 = script_getnum(st,5);
  7964. val3 = script_getnum(st,6);
  7965. val4 = script_getnum(st,7);
  7966. if( script_hasdata(st,8) )
  7967. bl = map_id2bl(script_getnum(st,8));
  7968. else
  7969. bl = map_id2bl(st->rid);
  7970. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7971. {// When there isn't a duration specified, try to get it from the skill_db
  7972. tick = skill_get_time(status_sc2skill(type), val1);
  7973. }
  7974. if( potion_flag == 1 && potion_target )
  7975. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7976. bl = map_id2bl(potion_target);
  7977. tick /= 2;// Thrown potions only last half.
  7978. }
  7979. if( bl )
  7980. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7981. return 0;
  7982. }
  7983. /// Ends one or all status effects on the target unit or on the attached player.
  7984. ///
  7985. /// sc_end <effect_id>{,<unit_id>};
  7986. BUILDIN_FUNC(sc_end)
  7987. {
  7988. struct block_list* bl;
  7989. int type;
  7990. type = script_getnum(st,2);
  7991. if( script_hasdata(st,3) )
  7992. bl = map_id2bl(script_getnum(st,3));
  7993. else
  7994. bl = map_id2bl(st->rid);
  7995. if( potion_flag==1 && potion_target )
  7996. {//##TODO how does this work [FlavioJS]
  7997. bl = map_id2bl(potion_target);
  7998. }
  7999. if( !bl ) return 0;
  8000. if( type >= 0 && type < SC_MAX )
  8001. {
  8002. struct status_change *sc = status_get_sc(bl);
  8003. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  8004. if (!sce) return 0;
  8005. //This should help status_change_end force disabling the SC in case it has no limit.
  8006. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8007. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8008. } else
  8009. status_change_clear(bl, 2);// remove all effects
  8010. return 0;
  8011. }
  8012. /*==========================================
  8013. * 状態異常耐性を計算した確率を返す
  8014. *------------------------------------------*/
  8015. BUILDIN_FUNC(getscrate)
  8016. {
  8017. struct block_list *bl;
  8018. int type,rate;
  8019. type=script_getnum(st,2);
  8020. rate=script_getnum(st,3);
  8021. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  8022. bl = map_id2bl(script_getnum(st,4));
  8023. else
  8024. bl = map_id2bl(st->rid);
  8025. if (bl)
  8026. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  8027. script_pushint(st,rate);
  8028. return 0;
  8029. }
  8030. /*==========================================
  8031. *
  8032. *------------------------------------------*/
  8033. BUILDIN_FUNC(debugmes)
  8034. {
  8035. const char *str;
  8036. str=script_getstr(st,2);
  8037. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8038. return 0;
  8039. }
  8040. /*==========================================
  8041. *捕獲アイテム使用
  8042. *------------------------------------------*/
  8043. BUILDIN_FUNC(catchpet)
  8044. {
  8045. int pet_id;
  8046. TBL_PC *sd;
  8047. pet_id= script_getnum(st,2);
  8048. sd=script_rid2sd(st);
  8049. if( sd == NULL )
  8050. return 0;
  8051. pet_catch_process1(sd,pet_id);
  8052. return 0;
  8053. }
  8054. /*==========================================
  8055. * [orn]
  8056. *------------------------------------------*/
  8057. BUILDIN_FUNC(homunculus_evolution)
  8058. {
  8059. TBL_PC *sd;
  8060. sd=script_rid2sd(st);
  8061. if( sd == NULL )
  8062. return 0;
  8063. if(merc_is_hom_active(sd->hd))
  8064. {
  8065. if (sd->hd->homunculus.intimacy > 91000)
  8066. merc_hom_evolution(sd->hd);
  8067. else
  8068. clif_emotion(&sd->hd->bl, E_SWT);
  8069. }
  8070. return 0;
  8071. }
  8072. // [Zephyrus]
  8073. BUILDIN_FUNC(homunculus_shuffle)
  8074. {
  8075. TBL_PC *sd;
  8076. sd=script_rid2sd(st);
  8077. if( sd == NULL )
  8078. return 0;
  8079. if(merc_is_hom_active(sd->hd))
  8080. merc_hom_shuffle(sd->hd);
  8081. return 0;
  8082. }
  8083. //These two functions bring the eA MAPID_* class functionality to scripts.
  8084. BUILDIN_FUNC(eaclass)
  8085. {
  8086. int class_;
  8087. if( script_hasdata(st,2) )
  8088. class_ = script_getnum(st,2);
  8089. else {
  8090. TBL_PC *sd;
  8091. sd=script_rid2sd(st);
  8092. if (!sd) {
  8093. script_pushint(st,-1);
  8094. return 0;
  8095. }
  8096. class_ = sd->status.class_;
  8097. }
  8098. script_pushint(st,pc_jobid2mapid(class_));
  8099. return 0;
  8100. }
  8101. BUILDIN_FUNC(roclass)
  8102. {
  8103. int class_ =script_getnum(st,2);
  8104. int sex;
  8105. if( script_hasdata(st,3) )
  8106. sex = script_getnum(st,3);
  8107. else {
  8108. TBL_PC *sd;
  8109. if (st->rid && (sd=script_rid2sd(st)))
  8110. sex = sd->status.sex;
  8111. else
  8112. sex = 1; //Just use male when not found.
  8113. }
  8114. script_pushint(st,pc_mapid2jobid(class_, sex));
  8115. return 0;
  8116. }
  8117. /*==========================================
  8118. * Tells client to open a hatching window, used for pet incubator
  8119. *------------------------------------------*/
  8120. BUILDIN_FUNC(birthpet)
  8121. {
  8122. TBL_PC *sd;
  8123. sd=script_rid2sd(st);
  8124. if( sd == NULL )
  8125. return 0;
  8126. if( sd->status.pet_id )
  8127. {// do not send egg list, when you already have a pet
  8128. return 0;
  8129. }
  8130. clif_sendegg(sd);
  8131. return 0;
  8132. }
  8133. /*==========================================
  8134. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8135. *------------------------------------------*/
  8136. BUILDIN_FUNC(resetlvl)
  8137. {
  8138. TBL_PC *sd;
  8139. int type=script_getnum(st,2);
  8140. sd=script_rid2sd(st);
  8141. if( sd == NULL )
  8142. return 0;
  8143. pc_resetlvl(sd,type);
  8144. return 0;
  8145. }
  8146. /*==========================================
  8147. * ステータスリセット
  8148. *------------------------------------------*/
  8149. BUILDIN_FUNC(resetstatus)
  8150. {
  8151. TBL_PC *sd;
  8152. sd=script_rid2sd(st);
  8153. pc_resetstate(sd);
  8154. return 0;
  8155. }
  8156. /*==========================================
  8157. * script command resetskill
  8158. *------------------------------------------*/
  8159. BUILDIN_FUNC(resetskill)
  8160. {
  8161. TBL_PC *sd;
  8162. sd=script_rid2sd(st);
  8163. pc_resetskill(sd,1);
  8164. return 0;
  8165. }
  8166. /*==========================================
  8167. * Counts total amount of skill points.
  8168. *------------------------------------------*/
  8169. BUILDIN_FUNC(skillpointcount)
  8170. {
  8171. TBL_PC *sd;
  8172. sd=script_rid2sd(st);
  8173. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8174. return 0;
  8175. }
  8176. /*==========================================
  8177. *
  8178. *------------------------------------------*/
  8179. BUILDIN_FUNC(changebase)
  8180. {
  8181. TBL_PC *sd=NULL;
  8182. int vclass;
  8183. if( script_hasdata(st,3) )
  8184. sd=map_id2sd(script_getnum(st,3));
  8185. else
  8186. sd=script_rid2sd(st);
  8187. if(sd == NULL)
  8188. return 0;
  8189. vclass = script_getnum(st,2);
  8190. if(vclass == JOB_WEDDING)
  8191. {
  8192. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8193. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8194. )
  8195. return 0;
  8196. }
  8197. if(!sd->disguise && vclass != sd->vd.class_) {
  8198. status_set_viewdata(&sd->bl, vclass);
  8199. //Updated client view. Base, Weapon and Cloth Colors.
  8200. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8201. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8202. if (sd->vd.cloth_color)
  8203. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8204. clif_skillinfoblock(sd);
  8205. }
  8206. return 0;
  8207. }
  8208. /*==========================================
  8209. * 性別変換
  8210. *------------------------------------------*/
  8211. BUILDIN_FUNC(changesex)
  8212. {
  8213. TBL_PC *sd = NULL;
  8214. sd = script_rid2sd(st);
  8215. chrif_changesex(sd);
  8216. return 0;
  8217. }
  8218. /*==========================================
  8219. * Works like 'announce' but outputs in the common chat window
  8220. *------------------------------------------*/
  8221. BUILDIN_FUNC(globalmes)
  8222. {
  8223. struct block_list *bl = map_id2bl(st->oid);
  8224. struct npc_data *nd = (struct npc_data *)bl;
  8225. const char *name=NULL,*mes;
  8226. mes=script_getstr(st,2); // メッセージの取得
  8227. if(mes==NULL) return 0;
  8228. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8229. name=script_getstr(st,3);
  8230. } else {
  8231. name=nd->name;
  8232. }
  8233. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8234. return 0;
  8235. }
  8236. /////////////////////////////////////////////////////////////////////
  8237. // NPC waiting room (chat room)
  8238. //
  8239. /// Creates a waiting room (chat room) for this npc.
  8240. ///
  8241. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  8242. BUILDIN_FUNC(waitingroom)
  8243. {
  8244. struct npc_data* nd;
  8245. int pub = 1;
  8246. const char* title = script_getstr(st, 2);
  8247. int limit = script_getnum(st, 3);
  8248. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  8249. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  8250. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  8251. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  8252. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  8253. nd = (struct npc_data *)map_id2bl(st->oid);
  8254. if( nd != NULL )
  8255. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  8256. return 0;
  8257. }
  8258. /// Removes the waiting room of the current or target npc.
  8259. ///
  8260. /// delwaitingroom "<npc_name>";
  8261. /// delwaitingroom;
  8262. BUILDIN_FUNC(delwaitingroom)
  8263. {
  8264. struct npc_data* nd;
  8265. if( script_hasdata(st,2) )
  8266. nd = npc_name2id(script_getstr(st, 2));
  8267. else
  8268. nd = (struct npc_data *)map_id2bl(st->oid);
  8269. if( nd != NULL )
  8270. chat_deletenpcchat(nd);
  8271. return 0;
  8272. }
  8273. /// Kicks all the players from the waiting room of the current or target npc.
  8274. ///
  8275. /// kickwaitingroomall "<npc_name>";
  8276. /// kickwaitingroomall;
  8277. BUILDIN_FUNC(waitingroomkickall)
  8278. {
  8279. struct npc_data* nd;
  8280. struct chat_data* cd;
  8281. if( script_hasdata(st,2) )
  8282. nd = npc_name2id(script_getstr(st,2));
  8283. else
  8284. nd = (struct npc_data *)map_id2bl(st->oid);
  8285. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8286. chat_npckickall(cd);
  8287. return 0;
  8288. }
  8289. /// Enables the waiting room event of the current or target npc.
  8290. ///
  8291. /// enablewaitingroomevent "<npc_name>";
  8292. /// enablewaitingroomevent;
  8293. BUILDIN_FUNC(enablewaitingroomevent)
  8294. {
  8295. struct npc_data* nd;
  8296. struct chat_data* cd;
  8297. if( script_hasdata(st,2) )
  8298. nd = npc_name2id(script_getstr(st, 2));
  8299. else
  8300. nd = (struct npc_data *)map_id2bl(st->oid);
  8301. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8302. chat_enableevent(cd);
  8303. return 0;
  8304. }
  8305. /// Disables the waiting room event of the current or target npc.
  8306. ///
  8307. /// disablewaitingroomevent "<npc_name>";
  8308. /// disablewaitingroomevent;
  8309. BUILDIN_FUNC(disablewaitingroomevent)
  8310. {
  8311. struct npc_data *nd;
  8312. struct chat_data *cd;
  8313. if( script_hasdata(st,2) )
  8314. nd = npc_name2id(script_getstr(st, 2));
  8315. else
  8316. nd = (struct npc_data *)map_id2bl(st->oid);
  8317. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8318. chat_disableevent(cd);
  8319. return 0;
  8320. }
  8321. /// Returns info on the waiting room of the current or target npc.
  8322. /// Returns -1 if the type unknown
  8323. /// <type>=0 : current number of users
  8324. /// <type>=1 : maximum number of users allowed
  8325. /// <type>=2 : the number of users that trigger the event
  8326. /// <type>=3 : if the trigger is disabled
  8327. /// <type>=4 : the title of the waiting room
  8328. /// <type>=5 : the password of the waiting room
  8329. /// <type>=16 : the name of the waiting room event
  8330. /// <type>=32 : if the waiting room is full
  8331. /// <type>=33 : if there are enough users to trigger the event
  8332. ///
  8333. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8334. /// getwaitingroomstate(<type>) -> <info>
  8335. BUILDIN_FUNC(getwaitingroomstate)
  8336. {
  8337. struct npc_data *nd;
  8338. struct chat_data *cd;
  8339. int type;
  8340. type = script_getnum(st,2);
  8341. if( script_hasdata(st,3) )
  8342. nd = npc_name2id(script_getstr(st, 3));
  8343. else
  8344. nd = (struct npc_data *)map_id2bl(st->oid);
  8345. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8346. {
  8347. script_pushint(st, -1);
  8348. return 0;
  8349. }
  8350. switch(type)
  8351. {
  8352. case 0: script_pushint(st, cd->users); break;
  8353. case 1: script_pushint(st, cd->limit); break;
  8354. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8355. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8356. case 4: script_pushstrcopy(st, cd->title); break;
  8357. case 5: script_pushstrcopy(st, cd->pass); break;
  8358. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8359. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8360. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8361. default: script_pushint(st, -1); break;
  8362. }
  8363. return 0;
  8364. }
  8365. /// Warps the trigger or target amount of players to the target map and position.
  8366. /// Players are automatically removed from the waiting room.
  8367. /// Those waiting the longest will get warped first.
  8368. /// The target map can be "Random" for a random position in the current map,
  8369. /// and "SavePoint" for the savepoint map+position.
  8370. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8371. ///
  8372. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8373. /// The total number of teleported players is put into $@warpwaitingpcnum
  8374. ///
  8375. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8376. /// warpwaitingpc "<map name>",<x>,<y>;
  8377. BUILDIN_FUNC(warpwaitingpc)
  8378. {
  8379. int x;
  8380. int y;
  8381. int i;
  8382. int n;
  8383. const char* map_name;
  8384. struct npc_data* nd;
  8385. struct chat_data* cd;
  8386. TBL_PC* sd;
  8387. nd = (struct npc_data *)map_id2bl(st->oid);
  8388. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8389. return 0;
  8390. map_name = script_getstr(st,2);
  8391. x = script_getnum(st,3);
  8392. y = script_getnum(st,4);
  8393. n = cd->trigger&0x7f;
  8394. if( script_hasdata(st,5) )
  8395. n = script_getnum(st,5);
  8396. for( i = 0; i < n && cd->users > 0; i++ )
  8397. {
  8398. sd = cd->usersd[0];
  8399. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  8400. {// can't teleport on this map
  8401. break;
  8402. }
  8403. if( cd->zeny )
  8404. {// fee set
  8405. if( (uint32)sd->status.zeny < cd->zeny )
  8406. {// no zeny to cover set fee
  8407. break;
  8408. }
  8409. pc_payzeny(sd, cd->zeny);
  8410. }
  8411. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  8412. if( strcmp(map_name,"Random") == 0 )
  8413. pc_randomwarp(sd,CLR_TELEPORT);
  8414. else if( strcmp(map_name,"SavePoint") == 0 )
  8415. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  8416. else
  8417. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  8418. }
  8419. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8420. return 0;
  8421. }
  8422. /////////////////////////////////////////////////////////////////////
  8423. // ...
  8424. //
  8425. /// Detaches a character from a script.
  8426. ///
  8427. /// @param st Script state to detach the character from.
  8428. static void script_detach_rid(struct script_state* st)
  8429. {
  8430. if(st->rid)
  8431. {
  8432. script_detach_state(st, false);
  8433. st->rid = 0;
  8434. }
  8435. }
  8436. /*==========================================
  8437. * RIDのアタッチ
  8438. *------------------------------------------*/
  8439. BUILDIN_FUNC(attachrid)
  8440. {
  8441. int rid = script_getnum(st,2);
  8442. struct map_session_data* sd;
  8443. if ((sd = map_id2sd(rid))!=NULL) {
  8444. script_detach_rid(st);
  8445. st->rid = rid;
  8446. script_attach_state(st);
  8447. script_pushint(st,1);
  8448. } else
  8449. script_pushint(st,0);
  8450. return 0;
  8451. }
  8452. /*==========================================
  8453. * RIDのデタッチ
  8454. *------------------------------------------*/
  8455. BUILDIN_FUNC(detachrid)
  8456. {
  8457. script_detach_rid(st);
  8458. return 0;
  8459. }
  8460. /*==========================================
  8461. * 存在チェック
  8462. *------------------------------------------*/
  8463. BUILDIN_FUNC(isloggedin)
  8464. {
  8465. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8466. if (script_hasdata(st,3) && sd &&
  8467. sd->status.char_id != script_getnum(st,3))
  8468. sd = NULL;
  8469. push_val(st->stack,C_INT,sd!=NULL);
  8470. return 0;
  8471. }
  8472. /*==========================================
  8473. *
  8474. *------------------------------------------*/
  8475. BUILDIN_FUNC(setmapflagnosave)
  8476. {
  8477. int m,x,y;
  8478. unsigned short mapindex;
  8479. const char *str,*str2;
  8480. str=script_getstr(st,2);
  8481. str2=script_getstr(st,3);
  8482. x=script_getnum(st,4);
  8483. y=script_getnum(st,5);
  8484. m = map_mapname2mapid(str);
  8485. mapindex = mapindex_name2id(str2);
  8486. if(m >= 0 && mapindex) {
  8487. map[m].flag.nosave=1;
  8488. map[m].save.map=mapindex;
  8489. map[m].save.x=x;
  8490. map[m].save.y=y;
  8491. }
  8492. return 0;
  8493. }
  8494. BUILDIN_FUNC(getmapflag)
  8495. {
  8496. int m,i;
  8497. const char *str;
  8498. str=script_getstr(st,2);
  8499. i=script_getnum(st,3);
  8500. m = map_mapname2mapid(str);
  8501. if(m >= 0) {
  8502. switch(i) {
  8503. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8504. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8505. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  8506. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8507. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8508. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8509. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8510. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8511. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8512. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8513. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8514. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8515. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8516. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8517. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8518. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8519. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8520. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8521. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8522. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8523. /**
  8524. * No longer available, keeping here just in case it's back someday. [Ind]
  8525. **/
  8526. //case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8527. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8528. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8529. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8530. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8531. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8532. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8533. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8534. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8535. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8536. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8537. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8538. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8539. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8540. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8541. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8542. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8543. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8544. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8545. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8546. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8547. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8548. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8549. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  8550. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8551. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8552. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8553. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8554. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8555. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8556. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8557. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  8558. }
  8559. }
  8560. return 0;
  8561. }
  8562. BUILDIN_FUNC(setmapflag)
  8563. {
  8564. int m,i;
  8565. const char *str;
  8566. const char *val=NULL;
  8567. str=script_getstr(st,2);
  8568. i=script_getnum(st,3);
  8569. if(script_hasdata(st,4)){
  8570. val=script_getstr(st,4);
  8571. }
  8572. m = map_mapname2mapid(str);
  8573. if(m >= 0) {
  8574. switch(i) {
  8575. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  8576. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  8577. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  8578. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  8579. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  8580. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  8581. case MF_PVP: map[m].flag.pvp = 1; break;
  8582. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  8583. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  8584. case MF_GVG: map[m].flag.gvg = 1; break;
  8585. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  8586. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  8587. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  8588. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  8589. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  8590. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  8591. case MF_SNOW: map[m].flag.snow = 1; break;
  8592. case MF_FOG: map[m].flag.fog = 1; break;
  8593. case MF_SAKURA: map[m].flag.sakura = 1; break;
  8594. case MF_LEAVES: map[m].flag.leaves = 1; break;
  8595. /**
  8596. * No longer available, keeping here just in case it's back someday. [Ind]
  8597. **/
  8598. //case MF_RAIN: map[m].flag.rain = 1; break;
  8599. case MF_NOGO: map[m].flag.nogo = 1; break;
  8600. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  8601. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  8602. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  8603. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  8604. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  8605. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  8606. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  8607. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  8608. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  8609. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  8610. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  8611. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  8612. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  8613. case MF_RESTRICTED: map[m].flag.restricted = 1; break;
  8614. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8615. case MF_NODROP: map[m].flag.nodrop = 1; break;
  8616. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8617. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8618. case MF_NOVENDING: map[m].flag.novending = 1; break;
  8619. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  8620. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  8621. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  8622. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  8623. case MF_TOWN: map[m].flag.town = 1; break;
  8624. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  8625. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  8626. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  8627. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  8628. case MF_BATTLEGROUND: (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  8629. case MF_RESET: map[m].flag.reset = 1; break;
  8630. }
  8631. }
  8632. return 0;
  8633. }
  8634. BUILDIN_FUNC(removemapflag)
  8635. {
  8636. int m,i;
  8637. const char *str;
  8638. str=script_getstr(st,2);
  8639. i=script_getnum(st,3);
  8640. m = map_mapname2mapid(str);
  8641. if(m >= 0) {
  8642. switch(i) {
  8643. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  8644. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  8645. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  8646. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  8647. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  8648. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  8649. case MF_PVP: map[m].flag.pvp = 0; break;
  8650. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  8651. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  8652. case MF_GVG: map[m].flag.gvg = 0; break;
  8653. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  8654. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  8655. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  8656. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  8657. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  8658. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  8659. case MF_SNOW: map[m].flag.snow = 0; break;
  8660. case MF_FOG: map[m].flag.fog = 0; break;
  8661. case MF_SAKURA: map[m].flag.sakura = 0; break;
  8662. case MF_LEAVES: map[m].flag.leaves = 0; break;
  8663. /**
  8664. * No longer available, keeping here just in case it's back someday. [Ind]
  8665. **/
  8666. //case MF_RAIN: map[m].flag.rain = 0; break;
  8667. case MF_NOGO: map[m].flag.nogo = 0; break;
  8668. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  8669. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  8670. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  8671. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  8672. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  8673. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  8674. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  8675. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  8676. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  8677. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  8678. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  8679. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  8680. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  8681. case MF_RESTRICTED: map[m].flag.restricted = 0; break;
  8682. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  8683. case MF_NODROP: map[m].flag.nodrop = 0; break;
  8684. case MF_JEXP: map[m].jexp = 0; break;
  8685. case MF_BEXP: map[m].bexp = 0; break;
  8686. case MF_NOVENDING: map[m].flag.novending = 0; break;
  8687. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  8688. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  8689. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  8690. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  8691. case MF_TOWN: map[m].flag.town = 0; break;
  8692. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  8693. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  8694. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  8695. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  8696. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  8697. case MF_RESET: map[m].flag.reset = 0; break;
  8698. }
  8699. }
  8700. return 0;
  8701. }
  8702. BUILDIN_FUNC(pvpon)
  8703. {
  8704. int m;
  8705. const char *str;
  8706. TBL_PC* sd = NULL;
  8707. struct s_mapiterator* iter;
  8708. str = script_getstr(st,2);
  8709. m = map_mapname2mapid(str);
  8710. if( m < 0 || map[m].flag.pvp )
  8711. return 0; // nothing to do
  8712. map[m].flag.pvp = 1;
  8713. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  8714. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8715. return 0;
  8716. iter = mapit_getallusers();
  8717. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8718. {
  8719. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  8720. continue; // not applicable
  8721. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8722. sd->pvp_rank = 0;
  8723. sd->pvp_lastusers = 0;
  8724. sd->pvp_point = 5;
  8725. sd->pvp_won = 0;
  8726. sd->pvp_lost = 0;
  8727. }
  8728. mapit_free(iter);
  8729. return 0;
  8730. }
  8731. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8732. {
  8733. TBL_PC* sd = (TBL_PC*)bl;
  8734. clif_pvpset(sd, 0, 0, 2);
  8735. if (sd->pvp_timer != INVALID_TIMER) {
  8736. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8737. sd->pvp_timer = INVALID_TIMER;
  8738. }
  8739. return 0;
  8740. }
  8741. BUILDIN_FUNC(pvpoff)
  8742. {
  8743. int m;
  8744. const char *str;
  8745. str=script_getstr(st,2);
  8746. m = map_mapname2mapid(str);
  8747. if(m < 0 || !map[m].flag.pvp)
  8748. return 0; //fixed Lupus
  8749. map[m].flag.pvp = 0;
  8750. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8751. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8752. return 0;
  8753. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8754. return 0;
  8755. }
  8756. BUILDIN_FUNC(gvgon)
  8757. {
  8758. int m;
  8759. const char *str;
  8760. str=script_getstr(st,2);
  8761. m = map_mapname2mapid(str);
  8762. if(m >= 0 && !map[m].flag.gvg) {
  8763. map[m].flag.gvg = 1;
  8764. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  8765. }
  8766. return 0;
  8767. }
  8768. BUILDIN_FUNC(gvgoff)
  8769. {
  8770. int m;
  8771. const char *str;
  8772. str=script_getstr(st,2);
  8773. m = map_mapname2mapid(str);
  8774. if(m >= 0 && map[m].flag.gvg) {
  8775. map[m].flag.gvg = 0;
  8776. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8777. }
  8778. return 0;
  8779. }
  8780. /*==========================================
  8781. * Shows an emoticon on top of the player/npc
  8782. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  8783. *------------------------------------------*/
  8784. //Optional second parameter added by [Skotlex]
  8785. BUILDIN_FUNC(emotion)
  8786. {
  8787. int type;
  8788. int player=0;
  8789. type=script_getnum(st,2);
  8790. if(type < 0 || type > 100)
  8791. return 0;
  8792. if( script_hasdata(st,3) )
  8793. player=script_getnum(st,3);
  8794. if (player) {
  8795. TBL_PC *sd = NULL;
  8796. if( script_hasdata(st,4) )
  8797. sd = map_nick2sd(script_getstr(st,4));
  8798. else
  8799. sd = script_rid2sd(st);
  8800. if (sd)
  8801. clif_emotion(&sd->bl,type);
  8802. } else
  8803. if( script_hasdata(st,4) )
  8804. {
  8805. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  8806. if(nd)
  8807. clif_emotion(&nd->bl,type);
  8808. }
  8809. else
  8810. clif_emotion(map_id2bl(st->oid),type);
  8811. return 0;
  8812. }
  8813. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8814. {
  8815. int m=va_arg(ap,int);
  8816. int g_id=va_arg(ap,int);
  8817. int flag=va_arg(ap,int);
  8818. if(!sd || sd->bl.m != m)
  8819. return 0;
  8820. if(
  8821. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8822. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8823. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8824. )
  8825. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  8826. return 1;
  8827. }
  8828. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8829. {
  8830. struct mob_data *md=(struct mob_data *)bl;
  8831. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8832. status_kill(bl);
  8833. return 0;
  8834. }
  8835. BUILDIN_FUNC(maprespawnguildid)
  8836. {
  8837. const char *mapname=script_getstr(st,2);
  8838. int g_id=script_getnum(st,3);
  8839. int flag=script_getnum(st,4);
  8840. int m=map_mapname2mapid(mapname);
  8841. if(m == -1)
  8842. return 0;
  8843. //Catch ALL players (in case some are 'between maps' on execution time)
  8844. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8845. if (flag&4) //Remove script mobs.
  8846. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8847. return 0;
  8848. }
  8849. BUILDIN_FUNC(agitstart)
  8850. {
  8851. if(agit_flag==1) return 0; // Agit already Start.
  8852. agit_flag=1;
  8853. guild_agit_start();
  8854. return 0;
  8855. }
  8856. BUILDIN_FUNC(agitend)
  8857. {
  8858. if(agit_flag==0) return 0; // Agit already End.
  8859. agit_flag=0;
  8860. guild_agit_end();
  8861. return 0;
  8862. }
  8863. BUILDIN_FUNC(agitstart2)
  8864. {
  8865. if(agit2_flag==1) return 0; // Agit2 already Start.
  8866. agit2_flag=1;
  8867. guild_agit2_start();
  8868. return 0;
  8869. }
  8870. BUILDIN_FUNC(agitend2)
  8871. {
  8872. if(agit2_flag==0) return 0; // Agit2 already End.
  8873. agit2_flag=0;
  8874. guild_agit2_end();
  8875. return 0;
  8876. }
  8877. /*==========================================
  8878. * Returns whether woe is on or off. // choice script
  8879. *------------------------------------------*/
  8880. BUILDIN_FUNC(agitcheck)
  8881. {
  8882. script_pushint(st,agit_flag);
  8883. return 0;
  8884. }
  8885. /*==========================================
  8886. * Returns whether woese is on or off. // choice script
  8887. *------------------------------------------*/
  8888. BUILDIN_FUNC(agitcheck2)
  8889. {
  8890. script_pushint(st,agit2_flag);
  8891. return 0;
  8892. }
  8893. /// Sets the guild_id of this npc.
  8894. ///
  8895. /// flagemblem <guild_id>;
  8896. BUILDIN_FUNC(flagemblem)
  8897. {
  8898. TBL_NPC* nd;
  8899. int g_id=script_getnum(st,2);
  8900. if(g_id < 0) return 0;
  8901. nd = (TBL_NPC*)map_id2nd(st->oid);
  8902. if( nd == NULL )
  8903. {
  8904. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8905. }
  8906. else if( nd->subtype != SCRIPT )
  8907. {
  8908. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8909. }
  8910. else
  8911. {
  8912. nd->u.scr.guild_id = g_id;
  8913. clif_guild_emblem_area(&nd->bl);
  8914. }
  8915. return 0;
  8916. }
  8917. BUILDIN_FUNC(getcastlename)
  8918. {
  8919. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8920. struct guild_castle* gc = guild_mapname2gc(mapname);
  8921. const char* name = (gc) ? gc->castle_name : "";
  8922. script_pushstrcopy(st,name);
  8923. return 0;
  8924. }
  8925. BUILDIN_FUNC(getcastledata)
  8926. {
  8927. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8928. int index = script_getnum(st,3);
  8929. struct guild_castle* gc = guild_mapname2gc(mapname);
  8930. if(script_hasdata(st,4) && index==0 && gc) {
  8931. const char* event = script_getstr(st,4);
  8932. check_event(st, event);
  8933. guild_addcastleinfoevent(gc->castle_id,17,event);
  8934. }
  8935. if(gc){
  8936. switch(index){
  8937. case 0: {
  8938. int i;
  8939. for(i=1;i<18;i++) // Initialize[AgitInit]
  8940. guild_castledataload(gc->castle_id,i);
  8941. } break;
  8942. case 1:
  8943. script_pushint(st,gc->guild_id); break;
  8944. case 2:
  8945. script_pushint(st,gc->economy); break;
  8946. case 3:
  8947. script_pushint(st,gc->defense); break;
  8948. case 4:
  8949. script_pushint(st,gc->triggerE); break;
  8950. case 5:
  8951. script_pushint(st,gc->triggerD); break;
  8952. case 6:
  8953. script_pushint(st,gc->nextTime); break;
  8954. case 7:
  8955. script_pushint(st,gc->payTime); break;
  8956. case 8:
  8957. script_pushint(st,gc->createTime); break;
  8958. case 9:
  8959. script_pushint(st,gc->visibleC); break;
  8960. case 10:
  8961. case 11:
  8962. case 12:
  8963. case 13:
  8964. case 14:
  8965. case 15:
  8966. case 16:
  8967. case 17:
  8968. script_pushint(st,gc->guardian[index-10].visible); break;
  8969. default:
  8970. script_pushint(st,0); break;
  8971. }
  8972. return 0;
  8973. }
  8974. script_pushint(st,0);
  8975. return 0;
  8976. }
  8977. BUILDIN_FUNC(setcastledata)
  8978. {
  8979. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8980. int index = script_getnum(st,3);
  8981. int value = script_getnum(st,4);
  8982. struct guild_castle* gc = guild_mapname2gc(mapname);
  8983. if(gc) {
  8984. // Save Data byself First
  8985. switch(index){
  8986. case 1:
  8987. gc->guild_id = value; break;
  8988. case 2:
  8989. gc->economy = value; break;
  8990. case 3:
  8991. gc->defense = value; break;
  8992. case 4:
  8993. gc->triggerE = value; break;
  8994. case 5:
  8995. gc->triggerD = value; break;
  8996. case 6:
  8997. gc->nextTime = value; break;
  8998. case 7:
  8999. gc->payTime = value; break;
  9000. case 8:
  9001. gc->createTime = value; break;
  9002. case 9:
  9003. gc->visibleC = value; break;
  9004. case 10:
  9005. case 11:
  9006. case 12:
  9007. case 13:
  9008. case 14:
  9009. case 15:
  9010. case 16:
  9011. case 17:
  9012. gc->guardian[index-10].visible = value; break;
  9013. default:
  9014. return 0;
  9015. }
  9016. guild_castledatasave(gc->castle_id,index,value);
  9017. }
  9018. return 0;
  9019. }
  9020. /* =====================================================================
  9021. * ギルド情報を要求する
  9022. * ---------------------------------------------------------------------*/
  9023. BUILDIN_FUNC(requestguildinfo)
  9024. {
  9025. int guild_id=script_getnum(st,2);
  9026. const char *event=NULL;
  9027. if( script_hasdata(st,3) ){
  9028. event=script_getstr(st,3);
  9029. check_event(st, event);
  9030. }
  9031. if(guild_id>0)
  9032. guild_npc_request_info(guild_id,event);
  9033. return 0;
  9034. }
  9035. /// Returns the number of cards that have been compounded onto the specified equipped item.
  9036. /// getequipcardcnt(<equipment slot>);
  9037. BUILDIN_FUNC(getequipcardcnt)
  9038. {
  9039. int i=-1,j,num;
  9040. TBL_PC *sd;
  9041. int count;
  9042. num=script_getnum(st,2);
  9043. sd=script_rid2sd(st);
  9044. if (num > 0 && num <= ARRAYLENGTH(equip))
  9045. i=pc_checkequip(sd,equip[num-1]);
  9046. if (i < 0 || !sd->inventory_data[i]) {
  9047. script_pushint(st,0);
  9048. return 0;
  9049. }
  9050. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9051. {
  9052. script_pushint(st,0);
  9053. return 0;
  9054. }
  9055. count = 0;
  9056. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  9057. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  9058. count++;
  9059. script_pushint(st,count);
  9060. return 0;
  9061. }
  9062. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  9063. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  9064. /// successremovecards <slot>;
  9065. BUILDIN_FUNC(successremovecards)
  9066. {
  9067. int i=-1,j,c,cardflag=0;
  9068. TBL_PC* sd = script_rid2sd(st);
  9069. int num = script_getnum(st,2);
  9070. if (num > 0 && num <= ARRAYLENGTH(equip))
  9071. i=pc_checkequip(sd,equip[num-1]);
  9072. if (i < 0 || !sd->inventory_data[i]) {
  9073. return 0;
  9074. }
  9075. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9076. return 0;
  9077. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9078. {
  9079. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9080. {// extract this card from the item
  9081. int flag;
  9082. struct item item_tmp;
  9083. cardflag = 1;
  9084. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9085. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9086. item_tmp.attribute=0,item_tmp.expire_time=0;
  9087. for (j = 0; j < MAX_SLOTS; j++)
  9088. item_tmp.card[j]=0;
  9089. //Logs items, got from (N)PC scripts [Lupus]
  9090. if(log_config.enable_logs&0x40)
  9091. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  9092. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  9093. clif_additem(sd,0,0,flag);
  9094. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9095. }
  9096. }
  9097. }
  9098. if(cardflag == 1)
  9099. { // カードを取り除いたアイテム所得
  9100. int flag;
  9101. struct item item_tmp;
  9102. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9103. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9104. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9105. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9106. item_tmp.card[j]=0;
  9107. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9108. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9109. //Logs items, got from (N)PC scripts [Lupus]
  9110. if(log_config.enable_logs&0x40)
  9111. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9112. pc_delitem(sd,i,1,0,3);
  9113. //Logs items, got from (N)PC scripts [Lupus]
  9114. if(log_config.enable_logs&0x40)
  9115. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  9116. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  9117. clif_additem(sd,0,0,flag);
  9118. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9119. }
  9120. clif_misceffect(&sd->bl,3);
  9121. }
  9122. return 0;
  9123. }
  9124. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  9125. /// failedremovecards <slot>, <type>;
  9126. /// <type>=0 : will destroy both the item and the cards.
  9127. /// <type>=1 : will keep the item, but destroy the cards.
  9128. /// <type>=2 : will keep the cards, but destroy the item.
  9129. /// <type>=? : will just display the failure effect.
  9130. BUILDIN_FUNC(failedremovecards)
  9131. {
  9132. int i=-1,j,c,cardflag=0;
  9133. TBL_PC* sd = script_rid2sd(st);
  9134. int num = script_getnum(st,2);
  9135. int typefail = script_getnum(st,3);
  9136. if (num > 0 && num <= ARRAYLENGTH(equip))
  9137. i=pc_checkequip(sd,equip[num-1]);
  9138. if (i < 0 || !sd->inventory_data[i])
  9139. return 0;
  9140. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9141. return 0;
  9142. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9143. {
  9144. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9145. {
  9146. cardflag = 1;
  9147. if(typefail == 2)
  9148. {// add cards to inventory, clear
  9149. int flag;
  9150. struct item item_tmp;
  9151. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9152. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9153. item_tmp.attribute=0,item_tmp.expire_time=0;
  9154. for (j = 0; j < MAX_SLOTS; j++)
  9155. item_tmp.card[j]=0;
  9156. //Logs items, got from (N)PC scripts [Lupus]
  9157. if(log_config.enable_logs&0x40)
  9158. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  9159. if((flag=pc_additem(sd,&item_tmp,1))){
  9160. clif_additem(sd,0,0,flag);
  9161. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9162. }
  9163. }
  9164. }
  9165. }
  9166. if(cardflag == 1)
  9167. {
  9168. if(typefail == 0 || typefail == 2){ // 武具損失
  9169. //Logs items, got from (N)PC scripts [Lupus]
  9170. if(log_config.enable_logs&0x40)
  9171. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9172. pc_delitem(sd,i,1,0,2);
  9173. }
  9174. if(typefail == 1){ // カードのみ損失(武具を返す)
  9175. int flag;
  9176. struct item item_tmp;
  9177. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9178. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9179. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9180. //Logs items, got from (N)PC scripts [Lupus]
  9181. if(log_config.enable_logs&0x40)
  9182. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9183. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9184. item_tmp.card[j]=0;
  9185. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9186. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9187. pc_delitem(sd,i,1,0,2);
  9188. //Logs items, got from (N)PC scripts [Lupus]
  9189. if(log_config.enable_logs&0x40)
  9190. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  9191. if((flag=pc_additem(sd,&item_tmp,1))){
  9192. clif_additem(sd,0,0,flag);
  9193. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9194. }
  9195. }
  9196. clif_misceffect(&sd->bl,2);
  9197. }
  9198. return 0;
  9199. }
  9200. /* ================================================================
  9201. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  9202. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  9203. * improved by [Lance]
  9204. * ================================================================*/
  9205. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  9206. {
  9207. int x,y,m,check_val=0,check_ID=0,i=0;
  9208. struct guild *g = NULL;
  9209. struct party_data *p = NULL;
  9210. const char *str;
  9211. const char *mapname;
  9212. unsigned int index;
  9213. mapname=script_getstr(st,2);
  9214. str=script_getstr(st,3);
  9215. x=script_getnum(st,4);
  9216. y=script_getnum(st,5);
  9217. if(script_hasdata(st,7)){
  9218. check_val=script_getnum(st,6);
  9219. check_ID=script_getnum(st,7);
  9220. }
  9221. if((m=map_mapname2mapid(mapname))< 0)
  9222. return 0;
  9223. if(!(index=mapindex_name2id(str)))
  9224. return 0;
  9225. switch(check_val){
  9226. case 1:
  9227. g = guild_search(check_ID);
  9228. if (g){
  9229. for( i=0; i < g->max_member; i++)
  9230. {
  9231. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9232. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  9233. }
  9234. }
  9235. }
  9236. break;
  9237. case 2:
  9238. p = party_search(check_ID);
  9239. if(p){
  9240. for(i=0;i<MAX_PARTY; i++){
  9241. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9242. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  9243. }
  9244. }
  9245. }
  9246. break;
  9247. default:
  9248. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  9249. break;
  9250. }
  9251. return 0;
  9252. }
  9253. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9254. {
  9255. char *event=va_arg(ap,char *);
  9256. struct mob_data *md = ((struct mob_data *)bl);
  9257. if(strcmp(event,md->npc_event)==0 && md->status.hp > 0)
  9258. return 1;
  9259. return 0;
  9260. }
  9261. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9262. {
  9263. const char *mapname,*event;
  9264. int m;
  9265. mapname=script_getstr(st,2);
  9266. event=script_getstr(st,3);
  9267. check_event(st, event);
  9268. if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9269. script_pushint(st,-1);
  9270. return 0;
  9271. }
  9272. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9273. {
  9274. script_pushint(st,-1);
  9275. return 0;
  9276. }
  9277. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9278. return 0;
  9279. }
  9280. BUILDIN_FUNC(marriage)
  9281. {
  9282. const char *partner=script_getstr(st,2);
  9283. TBL_PC *sd=script_rid2sd(st);
  9284. TBL_PC *p_sd=map_nick2sd(partner);
  9285. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9286. script_pushint(st,0);
  9287. return 0;
  9288. }
  9289. script_pushint(st,1);
  9290. return 0;
  9291. }
  9292. BUILDIN_FUNC(wedding_effect)
  9293. {
  9294. TBL_PC *sd=script_rid2sd(st);
  9295. struct block_list *bl;
  9296. if(sd==NULL) {
  9297. bl=map_id2bl(st->oid);
  9298. } else
  9299. bl=&sd->bl;
  9300. clif_wedding_effect(bl);
  9301. return 0;
  9302. }
  9303. BUILDIN_FUNC(divorce)
  9304. {
  9305. TBL_PC *sd=script_rid2sd(st);
  9306. if(sd==NULL || pc_divorce(sd) < 0){
  9307. script_pushint(st,0);
  9308. return 0;
  9309. }
  9310. script_pushint(st,1);
  9311. return 0;
  9312. }
  9313. BUILDIN_FUNC(ispartneron)
  9314. {
  9315. TBL_PC *sd=script_rid2sd(st);
  9316. if(sd==NULL || !pc_ismarried(sd) ||
  9317. map_charid2sd(sd->status.partner_id) == NULL) {
  9318. script_pushint(st,0);
  9319. return 0;
  9320. }
  9321. script_pushint(st,1);
  9322. return 0;
  9323. }
  9324. BUILDIN_FUNC(getpartnerid)
  9325. {
  9326. TBL_PC *sd=script_rid2sd(st);
  9327. if (sd == NULL) {
  9328. script_pushint(st,0);
  9329. return 0;
  9330. }
  9331. script_pushint(st,sd->status.partner_id);
  9332. return 0;
  9333. }
  9334. BUILDIN_FUNC(getchildid)
  9335. {
  9336. TBL_PC *sd=script_rid2sd(st);
  9337. if (sd == NULL) {
  9338. script_pushint(st,0);
  9339. return 0;
  9340. }
  9341. script_pushint(st,sd->status.child);
  9342. return 0;
  9343. }
  9344. BUILDIN_FUNC(getmotherid)
  9345. {
  9346. TBL_PC *sd=script_rid2sd(st);
  9347. if (sd == NULL) {
  9348. script_pushint(st,0);
  9349. return 0;
  9350. }
  9351. script_pushint(st,sd->status.mother);
  9352. return 0;
  9353. }
  9354. BUILDIN_FUNC(getfatherid)
  9355. {
  9356. TBL_PC *sd=script_rid2sd(st);
  9357. if (sd == NULL) {
  9358. script_pushint(st,0);
  9359. return 0;
  9360. }
  9361. script_pushint(st,sd->status.father);
  9362. return 0;
  9363. }
  9364. BUILDIN_FUNC(warppartner)
  9365. {
  9366. int x,y;
  9367. unsigned short mapindex;
  9368. const char *str;
  9369. TBL_PC *sd=script_rid2sd(st);
  9370. TBL_PC *p_sd=NULL;
  9371. if(sd==NULL || !pc_ismarried(sd) ||
  9372. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9373. script_pushint(st,0);
  9374. return 0;
  9375. }
  9376. str=script_getstr(st,2);
  9377. x=script_getnum(st,3);
  9378. y=script_getnum(st,4);
  9379. mapindex = mapindex_name2id(str);
  9380. if (mapindex) {
  9381. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  9382. script_pushint(st,1);
  9383. } else
  9384. script_pushint(st,0);
  9385. return 0;
  9386. }
  9387. /*================================================
  9388. * Script for Displaying MOB Information [Valaris]
  9389. *------------------------------------------------*/
  9390. BUILDIN_FUNC(strmobinfo)
  9391. {
  9392. int num=script_getnum(st,2);
  9393. int class_=script_getnum(st,3);
  9394. if(!mobdb_checkid(class_))
  9395. {
  9396. script_pushint(st,0);
  9397. return 0;
  9398. }
  9399. switch (num) {
  9400. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9401. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9402. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9403. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9404. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9405. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9406. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9407. default:
  9408. script_pushint(st,0);
  9409. break;
  9410. }
  9411. return 0;
  9412. }
  9413. /*==========================================
  9414. * Summon guardians [Valaris]
  9415. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9416. *------------------------------------------*/
  9417. BUILDIN_FUNC(guardian)
  9418. {
  9419. int class_=0,x=0,y=0,guardian=0;
  9420. const char *str,*map,*evt="";
  9421. struct script_data *data;
  9422. bool has_index = false;
  9423. map =script_getstr(st,2);
  9424. x =script_getnum(st,3);
  9425. y =script_getnum(st,4);
  9426. str =script_getstr(st,5);
  9427. class_=script_getnum(st,6);
  9428. if( script_hasdata(st,8) )
  9429. {// "<event label>",<guardian index>
  9430. evt=script_getstr(st,7);
  9431. guardian=script_getnum(st,8);
  9432. has_index = true;
  9433. } else if( script_hasdata(st,7) ){
  9434. data=script_getdata(st,7);
  9435. get_val(st,data);
  9436. if( data_isstring(data) )
  9437. {// "<event label>"
  9438. evt=script_getstr(st,7);
  9439. } else if( data_isint(data) )
  9440. {// <guardian index>
  9441. guardian=script_getnum(st,7);
  9442. has_index = true;
  9443. } else {
  9444. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9445. script_reportdata(data);
  9446. return 1;
  9447. }
  9448. }
  9449. check_event(st, evt);
  9450. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9451. return 0;
  9452. }
  9453. /*==========================================
  9454. * Invisible Walls [Zephyrus]
  9455. *------------------------------------------*/
  9456. BUILDIN_FUNC(setwall)
  9457. {
  9458. const char *map, *name;
  9459. int x, y, m, size, dir;
  9460. bool shootable;
  9461. map = script_getstr(st,2);
  9462. x = script_getnum(st,3);
  9463. y = script_getnum(st,4);
  9464. size = script_getnum(st,5);
  9465. dir = script_getnum(st,6);
  9466. shootable = script_getnum(st,7);
  9467. name = script_getstr(st,8);
  9468. if( (m = map_mapname2mapid(map)) < 0 )
  9469. return 0; // Invalid Map
  9470. map_iwall_set(m, x, y, size, dir, shootable, name);
  9471. return 0;
  9472. }
  9473. BUILDIN_FUNC(delwall)
  9474. {
  9475. const char *name = script_getstr(st,2);
  9476. map_iwall_remove(name);
  9477. return 0;
  9478. }
  9479. /// Retrieves various information about the specified guardian.
  9480. ///
  9481. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9482. /// type: 0 - whether it is deployed or not
  9483. /// 1 - maximum hp
  9484. /// 2 - current hp
  9485. ///
  9486. BUILDIN_FUNC(guardianinfo)
  9487. {
  9488. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9489. int id = script_getnum(st,3);
  9490. int type = script_getnum(st,4);
  9491. struct guild_castle* gc = guild_mapname2gc(mapname);
  9492. struct mob_data* gd;
  9493. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9494. {
  9495. script_pushint(st,-1);
  9496. return 0;
  9497. }
  9498. if( type == 0 )
  9499. script_pushint(st, gc->guardian[id].visible);
  9500. else
  9501. if( !gc->guardian[id].visible )
  9502. script_pushint(st,-1);
  9503. else
  9504. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9505. script_pushint(st,-1);
  9506. else
  9507. {
  9508. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9509. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9510. else
  9511. script_pushint(st,-1);
  9512. }
  9513. return 0;
  9514. }
  9515. /*==========================================
  9516. * IDからItem名
  9517. *------------------------------------------*/
  9518. BUILDIN_FUNC(getitemname)
  9519. {
  9520. int item_id=0;
  9521. struct item_data *i_data;
  9522. char *item_name;
  9523. struct script_data *data;
  9524. data=script_getdata(st,2);
  9525. get_val(st,data);
  9526. if( data_isstring(data) ){
  9527. const char *name=conv_str(st,data);
  9528. struct item_data *item_data = itemdb_searchname(name);
  9529. if( item_data )
  9530. item_id=item_data->nameid;
  9531. }else
  9532. item_id=conv_num(st,data);
  9533. i_data = itemdb_exists(item_id);
  9534. if (i_data == NULL)
  9535. {
  9536. script_pushconststr(st,"null");
  9537. return 0;
  9538. }
  9539. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9540. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9541. script_pushstr(st,item_name);
  9542. return 0;
  9543. }
  9544. /*==========================================
  9545. * Returns number of slots an item has. [Skotlex]
  9546. *------------------------------------------*/
  9547. BUILDIN_FUNC(getitemslots)
  9548. {
  9549. int item_id;
  9550. struct item_data *i_data;
  9551. item_id=script_getnum(st,2);
  9552. i_data = itemdb_exists(item_id);
  9553. if (i_data)
  9554. script_pushint(st,i_data->slot);
  9555. else
  9556. script_pushint(st,-1);
  9557. return 0;
  9558. }
  9559. /*==========================================
  9560. * Returns some values of an item [Lupus]
  9561. * Price, Weight, etc...
  9562. getiteminfo(itemID,n), where n
  9563. 0 value_buy;
  9564. 1 value_sell;
  9565. 2 type;
  9566. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9567. if = 0, then monsters don't drop it at all (rare or a quest item)
  9568. if = -1, then this item is sold in NPC shops only
  9569. 4 sex;
  9570. 5 equip;
  9571. 6 weight;
  9572. 7 atk;
  9573. 8 def;
  9574. 9 range;
  9575. 10 slot;
  9576. 11 look;
  9577. 12 elv;
  9578. 13 wlv;
  9579. 14 view id
  9580. *------------------------------------------*/
  9581. BUILDIN_FUNC(getiteminfo)
  9582. {
  9583. int item_id,n;
  9584. int *item_arr;
  9585. struct item_data *i_data;
  9586. item_id = script_getnum(st,2);
  9587. n = script_getnum(st,3);
  9588. i_data = itemdb_exists(item_id);
  9589. if (i_data && n>=0 && n<=14) {
  9590. item_arr = (int*)&i_data->value_buy;
  9591. script_pushint(st,item_arr[n]);
  9592. } else
  9593. script_pushint(st,-1);
  9594. return 0;
  9595. }
  9596. /*==========================================
  9597. * Set some values of an item [Lupus]
  9598. * Price, Weight, etc...
  9599. setiteminfo(itemID,n,Value), where n
  9600. 0 value_buy;
  9601. 1 value_sell;
  9602. 2 type;
  9603. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9604. if = 0, then monsters don't drop it at all (rare or a quest item)
  9605. if = -1, then this item is sold in NPC shops only
  9606. 4 sex;
  9607. 5 equip;
  9608. 6 weight;
  9609. 7 atk;
  9610. 8 def;
  9611. 9 range;
  9612. 10 slot;
  9613. 11 look;
  9614. 12 elv;
  9615. 13 wlv;
  9616. 14 view id
  9617. * Returns Value or -1 if the wrong field's been set
  9618. *------------------------------------------*/
  9619. BUILDIN_FUNC(setiteminfo)
  9620. {
  9621. int item_id,n,value;
  9622. int *item_arr;
  9623. struct item_data *i_data;
  9624. item_id = script_getnum(st,2);
  9625. n = script_getnum(st,3);
  9626. value = script_getnum(st,4);
  9627. i_data = itemdb_exists(item_id);
  9628. if (i_data && n>=0 && n<=14) {
  9629. item_arr = (int*)&i_data->value_buy;
  9630. item_arr[n] = value;
  9631. script_pushint(st,value);
  9632. } else
  9633. script_pushint(st,-1);
  9634. return 0;
  9635. }
  9636. /*==========================================
  9637. * Returns value from equipped item slot n [Lupus]
  9638. getequipcardid(num,slot)
  9639. where
  9640. num = eqip position slot
  9641. slot = 0,1,2,3 (Card Slot N)
  9642. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9643. it's useful when you want to check item cards or if it's signed
  9644. Useful for such quests as "Sign this refined item with players name" etc
  9645. Hat[0] +4 -> Player's Hat[0] +4
  9646. *------------------------------------------*/
  9647. BUILDIN_FUNC(getequipcardid)
  9648. {
  9649. int i=-1,num,slot;
  9650. TBL_PC *sd;
  9651. num=script_getnum(st,2);
  9652. slot=script_getnum(st,3);
  9653. sd=script_rid2sd(st);
  9654. if (num > 0 && num <= ARRAYLENGTH(equip))
  9655. i=pc_checkequip(sd,equip[num-1]);
  9656. if(i >= 0 && slot>=0 && slot<4)
  9657. script_pushint(st,sd->status.inventory[i].card[slot]);
  9658. else
  9659. script_pushint(st,0);
  9660. return 0;
  9661. }
  9662. /*==========================================
  9663. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9664. *------------------------------------------*/
  9665. BUILDIN_FUNC(petskillbonus)
  9666. {
  9667. struct pet_data *pd;
  9668. TBL_PC *sd=script_rid2sd(st);
  9669. if(sd==NULL || sd->pd==NULL)
  9670. return 0;
  9671. pd=sd->pd;
  9672. if (pd->bonus)
  9673. { //Clear previous bonus
  9674. if (pd->bonus->timer != INVALID_TIMER)
  9675. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9676. } else //init
  9677. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9678. pd->bonus->type=script_getnum(st,2);
  9679. pd->bonus->val=script_getnum(st,3);
  9680. pd->bonus->duration=script_getnum(st,4);
  9681. pd->bonus->delay=script_getnum(st,5);
  9682. if (pd->state.skillbonus == 1)
  9683. pd->state.skillbonus=0; // waiting state
  9684. // wait for timer to start
  9685. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9686. pd->bonus->timer = INVALID_TIMER;
  9687. else
  9688. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9689. return 0;
  9690. }
  9691. /*==========================================
  9692. * pet looting [Valaris] //Rewritten by [Skotlex]
  9693. *------------------------------------------*/
  9694. BUILDIN_FUNC(petloot)
  9695. {
  9696. int max;
  9697. struct pet_data *pd;
  9698. TBL_PC *sd=script_rid2sd(st);
  9699. if(sd==NULL || sd->pd==NULL)
  9700. return 0;
  9701. max=script_getnum(st,2);
  9702. if(max < 1)
  9703. max = 1; //Let'em loot at least 1 item.
  9704. else if (max > MAX_PETLOOT_SIZE)
  9705. max = MAX_PETLOOT_SIZE;
  9706. pd = sd->pd;
  9707. if (pd->loot != NULL)
  9708. { //Release whatever was there already and reallocate memory
  9709. pet_lootitem_drop(pd, pd->msd);
  9710. aFree(pd->loot->item);
  9711. }
  9712. else
  9713. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9714. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9715. pd->loot->max=max;
  9716. pd->loot->count = 0;
  9717. pd->loot->weight = 0;
  9718. return 0;
  9719. }
  9720. /*==========================================
  9721. * PCの所持品情報読み取り
  9722. *------------------------------------------*/
  9723. BUILDIN_FUNC(getinventorylist)
  9724. {
  9725. TBL_PC *sd=script_rid2sd(st);
  9726. char card_var[NAME_LENGTH];
  9727. int i,j=0,k;
  9728. if(!sd) return 0;
  9729. for(i=0;i<MAX_INVENTORY;i++){
  9730. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9731. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  9732. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  9733. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  9734. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  9735. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  9736. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  9737. for (k = 0; k < MAX_SLOTS; k++)
  9738. {
  9739. sprintf(card_var, "@inventorylist_card%d",k+1);
  9740. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  9741. }
  9742. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  9743. j++;
  9744. }
  9745. }
  9746. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9747. return 0;
  9748. }
  9749. BUILDIN_FUNC(getskilllist)
  9750. {
  9751. TBL_PC *sd=script_rid2sd(st);
  9752. int i,j=0;
  9753. if(!sd) return 0;
  9754. for(i=0;i<MAX_SKILL;i++){
  9755. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9756. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  9757. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  9758. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  9759. j++;
  9760. }
  9761. }
  9762. pc_setreg(sd,add_str("@skilllist_count"),j);
  9763. return 0;
  9764. }
  9765. BUILDIN_FUNC(clearitem)
  9766. {
  9767. TBL_PC *sd=script_rid2sd(st);
  9768. int i;
  9769. if(sd==NULL) return 0;
  9770. for (i=0; i<MAX_INVENTORY; i++) {
  9771. if (sd->status.inventory[i].amount) {
  9772. //Logs items, got from (N)PC scripts [Lupus]
  9773. if(log_config.enable_logs&0x40)
  9774. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9775. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0);
  9776. }
  9777. }
  9778. return 0;
  9779. }
  9780. /*==========================================
  9781. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9782. *------------------------------------------*/
  9783. BUILDIN_FUNC(disguise)
  9784. {
  9785. int id;
  9786. TBL_PC* sd = script_rid2sd(st);
  9787. if (sd == NULL) return 0;
  9788. id = script_getnum(st,2);
  9789. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9790. pc_disguise(sd, id);
  9791. script_pushint(st,id);
  9792. } else
  9793. script_pushint(st,0);
  9794. return 0;
  9795. }
  9796. /*==========================================
  9797. * Undisguise Player (returns 1 if success, 0 on fail)
  9798. *------------------------------------------*/
  9799. BUILDIN_FUNC(undisguise)
  9800. {
  9801. TBL_PC* sd = script_rid2sd(st);
  9802. if (sd == NULL) return 0;
  9803. if (sd->disguise) {
  9804. pc_disguise(sd, 0);
  9805. script_pushint(st,0);
  9806. } else {
  9807. script_pushint(st,1);
  9808. }
  9809. return 0;
  9810. }
  9811. /*==========================================
  9812. * NPCクラスチェンジ
  9813. * classは変わりたいclass
  9814. * typeは通常0なのかな?
  9815. *------------------------------------------*/
  9816. BUILDIN_FUNC(classchange)
  9817. {
  9818. int _class,type;
  9819. struct block_list *bl=map_id2bl(st->oid);
  9820. if(bl==NULL) return 0;
  9821. _class=script_getnum(st,2);
  9822. type=script_getnum(st,3);
  9823. clif_class_change(bl,_class,type);
  9824. return 0;
  9825. }
  9826. /*==========================================
  9827. * NPCから発生するエフェクト
  9828. *------------------------------------------*/
  9829. BUILDIN_FUNC(misceffect)
  9830. {
  9831. int type;
  9832. type=script_getnum(st,2);
  9833. if(st->oid && st->oid != fake_nd->bl.id) {
  9834. struct block_list *bl = map_id2bl(st->oid);
  9835. if (bl)
  9836. clif_specialeffect(bl,type,AREA);
  9837. } else{
  9838. TBL_PC *sd=script_rid2sd(st);
  9839. if(sd)
  9840. clif_specialeffect(&sd->bl,type,AREA);
  9841. }
  9842. return 0;
  9843. }
  9844. /*==========================================
  9845. * Play a BGM on a single client [Rikter/Yommy]
  9846. *------------------------------------------*/
  9847. BUILDIN_FUNC(playBGM)
  9848. {
  9849. const char* name;
  9850. struct map_session_data* sd;
  9851. if( ( sd = script_rid2sd(st) ) != NULL )
  9852. {
  9853. name = script_getstr(st,2);
  9854. clif_playBGM(sd, name);
  9855. }
  9856. return 0;
  9857. }
  9858. static int playBGM_sub(struct block_list* bl,va_list ap)
  9859. {
  9860. const char* name = va_arg(ap,const char*);
  9861. clif_playBGM(BL_CAST(BL_PC, bl), name);
  9862. return 0;
  9863. }
  9864. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  9865. {
  9866. const char* name = va_arg(args, const char*);
  9867. clif_playBGM(sd, name);
  9868. return 0;
  9869. }
  9870. /*==========================================
  9871. * Play a BGM on multiple client [Rikter/Yommy]
  9872. *------------------------------------------*/
  9873. BUILDIN_FUNC(playBGMall)
  9874. {
  9875. const char* name;
  9876. name = script_getstr(st,2);
  9877. if( script_hasdata(st,7) )
  9878. {// specified part of map
  9879. const char* map = script_getstr(st,3);
  9880. int x0 = script_getnum(st,4);
  9881. int y0 = script_getnum(st,5);
  9882. int x1 = script_getnum(st,6);
  9883. int y1 = script_getnum(st,7);
  9884. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  9885. }
  9886. else if( script_hasdata(st,3) )
  9887. {// entire map
  9888. const char* map = script_getstr(st,3);
  9889. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  9890. }
  9891. else
  9892. {// entire server
  9893. map_foreachpc(&playBGM_foreachpc_sub, name);
  9894. }
  9895. return 0;
  9896. }
  9897. /*==========================================
  9898. * サウンドエフェクト
  9899. *------------------------------------------*/
  9900. BUILDIN_FUNC(soundeffect)
  9901. {
  9902. TBL_PC* sd = script_rid2sd(st);
  9903. const char* name = script_getstr(st,2);
  9904. int type = script_getnum(st,3);
  9905. if(sd)
  9906. {
  9907. clif_soundeffect(sd,&sd->bl,name,type);
  9908. }
  9909. return 0;
  9910. }
  9911. int soundeffect_sub(struct block_list* bl,va_list ap)
  9912. {
  9913. char* name = va_arg(ap,char*);
  9914. int type = va_arg(ap,int);
  9915. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9916. return 0;
  9917. }
  9918. /*==========================================
  9919. * Play a sound effect (.wav) on multiple clients
  9920. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9921. *------------------------------------------*/
  9922. BUILDIN_FUNC(soundeffectall)
  9923. {
  9924. struct block_list* bl;
  9925. const char* name;
  9926. int type;
  9927. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9928. if (!bl)
  9929. return 0;
  9930. name = script_getstr(st,2);
  9931. type = script_getnum(st,3);
  9932. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9933. if(!script_hasdata(st,4))
  9934. { // area around
  9935. clif_soundeffectall(bl, name, type, AREA);
  9936. }
  9937. else
  9938. if(!script_hasdata(st,5))
  9939. { // entire map
  9940. const char* map = script_getstr(st,4);
  9941. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9942. }
  9943. else
  9944. if(script_hasdata(st,8))
  9945. { // specified part of map
  9946. const char* map = script_getstr(st,4);
  9947. int x0 = script_getnum(st,5);
  9948. int y0 = script_getnum(st,6);
  9949. int x1 = script_getnum(st,7);
  9950. int y1 = script_getnum(st,8);
  9951. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9952. }
  9953. else
  9954. {
  9955. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9956. }
  9957. return 0;
  9958. }
  9959. /*==========================================
  9960. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9961. *------------------------------------------*/
  9962. BUILDIN_FUNC(petrecovery)
  9963. {
  9964. struct pet_data *pd;
  9965. TBL_PC *sd=script_rid2sd(st);
  9966. if(sd==NULL || sd->pd==NULL)
  9967. return 0;
  9968. pd=sd->pd;
  9969. if (pd->recovery)
  9970. { //Halt previous bonus
  9971. if (pd->recovery->timer != INVALID_TIMER)
  9972. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9973. } else //Init
  9974. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9975. pd->recovery->type = (sc_type)script_getnum(st,2);
  9976. pd->recovery->delay = script_getnum(st,3);
  9977. pd->recovery->timer = INVALID_TIMER;
  9978. return 0;
  9979. }
  9980. /*==========================================
  9981. * pet healing [Valaris] //Rewritten by [Skotlex]
  9982. *------------------------------------------*/
  9983. BUILDIN_FUNC(petheal)
  9984. {
  9985. struct pet_data *pd;
  9986. TBL_PC *sd=script_rid2sd(st);
  9987. if(sd==NULL || sd->pd==NULL)
  9988. return 0;
  9989. pd=sd->pd;
  9990. if (pd->s_skill)
  9991. { //Clear previous skill
  9992. if (pd->s_skill->timer != INVALID_TIMER)
  9993. {
  9994. if (pd->s_skill->id)
  9995. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9996. else
  9997. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9998. }
  9999. } else //init memory
  10000. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10001. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  10002. //Use the lv as the amount to heal
  10003. pd->s_skill->lv=script_getnum(st,2);
  10004. pd->s_skill->delay=script_getnum(st,3);
  10005. pd->s_skill->hp=script_getnum(st,4);
  10006. pd->s_skill->sp=script_getnum(st,5);
  10007. //Use delay as initial offset to avoid skill/heal exploits
  10008. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10009. pd->s_skill->timer = INVALID_TIMER;
  10010. else
  10011. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  10012. return 0;
  10013. }
  10014. /*==========================================
  10015. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  10016. *------------------------------------------*/
  10017. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  10018. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  10019. BUILDIN_FUNC(petskillattack)
  10020. {
  10021. struct pet_data *pd;
  10022. TBL_PC *sd=script_rid2sd(st);
  10023. if(sd==NULL || sd->pd==NULL)
  10024. return 0;
  10025. pd=sd->pd;
  10026. if (pd->a_skill == NULL)
  10027. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10028. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10029. pd->a_skill->lv=script_getnum(st,3);
  10030. pd->a_skill->div_ = 0;
  10031. pd->a_skill->rate=script_getnum(st,4);
  10032. pd->a_skill->bonusrate=script_getnum(st,5);
  10033. return 0;
  10034. }
  10035. /*==========================================
  10036. * pet attack skills [Valaris]
  10037. *------------------------------------------*/
  10038. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  10039. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  10040. BUILDIN_FUNC(petskillattack2)
  10041. {
  10042. struct pet_data *pd;
  10043. TBL_PC *sd=script_rid2sd(st);
  10044. if(sd==NULL || sd->pd==NULL)
  10045. return 0;
  10046. pd=sd->pd;
  10047. if (pd->a_skill == NULL)
  10048. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10049. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10050. pd->a_skill->lv=script_getnum(st,3);
  10051. pd->a_skill->div_ = script_getnum(st,4);
  10052. pd->a_skill->rate=script_getnum(st,5);
  10053. pd->a_skill->bonusrate=script_getnum(st,6);
  10054. return 0;
  10055. }
  10056. /*==========================================
  10057. * pet support skills [Skotlex]
  10058. *------------------------------------------*/
  10059. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  10060. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  10061. BUILDIN_FUNC(petskillsupport)
  10062. {
  10063. struct pet_data *pd;
  10064. TBL_PC *sd=script_rid2sd(st);
  10065. if(sd==NULL || sd->pd==NULL)
  10066. return 0;
  10067. pd=sd->pd;
  10068. if (pd->s_skill)
  10069. { //Clear previous skill
  10070. if (pd->s_skill->timer != INVALID_TIMER)
  10071. {
  10072. if (pd->s_skill->id)
  10073. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10074. else
  10075. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10076. }
  10077. } else //init memory
  10078. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10079. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10080. pd->s_skill->lv=script_getnum(st,3);
  10081. pd->s_skill->delay=script_getnum(st,4);
  10082. pd->s_skill->hp=script_getnum(st,5);
  10083. pd->s_skill->sp=script_getnum(st,6);
  10084. //Use delay as initial offset to avoid skill/heal exploits
  10085. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10086. pd->s_skill->timer = INVALID_TIMER;
  10087. else
  10088. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  10089. return 0;
  10090. }
  10091. /*==========================================
  10092. * Scripted skill effects [Celest]
  10093. *------------------------------------------*/
  10094. /// skilleffect <skill id>,<level>
  10095. /// skilleffect "<skill name>",<level>
  10096. BUILDIN_FUNC(skilleffect)
  10097. {
  10098. TBL_PC *sd;
  10099. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10100. int skilllv=script_getnum(st,3);
  10101. sd=script_rid2sd(st);
  10102. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  10103. return 0;
  10104. }
  10105. /*==========================================
  10106. * NPC skill effects [Valaris]
  10107. *------------------------------------------*/
  10108. /// npcskilleffect <skill id>,<level>,<x>,<y>
  10109. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  10110. BUILDIN_FUNC(npcskilleffect)
  10111. {
  10112. struct block_list *bl= map_id2bl(st->oid);
  10113. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10114. int skilllv=script_getnum(st,3);
  10115. int x=script_getnum(st,4);
  10116. int y=script_getnum(st,5);
  10117. if (bl)
  10118. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  10119. return 0;
  10120. }
  10121. /*==========================================
  10122. * Special effects [Valaris]
  10123. *------------------------------------------*/
  10124. BUILDIN_FUNC(specialeffect)
  10125. {
  10126. struct block_list *bl=map_id2bl(st->oid);
  10127. int type = script_getnum(st,2);
  10128. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10129. if(bl==NULL)
  10130. return 0;
  10131. if( script_hasdata(st,4) )
  10132. {
  10133. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10134. if(nd)
  10135. clif_specialeffect(&nd->bl, type, target);
  10136. }
  10137. else
  10138. {
  10139. if (target == SELF) {
  10140. TBL_PC *sd=script_rid2sd(st);
  10141. if (sd)
  10142. clif_specialeffect_single(bl,type,sd->fd);
  10143. } else {
  10144. clif_specialeffect(bl, type, target);
  10145. }
  10146. }
  10147. return 0;
  10148. }
  10149. BUILDIN_FUNC(specialeffect2)
  10150. {
  10151. TBL_PC *sd=script_rid2sd(st);
  10152. int type = script_getnum(st,2);
  10153. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10154. if( script_hasdata(st,4) )
  10155. sd = map_nick2sd(script_getstr(st,4));
  10156. if (sd)
  10157. clif_specialeffect(&sd->bl, type, target);
  10158. return 0;
  10159. }
  10160. /*==========================================
  10161. * Nude [Valaris]
  10162. *------------------------------------------*/
  10163. BUILDIN_FUNC(nude)
  10164. {
  10165. TBL_PC *sd=script_rid2sd(st);
  10166. int i,calcflag=0;
  10167. if(sd==NULL)
  10168. return 0;
  10169. for(i=0;i<11;i++)
  10170. if(sd->equip_index[i] >= 0) {
  10171. if(!calcflag)
  10172. calcflag=1;
  10173. pc_unequipitem(sd,sd->equip_index[i],2);
  10174. }
  10175. if(calcflag)
  10176. status_calc_pc(sd,0);
  10177. return 0;
  10178. }
  10179. /*==========================================
  10180. * gmcommand [MouseJstr]
  10181. *
  10182. * suggested on the forums...
  10183. * splitted into atcommand & charcommand by [Skotlex]
  10184. *------------------------------------------*/
  10185. BUILDIN_FUNC(atcommand)
  10186. {
  10187. TBL_PC dummy_sd;
  10188. TBL_PC* sd;
  10189. int fd;
  10190. const char* cmd;
  10191. cmd = script_getstr(st,2);
  10192. if (st->rid) {
  10193. sd = script_rid2sd(st);
  10194. fd = sd->fd;
  10195. } else { //Use a dummy character.
  10196. sd = &dummy_sd;
  10197. fd = 0;
  10198. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10199. if (st->oid)
  10200. {
  10201. struct block_list* bl = map_id2bl(st->oid);
  10202. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10203. if (bl->type == BL_NPC)
  10204. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10205. }
  10206. }
  10207. // compatibility with previous implementation (deprecated!)
  10208. if(cmd[0] != atcommand_symbol)
  10209. {
  10210. cmd += strlen(sd->status.name);
  10211. while(*cmd != atcommand_symbol && *cmd != 0)
  10212. cmd++;
  10213. }
  10214. is_atcommand(fd, sd, cmd, 0);
  10215. return 0;
  10216. }
  10217. BUILDIN_FUNC(charcommand)
  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 (*cmd != charcommand_symbol) {
  10240. ShowWarning("script: buildin_charcommand: No '#' symbol!\n");
  10241. script_reportsrc(st);
  10242. return 1;
  10243. }
  10244. is_atcommand(fd, sd, cmd, 0);
  10245. return 0;
  10246. }
  10247. /*==========================================
  10248. * Displays a message for the player only (like system messages like "you got an apple" )
  10249. *------------------------------------------*/
  10250. BUILDIN_FUNC(dispbottom)
  10251. {
  10252. TBL_PC *sd=script_rid2sd(st);
  10253. const char *message;
  10254. message=script_getstr(st,2);
  10255. if(sd)
  10256. clif_disp_onlyself(sd,message,(int)strlen(message));
  10257. return 0;
  10258. }
  10259. /*==========================================
  10260. * All The Players Full Recovery
  10261. * (HP/SP full restore and resurrect if need)
  10262. *------------------------------------------*/
  10263. BUILDIN_FUNC(recovery)
  10264. {
  10265. TBL_PC* sd;
  10266. struct s_mapiterator* iter;
  10267. iter = mapit_getallusers();
  10268. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10269. {
  10270. if(pc_isdead(sd))
  10271. status_revive(&sd->bl, 100, 100);
  10272. else
  10273. status_percent_heal(&sd->bl, 100, 100);
  10274. clif_displaymessage(sd->fd,"You have been recovered!");
  10275. }
  10276. mapit_free(iter);
  10277. return 0;
  10278. }
  10279. /*==========================================
  10280. * Get your pet info: getpetinfo(n)
  10281. * n -> 0:pet_id 1:pet_class 2:pet_name
  10282. * 3:friendly 4:hungry, 5: rename flag.
  10283. *------------------------------------------*/
  10284. BUILDIN_FUNC(getpetinfo)
  10285. {
  10286. TBL_PC *sd=script_rid2sd(st);
  10287. TBL_PET *pd;
  10288. int type=script_getnum(st,2);
  10289. if(!sd || !sd->pd) {
  10290. if (type == 2)
  10291. script_pushconststr(st,"null");
  10292. else
  10293. script_pushint(st,0);
  10294. return 0;
  10295. }
  10296. pd = sd->pd;
  10297. switch(type){
  10298. case 0: script_pushint(st,pd->pet.pet_id); break;
  10299. case 1: script_pushint(st,pd->pet.class_); break;
  10300. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10301. case 3: script_pushint(st,pd->pet.intimate); break;
  10302. case 4: script_pushint(st,pd->pet.hungry); break;
  10303. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10304. default:
  10305. script_pushint(st,0);
  10306. break;
  10307. }
  10308. return 0;
  10309. }
  10310. /*==========================================
  10311. * Get your homunculus info: gethominfo(n)
  10312. * n -> 0:hom_id 1:class 2:name
  10313. * 3:friendly 4:hungry, 5: rename flag.
  10314. * 6: level
  10315. *------------------------------------------*/
  10316. BUILDIN_FUNC(gethominfo)
  10317. {
  10318. TBL_PC *sd=script_rid2sd(st);
  10319. TBL_HOM *hd;
  10320. int type=script_getnum(st,2);
  10321. hd = sd?sd->hd:NULL;
  10322. if(!merc_is_hom_active(hd))
  10323. {
  10324. if (type == 2)
  10325. script_pushconststr(st,"null");
  10326. else
  10327. script_pushint(st,0);
  10328. return 0;
  10329. }
  10330. switch(type){
  10331. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10332. case 1: script_pushint(st,hd->homunculus.class_); break;
  10333. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10334. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10335. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10336. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10337. case 6: script_pushint(st,hd->homunculus.level); break;
  10338. default:
  10339. script_pushint(st,0);
  10340. break;
  10341. }
  10342. return 0;
  10343. }
  10344. /// Retrieves information about character's mercenary
  10345. /// getmercinfo <type>[,<char id>];
  10346. BUILDIN_FUNC(getmercinfo)
  10347. {
  10348. int type, char_id;
  10349. struct map_session_data* sd;
  10350. struct mercenary_data* md;
  10351. type = script_getnum(st,2);
  10352. if( script_hasdata(st,3) )
  10353. {
  10354. char_id = script_getnum(st,3);
  10355. if( ( sd = map_charid2sd(char_id) ) == NULL )
  10356. {
  10357. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  10358. script_pushnil(st);
  10359. return 1;
  10360. }
  10361. }
  10362. else
  10363. {
  10364. if( ( sd = script_rid2sd(st) ) == NULL )
  10365. {
  10366. script_pushnil(st);
  10367. return 0;
  10368. }
  10369. }
  10370. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  10371. switch( type )
  10372. {
  10373. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  10374. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  10375. case 2:
  10376. if( md )
  10377. script_pushstrcopy(st,md->db->name);
  10378. else
  10379. script_pushconststr(st,"");
  10380. break;
  10381. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  10382. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  10383. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  10384. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  10385. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  10386. default:
  10387. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  10388. script_pushnil(st);
  10389. return 1;
  10390. }
  10391. return 0;
  10392. }
  10393. /*==========================================
  10394. * Shows wether your inventory(and equips) contain
  10395. selected card or not.
  10396. checkequipedcard(4001);
  10397. *------------------------------------------*/
  10398. BUILDIN_FUNC(checkequipedcard)
  10399. {
  10400. TBL_PC *sd=script_rid2sd(st);
  10401. int n,i,c=0;
  10402. c=script_getnum(st,2);
  10403. if(sd){
  10404. for(i=0;i<MAX_INVENTORY;i++){
  10405. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10406. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10407. continue;
  10408. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10409. if(sd->status.inventory[i].card[n]==c){
  10410. script_pushint(st,1);
  10411. return 0;
  10412. }
  10413. }
  10414. }
  10415. }
  10416. }
  10417. script_pushint(st,0);
  10418. return 0;
  10419. }
  10420. BUILDIN_FUNC(jump_zero)
  10421. {
  10422. int sel;
  10423. sel=script_getnum(st,2);
  10424. if(!sel) {
  10425. int pos;
  10426. if( !data_islabel(script_getdata(st,3)) ){
  10427. ShowError("script: jump_zero: not label !\n");
  10428. st->state=END;
  10429. return 1;
  10430. }
  10431. pos=script_getnum(st,3);
  10432. st->pos=pos;
  10433. st->state=GOTO;
  10434. }
  10435. return 0;
  10436. }
  10437. /*==========================================
  10438. * GetMapMobs
  10439. returns mob counts on a set map:
  10440. e.g. GetMapMobs("prontera")
  10441. use "this" - for player's map
  10442. *------------------------------------------*/
  10443. BUILDIN_FUNC(getmapmobs)
  10444. {
  10445. const char *str=NULL;
  10446. int m=-1,bx,by;
  10447. int count=0;
  10448. struct block_list *bl;
  10449. str=script_getstr(st,2);
  10450. if(strcmp(str,"this")==0){
  10451. TBL_PC *sd=script_rid2sd(st);
  10452. if(sd)
  10453. m=sd->bl.m;
  10454. else{
  10455. script_pushint(st,-1);
  10456. return 0;
  10457. }
  10458. }else
  10459. m=map_mapname2mapid(str);
  10460. if(m < 0){
  10461. script_pushint(st,-1);
  10462. return 0;
  10463. }
  10464. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  10465. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  10466. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  10467. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  10468. count++;
  10469. script_pushint(st,count);
  10470. return 0;
  10471. }
  10472. /*==========================================
  10473. * movenpc [MouseJstr]
  10474. *------------------------------------------*/
  10475. BUILDIN_FUNC(movenpc)
  10476. {
  10477. TBL_NPC *nd = NULL;
  10478. const char *npc;
  10479. int x,y;
  10480. npc = script_getstr(st,2);
  10481. x = script_getnum(st,3);
  10482. y = script_getnum(st,4);
  10483. if ((nd = npc_name2id(npc)) == NULL)
  10484. return -1;
  10485. if (script_hasdata(st,5))
  10486. nd->ud.dir = script_getnum(st,5) % 8;
  10487. npc_movenpc(nd, x, y);
  10488. return 0;
  10489. }
  10490. /*==========================================
  10491. * message [MouseJstr]
  10492. *------------------------------------------*/
  10493. BUILDIN_FUNC(message)
  10494. {
  10495. const char *msg,*player;
  10496. TBL_PC *pl_sd = NULL;
  10497. player = script_getstr(st,2);
  10498. msg = script_getstr(st,3);
  10499. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10500. return 0;
  10501. clif_displaymessage(pl_sd->fd, msg);
  10502. return 0;
  10503. }
  10504. /*==========================================
  10505. * npctalk (sends message to surrounding area)
  10506. *------------------------------------------*/
  10507. BUILDIN_FUNC(npctalk)
  10508. {
  10509. const char* str;
  10510. char name[NAME_LENGTH], message[256];
  10511. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10512. str = script_getstr(st,2);
  10513. if(nd)
  10514. {
  10515. safestrncpy(name, nd->name, sizeof(name));
  10516. strtok(name, "#"); // discard extra name identifier if present
  10517. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  10518. clif_message(&nd->bl, message);
  10519. }
  10520. return 0;
  10521. }
  10522. // change npc walkspeed [Valaris]
  10523. BUILDIN_FUNC(npcspeed)
  10524. {
  10525. struct npc_data* nd;
  10526. int speed;
  10527. speed = script_getnum(st,2);
  10528. nd =(struct npc_data *)map_id2bl(st->oid);
  10529. if( nd )
  10530. {
  10531. nd->speed = speed;
  10532. nd->ud.state.speed_changed = 1;
  10533. }
  10534. return 0;
  10535. }
  10536. // make an npc walk to a position [Valaris]
  10537. BUILDIN_FUNC(npcwalkto)
  10538. {
  10539. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10540. int x=0,y=0;
  10541. x=script_getnum(st,2);
  10542. y=script_getnum(st,3);
  10543. if(nd) {
  10544. unit_walktoxy(&nd->bl,x,y,0);
  10545. }
  10546. return 0;
  10547. }
  10548. // stop an npc's movement [Valaris]
  10549. BUILDIN_FUNC(npcstop)
  10550. {
  10551. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10552. if(nd) {
  10553. unit_stop_walking(&nd->bl,1|4);
  10554. }
  10555. return 0;
  10556. }
  10557. /*==========================================
  10558. * getlook char info. getlook(arg)
  10559. *------------------------------------------*/
  10560. BUILDIN_FUNC(getlook)
  10561. {
  10562. int type,val;
  10563. TBL_PC *sd;
  10564. sd=script_rid2sd(st);
  10565. type=script_getnum(st,2);
  10566. val=-1;
  10567. switch(type) {
  10568. case LOOK_HAIR: val=sd->status.hair; break; //1
  10569. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10570. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10571. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10572. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10573. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10574. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10575. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10576. case LOOK_SHOES: break; //9
  10577. }
  10578. script_pushint(st,val);
  10579. return 0;
  10580. }
  10581. /*==========================================
  10582. * get char save point. argument: 0- map name, 1- x, 2- y
  10583. *------------------------------------------*/
  10584. BUILDIN_FUNC(getsavepoint)
  10585. {
  10586. TBL_PC* sd;
  10587. int type;
  10588. sd = script_rid2sd(st);
  10589. if (sd == NULL) {
  10590. script_pushint(st,0);
  10591. return 0;
  10592. }
  10593. type = script_getnum(st,2);
  10594. switch(type) {
  10595. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10596. case 1: script_pushint(st,sd->status.save_point.x); break;
  10597. case 2: script_pushint(st,sd->status.save_point.y); break;
  10598. default:
  10599. script_pushint(st,0);
  10600. break;
  10601. }
  10602. return 0;
  10603. }
  10604. /*==========================================
  10605. * Get position for char/npc/pet/mob objects. Added by Lorky
  10606. *
  10607. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10608. * where type:
  10609. * MapName$ - String variable for output map name
  10610. * MapX - Integer variable for output coord X
  10611. * MapY - Integer variable for output coord Y
  10612. * type - type of object
  10613. * 0 - Character coord
  10614. * 1 - NPC coord
  10615. * 2 - Pet coord
  10616. * 3 - Mob coord (not released)
  10617. * 4 - Homun coord
  10618. * CharName$ - Name object. If miss or "this" the current object
  10619. *
  10620. * Return:
  10621. * 0 - success
  10622. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10623. *------------------------------------------*/
  10624. BUILDIN_FUNC(getmapxy)
  10625. {
  10626. struct block_list *bl = NULL;
  10627. TBL_PC *sd=NULL;
  10628. int num;
  10629. const char *name;
  10630. char prefix;
  10631. int x,y,type;
  10632. char mapname[MAP_NAME_LENGTH];
  10633. if( !data_isreference(script_getdata(st,2)) ){
  10634. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10635. script_pushint(st,-1);
  10636. return 1;
  10637. }
  10638. if( !data_isreference(script_getdata(st,3)) ){
  10639. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10640. script_pushint(st,-1);
  10641. return 1;
  10642. }
  10643. if( !data_isreference(script_getdata(st,4)) ){
  10644. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10645. script_pushint(st,-1);
  10646. return 1;
  10647. }
  10648. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10649. type=script_getnum(st,5);
  10650. switch (type){
  10651. case 0: //Get Character Position
  10652. if( script_hasdata(st,6) )
  10653. sd=map_nick2sd(script_getstr(st,6));
  10654. else
  10655. sd=script_rid2sd(st);
  10656. if (sd)
  10657. bl = &sd->bl;
  10658. break;
  10659. case 1: //Get NPC Position
  10660. if( script_hasdata(st,6) )
  10661. {
  10662. struct npc_data *nd;
  10663. nd=npc_name2id(script_getstr(st,6));
  10664. if (nd)
  10665. bl = &nd->bl;
  10666. } else //In case the origin is not an npc?
  10667. bl=map_id2bl(st->oid);
  10668. break;
  10669. case 2: //Get Pet Position
  10670. if(script_hasdata(st,6))
  10671. sd=map_nick2sd(script_getstr(st,6));
  10672. else
  10673. sd=script_rid2sd(st);
  10674. if (sd && sd->pd)
  10675. bl = &sd->pd->bl;
  10676. break;
  10677. case 3: //Get Mob Position
  10678. break; //Not supported?
  10679. case 4: //Get Homun Position
  10680. if(script_hasdata(st,6))
  10681. sd=map_nick2sd(script_getstr(st,6));
  10682. else
  10683. sd=script_rid2sd(st);
  10684. if (sd && sd->hd)
  10685. bl = &sd->hd->bl;
  10686. break;
  10687. default:
  10688. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  10689. script_pushint(st,-1);
  10690. return 1;
  10691. }
  10692. if (!bl) { //No object found.
  10693. script_pushint(st,-1);
  10694. return 0;
  10695. }
  10696. x= bl->x;
  10697. y= bl->y;
  10698. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10699. //Set MapName$
  10700. num=st->stack->stack_data[st->start+2].u.num;
  10701. name=get_str(num&0x00ffffff);
  10702. prefix=*name;
  10703. if(not_server_variable(prefix))
  10704. sd=script_rid2sd(st);
  10705. else
  10706. sd=NULL;
  10707. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10708. //Set MapX
  10709. num=st->stack->stack_data[st->start+3].u.num;
  10710. name=get_str(num&0x00ffffff);
  10711. prefix=*name;
  10712. if(not_server_variable(prefix))
  10713. sd=script_rid2sd(st);
  10714. else
  10715. sd=NULL;
  10716. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10717. //Set MapY
  10718. num=st->stack->stack_data[st->start+4].u.num;
  10719. name=get_str(num&0x00ffffff);
  10720. prefix=*name;
  10721. if(not_server_variable(prefix))
  10722. sd=script_rid2sd(st);
  10723. else
  10724. sd=NULL;
  10725. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10726. //Return Success value
  10727. script_pushint(st,0);
  10728. return 0;
  10729. }
  10730. /*==========================================
  10731. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10732. *------------------------------------------*/
  10733. BUILDIN_FUNC(logmes)
  10734. {
  10735. const char *str;
  10736. TBL_PC* sd;
  10737. if( log_config.npc <= 0 )
  10738. return 0;
  10739. sd = script_rid2sd(st);
  10740. if( sd == NULL )
  10741. return 1;
  10742. str = script_getstr(st,2);
  10743. log_npc(sd,str);
  10744. return 0;
  10745. }
  10746. BUILDIN_FUNC(summon)
  10747. {
  10748. int _class, timeout=0;
  10749. const char *str,*event="";
  10750. TBL_PC *sd;
  10751. struct mob_data *md;
  10752. int tick = gettick();
  10753. sd=script_rid2sd(st);
  10754. if (!sd) return 0;
  10755. str =script_getstr(st,2);
  10756. _class=script_getnum(st,3);
  10757. if( script_hasdata(st,4) )
  10758. timeout=script_getnum(st,4);
  10759. if( script_hasdata(st,5) ){
  10760. event=script_getstr(st,5);
  10761. check_event(st, event);
  10762. }
  10763. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10764. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10765. if (md) {
  10766. md->master_id=sd->bl.id;
  10767. md->special_state.ai=1;
  10768. if( md->deletetimer != INVALID_TIMER )
  10769. delete_timer(md->deletetimer, mob_timer_delete);
  10770. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10771. mob_spawn (md); //Now it is ready for spawning.
  10772. clif_specialeffect(&md->bl,344,AREA);
  10773. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10774. }
  10775. return 0;
  10776. }
  10777. /*==========================================
  10778. * Checks whether it is daytime/nighttime
  10779. *------------------------------------------*/
  10780. BUILDIN_FUNC(isnight)
  10781. {
  10782. script_pushint(st,(night_flag == 1));
  10783. return 0;
  10784. }
  10785. BUILDIN_FUNC(isday)
  10786. {
  10787. script_pushint(st,(night_flag == 0));
  10788. return 0;
  10789. }
  10790. /*================================================
  10791. * Check how many items/cards in the list are
  10792. * equipped - used for 2/15's cards patch [celest]
  10793. *------------------------------------------------*/
  10794. BUILDIN_FUNC(isequippedcnt)
  10795. {
  10796. TBL_PC *sd;
  10797. int i, j, k, id = 1;
  10798. int ret = 0;
  10799. sd = script_rid2sd(st);
  10800. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10801. script_pushint(st,0);
  10802. return 0;
  10803. }
  10804. for (i=0; id!=0; i++) {
  10805. FETCH (i+2, id) else id = 0;
  10806. if (id <= 0)
  10807. continue;
  10808. for (j=0; j<EQI_MAX; j++) {
  10809. int index;
  10810. index = sd->equip_index[j];
  10811. if(index < 0) continue;
  10812. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10813. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10814. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10815. if(!sd->inventory_data[index])
  10816. continue;
  10817. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10818. if (sd->inventory_data[index]->nameid == id)
  10819. ret+= sd->status.inventory[index].amount;
  10820. } else { //Count cards.
  10821. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10822. continue; //No cards
  10823. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10824. if (sd->status.inventory[index].card[k] == id)
  10825. ret++; //[Lupus]
  10826. }
  10827. }
  10828. }
  10829. }
  10830. script_pushint(st,ret);
  10831. return 0;
  10832. }
  10833. /*================================================
  10834. * Check whether another card has been
  10835. * equipped - used for 2/15's cards patch [celest]
  10836. * -- Items checked cannot be reused in another
  10837. * card set to prevent exploits
  10838. *------------------------------------------------*/
  10839. BUILDIN_FUNC(isequipped)
  10840. {
  10841. TBL_PC *sd;
  10842. int i, j, k, id = 1;
  10843. int index, flag;
  10844. int ret = -1;
  10845. //Original hash to reverse it when full check fails.
  10846. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10847. sd = script_rid2sd(st);
  10848. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10849. script_pushint(st,0);
  10850. return 0;
  10851. }
  10852. setitem_hash = sd->setitem_hash;
  10853. setitem_hash2 = sd->setitem_hash2;
  10854. for (i=0; id!=0; i++)
  10855. {
  10856. FETCH (i+2, id) else id = 0;
  10857. if (id <= 0)
  10858. continue;
  10859. flag = 0;
  10860. for (j=0; j<EQI_MAX; j++)
  10861. {
  10862. index = sd->equip_index[j];
  10863. if(index < 0) continue;
  10864. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10865. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10866. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10867. if(!sd->inventory_data[index])
  10868. continue;
  10869. if (itemdb_type(id) != IT_CARD) {
  10870. if (sd->inventory_data[index]->nameid != id)
  10871. continue;
  10872. flag = 1;
  10873. break;
  10874. } else { //Cards
  10875. if (sd->inventory_data[index]->slot == 0 ||
  10876. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10877. continue;
  10878. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10879. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10880. unsigned int hash = 0;
  10881. if (sd->status.inventory[index].card[k] != id)
  10882. continue;
  10883. hash = 1<<((j<5?j:j-5)*4 + k);
  10884. // check if card is already used by another set
  10885. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10886. continue;
  10887. // We have found a match
  10888. flag = 1;
  10889. // Set hash so this card cannot be used by another
  10890. if (j<5)
  10891. sd->setitem_hash |= hash;
  10892. else
  10893. sd->setitem_hash2 |= hash;
  10894. break;
  10895. }
  10896. }
  10897. if (flag) break; //Card found
  10898. }
  10899. if (ret == -1)
  10900. ret = flag;
  10901. else
  10902. ret &= flag;
  10903. if (!ret) break;
  10904. }
  10905. if (!ret)
  10906. { //When check fails, restore original hash values. [Skotlex]
  10907. sd->setitem_hash = setitem_hash;
  10908. sd->setitem_hash2 = setitem_hash2;
  10909. }
  10910. script_pushint(st,ret);
  10911. return 0;
  10912. }
  10913. /*================================================
  10914. * Check how many given inserted cards in the CURRENT
  10915. * weapon - used for 2/15's cards patch [Lupus]
  10916. *------------------------------------------------*/
  10917. BUILDIN_FUNC(cardscnt)
  10918. {
  10919. TBL_PC *sd;
  10920. int i, k, id = 1;
  10921. int ret = 0;
  10922. int index;
  10923. sd = script_rid2sd(st);
  10924. for (i=0; id!=0; i++) {
  10925. FETCH (i+2, id) else id = 0;
  10926. if (id <= 0)
  10927. continue;
  10928. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10929. if(index < 0) continue;
  10930. if(!sd->inventory_data[index])
  10931. continue;
  10932. if(itemdb_type(id) != IT_CARD) {
  10933. if (sd->inventory_data[index]->nameid == id)
  10934. ret+= sd->status.inventory[index].amount;
  10935. } else {
  10936. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10937. continue;
  10938. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10939. if (sd->status.inventory[index].card[k] == id)
  10940. ret++;
  10941. }
  10942. }
  10943. }
  10944. script_pushint(st,ret);
  10945. // script_pushint(st,current_equip_item_index);
  10946. return 0;
  10947. }
  10948. /*=======================================================
  10949. * Returns the refined number of the current item, or an
  10950. * item with inventory index specified
  10951. *-------------------------------------------------------*/
  10952. BUILDIN_FUNC(getrefine)
  10953. {
  10954. TBL_PC *sd;
  10955. if ((sd = script_rid2sd(st))!= NULL)
  10956. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10957. else
  10958. script_pushint(st,0);
  10959. return 0;
  10960. }
  10961. /*=======================================================
  10962. * Day/Night controls
  10963. *-------------------------------------------------------*/
  10964. BUILDIN_FUNC(night)
  10965. {
  10966. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10967. return 0;
  10968. }
  10969. BUILDIN_FUNC(day)
  10970. {
  10971. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10972. return 0;
  10973. }
  10974. //=======================================================
  10975. // Unequip [Spectre]
  10976. //-------------------------------------------------------
  10977. BUILDIN_FUNC(unequip)
  10978. {
  10979. int i;
  10980. size_t num;
  10981. TBL_PC *sd;
  10982. num = script_getnum(st,2);
  10983. sd = script_rid2sd(st);
  10984. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10985. {
  10986. i = pc_checkequip(sd,equip[num-1]);
  10987. if (i >= 0)
  10988. pc_unequipitem(sd,i,1|2);
  10989. }
  10990. return 0;
  10991. }
  10992. BUILDIN_FUNC(equip)
  10993. {
  10994. int nameid=0,i;
  10995. TBL_PC *sd;
  10996. struct item_data *item_data;
  10997. sd = script_rid2sd(st);
  10998. nameid=script_getnum(st,2);
  10999. if((item_data = itemdb_exists(nameid)) == NULL)
  11000. {
  11001. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  11002. return 1;
  11003. }
  11004. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  11005. if( i < MAX_INVENTORY )
  11006. pc_equipitem(sd,i,item_data->equip);
  11007. return 0;
  11008. }
  11009. BUILDIN_FUNC(autoequip)
  11010. {
  11011. int nameid, flag;
  11012. struct item_data *item_data;
  11013. nameid=script_getnum(st,2);
  11014. flag=script_getnum(st,3);
  11015. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  11016. {
  11017. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  11018. return 1;
  11019. }
  11020. if( !itemdb_isequip2(item_data) )
  11021. {
  11022. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  11023. return 1;
  11024. }
  11025. item_data->flag.autoequip = flag>0?1:0;
  11026. return 0;
  11027. }
  11028. BUILDIN_FUNC(setbattleflag)
  11029. {
  11030. const char *flag, *value;
  11031. flag = script_getstr(st,2);
  11032. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  11033. if (battle_set_value(flag, value) == 0)
  11034. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  11035. else
  11036. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  11037. return 0;
  11038. }
  11039. BUILDIN_FUNC(getbattleflag)
  11040. {
  11041. const char *flag;
  11042. flag = script_getstr(st,2);
  11043. script_pushint(st,battle_get_value(flag));
  11044. return 0;
  11045. }
  11046. //=======================================================
  11047. // strlen [Valaris]
  11048. //-------------------------------------------------------
  11049. BUILDIN_FUNC(getstrlen)
  11050. {
  11051. const char *str = script_getstr(st,2);
  11052. int len = (str) ? (int)strlen(str) : 0;
  11053. script_pushint(st,len);
  11054. return 0;
  11055. }
  11056. //=======================================================
  11057. // isalpha [Valaris]
  11058. //-------------------------------------------------------
  11059. BUILDIN_FUNC(charisalpha)
  11060. {
  11061. const char *str=script_getstr(st,2);
  11062. int pos=script_getnum(st,3);
  11063. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  11064. script_pushint(st,val);
  11065. return 0;
  11066. }
  11067. //=======================================================
  11068. // charisupper <str>, <index>
  11069. //-------------------------------------------------------
  11070. BUILDIN_FUNC(charisupper)
  11071. {
  11072. const char *str = script_getstr(st,2);
  11073. int pos = script_getnum(st,3);
  11074. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  11075. script_pushint(st,val);
  11076. return 0;
  11077. }
  11078. //=======================================================
  11079. // charislower <str>, <index>
  11080. //-------------------------------------------------------
  11081. BUILDIN_FUNC(charislower)
  11082. {
  11083. const char *str = script_getstr(st,2);
  11084. int pos = script_getnum(st,3);
  11085. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  11086. script_pushint(st,val);
  11087. return 0;
  11088. }
  11089. //=======================================================
  11090. // charat <str>, <index>
  11091. //-------------------------------------------------------
  11092. BUILDIN_FUNC(charat)
  11093. {
  11094. const char *str = script_getstr(st,2);
  11095. int pos = script_getnum(st,3);
  11096. char *output;
  11097. output = (char*)aMallocA(2*sizeof(char));
  11098. output[0] = '\0';
  11099. if(str && pos >= 0 && (unsigned int)pos < strlen(str))
  11100. sprintf(output, "%c", str[pos]);
  11101. script_pushstr(st, output);
  11102. return 0;
  11103. }
  11104. //=======================================================
  11105. // setchar <string>, <char>, <index>
  11106. //-------------------------------------------------------
  11107. BUILDIN_FUNC(setchar)
  11108. {
  11109. const char *str = script_getstr(st,2);
  11110. const char *c = script_getstr(st,3);
  11111. int index = script_getnum(st,4);
  11112. char *output;
  11113. size_t len = strlen(str);
  11114. output = (char*)aMallocA(len + 1);
  11115. memcpy(output, str, len);
  11116. output[len] = '\0';
  11117. if(index >= 0 && index < len)
  11118. output[index] = c[0];
  11119. script_pushstr(st, output);
  11120. return 0;
  11121. }
  11122. //=======================================================
  11123. // insertchar <string>, <char>, <index>
  11124. //-------------------------------------------------------
  11125. BUILDIN_FUNC(insertchar)
  11126. {
  11127. const char *str = script_getstr(st,2);
  11128. const char *c = script_getstr(st,3);
  11129. int index = script_getnum(st,4);
  11130. char *output;
  11131. size_t len = strlen(str);
  11132. if(index < 0)
  11133. index = 0;
  11134. else if(index > len)
  11135. index = len;
  11136. output = (char*)aMallocA(len + 2);
  11137. memcpy(output, str, index);
  11138. output[index] = c[0];
  11139. memcpy(&output[index+1], &str[index], len - index);
  11140. output[len+1] = '\0';
  11141. script_pushstr(st, output);
  11142. return 0;
  11143. }
  11144. //=======================================================
  11145. // delchar <string>, <index>
  11146. //-------------------------------------------------------
  11147. BUILDIN_FUNC(delchar)
  11148. {
  11149. const char *str = script_getstr(st,2);
  11150. int index = script_getnum(st,3);
  11151. char *output;
  11152. size_t len = strlen(str);
  11153. if(index < 0 || index > len) {
  11154. //return original
  11155. ++len;
  11156. output = (char*)aMallocA(len);
  11157. memcpy(output, str, len);
  11158. script_pushstr(st, output);
  11159. return 0;
  11160. }
  11161. output = (char*)aMallocA(len);
  11162. memcpy(output, str, index);
  11163. memcpy(&output[index], &str[index+1], len - index);
  11164. script_pushstr(st, output);
  11165. return 0;
  11166. }
  11167. //=======================================================
  11168. // strtoupper <str>
  11169. //-------------------------------------------------------
  11170. BUILDIN_FUNC(strtoupper)
  11171. {
  11172. const char *str = script_getstr(st,2);
  11173. char *output;
  11174. int i = 0;
  11175. output = (char*)aMallocA(strlen(str) + 1);
  11176. while(str[i] != '\0')
  11177. output[i++] = TOUPPER(str[i]);
  11178. output[i] = '\0';
  11179. script_pushstr(st, output);
  11180. return 0;
  11181. }
  11182. //=======================================================
  11183. // strtolower <str>
  11184. //-------------------------------------------------------
  11185. BUILDIN_FUNC(strtolower)
  11186. {
  11187. const char *str = script_getstr(st,2);
  11188. char *output;
  11189. int i = 0;
  11190. output = (char*)aMallocA(strlen(str) + 1);
  11191. while(str[i] != '\0')
  11192. output[i++] = TOLOWER(str[i]);
  11193. output[i] = '\0';
  11194. script_pushstr(st, output);
  11195. return 0;
  11196. }
  11197. //=======================================================
  11198. // substr <str>, <start>, <end>
  11199. //-------------------------------------------------------
  11200. BUILDIN_FUNC(substr)
  11201. {
  11202. const char *str = script_getstr(st,2);
  11203. char *output;
  11204. int start = script_getnum(st,3);
  11205. int end = script_getnum(st,4);
  11206. int len = 0;
  11207. if(start >= 0 && end < strlen(str) && start <= end) {
  11208. len = end - start + 1;
  11209. output = (char*)aMallocA(len + 1);
  11210. memcpy(output, &str[start], len);
  11211. } else
  11212. output = (char*)aMallocA(1);
  11213. output[len] = '\0';
  11214. script_pushstr(st, output);
  11215. return 0;
  11216. }
  11217. //=======================================================
  11218. // explode <dest_string_array>, <str>, <delimiter>
  11219. // Note: delimiter is limited to 1 char
  11220. //-------------------------------------------------------
  11221. BUILDIN_FUNC(explode)
  11222. {
  11223. struct script_data* data = script_getdata(st, 2);
  11224. const char *str = script_getstr(st,3);
  11225. const char delimiter = script_getstr(st, 4)[0];
  11226. int32 id;
  11227. size_t len = strlen(str);
  11228. int i = 0, j = 0;
  11229. int start;
  11230. char *temp;
  11231. const char* name;
  11232. TBL_PC* sd = NULL;
  11233. temp = (char*)aMallocA(len + 1);
  11234. if( !data_isreference(data) )
  11235. {
  11236. ShowError("script:explode: not a variable\n");
  11237. script_reportdata(data);
  11238. st->state = END;
  11239. return 1;// not a variable
  11240. }
  11241. id = reference_getid(data);
  11242. start = reference_getindex(data);
  11243. name = reference_getname(data);
  11244. if( not_array_variable(*name) )
  11245. {
  11246. ShowError("script:explode: illegal scope\n");
  11247. script_reportdata(data);
  11248. st->state = END;
  11249. return 1;// not supported
  11250. }
  11251. if( !is_string_variable(name) )
  11252. {
  11253. ShowError("script:explode: not string array\n");
  11254. script_reportdata(data);
  11255. st->state = END;
  11256. return 1;// data type mismatch
  11257. }
  11258. if( not_server_variable(*name) )
  11259. {
  11260. sd = script_rid2sd(st);
  11261. if( sd == NULL )
  11262. return 0;// no player attached
  11263. }
  11264. while(str[i] != '\0') {
  11265. if(str[i] == delimiter && start < 127) { //break at delimiter but ignore after reaching last array index
  11266. temp[j] = '\0';
  11267. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  11268. j = 0;
  11269. ++i;
  11270. } else {
  11271. temp[j++] = str[i++];
  11272. }
  11273. }
  11274. //set last string
  11275. temp[j] = '\0';
  11276. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  11277. aFree(temp);
  11278. return 0;
  11279. }
  11280. //=======================================================
  11281. // implode <string_array>
  11282. // implode <string_array>, <glue>
  11283. //-------------------------------------------------------
  11284. BUILDIN_FUNC(implode)
  11285. {
  11286. struct script_data* data = script_getdata(st, 2);
  11287. const char *glue = NULL, *name, *temp;
  11288. int32 glue_len = 0, array_size, id;
  11289. size_t len = 0;
  11290. int i, k = 0;
  11291. TBL_PC* sd = NULL;
  11292. char *output;
  11293. if( !data_isreference(data) )
  11294. {
  11295. ShowError("script:implode: not a variable\n");
  11296. script_reportdata(data);
  11297. st->state = END;
  11298. return 1;// not a variable
  11299. }
  11300. id = reference_getid(data);
  11301. name = reference_getname(data);
  11302. if( not_array_variable(*name) )
  11303. {
  11304. ShowError("script:implode: illegal scope\n");
  11305. script_reportdata(data);
  11306. st->state = END;
  11307. return 1;// not supported
  11308. }
  11309. if( !is_string_variable(name) )
  11310. {
  11311. ShowError("script:implode: not string array\n");
  11312. script_reportdata(data);
  11313. st->state = END;
  11314. return 1;// data type mismatch
  11315. }
  11316. if( not_server_variable(*name) )
  11317. {
  11318. sd = script_rid2sd(st);
  11319. if( sd == NULL )
  11320. return 0;// no player attached
  11321. }
  11322. //count chars
  11323. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  11324. if(array_size == -1) //empty array check (AmsTaff)
  11325. {
  11326. ShowWarning("script:implode: array length = 0\n");
  11327. output = (char*)aMallocA(sizeof(char)*5);
  11328. sprintf(output,"%s","NULL");
  11329. } else {
  11330. for(i = 0; i <= array_size; ++i) {
  11331. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11332. len += strlen(temp);
  11333. script_removetop(st, -1, 0);
  11334. }
  11335. //allocate mem
  11336. if( script_hasdata(st,3) ) {
  11337. glue = script_getstr(st,3);
  11338. glue_len = strlen(glue);
  11339. len += glue_len * (array_size);
  11340. }
  11341. output = (char*)aMallocA(len + 1);
  11342. //build output
  11343. for(i = 0; i < array_size; ++i) {
  11344. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11345. len = strlen(temp);
  11346. memcpy(&output[k], temp, len);
  11347. k += len;
  11348. if(glue_len != 0) {
  11349. memcpy(&output[k], glue, glue_len);
  11350. k += glue_len;
  11351. }
  11352. script_removetop(st, -1, 0);
  11353. }
  11354. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  11355. len = strlen(temp);
  11356. memcpy(&output[k], temp, len);
  11357. k += len;
  11358. script_removetop(st, -1, 0);
  11359. output[k] = '\0';
  11360. }
  11361. script_pushstr(st, output);
  11362. return 0;
  11363. }
  11364. //=======================================================
  11365. // sprintf(<format>, ...);
  11366. // Implements C sprintf, except format %n. The resulting string is
  11367. // returned, instead of being saved in variable by reference.
  11368. //-------------------------------------------------------
  11369. BUILDIN_FUNC(sprintf)
  11370. {
  11371. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  11372. const char* format;
  11373. char* p;
  11374. char* q;
  11375. char* buf = NULL;
  11376. char* buf2 = NULL;
  11377. struct script_data* data;
  11378. StringBuf final_buf;
  11379. // Fetch init data
  11380. format = script_getstr(st, 2);
  11381. argc = script_lastdata(st)-2;
  11382. len = strlen(format);
  11383. // Skip parsing, where no parsing is required.
  11384. if(len==0){
  11385. script_pushconststr(st,"");
  11386. return 0;
  11387. }
  11388. // Pessimistic alloc
  11389. CREATE(buf, char, len+1);
  11390. // Need not be parsed, just solve stuff like %%.
  11391. if(argc==0){
  11392. sprintf(buf, format);
  11393. script_pushstrcopy(st, buf);
  11394. aFree(buf);
  11395. return 0;
  11396. }
  11397. safestrncpy(buf, format, len+1);
  11398. // Issue sprintf for each parameter
  11399. StringBuf_Init(&final_buf);
  11400. q = buf;
  11401. while((p = strchr(q, '%'))!=NULL){
  11402. if(p!=q){
  11403. len = p-q+1;
  11404. if(buf2_len<len){
  11405. RECREATE(buf2, char, len);
  11406. buf2_len = len;
  11407. }
  11408. safestrncpy(buf2, q, len);
  11409. StringBuf_AppendStr(&final_buf, buf2);
  11410. q = p;
  11411. }
  11412. p = q+1;
  11413. if(*p=='%'){ // %%
  11414. StringBuf_AppendStr(&final_buf, "%");
  11415. q+=2;
  11416. continue;
  11417. }
  11418. if(*p=='n'){ // %n
  11419. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  11420. script_reportsrc(st);
  11421. q+=2;
  11422. continue;
  11423. }
  11424. if(arg>=argc){
  11425. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  11426. if(buf) aFree(buf);
  11427. if(buf2) aFree(buf2);
  11428. StringBuf_Destroy(&final_buf);
  11429. script_pushconststr(st,"");
  11430. return 1;
  11431. }
  11432. if((p = strchr(q+1, '%'))==NULL){
  11433. p = strchr(q, 0); // EOS
  11434. }
  11435. len = p-q+1;
  11436. if(buf2_len<len){
  11437. RECREATE(buf2, char, len);
  11438. buf2_len = len;
  11439. }
  11440. safestrncpy(buf2, q, len);
  11441. q = p;
  11442. // Note: This assumes the passed value being the correct
  11443. // type to the current format specifier. If not, the server
  11444. // probably crashes or returns anything else, than expected,
  11445. // but it would behave in normal code the same way so it's
  11446. // the scripter's responsibility.
  11447. data = script_getdata(st, arg+3);
  11448. if(data_isstring(data)){ // String
  11449. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11450. }else if(data_isint(data)){ // Number
  11451. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11452. }else if(data_isreference(data)){ // Variable
  11453. char* name = reference_getname(data);
  11454. if(name[strlen(name)-1]=='$'){ // var Str
  11455. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11456. }else{ // var Int
  11457. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11458. }
  11459. }else{ // Unsupported type
  11460. ShowError("buildin_sprintf: Unknown argument type!\n");
  11461. if(buf) aFree(buf);
  11462. if(buf2) aFree(buf2);
  11463. StringBuf_Destroy(&final_buf);
  11464. script_pushconststr(st,"");
  11465. return 1;
  11466. }
  11467. arg++;
  11468. }
  11469. // Append anything left
  11470. if(*q){
  11471. StringBuf_AppendStr(&final_buf, q);
  11472. }
  11473. // Passed more, than needed
  11474. if(arg<argc){
  11475. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  11476. script_reportsrc(st);
  11477. }
  11478. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  11479. if(buf) aFree(buf);
  11480. if(buf2) aFree(buf2);
  11481. StringBuf_Destroy(&final_buf);
  11482. return 0;
  11483. }
  11484. //=======================================================
  11485. // sscanf(<str>, <format>, ...);
  11486. // Implements C sscanf.
  11487. //-------------------------------------------------------
  11488. BUILDIN_FUNC(sscanf){
  11489. unsigned int argc, arg = 0, len;
  11490. struct script_data* data;
  11491. struct map_session_data* sd = NULL;
  11492. const char* str;
  11493. const char* format;
  11494. const char* p;
  11495. const char* q;
  11496. char* buf = NULL;
  11497. char* buf_p;
  11498. char* ref_str = NULL;
  11499. int ref_int;
  11500. // Get data
  11501. str = script_getstr(st, 2);
  11502. format = script_getstr(st, 3);
  11503. argc = script_lastdata(st)-3;
  11504. len = strlen(format);
  11505. CREATE(buf, char, len*2+1);
  11506. // Issue sscanf for each parameter
  11507. *buf = 0;
  11508. q = format;
  11509. while((p = strchr(q, '%'))){
  11510. if(p!=q){
  11511. strncat(buf, q, (size_t)(p-q));
  11512. q = p;
  11513. }
  11514. p = q+1;
  11515. if(*p=='*' || *p=='%'){ // Skip
  11516. strncat(buf, q, 2);
  11517. q+=2;
  11518. continue;
  11519. }
  11520. if(arg>=argc){
  11521. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  11522. script_pushint(st, -1);
  11523. if(buf) aFree(buf);
  11524. if(ref_str) aFree(ref_str);
  11525. return 1;
  11526. }
  11527. if((p = strchr(q+1, '%'))==NULL){
  11528. p = strchr(q, 0); // EOS
  11529. }
  11530. len = p-q;
  11531. strncat(buf, q, len);
  11532. q = p;
  11533. // Validate output
  11534. data = script_getdata(st, arg+4);
  11535. if(!data_isreference(data) || !reference_tovariable(data)){
  11536. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  11537. script_pushint(st, -1);
  11538. if(buf) aFree(buf);
  11539. if(ref_str) aFree(ref_str);
  11540. return 1;
  11541. }
  11542. buf_p = reference_getname(data);
  11543. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  11544. script_pushint(st, -1);
  11545. if(buf) aFree(buf);
  11546. if(ref_str) aFree(ref_str);
  11547. return 0;
  11548. }
  11549. // Save value if any
  11550. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  11551. if(ref_str==NULL){
  11552. CREATE(ref_str, char, strlen(str)+1);
  11553. }
  11554. if(sscanf(str, buf, ref_str)==0){
  11555. break;
  11556. }
  11557. set_reg(st, sd, add_str(buf_p), buf_p, (void *)(ref_str), reference_getref(data));
  11558. }else{ // Number
  11559. if(sscanf(str, buf, &ref_int)==0){
  11560. break;
  11561. }
  11562. set_reg(st, sd, add_str(buf_p), buf_p, (void *)(ref_int), reference_getref(data));
  11563. }
  11564. arg++;
  11565. // Disable used format (%... -> %*...)
  11566. buf_p = strchr(buf, 0);
  11567. memmove(buf_p-len+2, buf_p-len+1, len);
  11568. *(buf_p-len+1) = '*';
  11569. }
  11570. // Passed more, than needed
  11571. if(arg<argc){
  11572. ShowWarning("buildin_sscanf: Unused arguments passed.\n");
  11573. script_reportsrc(st);
  11574. }
  11575. script_pushint(st, arg);
  11576. if(buf) aFree(buf);
  11577. if(ref_str) aFree(ref_str);
  11578. return 0;
  11579. }
  11580. //=======================================================
  11581. // strpos(<haystack>, <needle>)
  11582. // strpos(<haystack>, <needle>, <offset>)
  11583. //
  11584. // Implements PHP style strpos. Adapted from code from
  11585. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  11586. //-------------------------------------------------------
  11587. BUILDIN_FUNC(strpos) {
  11588. const char *haystack = script_getstr(st,2);
  11589. const char *needle = script_getstr(st,3);
  11590. int i;
  11591. size_t len;
  11592. if( script_hasdata(st,4) )
  11593. i = script_getnum(st,4);
  11594. else
  11595. i = 0;
  11596. if ( strlen(needle) == 0 ) {
  11597. script_pushint(st, -1);
  11598. return 0;
  11599. }
  11600. len = strlen(haystack);
  11601. for ( ; i < len; ++i ) {
  11602. if ( haystack[i] == *needle ) {
  11603. // matched starting char -- loop through remaining chars
  11604. const char *h, *n;
  11605. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  11606. if ( *h != *n ) {
  11607. break;
  11608. }
  11609. }
  11610. if ( !*n ) { // matched all of 'needle' to null termination
  11611. script_pushint(st, i);
  11612. return 0;
  11613. }
  11614. }
  11615. }
  11616. script_pushint(st, -1);
  11617. return 0;
  11618. }
  11619. //===============================================================
  11620. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  11621. //
  11622. // Note: Finds all instances of <search> in <input> and replaces
  11623. // with <replace>. If specified will only replace as many
  11624. // instances as specified in <count>. By default will be case
  11625. // sensitive.
  11626. //---------------------------------------------------------------
  11627. BUILDIN_FUNC(replacestr)
  11628. {
  11629. const char *input = script_getstr(st, 2);
  11630. const char *find = script_getstr(st, 3);
  11631. const char *replace = script_getstr(st, 4);
  11632. size_t inputlen = strlen(input);
  11633. size_t findlen = strlen(find);
  11634. struct StringBuf output;
  11635. bool usecase = true;
  11636. int count = 0;
  11637. int numFinds = 0;
  11638. int i = 0, f = 0;
  11639. if(findlen == 0) {
  11640. ShowError("script:replacestr: Invalid search length.\n");
  11641. st->state = END;
  11642. return 1;
  11643. }
  11644. if(script_hasdata(st, 5)) {
  11645. if(script_isint(st,5))
  11646. usecase = script_getnum(st, 5) != 0;
  11647. else {
  11648. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  11649. st->state = END;
  11650. return 1;
  11651. }
  11652. }
  11653. if(script_hasdata(st, 6)) {
  11654. if(script_isint(st,6))
  11655. count = script_getnum(st, 6);
  11656. else {
  11657. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  11658. st->state = END;
  11659. return 1;
  11660. }
  11661. }
  11662. StringBuf_Init(&output);
  11663. for(; i < inputlen; i++) {
  11664. if(count && count == numFinds) { //found enough, stop looking
  11665. break;
  11666. }
  11667. for(f = 0; f <= findlen; f++) {
  11668. if(f == findlen) { //complete match
  11669. numFinds++;
  11670. StringBuf_AppendStr(&output, replace);
  11671. i += findlen - 1;
  11672. break;
  11673. } else {
  11674. if(usecase) {
  11675. if((i + f) > inputlen || input[i + f] != find[f]) {
  11676. StringBuf_Printf(&output, "%c", input[i]);
  11677. break;
  11678. }
  11679. } else {
  11680. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11681. StringBuf_Printf(&output, "%c", input[i]);
  11682. break;
  11683. }
  11684. }
  11685. }
  11686. }
  11687. }
  11688. //append excess after enough found
  11689. if(i < inputlen)
  11690. StringBuf_AppendStr(&output, &(input[i]));
  11691. script_pushstrcopy(st, StringBuf_Value(&output));
  11692. StringBuf_Destroy(&output);
  11693. return 0;
  11694. }
  11695. //========================================================
  11696. // countstr <input>, <search>{, <usecase>}
  11697. //
  11698. // Note: Counts the number of times <search> occurs in
  11699. // <input>. By default will be case sensitive.
  11700. //--------------------------------------------------------
  11701. BUILDIN_FUNC(countstr)
  11702. {
  11703. const char *input = script_getstr(st, 2);
  11704. const char *find = script_getstr(st, 3);
  11705. size_t inputlen = strlen(input);
  11706. size_t findlen = strlen(find);
  11707. bool usecase = true;
  11708. int numFinds = 0;
  11709. int i = 0, f = 0;
  11710. if(findlen == 0) {
  11711. ShowError("script:countstr: Invalid search length.\n");
  11712. st->state = END;
  11713. return 1;
  11714. }
  11715. if(script_hasdata(st, 4)) {
  11716. if(script_isint(st,4))
  11717. usecase = script_getnum(st, 4) != 0;
  11718. else {
  11719. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  11720. st->state = END;
  11721. return 1;
  11722. }
  11723. }
  11724. for(; i < inputlen; i++) {
  11725. for(f = 0; f <= findlen; f++) {
  11726. if(f == findlen) { //complete match
  11727. numFinds++;
  11728. i += findlen - 1;
  11729. break;
  11730. } else {
  11731. if(usecase) {
  11732. if((i + f) > inputlen || input[i + f] != find[f]) {
  11733. break;
  11734. }
  11735. } else {
  11736. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11737. break;
  11738. }
  11739. }
  11740. }
  11741. }
  11742. }
  11743. script_pushint(st, numFinds);
  11744. return 0;
  11745. }
  11746. /// Changes the display name and/or display class of the npc.
  11747. /// Returns 0 is successful, 1 if the npc does not exist.
  11748. ///
  11749. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  11750. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  11751. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  11752. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  11753. BUILDIN_FUNC(setnpcdisplay)
  11754. {
  11755. const char* name;
  11756. const char* newname = NULL;
  11757. int class_ = -1, size = -1;
  11758. struct script_data* data;
  11759. struct npc_data* nd;
  11760. name = script_getstr(st,2);
  11761. data = script_getdata(st,3);
  11762. if( script_hasdata(st,4) )
  11763. class_ = script_getnum(st,4);
  11764. if( script_hasdata(st,5) )
  11765. size = script_getnum(st,5);
  11766. get_val(st, data);
  11767. if( data_isstring(data) )
  11768. newname = conv_str(st,data);
  11769. else if( data_isint(data) )
  11770. class_ = conv_num(st,data);
  11771. else
  11772. {
  11773. ShowError("script:setnpcdisplay: expected a string or number\n");
  11774. script_reportdata(data);
  11775. return 1;
  11776. }
  11777. nd = npc_name2id(name);
  11778. if( nd == NULL )
  11779. {// not found
  11780. script_pushint(st,1);
  11781. return 0;
  11782. }
  11783. // update npc
  11784. if( newname )
  11785. npc_setdisplayname(nd, newname);
  11786. if( size != -1 && size != (int)nd->size )
  11787. nd->size = size;
  11788. else
  11789. size = -1;
  11790. if( class_ != -1 && nd->class_ != class_ )
  11791. npc_setclass(nd, class_);
  11792. else if( size != -1 )
  11793. { // Required to update the visual size
  11794. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  11795. clif_spawn(&nd->bl);
  11796. }
  11797. script_pushint(st,0);
  11798. return 0;
  11799. }
  11800. BUILDIN_FUNC(atoi)
  11801. {
  11802. const char *value;
  11803. value = script_getstr(st,2);
  11804. script_pushint(st,atoi(value));
  11805. return 0;
  11806. }
  11807. // case-insensitive substring search [lordalfa]
  11808. BUILDIN_FUNC(compare)
  11809. {
  11810. const char *message;
  11811. const char *cmpstring;
  11812. message = script_getstr(st,2);
  11813. cmpstring = script_getstr(st,3);
  11814. script_pushint(st,(stristr(message,cmpstring) != NULL));
  11815. return 0;
  11816. }
  11817. // [zBuffer] List of mathematics commands --->
  11818. BUILDIN_FUNC(sqrt)
  11819. {
  11820. double i, a;
  11821. i = script_getnum(st,2);
  11822. a = sqrt(i);
  11823. script_pushint(st,(int)a);
  11824. return 0;
  11825. }
  11826. BUILDIN_FUNC(pow)
  11827. {
  11828. double i, a, b;
  11829. a = script_getnum(st,2);
  11830. b = script_getnum(st,3);
  11831. i = pow(a,b);
  11832. script_pushint(st,(int)i);
  11833. return 0;
  11834. }
  11835. BUILDIN_FUNC(distance)
  11836. {
  11837. int x0, y0, x1, y1;
  11838. x0 = script_getnum(st,2);
  11839. y0 = script_getnum(st,3);
  11840. x1 = script_getnum(st,4);
  11841. y1 = script_getnum(st,5);
  11842. script_pushint(st,distance_xy(x0,y0,x1,y1));
  11843. return 0;
  11844. }
  11845. // <--- [zBuffer] List of mathematics commands
  11846. BUILDIN_FUNC(md5)
  11847. {
  11848. const char *tmpstr;
  11849. char *md5str;
  11850. tmpstr = script_getstr(st,2);
  11851. md5str = (char *)aMallocA((32+1)*sizeof(char));
  11852. MD5_String(tmpstr, md5str);
  11853. script_pushstr(st, md5str);
  11854. return 0;
  11855. }
  11856. // [zBuffer] List of dynamic var commands --->
  11857. BUILDIN_FUNC(setd)
  11858. {
  11859. TBL_PC *sd=NULL;
  11860. char varname[100];
  11861. const char *buffer;
  11862. int elem;
  11863. buffer = script_getstr(st, 2);
  11864. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  11865. elem = 0;
  11866. if( not_server_variable(*varname) )
  11867. {
  11868. sd = script_rid2sd(st);
  11869. if( sd == NULL )
  11870. {
  11871. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  11872. return 0;
  11873. }
  11874. }
  11875. if( is_string_variable(varname) ) {
  11876. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  11877. } else {
  11878. setd_sub(st, sd, varname, elem, (void *)script_getnum(st, 3), NULL);
  11879. }
  11880. return 0;
  11881. }
  11882. #ifndef TXT_ONLY
  11883. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  11884. {
  11885. int i, j;
  11886. TBL_PC* sd = NULL;
  11887. const char* query;
  11888. struct script_data* data;
  11889. const char* name;
  11890. int max_rows = SCRIPT_MAX_ARRAYSIZE;// maximum number of rows
  11891. int num_vars;
  11892. int num_cols;
  11893. // check target variables
  11894. for( i = 3; script_hasdata(st,i); ++i )
  11895. {
  11896. data = script_getdata(st, i);
  11897. if( data_isreference(data) && reference_tovariable(data) )
  11898. {// it's a variable
  11899. name = reference_getname(data);
  11900. if( not_server_variable(*name) && sd == NULL )
  11901. {// requires a player
  11902. sd = script_rid2sd(st);
  11903. if( sd == NULL )
  11904. {// no player attached
  11905. script_reportdata(data);
  11906. st->state = END;
  11907. return 1;
  11908. }
  11909. }
  11910. if( not_array_variable(*name) )
  11911. max_rows = 1;// not an array, limit to one row
  11912. }
  11913. else
  11914. {
  11915. ShowError("script:query_sql: not a variable\n");
  11916. script_reportdata(data);
  11917. st->state = END;
  11918. return 1;
  11919. }
  11920. }
  11921. num_vars = i - 3;
  11922. // Execute the query
  11923. query = script_getstr(st,2);
  11924. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  11925. {
  11926. Sql_ShowDebug(handle);
  11927. script_pushint(st, 0);
  11928. return 1;
  11929. }
  11930. if( Sql_NumRows(handle) == 0 )
  11931. {// No data received
  11932. Sql_FreeResult(handle);
  11933. script_pushint(st, 0);
  11934. return 0;
  11935. }
  11936. // Count the number of columns to store
  11937. num_cols = Sql_NumColumns(handle);
  11938. if( num_vars < num_cols )
  11939. {
  11940. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  11941. script_reportsrc(st);
  11942. }
  11943. else if( num_vars > num_cols )
  11944. {
  11945. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  11946. script_reportsrc(st);
  11947. }
  11948. // Store data
  11949. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  11950. {
  11951. for( j = 0; j < num_vars; ++j )
  11952. {
  11953. char* str = NULL;
  11954. if( j < num_cols )
  11955. Sql_GetData(handle, j, &str, NULL);
  11956. data = script_getdata(st, j+3);
  11957. name = reference_getname(data);
  11958. if( is_string_variable(name) )
  11959. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  11960. else
  11961. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  11962. }
  11963. }
  11964. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  11965. {
  11966. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  11967. script_reportsrc(st);
  11968. }
  11969. // Free data
  11970. Sql_FreeResult(handle);
  11971. script_pushint(st, i);
  11972. return 0;
  11973. }
  11974. #endif
  11975. BUILDIN_FUNC(query_sql)
  11976. {
  11977. #ifndef TXT_ONLY
  11978. return buildin_query_sql_sub(st, mmysql_handle);
  11979. #else
  11980. //for TXT version, we always return -1
  11981. script_pushint(st,-1);
  11982. return 0;
  11983. #endif
  11984. }
  11985. BUILDIN_FUNC(query_logsql)
  11986. {
  11987. #ifndef TXT_ONLY
  11988. if( !log_config.sql_logs )
  11989. {// logmysql_handle == NULL
  11990. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  11991. script_pushint(st,-1);
  11992. return 1;
  11993. }
  11994. return buildin_query_sql_sub(st, logmysql_handle);
  11995. #else
  11996. //for TXT version, we always return -1
  11997. script_pushint(st,-1);
  11998. return 0;
  11999. #endif
  12000. }
  12001. //Allows escaping of a given string.
  12002. BUILDIN_FUNC(escape_sql)
  12003. {
  12004. const char *str;
  12005. char *esc_str;
  12006. size_t len;
  12007. str = script_getstr(st,2);
  12008. len = strlen(str);
  12009. esc_str = (char*)aMallocA(len*2+1);
  12010. #if defined(TXT_ONLY)
  12011. jstrescapecpy(esc_str, str);
  12012. #else
  12013. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  12014. #endif
  12015. script_pushstr(st, esc_str);
  12016. return 0;
  12017. }
  12018. BUILDIN_FUNC(getd)
  12019. {
  12020. char varname[100];
  12021. const char *buffer;
  12022. int elem;
  12023. buffer = script_getstr(st, 2);
  12024. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  12025. elem = 0;
  12026. // Push the 'pointer' so it's more flexible [Lance]
  12027. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  12028. return 0;
  12029. }
  12030. // <--- [zBuffer] List of dynamic var commands
  12031. // Pet stat [Lance]
  12032. BUILDIN_FUNC(petstat)
  12033. {
  12034. TBL_PC *sd = NULL;
  12035. struct pet_data *pd;
  12036. int flag = script_getnum(st,2);
  12037. sd = script_rid2sd(st);
  12038. if(!sd || !sd->status.pet_id || !sd->pd){
  12039. if(flag == 2)
  12040. script_pushconststr(st, "");
  12041. else
  12042. script_pushint(st,0);
  12043. return 0;
  12044. }
  12045. pd = sd->pd;
  12046. switch(flag){
  12047. case 1: script_pushint(st,(int)pd->pet.class_); break;
  12048. case 2: script_pushstrcopy(st, pd->pet.name); break;
  12049. case 3: script_pushint(st,(int)pd->pet.level); break;
  12050. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  12051. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  12052. default:
  12053. script_pushint(st,0);
  12054. break;
  12055. }
  12056. return 0;
  12057. }
  12058. BUILDIN_FUNC(callshop)
  12059. {
  12060. TBL_PC *sd = NULL;
  12061. struct npc_data *nd;
  12062. const char *shopname;
  12063. int flag = 0;
  12064. sd = script_rid2sd(st);
  12065. if (!sd) {
  12066. script_pushint(st,0);
  12067. return 0;
  12068. }
  12069. shopname = script_getstr(st, 2);
  12070. if( script_hasdata(st,3) )
  12071. flag = script_getnum(st,3);
  12072. nd = npc_name2id(shopname);
  12073. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  12074. {
  12075. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  12076. script_pushint(st,0);
  12077. return 1;
  12078. }
  12079. if( nd->subtype == SHOP )
  12080. {
  12081. switch( flag )
  12082. {
  12083. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  12084. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  12085. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  12086. }
  12087. }
  12088. else
  12089. clif_cashshop_show(sd, nd);
  12090. sd->npc_shopid = nd->bl.id;
  12091. script_pushint(st,1);
  12092. return 0;
  12093. }
  12094. BUILDIN_FUNC(npcshopitem)
  12095. {
  12096. const char* npcname = script_getstr(st, 2);
  12097. struct npc_data* nd = npc_name2id(npcname);
  12098. int n, i;
  12099. int amount;
  12100. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12101. { //Not found.
  12102. script_pushint(st,0);
  12103. return 0;
  12104. }
  12105. // get the count of new entries
  12106. amount = (script_lastdata(st)-2)/2;
  12107. // generate new shop item list
  12108. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  12109. for( n = 0, i = 3; n < amount; n++, i+=2 )
  12110. {
  12111. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12112. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12113. }
  12114. nd->u.shop.count = n;
  12115. script_pushint(st,1);
  12116. return 0;
  12117. }
  12118. BUILDIN_FUNC(npcshopadditem)
  12119. {
  12120. const char* npcname = script_getstr(st,2);
  12121. struct npc_data* nd = npc_name2id(npcname);
  12122. int n, i;
  12123. int amount;
  12124. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12125. { //Not found.
  12126. script_pushint(st,0);
  12127. return 0;
  12128. }
  12129. // get the count of new entries
  12130. amount = (script_lastdata(st)-2)/2;
  12131. // append new items to existing shop item list
  12132. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  12133. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  12134. {
  12135. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12136. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12137. }
  12138. nd->u.shop.count = n;
  12139. script_pushint(st,1);
  12140. return 0;
  12141. }
  12142. BUILDIN_FUNC(npcshopdelitem)
  12143. {
  12144. const char* npcname = script_getstr(st,2);
  12145. struct npc_data* nd = npc_name2id(npcname);
  12146. unsigned int nameid;
  12147. int n, i;
  12148. int amount;
  12149. int size;
  12150. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12151. { //Not found.
  12152. script_pushint(st,0);
  12153. return 0;
  12154. }
  12155. amount = script_lastdata(st)-2;
  12156. size = nd->u.shop.count;
  12157. // remove specified items from the shop item list
  12158. for( i = 3; i < 3 + amount; i++ )
  12159. {
  12160. nameid = script_getnum(st,i);
  12161. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  12162. if( n < size )
  12163. {
  12164. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  12165. size--;
  12166. }
  12167. }
  12168. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  12169. nd->u.shop.count = size;
  12170. script_pushint(st,1);
  12171. return 0;
  12172. }
  12173. //Sets a script to attach to a shop npc.
  12174. BUILDIN_FUNC(npcshopattach)
  12175. {
  12176. const char* npcname = script_getstr(st,2);
  12177. struct npc_data* nd = npc_name2id(npcname);
  12178. int flag = 1;
  12179. if( script_hasdata(st,3) )
  12180. flag = script_getnum(st,3);
  12181. if( !nd || nd->subtype != SHOP )
  12182. { //Not found.
  12183. script_pushint(st,0);
  12184. return 0;
  12185. }
  12186. if (flag)
  12187. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  12188. else
  12189. nd->master_nd = NULL;
  12190. script_pushint(st,1);
  12191. return 0;
  12192. }
  12193. /*==========================================
  12194. * Returns some values of an item [Lupus]
  12195. * Price, Weight, etc...
  12196. setitemscript(itemID,"{new item bonus script}",[n]);
  12197. Where n:
  12198. 0 - script
  12199. 1 - Equip script
  12200. 2 - Unequip script
  12201. *------------------------------------------*/
  12202. BUILDIN_FUNC(setitemscript)
  12203. {
  12204. int item_id,n=0;
  12205. const char *script;
  12206. struct item_data *i_data;
  12207. struct script_code **dstscript;
  12208. item_id = script_getnum(st,2);
  12209. script = script_getstr(st,3);
  12210. if( script_hasdata(st,4) )
  12211. n=script_getnum(st,4);
  12212. i_data = itemdb_exists(item_id);
  12213. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  12214. script_pushint(st,0);
  12215. return 0;
  12216. }
  12217. switch (n) {
  12218. case 2:
  12219. dstscript = &i_data->unequip_script;
  12220. break;
  12221. case 1:
  12222. dstscript = &i_data->equip_script;
  12223. break;
  12224. default:
  12225. dstscript = &i_data->script;
  12226. break;
  12227. }
  12228. if(*dstscript)
  12229. script_free_code(*dstscript);
  12230. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  12231. script_pushint(st,1);
  12232. return 0;
  12233. }
  12234. /* Work In Progress [Lupus]
  12235. BUILDIN_FUNC(addmonsterdrop)
  12236. {
  12237. int class_,item_id,chance;
  12238. class_=script_getnum(st,2);
  12239. item_id=script_getnum(st,3);
  12240. chance=script_getnum(st,4);
  12241. if(class_>1000 && item_id>500 && chance>0) {
  12242. script_pushint(st,1);
  12243. } else {
  12244. script_pushint(st,0);
  12245. }
  12246. }
  12247. BUILDIN_FUNC(delmonsterdrop)
  12248. {
  12249. int class_,item_id;
  12250. class_=script_getnum(st,2);
  12251. item_id=script_getnum(st,3);
  12252. if(class_>1000 && item_id>500) {
  12253. script_pushint(st,1);
  12254. } else {
  12255. script_pushint(st,0);
  12256. }
  12257. }
  12258. */
  12259. /*==========================================
  12260. * Returns some values of a monster [Lupus]
  12261. * Name, Level, race, size, etc...
  12262. getmonsterinfo(monsterID,queryIndex);
  12263. *------------------------------------------*/
  12264. BUILDIN_FUNC(getmonsterinfo)
  12265. {
  12266. struct mob_db *mob;
  12267. int mob_id;
  12268. mob_id = script_getnum(st,2);
  12269. if (!mobdb_checkid(mob_id)) {
  12270. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  12271. if ( !script_getnum(st,3) ) //requested a string
  12272. script_pushconststr(st,"null");
  12273. else
  12274. script_pushint(st,-1);
  12275. return -1;
  12276. }
  12277. mob = mob_db(mob_id);
  12278. switch ( script_getnum(st,3) ) {
  12279. case 0: script_pushstrcopy(st,mob->jname); break;
  12280. case 1: script_pushint(st,mob->lv); break;
  12281. case 2: script_pushint(st,mob->status.max_hp); break;
  12282. case 3: script_pushint(st,mob->base_exp); break;
  12283. case 4: script_pushint(st,mob->job_exp); break;
  12284. case 5: script_pushint(st,mob->status.rhw.atk); break;
  12285. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  12286. case 7: script_pushint(st,mob->status.def); break;
  12287. case 8: script_pushint(st,mob->status.mdef); break;
  12288. case 9: script_pushint(st,mob->status.str); break;
  12289. case 10: script_pushint(st,mob->status.agi); break;
  12290. case 11: script_pushint(st,mob->status.vit); break;
  12291. case 12: script_pushint(st,mob->status.int_); break;
  12292. case 13: script_pushint(st,mob->status.dex); break;
  12293. case 14: script_pushint(st,mob->status.luk); break;
  12294. case 15: script_pushint(st,mob->status.rhw.range); break;
  12295. case 16: script_pushint(st,mob->range2); break;
  12296. case 17: script_pushint(st,mob->range3); break;
  12297. case 18: script_pushint(st,mob->status.size); break;
  12298. case 19: script_pushint(st,mob->status.race); break;
  12299. case 20: script_pushint(st,mob->status.def_ele); break;
  12300. case 21: script_pushint(st,mob->status.mode); break;
  12301. default: script_pushint(st,-1); //wrong Index
  12302. }
  12303. return 0;
  12304. }
  12305. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  12306. {
  12307. TBL_PC *sd = NULL;
  12308. if(script_hasdata(st,2))
  12309. sd = map_nick2sd(script_getstr(st,2));
  12310. else
  12311. sd = script_rid2sd(st);
  12312. if(sd)
  12313. script_pushint(st,sd->state.vending);
  12314. else
  12315. script_pushint(st,0);
  12316. return 0;
  12317. }
  12318. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  12319. {
  12320. TBL_PC *sd = NULL;
  12321. if(script_hasdata(st,2))
  12322. sd = map_nick2sd(script_getstr(st,2));
  12323. else
  12324. sd = script_rid2sd(st);
  12325. if(sd)
  12326. script_pushint(st,(sd->chatID != 0));
  12327. else
  12328. script_pushint(st,0);
  12329. return 0;
  12330. }
  12331. BUILDIN_FUNC(searchitem)
  12332. {
  12333. struct script_data* data = script_getdata(st, 2);
  12334. const char *itemname = script_getstr(st,3);
  12335. struct item_data *items[MAX_SEARCH];
  12336. int count;
  12337. char* name;
  12338. int32 start;
  12339. int32 id;
  12340. int32 i;
  12341. TBL_PC* sd = NULL;
  12342. if ((items[0] = itemdb_exists(atoi(itemname))))
  12343. count = 1;
  12344. else {
  12345. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  12346. if (count > MAX_SEARCH) count = MAX_SEARCH;
  12347. }
  12348. if (!count) {
  12349. script_pushint(st, 0);
  12350. return 0;
  12351. }
  12352. if( !data_isreference(data) )
  12353. {
  12354. ShowError("script:searchitem: not a variable\n");
  12355. script_reportdata(data);
  12356. st->state = END;
  12357. return 1;// not a variable
  12358. }
  12359. id = reference_getid(data);
  12360. start = reference_getindex(data);
  12361. name = reference_getname(data);
  12362. if( not_array_variable(*name) )
  12363. {
  12364. ShowError("script:searchitem: illegal scope\n");
  12365. script_reportdata(data);
  12366. st->state = END;
  12367. return 1;// not supported
  12368. }
  12369. if( not_server_variable(*name) )
  12370. {
  12371. sd = script_rid2sd(st);
  12372. if( sd == NULL )
  12373. return 0;// no player attached
  12374. }
  12375. if( is_string_variable(name) )
  12376. {// string array
  12377. ShowError("script:searchitem: not an integer array reference\n");
  12378. script_reportdata(data);
  12379. st->state = END;
  12380. return 1;// not supported
  12381. }
  12382. for( i = 0; i < count; ++start, ++i )
  12383. {// Set array
  12384. void* v = (void*)items[i]->nameid;
  12385. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  12386. }
  12387. script_pushint(st, count);
  12388. return 0;
  12389. }
  12390. int axtoi(const char *hexStg)
  12391. {
  12392. int n = 0; // position in string
  12393. int m = 0; // position in digit[] to shift
  12394. int count; // loop index
  12395. int intValue = 0; // integer value of hex string
  12396. int digit[11]; // hold values to convert
  12397. while (n < 10) {
  12398. if (hexStg[n]=='\0')
  12399. break;
  12400. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  12401. digit[n] = hexStg[n] & 0x0f; //convert to int
  12402. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  12403. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12404. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  12405. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12406. else break;
  12407. n++;
  12408. }
  12409. count = n;
  12410. m = n - 1;
  12411. n = 0;
  12412. while(n < count) {
  12413. // digit[n] is value of hex digit at position n
  12414. // (m << 2) is the number of positions to shift
  12415. // OR the bits into return value
  12416. intValue = intValue | (digit[n] << (m << 2));
  12417. m--; // adjust the position to set
  12418. n++; // next digit to process
  12419. }
  12420. return (intValue);
  12421. }
  12422. // [Lance] Hex string to integer converter
  12423. BUILDIN_FUNC(axtoi)
  12424. {
  12425. const char *hex = script_getstr(st,2);
  12426. script_pushint(st,axtoi(hex));
  12427. return 0;
  12428. }
  12429. // [zBuffer] List of player cont commands --->
  12430. BUILDIN_FUNC(rid2name)
  12431. {
  12432. struct block_list *bl = NULL;
  12433. int rid = script_getnum(st,2);
  12434. if((bl = map_id2bl(rid)))
  12435. {
  12436. switch(bl->type) {
  12437. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  12438. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  12439. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  12440. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  12441. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  12442. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  12443. default:
  12444. ShowError("buildin_rid2name: BL type unknown.\n");
  12445. script_pushconststr(st,"");
  12446. break;
  12447. }
  12448. } else {
  12449. ShowError("buildin_rid2name: invalid RID\n");
  12450. script_pushconststr(st,"(null)");
  12451. }
  12452. return 0;
  12453. }
  12454. BUILDIN_FUNC(pcblockmove)
  12455. {
  12456. int id, flag;
  12457. TBL_PC *sd = NULL;
  12458. id = script_getnum(st,2);
  12459. flag = script_getnum(st,3);
  12460. if(id)
  12461. sd = map_id2sd(id);
  12462. else
  12463. sd = script_rid2sd(st);
  12464. if(sd)
  12465. sd->state.blockedmove = flag > 0;
  12466. return 0;
  12467. }
  12468. BUILDIN_FUNC(pcfollow)
  12469. {
  12470. int id, targetid;
  12471. TBL_PC *sd = NULL;
  12472. id = script_getnum(st,2);
  12473. targetid = script_getnum(st,3);
  12474. if(id)
  12475. sd = map_id2sd(id);
  12476. else
  12477. sd = script_rid2sd(st);
  12478. if(sd)
  12479. pc_follow(sd, targetid);
  12480. return 0;
  12481. }
  12482. BUILDIN_FUNC(pcstopfollow)
  12483. {
  12484. int id;
  12485. TBL_PC *sd = NULL;
  12486. id = script_getnum(st,2);
  12487. if(id)
  12488. sd = map_id2sd(id);
  12489. else
  12490. sd = script_rid2sd(st);
  12491. if(sd)
  12492. pc_stop_following(sd);
  12493. return 0;
  12494. }
  12495. // <--- [zBuffer] List of player cont commands
  12496. // [zBuffer] List of mob control commands --->
  12497. //## TODO always return if the request/whatever was successfull [FlavioJS]
  12498. /// Makes the unit walk to target position or map
  12499. /// Returns if it was successfull
  12500. ///
  12501. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  12502. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  12503. BUILDIN_FUNC(unitwalk)
  12504. {
  12505. struct block_list* bl;
  12506. bl = map_id2bl(script_getnum(st,2));
  12507. if( bl == NULL )
  12508. {
  12509. script_pushint(st, 0);
  12510. }
  12511. else if( script_hasdata(st,4) )
  12512. {
  12513. int x = script_getnum(st,3);
  12514. int y = script_getnum(st,4);
  12515. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  12516. }
  12517. else
  12518. {
  12519. int map_id = script_getnum(st,3);
  12520. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  12521. }
  12522. return 0;
  12523. }
  12524. /// Kills the unit
  12525. ///
  12526. /// unitkill <unit_id>;
  12527. BUILDIN_FUNC(unitkill)
  12528. {
  12529. struct block_list* bl = map_id2bl(script_getnum(st,2));
  12530. if( bl != NULL )
  12531. status_kill(bl);
  12532. return 0;
  12533. }
  12534. /// Warps the unit to the target position in the target map
  12535. /// Returns if it was successfull
  12536. ///
  12537. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  12538. BUILDIN_FUNC(unitwarp)
  12539. {
  12540. int unit_id;
  12541. int map;
  12542. short x;
  12543. short y;
  12544. struct block_list* bl;
  12545. const char *mapname;
  12546. unit_id = script_getnum(st,2);
  12547. mapname = script_getstr(st, 3);
  12548. x = (short)script_getnum(st,4);
  12549. y = (short)script_getnum(st,5);
  12550. if (!unit_id) //Warp the script's runner
  12551. bl = map_id2bl(st->rid);
  12552. else
  12553. bl = map_id2bl(unit_id);
  12554. if( strcmp(mapname,"this") == 0 )
  12555. map = bl?bl->m:-1;
  12556. else
  12557. map = map_mapname2mapid(mapname);
  12558. if( map >= 0 && bl != NULL )
  12559. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  12560. else
  12561. script_pushint(st, 0);
  12562. return 0;
  12563. }
  12564. /// Makes the unit attack the target.
  12565. /// If the unit is a player and <action type> is not 0, it does a continuous
  12566. /// attack instead of a single attack.
  12567. /// Returns if the request was successfull.
  12568. ///
  12569. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  12570. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  12571. BUILDIN_FUNC(unitattack)
  12572. {
  12573. struct block_list* unit_bl;
  12574. struct block_list* target_bl = NULL;
  12575. struct script_data* data;
  12576. int actiontype = 0;
  12577. // get unit
  12578. unit_bl = map_id2bl(script_getnum(st,2));
  12579. if( unit_bl == NULL ) {
  12580. script_pushint(st, 0);
  12581. return 0;
  12582. }
  12583. data = script_getdata(st, 3);
  12584. get_val(st, data);
  12585. if( data_isstring(data) )
  12586. {
  12587. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  12588. if( sd != NULL )
  12589. target_bl = &sd->bl;
  12590. } else
  12591. target_bl = map_id2bl(conv_num(st, data));
  12592. // request the attack
  12593. if( target_bl == NULL )
  12594. {
  12595. script_pushint(st, 0);
  12596. return 0;
  12597. }
  12598. // get actiontype
  12599. if( script_hasdata(st,4) )
  12600. actiontype = script_getnum(st,4);
  12601. switch( unit_bl->type )
  12602. {
  12603. case BL_PC:
  12604. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  12605. script_pushint(st, 1);
  12606. return 0;
  12607. case BL_MOB:
  12608. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  12609. break;
  12610. case BL_PET:
  12611. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  12612. break;
  12613. default:
  12614. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  12615. script_pushint(st, 0);
  12616. return 1;
  12617. }
  12618. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  12619. return 0;
  12620. }
  12621. /// Makes the unit stop attacking and moving
  12622. ///
  12623. /// unitstop <unit_id>;
  12624. BUILDIN_FUNC(unitstop)
  12625. {
  12626. int unit_id;
  12627. struct block_list* bl;
  12628. unit_id = script_getnum(st,2);
  12629. bl = map_id2bl(unit_id);
  12630. if( bl != NULL )
  12631. {
  12632. unit_stop_attack(bl);
  12633. unit_stop_walking(bl,4);
  12634. if( bl->type == BL_MOB )
  12635. ((TBL_MOB*)bl)->target_id = 0;
  12636. }
  12637. return 0;
  12638. }
  12639. /// Makes the unit say the message
  12640. ///
  12641. /// unittalk <unit_id>,"<message>";
  12642. BUILDIN_FUNC(unittalk)
  12643. {
  12644. int unit_id;
  12645. const char* message;
  12646. struct block_list* bl;
  12647. unit_id = script_getnum(st,2);
  12648. message = script_getstr(st, 3);
  12649. bl = map_id2bl(unit_id);
  12650. if( bl != NULL )
  12651. {
  12652. struct StringBuf sbuf;
  12653. StringBuf_Init(&sbuf);
  12654. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  12655. clif_message(bl, StringBuf_Value(&sbuf));
  12656. if( bl->type == BL_PC )
  12657. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  12658. StringBuf_Destroy(&sbuf);
  12659. }
  12660. return 0;
  12661. }
  12662. /// Makes the unit do an emotion
  12663. ///
  12664. /// unitemote <unit_id>,<emotion>;
  12665. ///
  12666. /// @see e_* in const.txt
  12667. BUILDIN_FUNC(unitemote)
  12668. {
  12669. int unit_id;
  12670. int emotion;
  12671. struct block_list* bl;
  12672. unit_id = script_getnum(st,2);
  12673. emotion = script_getnum(st,3);
  12674. bl = map_id2bl(unit_id);
  12675. if( bl != NULL )
  12676. clif_emotion(bl, emotion);
  12677. return 0;
  12678. }
  12679. /// Makes the unit cast the skill on the target or self if no target is specified
  12680. ///
  12681. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  12682. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  12683. BUILDIN_FUNC(unitskilluseid)
  12684. {
  12685. int unit_id;
  12686. int skill_id;
  12687. int skill_lv;
  12688. int target_id;
  12689. struct block_list* bl;
  12690. unit_id = script_getnum(st,2);
  12691. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12692. skill_lv = script_getnum(st,4);
  12693. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  12694. bl = map_id2bl(unit_id);
  12695. if( bl != NULL )
  12696. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  12697. return 0;
  12698. }
  12699. /// Makes the unit cast the skill on the target position.
  12700. ///
  12701. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  12702. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  12703. BUILDIN_FUNC(unitskillusepos)
  12704. {
  12705. int unit_id;
  12706. int skill_id;
  12707. int skill_lv;
  12708. int skill_x;
  12709. int skill_y;
  12710. struct block_list* bl;
  12711. unit_id = script_getnum(st,2);
  12712. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12713. skill_lv = script_getnum(st,4);
  12714. skill_x = script_getnum(st,5);
  12715. skill_y = script_getnum(st,6);
  12716. bl = map_id2bl(unit_id);
  12717. if( bl != NULL )
  12718. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  12719. return 0;
  12720. }
  12721. // <--- [zBuffer] List of mob control commands
  12722. /// Pauses the execution of the script, detaching the player
  12723. ///
  12724. /// sleep <mili seconds>;
  12725. BUILDIN_FUNC(sleep)
  12726. {
  12727. int ticks;
  12728. ticks = script_getnum(st,2);
  12729. // detach the player
  12730. script_detach_rid(st);
  12731. if( ticks <= 0 )
  12732. {// do nothing
  12733. }
  12734. else if( st->sleep.tick == 0 )
  12735. {// sleep for the target amount of time
  12736. st->state = RERUNLINE;
  12737. st->sleep.tick = ticks;
  12738. }
  12739. else
  12740. {// sleep time is over
  12741. st->state = RUN;
  12742. st->sleep.tick = 0;
  12743. }
  12744. return 0;
  12745. }
  12746. /// Pauses the execution of the script, keeping the player attached
  12747. /// Returns if a player is still attached
  12748. ///
  12749. /// sleep2(<mili secconds>) -> <bool>
  12750. BUILDIN_FUNC(sleep2)
  12751. {
  12752. int ticks;
  12753. ticks = script_getnum(st,2);
  12754. if( ticks <= 0 )
  12755. {// do nothing
  12756. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12757. }
  12758. else if( !st->sleep.tick )
  12759. {// sleep for the target amount of time
  12760. st->state = RERUNLINE;
  12761. st->sleep.tick = ticks;
  12762. }
  12763. else
  12764. {// sleep time is over
  12765. st->state = RUN;
  12766. st->sleep.tick = 0;
  12767. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12768. }
  12769. return 0;
  12770. }
  12771. /// Awakes all the sleep timers of the target npc
  12772. ///
  12773. /// awake "<npc name>";
  12774. BUILDIN_FUNC(awake)
  12775. {
  12776. struct npc_data* nd;
  12777. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  12778. nd = npc_name2id(script_getstr(st, 2));
  12779. if( nd == NULL ) {
  12780. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  12781. return 1;
  12782. }
  12783. while( node )
  12784. {
  12785. if( (int)node->key == nd->bl.id )
  12786. {// sleep timer for the npc
  12787. struct script_state* tst = (struct script_state*)node->data;
  12788. TBL_PC* sd = map_id2sd(tst->rid);
  12789. if( tst->sleep.timer == INVALID_TIMER )
  12790. {// already awake ???
  12791. node = node->next;
  12792. continue;
  12793. }
  12794. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  12795. {// char not online anymore / another char of the same account is online - Cancel execution
  12796. tst->state = END;
  12797. tst->rid = 0;
  12798. }
  12799. delete_timer(tst->sleep.timer, run_script_timer);
  12800. node = script_erase_sleepdb(node);
  12801. tst->sleep.timer = INVALID_TIMER;
  12802. if(tst->state != RERUNLINE)
  12803. tst->sleep.tick = 0;
  12804. run_script_main(tst);
  12805. }
  12806. else
  12807. {
  12808. node = node->next;
  12809. }
  12810. }
  12811. return 0;
  12812. }
  12813. /// Returns a reference to a variable of the target NPC.
  12814. /// Returns 0 if an error occurs.
  12815. ///
  12816. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  12817. BUILDIN_FUNC(getvariableofnpc)
  12818. {
  12819. struct script_data* data;
  12820. const char* name;
  12821. struct npc_data* nd;
  12822. data = script_getdata(st,2);
  12823. if( !data_isreference(data) )
  12824. {// Not a reference (aka varaible name)
  12825. ShowError("script:getvariableofnpc: not a variable\n");
  12826. script_reportdata(data);
  12827. script_pushnil(st);
  12828. st->state = END;
  12829. return 1;
  12830. }
  12831. name = reference_getname(data);
  12832. if( *name != '.' || name[1] == '@' )
  12833. {// not a npc variable
  12834. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  12835. script_reportdata(data);
  12836. script_pushnil(st);
  12837. st->state = END;
  12838. return 1;
  12839. }
  12840. nd = npc_name2id(script_getstr(st,3));
  12841. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  12842. {// NPC not found or has no script
  12843. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  12844. script_pushnil(st);
  12845. st->state = END;
  12846. return 1;
  12847. }
  12848. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  12849. return 0;
  12850. }
  12851. /// Opens a warp portal.
  12852. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  12853. ///
  12854. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  12855. ///
  12856. /// @author blackhole89
  12857. BUILDIN_FUNC(warpportal)
  12858. {
  12859. int spx;
  12860. int spy;
  12861. unsigned short mapindex;
  12862. int tpx;
  12863. int tpy;
  12864. struct skill_unit_group* group;
  12865. struct block_list* bl;
  12866. bl = map_id2bl(st->oid);
  12867. if( bl == NULL )
  12868. {
  12869. ShowError("script:warpportal: npc is needed\n");
  12870. return 1;
  12871. }
  12872. spx = script_getnum(st,2);
  12873. spy = script_getnum(st,3);
  12874. mapindex = mapindex_name2id(script_getstr(st, 4));
  12875. tpx = script_getnum(st,5);
  12876. tpy = script_getnum(st,6);
  12877. if( mapindex == 0 )
  12878. return 0;// map not found
  12879. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  12880. if( group == NULL )
  12881. return 0;// failed
  12882. group->val2 = (tpx<<16) | tpy;
  12883. group->val3 = mapindex;
  12884. return 0;
  12885. }
  12886. BUILDIN_FUNC(openmail)
  12887. {
  12888. TBL_PC* sd;
  12889. sd = script_rid2sd(st);
  12890. if( sd == NULL )
  12891. return 0;
  12892. #ifndef TXT_ONLY
  12893. mail_openmail(sd);
  12894. #endif
  12895. return 0;
  12896. }
  12897. BUILDIN_FUNC(openauction)
  12898. {
  12899. TBL_PC* sd;
  12900. sd = script_rid2sd(st);
  12901. if( sd == NULL )
  12902. return 0;
  12903. #ifndef TXT_ONLY
  12904. clif_Auction_openwindow(sd);
  12905. #endif
  12906. return 0;
  12907. }
  12908. /// Retrieves the value of the specified flag of the specified cell.
  12909. ///
  12910. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  12911. ///
  12912. /// @see cell_chk* constants in const.txt for the types
  12913. BUILDIN_FUNC(checkcell)
  12914. {
  12915. int m = map_mapname2mapid(script_getstr(st,2));
  12916. int x = script_getnum(st,3);
  12917. int y = script_getnum(st,4);
  12918. cell_chk type = (cell_chk)script_getnum(st,5);
  12919. script_pushint(st, map_getcell(m, x, y, type));
  12920. return 0;
  12921. }
  12922. /// Modifies flags of cells in the specified area.
  12923. ///
  12924. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  12925. ///
  12926. /// @see cell_* constants in const.txt for the types
  12927. BUILDIN_FUNC(setcell)
  12928. {
  12929. int m = map_mapname2mapid(script_getstr(st,2));
  12930. int x1 = script_getnum(st,3);
  12931. int y1 = script_getnum(st,4);
  12932. int x2 = script_getnum(st,5);
  12933. int y2 = script_getnum(st,6);
  12934. cell_t type = (cell_t)script_getnum(st,7);
  12935. bool flag = (bool)script_getnum(st,8);
  12936. int x,y;
  12937. if( x1 > x2 ) swap(x1,x2);
  12938. if( y1 > y2 ) swap(y1,y2);
  12939. for( y = y1; y <= y2; ++y )
  12940. for( x = x1; x <= x2; ++x )
  12941. map_setcell(m, x, y, type, flag);
  12942. return 0;
  12943. }
  12944. /*==========================================
  12945. * Mercenary Commands
  12946. *------------------------------------------*/
  12947. BUILDIN_FUNC(mercenary_create)
  12948. {
  12949. struct map_session_data *sd;
  12950. int class_, contract_time;
  12951. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  12952. return 0;
  12953. class_ = script_getnum(st,2);
  12954. if( !merc_class(class_) )
  12955. return 0;
  12956. contract_time = script_getnum(st,3);
  12957. merc_create(sd, class_, contract_time);
  12958. return 0;
  12959. }
  12960. BUILDIN_FUNC(mercenary_heal)
  12961. {
  12962. struct map_session_data *sd = script_rid2sd(st);
  12963. int hp, sp;
  12964. if( sd == NULL || sd->md == NULL )
  12965. return 0;
  12966. hp = script_getnum(st,2);
  12967. sp = script_getnum(st,3);
  12968. status_heal(&sd->md->bl, hp, sp, 0);
  12969. return 0;
  12970. }
  12971. BUILDIN_FUNC(mercenary_sc_start)
  12972. {
  12973. struct map_session_data *sd = script_rid2sd(st);
  12974. enum sc_type type;
  12975. int tick, val1;
  12976. if( sd == NULL || sd->md == NULL )
  12977. return 0;
  12978. type = (sc_type)script_getnum(st,2);
  12979. tick = script_getnum(st,3);
  12980. val1 = script_getnum(st,4);
  12981. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  12982. return 0;
  12983. }
  12984. BUILDIN_FUNC(mercenary_get_calls)
  12985. {
  12986. struct map_session_data *sd = script_rid2sd(st);
  12987. int guild;
  12988. if( sd == NULL )
  12989. return 0;
  12990. guild = script_getnum(st,2);
  12991. switch( guild )
  12992. {
  12993. case ARCH_MERC_GUILD:
  12994. script_pushint(st,sd->status.arch_calls);
  12995. break;
  12996. case SPEAR_MERC_GUILD:
  12997. script_pushint(st,sd->status.spear_calls);
  12998. break;
  12999. case SWORD_MERC_GUILD:
  13000. script_pushint(st,sd->status.sword_calls);
  13001. break;
  13002. default:
  13003. script_pushint(st,0);
  13004. break;
  13005. }
  13006. return 0;
  13007. }
  13008. BUILDIN_FUNC(mercenary_set_calls)
  13009. {
  13010. struct map_session_data *sd = script_rid2sd(st);
  13011. int guild, value, *calls;
  13012. if( sd == NULL )
  13013. return 0;
  13014. guild = script_getnum(st,2);
  13015. value = script_getnum(st,3);
  13016. switch( guild )
  13017. {
  13018. case ARCH_MERC_GUILD:
  13019. calls = &sd->status.arch_calls;
  13020. break;
  13021. case SPEAR_MERC_GUILD:
  13022. calls = &sd->status.spear_calls;
  13023. break;
  13024. case SWORD_MERC_GUILD:
  13025. calls = &sd->status.sword_calls;
  13026. break;
  13027. default:
  13028. return 0; // Invalid Guild
  13029. }
  13030. *calls += value;
  13031. *calls = cap_value(*calls, 0, INT_MAX);
  13032. return 0;
  13033. }
  13034. BUILDIN_FUNC(mercenary_get_faith)
  13035. {
  13036. struct map_session_data *sd = script_rid2sd(st);
  13037. int guild;
  13038. if( sd == NULL )
  13039. return 0;
  13040. guild = script_getnum(st,2);
  13041. switch( guild )
  13042. {
  13043. case ARCH_MERC_GUILD:
  13044. script_pushint(st,sd->status.arch_faith);
  13045. break;
  13046. case SPEAR_MERC_GUILD:
  13047. script_pushint(st,sd->status.spear_faith);
  13048. break;
  13049. case SWORD_MERC_GUILD:
  13050. script_pushint(st,sd->status.sword_faith);
  13051. break;
  13052. default:
  13053. script_pushint(st,0);
  13054. break;
  13055. }
  13056. return 0;
  13057. }
  13058. BUILDIN_FUNC(mercenary_set_faith)
  13059. {
  13060. struct map_session_data *sd = script_rid2sd(st);
  13061. int guild, value, *calls;
  13062. if( sd == NULL )
  13063. return 0;
  13064. guild = script_getnum(st,2);
  13065. value = script_getnum(st,3);
  13066. switch( guild )
  13067. {
  13068. case ARCH_MERC_GUILD:
  13069. calls = &sd->status.arch_faith;
  13070. break;
  13071. case SPEAR_MERC_GUILD:
  13072. calls = &sd->status.spear_faith;
  13073. break;
  13074. case SWORD_MERC_GUILD:
  13075. calls = &sd->status.sword_faith;
  13076. break;
  13077. default:
  13078. return 0; // Invalid Guild
  13079. }
  13080. *calls += value;
  13081. *calls = cap_value(*calls, 0, INT_MAX);
  13082. if( mercenary_get_guild(sd->md) == guild )
  13083. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  13084. return 0;
  13085. }
  13086. /*------------------------------------------
  13087. * Book Reading
  13088. *------------------------------------------*/
  13089. BUILDIN_FUNC(readbook)
  13090. {
  13091. struct map_session_data *sd;
  13092. int book_id, page;
  13093. if( (sd = script_rid2sd(st)) == NULL )
  13094. return 0;
  13095. book_id = script_getnum(st,2);
  13096. page = script_getnum(st,3);
  13097. clif_readbook(sd->fd, book_id, page);
  13098. return 0;
  13099. }
  13100. /******************
  13101. Questlog script commands
  13102. *******************/
  13103. BUILDIN_FUNC(setquest)
  13104. {
  13105. TBL_PC * sd = script_rid2sd(st);
  13106. quest_add(sd, script_getnum(st, 2));
  13107. return 0;
  13108. }
  13109. BUILDIN_FUNC(erasequest)
  13110. {
  13111. TBL_PC * sd = script_rid2sd(st);
  13112. quest_delete(sd, script_getnum(st, 2));
  13113. return 0;
  13114. }
  13115. BUILDIN_FUNC(completequest)
  13116. {
  13117. TBL_PC * sd = script_rid2sd(st);
  13118. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  13119. return 0;
  13120. }
  13121. BUILDIN_FUNC(changequest)
  13122. {
  13123. TBL_PC * sd = script_rid2sd(st);
  13124. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  13125. return 0;
  13126. }
  13127. BUILDIN_FUNC(checkquest)
  13128. {
  13129. TBL_PC * sd = script_rid2sd(st);
  13130. quest_check_type type = HAVEQUEST;
  13131. if( script_hasdata(st, 3) )
  13132. type = (quest_check_type)script_getnum(st, 3);
  13133. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  13134. return 0;
  13135. }
  13136. BUILDIN_FUNC(showevent)
  13137. {
  13138. TBL_PC *sd = script_rid2sd(st);
  13139. struct npc_data *nd = map_id2nd(st->oid);
  13140. int state, color;
  13141. if( sd == NULL || nd == NULL )
  13142. return 0;
  13143. state = script_getnum(st, 2);
  13144. color = script_getnum(st, 3);
  13145. if( color < 0 || color > 4 )
  13146. color = 0; // set default color
  13147. clif_quest_show_event(sd, &nd->bl, state, color);
  13148. return 0;
  13149. }
  13150. /*==========================================
  13151. * BattleGround System
  13152. *------------------------------------------*/
  13153. BUILDIN_FUNC(waitingroom2bg)
  13154. {
  13155. struct npc_data *nd;
  13156. struct chat_data *cd;
  13157. const char *map_name, *ev = "", *dev = "";
  13158. int x, y, i, mapindex = 0, bg_id, n;
  13159. struct map_session_data *sd;
  13160. if( script_hasdata(st,7) )
  13161. nd = npc_name2id(script_getstr(st,7));
  13162. else
  13163. nd = (struct npc_data *)map_id2bl(st->oid);
  13164. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  13165. {
  13166. script_pushint(st,0);
  13167. return 0;
  13168. }
  13169. map_name = script_getstr(st,2);
  13170. if( strcmp(map_name,"-") != 0 )
  13171. {
  13172. mapindex = mapindex_name2id(map_name);
  13173. if( mapindex == 0 )
  13174. { // Invalid Map
  13175. script_pushint(st,0);
  13176. return 0;
  13177. }
  13178. }
  13179. x = script_getnum(st,3);
  13180. y = script_getnum(st,4);
  13181. ev = script_getstr(st,5); // Logout Event
  13182. dev = script_getstr(st,6); // Die Event
  13183. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  13184. { // Creation failed
  13185. script_pushint(st,0);
  13186. return 0;
  13187. }
  13188. n = cd->users;
  13189. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  13190. {
  13191. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  13192. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  13193. else
  13194. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  13195. }
  13196. mapreg_setreg(add_str("$@arenamembersnum"), i);
  13197. script_pushint(st,bg_id);
  13198. return 0;
  13199. }
  13200. BUILDIN_FUNC(waitingroom2bg_single)
  13201. {
  13202. const char* map_name;
  13203. struct npc_data *nd;
  13204. struct chat_data *cd;
  13205. struct map_session_data *sd;
  13206. int x, y, mapindex, bg_id;
  13207. bg_id = script_getnum(st,2);
  13208. map_name = script_getstr(st,3);
  13209. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13210. return 0; // Invalid Map
  13211. x = script_getnum(st,4);
  13212. y = script_getnum(st,5);
  13213. nd = npc_name2id(script_getstr(st,6));
  13214. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  13215. return 0;
  13216. if( (sd = cd->usersd[0]) == NULL )
  13217. return 0;
  13218. if( bg_team_join(bg_id, sd) )
  13219. {
  13220. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  13221. script_pushint(st,1);
  13222. }
  13223. else
  13224. script_pushint(st,0);
  13225. return 0;
  13226. }
  13227. BUILDIN_FUNC(bg_team_setxy)
  13228. {
  13229. struct battleground_data *bg;
  13230. int bg_id;
  13231. bg_id = script_getnum(st,2);
  13232. if( (bg = bg_team_search(bg_id)) == NULL )
  13233. return 0;
  13234. bg->x = script_getnum(st,3);
  13235. bg->y = script_getnum(st,4);
  13236. return 0;
  13237. }
  13238. BUILDIN_FUNC(bg_warp)
  13239. {
  13240. int x, y, mapindex, bg_id;
  13241. const char* map_name;
  13242. bg_id = script_getnum(st,2);
  13243. map_name = script_getstr(st,3);
  13244. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13245. return 0; // Invalid Map
  13246. x = script_getnum(st,4);
  13247. y = script_getnum(st,5);
  13248. bg_team_warp(bg_id, mapindex, x, y);
  13249. return 0;
  13250. }
  13251. BUILDIN_FUNC(bg_monster)
  13252. {
  13253. int class_ = 0, x = 0, y = 0, bg_id = 0;
  13254. const char *str,*map, *evt="";
  13255. bg_id = script_getnum(st,2);
  13256. map = script_getstr(st,3);
  13257. x = script_getnum(st,4);
  13258. y = script_getnum(st,5);
  13259. str = script_getstr(st,6);
  13260. class_ = script_getnum(st,7);
  13261. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  13262. check_event(st, evt);
  13263. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  13264. return 0;
  13265. }
  13266. BUILDIN_FUNC(bg_monster_set_team)
  13267. {
  13268. struct mob_data *md;
  13269. struct block_list *mbl;
  13270. int id = script_getnum(st,2),
  13271. bg_id = script_getnum(st,3);
  13272. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  13273. return 0;
  13274. md = (TBL_MOB *)mbl;
  13275. md->bg_id = bg_id;
  13276. mob_stop_attack(md);
  13277. mob_stop_walking(md, 0);
  13278. md->target_id = md->attacked_id = 0;
  13279. clif_charnameack(0, &md->bl);
  13280. return 0;
  13281. }
  13282. BUILDIN_FUNC(bg_leave)
  13283. {
  13284. struct map_session_data *sd = script_rid2sd(st);
  13285. if( sd == NULL || !sd->bg_id )
  13286. return 0;
  13287. bg_team_leave(sd,0);
  13288. return 0;
  13289. }
  13290. BUILDIN_FUNC(bg_destroy)
  13291. {
  13292. int bg_id = script_getnum(st,2);
  13293. bg_team_delete(bg_id);
  13294. return 0;
  13295. }
  13296. BUILDIN_FUNC(bg_getareausers)
  13297. {
  13298. const char *str;
  13299. int m, x0, y0, x1, y1, bg_id;
  13300. int i = 0, c = 0;
  13301. struct battleground_data *bg = NULL;
  13302. struct map_session_data *sd;
  13303. bg_id = script_getnum(st,2);
  13304. str = script_getstr(st,3);
  13305. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  13306. {
  13307. script_pushint(st,0);
  13308. return 0;
  13309. }
  13310. x0 = script_getnum(st,4);
  13311. y0 = script_getnum(st,5);
  13312. x1 = script_getnum(st,6);
  13313. y1 = script_getnum(st,7);
  13314. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  13315. {
  13316. if( (sd = bg->members[i].sd) == NULL )
  13317. continue;
  13318. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  13319. continue;
  13320. c++;
  13321. }
  13322. script_pushint(st,c);
  13323. return 0;
  13324. }
  13325. BUILDIN_FUNC(bg_updatescore)
  13326. {
  13327. const char *str;
  13328. int m;
  13329. str = script_getstr(st,2);
  13330. if( (m = map_mapname2mapid(str)) < 0 )
  13331. return 0;
  13332. map[m].bgscore_lion = script_getnum(st,3);
  13333. map[m].bgscore_eagle = script_getnum(st,4);
  13334. clif_bg_updatescore(m);
  13335. return 0;
  13336. }
  13337. BUILDIN_FUNC(bg_get_data)
  13338. {
  13339. struct battleground_data *bg;
  13340. int bg_id = script_getnum(st,2),
  13341. type = script_getnum(st,3);
  13342. if( (bg = bg_team_search(bg_id)) == NULL )
  13343. {
  13344. script_pushint(st,0);
  13345. return 0;
  13346. }
  13347. switch( type )
  13348. {
  13349. case 0: script_pushint(st, bg->count); break;
  13350. default:
  13351. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  13352. break;
  13353. }
  13354. return 0;
  13355. }
  13356. /*==========================================
  13357. * Instancing Script Commands
  13358. *------------------------------------------*/
  13359. BUILDIN_FUNC(instance_create)
  13360. {
  13361. const char *name;
  13362. int party_id, res;
  13363. name = script_getstr(st, 2);
  13364. party_id = script_getnum(st, 3);
  13365. res = instance_create(party_id, name);
  13366. if( res == -4 ) // Already exists
  13367. {
  13368. script_pushint(st, -1);
  13369. return 0;
  13370. }
  13371. else if( res < 0 )
  13372. {
  13373. const char *err;
  13374. switch(res)
  13375. {
  13376. case -3: err = "No free instances"; break;
  13377. case -2: err = "Invalid party ID"; break;
  13378. case -1: err = "Invalid type"; break;
  13379. default: err = "Unknown"; break;
  13380. }
  13381. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  13382. script_pushint(st, -2);
  13383. return 0;
  13384. }
  13385. script_pushint(st, res);
  13386. return 0;
  13387. }
  13388. BUILDIN_FUNC(instance_destroy)
  13389. {
  13390. int instance_id;
  13391. struct map_session_data *sd;
  13392. struct party_data *p;
  13393. if( script_hasdata(st, 2) )
  13394. instance_id = script_getnum(st, 2);
  13395. else if( st->instance_id )
  13396. instance_id = st->instance_id;
  13397. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13398. instance_id = p->instance_id;
  13399. else return 0;
  13400. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13401. {
  13402. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  13403. return 0;
  13404. }
  13405. instance_destroy(instance_id);
  13406. return 0;
  13407. }
  13408. BUILDIN_FUNC(instance_attachmap)
  13409. {
  13410. const char *name;
  13411. int m;
  13412. int instance_id;
  13413. bool usebasename = false;
  13414. name = script_getstr(st,2);
  13415. instance_id = script_getnum(st,3);
  13416. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  13417. usebasename = true;
  13418. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  13419. {
  13420. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  13421. script_pushconststr(st, "");
  13422. return 0;
  13423. }
  13424. script_pushconststr(st, map[m].name);
  13425. return 0;
  13426. }
  13427. BUILDIN_FUNC(instance_detachmap)
  13428. {
  13429. struct map_session_data *sd;
  13430. struct party_data *p;
  13431. const char *str;
  13432. int m, instance_id;
  13433. str = script_getstr(st, 2);
  13434. if( script_hasdata(st, 3) )
  13435. instance_id = script_getnum(st, 3);
  13436. else if( st->instance_id )
  13437. instance_id = st->instance_id;
  13438. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13439. instance_id = p->instance_id;
  13440. else return 0;
  13441. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  13442. {
  13443. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  13444. return 0;
  13445. }
  13446. instance_del_map(m);
  13447. return 0;
  13448. }
  13449. BUILDIN_FUNC(instance_attach)
  13450. {
  13451. int instance_id;
  13452. instance_id = script_getnum(st, 2);
  13453. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13454. return 0;
  13455. st->instance_id = instance_id;
  13456. return 0;
  13457. }
  13458. BUILDIN_FUNC(instance_id)
  13459. {
  13460. int type, instance_id;
  13461. struct map_session_data *sd;
  13462. struct party_data *p;
  13463. if( script_hasdata(st, 2) )
  13464. {
  13465. type = script_getnum(st, 2);
  13466. if( type == 0 )
  13467. instance_id = st->instance_id;
  13468. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  13469. instance_id = p->instance_id;
  13470. else
  13471. instance_id = 0;
  13472. }
  13473. else
  13474. instance_id = st->instance_id;
  13475. script_pushint(st, instance_id);
  13476. return 0;
  13477. }
  13478. BUILDIN_FUNC(instance_set_timeout)
  13479. {
  13480. int progress_timeout, idle_timeout;
  13481. int instance_id;
  13482. struct map_session_data *sd;
  13483. struct party_data *p;
  13484. progress_timeout = script_getnum(st, 2);
  13485. idle_timeout = script_getnum(st, 3);
  13486. if( script_hasdata(st, 4) )
  13487. instance_id = script_getnum(st, 4);
  13488. else if( st->instance_id )
  13489. instance_id = st->instance_id;
  13490. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13491. instance_id = p->instance_id;
  13492. else return 0;
  13493. if( instance_id > 0 )
  13494. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  13495. return 0;
  13496. }
  13497. BUILDIN_FUNC(instance_init)
  13498. {
  13499. int instance_id = script_getnum(st, 2);
  13500. if( instance[instance_id].state != INSTANCE_IDLE )
  13501. {
  13502. ShowError("instance_init: instance already initialized.\n");
  13503. return 0;
  13504. }
  13505. instance_init(instance_id);
  13506. return 0;
  13507. }
  13508. BUILDIN_FUNC(instance_announce)
  13509. {
  13510. int instance_id = script_getnum(st,2);
  13511. const char *mes = script_getstr(st,3);
  13512. int flag = script_getnum(st,4);
  13513. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  13514. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  13515. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  13516. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  13517. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  13518. int i;
  13519. struct map_session_data *sd;
  13520. struct party_data *p;
  13521. if( instance_id == 0 )
  13522. {
  13523. if( st->instance_id )
  13524. instance_id = st->instance_id;
  13525. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13526. instance_id = p->instance_id;
  13527. else return 0;
  13528. }
  13529. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13530. return 0;
  13531. for( i = 0; i < instance[instance_id].num_map; i++ )
  13532. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  13533. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  13534. return 0;
  13535. }
  13536. BUILDIN_FUNC(instance_npcname)
  13537. {
  13538. const char *str;
  13539. int instance_id = 0;
  13540. struct map_session_data *sd;
  13541. struct party_data *p;
  13542. struct npc_data *nd;
  13543. str = script_getstr(st, 2);
  13544. if( script_hasdata(st, 3) )
  13545. instance_id = script_getnum(st, 3);
  13546. else if( st->instance_id )
  13547. instance_id = st->instance_id;
  13548. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13549. instance_id = p->instance_id;
  13550. if( instance_id && (nd = npc_name2id(str)) != NULL )
  13551. {
  13552. static char npcname[NAME_LENGTH];
  13553. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  13554. script_pushconststr(st,npcname);
  13555. }
  13556. else
  13557. {
  13558. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  13559. st->state = END;
  13560. return 1;
  13561. }
  13562. return 0;
  13563. }
  13564. BUILDIN_FUNC(has_instance)
  13565. {
  13566. struct map_session_data *sd;
  13567. struct party_data *p;
  13568. const char *str;
  13569. int m, instance_id = 0;
  13570. str = script_getstr(st, 2);
  13571. if( script_hasdata(st, 3) )
  13572. instance_id = script_getnum(st, 3);
  13573. else if( st->instance_id )
  13574. instance_id = st->instance_id;
  13575. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13576. instance_id = p->instance_id;
  13577. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  13578. {
  13579. script_pushconststr(st, "");
  13580. return 0;
  13581. }
  13582. script_pushconststr(st, map[m].name);
  13583. return 0;
  13584. }
  13585. BUILDIN_FUNC(instance_warpall)
  13586. {
  13587. struct map_session_data *pl_sd;
  13588. int m, i, instance_id;
  13589. const char *mapn;
  13590. int x, y;
  13591. unsigned short mapindex;
  13592. struct party_data *p = NULL;
  13593. mapn = script_getstr(st,2);
  13594. x = script_getnum(st,3);
  13595. y = script_getnum(st,4);
  13596. if( script_hasdata(st,5) )
  13597. instance_id = script_getnum(st,5);
  13598. else if( st->instance_id )
  13599. instance_id = st->instance_id;
  13600. 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 )
  13601. instance_id = p->instance_id;
  13602. else return 0;
  13603. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  13604. return 0;
  13605. if( !(p = party_search(instance[instance_id].party_id)) )
  13606. return 0;
  13607. mapindex = map_id2index(m);
  13608. for( i = 0; i < MAX_PARTY; i++ )
  13609. 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);
  13610. return 0;
  13611. }
  13612. /*==========================================
  13613. * Custom Fonts
  13614. *------------------------------------------*/
  13615. BUILDIN_FUNC(setfont)
  13616. {
  13617. struct map_session_data *sd = script_rid2sd(st);
  13618. int font = script_getnum(st,2);
  13619. if( sd == NULL )
  13620. return 0;
  13621. if( sd->user_font != font )
  13622. sd->user_font = font;
  13623. else
  13624. sd->user_font = 0;
  13625. clif_font(sd);
  13626. return 0;
  13627. }
  13628. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  13629. {
  13630. TBL_MOB* md = (TBL_MOB*)bl;
  13631. struct block_list *tbl;
  13632. int mobid = va_arg(ap,int);
  13633. int skillid = va_arg(ap,int);
  13634. int skilllv = va_arg(ap,int);
  13635. int casttime = va_arg(ap,int);
  13636. int cancel = va_arg(ap,int);
  13637. int emotion = va_arg(ap,int);
  13638. int target = va_arg(ap,int);
  13639. if( md->class_ != mobid )
  13640. return 0;
  13641. // 0:self, 1:target, 2:master, default:random
  13642. switch( target )
  13643. {
  13644. case 0: tbl = map_id2bl(md->bl.id); break;
  13645. case 1: tbl = map_id2bl(md->target_id); break;
  13646. case 2: tbl = map_id2bl(md->master_id); break;
  13647. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  13648. }
  13649. if( !tbl )
  13650. return 0;
  13651. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  13652. unit_skillcastcancel(bl,0);
  13653. if( skill_get_casttype(skillid) == CAST_GROUND )
  13654. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  13655. else
  13656. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  13657. clif_emotion(&md->bl, emotion);
  13658. return 0;
  13659. }
  13660. /*==========================================
  13661. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  13662. *------------------------------------------*/
  13663. BUILDIN_FUNC(areamobuseskill)
  13664. {
  13665. struct block_list center;
  13666. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  13667. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  13668. {
  13669. ShowError("areamobuseskill: invalid map name.\n");
  13670. return 0;
  13671. }
  13672. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  13673. return 0;
  13674. center.m = m;
  13675. center.x = script_getnum(st,3);
  13676. center.y = script_getnum(st,4);
  13677. range = script_getnum(st,5);
  13678. mobid = script_getnum(st,6);
  13679. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  13680. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  13681. skilllv = battle_config.mob_max_skilllvl;
  13682. casttime = script_getnum(st,9);
  13683. cancel = script_getnum(st,10);
  13684. emotion = script_getnum(st,11);
  13685. target = script_getnum(st,12);
  13686. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  13687. return 0;
  13688. }
  13689. BUILDIN_FUNC(progressbar)
  13690. {
  13691. #if PACKETVER >= 20080318
  13692. struct map_session_data * sd = script_rid2sd(st);
  13693. const char * color;
  13694. unsigned int second;
  13695. if( !st || !sd )
  13696. return 0;
  13697. st->state = STOP;
  13698. color = script_getstr(st,2);
  13699. second = script_getnum(st,3);
  13700. sd->progressbar.npc_id = st->oid;
  13701. sd->progressbar.timeout = gettick() + second*1000;
  13702. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  13703. #endif
  13704. return 0;
  13705. }
  13706. BUILDIN_FUNC(pushpc)
  13707. {
  13708. int direction, cells, dx, dy;
  13709. struct map_session_data* sd;
  13710. if((sd = script_rid2sd(st))==NULL)
  13711. {
  13712. return 0;
  13713. }
  13714. direction = script_getnum(st,2);
  13715. cells = script_getnum(st,3);
  13716. if(direction<0 || direction>7)
  13717. {
  13718. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  13719. script_reportsrc(st);
  13720. direction%= 8; // trim spin-over
  13721. }
  13722. if(!cells)
  13723. {// zero distance
  13724. return 0;
  13725. }
  13726. else if(cells<0)
  13727. {// pushing backwards
  13728. direction = (direction+4)%8; // turn around
  13729. cells = -cells;
  13730. }
  13731. dx = dirx[direction];
  13732. dy = diry[direction];
  13733. unit_blown(&sd->bl, dx, dy, cells, 0);
  13734. return 0;
  13735. }
  13736. /// Invokes buying store preparation window
  13737. /// buyingstore <slots>;
  13738. BUILDIN_FUNC(buyingstore)
  13739. {
  13740. struct map_session_data* sd;
  13741. if( ( sd = script_rid2sd(st) ) == NULL )
  13742. {
  13743. return 0;
  13744. }
  13745. buyingstore_setup(sd, script_getnum(st,2));
  13746. return 0;
  13747. }
  13748. /// Invokes search store info window
  13749. /// searchstores <uses>,<effect>;
  13750. BUILDIN_FUNC(searchstores)
  13751. {
  13752. unsigned short effect;
  13753. unsigned int uses;
  13754. struct map_session_data* sd;
  13755. if( ( sd = script_rid2sd(st) ) == NULL )
  13756. {
  13757. return 0;
  13758. }
  13759. uses = script_getnum(st,2);
  13760. effect = script_getnum(st,3);
  13761. if( !uses )
  13762. {
  13763. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  13764. return 1;
  13765. }
  13766. if( effect > 1 )
  13767. {
  13768. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  13769. return 1;
  13770. }
  13771. searchstore_open(sd, uses, effect);
  13772. return 0;
  13773. }
  13774. /// Displays a number as large digital clock.
  13775. /// showdigit <value>[,<type>];
  13776. BUILDIN_FUNC(showdigit)
  13777. {
  13778. unsigned int type = 0;
  13779. int value;
  13780. struct map_session_data* sd;
  13781. if( ( sd = script_rid2sd(st) ) == NULL )
  13782. {
  13783. return 0;
  13784. }
  13785. value = script_getnum(st,2);
  13786. if( script_hasdata(st,3) )
  13787. {
  13788. type = script_getnum(st,3);
  13789. if( type > 3 )
  13790. {
  13791. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  13792. return 1;
  13793. }
  13794. }
  13795. clif_showdigit(sd, (unsigned char)type, value);
  13796. return 0;
  13797. }
  13798. /**
  13799. * Rune Knight
  13800. **/
  13801. BUILDIN_FUNC(makerune) {
  13802. TBL_PC* sd;
  13803. if( (sd = script_rid2sd(st)) == NULL )
  13804. return 0;
  13805. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  13806. sd->itemid = script_getnum(st,2);
  13807. return 0;
  13808. }
  13809. /**
  13810. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  13811. **/
  13812. BUILDIN_FUNC(checkdragon) {
  13813. TBL_PC* sd;
  13814. if( (sd = script_rid2sd(st)) == NULL )
  13815. return 0;
  13816. if( sd->sc.option&OPTION_DRAGON )
  13817. script_pushint(st,1);
  13818. else
  13819. script_pushint(st,0);
  13820. return 0;
  13821. }
  13822. /**
  13823. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  13824. * - Toggles the dragon on a RK if he can mount;
  13825. * @param Color - when not provided uses the green dragon;
  13826. * - 1 : Green Dragon
  13827. * - 2 : Brown Dragon
  13828. * - 3 : Gray Dragon
  13829. * - 4 : Blue Dragon
  13830. * - 5 : Red Dragon
  13831. **/
  13832. BUILDIN_FUNC(setdragon) {
  13833. TBL_PC* sd;
  13834. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  13835. unsigned int option = OPTION_DRAGON1;
  13836. if( (sd = script_rid2sd(st)) == NULL )
  13837. return 0;
  13838. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  13839. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  13840. else if ( sd->sc.option&OPTION_DRAGON ) {//Is mounted; release
  13841. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  13842. script_pushint(st,1);
  13843. } else {//Not mounted; Mount now.
  13844. if( color ) {
  13845. option = ( color == 1 ? OPTION_DRAGON1 :
  13846. color == 2 ? OPTION_DRAGON2 :
  13847. color == 3 ? OPTION_DRAGON3 :
  13848. color == 4 ? OPTION_DRAGON4 :
  13849. color == 5 ? OPTION_DRAGON5 : 0);
  13850. if( !option ) {
  13851. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  13852. option = OPTION_DRAGON1;
  13853. }
  13854. }
  13855. pc_setoption(sd, sd->sc.option|option);
  13856. script_pushint(st,1);
  13857. }
  13858. return 0;
  13859. }
  13860. /**
  13861. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  13862. **/
  13863. BUILDIN_FUNC(ismounting) {
  13864. TBL_PC* sd;
  13865. if( (sd = script_rid2sd(st)) == NULL )
  13866. return 0;
  13867. if( sd->sc.option&OPTION_MOUNTING )
  13868. script_pushint(st,1);
  13869. else
  13870. script_pushint(st,0);
  13871. return 0;
  13872. }
  13873. /**
  13874. * setmounting() returns 1 on success or 0 otherwise
  13875. * - Toggles new mounts on a player when he can mount
  13876. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  13877. * - Will unmount the player is he is already mounting
  13878. **/
  13879. BUILDIN_FUNC(setmounting) {
  13880. TBL_PC* sd;
  13881. if( (sd = script_rid2sd(st)) == NULL )
  13882. return 0;
  13883. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  13884. script_pushint(st,0);//can't mount with one of these
  13885. else {
  13886. if( sd->sc.option&OPTION_MOUNTING )
  13887. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  13888. else
  13889. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  13890. script_pushint(st,1);//in both cases, return 1.
  13891. }
  13892. return 0;
  13893. }
  13894. // declarations that were supposed to be exported from npc_chat.c
  13895. #ifdef PCRE_SUPPORT
  13896. BUILDIN_FUNC(defpattern);
  13897. BUILDIN_FUNC(activatepset);
  13898. BUILDIN_FUNC(deactivatepset);
  13899. BUILDIN_FUNC(deletepset);
  13900. #endif
  13901. /// script command definitions
  13902. /// for an explanation on args, see add_buildin_func
  13903. struct script_function buildin_func[] = {
  13904. // NPC interaction
  13905. BUILDIN_DEF(mes,"s"),
  13906. BUILDIN_DEF(next,""),
  13907. BUILDIN_DEF(close,""),
  13908. BUILDIN_DEF(close2,""),
  13909. BUILDIN_DEF(menu,"sl*"),
  13910. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  13911. BUILDIN_DEF(prompt,"s*"),
  13912. //
  13913. BUILDIN_DEF(goto,"l"),
  13914. BUILDIN_DEF(callsub,"l*"),
  13915. BUILDIN_DEF(callfunc,"s*"),
  13916. BUILDIN_DEF(return,"?"),
  13917. BUILDIN_DEF(getarg,"i?"),
  13918. BUILDIN_DEF(jobchange,"i?"),
  13919. BUILDIN_DEF(jobname,"i"),
  13920. BUILDIN_DEF(input,"r??"),
  13921. BUILDIN_DEF(warp,"sii"),
  13922. BUILDIN_DEF(areawarp,"siiiisii"),
  13923. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  13924. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  13925. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  13926. BUILDIN_DEF(setlook,"ii"),
  13927. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  13928. BUILDIN_DEF(set,"rv"),
  13929. BUILDIN_DEF(setarray,"rv*"),
  13930. BUILDIN_DEF(cleararray,"rvi"),
  13931. BUILDIN_DEF(copyarray,"rri"),
  13932. BUILDIN_DEF(getarraysize,"r"),
  13933. BUILDIN_DEF(deletearray,"r?"),
  13934. BUILDIN_DEF(getelementofarray,"ri"),
  13935. BUILDIN_DEF(getitem,"vi?"),
  13936. BUILDIN_DEF(rentitem,"vi"),
  13937. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  13938. BUILDIN_DEF(getnameditem,"vv"),
  13939. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  13940. BUILDIN_DEF(makeitem,"visii"),
  13941. BUILDIN_DEF(delitem,"vi?"),
  13942. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  13943. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  13944. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  13945. BUILDIN_DEF(cutin,"si"),
  13946. BUILDIN_DEF(viewpoint,"iiiii"),
  13947. BUILDIN_DEF(heal,"ii"),
  13948. BUILDIN_DEF(itemheal,"ii"),
  13949. BUILDIN_DEF(percentheal,"ii"),
  13950. BUILDIN_DEF(rand,"i?"),
  13951. BUILDIN_DEF(countitem,"v"),
  13952. BUILDIN_DEF(countitem2,"viiiiiii"),
  13953. BUILDIN_DEF(checkweight,"vi"),
  13954. BUILDIN_DEF(readparam,"i?"),
  13955. BUILDIN_DEF(getcharid,"i?"),
  13956. BUILDIN_DEF(getpartyname,"i"),
  13957. BUILDIN_DEF(getpartymember,"i?"),
  13958. BUILDIN_DEF(getpartyleader,"i?"),
  13959. BUILDIN_DEF(getguildname,"i"),
  13960. BUILDIN_DEF(getguildmaster,"i"),
  13961. BUILDIN_DEF(getguildmasterid,"i"),
  13962. BUILDIN_DEF(strcharinfo,"i"),
  13963. BUILDIN_DEF(strnpcinfo,"i"),
  13964. BUILDIN_DEF(getequipid,"i"),
  13965. BUILDIN_DEF(getequipname,"i"),
  13966. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  13967. BUILDIN_DEF(repair,"i"), // [Valaris]
  13968. BUILDIN_DEF(getequipisequiped,"i"),
  13969. BUILDIN_DEF(getequipisenableref,"i"),
  13970. BUILDIN_DEF(getequipisidentify,"i"),
  13971. BUILDIN_DEF(getequiprefinerycnt,"i"),
  13972. BUILDIN_DEF(getequipweaponlv,"i"),
  13973. BUILDIN_DEF(getequippercentrefinery,"i"),
  13974. BUILDIN_DEF(successrefitem,"i"),
  13975. BUILDIN_DEF(failedrefitem,"i"),
  13976. BUILDIN_DEF(statusup,"i"),
  13977. BUILDIN_DEF(statusup2,"ii"),
  13978. BUILDIN_DEF(bonus,"iv"),
  13979. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  13980. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  13981. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  13982. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  13983. BUILDIN_DEF(autobonus,"sii??"),
  13984. BUILDIN_DEF(autobonus2,"sii??"),
  13985. BUILDIN_DEF(autobonus3,"siiv?"),
  13986. BUILDIN_DEF(skill,"vi?"),
  13987. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  13988. BUILDIN_DEF(guildskill,"vi"),
  13989. BUILDIN_DEF(getskilllv,"v"),
  13990. BUILDIN_DEF(getgdskilllv,"iv"),
  13991. BUILDIN_DEF(basicskillcheck,""),
  13992. BUILDIN_DEF(getgmlevel,""),
  13993. BUILDIN_DEF(end,""),
  13994. BUILDIN_DEF(checkoption,"i"),
  13995. BUILDIN_DEF(setoption,"i?"),
  13996. BUILDIN_DEF(setcart,"?"),
  13997. BUILDIN_DEF(checkcart,""),
  13998. BUILDIN_DEF(setfalcon,"?"),
  13999. BUILDIN_DEF(checkfalcon,""),
  14000. BUILDIN_DEF(setriding,"?"),
  14001. BUILDIN_DEF(checkriding,""),
  14002. BUILDIN_DEF2(savepoint,"save","sii"),
  14003. BUILDIN_DEF(savepoint,"sii"),
  14004. BUILDIN_DEF(gettimetick,"i"),
  14005. BUILDIN_DEF(gettime,"i"),
  14006. BUILDIN_DEF(gettimestr,"si"),
  14007. BUILDIN_DEF(openstorage,""),
  14008. BUILDIN_DEF(guildopenstorage,""),
  14009. BUILDIN_DEF(itemskill,"vi"),
  14010. BUILDIN_DEF(produce,"i"),
  14011. BUILDIN_DEF(cooking,"i"),
  14012. BUILDIN_DEF(monster,"siisii?"),
  14013. BUILDIN_DEF(getmobdrops,"i"),
  14014. BUILDIN_DEF(areamonster,"siiiisii?"),
  14015. BUILDIN_DEF(killmonster,"ss?"),
  14016. BUILDIN_DEF(killmonsterall,"s?"),
  14017. BUILDIN_DEF(clone,"siisi????"),
  14018. BUILDIN_DEF(doevent,"s"),
  14019. BUILDIN_DEF(donpcevent,"s"),
  14020. BUILDIN_DEF(cmdothernpc,"ss"),
  14021. BUILDIN_DEF(addtimer,"is"),
  14022. BUILDIN_DEF(deltimer,"s"),
  14023. BUILDIN_DEF(addtimercount,"si"),
  14024. BUILDIN_DEF(initnpctimer,"??"),
  14025. BUILDIN_DEF(stopnpctimer,"??"),
  14026. BUILDIN_DEF(startnpctimer,"??"),
  14027. BUILDIN_DEF(setnpctimer,"i?"),
  14028. BUILDIN_DEF(getnpctimer,"i?"),
  14029. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  14030. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  14031. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  14032. BUILDIN_DEF(announce,"si?????"),
  14033. BUILDIN_DEF(mapannounce,"ssi?????"),
  14034. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  14035. BUILDIN_DEF(getusers,"i"),
  14036. BUILDIN_DEF(getmapguildusers,"si"),
  14037. BUILDIN_DEF(getmapusers,"s"),
  14038. BUILDIN_DEF(getareausers,"siiii"),
  14039. BUILDIN_DEF(getareadropitem,"siiiiv"),
  14040. BUILDIN_DEF(enablenpc,"s"),
  14041. BUILDIN_DEF(disablenpc,"s"),
  14042. BUILDIN_DEF(hideoffnpc,"s"),
  14043. BUILDIN_DEF(hideonnpc,"s"),
  14044. BUILDIN_DEF(sc_start,"iii?"),
  14045. BUILDIN_DEF(sc_start2,"iiii?"),
  14046. BUILDIN_DEF(sc_start4,"iiiiii?"),
  14047. BUILDIN_DEF(sc_end,"i?"),
  14048. BUILDIN_DEF(getscrate,"ii?"),
  14049. BUILDIN_DEF(debugmes,"s"),
  14050. BUILDIN_DEF2(catchpet,"pet","i"),
  14051. BUILDIN_DEF2(birthpet,"bpet",""),
  14052. BUILDIN_DEF(resetlvl,"i"),
  14053. BUILDIN_DEF(resetstatus,""),
  14054. BUILDIN_DEF(resetskill,""),
  14055. BUILDIN_DEF(skillpointcount,""),
  14056. BUILDIN_DEF(changebase,"i?"),
  14057. BUILDIN_DEF(changesex,""),
  14058. BUILDIN_DEF(waitingroom,"si?????"),
  14059. BUILDIN_DEF(delwaitingroom,"?"),
  14060. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  14061. BUILDIN_DEF(enablewaitingroomevent,"?"),
  14062. BUILDIN_DEF(disablewaitingroomevent,"?"),
  14063. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  14064. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  14065. BUILDIN_DEF(getwaitingroomstate,"i?"),
  14066. BUILDIN_DEF(warpwaitingpc,"sii?"),
  14067. BUILDIN_DEF(attachrid,"i"),
  14068. BUILDIN_DEF(detachrid,""),
  14069. BUILDIN_DEF(isloggedin,"i?"),
  14070. BUILDIN_DEF(setmapflagnosave,"ssii"),
  14071. BUILDIN_DEF(getmapflag,"si"),
  14072. BUILDIN_DEF(setmapflag,"si?"),
  14073. BUILDIN_DEF(removemapflag,"si"),
  14074. BUILDIN_DEF(pvpon,"s"),
  14075. BUILDIN_DEF(pvpoff,"s"),
  14076. BUILDIN_DEF(gvgon,"s"),
  14077. BUILDIN_DEF(gvgoff,"s"),
  14078. BUILDIN_DEF(emotion,"i??"),
  14079. BUILDIN_DEF(maprespawnguildid,"sii"),
  14080. BUILDIN_DEF(agitstart,""), // <Agit>
  14081. BUILDIN_DEF(agitend,""),
  14082. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  14083. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  14084. BUILDIN_DEF(getcastlename,"s"),
  14085. BUILDIN_DEF(getcastledata,"si?"),
  14086. BUILDIN_DEF(setcastledata,"sii"),
  14087. BUILDIN_DEF(requestguildinfo,"i?"),
  14088. BUILDIN_DEF(getequipcardcnt,"i"),
  14089. BUILDIN_DEF(successremovecards,"i"),
  14090. BUILDIN_DEF(failedremovecards,"ii"),
  14091. BUILDIN_DEF(marriage,"s"),
  14092. BUILDIN_DEF2(wedding_effect,"wedding",""),
  14093. BUILDIN_DEF(divorce,""),
  14094. BUILDIN_DEF(ispartneron,""),
  14095. BUILDIN_DEF(getpartnerid,""),
  14096. BUILDIN_DEF(getchildid,""),
  14097. BUILDIN_DEF(getmotherid,""),
  14098. BUILDIN_DEF(getfatherid,""),
  14099. BUILDIN_DEF(warppartner,"sii"),
  14100. BUILDIN_DEF(getitemname,"v"),
  14101. BUILDIN_DEF(getitemslots,"i"),
  14102. BUILDIN_DEF(makepet,"i"),
  14103. BUILDIN_DEF(getexp,"ii"),
  14104. BUILDIN_DEF(getinventorylist,""),
  14105. BUILDIN_DEF(getskilllist,""),
  14106. BUILDIN_DEF(clearitem,""),
  14107. BUILDIN_DEF(classchange,"ii"),
  14108. BUILDIN_DEF(misceffect,"i"),
  14109. BUILDIN_DEF(playBGM,"s"),
  14110. BUILDIN_DEF(playBGMall,"s?????"),
  14111. BUILDIN_DEF(soundeffect,"si"),
  14112. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  14113. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  14114. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  14115. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  14116. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  14117. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  14118. BUILDIN_DEF(petloot,"i"), // [Valaris]
  14119. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  14120. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  14121. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  14122. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  14123. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  14124. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  14125. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  14126. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  14127. BUILDIN_DEF(nude,""), // nude command [Valaris]
  14128. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  14129. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  14130. BUILDIN_DEF(charcommand,"s"), // [MouseJstr]
  14131. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  14132. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  14133. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  14134. BUILDIN_DEF(mobcount,"ss"),
  14135. BUILDIN_DEF(getlook,"i"),
  14136. BUILDIN_DEF(getsavepoint,"i"),
  14137. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  14138. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  14139. BUILDIN_DEF(npcstop,""), // [Valaris]
  14140. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  14141. BUILDIN_DEF(checkoption1,"i"),
  14142. BUILDIN_DEF(checkoption2,"i"),
  14143. BUILDIN_DEF(guildgetexp,"i"),
  14144. BUILDIN_DEF(guildchangegm,"is"),
  14145. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  14146. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  14147. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  14148. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  14149. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  14150. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  14151. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  14152. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  14153. BUILDIN_DEF(night,""), // sets the server to night time
  14154. BUILDIN_DEF(day,""), // sets the server to day time
  14155. #ifdef PCRE_SUPPORT
  14156. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  14157. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  14158. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  14159. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  14160. #endif
  14161. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  14162. BUILDIN_DEF(getusersname,""),
  14163. BUILDIN_DEF(recovery,""),
  14164. BUILDIN_DEF(getpetinfo,"i"),
  14165. BUILDIN_DEF(gethominfo,"i"),
  14166. BUILDIN_DEF(getmercinfo,"i?"),
  14167. BUILDIN_DEF(checkequipedcard,"i"),
  14168. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  14169. BUILDIN_DEF(globalmes,"s?"),
  14170. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  14171. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  14172. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  14173. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  14174. BUILDIN_DEF(charat,"si"),
  14175. BUILDIN_DEF(setchar,"ssi"),
  14176. BUILDIN_DEF(insertchar,"ssi"),
  14177. BUILDIN_DEF(delchar,"si"),
  14178. BUILDIN_DEF(strtoupper,"s"),
  14179. BUILDIN_DEF(strtolower,"s"),
  14180. BUILDIN_DEF(charisupper, "si"),
  14181. BUILDIN_DEF(charislower, "si"),
  14182. BUILDIN_DEF(substr,"sii"),
  14183. BUILDIN_DEF(explode, "rss"),
  14184. BUILDIN_DEF(implode, "r?"),
  14185. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  14186. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  14187. BUILDIN_DEF(strpos,"ss?"),
  14188. BUILDIN_DEF(replacestr,"sss??"),
  14189. BUILDIN_DEF(countstr,"ss?"),
  14190. BUILDIN_DEF(setnpcdisplay,"sv??"),
  14191. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  14192. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  14193. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  14194. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  14195. // [zBuffer] List of mathematics commands --->
  14196. BUILDIN_DEF(sqrt,"i"),
  14197. BUILDIN_DEF(pow,"ii"),
  14198. BUILDIN_DEF(distance,"iiii"),
  14199. // <--- [zBuffer] List of mathematics commands
  14200. BUILDIN_DEF(md5,"s"),
  14201. // [zBuffer] List of dynamic var commands --->
  14202. BUILDIN_DEF(getd,"s"),
  14203. BUILDIN_DEF(setd,"sv"),
  14204. // <--- [zBuffer] List of dynamic var commands
  14205. BUILDIN_DEF(petstat,"i"),
  14206. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  14207. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  14208. BUILDIN_DEF(npcshopadditem,"sii*"),
  14209. BUILDIN_DEF(npcshopdelitem,"si*"),
  14210. BUILDIN_DEF(npcshopattach,"s?"),
  14211. BUILDIN_DEF(equip,"i"),
  14212. BUILDIN_DEF(autoequip,"ii"),
  14213. BUILDIN_DEF(setbattleflag,"si"),
  14214. BUILDIN_DEF(getbattleflag,"s"),
  14215. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  14216. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  14217. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  14218. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  14219. BUILDIN_DEF(axtoi,"s"),
  14220. BUILDIN_DEF(query_sql,"s*"),
  14221. BUILDIN_DEF(query_logsql,"s*"),
  14222. BUILDIN_DEF(escape_sql,"s"),
  14223. BUILDIN_DEF(atoi,"s"),
  14224. // [zBuffer] List of player cont commands --->
  14225. BUILDIN_DEF(rid2name,"i"),
  14226. BUILDIN_DEF(pcfollow,"ii"),
  14227. BUILDIN_DEF(pcstopfollow,"i"),
  14228. BUILDIN_DEF(pcblockmove,"ii"),
  14229. // <--- [zBuffer] List of player cont commands
  14230. // [zBuffer] List of mob control commands --->
  14231. BUILDIN_DEF(unitwalk,"ii?"),
  14232. BUILDIN_DEF(unitkill,"i"),
  14233. BUILDIN_DEF(unitwarp,"isii"),
  14234. BUILDIN_DEF(unitattack,"iv?"),
  14235. BUILDIN_DEF(unitstop,"i"),
  14236. BUILDIN_DEF(unittalk,"is"),
  14237. BUILDIN_DEF(unitemote,"ii"),
  14238. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  14239. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  14240. // <--- [zBuffer] List of mob control commands
  14241. BUILDIN_DEF(sleep,"i"),
  14242. BUILDIN_DEF(sleep2,"i"),
  14243. BUILDIN_DEF(awake,"s"),
  14244. BUILDIN_DEF(getvariableofnpc,"rs"),
  14245. BUILDIN_DEF(warpportal,"iisii"),
  14246. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  14247. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  14248. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  14249. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  14250. BUILDIN_DEF(checkvending,"?"),
  14251. BUILDIN_DEF(checkchatting,"?"),
  14252. BUILDIN_DEF(openmail,""),
  14253. BUILDIN_DEF(openauction,""),
  14254. BUILDIN_DEF(checkcell,"siii"),
  14255. BUILDIN_DEF(setcell,"siiiiii"),
  14256. BUILDIN_DEF(setwall,"siiiiis"),
  14257. BUILDIN_DEF(delwall,"s"),
  14258. BUILDIN_DEF(searchitem,"rs"),
  14259. BUILDIN_DEF(mercenary_create,"ii"),
  14260. BUILDIN_DEF(mercenary_heal,"ii"),
  14261. BUILDIN_DEF(mercenary_sc_start,"iii"),
  14262. BUILDIN_DEF(mercenary_get_calls,"i"),
  14263. BUILDIN_DEF(mercenary_get_faith,"i"),
  14264. BUILDIN_DEF(mercenary_set_calls,"ii"),
  14265. BUILDIN_DEF(mercenary_set_faith,"ii"),
  14266. BUILDIN_DEF(readbook,"ii"),
  14267. BUILDIN_DEF(setfont,"i"),
  14268. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  14269. BUILDIN_DEF(progressbar,"si"),
  14270. BUILDIN_DEF(pushpc,"ii"),
  14271. BUILDIN_DEF(buyingstore,"i"),
  14272. BUILDIN_DEF(searchstores,"ii"),
  14273. BUILDIN_DEF(showdigit,"i?"),
  14274. // WoE SE
  14275. BUILDIN_DEF(agitstart2,""),
  14276. BUILDIN_DEF(agitend2,""),
  14277. BUILDIN_DEF(agitcheck2,""),
  14278. // BattleGround
  14279. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  14280. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  14281. BUILDIN_DEF(bg_team_setxy,"iii"),
  14282. BUILDIN_DEF(bg_warp,"isii"),
  14283. BUILDIN_DEF(bg_monster,"isiisi?"),
  14284. BUILDIN_DEF(bg_monster_set_team,"ii"),
  14285. BUILDIN_DEF(bg_leave,""),
  14286. BUILDIN_DEF(bg_destroy,"i"),
  14287. BUILDIN_DEF(areapercentheal,"siiiiii"),
  14288. BUILDIN_DEF(bg_get_data,"ii"),
  14289. BUILDIN_DEF(bg_getareausers,"isiiii"),
  14290. BUILDIN_DEF(bg_updatescore,"sii"),
  14291. // Instancing
  14292. BUILDIN_DEF(instance_create,"si"),
  14293. BUILDIN_DEF(instance_destroy,"?"),
  14294. BUILDIN_DEF(instance_attachmap,"si?"),
  14295. BUILDIN_DEF(instance_detachmap,"s?"),
  14296. BUILDIN_DEF(instance_attach,"i"),
  14297. BUILDIN_DEF(instance_id,"?"),
  14298. BUILDIN_DEF(instance_set_timeout,"ii?"),
  14299. BUILDIN_DEF(instance_init,"i"),
  14300. BUILDIN_DEF(instance_announce,"isi?????"),
  14301. BUILDIN_DEF(instance_npcname,"s?"),
  14302. BUILDIN_DEF(has_instance,"s?"),
  14303. BUILDIN_DEF(instance_warpall,"sii?"),
  14304. /**
  14305. * 3rd-related
  14306. **/
  14307. BUILDIN_DEF(makerune,"i"),
  14308. BUILDIN_DEF(checkdragon,""),//[Ind]
  14309. BUILDIN_DEF(setdragon,"?"),//[Ind]
  14310. BUILDIN_DEF(ismounting,""),//[Ind]
  14311. BUILDIN_DEF(setmounting,""),//[Ind]
  14312. //Quest Log System [Inkfish]
  14313. BUILDIN_DEF(setquest, "i"),
  14314. BUILDIN_DEF(erasequest, "i"),
  14315. BUILDIN_DEF(completequest, "i"),
  14316. BUILDIN_DEF(checkquest, "i?"),
  14317. BUILDIN_DEF(changequest, "ii"),
  14318. BUILDIN_DEF(showevent, "ii"),
  14319. {NULL,NULL,NULL},
  14320. };