script.c 371 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348
  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. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  153. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  154. // String buffer structures.
  155. // str_data stores string information
  156. static struct str_data_struct {
  157. enum c_op type;
  158. int str;
  159. int backpatch;
  160. int label;
  161. int (*func)(struct script_state *st);
  162. int val;
  163. int next;
  164. } *str_data = NULL;
  165. static int str_data_size = 0; // size of the data
  166. static int str_num = LABEL_START; // next id to be assigned
  167. // str_buf holds the strings themselves
  168. static char *str_buf;
  169. static int str_size = 0; // size of the buffer
  170. static int str_pos = 0; // next position to be assigned
  171. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  172. #define SCRIPT_HASH_SIZE 1021
  173. int str_hash[SCRIPT_HASH_SIZE];
  174. // Specifies which string hashing method to use
  175. //#define SCRIPT_HASH_DJB2
  176. //#define SCRIPT_HASH_SDBM
  177. #define SCRIPT_HASH_ELF
  178. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  179. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  180. static int parse_options=0;
  181. DBMap* script_get_label_db(){ return scriptlabel_db; }
  182. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  183. // Caches compiled autoscript item code.
  184. // Note: This is not cleared when reloading itemdb.
  185. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  186. struct Script_Config script_config = {
  187. 1, // warn_func_mismatch_argtypes
  188. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  189. 0, INT_MAX, // input_min_value/input_max_value
  190. "OnPCDieEvent", //die_event_name
  191. "OnPCKillEvent", //kill_pc_event_name
  192. "OnNPCKillEvent", //kill_mob_event_name
  193. "OnPCLoginEvent", //login_event_name
  194. "OnPCLogoutEvent", //logout_event_name
  195. "OnPCLoadMapEvent", //loadmap_event_name
  196. "OnPCBaseLvUpEvent", //baselvup_event_name
  197. "OnPCJobLvUpEvent", //joblvup_event_name
  198. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  199. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  200. };
  201. static jmp_buf error_jump;
  202. static char* error_msg;
  203. static const char* error_pos;
  204. static int error_report; // if the error should produce output
  205. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  206. // [Eoe / jA 1080, 1081, 1094, 1164]
  207. enum curly_type {
  208. TYPE_NULL = 0,
  209. TYPE_IF,
  210. TYPE_SWITCH,
  211. TYPE_WHILE,
  212. TYPE_FOR,
  213. TYPE_DO,
  214. TYPE_USERFUNC,
  215. TYPE_ARGLIST // function argument list
  216. };
  217. enum e_arglist
  218. {
  219. ARGLIST_UNDEFINED = 0,
  220. ARGLIST_NO_PAREN = 1,
  221. ARGLIST_PAREN = 2,
  222. };
  223. static struct {
  224. struct {
  225. enum curly_type type;
  226. int index;
  227. int count;
  228. int flag;
  229. struct linkdb_node *case_label;
  230. } curly[256]; // 右カッコの情報
  231. int curly_count; // 右カッコの数
  232. int index; // スクリプト内で使用した構文の数
  233. } syntax;
  234. const char* parse_curly_close(const char* p);
  235. const char* parse_syntax_close(const char* p);
  236. const char* parse_syntax_close_sub(const char* p,int* flag);
  237. const char* parse_syntax(const char* p);
  238. static int parse_syntax_for_flag = 0;
  239. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  240. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  241. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  242. int potion_target=0;
  243. c_op get_com(unsigned char *script,int *pos);
  244. int get_num(unsigned char *script,int *pos);
  245. typedef struct script_function {
  246. int (*func)(struct script_state *st);
  247. const char *name;
  248. const char *arg;
  249. } script_function;
  250. extern script_function buildin_func[];
  251. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  252. /*==========================================
  253. * ローカルプロトタイプ宣言 (必要な物のみ)
  254. *------------------------------------------*/
  255. const char* parse_subexpr(const char* p,int limit);
  256. int run_func(struct script_state *st);
  257. enum {
  258. MF_NOMEMO, //0
  259. MF_NOTELEPORT,
  260. MF_NOSAVE,
  261. MF_NOBRANCH,
  262. MF_NOPENALTY,
  263. MF_NOZENYPENALTY,
  264. MF_PVP,
  265. MF_PVP_NOPARTY,
  266. MF_PVP_NOGUILD,
  267. MF_GVG,
  268. MF_GVG_NOPARTY, //10
  269. MF_NOTRADE,
  270. MF_NOSKILL,
  271. MF_NOWARP,
  272. MF_PARTYLOCK,
  273. MF_NOICEWALL,
  274. MF_SNOW,
  275. MF_FOG,
  276. MF_SAKURA,
  277. MF_LEAVES,
  278. MF_RAIN, //20
  279. // 21 free
  280. MF_NOGO = 22,
  281. MF_CLOUDS,
  282. MF_CLOUDS2,
  283. MF_FIREWORKS,
  284. MF_GVG_CASTLE,
  285. MF_GVG_DUNGEON,
  286. MF_NIGHTENABLED,
  287. MF_NOBASEEXP,
  288. MF_NOJOBEXP, //30
  289. MF_NOMOBLOOT,
  290. MF_NOMVPLOOT,
  291. MF_NORETURN,
  292. MF_NOWARPTO,
  293. MF_NIGHTMAREDROP,
  294. MF_RESTRICTED,
  295. MF_NOCOMMAND,
  296. MF_NODROP,
  297. MF_JEXP,
  298. MF_BEXP, //40
  299. MF_NOVENDING,
  300. MF_LOADEVENT,
  301. MF_NOCHAT,
  302. MF_NOEXPPENALTY,
  303. MF_GUILDLOCK,
  304. MF_TOWN,
  305. MF_AUTOTRADE,
  306. MF_ALLOWKS,
  307. MF_MONSTER_NOTELEPORT,
  308. MF_PVP_NOCALCRANK, //50
  309. MF_BATTLEGROUND
  310. };
  311. const char* script_op2name(int op)
  312. {
  313. #define RETURN_OP_NAME(type) case type: return #type
  314. switch( op )
  315. {
  316. RETURN_OP_NAME(C_NOP);
  317. RETURN_OP_NAME(C_POS);
  318. RETURN_OP_NAME(C_INT);
  319. RETURN_OP_NAME(C_PARAM);
  320. RETURN_OP_NAME(C_FUNC);
  321. RETURN_OP_NAME(C_STR);
  322. RETURN_OP_NAME(C_CONSTSTR);
  323. RETURN_OP_NAME(C_ARG);
  324. RETURN_OP_NAME(C_NAME);
  325. RETURN_OP_NAME(C_EOL);
  326. RETURN_OP_NAME(C_RETINFO);
  327. RETURN_OP_NAME(C_USERFUNC);
  328. RETURN_OP_NAME(C_USERFUNC_POS);
  329. // operators
  330. RETURN_OP_NAME(C_OP3);
  331. RETURN_OP_NAME(C_LOR);
  332. RETURN_OP_NAME(C_LAND);
  333. RETURN_OP_NAME(C_LE);
  334. RETURN_OP_NAME(C_LT);
  335. RETURN_OP_NAME(C_GE);
  336. RETURN_OP_NAME(C_GT);
  337. RETURN_OP_NAME(C_EQ);
  338. RETURN_OP_NAME(C_NE);
  339. RETURN_OP_NAME(C_XOR);
  340. RETURN_OP_NAME(C_OR);
  341. RETURN_OP_NAME(C_AND);
  342. RETURN_OP_NAME(C_ADD);
  343. RETURN_OP_NAME(C_SUB);
  344. RETURN_OP_NAME(C_MUL);
  345. RETURN_OP_NAME(C_DIV);
  346. RETURN_OP_NAME(C_MOD);
  347. RETURN_OP_NAME(C_NEG);
  348. RETURN_OP_NAME(C_LNOT);
  349. RETURN_OP_NAME(C_NOT);
  350. RETURN_OP_NAME(C_R_SHIFT);
  351. RETURN_OP_NAME(C_L_SHIFT);
  352. default:
  353. ShowDebug("script_op2name: unexpected op=%d\n", op);
  354. return "???";
  355. }
  356. #undef RETURN_OP_NAME
  357. }
  358. #ifdef DEBUG_DUMP_STACK
  359. static void script_dump_stack(struct script_state* st)
  360. {
  361. int i;
  362. ShowMessage("\tstart = %d\n", st->start);
  363. ShowMessage("\tend = %d\n", st->end);
  364. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  365. ShowMessage("\tsp = %d\n", st->stack->sp);
  366. for( i = 0; i < st->stack->sp; ++i )
  367. {
  368. struct script_data* data = &st->stack->stack_data[i];
  369. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  370. switch( data->type )
  371. {
  372. case C_INT:
  373. case C_POS:
  374. ShowMessage(" %d\n", data->u.num);
  375. break;
  376. case C_STR:
  377. case C_CONSTSTR:
  378. ShowMessage(" \"%s\"\n", data->u.str);
  379. break;
  380. case C_NAME:
  381. 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));
  382. break;
  383. case C_RETINFO:
  384. {
  385. struct script_retinfo* ri = data->u.ri;
  386. 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);
  387. }
  388. break;
  389. default:
  390. ShowMessage("\n");
  391. break;
  392. }
  393. }
  394. }
  395. #endif
  396. /// Reports on the console the src of a script error.
  397. static void script_reportsrc(struct script_state *st)
  398. {
  399. struct block_list* bl;
  400. if( st->oid == 0 )
  401. return; //Can't report source.
  402. bl = map_id2bl(st->oid);
  403. if( bl == NULL )
  404. return;
  405. switch( bl->type )
  406. {
  407. case BL_NPC:
  408. if( bl->m >= 0 )
  409. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  410. else
  411. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  412. break;
  413. default:
  414. if( bl->m >= 0 )
  415. 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);
  416. else
  417. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  418. break;
  419. }
  420. }
  421. /// Reports on the console information about the script data.
  422. static void script_reportdata(struct script_data* data)
  423. {
  424. if( data == NULL )
  425. return;
  426. switch( data->type )
  427. {
  428. case C_NOP:// no value
  429. ShowDebug("Data: nothing (nil)\n");
  430. break;
  431. case C_INT:// number
  432. ShowDebug("Data: number value=%d\n", data->u.num);
  433. break;
  434. case C_STR:
  435. case C_CONSTSTR:// string
  436. if( data->u.str )
  437. {
  438. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  439. }
  440. else
  441. {
  442. ShowDebug("Data: string value=NULL\n");
  443. }
  444. break;
  445. case C_NAME:// reference
  446. if( reference_tovariable(data) )
  447. {// variable
  448. const char* name = reference_getname(data);
  449. if( not_array_variable(*name) )
  450. ShowDebug("Data: variable name='%s'\n", name);
  451. else
  452. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  453. }
  454. else if( reference_toconstant(data) )
  455. {// constant
  456. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  457. }
  458. else if( reference_toparam(data) )
  459. {// param
  460. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  461. }
  462. else
  463. {// ???
  464. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  465. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  466. }
  467. break;
  468. case C_POS:// label
  469. ShowDebug("Data: label pos=%d\n", data->u.num);
  470. break;
  471. default:
  472. ShowDebug("Data: %s\n", script_op2name(data->type));
  473. break;
  474. }
  475. }
  476. /// Reports on the console information about the current built-in function.
  477. static void script_reportfunc(struct script_state* st)
  478. {
  479. int i, params, id;
  480. struct script_data* data;
  481. if( !script_hasdata(st,0) )
  482. {// no stack
  483. return;
  484. }
  485. data = script_getdata(st,0);
  486. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  487. {// script currently not executing a built-in function or corrupt stack
  488. return;
  489. }
  490. id = reference_getid(data);
  491. params = script_lastdata(st)-1;
  492. if( params > 0 )
  493. {
  494. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  495. for( i = 2; i <= script_lastdata(st); i++ )
  496. {
  497. script_reportdata(script_getdata(st,i));
  498. }
  499. }
  500. else
  501. {
  502. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  503. }
  504. }
  505. /*==========================================
  506. * エラーメッセージ出力
  507. *------------------------------------------*/
  508. static void disp_error_message2(const char *mes,const char *pos,int report)
  509. {
  510. error_msg = aStrdup(mes);
  511. error_pos = pos;
  512. error_report = report;
  513. longjmp( error_jump, 1 );
  514. }
  515. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  516. /// Checks event parameter validity
  517. static void check_event(struct script_state *st, const char *evt)
  518. {
  519. if( evt && evt[0] && !stristr(evt, "::On") )
  520. {
  521. if( npc_event_isspecial(evt) )
  522. {
  523. ; // portable small/large monsters or other attributes
  524. }
  525. else
  526. {
  527. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  528. script_reportsrc(st);
  529. }
  530. }
  531. }
  532. /*==========================================
  533. * Hashes the input string
  534. *------------------------------------------*/
  535. static unsigned int calc_hash(const char* p)
  536. {
  537. unsigned int h;
  538. #if defined(SCRIPT_HASH_DJB2)
  539. h = 5381;
  540. while( *p ) // hash*33 + c
  541. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  542. #elif defined(SCRIPT_HASH_SDBM)
  543. h = 0;
  544. while( *p ) // hash*65599 + c
  545. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  546. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  547. h = 0;
  548. while( *p ){
  549. unsigned int g;
  550. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  551. g = h & 0xF0000000;
  552. if( g )
  553. {
  554. h ^= g >> 24;
  555. h &= ~g;
  556. }
  557. }
  558. #else // athena hash
  559. h = 0;
  560. while( *p )
  561. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  562. #endif
  563. return h % SCRIPT_HASH_SIZE;
  564. }
  565. /*==========================================
  566. * str_data manipulation functions
  567. *------------------------------------------*/
  568. /// Looks up string using the provided id.
  569. const char* get_str(int id)
  570. {
  571. Assert( id >= LABEL_START && id < str_size );
  572. return str_buf+str_data[id].str;
  573. }
  574. /// Returns the uid of the string, or -1.
  575. static int search_str(const char* p)
  576. {
  577. int i;
  578. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  579. if( strcasecmp(get_str(i),p) == 0 )
  580. return i;
  581. return -1;
  582. }
  583. /// Stores a copy of the string and returns its id.
  584. /// If an identical string is already present, returns its id instead.
  585. int add_str(const char* p)
  586. {
  587. int i, h;
  588. int len;
  589. h = calc_hash(p);
  590. if( str_hash[h] == 0 )
  591. {// empty bucket, add new node here
  592. str_hash[h] = str_num;
  593. }
  594. else
  595. {// scan for end of list, or occurence of identical string
  596. for( i = str_hash[h]; ; i = str_data[i].next )
  597. {
  598. if( strcasecmp(get_str(i),p) == 0 )
  599. return i; // string already in list
  600. if( str_data[i].next == 0 )
  601. break; // reached the end
  602. }
  603. // append node to end of list
  604. str_data[i].next = str_num;
  605. }
  606. // grow list if neccessary
  607. if( str_num >= str_data_size )
  608. {
  609. str_data_size += 128;
  610. RECREATE(str_data,struct str_data_struct,str_data_size);
  611. memset(str_data + (str_data_size - 128), '\0', 128);
  612. }
  613. len=(int)strlen(p);
  614. // grow string buffer if neccessary
  615. while( str_pos+len+1 >= str_size )
  616. {
  617. str_size += 256;
  618. RECREATE(str_buf,char,str_size);
  619. memset(str_buf + (str_size - 256), '\0', 256);
  620. }
  621. safestrncpy(str_buf+str_pos, p, len+1);
  622. str_data[str_num].type = C_NOP;
  623. str_data[str_num].str = str_pos;
  624. str_data[str_num].next = 0;
  625. str_data[str_num].func = NULL;
  626. str_data[str_num].backpatch = -1;
  627. str_data[str_num].label = -1;
  628. str_pos += len+1;
  629. return str_num++;
  630. }
  631. /// Appends 1 byte to the script buffer.
  632. static void add_scriptb(int a)
  633. {
  634. if( script_pos+1 >= script_size )
  635. {
  636. script_size += SCRIPT_BLOCK_SIZE;
  637. RECREATE(script_buf,unsigned char,script_size);
  638. }
  639. script_buf[script_pos++] = (uint8)(a);
  640. }
  641. /// Appends a c_op value to the script buffer.
  642. /// The value is variable-length encoded into 8-bit blocks.
  643. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  644. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  645. static void add_scriptc(int a)
  646. {
  647. while( a >= 0x40 )
  648. {
  649. add_scriptb((a&0x3f)|0x40);
  650. a = (a - 0x40) >> 6;
  651. }
  652. add_scriptb(a);
  653. }
  654. /// Appends an integer value to the script buffer.
  655. /// The value is variable-length encoded into 8-bit blocks.
  656. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  657. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  658. static void add_scripti(int a)
  659. {
  660. while( a >= 0x40 )
  661. {
  662. add_scriptb((a&0x3f)|0xc0);
  663. a = (a - 0x40) >> 6;
  664. }
  665. add_scriptb(a|0x80);
  666. }
  667. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  668. ///
  669. /// @param l The id of the str_data entry
  670. // 最大16Mまで
  671. static void add_scriptl(int l)
  672. {
  673. int backpatch = str_data[l].backpatch;
  674. switch(str_data[l].type){
  675. case C_POS:
  676. case C_USERFUNC_POS:
  677. add_scriptc(C_POS);
  678. add_scriptb(str_data[l].label);
  679. add_scriptb(str_data[l].label>>8);
  680. add_scriptb(str_data[l].label>>16);
  681. break;
  682. case C_NOP:
  683. case C_USERFUNC:
  684. // ラベルの可能性があるのでbackpatch用データ埋め込み
  685. add_scriptc(C_NAME);
  686. str_data[l].backpatch = script_pos;
  687. add_scriptb(backpatch);
  688. add_scriptb(backpatch>>8);
  689. add_scriptb(backpatch>>16);
  690. break;
  691. case C_INT:
  692. add_scripti(abs(str_data[l].val));
  693. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  694. add_scriptc(C_NEG);
  695. break;
  696. default: // assume C_NAME
  697. add_scriptc(C_NAME);
  698. add_scriptb(l);
  699. add_scriptb(l>>8);
  700. add_scriptb(l>>16);
  701. break;
  702. }
  703. }
  704. /*==========================================
  705. * ラベルを解決する
  706. *------------------------------------------*/
  707. void set_label(int l,int pos, const char* script_pos)
  708. {
  709. int i,next;
  710. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  711. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  712. disp_error_message("set_label: invalid label name",script_pos);
  713. return;
  714. }
  715. if(str_data[l].label!=-1){
  716. disp_error_message("set_label: dup label ",script_pos);
  717. return;
  718. }
  719. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  720. str_data[l].label=pos;
  721. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  722. next=GETVALUE(script_buf,i);
  723. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  724. SETVALUE(script_buf,i,pos);
  725. i=next;
  726. }
  727. }
  728. /// Skips spaces and/or comments.
  729. const char* skip_space(const char* p)
  730. {
  731. if( p == NULL )
  732. return NULL;
  733. for(;;)
  734. {
  735. while( ISSPACE(*p) )
  736. ++p;
  737. if( *p == '/' && p[1] == '/' )
  738. {// line comment
  739. while(*p && *p!='\n')
  740. ++p;
  741. }
  742. else if( *p == '/' && p[1] == '*' )
  743. {// block comment
  744. p += 2;
  745. for(;;)
  746. {
  747. if( *p == '\0' )
  748. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  749. if( *p == '*' && p[1] == '/' )
  750. {// end of block comment
  751. p += 2;
  752. break;
  753. }
  754. ++p;
  755. }
  756. }
  757. else
  758. break;
  759. }
  760. return p;
  761. }
  762. /// Skips a word.
  763. /// A word consists of undercores and/or alfanumeric characters,
  764. /// and valid variable prefixes/postfixes.
  765. static
  766. const char* skip_word(const char* p)
  767. {
  768. // prefix
  769. switch( *p )
  770. {
  771. case '@':// temporary char variable
  772. ++p; break;
  773. case '#':// account variable
  774. p += ( p[1] == '#' ? 2 : 1 ); break;
  775. case '\'':// instance variable
  776. ++p; break;
  777. case '.':// npc variable
  778. p += ( p[1] == '@' ? 2 : 1 ); break;
  779. case '$':// global variable
  780. p += ( p[1] == '@' ? 2 : 1 ); break;
  781. }
  782. while( ISALNUM(*p) || *p == '_' )
  783. ++p;
  784. // postfix
  785. if( *p == '$' )// string
  786. p++;
  787. return p;
  788. }
  789. /// Adds a word to str_data.
  790. /// @see skip_word
  791. /// @see add_str
  792. static
  793. int add_word(const char* p)
  794. {
  795. char* word;
  796. int len;
  797. int i;
  798. // Check for a word
  799. len = skip_word(p) - p;
  800. if( len == 0 )
  801. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  802. // Duplicate the word
  803. word = (char*)aMalloc(len+1);
  804. memcpy(word, p, len);
  805. word[len] = 0;
  806. // add the word
  807. i = add_str(word);
  808. aFree(word);
  809. return i;
  810. }
  811. /// Parses a function call.
  812. /// The argument list can have parenthesis or not.
  813. /// The number of arguments is checked.
  814. static
  815. const char* parse_callfunc(const char* p, int require_paren)
  816. {
  817. const char* p2;
  818. const char* arg=NULL;
  819. int func;
  820. func = add_word(p);
  821. if( str_data[func].type == C_FUNC ){
  822. // buildin function
  823. add_scriptl(func);
  824. add_scriptc(C_ARG);
  825. arg = buildin_func[str_data[func].val].arg;
  826. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  827. // script defined function
  828. int callsub = search_str("callsub");
  829. add_scriptl(callsub);
  830. add_scriptc(C_ARG);
  831. add_scriptl(func);
  832. arg = buildin_func[str_data[callsub].val].arg;
  833. if( *arg == 0 )
  834. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  835. if( *arg != '*' )
  836. ++arg; // count func as argument
  837. } else
  838. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  839. p = skip_word(p);
  840. p = skip_space(p);
  841. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  842. syntax.curly[syntax.curly_count].count = 0;
  843. if( *p == ';' )
  844. {// <func name> ';'
  845. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  846. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  847. {// <func name> '(' ')'
  848. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  849. p = p2;
  850. /*
  851. } else if( 0 && require_paren && *p != '(' )
  852. {// <func name>
  853. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  854. */
  855. } else
  856. {// <func name> <arg list>
  857. if( require_paren ){
  858. if( *p != '(' )
  859. disp_error_message("need '('",p);
  860. ++p; // skip '('
  861. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  862. } else if( *p == '(' ){
  863. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  864. } else {
  865. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  866. }
  867. ++syntax.curly_count;
  868. while( *arg ) {
  869. p2=parse_subexpr(p,-1);
  870. if( p == p2 )
  871. break; // not an argument
  872. if( *arg != '*' )
  873. ++arg; // next argument
  874. p=skip_space(p2);
  875. if( *arg == 0 || *p != ',' )
  876. break; // no more arguments
  877. ++p; // skip comma
  878. }
  879. --syntax.curly_count;
  880. }
  881. if( *arg && *arg != '?' && *arg != '*' )
  882. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  883. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  884. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  885. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  886. if( *p != ')' )
  887. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  888. ++p;
  889. }
  890. add_scriptc(C_FUNC);
  891. return p;
  892. }
  893. /// Processes end of logical script line.
  894. /// @param first When true, only fix up scheduling data is initialized
  895. /// @param p Script position for error reporting in set_label
  896. static void parse_nextline(bool first, const char* p)
  897. {
  898. if( !first )
  899. {
  900. add_scriptc(C_EOL); // mark end of line for stack cleanup
  901. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  902. }
  903. // initialize data for new '-' label fix up scheduling
  904. str_data[LABEL_NEXTLINE].type = C_NOP;
  905. str_data[LABEL_NEXTLINE].backpatch = -1;
  906. str_data[LABEL_NEXTLINE].label = -1;
  907. }
  908. /*==========================================
  909. * 項の解析
  910. *------------------------------------------*/
  911. const char* parse_simpleexpr(const char *p)
  912. {
  913. int i;
  914. p=skip_space(p);
  915. if(*p==';' || *p==',')
  916. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  917. if(*p=='('){
  918. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  919. ++syntax.curly[i].count;
  920. p=parse_subexpr(p+1,-1);
  921. p=skip_space(p);
  922. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  923. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  924. ){
  925. if( *p == ',' ){
  926. syntax.curly[i].flag = ARGLIST_PAREN;
  927. return p;
  928. } else
  929. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  930. }
  931. if( *p != ')' )
  932. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  933. ++p;
  934. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  935. char *np;
  936. i=strtoul(p,&np,0);
  937. add_scripti(i);
  938. p=np;
  939. } else if(*p=='"'){
  940. add_scriptc(C_STR);
  941. p++;
  942. while( *p && *p != '"' ){
  943. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  944. {
  945. char buf[8];
  946. size_t len = skip_escaped_c(p) - p;
  947. size_t n = sv_unescape_c(buf, p, len);
  948. if( n != 1 )
  949. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  950. p += len;
  951. add_scriptb(*buf);
  952. continue;
  953. }
  954. else if( *p == '\n' )
  955. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  956. add_scriptb(*p++);
  957. }
  958. if(!*p)
  959. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  960. add_scriptb(0);
  961. p++; //'"'
  962. } else {
  963. int l;
  964. // label , register , function etc
  965. if(skip_word(p)==p)
  966. disp_error_message("parse_simpleexpr: unexpected character",p);
  967. l=add_word(p);
  968. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  969. return parse_callfunc(p,1);
  970. p=skip_word(p);
  971. if( *p == '[' ){
  972. // array(name[i] => getelementofarray(name,i) )
  973. add_scriptl(search_str("getelementofarray"));
  974. add_scriptc(C_ARG);
  975. add_scriptl(l);
  976. p=parse_subexpr(p+1,-1);
  977. p=skip_space(p);
  978. if( *p != ']' )
  979. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  980. ++p;
  981. add_scriptc(C_FUNC);
  982. }else
  983. add_scriptl(l);
  984. }
  985. return p;
  986. }
  987. /*==========================================
  988. * 式の解析
  989. *------------------------------------------*/
  990. const char* parse_subexpr(const char* p,int limit)
  991. {
  992. int op,opl,len;
  993. const char* tmpp;
  994. p=skip_space(p);
  995. if(*p=='-'){
  996. tmpp=skip_space(p+1);
  997. if(*tmpp==';' || *tmpp==','){
  998. add_scriptl(LABEL_NEXTLINE);
  999. p++;
  1000. return p;
  1001. }
  1002. }
  1003. tmpp=p;
  1004. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1005. p=parse_subexpr(p+1,10);
  1006. add_scriptc(op);
  1007. } else
  1008. p=parse_simpleexpr(p);
  1009. p=skip_space(p);
  1010. while((
  1011. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1012. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1013. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1014. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1015. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1016. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1017. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1018. (op=C_AND,opl=6,len=1,*p=='&') ||
  1019. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1020. (op=C_OR,opl=5,len=1,*p=='|') ||
  1021. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1022. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1023. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1024. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1025. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1026. (op=C_GT,opl=3,len=1,*p=='>') ||
  1027. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1028. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1029. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1030. p+=len;
  1031. if(op == C_OP3) {
  1032. p=parse_subexpr(p,-1);
  1033. p=skip_space(p);
  1034. if( *(p++) != ':')
  1035. disp_error_message("parse_subexpr: need ':'", p-1);
  1036. p=parse_subexpr(p,-1);
  1037. } else {
  1038. p=parse_subexpr(p,opl);
  1039. }
  1040. add_scriptc(op);
  1041. p=skip_space(p);
  1042. }
  1043. return p; /* return first untreated operator */
  1044. }
  1045. /*==========================================
  1046. * 式の評価
  1047. *------------------------------------------*/
  1048. const char* parse_expr(const char *p)
  1049. {
  1050. switch(*p){
  1051. case ')': case ';': case ':': case '[': case ']':
  1052. case '}':
  1053. disp_error_message("parse_expr: unexpected char",p);
  1054. }
  1055. p=parse_subexpr(p,-1);
  1056. return p;
  1057. }
  1058. /*==========================================
  1059. * 行の解析
  1060. *------------------------------------------*/
  1061. const char* parse_line(const char* p)
  1062. {
  1063. const char* p2;
  1064. p=skip_space(p);
  1065. if(*p==';') {
  1066. // if(); for(); while(); のために閉じ判定
  1067. p = parse_syntax_close(p + 1);
  1068. return p;
  1069. }
  1070. if(*p==')' && parse_syntax_for_flag)
  1071. return p+1;
  1072. p = skip_space(p);
  1073. if(p[0] == '{') {
  1074. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1075. syntax.curly[syntax.curly_count].count = -1;
  1076. syntax.curly[syntax.curly_count].index = -1;
  1077. syntax.curly_count++;
  1078. return p + 1;
  1079. } else if(p[0] == '}') {
  1080. return parse_curly_close(p);
  1081. }
  1082. // 構文関連の処理
  1083. p2 = parse_syntax(p);
  1084. if(p2 != NULL)
  1085. return p2;
  1086. p = parse_callfunc(p,0);
  1087. p = skip_space(p);
  1088. if(parse_syntax_for_flag) {
  1089. if( *p != ')' )
  1090. disp_error_message("parse_line: need ')'",p);
  1091. } else {
  1092. if( *p != ';' )
  1093. disp_error_message("parse_line: need ';'",p);
  1094. }
  1095. // if, for , while の閉じ判定
  1096. p = parse_syntax_close(p+1);
  1097. return p;
  1098. }
  1099. // { ... } の閉じ処理
  1100. const char* parse_curly_close(const char* p)
  1101. {
  1102. if(syntax.curly_count <= 0) {
  1103. disp_error_message("parse_curly_close: unexpected string",p);
  1104. return p + 1;
  1105. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1106. syntax.curly_count--;
  1107. // if, for , while の閉じ判定
  1108. p = parse_syntax_close(p + 1);
  1109. return p;
  1110. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1111. // switch() 閉じ判定
  1112. int pos = syntax.curly_count-1;
  1113. char label[256];
  1114. int l;
  1115. // 一時変数を消す
  1116. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1117. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1118. parse_line(label);
  1119. syntax.curly_count--;
  1120. // 無条件で終了ポインタに移動
  1121. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1122. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1123. parse_line(label);
  1124. syntax.curly_count--;
  1125. // 現在地のラベルを付ける
  1126. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1127. l=add_str(label);
  1128. set_label(l,script_pos, p);
  1129. if(syntax.curly[pos].flag) {
  1130. // default が存在する
  1131. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1132. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1133. parse_line(label);
  1134. syntax.curly_count--;
  1135. }
  1136. // 終了ラベルを付ける
  1137. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1138. l=add_str(label);
  1139. set_label(l,script_pos, p);
  1140. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1141. syntax.curly_count--;
  1142. // if, for , while の閉じ判定
  1143. p = parse_syntax_close(p + 1);
  1144. return p;
  1145. } else {
  1146. disp_error_message("parse_curly_close: unexpected string",p);
  1147. return p + 1;
  1148. }
  1149. }
  1150. // 構文関連の処理
  1151. // break, case, continue, default, do, for, function,
  1152. // if, switch, while をこの内部で処理します。
  1153. const char* parse_syntax(const char* p)
  1154. {
  1155. const char *p2 = skip_word(p);
  1156. switch(*p) {
  1157. case 'B':
  1158. case 'b':
  1159. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1160. // break の処理
  1161. char label[256];
  1162. int pos = syntax.curly_count - 1;
  1163. while(pos >= 0) {
  1164. if(syntax.curly[pos].type == TYPE_DO) {
  1165. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1166. break;
  1167. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1168. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1169. break;
  1170. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1171. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1172. break;
  1173. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1174. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1175. break;
  1176. }
  1177. pos--;
  1178. }
  1179. if(pos < 0) {
  1180. disp_error_message("parse_syntax: unexpected 'break'",p);
  1181. } else {
  1182. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1183. parse_line(label);
  1184. syntax.curly_count--;
  1185. }
  1186. p = skip_space(p2);
  1187. if(*p != ';')
  1188. disp_error_message("parse_syntax: need ';'",p);
  1189. // if, for , while の閉じ判定
  1190. p = parse_syntax_close(p + 1);
  1191. return p;
  1192. }
  1193. break;
  1194. case 'c':
  1195. case 'C':
  1196. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1197. // case の処理
  1198. int pos = syntax.curly_count-1;
  1199. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1200. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1201. return p+1;
  1202. } else {
  1203. char label[256];
  1204. int l,v;
  1205. char *np;
  1206. if(syntax.curly[pos].count != 1) {
  1207. // FALLTHRU 用のジャンプ
  1208. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1209. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1210. parse_line(label);
  1211. syntax.curly_count--;
  1212. // 現在地のラベルを付ける
  1213. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1214. l=add_str(label);
  1215. set_label(l,script_pos, p);
  1216. }
  1217. // switch 判定文
  1218. p = skip_space(p2);
  1219. if(p == p2) {
  1220. disp_error_message("parse_syntax: expect space ' '",p);
  1221. }
  1222. // check whether case label is integer or not
  1223. v = strtol(p,&np,0);
  1224. if(np == p) { //Check for constants
  1225. p2 = skip_word(p);
  1226. v = p2-p; // length of word at p2
  1227. memcpy(label,p,v);
  1228. label[v]='\0';
  1229. if( !script_get_constant(label, &v) )
  1230. disp_error_message("parse_syntax: 'case' label not integer",p);
  1231. p = skip_word(p);
  1232. } else { //Numeric value
  1233. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1234. p++;
  1235. p = skip_word(p);
  1236. if(np != p)
  1237. disp_error_message("parse_syntax: 'case' label not integer",np);
  1238. }
  1239. p = skip_space(p);
  1240. if(*p != ':')
  1241. disp_error_message("parse_syntax: expect ':'",p);
  1242. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1243. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1244. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1245. // 2回parse しないとダメ
  1246. p2 = parse_line(label);
  1247. parse_line(p2);
  1248. syntax.curly_count--;
  1249. if(syntax.curly[pos].count != 1) {
  1250. // FALLTHRU 終了後のラベル
  1251. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1252. l=add_str(label);
  1253. set_label(l,script_pos,p);
  1254. }
  1255. // check duplication of case label [Rayce]
  1256. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1257. disp_error_message("parse_syntax: dup 'case'",p);
  1258. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1259. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1260. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1261. parse_line(label);
  1262. syntax.curly_count--;
  1263. syntax.curly[pos].count++;
  1264. }
  1265. return p + 1;
  1266. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1267. // continue の処理
  1268. char label[256];
  1269. int pos = syntax.curly_count - 1;
  1270. while(pos >= 0) {
  1271. if(syntax.curly[pos].type == TYPE_DO) {
  1272. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1273. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1274. break;
  1275. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1276. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1277. break;
  1278. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1279. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1280. break;
  1281. }
  1282. pos--;
  1283. }
  1284. if(pos < 0) {
  1285. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1286. } else {
  1287. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1288. parse_line(label);
  1289. syntax.curly_count--;
  1290. }
  1291. p = skip_space(p2);
  1292. if(*p != ';')
  1293. disp_error_message("parse_syntax: need ';'",p);
  1294. // if, for , while の閉じ判定
  1295. p = parse_syntax_close(p + 1);
  1296. return p;
  1297. }
  1298. break;
  1299. case 'd':
  1300. case 'D':
  1301. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1302. // switch - default の処理
  1303. int pos = syntax.curly_count-1;
  1304. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1305. disp_error_message("parse_syntax: unexpected 'default'",p);
  1306. } else if(syntax.curly[pos].flag) {
  1307. disp_error_message("parse_syntax: dup 'default'",p);
  1308. } else {
  1309. char label[256];
  1310. int l;
  1311. // 現在地のラベルを付ける
  1312. p = skip_space(p2);
  1313. if(*p != ':') {
  1314. disp_error_message("parse_syntax: need ':'",p);
  1315. }
  1316. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1317. l=add_str(label);
  1318. set_label(l,script_pos,p);
  1319. // 無条件で次のリンクに飛ばす
  1320. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1321. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1322. parse_line(label);
  1323. syntax.curly_count--;
  1324. // default のラベルを付ける
  1325. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1326. l=add_str(label);
  1327. set_label(l,script_pos,p);
  1328. syntax.curly[syntax.curly_count - 1].flag = 1;
  1329. syntax.curly[pos].count++;
  1330. }
  1331. return p + 1;
  1332. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1333. int l;
  1334. char label[256];
  1335. p=skip_space(p2);
  1336. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1337. syntax.curly[syntax.curly_count].count = 1;
  1338. syntax.curly[syntax.curly_count].index = syntax.index++;
  1339. syntax.curly[syntax.curly_count].flag = 0;
  1340. // 現在地のラベル形成する
  1341. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1342. l=add_str(label);
  1343. set_label(l,script_pos,p);
  1344. syntax.curly_count++;
  1345. return p;
  1346. }
  1347. break;
  1348. case 'f':
  1349. case 'F':
  1350. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1351. int l;
  1352. char label[256];
  1353. int pos = syntax.curly_count;
  1354. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1355. syntax.curly[syntax.curly_count].count = 1;
  1356. syntax.curly[syntax.curly_count].index = syntax.index++;
  1357. syntax.curly[syntax.curly_count].flag = 0;
  1358. syntax.curly_count++;
  1359. p=skip_space(p2);
  1360. if(*p != '(')
  1361. disp_error_message("parse_syntax: need '('",p);
  1362. p++;
  1363. // 初期化文を実行する
  1364. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1365. p=parse_line(p);
  1366. syntax.curly_count--;
  1367. // 条件判断開始のラベル形成する
  1368. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1369. l=add_str(label);
  1370. set_label(l,script_pos,p);
  1371. p=skip_space(p);
  1372. if(*p == ';') {
  1373. // for(;;) のパターンなので必ず真
  1374. ;
  1375. } else {
  1376. // 条件が偽なら終了地点に飛ばす
  1377. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1378. add_scriptl(add_str("jump_zero"));
  1379. add_scriptc(C_ARG);
  1380. p=parse_expr(p);
  1381. p=skip_space(p);
  1382. add_scriptl(add_str(label));
  1383. add_scriptc(C_FUNC);
  1384. }
  1385. if(*p != ';')
  1386. disp_error_message("parse_syntax: need ';'",p);
  1387. p++;
  1388. // ループ開始に飛ばす
  1389. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1390. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1391. parse_line(label);
  1392. syntax.curly_count--;
  1393. // 次のループへのラベル形成する
  1394. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1395. l=add_str(label);
  1396. set_label(l,script_pos,p);
  1397. // 次のループに入る時の処理
  1398. // for 最後の ')' を ';' として扱うフラグ
  1399. parse_syntax_for_flag = 1;
  1400. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1401. p=parse_line(p);
  1402. syntax.curly_count--;
  1403. parse_syntax_for_flag = 0;
  1404. // 条件判定処理に飛ばす
  1405. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1406. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1407. parse_line(label);
  1408. syntax.curly_count--;
  1409. // ループ開始のラベル付け
  1410. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1411. l=add_str(label);
  1412. set_label(l,script_pos,p);
  1413. return p;
  1414. }
  1415. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1416. {// internal script function
  1417. const char *func_name;
  1418. func_name = skip_space(p2);
  1419. p = skip_word(func_name);
  1420. if( p == func_name )
  1421. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1422. p2 = skip_space(p);
  1423. if( *p2 == ';' )
  1424. {// function <name> ;
  1425. // function declaration - just register the name
  1426. int l;
  1427. l = add_word(func_name);
  1428. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1429. str_data[l].type = C_USERFUNC;
  1430. else if( str_data[l].type == C_USERFUNC )
  1431. ; // already registered
  1432. else
  1433. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1434. // if, for , while の閉じ判定
  1435. p = parse_syntax_close(p2 + 1);
  1436. return p;
  1437. }
  1438. else if(*p2 == '{')
  1439. {// function <name> <line/block of code>
  1440. char label[256];
  1441. int l;
  1442. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1443. syntax.curly[syntax.curly_count].count = 1;
  1444. syntax.curly[syntax.curly_count].index = syntax.index++;
  1445. syntax.curly[syntax.curly_count].flag = 0;
  1446. ++syntax.curly_count;
  1447. // Jump over the function code
  1448. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1449. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1450. ++syntax.curly_count;
  1451. parse_line(label);
  1452. --syntax.curly_count;
  1453. // Set the position of the function (label)
  1454. l=add_word(func_name);
  1455. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1456. {
  1457. str_data[l].type = C_USERFUNC;
  1458. set_label(l, script_pos, p);
  1459. if( parse_options&SCRIPT_USE_LABEL_DB )
  1460. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1461. }
  1462. else
  1463. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1464. return skip_space(p);
  1465. }
  1466. else
  1467. {
  1468. disp_error_message("expect ';' or '{' at function syntax",p);
  1469. }
  1470. }
  1471. break;
  1472. case 'i':
  1473. case 'I':
  1474. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1475. // if() の処理
  1476. char label[256];
  1477. p=skip_space(p2);
  1478. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1479. disp_error_message("need '('",p);
  1480. }
  1481. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1482. syntax.curly[syntax.curly_count].count = 1;
  1483. syntax.curly[syntax.curly_count].index = syntax.index++;
  1484. syntax.curly[syntax.curly_count].flag = 0;
  1485. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1486. syntax.curly_count++;
  1487. add_scriptl(add_str("jump_zero"));
  1488. add_scriptc(C_ARG);
  1489. p=parse_expr(p);
  1490. p=skip_space(p);
  1491. add_scriptl(add_str(label));
  1492. add_scriptc(C_FUNC);
  1493. return p;
  1494. }
  1495. break;
  1496. case 's':
  1497. case 'S':
  1498. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1499. // switch() の処理
  1500. char label[256];
  1501. p=skip_space(p2);
  1502. if(*p != '(') {
  1503. disp_error_message("need '('",p);
  1504. }
  1505. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1506. syntax.curly[syntax.curly_count].count = 1;
  1507. syntax.curly[syntax.curly_count].index = syntax.index++;
  1508. syntax.curly[syntax.curly_count].flag = 0;
  1509. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1510. syntax.curly_count++;
  1511. add_scriptl(add_str("set"));
  1512. add_scriptc(C_ARG);
  1513. add_scriptl(add_str(label));
  1514. p=parse_expr(p);
  1515. p=skip_space(p);
  1516. if(*p != '{') {
  1517. disp_error_message("parse_syntax: need '{'",p);
  1518. }
  1519. add_scriptc(C_FUNC);
  1520. return p + 1;
  1521. }
  1522. break;
  1523. case 'w':
  1524. case 'W':
  1525. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1526. int l;
  1527. char label[256];
  1528. p=skip_space(p2);
  1529. if(*p != '(') {
  1530. disp_error_message("need '('",p);
  1531. }
  1532. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1533. syntax.curly[syntax.curly_count].count = 1;
  1534. syntax.curly[syntax.curly_count].index = syntax.index++;
  1535. syntax.curly[syntax.curly_count].flag = 0;
  1536. // 条件判断開始のラベル形成する
  1537. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1538. l=add_str(label);
  1539. set_label(l,script_pos,p);
  1540. // 条件が偽なら終了地点に飛ばす
  1541. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1542. syntax.curly_count++;
  1543. add_scriptl(add_str("jump_zero"));
  1544. add_scriptc(C_ARG);
  1545. p=parse_expr(p);
  1546. p=skip_space(p);
  1547. add_scriptl(add_str(label));
  1548. add_scriptc(C_FUNC);
  1549. return p;
  1550. }
  1551. break;
  1552. }
  1553. return NULL;
  1554. }
  1555. const char* parse_syntax_close(const char *p) {
  1556. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1557. int flag;
  1558. do {
  1559. p = parse_syntax_close_sub(p,&flag);
  1560. } while(flag);
  1561. return p;
  1562. }
  1563. // if, for , while , do の閉じ判定
  1564. // flag == 1 : 閉じられた
  1565. // flag == 0 : 閉じられない
  1566. const char* parse_syntax_close_sub(const char* p,int* flag)
  1567. {
  1568. char label[256];
  1569. int pos = syntax.curly_count - 1;
  1570. int l;
  1571. *flag = 1;
  1572. if(syntax.curly_count <= 0) {
  1573. *flag = 0;
  1574. return p;
  1575. } else if(syntax.curly[pos].type == TYPE_IF) {
  1576. const char *bp = p;
  1577. const char *p2;
  1578. // if-block and else-block end is a new line
  1579. parse_nextline(false, p);
  1580. // if 最終場所へ飛ばす
  1581. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1582. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1583. parse_line(label);
  1584. syntax.curly_count--;
  1585. // 現在地のラベルを付ける
  1586. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1587. l=add_str(label);
  1588. set_label(l,script_pos,p);
  1589. syntax.curly[pos].count++;
  1590. p = skip_space(p);
  1591. p2 = skip_word(p);
  1592. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1593. // else or else - if
  1594. p = skip_space(p2);
  1595. p2 = skip_word(p);
  1596. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1597. // else - if
  1598. p=skip_space(p2);
  1599. if(*p != '(') {
  1600. disp_error_message("need '('",p);
  1601. }
  1602. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1603. add_scriptl(add_str("jump_zero"));
  1604. add_scriptc(C_ARG);
  1605. p=parse_expr(p);
  1606. p=skip_space(p);
  1607. add_scriptl(add_str(label));
  1608. add_scriptc(C_FUNC);
  1609. *flag = 0;
  1610. return p;
  1611. } else {
  1612. // else
  1613. if(!syntax.curly[pos].flag) {
  1614. syntax.curly[pos].flag = 1;
  1615. *flag = 0;
  1616. return p;
  1617. }
  1618. }
  1619. }
  1620. // if 閉じ
  1621. syntax.curly_count--;
  1622. // 最終地のラベルを付ける
  1623. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1624. l=add_str(label);
  1625. set_label(l,script_pos,p);
  1626. if(syntax.curly[pos].flag == 1) {
  1627. // このifに対するelseじゃないのでポインタの位置は同じ
  1628. return bp;
  1629. }
  1630. return p;
  1631. } else if(syntax.curly[pos].type == TYPE_DO) {
  1632. int l;
  1633. char label[256];
  1634. const char *p2;
  1635. if(syntax.curly[pos].flag) {
  1636. // 現在地のラベル形成する(continue でここに来る)
  1637. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1638. l=add_str(label);
  1639. set_label(l,script_pos,p);
  1640. }
  1641. // 条件が偽なら終了地点に飛ばす
  1642. p = skip_space(p);
  1643. p2 = skip_word(p);
  1644. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1645. disp_error_message("parse_syntax: need 'while'",p);
  1646. p = skip_space(p2);
  1647. if(*p != '(') {
  1648. disp_error_message("need '('",p);
  1649. }
  1650. // do-block end is a new line
  1651. parse_nextline(false, p);
  1652. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1653. add_scriptl(add_str("jump_zero"));
  1654. add_scriptc(C_ARG);
  1655. p=parse_expr(p);
  1656. p=skip_space(p);
  1657. add_scriptl(add_str(label));
  1658. add_scriptc(C_FUNC);
  1659. // 開始地点に飛ばす
  1660. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1661. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1662. parse_line(label);
  1663. syntax.curly_count--;
  1664. // 条件終了地点のラベル形成する
  1665. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1666. l=add_str(label);
  1667. set_label(l,script_pos,p);
  1668. p = skip_space(p);
  1669. if(*p != ';') {
  1670. disp_error_message("parse_syntax: need ';'",p);
  1671. return p+1;
  1672. }
  1673. p++;
  1674. syntax.curly_count--;
  1675. return p;
  1676. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1677. // for-block end is a new line
  1678. parse_nextline(false, p);
  1679. // 次のループに飛ばす
  1680. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1681. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1682. parse_line(label);
  1683. syntax.curly_count--;
  1684. // for 終了のラベル付け
  1685. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1686. l=add_str(label);
  1687. set_label(l,script_pos,p);
  1688. syntax.curly_count--;
  1689. return p;
  1690. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1691. // while-block end is a new line
  1692. parse_nextline(false, p);
  1693. // while 条件判断へ飛ばす
  1694. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1695. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1696. parse_line(label);
  1697. syntax.curly_count--;
  1698. // while 終了のラベル付け
  1699. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1700. l=add_str(label);
  1701. set_label(l,script_pos,p);
  1702. syntax.curly_count--;
  1703. return p;
  1704. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1705. int pos = syntax.curly_count-1;
  1706. char label[256];
  1707. int l;
  1708. // 戻す
  1709. sprintf(label,"return;");
  1710. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1711. parse_line(label);
  1712. syntax.curly_count--;
  1713. // 現在地のラベルを付ける
  1714. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1715. l=add_str(label);
  1716. set_label(l,script_pos,p);
  1717. syntax.curly_count--;
  1718. return p;
  1719. } else {
  1720. *flag = 0;
  1721. return p;
  1722. }
  1723. }
  1724. /*==========================================
  1725. * 組み込み関数の追加
  1726. *------------------------------------------*/
  1727. static void add_buildin_func(void)
  1728. {
  1729. int i,n;
  1730. const char* p;
  1731. for( i = 0; buildin_func[i].func; i++ )
  1732. {
  1733. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1734. // 'v' - value (either string or int or reference)
  1735. // 's' - string
  1736. // 'i' - int
  1737. // 'r' - reference (of a variable)
  1738. // 'l' - label
  1739. // '?' - one optional parameter
  1740. // '*' - unknown number of optional parameters
  1741. p = buildin_func[i].arg;
  1742. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1743. while( *p == '?' ) ++p;
  1744. if( *p == '*' ) ++p;
  1745. if( *p != 0){
  1746. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1747. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1748. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1749. } else {
  1750. n = add_str(buildin_func[i].name);
  1751. str_data[n].type = C_FUNC;
  1752. str_data[n].val = i;
  1753. str_data[n].func = buildin_func[i].func;
  1754. }
  1755. }
  1756. }
  1757. /// Retrieves the value of a constant.
  1758. bool script_get_constant(const char* name, int* value)
  1759. {
  1760. int n = search_str(name);
  1761. if( n == -1 || str_data[n].type != C_INT )
  1762. {// not found or not a constant
  1763. return false;
  1764. }
  1765. value[0] = str_data[n].val;
  1766. return true;
  1767. }
  1768. /// Creates new constant or parameter with given value.
  1769. void script_set_constant(const char* name, int value, bool isparameter)
  1770. {
  1771. int n = add_str(name);
  1772. if( str_data[n].type == C_NOP )
  1773. {// new
  1774. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1775. str_data[n].val = value;
  1776. }
  1777. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1778. {// existing parameter or constant
  1779. 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);
  1780. }
  1781. else
  1782. {// existing name
  1783. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1784. }
  1785. }
  1786. /*==========================================
  1787. * 定数データベースの読み込み
  1788. *------------------------------------------*/
  1789. static void read_constdb(void)
  1790. {
  1791. FILE *fp;
  1792. char line[1024],name[1024],val[1024];
  1793. int type;
  1794. sprintf(line, "%s/const.txt", db_path);
  1795. fp=fopen(line, "r");
  1796. if(fp==NULL){
  1797. ShowError("can't read %s\n", line);
  1798. return ;
  1799. }
  1800. while(fgets(line, sizeof(line), fp))
  1801. {
  1802. if(line[0]=='/' && line[1]=='/')
  1803. continue;
  1804. type=0;
  1805. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1806. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1807. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  1808. }
  1809. }
  1810. fclose(fp);
  1811. }
  1812. /*==========================================
  1813. * エラー表示
  1814. *------------------------------------------*/
  1815. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1816. {
  1817. int i;
  1818. if( p == NULL || !p[0] ) return NULL;
  1819. if( line < 0 )
  1820. StringBuf_Printf(buf, "*% 5d : ", -line);
  1821. else
  1822. StringBuf_Printf(buf, " % 5d : ", line);
  1823. for(i=0;p[i] && p[i] != '\n';i++){
  1824. if(p + i != mark)
  1825. StringBuf_Printf(buf, "%c", p[i]);
  1826. else
  1827. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1828. }
  1829. StringBuf_AppendStr(buf, "\n");
  1830. return p+i+(p[i] == '\n' ? 1 : 0);
  1831. }
  1832. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1833. {
  1834. // エラーが発生した行を求める
  1835. int j;
  1836. int line = start_line;
  1837. const char *p;
  1838. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1839. StringBuf buf;
  1840. for(p=src;p && *p;line++){
  1841. const char *lineend=strchr(p,'\n');
  1842. if(lineend==NULL || error_pos<lineend){
  1843. break;
  1844. }
  1845. for( j = 0; j < 4; j++ ) {
  1846. linestart[j] = linestart[j+1];
  1847. }
  1848. linestart[4] = p;
  1849. p=lineend+1;
  1850. }
  1851. StringBuf_Init(&buf);
  1852. StringBuf_AppendStr(&buf, "\a\n");
  1853. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1854. StringBuf_Printf(&buf, " %s\n", error_msg);
  1855. for(j = 0; j < 5; j++ ) {
  1856. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1857. }
  1858. p = script_print_line(&buf, p, error_pos, -line);
  1859. for(j = 0; j < 5; j++) {
  1860. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1861. }
  1862. ShowError("%s", StringBuf_Value(&buf));
  1863. StringBuf_Destroy(&buf);
  1864. }
  1865. /*==========================================
  1866. * スクリプトの解析
  1867. *------------------------------------------*/
  1868. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1869. {
  1870. const char *p,*tmpp;
  1871. int i;
  1872. struct script_code* code = NULL;
  1873. static int first=1;
  1874. char end;
  1875. bool unresolved_names = false;
  1876. if( src == NULL )
  1877. return NULL;// empty script
  1878. memset(&syntax,0,sizeof(syntax));
  1879. if(first){
  1880. add_buildin_func();
  1881. read_constdb();
  1882. first=0;
  1883. }
  1884. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1885. script_pos=0;
  1886. script_size=SCRIPT_BLOCK_SIZE;
  1887. parse_nextline(true, NULL);
  1888. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1889. if( options&SCRIPT_USE_LABEL_DB )
  1890. scriptlabel_db->clear(scriptlabel_db, NULL);
  1891. parse_options = options;
  1892. if( setjmp( error_jump ) != 0 ) {
  1893. //Restore program state when script has problems. [from jA]
  1894. int i;
  1895. const int size = ARRAYLENGTH(syntax.curly);
  1896. if( error_report )
  1897. script_error(src,file,line,error_msg,error_pos);
  1898. aFree( error_msg );
  1899. aFree( script_buf );
  1900. script_pos = 0;
  1901. script_size = 0;
  1902. script_buf = NULL;
  1903. for(i=LABEL_START;i<str_num;i++)
  1904. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1905. for(i=0; i<size; i++)
  1906. linkdb_final(&syntax.curly[i].case_label);
  1907. return NULL;
  1908. }
  1909. parse_syntax_for_flag=0;
  1910. p=src;
  1911. p=skip_space(p);
  1912. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1913. {// does not require brackets around the script
  1914. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1915. {// empty script and can return NULL
  1916. aFree( script_buf );
  1917. script_pos = 0;
  1918. script_size = 0;
  1919. script_buf = NULL;
  1920. return NULL;
  1921. }
  1922. end = '\0';
  1923. }
  1924. else
  1925. {// requires brackets around the script
  1926. if( *p != '{' )
  1927. disp_error_message("not found '{'",p);
  1928. p = skip_space(p+1);
  1929. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1930. {// empty script and can return NULL
  1931. aFree( script_buf );
  1932. script_pos = 0;
  1933. script_size = 0;
  1934. script_buf = NULL;
  1935. return NULL;
  1936. }
  1937. end = '}';
  1938. }
  1939. // clear references of labels, variables and internal functions
  1940. for(i=LABEL_START;i<str_num;i++){
  1941. if(
  1942. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1943. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1944. ){
  1945. str_data[i].type=C_NOP;
  1946. str_data[i].backpatch=-1;
  1947. str_data[i].label=-1;
  1948. }
  1949. }
  1950. while( syntax.curly_count != 0 || *p != end )
  1951. {
  1952. if( *p == '\0' )
  1953. disp_error_message("unexpected end of script",p);
  1954. // labelだけ特殊処理
  1955. tmpp=skip_space(skip_word(p));
  1956. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1957. i=add_word(p);
  1958. set_label(i,script_pos,p);
  1959. if( parse_options&SCRIPT_USE_LABEL_DB )
  1960. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1961. p=tmpp+1;
  1962. p=skip_space(p);
  1963. continue;
  1964. }
  1965. // 他は全部一緒くた
  1966. p=parse_line(p);
  1967. p=skip_space(p);
  1968. parse_nextline(false, p);
  1969. }
  1970. add_scriptc(C_NOP);
  1971. // trim code to size
  1972. script_size = script_pos;
  1973. RECREATE(script_buf,unsigned char,script_pos);
  1974. // default unknown references to variables
  1975. for(i=LABEL_START;i<str_num;i++){
  1976. if(str_data[i].type==C_NOP){
  1977. int j,next;
  1978. str_data[i].type=C_NAME;
  1979. str_data[i].label=i;
  1980. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1981. next=GETVALUE(script_buf,j);
  1982. SETVALUE(script_buf,j,i);
  1983. j=next;
  1984. }
  1985. }
  1986. else if( str_data[i].type == C_USERFUNC )
  1987. {// 'function name;' without follow-up code
  1988. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  1989. unresolved_names = true;
  1990. }
  1991. }
  1992. if( unresolved_names )
  1993. {
  1994. disp_error_message("parse_script: unresolved function references", p);
  1995. }
  1996. #ifdef DEBUG_DISP
  1997. for(i=0;i<script_pos;i++){
  1998. if((i&15)==0) ShowMessage("%04x : ",i);
  1999. ShowMessage("%02x ",script_buf[i]);
  2000. if((i&15)==15) ShowMessage("\n");
  2001. }
  2002. ShowMessage("\n");
  2003. #endif
  2004. #ifdef DEBUG_DISASM
  2005. {
  2006. int i = 0,j;
  2007. while(i < script_pos) {
  2008. c_op op = get_com(script_buf,&i);
  2009. ShowMessage("%06x %s", i, script_op2name(op));
  2010. j = i;
  2011. switch(op) {
  2012. case C_INT:
  2013. ShowMessage(" %d", get_num(script_buf,&i));
  2014. break;
  2015. case C_POS:
  2016. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2017. i += 3;
  2018. break;
  2019. case C_NAME:
  2020. j = (*(int*)(script_buf+i)&0xffffff);
  2021. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2022. i += 3;
  2023. break;
  2024. case C_STR:
  2025. j = strlen(script_buf + i);
  2026. ShowMessage(" %s", script_buf + i);
  2027. i += j+1;
  2028. break;
  2029. }
  2030. ShowMessage(CL_CLL"\n");
  2031. }
  2032. }
  2033. #endif
  2034. CREATE(code,struct script_code,1);
  2035. code->script_buf = script_buf;
  2036. code->script_size = script_size;
  2037. code->script_vars = NULL;
  2038. return code;
  2039. }
  2040. /// Returns the player attached to this script, identified by the rid.
  2041. /// If there is no player attached, the script is terminated.
  2042. TBL_PC *script_rid2sd(struct script_state *st)
  2043. {
  2044. TBL_PC *sd=map_id2sd(st->rid);
  2045. if(!sd){
  2046. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2047. script_reportfunc(st);
  2048. script_reportsrc(st);
  2049. st->state = END;
  2050. }
  2051. return sd;
  2052. }
  2053. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2054. ///
  2055. /// @param st Script state
  2056. /// @param data Variable/constant
  2057. void get_val(struct script_state* st, struct script_data* data)
  2058. {
  2059. const char* name;
  2060. char prefix;
  2061. char postfix;
  2062. TBL_PC* sd = NULL;
  2063. if( !data_isreference(data) )
  2064. return;// not a variable/constant
  2065. name = reference_getname(data);
  2066. prefix = name[0];
  2067. postfix = name[strlen(name) - 1];
  2068. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2069. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2070. {
  2071. sd = script_rid2sd(st);
  2072. if( sd == NULL )
  2073. {// needs player attached
  2074. if( postfix == '$' )
  2075. {// string variable
  2076. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2077. data->type = C_CONSTSTR;
  2078. data->u.str = "";
  2079. }
  2080. else
  2081. {// integer variable
  2082. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2083. data->type = C_INT;
  2084. data->u.num = 0;
  2085. }
  2086. return;
  2087. }
  2088. }
  2089. if( postfix == '$' )
  2090. {// string variable
  2091. switch( prefix )
  2092. {
  2093. case '@':
  2094. data->u.str = pc_readregstr(sd, data->u.num);
  2095. break;
  2096. case '$':
  2097. data->u.str = mapreg_readregstr(data->u.num);
  2098. break;
  2099. case '#':
  2100. if( name[1] == '#' )
  2101. data->u.str = pc_readaccountreg2str(sd, name);// global
  2102. else
  2103. data->u.str = pc_readaccountregstr(sd, name);// local
  2104. break;
  2105. case '.':
  2106. {
  2107. struct linkdb_node** n =
  2108. data->ref ? data->ref:
  2109. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2110. &st->script->script_vars;// npc variable
  2111. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2112. }
  2113. break;
  2114. case '\'':
  2115. {
  2116. struct linkdb_node** n = NULL;
  2117. if( st->instance_id )
  2118. n = &instance[st->instance_id].svar;
  2119. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2120. }
  2121. break;
  2122. default:
  2123. data->u.str = pc_readglobalreg_str(sd, name);
  2124. break;
  2125. }
  2126. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2127. {// empty string
  2128. data->type = C_CONSTSTR;
  2129. data->u.str = "";
  2130. }
  2131. else
  2132. {// duplicate string
  2133. data->type = C_STR;
  2134. data->u.str = aStrdup(data->u.str);
  2135. }
  2136. }
  2137. else
  2138. {// integer variable
  2139. data->type = C_INT;
  2140. if( reference_toconstant(data) )
  2141. {
  2142. data->u.num = reference_getconstant(data);
  2143. }
  2144. else if( reference_toparam(data) )
  2145. {
  2146. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2147. }
  2148. else
  2149. switch( prefix )
  2150. {
  2151. case '@':
  2152. data->u.num = pc_readreg(sd, data->u.num);
  2153. break;
  2154. case '$':
  2155. data->u.num = mapreg_readreg(data->u.num);
  2156. break;
  2157. case '#':
  2158. if( name[1] == '#' )
  2159. data->u.num = pc_readaccountreg2(sd, name);// global
  2160. else
  2161. data->u.num = pc_readaccountreg(sd, name);// local
  2162. break;
  2163. case '.':
  2164. {
  2165. struct linkdb_node** n =
  2166. data->ref ? data->ref:
  2167. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2168. &st->script->script_vars;// npc variable
  2169. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2170. }
  2171. break;
  2172. case '\'':
  2173. {
  2174. struct linkdb_node** n = NULL;
  2175. if( st->instance_id )
  2176. n = &instance[st->instance_id].ivar;
  2177. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2178. }
  2179. break;
  2180. default:
  2181. data->u.num = pc_readglobalreg(sd, name);
  2182. break;
  2183. }
  2184. }
  2185. return;
  2186. }
  2187. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2188. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2189. /// The value is left in the top of the stack and needs to be removed manually.
  2190. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2191. {
  2192. struct script_data* data;
  2193. push_val2(st->stack, C_NAME, uid, ref);
  2194. data = script_getdatatop(st, -1);
  2195. get_val(st, data);
  2196. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2197. }
  2198. /*==========================================
  2199. * Stores the value of a script variable
  2200. * Return value is 0 on fail, 1 on success.
  2201. *------------------------------------------*/
  2202. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2203. {
  2204. char prefix = name[0];
  2205. if( is_string_variable(name) )
  2206. {// string variable
  2207. const char* str = (const char*)value;
  2208. switch (prefix) {
  2209. case '@':
  2210. return pc_setregstr(sd, num, str);
  2211. case '$':
  2212. return mapreg_setregstr(num, str);
  2213. case '#':
  2214. return (name[1] == '#') ?
  2215. pc_setaccountreg2str(sd, name, str) :
  2216. pc_setaccountregstr(sd, name, str);
  2217. case '.': {
  2218. char* p;
  2219. struct linkdb_node** n;
  2220. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2221. p = (char*)linkdb_erase(n, (void*)num);
  2222. if (p) aFree(p);
  2223. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2224. }
  2225. return 1;
  2226. case '\'': {
  2227. char *p;
  2228. struct linkdb_node** n = NULL;
  2229. if( st->instance_id )
  2230. n = &instance[st->instance_id].svar;
  2231. p = (char*)linkdb_erase(n, (void*)num);
  2232. if (p) aFree(p);
  2233. if( str[0] ) linkdb_insert(n, (void*)num, aStrdup(str));
  2234. }
  2235. return 1;
  2236. default:
  2237. return pc_setglobalreg_str(sd, name, str);
  2238. }
  2239. }
  2240. else
  2241. {// integer variable
  2242. int val = (int)value;
  2243. if(str_data[num&0x00ffffff].type == C_PARAM)
  2244. {
  2245. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2246. {
  2247. if( st != NULL )
  2248. {
  2249. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2250. script_reportsrc(st);
  2251. st->state = END;
  2252. }
  2253. return 0;
  2254. }
  2255. return 1;
  2256. }
  2257. switch (prefix) {
  2258. case '@':
  2259. return pc_setreg(sd, num, val);
  2260. case '$':
  2261. return mapreg_setreg(num, val);
  2262. case '#':
  2263. return (name[1] == '#') ?
  2264. pc_setaccountreg2(sd, name, val) :
  2265. pc_setaccountreg(sd, name, val);
  2266. case '.': {
  2267. struct linkdb_node** n;
  2268. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2269. if (val == 0)
  2270. linkdb_erase(n, (void*)num);
  2271. else
  2272. linkdb_replace(n, (void*)num, (void*)val);
  2273. }
  2274. return 1;
  2275. case '\'':
  2276. {
  2277. struct linkdb_node** n = NULL;
  2278. if( st->instance_id )
  2279. n = &instance[st->instance_id].ivar;
  2280. if( val == 0 )
  2281. linkdb_erase(n, (void*)num);
  2282. else
  2283. linkdb_replace(n, (void*)num, (void*)val);
  2284. return 1;
  2285. }
  2286. default:
  2287. return pc_setglobalreg(sd, name, val);
  2288. }
  2289. }
  2290. }
  2291. int set_var(TBL_PC* sd, char* name, void* val)
  2292. {
  2293. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2294. }
  2295. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2296. {
  2297. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2298. }
  2299. /// Converts the data to a string
  2300. const char* conv_str(struct script_state* st, struct script_data* data)
  2301. {
  2302. char* p;
  2303. get_val(st, data);
  2304. if( data_isstring(data) )
  2305. {// nothing to convert
  2306. }
  2307. else if( data_isint(data) )
  2308. {// int -> string
  2309. CREATE(p, char, ITEM_NAME_LENGTH);
  2310. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2311. p[ITEM_NAME_LENGTH-1] = '\0';
  2312. data->type = C_STR;
  2313. data->u.str = p;
  2314. }
  2315. else if( data_isreference(data) )
  2316. {// reference -> string
  2317. //##TODO when does this happen (check get_val) [FlavioJS]
  2318. data->type = C_CONSTSTR;
  2319. data->u.str = reference_getname(data);
  2320. }
  2321. else
  2322. {// unsupported data type
  2323. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2324. script_reportdata(data);
  2325. script_reportsrc(st);
  2326. data->type = C_CONSTSTR;
  2327. data->u.str = "";
  2328. }
  2329. return data->u.str;
  2330. }
  2331. /// Converts the data to an int
  2332. int conv_num(struct script_state* st, struct script_data* data)
  2333. {
  2334. char* p;
  2335. long num;
  2336. get_val(st, data);
  2337. if( data_isint(data) )
  2338. {// nothing to convert
  2339. }
  2340. else if( data_isstring(data) )
  2341. {// string -> int
  2342. // the result does not overflow or underflow, it is capped instead
  2343. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2344. p = data->u.str;
  2345. errno = 0;
  2346. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2347. if( errno == ERANGE
  2348. #if LONG_MAX > INT_MAX
  2349. || num < INT_MIN || num > INT_MAX
  2350. #endif
  2351. )
  2352. {
  2353. if( num <= INT_MIN )
  2354. {
  2355. num = INT_MIN;
  2356. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2357. }
  2358. else//if( num >= INT_MAX )
  2359. {
  2360. num = INT_MAX;
  2361. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2362. }
  2363. script_reportdata(data);
  2364. script_reportsrc(st);
  2365. }
  2366. if( data->type == C_STR )
  2367. aFree(p);
  2368. data->type = C_INT;
  2369. data->u.num = (int)num;
  2370. }
  2371. #if 0
  2372. // FIXME this function is being used to retrieve the position of labels and
  2373. // probably other stuff [FlavioJS]
  2374. else
  2375. {// unsupported data type
  2376. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2377. script_reportdata(data);
  2378. script_reportsrc(st);
  2379. data->type = C_INT;
  2380. data->u.num = 0;
  2381. }
  2382. #endif
  2383. return data->u.num;
  2384. }
  2385. //
  2386. // Stack operations
  2387. //
  2388. /// Increases the size of the stack
  2389. void stack_expand(struct script_stack* stack)
  2390. {
  2391. stack->sp_max += 64;
  2392. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2393. stack->sp_max * sizeof(stack->stack_data[0]) );
  2394. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2395. 64 * sizeof(stack->stack_data[0]) );
  2396. }
  2397. /// Pushes a value into the stack
  2398. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2399. /// Pushes a value into the stack (with reference)
  2400. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2401. {
  2402. if( stack->sp >= stack->sp_max )
  2403. stack_expand(stack);
  2404. stack->stack_data[stack->sp].type = type;
  2405. stack->stack_data[stack->sp].u.num = val;
  2406. stack->stack_data[stack->sp].ref = ref;
  2407. stack->sp++;
  2408. return &stack->stack_data[stack->sp-1];
  2409. }
  2410. /// Pushes a string into the stack
  2411. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2412. {
  2413. if( stack->sp >= stack->sp_max )
  2414. stack_expand(stack);
  2415. stack->stack_data[stack->sp].type = type;
  2416. stack->stack_data[stack->sp].u.str = str;
  2417. stack->stack_data[stack->sp].ref = NULL;
  2418. stack->sp++;
  2419. return &stack->stack_data[stack->sp-1];
  2420. }
  2421. /// Pushes a retinfo into the stack
  2422. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2423. {
  2424. if( stack->sp >= stack->sp_max )
  2425. stack_expand(stack);
  2426. stack->stack_data[stack->sp].type = C_RETINFO;
  2427. stack->stack_data[stack->sp].u.ri = ri;
  2428. stack->stack_data[stack->sp].ref = NULL;
  2429. stack->sp++;
  2430. return &stack->stack_data[stack->sp-1];
  2431. }
  2432. /// Pushes a copy of the target position into the stack
  2433. struct script_data* push_copy(struct script_stack* stack, int pos)
  2434. {
  2435. switch( stack->stack_data[pos].type )
  2436. {
  2437. case C_CONSTSTR:
  2438. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2439. break;
  2440. case C_STR:
  2441. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2442. break;
  2443. case C_RETINFO:
  2444. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2445. exit(1);
  2446. break;
  2447. default:
  2448. return push_val2(
  2449. stack,stack->stack_data[pos].type,
  2450. stack->stack_data[pos].u.num,
  2451. stack->stack_data[pos].ref
  2452. );
  2453. break;
  2454. }
  2455. }
  2456. /// Removes the values in indexes [start,end[ from the stack.
  2457. /// Adjusts all stack pointers.
  2458. void pop_stack(struct script_state* st, int start, int end)
  2459. {
  2460. struct script_stack* stack = st->stack;
  2461. struct script_data* data;
  2462. int i;
  2463. if( start < 0 )
  2464. start = 0;
  2465. if( end > stack->sp )
  2466. end = stack->sp;
  2467. if( start >= end )
  2468. return;// nothing to pop
  2469. // free stack elements
  2470. for( i = start; i < end; i++ )
  2471. {
  2472. data = &stack->stack_data[i];
  2473. if( data->type == C_STR )
  2474. aFree(data->u.str);
  2475. if( data->type == C_RETINFO )
  2476. {
  2477. struct script_retinfo* ri = data->u.ri;
  2478. if( ri->var_function )
  2479. {
  2480. script_free_vars(ri->var_function);
  2481. aFree(ri->var_function);
  2482. }
  2483. aFree(ri);
  2484. }
  2485. data->type = C_NOP;
  2486. }
  2487. // move the rest of the elements
  2488. if( stack->sp > end )
  2489. {
  2490. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2491. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2492. stack->stack_data[i].type = C_NOP;
  2493. }
  2494. // adjust stack pointers
  2495. if( st->start > end ) st->start -= end - start;
  2496. else if( st->start > start ) st->start = start;
  2497. if( st->end > end ) st->end -= end - start;
  2498. else if( st->end > start ) st->end = start;
  2499. if( stack->defsp > end ) stack->defsp -= end - start;
  2500. else if( stack->defsp > start ) stack->defsp = start;
  2501. stack->sp -= end - start;
  2502. }
  2503. ///
  2504. ///
  2505. ///
  2506. /*==========================================
  2507. * スクリプト依存変数、関数依存変数の解放
  2508. *------------------------------------------*/
  2509. void script_free_vars(struct linkdb_node **node)
  2510. {
  2511. struct linkdb_node* n = *node;
  2512. while( n != NULL)
  2513. {
  2514. const char* name = get_str((int)(n->key)&0x00ffffff);
  2515. if( is_string_variable(name) )
  2516. aFree(n->data); // 文字型変数なので、データ削除
  2517. n = n->next;
  2518. }
  2519. linkdb_final( node );
  2520. }
  2521. void script_free_code(struct script_code* code)
  2522. {
  2523. script_free_vars( &code->script_vars );
  2524. aFree( code->script_buf );
  2525. aFree( code );
  2526. }
  2527. /// Creates a new script state.
  2528. ///
  2529. /// @param script Script code
  2530. /// @param pos Position in the code
  2531. /// @param rid Who is running the script (attached player)
  2532. /// @param oid Where the code is being run (npc 'object')
  2533. /// @return Script state
  2534. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2535. {
  2536. struct script_state* st;
  2537. CREATE(st, struct script_state, 1);
  2538. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2539. st->stack->sp = 0;
  2540. st->stack->sp_max = 64;
  2541. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2542. st->stack->defsp = st->stack->sp;
  2543. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2544. st->state = RUN;
  2545. st->script = script;
  2546. //st->scriptroot = script;
  2547. st->pos = pos;
  2548. st->rid = rid;
  2549. st->oid = oid;
  2550. st->sleep.timer = INVALID_TIMER;
  2551. return st;
  2552. }
  2553. /// Frees a script state.
  2554. ///
  2555. /// @param st Script state
  2556. void script_free_state(struct script_state* st)
  2557. {
  2558. if(st->bk_st)
  2559. {// backup was not restored
  2560. 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);
  2561. }
  2562. if( st->sleep.timer != INVALID_TIMER )
  2563. delete_timer(st->sleep.timer, run_script_timer);
  2564. script_free_vars(st->stack->var_function);
  2565. aFree(st->stack->var_function);
  2566. pop_stack(st, 0, st->stack->sp);
  2567. aFree(st->stack->stack_data);
  2568. aFree(st->stack);
  2569. st->pos = -1;
  2570. aFree(st);
  2571. }
  2572. //
  2573. // 実行部main
  2574. //
  2575. /*==========================================
  2576. * コマンドの読み取り
  2577. *------------------------------------------*/
  2578. c_op get_com(unsigned char *script,int *pos)
  2579. {
  2580. int i = 0, j = 0;
  2581. if(script[*pos]>=0x80){
  2582. return C_INT;
  2583. }
  2584. while(script[*pos]>=0x40){
  2585. i=script[(*pos)++]<<j;
  2586. j+=6;
  2587. }
  2588. return (c_op)(i+(script[(*pos)++]<<j));
  2589. }
  2590. /*==========================================
  2591. * 数値の所得
  2592. *------------------------------------------*/
  2593. int get_num(unsigned char *script,int *pos)
  2594. {
  2595. int i,j;
  2596. i=0; j=0;
  2597. while(script[*pos]>=0xc0){
  2598. i+=(script[(*pos)++]&0x7f)<<j;
  2599. j+=6;
  2600. }
  2601. return i+((script[(*pos)++]&0x7f)<<j);
  2602. }
  2603. /*==========================================
  2604. * スタックから値を取り出す
  2605. *------------------------------------------*/
  2606. int pop_val(struct script_state* st)
  2607. {
  2608. if(st->stack->sp<=0)
  2609. return 0;
  2610. st->stack->sp--;
  2611. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2612. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2613. return st->stack->stack_data[st->stack->sp].u.num;
  2614. return 0;
  2615. }
  2616. /// Ternary operators
  2617. /// test ? if_true : if_false
  2618. void op_3(struct script_state* st, int op)
  2619. {
  2620. struct script_data* data;
  2621. int flag = 0;
  2622. data = script_getdatatop(st, -3);
  2623. get_val(st, data);
  2624. if( data_isstring(data) )
  2625. flag = data->u.str[0];// "" -> false
  2626. else if( data_isint(data) )
  2627. flag = data->u.num;// 0 -> false
  2628. else
  2629. {
  2630. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2631. script_reportdata(data);
  2632. script_reportsrc(st);
  2633. script_removetop(st, -3, 0);
  2634. script_pushnil(st);
  2635. return;
  2636. }
  2637. if( flag )
  2638. script_pushcopytop(st, -2);
  2639. else
  2640. script_pushcopytop(st, -1);
  2641. script_removetop(st, -4, -1);
  2642. }
  2643. /// Binary string operators
  2644. /// s1 EQ s2 -> i
  2645. /// s1 NE s2 -> i
  2646. /// s1 GT s2 -> i
  2647. /// s1 GE s2 -> i
  2648. /// s1 LT s2 -> i
  2649. /// s1 LE s2 -> i
  2650. /// s1 ADD s2 -> s
  2651. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2652. {
  2653. int a = 0;
  2654. switch(op){
  2655. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2656. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2657. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2658. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2659. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2660. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2661. case C_ADD:
  2662. {
  2663. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2664. strcpy(buf, s1);
  2665. strcat(buf, s2);
  2666. script_pushstr(st, buf);
  2667. return;
  2668. }
  2669. default:
  2670. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2671. script_reportsrc(st);
  2672. script_pushnil(st);
  2673. st->state = END;
  2674. return;
  2675. }
  2676. script_pushint(st,a);
  2677. }
  2678. /// Binary number operators
  2679. /// i OP i -> i
  2680. void op_2num(struct script_state* st, int op, int i1, int i2)
  2681. {
  2682. int ret;
  2683. double ret_double;
  2684. switch( op )
  2685. {
  2686. case C_AND: ret = i1 & i2; break;
  2687. case C_OR: ret = i1 | i2; break;
  2688. case C_XOR: ret = i1 ^ i2; break;
  2689. case C_LAND: ret = (i1 && i2); break;
  2690. case C_LOR: ret = (i1 || i2); break;
  2691. case C_EQ: ret = (i1 == i2); break;
  2692. case C_NE: ret = (i1 != i2); break;
  2693. case C_GT: ret = (i1 > i2); break;
  2694. case C_GE: ret = (i1 >= i2); break;
  2695. case C_LT: ret = (i1 < i2); break;
  2696. case C_LE: ret = (i1 <= i2); break;
  2697. case C_R_SHIFT: ret = i1>>i2; break;
  2698. case C_L_SHIFT: ret = i1<<i2; break;
  2699. case C_DIV:
  2700. case C_MOD:
  2701. if( i2 == 0 )
  2702. {
  2703. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2704. script_reportsrc(st);
  2705. script_pushnil(st);
  2706. st->state = END;
  2707. return;
  2708. }
  2709. else if( op == C_DIV )
  2710. ret = i1 / i2;
  2711. else//if( op == C_MOD )
  2712. ret = i1 % i2;
  2713. break;
  2714. default:
  2715. switch( op )
  2716. {// operators that can overflow/underflow
  2717. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2718. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2719. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2720. default:
  2721. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2722. script_reportsrc(st);
  2723. script_pushnil(st);
  2724. return;
  2725. }
  2726. if( ret_double < (double)INT_MIN )
  2727. {
  2728. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2729. script_reportsrc(st);
  2730. ret = INT_MIN;
  2731. }
  2732. else if( ret_double > (double)INT_MAX )
  2733. {
  2734. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2735. script_reportsrc(st);
  2736. ret = INT_MAX;
  2737. }
  2738. }
  2739. script_pushint(st, ret);
  2740. }
  2741. /// Binary operators
  2742. void op_2(struct script_state *st, int op)
  2743. {
  2744. struct script_data* left;
  2745. struct script_data* right;
  2746. left = script_getdatatop(st, -2);
  2747. right = script_getdatatop(st, -1);
  2748. get_val(st, left);
  2749. get_val(st, right);
  2750. // automatic conversions
  2751. switch( op )
  2752. {
  2753. case C_ADD:
  2754. if( data_isint(left) && data_isstring(right) )
  2755. {// convert int-string to string-string
  2756. conv_str(st, left);
  2757. }
  2758. else if( data_isstring(left) && data_isint(right) )
  2759. {// convert string-int to string-string
  2760. conv_str(st, right);
  2761. }
  2762. break;
  2763. }
  2764. if( data_isstring(left) && data_isstring(right) )
  2765. {// ss => op_2str
  2766. op_2str(st, op, left->u.str, right->u.str);
  2767. script_removetop(st, -3, -1);// pop the two values before the top one
  2768. }
  2769. else if( data_isint(left) && data_isint(right) )
  2770. {// ii => op_2num
  2771. int i1 = left->u.num;
  2772. int i2 = right->u.num;
  2773. script_removetop(st, -2, 0);
  2774. op_2num(st, op, i1, i2);
  2775. }
  2776. else
  2777. {// invalid argument
  2778. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2779. script_reportdata(left);
  2780. script_reportdata(right);
  2781. script_reportsrc(st);
  2782. script_removetop(st, -2, 0);
  2783. script_pushnil(st);
  2784. st->state = END;
  2785. }
  2786. }
  2787. /// Unary operators
  2788. /// NEG i -> i
  2789. /// NOT i -> i
  2790. /// LNOT i -> i
  2791. void op_1(struct script_state* st, int op)
  2792. {
  2793. struct script_data* data;
  2794. int i1;
  2795. data = script_getdatatop(st, -1);
  2796. get_val(st, data);
  2797. if( !data_isint(data) )
  2798. {// not a number
  2799. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2800. script_reportdata(data);
  2801. script_reportsrc(st);
  2802. script_pushnil(st);
  2803. st->state = END;
  2804. return;
  2805. }
  2806. i1 = data->u.num;
  2807. script_removetop(st, -1, 0);
  2808. switch( op )
  2809. {
  2810. case C_NEG: i1 = -i1; break;
  2811. case C_NOT: i1 = ~i1; break;
  2812. case C_LNOT: i1 = !i1; break;
  2813. default:
  2814. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2815. script_reportsrc(st);
  2816. script_pushnil(st);
  2817. st->state = END;
  2818. return;
  2819. }
  2820. script_pushint(st, i1);
  2821. }
  2822. /// Checks the type of all arguments passed to a built-in function.
  2823. ///
  2824. /// @param st Script state whose stack arguments should be inspected.
  2825. /// @param func Built-in function for which the arguments are intended.
  2826. static void script_check_buildin_argtype(struct script_state* st, int func)
  2827. {
  2828. char type;
  2829. int idx, invalid = 0;
  2830. script_function* sf = &buildin_func[str_data[func].val];
  2831. for( idx = 2; script_hasdata(st, idx); idx++ )
  2832. {
  2833. struct script_data* data = script_getdata(st, idx);
  2834. type = sf->arg[idx-2];
  2835. if( type == '?' || type == '*' )
  2836. {// optional argument or unknown number of optional parameters ( no types are after this )
  2837. break;
  2838. }
  2839. else if( type == 0 )
  2840. {// more arguments than necessary ( should not happen, as it is checked before )
  2841. ShowWarning("Found more arguments than necessary.\n");
  2842. invalid++;
  2843. break;
  2844. }
  2845. else
  2846. {
  2847. const char* name = NULL;
  2848. if( data_isreference(data) )
  2849. {// get name for variables to determine the type they refer to
  2850. name = reference_getname(data);
  2851. }
  2852. switch( type )
  2853. {
  2854. case 'v':
  2855. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  2856. {// variant
  2857. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  2858. script_reportdata(data);
  2859. invalid++;
  2860. }
  2861. break;
  2862. case 's':
  2863. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  2864. {// string
  2865. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  2866. script_reportdata(data);
  2867. invalid++;
  2868. }
  2869. break;
  2870. case 'i':
  2871. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  2872. {// int ( params and constants are always int )
  2873. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  2874. script_reportdata(data);
  2875. invalid++;
  2876. }
  2877. break;
  2878. case 'r':
  2879. if( !data_isreference(data) )
  2880. {// variables
  2881. ShowWarning("Unexpected type for argument %d. Expected variable.\n", idx-1);
  2882. script_reportdata(data);
  2883. invalid++;
  2884. }
  2885. break;
  2886. case 'l':
  2887. if( !data_islabel(data) && !data_isfunclabel(data) )
  2888. {// label
  2889. ShowWarning("Unexpected type for argument %d. Expected label.\n", idx-1);
  2890. script_reportdata(data);
  2891. invalid++;
  2892. }
  2893. break;
  2894. }
  2895. }
  2896. }
  2897. if(invalid)
  2898. {
  2899. ShowDebug("Function: %s\n", get_str(func));
  2900. script_reportsrc(st);
  2901. }
  2902. }
  2903. /// Executes a buildin command.
  2904. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2905. int run_func(struct script_state *st)
  2906. {
  2907. struct script_data* data;
  2908. int i,start_sp,end_sp,func;
  2909. end_sp = st->stack->sp;// position after the last argument
  2910. for( i = end_sp-1; i > 0 ; --i )
  2911. if( st->stack->stack_data[i].type == C_ARG )
  2912. break;
  2913. if( i == 0 )
  2914. {
  2915. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2916. st->state = END;
  2917. script_reportsrc(st);
  2918. return 1;
  2919. }
  2920. start_sp = i-1;// C_NAME of the command
  2921. st->start = start_sp;
  2922. st->end = end_sp;
  2923. data = &st->stack->stack_data[st->start];
  2924. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2925. func = data->u.num;
  2926. else
  2927. {
  2928. ShowError("script:run_func: not a buildin command.\n");
  2929. script_reportdata(data);
  2930. script_reportsrc(st);
  2931. st->state = END;
  2932. return 1;
  2933. }
  2934. if( script_config.warn_func_mismatch_argtypes )
  2935. {
  2936. script_check_buildin_argtype(st, func);
  2937. }
  2938. if(str_data[func].func){
  2939. if (str_data[func].func(st)) //Report error
  2940. script_reportsrc(st);
  2941. } else {
  2942. 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));
  2943. script_reportsrc(st);
  2944. st->state = END;
  2945. }
  2946. // Stack's datum are used when re-running functions [Eoe]
  2947. if( st->state == RERUNLINE )
  2948. return 0;
  2949. pop_stack(st, st->start, st->end);
  2950. if( st->state == RETFUNC )
  2951. {// return from a user-defined function
  2952. struct script_retinfo* ri;
  2953. int olddefsp = st->stack->defsp;
  2954. int nargs;
  2955. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2956. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2957. {
  2958. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  2959. script_reportsrc(st);
  2960. st->state = END;
  2961. return 1;
  2962. }
  2963. script_free_vars( st->stack->var_function );
  2964. aFree(st->stack->var_function);
  2965. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  2966. nargs = ri->nargs;
  2967. st->pos = ri->pos;
  2968. st->script = ri->script;
  2969. st->stack->var_function = ri->var_function;
  2970. st->stack->defsp = ri->defsp;
  2971. memset(ri, 0, sizeof(struct script_retinfo));
  2972. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  2973. st->state = GOTO;
  2974. }
  2975. return 0;
  2976. }
  2977. /*==========================================
  2978. * script execution
  2979. *------------------------------------------*/
  2980. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2981. {
  2982. struct script_state *st;
  2983. if( rootscript == NULL || pos < 0 )
  2984. return;
  2985. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  2986. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  2987. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  2988. st = script_alloc_state(rootscript, pos, rid, oid);
  2989. run_script_main(st);
  2990. }
  2991. void script_stop_sleeptimers(int id)
  2992. {
  2993. struct script_state* st;
  2994. for(;;)
  2995. {
  2996. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2997. if( st == NULL )
  2998. break; // no more sleep timers
  2999. script_free_state(st);
  3000. }
  3001. }
  3002. /*==========================================
  3003. * 指定ノードをsleep_dbから削除
  3004. *------------------------------------------*/
  3005. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3006. {
  3007. struct linkdb_node *retnode;
  3008. if( n == NULL)
  3009. return NULL;
  3010. if( n->prev == NULL )
  3011. sleep_db = n->next;
  3012. else
  3013. n->prev->next = n->next;
  3014. if( n->next )
  3015. n->next->prev = n->prev;
  3016. retnode = n->next;
  3017. aFree( n );
  3018. return retnode; // 次のノードを返す
  3019. }
  3020. /*==========================================
  3021. * sleep用タイマー関数
  3022. *------------------------------------------*/
  3023. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3024. {
  3025. struct script_state *st = (struct script_state *)data;
  3026. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3027. TBL_PC *sd = map_id2sd(st->rid);
  3028. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3029. { //Character mismatch. Cancel execution.
  3030. st->rid = 0;
  3031. st->state = END;
  3032. }
  3033. while( node && st->sleep.timer != INVALID_TIMER ) {
  3034. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3035. script_erase_sleepdb(node);
  3036. st->sleep.timer = INVALID_TIMER;
  3037. break;
  3038. }
  3039. node = node->next;
  3040. }
  3041. if(st->state != RERUNLINE)
  3042. st->sleep.tick = 0;
  3043. run_script_main(st);
  3044. return 0;
  3045. }
  3046. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3047. ///
  3048. /// @param st Script state to detach.
  3049. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3050. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3051. {
  3052. struct map_session_data* sd;
  3053. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3054. {
  3055. sd->st = st->bk_st;
  3056. sd->npc_id = st->bk_npcid;
  3057. if(st->bk_st)
  3058. {
  3059. //Remove tag for removal.
  3060. st->bk_st = NULL;
  3061. st->bk_npcid = 0;
  3062. }
  3063. else if(dequeue_event)
  3064. {
  3065. npc_event_dequeue(sd);
  3066. }
  3067. }
  3068. else if(st->bk_st)
  3069. {// rid was set to 0, before detaching the script state
  3070. 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);
  3071. script_reportsrc(st->bk_st);
  3072. script_free_state(st->bk_st);
  3073. st->bk_st = NULL;
  3074. }
  3075. }
  3076. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3077. ///
  3078. /// @param st Script state to attach.
  3079. static void script_attach_state(struct script_state* st)
  3080. {
  3081. struct map_session_data* sd;
  3082. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3083. {
  3084. if(st!=sd->st)
  3085. {
  3086. if(st->bk_st)
  3087. {// there is already a backup
  3088. 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);
  3089. }
  3090. st->bk_st = sd->st;
  3091. st->bk_npcid = sd->npc_id;
  3092. }
  3093. sd->st = st;
  3094. sd->npc_id = st->oid;
  3095. }
  3096. }
  3097. /*==========================================
  3098. * スクリプトの実行メイン部分
  3099. *------------------------------------------*/
  3100. void run_script_main(struct script_state *st)
  3101. {
  3102. int cmdcount=script_config.check_cmdcount;
  3103. int gotocount=script_config.check_gotocount;
  3104. TBL_PC *sd;
  3105. struct script_stack *stack=st->stack;
  3106. struct npc_data *nd;
  3107. script_attach_state(st);
  3108. nd = map_id2nd(st->oid);
  3109. if( nd && map[nd->bl.m].instance_id > 0 )
  3110. st->instance_id = map[nd->bl.m].instance_id;
  3111. if(st->state == RERUNLINE) {
  3112. run_func(st);
  3113. if(st->state == GOTO)
  3114. st->state = RUN;
  3115. } else if(st->state != END)
  3116. st->state = RUN;
  3117. while(st->state == RUN)
  3118. {
  3119. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3120. switch(c){
  3121. case C_EOL:
  3122. if( stack->defsp > stack->sp )
  3123. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3124. else
  3125. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3126. break;
  3127. case C_INT:
  3128. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3129. break;
  3130. case C_POS:
  3131. case C_NAME:
  3132. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3133. st->pos+=3;
  3134. break;
  3135. case C_ARG:
  3136. push_val(stack,c,0);
  3137. break;
  3138. case C_STR:
  3139. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3140. while(st->script->script_buf[st->pos++]);
  3141. break;
  3142. case C_FUNC:
  3143. run_func(st);
  3144. if(st->state==GOTO){
  3145. st->state = RUN;
  3146. if( gotocount>0 && (--gotocount)<=0 ){
  3147. ShowError("run_script: infinity loop !\n");
  3148. script_reportsrc(st);
  3149. st->state=END;
  3150. }
  3151. }
  3152. break;
  3153. case C_NEG:
  3154. case C_NOT:
  3155. case C_LNOT:
  3156. op_1(st ,c);
  3157. break;
  3158. case C_ADD:
  3159. case C_SUB:
  3160. case C_MUL:
  3161. case C_DIV:
  3162. case C_MOD:
  3163. case C_EQ:
  3164. case C_NE:
  3165. case C_GT:
  3166. case C_GE:
  3167. case C_LT:
  3168. case C_LE:
  3169. case C_AND:
  3170. case C_OR:
  3171. case C_XOR:
  3172. case C_LAND:
  3173. case C_LOR:
  3174. case C_R_SHIFT:
  3175. case C_L_SHIFT:
  3176. op_2(st, c);
  3177. break;
  3178. case C_OP3:
  3179. op_3(st, c);
  3180. break;
  3181. case C_NOP:
  3182. st->state=END;
  3183. break;
  3184. default:
  3185. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3186. st->state=END;
  3187. break;
  3188. }
  3189. if( cmdcount>0 && (--cmdcount)<=0 ){
  3190. ShowError("run_script: infinity loop !\n");
  3191. script_reportsrc(st);
  3192. st->state=END;
  3193. }
  3194. }
  3195. if(st->sleep.tick > 0) {
  3196. //Restore previous script
  3197. script_detach_state(st, false);
  3198. //Delay execution
  3199. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3200. st->sleep.charid = sd?sd->status.char_id:0;
  3201. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3202. run_script_timer, st->sleep.charid, (intptr_t)st);
  3203. linkdb_insert(&sleep_db, (void*)st->oid, st);
  3204. }
  3205. else if(st->state != END && st->rid){
  3206. //Resume later (st is already attached to player).
  3207. if(st->bk_st) {
  3208. ShowWarning("Unable to restore stack! Double continuation!\n");
  3209. //Report BOTH scripts to see if that can help somehow.
  3210. ShowDebug("Previous script (lost):\n");
  3211. script_reportsrc(st->bk_st);
  3212. ShowDebug("Current script:\n");
  3213. script_reportsrc(st);
  3214. script_free_state(st->bk_st);
  3215. st->bk_st = NULL;
  3216. }
  3217. } else {
  3218. //Dispose of script.
  3219. if ((sd = map_id2sd(st->rid))!=NULL)
  3220. { //Restore previous stack and save char.
  3221. if(sd->state.using_fake_npc){
  3222. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3223. sd->state.using_fake_npc = 0;
  3224. }
  3225. //Restore previous script if any.
  3226. script_detach_state(st, true);
  3227. if (sd->state.reg_dirty&2)
  3228. intif_saveregistry(sd,2);
  3229. if (sd->state.reg_dirty&1)
  3230. intif_saveregistry(sd,1);
  3231. }
  3232. script_free_state(st);
  3233. st = NULL;
  3234. }
  3235. }
  3236. int script_config_read(char *cfgName)
  3237. {
  3238. int i;
  3239. char line[1024],w1[1024],w2[1024];
  3240. FILE *fp;
  3241. fp=fopen(cfgName,"r");
  3242. if(fp==NULL){
  3243. ShowError("file not found: [%s]\n", cfgName);
  3244. return 1;
  3245. }
  3246. while(fgets(line, sizeof(line), fp))
  3247. {
  3248. if(line[0] == '/' && line[1] == '/')
  3249. continue;
  3250. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3251. if(i!=2)
  3252. continue;
  3253. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3254. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3255. }
  3256. else if(strcmpi(w1,"check_cmdcount")==0) {
  3257. script_config.check_cmdcount = config_switch(w2);
  3258. }
  3259. else if(strcmpi(w1,"check_gotocount")==0) {
  3260. script_config.check_gotocount = config_switch(w2);
  3261. }
  3262. else if(strcmpi(w1,"input_min_value")==0) {
  3263. script_config.input_min_value = config_switch(w2);
  3264. }
  3265. else if(strcmpi(w1,"input_max_value")==0) {
  3266. script_config.input_max_value = config_switch(w2);
  3267. }
  3268. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3269. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3270. }
  3271. else if(strcmpi(w1,"import")==0){
  3272. script_config_read(w2);
  3273. }
  3274. }
  3275. fclose(fp);
  3276. return 0;
  3277. }
  3278. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3279. {
  3280. struct script_code *code = (struct script_code *)data;
  3281. if(code){
  3282. script_free_vars( &code->script_vars );
  3283. aFree( code->script_buf );
  3284. aFree( code );
  3285. }
  3286. return 0;
  3287. }
  3288. static int do_final_autobonus_sub (DBKey key,void *data,va_list ap)
  3289. {
  3290. struct script_code *script = (struct script_code *)data;
  3291. if( script )
  3292. script_free_code(script);
  3293. return 0;
  3294. }
  3295. void script_run_autobonus(const char *autobonus, int id, int pos)
  3296. {
  3297. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3298. if( script )
  3299. {
  3300. current_equip_item_index = pos;
  3301. run_script(script,0,id,0);
  3302. }
  3303. }
  3304. void script_add_autobonus(const char *autobonus)
  3305. {
  3306. if( strdb_get(autobonus_db, autobonus) == NULL )
  3307. {
  3308. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3309. if( script )
  3310. strdb_put(autobonus_db, autobonus, script);
  3311. }
  3312. }
  3313. /// resets a temporary character array variable to given value
  3314. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3315. {
  3316. int key;
  3317. uint8 idx;
  3318. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3319. {
  3320. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3321. return;
  3322. }
  3323. key = add_str(varname);
  3324. if( is_string_variable(varname) )
  3325. {
  3326. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3327. {
  3328. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3329. }
  3330. }
  3331. else
  3332. {
  3333. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3334. {
  3335. pc_setreg(sd, reference_uid(key, idx), (int)value);
  3336. }
  3337. }
  3338. }
  3339. /// sets a temporary character array variable element idx to given value
  3340. /// @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.
  3341. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3342. {
  3343. int key;
  3344. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3345. {
  3346. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3347. return;
  3348. }
  3349. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3350. {
  3351. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3352. return;
  3353. }
  3354. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3355. if( is_string_variable(varname) )
  3356. {
  3357. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3358. }
  3359. else
  3360. {
  3361. pc_setreg(sd, reference_uid(key, idx), (int)value);
  3362. }
  3363. if( refcache )
  3364. {// save to avoid repeated add_str calls
  3365. refcache[0] = key;
  3366. }
  3367. }
  3368. /*==========================================
  3369. * 終了
  3370. *------------------------------------------*/
  3371. int do_final_script()
  3372. {
  3373. #ifdef DEBUG_HASH
  3374. if (battle_config.etc_log)
  3375. {
  3376. FILE *fp = fopen("hash_dump.txt","wt");
  3377. if(fp) {
  3378. int i,count[SCRIPT_HASH_SIZE];
  3379. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3380. int n=0;
  3381. int min=INT_MAX,max=0,zero=0;
  3382. double mean=0.0f;
  3383. double median=0.0f;
  3384. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3385. memset(count, 0, sizeof(count));
  3386. fprintf(fp,"num : hash : data_name\n");
  3387. fprintf(fp,"---------------------------------------------------------------\n");
  3388. for(i=LABEL_START; i<str_num; i++) {
  3389. unsigned int h = calc_hash(get_str(i));
  3390. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3391. ++count[h];
  3392. }
  3393. fprintf(fp,"--------------------\n\n");
  3394. memset(count2, 0, sizeof(count2));
  3395. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3396. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3397. if(min > count[i])
  3398. min = count[i]; // minimun count of collision
  3399. if(max < count[i])
  3400. max = count[i]; // maximun count of collision
  3401. if(count[i] == 0)
  3402. zero++;
  3403. ++count2[count[i]];
  3404. }
  3405. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3406. for( i=min; i <= max; ++i ){
  3407. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3408. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3409. }
  3410. for( i=min; i <= max; ++i ){
  3411. n += count2[i];
  3412. if( n*2 >= SCRIPT_HASH_SIZE )
  3413. {
  3414. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3415. median = (i+i+1)/2.0f;
  3416. else
  3417. median = i;
  3418. break;
  3419. }
  3420. }
  3421. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3422. fclose(fp);
  3423. }
  3424. }
  3425. #endif
  3426. mapreg_final();
  3427. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3428. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3429. autobonus_db->destroy(autobonus_db, do_final_autobonus_sub);
  3430. if(sleep_db) {
  3431. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3432. while(n) {
  3433. struct script_state *st = (struct script_state *)n->data;
  3434. script_free_state(st);
  3435. n = n->next;
  3436. }
  3437. linkdb_final(&sleep_db);
  3438. }
  3439. if (str_data)
  3440. aFree(str_data);
  3441. if (str_buf)
  3442. aFree(str_buf);
  3443. return 0;
  3444. }
  3445. /*==========================================
  3446. * 初期化
  3447. *------------------------------------------*/
  3448. int do_init_script()
  3449. {
  3450. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3451. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3452. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3453. mapreg_init();
  3454. return 0;
  3455. }
  3456. int script_reload()
  3457. {
  3458. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3459. scriptlabel_db->clear(scriptlabel_db, NULL);
  3460. if(sleep_db) {
  3461. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3462. while(n) {
  3463. struct script_state *st = (struct script_state *)n->data;
  3464. script_free_state(st);
  3465. n = n->next;
  3466. }
  3467. linkdb_final(&sleep_db);
  3468. }
  3469. mapreg_reload();
  3470. return 0;
  3471. }
  3472. //-----------------------------------------------------------------------------
  3473. // buildin functions
  3474. //
  3475. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3476. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3477. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3478. /////////////////////////////////////////////////////////////////////
  3479. // NPC interaction
  3480. //
  3481. /// Appends a message to the npc dialog.
  3482. /// If a dialog doesn't exist yet, one is created.
  3483. ///
  3484. /// mes "<message>";
  3485. BUILDIN_FUNC(mes)
  3486. {
  3487. TBL_PC* sd = script_rid2sd(st);
  3488. if( sd == NULL )
  3489. return 0;
  3490. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3491. return 0;
  3492. }
  3493. /// Displays the button 'next' in the npc dialog.
  3494. /// The dialog text is cleared and the script continues when the button is pressed.
  3495. ///
  3496. /// next;
  3497. BUILDIN_FUNC(next)
  3498. {
  3499. TBL_PC* sd;
  3500. sd = script_rid2sd(st);
  3501. if( sd == NULL )
  3502. return 0;
  3503. st->state = STOP;
  3504. clif_scriptnext(sd, st->oid);
  3505. return 0;
  3506. }
  3507. /// Ends the script and displays the button 'close' on the npc dialog.
  3508. /// The dialog is closed when the button is pressed.
  3509. ///
  3510. /// close;
  3511. BUILDIN_FUNC(close)
  3512. {
  3513. TBL_PC* sd;
  3514. sd = script_rid2sd(st);
  3515. if( sd == NULL )
  3516. return 0;
  3517. st->state = END;
  3518. clif_scriptclose(sd, st->oid);
  3519. return 0;
  3520. }
  3521. /// Displays the button 'close' on the npc dialog.
  3522. /// The dialog is closed and the script continues when the button is pressed.
  3523. ///
  3524. /// close2;
  3525. BUILDIN_FUNC(close2)
  3526. {
  3527. TBL_PC* sd;
  3528. sd = script_rid2sd(st);
  3529. if( sd == NULL )
  3530. return 0;
  3531. st->state = STOP;
  3532. clif_scriptclose(sd, st->oid);
  3533. return 0;
  3534. }
  3535. /// Counts the number of valid and total number of options in 'str'
  3536. /// If max_count > 0 the counting stops when that valid option is reached
  3537. /// total is incremented for each option (NULL is supported)
  3538. static int menu_countoptions(const char* str, int max_count, int* total)
  3539. {
  3540. int count = 0;
  3541. int bogus_total;
  3542. if( total == NULL )
  3543. total = &bogus_total;
  3544. ++(*total);
  3545. // initial empty options
  3546. while( *str == ':' )
  3547. {
  3548. ++str;
  3549. ++(*total);
  3550. }
  3551. // count menu options
  3552. while( *str != '\0' )
  3553. {
  3554. ++count;
  3555. --max_count;
  3556. if( max_count == 0 )
  3557. break;
  3558. while( *str != ':' && *str != '\0' )
  3559. ++str;
  3560. while( *str == ':' )
  3561. {
  3562. ++str;
  3563. ++(*total);
  3564. }
  3565. }
  3566. return count;
  3567. }
  3568. /// Displays a menu with options and goes to the target label.
  3569. /// The script is stopped if cancel is pressed.
  3570. /// Options with no text are not displayed in the client.
  3571. ///
  3572. /// Options can be grouped together, separated by the character ':' in the text:
  3573. /// ex: menu "A:B:C",L_target;
  3574. /// All these options go to the specified target label.
  3575. ///
  3576. /// The index of the selected option is put in the variable @menu.
  3577. /// Indexes start with 1 and are consistent with grouped and empty options.
  3578. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3579. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3580. ///
  3581. /// NOTE: the client closes the npc dialog when cancel is pressed
  3582. ///
  3583. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3584. BUILDIN_FUNC(menu)
  3585. {
  3586. int i;
  3587. const char* text;
  3588. TBL_PC* sd;
  3589. sd = script_rid2sd(st);
  3590. if( sd == NULL )
  3591. return 0;
  3592. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3593. if( sd->state.menu_or_input == 0 )
  3594. {
  3595. struct StringBuf buf;
  3596. struct script_data* data;
  3597. if( script_lastdata(st) % 2 == 0 )
  3598. {// argument count is not even (1st argument is at index 2)
  3599. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3600. st->state = END;
  3601. return 1;
  3602. }
  3603. StringBuf_Init(&buf);
  3604. sd->npc_menu = 0;
  3605. for( i = 2; i < script_lastdata(st); i += 2 )
  3606. {
  3607. // menu options
  3608. text = script_getstr(st, i);
  3609. // target label
  3610. data = script_getdata(st, i+1);
  3611. if( !data_islabel(data) )
  3612. {// not a label
  3613. StringBuf_Destroy(&buf);
  3614. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3615. script_reportdata(data);
  3616. st->state = END;
  3617. return 1;
  3618. }
  3619. // append option(s)
  3620. if( text[0] == '\0' )
  3621. continue;// empty string, ignore
  3622. if( sd->npc_menu > 0 )
  3623. StringBuf_AppendStr(&buf, ":");
  3624. StringBuf_AppendStr(&buf, text);
  3625. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3626. }
  3627. st->state = RERUNLINE;
  3628. sd->state.menu_or_input = 1;
  3629. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3630. StringBuf_Destroy(&buf);
  3631. if( sd->npc_menu >= 0xff )
  3632. {// 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
  3633. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3634. script_reportsrc(st);
  3635. }
  3636. }
  3637. else if( sd->npc_menu == 0xff )
  3638. {// Cancel was pressed
  3639. sd->state.menu_or_input = 0;
  3640. st->state = END;
  3641. }
  3642. else
  3643. {// goto target label
  3644. int menu = 0;
  3645. sd->state.menu_or_input = 0;
  3646. if( sd->npc_menu <= 0 )
  3647. {
  3648. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3649. st->state = END;
  3650. return 1;
  3651. }
  3652. // get target label
  3653. for( i = 2; i < script_lastdata(st); i += 2 )
  3654. {
  3655. text = script_getstr(st, i);
  3656. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3657. if( sd->npc_menu <= 0 )
  3658. break;// entry found
  3659. }
  3660. if( sd->npc_menu > 0 )
  3661. {// Invalid selection
  3662. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3663. st->state = END;
  3664. return 1;
  3665. }
  3666. if( !data_islabel(script_getdata(st, i + 1)) )
  3667. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3668. ShowError("script:menu: unexpected data in label argument\n");
  3669. script_reportdata(script_getdata(st, i + 1));
  3670. st->state = END;
  3671. return 1;
  3672. }
  3673. pc_setreg(sd, add_str("@menu"), menu);
  3674. st->pos = script_getnum(st, i + 1);
  3675. st->state = GOTO;
  3676. }
  3677. return 0;
  3678. }
  3679. /// Displays a menu with options and returns the selected option.
  3680. /// Behaves like 'menu' without the target labels.
  3681. ///
  3682. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3683. ///
  3684. /// @see menu
  3685. BUILDIN_FUNC(select)
  3686. {
  3687. int i;
  3688. const char* text;
  3689. TBL_PC* sd;
  3690. sd = script_rid2sd(st);
  3691. if( sd == NULL )
  3692. return 0;
  3693. if( sd->state.menu_or_input == 0 )
  3694. {
  3695. struct StringBuf buf;
  3696. StringBuf_Init(&buf);
  3697. sd->npc_menu = 0;
  3698. for( i = 2; i <= script_lastdata(st); ++i )
  3699. {
  3700. text = script_getstr(st, i);
  3701. if( sd->npc_menu > 0 )
  3702. StringBuf_AppendStr(&buf, ":");
  3703. StringBuf_AppendStr(&buf, text);
  3704. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3705. }
  3706. st->state = RERUNLINE;
  3707. sd->state.menu_or_input = 1;
  3708. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3709. StringBuf_Destroy(&buf);
  3710. if( sd->npc_menu >= 0xff )
  3711. {
  3712. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3713. script_reportsrc(st);
  3714. }
  3715. }
  3716. else if( sd->npc_menu == 0xff )
  3717. {// Cancel was pressed
  3718. sd->state.menu_or_input = 0;
  3719. st->state = END;
  3720. }
  3721. else
  3722. {// return selected option
  3723. int menu = 0;
  3724. sd->state.menu_or_input = 0;
  3725. for( i = 2; i <= script_lastdata(st); ++i )
  3726. {
  3727. text = script_getstr(st, i);
  3728. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3729. if( sd->npc_menu <= 0 )
  3730. break;// entry found
  3731. }
  3732. pc_setreg(sd, add_str("@menu"), menu);
  3733. script_pushint(st, menu);
  3734. st->state = RUN;
  3735. }
  3736. return 0;
  3737. }
  3738. /// Displays a menu with options and returns the selected option.
  3739. /// Behaves like 'menu' without the target labels, except when cancel is
  3740. /// pressed.
  3741. /// When cancel is pressed, the script continues and 255 is returned.
  3742. ///
  3743. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3744. ///
  3745. /// @see menu
  3746. BUILDIN_FUNC(prompt)
  3747. {
  3748. int i;
  3749. const char *text;
  3750. TBL_PC* sd;
  3751. sd = script_rid2sd(st);
  3752. if( sd == NULL )
  3753. return 0;
  3754. if( sd->state.menu_or_input == 0 )
  3755. {
  3756. struct StringBuf buf;
  3757. StringBuf_Init(&buf);
  3758. sd->npc_menu = 0;
  3759. for( i = 2; i <= script_lastdata(st); ++i )
  3760. {
  3761. text = script_getstr(st, i);
  3762. if( sd->npc_menu > 0 )
  3763. StringBuf_AppendStr(&buf, ":");
  3764. StringBuf_AppendStr(&buf, text);
  3765. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3766. }
  3767. st->state = RERUNLINE;
  3768. sd->state.menu_or_input = 1;
  3769. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3770. StringBuf_Destroy(&buf);
  3771. if( sd->npc_menu >= 0xff )
  3772. {
  3773. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  3774. script_reportsrc(st);
  3775. }
  3776. }
  3777. else if( sd->npc_menu == 0xff )
  3778. {// Cancel was pressed
  3779. sd->state.menu_or_input = 0;
  3780. pc_setreg(sd, add_str("@menu"), 0xff);
  3781. script_pushint(st, 0xff);
  3782. st->state = RUN;
  3783. }
  3784. else
  3785. {// return selected option
  3786. int menu = 0;
  3787. sd->state.menu_or_input = 0;
  3788. for( i = 2; i <= script_lastdata(st); ++i )
  3789. {
  3790. text = script_getstr(st, i);
  3791. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3792. if( sd->npc_menu <= 0 )
  3793. break;// entry found
  3794. }
  3795. pc_setreg(sd, add_str("@menu"), menu);
  3796. script_pushint(st, menu);
  3797. st->state = RUN;
  3798. }
  3799. return 0;
  3800. }
  3801. /////////////////////////////////////////////////////////////////////
  3802. // ...
  3803. //
  3804. /// Jumps to the target script label.
  3805. ///
  3806. /// goto <label>;
  3807. BUILDIN_FUNC(goto)
  3808. {
  3809. if( !data_islabel(script_getdata(st,2)) )
  3810. {
  3811. ShowError("script:goto: not a label\n");
  3812. script_reportdata(script_getdata(st,2));
  3813. st->state = END;
  3814. return 1;
  3815. }
  3816. st->pos = script_getnum(st,2);
  3817. st->state = GOTO;
  3818. return 0;
  3819. }
  3820. /*==========================================
  3821. * user-defined function call
  3822. *------------------------------------------*/
  3823. BUILDIN_FUNC(callfunc)
  3824. {
  3825. int i, j;
  3826. struct script_retinfo* ri;
  3827. struct script_code* scr;
  3828. const char* str = script_getstr(st,2);
  3829. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3830. if( !scr )
  3831. {
  3832. ShowError("script:callfunc: function not found! [%s]\n", str);
  3833. st->state = END;
  3834. return 1;
  3835. }
  3836. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3837. {
  3838. struct script_data* data = push_copy(st->stack,i);
  3839. if( data_isreference(data) && !data->ref )
  3840. {
  3841. const char* name = reference_getname(data);
  3842. if( name[0] == '.' && name[1] == '@' )
  3843. data->ref = st->stack->var_function;
  3844. else if( name[0] == '.' )
  3845. data->ref = &st->script->script_vars;
  3846. }
  3847. }
  3848. CREATE(ri, struct script_retinfo, 1);
  3849. ri->script = st->script;// script code
  3850. ri->var_function = st->stack->var_function;// scope variables
  3851. ri->pos = st->pos;// script location
  3852. ri->nargs = j;// argument count
  3853. ri->defsp = st->stack->defsp;// default stack pointer
  3854. push_retinfo(st->stack, ri);
  3855. st->pos = 0;
  3856. st->script = scr;
  3857. st->stack->defsp = st->stack->sp;
  3858. st->state = GOTO;
  3859. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3860. return 0;
  3861. }
  3862. /*==========================================
  3863. * subroutine call
  3864. *------------------------------------------*/
  3865. BUILDIN_FUNC(callsub)
  3866. {
  3867. int i,j;
  3868. struct script_retinfo* ri;
  3869. int pos = script_getnum(st,2);
  3870. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3871. {
  3872. ShowError("script:callsub: argument is not a label\n");
  3873. script_reportdata(script_getdata(st,2));
  3874. st->state = END;
  3875. return 1;
  3876. }
  3877. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3878. {
  3879. struct script_data* data = push_copy(st->stack,i);
  3880. if( data_isreference(data) && !data->ref )
  3881. {
  3882. const char* name = reference_getname(data);
  3883. if( name[0] == '.' && name[1] == '@' )
  3884. data->ref = st->stack->var_function;
  3885. }
  3886. }
  3887. CREATE(ri, struct script_retinfo, 1);
  3888. ri->script = st->script;// script code
  3889. ri->var_function = st->stack->var_function;// scope variables
  3890. ri->pos = st->pos;// script location
  3891. ri->nargs = j;// argument count
  3892. ri->defsp = st->stack->defsp;// default stack pointer
  3893. push_retinfo(st->stack, ri);
  3894. st->pos = pos;
  3895. st->stack->defsp = st->stack->sp;
  3896. st->state = GOTO;
  3897. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3898. return 0;
  3899. }
  3900. /// Retrieves an argument provided to callfunc/callsub.
  3901. /// If the argument doesn't exist
  3902. ///
  3903. /// getarg(<index>{,<default_value>}) -> <value>
  3904. BUILDIN_FUNC(getarg)
  3905. {
  3906. struct script_retinfo* ri;
  3907. int idx;
  3908. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3909. {
  3910. ShowError("script:getarg: no callfunc or callsub!\n");
  3911. st->state = END;
  3912. return 1;
  3913. }
  3914. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3915. idx = script_getnum(st,2);
  3916. if( idx >= 0 && idx < ri->nargs )
  3917. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3918. else if( script_hasdata(st,3) )
  3919. script_pushcopy(st, 3);
  3920. else
  3921. {
  3922. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3923. st->state = END;
  3924. return 1;
  3925. }
  3926. return 0;
  3927. }
  3928. /// Returns from the current function, optionaly returning a value from the functions.
  3929. /// Don't use outside script functions.
  3930. ///
  3931. /// return;
  3932. /// return <value>;
  3933. BUILDIN_FUNC(return)
  3934. {
  3935. if( script_hasdata(st,2) )
  3936. {// return value
  3937. struct script_data* data;
  3938. script_pushcopy(st, 2);
  3939. data = script_getdatatop(st, -1);
  3940. if( data_isreference(data) )
  3941. {
  3942. const char* name = reference_getname(data);
  3943. if( name[0] == '.' && name[1] == '@' )
  3944. {// scope variable
  3945. if( !data->ref || data->ref == st->stack->var_function )
  3946. get_val(st, data);// current scope, convert to value
  3947. }
  3948. else if( name[0] == '.' && !data->ref )
  3949. {// script variable, link to current script
  3950. data->ref = &st->script->script_vars;
  3951. }
  3952. }
  3953. }
  3954. else
  3955. {// no return value
  3956. script_pushnil(st);
  3957. }
  3958. st->state = RETFUNC;
  3959. return 0;
  3960. }
  3961. /// Returns a random number from 0 to <range>-1.
  3962. /// Or returns a random number from <min> to <max>.
  3963. /// If <min> is greater than <max>, their numbers are switched.
  3964. /// rand(<range>) -> <int>
  3965. /// rand(<min>,<max>) -> <int>
  3966. BUILDIN_FUNC(rand)
  3967. {
  3968. int range;
  3969. int min;
  3970. int max;
  3971. if( script_hasdata(st,3) )
  3972. {// min,max
  3973. min = script_getnum(st,2);
  3974. max = script_getnum(st,3);
  3975. if( max < min )
  3976. swap(min, max);
  3977. range = max - min + 1;
  3978. }
  3979. else
  3980. {// range
  3981. min = 0;
  3982. range = script_getnum(st,2);
  3983. }
  3984. if( range <= 1 )
  3985. script_pushint(st, min);
  3986. else
  3987. script_pushint(st, rand()%range + min);
  3988. return 0;
  3989. }
  3990. /*==========================================
  3991. *
  3992. *------------------------------------------*/
  3993. BUILDIN_FUNC(warp)
  3994. {
  3995. int ret;
  3996. int x,y;
  3997. const char* str;
  3998. TBL_PC* sd;
  3999. sd = script_rid2sd(st);
  4000. if( sd == NULL )
  4001. return 0;
  4002. str = script_getstr(st,2);
  4003. x = script_getnum(st,3);
  4004. y = script_getnum(st,4);
  4005. if(strcmp(str,"Random")==0)
  4006. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4007. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4008. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4009. else
  4010. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4011. if( ret ) {
  4012. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4013. script_reportsrc(st);
  4014. }
  4015. return 0;
  4016. }
  4017. /*==========================================
  4018. * エリア指定ワープ
  4019. *------------------------------------------*/
  4020. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4021. {
  4022. int x,y;
  4023. unsigned int map;
  4024. map=va_arg(ap, unsigned int);
  4025. x=va_arg(ap,int);
  4026. y=va_arg(ap,int);
  4027. if(map == 0)
  4028. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4029. else
  4030. pc_setpos((TBL_PC *)bl,map,x,y,CLR_OUTSIGHT);
  4031. return 0;
  4032. }
  4033. BUILDIN_FUNC(areawarp)
  4034. {
  4035. int x,y,m;
  4036. unsigned int index;
  4037. const char *str;
  4038. const char *mapname;
  4039. int x0,y0,x1,y1;
  4040. mapname=script_getstr(st,2);
  4041. x0=script_getnum(st,3);
  4042. y0=script_getnum(st,4);
  4043. x1=script_getnum(st,5);
  4044. y1=script_getnum(st,6);
  4045. str=script_getstr(st,7);
  4046. x=script_getnum(st,8);
  4047. y=script_getnum(st,9);
  4048. if( (m=map_mapname2mapid(mapname))< 0)
  4049. return 0;
  4050. if(strcmp(str,"Random")==0)
  4051. index = 0;
  4052. else if(!(index=mapindex_name2id(str)))
  4053. return 0;
  4054. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  4055. return 0;
  4056. }
  4057. /*==========================================
  4058. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4059. *------------------------------------------*/
  4060. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4061. {
  4062. int hp, sp;
  4063. hp = va_arg(ap, int);
  4064. sp = va_arg(ap, int);
  4065. pc_percentheal((TBL_PC *)bl,hp,sp);
  4066. return 0;
  4067. }
  4068. BUILDIN_FUNC(areapercentheal)
  4069. {
  4070. int hp,sp,m;
  4071. const char *mapname;
  4072. int x0,y0,x1,y1;
  4073. mapname=script_getstr(st,2);
  4074. x0=script_getnum(st,3);
  4075. y0=script_getnum(st,4);
  4076. x1=script_getnum(st,5);
  4077. y1=script_getnum(st,6);
  4078. hp=script_getnum(st,7);
  4079. sp=script_getnum(st,8);
  4080. if( (m=map_mapname2mapid(mapname))< 0)
  4081. return 0;
  4082. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4083. return 0;
  4084. }
  4085. /*==========================================
  4086. * warpchar [LuzZza]
  4087. * Useful for warp one player from
  4088. * another player npc-session.
  4089. * Using: warpchar "mapname",x,y,Char_ID;
  4090. *------------------------------------------*/
  4091. BUILDIN_FUNC(warpchar)
  4092. {
  4093. int x,y,a;
  4094. const char *str;
  4095. TBL_PC *sd;
  4096. str=script_getstr(st,2);
  4097. x=script_getnum(st,3);
  4098. y=script_getnum(st,4);
  4099. a=script_getnum(st,5);
  4100. sd = map_charid2sd(a);
  4101. if( sd == NULL )
  4102. return 0;
  4103. if(strcmp(str, "Random") == 0)
  4104. pc_randomwarp(sd, CLR_TELEPORT);
  4105. else
  4106. if(strcmp(str, "SavePoint") == 0)
  4107. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4108. else
  4109. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4110. return 0;
  4111. }
  4112. /*==========================================
  4113. * Warpparty - [Fredzilla] [Paradox924X]
  4114. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4115. * If 'from_mapname' is specified, only the party members on that map will be warped
  4116. *------------------------------------------*/
  4117. BUILDIN_FUNC(warpparty)
  4118. {
  4119. TBL_PC *sd = NULL;
  4120. TBL_PC *pl_sd;
  4121. struct party_data* p;
  4122. int type;
  4123. int mapindex;
  4124. int i;
  4125. const char* str = script_getstr(st,2);
  4126. int x = script_getnum(st,3);
  4127. int y = script_getnum(st,4);
  4128. int p_id = script_getnum(st,5);
  4129. const char* str2 = NULL;
  4130. if ( script_hasdata(st,6) )
  4131. str2 = script_getstr(st,6);
  4132. p = party_search(p_id);
  4133. if(!p)
  4134. return 0;
  4135. type = ( strcmp(str,"Random")==0 ) ? 0
  4136. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4137. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4138. : ( strcmp(str,"Leader")==0 ) ? 3
  4139. : 4;
  4140. switch (type)
  4141. {
  4142. case 3:
  4143. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4144. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4145. return 0;
  4146. pl_sd = p->data[i].sd;
  4147. mapindex = pl_sd->mapindex;
  4148. x = pl_sd->bl.x;
  4149. y = pl_sd->bl.y;
  4150. break;
  4151. case 4:
  4152. mapindex = mapindex_name2id(str);
  4153. break;
  4154. case 2:
  4155. //"SavePoint" uses save point of the currently attached player
  4156. if (( sd = script_rid2sd(st) ) == NULL )
  4157. return 0;
  4158. default:
  4159. mapindex = 0;
  4160. break;
  4161. }
  4162. for (i = 0; i < MAX_PARTY; i++)
  4163. {
  4164. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4165. continue;
  4166. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4167. continue;
  4168. if( pc_isdead(pl_sd) )
  4169. continue;
  4170. switch( type )
  4171. {
  4172. case 0: // Random
  4173. if(!map[pl_sd->bl.m].flag.nowarp)
  4174. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4175. break;
  4176. case 1: // SavePointAll
  4177. if(!map[pl_sd->bl.m].flag.noreturn)
  4178. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4179. break;
  4180. case 2: // SavePoint
  4181. if(!map[pl_sd->bl.m].flag.noreturn)
  4182. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4183. break;
  4184. case 3: // Leader
  4185. case 4: // m,x,y
  4186. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4187. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4188. break;
  4189. }
  4190. }
  4191. return 0;
  4192. }
  4193. /*==========================================
  4194. * Warpguild - [Fredzilla]
  4195. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4196. *------------------------------------------*/
  4197. BUILDIN_FUNC(warpguild)
  4198. {
  4199. TBL_PC *sd = NULL;
  4200. TBL_PC *pl_sd;
  4201. struct guild* g;
  4202. struct s_mapiterator* iter;
  4203. int type;
  4204. const char* str = script_getstr(st,2);
  4205. int x = script_getnum(st,3);
  4206. int y = script_getnum(st,4);
  4207. int gid = script_getnum(st,5);
  4208. g = guild_search(gid);
  4209. if( g == NULL )
  4210. return 0;
  4211. type = ( strcmp(str,"Random")==0 ) ? 0
  4212. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4213. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4214. : 3;
  4215. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4216. {// "SavePoint" uses save point of the currently attached player
  4217. return 0;
  4218. }
  4219. iter = mapit_getallusers();
  4220. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4221. {
  4222. if( pl_sd->status.guild_id != gid )
  4223. continue;
  4224. switch( type )
  4225. {
  4226. case 0: // Random
  4227. if(!map[pl_sd->bl.m].flag.nowarp)
  4228. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4229. break;
  4230. case 1: // SavePointAll
  4231. if(!map[pl_sd->bl.m].flag.noreturn)
  4232. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4233. break;
  4234. case 2: // SavePoint
  4235. if(!map[pl_sd->bl.m].flag.noreturn)
  4236. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4237. break;
  4238. case 3: // m,x,y
  4239. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4240. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4241. break;
  4242. }
  4243. }
  4244. mapit_free(iter);
  4245. return 0;
  4246. }
  4247. /*==========================================
  4248. *
  4249. *------------------------------------------*/
  4250. BUILDIN_FUNC(heal)
  4251. {
  4252. TBL_PC *sd;
  4253. int hp,sp;
  4254. sd = script_rid2sd(st);
  4255. if (!sd) return 0;
  4256. hp=script_getnum(st,2);
  4257. sp=script_getnum(st,3);
  4258. status_heal(&sd->bl, hp, sp, 1);
  4259. return 0;
  4260. }
  4261. /*==========================================
  4262. *
  4263. *------------------------------------------*/
  4264. BUILDIN_FUNC(itemheal)
  4265. {
  4266. TBL_PC *sd;
  4267. int hp,sp;
  4268. hp=script_getnum(st,2);
  4269. sp=script_getnum(st,3);
  4270. if(potion_flag==1) {
  4271. potion_hp = hp;
  4272. potion_sp = sp;
  4273. return 0;
  4274. }
  4275. sd = script_rid2sd(st);
  4276. if (!sd) return 0;
  4277. pc_itemheal(sd,sd->itemid,hp,sp);
  4278. return 0;
  4279. }
  4280. /*==========================================
  4281. *
  4282. *------------------------------------------*/
  4283. BUILDIN_FUNC(percentheal)
  4284. {
  4285. int hp,sp;
  4286. TBL_PC* sd;
  4287. hp=script_getnum(st,2);
  4288. sp=script_getnum(st,3);
  4289. if(potion_flag==1) {
  4290. potion_per_hp = hp;
  4291. potion_per_sp = sp;
  4292. return 0;
  4293. }
  4294. sd = script_rid2sd(st);
  4295. if( sd == NULL )
  4296. return 0;
  4297. pc_percentheal(sd,hp,sp);
  4298. return 0;
  4299. }
  4300. /*==========================================
  4301. *
  4302. *------------------------------------------*/
  4303. BUILDIN_FUNC(jobchange)
  4304. {
  4305. int job, upper=-1;
  4306. job=script_getnum(st,2);
  4307. if( script_hasdata(st,3) )
  4308. upper=script_getnum(st,3);
  4309. if (pcdb_checkid(job))
  4310. {
  4311. TBL_PC* sd;
  4312. sd = script_rid2sd(st);
  4313. if( sd == NULL )
  4314. return 0;
  4315. pc_jobchange(sd, job, upper);
  4316. }
  4317. return 0;
  4318. }
  4319. /*==========================================
  4320. *
  4321. *------------------------------------------*/
  4322. BUILDIN_FUNC(jobname)
  4323. {
  4324. int class_=script_getnum(st,2);
  4325. script_pushconststr(st, (char*)job_name(class_));
  4326. return 0;
  4327. }
  4328. /// Get input from the player.
  4329. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4330. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4331. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4332. /// shorter than 'min' and 0 otherwise.
  4333. ///
  4334. /// input(<var>{,<min>{,<max>}}) -> <int>
  4335. BUILDIN_FUNC(input)
  4336. {
  4337. TBL_PC* sd;
  4338. struct script_data* data;
  4339. int uid;
  4340. const char* name;
  4341. int min;
  4342. int max;
  4343. sd = script_rid2sd(st);
  4344. if( sd == NULL )
  4345. return 0;
  4346. data = script_getdata(st,2);
  4347. if( !data_isreference(data) ){
  4348. ShowError("script:input: not a variable\n");
  4349. script_reportdata(data);
  4350. st->state = END;
  4351. return 1;
  4352. }
  4353. uid = reference_getuid(data);
  4354. name = reference_getname(data);
  4355. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4356. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4357. if( !sd->state.menu_or_input )
  4358. { // first invocation, display npc input box
  4359. sd->state.menu_or_input = 1;
  4360. st->state = RERUNLINE;
  4361. if( is_string_variable(name) )
  4362. clif_scriptinputstr(sd,st->oid);
  4363. else
  4364. clif_scriptinput(sd,st->oid);
  4365. }
  4366. else
  4367. { // take received text/value and store it in the designated variable
  4368. sd->state.menu_or_input = 0;
  4369. if( is_string_variable(name) )
  4370. {
  4371. int len = (int)strlen(sd->npc_str);
  4372. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4373. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4374. }
  4375. else
  4376. {
  4377. int amount = sd->npc_amount;
  4378. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4379. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4380. }
  4381. st->state = RUN;
  4382. }
  4383. return 0;
  4384. }
  4385. /// Sets the value of a variable.
  4386. /// The value is converted to the type of the variable.
  4387. ///
  4388. /// set(<variable>,<value>) -> <variable>
  4389. BUILDIN_FUNC(set)
  4390. {
  4391. TBL_PC* sd = NULL;
  4392. struct script_data* data;
  4393. int num;
  4394. const char* name;
  4395. char prefix;
  4396. data = script_getdata(st,2);
  4397. if( !data_isreference(data) )
  4398. {
  4399. ShowError("script:set: not a variable\n");
  4400. script_reportdata(script_getdata(st,2));
  4401. st->state = END;
  4402. return 1;
  4403. }
  4404. num = reference_getuid(data);
  4405. name = reference_getname(data);
  4406. prefix = *name;
  4407. if( not_server_variable(prefix) )
  4408. {
  4409. sd = script_rid2sd(st);
  4410. if( sd == NULL )
  4411. {
  4412. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4413. return 0;
  4414. }
  4415. }
  4416. if( is_string_variable(name) )
  4417. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4418. else
  4419. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4420. // return a copy of the variable reference
  4421. script_pushcopy(st,2);
  4422. return 0;
  4423. }
  4424. /////////////////////////////////////////////////////////////////////
  4425. /// Array variables
  4426. ///
  4427. /// Returns the size of the specified array
  4428. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4429. {
  4430. int32 ret = idx;
  4431. if( isstring )
  4432. {
  4433. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4434. {
  4435. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4436. if( str && *str )
  4437. ret = idx + 1;
  4438. script_removetop(st, -1, 0);
  4439. }
  4440. }
  4441. else
  4442. {
  4443. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4444. {
  4445. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4446. if( num )
  4447. ret = idx + 1;
  4448. script_removetop(st, -1, 0);
  4449. }
  4450. }
  4451. return ret;
  4452. }
  4453. /// Sets values of an array, from the starting index.
  4454. /// ex: setarray arr[1],1,2,3;
  4455. ///
  4456. /// setarray <array variable>,<value1>{,<value2>...};
  4457. BUILDIN_FUNC(setarray)
  4458. {
  4459. struct script_data* data;
  4460. const char* name;
  4461. int32 start;
  4462. int32 end;
  4463. int32 id;
  4464. int32 i;
  4465. TBL_PC* sd = NULL;
  4466. data = script_getdata(st, 2);
  4467. if( !data_isreference(data) )
  4468. {
  4469. ShowError("script:setarray: not a variable\n");
  4470. script_reportdata(data);
  4471. st->state = END;
  4472. return 1;// not a variable
  4473. }
  4474. id = reference_getid(data);
  4475. start = reference_getindex(data);
  4476. name = reference_getname(data);
  4477. if( not_array_variable(*name) )
  4478. {
  4479. ShowError("script:setarray: illegal scope\n");
  4480. script_reportdata(data);
  4481. st->state = END;
  4482. return 1;// not supported
  4483. }
  4484. if( not_server_variable(*name) )
  4485. {
  4486. sd = script_rid2sd(st);
  4487. if( sd == NULL )
  4488. return 0;// no player attached
  4489. }
  4490. end = start + script_lastdata(st) - 2;
  4491. if( end > SCRIPT_MAX_ARRAYSIZE )
  4492. end = SCRIPT_MAX_ARRAYSIZE;
  4493. if( is_string_variable(name) )
  4494. {// string array
  4495. for( i = 3; start < end; ++start, ++i )
  4496. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4497. }
  4498. else
  4499. {// int array
  4500. for( i = 3; start < end; ++start, ++i )
  4501. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4502. }
  4503. return 0;
  4504. }
  4505. /// Sets count values of an array, from the starting index.
  4506. /// ex: cleararray arr[0],0,1;
  4507. ///
  4508. /// cleararray <array variable>,<value>,<count>;
  4509. BUILDIN_FUNC(cleararray)
  4510. {
  4511. struct script_data* data;
  4512. const char* name;
  4513. int32 start;
  4514. int32 end;
  4515. int32 id;
  4516. void* v;
  4517. TBL_PC* sd = NULL;
  4518. data = script_getdata(st, 2);
  4519. if( !data_isreference(data) )
  4520. {
  4521. ShowError("script:cleararray: not a variable\n");
  4522. script_reportdata(data);
  4523. st->state = END;
  4524. return 1;// not a variable
  4525. }
  4526. id = reference_getid(data);
  4527. start = reference_getindex(data);
  4528. name = reference_getname(data);
  4529. if( not_array_variable(*name) )
  4530. {
  4531. ShowError("script:cleararray: illegal scope\n");
  4532. script_reportdata(data);
  4533. st->state = END;
  4534. return 1;// not supported
  4535. }
  4536. if( not_server_variable(*name) )
  4537. {
  4538. sd = script_rid2sd(st);
  4539. if( sd == NULL )
  4540. return 0;// no player attached
  4541. }
  4542. if( is_string_variable(name) )
  4543. v = (void*)script_getstr(st, 3);
  4544. else
  4545. v = (void*)script_getnum(st, 3);
  4546. end = start + script_getnum(st, 4);
  4547. if( end > SCRIPT_MAX_ARRAYSIZE )
  4548. end = SCRIPT_MAX_ARRAYSIZE;
  4549. for( ; start < end; ++start )
  4550. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4551. return 0;
  4552. }
  4553. /// Copies data from one array to another.
  4554. /// ex: copyarray arr[0],arr[2],2;
  4555. ///
  4556. /// copyarray <destination array variable>,<source array variable>,<count>;
  4557. BUILDIN_FUNC(copyarray)
  4558. {
  4559. struct script_data* data1;
  4560. struct script_data* data2;
  4561. const char* name1;
  4562. const char* name2;
  4563. int32 idx1;
  4564. int32 idx2;
  4565. int32 id1;
  4566. int32 id2;
  4567. void* v;
  4568. int32 i;
  4569. int32 count;
  4570. TBL_PC* sd = NULL;
  4571. data1 = script_getdata(st, 2);
  4572. data2 = script_getdata(st, 3);
  4573. if( !data_isreference(data1) || !data_isreference(data2) )
  4574. {
  4575. ShowError("script:copyarray: not a variable\n");
  4576. script_reportdata(data1);
  4577. script_reportdata(data2);
  4578. st->state = END;
  4579. return 1;// not a variable
  4580. }
  4581. id1 = reference_getid(data1);
  4582. id2 = reference_getid(data2);
  4583. idx1 = reference_getindex(data1);
  4584. idx2 = reference_getindex(data2);
  4585. name1 = reference_getname(data1);
  4586. name2 = reference_getname(data2);
  4587. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4588. {
  4589. ShowError("script:copyarray: illegal scope\n");
  4590. script_reportdata(data1);
  4591. script_reportdata(data2);
  4592. st->state = END;
  4593. return 1;// not supported
  4594. }
  4595. if( is_string_variable(name1) != is_string_variable(name2) )
  4596. {
  4597. ShowError("script:copyarray: type mismatch\n");
  4598. script_reportdata(data1);
  4599. script_reportdata(data2);
  4600. st->state = END;
  4601. return 1;// data type mismatch
  4602. }
  4603. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4604. {
  4605. sd = script_rid2sd(st);
  4606. if( sd == NULL )
  4607. return 0;// no player attached
  4608. }
  4609. count = script_getnum(st, 4);
  4610. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  4611. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  4612. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4613. return 0;// nothing to copy
  4614. if( id1 == id2 && idx1 > idx2 )
  4615. {// destination might be overlapping the source - copy in reverse order
  4616. for( i = count - 1; i >= 0; --i )
  4617. {
  4618. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4619. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4620. script_removetop(st, -1, 0);
  4621. }
  4622. }
  4623. else
  4624. {// normal copy
  4625. for( i = 0; i < count; ++i )
  4626. {
  4627. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  4628. {
  4629. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4630. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4631. script_removetop(st, -1, 0);
  4632. }
  4633. else// out of range - assume ""/0
  4634. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4635. }
  4636. }
  4637. return 0;
  4638. }
  4639. /// Returns the size of the array.
  4640. /// Assumes that everything before the starting index exists.
  4641. /// ex: getarraysize(arr[3])
  4642. ///
  4643. /// getarraysize(<array variable>) -> <int>
  4644. BUILDIN_FUNC(getarraysize)
  4645. {
  4646. struct script_data* data;
  4647. const char* name;
  4648. data = script_getdata(st, 2);
  4649. if( !data_isreference(data) )
  4650. {
  4651. ShowError("script:getarraysize: not a variable\n");
  4652. script_reportdata(data);
  4653. script_pushnil(st);
  4654. st->state = END;
  4655. return 1;// not a variable
  4656. }
  4657. name = reference_getname(data);
  4658. if( not_array_variable(*name) )
  4659. {
  4660. ShowError("script:getarraysize: illegal scope\n");
  4661. script_reportdata(data);
  4662. script_pushnil(st);
  4663. st->state = END;
  4664. return 1;// not supported
  4665. }
  4666. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4667. return 0;
  4668. }
  4669. /// Deletes count or all the elements in an array, from the starting index.
  4670. /// ex: deletearray arr[4],2;
  4671. ///
  4672. /// deletearray <array variable>;
  4673. /// deletearray <array variable>,<count>;
  4674. BUILDIN_FUNC(deletearray)
  4675. {
  4676. struct script_data* data;
  4677. const char* name;
  4678. int start;
  4679. int end;
  4680. int id;
  4681. TBL_PC *sd = NULL;
  4682. data = script_getdata(st, 2);
  4683. if( !data_isreference(data) )
  4684. {
  4685. ShowError("script:deletearray: not a variable\n");
  4686. script_reportdata(data);
  4687. st->state = END;
  4688. return 1;// not a variable
  4689. }
  4690. id = reference_getid(data);
  4691. start = reference_getindex(data);
  4692. name = reference_getname(data);
  4693. if( not_array_variable(*name) )
  4694. {
  4695. ShowError("script:deletearray: illegal scope\n");
  4696. script_reportdata(data);
  4697. st->state = END;
  4698. return 1;// not supported
  4699. }
  4700. if( not_server_variable(*name) )
  4701. {
  4702. sd = script_rid2sd(st);
  4703. if( sd == NULL )
  4704. return 0;// no player attached
  4705. }
  4706. end = SCRIPT_MAX_ARRAYSIZE;
  4707. if( start >= end )
  4708. return 0;// nothing to free
  4709. if( script_hasdata(st,3) )
  4710. {
  4711. int count = script_getnum(st, 3);
  4712. if( count > end - start )
  4713. count = end - start;
  4714. if( count <= 0 )
  4715. return 0;// nothing to free
  4716. // move rest of the elements backward
  4717. for( ; start + count < end; ++start )
  4718. {
  4719. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4720. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4721. script_removetop(st, -1, 0);
  4722. }
  4723. }
  4724. // clear the rest of the array
  4725. if( is_string_variable(name) )
  4726. {
  4727. for( ; start < end; ++start )
  4728. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4729. }
  4730. else
  4731. {
  4732. for( ; start < end; ++start )
  4733. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4734. }
  4735. return 0;
  4736. }
  4737. /// Returns a reference to the target index of the array variable.
  4738. /// Equivalent to var[index].
  4739. ///
  4740. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4741. BUILDIN_FUNC(getelementofarray)
  4742. {
  4743. struct script_data* data;
  4744. const char* name;
  4745. int32 id;
  4746. int i;
  4747. data = script_getdata(st, 2);
  4748. if( !data_isreference(data) )
  4749. {
  4750. ShowError("script:getelementofarray: not a variable\n");
  4751. script_reportdata(data);
  4752. script_pushnil(st);
  4753. st->state = END;
  4754. return 1;// not a variable
  4755. }
  4756. id = reference_getid(data);
  4757. name = reference_getname(data);
  4758. if( not_array_variable(*name) )
  4759. {
  4760. ShowError("script:getelementofarray: illegal scope\n");
  4761. script_reportdata(data);
  4762. script_pushnil(st);
  4763. st->state = END;
  4764. return 1;// not supported
  4765. }
  4766. i = script_getnum(st, 3);
  4767. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  4768. {
  4769. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4770. script_reportdata(data);
  4771. script_pushnil(st);
  4772. st->state = END;
  4773. return 1;// out of range
  4774. }
  4775. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4776. return 0;
  4777. }
  4778. /////////////////////////////////////////////////////////////////////
  4779. /// ...
  4780. ///
  4781. /*==========================================
  4782. *
  4783. *------------------------------------------*/
  4784. BUILDIN_FUNC(setlook)
  4785. {
  4786. int type,val;
  4787. TBL_PC* sd;
  4788. type=script_getnum(st,2);
  4789. val=script_getnum(st,3);
  4790. sd = script_rid2sd(st);
  4791. if( sd == NULL )
  4792. return 0;
  4793. pc_changelook(sd,type,val);
  4794. return 0;
  4795. }
  4796. BUILDIN_FUNC(changelook)
  4797. { // As setlook but only client side
  4798. int type,val;
  4799. TBL_PC* sd;
  4800. type=script_getnum(st,2);
  4801. val=script_getnum(st,3);
  4802. sd = script_rid2sd(st);
  4803. if( sd == NULL )
  4804. return 0;
  4805. clif_changelook(&sd->bl,type,val);
  4806. return 0;
  4807. }
  4808. /*==========================================
  4809. *
  4810. *------------------------------------------*/
  4811. BUILDIN_FUNC(cutin)
  4812. {
  4813. TBL_PC* sd;
  4814. sd = script_rid2sd(st);
  4815. if( sd == NULL )
  4816. return 0;
  4817. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4818. return 0;
  4819. }
  4820. /*==========================================
  4821. *
  4822. *------------------------------------------*/
  4823. BUILDIN_FUNC(viewpoint)
  4824. {
  4825. int type,x,y,id,color;
  4826. TBL_PC* sd;
  4827. type=script_getnum(st,2);
  4828. x=script_getnum(st,3);
  4829. y=script_getnum(st,4);
  4830. id=script_getnum(st,5);
  4831. color=script_getnum(st,6);
  4832. sd = script_rid2sd(st);
  4833. if( sd == NULL )
  4834. return 0;
  4835. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4836. return 0;
  4837. }
  4838. /*==========================================
  4839. *
  4840. *------------------------------------------*/
  4841. BUILDIN_FUNC(countitem)
  4842. {
  4843. int nameid, i;
  4844. int count = 0;
  4845. struct item_data* id = NULL;
  4846. struct script_data* data;
  4847. TBL_PC* sd = script_rid2sd(st);
  4848. if (!sd) {
  4849. script_pushint(st,0);
  4850. return 0;
  4851. }
  4852. data = script_getdata(st,2);
  4853. get_val(st, data); // convert into value in case of a variable
  4854. if( data_isstring(data) )
  4855. {// item name
  4856. id = itemdb_searchname(conv_str(st, data));
  4857. }
  4858. else
  4859. {// item id
  4860. id = itemdb_exists(conv_num(st, data));
  4861. }
  4862. if( id == NULL )
  4863. {
  4864. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4865. script_pushint(st,0);
  4866. return 1;
  4867. }
  4868. nameid = id->nameid;
  4869. for(i = 0; i < MAX_INVENTORY; i++)
  4870. if(sd->status.inventory[i].nameid == nameid)
  4871. count += sd->status.inventory[i].amount;
  4872. script_pushint(st,count);
  4873. return 0;
  4874. }
  4875. /*==========================================
  4876. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4877. * returns number of items that meet the conditions
  4878. *------------------------------------------*/
  4879. BUILDIN_FUNC(countitem2)
  4880. {
  4881. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4882. int count = 0;
  4883. int i;
  4884. struct item_data* id = NULL;
  4885. struct script_data* data;
  4886. TBL_PC* sd = script_rid2sd(st);
  4887. if (!sd) {
  4888. script_pushint(st,0);
  4889. return 0;
  4890. }
  4891. data = script_getdata(st,2);
  4892. get_val(st, data); // convert into value in case of a variable
  4893. if( data_isstring(data) )
  4894. {// item name
  4895. id = itemdb_searchname(conv_str(st, data));
  4896. }
  4897. else
  4898. {// item id
  4899. id = itemdb_exists(conv_num(st, data));
  4900. }
  4901. if( id == NULL )
  4902. {
  4903. ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4904. script_pushint(st,0);
  4905. return 1;
  4906. }
  4907. nameid = id->nameid;
  4908. iden = script_getnum(st,3);
  4909. ref = script_getnum(st,4);
  4910. attr = script_getnum(st,5);
  4911. c1 = (short)script_getnum(st,6);
  4912. c2 = (short)script_getnum(st,7);
  4913. c3 = (short)script_getnum(st,8);
  4914. c4 = (short)script_getnum(st,9);
  4915. for(i = 0; i < MAX_INVENTORY; i++)
  4916. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4917. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4918. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4919. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4920. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4921. sd->status.inventory[i].card[3] == c4
  4922. )
  4923. count += sd->status.inventory[i].amount;
  4924. script_pushint(st,count);
  4925. return 0;
  4926. }
  4927. /*==========================================
  4928. * 重量チェック
  4929. *------------------------------------------*/
  4930. BUILDIN_FUNC(checkweight)
  4931. {
  4932. int nameid, amount, slots;
  4933. unsigned int weight;
  4934. struct item_data* id = NULL;
  4935. struct map_session_data* sd;
  4936. struct script_data* data;
  4937. if( ( sd = script_rid2sd(st) ) == NULL )
  4938. {
  4939. return 0;
  4940. }
  4941. data = script_getdata(st,2);
  4942. get_val(st, data); // convert into value in case of a variable
  4943. if( data_isstring(data) )
  4944. {// item name
  4945. id = itemdb_searchname(conv_str(st, data));
  4946. }
  4947. else
  4948. {// item id
  4949. id = itemdb_exists(conv_num(st, data));
  4950. }
  4951. if( id == NULL )
  4952. {
  4953. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  4954. script_pushint(st,0);
  4955. return 1;
  4956. }
  4957. nameid = id->nameid;
  4958. amount = script_getnum(st,3);
  4959. if( amount < 1 )
  4960. {
  4961. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  4962. script_pushint(st,0);
  4963. return 1;
  4964. }
  4965. weight = itemdb_weight(nameid)*amount;
  4966. if( weight + sd->weight > sd->max_weight )
  4967. {// too heavy
  4968. script_pushint(st,0);
  4969. return 0;
  4970. }
  4971. switch( pc_checkadditem(sd, nameid, amount) )
  4972. {
  4973. case ADDITEM_EXIST:
  4974. // item is already in inventory, but there is still space for the requested amount
  4975. break;
  4976. case ADDITEM_NEW:
  4977. slots = pc_inventoryblank(sd);
  4978. if( itemdb_isstackable(nameid) )
  4979. {// stackable
  4980. if( slots < 1 )
  4981. {
  4982. script_pushint(st,0);
  4983. return 0;
  4984. }
  4985. }
  4986. else
  4987. {// non-stackable
  4988. if( slots < amount )
  4989. {
  4990. script_pushint(st,0);
  4991. return 0;
  4992. }
  4993. }
  4994. break;
  4995. case ADDITEM_OVERAMOUNT:
  4996. script_pushint(st,0);
  4997. return 0;
  4998. }
  4999. script_pushint(st,1);
  5000. return 0;
  5001. }
  5002. /*==========================================
  5003. * getitem <item id>,<amount>{,<account ID>};
  5004. * getitem "<item name>",<amount>{,<account ID>};
  5005. *------------------------------------------*/
  5006. BUILDIN_FUNC(getitem)
  5007. {
  5008. int nameid,amount,get_count,i,flag = 0;
  5009. struct item it;
  5010. TBL_PC *sd;
  5011. struct script_data *data;
  5012. data=script_getdata(st,2);
  5013. get_val(st,data);
  5014. if( data_isstring(data) )
  5015. {// "<item name>"
  5016. const char *name=conv_str(st,data);
  5017. struct item_data *item_data = itemdb_searchname(name);
  5018. if( item_data == NULL ){
  5019. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5020. return 1; //No item created.
  5021. }
  5022. nameid=item_data->nameid;
  5023. } else if( data_isint(data) )
  5024. {// <item id>
  5025. nameid=conv_num(st,data);
  5026. //Violet Box, Blue Box, etc - random item pick
  5027. if( nameid < 0 ) {
  5028. nameid=itemdb_searchrandomid(-nameid);
  5029. flag = 1;
  5030. }
  5031. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5032. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5033. return 1; //No item created.
  5034. }
  5035. } else {
  5036. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5037. return 1;
  5038. }
  5039. // <amount>
  5040. if( (amount=script_getnum(st,3)) <= 0)
  5041. return 0; //return if amount <=0, skip the useles iteration
  5042. memset(&it,0,sizeof(it));
  5043. it.nameid=nameid;
  5044. if(!flag)
  5045. it.identify=1;
  5046. else
  5047. it.identify=itemdb_isidentified(nameid);
  5048. if( script_hasdata(st,4) )
  5049. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5050. else
  5051. sd=script_rid2sd(st); // Attached player
  5052. if( sd == NULL ) // no target
  5053. return 0;
  5054. //Check if it's stackable.
  5055. if (!itemdb_isstackable(nameid))
  5056. get_count = 1;
  5057. else
  5058. get_count = amount;
  5059. for (i = 0; i < amount; i += get_count)
  5060. {
  5061. // if not pet egg
  5062. if (!pet_create_egg(sd, nameid))
  5063. {
  5064. if ((flag = pc_additem(sd, &it, get_count)))
  5065. {
  5066. clif_additem(sd, 0, 0, flag);
  5067. if( pc_candrop(sd,&it) )
  5068. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5069. }
  5070. }
  5071. }
  5072. //Logs items, got from (N)PC scripts [Lupus]
  5073. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  5074. log_pick_pc(sd, "N", nameid, amount, NULL);
  5075. return 0;
  5076. }
  5077. /*==========================================
  5078. *
  5079. *------------------------------------------*/
  5080. BUILDIN_FUNC(getitem2)
  5081. {
  5082. int nameid,amount,get_count,i,flag = 0;
  5083. int iden,ref,attr,c1,c2,c3,c4;
  5084. struct item_data *item_data;
  5085. struct item item_tmp;
  5086. TBL_PC *sd;
  5087. struct script_data *data;
  5088. if( script_hasdata(st,11) )
  5089. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5090. else
  5091. sd=script_rid2sd(st); // Attached player
  5092. if( sd == NULL ) // no target
  5093. return 0;
  5094. data=script_getdata(st,2);
  5095. get_val(st,data);
  5096. if( data_isstring(data) ){
  5097. const char *name=conv_str(st,data);
  5098. struct item_data *item_data = itemdb_searchname(name);
  5099. if( item_data )
  5100. nameid=item_data->nameid;
  5101. else
  5102. nameid=UNKNOWN_ITEM_ID;
  5103. }else
  5104. nameid=conv_num(st,data);
  5105. amount=script_getnum(st,3);
  5106. iden=script_getnum(st,4);
  5107. ref=script_getnum(st,5);
  5108. attr=script_getnum(st,6);
  5109. c1=(short)script_getnum(st,7);
  5110. c2=(short)script_getnum(st,8);
  5111. c3=(short)script_getnum(st,9);
  5112. c4=(short)script_getnum(st,10);
  5113. if(nameid<0) { // ランダム
  5114. nameid=itemdb_searchrandomid(-nameid);
  5115. flag = 1;
  5116. }
  5117. if(nameid > 0) {
  5118. memset(&item_tmp,0,sizeof(item_tmp));
  5119. item_data=itemdb_exists(nameid);
  5120. if (item_data == NULL)
  5121. return -1;
  5122. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5123. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5124. }
  5125. else if(item_data->type==IT_PETEGG) {
  5126. iden = 1;
  5127. ref = 0;
  5128. }
  5129. else {
  5130. iden = 1;
  5131. ref = attr = 0;
  5132. }
  5133. item_tmp.nameid=nameid;
  5134. if(!flag)
  5135. item_tmp.identify=iden;
  5136. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5137. item_tmp.identify=0;
  5138. item_tmp.refine=ref;
  5139. item_tmp.attribute=attr;
  5140. item_tmp.card[0]=(short)c1;
  5141. item_tmp.card[1]=(short)c2;
  5142. item_tmp.card[2]=(short)c3;
  5143. item_tmp.card[3]=(short)c4;
  5144. //Check if it's stackable.
  5145. if (!itemdb_isstackable(nameid))
  5146. get_count = 1;
  5147. else
  5148. get_count = amount;
  5149. for (i = 0; i < amount; i += get_count)
  5150. {
  5151. // if not pet egg
  5152. if (!pet_create_egg(sd, nameid))
  5153. {
  5154. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  5155. {
  5156. clif_additem(sd, 0, 0, flag);
  5157. if( pc_candrop(sd,&item_tmp) )
  5158. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5159. }
  5160. }
  5161. }
  5162. //Logs items, got from (N)PC scripts [Lupus]
  5163. if(log_config.enable_logs&0x40)
  5164. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  5165. }
  5166. return 0;
  5167. }
  5168. /*==========================================
  5169. * rentitem <item id>,<seconds>
  5170. * rentitem "<item name>",<seconds>
  5171. *------------------------------------------*/
  5172. BUILDIN_FUNC(rentitem)
  5173. {
  5174. struct map_session_data *sd;
  5175. struct script_data *data;
  5176. struct item it;
  5177. int seconds;
  5178. int nameid = 0, flag;
  5179. data = script_getdata(st,2);
  5180. get_val(st,data);
  5181. if( (sd = script_rid2sd(st)) == NULL )
  5182. return 0;
  5183. if( data_isstring(data) )
  5184. {
  5185. const char *name = conv_str(st,data);
  5186. struct item_data *itd = itemdb_searchname(name);
  5187. if( itd == NULL )
  5188. {
  5189. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5190. return 1;
  5191. }
  5192. nameid = itd->nameid;
  5193. }
  5194. else if( data_isint(data) )
  5195. {
  5196. nameid = conv_num(st,data);
  5197. if( nameid <= 0 || !itemdb_exists(nameid) )
  5198. {
  5199. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5200. return 1;
  5201. }
  5202. }
  5203. else
  5204. {
  5205. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5206. return 1;
  5207. }
  5208. seconds = script_getnum(st,3);
  5209. memset(&it, 0, sizeof(it));
  5210. it.nameid = nameid;
  5211. it.identify = 1;
  5212. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5213. if( (flag = pc_additem(sd, &it, 1)) )
  5214. {
  5215. clif_additem(sd, 0, 0, flag);
  5216. return 1;
  5217. }
  5218. clif_rental_time(sd->fd, nameid, seconds);
  5219. pc_inventory_rental_add(sd, seconds);
  5220. if( log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS )
  5221. log_pick_pc(sd, "N", nameid, 1, NULL);
  5222. return 0;
  5223. }
  5224. /*==========================================
  5225. * gets an item with someone's name inscribed [Skotlex]
  5226. * getinscribeditem item_num, character_name
  5227. * Returned Qty is always 1, only works on equip-able
  5228. * equipment
  5229. *------------------------------------------*/
  5230. BUILDIN_FUNC(getnameditem)
  5231. {
  5232. int nameid;
  5233. struct item item_tmp;
  5234. TBL_PC *sd, *tsd;
  5235. struct script_data *data;
  5236. sd = script_rid2sd(st);
  5237. if (sd == NULL)
  5238. { //Player not attached!
  5239. script_pushint(st,0);
  5240. return 0;
  5241. }
  5242. data=script_getdata(st,2);
  5243. get_val(st,data);
  5244. if( data_isstring(data) ){
  5245. const char *name=conv_str(st,data);
  5246. struct item_data *item_data = itemdb_searchname(name);
  5247. if( item_data == NULL)
  5248. { //Failed
  5249. script_pushint(st,0);
  5250. return 0;
  5251. }
  5252. nameid = item_data->nameid;
  5253. }else
  5254. nameid = conv_num(st,data);
  5255. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5256. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5257. script_pushint(st,0);
  5258. return 0;
  5259. }
  5260. data=script_getdata(st,3);
  5261. get_val(st,data);
  5262. if( data_isstring(data) ) //Char Name
  5263. tsd=map_nick2sd(conv_str(st,data));
  5264. else //Char Id was given
  5265. tsd=map_charid2sd(conv_num(st,data));
  5266. if( tsd == NULL )
  5267. { //Failed
  5268. script_pushint(st,0);
  5269. return 0;
  5270. }
  5271. memset(&item_tmp,0,sizeof(item_tmp));
  5272. item_tmp.nameid=nameid;
  5273. item_tmp.amount=1;
  5274. item_tmp.identify=1;
  5275. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5276. item_tmp.card[2]=tsd->status.char_id;
  5277. item_tmp.card[3]=tsd->status.char_id >> 16;
  5278. if(pc_additem(sd,&item_tmp,1)) {
  5279. script_pushint(st,0);
  5280. return 0; //Failed to add item, we will not drop if they don't fit
  5281. }
  5282. //Logs items, got from (N)PC scripts [Lupus]
  5283. if(log_config.enable_logs&0x40)
  5284. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5285. script_pushint(st,1);
  5286. return 0;
  5287. }
  5288. /*==========================================
  5289. * gets a random item ID from an item group [Skotlex]
  5290. * groupranditem group_num
  5291. *------------------------------------------*/
  5292. BUILDIN_FUNC(grouprandomitem)
  5293. {
  5294. int group;
  5295. group = script_getnum(st,2);
  5296. script_pushint(st,itemdb_searchrandomid(group));
  5297. return 0;
  5298. }
  5299. /*==========================================
  5300. *
  5301. *------------------------------------------*/
  5302. BUILDIN_FUNC(makeitem)
  5303. {
  5304. int nameid,amount,flag = 0;
  5305. int x,y,m;
  5306. const char *mapname;
  5307. struct item item_tmp;
  5308. struct script_data *data;
  5309. data=script_getdata(st,2);
  5310. get_val(st,data);
  5311. if( data_isstring(data) ){
  5312. const char *name=conv_str(st,data);
  5313. struct item_data *item_data = itemdb_searchname(name);
  5314. if( item_data )
  5315. nameid=item_data->nameid;
  5316. else
  5317. nameid=UNKNOWN_ITEM_ID;
  5318. }else
  5319. nameid=conv_num(st,data);
  5320. amount=script_getnum(st,3);
  5321. mapname =script_getstr(st,4);
  5322. x =script_getnum(st,5);
  5323. y =script_getnum(st,6);
  5324. if(strcmp(mapname,"this")==0)
  5325. {
  5326. TBL_PC *sd;
  5327. sd = script_rid2sd(st);
  5328. if (!sd) return 0; //Failed...
  5329. m=sd->bl.m;
  5330. } else
  5331. m=map_mapname2mapid(mapname);
  5332. if(nameid<0) { // ランダム
  5333. nameid=itemdb_searchrandomid(-nameid);
  5334. flag = 1;
  5335. }
  5336. if(nameid > 0) {
  5337. memset(&item_tmp,0,sizeof(item_tmp));
  5338. item_tmp.nameid=nameid;
  5339. if(!flag)
  5340. item_tmp.identify=1;
  5341. else
  5342. item_tmp.identify=itemdb_isidentified(nameid);
  5343. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5344. }
  5345. return 0;
  5346. }
  5347. /// Counts / deletes the current item given by idx.
  5348. /// Used by buildin_delitem_search
  5349. /// Relies on all input data being already fully valid.
  5350. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5351. {
  5352. int delamount;
  5353. struct item* inv = &sd->status.inventory[idx];
  5354. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5355. if( delete_items )
  5356. {
  5357. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5358. {// delete associated pet
  5359. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5360. }
  5361. //Logs items, got from (N)PC scripts [Lupus]
  5362. if( log_config.enable_logs&0x40 )
  5363. {
  5364. log_pick_pc(sd, "N", inv->nameid, -delamount, inv);
  5365. }
  5366. //Logs
  5367. pc_delitem(sd, idx, delamount, 0, 0);
  5368. }
  5369. amount[0]-= delamount;
  5370. }
  5371. /// Searches for item(s) and checks, if there is enough of them.
  5372. /// Used by delitem and delitem2
  5373. /// Relies on all input data being already fully valid.
  5374. /// @param exact_match will also match item attributes and cards, not just name id
  5375. /// @return true when all items could be deleted, false when there were not enough items to delete
  5376. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5377. {
  5378. bool delete_items = false;
  5379. int i, amount, important;
  5380. struct item* inv;
  5381. // prefer always non-equipped items
  5382. it->equip = 0;
  5383. // when searching for nameid only, prefer additionally
  5384. if( !exact_match )
  5385. {
  5386. // non-refined items
  5387. it->refine = 0;
  5388. // card-less items
  5389. memset(it->card, 0, sizeof(it->card));
  5390. }
  5391. for(;;)
  5392. {
  5393. amount = it->amount;
  5394. important = 0;
  5395. // 1st pass -- less important items / exact match
  5396. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5397. {
  5398. inv = &sd->status.inventory[i];
  5399. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5400. {// wrong/invalid item
  5401. continue;
  5402. }
  5403. if( inv->equip != it->equip || inv->refine != it->refine )
  5404. {// not matching attributes
  5405. important++;
  5406. continue;
  5407. }
  5408. if( exact_match )
  5409. {
  5410. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5411. {// not matching exact attributes
  5412. continue;
  5413. }
  5414. }
  5415. else
  5416. {
  5417. if( sd->inventory_data[i]->type == IT_PETEGG )
  5418. {
  5419. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5420. {// pet which cannot be deleted
  5421. continue;
  5422. }
  5423. }
  5424. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  5425. {// named/carded item
  5426. important++;
  5427. continue;
  5428. }
  5429. }
  5430. // count / delete item
  5431. buildin_delitem_delete(sd, i, &amount, delete_items);
  5432. }
  5433. // 2nd pass -- any matching item
  5434. if( amount == 0 || important == 0 )
  5435. {// either everything was already consumed or no items were skipped
  5436. ;
  5437. }
  5438. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5439. {
  5440. inv = &sd->status.inventory[i];
  5441. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5442. {// wrong/invalid item
  5443. continue;
  5444. }
  5445. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  5446. {// pet which cannot be deleted
  5447. continue;
  5448. }
  5449. if( exact_match )
  5450. {
  5451. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5452. {// not matching attributes
  5453. continue;
  5454. }
  5455. }
  5456. // count / delete item
  5457. buildin_delitem_delete(sd, i, &amount, delete_items);
  5458. }
  5459. if( amount )
  5460. {// not enough items
  5461. return false;
  5462. }
  5463. else if( delete_items )
  5464. {// we are done with the work
  5465. return true;
  5466. }
  5467. else
  5468. {// get rid of the items now
  5469. delete_items = true;
  5470. }
  5471. }
  5472. }
  5473. /// Deletes items from the target/attached player.
  5474. /// Prioritizes ordinary items.
  5475. ///
  5476. /// delitem <item id>,<amount>{,<account id>}
  5477. /// delitem "<item name>",<amount>{,<account id>}
  5478. BUILDIN_FUNC(delitem)
  5479. {
  5480. TBL_PC *sd;
  5481. struct item it;
  5482. struct script_data *data;
  5483. if( script_hasdata(st,4) )
  5484. {
  5485. int account_id = script_getnum(st,4);
  5486. sd = map_id2sd(account_id); // <account id>
  5487. if( sd == NULL )
  5488. {
  5489. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5490. st->state = END;
  5491. return 1;
  5492. }
  5493. }
  5494. else
  5495. {
  5496. sd = script_rid2sd(st);// attached player
  5497. if( sd == NULL )
  5498. return 0;
  5499. }
  5500. data = script_getdata(st,2);
  5501. get_val(st,data);
  5502. if( data_isstring(data) )
  5503. {
  5504. const char* item_name = conv_str(st,data);
  5505. struct item_data* id = itemdb_searchname(item_name);
  5506. if( id == NULL )
  5507. {
  5508. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5509. st->state = END;
  5510. return 1;
  5511. }
  5512. it.nameid = id->nameid;// "<item name>"
  5513. }
  5514. else
  5515. {
  5516. it.nameid = conv_num(st,data);// <item id>
  5517. if( !itemdb_exists( it.nameid ) )
  5518. {
  5519. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5520. st->state = END;
  5521. return 1;
  5522. }
  5523. }
  5524. it.amount=script_getnum(st,3);
  5525. if( it.amount <= 0 )
  5526. return 0;// nothing to do
  5527. if( buildin_delitem_search(sd, &it, false) )
  5528. {// success
  5529. return 0;
  5530. }
  5531. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5532. st->state = END;
  5533. clif_scriptclose(sd, st->oid);
  5534. return 1;
  5535. }
  5536. /// Deletes items from the target/attached player.
  5537. ///
  5538. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5539. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5540. BUILDIN_FUNC(delitem2)
  5541. {
  5542. TBL_PC *sd;
  5543. struct item it;
  5544. struct script_data *data;
  5545. if( script_hasdata(st,11) )
  5546. {
  5547. int account_id = script_getnum(st,11);
  5548. sd = map_id2sd(account_id); // <account id>
  5549. if( sd == NULL )
  5550. {
  5551. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5552. st->state = END;
  5553. return 1;
  5554. }
  5555. }
  5556. else
  5557. {
  5558. sd = script_rid2sd(st);// attached player
  5559. if( sd == NULL )
  5560. return 0;
  5561. }
  5562. data = script_getdata(st,2);
  5563. get_val(st,data);
  5564. if( data_isstring(data) )
  5565. {
  5566. const char* item_name = conv_str(st,data);
  5567. struct item_data* id = itemdb_searchname(item_name);
  5568. if( id == NULL )
  5569. {
  5570. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5571. st->state = END;
  5572. return 1;
  5573. }
  5574. it.nameid = id->nameid;// "<item name>"
  5575. }
  5576. else
  5577. {
  5578. it.nameid = conv_num(st,data);// <item id>
  5579. if( !itemdb_exists( it.nameid ) )
  5580. {
  5581. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  5582. st->state = END;
  5583. return 1;
  5584. }
  5585. }
  5586. it.amount=script_getnum(st,3);
  5587. it.identify=script_getnum(st,4);
  5588. it.refine=script_getnum(st,5);
  5589. it.attribute=script_getnum(st,6);
  5590. it.card[0]=(short)script_getnum(st,7);
  5591. it.card[1]=(short)script_getnum(st,8);
  5592. it.card[2]=(short)script_getnum(st,9);
  5593. it.card[3]=(short)script_getnum(st,10);
  5594. if( it.amount <= 0 )
  5595. return 0;// nothing to do
  5596. if( buildin_delitem_search(sd, &it, true) )
  5597. {// success
  5598. return 0;
  5599. }
  5600. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  5601. st->state = END;
  5602. clif_scriptclose(sd, st->oid);
  5603. return 1;
  5604. }
  5605. /*==========================================
  5606. * Enables/Disables use of items while in an NPC [Skotlex]
  5607. *------------------------------------------*/
  5608. BUILDIN_FUNC(enableitemuse)
  5609. {
  5610. TBL_PC *sd;
  5611. sd=script_rid2sd(st);
  5612. if (sd)
  5613. sd->npc_item_flag = st->oid;
  5614. return 0;
  5615. }
  5616. BUILDIN_FUNC(disableitemuse)
  5617. {
  5618. TBL_PC *sd;
  5619. sd=script_rid2sd(st);
  5620. if (sd)
  5621. sd->npc_item_flag = 0;
  5622. return 0;
  5623. }
  5624. /*==========================================
  5625. *キャラ関係のパラメータ取得
  5626. *------------------------------------------*/
  5627. BUILDIN_FUNC(readparam)
  5628. {
  5629. int type;
  5630. TBL_PC *sd;
  5631. type=script_getnum(st,2);
  5632. if( script_hasdata(st,3) )
  5633. sd=map_nick2sd(script_getstr(st,3));
  5634. else
  5635. sd=script_rid2sd(st);
  5636. if(sd==NULL){
  5637. script_pushint(st,-1);
  5638. return 0;
  5639. }
  5640. script_pushint(st,pc_readparam(sd,type));
  5641. return 0;
  5642. }
  5643. /*==========================================
  5644. *キャラ関係のID取得
  5645. *------------------------------------------*/
  5646. BUILDIN_FUNC(getcharid)
  5647. {
  5648. int num;
  5649. TBL_PC *sd;
  5650. num = script_getnum(st,2);
  5651. if( script_hasdata(st,3) )
  5652. sd=map_nick2sd(script_getstr(st,3));
  5653. else
  5654. sd=script_rid2sd(st);
  5655. if(sd==NULL){
  5656. script_pushint(st,0); //return 0, according docs
  5657. return 0;
  5658. }
  5659. switch( num ) {
  5660. case 0: script_pushint(st,sd->status.char_id); break;
  5661. case 1: script_pushint(st,sd->status.party_id); break;
  5662. case 2: script_pushint(st,sd->status.guild_id); break;
  5663. case 3: script_pushint(st,sd->status.account_id); break;
  5664. case 4: script_pushint(st,sd->bg_id); break;
  5665. default:
  5666. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5667. script_pushint(st,0);
  5668. break;
  5669. }
  5670. return 0;
  5671. }
  5672. /*==========================================
  5673. *指定IDのPT名取得
  5674. *------------------------------------------*/
  5675. BUILDIN_FUNC(getpartyname)
  5676. {
  5677. int party_id;
  5678. struct party_data* p;
  5679. party_id = script_getnum(st,2);
  5680. if( ( p = party_search(party_id) ) != NULL )
  5681. {
  5682. script_pushstrcopy(st,p->party.name);
  5683. }
  5684. else
  5685. {
  5686. script_pushconststr(st,"null");
  5687. }
  5688. return 0;
  5689. }
  5690. /*==========================================
  5691. *指定IDのPT人数とメンバーID取得
  5692. *------------------------------------------*/
  5693. BUILDIN_FUNC(getpartymember)
  5694. {
  5695. struct party_data *p;
  5696. int i,j=0,type=0;
  5697. p=party_search(script_getnum(st,2));
  5698. if( script_hasdata(st,3) )
  5699. type=script_getnum(st,3);
  5700. if(p!=NULL){
  5701. for(i=0;i<MAX_PARTY;i++){
  5702. if(p->party.member[i].account_id){
  5703. switch (type) {
  5704. case 2:
  5705. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  5706. break;
  5707. case 1:
  5708. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  5709. break;
  5710. default:
  5711. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  5712. }
  5713. j++;
  5714. }
  5715. }
  5716. }
  5717. mapreg_setreg(add_str("$@partymembercount"),j);
  5718. return 0;
  5719. }
  5720. /*==========================================
  5721. * Retrieves party leader. if flag is specified,
  5722. * return some of the leader data. Otherwise, return name.
  5723. *------------------------------------------*/
  5724. BUILDIN_FUNC(getpartyleader)
  5725. {
  5726. int party_id, type = 0, i=0;
  5727. struct party_data *p;
  5728. party_id=script_getnum(st,2);
  5729. if( script_hasdata(st,3) )
  5730. type=script_getnum(st,3);
  5731. p=party_search(party_id);
  5732. if (p) //Search leader
  5733. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5734. if (!p || i == MAX_PARTY) { //leader not found
  5735. if (type)
  5736. script_pushint(st,-1);
  5737. else
  5738. script_pushconststr(st,"null");
  5739. return 0;
  5740. }
  5741. switch (type) {
  5742. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5743. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5744. case 3: script_pushint(st,p->party.member[i].class_); break;
  5745. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5746. case 5: script_pushint(st,p->party.member[i].lv); break;
  5747. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5748. }
  5749. return 0;
  5750. }
  5751. /*==========================================
  5752. *指定IDのギルド名取得
  5753. *------------------------------------------*/
  5754. BUILDIN_FUNC(getguildname)
  5755. {
  5756. int guild_id;
  5757. struct guild* g;
  5758. guild_id = script_getnum(st,2);
  5759. if( ( g = guild_search(guild_id) ) != NULL )
  5760. {
  5761. script_pushstrcopy(st,g->name);
  5762. }
  5763. else
  5764. {
  5765. script_pushconststr(st,"null");
  5766. }
  5767. return 0;
  5768. }
  5769. /*==========================================
  5770. *指定IDのGuildMaster名取得
  5771. *------------------------------------------*/
  5772. BUILDIN_FUNC(getguildmaster)
  5773. {
  5774. int guild_id;
  5775. struct guild* g;
  5776. guild_id = script_getnum(st,2);
  5777. if( ( g = guild_search(guild_id) ) != NULL )
  5778. {
  5779. script_pushstrcopy(st,g->member[0].name);
  5780. }
  5781. else
  5782. {
  5783. script_pushconststr(st,"null");
  5784. }
  5785. return 0;
  5786. }
  5787. BUILDIN_FUNC(getguildmasterid)
  5788. {
  5789. int guild_id;
  5790. struct guild* g;
  5791. guild_id = script_getnum(st,2);
  5792. if( ( g = guild_search(guild_id) ) != NULL )
  5793. {
  5794. script_pushint(st,g->member[0].char_id);
  5795. }
  5796. else
  5797. {
  5798. script_pushint(st,0);
  5799. }
  5800. return 0;
  5801. }
  5802. /*==========================================
  5803. * キャラクタの名前
  5804. *------------------------------------------*/
  5805. BUILDIN_FUNC(strcharinfo)
  5806. {
  5807. TBL_PC *sd;
  5808. int num;
  5809. struct guild* g;
  5810. struct party_data* p;
  5811. sd=script_rid2sd(st);
  5812. if (!sd) { //Avoid crashing....
  5813. script_pushconststr(st,"");
  5814. return 0;
  5815. }
  5816. num=script_getnum(st,2);
  5817. switch(num){
  5818. case 0:
  5819. script_pushstrcopy(st,sd->status.name);
  5820. break;
  5821. case 1:
  5822. if( ( p = party_search(sd->status.party_id) ) != NULL )
  5823. {
  5824. script_pushstrcopy(st,p->party.name);
  5825. }
  5826. else
  5827. {
  5828. script_pushconststr(st,"");
  5829. }
  5830. break;
  5831. case 2:
  5832. if( ( g = guild_search(sd->status.guild_id) ) != NULL )
  5833. {
  5834. script_pushstrcopy(st,g->name);
  5835. }
  5836. else
  5837. {
  5838. script_pushconststr(st,"");
  5839. }
  5840. break;
  5841. case 3:
  5842. script_pushconststr(st,map[sd->bl.m].name);
  5843. break;
  5844. default:
  5845. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5846. script_pushconststr(st,"");
  5847. break;
  5848. }
  5849. return 0;
  5850. }
  5851. /*==========================================
  5852. * 呼び出し元のNPC情報を取得する
  5853. *------------------------------------------*/
  5854. BUILDIN_FUNC(strnpcinfo)
  5855. {
  5856. TBL_NPC* nd;
  5857. int num;
  5858. char *buf,*name=NULL;
  5859. nd = map_id2nd(st->oid);
  5860. if (!nd) {
  5861. script_pushconststr(st, "");
  5862. return 0;
  5863. }
  5864. num = script_getnum(st,2);
  5865. switch(num){
  5866. case 0: // display name
  5867. name = aStrdup(nd->name);
  5868. break;
  5869. case 1: // visible part of display name
  5870. if((buf = strchr(nd->name,'#')) != NULL)
  5871. {
  5872. name = aStrdup(nd->name);
  5873. name[buf - nd->name] = 0;
  5874. } else // Return the name, there is no '#' present
  5875. name = aStrdup(nd->name);
  5876. break;
  5877. case 2: // # fragment
  5878. if((buf = strchr(nd->name,'#')) != NULL)
  5879. name = aStrdup(buf+1);
  5880. break;
  5881. case 3: // unique name
  5882. name = aStrdup(nd->exname);
  5883. break;
  5884. case 4: // map name
  5885. name = aStrdup(map[nd->bl.m].name);
  5886. break;
  5887. }
  5888. if(name)
  5889. script_pushstr(st, name);
  5890. else
  5891. script_pushconststr(st, "");
  5892. return 0;
  5893. }
  5894. // aegis->athena slot position conversion table
  5895. 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};
  5896. /*==========================================
  5897. * GetEquipID(Pos); Pos: 1-10
  5898. *------------------------------------------*/
  5899. BUILDIN_FUNC(getequipid)
  5900. {
  5901. int i, num;
  5902. TBL_PC* sd;
  5903. struct item_data* item;
  5904. sd = script_rid2sd(st);
  5905. if( sd == NULL )
  5906. return 0;
  5907. num = script_getnum(st,2) - 1;
  5908. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5909. {
  5910. script_pushint(st,-1);
  5911. return 0;
  5912. }
  5913. // get inventory position of item
  5914. i = pc_checkequip(sd,equip[num]);
  5915. if( i < 0 )
  5916. {
  5917. script_pushint(st,-1);
  5918. return 0;
  5919. }
  5920. item = sd->inventory_data[i];
  5921. if( item != 0 )
  5922. script_pushint(st,item->nameid);
  5923. else
  5924. script_pushint(st,0);
  5925. return 0;
  5926. }
  5927. /*==========================================
  5928. * 装備名文字列(精錬メニュー用)
  5929. *------------------------------------------*/
  5930. BUILDIN_FUNC(getequipname)
  5931. {
  5932. int i, num;
  5933. TBL_PC* sd;
  5934. struct item_data* item;
  5935. sd = script_rid2sd(st);
  5936. if( sd == NULL )
  5937. return 0;
  5938. num = script_getnum(st,2) - 1;
  5939. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5940. {
  5941. script_pushconststr(st,"");
  5942. return 0;
  5943. }
  5944. // get inventory position of item
  5945. i = pc_checkequip(sd,equip[num]);
  5946. if( i < 0 )
  5947. {
  5948. script_pushint(st,-1);
  5949. return 0;
  5950. }
  5951. item = sd->inventory_data[i];
  5952. if( item != 0 )
  5953. script_pushstrcopy(st,item->jname);
  5954. else
  5955. script_pushconststr(st,"");
  5956. return 0;
  5957. }
  5958. /*==========================================
  5959. * getbrokenid [Valaris]
  5960. *------------------------------------------*/
  5961. BUILDIN_FUNC(getbrokenid)
  5962. {
  5963. int i,num,id=0,brokencounter=0;
  5964. TBL_PC *sd;
  5965. sd = script_rid2sd(st);
  5966. if( sd == NULL )
  5967. return 0;
  5968. num=script_getnum(st,2);
  5969. for(i=0; i<MAX_INVENTORY; i++) {
  5970. if(sd->status.inventory[i].attribute){
  5971. brokencounter++;
  5972. if(num==brokencounter){
  5973. id=sd->status.inventory[i].nameid;
  5974. break;
  5975. }
  5976. }
  5977. }
  5978. script_pushint(st,id);
  5979. return 0;
  5980. }
  5981. /*==========================================
  5982. * repair [Valaris]
  5983. *------------------------------------------*/
  5984. BUILDIN_FUNC(repair)
  5985. {
  5986. int i,num;
  5987. int repaircounter=0;
  5988. TBL_PC *sd;
  5989. sd = script_rid2sd(st);
  5990. if( sd == NULL )
  5991. return 0;
  5992. num=script_getnum(st,2);
  5993. for(i=0; i<MAX_INVENTORY; i++) {
  5994. if(sd->status.inventory[i].attribute){
  5995. repaircounter++;
  5996. if(num==repaircounter){
  5997. sd->status.inventory[i].attribute=0;
  5998. clif_equiplist(sd);
  5999. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6000. clif_misceffect(&sd->bl, 3);
  6001. break;
  6002. }
  6003. }
  6004. }
  6005. return 0;
  6006. }
  6007. /*==========================================
  6008. * 装備チェック
  6009. *------------------------------------------*/
  6010. BUILDIN_FUNC(getequipisequiped)
  6011. {
  6012. int i=-1,num;
  6013. TBL_PC *sd;
  6014. num=script_getnum(st,2);
  6015. sd = script_rid2sd(st);
  6016. if( sd == NULL )
  6017. return 0;
  6018. if (num > 0 && num <= ARRAYLENGTH(equip))
  6019. i=pc_checkequip(sd,equip[num-1]);
  6020. if(i >= 0)
  6021. script_pushint(st,1);
  6022. else
  6023. script_pushint(st,0);
  6024. return 0;
  6025. }
  6026. /*==========================================
  6027. * 装備品精錬可能チェック
  6028. *------------------------------------------*/
  6029. BUILDIN_FUNC(getequipisenableref)
  6030. {
  6031. int i=-1,num;
  6032. TBL_PC *sd;
  6033. num=script_getnum(st,2);
  6034. sd = script_rid2sd(st);
  6035. if( sd == NULL )
  6036. return 0;
  6037. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6038. i = pc_checkequip(sd,equip[num-1]);
  6039. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6040. script_pushint(st,1);
  6041. else
  6042. script_pushint(st,0);
  6043. return 0;
  6044. }
  6045. /*==========================================
  6046. * 装備品鑑定チェック
  6047. *------------------------------------------*/
  6048. BUILDIN_FUNC(getequipisidentify)
  6049. {
  6050. int i=-1,num;
  6051. TBL_PC *sd;
  6052. num=script_getnum(st,2);
  6053. sd = script_rid2sd(st);
  6054. if( sd == NULL )
  6055. return 0;
  6056. if (num > 0 && num <= ARRAYLENGTH(equip))
  6057. i=pc_checkequip(sd,equip[num-1]);
  6058. if(i >= 0)
  6059. script_pushint(st,sd->status.inventory[i].identify);
  6060. else
  6061. script_pushint(st,0);
  6062. return 0;
  6063. }
  6064. /*==========================================
  6065. * 装備品精錬度
  6066. *------------------------------------------*/
  6067. BUILDIN_FUNC(getequiprefinerycnt)
  6068. {
  6069. int i=-1,num;
  6070. TBL_PC *sd;
  6071. num=script_getnum(st,2);
  6072. sd = script_rid2sd(st);
  6073. if( sd == NULL )
  6074. return 0;
  6075. if (num > 0 && num <= ARRAYLENGTH(equip))
  6076. i=pc_checkequip(sd,equip[num-1]);
  6077. if(i >= 0)
  6078. script_pushint(st,sd->status.inventory[i].refine);
  6079. else
  6080. script_pushint(st,0);
  6081. return 0;
  6082. }
  6083. /*==========================================
  6084. * 装備品武器LV
  6085. *------------------------------------------*/
  6086. BUILDIN_FUNC(getequipweaponlv)
  6087. {
  6088. int i=-1,num;
  6089. TBL_PC *sd;
  6090. num=script_getnum(st,2);
  6091. sd = script_rid2sd(st);
  6092. if( sd == NULL )
  6093. return 0;
  6094. if (num > 0 && num <= ARRAYLENGTH(equip))
  6095. i=pc_checkequip(sd,equip[num-1]);
  6096. if(i >= 0 && sd->inventory_data[i])
  6097. script_pushint(st,sd->inventory_data[i]->wlv);
  6098. else
  6099. script_pushint(st,0);
  6100. return 0;
  6101. }
  6102. /*==========================================
  6103. * 装備品精錬成功率
  6104. *------------------------------------------*/
  6105. BUILDIN_FUNC(getequippercentrefinery)
  6106. {
  6107. int i=-1,num;
  6108. TBL_PC *sd;
  6109. num=script_getnum(st,2);
  6110. sd = script_rid2sd(st);
  6111. if( sd == NULL )
  6112. return 0;
  6113. if (num > 0 && num <= ARRAYLENGTH(equip))
  6114. i=pc_checkequip(sd,equip[num-1]);
  6115. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6116. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  6117. else
  6118. script_pushint(st,0);
  6119. return 0;
  6120. }
  6121. /*==========================================
  6122. * 精錬成功
  6123. *------------------------------------------*/
  6124. BUILDIN_FUNC(successrefitem)
  6125. {
  6126. int i=-1,num,ep;
  6127. TBL_PC *sd;
  6128. num=script_getnum(st,2);
  6129. sd = script_rid2sd(st);
  6130. if( sd == NULL )
  6131. return 0;
  6132. if (num > 0 && num <= ARRAYLENGTH(equip))
  6133. i=pc_checkequip(sd,equip[num-1]);
  6134. if(i >= 0) {
  6135. ep=sd->status.inventory[i].equip;
  6136. //Logs items, got from (N)PC scripts [Lupus]
  6137. if(log_config.enable_logs&0x40)
  6138. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6139. sd->status.inventory[i].refine++;
  6140. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6141. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6142. clif_delitem(sd,i,1,3);
  6143. //Logs items, got from (N)PC scripts [Lupus]
  6144. if(log_config.enable_logs&0x40)
  6145. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  6146. clif_additem(sd,i,1,0);
  6147. pc_equipitem(sd,i,ep);
  6148. clif_misceffect(&sd->bl,3);
  6149. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6150. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6151. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6152. ){ // Fame point system [DracoRPG]
  6153. switch (sd->inventory_data[i]->wlv){
  6154. case 1:
  6155. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6156. break;
  6157. case 2:
  6158. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6159. break;
  6160. case 3:
  6161. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6162. break;
  6163. }
  6164. }
  6165. }
  6166. return 0;
  6167. }
  6168. /*==========================================
  6169. * 精錬失敗
  6170. *------------------------------------------*/
  6171. BUILDIN_FUNC(failedrefitem)
  6172. {
  6173. int i=-1,num;
  6174. TBL_PC *sd;
  6175. num=script_getnum(st,2);
  6176. sd = script_rid2sd(st);
  6177. if( sd == NULL )
  6178. return 0;
  6179. if (num > 0 && num <= ARRAYLENGTH(equip))
  6180. i=pc_checkequip(sd,equip[num-1]);
  6181. if(i >= 0) {
  6182. //Logs items, got from (N)PC scripts [Lupus]
  6183. if(log_config.enable_logs&0x40)
  6184. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6185. sd->status.inventory[i].refine = 0;
  6186. pc_unequipitem(sd,i,3);
  6187. // 精錬失敗エフェクトのパケット
  6188. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6189. pc_delitem(sd,i,1,0,2);
  6190. // 他の人にも失敗を通知
  6191. clif_misceffect(&sd->bl,2);
  6192. }
  6193. return 0;
  6194. }
  6195. /*==========================================
  6196. *
  6197. *------------------------------------------*/
  6198. BUILDIN_FUNC(statusup)
  6199. {
  6200. int type;
  6201. TBL_PC *sd;
  6202. type=script_getnum(st,2);
  6203. sd = script_rid2sd(st);
  6204. if( sd == NULL )
  6205. return 0;
  6206. pc_statusup(sd,type);
  6207. return 0;
  6208. }
  6209. /*==========================================
  6210. *
  6211. *------------------------------------------*/
  6212. BUILDIN_FUNC(statusup2)
  6213. {
  6214. int type,val;
  6215. TBL_PC *sd;
  6216. type=script_getnum(st,2);
  6217. val=script_getnum(st,3);
  6218. sd = script_rid2sd(st);
  6219. if( sd == NULL )
  6220. return 0;
  6221. pc_statusup2(sd,type,val);
  6222. return 0;
  6223. }
  6224. /// See 'doc/item_bonus.txt'
  6225. ///
  6226. /// bonus <bonus type>,<val1>;
  6227. /// bonus2 <bonus type>,<val1>,<val2>;
  6228. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6229. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6230. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6231. BUILDIN_FUNC(bonus)
  6232. {
  6233. int type;
  6234. int val1;
  6235. int val2 = 0;
  6236. int val3 = 0;
  6237. int val4 = 0;
  6238. int val5 = 0;
  6239. TBL_PC* sd;
  6240. sd = script_rid2sd(st);
  6241. if( sd == NULL )
  6242. return 0; // no player attached
  6243. type = script_getnum(st,2);
  6244. switch( type )
  6245. {
  6246. case SP_AUTOSPELL:
  6247. case SP_AUTOSPELL_WHENHIT:
  6248. case SP_AUTOSPELL_ONSKILL:
  6249. case SP_SKILL_ATK:
  6250. case SP_SKILL_HEAL:
  6251. case SP_SKILL_HEAL2:
  6252. case SP_ADD_SKILL_BLOW:
  6253. case SP_CASTRATE:
  6254. case SP_ADDEFF_ONSKILL:
  6255. // these bonuses support skill names
  6256. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6257. break;
  6258. default:
  6259. val1 = script_getnum(st,3);
  6260. break;
  6261. }
  6262. switch( script_lastdata(st)-2 )
  6263. {
  6264. case 1:
  6265. pc_bonus(sd, type, val1);
  6266. break;
  6267. case 2:
  6268. val2 = script_getnum(st,4);
  6269. pc_bonus2(sd, type, val1, val2);
  6270. break;
  6271. case 3:
  6272. val2 = script_getnum(st,4);
  6273. val3 = script_getnum(st,5);
  6274. pc_bonus3(sd, type, val1, val2, val3);
  6275. break;
  6276. case 4:
  6277. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6278. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6279. else
  6280. val2 = script_getnum(st,4);
  6281. val3 = script_getnum(st,5);
  6282. val4 = script_getnum(st,6);
  6283. pc_bonus4(sd, type, val1, val2, val3, val4);
  6284. break;
  6285. case 5:
  6286. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6287. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6288. else
  6289. val2 = script_getnum(st,4);
  6290. val3 = script_getnum(st,5);
  6291. val4 = script_getnum(st,6);
  6292. val5 = script_getnum(st,7);
  6293. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6294. break;
  6295. default:
  6296. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6297. break;
  6298. }
  6299. return 0;
  6300. }
  6301. BUILDIN_FUNC(autobonus)
  6302. {
  6303. unsigned int dur;
  6304. short rate;
  6305. short atk_type = 0;
  6306. TBL_PC* sd;
  6307. const char *bonus_script, *other_script = NULL;
  6308. sd = script_rid2sd(st);
  6309. if( sd == NULL )
  6310. return 0; // no player attached
  6311. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6312. return 0;
  6313. rate = script_getnum(st,3);
  6314. dur = script_getnum(st,4);
  6315. bonus_script = script_getstr(st,2);
  6316. if( !rate || !dur || !bonus_script )
  6317. return 0;
  6318. if( script_hasdata(st,5) )
  6319. atk_type = script_getnum(st,5);
  6320. if( script_hasdata(st,6) )
  6321. other_script = script_getstr(st,6);
  6322. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  6323. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6324. {
  6325. script_add_autobonus(bonus_script);
  6326. if( other_script )
  6327. script_add_autobonus(other_script);
  6328. }
  6329. return 0;
  6330. }
  6331. BUILDIN_FUNC(autobonus2)
  6332. {
  6333. unsigned int dur;
  6334. short rate;
  6335. short atk_type = 0;
  6336. TBL_PC* sd;
  6337. const char *bonus_script, *other_script = NULL;
  6338. sd = script_rid2sd(st);
  6339. if( sd == NULL )
  6340. return 0; // no player attached
  6341. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6342. return 0;
  6343. rate = script_getnum(st,3);
  6344. dur = script_getnum(st,4);
  6345. bonus_script = script_getstr(st,2);
  6346. if( !rate || !dur || !bonus_script )
  6347. return 0;
  6348. if( script_hasdata(st,5) )
  6349. atk_type = script_getnum(st,5);
  6350. if( script_hasdata(st,6) )
  6351. other_script = script_getstr(st,6);
  6352. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  6353. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6354. {
  6355. script_add_autobonus(bonus_script);
  6356. if( other_script )
  6357. script_add_autobonus(other_script);
  6358. }
  6359. return 0;
  6360. }
  6361. BUILDIN_FUNC(autobonus3)
  6362. {
  6363. unsigned int dur;
  6364. short rate,atk_type;
  6365. TBL_PC* sd;
  6366. const char *bonus_script, *other_script = NULL;
  6367. sd = script_rid2sd(st);
  6368. if( sd == NULL )
  6369. return 0; // no player attached
  6370. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6371. return 0;
  6372. rate = script_getnum(st,3);
  6373. dur = script_getnum(st,4);
  6374. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6375. bonus_script = script_getstr(st,2);
  6376. if( !rate || !dur || !atk_type || !bonus_script )
  6377. return 0;
  6378. if( script_hasdata(st,6) )
  6379. other_script = script_getstr(st,6);
  6380. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  6381. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6382. {
  6383. script_add_autobonus(bonus_script);
  6384. if( other_script )
  6385. script_add_autobonus(other_script);
  6386. }
  6387. return 0;
  6388. }
  6389. /// Changes the level of a player skill.
  6390. /// <flag> defaults to 1
  6391. /// <flag>=0 : set the level of the skill
  6392. /// <flag>=1 : set the temporary level of the skill
  6393. /// <flag>=2 : add to the level of the skill
  6394. ///
  6395. /// skill <skill id>,<level>,<flag>
  6396. /// skill <skill id>,<level>
  6397. /// skill "<skill name>",<level>,<flag>
  6398. /// skill "<skill name>",<level>
  6399. BUILDIN_FUNC(skill)
  6400. {
  6401. int id;
  6402. int level;
  6403. int flag = 1;
  6404. TBL_PC* sd;
  6405. sd = script_rid2sd(st);
  6406. if( sd == NULL )
  6407. return 0;// no player attached, report source
  6408. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6409. level = script_getnum(st,3);
  6410. if( script_hasdata(st,4) )
  6411. flag = script_getnum(st,4);
  6412. pc_skill(sd, id, level, flag);
  6413. return 0;
  6414. }
  6415. /// Changes the level of a player skill.
  6416. /// like skill, but <flag> defaults to 2
  6417. ///
  6418. /// addtoskill <skill id>,<amount>,<flag>
  6419. /// addtoskill <skill id>,<amount>
  6420. /// addtoskill "<skill name>",<amount>,<flag>
  6421. /// addtoskill "<skill name>",<amount>
  6422. ///
  6423. /// @see skill
  6424. BUILDIN_FUNC(addtoskill)
  6425. {
  6426. int id;
  6427. int level;
  6428. int flag = 2;
  6429. TBL_PC* sd;
  6430. sd = script_rid2sd(st);
  6431. if( sd == NULL )
  6432. return 0;// no player attached, report source
  6433. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6434. level = script_getnum(st,3);
  6435. if( script_hasdata(st,4) )
  6436. flag = script_getnum(st,4);
  6437. pc_skill(sd, id, level, flag);
  6438. return 0;
  6439. }
  6440. /// Increases the level of a guild skill.
  6441. ///
  6442. /// guildskill <skill id>,<amount>;
  6443. /// guildskill "<skill name>",<amount>;
  6444. BUILDIN_FUNC(guildskill)
  6445. {
  6446. int id;
  6447. int level;
  6448. TBL_PC* sd;
  6449. int i;
  6450. sd = script_rid2sd(st);
  6451. if( sd == NULL )
  6452. return 0;// no player attached, report source
  6453. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6454. level = script_getnum(st,3);
  6455. for( i=0; i < level; i++ )
  6456. guild_skillup(sd, id);
  6457. return 0;
  6458. }
  6459. /// Returns the level of the player skill.
  6460. ///
  6461. /// getskilllv(<skill id>) -> <level>
  6462. /// getskilllv("<skill name>") -> <level>
  6463. BUILDIN_FUNC(getskilllv)
  6464. {
  6465. int id;
  6466. TBL_PC* sd;
  6467. sd = script_rid2sd(st);
  6468. if( sd == NULL )
  6469. return 0;// no player attached, report source
  6470. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6471. script_pushint(st, pc_checkskill(sd,id));
  6472. return 0;
  6473. }
  6474. /// Returns the level of the guild skill.
  6475. ///
  6476. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6477. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6478. BUILDIN_FUNC(getgdskilllv)
  6479. {
  6480. int guild_id;
  6481. int skill_id;
  6482. struct guild* g;
  6483. guild_id = script_getnum(st,2);
  6484. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6485. g = guild_search(guild_id);
  6486. if( g == NULL )
  6487. script_pushint(st, -1);
  6488. else
  6489. script_pushint(st, guild_checkskill(g,skill_id));
  6490. return 0;
  6491. }
  6492. /// Returns the 'basic_skill_check' setting.
  6493. /// This config determines if the server checks the skill level of NV_BASIC
  6494. /// before allowing the basic actions.
  6495. ///
  6496. /// basicskillcheck() -> <bool>
  6497. BUILDIN_FUNC(basicskillcheck)
  6498. {
  6499. script_pushint(st, battle_config.basic_skill_check);
  6500. return 0;
  6501. }
  6502. /// Returns the GM level of the player.
  6503. ///
  6504. /// getgmlevel() -> <level>
  6505. BUILDIN_FUNC(getgmlevel)
  6506. {
  6507. TBL_PC* sd;
  6508. sd = script_rid2sd(st);
  6509. if( sd == NULL )
  6510. return 0;// no player attached, report source
  6511. script_pushint(st, pc_isGM(sd));
  6512. return 0;
  6513. }
  6514. /// Terminates the execution of this script instance.
  6515. ///
  6516. /// end
  6517. BUILDIN_FUNC(end)
  6518. {
  6519. st->state = END;
  6520. return 0;
  6521. }
  6522. /// Checks if the player has that effect state (option).
  6523. ///
  6524. /// checkoption(<option>) -> <bool>
  6525. BUILDIN_FUNC(checkoption)
  6526. {
  6527. int option;
  6528. TBL_PC* sd;
  6529. sd = script_rid2sd(st);
  6530. if( sd == NULL )
  6531. return 0;// no player attached, report source
  6532. option = script_getnum(st,2);
  6533. if( sd->sc.option&option )
  6534. script_pushint(st, 1);
  6535. else
  6536. script_pushint(st, 0);
  6537. return 0;
  6538. }
  6539. /// Checks if the player is in that body state (opt1).
  6540. ///
  6541. /// checkoption1(<opt1>) -> <bool>
  6542. BUILDIN_FUNC(checkoption1)
  6543. {
  6544. int opt1;
  6545. TBL_PC* sd;
  6546. sd = script_rid2sd(st);
  6547. if( sd == NULL )
  6548. return 0;// no player attached, report source
  6549. opt1 = script_getnum(st,2);
  6550. if( sd->sc.opt1 == opt1 )
  6551. script_pushint(st, 1);
  6552. else
  6553. script_pushint(st, 0);
  6554. return 0;
  6555. }
  6556. /// Checks if the player has that health state (opt2).
  6557. ///
  6558. /// checkoption2(<opt2>) -> <bool>
  6559. BUILDIN_FUNC(checkoption2)
  6560. {
  6561. int opt2;
  6562. TBL_PC* sd;
  6563. sd = script_rid2sd(st);
  6564. if( sd == NULL )
  6565. return 0;// no player attached, report source
  6566. opt2 = script_getnum(st,2);
  6567. if( sd->sc.opt2&opt2 )
  6568. script_pushint(st, 1);
  6569. else
  6570. script_pushint(st, 0);
  6571. return 0;
  6572. }
  6573. /// Changes the effect state (option) of the player.
  6574. /// <flag> defaults to 1
  6575. /// <flag>=0 : removes the option
  6576. /// <flag>=other : adds the option
  6577. ///
  6578. /// setoption <option>,<flag>;
  6579. /// setoption <option>;
  6580. BUILDIN_FUNC(setoption)
  6581. {
  6582. int option;
  6583. int flag = 1;
  6584. TBL_PC* sd;
  6585. sd = script_rid2sd(st);
  6586. if( sd == NULL )
  6587. return 0;// no player attached, report source
  6588. option = script_getnum(st,2);
  6589. if( script_hasdata(st,3) )
  6590. flag = script_getnum(st,3);
  6591. else if( !option ){// Request to remove everything.
  6592. flag = 0;
  6593. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6594. }
  6595. if( flag ){// Add option
  6596. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6597. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6598. pc_setoption(sd, sd->sc.option|option);
  6599. } else// Remove option
  6600. pc_setoption(sd, sd->sc.option&~option);
  6601. return 0;
  6602. }
  6603. /// Returns if the player has a cart.
  6604. ///
  6605. /// checkcart() -> <bool>
  6606. ///
  6607. /// @author Valaris
  6608. BUILDIN_FUNC(checkcart)
  6609. {
  6610. TBL_PC* sd;
  6611. sd = script_rid2sd(st);
  6612. if( sd == NULL )
  6613. return 0;// no player attached, report source
  6614. if( pc_iscarton(sd) )
  6615. script_pushint(st, 1);
  6616. else
  6617. script_pushint(st, 0);
  6618. return 0;
  6619. }
  6620. /// Sets the cart of the player.
  6621. /// <type> defaults to 1
  6622. /// <type>=0 : removes the cart
  6623. /// <type>=1 : Normal cart
  6624. /// <type>=2 : Wooden cart
  6625. /// <type>=3 : Covered cart with flowers and ferns
  6626. /// <type>=4 : Wooden cart with a Panda doll on the back
  6627. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6628. ///
  6629. /// setcart <type>;
  6630. /// setcart;
  6631. BUILDIN_FUNC(setcart)
  6632. {
  6633. int type = 1;
  6634. TBL_PC* sd;
  6635. sd = script_rid2sd(st);
  6636. if( sd == NULL )
  6637. return 0;// no player attached, report source
  6638. if( script_hasdata(st,2) )
  6639. type = script_getnum(st,2);
  6640. pc_setcart(sd, type);
  6641. return 0;
  6642. }
  6643. /// Returns if the player has a falcon.
  6644. ///
  6645. /// checkfalcon() -> <bool>
  6646. ///
  6647. /// @author Valaris
  6648. BUILDIN_FUNC(checkfalcon)
  6649. {
  6650. TBL_PC* sd;
  6651. sd = script_rid2sd(st);
  6652. if( sd == NULL )
  6653. return 0;// no player attached, report source
  6654. if( pc_isfalcon(sd) )
  6655. script_pushint(st, 1);
  6656. else
  6657. script_pushint(st, 0);
  6658. return 0;
  6659. }
  6660. /// Sets if the player has a falcon or not.
  6661. /// <flag> defaults to 1
  6662. ///
  6663. /// setfalcon <flag>;
  6664. /// setfalcon;
  6665. BUILDIN_FUNC(setfalcon)
  6666. {
  6667. int flag = 1;
  6668. TBL_PC* sd;
  6669. sd = script_rid2sd(st);
  6670. if( sd == NULL )
  6671. return 0;// no player attached, report source
  6672. if( script_hasdata(st,2) )
  6673. flag = script_getnum(st,2);
  6674. pc_setfalcon(sd, flag);
  6675. return 0;
  6676. }
  6677. /// Returns if the player is riding.
  6678. ///
  6679. /// checkriding() -> <bool>
  6680. ///
  6681. /// @author Valaris
  6682. BUILDIN_FUNC(checkriding)
  6683. {
  6684. TBL_PC* sd;
  6685. sd = script_rid2sd(st);
  6686. if( sd == NULL )
  6687. return 0;// no player attached, report source
  6688. if( pc_isriding(sd) )
  6689. script_pushint(st, 1);
  6690. else
  6691. script_pushint(st, 0);
  6692. return 0;
  6693. }
  6694. /// Sets if the player is riding.
  6695. /// <flag> defaults to 1
  6696. ///
  6697. /// setriding <flag>;
  6698. /// setriding;
  6699. BUILDIN_FUNC(setriding)
  6700. {
  6701. int flag = 1;
  6702. TBL_PC* sd;
  6703. sd = script_rid2sd(st);
  6704. if( sd == NULL )
  6705. return 0;// no player attached, report source
  6706. if( script_hasdata(st,2) )
  6707. flag = script_getnum(st,2);
  6708. pc_setriding(sd, flag);
  6709. return 0;
  6710. }
  6711. /// Sets the save point of the player.
  6712. ///
  6713. /// save "<map name>",<x>,<y>
  6714. /// savepoint "<map name>",<x>,<y>
  6715. BUILDIN_FUNC(savepoint)
  6716. {
  6717. int x;
  6718. int y;
  6719. short map;
  6720. const char* str;
  6721. TBL_PC* sd;
  6722. sd = script_rid2sd(st);
  6723. if( sd == NULL )
  6724. return 0;// no player attached, report source
  6725. str = script_getstr(st, 2);
  6726. x = script_getnum(st,3);
  6727. y = script_getnum(st,4);
  6728. map = mapindex_name2id(str);
  6729. if( map )
  6730. pc_setsavepoint(sd, map, x, y);
  6731. return 0;
  6732. }
  6733. /*==========================================
  6734. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6735. *------------------------------------------*/
  6736. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6737. {
  6738. int type;
  6739. time_t timer;
  6740. struct tm *t;
  6741. type=script_getnum(st,2);
  6742. switch(type){
  6743. case 2:
  6744. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6745. // from the system clock.)
  6746. script_pushint(st,(int)time(NULL));
  6747. break;
  6748. case 1:
  6749. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6750. time(&timer);
  6751. t=localtime(&timer);
  6752. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6753. break;
  6754. case 0:
  6755. default:
  6756. //type 0:(System Ticks)
  6757. script_pushint(st,gettick());
  6758. break;
  6759. }
  6760. return 0;
  6761. }
  6762. /*==========================================
  6763. * GetTime(Type);
  6764. * 1: Sec 2: Min 3: Hour
  6765. * 4: WeekDay 5: MonthDay 6: Month
  6766. * 7: Year
  6767. *------------------------------------------*/
  6768. BUILDIN_FUNC(gettime) /* Asgard Version */
  6769. {
  6770. int type;
  6771. time_t timer;
  6772. struct tm *t;
  6773. type=script_getnum(st,2);
  6774. time(&timer);
  6775. t=localtime(&timer);
  6776. switch(type){
  6777. case 1://Sec(0~59)
  6778. script_pushint(st,t->tm_sec);
  6779. break;
  6780. case 2://Min(0~59)
  6781. script_pushint(st,t->tm_min);
  6782. break;
  6783. case 3://Hour(0~23)
  6784. script_pushint(st,t->tm_hour);
  6785. break;
  6786. case 4://WeekDay(0~6)
  6787. script_pushint(st,t->tm_wday);
  6788. break;
  6789. case 5://MonthDay(01~31)
  6790. script_pushint(st,t->tm_mday);
  6791. break;
  6792. case 6://Month(01~12)
  6793. script_pushint(st,t->tm_mon+1);
  6794. break;
  6795. case 7://Year(20xx)
  6796. script_pushint(st,t->tm_year+1900);
  6797. break;
  6798. case 8://Year Day(01~366)
  6799. script_pushint(st,t->tm_yday+1);
  6800. break;
  6801. default://(format error)
  6802. script_pushint(st,-1);
  6803. break;
  6804. }
  6805. return 0;
  6806. }
  6807. /*==========================================
  6808. * GetTimeStr("TimeFMT", Length);
  6809. *------------------------------------------*/
  6810. BUILDIN_FUNC(gettimestr)
  6811. {
  6812. char *tmpstr;
  6813. const char *fmtstr;
  6814. int maxlen;
  6815. time_t now = time(NULL);
  6816. fmtstr=script_getstr(st,2);
  6817. maxlen=script_getnum(st,3);
  6818. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6819. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6820. tmpstr[maxlen]='\0';
  6821. script_pushstr(st,tmpstr);
  6822. return 0;
  6823. }
  6824. /*==========================================
  6825. * カプラ倉庫を開く
  6826. *------------------------------------------*/
  6827. BUILDIN_FUNC(openstorage)
  6828. {
  6829. TBL_PC* sd;
  6830. sd = script_rid2sd(st);
  6831. if( sd == NULL )
  6832. return 0;
  6833. storage_storageopen(sd);
  6834. return 0;
  6835. }
  6836. BUILDIN_FUNC(guildopenstorage)
  6837. {
  6838. TBL_PC* sd;
  6839. int ret;
  6840. sd = script_rid2sd(st);
  6841. if( sd == NULL )
  6842. return 0;
  6843. ret = storage_guild_storageopen(sd);
  6844. script_pushint(st,ret);
  6845. return 0;
  6846. }
  6847. /*==========================================
  6848. * アイテムによるスキル発動
  6849. *------------------------------------------*/
  6850. /// itemskill <skill id>,<level>
  6851. /// itemskill "<skill name>",<level>
  6852. BUILDIN_FUNC(itemskill)
  6853. {
  6854. int id;
  6855. int lv;
  6856. TBL_PC* sd;
  6857. sd = script_rid2sd(st);
  6858. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6859. return 0;
  6860. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6861. lv = script_getnum(st,3);
  6862. sd->skillitem=id;
  6863. sd->skillitemlv=lv;
  6864. clif_item_skill(sd,id,lv);
  6865. return 0;
  6866. }
  6867. /*==========================================
  6868. * アイテム作成
  6869. *------------------------------------------*/
  6870. BUILDIN_FUNC(produce)
  6871. {
  6872. int trigger;
  6873. TBL_PC* sd;
  6874. sd = script_rid2sd(st);
  6875. if( sd == NULL )
  6876. return 0;
  6877. trigger=script_getnum(st,2);
  6878. clif_skill_produce_mix_list(sd, trigger);
  6879. return 0;
  6880. }
  6881. /*==========================================
  6882. *
  6883. *------------------------------------------*/
  6884. BUILDIN_FUNC(cooking)
  6885. {
  6886. int trigger;
  6887. TBL_PC* sd;
  6888. sd = script_rid2sd(st);
  6889. if( sd == NULL )
  6890. return 0;
  6891. trigger=script_getnum(st,2);
  6892. clif_cooking_list(sd, trigger);
  6893. return 0;
  6894. }
  6895. /*==========================================
  6896. * NPCでペット作る
  6897. *------------------------------------------*/
  6898. BUILDIN_FUNC(makepet)
  6899. {
  6900. TBL_PC* sd;
  6901. int id,pet_id;
  6902. id=script_getnum(st,2);
  6903. sd = script_rid2sd(st);
  6904. if( sd == NULL )
  6905. return 0;
  6906. pet_id = search_petDB_index(id, PET_CLASS);
  6907. if (pet_id < 0)
  6908. pet_id = search_petDB_index(id, PET_EGG);
  6909. if (pet_id >= 0 && sd) {
  6910. sd->catch_target_class = pet_db[pet_id].class_;
  6911. intif_create_pet(
  6912. sd->status.account_id, sd->status.char_id,
  6913. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6914. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6915. 100, 0, 1, pet_db[pet_id].jname);
  6916. }
  6917. return 0;
  6918. }
  6919. /*==========================================
  6920. * NPCで経験値上げる
  6921. *------------------------------------------*/
  6922. BUILDIN_FUNC(getexp)
  6923. {
  6924. TBL_PC* sd;
  6925. int base=0,job=0;
  6926. double bonus;
  6927. sd = script_rid2sd(st);
  6928. if( sd == NULL )
  6929. return 0;
  6930. base=script_getnum(st,2);
  6931. job =script_getnum(st,3);
  6932. if(base<0 || job<0)
  6933. return 0;
  6934. // bonus for npc-given exp
  6935. bonus = battle_config.quest_exp_rate / 100.;
  6936. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6937. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6938. pc_gainexp(sd, NULL, base, job, true);
  6939. return 0;
  6940. }
  6941. /*==========================================
  6942. * Gain guild exp [Celest]
  6943. *------------------------------------------*/
  6944. BUILDIN_FUNC(guildgetexp)
  6945. {
  6946. TBL_PC* sd;
  6947. int exp;
  6948. sd = script_rid2sd(st);
  6949. if( sd == NULL )
  6950. return 0;
  6951. exp = script_getnum(st,2);
  6952. if(exp < 0)
  6953. return 0;
  6954. if(sd && sd->status.guild_id > 0)
  6955. guild_getexp (sd, exp);
  6956. return 0;
  6957. }
  6958. /*==========================================
  6959. * Changes the guild master of a guild [Skotlex]
  6960. *------------------------------------------*/
  6961. BUILDIN_FUNC(guildchangegm)
  6962. {
  6963. TBL_PC *sd;
  6964. int guild_id;
  6965. const char *name;
  6966. guild_id = script_getnum(st,2);
  6967. name = script_getstr(st,3);
  6968. sd=map_nick2sd(name);
  6969. if (!sd)
  6970. script_pushint(st,0);
  6971. else
  6972. script_pushint(st,guild_gm_change(guild_id, sd));
  6973. return 0;
  6974. }
  6975. /*==========================================
  6976. * モンスター発生
  6977. *------------------------------------------*/
  6978. BUILDIN_FUNC(monster)
  6979. {
  6980. const char* mapn = script_getstr(st,2);
  6981. int x = script_getnum(st,3);
  6982. int y = script_getnum(st,4);
  6983. const char* str = script_getstr(st,5);
  6984. int class_ = script_getnum(st,6);
  6985. int amount = script_getnum(st,7);
  6986. const char* event = "";
  6987. struct map_session_data* sd;
  6988. int m;
  6989. if( script_hasdata(st,8) )
  6990. {
  6991. event = script_getstr(st,8);
  6992. check_event(st, event);
  6993. }
  6994. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6995. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6996. return 1;
  6997. }
  6998. sd = map_id2sd(st->rid);
  6999. if( sd && strcmp(mapn,"this") == 0 )
  7000. m = sd->bl.m;
  7001. else
  7002. {
  7003. m = map_mapname2mapid(mapn);
  7004. if( map[m].flag.src4instance && st->instance_id )
  7005. { // Try to redirect to the instance map, not the src map
  7006. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7007. {
  7008. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7009. return 1;
  7010. }
  7011. }
  7012. }
  7013. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  7014. return 0;
  7015. }
  7016. /*==========================================
  7017. * Request List of Monster Drops
  7018. *------------------------------------------*/
  7019. BUILDIN_FUNC(getmobdrops)
  7020. {
  7021. int class_ = script_getnum(st,2);
  7022. int i, j = 0;
  7023. struct mob_db *mob;
  7024. if( !mobdb_checkid(class_) )
  7025. {
  7026. script_pushint(st, 0);
  7027. return 0;
  7028. }
  7029. mob = mob_db(class_);
  7030. for( i = 0; i < MAX_MOB_DROP; i++ )
  7031. {
  7032. if( mob->dropitem[i].nameid < 1 )
  7033. continue;
  7034. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7035. continue;
  7036. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7037. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7038. j++;
  7039. }
  7040. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7041. script_pushint(st, 1);
  7042. return 0;
  7043. }
  7044. /*==========================================
  7045. * モンスター発生
  7046. *------------------------------------------*/
  7047. BUILDIN_FUNC(areamonster)
  7048. {
  7049. const char* mapn = script_getstr(st,2);
  7050. int x0 = script_getnum(st,3);
  7051. int y0 = script_getnum(st,4);
  7052. int x1 = script_getnum(st,5);
  7053. int y1 = script_getnum(st,6);
  7054. const char* str = script_getstr(st,7);
  7055. int class_ = script_getnum(st,8);
  7056. int amount = script_getnum(st,9);
  7057. const char* event = "";
  7058. struct map_session_data* sd;
  7059. int m;
  7060. if( script_hasdata(st,10) )
  7061. {
  7062. event = script_getstr(st,10);
  7063. check_event(st, event);
  7064. }
  7065. sd = map_id2sd(st->rid);
  7066. if( sd && strcmp(mapn,"this") == 0 )
  7067. m = sd->bl.m;
  7068. else
  7069. {
  7070. m = map_mapname2mapid(mapn);
  7071. if( map[m].flag.src4instance && st->instance_id )
  7072. { // Try to redirect to the instance map, not the src map
  7073. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7074. {
  7075. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7076. return 1;
  7077. }
  7078. }
  7079. }
  7080. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  7081. return 0;
  7082. }
  7083. /*==========================================
  7084. * モンスター削除
  7085. *------------------------------------------*/
  7086. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7087. { //same fix but with killmonster instead - stripping events from mobs.
  7088. TBL_MOB* md = (TBL_MOB*)bl;
  7089. char *event=va_arg(ap,char *);
  7090. int allflag=va_arg(ap,int);
  7091. md->state.npc_killmonster = 1;
  7092. if(!allflag){
  7093. if(strcmp(event,md->npc_event)==0)
  7094. status_kill(bl);
  7095. }else{
  7096. if(!md->spawn)
  7097. status_kill(bl);
  7098. }
  7099. md->state.npc_killmonster = 0;
  7100. return 0;
  7101. }
  7102. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7103. {
  7104. TBL_MOB* md = (TBL_MOB*)bl;
  7105. char *event=va_arg(ap,char *);
  7106. int allflag=va_arg(ap,int);
  7107. if(!allflag){
  7108. if(strcmp(event,md->npc_event)==0)
  7109. status_kill(bl);
  7110. }else{
  7111. if(!md->spawn)
  7112. status_kill(bl);
  7113. }
  7114. return 0;
  7115. }
  7116. BUILDIN_FUNC(killmonster)
  7117. {
  7118. const char *mapname,*event;
  7119. int m,allflag=0;
  7120. mapname=script_getstr(st,2);
  7121. event=script_getstr(st,3);
  7122. if(strcmp(event,"All")==0)
  7123. allflag = 1;
  7124. else
  7125. check_event(st, event);
  7126. if( (m=map_mapname2mapid(mapname))<0 )
  7127. return 0;
  7128. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7129. return 0;
  7130. if( script_hasdata(st,4) ) {
  7131. if ( script_getnum(st,4) == 1 ) {
  7132. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7133. return 0;
  7134. }
  7135. }
  7136. map_freeblock_lock();
  7137. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7138. map_freeblock_unlock();
  7139. return 0;
  7140. }
  7141. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7142. { //Strips the event from the mob if it's killed the old method.
  7143. struct mob_data *md;
  7144. md = BL_CAST(BL_MOB, bl);
  7145. if (md->npc_event[0])
  7146. md->npc_event[0] = 0;
  7147. status_kill(bl);
  7148. return 0;
  7149. }
  7150. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7151. {
  7152. status_kill(bl);
  7153. return 0;
  7154. }
  7155. BUILDIN_FUNC(killmonsterall)
  7156. {
  7157. const char *mapname;
  7158. int m;
  7159. mapname=script_getstr(st,2);
  7160. if( (m = map_mapname2mapid(mapname))<0 )
  7161. return 0;
  7162. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7163. return 0;
  7164. if( script_hasdata(st,3) ) {
  7165. if ( script_getnum(st,3) == 1 ) {
  7166. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7167. return 0;
  7168. }
  7169. }
  7170. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7171. return 0;
  7172. }
  7173. /*==========================================
  7174. * Creates a clone of a player.
  7175. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7176. *------------------------------------------*/
  7177. BUILDIN_FUNC(clone)
  7178. {
  7179. TBL_PC *sd, *msd=NULL;
  7180. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7181. unsigned int duration = 0;
  7182. const char *map,*event="";
  7183. map=script_getstr(st,2);
  7184. x=script_getnum(st,3);
  7185. y=script_getnum(st,4);
  7186. event=script_getstr(st,5);
  7187. char_id=script_getnum(st,6);
  7188. if( script_hasdata(st,7) )
  7189. master_id=script_getnum(st,7);
  7190. if( script_hasdata(st,8) )
  7191. mode=script_getnum(st,8);
  7192. if( script_hasdata(st,9) )
  7193. flag=script_getnum(st,9);
  7194. if( script_hasdata(st,10) )
  7195. duration=script_getnum(st,10);
  7196. check_event(st, event);
  7197. m = map_mapname2mapid(map);
  7198. if (m < 0) return 0;
  7199. sd = map_charid2sd(char_id);
  7200. if (master_id) {
  7201. msd = map_charid2sd(master_id);
  7202. if (msd)
  7203. master_id = msd->bl.id;
  7204. else
  7205. master_id = 0;
  7206. }
  7207. if (sd) //Return ID of newly crafted clone.
  7208. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7209. else //Failed to create clone.
  7210. script_pushint(st,0);
  7211. return 0;
  7212. }
  7213. /*==========================================
  7214. * イベント実行
  7215. *------------------------------------------*/
  7216. BUILDIN_FUNC(doevent)
  7217. {
  7218. const char* event = script_getstr(st,2);
  7219. struct map_session_data* sd;
  7220. if( ( sd = script_rid2sd(st) ) == NULL )
  7221. {
  7222. return 0;
  7223. }
  7224. check_event(st, event);
  7225. npc_event(sd, event, 0);
  7226. return 0;
  7227. }
  7228. /*==========================================
  7229. * NPC主体イベント実行
  7230. *------------------------------------------*/
  7231. BUILDIN_FUNC(donpcevent)
  7232. {
  7233. const char* event = script_getstr(st,2);
  7234. check_event(st, event);
  7235. npc_event_do(event);
  7236. return 0;
  7237. }
  7238. /// for Aegis compatibility
  7239. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  7240. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  7241. {
  7242. const char* npc = script_getstr(st,2);
  7243. const char* command = script_getstr(st,3);
  7244. char event[EVENT_NAME_LENGTH];
  7245. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  7246. check_event(st, event);
  7247. npc_event_do(event);
  7248. return 0;
  7249. }
  7250. /*==========================================
  7251. * イベントタイマー追加
  7252. *------------------------------------------*/
  7253. BUILDIN_FUNC(addtimer)
  7254. {
  7255. int tick = script_getnum(st,2);
  7256. const char* event = script_getstr(st, 3);
  7257. TBL_PC* sd;
  7258. check_event(st, event);
  7259. sd = script_rid2sd(st);
  7260. if( sd == NULL )
  7261. return 0;
  7262. pc_addeventtimer(sd,tick,event);
  7263. return 0;
  7264. }
  7265. /*==========================================
  7266. * イベントタイマー削除
  7267. *------------------------------------------*/
  7268. BUILDIN_FUNC(deltimer)
  7269. {
  7270. const char *event;
  7271. TBL_PC* sd;
  7272. event=script_getstr(st, 2);
  7273. sd = script_rid2sd(st);
  7274. if( sd == NULL )
  7275. return 0;
  7276. check_event(st, event);
  7277. pc_deleventtimer(sd,event);
  7278. return 0;
  7279. }
  7280. /*==========================================
  7281. * イベントタイマーのカウント値追加
  7282. *------------------------------------------*/
  7283. BUILDIN_FUNC(addtimercount)
  7284. {
  7285. const char *event;
  7286. int tick;
  7287. TBL_PC* sd;
  7288. event=script_getstr(st, 2);
  7289. tick=script_getnum(st,3);
  7290. sd = script_rid2sd(st);
  7291. if( sd == NULL )
  7292. return 0;
  7293. check_event(st, event);
  7294. pc_addeventtimercount(sd,event,tick);
  7295. return 0;
  7296. }
  7297. /*==========================================
  7298. * NPCタイマー初期化
  7299. *------------------------------------------*/
  7300. BUILDIN_FUNC(initnpctimer)
  7301. {
  7302. struct npc_data *nd;
  7303. int flag = 0;
  7304. if( script_hasdata(st,3) )
  7305. { //Two arguments: NPC name and attach flag.
  7306. nd = npc_name2id(script_getstr(st, 2));
  7307. flag = script_getnum(st,3);
  7308. }
  7309. else if( script_hasdata(st,2) )
  7310. { //Check if argument is numeric (flag) or string (npc name)
  7311. struct script_data *data;
  7312. data = script_getdata(st,2);
  7313. get_val(st,data);
  7314. if( data_isstring(data) ) //NPC name
  7315. nd = npc_name2id(conv_str(st, data));
  7316. else if( data_isint(data) ) //Flag
  7317. {
  7318. nd = (struct npc_data *)map_id2bl(st->oid);
  7319. flag = conv_num(st,data);
  7320. }
  7321. else
  7322. {
  7323. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7324. return 1;
  7325. }
  7326. }
  7327. else
  7328. nd = (struct npc_data *)map_id2bl(st->oid);
  7329. if( !nd )
  7330. return 0;
  7331. if( flag ) //Attach
  7332. {
  7333. TBL_PC* sd = script_rid2sd(st);
  7334. if( sd == NULL )
  7335. return 0;
  7336. nd->u.scr.rid = sd->bl.id;
  7337. }
  7338. npc_settimerevent_tick(nd,0);
  7339. npc_timerevent_start(nd, st->rid);
  7340. return 0;
  7341. }
  7342. /*==========================================
  7343. * NPCタイマー開始
  7344. *------------------------------------------*/
  7345. BUILDIN_FUNC(startnpctimer)
  7346. {
  7347. struct npc_data *nd;
  7348. int flag = 0;
  7349. if( script_hasdata(st,3) )
  7350. { //Two arguments: NPC name and attach flag.
  7351. nd = npc_name2id(script_getstr(st, 2));
  7352. flag = script_getnum(st,3);
  7353. }
  7354. else if( script_hasdata(st,2) )
  7355. { //Check if argument is numeric (flag) or string (npc name)
  7356. struct script_data *data;
  7357. data = script_getdata(st,2);
  7358. get_val(st,data);
  7359. if( data_isstring(data) ) //NPC name
  7360. nd = npc_name2id(conv_str(st, data));
  7361. else if( data_isint(data) ) //Flag
  7362. {
  7363. nd = (struct npc_data *)map_id2bl(st->oid);
  7364. flag = conv_num(st,data);
  7365. }
  7366. else
  7367. {
  7368. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7369. return 1;
  7370. }
  7371. }
  7372. else
  7373. nd=(struct npc_data *)map_id2bl(st->oid);
  7374. if( !nd )
  7375. return 0;
  7376. if( flag ) //Attach
  7377. {
  7378. TBL_PC* sd = script_rid2sd(st);
  7379. if( sd == NULL )
  7380. return 0;
  7381. nd->u.scr.rid = sd->bl.id;
  7382. }
  7383. npc_timerevent_start(nd, st->rid);
  7384. return 0;
  7385. }
  7386. /*==========================================
  7387. * NPCタイマー停止
  7388. *------------------------------------------*/
  7389. BUILDIN_FUNC(stopnpctimer)
  7390. {
  7391. struct npc_data *nd;
  7392. int flag = 0;
  7393. if( script_hasdata(st,3) )
  7394. { //Two arguments: NPC name and attach flag.
  7395. nd = npc_name2id(script_getstr(st, 2));
  7396. flag = script_getnum(st,3);
  7397. }
  7398. else if( script_hasdata(st,2) )
  7399. { //Check if argument is numeric (flag) or string (npc name)
  7400. struct script_data *data;
  7401. data = script_getdata(st,2);
  7402. get_val(st,data);
  7403. if( data_isstring(data) ) //NPC name
  7404. nd = npc_name2id(conv_str(st, data));
  7405. else if( data_isint(data) ) //Flag
  7406. {
  7407. nd = (struct npc_data *)map_id2bl(st->oid);
  7408. flag = conv_num(st,data);
  7409. }
  7410. else
  7411. {
  7412. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7413. return 1;
  7414. }
  7415. }
  7416. else
  7417. nd=(struct npc_data *)map_id2bl(st->oid);
  7418. if( !nd )
  7419. return 0;
  7420. if( flag ) //Detach
  7421. nd->u.scr.rid = 0;
  7422. npc_timerevent_stop(nd);
  7423. return 0;
  7424. }
  7425. /*==========================================
  7426. * NPCタイマー情報所得
  7427. *------------------------------------------*/
  7428. BUILDIN_FUNC(getnpctimer)
  7429. {
  7430. struct npc_data *nd;
  7431. TBL_PC *sd;
  7432. int type = script_getnum(st,2);
  7433. int val = 0;
  7434. if( script_hasdata(st,3) )
  7435. nd = npc_name2id(script_getstr(st,3));
  7436. else
  7437. nd = (struct npc_data *)map_id2bl(st->oid);
  7438. if( !nd || nd->bl.type != BL_NPC )
  7439. {
  7440. script_pushint(st,0);
  7441. ShowError("getnpctimer: Invalid NPC.\n");
  7442. return 1;
  7443. }
  7444. switch( type )
  7445. {
  7446. case 0: val = npc_gettimerevent_tick(nd); break;
  7447. case 1:
  7448. if( nd->u.scr.rid )
  7449. {
  7450. sd = map_id2sd(nd->u.scr.rid);
  7451. if( !sd )
  7452. {
  7453. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7454. break;
  7455. }
  7456. val = (sd->npc_timer_id != INVALID_TIMER);
  7457. }
  7458. else
  7459. val = (nd->u.scr.timerid != INVALID_TIMER);
  7460. break;
  7461. case 2: val = nd->u.scr.timeramount; break;
  7462. }
  7463. script_pushint(st,val);
  7464. return 0;
  7465. }
  7466. /*==========================================
  7467. * NPCタイマー値設定
  7468. *------------------------------------------*/
  7469. BUILDIN_FUNC(setnpctimer)
  7470. {
  7471. int tick;
  7472. struct npc_data *nd;
  7473. tick = script_getnum(st,2);
  7474. if( script_hasdata(st,3) )
  7475. nd = npc_name2id(script_getstr(st,3));
  7476. else
  7477. nd = (struct npc_data *)map_id2bl(st->oid);
  7478. if( !nd || nd->bl.type != BL_NPC )
  7479. {
  7480. script_pushint(st,1);
  7481. ShowError("setnpctimer: Invalid NPC.\n");
  7482. return 1;
  7483. }
  7484. npc_settimerevent_tick(nd,tick);
  7485. script_pushint(st,0);
  7486. return 0;
  7487. }
  7488. /*==========================================
  7489. * attaches the player rid to the timer [Celest]
  7490. *------------------------------------------*/
  7491. BUILDIN_FUNC(attachnpctimer)
  7492. {
  7493. TBL_PC *sd;
  7494. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7495. if( !nd || nd->bl.type != BL_NPC )
  7496. {
  7497. script_pushint(st,1);
  7498. ShowError("setnpctimer: Invalid NPC.\n");
  7499. return 1;
  7500. }
  7501. if( script_hasdata(st,2) )
  7502. sd = map_nick2sd(script_getstr(st,2));
  7503. else
  7504. sd = script_rid2sd(st);
  7505. if( !sd )
  7506. {
  7507. script_pushint(st,1);
  7508. ShowWarning("attachnpctimer: Invalid player.\n");
  7509. return 1;
  7510. }
  7511. nd->u.scr.rid = sd->bl.id;
  7512. script_pushint(st,0);
  7513. return 0;
  7514. }
  7515. /*==========================================
  7516. * detaches a player rid from the timer [Celest]
  7517. *------------------------------------------*/
  7518. BUILDIN_FUNC(detachnpctimer)
  7519. {
  7520. struct npc_data *nd;
  7521. if( script_hasdata(st,2) )
  7522. nd = npc_name2id(script_getstr(st,2));
  7523. else
  7524. nd = (struct npc_data *)map_id2bl(st->oid);
  7525. if( !nd || nd->bl.type != BL_NPC )
  7526. {
  7527. script_pushint(st,1);
  7528. ShowError("detachnpctimer: Invalid NPC.\n");
  7529. return 1;
  7530. }
  7531. nd->u.scr.rid = 0;
  7532. script_pushint(st,0);
  7533. return 0;
  7534. }
  7535. /*==========================================
  7536. * To avoid "player not attached" script errors, this function is provided,
  7537. * it checks if there is a player attached to the current script. [Skotlex]
  7538. * If no, returns 0, if yes, returns the account_id of the attached player.
  7539. *------------------------------------------*/
  7540. BUILDIN_FUNC(playerattached)
  7541. {
  7542. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7543. script_pushint(st,0);
  7544. else
  7545. script_pushint(st,st->rid);
  7546. return 0;
  7547. }
  7548. /*==========================================
  7549. * 天の声アナウンス
  7550. *------------------------------------------*/
  7551. BUILDIN_FUNC(announce)
  7552. {
  7553. const char *mes = script_getstr(st,2);
  7554. int flag = script_getnum(st,3);
  7555. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  7556. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  7557. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  7558. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  7559. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  7560. if (flag&0x0f) // Broadcast source or broadcast region defined
  7561. {
  7562. send_target target;
  7563. 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
  7564. if (bl == NULL)
  7565. return 0;
  7566. flag &= 0x07;
  7567. target = (flag == 1) ? ALL_SAMEMAP :
  7568. (flag == 2) ? AREA :
  7569. (flag == 3) ? SELF :
  7570. ALL_CLIENT;
  7571. if (fontColor)
  7572. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  7573. else
  7574. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  7575. }
  7576. else
  7577. {
  7578. if (fontColor)
  7579. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  7580. else
  7581. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  7582. }
  7583. return 0;
  7584. }
  7585. /*==========================================
  7586. * 天の声アナウンス(特定マップ)
  7587. *------------------------------------------*/
  7588. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  7589. {
  7590. char *mes = va_arg(ap, char *);
  7591. int len = va_arg(ap, int);
  7592. int type = va_arg(ap, int);
  7593. char *fontColor = va_arg(ap, char *);
  7594. short fontType = (short)va_arg(ap, int);
  7595. short fontSize = (short)va_arg(ap, int);
  7596. short fontAlign = (short)va_arg(ap, int);
  7597. short fontY = (short)va_arg(ap, int);
  7598. if (fontColor)
  7599. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  7600. else
  7601. clif_broadcast(bl, mes, len, type, SELF);
  7602. return 0;
  7603. }
  7604. BUILDIN_FUNC(mapannounce)
  7605. {
  7606. const char *mapname = script_getstr(st,2);
  7607. const char *mes = script_getstr(st,3);
  7608. int flag = script_getnum(st,4);
  7609. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  7610. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  7611. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  7612. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  7613. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  7614. int m;
  7615. if ((m = map_mapname2mapid(mapname)) < 0)
  7616. return 0;
  7617. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  7618. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7619. return 0;
  7620. }
  7621. /*==========================================
  7622. * 天の声アナウンス(特定エリア)
  7623. *------------------------------------------*/
  7624. BUILDIN_FUNC(areaannounce)
  7625. {
  7626. const char *mapname = script_getstr(st,2);
  7627. int x0 = script_getnum(st,3);
  7628. int y0 = script_getnum(st,4);
  7629. int x1 = script_getnum(st,5);
  7630. int y1 = script_getnum(st,6);
  7631. const char *mes = script_getstr(st,7);
  7632. int flag = script_getnum(st,8);
  7633. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  7634. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  7635. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  7636. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  7637. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  7638. int m;
  7639. if ((m = map_mapname2mapid(mapname)) < 0)
  7640. return 0;
  7641. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  7642. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  7643. return 0;
  7644. }
  7645. /*==========================================
  7646. * ユーザー数所得
  7647. *------------------------------------------*/
  7648. BUILDIN_FUNC(getusers)
  7649. {
  7650. int flag, val = 0;
  7651. struct map_session_data* sd;
  7652. struct block_list* bl = NULL;
  7653. flag = script_getnum(st,2);
  7654. switch(flag&0x07)
  7655. {
  7656. case 0:
  7657. if(flag&0x8)
  7658. {// npc
  7659. bl = map_id2bl(st->oid);
  7660. }
  7661. else if((sd = script_rid2sd(st))!=NULL)
  7662. {// pc
  7663. bl = &sd->bl;
  7664. }
  7665. if(bl)
  7666. {
  7667. val = map[bl->m].users;
  7668. }
  7669. break;
  7670. case 1:
  7671. val = map_getusers();
  7672. break;
  7673. default:
  7674. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  7675. script_pushint(st,0);
  7676. return 1;
  7677. }
  7678. script_pushint(st,val);
  7679. return 0;
  7680. }
  7681. /*==========================================
  7682. * Works like @WHO - displays all online users names in window
  7683. *------------------------------------------*/
  7684. BUILDIN_FUNC(getusersname)
  7685. {
  7686. TBL_PC *sd, *pl_sd;
  7687. int disp_num=1;
  7688. struct s_mapiterator* iter;
  7689. sd = script_rid2sd(st);
  7690. if (!sd) return 0;
  7691. iter = mapit_getallusers();
  7692. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7693. {
  7694. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7695. continue; // skip hidden GMs
  7696. if((disp_num++)%10==0)
  7697. clif_scriptnext(sd,st->oid);
  7698. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7699. }
  7700. mapit_free(iter);
  7701. return 0;
  7702. }
  7703. /*==========================================
  7704. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7705. *------------------------------------------*/
  7706. BUILDIN_FUNC(getmapguildusers)
  7707. {
  7708. const char *str;
  7709. int m, gid;
  7710. int i=0,c=0;
  7711. struct guild *g = NULL;
  7712. str=script_getstr(st,2);
  7713. gid=script_getnum(st,3);
  7714. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7715. script_pushint(st,-1);
  7716. return 0;
  7717. }
  7718. g = guild_search(gid);
  7719. if (g){
  7720. for(i = 0; i < g->max_member; i++)
  7721. {
  7722. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7723. c++;
  7724. }
  7725. }
  7726. script_pushint(st,c);
  7727. return 0;
  7728. }
  7729. /*==========================================
  7730. * マップ指定ユーザー数所得
  7731. *------------------------------------------*/
  7732. BUILDIN_FUNC(getmapusers)
  7733. {
  7734. const char *str;
  7735. int m;
  7736. str=script_getstr(st,2);
  7737. if( (m=map_mapname2mapid(str))< 0){
  7738. script_pushint(st,-1);
  7739. return 0;
  7740. }
  7741. script_pushint(st,map[m].users);
  7742. return 0;
  7743. }
  7744. /*==========================================
  7745. * エリア指定ユーザー数所得
  7746. *------------------------------------------*/
  7747. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7748. {
  7749. int *users=va_arg(ap,int *);
  7750. (*users)++;
  7751. return 0;
  7752. }
  7753. BUILDIN_FUNC(getareausers)
  7754. {
  7755. const char *str;
  7756. int m,x0,y0,x1,y1,users=0;
  7757. str=script_getstr(st,2);
  7758. x0=script_getnum(st,3);
  7759. y0=script_getnum(st,4);
  7760. x1=script_getnum(st,5);
  7761. y1=script_getnum(st,6);
  7762. if( (m=map_mapname2mapid(str))< 0){
  7763. script_pushint(st,-1);
  7764. return 0;
  7765. }
  7766. map_foreachinarea(buildin_getareausers_sub,
  7767. m,x0,y0,x1,y1,BL_PC,&users);
  7768. script_pushint(st,users);
  7769. return 0;
  7770. }
  7771. /*==========================================
  7772. * エリア指定ドロップアイテム数所得
  7773. *------------------------------------------*/
  7774. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7775. {
  7776. int item=va_arg(ap,int);
  7777. int *amount=va_arg(ap,int *);
  7778. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7779. if(drop->item_data.nameid==item)
  7780. (*amount)+=drop->item_data.amount;
  7781. return 0;
  7782. }
  7783. BUILDIN_FUNC(getareadropitem)
  7784. {
  7785. const char *str;
  7786. int m,x0,y0,x1,y1,item,amount=0;
  7787. struct script_data *data;
  7788. str=script_getstr(st,2);
  7789. x0=script_getnum(st,3);
  7790. y0=script_getnum(st,4);
  7791. x1=script_getnum(st,5);
  7792. y1=script_getnum(st,6);
  7793. data=script_getdata(st,7);
  7794. get_val(st,data);
  7795. if( data_isstring(data) ){
  7796. const char *name=conv_str(st,data);
  7797. struct item_data *item_data = itemdb_searchname(name);
  7798. item=UNKNOWN_ITEM_ID;
  7799. if( item_data )
  7800. item=item_data->nameid;
  7801. }else
  7802. item=conv_num(st,data);
  7803. if( (m=map_mapname2mapid(str))< 0){
  7804. script_pushint(st,-1);
  7805. return 0;
  7806. }
  7807. map_foreachinarea(buildin_getareadropitem_sub,
  7808. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7809. script_pushint(st,amount);
  7810. return 0;
  7811. }
  7812. /*==========================================
  7813. * NPCの有効化
  7814. *------------------------------------------*/
  7815. BUILDIN_FUNC(enablenpc)
  7816. {
  7817. const char *str;
  7818. str=script_getstr(st,2);
  7819. npc_enable(str,1);
  7820. return 0;
  7821. }
  7822. /*==========================================
  7823. * NPCの無効化
  7824. *------------------------------------------*/
  7825. BUILDIN_FUNC(disablenpc)
  7826. {
  7827. const char *str;
  7828. str=script_getstr(st,2);
  7829. npc_enable(str,0);
  7830. return 0;
  7831. }
  7832. /*==========================================
  7833. * 隠れているNPCの表示
  7834. *------------------------------------------*/
  7835. BUILDIN_FUNC(hideoffnpc)
  7836. {
  7837. const char *str;
  7838. str=script_getstr(st,2);
  7839. npc_enable(str,2);
  7840. return 0;
  7841. }
  7842. /*==========================================
  7843. * NPCをハイディング
  7844. *------------------------------------------*/
  7845. BUILDIN_FUNC(hideonnpc)
  7846. {
  7847. const char *str;
  7848. str=script_getstr(st,2);
  7849. npc_enable(str,4);
  7850. return 0;
  7851. }
  7852. /// Starts a status effect on the target unit or on the attached player.
  7853. ///
  7854. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7855. BUILDIN_FUNC(sc_start)
  7856. {
  7857. struct block_list* bl;
  7858. enum sc_type type;
  7859. int tick;
  7860. int val1;
  7861. int val4 = 0;
  7862. type = (sc_type)script_getnum(st,2);
  7863. tick = script_getnum(st,3);
  7864. val1 = script_getnum(st,4);
  7865. if( script_hasdata(st,5) )
  7866. bl = map_id2bl(script_getnum(st,5));
  7867. else
  7868. bl = map_id2bl(st->rid);
  7869. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7870. {// When there isn't a duration specified, try to get it from the skill_db
  7871. tick = skill_get_time(status_sc2skill(type), val1);
  7872. }
  7873. if( potion_flag == 1 && potion_target )
  7874. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7875. bl = map_id2bl(potion_target);
  7876. tick /= 2;// Thrown potions only last half.
  7877. val4 = 1;// Mark that this was a thrown sc_effect
  7878. }
  7879. if( bl )
  7880. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7881. return 0;
  7882. }
  7883. /// Starts a status effect on the target unit or on the attached player.
  7884. ///
  7885. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7886. BUILDIN_FUNC(sc_start2)
  7887. {
  7888. struct block_list* bl;
  7889. enum sc_type type;
  7890. int tick;
  7891. int val1;
  7892. int val4 = 0;
  7893. int rate;
  7894. type = (sc_type)script_getnum(st,2);
  7895. tick = script_getnum(st,3);
  7896. val1 = script_getnum(st,4);
  7897. rate = script_getnum(st,5);
  7898. if( script_hasdata(st,6) )
  7899. bl = map_id2bl(script_getnum(st,6));
  7900. else
  7901. bl = map_id2bl(st->rid);
  7902. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7903. {// When there isn't a duration specified, try to get it from the skill_db
  7904. tick = skill_get_time(status_sc2skill(type), val1);
  7905. }
  7906. if( potion_flag == 1 && potion_target )
  7907. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7908. bl = map_id2bl(potion_target);
  7909. tick /= 2;// Thrown potions only last half.
  7910. val4 = 1;// Mark that this was a thrown sc_effect
  7911. }
  7912. if( bl )
  7913. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7914. return 0;
  7915. }
  7916. /// Starts a status effect on the target unit or on the attached player.
  7917. ///
  7918. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7919. BUILDIN_FUNC(sc_start4)
  7920. {
  7921. struct block_list* bl;
  7922. enum sc_type type;
  7923. int tick;
  7924. int val1;
  7925. int val2;
  7926. int val3;
  7927. int val4;
  7928. type = (sc_type)script_getnum(st,2);
  7929. tick = script_getnum(st,3);
  7930. val1 = script_getnum(st,4);
  7931. val2 = script_getnum(st,5);
  7932. val3 = script_getnum(st,6);
  7933. val4 = script_getnum(st,7);
  7934. if( script_hasdata(st,8) )
  7935. bl = map_id2bl(script_getnum(st,8));
  7936. else
  7937. bl = map_id2bl(st->rid);
  7938. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7939. {// When there isn't a duration specified, try to get it from the skill_db
  7940. tick = skill_get_time(status_sc2skill(type), val1);
  7941. }
  7942. if( potion_flag == 1 && potion_target )
  7943. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7944. bl = map_id2bl(potion_target);
  7945. tick /= 2;// Thrown potions only last half.
  7946. }
  7947. if( bl )
  7948. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7949. return 0;
  7950. }
  7951. /// Ends one or all status effects on the target unit or on the attached player.
  7952. ///
  7953. /// sc_end <effect_id>{,<unit_id>};
  7954. BUILDIN_FUNC(sc_end)
  7955. {
  7956. struct block_list* bl;
  7957. int type;
  7958. type = script_getnum(st,2);
  7959. if( script_hasdata(st,3) )
  7960. bl = map_id2bl(script_getnum(st,3));
  7961. else
  7962. bl = map_id2bl(st->rid);
  7963. if( potion_flag==1 && potion_target )
  7964. {//##TODO how does this work [FlavioJS]
  7965. bl = map_id2bl(potion_target);
  7966. }
  7967. if( !bl ) return 0;
  7968. if( type >= 0 && type < SC_MAX )
  7969. {
  7970. struct status_change *sc = status_get_sc(bl);
  7971. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7972. if (!sce) return 0;
  7973. //This should help status_change_end force disabling the SC in case it has no limit.
  7974. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7975. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7976. } else
  7977. status_change_clear(bl, 2);// remove all effects
  7978. return 0;
  7979. }
  7980. /*==========================================
  7981. * 状態異常耐性を計算した確率を返す
  7982. *------------------------------------------*/
  7983. BUILDIN_FUNC(getscrate)
  7984. {
  7985. struct block_list *bl;
  7986. int type,rate;
  7987. type=script_getnum(st,2);
  7988. rate=script_getnum(st,3);
  7989. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7990. bl = map_id2bl(script_getnum(st,4));
  7991. else
  7992. bl = map_id2bl(st->rid);
  7993. if (bl)
  7994. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7995. script_pushint(st,rate);
  7996. return 0;
  7997. }
  7998. /*==========================================
  7999. *
  8000. *------------------------------------------*/
  8001. BUILDIN_FUNC(debugmes)
  8002. {
  8003. const char *str;
  8004. str=script_getstr(st,2);
  8005. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8006. return 0;
  8007. }
  8008. /*==========================================
  8009. *捕獲アイテム使用
  8010. *------------------------------------------*/
  8011. BUILDIN_FUNC(catchpet)
  8012. {
  8013. int pet_id;
  8014. TBL_PC *sd;
  8015. pet_id= script_getnum(st,2);
  8016. sd=script_rid2sd(st);
  8017. if( sd == NULL )
  8018. return 0;
  8019. pet_catch_process1(sd,pet_id);
  8020. return 0;
  8021. }
  8022. /*==========================================
  8023. * [orn]
  8024. *------------------------------------------*/
  8025. BUILDIN_FUNC(homunculus_evolution)
  8026. {
  8027. TBL_PC *sd;
  8028. sd=script_rid2sd(st);
  8029. if( sd == NULL )
  8030. return 0;
  8031. if(merc_is_hom_active(sd->hd))
  8032. {
  8033. if (sd->hd->homunculus.intimacy > 91000)
  8034. merc_hom_evolution(sd->hd);
  8035. else
  8036. clif_emotion(&sd->hd->bl, E_SWT);
  8037. }
  8038. return 0;
  8039. }
  8040. // [Zephyrus]
  8041. BUILDIN_FUNC(homunculus_shuffle)
  8042. {
  8043. TBL_PC *sd;
  8044. sd=script_rid2sd(st);
  8045. if( sd == NULL )
  8046. return 0;
  8047. if(merc_is_hom_active(sd->hd))
  8048. merc_hom_shuffle(sd->hd);
  8049. return 0;
  8050. }
  8051. //These two functions bring the eA MAPID_* class functionality to scripts.
  8052. BUILDIN_FUNC(eaclass)
  8053. {
  8054. int class_;
  8055. if( script_hasdata(st,2) )
  8056. class_ = script_getnum(st,2);
  8057. else {
  8058. TBL_PC *sd;
  8059. sd=script_rid2sd(st);
  8060. if (!sd) {
  8061. script_pushint(st,-1);
  8062. return 0;
  8063. }
  8064. class_ = sd->status.class_;
  8065. }
  8066. script_pushint(st,pc_jobid2mapid(class_));
  8067. return 0;
  8068. }
  8069. BUILDIN_FUNC(roclass)
  8070. {
  8071. int class_ =script_getnum(st,2);
  8072. int sex;
  8073. if( script_hasdata(st,3) )
  8074. sex = script_getnum(st,3);
  8075. else {
  8076. TBL_PC *sd;
  8077. if (st->rid && (sd=script_rid2sd(st)))
  8078. sex = sd->status.sex;
  8079. else
  8080. sex = 1; //Just use male when not found.
  8081. }
  8082. script_pushint(st,pc_mapid2jobid(class_, sex));
  8083. return 0;
  8084. }
  8085. /*==========================================
  8086. *携帯卵孵化機使用
  8087. *------------------------------------------*/
  8088. BUILDIN_FUNC(birthpet)
  8089. {
  8090. TBL_PC *sd;
  8091. sd=script_rid2sd(st);
  8092. if( sd == NULL )
  8093. return 0;
  8094. if( sd->status.pet_id )
  8095. {// do not send egg list, when you already have a pet
  8096. return 0;
  8097. }
  8098. clif_sendegg(sd);
  8099. return 0;
  8100. }
  8101. /*==========================================
  8102. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8103. *------------------------------------------*/
  8104. BUILDIN_FUNC(resetlvl)
  8105. {
  8106. TBL_PC *sd;
  8107. int type=script_getnum(st,2);
  8108. sd=script_rid2sd(st);
  8109. if( sd == NULL )
  8110. return 0;
  8111. pc_resetlvl(sd,type);
  8112. return 0;
  8113. }
  8114. /*==========================================
  8115. * ステータスリセット
  8116. *------------------------------------------*/
  8117. BUILDIN_FUNC(resetstatus)
  8118. {
  8119. TBL_PC *sd;
  8120. sd=script_rid2sd(st);
  8121. pc_resetstate(sd);
  8122. return 0;
  8123. }
  8124. /*==========================================
  8125. * script command resetskill
  8126. *------------------------------------------*/
  8127. BUILDIN_FUNC(resetskill)
  8128. {
  8129. TBL_PC *sd;
  8130. sd=script_rid2sd(st);
  8131. pc_resetskill(sd,1);
  8132. return 0;
  8133. }
  8134. /*==========================================
  8135. * Counts total amount of skill points.
  8136. *------------------------------------------*/
  8137. BUILDIN_FUNC(skillpointcount)
  8138. {
  8139. TBL_PC *sd;
  8140. sd=script_rid2sd(st);
  8141. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8142. return 0;
  8143. }
  8144. /*==========================================
  8145. *
  8146. *------------------------------------------*/
  8147. BUILDIN_FUNC(changebase)
  8148. {
  8149. TBL_PC *sd=NULL;
  8150. int vclass;
  8151. if( script_hasdata(st,3) )
  8152. sd=map_id2sd(script_getnum(st,3));
  8153. else
  8154. sd=script_rid2sd(st);
  8155. if(sd == NULL)
  8156. return 0;
  8157. vclass = script_getnum(st,2);
  8158. if(vclass == JOB_WEDDING)
  8159. {
  8160. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8161. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8162. )
  8163. return 0;
  8164. }
  8165. if(!sd->disguise && vclass != sd->vd.class_) {
  8166. status_set_viewdata(&sd->bl, vclass);
  8167. //Updated client view. Base, Weapon and Cloth Colors.
  8168. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8169. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8170. if (sd->vd.cloth_color)
  8171. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8172. clif_skillinfoblock(sd);
  8173. }
  8174. return 0;
  8175. }
  8176. /*==========================================
  8177. * 性別変換
  8178. *------------------------------------------*/
  8179. BUILDIN_FUNC(changesex)
  8180. {
  8181. TBL_PC *sd = NULL;
  8182. sd = script_rid2sd(st);
  8183. chrif_changesex(sd);
  8184. return 0;
  8185. }
  8186. /*==========================================
  8187. * Works like 'announce' but outputs in the common chat window
  8188. *------------------------------------------*/
  8189. BUILDIN_FUNC(globalmes)
  8190. {
  8191. struct block_list *bl = map_id2bl(st->oid);
  8192. struct npc_data *nd = (struct npc_data *)bl;
  8193. const char *name=NULL,*mes;
  8194. mes=script_getstr(st,2); // メッセージの取得
  8195. if(mes==NULL) return 0;
  8196. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8197. name=script_getstr(st,3);
  8198. } else {
  8199. name=nd->name;
  8200. }
  8201. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8202. return 0;
  8203. }
  8204. /////////////////////////////////////////////////////////////////////
  8205. // NPC waiting room (chat room)
  8206. //
  8207. /// Creates a waiting room (chat room) for this npc.
  8208. ///
  8209. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  8210. BUILDIN_FUNC(waitingroom)
  8211. {
  8212. struct npc_data* nd;
  8213. int pub = 1;
  8214. const char* title = script_getstr(st, 2);
  8215. int limit = script_getnum(st, 3);
  8216. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  8217. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  8218. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  8219. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  8220. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  8221. nd = (struct npc_data *)map_id2bl(st->oid);
  8222. if( nd != NULL )
  8223. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  8224. return 0;
  8225. }
  8226. /// Removes the waiting room of the current or target npc.
  8227. ///
  8228. /// delwaitingroom "<npc_name>";
  8229. /// delwaitingroom;
  8230. BUILDIN_FUNC(delwaitingroom)
  8231. {
  8232. struct npc_data* nd;
  8233. if( script_hasdata(st,2) )
  8234. nd = npc_name2id(script_getstr(st, 2));
  8235. else
  8236. nd = (struct npc_data *)map_id2bl(st->oid);
  8237. if( nd != NULL )
  8238. chat_deletenpcchat(nd);
  8239. return 0;
  8240. }
  8241. /// Kicks all the players from the waiting room of the current or target npc.
  8242. ///
  8243. /// kickwaitingroomall "<npc_name>";
  8244. /// kickwaitingroomall;
  8245. BUILDIN_FUNC(waitingroomkickall)
  8246. {
  8247. struct npc_data* nd;
  8248. struct chat_data* cd;
  8249. if( script_hasdata(st,2) )
  8250. nd = npc_name2id(script_getstr(st,2));
  8251. else
  8252. nd = (struct npc_data *)map_id2bl(st->oid);
  8253. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8254. chat_npckickall(cd);
  8255. return 0;
  8256. }
  8257. /// Enables the waiting room event of the current or target npc.
  8258. ///
  8259. /// enablewaitingroomevent "<npc_name>";
  8260. /// enablewaitingroomevent;
  8261. BUILDIN_FUNC(enablewaitingroomevent)
  8262. {
  8263. struct npc_data* nd;
  8264. struct chat_data* cd;
  8265. if( script_hasdata(st,2) )
  8266. nd = npc_name2id(script_getstr(st, 2));
  8267. else
  8268. nd = (struct npc_data *)map_id2bl(st->oid);
  8269. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8270. chat_enableevent(cd);
  8271. return 0;
  8272. }
  8273. /// Disables the waiting room event of the current or target npc.
  8274. ///
  8275. /// disablewaitingroomevent "<npc_name>";
  8276. /// disablewaitingroomevent;
  8277. BUILDIN_FUNC(disablewaitingroomevent)
  8278. {
  8279. struct npc_data *nd;
  8280. struct chat_data *cd;
  8281. if( script_hasdata(st,2) )
  8282. nd = npc_name2id(script_getstr(st, 2));
  8283. else
  8284. nd = (struct npc_data *)map_id2bl(st->oid);
  8285. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8286. chat_disableevent(cd);
  8287. return 0;
  8288. }
  8289. /// Returns info on the waiting room of the current or target npc.
  8290. /// Returns -1 if the type unknown
  8291. /// <type>=0 : current number of users
  8292. /// <type>=1 : maximum number of users allowed
  8293. /// <type>=2 : the number of users that trigger the event
  8294. /// <type>=3 : if the trigger is disabled
  8295. /// <type>=4 : the title of the waiting room
  8296. /// <type>=5 : the password of the waiting room
  8297. /// <type>=16 : the name of the waiting room event
  8298. /// <type>=32 : if the waiting room is full
  8299. /// <type>=33 : if there are enough users to trigger the event
  8300. ///
  8301. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8302. /// getwaitingroomstate(<type>) -> <info>
  8303. BUILDIN_FUNC(getwaitingroomstate)
  8304. {
  8305. struct npc_data *nd;
  8306. struct chat_data *cd;
  8307. int type;
  8308. type = script_getnum(st,2);
  8309. if( script_hasdata(st,3) )
  8310. nd = npc_name2id(script_getstr(st, 3));
  8311. else
  8312. nd = (struct npc_data *)map_id2bl(st->oid);
  8313. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8314. {
  8315. script_pushint(st, -1);
  8316. return 0;
  8317. }
  8318. switch(type)
  8319. {
  8320. case 0: script_pushint(st, cd->users); break;
  8321. case 1: script_pushint(st, cd->limit); break;
  8322. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8323. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8324. case 4: script_pushstrcopy(st, cd->title); break;
  8325. case 5: script_pushstrcopy(st, cd->pass); break;
  8326. case 16: script_pushstrcopy(st, cd->npc_event);break;
  8327. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8328. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8329. default: script_pushint(st, -1); break;
  8330. }
  8331. return 0;
  8332. }
  8333. /// Warps the trigger or target amount of players to the target map and position.
  8334. /// Players are automatically removed from the waiting room.
  8335. /// Those waiting the longest will get warped first.
  8336. /// The target map can be "Random" for a random position in the current map,
  8337. /// and "SavePoint" for the savepoint map+position.
  8338. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8339. ///
  8340. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8341. /// The total number of teleported players is put into $@warpwaitingpcnum
  8342. ///
  8343. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8344. /// warpwaitingpc "<map name>",<x>,<y>;
  8345. BUILDIN_FUNC(warpwaitingpc)
  8346. {
  8347. int x;
  8348. int y;
  8349. int i;
  8350. int n;
  8351. const char* map_name;
  8352. struct npc_data* nd;
  8353. struct chat_data* cd;
  8354. TBL_PC* sd;
  8355. nd = (struct npc_data *)map_id2bl(st->oid);
  8356. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8357. return 0;
  8358. map_name = script_getstr(st,2);
  8359. x = script_getnum(st,3);
  8360. y = script_getnum(st,4);
  8361. n = cd->trigger&0x7f;
  8362. if( script_hasdata(st,5) )
  8363. n = script_getnum(st,5);
  8364. for( i = 0; i < n && cd->users > 0; i++ )
  8365. {
  8366. sd = cd->usersd[0];
  8367. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  8368. {// can't teleport on this map
  8369. break;
  8370. }
  8371. if( cd->zeny )
  8372. {// fee set
  8373. if( (uint32)sd->status.zeny < cd->zeny )
  8374. {// no zeny to cover set fee
  8375. break;
  8376. }
  8377. pc_payzeny(sd, cd->zeny);
  8378. }
  8379. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  8380. if( strcmp(map_name,"Random") == 0 )
  8381. pc_randomwarp(sd,CLR_TELEPORT);
  8382. else if( strcmp(map_name,"SavePoint") == 0 )
  8383. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  8384. else
  8385. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  8386. }
  8387. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8388. return 0;
  8389. }
  8390. /////////////////////////////////////////////////////////////////////
  8391. // ...
  8392. //
  8393. /// Detaches a character from a script.
  8394. ///
  8395. /// @param st Script state to detach the character from.
  8396. static void script_detach_rid(struct script_state* st)
  8397. {
  8398. if(st->rid)
  8399. {
  8400. script_detach_state(st, false);
  8401. st->rid = 0;
  8402. }
  8403. }
  8404. /*==========================================
  8405. * RIDのアタッチ
  8406. *------------------------------------------*/
  8407. BUILDIN_FUNC(attachrid)
  8408. {
  8409. int rid = script_getnum(st,2);
  8410. struct map_session_data* sd;
  8411. if ((sd = map_id2sd(rid))!=NULL) {
  8412. script_detach_rid(st);
  8413. st->rid = rid;
  8414. script_attach_state(st);
  8415. script_pushint(st,1);
  8416. } else
  8417. script_pushint(st,0);
  8418. return 0;
  8419. }
  8420. /*==========================================
  8421. * RIDのデタッチ
  8422. *------------------------------------------*/
  8423. BUILDIN_FUNC(detachrid)
  8424. {
  8425. script_detach_rid(st);
  8426. return 0;
  8427. }
  8428. /*==========================================
  8429. * 存在チェック
  8430. *------------------------------------------*/
  8431. BUILDIN_FUNC(isloggedin)
  8432. {
  8433. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8434. if (script_hasdata(st,3) && sd &&
  8435. sd->status.char_id != script_getnum(st,3))
  8436. sd = NULL;
  8437. push_val(st->stack,C_INT,sd!=NULL);
  8438. return 0;
  8439. }
  8440. /*==========================================
  8441. *
  8442. *------------------------------------------*/
  8443. BUILDIN_FUNC(setmapflagnosave)
  8444. {
  8445. int m,x,y;
  8446. unsigned short mapindex;
  8447. const char *str,*str2;
  8448. str=script_getstr(st,2);
  8449. str2=script_getstr(st,3);
  8450. x=script_getnum(st,4);
  8451. y=script_getnum(st,5);
  8452. m = map_mapname2mapid(str);
  8453. mapindex = mapindex_name2id(str2);
  8454. if(m >= 0 && mapindex) {
  8455. map[m].flag.nosave=1;
  8456. map[m].save.map=mapindex;
  8457. map[m].save.x=x;
  8458. map[m].save.y=y;
  8459. }
  8460. return 0;
  8461. }
  8462. BUILDIN_FUNC(getmapflag)
  8463. {
  8464. int m,i;
  8465. const char *str;
  8466. str=script_getstr(st,2);
  8467. i=script_getnum(st,3);
  8468. m = map_mapname2mapid(str);
  8469. if(m >= 0) {
  8470. switch(i) {
  8471. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8472. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8473. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8474. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8475. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8476. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8477. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8478. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8479. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8480. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8481. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8482. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8483. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8484. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8485. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8486. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8487. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8488. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8489. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8490. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8491. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8492. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8493. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8494. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8495. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8496. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8497. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8498. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8499. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8500. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8501. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8502. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8503. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8504. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8505. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8506. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8507. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8508. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8509. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8510. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8511. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8512. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8513. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8514. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  8515. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  8516. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  8517. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  8518. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  8519. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  8520. }
  8521. }
  8522. return 0;
  8523. }
  8524. BUILDIN_FUNC(setmapflag)
  8525. {
  8526. int m,i;
  8527. const char *str;
  8528. const char *val=NULL;
  8529. str=script_getstr(st,2);
  8530. i=script_getnum(st,3);
  8531. if(script_hasdata(st,4)){
  8532. val=script_getstr(st,4);
  8533. }
  8534. m = map_mapname2mapid(str);
  8535. if(m >= 0) {
  8536. switch(i) {
  8537. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  8538. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  8539. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  8540. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  8541. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  8542. case MF_PVP: map[m].flag.pvp=1; break;
  8543. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  8544. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  8545. case MF_GVG: map[m].flag.gvg=1; break;
  8546. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  8547. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  8548. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  8549. case MF_NOTRADE: map[m].flag.notrade=1; break;
  8550. case MF_NODROP: map[m].flag.nodrop=1; break;
  8551. case MF_NOSKILL: map[m].flag.noskill=1; break;
  8552. case MF_NOWARP: map[m].flag.nowarp=1; break;
  8553. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  8554. case MF_SNOW: map[m].flag.snow=1; break;
  8555. case MF_CLOUDS: map[m].flag.clouds=1; break;
  8556. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  8557. case MF_FOG: map[m].flag.fog=1; break;
  8558. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  8559. case MF_SAKURA: map[m].flag.sakura=1; break;
  8560. case MF_LEAVES: map[m].flag.leaves=1; break;
  8561. case MF_RAIN: map[m].flag.rain=1; break;
  8562. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  8563. case MF_NOGO: map[m].flag.nogo=1; break;
  8564. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  8565. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  8566. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  8567. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  8568. case MF_NORETURN: map[m].flag.noreturn=1; break;
  8569. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  8570. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  8571. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  8572. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8573. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8574. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8575. case MF_NOVENDING: map[m].flag.novending=1; break;
  8576. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  8577. case MF_NOCHAT: map[m].flag.nochat=1; break;
  8578. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  8579. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  8580. case MF_TOWN: map[m].flag.town=1; break;
  8581. case MF_AUTOTRADE: map[m].flag.autotrade=1; break;
  8582. case MF_ALLOWKS: map[m].flag.allowks=1; break;
  8583. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport=1; break;
  8584. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank=1; break;
  8585. case MF_BATTLEGROUND: map[m].flag.battleground = (!val || atoi(val) < 0 || atoi(val) > 2) ? 1 : atoi(val); break;
  8586. }
  8587. }
  8588. return 0;
  8589. }
  8590. BUILDIN_FUNC(removemapflag)
  8591. {
  8592. int m,i;
  8593. const char *str;
  8594. str=script_getstr(st,2);
  8595. i=script_getnum(st,3);
  8596. m = map_mapname2mapid(str);
  8597. if(m >= 0) {
  8598. switch(i) {
  8599. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  8600. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  8601. case MF_NOSAVE: map[m].flag.nosave=0; break;
  8602. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  8603. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  8604. case MF_PVP: map[m].flag.pvp=0; break;
  8605. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  8606. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  8607. case MF_GVG: map[m].flag.gvg=0; break;
  8608. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8609. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8610. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8611. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8612. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8613. case MF_NODROP: map[m].flag.nodrop=0; break;
  8614. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8615. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8616. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8617. case MF_SNOW: map[m].flag.snow=0; break;
  8618. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8619. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8620. case MF_FOG: map[m].flag.fog=0; break;
  8621. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8622. case MF_SAKURA: map[m].flag.sakura=0; break;
  8623. case MF_LEAVES: map[m].flag.leaves=0; break;
  8624. case MF_RAIN: map[m].flag.rain=0; break;
  8625. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8626. case MF_NOGO: map[m].flag.nogo=0; break;
  8627. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8628. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8629. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8630. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8631. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8632. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8633. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8634. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8635. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8636. case MF_JEXP: map[m].jexp=100; break;
  8637. case MF_BEXP: map[m].bexp=100; break;
  8638. case MF_NOVENDING: map[m].flag.novending=0; break;
  8639. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8640. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8641. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8642. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8643. case MF_TOWN: map[m].flag.town=0; break;
  8644. case MF_AUTOTRADE: map[m].flag.autotrade=0; break;
  8645. case MF_ALLOWKS: map[m].flag.allowks=0; break;
  8646. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport=0; break;
  8647. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank=0; break;
  8648. case MF_BATTLEGROUND: map[m].flag.battleground=0; break;
  8649. }
  8650. }
  8651. return 0;
  8652. }
  8653. BUILDIN_FUNC(pvpon)
  8654. {
  8655. int m;
  8656. const char *str;
  8657. TBL_PC* sd = NULL;
  8658. struct s_mapiterator* iter;
  8659. str = script_getstr(st,2);
  8660. m = map_mapname2mapid(str);
  8661. if( m < 0 || map[m].flag.pvp )
  8662. return 0; // nothing to do
  8663. map[m].flag.pvp = 1;
  8664. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  8665. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8666. return 0;
  8667. iter = mapit_getallusers();
  8668. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8669. {
  8670. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  8671. continue; // not applicable
  8672. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8673. sd->pvp_rank = 0;
  8674. sd->pvp_lastusers = 0;
  8675. sd->pvp_point = 5;
  8676. sd->pvp_won = 0;
  8677. sd->pvp_lost = 0;
  8678. }
  8679. mapit_free(iter);
  8680. return 0;
  8681. }
  8682. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8683. {
  8684. TBL_PC* sd = (TBL_PC*)bl;
  8685. clif_pvpset(sd, 0, 0, 2);
  8686. if (sd->pvp_timer != INVALID_TIMER) {
  8687. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8688. sd->pvp_timer = INVALID_TIMER;
  8689. }
  8690. return 0;
  8691. }
  8692. BUILDIN_FUNC(pvpoff)
  8693. {
  8694. int m;
  8695. const char *str;
  8696. str=script_getstr(st,2);
  8697. m = map_mapname2mapid(str);
  8698. if(m < 0 || !map[m].flag.pvp)
  8699. return 0; //fixed Lupus
  8700. map[m].flag.pvp = 0;
  8701. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8702. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8703. return 0;
  8704. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8705. return 0;
  8706. }
  8707. BUILDIN_FUNC(gvgon)
  8708. {
  8709. int m;
  8710. const char *str;
  8711. str=script_getstr(st,2);
  8712. m = map_mapname2mapid(str);
  8713. if(m >= 0 && !map[m].flag.gvg) {
  8714. map[m].flag.gvg = 1;
  8715. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  8716. }
  8717. return 0;
  8718. }
  8719. BUILDIN_FUNC(gvgoff)
  8720. {
  8721. int m;
  8722. const char *str;
  8723. str=script_getstr(st,2);
  8724. m = map_mapname2mapid(str);
  8725. if(m >= 0 && map[m].flag.gvg) {
  8726. map[m].flag.gvg = 0;
  8727. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  8728. }
  8729. return 0;
  8730. }
  8731. /*==========================================
  8732. * Shows an emoticon on top of the player/npc
  8733. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  8734. *------------------------------------------*/
  8735. //Optional second parameter added by [Skotlex]
  8736. BUILDIN_FUNC(emotion)
  8737. {
  8738. int type;
  8739. int player=0;
  8740. type=script_getnum(st,2);
  8741. if(type < 0 || type > 100)
  8742. return 0;
  8743. if( script_hasdata(st,3) )
  8744. player=script_getnum(st,3);
  8745. if (player) {
  8746. TBL_PC *sd = NULL;
  8747. if( script_hasdata(st,4) )
  8748. sd = map_nick2sd(script_getstr(st,4));
  8749. else
  8750. sd = script_rid2sd(st);
  8751. if (sd)
  8752. clif_emotion(&sd->bl,type);
  8753. } else
  8754. if( script_hasdata(st,4) )
  8755. {
  8756. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  8757. if(nd)
  8758. clif_emotion(&nd->bl,type);
  8759. }
  8760. else
  8761. clif_emotion(map_id2bl(st->oid),type);
  8762. return 0;
  8763. }
  8764. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8765. {
  8766. int m=va_arg(ap,int);
  8767. int g_id=va_arg(ap,int);
  8768. int flag=va_arg(ap,int);
  8769. if(!sd || sd->bl.m != m)
  8770. return 0;
  8771. if(
  8772. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8773. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8774. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8775. )
  8776. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  8777. return 1;
  8778. }
  8779. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8780. {
  8781. struct mob_data *md=(struct mob_data *)bl;
  8782. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8783. status_kill(bl);
  8784. return 0;
  8785. }
  8786. BUILDIN_FUNC(maprespawnguildid)
  8787. {
  8788. const char *mapname=script_getstr(st,2);
  8789. int g_id=script_getnum(st,3);
  8790. int flag=script_getnum(st,4);
  8791. int m=map_mapname2mapid(mapname);
  8792. if(m == -1)
  8793. return 0;
  8794. //Catch ALL players (in case some are 'between maps' on execution time)
  8795. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8796. if (flag&4) //Remove script mobs.
  8797. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8798. return 0;
  8799. }
  8800. BUILDIN_FUNC(agitstart)
  8801. {
  8802. if(agit_flag==1) return 0; // Agit already Start.
  8803. agit_flag=1;
  8804. guild_agit_start();
  8805. return 0;
  8806. }
  8807. BUILDIN_FUNC(agitend)
  8808. {
  8809. if(agit_flag==0) return 0; // Agit already End.
  8810. agit_flag=0;
  8811. guild_agit_end();
  8812. return 0;
  8813. }
  8814. BUILDIN_FUNC(agitstart2)
  8815. {
  8816. if(agit2_flag==1) return 0; // Agit2 already Start.
  8817. agit2_flag=1;
  8818. guild_agit2_start();
  8819. return 0;
  8820. }
  8821. BUILDIN_FUNC(agitend2)
  8822. {
  8823. if(agit2_flag==0) return 0; // Agit2 already End.
  8824. agit2_flag=0;
  8825. guild_agit2_end();
  8826. return 0;
  8827. }
  8828. /*==========================================
  8829. * Returns whether woe is on or off. // choice script
  8830. *------------------------------------------*/
  8831. BUILDIN_FUNC(agitcheck)
  8832. {
  8833. script_pushint(st,agit_flag);
  8834. return 0;
  8835. }
  8836. /*==========================================
  8837. * Returns whether woese is on or off. // choice script
  8838. *------------------------------------------*/
  8839. BUILDIN_FUNC(agitcheck2)
  8840. {
  8841. script_pushint(st,agit2_flag);
  8842. return 0;
  8843. }
  8844. /// Sets the guild_id of this npc.
  8845. ///
  8846. /// flagemblem <guild_id>;
  8847. BUILDIN_FUNC(flagemblem)
  8848. {
  8849. TBL_NPC* nd;
  8850. int g_id=script_getnum(st,2);
  8851. if(g_id < 0) return 0;
  8852. nd = (TBL_NPC*)map_id2nd(st->oid);
  8853. if( nd == NULL )
  8854. {
  8855. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8856. }
  8857. else if( nd->subtype != SCRIPT )
  8858. {
  8859. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8860. }
  8861. else
  8862. {
  8863. nd->u.scr.guild_id = g_id;
  8864. clif_guild_emblem_area(&nd->bl);
  8865. }
  8866. return 0;
  8867. }
  8868. BUILDIN_FUNC(getcastlename)
  8869. {
  8870. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8871. struct guild_castle* gc = guild_mapname2gc(mapname);
  8872. const char* name = (gc) ? gc->castle_name : "";
  8873. script_pushstrcopy(st,name);
  8874. return 0;
  8875. }
  8876. BUILDIN_FUNC(getcastledata)
  8877. {
  8878. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8879. int index = script_getnum(st,3);
  8880. struct guild_castle* gc = guild_mapname2gc(mapname);
  8881. if(script_hasdata(st,4) && index==0 && gc) {
  8882. const char* event = script_getstr(st,4);
  8883. check_event(st, event);
  8884. guild_addcastleinfoevent(gc->castle_id,17,event);
  8885. }
  8886. if(gc){
  8887. switch(index){
  8888. case 0: {
  8889. int i;
  8890. for(i=1;i<18;i++) // Initialize[AgitInit]
  8891. guild_castledataload(gc->castle_id,i);
  8892. } break;
  8893. case 1:
  8894. script_pushint(st,gc->guild_id); break;
  8895. case 2:
  8896. script_pushint(st,gc->economy); break;
  8897. case 3:
  8898. script_pushint(st,gc->defense); break;
  8899. case 4:
  8900. script_pushint(st,gc->triggerE); break;
  8901. case 5:
  8902. script_pushint(st,gc->triggerD); break;
  8903. case 6:
  8904. script_pushint(st,gc->nextTime); break;
  8905. case 7:
  8906. script_pushint(st,gc->payTime); break;
  8907. case 8:
  8908. script_pushint(st,gc->createTime); break;
  8909. case 9:
  8910. script_pushint(st,gc->visibleC); break;
  8911. case 10:
  8912. case 11:
  8913. case 12:
  8914. case 13:
  8915. case 14:
  8916. case 15:
  8917. case 16:
  8918. case 17:
  8919. script_pushint(st,gc->guardian[index-10].visible); break;
  8920. default:
  8921. script_pushint(st,0); break;
  8922. }
  8923. return 0;
  8924. }
  8925. script_pushint(st,0);
  8926. return 0;
  8927. }
  8928. BUILDIN_FUNC(setcastledata)
  8929. {
  8930. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8931. int index = script_getnum(st,3);
  8932. int value = script_getnum(st,4);
  8933. struct guild_castle* gc = guild_mapname2gc(mapname);
  8934. if(gc) {
  8935. // Save Data byself First
  8936. switch(index){
  8937. case 1:
  8938. gc->guild_id = value; break;
  8939. case 2:
  8940. gc->economy = value; break;
  8941. case 3:
  8942. gc->defense = value; break;
  8943. case 4:
  8944. gc->triggerE = value; break;
  8945. case 5:
  8946. gc->triggerD = value; break;
  8947. case 6:
  8948. gc->nextTime = value; break;
  8949. case 7:
  8950. gc->payTime = value; break;
  8951. case 8:
  8952. gc->createTime = value; break;
  8953. case 9:
  8954. gc->visibleC = value; break;
  8955. case 10:
  8956. case 11:
  8957. case 12:
  8958. case 13:
  8959. case 14:
  8960. case 15:
  8961. case 16:
  8962. case 17:
  8963. gc->guardian[index-10].visible = value; break;
  8964. default:
  8965. return 0;
  8966. }
  8967. guild_castledatasave(gc->castle_id,index,value);
  8968. }
  8969. return 0;
  8970. }
  8971. /* =====================================================================
  8972. * ギルド情報を要求する
  8973. * ---------------------------------------------------------------------*/
  8974. BUILDIN_FUNC(requestguildinfo)
  8975. {
  8976. int guild_id=script_getnum(st,2);
  8977. const char *event=NULL;
  8978. if( script_hasdata(st,3) ){
  8979. event=script_getstr(st,3);
  8980. check_event(st, event);
  8981. }
  8982. if(guild_id>0)
  8983. guild_npc_request_info(guild_id,event);
  8984. return 0;
  8985. }
  8986. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8987. /// getequipcardcnt(<equipment slot>);
  8988. BUILDIN_FUNC(getequipcardcnt)
  8989. {
  8990. int i=-1,j,num;
  8991. TBL_PC *sd;
  8992. int count;
  8993. num=script_getnum(st,2);
  8994. sd=script_rid2sd(st);
  8995. if (num > 0 && num <= ARRAYLENGTH(equip))
  8996. i=pc_checkequip(sd,equip[num-1]);
  8997. if (i < 0 || !sd->inventory_data[i]) {
  8998. script_pushint(st,0);
  8999. return 0;
  9000. }
  9001. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9002. {
  9003. script_pushint(st,0);
  9004. return 0;
  9005. }
  9006. count = 0;
  9007. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  9008. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  9009. count++;
  9010. script_pushint(st,count);
  9011. return 0;
  9012. }
  9013. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  9014. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  9015. /// successremovecards <slot>;
  9016. BUILDIN_FUNC(successremovecards)
  9017. {
  9018. int i=-1,j,c,cardflag=0;
  9019. TBL_PC* sd = script_rid2sd(st);
  9020. int num = script_getnum(st,2);
  9021. if (num > 0 && num <= ARRAYLENGTH(equip))
  9022. i=pc_checkequip(sd,equip[num-1]);
  9023. if (i < 0 || !sd->inventory_data[i]) {
  9024. return 0;
  9025. }
  9026. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9027. return 0;
  9028. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9029. {
  9030. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9031. {// extract this card from the item
  9032. int flag;
  9033. struct item item_tmp;
  9034. cardflag = 1;
  9035. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9036. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9037. item_tmp.attribute=0,item_tmp.expire_time=0;
  9038. for (j = 0; j < MAX_SLOTS; j++)
  9039. item_tmp.card[j]=0;
  9040. //Logs items, got from (N)PC scripts [Lupus]
  9041. if(log_config.enable_logs&0x40)
  9042. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  9043. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  9044. clif_additem(sd,0,0,flag);
  9045. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9046. }
  9047. }
  9048. }
  9049. if(cardflag == 1)
  9050. { // カードを取り除いたアイテム所得
  9051. int flag;
  9052. struct item item_tmp;
  9053. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9054. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9055. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9056. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9057. item_tmp.card[j]=0;
  9058. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9059. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9060. //Logs items, got from (N)PC scripts [Lupus]
  9061. if(log_config.enable_logs&0x40)
  9062. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9063. pc_delitem(sd,i,1,0,3);
  9064. //Logs items, got from (N)PC scripts [Lupus]
  9065. if(log_config.enable_logs&0x40)
  9066. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  9067. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  9068. clif_additem(sd,0,0,flag);
  9069. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9070. }
  9071. clif_misceffect(&sd->bl,3);
  9072. }
  9073. return 0;
  9074. }
  9075. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  9076. /// failedremovecards <slot>, <type>;
  9077. /// <type>=0 : will destroy both the item and the cards.
  9078. /// <type>=1 : will keep the item, but destroy the cards.
  9079. /// <type>=2 : will keep the cards, but destroy the item.
  9080. /// <type>=? : will just display the failure effect.
  9081. BUILDIN_FUNC(failedremovecards)
  9082. {
  9083. int i=-1,j,c,cardflag=0;
  9084. TBL_PC* sd = script_rid2sd(st);
  9085. int num = script_getnum(st,2);
  9086. int typefail = script_getnum(st,3);
  9087. if (num > 0 && num <= ARRAYLENGTH(equip))
  9088. i=pc_checkequip(sd,equip[num-1]);
  9089. if (i < 0 || !sd->inventory_data[i])
  9090. return 0;
  9091. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9092. return 0;
  9093. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  9094. {
  9095. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  9096. {
  9097. cardflag = 1;
  9098. if(typefail == 2)
  9099. {// add cards to inventory, clear
  9100. int flag;
  9101. struct item item_tmp;
  9102. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  9103. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  9104. item_tmp.attribute=0,item_tmp.expire_time=0;
  9105. for (j = 0; j < MAX_SLOTS; j++)
  9106. item_tmp.card[j]=0;
  9107. //Logs items, got from (N)PC scripts [Lupus]
  9108. if(log_config.enable_logs&0x40)
  9109. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  9110. if((flag=pc_additem(sd,&item_tmp,1))){
  9111. clif_additem(sd,0,0,flag);
  9112. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9113. }
  9114. }
  9115. }
  9116. }
  9117. if(cardflag == 1)
  9118. {
  9119. if(typefail == 0 || typefail == 2){ // 武具損失
  9120. //Logs items, got from (N)PC scripts [Lupus]
  9121. if(log_config.enable_logs&0x40)
  9122. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9123. pc_delitem(sd,i,1,0,2);
  9124. }
  9125. if(typefail == 1){ // カードのみ損失(武具を返す)
  9126. int flag;
  9127. struct item item_tmp;
  9128. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  9129. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  9130. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  9131. //Logs items, got from (N)PC scripts [Lupus]
  9132. if(log_config.enable_logs&0x40)
  9133. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  9134. for (j = 0; j < sd->inventory_data[i]->slot; j++)
  9135. item_tmp.card[j]=0;
  9136. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  9137. item_tmp.card[j]=sd->status.inventory[i].card[j];
  9138. pc_delitem(sd,i,1,0,2);
  9139. //Logs items, got from (N)PC scripts [Lupus]
  9140. if(log_config.enable_logs&0x40)
  9141. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  9142. if((flag=pc_additem(sd,&item_tmp,1))){
  9143. clif_additem(sd,0,0,flag);
  9144. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  9145. }
  9146. }
  9147. clif_misceffect(&sd->bl,2);
  9148. }
  9149. return 0;
  9150. }
  9151. /* ================================================================
  9152. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  9153. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  9154. * improved by [Lance]
  9155. * ================================================================*/
  9156. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  9157. {
  9158. int x,y,m,check_val=0,check_ID=0,i=0;
  9159. struct guild *g = NULL;
  9160. struct party_data *p = NULL;
  9161. const char *str;
  9162. const char *mapname;
  9163. unsigned int index;
  9164. mapname=script_getstr(st,2);
  9165. str=script_getstr(st,3);
  9166. x=script_getnum(st,4);
  9167. y=script_getnum(st,5);
  9168. if(script_hasdata(st,7)){
  9169. check_val=script_getnum(st,6);
  9170. check_ID=script_getnum(st,7);
  9171. }
  9172. if((m=map_mapname2mapid(mapname))< 0)
  9173. return 0;
  9174. if(!(index=mapindex_name2id(str)))
  9175. return 0;
  9176. switch(check_val){
  9177. case 1:
  9178. g = guild_search(check_ID);
  9179. if (g){
  9180. for( i=0; i < g->max_member; i++)
  9181. {
  9182. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9183. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  9184. }
  9185. }
  9186. }
  9187. break;
  9188. case 2:
  9189. p = party_search(check_ID);
  9190. if(p){
  9191. for(i=0;i<MAX_PARTY; i++){
  9192. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9193. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  9194. }
  9195. }
  9196. }
  9197. break;
  9198. default:
  9199. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  9200. break;
  9201. }
  9202. return 0;
  9203. }
  9204. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9205. {
  9206. char *event=va_arg(ap,char *);
  9207. struct mob_data *md = ((struct mob_data *)bl);
  9208. if(strcmp(event,md->npc_event)==0 && md->status.hp > 0)
  9209. return 1;
  9210. return 0;
  9211. }
  9212. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9213. {
  9214. const char *mapname,*event;
  9215. int m;
  9216. mapname=script_getstr(st,2);
  9217. event=script_getstr(st,3);
  9218. check_event(st, event);
  9219. if( (m = map_mapname2mapid(mapname)) < 0 ) {
  9220. script_pushint(st,-1);
  9221. return 0;
  9222. }
  9223. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  9224. {
  9225. script_pushint(st,-1);
  9226. return 0;
  9227. }
  9228. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9229. return 0;
  9230. }
  9231. BUILDIN_FUNC(marriage)
  9232. {
  9233. const char *partner=script_getstr(st,2);
  9234. TBL_PC *sd=script_rid2sd(st);
  9235. TBL_PC *p_sd=map_nick2sd(partner);
  9236. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9237. script_pushint(st,0);
  9238. return 0;
  9239. }
  9240. script_pushint(st,1);
  9241. return 0;
  9242. }
  9243. BUILDIN_FUNC(wedding_effect)
  9244. {
  9245. TBL_PC *sd=script_rid2sd(st);
  9246. struct block_list *bl;
  9247. if(sd==NULL) {
  9248. bl=map_id2bl(st->oid);
  9249. } else
  9250. bl=&sd->bl;
  9251. clif_wedding_effect(bl);
  9252. return 0;
  9253. }
  9254. BUILDIN_FUNC(divorce)
  9255. {
  9256. TBL_PC *sd=script_rid2sd(st);
  9257. if(sd==NULL || pc_divorce(sd) < 0){
  9258. script_pushint(st,0);
  9259. return 0;
  9260. }
  9261. script_pushint(st,1);
  9262. return 0;
  9263. }
  9264. BUILDIN_FUNC(ispartneron)
  9265. {
  9266. TBL_PC *sd=script_rid2sd(st);
  9267. if(sd==NULL || !pc_ismarried(sd) ||
  9268. map_charid2sd(sd->status.partner_id) == NULL) {
  9269. script_pushint(st,0);
  9270. return 0;
  9271. }
  9272. script_pushint(st,1);
  9273. return 0;
  9274. }
  9275. BUILDIN_FUNC(getpartnerid)
  9276. {
  9277. TBL_PC *sd=script_rid2sd(st);
  9278. if (sd == NULL) {
  9279. script_pushint(st,0);
  9280. return 0;
  9281. }
  9282. script_pushint(st,sd->status.partner_id);
  9283. return 0;
  9284. }
  9285. BUILDIN_FUNC(getchildid)
  9286. {
  9287. TBL_PC *sd=script_rid2sd(st);
  9288. if (sd == NULL) {
  9289. script_pushint(st,0);
  9290. return 0;
  9291. }
  9292. script_pushint(st,sd->status.child);
  9293. return 0;
  9294. }
  9295. BUILDIN_FUNC(getmotherid)
  9296. {
  9297. TBL_PC *sd=script_rid2sd(st);
  9298. if (sd == NULL) {
  9299. script_pushint(st,0);
  9300. return 0;
  9301. }
  9302. script_pushint(st,sd->status.mother);
  9303. return 0;
  9304. }
  9305. BUILDIN_FUNC(getfatherid)
  9306. {
  9307. TBL_PC *sd=script_rid2sd(st);
  9308. if (sd == NULL) {
  9309. script_pushint(st,0);
  9310. return 0;
  9311. }
  9312. script_pushint(st,sd->status.father);
  9313. return 0;
  9314. }
  9315. BUILDIN_FUNC(warppartner)
  9316. {
  9317. int x,y;
  9318. unsigned short mapindex;
  9319. const char *str;
  9320. TBL_PC *sd=script_rid2sd(st);
  9321. TBL_PC *p_sd=NULL;
  9322. if(sd==NULL || !pc_ismarried(sd) ||
  9323. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9324. script_pushint(st,0);
  9325. return 0;
  9326. }
  9327. str=script_getstr(st,2);
  9328. x=script_getnum(st,3);
  9329. y=script_getnum(st,4);
  9330. mapindex = mapindex_name2id(str);
  9331. if (mapindex) {
  9332. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  9333. script_pushint(st,1);
  9334. } else
  9335. script_pushint(st,0);
  9336. return 0;
  9337. }
  9338. /*================================================
  9339. * Script for Displaying MOB Information [Valaris]
  9340. *------------------------------------------------*/
  9341. BUILDIN_FUNC(strmobinfo)
  9342. {
  9343. int num=script_getnum(st,2);
  9344. int class_=script_getnum(st,3);
  9345. if(!mobdb_checkid(class_))
  9346. {
  9347. script_pushint(st,0);
  9348. return 0;
  9349. }
  9350. switch (num) {
  9351. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  9352. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  9353. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9354. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9355. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9356. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9357. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9358. default:
  9359. script_pushint(st,0);
  9360. break;
  9361. }
  9362. return 0;
  9363. }
  9364. /*==========================================
  9365. * Summon guardians [Valaris]
  9366. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  9367. *------------------------------------------*/
  9368. BUILDIN_FUNC(guardian)
  9369. {
  9370. int class_=0,x=0,y=0,guardian=0;
  9371. const char *str,*map,*evt="";
  9372. struct script_data *data;
  9373. bool has_index = false;
  9374. map =script_getstr(st,2);
  9375. x =script_getnum(st,3);
  9376. y =script_getnum(st,4);
  9377. str =script_getstr(st,5);
  9378. class_=script_getnum(st,6);
  9379. if( script_hasdata(st,8) )
  9380. {// "<event label>",<guardian index>
  9381. evt=script_getstr(st,7);
  9382. guardian=script_getnum(st,8);
  9383. has_index = true;
  9384. } else if( script_hasdata(st,7) ){
  9385. data=script_getdata(st,7);
  9386. get_val(st,data);
  9387. if( data_isstring(data) )
  9388. {// "<event label>"
  9389. evt=script_getstr(st,7);
  9390. } else if( data_isint(data) )
  9391. {// <guardian index>
  9392. guardian=script_getnum(st,7);
  9393. has_index = true;
  9394. } else {
  9395. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9396. script_reportdata(data);
  9397. return 1;
  9398. }
  9399. }
  9400. check_event(st, evt);
  9401. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  9402. return 0;
  9403. }
  9404. /*==========================================
  9405. * Invisible Walls [Zephyrus]
  9406. *------------------------------------------*/
  9407. BUILDIN_FUNC(setwall)
  9408. {
  9409. const char *map, *name;
  9410. int x, y, m, size, dir;
  9411. bool shootable;
  9412. map = script_getstr(st,2);
  9413. x = script_getnum(st,3);
  9414. y = script_getnum(st,4);
  9415. size = script_getnum(st,5);
  9416. dir = script_getnum(st,6);
  9417. shootable = script_getnum(st,7);
  9418. name = script_getstr(st,8);
  9419. if( (m = map_mapname2mapid(map)) < 0 )
  9420. return 0; // Invalid Map
  9421. map_iwall_set(m, x, y, size, dir, shootable, name);
  9422. return 0;
  9423. }
  9424. BUILDIN_FUNC(delwall)
  9425. {
  9426. const char *name = script_getstr(st,2);
  9427. map_iwall_remove(name);
  9428. return 0;
  9429. }
  9430. /// Retrieves various information about the specified guardian.
  9431. ///
  9432. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9433. /// type: 0 - whether it is deployed or not
  9434. /// 1 - maximum hp
  9435. /// 2 - current hp
  9436. ///
  9437. BUILDIN_FUNC(guardianinfo)
  9438. {
  9439. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9440. int id = script_getnum(st,3);
  9441. int type = script_getnum(st,4);
  9442. struct guild_castle* gc = guild_mapname2gc(mapname);
  9443. struct mob_data* gd;
  9444. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9445. {
  9446. script_pushint(st,-1);
  9447. return 0;
  9448. }
  9449. if( type == 0 )
  9450. script_pushint(st, gc->guardian[id].visible);
  9451. else
  9452. if( !gc->guardian[id].visible )
  9453. script_pushint(st,-1);
  9454. else
  9455. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9456. script_pushint(st,-1);
  9457. else
  9458. {
  9459. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9460. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9461. else
  9462. script_pushint(st,-1);
  9463. }
  9464. return 0;
  9465. }
  9466. /*==========================================
  9467. * IDからItem名
  9468. *------------------------------------------*/
  9469. BUILDIN_FUNC(getitemname)
  9470. {
  9471. int item_id=0;
  9472. struct item_data *i_data;
  9473. char *item_name;
  9474. struct script_data *data;
  9475. data=script_getdata(st,2);
  9476. get_val(st,data);
  9477. if( data_isstring(data) ){
  9478. const char *name=conv_str(st,data);
  9479. struct item_data *item_data = itemdb_searchname(name);
  9480. if( item_data )
  9481. item_id=item_data->nameid;
  9482. }else
  9483. item_id=conv_num(st,data);
  9484. i_data = itemdb_exists(item_id);
  9485. if (i_data == NULL)
  9486. {
  9487. script_pushconststr(st,"null");
  9488. return 0;
  9489. }
  9490. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9491. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9492. script_pushstr(st,item_name);
  9493. return 0;
  9494. }
  9495. /*==========================================
  9496. * Returns number of slots an item has. [Skotlex]
  9497. *------------------------------------------*/
  9498. BUILDIN_FUNC(getitemslots)
  9499. {
  9500. int item_id;
  9501. struct item_data *i_data;
  9502. item_id=script_getnum(st,2);
  9503. i_data = itemdb_exists(item_id);
  9504. if (i_data)
  9505. script_pushint(st,i_data->slot);
  9506. else
  9507. script_pushint(st,-1);
  9508. return 0;
  9509. }
  9510. /*==========================================
  9511. * Returns some values of an item [Lupus]
  9512. * Price, Weight, etc...
  9513. getiteminfo(itemID,n), where n
  9514. 0 value_buy;
  9515. 1 value_sell;
  9516. 2 type;
  9517. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9518. if = 0, then monsters don't drop it at all (rare or a quest item)
  9519. if = -1, then this item is sold in NPC shops only
  9520. 4 sex;
  9521. 5 equip;
  9522. 6 weight;
  9523. 7 atk;
  9524. 8 def;
  9525. 9 range;
  9526. 10 slot;
  9527. 11 look;
  9528. 12 elv;
  9529. 13 wlv;
  9530. 14 view id
  9531. *------------------------------------------*/
  9532. BUILDIN_FUNC(getiteminfo)
  9533. {
  9534. int item_id,n;
  9535. int *item_arr;
  9536. struct item_data *i_data;
  9537. item_id = script_getnum(st,2);
  9538. n = script_getnum(st,3);
  9539. i_data = itemdb_exists(item_id);
  9540. if (i_data && n>=0 && n<=14) {
  9541. item_arr = (int*)&i_data->value_buy;
  9542. script_pushint(st,item_arr[n]);
  9543. } else
  9544. script_pushint(st,-1);
  9545. return 0;
  9546. }
  9547. /*==========================================
  9548. * Set some values of an item [Lupus]
  9549. * Price, Weight, etc...
  9550. setiteminfo(itemID,n,Value), where n
  9551. 0 value_buy;
  9552. 1 value_sell;
  9553. 2 type;
  9554. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9555. if = 0, then monsters don't drop it at all (rare or a quest item)
  9556. if = -1, then this item is sold in NPC shops only
  9557. 4 sex;
  9558. 5 equip;
  9559. 6 weight;
  9560. 7 atk;
  9561. 8 def;
  9562. 9 range;
  9563. 10 slot;
  9564. 11 look;
  9565. 12 elv;
  9566. 13 wlv;
  9567. 14 view id
  9568. * Returns Value or -1 if the wrong field's been set
  9569. *------------------------------------------*/
  9570. BUILDIN_FUNC(setiteminfo)
  9571. {
  9572. int item_id,n,value;
  9573. int *item_arr;
  9574. struct item_data *i_data;
  9575. item_id = script_getnum(st,2);
  9576. n = script_getnum(st,3);
  9577. value = script_getnum(st,4);
  9578. i_data = itemdb_exists(item_id);
  9579. if (i_data && n>=0 && n<=14) {
  9580. item_arr = (int*)&i_data->value_buy;
  9581. item_arr[n] = value;
  9582. script_pushint(st,value);
  9583. } else
  9584. script_pushint(st,-1);
  9585. return 0;
  9586. }
  9587. /*==========================================
  9588. * Returns value from equipped item slot n [Lupus]
  9589. getequipcardid(num,slot)
  9590. where
  9591. num = eqip position slot
  9592. slot = 0,1,2,3 (Card Slot N)
  9593. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9594. it's useful when you want to check item cards or if it's signed
  9595. Useful for such quests as "Sign this refined item with players name" etc
  9596. Hat[0] +4 -> Player's Hat[0] +4
  9597. *------------------------------------------*/
  9598. BUILDIN_FUNC(getequipcardid)
  9599. {
  9600. int i=-1,num,slot;
  9601. TBL_PC *sd;
  9602. num=script_getnum(st,2);
  9603. slot=script_getnum(st,3);
  9604. sd=script_rid2sd(st);
  9605. if (num > 0 && num <= ARRAYLENGTH(equip))
  9606. i=pc_checkequip(sd,equip[num-1]);
  9607. if(i >= 0 && slot>=0 && slot<4)
  9608. script_pushint(st,sd->status.inventory[i].card[slot]);
  9609. else
  9610. script_pushint(st,0);
  9611. return 0;
  9612. }
  9613. /*==========================================
  9614. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9615. *------------------------------------------*/
  9616. BUILDIN_FUNC(petskillbonus)
  9617. {
  9618. struct pet_data *pd;
  9619. TBL_PC *sd=script_rid2sd(st);
  9620. if(sd==NULL || sd->pd==NULL)
  9621. return 0;
  9622. pd=sd->pd;
  9623. if (pd->bonus)
  9624. { //Clear previous bonus
  9625. if (pd->bonus->timer != INVALID_TIMER)
  9626. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9627. } else //init
  9628. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9629. pd->bonus->type=script_getnum(st,2);
  9630. pd->bonus->val=script_getnum(st,3);
  9631. pd->bonus->duration=script_getnum(st,4);
  9632. pd->bonus->delay=script_getnum(st,5);
  9633. if (pd->state.skillbonus == 1)
  9634. pd->state.skillbonus=0; // waiting state
  9635. // wait for timer to start
  9636. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9637. pd->bonus->timer = INVALID_TIMER;
  9638. else
  9639. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9640. return 0;
  9641. }
  9642. /*==========================================
  9643. * pet looting [Valaris] //Rewritten by [Skotlex]
  9644. *------------------------------------------*/
  9645. BUILDIN_FUNC(petloot)
  9646. {
  9647. int max;
  9648. struct pet_data *pd;
  9649. TBL_PC *sd=script_rid2sd(st);
  9650. if(sd==NULL || sd->pd==NULL)
  9651. return 0;
  9652. max=script_getnum(st,2);
  9653. if(max < 1)
  9654. max = 1; //Let'em loot at least 1 item.
  9655. else if (max > MAX_PETLOOT_SIZE)
  9656. max = MAX_PETLOOT_SIZE;
  9657. pd = sd->pd;
  9658. if (pd->loot != NULL)
  9659. { //Release whatever was there already and reallocate memory
  9660. pet_lootitem_drop(pd, pd->msd);
  9661. aFree(pd->loot->item);
  9662. }
  9663. else
  9664. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9665. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9666. pd->loot->max=max;
  9667. pd->loot->count = 0;
  9668. pd->loot->weight = 0;
  9669. return 0;
  9670. }
  9671. /*==========================================
  9672. * PCの所持品情報読み取り
  9673. *------------------------------------------*/
  9674. BUILDIN_FUNC(getinventorylist)
  9675. {
  9676. TBL_PC *sd=script_rid2sd(st);
  9677. char card_var[NAME_LENGTH];
  9678. int i,j=0,k;
  9679. if(!sd) return 0;
  9680. for(i=0;i<MAX_INVENTORY;i++){
  9681. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9682. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  9683. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  9684. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  9685. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  9686. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  9687. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  9688. for (k = 0; k < MAX_SLOTS; k++)
  9689. {
  9690. sprintf(card_var, "@inventorylist_card%d",k+1);
  9691. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  9692. }
  9693. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  9694. j++;
  9695. }
  9696. }
  9697. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9698. return 0;
  9699. }
  9700. BUILDIN_FUNC(getskilllist)
  9701. {
  9702. TBL_PC *sd=script_rid2sd(st);
  9703. int i,j=0;
  9704. if(!sd) return 0;
  9705. for(i=0;i<MAX_SKILL;i++){
  9706. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9707. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  9708. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  9709. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  9710. j++;
  9711. }
  9712. }
  9713. pc_setreg(sd,add_str("@skilllist_count"),j);
  9714. return 0;
  9715. }
  9716. BUILDIN_FUNC(clearitem)
  9717. {
  9718. TBL_PC *sd=script_rid2sd(st);
  9719. int i;
  9720. if(sd==NULL) return 0;
  9721. for (i=0; i<MAX_INVENTORY; i++) {
  9722. if (sd->status.inventory[i].amount) {
  9723. //Logs items, got from (N)PC scripts [Lupus]
  9724. if(log_config.enable_logs&0x40)
  9725. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9726. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0);
  9727. }
  9728. }
  9729. return 0;
  9730. }
  9731. /*==========================================
  9732. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9733. *------------------------------------------*/
  9734. BUILDIN_FUNC(disguise)
  9735. {
  9736. int id;
  9737. TBL_PC* sd = script_rid2sd(st);
  9738. if (sd == NULL) return 0;
  9739. id = script_getnum(st,2);
  9740. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9741. pc_disguise(sd, id);
  9742. script_pushint(st,id);
  9743. } else
  9744. script_pushint(st,0);
  9745. return 0;
  9746. }
  9747. /*==========================================
  9748. * Undisguise Player (returns 1 if success, 0 on fail)
  9749. *------------------------------------------*/
  9750. BUILDIN_FUNC(undisguise)
  9751. {
  9752. TBL_PC* sd = script_rid2sd(st);
  9753. if (sd == NULL) return 0;
  9754. if (sd->disguise) {
  9755. pc_disguise(sd, 0);
  9756. script_pushint(st,0);
  9757. } else {
  9758. script_pushint(st,1);
  9759. }
  9760. return 0;
  9761. }
  9762. /*==========================================
  9763. * NPCクラスチェンジ
  9764. * classは変わりたいclass
  9765. * typeは通常0なのかな?
  9766. *------------------------------------------*/
  9767. BUILDIN_FUNC(classchange)
  9768. {
  9769. int _class,type;
  9770. struct block_list *bl=map_id2bl(st->oid);
  9771. if(bl==NULL) return 0;
  9772. _class=script_getnum(st,2);
  9773. type=script_getnum(st,3);
  9774. clif_class_change(bl,_class,type);
  9775. return 0;
  9776. }
  9777. /*==========================================
  9778. * NPCから発生するエフェクト
  9779. *------------------------------------------*/
  9780. BUILDIN_FUNC(misceffect)
  9781. {
  9782. int type;
  9783. type=script_getnum(st,2);
  9784. if(st->oid && st->oid != fake_nd->bl.id) {
  9785. struct block_list *bl = map_id2bl(st->oid);
  9786. if (bl)
  9787. clif_specialeffect(bl,type,AREA);
  9788. } else{
  9789. TBL_PC *sd=script_rid2sd(st);
  9790. if(sd)
  9791. clif_specialeffect(&sd->bl,type,AREA);
  9792. }
  9793. return 0;
  9794. }
  9795. /*==========================================
  9796. * Play a BGM on a single client [Rikter/Yommy]
  9797. *------------------------------------------*/
  9798. BUILDIN_FUNC(playBGM)
  9799. {
  9800. const char* name;
  9801. struct map_session_data* sd;
  9802. if( ( sd = script_rid2sd(st) ) != NULL )
  9803. {
  9804. name = script_getstr(st,2);
  9805. clif_playBGM(sd, name);
  9806. }
  9807. return 0;
  9808. }
  9809. static int playBGM_sub(struct block_list* bl,va_list ap)
  9810. {
  9811. const char* name = va_arg(ap,const char*);
  9812. clif_playBGM(BL_CAST(BL_PC, bl), name);
  9813. return 0;
  9814. }
  9815. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  9816. {
  9817. const char* name = va_arg(args, const char*);
  9818. clif_playBGM(sd, name);
  9819. return 0;
  9820. }
  9821. /*==========================================
  9822. * Play a BGM on multiple client [Rikter/Yommy]
  9823. *------------------------------------------*/
  9824. BUILDIN_FUNC(playBGMall)
  9825. {
  9826. const char* name;
  9827. name = script_getstr(st,2);
  9828. if( script_hasdata(st,7) )
  9829. {// specified part of map
  9830. const char* map = script_getstr(st,3);
  9831. int x0 = script_getnum(st,4);
  9832. int y0 = script_getnum(st,5);
  9833. int x1 = script_getnum(st,6);
  9834. int y1 = script_getnum(st,7);
  9835. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  9836. }
  9837. else if( script_hasdata(st,3) )
  9838. {// entire map
  9839. const char* map = script_getstr(st,3);
  9840. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  9841. }
  9842. else
  9843. {// entire server
  9844. map_foreachpc(&playBGM_foreachpc_sub, name);
  9845. }
  9846. return 0;
  9847. }
  9848. /*==========================================
  9849. * サウンドエフェクト
  9850. *------------------------------------------*/
  9851. BUILDIN_FUNC(soundeffect)
  9852. {
  9853. TBL_PC* sd = script_rid2sd(st);
  9854. const char* name = script_getstr(st,2);
  9855. int type = script_getnum(st,3);
  9856. if(sd)
  9857. {
  9858. clif_soundeffect(sd,&sd->bl,name,type);
  9859. }
  9860. return 0;
  9861. }
  9862. int soundeffect_sub(struct block_list* bl,va_list ap)
  9863. {
  9864. char* name = va_arg(ap,char*);
  9865. int type = va_arg(ap,int);
  9866. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9867. return 0;
  9868. }
  9869. /*==========================================
  9870. * Play a sound effect (.wav) on multiple clients
  9871. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9872. *------------------------------------------*/
  9873. BUILDIN_FUNC(soundeffectall)
  9874. {
  9875. struct block_list* bl;
  9876. const char* name;
  9877. int type;
  9878. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9879. if (!bl)
  9880. return 0;
  9881. name = script_getstr(st,2);
  9882. type = script_getnum(st,3);
  9883. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9884. if(!script_hasdata(st,4))
  9885. { // area around
  9886. clif_soundeffectall(bl, name, type, AREA);
  9887. }
  9888. else
  9889. if(!script_hasdata(st,5))
  9890. { // entire map
  9891. const char* map = script_getstr(st,4);
  9892. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9893. }
  9894. else
  9895. if(script_hasdata(st,8))
  9896. { // specified part of map
  9897. const char* map = script_getstr(st,4);
  9898. int x0 = script_getnum(st,5);
  9899. int y0 = script_getnum(st,6);
  9900. int x1 = script_getnum(st,7);
  9901. int y1 = script_getnum(st,8);
  9902. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9903. }
  9904. else
  9905. {
  9906. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9907. }
  9908. return 0;
  9909. }
  9910. /*==========================================
  9911. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9912. *------------------------------------------*/
  9913. BUILDIN_FUNC(petrecovery)
  9914. {
  9915. struct pet_data *pd;
  9916. TBL_PC *sd=script_rid2sd(st);
  9917. if(sd==NULL || sd->pd==NULL)
  9918. return 0;
  9919. pd=sd->pd;
  9920. if (pd->recovery)
  9921. { //Halt previous bonus
  9922. if (pd->recovery->timer != INVALID_TIMER)
  9923. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9924. } else //Init
  9925. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9926. pd->recovery->type = (sc_type)script_getnum(st,2);
  9927. pd->recovery->delay = script_getnum(st,3);
  9928. pd->recovery->timer = INVALID_TIMER;
  9929. return 0;
  9930. }
  9931. /*==========================================
  9932. * pet healing [Valaris] //Rewritten by [Skotlex]
  9933. *------------------------------------------*/
  9934. BUILDIN_FUNC(petheal)
  9935. {
  9936. struct pet_data *pd;
  9937. TBL_PC *sd=script_rid2sd(st);
  9938. if(sd==NULL || sd->pd==NULL)
  9939. return 0;
  9940. pd=sd->pd;
  9941. if (pd->s_skill)
  9942. { //Clear previous skill
  9943. if (pd->s_skill->timer != INVALID_TIMER)
  9944. {
  9945. if (pd->s_skill->id)
  9946. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9947. else
  9948. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9949. }
  9950. } else //init memory
  9951. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9952. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9953. //Use the lv as the amount to heal
  9954. pd->s_skill->lv=script_getnum(st,2);
  9955. pd->s_skill->delay=script_getnum(st,3);
  9956. pd->s_skill->hp=script_getnum(st,4);
  9957. pd->s_skill->sp=script_getnum(st,5);
  9958. //Use delay as initial offset to avoid skill/heal exploits
  9959. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9960. pd->s_skill->timer = INVALID_TIMER;
  9961. else
  9962. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9963. return 0;
  9964. }
  9965. /*==========================================
  9966. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9967. *------------------------------------------*/
  9968. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  9969. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  9970. BUILDIN_FUNC(petskillattack)
  9971. {
  9972. struct pet_data *pd;
  9973. TBL_PC *sd=script_rid2sd(st);
  9974. if(sd==NULL || sd->pd==NULL)
  9975. return 0;
  9976. pd=sd->pd;
  9977. if (pd->a_skill == NULL)
  9978. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9979. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9980. pd->a_skill->lv=script_getnum(st,3);
  9981. pd->a_skill->div_ = 0;
  9982. pd->a_skill->rate=script_getnum(st,4);
  9983. pd->a_skill->bonusrate=script_getnum(st,5);
  9984. return 0;
  9985. }
  9986. /*==========================================
  9987. * pet attack skills [Valaris]
  9988. *------------------------------------------*/
  9989. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  9990. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  9991. BUILDIN_FUNC(petskillattack2)
  9992. {
  9993. struct pet_data *pd;
  9994. TBL_PC *sd=script_rid2sd(st);
  9995. if(sd==NULL || sd->pd==NULL)
  9996. return 0;
  9997. pd=sd->pd;
  9998. if (pd->a_skill == NULL)
  9999. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10000. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10001. pd->a_skill->lv=script_getnum(st,3);
  10002. pd->a_skill->div_ = script_getnum(st,4);
  10003. pd->a_skill->rate=script_getnum(st,5);
  10004. pd->a_skill->bonusrate=script_getnum(st,6);
  10005. return 0;
  10006. }
  10007. /*==========================================
  10008. * pet support skills [Skotlex]
  10009. *------------------------------------------*/
  10010. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  10011. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  10012. BUILDIN_FUNC(petskillsupport)
  10013. {
  10014. struct pet_data *pd;
  10015. TBL_PC *sd=script_rid2sd(st);
  10016. if(sd==NULL || sd->pd==NULL)
  10017. return 0;
  10018. pd=sd->pd;
  10019. if (pd->s_skill)
  10020. { //Clear previous skill
  10021. if (pd->s_skill->timer != INVALID_TIMER)
  10022. {
  10023. if (pd->s_skill->id)
  10024. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10025. else
  10026. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10027. }
  10028. } else //init memory
  10029. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10030. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10031. pd->s_skill->lv=script_getnum(st,3);
  10032. pd->s_skill->delay=script_getnum(st,4);
  10033. pd->s_skill->hp=script_getnum(st,5);
  10034. pd->s_skill->sp=script_getnum(st,6);
  10035. //Use delay as initial offset to avoid skill/heal exploits
  10036. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10037. pd->s_skill->timer = INVALID_TIMER;
  10038. else
  10039. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  10040. return 0;
  10041. }
  10042. /*==========================================
  10043. * Scripted skill effects [Celest]
  10044. *------------------------------------------*/
  10045. /// skilleffect <skill id>,<level>
  10046. /// skilleffect "<skill name>",<level>
  10047. BUILDIN_FUNC(skilleffect)
  10048. {
  10049. TBL_PC *sd;
  10050. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10051. int skilllv=script_getnum(st,3);
  10052. sd=script_rid2sd(st);
  10053. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  10054. return 0;
  10055. }
  10056. /*==========================================
  10057. * NPC skill effects [Valaris]
  10058. *------------------------------------------*/
  10059. /// npcskilleffect <skill id>,<level>,<x>,<y>
  10060. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  10061. BUILDIN_FUNC(npcskilleffect)
  10062. {
  10063. struct block_list *bl= map_id2bl(st->oid);
  10064. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10065. int skilllv=script_getnum(st,3);
  10066. int x=script_getnum(st,4);
  10067. int y=script_getnum(st,5);
  10068. if (bl)
  10069. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  10070. return 0;
  10071. }
  10072. /*==========================================
  10073. * Special effects [Valaris]
  10074. *------------------------------------------*/
  10075. BUILDIN_FUNC(specialeffect)
  10076. {
  10077. struct block_list *bl=map_id2bl(st->oid);
  10078. int type = script_getnum(st,2);
  10079. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10080. if(bl==NULL)
  10081. return 0;
  10082. if( script_hasdata(st,4) )
  10083. {
  10084. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10085. if(nd)
  10086. clif_specialeffect(&nd->bl, type, target);
  10087. }
  10088. else
  10089. {
  10090. if (target == SELF) {
  10091. TBL_PC *sd=script_rid2sd(st);
  10092. if (sd)
  10093. clif_specialeffect_single(bl,type,sd->fd);
  10094. } else {
  10095. clif_specialeffect(bl, type, target);
  10096. }
  10097. }
  10098. return 0;
  10099. }
  10100. BUILDIN_FUNC(specialeffect2)
  10101. {
  10102. TBL_PC *sd=script_rid2sd(st);
  10103. int type = script_getnum(st,2);
  10104. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  10105. if( script_hasdata(st,4) )
  10106. sd = map_nick2sd(script_getstr(st,4));
  10107. if (sd)
  10108. clif_specialeffect(&sd->bl, type, target);
  10109. return 0;
  10110. }
  10111. /*==========================================
  10112. * Nude [Valaris]
  10113. *------------------------------------------*/
  10114. BUILDIN_FUNC(nude)
  10115. {
  10116. TBL_PC *sd=script_rid2sd(st);
  10117. int i,calcflag=0;
  10118. if(sd==NULL)
  10119. return 0;
  10120. for(i=0;i<11;i++)
  10121. if(sd->equip_index[i] >= 0) {
  10122. if(!calcflag)
  10123. calcflag=1;
  10124. pc_unequipitem(sd,sd->equip_index[i],2);
  10125. }
  10126. if(calcflag)
  10127. status_calc_pc(sd,0);
  10128. return 0;
  10129. }
  10130. /*==========================================
  10131. * gmcommand [MouseJstr]
  10132. *
  10133. * suggested on the forums...
  10134. * splitted into atcommand & charcommand by [Skotlex]
  10135. *------------------------------------------*/
  10136. BUILDIN_FUNC(atcommand)
  10137. {
  10138. TBL_PC dummy_sd;
  10139. TBL_PC* sd;
  10140. int fd;
  10141. const char* cmd;
  10142. cmd = script_getstr(st,2);
  10143. if (st->rid) {
  10144. sd = script_rid2sd(st);
  10145. fd = sd->fd;
  10146. } else { //Use a dummy character.
  10147. sd = &dummy_sd;
  10148. fd = 0;
  10149. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10150. if (st->oid)
  10151. {
  10152. struct block_list* bl = map_id2bl(st->oid);
  10153. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10154. if (bl->type == BL_NPC)
  10155. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10156. }
  10157. }
  10158. // compatibility with previous implementation (deprecated!)
  10159. if(cmd[0] != atcommand_symbol)
  10160. {
  10161. cmd += strlen(sd->status.name);
  10162. while(*cmd != atcommand_symbol && *cmd != 0)
  10163. cmd++;
  10164. }
  10165. is_atcommand(fd, sd, cmd, 0);
  10166. return 0;
  10167. }
  10168. BUILDIN_FUNC(charcommand)
  10169. {
  10170. TBL_PC dummy_sd;
  10171. TBL_PC* sd;
  10172. int fd;
  10173. const char* cmd;
  10174. cmd = script_getstr(st,2);
  10175. if (st->rid) {
  10176. sd = script_rid2sd(st);
  10177. fd = sd->fd;
  10178. } else { //Use a dummy character.
  10179. sd = &dummy_sd;
  10180. fd = 0;
  10181. memset(&dummy_sd, 0, sizeof(TBL_PC));
  10182. if (st->oid)
  10183. {
  10184. struct block_list* bl = map_id2bl(st->oid);
  10185. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  10186. if (bl->type == BL_NPC)
  10187. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  10188. }
  10189. }
  10190. if (*cmd != charcommand_symbol) {
  10191. ShowWarning("script: buildin_charcommand: No '#' symbol!\n");
  10192. script_reportsrc(st);
  10193. return 1;
  10194. }
  10195. is_atcommand(fd, sd, cmd, 0);
  10196. return 0;
  10197. }
  10198. /*==========================================
  10199. * Displays a message for the player only (like system messages like "you got an apple" )
  10200. *------------------------------------------*/
  10201. BUILDIN_FUNC(dispbottom)
  10202. {
  10203. TBL_PC *sd=script_rid2sd(st);
  10204. const char *message;
  10205. message=script_getstr(st,2);
  10206. if(sd)
  10207. clif_disp_onlyself(sd,message,(int)strlen(message));
  10208. return 0;
  10209. }
  10210. /*==========================================
  10211. * All The Players Full Recovery
  10212. * (HP/SP full restore and resurrect if need)
  10213. *------------------------------------------*/
  10214. BUILDIN_FUNC(recovery)
  10215. {
  10216. TBL_PC* sd;
  10217. struct s_mapiterator* iter;
  10218. iter = mapit_getallusers();
  10219. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10220. {
  10221. if(pc_isdead(sd))
  10222. status_revive(&sd->bl, 100, 100);
  10223. else
  10224. status_percent_heal(&sd->bl, 100, 100);
  10225. clif_displaymessage(sd->fd,"You have been recovered!");
  10226. }
  10227. mapit_free(iter);
  10228. return 0;
  10229. }
  10230. /*==========================================
  10231. * Get your pet info: getpetinfo(n)
  10232. * n -> 0:pet_id 1:pet_class 2:pet_name
  10233. * 3:friendly 4:hungry, 5: rename flag.
  10234. *------------------------------------------*/
  10235. BUILDIN_FUNC(getpetinfo)
  10236. {
  10237. TBL_PC *sd=script_rid2sd(st);
  10238. TBL_PET *pd;
  10239. int type=script_getnum(st,2);
  10240. if(!sd || !sd->pd) {
  10241. if (type == 2)
  10242. script_pushconststr(st,"null");
  10243. else
  10244. script_pushint(st,0);
  10245. return 0;
  10246. }
  10247. pd = sd->pd;
  10248. switch(type){
  10249. case 0: script_pushint(st,pd->pet.pet_id); break;
  10250. case 1: script_pushint(st,pd->pet.class_); break;
  10251. case 2: script_pushstrcopy(st,pd->pet.name); break;
  10252. case 3: script_pushint(st,pd->pet.intimate); break;
  10253. case 4: script_pushint(st,pd->pet.hungry); break;
  10254. case 5: script_pushint(st,pd->pet.rename_flag); break;
  10255. default:
  10256. script_pushint(st,0);
  10257. break;
  10258. }
  10259. return 0;
  10260. }
  10261. /*==========================================
  10262. * Get your homunculus info: gethominfo(n)
  10263. * n -> 0:hom_id 1:class 2:name
  10264. * 3:friendly 4:hungry, 5: rename flag.
  10265. * 6: level
  10266. *------------------------------------------*/
  10267. BUILDIN_FUNC(gethominfo)
  10268. {
  10269. TBL_PC *sd=script_rid2sd(st);
  10270. TBL_HOM *hd;
  10271. int type=script_getnum(st,2);
  10272. hd = sd?sd->hd:NULL;
  10273. if(!merc_is_hom_active(hd))
  10274. {
  10275. if (type == 2)
  10276. script_pushconststr(st,"null");
  10277. else
  10278. script_pushint(st,0);
  10279. return 0;
  10280. }
  10281. switch(type){
  10282. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  10283. case 1: script_pushint(st,hd->homunculus.class_); break;
  10284. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  10285. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10286. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10287. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10288. case 6: script_pushint(st,hd->homunculus.level); break;
  10289. default:
  10290. script_pushint(st,0);
  10291. break;
  10292. }
  10293. return 0;
  10294. }
  10295. /// Retrieves information about character's mercenary
  10296. /// getmercinfo <type>[,<char id>];
  10297. BUILDIN_FUNC(getmercinfo)
  10298. {
  10299. int type, char_id;
  10300. struct map_session_data* sd;
  10301. struct mercenary_data* md;
  10302. type = script_getnum(st,2);
  10303. if( script_hasdata(st,3) )
  10304. {
  10305. char_id = script_getnum(st,3);
  10306. if( ( sd = map_charid2sd(char_id) ) == NULL )
  10307. {
  10308. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  10309. script_pushnil(st);
  10310. return 1;
  10311. }
  10312. }
  10313. else
  10314. {
  10315. if( ( sd = script_rid2sd(st) ) == NULL )
  10316. {
  10317. script_pushnil(st);
  10318. return 0;
  10319. }
  10320. }
  10321. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  10322. switch( type )
  10323. {
  10324. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  10325. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  10326. case 2:
  10327. if( md )
  10328. script_pushstrcopy(st,md->db->name);
  10329. else
  10330. script_pushconststr(st,"");
  10331. break;
  10332. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  10333. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  10334. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  10335. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  10336. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  10337. default:
  10338. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  10339. script_pushnil(st);
  10340. return 1;
  10341. }
  10342. return 0;
  10343. }
  10344. /*==========================================
  10345. * Shows wether your inventory(and equips) contain
  10346. selected card or not.
  10347. checkequipedcard(4001);
  10348. *------------------------------------------*/
  10349. BUILDIN_FUNC(checkequipedcard)
  10350. {
  10351. TBL_PC *sd=script_rid2sd(st);
  10352. int n,i,c=0;
  10353. c=script_getnum(st,2);
  10354. if(sd){
  10355. for(i=0;i<MAX_INVENTORY;i++){
  10356. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  10357. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  10358. continue;
  10359. for(n=0;n<sd->inventory_data[i]->slot;n++){
  10360. if(sd->status.inventory[i].card[n]==c){
  10361. script_pushint(st,1);
  10362. return 0;
  10363. }
  10364. }
  10365. }
  10366. }
  10367. }
  10368. script_pushint(st,0);
  10369. return 0;
  10370. }
  10371. BUILDIN_FUNC(jump_zero)
  10372. {
  10373. int sel;
  10374. sel=script_getnum(st,2);
  10375. if(!sel) {
  10376. int pos;
  10377. if( !data_islabel(script_getdata(st,3)) ){
  10378. ShowError("script: jump_zero: not label !\n");
  10379. st->state=END;
  10380. return 1;
  10381. }
  10382. pos=script_getnum(st,3);
  10383. st->pos=pos;
  10384. st->state=GOTO;
  10385. }
  10386. return 0;
  10387. }
  10388. /*==========================================
  10389. * GetMapMobs
  10390. returns mob counts on a set map:
  10391. e.g. GetMapMobs("prontera")
  10392. use "this" - for player's map
  10393. *------------------------------------------*/
  10394. BUILDIN_FUNC(getmapmobs)
  10395. {
  10396. const char *str=NULL;
  10397. int m=-1,bx,by;
  10398. int count=0;
  10399. struct block_list *bl;
  10400. str=script_getstr(st,2);
  10401. if(strcmp(str,"this")==0){
  10402. TBL_PC *sd=script_rid2sd(st);
  10403. if(sd)
  10404. m=sd->bl.m;
  10405. else{
  10406. script_pushint(st,-1);
  10407. return 0;
  10408. }
  10409. }else
  10410. m=map_mapname2mapid(str);
  10411. if(m < 0){
  10412. script_pushint(st,-1);
  10413. return 0;
  10414. }
  10415. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  10416. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  10417. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  10418. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  10419. count++;
  10420. script_pushint(st,count);
  10421. return 0;
  10422. }
  10423. /*==========================================
  10424. * movenpc [MouseJstr]
  10425. *------------------------------------------*/
  10426. BUILDIN_FUNC(movenpc)
  10427. {
  10428. TBL_NPC *nd = NULL;
  10429. const char *npc;
  10430. int x,y;
  10431. npc = script_getstr(st,2);
  10432. x = script_getnum(st,3);
  10433. y = script_getnum(st,4);
  10434. if ((nd = npc_name2id(npc)) == NULL)
  10435. return -1;
  10436. if (script_hasdata(st,5))
  10437. nd->ud.dir = script_getnum(st,5) % 8;
  10438. npc_movenpc(nd, x, y);
  10439. return 0;
  10440. }
  10441. /*==========================================
  10442. * message [MouseJstr]
  10443. *------------------------------------------*/
  10444. BUILDIN_FUNC(message)
  10445. {
  10446. const char *msg,*player;
  10447. TBL_PC *pl_sd = NULL;
  10448. player = script_getstr(st,2);
  10449. msg = script_getstr(st,3);
  10450. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10451. return 0;
  10452. clif_displaymessage(pl_sd->fd, msg);
  10453. return 0;
  10454. }
  10455. /*==========================================
  10456. * npctalk (sends message to surrounding area)
  10457. *------------------------------------------*/
  10458. BUILDIN_FUNC(npctalk)
  10459. {
  10460. const char* str;
  10461. char name[NAME_LENGTH], message[256];
  10462. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10463. str = script_getstr(st,2);
  10464. if(nd)
  10465. {
  10466. safestrncpy(name, nd->name, sizeof(name));
  10467. strtok(name, "#"); // discard extra name identifier if present
  10468. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  10469. clif_message(&nd->bl, message);
  10470. }
  10471. return 0;
  10472. }
  10473. // change npc walkspeed [Valaris]
  10474. BUILDIN_FUNC(npcspeed)
  10475. {
  10476. struct npc_data* nd;
  10477. int speed;
  10478. speed = script_getnum(st,2);
  10479. nd =(struct npc_data *)map_id2bl(st->oid);
  10480. if( nd )
  10481. {
  10482. nd->speed = speed;
  10483. nd->ud.state.speed_changed = 1;
  10484. }
  10485. return 0;
  10486. }
  10487. // make an npc walk to a position [Valaris]
  10488. BUILDIN_FUNC(npcwalkto)
  10489. {
  10490. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10491. int x=0,y=0;
  10492. x=script_getnum(st,2);
  10493. y=script_getnum(st,3);
  10494. if(nd) {
  10495. unit_walktoxy(&nd->bl,x,y,0);
  10496. }
  10497. return 0;
  10498. }
  10499. // stop an npc's movement [Valaris]
  10500. BUILDIN_FUNC(npcstop)
  10501. {
  10502. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10503. if(nd) {
  10504. unit_stop_walking(&nd->bl,1|4);
  10505. }
  10506. return 0;
  10507. }
  10508. /*==========================================
  10509. * getlook char info. getlook(arg)
  10510. *------------------------------------------*/
  10511. BUILDIN_FUNC(getlook)
  10512. {
  10513. int type,val;
  10514. TBL_PC *sd;
  10515. sd=script_rid2sd(st);
  10516. type=script_getnum(st,2);
  10517. val=-1;
  10518. switch(type) {
  10519. case LOOK_HAIR: val=sd->status.hair; break; //1
  10520. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10521. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10522. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10523. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10524. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10525. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10526. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10527. case LOOK_SHOES: break; //9
  10528. }
  10529. script_pushint(st,val);
  10530. return 0;
  10531. }
  10532. /*==========================================
  10533. * get char save point. argument: 0- map name, 1- x, 2- y
  10534. *------------------------------------------*/
  10535. BUILDIN_FUNC(getsavepoint)
  10536. {
  10537. TBL_PC* sd;
  10538. int type;
  10539. sd = script_rid2sd(st);
  10540. if (sd == NULL) {
  10541. script_pushint(st,0);
  10542. return 0;
  10543. }
  10544. type = script_getnum(st,2);
  10545. switch(type) {
  10546. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10547. case 1: script_pushint(st,sd->status.save_point.x); break;
  10548. case 2: script_pushint(st,sd->status.save_point.y); break;
  10549. default:
  10550. script_pushint(st,0);
  10551. break;
  10552. }
  10553. return 0;
  10554. }
  10555. /*==========================================
  10556. * Get position for char/npc/pet/mob objects. Added by Lorky
  10557. *
  10558. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10559. * where type:
  10560. * MapName$ - String variable for output map name
  10561. * MapX - Integer variable for output coord X
  10562. * MapY - Integer variable for output coord Y
  10563. * type - type of object
  10564. * 0 - Character coord
  10565. * 1 - NPC coord
  10566. * 2 - Pet coord
  10567. * 3 - Mob coord (not released)
  10568. * 4 - Homun coord
  10569. * CharName$ - Name object. If miss or "this" the current object
  10570. *
  10571. * Return:
  10572. * 0 - success
  10573. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10574. *------------------------------------------*/
  10575. BUILDIN_FUNC(getmapxy)
  10576. {
  10577. struct block_list *bl = NULL;
  10578. TBL_PC *sd=NULL;
  10579. int num;
  10580. const char *name;
  10581. char prefix;
  10582. int x,y,type;
  10583. char mapname[MAP_NAME_LENGTH];
  10584. if( !data_isreference(script_getdata(st,2)) ){
  10585. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10586. script_pushint(st,-1);
  10587. return 1;
  10588. }
  10589. if( !data_isreference(script_getdata(st,3)) ){
  10590. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10591. script_pushint(st,-1);
  10592. return 1;
  10593. }
  10594. if( !data_isreference(script_getdata(st,4)) ){
  10595. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10596. script_pushint(st,-1);
  10597. return 1;
  10598. }
  10599. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10600. type=script_getnum(st,5);
  10601. switch (type){
  10602. case 0: //Get Character Position
  10603. if( script_hasdata(st,6) )
  10604. sd=map_nick2sd(script_getstr(st,6));
  10605. else
  10606. sd=script_rid2sd(st);
  10607. if (sd)
  10608. bl = &sd->bl;
  10609. break;
  10610. case 1: //Get NPC Position
  10611. if( script_hasdata(st,6) )
  10612. {
  10613. struct npc_data *nd;
  10614. nd=npc_name2id(script_getstr(st,6));
  10615. if (nd)
  10616. bl = &nd->bl;
  10617. } else //In case the origin is not an npc?
  10618. bl=map_id2bl(st->oid);
  10619. break;
  10620. case 2: //Get Pet Position
  10621. if(script_hasdata(st,6))
  10622. sd=map_nick2sd(script_getstr(st,6));
  10623. else
  10624. sd=script_rid2sd(st);
  10625. if (sd && sd->pd)
  10626. bl = &sd->pd->bl;
  10627. break;
  10628. case 3: //Get Mob Position
  10629. break; //Not supported?
  10630. case 4: //Get Homun Position
  10631. if(script_hasdata(st,6))
  10632. sd=map_nick2sd(script_getstr(st,6));
  10633. else
  10634. sd=script_rid2sd(st);
  10635. if (sd && sd->hd)
  10636. bl = &sd->hd->bl;
  10637. break;
  10638. default:
  10639. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  10640. script_pushint(st,-1);
  10641. return 1;
  10642. }
  10643. if (!bl) { //No object found.
  10644. script_pushint(st,-1);
  10645. return 0;
  10646. }
  10647. x= bl->x;
  10648. y= bl->y;
  10649. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10650. //Set MapName$
  10651. num=st->stack->stack_data[st->start+2].u.num;
  10652. name=get_str(num&0x00ffffff);
  10653. prefix=*name;
  10654. if(not_server_variable(prefix))
  10655. sd=script_rid2sd(st);
  10656. else
  10657. sd=NULL;
  10658. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10659. //Set MapX
  10660. num=st->stack->stack_data[st->start+3].u.num;
  10661. name=get_str(num&0x00ffffff);
  10662. prefix=*name;
  10663. if(not_server_variable(prefix))
  10664. sd=script_rid2sd(st);
  10665. else
  10666. sd=NULL;
  10667. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10668. //Set MapY
  10669. num=st->stack->stack_data[st->start+4].u.num;
  10670. name=get_str(num&0x00ffffff);
  10671. prefix=*name;
  10672. if(not_server_variable(prefix))
  10673. sd=script_rid2sd(st);
  10674. else
  10675. sd=NULL;
  10676. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10677. //Return Success value
  10678. script_pushint(st,0);
  10679. return 0;
  10680. }
  10681. /*==========================================
  10682. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10683. *------------------------------------------*/
  10684. BUILDIN_FUNC(logmes)
  10685. {
  10686. const char *str;
  10687. TBL_PC* sd;
  10688. if( log_config.npc <= 0 )
  10689. return 0;
  10690. sd = script_rid2sd(st);
  10691. if( sd == NULL )
  10692. return 1;
  10693. str = script_getstr(st,2);
  10694. log_npc(sd,str);
  10695. return 0;
  10696. }
  10697. BUILDIN_FUNC(summon)
  10698. {
  10699. int _class, timeout=0;
  10700. const char *str,*event="";
  10701. TBL_PC *sd;
  10702. struct mob_data *md;
  10703. int tick = gettick();
  10704. sd=script_rid2sd(st);
  10705. if (!sd) return 0;
  10706. str =script_getstr(st,2);
  10707. _class=script_getnum(st,3);
  10708. if( script_hasdata(st,4) )
  10709. timeout=script_getnum(st,4);
  10710. if( script_hasdata(st,5) ){
  10711. event=script_getstr(st,5);
  10712. check_event(st, event);
  10713. }
  10714. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10715. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10716. if (md) {
  10717. md->master_id=sd->bl.id;
  10718. md->special_state.ai=1;
  10719. if( md->deletetimer != INVALID_TIMER )
  10720. delete_timer(md->deletetimer, mob_timer_delete);
  10721. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10722. mob_spawn (md); //Now it is ready for spawning.
  10723. clif_specialeffect(&md->bl,344,AREA);
  10724. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10725. }
  10726. return 0;
  10727. }
  10728. /*==========================================
  10729. * Checks whether it is daytime/nighttime
  10730. *------------------------------------------*/
  10731. BUILDIN_FUNC(isnight)
  10732. {
  10733. script_pushint(st,(night_flag == 1));
  10734. return 0;
  10735. }
  10736. BUILDIN_FUNC(isday)
  10737. {
  10738. script_pushint(st,(night_flag == 0));
  10739. return 0;
  10740. }
  10741. /*================================================
  10742. * Check how many items/cards in the list are
  10743. * equipped - used for 2/15's cards patch [celest]
  10744. *------------------------------------------------*/
  10745. BUILDIN_FUNC(isequippedcnt)
  10746. {
  10747. TBL_PC *sd;
  10748. int i, j, k, id = 1;
  10749. int ret = 0;
  10750. sd = script_rid2sd(st);
  10751. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10752. script_pushint(st,0);
  10753. return 0;
  10754. }
  10755. for (i=0; id!=0; i++) {
  10756. FETCH (i+2, id) else id = 0;
  10757. if (id <= 0)
  10758. continue;
  10759. for (j=0; j<EQI_MAX; j++) {
  10760. int index;
  10761. index = sd->equip_index[j];
  10762. if(index < 0) continue;
  10763. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10764. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10765. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10766. if(!sd->inventory_data[index])
  10767. continue;
  10768. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10769. if (sd->inventory_data[index]->nameid == id)
  10770. ret+= sd->status.inventory[index].amount;
  10771. } else { //Count cards.
  10772. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10773. continue; //No cards
  10774. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10775. if (sd->status.inventory[index].card[k] == id)
  10776. ret++; //[Lupus]
  10777. }
  10778. }
  10779. }
  10780. }
  10781. script_pushint(st,ret);
  10782. return 0;
  10783. }
  10784. /*================================================
  10785. * Check whether another card has been
  10786. * equipped - used for 2/15's cards patch [celest]
  10787. * -- Items checked cannot be reused in another
  10788. * card set to prevent exploits
  10789. *------------------------------------------------*/
  10790. BUILDIN_FUNC(isequipped)
  10791. {
  10792. TBL_PC *sd;
  10793. int i, j, k, id = 1;
  10794. int index, flag;
  10795. int ret = -1;
  10796. //Original hash to reverse it when full check fails.
  10797. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10798. sd = script_rid2sd(st);
  10799. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10800. script_pushint(st,0);
  10801. return 0;
  10802. }
  10803. setitem_hash = sd->setitem_hash;
  10804. setitem_hash2 = sd->setitem_hash2;
  10805. for (i=0; id!=0; i++)
  10806. {
  10807. FETCH (i+2, id) else id = 0;
  10808. if (id <= 0)
  10809. continue;
  10810. flag = 0;
  10811. for (j=0; j<EQI_MAX; j++)
  10812. {
  10813. index = sd->equip_index[j];
  10814. if(index < 0) continue;
  10815. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10816. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10817. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10818. if(!sd->inventory_data[index])
  10819. continue;
  10820. if (itemdb_type(id) != IT_CARD) {
  10821. if (sd->inventory_data[index]->nameid != id)
  10822. continue;
  10823. flag = 1;
  10824. break;
  10825. } else { //Cards
  10826. if (sd->inventory_data[index]->slot == 0 ||
  10827. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10828. continue;
  10829. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10830. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10831. unsigned int hash = 0;
  10832. if (sd->status.inventory[index].card[k] != id)
  10833. continue;
  10834. hash = 1<<((j<5?j:j-5)*4 + k);
  10835. // check if card is already used by another set
  10836. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10837. continue;
  10838. // We have found a match
  10839. flag = 1;
  10840. // Set hash so this card cannot be used by another
  10841. if (j<5)
  10842. sd->setitem_hash |= hash;
  10843. else
  10844. sd->setitem_hash2 |= hash;
  10845. break;
  10846. }
  10847. }
  10848. if (flag) break; //Card found
  10849. }
  10850. if (ret == -1)
  10851. ret = flag;
  10852. else
  10853. ret &= flag;
  10854. if (!ret) break;
  10855. }
  10856. if (!ret)
  10857. { //When check fails, restore original hash values. [Skotlex]
  10858. sd->setitem_hash = setitem_hash;
  10859. sd->setitem_hash2 = setitem_hash2;
  10860. }
  10861. script_pushint(st,ret);
  10862. return 0;
  10863. }
  10864. /*================================================
  10865. * Check how many given inserted cards in the CURRENT
  10866. * weapon - used for 2/15's cards patch [Lupus]
  10867. *------------------------------------------------*/
  10868. BUILDIN_FUNC(cardscnt)
  10869. {
  10870. TBL_PC *sd;
  10871. int i, k, id = 1;
  10872. int ret = 0;
  10873. int index;
  10874. sd = script_rid2sd(st);
  10875. for (i=0; id!=0; i++) {
  10876. FETCH (i+2, id) else id = 0;
  10877. if (id <= 0)
  10878. continue;
  10879. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10880. if(index < 0) continue;
  10881. if(!sd->inventory_data[index])
  10882. continue;
  10883. if(itemdb_type(id) != IT_CARD) {
  10884. if (sd->inventory_data[index]->nameid == id)
  10885. ret+= sd->status.inventory[index].amount;
  10886. } else {
  10887. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10888. continue;
  10889. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10890. if (sd->status.inventory[index].card[k] == id)
  10891. ret++;
  10892. }
  10893. }
  10894. }
  10895. script_pushint(st,ret);
  10896. // script_pushint(st,current_equip_item_index);
  10897. return 0;
  10898. }
  10899. /*=======================================================
  10900. * Returns the refined number of the current item, or an
  10901. * item with inventory index specified
  10902. *-------------------------------------------------------*/
  10903. BUILDIN_FUNC(getrefine)
  10904. {
  10905. TBL_PC *sd;
  10906. if ((sd = script_rid2sd(st))!= NULL)
  10907. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10908. else
  10909. script_pushint(st,0);
  10910. return 0;
  10911. }
  10912. /*=======================================================
  10913. * Day/Night controls
  10914. *-------------------------------------------------------*/
  10915. BUILDIN_FUNC(night)
  10916. {
  10917. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10918. return 0;
  10919. }
  10920. BUILDIN_FUNC(day)
  10921. {
  10922. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10923. return 0;
  10924. }
  10925. //=======================================================
  10926. // Unequip [Spectre]
  10927. //-------------------------------------------------------
  10928. BUILDIN_FUNC(unequip)
  10929. {
  10930. int i;
  10931. size_t num;
  10932. TBL_PC *sd;
  10933. num = script_getnum(st,2);
  10934. sd = script_rid2sd(st);
  10935. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10936. {
  10937. i = pc_checkequip(sd,equip[num-1]);
  10938. if (i >= 0)
  10939. pc_unequipitem(sd,i,1|2);
  10940. }
  10941. return 0;
  10942. }
  10943. BUILDIN_FUNC(equip)
  10944. {
  10945. int nameid=0,i;
  10946. TBL_PC *sd;
  10947. struct item_data *item_data;
  10948. sd = script_rid2sd(st);
  10949. nameid=script_getnum(st,2);
  10950. if((item_data = itemdb_exists(nameid)) == NULL)
  10951. {
  10952. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10953. return 1;
  10954. }
  10955. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10956. if( i < MAX_INVENTORY )
  10957. pc_equipitem(sd,i,item_data->equip);
  10958. return 0;
  10959. }
  10960. BUILDIN_FUNC(autoequip)
  10961. {
  10962. int nameid, flag;
  10963. struct item_data *item_data;
  10964. nameid=script_getnum(st,2);
  10965. flag=script_getnum(st,3);
  10966. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  10967. {
  10968. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  10969. return 1;
  10970. }
  10971. if( !itemdb_isequip2(item_data) )
  10972. {
  10973. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  10974. return 1;
  10975. }
  10976. item_data->flag.autoequip = flag>0?1:0;
  10977. return 0;
  10978. }
  10979. BUILDIN_FUNC(setbattleflag)
  10980. {
  10981. const char *flag, *value;
  10982. flag = script_getstr(st,2);
  10983. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  10984. if (battle_set_value(flag, value) == 0)
  10985. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10986. else
  10987. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10988. return 0;
  10989. }
  10990. BUILDIN_FUNC(getbattleflag)
  10991. {
  10992. const char *flag;
  10993. flag = script_getstr(st,2);
  10994. script_pushint(st,battle_get_value(flag));
  10995. return 0;
  10996. }
  10997. //=======================================================
  10998. // strlen [Valaris]
  10999. //-------------------------------------------------------
  11000. BUILDIN_FUNC(getstrlen)
  11001. {
  11002. const char *str = script_getstr(st,2);
  11003. int len = (str) ? (int)strlen(str) : 0;
  11004. script_pushint(st,len);
  11005. return 0;
  11006. }
  11007. //=======================================================
  11008. // isalpha [Valaris]
  11009. //-------------------------------------------------------
  11010. BUILDIN_FUNC(charisalpha)
  11011. {
  11012. const char *str=script_getstr(st,2);
  11013. int pos=script_getnum(st,3);
  11014. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  11015. script_pushint(st,val);
  11016. return 0;
  11017. }
  11018. /// Changes the display name and/or display class of the npc.
  11019. /// Returns 0 is successful, 1 if the npc does not exist.
  11020. ///
  11021. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  11022. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  11023. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  11024. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  11025. BUILDIN_FUNC(setnpcdisplay)
  11026. {
  11027. const char* name;
  11028. const char* newname = NULL;
  11029. int class_ = -1, size = -1;
  11030. struct script_data* data;
  11031. struct npc_data* nd;
  11032. name = script_getstr(st,2);
  11033. data = script_getdata(st,3);
  11034. if( script_hasdata(st,4) )
  11035. class_ = script_getnum(st,4);
  11036. if( script_hasdata(st,5) )
  11037. size = script_getnum(st,5);
  11038. get_val(st, data);
  11039. if( data_isstring(data) )
  11040. newname = conv_str(st,data);
  11041. else if( data_isint(data) )
  11042. class_ = conv_num(st,data);
  11043. else
  11044. {
  11045. ShowError("script:setnpcdisplay: expected a string or number\n");
  11046. script_reportdata(data);
  11047. return 1;
  11048. }
  11049. nd = npc_name2id(name);
  11050. if( nd == NULL )
  11051. {// not found
  11052. script_pushint(st,1);
  11053. return 0;
  11054. }
  11055. // update npc
  11056. if( newname )
  11057. npc_setdisplayname(nd, newname);
  11058. if( size != -1 && size != (int)nd->size )
  11059. nd->size = size;
  11060. else
  11061. size = -1;
  11062. if( class_ != -1 && nd->class_ != class_ )
  11063. npc_setclass(nd, class_);
  11064. else if( size != -1 )
  11065. { // Required to update the visual size
  11066. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  11067. clif_spawn(&nd->bl);
  11068. }
  11069. script_pushint(st,0);
  11070. return 0;
  11071. }
  11072. BUILDIN_FUNC(atoi)
  11073. {
  11074. const char *value;
  11075. value = script_getstr(st,2);
  11076. script_pushint(st,atoi(value));
  11077. return 0;
  11078. }
  11079. // case-insensitive substring search [lordalfa]
  11080. BUILDIN_FUNC(compare)
  11081. {
  11082. const char *message;
  11083. const char *cmpstring;
  11084. message = script_getstr(st,2);
  11085. cmpstring = script_getstr(st,3);
  11086. script_pushint(st,(stristr(message,cmpstring) != NULL));
  11087. return 0;
  11088. }
  11089. // [zBuffer] List of mathematics commands --->
  11090. BUILDIN_FUNC(sqrt)
  11091. {
  11092. double i, a;
  11093. i = script_getnum(st,2);
  11094. a = sqrt(i);
  11095. script_pushint(st,(int)a);
  11096. return 0;
  11097. }
  11098. BUILDIN_FUNC(pow)
  11099. {
  11100. double i, a, b;
  11101. a = script_getnum(st,2);
  11102. b = script_getnum(st,3);
  11103. i = pow(a,b);
  11104. script_pushint(st,(int)i);
  11105. return 0;
  11106. }
  11107. BUILDIN_FUNC(distance)
  11108. {
  11109. int x0, y0, x1, y1;
  11110. x0 = script_getnum(st,2);
  11111. y0 = script_getnum(st,3);
  11112. x1 = script_getnum(st,4);
  11113. y1 = script_getnum(st,5);
  11114. script_pushint(st,distance_xy(x0,y0,x1,y1));
  11115. return 0;
  11116. }
  11117. // <--- [zBuffer] List of mathematics commands
  11118. BUILDIN_FUNC(md5)
  11119. {
  11120. const char *tmpstr;
  11121. char *md5str;
  11122. tmpstr = script_getstr(st,2);
  11123. md5str = (char *)aMallocA((32+1)*sizeof(char));
  11124. MD5_String(tmpstr, md5str);
  11125. script_pushstr(st, md5str);
  11126. return 0;
  11127. }
  11128. // [zBuffer] List of dynamic var commands --->
  11129. BUILDIN_FUNC(setd)
  11130. {
  11131. TBL_PC *sd=NULL;
  11132. char varname[100];
  11133. const char *buffer;
  11134. int elem;
  11135. buffer = script_getstr(st, 2);
  11136. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  11137. elem = 0;
  11138. if( not_server_variable(*varname) )
  11139. {
  11140. sd = script_rid2sd(st);
  11141. if( sd == NULL )
  11142. {
  11143. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  11144. return 0;
  11145. }
  11146. }
  11147. if( is_string_variable(varname) ) {
  11148. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  11149. } else {
  11150. setd_sub(st, sd, varname, elem, (void *)script_getnum(st, 3), NULL);
  11151. }
  11152. return 0;
  11153. }
  11154. #ifndef TXT_ONLY
  11155. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  11156. {
  11157. int i, j;
  11158. TBL_PC* sd = NULL;
  11159. const char* query;
  11160. struct script_data* data;
  11161. const char* name;
  11162. int max_rows = SCRIPT_MAX_ARRAYSIZE;// maximum number of rows
  11163. int num_vars;
  11164. int num_cols;
  11165. // check target variables
  11166. for( i = 3; script_hasdata(st,i); ++i )
  11167. {
  11168. data = script_getdata(st, i);
  11169. if( data_isreference(data) && reference_tovariable(data) )
  11170. {// it's a variable
  11171. name = reference_getname(data);
  11172. if( not_server_variable(*name) && sd == NULL )
  11173. {// requires a player
  11174. sd = script_rid2sd(st);
  11175. if( sd == NULL )
  11176. {// no player attached
  11177. script_reportdata(data);
  11178. st->state = END;
  11179. return 1;
  11180. }
  11181. }
  11182. if( not_array_variable(*name) )
  11183. max_rows = 1;// not an array, limit to one row
  11184. }
  11185. else
  11186. {
  11187. ShowError("script:query_sql: not a variable\n");
  11188. script_reportdata(data);
  11189. st->state = END;
  11190. return 1;
  11191. }
  11192. }
  11193. num_vars = i - 3;
  11194. // Execute the query
  11195. query = script_getstr(st,2);
  11196. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  11197. {
  11198. Sql_ShowDebug(handle);
  11199. script_pushint(st, 0);
  11200. return 1;
  11201. }
  11202. if( Sql_NumRows(handle) == 0 )
  11203. {// No data received
  11204. Sql_FreeResult(handle);
  11205. script_pushint(st, 0);
  11206. return 0;
  11207. }
  11208. // Count the number of columns to store
  11209. num_cols = Sql_NumColumns(handle);
  11210. if( num_vars < num_cols )
  11211. {
  11212. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  11213. script_reportsrc(st);
  11214. }
  11215. else if( num_vars > num_cols )
  11216. {
  11217. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  11218. script_reportsrc(st);
  11219. }
  11220. // Store data
  11221. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  11222. {
  11223. for( j = 0; j < num_vars; ++j )
  11224. {
  11225. char* str = NULL;
  11226. if( j < num_cols )
  11227. Sql_GetData(handle, j, &str, NULL);
  11228. data = script_getdata(st, j+3);
  11229. name = reference_getname(data);
  11230. if( is_string_variable(name) )
  11231. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  11232. else
  11233. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  11234. }
  11235. }
  11236. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  11237. {
  11238. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  11239. script_reportsrc(st);
  11240. }
  11241. // Free data
  11242. Sql_FreeResult(handle);
  11243. script_pushint(st, i);
  11244. return 0;
  11245. }
  11246. #endif
  11247. BUILDIN_FUNC(query_sql)
  11248. {
  11249. #ifndef TXT_ONLY
  11250. return buildin_query_sql_sub(st, mmysql_handle);
  11251. #else
  11252. //for TXT version, we always return -1
  11253. script_pushint(st,-1);
  11254. return 0;
  11255. #endif
  11256. }
  11257. BUILDIN_FUNC(query_logsql)
  11258. {
  11259. #ifndef TXT_ONLY
  11260. if( !log_config.sql_logs )
  11261. {// logmysql_handle == NULL
  11262. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  11263. script_pushint(st,-1);
  11264. return 1;
  11265. }
  11266. return buildin_query_sql_sub(st, logmysql_handle);
  11267. #else
  11268. //for TXT version, we always return -1
  11269. script_pushint(st,-1);
  11270. return 0;
  11271. #endif
  11272. }
  11273. //Allows escaping of a given string.
  11274. BUILDIN_FUNC(escape_sql)
  11275. {
  11276. const char *str;
  11277. char *esc_str;
  11278. size_t len;
  11279. str = script_getstr(st,2);
  11280. len = strlen(str);
  11281. esc_str = (char*)aMallocA(len*2+1);
  11282. #if defined(TXT_ONLY)
  11283. jstrescapecpy(esc_str, str);
  11284. #else
  11285. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  11286. #endif
  11287. script_pushstr(st, esc_str);
  11288. return 0;
  11289. }
  11290. BUILDIN_FUNC(getd)
  11291. {
  11292. char varname[100];
  11293. const char *buffer;
  11294. int elem;
  11295. buffer = script_getstr(st, 2);
  11296. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  11297. elem = 0;
  11298. // Push the 'pointer' so it's more flexible [Lance]
  11299. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  11300. return 0;
  11301. }
  11302. // <--- [zBuffer] List of dynamic var commands
  11303. // Pet stat [Lance]
  11304. BUILDIN_FUNC(petstat)
  11305. {
  11306. TBL_PC *sd = NULL;
  11307. struct pet_data *pd;
  11308. int flag = script_getnum(st,2);
  11309. sd = script_rid2sd(st);
  11310. if(!sd || !sd->status.pet_id || !sd->pd){
  11311. if(flag == 2)
  11312. script_pushconststr(st, "");
  11313. else
  11314. script_pushint(st,0);
  11315. return 0;
  11316. }
  11317. pd = sd->pd;
  11318. switch(flag){
  11319. case 1: script_pushint(st,(int)pd->pet.class_); break;
  11320. case 2: script_pushstrcopy(st, pd->pet.name); break;
  11321. case 3: script_pushint(st,(int)pd->pet.level); break;
  11322. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  11323. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  11324. default:
  11325. script_pushint(st,0);
  11326. break;
  11327. }
  11328. return 0;
  11329. }
  11330. BUILDIN_FUNC(callshop)
  11331. {
  11332. TBL_PC *sd = NULL;
  11333. struct npc_data *nd;
  11334. const char *shopname;
  11335. int flag = 0;
  11336. sd = script_rid2sd(st);
  11337. if (!sd) {
  11338. script_pushint(st,0);
  11339. return 0;
  11340. }
  11341. shopname = script_getstr(st, 2);
  11342. if( script_hasdata(st,3) )
  11343. flag = script_getnum(st,3);
  11344. nd = npc_name2id(shopname);
  11345. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  11346. {
  11347. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  11348. script_pushint(st,0);
  11349. return 1;
  11350. }
  11351. if( nd->subtype == SHOP )
  11352. {
  11353. switch( flag )
  11354. {
  11355. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  11356. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  11357. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  11358. }
  11359. }
  11360. else
  11361. clif_cashshop_show(sd, nd);
  11362. sd->npc_shopid = nd->bl.id;
  11363. script_pushint(st,1);
  11364. return 0;
  11365. }
  11366. BUILDIN_FUNC(npcshopitem)
  11367. {
  11368. const char* npcname = script_getstr(st, 2);
  11369. struct npc_data* nd = npc_name2id(npcname);
  11370. int n, i;
  11371. int amount;
  11372. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  11373. { //Not found.
  11374. script_pushint(st,0);
  11375. return 0;
  11376. }
  11377. // get the count of new entries
  11378. amount = (script_lastdata(st)-2)/2;
  11379. // generate new shop item list
  11380. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  11381. for( n = 0, i = 3; n < amount; n++, i+=2 )
  11382. {
  11383. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  11384. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  11385. }
  11386. nd->u.shop.count = n;
  11387. script_pushint(st,1);
  11388. return 0;
  11389. }
  11390. BUILDIN_FUNC(npcshopadditem)
  11391. {
  11392. const char* npcname = script_getstr(st,2);
  11393. struct npc_data* nd = npc_name2id(npcname);
  11394. int n, i;
  11395. int amount;
  11396. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  11397. { //Not found.
  11398. script_pushint(st,0);
  11399. return 0;
  11400. }
  11401. // get the count of new entries
  11402. amount = (script_lastdata(st)-2)/2;
  11403. // append new items to existing shop item list
  11404. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  11405. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  11406. {
  11407. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  11408. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  11409. }
  11410. nd->u.shop.count = n;
  11411. script_pushint(st,1);
  11412. return 0;
  11413. }
  11414. BUILDIN_FUNC(npcshopdelitem)
  11415. {
  11416. const char* npcname = script_getstr(st,2);
  11417. struct npc_data* nd = npc_name2id(npcname);
  11418. unsigned int nameid;
  11419. int n, i;
  11420. int amount;
  11421. int size;
  11422. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  11423. { //Not found.
  11424. script_pushint(st,0);
  11425. return 0;
  11426. }
  11427. amount = script_lastdata(st)-2;
  11428. size = nd->u.shop.count;
  11429. // remove specified items from the shop item list
  11430. for( i = 3; i < 3 + amount; i++ )
  11431. {
  11432. nameid = script_getnum(st,i);
  11433. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  11434. if( n < size )
  11435. {
  11436. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  11437. size--;
  11438. }
  11439. }
  11440. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  11441. nd->u.shop.count = size;
  11442. script_pushint(st,1);
  11443. return 0;
  11444. }
  11445. //Sets a script to attach to a shop npc.
  11446. BUILDIN_FUNC(npcshopattach)
  11447. {
  11448. const char* npcname = script_getstr(st,2);
  11449. struct npc_data* nd = npc_name2id(npcname);
  11450. int flag = 1;
  11451. if( script_hasdata(st,3) )
  11452. flag = script_getnum(st,3);
  11453. if( !nd || nd->subtype != SHOP )
  11454. { //Not found.
  11455. script_pushint(st,0);
  11456. return 0;
  11457. }
  11458. if (flag)
  11459. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  11460. else
  11461. nd->master_nd = NULL;
  11462. script_pushint(st,1);
  11463. return 0;
  11464. }
  11465. /*==========================================
  11466. * Returns some values of an item [Lupus]
  11467. * Price, Weight, etc...
  11468. setitemscript(itemID,"{new item bonus script}",[n]);
  11469. Where n:
  11470. 0 - script
  11471. 1 - Equip script
  11472. 2 - Unequip script
  11473. *------------------------------------------*/
  11474. BUILDIN_FUNC(setitemscript)
  11475. {
  11476. int item_id,n=0;
  11477. const char *script;
  11478. struct item_data *i_data;
  11479. struct script_code **dstscript;
  11480. item_id = script_getnum(st,2);
  11481. script = script_getstr(st,3);
  11482. if( script_hasdata(st,4) )
  11483. n=script_getnum(st,4);
  11484. i_data = itemdb_exists(item_id);
  11485. if (!i_data || script==NULL || script[0]!='{') {
  11486. script_pushint(st,0);
  11487. return 0;
  11488. }
  11489. switch (n) {
  11490. case 2:
  11491. dstscript = &i_data->unequip_script;
  11492. break;
  11493. case 1:
  11494. dstscript = &i_data->equip_script;
  11495. break;
  11496. default:
  11497. dstscript = &i_data->script;
  11498. break;
  11499. }
  11500. if(*dstscript)
  11501. script_free_code(*dstscript);
  11502. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11503. script_pushint(st,1);
  11504. return 0;
  11505. }
  11506. /* Work In Progress [Lupus]
  11507. BUILDIN_FUNC(addmonsterdrop)
  11508. {
  11509. int class_,item_id,chance;
  11510. class_=script_getnum(st,2);
  11511. item_id=script_getnum(st,3);
  11512. chance=script_getnum(st,4);
  11513. if(class_>1000 && item_id>500 && chance>0) {
  11514. script_pushint(st,1);
  11515. } else {
  11516. script_pushint(st,0);
  11517. }
  11518. }
  11519. BUILDIN_FUNC(delmonsterdrop)
  11520. {
  11521. int class_,item_id;
  11522. class_=script_getnum(st,2);
  11523. item_id=script_getnum(st,3);
  11524. if(class_>1000 && item_id>500) {
  11525. script_pushint(st,1);
  11526. } else {
  11527. script_pushint(st,0);
  11528. }
  11529. }
  11530. */
  11531. /*==========================================
  11532. * Returns some values of a monster [Lupus]
  11533. * Name, Level, race, size, etc...
  11534. getmonsterinfo(monsterID,queryIndex);
  11535. *------------------------------------------*/
  11536. BUILDIN_FUNC(getmonsterinfo)
  11537. {
  11538. struct mob_db *mob;
  11539. int mob_id;
  11540. mob_id = script_getnum(st,2);
  11541. if (!mobdb_checkid(mob_id)) {
  11542. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  11543. if ( !script_getnum(st,3) ) //requested a string
  11544. script_pushconststr(st,"null");
  11545. else
  11546. script_pushint(st,-1);
  11547. return -1;
  11548. }
  11549. mob = mob_db(mob_id);
  11550. switch ( script_getnum(st,3) ) {
  11551. case 0: script_pushstrcopy(st,mob->jname); break;
  11552. case 1: script_pushint(st,mob->lv); break;
  11553. case 2: script_pushint(st,mob->status.max_hp); break;
  11554. case 3: script_pushint(st,mob->base_exp); break;
  11555. case 4: script_pushint(st,mob->job_exp); break;
  11556. case 5: script_pushint(st,mob->status.rhw.atk); break;
  11557. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  11558. case 7: script_pushint(st,mob->status.def); break;
  11559. case 8: script_pushint(st,mob->status.mdef); break;
  11560. case 9: script_pushint(st,mob->status.str); break;
  11561. case 10: script_pushint(st,mob->status.agi); break;
  11562. case 11: script_pushint(st,mob->status.vit); break;
  11563. case 12: script_pushint(st,mob->status.int_); break;
  11564. case 13: script_pushint(st,mob->status.dex); break;
  11565. case 14: script_pushint(st,mob->status.luk); break;
  11566. case 15: script_pushint(st,mob->status.rhw.range); break;
  11567. case 16: script_pushint(st,mob->range2); break;
  11568. case 17: script_pushint(st,mob->range3); break;
  11569. case 18: script_pushint(st,mob->status.size); break;
  11570. case 19: script_pushint(st,mob->status.race); break;
  11571. case 20: script_pushint(st,mob->status.def_ele); break;
  11572. case 21: script_pushint(st,mob->status.mode); break;
  11573. default: script_pushint(st,-1); //wrong Index
  11574. }
  11575. return 0;
  11576. }
  11577. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11578. {
  11579. TBL_PC *sd = NULL;
  11580. if(script_hasdata(st,2))
  11581. sd = map_nick2sd(script_getstr(st,2));
  11582. else
  11583. sd = script_rid2sd(st);
  11584. if(sd)
  11585. script_pushint(st,sd->state.vending);
  11586. else
  11587. script_pushint(st,0);
  11588. return 0;
  11589. }
  11590. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11591. {
  11592. TBL_PC *sd = NULL;
  11593. if(script_hasdata(st,2))
  11594. sd = map_nick2sd(script_getstr(st,2));
  11595. else
  11596. sd = script_rid2sd(st);
  11597. if(sd)
  11598. script_pushint(st,(sd->chatID != 0));
  11599. else
  11600. script_pushint(st,0);
  11601. return 0;
  11602. }
  11603. BUILDIN_FUNC(searchitem)
  11604. {
  11605. struct script_data* data = script_getdata(st, 2);
  11606. const char *itemname = script_getstr(st,3);
  11607. struct item_data *items[MAX_SEARCH];
  11608. int count;
  11609. char* name;
  11610. int32 start;
  11611. int32 id;
  11612. int32 i;
  11613. TBL_PC* sd = NULL;
  11614. if ((items[0] = itemdb_exists(atoi(itemname))))
  11615. count = 1;
  11616. else {
  11617. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  11618. if (count > MAX_SEARCH) count = MAX_SEARCH;
  11619. }
  11620. if (!count) {
  11621. script_pushint(st, 0);
  11622. return 0;
  11623. }
  11624. if( !data_isreference(data) )
  11625. {
  11626. ShowError("script:searchitem: not a variable\n");
  11627. script_reportdata(data);
  11628. st->state = END;
  11629. return 1;// not a variable
  11630. }
  11631. id = reference_getid(data);
  11632. start = reference_getindex(data);
  11633. name = reference_getname(data);
  11634. if( not_array_variable(*name) )
  11635. {
  11636. ShowError("script:searchitem: illegal scope\n");
  11637. script_reportdata(data);
  11638. st->state = END;
  11639. return 1;// not supported
  11640. }
  11641. if( not_server_variable(*name) )
  11642. {
  11643. sd = script_rid2sd(st);
  11644. if( sd == NULL )
  11645. return 0;// no player attached
  11646. }
  11647. if( is_string_variable(name) )
  11648. {// string array
  11649. ShowError("script:searchitem: not an integer array reference\n");
  11650. script_reportdata(data);
  11651. st->state = END;
  11652. return 1;// not supported
  11653. }
  11654. for( i = 0; i < count; ++start, ++i )
  11655. {// Set array
  11656. void* v = (void*)items[i]->nameid;
  11657. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  11658. }
  11659. script_pushint(st, count);
  11660. return 0;
  11661. }
  11662. int axtoi(const char *hexStg)
  11663. {
  11664. int n = 0; // position in string
  11665. int m = 0; // position in digit[] to shift
  11666. int count; // loop index
  11667. int intValue = 0; // integer value of hex string
  11668. int digit[11]; // hold values to convert
  11669. while (n < 10) {
  11670. if (hexStg[n]=='\0')
  11671. break;
  11672. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11673. digit[n] = hexStg[n] & 0x0f; //convert to int
  11674. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11675. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11676. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11677. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11678. else break;
  11679. n++;
  11680. }
  11681. count = n;
  11682. m = n - 1;
  11683. n = 0;
  11684. while(n < count) {
  11685. // digit[n] is value of hex digit at position n
  11686. // (m << 2) is the number of positions to shift
  11687. // OR the bits into return value
  11688. intValue = intValue | (digit[n] << (m << 2));
  11689. m--; // adjust the position to set
  11690. n++; // next digit to process
  11691. }
  11692. return (intValue);
  11693. }
  11694. // [Lance] Hex string to integer converter
  11695. BUILDIN_FUNC(axtoi)
  11696. {
  11697. const char *hex = script_getstr(st,2);
  11698. script_pushint(st,axtoi(hex));
  11699. return 0;
  11700. }
  11701. // [zBuffer] List of player cont commands --->
  11702. BUILDIN_FUNC(rid2name)
  11703. {
  11704. struct block_list *bl = NULL;
  11705. int rid = script_getnum(st,2);
  11706. if((bl = map_id2bl(rid)))
  11707. {
  11708. switch(bl->type) {
  11709. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  11710. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  11711. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  11712. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  11713. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  11714. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  11715. default:
  11716. ShowError("buildin_rid2name: BL type unknown.\n");
  11717. script_pushconststr(st,"");
  11718. break;
  11719. }
  11720. } else {
  11721. ShowError("buildin_rid2name: invalid RID\n");
  11722. script_pushconststr(st,"(null)");
  11723. }
  11724. return 0;
  11725. }
  11726. BUILDIN_FUNC(pcblockmove)
  11727. {
  11728. int id, flag;
  11729. TBL_PC *sd = NULL;
  11730. id = script_getnum(st,2);
  11731. flag = script_getnum(st,3);
  11732. if(id)
  11733. sd = map_id2sd(id);
  11734. else
  11735. sd = script_rid2sd(st);
  11736. if(sd)
  11737. sd->state.blockedmove = flag > 0;
  11738. return 0;
  11739. }
  11740. BUILDIN_FUNC(pcfollow)
  11741. {
  11742. int id, targetid;
  11743. TBL_PC *sd = NULL;
  11744. id = script_getnum(st,2);
  11745. targetid = script_getnum(st,3);
  11746. if(id)
  11747. sd = map_id2sd(id);
  11748. else
  11749. sd = script_rid2sd(st);
  11750. if(sd)
  11751. pc_follow(sd, targetid);
  11752. return 0;
  11753. }
  11754. BUILDIN_FUNC(pcstopfollow)
  11755. {
  11756. int id;
  11757. TBL_PC *sd = NULL;
  11758. id = script_getnum(st,2);
  11759. if(id)
  11760. sd = map_id2sd(id);
  11761. else
  11762. sd = script_rid2sd(st);
  11763. if(sd)
  11764. pc_stop_following(sd);
  11765. return 0;
  11766. }
  11767. // <--- [zBuffer] List of player cont commands
  11768. // [zBuffer] List of mob control commands --->
  11769. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11770. /// Makes the unit walk to target position or map
  11771. /// Returns if it was successfull
  11772. ///
  11773. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11774. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11775. BUILDIN_FUNC(unitwalk)
  11776. {
  11777. struct block_list* bl;
  11778. bl = map_id2bl(script_getnum(st,2));
  11779. if( bl == NULL )
  11780. {
  11781. script_pushint(st, 0);
  11782. }
  11783. else if( script_hasdata(st,4) )
  11784. {
  11785. int x = script_getnum(st,3);
  11786. int y = script_getnum(st,4);
  11787. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11788. }
  11789. else
  11790. {
  11791. int map_id = script_getnum(st,3);
  11792. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11793. }
  11794. return 0;
  11795. }
  11796. /// Kills the unit
  11797. ///
  11798. /// unitkill <unit_id>;
  11799. BUILDIN_FUNC(unitkill)
  11800. {
  11801. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11802. if( bl != NULL )
  11803. status_kill(bl);
  11804. return 0;
  11805. }
  11806. /// Warps the unit to the target position in the target map
  11807. /// Returns if it was successfull
  11808. ///
  11809. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11810. BUILDIN_FUNC(unitwarp)
  11811. {
  11812. int unit_id;
  11813. int map;
  11814. short x;
  11815. short y;
  11816. struct block_list* bl;
  11817. const char *mapname;
  11818. unit_id = script_getnum(st,2);
  11819. mapname = script_getstr(st, 3);
  11820. x = (short)script_getnum(st,4);
  11821. y = (short)script_getnum(st,5);
  11822. if (!unit_id) //Warp the script's runner
  11823. bl = map_id2bl(st->rid);
  11824. else
  11825. bl = map_id2bl(unit_id);
  11826. if( strcmp(mapname,"this") == 0 )
  11827. map = bl?bl->m:-1;
  11828. else
  11829. map = map_mapname2mapid(mapname);
  11830. if( map >= 0 && bl != NULL )
  11831. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  11832. else
  11833. script_pushint(st, 0);
  11834. return 0;
  11835. }
  11836. /// Makes the unit attack the target.
  11837. /// If the unit is a player and <action type> is not 0, it does a continuous
  11838. /// attack instead of a single attack.
  11839. /// Returns if the request was successfull.
  11840. ///
  11841. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11842. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11843. BUILDIN_FUNC(unitattack)
  11844. {
  11845. struct block_list* unit_bl;
  11846. struct block_list* target_bl = NULL;
  11847. struct script_data* data;
  11848. int actiontype = 0;
  11849. // get unit
  11850. unit_bl = map_id2bl(script_getnum(st,2));
  11851. if( unit_bl == NULL ) {
  11852. script_pushint(st, 0);
  11853. return 0;
  11854. }
  11855. data = script_getdata(st, 3);
  11856. get_val(st, data);
  11857. if( data_isstring(data) )
  11858. {
  11859. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11860. if( sd != NULL )
  11861. target_bl = &sd->bl;
  11862. } else
  11863. target_bl = map_id2bl(conv_num(st, data));
  11864. // request the attack
  11865. if( target_bl == NULL )
  11866. {
  11867. script_pushint(st, 0);
  11868. return 0;
  11869. }
  11870. // get actiontype
  11871. if( script_hasdata(st,4) )
  11872. actiontype = script_getnum(st,4);
  11873. switch( unit_bl->type )
  11874. {
  11875. case BL_PC:
  11876. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11877. script_pushint(st, 1);
  11878. return 0;
  11879. case BL_MOB:
  11880. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11881. break;
  11882. case BL_PET:
  11883. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11884. break;
  11885. default:
  11886. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11887. script_pushint(st, 0);
  11888. return 1;
  11889. }
  11890. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11891. return 0;
  11892. }
  11893. /// Makes the unit stop attacking and moving
  11894. ///
  11895. /// unitstop <unit_id>;
  11896. BUILDIN_FUNC(unitstop)
  11897. {
  11898. int unit_id;
  11899. struct block_list* bl;
  11900. unit_id = script_getnum(st,2);
  11901. bl = map_id2bl(unit_id);
  11902. if( bl != NULL )
  11903. {
  11904. unit_stop_attack(bl);
  11905. unit_stop_walking(bl,4);
  11906. if( bl->type == BL_MOB )
  11907. ((TBL_MOB*)bl)->target_id = 0;
  11908. }
  11909. return 0;
  11910. }
  11911. /// Makes the unit say the message
  11912. ///
  11913. /// unittalk <unit_id>,"<message>";
  11914. BUILDIN_FUNC(unittalk)
  11915. {
  11916. int unit_id;
  11917. const char* message;
  11918. struct block_list* bl;
  11919. unit_id = script_getnum(st,2);
  11920. message = script_getstr(st, 3);
  11921. bl = map_id2bl(unit_id);
  11922. if( bl != NULL )
  11923. {
  11924. struct StringBuf sbuf;
  11925. StringBuf_Init(&sbuf);
  11926. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11927. clif_message(bl, StringBuf_Value(&sbuf));
  11928. if( bl->type == BL_PC )
  11929. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11930. StringBuf_Destroy(&sbuf);
  11931. }
  11932. return 0;
  11933. }
  11934. /// Makes the unit do an emotion
  11935. ///
  11936. /// unitemote <unit_id>,<emotion>;
  11937. ///
  11938. /// @see e_* in const.txt
  11939. BUILDIN_FUNC(unitemote)
  11940. {
  11941. int unit_id;
  11942. int emotion;
  11943. struct block_list* bl;
  11944. unit_id = script_getnum(st,2);
  11945. emotion = script_getnum(st,3);
  11946. bl = map_id2bl(unit_id);
  11947. if( bl != NULL )
  11948. clif_emotion(bl, emotion);
  11949. return 0;
  11950. }
  11951. /// Makes the unit cast the skill on the target or self if no target is specified
  11952. ///
  11953. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11954. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  11955. BUILDIN_FUNC(unitskilluseid)
  11956. {
  11957. int unit_id;
  11958. int skill_id;
  11959. int skill_lv;
  11960. int target_id;
  11961. struct block_list* bl;
  11962. unit_id = script_getnum(st,2);
  11963. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11964. skill_lv = script_getnum(st,4);
  11965. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11966. bl = map_id2bl(unit_id);
  11967. if( bl != NULL )
  11968. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11969. return 0;
  11970. }
  11971. /// Makes the unit cast the skill on the target position.
  11972. ///
  11973. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11974. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  11975. BUILDIN_FUNC(unitskillusepos)
  11976. {
  11977. int unit_id;
  11978. int skill_id;
  11979. int skill_lv;
  11980. int skill_x;
  11981. int skill_y;
  11982. struct block_list* bl;
  11983. unit_id = script_getnum(st,2);
  11984. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11985. skill_lv = script_getnum(st,4);
  11986. skill_x = script_getnum(st,5);
  11987. skill_y = script_getnum(st,6);
  11988. bl = map_id2bl(unit_id);
  11989. if( bl != NULL )
  11990. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11991. return 0;
  11992. }
  11993. // <--- [zBuffer] List of mob control commands
  11994. /// Pauses the execution of the script, detaching the player
  11995. ///
  11996. /// sleep <mili seconds>;
  11997. BUILDIN_FUNC(sleep)
  11998. {
  11999. int ticks;
  12000. ticks = script_getnum(st,2);
  12001. // detach the player
  12002. script_detach_rid(st);
  12003. if( ticks <= 0 )
  12004. {// do nothing
  12005. }
  12006. else if( st->sleep.tick == 0 )
  12007. {// sleep for the target amount of time
  12008. st->state = RERUNLINE;
  12009. st->sleep.tick = ticks;
  12010. }
  12011. else
  12012. {// sleep time is over
  12013. st->state = RUN;
  12014. st->sleep.tick = 0;
  12015. }
  12016. return 0;
  12017. }
  12018. /// Pauses the execution of the script, keeping the player attached
  12019. /// Returns if a player is still attached
  12020. ///
  12021. /// sleep2(<mili secconds>) -> <bool>
  12022. BUILDIN_FUNC(sleep2)
  12023. {
  12024. int ticks;
  12025. ticks = script_getnum(st,2);
  12026. if( ticks <= 0 )
  12027. {// do nothing
  12028. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12029. }
  12030. else if( !st->sleep.tick )
  12031. {// sleep for the target amount of time
  12032. st->state = RERUNLINE;
  12033. st->sleep.tick = ticks;
  12034. }
  12035. else
  12036. {// sleep time is over
  12037. st->state = RUN;
  12038. st->sleep.tick = 0;
  12039. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  12040. }
  12041. return 0;
  12042. }
  12043. /// Awakes all the sleep timers of the target npc
  12044. ///
  12045. /// awake "<npc name>";
  12046. BUILDIN_FUNC(awake)
  12047. {
  12048. struct npc_data* nd;
  12049. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  12050. nd = npc_name2id(script_getstr(st, 2));
  12051. if( nd == NULL ) {
  12052. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  12053. return 1;
  12054. }
  12055. while( node )
  12056. {
  12057. if( (int)node->key == nd->bl.id )
  12058. {// sleep timer for the npc
  12059. struct script_state* tst = (struct script_state*)node->data;
  12060. TBL_PC* sd = map_id2sd(tst->rid);
  12061. if( tst->sleep.timer == INVALID_TIMER )
  12062. {// already awake ???
  12063. node = node->next;
  12064. continue;
  12065. }
  12066. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  12067. {// char not online anymore / another char of the same account is online - Cancel execution
  12068. tst->state = END;
  12069. tst->rid = 0;
  12070. }
  12071. delete_timer(tst->sleep.timer, run_script_timer);
  12072. node = script_erase_sleepdb(node);
  12073. tst->sleep.timer = INVALID_TIMER;
  12074. if(tst->state != RERUNLINE)
  12075. tst->sleep.tick = 0;
  12076. run_script_main(tst);
  12077. }
  12078. else
  12079. {
  12080. node = node->next;
  12081. }
  12082. }
  12083. return 0;
  12084. }
  12085. /// Returns a reference to a variable of the target NPC.
  12086. /// Returns 0 if an error occurs.
  12087. ///
  12088. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  12089. BUILDIN_FUNC(getvariableofnpc)
  12090. {
  12091. struct script_data* data;
  12092. const char* name;
  12093. struct npc_data* nd;
  12094. data = script_getdata(st,2);
  12095. if( !data_isreference(data) )
  12096. {// Not a reference (aka varaible name)
  12097. ShowError("script:getvariableofnpc: not a variable\n");
  12098. script_reportdata(data);
  12099. script_pushnil(st);
  12100. st->state = END;
  12101. return 1;
  12102. }
  12103. name = reference_getname(data);
  12104. if( *name != '.' || name[1] == '@' )
  12105. {// not a npc variable
  12106. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  12107. script_reportdata(data);
  12108. script_pushnil(st);
  12109. st->state = END;
  12110. return 1;
  12111. }
  12112. nd = npc_name2id(script_getstr(st,3));
  12113. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  12114. {// NPC not found or has no script
  12115. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  12116. script_pushnil(st);
  12117. st->state = END;
  12118. return 1;
  12119. }
  12120. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  12121. return 0;
  12122. }
  12123. /// Opens a warp portal.
  12124. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  12125. ///
  12126. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  12127. ///
  12128. /// @author blackhole89
  12129. BUILDIN_FUNC(warpportal)
  12130. {
  12131. int spx;
  12132. int spy;
  12133. unsigned short mapindex;
  12134. int tpx;
  12135. int tpy;
  12136. struct skill_unit_group* group;
  12137. struct block_list* bl;
  12138. bl = map_id2bl(st->oid);
  12139. if( bl == NULL )
  12140. {
  12141. ShowError("script:warpportal: npc is needed\n");
  12142. return 1;
  12143. }
  12144. spx = script_getnum(st,2);
  12145. spy = script_getnum(st,3);
  12146. mapindex = mapindex_name2id(script_getstr(st, 4));
  12147. tpx = script_getnum(st,5);
  12148. tpy = script_getnum(st,6);
  12149. if( mapindex == 0 )
  12150. return 0;// map not found
  12151. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  12152. if( group == NULL )
  12153. return 0;// failed
  12154. group->val2 = (tpx<<16) | tpy;
  12155. group->val3 = mapindex;
  12156. return 0;
  12157. }
  12158. BUILDIN_FUNC(openmail)
  12159. {
  12160. TBL_PC* sd;
  12161. sd = script_rid2sd(st);
  12162. if( sd == NULL )
  12163. return 0;
  12164. #ifndef TXT_ONLY
  12165. mail_openmail(sd);
  12166. #endif
  12167. return 0;
  12168. }
  12169. BUILDIN_FUNC(openauction)
  12170. {
  12171. TBL_PC* sd;
  12172. sd = script_rid2sd(st);
  12173. if( sd == NULL )
  12174. return 0;
  12175. #ifndef TXT_ONLY
  12176. clif_Auction_openwindow(sd);
  12177. #endif
  12178. return 0;
  12179. }
  12180. /// Retrieves the value of the specified flag of the specified cell.
  12181. ///
  12182. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  12183. ///
  12184. /// @see cell_chk* constants in const.txt for the types
  12185. BUILDIN_FUNC(checkcell)
  12186. {
  12187. int m = map_mapname2mapid(script_getstr(st,2));
  12188. int x = script_getnum(st,3);
  12189. int y = script_getnum(st,4);
  12190. cell_chk type = (cell_chk)script_getnum(st,5);
  12191. script_pushint(st, map_getcell(m, x, y, type));
  12192. return 0;
  12193. }
  12194. /// Modifies flags of cells in the specified area.
  12195. ///
  12196. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  12197. ///
  12198. /// @see cell_* constants in const.txt for the types
  12199. BUILDIN_FUNC(setcell)
  12200. {
  12201. int m = map_mapname2mapid(script_getstr(st,2));
  12202. int x1 = script_getnum(st,3);
  12203. int y1 = script_getnum(st,4);
  12204. int x2 = script_getnum(st,5);
  12205. int y2 = script_getnum(st,6);
  12206. cell_t type = (cell_t)script_getnum(st,7);
  12207. bool flag = (bool)script_getnum(st,8);
  12208. int x,y;
  12209. if( x1 > x2 ) swap(x1,x2);
  12210. if( y1 > y2 ) swap(y1,y2);
  12211. for( y = y1; y <= y2; ++y )
  12212. for( x = x1; x <= x2; ++x )
  12213. map_setcell(m, x, y, type, flag);
  12214. return 0;
  12215. }
  12216. /*==========================================
  12217. * Mercenary Commands
  12218. *------------------------------------------*/
  12219. BUILDIN_FUNC(mercenary_create)
  12220. {
  12221. struct map_session_data *sd;
  12222. int class_, contract_time;
  12223. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  12224. return 0;
  12225. class_ = script_getnum(st,2);
  12226. if( !merc_class(class_) )
  12227. return 0;
  12228. contract_time = script_getnum(st,3);
  12229. merc_create(sd, class_, contract_time);
  12230. return 0;
  12231. }
  12232. BUILDIN_FUNC(mercenary_heal)
  12233. {
  12234. struct map_session_data *sd = script_rid2sd(st);
  12235. int hp, sp;
  12236. if( sd == NULL || sd->md == NULL )
  12237. return 0;
  12238. hp = script_getnum(st,2);
  12239. sp = script_getnum(st,3);
  12240. status_heal(&sd->md->bl, hp, sp, 0);
  12241. return 0;
  12242. }
  12243. BUILDIN_FUNC(mercenary_sc_start)
  12244. {
  12245. struct map_session_data *sd = script_rid2sd(st);
  12246. enum sc_type type;
  12247. int tick, val1;
  12248. if( sd == NULL || sd->md == NULL )
  12249. return 0;
  12250. type = (sc_type)script_getnum(st,2);
  12251. tick = script_getnum(st,3);
  12252. val1 = script_getnum(st,4);
  12253. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  12254. return 0;
  12255. }
  12256. BUILDIN_FUNC(mercenary_get_calls)
  12257. {
  12258. struct map_session_data *sd = script_rid2sd(st);
  12259. int guild;
  12260. if( sd == NULL )
  12261. return 0;
  12262. guild = script_getnum(st,2);
  12263. switch( guild )
  12264. {
  12265. case ARCH_MERC_GUILD:
  12266. script_pushint(st,sd->status.arch_calls);
  12267. break;
  12268. case SPEAR_MERC_GUILD:
  12269. script_pushint(st,sd->status.spear_calls);
  12270. break;
  12271. case SWORD_MERC_GUILD:
  12272. script_pushint(st,sd->status.sword_calls);
  12273. break;
  12274. default:
  12275. script_pushint(st,0);
  12276. break;
  12277. }
  12278. return 0;
  12279. }
  12280. BUILDIN_FUNC(mercenary_set_calls)
  12281. {
  12282. struct map_session_data *sd = script_rid2sd(st);
  12283. int guild, value, *calls;
  12284. if( sd == NULL )
  12285. return 0;
  12286. guild = script_getnum(st,2);
  12287. value = script_getnum(st,3);
  12288. switch( guild )
  12289. {
  12290. case ARCH_MERC_GUILD:
  12291. calls = &sd->status.arch_calls;
  12292. break;
  12293. case SPEAR_MERC_GUILD:
  12294. calls = &sd->status.spear_calls;
  12295. break;
  12296. case SWORD_MERC_GUILD:
  12297. calls = &sd->status.sword_calls;
  12298. break;
  12299. default:
  12300. return 0; // Invalid Guild
  12301. }
  12302. *calls += value;
  12303. *calls = cap_value(*calls, 0, INT_MAX);
  12304. return 0;
  12305. }
  12306. BUILDIN_FUNC(mercenary_get_faith)
  12307. {
  12308. struct map_session_data *sd = script_rid2sd(st);
  12309. int guild;
  12310. if( sd == NULL )
  12311. return 0;
  12312. guild = script_getnum(st,2);
  12313. switch( guild )
  12314. {
  12315. case ARCH_MERC_GUILD:
  12316. script_pushint(st,sd->status.arch_faith);
  12317. break;
  12318. case SPEAR_MERC_GUILD:
  12319. script_pushint(st,sd->status.spear_faith);
  12320. break;
  12321. case SWORD_MERC_GUILD:
  12322. script_pushint(st,sd->status.sword_faith);
  12323. break;
  12324. default:
  12325. script_pushint(st,0);
  12326. break;
  12327. }
  12328. return 0;
  12329. }
  12330. BUILDIN_FUNC(mercenary_set_faith)
  12331. {
  12332. struct map_session_data *sd = script_rid2sd(st);
  12333. int guild, value, *calls;
  12334. if( sd == NULL )
  12335. return 0;
  12336. guild = script_getnum(st,2);
  12337. value = script_getnum(st,3);
  12338. switch( guild )
  12339. {
  12340. case ARCH_MERC_GUILD:
  12341. calls = &sd->status.arch_faith;
  12342. break;
  12343. case SPEAR_MERC_GUILD:
  12344. calls = &sd->status.spear_faith;
  12345. break;
  12346. case SWORD_MERC_GUILD:
  12347. calls = &sd->status.sword_faith;
  12348. break;
  12349. default:
  12350. return 0; // Invalid Guild
  12351. }
  12352. *calls += value;
  12353. *calls = cap_value(*calls, 0, INT_MAX);
  12354. if( mercenary_get_guild(sd->md) == guild )
  12355. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  12356. return 0;
  12357. }
  12358. /*------------------------------------------
  12359. * Book Reading
  12360. *------------------------------------------*/
  12361. BUILDIN_FUNC(readbook)
  12362. {
  12363. struct map_session_data *sd;
  12364. int book_id, page;
  12365. if( (sd = script_rid2sd(st)) == NULL )
  12366. return 0;
  12367. book_id = script_getnum(st,2);
  12368. page = script_getnum(st,3);
  12369. clif_readbook(sd->fd, book_id, page);
  12370. return 0;
  12371. }
  12372. /******************
  12373. Questlog script commands
  12374. *******************/
  12375. BUILDIN_FUNC(setquest)
  12376. {
  12377. TBL_PC * sd = script_rid2sd(st);
  12378. quest_add(sd, script_getnum(st, 2));
  12379. return 0;
  12380. }
  12381. BUILDIN_FUNC(erasequest)
  12382. {
  12383. TBL_PC * sd = script_rid2sd(st);
  12384. quest_delete(sd, script_getnum(st, 2));
  12385. return 0;
  12386. }
  12387. BUILDIN_FUNC(completequest)
  12388. {
  12389. TBL_PC * sd = script_rid2sd(st);
  12390. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  12391. return 0;
  12392. }
  12393. BUILDIN_FUNC(changequest)
  12394. {
  12395. TBL_PC * sd = script_rid2sd(st);
  12396. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  12397. return 0;
  12398. }
  12399. BUILDIN_FUNC(checkquest)
  12400. {
  12401. TBL_PC * sd = script_rid2sd(st);
  12402. quest_check_type type = HAVEQUEST;
  12403. if( script_hasdata(st, 3) )
  12404. type = (quest_check_type)script_getnum(st, 3);
  12405. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  12406. return 0;
  12407. }
  12408. BUILDIN_FUNC(showevent)
  12409. {
  12410. TBL_PC *sd = script_rid2sd(st);
  12411. struct npc_data *nd = map_id2nd(st->oid);
  12412. int state, color;
  12413. if( sd == NULL || nd == NULL )
  12414. return 0;
  12415. state = script_getnum(st, 2);
  12416. color = script_getnum(st, 3);
  12417. if( color < 0 || color > 4 )
  12418. color = 0; // set default color
  12419. clif_quest_show_event(sd, &nd->bl, state, color);
  12420. return 0;
  12421. }
  12422. /*==========================================
  12423. * BattleGround System
  12424. *------------------------------------------*/
  12425. BUILDIN_FUNC(waitingroom2bg)
  12426. {
  12427. struct npc_data *nd;
  12428. struct chat_data *cd;
  12429. const char *map_name, *ev = "", *dev = "";
  12430. int x, y, i, mapindex = 0, bg_id, n;
  12431. struct map_session_data *sd;
  12432. if( script_hasdata(st,7) )
  12433. nd = npc_name2id(script_getstr(st,7));
  12434. else
  12435. nd = (struct npc_data *)map_id2bl(st->oid);
  12436. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  12437. {
  12438. script_pushint(st,0);
  12439. return 0;
  12440. }
  12441. map_name = script_getstr(st,2);
  12442. if( strcmp(map_name,"-") != 0 )
  12443. {
  12444. mapindex = mapindex_name2id(map_name);
  12445. if( mapindex == 0 )
  12446. { // Invalid Map
  12447. script_pushint(st,0);
  12448. return 0;
  12449. }
  12450. }
  12451. x = script_getnum(st,3);
  12452. y = script_getnum(st,4);
  12453. ev = script_getstr(st,5); // Logout Event
  12454. dev = script_getstr(st,6); // Die Event
  12455. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  12456. { // Creation failed
  12457. script_pushint(st,0);
  12458. return 0;
  12459. }
  12460. n = cd->users;
  12461. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  12462. {
  12463. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  12464. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  12465. else
  12466. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  12467. }
  12468. mapreg_setreg(add_str("$@arenamembersnum"), i);
  12469. script_pushint(st,bg_id);
  12470. return 0;
  12471. }
  12472. BUILDIN_FUNC(waitingroom2bg_single)
  12473. {
  12474. const char* map_name;
  12475. struct npc_data *nd;
  12476. struct chat_data *cd;
  12477. struct map_session_data *sd;
  12478. int x, y, mapindex, bg_id;
  12479. bg_id = script_getnum(st,2);
  12480. map_name = script_getstr(st,3);
  12481. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12482. return 0; // Invalid Map
  12483. x = script_getnum(st,4);
  12484. y = script_getnum(st,5);
  12485. nd = npc_name2id(script_getstr(st,6));
  12486. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  12487. return 0;
  12488. if( (sd = cd->usersd[0]) == NULL )
  12489. return 0;
  12490. if( bg_team_join(bg_id, sd) )
  12491. {
  12492. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  12493. script_pushint(st,1);
  12494. }
  12495. else
  12496. script_pushint(st,0);
  12497. return 0;
  12498. }
  12499. BUILDIN_FUNC(bg_team_setxy)
  12500. {
  12501. struct battleground_data *bg;
  12502. int bg_id;
  12503. bg_id = script_getnum(st,2);
  12504. if( (bg = bg_team_search(bg_id)) == NULL )
  12505. return 0;
  12506. bg->x = script_getnum(st,3);
  12507. bg->y = script_getnum(st,4);
  12508. return 0;
  12509. }
  12510. BUILDIN_FUNC(bg_warp)
  12511. {
  12512. int x, y, mapindex, bg_id;
  12513. const char* map_name;
  12514. bg_id = script_getnum(st,2);
  12515. map_name = script_getstr(st,3);
  12516. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  12517. return 0; // Invalid Map
  12518. x = script_getnum(st,4);
  12519. y = script_getnum(st,5);
  12520. bg_team_warp(bg_id, mapindex, x, y);
  12521. return 0;
  12522. }
  12523. BUILDIN_FUNC(bg_monster)
  12524. {
  12525. int class_ = 0, x = 0, y = 0, bg_id = 0;
  12526. const char *str,*map, *evt="";
  12527. bg_id = script_getnum(st,2);
  12528. map = script_getstr(st,3);
  12529. x = script_getnum(st,4);
  12530. y = script_getnum(st,5);
  12531. str = script_getstr(st,6);
  12532. class_ = script_getnum(st,7);
  12533. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  12534. check_event(st, evt);
  12535. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  12536. return 0;
  12537. }
  12538. BUILDIN_FUNC(bg_monster_set_team)
  12539. {
  12540. struct mob_data *md;
  12541. struct block_list *mbl;
  12542. int id = script_getnum(st,2),
  12543. bg_id = script_getnum(st,3);
  12544. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  12545. return 0;
  12546. md = (TBL_MOB *)mbl;
  12547. md->bg_id = bg_id;
  12548. mob_stop_attack(md);
  12549. mob_stop_walking(md, 0);
  12550. md->target_id = md->attacked_id = 0;
  12551. clif_charnameack(0, &md->bl);
  12552. return 0;
  12553. }
  12554. BUILDIN_FUNC(bg_leave)
  12555. {
  12556. struct map_session_data *sd = script_rid2sd(st);
  12557. if( sd == NULL || !sd->bg_id )
  12558. return 0;
  12559. bg_team_leave(sd,0);
  12560. return 0;
  12561. }
  12562. BUILDIN_FUNC(bg_destroy)
  12563. {
  12564. int bg_id = script_getnum(st,2);
  12565. bg_team_delete(bg_id);
  12566. return 0;
  12567. }
  12568. BUILDIN_FUNC(bg_getareausers)
  12569. {
  12570. const char *str;
  12571. int m, x0, y0, x1, y1, bg_id;
  12572. int i = 0, c = 0;
  12573. struct battleground_data *bg = NULL;
  12574. struct map_session_data *sd;
  12575. bg_id = script_getnum(st,2);
  12576. str = script_getstr(st,3);
  12577. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  12578. {
  12579. script_pushint(st,0);
  12580. return 0;
  12581. }
  12582. x0 = script_getnum(st,4);
  12583. y0 = script_getnum(st,5);
  12584. x1 = script_getnum(st,6);
  12585. y1 = script_getnum(st,7);
  12586. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  12587. {
  12588. if( (sd = bg->members[i].sd) == NULL )
  12589. continue;
  12590. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  12591. continue;
  12592. c++;
  12593. }
  12594. script_pushint(st,c);
  12595. return 0;
  12596. }
  12597. BUILDIN_FUNC(bg_updatescore)
  12598. {
  12599. const char *str;
  12600. int m;
  12601. str = script_getstr(st,2);
  12602. if( (m = map_mapname2mapid(str)) < 0 )
  12603. return 0;
  12604. map[m].bgscore_lion = script_getnum(st,3);
  12605. map[m].bgscore_eagle = script_getnum(st,4);
  12606. clif_bg_updatescore(m);
  12607. return 0;
  12608. }
  12609. BUILDIN_FUNC(bg_get_data)
  12610. {
  12611. struct battleground_data *bg;
  12612. int bg_id = script_getnum(st,2),
  12613. type = script_getnum(st,3);
  12614. if( (bg = bg_team_search(bg_id)) == NULL )
  12615. {
  12616. script_pushint(st,0);
  12617. return 0;
  12618. }
  12619. switch( type )
  12620. {
  12621. case 0: script_pushint(st, bg->count); break;
  12622. default:
  12623. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  12624. break;
  12625. }
  12626. return 0;
  12627. }
  12628. /*==========================================
  12629. * Instancing Script Commands
  12630. *------------------------------------------*/
  12631. BUILDIN_FUNC(instance_create)
  12632. {
  12633. const char *name;
  12634. int party_id, res;
  12635. name = script_getstr(st, 2);
  12636. party_id = script_getnum(st, 3);
  12637. res = instance_create(party_id, name);
  12638. if( res == -4 ) // Already exists
  12639. {
  12640. script_pushint(st, -1);
  12641. return 0;
  12642. }
  12643. else if( res < 0 )
  12644. {
  12645. const char *err;
  12646. switch(res)
  12647. {
  12648. case -3: err = "No free instances"; break;
  12649. case -2: err = "Invalid party ID"; break;
  12650. case -1: err = "Invalid type"; break;
  12651. default: err = "Unknown"; break;
  12652. }
  12653. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  12654. script_pushint(st, -2);
  12655. return 0;
  12656. }
  12657. script_pushint(st, res);
  12658. return 0;
  12659. }
  12660. BUILDIN_FUNC(instance_destroy)
  12661. {
  12662. int instance_id;
  12663. struct map_session_data *sd;
  12664. struct party_data *p;
  12665. if( script_hasdata(st, 2) )
  12666. instance_id = script_getnum(st, 2);
  12667. else if( st->instance_id )
  12668. instance_id = st->instance_id;
  12669. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12670. instance_id = p->instance_id;
  12671. else return 0;
  12672. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12673. {
  12674. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  12675. return 0;
  12676. }
  12677. instance_destroy(instance_id);
  12678. return 0;
  12679. }
  12680. BUILDIN_FUNC(instance_attachmap)
  12681. {
  12682. const char *name;
  12683. int m;
  12684. int instance_id;
  12685. bool usebasename = false;
  12686. name = script_getstr(st,2);
  12687. instance_id = script_getnum(st,3);
  12688. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  12689. usebasename = true;
  12690. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  12691. {
  12692. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  12693. script_pushconststr(st, "");
  12694. return 0;
  12695. }
  12696. script_pushconststr(st, map[m].name);
  12697. return 0;
  12698. }
  12699. BUILDIN_FUNC(instance_detachmap)
  12700. {
  12701. struct map_session_data *sd;
  12702. struct party_data *p;
  12703. const char *str;
  12704. int m, instance_id;
  12705. str = script_getstr(st, 2);
  12706. if( script_hasdata(st, 3) )
  12707. instance_id = script_getnum(st, 3);
  12708. else if( st->instance_id )
  12709. instance_id = st->instance_id;
  12710. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12711. instance_id = p->instance_id;
  12712. else return 0;
  12713. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  12714. {
  12715. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  12716. return 0;
  12717. }
  12718. instance_del_map(m);
  12719. return 0;
  12720. }
  12721. BUILDIN_FUNC(instance_attach)
  12722. {
  12723. int instance_id;
  12724. instance_id = script_getnum(st, 2);
  12725. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12726. return 0;
  12727. st->instance_id = instance_id;
  12728. return 0;
  12729. }
  12730. BUILDIN_FUNC(instance_id)
  12731. {
  12732. int type, instance_id;
  12733. struct map_session_data *sd;
  12734. struct party_data *p;
  12735. if( script_hasdata(st, 2) )
  12736. {
  12737. type = script_getnum(st, 2);
  12738. if( type == 0 )
  12739. instance_id = st->instance_id;
  12740. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  12741. instance_id = p->instance_id;
  12742. else
  12743. instance_id = 0;
  12744. }
  12745. else
  12746. instance_id = st->instance_id;
  12747. script_pushint(st, instance_id);
  12748. return 0;
  12749. }
  12750. BUILDIN_FUNC(instance_set_timeout)
  12751. {
  12752. int progress_timeout, idle_timeout;
  12753. int instance_id;
  12754. struct map_session_data *sd;
  12755. struct party_data *p;
  12756. progress_timeout = script_getnum(st, 2);
  12757. idle_timeout = script_getnum(st, 3);
  12758. if( script_hasdata(st, 4) )
  12759. instance_id = script_getnum(st, 4);
  12760. else if( st->instance_id )
  12761. instance_id = st->instance_id;
  12762. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12763. instance_id = p->instance_id;
  12764. else return 0;
  12765. if( instance_id > 0 )
  12766. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  12767. return 0;
  12768. }
  12769. BUILDIN_FUNC(instance_init)
  12770. {
  12771. int instance_id = script_getnum(st, 2);
  12772. if( instance[instance_id].state != INSTANCE_IDLE )
  12773. {
  12774. ShowError("instance_init: instance already initialized.\n");
  12775. return 0;
  12776. }
  12777. instance_init(instance_id);
  12778. return 0;
  12779. }
  12780. BUILDIN_FUNC(instance_announce)
  12781. {
  12782. int instance_id = script_getnum(st,2);
  12783. const char *mes = script_getstr(st,3);
  12784. int flag = script_getnum(st,4);
  12785. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  12786. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  12787. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  12788. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  12789. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  12790. int i;
  12791. struct map_session_data *sd;
  12792. struct party_data *p;
  12793. if( instance_id == 0 )
  12794. {
  12795. if( st->instance_id )
  12796. instance_id = st->instance_id;
  12797. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12798. instance_id = p->instance_id;
  12799. else return 0;
  12800. }
  12801. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12802. return 0;
  12803. for( i = 0; i < instance[instance_id].num_map; i++ )
  12804. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  12805. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  12806. return 0;
  12807. }
  12808. BUILDIN_FUNC(instance_npcname)
  12809. {
  12810. const char *str;
  12811. int instance_id = 0;
  12812. struct map_session_data *sd;
  12813. struct party_data *p;
  12814. struct npc_data *nd;
  12815. str = script_getstr(st, 2);
  12816. if( script_hasdata(st, 3) )
  12817. instance_id = script_getnum(st, 3);
  12818. else if( st->instance_id )
  12819. instance_id = st->instance_id;
  12820. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12821. instance_id = p->instance_id;
  12822. if( instance_id && (nd = npc_name2id(str)) != NULL )
  12823. {
  12824. static char npcname[NAME_LENGTH];
  12825. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  12826. script_pushconststr(st,npcname);
  12827. }
  12828. else
  12829. {
  12830. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  12831. st->state = END;
  12832. return 1;
  12833. }
  12834. return 0;
  12835. }
  12836. BUILDIN_FUNC(has_instance)
  12837. {
  12838. struct map_session_data *sd;
  12839. struct party_data *p;
  12840. const char *str;
  12841. int m, instance_id = 0;
  12842. str = script_getstr(st, 2);
  12843. if( script_hasdata(st, 3) )
  12844. instance_id = script_getnum(st, 3);
  12845. else if( st->instance_id )
  12846. instance_id = st->instance_id;
  12847. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12848. instance_id = p->instance_id;
  12849. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  12850. {
  12851. script_pushconststr(st, "");
  12852. return 0;
  12853. }
  12854. script_pushconststr(st, map[m].name);
  12855. return 0;
  12856. }
  12857. BUILDIN_FUNC(instance_warpall)
  12858. {
  12859. struct map_session_data *pl_sd;
  12860. int m, i, instance_id;
  12861. const char *mapn;
  12862. int x, y;
  12863. unsigned short mapindex;
  12864. struct party_data *p = NULL;
  12865. mapn = script_getstr(st,2);
  12866. x = script_getnum(st,3);
  12867. y = script_getnum(st,4);
  12868. if( script_hasdata(st,5) )
  12869. instance_id = script_getnum(st,5);
  12870. else if( st->instance_id )
  12871. instance_id = st->instance_id;
  12872. 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 )
  12873. instance_id = p->instance_id;
  12874. else return 0;
  12875. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  12876. return 0;
  12877. if( !(p = party_search(instance[instance_id].party_id)) )
  12878. return 0;
  12879. mapindex = map_id2index(m);
  12880. for( i = 0; i < MAX_PARTY; i++ )
  12881. 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);
  12882. return 0;
  12883. }
  12884. /*==========================================
  12885. * Custom Fonts
  12886. *------------------------------------------*/
  12887. BUILDIN_FUNC(setfont)
  12888. {
  12889. struct map_session_data *sd = script_rid2sd(st);
  12890. int font = script_getnum(st,2);
  12891. if( sd == NULL )
  12892. return 0;
  12893. if( sd->user_font != font )
  12894. sd->user_font = font;
  12895. else
  12896. sd->user_font = 0;
  12897. clif_font(sd);
  12898. return 0;
  12899. }
  12900. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  12901. {
  12902. TBL_MOB* md = (TBL_MOB*)bl;
  12903. struct block_list *tbl;
  12904. int mobid = va_arg(ap,int);
  12905. int skillid = va_arg(ap,int);
  12906. int skilllv = va_arg(ap,int);
  12907. int casttime = va_arg(ap,int);
  12908. int cancel = va_arg(ap,int);
  12909. int emotion = va_arg(ap,int);
  12910. int target = va_arg(ap,int);
  12911. if( md->class_ != mobid )
  12912. return 0;
  12913. // 0:self, 1:target, 2:master, default:random
  12914. switch( target )
  12915. {
  12916. case 0: tbl = map_id2bl(md->bl.id); break;
  12917. case 1: tbl = map_id2bl(md->target_id); break;
  12918. case 2: tbl = map_id2bl(md->master_id); break;
  12919. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  12920. }
  12921. if( !tbl )
  12922. return 0;
  12923. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  12924. unit_skillcastcancel(bl,0);
  12925. if( skill_get_casttype(skillid) == CAST_GROUND )
  12926. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  12927. else
  12928. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  12929. clif_emotion(&md->bl, emotion);
  12930. return 0;
  12931. }
  12932. /*==========================================
  12933. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  12934. *------------------------------------------*/
  12935. BUILDIN_FUNC(areamobuseskill)
  12936. {
  12937. struct block_list center;
  12938. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  12939. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  12940. {
  12941. ShowError("areamobuseskill: invalid map name.\n");
  12942. return 0;
  12943. }
  12944. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  12945. return 0;
  12946. center.m = m;
  12947. center.x = script_getnum(st,3);
  12948. center.y = script_getnum(st,4);
  12949. range = script_getnum(st,5);
  12950. mobid = script_getnum(st,6);
  12951. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  12952. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  12953. skilllv = battle_config.mob_max_skilllvl;
  12954. casttime = script_getnum(st,9);
  12955. cancel = script_getnum(st,10);
  12956. emotion = script_getnum(st,11);
  12957. target = script_getnum(st,12);
  12958. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  12959. return 0;
  12960. }
  12961. BUILDIN_FUNC(progressbar)
  12962. {
  12963. #if PACKETVER >= 20080318
  12964. struct map_session_data * sd = script_rid2sd(st);
  12965. const char * color;
  12966. unsigned int second;
  12967. if( !st || !sd )
  12968. return 0;
  12969. st->state = STOP;
  12970. color = script_getstr(st,2);
  12971. second = script_getnum(st,3);
  12972. sd->progressbar.npc_id = st->oid;
  12973. sd->progressbar.timeout = gettick() + second*1000;
  12974. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  12975. #endif
  12976. return 0;
  12977. }
  12978. BUILDIN_FUNC(pushpc)
  12979. {
  12980. int direction, cells, dx, dy;
  12981. struct map_session_data* sd;
  12982. if((sd = script_rid2sd(st))==NULL)
  12983. {
  12984. return 0;
  12985. }
  12986. direction = script_getnum(st,2);
  12987. cells = script_getnum(st,3);
  12988. if(direction<0 || direction>7)
  12989. {
  12990. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", direction);
  12991. script_reportsrc(st);
  12992. direction%= 8; // trim spin-over
  12993. }
  12994. if(!cells)
  12995. {// zero distance
  12996. return 0;
  12997. }
  12998. else if(cells<0)
  12999. {// pushing backwards
  13000. direction = (direction+4)%8; // turn around
  13001. cells = -cells;
  13002. }
  13003. dx = dirx[direction];
  13004. dy = diry[direction];
  13005. unit_blown(&sd->bl, dx, dy, cells, 0);
  13006. return 0;
  13007. }
  13008. /// Invokes buying store preparation window
  13009. /// buyingstore <slots>;
  13010. BUILDIN_FUNC(buyingstore)
  13011. {
  13012. struct map_session_data* sd;
  13013. if( ( sd = script_rid2sd(st) ) == NULL )
  13014. {
  13015. return 0;
  13016. }
  13017. buyingstore_setup(sd, script_getnum(st,2));
  13018. return 0;
  13019. }
  13020. /// Invokes search store info window
  13021. /// searchstores <uses>,<effect>;
  13022. BUILDIN_FUNC(searchstores)
  13023. {
  13024. unsigned short effect;
  13025. unsigned int uses;
  13026. struct map_session_data* sd;
  13027. if( ( sd = script_rid2sd(st) ) == NULL )
  13028. {
  13029. return 0;
  13030. }
  13031. uses = script_getnum(st,2);
  13032. effect = script_getnum(st,3);
  13033. if( !uses )
  13034. {
  13035. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  13036. return 1;
  13037. }
  13038. if( effect > 1 )
  13039. {
  13040. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  13041. return 1;
  13042. }
  13043. searchstore_open(sd, uses, effect);
  13044. return 0;
  13045. }
  13046. // declarations that were supposed to be exported from npc_chat.c
  13047. #ifdef PCRE_SUPPORT
  13048. BUILDIN_FUNC(defpattern);
  13049. BUILDIN_FUNC(activatepset);
  13050. BUILDIN_FUNC(deactivatepset);
  13051. BUILDIN_FUNC(deletepset);
  13052. #endif
  13053. /// script command definitions
  13054. /// for an explanation on args, see add_buildin_func
  13055. struct script_function buildin_func[] = {
  13056. // NPC interaction
  13057. BUILDIN_DEF(mes,"s"),
  13058. BUILDIN_DEF(next,""),
  13059. BUILDIN_DEF(close,""),
  13060. BUILDIN_DEF(close2,""),
  13061. BUILDIN_DEF(menu,"sl*"),
  13062. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  13063. BUILDIN_DEF(prompt,"s*"),
  13064. //
  13065. BUILDIN_DEF(goto,"l"),
  13066. BUILDIN_DEF(callsub,"l*"),
  13067. BUILDIN_DEF(callfunc,"s*"),
  13068. BUILDIN_DEF(return,"?"),
  13069. BUILDIN_DEF(getarg,"i?"),
  13070. BUILDIN_DEF(jobchange,"i?"),
  13071. BUILDIN_DEF(jobname,"i"),
  13072. BUILDIN_DEF(input,"r??"),
  13073. BUILDIN_DEF(warp,"sii"),
  13074. BUILDIN_DEF(areawarp,"siiiisii"),
  13075. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  13076. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  13077. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  13078. BUILDIN_DEF(setlook,"ii"),
  13079. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  13080. BUILDIN_DEF(set,"rv"),
  13081. BUILDIN_DEF(setarray,"rv*"),
  13082. BUILDIN_DEF(cleararray,"rvi"),
  13083. BUILDIN_DEF(copyarray,"rri"),
  13084. BUILDIN_DEF(getarraysize,"r"),
  13085. BUILDIN_DEF(deletearray,"r?"),
  13086. BUILDIN_DEF(getelementofarray,"ri"),
  13087. BUILDIN_DEF(getitem,"vi?"),
  13088. BUILDIN_DEF(rentitem,"vi"),
  13089. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  13090. BUILDIN_DEF(getnameditem,"vv"),
  13091. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  13092. BUILDIN_DEF(makeitem,"visii"),
  13093. BUILDIN_DEF(delitem,"vi?"),
  13094. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  13095. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  13096. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  13097. BUILDIN_DEF(cutin,"si"),
  13098. BUILDIN_DEF(viewpoint,"iiiii"),
  13099. BUILDIN_DEF(heal,"ii"),
  13100. BUILDIN_DEF(itemheal,"ii"),
  13101. BUILDIN_DEF(percentheal,"ii"),
  13102. BUILDIN_DEF(rand,"i?"),
  13103. BUILDIN_DEF(countitem,"v"),
  13104. BUILDIN_DEF(countitem2,"viiiiiii"),
  13105. BUILDIN_DEF(checkweight,"vi"),
  13106. BUILDIN_DEF(readparam,"i?"),
  13107. BUILDIN_DEF(getcharid,"i?"),
  13108. BUILDIN_DEF(getpartyname,"i"),
  13109. BUILDIN_DEF(getpartymember,"i?"),
  13110. BUILDIN_DEF(getpartyleader,"i?"),
  13111. BUILDIN_DEF(getguildname,"i"),
  13112. BUILDIN_DEF(getguildmaster,"i"),
  13113. BUILDIN_DEF(getguildmasterid,"i"),
  13114. BUILDIN_DEF(strcharinfo,"i"),
  13115. BUILDIN_DEF(strnpcinfo,"i"),
  13116. BUILDIN_DEF(getequipid,"i"),
  13117. BUILDIN_DEF(getequipname,"i"),
  13118. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  13119. BUILDIN_DEF(repair,"i"), // [Valaris]
  13120. BUILDIN_DEF(getequipisequiped,"i"),
  13121. BUILDIN_DEF(getequipisenableref,"i"),
  13122. BUILDIN_DEF(getequipisidentify,"i"),
  13123. BUILDIN_DEF(getequiprefinerycnt,"i"),
  13124. BUILDIN_DEF(getequipweaponlv,"i"),
  13125. BUILDIN_DEF(getequippercentrefinery,"i"),
  13126. BUILDIN_DEF(successrefitem,"i"),
  13127. BUILDIN_DEF(failedrefitem,"i"),
  13128. BUILDIN_DEF(statusup,"i"),
  13129. BUILDIN_DEF(statusup2,"ii"),
  13130. BUILDIN_DEF(bonus,"iv"),
  13131. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  13132. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  13133. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  13134. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  13135. BUILDIN_DEF(autobonus,"sii??"),
  13136. BUILDIN_DEF(autobonus2,"sii??"),
  13137. BUILDIN_DEF(autobonus3,"siiv?"),
  13138. BUILDIN_DEF(skill,"vi?"),
  13139. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  13140. BUILDIN_DEF(guildskill,"vi"),
  13141. BUILDIN_DEF(getskilllv,"v"),
  13142. BUILDIN_DEF(getgdskilllv,"iv"),
  13143. BUILDIN_DEF(basicskillcheck,""),
  13144. BUILDIN_DEF(getgmlevel,""),
  13145. BUILDIN_DEF(end,""),
  13146. BUILDIN_DEF(checkoption,"i"),
  13147. BUILDIN_DEF(setoption,"i?"),
  13148. BUILDIN_DEF(setcart,"?"),
  13149. BUILDIN_DEF(checkcart,""),
  13150. BUILDIN_DEF(setfalcon,"?"),
  13151. BUILDIN_DEF(checkfalcon,""),
  13152. BUILDIN_DEF(setriding,"?"),
  13153. BUILDIN_DEF(checkriding,""),
  13154. BUILDIN_DEF2(savepoint,"save","sii"),
  13155. BUILDIN_DEF(savepoint,"sii"),
  13156. BUILDIN_DEF(gettimetick,"i"),
  13157. BUILDIN_DEF(gettime,"i"),
  13158. BUILDIN_DEF(gettimestr,"si"),
  13159. BUILDIN_DEF(openstorage,""),
  13160. BUILDIN_DEF(guildopenstorage,""),
  13161. BUILDIN_DEF(itemskill,"vi"),
  13162. BUILDIN_DEF(produce,"i"),
  13163. BUILDIN_DEF(cooking,"i"),
  13164. BUILDIN_DEF(monster,"siisii?"),
  13165. BUILDIN_DEF(getmobdrops,"i"),
  13166. BUILDIN_DEF(areamonster,"siiiisii?"),
  13167. BUILDIN_DEF(killmonster,"ss?"),
  13168. BUILDIN_DEF(killmonsterall,"s?"),
  13169. BUILDIN_DEF(clone,"siisi????"),
  13170. BUILDIN_DEF(doevent,"s"),
  13171. BUILDIN_DEF(donpcevent,"s"),
  13172. BUILDIN_DEF(cmdothernpc,"ss"),
  13173. BUILDIN_DEF(addtimer,"is"),
  13174. BUILDIN_DEF(deltimer,"s"),
  13175. BUILDIN_DEF(addtimercount,"si"),
  13176. BUILDIN_DEF(initnpctimer,"??"),
  13177. BUILDIN_DEF(stopnpctimer,"??"),
  13178. BUILDIN_DEF(startnpctimer,"??"),
  13179. BUILDIN_DEF(setnpctimer,"i?"),
  13180. BUILDIN_DEF(getnpctimer,"i?"),
  13181. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  13182. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  13183. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  13184. BUILDIN_DEF(announce,"si?????"),
  13185. BUILDIN_DEF(mapannounce,"ssi?????"),
  13186. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  13187. BUILDIN_DEF(getusers,"i"),
  13188. BUILDIN_DEF(getmapguildusers,"si"),
  13189. BUILDIN_DEF(getmapusers,"s"),
  13190. BUILDIN_DEF(getareausers,"siiii"),
  13191. BUILDIN_DEF(getareadropitem,"siiiiv"),
  13192. BUILDIN_DEF(enablenpc,"s"),
  13193. BUILDIN_DEF(disablenpc,"s"),
  13194. BUILDIN_DEF(hideoffnpc,"s"),
  13195. BUILDIN_DEF(hideonnpc,"s"),
  13196. BUILDIN_DEF(sc_start,"iii?"),
  13197. BUILDIN_DEF(sc_start2,"iiii?"),
  13198. BUILDIN_DEF(sc_start4,"iiiiii?"),
  13199. BUILDIN_DEF(sc_end,"i?"),
  13200. BUILDIN_DEF(getscrate,"ii?"),
  13201. BUILDIN_DEF(debugmes,"s"),
  13202. BUILDIN_DEF2(catchpet,"pet","i"),
  13203. BUILDIN_DEF2(birthpet,"bpet",""),
  13204. BUILDIN_DEF(resetlvl,"i"),
  13205. BUILDIN_DEF(resetstatus,""),
  13206. BUILDIN_DEF(resetskill,""),
  13207. BUILDIN_DEF(skillpointcount,""),
  13208. BUILDIN_DEF(changebase,"i?"),
  13209. BUILDIN_DEF(changesex,""),
  13210. BUILDIN_DEF(waitingroom,"si?????"),
  13211. BUILDIN_DEF(delwaitingroom,"?"),
  13212. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  13213. BUILDIN_DEF(enablewaitingroomevent,"?"),
  13214. BUILDIN_DEF(disablewaitingroomevent,"?"),
  13215. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  13216. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  13217. BUILDIN_DEF(getwaitingroomstate,"i?"),
  13218. BUILDIN_DEF(warpwaitingpc,"sii?"),
  13219. BUILDIN_DEF(attachrid,"i"),
  13220. BUILDIN_DEF(detachrid,""),
  13221. BUILDIN_DEF(isloggedin,"i?"),
  13222. BUILDIN_DEF(setmapflagnosave,"ssii"),
  13223. BUILDIN_DEF(getmapflag,"si"),
  13224. BUILDIN_DEF(setmapflag,"si?"),
  13225. BUILDIN_DEF(removemapflag,"si"),
  13226. BUILDIN_DEF(pvpon,"s"),
  13227. BUILDIN_DEF(pvpoff,"s"),
  13228. BUILDIN_DEF(gvgon,"s"),
  13229. BUILDIN_DEF(gvgoff,"s"),
  13230. BUILDIN_DEF(emotion,"i??"),
  13231. BUILDIN_DEF(maprespawnguildid,"sii"),
  13232. BUILDIN_DEF(agitstart,""), // <Agit>
  13233. BUILDIN_DEF(agitend,""),
  13234. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  13235. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  13236. BUILDIN_DEF(getcastlename,"s"),
  13237. BUILDIN_DEF(getcastledata,"si?"),
  13238. BUILDIN_DEF(setcastledata,"sii"),
  13239. BUILDIN_DEF(requestguildinfo,"i?"),
  13240. BUILDIN_DEF(getequipcardcnt,"i"),
  13241. BUILDIN_DEF(successremovecards,"i"),
  13242. BUILDIN_DEF(failedremovecards,"ii"),
  13243. BUILDIN_DEF(marriage,"s"),
  13244. BUILDIN_DEF2(wedding_effect,"wedding",""),
  13245. BUILDIN_DEF(divorce,""),
  13246. BUILDIN_DEF(ispartneron,""),
  13247. BUILDIN_DEF(getpartnerid,""),
  13248. BUILDIN_DEF(getchildid,""),
  13249. BUILDIN_DEF(getmotherid,""),
  13250. BUILDIN_DEF(getfatherid,""),
  13251. BUILDIN_DEF(warppartner,"sii"),
  13252. BUILDIN_DEF(getitemname,"v"),
  13253. BUILDIN_DEF(getitemslots,"i"),
  13254. BUILDIN_DEF(makepet,"i"),
  13255. BUILDIN_DEF(getexp,"ii"),
  13256. BUILDIN_DEF(getinventorylist,""),
  13257. BUILDIN_DEF(getskilllist,""),
  13258. BUILDIN_DEF(clearitem,""),
  13259. BUILDIN_DEF(classchange,"ii"),
  13260. BUILDIN_DEF(misceffect,"i"),
  13261. BUILDIN_DEF(playBGM,"s"),
  13262. BUILDIN_DEF(playBGMall,"s?????"),
  13263. BUILDIN_DEF(soundeffect,"si"),
  13264. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  13265. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  13266. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  13267. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  13268. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  13269. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  13270. BUILDIN_DEF(petloot,"i"), // [Valaris]
  13271. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  13272. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  13273. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  13274. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  13275. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  13276. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  13277. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  13278. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  13279. BUILDIN_DEF(nude,""), // nude command [Valaris]
  13280. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  13281. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  13282. BUILDIN_DEF(charcommand,"s"), // [MouseJstr]
  13283. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  13284. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  13285. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  13286. BUILDIN_DEF(mobcount,"ss"),
  13287. BUILDIN_DEF(getlook,"i"),
  13288. BUILDIN_DEF(getsavepoint,"i"),
  13289. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  13290. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  13291. BUILDIN_DEF(npcstop,""), // [Valaris]
  13292. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  13293. BUILDIN_DEF(checkoption1,"i"),
  13294. BUILDIN_DEF(checkoption2,"i"),
  13295. BUILDIN_DEF(guildgetexp,"i"),
  13296. BUILDIN_DEF(guildchangegm,"is"),
  13297. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  13298. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  13299. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  13300. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  13301. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  13302. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  13303. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  13304. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  13305. BUILDIN_DEF(night,""), // sets the server to night time
  13306. BUILDIN_DEF(day,""), // sets the server to day time
  13307. #ifdef PCRE_SUPPORT
  13308. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  13309. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  13310. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  13311. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  13312. #endif
  13313. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  13314. BUILDIN_DEF(getusersname,""),
  13315. BUILDIN_DEF(recovery,""),
  13316. BUILDIN_DEF(getpetinfo,"i"),
  13317. BUILDIN_DEF(gethominfo,"i"),
  13318. BUILDIN_DEF(getmercinfo,"i?"),
  13319. BUILDIN_DEF(checkequipedcard,"i"),
  13320. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  13321. BUILDIN_DEF(globalmes,"s?"),
  13322. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  13323. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  13324. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  13325. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  13326. BUILDIN_DEF(setnpcdisplay,"sv??"),
  13327. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  13328. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  13329. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  13330. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  13331. // [zBuffer] List of mathematics commands --->
  13332. BUILDIN_DEF(sqrt,"i"),
  13333. BUILDIN_DEF(pow,"ii"),
  13334. BUILDIN_DEF(distance,"iiii"),
  13335. // <--- [zBuffer] List of mathematics commands
  13336. BUILDIN_DEF(md5,"s"),
  13337. // [zBuffer] List of dynamic var commands --->
  13338. BUILDIN_DEF(getd,"s"),
  13339. BUILDIN_DEF(setd,"sv"),
  13340. // <--- [zBuffer] List of dynamic var commands
  13341. BUILDIN_DEF(petstat,"i"),
  13342. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  13343. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  13344. BUILDIN_DEF(npcshopadditem,"sii*"),
  13345. BUILDIN_DEF(npcshopdelitem,"si*"),
  13346. BUILDIN_DEF(npcshopattach,"s?"),
  13347. BUILDIN_DEF(equip,"i"),
  13348. BUILDIN_DEF(autoequip,"ii"),
  13349. BUILDIN_DEF(setbattleflag,"si"),
  13350. BUILDIN_DEF(getbattleflag,"s"),
  13351. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  13352. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  13353. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  13354. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  13355. BUILDIN_DEF(axtoi,"s"),
  13356. BUILDIN_DEF(query_sql,"s*"),
  13357. BUILDIN_DEF(query_logsql,"s*"),
  13358. BUILDIN_DEF(escape_sql,"s"),
  13359. BUILDIN_DEF(atoi,"s"),
  13360. // [zBuffer] List of player cont commands --->
  13361. BUILDIN_DEF(rid2name,"i"),
  13362. BUILDIN_DEF(pcfollow,"ii"),
  13363. BUILDIN_DEF(pcstopfollow,"i"),
  13364. BUILDIN_DEF(pcblockmove,"ii"),
  13365. // <--- [zBuffer] List of player cont commands
  13366. // [zBuffer] List of mob control commands --->
  13367. BUILDIN_DEF(unitwalk,"ii?"),
  13368. BUILDIN_DEF(unitkill,"i"),
  13369. BUILDIN_DEF(unitwarp,"isii"),
  13370. BUILDIN_DEF(unitattack,"iv?"),
  13371. BUILDIN_DEF(unitstop,"i"),
  13372. BUILDIN_DEF(unittalk,"is"),
  13373. BUILDIN_DEF(unitemote,"ii"),
  13374. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  13375. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  13376. // <--- [zBuffer] List of mob control commands
  13377. BUILDIN_DEF(sleep,"i"),
  13378. BUILDIN_DEF(sleep2,"i"),
  13379. BUILDIN_DEF(awake,"s"),
  13380. BUILDIN_DEF(getvariableofnpc,"rs"),
  13381. BUILDIN_DEF(warpportal,"iisii"),
  13382. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  13383. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  13384. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  13385. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  13386. BUILDIN_DEF(checkvending,"?"),
  13387. BUILDIN_DEF(checkchatting,"?"),
  13388. BUILDIN_DEF(openmail,""),
  13389. BUILDIN_DEF(openauction,""),
  13390. BUILDIN_DEF(checkcell,"siii"),
  13391. BUILDIN_DEF(setcell,"siiiiii"),
  13392. BUILDIN_DEF(setwall,"siiiiis"),
  13393. BUILDIN_DEF(delwall,"s"),
  13394. BUILDIN_DEF(searchitem,"rs"),
  13395. BUILDIN_DEF(mercenary_create,"ii"),
  13396. BUILDIN_DEF(mercenary_heal,"ii"),
  13397. BUILDIN_DEF(mercenary_sc_start,"iii"),
  13398. BUILDIN_DEF(mercenary_get_calls,"i"),
  13399. BUILDIN_DEF(mercenary_get_faith,"i"),
  13400. BUILDIN_DEF(mercenary_set_calls,"ii"),
  13401. BUILDIN_DEF(mercenary_set_faith,"ii"),
  13402. BUILDIN_DEF(readbook,"ii"),
  13403. BUILDIN_DEF(setfont,"i"),
  13404. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  13405. BUILDIN_DEF(progressbar,"si"),
  13406. BUILDIN_DEF(pushpc,"ii"),
  13407. BUILDIN_DEF(buyingstore,"i"),
  13408. BUILDIN_DEF(searchstores,"ii"),
  13409. // WoE SE
  13410. BUILDIN_DEF(agitstart2,""),
  13411. BUILDIN_DEF(agitend2,""),
  13412. BUILDIN_DEF(agitcheck2,""),
  13413. // BattleGround
  13414. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  13415. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  13416. BUILDIN_DEF(bg_team_setxy,"iii"),
  13417. BUILDIN_DEF(bg_warp,"isii"),
  13418. BUILDIN_DEF(bg_monster,"isiisi?"),
  13419. BUILDIN_DEF(bg_monster_set_team,"ii"),
  13420. BUILDIN_DEF(bg_leave,""),
  13421. BUILDIN_DEF(bg_destroy,"i"),
  13422. BUILDIN_DEF(areapercentheal,"siiiiii"),
  13423. BUILDIN_DEF(bg_get_data,"ii"),
  13424. BUILDIN_DEF(bg_getareausers,"isiiii"),
  13425. BUILDIN_DEF(bg_updatescore,"sii"),
  13426. // Instancing
  13427. BUILDIN_DEF(instance_create,"si"),
  13428. BUILDIN_DEF(instance_destroy,"?"),
  13429. BUILDIN_DEF(instance_attachmap,"si?"),
  13430. BUILDIN_DEF(instance_detachmap,"s?"),
  13431. BUILDIN_DEF(instance_attach,"i"),
  13432. BUILDIN_DEF(instance_id,"?"),
  13433. BUILDIN_DEF(instance_set_timeout,"ii?"),
  13434. BUILDIN_DEF(instance_init,"i"),
  13435. BUILDIN_DEF(instance_announce,"isi?????"),
  13436. BUILDIN_DEF(instance_npcname,"s?"),
  13437. BUILDIN_DEF(has_instance,"s?"),
  13438. BUILDIN_DEF(instance_warpall,"sii?"),
  13439. //Quest Log System [Inkfish]
  13440. BUILDIN_DEF(setquest, "i"),
  13441. BUILDIN_DEF(erasequest, "i"),
  13442. BUILDIN_DEF(completequest, "i"),
  13443. BUILDIN_DEF(checkquest, "i?"),
  13444. BUILDIN_DEF(changequest, "ii"),
  13445. BUILDIN_DEF(showevent, "ii"),
  13446. {NULL,NULL,NULL},
  13447. };