script.c 453 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371
  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/nullpo.h"
  12. #include "../common/random.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/socket.h"
  15. #include "../common/strlib.h"
  16. #include "../common/timer.h"
  17. #include "../common/utils.h"
  18. #include "map.h"
  19. #include "path.h"
  20. #include "clif.h"
  21. #include "chrif.h"
  22. #include "itemdb.h"
  23. #include "pc.h"
  24. #include "status.h"
  25. #include "storage.h"
  26. #include "mob.h"
  27. #include "npc.h"
  28. #include "pet.h"
  29. #include "mapreg.h"
  30. #include "homunculus.h"
  31. #include "instance.h"
  32. #include "mercenary.h"
  33. #include "intif.h"
  34. #include "skill.h"
  35. #include "status.h"
  36. #include "chat.h"
  37. #include "battle.h"
  38. #include "battleground.h"
  39. #include "party.h"
  40. #include "guild.h"
  41. #include "atcommand.h"
  42. #include "log.h"
  43. #include "unit.h"
  44. #include "pet.h"
  45. #include "mail.h"
  46. #include "script.h"
  47. #include "quest.h"
  48. #include "elemental.h"
  49. #include "../config/core.h"
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include <string.h>
  53. #include <math.h>
  54. #ifndef WIN32
  55. #include <sys/time.h>
  56. #endif
  57. #include <time.h>
  58. #include <setjmp.h>
  59. #include <errno.h>
  60. #ifdef BETA_THREAD_TEST
  61. #include "../common/atomic.h"
  62. #include "../common/spinlock.h"
  63. #include "../common/thread.h"
  64. #include "../common/mutex.h"
  65. #endif
  66. ///////////////////////////////////////////////////////////////////////////////
  67. //## TODO possible enhancements: [FlavioJS]
  68. // - 'callfunc' supporting labels in the current npc "::LabelName"
  69. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  70. // - 'function FuncName;' function declarations reverting to global functions
  71. // if local label isn't found
  72. // - join callfunc and callsub's functionality
  73. // - remove dynamic allocation in add_word()
  74. // - remove GETVALUE / SETVALUE
  75. // - clean up the set_reg / set_val / setd_sub mess
  76. // - detect invalid label references at parse-time
  77. //
  78. // struct script_state* st;
  79. //
  80. /// Returns the script_data at the target index
  81. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  82. /// Returns if the stack contains data at the target index
  83. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  84. /// Returns the index of the last data in the stack
  85. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  86. /// Pushes an int into the stack
  87. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  88. /// Pushes a string into the stack (script engine frees it automatically)
  89. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  90. /// Pushes a copy of a string into the stack
  91. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  92. /// Pushes a constant string into the stack (must never change or be freed)
  93. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  94. /// Pushes a nil into the stack
  95. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  96. /// Pushes a copy of the data in the target index
  97. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  98. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  99. #define script_isint(st,i) data_isint(script_getdata(st,i))
  100. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  101. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  102. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  103. // Returns name of currently running function
  104. #define script_getfuncname(st) ( st->funcname )
  105. // Note: "top" functions/defines use indexes relative to the top of the stack
  106. // -1 is the index of the data at the top
  107. /// Returns the script_data at the target index relative to the top of the stack
  108. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  109. /// Pushes a copy of the data in the target index relative to the top of the stack
  110. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  111. /// Removes the range of values [start,end[ relative to the top of the stack
  112. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  113. //
  114. // struct script_data* data;
  115. //
  116. /// Returns if the script data is a string
  117. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  118. /// Returns if the script data is an int
  119. #define data_isint(data) ( (data)->type == C_INT )
  120. /// Returns if the script data is a reference
  121. #define data_isreference(data) ( (data)->type == C_NAME )
  122. /// Returns if the script data is a label
  123. #define data_islabel(data) ( (data)->type == C_POS )
  124. /// Returns if the script data is an internal script function label
  125. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  126. /// Returns if this is a reference to a constant
  127. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  128. /// Returns if this a reference to a param
  129. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  130. /// Returns if this a reference to a variable
  131. //##TODO confirm it's C_NAME [FlavioJS]
  132. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  133. /// Returns the unique id of the reference (id and index)
  134. #define reference_getuid(data) ( (data)->u.num )
  135. /// Returns the id of the reference
  136. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  137. /// Returns the array index of the reference
  138. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  139. /// Returns the name of the reference
  140. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  141. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  142. #define reference_getref(data) ( (data)->ref )
  143. /// Returns the value of the constant
  144. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  145. /// Returns the type of param
  146. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  147. /// Composes the uid of a reference from the id and the index
  148. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  149. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  150. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  151. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  152. #define FETCH(n, t) \
  153. if( script_hasdata(st,n) ) \
  154. (t)=script_getnum(st,n);
  155. /// Maximum amount of elements in script arrays
  156. #define SCRIPT_MAX_ARRAYSIZE 128
  157. #define SCRIPT_BLOCK_SIZE 512
  158. enum { LABEL_NEXTLINE=1,LABEL_START };
  159. /// temporary buffer for passing around compiled bytecode
  160. /// @see add_scriptb, set_label, parse_script
  161. static unsigned char* script_buf = NULL;
  162. static int script_pos = 0, script_size = 0;
  163. static inline int GETVALUE(const unsigned char* buf, int i)
  164. {
  165. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  166. }
  167. static inline void SETVALUE(unsigned char* buf, int i, int n)
  168. {
  169. buf[i] = GetByte(n, 0);
  170. buf[i+1] = GetByte(n, 1);
  171. buf[i+2] = GetByte(n, 2);
  172. }
  173. // String buffer structures.
  174. // str_data stores string information
  175. static struct str_data_struct {
  176. enum c_op type;
  177. int str;
  178. int backpatch;
  179. int label;
  180. int (*func)(struct script_state *st);
  181. int val;
  182. int next;
  183. } *str_data = NULL;
  184. static int str_data_size = 0; // size of the data
  185. static int str_num = LABEL_START; // next id to be assigned
  186. // str_buf holds the strings themselves
  187. static char *str_buf;
  188. static int str_size = 0; // size of the buffer
  189. static int str_pos = 0; // next position to be assigned
  190. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  191. #define SCRIPT_HASH_SIZE 1021
  192. int str_hash[SCRIPT_HASH_SIZE];
  193. // Specifies which string hashing method to use
  194. //#define SCRIPT_HASH_DJB2
  195. //#define SCRIPT_HASH_SDBM
  196. #define SCRIPT_HASH_ELF
  197. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  198. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  199. static int parse_options=0;
  200. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  201. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  202. // important buildin function references for usage in scripts
  203. static int buildin_set_ref = 0;
  204. static int buildin_callsub_ref = 0;
  205. static int buildin_callfunc_ref = 0;
  206. static int buildin_getelementofarray_ref = 0;
  207. // Caches compiled autoscript item code.
  208. // Note: This is not cleared when reloading itemdb.
  209. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  210. struct Script_Config script_config = {
  211. 1, // warn_func_mismatch_argtypes
  212. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  213. 0, INT_MAX, // input_min_value/input_max_value
  214. "OnPCDieEvent", //die_event_name
  215. "OnPCKillEvent", //kill_pc_event_name
  216. "OnNPCKillEvent", //kill_mob_event_name
  217. "OnPCLoginEvent", //login_event_name
  218. "OnPCLogoutEvent", //logout_event_name
  219. "OnPCLoadMapEvent", //loadmap_event_name
  220. "OnPCBaseLvUpEvent", //baselvup_event_name
  221. "OnPCJobLvUpEvent", //joblvup_event_name
  222. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  223. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  224. };
  225. static jmp_buf error_jump;
  226. static char* error_msg;
  227. static const char* error_pos;
  228. static int error_report; // if the error should produce output
  229. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  230. // [Eoe / jA 1080, 1081, 1094, 1164]
  231. enum curly_type {
  232. TYPE_NULL = 0,
  233. TYPE_IF,
  234. TYPE_SWITCH,
  235. TYPE_WHILE,
  236. TYPE_FOR,
  237. TYPE_DO,
  238. TYPE_USERFUNC,
  239. TYPE_ARGLIST // function argument list
  240. };
  241. enum e_arglist
  242. {
  243. ARGLIST_UNDEFINED = 0,
  244. ARGLIST_NO_PAREN = 1,
  245. ARGLIST_PAREN = 2,
  246. };
  247. static struct {
  248. struct {
  249. enum curly_type type;
  250. int index;
  251. int count;
  252. int flag;
  253. struct linkdb_node *case_label;
  254. } curly[256]; // Information right parenthesis
  255. int curly_count; // The number of right brackets
  256. int index; // Number of the syntax used in the script
  257. } syntax;
  258. const char* parse_curly_close(const char* p);
  259. const char* parse_syntax_close(const char* p);
  260. const char* parse_syntax_close_sub(const char* p,int* flag);
  261. const char* parse_syntax(const char* p);
  262. static int parse_syntax_for_flag = 0;
  263. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  264. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  265. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  266. int potion_target=0;
  267. c_op get_com(unsigned char *script,int *pos);
  268. int get_num(unsigned char *script,int *pos);
  269. typedef struct script_function {
  270. int (*func)(struct script_state *st);
  271. const char *name;
  272. const char *arg;
  273. } script_function;
  274. extern script_function buildin_func[];
  275. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  276. #ifdef BETA_THREAD_TEST
  277. /**
  278. * MySQL Query Slave
  279. **/
  280. static SPIN_LOCK queryThreadLock;
  281. static rAthread queryThread = NULL;
  282. static ramutex queryThreadMutex = NULL;
  283. static racond queryThreadCond = NULL;
  284. static volatile int32 queryThreadTerminate = 0;
  285. struct queryThreadEntry {
  286. bool ok;
  287. bool type; /* main db or log db? */
  288. struct script_state *st;
  289. };
  290. /* Ladies and Gentleman the Manager! */
  291. struct {
  292. struct queryThreadEntry **entry;/* array of structs */
  293. int count;
  294. int timer;/* used to receive processed entries */
  295. } queryThreadData;
  296. #endif
  297. /*==========================================
  298. * (Only those needed) local declaration prototype
  299. *------------------------------------------*/
  300. const char* parse_subexpr(const char* p,int limit);
  301. int run_func(struct script_state *st);
  302. int script_instancegetid(struct script_state *st);
  303. enum {
  304. MF_NOMEMO, //0
  305. MF_NOTELEPORT,
  306. MF_NOSAVE,
  307. MF_NOBRANCH,
  308. MF_NOPENALTY,
  309. MF_NOZENYPENALTY,
  310. MF_PVP,
  311. MF_PVP_NOPARTY,
  312. MF_PVP_NOGUILD,
  313. MF_GVG,
  314. MF_GVG_NOPARTY, //10
  315. MF_NOTRADE,
  316. MF_NOSKILL,
  317. MF_NOWARP,
  318. MF_PARTYLOCK,
  319. MF_NOICEWALL,
  320. MF_SNOW,
  321. MF_FOG,
  322. MF_SAKURA,
  323. MF_LEAVES,
  324. /**
  325. * No longer available, keeping here just in case it's back someday. [Ind]
  326. **/
  327. //MF_RAIN, //20
  328. // 21 free
  329. MF_NOGO = 22,
  330. MF_CLOUDS,
  331. MF_CLOUDS2,
  332. MF_FIREWORKS,
  333. MF_GVG_CASTLE,
  334. MF_GVG_DUNGEON,
  335. MF_NIGHTENABLED,
  336. MF_NOBASEEXP,
  337. MF_NOJOBEXP, //30
  338. MF_NOMOBLOOT,
  339. MF_NOMVPLOOT,
  340. MF_NORETURN,
  341. MF_NOWARPTO,
  342. MF_NIGHTMAREDROP,
  343. MF_RESTRICTED,
  344. MF_NOCOMMAND,
  345. MF_NODROP,
  346. MF_JEXP,
  347. MF_BEXP, //40
  348. MF_NOVENDING,
  349. MF_LOADEVENT,
  350. MF_NOCHAT,
  351. MF_NOEXPPENALTY,
  352. MF_GUILDLOCK,
  353. MF_TOWN,
  354. MF_AUTOTRADE,
  355. MF_ALLOWKS,
  356. MF_MONSTER_NOTELEPORT,
  357. MF_PVP_NOCALCRANK, //50
  358. MF_BATTLEGROUND,
  359. MF_RESET,
  360. MF_CHANNELAUTOJOIN,
  361. MF_NOUSECART,
  362. MF_NOITEMCONSUMPTION,
  363. MF_SUMSTARTMIRACLE,
  364. MF_NOMINEEFFECT,
  365. MF_NOLOCKON,
  366. MF_NOTOMB
  367. };
  368. const char* script_op2name(int op)
  369. {
  370. #define RETURN_OP_NAME(type) case type: return #type
  371. switch( op )
  372. {
  373. RETURN_OP_NAME(C_NOP);
  374. RETURN_OP_NAME(C_POS);
  375. RETURN_OP_NAME(C_INT);
  376. RETURN_OP_NAME(C_PARAM);
  377. RETURN_OP_NAME(C_FUNC);
  378. RETURN_OP_NAME(C_STR);
  379. RETURN_OP_NAME(C_CONSTSTR);
  380. RETURN_OP_NAME(C_ARG);
  381. RETURN_OP_NAME(C_NAME);
  382. RETURN_OP_NAME(C_EOL);
  383. RETURN_OP_NAME(C_RETINFO);
  384. RETURN_OP_NAME(C_USERFUNC);
  385. RETURN_OP_NAME(C_USERFUNC_POS);
  386. // operators
  387. RETURN_OP_NAME(C_OP3);
  388. RETURN_OP_NAME(C_LOR);
  389. RETURN_OP_NAME(C_LAND);
  390. RETURN_OP_NAME(C_LE);
  391. RETURN_OP_NAME(C_LT);
  392. RETURN_OP_NAME(C_GE);
  393. RETURN_OP_NAME(C_GT);
  394. RETURN_OP_NAME(C_EQ);
  395. RETURN_OP_NAME(C_NE);
  396. RETURN_OP_NAME(C_XOR);
  397. RETURN_OP_NAME(C_OR);
  398. RETURN_OP_NAME(C_AND);
  399. RETURN_OP_NAME(C_ADD);
  400. RETURN_OP_NAME(C_SUB);
  401. RETURN_OP_NAME(C_MUL);
  402. RETURN_OP_NAME(C_DIV);
  403. RETURN_OP_NAME(C_MOD);
  404. RETURN_OP_NAME(C_NEG);
  405. RETURN_OP_NAME(C_LNOT);
  406. RETURN_OP_NAME(C_NOT);
  407. RETURN_OP_NAME(C_R_SHIFT);
  408. RETURN_OP_NAME(C_L_SHIFT);
  409. default:
  410. ShowDebug("script_op2name: unexpected op=%d\n", op);
  411. return "???";
  412. }
  413. #undef RETURN_OP_NAME
  414. }
  415. #ifdef DEBUG_DUMP_STACK
  416. static void script_dump_stack(struct script_state* st)
  417. {
  418. int i;
  419. ShowMessage("\tstart = %d\n", st->start);
  420. ShowMessage("\tend = %d\n", st->end);
  421. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  422. ShowMessage("\tsp = %d\n", st->stack->sp);
  423. for( i = 0; i < st->stack->sp; ++i )
  424. {
  425. struct script_data* data = &st->stack->stack_data[i];
  426. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  427. switch( data->type )
  428. {
  429. case C_INT:
  430. case C_POS:
  431. ShowMessage(" %d\n", data->u.num);
  432. break;
  433. case C_STR:
  434. case C_CONSTSTR:
  435. ShowMessage(" \"%s\"\n", data->u.str);
  436. break;
  437. case C_NAME:
  438. 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));
  439. break;
  440. case C_RETINFO:
  441. {
  442. struct script_retinfo* ri = data->u.ri;
  443. 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);
  444. }
  445. break;
  446. default:
  447. ShowMessage("\n");
  448. break;
  449. }
  450. }
  451. }
  452. #endif
  453. /// Reports on the console the src of a script error.
  454. static void script_reportsrc(struct script_state *st)
  455. {
  456. struct block_list* bl;
  457. if( st->oid == 0 )
  458. return; //Can't report source.
  459. bl = map_id2bl(st->oid);
  460. if( bl == NULL )
  461. return;
  462. switch( bl->type )
  463. {
  464. case BL_NPC:
  465. if( bl->m >= 0 )
  466. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  467. else
  468. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  469. break;
  470. default:
  471. if( bl->m >= 0 )
  472. 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);
  473. else
  474. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  475. break;
  476. }
  477. }
  478. /// Reports on the console information about the script data.
  479. static void script_reportdata(struct script_data* data)
  480. {
  481. if( data == NULL )
  482. return;
  483. switch( data->type )
  484. {
  485. case C_NOP:// no value
  486. ShowDebug("Data: nothing (nil)\n");
  487. break;
  488. case C_INT:// number
  489. ShowDebug("Data: number value=%d\n", data->u.num);
  490. break;
  491. case C_STR:
  492. case C_CONSTSTR:// string
  493. if( data->u.str )
  494. {
  495. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  496. }
  497. else
  498. {
  499. ShowDebug("Data: string value=NULL\n");
  500. }
  501. break;
  502. case C_NAME:// reference
  503. if( reference_tovariable(data) )
  504. {// variable
  505. const char* name = reference_getname(data);
  506. if( not_array_variable(*name) )
  507. ShowDebug("Data: variable name='%s'\n", name);
  508. else
  509. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  510. }
  511. else if( reference_toconstant(data) )
  512. {// constant
  513. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  514. }
  515. else if( reference_toparam(data) )
  516. {// param
  517. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  518. }
  519. else
  520. {// ???
  521. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  522. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  523. }
  524. break;
  525. case C_POS:// label
  526. ShowDebug("Data: label pos=%d\n", data->u.num);
  527. break;
  528. default:
  529. ShowDebug("Data: %s\n", script_op2name(data->type));
  530. break;
  531. }
  532. }
  533. /// Reports on the console information about the current built-in function.
  534. static void script_reportfunc(struct script_state* st)
  535. {
  536. int i, params, id;
  537. struct script_data* data;
  538. if( !script_hasdata(st,0) )
  539. {// no stack
  540. return;
  541. }
  542. data = script_getdata(st,0);
  543. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  544. {// script currently not executing a built-in function or corrupt stack
  545. return;
  546. }
  547. id = reference_getid(data);
  548. params = script_lastdata(st)-1;
  549. if( params > 0 )
  550. {
  551. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  552. for( i = 2; i <= script_lastdata(st); i++ )
  553. {
  554. script_reportdata(script_getdata(st,i));
  555. }
  556. }
  557. else
  558. {
  559. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  560. }
  561. }
  562. /*==========================================
  563. * Output error message
  564. *------------------------------------------*/
  565. static void disp_error_message2(const char *mes,const char *pos,int report)
  566. {
  567. error_msg = aStrdup(mes);
  568. error_pos = pos;
  569. error_report = report;
  570. longjmp( error_jump, 1 );
  571. }
  572. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  573. /// Checks event parameter validity
  574. static void check_event(struct script_state *st, const char *evt)
  575. {
  576. if( evt && evt[0] && !stristr(evt, "::On") )
  577. {
  578. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  579. script_reportsrc(st);
  580. }
  581. }
  582. /*==========================================
  583. * Hashes the input string
  584. *------------------------------------------*/
  585. static unsigned int calc_hash(const char* p)
  586. {
  587. unsigned int h;
  588. #if defined(SCRIPT_HASH_DJB2)
  589. h = 5381;
  590. while( *p ) // hash*33 + c
  591. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  592. #elif defined(SCRIPT_HASH_SDBM)
  593. h = 0;
  594. while( *p ) // hash*65599 + c
  595. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  596. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  597. h = 0;
  598. while( *p ){
  599. unsigned int g;
  600. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  601. g = h & 0xF0000000;
  602. if( g )
  603. {
  604. h ^= g >> 24;
  605. h &= ~g;
  606. }
  607. }
  608. #else // athena hash
  609. h = 0;
  610. while( *p )
  611. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  612. #endif
  613. return h % SCRIPT_HASH_SIZE;
  614. }
  615. /*==========================================
  616. * str_data manipulation functions
  617. *------------------------------------------*/
  618. /// Looks up string using the provided id.
  619. const char* get_str(int id)
  620. {
  621. Assert( id >= LABEL_START && id < str_size );
  622. return str_buf+str_data[id].str;
  623. }
  624. /// Returns the uid of the string, or -1.
  625. static int search_str(const char* p)
  626. {
  627. int i;
  628. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  629. if( strcasecmp(get_str(i),p) == 0 )
  630. return i;
  631. return -1;
  632. }
  633. /// Stores a copy of the string and returns its id.
  634. /// If an identical string is already present, returns its id instead.
  635. int add_str(const char* p)
  636. {
  637. int i, h;
  638. int len;
  639. h = calc_hash(p);
  640. if( str_hash[h] == 0 )
  641. {// empty bucket, add new node here
  642. str_hash[h] = str_num;
  643. }
  644. else
  645. {// scan for end of list, or occurence of identical string
  646. for( i = str_hash[h]; ; i = str_data[i].next )
  647. {
  648. if( strcasecmp(get_str(i),p) == 0 )
  649. return i; // string already in list
  650. if( str_data[i].next == 0 )
  651. break; // reached the end
  652. }
  653. // append node to end of list
  654. str_data[i].next = str_num;
  655. }
  656. // grow list if neccessary
  657. if( str_num >= str_data_size )
  658. {
  659. str_data_size += 128;
  660. RECREATE(str_data,struct str_data_struct,str_data_size);
  661. memset(str_data + (str_data_size - 128), '\0', 128);
  662. }
  663. len=(int)strlen(p);
  664. // grow string buffer if neccessary
  665. while( str_pos+len+1 >= str_size )
  666. {
  667. str_size += 256;
  668. RECREATE(str_buf,char,str_size);
  669. memset(str_buf + (str_size - 256), '\0', 256);
  670. }
  671. safestrncpy(str_buf+str_pos, p, len+1);
  672. str_data[str_num].type = C_NOP;
  673. str_data[str_num].str = str_pos;
  674. str_data[str_num].next = 0;
  675. str_data[str_num].func = NULL;
  676. str_data[str_num].backpatch = -1;
  677. str_data[str_num].label = -1;
  678. str_pos += len+1;
  679. return str_num++;
  680. }
  681. /// Appends 1 byte to the script buffer.
  682. static void add_scriptb(int a)
  683. {
  684. if( script_pos+1 >= script_size )
  685. {
  686. script_size += SCRIPT_BLOCK_SIZE;
  687. RECREATE(script_buf,unsigned char,script_size);
  688. }
  689. script_buf[script_pos++] = (uint8)(a);
  690. }
  691. /// Appends a c_op value to the script buffer.
  692. /// The value is variable-length encoded into 8-bit blocks.
  693. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  694. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  695. static void add_scriptc(int a)
  696. {
  697. while( a >= 0x40 )
  698. {
  699. add_scriptb((a&0x3f)|0x40);
  700. a = (a - 0x40) >> 6;
  701. }
  702. add_scriptb(a);
  703. }
  704. /// Appends an integer value to the script buffer.
  705. /// The value is variable-length encoded into 8-bit blocks.
  706. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  707. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  708. static void add_scripti(int a)
  709. {
  710. while( a >= 0x40 )
  711. {
  712. add_scriptb((a&0x3f)|0xc0);
  713. a = (a - 0x40) >> 6;
  714. }
  715. add_scriptb(a|0x80);
  716. }
  717. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  718. ///
  719. /// @param l The id of the str_data entry
  720. // Maximum up to 16M
  721. static void add_scriptl(int l)
  722. {
  723. int backpatch = str_data[l].backpatch;
  724. switch(str_data[l].type){
  725. case C_POS:
  726. case C_USERFUNC_POS:
  727. add_scriptc(C_POS);
  728. add_scriptb(str_data[l].label);
  729. add_scriptb(str_data[l].label>>8);
  730. add_scriptb(str_data[l].label>>16);
  731. break;
  732. case C_NOP:
  733. case C_USERFUNC:
  734. // Embedded data backpatch there is a possibility of label
  735. add_scriptc(C_NAME);
  736. str_data[l].backpatch = script_pos;
  737. add_scriptb(backpatch);
  738. add_scriptb(backpatch>>8);
  739. add_scriptb(backpatch>>16);
  740. break;
  741. case C_INT:
  742. add_scripti(abs(str_data[l].val));
  743. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  744. add_scriptc(C_NEG);
  745. break;
  746. default: // assume C_NAME
  747. add_scriptc(C_NAME);
  748. add_scriptb(l);
  749. add_scriptb(l>>8);
  750. add_scriptb(l>>16);
  751. break;
  752. }
  753. }
  754. /*==========================================
  755. * Resolve the label
  756. *------------------------------------------*/
  757. void set_label(int l,int pos, const char* script_pos)
  758. {
  759. int i,next;
  760. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  761. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  762. disp_error_message("set_label: invalid label name",script_pos);
  763. return;
  764. }
  765. if(str_data[l].label!=-1){
  766. disp_error_message("set_label: dup label ",script_pos);
  767. return;
  768. }
  769. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  770. str_data[l].label=pos;
  771. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  772. next=GETVALUE(script_buf,i);
  773. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  774. SETVALUE(script_buf,i,pos);
  775. i=next;
  776. }
  777. }
  778. /// Skips spaces and/or comments.
  779. const char* skip_space(const char* p)
  780. {
  781. if( p == NULL )
  782. return NULL;
  783. for(;;)
  784. {
  785. while( ISSPACE(*p) )
  786. ++p;
  787. if( *p == '/' && p[1] == '/' )
  788. {// line comment
  789. while(*p && *p!='\n')
  790. ++p;
  791. }
  792. else if( *p == '/' && p[1] == '*' )
  793. {// block comment
  794. p += 2;
  795. for(;;)
  796. {
  797. if( *p == '\0' )
  798. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  799. if( *p == '*' && p[1] == '/' )
  800. {// end of block comment
  801. p += 2;
  802. break;
  803. }
  804. ++p;
  805. }
  806. }
  807. else
  808. break;
  809. }
  810. return p;
  811. }
  812. /// Skips a word.
  813. /// A word consists of undercores and/or alphanumeric characters,
  814. /// and valid variable prefixes/postfixes.
  815. static
  816. const char* skip_word(const char* p)
  817. {
  818. // prefix
  819. switch( *p )
  820. {
  821. case '@':// temporary char variable
  822. ++p; break;
  823. case '#':// account variable
  824. p += ( p[1] == '#' ? 2 : 1 ); break;
  825. case '\'':// instance variable
  826. ++p; break;
  827. case '.':// npc variable
  828. p += ( p[1] == '@' ? 2 : 1 ); break;
  829. case '$':// global variable
  830. p += ( p[1] == '@' ? 2 : 1 ); break;
  831. }
  832. while( ISALNUM(*p) || *p == '_' )
  833. ++p;
  834. // postfix
  835. if( *p == '$' )// string
  836. p++;
  837. return p;
  838. }
  839. /// Adds a word to str_data.
  840. /// @see skip_word
  841. /// @see add_str
  842. static
  843. int add_word(const char* p)
  844. {
  845. char* word;
  846. int len;
  847. int i;
  848. // Check for a word
  849. len = skip_word(p) - p;
  850. if( len == 0 )
  851. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  852. // Duplicate the word
  853. word = (char*)aMalloc(len+1);
  854. memcpy(word, p, len);
  855. word[len] = 0;
  856. // add the word
  857. i = add_str(word);
  858. aFree(word);
  859. return i;
  860. }
  861. /// Parses a function call.
  862. /// The argument list can have parenthesis or not.
  863. /// The number of arguments is checked.
  864. static
  865. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  866. {
  867. const char* p2;
  868. const char* arg=NULL;
  869. int func;
  870. func = add_word(p);
  871. if( str_data[func].type == C_FUNC ){
  872. // buildin function
  873. add_scriptl(func);
  874. add_scriptc(C_ARG);
  875. arg = buildin_func[str_data[func].val].arg;
  876. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  877. // script defined function
  878. add_scriptl(buildin_callsub_ref);
  879. add_scriptc(C_ARG);
  880. add_scriptl(func);
  881. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  882. if( *arg == 0 )
  883. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  884. if( *arg != '*' )
  885. ++arg; // count func as argument
  886. } else {
  887. #ifdef SCRIPT_CALLFUNC_CHECK
  888. const char* name = get_str(func);
  889. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  890. #endif
  891. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  892. #ifdef SCRIPT_CALLFUNC_CHECK
  893. } else {;
  894. add_scriptl(buildin_callfunc_ref);
  895. add_scriptc(C_ARG);
  896. add_scriptc(C_STR);
  897. while( *name ) add_scriptb(*name ++);
  898. add_scriptb(0);
  899. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  900. if( *arg != '*' ) ++ arg;
  901. }
  902. #endif
  903. }
  904. p = skip_word(p);
  905. p = skip_space(p);
  906. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  907. syntax.curly[syntax.curly_count].count = 0;
  908. if( *p == ';' )
  909. {// <func name> ';'
  910. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  911. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  912. {// <func name> '(' ')'
  913. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  914. p = p2;
  915. /*
  916. } else if( 0 && require_paren && *p != '(' )
  917. {// <func name>
  918. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  919. */
  920. } else
  921. {// <func name> <arg list>
  922. if( require_paren ){
  923. if( *p != '(' )
  924. disp_error_message("need '('",p);
  925. ++p; // skip '('
  926. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  927. } else if( *p == '(' ){
  928. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  929. } else {
  930. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  931. }
  932. ++syntax.curly_count;
  933. while( *arg ) {
  934. p2=parse_subexpr(p,-1);
  935. if( p == p2 )
  936. break; // not an argument
  937. if( *arg != '*' )
  938. ++arg; // next argument
  939. p=skip_space(p2);
  940. if( *arg == 0 || *p != ',' )
  941. break; // no more arguments
  942. ++p; // skip comma
  943. }
  944. --syntax.curly_count;
  945. }
  946. if( *arg && *arg != '?' && *arg != '*' )
  947. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  948. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  949. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  950. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  951. if( *p != ')' )
  952. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  953. ++p;
  954. }
  955. add_scriptc(C_FUNC);
  956. return p;
  957. }
  958. /// Processes end of logical script line.
  959. /// @param first When true, only fix up scheduling data is initialized
  960. /// @param p Script position for error reporting in set_label
  961. static void parse_nextline(bool first, const char* p)
  962. {
  963. if( !first )
  964. {
  965. add_scriptc(C_EOL); // mark end of line for stack cleanup
  966. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  967. }
  968. // initialize data for new '-' label fix up scheduling
  969. str_data[LABEL_NEXTLINE].type = C_NOP;
  970. str_data[LABEL_NEXTLINE].backpatch = -1;
  971. str_data[LABEL_NEXTLINE].label = -1;
  972. }
  973. /// Parse a variable assignment using the direct equals operator
  974. /// @param p script position where the function should run from
  975. /// @return NULL if not a variable assignment, the new position otherwise
  976. const char* parse_variable(const char* p) {
  977. int i, j, word;
  978. c_op type = C_NOP;
  979. const char *p2 = NULL;
  980. const char *var = p;
  981. // skip the variable where applicable
  982. p = skip_word(p);
  983. p = skip_space(p);
  984. if( p == NULL ) {// end of the line or invalid buffer
  985. return NULL;
  986. }
  987. if( *p == '[' ) {// array variable so process the array as appropriate
  988. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  989. if( *p ++ == ']' && --(j) == 0 ) break;
  990. if( *p == '[' ) ++ j;
  991. }
  992. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  993. disp_error_message("Missing right expression or closing bracket for variable.", p);
  994. }
  995. }
  996. if( type == C_NOP &&
  997. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  998. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  999. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1000. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1001. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1002. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1003. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1004. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1005. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1006. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1007. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  1008. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  1009. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1010. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1011. ) )
  1012. {// failed to find a matching operator combination so invalid
  1013. return NULL;
  1014. }
  1015. switch( type ) {
  1016. case C_EQ: {// incremental modifier
  1017. p = skip_space( &p[1] );
  1018. }
  1019. break;
  1020. case C_L_SHIFT:
  1021. case C_R_SHIFT: {// left or right shift modifier
  1022. p = skip_space( &p[3] );
  1023. }
  1024. break;
  1025. default: {// normal incremental command
  1026. p = skip_space( &p[2] );
  1027. }
  1028. }
  1029. if( p == NULL ) {// end of line or invalid buffer
  1030. return NULL;
  1031. }
  1032. // push the set function onto the stack
  1033. add_scriptl(buildin_set_ref);
  1034. add_scriptc(C_ARG);
  1035. // always append parenthesis to avoid errors
  1036. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1037. syntax.curly[syntax.curly_count].count = 0;
  1038. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1039. // increment the total curly count for the position in the script
  1040. ++ syntax.curly_count;
  1041. // parse the variable currently being modified
  1042. word = add_word(var);
  1043. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1044. {// cannot assign a variable which exists as a function or label
  1045. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1046. }
  1047. if( p2 ) {// process the variable index
  1048. const char* p3 = NULL;
  1049. // push the getelementofarray method into the stack
  1050. add_scriptl(buildin_getelementofarray_ref);
  1051. add_scriptc(C_ARG);
  1052. add_scriptl(word);
  1053. // process the sub-expression for this assignment
  1054. p3 = parse_subexpr(p2 + 1, 1);
  1055. p3 = skip_space(p3);
  1056. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1057. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1058. }
  1059. // push the closing function stack operator onto the stack
  1060. add_scriptc(C_FUNC);
  1061. p3 ++;
  1062. } else {// simply push the variable or value onto the stack
  1063. add_scriptl(word);
  1064. }
  1065. if( type != C_EQ )
  1066. add_scriptc(C_REF);
  1067. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1068. add_scripti(1);
  1069. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1070. } else {// process the value as an expression
  1071. p = parse_subexpr(p, -1);
  1072. if( type != C_EQ )
  1073. {// push the type of modifier onto the stack
  1074. add_scriptc(type);
  1075. }
  1076. }
  1077. // decrement the curly count for the position within the script
  1078. -- syntax.curly_count;
  1079. // close the script by appending the function operator
  1080. add_scriptc(C_FUNC);
  1081. // push the buffer from the method
  1082. return p;
  1083. }
  1084. /*==========================================
  1085. * Analysis section
  1086. *------------------------------------------*/
  1087. const char* parse_simpleexpr(const char *p)
  1088. {
  1089. int i;
  1090. p=skip_space(p);
  1091. if(*p==';' || *p==',')
  1092. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1093. if(*p=='('){
  1094. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1095. ++syntax.curly[i].count;
  1096. p=parse_subexpr(p+1,-1);
  1097. p=skip_space(p);
  1098. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1099. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1100. ){
  1101. if( *p == ',' ){
  1102. syntax.curly[i].flag = ARGLIST_PAREN;
  1103. return p;
  1104. } else
  1105. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1106. }
  1107. if( *p != ')' )
  1108. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1109. ++p;
  1110. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  1111. char *np;
  1112. while(*p == '0' && ISDIGIT(p[1])) p++;
  1113. i=strtoul(p,&np,0);
  1114. add_scripti(i);
  1115. p=np;
  1116. } else if(*p=='"'){
  1117. add_scriptc(C_STR);
  1118. p++;
  1119. while( *p && *p != '"' ){
  1120. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1121. {
  1122. char buf[8];
  1123. size_t len = skip_escaped_c(p) - p;
  1124. size_t n = sv_unescape_c(buf, p, len);
  1125. if( n != 1 )
  1126. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1127. p += len;
  1128. add_scriptb(*buf);
  1129. continue;
  1130. }
  1131. else if( *p == '\n' )
  1132. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1133. add_scriptb(*p++);
  1134. }
  1135. if(!*p)
  1136. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1137. add_scriptb(0);
  1138. p++; //'"'
  1139. } else {
  1140. int l;
  1141. const char* pv;
  1142. // label , register , function etc
  1143. if(skip_word(p)==p)
  1144. disp_error_message("parse_simpleexpr: unexpected character",p);
  1145. l=add_word(p);
  1146. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1147. return parse_callfunc(p,1,0);
  1148. #ifdef SCRIPT_CALLFUNC_CHECK
  1149. else {
  1150. const char* name = get_str(l);
  1151. if( strdb_get(userfunc_db,name) != NULL ) {
  1152. return parse_callfunc(p,1,1);
  1153. }
  1154. }
  1155. #endif
  1156. if( (pv = parse_variable(p)) )
  1157. {// successfully processed a variable assignment
  1158. return pv;
  1159. }
  1160. p=skip_word(p);
  1161. if( *p == '[' ){
  1162. // array(name[i] => getelementofarray(name,i) )
  1163. add_scriptl(buildin_getelementofarray_ref);
  1164. add_scriptc(C_ARG);
  1165. add_scriptl(l);
  1166. p=parse_subexpr(p+1,-1);
  1167. p=skip_space(p);
  1168. if( *p != ']' )
  1169. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1170. ++p;
  1171. add_scriptc(C_FUNC);
  1172. }else
  1173. add_scriptl(l);
  1174. }
  1175. return p;
  1176. }
  1177. /*==========================================
  1178. * Analysis of the expression
  1179. *------------------------------------------*/
  1180. const char* parse_subexpr(const char* p,int limit)
  1181. {
  1182. int op,opl,len;
  1183. const char* tmpp;
  1184. p=skip_space(p);
  1185. if( *p == '-' ){
  1186. tmpp = skip_space(p+1);
  1187. if( *tmpp == ';' || *tmpp == ',' ){
  1188. add_scriptl(LABEL_NEXTLINE);
  1189. p++;
  1190. return p;
  1191. }
  1192. }
  1193. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1194. p=parse_subexpr(p+1,10);
  1195. add_scriptc(op);
  1196. } else
  1197. p=parse_simpleexpr(p);
  1198. p=skip_space(p);
  1199. while((
  1200. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1201. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1202. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1203. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1204. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1205. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1206. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1207. (op=C_AND,opl=6,len=1,*p=='&') ||
  1208. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1209. (op=C_OR,opl=5,len=1,*p=='|') ||
  1210. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1211. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1212. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1213. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1214. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1215. (op=C_GT,opl=3,len=1,*p=='>') ||
  1216. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1217. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1218. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1219. p+=len;
  1220. if(op == C_OP3) {
  1221. p=parse_subexpr(p,-1);
  1222. p=skip_space(p);
  1223. if( *(p++) != ':')
  1224. disp_error_message("parse_subexpr: expected ':'", p-1);
  1225. p=parse_subexpr(p,-1);
  1226. } else {
  1227. p=parse_subexpr(p,opl);
  1228. }
  1229. add_scriptc(op);
  1230. p=skip_space(p);
  1231. }
  1232. return p; /* return first untreated operator */
  1233. }
  1234. /*==========================================
  1235. * Evaluation of the expression
  1236. *------------------------------------------*/
  1237. const char* parse_expr(const char *p)
  1238. {
  1239. switch(*p){
  1240. case ')': case ';': case ':': case '[': case ']':
  1241. case '}':
  1242. disp_error_message("parse_expr: unexpected character",p);
  1243. }
  1244. p=parse_subexpr(p,-1);
  1245. return p;
  1246. }
  1247. /*==========================================
  1248. * Analysis of the line
  1249. *------------------------------------------*/
  1250. const char* parse_line(const char* p)
  1251. {
  1252. const char* p2;
  1253. p=skip_space(p);
  1254. if(*p==';') {
  1255. //Close decision for if(); for(); while();
  1256. p = parse_syntax_close(p + 1);
  1257. return p;
  1258. }
  1259. if(*p==')' && parse_syntax_for_flag)
  1260. return p+1;
  1261. p = skip_space(p);
  1262. if(p[0] == '{') {
  1263. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1264. syntax.curly[syntax.curly_count].count = -1;
  1265. syntax.curly[syntax.curly_count].index = -1;
  1266. syntax.curly_count++;
  1267. return p + 1;
  1268. } else if(p[0] == '}') {
  1269. return parse_curly_close(p);
  1270. }
  1271. // Syntax-related processing
  1272. p2 = parse_syntax(p);
  1273. if(p2 != NULL)
  1274. return p2;
  1275. // attempt to process a variable assignment
  1276. p2 = parse_variable(p);
  1277. if( p2 != NULL )
  1278. {// variable assignment processed so leave the method
  1279. return parse_syntax_close(p2 + 1);
  1280. }
  1281. p = parse_callfunc(p,0,0);
  1282. p = skip_space(p);
  1283. if(parse_syntax_for_flag) {
  1284. if( *p != ')' )
  1285. disp_error_message("parse_line: expected ')'",p);
  1286. } else {
  1287. if( *p != ';' )
  1288. disp_error_message("parse_line: expected ';'",p);
  1289. }
  1290. //Binding decision for if(), for(), while()
  1291. p = parse_syntax_close(p+1);
  1292. return p;
  1293. }
  1294. // { ... } Closing process
  1295. const char* parse_curly_close(const char* p)
  1296. {
  1297. if(syntax.curly_count <= 0) {
  1298. disp_error_message("parse_curly_close: unexpected string",p);
  1299. return p + 1;
  1300. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1301. syntax.curly_count--;
  1302. //Close decision if, for , while
  1303. p = parse_syntax_close(p + 1);
  1304. return p;
  1305. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1306. //Closing switch()
  1307. int pos = syntax.curly_count-1;
  1308. char label[256];
  1309. int l;
  1310. // Remove temporary variables
  1311. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1312. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1313. parse_line(label);
  1314. syntax.curly_count--;
  1315. // Go to the end pointer unconditionally
  1316. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1317. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1318. parse_line(label);
  1319. syntax.curly_count--;
  1320. // You are here labeled
  1321. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1322. l=add_str(label);
  1323. set_label(l,script_pos, p);
  1324. if(syntax.curly[pos].flag) {
  1325. //Exists default
  1326. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1327. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1328. parse_line(label);
  1329. syntax.curly_count--;
  1330. }
  1331. // Label end
  1332. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1333. l=add_str(label);
  1334. set_label(l,script_pos, p);
  1335. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1336. syntax.curly_count--;
  1337. //Closing decision if, for , while
  1338. p = parse_syntax_close(p + 1);
  1339. return p;
  1340. } else {
  1341. disp_error_message("parse_curly_close: unexpected string",p);
  1342. return p + 1;
  1343. }
  1344. }
  1345. // Syntax-related processing
  1346. // break, case, continue, default, do, for, function,
  1347. // if, switch, while ? will handle this internally.
  1348. const char* parse_syntax(const char* p)
  1349. {
  1350. const char *p2 = skip_word(p);
  1351. switch(*p) {
  1352. case 'B':
  1353. case 'b':
  1354. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1355. // break Processing
  1356. char label[256];
  1357. int pos = syntax.curly_count - 1;
  1358. while(pos >= 0) {
  1359. if(syntax.curly[pos].type == TYPE_DO) {
  1360. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1361. break;
  1362. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1363. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1364. break;
  1365. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1366. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1367. break;
  1368. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1369. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1370. break;
  1371. }
  1372. pos--;
  1373. }
  1374. if(pos < 0) {
  1375. disp_error_message("parse_syntax: unexpected 'break'",p);
  1376. } else {
  1377. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1378. parse_line(label);
  1379. syntax.curly_count--;
  1380. }
  1381. p = skip_space(p2);
  1382. if(*p != ';')
  1383. disp_error_message("parse_syntax: expected ';'",p);
  1384. // Closing decision if, for , while
  1385. p = parse_syntax_close(p + 1);
  1386. return p;
  1387. }
  1388. break;
  1389. case 'c':
  1390. case 'C':
  1391. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1392. //Processing case
  1393. int pos = syntax.curly_count-1;
  1394. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1395. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1396. return p+1;
  1397. } else {
  1398. char label[256];
  1399. int l,v;
  1400. char *np;
  1401. if(syntax.curly[pos].count != 1) {
  1402. //Jump for FALLTHRU
  1403. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1404. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1405. parse_line(label);
  1406. syntax.curly_count--;
  1407. // You are here labeled
  1408. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1409. l=add_str(label);
  1410. set_label(l,script_pos, p);
  1411. }
  1412. //Decision statement switch
  1413. p = skip_space(p2);
  1414. if(p == p2) {
  1415. disp_error_message("parse_syntax: expected a space ' '",p);
  1416. }
  1417. // check whether case label is integer or not
  1418. v = strtol(p,&np,0);
  1419. if(np == p) { //Check for constants
  1420. p2 = skip_word(p);
  1421. v = p2-p; // length of word at p2
  1422. memcpy(label,p,v);
  1423. label[v]='\0';
  1424. if( !script_get_constant(label, &v) )
  1425. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1426. p = skip_word(p);
  1427. } else { //Numeric value
  1428. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1429. p++;
  1430. p = skip_word(p);
  1431. if(np != p)
  1432. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1433. }
  1434. p = skip_space(p);
  1435. if(*p != ':')
  1436. disp_error_message("parse_syntax: expect ':'",p);
  1437. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1438. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1439. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1440. // Bad I do not parse twice
  1441. p2 = parse_line(label);
  1442. parse_line(p2);
  1443. syntax.curly_count--;
  1444. if(syntax.curly[pos].count != 1) {
  1445. // Label after the completion of FALLTHRU
  1446. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1447. l=add_str(label);
  1448. set_label(l,script_pos,p);
  1449. }
  1450. // check duplication of case label [Rayce]
  1451. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1452. disp_error_message("parse_syntax: dup 'case'",p);
  1453. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1454. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1455. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1456. parse_line(label);
  1457. syntax.curly_count--;
  1458. syntax.curly[pos].count++;
  1459. }
  1460. return p + 1;
  1461. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1462. // Processing continue
  1463. char label[256];
  1464. int pos = syntax.curly_count - 1;
  1465. while(pos >= 0) {
  1466. if(syntax.curly[pos].type == TYPE_DO) {
  1467. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1468. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1469. break;
  1470. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1471. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1472. break;
  1473. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1474. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1475. break;
  1476. }
  1477. pos--;
  1478. }
  1479. if(pos < 0) {
  1480. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1481. } else {
  1482. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1483. parse_line(label);
  1484. syntax.curly_count--;
  1485. }
  1486. p = skip_space(p2);
  1487. if(*p != ';')
  1488. disp_error_message("parse_syntax: expected ';'",p);
  1489. //Closing decision if, for , while
  1490. p = parse_syntax_close(p + 1);
  1491. return p;
  1492. }
  1493. break;
  1494. case 'd':
  1495. case 'D':
  1496. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1497. // Switch - default processing
  1498. int pos = syntax.curly_count-1;
  1499. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1500. disp_error_message("parse_syntax: unexpected 'default'",p);
  1501. } else if(syntax.curly[pos].flag) {
  1502. disp_error_message("parse_syntax: dup 'default'",p);
  1503. } else {
  1504. char label[256];
  1505. int l;
  1506. // Put the label location
  1507. p = skip_space(p2);
  1508. if(*p != ':') {
  1509. disp_error_message("parse_syntax: expected ':'",p);
  1510. }
  1511. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1512. l=add_str(label);
  1513. set_label(l,script_pos,p);
  1514. // Skip to the next link w/o condition
  1515. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1516. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1517. parse_line(label);
  1518. syntax.curly_count--;
  1519. // The default label
  1520. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1521. l=add_str(label);
  1522. set_label(l,script_pos,p);
  1523. syntax.curly[syntax.curly_count - 1].flag = 1;
  1524. syntax.curly[pos].count++;
  1525. }
  1526. return p + 1;
  1527. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1528. int l;
  1529. char label[256];
  1530. p=skip_space(p2);
  1531. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1532. syntax.curly[syntax.curly_count].count = 1;
  1533. syntax.curly[syntax.curly_count].index = syntax.index++;
  1534. syntax.curly[syntax.curly_count].flag = 0;
  1535. // Label of the (do) form here
  1536. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1537. l=add_str(label);
  1538. set_label(l,script_pos,p);
  1539. syntax.curly_count++;
  1540. return p;
  1541. }
  1542. break;
  1543. case 'f':
  1544. case 'F':
  1545. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1546. int l;
  1547. char label[256];
  1548. int pos = syntax.curly_count;
  1549. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1550. syntax.curly[syntax.curly_count].count = 1;
  1551. syntax.curly[syntax.curly_count].index = syntax.index++;
  1552. syntax.curly[syntax.curly_count].flag = 0;
  1553. syntax.curly_count++;
  1554. p=skip_space(p2);
  1555. if(*p != '(')
  1556. disp_error_message("parse_syntax: expected '('",p);
  1557. p++;
  1558. // Execute the initialization statement
  1559. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1560. p=parse_line(p);
  1561. syntax.curly_count--;
  1562. // Form the start of label decision
  1563. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1564. l=add_str(label);
  1565. set_label(l,script_pos,p);
  1566. p=skip_space(p);
  1567. if(*p == ';') {
  1568. // For (; Because the pattern of always true ;)
  1569. ;
  1570. } else {
  1571. // Skip to the end point if the condition is false
  1572. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1573. add_scriptl(add_str("jump_zero"));
  1574. add_scriptc(C_ARG);
  1575. p=parse_expr(p);
  1576. p=skip_space(p);
  1577. add_scriptl(add_str(label));
  1578. add_scriptc(C_FUNC);
  1579. }
  1580. if(*p != ';')
  1581. disp_error_message("parse_syntax: expected ';'",p);
  1582. p++;
  1583. // Skip to the beginning of the loop
  1584. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1585. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1586. parse_line(label);
  1587. syntax.curly_count--;
  1588. // Labels to form the next loop
  1589. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1590. l=add_str(label);
  1591. set_label(l,script_pos,p);
  1592. // Process the next time you enter the loop
  1593. // A ')' last for; flag to be treated as'
  1594. parse_syntax_for_flag = 1;
  1595. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1596. p=parse_line(p);
  1597. syntax.curly_count--;
  1598. parse_syntax_for_flag = 0;
  1599. // Skip to the determination process conditions
  1600. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1601. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1602. parse_line(label);
  1603. syntax.curly_count--;
  1604. // Loop start labeling
  1605. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1606. l=add_str(label);
  1607. set_label(l,script_pos,p);
  1608. return p;
  1609. }
  1610. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1611. {// internal script function
  1612. const char *func_name;
  1613. func_name = skip_space(p2);
  1614. p = skip_word(func_name);
  1615. if( p == func_name )
  1616. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1617. p2 = skip_space(p);
  1618. if( *p2 == ';' )
  1619. {// function <name> ;
  1620. // function declaration - just register the name
  1621. int l;
  1622. l = add_word(func_name);
  1623. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1624. str_data[l].type = C_USERFUNC;
  1625. else if( str_data[l].type == C_USERFUNC )
  1626. ; // already registered
  1627. else
  1628. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1629. // Close condition of if, for, while
  1630. p = parse_syntax_close(p2 + 1);
  1631. return p;
  1632. }
  1633. else if(*p2 == '{')
  1634. {// function <name> <line/block of code>
  1635. char label[256];
  1636. int l;
  1637. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1638. syntax.curly[syntax.curly_count].count = 1;
  1639. syntax.curly[syntax.curly_count].index = syntax.index++;
  1640. syntax.curly[syntax.curly_count].flag = 0;
  1641. ++syntax.curly_count;
  1642. // Jump over the function code
  1643. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1644. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1645. ++syntax.curly_count;
  1646. parse_line(label);
  1647. --syntax.curly_count;
  1648. // Set the position of the function (label)
  1649. l=add_word(func_name);
  1650. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1651. {
  1652. str_data[l].type = C_USERFUNC;
  1653. set_label(l, script_pos, p);
  1654. if( parse_options&SCRIPT_USE_LABEL_DB )
  1655. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1656. }
  1657. else
  1658. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1659. return skip_space(p);
  1660. }
  1661. else
  1662. {
  1663. disp_error_message("expect ';' or '{' at function syntax",p);
  1664. }
  1665. }
  1666. break;
  1667. case 'i':
  1668. case 'I':
  1669. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1670. // If process
  1671. char label[256];
  1672. p=skip_space(p2);
  1673. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1674. disp_error_message("need '('",p);
  1675. }
  1676. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1677. syntax.curly[syntax.curly_count].count = 1;
  1678. syntax.curly[syntax.curly_count].index = syntax.index++;
  1679. syntax.curly[syntax.curly_count].flag = 0;
  1680. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1681. syntax.curly_count++;
  1682. add_scriptl(add_str("jump_zero"));
  1683. add_scriptc(C_ARG);
  1684. p=parse_expr(p);
  1685. p=skip_space(p);
  1686. add_scriptl(add_str(label));
  1687. add_scriptc(C_FUNC);
  1688. return p;
  1689. }
  1690. break;
  1691. case 's':
  1692. case 'S':
  1693. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1694. // Processing of switch ()
  1695. char label[256];
  1696. p=skip_space(p2);
  1697. if(*p != '(') {
  1698. disp_error_message("need '('",p);
  1699. }
  1700. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1701. syntax.curly[syntax.curly_count].count = 1;
  1702. syntax.curly[syntax.curly_count].index = syntax.index++;
  1703. syntax.curly[syntax.curly_count].flag = 0;
  1704. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1705. syntax.curly_count++;
  1706. add_scriptl(add_str("set"));
  1707. add_scriptc(C_ARG);
  1708. add_scriptl(add_str(label));
  1709. p=parse_expr(p);
  1710. p=skip_space(p);
  1711. if(*p != '{') {
  1712. disp_error_message("parse_syntax: expected '{'",p);
  1713. }
  1714. add_scriptc(C_FUNC);
  1715. return p + 1;
  1716. }
  1717. break;
  1718. case 'w':
  1719. case 'W':
  1720. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1721. int l;
  1722. char label[256];
  1723. p=skip_space(p2);
  1724. if(*p != '(') {
  1725. disp_error_message("need '('",p);
  1726. }
  1727. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1728. syntax.curly[syntax.curly_count].count = 1;
  1729. syntax.curly[syntax.curly_count].index = syntax.index++;
  1730. syntax.curly[syntax.curly_count].flag = 0;
  1731. // Form the start of label decision
  1732. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1733. l=add_str(label);
  1734. set_label(l,script_pos,p);
  1735. // Skip to the end point if the condition is false
  1736. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1737. syntax.curly_count++;
  1738. add_scriptl(add_str("jump_zero"));
  1739. add_scriptc(C_ARG);
  1740. p=parse_expr(p);
  1741. p=skip_space(p);
  1742. add_scriptl(add_str(label));
  1743. add_scriptc(C_FUNC);
  1744. return p;
  1745. }
  1746. break;
  1747. }
  1748. return NULL;
  1749. }
  1750. const char* parse_syntax_close(const char *p) {
  1751. // If (...) for (...) hoge (); as to make sure closed closed once again
  1752. int flag;
  1753. do {
  1754. p = parse_syntax_close_sub(p,&flag);
  1755. } while(flag);
  1756. return p;
  1757. }
  1758. // Close judgment if, for, while, of do
  1759. // flag == 1 : closed
  1760. // flag == 0 : not closed
  1761. const char* parse_syntax_close_sub(const char* p,int* flag)
  1762. {
  1763. char label[256];
  1764. int pos = syntax.curly_count - 1;
  1765. int l;
  1766. *flag = 1;
  1767. if(syntax.curly_count <= 0) {
  1768. *flag = 0;
  1769. return p;
  1770. } else if(syntax.curly[pos].type == TYPE_IF) {
  1771. const char *bp = p;
  1772. const char *p2;
  1773. // if-block and else-block end is a new line
  1774. parse_nextline(false, p);
  1775. // Skip to the last location if
  1776. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1777. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1778. parse_line(label);
  1779. syntax.curly_count--;
  1780. // Put the label of the location
  1781. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1782. l=add_str(label);
  1783. set_label(l,script_pos,p);
  1784. syntax.curly[pos].count++;
  1785. p = skip_space(p);
  1786. p2 = skip_word(p);
  1787. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1788. // else or else - if
  1789. p = skip_space(p2);
  1790. p2 = skip_word(p);
  1791. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1792. // else - if
  1793. p=skip_space(p2);
  1794. if(*p != '(') {
  1795. disp_error_message("need '('",p);
  1796. }
  1797. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1798. add_scriptl(add_str("jump_zero"));
  1799. add_scriptc(C_ARG);
  1800. p=parse_expr(p);
  1801. p=skip_space(p);
  1802. add_scriptl(add_str(label));
  1803. add_scriptc(C_FUNC);
  1804. *flag = 0;
  1805. return p;
  1806. } else {
  1807. // else
  1808. if(!syntax.curly[pos].flag) {
  1809. syntax.curly[pos].flag = 1;
  1810. *flag = 0;
  1811. return p;
  1812. }
  1813. }
  1814. }
  1815. // Close if
  1816. syntax.curly_count--;
  1817. // Put the label of the final location
  1818. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1819. l=add_str(label);
  1820. set_label(l,script_pos,p);
  1821. if(syntax.curly[pos].flag == 1) {
  1822. // Because the position of the pointer is the same if not else for this
  1823. return bp;
  1824. }
  1825. return p;
  1826. } else if(syntax.curly[pos].type == TYPE_DO) {
  1827. int l;
  1828. char label[256];
  1829. const char *p2;
  1830. if(syntax.curly[pos].flag) {
  1831. // (Come here continue) to form the label here
  1832. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1833. l=add_str(label);
  1834. set_label(l,script_pos,p);
  1835. }
  1836. // Skip to the end point if the condition is false
  1837. p = skip_space(p);
  1838. p2 = skip_word(p);
  1839. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1840. disp_error_message("parse_syntax: expected 'while'",p);
  1841. p = skip_space(p2);
  1842. if(*p != '(') {
  1843. disp_error_message("need '('",p);
  1844. }
  1845. // do-block end is a new line
  1846. parse_nextline(false, p);
  1847. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1848. add_scriptl(add_str("jump_zero"));
  1849. add_scriptc(C_ARG);
  1850. p=parse_expr(p);
  1851. p=skip_space(p);
  1852. add_scriptl(add_str(label));
  1853. add_scriptc(C_FUNC);
  1854. // Skip to the starting point
  1855. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1856. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1857. parse_line(label);
  1858. syntax.curly_count--;
  1859. // Form label of the end point conditions
  1860. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1861. l=add_str(label);
  1862. set_label(l,script_pos,p);
  1863. p = skip_space(p);
  1864. if(*p != ';') {
  1865. disp_error_message("parse_syntax: expected ';'",p);
  1866. return p+1;
  1867. }
  1868. p++;
  1869. syntax.curly_count--;
  1870. return p;
  1871. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1872. // for-block end is a new line
  1873. parse_nextline(false, p);
  1874. // Skip to the next loop
  1875. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1876. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1877. parse_line(label);
  1878. syntax.curly_count--;
  1879. // End for labeling
  1880. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1881. l=add_str(label);
  1882. set_label(l,script_pos,p);
  1883. syntax.curly_count--;
  1884. return p;
  1885. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1886. // while-block end is a new line
  1887. parse_nextline(false, p);
  1888. // Skip to the decision while
  1889. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1890. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1891. parse_line(label);
  1892. syntax.curly_count--;
  1893. // End while labeling
  1894. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1895. l=add_str(label);
  1896. set_label(l,script_pos,p);
  1897. syntax.curly_count--;
  1898. return p;
  1899. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1900. int pos = syntax.curly_count-1;
  1901. char label[256];
  1902. int l;
  1903. // Back
  1904. sprintf(label,"return;");
  1905. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1906. parse_line(label);
  1907. syntax.curly_count--;
  1908. // Put the label of the location
  1909. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1910. l=add_str(label);
  1911. set_label(l,script_pos,p);
  1912. syntax.curly_count--;
  1913. return p;
  1914. } else {
  1915. *flag = 0;
  1916. return p;
  1917. }
  1918. }
  1919. /*==========================================
  1920. * Added built-in functions
  1921. *------------------------------------------*/
  1922. static void add_buildin_func(void)
  1923. {
  1924. int i,n;
  1925. const char* p;
  1926. for( i = 0; buildin_func[i].func; i++ )
  1927. {
  1928. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1929. // 'v' - value (either string or int or reference)
  1930. // 's' - string
  1931. // 'i' - int
  1932. // 'r' - reference (of a variable)
  1933. // 'l' - label
  1934. // '?' - one optional parameter
  1935. // '*' - unknown number of optional parameters
  1936. p = buildin_func[i].arg;
  1937. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1938. while( *p == '?' ) ++p;
  1939. if( *p == '*' ) ++p;
  1940. if( *p != 0){
  1941. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1942. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1943. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1944. } else {
  1945. n = add_str(buildin_func[i].name);
  1946. str_data[n].type = C_FUNC;
  1947. str_data[n].val = i;
  1948. str_data[n].func = buildin_func[i].func;
  1949. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1950. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1951. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1952. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1953. }
  1954. }
  1955. }
  1956. /// Retrieves the value of a constant.
  1957. bool script_get_constant(const char* name, int* value)
  1958. {
  1959. int n = search_str(name);
  1960. if( n == -1 || str_data[n].type != C_INT )
  1961. {// not found or not a constant
  1962. return false;
  1963. }
  1964. value[0] = str_data[n].val;
  1965. return true;
  1966. }
  1967. /// Creates new constant or parameter with given value.
  1968. void script_set_constant(const char* name, int value, bool isparameter)
  1969. {
  1970. int n = add_str(name);
  1971. if( str_data[n].type == C_NOP )
  1972. {// new
  1973. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1974. str_data[n].val = value;
  1975. }
  1976. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1977. {// existing parameter or constant
  1978. 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);
  1979. }
  1980. else
  1981. {// existing name
  1982. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1983. }
  1984. }
  1985. /*==========================================
  1986. * Reading constant databases
  1987. * const.txt
  1988. *------------------------------------------*/
  1989. static void read_constdb(void)
  1990. {
  1991. FILE *fp;
  1992. char line[1024],name[1024],val[1024];
  1993. int type;
  1994. sprintf(line, "%s/const.txt", db_path);
  1995. fp=fopen(line, "r");
  1996. if(fp==NULL){
  1997. ShowError("can't read %s\n", line);
  1998. return ;
  1999. }
  2000. while(fgets(line, sizeof(line), fp))
  2001. {
  2002. if(line[0]=='/' && line[1]=='/')
  2003. continue;
  2004. type=0;
  2005. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  2006. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  2007. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2008. }
  2009. }
  2010. fclose(fp);
  2011. }
  2012. /*==========================================
  2013. * Display emplacement line of script
  2014. *------------------------------------------*/
  2015. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2016. {
  2017. int i;
  2018. if( p == NULL || !p[0] ) return NULL;
  2019. if( line < 0 )
  2020. StringBuf_Printf(buf, "*% 5d : ", -line);
  2021. else
  2022. StringBuf_Printf(buf, " % 5d : ", line);
  2023. for(i=0;p[i] && p[i] != '\n';i++){
  2024. if(p + i != mark)
  2025. StringBuf_Printf(buf, "%c", p[i]);
  2026. else
  2027. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2028. }
  2029. StringBuf_AppendStr(buf, "\n");
  2030. return p+i+(p[i] == '\n' ? 1 : 0);
  2031. }
  2032. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  2033. {
  2034. // Find the line where the error occurred
  2035. int j;
  2036. int line = start_line;
  2037. const char *p;
  2038. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2039. StringBuf buf;
  2040. for(p=src;p && *p;line++){
  2041. const char *lineend=strchr(p,'\n');
  2042. if(lineend==NULL || error_pos<lineend){
  2043. break;
  2044. }
  2045. for( j = 0; j < 4; j++ ) {
  2046. linestart[j] = linestart[j+1];
  2047. }
  2048. linestart[4] = p;
  2049. p=lineend+1;
  2050. }
  2051. StringBuf_Init(&buf);
  2052. StringBuf_AppendStr(&buf, "\a\n");
  2053. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  2054. StringBuf_Printf(&buf, " %s\n", error_msg);
  2055. for(j = 0; j < 5; j++ ) {
  2056. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  2057. }
  2058. p = script_print_line(&buf, p, error_pos, -line);
  2059. for(j = 0; j < 5; j++) {
  2060. p = script_print_line(&buf, p, NULL, line + j + 1 );
  2061. }
  2062. ShowError("%s", StringBuf_Value(&buf));
  2063. StringBuf_Destroy(&buf);
  2064. }
  2065. /*==========================================
  2066. * Analysis of the script
  2067. *------------------------------------------*/
  2068. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2069. {
  2070. const char *p,*tmpp;
  2071. int i;
  2072. struct script_code* code = NULL;
  2073. static int first=1;
  2074. char end;
  2075. bool unresolved_names = false;
  2076. if( src == NULL )
  2077. return NULL;// empty script
  2078. memset(&syntax,0,sizeof(syntax));
  2079. if(first){
  2080. add_buildin_func();
  2081. read_constdb();
  2082. first=0;
  2083. }
  2084. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2085. script_pos=0;
  2086. script_size=SCRIPT_BLOCK_SIZE;
  2087. parse_nextline(true, NULL);
  2088. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2089. if( options&SCRIPT_USE_LABEL_DB )
  2090. db_clear(scriptlabel_db);
  2091. parse_options = options;
  2092. if( setjmp( error_jump ) != 0 ) {
  2093. //Restore program state when script has problems. [from jA]
  2094. int i;
  2095. const int size = ARRAYLENGTH(syntax.curly);
  2096. if( error_report )
  2097. script_error(src,file,line,error_msg,error_pos);
  2098. aFree( error_msg );
  2099. aFree( script_buf );
  2100. script_pos = 0;
  2101. script_size = 0;
  2102. script_buf = NULL;
  2103. for(i=LABEL_START;i<str_num;i++)
  2104. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  2105. for(i=0; i<size; i++)
  2106. linkdb_final(&syntax.curly[i].case_label);
  2107. return NULL;
  2108. }
  2109. parse_syntax_for_flag=0;
  2110. p=src;
  2111. p=skip_space(p);
  2112. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2113. {// does not require brackets around the script
  2114. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2115. {// empty script and can return NULL
  2116. aFree( script_buf );
  2117. script_pos = 0;
  2118. script_size = 0;
  2119. script_buf = NULL;
  2120. return NULL;
  2121. }
  2122. end = '\0';
  2123. }
  2124. else
  2125. {// requires brackets around the script
  2126. if( *p != '{' )
  2127. disp_error_message("not found '{'",p);
  2128. p = skip_space(p+1);
  2129. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2130. {// empty script and can return NULL
  2131. aFree( script_buf );
  2132. script_pos = 0;
  2133. script_size = 0;
  2134. script_buf = NULL;
  2135. return NULL;
  2136. }
  2137. end = '}';
  2138. }
  2139. // clear references of labels, variables and internal functions
  2140. for(i=LABEL_START;i<str_num;i++){
  2141. if(
  2142. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2143. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2144. ){
  2145. str_data[i].type=C_NOP;
  2146. str_data[i].backpatch=-1;
  2147. str_data[i].label=-1;
  2148. }
  2149. }
  2150. while( syntax.curly_count != 0 || *p != end )
  2151. {
  2152. if( *p == '\0' )
  2153. disp_error_message("unexpected end of script",p);
  2154. // Special handling only label
  2155. tmpp=skip_space(skip_word(p));
  2156. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2157. i=add_word(p);
  2158. set_label(i,script_pos,p);
  2159. if( parse_options&SCRIPT_USE_LABEL_DB )
  2160. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2161. p=tmpp+1;
  2162. p=skip_space(p);
  2163. continue;
  2164. }
  2165. // All other lumped
  2166. p=parse_line(p);
  2167. p=skip_space(p);
  2168. parse_nextline(false, p);
  2169. }
  2170. add_scriptc(C_NOP);
  2171. // trim code to size
  2172. script_size = script_pos;
  2173. RECREATE(script_buf,unsigned char,script_pos);
  2174. // default unknown references to variables
  2175. for(i=LABEL_START;i<str_num;i++){
  2176. if(str_data[i].type==C_NOP){
  2177. int j,next;
  2178. str_data[i].type=C_NAME;
  2179. str_data[i].label=i;
  2180. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2181. next=GETVALUE(script_buf,j);
  2182. SETVALUE(script_buf,j,i);
  2183. j=next;
  2184. }
  2185. }
  2186. else if( str_data[i].type == C_USERFUNC )
  2187. {// 'function name;' without follow-up code
  2188. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2189. unresolved_names = true;
  2190. }
  2191. }
  2192. if( unresolved_names )
  2193. {
  2194. disp_error_message("parse_script: unresolved function references", p);
  2195. }
  2196. #ifdef DEBUG_DISP
  2197. for(i=0;i<script_pos;i++){
  2198. if((i&15)==0) ShowMessage("%04x : ",i);
  2199. ShowMessage("%02x ",script_buf[i]);
  2200. if((i&15)==15) ShowMessage("\n");
  2201. }
  2202. ShowMessage("\n");
  2203. #endif
  2204. #ifdef DEBUG_DISASM
  2205. {
  2206. int i = 0,j;
  2207. while(i < script_pos) {
  2208. c_op op = get_com(script_buf,&i);
  2209. ShowMessage("%06x %s", i, script_op2name(op));
  2210. j = i;
  2211. switch(op) {
  2212. case C_INT:
  2213. ShowMessage(" %d", get_num(script_buf,&i));
  2214. break;
  2215. case C_POS:
  2216. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2217. i += 3;
  2218. break;
  2219. case C_NAME:
  2220. j = (*(int*)(script_buf+i)&0xffffff);
  2221. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2222. i += 3;
  2223. break;
  2224. case C_STR:
  2225. j = strlen(script_buf + i);
  2226. ShowMessage(" %s", script_buf + i);
  2227. i += j+1;
  2228. break;
  2229. }
  2230. ShowMessage(CL_CLL"\n");
  2231. }
  2232. }
  2233. #endif
  2234. CREATE(code,struct script_code,1);
  2235. code->script_buf = script_buf;
  2236. code->script_size = script_size;
  2237. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2238. return code;
  2239. }
  2240. /// Returns the player attached to this script, identified by the rid.
  2241. /// If there is no player attached, the script is terminated.
  2242. TBL_PC *script_rid2sd(struct script_state *st)
  2243. {
  2244. TBL_PC *sd=map_id2sd(st->rid);
  2245. if(!sd){
  2246. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2247. script_reportfunc(st);
  2248. script_reportsrc(st);
  2249. st->state = END;
  2250. }
  2251. return sd;
  2252. }
  2253. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2254. ///
  2255. /// @param st Script state
  2256. /// @param data Variable/constant
  2257. void get_val(struct script_state* st, struct script_data* data)
  2258. {
  2259. const char* name;
  2260. char prefix;
  2261. char postfix;
  2262. TBL_PC* sd = NULL;
  2263. if( !data_isreference(data) )
  2264. return;// not a variable/constant
  2265. name = reference_getname(data);
  2266. prefix = name[0];
  2267. postfix = name[strlen(name) - 1];
  2268. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2269. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2270. {
  2271. sd = script_rid2sd(st);
  2272. if( sd == NULL )
  2273. {// needs player attached
  2274. if( postfix == '$' )
  2275. {// string variable
  2276. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2277. data->type = C_CONSTSTR;
  2278. data->u.str = "";
  2279. }
  2280. else
  2281. {// integer variable
  2282. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2283. data->type = C_INT;
  2284. data->u.num = 0;
  2285. }
  2286. return;
  2287. }
  2288. }
  2289. if( postfix == '$' )
  2290. {// string variable
  2291. switch( prefix )
  2292. {
  2293. case '@':
  2294. data->u.str = pc_readregstr(sd, data->u.num);
  2295. break;
  2296. case '$':
  2297. data->u.str = mapreg_readregstr(data->u.num);
  2298. break;
  2299. case '#':
  2300. if( name[1] == '#' )
  2301. data->u.str = pc_readaccountreg2str(sd, name);// global
  2302. else
  2303. data->u.str = pc_readaccountregstr(sd, name);// local
  2304. break;
  2305. case '.':
  2306. {
  2307. struct DBMap* n =
  2308. data->ref ? *data->ref:
  2309. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2310. st->script->script_vars;// npc variable
  2311. if( n )
  2312. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2313. else
  2314. data->u.str = NULL;
  2315. }
  2316. break;
  2317. case '\'':
  2318. {
  2319. int instance_id = script_instancegetid(st);
  2320. if( instance_id )
  2321. data->u.str = (char*)idb_get(instance_data[instance_id].vars,reference_getuid(data));
  2322. else {
  2323. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2324. data->u.str = NULL;
  2325. }
  2326. }
  2327. break;
  2328. default:
  2329. data->u.str = pc_readglobalreg_str(sd, name);
  2330. break;
  2331. }
  2332. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2333. {// empty string
  2334. data->type = C_CONSTSTR;
  2335. data->u.str = "";
  2336. }
  2337. else
  2338. {// duplicate string
  2339. data->type = C_STR;
  2340. data->u.str = aStrdup(data->u.str);
  2341. }
  2342. }
  2343. else
  2344. {// integer variable
  2345. data->type = C_INT;
  2346. if( reference_toconstant(data) )
  2347. {
  2348. data->u.num = reference_getconstant(data);
  2349. }
  2350. else if( reference_toparam(data) )
  2351. {
  2352. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2353. }
  2354. else
  2355. switch( prefix )
  2356. {
  2357. case '@':
  2358. data->u.num = pc_readreg(sd, data->u.num);
  2359. break;
  2360. case '$':
  2361. data->u.num = mapreg_readreg(data->u.num);
  2362. break;
  2363. case '#':
  2364. if( name[1] == '#' )
  2365. data->u.num = pc_readaccountreg2(sd, name);// global
  2366. else
  2367. data->u.num = pc_readaccountreg(sd, name);// local
  2368. break;
  2369. case '.':
  2370. {
  2371. struct DBMap* n =
  2372. data->ref ? *data->ref:
  2373. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2374. st->script->script_vars;// npc variable
  2375. if( n )
  2376. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2377. else
  2378. data->u.num = 0;
  2379. }
  2380. break;
  2381. case '\'':
  2382. {
  2383. int instance_id = script_instancegetid(st);
  2384. if( instance_id )
  2385. data->u.num = (int)idb_iget(instance_data[instance_id].vars,reference_getuid(data));
  2386. else {
  2387. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2388. data->u.num = 0;
  2389. }
  2390. }
  2391. break;
  2392. default:
  2393. data->u.num = pc_readglobalreg(sd, name);
  2394. break;
  2395. }
  2396. }
  2397. return;
  2398. }
  2399. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2400. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2401. /// The value is left in the top of the stack and needs to be removed manually.
  2402. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2403. {
  2404. struct script_data* data;
  2405. push_val2(st->stack, C_NAME, uid, ref);
  2406. data = script_getdatatop(st, -1);
  2407. get_val(st, data);
  2408. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2409. }
  2410. /*==========================================
  2411. * Stores the value of a script variable
  2412. * Return value is 0 on fail, 1 on success.
  2413. *------------------------------------------*/
  2414. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2415. {
  2416. char prefix = name[0];
  2417. if( is_string_variable(name) ) {// string variable
  2418. const char* str = (const char*)value;
  2419. switch (prefix) {
  2420. case '@':
  2421. return pc_setregstr(sd, num, str);
  2422. case '$':
  2423. return mapreg_setregstr(num, str);
  2424. case '#':
  2425. return (name[1] == '#') ?
  2426. pc_setaccountreg2str(sd, name, str) :
  2427. pc_setaccountregstr(sd, name, str);
  2428. case '.':
  2429. {
  2430. struct DBMap* n;
  2431. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2432. if( n ) {
  2433. idb_remove(n, num);
  2434. if (str[0]) idb_put(n, num, aStrdup(str));
  2435. }
  2436. }
  2437. return 1;
  2438. case '\'':
  2439. {
  2440. int instance_id = script_instancegetid(st);
  2441. if( instance_id ) {
  2442. idb_remove(instance_data[instance_id].vars, num);
  2443. if( str[0] ) idb_put(instance_data[instance_id].vars, num, aStrdup(str));
  2444. }
  2445. return 1;
  2446. }
  2447. default:
  2448. return pc_setglobalreg_str(sd, name, str);
  2449. }
  2450. } else {// integer variable
  2451. int val = (int)__64BPRTSIZE(value);
  2452. if(str_data[num&0x00ffffff].type == C_PARAM)
  2453. {
  2454. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2455. {
  2456. if( st != NULL )
  2457. {
  2458. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2459. script_reportsrc(st);
  2460. st->state = END;
  2461. }
  2462. return 0;
  2463. }
  2464. return 1;
  2465. }
  2466. switch (prefix) {
  2467. case '@':
  2468. return pc_setreg(sd, num, val);
  2469. case '$':
  2470. return mapreg_setreg(num, val);
  2471. case '#':
  2472. return (name[1] == '#') ?
  2473. pc_setaccountreg2(sd, name, val) :
  2474. pc_setaccountreg(sd, name, val);
  2475. case '.':
  2476. {
  2477. struct DBMap* n;
  2478. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2479. if( n ) {
  2480. idb_remove(n, num);
  2481. if( val != 0 )
  2482. idb_iput(n, num, val);
  2483. }
  2484. }
  2485. return 1;
  2486. case '\'':
  2487. {
  2488. int instance_id = script_instancegetid(st);
  2489. if( instance_id ) {
  2490. idb_remove(instance_data[instance_id].vars, num);
  2491. if( val != 0 )
  2492. idb_iput(instance_data[instance_id].vars, num, val);
  2493. }
  2494. return 1;
  2495. }
  2496. default:
  2497. return pc_setglobalreg(sd, name, val);
  2498. }
  2499. }
  2500. }
  2501. int set_var(TBL_PC* sd, char* name, void* val)
  2502. {
  2503. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2504. }
  2505. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2506. {
  2507. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2508. }
  2509. /// Converts the data to a string
  2510. const char* conv_str(struct script_state* st, struct script_data* data)
  2511. {
  2512. char* p;
  2513. get_val(st, data);
  2514. if( data_isstring(data) )
  2515. {// nothing to convert
  2516. }
  2517. else if( data_isint(data) )
  2518. {// int -> string
  2519. CREATE(p, char, ITEM_NAME_LENGTH);
  2520. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2521. p[ITEM_NAME_LENGTH-1] = '\0';
  2522. data->type = C_STR;
  2523. data->u.str = p;
  2524. }
  2525. else if( data_isreference(data) )
  2526. {// reference -> string
  2527. //##TODO when does this happen (check get_val) [FlavioJS]
  2528. data->type = C_CONSTSTR;
  2529. data->u.str = reference_getname(data);
  2530. }
  2531. else
  2532. {// unsupported data type
  2533. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2534. script_reportdata(data);
  2535. script_reportsrc(st);
  2536. data->type = C_CONSTSTR;
  2537. data->u.str = "";
  2538. }
  2539. return data->u.str;
  2540. }
  2541. /// Converts the data to an int
  2542. int conv_num(struct script_state* st, struct script_data* data)
  2543. {
  2544. char* p;
  2545. long num;
  2546. get_val(st, data);
  2547. if( data_isint(data) )
  2548. {// nothing to convert
  2549. }
  2550. else if( data_isstring(data) )
  2551. {// string -> int
  2552. // the result does not overflow or underflow, it is capped instead
  2553. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2554. p = data->u.str;
  2555. errno = 0;
  2556. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2557. if( errno == ERANGE
  2558. #if LONG_MAX > INT_MAX
  2559. || num < INT_MIN || num > INT_MAX
  2560. #endif
  2561. )
  2562. {
  2563. if( num <= INT_MIN )
  2564. {
  2565. num = INT_MIN;
  2566. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2567. }
  2568. else//if( num >= INT_MAX )
  2569. {
  2570. num = INT_MAX;
  2571. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2572. }
  2573. script_reportdata(data);
  2574. script_reportsrc(st);
  2575. }
  2576. if( data->type == C_STR )
  2577. aFree(p);
  2578. data->type = C_INT;
  2579. data->u.num = (int)num;
  2580. }
  2581. #if 0
  2582. // FIXME this function is being used to retrieve the position of labels and
  2583. // probably other stuff [FlavioJS]
  2584. else
  2585. {// unsupported data type
  2586. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2587. script_reportdata(data);
  2588. script_reportsrc(st);
  2589. data->type = C_INT;
  2590. data->u.num = 0;
  2591. }
  2592. #endif
  2593. return data->u.num;
  2594. }
  2595. //
  2596. // Stack operations
  2597. //
  2598. /// Increases the size of the stack
  2599. void stack_expand(struct script_stack* stack)
  2600. {
  2601. stack->sp_max += 64;
  2602. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2603. stack->sp_max * sizeof(stack->stack_data[0]) );
  2604. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2605. 64 * sizeof(stack->stack_data[0]) );
  2606. }
  2607. /// Pushes a value into the stack
  2608. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2609. /// Pushes a value into the stack (with reference)
  2610. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2611. {
  2612. if( stack->sp >= stack->sp_max )
  2613. stack_expand(stack);
  2614. stack->stack_data[stack->sp].type = type;
  2615. stack->stack_data[stack->sp].u.num = val;
  2616. stack->stack_data[stack->sp].ref = ref;
  2617. stack->sp++;
  2618. return &stack->stack_data[stack->sp-1];
  2619. }
  2620. /// Pushes a string into the stack
  2621. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2622. {
  2623. if( stack->sp >= stack->sp_max )
  2624. stack_expand(stack);
  2625. stack->stack_data[stack->sp].type = type;
  2626. stack->stack_data[stack->sp].u.str = str;
  2627. stack->stack_data[stack->sp].ref = NULL;
  2628. stack->sp++;
  2629. return &stack->stack_data[stack->sp-1];
  2630. }
  2631. /// Pushes a retinfo into the stack
  2632. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2633. {
  2634. if( stack->sp >= stack->sp_max )
  2635. stack_expand(stack);
  2636. stack->stack_data[stack->sp].type = C_RETINFO;
  2637. stack->stack_data[stack->sp].u.ri = ri;
  2638. stack->stack_data[stack->sp].ref = ref;
  2639. stack->sp++;
  2640. return &stack->stack_data[stack->sp-1];
  2641. }
  2642. /// Pushes a copy of the target position into the stack
  2643. struct script_data* push_copy(struct script_stack* stack, int pos)
  2644. {
  2645. switch( stack->stack_data[pos].type )
  2646. {
  2647. case C_CONSTSTR:
  2648. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2649. break;
  2650. case C_STR:
  2651. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2652. break;
  2653. case C_RETINFO:
  2654. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2655. exit(1);
  2656. break;
  2657. default:
  2658. return push_val2(
  2659. stack,stack->stack_data[pos].type,
  2660. stack->stack_data[pos].u.num,
  2661. stack->stack_data[pos].ref
  2662. );
  2663. break;
  2664. }
  2665. }
  2666. /// Removes the values in indexes [start,end[ from the stack.
  2667. /// Adjusts all stack pointers.
  2668. void pop_stack(struct script_state* st, int start, int end)
  2669. {
  2670. struct script_stack* stack = st->stack;
  2671. struct script_data* data;
  2672. int i;
  2673. if( start < 0 )
  2674. start = 0;
  2675. if( end > stack->sp )
  2676. end = stack->sp;
  2677. if( start >= end )
  2678. return;// nothing to pop
  2679. // free stack elements
  2680. for( i = start; i < end; i++ )
  2681. {
  2682. data = &stack->stack_data[i];
  2683. if( data->type == C_STR )
  2684. aFree(data->u.str);
  2685. if( data->type == C_RETINFO )
  2686. {
  2687. struct script_retinfo* ri = data->u.ri;
  2688. if( ri->var_function )
  2689. script_free_vars(ri->var_function);
  2690. if( data->ref )
  2691. aFree(data->ref);
  2692. aFree(ri);
  2693. }
  2694. data->type = C_NOP;
  2695. }
  2696. // move the rest of the elements
  2697. if( stack->sp > end )
  2698. {
  2699. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2700. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2701. stack->stack_data[i].type = C_NOP;
  2702. }
  2703. // adjust stack pointers
  2704. if( st->start > end ) st->start -= end - start;
  2705. else if( st->start > start ) st->start = start;
  2706. if( st->end > end ) st->end -= end - start;
  2707. else if( st->end > start ) st->end = start;
  2708. if( stack->defsp > end ) stack->defsp -= end - start;
  2709. else if( stack->defsp > start ) stack->defsp = start;
  2710. stack->sp -= end - start;
  2711. }
  2712. ///
  2713. ///
  2714. ///
  2715. /*==========================================
  2716. * Release script dependent variable, dependent variable of function
  2717. *------------------------------------------*/
  2718. void script_free_vars(struct DBMap* storage)
  2719. {
  2720. if( storage )
  2721. {// destroy the storage construct containing the variables
  2722. db_destroy(storage);
  2723. }
  2724. }
  2725. void script_free_code(struct script_code* code)
  2726. {
  2727. script_free_vars( code->script_vars );
  2728. aFree( code->script_buf );
  2729. aFree( code );
  2730. }
  2731. /// Creates a new script state.
  2732. ///
  2733. /// @param script Script code
  2734. /// @param pos Position in the code
  2735. /// @param rid Who is running the script (attached player)
  2736. /// @param oid Where the code is being run (npc 'object')
  2737. /// @return Script state
  2738. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2739. {
  2740. struct script_state* st;
  2741. CREATE(st, struct script_state, 1);
  2742. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2743. st->stack->sp = 0;
  2744. st->stack->sp_max = 64;
  2745. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2746. st->stack->defsp = st->stack->sp;
  2747. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2748. st->state = RUN;
  2749. st->script = script;
  2750. //st->scriptroot = script;
  2751. st->pos = pos;
  2752. st->rid = rid;
  2753. st->oid = oid;
  2754. st->sleep.timer = INVALID_TIMER;
  2755. st->npc_item_flag = battle_config.item_enabled_npc;
  2756. return st;
  2757. }
  2758. /// Frees a script state.
  2759. ///
  2760. /// @param st Script state
  2761. void script_free_state(struct script_state* st)
  2762. {
  2763. if(st->bk_st)
  2764. {// backup was not restored
  2765. 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);
  2766. }
  2767. if( st->sleep.timer != INVALID_TIMER )
  2768. delete_timer(st->sleep.timer, run_script_timer);
  2769. script_free_vars(st->stack->var_function);
  2770. pop_stack(st, 0, st->stack->sp);
  2771. aFree(st->stack->stack_data);
  2772. aFree(st->stack);
  2773. st->stack = NULL;
  2774. st->pos = -1;
  2775. aFree(st);
  2776. }
  2777. //
  2778. // Main execution unit
  2779. //
  2780. /*==========================================
  2781. * Read command
  2782. *------------------------------------------*/
  2783. c_op get_com(unsigned char *script,int *pos)
  2784. {
  2785. int i = 0, j = 0;
  2786. if(script[*pos]>=0x80){
  2787. return C_INT;
  2788. }
  2789. while(script[*pos]>=0x40){
  2790. i=script[(*pos)++]<<j;
  2791. j+=6;
  2792. }
  2793. return (c_op)(i+(script[(*pos)++]<<j));
  2794. }
  2795. /*==========================================
  2796. * Income figures
  2797. *------------------------------------------*/
  2798. int get_num(unsigned char *script,int *pos)
  2799. {
  2800. int i,j;
  2801. i=0; j=0;
  2802. while(script[*pos]>=0xc0){
  2803. i+=(script[(*pos)++]&0x7f)<<j;
  2804. j+=6;
  2805. }
  2806. return i+((script[(*pos)++]&0x7f)<<j);
  2807. }
  2808. /*==========================================
  2809. * Remove the value from the stack
  2810. *------------------------------------------*/
  2811. int pop_val(struct script_state* st)
  2812. {
  2813. if(st->stack->sp<=0)
  2814. return 0;
  2815. st->stack->sp--;
  2816. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2817. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2818. return st->stack->stack_data[st->stack->sp].u.num;
  2819. return 0;
  2820. }
  2821. /// Ternary operators
  2822. /// test ? if_true : if_false
  2823. void op_3(struct script_state* st, int op)
  2824. {
  2825. struct script_data* data;
  2826. int flag = 0;
  2827. data = script_getdatatop(st, -3);
  2828. get_val(st, data);
  2829. if( data_isstring(data) )
  2830. flag = data->u.str[0];// "" -> false
  2831. else if( data_isint(data) )
  2832. flag = data->u.num;// 0 -> false
  2833. else
  2834. {
  2835. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2836. script_reportdata(data);
  2837. script_reportsrc(st);
  2838. script_removetop(st, -3, 0);
  2839. script_pushnil(st);
  2840. return;
  2841. }
  2842. if( flag )
  2843. script_pushcopytop(st, -2);
  2844. else
  2845. script_pushcopytop(st, -1);
  2846. script_removetop(st, -4, -1);
  2847. }
  2848. /// Binary string operators
  2849. /// s1 EQ s2 -> i
  2850. /// s1 NE s2 -> i
  2851. /// s1 GT s2 -> i
  2852. /// s1 GE s2 -> i
  2853. /// s1 LT s2 -> i
  2854. /// s1 LE s2 -> i
  2855. /// s1 ADD s2 -> s
  2856. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2857. {
  2858. int a = 0;
  2859. switch(op){
  2860. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2861. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2862. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2863. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2864. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2865. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2866. case C_ADD:
  2867. {
  2868. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2869. strcpy(buf, s1);
  2870. strcat(buf, s2);
  2871. script_pushstr(st, buf);
  2872. return;
  2873. }
  2874. default:
  2875. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2876. script_reportsrc(st);
  2877. script_pushnil(st);
  2878. st->state = END;
  2879. return;
  2880. }
  2881. script_pushint(st,a);
  2882. }
  2883. /// Binary number operators
  2884. /// i OP i -> i
  2885. void op_2num(struct script_state* st, int op, int i1, int i2)
  2886. {
  2887. int ret;
  2888. double ret_double;
  2889. switch( op )
  2890. {
  2891. case C_AND: ret = i1 & i2; break;
  2892. case C_OR: ret = i1 | i2; break;
  2893. case C_XOR: ret = i1 ^ i2; break;
  2894. case C_LAND: ret = (i1 && i2); break;
  2895. case C_LOR: ret = (i1 || i2); break;
  2896. case C_EQ: ret = (i1 == i2); break;
  2897. case C_NE: ret = (i1 != i2); break;
  2898. case C_GT: ret = (i1 > i2); break;
  2899. case C_GE: ret = (i1 >= i2); break;
  2900. case C_LT: ret = (i1 < i2); break;
  2901. case C_LE: ret = (i1 <= i2); break;
  2902. case C_R_SHIFT: ret = i1>>i2; break;
  2903. case C_L_SHIFT: ret = i1<<i2; break;
  2904. case C_DIV:
  2905. case C_MOD:
  2906. if( i2 == 0 )
  2907. {
  2908. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2909. script_reportsrc(st);
  2910. script_pushnil(st);
  2911. st->state = END;
  2912. return;
  2913. }
  2914. else if( op == C_DIV )
  2915. ret = i1 / i2;
  2916. else//if( op == C_MOD )
  2917. ret = i1 % i2;
  2918. break;
  2919. default:
  2920. switch( op )
  2921. {// operators that can overflow/underflow
  2922. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2923. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2924. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2925. default:
  2926. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2927. script_reportsrc(st);
  2928. script_pushnil(st);
  2929. return;
  2930. }
  2931. if( ret_double < (double)INT_MIN )
  2932. {
  2933. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2934. script_reportsrc(st);
  2935. ret = INT_MIN;
  2936. }
  2937. else if( ret_double > (double)INT_MAX )
  2938. {
  2939. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2940. script_reportsrc(st);
  2941. ret = INT_MAX;
  2942. }
  2943. }
  2944. script_pushint(st, ret);
  2945. }
  2946. /// Binary operators
  2947. void op_2(struct script_state *st, int op)
  2948. {
  2949. struct script_data* left, leftref;
  2950. struct script_data* right;
  2951. leftref.type = C_NOP;
  2952. left = script_getdatatop(st, -2);
  2953. right = script_getdatatop(st, -1);
  2954. if (st->op2ref) {
  2955. if (data_isreference(left)) {
  2956. leftref = *left;
  2957. }
  2958. st->op2ref = 0;
  2959. }
  2960. get_val(st, left);
  2961. get_val(st, right);
  2962. // automatic conversions
  2963. switch( op )
  2964. {
  2965. case C_ADD:
  2966. if( data_isint(left) && data_isstring(right) )
  2967. {// convert int-string to string-string
  2968. conv_str(st, left);
  2969. }
  2970. else if( data_isstring(left) && data_isint(right) )
  2971. {// convert string-int to string-string
  2972. conv_str(st, right);
  2973. }
  2974. break;
  2975. }
  2976. if( data_isstring(left) && data_isstring(right) )
  2977. {// ss => op_2str
  2978. op_2str(st, op, left->u.str, right->u.str);
  2979. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  2980. if (leftref.type != C_NOP)
  2981. {
  2982. aFree(left->u.str);
  2983. *left = leftref;
  2984. }
  2985. }
  2986. else if( data_isint(left) && data_isint(right) )
  2987. {// ii => op_2num
  2988. int i1 = left->u.num;
  2989. int i2 = right->u.num;
  2990. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  2991. op_2num(st, op, i1, i2);
  2992. if (leftref.type != C_NOP)
  2993. *left = leftref;
  2994. }
  2995. else
  2996. {// invalid argument
  2997. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2998. script_reportdata(left);
  2999. script_reportdata(right);
  3000. script_reportsrc(st);
  3001. script_removetop(st, -2, 0);
  3002. script_pushnil(st);
  3003. st->state = END;
  3004. }
  3005. }
  3006. /// Unary operators
  3007. /// NEG i -> i
  3008. /// NOT i -> i
  3009. /// LNOT i -> i
  3010. void op_1(struct script_state* st, int op)
  3011. {
  3012. struct script_data* data;
  3013. int i1;
  3014. data = script_getdatatop(st, -1);
  3015. get_val(st, data);
  3016. if( !data_isint(data) )
  3017. {// not a number
  3018. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3019. script_reportdata(data);
  3020. script_reportsrc(st);
  3021. script_pushnil(st);
  3022. st->state = END;
  3023. return;
  3024. }
  3025. i1 = data->u.num;
  3026. script_removetop(st, -1, 0);
  3027. switch( op )
  3028. {
  3029. case C_NEG: i1 = -i1; break;
  3030. case C_NOT: i1 = ~i1; break;
  3031. case C_LNOT: i1 = !i1; break;
  3032. default:
  3033. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3034. script_reportsrc(st);
  3035. script_pushnil(st);
  3036. st->state = END;
  3037. return;
  3038. }
  3039. script_pushint(st, i1);
  3040. }
  3041. /// Checks the type of all arguments passed to a built-in function.
  3042. ///
  3043. /// @param st Script state whose stack arguments should be inspected.
  3044. /// @param func Built-in function for which the arguments are intended.
  3045. static void script_check_buildin_argtype(struct script_state* st, int func)
  3046. {
  3047. char type;
  3048. int idx, invalid = 0;
  3049. script_function* sf = &buildin_func[str_data[func].val];
  3050. for( idx = 2; script_hasdata(st, idx); idx++ )
  3051. {
  3052. struct script_data* data = script_getdata(st, idx);
  3053. type = sf->arg[idx-2];
  3054. if( type == '?' || type == '*' )
  3055. {// optional argument or unknown number of optional parameters ( no types are after this )
  3056. break;
  3057. }
  3058. else if( type == 0 )
  3059. {// more arguments than necessary ( should not happen, as it is checked before )
  3060. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3061. invalid++;
  3062. break;
  3063. }
  3064. else
  3065. {
  3066. const char* name = NULL;
  3067. if( data_isreference(data) )
  3068. {// get name for variables to determine the type they refer to
  3069. name = reference_getname(data);
  3070. }
  3071. switch( type )
  3072. {
  3073. case 'v':
  3074. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3075. {// variant
  3076. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3077. script_reportdata(data);
  3078. invalid++;
  3079. }
  3080. break;
  3081. case 's':
  3082. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3083. {// string
  3084. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3085. script_reportdata(data);
  3086. invalid++;
  3087. }
  3088. break;
  3089. case 'i':
  3090. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3091. {// int ( params and constants are always int )
  3092. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3093. script_reportdata(data);
  3094. invalid++;
  3095. }
  3096. break;
  3097. case 'r':
  3098. if( !data_isreference(data) )
  3099. {// variables
  3100. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3101. script_reportdata(data);
  3102. invalid++;
  3103. }
  3104. break;
  3105. case 'l':
  3106. if( !data_islabel(data) && !data_isfunclabel(data) )
  3107. {// label
  3108. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3109. script_reportdata(data);
  3110. invalid++;
  3111. }
  3112. break;
  3113. }
  3114. }
  3115. }
  3116. if(invalid)
  3117. {
  3118. ShowDebug("Function: %s\n", get_str(func));
  3119. script_reportsrc(st);
  3120. }
  3121. }
  3122. /// Executes a buildin command.
  3123. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3124. int run_func(struct script_state *st)
  3125. {
  3126. struct script_data* data;
  3127. int i,start_sp,end_sp,func;
  3128. end_sp = st->stack->sp;// position after the last argument
  3129. for( i = end_sp-1; i > 0 ; --i )
  3130. if( st->stack->stack_data[i].type == C_ARG )
  3131. break;
  3132. if( i == 0 ) {
  3133. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3134. st->state = END;
  3135. script_reportsrc(st);
  3136. return 1;
  3137. }
  3138. start_sp = i-1;// C_NAME of the command
  3139. st->start = start_sp;
  3140. st->end = end_sp;
  3141. data = &st->stack->stack_data[st->start];
  3142. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3143. func = data->u.num;
  3144. st->funcname = reference_getname(data);
  3145. } else {
  3146. ShowError("script:run_func: not a buildin command.\n");
  3147. script_reportdata(data);
  3148. script_reportsrc(st);
  3149. st->state = END;
  3150. return 1;
  3151. }
  3152. if( script_config.warn_func_mismatch_argtypes ) {
  3153. script_check_buildin_argtype(st, func);
  3154. }
  3155. if(str_data[func].func) {
  3156. if (str_data[func].func(st)) //Report error
  3157. script_reportsrc(st);
  3158. } else {
  3159. 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));
  3160. script_reportsrc(st);
  3161. st->state = END;
  3162. }
  3163. // Stack's datum are used when re-running functions [Eoe]
  3164. if( st->state == RERUNLINE )
  3165. return 0;
  3166. pop_stack(st, st->start, st->end);
  3167. if( st->state == RETFUNC ) {// return from a user-defined function
  3168. struct script_retinfo* ri;
  3169. int olddefsp = st->stack->defsp;
  3170. int nargs;
  3171. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3172. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3173. {
  3174. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3175. script_reportsrc(st);
  3176. st->state = END;
  3177. return 1;
  3178. }
  3179. script_free_vars( st->stack->var_function );
  3180. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3181. nargs = ri->nargs;
  3182. st->pos = ri->pos;
  3183. st->script = ri->script;
  3184. st->stack->var_function = ri->var_function;
  3185. st->stack->defsp = ri->defsp;
  3186. memset(ri, 0, sizeof(struct script_retinfo));
  3187. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3188. st->state = GOTO;
  3189. }
  3190. return 0;
  3191. }
  3192. /*==========================================
  3193. * script execution
  3194. *------------------------------------------*/
  3195. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3196. {
  3197. struct script_state *st;
  3198. if( rootscript == NULL || pos < 0 )
  3199. return;
  3200. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3201. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3202. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3203. st = script_alloc_state(rootscript, pos, rid, oid);
  3204. run_script_main(st);
  3205. }
  3206. void script_stop_sleeptimers(int id)
  3207. {
  3208. struct script_state* st;
  3209. for(;;)
  3210. {
  3211. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3212. if( st == NULL )
  3213. break; // no more sleep timers
  3214. script_free_state(st);
  3215. }
  3216. }
  3217. /*==========================================
  3218. * Delete the specified node from sleep_db
  3219. *------------------------------------------*/
  3220. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3221. {
  3222. struct linkdb_node *retnode;
  3223. if( n == NULL)
  3224. return NULL;
  3225. if( n->prev == NULL )
  3226. sleep_db = n->next;
  3227. else
  3228. n->prev->next = n->next;
  3229. if( n->next )
  3230. n->next->prev = n->prev;
  3231. retnode = n->next;
  3232. aFree( n );
  3233. return retnode; // The following; return retnode
  3234. }
  3235. /*==========================================
  3236. * Timer function for sleep
  3237. *------------------------------------------*/
  3238. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3239. {
  3240. struct script_state *st = (struct script_state *)data;
  3241. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3242. TBL_PC *sd = map_id2sd(st->rid);
  3243. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3244. { //Character mismatch. Cancel execution.
  3245. st->rid = 0;
  3246. st->state = END;
  3247. }
  3248. while( node && st->sleep.timer != INVALID_TIMER ) {
  3249. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3250. script_erase_sleepdb(node);
  3251. st->sleep.timer = INVALID_TIMER;
  3252. break;
  3253. }
  3254. node = node->next;
  3255. }
  3256. if(st->state != RERUNLINE)
  3257. st->sleep.tick = 0;
  3258. run_script_main(st);
  3259. return 0;
  3260. }
  3261. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3262. ///
  3263. /// @param st Script state to detach.
  3264. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3265. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3266. {
  3267. struct map_session_data* sd;
  3268. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3269. sd->st = st->bk_st;
  3270. sd->npc_id = st->bk_npcid;
  3271. if(st->bk_st) {
  3272. //Remove tag for removal.
  3273. st->bk_st = NULL;
  3274. st->bk_npcid = 0;
  3275. } else if(dequeue_event) {
  3276. #ifdef SECURE_NPCTIMEOUT
  3277. /**
  3278. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3279. **/
  3280. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3281. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3282. sd->npc_idle_timer = INVALID_TIMER;
  3283. }
  3284. #endif
  3285. npc_event_dequeue(sd);
  3286. }
  3287. }
  3288. else if(st->bk_st)
  3289. {// rid was set to 0, before detaching the script state
  3290. 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);
  3291. script_reportsrc(st->bk_st);
  3292. script_free_state(st->bk_st);
  3293. st->bk_st = NULL;
  3294. }
  3295. }
  3296. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3297. ///
  3298. /// @param st Script state to attach.
  3299. static void script_attach_state(struct script_state* st)
  3300. {
  3301. struct map_session_data* sd;
  3302. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3303. {
  3304. if(st!=sd->st)
  3305. {
  3306. if(st->bk_st)
  3307. {// there is already a backup
  3308. 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);
  3309. }
  3310. st->bk_st = sd->st;
  3311. st->bk_npcid = sd->npc_id;
  3312. }
  3313. sd->st = st;
  3314. sd->npc_id = st->oid;
  3315. sd->npc_item_flag = st->npc_item_flag; // load default.
  3316. #ifdef SECURE_NPCTIMEOUT
  3317. if( sd->npc_idle_timer == INVALID_TIMER )
  3318. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3319. sd->npc_idle_tick = gettick();
  3320. #endif
  3321. }
  3322. }
  3323. /*==========================================
  3324. * The main part of the script execution
  3325. *------------------------------------------*/
  3326. void run_script_main(struct script_state *st)
  3327. {
  3328. int cmdcount = script_config.check_cmdcount;
  3329. int gotocount = script_config.check_gotocount;
  3330. TBL_PC *sd;
  3331. struct script_stack *stack=st->stack;
  3332. script_attach_state(st);
  3333. if(st->state == RERUNLINE) {
  3334. run_func(st);
  3335. if(st->state == GOTO)
  3336. st->state = RUN;
  3337. } else if(st->state != END)
  3338. st->state = RUN;
  3339. while(st->state == RUN)
  3340. {
  3341. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3342. switch(c){
  3343. case C_EOL:
  3344. if( stack->defsp > stack->sp )
  3345. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3346. else
  3347. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3348. break;
  3349. case C_INT:
  3350. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3351. break;
  3352. case C_POS:
  3353. case C_NAME:
  3354. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3355. st->pos+=3;
  3356. break;
  3357. case C_ARG:
  3358. push_val(stack,c,0);
  3359. break;
  3360. case C_STR:
  3361. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3362. while(st->script->script_buf[st->pos++]);
  3363. break;
  3364. case C_FUNC:
  3365. run_func(st);
  3366. if(st->state==GOTO){
  3367. st->state = RUN;
  3368. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3369. ShowError("run_script: infinity loop !\n");
  3370. script_reportsrc(st);
  3371. st->state=END;
  3372. }
  3373. }
  3374. break;
  3375. case C_REF:
  3376. st->op2ref = 1;
  3377. break;
  3378. case C_NEG:
  3379. case C_NOT:
  3380. case C_LNOT:
  3381. op_1(st ,c);
  3382. break;
  3383. case C_ADD:
  3384. case C_SUB:
  3385. case C_MUL:
  3386. case C_DIV:
  3387. case C_MOD:
  3388. case C_EQ:
  3389. case C_NE:
  3390. case C_GT:
  3391. case C_GE:
  3392. case C_LT:
  3393. case C_LE:
  3394. case C_AND:
  3395. case C_OR:
  3396. case C_XOR:
  3397. case C_LAND:
  3398. case C_LOR:
  3399. case C_R_SHIFT:
  3400. case C_L_SHIFT:
  3401. op_2(st, c);
  3402. break;
  3403. case C_OP3:
  3404. op_3(st, c);
  3405. break;
  3406. case C_NOP:
  3407. st->state=END;
  3408. break;
  3409. default:
  3410. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3411. st->state=END;
  3412. break;
  3413. }
  3414. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3415. ShowError("run_script: infinity loop !\n");
  3416. script_reportsrc(st);
  3417. st->state=END;
  3418. }
  3419. }
  3420. if(st->sleep.tick > 0) {
  3421. //Restore previous script
  3422. script_detach_state(st, false);
  3423. //Delay execution
  3424. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3425. st->sleep.charid = sd?sd->status.char_id:0;
  3426. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3427. run_script_timer, st->sleep.charid, (intptr_t)st);
  3428. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3429. }
  3430. else if(st->state != END && st->rid){
  3431. //Resume later (st is already attached to player).
  3432. if(st->bk_st) {
  3433. ShowWarning("Unable to restore stack! Double continuation!\n");
  3434. //Report BOTH scripts to see if that can help somehow.
  3435. ShowDebug("Previous script (lost):\n");
  3436. script_reportsrc(st->bk_st);
  3437. ShowDebug("Current script:\n");
  3438. script_reportsrc(st);
  3439. script_free_state(st->bk_st);
  3440. st->bk_st = NULL;
  3441. }
  3442. } else {
  3443. //Dispose of script.
  3444. if ((sd = map_id2sd(st->rid))!=NULL)
  3445. { //Restore previous stack and save char.
  3446. if(sd->state.using_fake_npc){
  3447. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3448. sd->state.using_fake_npc = 0;
  3449. }
  3450. //Restore previous script if any.
  3451. script_detach_state(st, true);
  3452. if (sd->state.reg_dirty&2)
  3453. intif_saveregistry(sd,2);
  3454. if (sd->state.reg_dirty&1)
  3455. intif_saveregistry(sd,1);
  3456. }
  3457. script_free_state(st);
  3458. st = NULL;
  3459. }
  3460. }
  3461. int script_config_read(char *cfgName)
  3462. {
  3463. int i;
  3464. char line[1024],w1[1024],w2[1024];
  3465. FILE *fp;
  3466. fp=fopen(cfgName,"r");
  3467. if(fp==NULL){
  3468. ShowError("File not found: %s\n", cfgName);
  3469. return 1;
  3470. }
  3471. while(fgets(line, sizeof(line), fp))
  3472. {
  3473. if(line[0] == '/' && line[1] == '/')
  3474. continue;
  3475. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3476. if(i!=2)
  3477. continue;
  3478. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3479. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3480. }
  3481. else if(strcmpi(w1,"check_cmdcount")==0) {
  3482. script_config.check_cmdcount = config_switch(w2);
  3483. }
  3484. else if(strcmpi(w1,"check_gotocount")==0) {
  3485. script_config.check_gotocount = config_switch(w2);
  3486. }
  3487. else if(strcmpi(w1,"input_min_value")==0) {
  3488. script_config.input_min_value = config_switch(w2);
  3489. }
  3490. else if(strcmpi(w1,"input_max_value")==0) {
  3491. script_config.input_max_value = config_switch(w2);
  3492. }
  3493. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3494. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3495. }
  3496. else if(strcmpi(w1,"import")==0){
  3497. script_config_read(w2);
  3498. }
  3499. else {
  3500. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3501. }
  3502. }
  3503. fclose(fp);
  3504. return 0;
  3505. }
  3506. /**
  3507. * @see DBApply
  3508. */
  3509. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3510. {
  3511. struct script_code *code = db_data2ptr(data);
  3512. if (code)
  3513. script_free_code(code);
  3514. return 0;
  3515. }
  3516. void script_run_autobonus(const char *autobonus, int id, int pos)
  3517. {
  3518. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3519. if( script )
  3520. {
  3521. current_equip_item_index = pos;
  3522. run_script(script,0,id,0);
  3523. }
  3524. }
  3525. void script_add_autobonus(const char *autobonus)
  3526. {
  3527. if( strdb_get(autobonus_db, autobonus) == NULL )
  3528. {
  3529. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3530. if( script )
  3531. strdb_put(autobonus_db, autobonus, script);
  3532. }
  3533. }
  3534. /// resets a temporary character array variable to given value
  3535. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3536. {
  3537. int key;
  3538. uint8 idx;
  3539. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3540. {
  3541. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3542. return;
  3543. }
  3544. key = add_str(varname);
  3545. if( is_string_variable(varname) )
  3546. {
  3547. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3548. {
  3549. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3550. }
  3551. }
  3552. else
  3553. {
  3554. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3555. {
  3556. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3557. }
  3558. }
  3559. }
  3560. /// sets a temporary character array variable element idx to given value
  3561. /// @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.
  3562. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3563. {
  3564. int key;
  3565. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3566. {
  3567. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3568. return;
  3569. }
  3570. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3571. {
  3572. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3573. return;
  3574. }
  3575. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3576. if( is_string_variable(varname) )
  3577. {
  3578. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3579. }
  3580. else
  3581. {
  3582. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3583. }
  3584. if( refcache )
  3585. {// save to avoid repeated add_str calls
  3586. refcache[0] = key;
  3587. }
  3588. }
  3589. #ifdef BETA_THREAD_TEST
  3590. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3591. /* used to receive items the queryThread has already processed */
  3592. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3593. int i, cursor = 0;
  3594. bool allOk = true;
  3595. EnterSpinLock(&queryThreadLock);
  3596. for( i = 0; i < queryThreadData.count; i++ ) {
  3597. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3598. if( !entry->ok ) {
  3599. allOk = false;
  3600. continue;
  3601. }
  3602. run_script_main(entry->st);
  3603. entry->st = NULL;/* empty entries */
  3604. aFree(entry);
  3605. queryThreadData.entry[i] = NULL;
  3606. }
  3607. if( allOk ) {
  3608. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3609. delete_timer(queryThreadData.timer, queryThread_timer);
  3610. queryThreadData.timer = INVALID_TIMER;
  3611. }
  3612. /* now lets clear the mess. */
  3613. for( i = 0; i < queryThreadData.count; i++ ) {
  3614. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3615. if( entry == NULL )
  3616. continue;/* entry on hold */
  3617. /* move */
  3618. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3619. cursor++;
  3620. }
  3621. queryThreadData.count = cursor;
  3622. LeaveSpinLock(&queryThreadLock);
  3623. return 0;
  3624. }
  3625. void queryThread_add(struct script_state *st, bool type) {
  3626. int idx = 0;
  3627. struct queryThreadEntry* entry = NULL;
  3628. EnterSpinLock(&queryThreadLock);
  3629. if( queryThreadData.count++ != 0 )
  3630. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3631. idx = queryThreadData.count-1;
  3632. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3633. entry = queryThreadData.entry[idx];
  3634. entry->st = st;
  3635. entry->ok = false;
  3636. entry->type = type;
  3637. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3638. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3639. }
  3640. LeaveSpinLock(&queryThreadLock);
  3641. /* unlock the queryThread */
  3642. racond_signal(queryThreadCond);
  3643. }
  3644. /* adds a new log to the queue */
  3645. void queryThread_log(char * entry, int length) {
  3646. int idx = logThreadData.count;
  3647. EnterSpinLock(&queryThreadLock);
  3648. if( logThreadData.count++ != 0 )
  3649. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3650. CREATE(logThreadData.entry[idx], char, length + 1 );
  3651. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3652. LeaveSpinLock(&queryThreadLock);
  3653. /* unlock the queryThread */
  3654. racond_signal(queryThreadCond);
  3655. }
  3656. /* queryThread_main */
  3657. static void *queryThread_main(void *x) {
  3658. Sql *queryThread_handle = Sql_Malloc();
  3659. int i;
  3660. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) )
  3661. exit(EXIT_FAILURE);
  3662. if( strlen(default_codepage) > 0 )
  3663. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3664. Sql_ShowDebug(queryThread_handle);
  3665. if( log_config.sql_logs ) {
  3666. logmysql_handle = Sql_Malloc();
  3667. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) )
  3668. exit(EXIT_FAILURE);
  3669. if( strlen(default_codepage) > 0 )
  3670. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3671. Sql_ShowDebug(logmysql_handle);
  3672. }
  3673. while( 1 ) {
  3674. if(queryThreadTerminate > 0)
  3675. break;
  3676. EnterSpinLock(&queryThreadLock);
  3677. /* mess with queryThreadData within the lock */
  3678. for( i = 0; i < queryThreadData.count; i++ ) {
  3679. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3680. if( entry->ok )
  3681. continue;
  3682. else if ( !entry->st || !entry->st->stack ) {
  3683. entry->ok = true;/* dispose */
  3684. continue;
  3685. }
  3686. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3687. entry->ok = true;/* we're done with this */
  3688. }
  3689. /* also check for any logs in need to be sent */
  3690. if( log_config.sql_logs ) {
  3691. for( i = 0; i < logThreadData.count; i++ ) {
  3692. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3693. Sql_ShowDebug(logmysql_handle);
  3694. aFree(logThreadData.entry[i]);
  3695. }
  3696. logThreadData.count = 0;
  3697. }
  3698. LeaveSpinLock(&queryThreadLock);
  3699. ramutex_lock( queryThreadMutex );
  3700. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3701. ramutex_unlock( queryThreadMutex );
  3702. }
  3703. Sql_Free(queryThread_handle);
  3704. if( log_config.sql_logs ) {
  3705. Sql_Free(logmysql_handle);
  3706. }
  3707. return NULL;
  3708. }
  3709. #endif
  3710. /*==========================================
  3711. * Destructor
  3712. *------------------------------------------*/
  3713. int do_final_script() {
  3714. int i;
  3715. #ifdef DEBUG_HASH
  3716. if (battle_config.etc_log)
  3717. {
  3718. FILE *fp = fopen("hash_dump.txt","wt");
  3719. if(fp) {
  3720. int count[SCRIPT_HASH_SIZE];
  3721. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3722. int n=0;
  3723. int min=INT_MAX,max=0,zero=0;
  3724. double mean=0.0f;
  3725. double median=0.0f;
  3726. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3727. memset(count, 0, sizeof(count));
  3728. fprintf(fp,"num : hash : data_name\n");
  3729. fprintf(fp,"---------------------------------------------------------------\n");
  3730. for(i=LABEL_START; i<str_num; i++) {
  3731. unsigned int h = calc_hash(get_str(i));
  3732. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3733. ++count[h];
  3734. }
  3735. fprintf(fp,"--------------------\n\n");
  3736. memset(count2, 0, sizeof(count2));
  3737. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3738. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3739. if(min > count[i])
  3740. min = count[i]; // minimun count of collision
  3741. if(max < count[i])
  3742. max = count[i]; // maximun count of collision
  3743. if(count[i] == 0)
  3744. zero++;
  3745. ++count2[count[i]];
  3746. }
  3747. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3748. for( i=min; i <= max; ++i ){
  3749. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3750. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3751. }
  3752. for( i=min; i <= max; ++i ){
  3753. n += count2[i];
  3754. if( n*2 >= SCRIPT_HASH_SIZE )
  3755. {
  3756. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3757. median = (i+i+1)/2.0f;
  3758. else
  3759. median = i;
  3760. break;
  3761. }
  3762. }
  3763. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3764. fclose(fp);
  3765. }
  3766. }
  3767. #endif
  3768. mapreg_final();
  3769. db_destroy(scriptlabel_db);
  3770. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3771. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3772. if(sleep_db) {
  3773. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3774. while(n) {
  3775. struct script_state *st = (struct script_state *)n->data;
  3776. script_free_state(st);
  3777. n = n->next;
  3778. }
  3779. linkdb_final(&sleep_db);
  3780. }
  3781. if (str_data)
  3782. aFree(str_data);
  3783. if (str_buf)
  3784. aFree(str_buf);
  3785. for( i = 0; i < atcmd_binding_count; i++ ) {
  3786. aFree(atcmd_binding[i]);
  3787. }
  3788. if( atcmd_binding_count != 0 )
  3789. aFree(atcmd_binding);
  3790. #ifdef BETA_THREAD_TEST
  3791. /* QueryThread */
  3792. InterlockedIncrement(&queryThreadTerminate);
  3793. racond_signal(queryThreadCond);
  3794. rathread_wait(queryThread, NULL);
  3795. // Destroy cond var and mutex.
  3796. racond_destroy( queryThreadCond );
  3797. ramutex_destroy( queryThreadMutex );
  3798. /* Clear missing vars */
  3799. for( i = 0; i < queryThreadData.count; i++ ) {
  3800. aFree(queryThreadData.entry[i]);
  3801. }
  3802. aFree(queryThreadData.entry);
  3803. for( i = 0; i < logThreadData.count; i++ ) {
  3804. aFree(logThreadData.entry[i]);
  3805. }
  3806. aFree(logThreadData.entry);
  3807. #endif
  3808. return 0;
  3809. }
  3810. /*==========================================
  3811. * Initialization
  3812. *------------------------------------------*/
  3813. int do_init_script() {
  3814. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3815. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3816. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3817. mapreg_init();
  3818. #ifdef BETA_THREAD_TEST
  3819. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  3820. queryThreadData.count = 0;
  3821. CREATE(logThreadData.entry, char *, 1);
  3822. logThreadData.count = 0;
  3823. /* QueryThread Start */
  3824. InitializeSpinLock(&queryThreadLock);
  3825. queryThreadData.timer = INVALID_TIMER;
  3826. queryThreadTerminate = 0;
  3827. queryThreadMutex = ramutex_create();
  3828. queryThreadCond = racond_create();
  3829. queryThread = rathread_create(queryThread_main, NULL);
  3830. if(queryThread == NULL){
  3831. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  3832. exit(EXIT_FAILURE);
  3833. }
  3834. add_timer_func_list(queryThread_timer, "queryThread_timer");
  3835. #endif
  3836. return 0;
  3837. }
  3838. int script_reload() {
  3839. int i;
  3840. #ifdef BETA_THREAD_TEST
  3841. /* we're reloading so any queries undergoing should be...exterminated. */
  3842. EnterSpinLock(&queryThreadLock);
  3843. for( i = 0; i < queryThreadData.count; i++ ) {
  3844. aFree(queryThreadData.entry[i]);
  3845. }
  3846. queryThreadData.count = 0;
  3847. if( queryThreadData.timer != INVALID_TIMER ) {
  3848. delete_timer(queryThreadData.timer, queryThread_timer);
  3849. queryThreadData.timer = INVALID_TIMER;
  3850. }
  3851. LeaveSpinLock(&queryThreadLock);
  3852. #endif
  3853. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3854. db_clear(scriptlabel_db);
  3855. // @commands (script based)
  3856. // Clear bindings
  3857. for( i = 0; i < atcmd_binding_count; i++ ) {
  3858. aFree(atcmd_binding[i]);
  3859. }
  3860. if( atcmd_binding_count != 0 )
  3861. aFree(atcmd_binding);
  3862. atcmd_binding_count = 0;
  3863. if(sleep_db) {
  3864. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3865. while(n) {
  3866. struct script_state *st = (struct script_state *)n->data;
  3867. script_free_state(st);
  3868. n = n->next;
  3869. }
  3870. linkdb_final(&sleep_db);
  3871. }
  3872. mapreg_reload();
  3873. return 0;
  3874. }
  3875. //-----------------------------------------------------------------------------
  3876. // buildin functions
  3877. //
  3878. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3879. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3880. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3881. /////////////////////////////////////////////////////////////////////
  3882. // NPC interaction
  3883. //
  3884. /// Appends a message to the npc dialog.
  3885. /// If a dialog doesn't exist yet, one is created.
  3886. ///
  3887. /// mes "<message>";
  3888. BUILDIN_FUNC(mes)
  3889. {
  3890. TBL_PC* sd = script_rid2sd(st);
  3891. if( sd == NULL )
  3892. return 0;
  3893. if( !script_hasdata(st, 3) )
  3894. {// only a single line detected in the script
  3895. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3896. }
  3897. else
  3898. {// parse multiple lines as they exist
  3899. int i;
  3900. for( i = 2; script_hasdata(st, i); i++ )
  3901. {
  3902. // send the message to the client
  3903. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3904. }
  3905. }
  3906. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  3907. return 0;
  3908. }
  3909. /// Displays the button 'next' in the npc dialog.
  3910. /// The dialog text is cleared and the script continues when the button is pressed.
  3911. ///
  3912. /// next;
  3913. BUILDIN_FUNC(next)
  3914. {
  3915. TBL_PC* sd;
  3916. sd = script_rid2sd(st);
  3917. if( sd == NULL )
  3918. return 0;
  3919. #ifdef SECURE_NPCTIMEOUT
  3920. sd->npc_idle_type = NPCT_WAIT;
  3921. #endif
  3922. st->state = STOP;
  3923. clif_scriptnext(sd, st->oid);
  3924. return 0;
  3925. }
  3926. /// Ends the script and displays the button 'close' on the npc dialog.
  3927. /// The dialog is closed when the button is pressed.
  3928. ///
  3929. /// close;
  3930. BUILDIN_FUNC(close)
  3931. {
  3932. TBL_PC* sd;
  3933. sd = script_rid2sd(st);
  3934. if( sd == NULL )
  3935. return 0;
  3936. if( !st->mes_active ) {
  3937. TBL_NPC* nd = map_id2nd(st->oid);
  3938. st->state = END; // Keep backwards compatibility.
  3939. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  3940. } else {
  3941. st->state = CLOSE;
  3942. st->mes_active = 0;
  3943. }
  3944. clif_scriptclose(sd, st->oid);
  3945. return 0;
  3946. }
  3947. /// Displays the button 'close' on the npc dialog.
  3948. /// The dialog is closed and the script continues when the button is pressed.
  3949. ///
  3950. /// close2;
  3951. BUILDIN_FUNC(close2)
  3952. {
  3953. TBL_PC* sd;
  3954. sd = script_rid2sd(st);
  3955. if( sd == NULL )
  3956. return 0;
  3957. st->state = STOP;
  3958. if( st->mes_active )
  3959. st->mes_active = 0;
  3960. clif_scriptclose(sd, st->oid);
  3961. return 0;
  3962. }
  3963. /// Counts the number of valid and total number of options in 'str'
  3964. /// If max_count > 0 the counting stops when that valid option is reached
  3965. /// total is incremented for each option (NULL is supported)
  3966. static int menu_countoptions(const char* str, int max_count, int* total)
  3967. {
  3968. int count = 0;
  3969. int bogus_total;
  3970. if( total == NULL )
  3971. total = &bogus_total;
  3972. ++(*total);
  3973. // initial empty options
  3974. while( *str == ':' )
  3975. {
  3976. ++str;
  3977. ++(*total);
  3978. }
  3979. // count menu options
  3980. while( *str != '\0' )
  3981. {
  3982. ++count;
  3983. --max_count;
  3984. if( max_count == 0 )
  3985. break;
  3986. while( *str != ':' && *str != '\0' )
  3987. ++str;
  3988. while( *str == ':' )
  3989. {
  3990. ++str;
  3991. ++(*total);
  3992. }
  3993. }
  3994. return count;
  3995. }
  3996. /// Displays a menu with options and goes to the target label.
  3997. /// The script is stopped if cancel is pressed.
  3998. /// Options with no text are not displayed in the client.
  3999. ///
  4000. /// Options can be grouped together, separated by the character ':' in the text:
  4001. /// ex: menu "A:B:C",L_target;
  4002. /// All these options go to the specified target label.
  4003. ///
  4004. /// The index of the selected option is put in the variable @menu.
  4005. /// Indexes start with 1 and are consistent with grouped and empty options.
  4006. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4007. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4008. ///
  4009. /// NOTE: the client closes the npc dialog when cancel is pressed
  4010. ///
  4011. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4012. BUILDIN_FUNC(menu)
  4013. {
  4014. int i;
  4015. const char* text;
  4016. TBL_PC* sd;
  4017. sd = script_rid2sd(st);
  4018. if( sd == NULL )
  4019. return 0;
  4020. #ifdef SECURE_NPCTIMEOUT
  4021. sd->npc_idle_type = NPCT_MENU;
  4022. #endif
  4023. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4024. if( sd->state.menu_or_input == 0 )
  4025. {
  4026. struct StringBuf buf;
  4027. struct script_data* data;
  4028. if( script_lastdata(st) % 2 == 0 )
  4029. {// argument count is not even (1st argument is at index 2)
  4030. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4031. st->state = END;
  4032. return 1;
  4033. }
  4034. StringBuf_Init(&buf);
  4035. sd->npc_menu = 0;
  4036. for( i = 2; i < script_lastdata(st); i += 2 )
  4037. {
  4038. // menu options
  4039. text = script_getstr(st, i);
  4040. // target label
  4041. data = script_getdata(st, i+1);
  4042. if( !data_islabel(data) )
  4043. {// not a label
  4044. StringBuf_Destroy(&buf);
  4045. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4046. script_reportdata(data);
  4047. st->state = END;
  4048. return 1;
  4049. }
  4050. // append option(s)
  4051. if( text[0] == '\0' )
  4052. continue;// empty string, ignore
  4053. if( sd->npc_menu > 0 )
  4054. StringBuf_AppendStr(&buf, ":");
  4055. StringBuf_AppendStr(&buf, text);
  4056. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4057. }
  4058. st->state = RERUNLINE;
  4059. sd->state.menu_or_input = 1;
  4060. /**
  4061. * menus beyond this length crash the client (see bugreport:6402)
  4062. **/
  4063. if( StringBuf_Length(&buf) >= 2047 ) {
  4064. struct npc_data * nd = map_id2nd(st->oid);
  4065. char* menu;
  4066. CREATE(menu, char, 2048);
  4067. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4068. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4069. clif_scriptmenu(sd, st->oid, menu);
  4070. aFree(menu);
  4071. } else
  4072. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4073. StringBuf_Destroy(&buf);
  4074. if( sd->npc_menu >= 0xff )
  4075. {// 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
  4076. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4077. script_reportsrc(st);
  4078. }
  4079. }
  4080. else if( sd->npc_menu == 0xff )
  4081. {// Cancel was pressed
  4082. sd->state.menu_or_input = 0;
  4083. st->state = END;
  4084. }
  4085. else
  4086. {// goto target label
  4087. int menu = 0;
  4088. sd->state.menu_or_input = 0;
  4089. if( sd->npc_menu <= 0 )
  4090. {
  4091. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4092. st->state = END;
  4093. return 1;
  4094. }
  4095. // get target label
  4096. for( i = 2; i < script_lastdata(st); i += 2 )
  4097. {
  4098. text = script_getstr(st, i);
  4099. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4100. if( sd->npc_menu <= 0 )
  4101. break;// entry found
  4102. }
  4103. if( sd->npc_menu > 0 )
  4104. {// Invalid selection
  4105. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4106. st->state = END;
  4107. return 1;
  4108. }
  4109. if( !data_islabel(script_getdata(st, i + 1)) )
  4110. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4111. ShowError("script:menu: unexpected data in label argument\n");
  4112. script_reportdata(script_getdata(st, i + 1));
  4113. st->state = END;
  4114. return 1;
  4115. }
  4116. pc_setreg(sd, add_str("@menu"), menu);
  4117. st->pos = script_getnum(st, i + 1);
  4118. st->state = GOTO;
  4119. }
  4120. return 0;
  4121. }
  4122. /// Displays a menu with options and returns the selected option.
  4123. /// Behaves like 'menu' without the target labels.
  4124. ///
  4125. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4126. ///
  4127. /// @see menu
  4128. BUILDIN_FUNC(select)
  4129. {
  4130. int i;
  4131. const char* text;
  4132. TBL_PC* sd;
  4133. sd = script_rid2sd(st);
  4134. if( sd == NULL )
  4135. return 0;
  4136. #ifdef SECURE_NPCTIMEOUT
  4137. sd->npc_idle_type = NPCT_MENU;
  4138. #endif
  4139. if( sd->state.menu_or_input == 0 ) {
  4140. struct StringBuf buf;
  4141. StringBuf_Init(&buf);
  4142. sd->npc_menu = 0;
  4143. for( i = 2; i <= script_lastdata(st); ++i ) {
  4144. text = script_getstr(st, i);
  4145. if( sd->npc_menu > 0 )
  4146. StringBuf_AppendStr(&buf, ":");
  4147. StringBuf_AppendStr(&buf, text);
  4148. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4149. }
  4150. st->state = RERUNLINE;
  4151. sd->state.menu_or_input = 1;
  4152. /**
  4153. * menus beyond this length crash the client (see bugreport:6402)
  4154. **/
  4155. if( StringBuf_Length(&buf) >= 2047 ) {
  4156. struct npc_data * nd = map_id2nd(st->oid);
  4157. char* menu;
  4158. CREATE(menu, char, 2048);
  4159. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4160. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4161. clif_scriptmenu(sd, st->oid, menu);
  4162. aFree(menu);
  4163. } else
  4164. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4165. StringBuf_Destroy(&buf);
  4166. if( sd->npc_menu >= 0xff ) {
  4167. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4168. script_reportsrc(st);
  4169. }
  4170. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4171. sd->state.menu_or_input = 0;
  4172. st->state = END;
  4173. } else {// return selected option
  4174. int menu = 0;
  4175. sd->state.menu_or_input = 0;
  4176. for( i = 2; i <= script_lastdata(st); ++i ) {
  4177. text = script_getstr(st, i);
  4178. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4179. if( sd->npc_menu <= 0 )
  4180. break;// entry found
  4181. }
  4182. pc_setreg(sd, add_str("@menu"), menu);
  4183. script_pushint(st, menu);
  4184. st->state = RUN;
  4185. }
  4186. return 0;
  4187. }
  4188. /// Displays a menu with options and returns the selected option.
  4189. /// Behaves like 'menu' without the target labels, except when cancel is
  4190. /// pressed.
  4191. /// When cancel is pressed, the script continues and 255 is returned.
  4192. ///
  4193. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4194. ///
  4195. /// @see menu
  4196. BUILDIN_FUNC(prompt)
  4197. {
  4198. int i;
  4199. const char *text;
  4200. TBL_PC* sd;
  4201. sd = script_rid2sd(st);
  4202. if( sd == NULL )
  4203. return 0;
  4204. #ifdef SECURE_NPCTIMEOUT
  4205. sd->npc_idle_type = NPCT_MENU;
  4206. #endif
  4207. if( sd->state.menu_or_input == 0 )
  4208. {
  4209. struct StringBuf buf;
  4210. StringBuf_Init(&buf);
  4211. sd->npc_menu = 0;
  4212. for( i = 2; i <= script_lastdata(st); ++i )
  4213. {
  4214. text = script_getstr(st, i);
  4215. if( sd->npc_menu > 0 )
  4216. StringBuf_AppendStr(&buf, ":");
  4217. StringBuf_AppendStr(&buf, text);
  4218. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4219. }
  4220. st->state = RERUNLINE;
  4221. sd->state.menu_or_input = 1;
  4222. /**
  4223. * menus beyond this length crash the client (see bugreport:6402)
  4224. **/
  4225. if( StringBuf_Length(&buf) >= 2047 ) {
  4226. struct npc_data * nd = map_id2nd(st->oid);
  4227. char* menu;
  4228. CREATE(menu, char, 2048);
  4229. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4230. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4231. clif_scriptmenu(sd, st->oid, menu);
  4232. aFree(menu);
  4233. } else
  4234. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4235. StringBuf_Destroy(&buf);
  4236. if( sd->npc_menu >= 0xff )
  4237. {
  4238. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4239. script_reportsrc(st);
  4240. }
  4241. }
  4242. else if( sd->npc_menu == 0xff )
  4243. {// Cancel was pressed
  4244. sd->state.menu_or_input = 0;
  4245. pc_setreg(sd, add_str("@menu"), 0xff);
  4246. script_pushint(st, 0xff);
  4247. st->state = RUN;
  4248. }
  4249. else
  4250. {// return selected option
  4251. int menu = 0;
  4252. sd->state.menu_or_input = 0;
  4253. for( i = 2; i <= script_lastdata(st); ++i )
  4254. {
  4255. text = script_getstr(st, i);
  4256. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4257. if( sd->npc_menu <= 0 )
  4258. break;// entry found
  4259. }
  4260. pc_setreg(sd, add_str("@menu"), menu);
  4261. script_pushint(st, menu);
  4262. st->state = RUN;
  4263. }
  4264. return 0;
  4265. }
  4266. /////////////////////////////////////////////////////////////////////
  4267. // ...
  4268. //
  4269. /// Jumps to the target script label.
  4270. ///
  4271. /// goto <label>;
  4272. BUILDIN_FUNC(goto)
  4273. {
  4274. if( !data_islabel(script_getdata(st,2)) )
  4275. {
  4276. ShowError("script:goto: not a label\n");
  4277. script_reportdata(script_getdata(st,2));
  4278. st->state = END;
  4279. return 1;
  4280. }
  4281. st->pos = script_getnum(st,2);
  4282. st->state = GOTO;
  4283. return 0;
  4284. }
  4285. /*==========================================
  4286. * user-defined function call
  4287. *------------------------------------------*/
  4288. BUILDIN_FUNC(callfunc)
  4289. {
  4290. int i, j;
  4291. struct script_retinfo* ri;
  4292. struct script_code* scr;
  4293. const char* str = script_getstr(st,2);
  4294. DBMap **ref = NULL;
  4295. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4296. if( !scr )
  4297. {
  4298. ShowError("script:callfunc: function not found! [%s]\n", str);
  4299. st->state = END;
  4300. return 1;
  4301. }
  4302. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4303. {
  4304. struct script_data* data = push_copy(st->stack,i);
  4305. if( data_isreference(data) && !data->ref )
  4306. {
  4307. const char* name = reference_getname(data);
  4308. if( name[0] == '.' ) {
  4309. if(!ref){
  4310. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4311. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4312. }
  4313. data->ref = ref;
  4314. }
  4315. }
  4316. }
  4317. CREATE(ri, struct script_retinfo, 1);
  4318. ri->script = st->script;// script code
  4319. ri->var_function = st->stack->var_function;// scope variables
  4320. ri->pos = st->pos;// script location
  4321. ri->nargs = j;// argument count
  4322. ri->defsp = st->stack->defsp;// default stack pointer
  4323. push_retinfo(st->stack, ri, ref);
  4324. st->pos = 0;
  4325. st->script = scr;
  4326. st->stack->defsp = st->stack->sp;
  4327. st->state = GOTO;
  4328. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4329. return 0;
  4330. }
  4331. /*==========================================
  4332. * subroutine call
  4333. *------------------------------------------*/
  4334. BUILDIN_FUNC(callsub)
  4335. {
  4336. int i,j;
  4337. struct script_retinfo* ri;
  4338. int pos = script_getnum(st,2);
  4339. DBMap **ref = NULL;
  4340. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4341. {
  4342. ShowError("script:callsub: argument is not a label\n");
  4343. script_reportdata(script_getdata(st,2));
  4344. st->state = END;
  4345. return 1;
  4346. }
  4347. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4348. {
  4349. struct script_data* data = push_copy(st->stack,i);
  4350. if( data_isreference(data) && !data->ref )
  4351. {
  4352. const char* name = reference_getname(data);
  4353. if( name[0] == '.' && name[1] == '@' ) {
  4354. if ( !ref ) {
  4355. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4356. ref[0] = st->stack->var_function;
  4357. }
  4358. data->ref = ref;
  4359. }
  4360. }
  4361. }
  4362. CREATE(ri, struct script_retinfo, 1);
  4363. ri->script = st->script;// script code
  4364. ri->var_function = st->stack->var_function;// scope variables
  4365. ri->pos = st->pos;// script location
  4366. ri->nargs = j;// argument count
  4367. ri->defsp = st->stack->defsp;// default stack pointer
  4368. push_retinfo(st->stack, ri, ref);
  4369. st->pos = pos;
  4370. st->stack->defsp = st->stack->sp;
  4371. st->state = GOTO;
  4372. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4373. return 0;
  4374. }
  4375. /// Retrieves an argument provided to callfunc/callsub.
  4376. /// If the argument doesn't exist
  4377. ///
  4378. /// getarg(<index>{,<default_value>}) -> <value>
  4379. BUILDIN_FUNC(getarg)
  4380. {
  4381. struct script_retinfo* ri;
  4382. int idx;
  4383. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4384. {
  4385. ShowError("script:getarg: no callfunc or callsub!\n");
  4386. st->state = END;
  4387. return 1;
  4388. }
  4389. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4390. idx = script_getnum(st,2);
  4391. if( idx >= 0 && idx < ri->nargs )
  4392. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4393. else if( script_hasdata(st,3) )
  4394. script_pushcopy(st, 3);
  4395. else
  4396. {
  4397. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4398. st->state = END;
  4399. return 1;
  4400. }
  4401. return 0;
  4402. }
  4403. /// Returns from the current function, optionaly returning a value from the functions.
  4404. /// Don't use outside script functions.
  4405. ///
  4406. /// return;
  4407. /// return <value>;
  4408. BUILDIN_FUNC(return)
  4409. {
  4410. if( script_hasdata(st,2) )
  4411. {// return value
  4412. struct script_data* data;
  4413. script_pushcopy(st, 2);
  4414. data = script_getdatatop(st, -1);
  4415. if( data_isreference(data) )
  4416. {
  4417. const char* name = reference_getname(data);
  4418. if( name[0] == '.' && name[1] == '@' )
  4419. {// scope variable
  4420. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4421. get_val(st, data);// current scope, convert to value
  4422. }
  4423. else if( name[0] == '.' && !data->ref )
  4424. {// script variable, link to current script
  4425. data->ref = &st->script->script_vars;
  4426. }
  4427. }
  4428. }
  4429. else
  4430. {// no return value
  4431. script_pushnil(st);
  4432. }
  4433. st->state = RETFUNC;
  4434. return 0;
  4435. }
  4436. /// Returns a random number from 0 to <range>-1.
  4437. /// Or returns a random number from <min> to <max>.
  4438. /// If <min> is greater than <max>, their numbers are switched.
  4439. /// rand(<range>) -> <int>
  4440. /// rand(<min>,<max>) -> <int>
  4441. BUILDIN_FUNC(rand)
  4442. {
  4443. int range;
  4444. int min;
  4445. int max;
  4446. if( script_hasdata(st,3) )
  4447. {// min,max
  4448. min = script_getnum(st,2);
  4449. max = script_getnum(st,3);
  4450. if( max < min )
  4451. swap(min, max);
  4452. range = max - min + 1;
  4453. }
  4454. else
  4455. {// range
  4456. min = 0;
  4457. range = script_getnum(st,2);
  4458. }
  4459. if( range <= 1 )
  4460. script_pushint(st, min);
  4461. else
  4462. script_pushint(st, rnd()%range + min);
  4463. return 0;
  4464. }
  4465. /*==========================================
  4466. * Warp sd to str,x,y or Random or SavePoint/Save
  4467. *------------------------------------------*/
  4468. BUILDIN_FUNC(warp)
  4469. {
  4470. int ret;
  4471. int x,y;
  4472. const char* str;
  4473. TBL_PC* sd;
  4474. sd = script_rid2sd(st);
  4475. if( sd == NULL )
  4476. return 0;
  4477. str = script_getstr(st,2);
  4478. x = script_getnum(st,3);
  4479. y = script_getnum(st,4);
  4480. if(strcmp(str,"Random")==0)
  4481. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4482. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4483. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4484. else
  4485. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4486. if( ret ) {
  4487. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4488. script_reportsrc(st);
  4489. }
  4490. return 0;
  4491. }
  4492. /*==========================================
  4493. * Warp a specified area
  4494. *------------------------------------------*/
  4495. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4496. {
  4497. int x2,y2,x3,y3;
  4498. unsigned int index;
  4499. index = va_arg(ap,unsigned int);
  4500. x2 = va_arg(ap,int);
  4501. y2 = va_arg(ap,int);
  4502. x3 = va_arg(ap,int);
  4503. y3 = va_arg(ap,int);
  4504. if(index == 0)
  4505. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4506. else if(x3 && y3) {
  4507. int max, tx, ty, j = 0;
  4508. // choose a suitable max number of attempts
  4509. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4510. max = 1000;
  4511. // find a suitable map cell
  4512. do {
  4513. tx = rnd()%(x3-x2+1)+x2;
  4514. ty = rnd()%(y3-y2+1)+y2;
  4515. j++;
  4516. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4517. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4518. }
  4519. else
  4520. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4521. return 0;
  4522. }
  4523. BUILDIN_FUNC(areawarp)
  4524. {
  4525. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4526. unsigned int index;
  4527. const char *str;
  4528. const char *mapname;
  4529. mapname = script_getstr(st,2);
  4530. x0 = script_getnum(st,3);
  4531. y0 = script_getnum(st,4);
  4532. x1 = script_getnum(st,5);
  4533. y1 = script_getnum(st,6);
  4534. str = script_getstr(st,7);
  4535. x2 = script_getnum(st,8);
  4536. y2 = script_getnum(st,9);
  4537. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4538. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4539. x3 = 0;
  4540. y3 = 0;
  4541. } else if( x3 && y3 ) {
  4542. // normalize x3/y3 coordinates
  4543. if( x3 < x2 ) swap(x3,x2);
  4544. if( y3 < y2 ) swap(y3,y2);
  4545. }
  4546. }
  4547. if( (m = map_mapname2mapid(mapname)) < 0 )
  4548. return 0;
  4549. if( strcmp(str,"Random") == 0 )
  4550. index = 0;
  4551. else if( !(index=mapindex_name2id(str)) )
  4552. return 0;
  4553. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4554. return 0;
  4555. }
  4556. /*==========================================
  4557. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4558. *------------------------------------------*/
  4559. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4560. {
  4561. int hp, sp;
  4562. hp = va_arg(ap, int);
  4563. sp = va_arg(ap, int);
  4564. pc_percentheal((TBL_PC *)bl,hp,sp);
  4565. return 0;
  4566. }
  4567. BUILDIN_FUNC(areapercentheal)
  4568. {
  4569. int hp,sp,m;
  4570. const char *mapname;
  4571. int x0,y0,x1,y1;
  4572. mapname=script_getstr(st,2);
  4573. x0=script_getnum(st,3);
  4574. y0=script_getnum(st,4);
  4575. x1=script_getnum(st,5);
  4576. y1=script_getnum(st,6);
  4577. hp=script_getnum(st,7);
  4578. sp=script_getnum(st,8);
  4579. if( (m=map_mapname2mapid(mapname))< 0)
  4580. return 0;
  4581. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4582. return 0;
  4583. }
  4584. /*==========================================
  4585. * warpchar [LuzZza]
  4586. * Useful for warp one player from
  4587. * another player npc-session.
  4588. * Using: warpchar "mapname",x,y,Char_ID;
  4589. *------------------------------------------*/
  4590. BUILDIN_FUNC(warpchar)
  4591. {
  4592. int x,y,a;
  4593. const char *str;
  4594. TBL_PC *sd;
  4595. str=script_getstr(st,2);
  4596. x=script_getnum(st,3);
  4597. y=script_getnum(st,4);
  4598. a=script_getnum(st,5);
  4599. sd = map_charid2sd(a);
  4600. if( sd == NULL )
  4601. return 0;
  4602. if(strcmp(str, "Random") == 0)
  4603. pc_randomwarp(sd, CLR_TELEPORT);
  4604. else
  4605. if(strcmp(str, "SavePoint") == 0)
  4606. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4607. else
  4608. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4609. return 0;
  4610. }
  4611. /*==========================================
  4612. * Warpparty - [Fredzilla] [Paradox924X]
  4613. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4614. * If 'from_mapname' is specified, only the party members on that map will be warped
  4615. *------------------------------------------*/
  4616. BUILDIN_FUNC(warpparty)
  4617. {
  4618. TBL_PC *sd = NULL;
  4619. TBL_PC *pl_sd;
  4620. struct party_data* p;
  4621. int type;
  4622. int mapindex;
  4623. int i;
  4624. const char* str = script_getstr(st,2);
  4625. int x = script_getnum(st,3);
  4626. int y = script_getnum(st,4);
  4627. int p_id = script_getnum(st,5);
  4628. const char* str2 = NULL;
  4629. if ( script_hasdata(st,6) )
  4630. str2 = script_getstr(st,6);
  4631. p = party_search(p_id);
  4632. if(!p)
  4633. return 0;
  4634. type = ( strcmp(str,"Random")==0 ) ? 0
  4635. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4636. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4637. : ( strcmp(str,"Leader")==0 ) ? 3
  4638. : 4;
  4639. switch (type)
  4640. {
  4641. case 3:
  4642. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4643. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4644. return 0;
  4645. pl_sd = p->data[i].sd;
  4646. mapindex = pl_sd->mapindex;
  4647. x = pl_sd->bl.x;
  4648. y = pl_sd->bl.y;
  4649. break;
  4650. case 4:
  4651. mapindex = mapindex_name2id(str);
  4652. break;
  4653. case 2:
  4654. //"SavePoint" uses save point of the currently attached player
  4655. if (( sd = script_rid2sd(st) ) == NULL )
  4656. return 0;
  4657. default:
  4658. mapindex = 0;
  4659. break;
  4660. }
  4661. for (i = 0; i < MAX_PARTY; i++)
  4662. {
  4663. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4664. continue;
  4665. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4666. continue;
  4667. if( pc_isdead(pl_sd) )
  4668. continue;
  4669. switch( type )
  4670. {
  4671. case 0: // Random
  4672. if(!map[pl_sd->bl.m].flag.nowarp)
  4673. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4674. break;
  4675. case 1: // SavePointAll
  4676. if(!map[pl_sd->bl.m].flag.noreturn)
  4677. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4678. break;
  4679. case 2: // SavePoint
  4680. if(!map[pl_sd->bl.m].flag.noreturn)
  4681. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4682. break;
  4683. case 3: // Leader
  4684. case 4: // m,x,y
  4685. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4686. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4687. break;
  4688. }
  4689. }
  4690. return 0;
  4691. }
  4692. /*==========================================
  4693. * Warpguild - [Fredzilla]
  4694. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4695. *------------------------------------------*/
  4696. BUILDIN_FUNC(warpguild)
  4697. {
  4698. TBL_PC *sd = NULL;
  4699. TBL_PC *pl_sd;
  4700. struct guild* g;
  4701. struct s_mapiterator* iter;
  4702. int type;
  4703. const char* str = script_getstr(st,2);
  4704. int x = script_getnum(st,3);
  4705. int y = script_getnum(st,4);
  4706. int gid = script_getnum(st,5);
  4707. g = guild_search(gid);
  4708. if( g == NULL )
  4709. return 0;
  4710. type = ( strcmp(str,"Random")==0 ) ? 0
  4711. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4712. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4713. : 3;
  4714. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4715. {// "SavePoint" uses save point of the currently attached player
  4716. return 0;
  4717. }
  4718. iter = mapit_getallusers();
  4719. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4720. {
  4721. if( pl_sd->status.guild_id != gid )
  4722. continue;
  4723. switch( type )
  4724. {
  4725. case 0: // Random
  4726. if(!map[pl_sd->bl.m].flag.nowarp)
  4727. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4728. break;
  4729. case 1: // SavePointAll
  4730. if(!map[pl_sd->bl.m].flag.noreturn)
  4731. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4732. break;
  4733. case 2: // SavePoint
  4734. if(!map[pl_sd->bl.m].flag.noreturn)
  4735. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4736. break;
  4737. case 3: // m,x,y
  4738. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4739. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4740. break;
  4741. }
  4742. }
  4743. mapit_free(iter);
  4744. return 0;
  4745. }
  4746. /*==========================================
  4747. * Force Heal a player (hp and sp)
  4748. *------------------------------------------*/
  4749. BUILDIN_FUNC(heal)
  4750. {
  4751. TBL_PC *sd;
  4752. int hp,sp;
  4753. sd = script_rid2sd(st);
  4754. if (!sd) return 0;
  4755. hp=script_getnum(st,2);
  4756. sp=script_getnum(st,3);
  4757. status_heal(&sd->bl, hp, sp, 1);
  4758. return 0;
  4759. }
  4760. /*==========================================
  4761. * Heal a player by item (get vit bonus etc)
  4762. *------------------------------------------*/
  4763. BUILDIN_FUNC(itemheal)
  4764. {
  4765. TBL_PC *sd;
  4766. int hp,sp;
  4767. hp=script_getnum(st,2);
  4768. sp=script_getnum(st,3);
  4769. if(potion_flag==1) {
  4770. potion_hp = hp;
  4771. potion_sp = sp;
  4772. return 0;
  4773. }
  4774. sd = script_rid2sd(st);
  4775. if (!sd) return 0;
  4776. pc_itemheal(sd,sd->itemid,hp,sp);
  4777. return 0;
  4778. }
  4779. /*==========================================
  4780. *
  4781. *------------------------------------------*/
  4782. BUILDIN_FUNC(percentheal)
  4783. {
  4784. int hp,sp;
  4785. TBL_PC* sd;
  4786. hp=script_getnum(st,2);
  4787. sp=script_getnum(st,3);
  4788. if(potion_flag==1) {
  4789. potion_per_hp = hp;
  4790. potion_per_sp = sp;
  4791. return 0;
  4792. }
  4793. sd = script_rid2sd(st);
  4794. if( sd == NULL )
  4795. return 0;
  4796. #ifdef RENEWAL
  4797. if( sd->sc.data[SC_EXTREMITYFIST2] )
  4798. sp = 0;
  4799. #endif
  4800. pc_percentheal(sd,hp,sp);
  4801. return 0;
  4802. }
  4803. /*==========================================
  4804. *
  4805. *------------------------------------------*/
  4806. BUILDIN_FUNC(jobchange)
  4807. {
  4808. int job, upper=-1;
  4809. job=script_getnum(st,2);
  4810. if( script_hasdata(st,3) )
  4811. upper=script_getnum(st,3);
  4812. if (pcdb_checkid(job))
  4813. {
  4814. TBL_PC* sd;
  4815. sd = script_rid2sd(st);
  4816. if( sd == NULL )
  4817. return 0;
  4818. pc_jobchange(sd, job, upper);
  4819. }
  4820. return 0;
  4821. }
  4822. /*==========================================
  4823. *
  4824. *------------------------------------------*/
  4825. BUILDIN_FUNC(jobname)
  4826. {
  4827. int class_=script_getnum(st,2);
  4828. script_pushconststr(st, (char*)job_name(class_));
  4829. return 0;
  4830. }
  4831. /// Get input from the player.
  4832. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4833. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4834. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4835. /// shorter than 'min' and 0 otherwise.
  4836. ///
  4837. /// input(<var>{,<min>{,<max>}}) -> <int>
  4838. BUILDIN_FUNC(input)
  4839. {
  4840. TBL_PC* sd;
  4841. struct script_data* data;
  4842. int uid;
  4843. const char* name;
  4844. int min;
  4845. int max;
  4846. sd = script_rid2sd(st);
  4847. if( sd == NULL )
  4848. return 0;
  4849. data = script_getdata(st,2);
  4850. if( !data_isreference(data) ){
  4851. ShowError("script:input: not a variable\n");
  4852. script_reportdata(data);
  4853. st->state = END;
  4854. return 1;
  4855. }
  4856. uid = reference_getuid(data);
  4857. name = reference_getname(data);
  4858. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4859. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4860. #ifdef SECURE_NPCTIMEOUT
  4861. sd->npc_idle_type = NPCT_WAIT;
  4862. #endif
  4863. if( !sd->state.menu_or_input )
  4864. { // first invocation, display npc input box
  4865. sd->state.menu_or_input = 1;
  4866. st->state = RERUNLINE;
  4867. if( is_string_variable(name) )
  4868. clif_scriptinputstr(sd,st->oid);
  4869. else
  4870. clif_scriptinput(sd,st->oid);
  4871. }
  4872. else
  4873. { // take received text/value and store it in the designated variable
  4874. sd->state.menu_or_input = 0;
  4875. if( is_string_variable(name) )
  4876. {
  4877. int len = (int)strlen(sd->npc_str);
  4878. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4879. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4880. }
  4881. else
  4882. {
  4883. int amount = sd->npc_amount;
  4884. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4885. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4886. }
  4887. st->state = RUN;
  4888. }
  4889. return 0;
  4890. }
  4891. // declare the copyarray method here for future reference
  4892. BUILDIN_FUNC(copyarray);
  4893. /// Sets the value of a variable.
  4894. /// The value is converted to the type of the variable.
  4895. ///
  4896. /// set(<variable>,<value>) -> <variable>
  4897. BUILDIN_FUNC(set)
  4898. {
  4899. TBL_PC* sd = NULL;
  4900. struct script_data* data;
  4901. //struct script_data* datavalue;
  4902. int num;
  4903. const char* name;
  4904. char prefix;
  4905. data = script_getdata(st,2);
  4906. //datavalue = script_getdata(st,3);
  4907. if( !data_isreference(data) )
  4908. {
  4909. ShowError("script:set: not a variable\n");
  4910. script_reportdata(script_getdata(st,2));
  4911. st->state = END;
  4912. return 1;
  4913. }
  4914. num = reference_getuid(data);
  4915. name = reference_getname(data);
  4916. prefix = *name;
  4917. if( not_server_variable(prefix) )
  4918. {
  4919. sd = script_rid2sd(st);
  4920. if( sd == NULL )
  4921. {
  4922. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4923. return 0;
  4924. }
  4925. }
  4926. #if 0
  4927. if( data_isreference(datavalue) )
  4928. {// the value being referenced is a variable
  4929. const char* namevalue = reference_getname(datavalue);
  4930. if( !not_array_variable(*namevalue) )
  4931. {// array variable being copied into another array variable
  4932. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  4933. {// player must be attached in order to copy a player variable
  4934. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  4935. return 0;
  4936. }
  4937. if( is_string_variable(namevalue) != is_string_variable(name) )
  4938. {// non-matching array value types
  4939. ShowWarning("script:set: two array variables do not match in type.\n");
  4940. return 0;
  4941. }
  4942. // push the maximum number of array values to the stack
  4943. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  4944. // call the copy array method directly
  4945. return buildin_copyarray(st);
  4946. }
  4947. }
  4948. #endif
  4949. if( is_string_variable(name) )
  4950. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4951. else
  4952. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4953. // return a copy of the variable reference
  4954. script_pushcopy(st,2);
  4955. return 0;
  4956. }
  4957. /////////////////////////////////////////////////////////////////////
  4958. /// Array variables
  4959. ///
  4960. /// Returns the size of the specified array
  4961. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  4962. {
  4963. int32 ret = idx;
  4964. if( isstring )
  4965. {
  4966. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4967. {
  4968. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4969. if( str && *str )
  4970. ret = idx + 1;
  4971. script_removetop(st, -1, 0);
  4972. }
  4973. }
  4974. else
  4975. {
  4976. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4977. {
  4978. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  4979. if( num )
  4980. ret = idx + 1;
  4981. script_removetop(st, -1, 0);
  4982. }
  4983. }
  4984. return ret;
  4985. }
  4986. /// Sets values of an array, from the starting index.
  4987. /// ex: setarray arr[1],1,2,3;
  4988. ///
  4989. /// setarray <array variable>,<value1>{,<value2>...};
  4990. BUILDIN_FUNC(setarray)
  4991. {
  4992. struct script_data* data;
  4993. const char* name;
  4994. int32 start;
  4995. int32 end;
  4996. int32 id;
  4997. int32 i;
  4998. TBL_PC* sd = NULL;
  4999. data = script_getdata(st, 2);
  5000. if( !data_isreference(data) )
  5001. {
  5002. ShowError("script:setarray: not a variable\n");
  5003. script_reportdata(data);
  5004. st->state = END;
  5005. return 1;// not a variable
  5006. }
  5007. id = reference_getid(data);
  5008. start = reference_getindex(data);
  5009. name = reference_getname(data);
  5010. if( not_array_variable(*name) )
  5011. {
  5012. ShowError("script:setarray: illegal scope\n");
  5013. script_reportdata(data);
  5014. st->state = END;
  5015. return 1;// not supported
  5016. }
  5017. if( not_server_variable(*name) )
  5018. {
  5019. sd = script_rid2sd(st);
  5020. if( sd == NULL )
  5021. return 0;// no player attached
  5022. }
  5023. end = start + script_lastdata(st) - 2;
  5024. if( end > SCRIPT_MAX_ARRAYSIZE )
  5025. end = SCRIPT_MAX_ARRAYSIZE;
  5026. if( is_string_variable(name) )
  5027. {// string array
  5028. for( i = 3; start < end; ++start, ++i )
  5029. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5030. }
  5031. else
  5032. {// int array
  5033. for( i = 3; start < end; ++start, ++i )
  5034. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5035. }
  5036. return 0;
  5037. }
  5038. /// Sets count values of an array, from the starting index.
  5039. /// ex: cleararray arr[0],0,1;
  5040. ///
  5041. /// cleararray <array variable>,<value>,<count>;
  5042. BUILDIN_FUNC(cleararray)
  5043. {
  5044. struct script_data* data;
  5045. const char* name;
  5046. int32 start;
  5047. int32 end;
  5048. int32 id;
  5049. void* v;
  5050. TBL_PC* sd = NULL;
  5051. data = script_getdata(st, 2);
  5052. if( !data_isreference(data) )
  5053. {
  5054. ShowError("script:cleararray: not a variable\n");
  5055. script_reportdata(data);
  5056. st->state = END;
  5057. return 1;// not a variable
  5058. }
  5059. id = reference_getid(data);
  5060. start = reference_getindex(data);
  5061. name = reference_getname(data);
  5062. if( not_array_variable(*name) )
  5063. {
  5064. ShowError("script:cleararray: illegal scope\n");
  5065. script_reportdata(data);
  5066. st->state = END;
  5067. return 1;// not supported
  5068. }
  5069. if( not_server_variable(*name) )
  5070. {
  5071. sd = script_rid2sd(st);
  5072. if( sd == NULL )
  5073. return 0;// no player attached
  5074. }
  5075. if( is_string_variable(name) )
  5076. v = (void*)script_getstr(st, 3);
  5077. else
  5078. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5079. end = start + script_getnum(st, 4);
  5080. if( end > SCRIPT_MAX_ARRAYSIZE )
  5081. end = SCRIPT_MAX_ARRAYSIZE;
  5082. for( ; start < end; ++start )
  5083. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5084. return 0;
  5085. }
  5086. /// Copies data from one array to another.
  5087. /// ex: copyarray arr[0],arr[2],2;
  5088. ///
  5089. /// copyarray <destination array variable>,<source array variable>,<count>;
  5090. BUILDIN_FUNC(copyarray)
  5091. {
  5092. struct script_data* data1;
  5093. struct script_data* data2;
  5094. const char* name1;
  5095. const char* name2;
  5096. int32 idx1;
  5097. int32 idx2;
  5098. int32 id1;
  5099. int32 id2;
  5100. void* v;
  5101. int32 i;
  5102. int32 count;
  5103. TBL_PC* sd = NULL;
  5104. data1 = script_getdata(st, 2);
  5105. data2 = script_getdata(st, 3);
  5106. if( !data_isreference(data1) || !data_isreference(data2) )
  5107. {
  5108. ShowError("script:copyarray: not a variable\n");
  5109. script_reportdata(data1);
  5110. script_reportdata(data2);
  5111. st->state = END;
  5112. return 1;// not a variable
  5113. }
  5114. id1 = reference_getid(data1);
  5115. id2 = reference_getid(data2);
  5116. idx1 = reference_getindex(data1);
  5117. idx2 = reference_getindex(data2);
  5118. name1 = reference_getname(data1);
  5119. name2 = reference_getname(data2);
  5120. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5121. {
  5122. ShowError("script:copyarray: illegal scope\n");
  5123. script_reportdata(data1);
  5124. script_reportdata(data2);
  5125. st->state = END;
  5126. return 1;// not supported
  5127. }
  5128. if( is_string_variable(name1) != is_string_variable(name2) )
  5129. {
  5130. ShowError("script:copyarray: type mismatch\n");
  5131. script_reportdata(data1);
  5132. script_reportdata(data2);
  5133. st->state = END;
  5134. return 1;// data type mismatch
  5135. }
  5136. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5137. {
  5138. sd = script_rid2sd(st);
  5139. if( sd == NULL )
  5140. return 0;// no player attached
  5141. }
  5142. count = script_getnum(st, 4);
  5143. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5144. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5145. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5146. return 0;// nothing to copy
  5147. if( id1 == id2 && idx1 > idx2 )
  5148. {// destination might be overlapping the source - copy in reverse order
  5149. for( i = count - 1; i >= 0; --i )
  5150. {
  5151. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5152. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5153. script_removetop(st, -1, 0);
  5154. }
  5155. }
  5156. else
  5157. {// normal copy
  5158. for( i = 0; i < count; ++i )
  5159. {
  5160. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5161. {
  5162. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5163. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5164. script_removetop(st, -1, 0);
  5165. }
  5166. else// out of range - assume ""/0
  5167. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5168. }
  5169. }
  5170. return 0;
  5171. }
  5172. /// Returns the size of the array.
  5173. /// Assumes that everything before the starting index exists.
  5174. /// ex: getarraysize(arr[3])
  5175. ///
  5176. /// getarraysize(<array variable>) -> <int>
  5177. BUILDIN_FUNC(getarraysize)
  5178. {
  5179. struct script_data* data;
  5180. const char* name;
  5181. data = script_getdata(st, 2);
  5182. if( !data_isreference(data) )
  5183. {
  5184. ShowError("script:getarraysize: not a variable\n");
  5185. script_reportdata(data);
  5186. script_pushnil(st);
  5187. st->state = END;
  5188. return 1;// not a variable
  5189. }
  5190. name = reference_getname(data);
  5191. if( not_array_variable(*name) )
  5192. {
  5193. ShowError("script:getarraysize: illegal scope\n");
  5194. script_reportdata(data);
  5195. script_pushnil(st);
  5196. st->state = END;
  5197. return 1;// not supported
  5198. }
  5199. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5200. return 0;
  5201. }
  5202. /// Deletes count or all the elements in an array, from the starting index.
  5203. /// ex: deletearray arr[4],2;
  5204. ///
  5205. /// deletearray <array variable>;
  5206. /// deletearray <array variable>,<count>;
  5207. BUILDIN_FUNC(deletearray)
  5208. {
  5209. struct script_data* data;
  5210. const char* name;
  5211. int start;
  5212. int end;
  5213. int id;
  5214. TBL_PC *sd = NULL;
  5215. data = script_getdata(st, 2);
  5216. if( !data_isreference(data) )
  5217. {
  5218. ShowError("script:deletearray: not a variable\n");
  5219. script_reportdata(data);
  5220. st->state = END;
  5221. return 1;// not a variable
  5222. }
  5223. id = reference_getid(data);
  5224. start = reference_getindex(data);
  5225. name = reference_getname(data);
  5226. if( not_array_variable(*name) )
  5227. {
  5228. ShowError("script:deletearray: illegal scope\n");
  5229. script_reportdata(data);
  5230. st->state = END;
  5231. return 1;// not supported
  5232. }
  5233. if( not_server_variable(*name) )
  5234. {
  5235. sd = script_rid2sd(st);
  5236. if( sd == NULL )
  5237. return 0;// no player attached
  5238. }
  5239. end = SCRIPT_MAX_ARRAYSIZE;
  5240. if( start >= end )
  5241. return 0;// nothing to free
  5242. if( script_hasdata(st,3) )
  5243. {
  5244. int count = script_getnum(st, 3);
  5245. if( count > end - start )
  5246. count = end - start;
  5247. if( count <= 0 )
  5248. return 0;// nothing to free
  5249. // move rest of the elements backward
  5250. for( ; start + count < end; ++start )
  5251. {
  5252. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5253. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5254. script_removetop(st, -1, 0);
  5255. }
  5256. }
  5257. // clear the rest of the array
  5258. if( is_string_variable(name) )
  5259. {
  5260. for( ; start < end; ++start )
  5261. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5262. }
  5263. else
  5264. {
  5265. for( ; start < end; ++start )
  5266. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5267. }
  5268. return 0;
  5269. }
  5270. /// Returns a reference to the target index of the array variable.
  5271. /// Equivalent to var[index].
  5272. ///
  5273. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5274. BUILDIN_FUNC(getelementofarray)
  5275. {
  5276. struct script_data* data;
  5277. const char* name;
  5278. int32 id;
  5279. int i;
  5280. data = script_getdata(st, 2);
  5281. if( !data_isreference(data) )
  5282. {
  5283. ShowError("script:getelementofarray: not a variable\n");
  5284. script_reportdata(data);
  5285. script_pushnil(st);
  5286. st->state = END;
  5287. return 1;// not a variable
  5288. }
  5289. id = reference_getid(data);
  5290. name = reference_getname(data);
  5291. if( not_array_variable(*name) )
  5292. {
  5293. ShowError("script:getelementofarray: illegal scope\n");
  5294. script_reportdata(data);
  5295. script_pushnil(st);
  5296. st->state = END;
  5297. return 1;// not supported
  5298. }
  5299. i = script_getnum(st, 3);
  5300. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5301. {
  5302. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5303. script_reportdata(data);
  5304. script_pushnil(st);
  5305. st->state = END;
  5306. return 1;// out of range
  5307. }
  5308. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5309. return 0;
  5310. }
  5311. /////////////////////////////////////////////////////////////////////
  5312. /// ...
  5313. ///
  5314. /*==========================================
  5315. *
  5316. *------------------------------------------*/
  5317. BUILDIN_FUNC(setlook)
  5318. {
  5319. int type,val;
  5320. TBL_PC* sd;
  5321. type=script_getnum(st,2);
  5322. val=script_getnum(st,3);
  5323. sd = script_rid2sd(st);
  5324. if( sd == NULL )
  5325. return 0;
  5326. pc_changelook(sd,type,val);
  5327. return 0;
  5328. }
  5329. BUILDIN_FUNC(changelook)
  5330. { // As setlook but only client side
  5331. int type,val;
  5332. TBL_PC* sd;
  5333. type=script_getnum(st,2);
  5334. val=script_getnum(st,3);
  5335. sd = script_rid2sd(st);
  5336. if( sd == NULL )
  5337. return 0;
  5338. clif_changelook(&sd->bl,type,val);
  5339. return 0;
  5340. }
  5341. /*==========================================
  5342. *
  5343. *------------------------------------------*/
  5344. BUILDIN_FUNC(cutin)
  5345. {
  5346. TBL_PC* sd;
  5347. sd = script_rid2sd(st);
  5348. if( sd == NULL )
  5349. return 0;
  5350. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5351. return 0;
  5352. }
  5353. /*==========================================
  5354. *
  5355. *------------------------------------------*/
  5356. BUILDIN_FUNC(viewpoint)
  5357. {
  5358. int type,x,y,id,color;
  5359. TBL_PC* sd;
  5360. type=script_getnum(st,2);
  5361. x=script_getnum(st,3);
  5362. y=script_getnum(st,4);
  5363. id=script_getnum(st,5);
  5364. color=script_getnum(st,6);
  5365. sd = script_rid2sd(st);
  5366. if( sd == NULL )
  5367. return 0;
  5368. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5369. return 0;
  5370. }
  5371. /*==========================================
  5372. * countitem(nameID)
  5373. * returns number of items in inventory
  5374. *==========================================
  5375. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5376. * returns number of items that meet the conditions
  5377. *------------------------------------------*/
  5378. BUILDIN_FUNC(countitem)
  5379. {
  5380. int i, count = 0;
  5381. struct item_data* id = NULL;
  5382. struct script_data* data;
  5383. TBL_PC* sd = script_rid2sd(st);
  5384. if (!sd) {
  5385. script_pushint(st,0);
  5386. return 0;
  5387. }
  5388. data = script_getdata(st,2);
  5389. get_val(st, data); // convert into value in case of a variable
  5390. if( data_isstring(data) ) // item name
  5391. id = itemdb_searchname(conv_str(st, data));
  5392. else // item id
  5393. id = itemdb_exists(conv_num(st, data));
  5394. if( id == NULL ) {
  5395. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5396. script_pushint(st,0);
  5397. return 1;
  5398. }
  5399. if(script_lastdata(st) == 2) { // For countitem() function
  5400. int nameid = id->nameid;
  5401. for(i = 0; i < MAX_INVENTORY; i++)
  5402. if(sd->status.inventory[i].nameid == nameid)
  5403. count += sd->status.inventory[i].amount;
  5404. } else { // For countitem2() function
  5405. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5406. nameid = id->nameid;
  5407. iden = script_getnum(st,3);
  5408. ref = script_getnum(st,4);
  5409. attr = script_getnum(st,5);
  5410. c1 = (short)script_getnum(st,6);
  5411. c2 = (short)script_getnum(st,7);
  5412. c3 = (short)script_getnum(st,8);
  5413. c4 = (short)script_getnum(st,9);
  5414. for(i = 0; i < MAX_INVENTORY; i++)
  5415. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5416. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5417. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5418. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5419. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5420. sd->status.inventory[i].card[3] == c4 )
  5421. count += sd->status.inventory[i].amount;
  5422. }
  5423. script_pushint(st,count);
  5424. return 0;
  5425. }
  5426. int checkweight_sub(TBL_PC *sd,int nbargs,int32 *eitemid,int32 *eamount)
  5427. {
  5428. struct item_data* id = NULL;
  5429. int nameid,amount;
  5430. uint16 amount2=0,slots,weight=0,i;
  5431. slots = pc_inventoryblank(sd); //nb of empty slot
  5432. for(i=0; i<nbargs; i++) {
  5433. if(!eitemid[i])
  5434. continue;
  5435. id = itemdb_exists(eitemid[i]);
  5436. if( id == NULL ) {
  5437. ShowError("checkweight_sub: Invalid item '%d'.\n", eitemid[i]);
  5438. return 0;
  5439. }
  5440. nameid = id->nameid;
  5441. amount = eamount[i];
  5442. if( amount < 1 ) {
  5443. ShowError("checkweight_sub: Invalid amount '%d'.\n", eamount[i]);
  5444. return 0;
  5445. }
  5446. weight += (id->weight)*amount; //total weight for all chk
  5447. if( weight + sd->weight > sd->max_weight ) // too heavy
  5448. return 0;
  5449. switch( pc_checkadditem(sd, nameid, amount) ) {
  5450. case CHKADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount
  5451. break;
  5452. case CHKADDITEM_NEW:
  5453. if( itemdb_isstackable(nameid) )
  5454. amount2++; // stackable
  5455. else
  5456. amount2 += amount; // non-stackable
  5457. if( slots < amount2)
  5458. return 0;
  5459. break;
  5460. case CHKADDITEM_OVERAMOUNT:
  5461. return 0;
  5462. }
  5463. }
  5464. return 1;
  5465. }
  5466. /*==========================================
  5467. * Check if item with this amount can fit in inventory
  5468. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5469. * Return
  5470. * 0 : fail
  5471. * 1 : success (npc side only)
  5472. *------------------------------------------*/
  5473. BUILDIN_FUNC(checkweight)
  5474. {
  5475. struct map_session_data* sd;
  5476. struct script_data* data;
  5477. struct item_data* id = NULL;
  5478. int32 nameid[SCRIPT_MAX_ARRAYSIZE], amount[SCRIPT_MAX_ARRAYSIZE];
  5479. uint16 nbargs,i,j=0;
  5480. if( ( sd = script_rid2sd(st) ) == NULL )
  5481. return 0;
  5482. nbargs = script_lastdata(st)+1;
  5483. if(nbargs%2) {
  5484. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5485. script_pushint(st,0);
  5486. return 1;
  5487. }
  5488. for(i=2; i<nbargs; i=i+2) {
  5489. data = script_getdata(st,i);
  5490. get_val(st, data); // convert into value in case of a variable
  5491. if( data_isstring(data) ) // item name
  5492. id = itemdb_searchname(conv_str(st, data));
  5493. else // item id
  5494. id = itemdb_exists(conv_num(st, data));
  5495. if( id == NULL ) {
  5496. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5497. script_pushint(st,0);
  5498. return 1;
  5499. }
  5500. nameid[j] = id->nameid;
  5501. amount[j] = script_getnum(st,i+1);
  5502. j++;
  5503. }
  5504. script_pushint(st,checkweight_sub(sd,(nbargs-2)/2,nameid,amount));
  5505. return 0;
  5506. }
  5507. BUILDIN_FUNC(checkweight2){
  5508. //variable sub checkweight
  5509. int32 nameid[SCRIPT_MAX_ARRAYSIZE], amount[SCRIPT_MAX_ARRAYSIZE], i;
  5510. //variable for array parsing
  5511. struct script_data* data_it;
  5512. struct script_data* data_nb;
  5513. const char* name_it;
  5514. const char* name_nb;
  5515. int32 id_it, id_nb;
  5516. int32 idx_it, idx_nb;
  5517. int nb_it, nb_nb; //array size
  5518. TBL_PC *sd = script_rid2sd(st);
  5519. nullpo_retr(1,sd);
  5520. data_it = script_getdata(st, 2);
  5521. data_nb = script_getdata(st, 3);
  5522. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5523. ShowError("buildin_checkweight2: parameter not a variable\n");
  5524. script_pushint(st,0);
  5525. return 1;// not a variable
  5526. }
  5527. id_it = reference_getid(data_it);
  5528. id_nb = reference_getid(data_nb);
  5529. idx_it = reference_getindex(data_it);
  5530. idx_nb = reference_getindex(data_nb);
  5531. name_it = reference_getname(data_it);
  5532. name_nb = reference_getname(data_nb);
  5533. if( not_array_variable(*name_it) || not_array_variable(*name_nb)) {
  5534. ShowError("buildin_checkweight2: illegal scope\n");
  5535. script_pushint(st,0);
  5536. return 1;// not supported
  5537. }
  5538. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5539. ShowError("buildin_checkweight2: illegal type, need int\n");
  5540. script_pushint(st,0);
  5541. return 1;// not supported
  5542. }
  5543. nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it));
  5544. nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb));
  5545. if(nb_it != nb_nb) {
  5546. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5547. script_pushint(st,0);
  5548. return 1;
  5549. }
  5550. for(i=0; i<nb_it; i++) {
  5551. nameid[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5552. script_removetop(st, -1, 0);
  5553. amount[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5554. script_removetop(st, -1, 0);
  5555. } //end loop DO NOT break it prematurly we need to depop all stack
  5556. script_pushint(st,checkweight_sub(sd,nb_it,nameid,amount)); //push result of sub to script
  5557. return 0;
  5558. }
  5559. /*==========================================
  5560. * getitem <item id>,<amount>{,<account ID>};
  5561. * getitem "<item name>",<amount>{,<account ID>};
  5562. *
  5563. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5564. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5565. * Type:
  5566. * 1 - Account Bound
  5567. * 2 - Guild Bound
  5568. * 3 - Party Bound
  5569. * 4 - Character Bound
  5570. *------------------------------------------*/
  5571. BUILDIN_FUNC(getitem)
  5572. {
  5573. int nameid,amount,get_count,i,flag = 0;
  5574. struct item it;
  5575. TBL_PC *sd;
  5576. struct script_data *data;
  5577. data=script_getdata(st,2);
  5578. get_val(st,data);
  5579. if( data_isstring(data) ) {// "<item name>"
  5580. const char *name=conv_str(st,data);
  5581. struct item_data *item_data = itemdb_searchname(name);
  5582. if( item_data == NULL ){
  5583. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5584. return 1; //No item created.
  5585. }
  5586. nameid=item_data->nameid;
  5587. } else if( data_isint(data) ) {// <item id>
  5588. nameid=conv_num(st,data);
  5589. //Violet Box, Blue Box, etc - random item pick
  5590. if( nameid < 0 ) {
  5591. nameid = -nameid;
  5592. flag = 1;
  5593. }
  5594. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5595. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5596. return 1; //No item created.
  5597. }
  5598. } else {
  5599. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5600. return 1;
  5601. }
  5602. // <amount>
  5603. if( (amount=script_getnum(st,3)) <= 0)
  5604. return 0; //return if amount <=0, skip the useles iteration
  5605. memset(&it,0,sizeof(it));
  5606. it.nameid=nameid;
  5607. if(!flag)
  5608. it.identify=1;
  5609. else
  5610. it.identify=itemdb_isidentified(nameid);
  5611. if( !strcmp(script_getfuncname(st),"getitembound") ) {
  5612. char bound = script_getnum(st,4);
  5613. if( bound < 1 || bound > 4) { //Not a correct bound type
  5614. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5615. return 1;
  5616. }
  5617. it.bound = bound;
  5618. if( script_hasdata(st,5) )
  5619. sd=map_id2sd(script_getnum(st,5));
  5620. else
  5621. sd=script_rid2sd(st); // Attached player
  5622. } else if( script_hasdata(st,4) )
  5623. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5624. else
  5625. sd=script_rid2sd(st); // Attached player
  5626. if( sd == NULL ) // no target
  5627. return 0;
  5628. //Check if it's stackable.
  5629. if (!itemdb_isstackable(nameid))
  5630. get_count = 1;
  5631. else
  5632. get_count = amount;
  5633. for (i = 0; i < amount; i += get_count)
  5634. {
  5635. // if not pet egg
  5636. if (!pet_create_egg(sd, nameid))
  5637. {
  5638. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5639. {
  5640. clif_additem(sd, 0, 0, flag);
  5641. if( pc_candrop(sd,&it) )
  5642. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5643. }
  5644. }
  5645. }
  5646. return 0;
  5647. }
  5648. /*==========================================
  5649. *
  5650. *------------------------------------------*/
  5651. BUILDIN_FUNC(getitem2)
  5652. {
  5653. int nameid,amount,get_count,i,flag = 0;
  5654. int iden,ref,attr,c1,c2,c3,c4;
  5655. char bound=0;
  5656. struct item_data *item_data;
  5657. struct item item_tmp;
  5658. TBL_PC *sd;
  5659. struct script_data *data;
  5660. if( !strcmp(script_getfuncname(st),"getitembound2") ) {
  5661. bound = script_getnum(st,11);
  5662. if( bound < 1 || bound > 3) { //Not a correct bound type
  5663. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  5664. return 1;
  5665. }
  5666. if( script_hasdata(st,12) )
  5667. sd=map_id2sd(script_getnum(st,12));
  5668. else
  5669. sd=script_rid2sd(st); // Attached player
  5670. } else if( script_hasdata(st,11) )
  5671. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5672. else
  5673. sd=script_rid2sd(st); // Attached player
  5674. if( sd == NULL ) // no target
  5675. return 0;
  5676. data=script_getdata(st,2);
  5677. get_val(st,data);
  5678. if( data_isstring(data) ) {
  5679. const char *name=conv_str(st,data);
  5680. struct item_data *item_data = itemdb_searchname(name);
  5681. if( item_data )
  5682. nameid=item_data->nameid;
  5683. else
  5684. nameid=UNKNOWN_ITEM_ID;
  5685. } else
  5686. nameid=conv_num(st,data);
  5687. amount=script_getnum(st,3);
  5688. iden=script_getnum(st,4);
  5689. ref=script_getnum(st,5);
  5690. attr=script_getnum(st,6);
  5691. c1=(short)script_getnum(st,7);
  5692. c2=(short)script_getnum(st,8);
  5693. c3=(short)script_getnum(st,9);
  5694. c4=(short)script_getnum(st,10);
  5695. if(nameid<0) { // Invalide nameid
  5696. nameid = -nameid;
  5697. flag = 1;
  5698. }
  5699. if(nameid > 0) {
  5700. memset(&item_tmp,0,sizeof(item_tmp));
  5701. item_data=itemdb_exists(nameid);
  5702. if (item_data == NULL)
  5703. return -1;
  5704. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5705. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5706. }
  5707. else if(item_data->type==IT_PETEGG) {
  5708. iden = 1;
  5709. ref = 0;
  5710. }
  5711. else {
  5712. iden = 1;
  5713. ref = attr = 0;
  5714. }
  5715. item_tmp.nameid=nameid;
  5716. if(!flag)
  5717. item_tmp.identify=iden;
  5718. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5719. item_tmp.identify=0;
  5720. item_tmp.refine=ref;
  5721. item_tmp.attribute=attr;
  5722. item_tmp.card[0]=(short)c1;
  5723. item_tmp.card[1]=(short)c2;
  5724. item_tmp.card[2]=(short)c3;
  5725. item_tmp.card[3]=(short)c4;
  5726. item_tmp.bound=bound;
  5727. //Check if it's stackable.
  5728. if (!itemdb_isstackable(nameid))
  5729. get_count = 1;
  5730. else
  5731. get_count = amount;
  5732. for (i = 0; i < amount; i += get_count)
  5733. {
  5734. // if not pet egg
  5735. if (!pet_create_egg(sd, nameid))
  5736. {
  5737. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5738. {
  5739. clif_additem(sd, 0, 0, flag);
  5740. if( pc_candrop(sd,&item_tmp) )
  5741. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5742. }
  5743. }
  5744. }
  5745. }
  5746. return 0;
  5747. }
  5748. /*==========================================
  5749. * rentitem <item id>,<seconds>
  5750. * rentitem "<item name>",<seconds>
  5751. *------------------------------------------*/
  5752. BUILDIN_FUNC(rentitem)
  5753. {
  5754. struct map_session_data *sd;
  5755. struct script_data *data;
  5756. struct item it;
  5757. int seconds;
  5758. int nameid = 0, flag;
  5759. data = script_getdata(st,2);
  5760. get_val(st,data);
  5761. if( (sd = script_rid2sd(st)) == NULL )
  5762. return 0;
  5763. if( data_isstring(data) )
  5764. {
  5765. const char *name = conv_str(st,data);
  5766. struct item_data *itd = itemdb_searchname(name);
  5767. if( itd == NULL )
  5768. {
  5769. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5770. return 1;
  5771. }
  5772. nameid = itd->nameid;
  5773. }
  5774. else if( data_isint(data) )
  5775. {
  5776. nameid = conv_num(st,data);
  5777. if( nameid <= 0 || !itemdb_exists(nameid) )
  5778. {
  5779. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5780. return 1;
  5781. }
  5782. }
  5783. else
  5784. {
  5785. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5786. return 1;
  5787. }
  5788. seconds = script_getnum(st,3);
  5789. memset(&it, 0, sizeof(it));
  5790. it.nameid = nameid;
  5791. it.identify = 1;
  5792. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5793. it.bound = 0;
  5794. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5795. {
  5796. clif_additem(sd, 0, 0, flag);
  5797. return 1;
  5798. }
  5799. return 0;
  5800. }
  5801. /*==========================================
  5802. * gets an item with someone's name inscribed [Skotlex]
  5803. * getinscribeditem item_num, character_name
  5804. * Returned Qty is always 1, only works on equip-able
  5805. * equipment
  5806. *------------------------------------------*/
  5807. BUILDIN_FUNC(getnameditem)
  5808. {
  5809. int nameid;
  5810. struct item item_tmp;
  5811. TBL_PC *sd, *tsd;
  5812. struct script_data *data;
  5813. sd = script_rid2sd(st);
  5814. if (sd == NULL)
  5815. { //Player not attached!
  5816. script_pushint(st,0);
  5817. return 0;
  5818. }
  5819. data=script_getdata(st,2);
  5820. get_val(st,data);
  5821. if( data_isstring(data) ){
  5822. const char *name=conv_str(st,data);
  5823. struct item_data *item_data = itemdb_searchname(name);
  5824. if( item_data == NULL)
  5825. { //Failed
  5826. script_pushint(st,0);
  5827. return 0;
  5828. }
  5829. nameid = item_data->nameid;
  5830. }else
  5831. nameid = conv_num(st,data);
  5832. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5833. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5834. script_pushint(st,0);
  5835. return 0;
  5836. }
  5837. data=script_getdata(st,3);
  5838. get_val(st,data);
  5839. if( data_isstring(data) ) //Char Name
  5840. tsd=map_nick2sd(conv_str(st,data));
  5841. else //Char Id was given
  5842. tsd=map_charid2sd(conv_num(st,data));
  5843. if( tsd == NULL )
  5844. { //Failed
  5845. script_pushint(st,0);
  5846. return 0;
  5847. }
  5848. memset(&item_tmp,0,sizeof(item_tmp));
  5849. item_tmp.nameid=nameid;
  5850. item_tmp.amount=1;
  5851. item_tmp.identify=1;
  5852. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5853. item_tmp.card[2]=tsd->status.char_id;
  5854. item_tmp.card[3]=tsd->status.char_id >> 16;
  5855. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5856. script_pushint(st,0);
  5857. return 0; //Failed to add item, we will not drop if they don't fit
  5858. }
  5859. script_pushint(st,1);
  5860. return 0;
  5861. }
  5862. /*==========================================
  5863. * gets a random item ID from an item group [Skotlex]
  5864. * groupranditem group_num
  5865. *------------------------------------------*/
  5866. BUILDIN_FUNC(grouprandomitem)
  5867. {
  5868. int group;
  5869. group = script_getnum(st,2);
  5870. script_pushint(st,itemdb_searchrandomid(group));
  5871. return 0;
  5872. }
  5873. /*==========================================
  5874. *
  5875. *------------------------------------------*/
  5876. BUILDIN_FUNC(makeitem)
  5877. {
  5878. int nameid,amount,flag = 0;
  5879. int x,y,m;
  5880. const char *mapname;
  5881. struct item item_tmp;
  5882. struct script_data *data;
  5883. data=script_getdata(st,2);
  5884. get_val(st,data);
  5885. if( data_isstring(data) ){
  5886. const char *name=conv_str(st,data);
  5887. struct item_data *item_data = itemdb_searchname(name);
  5888. if( item_data )
  5889. nameid=item_data->nameid;
  5890. else
  5891. nameid=UNKNOWN_ITEM_ID;
  5892. }else
  5893. nameid=conv_num(st,data);
  5894. amount=script_getnum(st,3);
  5895. mapname =script_getstr(st,4);
  5896. x =script_getnum(st,5);
  5897. y =script_getnum(st,6);
  5898. if(strcmp(mapname,"this")==0)
  5899. {
  5900. TBL_PC *sd;
  5901. sd = script_rid2sd(st);
  5902. if (!sd) return 0; //Failed...
  5903. m=sd->bl.m;
  5904. } else
  5905. m=map_mapname2mapid(mapname);
  5906. if(nameid<0) {
  5907. nameid = -nameid;
  5908. flag = 1;
  5909. }
  5910. if(nameid > 0) {
  5911. memset(&item_tmp,0,sizeof(item_tmp));
  5912. item_tmp.nameid=nameid;
  5913. if(!flag)
  5914. item_tmp.identify=1;
  5915. else
  5916. item_tmp.identify=itemdb_isidentified(nameid);
  5917. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  5918. }
  5919. return 0;
  5920. }
  5921. /// Counts / deletes the current item given by idx.
  5922. /// Used by buildin_delitem_search
  5923. /// Relies on all input data being already fully valid.
  5924. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5925. {
  5926. int delamount;
  5927. struct item* inv = &sd->status.inventory[idx];
  5928. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5929. if( delete_items )
  5930. {
  5931. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5932. {// delete associated pet
  5933. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5934. }
  5935. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5936. }
  5937. amount[0]-= delamount;
  5938. }
  5939. /// Searches for item(s) and checks, if there is enough of them.
  5940. /// Used by delitem and delitem2
  5941. /// Relies on all input data being already fully valid.
  5942. /// @param exact_match will also match item attributes and cards, not just name id
  5943. /// @return true when all items could be deleted, false when there were not enough items to delete
  5944. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5945. {
  5946. bool delete_items = false;
  5947. int i, amount, important;
  5948. struct item* inv;
  5949. // prefer always non-equipped items
  5950. it->equip = 0;
  5951. // when searching for nameid only, prefer additionally
  5952. if( !exact_match )
  5953. {
  5954. // non-refined items
  5955. it->refine = 0;
  5956. // card-less items
  5957. memset(it->card, 0, sizeof(it->card));
  5958. }
  5959. for(;;)
  5960. {
  5961. amount = it->amount;
  5962. important = 0;
  5963. // 1st pass -- less important items / exact match
  5964. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5965. {
  5966. inv = &sd->status.inventory[i];
  5967. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5968. {// wrong/invalid item
  5969. continue;
  5970. }
  5971. if( inv->equip != it->equip || inv->refine != it->refine )
  5972. {// not matching attributes
  5973. important++;
  5974. continue;
  5975. }
  5976. if( exact_match )
  5977. {
  5978. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5979. {// not matching exact attributes
  5980. continue;
  5981. }
  5982. }
  5983. else
  5984. {
  5985. if( sd->inventory_data[i]->type == IT_PETEGG )
  5986. {
  5987. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5988. {// pet which cannot be deleted
  5989. continue;
  5990. }
  5991. }
  5992. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  5993. {// named/carded item
  5994. important++;
  5995. continue;
  5996. }
  5997. }
  5998. // count / delete item
  5999. buildin_delitem_delete(sd, i, &amount, delete_items);
  6000. }
  6001. // 2nd pass -- any matching item
  6002. if( amount == 0 || important == 0 )
  6003. {// either everything was already consumed or no items were skipped
  6004. ;
  6005. }
  6006. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  6007. {
  6008. inv = &sd->status.inventory[i];
  6009. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  6010. {// wrong/invalid item
  6011. continue;
  6012. }
  6013. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  6014. {// pet which cannot be deleted
  6015. continue;
  6016. }
  6017. if( exact_match )
  6018. {
  6019. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  6020. {// not matching attributes
  6021. continue;
  6022. }
  6023. }
  6024. // count / delete item
  6025. buildin_delitem_delete(sd, i, &amount, delete_items);
  6026. }
  6027. if( amount )
  6028. {// not enough items
  6029. return false;
  6030. }
  6031. else if( delete_items )
  6032. {// we are done with the work
  6033. return true;
  6034. }
  6035. else
  6036. {// get rid of the items now
  6037. delete_items = true;
  6038. }
  6039. }
  6040. }
  6041. /// Deletes items from the target/attached player.
  6042. /// Prioritizes ordinary items.
  6043. ///
  6044. /// delitem <item id>,<amount>{,<account id>}
  6045. /// delitem "<item name>",<amount>{,<account id>}
  6046. BUILDIN_FUNC(delitem)
  6047. {
  6048. TBL_PC *sd;
  6049. struct item it;
  6050. struct script_data *data;
  6051. if( script_hasdata(st,4) )
  6052. {
  6053. int account_id = script_getnum(st,4);
  6054. sd = map_id2sd(account_id); // <account id>
  6055. if( sd == NULL )
  6056. {
  6057. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  6058. st->state = END;
  6059. return 1;
  6060. }
  6061. }
  6062. else
  6063. {
  6064. sd = script_rid2sd(st);// attached player
  6065. if( sd == NULL )
  6066. return 0;
  6067. }
  6068. data = script_getdata(st,2);
  6069. get_val(st,data);
  6070. if( data_isstring(data) )
  6071. {
  6072. const char* item_name = conv_str(st,data);
  6073. struct item_data* id = itemdb_searchname(item_name);
  6074. if( id == NULL )
  6075. {
  6076. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  6077. st->state = END;
  6078. return 1;
  6079. }
  6080. it.nameid = id->nameid;// "<item name>"
  6081. }
  6082. else
  6083. {
  6084. it.nameid = conv_num(st,data);// <item id>
  6085. if( !itemdb_exists( it.nameid ) )
  6086. {
  6087. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6088. st->state = END;
  6089. return 1;
  6090. }
  6091. }
  6092. it.amount=script_getnum(st,3);
  6093. if( it.amount <= 0 )
  6094. return 0;// nothing to do
  6095. if( buildin_delitem_search(sd, &it, false) )
  6096. {// success
  6097. return 0;
  6098. }
  6099. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6100. st->state = END;
  6101. st->mes_active = 0;
  6102. clif_scriptclose(sd, st->oid);
  6103. return 1;
  6104. }
  6105. /// Deletes items from the target/attached player.
  6106. ///
  6107. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6108. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6109. BUILDIN_FUNC(delitem2)
  6110. {
  6111. TBL_PC *sd;
  6112. struct item it;
  6113. struct script_data *data;
  6114. if( script_hasdata(st,11) )
  6115. {
  6116. int account_id = script_getnum(st,11);
  6117. sd = map_id2sd(account_id); // <account id>
  6118. if( sd == NULL )
  6119. {
  6120. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  6121. st->state = END;
  6122. return 1;
  6123. }
  6124. }
  6125. else
  6126. {
  6127. sd = script_rid2sd(st);// attached player
  6128. if( sd == NULL )
  6129. return 0;
  6130. }
  6131. data = script_getdata(st,2);
  6132. get_val(st,data);
  6133. if( data_isstring(data) )
  6134. {
  6135. const char* item_name = conv_str(st,data);
  6136. struct item_data* id = itemdb_searchname(item_name);
  6137. if( id == NULL )
  6138. {
  6139. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  6140. st->state = END;
  6141. return 1;
  6142. }
  6143. it.nameid = id->nameid;// "<item name>"
  6144. }
  6145. else
  6146. {
  6147. it.nameid = conv_num(st,data);// <item id>
  6148. if( !itemdb_exists( it.nameid ) )
  6149. {
  6150. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6151. st->state = END;
  6152. return 1;
  6153. }
  6154. }
  6155. it.amount=script_getnum(st,3);
  6156. it.identify=script_getnum(st,4);
  6157. it.refine=script_getnum(st,5);
  6158. it.attribute=script_getnum(st,6);
  6159. it.card[0]=(short)script_getnum(st,7);
  6160. it.card[1]=(short)script_getnum(st,8);
  6161. it.card[2]=(short)script_getnum(st,9);
  6162. it.card[3]=(short)script_getnum(st,10);
  6163. if( it.amount <= 0 )
  6164. return 0;// nothing to do
  6165. if( buildin_delitem_search(sd, &it, true) )
  6166. {// success
  6167. return 0;
  6168. }
  6169. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6170. st->state = END;
  6171. st->mes_active = 0;
  6172. clif_scriptclose(sd, st->oid);
  6173. return 1;
  6174. }
  6175. /*==========================================
  6176. * Enables/Disables use of items while in an NPC [Skotlex]
  6177. *------------------------------------------*/
  6178. BUILDIN_FUNC(enableitemuse)
  6179. {
  6180. TBL_PC *sd;
  6181. sd=script_rid2sd(st);
  6182. if (sd)
  6183. st->npc_item_flag = sd->npc_item_flag = 1;
  6184. return 0;
  6185. }
  6186. BUILDIN_FUNC(disableitemuse)
  6187. {
  6188. TBL_PC *sd;
  6189. sd=script_rid2sd(st);
  6190. if (sd)
  6191. st->npc_item_flag = sd->npc_item_flag = 0;
  6192. return 0;
  6193. }
  6194. /*==========================================
  6195. * Returns a character's specified stat.
  6196. * Check pc_readparam for available options.
  6197. *------------------------------------------*/
  6198. BUILDIN_FUNC(readparam)
  6199. {
  6200. int type;
  6201. TBL_PC *sd;
  6202. type=script_getnum(st,2);
  6203. if( script_hasdata(st,3) )
  6204. sd=map_nick2sd(script_getstr(st,3));
  6205. else
  6206. sd=script_rid2sd(st);
  6207. if(sd==NULL){
  6208. script_pushint(st,-1);
  6209. return 0;
  6210. }
  6211. script_pushint(st,pc_readparam(sd,type));
  6212. return 0;
  6213. }
  6214. /*==========================================
  6215. * Return charid identification
  6216. * return by @num :
  6217. * 0 : char_id
  6218. * 1 : party_id
  6219. * 2 : guild_id
  6220. * 3 : account_id
  6221. * 4 : bg_id
  6222. *------------------------------------------*/
  6223. BUILDIN_FUNC(getcharid)
  6224. {
  6225. int num;
  6226. TBL_PC *sd;
  6227. num = script_getnum(st,2);
  6228. if( script_hasdata(st,3) )
  6229. sd=map_nick2sd(script_getstr(st,3));
  6230. else
  6231. sd=script_rid2sd(st);
  6232. if(sd==NULL){
  6233. script_pushint(st,0); //return 0, according docs
  6234. return 0;
  6235. }
  6236. switch( num ) {
  6237. case 0: script_pushint(st,sd->status.char_id); break;
  6238. case 1: script_pushint(st,sd->status.party_id); break;
  6239. case 2: script_pushint(st,sd->status.guild_id); break;
  6240. case 3: script_pushint(st,sd->status.account_id); break;
  6241. case 4: script_pushint(st,sd->bg_id); break;
  6242. default:
  6243. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6244. script_pushint(st,0);
  6245. break;
  6246. }
  6247. return 0;
  6248. }
  6249. /*==========================================
  6250. * returns the GID of an NPC
  6251. *------------------------------------------*/
  6252. BUILDIN_FUNC(getnpcid)
  6253. {
  6254. int num = script_getnum(st,2);
  6255. struct npc_data* nd = NULL;
  6256. if( script_hasdata(st,3) )
  6257. {// unique npc name
  6258. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6259. {
  6260. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6261. script_pushint(st,0);
  6262. return 1;
  6263. }
  6264. }
  6265. switch (num) {
  6266. case 0:
  6267. script_pushint(st,nd ? nd->bl.id : st->oid);
  6268. break;
  6269. default:
  6270. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6271. script_pushint(st,0);
  6272. return 1;
  6273. }
  6274. return 0;
  6275. }
  6276. /*==========================================
  6277. * Return the name of the party_id
  6278. * null if not found
  6279. *------------------------------------------*/
  6280. BUILDIN_FUNC(getpartyname)
  6281. {
  6282. int party_id;
  6283. struct party_data* p;
  6284. party_id = script_getnum(st,2);
  6285. if( ( p = party_search(party_id) ) != NULL )
  6286. {
  6287. script_pushstrcopy(st,p->party.name);
  6288. }
  6289. else
  6290. {
  6291. script_pushconststr(st,"null");
  6292. }
  6293. return 0;
  6294. }
  6295. /*==========================================
  6296. * Get the information of the members of a party by type
  6297. * @party_id, @type
  6298. * return by @type :
  6299. * - : nom des membres
  6300. * 1 : char_id des membres
  6301. * 2 : account_id des membres
  6302. *------------------------------------------*/
  6303. BUILDIN_FUNC(getpartymember)
  6304. {
  6305. struct party_data *p;
  6306. int i,j=0,type=0;
  6307. p=party_search(script_getnum(st,2));
  6308. if( script_hasdata(st,3) )
  6309. type=script_getnum(st,3);
  6310. if(p!=NULL){
  6311. for(i=0;i<MAX_PARTY;i++){
  6312. if(p->party.member[i].account_id){
  6313. switch (type) {
  6314. case 2:
  6315. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6316. break;
  6317. case 1:
  6318. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6319. break;
  6320. default:
  6321. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6322. }
  6323. j++;
  6324. }
  6325. }
  6326. }
  6327. mapreg_setreg(add_str("$@partymembercount"),j);
  6328. return 0;
  6329. }
  6330. /*==========================================
  6331. * Retrieves party leader. if flag is specified,
  6332. * return some of the leader data. Otherwise, return name.
  6333. *------------------------------------------*/
  6334. BUILDIN_FUNC(getpartyleader)
  6335. {
  6336. int party_id, type = 0, i=0;
  6337. struct party_data *p;
  6338. party_id=script_getnum(st,2);
  6339. if( script_hasdata(st,3) )
  6340. type=script_getnum(st,3);
  6341. p=party_search(party_id);
  6342. if (p) //Search leader
  6343. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6344. if (!p || i == MAX_PARTY) { //leader not found
  6345. if (type)
  6346. script_pushint(st,-1);
  6347. else
  6348. script_pushconststr(st,"null");
  6349. return 0;
  6350. }
  6351. switch (type) {
  6352. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6353. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6354. case 3: script_pushint(st,p->party.member[i].class_); break;
  6355. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6356. case 5: script_pushint(st,p->party.member[i].lv); break;
  6357. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6358. }
  6359. return 0;
  6360. }
  6361. /*==========================================
  6362. * Return the name of the @guild_id
  6363. * null if not found
  6364. *------------------------------------------*/
  6365. BUILDIN_FUNC(getguildname)
  6366. {
  6367. int guild_id;
  6368. struct guild* g;
  6369. guild_id = script_getnum(st,2);
  6370. if( ( g = guild_search(guild_id) ) != NULL )
  6371. {
  6372. script_pushstrcopy(st,g->name);
  6373. }
  6374. else
  6375. {
  6376. script_pushconststr(st,"null");
  6377. }
  6378. return 0;
  6379. }
  6380. /*==========================================
  6381. * Return the name of the guild master of @guild_id
  6382. * null if not found
  6383. *------------------------------------------*/
  6384. BUILDIN_FUNC(getguildmaster)
  6385. {
  6386. int guild_id;
  6387. struct guild* g;
  6388. guild_id = script_getnum(st,2);
  6389. if( ( g = guild_search(guild_id) ) != NULL )
  6390. {
  6391. script_pushstrcopy(st,g->member[0].name);
  6392. }
  6393. else
  6394. {
  6395. script_pushconststr(st,"null");
  6396. }
  6397. return 0;
  6398. }
  6399. BUILDIN_FUNC(getguildmasterid)
  6400. {
  6401. int guild_id;
  6402. struct guild* g;
  6403. guild_id = script_getnum(st,2);
  6404. if( ( g = guild_search(guild_id) ) != NULL )
  6405. {
  6406. script_pushint(st,g->member[0].char_id);
  6407. }
  6408. else
  6409. {
  6410. script_pushint(st,0);
  6411. }
  6412. return 0;
  6413. }
  6414. /*==========================================
  6415. * Get char string information by type :
  6416. * Return by @type :
  6417. * 0 : char_name
  6418. * 1 : party_name or ""
  6419. * 2 : guild_name or ""
  6420. * 3 : map_name
  6421. * - : ""
  6422. *------------------------------------------*/
  6423. BUILDIN_FUNC(strcharinfo)
  6424. {
  6425. TBL_PC *sd;
  6426. int num;
  6427. struct guild* g;
  6428. struct party_data* p;
  6429. sd=script_rid2sd(st);
  6430. if (!sd) { //Avoid crashing....
  6431. script_pushconststr(st,"");
  6432. return 0;
  6433. }
  6434. num=script_getnum(st,2);
  6435. switch(num){
  6436. case 0:
  6437. script_pushstrcopy(st,sd->status.name);
  6438. break;
  6439. case 1:
  6440. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  6441. script_pushstrcopy(st,p->party.name);
  6442. } else {
  6443. script_pushconststr(st,"");
  6444. }
  6445. break;
  6446. case 2:
  6447. if( ( g = sd->guild ) != NULL ) {
  6448. script_pushstrcopy(st,g->name);
  6449. } else {
  6450. script_pushconststr(st,"");
  6451. }
  6452. break;
  6453. case 3:
  6454. script_pushconststr(st,map[sd->bl.m].name);
  6455. break;
  6456. default:
  6457. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6458. script_pushconststr(st,"");
  6459. break;
  6460. }
  6461. return 0;
  6462. }
  6463. /*==========================================
  6464. * Get npc string information by type
  6465. * return by @type:
  6466. * 0 : name
  6467. * 1 : str#
  6468. * 2 : #str
  6469. * 3 : ::str
  6470. * 4 : map name
  6471. *------------------------------------------*/
  6472. BUILDIN_FUNC(strnpcinfo)
  6473. {
  6474. TBL_NPC* nd;
  6475. int num;
  6476. char *buf,*name=NULL;
  6477. nd = map_id2nd(st->oid);
  6478. if (!nd) {
  6479. script_pushconststr(st, "");
  6480. return 0;
  6481. }
  6482. num = script_getnum(st,2);
  6483. switch(num){
  6484. case 0: // display name
  6485. name = aStrdup(nd->name);
  6486. break;
  6487. case 1: // visible part of display name
  6488. if((buf = strchr(nd->name,'#')) != NULL)
  6489. {
  6490. name = aStrdup(nd->name);
  6491. name[buf - nd->name] = 0;
  6492. } else // Return the name, there is no '#' present
  6493. name = aStrdup(nd->name);
  6494. break;
  6495. case 2: // # fragment
  6496. if((buf = strchr(nd->name,'#')) != NULL)
  6497. name = aStrdup(buf+1);
  6498. break;
  6499. case 3: // unique name
  6500. name = aStrdup(nd->exname);
  6501. break;
  6502. case 4: // map name
  6503. name = aStrdup(map[nd->bl.m].name);
  6504. break;
  6505. }
  6506. if(name)
  6507. script_pushstr(st, name);
  6508. else
  6509. script_pushconststr(st, "");
  6510. return 0;
  6511. }
  6512. // aegis->athena slot position conversion table
  6513. 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,EQP_COSTUME_HEAD_LOW,EQP_COSTUME_HEAD_MID,EQP_COSTUME_HEAD_TOP,EQP_COSTUME_GARMENT};
  6514. /*==========================================
  6515. * GetEquipID(Pos); Pos: 1-14
  6516. *------------------------------------------*/
  6517. BUILDIN_FUNC(getequipid)
  6518. {
  6519. int i, num;
  6520. TBL_PC* sd;
  6521. struct item_data* item;
  6522. sd = script_rid2sd(st);
  6523. if( sd == NULL )
  6524. return 0;
  6525. num = script_getnum(st,2) - 1;
  6526. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6527. {
  6528. script_pushint(st,-1);
  6529. return 0;
  6530. }
  6531. // get inventory position of item
  6532. i = pc_checkequip(sd,equip[num]);
  6533. if( i < 0 )
  6534. {
  6535. script_pushint(st,-1);
  6536. return 0;
  6537. }
  6538. item = sd->inventory_data[i];
  6539. if( item != 0 )
  6540. script_pushint(st,item->nameid);
  6541. else
  6542. script_pushint(st,0);
  6543. return 0;
  6544. }
  6545. /*==========================================
  6546. * Get the equipement name at pos
  6547. * return item jname or ""
  6548. *------------------------------------------*/
  6549. BUILDIN_FUNC(getequipname)
  6550. {
  6551. int i, num;
  6552. TBL_PC* sd;
  6553. struct item_data* item;
  6554. sd = script_rid2sd(st);
  6555. if( sd == NULL )
  6556. return 0;
  6557. num = script_getnum(st,2) - 1;
  6558. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6559. {
  6560. script_pushconststr(st,"");
  6561. return 0;
  6562. }
  6563. // get inventory position of item
  6564. i = pc_checkequip(sd,equip[num]);
  6565. if( i < 0 )
  6566. {
  6567. script_pushconststr(st,"");
  6568. return 0;
  6569. }
  6570. item = sd->inventory_data[i];
  6571. if( item != 0 )
  6572. script_pushstrcopy(st,item->jname);
  6573. else
  6574. script_pushconststr(st,"");
  6575. return 0;
  6576. }
  6577. /*==========================================
  6578. * getbrokenid [Valaris]
  6579. *------------------------------------------*/
  6580. BUILDIN_FUNC(getbrokenid)
  6581. {
  6582. int i,num,id=0,brokencounter=0;
  6583. TBL_PC *sd;
  6584. sd = script_rid2sd(st);
  6585. if( sd == NULL )
  6586. return 0;
  6587. num=script_getnum(st,2);
  6588. for(i=0; i<MAX_INVENTORY; i++) {
  6589. if(sd->status.inventory[i].attribute){
  6590. brokencounter++;
  6591. if(num==brokencounter){
  6592. id=sd->status.inventory[i].nameid;
  6593. break;
  6594. }
  6595. }
  6596. }
  6597. script_pushint(st,id);
  6598. return 0;
  6599. }
  6600. /*==========================================
  6601. * repair [Valaris]
  6602. *------------------------------------------*/
  6603. BUILDIN_FUNC(repair)
  6604. {
  6605. int i,num;
  6606. int repaircounter=0;
  6607. TBL_PC *sd;
  6608. sd = script_rid2sd(st);
  6609. if( sd == NULL )
  6610. return 0;
  6611. num=script_getnum(st,2);
  6612. for(i=0; i<MAX_INVENTORY; i++) {
  6613. if(sd->status.inventory[i].attribute){
  6614. repaircounter++;
  6615. if(num==repaircounter){
  6616. sd->status.inventory[i].attribute=0;
  6617. clif_equiplist(sd);
  6618. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6619. clif_misceffect(&sd->bl, 3);
  6620. break;
  6621. }
  6622. }
  6623. }
  6624. return 0;
  6625. }
  6626. /*==========================================
  6627. * repairall
  6628. *------------------------------------------*/
  6629. BUILDIN_FUNC(repairall)
  6630. {
  6631. int i, repaircounter = 0;
  6632. TBL_PC *sd;
  6633. sd = script_rid2sd(st);
  6634. if(sd == NULL)
  6635. return 0;
  6636. for(i = 0; i < MAX_INVENTORY; i++)
  6637. {
  6638. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  6639. {
  6640. sd->status.inventory[i].attribute = 0;
  6641. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  6642. repaircounter++;
  6643. }
  6644. }
  6645. if(repaircounter)
  6646. {
  6647. clif_misceffect(&sd->bl, 3);
  6648. clif_equiplist(sd);
  6649. }
  6650. return 0;
  6651. }
  6652. /*==========================================
  6653. * Chk if player have something equiped at pos
  6654. *------------------------------------------*/
  6655. BUILDIN_FUNC(getequipisequiped)
  6656. {
  6657. int i = -1,num;
  6658. TBL_PC *sd;
  6659. num = script_getnum(st,2);
  6660. sd = script_rid2sd(st);
  6661. if( sd == NULL )
  6662. return 0;
  6663. if (num > 0 && num <= ARRAYLENGTH(equip))
  6664. i=pc_checkequip(sd,equip[num-1]);
  6665. if(i >= 0)
  6666. script_pushint(st,1);
  6667. else
  6668. script_pushint(st,0);
  6669. return 0;
  6670. }
  6671. /*==========================================
  6672. * Chk if the player have something equiped at pos
  6673. * if so chk if this item ain't marked not refinable or rental
  6674. * return (npc)
  6675. * 1 : true
  6676. * 0 : false
  6677. *------------------------------------------*/
  6678. BUILDIN_FUNC(getequipisenableref)
  6679. {
  6680. int i = -1,num;
  6681. TBL_PC *sd;
  6682. num = script_getnum(st,2);
  6683. sd = script_rid2sd(st);
  6684. if( sd == NULL )
  6685. return 0;
  6686. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6687. i = pc_checkequip(sd,equip[num-1]);
  6688. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6689. script_pushint(st,1);
  6690. else
  6691. script_pushint(st,0);
  6692. return 0;
  6693. }
  6694. /*==========================================
  6695. * Get the item refined value at pos
  6696. * return (npc)
  6697. * x : refine amount
  6698. * 0 : false (not refined)
  6699. *------------------------------------------*/
  6700. BUILDIN_FUNC(getequiprefinerycnt)
  6701. {
  6702. int i = -1,num;
  6703. TBL_PC *sd;
  6704. num = script_getnum(st,2);
  6705. sd = script_rid2sd(st);
  6706. if( sd == NULL )
  6707. return 0;
  6708. if (num > 0 && num <= ARRAYLENGTH(equip))
  6709. i=pc_checkequip(sd,equip[num-1]);
  6710. if(i >= 0)
  6711. script_pushint(st,sd->status.inventory[i].refine);
  6712. else
  6713. script_pushint(st,0);
  6714. return 0;
  6715. }
  6716. /*==========================================
  6717. * Get the weapon level value at pos
  6718. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  6719. * return (npc)
  6720. * x : weapon level
  6721. * 0 : false
  6722. *------------------------------------------*/
  6723. BUILDIN_FUNC(getequipweaponlv)
  6724. {
  6725. int i = -1,num;
  6726. TBL_PC *sd;
  6727. num = script_getnum(st,2);
  6728. sd = script_rid2sd(st);
  6729. if( sd == NULL )
  6730. return 0;
  6731. if (num > 0 && num <= ARRAYLENGTH(equip))
  6732. i=pc_checkequip(sd,equip[num-1]);
  6733. if(i >= 0 && sd->inventory_data[i])
  6734. script_pushint(st,sd->inventory_data[i]->wlv);
  6735. else
  6736. script_pushint(st,0);
  6737. return 0;
  6738. }
  6739. /*==========================================
  6740. * Get the item refine chance (from refine.txt) for item at pos
  6741. * return (npc)
  6742. * x : refine chance
  6743. * 0 : false (max refine level or unequip..)
  6744. *------------------------------------------*/
  6745. BUILDIN_FUNC(getequippercentrefinery)
  6746. {
  6747. int i = -1,num;
  6748. TBL_PC *sd;
  6749. num = script_getnum(st,2);
  6750. sd = script_rid2sd(st);
  6751. if( sd == NULL )
  6752. return 0;
  6753. if (num > 0 && num <= ARRAYLENGTH(equip))
  6754. i=pc_checkequip(sd,equip[num-1]);
  6755. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6756. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6757. else
  6758. script_pushint(st,0);
  6759. return 0;
  6760. }
  6761. /*==========================================
  6762. * Refine +1 item at pos and log and display refine
  6763. *------------------------------------------*/
  6764. BUILDIN_FUNC(successrefitem)
  6765. {
  6766. int i=-1,num,ep;
  6767. TBL_PC *sd;
  6768. num = script_getnum(st,2);
  6769. sd = script_rid2sd(st);
  6770. if( sd == NULL )
  6771. return 0;
  6772. if (num > 0 && num <= ARRAYLENGTH(equip))
  6773. i=pc_checkequip(sd,equip[num-1]);
  6774. if(i >= 0) {
  6775. ep=sd->status.inventory[i].equip;
  6776. //Logs items, got from (N)PC scripts [Lupus]
  6777. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6778. sd->status.inventory[i].refine++;
  6779. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6780. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6781. clif_delitem(sd,i,1,3);
  6782. //Logs items, got from (N)PC scripts [Lupus]
  6783. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6784. clif_additem(sd,i,1,0);
  6785. pc_equipitem(sd,i,ep);
  6786. clif_misceffect(&sd->bl,3);
  6787. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6788. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6789. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6790. ){ // Fame point system [DracoRPG]
  6791. switch (sd->inventory_data[i]->wlv){
  6792. case 1:
  6793. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6794. break;
  6795. case 2:
  6796. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6797. break;
  6798. case 3:
  6799. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6800. break;
  6801. }
  6802. }
  6803. }
  6804. return 0;
  6805. }
  6806. /*==========================================
  6807. * Show a failed Refine +1 attempt
  6808. *------------------------------------------*/
  6809. BUILDIN_FUNC(failedrefitem)
  6810. {
  6811. int i=-1,num;
  6812. TBL_PC *sd;
  6813. num = script_getnum(st,2);
  6814. sd = script_rid2sd(st);
  6815. if( sd == NULL )
  6816. return 0;
  6817. if (num > 0 && num <= ARRAYLENGTH(equip))
  6818. i=pc_checkequip(sd,equip[num-1]);
  6819. if(i >= 0) {
  6820. sd->status.inventory[i].refine = 0;
  6821. pc_unequipitem(sd,i,3); //recalculate bonus
  6822. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  6823. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6824. clif_misceffect(&sd->bl,2); // display failure effect
  6825. }
  6826. return 0;
  6827. }
  6828. /*==========================================
  6829. * Downgrades an Equipment Part by -1 . [Masao]
  6830. *------------------------------------------*/
  6831. BUILDIN_FUNC(downrefitem)
  6832. {
  6833. int i = -1,num,ep;
  6834. TBL_PC *sd;
  6835. num = script_getnum(st,2);
  6836. sd = script_rid2sd(st);
  6837. if( sd == NULL )
  6838. return 0;
  6839. if (num > 0 && num <= ARRAYLENGTH(equip))
  6840. i = pc_checkequip(sd,equip[num-1]);
  6841. if(i >= 0) {
  6842. ep = sd->status.inventory[i].equip;
  6843. //Logs items, got from (N)PC scripts [Lupus]
  6844. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6845. sd->status.inventory[i].refine++;
  6846. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6847. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine = sd->status.inventory[i].refine - 2);
  6848. clif_delitem(sd,i,1,3);
  6849. //Logs items, got from (N)PC scripts [Lupus]
  6850. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6851. clif_additem(sd,i,1,0);
  6852. pc_equipitem(sd,i,ep);
  6853. clif_misceffect(&sd->bl,2);
  6854. }
  6855. return 0;
  6856. }
  6857. /*==========================================
  6858. * Delete the item equipped at pos.
  6859. *------------------------------------------*/
  6860. BUILDIN_FUNC(delequip)
  6861. {
  6862. int i=-1,num;
  6863. TBL_PC *sd;
  6864. num = script_getnum(st,2);
  6865. sd = script_rid2sd(st);
  6866. if( sd == NULL )
  6867. return 0;
  6868. if (num > 0 && num <= ARRAYLENGTH(equip))
  6869. i=pc_checkequip(sd,equip[num-1]);
  6870. if(i >= 0) {
  6871. pc_unequipitem(sd,i,3); //recalculate bonus
  6872. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6873. }
  6874. return 0;
  6875. }
  6876. /*==========================================
  6877. *
  6878. *------------------------------------------*/
  6879. BUILDIN_FUNC(statusup)
  6880. {
  6881. int type;
  6882. TBL_PC *sd;
  6883. type=script_getnum(st,2);
  6884. sd = script_rid2sd(st);
  6885. if( sd == NULL )
  6886. return 0;
  6887. pc_statusup(sd,type);
  6888. return 0;
  6889. }
  6890. /*==========================================
  6891. *
  6892. *------------------------------------------*/
  6893. BUILDIN_FUNC(statusup2)
  6894. {
  6895. int type,val;
  6896. TBL_PC *sd;
  6897. type=script_getnum(st,2);
  6898. val=script_getnum(st,3);
  6899. sd = script_rid2sd(st);
  6900. if( sd == NULL )
  6901. return 0;
  6902. pc_statusup2(sd,type,val);
  6903. return 0;
  6904. }
  6905. /// See 'doc/item_bonus.txt'
  6906. ///
  6907. /// bonus <bonus type>,<val1>;
  6908. /// bonus2 <bonus type>,<val1>,<val2>;
  6909. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6910. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6911. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6912. BUILDIN_FUNC(bonus)
  6913. {
  6914. int type;
  6915. int val1;
  6916. int val2 = 0;
  6917. int val3 = 0;
  6918. int val4 = 0;
  6919. int val5 = 0;
  6920. TBL_PC* sd;
  6921. sd = script_rid2sd(st);
  6922. if( sd == NULL )
  6923. return 0; // no player attached
  6924. type = script_getnum(st,2);
  6925. switch( type ) {
  6926. case SP_AUTOSPELL:
  6927. case SP_AUTOSPELL_WHENHIT:
  6928. case SP_AUTOSPELL_ONSKILL:
  6929. case SP_SKILL_ATK:
  6930. case SP_SKILL_HEAL:
  6931. case SP_SKILL_HEAL2:
  6932. case SP_ADD_SKILL_BLOW:
  6933. case SP_CASTRATE:
  6934. case SP_ADDEFF_ONSKILL:
  6935. case SP_SKILL_USE_SP_RATE:
  6936. case SP_SKILL_COOLDOWN:
  6937. case SP_SKILL_FIXEDCAST:
  6938. case SP_SKILL_VARIABLECAST:
  6939. case SP_VARCASTRATE:
  6940. case SP_FIXCASTRATE:
  6941. case SP_SKILL_USE_SP:
  6942. // these bonuses support skill names
  6943. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6944. break;
  6945. default:
  6946. val1 = script_getnum(st,3);
  6947. break;
  6948. }
  6949. switch( script_lastdata(st)-2 ) {
  6950. case 1:
  6951. pc_bonus(sd, type, val1);
  6952. break;
  6953. case 2:
  6954. val2 = script_getnum(st,4);
  6955. pc_bonus2(sd, type, val1, val2);
  6956. break;
  6957. case 3:
  6958. val2 = script_getnum(st,4);
  6959. val3 = script_getnum(st,5);
  6960. pc_bonus3(sd, type, val1, val2, val3);
  6961. break;
  6962. case 4:
  6963. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6964. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6965. else
  6966. val2 = script_getnum(st,4);
  6967. val3 = script_getnum(st,5);
  6968. val4 = script_getnum(st,6);
  6969. pc_bonus4(sd, type, val1, val2, val3, val4);
  6970. break;
  6971. case 5:
  6972. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6973. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6974. else
  6975. val2 = script_getnum(st,4);
  6976. val3 = script_getnum(st,5);
  6977. val4 = script_getnum(st,6);
  6978. val5 = script_getnum(st,7);
  6979. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  6980. break;
  6981. default:
  6982. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  6983. break;
  6984. }
  6985. return 0;
  6986. }
  6987. BUILDIN_FUNC(autobonus)
  6988. {
  6989. unsigned int dur;
  6990. short rate;
  6991. short atk_type = 0;
  6992. TBL_PC* sd;
  6993. const char *bonus_script, *other_script = NULL;
  6994. sd = script_rid2sd(st);
  6995. if( sd == NULL )
  6996. return 0; // no player attached
  6997. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6998. return 0;
  6999. rate = script_getnum(st,3);
  7000. dur = script_getnum(st,4);
  7001. bonus_script = script_getstr(st,2);
  7002. if( !rate || !dur || !bonus_script )
  7003. return 0;
  7004. if( script_hasdata(st,5) )
  7005. atk_type = script_getnum(st,5);
  7006. if( script_hasdata(st,6) )
  7007. other_script = script_getstr(st,6);
  7008. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7009. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7010. {
  7011. script_add_autobonus(bonus_script);
  7012. if( other_script )
  7013. script_add_autobonus(other_script);
  7014. }
  7015. return 0;
  7016. }
  7017. BUILDIN_FUNC(autobonus2)
  7018. {
  7019. unsigned int dur;
  7020. short rate;
  7021. short atk_type = 0;
  7022. TBL_PC* sd;
  7023. const char *bonus_script, *other_script = NULL;
  7024. sd = script_rid2sd(st);
  7025. if( sd == NULL )
  7026. return 0; // no player attached
  7027. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7028. return 0;
  7029. rate = script_getnum(st,3);
  7030. dur = script_getnum(st,4);
  7031. bonus_script = script_getstr(st,2);
  7032. if( !rate || !dur || !bonus_script )
  7033. return 0;
  7034. if( script_hasdata(st,5) )
  7035. atk_type = script_getnum(st,5);
  7036. if( script_hasdata(st,6) )
  7037. other_script = script_getstr(st,6);
  7038. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7039. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7040. {
  7041. script_add_autobonus(bonus_script);
  7042. if( other_script )
  7043. script_add_autobonus(other_script);
  7044. }
  7045. return 0;
  7046. }
  7047. BUILDIN_FUNC(autobonus3)
  7048. {
  7049. unsigned int dur;
  7050. short rate,atk_type;
  7051. TBL_PC* sd;
  7052. const char *bonus_script, *other_script = NULL;
  7053. sd = script_rid2sd(st);
  7054. if( sd == NULL )
  7055. return 0; // no player attached
  7056. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7057. return 0;
  7058. rate = script_getnum(st,3);
  7059. dur = script_getnum(st,4);
  7060. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7061. bonus_script = script_getstr(st,2);
  7062. if( !rate || !dur || !atk_type || !bonus_script )
  7063. return 0;
  7064. if( script_hasdata(st,6) )
  7065. other_script = script_getstr(st,6);
  7066. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7067. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  7068. {
  7069. script_add_autobonus(bonus_script);
  7070. if( other_script )
  7071. script_add_autobonus(other_script);
  7072. }
  7073. return 0;
  7074. }
  7075. /// Changes the level of a player skill.
  7076. /// <flag> defaults to 1
  7077. /// <flag>=0 : set the level of the skill
  7078. /// <flag>=1 : set the temporary level of the skill
  7079. /// <flag>=2 : add to the level of the skill
  7080. ///
  7081. /// skill <skill id>,<level>,<flag>
  7082. /// skill <skill id>,<level>
  7083. /// skill "<skill name>",<level>,<flag>
  7084. /// skill "<skill name>",<level>
  7085. BUILDIN_FUNC(skill)
  7086. {
  7087. int id;
  7088. int level;
  7089. int flag = 1;
  7090. TBL_PC* sd;
  7091. sd = script_rid2sd(st);
  7092. if( sd == NULL )
  7093. return 0;// no player attached, report source
  7094. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7095. level = script_getnum(st,3);
  7096. if( script_hasdata(st,4) )
  7097. flag = script_getnum(st,4);
  7098. pc_skill(sd, id, level, flag);
  7099. return 0;
  7100. }
  7101. /// Changes the level of a player skill.
  7102. /// like skill, but <flag> defaults to 2
  7103. ///
  7104. /// addtoskill <skill id>,<amount>,<flag>
  7105. /// addtoskill <skill id>,<amount>
  7106. /// addtoskill "<skill name>",<amount>,<flag>
  7107. /// addtoskill "<skill name>",<amount>
  7108. ///
  7109. /// @see skill
  7110. BUILDIN_FUNC(addtoskill)
  7111. {
  7112. int id;
  7113. int level;
  7114. int flag = 2;
  7115. TBL_PC* sd;
  7116. sd = script_rid2sd(st);
  7117. if( sd == NULL )
  7118. return 0;// no player attached, report source
  7119. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7120. level = script_getnum(st,3);
  7121. if( script_hasdata(st,4) )
  7122. flag = script_getnum(st,4);
  7123. pc_skill(sd, id, level, flag);
  7124. return 0;
  7125. }
  7126. /// Increases the level of a guild skill.
  7127. ///
  7128. /// guildskill <skill id>,<amount>;
  7129. /// guildskill "<skill name>",<amount>;
  7130. BUILDIN_FUNC(guildskill)
  7131. {
  7132. int id;
  7133. int level;
  7134. TBL_PC* sd;
  7135. int i;
  7136. sd = script_rid2sd(st);
  7137. if( sd == NULL )
  7138. return 0;// no player attached, report source
  7139. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7140. level = script_getnum(st,3);
  7141. for( i=0; i < level; i++ )
  7142. guild_skillup(sd, id);
  7143. return 0;
  7144. }
  7145. /// Returns the level of the player skill.
  7146. ///
  7147. /// getskilllv(<skill id>) -> <level>
  7148. /// getskilllv("<skill name>") -> <level>
  7149. BUILDIN_FUNC(getskilllv)
  7150. {
  7151. int id;
  7152. TBL_PC* sd;
  7153. sd = script_rid2sd(st);
  7154. if( sd == NULL )
  7155. return 0;// no player attached, report source
  7156. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7157. script_pushint(st, pc_checkskill(sd,id));
  7158. return 0;
  7159. }
  7160. /// Returns the level of the guild skill.
  7161. ///
  7162. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7163. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7164. BUILDIN_FUNC(getgdskilllv)
  7165. {
  7166. int guild_id;
  7167. uint16 skill_id;
  7168. struct guild* g;
  7169. guild_id = script_getnum(st,2);
  7170. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7171. g = guild_search(guild_id);
  7172. if( g == NULL )
  7173. script_pushint(st, -1);
  7174. else
  7175. script_pushint(st, guild_checkskill(g,skill_id));
  7176. return 0;
  7177. }
  7178. /// Returns the 'basic_skill_check' setting.
  7179. /// This config determines if the server checks the skill level of NV_BASIC
  7180. /// before allowing the basic actions.
  7181. ///
  7182. /// basicskillcheck() -> <bool>
  7183. BUILDIN_FUNC(basicskillcheck)
  7184. {
  7185. script_pushint(st, battle_config.basic_skill_check);
  7186. return 0;
  7187. }
  7188. /// Returns the GM level of the player.
  7189. ///
  7190. /// getgmlevel() -> <level>
  7191. BUILDIN_FUNC(getgmlevel)
  7192. {
  7193. TBL_PC* sd;
  7194. sd = script_rid2sd(st);
  7195. if( sd == NULL )
  7196. return 0;// no player attached, report source
  7197. script_pushint(st, pc_get_group_level(sd));
  7198. return 0;
  7199. }
  7200. /// Returns the group ID of the player.
  7201. ///
  7202. /// getgroupid() -> <int>
  7203. BUILDIN_FUNC(getgroupid)
  7204. {
  7205. TBL_PC* sd;
  7206. sd = script_rid2sd(st);
  7207. if (sd == NULL)
  7208. return 1; // no player attached, report source
  7209. script_pushint(st, pc_get_group_id(sd));
  7210. return 0;
  7211. }
  7212. /// Terminates the execution of this script instance.
  7213. ///
  7214. /// end
  7215. BUILDIN_FUNC(end)
  7216. {
  7217. TBL_PC* sd;
  7218. sd = map_id2sd(st->rid);
  7219. st->state = END;
  7220. if( st->mes_active )
  7221. st->mes_active = 0;
  7222. if( sd )
  7223. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7224. return 0;
  7225. }
  7226. /// Checks if the player has that effect state (option).
  7227. ///
  7228. /// checkoption(<option>) -> <bool>
  7229. BUILDIN_FUNC(checkoption)
  7230. {
  7231. int option;
  7232. TBL_PC* sd;
  7233. sd = script_rid2sd(st);
  7234. if( sd == NULL )
  7235. return 0;// no player attached, report source
  7236. option = script_getnum(st,2);
  7237. if( sd->sc.option&option )
  7238. script_pushint(st, 1);
  7239. else
  7240. script_pushint(st, 0);
  7241. return 0;
  7242. }
  7243. /// Checks if the player is in that body state (opt1).
  7244. ///
  7245. /// checkoption1(<opt1>) -> <bool>
  7246. BUILDIN_FUNC(checkoption1)
  7247. {
  7248. int opt1;
  7249. TBL_PC* sd;
  7250. sd = script_rid2sd(st);
  7251. if( sd == NULL )
  7252. return 0;// no player attached, report source
  7253. opt1 = script_getnum(st,2);
  7254. if( sd->sc.opt1 == opt1 )
  7255. script_pushint(st, 1);
  7256. else
  7257. script_pushint(st, 0);
  7258. return 0;
  7259. }
  7260. /// Checks if the player has that health state (opt2).
  7261. ///
  7262. /// checkoption2(<opt2>) -> <bool>
  7263. BUILDIN_FUNC(checkoption2)
  7264. {
  7265. int opt2;
  7266. TBL_PC* sd;
  7267. sd = script_rid2sd(st);
  7268. if( sd == NULL )
  7269. return 0;// no player attached, report source
  7270. opt2 = script_getnum(st,2);
  7271. if( sd->sc.opt2&opt2 )
  7272. script_pushint(st, 1);
  7273. else
  7274. script_pushint(st, 0);
  7275. return 0;
  7276. }
  7277. /// Changes the effect state (option) of the player.
  7278. /// <flag> defaults to 1
  7279. /// <flag>=0 : removes the option
  7280. /// <flag>=other : adds the option
  7281. ///
  7282. /// setoption <option>,<flag>;
  7283. /// setoption <option>;
  7284. BUILDIN_FUNC(setoption)
  7285. {
  7286. int option;
  7287. int flag = 1;
  7288. TBL_PC* sd;
  7289. sd = script_rid2sd(st);
  7290. if( sd == NULL )
  7291. return 0;// no player attached, report source
  7292. option = script_getnum(st,2);
  7293. if( script_hasdata(st,3) )
  7294. flag = script_getnum(st,3);
  7295. else if( !option ){// Request to remove everything.
  7296. flag = 0;
  7297. option = OPTION_FALCON|OPTION_RIDING;
  7298. #ifndef NEW_CARTS
  7299. option |= OPTION_CART;
  7300. #endif
  7301. }
  7302. if( flag ){// Add option
  7303. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7304. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7305. pc_setoption(sd, sd->sc.option|option);
  7306. } else// Remove option
  7307. pc_setoption(sd, sd->sc.option&~option);
  7308. return 0;
  7309. }
  7310. /// Returns if the player has a cart.
  7311. ///
  7312. /// checkcart() -> <bool>
  7313. ///
  7314. /// @author Valaris
  7315. BUILDIN_FUNC(checkcart)
  7316. {
  7317. TBL_PC* sd;
  7318. sd = script_rid2sd(st);
  7319. if( sd == NULL )
  7320. return 0;// no player attached, report source
  7321. if( pc_iscarton(sd) )
  7322. script_pushint(st, 1);
  7323. else
  7324. script_pushint(st, 0);
  7325. return 0;
  7326. }
  7327. /// Sets the cart of the player.
  7328. /// <type> defaults to 1
  7329. /// <type>=0 : removes the cart
  7330. /// <type>=1 : Normal cart
  7331. /// <type>=2 : Wooden cart
  7332. /// <type>=3 : Covered cart with flowers and ferns
  7333. /// <type>=4 : Wooden cart with a Panda doll on the back
  7334. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7335. ///
  7336. /// setcart <type>;
  7337. /// setcart;
  7338. BUILDIN_FUNC(setcart)
  7339. {
  7340. int type = 1;
  7341. TBL_PC* sd;
  7342. sd = script_rid2sd(st);
  7343. if( sd == NULL )
  7344. return 0;// no player attached, report source
  7345. if( script_hasdata(st,2) )
  7346. type = script_getnum(st,2);
  7347. pc_setcart(sd, type);
  7348. return 0;
  7349. }
  7350. /// Returns if the player has a falcon.
  7351. ///
  7352. /// checkfalcon() -> <bool>
  7353. ///
  7354. /// @author Valaris
  7355. BUILDIN_FUNC(checkfalcon)
  7356. {
  7357. TBL_PC* sd;
  7358. sd = script_rid2sd(st);
  7359. if( sd == NULL )
  7360. return 0;// no player attached, report source
  7361. if( pc_isfalcon(sd) )
  7362. script_pushint(st, 1);
  7363. else
  7364. script_pushint(st, 0);
  7365. return 0;
  7366. }
  7367. /// Sets if the player has a falcon or not.
  7368. /// <flag> defaults to 1
  7369. ///
  7370. /// setfalcon <flag>;
  7371. /// setfalcon;
  7372. BUILDIN_FUNC(setfalcon)
  7373. {
  7374. int flag = 1;
  7375. TBL_PC* sd;
  7376. sd = script_rid2sd(st);
  7377. if( sd == NULL )
  7378. return 0;// no player attached, report source
  7379. if( script_hasdata(st,2) )
  7380. flag = script_getnum(st,2);
  7381. pc_setfalcon(sd, flag);
  7382. return 0;
  7383. }
  7384. /// Returns if the player is riding.
  7385. ///
  7386. /// checkriding() -> <bool>
  7387. ///
  7388. /// @author Valaris
  7389. BUILDIN_FUNC(checkriding)
  7390. {
  7391. TBL_PC* sd;
  7392. sd = script_rid2sd(st);
  7393. if( sd == NULL )
  7394. return 0;// no player attached, report source
  7395. if( pc_isriding(sd) || pc_isridingwug(sd) || pc_isridingdragon(sd) )
  7396. script_pushint(st, 1);
  7397. else
  7398. script_pushint(st, 0);
  7399. return 0;
  7400. }
  7401. /// Sets if the player is riding.
  7402. /// <flag> defaults to 1
  7403. ///
  7404. /// setriding <flag>;
  7405. /// setriding;
  7406. BUILDIN_FUNC(setriding)
  7407. {
  7408. int flag = 1;
  7409. TBL_PC* sd;
  7410. sd = script_rid2sd(st);
  7411. if( sd == NULL )
  7412. return 0;// no player attached, report source
  7413. if( script_hasdata(st,2) )
  7414. flag = script_getnum(st,2);
  7415. pc_setriding(sd, flag);
  7416. return 0;
  7417. }
  7418. /// Returns if the player has a warg.
  7419. ///
  7420. /// checkwug() -> <bool>
  7421. ///
  7422. BUILDIN_FUNC(checkwug)
  7423. {
  7424. TBL_PC* sd;
  7425. sd = script_rid2sd(st);
  7426. if( sd == NULL )
  7427. return 0;// no player attached, report source
  7428. if( pc_iswug(sd) || pc_isridingwug(sd) )
  7429. script_pushint(st, 1);
  7430. else
  7431. script_pushint(st, 0);
  7432. return 0;
  7433. }
  7434. /// Returns if the player is wearing MADO Gear.
  7435. ///
  7436. /// checkmadogear() -> <bool>
  7437. ///
  7438. BUILDIN_FUNC(checkmadogear)
  7439. {
  7440. TBL_PC* sd;
  7441. sd = script_rid2sd(st);
  7442. if( sd == NULL )
  7443. return 0;// no player attached, report source
  7444. if( pc_ismadogear(sd) )
  7445. script_pushint(st, 1);
  7446. else
  7447. script_pushint(st, 0);
  7448. return 0;
  7449. }
  7450. /// Sets if the player is riding MADO Gear.
  7451. /// <flag> defaults to 1
  7452. ///
  7453. /// setmadogear <flag>;
  7454. /// setmadogear;
  7455. BUILDIN_FUNC(setmadogear)
  7456. {
  7457. int flag = 1;
  7458. TBL_PC* sd;
  7459. sd = script_rid2sd(st);
  7460. if( sd == NULL )
  7461. return 0;// no player attached, report source
  7462. if( script_hasdata(st,2) )
  7463. flag = script_getnum(st,2);
  7464. pc_setmadogear(sd, flag);
  7465. return 0;
  7466. }
  7467. /// Sets the save point of the player.
  7468. ///
  7469. /// save "<map name>",<x>,<y>
  7470. /// savepoint "<map name>",<x>,<y>
  7471. BUILDIN_FUNC(savepoint)
  7472. {
  7473. int x;
  7474. int y;
  7475. short map;
  7476. const char* str;
  7477. TBL_PC* sd;
  7478. sd = script_rid2sd(st);
  7479. if( sd == NULL )
  7480. return 0;// no player attached, report source
  7481. str = script_getstr(st, 2);
  7482. x = script_getnum(st,3);
  7483. y = script_getnum(st,4);
  7484. map = mapindex_name2id(str);
  7485. if( map )
  7486. pc_setsavepoint(sd, map, x, y);
  7487. return 0;
  7488. }
  7489. /*==========================================
  7490. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  7491. *------------------------------------------*/
  7492. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  7493. {
  7494. int type;
  7495. time_t timer;
  7496. struct tm *t;
  7497. type=script_getnum(st,2);
  7498. switch(type){
  7499. case 2:
  7500. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  7501. // from the system clock.)
  7502. script_pushint(st,(int)time(NULL));
  7503. break;
  7504. case 1:
  7505. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  7506. time(&timer);
  7507. t=localtime(&timer);
  7508. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  7509. break;
  7510. case 0:
  7511. default:
  7512. //type 0:(System Ticks)
  7513. script_pushint(st,gettick());
  7514. break;
  7515. }
  7516. return 0;
  7517. }
  7518. /*==========================================
  7519. * GetTime(Type);
  7520. * 1: Sec 2: Min 3: Hour
  7521. * 4: WeekDay 5: MonthDay 6: Month
  7522. * 7: Year
  7523. *------------------------------------------*/
  7524. BUILDIN_FUNC(gettime) /* Asgard Version */
  7525. {
  7526. int type;
  7527. time_t timer;
  7528. struct tm *t;
  7529. type=script_getnum(st,2);
  7530. time(&timer);
  7531. t=localtime(&timer);
  7532. switch(type){
  7533. case 1://Sec(0~59)
  7534. script_pushint(st,t->tm_sec);
  7535. break;
  7536. case 2://Min(0~59)
  7537. script_pushint(st,t->tm_min);
  7538. break;
  7539. case 3://Hour(0~23)
  7540. script_pushint(st,t->tm_hour);
  7541. break;
  7542. case 4://WeekDay(0~6)
  7543. script_pushint(st,t->tm_wday);
  7544. break;
  7545. case 5://MonthDay(01~31)
  7546. script_pushint(st,t->tm_mday);
  7547. break;
  7548. case 6://Month(01~12)
  7549. script_pushint(st,t->tm_mon+1);
  7550. break;
  7551. case 7://Year(20xx)
  7552. script_pushint(st,t->tm_year+1900);
  7553. break;
  7554. case 8://Year Day(01~366)
  7555. script_pushint(st,t->tm_yday+1);
  7556. break;
  7557. default://(format error)
  7558. script_pushint(st,-1);
  7559. break;
  7560. }
  7561. return 0;
  7562. }
  7563. /*==========================================
  7564. * GetTimeStr("TimeFMT", Length);
  7565. *------------------------------------------*/
  7566. BUILDIN_FUNC(gettimestr)
  7567. {
  7568. char *tmpstr;
  7569. const char *fmtstr;
  7570. int maxlen;
  7571. time_t now = time(NULL);
  7572. fmtstr=script_getstr(st,2);
  7573. maxlen=script_getnum(st,3);
  7574. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  7575. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7576. tmpstr[maxlen]='\0';
  7577. script_pushstr(st,tmpstr);
  7578. return 0;
  7579. }
  7580. /*==========================================
  7581. * Open player storage
  7582. *------------------------------------------*/
  7583. BUILDIN_FUNC(openstorage)
  7584. {
  7585. TBL_PC* sd;
  7586. sd = script_rid2sd(st);
  7587. if( sd == NULL )
  7588. return 0;
  7589. storage_storageopen(sd);
  7590. return 0;
  7591. }
  7592. BUILDIN_FUNC(guildopenstorage)
  7593. {
  7594. TBL_PC* sd;
  7595. int ret;
  7596. sd = script_rid2sd(st);
  7597. if( sd == NULL )
  7598. return 0;
  7599. ret = storage_guild_storageopen(sd);
  7600. script_pushint(st,ret);
  7601. return 0;
  7602. }
  7603. /*==========================================
  7604. * Make player use a skill trought item usage
  7605. *------------------------------------------*/
  7606. /// itemskill <skill id>,<level>
  7607. /// itemskill "<skill name>",<level>
  7608. BUILDIN_FUNC(itemskill)
  7609. {
  7610. int id;
  7611. int lv;
  7612. TBL_PC* sd;
  7613. sd = script_rid2sd(st);
  7614. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  7615. return 0;
  7616. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7617. lv = script_getnum(st,3);
  7618. sd->skillitem=id;
  7619. sd->skillitemlv=lv;
  7620. clif_item_skill(sd,id,lv);
  7621. return 0;
  7622. }
  7623. /*==========================================
  7624. * Attempt to create an item
  7625. *------------------------------------------*/
  7626. BUILDIN_FUNC(produce)
  7627. {
  7628. int trigger;
  7629. TBL_PC* sd;
  7630. sd = script_rid2sd(st);
  7631. if( sd == NULL )
  7632. return 0;
  7633. trigger=script_getnum(st,2);
  7634. clif_skill_produce_mix_list(sd, -1, trigger);
  7635. return 0;
  7636. }
  7637. /*==========================================
  7638. *
  7639. *------------------------------------------*/
  7640. BUILDIN_FUNC(cooking)
  7641. {
  7642. int trigger;
  7643. TBL_PC* sd;
  7644. sd = script_rid2sd(st);
  7645. if( sd == NULL )
  7646. return 0;
  7647. trigger=script_getnum(st,2);
  7648. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  7649. return 0;
  7650. }
  7651. /*==========================================
  7652. * Create a pet
  7653. *------------------------------------------*/
  7654. BUILDIN_FUNC(makepet)
  7655. {
  7656. TBL_PC* sd;
  7657. int id,pet_id;
  7658. id=script_getnum(st,2);
  7659. sd = script_rid2sd(st);
  7660. if( sd == NULL )
  7661. return 0;
  7662. pet_id = search_petDB_index(id, PET_CLASS);
  7663. if (pet_id < 0)
  7664. pet_id = search_petDB_index(id, PET_EGG);
  7665. if (pet_id >= 0 && sd) {
  7666. sd->catch_target_class = pet_db[pet_id].class_;
  7667. intif_create_pet(
  7668. sd->status.account_id, sd->status.char_id,
  7669. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7670. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7671. 100, 0, 1, pet_db[pet_id].jname);
  7672. }
  7673. return 0;
  7674. }
  7675. /*==========================================
  7676. * Give player exp base,job * quest_exp_rate/100
  7677. *------------------------------------------*/
  7678. BUILDIN_FUNC(getexp)
  7679. {
  7680. TBL_PC* sd;
  7681. int base=0,job=0;
  7682. double bonus;
  7683. sd = script_rid2sd(st);
  7684. if( sd == NULL )
  7685. return 0;
  7686. base=script_getnum(st,2);
  7687. job =script_getnum(st,3);
  7688. if(base<0 || job<0)
  7689. return 0;
  7690. // bonus for npc-given exp
  7691. bonus = battle_config.quest_exp_rate / 100.;
  7692. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7693. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7694. pc_gainexp(sd, NULL, base, job, true);
  7695. return 0;
  7696. }
  7697. /*==========================================
  7698. * Gain guild exp [Celest]
  7699. *------------------------------------------*/
  7700. BUILDIN_FUNC(guildgetexp)
  7701. {
  7702. TBL_PC* sd;
  7703. int exp;
  7704. sd = script_rid2sd(st);
  7705. if( sd == NULL )
  7706. return 0;
  7707. exp = script_getnum(st,2);
  7708. if(exp < 0)
  7709. return 0;
  7710. if(sd && sd->status.guild_id > 0)
  7711. guild_getexp (sd, exp);
  7712. return 0;
  7713. }
  7714. /*==========================================
  7715. * Changes the guild master of a guild [Skotlex]
  7716. *------------------------------------------*/
  7717. BUILDIN_FUNC(guildchangegm)
  7718. {
  7719. TBL_PC *sd;
  7720. int guild_id;
  7721. const char *name;
  7722. guild_id = script_getnum(st,2);
  7723. name = script_getstr(st,3);
  7724. sd=map_nick2sd(name);
  7725. if (!sd)
  7726. script_pushint(st,0);
  7727. else
  7728. script_pushint(st,guild_gm_change(guild_id, sd));
  7729. return 0;
  7730. }
  7731. /*==========================================
  7732. * Spawn a monster :
  7733. @mapn,x,y : location
  7734. @str : monster name
  7735. @class_ : mob_id
  7736. @amount : nb to spawn
  7737. @event : event to attach to mob
  7738. *------------------------------------------*/
  7739. BUILDIN_FUNC(monster)
  7740. {
  7741. const char* mapn = script_getstr(st,2);
  7742. int x = script_getnum(st,3);
  7743. int y = script_getnum(st,4);
  7744. const char* str = script_getstr(st,5);
  7745. int class_ = script_getnum(st,6);
  7746. int amount = script_getnum(st,7);
  7747. const char* event = "";
  7748. unsigned int size = SZ_SMALL;
  7749. unsigned int ai = AI_NONE;
  7750. struct map_session_data* sd;
  7751. int16 m;
  7752. if (script_hasdata(st, 8)) {
  7753. event = script_getstr(st, 8);
  7754. check_event(st, event);
  7755. }
  7756. if (script_hasdata(st, 9)) {
  7757. size = script_getnum(st, 9);
  7758. if (size > 3) {
  7759. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7760. return 1;
  7761. }
  7762. }
  7763. if (script_hasdata(st, 10)) {
  7764. ai = script_getnum(st, 10);
  7765. if (ai >= AI_MAX) {
  7766. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7767. return 1;
  7768. }
  7769. }
  7770. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7771. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7772. return 1;
  7773. }
  7774. sd = map_id2sd(st->rid);
  7775. if (sd && strcmp(mapn, "this") == 0)
  7776. m = sd->bl.m;
  7777. else
  7778. m = map_mapname2mapid(mapn);
  7779. mob_once_spawn(sd, m, x, y, str, class_, amount, event, size, ai);
  7780. return 0;
  7781. }
  7782. /*==========================================
  7783. * Request List of Monster Drops
  7784. *------------------------------------------*/
  7785. BUILDIN_FUNC(getmobdrops)
  7786. {
  7787. int class_ = script_getnum(st,2);
  7788. int i, j = 0;
  7789. struct mob_db *mob;
  7790. if( !mobdb_checkid(class_) )
  7791. {
  7792. script_pushint(st, 0);
  7793. return 0;
  7794. }
  7795. mob = mob_db(class_);
  7796. for( i = 0; i < MAX_MOB_DROP; i++ )
  7797. {
  7798. if( mob->dropitem[i].nameid < 1 )
  7799. continue;
  7800. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7801. continue;
  7802. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7803. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7804. j++;
  7805. }
  7806. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7807. script_pushint(st, 1);
  7808. return 0;
  7809. }
  7810. /*==========================================
  7811. * Same as monster but randomize location in x0,x1,y0,y1 area
  7812. *------------------------------------------*/
  7813. BUILDIN_FUNC(areamonster)
  7814. {
  7815. const char* mapn = script_getstr(st,2);
  7816. int x0 = script_getnum(st,3);
  7817. int y0 = script_getnum(st,4);
  7818. int x1 = script_getnum(st,5);
  7819. int y1 = script_getnum(st,6);
  7820. const char* str = script_getstr(st,7);
  7821. int class_ = script_getnum(st,8);
  7822. int amount = script_getnum(st,9);
  7823. const char* event = "";
  7824. unsigned int size = SZ_SMALL;
  7825. unsigned int ai = AI_NONE;
  7826. struct map_session_data* sd;
  7827. int16 m;
  7828. if (script_hasdata(st,10)) {
  7829. event = script_getstr(st, 10);
  7830. check_event(st, event);
  7831. }
  7832. if (script_hasdata(st, 11)) {
  7833. size = script_getnum(st, 11);
  7834. if (size > 3) {
  7835. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7836. return 1;
  7837. }
  7838. }
  7839. if (script_hasdata(st, 12)) {
  7840. ai = script_getnum(st, 12);
  7841. if (ai > 4) {
  7842. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7843. return 1;
  7844. }
  7845. }
  7846. sd = map_id2sd(st->rid);
  7847. if (sd && strcmp(mapn, "this") == 0)
  7848. m = sd->bl.m;
  7849. else
  7850. m = map_mapname2mapid(mapn);
  7851. mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai);
  7852. return 0;
  7853. }
  7854. /*==========================================
  7855. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  7856. *------------------------------------------*/
  7857. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7858. { //same fix but with killmonster instead - stripping events from mobs.
  7859. TBL_MOB* md = (TBL_MOB*)bl;
  7860. char *event=va_arg(ap,char *);
  7861. int allflag=va_arg(ap,int);
  7862. md->state.npc_killmonster = 1;
  7863. if(!allflag){
  7864. if(strcmp(event,md->npc_event)==0)
  7865. status_kill(bl);
  7866. }else{
  7867. if(!md->spawn)
  7868. status_kill(bl);
  7869. }
  7870. md->state.npc_killmonster = 0;
  7871. return 0;
  7872. }
  7873. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7874. {
  7875. TBL_MOB* md = (TBL_MOB*)bl;
  7876. char *event=va_arg(ap,char *);
  7877. int allflag=va_arg(ap,int);
  7878. if(!allflag){
  7879. if(strcmp(event,md->npc_event)==0)
  7880. status_kill(bl);
  7881. }else{
  7882. if(!md->spawn)
  7883. status_kill(bl);
  7884. }
  7885. return 0;
  7886. }
  7887. BUILDIN_FUNC(killmonster)
  7888. {
  7889. const char *mapname,*event;
  7890. int16 m,allflag=0;
  7891. mapname=script_getstr(st,2);
  7892. event=script_getstr(st,3);
  7893. if(strcmp(event,"All")==0)
  7894. allflag = 1;
  7895. else
  7896. check_event(st, event);
  7897. if( (m=map_mapname2mapid(mapname))<0 )
  7898. return 0;
  7899. if( script_hasdata(st,4) ) {
  7900. if ( script_getnum(st,4) == 1 ) {
  7901. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7902. return 0;
  7903. }
  7904. }
  7905. map_freeblock_lock();
  7906. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7907. map_freeblock_unlock();
  7908. return 0;
  7909. }
  7910. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7911. { //Strips the event from the mob if it's killed the old method.
  7912. struct mob_data *md;
  7913. md = BL_CAST(BL_MOB, bl);
  7914. if (md->npc_event[0])
  7915. md->npc_event[0] = 0;
  7916. status_kill(bl);
  7917. return 0;
  7918. }
  7919. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7920. {
  7921. status_kill(bl);
  7922. return 0;
  7923. }
  7924. BUILDIN_FUNC(killmonsterall)
  7925. {
  7926. const char *mapname;
  7927. int16 m;
  7928. mapname=script_getstr(st,2);
  7929. if( (m = map_mapname2mapid(mapname))<0 )
  7930. return 0;
  7931. if( script_hasdata(st,3) ) {
  7932. if ( script_getnum(st,3) == 1 ) {
  7933. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7934. return 0;
  7935. }
  7936. }
  7937. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7938. return 0;
  7939. }
  7940. /*==========================================
  7941. * Creates a clone of a player.
  7942. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7943. *------------------------------------------*/
  7944. BUILDIN_FUNC(clone)
  7945. {
  7946. TBL_PC *sd, *msd=NULL;
  7947. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7948. unsigned int duration = 0;
  7949. const char *map,*event="";
  7950. map=script_getstr(st,2);
  7951. x=script_getnum(st,3);
  7952. y=script_getnum(st,4);
  7953. event=script_getstr(st,5);
  7954. char_id=script_getnum(st,6);
  7955. if( script_hasdata(st,7) )
  7956. master_id=script_getnum(st,7);
  7957. if( script_hasdata(st,8) )
  7958. mode=script_getnum(st,8);
  7959. if( script_hasdata(st,9) )
  7960. flag=script_getnum(st,9);
  7961. if( script_hasdata(st,10) )
  7962. duration=script_getnum(st,10);
  7963. check_event(st, event);
  7964. m = map_mapname2mapid(map);
  7965. if (m < 0) return 0;
  7966. sd = map_charid2sd(char_id);
  7967. if (master_id) {
  7968. msd = map_charid2sd(master_id);
  7969. if (msd)
  7970. master_id = msd->bl.id;
  7971. else
  7972. master_id = 0;
  7973. }
  7974. if (sd) //Return ID of newly crafted clone.
  7975. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7976. else //Failed to create clone.
  7977. script_pushint(st,0);
  7978. return 0;
  7979. }
  7980. /*==========================================
  7981. *------------------------------------------*/
  7982. BUILDIN_FUNC(doevent)
  7983. {
  7984. const char* event = script_getstr(st,2);
  7985. struct map_session_data* sd;
  7986. if( ( sd = script_rid2sd(st) ) == NULL )
  7987. {
  7988. return 0;
  7989. }
  7990. check_event(st, event);
  7991. npc_event(sd, event, 0);
  7992. return 0;
  7993. }
  7994. /*==========================================
  7995. *------------------------------------------*/
  7996. BUILDIN_FUNC(donpcevent)
  7997. {
  7998. const char* event = script_getstr(st,2);
  7999. check_event(st, event);
  8000. if( !npc_event_do(event) ) {
  8001. struct npc_data * nd = map_id2nd(st->oid);
  8002. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8003. script_pushint(st, 0);
  8004. } else
  8005. script_pushint(st, 1);
  8006. return 0;
  8007. }
  8008. /// for Aegis compatibility
  8009. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8010. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8011. {
  8012. const char* npc = script_getstr(st,2);
  8013. const char* command = script_getstr(st,3);
  8014. char event[EVENT_NAME_LENGTH];
  8015. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8016. check_event(st, event);
  8017. npc_event_do(event);
  8018. return 0;
  8019. }
  8020. /*==========================================
  8021. *------------------------------------------*/
  8022. BUILDIN_FUNC(addtimer)
  8023. {
  8024. int tick = script_getnum(st,2);
  8025. const char* event = script_getstr(st, 3);
  8026. TBL_PC* sd;
  8027. check_event(st, event);
  8028. sd = script_rid2sd(st);
  8029. if( sd == NULL )
  8030. return 0;
  8031. pc_addeventtimer(sd,tick,event);
  8032. return 0;
  8033. }
  8034. /*==========================================
  8035. *------------------------------------------*/
  8036. BUILDIN_FUNC(deltimer)
  8037. {
  8038. const char *event;
  8039. TBL_PC* sd;
  8040. event=script_getstr(st, 2);
  8041. sd = script_rid2sd(st);
  8042. if( sd == NULL )
  8043. return 0;
  8044. check_event(st, event);
  8045. pc_deleventtimer(sd,event);
  8046. return 0;
  8047. }
  8048. /*==========================================
  8049. *------------------------------------------*/
  8050. BUILDIN_FUNC(addtimercount)
  8051. {
  8052. const char *event;
  8053. int tick;
  8054. TBL_PC* sd;
  8055. event=script_getstr(st, 2);
  8056. tick=script_getnum(st,3);
  8057. sd = script_rid2sd(st);
  8058. if( sd == NULL )
  8059. return 0;
  8060. check_event(st, event);
  8061. pc_addeventtimercount(sd,event,tick);
  8062. return 0;
  8063. }
  8064. /*==========================================
  8065. *------------------------------------------*/
  8066. BUILDIN_FUNC(initnpctimer)
  8067. {
  8068. struct npc_data *nd;
  8069. int flag = 0;
  8070. if( script_hasdata(st,3) )
  8071. { //Two arguments: NPC name and attach flag.
  8072. nd = npc_name2id(script_getstr(st, 2));
  8073. flag = script_getnum(st,3);
  8074. }
  8075. else if( script_hasdata(st,2) )
  8076. { //Check if argument is numeric (flag) or string (npc name)
  8077. struct script_data *data;
  8078. data = script_getdata(st,2);
  8079. get_val(st,data);
  8080. if( data_isstring(data) ) //NPC name
  8081. nd = npc_name2id(conv_str(st, data));
  8082. else if( data_isint(data) ) //Flag
  8083. {
  8084. nd = (struct npc_data *)map_id2bl(st->oid);
  8085. flag = conv_num(st,data);
  8086. }
  8087. else
  8088. {
  8089. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8090. return 1;
  8091. }
  8092. }
  8093. else
  8094. nd = (struct npc_data *)map_id2bl(st->oid);
  8095. if( !nd )
  8096. return 0;
  8097. if( flag ) //Attach
  8098. {
  8099. TBL_PC* sd = script_rid2sd(st);
  8100. if( sd == NULL )
  8101. return 0;
  8102. nd->u.scr.rid = sd->bl.id;
  8103. }
  8104. nd->u.scr.timertick = 0;
  8105. npc_settimerevent_tick(nd,0);
  8106. npc_timerevent_start(nd, st->rid);
  8107. return 0;
  8108. }
  8109. /*==========================================
  8110. *------------------------------------------*/
  8111. BUILDIN_FUNC(startnpctimer)
  8112. {
  8113. struct npc_data *nd;
  8114. int flag = 0;
  8115. if( script_hasdata(st,3) )
  8116. { //Two arguments: NPC name and attach flag.
  8117. nd = npc_name2id(script_getstr(st, 2));
  8118. flag = script_getnum(st,3);
  8119. }
  8120. else if( script_hasdata(st,2) )
  8121. { //Check if argument is numeric (flag) or string (npc name)
  8122. struct script_data *data;
  8123. data = script_getdata(st,2);
  8124. get_val(st,data);
  8125. if( data_isstring(data) ) //NPC name
  8126. nd = npc_name2id(conv_str(st, data));
  8127. else if( data_isint(data) ) //Flag
  8128. {
  8129. nd = (struct npc_data *)map_id2bl(st->oid);
  8130. flag = conv_num(st,data);
  8131. }
  8132. else
  8133. {
  8134. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8135. return 1;
  8136. }
  8137. }
  8138. else
  8139. nd=(struct npc_data *)map_id2bl(st->oid);
  8140. if( !nd )
  8141. return 0;
  8142. if( flag ) //Attach
  8143. {
  8144. TBL_PC* sd = script_rid2sd(st);
  8145. if( sd == NULL )
  8146. return 0;
  8147. nd->u.scr.rid = sd->bl.id;
  8148. }
  8149. npc_timerevent_start(nd, st->rid);
  8150. return 0;
  8151. }
  8152. /*==========================================
  8153. *------------------------------------------*/
  8154. BUILDIN_FUNC(stopnpctimer)
  8155. {
  8156. struct npc_data *nd;
  8157. int flag = 0;
  8158. if( script_hasdata(st,3) )
  8159. { //Two arguments: NPC name and attach flag.
  8160. nd = npc_name2id(script_getstr(st, 2));
  8161. flag = script_getnum(st,3);
  8162. }
  8163. else if( script_hasdata(st,2) )
  8164. { //Check if argument is numeric (flag) or string (npc name)
  8165. struct script_data *data;
  8166. data = script_getdata(st,2);
  8167. get_val(st,data);
  8168. if( data_isstring(data) ) //NPC name
  8169. nd = npc_name2id(conv_str(st, data));
  8170. else if( data_isint(data) ) //Flag
  8171. {
  8172. nd = (struct npc_data *)map_id2bl(st->oid);
  8173. flag = conv_num(st,data);
  8174. }
  8175. else
  8176. {
  8177. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8178. return 1;
  8179. }
  8180. }
  8181. else
  8182. nd=(struct npc_data *)map_id2bl(st->oid);
  8183. if( !nd )
  8184. return 0;
  8185. if( flag ) //Detach
  8186. nd->u.scr.rid = 0;
  8187. npc_timerevent_stop(nd);
  8188. return 0;
  8189. }
  8190. /*==========================================
  8191. *------------------------------------------*/
  8192. BUILDIN_FUNC(getnpctimer)
  8193. {
  8194. struct npc_data *nd;
  8195. TBL_PC *sd;
  8196. int type = script_getnum(st,2);
  8197. int val = 0;
  8198. if( script_hasdata(st,3) )
  8199. nd = npc_name2id(script_getstr(st,3));
  8200. else
  8201. nd = (struct npc_data *)map_id2bl(st->oid);
  8202. if( !nd || nd->bl.type != BL_NPC )
  8203. {
  8204. script_pushint(st,0);
  8205. ShowError("getnpctimer: Invalid NPC.\n");
  8206. return 1;
  8207. }
  8208. switch( type )
  8209. {
  8210. case 0: val = npc_gettimerevent_tick(nd); break;
  8211. case 1:
  8212. if( nd->u.scr.rid )
  8213. {
  8214. sd = map_id2sd(nd->u.scr.rid);
  8215. if( !sd )
  8216. {
  8217. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8218. break;
  8219. }
  8220. val = (sd->npc_timer_id != INVALID_TIMER);
  8221. }
  8222. else
  8223. val = (nd->u.scr.timerid != INVALID_TIMER);
  8224. break;
  8225. case 2: val = nd->u.scr.timeramount; break;
  8226. }
  8227. script_pushint(st,val);
  8228. return 0;
  8229. }
  8230. /*==========================================
  8231. *------------------------------------------*/
  8232. BUILDIN_FUNC(setnpctimer)
  8233. {
  8234. int tick;
  8235. struct npc_data *nd;
  8236. tick = script_getnum(st,2);
  8237. if( script_hasdata(st,3) )
  8238. nd = npc_name2id(script_getstr(st,3));
  8239. else
  8240. nd = (struct npc_data *)map_id2bl(st->oid);
  8241. if( !nd || nd->bl.type != BL_NPC )
  8242. {
  8243. script_pushint(st,1);
  8244. ShowError("setnpctimer: Invalid NPC.\n");
  8245. return 1;
  8246. }
  8247. npc_settimerevent_tick(nd,tick);
  8248. script_pushint(st,0);
  8249. return 0;
  8250. }
  8251. /*==========================================
  8252. * attaches the player rid to the timer [Celest]
  8253. *------------------------------------------*/
  8254. BUILDIN_FUNC(attachnpctimer)
  8255. {
  8256. TBL_PC *sd;
  8257. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8258. if( !nd || nd->bl.type != BL_NPC )
  8259. {
  8260. script_pushint(st,1);
  8261. ShowError("setnpctimer: Invalid NPC.\n");
  8262. return 1;
  8263. }
  8264. if( script_hasdata(st,2) )
  8265. sd = map_nick2sd(script_getstr(st,2));
  8266. else
  8267. sd = script_rid2sd(st);
  8268. if( !sd )
  8269. {
  8270. script_pushint(st,1);
  8271. ShowWarning("attachnpctimer: Invalid player.\n");
  8272. return 1;
  8273. }
  8274. nd->u.scr.rid = sd->bl.id;
  8275. script_pushint(st,0);
  8276. return 0;
  8277. }
  8278. /*==========================================
  8279. * detaches a player rid from the timer [Celest]
  8280. *------------------------------------------*/
  8281. BUILDIN_FUNC(detachnpctimer)
  8282. {
  8283. struct npc_data *nd;
  8284. if( script_hasdata(st,2) )
  8285. nd = npc_name2id(script_getstr(st,2));
  8286. else
  8287. nd = (struct npc_data *)map_id2bl(st->oid);
  8288. if( !nd || nd->bl.type != BL_NPC )
  8289. {
  8290. script_pushint(st,1);
  8291. ShowError("detachnpctimer: Invalid NPC.\n");
  8292. return 1;
  8293. }
  8294. nd->u.scr.rid = 0;
  8295. script_pushint(st,0);
  8296. return 0;
  8297. }
  8298. /*==========================================
  8299. * To avoid "player not attached" script errors, this function is provided,
  8300. * it checks if there is a player attached to the current script. [Skotlex]
  8301. * If no, returns 0, if yes, returns the account_id of the attached player.
  8302. *------------------------------------------*/
  8303. BUILDIN_FUNC(playerattached)
  8304. {
  8305. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8306. script_pushint(st,0);
  8307. else
  8308. script_pushint(st,st->rid);
  8309. return 0;
  8310. }
  8311. /*==========================================
  8312. *------------------------------------------*/
  8313. BUILDIN_FUNC(announce)
  8314. {
  8315. const char *mes = script_getstr(st,2);
  8316. int flag = script_getnum(st,3);
  8317. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8318. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8319. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8320. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8321. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8322. if (flag&0x0f) // Broadcast source or broadcast region defined
  8323. {
  8324. send_target target;
  8325. 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
  8326. if (bl == NULL)
  8327. return 0;
  8328. flag &= 0x07;
  8329. target = (flag == 1) ? ALL_SAMEMAP :
  8330. (flag == 2) ? AREA :
  8331. (flag == 3) ? SELF :
  8332. ALL_CLIENT;
  8333. if (fontColor)
  8334. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8335. else
  8336. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  8337. }
  8338. else
  8339. {
  8340. if (fontColor)
  8341. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8342. else
  8343. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  8344. }
  8345. return 0;
  8346. }
  8347. /*==========================================
  8348. *------------------------------------------*/
  8349. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8350. {
  8351. char *mes = va_arg(ap, char *);
  8352. int len = va_arg(ap, int);
  8353. int type = va_arg(ap, int);
  8354. char *fontColor = va_arg(ap, char *);
  8355. short fontType = (short)va_arg(ap, int);
  8356. short fontSize = (short)va_arg(ap, int);
  8357. short fontAlign = (short)va_arg(ap, int);
  8358. short fontY = (short)va_arg(ap, int);
  8359. if (fontColor)
  8360. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8361. else
  8362. clif_broadcast(bl, mes, len, type, SELF);
  8363. return 0;
  8364. }
  8365. BUILDIN_FUNC(mapannounce)
  8366. {
  8367. const char *mapname = script_getstr(st,2);
  8368. const char *mes = script_getstr(st,3);
  8369. int flag = script_getnum(st,4);
  8370. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8371. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8372. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8373. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8374. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8375. int16 m;
  8376. if ((m = map_mapname2mapid(mapname)) < 0)
  8377. return 0;
  8378. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8379. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8380. return 0;
  8381. }
  8382. /*==========================================
  8383. *------------------------------------------*/
  8384. BUILDIN_FUNC(areaannounce)
  8385. {
  8386. const char *mapname = script_getstr(st,2);
  8387. int x0 = script_getnum(st,3);
  8388. int y0 = script_getnum(st,4);
  8389. int x1 = script_getnum(st,5);
  8390. int y1 = script_getnum(st,6);
  8391. const char *mes = script_getstr(st,7);
  8392. int flag = script_getnum(st,8);
  8393. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  8394. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  8395. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  8396. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  8397. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  8398. int16 m;
  8399. if ((m = map_mapname2mapid(mapname)) < 0)
  8400. return 0;
  8401. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8402. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8403. return 0;
  8404. }
  8405. /*==========================================
  8406. *------------------------------------------*/
  8407. BUILDIN_FUNC(getusers)
  8408. {
  8409. int flag, val = 0;
  8410. struct map_session_data* sd;
  8411. struct block_list* bl = NULL;
  8412. flag = script_getnum(st,2);
  8413. switch(flag&0x07)
  8414. {
  8415. case 0:
  8416. if(flag&0x8)
  8417. {// npc
  8418. bl = map_id2bl(st->oid);
  8419. }
  8420. else if((sd = script_rid2sd(st))!=NULL)
  8421. {// pc
  8422. bl = &sd->bl;
  8423. }
  8424. if(bl)
  8425. {
  8426. val = map[bl->m].users;
  8427. }
  8428. break;
  8429. case 1:
  8430. val = map_getusers();
  8431. break;
  8432. default:
  8433. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8434. script_pushint(st,0);
  8435. return 1;
  8436. }
  8437. script_pushint(st,val);
  8438. return 0;
  8439. }
  8440. /*==========================================
  8441. * Works like @WHO - displays all online users names in window
  8442. *------------------------------------------*/
  8443. BUILDIN_FUNC(getusersname)
  8444. {
  8445. TBL_PC *sd, *pl_sd;
  8446. int /*disp_num=1,*/ group_level = 0;
  8447. struct s_mapiterator* iter;
  8448. sd = script_rid2sd(st);
  8449. if (!sd) return 0;
  8450. group_level = pc_get_group_level(sd);
  8451. iter = mapit_getallusers();
  8452. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  8453. {
  8454. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  8455. continue; // skip hidden sessions
  8456. /* Temporary fix for bugreport:1023.
  8457. * Do not uncomment unless you want thousands of 'next' buttons.
  8458. if((disp_num++)%10==0)
  8459. clif_scriptnext(sd,st->oid);*/
  8460. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  8461. }
  8462. mapit_free(iter);
  8463. return 0;
  8464. }
  8465. /*==========================================
  8466. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  8467. *------------------------------------------*/
  8468. BUILDIN_FUNC(getmapguildusers)
  8469. {
  8470. const char *str;
  8471. int16 m;
  8472. int gid;
  8473. int i=0,c=0;
  8474. struct guild *g = NULL;
  8475. str=script_getstr(st,2);
  8476. gid=script_getnum(st,3);
  8477. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  8478. script_pushint(st,-1);
  8479. return 0;
  8480. }
  8481. g = guild_search(gid);
  8482. if (g){
  8483. for(i = 0; i < g->max_member; i++)
  8484. {
  8485. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  8486. c++;
  8487. }
  8488. }
  8489. script_pushint(st,c);
  8490. return 0;
  8491. }
  8492. /*==========================================
  8493. *------------------------------------------*/
  8494. BUILDIN_FUNC(getmapusers)
  8495. {
  8496. const char *str;
  8497. int16 m;
  8498. str=script_getstr(st,2);
  8499. if( (m=map_mapname2mapid(str))< 0){
  8500. script_pushint(st,-1);
  8501. return 0;
  8502. }
  8503. script_pushint(st,map[m].users);
  8504. return 0;
  8505. }
  8506. /*==========================================
  8507. *------------------------------------------*/
  8508. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  8509. {
  8510. int *users=va_arg(ap,int *);
  8511. (*users)++;
  8512. return 0;
  8513. }
  8514. BUILDIN_FUNC(getareausers)
  8515. {
  8516. const char *str;
  8517. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  8518. str=script_getstr(st,2);
  8519. x0=script_getnum(st,3);
  8520. y0=script_getnum(st,4);
  8521. x1=script_getnum(st,5);
  8522. y1=script_getnum(st,6);
  8523. if( (m=map_mapname2mapid(str))< 0){
  8524. script_pushint(st,-1);
  8525. return 0;
  8526. }
  8527. map_foreachinarea(buildin_getareausers_sub,
  8528. m,x0,y0,x1,y1,BL_PC,&users);
  8529. script_pushint(st,users);
  8530. return 0;
  8531. }
  8532. /*==========================================
  8533. *------------------------------------------*/
  8534. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  8535. {
  8536. int item=va_arg(ap,int);
  8537. int *amount=va_arg(ap,int *);
  8538. struct flooritem_data *drop=(struct flooritem_data *)bl;
  8539. if(drop->item_data.nameid==item)
  8540. (*amount)+=drop->item_data.amount;
  8541. return 0;
  8542. }
  8543. BUILDIN_FUNC(getareadropitem)
  8544. {
  8545. const char *str;
  8546. int16 m,x0,y0,x1,y1;
  8547. int item,amount=0;
  8548. struct script_data *data;
  8549. str=script_getstr(st,2);
  8550. x0=script_getnum(st,3);
  8551. y0=script_getnum(st,4);
  8552. x1=script_getnum(st,5);
  8553. y1=script_getnum(st,6);
  8554. data=script_getdata(st,7);
  8555. get_val(st,data);
  8556. if( data_isstring(data) ){
  8557. const char *name=conv_str(st,data);
  8558. struct item_data *item_data = itemdb_searchname(name);
  8559. item=UNKNOWN_ITEM_ID;
  8560. if( item_data )
  8561. item=item_data->nameid;
  8562. }else
  8563. item=conv_num(st,data);
  8564. if( (m=map_mapname2mapid(str))< 0){
  8565. script_pushint(st,-1);
  8566. return 0;
  8567. }
  8568. map_foreachinarea(buildin_getareadropitem_sub,
  8569. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  8570. script_pushint(st,amount);
  8571. return 0;
  8572. }
  8573. /*==========================================
  8574. *------------------------------------------*/
  8575. BUILDIN_FUNC(enablenpc)
  8576. {
  8577. const char *str;
  8578. str=script_getstr(st,2);
  8579. npc_enable(str,1);
  8580. return 0;
  8581. }
  8582. /*==========================================
  8583. *------------------------------------------*/
  8584. BUILDIN_FUNC(disablenpc)
  8585. {
  8586. const char *str;
  8587. str=script_getstr(st,2);
  8588. npc_enable(str,0);
  8589. return 0;
  8590. }
  8591. /*==========================================
  8592. *------------------------------------------*/
  8593. BUILDIN_FUNC(hideoffnpc)
  8594. {
  8595. const char *str;
  8596. str=script_getstr(st,2);
  8597. npc_enable(str,2);
  8598. return 0;
  8599. }
  8600. /*==========================================
  8601. *------------------------------------------*/
  8602. BUILDIN_FUNC(hideonnpc)
  8603. {
  8604. const char *str;
  8605. str=script_getstr(st,2);
  8606. npc_enable(str,4);
  8607. return 0;
  8608. }
  8609. /* Starts a status effect on the target unit or on the attached player.
  8610. *
  8611. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  8612. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  8613. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  8614. * <flag>
  8615. * &1: Cannot be avoided (it has to start)
  8616. * &2: Tick should not be reduced (by vit, luk, lv, etc)
  8617. * &4: sc_data loaded, no value has to be altered.
  8618. * &8: rate should not be reduced
  8619. */
  8620. BUILDIN_FUNC(sc_start)
  8621. {
  8622. TBL_NPC * nd = map_id2nd(st->oid);
  8623. struct block_list* bl;
  8624. enum sc_type type;
  8625. int tick, val1, val2, val3, val4=0, rate, flag;
  8626. char start_type;
  8627. const char* command = script_getfuncname(st);
  8628. if(strstr(command, "4"))
  8629. start_type = 4;
  8630. else if(strstr(command, "2"))
  8631. start_type = 2;
  8632. else
  8633. start_type = 1;
  8634. type = (sc_type)script_getnum(st,2);
  8635. tick = script_getnum(st,3);
  8636. val1 = script_getnum(st,4);
  8637. //If from NPC we make default flag 1 to be unavoidable
  8638. if(nd && nd->bl.id == fake_nd->bl.id)
  8639. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):2;
  8640. else
  8641. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):1;
  8642. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  8643. if(script_hasdata(st,(6+start_type)))
  8644. bl = map_id2bl(script_getnum(st,(6+start_type)));
  8645. else
  8646. bl = map_id2bl(st->rid);
  8647. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  8648. {// When there isn't a duration specified, try to get it from the skill_db
  8649. tick = skill_get_time(status_sc2skill(type), val1);
  8650. }
  8651. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  8652. bl = map_id2bl(potion_target);
  8653. tick /= 2;// Thrown potions only last half.
  8654. val4 = 1;// Mark that this was a thrown sc_effect
  8655. }
  8656. if(!bl)
  8657. return 0;
  8658. switch(start_type) {
  8659. case 1:
  8660. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  8661. break;
  8662. case 2:
  8663. val2 = script_getnum(st,5);
  8664. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  8665. break;
  8666. case 4:
  8667. val2 = script_getnum(st,5);
  8668. val3 = script_getnum(st,6);
  8669. val4 = script_getnum(st,7);
  8670. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  8671. break;
  8672. }
  8673. return 0;
  8674. }
  8675. /// Ends one or all status effects on the target unit or on the attached player.
  8676. ///
  8677. /// sc_end <effect_id>{,<unit_id>};
  8678. BUILDIN_FUNC(sc_end)
  8679. {
  8680. struct block_list* bl;
  8681. int type;
  8682. type = script_getnum(st, 2);
  8683. if (script_hasdata(st, 3))
  8684. bl = map_id2bl(script_getnum(st, 3));
  8685. else
  8686. bl = map_id2bl(st->rid);
  8687. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  8688. bl = map_id2bl(potion_target);
  8689. if (!bl)
  8690. return 0;
  8691. if (type >= 0 && type < SC_MAX)
  8692. {
  8693. struct status_change *sc = status_get_sc(bl);
  8694. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  8695. if (!sce)
  8696. return 0;
  8697. switch (type)
  8698. {
  8699. case SC_WEIGHT50:
  8700. case SC_WEIGHT90:
  8701. case SC_NOCHAT:
  8702. case SC_PUSH_CART:
  8703. return 0;
  8704. default:
  8705. break;
  8706. }
  8707. //This should help status_change_end force disabling the SC in case it has no limit.
  8708. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8709. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8710. }
  8711. else
  8712. status_change_clear(bl, 3); // remove all effects
  8713. return 0;
  8714. }
  8715. /*==========================================
  8716. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  8717. *------------------------------------------*/
  8718. BUILDIN_FUNC(getscrate)
  8719. {
  8720. struct block_list *bl;
  8721. int type,rate;
  8722. type=script_getnum(st,2);
  8723. rate=script_getnum(st,3);
  8724. if( script_hasdata(st,4) ) //get for the bl assigned
  8725. bl = map_id2bl(script_getnum(st,4));
  8726. else
  8727. bl = map_id2bl(st->rid);
  8728. if (bl)
  8729. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, 0);
  8730. script_pushint(st,rate);
  8731. return 0;
  8732. }
  8733. /*==========================================
  8734. * getstatus <type>{, <info>};
  8735. *------------------------------------------*/
  8736. BUILDIN_FUNC(getstatus)
  8737. {
  8738. int id, type;
  8739. struct map_session_data* sd = script_rid2sd(st);
  8740. if( sd == NULL )
  8741. {// no player attached
  8742. return 0;
  8743. }
  8744. id = script_getnum(st, 2);
  8745. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8746. if( id <= SC_NONE || id >= SC_MAX )
  8747. {// invalid status type given
  8748. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8749. return 0;
  8750. }
  8751. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8752. {// no status is active
  8753. script_pushint(st, 0);
  8754. return 0;
  8755. }
  8756. switch( type )
  8757. {
  8758. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8759. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8760. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8761. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8762. case 5:
  8763. {
  8764. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8765. if( timer )
  8766. {// return the amount of time remaining
  8767. script_pushint(st, timer->tick - gettick());
  8768. }
  8769. }
  8770. break;
  8771. default: script_pushint(st, 1); break;
  8772. }
  8773. return 0;
  8774. }
  8775. /*==========================================
  8776. *
  8777. *------------------------------------------*/
  8778. BUILDIN_FUNC(debugmes)
  8779. {
  8780. const char *str;
  8781. str=script_getstr(st,2);
  8782. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8783. return 0;
  8784. }
  8785. /*==========================================
  8786. *------------------------------------------*/
  8787. BUILDIN_FUNC(catchpet)
  8788. {
  8789. int pet_id;
  8790. TBL_PC *sd;
  8791. pet_id= script_getnum(st,2);
  8792. sd=script_rid2sd(st);
  8793. if( sd == NULL )
  8794. return 0;
  8795. pet_catch_process1(sd,pet_id);
  8796. return 0;
  8797. }
  8798. /*==========================================
  8799. * [orn]
  8800. *------------------------------------------*/
  8801. BUILDIN_FUNC(homunculus_evolution)
  8802. {
  8803. TBL_PC *sd;
  8804. sd=script_rid2sd(st);
  8805. if( sd == NULL )
  8806. return 0;
  8807. if(merc_is_hom_active(sd->hd))
  8808. {
  8809. if (sd->hd->homunculus.intimacy > 91000)
  8810. merc_hom_evolution(sd->hd);
  8811. else
  8812. clif_emotion(&sd->hd->bl, E_SWT);
  8813. }
  8814. return 0;
  8815. }
  8816. /*==========================================
  8817. * Checks for vaporized morph state
  8818. * and deletes ITEMID_STRANGE_EMBRYO.
  8819. *------------------------------------------*/
  8820. BUILDIN_FUNC(homunculus_mutate)
  8821. {
  8822. int homun_id, m_class, m_id, i;
  8823. TBL_PC *sd;
  8824. sd = script_rid2sd(st);
  8825. if( sd == NULL || sd->hd == NULL )
  8826. return 0;
  8827. if(script_hasdata(st,2))
  8828. homun_id = script_getnum(st,2);
  8829. else
  8830. homun_id = 6048 + (rnd() % 4);
  8831. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  8832. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8833. m_id = hom_class2mapid(homun_id);
  8834. i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  8835. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  8836. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  8837. merc_call_homunculus(sd); // Respawn homunculus.
  8838. hom_mutate(sd->hd, homun_id);
  8839. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  8840. script_pushint(st, 1);
  8841. return 0;
  8842. } else
  8843. clif_emotion(&sd->bl, E_SWT);
  8844. } else
  8845. clif_emotion(&sd->bl, E_SWT);
  8846. script_pushint(st, 0);
  8847. return 0;
  8848. }
  8849. /*==========================================
  8850. * Puts homunculus into morph state
  8851. * and gives ITEMID_STRANGE_EMBRYO.
  8852. *------------------------------------------*/
  8853. BUILDIN_FUNC(morphembryo)
  8854. {
  8855. struct item item_tmp;
  8856. int m_class, i=0;
  8857. TBL_PC *sd;
  8858. sd = script_rid2sd(st);
  8859. if( sd == NULL || sd->hd == NULL )
  8860. return 0;
  8861. if( merc_is_hom_active(sd->hd) ) {
  8862. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8863. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  8864. memset(&item_tmp, 0, sizeof(item_tmp));
  8865. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  8866. item_tmp.identify = 1;
  8867. if( item_tmp.nameid == 0 || (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  8868. clif_additem(sd, 0, 0, i);
  8869. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  8870. } else {
  8871. merc_hom_vaporize(sd, HOM_ST_MORPH);
  8872. script_pushint(st, 1);
  8873. return 0;
  8874. }
  8875. } else
  8876. clif_emotion(&sd->hd->bl, E_SWT);
  8877. } else
  8878. clif_emotion(&sd->bl, E_SWT);
  8879. script_pushint(st, 0);
  8880. return 0;
  8881. }
  8882. // [Zephyrus]
  8883. BUILDIN_FUNC(homunculus_shuffle)
  8884. {
  8885. TBL_PC *sd;
  8886. sd=script_rid2sd(st);
  8887. if( sd == NULL )
  8888. return 0;
  8889. if(merc_is_hom_active(sd->hd))
  8890. merc_hom_shuffle(sd->hd);
  8891. return 0;
  8892. }
  8893. /*==========================================
  8894. * Check for homunculus state.
  8895. * Return: -1 = No homunculus
  8896. * 0 = Homunculus is active
  8897. * 1 = Homunculus is vaporized (rest)
  8898. * 2 = Homunculus is in morph state
  8899. *------------------------------------------*/
  8900. BUILDIN_FUNC(checkhomcall)
  8901. {
  8902. TBL_PC *sd = script_rid2sd(st);
  8903. TBL_HOM *hd;
  8904. if( sd == NULL )
  8905. return 0;
  8906. hd = sd->hd;
  8907. if( !hd )
  8908. script_pushint(st, -1);
  8909. else
  8910. script_pushint(st, hd->homunculus.vaporize);
  8911. return 0;
  8912. }
  8913. //These two functions bring the eA MAPID_* class functionality to scripts.
  8914. BUILDIN_FUNC(eaclass)
  8915. {
  8916. int class_;
  8917. if( script_hasdata(st,2) )
  8918. class_ = script_getnum(st,2);
  8919. else {
  8920. TBL_PC *sd;
  8921. sd=script_rid2sd(st);
  8922. if (!sd) {
  8923. script_pushint(st,-1);
  8924. return 0;
  8925. }
  8926. class_ = sd->status.class_;
  8927. }
  8928. script_pushint(st,pc_jobid2mapid(class_));
  8929. return 0;
  8930. }
  8931. BUILDIN_FUNC(roclass)
  8932. {
  8933. int class_ =script_getnum(st,2);
  8934. int sex;
  8935. if( script_hasdata(st,3) )
  8936. sex = script_getnum(st,3);
  8937. else {
  8938. TBL_PC *sd;
  8939. if (st->rid && (sd=script_rid2sd(st)))
  8940. sex = sd->status.sex;
  8941. else
  8942. sex = 1; //Just use male when not found.
  8943. }
  8944. script_pushint(st,pc_mapid2jobid(class_, sex));
  8945. return 0;
  8946. }
  8947. /*==========================================
  8948. * Tells client to open a hatching window, used for pet incubator
  8949. *------------------------------------------*/
  8950. BUILDIN_FUNC(birthpet)
  8951. {
  8952. TBL_PC *sd;
  8953. sd=script_rid2sd(st);
  8954. if( sd == NULL )
  8955. return 0;
  8956. if( sd->status.pet_id )
  8957. {// do not send egg list, when you already have a pet
  8958. return 0;
  8959. }
  8960. clif_sendegg(sd);
  8961. return 0;
  8962. }
  8963. /*==========================================
  8964. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  8965. * @type
  8966. * 1 : make like after rebirth
  8967. * 2 : blvl,jlvl=1, skillpoint=0
  8968. * 3 : don't reset skill, blvl=1
  8969. * 4 : jlvl=0
  8970. *------------------------------------------*/
  8971. BUILDIN_FUNC(resetlvl)
  8972. {
  8973. TBL_PC *sd;
  8974. int type=script_getnum(st,2);
  8975. sd=script_rid2sd(st);
  8976. if( sd == NULL )
  8977. return 0;
  8978. pc_resetlvl(sd,type);
  8979. return 0;
  8980. }
  8981. /*==========================================
  8982. * Reset a player status point
  8983. *------------------------------------------*/
  8984. BUILDIN_FUNC(resetstatus)
  8985. {
  8986. TBL_PC *sd;
  8987. sd=script_rid2sd(st);
  8988. pc_resetstate(sd);
  8989. return 0;
  8990. }
  8991. /*==========================================
  8992. * script command resetskill
  8993. *------------------------------------------*/
  8994. BUILDIN_FUNC(resetskill)
  8995. {
  8996. TBL_PC *sd;
  8997. sd=script_rid2sd(st);
  8998. pc_resetskill(sd,1);
  8999. return 0;
  9000. }
  9001. /*==========================================
  9002. * Counts total amount of skill points.
  9003. *------------------------------------------*/
  9004. BUILDIN_FUNC(skillpointcount)
  9005. {
  9006. TBL_PC *sd;
  9007. sd=script_rid2sd(st);
  9008. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9009. return 0;
  9010. }
  9011. /*==========================================
  9012. *
  9013. *------------------------------------------*/
  9014. BUILDIN_FUNC(changebase)
  9015. {
  9016. TBL_PC *sd=NULL;
  9017. int vclass;
  9018. if( script_hasdata(st,3) )
  9019. sd=map_id2sd(script_getnum(st,3));
  9020. else
  9021. sd=script_rid2sd(st);
  9022. if(sd == NULL)
  9023. return 0;
  9024. vclass = script_getnum(st,2);
  9025. if(vclass == JOB_WEDDING)
  9026. {
  9027. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9028. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9029. )
  9030. return 0;
  9031. }
  9032. if(!sd->disguise && vclass != sd->vd.class_) {
  9033. status_set_viewdata(&sd->bl, vclass);
  9034. //Updated client view. Base, Weapon and Cloth Colors.
  9035. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9036. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9037. if (sd->vd.cloth_color)
  9038. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9039. clif_skillinfoblock(sd);
  9040. }
  9041. return 0;
  9042. }
  9043. /*==========================================
  9044. * Unequip all item and request for a changesex to char-serv
  9045. *------------------------------------------*/
  9046. BUILDIN_FUNC(changesex)
  9047. {
  9048. int i;
  9049. TBL_PC *sd = NULL;
  9050. sd = script_rid2sd(st);
  9051. pc_resetskill(sd,4);
  9052. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9053. for( i=0; i<EQI_MAX; i++ )
  9054. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  9055. chrif_changesex(sd);
  9056. return 0;
  9057. }
  9058. /*==========================================
  9059. * Works like 'announce' but outputs in the common chat window
  9060. *------------------------------------------*/
  9061. BUILDIN_FUNC(globalmes)
  9062. {
  9063. struct block_list *bl = map_id2bl(st->oid);
  9064. struct npc_data *nd = (struct npc_data *)bl;
  9065. const char *name=NULL,*mes;
  9066. mes=script_getstr(st,2);
  9067. if(mes==NULL) return 0;
  9068. if(script_hasdata(st,3)){ // npc name to display
  9069. name=script_getstr(st,3);
  9070. } else {
  9071. name=nd->name; //use current npc name
  9072. }
  9073. npc_globalmessage(name,mes); // broadcast to all players connected
  9074. return 0;
  9075. }
  9076. /////////////////////////////////////////////////////////////////////
  9077. // NPC waiting room (chat room)
  9078. //
  9079. /// Creates a waiting room (chat room) for this npc.
  9080. ///
  9081. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9082. BUILDIN_FUNC(waitingroom)
  9083. {
  9084. struct npc_data* nd;
  9085. int pub = 1;
  9086. const char* title = script_getstr(st, 2);
  9087. int limit = script_getnum(st, 3);
  9088. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9089. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9090. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9091. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9092. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9093. nd = (struct npc_data *)map_id2bl(st->oid);
  9094. if( nd != NULL )
  9095. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  9096. return 0;
  9097. }
  9098. /// Removes the waiting room of the current or target npc.
  9099. ///
  9100. /// delwaitingroom "<npc_name>";
  9101. /// delwaitingroom;
  9102. BUILDIN_FUNC(delwaitingroom)
  9103. {
  9104. struct npc_data* nd;
  9105. if( script_hasdata(st,2) )
  9106. nd = npc_name2id(script_getstr(st, 2));
  9107. else
  9108. nd = (struct npc_data *)map_id2bl(st->oid);
  9109. if( nd != NULL )
  9110. chat_deletenpcchat(nd);
  9111. return 0;
  9112. }
  9113. /// Kicks all the players from the waiting room of the current or target npc.
  9114. ///
  9115. /// kickwaitingroomall "<npc_name>";
  9116. /// kickwaitingroomall;
  9117. BUILDIN_FUNC(waitingroomkickall)
  9118. {
  9119. struct npc_data* nd;
  9120. struct chat_data* cd;
  9121. if( script_hasdata(st,2) )
  9122. nd = npc_name2id(script_getstr(st,2));
  9123. else
  9124. nd = (struct npc_data *)map_id2bl(st->oid);
  9125. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9126. chat_npckickall(cd);
  9127. return 0;
  9128. }
  9129. /// Enables the waiting room event of the current or target npc.
  9130. ///
  9131. /// enablewaitingroomevent "<npc_name>";
  9132. /// enablewaitingroomevent;
  9133. BUILDIN_FUNC(enablewaitingroomevent)
  9134. {
  9135. struct npc_data* nd;
  9136. struct chat_data* cd;
  9137. if( script_hasdata(st,2) )
  9138. nd = npc_name2id(script_getstr(st, 2));
  9139. else
  9140. nd = (struct npc_data *)map_id2bl(st->oid);
  9141. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9142. chat_enableevent(cd);
  9143. return 0;
  9144. }
  9145. /// Disables the waiting room event of the current or target npc.
  9146. ///
  9147. /// disablewaitingroomevent "<npc_name>";
  9148. /// disablewaitingroomevent;
  9149. BUILDIN_FUNC(disablewaitingroomevent)
  9150. {
  9151. struct npc_data *nd;
  9152. struct chat_data *cd;
  9153. if( script_hasdata(st,2) )
  9154. nd = npc_name2id(script_getstr(st, 2));
  9155. else
  9156. nd = (struct npc_data *)map_id2bl(st->oid);
  9157. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9158. chat_disableevent(cd);
  9159. return 0;
  9160. }
  9161. /// Returns info on the waiting room of the current or target npc.
  9162. /// Returns -1 if the type unknown
  9163. /// <type>=0 : current number of users
  9164. /// <type>=1 : maximum number of users allowed
  9165. /// <type>=2 : the number of users that trigger the event
  9166. /// <type>=3 : if the trigger is disabled
  9167. /// <type>=4 : the title of the waiting room
  9168. /// <type>=5 : the password of the waiting room
  9169. /// <type>=16 : the name of the waiting room event
  9170. /// <type>=32 : if the waiting room is full
  9171. /// <type>=33 : if there are enough users to trigger the event
  9172. ///
  9173. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9174. /// getwaitingroomstate(<type>) -> <info>
  9175. BUILDIN_FUNC(getwaitingroomstate)
  9176. {
  9177. struct npc_data *nd;
  9178. struct chat_data *cd;
  9179. int type;
  9180. type = script_getnum(st,2);
  9181. if( script_hasdata(st,3) )
  9182. nd = npc_name2id(script_getstr(st, 3));
  9183. else
  9184. nd = (struct npc_data *)map_id2bl(st->oid);
  9185. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9186. {
  9187. script_pushint(st, -1);
  9188. return 0;
  9189. }
  9190. switch(type)
  9191. {
  9192. case 0: script_pushint(st, cd->users); break;
  9193. case 1: script_pushint(st, cd->limit); break;
  9194. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9195. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9196. case 4: script_pushstrcopy(st, cd->title); break;
  9197. case 5: script_pushstrcopy(st, cd->pass); break;
  9198. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9199. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9200. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9201. default: script_pushint(st, -1); break;
  9202. }
  9203. return 0;
  9204. }
  9205. /// Warps the trigger or target amount of players to the target map and position.
  9206. /// Players are automatically removed from the waiting room.
  9207. /// Those waiting the longest will get warped first.
  9208. /// The target map can be "Random" for a random position in the current map,
  9209. /// and "SavePoint" for the savepoint map+position.
  9210. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9211. ///
  9212. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9213. /// The total number of teleported players is put into $@warpwaitingpcnum
  9214. ///
  9215. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9216. /// warpwaitingpc "<map name>",<x>,<y>;
  9217. BUILDIN_FUNC(warpwaitingpc)
  9218. {
  9219. int x;
  9220. int y;
  9221. int i;
  9222. int n;
  9223. const char* map_name;
  9224. struct npc_data* nd;
  9225. struct chat_data* cd;
  9226. TBL_PC* sd;
  9227. nd = (struct npc_data *)map_id2bl(st->oid);
  9228. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9229. return 0;
  9230. map_name = script_getstr(st,2);
  9231. x = script_getnum(st,3);
  9232. y = script_getnum(st,4);
  9233. n = cd->trigger&0x7f;
  9234. if( script_hasdata(st,5) )
  9235. n = script_getnum(st,5);
  9236. for( i = 0; i < n && cd->users > 0; i++ )
  9237. {
  9238. sd = cd->usersd[0];
  9239. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9240. {// can't teleport on this map
  9241. break;
  9242. }
  9243. if( cd->zeny )
  9244. {// fee set
  9245. if( (uint32)sd->status.zeny < cd->zeny )
  9246. {// no zeny to cover set fee
  9247. break;
  9248. }
  9249. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  9250. }
  9251. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9252. if( strcmp(map_name,"Random") == 0 )
  9253. pc_randomwarp(sd,CLR_TELEPORT);
  9254. else if( strcmp(map_name,"SavePoint") == 0 )
  9255. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9256. else
  9257. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9258. }
  9259. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9260. return 0;
  9261. }
  9262. /////////////////////////////////////////////////////////////////////
  9263. // ...
  9264. //
  9265. /// Detaches a character from a script.
  9266. ///
  9267. /// @param st Script state to detach the character from.
  9268. static void script_detach_rid(struct script_state* st)
  9269. {
  9270. if(st->rid)
  9271. {
  9272. script_detach_state(st, false);
  9273. st->rid = 0;
  9274. }
  9275. }
  9276. /*=========================================================================
  9277. * Attaches a set of RIDs to the current script. [digitalhamster]
  9278. * addrid(<type>{,<flag>{,<parameters>}});
  9279. * <type>:
  9280. * 0 : All players in the server.
  9281. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  9282. * 2 : Party members of a specified party ID.
  9283. * [ Parameters: <party id> ]
  9284. * 3 : Guild members of a specified guild ID.
  9285. * [ Parameters: <guild id> ]
  9286. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  9287. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  9288. * Account ID: The specified account ID.
  9289. * <flag>:
  9290. * 0 : Players are always attached. (default)
  9291. * 1 : Players currently running another script will not be attached.
  9292. *-------------------------------------------------------------------------*/
  9293. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  9294. {
  9295. int forceflag;
  9296. struct map_session_data *sd = (TBL_PC *)bl;
  9297. struct script_state* st;
  9298. st=va_arg(ap,struct script_state*);
  9299. forceflag=va_arg(ap,int);
  9300. if(!forceflag||!sd->st)
  9301. if(sd->status.account_id!=st->rid)
  9302. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9303. return 0;
  9304. }
  9305. BUILDIN_FUNC(addrid)
  9306. {
  9307. struct s_mapiterator* iter;
  9308. struct block_list *bl;
  9309. TBL_PC *sd;
  9310. if(st->rid<1){
  9311. st->state = END;
  9312. bl=map_id2bl(st->oid);
  9313. } else
  9314. bl=map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  9315. iter = mapit_getallusers();
  9316. switch(script_getnum(st,2)){
  9317. case 0:
  9318. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9319. if(!script_getnum(st,3)||!sd->st)
  9320. if(sd->status.account_id!=st->rid) //attached player already runs.
  9321. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9322. }
  9323. break;
  9324. case 1:
  9325. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9326. if(!script_getnum(st,3)||!sd->st)
  9327. if((sd->bl.m == bl->m)&&(sd->status.account_id!=st->rid))
  9328. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9329. }
  9330. break;
  9331. case 2:
  9332. if(script_getnum(st,4)==0){
  9333. script_pushint(st,0);
  9334. return 0;
  9335. }
  9336. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9337. if(!script_getnum(st,3)||!sd->st)
  9338. if((sd->status.account_id!=st->rid)&&(sd->status.party_id==script_getnum(st,4))) //attached player already runs.
  9339. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9340. }
  9341. break;
  9342. case 3:
  9343. if(script_getnum(st,4)==0){
  9344. script_pushint(st,0);
  9345. return 0;
  9346. }
  9347. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)){
  9348. if(!script_getnum(st,3)||!sd->st)
  9349. if((sd->status.account_id!=st->rid)&&(sd->status.guild_id==script_getnum(st,4))) //attached player already runs.
  9350. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9351. }
  9352. break;
  9353. case 4:
  9354. map_foreachinarea(buildin_addrid_sub,
  9355. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  9356. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  9357. break;
  9358. default:
  9359. if((map_id2sd(script_getnum(st,2)))==NULL){ // Player not found.
  9360. script_pushint(st,0);
  9361. return 0;
  9362. }
  9363. if(!script_getnum(st,3)||!map_id2sd(script_getnum(st,2))->st) {
  9364. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  9365. script_pushint(st,1);
  9366. }
  9367. return 0;
  9368. }
  9369. mapit_free(iter);
  9370. script_pushint(st,1);
  9371. return 0;
  9372. }
  9373. /*==========================================
  9374. * Attach sd char id to script and detach current one if any
  9375. *------------------------------------------*/
  9376. BUILDIN_FUNC(attachrid)
  9377. {
  9378. int rid = script_getnum(st,2);
  9379. if (map_id2sd(rid) != NULL) {
  9380. script_detach_rid(st);
  9381. st->rid = rid;
  9382. script_attach_state(st);
  9383. script_pushint(st,1);
  9384. } else
  9385. script_pushint(st,0);
  9386. return 0;
  9387. }
  9388. /*==========================================
  9389. * Detach script to rid
  9390. *------------------------------------------*/
  9391. BUILDIN_FUNC(detachrid)
  9392. {
  9393. script_detach_rid(st);
  9394. return 0;
  9395. }
  9396. /*==========================================
  9397. * Chk if account connected, (and charid from account if specified)
  9398. *------------------------------------------*/
  9399. BUILDIN_FUNC(isloggedin)
  9400. {
  9401. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  9402. if (script_hasdata(st,3) && sd &&
  9403. sd->status.char_id != script_getnum(st,3))
  9404. sd = NULL;
  9405. push_val(st->stack,C_INT,sd!=NULL);
  9406. return 0;
  9407. }
  9408. /*==========================================
  9409. *
  9410. *------------------------------------------*/
  9411. BUILDIN_FUNC(setmapflagnosave)
  9412. {
  9413. int16 m,x,y;
  9414. unsigned short mapindex;
  9415. const char *str,*str2;
  9416. str=script_getstr(st,2);
  9417. str2=script_getstr(st,3);
  9418. x=script_getnum(st,4);
  9419. y=script_getnum(st,5);
  9420. m = map_mapname2mapid(str);
  9421. mapindex = mapindex_name2id(str2);
  9422. if(m >= 0 && mapindex) {
  9423. map[m].flag.nosave=1;
  9424. map[m].save.map=mapindex;
  9425. map[m].save.x=x;
  9426. map[m].save.y=y;
  9427. }
  9428. return 0;
  9429. }
  9430. BUILDIN_FUNC(getmapflag)
  9431. {
  9432. int16 m,i;
  9433. const char *str;
  9434. str=script_getstr(st,2);
  9435. i=script_getnum(st,3);
  9436. m = map_mapname2mapid(str);
  9437. if(m >= 0) {
  9438. switch(i) {
  9439. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  9440. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  9441. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  9442. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  9443. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  9444. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  9445. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  9446. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  9447. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  9448. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  9449. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  9450. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  9451. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  9452. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  9453. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  9454. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  9455. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  9456. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  9457. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  9458. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  9459. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  9460. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  9461. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  9462. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  9463. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  9464. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  9465. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  9466. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  9467. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  9468. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  9469. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  9470. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  9471. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  9472. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  9473. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  9474. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  9475. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  9476. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  9477. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  9478. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  9479. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  9480. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  9481. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  9482. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  9483. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  9484. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  9485. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  9486. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  9487. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  9488. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  9489. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  9490. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  9491. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  9492. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  9493. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  9494. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  9495. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  9496. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  9497. }
  9498. }
  9499. return 0;
  9500. }
  9501. /* pvp timer handling */
  9502. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  9503. TBL_PC* sd = (TBL_PC*)bl;
  9504. if (sd->pvp_timer == INVALID_TIMER) {
  9505. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  9506. sd->pvp_rank = 0;
  9507. sd->pvp_lastusers = 0;
  9508. sd->pvp_point = 5;
  9509. sd->pvp_won = 0;
  9510. sd->pvp_lost = 0;
  9511. }
  9512. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  9513. clif_maptypeproperty2(&sd->bl,SELF);
  9514. return 0;
  9515. }
  9516. BUILDIN_FUNC(setmapflag)
  9517. {
  9518. int16 m,i;
  9519. const char *str;
  9520. int val=0;
  9521. str=script_getstr(st,2);
  9522. i=script_getnum(st,3);
  9523. if(script_hasdata(st,4)){
  9524. val=script_getnum(st,4);
  9525. }
  9526. m = map_mapname2mapid(str);
  9527. if(m >= 0) {
  9528. switch(i) {
  9529. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  9530. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  9531. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  9532. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  9533. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  9534. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  9535. case MF_PVP:
  9536. map[m].flag.pvp = 1;
  9537. if( !battle_config.pk_mode ) {
  9538. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  9539. }
  9540. break;
  9541. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  9542. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  9543. case MF_GVG: {
  9544. struct block_list bl;
  9545. map[m].flag.gvg = 1;
  9546. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9547. bl.type = BL_NUL;
  9548. bl.m = m;
  9549. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9550. }
  9551. break;
  9552. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  9553. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  9554. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  9555. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  9556. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  9557. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  9558. case MF_SNOW: map[m].flag.snow = 1; break;
  9559. case MF_FOG: map[m].flag.fog = 1; break;
  9560. case MF_SAKURA: map[m].flag.sakura = 1; break;
  9561. case MF_LEAVES: map[m].flag.leaves = 1; break;
  9562. case MF_NOGO: map[m].flag.nogo = 1; break;
  9563. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  9564. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  9565. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  9566. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  9567. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  9568. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  9569. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  9570. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  9571. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  9572. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  9573. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  9574. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  9575. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  9576. case MF_RESTRICTED:
  9577. map[m].zone |= 1<<(val+1);
  9578. map[m].flag.restricted=1;
  9579. break;
  9580. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  9581. case MF_NODROP: map[m].flag.nodrop = 1; break;
  9582. case MF_JEXP: map[m].jexp = (val <= 0) ? 100 : val; break;
  9583. case MF_BEXP: map[m].bexp = (val <= 0) ? 100 : val; break;
  9584. case MF_NOVENDING: map[m].flag.novending = 1; break;
  9585. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  9586. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  9587. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  9588. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  9589. case MF_TOWN: map[m].flag.town = 1; break;
  9590. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  9591. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  9592. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  9593. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  9594. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  9595. case MF_RESET: map[m].flag.reset = 1; break;
  9596. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  9597. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  9598. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  9599. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  9600. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  9601. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  9602. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  9603. }
  9604. }
  9605. return 0;
  9606. }
  9607. BUILDIN_FUNC(removemapflag)
  9608. {
  9609. int16 m,i;
  9610. const char *str;
  9611. int val=0;
  9612. str=script_getstr(st,2);
  9613. i=script_getnum(st,3);
  9614. if(script_hasdata(st,4)){
  9615. val=script_getnum(st,4);
  9616. }
  9617. m = map_mapname2mapid(str);
  9618. if(m >= 0) {
  9619. switch(i) {
  9620. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  9621. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  9622. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  9623. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  9624. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  9625. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  9626. case MF_PVP: {
  9627. struct block_list bl;
  9628. bl.type = BL_NUL;
  9629. bl.m = m;
  9630. map[m].flag.pvp = 0;
  9631. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9632. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9633. }
  9634. break;
  9635. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  9636. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  9637. case MF_GVG: {
  9638. struct block_list bl;
  9639. bl.type = BL_NUL;
  9640. bl.m = m;
  9641. map[m].flag.gvg = 0;
  9642. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9643. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9644. }
  9645. break;
  9646. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  9647. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  9648. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  9649. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  9650. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  9651. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  9652. case MF_SNOW: map[m].flag.snow = 0; break;
  9653. case MF_FOG: map[m].flag.fog = 0; break;
  9654. case MF_SAKURA: map[m].flag.sakura = 0; break;
  9655. case MF_LEAVES: map[m].flag.leaves = 0; break;
  9656. case MF_NOGO: map[m].flag.nogo = 0; break;
  9657. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  9658. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  9659. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  9660. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  9661. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  9662. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  9663. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  9664. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  9665. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  9666. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  9667. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  9668. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  9669. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  9670. case MF_RESTRICTED:
  9671. map[m].zone ^= 1<<(val+1);
  9672. if (map[m].zone == 0){
  9673. map[m].flag.restricted=0;
  9674. }
  9675. break;
  9676. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  9677. case MF_NODROP: map[m].flag.nodrop = 0; break;
  9678. case MF_JEXP: map[m].jexp = 0; break;
  9679. case MF_BEXP: map[m].bexp = 0; break;
  9680. case MF_NOVENDING: map[m].flag.novending = 0; break;
  9681. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  9682. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  9683. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  9684. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  9685. case MF_TOWN: map[m].flag.town = 0; break;
  9686. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  9687. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  9688. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  9689. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  9690. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  9691. case MF_RESET: map[m].flag.reset = 0; break;
  9692. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  9693. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  9694. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  9695. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  9696. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  9697. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  9698. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  9699. }
  9700. }
  9701. return 0;
  9702. }
  9703. BUILDIN_FUNC(pvpon)
  9704. {
  9705. int16 m;
  9706. const char *str;
  9707. TBL_PC* sd = NULL;
  9708. struct s_mapiterator* iter;
  9709. struct block_list bl;
  9710. str = script_getstr(st,2);
  9711. m = map_mapname2mapid(str);
  9712. if( m < 0 || map[m].flag.pvp )
  9713. return 0; // nothing to do
  9714. map[m].flag.pvp = 1;
  9715. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  9716. bl.type = BL_NUL;
  9717. bl.m = m;
  9718. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9719. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  9720. return 0;
  9721. iter = mapit_getallusers();
  9722. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9723. {
  9724. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  9725. continue; // not applicable
  9726. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  9727. sd->pvp_rank = 0;
  9728. sd->pvp_lastusers = 0;
  9729. sd->pvp_point = 5;
  9730. sd->pvp_won = 0;
  9731. sd->pvp_lost = 0;
  9732. }
  9733. mapit_free(iter);
  9734. return 0;
  9735. }
  9736. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  9737. {
  9738. TBL_PC* sd = (TBL_PC*)bl;
  9739. clif_pvpset(sd, 0, 0, 2);
  9740. if (sd->pvp_timer != INVALID_TIMER) {
  9741. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  9742. sd->pvp_timer = INVALID_TIMER;
  9743. }
  9744. return 0;
  9745. }
  9746. BUILDIN_FUNC(pvpoff)
  9747. {
  9748. int16 m;
  9749. const char *str;
  9750. struct block_list bl;
  9751. str=script_getstr(st,2);
  9752. m = map_mapname2mapid(str);
  9753. if(m < 0 || !map[m].flag.pvp)
  9754. return 0; //fixed Lupus
  9755. map[m].flag.pvp = 0;
  9756. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9757. bl.type = BL_NUL;
  9758. bl.m = m;
  9759. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9760. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  9761. return 0;
  9762. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  9763. return 0;
  9764. }
  9765. BUILDIN_FUNC(gvgon)
  9766. {
  9767. int16 m;
  9768. const char *str;
  9769. struct block_list bl;
  9770. str=script_getstr(st,2);
  9771. m = map_mapname2mapid(str);
  9772. if(m >= 0 && !map[m].flag.gvg) {
  9773. map[m].flag.gvg = 1;
  9774. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9775. bl.type = BL_NUL;
  9776. bl.m = m;
  9777. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9778. }
  9779. return 0;
  9780. }
  9781. BUILDIN_FUNC(gvgoff)
  9782. {
  9783. int16 m;
  9784. const char *str;
  9785. str=script_getstr(st,2);
  9786. m = map_mapname2mapid(str);
  9787. if(m >= 0 && map[m].flag.gvg) {
  9788. struct block_list bl;
  9789. map[m].flag.gvg = 0;
  9790. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9791. bl.type = BL_NUL;
  9792. bl.m = m;
  9793. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  9794. }
  9795. return 0;
  9796. }
  9797. /*==========================================
  9798. * Shows an emoticon on top of the player/npc
  9799. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  9800. *------------------------------------------*/
  9801. //Optional second parameter added by [Skotlex]
  9802. BUILDIN_FUNC(emotion)
  9803. {
  9804. int type;
  9805. int player=0;
  9806. type=script_getnum(st,2);
  9807. if(type < 0 || type > 100)
  9808. return 0;
  9809. if( script_hasdata(st,3) )
  9810. player=script_getnum(st,3);
  9811. if (player) {
  9812. TBL_PC *sd = NULL;
  9813. if( script_hasdata(st,4) )
  9814. sd = map_nick2sd(script_getstr(st,4));
  9815. else
  9816. sd = script_rid2sd(st);
  9817. if (sd)
  9818. clif_emotion(&sd->bl,type);
  9819. } else
  9820. if( script_hasdata(st,4) )
  9821. {
  9822. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9823. if(nd)
  9824. clif_emotion(&nd->bl,type);
  9825. }
  9826. else
  9827. clif_emotion(map_id2bl(st->oid),type);
  9828. return 0;
  9829. }
  9830. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  9831. {
  9832. int16 m=va_arg(ap,int);
  9833. int g_id=va_arg(ap,int);
  9834. int flag=va_arg(ap,int);
  9835. if(!sd || sd->bl.m != m)
  9836. return 0;
  9837. if(
  9838. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  9839. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  9840. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  9841. )
  9842. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  9843. return 1;
  9844. }
  9845. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  9846. {
  9847. struct mob_data *md=(struct mob_data *)bl;
  9848. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  9849. status_kill(bl);
  9850. return 0;
  9851. }
  9852. /*
  9853. * Function to kick guild members out of a map and to their save points.
  9854. * m : mapid
  9855. * g_id : owner guild id
  9856. * flag & 1 : Warp out owners
  9857. * flag & 2 : Warp out outsiders
  9858. * flag & 4 : reserved for mobs
  9859. * */
  9860. BUILDIN_FUNC(maprespawnguildid)
  9861. {
  9862. const char *mapname=script_getstr(st,2);
  9863. int g_id=script_getnum(st,3);
  9864. int flag=script_getnum(st,4);
  9865. int16 m=map_mapname2mapid(mapname);
  9866. if(m == -1)
  9867. return 0;
  9868. //Catch ALL players (in case some are 'between maps' on execution time)
  9869. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  9870. if (flag&4) //Remove script mobs.
  9871. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  9872. return 0;
  9873. }
  9874. BUILDIN_FUNC(agitstart)
  9875. {
  9876. if(agit_flag==1) return 0; // Agit already Start.
  9877. agit_flag=1;
  9878. guild_agit_start();
  9879. return 0;
  9880. }
  9881. BUILDIN_FUNC(agitend)
  9882. {
  9883. if(agit_flag==0) return 0; // Agit already End.
  9884. agit_flag=0;
  9885. guild_agit_end();
  9886. return 0;
  9887. }
  9888. BUILDIN_FUNC(agitstart2)
  9889. {
  9890. if(agit2_flag==1) return 0; // Agit2 already Start.
  9891. agit2_flag=1;
  9892. guild_agit2_start();
  9893. return 0;
  9894. }
  9895. BUILDIN_FUNC(agitend2)
  9896. {
  9897. if(agit2_flag==0) return 0; // Agit2 already End.
  9898. agit2_flag=0;
  9899. guild_agit2_end();
  9900. return 0;
  9901. }
  9902. /*==========================================
  9903. * Returns whether woe is on or off. // choice script
  9904. *------------------------------------------*/
  9905. BUILDIN_FUNC(agitcheck)
  9906. {
  9907. script_pushint(st,agit_flag);
  9908. return 0;
  9909. }
  9910. /*==========================================
  9911. * Returns whether woese is on or off. // choice script
  9912. *------------------------------------------*/
  9913. BUILDIN_FUNC(agitcheck2)
  9914. {
  9915. script_pushint(st,agit2_flag);
  9916. return 0;
  9917. }
  9918. /// Sets the guild_id of this npc.
  9919. ///
  9920. /// flagemblem <guild_id>;
  9921. BUILDIN_FUNC(flagemblem)
  9922. {
  9923. TBL_NPC* nd;
  9924. int g_id = script_getnum(st,2);
  9925. if(g_id < 0) return 0;
  9926. nd = (TBL_NPC*)map_id2nd(st->oid);
  9927. if( nd == NULL ) {
  9928. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  9929. } else if( nd->subtype != SCRIPT ) {
  9930. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  9931. } else {
  9932. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  9933. nd->u.scr.guild_id = g_id;
  9934. clif_guild_emblem_area(&nd->bl);
  9935. /* guild flag caching */
  9936. if( g_id ) /* adding a id */
  9937. guild_flag_add(nd);
  9938. else if( changed ) /* removing a flag */
  9939. guild_flag_remove(nd);
  9940. }
  9941. return 0;
  9942. }
  9943. BUILDIN_FUNC(getcastlename)
  9944. {
  9945. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9946. struct guild_castle* gc = guild_mapname2gc(mapname);
  9947. const char* name = (gc) ? gc->castle_name : "";
  9948. script_pushstrcopy(st,name);
  9949. return 0;
  9950. }
  9951. BUILDIN_FUNC(getcastledata)
  9952. {
  9953. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9954. int index = script_getnum(st,3);
  9955. struct guild_castle *gc = guild_mapname2gc(mapname);
  9956. if (gc == NULL) {
  9957. script_pushint(st,0);
  9958. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9959. return 1;
  9960. }
  9961. switch (index) {
  9962. case 1:
  9963. script_pushint(st,gc->guild_id); break;
  9964. case 2:
  9965. script_pushint(st,gc->economy); break;
  9966. case 3:
  9967. script_pushint(st,gc->defense); break;
  9968. case 4:
  9969. script_pushint(st,gc->triggerE); break;
  9970. case 5:
  9971. script_pushint(st,gc->triggerD); break;
  9972. case 6:
  9973. script_pushint(st,gc->nextTime); break;
  9974. case 7:
  9975. script_pushint(st,gc->payTime); break;
  9976. case 8:
  9977. script_pushint(st,gc->createTime); break;
  9978. case 9:
  9979. script_pushint(st,gc->visibleC); break;
  9980. default:
  9981. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  9982. script_pushint(st,gc->guardian[index-10].visible);
  9983. break;
  9984. }
  9985. script_pushint(st,0);
  9986. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9987. return 1;
  9988. }
  9989. return 0;
  9990. }
  9991. BUILDIN_FUNC(setcastledata)
  9992. {
  9993. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9994. int index = script_getnum(st,3);
  9995. int value = script_getnum(st,4);
  9996. struct guild_castle *gc = guild_mapname2gc(mapname);
  9997. if (gc == NULL) {
  9998. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9999. return 1;
  10000. }
  10001. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10002. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10003. return 1;
  10004. }
  10005. guild_castledatasave(gc->castle_id, index, value);
  10006. return 0;
  10007. }
  10008. /* =====================================================================
  10009. * ---------------------------------------------------------------------*/
  10010. BUILDIN_FUNC(requestguildinfo)
  10011. {
  10012. int guild_id=script_getnum(st,2);
  10013. const char *event=NULL;
  10014. if( script_hasdata(st,3) ){
  10015. event=script_getstr(st,3);
  10016. check_event(st, event);
  10017. }
  10018. if(guild_id>0)
  10019. guild_npc_request_info(guild_id,event);
  10020. return 0;
  10021. }
  10022. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10023. /// getequipcardcnt(<equipment slot>);
  10024. BUILDIN_FUNC(getequipcardcnt)
  10025. {
  10026. int i=-1,j,num;
  10027. TBL_PC *sd;
  10028. int count;
  10029. num=script_getnum(st,2);
  10030. sd=script_rid2sd(st);
  10031. if (num > 0 && num <= ARRAYLENGTH(equip))
  10032. i=pc_checkequip(sd,equip[num-1]);
  10033. if (i < 0 || !sd->inventory_data[i]) {
  10034. script_pushint(st,0);
  10035. return 0;
  10036. }
  10037. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10038. {
  10039. script_pushint(st,0);
  10040. return 0;
  10041. }
  10042. count = 0;
  10043. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10044. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10045. count++;
  10046. script_pushint(st,count);
  10047. return 0;
  10048. }
  10049. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10050. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10051. /// successremovecards <slot>;
  10052. BUILDIN_FUNC(successremovecards) {
  10053. int i=-1,j,c,cardflag=0;
  10054. TBL_PC* sd = script_rid2sd(st);
  10055. int num = script_getnum(st,2);
  10056. if (num > 0 && num <= ARRAYLENGTH(equip))
  10057. i=pc_checkequip(sd,equip[num-1]);
  10058. if (i < 0 || !sd->inventory_data[i]) {
  10059. return 0;
  10060. }
  10061. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10062. return 0;
  10063. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10064. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10065. int flag;
  10066. struct item item_tmp;
  10067. memset(&item_tmp,0,sizeof(item_tmp));
  10068. cardflag = 1;
  10069. item_tmp.nameid = sd->status.inventory[i].card[c];
  10070. item_tmp.identify = 1;
  10071. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10072. clif_additem(sd,0,0,flag);
  10073. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10074. }
  10075. }
  10076. }
  10077. if(cardflag == 1) {//if card was remove remplace item with no card
  10078. int flag;
  10079. struct item item_tmp;
  10080. memset(&item_tmp,0,sizeof(item_tmp));
  10081. item_tmp.nameid = sd->status.inventory[i].nameid;
  10082. item_tmp.identify = 1;
  10083. item_tmp.refine = sd->status.inventory[i].refine;
  10084. item_tmp.attribute = sd->status.inventory[i].attribute;
  10085. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10086. item_tmp.bound = sd->status.inventory[i].bound;
  10087. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10088. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10089. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10090. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10091. clif_additem(sd,0,0,flag);
  10092. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10093. }
  10094. clif_misceffect(&sd->bl,3);
  10095. }
  10096. return 0;
  10097. }
  10098. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10099. /// failedremovecards <slot>, <type>;
  10100. /// <type>=0 : will destroy both the item and the cards.
  10101. /// <type>=1 : will keep the item, but destroy the cards.
  10102. /// <type>=2 : will keep the cards, but destroy the item.
  10103. /// <type>=? : will just display the failure effect.
  10104. BUILDIN_FUNC(failedremovecards) {
  10105. int i=-1,j,c,cardflag=0;
  10106. TBL_PC* sd = script_rid2sd(st);
  10107. int num = script_getnum(st,2);
  10108. int typefail = script_getnum(st,3);
  10109. if (num > 0 && num <= ARRAYLENGTH(equip))
  10110. i=pc_checkequip(sd,equip[num-1]);
  10111. if (i < 0 || !sd->inventory_data[i])
  10112. return 0;
  10113. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10114. return 0;
  10115. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10116. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10117. cardflag = 1;
  10118. if(typefail == 2) {// add cards to inventory, clear
  10119. int flag;
  10120. struct item item_tmp;
  10121. memset(&item_tmp,0,sizeof(item_tmp));
  10122. item_tmp.nameid = sd->status.inventory[i].card[c];
  10123. item_tmp.identify = 1;
  10124. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10125. clif_additem(sd,0,0,flag);
  10126. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10127. }
  10128. }
  10129. }
  10130. }
  10131. if(cardflag == 1) {
  10132. if(typefail == 0 || typefail == 2){ // destroy the item
  10133. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10134. }
  10135. if(typefail == 1){ // destroy the card
  10136. int flag;
  10137. struct item item_tmp;
  10138. memset(&item_tmp,0,sizeof(item_tmp));
  10139. item_tmp.nameid = sd->status.inventory[i].nameid;
  10140. item_tmp.identify = 1;
  10141. item_tmp.refine = sd->status.inventory[i].refine;
  10142. item_tmp.attribute = sd->status.inventory[i].attribute;
  10143. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10144. item_tmp.bound = sd->status.inventory[i].bound;
  10145. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10146. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10147. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10148. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10149. clif_additem(sd,0,0,flag);
  10150. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10151. }
  10152. }
  10153. clif_misceffect(&sd->bl,2);
  10154. }
  10155. return 0;
  10156. }
  10157. /* ================================================================
  10158. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  10159. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  10160. * improved by [Lance]
  10161. * ================================================================*/
  10162. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  10163. {
  10164. int x,y,m,check_val=0,check_ID=0,i=0;
  10165. struct guild *g = NULL;
  10166. struct party_data *p = NULL;
  10167. const char *str;
  10168. const char *mapname;
  10169. unsigned int index;
  10170. mapname=script_getstr(st,2);
  10171. str=script_getstr(st,3);
  10172. x=script_getnum(st,4);
  10173. y=script_getnum(st,5);
  10174. if(script_hasdata(st,7)){
  10175. check_val=script_getnum(st,6);
  10176. check_ID=script_getnum(st,7);
  10177. }
  10178. if((m=map_mapname2mapid(mapname))< 0)
  10179. return 0;
  10180. if(!(index=mapindex_name2id(str)))
  10181. return 0;
  10182. switch(check_val){
  10183. case 1:
  10184. g = guild_search(check_ID);
  10185. if (g){
  10186. for( i=0; i < g->max_member; i++)
  10187. {
  10188. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  10189. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  10190. }
  10191. }
  10192. }
  10193. break;
  10194. case 2:
  10195. p = party_search(check_ID);
  10196. if(p){
  10197. for(i=0;i<MAX_PARTY; i++){
  10198. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  10199. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  10200. }
  10201. }
  10202. }
  10203. break;
  10204. default:
  10205. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  10206. break;
  10207. }
  10208. return 0;
  10209. }
  10210. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  10211. {
  10212. char *event=va_arg(ap,char *);
  10213. struct mob_data *md = ((struct mob_data *)bl);
  10214. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  10215. return 1;
  10216. return 0;
  10217. }
  10218. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  10219. {
  10220. const char *mapname,*event;
  10221. int16 m;
  10222. mapname=script_getstr(st,2);
  10223. event=script_getstr(st,3);
  10224. if( strcmp(event, "all") == 0 )
  10225. event = NULL;
  10226. else
  10227. check_event(st, event);
  10228. if( strcmp(mapname, "this") == 0 ) {
  10229. struct map_session_data *sd = script_rid2sd(st);
  10230. if( sd )
  10231. m = sd->bl.m;
  10232. else {
  10233. script_pushint(st,-1);
  10234. return 0;
  10235. }
  10236. }
  10237. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  10238. script_pushint(st,-1);
  10239. return 0;
  10240. }
  10241. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  10242. return 0;
  10243. }
  10244. BUILDIN_FUNC(marriage)
  10245. {
  10246. const char *partner=script_getstr(st,2);
  10247. TBL_PC *sd=script_rid2sd(st);
  10248. TBL_PC *p_sd=map_nick2sd(partner);
  10249. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  10250. script_pushint(st,0);
  10251. return 0;
  10252. }
  10253. script_pushint(st,1);
  10254. return 0;
  10255. }
  10256. BUILDIN_FUNC(wedding_effect)
  10257. {
  10258. TBL_PC *sd=script_rid2sd(st);
  10259. struct block_list *bl;
  10260. if(sd==NULL) {
  10261. bl=map_id2bl(st->oid);
  10262. } else
  10263. bl=&sd->bl;
  10264. clif_wedding_effect(bl);
  10265. return 0;
  10266. }
  10267. BUILDIN_FUNC(divorce)
  10268. {
  10269. TBL_PC *sd=script_rid2sd(st);
  10270. if(sd==NULL || pc_divorce(sd) < 0){
  10271. script_pushint(st,0);
  10272. return 0;
  10273. }
  10274. script_pushint(st,1);
  10275. return 0;
  10276. }
  10277. BUILDIN_FUNC(ispartneron)
  10278. {
  10279. TBL_PC *sd=script_rid2sd(st);
  10280. if(sd==NULL || !pc_ismarried(sd) ||
  10281. map_charid2sd(sd->status.partner_id) == NULL) {
  10282. script_pushint(st,0);
  10283. return 0;
  10284. }
  10285. script_pushint(st,1);
  10286. return 0;
  10287. }
  10288. BUILDIN_FUNC(getpartnerid)
  10289. {
  10290. TBL_PC *sd=script_rid2sd(st);
  10291. if (sd == NULL) {
  10292. script_pushint(st,0);
  10293. return 0;
  10294. }
  10295. script_pushint(st,sd->status.partner_id);
  10296. return 0;
  10297. }
  10298. BUILDIN_FUNC(getchildid)
  10299. {
  10300. TBL_PC *sd=script_rid2sd(st);
  10301. if (sd == NULL) {
  10302. script_pushint(st,0);
  10303. return 0;
  10304. }
  10305. script_pushint(st,sd->status.child);
  10306. return 0;
  10307. }
  10308. BUILDIN_FUNC(getmotherid)
  10309. {
  10310. TBL_PC *sd=script_rid2sd(st);
  10311. if (sd == NULL) {
  10312. script_pushint(st,0);
  10313. return 0;
  10314. }
  10315. script_pushint(st,sd->status.mother);
  10316. return 0;
  10317. }
  10318. BUILDIN_FUNC(getfatherid)
  10319. {
  10320. TBL_PC *sd=script_rid2sd(st);
  10321. if (sd == NULL) {
  10322. script_pushint(st,0);
  10323. return 0;
  10324. }
  10325. script_pushint(st,sd->status.father);
  10326. return 0;
  10327. }
  10328. BUILDIN_FUNC(warppartner)
  10329. {
  10330. int x,y;
  10331. unsigned short mapindex;
  10332. const char *str;
  10333. TBL_PC *sd=script_rid2sd(st);
  10334. TBL_PC *p_sd=NULL;
  10335. if(sd==NULL || !pc_ismarried(sd) ||
  10336. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  10337. script_pushint(st,0);
  10338. return 0;
  10339. }
  10340. str=script_getstr(st,2);
  10341. x=script_getnum(st,3);
  10342. y=script_getnum(st,4);
  10343. mapindex = mapindex_name2id(str);
  10344. if (mapindex) {
  10345. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  10346. script_pushint(st,1);
  10347. } else
  10348. script_pushint(st,0);
  10349. return 0;
  10350. }
  10351. /*================================================
  10352. * Script for Displaying MOB Information [Valaris]
  10353. *------------------------------------------------*/
  10354. BUILDIN_FUNC(strmobinfo)
  10355. {
  10356. int num=script_getnum(st,2);
  10357. int class_=script_getnum(st,3);
  10358. if(!mobdb_checkid(class_))
  10359. {
  10360. if (num < 3) //requested a string
  10361. script_pushconststr(st,"");
  10362. else
  10363. script_pushint(st,0);
  10364. return 0;
  10365. }
  10366. switch (num) {
  10367. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  10368. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  10369. case 3: script_pushint(st,mob_db(class_)->lv); break;
  10370. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  10371. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  10372. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  10373. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  10374. default:
  10375. script_pushint(st,0);
  10376. break;
  10377. }
  10378. return 0;
  10379. }
  10380. /*==========================================
  10381. * Summon guardians [Valaris]
  10382. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  10383. *------------------------------------------*/
  10384. BUILDIN_FUNC(guardian)
  10385. {
  10386. int class_=0,x=0,y=0,guardian=0;
  10387. const char *str,*map,*evt="";
  10388. struct script_data *data;
  10389. bool has_index = false;
  10390. map =script_getstr(st,2);
  10391. x =script_getnum(st,3);
  10392. y =script_getnum(st,4);
  10393. str =script_getstr(st,5);
  10394. class_=script_getnum(st,6);
  10395. if( script_hasdata(st,8) )
  10396. {// "<event label>",<guardian index>
  10397. evt=script_getstr(st,7);
  10398. guardian=script_getnum(st,8);
  10399. has_index = true;
  10400. } else if( script_hasdata(st,7) ){
  10401. data=script_getdata(st,7);
  10402. get_val(st,data);
  10403. if( data_isstring(data) )
  10404. {// "<event label>"
  10405. evt=script_getstr(st,7);
  10406. } else if( data_isint(data) )
  10407. {// <guardian index>
  10408. guardian=script_getnum(st,7);
  10409. has_index = true;
  10410. } else {
  10411. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  10412. script_reportdata(data);
  10413. return 1;
  10414. }
  10415. }
  10416. check_event(st, evt);
  10417. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  10418. return 0;
  10419. }
  10420. /*==========================================
  10421. * Invisible Walls [Zephyrus]
  10422. *------------------------------------------*/
  10423. BUILDIN_FUNC(setwall)
  10424. {
  10425. const char *map, *name;
  10426. int x, y, m, size, dir;
  10427. bool shootable;
  10428. map = script_getstr(st,2);
  10429. x = script_getnum(st,3);
  10430. y = script_getnum(st,4);
  10431. size = script_getnum(st,5);
  10432. dir = script_getnum(st,6);
  10433. shootable = script_getnum(st,7);
  10434. name = script_getstr(st,8);
  10435. if( (m = map_mapname2mapid(map)) < 0 )
  10436. return 0; // Invalid Map
  10437. map_iwall_set(m, x, y, size, dir, shootable, name);
  10438. return 0;
  10439. }
  10440. BUILDIN_FUNC(delwall)
  10441. {
  10442. const char *name = script_getstr(st,2);
  10443. map_iwall_remove(name);
  10444. return 0;
  10445. }
  10446. /// Retrieves various information about the specified guardian.
  10447. ///
  10448. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  10449. /// type: 0 - whether it is deployed or not
  10450. /// 1 - maximum hp
  10451. /// 2 - current hp
  10452. ///
  10453. BUILDIN_FUNC(guardianinfo)
  10454. {
  10455. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10456. int id = script_getnum(st,3);
  10457. int type = script_getnum(st,4);
  10458. struct guild_castle* gc = guild_mapname2gc(mapname);
  10459. struct mob_data* gd;
  10460. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  10461. {
  10462. script_pushint(st,-1);
  10463. return 0;
  10464. }
  10465. if( type == 0 )
  10466. script_pushint(st, gc->guardian[id].visible);
  10467. else
  10468. if( !gc->guardian[id].visible )
  10469. script_pushint(st,-1);
  10470. else
  10471. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  10472. script_pushint(st,-1);
  10473. else
  10474. {
  10475. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  10476. else if( type == 2 ) script_pushint(st,gd->status.hp);
  10477. else
  10478. script_pushint(st,-1);
  10479. }
  10480. return 0;
  10481. }
  10482. /*==========================================
  10483. * Get the item name by item_id or null
  10484. *------------------------------------------*/
  10485. BUILDIN_FUNC(getitemname)
  10486. {
  10487. int item_id=0;
  10488. struct item_data *i_data;
  10489. char *item_name;
  10490. struct script_data *data;
  10491. data=script_getdata(st,2);
  10492. get_val(st,data);
  10493. if( data_isstring(data) ){
  10494. const char *name=conv_str(st,data);
  10495. struct item_data *item_data = itemdb_searchname(name);
  10496. if( item_data )
  10497. item_id=item_data->nameid;
  10498. }else
  10499. item_id=conv_num(st,data);
  10500. i_data = itemdb_exists(item_id);
  10501. if (i_data == NULL)
  10502. {
  10503. script_pushconststr(st,"null");
  10504. return 0;
  10505. }
  10506. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  10507. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  10508. script_pushstr(st,item_name);
  10509. return 0;
  10510. }
  10511. /*==========================================
  10512. * Returns number of slots an item has. [Skotlex]
  10513. *------------------------------------------*/
  10514. BUILDIN_FUNC(getitemslots)
  10515. {
  10516. int item_id;
  10517. struct item_data *i_data;
  10518. item_id=script_getnum(st,2);
  10519. i_data = itemdb_exists(item_id);
  10520. if (i_data)
  10521. script_pushint(st,i_data->slot);
  10522. else
  10523. script_pushint(st,-1);
  10524. return 0;
  10525. }
  10526. // TODO: add matk here if needed/once we get rid of RENEWAL
  10527. /*==========================================
  10528. * Returns some values of an item [Lupus]
  10529. * Price, Weight, etc...
  10530. getiteminfo(itemID,n), where n
  10531. 0 value_buy;
  10532. 1 value_sell;
  10533. 2 type;
  10534. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10535. if = 0, then monsters don't drop it at all (rare or a quest item)
  10536. if = -1, then this item is sold in NPC shops only
  10537. 4 sex;
  10538. 5 equip;
  10539. 6 weight;
  10540. 7 atk;
  10541. 8 def;
  10542. 9 range;
  10543. 10 slot;
  10544. 11 look;
  10545. 12 elv;
  10546. 13 wlv;
  10547. 14 view id
  10548. *------------------------------------------*/
  10549. BUILDIN_FUNC(getiteminfo)
  10550. {
  10551. int item_id,n;
  10552. int *item_arr;
  10553. struct item_data *i_data;
  10554. item_id = script_getnum(st,2);
  10555. n = script_getnum(st,3);
  10556. i_data = itemdb_exists(item_id);
  10557. if (i_data && n>=0 && n<=14) {
  10558. item_arr = (int*)&i_data->value_buy;
  10559. script_pushint(st,item_arr[n]);
  10560. } else
  10561. script_pushint(st,-1);
  10562. return 0;
  10563. }
  10564. /*==========================================
  10565. * Set some values of an item [Lupus]
  10566. * Price, Weight, etc...
  10567. setiteminfo(itemID,n,Value), where n
  10568. 0 value_buy;
  10569. 1 value_sell;
  10570. 2 type;
  10571. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10572. if = 0, then monsters don't drop it at all (rare or a quest item)
  10573. if = -1, then this item is sold in NPC shops only
  10574. 4 sex;
  10575. 5 equip;
  10576. 6 weight;
  10577. 7 atk;
  10578. 8 def;
  10579. 9 range;
  10580. 10 slot;
  10581. 11 look;
  10582. 12 elv;
  10583. 13 wlv;
  10584. 14 view id
  10585. * Returns Value or -1 if the wrong field's been set
  10586. *------------------------------------------*/
  10587. BUILDIN_FUNC(setiteminfo)
  10588. {
  10589. int item_id,n,value;
  10590. int *item_arr;
  10591. struct item_data *i_data;
  10592. item_id = script_getnum(st,2);
  10593. n = script_getnum(st,3);
  10594. value = script_getnum(st,4);
  10595. i_data = itemdb_exists(item_id);
  10596. if (i_data && n>=0 && n<=14) {
  10597. item_arr = (int*)&i_data->value_buy;
  10598. item_arr[n] = value;
  10599. script_pushint(st,value);
  10600. } else
  10601. script_pushint(st,-1);
  10602. return 0;
  10603. }
  10604. /*==========================================
  10605. * Returns value from equipped item slot n [Lupus]
  10606. getequipcardid(num,slot)
  10607. where
  10608. num = eqip position slot
  10609. slot = 0,1,2,3 (Card Slot N)
  10610. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  10611. it's useful when you want to check item cards or if it's signed
  10612. Useful for such quests as "Sign this refined item with players name" etc
  10613. Hat[0] +4 -> Player's Hat[0] +4
  10614. *------------------------------------------*/
  10615. BUILDIN_FUNC(getequipcardid)
  10616. {
  10617. int i=-1,num,slot;
  10618. TBL_PC *sd;
  10619. num=script_getnum(st,2);
  10620. slot=script_getnum(st,3);
  10621. sd=script_rid2sd(st);
  10622. if (num > 0 && num <= ARRAYLENGTH(equip))
  10623. i=pc_checkequip(sd,equip[num-1]);
  10624. if(i >= 0 && slot>=0 && slot<4)
  10625. script_pushint(st,sd->status.inventory[i].card[slot]);
  10626. else
  10627. script_pushint(st,0);
  10628. return 0;
  10629. }
  10630. /*==========================================
  10631. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  10632. *------------------------------------------*/
  10633. BUILDIN_FUNC(petskillbonus)
  10634. {
  10635. struct pet_data *pd;
  10636. TBL_PC *sd=script_rid2sd(st);
  10637. if(sd==NULL || sd->pd==NULL)
  10638. return 0;
  10639. pd=sd->pd;
  10640. if (pd->bonus)
  10641. { //Clear previous bonus
  10642. if (pd->bonus->timer != INVALID_TIMER)
  10643. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  10644. } else //init
  10645. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  10646. pd->bonus->type=script_getnum(st,2);
  10647. pd->bonus->val=script_getnum(st,3);
  10648. pd->bonus->duration=script_getnum(st,4);
  10649. pd->bonus->delay=script_getnum(st,5);
  10650. if (pd->state.skillbonus == 1)
  10651. pd->state.skillbonus=0; // waiting state
  10652. // wait for timer to start
  10653. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10654. pd->bonus->timer = INVALID_TIMER;
  10655. else
  10656. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  10657. return 0;
  10658. }
  10659. /*==========================================
  10660. * pet looting [Valaris] //Rewritten by [Skotlex]
  10661. *------------------------------------------*/
  10662. BUILDIN_FUNC(petloot)
  10663. {
  10664. int max;
  10665. struct pet_data *pd;
  10666. TBL_PC *sd=script_rid2sd(st);
  10667. if(sd==NULL || sd->pd==NULL)
  10668. return 0;
  10669. max=script_getnum(st,2);
  10670. if(max < 1)
  10671. max = 1; //Let'em loot at least 1 item.
  10672. else if (max > MAX_PETLOOT_SIZE)
  10673. max = MAX_PETLOOT_SIZE;
  10674. pd = sd->pd;
  10675. if (pd->loot != NULL)
  10676. { //Release whatever was there already and reallocate memory
  10677. pet_lootitem_drop(pd, pd->master);
  10678. aFree(pd->loot->item);
  10679. }
  10680. else
  10681. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  10682. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  10683. pd->loot->max=max;
  10684. pd->loot->count = 0;
  10685. pd->loot->weight = 0;
  10686. return 0;
  10687. }
  10688. /*==========================================
  10689. * Set arrays with info of all sd inventory :
  10690. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  10691. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  10692. * @inventorylist_card(0..3), @inventorylist_expire
  10693. * @inventorylist_count = scalar
  10694. *------------------------------------------*/
  10695. BUILDIN_FUNC(getinventorylist)
  10696. {
  10697. TBL_PC *sd=script_rid2sd(st);
  10698. char card_var[NAME_LENGTH];
  10699. int i,j=0,k;
  10700. if(!sd) return 0;
  10701. for(i=0;i<MAX_INVENTORY;i++){
  10702. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  10703. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  10704. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  10705. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  10706. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  10707. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  10708. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  10709. for (k = 0; k < MAX_SLOTS; k++)
  10710. {
  10711. sprintf(card_var, "@inventorylist_card%d",k+1);
  10712. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  10713. }
  10714. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  10715. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  10716. j++;
  10717. }
  10718. }
  10719. pc_setreg(sd,add_str("@inventorylist_count"),j);
  10720. return 0;
  10721. }
  10722. BUILDIN_FUNC(getskilllist)
  10723. {
  10724. TBL_PC *sd=script_rid2sd(st);
  10725. int i,j=0;
  10726. if(!sd) return 0;
  10727. for(i=0;i<MAX_SKILL;i++){
  10728. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  10729. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  10730. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  10731. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  10732. j++;
  10733. }
  10734. }
  10735. pc_setreg(sd,add_str("@skilllist_count"),j);
  10736. return 0;
  10737. }
  10738. BUILDIN_FUNC(clearitem)
  10739. {
  10740. TBL_PC *sd=script_rid2sd(st);
  10741. int i;
  10742. if(sd==NULL) return 0;
  10743. for (i=0; i<MAX_INVENTORY; i++) {
  10744. if (sd->status.inventory[i].amount) {
  10745. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  10746. }
  10747. }
  10748. return 0;
  10749. }
  10750. /*==========================================
  10751. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  10752. *------------------------------------------*/
  10753. BUILDIN_FUNC(disguise)
  10754. {
  10755. int id;
  10756. TBL_PC* sd = script_rid2sd(st);
  10757. if (sd == NULL) return 0;
  10758. id = script_getnum(st,2);
  10759. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  10760. pc_disguise(sd, id);
  10761. script_pushint(st,id);
  10762. } else
  10763. script_pushint(st,0);
  10764. return 0;
  10765. }
  10766. /*==========================================
  10767. * Undisguise Player (returns 1 if success, 0 on fail)
  10768. *------------------------------------------*/
  10769. BUILDIN_FUNC(undisguise)
  10770. {
  10771. TBL_PC* sd = script_rid2sd(st);
  10772. if (sd == NULL) return 0;
  10773. if (sd->disguise) {
  10774. pc_disguise(sd, 0);
  10775. script_pushint(st,0);
  10776. } else {
  10777. script_pushint(st,1);
  10778. }
  10779. return 0;
  10780. }
  10781. /*==========================================
  10782. * Transform a bl to another _class,
  10783. * @type unused
  10784. *------------------------------------------*/
  10785. BUILDIN_FUNC(classchange)
  10786. {
  10787. int _class,type;
  10788. struct block_list *bl=map_id2bl(st->oid);
  10789. if(bl==NULL) return 0;
  10790. _class=script_getnum(st,2);
  10791. type=script_getnum(st,3);
  10792. clif_class_change(bl,_class,type);
  10793. return 0;
  10794. }
  10795. /*==========================================
  10796. * Display an effect
  10797. *------------------------------------------*/
  10798. BUILDIN_FUNC(misceffect)
  10799. {
  10800. int type;
  10801. type=script_getnum(st,2);
  10802. if(st->oid && st->oid != fake_nd->bl.id) {
  10803. struct block_list *bl = map_id2bl(st->oid);
  10804. if (bl)
  10805. clif_specialeffect(bl,type,AREA);
  10806. } else{
  10807. TBL_PC *sd=script_rid2sd(st);
  10808. if(sd)
  10809. clif_specialeffect(&sd->bl,type,AREA);
  10810. }
  10811. return 0;
  10812. }
  10813. /*==========================================
  10814. * Play a BGM on a single client [Rikter/Yommy]
  10815. *------------------------------------------*/
  10816. BUILDIN_FUNC(playBGM)
  10817. {
  10818. const char* name;
  10819. struct map_session_data* sd;
  10820. if( ( sd = script_rid2sd(st) ) != NULL )
  10821. {
  10822. name = script_getstr(st,2);
  10823. clif_playBGM(sd, name);
  10824. }
  10825. return 0;
  10826. }
  10827. static int playBGM_sub(struct block_list* bl,va_list ap)
  10828. {
  10829. const char* name = va_arg(ap,const char*);
  10830. clif_playBGM(BL_CAST(BL_PC, bl), name);
  10831. return 0;
  10832. }
  10833. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  10834. {
  10835. const char* name = va_arg(args, const char*);
  10836. clif_playBGM(sd, name);
  10837. return 0;
  10838. }
  10839. /*==========================================
  10840. * Play a BGM on multiple client [Rikter/Yommy]
  10841. *------------------------------------------*/
  10842. BUILDIN_FUNC(playBGMall)
  10843. {
  10844. const char* name;
  10845. name = script_getstr(st,2);
  10846. if( script_hasdata(st,7) )
  10847. {// specified part of map
  10848. const char* map = script_getstr(st,3);
  10849. int x0 = script_getnum(st,4);
  10850. int y0 = script_getnum(st,5);
  10851. int x1 = script_getnum(st,6);
  10852. int y1 = script_getnum(st,7);
  10853. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  10854. }
  10855. else if( script_hasdata(st,3) )
  10856. {// entire map
  10857. const char* map = script_getstr(st,3);
  10858. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  10859. }
  10860. else
  10861. {// entire server
  10862. map_foreachpc(&playBGM_foreachpc_sub, name);
  10863. }
  10864. return 0;
  10865. }
  10866. /*==========================================
  10867. * Play a .wav sound for sd
  10868. *------------------------------------------*/
  10869. BUILDIN_FUNC(soundeffect)
  10870. {
  10871. TBL_PC* sd = script_rid2sd(st);
  10872. const char* name = script_getstr(st,2);
  10873. int type = script_getnum(st,3);
  10874. if(sd)
  10875. {
  10876. clif_soundeffect(sd,&sd->bl,name,type);
  10877. }
  10878. return 0;
  10879. }
  10880. int soundeffect_sub(struct block_list* bl,va_list ap)
  10881. {
  10882. char* name = va_arg(ap,char*);
  10883. int type = va_arg(ap,int);
  10884. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  10885. return 0;
  10886. }
  10887. /*==========================================
  10888. * Play a sound effect (.wav) on multiple clients
  10889. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  10890. *------------------------------------------*/
  10891. BUILDIN_FUNC(soundeffectall)
  10892. {
  10893. struct block_list* bl;
  10894. const char* name;
  10895. int type;
  10896. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  10897. if (!bl)
  10898. return 0;
  10899. name = script_getstr(st,2);
  10900. type = script_getnum(st,3);
  10901. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  10902. if(!script_hasdata(st,4))
  10903. { // area around
  10904. clif_soundeffectall(bl, name, type, AREA);
  10905. }
  10906. else
  10907. if(!script_hasdata(st,5))
  10908. { // entire map
  10909. const char* map = script_getstr(st,4);
  10910. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  10911. }
  10912. else
  10913. if(script_hasdata(st,8))
  10914. { // specified part of map
  10915. const char* map = script_getstr(st,4);
  10916. int x0 = script_getnum(st,5);
  10917. int y0 = script_getnum(st,6);
  10918. int x1 = script_getnum(st,7);
  10919. int y1 = script_getnum(st,8);
  10920. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  10921. }
  10922. else
  10923. {
  10924. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  10925. }
  10926. return 0;
  10927. }
  10928. /*==========================================
  10929. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  10930. *------------------------------------------*/
  10931. BUILDIN_FUNC(petrecovery)
  10932. {
  10933. struct pet_data *pd;
  10934. TBL_PC *sd=script_rid2sd(st);
  10935. if(sd==NULL || sd->pd==NULL)
  10936. return 0;
  10937. pd=sd->pd;
  10938. if (pd->recovery)
  10939. { //Halt previous bonus
  10940. if (pd->recovery->timer != INVALID_TIMER)
  10941. delete_timer(pd->recovery->timer, pet_recovery_timer);
  10942. } else //Init
  10943. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  10944. pd->recovery->type = (sc_type)script_getnum(st,2);
  10945. pd->recovery->delay = script_getnum(st,3);
  10946. pd->recovery->timer = INVALID_TIMER;
  10947. return 0;
  10948. }
  10949. /*==========================================
  10950. * pet healing [Valaris] //Rewritten by [Skotlex]
  10951. *------------------------------------------*/
  10952. BUILDIN_FUNC(petheal)
  10953. {
  10954. struct pet_data *pd;
  10955. TBL_PC *sd=script_rid2sd(st);
  10956. if(sd==NULL || sd->pd==NULL)
  10957. return 0;
  10958. pd=sd->pd;
  10959. if (pd->s_skill)
  10960. { //Clear previous skill
  10961. if (pd->s_skill->timer != INVALID_TIMER)
  10962. {
  10963. if (pd->s_skill->id)
  10964. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10965. else
  10966. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10967. }
  10968. } else //init memory
  10969. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10970. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  10971. //Use the lv as the amount to heal
  10972. pd->s_skill->lv=script_getnum(st,2);
  10973. pd->s_skill->delay=script_getnum(st,3);
  10974. pd->s_skill->hp=script_getnum(st,4);
  10975. pd->s_skill->sp=script_getnum(st,5);
  10976. //Use delay as initial offset to avoid skill/heal exploits
  10977. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10978. pd->s_skill->timer = INVALID_TIMER;
  10979. else
  10980. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  10981. return 0;
  10982. }
  10983. /*==========================================
  10984. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  10985. *------------------------------------------*/
  10986. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  10987. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  10988. BUILDIN_FUNC(petskillattack)
  10989. {
  10990. struct pet_data *pd;
  10991. TBL_PC *sd=script_rid2sd(st);
  10992. if(sd==NULL || sd->pd==NULL)
  10993. return 0;
  10994. pd=sd->pd;
  10995. if (pd->a_skill == NULL)
  10996. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10997. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10998. pd->a_skill->lv=script_getnum(st,3);
  10999. pd->a_skill->div_ = 0;
  11000. pd->a_skill->rate=script_getnum(st,4);
  11001. pd->a_skill->bonusrate=script_getnum(st,5);
  11002. return 0;
  11003. }
  11004. /*==========================================
  11005. * pet attack skills [Valaris]
  11006. *------------------------------------------*/
  11007. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  11008. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  11009. BUILDIN_FUNC(petskillattack2)
  11010. {
  11011. struct pet_data *pd;
  11012. TBL_PC *sd=script_rid2sd(st);
  11013. if(sd==NULL || sd->pd==NULL)
  11014. return 0;
  11015. pd=sd->pd;
  11016. if (pd->a_skill == NULL)
  11017. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11018. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11019. pd->a_skill->lv=script_getnum(st,3);
  11020. pd->a_skill->div_ = script_getnum(st,4);
  11021. pd->a_skill->rate=script_getnum(st,5);
  11022. pd->a_skill->bonusrate=script_getnum(st,6);
  11023. return 0;
  11024. }
  11025. /*==========================================
  11026. * pet support skills [Skotlex]
  11027. *------------------------------------------*/
  11028. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11029. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11030. BUILDIN_FUNC(petskillsupport)
  11031. {
  11032. struct pet_data *pd;
  11033. TBL_PC *sd=script_rid2sd(st);
  11034. if(sd==NULL || sd->pd==NULL)
  11035. return 0;
  11036. pd=sd->pd;
  11037. if (pd->s_skill)
  11038. { //Clear previous skill
  11039. if (pd->s_skill->timer != INVALID_TIMER)
  11040. {
  11041. if (pd->s_skill->id)
  11042. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11043. else
  11044. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11045. }
  11046. } else //init memory
  11047. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11048. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11049. pd->s_skill->lv=script_getnum(st,3);
  11050. pd->s_skill->delay=script_getnum(st,4);
  11051. pd->s_skill->hp=script_getnum(st,5);
  11052. pd->s_skill->sp=script_getnum(st,6);
  11053. //Use delay as initial offset to avoid skill/heal exploits
  11054. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11055. pd->s_skill->timer = INVALID_TIMER;
  11056. else
  11057. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11058. return 0;
  11059. }
  11060. /*==========================================
  11061. * Scripted skill effects [Celest]
  11062. *------------------------------------------*/
  11063. /// skilleffect <skill id>,<level>
  11064. /// skilleffect "<skill name>",<level>
  11065. BUILDIN_FUNC(skilleffect)
  11066. {
  11067. TBL_PC *sd;
  11068. uint16 skill_id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11069. uint16 skill_lv=script_getnum(st,3);
  11070. sd=script_rid2sd(st);
  11071. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11072. return 0;
  11073. }
  11074. /*==========================================
  11075. * NPC skill effects [Valaris]
  11076. *------------------------------------------*/
  11077. /// npcskilleffect <skill id>,<level>,<x>,<y>
  11078. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  11079. BUILDIN_FUNC(npcskilleffect)
  11080. {
  11081. struct block_list *bl= map_id2bl(st->oid);
  11082. uint16 skill_id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11083. uint16 skill_lv=script_getnum(st,3);
  11084. int x=script_getnum(st,4);
  11085. int y=script_getnum(st,5);
  11086. if (bl)
  11087. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  11088. return 0;
  11089. }
  11090. /*==========================================
  11091. * Special effects [Valaris]
  11092. *------------------------------------------*/
  11093. BUILDIN_FUNC(specialeffect)
  11094. {
  11095. struct block_list *bl=map_id2bl(st->oid);
  11096. int type = script_getnum(st,2);
  11097. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11098. if(bl==NULL)
  11099. return 0;
  11100. if( script_hasdata(st,4) )
  11101. {
  11102. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11103. if(nd)
  11104. clif_specialeffect(&nd->bl, type, target);
  11105. }
  11106. else
  11107. {
  11108. if (target == SELF) {
  11109. TBL_PC *sd=script_rid2sd(st);
  11110. if (sd)
  11111. clif_specialeffect_single(bl,type,sd->fd);
  11112. } else {
  11113. clif_specialeffect(bl, type, target);
  11114. }
  11115. }
  11116. return 0;
  11117. }
  11118. BUILDIN_FUNC(specialeffect2)
  11119. {
  11120. TBL_PC *sd=script_rid2sd(st);
  11121. int type = script_getnum(st,2);
  11122. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11123. if( script_hasdata(st,4) )
  11124. sd = map_nick2sd(script_getstr(st,4));
  11125. if (sd)
  11126. clif_specialeffect(&sd->bl, type, target);
  11127. return 0;
  11128. }
  11129. /*==========================================
  11130. * Nude [Valaris]
  11131. *------------------------------------------*/
  11132. BUILDIN_FUNC(nude)
  11133. {
  11134. TBL_PC *sd = script_rid2sd(st);
  11135. int i, calcflag = 0;
  11136. if( sd == NULL )
  11137. return 0;
  11138. for( i = 0 ; i < EQI_MAX; i++ ) {
  11139. if( sd->equip_index[ i ] >= 0 ) {
  11140. if( !calcflag )
  11141. calcflag = 1;
  11142. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  11143. }
  11144. }
  11145. if( calcflag )
  11146. status_calc_pc(sd,0);
  11147. return 0;
  11148. }
  11149. /*==========================================
  11150. * gmcommand [MouseJstr]
  11151. *------------------------------------------*/
  11152. BUILDIN_FUNC(atcommand)
  11153. {
  11154. TBL_PC dummy_sd;
  11155. TBL_PC* sd;
  11156. int fd;
  11157. const char* cmd;
  11158. cmd = script_getstr(st,2);
  11159. if (st->rid) {
  11160. sd = script_rid2sd(st);
  11161. fd = sd->fd;
  11162. } else { //Use a dummy character.
  11163. sd = &dummy_sd;
  11164. fd = 0;
  11165. memset(&dummy_sd, 0, sizeof(TBL_PC));
  11166. if (st->oid)
  11167. {
  11168. struct block_list* bl = map_id2bl(st->oid);
  11169. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  11170. if (bl->type == BL_NPC)
  11171. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  11172. }
  11173. }
  11174. if (!is_atcommand(fd, sd, cmd, 0)) {
  11175. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  11176. script_reportsrc(st);
  11177. return 1;
  11178. }
  11179. return 0;
  11180. }
  11181. /*==========================================
  11182. * Displays a message for the player only (like system messages like "you got an apple" )
  11183. *------------------------------------------*/
  11184. BUILDIN_FUNC(dispbottom)
  11185. {
  11186. TBL_PC *sd=script_rid2sd(st);
  11187. const char *message;
  11188. message=script_getstr(st,2);
  11189. if(sd)
  11190. clif_disp_onlyself(sd,message,(int)strlen(message));
  11191. return 0;
  11192. }
  11193. /*===================================
  11194. * Heal portion of recovery command
  11195. *-----------------------------------*/
  11196. int recovery_sub(struct map_session_data* sd, int revive)
  11197. {
  11198. if(revive&(1|4) && pc_isdead(sd)) {
  11199. status_revive(&sd->bl, 100, 100);
  11200. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  11201. clif_specialeffect(&sd->bl, 77, AREA);
  11202. } else if(revive&(1|2) && !pc_isdead(sd)) {
  11203. status_percent_heal(&sd->bl, 100, 100);
  11204. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  11205. }
  11206. return 0;
  11207. }
  11208. /*=========================================================================
  11209. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  11210. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  11211. * <type> determines <option>:
  11212. * 0 : char_id
  11213. * 1 : party_id
  11214. * 2 : guild_id
  11215. * 3 : map_name
  11216. * 4 : all characters
  11217. * <revive_flag>:
  11218. * 1 : Revive and heal all players (default)
  11219. * 2 : Heal living players only
  11220. * 4 : Revive dead players only
  11221. * <map name>:
  11222. * for types 1-2 : map_name (null = all maps)
  11223. *-------------------------------------------------------------------------*/
  11224. BUILDIN_FUNC(recovery)
  11225. {
  11226. TBL_PC *sd = script_rid2sd(st);
  11227. int map = 0, type = 0, revive = 1;
  11228. type = script_getnum(st,2);
  11229. if(script_hasdata(st,4))
  11230. revive = script_getnum(st,4);
  11231. switch(type) {
  11232. case 0:
  11233. if(script_hasdata(st,3))
  11234. sd=map_charid2sd(script_getnum(st,3));
  11235. if(sd == NULL) //If we don't have sd by now, bail out
  11236. return 0;
  11237. recovery_sub(sd, revive);
  11238. break;
  11239. case 1:
  11240. {
  11241. struct party_data* p;
  11242. struct map_session_data* pl_sd;
  11243. //When no party given, we use invoker party
  11244. int p_id, i;
  11245. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11246. map = map_mapname2mapid(script_getstr(st,5));
  11247. if(map < 1) { //But we'll check anyways
  11248. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11249. return 1;
  11250. }
  11251. }
  11252. if(script_hasdata(st,3))
  11253. p_id = script_getnum(st,3);
  11254. else
  11255. p_id = (sd)?sd->status.party_id:0;
  11256. p = party_search(p_id);
  11257. if(p == NULL)
  11258. return 0;
  11259. for (i = 0; i < MAX_PARTY; i++) {
  11260. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  11261. || (map && pl_sd->bl.m != map))
  11262. continue;
  11263. recovery_sub(pl_sd, revive);
  11264. }
  11265. break;
  11266. }
  11267. case 2:
  11268. {
  11269. struct guild* g;
  11270. struct map_session_data* pl_sd;
  11271. //When no guild given, we use invoker guild
  11272. int g_id, i;
  11273. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11274. map = map_mapname2mapid(script_getstr(st,5));
  11275. if(map < 1) { //But we'll check anyways
  11276. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11277. return 1;
  11278. }
  11279. }
  11280. if(script_hasdata(st,3))
  11281. g_id = script_getnum(st,3);
  11282. else
  11283. g_id = (sd)?sd->status.guild_id:0;
  11284. g = guild_search(g_id);
  11285. if(g == NULL)
  11286. return 0;
  11287. for (i = 0; i < MAX_GUILD; i++) {
  11288. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  11289. || (map && pl_sd->bl.m != map))
  11290. continue;
  11291. recovery_sub(pl_sd, revive);
  11292. }
  11293. break;
  11294. }
  11295. case 3:
  11296. if(script_hasdata(st,3))
  11297. map = map_mapname2mapid(script_getstr(st,3));
  11298. else
  11299. map = (sd)?sd->bl.m:0; //No sd and no map given - return
  11300. if(map < 1)
  11301. return 1;
  11302. case 4:
  11303. {
  11304. struct s_mapiterator *iter;
  11305. if(script_hasdata(st,3) && !script_isstring(st,3))
  11306. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  11307. iter = mapit_getallusers();
  11308. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11309. if(type == 3 && sd->bl.m != map)
  11310. continue;
  11311. recovery_sub(sd, revive);
  11312. }
  11313. mapit_free(iter);
  11314. break;
  11315. }
  11316. default:
  11317. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  11318. script_pushint(st,-1);
  11319. return 1;
  11320. }
  11321. script_pushint(st,1); //Successfully executed without errors
  11322. return 0;
  11323. }
  11324. /*==========================================
  11325. * Get your pet info: getpetinfo(n)
  11326. * n -> 0:pet_id 1:pet_class 2:pet_name
  11327. * 3:friendly 4:hungry, 5: rename flag.
  11328. *------------------------------------------*/
  11329. BUILDIN_FUNC(getpetinfo)
  11330. {
  11331. TBL_PC *sd=script_rid2sd(st);
  11332. TBL_PET *pd;
  11333. int type=script_getnum(st,2);
  11334. if(!sd || !sd->pd) {
  11335. if (type == 2)
  11336. script_pushconststr(st,"null");
  11337. else
  11338. script_pushint(st,0);
  11339. return 0;
  11340. }
  11341. pd = sd->pd;
  11342. switch(type){
  11343. case 0: script_pushint(st,pd->pet.pet_id); break;
  11344. case 1: script_pushint(st,pd->pet.class_); break;
  11345. case 2: script_pushstrcopy(st,pd->pet.name); break;
  11346. case 3: script_pushint(st,pd->pet.intimate); break;
  11347. case 4: script_pushint(st,pd->pet.hungry); break;
  11348. case 5: script_pushint(st,pd->pet.rename_flag); break;
  11349. default:
  11350. script_pushint(st,0);
  11351. break;
  11352. }
  11353. return 0;
  11354. }
  11355. /*==========================================
  11356. * Get your homunculus info: gethominfo(n)
  11357. * n -> 0:hom_id 1:class 2:name
  11358. * 3:friendly 4:hungry, 5: rename flag.
  11359. * 6: level
  11360. *------------------------------------------*/
  11361. BUILDIN_FUNC(gethominfo)
  11362. {
  11363. TBL_PC *sd=script_rid2sd(st);
  11364. TBL_HOM *hd;
  11365. int type=script_getnum(st,2);
  11366. hd = sd?sd->hd:NULL;
  11367. if(!hd) {
  11368. if (type == 2)
  11369. script_pushconststr(st,"null");
  11370. else
  11371. script_pushint(st,0);
  11372. return 0;
  11373. }
  11374. switch(type){
  11375. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  11376. case 1: script_pushint(st,hd->homunculus.class_); break;
  11377. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  11378. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  11379. case 4: script_pushint(st,hd->homunculus.hunger); break;
  11380. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  11381. case 6: script_pushint(st,hd->homunculus.level); break;
  11382. default:
  11383. script_pushint(st,0);
  11384. break;
  11385. }
  11386. return 0;
  11387. }
  11388. /// Retrieves information about character's mercenary
  11389. /// getmercinfo <type>[,<char id>];
  11390. BUILDIN_FUNC(getmercinfo)
  11391. {
  11392. int type, char_id;
  11393. struct map_session_data* sd;
  11394. struct mercenary_data* md;
  11395. type = script_getnum(st,2);
  11396. if( script_hasdata(st,3) )
  11397. {
  11398. char_id = script_getnum(st,3);
  11399. if( ( sd = map_charid2sd(char_id) ) == NULL )
  11400. {
  11401. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  11402. script_pushnil(st);
  11403. return 1;
  11404. }
  11405. }
  11406. else
  11407. {
  11408. if( ( sd = script_rid2sd(st) ) == NULL )
  11409. {
  11410. script_pushnil(st);
  11411. return 0;
  11412. }
  11413. }
  11414. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  11415. switch( type )
  11416. {
  11417. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  11418. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  11419. case 2:
  11420. if( md )
  11421. script_pushstrcopy(st,md->db->name);
  11422. else
  11423. script_pushconststr(st,"");
  11424. break;
  11425. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  11426. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  11427. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  11428. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  11429. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  11430. default:
  11431. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  11432. script_pushnil(st);
  11433. return 1;
  11434. }
  11435. return 0;
  11436. }
  11437. /*==========================================
  11438. * Shows wether your inventory(and equips) contain
  11439. selected card or not.
  11440. checkequipedcard(4001);
  11441. *------------------------------------------*/
  11442. BUILDIN_FUNC(checkequipedcard)
  11443. {
  11444. TBL_PC *sd=script_rid2sd(st);
  11445. if(sd){
  11446. int n,i,c=0;
  11447. c=script_getnum(st,2);
  11448. for(i=0;i<MAX_INVENTORY;i++){
  11449. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  11450. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  11451. continue;
  11452. for(n=0;n<sd->inventory_data[i]->slot;n++){
  11453. if(sd->status.inventory[i].card[n]==c){
  11454. script_pushint(st,1);
  11455. return 0;
  11456. }
  11457. }
  11458. }
  11459. }
  11460. }
  11461. script_pushint(st,0);
  11462. return 0;
  11463. }
  11464. BUILDIN_FUNC(jump_zero)
  11465. {
  11466. int sel;
  11467. sel=script_getnum(st,2);
  11468. if(!sel) {
  11469. int pos;
  11470. if( !data_islabel(script_getdata(st,3)) ){
  11471. ShowError("script: jump_zero: not label !\n");
  11472. st->state=END;
  11473. return 1;
  11474. }
  11475. pos=script_getnum(st,3);
  11476. st->pos=pos;
  11477. st->state=GOTO;
  11478. }
  11479. return 0;
  11480. }
  11481. /*==========================================
  11482. * movenpc [MouseJstr]
  11483. *------------------------------------------*/
  11484. BUILDIN_FUNC(movenpc)
  11485. {
  11486. TBL_NPC *nd = NULL;
  11487. const char *npc;
  11488. int x,y;
  11489. npc = script_getstr(st,2);
  11490. x = script_getnum(st,3);
  11491. y = script_getnum(st,4);
  11492. if ((nd = npc_name2id(npc)) == NULL)
  11493. return -1;
  11494. if (script_hasdata(st,5))
  11495. nd->ud.dir = script_getnum(st,5) % 8;
  11496. npc_movenpc(nd, x, y);
  11497. return 0;
  11498. }
  11499. /*==========================================
  11500. * message [MouseJstr]
  11501. *------------------------------------------*/
  11502. BUILDIN_FUNC(message)
  11503. {
  11504. const char *msg,*player;
  11505. TBL_PC *pl_sd = NULL;
  11506. player = script_getstr(st,2);
  11507. msg = script_getstr(st,3);
  11508. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  11509. return 0;
  11510. clif_displaymessage(pl_sd->fd, msg);
  11511. return 0;
  11512. }
  11513. /*==========================================
  11514. * npctalk (sends message to surrounding area)
  11515. *------------------------------------------*/
  11516. BUILDIN_FUNC(npctalk)
  11517. {
  11518. const char* str;
  11519. char name[NAME_LENGTH], message[256];
  11520. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  11521. str = script_getstr(st,2);
  11522. if(nd)
  11523. {
  11524. safestrncpy(name, nd->name, sizeof(name));
  11525. strtok(name, "#"); // discard extra name identifier if present
  11526. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  11527. clif_disp_overhead(&nd->bl, message);
  11528. }
  11529. return 0;
  11530. }
  11531. // change npc walkspeed [Valaris]
  11532. BUILDIN_FUNC(npcspeed)
  11533. {
  11534. struct npc_data* nd;
  11535. int speed;
  11536. speed = script_getnum(st,2);
  11537. nd =(struct npc_data *)map_id2bl(st->oid);
  11538. if( nd )
  11539. {
  11540. nd->speed = speed;
  11541. nd->ud.state.speed_changed = 1;
  11542. }
  11543. return 0;
  11544. }
  11545. // make an npc walk to a position [Valaris]
  11546. BUILDIN_FUNC(npcwalkto)
  11547. {
  11548. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11549. int x=0,y=0;
  11550. x=script_getnum(st,2);
  11551. y=script_getnum(st,3);
  11552. if(nd) {
  11553. if (!nd->status.hp) {
  11554. status_calc_npc(nd, true);
  11555. } else {
  11556. status_calc_npc(nd, false);
  11557. }
  11558. unit_walktoxy(&nd->bl,x,y,0);
  11559. }
  11560. return 0;
  11561. }
  11562. // stop an npc's movement [Valaris]
  11563. BUILDIN_FUNC(npcstop)
  11564. {
  11565. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11566. if(nd) {
  11567. unit_stop_walking(&nd->bl,1|4);
  11568. }
  11569. return 0;
  11570. }
  11571. /*==========================================
  11572. * getlook char info. getlook(arg)
  11573. *------------------------------------------*/
  11574. BUILDIN_FUNC(getlook)
  11575. {
  11576. int type,val;
  11577. TBL_PC *sd;
  11578. sd=script_rid2sd(st);
  11579. type=script_getnum(st,2);
  11580. val=-1;
  11581. switch(type) {
  11582. case LOOK_HAIR: val=sd->status.hair; break; //1
  11583. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  11584. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  11585. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  11586. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  11587. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  11588. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  11589. case LOOK_SHIELD: val=sd->status.shield; break; //8
  11590. case LOOK_SHOES: break; //9
  11591. case LOOK_ROBE: val=sd->status.robe; break; //12
  11592. }
  11593. script_pushint(st,val);
  11594. return 0;
  11595. }
  11596. /*==========================================
  11597. * get char save point. argument: 0- map name, 1- x, 2- y
  11598. *------------------------------------------*/
  11599. BUILDIN_FUNC(getsavepoint)
  11600. {
  11601. TBL_PC* sd;
  11602. int type;
  11603. sd = script_rid2sd(st);
  11604. if (sd == NULL) {
  11605. script_pushint(st,0);
  11606. return 0;
  11607. }
  11608. type = script_getnum(st,2);
  11609. switch(type) {
  11610. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  11611. case 1: script_pushint(st,sd->status.save_point.x); break;
  11612. case 2: script_pushint(st,sd->status.save_point.y); break;
  11613. default:
  11614. script_pushint(st,0);
  11615. break;
  11616. }
  11617. return 0;
  11618. }
  11619. /*==========================================
  11620. * Get position for char/npc/pet/mob objects. Added by Lorky
  11621. *
  11622. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  11623. * where type:
  11624. * MapName$ - String variable for output map name
  11625. * MapX - Integer variable for output coord X
  11626. * MapY - Integer variable for output coord Y
  11627. * type - type of object
  11628. * 0 - Character coord
  11629. * 1 - NPC coord
  11630. * 2 - Pet coord
  11631. * 3 - Mob coord (not released)
  11632. * 4 - Homun coord
  11633. * 5 - Mercenary coord
  11634. * 6 - Elemental coord
  11635. * CharName$ - Name object. If miss or "this" the current object
  11636. *
  11637. * Return:
  11638. * 0 - success
  11639. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  11640. *------------------------------------------*/
  11641. BUILDIN_FUNC(getmapxy)
  11642. {
  11643. struct block_list *bl = NULL;
  11644. TBL_PC *sd=NULL;
  11645. int num;
  11646. const char *name;
  11647. char prefix;
  11648. int x,y,type;
  11649. char mapname[MAP_NAME_LENGTH];
  11650. if( !data_isreference(script_getdata(st,2)) ){
  11651. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  11652. script_pushint(st,-1);
  11653. return 1;
  11654. }
  11655. if( !data_isreference(script_getdata(st,3)) ){
  11656. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  11657. script_pushint(st,-1);
  11658. return 1;
  11659. }
  11660. if( !data_isreference(script_getdata(st,4)) ){
  11661. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  11662. script_pushint(st,-1);
  11663. return 1;
  11664. }
  11665. // Possible needly check function parameters on C_STR,C_INT,C_INT
  11666. type=script_getnum(st,5);
  11667. switch (type){
  11668. case 0: //Get Character Position
  11669. if( script_hasdata(st,6) )
  11670. sd=map_nick2sd(script_getstr(st,6));
  11671. else
  11672. sd=script_rid2sd(st);
  11673. if (sd)
  11674. bl = &sd->bl;
  11675. break;
  11676. case 1: //Get NPC Position
  11677. if( script_hasdata(st,6) )
  11678. {
  11679. struct npc_data *nd;
  11680. nd=npc_name2id(script_getstr(st,6));
  11681. if (nd)
  11682. bl = &nd->bl;
  11683. } else //In case the origin is not an npc?
  11684. bl=map_id2bl(st->oid);
  11685. break;
  11686. case 2: //Get Pet Position
  11687. if(script_hasdata(st,6))
  11688. sd=map_nick2sd(script_getstr(st,6));
  11689. else
  11690. sd=script_rid2sd(st);
  11691. if (sd && sd->pd)
  11692. bl = &sd->pd->bl;
  11693. break;
  11694. case 3: //Get Mob Position
  11695. break; //Not supported?
  11696. case 4: //Get Homun Position
  11697. if(script_hasdata(st,6))
  11698. sd=map_nick2sd(script_getstr(st,6));
  11699. else
  11700. sd=script_rid2sd(st);
  11701. if (sd && sd->hd)
  11702. bl = &sd->hd->bl;
  11703. break;
  11704. case 5: //Get Mercenary Position
  11705. if(script_hasdata(st,6))
  11706. sd=map_nick2sd(script_getstr(st,6));
  11707. else
  11708. sd=script_rid2sd(st);
  11709. if (sd && sd->md)
  11710. bl = &sd->md->bl;
  11711. break;
  11712. case 6: //Get Elemental Position
  11713. if(script_hasdata(st,6))
  11714. sd=map_nick2sd(script_getstr(st,6));
  11715. else
  11716. sd=script_rid2sd(st);
  11717. if (sd && sd->ed)
  11718. bl = &sd->ed->bl;
  11719. break;
  11720. default:
  11721. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  11722. script_pushint(st,-1);
  11723. return 1;
  11724. }
  11725. if (!bl) { //No object found.
  11726. script_pushint(st,-1);
  11727. return 0;
  11728. }
  11729. x= bl->x;
  11730. y= bl->y;
  11731. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  11732. //Set MapName$
  11733. num=st->stack->stack_data[st->start+2].u.num;
  11734. name=get_str(num&0x00ffffff);
  11735. prefix=*name;
  11736. if(not_server_variable(prefix))
  11737. sd=script_rid2sd(st);
  11738. else
  11739. sd=NULL;
  11740. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  11741. //Set MapX
  11742. num=st->stack->stack_data[st->start+3].u.num;
  11743. name=get_str(num&0x00ffffff);
  11744. prefix=*name;
  11745. if(not_server_variable(prefix))
  11746. sd=script_rid2sd(st);
  11747. else
  11748. sd=NULL;
  11749. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  11750. //Set MapY
  11751. num=st->stack->stack_data[st->start+4].u.num;
  11752. name=get_str(num&0x00ffffff);
  11753. prefix=*name;
  11754. if(not_server_variable(prefix))
  11755. sd=script_rid2sd(st);
  11756. else
  11757. sd=NULL;
  11758. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  11759. //Return Success value
  11760. script_pushint(st,0);
  11761. return 0;
  11762. }
  11763. /*==========================================
  11764. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  11765. *------------------------------------------*/
  11766. BUILDIN_FUNC(logmes)
  11767. {
  11768. const char *str;
  11769. TBL_PC* sd;
  11770. sd = script_rid2sd(st);
  11771. if( sd == NULL )
  11772. return 1;
  11773. str = script_getstr(st,2);
  11774. log_npc(sd,str);
  11775. return 0;
  11776. }
  11777. BUILDIN_FUNC(summon)
  11778. {
  11779. int _class, timeout=0;
  11780. const char *str,*event="";
  11781. TBL_PC *sd;
  11782. struct mob_data *md;
  11783. int tick = gettick();
  11784. sd=script_rid2sd(st);
  11785. if (!sd) return 0;
  11786. str =script_getstr(st,2);
  11787. _class=script_getnum(st,3);
  11788. if( script_hasdata(st,4) )
  11789. timeout=script_getnum(st,4);
  11790. if( script_hasdata(st,5) ){
  11791. event=script_getstr(st,5);
  11792. check_event(st, event);
  11793. }
  11794. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  11795. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  11796. if (md) {
  11797. md->master_id=sd->bl.id;
  11798. md->special_state.ai = AI_ATTACK;
  11799. if( md->deletetimer != INVALID_TIMER )
  11800. delete_timer(md->deletetimer, mob_timer_delete);
  11801. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  11802. mob_spawn (md); //Now it is ready for spawning.
  11803. clif_specialeffect(&md->bl,344,AREA);
  11804. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  11805. }
  11806. return 0;
  11807. }
  11808. /*==========================================
  11809. * Checks whether it is daytime/nighttime
  11810. *------------------------------------------*/
  11811. BUILDIN_FUNC(isnight)
  11812. {
  11813. script_pushint(st,(night_flag == 1));
  11814. return 0;
  11815. }
  11816. BUILDIN_FUNC(isday)
  11817. {
  11818. script_pushint(st,(night_flag == 0));
  11819. return 0;
  11820. }
  11821. /*================================================
  11822. * Check how many items/cards in the list are
  11823. * equipped - used for 2/15's cards patch [celest]
  11824. *------------------------------------------------*/
  11825. BUILDIN_FUNC(isequippedcnt)
  11826. {
  11827. TBL_PC *sd;
  11828. int i, j, k, id = 1;
  11829. int ret = 0;
  11830. sd = script_rid2sd(st);
  11831. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11832. script_pushint(st,0);
  11833. return 0;
  11834. }
  11835. for (i=0; id!=0; i++) {
  11836. FETCH (i+2, id) else id = 0;
  11837. if (id <= 0)
  11838. continue;
  11839. for (j=0; j<EQI_MAX; j++) {
  11840. int index;
  11841. index = sd->equip_index[j];
  11842. if(index < 0) continue;
  11843. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11844. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11845. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11846. if(!sd->inventory_data[index])
  11847. continue;
  11848. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  11849. if (sd->inventory_data[index]->nameid == id)
  11850. ret+= sd->status.inventory[index].amount;
  11851. } else { //Count cards.
  11852. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11853. continue; //No cards
  11854. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11855. if (sd->status.inventory[index].card[k] == id)
  11856. ret++; //[Lupus]
  11857. }
  11858. }
  11859. }
  11860. }
  11861. script_pushint(st,ret);
  11862. return 0;
  11863. }
  11864. /*================================================
  11865. * Check whether another card has been
  11866. * equipped - used for 2/15's cards patch [celest]
  11867. * -- Items checked cannot be reused in another
  11868. * card set to prevent exploits
  11869. *------------------------------------------------*/
  11870. BUILDIN_FUNC(isequipped)
  11871. {
  11872. TBL_PC *sd;
  11873. int i, j, k, id = 1;
  11874. int index, flag;
  11875. int ret = -1;
  11876. //Original hash to reverse it when full check fails.
  11877. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  11878. sd = script_rid2sd(st);
  11879. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11880. script_pushint(st,0);
  11881. return 0;
  11882. }
  11883. setitem_hash = sd->bonus.setitem_hash;
  11884. setitem_hash2 = sd->bonus.setitem_hash2;
  11885. for (i=0; id!=0; i++) {
  11886. FETCH (i+2, id) else id = 0;
  11887. if (id <= 0)
  11888. continue;
  11889. flag = 0;
  11890. for (j=0; j<EQI_MAX; j++) {
  11891. index = sd->equip_index[j];
  11892. if(index < 0) continue;
  11893. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11894. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11895. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11896. if(!sd->inventory_data[index])
  11897. continue;
  11898. if (itemdb_type(id) != IT_CARD) {
  11899. if (sd->inventory_data[index]->nameid != id)
  11900. continue;
  11901. flag = 1;
  11902. break;
  11903. } else { //Cards
  11904. if (sd->inventory_data[index]->slot == 0 ||
  11905. itemdb_isspecial(sd->status.inventory[index].card[0]))
  11906. continue;
  11907. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  11908. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  11909. unsigned int hash = 0;
  11910. if (sd->status.inventory[index].card[k] != id)
  11911. continue;
  11912. hash = 1<<((j<5?j:j-5)*4 + k);
  11913. // check if card is already used by another set
  11914. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  11915. continue;
  11916. // We have found a match
  11917. flag = 1;
  11918. // Set hash so this card cannot be used by another
  11919. if (j<5)
  11920. sd->bonus.setitem_hash |= hash;
  11921. else
  11922. sd->bonus.setitem_hash2 |= hash;
  11923. break;
  11924. }
  11925. }
  11926. if (flag) break; //Card found
  11927. }
  11928. if (ret == -1)
  11929. ret = flag;
  11930. else
  11931. ret &= flag;
  11932. if (!ret) break;
  11933. }
  11934. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  11935. sd->bonus.setitem_hash = setitem_hash;
  11936. sd->bonus.setitem_hash2 = setitem_hash2;
  11937. }
  11938. script_pushint(st,ret);
  11939. return 0;
  11940. }
  11941. /*================================================
  11942. * Check how many given inserted cards in the CURRENT
  11943. * weapon - used for 2/15's cards patch [Lupus]
  11944. *------------------------------------------------*/
  11945. BUILDIN_FUNC(cardscnt)
  11946. {
  11947. TBL_PC *sd;
  11948. int i, k, id = 1;
  11949. int ret = 0;
  11950. int index;
  11951. sd = script_rid2sd(st);
  11952. for (i=0; id!=0; i++) {
  11953. FETCH (i+2, id) else id = 0;
  11954. if (id <= 0)
  11955. continue;
  11956. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  11957. if(index < 0) continue;
  11958. if(!sd->inventory_data[index])
  11959. continue;
  11960. if(itemdb_type(id) != IT_CARD) {
  11961. if (sd->inventory_data[index]->nameid == id)
  11962. ret+= sd->status.inventory[index].amount;
  11963. } else {
  11964. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11965. continue;
  11966. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11967. if (sd->status.inventory[index].card[k] == id)
  11968. ret++;
  11969. }
  11970. }
  11971. }
  11972. script_pushint(st,ret);
  11973. // script_pushint(st,current_equip_item_index);
  11974. return 0;
  11975. }
  11976. /*=======================================================
  11977. * Returns the refined number of the current item, or an
  11978. * item with inventory index specified
  11979. *-------------------------------------------------------*/
  11980. BUILDIN_FUNC(getrefine)
  11981. {
  11982. TBL_PC *sd;
  11983. if ((sd = script_rid2sd(st))!= NULL)
  11984. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  11985. else
  11986. script_pushint(st,0);
  11987. return 0;
  11988. }
  11989. /*=======================================================
  11990. * Day/Night controls
  11991. *-------------------------------------------------------*/
  11992. BUILDIN_FUNC(night)
  11993. {
  11994. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  11995. return 0;
  11996. }
  11997. BUILDIN_FUNC(day)
  11998. {
  11999. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12000. return 0;
  12001. }
  12002. //=======================================================
  12003. // Unequip [Spectre]
  12004. //-------------------------------------------------------
  12005. BUILDIN_FUNC(unequip)
  12006. {
  12007. int i;
  12008. size_t num;
  12009. TBL_PC *sd;
  12010. num = script_getnum(st,2);
  12011. sd = script_rid2sd(st);
  12012. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  12013. {
  12014. i = pc_checkequip(sd,equip[num-1]);
  12015. if (i >= 0)
  12016. pc_unequipitem(sd,i,1|2);
  12017. }
  12018. return 0;
  12019. }
  12020. BUILDIN_FUNC(equip)
  12021. {
  12022. int nameid=0,i;
  12023. TBL_PC *sd;
  12024. struct item_data *item_data;
  12025. sd = script_rid2sd(st);
  12026. nameid=script_getnum(st,2);
  12027. if((item_data = itemdb_exists(nameid)) == NULL)
  12028. {
  12029. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  12030. return 1;
  12031. }
  12032. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  12033. if( i < MAX_INVENTORY )
  12034. pc_equipitem(sd,i,item_data->equip);
  12035. return 0;
  12036. }
  12037. BUILDIN_FUNC(autoequip)
  12038. {
  12039. int nameid, flag;
  12040. struct item_data *item_data;
  12041. nameid=script_getnum(st,2);
  12042. flag=script_getnum(st,3);
  12043. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  12044. {
  12045. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  12046. return 1;
  12047. }
  12048. if( !itemdb_isequip2(item_data) )
  12049. {
  12050. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  12051. return 1;
  12052. }
  12053. item_data->flag.autoequip = flag>0?1:0;
  12054. return 0;
  12055. }
  12056. BUILDIN_FUNC(setbattleflag)
  12057. {
  12058. const char *flag, *value;
  12059. flag = script_getstr(st,2);
  12060. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  12061. if (battle_set_value(flag, value) == 0)
  12062. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  12063. else
  12064. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  12065. return 0;
  12066. }
  12067. BUILDIN_FUNC(getbattleflag)
  12068. {
  12069. const char *flag;
  12070. flag = script_getstr(st,2);
  12071. script_pushint(st,battle_get_value(flag));
  12072. return 0;
  12073. }
  12074. //=======================================================
  12075. // strlen [Valaris]
  12076. //-------------------------------------------------------
  12077. BUILDIN_FUNC(getstrlen)
  12078. {
  12079. const char *str = script_getstr(st,2);
  12080. int len = (str) ? (int)strlen(str) : 0;
  12081. script_pushint(st,len);
  12082. return 0;
  12083. }
  12084. //=======================================================
  12085. // isalpha [Valaris]
  12086. //-------------------------------------------------------
  12087. BUILDIN_FUNC(charisalpha)
  12088. {
  12089. const char *str=script_getstr(st,2);
  12090. int pos=script_getnum(st,3);
  12091. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  12092. script_pushint(st,val);
  12093. return 0;
  12094. }
  12095. //=======================================================
  12096. // charisupper <str>, <index>
  12097. //-------------------------------------------------------
  12098. BUILDIN_FUNC(charisupper)
  12099. {
  12100. const char *str = script_getstr(st,2);
  12101. int pos = script_getnum(st,3);
  12102. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  12103. script_pushint(st,val);
  12104. return 0;
  12105. }
  12106. //=======================================================
  12107. // charislower <str>, <index>
  12108. //-------------------------------------------------------
  12109. BUILDIN_FUNC(charislower)
  12110. {
  12111. const char *str = script_getstr(st,2);
  12112. int pos = script_getnum(st,3);
  12113. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  12114. script_pushint(st,val);
  12115. return 0;
  12116. }
  12117. //=======================================================
  12118. // charat <str>, <index>
  12119. //-------------------------------------------------------
  12120. BUILDIN_FUNC(charat) {
  12121. const char *str = script_getstr(st,2);
  12122. int pos = script_getnum(st,3);
  12123. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  12124. char output[2];
  12125. output[0] = str[pos];
  12126. output[1] = '\0';
  12127. script_pushstrcopy(st, output);
  12128. } else
  12129. script_pushconststr(st, "");
  12130. return 0;
  12131. }
  12132. //=======================================================
  12133. // setchar <string>, <char>, <index>
  12134. //-------------------------------------------------------
  12135. BUILDIN_FUNC(setchar)
  12136. {
  12137. const char *str = script_getstr(st,2);
  12138. const char *c = script_getstr(st,3);
  12139. int index = script_getnum(st,4);
  12140. char *output = aStrdup(str);
  12141. if(index >= 0 && index < strlen(output))
  12142. output[index] = *c;
  12143. script_pushstr(st, output);
  12144. return 0;
  12145. }
  12146. //=======================================================
  12147. // insertchar <string>, <char>, <index>
  12148. //-------------------------------------------------------
  12149. BUILDIN_FUNC(insertchar)
  12150. {
  12151. const char *str = script_getstr(st,2);
  12152. const char *c = script_getstr(st,3);
  12153. int index = script_getnum(st,4);
  12154. char *output;
  12155. size_t len = strlen(str);
  12156. if(index < 0)
  12157. index = 0;
  12158. else if(index > len)
  12159. index = len;
  12160. output = (char*)aMalloc(len + 2);
  12161. memcpy(output, str, index);
  12162. output[index] = c[0];
  12163. memcpy(&output[index+1], &str[index], len - index);
  12164. output[len+1] = '\0';
  12165. script_pushstr(st, output);
  12166. return 0;
  12167. }
  12168. //=======================================================
  12169. // delchar <string>, <index>
  12170. //-------------------------------------------------------
  12171. BUILDIN_FUNC(delchar)
  12172. {
  12173. const char *str = script_getstr(st,2);
  12174. int index = script_getnum(st,3);
  12175. char *output;
  12176. size_t len = strlen(str);
  12177. if(index < 0 || index > len) {
  12178. //return original
  12179. output = aStrdup(str);
  12180. script_pushstr(st, output);
  12181. return 0;
  12182. }
  12183. output = (char*)aMalloc(len);
  12184. memcpy(output, str, index);
  12185. memcpy(&output[index], &str[index+1], len - index);
  12186. script_pushstr(st, output);
  12187. return 0;
  12188. }
  12189. //=======================================================
  12190. // strtoupper <str>
  12191. //-------------------------------------------------------
  12192. BUILDIN_FUNC(strtoupper)
  12193. {
  12194. const char *str = script_getstr(st,2);
  12195. char *output = aStrdup(str);
  12196. char *cursor = output;
  12197. while (*cursor != '\0') {
  12198. *cursor = TOUPPER(*cursor);
  12199. cursor++;
  12200. }
  12201. script_pushstr(st, output);
  12202. return 0;
  12203. }
  12204. //=======================================================
  12205. // strtolower <str>
  12206. //-------------------------------------------------------
  12207. BUILDIN_FUNC(strtolower)
  12208. {
  12209. const char *str = script_getstr(st,2);
  12210. char *output = aStrdup(str);
  12211. char *cursor = output;
  12212. while (*cursor != '\0') {
  12213. *cursor = TOLOWER(*cursor);
  12214. cursor++;
  12215. }
  12216. script_pushstr(st, output);
  12217. return 0;
  12218. }
  12219. //=======================================================
  12220. // substr <str>, <start>, <end>
  12221. //-------------------------------------------------------
  12222. BUILDIN_FUNC(substr)
  12223. {
  12224. const char *str = script_getstr(st,2);
  12225. char *output;
  12226. int start = script_getnum(st,3);
  12227. int end = script_getnum(st,4);
  12228. int len = 0;
  12229. if(start >= 0 && end < strlen(str) && start <= end) {
  12230. len = end - start + 1;
  12231. output = (char*)aMalloc(len + 1);
  12232. memcpy(output, &str[start], len);
  12233. } else
  12234. output = (char*)aMalloc(1);
  12235. output[len] = '\0';
  12236. script_pushstr(st, output);
  12237. return 0;
  12238. }
  12239. //=======================================================
  12240. // explode <dest_string_array>, <str>, <delimiter>
  12241. // Note: delimiter is limited to 1 char
  12242. //-------------------------------------------------------
  12243. BUILDIN_FUNC(explode)
  12244. {
  12245. struct script_data* data = script_getdata(st, 2);
  12246. const char *str = script_getstr(st,3);
  12247. const char delimiter = script_getstr(st, 4)[0];
  12248. int32 id;
  12249. size_t len = strlen(str);
  12250. int i = 0, j = 0;
  12251. int start;
  12252. char *temp;
  12253. const char* name;
  12254. TBL_PC* sd = NULL;
  12255. temp = (char*)aMalloc(len + 1);
  12256. if( !data_isreference(data) )
  12257. {
  12258. ShowError("script:explode: not a variable\n");
  12259. script_reportdata(data);
  12260. st->state = END;
  12261. return 1;// not a variable
  12262. }
  12263. id = reference_getid(data);
  12264. start = reference_getindex(data);
  12265. name = reference_getname(data);
  12266. if( not_array_variable(*name) )
  12267. {
  12268. ShowError("script:explode: illegal scope\n");
  12269. script_reportdata(data);
  12270. st->state = END;
  12271. return 1;// not supported
  12272. }
  12273. if( !is_string_variable(name) )
  12274. {
  12275. ShowError("script:explode: not string array\n");
  12276. script_reportdata(data);
  12277. st->state = END;
  12278. return 1;// data type mismatch
  12279. }
  12280. if( not_server_variable(*name) )
  12281. {
  12282. sd = script_rid2sd(st);
  12283. if( sd == NULL )
  12284. return 0;// no player attached
  12285. }
  12286. while(str[i] != '\0') {
  12287. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  12288. temp[j] = '\0';
  12289. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  12290. j = 0;
  12291. ++i;
  12292. } else {
  12293. temp[j++] = str[i++];
  12294. }
  12295. }
  12296. //set last string
  12297. temp[j] = '\0';
  12298. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  12299. aFree(temp);
  12300. return 0;
  12301. }
  12302. //=======================================================
  12303. // implode <string_array>
  12304. // implode <string_array>, <glue>
  12305. //-------------------------------------------------------
  12306. BUILDIN_FUNC(implode)
  12307. {
  12308. struct script_data* data = script_getdata(st, 2);
  12309. const char *glue = NULL, *name, *temp;
  12310. int32 glue_len = 0, array_size, id;
  12311. size_t len = 0;
  12312. int i, k = 0;
  12313. TBL_PC* sd = NULL;
  12314. char *output;
  12315. if( !data_isreference(data) )
  12316. {
  12317. ShowError("script:implode: not a variable\n");
  12318. script_reportdata(data);
  12319. st->state = END;
  12320. return 1;// not a variable
  12321. }
  12322. id = reference_getid(data);
  12323. name = reference_getname(data);
  12324. if( not_array_variable(*name) )
  12325. {
  12326. ShowError("script:implode: illegal scope\n");
  12327. script_reportdata(data);
  12328. st->state = END;
  12329. return 1;// not supported
  12330. }
  12331. if( !is_string_variable(name) )
  12332. {
  12333. ShowError("script:implode: not string array\n");
  12334. script_reportdata(data);
  12335. st->state = END;
  12336. return 1;// data type mismatch
  12337. }
  12338. if( not_server_variable(*name) )
  12339. {
  12340. sd = script_rid2sd(st);
  12341. if( sd == NULL )
  12342. return 0;// no player attached
  12343. }
  12344. //count chars
  12345. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  12346. if(array_size == -1) //empty array check (AmsTaff)
  12347. {
  12348. ShowWarning("script:implode: array length = 0\n");
  12349. output = (char*)aMalloc(sizeof(char)*5);
  12350. sprintf(output,"%s","NULL");
  12351. } else {
  12352. for(i = 0; i <= array_size; ++i) {
  12353. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12354. len += strlen(temp);
  12355. script_removetop(st, -1, 0);
  12356. }
  12357. //allocate mem
  12358. if( script_hasdata(st,3) ) {
  12359. glue = script_getstr(st,3);
  12360. glue_len = strlen(glue);
  12361. len += glue_len * (array_size);
  12362. }
  12363. output = (char*)aMalloc(len + 1);
  12364. //build output
  12365. for(i = 0; i < array_size; ++i) {
  12366. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12367. len = strlen(temp);
  12368. memcpy(&output[k], temp, len);
  12369. k += len;
  12370. if(glue_len != 0) {
  12371. memcpy(&output[k], glue, glue_len);
  12372. k += glue_len;
  12373. }
  12374. script_removetop(st, -1, 0);
  12375. }
  12376. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  12377. len = strlen(temp);
  12378. memcpy(&output[k], temp, len);
  12379. k += len;
  12380. script_removetop(st, -1, 0);
  12381. output[k] = '\0';
  12382. }
  12383. script_pushstr(st, output);
  12384. return 0;
  12385. }
  12386. //=======================================================
  12387. // sprintf(<format>, ...);
  12388. // Implements C sprintf, except format %n. The resulting string is
  12389. // returned, instead of being saved in variable by reference.
  12390. //-------------------------------------------------------
  12391. BUILDIN_FUNC(sprintf)
  12392. {
  12393. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  12394. const char* format;
  12395. char* p;
  12396. char* q;
  12397. char* buf = NULL;
  12398. char* buf2 = NULL;
  12399. struct script_data* data;
  12400. StringBuf final_buf;
  12401. // Fetch init data
  12402. format = script_getstr(st, 2);
  12403. argc = script_lastdata(st)-2;
  12404. len = strlen(format);
  12405. // Skip parsing, where no parsing is required.
  12406. if(len==0){
  12407. script_pushconststr(st,"");
  12408. return 0;
  12409. }
  12410. // Pessimistic alloc
  12411. CREATE(buf, char, len+1);
  12412. // Need not be parsed, just solve stuff like %%.
  12413. if(argc==0){
  12414. memcpy(buf,format,len+1);
  12415. script_pushstrcopy(st, buf);
  12416. aFree(buf);
  12417. return 0;
  12418. }
  12419. safestrncpy(buf, format, len+1);
  12420. // Issue sprintf for each parameter
  12421. StringBuf_Init(&final_buf);
  12422. q = buf;
  12423. while((p = strchr(q, '%'))!=NULL){
  12424. if(p!=q){
  12425. len = p-q+1;
  12426. if(buf2_len<len){
  12427. RECREATE(buf2, char, len);
  12428. buf2_len = len;
  12429. }
  12430. safestrncpy(buf2, q, len);
  12431. StringBuf_AppendStr(&final_buf, buf2);
  12432. q = p;
  12433. }
  12434. p = q+1;
  12435. if(*p=='%'){ // %%
  12436. StringBuf_AppendStr(&final_buf, "%");
  12437. q+=2;
  12438. continue;
  12439. }
  12440. if(*p=='n'){ // %n
  12441. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  12442. script_reportsrc(st);
  12443. q+=2;
  12444. continue;
  12445. }
  12446. if(arg>=argc){
  12447. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  12448. if(buf) aFree(buf);
  12449. if(buf2) aFree(buf2);
  12450. StringBuf_Destroy(&final_buf);
  12451. script_pushconststr(st,"");
  12452. return 1;
  12453. }
  12454. if((p = strchr(q+1, '%'))==NULL){
  12455. p = strchr(q, 0); // EOS
  12456. }
  12457. len = p-q+1;
  12458. if(buf2_len<len){
  12459. RECREATE(buf2, char, len);
  12460. buf2_len = len;
  12461. }
  12462. safestrncpy(buf2, q, len);
  12463. q = p;
  12464. // Note: This assumes the passed value being the correct
  12465. // type to the current format specifier. If not, the server
  12466. // probably crashes or returns anything else, than expected,
  12467. // but it would behave in normal code the same way so it's
  12468. // the scripter's responsibility.
  12469. data = script_getdata(st, arg+3);
  12470. if(data_isstring(data)){ // String
  12471. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12472. }else if(data_isint(data)){ // Number
  12473. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12474. }else if(data_isreference(data)){ // Variable
  12475. char* name = reference_getname(data);
  12476. if(name[strlen(name)-1]=='$'){ // var Str
  12477. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12478. }else{ // var Int
  12479. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12480. }
  12481. }else{ // Unsupported type
  12482. ShowError("buildin_sprintf: Unknown argument type!\n");
  12483. if(buf) aFree(buf);
  12484. if(buf2) aFree(buf2);
  12485. StringBuf_Destroy(&final_buf);
  12486. script_pushconststr(st,"");
  12487. return 1;
  12488. }
  12489. arg++;
  12490. }
  12491. // Append anything left
  12492. if(*q){
  12493. StringBuf_AppendStr(&final_buf, q);
  12494. }
  12495. // Passed more, than needed
  12496. if(arg<argc){
  12497. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  12498. script_reportsrc(st);
  12499. }
  12500. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  12501. if(buf) aFree(buf);
  12502. if(buf2) aFree(buf2);
  12503. StringBuf_Destroy(&final_buf);
  12504. return 0;
  12505. }
  12506. //=======================================================
  12507. // sscanf(<str>, <format>, ...);
  12508. // Implements C sscanf.
  12509. //-------------------------------------------------------
  12510. BUILDIN_FUNC(sscanf){
  12511. unsigned int argc, arg = 0, len;
  12512. struct script_data* data;
  12513. struct map_session_data* sd = NULL;
  12514. const char* str;
  12515. const char* format;
  12516. const char* p;
  12517. const char* q;
  12518. char* buf = NULL;
  12519. char* buf_p;
  12520. char* ref_str = NULL;
  12521. int ref_int;
  12522. // Get data
  12523. str = script_getstr(st, 2);
  12524. format = script_getstr(st, 3);
  12525. argc = script_lastdata(st)-3;
  12526. len = strlen(format);
  12527. CREATE(buf, char, len*2+1);
  12528. // Issue sscanf for each parameter
  12529. *buf = 0;
  12530. q = format;
  12531. while((p = strchr(q, '%'))){
  12532. if(p!=q){
  12533. strncat(buf, q, (size_t)(p-q));
  12534. q = p;
  12535. }
  12536. p = q+1;
  12537. if(*p=='*' || *p=='%'){ // Skip
  12538. strncat(buf, q, 2);
  12539. q+=2;
  12540. continue;
  12541. }
  12542. if(arg>=argc){
  12543. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  12544. script_pushint(st, -1);
  12545. if(buf) aFree(buf);
  12546. if(ref_str) aFree(ref_str);
  12547. return 1;
  12548. }
  12549. if((p = strchr(q+1, '%'))==NULL){
  12550. p = strchr(q, 0); // EOS
  12551. }
  12552. len = p-q;
  12553. strncat(buf, q, len);
  12554. q = p;
  12555. // Validate output
  12556. data = script_getdata(st, arg+4);
  12557. if(!data_isreference(data) || !reference_tovariable(data)){
  12558. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  12559. script_pushint(st, -1);
  12560. if(buf) aFree(buf);
  12561. if(ref_str) aFree(ref_str);
  12562. return 1;
  12563. }
  12564. buf_p = reference_getname(data);
  12565. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  12566. script_pushint(st, -1);
  12567. if(buf) aFree(buf);
  12568. if(ref_str) aFree(ref_str);
  12569. return 0;
  12570. }
  12571. // Save value if any
  12572. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  12573. if(ref_str==NULL){
  12574. CREATE(ref_str, char, strlen(str)+1);
  12575. }
  12576. if(sscanf(str, buf, ref_str)==0){
  12577. break;
  12578. }
  12579. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  12580. } else { // Number
  12581. if(sscanf(str, buf, &ref_int)==0){
  12582. break;
  12583. }
  12584. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  12585. }
  12586. arg++;
  12587. // Disable used format (%... -> %*...)
  12588. buf_p = strchr(buf, 0);
  12589. memmove(buf_p-len+2, buf_p-len+1, len);
  12590. *(buf_p-len+1) = '*';
  12591. }
  12592. script_pushint(st, arg);
  12593. if(buf) aFree(buf);
  12594. if(ref_str) aFree(ref_str);
  12595. return 0;
  12596. }
  12597. //=======================================================
  12598. // strpos(<haystack>, <needle>)
  12599. // strpos(<haystack>, <needle>, <offset>)
  12600. //
  12601. // Implements PHP style strpos. Adapted from code from
  12602. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  12603. //-------------------------------------------------------
  12604. BUILDIN_FUNC(strpos) {
  12605. const char *haystack = script_getstr(st,2);
  12606. const char *needle = script_getstr(st,3);
  12607. int i;
  12608. size_t len;
  12609. if( script_hasdata(st,4) )
  12610. i = script_getnum(st,4);
  12611. else
  12612. i = 0;
  12613. if (needle[0] == '\0') {
  12614. script_pushint(st, -1);
  12615. return 0;
  12616. }
  12617. len = strlen(haystack);
  12618. for ( ; i < len; ++i ) {
  12619. if ( haystack[i] == *needle ) {
  12620. // matched starting char -- loop through remaining chars
  12621. const char *h, *n;
  12622. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  12623. if ( *h != *n ) {
  12624. break;
  12625. }
  12626. }
  12627. if ( !*n ) { // matched all of 'needle' to null termination
  12628. script_pushint(st, i);
  12629. return 0;
  12630. }
  12631. }
  12632. }
  12633. script_pushint(st, -1);
  12634. return 0;
  12635. }
  12636. //===============================================================
  12637. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  12638. //
  12639. // Note: Finds all instances of <search> in <input> and replaces
  12640. // with <replace>. If specified will only replace as many
  12641. // instances as specified in <count>. By default will be case
  12642. // sensitive.
  12643. //---------------------------------------------------------------
  12644. BUILDIN_FUNC(replacestr)
  12645. {
  12646. const char *input = script_getstr(st, 2);
  12647. const char *find = script_getstr(st, 3);
  12648. const char *replace = script_getstr(st, 4);
  12649. size_t inputlen = strlen(input);
  12650. size_t findlen = strlen(find);
  12651. struct StringBuf output;
  12652. bool usecase = true;
  12653. int count = 0;
  12654. int numFinds = 0;
  12655. int i = 0, f = 0;
  12656. if(findlen == 0) {
  12657. ShowError("script:replacestr: Invalid search length.\n");
  12658. st->state = END;
  12659. return 1;
  12660. }
  12661. if(script_hasdata(st, 5)) {
  12662. if( !script_isstring(st,5) )
  12663. usecase = script_getnum(st, 5) != 0;
  12664. else {
  12665. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  12666. st->state = END;
  12667. return 1;
  12668. }
  12669. }
  12670. if(script_hasdata(st, 6)) {
  12671. count = script_getnum(st, 6);
  12672. if(count == 0) {
  12673. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  12674. st->state = END;
  12675. return 1;
  12676. }
  12677. }
  12678. StringBuf_Init(&output);
  12679. for(; i < inputlen; i++) {
  12680. if(count && count == numFinds) { //found enough, stop looking
  12681. break;
  12682. }
  12683. for(f = 0; f <= findlen; f++) {
  12684. if(f == findlen) { //complete match
  12685. numFinds++;
  12686. StringBuf_AppendStr(&output, replace);
  12687. i += findlen - 1;
  12688. break;
  12689. } else {
  12690. if(usecase) {
  12691. if((i + f) > inputlen || input[i + f] != find[f]) {
  12692. StringBuf_Printf(&output, "%c", input[i]);
  12693. break;
  12694. }
  12695. } else {
  12696. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12697. StringBuf_Printf(&output, "%c", input[i]);
  12698. break;
  12699. }
  12700. }
  12701. }
  12702. }
  12703. }
  12704. //append excess after enough found
  12705. if(i < inputlen)
  12706. StringBuf_AppendStr(&output, &(input[i]));
  12707. script_pushstrcopy(st, StringBuf_Value(&output));
  12708. StringBuf_Destroy(&output);
  12709. return 0;
  12710. }
  12711. //========================================================
  12712. // countstr <input>, <search>{, <usecase>}
  12713. //
  12714. // Note: Counts the number of times <search> occurs in
  12715. // <input>. By default will be case sensitive.
  12716. //--------------------------------------------------------
  12717. BUILDIN_FUNC(countstr)
  12718. {
  12719. const char *input = script_getstr(st, 2);
  12720. const char *find = script_getstr(st, 3);
  12721. size_t inputlen = strlen(input);
  12722. size_t findlen = strlen(find);
  12723. bool usecase = true;
  12724. int numFinds = 0;
  12725. int i = 0, f = 0;
  12726. if(findlen == 0) {
  12727. ShowError("script:countstr: Invalid search length.\n");
  12728. st->state = END;
  12729. return 1;
  12730. }
  12731. if(script_hasdata(st, 4)) {
  12732. if( !script_isstring(st,4) )
  12733. usecase = script_getnum(st, 4) != 0;
  12734. else {
  12735. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  12736. st->state = END;
  12737. return 1;
  12738. }
  12739. }
  12740. for(; i < inputlen; i++) {
  12741. for(f = 0; f <= findlen; f++) {
  12742. if(f == findlen) { //complete match
  12743. numFinds++;
  12744. i += findlen - 1;
  12745. break;
  12746. } else {
  12747. if(usecase) {
  12748. if((i + f) > inputlen || input[i + f] != find[f]) {
  12749. break;
  12750. }
  12751. } else {
  12752. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12753. break;
  12754. }
  12755. }
  12756. }
  12757. }
  12758. }
  12759. script_pushint(st, numFinds);
  12760. return 0;
  12761. }
  12762. /// Changes the display name and/or display class of the npc.
  12763. /// Returns 0 is successful, 1 if the npc does not exist.
  12764. ///
  12765. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  12766. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  12767. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  12768. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  12769. BUILDIN_FUNC(setnpcdisplay)
  12770. {
  12771. const char* name;
  12772. const char* newname = NULL;
  12773. int class_ = -1, size = -1;
  12774. struct script_data* data;
  12775. struct npc_data* nd;
  12776. name = script_getstr(st,2);
  12777. data = script_getdata(st,3);
  12778. if( script_hasdata(st,4) )
  12779. class_ = script_getnum(st,4);
  12780. if( script_hasdata(st,5) )
  12781. size = script_getnum(st,5);
  12782. get_val(st, data);
  12783. if( data_isstring(data) )
  12784. newname = conv_str(st,data);
  12785. else if( data_isint(data) )
  12786. class_ = conv_num(st,data);
  12787. else
  12788. {
  12789. ShowError("script:setnpcdisplay: expected string or number\n");
  12790. script_reportdata(data);
  12791. return 1;
  12792. }
  12793. nd = npc_name2id(name);
  12794. if( nd == NULL )
  12795. {// not found
  12796. script_pushint(st,1);
  12797. return 0;
  12798. }
  12799. // update npc
  12800. if( newname )
  12801. npc_setdisplayname(nd, newname);
  12802. if( size != -1 && size != (int)nd->size )
  12803. nd->size = size;
  12804. else
  12805. size = -1;
  12806. if( class_ != -1 && nd->class_ != class_ )
  12807. npc_setclass(nd, class_);
  12808. else if( size != -1 )
  12809. { // Required to update the visual size
  12810. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  12811. clif_spawn(&nd->bl);
  12812. }
  12813. script_pushint(st,0);
  12814. return 0;
  12815. }
  12816. BUILDIN_FUNC(atoi)
  12817. {
  12818. const char *value;
  12819. value = script_getstr(st,2);
  12820. script_pushint(st,atoi(value));
  12821. return 0;
  12822. }
  12823. // case-insensitive substring search [lordalfa]
  12824. BUILDIN_FUNC(compare)
  12825. {
  12826. const char *message;
  12827. const char *cmpstring;
  12828. message = script_getstr(st,2);
  12829. cmpstring = script_getstr(st,3);
  12830. script_pushint(st,(stristr(message,cmpstring) != NULL));
  12831. return 0;
  12832. }
  12833. // [zBuffer] List of mathematics commands --->
  12834. BUILDIN_FUNC(sqrt)
  12835. {
  12836. double i, a;
  12837. i = script_getnum(st,2);
  12838. a = sqrt(i);
  12839. script_pushint(st,(int)a);
  12840. return 0;
  12841. }
  12842. BUILDIN_FUNC(pow)
  12843. {
  12844. double i, a, b;
  12845. a = script_getnum(st,2);
  12846. b = script_getnum(st,3);
  12847. i = pow(a,b);
  12848. script_pushint(st,(int)i);
  12849. return 0;
  12850. }
  12851. BUILDIN_FUNC(distance)
  12852. {
  12853. int x0, y0, x1, y1;
  12854. x0 = script_getnum(st,2);
  12855. y0 = script_getnum(st,3);
  12856. x1 = script_getnum(st,4);
  12857. y1 = script_getnum(st,5);
  12858. script_pushint(st,distance_xy(x0,y0,x1,y1));
  12859. return 0;
  12860. }
  12861. // <--- [zBuffer] List of mathematics commands
  12862. BUILDIN_FUNC(md5)
  12863. {
  12864. const char *tmpstr;
  12865. char *md5str;
  12866. tmpstr = script_getstr(st,2);
  12867. md5str = (char *)aMalloc((32+1)*sizeof(char));
  12868. MD5_String(tmpstr, md5str);
  12869. script_pushstr(st, md5str);
  12870. return 0;
  12871. }
  12872. // [zBuffer] List of dynamic var commands --->
  12873. BUILDIN_FUNC(setd)
  12874. {
  12875. TBL_PC *sd=NULL;
  12876. char varname[100];
  12877. const char *buffer;
  12878. int elem;
  12879. buffer = script_getstr(st, 2);
  12880. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  12881. elem = 0;
  12882. if( not_server_variable(*varname) )
  12883. {
  12884. sd = script_rid2sd(st);
  12885. if( sd == NULL )
  12886. {
  12887. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  12888. return 0;
  12889. }
  12890. }
  12891. if( is_string_variable(varname) ) {
  12892. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  12893. } else {
  12894. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  12895. }
  12896. return 0;
  12897. }
  12898. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  12899. {
  12900. int i, j;
  12901. TBL_PC* sd = NULL;
  12902. const char* query;
  12903. struct script_data* data;
  12904. const char* name;
  12905. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  12906. int num_vars;
  12907. int num_cols;
  12908. // check target variables
  12909. for( i = 3; script_hasdata(st,i); ++i ) {
  12910. data = script_getdata(st, i);
  12911. if( data_isreference(data) ) { // it's a variable
  12912. name = reference_getname(data);
  12913. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  12914. sd = script_rid2sd(st);
  12915. if( sd == NULL ) { // no player attached
  12916. script_reportdata(data);
  12917. st->state = END;
  12918. return 1;
  12919. }
  12920. }
  12921. if( not_array_variable(*name) )
  12922. max_rows = 1;// not an array, limit to one row
  12923. } else {
  12924. ShowError("script:query_sql: not a variable\n");
  12925. script_reportdata(data);
  12926. st->state = END;
  12927. return 1;
  12928. }
  12929. }
  12930. num_vars = i - 3;
  12931. // Execute the query
  12932. query = script_getstr(st,2);
  12933. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  12934. Sql_ShowDebug(handle);
  12935. script_pushint(st, 0);
  12936. return 1;
  12937. }
  12938. if( Sql_NumRows(handle) == 0 ) { // No data received
  12939. Sql_FreeResult(handle);
  12940. script_pushint(st, 0);
  12941. return 0;
  12942. }
  12943. // Count the number of columns to store
  12944. num_cols = Sql_NumColumns(handle);
  12945. if( num_vars < num_cols ) {
  12946. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  12947. script_reportsrc(st);
  12948. } else if( num_vars > num_cols ) {
  12949. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  12950. script_reportsrc(st);
  12951. }
  12952. // Store data
  12953. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  12954. for( j = 0; j < num_vars; ++j ) {
  12955. char* str = NULL;
  12956. if( j < num_cols )
  12957. Sql_GetData(handle, j, &str, NULL);
  12958. data = script_getdata(st, j+3);
  12959. name = reference_getname(data);
  12960. if( is_string_variable(name) )
  12961. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  12962. else
  12963. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  12964. }
  12965. }
  12966. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  12967. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  12968. script_reportsrc(st);
  12969. }
  12970. // Free data
  12971. Sql_FreeResult(handle);
  12972. script_pushint(st, i);
  12973. return 0;
  12974. }
  12975. BUILDIN_FUNC(query_sql) {
  12976. #ifdef BETA_THREAD_TEST
  12977. if( st->state != RERUNLINE ) {
  12978. queryThread_add(st,false);
  12979. st->state = RERUNLINE;/* will continue when the query is finished running. */
  12980. } else
  12981. st->state = RUN;
  12982. return 0;
  12983. #else
  12984. return buildin_query_sql_sub(st, mmysql_handle);
  12985. #endif
  12986. }
  12987. BUILDIN_FUNC(query_logsql) {
  12988. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  12989. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  12990. script_pushint(st,-1);
  12991. return 1;
  12992. }
  12993. #ifdef BETA_THREAD_TEST
  12994. if( st->state != RERUNLINE ) {
  12995. queryThread_add(st,true);
  12996. st->state = RERUNLINE;/* will continue when the query is finished running. */
  12997. } else
  12998. st->state = RUN;
  12999. return 0;
  13000. #else
  13001. return buildin_query_sql_sub(st, logmysql_handle);
  13002. #endif
  13003. }
  13004. //Allows escaping of a given string.
  13005. BUILDIN_FUNC(escape_sql)
  13006. {
  13007. const char *str;
  13008. char *esc_str;
  13009. size_t len;
  13010. str = script_getstr(st,2);
  13011. len = strlen(str);
  13012. esc_str = (char*)aMalloc(len*2+1);
  13013. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  13014. script_pushstr(st, esc_str);
  13015. return 0;
  13016. }
  13017. BUILDIN_FUNC(getd)
  13018. {
  13019. char varname[100];
  13020. const char *buffer;
  13021. int elem;
  13022. buffer = script_getstr(st, 2);
  13023. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  13024. elem = 0;
  13025. // Push the 'pointer' so it's more flexible [Lance]
  13026. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  13027. return 0;
  13028. }
  13029. // <--- [zBuffer] List of dynamic var commands
  13030. // Pet stat [Lance]
  13031. BUILDIN_FUNC(petstat)
  13032. {
  13033. TBL_PC *sd = NULL;
  13034. struct pet_data *pd;
  13035. int flag = script_getnum(st,2);
  13036. sd = script_rid2sd(st);
  13037. if(!sd || !sd->status.pet_id || !sd->pd){
  13038. if(flag == 2)
  13039. script_pushconststr(st, "");
  13040. else
  13041. script_pushint(st,0);
  13042. return 0;
  13043. }
  13044. pd = sd->pd;
  13045. switch(flag){
  13046. case 1: script_pushint(st,(int)pd->pet.class_); break;
  13047. case 2: script_pushstrcopy(st, pd->pet.name); break;
  13048. case 3: script_pushint(st,(int)pd->pet.level); break;
  13049. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  13050. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  13051. default:
  13052. script_pushint(st,0);
  13053. break;
  13054. }
  13055. return 0;
  13056. }
  13057. BUILDIN_FUNC(callshop)
  13058. {
  13059. TBL_PC *sd = NULL;
  13060. struct npc_data *nd;
  13061. const char *shopname;
  13062. int flag = 0;
  13063. sd = script_rid2sd(st);
  13064. if (!sd) {
  13065. script_pushint(st,0);
  13066. return 0;
  13067. }
  13068. shopname = script_getstr(st, 2);
  13069. if( script_hasdata(st,3) )
  13070. flag = script_getnum(st,3);
  13071. nd = npc_name2id(shopname);
  13072. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  13073. {
  13074. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  13075. script_pushint(st,0);
  13076. return 1;
  13077. }
  13078. if( nd->subtype == SHOP )
  13079. {
  13080. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  13081. sd->state.callshop = 1;
  13082. switch( flag )
  13083. {
  13084. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  13085. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  13086. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  13087. }
  13088. }
  13089. else
  13090. clif_cashshop_show(sd, nd);
  13091. sd->npc_shopid = nd->bl.id;
  13092. script_pushint(st,1);
  13093. return 0;
  13094. }
  13095. BUILDIN_FUNC(npcshopitem)
  13096. {
  13097. const char* npcname = script_getstr(st, 2);
  13098. struct npc_data* nd = npc_name2id(npcname);
  13099. int n, i;
  13100. int amount;
  13101. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  13102. { //Not found.
  13103. script_pushint(st,0);
  13104. return 0;
  13105. }
  13106. // get the count of new entries
  13107. amount = (script_lastdata(st)-2)/2;
  13108. // generate new shop item list
  13109. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  13110. for( n = 0, i = 3; n < amount; n++, i+=2 )
  13111. {
  13112. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13113. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13114. }
  13115. nd->u.shop.count = n;
  13116. script_pushint(st,1);
  13117. return 0;
  13118. }
  13119. BUILDIN_FUNC(npcshopadditem)
  13120. {
  13121. const char* npcname = script_getstr(st,2);
  13122. struct npc_data* nd = npc_name2id(npcname);
  13123. int n, i;
  13124. int amount;
  13125. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  13126. { //Not found.
  13127. script_pushint(st,0);
  13128. return 0;
  13129. }
  13130. // get the count of new entries
  13131. amount = (script_lastdata(st)-2)/2;
  13132. // append new items to existing shop item list
  13133. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  13134. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  13135. {
  13136. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13137. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13138. }
  13139. nd->u.shop.count = n;
  13140. script_pushint(st,1);
  13141. return 0;
  13142. }
  13143. BUILDIN_FUNC(npcshopdelitem)
  13144. {
  13145. const char* npcname = script_getstr(st,2);
  13146. struct npc_data* nd = npc_name2id(npcname);
  13147. unsigned int nameid;
  13148. int n, i;
  13149. int amount;
  13150. int size;
  13151. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  13152. { //Not found.
  13153. script_pushint(st,0);
  13154. return 0;
  13155. }
  13156. amount = script_lastdata(st)-2;
  13157. size = nd->u.shop.count;
  13158. // remove specified items from the shop item list
  13159. for( i = 3; i < 3 + amount; i++ )
  13160. {
  13161. nameid = script_getnum(st,i);
  13162. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  13163. if( n < size )
  13164. {
  13165. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  13166. size--;
  13167. }
  13168. }
  13169. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  13170. nd->u.shop.count = size;
  13171. script_pushint(st,1);
  13172. return 0;
  13173. }
  13174. //Sets a script to attach to a shop npc.
  13175. BUILDIN_FUNC(npcshopattach)
  13176. {
  13177. const char* npcname = script_getstr(st,2);
  13178. struct npc_data* nd = npc_name2id(npcname);
  13179. int flag = 1;
  13180. if( script_hasdata(st,3) )
  13181. flag = script_getnum(st,3);
  13182. if( !nd || nd->subtype != SHOP )
  13183. { //Not found.
  13184. script_pushint(st,0);
  13185. return 0;
  13186. }
  13187. if (flag)
  13188. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  13189. else
  13190. nd->master_nd = NULL;
  13191. script_pushint(st,1);
  13192. return 0;
  13193. }
  13194. /*==========================================
  13195. * Returns some values of an item [Lupus]
  13196. * Price, Weight, etc...
  13197. setitemscript(itemID,"{new item bonus script}",[n]);
  13198. Where n:
  13199. 0 - script
  13200. 1 - Equip script
  13201. 2 - Unequip script
  13202. *------------------------------------------*/
  13203. BUILDIN_FUNC(setitemscript)
  13204. {
  13205. int item_id,n=0;
  13206. const char *script;
  13207. struct item_data *i_data;
  13208. struct script_code **dstscript;
  13209. item_id = script_getnum(st,2);
  13210. script = script_getstr(st,3);
  13211. if( script_hasdata(st,4) )
  13212. n=script_getnum(st,4);
  13213. i_data = itemdb_exists(item_id);
  13214. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  13215. script_pushint(st,0);
  13216. return 0;
  13217. }
  13218. switch (n) {
  13219. case 2:
  13220. dstscript = &i_data->unequip_script;
  13221. break;
  13222. case 1:
  13223. dstscript = &i_data->equip_script;
  13224. break;
  13225. default:
  13226. dstscript = &i_data->script;
  13227. break;
  13228. }
  13229. if(*dstscript)
  13230. script_free_code(*dstscript);
  13231. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  13232. script_pushint(st,1);
  13233. return 0;
  13234. }
  13235. /*=======================================================
  13236. * Add or Update a mob drop temporarily [Akinari]
  13237. * Original Idea By: [Lupus]
  13238. *
  13239. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  13240. *
  13241. * If given an item the mob already drops, the rate
  13242. * is updated to the new rate. Rate cannot exceed 10000
  13243. * Returns 1 if succeeded (added/updated a mob drop)
  13244. *-------------------------------------------------------*/
  13245. BUILDIN_FUNC(addmonsterdrop)
  13246. {
  13247. struct mob_db *mob;
  13248. int item_id,rate,i,c = 0;
  13249. if(script_isstring(st,2))
  13250. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13251. else
  13252. mob = mob_db(script_getnum(st,2));
  13253. item_id=script_getnum(st,3);
  13254. rate=script_getnum(st,4);
  13255. if(!itemdb_exists(item_id)){
  13256. ShowError("addmonsterdrop: Nonexistant item %d requested.\n", item_id );
  13257. return 1;
  13258. }
  13259. if(mob) { //We got a valid monster, check for available drop slot
  13260. for(i = 0; i < MAX_MOB_DROP; i++) {
  13261. if(mob->dropitem[i].nameid) {
  13262. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  13263. c = i;
  13264. break;
  13265. }
  13266. continue;
  13267. }
  13268. if(!c) //Accept first available slot only
  13269. c = i;
  13270. }
  13271. if(c) { //Fill in the slot with the item and rate
  13272. mob->dropitem[c].nameid = item_id;
  13273. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  13274. script_pushint(st,1);
  13275. } else //No place to put the new drop
  13276. script_pushint(st,0);
  13277. } else {
  13278. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13279. return 1;
  13280. }
  13281. return 0;
  13282. }
  13283. /*=======================================================
  13284. * Delete a mob drop temporarily [Akinari]
  13285. * Original Idea By: [Lupus]
  13286. *
  13287. * delmonsterdrop <mob_id or name>,<item_id>;
  13288. *
  13289. * Returns 1 if succeeded (deleted a mob drop)
  13290. *-------------------------------------------------------*/
  13291. BUILDIN_FUNC(delmonsterdrop)
  13292. {
  13293. struct mob_db *mob;
  13294. int item_id,i;
  13295. if(script_isstring(st,2))
  13296. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13297. else
  13298. mob = mob_db(script_getnum(st,2));
  13299. item_id=script_getnum(st,3);
  13300. if(!itemdb_exists(item_id)){
  13301. ShowError("delmonsterdrop: Nonexistant item %d requested.\n", item_id );
  13302. return 1;
  13303. }
  13304. if(mob) { //We got a valid monster, check for item drop on monster
  13305. for(i = 0; i < MAX_MOB_DROP; i++) {
  13306. if(mob->dropitem[i].nameid == item_id) {
  13307. mob->dropitem[i].nameid = 0;
  13308. mob->dropitem[i].p = 0;
  13309. script_pushint(st,1);
  13310. return 0;
  13311. }
  13312. }
  13313. //No drop on that monster
  13314. script_pushint(st,0);
  13315. } else {
  13316. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13317. return 1;
  13318. }
  13319. return 0;
  13320. }
  13321. /*==========================================
  13322. * Returns some values of a monster [Lupus]
  13323. * Name, Level, race, size, etc...
  13324. getmonsterinfo(monsterID,queryIndex);
  13325. *------------------------------------------*/
  13326. BUILDIN_FUNC(getmonsterinfo)
  13327. {
  13328. struct mob_db *mob;
  13329. int mob_id;
  13330. mob_id = script_getnum(st,2);
  13331. if (!mobdb_checkid(mob_id)) {
  13332. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  13333. if ( !script_getnum(st,3) ) //requested a string
  13334. script_pushconststr(st,"null");
  13335. else
  13336. script_pushint(st,-1);
  13337. return -1;
  13338. }
  13339. mob = mob_db(mob_id);
  13340. switch ( script_getnum(st,3) ) {
  13341. case 0: script_pushstrcopy(st,mob->jname); break;
  13342. case 1: script_pushint(st,mob->lv); break;
  13343. case 2: script_pushint(st,mob->status.max_hp); break;
  13344. case 3: script_pushint(st,mob->base_exp); break;
  13345. case 4: script_pushint(st,mob->job_exp); break;
  13346. case 5: script_pushint(st,mob->status.rhw.atk); break;
  13347. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  13348. case 7: script_pushint(st,mob->status.def); break;
  13349. case 8: script_pushint(st,mob->status.mdef); break;
  13350. case 9: script_pushint(st,mob->status.str); break;
  13351. case 10: script_pushint(st,mob->status.agi); break;
  13352. case 11: script_pushint(st,mob->status.vit); break;
  13353. case 12: script_pushint(st,mob->status.int_); break;
  13354. case 13: script_pushint(st,mob->status.dex); break;
  13355. case 14: script_pushint(st,mob->status.luk); break;
  13356. case 15: script_pushint(st,mob->status.rhw.range); break;
  13357. case 16: script_pushint(st,mob->range2); break;
  13358. case 17: script_pushint(st,mob->range3); break;
  13359. case 18: script_pushint(st,mob->status.size); break;
  13360. case 19: script_pushint(st,mob->status.race); break;
  13361. case 20: script_pushint(st,mob->status.def_ele); break;
  13362. case 21: script_pushint(st,mob->status.mode); break;
  13363. case 22: script_pushint(st,mob->mexp); break;
  13364. default: script_pushint(st,-1); //wrong Index
  13365. }
  13366. return 0;
  13367. }
  13368. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  13369. {
  13370. TBL_PC *sd = NULL;
  13371. if(script_hasdata(st,2))
  13372. sd = map_nick2sd(script_getstr(st,2));
  13373. else
  13374. sd = script_rid2sd(st);
  13375. if(sd)
  13376. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  13377. else
  13378. script_pushint(st,0);
  13379. return 0;
  13380. }
  13381. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  13382. {
  13383. TBL_PC *sd = NULL;
  13384. if(script_hasdata(st,2))
  13385. sd = map_nick2sd(script_getstr(st,2));
  13386. else
  13387. sd = script_rid2sd(st);
  13388. if(sd)
  13389. script_pushint(st,(sd->chatID != 0));
  13390. else
  13391. script_pushint(st,0);
  13392. return 0;
  13393. }
  13394. BUILDIN_FUNC(checkidle)
  13395. {
  13396. TBL_PC *sd = NULL;
  13397. if (script_hasdata(st, 2))
  13398. sd = map_nick2sd(script_getstr(st, 2));
  13399. else
  13400. sd = script_rid2sd(st);
  13401. if (sd)
  13402. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  13403. else
  13404. script_pushint(st, 0);
  13405. return 0;
  13406. }
  13407. BUILDIN_FUNC(searchitem)
  13408. {
  13409. struct script_data* data = script_getdata(st, 2);
  13410. const char *itemname = script_getstr(st,3);
  13411. struct item_data *items[MAX_SEARCH];
  13412. int count;
  13413. char* name;
  13414. int32 start;
  13415. int32 id;
  13416. int32 i;
  13417. TBL_PC* sd = NULL;
  13418. if ((items[0] = itemdb_exists(atoi(itemname))))
  13419. count = 1;
  13420. else {
  13421. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  13422. if (count > MAX_SEARCH) count = MAX_SEARCH;
  13423. }
  13424. if (!count) {
  13425. script_pushint(st, 0);
  13426. return 0;
  13427. }
  13428. if( !data_isreference(data) )
  13429. {
  13430. ShowError("script:searchitem: not a variable\n");
  13431. script_reportdata(data);
  13432. st->state = END;
  13433. return 1;// not a variable
  13434. }
  13435. id = reference_getid(data);
  13436. start = reference_getindex(data);
  13437. name = reference_getname(data);
  13438. if( not_array_variable(*name) )
  13439. {
  13440. ShowError("script:searchitem: illegal scope\n");
  13441. script_reportdata(data);
  13442. st->state = END;
  13443. return 1;// not supported
  13444. }
  13445. if( not_server_variable(*name) )
  13446. {
  13447. sd = script_rid2sd(st);
  13448. if( sd == NULL )
  13449. return 0;// no player attached
  13450. }
  13451. if( is_string_variable(name) )
  13452. {// string array
  13453. ShowError("script:searchitem: not an integer array reference\n");
  13454. script_reportdata(data);
  13455. st->state = END;
  13456. return 1;// not supported
  13457. }
  13458. for( i = 0; i < count; ++start, ++i )
  13459. {// Set array
  13460. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  13461. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  13462. }
  13463. script_pushint(st, count);
  13464. return 0;
  13465. }
  13466. int axtoi(const char *hexStg)
  13467. {
  13468. int n = 0; // position in string
  13469. int16 m = 0; // position in digit[] to shift
  13470. int count; // loop index
  13471. int intValue = 0; // integer value of hex string
  13472. int digit[11]; // hold values to convert
  13473. while (n < 10) {
  13474. if (hexStg[n]=='\0')
  13475. break;
  13476. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  13477. digit[n] = hexStg[n] & 0x0f; //convert to int
  13478. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  13479. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13480. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  13481. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13482. else break;
  13483. n++;
  13484. }
  13485. count = n;
  13486. m = n - 1;
  13487. n = 0;
  13488. while(n < count) {
  13489. // digit[n] is value of hex digit at position n
  13490. // (m << 2) is the number of positions to shift
  13491. // OR the bits into return value
  13492. intValue = intValue | (digit[n] << (m << 2));
  13493. m--; // adjust the position to set
  13494. n++; // next digit to process
  13495. }
  13496. return (intValue);
  13497. }
  13498. // [Lance] Hex string to integer converter
  13499. BUILDIN_FUNC(axtoi)
  13500. {
  13501. const char *hex = script_getstr(st,2);
  13502. script_pushint(st,axtoi(hex));
  13503. return 0;
  13504. }
  13505. // [zBuffer] List of player cont commands --->
  13506. BUILDIN_FUNC(rid2name)
  13507. {
  13508. struct block_list *bl = NULL;
  13509. int rid = script_getnum(st,2);
  13510. if((bl = map_id2bl(rid)))
  13511. {
  13512. switch(bl->type) {
  13513. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  13514. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  13515. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  13516. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  13517. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  13518. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  13519. default:
  13520. ShowError("buildin_rid2name: BL type unknown.\n");
  13521. script_pushconststr(st,"");
  13522. break;
  13523. }
  13524. } else {
  13525. ShowError("buildin_rid2name: invalid RID\n");
  13526. script_pushconststr(st,"(null)");
  13527. }
  13528. return 0;
  13529. }
  13530. BUILDIN_FUNC(pcblockmove)
  13531. {
  13532. int id, flag;
  13533. TBL_PC *sd = NULL;
  13534. id = script_getnum(st,2);
  13535. flag = script_getnum(st,3);
  13536. if(id)
  13537. sd = map_id2sd(id);
  13538. else
  13539. sd = script_rid2sd(st);
  13540. if(sd)
  13541. sd->state.blockedmove = flag > 0;
  13542. return 0;
  13543. }
  13544. BUILDIN_FUNC(pcfollow)
  13545. {
  13546. int id, targetid;
  13547. TBL_PC *sd = NULL;
  13548. id = script_getnum(st,2);
  13549. targetid = script_getnum(st,3);
  13550. if(id)
  13551. sd = map_id2sd(id);
  13552. else
  13553. sd = script_rid2sd(st);
  13554. if(sd)
  13555. pc_follow(sd, targetid);
  13556. return 0;
  13557. }
  13558. BUILDIN_FUNC(pcstopfollow)
  13559. {
  13560. int id;
  13561. TBL_PC *sd = NULL;
  13562. id = script_getnum(st,2);
  13563. if(id)
  13564. sd = map_id2sd(id);
  13565. else
  13566. sd = script_rid2sd(st);
  13567. if(sd)
  13568. pc_stop_following(sd);
  13569. return 0;
  13570. }
  13571. // <--- [zBuffer] List of player cont commands
  13572. // [zBuffer] List of mob control commands --->
  13573. /// Makes the unit walk to target position or map
  13574. /// Returns if it was successfull
  13575. ///
  13576. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  13577. /// unitwalk(<unit_id>,<target_id>) -> <bool>
  13578. BUILDIN_FUNC(unitwalk)
  13579. {
  13580. struct block_list* bl;
  13581. bl = map_id2bl(script_getnum(st,2));
  13582. if( bl == NULL )
  13583. script_pushint(st, 0);
  13584. else if( script_hasdata(st,4) ) {
  13585. int x = script_getnum(st,3);
  13586. int y = script_getnum(st,4);
  13587. if( script_pushint(st, unit_can_reach_pos(bl,x,y,0)) )
  13588. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  13589. } else {
  13590. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  13591. if( tbl == NULL ) {
  13592. ShowError("script:unitwalk: bad target destination\n");
  13593. script_pushint(st, 0);
  13594. return 1;
  13595. }
  13596. else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  13597. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  13598. }
  13599. return 0;
  13600. }
  13601. /// Kills the unit
  13602. ///
  13603. /// unitkill <unit_id>;
  13604. BUILDIN_FUNC(unitkill)
  13605. {
  13606. struct block_list* bl = map_id2bl(script_getnum(st,2));
  13607. if( bl != NULL )
  13608. status_kill(bl);
  13609. return 0;
  13610. }
  13611. /// Warps the unit to the target position in the target map
  13612. /// Returns if it was successfull
  13613. ///
  13614. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  13615. BUILDIN_FUNC(unitwarp)
  13616. {
  13617. int unit_id;
  13618. int map;
  13619. short x;
  13620. short y;
  13621. struct block_list* bl;
  13622. const char *mapname;
  13623. unit_id = script_getnum(st,2);
  13624. mapname = script_getstr(st, 3);
  13625. x = (short)script_getnum(st,4);
  13626. y = (short)script_getnum(st,5);
  13627. if (!unit_id) //Warp the script's runner
  13628. bl = map_id2bl(st->rid);
  13629. else
  13630. bl = map_id2bl(unit_id);
  13631. if( strcmp(mapname,"this") == 0 )
  13632. map = bl?bl->m:-1;
  13633. else
  13634. map = map_mapname2mapid(mapname);
  13635. if( map >= 0 && bl != NULL )
  13636. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  13637. else
  13638. script_pushint(st, 0);
  13639. return 0;
  13640. }
  13641. /// Makes the unit attack the target.
  13642. /// If the unit is a player and <action type> is not 0, it does a continuous
  13643. /// attack instead of a single attack.
  13644. /// Returns if the request was successful.
  13645. ///
  13646. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  13647. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  13648. BUILDIN_FUNC(unitattack)
  13649. {
  13650. struct block_list* unit_bl;
  13651. struct block_list* target_bl = NULL;
  13652. struct script_data* data;
  13653. int actiontype = 0;
  13654. // get unit
  13655. unit_bl = map_id2bl(script_getnum(st,2));
  13656. if( unit_bl == NULL ) {
  13657. script_pushint(st, 0);
  13658. return 0;
  13659. }
  13660. data = script_getdata(st, 3);
  13661. get_val(st, data);
  13662. if( data_isstring(data) )
  13663. {
  13664. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  13665. if( sd != NULL )
  13666. target_bl = &sd->bl;
  13667. } else
  13668. target_bl = map_id2bl(conv_num(st, data));
  13669. // request the attack
  13670. if( target_bl == NULL )
  13671. {
  13672. script_pushint(st, 0);
  13673. return 0;
  13674. }
  13675. // get actiontype
  13676. if( script_hasdata(st,4) )
  13677. actiontype = script_getnum(st,4);
  13678. switch( unit_bl->type )
  13679. {
  13680. case BL_PC:
  13681. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  13682. script_pushint(st, 1);
  13683. return 0;
  13684. case BL_MOB:
  13685. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  13686. break;
  13687. case BL_PET:
  13688. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  13689. break;
  13690. default:
  13691. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  13692. script_pushint(st, 0);
  13693. return 1;
  13694. }
  13695. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  13696. return 0;
  13697. }
  13698. /// Makes the unit stop attacking and moving
  13699. ///
  13700. /// unitstop <unit_id>;
  13701. BUILDIN_FUNC(unitstop)
  13702. {
  13703. int unit_id;
  13704. struct block_list* bl;
  13705. unit_id = script_getnum(st,2);
  13706. bl = map_id2bl(unit_id);
  13707. if( bl != NULL )
  13708. {
  13709. unit_stop_attack(bl);
  13710. unit_stop_walking(bl,4);
  13711. if( bl->type == BL_MOB )
  13712. ((TBL_MOB*)bl)->target_id = 0;
  13713. }
  13714. return 0;
  13715. }
  13716. /// Makes the unit say the message
  13717. ///
  13718. /// unittalk <unit_id>,"<message>";
  13719. BUILDIN_FUNC(unittalk)
  13720. {
  13721. int unit_id;
  13722. const char* message;
  13723. struct block_list* bl;
  13724. unit_id = script_getnum(st,2);
  13725. message = script_getstr(st, 3);
  13726. bl = map_id2bl(unit_id);
  13727. if( bl != NULL )
  13728. {
  13729. struct StringBuf sbuf;
  13730. StringBuf_Init(&sbuf);
  13731. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  13732. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  13733. StringBuf_Destroy(&sbuf);
  13734. }
  13735. return 0;
  13736. }
  13737. /// Makes the unit do an emotion
  13738. ///
  13739. /// unitemote <unit_id>,<emotion>;
  13740. ///
  13741. /// @see e_* in const.txt
  13742. BUILDIN_FUNC(unitemote)
  13743. {
  13744. int unit_id;
  13745. int emotion;
  13746. struct block_list* bl;
  13747. unit_id = script_getnum(st,2);
  13748. emotion = script_getnum(st,3);
  13749. bl = map_id2bl(unit_id);
  13750. if( bl != NULL )
  13751. clif_emotion(bl, emotion);
  13752. return 0;
  13753. }
  13754. /// Makes the unit cast the skill on the target or self if no target is specified
  13755. ///
  13756. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  13757. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  13758. BUILDIN_FUNC(unitskilluseid)
  13759. {
  13760. int unit_id;
  13761. uint16 skill_id;
  13762. uint16 skill_lv;
  13763. int target_id;
  13764. struct block_list* bl;
  13765. unit_id = script_getnum(st,2);
  13766. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13767. skill_lv = script_getnum(st,4);
  13768. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  13769. bl = map_id2bl(unit_id);
  13770. if( bl != NULL )
  13771. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  13772. return 0;
  13773. }
  13774. /// Makes the unit cast the skill on the target position.
  13775. ///
  13776. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  13777. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  13778. BUILDIN_FUNC(unitskillusepos)
  13779. {
  13780. int unit_id;
  13781. uint16 skill_id;
  13782. uint16 skill_lv;
  13783. int skill_x;
  13784. int skill_y;
  13785. struct block_list* bl;
  13786. unit_id = script_getnum(st,2);
  13787. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13788. skill_lv = script_getnum(st,4);
  13789. skill_x = script_getnum(st,5);
  13790. skill_y = script_getnum(st,6);
  13791. bl = map_id2bl(unit_id);
  13792. if( bl != NULL )
  13793. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  13794. return 0;
  13795. }
  13796. // <--- [zBuffer] List of mob control commands
  13797. /// Pauses the execution of the script, detaching the player
  13798. ///
  13799. /// sleep <mili seconds>;
  13800. BUILDIN_FUNC(sleep)
  13801. {
  13802. int ticks;
  13803. ticks = script_getnum(st,2);
  13804. // detach the player
  13805. script_detach_rid(st);
  13806. if( ticks <= 0 )
  13807. {// do nothing
  13808. }
  13809. else if( st->sleep.tick == 0 )
  13810. {// sleep for the target amount of time
  13811. st->state = RERUNLINE;
  13812. st->sleep.tick = ticks;
  13813. }
  13814. else
  13815. {// sleep time is over
  13816. st->state = RUN;
  13817. st->sleep.tick = 0;
  13818. }
  13819. return 0;
  13820. }
  13821. /// Pauses the execution of the script, keeping the player attached
  13822. /// Returns if a player is still attached
  13823. ///
  13824. /// sleep2(<mili secconds>) -> <bool>
  13825. BUILDIN_FUNC(sleep2)
  13826. {
  13827. int ticks;
  13828. ticks = script_getnum(st,2);
  13829. if( ticks <= 0 )
  13830. {// do nothing
  13831. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13832. }
  13833. else if( !st->sleep.tick )
  13834. {// sleep for the target amount of time
  13835. st->state = RERUNLINE;
  13836. st->sleep.tick = ticks;
  13837. }
  13838. else
  13839. {// sleep time is over
  13840. st->state = RUN;
  13841. st->sleep.tick = 0;
  13842. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13843. }
  13844. return 0;
  13845. }
  13846. /// Awakes all the sleep timers of the target npc
  13847. ///
  13848. /// awake "<npc name>";
  13849. BUILDIN_FUNC(awake)
  13850. {
  13851. struct npc_data* nd;
  13852. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  13853. nd = npc_name2id(script_getstr(st, 2));
  13854. if( nd == NULL ) {
  13855. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  13856. return 1;
  13857. }
  13858. while( node )
  13859. {
  13860. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  13861. {// sleep timer for the npc
  13862. struct script_state* tst = (struct script_state*)node->data;
  13863. TBL_PC* sd = map_id2sd(tst->rid);
  13864. if( tst->sleep.timer == INVALID_TIMER )
  13865. {// already awake ???
  13866. node = node->next;
  13867. continue;
  13868. }
  13869. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  13870. {// char not online anymore / another char of the same account is online - Cancel execution
  13871. tst->state = END;
  13872. tst->rid = 0;
  13873. }
  13874. delete_timer(tst->sleep.timer, run_script_timer);
  13875. node = script_erase_sleepdb(node);
  13876. tst->sleep.timer = INVALID_TIMER;
  13877. if(tst->state != RERUNLINE)
  13878. tst->sleep.tick = 0;
  13879. run_script_main(tst);
  13880. }
  13881. else
  13882. {
  13883. node = node->next;
  13884. }
  13885. }
  13886. return 0;
  13887. }
  13888. /// Returns a reference to a variable of the target NPC.
  13889. /// Returns 0 if an error occurs.
  13890. ///
  13891. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  13892. BUILDIN_FUNC(getvariableofnpc)
  13893. {
  13894. struct script_data* data;
  13895. const char* name;
  13896. struct npc_data* nd;
  13897. data = script_getdata(st,2);
  13898. if( !data_isreference(data) )
  13899. {// Not a reference (aka varaible name)
  13900. ShowError("script:getvariableofnpc: not a variable\n");
  13901. script_reportdata(data);
  13902. script_pushnil(st);
  13903. st->state = END;
  13904. return 1;
  13905. }
  13906. name = reference_getname(data);
  13907. if( *name != '.' || name[1] == '@' )
  13908. {// not a npc variable
  13909. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  13910. script_reportdata(data);
  13911. script_pushnil(st);
  13912. st->state = END;
  13913. return 1;
  13914. }
  13915. nd = npc_name2id(script_getstr(st,3));
  13916. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  13917. {// NPC not found or has no script
  13918. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  13919. script_pushnil(st);
  13920. st->state = END;
  13921. return 1;
  13922. }
  13923. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  13924. return 0;
  13925. }
  13926. /// Opens a warp portal.
  13927. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  13928. ///
  13929. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  13930. ///
  13931. /// @author blackhole89
  13932. BUILDIN_FUNC(warpportal)
  13933. {
  13934. int spx;
  13935. int spy;
  13936. unsigned short mapindex;
  13937. int tpx;
  13938. int tpy;
  13939. struct skill_unit_group* group;
  13940. struct block_list* bl;
  13941. bl = map_id2bl(st->oid);
  13942. if( bl == NULL )
  13943. {
  13944. ShowError("script:warpportal: npc is needed\n");
  13945. return 1;
  13946. }
  13947. spx = script_getnum(st,2);
  13948. spy = script_getnum(st,3);
  13949. mapindex = mapindex_name2id(script_getstr(st, 4));
  13950. tpx = script_getnum(st,5);
  13951. tpy = script_getnum(st,6);
  13952. if( mapindex == 0 )
  13953. return 0;// map not found
  13954. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  13955. if( group == NULL )
  13956. return 0;// failed
  13957. group->val1 = (group->val1<<16)|(short)0;
  13958. group->val2 = (tpx<<16) | tpy;
  13959. group->val3 = mapindex;
  13960. return 0;
  13961. }
  13962. BUILDIN_FUNC(openmail)
  13963. {
  13964. TBL_PC* sd;
  13965. sd = script_rid2sd(st);
  13966. if( sd == NULL )
  13967. return 0;
  13968. mail_openmail(sd);
  13969. return 0;
  13970. }
  13971. BUILDIN_FUNC(openauction)
  13972. {
  13973. TBL_PC* sd;
  13974. sd = script_rid2sd(st);
  13975. if( sd == NULL )
  13976. return 0;
  13977. clif_Auction_openwindow(sd);
  13978. return 0;
  13979. }
  13980. /// Retrieves the value of the specified flag of the specified cell.
  13981. ///
  13982. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  13983. ///
  13984. /// @see cell_chk* constants in const.txt for the types
  13985. BUILDIN_FUNC(checkcell)
  13986. {
  13987. int16 m = map_mapname2mapid(script_getstr(st,2));
  13988. int16 x = script_getnum(st,3);
  13989. int16 y = script_getnum(st,4);
  13990. cell_chk type = (cell_chk)script_getnum(st,5);
  13991. script_pushint(st, map_getcell(m, x, y, type));
  13992. return 0;
  13993. }
  13994. /// Modifies flags of cells in the specified area.
  13995. ///
  13996. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  13997. ///
  13998. /// @see cell_* constants in const.txt for the types
  13999. BUILDIN_FUNC(setcell)
  14000. {
  14001. int16 m = map_mapname2mapid(script_getstr(st,2));
  14002. int16 x1 = script_getnum(st,3);
  14003. int16 y1 = script_getnum(st,4);
  14004. int16 x2 = script_getnum(st,5);
  14005. int16 y2 = script_getnum(st,6);
  14006. cell_t type = (cell_t)script_getnum(st,7);
  14007. bool flag = (bool)script_getnum(st,8);
  14008. int x,y;
  14009. if( x1 > x2 ) swap(x1,x2);
  14010. if( y1 > y2 ) swap(y1,y2);
  14011. for( y = y1; y <= y2; ++y )
  14012. for( x = x1; x <= x2; ++x )
  14013. map_setcell(m, x, y, type, flag);
  14014. return 0;
  14015. }
  14016. /*==========================================
  14017. * Mercenary Commands
  14018. *------------------------------------------*/
  14019. BUILDIN_FUNC(mercenary_create)
  14020. {
  14021. struct map_session_data *sd;
  14022. int class_, contract_time;
  14023. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  14024. return 0;
  14025. class_ = script_getnum(st,2);
  14026. if( !merc_class(class_) )
  14027. return 0;
  14028. contract_time = script_getnum(st,3);
  14029. merc_create(sd, class_, contract_time);
  14030. return 0;
  14031. }
  14032. BUILDIN_FUNC(mercenary_heal)
  14033. {
  14034. struct map_session_data *sd = script_rid2sd(st);
  14035. int hp, sp;
  14036. if( sd == NULL || sd->md == NULL )
  14037. return 0;
  14038. hp = script_getnum(st,2);
  14039. sp = script_getnum(st,3);
  14040. status_heal(&sd->md->bl, hp, sp, 0);
  14041. return 0;
  14042. }
  14043. BUILDIN_FUNC(mercenary_sc_start)
  14044. {
  14045. struct map_session_data *sd = script_rid2sd(st);
  14046. enum sc_type type;
  14047. int tick, val1;
  14048. if( sd == NULL || sd->md == NULL )
  14049. return 0;
  14050. type = (sc_type)script_getnum(st,2);
  14051. tick = script_getnum(st,3);
  14052. val1 = script_getnum(st,4);
  14053. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  14054. return 0;
  14055. }
  14056. BUILDIN_FUNC(mercenary_get_calls)
  14057. {
  14058. struct map_session_data *sd = script_rid2sd(st);
  14059. int guild;
  14060. if( sd == NULL )
  14061. return 0;
  14062. guild = script_getnum(st,2);
  14063. switch( guild )
  14064. {
  14065. case ARCH_MERC_GUILD:
  14066. script_pushint(st,sd->status.arch_calls);
  14067. break;
  14068. case SPEAR_MERC_GUILD:
  14069. script_pushint(st,sd->status.spear_calls);
  14070. break;
  14071. case SWORD_MERC_GUILD:
  14072. script_pushint(st,sd->status.sword_calls);
  14073. break;
  14074. default:
  14075. script_pushint(st,0);
  14076. break;
  14077. }
  14078. return 0;
  14079. }
  14080. BUILDIN_FUNC(mercenary_set_calls)
  14081. {
  14082. struct map_session_data *sd = script_rid2sd(st);
  14083. int guild, value, *calls;
  14084. if( sd == NULL )
  14085. return 0;
  14086. guild = script_getnum(st,2);
  14087. value = script_getnum(st,3);
  14088. switch( guild )
  14089. {
  14090. case ARCH_MERC_GUILD:
  14091. calls = &sd->status.arch_calls;
  14092. break;
  14093. case SPEAR_MERC_GUILD:
  14094. calls = &sd->status.spear_calls;
  14095. break;
  14096. case SWORD_MERC_GUILD:
  14097. calls = &sd->status.sword_calls;
  14098. break;
  14099. default:
  14100. return 0; // Invalid Guild
  14101. }
  14102. *calls += value;
  14103. *calls = cap_value(*calls, 0, INT_MAX);
  14104. return 0;
  14105. }
  14106. BUILDIN_FUNC(mercenary_get_faith)
  14107. {
  14108. struct map_session_data *sd = script_rid2sd(st);
  14109. int guild;
  14110. if( sd == NULL )
  14111. return 0;
  14112. guild = script_getnum(st,2);
  14113. switch( guild )
  14114. {
  14115. case ARCH_MERC_GUILD:
  14116. script_pushint(st,sd->status.arch_faith);
  14117. break;
  14118. case SPEAR_MERC_GUILD:
  14119. script_pushint(st,sd->status.spear_faith);
  14120. break;
  14121. case SWORD_MERC_GUILD:
  14122. script_pushint(st,sd->status.sword_faith);
  14123. break;
  14124. default:
  14125. script_pushint(st,0);
  14126. break;
  14127. }
  14128. return 0;
  14129. }
  14130. BUILDIN_FUNC(mercenary_set_faith)
  14131. {
  14132. struct map_session_data *sd = script_rid2sd(st);
  14133. int guild, value, *calls;
  14134. if( sd == NULL )
  14135. return 0;
  14136. guild = script_getnum(st,2);
  14137. value = script_getnum(st,3);
  14138. switch( guild )
  14139. {
  14140. case ARCH_MERC_GUILD:
  14141. calls = &sd->status.arch_faith;
  14142. break;
  14143. case SPEAR_MERC_GUILD:
  14144. calls = &sd->status.spear_faith;
  14145. break;
  14146. case SWORD_MERC_GUILD:
  14147. calls = &sd->status.sword_faith;
  14148. break;
  14149. default:
  14150. return 0; // Invalid Guild
  14151. }
  14152. *calls += value;
  14153. *calls = cap_value(*calls, 0, INT_MAX);
  14154. if( mercenary_get_guild(sd->md) == guild )
  14155. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  14156. return 0;
  14157. }
  14158. /*------------------------------------------
  14159. * Book Reading
  14160. *------------------------------------------*/
  14161. BUILDIN_FUNC(readbook)
  14162. {
  14163. struct map_session_data *sd;
  14164. int book_id, page;
  14165. if( (sd = script_rid2sd(st)) == NULL )
  14166. return 0;
  14167. book_id = script_getnum(st,2);
  14168. page = script_getnum(st,3);
  14169. clif_readbook(sd->fd, book_id, page);
  14170. return 0;
  14171. }
  14172. /******************
  14173. Questlog script commands
  14174. *******************/
  14175. BUILDIN_FUNC(setquest)
  14176. {
  14177. struct map_session_data *sd = script_rid2sd(st);
  14178. nullpo_ret(sd);
  14179. quest_add(sd, script_getnum(st, 2));
  14180. return 0;
  14181. }
  14182. BUILDIN_FUNC(erasequest)
  14183. {
  14184. struct map_session_data *sd = script_rid2sd(st);
  14185. nullpo_ret(sd);
  14186. quest_delete(sd, script_getnum(st, 2));
  14187. return 0;
  14188. }
  14189. BUILDIN_FUNC(completequest)
  14190. {
  14191. struct map_session_data *sd = script_rid2sd(st);
  14192. nullpo_ret(sd);
  14193. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  14194. return 0;
  14195. }
  14196. BUILDIN_FUNC(changequest)
  14197. {
  14198. struct map_session_data *sd = script_rid2sd(st);
  14199. nullpo_ret(sd);
  14200. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  14201. return 0;
  14202. }
  14203. BUILDIN_FUNC(checkquest)
  14204. {
  14205. struct map_session_data *sd = script_rid2sd(st);
  14206. quest_check_type type = HAVEQUEST;
  14207. nullpo_ret(sd);
  14208. if( script_hasdata(st, 3) )
  14209. type = (quest_check_type)script_getnum(st, 3);
  14210. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  14211. return 0;
  14212. }
  14213. BUILDIN_FUNC(showevent)
  14214. {
  14215. TBL_PC *sd = script_rid2sd(st);
  14216. struct npc_data *nd = map_id2nd(st->oid);
  14217. int state, color;
  14218. if( sd == NULL || nd == NULL )
  14219. return 0;
  14220. state = script_getnum(st, 2);
  14221. color = script_getnum(st, 3);
  14222. if( color < 0 || color > 3 )
  14223. color = 0; // set default color
  14224. clif_quest_show_event(sd, &nd->bl, state, color);
  14225. return 0;
  14226. }
  14227. /*==========================================
  14228. * BattleGround System
  14229. *------------------------------------------*/
  14230. BUILDIN_FUNC(waitingroom2bg)
  14231. {
  14232. struct npc_data *nd;
  14233. struct chat_data *cd;
  14234. const char *map_name, *ev = "", *dev = "";
  14235. int x, y, i, mapindex = 0, bg_id, n;
  14236. struct map_session_data *sd;
  14237. if( script_hasdata(st,7) )
  14238. nd = npc_name2id(script_getstr(st,7));
  14239. else
  14240. nd = (struct npc_data *)map_id2bl(st->oid);
  14241. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  14242. {
  14243. script_pushint(st,0);
  14244. return 0;
  14245. }
  14246. map_name = script_getstr(st,2);
  14247. if( strcmp(map_name,"-") != 0 )
  14248. {
  14249. mapindex = mapindex_name2id(map_name);
  14250. if( mapindex == 0 )
  14251. { // Invalid Map
  14252. script_pushint(st,0);
  14253. return 0;
  14254. }
  14255. }
  14256. x = script_getnum(st,3);
  14257. y = script_getnum(st,4);
  14258. ev = script_getstr(st,5); // Logout Event
  14259. dev = script_getstr(st,6); // Die Event
  14260. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  14261. { // Creation failed
  14262. script_pushint(st,0);
  14263. return 0;
  14264. }
  14265. n = cd->users;
  14266. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  14267. {
  14268. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  14269. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  14270. else
  14271. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  14272. }
  14273. mapreg_setreg(add_str("$@arenamembersnum"), i);
  14274. script_pushint(st,bg_id);
  14275. return 0;
  14276. }
  14277. BUILDIN_FUNC(waitingroom2bg_single)
  14278. {
  14279. const char* map_name;
  14280. struct npc_data *nd;
  14281. struct chat_data *cd;
  14282. struct map_session_data *sd;
  14283. int x, y, mapindex, bg_id;
  14284. bg_id = script_getnum(st,2);
  14285. map_name = script_getstr(st,3);
  14286. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14287. return 0; // Invalid Map
  14288. x = script_getnum(st,4);
  14289. y = script_getnum(st,5);
  14290. nd = npc_name2id(script_getstr(st,6));
  14291. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  14292. return 0;
  14293. if( (sd = cd->usersd[0]) == NULL )
  14294. return 0;
  14295. if( bg_team_join(bg_id, sd) )
  14296. {
  14297. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  14298. script_pushint(st,1);
  14299. }
  14300. else
  14301. script_pushint(st,0);
  14302. return 0;
  14303. }
  14304. BUILDIN_FUNC(bg_team_setxy)
  14305. {
  14306. struct battleground_data *bg;
  14307. int bg_id;
  14308. bg_id = script_getnum(st,2);
  14309. if( (bg = bg_team_search(bg_id)) == NULL )
  14310. return 0;
  14311. bg->x = script_getnum(st,3);
  14312. bg->y = script_getnum(st,4);
  14313. return 0;
  14314. }
  14315. BUILDIN_FUNC(bg_warp)
  14316. {
  14317. int x, y, mapindex, bg_id;
  14318. const char* map_name;
  14319. bg_id = script_getnum(st,2);
  14320. map_name = script_getstr(st,3);
  14321. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14322. return 0; // Invalid Map
  14323. x = script_getnum(st,4);
  14324. y = script_getnum(st,5);
  14325. bg_team_warp(bg_id, mapindex, x, y);
  14326. return 0;
  14327. }
  14328. BUILDIN_FUNC(bg_monster)
  14329. {
  14330. int class_ = 0, x = 0, y = 0, bg_id = 0;
  14331. const char *str,*map, *evt="";
  14332. bg_id = script_getnum(st,2);
  14333. map = script_getstr(st,3);
  14334. x = script_getnum(st,4);
  14335. y = script_getnum(st,5);
  14336. str = script_getstr(st,6);
  14337. class_ = script_getnum(st,7);
  14338. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  14339. check_event(st, evt);
  14340. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  14341. return 0;
  14342. }
  14343. BUILDIN_FUNC(bg_monster_set_team)
  14344. {
  14345. struct mob_data *md;
  14346. struct block_list *mbl;
  14347. int id = script_getnum(st,2),
  14348. bg_id = script_getnum(st,3);
  14349. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  14350. return 0;
  14351. md = (TBL_MOB *)mbl;
  14352. md->bg_id = bg_id;
  14353. mob_stop_attack(md);
  14354. mob_stop_walking(md, 0);
  14355. md->target_id = md->attacked_id = 0;
  14356. clif_charnameack(0, &md->bl);
  14357. return 0;
  14358. }
  14359. BUILDIN_FUNC(bg_leave)
  14360. {
  14361. struct map_session_data *sd = script_rid2sd(st);
  14362. if( sd == NULL || !sd->bg_id )
  14363. return 0;
  14364. bg_team_leave(sd,0);
  14365. return 0;
  14366. }
  14367. BUILDIN_FUNC(bg_destroy)
  14368. {
  14369. int bg_id = script_getnum(st,2);
  14370. bg_team_delete(bg_id);
  14371. return 0;
  14372. }
  14373. BUILDIN_FUNC(bg_getareausers)
  14374. {
  14375. const char *str;
  14376. int16 m, x0, y0, x1, y1;
  14377. int bg_id;
  14378. int i = 0, c = 0;
  14379. struct battleground_data *bg = NULL;
  14380. struct map_session_data *sd;
  14381. bg_id = script_getnum(st,2);
  14382. str = script_getstr(st,3);
  14383. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  14384. {
  14385. script_pushint(st,0);
  14386. return 0;
  14387. }
  14388. x0 = script_getnum(st,4);
  14389. y0 = script_getnum(st,5);
  14390. x1 = script_getnum(st,6);
  14391. y1 = script_getnum(st,7);
  14392. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  14393. {
  14394. if( (sd = bg->members[i].sd) == NULL )
  14395. continue;
  14396. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  14397. continue;
  14398. c++;
  14399. }
  14400. script_pushint(st,c);
  14401. return 0;
  14402. }
  14403. BUILDIN_FUNC(bg_updatescore)
  14404. {
  14405. const char *str;
  14406. int16 m;
  14407. str = script_getstr(st,2);
  14408. if( (m = map_mapname2mapid(str)) < 0 )
  14409. return 0;
  14410. map[m].bgscore_lion = script_getnum(st,3);
  14411. map[m].bgscore_eagle = script_getnum(st,4);
  14412. clif_bg_updatescore(m);
  14413. return 0;
  14414. }
  14415. BUILDIN_FUNC(bg_get_data)
  14416. {
  14417. struct battleground_data *bg;
  14418. int bg_id = script_getnum(st,2),
  14419. type = script_getnum(st,3);
  14420. if( (bg = bg_team_search(bg_id)) == NULL )
  14421. {
  14422. script_pushint(st,0);
  14423. return 0;
  14424. }
  14425. switch( type )
  14426. {
  14427. case 0: script_pushint(st, bg->count); break;
  14428. default:
  14429. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  14430. break;
  14431. }
  14432. return 0;
  14433. }
  14434. /*==========================================
  14435. * Instancing System
  14436. *------------------------------------------*/
  14437. //Returns an Instance ID
  14438. //Checks NPC first, then if player is attached we check party
  14439. int script_instancegetid(struct script_state* st)
  14440. {
  14441. short instance_id = 0;
  14442. struct npc_data *nd;
  14443. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  14444. instance_id = nd->instance_id;
  14445. else {
  14446. struct map_session_data *sd;
  14447. struct party_data *p;
  14448. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14449. instance_id = p->instance_id;
  14450. }
  14451. return instance_id;
  14452. }
  14453. /*==========================================
  14454. * Creates the instance
  14455. * Returns Instance ID if created successfully
  14456. *------------------------------------------*/
  14457. BUILDIN_FUNC(instance_create)
  14458. {
  14459. struct map_session_data *sd;
  14460. if((sd = script_rid2sd(st)) == NULL)
  14461. return -1;
  14462. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  14463. return 0;
  14464. }
  14465. /*==========================================
  14466. * Destroys an instance (unofficial)
  14467. * Officially instances are only destroyed by timeout
  14468. *
  14469. * instance_destroy {<instance_id>};
  14470. *------------------------------------------*/
  14471. BUILDIN_FUNC(instance_destroy)
  14472. {
  14473. short instance_id;
  14474. if( script_hasdata(st,2) )
  14475. instance_id = script_getnum(st,2);
  14476. else
  14477. instance_id = script_instancegetid(st);
  14478. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  14479. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  14480. return 0;
  14481. }
  14482. instance_destroy(instance_id);
  14483. return 0;
  14484. }
  14485. /*==========================================
  14486. * Warps player to instance
  14487. * Results:
  14488. * 0: Success
  14489. * 1: Character not in party
  14490. * 2: Party doesn't have instance
  14491. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  14492. *------------------------------------------*/
  14493. BUILDIN_FUNC(instance_enter)
  14494. {
  14495. struct map_session_data *sd;
  14496. if((sd = script_rid2sd(st)) != NULL)
  14497. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  14498. else
  14499. return 1;
  14500. return 0;
  14501. }
  14502. /*==========================================
  14503. * Returns the name of a duplicated NPC
  14504. *
  14505. * instance_npcname <npc_name>{,<instance_id>};
  14506. * <npc_name> is the full name of an NPC.
  14507. *------------------------------------------*/
  14508. BUILDIN_FUNC(instance_npcname)
  14509. {
  14510. const char *str;
  14511. short instance_id = 0;
  14512. struct npc_data *nd;
  14513. str = script_getstr(st,2);
  14514. if( script_hasdata(st,3) )
  14515. instance_id = script_getnum(st,3);
  14516. else
  14517. instance_id = script_instancegetid(st);
  14518. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  14519. static char npcname[NAME_LENGTH];
  14520. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  14521. script_pushconststr(st,npcname);
  14522. } else {
  14523. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  14524. st->state = END;
  14525. return 1;
  14526. }
  14527. return 0;
  14528. }
  14529. /*==========================================
  14530. * Returns the name of a duplicated map
  14531. *
  14532. * instance_mapname <map_name>{,<instance_id>};
  14533. *------------------------------------------*/
  14534. BUILDIN_FUNC(instance_mapname)
  14535. {
  14536. const char *str;
  14537. int16 m;
  14538. short instance_id = 0;
  14539. str = script_getstr(st,2);
  14540. if( script_hasdata(st,3) )
  14541. instance_id = script_getnum(st,3);
  14542. else
  14543. instance_id = script_instancegetid(st);
  14544. // Check that instance mapname is a valid map
  14545. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  14546. script_pushconststr(st, "");
  14547. else
  14548. script_pushconststr(st, map[m].name);
  14549. return 0;
  14550. }
  14551. /*==========================================
  14552. * Returns an Instance ID
  14553. *------------------------------------------*/
  14554. BUILDIN_FUNC(instance_id)
  14555. {
  14556. short instance_id;
  14557. instance_id = script_instancegetid(st);
  14558. if(!instance_id) {
  14559. //ShowError("script:instance_id: No instance attached to NPC or player");
  14560. script_pushint(st, 0);
  14561. return 1;
  14562. }
  14563. script_pushint(st, instance_id);
  14564. return 0;
  14565. }
  14566. /*==========================================
  14567. * Warps all players inside an instance
  14568. *
  14569. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  14570. *------------------------------------------*/
  14571. BUILDIN_FUNC(instance_warpall)
  14572. {
  14573. struct party_data *p;
  14574. struct map_session_data *pl_sd;
  14575. int16 m, i;
  14576. short instance_id;
  14577. const char *mapn;
  14578. int x, y;
  14579. mapn = script_getstr(st,2);
  14580. x = script_getnum(st,3);
  14581. y = script_getnum(st,4);
  14582. if( script_hasdata(st,5) )
  14583. instance_id = script_getnum(st,5);
  14584. else
  14585. instance_id = script_instancegetid(st);
  14586. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  14587. return 0;
  14588. if( !(p = party_search(instance_data[instance_id].party_id)) )
  14589. return 0;
  14590. for( i = 0; i < MAX_PARTY; i++ )
  14591. if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == instance_id ) pc_setpos(pl_sd,map_id2index(m),x,y,CLR_TELEPORT);
  14592. return 0;
  14593. }
  14594. /*==========================================
  14595. * instance_check_party [malufett]
  14596. * Values:
  14597. * party_id : Party ID of the invoking character. [Required Parameter]
  14598. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  14599. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  14600. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  14601. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  14602. * Example 2: instance_check_party (getcharid(1),1,1,99);
  14603. *------------------------------------------*/
  14604. BUILDIN_FUNC(instance_check_party)
  14605. {
  14606. struct map_session_data *pl_sd;
  14607. int amount, min, max, i, party_id, c = 0;
  14608. struct party_data *p = NULL;
  14609. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  14610. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  14611. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  14612. if( min < 1 || min > MAX_LEVEL) {
  14613. ShowError("script:check_party: Invalid min level, %d\n", min);
  14614. return 1;
  14615. } else if( max < 1 || max > MAX_LEVEL) {
  14616. ShowError("script:check_party: Invalid max level, %d\n", max);
  14617. return 1;
  14618. }
  14619. if( script_hasdata(st,2) )
  14620. party_id = script_getnum(st,2);
  14621. else return 0;
  14622. if( !(p = party_search(party_id)) ) {
  14623. script_pushint(st, 0); // Returns false if party does not exist.
  14624. return 0;
  14625. }
  14626. for( i = 0; i < MAX_PARTY; i++ )
  14627. if( (pl_sd = p->data[i].sd) )
  14628. if(map_id2bl(pl_sd->bl.id)) {
  14629. if(pl_sd->status.base_level < min) {
  14630. script_pushint(st, 0);
  14631. return 0;
  14632. } else if(pl_sd->status.base_level > max) {
  14633. script_pushint(st, 0);
  14634. return 0;
  14635. }
  14636. c++;
  14637. }
  14638. if(c < amount)
  14639. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  14640. else
  14641. script_pushint(st, 1);
  14642. return 0;
  14643. }
  14644. /*==========================================
  14645. * Custom Fonts
  14646. *------------------------------------------*/
  14647. BUILDIN_FUNC(setfont)
  14648. {
  14649. struct map_session_data *sd = script_rid2sd(st);
  14650. int font = script_getnum(st,2);
  14651. if( sd == NULL )
  14652. return 0;
  14653. if( sd->user_font != font )
  14654. sd->user_font = font;
  14655. else
  14656. sd->user_font = 0;
  14657. clif_font(sd);
  14658. return 0;
  14659. }
  14660. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  14661. {
  14662. TBL_MOB* md = (TBL_MOB*)bl;
  14663. struct block_list *tbl;
  14664. int mobid = va_arg(ap,int);
  14665. uint16 skill_id = va_arg(ap,int);
  14666. uint16 skill_lv = va_arg(ap,int);
  14667. int casttime = va_arg(ap,int);
  14668. int cancel = va_arg(ap,int);
  14669. int emotion = va_arg(ap,int);
  14670. int target = va_arg(ap,int);
  14671. if( md->class_ != mobid )
  14672. return 0;
  14673. // 0:self, 1:target, 2:master, default:random
  14674. switch( target )
  14675. {
  14676. case 0: tbl = map_id2bl(md->bl.id); break;
  14677. case 1: tbl = map_id2bl(md->target_id); break;
  14678. case 2: tbl = map_id2bl(md->master_id); break;
  14679. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  14680. }
  14681. if( !tbl )
  14682. return 0;
  14683. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  14684. unit_skillcastcancel(bl,0);
  14685. if( skill_get_casttype(skill_id) == CAST_GROUND )
  14686. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  14687. else
  14688. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  14689. clif_emotion(&md->bl, emotion);
  14690. return 0;
  14691. }
  14692. /*==========================================
  14693. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  14694. *------------------------------------------*/
  14695. BUILDIN_FUNC(areamobuseskill)
  14696. {
  14697. struct block_list center;
  14698. int16 m;
  14699. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  14700. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  14701. ShowError("areamobuseskill: invalid map name.\n");
  14702. return 0;
  14703. }
  14704. center.m = m;
  14705. center.x = script_getnum(st,3);
  14706. center.y = script_getnum(st,4);
  14707. range = script_getnum(st,5);
  14708. mobid = script_getnum(st,6);
  14709. skill_id = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  14710. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  14711. skill_lv = battle_config.mob_max_skilllvl;
  14712. casttime = script_getnum(st,9);
  14713. cancel = script_getnum(st,10);
  14714. emotion = script_getnum(st,11);
  14715. target = script_getnum(st,12);
  14716. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  14717. return 0;
  14718. }
  14719. BUILDIN_FUNC(progressbar)
  14720. {
  14721. struct map_session_data * sd = script_rid2sd(st);
  14722. const char * color;
  14723. unsigned int second;
  14724. if( !st || !sd )
  14725. return 0;
  14726. st->state = STOP;
  14727. color = script_getstr(st,2);
  14728. second = script_getnum(st,3);
  14729. sd->progressbar.npc_id = st->oid;
  14730. sd->progressbar.timeout = gettick() + second*1000;
  14731. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  14732. return 0;
  14733. }
  14734. BUILDIN_FUNC(pushpc)
  14735. {
  14736. uint8 dir;
  14737. int cells, dx, dy;
  14738. struct map_session_data* sd;
  14739. if((sd = script_rid2sd(st))==NULL)
  14740. {
  14741. return 0;
  14742. }
  14743. dir = script_getnum(st,2);
  14744. cells = script_getnum(st,3);
  14745. if(dir>7)
  14746. {
  14747. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  14748. script_reportsrc(st);
  14749. dir%= 8; // trim spin-over
  14750. }
  14751. if(!cells)
  14752. {// zero distance
  14753. return 0;
  14754. }
  14755. else if(cells<0)
  14756. {// pushing backwards
  14757. dir = (dir+4)%8; // turn around
  14758. cells = -cells;
  14759. }
  14760. dx = dirx[dir];
  14761. dy = diry[dir];
  14762. unit_blown(&sd->bl, dx, dy, cells, 0);
  14763. return 0;
  14764. }
  14765. /// Invokes buying store preparation window
  14766. /// buyingstore <slots>;
  14767. BUILDIN_FUNC(buyingstore)
  14768. {
  14769. struct map_session_data* sd;
  14770. if( ( sd = script_rid2sd(st) ) == NULL )
  14771. {
  14772. return 0;
  14773. }
  14774. if( npc_isnear(&sd->bl) ) {
  14775. char output[150];
  14776. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  14777. clif_displaymessage(sd->fd, output);
  14778. return 0;
  14779. }
  14780. buyingstore_setup(sd, script_getnum(st,2));
  14781. return 0;
  14782. }
  14783. /// Invokes search store info window
  14784. /// searchstores <uses>,<effect>;
  14785. BUILDIN_FUNC(searchstores)
  14786. {
  14787. unsigned short effect;
  14788. unsigned int uses;
  14789. struct map_session_data* sd;
  14790. if( ( sd = script_rid2sd(st) ) == NULL )
  14791. {
  14792. return 0;
  14793. }
  14794. uses = script_getnum(st,2);
  14795. effect = script_getnum(st,3);
  14796. if( !uses )
  14797. {
  14798. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  14799. return 1;
  14800. }
  14801. if( effect > 1 )
  14802. {
  14803. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  14804. return 1;
  14805. }
  14806. searchstore_open(sd, uses, effect);
  14807. return 0;
  14808. }
  14809. /// Displays a number as large digital clock.
  14810. /// showdigit <value>[,<type>];
  14811. BUILDIN_FUNC(showdigit)
  14812. {
  14813. unsigned int type = 0;
  14814. int value;
  14815. struct map_session_data* sd;
  14816. if( ( sd = script_rid2sd(st) ) == NULL )
  14817. {
  14818. return 0;
  14819. }
  14820. value = script_getnum(st,2);
  14821. if( script_hasdata(st,3) )
  14822. {
  14823. type = script_getnum(st,3);
  14824. if( type > 3 )
  14825. {
  14826. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  14827. return 1;
  14828. }
  14829. }
  14830. clif_showdigit(sd, (unsigned char)type, value);
  14831. return 0;
  14832. }
  14833. /**
  14834. * Rune Knight
  14835. **/
  14836. BUILDIN_FUNC(makerune) {
  14837. TBL_PC* sd;
  14838. if( (sd = script_rid2sd(st)) == NULL )
  14839. return 0;
  14840. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  14841. sd->itemid = script_getnum(st,2);
  14842. return 0;
  14843. }
  14844. /**
  14845. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  14846. **/
  14847. BUILDIN_FUNC(checkdragon) {
  14848. TBL_PC* sd;
  14849. if( (sd = script_rid2sd(st)) == NULL )
  14850. return 0;
  14851. if( pc_isridingdragon(sd) )
  14852. script_pushint(st,1);
  14853. else
  14854. script_pushint(st,0);
  14855. return 0;
  14856. }
  14857. /**
  14858. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  14859. * - Toggles the dragon on a RK if he can mount;
  14860. * @param Color - when not provided uses the green dragon;
  14861. * - 1 : Green Dragon
  14862. * - 2 : Brown Dragon
  14863. * - 3 : Gray Dragon
  14864. * - 4 : Blue Dragon
  14865. * - 5 : Red Dragon
  14866. **/
  14867. BUILDIN_FUNC(setdragon) {
  14868. TBL_PC* sd;
  14869. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  14870. if( (sd = script_rid2sd(st)) == NULL )
  14871. return 0;
  14872. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  14873. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  14874. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  14875. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  14876. script_pushint(st,1);
  14877. } else {//Not mounted; Mount now.
  14878. unsigned int option = OPTION_DRAGON1;
  14879. if( color ) {
  14880. option = ( color == 1 ? OPTION_DRAGON1 :
  14881. color == 2 ? OPTION_DRAGON2 :
  14882. color == 3 ? OPTION_DRAGON3 :
  14883. color == 4 ? OPTION_DRAGON4 :
  14884. color == 5 ? OPTION_DRAGON5 : 0);
  14885. if( !option ) {
  14886. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  14887. option = OPTION_DRAGON1;
  14888. }
  14889. }
  14890. pc_setoption(sd, sd->sc.option|option);
  14891. script_pushint(st,1);
  14892. }
  14893. return 0;
  14894. }
  14895. /**
  14896. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  14897. **/
  14898. BUILDIN_FUNC(ismounting) {
  14899. TBL_PC* sd;
  14900. if( (sd = script_rid2sd(st)) == NULL )
  14901. return 0;
  14902. if( sd->sc.option&OPTION_MOUNTING )
  14903. script_pushint(st,1);
  14904. else
  14905. script_pushint(st,0);
  14906. return 0;
  14907. }
  14908. /**
  14909. * setmounting() returns 1 on success or 0 otherwise
  14910. * - Toggles new mounts on a player when he can mount
  14911. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  14912. * - Will unmount the player is he is already mounting
  14913. **/
  14914. BUILDIN_FUNC(setmounting) {
  14915. TBL_PC* sd;
  14916. if( (sd = script_rid2sd(st)) == NULL )
  14917. return 0;
  14918. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  14919. script_pushint(st,0);//can't mount with one of these
  14920. else {
  14921. if( sd->sc.option&OPTION_MOUNTING )
  14922. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  14923. else
  14924. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  14925. script_pushint(st,1);//in both cases, return 1.
  14926. }
  14927. return 0;
  14928. }
  14929. /**
  14930. * Retrieves quantity of arguments provided to callfunc/callsub.
  14931. * getargcount() -> amount of arguments received in a function
  14932. **/
  14933. BUILDIN_FUNC(getargcount) {
  14934. struct script_retinfo* ri;
  14935. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  14936. ShowError("script:getargcount: used out of function or callsub label!\n");
  14937. st->state = END;
  14938. return 1;
  14939. }
  14940. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  14941. script_pushint(st, ri->nargs);
  14942. return 0;
  14943. }
  14944. /**
  14945. * getcharip(<account ID>/<character ID>/<character name>)
  14946. **/
  14947. BUILDIN_FUNC(getcharip)
  14948. {
  14949. struct map_session_data* sd = NULL;
  14950. /* check if a character name is specified */
  14951. if( script_hasdata(st, 2) )
  14952. {
  14953. if (script_isstring(st, 2))
  14954. sd = map_nick2sd(script_getstr(st, 2));
  14955. else if (script_isint(st, 2) || script_getnum(st, 2))
  14956. {
  14957. int id = 0;
  14958. id = script_getnum(st, 2);
  14959. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  14960. }
  14961. }
  14962. else
  14963. sd = script_rid2sd(st);
  14964. /* check for sd and IP */
  14965. if (!sd || !session[sd->fd]->client_addr)
  14966. {
  14967. script_pushconststr(st, "");
  14968. return 0;
  14969. }
  14970. /* return the client ip_addr converted for output */
  14971. if (sd && sd->fd && session[sd->fd])
  14972. {
  14973. /* initiliaze */
  14974. const char *ip_addr = NULL;
  14975. uint32 ip;
  14976. /* set ip, ip_addr and convert to ip and push str */
  14977. ip = session[sd->fd]->client_addr;
  14978. ip_addr = ip2str(ip, NULL);
  14979. script_pushstrcopy(st, ip_addr);
  14980. }
  14981. return 0;
  14982. }
  14983. /**
  14984. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  14985. **/
  14986. BUILDIN_FUNC(is_function) {
  14987. const char* str = script_getstr(st,2);
  14988. if( strdb_exists(userfunc_db, str) )
  14989. script_pushint(st,1);
  14990. else
  14991. script_pushint(st,0);
  14992. return 0;
  14993. }
  14994. /**
  14995. * get_revision() -> retrieves the current svn revision (if available)
  14996. **/
  14997. BUILDIN_FUNC(get_revision) {
  14998. const char *svn = get_svn_revision();
  14999. if ( svn[0] != UNKNOWN_VERSION )
  15000. script_pushint(st,atoi(svn));
  15001. else
  15002. script_pushint(st,-1); //unknown
  15003. return 0;
  15004. }
  15005. /**
  15006. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  15007. **/
  15008. BUILDIN_FUNC(freeloop) {
  15009. if( script_getnum(st,2) )
  15010. st->freeloop = 1;
  15011. else
  15012. st->freeloop = 0;
  15013. script_pushint(st, st->freeloop);
  15014. return 0;
  15015. }
  15016. /**
  15017. * @commands (script based)
  15018. **/
  15019. BUILDIN_FUNC(bindatcmd) {
  15020. const char* atcmd;
  15021. const char* eventName;
  15022. int i, level = 0, level2 = 0;
  15023. bool create = false;
  15024. atcmd = script_getstr(st,2);
  15025. eventName = script_getstr(st,3);
  15026. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15027. atcmd++;
  15028. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  15029. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  15030. if( atcmd_binding_count == 0 ) {
  15031. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  15032. create = true;
  15033. } else {
  15034. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  15035. if( i < atcmd_binding_count ) {/* update existent entry */
  15036. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15037. atcmd_binding[i]->level = level;
  15038. atcmd_binding[i]->level2 = level2;
  15039. } else
  15040. create = true;
  15041. }
  15042. if( create ) {
  15043. i = atcmd_binding_count;
  15044. if( atcmd_binding_count++ != 0 )
  15045. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  15046. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  15047. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  15048. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  15049. atcmd_binding[i]->level = level;
  15050. atcmd_binding[i]->level2 = level2;
  15051. }
  15052. return 0;
  15053. }
  15054. BUILDIN_FUNC(unbindatcmd) {
  15055. const char* atcmd;
  15056. int i = 0;
  15057. atcmd = script_getstr(st, 2);
  15058. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  15059. atcmd++;
  15060. if( atcmd_binding_count == 0 ) {
  15061. script_pushint(st, 0);
  15062. return 0;
  15063. }
  15064. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  15065. if( i < atcmd_binding_count ) {
  15066. int cursor = 0;
  15067. aFree(atcmd_binding[i]);
  15068. atcmd_binding[i] = NULL;
  15069. /* compact the list now that we freed a slot somewhere */
  15070. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  15071. if( atcmd_binding[i] == NULL )
  15072. continue;
  15073. if( cursor != i ) {
  15074. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  15075. }
  15076. cursor++;
  15077. }
  15078. if( (atcmd_binding_count = cursor) == 0 )
  15079. aFree(atcmd_binding);
  15080. script_pushint(st, 1);
  15081. } else
  15082. script_pushint(st, 0);/* not found */
  15083. return 0;
  15084. }
  15085. BUILDIN_FUNC(useatcmd)
  15086. {
  15087. TBL_PC dummy_sd;
  15088. TBL_PC* sd;
  15089. int fd;
  15090. const char* cmd;
  15091. cmd = script_getstr(st,2);
  15092. if( st->rid )
  15093. {
  15094. sd = script_rid2sd(st);
  15095. fd = sd->fd;
  15096. }
  15097. else
  15098. { // Use a dummy character.
  15099. sd = &dummy_sd;
  15100. fd = 0;
  15101. memset(&dummy_sd, 0, sizeof(TBL_PC));
  15102. if( st->oid )
  15103. {
  15104. struct block_list* bl = map_id2bl(st->oid);
  15105. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  15106. if( bl->type == BL_NPC )
  15107. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  15108. }
  15109. }
  15110. // compatibility with previous implementation (deprecated!)
  15111. if( cmd[0] != atcommand_symbol )
  15112. {
  15113. cmd += strlen(sd->status.name);
  15114. while( *cmd != atcommand_symbol && *cmd != 0 )
  15115. cmd++;
  15116. }
  15117. is_atcommand(fd, sd, cmd, 1);
  15118. return 0;
  15119. }
  15120. BUILDIN_FUNC(checkre)
  15121. {
  15122. int num;
  15123. num=script_getnum(st,2);
  15124. switch(num){
  15125. case 0:
  15126. #ifdef RENEWAL
  15127. script_pushint(st, 1);
  15128. #else
  15129. script_pushint(st, 0);
  15130. #endif
  15131. break;
  15132. case 1:
  15133. #ifdef RENEWAL_CAST
  15134. script_pushint(st, 1);
  15135. #else
  15136. script_pushint(st, 0);
  15137. #endif
  15138. break;
  15139. case 2:
  15140. #ifdef RENEWAL_DROP
  15141. script_pushint(st, 1);
  15142. #else
  15143. script_pushint(st, 0);
  15144. #endif
  15145. break;
  15146. case 3:
  15147. #ifdef RENEWAL_EXP
  15148. script_pushint(st, 1);
  15149. #else
  15150. script_pushint(st, 0);
  15151. #endif
  15152. break;
  15153. case 4:
  15154. #ifdef RENEWAL_LVDMG
  15155. script_pushint(st, 1);
  15156. #else
  15157. script_pushint(st, 0);
  15158. #endif
  15159. break;
  15160. case 5:
  15161. #ifdef RENEWAL_EDP
  15162. script_pushint(st, 1);
  15163. #else
  15164. script_pushint(st, 0);
  15165. #endif
  15166. break;
  15167. case 6:
  15168. #ifdef RENEWAL_ASPD
  15169. script_pushint(st, 1);
  15170. #else
  15171. script_pushint(st, 0);
  15172. #endif
  15173. break;
  15174. default:
  15175. ShowWarning("buildin_checkre: unknown parameter.\n");
  15176. break;
  15177. }
  15178. return 0;
  15179. }
  15180. /* getrandgroupitem <group_id>,<quantity> */
  15181. BUILDIN_FUNC(getrandgroupitem) {
  15182. TBL_PC* sd;
  15183. int i, get_count = 0, flag, nameid, group = script_getnum(st, 2), qty = script_getnum(st,3);
  15184. struct item item_tmp;
  15185. if( !( sd = script_rid2sd(st) ) )
  15186. return 0;
  15187. if( qty <= 0 ) {
  15188. ShowError("getrandgroupitem: qty is <= 0!\n");
  15189. return 1;
  15190. }
  15191. if( (nameid = itemdb_searchrandomid(group)) == UNKNOWN_ITEM_ID ) {
  15192. return 1; //ensure valid itemid
  15193. }
  15194. memset(&item_tmp,0,sizeof(item_tmp));
  15195. item_tmp.nameid = nameid;
  15196. item_tmp.identify = itemdb_isidentified(nameid);
  15197. //Check if it's stackable.
  15198. if (!itemdb_isstackable(nameid))
  15199. get_count = 1;
  15200. else
  15201. get_count = qty;
  15202. for (i = 0; i < qty; i += get_count) {
  15203. // if not pet egg
  15204. if (!pet_create_egg(sd, nameid)) {
  15205. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT))) {
  15206. clif_additem(sd, 0, 0, flag);
  15207. if( pc_candrop(sd,&item_tmp) )
  15208. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  15209. }
  15210. }
  15211. }
  15212. return 0;
  15213. }
  15214. /* cleanmap <map_name>;
  15215. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  15216. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  15217. {
  15218. nullpo_ret(bl);
  15219. map_clearflooritem(bl);
  15220. return 0;
  15221. }
  15222. BUILDIN_FUNC(cleanmap)
  15223. {
  15224. const char *map;
  15225. int16 m = -1;
  15226. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  15227. map = script_getstr(st, 2);
  15228. m = map_mapname2mapid(map);
  15229. if (!m)
  15230. return 1;
  15231. if ((script_lastdata(st) - 2) < 4) {
  15232. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  15233. } else {
  15234. x0 = script_getnum(st, 3);
  15235. y0 = script_getnum(st, 4);
  15236. x1 = script_getnum(st, 5);
  15237. y1 = script_getnum(st, 6);
  15238. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  15239. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  15240. } else {
  15241. ShowError("cleanarea: invalid coordinate defined!\n");
  15242. return 1;
  15243. }
  15244. }
  15245. return 0;
  15246. }
  15247. /* Cast a skill on the attached player.
  15248. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  15249. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  15250. BUILDIN_FUNC(npcskill)
  15251. {
  15252. uint16 skill_id;
  15253. unsigned short skill_level;
  15254. unsigned int stat_point;
  15255. unsigned int npc_level;
  15256. struct npc_data *nd;
  15257. struct map_session_data *sd;
  15258. skill_id = script_isstring(st, 2) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  15259. skill_level = script_getnum(st, 3);
  15260. stat_point = script_getnum(st, 4);
  15261. npc_level = script_getnum(st, 5);
  15262. sd = script_rid2sd(st);
  15263. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  15264. if (stat_point > battle_config.max_third_parameter) {
  15265. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  15266. return 1;
  15267. }
  15268. if (npc_level > MAX_LEVEL) {
  15269. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  15270. return 1;
  15271. }
  15272. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  15273. return 1;
  15274. }
  15275. nd->level = npc_level;
  15276. nd->stat_point = stat_point;
  15277. if (!nd->status.hp) {
  15278. status_calc_npc(nd, true);
  15279. } else {
  15280. status_calc_npc(nd, false);
  15281. }
  15282. if (skill_get_inf(skill_id)&INF_GROUND_SKILL) {
  15283. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  15284. } else {
  15285. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  15286. }
  15287. return 0;
  15288. }
  15289. /* Consumes an item.
  15290. * consumeitem <item id>;
  15291. * consumeitem "<item name>"; */
  15292. BUILDIN_FUNC(consumeitem)
  15293. {
  15294. TBL_NPC *nd;
  15295. TBL_PC *sd;
  15296. struct script_data *data;
  15297. struct item_data *item_data;
  15298. nullpo_retr( 1, ( sd = script_rid2sd( st ) ) );
  15299. nullpo_retr( 1, ( nd = (TBL_NPC *)map_id2bl( sd->npc_id ) ) );
  15300. data = script_getdata( st, 2 );
  15301. get_val( st, data );
  15302. if( data_isstring( data ) ){
  15303. const char *name = conv_str( st, data );
  15304. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  15305. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  15306. return 1;
  15307. }
  15308. }else if( data_isint( data ) ){
  15309. int nameid = conv_num( st, data );
  15310. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  15311. ShowError("buildin_consumeitem: Nonexistant item %d requested.\n", nameid );
  15312. return 1;
  15313. }
  15314. }else{
  15315. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).", data->type );
  15316. return 1;
  15317. }
  15318. run_script( item_data->script, 0, sd->bl.id, nd->bl.id );
  15319. return 0;
  15320. }
  15321. /*=======================================================
  15322. * Make a player sit/stand.
  15323. * sit {"<character name>"};
  15324. * stand {"<character name>"};
  15325. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  15326. *-------------------------------------------------------*/
  15327. BUILDIN_FUNC(sit)
  15328. {
  15329. TBL_PC *sd;
  15330. if( script_hasdata(st, 2) )
  15331. sd = map_nick2sd(script_getstr(st, 2));
  15332. else
  15333. sd = script_rid2sd(st);
  15334. if( sd == NULL)
  15335. return 0;
  15336. if( !pc_issit(sd) ) {
  15337. unit_stop_walking(&sd->bl, 1|4);
  15338. pc_setsit(sd);
  15339. skill_sit(sd, 1);
  15340. clif_sitting(&sd->bl);
  15341. }
  15342. return 0;
  15343. }
  15344. BUILDIN_FUNC(stand)
  15345. {
  15346. TBL_PC *sd;
  15347. if( script_hasdata(st, 2) )
  15348. sd = map_nick2sd(script_getstr(st, 2));
  15349. else
  15350. sd = script_rid2sd(st);
  15351. if( sd == NULL)
  15352. return 0;
  15353. if( pc_issit(sd) ) {
  15354. pc_setstand(sd);
  15355. skill_sit(sd, 0);
  15356. clif_standing(&sd->bl);
  15357. }
  15358. return 0;
  15359. }
  15360. /*==========================================
  15361. * countbound {<type>};
  15362. * Creates an array of bounded item IDs
  15363. * Returns amount of items found
  15364. * Type:
  15365. * 1 - Account Bound
  15366. * 2 - Guild Bound
  15367. * 3 - Party Bound
  15368. *------------------------------------------*/
  15369. BUILDIN_FUNC(countbound)
  15370. {
  15371. int i, type, j=0, k=0;
  15372. TBL_PC *sd;
  15373. if( (sd = script_rid2sd(st)) == NULL )
  15374. return 0;
  15375. type = script_hasdata(st,2)?script_getnum(st,2):0;
  15376. for(i=0;i<MAX_INVENTORY;i++){
  15377. if(sd->status.inventory[i].nameid > 0 && (
  15378. (!type && sd->status.inventory[i].bound > 0) ||
  15379. (type && sd->status.inventory[i].bound == type)
  15380. )) {
  15381. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  15382. k++;
  15383. j += sd->status.inventory[i].amount;
  15384. }
  15385. }
  15386. script_pushint(st,j);
  15387. return 0;
  15388. }
  15389. /*==========================================
  15390. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  15391. * <item share>: 0-Each Take. 1-Party Share
  15392. * <item share type>: 0-Each Take. 1-Even Share
  15393. * Return values:
  15394. * -3 - party name is exist
  15395. * -2 - player is in party already
  15396. * -1 - player is not found
  15397. * 0 - unknown error
  15398. * 1 - success, will return party id $@party_create_id
  15399. *------------------------------------------*/
  15400. BUILDIN_FUNC(party_create)
  15401. {
  15402. char party_name[NAME_LENGTH];
  15403. int item1 = 0, item2 = 0;
  15404. TBL_PC *sd;
  15405. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  15406. script_pushint(st,-1);
  15407. return 0;
  15408. }
  15409. if( sd->status.party_id ) {
  15410. script_pushint(st,-2);
  15411. return 0;
  15412. }
  15413. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  15414. trim(party_name);
  15415. if( party_searchname(party_name) ) {
  15416. script_pushint(st,-3);
  15417. return 0;
  15418. }
  15419. if( script_getnum(st,4) )
  15420. item1 = 1;
  15421. if( script_getnum(st,5) )
  15422. item2 = 1;
  15423. party_create_byscript = 1;
  15424. script_pushint(st,party_create(sd,party_name,item1,item2));
  15425. return 0;
  15426. }
  15427. /*==========================================
  15428. * party_addmember <party id>,<char id>;
  15429. * Adds player to specified party
  15430. * Return values:
  15431. * -4 - party is full
  15432. * -3 - party is not found
  15433. * -2 - player is in party already
  15434. * -1 - player is not found
  15435. * 0 - unknown error
  15436. * 1 - success
  15437. *------------------------------------------*/
  15438. BUILDIN_FUNC(party_addmember)
  15439. {
  15440. int party_id = script_getnum(st,2);
  15441. TBL_PC *sd;
  15442. struct party_data *party;
  15443. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  15444. script_pushint(st,-1);
  15445. return 0;
  15446. }
  15447. if( sd->status.party_id ) {
  15448. script_pushint(st,-2);
  15449. return 0;
  15450. }
  15451. if( !(party = party_search(party_id)) ) {
  15452. script_pushint(st,-3);
  15453. return 0;
  15454. }
  15455. if( party->party.count >= MAX_PARTY ) {
  15456. script_pushint(st,-4);
  15457. return 0;
  15458. }
  15459. sd->party_invite = party_id;
  15460. script_pushint(st,party_add_member(party_id,sd));
  15461. return 0;
  15462. }
  15463. /*==========================================
  15464. * party_delmember {<char id>,<party_id>};
  15465. * Removes player from his/her party. If party_id and char_id is empty
  15466. * remove the invoker from his/her party
  15467. * Return values:
  15468. * -3 - player is not in party
  15469. * -2 - party is not found
  15470. * -1 - player is not found
  15471. * 0 - unknown error
  15472. * 1 - success
  15473. *------------------------------------------*/
  15474. BUILDIN_FUNC(party_delmember)
  15475. {
  15476. TBL_PC *sd = NULL;
  15477. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  15478. script_pushint(st,-1);
  15479. return 0;
  15480. }
  15481. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  15482. script_pushint(st,party_removemember2(sd,0,0));
  15483. else
  15484. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  15485. return 0;
  15486. }
  15487. /*==========================================
  15488. * party_changeleader <party id>,<char id>;
  15489. * Can change party leader even the leader is not online
  15490. * Return values:
  15491. * -4 - player is party leader already
  15492. * -3 - player is not in this party
  15493. * -2 - player is not found
  15494. * -1 - party is not found
  15495. * 0 - unknown error
  15496. * 1 - success
  15497. *------------------------------------------*/
  15498. BUILDIN_FUNC(party_changeleader)
  15499. {
  15500. int i, party_id = script_getnum(st,2);
  15501. TBL_PC *sd = NULL;
  15502. TBL_PC *tsd = NULL;
  15503. struct party_data *party = NULL;
  15504. if( !(party = party_search(party_id)) ) {
  15505. script_pushint(st,-1);
  15506. return 0;
  15507. }
  15508. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  15509. script_pushint(st,-2);
  15510. return 0;
  15511. }
  15512. if( tsd->status.party_id != party_id ) {
  15513. script_pushint(st,-3);
  15514. return 0;
  15515. }
  15516. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  15517. if( i >= MAX_PARTY ) { //this is should impossible!
  15518. script_pushint(st,0);
  15519. return 0;
  15520. }
  15521. if( party->data[i].sd == tsd ) {
  15522. script_pushint(st,-4);
  15523. return 0;
  15524. }
  15525. script_pushint(st,party_changeleader(sd,tsd,party));
  15526. return 0;
  15527. }
  15528. /*==========================================
  15529. * party_changeoption <party id>,<option>,<flag>;
  15530. * Return values:
  15531. * -1 - party is not found
  15532. * 0 - invalid option
  15533. * 1 - success
  15534. *------------------------------------------*/
  15535. BUILDIN_FUNC(party_changeoption)
  15536. {
  15537. struct party_data *party;
  15538. if( !(party = party_search(script_getnum(st,2))) ) {
  15539. script_pushint(st,-1);
  15540. return 0;
  15541. }
  15542. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  15543. return 0;
  15544. }
  15545. /*==========================================
  15546. * party_destroy <party id>;
  15547. * Destroys party with party id.
  15548. * Return values:
  15549. * 0 - failed
  15550. * 1 - success
  15551. *------------------------------------------*/
  15552. BUILDIN_FUNC(party_destroy)
  15553. {
  15554. int i;
  15555. struct party_data *party;
  15556. if( !(party = party_search(script_getnum(st,2))) ) {
  15557. script_pushint(st,0);
  15558. return 0;
  15559. }
  15560. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  15561. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  15562. int j;
  15563. for( j = 0; j < MAX_PARTY; j++ ) {
  15564. TBL_PC *sd = party->data[j].sd;
  15565. if(sd)
  15566. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  15567. else if( party->party.member[j].char_id )
  15568. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  15569. }
  15570. party_broken(party->party.party_id);
  15571. script_pushint(st,1);
  15572. }
  15573. else //leader leave = party broken
  15574. script_pushint(st,party_leave(party->data[i].sd));
  15575. return 0;
  15576. }
  15577. // declarations that were supposed to be exported from npc_chat.c
  15578. #ifdef PCRE_SUPPORT
  15579. BUILDIN_FUNC(defpattern);
  15580. BUILDIN_FUNC(activatepset);
  15581. BUILDIN_FUNC(deactivatepset);
  15582. BUILDIN_FUNC(deletepset);
  15583. #endif
  15584. /// script command definitions
  15585. /// for an explanation on args, see add_buildin_func
  15586. struct script_function buildin_func[] = {
  15587. // NPC interaction
  15588. BUILDIN_DEF(mes,"s*"),
  15589. BUILDIN_DEF(next,""),
  15590. BUILDIN_DEF(close,""),
  15591. BUILDIN_DEF(close2,""),
  15592. BUILDIN_DEF(menu,"sl*"),
  15593. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  15594. BUILDIN_DEF(prompt,"s*"),
  15595. //
  15596. BUILDIN_DEF(goto,"l"),
  15597. BUILDIN_DEF(callsub,"l*"),
  15598. BUILDIN_DEF(callfunc,"s*"),
  15599. BUILDIN_DEF(return,"?"),
  15600. BUILDIN_DEF(getarg,"i?"),
  15601. BUILDIN_DEF(jobchange,"i?"),
  15602. BUILDIN_DEF(jobname,"i"),
  15603. BUILDIN_DEF(input,"r??"),
  15604. BUILDIN_DEF(warp,"sii"),
  15605. BUILDIN_DEF(areawarp,"siiiisii??"),
  15606. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  15607. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  15608. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  15609. BUILDIN_DEF(setlook,"ii"),
  15610. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  15611. BUILDIN_DEF(set,"rv"),
  15612. BUILDIN_DEF(setarray,"rv*"),
  15613. BUILDIN_DEF(cleararray,"rvi"),
  15614. BUILDIN_DEF(copyarray,"rri"),
  15615. BUILDIN_DEF(getarraysize,"r"),
  15616. BUILDIN_DEF(deletearray,"r?"),
  15617. BUILDIN_DEF(getelementofarray,"ri"),
  15618. BUILDIN_DEF(getitem,"vi?"),
  15619. BUILDIN_DEF(rentitem,"vi"),
  15620. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  15621. BUILDIN_DEF(getnameditem,"vv"),
  15622. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  15623. BUILDIN_DEF(makeitem,"visii"),
  15624. BUILDIN_DEF(delitem,"vi?"),
  15625. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  15626. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  15627. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  15628. BUILDIN_DEF(cutin,"si"),
  15629. BUILDIN_DEF(viewpoint,"iiiii"),
  15630. BUILDIN_DEF(heal,"ii"),
  15631. BUILDIN_DEF(itemheal,"ii"),
  15632. BUILDIN_DEF(percentheal,"ii"),
  15633. BUILDIN_DEF(rand,"i?"),
  15634. BUILDIN_DEF(countitem,"v"),
  15635. BUILDIN_DEF2(countitem,"countitem2","viiiiiii"),
  15636. BUILDIN_DEF(checkweight,"vi*"),
  15637. BUILDIN_DEF(checkweight2,"rr"),
  15638. BUILDIN_DEF(readparam,"i?"),
  15639. BUILDIN_DEF(getcharid,"i?"),
  15640. BUILDIN_DEF(getnpcid,"i?"),
  15641. BUILDIN_DEF(getpartyname,"i"),
  15642. BUILDIN_DEF(getpartymember,"i?"),
  15643. BUILDIN_DEF(getpartyleader,"i?"),
  15644. BUILDIN_DEF(getguildname,"i"),
  15645. BUILDIN_DEF(getguildmaster,"i"),
  15646. BUILDIN_DEF(getguildmasterid,"i"),
  15647. BUILDIN_DEF(strcharinfo,"i"),
  15648. BUILDIN_DEF(strnpcinfo,"i"),
  15649. BUILDIN_DEF(getequipid,"i"),
  15650. BUILDIN_DEF(getequipname,"i"),
  15651. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  15652. BUILDIN_DEF(repair,"i"), // [Valaris]
  15653. BUILDIN_DEF(repairall,""),
  15654. BUILDIN_DEF(getequipisequiped,"i"),
  15655. BUILDIN_DEF(getequipisenableref,"i"),
  15656. BUILDIN_DEF(getequiprefinerycnt,"i"),
  15657. BUILDIN_DEF(getequipweaponlv,"i"),
  15658. BUILDIN_DEF(getequippercentrefinery,"i"),
  15659. BUILDIN_DEF(successrefitem,"i"),
  15660. BUILDIN_DEF(failedrefitem,"i"),
  15661. BUILDIN_DEF(downrefitem,"i"),
  15662. BUILDIN_DEF(statusup,"i"),
  15663. BUILDIN_DEF(statusup2,"ii"),
  15664. BUILDIN_DEF(bonus,"iv"),
  15665. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  15666. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  15667. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  15668. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  15669. BUILDIN_DEF(autobonus,"sii??"),
  15670. BUILDIN_DEF(autobonus2,"sii??"),
  15671. BUILDIN_DEF(autobonus3,"siiv?"),
  15672. BUILDIN_DEF(skill,"vi?"),
  15673. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  15674. BUILDIN_DEF(guildskill,"vi"),
  15675. BUILDIN_DEF(getskilllv,"v"),
  15676. BUILDIN_DEF(getgdskilllv,"iv"),
  15677. BUILDIN_DEF(basicskillcheck,""),
  15678. BUILDIN_DEF(getgmlevel,""),
  15679. BUILDIN_DEF(getgroupid,""),
  15680. BUILDIN_DEF(end,""),
  15681. BUILDIN_DEF(checkoption,"i"),
  15682. BUILDIN_DEF(setoption,"i?"),
  15683. BUILDIN_DEF(setcart,"?"),
  15684. BUILDIN_DEF(checkcart,""),
  15685. BUILDIN_DEF(setfalcon,"?"),
  15686. BUILDIN_DEF(checkfalcon,""),
  15687. BUILDIN_DEF(setriding,"?"),
  15688. BUILDIN_DEF(checkriding,""),
  15689. BUILDIN_DEF(checkwug,""),
  15690. BUILDIN_DEF(checkmadogear,""),
  15691. BUILDIN_DEF(setmadogear,"?"),
  15692. BUILDIN_DEF2(savepoint,"save","sii"),
  15693. BUILDIN_DEF(savepoint,"sii"),
  15694. BUILDIN_DEF(gettimetick,"i"),
  15695. BUILDIN_DEF(gettime,"i"),
  15696. BUILDIN_DEF(gettimestr,"si"),
  15697. BUILDIN_DEF(openstorage,""),
  15698. BUILDIN_DEF(guildopenstorage,""),
  15699. BUILDIN_DEF(itemskill,"vi"),
  15700. BUILDIN_DEF(produce,"i"),
  15701. BUILDIN_DEF(cooking,"i"),
  15702. BUILDIN_DEF(monster,"siisii???"),
  15703. BUILDIN_DEF(getmobdrops,"i"),
  15704. BUILDIN_DEF(areamonster,"siiiisii???"),
  15705. BUILDIN_DEF(killmonster,"ss?"),
  15706. BUILDIN_DEF(killmonsterall,"s?"),
  15707. BUILDIN_DEF(clone,"siisi????"),
  15708. BUILDIN_DEF(doevent,"s"),
  15709. BUILDIN_DEF(donpcevent,"s"),
  15710. BUILDIN_DEF(cmdothernpc,"ss"),
  15711. BUILDIN_DEF(addtimer,"is"),
  15712. BUILDIN_DEF(deltimer,"s"),
  15713. BUILDIN_DEF(addtimercount,"si"),
  15714. BUILDIN_DEF(initnpctimer,"??"),
  15715. BUILDIN_DEF(stopnpctimer,"??"),
  15716. BUILDIN_DEF(startnpctimer,"??"),
  15717. BUILDIN_DEF(setnpctimer,"i?"),
  15718. BUILDIN_DEF(getnpctimer,"i?"),
  15719. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  15720. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  15721. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  15722. BUILDIN_DEF(announce,"si?????"),
  15723. BUILDIN_DEF(mapannounce,"ssi?????"),
  15724. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  15725. BUILDIN_DEF(getusers,"i"),
  15726. BUILDIN_DEF(getmapguildusers,"si"),
  15727. BUILDIN_DEF(getmapusers,"s"),
  15728. BUILDIN_DEF(getareausers,"siiii"),
  15729. BUILDIN_DEF(getareadropitem,"siiiiv"),
  15730. BUILDIN_DEF(enablenpc,"s"),
  15731. BUILDIN_DEF(disablenpc,"s"),
  15732. BUILDIN_DEF(hideoffnpc,"s"),
  15733. BUILDIN_DEF(hideonnpc,"s"),
  15734. BUILDIN_DEF(sc_start,"iii???"),
  15735. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  15736. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  15737. BUILDIN_DEF(sc_end,"i?"),
  15738. BUILDIN_DEF(getstatus, "i?"),
  15739. BUILDIN_DEF(getscrate,"ii?"),
  15740. BUILDIN_DEF(debugmes,"s"),
  15741. BUILDIN_DEF2(catchpet,"pet","i"),
  15742. BUILDIN_DEF2(birthpet,"bpet",""),
  15743. BUILDIN_DEF(resetlvl,"i"),
  15744. BUILDIN_DEF(resetstatus,""),
  15745. BUILDIN_DEF(resetskill,""),
  15746. BUILDIN_DEF(skillpointcount,""),
  15747. BUILDIN_DEF(changebase,"i?"),
  15748. BUILDIN_DEF(changesex,""),
  15749. BUILDIN_DEF(waitingroom,"si?????"),
  15750. BUILDIN_DEF(delwaitingroom,"?"),
  15751. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  15752. BUILDIN_DEF(enablewaitingroomevent,"?"),
  15753. BUILDIN_DEF(disablewaitingroomevent,"?"),
  15754. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  15755. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  15756. BUILDIN_DEF(getwaitingroomstate,"i?"),
  15757. BUILDIN_DEF(warpwaitingpc,"sii?"),
  15758. BUILDIN_DEF(attachrid,"i"),
  15759. BUILDIN_DEF(addrid,"i?????"),
  15760. BUILDIN_DEF(detachrid,""),
  15761. BUILDIN_DEF(isloggedin,"i?"),
  15762. BUILDIN_DEF(setmapflagnosave,"ssii"),
  15763. BUILDIN_DEF(getmapflag,"si"),
  15764. BUILDIN_DEF(setmapflag,"si?"),
  15765. BUILDIN_DEF(removemapflag,"si?"),
  15766. BUILDIN_DEF(pvpon,"s"),
  15767. BUILDIN_DEF(pvpoff,"s"),
  15768. BUILDIN_DEF(gvgon,"s"),
  15769. BUILDIN_DEF(gvgoff,"s"),
  15770. BUILDIN_DEF(emotion,"i??"),
  15771. BUILDIN_DEF(maprespawnguildid,"sii"),
  15772. BUILDIN_DEF(agitstart,""), // <Agit>
  15773. BUILDIN_DEF(agitend,""),
  15774. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  15775. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  15776. BUILDIN_DEF(getcastlename,"s"),
  15777. BUILDIN_DEF(getcastledata,"si"),
  15778. BUILDIN_DEF(setcastledata,"sii"),
  15779. BUILDIN_DEF(requestguildinfo,"i?"),
  15780. BUILDIN_DEF(getequipcardcnt,"i"),
  15781. BUILDIN_DEF(successremovecards,"i"),
  15782. BUILDIN_DEF(failedremovecards,"ii"),
  15783. BUILDIN_DEF(marriage,"s"),
  15784. BUILDIN_DEF2(wedding_effect,"wedding",""),
  15785. BUILDIN_DEF(divorce,""),
  15786. BUILDIN_DEF(ispartneron,""),
  15787. BUILDIN_DEF(getpartnerid,""),
  15788. BUILDIN_DEF(getchildid,""),
  15789. BUILDIN_DEF(getmotherid,""),
  15790. BUILDIN_DEF(getfatherid,""),
  15791. BUILDIN_DEF(warppartner,"sii"),
  15792. BUILDIN_DEF(getitemname,"v"),
  15793. BUILDIN_DEF(getitemslots,"i"),
  15794. BUILDIN_DEF(makepet,"i"),
  15795. BUILDIN_DEF(getexp,"ii"),
  15796. BUILDIN_DEF(getinventorylist,""),
  15797. BUILDIN_DEF(getskilllist,""),
  15798. BUILDIN_DEF(clearitem,""),
  15799. BUILDIN_DEF(classchange,"ii"),
  15800. BUILDIN_DEF(misceffect,"i"),
  15801. BUILDIN_DEF(playBGM,"s"),
  15802. BUILDIN_DEF(playBGMall,"s?????"),
  15803. BUILDIN_DEF(soundeffect,"si"),
  15804. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  15805. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  15806. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  15807. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  15808. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  15809. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  15810. BUILDIN_DEF(petloot,"i"), // [Valaris]
  15811. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  15812. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  15813. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  15814. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  15815. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  15816. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  15817. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  15818. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  15819. BUILDIN_DEF(nude,""), // nude command [Valaris]
  15820. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  15821. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  15822. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  15823. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  15824. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  15825. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  15826. BUILDIN_DEF(mobcount,"ss"),
  15827. BUILDIN_DEF(getlook,"i"),
  15828. BUILDIN_DEF(getsavepoint,"i"),
  15829. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  15830. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  15831. BUILDIN_DEF(npcstop,""), // [Valaris]
  15832. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  15833. BUILDIN_DEF(checkoption1,"i"),
  15834. BUILDIN_DEF(checkoption2,"i"),
  15835. BUILDIN_DEF(guildgetexp,"i"),
  15836. BUILDIN_DEF(guildchangegm,"is"),
  15837. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  15838. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  15839. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  15840. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  15841. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  15842. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  15843. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  15844. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  15845. BUILDIN_DEF(night,""), // sets the server to night time
  15846. BUILDIN_DEF(day,""), // sets the server to day time
  15847. #ifdef PCRE_SUPPORT
  15848. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  15849. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  15850. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  15851. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  15852. #endif
  15853. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  15854. BUILDIN_DEF(getusersname,""),
  15855. BUILDIN_DEF(recovery,"i???"),
  15856. BUILDIN_DEF(getpetinfo,"i"),
  15857. BUILDIN_DEF(gethominfo,"i"),
  15858. BUILDIN_DEF(getmercinfo,"i?"),
  15859. BUILDIN_DEF(checkequipedcard,"i"),
  15860. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  15861. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  15862. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  15863. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  15864. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  15865. BUILDIN_DEF(charat,"si"),
  15866. BUILDIN_DEF(setchar,"ssi"),
  15867. BUILDIN_DEF(insertchar,"ssi"),
  15868. BUILDIN_DEF(delchar,"si"),
  15869. BUILDIN_DEF(strtoupper,"s"),
  15870. BUILDIN_DEF(strtolower,"s"),
  15871. BUILDIN_DEF(charisupper, "si"),
  15872. BUILDIN_DEF(charislower, "si"),
  15873. BUILDIN_DEF(substr,"sii"),
  15874. BUILDIN_DEF(explode, "rss"),
  15875. BUILDIN_DEF(implode, "r?"),
  15876. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  15877. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  15878. BUILDIN_DEF(strpos,"ss?"),
  15879. BUILDIN_DEF(replacestr,"sss??"),
  15880. BUILDIN_DEF(countstr,"ss?"),
  15881. BUILDIN_DEF(setnpcdisplay,"sv??"),
  15882. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  15883. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  15884. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  15885. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  15886. // [zBuffer] List of mathematics commands --->
  15887. BUILDIN_DEF(sqrt,"i"),
  15888. BUILDIN_DEF(pow,"ii"),
  15889. BUILDIN_DEF(distance,"iiii"),
  15890. // <--- [zBuffer] List of mathematics commands
  15891. BUILDIN_DEF(md5,"s"),
  15892. // [zBuffer] List of dynamic var commands --->
  15893. BUILDIN_DEF(getd,"s"),
  15894. BUILDIN_DEF(setd,"sv"),
  15895. // <--- [zBuffer] List of dynamic var commands
  15896. BUILDIN_DEF(petstat,"i"),
  15897. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  15898. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  15899. BUILDIN_DEF(npcshopadditem,"sii*"),
  15900. BUILDIN_DEF(npcshopdelitem,"si*"),
  15901. BUILDIN_DEF(npcshopattach,"s?"),
  15902. BUILDIN_DEF(equip,"i"),
  15903. BUILDIN_DEF(autoequip,"ii"),
  15904. BUILDIN_DEF(setbattleflag,"si"),
  15905. BUILDIN_DEF(getbattleflag,"s"),
  15906. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  15907. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  15908. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  15909. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  15910. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  15911. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  15912. BUILDIN_DEF(axtoi,"s"),
  15913. BUILDIN_DEF(query_sql,"s*"),
  15914. BUILDIN_DEF(query_logsql,"s*"),
  15915. BUILDIN_DEF(escape_sql,"v"),
  15916. BUILDIN_DEF(atoi,"s"),
  15917. // [zBuffer] List of player cont commands --->
  15918. BUILDIN_DEF(rid2name,"i"),
  15919. BUILDIN_DEF(pcfollow,"ii"),
  15920. BUILDIN_DEF(pcstopfollow,"i"),
  15921. BUILDIN_DEF(pcblockmove,"ii"),
  15922. // <--- [zBuffer] List of player cont commands
  15923. // [zBuffer] List of mob control commands --->
  15924. BUILDIN_DEF(unitwalk,"ii?"),
  15925. BUILDIN_DEF(unitkill,"i"),
  15926. BUILDIN_DEF(unitwarp,"isii"),
  15927. BUILDIN_DEF(unitattack,"iv?"),
  15928. BUILDIN_DEF(unitstop,"i"),
  15929. BUILDIN_DEF(unittalk,"is"),
  15930. BUILDIN_DEF(unitemote,"ii"),
  15931. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  15932. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  15933. // <--- [zBuffer] List of mob control commands
  15934. BUILDIN_DEF(sleep,"i"),
  15935. BUILDIN_DEF(sleep2,"i"),
  15936. BUILDIN_DEF(awake,"s"),
  15937. BUILDIN_DEF(getvariableofnpc,"rs"),
  15938. BUILDIN_DEF(warpportal,"iisii"),
  15939. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  15940. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  15941. BUILDIN_DEF(morphembryo,""),
  15942. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  15943. BUILDIN_DEF(checkhomcall,""),
  15944. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  15945. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  15946. BUILDIN_DEF(checkvending,"?"),
  15947. BUILDIN_DEF(checkchatting,"?"),
  15948. BUILDIN_DEF(checkidle,"?"),
  15949. BUILDIN_DEF(openmail,""),
  15950. BUILDIN_DEF(openauction,""),
  15951. BUILDIN_DEF(checkcell,"siii"),
  15952. BUILDIN_DEF(setcell,"siiiiii"),
  15953. BUILDIN_DEF(setwall,"siiiiis"),
  15954. BUILDIN_DEF(delwall,"s"),
  15955. BUILDIN_DEF(searchitem,"rs"),
  15956. BUILDIN_DEF(mercenary_create,"ii"),
  15957. BUILDIN_DEF(mercenary_heal,"ii"),
  15958. BUILDIN_DEF(mercenary_sc_start,"iii"),
  15959. BUILDIN_DEF(mercenary_get_calls,"i"),
  15960. BUILDIN_DEF(mercenary_get_faith,"i"),
  15961. BUILDIN_DEF(mercenary_set_calls,"ii"),
  15962. BUILDIN_DEF(mercenary_set_faith,"ii"),
  15963. BUILDIN_DEF(readbook,"ii"),
  15964. BUILDIN_DEF(setfont,"i"),
  15965. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  15966. BUILDIN_DEF(progressbar,"si"),
  15967. BUILDIN_DEF(pushpc,"ii"),
  15968. BUILDIN_DEF(buyingstore,"i"),
  15969. BUILDIN_DEF(searchstores,"ii"),
  15970. BUILDIN_DEF(showdigit,"i?"),
  15971. // WoE SE
  15972. BUILDIN_DEF(agitstart2,""),
  15973. BUILDIN_DEF(agitend2,""),
  15974. BUILDIN_DEF(agitcheck2,""),
  15975. // BattleGround
  15976. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  15977. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  15978. BUILDIN_DEF(bg_team_setxy,"iii"),
  15979. BUILDIN_DEF(bg_warp,"isii"),
  15980. BUILDIN_DEF(bg_monster,"isiisi?"),
  15981. BUILDIN_DEF(bg_monster_set_team,"ii"),
  15982. BUILDIN_DEF(bg_leave,""),
  15983. BUILDIN_DEF(bg_destroy,"i"),
  15984. BUILDIN_DEF(areapercentheal,"siiiiii"),
  15985. BUILDIN_DEF(bg_get_data,"ii"),
  15986. BUILDIN_DEF(bg_getareausers,"isiiii"),
  15987. BUILDIN_DEF(bg_updatescore,"sii"),
  15988. // Instancing
  15989. BUILDIN_DEF(instance_create,"s"),
  15990. BUILDIN_DEF(instance_destroy,"?"),
  15991. BUILDIN_DEF(instance_id,""),
  15992. BUILDIN_DEF(instance_enter,"s"),
  15993. BUILDIN_DEF(instance_npcname,"s?"),
  15994. BUILDIN_DEF(instance_mapname,"s?"),
  15995. BUILDIN_DEF(instance_warpall,"sii?"),
  15996. BUILDIN_DEF(instance_check_party,"i???"),
  15997. /**
  15998. * 3rd-related
  15999. **/
  16000. BUILDIN_DEF(makerune,"i"),
  16001. BUILDIN_DEF(checkdragon,""),//[Ind]
  16002. BUILDIN_DEF(setdragon,"?"),//[Ind]
  16003. BUILDIN_DEF(ismounting,""),//[Ind]
  16004. BUILDIN_DEF(setmounting,""),//[Ind]
  16005. BUILDIN_DEF(checkre,"i"),
  16006. /**
  16007. * rAthena and beyond!
  16008. **/
  16009. BUILDIN_DEF(getargcount,""),
  16010. BUILDIN_DEF(getcharip,"?"),
  16011. BUILDIN_DEF(is_function,"s"),
  16012. BUILDIN_DEF(get_revision,""),
  16013. BUILDIN_DEF(freeloop,"i"),
  16014. BUILDIN_DEF(getrandgroupitem,"ii"),
  16015. BUILDIN_DEF(cleanmap,"s"),
  16016. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  16017. BUILDIN_DEF(npcskill,"viii"),
  16018. BUILDIN_DEF(consumeitem,"v"),
  16019. BUILDIN_DEF(delequip,"i"),
  16020. BUILDIN_DEF(sit,"?"),
  16021. BUILDIN_DEF(stand,"?"),
  16022. /**
  16023. * @commands (script based)
  16024. **/
  16025. BUILDIN_DEF(bindatcmd, "ss??"),
  16026. BUILDIN_DEF(unbindatcmd, "s"),
  16027. BUILDIN_DEF(useatcmd, "s"),
  16028. //Quest Log System [Inkfish]
  16029. BUILDIN_DEF(setquest, "i"),
  16030. BUILDIN_DEF(erasequest, "i"),
  16031. BUILDIN_DEF(completequest, "i"),
  16032. BUILDIN_DEF(checkquest, "i?"),
  16033. BUILDIN_DEF(changequest, "ii"),
  16034. BUILDIN_DEF(showevent, "ii"),
  16035. //Bound items [Xantara] & [Akinari]
  16036. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  16037. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  16038. BUILDIN_DEF(countbound, "?"),
  16039. // Party related
  16040. BUILDIN_DEF(party_create,"s???"),
  16041. BUILDIN_DEF(party_addmember,"ii"),
  16042. BUILDIN_DEF(party_delmember,"??"),
  16043. BUILDIN_DEF(party_changeleader,"ii"),
  16044. BUILDIN_DEF(party_changeoption,"iii"),
  16045. BUILDIN_DEF(party_destroy,"i"),
  16046. {NULL,NULL,NULL},
  16047. };