script.c 398 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402
  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. } else {
  2132. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2133. }
  2134. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2135. }
  2136. break;
  2137. default:
  2138. data->u.str = pc_readglobalreg_str(sd, name);
  2139. break;
  2140. }
  2141. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2142. {// empty string
  2143. data->type = C_CONSTSTR;
  2144. data->u.str = "";
  2145. }
  2146. else
  2147. {// duplicate string
  2148. data->type = C_STR;
  2149. data->u.str = aStrdup(data->u.str);
  2150. }
  2151. }
  2152. else
  2153. {// integer variable
  2154. data->type = C_INT;
  2155. if( reference_toconstant(data) )
  2156. {
  2157. data->u.num = reference_getconstant(data);
  2158. }
  2159. else if( reference_toparam(data) )
  2160. {
  2161. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2162. }
  2163. else
  2164. switch( prefix )
  2165. {
  2166. case '@':
  2167. data->u.num = pc_readreg(sd, data->u.num);
  2168. break;
  2169. case '$':
  2170. data->u.num = mapreg_readreg(data->u.num);
  2171. break;
  2172. case '#':
  2173. if( name[1] == '#' )
  2174. data->u.num = pc_readaccountreg2(sd, name);// global
  2175. else
  2176. data->u.num = pc_readaccountreg(sd, name);// local
  2177. break;
  2178. case '.':
  2179. {
  2180. struct linkdb_node** n =
  2181. data->ref ? data->ref:
  2182. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2183. &st->script->script_vars;// npc variable
  2184. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2185. }
  2186. break;
  2187. case '\'':
  2188. {
  2189. struct linkdb_node** n = NULL;
  2190. if( st->instance_id )
  2191. n = &instance[st->instance_id].ivar;
  2192. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2193. }
  2194. break;
  2195. default:
  2196. data->u.num = pc_readglobalreg(sd, name);
  2197. break;
  2198. }
  2199. }
  2200. return;
  2201. }
  2202. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2203. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2204. /// The value is left in the top of the stack and needs to be removed manually.
  2205. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2206. {
  2207. struct script_data* data;
  2208. push_val2(st->stack, C_NAME, uid, ref);
  2209. data = script_getdatatop(st, -1);
  2210. get_val(st, data);
  2211. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2212. }
  2213. /*==========================================
  2214. * Stores the value of a script variable
  2215. * Return value is 0 on fail, 1 on success.
  2216. *------------------------------------------*/
  2217. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2218. {
  2219. char prefix = name[0];
  2220. if( is_string_variable(name) )
  2221. {// string variable
  2222. const char* str = (const char*)value;
  2223. switch (prefix) {
  2224. case '@':
  2225. return pc_setregstr(sd, num, str);
  2226. case '$':
  2227. return mapreg_setregstr(num, str);
  2228. case '#':
  2229. return (name[1] == '#') ?
  2230. pc_setaccountreg2str(sd, name, str) :
  2231. pc_setaccountregstr(sd, name, str);
  2232. case '.': {
  2233. char* p;
  2234. struct linkdb_node** n;
  2235. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2236. p = (char*)linkdb_erase(n, (void*)num);
  2237. if (p) aFree(p);
  2238. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2239. }
  2240. return 1;
  2241. case '\'': {
  2242. char *p;
  2243. struct linkdb_node** n = NULL;
  2244. if( st->instance_id )
  2245. n = &instance[st->instance_id].svar;
  2246. p = (char*)linkdb_erase(n, (void*)num);
  2247. if (p) aFree(p);
  2248. if( str[0] ) linkdb_insert(n, (void*)num, aStrdup(str));
  2249. }
  2250. return 1;
  2251. default:
  2252. return pc_setglobalreg_str(sd, name, str);
  2253. }
  2254. }
  2255. else
  2256. {// integer variable
  2257. int val = (int)value;
  2258. if(str_data[num&0x00ffffff].type == C_PARAM)
  2259. {
  2260. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2261. {
  2262. if( st != NULL )
  2263. {
  2264. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2265. script_reportsrc(st);
  2266. st->state = END;
  2267. }
  2268. return 0;
  2269. }
  2270. return 1;
  2271. }
  2272. switch (prefix) {
  2273. case '@':
  2274. return pc_setreg(sd, num, val);
  2275. case '$':
  2276. return mapreg_setreg(num, val);
  2277. case '#':
  2278. return (name[1] == '#') ?
  2279. pc_setaccountreg2(sd, name, val) :
  2280. pc_setaccountreg(sd, name, val);
  2281. case '.': {
  2282. struct linkdb_node** n;
  2283. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2284. if (val == 0)
  2285. linkdb_erase(n, (void*)num);
  2286. else
  2287. linkdb_replace(n, (void*)num, (void*)val);
  2288. }
  2289. return 1;
  2290. case '\'':
  2291. {
  2292. struct linkdb_node** n = NULL;
  2293. if( st->instance_id )
  2294. n = &instance[st->instance_id].ivar;
  2295. if( val == 0 )
  2296. linkdb_erase(n, (void*)num);
  2297. else
  2298. linkdb_replace(n, (void*)num, (void*)val);
  2299. return 1;
  2300. }
  2301. default:
  2302. return pc_setglobalreg(sd, name, val);
  2303. }
  2304. }
  2305. }
  2306. int set_var(TBL_PC* sd, char* name, void* val)
  2307. {
  2308. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2309. }
  2310. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2311. {
  2312. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2313. }
  2314. /// Converts the data to a string
  2315. const char* conv_str(struct script_state* st, struct script_data* data)
  2316. {
  2317. char* p;
  2318. get_val(st, data);
  2319. if( data_isstring(data) )
  2320. {// nothing to convert
  2321. }
  2322. else if( data_isint(data) )
  2323. {// int -> string
  2324. CREATE(p, char, ITEM_NAME_LENGTH);
  2325. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2326. p[ITEM_NAME_LENGTH-1] = '\0';
  2327. data->type = C_STR;
  2328. data->u.str = p;
  2329. }
  2330. else if( data_isreference(data) )
  2331. {// reference -> string
  2332. //##TODO when does this happen (check get_val) [FlavioJS]
  2333. data->type = C_CONSTSTR;
  2334. data->u.str = reference_getname(data);
  2335. }
  2336. else
  2337. {// unsupported data type
  2338. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2339. script_reportdata(data);
  2340. script_reportsrc(st);
  2341. data->type = C_CONSTSTR;
  2342. data->u.str = "";
  2343. }
  2344. return data->u.str;
  2345. }
  2346. /// Converts the data to an int
  2347. int conv_num(struct script_state* st, struct script_data* data)
  2348. {
  2349. char* p;
  2350. long num;
  2351. get_val(st, data);
  2352. if( data_isint(data) )
  2353. {// nothing to convert
  2354. }
  2355. else if( data_isstring(data) )
  2356. {// string -> int
  2357. // the result does not overflow or underflow, it is capped instead
  2358. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2359. p = data->u.str;
  2360. errno = 0;
  2361. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2362. if( errno == ERANGE
  2363. #if LONG_MAX > INT_MAX
  2364. || num < INT_MIN || num > INT_MAX
  2365. #endif
  2366. )
  2367. {
  2368. if( num <= INT_MIN )
  2369. {
  2370. num = INT_MIN;
  2371. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2372. }
  2373. else//if( num >= INT_MAX )
  2374. {
  2375. num = INT_MAX;
  2376. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2377. }
  2378. script_reportdata(data);
  2379. script_reportsrc(st);
  2380. }
  2381. if( data->type == C_STR )
  2382. aFree(p);
  2383. data->type = C_INT;
  2384. data->u.num = (int)num;
  2385. }
  2386. #if 0
  2387. // FIXME this function is being used to retrieve the position of labels and
  2388. // probably other stuff [FlavioJS]
  2389. else
  2390. {// unsupported data type
  2391. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2392. script_reportdata(data);
  2393. script_reportsrc(st);
  2394. data->type = C_INT;
  2395. data->u.num = 0;
  2396. }
  2397. #endif
  2398. return data->u.num;
  2399. }
  2400. //
  2401. // Stack operations
  2402. //
  2403. /// Increases the size of the stack
  2404. void stack_expand(struct script_stack* stack)
  2405. {
  2406. stack->sp_max += 64;
  2407. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2408. stack->sp_max * sizeof(stack->stack_data[0]) );
  2409. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2410. 64 * sizeof(stack->stack_data[0]) );
  2411. }
  2412. /// Pushes a value into the stack
  2413. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2414. /// Pushes a value into the stack (with reference)
  2415. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2416. {
  2417. if( stack->sp >= stack->sp_max )
  2418. stack_expand(stack);
  2419. stack->stack_data[stack->sp].type = type;
  2420. stack->stack_data[stack->sp].u.num = val;
  2421. stack->stack_data[stack->sp].ref = ref;
  2422. stack->sp++;
  2423. return &stack->stack_data[stack->sp-1];
  2424. }
  2425. /// Pushes a string into the stack
  2426. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2427. {
  2428. if( stack->sp >= stack->sp_max )
  2429. stack_expand(stack);
  2430. stack->stack_data[stack->sp].type = type;
  2431. stack->stack_data[stack->sp].u.str = str;
  2432. stack->stack_data[stack->sp].ref = NULL;
  2433. stack->sp++;
  2434. return &stack->stack_data[stack->sp-1];
  2435. }
  2436. /// Pushes a retinfo into the stack
  2437. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2438. {
  2439. if( stack->sp >= stack->sp_max )
  2440. stack_expand(stack);
  2441. stack->stack_data[stack->sp].type = C_RETINFO;
  2442. stack->stack_data[stack->sp].u.ri = ri;
  2443. stack->stack_data[stack->sp].ref = NULL;
  2444. stack->sp++;
  2445. return &stack->stack_data[stack->sp-1];
  2446. }
  2447. /// Pushes a copy of the target position into the stack
  2448. struct script_data* push_copy(struct script_stack* stack, int pos)
  2449. {
  2450. switch( stack->stack_data[pos].type )
  2451. {
  2452. case C_CONSTSTR:
  2453. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2454. break;
  2455. case C_STR:
  2456. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2457. break;
  2458. case C_RETINFO:
  2459. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2460. exit(1);
  2461. break;
  2462. default:
  2463. return push_val2(
  2464. stack,stack->stack_data[pos].type,
  2465. stack->stack_data[pos].u.num,
  2466. stack->stack_data[pos].ref
  2467. );
  2468. break;
  2469. }
  2470. }
  2471. /// Removes the values in indexes [start,end[ from the stack.
  2472. /// Adjusts all stack pointers.
  2473. void pop_stack(struct script_state* st, int start, int end)
  2474. {
  2475. struct script_stack* stack = st->stack;
  2476. struct script_data* data;
  2477. int i;
  2478. if( start < 0 )
  2479. start = 0;
  2480. if( end > stack->sp )
  2481. end = stack->sp;
  2482. if( start >= end )
  2483. return;// nothing to pop
  2484. // free stack elements
  2485. for( i = start; i < end; i++ )
  2486. {
  2487. data = &stack->stack_data[i];
  2488. if( data->type == C_STR )
  2489. aFree(data->u.str);
  2490. if( data->type == C_RETINFO )
  2491. {
  2492. struct script_retinfo* ri = data->u.ri;
  2493. if( ri->var_function )
  2494. {
  2495. script_free_vars(ri->var_function);
  2496. aFree(ri->var_function);
  2497. }
  2498. aFree(ri);
  2499. }
  2500. data->type = C_NOP;
  2501. }
  2502. // move the rest of the elements
  2503. if( stack->sp > end )
  2504. {
  2505. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2506. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2507. stack->stack_data[i].type = C_NOP;
  2508. }
  2509. // adjust stack pointers
  2510. if( st->start > end ) st->start -= end - start;
  2511. else if( st->start > start ) st->start = start;
  2512. if( st->end > end ) st->end -= end - start;
  2513. else if( st->end > start ) st->end = start;
  2514. if( stack->defsp > end ) stack->defsp -= end - start;
  2515. else if( stack->defsp > start ) stack->defsp = start;
  2516. stack->sp -= end - start;
  2517. }
  2518. ///
  2519. ///
  2520. ///
  2521. /*==========================================
  2522. * スクリプト依存変数、関数依存変数の解放
  2523. *------------------------------------------*/
  2524. void script_free_vars(struct linkdb_node **node)
  2525. {
  2526. struct linkdb_node* n = *node;
  2527. while( n != NULL)
  2528. {
  2529. const char* name = get_str((int)(n->key)&0x00ffffff);
  2530. if( is_string_variable(name) )
  2531. aFree(n->data); // 文字型変数なので、データ削除
  2532. n = n->next;
  2533. }
  2534. linkdb_final( node );
  2535. }
  2536. void script_free_code(struct script_code* code)
  2537. {
  2538. script_free_vars( &code->script_vars );
  2539. aFree( code->script_buf );
  2540. aFree( code );
  2541. }
  2542. /// Creates a new script state.
  2543. ///
  2544. /// @param script Script code
  2545. /// @param pos Position in the code
  2546. /// @param rid Who is running the script (attached player)
  2547. /// @param oid Where the code is being run (npc 'object')
  2548. /// @return Script state
  2549. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2550. {
  2551. struct script_state* st;
  2552. CREATE(st, struct script_state, 1);
  2553. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2554. st->stack->sp = 0;
  2555. st->stack->sp_max = 64;
  2556. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2557. st->stack->defsp = st->stack->sp;
  2558. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2559. st->state = RUN;
  2560. st->script = script;
  2561. //st->scriptroot = script;
  2562. st->pos = pos;
  2563. st->rid = rid;
  2564. st->oid = oid;
  2565. st->sleep.timer = INVALID_TIMER;
  2566. return st;
  2567. }
  2568. /// Frees a script state.
  2569. ///
  2570. /// @param st Script state
  2571. void script_free_state(struct script_state* st)
  2572. {
  2573. if(st->bk_st)
  2574. {// backup was not restored
  2575. 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);
  2576. }
  2577. if( st->sleep.timer != INVALID_TIMER )
  2578. delete_timer(st->sleep.timer, run_script_timer);
  2579. script_free_vars(st->stack->var_function);
  2580. aFree(st->stack->var_function);
  2581. pop_stack(st, 0, st->stack->sp);
  2582. aFree(st->stack->stack_data);
  2583. aFree(st->stack);
  2584. st->pos = -1;
  2585. aFree(st);
  2586. }
  2587. //
  2588. // 実行部main
  2589. //
  2590. /*==========================================
  2591. * コマンドの読み取り
  2592. *------------------------------------------*/
  2593. c_op get_com(unsigned char *script,int *pos)
  2594. {
  2595. int i = 0, j = 0;
  2596. if(script[*pos]>=0x80){
  2597. return C_INT;
  2598. }
  2599. while(script[*pos]>=0x40){
  2600. i=script[(*pos)++]<<j;
  2601. j+=6;
  2602. }
  2603. return (c_op)(i+(script[(*pos)++]<<j));
  2604. }
  2605. /*==========================================
  2606. * 数値の所得
  2607. *------------------------------------------*/
  2608. int get_num(unsigned char *script,int *pos)
  2609. {
  2610. int i,j;
  2611. i=0; j=0;
  2612. while(script[*pos]>=0xc0){
  2613. i+=(script[(*pos)++]&0x7f)<<j;
  2614. j+=6;
  2615. }
  2616. return i+((script[(*pos)++]&0x7f)<<j);
  2617. }
  2618. /*==========================================
  2619. * スタックから値を取り出す
  2620. *------------------------------------------*/
  2621. int pop_val(struct script_state* st)
  2622. {
  2623. if(st->stack->sp<=0)
  2624. return 0;
  2625. st->stack->sp--;
  2626. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2627. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2628. return st->stack->stack_data[st->stack->sp].u.num;
  2629. return 0;
  2630. }
  2631. /// Ternary operators
  2632. /// test ? if_true : if_false
  2633. void op_3(struct script_state* st, int op)
  2634. {
  2635. struct script_data* data;
  2636. int flag = 0;
  2637. data = script_getdatatop(st, -3);
  2638. get_val(st, data);
  2639. if( data_isstring(data) )
  2640. flag = data->u.str[0];// "" -> false
  2641. else if( data_isint(data) )
  2642. flag = data->u.num;// 0 -> false
  2643. else
  2644. {
  2645. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2646. script_reportdata(data);
  2647. script_reportsrc(st);
  2648. script_removetop(st, -3, 0);
  2649. script_pushnil(st);
  2650. return;
  2651. }
  2652. if( flag )
  2653. script_pushcopytop(st, -2);
  2654. else
  2655. script_pushcopytop(st, -1);
  2656. script_removetop(st, -4, -1);
  2657. }
  2658. /// Binary string operators
  2659. /// s1 EQ s2 -> i
  2660. /// s1 NE s2 -> i
  2661. /// s1 GT s2 -> i
  2662. /// s1 GE s2 -> i
  2663. /// s1 LT s2 -> i
  2664. /// s1 LE s2 -> i
  2665. /// s1 ADD s2 -> s
  2666. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2667. {
  2668. int a = 0;
  2669. switch(op){
  2670. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2671. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2672. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2673. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2674. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2675. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2676. case C_ADD:
  2677. {
  2678. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2679. strcpy(buf, s1);
  2680. strcat(buf, s2);
  2681. script_pushstr(st, buf);
  2682. return;
  2683. }
  2684. default:
  2685. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2686. script_reportsrc(st);
  2687. script_pushnil(st);
  2688. st->state = END;
  2689. return;
  2690. }
  2691. script_pushint(st,a);
  2692. }
  2693. /// Binary number operators
  2694. /// i OP i -> i
  2695. void op_2num(struct script_state* st, int op, int i1, int i2)
  2696. {
  2697. int ret;
  2698. double ret_double;
  2699. switch( op )
  2700. {
  2701. case C_AND: ret = i1 & i2; break;
  2702. case C_OR: ret = i1 | i2; break;
  2703. case C_XOR: ret = i1 ^ i2; break;
  2704. case C_LAND: ret = (i1 && i2); break;
  2705. case C_LOR: ret = (i1 || i2); break;
  2706. case C_EQ: ret = (i1 == i2); break;
  2707. case C_NE: ret = (i1 != i2); break;
  2708. case C_GT: ret = (i1 > i2); break;
  2709. case C_GE: ret = (i1 >= i2); break;
  2710. case C_LT: ret = (i1 < i2); break;
  2711. case C_LE: ret = (i1 <= i2); break;
  2712. case C_R_SHIFT: ret = i1>>i2; break;
  2713. case C_L_SHIFT: ret = i1<<i2; break;
  2714. case C_DIV:
  2715. case C_MOD:
  2716. if( i2 == 0 )
  2717. {
  2718. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2719. script_reportsrc(st);
  2720. script_pushnil(st);
  2721. st->state = END;
  2722. return;
  2723. }
  2724. else if( op == C_DIV )
  2725. ret = i1 / i2;
  2726. else//if( op == C_MOD )
  2727. ret = i1 % i2;
  2728. break;
  2729. default:
  2730. switch( op )
  2731. {// operators that can overflow/underflow
  2732. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2733. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2734. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2735. default:
  2736. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2737. script_reportsrc(st);
  2738. script_pushnil(st);
  2739. return;
  2740. }
  2741. if( ret_double < (double)INT_MIN )
  2742. {
  2743. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2744. script_reportsrc(st);
  2745. ret = INT_MIN;
  2746. }
  2747. else if( ret_double > (double)INT_MAX )
  2748. {
  2749. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2750. script_reportsrc(st);
  2751. ret = INT_MAX;
  2752. }
  2753. }
  2754. script_pushint(st, ret);
  2755. }
  2756. /// Binary operators
  2757. void op_2(struct script_state *st, int op)
  2758. {
  2759. struct script_data* left;
  2760. struct script_data* right;
  2761. left = script_getdatatop(st, -2);
  2762. right = script_getdatatop(st, -1);
  2763. get_val(st, left);
  2764. get_val(st, right);
  2765. // automatic conversions
  2766. switch( op )
  2767. {
  2768. case C_ADD:
  2769. if( data_isint(left) && data_isstring(right) )
  2770. {// convert int-string to string-string
  2771. conv_str(st, left);
  2772. }
  2773. else if( data_isstring(left) && data_isint(right) )
  2774. {// convert string-int to string-string
  2775. conv_str(st, right);
  2776. }
  2777. break;
  2778. }
  2779. if( data_isstring(left) && data_isstring(right) )
  2780. {// ss => op_2str
  2781. op_2str(st, op, left->u.str, right->u.str);
  2782. script_removetop(st, -3, -1);// pop the two values before the top one
  2783. }
  2784. else if( data_isint(left) && data_isint(right) )
  2785. {// ii => op_2num
  2786. int i1 = left->u.num;
  2787. int i2 = right->u.num;
  2788. script_removetop(st, -2, 0);
  2789. op_2num(st, op, i1, i2);
  2790. }
  2791. else
  2792. {// invalid argument
  2793. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2794. script_reportdata(left);
  2795. script_reportdata(right);
  2796. script_reportsrc(st);
  2797. script_removetop(st, -2, 0);
  2798. script_pushnil(st);
  2799. st->state = END;
  2800. }
  2801. }
  2802. /// Unary operators
  2803. /// NEG i -> i
  2804. /// NOT i -> i
  2805. /// LNOT i -> i
  2806. void op_1(struct script_state* st, int op)
  2807. {
  2808. struct script_data* data;
  2809. int i1;
  2810. data = script_getdatatop(st, -1);
  2811. get_val(st, data);
  2812. if( !data_isint(data) )
  2813. {// not a number
  2814. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2815. script_reportdata(data);
  2816. script_reportsrc(st);
  2817. script_pushnil(st);
  2818. st->state = END;
  2819. return;
  2820. }
  2821. i1 = data->u.num;
  2822. script_removetop(st, -1, 0);
  2823. switch( op )
  2824. {
  2825. case C_NEG: i1 = -i1; break;
  2826. case C_NOT: i1 = ~i1; break;
  2827. case C_LNOT: i1 = !i1; break;
  2828. default:
  2829. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2830. script_reportsrc(st);
  2831. script_pushnil(st);
  2832. st->state = END;
  2833. return;
  2834. }
  2835. script_pushint(st, i1);
  2836. }
  2837. /// Checks the type of all arguments passed to a built-in function.
  2838. ///
  2839. /// @param st Script state whose stack arguments should be inspected.
  2840. /// @param func Built-in function for which the arguments are intended.
  2841. static void script_check_buildin_argtype(struct script_state* st, int func)
  2842. {
  2843. char type;
  2844. int idx, invalid = 0;
  2845. script_function* sf = &buildin_func[str_data[func].val];
  2846. for( idx = 2; script_hasdata(st, idx); idx++ )
  2847. {
  2848. struct script_data* data = script_getdata(st, idx);
  2849. type = sf->arg[idx-2];
  2850. if( type == '?' || type == '*' )
  2851. {// optional argument or unknown number of optional parameters ( no types are after this )
  2852. break;
  2853. }
  2854. else if( type == 0 )
  2855. {// more arguments than necessary ( should not happen, as it is checked before )
  2856. ShowWarning("Found more arguments than necessary.\n");
  2857. invalid++;
  2858. break;
  2859. }
  2860. else
  2861. {
  2862. const char* name = NULL;
  2863. if( data_isreference(data) )
  2864. {// get name for variables to determine the type they refer to
  2865. name = reference_getname(data);
  2866. }
  2867. switch( type )
  2868. {
  2869. case 'v':
  2870. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  2871. {// variant
  2872. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  2873. script_reportdata(data);
  2874. invalid++;
  2875. }
  2876. break;
  2877. case 's':
  2878. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  2879. {// string
  2880. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  2881. script_reportdata(data);
  2882. invalid++;
  2883. }
  2884. break;
  2885. case 'i':
  2886. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  2887. {// int ( params and constants are always int )
  2888. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  2889. script_reportdata(data);
  2890. invalid++;
  2891. }
  2892. break;
  2893. case 'r':
  2894. if( !data_isreference(data) )
  2895. {// variables
  2896. ShowWarning("Unexpected type for argument %d. Expected variable.\n", idx-1);
  2897. script_reportdata(data);
  2898. invalid++;
  2899. }
  2900. break;
  2901. case 'l':
  2902. if( !data_islabel(data) && !data_isfunclabel(data) )
  2903. {// label
  2904. ShowWarning("Unexpected type for argument %d. Expected label.\n", idx-1);
  2905. script_reportdata(data);
  2906. invalid++;
  2907. }
  2908. break;
  2909. }
  2910. }
  2911. }
  2912. if(invalid)
  2913. {
  2914. ShowDebug("Function: %s\n", get_str(func));
  2915. script_reportsrc(st);
  2916. }
  2917. }
  2918. /// Executes a buildin command.
  2919. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2920. int run_func(struct script_state *st)
  2921. {
  2922. struct script_data* data;
  2923. int i,start_sp,end_sp,func;
  2924. end_sp = st->stack->sp;// position after the last argument
  2925. for( i = end_sp-1; i > 0 ; --i )
  2926. if( st->stack->stack_data[i].type == C_ARG )
  2927. break;
  2928. if( i == 0 )
  2929. {
  2930. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2931. st->state = END;
  2932. script_reportsrc(st);
  2933. return 1;
  2934. }
  2935. start_sp = i-1;// C_NAME of the command
  2936. st->start = start_sp;
  2937. st->end = end_sp;
  2938. data = &st->stack->stack_data[st->start];
  2939. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2940. func = data->u.num;
  2941. else
  2942. {
  2943. ShowError("script:run_func: not a buildin command.\n");
  2944. script_reportdata(data);
  2945. script_reportsrc(st);
  2946. st->state = END;
  2947. return 1;
  2948. }
  2949. if( script_config.warn_func_mismatch_argtypes )
  2950. {
  2951. script_check_buildin_argtype(st, func);
  2952. }
  2953. if(str_data[func].func){
  2954. if (str_data[func].func(st)) //Report error
  2955. script_reportsrc(st);
  2956. } else {
  2957. 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));
  2958. script_reportsrc(st);
  2959. st->state = END;
  2960. }
  2961. // Stack's datum are used when re-running functions [Eoe]
  2962. if( st->state == RERUNLINE )
  2963. return 0;
  2964. pop_stack(st, st->start, st->end);
  2965. if( st->state == RETFUNC )
  2966. {// return from a user-defined function
  2967. struct script_retinfo* ri;
  2968. int olddefsp = st->stack->defsp;
  2969. int nargs;
  2970. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2971. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2972. {
  2973. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  2974. script_reportsrc(st);
  2975. st->state = END;
  2976. return 1;
  2977. }
  2978. script_free_vars( st->stack->var_function );
  2979. aFree(st->stack->var_function);
  2980. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  2981. nargs = ri->nargs;
  2982. st->pos = ri->pos;
  2983. st->script = ri->script;
  2984. st->stack->var_function = ri->var_function;
  2985. st->stack->defsp = ri->defsp;
  2986. memset(ri, 0, sizeof(struct script_retinfo));
  2987. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  2988. st->state = GOTO;
  2989. }
  2990. return 0;
  2991. }
  2992. /*==========================================
  2993. * script execution
  2994. *------------------------------------------*/
  2995. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2996. {
  2997. struct script_state *st;
  2998. if( rootscript == NULL || pos < 0 )
  2999. return;
  3000. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3001. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3002. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3003. st = script_alloc_state(rootscript, pos, rid, oid);
  3004. run_script_main(st);
  3005. }
  3006. void script_stop_sleeptimers(int id)
  3007. {
  3008. struct script_state* st;
  3009. for(;;)
  3010. {
  3011. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  3012. if( st == NULL )
  3013. break; // no more sleep timers
  3014. script_free_state(st);
  3015. }
  3016. }
  3017. /*==========================================
  3018. * 指定ノードをsleep_dbから削除
  3019. *------------------------------------------*/
  3020. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3021. {
  3022. struct linkdb_node *retnode;
  3023. if( n == NULL)
  3024. return NULL;
  3025. if( n->prev == NULL )
  3026. sleep_db = n->next;
  3027. else
  3028. n->prev->next = n->next;
  3029. if( n->next )
  3030. n->next->prev = n->prev;
  3031. retnode = n->next;
  3032. aFree( n );
  3033. return retnode; // 次のノードを返す
  3034. }
  3035. /*==========================================
  3036. * sleep用タイマー関数
  3037. *------------------------------------------*/
  3038. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3039. {
  3040. struct script_state *st = (struct script_state *)data;
  3041. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3042. TBL_PC *sd = map_id2sd(st->rid);
  3043. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3044. { //Character mismatch. Cancel execution.
  3045. st->rid = 0;
  3046. st->state = END;
  3047. }
  3048. while( node && st->sleep.timer != INVALID_TIMER ) {
  3049. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3050. script_erase_sleepdb(node);
  3051. st->sleep.timer = INVALID_TIMER;
  3052. break;
  3053. }
  3054. node = node->next;
  3055. }
  3056. if(st->state != RERUNLINE)
  3057. st->sleep.tick = 0;
  3058. run_script_main(st);
  3059. return 0;
  3060. }
  3061. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3062. ///
  3063. /// @param st Script state to detach.
  3064. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3065. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3066. {
  3067. struct map_session_data* sd;
  3068. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3069. {
  3070. sd->st = st->bk_st;
  3071. sd->npc_id = st->bk_npcid;
  3072. /**
  3073. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3074. **/
  3075. #if SECURE_NPCTIMEOUT
  3076. /**
  3077. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3078. **/
  3079. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3080. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3081. sd->npc_idle_timer = INVALID_TIMER;
  3082. }
  3083. #endif
  3084. if(st->bk_st)
  3085. {
  3086. //Remove tag for removal.
  3087. st->bk_st = NULL;
  3088. st->bk_npcid = 0;
  3089. }
  3090. else if(dequeue_event)
  3091. {
  3092. npc_event_dequeue(sd);
  3093. }
  3094. }
  3095. else if(st->bk_st)
  3096. {// rid was set to 0, before detaching the script state
  3097. 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);
  3098. script_reportsrc(st->bk_st);
  3099. script_free_state(st->bk_st);
  3100. st->bk_st = NULL;
  3101. }
  3102. }
  3103. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3104. ///
  3105. /// @param st Script state to attach.
  3106. static void script_attach_state(struct script_state* st)
  3107. {
  3108. struct map_session_data* sd;
  3109. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3110. {
  3111. if(st!=sd->st)
  3112. {
  3113. if(st->bk_st)
  3114. {// there is already a backup
  3115. 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);
  3116. }
  3117. st->bk_st = sd->st;
  3118. st->bk_npcid = sd->npc_id;
  3119. }
  3120. sd->st = st;
  3121. sd->npc_id = st->oid;
  3122. /**
  3123. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3124. **/
  3125. #if SECURE_NPCTIMEOUT
  3126. if( sd->npc_idle_timer == INVALID_TIMER )
  3127. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3128. sd->npc_idle_tick = gettick();
  3129. #endif
  3130. }
  3131. }
  3132. /*==========================================
  3133. * スクリプトの実行メイン部分
  3134. *------------------------------------------*/
  3135. void run_script_main(struct script_state *st)
  3136. {
  3137. int cmdcount=script_config.check_cmdcount;
  3138. int gotocount=script_config.check_gotocount;
  3139. TBL_PC *sd;
  3140. struct script_stack *stack=st->stack;
  3141. struct npc_data *nd;
  3142. script_attach_state(st);
  3143. nd = map_id2nd(st->oid);
  3144. if( nd && map[nd->bl.m].instance_id > 0 )
  3145. st->instance_id = map[nd->bl.m].instance_id;
  3146. if(st->state == RERUNLINE) {
  3147. run_func(st);
  3148. if(st->state == GOTO)
  3149. st->state = RUN;
  3150. } else if(st->state != END)
  3151. st->state = RUN;
  3152. while(st->state == RUN)
  3153. {
  3154. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3155. switch(c){
  3156. case C_EOL:
  3157. if( stack->defsp > stack->sp )
  3158. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3159. else
  3160. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3161. break;
  3162. case C_INT:
  3163. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3164. break;
  3165. case C_POS:
  3166. case C_NAME:
  3167. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3168. st->pos+=3;
  3169. break;
  3170. case C_ARG:
  3171. push_val(stack,c,0);
  3172. break;
  3173. case C_STR:
  3174. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3175. while(st->script->script_buf[st->pos++]);
  3176. break;
  3177. case C_FUNC:
  3178. run_func(st);
  3179. if(st->state==GOTO){
  3180. st->state = RUN;
  3181. if( gotocount>0 && (--gotocount)<=0 ){
  3182. ShowError("run_script: infinity loop !\n");
  3183. script_reportsrc(st);
  3184. st->state=END;
  3185. }
  3186. }
  3187. break;
  3188. case C_NEG:
  3189. case C_NOT:
  3190. case C_LNOT:
  3191. op_1(st ,c);
  3192. break;
  3193. case C_ADD:
  3194. case C_SUB:
  3195. case C_MUL:
  3196. case C_DIV:
  3197. case C_MOD:
  3198. case C_EQ:
  3199. case C_NE:
  3200. case C_GT:
  3201. case C_GE:
  3202. case C_LT:
  3203. case C_LE:
  3204. case C_AND:
  3205. case C_OR:
  3206. case C_XOR:
  3207. case C_LAND:
  3208. case C_LOR:
  3209. case C_R_SHIFT:
  3210. case C_L_SHIFT:
  3211. op_2(st, c);
  3212. break;
  3213. case C_OP3:
  3214. op_3(st, c);
  3215. break;
  3216. case C_NOP:
  3217. st->state=END;
  3218. break;
  3219. default:
  3220. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3221. st->state=END;
  3222. break;
  3223. }
  3224. if( cmdcount>0 && (--cmdcount)<=0 ){
  3225. ShowError("run_script: infinity loop !\n");
  3226. script_reportsrc(st);
  3227. st->state=END;
  3228. }
  3229. }
  3230. if(st->sleep.tick > 0) {
  3231. //Restore previous script
  3232. script_detach_state(st, false);
  3233. //Delay execution
  3234. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3235. st->sleep.charid = sd?sd->status.char_id:0;
  3236. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3237. run_script_timer, st->sleep.charid, (intptr_t)st);
  3238. linkdb_insert(&sleep_db, (void*)st->oid, st);
  3239. }
  3240. else if(st->state != END && st->rid){
  3241. //Resume later (st is already attached to player).
  3242. if(st->bk_st) {
  3243. ShowWarning("Unable to restore stack! Double continuation!\n");
  3244. //Report BOTH scripts to see if that can help somehow.
  3245. ShowDebug("Previous script (lost):\n");
  3246. script_reportsrc(st->bk_st);
  3247. ShowDebug("Current script:\n");
  3248. script_reportsrc(st);
  3249. script_free_state(st->bk_st);
  3250. st->bk_st = NULL;
  3251. }
  3252. } else {
  3253. //Dispose of script.
  3254. if ((sd = map_id2sd(st->rid))!=NULL)
  3255. { //Restore previous stack and save char.
  3256. if(sd->state.using_fake_npc){
  3257. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3258. sd->state.using_fake_npc = 0;
  3259. }
  3260. //Restore previous script if any.
  3261. script_detach_state(st, true);
  3262. if (sd->state.reg_dirty&2)
  3263. intif_saveregistry(sd,2);
  3264. if (sd->state.reg_dirty&1)
  3265. intif_saveregistry(sd,1);
  3266. }
  3267. script_free_state(st);
  3268. st = NULL;
  3269. }
  3270. }
  3271. int script_config_read(char *cfgName)
  3272. {
  3273. int i;
  3274. char line[1024],w1[1024],w2[1024];
  3275. FILE *fp;
  3276. fp=fopen(cfgName,"r");
  3277. if(fp==NULL){
  3278. ShowError("file not found: [%s]\n", cfgName);
  3279. return 1;
  3280. }
  3281. while(fgets(line, sizeof(line), fp))
  3282. {
  3283. if(line[0] == '/' && line[1] == '/')
  3284. continue;
  3285. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3286. if(i!=2)
  3287. continue;
  3288. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3289. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3290. }
  3291. else if(strcmpi(w1,"check_cmdcount")==0) {
  3292. script_config.check_cmdcount = config_switch(w2);
  3293. }
  3294. else if(strcmpi(w1,"check_gotocount")==0) {
  3295. script_config.check_gotocount = config_switch(w2);
  3296. }
  3297. else if(strcmpi(w1,"input_min_value")==0) {
  3298. script_config.input_min_value = config_switch(w2);
  3299. }
  3300. else if(strcmpi(w1,"input_max_value")==0) {
  3301. script_config.input_max_value = config_switch(w2);
  3302. }
  3303. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3304. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3305. }
  3306. else if(strcmpi(w1,"import")==0){
  3307. script_config_read(w2);
  3308. }
  3309. }
  3310. fclose(fp);
  3311. return 0;
  3312. }
  3313. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3314. {
  3315. struct script_code *code = (struct script_code *)data;
  3316. if(code){
  3317. script_free_vars( &code->script_vars );
  3318. aFree( code->script_buf );
  3319. aFree( code );
  3320. }
  3321. return 0;
  3322. }
  3323. static int do_final_autobonus_sub (DBKey key,void *data,va_list ap)
  3324. {
  3325. struct script_code *script = (struct script_code *)data;
  3326. if( script )
  3327. script_free_code(script);
  3328. return 0;
  3329. }
  3330. void script_run_autobonus(const char *autobonus, int id, int pos)
  3331. {
  3332. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3333. if( script )
  3334. {
  3335. current_equip_item_index = pos;
  3336. run_script(script,0,id,0);
  3337. }
  3338. }
  3339. void script_add_autobonus(const char *autobonus)
  3340. {
  3341. if( strdb_get(autobonus_db, autobonus) == NULL )
  3342. {
  3343. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3344. if( script )
  3345. strdb_put(autobonus_db, autobonus, script);
  3346. }
  3347. }
  3348. /// resets a temporary character array variable to given value
  3349. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3350. {
  3351. int key;
  3352. uint8 idx;
  3353. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3354. {
  3355. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3356. return;
  3357. }
  3358. key = add_str(varname);
  3359. if( is_string_variable(varname) )
  3360. {
  3361. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3362. {
  3363. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3364. }
  3365. }
  3366. else
  3367. {
  3368. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3369. {
  3370. pc_setreg(sd, reference_uid(key, idx), (int)value);
  3371. }
  3372. }
  3373. }
  3374. /// sets a temporary character array variable element idx to given value
  3375. /// @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.
  3376. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3377. {
  3378. int key;
  3379. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3380. {
  3381. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3382. return;
  3383. }
  3384. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3385. {
  3386. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3387. return;
  3388. }
  3389. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3390. if( is_string_variable(varname) )
  3391. {
  3392. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3393. }
  3394. else
  3395. {
  3396. pc_setreg(sd, reference_uid(key, idx), (int)value);
  3397. }
  3398. if( refcache )
  3399. {// save to avoid repeated add_str calls
  3400. refcache[0] = key;
  3401. }
  3402. }
  3403. /*==========================================
  3404. * 終了
  3405. *------------------------------------------*/
  3406. int do_final_script()
  3407. {
  3408. #ifdef DEBUG_HASH
  3409. if (battle_config.etc_log)
  3410. {
  3411. FILE *fp = fopen("hash_dump.txt","wt");
  3412. if(fp) {
  3413. int i,count[SCRIPT_HASH_SIZE];
  3414. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3415. int n=0;
  3416. int min=INT_MAX,max=0,zero=0;
  3417. double mean=0.0f;
  3418. double median=0.0f;
  3419. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3420. memset(count, 0, sizeof(count));
  3421. fprintf(fp,"num : hash : data_name\n");
  3422. fprintf(fp,"---------------------------------------------------------------\n");
  3423. for(i=LABEL_START; i<str_num; i++) {
  3424. unsigned int h = calc_hash(get_str(i));
  3425. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3426. ++count[h];
  3427. }
  3428. fprintf(fp,"--------------------\n\n");
  3429. memset(count2, 0, sizeof(count2));
  3430. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3431. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3432. if(min > count[i])
  3433. min = count[i]; // minimun count of collision
  3434. if(max < count[i])
  3435. max = count[i]; // maximun count of collision
  3436. if(count[i] == 0)
  3437. zero++;
  3438. ++count2[count[i]];
  3439. }
  3440. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3441. for( i=min; i <= max; ++i ){
  3442. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3443. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3444. }
  3445. for( i=min; i <= max; ++i ){
  3446. n += count2[i];
  3447. if( n*2 >= SCRIPT_HASH_SIZE )
  3448. {
  3449. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3450. median = (i+i+1)/2.0f;
  3451. else
  3452. median = i;
  3453. break;
  3454. }
  3455. }
  3456. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3457. fclose(fp);
  3458. }
  3459. }
  3460. #endif
  3461. mapreg_final();
  3462. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3463. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3464. autobonus_db->destroy(autobonus_db, do_final_autobonus_sub);
  3465. if(sleep_db) {
  3466. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3467. while(n) {
  3468. struct script_state *st = (struct script_state *)n->data;
  3469. script_free_state(st);
  3470. n = n->next;
  3471. }
  3472. linkdb_final(&sleep_db);
  3473. }
  3474. if (str_data)
  3475. aFree(str_data);
  3476. if (str_buf)
  3477. aFree(str_buf);
  3478. return 0;
  3479. }
  3480. /*==========================================
  3481. * 初期化
  3482. *------------------------------------------*/
  3483. int do_init_script()
  3484. {
  3485. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3486. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3487. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3488. mapreg_init();
  3489. return 0;
  3490. }
  3491. int script_reload()
  3492. {
  3493. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3494. scriptlabel_db->clear(scriptlabel_db, NULL);
  3495. if(sleep_db) {
  3496. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3497. while(n) {
  3498. struct script_state *st = (struct script_state *)n->data;
  3499. script_free_state(st);
  3500. n = n->next;
  3501. }
  3502. linkdb_final(&sleep_db);
  3503. }
  3504. mapreg_reload();
  3505. return 0;
  3506. }
  3507. //-----------------------------------------------------------------------------
  3508. // buildin functions
  3509. //
  3510. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3511. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3512. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3513. /////////////////////////////////////////////////////////////////////
  3514. // NPC interaction
  3515. //
  3516. /// Appends a message to the npc dialog.
  3517. /// If a dialog doesn't exist yet, one is created.
  3518. ///
  3519. /// mes "<message>";
  3520. BUILDIN_FUNC(mes)
  3521. {
  3522. int i;
  3523. TBL_PC* sd = script_rid2sd(st);
  3524. if( sd == NULL )
  3525. return 0;
  3526. if( !script_hasdata(st, 3) )
  3527. {// only a single line detected in the script
  3528. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3529. }
  3530. else
  3531. {// parse multiple lines as they exist
  3532. for( i = 2; script_hasdata(st, i); i++ )
  3533. {
  3534. // send the message to the client
  3535. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3536. }
  3537. }
  3538. return 0;
  3539. }
  3540. /// Displays the button 'next' in the npc dialog.
  3541. /// The dialog text is cleared and the script continues when the button is pressed.
  3542. ///
  3543. /// next;
  3544. BUILDIN_FUNC(next)
  3545. {
  3546. TBL_PC* sd;
  3547. sd = script_rid2sd(st);
  3548. if( sd == NULL )
  3549. return 0;
  3550. st->state = STOP;
  3551. clif_scriptnext(sd, st->oid);
  3552. return 0;
  3553. }
  3554. /// Ends the script and displays the button 'close' on the npc dialog.
  3555. /// The dialog is closed when the button is pressed.
  3556. ///
  3557. /// close;
  3558. BUILDIN_FUNC(close)
  3559. {
  3560. TBL_PC* sd;
  3561. sd = script_rid2sd(st);
  3562. if( sd == NULL )
  3563. return 0;
  3564. st->state = END;
  3565. clif_scriptclose(sd, st->oid);
  3566. return 0;
  3567. }
  3568. /// Displays the button 'close' on the npc dialog.
  3569. /// The dialog is closed and the script continues when the button is pressed.
  3570. ///
  3571. /// close2;
  3572. BUILDIN_FUNC(close2)
  3573. {
  3574. TBL_PC* sd;
  3575. sd = script_rid2sd(st);
  3576. if( sd == NULL )
  3577. return 0;
  3578. st->state = STOP;
  3579. clif_scriptclose(sd, st->oid);
  3580. return 0;
  3581. }
  3582. /// Counts the number of valid and total number of options in 'str'
  3583. /// If max_count > 0 the counting stops when that valid option is reached
  3584. /// total is incremented for each option (NULL is supported)
  3585. static int menu_countoptions(const char* str, int max_count, int* total)
  3586. {
  3587. int count = 0;
  3588. int bogus_total;
  3589. if( total == NULL )
  3590. total = &bogus_total;
  3591. ++(*total);
  3592. // initial empty options
  3593. while( *str == ':' )
  3594. {
  3595. ++str;
  3596. ++(*total);
  3597. }
  3598. // count menu options
  3599. while( *str != '\0' )
  3600. {
  3601. ++count;
  3602. --max_count;
  3603. if( max_count == 0 )
  3604. break;
  3605. while( *str != ':' && *str != '\0' )
  3606. ++str;
  3607. while( *str == ':' )
  3608. {
  3609. ++str;
  3610. ++(*total);
  3611. }
  3612. }
  3613. return count;
  3614. }
  3615. /// Displays a menu with options and goes to the target label.
  3616. /// The script is stopped if cancel is pressed.
  3617. /// Options with no text are not displayed in the client.
  3618. ///
  3619. /// Options can be grouped together, separated by the character ':' in the text:
  3620. /// ex: menu "A:B:C",L_target;
  3621. /// All these options go to the specified target label.
  3622. ///
  3623. /// The index of the selected option is put in the variable @menu.
  3624. /// Indexes start with 1 and are consistent with grouped and empty options.
  3625. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3626. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3627. ///
  3628. /// NOTE: the client closes the npc dialog when cancel is pressed
  3629. ///
  3630. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3631. BUILDIN_FUNC(menu)
  3632. {
  3633. int i;
  3634. const char* text;
  3635. TBL_PC* sd;
  3636. sd = script_rid2sd(st);
  3637. if( sd == NULL )
  3638. return 0;
  3639. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3640. if( sd->state.menu_or_input == 0 )
  3641. {
  3642. struct StringBuf buf;
  3643. struct script_data* data;
  3644. if( script_lastdata(st) % 2 == 0 )
  3645. {// argument count is not even (1st argument is at index 2)
  3646. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3647. st->state = END;
  3648. return 1;
  3649. }
  3650. StringBuf_Init(&buf);
  3651. sd->npc_menu = 0;
  3652. for( i = 2; i < script_lastdata(st); i += 2 )
  3653. {
  3654. // menu options
  3655. text = script_getstr(st, i);
  3656. // target label
  3657. data = script_getdata(st, i+1);
  3658. if( !data_islabel(data) )
  3659. {// not a label
  3660. StringBuf_Destroy(&buf);
  3661. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3662. script_reportdata(data);
  3663. st->state = END;
  3664. return 1;
  3665. }
  3666. // append option(s)
  3667. if( text[0] == '\0' )
  3668. continue;// empty string, ignore
  3669. if( sd->npc_menu > 0 )
  3670. StringBuf_AppendStr(&buf, ":");
  3671. StringBuf_AppendStr(&buf, text);
  3672. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3673. }
  3674. st->state = RERUNLINE;
  3675. sd->state.menu_or_input = 1;
  3676. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3677. StringBuf_Destroy(&buf);
  3678. if( sd->npc_menu >= 0xff )
  3679. {// 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
  3680. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3681. script_reportsrc(st);
  3682. }
  3683. }
  3684. else if( sd->npc_menu == 0xff )
  3685. {// Cancel was pressed
  3686. sd->state.menu_or_input = 0;
  3687. st->state = END;
  3688. }
  3689. else
  3690. {// goto target label
  3691. int menu = 0;
  3692. sd->state.menu_or_input = 0;
  3693. if( sd->npc_menu <= 0 )
  3694. {
  3695. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3696. st->state = END;
  3697. return 1;
  3698. }
  3699. // get target label
  3700. for( i = 2; i < script_lastdata(st); i += 2 )
  3701. {
  3702. text = script_getstr(st, i);
  3703. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3704. if( sd->npc_menu <= 0 )
  3705. break;// entry found
  3706. }
  3707. if( sd->npc_menu > 0 )
  3708. {// Invalid selection
  3709. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3710. st->state = END;
  3711. return 1;
  3712. }
  3713. if( !data_islabel(script_getdata(st, i + 1)) )
  3714. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3715. ShowError("script:menu: unexpected data in label argument\n");
  3716. script_reportdata(script_getdata(st, i + 1));
  3717. st->state = END;
  3718. return 1;
  3719. }
  3720. pc_setreg(sd, add_str("@menu"), menu);
  3721. st->pos = script_getnum(st, i + 1);
  3722. st->state = GOTO;
  3723. }
  3724. return 0;
  3725. }
  3726. /// Displays a menu with options and returns the selected option.
  3727. /// Behaves like 'menu' without the target labels.
  3728. ///
  3729. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3730. ///
  3731. /// @see menu
  3732. BUILDIN_FUNC(select)
  3733. {
  3734. int i;
  3735. const char* text;
  3736. TBL_PC* sd;
  3737. sd = script_rid2sd(st);
  3738. if( sd == NULL )
  3739. return 0;
  3740. if( sd->state.menu_or_input == 0 )
  3741. {
  3742. struct StringBuf buf;
  3743. StringBuf_Init(&buf);
  3744. sd->npc_menu = 0;
  3745. for( i = 2; i <= script_lastdata(st); ++i )
  3746. {
  3747. text = script_getstr(st, i);
  3748. if( sd->npc_menu > 0 )
  3749. StringBuf_AppendStr(&buf, ":");
  3750. StringBuf_AppendStr(&buf, text);
  3751. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3752. }
  3753. st->state = RERUNLINE;
  3754. sd->state.menu_or_input = 1;
  3755. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3756. StringBuf_Destroy(&buf);
  3757. if( sd->npc_menu >= 0xff )
  3758. {
  3759. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3760. script_reportsrc(st);
  3761. }
  3762. }
  3763. else if( sd->npc_menu == 0xff )
  3764. {// Cancel was pressed
  3765. sd->state.menu_or_input = 0;
  3766. st->state = END;
  3767. }
  3768. else
  3769. {// return selected option
  3770. int menu = 0;
  3771. sd->state.menu_or_input = 0;
  3772. for( i = 2; i <= script_lastdata(st); ++i )
  3773. {
  3774. text = script_getstr(st, i);
  3775. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3776. if( sd->npc_menu <= 0 )
  3777. break;// entry found
  3778. }
  3779. pc_setreg(sd, add_str("@menu"), menu);
  3780. script_pushint(st, menu);
  3781. st->state = RUN;
  3782. }
  3783. return 0;
  3784. }
  3785. /// Displays a menu with options and returns the selected option.
  3786. /// Behaves like 'menu' without the target labels, except when cancel is
  3787. /// pressed.
  3788. /// When cancel is pressed, the script continues and 255 is returned.
  3789. ///
  3790. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3791. ///
  3792. /// @see menu
  3793. BUILDIN_FUNC(prompt)
  3794. {
  3795. int i;
  3796. const char *text;
  3797. TBL_PC* sd;
  3798. sd = script_rid2sd(st);
  3799. if( sd == NULL )
  3800. return 0;
  3801. if( sd->state.menu_or_input == 0 )
  3802. {
  3803. struct StringBuf buf;
  3804. StringBuf_Init(&buf);
  3805. sd->npc_menu = 0;
  3806. for( i = 2; i <= script_lastdata(st); ++i )
  3807. {
  3808. text = script_getstr(st, i);
  3809. if( sd->npc_menu > 0 )
  3810. StringBuf_AppendStr(&buf, ":");
  3811. StringBuf_AppendStr(&buf, text);
  3812. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3813. }
  3814. st->state = RERUNLINE;
  3815. sd->state.menu_or_input = 1;
  3816. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3817. StringBuf_Destroy(&buf);
  3818. if( sd->npc_menu >= 0xff )
  3819. {
  3820. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3821. script_reportsrc(st);
  3822. }
  3823. }
  3824. else if( sd->npc_menu == 0xff )
  3825. {// Cancel was pressed
  3826. sd->state.menu_or_input = 0;
  3827. pc_setreg(sd, add_str("@menu"), 0xff);
  3828. script_pushint(st, 0xff);
  3829. st->state = RUN;
  3830. }
  3831. else
  3832. {// return selected option
  3833. int menu = 0;
  3834. sd->state.menu_or_input = 0;
  3835. for( i = 2; i <= script_lastdata(st); ++i )
  3836. {
  3837. text = script_getstr(st, i);
  3838. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3839. if( sd->npc_menu <= 0 )
  3840. break;// entry found
  3841. }
  3842. pc_setreg(sd, add_str("@menu"), menu);
  3843. script_pushint(st, menu);
  3844. st->state = RUN;
  3845. }
  3846. return 0;
  3847. }
  3848. /////////////////////////////////////////////////////////////////////
  3849. // ...
  3850. //
  3851. /// Jumps to the target script label.
  3852. ///
  3853. /// goto <label>;
  3854. BUILDIN_FUNC(goto)
  3855. {
  3856. if( !data_islabel(script_getdata(st,2)) )
  3857. {
  3858. ShowError("script:goto: not a label\n");
  3859. script_reportdata(script_getdata(st,2));
  3860. st->state = END;
  3861. return 1;
  3862. }
  3863. st->pos = script_getnum(st,2);
  3864. st->state = GOTO;
  3865. return 0;
  3866. }
  3867. /*==========================================
  3868. * user-defined function call
  3869. *------------------------------------------*/
  3870. BUILDIN_FUNC(callfunc)
  3871. {
  3872. int i, j;
  3873. struct script_retinfo* ri;
  3874. struct script_code* scr;
  3875. const char* str = script_getstr(st,2);
  3876. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3877. if( !scr )
  3878. {
  3879. ShowError("script:callfunc: function not found! [%s]\n", str);
  3880. st->state = END;
  3881. return 1;
  3882. }
  3883. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3884. {
  3885. struct script_data* data = push_copy(st->stack,i);
  3886. if( data_isreference(data) && !data->ref )
  3887. {
  3888. const char* name = reference_getname(data);
  3889. if( name[0] == '.' && name[1] == '@' )
  3890. data->ref = st->stack->var_function;
  3891. else if( name[0] == '.' )
  3892. data->ref = &st->script->script_vars;
  3893. }
  3894. }
  3895. CREATE(ri, struct script_retinfo, 1);
  3896. ri->script = st->script;// script code
  3897. ri->var_function = st->stack->var_function;// scope variables
  3898. ri->pos = st->pos;// script location
  3899. ri->nargs = j;// argument count
  3900. ri->defsp = st->stack->defsp;// default stack pointer
  3901. push_retinfo(st->stack, ri);
  3902. st->pos = 0;
  3903. st->script = scr;
  3904. st->stack->defsp = st->stack->sp;
  3905. st->state = GOTO;
  3906. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3907. return 0;
  3908. }
  3909. /*==========================================
  3910. * subroutine call
  3911. *------------------------------------------*/
  3912. BUILDIN_FUNC(callsub)
  3913. {
  3914. int i,j;
  3915. struct script_retinfo* ri;
  3916. int pos = script_getnum(st,2);
  3917. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3918. {
  3919. ShowError("script:callsub: argument is not a label\n");
  3920. script_reportdata(script_getdata(st,2));
  3921. st->state = END;
  3922. return 1;
  3923. }
  3924. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3925. {
  3926. struct script_data* data = push_copy(st->stack,i);
  3927. if( data_isreference(data) && !data->ref )
  3928. {
  3929. const char* name = reference_getname(data);
  3930. if( name[0] == '.' && name[1] == '@' )
  3931. data->ref = st->stack->var_function;
  3932. }
  3933. }
  3934. CREATE(ri, struct script_retinfo, 1);
  3935. ri->script = st->script;// script code
  3936. ri->var_function = st->stack->var_function;// scope variables
  3937. ri->pos = st->pos;// script location
  3938. ri->nargs = j;// argument count
  3939. ri->defsp = st->stack->defsp;// default stack pointer
  3940. push_retinfo(st->stack, ri);
  3941. st->pos = pos;
  3942. st->stack->defsp = st->stack->sp;
  3943. st->state = GOTO;
  3944. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3945. return 0;
  3946. }
  3947. /// Retrieves an argument provided to callfunc/callsub.
  3948. /// If the argument doesn't exist
  3949. ///
  3950. /// getarg(<index>{,<default_value>}) -> <value>
  3951. BUILDIN_FUNC(getarg)
  3952. {
  3953. struct script_retinfo* ri;
  3954. int idx;
  3955. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3956. {
  3957. ShowError("script:getarg: no callfunc or callsub!\n");
  3958. st->state = END;
  3959. return 1;
  3960. }
  3961. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3962. idx = script_getnum(st,2);
  3963. if( idx >= 0 && idx < ri->nargs )
  3964. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3965. else if( script_hasdata(st,3) )
  3966. script_pushcopy(st, 3);
  3967. else
  3968. {
  3969. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3970. st->state = END;
  3971. return 1;
  3972. }
  3973. return 0;
  3974. }
  3975. /// Returns from the current function, optionaly returning a value from the functions.
  3976. /// Don't use outside script functions.
  3977. ///
  3978. /// return;
  3979. /// return <value>;
  3980. BUILDIN_FUNC(return)
  3981. {
  3982. if( script_hasdata(st,2) )
  3983. {// return value
  3984. struct script_data* data;
  3985. script_pushcopy(st, 2);
  3986. data = script_getdatatop(st, -1);
  3987. if( data_isreference(data) )
  3988. {
  3989. const char* name = reference_getname(data);
  3990. if( name[0] == '.' && name[1] == '@' )
  3991. {// scope variable
  3992. if( !data->ref || data->ref == st->stack->var_function )
  3993. get_val(st, data);// current scope, convert to value
  3994. }
  3995. else if( name[0] == '.' && !data->ref )
  3996. {// script variable, link to current script
  3997. data->ref = &st->script->script_vars;
  3998. }
  3999. }
  4000. }
  4001. else
  4002. {// no return value
  4003. script_pushnil(st);
  4004. }
  4005. st->state = RETFUNC;
  4006. return 0;
  4007. }
  4008. /// Returns a random number from 0 to <range>-1.
  4009. /// Or returns a random number from <min> to <max>.
  4010. /// If <min> is greater than <max>, their numbers are switched.
  4011. /// rand(<range>) -> <int>
  4012. /// rand(<min>,<max>) -> <int>
  4013. BUILDIN_FUNC(rand)
  4014. {
  4015. int range;
  4016. int min;
  4017. int max;
  4018. if( script_hasdata(st,3) )
  4019. {// min,max
  4020. min = script_getnum(st,2);
  4021. max = script_getnum(st,3);
  4022. if( max < min )
  4023. swap(min, max);
  4024. range = max - min + 1;
  4025. }
  4026. else
  4027. {// range
  4028. min = 0;
  4029. range = script_getnum(st,2);
  4030. }
  4031. if( range <= 1 )
  4032. script_pushint(st, min);
  4033. else if( range > SHRT_MAX ) {
  4034. int step1 = rand()%(range&0xffff), step2 = rand()%(range>>16);
  4035. script_pushint(st, step1 + (step2<<16) + min);
  4036. }
  4037. else
  4038. script_pushint(st, rand()%range + min);
  4039. return 0;
  4040. }
  4041. /*==========================================
  4042. *
  4043. *------------------------------------------*/
  4044. BUILDIN_FUNC(warp)
  4045. {
  4046. int ret;
  4047. int x,y;
  4048. const char* str;
  4049. TBL_PC* sd;
  4050. sd = script_rid2sd(st);
  4051. if( sd == NULL )
  4052. return 0;
  4053. str = script_getstr(st,2);
  4054. x = script_getnum(st,3);
  4055. y = script_getnum(st,4);
  4056. if(strcmp(str,"Random")==0)
  4057. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4058. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4059. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4060. else
  4061. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4062. if( ret ) {
  4063. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4064. script_reportsrc(st);
  4065. }
  4066. return 0;
  4067. }
  4068. /*==========================================
  4069. * エリア指定ワープ
  4070. *------------------------------------------*/
  4071. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4072. {
  4073. int x,y;
  4074. unsigned int map;
  4075. map=va_arg(ap, unsigned int);
  4076. x=va_arg(ap,int);
  4077. y=va_arg(ap,int);
  4078. if(map == 0)
  4079. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4080. else
  4081. pc_setpos((TBL_PC *)bl,map,x,y,CLR_OUTSIGHT);
  4082. return 0;
  4083. }
  4084. BUILDIN_FUNC(areawarp)
  4085. {
  4086. int x,y,m;
  4087. unsigned int index;
  4088. const char *str;
  4089. const char *mapname;
  4090. int x0,y0,x1,y1;
  4091. mapname=script_getstr(st,2);
  4092. x0=script_getnum(st,3);
  4093. y0=script_getnum(st,4);
  4094. x1=script_getnum(st,5);
  4095. y1=script_getnum(st,6);
  4096. str=script_getstr(st,7);
  4097. x=script_getnum(st,8);
  4098. y=script_getnum(st,9);
  4099. if( (m=map_mapname2mapid(mapname))< 0)
  4100. return 0;
  4101. if(strcmp(str,"Random")==0)
  4102. index = 0;
  4103. else if(!(index=mapindex_name2id(str)))
  4104. return 0;
  4105. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  4106. return 0;
  4107. }
  4108. /*==========================================
  4109. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4110. *------------------------------------------*/
  4111. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4112. {
  4113. int hp, sp;
  4114. hp = va_arg(ap, int);
  4115. sp = va_arg(ap, int);
  4116. pc_percentheal((TBL_PC *)bl,hp,sp);
  4117. return 0;
  4118. }
  4119. BUILDIN_FUNC(areapercentheal)
  4120. {
  4121. int hp,sp,m;
  4122. const char *mapname;
  4123. int x0,y0,x1,y1;
  4124. mapname=script_getstr(st,2);
  4125. x0=script_getnum(st,3);
  4126. y0=script_getnum(st,4);
  4127. x1=script_getnum(st,5);
  4128. y1=script_getnum(st,6);
  4129. hp=script_getnum(st,7);
  4130. sp=script_getnum(st,8);
  4131. if( (m=map_mapname2mapid(mapname))< 0)
  4132. return 0;
  4133. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4134. return 0;
  4135. }
  4136. /*==========================================
  4137. * warpchar [LuzZza]
  4138. * Useful for warp one player from
  4139. * another player npc-session.
  4140. * Using: warpchar "mapname",x,y,Char_ID;
  4141. *------------------------------------------*/
  4142. BUILDIN_FUNC(warpchar)
  4143. {
  4144. int x,y,a;
  4145. const char *str;
  4146. TBL_PC *sd;
  4147. str=script_getstr(st,2);
  4148. x=script_getnum(st,3);
  4149. y=script_getnum(st,4);
  4150. a=script_getnum(st,5);
  4151. sd = map_charid2sd(a);
  4152. if( sd == NULL )
  4153. return 0;
  4154. if(strcmp(str, "Random") == 0)
  4155. pc_randomwarp(sd, CLR_TELEPORT);
  4156. else
  4157. if(strcmp(str, "SavePoint") == 0)
  4158. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4159. else
  4160. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4161. return 0;
  4162. }
  4163. /*==========================================
  4164. * Warpparty - [Fredzilla] [Paradox924X]
  4165. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4166. * If 'from_mapname' is specified, only the party members on that map will be warped
  4167. *------------------------------------------*/
  4168. BUILDIN_FUNC(warpparty)
  4169. {
  4170. TBL_PC *sd = NULL;
  4171. TBL_PC *pl_sd;
  4172. struct party_data* p;
  4173. int type;
  4174. int mapindex;
  4175. int i;
  4176. const char* str = script_getstr(st,2);
  4177. int x = script_getnum(st,3);
  4178. int y = script_getnum(st,4);
  4179. int p_id = script_getnum(st,5);
  4180. const char* str2 = NULL;
  4181. if ( script_hasdata(st,6) )
  4182. str2 = script_getstr(st,6);
  4183. p = party_search(p_id);
  4184. if(!p)
  4185. return 0;
  4186. type = ( strcmp(str,"Random")==0 ) ? 0
  4187. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4188. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4189. : ( strcmp(str,"Leader")==0 ) ? 3
  4190. : 4;
  4191. switch (type)
  4192. {
  4193. case 3:
  4194. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4195. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4196. return 0;
  4197. pl_sd = p->data[i].sd;
  4198. mapindex = pl_sd->mapindex;
  4199. x = pl_sd->bl.x;
  4200. y = pl_sd->bl.y;
  4201. break;
  4202. case 4:
  4203. mapindex = mapindex_name2id(str);
  4204. break;
  4205. case 2:
  4206. //"SavePoint" uses save point of the currently attached player
  4207. if (( sd = script_rid2sd(st) ) == NULL )
  4208. return 0;
  4209. default:
  4210. mapindex = 0;
  4211. break;
  4212. }
  4213. for (i = 0; i < MAX_PARTY; i++)
  4214. {
  4215. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4216. continue;
  4217. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4218. continue;
  4219. if( pc_isdead(pl_sd) )
  4220. continue;
  4221. switch( type )
  4222. {
  4223. case 0: // Random
  4224. if(!map[pl_sd->bl.m].flag.nowarp)
  4225. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4226. break;
  4227. case 1: // SavePointAll
  4228. if(!map[pl_sd->bl.m].flag.noreturn)
  4229. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4230. break;
  4231. case 2: // SavePoint
  4232. if(!map[pl_sd->bl.m].flag.noreturn)
  4233. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4234. break;
  4235. case 3: // Leader
  4236. case 4: // m,x,y
  4237. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4238. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4239. break;
  4240. }
  4241. }
  4242. return 0;
  4243. }
  4244. /*==========================================
  4245. * Warpguild - [Fredzilla]
  4246. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4247. *------------------------------------------*/
  4248. BUILDIN_FUNC(warpguild)
  4249. {
  4250. TBL_PC *sd = NULL;
  4251. TBL_PC *pl_sd;
  4252. struct guild* g;
  4253. struct s_mapiterator* iter;
  4254. int type;
  4255. const char* str = script_getstr(st,2);
  4256. int x = script_getnum(st,3);
  4257. int y = script_getnum(st,4);
  4258. int gid = script_getnum(st,5);
  4259. g = guild_search(gid);
  4260. if( g == NULL )
  4261. return 0;
  4262. type = ( strcmp(str,"Random")==0 ) ? 0
  4263. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4264. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4265. : 3;
  4266. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4267. {// "SavePoint" uses save point of the currently attached player
  4268. return 0;
  4269. }
  4270. iter = mapit_getallusers();
  4271. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4272. {
  4273. if( pl_sd->status.guild_id != gid )
  4274. continue;
  4275. switch( type )
  4276. {
  4277. case 0: // Random
  4278. if(!map[pl_sd->bl.m].flag.nowarp)
  4279. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4280. break;
  4281. case 1: // SavePointAll
  4282. if(!map[pl_sd->bl.m].flag.noreturn)
  4283. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4284. break;
  4285. case 2: // SavePoint
  4286. if(!map[pl_sd->bl.m].flag.noreturn)
  4287. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4288. break;
  4289. case 3: // m,x,y
  4290. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4291. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4292. break;
  4293. }
  4294. }
  4295. mapit_free(iter);
  4296. return 0;
  4297. }
  4298. /*==========================================
  4299. *
  4300. *------------------------------------------*/
  4301. BUILDIN_FUNC(heal)
  4302. {
  4303. TBL_PC *sd;
  4304. int hp,sp;
  4305. sd = script_rid2sd(st);
  4306. if (!sd) return 0;
  4307. hp=script_getnum(st,2);
  4308. sp=script_getnum(st,3);
  4309. status_heal(&sd->bl, hp, sp, 1);
  4310. return 0;
  4311. }
  4312. /*==========================================
  4313. *
  4314. *------------------------------------------*/
  4315. BUILDIN_FUNC(itemheal)
  4316. {
  4317. TBL_PC *sd;
  4318. int hp,sp;
  4319. hp=script_getnum(st,2);
  4320. sp=script_getnum(st,3);
  4321. if(potion_flag==1) {
  4322. potion_hp = hp;
  4323. potion_sp = sp;
  4324. return 0;
  4325. }
  4326. sd = script_rid2sd(st);
  4327. if (!sd) return 0;
  4328. pc_itemheal(sd,sd->itemid,hp,sp);
  4329. return 0;
  4330. }
  4331. /*==========================================
  4332. *
  4333. *------------------------------------------*/
  4334. BUILDIN_FUNC(percentheal)
  4335. {
  4336. int hp,sp;
  4337. TBL_PC* sd;
  4338. hp=script_getnum(st,2);
  4339. sp=script_getnum(st,3);
  4340. if(potion_flag==1) {
  4341. potion_per_hp = hp;
  4342. potion_per_sp = sp;
  4343. return 0;
  4344. }
  4345. sd = script_rid2sd(st);
  4346. if( sd == NULL )
  4347. return 0;
  4348. pc_percentheal(sd,hp,sp);
  4349. return 0;
  4350. }
  4351. /*==========================================
  4352. *
  4353. *------------------------------------------*/
  4354. BUILDIN_FUNC(jobchange)
  4355. {
  4356. int job, upper=-1;
  4357. job=script_getnum(st,2);
  4358. if( script_hasdata(st,3) )
  4359. upper=script_getnum(st,3);
  4360. if (pcdb_checkid(job))
  4361. {
  4362. TBL_PC* sd;
  4363. sd = script_rid2sd(st);
  4364. if( sd == NULL )
  4365. return 0;
  4366. pc_jobchange(sd, job, upper);
  4367. }
  4368. return 0;
  4369. }
  4370. /*==========================================
  4371. *
  4372. *------------------------------------------*/
  4373. BUILDIN_FUNC(jobname)
  4374. {
  4375. int class_=script_getnum(st,2);
  4376. script_pushconststr(st, (char*)job_name(class_));
  4377. return 0;
  4378. }
  4379. /// Get input from the player.
  4380. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4381. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4382. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4383. /// shorter than 'min' and 0 otherwise.
  4384. ///
  4385. /// input(<var>{,<min>{,<max>}}) -> <int>
  4386. BUILDIN_FUNC(input)
  4387. {
  4388. TBL_PC* sd;
  4389. struct script_data* data;
  4390. int uid;
  4391. const char* name;
  4392. int min;
  4393. int max;
  4394. sd = script_rid2sd(st);
  4395. if( sd == NULL )
  4396. return 0;
  4397. data = script_getdata(st,2);
  4398. if( !data_isreference(data) ){
  4399. ShowError("script:input: not a variable\n");
  4400. script_reportdata(data);
  4401. st->state = END;
  4402. return 1;
  4403. }
  4404. uid = reference_getuid(data);
  4405. name = reference_getname(data);
  4406. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4407. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4408. if( !sd->state.menu_or_input )
  4409. { // first invocation, display npc input box
  4410. sd->state.menu_or_input = 1;
  4411. st->state = RERUNLINE;
  4412. if( is_string_variable(name) )
  4413. clif_scriptinputstr(sd,st->oid);
  4414. else
  4415. clif_scriptinput(sd,st->oid);
  4416. }
  4417. else
  4418. { // take received text/value and store it in the designated variable
  4419. sd->state.menu_or_input = 0;
  4420. if( is_string_variable(name) )
  4421. {
  4422. int len = (int)strlen(sd->npc_str);
  4423. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4424. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4425. }
  4426. else
  4427. {
  4428. int amount = sd->npc_amount;
  4429. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4430. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4431. }
  4432. st->state = RUN;
  4433. }
  4434. return 0;
  4435. }
  4436. /// Sets the value of a variable.
  4437. /// The value is converted to the type of the variable.
  4438. ///
  4439. /// set(<variable>,<value>) -> <variable>
  4440. BUILDIN_FUNC(set)
  4441. {
  4442. TBL_PC* sd = NULL;
  4443. struct script_data* data;
  4444. int num;
  4445. const char* name;
  4446. char prefix;
  4447. data = script_getdata(st,2);
  4448. if( !data_isreference(data) )
  4449. {
  4450. ShowError("script:set: not a variable\n");
  4451. script_reportdata(script_getdata(st,2));
  4452. st->state = END;
  4453. return 1;
  4454. }
  4455. num = reference_getuid(data);
  4456. name = reference_getname(data);
  4457. prefix = *name;
  4458. if( not_server_variable(prefix) )
  4459. {
  4460. sd = script_rid2sd(st);
  4461. if( sd == NULL )
  4462. {
  4463. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4464. return 0;
  4465. }
  4466. }
  4467. if( is_string_variable(name) )
  4468. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4469. else
  4470. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4471. // return a copy of the variable reference
  4472. script_pushcopy(st,2);
  4473. return 0;
  4474. }
  4475. /////////////////////////////////////////////////////////////////////
  4476. /// Array variables
  4477. ///
  4478. /// Returns the size of the specified array
  4479. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4480. {
  4481. int32 ret = idx;
  4482. if( isstring )
  4483. {
  4484. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4485. {
  4486. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4487. if( str && *str )
  4488. ret = idx + 1;
  4489. script_removetop(st, -1, 0);
  4490. }
  4491. }
  4492. else
  4493. {
  4494. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4495. {
  4496. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4497. if( num )
  4498. ret = idx + 1;
  4499. script_removetop(st, -1, 0);
  4500. }
  4501. }
  4502. return ret;
  4503. }
  4504. /// Sets values of an array, from the starting index.
  4505. /// ex: setarray arr[1],1,2,3;
  4506. ///
  4507. /// setarray <array variable>,<value1>{,<value2>...};
  4508. BUILDIN_FUNC(setarray)
  4509. {
  4510. struct script_data* data;
  4511. const char* name;
  4512. int32 start;
  4513. int32 end;
  4514. int32 id;
  4515. int32 i;
  4516. TBL_PC* sd = NULL;
  4517. data = script_getdata(st, 2);
  4518. if( !data_isreference(data) )
  4519. {
  4520. ShowError("script:setarray: not a variable\n");
  4521. script_reportdata(data);
  4522. st->state = END;
  4523. return 1;// not a variable
  4524. }
  4525. id = reference_getid(data);
  4526. start = reference_getindex(data);
  4527. name = reference_getname(data);
  4528. if( not_array_variable(*name) )
  4529. {
  4530. ShowError("script:setarray: illegal scope\n");
  4531. script_reportdata(data);
  4532. st->state = END;
  4533. return 1;// not supported
  4534. }
  4535. if( not_server_variable(*name) )
  4536. {
  4537. sd = script_rid2sd(st);
  4538. if( sd == NULL )
  4539. return 0;// no player attached
  4540. }
  4541. end = start + script_lastdata(st) - 2;
  4542. if( end > SCRIPT_MAX_ARRAYSIZE )
  4543. end = SCRIPT_MAX_ARRAYSIZE;
  4544. if( is_string_variable(name) )
  4545. {// string array
  4546. for( i = 3; start < end; ++start, ++i )
  4547. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4548. }
  4549. else
  4550. {// int array
  4551. for( i = 3; start < end; ++start, ++i )
  4552. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4553. }
  4554. return 0;
  4555. }
  4556. /// Sets count values of an array, from the starting index.
  4557. /// ex: cleararray arr[0],0,1;
  4558. ///
  4559. /// cleararray <array variable>,<value>,<count>;
  4560. BUILDIN_FUNC(cleararray)
  4561. {
  4562. struct script_data* data;
  4563. const char* name;
  4564. int32 start;
  4565. int32 end;
  4566. int32 id;
  4567. void* v;
  4568. TBL_PC* sd = NULL;
  4569. data = script_getdata(st, 2);
  4570. if( !data_isreference(data) )
  4571. {
  4572. ShowError("script:cleararray: not a variable\n");
  4573. script_reportdata(data);
  4574. st->state = END;
  4575. return 1;// not a variable
  4576. }
  4577. id = reference_getid(data);
  4578. start = reference_getindex(data);
  4579. name = reference_getname(data);
  4580. if( not_array_variable(*name) )
  4581. {
  4582. ShowError("script:cleararray: illegal scope\n");
  4583. script_reportdata(data);
  4584. st->state = END;
  4585. return 1;// not supported
  4586. }
  4587. if( not_server_variable(*name) )
  4588. {
  4589. sd = script_rid2sd(st);
  4590. if( sd == NULL )
  4591. return 0;// no player attached
  4592. }
  4593. if( is_string_variable(name) )
  4594. v = (void*)script_getstr(st, 3);
  4595. else
  4596. v = (void*)script_getnum(st, 3);
  4597. end = start + script_getnum(st, 4);
  4598. if( end > SCRIPT_MAX_ARRAYSIZE )
  4599. end = SCRIPT_MAX_ARRAYSIZE;
  4600. for( ; start < end; ++start )
  4601. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4602. return 0;
  4603. }
  4604. /// Copies data from one array to another.
  4605. /// ex: copyarray arr[0],arr[2],2;
  4606. ///
  4607. /// copyarray <destination array variable>,<source array variable>,<count>;
  4608. BUILDIN_FUNC(copyarray)
  4609. {
  4610. struct script_data* data1;
  4611. struct script_data* data2;
  4612. const char* name1;
  4613. const char* name2;
  4614. int32 idx1;
  4615. int32 idx2;
  4616. int32 id1;
  4617. int32 id2;
  4618. void* v;
  4619. int32 i;
  4620. int32 count;
  4621. TBL_PC* sd = NULL;
  4622. data1 = script_getdata(st, 2);
  4623. data2 = script_getdata(st, 3);
  4624. if( !data_isreference(data1) || !data_isreference(data2) )
  4625. {
  4626. ShowError("script:copyarray: not a variable\n");
  4627. script_reportdata(data1);
  4628. script_reportdata(data2);
  4629. st->state = END;
  4630. return 1;// not a variable
  4631. }
  4632. id1 = reference_getid(data1);
  4633. id2 = reference_getid(data2);
  4634. idx1 = reference_getindex(data1);
  4635. idx2 = reference_getindex(data2);
  4636. name1 = reference_getname(data1);
  4637. name2 = reference_getname(data2);
  4638. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4639. {
  4640. ShowError("script:copyarray: illegal scope\n");
  4641. script_reportdata(data1);
  4642. script_reportdata(data2);
  4643. st->state = END;
  4644. return 1;// not supported
  4645. }
  4646. if( is_string_variable(name1) != is_string_variable(name2) )
  4647. {
  4648. ShowError("script:copyarray: type mismatch\n");
  4649. script_reportdata(data1);
  4650. script_reportdata(data2);
  4651. st->state = END;
  4652. return 1;// data type mismatch
  4653. }
  4654. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4655. {
  4656. sd = script_rid2sd(st);
  4657. if( sd == NULL )
  4658. return 0;// no player attached
  4659. }
  4660. count = script_getnum(st, 4);
  4661. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  4662. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  4663. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4664. return 0;// nothing to copy
  4665. if( id1 == id2 && idx1 > idx2 )
  4666. {// destination might be overlapping the source - copy in reverse order
  4667. for( i = count - 1; i >= 0; --i )
  4668. {
  4669. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4670. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4671. script_removetop(st, -1, 0);
  4672. }
  4673. }
  4674. else
  4675. {// normal copy
  4676. for( i = 0; i < count; ++i )
  4677. {
  4678. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  4679. {
  4680. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4681. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4682. script_removetop(st, -1, 0);
  4683. }
  4684. else// out of range - assume ""/0
  4685. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4686. }
  4687. }
  4688. return 0;
  4689. }
  4690. /// Returns the size of the array.
  4691. /// Assumes that everything before the starting index exists.
  4692. /// ex: getarraysize(arr[3])
  4693. ///
  4694. /// getarraysize(<array variable>) -> <int>
  4695. BUILDIN_FUNC(getarraysize)
  4696. {
  4697. struct script_data* data;
  4698. const char* name;
  4699. data = script_getdata(st, 2);
  4700. if( !data_isreference(data) )
  4701. {
  4702. ShowError("script:getarraysize: not a variable\n");
  4703. script_reportdata(data);
  4704. script_pushnil(st);
  4705. st->state = END;
  4706. return 1;// not a variable
  4707. }
  4708. name = reference_getname(data);
  4709. if( not_array_variable(*name) )
  4710. {
  4711. ShowError("script:getarraysize: illegal scope\n");
  4712. script_reportdata(data);
  4713. script_pushnil(st);
  4714. st->state = END;
  4715. return 1;// not supported
  4716. }
  4717. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4718. return 0;
  4719. }
  4720. /// Deletes count or all the elements in an array, from the starting index.
  4721. /// ex: deletearray arr[4],2;
  4722. ///
  4723. /// deletearray <array variable>;
  4724. /// deletearray <array variable>,<count>;
  4725. BUILDIN_FUNC(deletearray)
  4726. {
  4727. struct script_data* data;
  4728. const char* name;
  4729. int start;
  4730. int end;
  4731. int id;
  4732. TBL_PC *sd = NULL;
  4733. data = script_getdata(st, 2);
  4734. if( !data_isreference(data) )
  4735. {
  4736. ShowError("script:deletearray: not a variable\n");
  4737. script_reportdata(data);
  4738. st->state = END;
  4739. return 1;// not a variable
  4740. }
  4741. id = reference_getid(data);
  4742. start = reference_getindex(data);
  4743. name = reference_getname(data);
  4744. if( not_array_variable(*name) )
  4745. {
  4746. ShowError("script:deletearray: illegal scope\n");
  4747. script_reportdata(data);
  4748. st->state = END;
  4749. return 1;// not supported
  4750. }
  4751. if( not_server_variable(*name) )
  4752. {
  4753. sd = script_rid2sd(st);
  4754. if( sd == NULL )
  4755. return 0;// no player attached
  4756. }
  4757. end = SCRIPT_MAX_ARRAYSIZE;
  4758. if( start >= end )
  4759. return 0;// nothing to free
  4760. if( script_hasdata(st,3) )
  4761. {
  4762. int count = script_getnum(st, 3);
  4763. if( count > end - start )
  4764. count = end - start;
  4765. if( count <= 0 )
  4766. return 0;// nothing to free
  4767. // move rest of the elements backward
  4768. for( ; start + count < end; ++start )
  4769. {
  4770. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4771. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4772. script_removetop(st, -1, 0);
  4773. }
  4774. }
  4775. // clear the rest of the array
  4776. if( is_string_variable(name) )
  4777. {
  4778. for( ; start < end; ++start )
  4779. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4780. }
  4781. else
  4782. {
  4783. for( ; start < end; ++start )
  4784. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4785. }
  4786. return 0;
  4787. }
  4788. /// Returns a reference to the target index of the array variable.
  4789. /// Equivalent to var[index].
  4790. ///
  4791. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4792. BUILDIN_FUNC(getelementofarray)
  4793. {
  4794. struct script_data* data;
  4795. const char* name;
  4796. int32 id;
  4797. int i;
  4798. data = script_getdata(st, 2);
  4799. if( !data_isreference(data) )
  4800. {
  4801. ShowError("script:getelementofarray: not a variable\n");
  4802. script_reportdata(data);
  4803. script_pushnil(st);
  4804. st->state = END;
  4805. return 1;// not a variable
  4806. }
  4807. id = reference_getid(data);
  4808. name = reference_getname(data);
  4809. if( not_array_variable(*name) )
  4810. {
  4811. ShowError("script:getelementofarray: illegal scope\n");
  4812. script_reportdata(data);
  4813. script_pushnil(st);
  4814. st->state = END;
  4815. return 1;// not supported
  4816. }
  4817. i = script_getnum(st, 3);
  4818. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  4819. {
  4820. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4821. script_reportdata(data);
  4822. script_pushnil(st);
  4823. st->state = END;
  4824. return 1;// out of range
  4825. }
  4826. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4827. return 0;
  4828. }
  4829. /////////////////////////////////////////////////////////////////////
  4830. /// ...
  4831. ///
  4832. /*==========================================
  4833. *
  4834. *------------------------------------------*/
  4835. BUILDIN_FUNC(setlook)
  4836. {
  4837. int type,val;
  4838. TBL_PC* sd;
  4839. type=script_getnum(st,2);
  4840. val=script_getnum(st,3);
  4841. sd = script_rid2sd(st);
  4842. if( sd == NULL )
  4843. return 0;
  4844. pc_changelook(sd,type,val);
  4845. return 0;
  4846. }
  4847. BUILDIN_FUNC(changelook)
  4848. { // As setlook but only client side
  4849. int type,val;
  4850. TBL_PC* sd;
  4851. type=script_getnum(st,2);
  4852. val=script_getnum(st,3);
  4853. sd = script_rid2sd(st);
  4854. if( sd == NULL )
  4855. return 0;
  4856. clif_changelook(&sd->bl,type,val);
  4857. return 0;
  4858. }
  4859. /*==========================================
  4860. *
  4861. *------------------------------------------*/
  4862. BUILDIN_FUNC(cutin)
  4863. {
  4864. TBL_PC* sd;
  4865. sd = script_rid2sd(st);
  4866. if( sd == NULL )
  4867. return 0;
  4868. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4869. return 0;
  4870. }
  4871. /*==========================================
  4872. *
  4873. *------------------------------------------*/
  4874. BUILDIN_FUNC(viewpoint)
  4875. {
  4876. int type,x,y,id,color;
  4877. TBL_PC* sd;
  4878. type=script_getnum(st,2);
  4879. x=script_getnum(st,3);
  4880. y=script_getnum(st,4);
  4881. id=script_getnum(st,5);
  4882. color=script_getnum(st,6);
  4883. sd = script_rid2sd(st);
  4884. if( sd == NULL )
  4885. return 0;
  4886. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4887. return 0;
  4888. }
  4889. /*==========================================
  4890. *
  4891. *------------------------------------------*/
  4892. BUILDIN_FUNC(countitem)
  4893. {
  4894. int nameid, i;
  4895. int count = 0;
  4896. struct item_data* id = NULL;
  4897. struct script_data* data;
  4898. TBL_PC* sd = script_rid2sd(st);
  4899. if (!sd) {
  4900. script_pushint(st,0);
  4901. return 0;
  4902. }
  4903. data = script_getdata(st,2);
  4904. get_val(st, data); // convert into value in case of a variable
  4905. if( data_isstring(data) )
  4906. {// item name
  4907. id = itemdb_searchname(conv_str(st, data));
  4908. }
  4909. else
  4910. {// item id
  4911. id = itemdb_exists(conv_num(st, data));
  4912. }
  4913. if( id == NULL )
  4914. {
  4915. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4916. script_pushint(st,0);
  4917. return 1;
  4918. }
  4919. nameid = id->nameid;
  4920. for(i = 0; i < MAX_INVENTORY; i++)
  4921. if(sd->status.inventory[i].nameid == nameid)
  4922. count += sd->status.inventory[i].amount;
  4923. script_pushint(st,count);
  4924. return 0;
  4925. }
  4926. /*==========================================
  4927. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4928. * returns number of items that meet the conditions
  4929. *------------------------------------------*/
  4930. BUILDIN_FUNC(countitem2)
  4931. {
  4932. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4933. int count = 0;
  4934. int i;
  4935. struct item_data* id = NULL;
  4936. struct script_data* data;
  4937. TBL_PC* sd = script_rid2sd(st);
  4938. if (!sd) {
  4939. script_pushint(st,0);
  4940. return 0;
  4941. }
  4942. data = script_getdata(st,2);
  4943. get_val(st, data); // convert into value in case of a variable
  4944. if( data_isstring(data) )
  4945. {// item name
  4946. id = itemdb_searchname(conv_str(st, data));
  4947. }
  4948. else
  4949. {// item id
  4950. id = itemdb_exists(conv_num(st, data));
  4951. }
  4952. if( id == NULL )
  4953. {
  4954. ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4955. script_pushint(st,0);
  4956. return 1;
  4957. }
  4958. nameid = id->nameid;
  4959. iden = script_getnum(st,3);
  4960. ref = script_getnum(st,4);
  4961. attr = script_getnum(st,5);
  4962. c1 = (short)script_getnum(st,6);
  4963. c2 = (short)script_getnum(st,7);
  4964. c3 = (short)script_getnum(st,8);
  4965. c4 = (short)script_getnum(st,9);
  4966. for(i = 0; i < MAX_INVENTORY; i++)
  4967. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4968. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4969. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4970. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4971. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4972. sd->status.inventory[i].card[3] == c4
  4973. )
  4974. count += sd->status.inventory[i].amount;
  4975. script_pushint(st,count);
  4976. return 0;
  4977. }
  4978. /*==========================================
  4979. * 重量チェック
  4980. *------------------------------------------*/
  4981. BUILDIN_FUNC(checkweight)
  4982. {
  4983. int nameid, amount, slots;
  4984. unsigned int weight;
  4985. struct item_data* id = NULL;
  4986. struct map_session_data* sd;
  4987. struct script_data* data;
  4988. if( ( sd = script_rid2sd(st) ) == NULL )
  4989. {
  4990. return 0;
  4991. }
  4992. data = script_getdata(st,2);
  4993. get_val(st, data); // convert into value in case of a variable
  4994. if( data_isstring(data) )
  4995. {// item name
  4996. id = itemdb_searchname(conv_str(st, data));
  4997. }
  4998. else
  4999. {// item id
  5000. id = itemdb_exists(conv_num(st, data));
  5001. }
  5002. if( id == NULL )
  5003. {
  5004. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5005. script_pushint(st,0);
  5006. return 1;
  5007. }
  5008. nameid = id->nameid;
  5009. amount = script_getnum(st,3);
  5010. if( amount < 1 )
  5011. {
  5012. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5013. script_pushint(st,0);
  5014. return 1;
  5015. }
  5016. weight = itemdb_weight(nameid)*amount;
  5017. if( weight + sd->weight > sd->max_weight )
  5018. {// too heavy
  5019. script_pushint(st,0);
  5020. return 0;
  5021. }
  5022. switch( pc_checkadditem(sd, nameid, amount) )
  5023. {
  5024. case ADDITEM_EXIST:
  5025. // item is already in inventory, but there is still space for the requested amount
  5026. break;
  5027. case ADDITEM_NEW:
  5028. slots = pc_inventoryblank(sd);
  5029. if( itemdb_isstackable(nameid) )
  5030. {// stackable
  5031. if( slots < 1 )
  5032. {
  5033. script_pushint(st,0);
  5034. return 0;
  5035. }
  5036. }
  5037. else
  5038. {// non-stackable
  5039. if( slots < amount )
  5040. {
  5041. script_pushint(st,0);
  5042. return 0;
  5043. }
  5044. }
  5045. break;
  5046. case ADDITEM_OVERAMOUNT:
  5047. script_pushint(st,0);
  5048. return 0;
  5049. }
  5050. script_pushint(st,1);
  5051. return 0;
  5052. }
  5053. /*==========================================
  5054. * getitem <item id>,<amount>{,<account ID>};
  5055. * getitem "<item name>",<amount>{,<account ID>};
  5056. *------------------------------------------*/
  5057. BUILDIN_FUNC(getitem)
  5058. {
  5059. int nameid,amount,get_count,i,flag = 0;
  5060. struct item it;
  5061. TBL_PC *sd;
  5062. struct script_data *data;
  5063. data=script_getdata(st,2);
  5064. get_val(st,data);
  5065. if( data_isstring(data) )
  5066. {// "<item name>"
  5067. const char *name=conv_str(st,data);
  5068. struct item_data *item_data = itemdb_searchname(name);
  5069. if( item_data == NULL ){
  5070. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5071. return 1; //No item created.
  5072. }
  5073. nameid=item_data->nameid;
  5074. } else if( data_isint(data) )
  5075. {// <item id>
  5076. nameid=conv_num(st,data);
  5077. //Violet Box, Blue Box, etc - random item pick
  5078. if( nameid < 0 ) {
  5079. nameid=itemdb_searchrandomid(-nameid);
  5080. flag = 1;
  5081. }
  5082. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5083. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5084. return 1; //No item created.
  5085. }
  5086. } else {
  5087. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5088. return 1;
  5089. }
  5090. // <amount>
  5091. if( (amount=script_getnum(st,3)) <= 0)
  5092. return 0; //return if amount <=0, skip the useles iteration
  5093. memset(&it,0,sizeof(it));
  5094. it.nameid=nameid;
  5095. if(!flag)
  5096. it.identify=1;
  5097. else
  5098. it.identify=itemdb_isidentified(nameid);
  5099. if( script_hasdata(st,4) )
  5100. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5101. else
  5102. sd=script_rid2sd(st); // Attached player
  5103. if( sd == NULL ) // no target
  5104. return 0;
  5105. //Check if it's stackable.
  5106. if (!itemdb_isstackable(nameid))
  5107. get_count = 1;
  5108. else
  5109. get_count = amount;
  5110. for (i = 0; i < amount; i += get_count)
  5111. {
  5112. // if not pet egg
  5113. if (!pet_create_egg(sd, nameid))
  5114. {
  5115. if ((flag = pc_additem(sd, &it, get_count)))
  5116. {
  5117. clif_additem(sd, 0, 0, flag);
  5118. if( pc_candrop(sd,&it) )
  5119. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5120. }
  5121. }
  5122. }
  5123. //Logs items, got from (N)PC scripts [Lupus]
  5124. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  5125. log_pick_pc(sd, "N", nameid, amount, NULL);
  5126. return 0;
  5127. }
  5128. /*==========================================
  5129. *
  5130. *------------------------------------------*/
  5131. BUILDIN_FUNC(getitem2)
  5132. {
  5133. int nameid,amount,get_count,i,flag = 0;
  5134. int iden,ref,attr,c1,c2,c3,c4;
  5135. struct item_data *item_data;
  5136. struct item item_tmp;
  5137. TBL_PC *sd;
  5138. struct script_data *data;
  5139. if( script_hasdata(st,11) )
  5140. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5141. else
  5142. sd=script_rid2sd(st); // Attached player
  5143. if( sd == NULL ) // no target
  5144. return 0;
  5145. data=script_getdata(st,2);
  5146. get_val(st,data);
  5147. if( data_isstring(data) ){
  5148. const char *name=conv_str(st,data);
  5149. struct item_data *item_data = itemdb_searchname(name);
  5150. if( item_data )
  5151. nameid=item_data->nameid;
  5152. else
  5153. nameid=UNKNOWN_ITEM_ID;
  5154. }else
  5155. nameid=conv_num(st,data);
  5156. amount=script_getnum(st,3);
  5157. iden=script_getnum(st,4);
  5158. ref=script_getnum(st,5);
  5159. attr=script_getnum(st,6);
  5160. c1=(short)script_getnum(st,7);
  5161. c2=(short)script_getnum(st,8);
  5162. c3=(short)script_getnum(st,9);
  5163. c4=(short)script_getnum(st,10);
  5164. if(nameid<0) { // ランダム
  5165. nameid=itemdb_searchrandomid(-nameid);
  5166. flag = 1;
  5167. }
  5168. if(nameid > 0) {
  5169. memset(&item_tmp,0,sizeof(item_tmp));
  5170. item_data=itemdb_exists(nameid);
  5171. if (item_data == NULL)
  5172. return -1;
  5173. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5174. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5175. }
  5176. else if(item_data->type==IT_PETEGG) {
  5177. iden = 1;
  5178. ref = 0;
  5179. }
  5180. else {
  5181. iden = 1;
  5182. ref = attr = 0;
  5183. }
  5184. item_tmp.nameid=nameid;
  5185. if(!flag)
  5186. item_tmp.identify=iden;
  5187. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5188. item_tmp.identify=0;
  5189. item_tmp.refine=ref;
  5190. item_tmp.attribute=attr;
  5191. item_tmp.card[0]=(short)c1;
  5192. item_tmp.card[1]=(short)c2;
  5193. item_tmp.card[2]=(short)c3;
  5194. item_tmp.card[3]=(short)c4;
  5195. //Check if it's stackable.
  5196. if (!itemdb_isstackable(nameid))
  5197. get_count = 1;
  5198. else
  5199. get_count = amount;
  5200. for (i = 0; i < amount; i += get_count)
  5201. {
  5202. // if not pet egg
  5203. if (!pet_create_egg(sd, nameid))
  5204. {
  5205. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  5206. {
  5207. clif_additem(sd, 0, 0, flag);
  5208. if( pc_candrop(sd,&item_tmp) )
  5209. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5210. }
  5211. }
  5212. }
  5213. //Logs items, got from (N)PC scripts [Lupus]
  5214. if(log_config.enable_logs&0x40)
  5215. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  5216. }
  5217. return 0;
  5218. }
  5219. /*==========================================
  5220. * rentitem <item id>,<seconds>
  5221. * rentitem "<item name>",<seconds>
  5222. *------------------------------------------*/
  5223. BUILDIN_FUNC(rentitem)
  5224. {
  5225. struct map_session_data *sd;
  5226. struct script_data *data;
  5227. struct item it;
  5228. int seconds;
  5229. int nameid = 0, flag;
  5230. data = script_getdata(st,2);
  5231. get_val(st,data);
  5232. if( (sd = script_rid2sd(st)) == NULL )
  5233. return 0;
  5234. if( data_isstring(data) )
  5235. {
  5236. const char *name = conv_str(st,data);
  5237. struct item_data *itd = itemdb_searchname(name);
  5238. if( itd == NULL )
  5239. {
  5240. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5241. return 1;
  5242. }
  5243. nameid = itd->nameid;
  5244. }
  5245. else if( data_isint(data) )
  5246. {
  5247. nameid = conv_num(st,data);
  5248. if( nameid <= 0 || !itemdb_exists(nameid) )
  5249. {
  5250. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5251. return 1;
  5252. }
  5253. }
  5254. else
  5255. {
  5256. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5257. return 1;
  5258. }
  5259. seconds = script_getnum(st,3);
  5260. memset(&it, 0, sizeof(it));
  5261. it.nameid = nameid;
  5262. it.identify = 1;
  5263. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5264. if( (flag = pc_additem(sd, &it, 1)) )
  5265. {
  5266. clif_additem(sd, 0, 0, flag);
  5267. return 1;
  5268. }
  5269. clif_rental_time(sd->fd, nameid, seconds);
  5270. pc_inventory_rental_add(sd, seconds);
  5271. if( log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS )
  5272. log_pick_pc(sd, "N", nameid, 1, NULL);
  5273. return 0;
  5274. }
  5275. /*==========================================
  5276. * gets an item with someone's name inscribed [Skotlex]
  5277. * getinscribeditem item_num, character_name
  5278. * Returned Qty is always 1, only works on equip-able
  5279. * equipment
  5280. *------------------------------------------*/
  5281. BUILDIN_FUNC(getnameditem)
  5282. {
  5283. int nameid;
  5284. struct item item_tmp;
  5285. TBL_PC *sd, *tsd;
  5286. struct script_data *data;
  5287. sd = script_rid2sd(st);
  5288. if (sd == NULL)
  5289. { //Player not attached!
  5290. script_pushint(st,0);
  5291. return 0;
  5292. }
  5293. data=script_getdata(st,2);
  5294. get_val(st,data);
  5295. if( data_isstring(data) ){
  5296. const char *name=conv_str(st,data);
  5297. struct item_data *item_data = itemdb_searchname(name);
  5298. if( item_data == NULL)
  5299. { //Failed
  5300. script_pushint(st,0);
  5301. return 0;
  5302. }
  5303. nameid = item_data->nameid;
  5304. }else
  5305. nameid = conv_num(st,data);
  5306. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5307. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5308. script_pushint(st,0);
  5309. return 0;
  5310. }
  5311. data=script_getdata(st,3);
  5312. get_val(st,data);
  5313. if( data_isstring(data) ) //Char Name
  5314. tsd=map_nick2sd(conv_str(st,data));
  5315. else //Char Id was given
  5316. tsd=map_charid2sd(conv_num(st,data));
  5317. if( tsd == NULL )
  5318. { //Failed
  5319. script_pushint(st,0);
  5320. return 0;
  5321. }
  5322. memset(&item_tmp,0,sizeof(item_tmp));
  5323. item_tmp.nameid=nameid;
  5324. item_tmp.amount=1;
  5325. item_tmp.identify=1;
  5326. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5327. item_tmp.card[2]=tsd->status.char_id;
  5328. item_tmp.card[3]=tsd->status.char_id >> 16;
  5329. if(pc_additem(sd,&item_tmp,1)) {
  5330. script_pushint(st,0);
  5331. return 0; //Failed to add item, we will not drop if they don't fit
  5332. }
  5333. //Logs items, got from (N)PC scripts [Lupus]
  5334. if(log_config.enable_logs&0x40)
  5335. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5336. script_pushint(st,1);
  5337. return 0;
  5338. }
  5339. /*==========================================
  5340. * gets a random item ID from an item group [Skotlex]
  5341. * groupranditem group_num
  5342. *------------------------------------------*/
  5343. BUILDIN_FUNC(grouprandomitem)
  5344. {
  5345. int group;
  5346. group = script_getnum(st,2);
  5347. script_pushint(st,itemdb_searchrandomid(group));
  5348. return 0;
  5349. }
  5350. /*==========================================
  5351. *
  5352. *------------------------------------------*/
  5353. BUILDIN_FUNC(makeitem)
  5354. {
  5355. int nameid,amount,flag = 0;
  5356. int x,y,m;
  5357. const char *mapname;
  5358. struct item item_tmp;
  5359. struct script_data *data;
  5360. data=script_getdata(st,2);
  5361. get_val(st,data);
  5362. if( data_isstring(data) ){
  5363. const char *name=conv_str(st,data);
  5364. struct item_data *item_data = itemdb_searchname(name);
  5365. if( item_data )
  5366. nameid=item_data->nameid;
  5367. else
  5368. nameid=UNKNOWN_ITEM_ID;
  5369. }else
  5370. nameid=conv_num(st,data);
  5371. amount=script_getnum(st,3);
  5372. mapname =script_getstr(st,4);
  5373. x =script_getnum(st,5);
  5374. y =script_getnum(st,6);
  5375. if(strcmp(mapname,"this")==0)
  5376. {
  5377. TBL_PC *sd;
  5378. sd = script_rid2sd(st);
  5379. if (!sd) return 0; //Failed...
  5380. m=sd->bl.m;
  5381. } else
  5382. m=map_mapname2mapid(mapname);
  5383. if(nameid<0) { // ランダム
  5384. nameid=itemdb_searchrandomid(-nameid);
  5385. flag = 1;
  5386. }
  5387. if(nameid > 0) {
  5388. memset(&item_tmp,0,sizeof(item_tmp));
  5389. item_tmp.nameid=nameid;
  5390. if(!flag)
  5391. item_tmp.identify=1;
  5392. else
  5393. item_tmp.identify=itemdb_isidentified(nameid);
  5394. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5395. }
  5396. return 0;
  5397. }
  5398. /// Counts / deletes the current item given by idx.
  5399. /// Used by buildin_delitem_search
  5400. /// Relies on all input data being already fully valid.
  5401. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5402. {
  5403. int delamount;
  5404. struct item* inv = &sd->status.inventory[idx];
  5405. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5406. if( delete_items )
  5407. {
  5408. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5409. {// delete associated pet
  5410. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5411. }
  5412. //Logs items, got from (N)PC scripts [Lupus]
  5413. if( log_config.enable_logs&0x40 )
  5414. {
  5415. log_pick_pc(sd, "N", inv->nameid, -delamount, inv);
  5416. }
  5417. //Logs
  5418. pc_delitem(sd, idx, delamount, 0, 0);
  5419. }
  5420. amount[0]-= delamount;
  5421. }
  5422. /// Searches for item(s) and checks, if there is enough of them.
  5423. /// Used by delitem and delitem2
  5424. /// Relies on all input data being already fully valid.
  5425. /// @param exact_match will also match item attributes and cards, not just name id
  5426. /// @return true when all items could be deleted, false when there were not enough items to delete
  5427. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5428. {
  5429. bool delete_items = false;
  5430. int i, amount, important;
  5431. struct item* inv;
  5432. // prefer always non-equipped items
  5433. it->equip = 0;
  5434. // when searching for nameid only, prefer additionally
  5435. if( !exact_match )
  5436. {
  5437. // non-refined items
  5438. it->refine = 0;
  5439. // card-less items
  5440. memset(it->card, 0, sizeof(it->card));
  5441. }
  5442. for(;;)
  5443. {
  5444. amount = it->amount;
  5445. important = 0;
  5446. // 1st pass -- less important items / exact match
  5447. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5448. {
  5449. inv = &sd->status.inventory[i];
  5450. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5451. {// wrong/invalid item
  5452. continue;
  5453. }
  5454. if( inv->equip != it->equip || inv->refine != it->refine )
  5455. {// not matching attributes
  5456. important++;
  5457. continue;
  5458. }
  5459. if( exact_match )
  5460. {
  5461. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5462. {// not matching exact attributes
  5463. continue;
  5464. }
  5465. }
  5466. else
  5467. {
  5468. if( sd->inventory_data[i]->type == IT_PETEGG )
  5469. {
  5470. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5471. {// pet which cannot be deleted
  5472. continue;
  5473. }
  5474. }
  5475. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  5476. {// named/carded item
  5477. important++;
  5478. continue;
  5479. }
  5480. }
  5481. // count / delete item
  5482. buildin_delitem_delete(sd, i, &amount, delete_items);
  5483. }
  5484. // 2nd pass -- any matching item
  5485. if( amount == 0 || important == 0 )
  5486. {// either everything was already consumed or no items were skipped
  5487. ;
  5488. }
  5489. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5490. {
  5491. inv = &sd->status.inventory[i];
  5492. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5493. {// wrong/invalid item
  5494. continue;
  5495. }
  5496. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  5497. {// pet which cannot be deleted
  5498. continue;
  5499. }
  5500. if( exact_match )
  5501. {
  5502. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5503. {// not matching attributes
  5504. continue;
  5505. }
  5506. }
  5507. // count / delete item
  5508. buildin_delitem_delete(sd, i, &amount, delete_items);
  5509. }
  5510. if( amount )
  5511. {// not enough items
  5512. return false;
  5513. }
  5514. else if( delete_items )
  5515. {// we are done with the work
  5516. return true;
  5517. }
  5518. else
  5519. {// get rid of the items now
  5520. delete_items = true;
  5521. }
  5522. }
  5523. }
  5524. /// Deletes items from the target/attached player.
  5525. /// Prioritizes ordinary items.
  5526. ///
  5527. /// delitem <item id>,<amount>{,<account id>}
  5528. /// delitem "<item name>",<amount>{,<account id>}
  5529. BUILDIN_FUNC(delitem)
  5530. {
  5531. TBL_PC *sd;
  5532. struct item it;
  5533. struct script_data *data;
  5534. if( script_hasdata(st,4) )
  5535. {
  5536. int account_id = script_getnum(st,4);
  5537. sd = map_id2sd(account_id); // <account id>
  5538. if( sd == NULL )
  5539. {
  5540. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5541. st->state = END;
  5542. return 1;
  5543. }
  5544. }
  5545. else
  5546. {
  5547. sd = script_rid2sd(st);// attached player
  5548. if( sd == NULL )
  5549. return 0;
  5550. }
  5551. data = script_getdata(st,2);
  5552. get_val(st,data);
  5553. if( data_isstring(data) )
  5554. {
  5555. const char* item_name = conv_str(st,data);
  5556. struct item_data* id = itemdb_searchname(item_name);
  5557. if( id == NULL )
  5558. {
  5559. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5560. st->state = END;
  5561. return 1;
  5562. }
  5563. it.nameid = id->nameid;// "<item name>"
  5564. }
  5565. else
  5566. {
  5567. it.nameid = conv_num(st,data);// <item id>
  5568. if( !itemdb_exists( it.nameid ) )
  5569. {
  5570. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5571. st->state = END;
  5572. return 1;
  5573. }
  5574. }
  5575. it.amount=script_getnum(st,3);
  5576. if( it.amount <= 0 )
  5577. return 0;// nothing to do
  5578. if( buildin_delitem_search(sd, &it, false) )
  5579. {// success
  5580. return 0;
  5581. }
  5582. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5583. st->state = END;
  5584. clif_scriptclose(sd, st->oid);
  5585. return 1;
  5586. }
  5587. /// Deletes items from the target/attached player.
  5588. ///
  5589. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5590. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5591. BUILDIN_FUNC(delitem2)
  5592. {
  5593. TBL_PC *sd;
  5594. struct item it;
  5595. struct script_data *data;
  5596. if( script_hasdata(st,11) )
  5597. {
  5598. int account_id = script_getnum(st,11);
  5599. sd = map_id2sd(account_id); // <account id>
  5600. if( sd == NULL )
  5601. {
  5602. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5603. st->state = END;
  5604. return 1;
  5605. }
  5606. }
  5607. else
  5608. {
  5609. sd = script_rid2sd(st);// attached player
  5610. if( sd == NULL )
  5611. return 0;
  5612. }
  5613. data = script_getdata(st,2);
  5614. get_val(st,data);
  5615. if( data_isstring(data) )
  5616. {
  5617. const char* item_name = conv_str(st,data);
  5618. struct item_data* id = itemdb_searchname(item_name);
  5619. if( id == NULL )
  5620. {
  5621. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5622. st->state = END;
  5623. return 1;
  5624. }
  5625. it.nameid = id->nameid;// "<item name>"
  5626. }
  5627. else
  5628. {
  5629. it.nameid = conv_num(st,data);// <item id>
  5630. if( !itemdb_exists( it.nameid ) )
  5631. {
  5632. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5633. st->state = END;
  5634. return 1;
  5635. }
  5636. }
  5637. it.amount=script_getnum(st,3);
  5638. it.identify=script_getnum(st,4);
  5639. it.refine=script_getnum(st,5);
  5640. it.attribute=script_getnum(st,6);
  5641. it.card[0]=(short)script_getnum(st,7);
  5642. it.card[1]=(short)script_getnum(st,8);
  5643. it.card[2]=(short)script_getnum(st,9);
  5644. it.card[3]=(short)script_getnum(st,10);
  5645. if( it.amount <= 0 )
  5646. return 0;// nothing to do
  5647. if( buildin_delitem_search(sd, &it, true) )
  5648. {// success
  5649. return 0;
  5650. }
  5651. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5652. st->state = END;
  5653. clif_scriptclose(sd, st->oid);
  5654. return 1;
  5655. }
  5656. /*==========================================
  5657. * Enables/Disables use of items while in an NPC [Skotlex]
  5658. *------------------------------------------*/
  5659. BUILDIN_FUNC(enableitemuse)
  5660. {
  5661. TBL_PC *sd;
  5662. sd=script_rid2sd(st);
  5663. if (sd)
  5664. sd->npc_item_flag = st->oid;
  5665. return 0;
  5666. }
  5667. BUILDIN_FUNC(disableitemuse)
  5668. {
  5669. TBL_PC *sd;
  5670. sd=script_rid2sd(st);
  5671. if (sd)
  5672. sd->npc_item_flag = 0;
  5673. return 0;
  5674. }
  5675. /*==========================================
  5676. *キャラ関係のパラメータ取得
  5677. *------------------------------------------*/
  5678. BUILDIN_FUNC(readparam)
  5679. {
  5680. int type;
  5681. TBL_PC *sd;
  5682. type=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,-1);
  5689. return 0;
  5690. }
  5691. script_pushint(st,pc_readparam(sd,type));
  5692. return 0;
  5693. }
  5694. /*==========================================
  5695. *キャラ関係のID取得
  5696. *------------------------------------------*/
  5697. BUILDIN_FUNC(getcharid)
  5698. {
  5699. int num;
  5700. TBL_PC *sd;
  5701. num = script_getnum(st,2);
  5702. if( script_hasdata(st,3) )
  5703. sd=map_nick2sd(script_getstr(st,3));
  5704. else
  5705. sd=script_rid2sd(st);
  5706. if(sd==NULL){
  5707. script_pushint(st,0); //return 0, according docs
  5708. return 0;
  5709. }
  5710. switch( num ) {
  5711. case 0: script_pushint(st,sd->status.char_id); break;
  5712. case 1: script_pushint(st,sd->status.party_id); break;
  5713. case 2: script_pushint(st,sd->status.guild_id); break;
  5714. case 3: script_pushint(st,sd->status.account_id); break;
  5715. case 4: script_pushint(st,sd->bg_id); break;
  5716. default:
  5717. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5718. script_pushint(st,0);
  5719. break;
  5720. }
  5721. return 0;
  5722. }
  5723. /*==========================================
  5724. *指定IDのPT名取得
  5725. *------------------------------------------*/
  5726. BUILDIN_FUNC(getpartyname)
  5727. {
  5728. int party_id;
  5729. struct party_data* p;
  5730. party_id = script_getnum(st,2);
  5731. if( ( p = party_search(party_id) ) != NULL )
  5732. {
  5733. script_pushstrcopy(st,p->party.name);
  5734. }
  5735. else
  5736. {
  5737. script_pushconststr(st,"null");
  5738. }
  5739. return 0;
  5740. }
  5741. /*==========================================
  5742. *指定IDのPT人数とメンバーID取得
  5743. *------------------------------------------*/
  5744. BUILDIN_FUNC(getpartymember)
  5745. {
  5746. struct party_data *p;
  5747. int i,j=0,type=0;
  5748. p=party_search(script_getnum(st,2));
  5749. if( script_hasdata(st,3) )
  5750. type=script_getnum(st,3);
  5751. if(p!=NULL){
  5752. for(i=0;i<MAX_PARTY;i++){
  5753. if(p->party.member[i].account_id){
  5754. switch (type) {
  5755. case 2:
  5756. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  5757. break;
  5758. case 1:
  5759. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  5760. break;
  5761. default:
  5762. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  5763. }
  5764. j++;
  5765. }
  5766. }
  5767. }
  5768. mapreg_setreg(add_str("$@partymembercount"),j);
  5769. return 0;
  5770. }
  5771. /*==========================================
  5772. * Retrieves party leader. if flag is specified,
  5773. * return some of the leader data. Otherwise, return name.
  5774. *------------------------------------------*/
  5775. BUILDIN_FUNC(getpartyleader)
  5776. {
  5777. int party_id, type = 0, i=0;
  5778. struct party_data *p;
  5779. party_id=script_getnum(st,2);
  5780. if( script_hasdata(st,3) )
  5781. type=script_getnum(st,3);
  5782. p=party_search(party_id);
  5783. if (p) //Search leader
  5784. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5785. if (!p || i == MAX_PARTY) { //leader not found
  5786. if (type)
  5787. script_pushint(st,-1);
  5788. else
  5789. script_pushconststr(st,"null");
  5790. return 0;
  5791. }
  5792. switch (type) {
  5793. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5794. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5795. case 3: script_pushint(st,p->party.member[i].class_); break;
  5796. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5797. case 5: script_pushint(st,p->party.member[i].lv); break;
  5798. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5799. }
  5800. return 0;
  5801. }
  5802. /*==========================================
  5803. *指定IDのギルド名取得
  5804. *------------------------------------------*/
  5805. BUILDIN_FUNC(getguildname)
  5806. {
  5807. int guild_id;
  5808. struct guild* g;
  5809. guild_id = script_getnum(st,2);
  5810. if( ( g = guild_search(guild_id) ) != NULL )
  5811. {
  5812. script_pushstrcopy(st,g->name);
  5813. }
  5814. else
  5815. {
  5816. script_pushconststr(st,"null");
  5817. }
  5818. return 0;
  5819. }
  5820. /*==========================================
  5821. *指定IDのGuildMaster名取得
  5822. *------------------------------------------*/
  5823. BUILDIN_FUNC(getguildmaster)
  5824. {
  5825. int guild_id;
  5826. struct guild* g;
  5827. guild_id = script_getnum(st,2);
  5828. if( ( g = guild_search(guild_id) ) != NULL )
  5829. {
  5830. script_pushstrcopy(st,g->member[0].name);
  5831. }
  5832. else
  5833. {
  5834. script_pushconststr(st,"null");
  5835. }
  5836. return 0;
  5837. }
  5838. BUILDIN_FUNC(getguildmasterid)
  5839. {
  5840. int guild_id;
  5841. struct guild* g;
  5842. guild_id = script_getnum(st,2);
  5843. if( ( g = guild_search(guild_id) ) != NULL )
  5844. {
  5845. script_pushint(st,g->member[0].char_id);
  5846. }
  5847. else
  5848. {
  5849. script_pushint(st,0);
  5850. }
  5851. return 0;
  5852. }
  5853. /*==========================================
  5854. * キャラクタの名前
  5855. *------------------------------------------*/
  5856. BUILDIN_FUNC(strcharinfo)
  5857. {
  5858. TBL_PC *sd;
  5859. int num;
  5860. struct guild* g;
  5861. struct party_data* p;
  5862. sd=script_rid2sd(st);
  5863. if (!sd) { //Avoid crashing....
  5864. script_pushconststr(st,"");
  5865. return 0;
  5866. }
  5867. num=script_getnum(st,2);
  5868. switch(num){
  5869. case 0:
  5870. script_pushstrcopy(st,sd->status.name);
  5871. break;
  5872. case 1:
  5873. if( ( p = party_search(sd->status.party_id) ) != NULL )
  5874. {
  5875. script_pushstrcopy(st,p->party.name);
  5876. }
  5877. else
  5878. {
  5879. script_pushconststr(st,"");
  5880. }
  5881. break;
  5882. case 2:
  5883. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  5884. {
  5885. script_pushstrcopy(st,g->name);
  5886. }
  5887. else
  5888. {
  5889. script_pushconststr(st,"");
  5890. }
  5891. break;
  5892. case 3:
  5893. script_pushconststr(st,map[sd->bl.m].name);
  5894. break;
  5895. default:
  5896. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5897. script_pushconststr(st,"");
  5898. break;
  5899. }
  5900. return 0;
  5901. }
  5902. /*==========================================
  5903. * 呼び出し元のNPC情報を取得する
  5904. *------------------------------------------*/
  5905. BUILDIN_FUNC(strnpcinfo)
  5906. {
  5907. TBL_NPC* nd;
  5908. int num;
  5909. char *buf,*name=NULL;
  5910. nd = map_id2nd(st->oid);
  5911. if (!nd) {
  5912. script_pushconststr(st, "");
  5913. return 0;
  5914. }
  5915. num = script_getnum(st,2);
  5916. switch(num){
  5917. case 0: // display name
  5918. name = aStrdup(nd->name);
  5919. break;
  5920. case 1: // visible part of display name
  5921. if((buf = strchr(nd->name,'#')) != NULL)
  5922. {
  5923. name = aStrdup(nd->name);
  5924. name[buf - nd->name] = 0;
  5925. } else // Return the name, there is no '#' present
  5926. name = aStrdup(nd->name);
  5927. break;
  5928. case 2: // # fragment
  5929. if((buf = strchr(nd->name,'#')) != NULL)
  5930. name = aStrdup(buf+1);
  5931. break;
  5932. case 3: // unique name
  5933. name = aStrdup(nd->exname);
  5934. break;
  5935. case 4: // map name
  5936. name = aStrdup(map[nd->bl.m].name);
  5937. break;
  5938. }
  5939. if(name)
  5940. script_pushstr(st, name);
  5941. else
  5942. script_pushconststr(st, "");
  5943. return 0;
  5944. }
  5945. // aegis->athena slot position conversion table
  5946. static unsigned int equip[] = {EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_GARMENT,EQP_SHOES,EQP_ACC_L,EQP_ACC_R,EQP_HEAD_MID,EQP_HEAD_LOW};
  5947. /*==========================================
  5948. * GetEquipID(Pos); Pos: 1-10
  5949. *------------------------------------------*/
  5950. BUILDIN_FUNC(getequipid)
  5951. {
  5952. int i, num;
  5953. TBL_PC* sd;
  5954. struct item_data* item;
  5955. sd = script_rid2sd(st);
  5956. if( sd == NULL )
  5957. return 0;
  5958. num = script_getnum(st,2) - 1;
  5959. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5960. {
  5961. script_pushint(st,-1);
  5962. return 0;
  5963. }
  5964. // get inventory position of item
  5965. i = pc_checkequip(sd,equip[num]);
  5966. if( i < 0 )
  5967. {
  5968. script_pushint(st,-1);
  5969. return 0;
  5970. }
  5971. item = sd->inventory_data[i];
  5972. if( item != 0 )
  5973. script_pushint(st,item->nameid);
  5974. else
  5975. script_pushint(st,0);
  5976. return 0;
  5977. }
  5978. /*==========================================
  5979. * 装備名文字列(精錬メニュー用)
  5980. *------------------------------------------*/
  5981. BUILDIN_FUNC(getequipname)
  5982. {
  5983. int i, num;
  5984. TBL_PC* sd;
  5985. struct item_data* item;
  5986. sd = script_rid2sd(st);
  5987. if( sd == NULL )
  5988. return 0;
  5989. num = script_getnum(st,2) - 1;
  5990. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5991. {
  5992. script_pushconststr(st,"");
  5993. return 0;
  5994. }
  5995. // get inventory position of item
  5996. i = pc_checkequip(sd,equip[num]);
  5997. if( i < 0 )
  5998. {
  5999. script_pushint(st,-1);
  6000. return 0;
  6001. }
  6002. item = sd->inventory_data[i];
  6003. if( item != 0 )
  6004. script_pushstrcopy(st,item->jname);
  6005. else
  6006. script_pushconststr(st,"");
  6007. return 0;
  6008. }
  6009. /*==========================================
  6010. * getbrokenid [Valaris]
  6011. *------------------------------------------*/
  6012. BUILDIN_FUNC(getbrokenid)
  6013. {
  6014. int i,num,id=0,brokencounter=0;
  6015. TBL_PC *sd;
  6016. sd = script_rid2sd(st);
  6017. if( sd == NULL )
  6018. return 0;
  6019. num=script_getnum(st,2);
  6020. for(i=0; i<MAX_INVENTORY; i++) {
  6021. if(sd->status.inventory[i].attribute){
  6022. brokencounter++;
  6023. if(num==brokencounter){
  6024. id=sd->status.inventory[i].nameid;
  6025. break;
  6026. }
  6027. }
  6028. }
  6029. script_pushint(st,id);
  6030. return 0;
  6031. }
  6032. /*==========================================
  6033. * repair [Valaris]
  6034. *------------------------------------------*/
  6035. BUILDIN_FUNC(repair)
  6036. {
  6037. int i,num;
  6038. int repaircounter=0;
  6039. TBL_PC *sd;
  6040. sd = script_rid2sd(st);
  6041. if( sd == NULL )
  6042. return 0;
  6043. num=script_getnum(st,2);
  6044. for(i=0; i<MAX_INVENTORY; i++) {
  6045. if(sd->status.inventory[i].attribute){
  6046. repaircounter++;
  6047. if(num==repaircounter){
  6048. sd->status.inventory[i].attribute=0;
  6049. clif_equiplist(sd);
  6050. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6051. clif_misceffect(&sd->bl, 3);
  6052. break;
  6053. }
  6054. }
  6055. }
  6056. return 0;
  6057. }
  6058. /*==========================================
  6059. * 装備チェック
  6060. *------------------------------------------*/
  6061. BUILDIN_FUNC(getequipisequiped)
  6062. {
  6063. int i=-1,num;
  6064. TBL_PC *sd;
  6065. num=script_getnum(st,2);
  6066. sd = script_rid2sd(st);
  6067. if( sd == NULL )
  6068. return 0;
  6069. if (num > 0 && num <= ARRAYLENGTH(equip))
  6070. i=pc_checkequip(sd,equip[num-1]);
  6071. if(i >= 0)
  6072. script_pushint(st,1);
  6073. else
  6074. script_pushint(st,0);
  6075. return 0;
  6076. }
  6077. /*==========================================
  6078. * 装備品精錬可能チェック
  6079. *------------------------------------------*/
  6080. BUILDIN_FUNC(getequipisenableref)
  6081. {
  6082. int i=-1,num;
  6083. TBL_PC *sd;
  6084. num=script_getnum(st,2);
  6085. sd = script_rid2sd(st);
  6086. if( sd == NULL )
  6087. return 0;
  6088. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6089. i = pc_checkequip(sd,equip[num-1]);
  6090. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6091. script_pushint(st,1);
  6092. else
  6093. script_pushint(st,0);
  6094. return 0;
  6095. }
  6096. /*==========================================
  6097. * 装備品鑑定チェック
  6098. *------------------------------------------*/
  6099. BUILDIN_FUNC(getequipisidentify)
  6100. {
  6101. int i=-1,num;
  6102. TBL_PC *sd;
  6103. num=script_getnum(st,2);
  6104. sd = script_rid2sd(st);
  6105. if( sd == NULL )
  6106. return 0;
  6107. if (num > 0 && num <= ARRAYLENGTH(equip))
  6108. i=pc_checkequip(sd,equip[num-1]);
  6109. if(i >= 0)
  6110. script_pushint(st,sd->status.inventory[i].identify);
  6111. else
  6112. script_pushint(st,0);
  6113. return 0;
  6114. }
  6115. /*==========================================
  6116. * 装備品精錬度
  6117. *------------------------------------------*/
  6118. BUILDIN_FUNC(getequiprefinerycnt)
  6119. {
  6120. int i=-1,num;
  6121. TBL_PC *sd;
  6122. num=script_getnum(st,2);
  6123. sd = script_rid2sd(st);
  6124. if( sd == NULL )
  6125. return 0;
  6126. if (num > 0 && num <= ARRAYLENGTH(equip))
  6127. i=pc_checkequip(sd,equip[num-1]);
  6128. if(i >= 0)
  6129. script_pushint(st,sd->status.inventory[i].refine);
  6130. else
  6131. script_pushint(st,0);
  6132. return 0;
  6133. }
  6134. /*==========================================
  6135. * 装備品武器LV
  6136. *------------------------------------------*/
  6137. BUILDIN_FUNC(getequipweaponlv)
  6138. {
  6139. int i=-1,num;
  6140. TBL_PC *sd;
  6141. num=script_getnum(st,2);
  6142. sd = script_rid2sd(st);
  6143. if( sd == NULL )
  6144. return 0;
  6145. if (num > 0 && num <= ARRAYLENGTH(equip))
  6146. i=pc_checkequip(sd,equip[num-1]);
  6147. if(i >= 0 && sd->inventory_data[i])
  6148. script_pushint(st,sd->inventory_data[i]->wlv);
  6149. else
  6150. script_pushint(st,0);
  6151. return 0;
  6152. }
  6153. /*==========================================
  6154. * 装備品精錬成功率
  6155. *------------------------------------------*/
  6156. BUILDIN_FUNC(getequippercentrefinery)
  6157. {
  6158. int i=-1,num;
  6159. TBL_PC *sd;
  6160. num=script_getnum(st,2);
  6161. sd = script_rid2sd(st);
  6162. if( sd == NULL )
  6163. return 0;
  6164. if (num > 0 && num <= ARRAYLENGTH(equip))
  6165. i=pc_checkequip(sd,equip[num-1]);
  6166. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6167. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  6168. else
  6169. script_pushint(st,0);
  6170. return 0;
  6171. }
  6172. /*==========================================
  6173. * 精錬成功
  6174. *------------------------------------------*/
  6175. BUILDIN_FUNC(successrefitem)
  6176. {
  6177. int i=-1,num,ep;
  6178. TBL_PC *sd;
  6179. num=script_getnum(st,2);
  6180. sd = script_rid2sd(st);
  6181. if( sd == NULL )
  6182. return 0;
  6183. if (num > 0 && num <= ARRAYLENGTH(equip))
  6184. i=pc_checkequip(sd,equip[num-1]);
  6185. if(i >= 0) {
  6186. ep=sd->status.inventory[i].equip;
  6187. //Logs items, got from (N)PC scripts [Lupus]
  6188. if(log_config.enable_logs&0x40)
  6189. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6190. sd->status.inventory[i].refine++;
  6191. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6192. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6193. clif_delitem(sd,i,1,3);
  6194. //Logs items, got from (N)PC scripts [Lupus]
  6195. if(log_config.enable_logs&0x40)
  6196. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  6197. clif_additem(sd,i,1,0);
  6198. pc_equipitem(sd,i,ep);
  6199. clif_misceffect(&sd->bl,3);
  6200. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6201. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6202. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6203. ){ // Fame point system [DracoRPG]
  6204. switch (sd->inventory_data[i]->wlv){
  6205. case 1:
  6206. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6207. break;
  6208. case 2:
  6209. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6210. break;
  6211. case 3:
  6212. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6213. break;
  6214. }
  6215. }
  6216. }
  6217. return 0;
  6218. }
  6219. /*==========================================
  6220. * 精錬失敗
  6221. *------------------------------------------*/
  6222. BUILDIN_FUNC(failedrefitem)
  6223. {
  6224. int i=-1,num;
  6225. TBL_PC *sd;
  6226. num=script_getnum(st,2);
  6227. sd = script_rid2sd(st);
  6228. if( sd == NULL )
  6229. return 0;
  6230. if (num > 0 && num <= ARRAYLENGTH(equip))
  6231. i=pc_checkequip(sd,equip[num-1]);
  6232. if(i >= 0) {
  6233. //Logs items, got from (N)PC scripts [Lupus]
  6234. if(log_config.enable_logs&0x40)
  6235. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6236. sd->status.inventory[i].refine = 0;
  6237. pc_unequipitem(sd,i,3);
  6238. // 精錬失敗エフェクトのパケット
  6239. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6240. pc_delitem(sd,i,1,0,2);
  6241. // 他の人にも失敗を通知
  6242. clif_misceffect(&sd->bl,2);
  6243. }
  6244. return 0;
  6245. }
  6246. /*==========================================
  6247. *
  6248. *------------------------------------------*/
  6249. BUILDIN_FUNC(statusup)
  6250. {
  6251. int type;
  6252. TBL_PC *sd;
  6253. type=script_getnum(st,2);
  6254. sd = script_rid2sd(st);
  6255. if( sd == NULL )
  6256. return 0;
  6257. pc_statusup(sd,type);
  6258. return 0;
  6259. }
  6260. /*==========================================
  6261. *
  6262. *------------------------------------------*/
  6263. BUILDIN_FUNC(statusup2)
  6264. {
  6265. int type,val;
  6266. TBL_PC *sd;
  6267. type=script_getnum(st,2);
  6268. val=script_getnum(st,3);
  6269. sd = script_rid2sd(st);
  6270. if( sd == NULL )
  6271. return 0;
  6272. pc_statusup2(sd,type,val);
  6273. return 0;
  6274. }
  6275. /// See 'doc/item_bonus.txt'
  6276. ///
  6277. /// bonus <bonus type>,<val1>;
  6278. /// bonus2 <bonus type>,<val1>,<val2>;
  6279. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6280. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6281. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6282. BUILDIN_FUNC(bonus)
  6283. {
  6284. int type;
  6285. int val1;
  6286. int val2 = 0;
  6287. int val3 = 0;
  6288. int val4 = 0;
  6289. int val5 = 0;
  6290. TBL_PC* sd;
  6291. sd = script_rid2sd(st);
  6292. if( sd == NULL )
  6293. return 0; // no player attached
  6294. type = script_getnum(st,2);
  6295. switch( type )
  6296. {
  6297. case SP_AUTOSPELL:
  6298. case SP_AUTOSPELL_WHENHIT:
  6299. case SP_AUTOSPELL_ONSKILL:
  6300. case SP_SKILL_ATK:
  6301. case SP_SKILL_HEAL:
  6302. case SP_SKILL_HEAL2:
  6303. case SP_ADD_SKILL_BLOW:
  6304. case SP_CASTRATE:
  6305. case SP_ADDEFF_ONSKILL:
  6306. // these bonuses support skill names
  6307. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6308. break;
  6309. default:
  6310. val1 = script_getnum(st,3);
  6311. break;
  6312. }
  6313. switch( script_lastdata(st)-2 )
  6314. {
  6315. case 1:
  6316. pc_bonus(sd, type, val1);
  6317. break;
  6318. case 2:
  6319. val2 = script_getnum(st,4);
  6320. pc_bonus2(sd, type, val1, val2);
  6321. break;
  6322. case 3:
  6323. val2 = script_getnum(st,4);
  6324. val3 = script_getnum(st,5);
  6325. pc_bonus3(sd, type, val1, val2, val3);
  6326. break;
  6327. case 4:
  6328. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6329. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6330. else
  6331. val2 = script_getnum(st,4);
  6332. val3 = script_getnum(st,5);
  6333. val4 = script_getnum(st,6);
  6334. pc_bonus4(sd, type, val1, val2, val3, val4);
  6335. break;
  6336. case 5:
  6337. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6338. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6339. else
  6340. val2 = script_getnum(st,4);
  6341. val3 = script_getnum(st,5);
  6342. val4 = script_getnum(st,6);
  6343. val5 = script_getnum(st,7);
  6344. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6345. break;
  6346. default:
  6347. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6348. break;
  6349. }
  6350. return 0;
  6351. }
  6352. BUILDIN_FUNC(autobonus)
  6353. {
  6354. unsigned int dur;
  6355. short rate;
  6356. short atk_type = 0;
  6357. TBL_PC* sd;
  6358. const char *bonus_script, *other_script = NULL;
  6359. sd = script_rid2sd(st);
  6360. if( sd == NULL )
  6361. return 0; // no player attached
  6362. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6363. return 0;
  6364. rate = script_getnum(st,3);
  6365. dur = script_getnum(st,4);
  6366. bonus_script = script_getstr(st,2);
  6367. if( !rate || !dur || !bonus_script )
  6368. return 0;
  6369. if( script_hasdata(st,5) )
  6370. atk_type = script_getnum(st,5);
  6371. if( script_hasdata(st,6) )
  6372. other_script = script_getstr(st,6);
  6373. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6374. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6375. {
  6376. script_add_autobonus(bonus_script);
  6377. if( other_script )
  6378. script_add_autobonus(other_script);
  6379. }
  6380. return 0;
  6381. }
  6382. BUILDIN_FUNC(autobonus2)
  6383. {
  6384. unsigned int dur;
  6385. short rate;
  6386. short atk_type = 0;
  6387. TBL_PC* sd;
  6388. const char *bonus_script, *other_script = NULL;
  6389. sd = script_rid2sd(st);
  6390. if( sd == NULL )
  6391. return 0; // no player attached
  6392. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6393. return 0;
  6394. rate = script_getnum(st,3);
  6395. dur = script_getnum(st,4);
  6396. bonus_script = script_getstr(st,2);
  6397. if( !rate || !dur || !bonus_script )
  6398. return 0;
  6399. if( script_hasdata(st,5) )
  6400. atk_type = script_getnum(st,5);
  6401. if( script_hasdata(st,6) )
  6402. other_script = script_getstr(st,6);
  6403. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6404. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6405. {
  6406. script_add_autobonus(bonus_script);
  6407. if( other_script )
  6408. script_add_autobonus(other_script);
  6409. }
  6410. return 0;
  6411. }
  6412. BUILDIN_FUNC(autobonus3)
  6413. {
  6414. unsigned int dur;
  6415. short rate,atk_type;
  6416. TBL_PC* sd;
  6417. const char *bonus_script, *other_script = NULL;
  6418. sd = script_rid2sd(st);
  6419. if( sd == NULL )
  6420. return 0; // no player attached
  6421. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6422. return 0;
  6423. rate = script_getnum(st,3);
  6424. dur = script_getnum(st,4);
  6425. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6426. bonus_script = script_getstr(st,2);
  6427. if( !rate || !dur || !atk_type || !bonus_script )
  6428. return 0;
  6429. if( script_hasdata(st,6) )
  6430. other_script = script_getstr(st,6);
  6431. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6432. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6433. {
  6434. script_add_autobonus(bonus_script);
  6435. if( other_script )
  6436. script_add_autobonus(other_script);
  6437. }
  6438. return 0;
  6439. }
  6440. /// Changes the level of a player skill.
  6441. /// <flag> defaults to 1
  6442. /// <flag>=0 : set the level of the skill
  6443. /// <flag>=1 : set the temporary level of the skill
  6444. /// <flag>=2 : add to the level of the skill
  6445. ///
  6446. /// skill <skill id>,<level>,<flag>
  6447. /// skill <skill id>,<level>
  6448. /// skill "<skill name>",<level>,<flag>
  6449. /// skill "<skill name>",<level>
  6450. BUILDIN_FUNC(skill)
  6451. {
  6452. int id;
  6453. int level;
  6454. int flag = 1;
  6455. TBL_PC* sd;
  6456. sd = script_rid2sd(st);
  6457. if( sd == NULL )
  6458. return 0;// no player attached, report source
  6459. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6460. level = script_getnum(st,3);
  6461. if( script_hasdata(st,4) )
  6462. flag = script_getnum(st,4);
  6463. pc_skill(sd, id, level, flag);
  6464. return 0;
  6465. }
  6466. /// Changes the level of a player skill.
  6467. /// like skill, but <flag> defaults to 2
  6468. ///
  6469. /// addtoskill <skill id>,<amount>,<flag>
  6470. /// addtoskill <skill id>,<amount>
  6471. /// addtoskill "<skill name>",<amount>,<flag>
  6472. /// addtoskill "<skill name>",<amount>
  6473. ///
  6474. /// @see skill
  6475. BUILDIN_FUNC(addtoskill)
  6476. {
  6477. int id;
  6478. int level;
  6479. int flag = 2;
  6480. TBL_PC* sd;
  6481. sd = script_rid2sd(st);
  6482. if( sd == NULL )
  6483. return 0;// no player attached, report source
  6484. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6485. level = script_getnum(st,3);
  6486. if( script_hasdata(st,4) )
  6487. flag = script_getnum(st,4);
  6488. pc_skill(sd, id, level, flag);
  6489. return 0;
  6490. }
  6491. /// Increases the level of a guild skill.
  6492. ///
  6493. /// guildskill <skill id>,<amount>;
  6494. /// guildskill "<skill name>",<amount>;
  6495. BUILDIN_FUNC(guildskill)
  6496. {
  6497. int id;
  6498. int level;
  6499. TBL_PC* sd;
  6500. int i;
  6501. sd = script_rid2sd(st);
  6502. if( sd == NULL )
  6503. return 0;// no player attached, report source
  6504. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6505. level = script_getnum(st,3);
  6506. for( i=0; i < level; i++ )
  6507. guild_skillup(sd, id);
  6508. return 0;
  6509. }
  6510. /// Returns the level of the player skill.
  6511. ///
  6512. /// getskilllv(<skill id>) -> <level>
  6513. /// getskilllv("<skill name>") -> <level>
  6514. BUILDIN_FUNC(getskilllv)
  6515. {
  6516. int id;
  6517. TBL_PC* sd;
  6518. sd = script_rid2sd(st);
  6519. if( sd == NULL )
  6520. return 0;// no player attached, report source
  6521. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6522. script_pushint(st, pc_checkskill(sd,id));
  6523. return 0;
  6524. }
  6525. /// Returns the level of the guild skill.
  6526. ///
  6527. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6528. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6529. BUILDIN_FUNC(getgdskilllv)
  6530. {
  6531. int guild_id;
  6532. int skill_id;
  6533. struct guild* g;
  6534. guild_id = script_getnum(st,2);
  6535. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6536. g = guild_search(guild_id);
  6537. if( g == NULL )
  6538. script_pushint(st, -1);
  6539. else
  6540. script_pushint(st, guild_checkskill(g,skill_id));
  6541. return 0;
  6542. }
  6543. /// Returns the 'basic_skill_check' setting.
  6544. /// This config determines if the server checks the skill level of NV_BASIC
  6545. /// before allowing the basic actions.
  6546. ///
  6547. /// basicskillcheck() -> <bool>
  6548. BUILDIN_FUNC(basicskillcheck)
  6549. {
  6550. script_pushint(st, battle_config.basic_skill_check);
  6551. return 0;
  6552. }
  6553. /// Returns the GM level of the player.
  6554. ///
  6555. /// getgmlevel() -> <level>
  6556. BUILDIN_FUNC(getgmlevel)
  6557. {
  6558. TBL_PC* sd;
  6559. sd = script_rid2sd(st);
  6560. if( sd == NULL )
  6561. return 0;// no player attached, report source
  6562. script_pushint(st, pc_isGM(sd));
  6563. return 0;
  6564. }
  6565. /// Terminates the execution of this script instance.
  6566. ///
  6567. /// end
  6568. BUILDIN_FUNC(end)
  6569. {
  6570. st->state = END;
  6571. return 0;
  6572. }
  6573. /// Checks if the player has that effect state (option).
  6574. ///
  6575. /// checkoption(<option>) -> <bool>
  6576. BUILDIN_FUNC(checkoption)
  6577. {
  6578. int option;
  6579. TBL_PC* sd;
  6580. sd = script_rid2sd(st);
  6581. if( sd == NULL )
  6582. return 0;// no player attached, report source
  6583. option = script_getnum(st,2);
  6584. if( sd->sc.option&option )
  6585. script_pushint(st, 1);
  6586. else
  6587. script_pushint(st, 0);
  6588. return 0;
  6589. }
  6590. /// Checks if the player is in that body state (opt1).
  6591. ///
  6592. /// checkoption1(<opt1>) -> <bool>
  6593. BUILDIN_FUNC(checkoption1)
  6594. {
  6595. int opt1;
  6596. TBL_PC* sd;
  6597. sd = script_rid2sd(st);
  6598. if( sd == NULL )
  6599. return 0;// no player attached, report source
  6600. opt1 = script_getnum(st,2);
  6601. if( sd->sc.opt1 == opt1 )
  6602. script_pushint(st, 1);
  6603. else
  6604. script_pushint(st, 0);
  6605. return 0;
  6606. }
  6607. /// Checks if the player has that health state (opt2).
  6608. ///
  6609. /// checkoption2(<opt2>) -> <bool>
  6610. BUILDIN_FUNC(checkoption2)
  6611. {
  6612. int opt2;
  6613. TBL_PC* sd;
  6614. sd = script_rid2sd(st);
  6615. if( sd == NULL )
  6616. return 0;// no player attached, report source
  6617. opt2 = script_getnum(st,2);
  6618. if( sd->sc.opt2&opt2 )
  6619. script_pushint(st, 1);
  6620. else
  6621. script_pushint(st, 0);
  6622. return 0;
  6623. }
  6624. /// Changes the effect state (option) of the player.
  6625. /// <flag> defaults to 1
  6626. /// <flag>=0 : removes the option
  6627. /// <flag>=other : adds the option
  6628. ///
  6629. /// setoption <option>,<flag>;
  6630. /// setoption <option>;
  6631. BUILDIN_FUNC(setoption)
  6632. {
  6633. int option;
  6634. int flag = 1;
  6635. TBL_PC* sd;
  6636. sd = script_rid2sd(st);
  6637. if( sd == NULL )
  6638. return 0;// no player attached, report source
  6639. option = script_getnum(st,2);
  6640. if( script_hasdata(st,3) )
  6641. flag = script_getnum(st,3);
  6642. else if( !option ){// Request to remove everything.
  6643. flag = 0;
  6644. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6645. }
  6646. if( flag ){// Add option
  6647. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6648. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6649. pc_setoption(sd, sd->sc.option|option);
  6650. } else// Remove option
  6651. pc_setoption(sd, sd->sc.option&~option);
  6652. return 0;
  6653. }
  6654. /// Returns if the player has a cart.
  6655. ///
  6656. /// checkcart() -> <bool>
  6657. ///
  6658. /// @author Valaris
  6659. BUILDIN_FUNC(checkcart)
  6660. {
  6661. TBL_PC* sd;
  6662. sd = script_rid2sd(st);
  6663. if( sd == NULL )
  6664. return 0;// no player attached, report source
  6665. if( pc_iscarton(sd) )
  6666. script_pushint(st, 1);
  6667. else
  6668. script_pushint(st, 0);
  6669. return 0;
  6670. }
  6671. /// Sets the cart of the player.
  6672. /// <type> defaults to 1
  6673. /// <type>=0 : removes the cart
  6674. /// <type>=1 : Normal cart
  6675. /// <type>=2 : Wooden cart
  6676. /// <type>=3 : Covered cart with flowers and ferns
  6677. /// <type>=4 : Wooden cart with a Panda doll on the back
  6678. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6679. ///
  6680. /// setcart <type>;
  6681. /// setcart;
  6682. BUILDIN_FUNC(setcart)
  6683. {
  6684. int type = 1;
  6685. TBL_PC* sd;
  6686. sd = script_rid2sd(st);
  6687. if( sd == NULL )
  6688. return 0;// no player attached, report source
  6689. if( script_hasdata(st,2) )
  6690. type = script_getnum(st,2);
  6691. pc_setcart(sd, type);
  6692. return 0;
  6693. }
  6694. /// Returns if the player has a falcon.
  6695. ///
  6696. /// checkfalcon() -> <bool>
  6697. ///
  6698. /// @author Valaris
  6699. BUILDIN_FUNC(checkfalcon)
  6700. {
  6701. TBL_PC* sd;
  6702. sd = script_rid2sd(st);
  6703. if( sd == NULL )
  6704. return 0;// no player attached, report source
  6705. if( pc_isfalcon(sd) )
  6706. script_pushint(st, 1);
  6707. else
  6708. script_pushint(st, 0);
  6709. return 0;
  6710. }
  6711. /// Sets if the player has a falcon or not.
  6712. /// <flag> defaults to 1
  6713. ///
  6714. /// setfalcon <flag>;
  6715. /// setfalcon;
  6716. BUILDIN_FUNC(setfalcon)
  6717. {
  6718. int flag = 1;
  6719. TBL_PC* sd;
  6720. sd = script_rid2sd(st);
  6721. if( sd == NULL )
  6722. return 0;// no player attached, report source
  6723. if( script_hasdata(st,2) )
  6724. flag = script_getnum(st,2);
  6725. pc_setfalcon(sd, flag);
  6726. return 0;
  6727. }
  6728. /// Returns if the player is riding.
  6729. ///
  6730. /// checkriding() -> <bool>
  6731. ///
  6732. /// @author Valaris
  6733. BUILDIN_FUNC(checkriding)
  6734. {
  6735. TBL_PC* sd;
  6736. sd = script_rid2sd(st);
  6737. if( sd == NULL )
  6738. return 0;// no player attached, report source
  6739. if( pc_isriding(sd) || sd->sc.option&OPTION_MOUNTING )
  6740. script_pushint(st, 1);
  6741. else
  6742. script_pushint(st, 0);
  6743. return 0;
  6744. }
  6745. /// Sets if the player is riding.
  6746. /// <flag> defaults to 1
  6747. ///
  6748. /// setriding <flag>;
  6749. /// setriding;
  6750. BUILDIN_FUNC(setriding)
  6751. {
  6752. int flag = 1;
  6753. TBL_PC* sd;
  6754. sd = script_rid2sd(st);
  6755. if( sd == NULL )
  6756. return 0;// no player attached, report source
  6757. if( script_hasdata(st,2) )
  6758. flag = script_getnum(st,2);
  6759. pc_setriding(sd, flag);
  6760. return 0;
  6761. }
  6762. /// Sets the save point of the player.
  6763. ///
  6764. /// save "<map name>",<x>,<y>
  6765. /// savepoint "<map name>",<x>,<y>
  6766. BUILDIN_FUNC(savepoint)
  6767. {
  6768. int x;
  6769. int y;
  6770. short map;
  6771. const char* str;
  6772. TBL_PC* sd;
  6773. sd = script_rid2sd(st);
  6774. if( sd == NULL )
  6775. return 0;// no player attached, report source
  6776. str = script_getstr(st, 2);
  6777. x = script_getnum(st,3);
  6778. y = script_getnum(st,4);
  6779. map = mapindex_name2id(str);
  6780. if( map )
  6781. pc_setsavepoint(sd, map, x, y);
  6782. return 0;
  6783. }
  6784. /*==========================================
  6785. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6786. *------------------------------------------*/
  6787. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6788. {
  6789. int type;
  6790. time_t timer;
  6791. struct tm *t;
  6792. type=script_getnum(st,2);
  6793. switch(type){
  6794. case 2:
  6795. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6796. // from the system clock.)
  6797. script_pushint(st,(int)time(NULL));
  6798. break;
  6799. case 1:
  6800. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6801. time(&timer);
  6802. t=localtime(&timer);
  6803. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6804. break;
  6805. case 0:
  6806. default:
  6807. //type 0:(System Ticks)
  6808. script_pushint(st,gettick());
  6809. break;
  6810. }
  6811. return 0;
  6812. }
  6813. /*==========================================
  6814. * GetTime(Type);
  6815. * 1: Sec 2: Min 3: Hour
  6816. * 4: WeekDay 5: MonthDay 6: Month
  6817. * 7: Year
  6818. *------------------------------------------*/
  6819. BUILDIN_FUNC(gettime) /* Asgard Version */
  6820. {
  6821. int type;
  6822. time_t timer;
  6823. struct tm *t;
  6824. type=script_getnum(st,2);
  6825. time(&timer);
  6826. t=localtime(&timer);
  6827. switch(type){
  6828. case 1://Sec(0~59)
  6829. script_pushint(st,t->tm_sec);
  6830. break;
  6831. case 2://Min(0~59)
  6832. script_pushint(st,t->tm_min);
  6833. break;
  6834. case 3://Hour(0~23)
  6835. script_pushint(st,t->tm_hour);
  6836. break;
  6837. case 4://WeekDay(0~6)
  6838. script_pushint(st,t->tm_wday);
  6839. break;
  6840. case 5://MonthDay(01~31)
  6841. script_pushint(st,t->tm_mday);
  6842. break;
  6843. case 6://Month(01~12)
  6844. script_pushint(st,t->tm_mon+1);
  6845. break;
  6846. case 7://Year(20xx)
  6847. script_pushint(st,t->tm_year+1900);
  6848. break;
  6849. case 8://Year Day(01~366)
  6850. script_pushint(st,t->tm_yday+1);
  6851. break;
  6852. default://(format error)
  6853. script_pushint(st,-1);
  6854. break;
  6855. }
  6856. return 0;
  6857. }
  6858. /*==========================================
  6859. * GetTimeStr("TimeFMT", Length);
  6860. *------------------------------------------*/
  6861. BUILDIN_FUNC(gettimestr)
  6862. {
  6863. char *tmpstr;
  6864. const char *fmtstr;
  6865. int maxlen;
  6866. time_t now = time(NULL);
  6867. fmtstr=script_getstr(st,2);
  6868. maxlen=script_getnum(st,3);
  6869. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6870. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6871. tmpstr[maxlen]='\0';
  6872. script_pushstr(st,tmpstr);
  6873. return 0;
  6874. }
  6875. /*==========================================
  6876. * カプラ倉庫を開く
  6877. *------------------------------------------*/
  6878. BUILDIN_FUNC(openstorage)
  6879. {
  6880. TBL_PC* sd;
  6881. sd = script_rid2sd(st);
  6882. if( sd == NULL )
  6883. return 0;
  6884. storage_storageopen(sd);
  6885. return 0;
  6886. }
  6887. BUILDIN_FUNC(guildopenstorage)
  6888. {
  6889. TBL_PC* sd;
  6890. int ret;
  6891. sd = script_rid2sd(st);
  6892. if( sd == NULL )
  6893. return 0;
  6894. ret = storage_guild_storageopen(sd);
  6895. script_pushint(st,ret);
  6896. return 0;
  6897. }
  6898. /*==========================================
  6899. * アイテムによるスキル発動
  6900. *------------------------------------------*/
  6901. /// itemskill <skill id>,<level>
  6902. /// itemskill "<skill name>",<level>
  6903. BUILDIN_FUNC(itemskill)
  6904. {
  6905. int id;
  6906. int lv;
  6907. TBL_PC* sd;
  6908. sd = script_rid2sd(st);
  6909. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6910. return 0;
  6911. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6912. lv = script_getnum(st,3);
  6913. sd->skillitem=id;
  6914. sd->skillitemlv=lv;
  6915. clif_item_skill(sd,id,lv);
  6916. return 0;
  6917. }
  6918. /*==========================================
  6919. * アイテム作成
  6920. *------------------------------------------*/
  6921. BUILDIN_FUNC(produce)
  6922. {
  6923. int trigger;
  6924. TBL_PC* sd;
  6925. sd = script_rid2sd(st);
  6926. if( sd == NULL )
  6927. return 0;
  6928. trigger=script_getnum(st,2);
  6929. clif_skill_produce_mix_list(sd, -1, trigger);
  6930. return 0;
  6931. }
  6932. /*==========================================
  6933. *
  6934. *------------------------------------------*/
  6935. BUILDIN_FUNC(cooking)
  6936. {
  6937. int trigger;
  6938. TBL_PC* sd;
  6939. sd = script_rid2sd(st);
  6940. if( sd == NULL )
  6941. return 0;
  6942. trigger=script_getnum(st,2);
  6943. clif_cooking_list(sd, trigger);
  6944. return 0;
  6945. }
  6946. /*==========================================
  6947. * NPCでペット作る
  6948. *------------------------------------------*/
  6949. BUILDIN_FUNC(makepet)
  6950. {
  6951. TBL_PC* sd;
  6952. int id,pet_id;
  6953. id=script_getnum(st,2);
  6954. sd = script_rid2sd(st);
  6955. if( sd == NULL )
  6956. return 0;
  6957. pet_id = search_petDB_index(id, PET_CLASS);
  6958. if (pet_id < 0)
  6959. pet_id = search_petDB_index(id, PET_EGG);
  6960. if (pet_id >= 0 && sd) {
  6961. sd->catch_target_class = pet_db[pet_id].class_;
  6962. intif_create_pet(
  6963. sd->status.account_id, sd->status.char_id,
  6964. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6965. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6966. 100, 0, 1, pet_db[pet_id].jname);
  6967. }
  6968. return 0;
  6969. }
  6970. /*==========================================
  6971. * NPCで経験値上げる
  6972. *------------------------------------------*/
  6973. BUILDIN_FUNC(getexp)
  6974. {
  6975. TBL_PC* sd;
  6976. int base=0,job=0;
  6977. double bonus;
  6978. sd = script_rid2sd(st);
  6979. if( sd == NULL )
  6980. return 0;
  6981. base=script_getnum(st,2);
  6982. job =script_getnum(st,3);
  6983. if(base<0 || job<0)
  6984. return 0;
  6985. // bonus for npc-given exp
  6986. bonus = battle_config.quest_exp_rate / 100.;
  6987. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6988. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6989. pc_gainexp(sd, NULL, base, job, true);
  6990. return 0;
  6991. }
  6992. /*==========================================
  6993. * Gain guild exp [Celest]
  6994. *------------------------------------------*/
  6995. BUILDIN_FUNC(guildgetexp)
  6996. {
  6997. TBL_PC* sd;
  6998. int exp;
  6999. sd = script_rid2sd(st);
  7000. if( sd == NULL )
  7001. return 0;
  7002. exp = script_getnum(st,2);
  7003. if(exp < 0)
  7004. return 0;
  7005. if(sd && sd->status.guild_id > 0)
  7006. guild_getexp (sd, exp);
  7007. return 0;
  7008. }
  7009. /*==========================================
  7010. * Changes the guild master of a guild [Skotlex]
  7011. *------------------------------------------*/
  7012. BUILDIN_FUNC(guildchangegm)
  7013. {
  7014. TBL_PC *sd;
  7015. int guild_id;
  7016. const char *name;
  7017. guild_id = script_getnum(st,2);
  7018. name = script_getstr(st,3);
  7019. sd=map_nick2sd(name);
  7020. if (!sd)
  7021. script_pushint(st,0);
  7022. else
  7023. script_pushint(st,guild_gm_change(guild_id, sd));
  7024. return 0;
  7025. }
  7026. /*==========================================
  7027. * モンスター発生
  7028. *------------------------------------------*/
  7029. BUILDIN_FUNC(monster)
  7030. {
  7031. const char* mapn = script_getstr(st,2);
  7032. int x = script_getnum(st,3);
  7033. int y = script_getnum(st,4);
  7034. const char* str = script_getstr(st,5);
  7035. int class_ = script_getnum(st,6);
  7036. int amount = script_getnum(st,7);
  7037. const char* event = "";
  7038. struct map_session_data* sd;
  7039. int m;
  7040. if( script_hasdata(st,8) )
  7041. {
  7042. event = script_getstr(st,8);
  7043. check_event(st, event);
  7044. }
  7045. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7046. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7047. return 1;
  7048. }
  7049. sd = map_id2sd(st->rid);
  7050. if( sd && strcmp(mapn,"this") == 0 )
  7051. m = sd->bl.m;
  7052. else
  7053. {
  7054. m = map_mapname2mapid(mapn);
  7055. if( map[m].flag.src4instance && st->instance_id )
  7056. { // Try to redirect to the instance map, not the src map
  7057. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7058. {
  7059. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7060. return 1;
  7061. }
  7062. }
  7063. }
  7064. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  7065. return 0;
  7066. }
  7067. /*==========================================
  7068. * Request List of Monster Drops
  7069. *------------------------------------------*/
  7070. BUILDIN_FUNC(getmobdrops)
  7071. {
  7072. int class_ = script_getnum(st,2);
  7073. int i, j = 0;
  7074. struct mob_db *mob;
  7075. if( !mobdb_checkid(class_) )
  7076. {
  7077. script_pushint(st, 0);
  7078. return 0;
  7079. }
  7080. mob = mob_db(class_);
  7081. for( i = 0; i < MAX_MOB_DROP; i++ )
  7082. {
  7083. if( mob->dropitem[i].nameid < 1 )
  7084. continue;
  7085. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7086. continue;
  7087. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7088. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7089. j++;
  7090. }
  7091. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7092. script_pushint(st, 1);
  7093. return 0;
  7094. }
  7095. /*==========================================
  7096. * モンスター発生
  7097. *------------------------------------------*/
  7098. BUILDIN_FUNC(areamonster)
  7099. {
  7100. const char* mapn = script_getstr(st,2);
  7101. int x0 = script_getnum(st,3);
  7102. int y0 = script_getnum(st,4);
  7103. int x1 = script_getnum(st,5);
  7104. int y1 = script_getnum(st,6);
  7105. const char* str = script_getstr(st,7);
  7106. int class_ = script_getnum(st,8);
  7107. int amount = script_getnum(st,9);
  7108. const char* event = "";
  7109. struct map_session_data* sd;
  7110. int m;
  7111. if( script_hasdata(st,10) )
  7112. {
  7113. event = script_getstr(st,10);
  7114. check_event(st, event);
  7115. }
  7116. sd = map_id2sd(st->rid);
  7117. if( sd && strcmp(mapn,"this") == 0 )
  7118. m = sd->bl.m;
  7119. else
  7120. {
  7121. m = map_mapname2mapid(mapn);
  7122. if( map[m].flag.src4instance && st->instance_id )
  7123. { // Try to redirect to the instance map, not the src map
  7124. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7125. {
  7126. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7127. return 1;
  7128. }
  7129. }
  7130. }
  7131. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  7132. return 0;
  7133. }
  7134. /*==========================================
  7135. * モンスター削除
  7136. *------------------------------------------*/
  7137. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7138. { //same fix but with killmonster instead - stripping events from mobs.
  7139. TBL_MOB* md = (TBL_MOB*)bl;
  7140. char *event=va_arg(ap,char *);
  7141. int allflag=va_arg(ap,int);
  7142. md->state.npc_killmonster = 1;
  7143. if(!allflag){
  7144. if(strcmp(event,md->npc_event)==0)
  7145. status_kill(bl);
  7146. }else{
  7147. if(!md->spawn)
  7148. status_kill(bl);
  7149. }
  7150. md->state.npc_killmonster = 0;
  7151. return 0;
  7152. }
  7153. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7154. {
  7155. TBL_MOB* md = (TBL_MOB*)bl;
  7156. char *event=va_arg(ap,char *);
  7157. int allflag=va_arg(ap,int);
  7158. if(!allflag){
  7159. if(strcmp(event,md->npc_event)==0)
  7160. status_kill(bl);
  7161. }else{
  7162. if(!md->spawn)
  7163. status_kill(bl);
  7164. }
  7165. return 0;
  7166. }
  7167. BUILDIN_FUNC(killmonster)
  7168. {
  7169. const char *mapname,*event;
  7170. int m,allflag=0;
  7171. mapname=script_getstr(st,2);
  7172. event=script_getstr(st,3);
  7173. if(strcmp(event,"All")==0)
  7174. allflag = 1;
  7175. else
  7176. check_event(st, event);
  7177. if( (m=map_mapname2mapid(mapname))<0 )
  7178. return 0;
  7179. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7180. return 0;
  7181. if( script_hasdata(st,4) ) {
  7182. if ( script_getnum(st,4) == 1 ) {
  7183. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7184. return 0;
  7185. }
  7186. }
  7187. map_freeblock_lock();
  7188. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7189. map_freeblock_unlock();
  7190. return 0;
  7191. }
  7192. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7193. { //Strips the event from the mob if it's killed the old method.
  7194. struct mob_data *md;
  7195. md = BL_CAST(BL_MOB, bl);
  7196. if (md->npc_event[0])
  7197. md->npc_event[0] = 0;
  7198. status_kill(bl);
  7199. return 0;
  7200. }
  7201. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7202. {
  7203. status_kill(bl);
  7204. return 0;
  7205. }
  7206. BUILDIN_FUNC(killmonsterall)
  7207. {
  7208. const char *mapname;
  7209. int m;
  7210. mapname=script_getstr(st,2);
  7211. if( (m = map_mapname2mapid(mapname))<0 )
  7212. return 0;
  7213. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7214. return 0;
  7215. if( script_hasdata(st,3) ) {
  7216. if ( script_getnum(st,3) == 1 ) {
  7217. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7218. return 0;
  7219. }
  7220. }
  7221. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7222. return 0;
  7223. }
  7224. /*==========================================
  7225. * Creates a clone of a player.
  7226. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7227. *------------------------------------------*/
  7228. BUILDIN_FUNC(clone)
  7229. {
  7230. TBL_PC *sd, *msd=NULL;
  7231. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7232. unsigned int duration = 0;
  7233. const char *map,*event="";
  7234. map=script_getstr(st,2);
  7235. x=script_getnum(st,3);
  7236. y=script_getnum(st,4);
  7237. event=script_getstr(st,5);
  7238. char_id=script_getnum(st,6);
  7239. if( script_hasdata(st,7) )
  7240. master_id=script_getnum(st,7);
  7241. if( script_hasdata(st,8) )
  7242. mode=script_getnum(st,8);
  7243. if( script_hasdata(st,9) )
  7244. flag=script_getnum(st,9);
  7245. if( script_hasdata(st,10) )
  7246. duration=script_getnum(st,10);
  7247. check_event(st, event);
  7248. m = map_mapname2mapid(map);
  7249. if (m < 0) return 0;
  7250. sd = map_charid2sd(char_id);
  7251. if (master_id) {
  7252. msd = map_charid2sd(master_id);
  7253. if (msd)
  7254. master_id = msd->bl.id;
  7255. else
  7256. master_id = 0;
  7257. }
  7258. if (sd) //Return ID of newly crafted clone.
  7259. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7260. else //Failed to create clone.
  7261. script_pushint(st,0);
  7262. return 0;
  7263. }
  7264. /*==========================================
  7265. * イベント実行
  7266. *------------------------------------------*/
  7267. BUILDIN_FUNC(doevent)
  7268. {
  7269. const char* event = script_getstr(st,2);
  7270. struct map_session_data* sd;
  7271. if( ( sd = script_rid2sd(st) ) == NULL )
  7272. {
  7273. return 0;
  7274. }
  7275. check_event(st, event);
  7276. npc_event(sd, event, 0);
  7277. return 0;
  7278. }
  7279. /*==========================================
  7280. * NPC主体イベント実行
  7281. *------------------------------------------*/
  7282. BUILDIN_FUNC(donpcevent)
  7283. {
  7284. const char* event = script_getstr(st,2);
  7285. check_event(st, event);
  7286. npc_event_do(event);
  7287. return 0;
  7288. }
  7289. /// for Aegis compatibility
  7290. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7291. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7292. {
  7293. const char* npc = script_getstr(st,2);
  7294. const char* command = script_getstr(st,3);
  7295. char event[EVENT_NAME_LENGTH];
  7296. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  7297. check_event(st, event);
  7298. npc_event_do(event);
  7299. return 0;
  7300. }
  7301. /*==========================================
  7302. * イベントタイマー追加
  7303. *------------------------------------------*/
  7304. BUILDIN_FUNC(addtimer)
  7305. {
  7306. int tick = script_getnum(st,2);
  7307. const char* event = script_getstr(st, 3);
  7308. TBL_PC* sd;
  7309. check_event(st, event);
  7310. sd = script_rid2sd(st);
  7311. if( sd == NULL )
  7312. return 0;
  7313. pc_addeventtimer(sd,tick,event);
  7314. return 0;
  7315. }
  7316. /*==========================================
  7317. * イベントタイマー削除
  7318. *------------------------------------------*/
  7319. BUILDIN_FUNC(deltimer)
  7320. {
  7321. const char *event;
  7322. TBL_PC* sd;
  7323. event=script_getstr(st, 2);
  7324. sd = script_rid2sd(st);
  7325. if( sd == NULL )
  7326. return 0;
  7327. check_event(st, event);
  7328. pc_deleventtimer(sd,event);
  7329. return 0;
  7330. }
  7331. /*==========================================
  7332. * イベントタイマーのカウント値追加
  7333. *------------------------------------------*/
  7334. BUILDIN_FUNC(addtimercount)
  7335. {
  7336. const char *event;
  7337. int tick;
  7338. TBL_PC* sd;
  7339. event=script_getstr(st, 2);
  7340. tick=script_getnum(st,3);
  7341. sd = script_rid2sd(st);
  7342. if( sd == NULL )
  7343. return 0;
  7344. check_event(st, event);
  7345. pc_addeventtimercount(sd,event,tick);
  7346. return 0;
  7347. }
  7348. /*==========================================
  7349. * NPCタイマー初期化
  7350. *------------------------------------------*/
  7351. BUILDIN_FUNC(initnpctimer)
  7352. {
  7353. struct npc_data *nd;
  7354. int flag = 0;
  7355. if( script_hasdata(st,3) )
  7356. { //Two arguments: NPC name and attach flag.
  7357. nd = npc_name2id(script_getstr(st, 2));
  7358. flag = script_getnum(st,3);
  7359. }
  7360. else if( script_hasdata(st,2) )
  7361. { //Check if argument is numeric (flag) or string (npc name)
  7362. struct script_data *data;
  7363. data = script_getdata(st,2);
  7364. get_val(st,data);
  7365. if( data_isstring(data) ) //NPC name
  7366. nd = npc_name2id(conv_str(st, data));
  7367. else if( data_isint(data) ) //Flag
  7368. {
  7369. nd = (struct npc_data *)map_id2bl(st->oid);
  7370. flag = conv_num(st,data);
  7371. }
  7372. else
  7373. {
  7374. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7375. return 1;
  7376. }
  7377. }
  7378. else
  7379. nd = (struct npc_data *)map_id2bl(st->oid);
  7380. if( !nd )
  7381. return 0;
  7382. if( flag ) //Attach
  7383. {
  7384. TBL_PC* sd = script_rid2sd(st);
  7385. if( sd == NULL )
  7386. return 0;
  7387. nd->u.scr.rid = sd->bl.id;
  7388. }
  7389. npc_settimerevent_tick(nd,0);
  7390. npc_timerevent_start(nd, st->rid);
  7391. return 0;
  7392. }
  7393. /*==========================================
  7394. * NPCタイマー開始
  7395. *------------------------------------------*/
  7396. BUILDIN_FUNC(startnpctimer)
  7397. {
  7398. struct npc_data *nd;
  7399. int flag = 0;
  7400. if( script_hasdata(st,3) )
  7401. { //Two arguments: NPC name and attach flag.
  7402. nd = npc_name2id(script_getstr(st, 2));
  7403. flag = script_getnum(st,3);
  7404. }
  7405. else if( script_hasdata(st,2) )
  7406. { //Check if argument is numeric (flag) or string (npc name)
  7407. struct script_data *data;
  7408. data = script_getdata(st,2);
  7409. get_val(st,data);
  7410. if( data_isstring(data) ) //NPC name
  7411. nd = npc_name2id(conv_str(st, data));
  7412. else if( data_isint(data) ) //Flag
  7413. {
  7414. nd = (struct npc_data *)map_id2bl(st->oid);
  7415. flag = conv_num(st,data);
  7416. }
  7417. else
  7418. {
  7419. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7420. return 1;
  7421. }
  7422. }
  7423. else
  7424. nd=(struct npc_data *)map_id2bl(st->oid);
  7425. if( !nd )
  7426. return 0;
  7427. if( flag ) //Attach
  7428. {
  7429. TBL_PC* sd = script_rid2sd(st);
  7430. if( sd == NULL )
  7431. return 0;
  7432. nd->u.scr.rid = sd->bl.id;
  7433. }
  7434. npc_timerevent_start(nd, st->rid);
  7435. return 0;
  7436. }
  7437. /*==========================================
  7438. * NPCタイマー停止
  7439. *------------------------------------------*/
  7440. BUILDIN_FUNC(stopnpctimer)
  7441. {
  7442. struct npc_data *nd;
  7443. int flag = 0;
  7444. if( script_hasdata(st,3) )
  7445. { //Two arguments: NPC name and attach flag.
  7446. nd = npc_name2id(script_getstr(st, 2));
  7447. flag = script_getnum(st,3);
  7448. }
  7449. else if( script_hasdata(st,2) )
  7450. { //Check if argument is numeric (flag) or string (npc name)
  7451. struct script_data *data;
  7452. data = script_getdata(st,2);
  7453. get_val(st,data);
  7454. if( data_isstring(data) ) //NPC name
  7455. nd = npc_name2id(conv_str(st, data));
  7456. else if( data_isint(data) ) //Flag
  7457. {
  7458. nd = (struct npc_data *)map_id2bl(st->oid);
  7459. flag = conv_num(st,data);
  7460. }
  7461. else
  7462. {
  7463. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7464. return 1;
  7465. }
  7466. }
  7467. else
  7468. nd=(struct npc_data *)map_id2bl(st->oid);
  7469. if( !nd )
  7470. return 0;
  7471. if( flag ) //Detach
  7472. nd->u.scr.rid = 0;
  7473. npc_timerevent_stop(nd);
  7474. return 0;
  7475. }
  7476. /*==========================================
  7477. * NPCタイマー情報所得
  7478. *------------------------------------------*/
  7479. BUILDIN_FUNC(getnpctimer)
  7480. {
  7481. struct npc_data *nd;
  7482. TBL_PC *sd;
  7483. int type = script_getnum(st,2);
  7484. int val = 0;
  7485. if( script_hasdata(st,3) )
  7486. nd = npc_name2id(script_getstr(st,3));
  7487. else
  7488. nd = (struct npc_data *)map_id2bl(st->oid);
  7489. if( !nd || nd->bl.type != BL_NPC )
  7490. {
  7491. script_pushint(st,0);
  7492. ShowError("getnpctimer: Invalid NPC.\n");
  7493. return 1;
  7494. }
  7495. switch( type )
  7496. {
  7497. case 0: val = npc_gettimerevent_tick(nd); break;
  7498. case 1:
  7499. if( nd->u.scr.rid )
  7500. {
  7501. sd = map_id2sd(nd->u.scr.rid);
  7502. if( !sd )
  7503. {
  7504. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7505. break;
  7506. }
  7507. val = (sd->npc_timer_id != INVALID_TIMER);
  7508. }
  7509. else
  7510. val = (nd->u.scr.timerid != INVALID_TIMER);
  7511. break;
  7512. case 2: val = nd->u.scr.timeramount; break;
  7513. }
  7514. script_pushint(st,val);
  7515. return 0;
  7516. }
  7517. /*==========================================
  7518. * NPCタイマー値設定
  7519. *------------------------------------------*/
  7520. BUILDIN_FUNC(setnpctimer)
  7521. {
  7522. int tick;
  7523. struct npc_data *nd;
  7524. tick = script_getnum(st,2);
  7525. if( script_hasdata(st,3) )
  7526. nd = npc_name2id(script_getstr(st,3));
  7527. else
  7528. nd = (struct npc_data *)map_id2bl(st->oid);
  7529. if( !nd || nd->bl.type != BL_NPC )
  7530. {
  7531. script_pushint(st,1);
  7532. ShowError("setnpctimer: Invalid NPC.\n");
  7533. return 1;
  7534. }
  7535. npc_settimerevent_tick(nd,tick);
  7536. script_pushint(st,0);
  7537. return 0;
  7538. }
  7539. /*==========================================
  7540. * attaches the player rid to the timer [Celest]
  7541. *------------------------------------------*/
  7542. BUILDIN_FUNC(attachnpctimer)
  7543. {
  7544. TBL_PC *sd;
  7545. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7546. if( !nd || nd->bl.type != BL_NPC )
  7547. {
  7548. script_pushint(st,1);
  7549. ShowError("setnpctimer: Invalid NPC.\n");
  7550. return 1;
  7551. }
  7552. if( script_hasdata(st,2) )
  7553. sd = map_nick2sd(script_getstr(st,2));
  7554. else
  7555. sd = script_rid2sd(st);
  7556. if( !sd )
  7557. {
  7558. script_pushint(st,1);
  7559. ShowWarning("attachnpctimer: Invalid player.\n");
  7560. return 1;
  7561. }
  7562. nd->u.scr.rid = sd->bl.id;
  7563. script_pushint(st,0);
  7564. return 0;
  7565. }
  7566. /*==========================================
  7567. * detaches a player rid from the timer [Celest]
  7568. *------------------------------------------*/
  7569. BUILDIN_FUNC(detachnpctimer)
  7570. {
  7571. struct npc_data *nd;
  7572. if( script_hasdata(st,2) )
  7573. nd = npc_name2id(script_getstr(st,2));
  7574. else
  7575. nd = (struct npc_data *)map_id2bl(st->oid);
  7576. if( !nd || nd->bl.type != BL_NPC )
  7577. {
  7578. script_pushint(st,1);
  7579. ShowError("detachnpctimer: Invalid NPC.\n");
  7580. return 1;
  7581. }
  7582. nd->u.scr.rid = 0;
  7583. script_pushint(st,0);
  7584. return 0;
  7585. }
  7586. /*==========================================
  7587. * To avoid "player not attached" script errors, this function is provided,
  7588. * it checks if there is a player attached to the current script. [Skotlex]
  7589. * If no, returns 0, if yes, returns the account_id of the attached player.
  7590. *------------------------------------------*/
  7591. BUILDIN_FUNC(playerattached)
  7592. {
  7593. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7594. script_pushint(st,0);
  7595. else
  7596. script_pushint(st,st->rid);
  7597. return 0;
  7598. }
  7599. /*==========================================
  7600. * 天の声アナウンス
  7601. *------------------------------------------*/
  7602. BUILDIN_FUNC(announce)
  7603. {
  7604. const char *mes = script_getstr(st,2);
  7605. int flag = script_getnum(st,3);
  7606. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7607. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7608. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7609. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7610. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7611. if (flag&0x0f) // Broadcast source or broadcast region defined
  7612. {
  7613. send_target target;
  7614. 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
  7615. if (bl == NULL)
  7616. return 0;
  7617. flag &= 0x07;
  7618. target = (flag == 1) ? ALL_SAMEMAP :
  7619. (flag == 2) ? AREA :
  7620. (flag == 3) ? SELF :
  7621. ALL_CLIENT;
  7622. if (fontColor)
  7623. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7624. else
  7625. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7626. }
  7627. else
  7628. {
  7629. if (fontColor)
  7630. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7631. else
  7632. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7633. }
  7634. return 0;
  7635. }
  7636. /*==========================================
  7637. * 天の声アナウンス(特定マップ)
  7638. *------------------------------------------*/
  7639. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7640. {
  7641. char *mes = va_arg(ap, char *);
  7642. int len = va_arg(ap, int);
  7643. int type = va_arg(ap, int);
  7644. char *fontColor = va_arg(ap, char *);
  7645. short fontType = (short)va_arg(ap, int);
  7646. short fontSize = (short)va_arg(ap, int);
  7647. short fontAlign = (short)va_arg(ap, int);
  7648. short fontY = (short)va_arg(ap, int);
  7649. if (fontColor)
  7650. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7651. else
  7652. clif_broadcast(bl, mes, len, type, SELF);
  7653. return 0;
  7654. }
  7655. BUILDIN_FUNC(mapannounce)
  7656. {
  7657. const char *mapname = script_getstr(st,2);
  7658. const char *mes = script_getstr(st,3);
  7659. int flag = script_getnum(st,4);
  7660. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7661. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7662. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7663. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7664. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7665. int m;
  7666. if ((m = map_mapname2mapid(mapname)) < 0)
  7667. return 0;
  7668. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7669. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7670. return 0;
  7671. }
  7672. /*==========================================
  7673. * 天の声アナウンス(特定エリア)
  7674. *------------------------------------------*/
  7675. BUILDIN_FUNC(areaannounce)
  7676. {
  7677. const char *mapname = script_getstr(st,2);
  7678. int x0 = script_getnum(st,3);
  7679. int y0 = script_getnum(st,4);
  7680. int x1 = script_getnum(st,5);
  7681. int y1 = script_getnum(st,6);
  7682. const char *mes = script_getstr(st,7);
  7683. int flag = script_getnum(st,8);
  7684. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7685. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7686. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7687. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7688. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7689. int m;
  7690. if ((m = map_mapname2mapid(mapname)) < 0)
  7691. return 0;
  7692. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7693. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7694. return 0;
  7695. }
  7696. /*==========================================
  7697. * ユーザー数所得
  7698. *------------------------------------------*/
  7699. BUILDIN_FUNC(getusers)
  7700. {
  7701. int flag, val = 0;
  7702. struct map_session_data* sd;
  7703. struct block_list* bl = NULL;
  7704. flag = script_getnum(st,2);
  7705. switch(flag&0x07)
  7706. {
  7707. case 0:
  7708. if(flag&0x8)
  7709. {// npc
  7710. bl = map_id2bl(st->oid);
  7711. }
  7712. else if((sd = script_rid2sd(st))!=NULL)
  7713. {// pc
  7714. bl = &sd->bl;
  7715. }
  7716. if(bl)
  7717. {
  7718. val = map[bl->m].users;
  7719. }
  7720. break;
  7721. case 1:
  7722. val = map_getusers();
  7723. break;
  7724. default:
  7725. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  7726. script_pushint(st,0);
  7727. return 1;
  7728. }
  7729. script_pushint(st,val);
  7730. return 0;
  7731. }
  7732. /*==========================================
  7733. * Works like @WHO - displays all online users names in window
  7734. *------------------------------------------*/
  7735. BUILDIN_FUNC(getusersname)
  7736. {
  7737. TBL_PC *sd, *pl_sd;
  7738. int disp_num=1;
  7739. struct s_mapiterator* iter;
  7740. sd = script_rid2sd(st);
  7741. if (!sd) return 0;
  7742. iter = mapit_getallusers();
  7743. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7744. {
  7745. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7746. continue; // skip hidden GMs
  7747. if((disp_num++)%10==0)
  7748. clif_scriptnext(sd,st->oid);
  7749. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7750. }
  7751. mapit_free(iter);
  7752. return 0;
  7753. }
  7754. /*==========================================
  7755. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7756. *------------------------------------------*/
  7757. BUILDIN_FUNC(getmapguildusers)
  7758. {
  7759. const char *str;
  7760. int m, gid;
  7761. int i=0,c=0;
  7762. struct guild *g = NULL;
  7763. str=script_getstr(st,2);
  7764. gid=script_getnum(st,3);
  7765. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7766. script_pushint(st,-1);
  7767. return 0;
  7768. }
  7769. g = guild_search(gid);
  7770. if (g){
  7771. for(i = 0; i < g->max_member; i++)
  7772. {
  7773. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7774. c++;
  7775. }
  7776. }
  7777. script_pushint(st,c);
  7778. return 0;
  7779. }
  7780. /*==========================================
  7781. * マップ指定ユーザー数所得
  7782. *------------------------------------------*/
  7783. BUILDIN_FUNC(getmapusers)
  7784. {
  7785. const char *str;
  7786. int m;
  7787. str=script_getstr(st,2);
  7788. if( (m=map_mapname2mapid(str))< 0){
  7789. script_pushint(st,-1);
  7790. return 0;
  7791. }
  7792. script_pushint(st,map[m].users);
  7793. return 0;
  7794. }
  7795. /*==========================================
  7796. * エリア指定ユーザー数所得
  7797. *------------------------------------------*/
  7798. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7799. {
  7800. int *users=va_arg(ap,int *);
  7801. (*users)++;
  7802. return 0;
  7803. }
  7804. BUILDIN_FUNC(getareausers)
  7805. {
  7806. const char *str;
  7807. int m,x0,y0,x1,y1,users=0;
  7808. str=script_getstr(st,2);
  7809. x0=script_getnum(st,3);
  7810. y0=script_getnum(st,4);
  7811. x1=script_getnum(st,5);
  7812. y1=script_getnum(st,6);
  7813. if( (m=map_mapname2mapid(str))< 0){
  7814. script_pushint(st,-1);
  7815. return 0;
  7816. }
  7817. map_foreachinarea(buildin_getareausers_sub,
  7818. m,x0,y0,x1,y1,BL_PC,&users);
  7819. script_pushint(st,users);
  7820. return 0;
  7821. }
  7822. /*==========================================
  7823. * エリア指定ドロップアイテム数所得
  7824. *------------------------------------------*/
  7825. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7826. {
  7827. int item=va_arg(ap,int);
  7828. int *amount=va_arg(ap,int *);
  7829. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7830. if(drop->item_data.nameid==item)
  7831. (*amount)+=drop->item_data.amount;
  7832. return 0;
  7833. }
  7834. BUILDIN_FUNC(getareadropitem)
  7835. {
  7836. const char *str;
  7837. int m,x0,y0,x1,y1,item,amount=0;
  7838. struct script_data *data;
  7839. str=script_getstr(st,2);
  7840. x0=script_getnum(st,3);
  7841. y0=script_getnum(st,4);
  7842. x1=script_getnum(st,5);
  7843. y1=script_getnum(st,6);
  7844. data=script_getdata(st,7);
  7845. get_val(st,data);
  7846. if( data_isstring(data) ){
  7847. const char *name=conv_str(st,data);
  7848. struct item_data *item_data = itemdb_searchname(name);
  7849. item=UNKNOWN_ITEM_ID;
  7850. if( item_data )
  7851. item=item_data->nameid;
  7852. }else
  7853. item=conv_num(st,data);
  7854. if( (m=map_mapname2mapid(str))< 0){
  7855. script_pushint(st,-1);
  7856. return 0;
  7857. }
  7858. map_foreachinarea(buildin_getareadropitem_sub,
  7859. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7860. script_pushint(st,amount);
  7861. return 0;
  7862. }
  7863. /*==========================================
  7864. * NPCの有効化
  7865. *------------------------------------------*/
  7866. BUILDIN_FUNC(enablenpc)
  7867. {
  7868. const char *str;
  7869. str=script_getstr(st,2);
  7870. npc_enable(str,1);
  7871. return 0;
  7872. }
  7873. /*==========================================
  7874. * NPCの無効化
  7875. *------------------------------------------*/
  7876. BUILDIN_FUNC(disablenpc)
  7877. {
  7878. const char *str;
  7879. str=script_getstr(st,2);
  7880. npc_enable(str,0);
  7881. return 0;
  7882. }
  7883. /*==========================================
  7884. * 隠れているNPCの表示
  7885. *------------------------------------------*/
  7886. BUILDIN_FUNC(hideoffnpc)
  7887. {
  7888. const char *str;
  7889. str=script_getstr(st,2);
  7890. npc_enable(str,2);
  7891. return 0;
  7892. }
  7893. /*==========================================
  7894. * NPCをハイディング
  7895. *------------------------------------------*/
  7896. BUILDIN_FUNC(hideonnpc)
  7897. {
  7898. const char *str;
  7899. str=script_getstr(st,2);
  7900. npc_enable(str,4);
  7901. return 0;
  7902. }
  7903. /// Starts a status effect on the target unit or on the attached player.
  7904. ///
  7905. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7906. BUILDIN_FUNC(sc_start)
  7907. {
  7908. struct block_list* bl;
  7909. enum sc_type type;
  7910. int tick;
  7911. int val1;
  7912. int val4 = 0;
  7913. type = (sc_type)script_getnum(st,2);
  7914. tick = script_getnum(st,3);
  7915. val1 = script_getnum(st,4);
  7916. if( script_hasdata(st,5) )
  7917. bl = map_id2bl(script_getnum(st,5));
  7918. else
  7919. bl = map_id2bl(st->rid);
  7920. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7921. {// When there isn't a duration specified, try to get it from the skill_db
  7922. tick = skill_get_time(status_sc2skill(type), val1);
  7923. }
  7924. if( potion_flag == 1 && potion_target )
  7925. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7926. bl = map_id2bl(potion_target);
  7927. tick /= 2;// Thrown potions only last half.
  7928. val4 = 1;// Mark that this was a thrown sc_effect
  7929. }
  7930. if( bl )
  7931. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7932. return 0;
  7933. }
  7934. /// Starts a status effect on the target unit or on the attached player.
  7935. ///
  7936. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7937. BUILDIN_FUNC(sc_start2)
  7938. {
  7939. struct block_list* bl;
  7940. enum sc_type type;
  7941. int tick;
  7942. int val1;
  7943. int val4 = 0;
  7944. int rate;
  7945. type = (sc_type)script_getnum(st,2);
  7946. tick = script_getnum(st,3);
  7947. val1 = script_getnum(st,4);
  7948. rate = script_getnum(st,5);
  7949. if( script_hasdata(st,6) )
  7950. bl = map_id2bl(script_getnum(st,6));
  7951. else
  7952. bl = map_id2bl(st->rid);
  7953. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7954. {// When there isn't a duration specified, try to get it from the skill_db
  7955. tick = skill_get_time(status_sc2skill(type), val1);
  7956. }
  7957. if( potion_flag == 1 && potion_target )
  7958. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7959. bl = map_id2bl(potion_target);
  7960. tick /= 2;// Thrown potions only last half.
  7961. val4 = 1;// Mark that this was a thrown sc_effect
  7962. }
  7963. if( bl )
  7964. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7965. return 0;
  7966. }
  7967. /// Starts a status effect on the target unit or on the attached player.
  7968. ///
  7969. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7970. BUILDIN_FUNC(sc_start4)
  7971. {
  7972. struct block_list* bl;
  7973. enum sc_type type;
  7974. int tick;
  7975. int val1;
  7976. int val2;
  7977. int val3;
  7978. int val4;
  7979. type = (sc_type)script_getnum(st,2);
  7980. tick = script_getnum(st,3);
  7981. val1 = script_getnum(st,4);
  7982. val2 = script_getnum(st,5);
  7983. val3 = script_getnum(st,6);
  7984. val4 = script_getnum(st,7);
  7985. if( script_hasdata(st,8) )
  7986. bl = map_id2bl(script_getnum(st,8));
  7987. else
  7988. bl = map_id2bl(st->rid);
  7989. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7990. {// When there isn't a duration specified, try to get it from the skill_db
  7991. tick = skill_get_time(status_sc2skill(type), val1);
  7992. }
  7993. if( potion_flag == 1 && potion_target )
  7994. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7995. bl = map_id2bl(potion_target);
  7996. tick /= 2;// Thrown potions only last half.
  7997. }
  7998. if( bl )
  7999. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  8000. return 0;
  8001. }
  8002. /// Ends one or all status effects on the target unit or on the attached player.
  8003. ///
  8004. /// sc_end <effect_id>{,<unit_id>};
  8005. BUILDIN_FUNC(sc_end)
  8006. {
  8007. struct block_list* bl;
  8008. int type;
  8009. type = script_getnum(st,2);
  8010. if( script_hasdata(st,3) )
  8011. bl = map_id2bl(script_getnum(st,3));
  8012. else
  8013. bl = map_id2bl(st->rid);
  8014. if( potion_flag==1 && potion_target )
  8015. {//##TODO how does this work [FlavioJS]
  8016. bl = map_id2bl(potion_target);
  8017. }
  8018. if( !bl ) return 0;
  8019. if( type >= 0 && type < SC_MAX )
  8020. {
  8021. struct status_change *sc = status_get_sc(bl);
  8022. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  8023. if (!sce) return 0;
  8024. //This should help status_change_end force disabling the SC in case it has no limit.
  8025. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8026. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8027. } else
  8028. status_change_clear(bl, 2);// remove all effects
  8029. return 0;
  8030. }
  8031. /*==========================================
  8032. * 状態異常耐性を計算した確率を返す
  8033. *------------------------------------------*/
  8034. BUILDIN_FUNC(getscrate)
  8035. {
  8036. struct block_list *bl;
  8037. int type,rate;
  8038. type=script_getnum(st,2);
  8039. rate=script_getnum(st,3);
  8040. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  8041. bl = map_id2bl(script_getnum(st,4));
  8042. else
  8043. bl = map_id2bl(st->rid);
  8044. if (bl)
  8045. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  8046. script_pushint(st,rate);
  8047. return 0;
  8048. }
  8049. /*==========================================
  8050. * getstatus <type>{, <info>};
  8051. *------------------------------------------*/
  8052. BUILDIN_FUNC(getstatus)
  8053. {
  8054. int id, type;
  8055. struct map_session_data* sd = script_rid2sd(st);
  8056. if( sd == NULL )
  8057. {// no player attached
  8058. return 0;
  8059. }
  8060. id = script_getnum(st, 2);
  8061. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8062. if( id <= SC_NONE || id >= SC_MAX )
  8063. {// invalid status type given
  8064. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8065. return 0;
  8066. }
  8067. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8068. {// no status is active
  8069. script_pushint(st, 0);
  8070. return 0;
  8071. }
  8072. switch( type )
  8073. {
  8074. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8075. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8076. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8077. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8078. case 5:
  8079. {
  8080. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8081. if( timer )
  8082. {// return the amount of time remaining
  8083. script_pushint(st, timer->tick - gettick());
  8084. }
  8085. }
  8086. break;
  8087. default: script_pushint(st, 1); break;
  8088. }
  8089. return 0;
  8090. }
  8091. /*==========================================
  8092. *
  8093. *------------------------------------------*/
  8094. BUILDIN_FUNC(debugmes)
  8095. {
  8096. const char *str;
  8097. str=script_getstr(st,2);
  8098. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8099. return 0;
  8100. }
  8101. /*==========================================
  8102. *捕獲アイテム使用
  8103. *------------------------------------------*/
  8104. BUILDIN_FUNC(catchpet)
  8105. {
  8106. int pet_id;
  8107. TBL_PC *sd;
  8108. pet_id= script_getnum(st,2);
  8109. sd=script_rid2sd(st);
  8110. if( sd == NULL )
  8111. return 0;
  8112. pet_catch_process1(sd,pet_id);
  8113. return 0;
  8114. }
  8115. /*==========================================
  8116. * [orn]
  8117. *------------------------------------------*/
  8118. BUILDIN_FUNC(homunculus_evolution)
  8119. {
  8120. TBL_PC *sd;
  8121. sd=script_rid2sd(st);
  8122. if( sd == NULL )
  8123. return 0;
  8124. if(merc_is_hom_active(sd->hd))
  8125. {
  8126. if (sd->hd->homunculus.intimacy > 91000)
  8127. merc_hom_evolution(sd->hd);
  8128. else
  8129. clif_emotion(&sd->hd->bl, E_SWT);
  8130. }
  8131. return 0;
  8132. }
  8133. // [Zephyrus]
  8134. BUILDIN_FUNC(homunculus_shuffle)
  8135. {
  8136. TBL_PC *sd;
  8137. sd=script_rid2sd(st);
  8138. if( sd == NULL )
  8139. return 0;
  8140. if(merc_is_hom_active(sd->hd))
  8141. merc_hom_shuffle(sd->hd);
  8142. return 0;
  8143. }
  8144. //These two functions bring the eA MAPID_* class functionality to scripts.
  8145. BUILDIN_FUNC(eaclass)
  8146. {
  8147. int class_;
  8148. if( script_hasdata(st,2) )
  8149. class_ = script_getnum(st,2);
  8150. else {
  8151. TBL_PC *sd;
  8152. sd=script_rid2sd(st);
  8153. if (!sd) {
  8154. script_pushint(st,-1);
  8155. return 0;
  8156. }
  8157. class_ = sd->status.class_;
  8158. }
  8159. script_pushint(st,pc_jobid2mapid(class_));
  8160. return 0;
  8161. }
  8162. BUILDIN_FUNC(roclass)
  8163. {
  8164. int class_ =script_getnum(st,2);
  8165. int sex;
  8166. if( script_hasdata(st,3) )
  8167. sex = script_getnum(st,3);
  8168. else {
  8169. TBL_PC *sd;
  8170. if (st->rid && (sd=script_rid2sd(st)))
  8171. sex = sd->status.sex;
  8172. else
  8173. sex = 1; //Just use male when not found.
  8174. }
  8175. script_pushint(st,pc_mapid2jobid(class_, sex));
  8176. return 0;
  8177. }
  8178. /*==========================================
  8179. * Tells client to open a hatching window, used for pet incubator
  8180. *------------------------------------------*/
  8181. BUILDIN_FUNC(birthpet)
  8182. {
  8183. TBL_PC *sd;
  8184. sd=script_rid2sd(st);
  8185. if( sd == NULL )
  8186. return 0;
  8187. if( sd->status.pet_id )
  8188. {// do not send egg list, when you already have a pet
  8189. return 0;
  8190. }
  8191. clif_sendegg(sd);
  8192. return 0;
  8193. }
  8194. /*==========================================
  8195. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8196. *------------------------------------------*/
  8197. BUILDIN_FUNC(resetlvl)
  8198. {
  8199. TBL_PC *sd;
  8200. int type=script_getnum(st,2);
  8201. sd=script_rid2sd(st);
  8202. if( sd == NULL )
  8203. return 0;
  8204. pc_resetlvl(sd,type);
  8205. return 0;
  8206. }
  8207. /*==========================================
  8208. * ステータスリセット
  8209. *------------------------------------------*/
  8210. BUILDIN_FUNC(resetstatus)
  8211. {
  8212. TBL_PC *sd;
  8213. sd=script_rid2sd(st);
  8214. pc_resetstate(sd);
  8215. return 0;
  8216. }
  8217. /*==========================================
  8218. * script command resetskill
  8219. *------------------------------------------*/
  8220. BUILDIN_FUNC(resetskill)
  8221. {
  8222. TBL_PC *sd;
  8223. sd=script_rid2sd(st);
  8224. pc_resetskill(sd,1);
  8225. return 0;
  8226. }
  8227. /*==========================================
  8228. * Counts total amount of skill points.
  8229. *------------------------------------------*/
  8230. BUILDIN_FUNC(skillpointcount)
  8231. {
  8232. TBL_PC *sd;
  8233. sd=script_rid2sd(st);
  8234. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8235. return 0;
  8236. }
  8237. /*==========================================
  8238. *
  8239. *------------------------------------------*/
  8240. BUILDIN_FUNC(changebase)
  8241. {
  8242. TBL_PC *sd=NULL;
  8243. int vclass;
  8244. if( script_hasdata(st,3) )
  8245. sd=map_id2sd(script_getnum(st,3));
  8246. else
  8247. sd=script_rid2sd(st);
  8248. if(sd == NULL)
  8249. return 0;
  8250. vclass = script_getnum(st,2);
  8251. if(vclass == JOB_WEDDING)
  8252. {
  8253. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8254. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8255. )
  8256. return 0;
  8257. }
  8258. if(!sd->disguise && vclass != sd->vd.class_) {
  8259. status_set_viewdata(&sd->bl, vclass);
  8260. //Updated client view. Base, Weapon and Cloth Colors.
  8261. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8262. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8263. if (sd->vd.cloth_color)
  8264. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8265. clif_skillinfoblock(sd);
  8266. }
  8267. return 0;
  8268. }
  8269. /*==========================================
  8270. * 性別変換
  8271. *------------------------------------------*/
  8272. BUILDIN_FUNC(changesex)
  8273. {
  8274. TBL_PC *sd = NULL;
  8275. sd = script_rid2sd(st);
  8276. chrif_changesex(sd);
  8277. return 0;
  8278. }
  8279. /*==========================================
  8280. * Works like 'announce' but outputs in the common chat window
  8281. *------------------------------------------*/
  8282. BUILDIN_FUNC(globalmes)
  8283. {
  8284. struct block_list *bl = map_id2bl(st->oid);
  8285. struct npc_data *nd = (struct npc_data *)bl;
  8286. const char *name=NULL,*mes;
  8287. mes=script_getstr(st,2); // メッセージの取得
  8288. if(mes==NULL) return 0;
  8289. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8290. name=script_getstr(st,3);
  8291. } else {
  8292. name=nd->name;
  8293. }
  8294. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8295. return 0;
  8296. }
  8297. /////////////////////////////////////////////////////////////////////
  8298. // NPC waiting room (chat room)
  8299. //
  8300. /// Creates a waiting room (chat room) for this npc.
  8301. ///
  8302. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  8303. BUILDIN_FUNC(waitingroom)
  8304. {
  8305. struct npc_data* nd;
  8306. int pub = 1;
  8307. const char* title = script_getstr(st, 2);
  8308. int limit = script_getnum(st, 3);
  8309. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  8310. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  8311. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  8312. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  8313. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  8314. nd = (struct npc_data *)map_id2bl(st->oid);
  8315. if( nd != NULL )
  8316. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  8317. return 0;
  8318. }
  8319. /// Removes the waiting room of the current or target npc.
  8320. ///
  8321. /// delwaitingroom "<npc_name>";
  8322. /// delwaitingroom;
  8323. BUILDIN_FUNC(delwaitingroom)
  8324. {
  8325. struct npc_data* nd;
  8326. if( script_hasdata(st,2) )
  8327. nd = npc_name2id(script_getstr(st, 2));
  8328. else
  8329. nd = (struct npc_data *)map_id2bl(st->oid);
  8330. if( nd != NULL )
  8331. chat_deletenpcchat(nd);
  8332. return 0;
  8333. }
  8334. /// Kicks all the players from the waiting room of the current or target npc.
  8335. ///
  8336. /// kickwaitingroomall "<npc_name>";
  8337. /// kickwaitingroomall;
  8338. BUILDIN_FUNC(waitingroomkickall)
  8339. {
  8340. struct npc_data* nd;
  8341. struct chat_data* cd;
  8342. if( script_hasdata(st,2) )
  8343. nd = npc_name2id(script_getstr(st,2));
  8344. else
  8345. nd = (struct npc_data *)map_id2bl(st->oid);
  8346. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8347. chat_npckickall(cd);
  8348. return 0;
  8349. }
  8350. /// Enables the waiting room event of the current or target npc.
  8351. ///
  8352. /// enablewaitingroomevent "<npc_name>";
  8353. /// enablewaitingroomevent;
  8354. BUILDIN_FUNC(enablewaitingroomevent)
  8355. {
  8356. struct npc_data* nd;
  8357. struct chat_data* cd;
  8358. if( script_hasdata(st,2) )
  8359. nd = npc_name2id(script_getstr(st, 2));
  8360. else
  8361. nd = (struct npc_data *)map_id2bl(st->oid);
  8362. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8363. chat_enableevent(cd);
  8364. return 0;
  8365. }
  8366. /// Disables the waiting room event of the current or target npc.
  8367. ///
  8368. /// disablewaitingroomevent "<npc_name>";
  8369. /// disablewaitingroomevent;
  8370. BUILDIN_FUNC(disablewaitingroomevent)
  8371. {
  8372. struct npc_data *nd;
  8373. struct chat_data *cd;
  8374. if( script_hasdata(st,2) )
  8375. nd = npc_name2id(script_getstr(st, 2));
  8376. else
  8377. nd = (struct npc_data *)map_id2bl(st->oid);
  8378. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8379. chat_disableevent(cd);
  8380. return 0;
  8381. }
  8382. /// Returns info on the waiting room of the current or target npc.
  8383. /// Returns -1 if the type unknown
  8384. /// <type>=0 : current number of users
  8385. /// <type>=1 : maximum number of users allowed
  8386. /// <type>=2 : the number of users that trigger the event
  8387. /// <type>=3 : if the trigger is disabled
  8388. /// <type>=4 : the title of the waiting room
  8389. /// <type>=5 : the password of the waiting room
  8390. /// <type>=16 : the name of the waiting room event
  8391. /// <type>=32 : if the waiting room is full
  8392. /// <type>=33 : if there are enough users to trigger the event
  8393. ///
  8394. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8395. /// getwaitingroomstate(<type>) -> <info>
  8396. BUILDIN_FUNC(getwaitingroomstate)
  8397. {
  8398. struct npc_data *nd;
  8399. struct chat_data *cd;
  8400. int type;
  8401. type = script_getnum(st,2);
  8402. if( script_hasdata(st,3) )
  8403. nd = npc_name2id(script_getstr(st, 3));
  8404. else
  8405. nd = (struct npc_data *)map_id2bl(st->oid);
  8406. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8407. {
  8408. script_pushint(st, -1);
  8409. return 0;
  8410. }
  8411. switch(type)
  8412. {
  8413. case 0: script_pushint(st, cd->users); break;
  8414. case 1: script_pushint(st, cd->limit); break;
  8415. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8416. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8417. case 4: script_pushstrcopy(st, cd->title); break;
  8418. case 5: script_pushstrcopy(st, cd->pass); break;
  8419. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8420. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8421. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8422. default: script_pushint(st, -1); break;
  8423. }
  8424. return 0;
  8425. }
  8426. /// Warps the trigger or target amount of players to the target map and position.
  8427. /// Players are automatically removed from the waiting room.
  8428. /// Those waiting the longest will get warped first.
  8429. /// The target map can be "Random" for a random position in the current map,
  8430. /// and "SavePoint" for the savepoint map+position.
  8431. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8432. ///
  8433. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8434. /// The total number of teleported players is put into $@warpwaitingpcnum
  8435. ///
  8436. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8437. /// warpwaitingpc "<map name>",<x>,<y>;
  8438. BUILDIN_FUNC(warpwaitingpc)
  8439. {
  8440. int x;
  8441. int y;
  8442. int i;
  8443. int n;
  8444. const char* map_name;
  8445. struct npc_data* nd;
  8446. struct chat_data* cd;
  8447. TBL_PC* sd;
  8448. nd = (struct npc_data *)map_id2bl(st->oid);
  8449. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8450. return 0;
  8451. map_name = script_getstr(st,2);
  8452. x = script_getnum(st,3);
  8453. y = script_getnum(st,4);
  8454. n = cd->trigger&0x7f;
  8455. if( script_hasdata(st,5) )
  8456. n = script_getnum(st,5);
  8457. for( i = 0; i < n && cd->users > 0; i++ )
  8458. {
  8459. sd = cd->usersd[0];
  8460. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  8461. {// can't teleport on this map
  8462. break;
  8463. }
  8464. if( cd->zeny )
  8465. {// fee set
  8466. if( (uint32)sd->status.zeny < cd->zeny )
  8467. {// no zeny to cover set fee
  8468. break;
  8469. }
  8470. pc_payzeny(sd, cd->zeny);
  8471. }
  8472. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  8473. if( strcmp(map_name,"Random") == 0 )
  8474. pc_randomwarp(sd,CLR_TELEPORT);
  8475. else if( strcmp(map_name,"SavePoint") == 0 )
  8476. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  8477. else
  8478. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  8479. }
  8480. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8481. return 0;
  8482. }
  8483. /////////////////////////////////////////////////////////////////////
  8484. // ...
  8485. //
  8486. /// Detaches a character from a script.
  8487. ///
  8488. /// @param st Script state to detach the character from.
  8489. static void script_detach_rid(struct script_state* st)
  8490. {
  8491. if(st->rid)
  8492. {
  8493. script_detach_state(st, false);
  8494. st->rid = 0;
  8495. }
  8496. }
  8497. /*==========================================
  8498. * RIDのアタッチ
  8499. *------------------------------------------*/
  8500. BUILDIN_FUNC(attachrid)
  8501. {
  8502. int rid = script_getnum(st,2);
  8503. struct map_session_data* sd;
  8504. if ((sd = map_id2sd(rid))!=NULL) {
  8505. script_detach_rid(st);
  8506. st->rid = rid;
  8507. script_attach_state(st);
  8508. script_pushint(st,1);
  8509. } else
  8510. script_pushint(st,0);
  8511. return 0;
  8512. }
  8513. /*==========================================
  8514. * RIDのデタッチ
  8515. *------------------------------------------*/
  8516. BUILDIN_FUNC(detachrid)
  8517. {
  8518. script_detach_rid(st);
  8519. return 0;
  8520. }
  8521. /*==========================================
  8522. * 存在チェック
  8523. *------------------------------------------*/
  8524. BUILDIN_FUNC(isloggedin)
  8525. {
  8526. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8527. if (script_hasdata(st,3) && sd &&
  8528. sd->status.char_id != script_getnum(st,3))
  8529. sd = NULL;
  8530. push_val(st->stack,C_INT,sd!=NULL);
  8531. return 0;
  8532. }
  8533. /*==========================================
  8534. *
  8535. *------------------------------------------*/
  8536. BUILDIN_FUNC(setmapflagnosave)
  8537. {
  8538. int m,x,y;
  8539. unsigned short mapindex;
  8540. const char *str,*str2;
  8541. str=script_getstr(st,2);
  8542. str2=script_getstr(st,3);
  8543. x=script_getnum(st,4);
  8544. y=script_getnum(st,5);
  8545. m = map_mapname2mapid(str);
  8546. mapindex = mapindex_name2id(str2);
  8547. if(m >= 0 && mapindex) {
  8548. map[m].flag.nosave=1;
  8549. map[m].save.map=mapindex;
  8550. map[m].save.x=x;
  8551. map[m].save.y=y;
  8552. }
  8553. return 0;
  8554. }
  8555. BUILDIN_FUNC(getmapflag)
  8556. {
  8557. int m,i;
  8558. const char *str;
  8559. str=script_getstr(st,2);
  8560. i=script_getnum(st,3);
  8561. m = map_mapname2mapid(str);
  8562. if(m >= 0) {
  8563. switch(i) {
  8564. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8565. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8566. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  8567. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8568. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8569. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8570. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8571. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8572. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8573. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8574. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8575. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8576. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8577. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8578. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8579. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8580. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8581. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8582. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8583. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8584. /**
  8585. * No longer available, keeping here just in case it's back someday. [Ind]
  8586. **/
  8587. //case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8588. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8589. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8590. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8591. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8592. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8593. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8594. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8595. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8596. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8597. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8598. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8599. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8600. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8601. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8602. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8603. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8604. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8605. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8606. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8607. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8608. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8609. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8610. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  8611. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8612. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8613. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8614. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8615. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8616. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8617. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8618. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  8619. }
  8620. }
  8621. return 0;
  8622. }
  8623. BUILDIN_FUNC(setmapflag)
  8624. {
  8625. int m,i;
  8626. const char *str;
  8627. const char *val=NULL;
  8628. str=script_getstr(st,2);
  8629. i=script_getnum(st,3);
  8630. if(script_hasdata(st,4)){
  8631. val=script_getstr(st,4);
  8632. }
  8633. m = map_mapname2mapid(str);
  8634. if(m >= 0) {
  8635. switch(i) {
  8636. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  8637. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  8638. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  8639. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  8640. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  8641. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  8642. case MF_PVP: map[m].flag.pvp = 1; break;
  8643. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  8644. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  8645. case MF_GVG: map[m].flag.gvg = 1; break;
  8646. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  8647. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  8648. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  8649. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  8650. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  8651. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  8652. case MF_SNOW: map[m].flag.snow = 1; break;
  8653. case MF_FOG: map[m].flag.fog = 1; break;
  8654. case MF_SAKURA: map[m].flag.sakura = 1; break;
  8655. case MF_LEAVES: map[m].flag.leaves = 1; break;
  8656. /**
  8657. * No longer available, keeping here just in case it's back someday. [Ind]
  8658. **/
  8659. //case MF_RAIN: map[m].flag.rain = 1; break;
  8660. case MF_NOGO: map[m].flag.nogo = 1; break;
  8661. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  8662. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  8663. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  8664. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  8665. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  8666. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  8667. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  8668. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  8669. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  8670. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  8671. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  8672. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  8673. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  8674. case MF_RESTRICTED: map[m].flag.restricted = 1; break;
  8675. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8676. case MF_NODROP: map[m].flag.nodrop = 1; break;
  8677. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8678. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8679. case MF_NOVENDING: map[m].flag.novending = 1; break;
  8680. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  8681. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  8682. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  8683. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  8684. case MF_TOWN: map[m].flag.town = 1; break;
  8685. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  8686. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  8687. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  8688. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  8689. case MF_BATTLEGROUND: (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  8690. case MF_RESET: map[m].flag.reset = 1; break;
  8691. }
  8692. }
  8693. return 0;
  8694. }
  8695. BUILDIN_FUNC(removemapflag)
  8696. {
  8697. int m,i;
  8698. const char *str;
  8699. str=script_getstr(st,2);
  8700. i=script_getnum(st,3);
  8701. m = map_mapname2mapid(str);
  8702. if(m >= 0) {
  8703. switch(i) {
  8704. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  8705. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  8706. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  8707. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  8708. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  8709. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  8710. case MF_PVP: map[m].flag.pvp = 0; break;
  8711. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  8712. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  8713. case MF_GVG: map[m].flag.gvg = 0; break;
  8714. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  8715. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  8716. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  8717. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  8718. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  8719. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  8720. case MF_SNOW: map[m].flag.snow = 0; break;
  8721. case MF_FOG: map[m].flag.fog = 0; break;
  8722. case MF_SAKURA: map[m].flag.sakura = 0; break;
  8723. case MF_LEAVES: map[m].flag.leaves = 0; break;
  8724. /**
  8725. * No longer available, keeping here just in case it's back someday. [Ind]
  8726. **/
  8727. //case MF_RAIN: map[m].flag.rain = 0; break;
  8728. case MF_NOGO: map[m].flag.nogo = 0; break;
  8729. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  8730. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  8731. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  8732. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  8733. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  8734. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  8735. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  8736. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  8737. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  8738. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  8739. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  8740. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  8741. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  8742. case MF_RESTRICTED: map[m].flag.restricted = 0; break;
  8743. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  8744. case MF_NODROP: map[m].flag.nodrop = 0; break;
  8745. case MF_JEXP: map[m].jexp = 0; break;
  8746. case MF_BEXP: map[m].bexp = 0; break;
  8747. case MF_NOVENDING: map[m].flag.novending = 0; break;
  8748. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  8749. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  8750. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  8751. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  8752. case MF_TOWN: map[m].flag.town = 0; break;
  8753. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  8754. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  8755. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  8756. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  8757. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  8758. case MF_RESET: map[m].flag.reset = 0; break;
  8759. }
  8760. }
  8761. return 0;
  8762. }
  8763. BUILDIN_FUNC(pvpon)
  8764. {
  8765. int m;
  8766. const char *str;
  8767. TBL_PC* sd = NULL;
  8768. struct s_mapiterator* iter;
  8769. str = script_getstr(st,2);
  8770. m = map_mapname2mapid(str);
  8771. if( m < 0 || map[m].flag.pvp )
  8772. return 0; // nothing to do
  8773. map[m].flag.pvp = 1;
  8774. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  8775. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8776. return 0;
  8777. iter = mapit_getallusers();
  8778. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8779. {
  8780. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  8781. continue; // not applicable
  8782. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8783. sd->pvp_rank = 0;
  8784. sd->pvp_lastusers = 0;
  8785. sd->pvp_point = 5;
  8786. sd->pvp_won = 0;
  8787. sd->pvp_lost = 0;
  8788. }
  8789. mapit_free(iter);
  8790. return 0;
  8791. }
  8792. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8793. {
  8794. TBL_PC* sd = (TBL_PC*)bl;
  8795. clif_pvpset(sd, 0, 0, 2);
  8796. if (sd->pvp_timer != INVALID_TIMER) {
  8797. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8798. sd->pvp_timer = INVALID_TIMER;
  8799. }
  8800. return 0;
  8801. }
  8802. BUILDIN_FUNC(pvpoff)
  8803. {
  8804. int m;
  8805. const char *str;
  8806. str=script_getstr(st,2);
  8807. m = map_mapname2mapid(str);
  8808. if(m < 0 || !map[m].flag.pvp)
  8809. return 0; //fixed Lupus
  8810. map[m].flag.pvp = 0;
  8811. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8812. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8813. return 0;
  8814. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8815. return 0;
  8816. }
  8817. BUILDIN_FUNC(gvgon)
  8818. {
  8819. int m;
  8820. const char *str;
  8821. str=script_getstr(st,2);
  8822. m = map_mapname2mapid(str);
  8823. if(m >= 0 && !map[m].flag.gvg) {
  8824. map[m].flag.gvg = 1;
  8825. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  8826. }
  8827. return 0;
  8828. }
  8829. BUILDIN_FUNC(gvgoff)
  8830. {
  8831. int m;
  8832. const char *str;
  8833. str=script_getstr(st,2);
  8834. m = map_mapname2mapid(str);
  8835. if(m >= 0 && map[m].flag.gvg) {
  8836. map[m].flag.gvg = 0;
  8837. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8838. }
  8839. return 0;
  8840. }
  8841. /*==========================================
  8842. * Shows an emoticon on top of the player/npc
  8843. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  8844. *------------------------------------------*/
  8845. //Optional second parameter added by [Skotlex]
  8846. BUILDIN_FUNC(emotion)
  8847. {
  8848. int type;
  8849. int player=0;
  8850. type=script_getnum(st,2);
  8851. if(type < 0 || type > 100)
  8852. return 0;
  8853. if( script_hasdata(st,3) )
  8854. player=script_getnum(st,3);
  8855. if (player) {
  8856. TBL_PC *sd = NULL;
  8857. if( script_hasdata(st,4) )
  8858. sd = map_nick2sd(script_getstr(st,4));
  8859. else
  8860. sd = script_rid2sd(st);
  8861. if (sd)
  8862. clif_emotion(&sd->bl,type);
  8863. } else
  8864. if( script_hasdata(st,4) )
  8865. {
  8866. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  8867. if(nd)
  8868. clif_emotion(&nd->bl,type);
  8869. }
  8870. else
  8871. clif_emotion(map_id2bl(st->oid),type);
  8872. return 0;
  8873. }
  8874. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8875. {
  8876. int m=va_arg(ap,int);
  8877. int g_id=va_arg(ap,int);
  8878. int flag=va_arg(ap,int);
  8879. if(!sd || sd->bl.m != m)
  8880. return 0;
  8881. if(
  8882. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8883. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8884. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8885. )
  8886. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  8887. return 1;
  8888. }
  8889. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8890. {
  8891. struct mob_data *md=(struct mob_data *)bl;
  8892. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8893. status_kill(bl);
  8894. return 0;
  8895. }
  8896. BUILDIN_FUNC(maprespawnguildid)
  8897. {
  8898. const char *mapname=script_getstr(st,2);
  8899. int g_id=script_getnum(st,3);
  8900. int flag=script_getnum(st,4);
  8901. int m=map_mapname2mapid(mapname);
  8902. if(m == -1)
  8903. return 0;
  8904. //Catch ALL players (in case some are 'between maps' on execution time)
  8905. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8906. if (flag&4) //Remove script mobs.
  8907. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8908. return 0;
  8909. }
  8910. BUILDIN_FUNC(agitstart)
  8911. {
  8912. if(agit_flag==1) return 0; // Agit already Start.
  8913. agit_flag=1;
  8914. guild_agit_start();
  8915. return 0;
  8916. }
  8917. BUILDIN_FUNC(agitend)
  8918. {
  8919. if(agit_flag==0) return 0; // Agit already End.
  8920. agit_flag=0;
  8921. guild_agit_end();
  8922. return 0;
  8923. }
  8924. BUILDIN_FUNC(agitstart2)
  8925. {
  8926. if(agit2_flag==1) return 0; // Agit2 already Start.
  8927. agit2_flag=1;
  8928. guild_agit2_start();
  8929. return 0;
  8930. }
  8931. BUILDIN_FUNC(agitend2)
  8932. {
  8933. if(agit2_flag==0) return 0; // Agit2 already End.
  8934. agit2_flag=0;
  8935. guild_agit2_end();
  8936. return 0;
  8937. }
  8938. /*==========================================
  8939. * Returns whether woe is on or off. // choice script
  8940. *------------------------------------------*/
  8941. BUILDIN_FUNC(agitcheck)
  8942. {
  8943. script_pushint(st,agit_flag);
  8944. return 0;
  8945. }
  8946. /*==========================================
  8947. * Returns whether woese is on or off. // choice script
  8948. *------------------------------------------*/
  8949. BUILDIN_FUNC(agitcheck2)
  8950. {
  8951. script_pushint(st,agit2_flag);
  8952. return 0;
  8953. }
  8954. /// Sets the guild_id of this npc.
  8955. ///
  8956. /// flagemblem <guild_id>;
  8957. BUILDIN_FUNC(flagemblem)
  8958. {
  8959. TBL_NPC* nd;
  8960. int g_id=script_getnum(st,2);
  8961. if(g_id < 0) return 0;
  8962. nd = (TBL_NPC*)map_id2nd(st->oid);
  8963. if( nd == NULL )
  8964. {
  8965. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8966. }
  8967. else if( nd->subtype != SCRIPT )
  8968. {
  8969. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8970. }
  8971. else
  8972. {
  8973. nd->u.scr.guild_id = g_id;
  8974. clif_guild_emblem_area(&nd->bl);
  8975. }
  8976. return 0;
  8977. }
  8978. BUILDIN_FUNC(getcastlename)
  8979. {
  8980. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8981. struct guild_castle* gc = guild_mapname2gc(mapname);
  8982. const char* name = (gc) ? gc->castle_name : "";
  8983. script_pushstrcopy(st,name);
  8984. return 0;
  8985. }
  8986. BUILDIN_FUNC(getcastledata)
  8987. {
  8988. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8989. int index = script_getnum(st,3);
  8990. struct guild_castle* gc = guild_mapname2gc(mapname);
  8991. if(script_hasdata(st,4) && index==0 && gc) {
  8992. const char* event = script_getstr(st,4);
  8993. check_event(st, event);
  8994. guild_addcastleinfoevent(gc->castle_id,17,event);
  8995. }
  8996. if(gc){
  8997. switch(index){
  8998. case 0: {
  8999. int i;
  9000. for(i=1;i<18;i++) // Initialize[AgitInit]
  9001. guild_castledataload(gc->castle_id,i);
  9002. } break;
  9003. case 1:
  9004. script_pushint(st,gc->guild_id); break;
  9005. case 2:
  9006. script_pushint(st,gc->economy); break;
  9007. case 3:
  9008. script_pushint(st,gc->defense); break;
  9009. case 4:
  9010. script_pushint(st,gc->triggerE); break;
  9011. case 5:
  9012. script_pushint(st,gc->triggerD); break;
  9013. case 6:
  9014. script_pushint(st,gc->nextTime); break;
  9015. case 7:
  9016. script_pushint(st,gc->payTime); break;
  9017. case 8:
  9018. script_pushint(st,gc->createTime); break;
  9019. case 9:
  9020. script_pushint(st,gc->visibleC); break;
  9021. case 10:
  9022. case 11:
  9023. case 12:
  9024. case 13:
  9025. case 14:
  9026. case 15:
  9027. case 16:
  9028. case 17:
  9029. script_pushint(st,gc->guardian[index-10].visible); break;
  9030. default:
  9031. script_pushint(st,0); break;
  9032. }
  9033. return 0;
  9034. }
  9035. script_pushint(st,0);
  9036. return 0;
  9037. }
  9038. BUILDIN_FUNC(setcastledata)
  9039. {
  9040. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9041. int index = script_getnum(st,3);
  9042. int value = script_getnum(st,4);
  9043. struct guild_castle* gc = guild_mapname2gc(mapname);
  9044. if(gc) {
  9045. // Save Data byself First
  9046. switch(index){
  9047. case 1:
  9048. gc->guild_id = value; break;
  9049. case 2:
  9050. gc->economy = value; break;
  9051. case 3:
  9052. gc->defense = value; break;
  9053. case 4:
  9054. gc->triggerE = value; break;
  9055. case 5:
  9056. gc->triggerD = value; break;
  9057. case 6:
  9058. gc->nextTime = value; break;
  9059. case 7:
  9060. gc->payTime = value; break;
  9061. case 8:
  9062. gc->createTime = value; break;
  9063. case 9:
  9064. gc->visibleC = value; break;
  9065. case 10:
  9066. case 11:
  9067. case 12:
  9068. case 13:
  9069. case 14:
  9070. case 15:
  9071. case 16:
  9072. case 17:
  9073. gc->guardian[index-10].visible = value; break;
  9074. default:
  9075. return 0;
  9076. }
  9077. guild_castledatasave(gc->castle_id,index,value);
  9078. }
  9079. return 0;
  9080. }
  9081. /* =====================================================================
  9082. * ギルド情報を要求する
  9083. * ---------------------------------------------------------------------*/
  9084. BUILDIN_FUNC(requestguildinfo)
  9085. {
  9086. int guild_id=script_getnum(st,2);
  9087. const char *event=NULL;
  9088. if( script_hasdata(st,3) ){
  9089. event=script_getstr(st,3);
  9090. check_event(st, event);
  9091. }
  9092. if(guild_id>0)
  9093. guild_npc_request_info(guild_id,event);
  9094. return 0;
  9095. }
  9096. /// Returns the number of cards that have been compounded onto the specified equipped item.
  9097. /// getequipcardcnt(<equipment slot>);
  9098. BUILDIN_FUNC(getequipcardcnt)
  9099. {
  9100. int i=-1,j,num;
  9101. TBL_PC *sd;
  9102. int count;
  9103. num=script_getnum(st,2);
  9104. sd=script_rid2sd(st);
  9105. if (num > 0 && num <= ARRAYLENGTH(equip))
  9106. i=pc_checkequip(sd,equip[num-1]);
  9107. if (i < 0 || !sd->inventory_data[i]) {
  9108. script_pushint(st,0);
  9109. return 0;
  9110. }
  9111. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9112. {
  9113. script_pushint(st,0);
  9114. return 0;
  9115. }
  9116. count = 0;
  9117. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  9118. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  9119. count++;
  9120. script_pushint(st,count);
  9121. return 0;
  9122. }
  9123. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  9124. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  9125. /// successremovecards <slot>;
  9126. BUILDIN_FUNC(successremovecards)
  9127. {
  9128. int i=-1,j,c,cardflag=0;
  9129. TBL_PC* sd = script_rid2sd(st);
  9130. int num = script_getnum(st,2);
  9131. if (num > 0 && num <= ARRAYLENGTH(equip))
  9132. i=pc_checkequip(sd,equip[num-1]);
  9133. if (i < 0 || !sd->inventory_data[i]) {
  9134. return 0;
  9135. }
  9136. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9137. return 0;
  9138. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9139. {
  9140. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9141. {// extract this card from the item
  9142. int flag;
  9143. struct item item_tmp;
  9144. cardflag = 1;
  9145. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9146. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9147. item_tmp.attribute=0,item_tmp.expire_time=0;
  9148. for (j = 0; j < MAX_SLOTS; j++)
  9149. item_tmp.card[j]=0;
  9150. //Logs items, got from (N)PC scripts [Lupus]
  9151. if(log_config.enable_logs&0x40)
  9152. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  9153. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  9154. clif_additem(sd,0,0,flag);
  9155. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9156. }
  9157. }
  9158. }
  9159. if(cardflag == 1)
  9160. { // カードを取り除いたアイテム所得
  9161. int flag;
  9162. struct item item_tmp;
  9163. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9164. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9165. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9166. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9167. item_tmp.card[j]=0;
  9168. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9169. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9170. //Logs items, got from (N)PC scripts [Lupus]
  9171. if(log_config.enable_logs&0x40)
  9172. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9173. pc_delitem(sd,i,1,0,3);
  9174. //Logs items, got from (N)PC scripts [Lupus]
  9175. if(log_config.enable_logs&0x40)
  9176. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  9177. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  9178. clif_additem(sd,0,0,flag);
  9179. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9180. }
  9181. clif_misceffect(&sd->bl,3);
  9182. }
  9183. return 0;
  9184. }
  9185. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  9186. /// failedremovecards <slot>, <type>;
  9187. /// <type>=0 : will destroy both the item and the cards.
  9188. /// <type>=1 : will keep the item, but destroy the cards.
  9189. /// <type>=2 : will keep the cards, but destroy the item.
  9190. /// <type>=? : will just display the failure effect.
  9191. BUILDIN_FUNC(failedremovecards)
  9192. {
  9193. int i=-1,j,c,cardflag=0;
  9194. TBL_PC* sd = script_rid2sd(st);
  9195. int num = script_getnum(st,2);
  9196. int typefail = script_getnum(st,3);
  9197. if (num > 0 && num <= ARRAYLENGTH(equip))
  9198. i=pc_checkequip(sd,equip[num-1]);
  9199. if (i < 0 || !sd->inventory_data[i])
  9200. return 0;
  9201. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9202. return 0;
  9203. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9204. {
  9205. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9206. {
  9207. cardflag = 1;
  9208. if(typefail == 2)
  9209. {// add cards to inventory, clear
  9210. int flag;
  9211. struct item item_tmp;
  9212. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9213. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9214. item_tmp.attribute=0,item_tmp.expire_time=0;
  9215. for (j = 0; j < MAX_SLOTS; j++)
  9216. item_tmp.card[j]=0;
  9217. //Logs items, got from (N)PC scripts [Lupus]
  9218. if(log_config.enable_logs&0x40)
  9219. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  9220. if((flag=pc_additem(sd,&item_tmp,1))){
  9221. clif_additem(sd,0,0,flag);
  9222. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9223. }
  9224. }
  9225. }
  9226. }
  9227. if(cardflag == 1)
  9228. {
  9229. if(typefail == 0 || typefail == 2){ // 武具損失
  9230. //Logs items, got from (N)PC scripts [Lupus]
  9231. if(log_config.enable_logs&0x40)
  9232. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9233. pc_delitem(sd,i,1,0,2);
  9234. }
  9235. if(typefail == 1){ // カードのみ損失(武具を返す)
  9236. int flag;
  9237. struct item item_tmp;
  9238. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9239. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9240. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9241. //Logs items, got from (N)PC scripts [Lupus]
  9242. if(log_config.enable_logs&0x40)
  9243. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9244. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9245. item_tmp.card[j]=0;
  9246. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9247. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9248. pc_delitem(sd,i,1,0,2);
  9249. //Logs items, got from (N)PC scripts [Lupus]
  9250. if(log_config.enable_logs&0x40)
  9251. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  9252. if((flag=pc_additem(sd,&item_tmp,1))){
  9253. clif_additem(sd,0,0,flag);
  9254. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9255. }
  9256. }
  9257. clif_misceffect(&sd->bl,2);
  9258. }
  9259. return 0;
  9260. }
  9261. /* ================================================================
  9262. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  9263. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  9264. * improved by [Lance]
  9265. * ================================================================*/
  9266. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  9267. {
  9268. int x,y,m,check_val=0,check_ID=0,i=0;
  9269. struct guild *g = NULL;
  9270. struct party_data *p = NULL;
  9271. const char *str;
  9272. const char *mapname;
  9273. unsigned int index;
  9274. mapname=script_getstr(st,2);
  9275. str=script_getstr(st,3);
  9276. x=script_getnum(st,4);
  9277. y=script_getnum(st,5);
  9278. if(script_hasdata(st,7)){
  9279. check_val=script_getnum(st,6);
  9280. check_ID=script_getnum(st,7);
  9281. }
  9282. if((m=map_mapname2mapid(mapname))< 0)
  9283. return 0;
  9284. if(!(index=mapindex_name2id(str)))
  9285. return 0;
  9286. switch(check_val){
  9287. case 1:
  9288. g = guild_search(check_ID);
  9289. if (g){
  9290. for( i=0; i < g->max_member; i++)
  9291. {
  9292. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9293. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  9294. }
  9295. }
  9296. }
  9297. break;
  9298. case 2:
  9299. p = party_search(check_ID);
  9300. if(p){
  9301. for(i=0;i<MAX_PARTY; i++){
  9302. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9303. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  9304. }
  9305. }
  9306. }
  9307. break;
  9308. default:
  9309. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  9310. break;
  9311. }
  9312. return 0;
  9313. }
  9314. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9315. {
  9316. char *event=va_arg(ap,char *);
  9317. struct mob_data *md = ((struct mob_data *)bl);
  9318. if(strcmp(event,md->npc_event)==0 && md->status.hp > 0)
  9319. return 1;
  9320. return 0;
  9321. }
  9322. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9323. {
  9324. const char *mapname,*event;
  9325. int m;
  9326. mapname=script_getstr(st,2);
  9327. event=script_getstr(st,3);
  9328. check_event(st, event);
  9329. if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9330. script_pushint(st,-1);
  9331. return 0;
  9332. }
  9333. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9334. {
  9335. script_pushint(st,-1);
  9336. return 0;
  9337. }
  9338. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9339. return 0;
  9340. }
  9341. BUILDIN_FUNC(marriage)
  9342. {
  9343. const char *partner=script_getstr(st,2);
  9344. TBL_PC *sd=script_rid2sd(st);
  9345. TBL_PC *p_sd=map_nick2sd(partner);
  9346. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9347. script_pushint(st,0);
  9348. return 0;
  9349. }
  9350. script_pushint(st,1);
  9351. return 0;
  9352. }
  9353. BUILDIN_FUNC(wedding_effect)
  9354. {
  9355. TBL_PC *sd=script_rid2sd(st);
  9356. struct block_list *bl;
  9357. if(sd==NULL) {
  9358. bl=map_id2bl(st->oid);
  9359. } else
  9360. bl=&sd->bl;
  9361. clif_wedding_effect(bl);
  9362. return 0;
  9363. }
  9364. BUILDIN_FUNC(divorce)
  9365. {
  9366. TBL_PC *sd=script_rid2sd(st);
  9367. if(sd==NULL || pc_divorce(sd) < 0){
  9368. script_pushint(st,0);
  9369. return 0;
  9370. }
  9371. script_pushint(st,1);
  9372. return 0;
  9373. }
  9374. BUILDIN_FUNC(ispartneron)
  9375. {
  9376. TBL_PC *sd=script_rid2sd(st);
  9377. if(sd==NULL || !pc_ismarried(sd) ||
  9378. map_charid2sd(sd->status.partner_id) == NULL) {
  9379. script_pushint(st,0);
  9380. return 0;
  9381. }
  9382. script_pushint(st,1);
  9383. return 0;
  9384. }
  9385. BUILDIN_FUNC(getpartnerid)
  9386. {
  9387. TBL_PC *sd=script_rid2sd(st);
  9388. if (sd == NULL) {
  9389. script_pushint(st,0);
  9390. return 0;
  9391. }
  9392. script_pushint(st,sd->status.partner_id);
  9393. return 0;
  9394. }
  9395. BUILDIN_FUNC(getchildid)
  9396. {
  9397. TBL_PC *sd=script_rid2sd(st);
  9398. if (sd == NULL) {
  9399. script_pushint(st,0);
  9400. return 0;
  9401. }
  9402. script_pushint(st,sd->status.child);
  9403. return 0;
  9404. }
  9405. BUILDIN_FUNC(getmotherid)
  9406. {
  9407. TBL_PC *sd=script_rid2sd(st);
  9408. if (sd == NULL) {
  9409. script_pushint(st,0);
  9410. return 0;
  9411. }
  9412. script_pushint(st,sd->status.mother);
  9413. return 0;
  9414. }
  9415. BUILDIN_FUNC(getfatherid)
  9416. {
  9417. TBL_PC *sd=script_rid2sd(st);
  9418. if (sd == NULL) {
  9419. script_pushint(st,0);
  9420. return 0;
  9421. }
  9422. script_pushint(st,sd->status.father);
  9423. return 0;
  9424. }
  9425. BUILDIN_FUNC(warppartner)
  9426. {
  9427. int x,y;
  9428. unsigned short mapindex;
  9429. const char *str;
  9430. TBL_PC *sd=script_rid2sd(st);
  9431. TBL_PC *p_sd=NULL;
  9432. if(sd==NULL || !pc_ismarried(sd) ||
  9433. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9434. script_pushint(st,0);
  9435. return 0;
  9436. }
  9437. str=script_getstr(st,2);
  9438. x=script_getnum(st,3);
  9439. y=script_getnum(st,4);
  9440. mapindex = mapindex_name2id(str);
  9441. if (mapindex) {
  9442. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  9443. script_pushint(st,1);
  9444. } else
  9445. script_pushint(st,0);
  9446. return 0;
  9447. }
  9448. /*================================================
  9449. * Script for Displaying MOB Information [Valaris]
  9450. *------------------------------------------------*/
  9451. BUILDIN_FUNC(strmobinfo)
  9452. {
  9453. int num=script_getnum(st,2);
  9454. int class_=script_getnum(st,3);
  9455. if(!mobdb_checkid(class_))
  9456. {
  9457. script_pushint(st,0);
  9458. return 0;
  9459. }
  9460. switch (num) {
  9461. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9462. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9463. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9464. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9465. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9466. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9467. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9468. default:
  9469. script_pushint(st,0);
  9470. break;
  9471. }
  9472. return 0;
  9473. }
  9474. /*==========================================
  9475. * Summon guardians [Valaris]
  9476. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9477. *------------------------------------------*/
  9478. BUILDIN_FUNC(guardian)
  9479. {
  9480. int class_=0,x=0,y=0,guardian=0;
  9481. const char *str,*map,*evt="";
  9482. struct script_data *data;
  9483. bool has_index = false;
  9484. map =script_getstr(st,2);
  9485. x =script_getnum(st,3);
  9486. y =script_getnum(st,4);
  9487. str =script_getstr(st,5);
  9488. class_=script_getnum(st,6);
  9489. if( script_hasdata(st,8) )
  9490. {// "<event label>",<guardian index>
  9491. evt=script_getstr(st,7);
  9492. guardian=script_getnum(st,8);
  9493. has_index = true;
  9494. } else if( script_hasdata(st,7) ){
  9495. data=script_getdata(st,7);
  9496. get_val(st,data);
  9497. if( data_isstring(data) )
  9498. {// "<event label>"
  9499. evt=script_getstr(st,7);
  9500. } else if( data_isint(data) )
  9501. {// <guardian index>
  9502. guardian=script_getnum(st,7);
  9503. has_index = true;
  9504. } else {
  9505. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9506. script_reportdata(data);
  9507. return 1;
  9508. }
  9509. }
  9510. check_event(st, evt);
  9511. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9512. return 0;
  9513. }
  9514. /*==========================================
  9515. * Invisible Walls [Zephyrus]
  9516. *------------------------------------------*/
  9517. BUILDIN_FUNC(setwall)
  9518. {
  9519. const char *map, *name;
  9520. int x, y, m, size, dir;
  9521. bool shootable;
  9522. map = script_getstr(st,2);
  9523. x = script_getnum(st,3);
  9524. y = script_getnum(st,4);
  9525. size = script_getnum(st,5);
  9526. dir = script_getnum(st,6);
  9527. shootable = script_getnum(st,7);
  9528. name = script_getstr(st,8);
  9529. if( (m = map_mapname2mapid(map)) < 0 )
  9530. return 0; // Invalid Map
  9531. map_iwall_set(m, x, y, size, dir, shootable, name);
  9532. return 0;
  9533. }
  9534. BUILDIN_FUNC(delwall)
  9535. {
  9536. const char *name = script_getstr(st,2);
  9537. map_iwall_remove(name);
  9538. return 0;
  9539. }
  9540. /// Retrieves various information about the specified guardian.
  9541. ///
  9542. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9543. /// type: 0 - whether it is deployed or not
  9544. /// 1 - maximum hp
  9545. /// 2 - current hp
  9546. ///
  9547. BUILDIN_FUNC(guardianinfo)
  9548. {
  9549. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9550. int id = script_getnum(st,3);
  9551. int type = script_getnum(st,4);
  9552. struct guild_castle* gc = guild_mapname2gc(mapname);
  9553. struct mob_data* gd;
  9554. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9555. {
  9556. script_pushint(st,-1);
  9557. return 0;
  9558. }
  9559. if( type == 0 )
  9560. script_pushint(st, gc->guardian[id].visible);
  9561. else
  9562. if( !gc->guardian[id].visible )
  9563. script_pushint(st,-1);
  9564. else
  9565. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9566. script_pushint(st,-1);
  9567. else
  9568. {
  9569. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9570. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9571. else
  9572. script_pushint(st,-1);
  9573. }
  9574. return 0;
  9575. }
  9576. /*==========================================
  9577. * IDからItem名
  9578. *------------------------------------------*/
  9579. BUILDIN_FUNC(getitemname)
  9580. {
  9581. int item_id=0;
  9582. struct item_data *i_data;
  9583. char *item_name;
  9584. struct script_data *data;
  9585. data=script_getdata(st,2);
  9586. get_val(st,data);
  9587. if( data_isstring(data) ){
  9588. const char *name=conv_str(st,data);
  9589. struct item_data *item_data = itemdb_searchname(name);
  9590. if( item_data )
  9591. item_id=item_data->nameid;
  9592. }else
  9593. item_id=conv_num(st,data);
  9594. i_data = itemdb_exists(item_id);
  9595. if (i_data == NULL)
  9596. {
  9597. script_pushconststr(st,"null");
  9598. return 0;
  9599. }
  9600. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9601. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9602. script_pushstr(st,item_name);
  9603. return 0;
  9604. }
  9605. /*==========================================
  9606. * Returns number of slots an item has. [Skotlex]
  9607. *------------------------------------------*/
  9608. BUILDIN_FUNC(getitemslots)
  9609. {
  9610. int item_id;
  9611. struct item_data *i_data;
  9612. item_id=script_getnum(st,2);
  9613. i_data = itemdb_exists(item_id);
  9614. if (i_data)
  9615. script_pushint(st,i_data->slot);
  9616. else
  9617. script_pushint(st,-1);
  9618. return 0;
  9619. }
  9620. /*==========================================
  9621. * Returns some values of an item [Lupus]
  9622. * Price, Weight, etc...
  9623. getiteminfo(itemID,n), where n
  9624. 0 value_buy;
  9625. 1 value_sell;
  9626. 2 type;
  9627. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9628. if = 0, then monsters don't drop it at all (rare or a quest item)
  9629. if = -1, then this item is sold in NPC shops only
  9630. 4 sex;
  9631. 5 equip;
  9632. 6 weight;
  9633. 7 atk;
  9634. 8 def;
  9635. 9 range;
  9636. 10 slot;
  9637. 11 look;
  9638. 12 elv;
  9639. 13 wlv;
  9640. 14 view id
  9641. *------------------------------------------*/
  9642. BUILDIN_FUNC(getiteminfo)
  9643. {
  9644. int item_id,n;
  9645. int *item_arr;
  9646. struct item_data *i_data;
  9647. item_id = script_getnum(st,2);
  9648. n = script_getnum(st,3);
  9649. i_data = itemdb_exists(item_id);
  9650. if (i_data && n>=0 && n<=14) {
  9651. item_arr = (int*)&i_data->value_buy;
  9652. script_pushint(st,item_arr[n]);
  9653. } else
  9654. script_pushint(st,-1);
  9655. return 0;
  9656. }
  9657. /*==========================================
  9658. * Set some values of an item [Lupus]
  9659. * Price, Weight, etc...
  9660. setiteminfo(itemID,n,Value), where n
  9661. 0 value_buy;
  9662. 1 value_sell;
  9663. 2 type;
  9664. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9665. if = 0, then monsters don't drop it at all (rare or a quest item)
  9666. if = -1, then this item is sold in NPC shops only
  9667. 4 sex;
  9668. 5 equip;
  9669. 6 weight;
  9670. 7 atk;
  9671. 8 def;
  9672. 9 range;
  9673. 10 slot;
  9674. 11 look;
  9675. 12 elv;
  9676. 13 wlv;
  9677. 14 view id
  9678. * Returns Value or -1 if the wrong field's been set
  9679. *------------------------------------------*/
  9680. BUILDIN_FUNC(setiteminfo)
  9681. {
  9682. int item_id,n,value;
  9683. int *item_arr;
  9684. struct item_data *i_data;
  9685. item_id = script_getnum(st,2);
  9686. n = script_getnum(st,3);
  9687. value = script_getnum(st,4);
  9688. i_data = itemdb_exists(item_id);
  9689. if (i_data && n>=0 && n<=14) {
  9690. item_arr = (int*)&i_data->value_buy;
  9691. item_arr[n] = value;
  9692. script_pushint(st,value);
  9693. } else
  9694. script_pushint(st,-1);
  9695. return 0;
  9696. }
  9697. /*==========================================
  9698. * Returns value from equipped item slot n [Lupus]
  9699. getequipcardid(num,slot)
  9700. where
  9701. num = eqip position slot
  9702. slot = 0,1,2,3 (Card Slot N)
  9703. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9704. it's useful when you want to check item cards or if it's signed
  9705. Useful for such quests as "Sign this refined item with players name" etc
  9706. Hat[0] +4 -> Player's Hat[0] +4
  9707. *------------------------------------------*/
  9708. BUILDIN_FUNC(getequipcardid)
  9709. {
  9710. int i=-1,num,slot;
  9711. TBL_PC *sd;
  9712. num=script_getnum(st,2);
  9713. slot=script_getnum(st,3);
  9714. sd=script_rid2sd(st);
  9715. if (num > 0 && num <= ARRAYLENGTH(equip))
  9716. i=pc_checkequip(sd,equip[num-1]);
  9717. if(i >= 0 && slot>=0 && slot<4)
  9718. script_pushint(st,sd->status.inventory[i].card[slot]);
  9719. else
  9720. script_pushint(st,0);
  9721. return 0;
  9722. }
  9723. /*==========================================
  9724. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9725. *------------------------------------------*/
  9726. BUILDIN_FUNC(petskillbonus)
  9727. {
  9728. struct pet_data *pd;
  9729. TBL_PC *sd=script_rid2sd(st);
  9730. if(sd==NULL || sd->pd==NULL)
  9731. return 0;
  9732. pd=sd->pd;
  9733. if (pd->bonus)
  9734. { //Clear previous bonus
  9735. if (pd->bonus->timer != INVALID_TIMER)
  9736. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9737. } else //init
  9738. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9739. pd->bonus->type=script_getnum(st,2);
  9740. pd->bonus->val=script_getnum(st,3);
  9741. pd->bonus->duration=script_getnum(st,4);
  9742. pd->bonus->delay=script_getnum(st,5);
  9743. if (pd->state.skillbonus == 1)
  9744. pd->state.skillbonus=0; // waiting state
  9745. // wait for timer to start
  9746. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9747. pd->bonus->timer = INVALID_TIMER;
  9748. else
  9749. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9750. return 0;
  9751. }
  9752. /*==========================================
  9753. * pet looting [Valaris] //Rewritten by [Skotlex]
  9754. *------------------------------------------*/
  9755. BUILDIN_FUNC(petloot)
  9756. {
  9757. int max;
  9758. struct pet_data *pd;
  9759. TBL_PC *sd=script_rid2sd(st);
  9760. if(sd==NULL || sd->pd==NULL)
  9761. return 0;
  9762. max=script_getnum(st,2);
  9763. if(max < 1)
  9764. max = 1; //Let'em loot at least 1 item.
  9765. else if (max > MAX_PETLOOT_SIZE)
  9766. max = MAX_PETLOOT_SIZE;
  9767. pd = sd->pd;
  9768. if (pd->loot != NULL)
  9769. { //Release whatever was there already and reallocate memory
  9770. pet_lootitem_drop(pd, pd->msd);
  9771. aFree(pd->loot->item);
  9772. }
  9773. else
  9774. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9775. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9776. pd->loot->max=max;
  9777. pd->loot->count = 0;
  9778. pd->loot->weight = 0;
  9779. return 0;
  9780. }
  9781. /*==========================================
  9782. * PCの所持品情報読み取り
  9783. *------------------------------------------*/
  9784. BUILDIN_FUNC(getinventorylist)
  9785. {
  9786. TBL_PC *sd=script_rid2sd(st);
  9787. char card_var[NAME_LENGTH];
  9788. int i,j=0,k;
  9789. if(!sd) return 0;
  9790. for(i=0;i<MAX_INVENTORY;i++){
  9791. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9792. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  9793. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  9794. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  9795. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  9796. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  9797. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  9798. for (k = 0; k < MAX_SLOTS; k++)
  9799. {
  9800. sprintf(card_var, "@inventorylist_card%d",k+1);
  9801. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  9802. }
  9803. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  9804. j++;
  9805. }
  9806. }
  9807. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9808. return 0;
  9809. }
  9810. BUILDIN_FUNC(getskilllist)
  9811. {
  9812. TBL_PC *sd=script_rid2sd(st);
  9813. int i,j=0;
  9814. if(!sd) return 0;
  9815. for(i=0;i<MAX_SKILL;i++){
  9816. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9817. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  9818. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  9819. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  9820. j++;
  9821. }
  9822. }
  9823. pc_setreg(sd,add_str("@skilllist_count"),j);
  9824. return 0;
  9825. }
  9826. BUILDIN_FUNC(clearitem)
  9827. {
  9828. TBL_PC *sd=script_rid2sd(st);
  9829. int i;
  9830. if(sd==NULL) return 0;
  9831. for (i=0; i<MAX_INVENTORY; i++) {
  9832. if (sd->status.inventory[i].amount) {
  9833. //Logs items, got from (N)PC scripts [Lupus]
  9834. if(log_config.enable_logs&0x40)
  9835. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9836. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0);
  9837. }
  9838. }
  9839. return 0;
  9840. }
  9841. /*==========================================
  9842. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9843. *------------------------------------------*/
  9844. BUILDIN_FUNC(disguise)
  9845. {
  9846. int id;
  9847. TBL_PC* sd = script_rid2sd(st);
  9848. if (sd == NULL) return 0;
  9849. id = script_getnum(st,2);
  9850. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9851. pc_disguise(sd, id);
  9852. script_pushint(st,id);
  9853. } else
  9854. script_pushint(st,0);
  9855. return 0;
  9856. }
  9857. /*==========================================
  9858. * Undisguise Player (returns 1 if success, 0 on fail)
  9859. *------------------------------------------*/
  9860. BUILDIN_FUNC(undisguise)
  9861. {
  9862. TBL_PC* sd = script_rid2sd(st);
  9863. if (sd == NULL) return 0;
  9864. if (sd->disguise) {
  9865. pc_disguise(sd, 0);
  9866. script_pushint(st,0);
  9867. } else {
  9868. script_pushint(st,1);
  9869. }
  9870. return 0;
  9871. }
  9872. /*==========================================
  9873. * NPCクラスチェンジ
  9874. * classは変わりたいclass
  9875. * typeは通常0なのかな?
  9876. *------------------------------------------*/
  9877. BUILDIN_FUNC(classchange)
  9878. {
  9879. int _class,type;
  9880. struct block_list *bl=map_id2bl(st->oid);
  9881. if(bl==NULL) return 0;
  9882. _class=script_getnum(st,2);
  9883. type=script_getnum(st,3);
  9884. clif_class_change(bl,_class,type);
  9885. return 0;
  9886. }
  9887. /*==========================================
  9888. * NPCから発生するエフェクト
  9889. *------------------------------------------*/
  9890. BUILDIN_FUNC(misceffect)
  9891. {
  9892. int type;
  9893. type=script_getnum(st,2);
  9894. if(st->oid && st->oid != fake_nd->bl.id) {
  9895. struct block_list *bl = map_id2bl(st->oid);
  9896. if (bl)
  9897. clif_specialeffect(bl,type,AREA);
  9898. } else{
  9899. TBL_PC *sd=script_rid2sd(st);
  9900. if(sd)
  9901. clif_specialeffect(&sd->bl,type,AREA);
  9902. }
  9903. return 0;
  9904. }
  9905. /*==========================================
  9906. * Play a BGM on a single client [Rikter/Yommy]
  9907. *------------------------------------------*/
  9908. BUILDIN_FUNC(playBGM)
  9909. {
  9910. const char* name;
  9911. struct map_session_data* sd;
  9912. if( ( sd = script_rid2sd(st) ) != NULL )
  9913. {
  9914. name = script_getstr(st,2);
  9915. clif_playBGM(sd, name);
  9916. }
  9917. return 0;
  9918. }
  9919. static int playBGM_sub(struct block_list* bl,va_list ap)
  9920. {
  9921. const char* name = va_arg(ap,const char*);
  9922. clif_playBGM(BL_CAST(BL_PC, bl), name);
  9923. return 0;
  9924. }
  9925. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  9926. {
  9927. const char* name = va_arg(args, const char*);
  9928. clif_playBGM(sd, name);
  9929. return 0;
  9930. }
  9931. /*==========================================
  9932. * Play a BGM on multiple client [Rikter/Yommy]
  9933. *------------------------------------------*/
  9934. BUILDIN_FUNC(playBGMall)
  9935. {
  9936. const char* name;
  9937. name = script_getstr(st,2);
  9938. if( script_hasdata(st,7) )
  9939. {// specified part of map
  9940. const char* map = script_getstr(st,3);
  9941. int x0 = script_getnum(st,4);
  9942. int y0 = script_getnum(st,5);
  9943. int x1 = script_getnum(st,6);
  9944. int y1 = script_getnum(st,7);
  9945. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  9946. }
  9947. else if( script_hasdata(st,3) )
  9948. {// entire map
  9949. const char* map = script_getstr(st,3);
  9950. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  9951. }
  9952. else
  9953. {// entire server
  9954. map_foreachpc(&playBGM_foreachpc_sub, name);
  9955. }
  9956. return 0;
  9957. }
  9958. /*==========================================
  9959. * サウンドエフェクト
  9960. *------------------------------------------*/
  9961. BUILDIN_FUNC(soundeffect)
  9962. {
  9963. TBL_PC* sd = script_rid2sd(st);
  9964. const char* name = script_getstr(st,2);
  9965. int type = script_getnum(st,3);
  9966. if(sd)
  9967. {
  9968. clif_soundeffect(sd,&sd->bl,name,type);
  9969. }
  9970. return 0;
  9971. }
  9972. int soundeffect_sub(struct block_list* bl,va_list ap)
  9973. {
  9974. char* name = va_arg(ap,char*);
  9975. int type = va_arg(ap,int);
  9976. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9977. return 0;
  9978. }
  9979. /*==========================================
  9980. * Play a sound effect (.wav) on multiple clients
  9981. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9982. *------------------------------------------*/
  9983. BUILDIN_FUNC(soundeffectall)
  9984. {
  9985. struct block_list* bl;
  9986. const char* name;
  9987. int type;
  9988. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9989. if (!bl)
  9990. return 0;
  9991. name = script_getstr(st,2);
  9992. type = script_getnum(st,3);
  9993. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9994. if(!script_hasdata(st,4))
  9995. { // area around
  9996. clif_soundeffectall(bl, name, type, AREA);
  9997. }
  9998. else
  9999. if(!script_hasdata(st,5))
  10000. { // entire map
  10001. const char* map = script_getstr(st,4);
  10002. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  10003. }
  10004. else
  10005. if(script_hasdata(st,8))
  10006. { // specified part of map
  10007. const char* map = script_getstr(st,4);
  10008. int x0 = script_getnum(st,5);
  10009. int y0 = script_getnum(st,6);
  10010. int x1 = script_getnum(st,7);
  10011. int y1 = script_getnum(st,8);
  10012. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  10013. }
  10014. else
  10015. {
  10016. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  10017. }
  10018. return 0;
  10019. }
  10020. /*==========================================
  10021. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  10022. *------------------------------------------*/
  10023. BUILDIN_FUNC(petrecovery)
  10024. {
  10025. struct pet_data *pd;
  10026. TBL_PC *sd=script_rid2sd(st);
  10027. if(sd==NULL || sd->pd==NULL)
  10028. return 0;
  10029. pd=sd->pd;
  10030. if (pd->recovery)
  10031. { //Halt previous bonus
  10032. if (pd->recovery->timer != INVALID_TIMER)
  10033. delete_timer(pd->recovery->timer, pet_recovery_timer);
  10034. } else //Init
  10035. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  10036. pd->recovery->type = (sc_type)script_getnum(st,2);
  10037. pd->recovery->delay = script_getnum(st,3);
  10038. pd->recovery->timer = INVALID_TIMER;
  10039. return 0;
  10040. }
  10041. /*==========================================
  10042. * pet healing [Valaris] //Rewritten by [Skotlex]
  10043. *------------------------------------------*/
  10044. BUILDIN_FUNC(petheal)
  10045. {
  10046. struct pet_data *pd;
  10047. TBL_PC *sd=script_rid2sd(st);
  10048. if(sd==NULL || sd->pd==NULL)
  10049. return 0;
  10050. pd=sd->pd;
  10051. if (pd->s_skill)
  10052. { //Clear previous skill
  10053. if (pd->s_skill->timer != INVALID_TIMER)
  10054. {
  10055. if (pd->s_skill->id)
  10056. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10057. else
  10058. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10059. }
  10060. } else //init memory
  10061. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10062. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  10063. //Use the lv as the amount to heal
  10064. pd->s_skill->lv=script_getnum(st,2);
  10065. pd->s_skill->delay=script_getnum(st,3);
  10066. pd->s_skill->hp=script_getnum(st,4);
  10067. pd->s_skill->sp=script_getnum(st,5);
  10068. //Use delay as initial offset to avoid skill/heal exploits
  10069. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10070. pd->s_skill->timer = INVALID_TIMER;
  10071. else
  10072. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  10073. return 0;
  10074. }
  10075. /*==========================================
  10076. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  10077. *------------------------------------------*/
  10078. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  10079. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  10080. BUILDIN_FUNC(petskillattack)
  10081. {
  10082. struct pet_data *pd;
  10083. TBL_PC *sd=script_rid2sd(st);
  10084. if(sd==NULL || sd->pd==NULL)
  10085. return 0;
  10086. pd=sd->pd;
  10087. if (pd->a_skill == NULL)
  10088. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10089. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10090. pd->a_skill->lv=script_getnum(st,3);
  10091. pd->a_skill->div_ = 0;
  10092. pd->a_skill->rate=script_getnum(st,4);
  10093. pd->a_skill->bonusrate=script_getnum(st,5);
  10094. return 0;
  10095. }
  10096. /*==========================================
  10097. * pet attack skills [Valaris]
  10098. *------------------------------------------*/
  10099. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  10100. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  10101. BUILDIN_FUNC(petskillattack2)
  10102. {
  10103. struct pet_data *pd;
  10104. TBL_PC *sd=script_rid2sd(st);
  10105. if(sd==NULL || sd->pd==NULL)
  10106. return 0;
  10107. pd=sd->pd;
  10108. if (pd->a_skill == NULL)
  10109. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10110. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10111. pd->a_skill->lv=script_getnum(st,3);
  10112. pd->a_skill->div_ = script_getnum(st,4);
  10113. pd->a_skill->rate=script_getnum(st,5);
  10114. pd->a_skill->bonusrate=script_getnum(st,6);
  10115. return 0;
  10116. }
  10117. /*==========================================
  10118. * pet support skills [Skotlex]
  10119. *------------------------------------------*/
  10120. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  10121. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  10122. BUILDIN_FUNC(petskillsupport)
  10123. {
  10124. struct pet_data *pd;
  10125. TBL_PC *sd=script_rid2sd(st);
  10126. if(sd==NULL || sd->pd==NULL)
  10127. return 0;
  10128. pd=sd->pd;
  10129. if (pd->s_skill)
  10130. { //Clear previous skill
  10131. if (pd->s_skill->timer != INVALID_TIMER)
  10132. {
  10133. if (pd->s_skill->id)
  10134. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10135. else
  10136. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10137. }
  10138. } else //init memory
  10139. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10140. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10141. pd->s_skill->lv=script_getnum(st,3);
  10142. pd->s_skill->delay=script_getnum(st,4);
  10143. pd->s_skill->hp=script_getnum(st,5);
  10144. pd->s_skill->sp=script_getnum(st,6);
  10145. //Use delay as initial offset to avoid skill/heal exploits
  10146. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10147. pd->s_skill->timer = INVALID_TIMER;
  10148. else
  10149. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  10150. return 0;
  10151. }
  10152. /*==========================================
  10153. * Scripted skill effects [Celest]
  10154. *------------------------------------------*/
  10155. /// skilleffect <skill id>,<level>
  10156. /// skilleffect "<skill name>",<level>
  10157. BUILDIN_FUNC(skilleffect)
  10158. {
  10159. TBL_PC *sd;
  10160. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10161. int skilllv=script_getnum(st,3);
  10162. sd=script_rid2sd(st);
  10163. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  10164. return 0;
  10165. }
  10166. /*==========================================
  10167. * NPC skill effects [Valaris]
  10168. *------------------------------------------*/
  10169. /// npcskilleffect <skill id>,<level>,<x>,<y>
  10170. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  10171. BUILDIN_FUNC(npcskilleffect)
  10172. {
  10173. struct block_list *bl= map_id2bl(st->oid);
  10174. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10175. int skilllv=script_getnum(st,3);
  10176. int x=script_getnum(st,4);
  10177. int y=script_getnum(st,5);
  10178. if (bl)
  10179. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  10180. return 0;
  10181. }
  10182. /*==========================================
  10183. * Special effects [Valaris]
  10184. *------------------------------------------*/
  10185. BUILDIN_FUNC(specialeffect)
  10186. {
  10187. struct block_list *bl=map_id2bl(st->oid);
  10188. int type = script_getnum(st,2);
  10189. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10190. if(bl==NULL)
  10191. return 0;
  10192. if( script_hasdata(st,4) )
  10193. {
  10194. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10195. if(nd)
  10196. clif_specialeffect(&nd->bl, type, target);
  10197. }
  10198. else
  10199. {
  10200. if (target == SELF) {
  10201. TBL_PC *sd=script_rid2sd(st);
  10202. if (sd)
  10203. clif_specialeffect_single(bl,type,sd->fd);
  10204. } else {
  10205. clif_specialeffect(bl, type, target);
  10206. }
  10207. }
  10208. return 0;
  10209. }
  10210. BUILDIN_FUNC(specialeffect2)
  10211. {
  10212. TBL_PC *sd=script_rid2sd(st);
  10213. int type = script_getnum(st,2);
  10214. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10215. if( script_hasdata(st,4) )
  10216. sd = map_nick2sd(script_getstr(st,4));
  10217. if (sd)
  10218. clif_specialeffect(&sd->bl, type, target);
  10219. return 0;
  10220. }
  10221. /*==========================================
  10222. * Nude [Valaris]
  10223. *------------------------------------------*/
  10224. BUILDIN_FUNC(nude)
  10225. {
  10226. TBL_PC *sd=script_rid2sd(st);
  10227. int i,calcflag=0;
  10228. if(sd==NULL)
  10229. return 0;
  10230. for(i=0;i<11;i++)
  10231. if(sd->equip_index[i] >= 0) {
  10232. if(!calcflag)
  10233. calcflag=1;
  10234. pc_unequipitem(sd,sd->equip_index[i],2);
  10235. }
  10236. if(calcflag)
  10237. status_calc_pc(sd,0);
  10238. return 0;
  10239. }
  10240. /*==========================================
  10241. * gmcommand [MouseJstr]
  10242. *
  10243. * suggested on the forums...
  10244. * splitted into atcommand & charcommand by [Skotlex]
  10245. *------------------------------------------*/
  10246. BUILDIN_FUNC(atcommand)
  10247. {
  10248. TBL_PC dummy_sd;
  10249. TBL_PC* sd;
  10250. int fd;
  10251. const char* cmd;
  10252. cmd = script_getstr(st,2);
  10253. if (st->rid) {
  10254. sd = script_rid2sd(st);
  10255. fd = sd->fd;
  10256. } else { //Use a dummy character.
  10257. sd = &dummy_sd;
  10258. fd = 0;
  10259. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10260. if (st->oid)
  10261. {
  10262. struct block_list* bl = map_id2bl(st->oid);
  10263. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10264. if (bl->type == BL_NPC)
  10265. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10266. }
  10267. }
  10268. // compatibility with previous implementation (deprecated!)
  10269. if(cmd[0] != atcommand_symbol)
  10270. {
  10271. cmd += strlen(sd->status.name);
  10272. while(*cmd != atcommand_symbol && *cmd != 0)
  10273. cmd++;
  10274. }
  10275. is_atcommand(fd, sd, cmd, 0);
  10276. return 0;
  10277. }
  10278. BUILDIN_FUNC(charcommand)
  10279. {
  10280. TBL_PC dummy_sd;
  10281. TBL_PC* sd;
  10282. int fd;
  10283. const char* cmd;
  10284. cmd = script_getstr(st,2);
  10285. if (st->rid) {
  10286. sd = script_rid2sd(st);
  10287. fd = sd->fd;
  10288. } else { //Use a dummy character.
  10289. sd = &dummy_sd;
  10290. fd = 0;
  10291. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10292. if (st->oid)
  10293. {
  10294. struct block_list* bl = map_id2bl(st->oid);
  10295. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10296. if (bl->type == BL_NPC)
  10297. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10298. }
  10299. }
  10300. if (*cmd != charcommand_symbol) {
  10301. ShowWarning("script: buildin_charcommand: No '#' symbol!\n");
  10302. script_reportsrc(st);
  10303. return 1;
  10304. }
  10305. is_atcommand(fd, sd, cmd, 0);
  10306. return 0;
  10307. }
  10308. /*==========================================
  10309. * Displays a message for the player only (like system messages like "you got an apple" )
  10310. *------------------------------------------*/
  10311. BUILDIN_FUNC(dispbottom)
  10312. {
  10313. TBL_PC *sd=script_rid2sd(st);
  10314. const char *message;
  10315. message=script_getstr(st,2);
  10316. if(sd)
  10317. clif_disp_onlyself(sd,message,(int)strlen(message));
  10318. return 0;
  10319. }
  10320. /*==========================================
  10321. * All The Players Full Recovery
  10322. * (HP/SP full restore and resurrect if need)
  10323. *------------------------------------------*/
  10324. BUILDIN_FUNC(recovery)
  10325. {
  10326. TBL_PC* sd;
  10327. struct s_mapiterator* iter;
  10328. iter = mapit_getallusers();
  10329. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10330. {
  10331. if(pc_isdead(sd))
  10332. status_revive(&sd->bl, 100, 100);
  10333. else
  10334. status_percent_heal(&sd->bl, 100, 100);
  10335. clif_displaymessage(sd->fd,"You have been recovered!");
  10336. }
  10337. mapit_free(iter);
  10338. return 0;
  10339. }
  10340. /*==========================================
  10341. * Get your pet info: getpetinfo(n)
  10342. * n -> 0:pet_id 1:pet_class 2:pet_name
  10343. * 3:friendly 4:hungry, 5: rename flag.
  10344. *------------------------------------------*/
  10345. BUILDIN_FUNC(getpetinfo)
  10346. {
  10347. TBL_PC *sd=script_rid2sd(st);
  10348. TBL_PET *pd;
  10349. int type=script_getnum(st,2);
  10350. if(!sd || !sd->pd) {
  10351. if (type == 2)
  10352. script_pushconststr(st,"null");
  10353. else
  10354. script_pushint(st,0);
  10355. return 0;
  10356. }
  10357. pd = sd->pd;
  10358. switch(type){
  10359. case 0: script_pushint(st,pd->pet.pet_id); break;
  10360. case 1: script_pushint(st,pd->pet.class_); break;
  10361. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10362. case 3: script_pushint(st,pd->pet.intimate); break;
  10363. case 4: script_pushint(st,pd->pet.hungry); break;
  10364. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10365. default:
  10366. script_pushint(st,0);
  10367. break;
  10368. }
  10369. return 0;
  10370. }
  10371. /*==========================================
  10372. * Get your homunculus info: gethominfo(n)
  10373. * n -> 0:hom_id 1:class 2:name
  10374. * 3:friendly 4:hungry, 5: rename flag.
  10375. * 6: level
  10376. *------------------------------------------*/
  10377. BUILDIN_FUNC(gethominfo)
  10378. {
  10379. TBL_PC *sd=script_rid2sd(st);
  10380. TBL_HOM *hd;
  10381. int type=script_getnum(st,2);
  10382. hd = sd?sd->hd:NULL;
  10383. if(!merc_is_hom_active(hd))
  10384. {
  10385. if (type == 2)
  10386. script_pushconststr(st,"null");
  10387. else
  10388. script_pushint(st,0);
  10389. return 0;
  10390. }
  10391. switch(type){
  10392. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10393. case 1: script_pushint(st,hd->homunculus.class_); break;
  10394. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10395. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10396. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10397. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10398. case 6: script_pushint(st,hd->homunculus.level); break;
  10399. default:
  10400. script_pushint(st,0);
  10401. break;
  10402. }
  10403. return 0;
  10404. }
  10405. /// Retrieves information about character's mercenary
  10406. /// getmercinfo <type>[,<char id>];
  10407. BUILDIN_FUNC(getmercinfo)
  10408. {
  10409. int type, char_id;
  10410. struct map_session_data* sd;
  10411. struct mercenary_data* md;
  10412. type = script_getnum(st,2);
  10413. if( script_hasdata(st,3) )
  10414. {
  10415. char_id = script_getnum(st,3);
  10416. if( ( sd = map_charid2sd(char_id) ) == NULL )
  10417. {
  10418. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  10419. script_pushnil(st);
  10420. return 1;
  10421. }
  10422. }
  10423. else
  10424. {
  10425. if( ( sd = script_rid2sd(st) ) == NULL )
  10426. {
  10427. script_pushnil(st);
  10428. return 0;
  10429. }
  10430. }
  10431. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  10432. switch( type )
  10433. {
  10434. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  10435. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  10436. case 2:
  10437. if( md )
  10438. script_pushstrcopy(st,md->db->name);
  10439. else
  10440. script_pushconststr(st,"");
  10441. break;
  10442. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  10443. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  10444. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  10445. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  10446. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  10447. default:
  10448. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  10449. script_pushnil(st);
  10450. return 1;
  10451. }
  10452. return 0;
  10453. }
  10454. /*==========================================
  10455. * Shows wether your inventory(and equips) contain
  10456. selected card or not.
  10457. checkequipedcard(4001);
  10458. *------------------------------------------*/
  10459. BUILDIN_FUNC(checkequipedcard)
  10460. {
  10461. TBL_PC *sd=script_rid2sd(st);
  10462. int n,i,c=0;
  10463. c=script_getnum(st,2);
  10464. if(sd){
  10465. for(i=0;i<MAX_INVENTORY;i++){
  10466. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10467. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10468. continue;
  10469. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10470. if(sd->status.inventory[i].card[n]==c){
  10471. script_pushint(st,1);
  10472. return 0;
  10473. }
  10474. }
  10475. }
  10476. }
  10477. }
  10478. script_pushint(st,0);
  10479. return 0;
  10480. }
  10481. BUILDIN_FUNC(jump_zero)
  10482. {
  10483. int sel;
  10484. sel=script_getnum(st,2);
  10485. if(!sel) {
  10486. int pos;
  10487. if( !data_islabel(script_getdata(st,3)) ){
  10488. ShowError("script: jump_zero: not label !\n");
  10489. st->state=END;
  10490. return 1;
  10491. }
  10492. pos=script_getnum(st,3);
  10493. st->pos=pos;
  10494. st->state=GOTO;
  10495. }
  10496. return 0;
  10497. }
  10498. /*==========================================
  10499. * GetMapMobs
  10500. returns mob counts on a set map:
  10501. e.g. GetMapMobs("prontera")
  10502. use "this" - for player's map
  10503. *------------------------------------------*/
  10504. BUILDIN_FUNC(getmapmobs)
  10505. {
  10506. const char *str=NULL;
  10507. int m=-1,bx,by;
  10508. int count=0;
  10509. struct block_list *bl;
  10510. str=script_getstr(st,2);
  10511. if(strcmp(str,"this")==0){
  10512. TBL_PC *sd=script_rid2sd(st);
  10513. if(sd)
  10514. m=sd->bl.m;
  10515. else{
  10516. script_pushint(st,-1);
  10517. return 0;
  10518. }
  10519. }else
  10520. m=map_mapname2mapid(str);
  10521. if(m < 0){
  10522. script_pushint(st,-1);
  10523. return 0;
  10524. }
  10525. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  10526. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  10527. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  10528. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  10529. count++;
  10530. script_pushint(st,count);
  10531. return 0;
  10532. }
  10533. /*==========================================
  10534. * movenpc [MouseJstr]
  10535. *------------------------------------------*/
  10536. BUILDIN_FUNC(movenpc)
  10537. {
  10538. TBL_NPC *nd = NULL;
  10539. const char *npc;
  10540. int x,y;
  10541. npc = script_getstr(st,2);
  10542. x = script_getnum(st,3);
  10543. y = script_getnum(st,4);
  10544. if ((nd = npc_name2id(npc)) == NULL)
  10545. return -1;
  10546. if (script_hasdata(st,5))
  10547. nd->ud.dir = script_getnum(st,5) % 8;
  10548. npc_movenpc(nd, x, y);
  10549. return 0;
  10550. }
  10551. /*==========================================
  10552. * message [MouseJstr]
  10553. *------------------------------------------*/
  10554. BUILDIN_FUNC(message)
  10555. {
  10556. const char *msg,*player;
  10557. TBL_PC *pl_sd = NULL;
  10558. player = script_getstr(st,2);
  10559. msg = script_getstr(st,3);
  10560. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10561. return 0;
  10562. clif_displaymessage(pl_sd->fd, msg);
  10563. return 0;
  10564. }
  10565. /*==========================================
  10566. * npctalk (sends message to surrounding area)
  10567. *------------------------------------------*/
  10568. BUILDIN_FUNC(npctalk)
  10569. {
  10570. const char* str;
  10571. char name[NAME_LENGTH], message[256];
  10572. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10573. str = script_getstr(st,2);
  10574. if(nd)
  10575. {
  10576. safestrncpy(name, nd->name, sizeof(name));
  10577. strtok(name, "#"); // discard extra name identifier if present
  10578. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  10579. clif_message(&nd->bl, message);
  10580. }
  10581. return 0;
  10582. }
  10583. // change npc walkspeed [Valaris]
  10584. BUILDIN_FUNC(npcspeed)
  10585. {
  10586. struct npc_data* nd;
  10587. int speed;
  10588. speed = script_getnum(st,2);
  10589. nd =(struct npc_data *)map_id2bl(st->oid);
  10590. if( nd )
  10591. {
  10592. nd->speed = speed;
  10593. nd->ud.state.speed_changed = 1;
  10594. }
  10595. return 0;
  10596. }
  10597. // make an npc walk to a position [Valaris]
  10598. BUILDIN_FUNC(npcwalkto)
  10599. {
  10600. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10601. int x=0,y=0;
  10602. x=script_getnum(st,2);
  10603. y=script_getnum(st,3);
  10604. if(nd) {
  10605. unit_walktoxy(&nd->bl,x,y,0);
  10606. }
  10607. return 0;
  10608. }
  10609. // stop an npc's movement [Valaris]
  10610. BUILDIN_FUNC(npcstop)
  10611. {
  10612. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10613. if(nd) {
  10614. unit_stop_walking(&nd->bl,1|4);
  10615. }
  10616. return 0;
  10617. }
  10618. /*==========================================
  10619. * getlook char info. getlook(arg)
  10620. *------------------------------------------*/
  10621. BUILDIN_FUNC(getlook)
  10622. {
  10623. int type,val;
  10624. TBL_PC *sd;
  10625. sd=script_rid2sd(st);
  10626. type=script_getnum(st,2);
  10627. val=-1;
  10628. switch(type) {
  10629. case LOOK_HAIR: val=sd->status.hair; break; //1
  10630. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10631. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10632. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10633. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10634. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10635. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10636. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10637. case LOOK_SHOES: break; //9
  10638. }
  10639. script_pushint(st,val);
  10640. return 0;
  10641. }
  10642. /*==========================================
  10643. * get char save point. argument: 0- map name, 1- x, 2- y
  10644. *------------------------------------------*/
  10645. BUILDIN_FUNC(getsavepoint)
  10646. {
  10647. TBL_PC* sd;
  10648. int type;
  10649. sd = script_rid2sd(st);
  10650. if (sd == NULL) {
  10651. script_pushint(st,0);
  10652. return 0;
  10653. }
  10654. type = script_getnum(st,2);
  10655. switch(type) {
  10656. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10657. case 1: script_pushint(st,sd->status.save_point.x); break;
  10658. case 2: script_pushint(st,sd->status.save_point.y); break;
  10659. default:
  10660. script_pushint(st,0);
  10661. break;
  10662. }
  10663. return 0;
  10664. }
  10665. /*==========================================
  10666. * Get position for char/npc/pet/mob objects. Added by Lorky
  10667. *
  10668. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10669. * where type:
  10670. * MapName$ - String variable for output map name
  10671. * MapX - Integer variable for output coord X
  10672. * MapY - Integer variable for output coord Y
  10673. * type - type of object
  10674. * 0 - Character coord
  10675. * 1 - NPC coord
  10676. * 2 - Pet coord
  10677. * 3 - Mob coord (not released)
  10678. * 4 - Homun coord
  10679. * CharName$ - Name object. If miss or "this" the current object
  10680. *
  10681. * Return:
  10682. * 0 - success
  10683. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10684. *------------------------------------------*/
  10685. BUILDIN_FUNC(getmapxy)
  10686. {
  10687. struct block_list *bl = NULL;
  10688. TBL_PC *sd=NULL;
  10689. int num;
  10690. const char *name;
  10691. char prefix;
  10692. int x,y,type;
  10693. char mapname[MAP_NAME_LENGTH];
  10694. if( !data_isreference(script_getdata(st,2)) ){
  10695. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10696. script_pushint(st,-1);
  10697. return 1;
  10698. }
  10699. if( !data_isreference(script_getdata(st,3)) ){
  10700. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10701. script_pushint(st,-1);
  10702. return 1;
  10703. }
  10704. if( !data_isreference(script_getdata(st,4)) ){
  10705. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10706. script_pushint(st,-1);
  10707. return 1;
  10708. }
  10709. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10710. type=script_getnum(st,5);
  10711. switch (type){
  10712. case 0: //Get Character Position
  10713. if( script_hasdata(st,6) )
  10714. sd=map_nick2sd(script_getstr(st,6));
  10715. else
  10716. sd=script_rid2sd(st);
  10717. if (sd)
  10718. bl = &sd->bl;
  10719. break;
  10720. case 1: //Get NPC Position
  10721. if( script_hasdata(st,6) )
  10722. {
  10723. struct npc_data *nd;
  10724. nd=npc_name2id(script_getstr(st,6));
  10725. if (nd)
  10726. bl = &nd->bl;
  10727. } else //In case the origin is not an npc?
  10728. bl=map_id2bl(st->oid);
  10729. break;
  10730. case 2: //Get Pet Position
  10731. if(script_hasdata(st,6))
  10732. sd=map_nick2sd(script_getstr(st,6));
  10733. else
  10734. sd=script_rid2sd(st);
  10735. if (sd && sd->pd)
  10736. bl = &sd->pd->bl;
  10737. break;
  10738. case 3: //Get Mob Position
  10739. break; //Not supported?
  10740. case 4: //Get Homun Position
  10741. if(script_hasdata(st,6))
  10742. sd=map_nick2sd(script_getstr(st,6));
  10743. else
  10744. sd=script_rid2sd(st);
  10745. if (sd && sd->hd)
  10746. bl = &sd->hd->bl;
  10747. break;
  10748. default:
  10749. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  10750. script_pushint(st,-1);
  10751. return 1;
  10752. }
  10753. if (!bl) { //No object found.
  10754. script_pushint(st,-1);
  10755. return 0;
  10756. }
  10757. x= bl->x;
  10758. y= bl->y;
  10759. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10760. //Set MapName$
  10761. num=st->stack->stack_data[st->start+2].u.num;
  10762. name=get_str(num&0x00ffffff);
  10763. prefix=*name;
  10764. if(not_server_variable(prefix))
  10765. sd=script_rid2sd(st);
  10766. else
  10767. sd=NULL;
  10768. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10769. //Set MapX
  10770. num=st->stack->stack_data[st->start+3].u.num;
  10771. name=get_str(num&0x00ffffff);
  10772. prefix=*name;
  10773. if(not_server_variable(prefix))
  10774. sd=script_rid2sd(st);
  10775. else
  10776. sd=NULL;
  10777. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10778. //Set MapY
  10779. num=st->stack->stack_data[st->start+4].u.num;
  10780. name=get_str(num&0x00ffffff);
  10781. prefix=*name;
  10782. if(not_server_variable(prefix))
  10783. sd=script_rid2sd(st);
  10784. else
  10785. sd=NULL;
  10786. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10787. //Return Success value
  10788. script_pushint(st,0);
  10789. return 0;
  10790. }
  10791. /*==========================================
  10792. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10793. *------------------------------------------*/
  10794. BUILDIN_FUNC(logmes)
  10795. {
  10796. const char *str;
  10797. TBL_PC* sd;
  10798. if( log_config.npc <= 0 )
  10799. return 0;
  10800. sd = script_rid2sd(st);
  10801. if( sd == NULL )
  10802. return 1;
  10803. str = script_getstr(st,2);
  10804. log_npc(sd,str);
  10805. return 0;
  10806. }
  10807. BUILDIN_FUNC(summon)
  10808. {
  10809. int _class, timeout=0;
  10810. const char *str,*event="";
  10811. TBL_PC *sd;
  10812. struct mob_data *md;
  10813. int tick = gettick();
  10814. sd=script_rid2sd(st);
  10815. if (!sd) return 0;
  10816. str =script_getstr(st,2);
  10817. _class=script_getnum(st,3);
  10818. if( script_hasdata(st,4) )
  10819. timeout=script_getnum(st,4);
  10820. if( script_hasdata(st,5) ){
  10821. event=script_getstr(st,5);
  10822. check_event(st, event);
  10823. }
  10824. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10825. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10826. if (md) {
  10827. md->master_id=sd->bl.id;
  10828. md->special_state.ai=1;
  10829. if( md->deletetimer != INVALID_TIMER )
  10830. delete_timer(md->deletetimer, mob_timer_delete);
  10831. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10832. mob_spawn (md); //Now it is ready for spawning.
  10833. clif_specialeffect(&md->bl,344,AREA);
  10834. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10835. }
  10836. return 0;
  10837. }
  10838. /*==========================================
  10839. * Checks whether it is daytime/nighttime
  10840. *------------------------------------------*/
  10841. BUILDIN_FUNC(isnight)
  10842. {
  10843. script_pushint(st,(night_flag == 1));
  10844. return 0;
  10845. }
  10846. BUILDIN_FUNC(isday)
  10847. {
  10848. script_pushint(st,(night_flag == 0));
  10849. return 0;
  10850. }
  10851. /*================================================
  10852. * Check how many items/cards in the list are
  10853. * equipped - used for 2/15's cards patch [celest]
  10854. *------------------------------------------------*/
  10855. BUILDIN_FUNC(isequippedcnt)
  10856. {
  10857. TBL_PC *sd;
  10858. int i, j, k, id = 1;
  10859. int ret = 0;
  10860. sd = script_rid2sd(st);
  10861. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10862. script_pushint(st,0);
  10863. return 0;
  10864. }
  10865. for (i=0; id!=0; i++) {
  10866. FETCH (i+2, id) else id = 0;
  10867. if (id <= 0)
  10868. continue;
  10869. for (j=0; j<EQI_MAX; j++) {
  10870. int index;
  10871. index = sd->equip_index[j];
  10872. if(index < 0) continue;
  10873. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10874. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10875. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10876. if(!sd->inventory_data[index])
  10877. continue;
  10878. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10879. if (sd->inventory_data[index]->nameid == id)
  10880. ret+= sd->status.inventory[index].amount;
  10881. } else { //Count cards.
  10882. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10883. continue; //No cards
  10884. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10885. if (sd->status.inventory[index].card[k] == id)
  10886. ret++; //[Lupus]
  10887. }
  10888. }
  10889. }
  10890. }
  10891. script_pushint(st,ret);
  10892. return 0;
  10893. }
  10894. /*================================================
  10895. * Check whether another card has been
  10896. * equipped - used for 2/15's cards patch [celest]
  10897. * -- Items checked cannot be reused in another
  10898. * card set to prevent exploits
  10899. *------------------------------------------------*/
  10900. BUILDIN_FUNC(isequipped)
  10901. {
  10902. TBL_PC *sd;
  10903. int i, j, k, id = 1;
  10904. int index, flag;
  10905. int ret = -1;
  10906. //Original hash to reverse it when full check fails.
  10907. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10908. sd = script_rid2sd(st);
  10909. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10910. script_pushint(st,0);
  10911. return 0;
  10912. }
  10913. setitem_hash = sd->setitem_hash;
  10914. setitem_hash2 = sd->setitem_hash2;
  10915. for (i=0; id!=0; i++)
  10916. {
  10917. FETCH (i+2, id) else id = 0;
  10918. if (id <= 0)
  10919. continue;
  10920. flag = 0;
  10921. for (j=0; j<EQI_MAX; j++)
  10922. {
  10923. index = sd->equip_index[j];
  10924. if(index < 0) continue;
  10925. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10926. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10927. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10928. if(!sd->inventory_data[index])
  10929. continue;
  10930. if (itemdb_type(id) != IT_CARD) {
  10931. if (sd->inventory_data[index]->nameid != id)
  10932. continue;
  10933. flag = 1;
  10934. break;
  10935. } else { //Cards
  10936. if (sd->inventory_data[index]->slot == 0 ||
  10937. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10938. continue;
  10939. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10940. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10941. unsigned int hash = 0;
  10942. if (sd->status.inventory[index].card[k] != id)
  10943. continue;
  10944. hash = 1<<((j<5?j:j-5)*4 + k);
  10945. // check if card is already used by another set
  10946. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10947. continue;
  10948. // We have found a match
  10949. flag = 1;
  10950. // Set hash so this card cannot be used by another
  10951. if (j<5)
  10952. sd->setitem_hash |= hash;
  10953. else
  10954. sd->setitem_hash2 |= hash;
  10955. break;
  10956. }
  10957. }
  10958. if (flag) break; //Card found
  10959. }
  10960. if (ret == -1)
  10961. ret = flag;
  10962. else
  10963. ret &= flag;
  10964. if (!ret) break;
  10965. }
  10966. if (!ret)
  10967. { //When check fails, restore original hash values. [Skotlex]
  10968. sd->setitem_hash = setitem_hash;
  10969. sd->setitem_hash2 = setitem_hash2;
  10970. }
  10971. script_pushint(st,ret);
  10972. return 0;
  10973. }
  10974. /*================================================
  10975. * Check how many given inserted cards in the CURRENT
  10976. * weapon - used for 2/15's cards patch [Lupus]
  10977. *------------------------------------------------*/
  10978. BUILDIN_FUNC(cardscnt)
  10979. {
  10980. TBL_PC *sd;
  10981. int i, k, id = 1;
  10982. int ret = 0;
  10983. int index;
  10984. sd = script_rid2sd(st);
  10985. for (i=0; id!=0; i++) {
  10986. FETCH (i+2, id) else id = 0;
  10987. if (id <= 0)
  10988. continue;
  10989. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10990. if(index < 0) continue;
  10991. if(!sd->inventory_data[index])
  10992. continue;
  10993. if(itemdb_type(id) != IT_CARD) {
  10994. if (sd->inventory_data[index]->nameid == id)
  10995. ret+= sd->status.inventory[index].amount;
  10996. } else {
  10997. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10998. continue;
  10999. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11000. if (sd->status.inventory[index].card[k] == id)
  11001. ret++;
  11002. }
  11003. }
  11004. }
  11005. script_pushint(st,ret);
  11006. // script_pushint(st,current_equip_item_index);
  11007. return 0;
  11008. }
  11009. /*=======================================================
  11010. * Returns the refined number of the current item, or an
  11011. * item with inventory index specified
  11012. *-------------------------------------------------------*/
  11013. BUILDIN_FUNC(getrefine)
  11014. {
  11015. TBL_PC *sd;
  11016. if ((sd = script_rid2sd(st))!= NULL)
  11017. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  11018. else
  11019. script_pushint(st,0);
  11020. return 0;
  11021. }
  11022. /*=======================================================
  11023. * Day/Night controls
  11024. *-------------------------------------------------------*/
  11025. BUILDIN_FUNC(night)
  11026. {
  11027. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  11028. return 0;
  11029. }
  11030. BUILDIN_FUNC(day)
  11031. {
  11032. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  11033. return 0;
  11034. }
  11035. //=======================================================
  11036. // Unequip [Spectre]
  11037. //-------------------------------------------------------
  11038. BUILDIN_FUNC(unequip)
  11039. {
  11040. int i;
  11041. size_t num;
  11042. TBL_PC *sd;
  11043. num = script_getnum(st,2);
  11044. sd = script_rid2sd(st);
  11045. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  11046. {
  11047. i = pc_checkequip(sd,equip[num-1]);
  11048. if (i >= 0)
  11049. pc_unequipitem(sd,i,1|2);
  11050. }
  11051. return 0;
  11052. }
  11053. BUILDIN_FUNC(equip)
  11054. {
  11055. int nameid=0,i;
  11056. TBL_PC *sd;
  11057. struct item_data *item_data;
  11058. sd = script_rid2sd(st);
  11059. nameid=script_getnum(st,2);
  11060. if((item_data = itemdb_exists(nameid)) == NULL)
  11061. {
  11062. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  11063. return 1;
  11064. }
  11065. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  11066. if( i < MAX_INVENTORY )
  11067. pc_equipitem(sd,i,item_data->equip);
  11068. return 0;
  11069. }
  11070. BUILDIN_FUNC(autoequip)
  11071. {
  11072. int nameid, flag;
  11073. struct item_data *item_data;
  11074. nameid=script_getnum(st,2);
  11075. flag=script_getnum(st,3);
  11076. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  11077. {
  11078. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  11079. return 1;
  11080. }
  11081. if( !itemdb_isequip2(item_data) )
  11082. {
  11083. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  11084. return 1;
  11085. }
  11086. item_data->flag.autoequip = flag>0?1:0;
  11087. return 0;
  11088. }
  11089. BUILDIN_FUNC(setbattleflag)
  11090. {
  11091. const char *flag, *value;
  11092. flag = script_getstr(st,2);
  11093. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  11094. if (battle_set_value(flag, value) == 0)
  11095. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  11096. else
  11097. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  11098. return 0;
  11099. }
  11100. BUILDIN_FUNC(getbattleflag)
  11101. {
  11102. const char *flag;
  11103. flag = script_getstr(st,2);
  11104. script_pushint(st,battle_get_value(flag));
  11105. return 0;
  11106. }
  11107. //=======================================================
  11108. // strlen [Valaris]
  11109. //-------------------------------------------------------
  11110. BUILDIN_FUNC(getstrlen)
  11111. {
  11112. const char *str = script_getstr(st,2);
  11113. int len = (str) ? (int)strlen(str) : 0;
  11114. script_pushint(st,len);
  11115. return 0;
  11116. }
  11117. //=======================================================
  11118. // isalpha [Valaris]
  11119. //-------------------------------------------------------
  11120. BUILDIN_FUNC(charisalpha)
  11121. {
  11122. const char *str=script_getstr(st,2);
  11123. int pos=script_getnum(st,3);
  11124. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  11125. script_pushint(st,val);
  11126. return 0;
  11127. }
  11128. //=======================================================
  11129. // charisupper <str>, <index>
  11130. //-------------------------------------------------------
  11131. BUILDIN_FUNC(charisupper)
  11132. {
  11133. const char *str = script_getstr(st,2);
  11134. int pos = script_getnum(st,3);
  11135. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  11136. script_pushint(st,val);
  11137. return 0;
  11138. }
  11139. //=======================================================
  11140. // charislower <str>, <index>
  11141. //-------------------------------------------------------
  11142. BUILDIN_FUNC(charislower)
  11143. {
  11144. const char *str = script_getstr(st,2);
  11145. int pos = script_getnum(st,3);
  11146. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  11147. script_pushint(st,val);
  11148. return 0;
  11149. }
  11150. //=======================================================
  11151. // charat <str>, <index>
  11152. //-------------------------------------------------------
  11153. BUILDIN_FUNC(charat)
  11154. {
  11155. const char *str = script_getstr(st,2);
  11156. int pos = script_getnum(st,3);
  11157. char *output;
  11158. output = (char*)aMallocA(2*sizeof(char));
  11159. output[0] = '\0';
  11160. if(str && pos >= 0 && (unsigned int)pos < strlen(str))
  11161. sprintf(output, "%c", str[pos]);
  11162. script_pushstr(st, output);
  11163. return 0;
  11164. }
  11165. //=======================================================
  11166. // setchar <string>, <char>, <index>
  11167. //-------------------------------------------------------
  11168. BUILDIN_FUNC(setchar)
  11169. {
  11170. const char *str = script_getstr(st,2);
  11171. const char *c = script_getstr(st,3);
  11172. int index = script_getnum(st,4);
  11173. char *output;
  11174. size_t len = strlen(str);
  11175. output = (char*)aMallocA(len + 1);
  11176. memcpy(output, str, len);
  11177. output[len] = '\0';
  11178. if(index >= 0 && index < len)
  11179. output[index] = c[0];
  11180. script_pushstr(st, output);
  11181. return 0;
  11182. }
  11183. //=======================================================
  11184. // insertchar <string>, <char>, <index>
  11185. //-------------------------------------------------------
  11186. BUILDIN_FUNC(insertchar)
  11187. {
  11188. const char *str = script_getstr(st,2);
  11189. const char *c = script_getstr(st,3);
  11190. int index = script_getnum(st,4);
  11191. char *output;
  11192. size_t len = strlen(str);
  11193. if(index < 0)
  11194. index = 0;
  11195. else if(index > len)
  11196. index = len;
  11197. output = (char*)aMallocA(len + 2);
  11198. memcpy(output, str, index);
  11199. output[index] = c[0];
  11200. memcpy(&output[index+1], &str[index], len - index);
  11201. output[len+1] = '\0';
  11202. script_pushstr(st, output);
  11203. return 0;
  11204. }
  11205. //=======================================================
  11206. // delchar <string>, <index>
  11207. //-------------------------------------------------------
  11208. BUILDIN_FUNC(delchar)
  11209. {
  11210. const char *str = script_getstr(st,2);
  11211. int index = script_getnum(st,3);
  11212. char *output;
  11213. size_t len = strlen(str);
  11214. if(index < 0 || index > len) {
  11215. //return original
  11216. ++len;
  11217. output = (char*)aMallocA(len);
  11218. memcpy(output, str, len);
  11219. script_pushstr(st, output);
  11220. return 0;
  11221. }
  11222. output = (char*)aMallocA(len);
  11223. memcpy(output, str, index);
  11224. memcpy(&output[index], &str[index+1], len - index);
  11225. script_pushstr(st, output);
  11226. return 0;
  11227. }
  11228. //=======================================================
  11229. // strtoupper <str>
  11230. //-------------------------------------------------------
  11231. BUILDIN_FUNC(strtoupper)
  11232. {
  11233. const char *str = script_getstr(st,2);
  11234. char *output;
  11235. int i = 0;
  11236. output = (char*)aMallocA(strlen(str) + 1);
  11237. while(str[i] != '\0')
  11238. output[i++] = TOUPPER(str[i]);
  11239. output[i] = '\0';
  11240. script_pushstr(st, output);
  11241. return 0;
  11242. }
  11243. //=======================================================
  11244. // strtolower <str>
  11245. //-------------------------------------------------------
  11246. BUILDIN_FUNC(strtolower)
  11247. {
  11248. const char *str = script_getstr(st,2);
  11249. char *output;
  11250. int i = 0;
  11251. output = (char*)aMallocA(strlen(str) + 1);
  11252. while(str[i] != '\0')
  11253. output[i++] = TOLOWER(str[i]);
  11254. output[i] = '\0';
  11255. script_pushstr(st, output);
  11256. return 0;
  11257. }
  11258. //=======================================================
  11259. // substr <str>, <start>, <end>
  11260. //-------------------------------------------------------
  11261. BUILDIN_FUNC(substr)
  11262. {
  11263. const char *str = script_getstr(st,2);
  11264. char *output;
  11265. int start = script_getnum(st,3);
  11266. int end = script_getnum(st,4);
  11267. int len = 0;
  11268. if(start >= 0 && end < strlen(str) && start <= end) {
  11269. len = end - start + 1;
  11270. output = (char*)aMallocA(len + 1);
  11271. memcpy(output, &str[start], len);
  11272. } else
  11273. output = (char*)aMallocA(1);
  11274. output[len] = '\0';
  11275. script_pushstr(st, output);
  11276. return 0;
  11277. }
  11278. //=======================================================
  11279. // explode <dest_string_array>, <str>, <delimiter>
  11280. // Note: delimiter is limited to 1 char
  11281. //-------------------------------------------------------
  11282. BUILDIN_FUNC(explode)
  11283. {
  11284. struct script_data* data = script_getdata(st, 2);
  11285. const char *str = script_getstr(st,3);
  11286. const char delimiter = script_getstr(st, 4)[0];
  11287. int32 id;
  11288. size_t len = strlen(str);
  11289. int i = 0, j = 0;
  11290. int start;
  11291. char *temp;
  11292. const char* name;
  11293. TBL_PC* sd = NULL;
  11294. temp = (char*)aMallocA(len + 1);
  11295. if( !data_isreference(data) )
  11296. {
  11297. ShowError("script:explode: not a variable\n");
  11298. script_reportdata(data);
  11299. st->state = END;
  11300. return 1;// not a variable
  11301. }
  11302. id = reference_getid(data);
  11303. start = reference_getindex(data);
  11304. name = reference_getname(data);
  11305. if( not_array_variable(*name) )
  11306. {
  11307. ShowError("script:explode: illegal scope\n");
  11308. script_reportdata(data);
  11309. st->state = END;
  11310. return 1;// not supported
  11311. }
  11312. if( !is_string_variable(name) )
  11313. {
  11314. ShowError("script:explode: not string array\n");
  11315. script_reportdata(data);
  11316. st->state = END;
  11317. return 1;// data type mismatch
  11318. }
  11319. if( not_server_variable(*name) )
  11320. {
  11321. sd = script_rid2sd(st);
  11322. if( sd == NULL )
  11323. return 0;// no player attached
  11324. }
  11325. while(str[i] != '\0') {
  11326. if(str[i] == delimiter && start < 127) { //break at delimiter but ignore after reaching last array index
  11327. temp[j] = '\0';
  11328. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  11329. j = 0;
  11330. ++i;
  11331. } else {
  11332. temp[j++] = str[i++];
  11333. }
  11334. }
  11335. //set last string
  11336. temp[j] = '\0';
  11337. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  11338. aFree(temp);
  11339. return 0;
  11340. }
  11341. //=======================================================
  11342. // implode <string_array>
  11343. // implode <string_array>, <glue>
  11344. //-------------------------------------------------------
  11345. BUILDIN_FUNC(implode)
  11346. {
  11347. struct script_data* data = script_getdata(st, 2);
  11348. const char *glue = NULL, *name, *temp;
  11349. int32 glue_len = 0, array_size, id;
  11350. size_t len = 0;
  11351. int i, k = 0;
  11352. TBL_PC* sd = NULL;
  11353. char *output;
  11354. if( !data_isreference(data) )
  11355. {
  11356. ShowError("script:implode: not a variable\n");
  11357. script_reportdata(data);
  11358. st->state = END;
  11359. return 1;// not a variable
  11360. }
  11361. id = reference_getid(data);
  11362. name = reference_getname(data);
  11363. if( not_array_variable(*name) )
  11364. {
  11365. ShowError("script:implode: illegal scope\n");
  11366. script_reportdata(data);
  11367. st->state = END;
  11368. return 1;// not supported
  11369. }
  11370. if( !is_string_variable(name) )
  11371. {
  11372. ShowError("script:implode: not string array\n");
  11373. script_reportdata(data);
  11374. st->state = END;
  11375. return 1;// data type mismatch
  11376. }
  11377. if( not_server_variable(*name) )
  11378. {
  11379. sd = script_rid2sd(st);
  11380. if( sd == NULL )
  11381. return 0;// no player attached
  11382. }
  11383. //count chars
  11384. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  11385. if(array_size == -1) //empty array check (AmsTaff)
  11386. {
  11387. ShowWarning("script:implode: array length = 0\n");
  11388. output = (char*)aMallocA(sizeof(char)*5);
  11389. sprintf(output,"%s","NULL");
  11390. } else {
  11391. for(i = 0; i <= array_size; ++i) {
  11392. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11393. len += strlen(temp);
  11394. script_removetop(st, -1, 0);
  11395. }
  11396. //allocate mem
  11397. if( script_hasdata(st,3) ) {
  11398. glue = script_getstr(st,3);
  11399. glue_len = strlen(glue);
  11400. len += glue_len * (array_size);
  11401. }
  11402. output = (char*)aMallocA(len + 1);
  11403. //build output
  11404. for(i = 0; i < array_size; ++i) {
  11405. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  11406. len = strlen(temp);
  11407. memcpy(&output[k], temp, len);
  11408. k += len;
  11409. if(glue_len != 0) {
  11410. memcpy(&output[k], glue, glue_len);
  11411. k += glue_len;
  11412. }
  11413. script_removetop(st, -1, 0);
  11414. }
  11415. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  11416. len = strlen(temp);
  11417. memcpy(&output[k], temp, len);
  11418. k += len;
  11419. script_removetop(st, -1, 0);
  11420. output[k] = '\0';
  11421. }
  11422. script_pushstr(st, output);
  11423. return 0;
  11424. }
  11425. //=======================================================
  11426. // sprintf(<format>, ...);
  11427. // Implements C sprintf, except format %n. The resulting string is
  11428. // returned, instead of being saved in variable by reference.
  11429. //-------------------------------------------------------
  11430. BUILDIN_FUNC(sprintf)
  11431. {
  11432. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  11433. const char* format;
  11434. char* p;
  11435. char* q;
  11436. char* buf = NULL;
  11437. char* buf2 = NULL;
  11438. struct script_data* data;
  11439. StringBuf final_buf;
  11440. // Fetch init data
  11441. format = script_getstr(st, 2);
  11442. argc = script_lastdata(st)-2;
  11443. len = strlen(format);
  11444. // Skip parsing, where no parsing is required.
  11445. if(len==0){
  11446. script_pushconststr(st,"");
  11447. return 0;
  11448. }
  11449. // Pessimistic alloc
  11450. CREATE(buf, char, len+1);
  11451. // Need not be parsed, just solve stuff like %%.
  11452. if(argc==0){
  11453. memcpy(buf,format,len+1);
  11454. script_pushstrcopy(st, buf);
  11455. aFree(buf);
  11456. return 0;
  11457. }
  11458. safestrncpy(buf, format, len+1);
  11459. // Issue sprintf for each parameter
  11460. StringBuf_Init(&final_buf);
  11461. q = buf;
  11462. while((p = strchr(q, '%'))!=NULL){
  11463. if(p!=q){
  11464. len = p-q+1;
  11465. if(buf2_len<len){
  11466. RECREATE(buf2, char, len);
  11467. buf2_len = len;
  11468. }
  11469. safestrncpy(buf2, q, len);
  11470. StringBuf_AppendStr(&final_buf, buf2);
  11471. q = p;
  11472. }
  11473. p = q+1;
  11474. if(*p=='%'){ // %%
  11475. StringBuf_AppendStr(&final_buf, "%");
  11476. q+=2;
  11477. continue;
  11478. }
  11479. if(*p=='n'){ // %n
  11480. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  11481. script_reportsrc(st);
  11482. q+=2;
  11483. continue;
  11484. }
  11485. if(arg>=argc){
  11486. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  11487. if(buf) aFree(buf);
  11488. if(buf2) aFree(buf2);
  11489. StringBuf_Destroy(&final_buf);
  11490. script_pushconststr(st,"");
  11491. return 1;
  11492. }
  11493. if((p = strchr(q+1, '%'))==NULL){
  11494. p = strchr(q, 0); // EOS
  11495. }
  11496. len = p-q+1;
  11497. if(buf2_len<len){
  11498. RECREATE(buf2, char, len);
  11499. buf2_len = len;
  11500. }
  11501. safestrncpy(buf2, q, len);
  11502. q = p;
  11503. // Note: This assumes the passed value being the correct
  11504. // type to the current format specifier. If not, the server
  11505. // probably crashes or returns anything else, than expected,
  11506. // but it would behave in normal code the same way so it's
  11507. // the scripter's responsibility.
  11508. data = script_getdata(st, arg+3);
  11509. if(data_isstring(data)){ // String
  11510. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11511. }else if(data_isint(data)){ // Number
  11512. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11513. }else if(data_isreference(data)){ // Variable
  11514. char* name = reference_getname(data);
  11515. if(name[strlen(name)-1]=='$'){ // var Str
  11516. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  11517. }else{ // var Int
  11518. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  11519. }
  11520. }else{ // Unsupported type
  11521. ShowError("buildin_sprintf: Unknown argument type!\n");
  11522. if(buf) aFree(buf);
  11523. if(buf2) aFree(buf2);
  11524. StringBuf_Destroy(&final_buf);
  11525. script_pushconststr(st,"");
  11526. return 1;
  11527. }
  11528. arg++;
  11529. }
  11530. // Append anything left
  11531. if(*q){
  11532. StringBuf_AppendStr(&final_buf, q);
  11533. }
  11534. // Passed more, than needed
  11535. if(arg<argc){
  11536. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  11537. script_reportsrc(st);
  11538. }
  11539. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  11540. if(buf) aFree(buf);
  11541. if(buf2) aFree(buf2);
  11542. StringBuf_Destroy(&final_buf);
  11543. return 0;
  11544. }
  11545. //=======================================================
  11546. // sscanf(<str>, <format>, ...);
  11547. // Implements C sscanf.
  11548. //-------------------------------------------------------
  11549. BUILDIN_FUNC(sscanf){
  11550. unsigned int argc, arg = 0, len;
  11551. struct script_data* data;
  11552. struct map_session_data* sd = NULL;
  11553. const char* str;
  11554. const char* format;
  11555. const char* p;
  11556. const char* q;
  11557. char* buf = NULL;
  11558. char* buf_p;
  11559. char* ref_str = NULL;
  11560. int ref_int;
  11561. // Get data
  11562. str = script_getstr(st, 2);
  11563. format = script_getstr(st, 3);
  11564. argc = script_lastdata(st)-3;
  11565. len = strlen(format);
  11566. CREATE(buf, char, len*2+1);
  11567. // Issue sscanf for each parameter
  11568. *buf = 0;
  11569. q = format;
  11570. while((p = strchr(q, '%'))){
  11571. if(p!=q){
  11572. strncat(buf, q, (size_t)(p-q));
  11573. q = p;
  11574. }
  11575. p = q+1;
  11576. if(*p=='*' || *p=='%'){ // Skip
  11577. strncat(buf, q, 2);
  11578. q+=2;
  11579. continue;
  11580. }
  11581. if(arg>=argc){
  11582. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  11583. script_pushint(st, -1);
  11584. if(buf) aFree(buf);
  11585. if(ref_str) aFree(ref_str);
  11586. return 1;
  11587. }
  11588. if((p = strchr(q+1, '%'))==NULL){
  11589. p = strchr(q, 0); // EOS
  11590. }
  11591. len = p-q;
  11592. strncat(buf, q, len);
  11593. q = p;
  11594. // Validate output
  11595. data = script_getdata(st, arg+4);
  11596. if(!data_isreference(data) || !reference_tovariable(data)){
  11597. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  11598. script_pushint(st, -1);
  11599. if(buf) aFree(buf);
  11600. if(ref_str) aFree(ref_str);
  11601. return 1;
  11602. }
  11603. buf_p = reference_getname(data);
  11604. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  11605. script_pushint(st, -1);
  11606. if(buf) aFree(buf);
  11607. if(ref_str) aFree(ref_str);
  11608. return 0;
  11609. }
  11610. // Save value if any
  11611. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  11612. if(ref_str==NULL){
  11613. CREATE(ref_str, char, strlen(str)+1);
  11614. }
  11615. if(sscanf(str, buf, ref_str)==0){
  11616. break;
  11617. }
  11618. set_reg(st, sd, add_str(buf_p), buf_p, (void *)(ref_str), reference_getref(data));
  11619. }else{ // Number
  11620. if(sscanf(str, buf, &ref_int)==0){
  11621. break;
  11622. }
  11623. set_reg(st, sd, add_str(buf_p), buf_p, (void *)(ref_int), reference_getref(data));
  11624. }
  11625. arg++;
  11626. // Disable used format (%... -> %*...)
  11627. buf_p = strchr(buf, 0);
  11628. memmove(buf_p-len+2, buf_p-len+1, len);
  11629. *(buf_p-len+1) = '*';
  11630. }
  11631. // Passed more, than needed
  11632. if(arg<argc){
  11633. ShowWarning("buildin_sscanf: Unused arguments passed.\n");
  11634. script_reportsrc(st);
  11635. }
  11636. script_pushint(st, arg);
  11637. if(buf) aFree(buf);
  11638. if(ref_str) aFree(ref_str);
  11639. return 0;
  11640. }
  11641. //=======================================================
  11642. // strpos(<haystack>, <needle>)
  11643. // strpos(<haystack>, <needle>, <offset>)
  11644. //
  11645. // Implements PHP style strpos. Adapted from code from
  11646. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  11647. //-------------------------------------------------------
  11648. BUILDIN_FUNC(strpos) {
  11649. const char *haystack = script_getstr(st,2);
  11650. const char *needle = script_getstr(st,3);
  11651. int i;
  11652. size_t len;
  11653. if( script_hasdata(st,4) )
  11654. i = script_getnum(st,4);
  11655. else
  11656. i = 0;
  11657. if ( strlen(needle) == 0 ) {
  11658. script_pushint(st, -1);
  11659. return 0;
  11660. }
  11661. len = strlen(haystack);
  11662. for ( ; i < len; ++i ) {
  11663. if ( haystack[i] == *needle ) {
  11664. // matched starting char -- loop through remaining chars
  11665. const char *h, *n;
  11666. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  11667. if ( *h != *n ) {
  11668. break;
  11669. }
  11670. }
  11671. if ( !*n ) { // matched all of 'needle' to null termination
  11672. script_pushint(st, i);
  11673. return 0;
  11674. }
  11675. }
  11676. }
  11677. script_pushint(st, -1);
  11678. return 0;
  11679. }
  11680. //===============================================================
  11681. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  11682. //
  11683. // Note: Finds all instances of <search> in <input> and replaces
  11684. // with <replace>. If specified will only replace as many
  11685. // instances as specified in <count>. By default will be case
  11686. // sensitive.
  11687. //---------------------------------------------------------------
  11688. BUILDIN_FUNC(replacestr)
  11689. {
  11690. const char *input = script_getstr(st, 2);
  11691. const char *find = script_getstr(st, 3);
  11692. const char *replace = script_getstr(st, 4);
  11693. size_t inputlen = strlen(input);
  11694. size_t findlen = strlen(find);
  11695. struct StringBuf output;
  11696. bool usecase = true;
  11697. int count = 0;
  11698. int numFinds = 0;
  11699. int i = 0, f = 0;
  11700. if(findlen == 0) {
  11701. ShowError("script:replacestr: Invalid search length.\n");
  11702. st->state = END;
  11703. return 1;
  11704. }
  11705. if(script_hasdata(st, 5)) {
  11706. if(script_isint(st,5))
  11707. usecase = script_getnum(st, 5) != 0;
  11708. else {
  11709. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  11710. st->state = END;
  11711. return 1;
  11712. }
  11713. }
  11714. if(script_hasdata(st, 6)) {
  11715. if(script_isint(st,6))
  11716. count = script_getnum(st, 6);
  11717. else {
  11718. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  11719. st->state = END;
  11720. return 1;
  11721. }
  11722. }
  11723. StringBuf_Init(&output);
  11724. for(; i < inputlen; i++) {
  11725. if(count && count == numFinds) { //found enough, stop looking
  11726. break;
  11727. }
  11728. for(f = 0; f <= findlen; f++) {
  11729. if(f == findlen) { //complete match
  11730. numFinds++;
  11731. StringBuf_AppendStr(&output, replace);
  11732. i += findlen - 1;
  11733. break;
  11734. } else {
  11735. if(usecase) {
  11736. if((i + f) > inputlen || input[i + f] != find[f]) {
  11737. StringBuf_Printf(&output, "%c", input[i]);
  11738. break;
  11739. }
  11740. } else {
  11741. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11742. StringBuf_Printf(&output, "%c", input[i]);
  11743. break;
  11744. }
  11745. }
  11746. }
  11747. }
  11748. }
  11749. //append excess after enough found
  11750. if(i < inputlen)
  11751. StringBuf_AppendStr(&output, &(input[i]));
  11752. script_pushstrcopy(st, StringBuf_Value(&output));
  11753. StringBuf_Destroy(&output);
  11754. return 0;
  11755. }
  11756. //========================================================
  11757. // countstr <input>, <search>{, <usecase>}
  11758. //
  11759. // Note: Counts the number of times <search> occurs in
  11760. // <input>. By default will be case sensitive.
  11761. //--------------------------------------------------------
  11762. BUILDIN_FUNC(countstr)
  11763. {
  11764. const char *input = script_getstr(st, 2);
  11765. const char *find = script_getstr(st, 3);
  11766. size_t inputlen = strlen(input);
  11767. size_t findlen = strlen(find);
  11768. bool usecase = true;
  11769. int numFinds = 0;
  11770. int i = 0, f = 0;
  11771. if(findlen == 0) {
  11772. ShowError("script:countstr: Invalid search length.\n");
  11773. st->state = END;
  11774. return 1;
  11775. }
  11776. if(script_hasdata(st, 4)) {
  11777. if(script_isint(st,4))
  11778. usecase = script_getnum(st, 4) != 0;
  11779. else {
  11780. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  11781. st->state = END;
  11782. return 1;
  11783. }
  11784. }
  11785. for(; i < inputlen; i++) {
  11786. for(f = 0; f <= findlen; f++) {
  11787. if(f == findlen) { //complete match
  11788. numFinds++;
  11789. i += findlen - 1;
  11790. break;
  11791. } else {
  11792. if(usecase) {
  11793. if((i + f) > inputlen || input[i + f] != find[f]) {
  11794. break;
  11795. }
  11796. } else {
  11797. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  11798. break;
  11799. }
  11800. }
  11801. }
  11802. }
  11803. }
  11804. script_pushint(st, numFinds);
  11805. return 0;
  11806. }
  11807. /// Changes the display name and/or display class of the npc.
  11808. /// Returns 0 is successful, 1 if the npc does not exist.
  11809. ///
  11810. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  11811. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  11812. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  11813. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  11814. BUILDIN_FUNC(setnpcdisplay)
  11815. {
  11816. const char* name;
  11817. const char* newname = NULL;
  11818. int class_ = -1, size = -1;
  11819. struct script_data* data;
  11820. struct npc_data* nd;
  11821. name = script_getstr(st,2);
  11822. data = script_getdata(st,3);
  11823. if( script_hasdata(st,4) )
  11824. class_ = script_getnum(st,4);
  11825. if( script_hasdata(st,5) )
  11826. size = script_getnum(st,5);
  11827. get_val(st, data);
  11828. if( data_isstring(data) )
  11829. newname = conv_str(st,data);
  11830. else if( data_isint(data) )
  11831. class_ = conv_num(st,data);
  11832. else
  11833. {
  11834. ShowError("script:setnpcdisplay: expected a string or number\n");
  11835. script_reportdata(data);
  11836. return 1;
  11837. }
  11838. nd = npc_name2id(name);
  11839. if( nd == NULL )
  11840. {// not found
  11841. script_pushint(st,1);
  11842. return 0;
  11843. }
  11844. // update npc
  11845. if( newname )
  11846. npc_setdisplayname(nd, newname);
  11847. if( size != -1 && size != (int)nd->size )
  11848. nd->size = size;
  11849. else
  11850. size = -1;
  11851. if( class_ != -1 && nd->class_ != class_ )
  11852. npc_setclass(nd, class_);
  11853. else if( size != -1 )
  11854. { // Required to update the visual size
  11855. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  11856. clif_spawn(&nd->bl);
  11857. }
  11858. script_pushint(st,0);
  11859. return 0;
  11860. }
  11861. BUILDIN_FUNC(atoi)
  11862. {
  11863. const char *value;
  11864. value = script_getstr(st,2);
  11865. script_pushint(st,atoi(value));
  11866. return 0;
  11867. }
  11868. // case-insensitive substring search [lordalfa]
  11869. BUILDIN_FUNC(compare)
  11870. {
  11871. const char *message;
  11872. const char *cmpstring;
  11873. message = script_getstr(st,2);
  11874. cmpstring = script_getstr(st,3);
  11875. script_pushint(st,(stristr(message,cmpstring) != NULL));
  11876. return 0;
  11877. }
  11878. // [zBuffer] List of mathematics commands --->
  11879. BUILDIN_FUNC(sqrt)
  11880. {
  11881. double i, a;
  11882. i = script_getnum(st,2);
  11883. a = sqrt(i);
  11884. script_pushint(st,(int)a);
  11885. return 0;
  11886. }
  11887. BUILDIN_FUNC(pow)
  11888. {
  11889. double i, a, b;
  11890. a = script_getnum(st,2);
  11891. b = script_getnum(st,3);
  11892. i = pow(a,b);
  11893. script_pushint(st,(int)i);
  11894. return 0;
  11895. }
  11896. BUILDIN_FUNC(distance)
  11897. {
  11898. int x0, y0, x1, y1;
  11899. x0 = script_getnum(st,2);
  11900. y0 = script_getnum(st,3);
  11901. x1 = script_getnum(st,4);
  11902. y1 = script_getnum(st,5);
  11903. script_pushint(st,distance_xy(x0,y0,x1,y1));
  11904. return 0;
  11905. }
  11906. // <--- [zBuffer] List of mathematics commands
  11907. BUILDIN_FUNC(md5)
  11908. {
  11909. const char *tmpstr;
  11910. char *md5str;
  11911. tmpstr = script_getstr(st,2);
  11912. md5str = (char *)aMallocA((32+1)*sizeof(char));
  11913. MD5_String(tmpstr, md5str);
  11914. script_pushstr(st, md5str);
  11915. return 0;
  11916. }
  11917. // [zBuffer] List of dynamic var commands --->
  11918. BUILDIN_FUNC(setd)
  11919. {
  11920. TBL_PC *sd=NULL;
  11921. char varname[100];
  11922. const char *buffer;
  11923. int elem;
  11924. buffer = script_getstr(st, 2);
  11925. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  11926. elem = 0;
  11927. if( not_server_variable(*varname) )
  11928. {
  11929. sd = script_rid2sd(st);
  11930. if( sd == NULL )
  11931. {
  11932. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  11933. return 0;
  11934. }
  11935. }
  11936. if( is_string_variable(varname) ) {
  11937. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  11938. } else {
  11939. setd_sub(st, sd, varname, elem, (void *)script_getnum(st, 3), NULL);
  11940. }
  11941. return 0;
  11942. }
  11943. #ifndef TXT_ONLY
  11944. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  11945. {
  11946. int i, j;
  11947. TBL_PC* sd = NULL;
  11948. const char* query;
  11949. struct script_data* data;
  11950. const char* name;
  11951. int max_rows = SCRIPT_MAX_ARRAYSIZE;// maximum number of rows
  11952. int num_vars;
  11953. int num_cols;
  11954. // check target variables
  11955. for( i = 3; script_hasdata(st,i); ++i )
  11956. {
  11957. data = script_getdata(st, i);
  11958. if( data_isreference(data) && reference_tovariable(data) )
  11959. {// it's a variable
  11960. name = reference_getname(data);
  11961. if( not_server_variable(*name) && sd == NULL )
  11962. {// requires a player
  11963. sd = script_rid2sd(st);
  11964. if( sd == NULL )
  11965. {// no player attached
  11966. script_reportdata(data);
  11967. st->state = END;
  11968. return 1;
  11969. }
  11970. }
  11971. if( not_array_variable(*name) )
  11972. max_rows = 1;// not an array, limit to one row
  11973. }
  11974. else
  11975. {
  11976. ShowError("script:query_sql: not a variable\n");
  11977. script_reportdata(data);
  11978. st->state = END;
  11979. return 1;
  11980. }
  11981. }
  11982. num_vars = i - 3;
  11983. // Execute the query
  11984. query = script_getstr(st,2);
  11985. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  11986. {
  11987. Sql_ShowDebug(handle);
  11988. script_pushint(st, 0);
  11989. return 1;
  11990. }
  11991. if( Sql_NumRows(handle) == 0 )
  11992. {// No data received
  11993. Sql_FreeResult(handle);
  11994. script_pushint(st, 0);
  11995. return 0;
  11996. }
  11997. // Count the number of columns to store
  11998. num_cols = Sql_NumColumns(handle);
  11999. if( num_vars < num_cols )
  12000. {
  12001. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  12002. script_reportsrc(st);
  12003. }
  12004. else if( num_vars > num_cols )
  12005. {
  12006. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  12007. script_reportsrc(st);
  12008. }
  12009. // Store data
  12010. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  12011. {
  12012. for( j = 0; j < num_vars; ++j )
  12013. {
  12014. char* str = NULL;
  12015. if( j < num_cols )
  12016. Sql_GetData(handle, j, &str, NULL);
  12017. data = script_getdata(st, j+3);
  12018. name = reference_getname(data);
  12019. if( is_string_variable(name) )
  12020. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  12021. else
  12022. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  12023. }
  12024. }
  12025. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  12026. {
  12027. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  12028. script_reportsrc(st);
  12029. }
  12030. // Free data
  12031. Sql_FreeResult(handle);
  12032. script_pushint(st, i);
  12033. return 0;
  12034. }
  12035. #endif
  12036. BUILDIN_FUNC(query_sql)
  12037. {
  12038. #ifndef TXT_ONLY
  12039. return buildin_query_sql_sub(st, mmysql_handle);
  12040. #else
  12041. //for TXT version, we always return -1
  12042. script_pushint(st,-1);
  12043. return 0;
  12044. #endif
  12045. }
  12046. BUILDIN_FUNC(query_logsql)
  12047. {
  12048. #ifndef TXT_ONLY
  12049. if( !log_config.sql_logs )
  12050. {// logmysql_handle == NULL
  12051. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  12052. script_pushint(st,-1);
  12053. return 1;
  12054. }
  12055. return buildin_query_sql_sub(st, logmysql_handle);
  12056. #else
  12057. //for TXT version, we always return -1
  12058. script_pushint(st,-1);
  12059. return 0;
  12060. #endif
  12061. }
  12062. //Allows escaping of a given string.
  12063. BUILDIN_FUNC(escape_sql)
  12064. {
  12065. const char *str;
  12066. char *esc_str;
  12067. size_t len;
  12068. str = script_getstr(st,2);
  12069. len = strlen(str);
  12070. esc_str = (char*)aMallocA(len*2+1);
  12071. #if defined(TXT_ONLY)
  12072. jstrescapecpy(esc_str, str);
  12073. #else
  12074. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  12075. #endif
  12076. script_pushstr(st, esc_str);
  12077. return 0;
  12078. }
  12079. BUILDIN_FUNC(getd)
  12080. {
  12081. char varname[100];
  12082. const char *buffer;
  12083. int elem;
  12084. buffer = script_getstr(st, 2);
  12085. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  12086. elem = 0;
  12087. // Push the 'pointer' so it's more flexible [Lance]
  12088. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  12089. return 0;
  12090. }
  12091. // <--- [zBuffer] List of dynamic var commands
  12092. // Pet stat [Lance]
  12093. BUILDIN_FUNC(petstat)
  12094. {
  12095. TBL_PC *sd = NULL;
  12096. struct pet_data *pd;
  12097. int flag = script_getnum(st,2);
  12098. sd = script_rid2sd(st);
  12099. if(!sd || !sd->status.pet_id || !sd->pd){
  12100. if(flag == 2)
  12101. script_pushconststr(st, "");
  12102. else
  12103. script_pushint(st,0);
  12104. return 0;
  12105. }
  12106. pd = sd->pd;
  12107. switch(flag){
  12108. case 1: script_pushint(st,(int)pd->pet.class_); break;
  12109. case 2: script_pushstrcopy(st, pd->pet.name); break;
  12110. case 3: script_pushint(st,(int)pd->pet.level); break;
  12111. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  12112. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  12113. default:
  12114. script_pushint(st,0);
  12115. break;
  12116. }
  12117. return 0;
  12118. }
  12119. BUILDIN_FUNC(callshop)
  12120. {
  12121. TBL_PC *sd = NULL;
  12122. struct npc_data *nd;
  12123. const char *shopname;
  12124. int flag = 0;
  12125. sd = script_rid2sd(st);
  12126. if (!sd) {
  12127. script_pushint(st,0);
  12128. return 0;
  12129. }
  12130. shopname = script_getstr(st, 2);
  12131. if( script_hasdata(st,3) )
  12132. flag = script_getnum(st,3);
  12133. nd = npc_name2id(shopname);
  12134. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  12135. {
  12136. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  12137. script_pushint(st,0);
  12138. return 1;
  12139. }
  12140. if( nd->subtype == SHOP )
  12141. {
  12142. switch( flag )
  12143. {
  12144. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  12145. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  12146. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  12147. }
  12148. }
  12149. else
  12150. clif_cashshop_show(sd, nd);
  12151. sd->npc_shopid = nd->bl.id;
  12152. script_pushint(st,1);
  12153. return 0;
  12154. }
  12155. BUILDIN_FUNC(npcshopitem)
  12156. {
  12157. const char* npcname = script_getstr(st, 2);
  12158. struct npc_data* nd = npc_name2id(npcname);
  12159. int n, i;
  12160. int amount;
  12161. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12162. { //Not found.
  12163. script_pushint(st,0);
  12164. return 0;
  12165. }
  12166. // get the count of new entries
  12167. amount = (script_lastdata(st)-2)/2;
  12168. // generate new shop item list
  12169. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  12170. for( n = 0, i = 3; n < amount; n++, i+=2 )
  12171. {
  12172. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12173. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12174. }
  12175. nd->u.shop.count = n;
  12176. script_pushint(st,1);
  12177. return 0;
  12178. }
  12179. BUILDIN_FUNC(npcshopadditem)
  12180. {
  12181. const char* npcname = script_getstr(st,2);
  12182. struct npc_data* nd = npc_name2id(npcname);
  12183. int n, i;
  12184. int amount;
  12185. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12186. { //Not found.
  12187. script_pushint(st,0);
  12188. return 0;
  12189. }
  12190. // get the count of new entries
  12191. amount = (script_lastdata(st)-2)/2;
  12192. // append new items to existing shop item list
  12193. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  12194. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  12195. {
  12196. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12197. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12198. }
  12199. nd->u.shop.count = n;
  12200. script_pushint(st,1);
  12201. return 0;
  12202. }
  12203. BUILDIN_FUNC(npcshopdelitem)
  12204. {
  12205. const char* npcname = script_getstr(st,2);
  12206. struct npc_data* nd = npc_name2id(npcname);
  12207. unsigned int nameid;
  12208. int n, i;
  12209. int amount;
  12210. int size;
  12211. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12212. { //Not found.
  12213. script_pushint(st,0);
  12214. return 0;
  12215. }
  12216. amount = script_lastdata(st)-2;
  12217. size = nd->u.shop.count;
  12218. // remove specified items from the shop item list
  12219. for( i = 3; i < 3 + amount; i++ )
  12220. {
  12221. nameid = script_getnum(st,i);
  12222. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  12223. if( n < size )
  12224. {
  12225. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  12226. size--;
  12227. }
  12228. }
  12229. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  12230. nd->u.shop.count = size;
  12231. script_pushint(st,1);
  12232. return 0;
  12233. }
  12234. //Sets a script to attach to a shop npc.
  12235. BUILDIN_FUNC(npcshopattach)
  12236. {
  12237. const char* npcname = script_getstr(st,2);
  12238. struct npc_data* nd = npc_name2id(npcname);
  12239. int flag = 1;
  12240. if( script_hasdata(st,3) )
  12241. flag = script_getnum(st,3);
  12242. if( !nd || nd->subtype != SHOP )
  12243. { //Not found.
  12244. script_pushint(st,0);
  12245. return 0;
  12246. }
  12247. if (flag)
  12248. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  12249. else
  12250. nd->master_nd = NULL;
  12251. script_pushint(st,1);
  12252. return 0;
  12253. }
  12254. /*==========================================
  12255. * Returns some values of an item [Lupus]
  12256. * Price, Weight, etc...
  12257. setitemscript(itemID,"{new item bonus script}",[n]);
  12258. Where n:
  12259. 0 - script
  12260. 1 - Equip script
  12261. 2 - Unequip script
  12262. *------------------------------------------*/
  12263. BUILDIN_FUNC(setitemscript)
  12264. {
  12265. int item_id,n=0;
  12266. const char *script;
  12267. struct item_data *i_data;
  12268. struct script_code **dstscript;
  12269. item_id = script_getnum(st,2);
  12270. script = script_getstr(st,3);
  12271. if( script_hasdata(st,4) )
  12272. n=script_getnum(st,4);
  12273. i_data = itemdb_exists(item_id);
  12274. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  12275. script_pushint(st,0);
  12276. return 0;
  12277. }
  12278. switch (n) {
  12279. case 2:
  12280. dstscript = &i_data->unequip_script;
  12281. break;
  12282. case 1:
  12283. dstscript = &i_data->equip_script;
  12284. break;
  12285. default:
  12286. dstscript = &i_data->script;
  12287. break;
  12288. }
  12289. if(*dstscript)
  12290. script_free_code(*dstscript);
  12291. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  12292. script_pushint(st,1);
  12293. return 0;
  12294. }
  12295. /* Work In Progress [Lupus]
  12296. BUILDIN_FUNC(addmonsterdrop)
  12297. {
  12298. int class_,item_id,chance;
  12299. class_=script_getnum(st,2);
  12300. item_id=script_getnum(st,3);
  12301. chance=script_getnum(st,4);
  12302. if(class_>1000 && item_id>500 && chance>0) {
  12303. script_pushint(st,1);
  12304. } else {
  12305. script_pushint(st,0);
  12306. }
  12307. }
  12308. BUILDIN_FUNC(delmonsterdrop)
  12309. {
  12310. int class_,item_id;
  12311. class_=script_getnum(st,2);
  12312. item_id=script_getnum(st,3);
  12313. if(class_>1000 && item_id>500) {
  12314. script_pushint(st,1);
  12315. } else {
  12316. script_pushint(st,0);
  12317. }
  12318. }
  12319. */
  12320. /*==========================================
  12321. * Returns some values of a monster [Lupus]
  12322. * Name, Level, race, size, etc...
  12323. getmonsterinfo(monsterID,queryIndex);
  12324. *------------------------------------------*/
  12325. BUILDIN_FUNC(getmonsterinfo)
  12326. {
  12327. struct mob_db *mob;
  12328. int mob_id;
  12329. mob_id = script_getnum(st,2);
  12330. if (!mobdb_checkid(mob_id)) {
  12331. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  12332. if ( !script_getnum(st,3) ) //requested a string
  12333. script_pushconststr(st,"null");
  12334. else
  12335. script_pushint(st,-1);
  12336. return -1;
  12337. }
  12338. mob = mob_db(mob_id);
  12339. switch ( script_getnum(st,3) ) {
  12340. case 0: script_pushstrcopy(st,mob->jname); break;
  12341. case 1: script_pushint(st,mob->lv); break;
  12342. case 2: script_pushint(st,mob->status.max_hp); break;
  12343. case 3: script_pushint(st,mob->base_exp); break;
  12344. case 4: script_pushint(st,mob->job_exp); break;
  12345. case 5: script_pushint(st,mob->status.rhw.atk); break;
  12346. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  12347. case 7: script_pushint(st,mob->status.def); break;
  12348. case 8: script_pushint(st,mob->status.mdef); break;
  12349. case 9: script_pushint(st,mob->status.str); break;
  12350. case 10: script_pushint(st,mob->status.agi); break;
  12351. case 11: script_pushint(st,mob->status.vit); break;
  12352. case 12: script_pushint(st,mob->status.int_); break;
  12353. case 13: script_pushint(st,mob->status.dex); break;
  12354. case 14: script_pushint(st,mob->status.luk); break;
  12355. case 15: script_pushint(st,mob->status.rhw.range); break;
  12356. case 16: script_pushint(st,mob->range2); break;
  12357. case 17: script_pushint(st,mob->range3); break;
  12358. case 18: script_pushint(st,mob->status.size); break;
  12359. case 19: script_pushint(st,mob->status.race); break;
  12360. case 20: script_pushint(st,mob->status.def_ele); break;
  12361. case 21: script_pushint(st,mob->status.mode); break;
  12362. default: script_pushint(st,-1); //wrong Index
  12363. }
  12364. return 0;
  12365. }
  12366. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  12367. {
  12368. TBL_PC *sd = NULL;
  12369. if(script_hasdata(st,2))
  12370. sd = map_nick2sd(script_getstr(st,2));
  12371. else
  12372. sd = script_rid2sd(st);
  12373. if(sd)
  12374. script_pushint(st,sd->state.vending);
  12375. else
  12376. script_pushint(st,0);
  12377. return 0;
  12378. }
  12379. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  12380. {
  12381. TBL_PC *sd = NULL;
  12382. if(script_hasdata(st,2))
  12383. sd = map_nick2sd(script_getstr(st,2));
  12384. else
  12385. sd = script_rid2sd(st);
  12386. if(sd)
  12387. script_pushint(st,(sd->chatID != 0));
  12388. else
  12389. script_pushint(st,0);
  12390. return 0;
  12391. }
  12392. BUILDIN_FUNC(searchitem)
  12393. {
  12394. struct script_data* data = script_getdata(st, 2);
  12395. const char *itemname = script_getstr(st,3);
  12396. struct item_data *items[MAX_SEARCH];
  12397. int count;
  12398. char* name;
  12399. int32 start;
  12400. int32 id;
  12401. int32 i;
  12402. TBL_PC* sd = NULL;
  12403. if ((items[0] = itemdb_exists(atoi(itemname))))
  12404. count = 1;
  12405. else {
  12406. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  12407. if (count > MAX_SEARCH) count = MAX_SEARCH;
  12408. }
  12409. if (!count) {
  12410. script_pushint(st, 0);
  12411. return 0;
  12412. }
  12413. if( !data_isreference(data) )
  12414. {
  12415. ShowError("script:searchitem: not a variable\n");
  12416. script_reportdata(data);
  12417. st->state = END;
  12418. return 1;// not a variable
  12419. }
  12420. id = reference_getid(data);
  12421. start = reference_getindex(data);
  12422. name = reference_getname(data);
  12423. if( not_array_variable(*name) )
  12424. {
  12425. ShowError("script:searchitem: illegal scope\n");
  12426. script_reportdata(data);
  12427. st->state = END;
  12428. return 1;// not supported
  12429. }
  12430. if( not_server_variable(*name) )
  12431. {
  12432. sd = script_rid2sd(st);
  12433. if( sd == NULL )
  12434. return 0;// no player attached
  12435. }
  12436. if( is_string_variable(name) )
  12437. {// string array
  12438. ShowError("script:searchitem: not an integer array reference\n");
  12439. script_reportdata(data);
  12440. st->state = END;
  12441. return 1;// not supported
  12442. }
  12443. for( i = 0; i < count; ++start, ++i )
  12444. {// Set array
  12445. void* v = (void*)items[i]->nameid;
  12446. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  12447. }
  12448. script_pushint(st, count);
  12449. return 0;
  12450. }
  12451. int axtoi(const char *hexStg)
  12452. {
  12453. int n = 0; // position in string
  12454. int m = 0; // position in digit[] to shift
  12455. int count; // loop index
  12456. int intValue = 0; // integer value of hex string
  12457. int digit[11]; // hold values to convert
  12458. while (n < 10) {
  12459. if (hexStg[n]=='\0')
  12460. break;
  12461. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  12462. digit[n] = hexStg[n] & 0x0f; //convert to int
  12463. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  12464. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12465. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  12466. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  12467. else break;
  12468. n++;
  12469. }
  12470. count = n;
  12471. m = n - 1;
  12472. n = 0;
  12473. while(n < count) {
  12474. // digit[n] is value of hex digit at position n
  12475. // (m << 2) is the number of positions to shift
  12476. // OR the bits into return value
  12477. intValue = intValue | (digit[n] << (m << 2));
  12478. m--; // adjust the position to set
  12479. n++; // next digit to process
  12480. }
  12481. return (intValue);
  12482. }
  12483. // [Lance] Hex string to integer converter
  12484. BUILDIN_FUNC(axtoi)
  12485. {
  12486. const char *hex = script_getstr(st,2);
  12487. script_pushint(st,axtoi(hex));
  12488. return 0;
  12489. }
  12490. // [zBuffer] List of player cont commands --->
  12491. BUILDIN_FUNC(rid2name)
  12492. {
  12493. struct block_list *bl = NULL;
  12494. int rid = script_getnum(st,2);
  12495. if((bl = map_id2bl(rid)))
  12496. {
  12497. switch(bl->type) {
  12498. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  12499. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  12500. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  12501. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  12502. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  12503. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  12504. default:
  12505. ShowError("buildin_rid2name: BL type unknown.\n");
  12506. script_pushconststr(st,"");
  12507. break;
  12508. }
  12509. } else {
  12510. ShowError("buildin_rid2name: invalid RID\n");
  12511. script_pushconststr(st,"(null)");
  12512. }
  12513. return 0;
  12514. }
  12515. BUILDIN_FUNC(pcblockmove)
  12516. {
  12517. int id, flag;
  12518. TBL_PC *sd = NULL;
  12519. id = script_getnum(st,2);
  12520. flag = script_getnum(st,3);
  12521. if(id)
  12522. sd = map_id2sd(id);
  12523. else
  12524. sd = script_rid2sd(st);
  12525. if(sd)
  12526. sd->state.blockedmove = flag > 0;
  12527. return 0;
  12528. }
  12529. BUILDIN_FUNC(pcfollow)
  12530. {
  12531. int id, targetid;
  12532. TBL_PC *sd = NULL;
  12533. id = script_getnum(st,2);
  12534. targetid = script_getnum(st,3);
  12535. if(id)
  12536. sd = map_id2sd(id);
  12537. else
  12538. sd = script_rid2sd(st);
  12539. if(sd)
  12540. pc_follow(sd, targetid);
  12541. return 0;
  12542. }
  12543. BUILDIN_FUNC(pcstopfollow)
  12544. {
  12545. int id;
  12546. TBL_PC *sd = NULL;
  12547. id = script_getnum(st,2);
  12548. if(id)
  12549. sd = map_id2sd(id);
  12550. else
  12551. sd = script_rid2sd(st);
  12552. if(sd)
  12553. pc_stop_following(sd);
  12554. return 0;
  12555. }
  12556. // <--- [zBuffer] List of player cont commands
  12557. // [zBuffer] List of mob control commands --->
  12558. //## TODO always return if the request/whatever was successfull [FlavioJS]
  12559. /// Makes the unit walk to target position or map
  12560. /// Returns if it was successfull
  12561. ///
  12562. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  12563. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  12564. BUILDIN_FUNC(unitwalk)
  12565. {
  12566. struct block_list* bl;
  12567. bl = map_id2bl(script_getnum(st,2));
  12568. if( bl == NULL )
  12569. {
  12570. script_pushint(st, 0);
  12571. }
  12572. else if( script_hasdata(st,4) )
  12573. {
  12574. int x = script_getnum(st,3);
  12575. int y = script_getnum(st,4);
  12576. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  12577. }
  12578. else
  12579. {
  12580. int map_id = script_getnum(st,3);
  12581. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  12582. }
  12583. return 0;
  12584. }
  12585. /// Kills the unit
  12586. ///
  12587. /// unitkill <unit_id>;
  12588. BUILDIN_FUNC(unitkill)
  12589. {
  12590. struct block_list* bl = map_id2bl(script_getnum(st,2));
  12591. if( bl != NULL )
  12592. status_kill(bl);
  12593. return 0;
  12594. }
  12595. /// Warps the unit to the target position in the target map
  12596. /// Returns if it was successfull
  12597. ///
  12598. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  12599. BUILDIN_FUNC(unitwarp)
  12600. {
  12601. int unit_id;
  12602. int map;
  12603. short x;
  12604. short y;
  12605. struct block_list* bl;
  12606. const char *mapname;
  12607. unit_id = script_getnum(st,2);
  12608. mapname = script_getstr(st, 3);
  12609. x = (short)script_getnum(st,4);
  12610. y = (short)script_getnum(st,5);
  12611. if (!unit_id) //Warp the script's runner
  12612. bl = map_id2bl(st->rid);
  12613. else
  12614. bl = map_id2bl(unit_id);
  12615. if( strcmp(mapname,"this") == 0 )
  12616. map = bl?bl->m:-1;
  12617. else
  12618. map = map_mapname2mapid(mapname);
  12619. if( map >= 0 && bl != NULL )
  12620. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  12621. else
  12622. script_pushint(st, 0);
  12623. return 0;
  12624. }
  12625. /// Makes the unit attack the target.
  12626. /// If the unit is a player and <action type> is not 0, it does a continuous
  12627. /// attack instead of a single attack.
  12628. /// Returns if the request was successfull.
  12629. ///
  12630. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  12631. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  12632. BUILDIN_FUNC(unitattack)
  12633. {
  12634. struct block_list* unit_bl;
  12635. struct block_list* target_bl = NULL;
  12636. struct script_data* data;
  12637. int actiontype = 0;
  12638. // get unit
  12639. unit_bl = map_id2bl(script_getnum(st,2));
  12640. if( unit_bl == NULL ) {
  12641. script_pushint(st, 0);
  12642. return 0;
  12643. }
  12644. data = script_getdata(st, 3);
  12645. get_val(st, data);
  12646. if( data_isstring(data) )
  12647. {
  12648. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  12649. if( sd != NULL )
  12650. target_bl = &sd->bl;
  12651. } else
  12652. target_bl = map_id2bl(conv_num(st, data));
  12653. // request the attack
  12654. if( target_bl == NULL )
  12655. {
  12656. script_pushint(st, 0);
  12657. return 0;
  12658. }
  12659. // get actiontype
  12660. if( script_hasdata(st,4) )
  12661. actiontype = script_getnum(st,4);
  12662. switch( unit_bl->type )
  12663. {
  12664. case BL_PC:
  12665. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  12666. script_pushint(st, 1);
  12667. return 0;
  12668. case BL_MOB:
  12669. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  12670. break;
  12671. case BL_PET:
  12672. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  12673. break;
  12674. default:
  12675. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  12676. script_pushint(st, 0);
  12677. return 1;
  12678. }
  12679. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  12680. return 0;
  12681. }
  12682. /// Makes the unit stop attacking and moving
  12683. ///
  12684. /// unitstop <unit_id>;
  12685. BUILDIN_FUNC(unitstop)
  12686. {
  12687. int unit_id;
  12688. struct block_list* bl;
  12689. unit_id = script_getnum(st,2);
  12690. bl = map_id2bl(unit_id);
  12691. if( bl != NULL )
  12692. {
  12693. unit_stop_attack(bl);
  12694. unit_stop_walking(bl,4);
  12695. if( bl->type == BL_MOB )
  12696. ((TBL_MOB*)bl)->target_id = 0;
  12697. }
  12698. return 0;
  12699. }
  12700. /// Makes the unit say the message
  12701. ///
  12702. /// unittalk <unit_id>,"<message>";
  12703. BUILDIN_FUNC(unittalk)
  12704. {
  12705. int unit_id;
  12706. const char* message;
  12707. struct block_list* bl;
  12708. unit_id = script_getnum(st,2);
  12709. message = script_getstr(st, 3);
  12710. bl = map_id2bl(unit_id);
  12711. if( bl != NULL )
  12712. {
  12713. struct StringBuf sbuf;
  12714. StringBuf_Init(&sbuf);
  12715. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  12716. clif_message(bl, StringBuf_Value(&sbuf));
  12717. if( bl->type == BL_PC )
  12718. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  12719. StringBuf_Destroy(&sbuf);
  12720. }
  12721. return 0;
  12722. }
  12723. /// Makes the unit do an emotion
  12724. ///
  12725. /// unitemote <unit_id>,<emotion>;
  12726. ///
  12727. /// @see e_* in const.txt
  12728. BUILDIN_FUNC(unitemote)
  12729. {
  12730. int unit_id;
  12731. int emotion;
  12732. struct block_list* bl;
  12733. unit_id = script_getnum(st,2);
  12734. emotion = script_getnum(st,3);
  12735. bl = map_id2bl(unit_id);
  12736. if( bl != NULL )
  12737. clif_emotion(bl, emotion);
  12738. return 0;
  12739. }
  12740. /// Makes the unit cast the skill on the target or self if no target is specified
  12741. ///
  12742. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  12743. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  12744. BUILDIN_FUNC(unitskilluseid)
  12745. {
  12746. int unit_id;
  12747. int skill_id;
  12748. int skill_lv;
  12749. int target_id;
  12750. struct block_list* bl;
  12751. unit_id = script_getnum(st,2);
  12752. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12753. skill_lv = script_getnum(st,4);
  12754. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  12755. bl = map_id2bl(unit_id);
  12756. if( bl != NULL )
  12757. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  12758. return 0;
  12759. }
  12760. /// Makes the unit cast the skill on the target position.
  12761. ///
  12762. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  12763. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  12764. BUILDIN_FUNC(unitskillusepos)
  12765. {
  12766. int unit_id;
  12767. int skill_id;
  12768. int skill_lv;
  12769. int skill_x;
  12770. int skill_y;
  12771. struct block_list* bl;
  12772. unit_id = script_getnum(st,2);
  12773. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  12774. skill_lv = script_getnum(st,4);
  12775. skill_x = script_getnum(st,5);
  12776. skill_y = script_getnum(st,6);
  12777. bl = map_id2bl(unit_id);
  12778. if( bl != NULL )
  12779. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  12780. return 0;
  12781. }
  12782. // <--- [zBuffer] List of mob control commands
  12783. /// Pauses the execution of the script, detaching the player
  12784. ///
  12785. /// sleep <mili seconds>;
  12786. BUILDIN_FUNC(sleep)
  12787. {
  12788. int ticks;
  12789. ticks = script_getnum(st,2);
  12790. // detach the player
  12791. script_detach_rid(st);
  12792. if( ticks <= 0 )
  12793. {// do nothing
  12794. }
  12795. else if( st->sleep.tick == 0 )
  12796. {// sleep for the target amount of time
  12797. st->state = RERUNLINE;
  12798. st->sleep.tick = ticks;
  12799. }
  12800. else
  12801. {// sleep time is over
  12802. st->state = RUN;
  12803. st->sleep.tick = 0;
  12804. }
  12805. return 0;
  12806. }
  12807. /// Pauses the execution of the script, keeping the player attached
  12808. /// Returns if a player is still attached
  12809. ///
  12810. /// sleep2(<mili secconds>) -> <bool>
  12811. BUILDIN_FUNC(sleep2)
  12812. {
  12813. int ticks;
  12814. ticks = script_getnum(st,2);
  12815. if( ticks <= 0 )
  12816. {// do nothing
  12817. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12818. }
  12819. else if( !st->sleep.tick )
  12820. {// sleep for the target amount of time
  12821. st->state = RERUNLINE;
  12822. st->sleep.tick = ticks;
  12823. }
  12824. else
  12825. {// sleep time is over
  12826. st->state = RUN;
  12827. st->sleep.tick = 0;
  12828. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12829. }
  12830. return 0;
  12831. }
  12832. /// Awakes all the sleep timers of the target npc
  12833. ///
  12834. /// awake "<npc name>";
  12835. BUILDIN_FUNC(awake)
  12836. {
  12837. struct npc_data* nd;
  12838. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  12839. nd = npc_name2id(script_getstr(st, 2));
  12840. if( nd == NULL ) {
  12841. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  12842. return 1;
  12843. }
  12844. while( node )
  12845. {
  12846. if( (int)node->key == nd->bl.id )
  12847. {// sleep timer for the npc
  12848. struct script_state* tst = (struct script_state*)node->data;
  12849. TBL_PC* sd = map_id2sd(tst->rid);
  12850. if( tst->sleep.timer == INVALID_TIMER )
  12851. {// already awake ???
  12852. node = node->next;
  12853. continue;
  12854. }
  12855. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  12856. {// char not online anymore / another char of the same account is online - Cancel execution
  12857. tst->state = END;
  12858. tst->rid = 0;
  12859. }
  12860. delete_timer(tst->sleep.timer, run_script_timer);
  12861. node = script_erase_sleepdb(node);
  12862. tst->sleep.timer = INVALID_TIMER;
  12863. if(tst->state != RERUNLINE)
  12864. tst->sleep.tick = 0;
  12865. run_script_main(tst);
  12866. }
  12867. else
  12868. {
  12869. node = node->next;
  12870. }
  12871. }
  12872. return 0;
  12873. }
  12874. /// Returns a reference to a variable of the target NPC.
  12875. /// Returns 0 if an error occurs.
  12876. ///
  12877. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  12878. BUILDIN_FUNC(getvariableofnpc)
  12879. {
  12880. struct script_data* data;
  12881. const char* name;
  12882. struct npc_data* nd;
  12883. data = script_getdata(st,2);
  12884. if( !data_isreference(data) )
  12885. {// Not a reference (aka varaible name)
  12886. ShowError("script:getvariableofnpc: not a variable\n");
  12887. script_reportdata(data);
  12888. script_pushnil(st);
  12889. st->state = END;
  12890. return 1;
  12891. }
  12892. name = reference_getname(data);
  12893. if( *name != '.' || name[1] == '@' )
  12894. {// not a npc variable
  12895. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  12896. script_reportdata(data);
  12897. script_pushnil(st);
  12898. st->state = END;
  12899. return 1;
  12900. }
  12901. nd = npc_name2id(script_getstr(st,3));
  12902. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  12903. {// NPC not found or has no script
  12904. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  12905. script_pushnil(st);
  12906. st->state = END;
  12907. return 1;
  12908. }
  12909. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  12910. return 0;
  12911. }
  12912. /// Opens a warp portal.
  12913. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  12914. ///
  12915. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  12916. ///
  12917. /// @author blackhole89
  12918. BUILDIN_FUNC(warpportal)
  12919. {
  12920. int spx;
  12921. int spy;
  12922. unsigned short mapindex;
  12923. int tpx;
  12924. int tpy;
  12925. struct skill_unit_group* group;
  12926. struct block_list* bl;
  12927. bl = map_id2bl(st->oid);
  12928. if( bl == NULL )
  12929. {
  12930. ShowError("script:warpportal: npc is needed\n");
  12931. return 1;
  12932. }
  12933. spx = script_getnum(st,2);
  12934. spy = script_getnum(st,3);
  12935. mapindex = mapindex_name2id(script_getstr(st, 4));
  12936. tpx = script_getnum(st,5);
  12937. tpy = script_getnum(st,6);
  12938. if( mapindex == 0 )
  12939. return 0;// map not found
  12940. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  12941. if( group == NULL )
  12942. return 0;// failed
  12943. group->val2 = (tpx<<16) | tpy;
  12944. group->val3 = mapindex;
  12945. return 0;
  12946. }
  12947. BUILDIN_FUNC(openmail)
  12948. {
  12949. TBL_PC* sd;
  12950. sd = script_rid2sd(st);
  12951. if( sd == NULL )
  12952. return 0;
  12953. #ifndef TXT_ONLY
  12954. mail_openmail(sd);
  12955. #endif
  12956. return 0;
  12957. }
  12958. BUILDIN_FUNC(openauction)
  12959. {
  12960. TBL_PC* sd;
  12961. sd = script_rid2sd(st);
  12962. if( sd == NULL )
  12963. return 0;
  12964. #ifndef TXT_ONLY
  12965. clif_Auction_openwindow(sd);
  12966. #endif
  12967. return 0;
  12968. }
  12969. /// Retrieves the value of the specified flag of the specified cell.
  12970. ///
  12971. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  12972. ///
  12973. /// @see cell_chk* constants in const.txt for the types
  12974. BUILDIN_FUNC(checkcell)
  12975. {
  12976. int m = map_mapname2mapid(script_getstr(st,2));
  12977. int x = script_getnum(st,3);
  12978. int y = script_getnum(st,4);
  12979. cell_chk type = (cell_chk)script_getnum(st,5);
  12980. script_pushint(st, map_getcell(m, x, y, type));
  12981. return 0;
  12982. }
  12983. /// Modifies flags of cells in the specified area.
  12984. ///
  12985. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  12986. ///
  12987. /// @see cell_* constants in const.txt for the types
  12988. BUILDIN_FUNC(setcell)
  12989. {
  12990. int m = map_mapname2mapid(script_getstr(st,2));
  12991. int x1 = script_getnum(st,3);
  12992. int y1 = script_getnum(st,4);
  12993. int x2 = script_getnum(st,5);
  12994. int y2 = script_getnum(st,6);
  12995. cell_t type = (cell_t)script_getnum(st,7);
  12996. bool flag = (bool)script_getnum(st,8);
  12997. int x,y;
  12998. if( x1 > x2 ) swap(x1,x2);
  12999. if( y1 > y2 ) swap(y1,y2);
  13000. for( y = y1; y <= y2; ++y )
  13001. for( x = x1; x <= x2; ++x )
  13002. map_setcell(m, x, y, type, flag);
  13003. return 0;
  13004. }
  13005. /*==========================================
  13006. * Mercenary Commands
  13007. *------------------------------------------*/
  13008. BUILDIN_FUNC(mercenary_create)
  13009. {
  13010. struct map_session_data *sd;
  13011. int class_, contract_time;
  13012. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  13013. return 0;
  13014. class_ = script_getnum(st,2);
  13015. if( !merc_class(class_) )
  13016. return 0;
  13017. contract_time = script_getnum(st,3);
  13018. merc_create(sd, class_, contract_time);
  13019. return 0;
  13020. }
  13021. BUILDIN_FUNC(mercenary_heal)
  13022. {
  13023. struct map_session_data *sd = script_rid2sd(st);
  13024. int hp, sp;
  13025. if( sd == NULL || sd->md == NULL )
  13026. return 0;
  13027. hp = script_getnum(st,2);
  13028. sp = script_getnum(st,3);
  13029. status_heal(&sd->md->bl, hp, sp, 0);
  13030. return 0;
  13031. }
  13032. BUILDIN_FUNC(mercenary_sc_start)
  13033. {
  13034. struct map_session_data *sd = script_rid2sd(st);
  13035. enum sc_type type;
  13036. int tick, val1;
  13037. if( sd == NULL || sd->md == NULL )
  13038. return 0;
  13039. type = (sc_type)script_getnum(st,2);
  13040. tick = script_getnum(st,3);
  13041. val1 = script_getnum(st,4);
  13042. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  13043. return 0;
  13044. }
  13045. BUILDIN_FUNC(mercenary_get_calls)
  13046. {
  13047. struct map_session_data *sd = script_rid2sd(st);
  13048. int guild;
  13049. if( sd == NULL )
  13050. return 0;
  13051. guild = script_getnum(st,2);
  13052. switch( guild )
  13053. {
  13054. case ARCH_MERC_GUILD:
  13055. script_pushint(st,sd->status.arch_calls);
  13056. break;
  13057. case SPEAR_MERC_GUILD:
  13058. script_pushint(st,sd->status.spear_calls);
  13059. break;
  13060. case SWORD_MERC_GUILD:
  13061. script_pushint(st,sd->status.sword_calls);
  13062. break;
  13063. default:
  13064. script_pushint(st,0);
  13065. break;
  13066. }
  13067. return 0;
  13068. }
  13069. BUILDIN_FUNC(mercenary_set_calls)
  13070. {
  13071. struct map_session_data *sd = script_rid2sd(st);
  13072. int guild, value, *calls;
  13073. if( sd == NULL )
  13074. return 0;
  13075. guild = script_getnum(st,2);
  13076. value = script_getnum(st,3);
  13077. switch( guild )
  13078. {
  13079. case ARCH_MERC_GUILD:
  13080. calls = &sd->status.arch_calls;
  13081. break;
  13082. case SPEAR_MERC_GUILD:
  13083. calls = &sd->status.spear_calls;
  13084. break;
  13085. case SWORD_MERC_GUILD:
  13086. calls = &sd->status.sword_calls;
  13087. break;
  13088. default:
  13089. return 0; // Invalid Guild
  13090. }
  13091. *calls += value;
  13092. *calls = cap_value(*calls, 0, INT_MAX);
  13093. return 0;
  13094. }
  13095. BUILDIN_FUNC(mercenary_get_faith)
  13096. {
  13097. struct map_session_data *sd = script_rid2sd(st);
  13098. int guild;
  13099. if( sd == NULL )
  13100. return 0;
  13101. guild = script_getnum(st,2);
  13102. switch( guild )
  13103. {
  13104. case ARCH_MERC_GUILD:
  13105. script_pushint(st,sd->status.arch_faith);
  13106. break;
  13107. case SPEAR_MERC_GUILD:
  13108. script_pushint(st,sd->status.spear_faith);
  13109. break;
  13110. case SWORD_MERC_GUILD:
  13111. script_pushint(st,sd->status.sword_faith);
  13112. break;
  13113. default:
  13114. script_pushint(st,0);
  13115. break;
  13116. }
  13117. return 0;
  13118. }
  13119. BUILDIN_FUNC(mercenary_set_faith)
  13120. {
  13121. struct map_session_data *sd = script_rid2sd(st);
  13122. int guild, value, *calls;
  13123. if( sd == NULL )
  13124. return 0;
  13125. guild = script_getnum(st,2);
  13126. value = script_getnum(st,3);
  13127. switch( guild )
  13128. {
  13129. case ARCH_MERC_GUILD:
  13130. calls = &sd->status.arch_faith;
  13131. break;
  13132. case SPEAR_MERC_GUILD:
  13133. calls = &sd->status.spear_faith;
  13134. break;
  13135. case SWORD_MERC_GUILD:
  13136. calls = &sd->status.sword_faith;
  13137. break;
  13138. default:
  13139. return 0; // Invalid Guild
  13140. }
  13141. *calls += value;
  13142. *calls = cap_value(*calls, 0, INT_MAX);
  13143. if( mercenary_get_guild(sd->md) == guild )
  13144. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  13145. return 0;
  13146. }
  13147. /*------------------------------------------
  13148. * Book Reading
  13149. *------------------------------------------*/
  13150. BUILDIN_FUNC(readbook)
  13151. {
  13152. struct map_session_data *sd;
  13153. int book_id, page;
  13154. if( (sd = script_rid2sd(st)) == NULL )
  13155. return 0;
  13156. book_id = script_getnum(st,2);
  13157. page = script_getnum(st,3);
  13158. clif_readbook(sd->fd, book_id, page);
  13159. return 0;
  13160. }
  13161. /******************
  13162. Questlog script commands
  13163. *******************/
  13164. BUILDIN_FUNC(setquest)
  13165. {
  13166. TBL_PC * sd = script_rid2sd(st);
  13167. quest_add(sd, script_getnum(st, 2));
  13168. return 0;
  13169. }
  13170. BUILDIN_FUNC(erasequest)
  13171. {
  13172. TBL_PC * sd = script_rid2sd(st);
  13173. quest_delete(sd, script_getnum(st, 2));
  13174. return 0;
  13175. }
  13176. BUILDIN_FUNC(completequest)
  13177. {
  13178. TBL_PC * sd = script_rid2sd(st);
  13179. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  13180. return 0;
  13181. }
  13182. BUILDIN_FUNC(changequest)
  13183. {
  13184. TBL_PC * sd = script_rid2sd(st);
  13185. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  13186. return 0;
  13187. }
  13188. BUILDIN_FUNC(checkquest)
  13189. {
  13190. TBL_PC * sd = script_rid2sd(st);
  13191. quest_check_type type = HAVEQUEST;
  13192. if( script_hasdata(st, 3) )
  13193. type = (quest_check_type)script_getnum(st, 3);
  13194. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  13195. return 0;
  13196. }
  13197. BUILDIN_FUNC(showevent)
  13198. {
  13199. TBL_PC *sd = script_rid2sd(st);
  13200. struct npc_data *nd = map_id2nd(st->oid);
  13201. int state, color;
  13202. if( sd == NULL || nd == NULL )
  13203. return 0;
  13204. state = script_getnum(st, 2);
  13205. color = script_getnum(st, 3);
  13206. if( color < 0 || color > 4 )
  13207. color = 0; // set default color
  13208. clif_quest_show_event(sd, &nd->bl, state, color);
  13209. return 0;
  13210. }
  13211. /*==========================================
  13212. * BattleGround System
  13213. *------------------------------------------*/
  13214. BUILDIN_FUNC(waitingroom2bg)
  13215. {
  13216. struct npc_data *nd;
  13217. struct chat_data *cd;
  13218. const char *map_name, *ev = "", *dev = "";
  13219. int x, y, i, mapindex = 0, bg_id, n;
  13220. struct map_session_data *sd;
  13221. if( script_hasdata(st,7) )
  13222. nd = npc_name2id(script_getstr(st,7));
  13223. else
  13224. nd = (struct npc_data *)map_id2bl(st->oid);
  13225. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  13226. {
  13227. script_pushint(st,0);
  13228. return 0;
  13229. }
  13230. map_name = script_getstr(st,2);
  13231. if( strcmp(map_name,"-") != 0 )
  13232. {
  13233. mapindex = mapindex_name2id(map_name);
  13234. if( mapindex == 0 )
  13235. { // Invalid Map
  13236. script_pushint(st,0);
  13237. return 0;
  13238. }
  13239. }
  13240. x = script_getnum(st,3);
  13241. y = script_getnum(st,4);
  13242. ev = script_getstr(st,5); // Logout Event
  13243. dev = script_getstr(st,6); // Die Event
  13244. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  13245. { // Creation failed
  13246. script_pushint(st,0);
  13247. return 0;
  13248. }
  13249. n = cd->users;
  13250. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  13251. {
  13252. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  13253. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  13254. else
  13255. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  13256. }
  13257. mapreg_setreg(add_str("$@arenamembersnum"), i);
  13258. script_pushint(st,bg_id);
  13259. return 0;
  13260. }
  13261. BUILDIN_FUNC(waitingroom2bg_single)
  13262. {
  13263. const char* map_name;
  13264. struct npc_data *nd;
  13265. struct chat_data *cd;
  13266. struct map_session_data *sd;
  13267. int x, y, mapindex, bg_id;
  13268. bg_id = script_getnum(st,2);
  13269. map_name = script_getstr(st,3);
  13270. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13271. return 0; // Invalid Map
  13272. x = script_getnum(st,4);
  13273. y = script_getnum(st,5);
  13274. nd = npc_name2id(script_getstr(st,6));
  13275. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  13276. return 0;
  13277. if( (sd = cd->usersd[0]) == NULL )
  13278. return 0;
  13279. if( bg_team_join(bg_id, sd) )
  13280. {
  13281. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  13282. script_pushint(st,1);
  13283. }
  13284. else
  13285. script_pushint(st,0);
  13286. return 0;
  13287. }
  13288. BUILDIN_FUNC(bg_team_setxy)
  13289. {
  13290. struct battleground_data *bg;
  13291. int bg_id;
  13292. bg_id = script_getnum(st,2);
  13293. if( (bg = bg_team_search(bg_id)) == NULL )
  13294. return 0;
  13295. bg->x = script_getnum(st,3);
  13296. bg->y = script_getnum(st,4);
  13297. return 0;
  13298. }
  13299. BUILDIN_FUNC(bg_warp)
  13300. {
  13301. int x, y, mapindex, bg_id;
  13302. const char* map_name;
  13303. bg_id = script_getnum(st,2);
  13304. map_name = script_getstr(st,3);
  13305. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  13306. return 0; // Invalid Map
  13307. x = script_getnum(st,4);
  13308. y = script_getnum(st,5);
  13309. bg_team_warp(bg_id, mapindex, x, y);
  13310. return 0;
  13311. }
  13312. BUILDIN_FUNC(bg_monster)
  13313. {
  13314. int class_ = 0, x = 0, y = 0, bg_id = 0;
  13315. const char *str,*map, *evt="";
  13316. bg_id = script_getnum(st,2);
  13317. map = script_getstr(st,3);
  13318. x = script_getnum(st,4);
  13319. y = script_getnum(st,5);
  13320. str = script_getstr(st,6);
  13321. class_ = script_getnum(st,7);
  13322. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  13323. check_event(st, evt);
  13324. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  13325. return 0;
  13326. }
  13327. BUILDIN_FUNC(bg_monster_set_team)
  13328. {
  13329. struct mob_data *md;
  13330. struct block_list *mbl;
  13331. int id = script_getnum(st,2),
  13332. bg_id = script_getnum(st,3);
  13333. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  13334. return 0;
  13335. md = (TBL_MOB *)mbl;
  13336. md->bg_id = bg_id;
  13337. mob_stop_attack(md);
  13338. mob_stop_walking(md, 0);
  13339. md->target_id = md->attacked_id = 0;
  13340. clif_charnameack(0, &md->bl);
  13341. return 0;
  13342. }
  13343. BUILDIN_FUNC(bg_leave)
  13344. {
  13345. struct map_session_data *sd = script_rid2sd(st);
  13346. if( sd == NULL || !sd->bg_id )
  13347. return 0;
  13348. bg_team_leave(sd,0);
  13349. return 0;
  13350. }
  13351. BUILDIN_FUNC(bg_destroy)
  13352. {
  13353. int bg_id = script_getnum(st,2);
  13354. bg_team_delete(bg_id);
  13355. return 0;
  13356. }
  13357. BUILDIN_FUNC(bg_getareausers)
  13358. {
  13359. const char *str;
  13360. int m, x0, y0, x1, y1, bg_id;
  13361. int i = 0, c = 0;
  13362. struct battleground_data *bg = NULL;
  13363. struct map_session_data *sd;
  13364. bg_id = script_getnum(st,2);
  13365. str = script_getstr(st,3);
  13366. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  13367. {
  13368. script_pushint(st,0);
  13369. return 0;
  13370. }
  13371. x0 = script_getnum(st,4);
  13372. y0 = script_getnum(st,5);
  13373. x1 = script_getnum(st,6);
  13374. y1 = script_getnum(st,7);
  13375. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  13376. {
  13377. if( (sd = bg->members[i].sd) == NULL )
  13378. continue;
  13379. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  13380. continue;
  13381. c++;
  13382. }
  13383. script_pushint(st,c);
  13384. return 0;
  13385. }
  13386. BUILDIN_FUNC(bg_updatescore)
  13387. {
  13388. const char *str;
  13389. int m;
  13390. str = script_getstr(st,2);
  13391. if( (m = map_mapname2mapid(str)) < 0 )
  13392. return 0;
  13393. map[m].bgscore_lion = script_getnum(st,3);
  13394. map[m].bgscore_eagle = script_getnum(st,4);
  13395. clif_bg_updatescore(m);
  13396. return 0;
  13397. }
  13398. BUILDIN_FUNC(bg_get_data)
  13399. {
  13400. struct battleground_data *bg;
  13401. int bg_id = script_getnum(st,2),
  13402. type = script_getnum(st,3);
  13403. if( (bg = bg_team_search(bg_id)) == NULL )
  13404. {
  13405. script_pushint(st,0);
  13406. return 0;
  13407. }
  13408. switch( type )
  13409. {
  13410. case 0: script_pushint(st, bg->count); break;
  13411. default:
  13412. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  13413. break;
  13414. }
  13415. return 0;
  13416. }
  13417. /*==========================================
  13418. * Instancing Script Commands
  13419. *------------------------------------------*/
  13420. BUILDIN_FUNC(instance_create)
  13421. {
  13422. const char *name;
  13423. int party_id, res;
  13424. name = script_getstr(st, 2);
  13425. party_id = script_getnum(st, 3);
  13426. res = instance_create(party_id, name);
  13427. if( res == -4 ) // Already exists
  13428. {
  13429. script_pushint(st, -1);
  13430. return 0;
  13431. }
  13432. else if( res < 0 )
  13433. {
  13434. const char *err;
  13435. switch(res)
  13436. {
  13437. case -3: err = "No free instances"; break;
  13438. case -2: err = "Invalid party ID"; break;
  13439. case -1: err = "Invalid type"; break;
  13440. default: err = "Unknown"; break;
  13441. }
  13442. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  13443. script_pushint(st, -2);
  13444. return 0;
  13445. }
  13446. script_pushint(st, res);
  13447. return 0;
  13448. }
  13449. BUILDIN_FUNC(instance_destroy)
  13450. {
  13451. int instance_id;
  13452. struct map_session_data *sd;
  13453. struct party_data *p;
  13454. if( script_hasdata(st, 2) )
  13455. instance_id = script_getnum(st, 2);
  13456. else if( st->instance_id )
  13457. instance_id = st->instance_id;
  13458. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13459. instance_id = p->instance_id;
  13460. else return 0;
  13461. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13462. {
  13463. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  13464. return 0;
  13465. }
  13466. instance_destroy(instance_id);
  13467. return 0;
  13468. }
  13469. BUILDIN_FUNC(instance_attachmap)
  13470. {
  13471. const char *name;
  13472. int m;
  13473. int instance_id;
  13474. bool usebasename = false;
  13475. name = script_getstr(st,2);
  13476. instance_id = script_getnum(st,3);
  13477. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  13478. usebasename = true;
  13479. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  13480. {
  13481. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  13482. script_pushconststr(st, "");
  13483. return 0;
  13484. }
  13485. script_pushconststr(st, map[m].name);
  13486. return 0;
  13487. }
  13488. BUILDIN_FUNC(instance_detachmap)
  13489. {
  13490. struct map_session_data *sd;
  13491. struct party_data *p;
  13492. const char *str;
  13493. int m, instance_id;
  13494. str = script_getstr(st, 2);
  13495. if( script_hasdata(st, 3) )
  13496. instance_id = script_getnum(st, 3);
  13497. else if( st->instance_id )
  13498. instance_id = st->instance_id;
  13499. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13500. instance_id = p->instance_id;
  13501. else return 0;
  13502. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  13503. {
  13504. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  13505. return 0;
  13506. }
  13507. instance_del_map(m);
  13508. return 0;
  13509. }
  13510. BUILDIN_FUNC(instance_attach)
  13511. {
  13512. int instance_id;
  13513. instance_id = script_getnum(st, 2);
  13514. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13515. return 0;
  13516. st->instance_id = instance_id;
  13517. return 0;
  13518. }
  13519. BUILDIN_FUNC(instance_id)
  13520. {
  13521. int type, instance_id;
  13522. struct map_session_data *sd;
  13523. struct party_data *p;
  13524. if( script_hasdata(st, 2) )
  13525. {
  13526. type = script_getnum(st, 2);
  13527. if( type == 0 )
  13528. instance_id = st->instance_id;
  13529. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  13530. instance_id = p->instance_id;
  13531. else
  13532. instance_id = 0;
  13533. }
  13534. else
  13535. instance_id = st->instance_id;
  13536. script_pushint(st, instance_id);
  13537. return 0;
  13538. }
  13539. BUILDIN_FUNC(instance_set_timeout)
  13540. {
  13541. int progress_timeout, idle_timeout;
  13542. int instance_id;
  13543. struct map_session_data *sd;
  13544. struct party_data *p;
  13545. progress_timeout = script_getnum(st, 2);
  13546. idle_timeout = script_getnum(st, 3);
  13547. if( script_hasdata(st, 4) )
  13548. instance_id = script_getnum(st, 4);
  13549. else if( st->instance_id )
  13550. instance_id = st->instance_id;
  13551. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13552. instance_id = p->instance_id;
  13553. else return 0;
  13554. if( instance_id > 0 )
  13555. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  13556. return 0;
  13557. }
  13558. BUILDIN_FUNC(instance_init)
  13559. {
  13560. int instance_id = script_getnum(st, 2);
  13561. if( instance[instance_id].state != INSTANCE_IDLE )
  13562. {
  13563. ShowError("instance_init: instance already initialized.\n");
  13564. return 0;
  13565. }
  13566. instance_init(instance_id);
  13567. return 0;
  13568. }
  13569. BUILDIN_FUNC(instance_announce)
  13570. {
  13571. int instance_id = script_getnum(st,2);
  13572. const char *mes = script_getstr(st,3);
  13573. int flag = script_getnum(st,4);
  13574. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  13575. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  13576. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  13577. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  13578. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  13579. int i;
  13580. struct map_session_data *sd;
  13581. struct party_data *p;
  13582. if( instance_id == 0 )
  13583. {
  13584. if( st->instance_id )
  13585. instance_id = st->instance_id;
  13586. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13587. instance_id = p->instance_id;
  13588. else return 0;
  13589. }
  13590. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  13591. return 0;
  13592. for( i = 0; i < instance[instance_id].num_map; i++ )
  13593. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  13594. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  13595. return 0;
  13596. }
  13597. BUILDIN_FUNC(instance_npcname)
  13598. {
  13599. const char *str;
  13600. int instance_id = 0;
  13601. struct map_session_data *sd;
  13602. struct party_data *p;
  13603. struct npc_data *nd;
  13604. str = script_getstr(st, 2);
  13605. if( script_hasdata(st, 3) )
  13606. instance_id = script_getnum(st, 3);
  13607. else if( st->instance_id )
  13608. instance_id = st->instance_id;
  13609. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13610. instance_id = p->instance_id;
  13611. if( instance_id && (nd = npc_name2id(str)) != NULL )
  13612. {
  13613. static char npcname[NAME_LENGTH];
  13614. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  13615. script_pushconststr(st,npcname);
  13616. }
  13617. else
  13618. {
  13619. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  13620. st->state = END;
  13621. return 1;
  13622. }
  13623. return 0;
  13624. }
  13625. BUILDIN_FUNC(has_instance)
  13626. {
  13627. struct map_session_data *sd;
  13628. struct party_data *p;
  13629. const char *str;
  13630. int m, instance_id = 0;
  13631. str = script_getstr(st, 2);
  13632. if( script_hasdata(st, 3) )
  13633. instance_id = script_getnum(st, 3);
  13634. else if( st->instance_id )
  13635. instance_id = st->instance_id;
  13636. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  13637. instance_id = p->instance_id;
  13638. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  13639. {
  13640. script_pushconststr(st, "");
  13641. return 0;
  13642. }
  13643. script_pushconststr(st, map[m].name);
  13644. return 0;
  13645. }
  13646. BUILDIN_FUNC(instance_warpall)
  13647. {
  13648. struct map_session_data *pl_sd;
  13649. int m, i, instance_id;
  13650. const char *mapn;
  13651. int x, y;
  13652. unsigned short mapindex;
  13653. struct party_data *p = NULL;
  13654. mapn = script_getstr(st,2);
  13655. x = script_getnum(st,3);
  13656. y = script_getnum(st,4);
  13657. if( script_hasdata(st,5) )
  13658. instance_id = script_getnum(st,5);
  13659. else if( st->instance_id )
  13660. instance_id = st->instance_id;
  13661. 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 )
  13662. instance_id = p->instance_id;
  13663. else return 0;
  13664. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  13665. return 0;
  13666. if( !(p = party_search(instance[instance_id].party_id)) )
  13667. return 0;
  13668. mapindex = map_id2index(m);
  13669. for( i = 0; i < MAX_PARTY; i++ )
  13670. 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);
  13671. return 0;
  13672. }
  13673. /*==========================================
  13674. * Custom Fonts
  13675. *------------------------------------------*/
  13676. BUILDIN_FUNC(setfont)
  13677. {
  13678. struct map_session_data *sd = script_rid2sd(st);
  13679. int font = script_getnum(st,2);
  13680. if( sd == NULL )
  13681. return 0;
  13682. if( sd->user_font != font )
  13683. sd->user_font = font;
  13684. else
  13685. sd->user_font = 0;
  13686. clif_font(sd);
  13687. return 0;
  13688. }
  13689. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  13690. {
  13691. TBL_MOB* md = (TBL_MOB*)bl;
  13692. struct block_list *tbl;
  13693. int mobid = va_arg(ap,int);
  13694. int skillid = va_arg(ap,int);
  13695. int skilllv = va_arg(ap,int);
  13696. int casttime = va_arg(ap,int);
  13697. int cancel = va_arg(ap,int);
  13698. int emotion = va_arg(ap,int);
  13699. int target = va_arg(ap,int);
  13700. if( md->class_ != mobid )
  13701. return 0;
  13702. // 0:self, 1:target, 2:master, default:random
  13703. switch( target )
  13704. {
  13705. case 0: tbl = map_id2bl(md->bl.id); break;
  13706. case 1: tbl = map_id2bl(md->target_id); break;
  13707. case 2: tbl = map_id2bl(md->master_id); break;
  13708. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  13709. }
  13710. if( !tbl )
  13711. return 0;
  13712. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  13713. unit_skillcastcancel(bl,0);
  13714. if( skill_get_casttype(skillid) == CAST_GROUND )
  13715. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  13716. else
  13717. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  13718. clif_emotion(&md->bl, emotion);
  13719. return 0;
  13720. }
  13721. /*==========================================
  13722. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  13723. *------------------------------------------*/
  13724. BUILDIN_FUNC(areamobuseskill)
  13725. {
  13726. struct block_list center;
  13727. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  13728. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  13729. {
  13730. ShowError("areamobuseskill: invalid map name.\n");
  13731. return 0;
  13732. }
  13733. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  13734. return 0;
  13735. center.m = m;
  13736. center.x = script_getnum(st,3);
  13737. center.y = script_getnum(st,4);
  13738. range = script_getnum(st,5);
  13739. mobid = script_getnum(st,6);
  13740. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  13741. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  13742. skilllv = battle_config.mob_max_skilllvl;
  13743. casttime = script_getnum(st,9);
  13744. cancel = script_getnum(st,10);
  13745. emotion = script_getnum(st,11);
  13746. target = script_getnum(st,12);
  13747. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  13748. return 0;
  13749. }
  13750. BUILDIN_FUNC(progressbar)
  13751. {
  13752. #if PACKETVER >= 20080318
  13753. struct map_session_data * sd = script_rid2sd(st);
  13754. const char * color;
  13755. unsigned int second;
  13756. if( !st || !sd )
  13757. return 0;
  13758. st->state = STOP;
  13759. color = script_getstr(st,2);
  13760. second = script_getnum(st,3);
  13761. sd->progressbar.npc_id = st->oid;
  13762. sd->progressbar.timeout = gettick() + second*1000;
  13763. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  13764. #endif
  13765. return 0;
  13766. }
  13767. BUILDIN_FUNC(pushpc)
  13768. {
  13769. int direction, cells, dx, dy;
  13770. struct map_session_data* sd;
  13771. if((sd = script_rid2sd(st))==NULL)
  13772. {
  13773. return 0;
  13774. }
  13775. direction = script_getnum(st,2);
  13776. cells = script_getnum(st,3);
  13777. if(direction<0 || direction>7)
  13778. {
  13779. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  13780. script_reportsrc(st);
  13781. direction%= 8; // trim spin-over
  13782. }
  13783. if(!cells)
  13784. {// zero distance
  13785. return 0;
  13786. }
  13787. else if(cells<0)
  13788. {// pushing backwards
  13789. direction = (direction+4)%8; // turn around
  13790. cells = -cells;
  13791. }
  13792. dx = dirx[direction];
  13793. dy = diry[direction];
  13794. unit_blown(&sd->bl, dx, dy, cells, 0);
  13795. return 0;
  13796. }
  13797. /// Invokes buying store preparation window
  13798. /// buyingstore <slots>;
  13799. BUILDIN_FUNC(buyingstore)
  13800. {
  13801. struct map_session_data* sd;
  13802. if( ( sd = script_rid2sd(st) ) == NULL )
  13803. {
  13804. return 0;
  13805. }
  13806. buyingstore_setup(sd, script_getnum(st,2));
  13807. return 0;
  13808. }
  13809. /// Invokes search store info window
  13810. /// searchstores <uses>,<effect>;
  13811. BUILDIN_FUNC(searchstores)
  13812. {
  13813. unsigned short effect;
  13814. unsigned int uses;
  13815. struct map_session_data* sd;
  13816. if( ( sd = script_rid2sd(st) ) == NULL )
  13817. {
  13818. return 0;
  13819. }
  13820. uses = script_getnum(st,2);
  13821. effect = script_getnum(st,3);
  13822. if( !uses )
  13823. {
  13824. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  13825. return 1;
  13826. }
  13827. if( effect > 1 )
  13828. {
  13829. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  13830. return 1;
  13831. }
  13832. searchstore_open(sd, uses, effect);
  13833. return 0;
  13834. }
  13835. /// Displays a number as large digital clock.
  13836. /// showdigit <value>[,<type>];
  13837. BUILDIN_FUNC(showdigit)
  13838. {
  13839. unsigned int type = 0;
  13840. int value;
  13841. struct map_session_data* sd;
  13842. if( ( sd = script_rid2sd(st) ) == NULL )
  13843. {
  13844. return 0;
  13845. }
  13846. value = script_getnum(st,2);
  13847. if( script_hasdata(st,3) )
  13848. {
  13849. type = script_getnum(st,3);
  13850. if( type > 3 )
  13851. {
  13852. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  13853. return 1;
  13854. }
  13855. }
  13856. clif_showdigit(sd, (unsigned char)type, value);
  13857. return 0;
  13858. }
  13859. /**
  13860. * Rune Knight
  13861. **/
  13862. BUILDIN_FUNC(makerune) {
  13863. TBL_PC* sd;
  13864. if( (sd = script_rid2sd(st)) == NULL )
  13865. return 0;
  13866. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  13867. sd->itemid = script_getnum(st,2);
  13868. return 0;
  13869. }
  13870. /**
  13871. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  13872. **/
  13873. BUILDIN_FUNC(checkdragon) {
  13874. TBL_PC* sd;
  13875. if( (sd = script_rid2sd(st)) == NULL )
  13876. return 0;
  13877. if( sd->sc.option&OPTION_DRAGON )
  13878. script_pushint(st,1);
  13879. else
  13880. script_pushint(st,0);
  13881. return 0;
  13882. }
  13883. /**
  13884. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  13885. * - Toggles the dragon on a RK if he can mount;
  13886. * @param Color - when not provided uses the green dragon;
  13887. * - 1 : Green Dragon
  13888. * - 2 : Brown Dragon
  13889. * - 3 : Gray Dragon
  13890. * - 4 : Blue Dragon
  13891. * - 5 : Red Dragon
  13892. **/
  13893. BUILDIN_FUNC(setdragon) {
  13894. TBL_PC* sd;
  13895. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  13896. unsigned int option = OPTION_DRAGON1;
  13897. if( (sd = script_rid2sd(st)) == NULL )
  13898. return 0;
  13899. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  13900. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  13901. else if ( sd->sc.option&OPTION_DRAGON ) {//Is mounted; release
  13902. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  13903. script_pushint(st,1);
  13904. } else {//Not mounted; Mount now.
  13905. if( color ) {
  13906. option = ( color == 1 ? OPTION_DRAGON1 :
  13907. color == 2 ? OPTION_DRAGON2 :
  13908. color == 3 ? OPTION_DRAGON3 :
  13909. color == 4 ? OPTION_DRAGON4 :
  13910. color == 5 ? OPTION_DRAGON5 : 0);
  13911. if( !option ) {
  13912. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  13913. option = OPTION_DRAGON1;
  13914. }
  13915. }
  13916. pc_setoption(sd, sd->sc.option|option);
  13917. script_pushint(st,1);
  13918. }
  13919. return 0;
  13920. }
  13921. /**
  13922. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  13923. **/
  13924. BUILDIN_FUNC(ismounting) {
  13925. TBL_PC* sd;
  13926. if( (sd = script_rid2sd(st)) == NULL )
  13927. return 0;
  13928. if( sd->sc.option&OPTION_MOUNTING )
  13929. script_pushint(st,1);
  13930. else
  13931. script_pushint(st,0);
  13932. return 0;
  13933. }
  13934. /**
  13935. * setmounting() returns 1 on success or 0 otherwise
  13936. * - Toggles new mounts on a player when he can mount
  13937. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  13938. * - Will unmount the player is he is already mounting
  13939. **/
  13940. BUILDIN_FUNC(setmounting) {
  13941. TBL_PC* sd;
  13942. if( (sd = script_rid2sd(st)) == NULL )
  13943. return 0;
  13944. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  13945. script_pushint(st,0);//can't mount with one of these
  13946. else {
  13947. if( sd->sc.option&OPTION_MOUNTING )
  13948. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  13949. else
  13950. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  13951. script_pushint(st,1);//in both cases, return 1.
  13952. }
  13953. return 0;
  13954. }
  13955. // declarations that were supposed to be exported from npc_chat.c
  13956. #ifdef PCRE_SUPPORT
  13957. BUILDIN_FUNC(defpattern);
  13958. BUILDIN_FUNC(activatepset);
  13959. BUILDIN_FUNC(deactivatepset);
  13960. BUILDIN_FUNC(deletepset);
  13961. #endif
  13962. /// script command definitions
  13963. /// for an explanation on args, see add_buildin_func
  13964. struct script_function buildin_func[] = {
  13965. // NPC interaction
  13966. BUILDIN_DEF(mes,"s*"),
  13967. BUILDIN_DEF(next,""),
  13968. BUILDIN_DEF(close,""),
  13969. BUILDIN_DEF(close2,""),
  13970. BUILDIN_DEF(menu,"sl*"),
  13971. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  13972. BUILDIN_DEF(prompt,"s*"),
  13973. //
  13974. BUILDIN_DEF(goto,"l"),
  13975. BUILDIN_DEF(callsub,"l*"),
  13976. BUILDIN_DEF(callfunc,"s*"),
  13977. BUILDIN_DEF(return,"?"),
  13978. BUILDIN_DEF(getarg,"i?"),
  13979. BUILDIN_DEF(jobchange,"i?"),
  13980. BUILDIN_DEF(jobname,"i"),
  13981. BUILDIN_DEF(input,"r??"),
  13982. BUILDIN_DEF(warp,"sii"),
  13983. BUILDIN_DEF(areawarp,"siiiisii"),
  13984. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  13985. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  13986. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  13987. BUILDIN_DEF(setlook,"ii"),
  13988. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  13989. BUILDIN_DEF(set,"rv"),
  13990. BUILDIN_DEF(setarray,"rv*"),
  13991. BUILDIN_DEF(cleararray,"rvi"),
  13992. BUILDIN_DEF(copyarray,"rri"),
  13993. BUILDIN_DEF(getarraysize,"r"),
  13994. BUILDIN_DEF(deletearray,"r?"),
  13995. BUILDIN_DEF(getelementofarray,"ri"),
  13996. BUILDIN_DEF(getitem,"vi?"),
  13997. BUILDIN_DEF(rentitem,"vi"),
  13998. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  13999. BUILDIN_DEF(getnameditem,"vv"),
  14000. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  14001. BUILDIN_DEF(makeitem,"visii"),
  14002. BUILDIN_DEF(delitem,"vi?"),
  14003. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  14004. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  14005. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  14006. BUILDIN_DEF(cutin,"si"),
  14007. BUILDIN_DEF(viewpoint,"iiiii"),
  14008. BUILDIN_DEF(heal,"ii"),
  14009. BUILDIN_DEF(itemheal,"ii"),
  14010. BUILDIN_DEF(percentheal,"ii"),
  14011. BUILDIN_DEF(rand,"i?"),
  14012. BUILDIN_DEF(countitem,"v"),
  14013. BUILDIN_DEF(countitem2,"viiiiiii"),
  14014. BUILDIN_DEF(checkweight,"vi"),
  14015. BUILDIN_DEF(readparam,"i?"),
  14016. BUILDIN_DEF(getcharid,"i?"),
  14017. BUILDIN_DEF(getpartyname,"i"),
  14018. BUILDIN_DEF(getpartymember,"i?"),
  14019. BUILDIN_DEF(getpartyleader,"i?"),
  14020. BUILDIN_DEF(getguildname,"i"),
  14021. BUILDIN_DEF(getguildmaster,"i"),
  14022. BUILDIN_DEF(getguildmasterid,"i"),
  14023. BUILDIN_DEF(strcharinfo,"i"),
  14024. BUILDIN_DEF(strnpcinfo,"i"),
  14025. BUILDIN_DEF(getequipid,"i"),
  14026. BUILDIN_DEF(getequipname,"i"),
  14027. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  14028. BUILDIN_DEF(repair,"i"), // [Valaris]
  14029. BUILDIN_DEF(getequipisequiped,"i"),
  14030. BUILDIN_DEF(getequipisenableref,"i"),
  14031. BUILDIN_DEF(getequipisidentify,"i"),
  14032. BUILDIN_DEF(getequiprefinerycnt,"i"),
  14033. BUILDIN_DEF(getequipweaponlv,"i"),
  14034. BUILDIN_DEF(getequippercentrefinery,"i"),
  14035. BUILDIN_DEF(successrefitem,"i"),
  14036. BUILDIN_DEF(failedrefitem,"i"),
  14037. BUILDIN_DEF(statusup,"i"),
  14038. BUILDIN_DEF(statusup2,"ii"),
  14039. BUILDIN_DEF(bonus,"iv"),
  14040. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  14041. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  14042. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  14043. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  14044. BUILDIN_DEF(autobonus,"sii??"),
  14045. BUILDIN_DEF(autobonus2,"sii??"),
  14046. BUILDIN_DEF(autobonus3,"siiv?"),
  14047. BUILDIN_DEF(skill,"vi?"),
  14048. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  14049. BUILDIN_DEF(guildskill,"vi"),
  14050. BUILDIN_DEF(getskilllv,"v"),
  14051. BUILDIN_DEF(getgdskilllv,"iv"),
  14052. BUILDIN_DEF(basicskillcheck,""),
  14053. BUILDIN_DEF(getgmlevel,""),
  14054. BUILDIN_DEF(end,""),
  14055. BUILDIN_DEF(checkoption,"i"),
  14056. BUILDIN_DEF(setoption,"i?"),
  14057. BUILDIN_DEF(setcart,"?"),
  14058. BUILDIN_DEF(checkcart,""),
  14059. BUILDIN_DEF(setfalcon,"?"),
  14060. BUILDIN_DEF(checkfalcon,""),
  14061. BUILDIN_DEF(setriding,"?"),
  14062. BUILDIN_DEF(checkriding,""),
  14063. BUILDIN_DEF2(savepoint,"save","sii"),
  14064. BUILDIN_DEF(savepoint,"sii"),
  14065. BUILDIN_DEF(gettimetick,"i"),
  14066. BUILDIN_DEF(gettime,"i"),
  14067. BUILDIN_DEF(gettimestr,"si"),
  14068. BUILDIN_DEF(openstorage,""),
  14069. BUILDIN_DEF(guildopenstorage,""),
  14070. BUILDIN_DEF(itemskill,"vi"),
  14071. BUILDIN_DEF(produce,"i"),
  14072. BUILDIN_DEF(cooking,"i"),
  14073. BUILDIN_DEF(monster,"siisii?"),
  14074. BUILDIN_DEF(getmobdrops,"i"),
  14075. BUILDIN_DEF(areamonster,"siiiisii?"),
  14076. BUILDIN_DEF(killmonster,"ss?"),
  14077. BUILDIN_DEF(killmonsterall,"s?"),
  14078. BUILDIN_DEF(clone,"siisi????"),
  14079. BUILDIN_DEF(doevent,"s"),
  14080. BUILDIN_DEF(donpcevent,"s"),
  14081. BUILDIN_DEF(cmdothernpc,"ss"),
  14082. BUILDIN_DEF(addtimer,"is"),
  14083. BUILDIN_DEF(deltimer,"s"),
  14084. BUILDIN_DEF(addtimercount,"si"),
  14085. BUILDIN_DEF(initnpctimer,"??"),
  14086. BUILDIN_DEF(stopnpctimer,"??"),
  14087. BUILDIN_DEF(startnpctimer,"??"),
  14088. BUILDIN_DEF(setnpctimer,"i?"),
  14089. BUILDIN_DEF(getnpctimer,"i?"),
  14090. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  14091. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  14092. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  14093. BUILDIN_DEF(announce,"si?????"),
  14094. BUILDIN_DEF(mapannounce,"ssi?????"),
  14095. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  14096. BUILDIN_DEF(getusers,"i"),
  14097. BUILDIN_DEF(getmapguildusers,"si"),
  14098. BUILDIN_DEF(getmapusers,"s"),
  14099. BUILDIN_DEF(getareausers,"siiii"),
  14100. BUILDIN_DEF(getareadropitem,"siiiiv"),
  14101. BUILDIN_DEF(enablenpc,"s"),
  14102. BUILDIN_DEF(disablenpc,"s"),
  14103. BUILDIN_DEF(hideoffnpc,"s"),
  14104. BUILDIN_DEF(hideonnpc,"s"),
  14105. BUILDIN_DEF(sc_start,"iii?"),
  14106. BUILDIN_DEF(sc_start2,"iiii?"),
  14107. BUILDIN_DEF(sc_start4,"iiiiii?"),
  14108. BUILDIN_DEF(sc_end,"i?"),
  14109. BUILDIN_DEF(getstatus, "i?"),
  14110. BUILDIN_DEF(getscrate,"ii?"),
  14111. BUILDIN_DEF(debugmes,"s"),
  14112. BUILDIN_DEF2(catchpet,"pet","i"),
  14113. BUILDIN_DEF2(birthpet,"bpet",""),
  14114. BUILDIN_DEF(resetlvl,"i"),
  14115. BUILDIN_DEF(resetstatus,""),
  14116. BUILDIN_DEF(resetskill,""),
  14117. BUILDIN_DEF(skillpointcount,""),
  14118. BUILDIN_DEF(changebase,"i?"),
  14119. BUILDIN_DEF(changesex,""),
  14120. BUILDIN_DEF(waitingroom,"si?????"),
  14121. BUILDIN_DEF(delwaitingroom,"?"),
  14122. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  14123. BUILDIN_DEF(enablewaitingroomevent,"?"),
  14124. BUILDIN_DEF(disablewaitingroomevent,"?"),
  14125. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  14126. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  14127. BUILDIN_DEF(getwaitingroomstate,"i?"),
  14128. BUILDIN_DEF(warpwaitingpc,"sii?"),
  14129. BUILDIN_DEF(attachrid,"i"),
  14130. BUILDIN_DEF(detachrid,""),
  14131. BUILDIN_DEF(isloggedin,"i?"),
  14132. BUILDIN_DEF(setmapflagnosave,"ssii"),
  14133. BUILDIN_DEF(getmapflag,"si"),
  14134. BUILDIN_DEF(setmapflag,"si?"),
  14135. BUILDIN_DEF(removemapflag,"si"),
  14136. BUILDIN_DEF(pvpon,"s"),
  14137. BUILDIN_DEF(pvpoff,"s"),
  14138. BUILDIN_DEF(gvgon,"s"),
  14139. BUILDIN_DEF(gvgoff,"s"),
  14140. BUILDIN_DEF(emotion,"i??"),
  14141. BUILDIN_DEF(maprespawnguildid,"sii"),
  14142. BUILDIN_DEF(agitstart,""), // <Agit>
  14143. BUILDIN_DEF(agitend,""),
  14144. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  14145. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  14146. BUILDIN_DEF(getcastlename,"s"),
  14147. BUILDIN_DEF(getcastledata,"si?"),
  14148. BUILDIN_DEF(setcastledata,"sii"),
  14149. BUILDIN_DEF(requestguildinfo,"i?"),
  14150. BUILDIN_DEF(getequipcardcnt,"i"),
  14151. BUILDIN_DEF(successremovecards,"i"),
  14152. BUILDIN_DEF(failedremovecards,"ii"),
  14153. BUILDIN_DEF(marriage,"s"),
  14154. BUILDIN_DEF2(wedding_effect,"wedding",""),
  14155. BUILDIN_DEF(divorce,""),
  14156. BUILDIN_DEF(ispartneron,""),
  14157. BUILDIN_DEF(getpartnerid,""),
  14158. BUILDIN_DEF(getchildid,""),
  14159. BUILDIN_DEF(getmotherid,""),
  14160. BUILDIN_DEF(getfatherid,""),
  14161. BUILDIN_DEF(warppartner,"sii"),
  14162. BUILDIN_DEF(getitemname,"v"),
  14163. BUILDIN_DEF(getitemslots,"i"),
  14164. BUILDIN_DEF(makepet,"i"),
  14165. BUILDIN_DEF(getexp,"ii"),
  14166. BUILDIN_DEF(getinventorylist,""),
  14167. BUILDIN_DEF(getskilllist,""),
  14168. BUILDIN_DEF(clearitem,""),
  14169. BUILDIN_DEF(classchange,"ii"),
  14170. BUILDIN_DEF(misceffect,"i"),
  14171. BUILDIN_DEF(playBGM,"s"),
  14172. BUILDIN_DEF(playBGMall,"s?????"),
  14173. BUILDIN_DEF(soundeffect,"si"),
  14174. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  14175. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  14176. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  14177. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  14178. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  14179. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  14180. BUILDIN_DEF(petloot,"i"), // [Valaris]
  14181. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  14182. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  14183. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  14184. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  14185. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  14186. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  14187. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  14188. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  14189. BUILDIN_DEF(nude,""), // nude command [Valaris]
  14190. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  14191. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  14192. BUILDIN_DEF(charcommand,"s"), // [MouseJstr]
  14193. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  14194. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  14195. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  14196. BUILDIN_DEF(mobcount,"ss"),
  14197. BUILDIN_DEF(getlook,"i"),
  14198. BUILDIN_DEF(getsavepoint,"i"),
  14199. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  14200. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  14201. BUILDIN_DEF(npcstop,""), // [Valaris]
  14202. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  14203. BUILDIN_DEF(checkoption1,"i"),
  14204. BUILDIN_DEF(checkoption2,"i"),
  14205. BUILDIN_DEF(guildgetexp,"i"),
  14206. BUILDIN_DEF(guildchangegm,"is"),
  14207. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  14208. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  14209. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  14210. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  14211. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  14212. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  14213. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  14214. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  14215. BUILDIN_DEF(night,""), // sets the server to night time
  14216. BUILDIN_DEF(day,""), // sets the server to day time
  14217. #ifdef PCRE_SUPPORT
  14218. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  14219. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  14220. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  14221. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  14222. #endif
  14223. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  14224. BUILDIN_DEF(getusersname,""),
  14225. BUILDIN_DEF(recovery,""),
  14226. BUILDIN_DEF(getpetinfo,"i"),
  14227. BUILDIN_DEF(gethominfo,"i"),
  14228. BUILDIN_DEF(getmercinfo,"i?"),
  14229. BUILDIN_DEF(checkequipedcard,"i"),
  14230. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  14231. BUILDIN_DEF(globalmes,"s?"),
  14232. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  14233. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  14234. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  14235. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  14236. BUILDIN_DEF(charat,"si"),
  14237. BUILDIN_DEF(setchar,"ssi"),
  14238. BUILDIN_DEF(insertchar,"ssi"),
  14239. BUILDIN_DEF(delchar,"si"),
  14240. BUILDIN_DEF(strtoupper,"s"),
  14241. BUILDIN_DEF(strtolower,"s"),
  14242. BUILDIN_DEF(charisupper, "si"),
  14243. BUILDIN_DEF(charislower, "si"),
  14244. BUILDIN_DEF(substr,"sii"),
  14245. BUILDIN_DEF(explode, "rss"),
  14246. BUILDIN_DEF(implode, "r?"),
  14247. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  14248. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  14249. BUILDIN_DEF(strpos,"ss?"),
  14250. BUILDIN_DEF(replacestr,"sss??"),
  14251. BUILDIN_DEF(countstr,"ss?"),
  14252. BUILDIN_DEF(setnpcdisplay,"sv??"),
  14253. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  14254. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  14255. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  14256. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  14257. // [zBuffer] List of mathematics commands --->
  14258. BUILDIN_DEF(sqrt,"i"),
  14259. BUILDIN_DEF(pow,"ii"),
  14260. BUILDIN_DEF(distance,"iiii"),
  14261. // <--- [zBuffer] List of mathematics commands
  14262. BUILDIN_DEF(md5,"s"),
  14263. // [zBuffer] List of dynamic var commands --->
  14264. BUILDIN_DEF(getd,"s"),
  14265. BUILDIN_DEF(setd,"sv"),
  14266. // <--- [zBuffer] List of dynamic var commands
  14267. BUILDIN_DEF(petstat,"i"),
  14268. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  14269. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  14270. BUILDIN_DEF(npcshopadditem,"sii*"),
  14271. BUILDIN_DEF(npcshopdelitem,"si*"),
  14272. BUILDIN_DEF(npcshopattach,"s?"),
  14273. BUILDIN_DEF(equip,"i"),
  14274. BUILDIN_DEF(autoequip,"ii"),
  14275. BUILDIN_DEF(setbattleflag,"si"),
  14276. BUILDIN_DEF(getbattleflag,"s"),
  14277. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  14278. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  14279. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  14280. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  14281. BUILDIN_DEF(axtoi,"s"),
  14282. BUILDIN_DEF(query_sql,"s*"),
  14283. BUILDIN_DEF(query_logsql,"s*"),
  14284. BUILDIN_DEF(escape_sql,"s"),
  14285. BUILDIN_DEF(atoi,"s"),
  14286. // [zBuffer] List of player cont commands --->
  14287. BUILDIN_DEF(rid2name,"i"),
  14288. BUILDIN_DEF(pcfollow,"ii"),
  14289. BUILDIN_DEF(pcstopfollow,"i"),
  14290. BUILDIN_DEF(pcblockmove,"ii"),
  14291. // <--- [zBuffer] List of player cont commands
  14292. // [zBuffer] List of mob control commands --->
  14293. BUILDIN_DEF(unitwalk,"ii?"),
  14294. BUILDIN_DEF(unitkill,"i"),
  14295. BUILDIN_DEF(unitwarp,"isii"),
  14296. BUILDIN_DEF(unitattack,"iv?"),
  14297. BUILDIN_DEF(unitstop,"i"),
  14298. BUILDIN_DEF(unittalk,"is"),
  14299. BUILDIN_DEF(unitemote,"ii"),
  14300. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  14301. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  14302. // <--- [zBuffer] List of mob control commands
  14303. BUILDIN_DEF(sleep,"i"),
  14304. BUILDIN_DEF(sleep2,"i"),
  14305. BUILDIN_DEF(awake,"s"),
  14306. BUILDIN_DEF(getvariableofnpc,"rs"),
  14307. BUILDIN_DEF(warpportal,"iisii"),
  14308. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  14309. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  14310. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  14311. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  14312. BUILDIN_DEF(checkvending,"?"),
  14313. BUILDIN_DEF(checkchatting,"?"),
  14314. BUILDIN_DEF(openmail,""),
  14315. BUILDIN_DEF(openauction,""),
  14316. BUILDIN_DEF(checkcell,"siii"),
  14317. BUILDIN_DEF(setcell,"siiiiii"),
  14318. BUILDIN_DEF(setwall,"siiiiis"),
  14319. BUILDIN_DEF(delwall,"s"),
  14320. BUILDIN_DEF(searchitem,"rs"),
  14321. BUILDIN_DEF(mercenary_create,"ii"),
  14322. BUILDIN_DEF(mercenary_heal,"ii"),
  14323. BUILDIN_DEF(mercenary_sc_start,"iii"),
  14324. BUILDIN_DEF(mercenary_get_calls,"i"),
  14325. BUILDIN_DEF(mercenary_get_faith,"i"),
  14326. BUILDIN_DEF(mercenary_set_calls,"ii"),
  14327. BUILDIN_DEF(mercenary_set_faith,"ii"),
  14328. BUILDIN_DEF(readbook,"ii"),
  14329. BUILDIN_DEF(setfont,"i"),
  14330. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  14331. BUILDIN_DEF(progressbar,"si"),
  14332. BUILDIN_DEF(pushpc,"ii"),
  14333. BUILDIN_DEF(buyingstore,"i"),
  14334. BUILDIN_DEF(searchstores,"ii"),
  14335. BUILDIN_DEF(showdigit,"i?"),
  14336. // WoE SE
  14337. BUILDIN_DEF(agitstart2,""),
  14338. BUILDIN_DEF(agitend2,""),
  14339. BUILDIN_DEF(agitcheck2,""),
  14340. // BattleGround
  14341. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  14342. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  14343. BUILDIN_DEF(bg_team_setxy,"iii"),
  14344. BUILDIN_DEF(bg_warp,"isii"),
  14345. BUILDIN_DEF(bg_monster,"isiisi?"),
  14346. BUILDIN_DEF(bg_monster_set_team,"ii"),
  14347. BUILDIN_DEF(bg_leave,""),
  14348. BUILDIN_DEF(bg_destroy,"i"),
  14349. BUILDIN_DEF(areapercentheal,"siiiiii"),
  14350. BUILDIN_DEF(bg_get_data,"ii"),
  14351. BUILDIN_DEF(bg_getareausers,"isiiii"),
  14352. BUILDIN_DEF(bg_updatescore,"sii"),
  14353. // Instancing
  14354. BUILDIN_DEF(instance_create,"si"),
  14355. BUILDIN_DEF(instance_destroy,"?"),
  14356. BUILDIN_DEF(instance_attachmap,"si?"),
  14357. BUILDIN_DEF(instance_detachmap,"s?"),
  14358. BUILDIN_DEF(instance_attach,"i"),
  14359. BUILDIN_DEF(instance_id,"?"),
  14360. BUILDIN_DEF(instance_set_timeout,"ii?"),
  14361. BUILDIN_DEF(instance_init,"i"),
  14362. BUILDIN_DEF(instance_announce,"isi?????"),
  14363. BUILDIN_DEF(instance_npcname,"s?"),
  14364. BUILDIN_DEF(has_instance,"s?"),
  14365. BUILDIN_DEF(instance_warpall,"sii?"),
  14366. /**
  14367. * 3rd-related
  14368. **/
  14369. BUILDIN_DEF(makerune,"i"),
  14370. BUILDIN_DEF(checkdragon,""),//[Ind]
  14371. BUILDIN_DEF(setdragon,"?"),//[Ind]
  14372. BUILDIN_DEF(ismounting,""),//[Ind]
  14373. BUILDIN_DEF(setmounting,""),//[Ind]
  14374. //Quest Log System [Inkfish]
  14375. BUILDIN_DEF(setquest, "i"),
  14376. BUILDIN_DEF(erasequest, "i"),
  14377. BUILDIN_DEF(completequest, "i"),
  14378. BUILDIN_DEF(checkquest, "i?"),
  14379. BUILDIN_DEF(changequest, "ii"),
  14380. BUILDIN_DEF(showevent, "ii"),
  14381. {NULL,NULL,NULL},
  14382. };