script.c 440 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967
  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 <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <math.h>
  53. #ifndef WIN32
  54. #include <sys/time.h>
  55. #endif
  56. #include <time.h>
  57. #include <setjmp.h>
  58. #include <errno.h>
  59. #ifdef BETA_THREAD_TEST
  60. #include "../common/atomic.h"
  61. #include "../common/spinlock.h"
  62. #include "../common/thread.h"
  63. #include "../common/mutex.h"
  64. #endif
  65. ///////////////////////////////////////////////////////////////////////////////
  66. //## TODO possible enhancements: [FlavioJS]
  67. // - 'callfunc' supporting labels in the current npc "::LabelName"
  68. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  69. // - 'function FuncName;' function declarations reverting to global functions
  70. // if local label isn't found
  71. // - join callfunc and callsub's functionality
  72. // - remove dynamic allocation in add_word()
  73. // - remove GETVALUE / SETVALUE
  74. // - clean up the set_reg / set_val / setd_sub mess
  75. // - detect invalid label references at parse-time
  76. //
  77. // struct script_state* st;
  78. //
  79. /// Returns the script_data at the target index
  80. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  81. /// Returns if the stack contains data at the target index
  82. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  83. /// Returns the index of the last data in the stack
  84. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  85. /// Pushes an int into the stack
  86. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  87. /// Pushes a string into the stack (script engine frees it automatically)
  88. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  89. /// Pushes a copy of a string into the stack
  90. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  91. /// Pushes a constant string into the stack (must never change or be freed)
  92. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  93. /// Pushes a nil into the stack
  94. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  95. /// Pushes a copy of the data in the target index
  96. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  97. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  98. #define script_isint(st,i) data_isint(script_getdata(st,i))
  99. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  100. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  101. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  102. // Note: "top" functions/defines use indexes relative to the top of the stack
  103. // -1 is the index of the data at the top
  104. /// Returns the script_data at the target index relative to the top of the stack
  105. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  106. /// Pushes a copy of the data in the target index relative to the top of the stack
  107. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  108. /// Removes the range of values [start,end[ relative to the top of the stack
  109. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  110. //
  111. // struct script_data* data;
  112. //
  113. /// Returns if the script data is a string
  114. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  115. /// Returns if the script data is an int
  116. #define data_isint(data) ( (data)->type == C_INT )
  117. /// Returns if the script data is a reference
  118. #define data_isreference(data) ( (data)->type == C_NAME )
  119. /// Returns if the script data is a label
  120. #define data_islabel(data) ( (data)->type == C_POS )
  121. /// Returns if the script data is an internal script function label
  122. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  123. /// Returns if this is a reference to a constant
  124. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  125. /// Returns if this a reference to a param
  126. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  127. /// Returns if this a reference to a variable
  128. //##TODO confirm it's C_NAME [FlavioJS]
  129. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  130. /// Returns the unique id of the reference (id and index)
  131. #define reference_getuid(data) ( (data)->u.num )
  132. /// Returns the id of the reference
  133. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  134. /// Returns the array index of the reference
  135. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  136. /// Returns the name of the reference
  137. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  138. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  139. #define reference_getref(data) ( (data)->ref )
  140. /// Returns the value of the constant
  141. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  142. /// Returns the type of param
  143. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  144. /// Composes the uid of a reference from the id and the index
  145. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  146. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  147. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  148. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  149. #define FETCH(n, t) \
  150. if( script_hasdata(st,n) ) \
  151. (t)=script_getnum(st,n);
  152. /// Maximum amount of elements in script arrays
  153. #define SCRIPT_MAX_ARRAYSIZE 128
  154. #define SCRIPT_BLOCK_SIZE 512
  155. enum { LABEL_NEXTLINE=1,LABEL_START };
  156. /// temporary buffer for passing around compiled bytecode
  157. /// @see add_scriptb, set_label, parse_script
  158. static unsigned char* script_buf = NULL;
  159. static int script_pos = 0, script_size = 0;
  160. static inline int GETVALUE(const unsigned char* buf, int i)
  161. {
  162. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  163. }
  164. static inline void SETVALUE(unsigned char* buf, int i, int n)
  165. {
  166. buf[i] = GetByte(n, 0);
  167. buf[i+1] = GetByte(n, 1);
  168. buf[i+2] = GetByte(n, 2);
  169. }
  170. // String buffer structures.
  171. // str_data stores string information
  172. static struct str_data_struct {
  173. enum c_op type;
  174. int str;
  175. int backpatch;
  176. int label;
  177. int (*func)(struct script_state *st);
  178. int val;
  179. int next;
  180. } *str_data = NULL;
  181. static int str_data_size = 0; // size of the data
  182. static int str_num = LABEL_START; // next id to be assigned
  183. // str_buf holds the strings themselves
  184. static char *str_buf;
  185. static int str_size = 0; // size of the buffer
  186. static int str_pos = 0; // next position to be assigned
  187. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  188. #define SCRIPT_HASH_SIZE 1021
  189. int str_hash[SCRIPT_HASH_SIZE];
  190. // Specifies which string hashing method to use
  191. //#define SCRIPT_HASH_DJB2
  192. //#define SCRIPT_HASH_SDBM
  193. #define SCRIPT_HASH_ELF
  194. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  195. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  196. static int parse_options=0;
  197. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  198. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  199. // important buildin function references for usage in scripts
  200. static int buildin_set_ref = 0;
  201. static int buildin_callsub_ref = 0;
  202. static int buildin_callfunc_ref = 0;
  203. static int buildin_getelementofarray_ref = 0;
  204. // Caches compiled autoscript item code.
  205. // Note: This is not cleared when reloading itemdb.
  206. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  207. struct Script_Config script_config = {
  208. 1, // warn_func_mismatch_argtypes
  209. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  210. 0, INT_MAX, // input_min_value/input_max_value
  211. "OnPCDieEvent", //die_event_name
  212. "OnPCKillEvent", //kill_pc_event_name
  213. "OnNPCKillEvent", //kill_mob_event_name
  214. "OnPCLoginEvent", //login_event_name
  215. "OnPCLogoutEvent", //logout_event_name
  216. "OnPCLoadMapEvent", //loadmap_event_name
  217. "OnPCBaseLvUpEvent", //baselvup_event_name
  218. "OnPCJobLvUpEvent", //joblvup_event_name
  219. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  220. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  221. };
  222. static jmp_buf error_jump;
  223. static char* error_msg;
  224. static const char* error_pos;
  225. static int error_report; // if the error should produce output
  226. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  227. // [Eoe / jA 1080, 1081, 1094, 1164]
  228. enum curly_type {
  229. TYPE_NULL = 0,
  230. TYPE_IF,
  231. TYPE_SWITCH,
  232. TYPE_WHILE,
  233. TYPE_FOR,
  234. TYPE_DO,
  235. TYPE_USERFUNC,
  236. TYPE_ARGLIST // function argument list
  237. };
  238. enum e_arglist
  239. {
  240. ARGLIST_UNDEFINED = 0,
  241. ARGLIST_NO_PAREN = 1,
  242. ARGLIST_PAREN = 2,
  243. };
  244. static struct {
  245. struct {
  246. enum curly_type type;
  247. int index;
  248. int count;
  249. int flag;
  250. struct linkdb_node *case_label;
  251. } curly[256]; // Information right parenthesis
  252. int curly_count; // The number of right brackets
  253. int index; // Number of the syntax used in the script
  254. } syntax;
  255. const char* parse_curly_close(const char* p);
  256. const char* parse_syntax_close(const char* p);
  257. const char* parse_syntax_close_sub(const char* p,int* flag);
  258. const char* parse_syntax(const char* p);
  259. static int parse_syntax_for_flag = 0;
  260. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  261. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  262. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  263. int potion_target=0;
  264. c_op get_com(unsigned char *script,int *pos);
  265. int get_num(unsigned char *script,int *pos);
  266. typedef struct script_function {
  267. int (*func)(struct script_state *st);
  268. const char *name;
  269. const char *arg;
  270. } script_function;
  271. extern script_function buildin_func[];
  272. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  273. #ifdef BETA_THREAD_TEST
  274. /**
  275. * MySQL Query Slave
  276. **/
  277. static SPIN_LOCK queryThreadLock;
  278. static rAthread queryThread = NULL;
  279. static ramutex queryThreadMutex = NULL;
  280. static racond queryThreadCond = NULL;
  281. static volatile int32 queryThreadTerminate = 0;
  282. struct queryThreadEntry {
  283. bool ok;
  284. bool type; /* main db or log db? */
  285. struct script_state *st;
  286. };
  287. /* Ladies and Gentleman the Manager! */
  288. struct {
  289. struct queryThreadEntry **entry;/* array of structs */
  290. int count;
  291. int timer;/* used to receive processed entries */
  292. } queryThreadData;
  293. #endif
  294. /*==========================================
  295. * (Only those needed) local declaration prototype
  296. *------------------------------------------*/
  297. const char* parse_subexpr(const char* p,int limit);
  298. int run_func(struct script_state *st);
  299. enum {
  300. MF_NOMEMO, //0
  301. MF_NOTELEPORT,
  302. MF_NOSAVE,
  303. MF_NOBRANCH,
  304. MF_NOPENALTY,
  305. MF_NOZENYPENALTY,
  306. MF_PVP,
  307. MF_PVP_NOPARTY,
  308. MF_PVP_NOGUILD,
  309. MF_GVG,
  310. MF_GVG_NOPARTY, //10
  311. MF_NOTRADE,
  312. MF_NOSKILL,
  313. MF_NOWARP,
  314. MF_PARTYLOCK,
  315. MF_NOICEWALL,
  316. MF_SNOW,
  317. MF_FOG,
  318. MF_SAKURA,
  319. MF_LEAVES,
  320. /**
  321. * No longer available, keeping here just in case it's back someday. [Ind]
  322. **/
  323. //MF_RAIN, //20
  324. // 21 free
  325. MF_NOGO = 22,
  326. MF_CLOUDS,
  327. MF_CLOUDS2,
  328. MF_FIREWORKS,
  329. MF_GVG_CASTLE,
  330. MF_GVG_DUNGEON,
  331. MF_NIGHTENABLED,
  332. MF_NOBASEEXP,
  333. MF_NOJOBEXP, //30
  334. MF_NOMOBLOOT,
  335. MF_NOMVPLOOT,
  336. MF_NORETURN,
  337. MF_NOWARPTO,
  338. MF_NIGHTMAREDROP,
  339. MF_RESTRICTED,
  340. MF_NOCOMMAND,
  341. MF_NODROP,
  342. MF_JEXP,
  343. MF_BEXP, //40
  344. MF_NOVENDING,
  345. MF_LOADEVENT,
  346. MF_NOCHAT,
  347. MF_NOEXPPENALTY,
  348. MF_GUILDLOCK,
  349. MF_TOWN,
  350. MF_AUTOTRADE,
  351. MF_ALLOWKS,
  352. MF_MONSTER_NOTELEPORT,
  353. MF_PVP_NOCALCRANK, //50
  354. MF_BATTLEGROUND,
  355. MF_RESET
  356. };
  357. const char* script_op2name(int op)
  358. {
  359. #define RETURN_OP_NAME(type) case type: return #type
  360. switch( op )
  361. {
  362. RETURN_OP_NAME(C_NOP);
  363. RETURN_OP_NAME(C_POS);
  364. RETURN_OP_NAME(C_INT);
  365. RETURN_OP_NAME(C_PARAM);
  366. RETURN_OP_NAME(C_FUNC);
  367. RETURN_OP_NAME(C_STR);
  368. RETURN_OP_NAME(C_CONSTSTR);
  369. RETURN_OP_NAME(C_ARG);
  370. RETURN_OP_NAME(C_NAME);
  371. RETURN_OP_NAME(C_EOL);
  372. RETURN_OP_NAME(C_RETINFO);
  373. RETURN_OP_NAME(C_USERFUNC);
  374. RETURN_OP_NAME(C_USERFUNC_POS);
  375. // operators
  376. RETURN_OP_NAME(C_OP3);
  377. RETURN_OP_NAME(C_LOR);
  378. RETURN_OP_NAME(C_LAND);
  379. RETURN_OP_NAME(C_LE);
  380. RETURN_OP_NAME(C_LT);
  381. RETURN_OP_NAME(C_GE);
  382. RETURN_OP_NAME(C_GT);
  383. RETURN_OP_NAME(C_EQ);
  384. RETURN_OP_NAME(C_NE);
  385. RETURN_OP_NAME(C_XOR);
  386. RETURN_OP_NAME(C_OR);
  387. RETURN_OP_NAME(C_AND);
  388. RETURN_OP_NAME(C_ADD);
  389. RETURN_OP_NAME(C_SUB);
  390. RETURN_OP_NAME(C_MUL);
  391. RETURN_OP_NAME(C_DIV);
  392. RETURN_OP_NAME(C_MOD);
  393. RETURN_OP_NAME(C_NEG);
  394. RETURN_OP_NAME(C_LNOT);
  395. RETURN_OP_NAME(C_NOT);
  396. RETURN_OP_NAME(C_R_SHIFT);
  397. RETURN_OP_NAME(C_L_SHIFT);
  398. default:
  399. ShowDebug("script_op2name: unexpected op=%d\n", op);
  400. return "???";
  401. }
  402. #undef RETURN_OP_NAME
  403. }
  404. #ifdef DEBUG_DUMP_STACK
  405. static void script_dump_stack(struct script_state* st)
  406. {
  407. int i;
  408. ShowMessage("\tstart = %d\n", st->start);
  409. ShowMessage("\tend = %d\n", st->end);
  410. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  411. ShowMessage("\tsp = %d\n", st->stack->sp);
  412. for( i = 0; i < st->stack->sp; ++i )
  413. {
  414. struct script_data* data = &st->stack->stack_data[i];
  415. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  416. switch( data->type )
  417. {
  418. case C_INT:
  419. case C_POS:
  420. ShowMessage(" %d\n", data->u.num);
  421. break;
  422. case C_STR:
  423. case C_CONSTSTR:
  424. ShowMessage(" \"%s\"\n", data->u.str);
  425. break;
  426. case C_NAME:
  427. 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));
  428. break;
  429. case C_RETINFO:
  430. {
  431. struct script_retinfo* ri = data->u.ri;
  432. 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);
  433. }
  434. break;
  435. default:
  436. ShowMessage("\n");
  437. break;
  438. }
  439. }
  440. }
  441. #endif
  442. /// Reports on the console the src of a script error.
  443. static void script_reportsrc(struct script_state *st)
  444. {
  445. struct block_list* bl;
  446. if( st->oid == 0 )
  447. return; //Can't report source.
  448. bl = map_id2bl(st->oid);
  449. if( bl == NULL )
  450. return;
  451. switch( bl->type )
  452. {
  453. case BL_NPC:
  454. if( bl->m >= 0 )
  455. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  456. else
  457. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  458. break;
  459. default:
  460. if( bl->m >= 0 )
  461. 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);
  462. else
  463. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  464. break;
  465. }
  466. }
  467. /// Reports on the console information about the script data.
  468. static void script_reportdata(struct script_data* data)
  469. {
  470. if( data == NULL )
  471. return;
  472. switch( data->type )
  473. {
  474. case C_NOP:// no value
  475. ShowDebug("Data: nothing (nil)\n");
  476. break;
  477. case C_INT:// number
  478. ShowDebug("Data: number value=%d\n", data->u.num);
  479. break;
  480. case C_STR:
  481. case C_CONSTSTR:// string
  482. if( data->u.str )
  483. {
  484. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  485. }
  486. else
  487. {
  488. ShowDebug("Data: string value=NULL\n");
  489. }
  490. break;
  491. case C_NAME:// reference
  492. if( reference_tovariable(data) )
  493. {// variable
  494. const char* name = reference_getname(data);
  495. if( not_array_variable(*name) )
  496. ShowDebug("Data: variable name='%s'\n", name);
  497. else
  498. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  499. }
  500. else if( reference_toconstant(data) )
  501. {// constant
  502. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  503. }
  504. else if( reference_toparam(data) )
  505. {// param
  506. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  507. }
  508. else
  509. {// ???
  510. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  511. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  512. }
  513. break;
  514. case C_POS:// label
  515. ShowDebug("Data: label pos=%d\n", data->u.num);
  516. break;
  517. default:
  518. ShowDebug("Data: %s\n", script_op2name(data->type));
  519. break;
  520. }
  521. }
  522. /// Reports on the console information about the current built-in function.
  523. static void script_reportfunc(struct script_state* st)
  524. {
  525. int i, params, id;
  526. struct script_data* data;
  527. if( !script_hasdata(st,0) )
  528. {// no stack
  529. return;
  530. }
  531. data = script_getdata(st,0);
  532. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  533. {// script currently not executing a built-in function or corrupt stack
  534. return;
  535. }
  536. id = reference_getid(data);
  537. params = script_lastdata(st)-1;
  538. if( params > 0 )
  539. {
  540. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  541. for( i = 2; i <= script_lastdata(st); i++ )
  542. {
  543. script_reportdata(script_getdata(st,i));
  544. }
  545. }
  546. else
  547. {
  548. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  549. }
  550. }
  551. /*==========================================
  552. * Output error message
  553. *------------------------------------------*/
  554. static void disp_error_message2(const char *mes,const char *pos,int report)
  555. {
  556. error_msg = aStrdup(mes);
  557. error_pos = pos;
  558. error_report = report;
  559. longjmp( error_jump, 1 );
  560. }
  561. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  562. /// Checks event parameter validity
  563. static void check_event(struct script_state *st, const char *evt)
  564. {
  565. if( evt && evt[0] && !stristr(evt, "::On") )
  566. {
  567. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  568. script_reportsrc(st);
  569. }
  570. }
  571. /*==========================================
  572. * Hashes the input string
  573. *------------------------------------------*/
  574. static unsigned int calc_hash(const char* p)
  575. {
  576. unsigned int h;
  577. #if defined(SCRIPT_HASH_DJB2)
  578. h = 5381;
  579. while( *p ) // hash*33 + c
  580. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  581. #elif defined(SCRIPT_HASH_SDBM)
  582. h = 0;
  583. while( *p ) // hash*65599 + c
  584. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  585. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  586. h = 0;
  587. while( *p ){
  588. unsigned int g;
  589. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  590. g = h & 0xF0000000;
  591. if( g )
  592. {
  593. h ^= g >> 24;
  594. h &= ~g;
  595. }
  596. }
  597. #else // athena hash
  598. h = 0;
  599. while( *p )
  600. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  601. #endif
  602. return h % SCRIPT_HASH_SIZE;
  603. }
  604. /*==========================================
  605. * str_data manipulation functions
  606. *------------------------------------------*/
  607. /// Looks up string using the provided id.
  608. const char* get_str(int id)
  609. {
  610. Assert( id >= LABEL_START && id < str_size );
  611. return str_buf+str_data[id].str;
  612. }
  613. /// Returns the uid of the string, or -1.
  614. static int search_str(const char* p)
  615. {
  616. int i;
  617. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  618. if( strcasecmp(get_str(i),p) == 0 )
  619. return i;
  620. return -1;
  621. }
  622. /// Stores a copy of the string and returns its id.
  623. /// If an identical string is already present, returns its id instead.
  624. int add_str(const char* p)
  625. {
  626. int i, h;
  627. int len;
  628. h = calc_hash(p);
  629. if( str_hash[h] == 0 )
  630. {// empty bucket, add new node here
  631. str_hash[h] = str_num;
  632. }
  633. else
  634. {// scan for end of list, or occurence of identical string
  635. for( i = str_hash[h]; ; i = str_data[i].next )
  636. {
  637. if( strcasecmp(get_str(i),p) == 0 )
  638. return i; // string already in list
  639. if( str_data[i].next == 0 )
  640. break; // reached the end
  641. }
  642. // append node to end of list
  643. str_data[i].next = str_num;
  644. }
  645. // grow list if neccessary
  646. if( str_num >= str_data_size )
  647. {
  648. str_data_size += 128;
  649. RECREATE(str_data,struct str_data_struct,str_data_size);
  650. memset(str_data + (str_data_size - 128), '\0', 128);
  651. }
  652. len=(int)strlen(p);
  653. // grow string buffer if neccessary
  654. while( str_pos+len+1 >= str_size )
  655. {
  656. str_size += 256;
  657. RECREATE(str_buf,char,str_size);
  658. memset(str_buf + (str_size - 256), '\0', 256);
  659. }
  660. safestrncpy(str_buf+str_pos, p, len+1);
  661. str_data[str_num].type = C_NOP;
  662. str_data[str_num].str = str_pos;
  663. str_data[str_num].next = 0;
  664. str_data[str_num].func = NULL;
  665. str_data[str_num].backpatch = -1;
  666. str_data[str_num].label = -1;
  667. str_pos += len+1;
  668. return str_num++;
  669. }
  670. /// Appends 1 byte to the script buffer.
  671. static void add_scriptb(int a)
  672. {
  673. if( script_pos+1 >= script_size )
  674. {
  675. script_size += SCRIPT_BLOCK_SIZE;
  676. RECREATE(script_buf,unsigned char,script_size);
  677. }
  678. script_buf[script_pos++] = (uint8)(a);
  679. }
  680. /// Appends a c_op value to the script buffer.
  681. /// The value is variable-length encoded into 8-bit blocks.
  682. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  683. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  684. static void add_scriptc(int a)
  685. {
  686. while( a >= 0x40 )
  687. {
  688. add_scriptb((a&0x3f)|0x40);
  689. a = (a - 0x40) >> 6;
  690. }
  691. add_scriptb(a);
  692. }
  693. /// Appends an integer value to the script buffer.
  694. /// The value is variable-length encoded into 8-bit blocks.
  695. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  696. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  697. static void add_scripti(int a)
  698. {
  699. while( a >= 0x40 )
  700. {
  701. add_scriptb((a&0x3f)|0xc0);
  702. a = (a - 0x40) >> 6;
  703. }
  704. add_scriptb(a|0x80);
  705. }
  706. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  707. ///
  708. /// @param l The id of the str_data entry
  709. // Maximum up to 16M
  710. static void add_scriptl(int l)
  711. {
  712. int backpatch = str_data[l].backpatch;
  713. switch(str_data[l].type){
  714. case C_POS:
  715. case C_USERFUNC_POS:
  716. add_scriptc(C_POS);
  717. add_scriptb(str_data[l].label);
  718. add_scriptb(str_data[l].label>>8);
  719. add_scriptb(str_data[l].label>>16);
  720. break;
  721. case C_NOP:
  722. case C_USERFUNC:
  723. // Embedded data backpatch there is a possibility of label
  724. add_scriptc(C_NAME);
  725. str_data[l].backpatch = script_pos;
  726. add_scriptb(backpatch);
  727. add_scriptb(backpatch>>8);
  728. add_scriptb(backpatch>>16);
  729. break;
  730. case C_INT:
  731. add_scripti(abs(str_data[l].val));
  732. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  733. add_scriptc(C_NEG);
  734. break;
  735. default: // assume C_NAME
  736. add_scriptc(C_NAME);
  737. add_scriptb(l);
  738. add_scriptb(l>>8);
  739. add_scriptb(l>>16);
  740. break;
  741. }
  742. }
  743. /*==========================================
  744. * Resolve the label
  745. *------------------------------------------*/
  746. void set_label(int l,int pos, const char* script_pos)
  747. {
  748. int i,next;
  749. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  750. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  751. disp_error_message("set_label: invalid label name",script_pos);
  752. return;
  753. }
  754. if(str_data[l].label!=-1){
  755. disp_error_message("set_label: dup label ",script_pos);
  756. return;
  757. }
  758. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  759. str_data[l].label=pos;
  760. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  761. next=GETVALUE(script_buf,i);
  762. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  763. SETVALUE(script_buf,i,pos);
  764. i=next;
  765. }
  766. }
  767. /// Skips spaces and/or comments.
  768. const char* skip_space(const char* p)
  769. {
  770. if( p == NULL )
  771. return NULL;
  772. for(;;)
  773. {
  774. while( ISSPACE(*p) )
  775. ++p;
  776. if( *p == '/' && p[1] == '/' )
  777. {// line comment
  778. while(*p && *p!='\n')
  779. ++p;
  780. }
  781. else if( *p == '/' && p[1] == '*' )
  782. {// block comment
  783. p += 2;
  784. for(;;)
  785. {
  786. if( *p == '\0' )
  787. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  788. if( *p == '*' && p[1] == '/' )
  789. {// end of block comment
  790. p += 2;
  791. break;
  792. }
  793. ++p;
  794. }
  795. }
  796. else
  797. break;
  798. }
  799. return p;
  800. }
  801. /// Skips a word.
  802. /// A word consists of undercores and/or alfanumeric characters,
  803. /// and valid variable prefixes/postfixes.
  804. static
  805. const char* skip_word(const char* p)
  806. {
  807. // prefix
  808. switch( *p )
  809. {
  810. case '@':// temporary char variable
  811. ++p; break;
  812. case '#':// account variable
  813. p += ( p[1] == '#' ? 2 : 1 ); break;
  814. case '\'':// instance variable
  815. ++p; break;
  816. case '.':// npc variable
  817. p += ( p[1] == '@' ? 2 : 1 ); break;
  818. case '$':// global variable
  819. p += ( p[1] == '@' ? 2 : 1 ); break;
  820. }
  821. while( ISALNUM(*p) || *p == '_' )
  822. ++p;
  823. // postfix
  824. if( *p == '$' )// string
  825. p++;
  826. return p;
  827. }
  828. /// Adds a word to str_data.
  829. /// @see skip_word
  830. /// @see add_str
  831. static
  832. int add_word(const char* p)
  833. {
  834. char* word;
  835. int len;
  836. int i;
  837. // Check for a word
  838. len = skip_word(p) - p;
  839. if( len == 0 )
  840. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  841. // Duplicate the word
  842. word = (char*)aMalloc(len+1);
  843. memcpy(word, p, len);
  844. word[len] = 0;
  845. // add the word
  846. i = add_str(word);
  847. aFree(word);
  848. return i;
  849. }
  850. /// Parses a function call.
  851. /// The argument list can have parenthesis or not.
  852. /// The number of arguments is checked.
  853. static
  854. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  855. {
  856. const char* p2;
  857. const char* arg=NULL;
  858. int func;
  859. func = add_word(p);
  860. if( str_data[func].type == C_FUNC ){
  861. // buildin function
  862. add_scriptl(func);
  863. add_scriptc(C_ARG);
  864. arg = buildin_func[str_data[func].val].arg;
  865. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  866. // script defined function
  867. add_scriptl(buildin_callsub_ref);
  868. add_scriptc(C_ARG);
  869. add_scriptl(func);
  870. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  871. if( *arg == 0 )
  872. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  873. if( *arg != '*' )
  874. ++arg; // count func as argument
  875. } else {
  876. #ifdef SCRIPT_CALLFUNC_CHECK
  877. const char* name = get_str(func);
  878. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  879. #endif
  880. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  881. #ifdef SCRIPT_CALLFUNC_CHECK
  882. } else {;
  883. add_scriptl(buildin_callfunc_ref);
  884. add_scriptc(C_ARG);
  885. add_scriptc(C_STR);
  886. while( *name ) add_scriptb(*name ++);
  887. add_scriptb(0);
  888. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  889. if( *arg != '*' ) ++ arg;
  890. }
  891. #endif
  892. }
  893. p = skip_word(p);
  894. p = skip_space(p);
  895. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  896. syntax.curly[syntax.curly_count].count = 0;
  897. if( *p == ';' )
  898. {// <func name> ';'
  899. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  900. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  901. {// <func name> '(' ')'
  902. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  903. p = p2;
  904. /*
  905. } else if( 0 && require_paren && *p != '(' )
  906. {// <func name>
  907. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  908. */
  909. } else
  910. {// <func name> <arg list>
  911. if( require_paren ){
  912. if( *p != '(' )
  913. disp_error_message("need '('",p);
  914. ++p; // skip '('
  915. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  916. } else if( *p == '(' ){
  917. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  918. } else {
  919. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  920. }
  921. ++syntax.curly_count;
  922. while( *arg ) {
  923. p2=parse_subexpr(p,-1);
  924. if( p == p2 )
  925. break; // not an argument
  926. if( *arg != '*' )
  927. ++arg; // next argument
  928. p=skip_space(p2);
  929. if( *arg == 0 || *p != ',' )
  930. break; // no more arguments
  931. ++p; // skip comma
  932. }
  933. --syntax.curly_count;
  934. }
  935. if( *arg && *arg != '?' && *arg != '*' )
  936. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  937. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  938. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  939. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  940. if( *p != ')' )
  941. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  942. ++p;
  943. }
  944. add_scriptc(C_FUNC);
  945. return p;
  946. }
  947. /// Processes end of logical script line.
  948. /// @param first When true, only fix up scheduling data is initialized
  949. /// @param p Script position for error reporting in set_label
  950. static void parse_nextline(bool first, const char* p)
  951. {
  952. if( !first )
  953. {
  954. add_scriptc(C_EOL); // mark end of line for stack cleanup
  955. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  956. }
  957. // initialize data for new '-' label fix up scheduling
  958. str_data[LABEL_NEXTLINE].type = C_NOP;
  959. str_data[LABEL_NEXTLINE].backpatch = -1;
  960. str_data[LABEL_NEXTLINE].label = -1;
  961. }
  962. /// Parse a variable assignment using the direct equals operator
  963. /// @param p script position where the function should run from
  964. /// @return NULL if not a variable assignment, the new position otherwise
  965. const char* parse_variable(const char* p) {
  966. int i, j, word;
  967. c_op type = C_NOP;
  968. const char *p2 = NULL;
  969. const char *var = p;
  970. // skip the variable where applicable
  971. p = skip_word(p);
  972. p = skip_space(p);
  973. if( p == NULL ) {// end of the line or invalid buffer
  974. return NULL;
  975. }
  976. if( *p == '[' ) {// array variable so process the array as appropriate
  977. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  978. if( *p ++ == ']' && --(j) == 0 ) break;
  979. if( *p == '[' ) ++ j;
  980. }
  981. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  982. disp_error_message("Missing right expression or closing bracket for variable.", p);
  983. }
  984. }
  985. if( type == C_NOP &&
  986. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  987. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  988. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  989. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  990. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  991. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  992. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  993. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  994. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  995. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  996. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  997. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  998. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  999. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1000. ) )
  1001. {// failed to find a matching operator combination so invalid
  1002. return NULL;
  1003. }
  1004. switch( type ) {
  1005. case C_EQ: {// incremental modifier
  1006. p = skip_space( &p[1] );
  1007. }
  1008. break;
  1009. case C_L_SHIFT:
  1010. case C_R_SHIFT: {// left or right shift modifier
  1011. p = skip_space( &p[3] );
  1012. }
  1013. break;
  1014. default: {// normal incremental command
  1015. p = skip_space( &p[2] );
  1016. }
  1017. }
  1018. if( p == NULL ) {// end of line or invalid buffer
  1019. return NULL;
  1020. }
  1021. // push the set function onto the stack
  1022. add_scriptl(buildin_set_ref);
  1023. add_scriptc(C_ARG);
  1024. // always append parenthesis to avoid errors
  1025. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1026. syntax.curly[syntax.curly_count].count = 0;
  1027. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1028. // increment the total curly count for the position in the script
  1029. ++ syntax.curly_count;
  1030. // parse the variable currently being modified
  1031. word = add_word(var);
  1032. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1033. {// cannot assign a variable which exists as a function or label
  1034. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1035. }
  1036. if( p2 ) {// process the variable index
  1037. const char* p3 = NULL;
  1038. // push the getelementofarray method into the stack
  1039. add_scriptl(buildin_getelementofarray_ref);
  1040. add_scriptc(C_ARG);
  1041. add_scriptl(word);
  1042. // process the sub-expression for this assignment
  1043. p3 = parse_subexpr(p2 + 1, 1);
  1044. p3 = skip_space(p3);
  1045. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1046. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1047. }
  1048. // push the closing function stack operator onto the stack
  1049. add_scriptc(C_FUNC);
  1050. p3 ++;
  1051. } else {// simply push the variable or value onto the stack
  1052. add_scriptl(word);
  1053. }
  1054. if( type != C_EQ )
  1055. add_scriptc(C_REF);
  1056. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1057. add_scripti(1);
  1058. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1059. } else {// process the value as an expression
  1060. p = parse_subexpr(p, -1);
  1061. if( type != C_EQ )
  1062. {// push the type of modifier onto the stack
  1063. add_scriptc(type);
  1064. }
  1065. }
  1066. // decrement the curly count for the position within the script
  1067. -- syntax.curly_count;
  1068. // close the script by appending the function operator
  1069. add_scriptc(C_FUNC);
  1070. // push the buffer from the method
  1071. return p;
  1072. }
  1073. /*==========================================
  1074. * Analysis section
  1075. *------------------------------------------*/
  1076. const char* parse_simpleexpr(const char *p)
  1077. {
  1078. int i;
  1079. p=skip_space(p);
  1080. if(*p==';' || *p==',')
  1081. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  1082. if(*p=='('){
  1083. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1084. ++syntax.curly[i].count;
  1085. p=parse_subexpr(p+1,-1);
  1086. p=skip_space(p);
  1087. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1088. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1089. ){
  1090. if( *p == ',' ){
  1091. syntax.curly[i].flag = ARGLIST_PAREN;
  1092. return p;
  1093. } else
  1094. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1095. }
  1096. if( *p != ')' )
  1097. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  1098. ++p;
  1099. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  1100. char *np;
  1101. while(*p == '0' && ISDIGIT(p[1])) p++;
  1102. i=strtoul(p,&np,0);
  1103. add_scripti(i);
  1104. p=np;
  1105. } else if(*p=='"'){
  1106. add_scriptc(C_STR);
  1107. p++;
  1108. while( *p && *p != '"' ){
  1109. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1110. {
  1111. char buf[8];
  1112. size_t len = skip_escaped_c(p) - p;
  1113. size_t n = sv_unescape_c(buf, p, len);
  1114. if( n != 1 )
  1115. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1116. p += len;
  1117. add_scriptb(*buf);
  1118. continue;
  1119. }
  1120. else if( *p == '\n' )
  1121. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1122. add_scriptb(*p++);
  1123. }
  1124. if(!*p)
  1125. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1126. add_scriptb(0);
  1127. p++; //'"'
  1128. } else {
  1129. int l;
  1130. const char* pv;
  1131. // label , register , function etc
  1132. if(skip_word(p)==p)
  1133. disp_error_message("parse_simpleexpr: unexpected character",p);
  1134. l=add_word(p);
  1135. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1136. return parse_callfunc(p,1,0);
  1137. #ifdef SCRIPT_CALLFUNC_CHECK
  1138. else {
  1139. const char* name = get_str(l);
  1140. if( strdb_get(userfunc_db,name) != NULL ) {
  1141. return parse_callfunc(p,1,1);
  1142. }
  1143. }
  1144. #endif
  1145. if( (pv = parse_variable(p)) )
  1146. {// successfully processed a variable assignment
  1147. return pv;
  1148. }
  1149. p=skip_word(p);
  1150. if( *p == '[' ){
  1151. // array(name[i] => getelementofarray(name,i) )
  1152. add_scriptl(buildin_getelementofarray_ref);
  1153. add_scriptc(C_ARG);
  1154. add_scriptl(l);
  1155. p=parse_subexpr(p+1,-1);
  1156. p=skip_space(p);
  1157. if( *p != ']' )
  1158. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  1159. ++p;
  1160. add_scriptc(C_FUNC);
  1161. }else
  1162. add_scriptl(l);
  1163. }
  1164. return p;
  1165. }
  1166. /*==========================================
  1167. * Analysis of the expression
  1168. *------------------------------------------*/
  1169. const char* parse_subexpr(const char* p,int limit)
  1170. {
  1171. int op,opl,len;
  1172. const char* tmpp;
  1173. p=skip_space(p);
  1174. if( *p == '-' ){
  1175. tmpp = skip_space(p+1);
  1176. if( *tmpp == ';' || *tmpp == ',' ){
  1177. add_scriptl(LABEL_NEXTLINE);
  1178. p++;
  1179. return p;
  1180. }
  1181. }
  1182. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1183. p=parse_subexpr(p+1,10);
  1184. add_scriptc(op);
  1185. } else
  1186. p=parse_simpleexpr(p);
  1187. p=skip_space(p);
  1188. while((
  1189. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1190. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1191. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1192. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1193. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1194. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1195. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1196. (op=C_AND,opl=6,len=1,*p=='&') ||
  1197. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1198. (op=C_OR,opl=5,len=1,*p=='|') ||
  1199. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1200. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1201. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1202. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1203. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1204. (op=C_GT,opl=3,len=1,*p=='>') ||
  1205. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1206. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1207. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1208. p+=len;
  1209. if(op == C_OP3) {
  1210. p=parse_subexpr(p,-1);
  1211. p=skip_space(p);
  1212. if( *(p++) != ':')
  1213. disp_error_message("parse_subexpr: need ':'", p-1);
  1214. p=parse_subexpr(p,-1);
  1215. } else {
  1216. p=parse_subexpr(p,opl);
  1217. }
  1218. add_scriptc(op);
  1219. p=skip_space(p);
  1220. }
  1221. return p; /* return first untreated operator */
  1222. }
  1223. /*==========================================
  1224. * Evaluation of the expression
  1225. *------------------------------------------*/
  1226. const char* parse_expr(const char *p)
  1227. {
  1228. switch(*p){
  1229. case ')': case ';': case ':': case '[': case ']':
  1230. case '}':
  1231. disp_error_message("parse_expr: unexpected char",p);
  1232. }
  1233. p=parse_subexpr(p,-1);
  1234. return p;
  1235. }
  1236. /*==========================================
  1237. * Analysis of the line
  1238. *------------------------------------------*/
  1239. const char* parse_line(const char* p)
  1240. {
  1241. const char* p2;
  1242. p=skip_space(p);
  1243. if(*p==';') {
  1244. //Close decision for if(); for(); while();
  1245. p = parse_syntax_close(p + 1);
  1246. return p;
  1247. }
  1248. if(*p==')' && parse_syntax_for_flag)
  1249. return p+1;
  1250. p = skip_space(p);
  1251. if(p[0] == '{') {
  1252. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1253. syntax.curly[syntax.curly_count].count = -1;
  1254. syntax.curly[syntax.curly_count].index = -1;
  1255. syntax.curly_count++;
  1256. return p + 1;
  1257. } else if(p[0] == '}') {
  1258. return parse_curly_close(p);
  1259. }
  1260. // Syntax-related processing
  1261. p2 = parse_syntax(p);
  1262. if(p2 != NULL)
  1263. return p2;
  1264. // attempt to process a variable assignment
  1265. p2 = parse_variable(p);
  1266. if( p2 != NULL )
  1267. {// variable assignment processed so leave the method
  1268. return parse_syntax_close(p2 + 1);
  1269. }
  1270. p = parse_callfunc(p,0,0);
  1271. p = skip_space(p);
  1272. if(parse_syntax_for_flag) {
  1273. if( *p != ')' )
  1274. disp_error_message("parse_line: need ')'",p);
  1275. } else {
  1276. if( *p != ';' )
  1277. disp_error_message("parse_line: need ';'",p);
  1278. }
  1279. //Binding decision for if(), for(), while()
  1280. p = parse_syntax_close(p+1);
  1281. return p;
  1282. }
  1283. // { ... } Closing process
  1284. const char* parse_curly_close(const char* p)
  1285. {
  1286. if(syntax.curly_count <= 0) {
  1287. disp_error_message("parse_curly_close: unexpected string",p);
  1288. return p + 1;
  1289. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1290. syntax.curly_count--;
  1291. //Close decision if, for , while
  1292. p = parse_syntax_close(p + 1);
  1293. return p;
  1294. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1295. //Closing switch()
  1296. int pos = syntax.curly_count-1;
  1297. char label[256];
  1298. int l;
  1299. // Remove temporary variables
  1300. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1301. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1302. parse_line(label);
  1303. syntax.curly_count--;
  1304. // Go to the end pointer unconditionally
  1305. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1306. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1307. parse_line(label);
  1308. syntax.curly_count--;
  1309. // You are here labeled
  1310. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1311. l=add_str(label);
  1312. set_label(l,script_pos, p);
  1313. if(syntax.curly[pos].flag) {
  1314. //Exists default
  1315. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1316. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1317. parse_line(label);
  1318. syntax.curly_count--;
  1319. }
  1320. // Label end
  1321. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1322. l=add_str(label);
  1323. set_label(l,script_pos, p);
  1324. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1325. syntax.curly_count--;
  1326. //Closing decision if, for , while
  1327. p = parse_syntax_close(p + 1);
  1328. return p;
  1329. } else {
  1330. disp_error_message("parse_curly_close: unexpected string",p);
  1331. return p + 1;
  1332. }
  1333. }
  1334. // Syntax-related processing
  1335. // break, case, continue, default, do, for, function,
  1336. // if, switch, while ? will handle this internally.
  1337. const char* parse_syntax(const char* p)
  1338. {
  1339. const char *p2 = skip_word(p);
  1340. switch(*p) {
  1341. case 'B':
  1342. case 'b':
  1343. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1344. // break Processing
  1345. char label[256];
  1346. int pos = syntax.curly_count - 1;
  1347. while(pos >= 0) {
  1348. if(syntax.curly[pos].type == TYPE_DO) {
  1349. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1350. break;
  1351. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1352. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1353. break;
  1354. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1355. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1356. break;
  1357. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1358. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1359. break;
  1360. }
  1361. pos--;
  1362. }
  1363. if(pos < 0) {
  1364. disp_error_message("parse_syntax: unexpected 'break'",p);
  1365. } else {
  1366. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1367. parse_line(label);
  1368. syntax.curly_count--;
  1369. }
  1370. p = skip_space(p2);
  1371. if(*p != ';')
  1372. disp_error_message("parse_syntax: need ';'",p);
  1373. // Closing decision if, for , while
  1374. p = parse_syntax_close(p + 1);
  1375. return p;
  1376. }
  1377. break;
  1378. case 'c':
  1379. case 'C':
  1380. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1381. //Processing case
  1382. int pos = syntax.curly_count-1;
  1383. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1384. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1385. return p+1;
  1386. } else {
  1387. char label[256];
  1388. int l,v;
  1389. char *np;
  1390. if(syntax.curly[pos].count != 1) {
  1391. //Jump for FALLTHRU
  1392. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1393. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1394. parse_line(label);
  1395. syntax.curly_count--;
  1396. // You are here labeled
  1397. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1398. l=add_str(label);
  1399. set_label(l,script_pos, p);
  1400. }
  1401. //Decision statement switch
  1402. p = skip_space(p2);
  1403. if(p == p2) {
  1404. disp_error_message("parse_syntax: expect space ' '",p);
  1405. }
  1406. // check whether case label is integer or not
  1407. v = strtol(p,&np,0);
  1408. if(np == p) { //Check for constants
  1409. p2 = skip_word(p);
  1410. v = p2-p; // length of word at p2
  1411. memcpy(label,p,v);
  1412. label[v]='\0';
  1413. if( !script_get_constant(label, &v) )
  1414. disp_error_message("parse_syntax: 'case' label not integer",p);
  1415. p = skip_word(p);
  1416. } else { //Numeric value
  1417. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1418. p++;
  1419. p = skip_word(p);
  1420. if(np != p)
  1421. disp_error_message("parse_syntax: 'case' label not integer",np);
  1422. }
  1423. p = skip_space(p);
  1424. if(*p != ':')
  1425. disp_error_message("parse_syntax: expect ':'",p);
  1426. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1427. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1428. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1429. // Bad I do not parse twice
  1430. p2 = parse_line(label);
  1431. parse_line(p2);
  1432. syntax.curly_count--;
  1433. if(syntax.curly[pos].count != 1) {
  1434. // Label after the completion of FALLTHRU
  1435. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1436. l=add_str(label);
  1437. set_label(l,script_pos,p);
  1438. }
  1439. // check duplication of case label [Rayce]
  1440. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1441. disp_error_message("parse_syntax: dup 'case'",p);
  1442. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1443. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1444. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1445. parse_line(label);
  1446. syntax.curly_count--;
  1447. syntax.curly[pos].count++;
  1448. }
  1449. return p + 1;
  1450. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1451. // Processing continue
  1452. char label[256];
  1453. int pos = syntax.curly_count - 1;
  1454. while(pos >= 0) {
  1455. if(syntax.curly[pos].type == TYPE_DO) {
  1456. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1457. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1458. break;
  1459. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1460. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1461. break;
  1462. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1463. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1464. break;
  1465. }
  1466. pos--;
  1467. }
  1468. if(pos < 0) {
  1469. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1470. } else {
  1471. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1472. parse_line(label);
  1473. syntax.curly_count--;
  1474. }
  1475. p = skip_space(p2);
  1476. if(*p != ';')
  1477. disp_error_message("parse_syntax: need ';'",p);
  1478. //Closing decision if, for , while
  1479. p = parse_syntax_close(p + 1);
  1480. return p;
  1481. }
  1482. break;
  1483. case 'd':
  1484. case 'D':
  1485. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1486. // Switch - default processing
  1487. int pos = syntax.curly_count-1;
  1488. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1489. disp_error_message("parse_syntax: unexpected 'default'",p);
  1490. } else if(syntax.curly[pos].flag) {
  1491. disp_error_message("parse_syntax: dup 'default'",p);
  1492. } else {
  1493. char label[256];
  1494. int l;
  1495. // Put the label location
  1496. p = skip_space(p2);
  1497. if(*p != ':') {
  1498. disp_error_message("parse_syntax: need ':'",p);
  1499. }
  1500. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1501. l=add_str(label);
  1502. set_label(l,script_pos,p);
  1503. // Skip to the next link w/o condition
  1504. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1505. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1506. parse_line(label);
  1507. syntax.curly_count--;
  1508. // The default label
  1509. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1510. l=add_str(label);
  1511. set_label(l,script_pos,p);
  1512. syntax.curly[syntax.curly_count - 1].flag = 1;
  1513. syntax.curly[pos].count++;
  1514. }
  1515. return p + 1;
  1516. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1517. int l;
  1518. char label[256];
  1519. p=skip_space(p2);
  1520. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1521. syntax.curly[syntax.curly_count].count = 1;
  1522. syntax.curly[syntax.curly_count].index = syntax.index++;
  1523. syntax.curly[syntax.curly_count].flag = 0;
  1524. // Label of the (do) form here
  1525. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1526. l=add_str(label);
  1527. set_label(l,script_pos,p);
  1528. syntax.curly_count++;
  1529. return p;
  1530. }
  1531. break;
  1532. case 'f':
  1533. case 'F':
  1534. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1535. int l;
  1536. char label[256];
  1537. int pos = syntax.curly_count;
  1538. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1539. syntax.curly[syntax.curly_count].count = 1;
  1540. syntax.curly[syntax.curly_count].index = syntax.index++;
  1541. syntax.curly[syntax.curly_count].flag = 0;
  1542. syntax.curly_count++;
  1543. p=skip_space(p2);
  1544. if(*p != '(')
  1545. disp_error_message("parse_syntax: need '('",p);
  1546. p++;
  1547. // Execute the initialization statement
  1548. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1549. p=parse_line(p);
  1550. syntax.curly_count--;
  1551. // Form the start of label decision
  1552. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1553. l=add_str(label);
  1554. set_label(l,script_pos,p);
  1555. p=skip_space(p);
  1556. if(*p == ';') {
  1557. // For (; Because the pattern of always true ;)
  1558. ;
  1559. } else {
  1560. // Skip to the end point if the condition is false
  1561. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1562. add_scriptl(add_str("jump_zero"));
  1563. add_scriptc(C_ARG);
  1564. p=parse_expr(p);
  1565. p=skip_space(p);
  1566. add_scriptl(add_str(label));
  1567. add_scriptc(C_FUNC);
  1568. }
  1569. if(*p != ';')
  1570. disp_error_message("parse_syntax: need ';'",p);
  1571. p++;
  1572. // Skip to the beginning of the loop
  1573. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1574. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1575. parse_line(label);
  1576. syntax.curly_count--;
  1577. // Labels to form the next loop
  1578. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1579. l=add_str(label);
  1580. set_label(l,script_pos,p);
  1581. // Process the next time you enter the loop
  1582. // A ')' last for; flag to be treated as'
  1583. parse_syntax_for_flag = 1;
  1584. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1585. p=parse_line(p);
  1586. syntax.curly_count--;
  1587. parse_syntax_for_flag = 0;
  1588. // Skip to the determination process conditions
  1589. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1590. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1591. parse_line(label);
  1592. syntax.curly_count--;
  1593. // Loop start labeling
  1594. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1595. l=add_str(label);
  1596. set_label(l,script_pos,p);
  1597. return p;
  1598. }
  1599. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1600. {// internal script function
  1601. const char *func_name;
  1602. func_name = skip_space(p2);
  1603. p = skip_word(func_name);
  1604. if( p == func_name )
  1605. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1606. p2 = skip_space(p);
  1607. if( *p2 == ';' )
  1608. {// function <name> ;
  1609. // function declaration - just register the name
  1610. int l;
  1611. l = add_word(func_name);
  1612. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1613. str_data[l].type = C_USERFUNC;
  1614. else if( str_data[l].type == C_USERFUNC )
  1615. ; // already registered
  1616. else
  1617. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1618. // Close condition of if, for, while
  1619. p = parse_syntax_close(p2 + 1);
  1620. return p;
  1621. }
  1622. else if(*p2 == '{')
  1623. {// function <name> <line/block of code>
  1624. char label[256];
  1625. int l;
  1626. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1627. syntax.curly[syntax.curly_count].count = 1;
  1628. syntax.curly[syntax.curly_count].index = syntax.index++;
  1629. syntax.curly[syntax.curly_count].flag = 0;
  1630. ++syntax.curly_count;
  1631. // Jump over the function code
  1632. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1633. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1634. ++syntax.curly_count;
  1635. parse_line(label);
  1636. --syntax.curly_count;
  1637. // Set the position of the function (label)
  1638. l=add_word(func_name);
  1639. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1640. {
  1641. str_data[l].type = C_USERFUNC;
  1642. set_label(l, script_pos, p);
  1643. if( parse_options&SCRIPT_USE_LABEL_DB )
  1644. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1645. }
  1646. else
  1647. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1648. return skip_space(p);
  1649. }
  1650. else
  1651. {
  1652. disp_error_message("expect ';' or '{' at function syntax",p);
  1653. }
  1654. }
  1655. break;
  1656. case 'i':
  1657. case 'I':
  1658. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1659. // If process
  1660. char label[256];
  1661. p=skip_space(p2);
  1662. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1663. disp_error_message("need '('",p);
  1664. }
  1665. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1666. syntax.curly[syntax.curly_count].count = 1;
  1667. syntax.curly[syntax.curly_count].index = syntax.index++;
  1668. syntax.curly[syntax.curly_count].flag = 0;
  1669. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1670. syntax.curly_count++;
  1671. add_scriptl(add_str("jump_zero"));
  1672. add_scriptc(C_ARG);
  1673. p=parse_expr(p);
  1674. p=skip_space(p);
  1675. add_scriptl(add_str(label));
  1676. add_scriptc(C_FUNC);
  1677. return p;
  1678. }
  1679. break;
  1680. case 's':
  1681. case 'S':
  1682. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1683. // Processing of switch ()
  1684. char label[256];
  1685. p=skip_space(p2);
  1686. if(*p != '(') {
  1687. disp_error_message("need '('",p);
  1688. }
  1689. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1690. syntax.curly[syntax.curly_count].count = 1;
  1691. syntax.curly[syntax.curly_count].index = syntax.index++;
  1692. syntax.curly[syntax.curly_count].flag = 0;
  1693. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1694. syntax.curly_count++;
  1695. add_scriptl(add_str("set"));
  1696. add_scriptc(C_ARG);
  1697. add_scriptl(add_str(label));
  1698. p=parse_expr(p);
  1699. p=skip_space(p);
  1700. if(*p != '{') {
  1701. disp_error_message("parse_syntax: need '{'",p);
  1702. }
  1703. add_scriptc(C_FUNC);
  1704. return p + 1;
  1705. }
  1706. break;
  1707. case 'w':
  1708. case 'W':
  1709. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1710. int l;
  1711. char label[256];
  1712. p=skip_space(p2);
  1713. if(*p != '(') {
  1714. disp_error_message("need '('",p);
  1715. }
  1716. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1717. syntax.curly[syntax.curly_count].count = 1;
  1718. syntax.curly[syntax.curly_count].index = syntax.index++;
  1719. syntax.curly[syntax.curly_count].flag = 0;
  1720. // Form the start of label decision
  1721. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1722. l=add_str(label);
  1723. set_label(l,script_pos,p);
  1724. // Skip to the end point if the condition is false
  1725. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1726. syntax.curly_count++;
  1727. add_scriptl(add_str("jump_zero"));
  1728. add_scriptc(C_ARG);
  1729. p=parse_expr(p);
  1730. p=skip_space(p);
  1731. add_scriptl(add_str(label));
  1732. add_scriptc(C_FUNC);
  1733. return p;
  1734. }
  1735. break;
  1736. }
  1737. return NULL;
  1738. }
  1739. const char* parse_syntax_close(const char *p) {
  1740. // If (...) for (...) hoge (); as to make sure closed closed once again
  1741. int flag;
  1742. do {
  1743. p = parse_syntax_close_sub(p,&flag);
  1744. } while(flag);
  1745. return p;
  1746. }
  1747. // Close judgment if, for, while, of do
  1748. // flag == 1 : closed
  1749. // flag == 0 : not closed
  1750. const char* parse_syntax_close_sub(const char* p,int* flag)
  1751. {
  1752. char label[256];
  1753. int pos = syntax.curly_count - 1;
  1754. int l;
  1755. *flag = 1;
  1756. if(syntax.curly_count <= 0) {
  1757. *flag = 0;
  1758. return p;
  1759. } else if(syntax.curly[pos].type == TYPE_IF) {
  1760. const char *bp = p;
  1761. const char *p2;
  1762. // if-block and else-block end is a new line
  1763. parse_nextline(false, p);
  1764. // Skip to the last location if
  1765. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1766. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1767. parse_line(label);
  1768. syntax.curly_count--;
  1769. // Put the label of the location
  1770. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1771. l=add_str(label);
  1772. set_label(l,script_pos,p);
  1773. syntax.curly[pos].count++;
  1774. p = skip_space(p);
  1775. p2 = skip_word(p);
  1776. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1777. // else or else - if
  1778. p = skip_space(p2);
  1779. p2 = skip_word(p);
  1780. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1781. // else - if
  1782. p=skip_space(p2);
  1783. if(*p != '(') {
  1784. disp_error_message("need '('",p);
  1785. }
  1786. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1787. add_scriptl(add_str("jump_zero"));
  1788. add_scriptc(C_ARG);
  1789. p=parse_expr(p);
  1790. p=skip_space(p);
  1791. add_scriptl(add_str(label));
  1792. add_scriptc(C_FUNC);
  1793. *flag = 0;
  1794. return p;
  1795. } else {
  1796. // else
  1797. if(!syntax.curly[pos].flag) {
  1798. syntax.curly[pos].flag = 1;
  1799. *flag = 0;
  1800. return p;
  1801. }
  1802. }
  1803. }
  1804. // Close if
  1805. syntax.curly_count--;
  1806. // Put the label of the final location
  1807. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1808. l=add_str(label);
  1809. set_label(l,script_pos,p);
  1810. if(syntax.curly[pos].flag == 1) {
  1811. // Because the position of the pointer is the same if not else for this
  1812. return bp;
  1813. }
  1814. return p;
  1815. } else if(syntax.curly[pos].type == TYPE_DO) {
  1816. int l;
  1817. char label[256];
  1818. const char *p2;
  1819. if(syntax.curly[pos].flag) {
  1820. // (Come here continue) to form the label here
  1821. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1822. l=add_str(label);
  1823. set_label(l,script_pos,p);
  1824. }
  1825. // Skip to the end point if the condition is false
  1826. p = skip_space(p);
  1827. p2 = skip_word(p);
  1828. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1829. disp_error_message("parse_syntax: need 'while'",p);
  1830. p = skip_space(p2);
  1831. if(*p != '(') {
  1832. disp_error_message("need '('",p);
  1833. }
  1834. // do-block end is a new line
  1835. parse_nextline(false, p);
  1836. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1837. add_scriptl(add_str("jump_zero"));
  1838. add_scriptc(C_ARG);
  1839. p=parse_expr(p);
  1840. p=skip_space(p);
  1841. add_scriptl(add_str(label));
  1842. add_scriptc(C_FUNC);
  1843. // Skip to the starting point
  1844. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1845. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1846. parse_line(label);
  1847. syntax.curly_count--;
  1848. // Form label of the end point conditions
  1849. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1850. l=add_str(label);
  1851. set_label(l,script_pos,p);
  1852. p = skip_space(p);
  1853. if(*p != ';') {
  1854. disp_error_message("parse_syntax: need ';'",p);
  1855. return p+1;
  1856. }
  1857. p++;
  1858. syntax.curly_count--;
  1859. return p;
  1860. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1861. // for-block end is a new line
  1862. parse_nextline(false, p);
  1863. // Skip to the next loop
  1864. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1865. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1866. parse_line(label);
  1867. syntax.curly_count--;
  1868. // End for labeling
  1869. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1870. l=add_str(label);
  1871. set_label(l,script_pos,p);
  1872. syntax.curly_count--;
  1873. return p;
  1874. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1875. // while-block end is a new line
  1876. parse_nextline(false, p);
  1877. // Skip to the decision while
  1878. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1879. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1880. parse_line(label);
  1881. syntax.curly_count--;
  1882. // End while labeling
  1883. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1884. l=add_str(label);
  1885. set_label(l,script_pos,p);
  1886. syntax.curly_count--;
  1887. return p;
  1888. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1889. int pos = syntax.curly_count-1;
  1890. char label[256];
  1891. int l;
  1892. // Back
  1893. sprintf(label,"return;");
  1894. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1895. parse_line(label);
  1896. syntax.curly_count--;
  1897. // Put the label of the location
  1898. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1899. l=add_str(label);
  1900. set_label(l,script_pos,p);
  1901. syntax.curly_count--;
  1902. return p;
  1903. } else {
  1904. *flag = 0;
  1905. return p;
  1906. }
  1907. }
  1908. /*==========================================
  1909. * Added built-in functions
  1910. *------------------------------------------*/
  1911. static void add_buildin_func(void)
  1912. {
  1913. int i,n;
  1914. const char* p;
  1915. for( i = 0; buildin_func[i].func; i++ )
  1916. {
  1917. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1918. // 'v' - value (either string or int or reference)
  1919. // 's' - string
  1920. // 'i' - int
  1921. // 'r' - reference (of a variable)
  1922. // 'l' - label
  1923. // '?' - one optional parameter
  1924. // '*' - unknown number of optional parameters
  1925. p = buildin_func[i].arg;
  1926. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1927. while( *p == '?' ) ++p;
  1928. if( *p == '*' ) ++p;
  1929. if( *p != 0){
  1930. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1931. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1932. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1933. } else {
  1934. n = add_str(buildin_func[i].name);
  1935. str_data[n].type = C_FUNC;
  1936. str_data[n].val = i;
  1937. str_data[n].func = buildin_func[i].func;
  1938. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1939. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1940. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1941. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1942. }
  1943. }
  1944. }
  1945. /// Retrieves the value of a constant.
  1946. bool script_get_constant(const char* name, int* value)
  1947. {
  1948. int n = search_str(name);
  1949. if( n == -1 || str_data[n].type != C_INT )
  1950. {// not found or not a constant
  1951. return false;
  1952. }
  1953. value[0] = str_data[n].val;
  1954. return true;
  1955. }
  1956. /// Creates new constant or parameter with given value.
  1957. void script_set_constant(const char* name, int value, bool isparameter)
  1958. {
  1959. int n = add_str(name);
  1960. if( str_data[n].type == C_NOP )
  1961. {// new
  1962. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1963. str_data[n].val = value;
  1964. }
  1965. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1966. {// existing parameter or constant
  1967. 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);
  1968. }
  1969. else
  1970. {// existing name
  1971. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1972. }
  1973. }
  1974. /*==========================================
  1975. * Reading constant databases
  1976. * const.txt
  1977. *------------------------------------------*/
  1978. static void read_constdb(void)
  1979. {
  1980. FILE *fp;
  1981. char line[1024],name[1024],val[1024];
  1982. int type;
  1983. sprintf(line, "%s/const.txt", db_path);
  1984. fp=fopen(line, "r");
  1985. if(fp==NULL){
  1986. ShowError("can't read %s\n", line);
  1987. return ;
  1988. }
  1989. while(fgets(line, sizeof(line), fp))
  1990. {
  1991. if(line[0]=='/' && line[1]=='/')
  1992. continue;
  1993. type=0;
  1994. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1995. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1996. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  1997. }
  1998. }
  1999. fclose(fp);
  2000. }
  2001. /*==========================================
  2002. * Display emplacement line of script
  2003. *------------------------------------------*/
  2004. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2005. {
  2006. int i;
  2007. if( p == NULL || !p[0] ) return NULL;
  2008. if( line < 0 )
  2009. StringBuf_Printf(buf, "*% 5d : ", -line);
  2010. else
  2011. StringBuf_Printf(buf, " % 5d : ", line);
  2012. for(i=0;p[i] && p[i] != '\n';i++){
  2013. if(p + i != mark)
  2014. StringBuf_Printf(buf, "%c", p[i]);
  2015. else
  2016. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2017. }
  2018. StringBuf_AppendStr(buf, "\n");
  2019. return p+i+(p[i] == '\n' ? 1 : 0);
  2020. }
  2021. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  2022. {
  2023. // Find the line where the error occurred
  2024. int j;
  2025. int line = start_line;
  2026. const char *p;
  2027. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2028. StringBuf buf;
  2029. for(p=src;p && *p;line++){
  2030. const char *lineend=strchr(p,'\n');
  2031. if(lineend==NULL || error_pos<lineend){
  2032. break;
  2033. }
  2034. for( j = 0; j < 4; j++ ) {
  2035. linestart[j] = linestart[j+1];
  2036. }
  2037. linestart[4] = p;
  2038. p=lineend+1;
  2039. }
  2040. StringBuf_Init(&buf);
  2041. StringBuf_AppendStr(&buf, "\a\n");
  2042. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  2043. StringBuf_Printf(&buf, " %s\n", error_msg);
  2044. for(j = 0; j < 5; j++ ) {
  2045. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  2046. }
  2047. p = script_print_line(&buf, p, error_pos, -line);
  2048. for(j = 0; j < 5; j++) {
  2049. p = script_print_line(&buf, p, NULL, line + j + 1 );
  2050. }
  2051. ShowError("%s", StringBuf_Value(&buf));
  2052. StringBuf_Destroy(&buf);
  2053. }
  2054. /*==========================================
  2055. * Analysis of the script
  2056. *------------------------------------------*/
  2057. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2058. {
  2059. const char *p,*tmpp;
  2060. int i;
  2061. struct script_code* code = NULL;
  2062. static int first=1;
  2063. char end;
  2064. bool unresolved_names = false;
  2065. if( src == NULL )
  2066. return NULL;// empty script
  2067. memset(&syntax,0,sizeof(syntax));
  2068. if(first){
  2069. add_buildin_func();
  2070. read_constdb();
  2071. first=0;
  2072. }
  2073. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2074. script_pos=0;
  2075. script_size=SCRIPT_BLOCK_SIZE;
  2076. parse_nextline(true, NULL);
  2077. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2078. if( options&SCRIPT_USE_LABEL_DB )
  2079. db_clear(scriptlabel_db);
  2080. parse_options = options;
  2081. if( setjmp( error_jump ) != 0 ) {
  2082. //Restore program state when script has problems. [from jA]
  2083. int i;
  2084. const int size = ARRAYLENGTH(syntax.curly);
  2085. if( error_report )
  2086. script_error(src,file,line,error_msg,error_pos);
  2087. aFree( error_msg );
  2088. aFree( script_buf );
  2089. script_pos = 0;
  2090. script_size = 0;
  2091. script_buf = NULL;
  2092. for(i=LABEL_START;i<str_num;i++)
  2093. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  2094. for(i=0; i<size; i++)
  2095. linkdb_final(&syntax.curly[i].case_label);
  2096. return NULL;
  2097. }
  2098. parse_syntax_for_flag=0;
  2099. p=src;
  2100. p=skip_space(p);
  2101. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2102. {// does not require brackets around the script
  2103. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2104. {// empty script and can return NULL
  2105. aFree( script_buf );
  2106. script_pos = 0;
  2107. script_size = 0;
  2108. script_buf = NULL;
  2109. return NULL;
  2110. }
  2111. end = '\0';
  2112. }
  2113. else
  2114. {// requires brackets around the script
  2115. if( *p != '{' )
  2116. disp_error_message("not found '{'",p);
  2117. p = skip_space(p+1);
  2118. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2119. {// empty script and can return NULL
  2120. aFree( script_buf );
  2121. script_pos = 0;
  2122. script_size = 0;
  2123. script_buf = NULL;
  2124. return NULL;
  2125. }
  2126. end = '}';
  2127. }
  2128. // clear references of labels, variables and internal functions
  2129. for(i=LABEL_START;i<str_num;i++){
  2130. if(
  2131. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2132. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2133. ){
  2134. str_data[i].type=C_NOP;
  2135. str_data[i].backpatch=-1;
  2136. str_data[i].label=-1;
  2137. }
  2138. }
  2139. while( syntax.curly_count != 0 || *p != end )
  2140. {
  2141. if( *p == '\0' )
  2142. disp_error_message("unexpected end of script",p);
  2143. // Special handling only label
  2144. tmpp=skip_space(skip_word(p));
  2145. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2146. i=add_word(p);
  2147. set_label(i,script_pos,p);
  2148. if( parse_options&SCRIPT_USE_LABEL_DB )
  2149. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2150. p=tmpp+1;
  2151. p=skip_space(p);
  2152. continue;
  2153. }
  2154. // All other lumped
  2155. p=parse_line(p);
  2156. p=skip_space(p);
  2157. parse_nextline(false, p);
  2158. }
  2159. add_scriptc(C_NOP);
  2160. // trim code to size
  2161. script_size = script_pos;
  2162. RECREATE(script_buf,unsigned char,script_pos);
  2163. // default unknown references to variables
  2164. for(i=LABEL_START;i<str_num;i++){
  2165. if(str_data[i].type==C_NOP){
  2166. int j,next;
  2167. str_data[i].type=C_NAME;
  2168. str_data[i].label=i;
  2169. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2170. next=GETVALUE(script_buf,j);
  2171. SETVALUE(script_buf,j,i);
  2172. j=next;
  2173. }
  2174. }
  2175. else if( str_data[i].type == C_USERFUNC )
  2176. {// 'function name;' without follow-up code
  2177. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2178. unresolved_names = true;
  2179. }
  2180. }
  2181. if( unresolved_names )
  2182. {
  2183. disp_error_message("parse_script: unresolved function references", p);
  2184. }
  2185. #ifdef DEBUG_DISP
  2186. for(i=0;i<script_pos;i++){
  2187. if((i&15)==0) ShowMessage("%04x : ",i);
  2188. ShowMessage("%02x ",script_buf[i]);
  2189. if((i&15)==15) ShowMessage("\n");
  2190. }
  2191. ShowMessage("\n");
  2192. #endif
  2193. #ifdef DEBUG_DISASM
  2194. {
  2195. int i = 0,j;
  2196. while(i < script_pos) {
  2197. c_op op = get_com(script_buf,&i);
  2198. ShowMessage("%06x %s", i, script_op2name(op));
  2199. j = i;
  2200. switch(op) {
  2201. case C_INT:
  2202. ShowMessage(" %d", get_num(script_buf,&i));
  2203. break;
  2204. case C_POS:
  2205. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2206. i += 3;
  2207. break;
  2208. case C_NAME:
  2209. j = (*(int*)(script_buf+i)&0xffffff);
  2210. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2211. i += 3;
  2212. break;
  2213. case C_STR:
  2214. j = strlen(script_buf + i);
  2215. ShowMessage(" %s", script_buf + i);
  2216. i += j+1;
  2217. break;
  2218. }
  2219. ShowMessage(CL_CLL"\n");
  2220. }
  2221. }
  2222. #endif
  2223. CREATE(code,struct script_code,1);
  2224. code->script_buf = script_buf;
  2225. code->script_size = script_size;
  2226. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2227. return code;
  2228. }
  2229. /// Returns the player attached to this script, identified by the rid.
  2230. /// If there is no player attached, the script is terminated.
  2231. TBL_PC *script_rid2sd(struct script_state *st)
  2232. {
  2233. TBL_PC *sd=map_id2sd(st->rid);
  2234. if(!sd){
  2235. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2236. script_reportfunc(st);
  2237. script_reportsrc(st);
  2238. st->state = END;
  2239. }
  2240. return sd;
  2241. }
  2242. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2243. ///
  2244. /// @param st Script state
  2245. /// @param data Variable/constant
  2246. void get_val(struct script_state* st, struct script_data* data)
  2247. {
  2248. const char* name;
  2249. char prefix;
  2250. char postfix;
  2251. TBL_PC* sd = NULL;
  2252. if( !data_isreference(data) )
  2253. return;// not a variable/constant
  2254. name = reference_getname(data);
  2255. prefix = name[0];
  2256. postfix = name[strlen(name) - 1];
  2257. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2258. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2259. {
  2260. sd = script_rid2sd(st);
  2261. if( sd == NULL )
  2262. {// needs player attached
  2263. if( postfix == '$' )
  2264. {// string variable
  2265. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2266. data->type = C_CONSTSTR;
  2267. data->u.str = "";
  2268. }
  2269. else
  2270. {// integer variable
  2271. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2272. data->type = C_INT;
  2273. data->u.num = 0;
  2274. }
  2275. return;
  2276. }
  2277. }
  2278. if( postfix == '$' )
  2279. {// string variable
  2280. switch( prefix )
  2281. {
  2282. case '@':
  2283. data->u.str = pc_readregstr(sd, data->u.num);
  2284. break;
  2285. case '$':
  2286. data->u.str = mapreg_readregstr(data->u.num);
  2287. break;
  2288. case '#':
  2289. if( name[1] == '#' )
  2290. data->u.str = pc_readaccountreg2str(sd, name);// global
  2291. else
  2292. data->u.str = pc_readaccountregstr(sd, name);// local
  2293. break;
  2294. case '.':
  2295. {
  2296. struct DBMap* n =
  2297. data->ref ? *data->ref:
  2298. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2299. st->script->script_vars;// npc variable
  2300. if( n )
  2301. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2302. else
  2303. data->u.str = NULL;
  2304. }
  2305. break;
  2306. case '\'':
  2307. if (st->instance_id) {
  2308. data->u.str = (char*)idb_get(instance[st->instance_id].vars,reference_getuid(data));
  2309. } else {
  2310. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2311. data->u.str = NULL;
  2312. }
  2313. break;
  2314. default:
  2315. data->u.str = pc_readglobalreg_str(sd, name);
  2316. break;
  2317. }
  2318. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2319. {// empty string
  2320. data->type = C_CONSTSTR;
  2321. data->u.str = "";
  2322. }
  2323. else
  2324. {// duplicate string
  2325. data->type = C_STR;
  2326. data->u.str = aStrdup(data->u.str);
  2327. }
  2328. }
  2329. else
  2330. {// integer variable
  2331. data->type = C_INT;
  2332. if( reference_toconstant(data) )
  2333. {
  2334. data->u.num = reference_getconstant(data);
  2335. }
  2336. else if( reference_toparam(data) )
  2337. {
  2338. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2339. }
  2340. else
  2341. switch( prefix )
  2342. {
  2343. case '@':
  2344. data->u.num = pc_readreg(sd, data->u.num);
  2345. break;
  2346. case '$':
  2347. data->u.num = mapreg_readreg(data->u.num);
  2348. break;
  2349. case '#':
  2350. if( name[1] == '#' )
  2351. data->u.num = pc_readaccountreg2(sd, name);// global
  2352. else
  2353. data->u.num = pc_readaccountreg(sd, name);// local
  2354. break;
  2355. case '.':
  2356. {
  2357. struct DBMap* n =
  2358. data->ref ? *data->ref:
  2359. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2360. st->script->script_vars;// npc variable
  2361. if( n )
  2362. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2363. else
  2364. data->u.num = 0;
  2365. }
  2366. break;
  2367. case '\'':
  2368. if( st->instance_id )
  2369. data->u.num = (int)idb_iget(instance[st->instance_id].vars,reference_getuid(data));
  2370. else {
  2371. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2372. data->u.num = 0;
  2373. }
  2374. break;
  2375. default:
  2376. data->u.num = pc_readglobalreg(sd, name);
  2377. break;
  2378. }
  2379. }
  2380. return;
  2381. }
  2382. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2383. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2384. /// The value is left in the top of the stack and needs to be removed manually.
  2385. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2386. {
  2387. struct script_data* data;
  2388. push_val2(st->stack, C_NAME, uid, ref);
  2389. data = script_getdatatop(st, -1);
  2390. get_val(st, data);
  2391. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2392. }
  2393. /*==========================================
  2394. * Stores the value of a script variable
  2395. * Return value is 0 on fail, 1 on success.
  2396. *------------------------------------------*/
  2397. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2398. {
  2399. char prefix = name[0];
  2400. if( is_string_variable(name) )
  2401. {// string variable
  2402. const char* str = (const char*)value;
  2403. switch (prefix) {
  2404. case '@':
  2405. return pc_setregstr(sd, num, str);
  2406. case '$':
  2407. return mapreg_setregstr(num, str);
  2408. case '#':
  2409. return (name[1] == '#') ?
  2410. pc_setaccountreg2str(sd, name, str) :
  2411. pc_setaccountregstr(sd, name, str);
  2412. case '.':
  2413. {
  2414. struct DBMap* n;
  2415. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2416. if( n ) {
  2417. idb_remove(n, num);
  2418. if (str[0]) idb_put(n, num, aStrdup(str));
  2419. }
  2420. }
  2421. return 1;
  2422. case '\'':
  2423. if( st->instance_id ) {
  2424. idb_remove(instance[st->instance_id].vars, num);
  2425. if( str[0] ) idb_put(instance[st->instance_id].vars, num, aStrdup(str));
  2426. }
  2427. return 1;
  2428. default:
  2429. return pc_setglobalreg_str(sd, name, str);
  2430. }
  2431. }
  2432. else
  2433. {// integer variable
  2434. int val = (int)__64BPRTSIZE(value);
  2435. if(str_data[num&0x00ffffff].type == C_PARAM)
  2436. {
  2437. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2438. {
  2439. if( st != NULL )
  2440. {
  2441. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2442. script_reportsrc(st);
  2443. st->state = END;
  2444. }
  2445. return 0;
  2446. }
  2447. return 1;
  2448. }
  2449. switch (prefix) {
  2450. case '@':
  2451. return pc_setreg(sd, num, val);
  2452. case '$':
  2453. return mapreg_setreg(num, val);
  2454. case '#':
  2455. return (name[1] == '#') ?
  2456. pc_setaccountreg2(sd, name, val) :
  2457. pc_setaccountreg(sd, name, val);
  2458. case '.':
  2459. {
  2460. struct DBMap* n;
  2461. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2462. if( n ) {
  2463. idb_remove(n, num);
  2464. if( val != 0 )
  2465. idb_iput(n, num, val);
  2466. }
  2467. }
  2468. return 1;
  2469. case '\'':
  2470. if( st->instance_id ) {
  2471. idb_remove(instance[st->instance_id].vars, num);
  2472. if( val != 0 )
  2473. idb_iput(instance[st->instance_id].vars, num, val);
  2474. }
  2475. return 1;
  2476. default:
  2477. return pc_setglobalreg(sd, name, val);
  2478. }
  2479. }
  2480. }
  2481. int set_var(TBL_PC* sd, char* name, void* val)
  2482. {
  2483. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2484. }
  2485. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2486. {
  2487. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2488. }
  2489. /// Converts the data to a string
  2490. const char* conv_str(struct script_state* st, struct script_data* data)
  2491. {
  2492. char* p;
  2493. get_val(st, data);
  2494. if( data_isstring(data) )
  2495. {// nothing to convert
  2496. }
  2497. else if( data_isint(data) )
  2498. {// int -> string
  2499. CREATE(p, char, ITEM_NAME_LENGTH);
  2500. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2501. p[ITEM_NAME_LENGTH-1] = '\0';
  2502. data->type = C_STR;
  2503. data->u.str = p;
  2504. }
  2505. else if( data_isreference(data) )
  2506. {// reference -> string
  2507. //##TODO when does this happen (check get_val) [FlavioJS]
  2508. data->type = C_CONSTSTR;
  2509. data->u.str = reference_getname(data);
  2510. }
  2511. else
  2512. {// unsupported data type
  2513. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2514. script_reportdata(data);
  2515. script_reportsrc(st);
  2516. data->type = C_CONSTSTR;
  2517. data->u.str = "";
  2518. }
  2519. return data->u.str;
  2520. }
  2521. /// Converts the data to an int
  2522. int conv_num(struct script_state* st, struct script_data* data)
  2523. {
  2524. char* p;
  2525. long num;
  2526. get_val(st, data);
  2527. if( data_isint(data) )
  2528. {// nothing to convert
  2529. }
  2530. else if( data_isstring(data) )
  2531. {// string -> int
  2532. // the result does not overflow or underflow, it is capped instead
  2533. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2534. p = data->u.str;
  2535. errno = 0;
  2536. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2537. if( errno == ERANGE
  2538. #if LONG_MAX > INT_MAX
  2539. || num < INT_MIN || num > INT_MAX
  2540. #endif
  2541. )
  2542. {
  2543. if( num <= INT_MIN )
  2544. {
  2545. num = INT_MIN;
  2546. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2547. }
  2548. else//if( num >= INT_MAX )
  2549. {
  2550. num = INT_MAX;
  2551. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2552. }
  2553. script_reportdata(data);
  2554. script_reportsrc(st);
  2555. }
  2556. if( data->type == C_STR )
  2557. aFree(p);
  2558. data->type = C_INT;
  2559. data->u.num = (int)num;
  2560. }
  2561. #if 0
  2562. // FIXME this function is being used to retrieve the position of labels and
  2563. // probably other stuff [FlavioJS]
  2564. else
  2565. {// unsupported data type
  2566. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2567. script_reportdata(data);
  2568. script_reportsrc(st);
  2569. data->type = C_INT;
  2570. data->u.num = 0;
  2571. }
  2572. #endif
  2573. return data->u.num;
  2574. }
  2575. //
  2576. // Stack operations
  2577. //
  2578. /// Increases the size of the stack
  2579. void stack_expand(struct script_stack* stack)
  2580. {
  2581. stack->sp_max += 64;
  2582. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2583. stack->sp_max * sizeof(stack->stack_data[0]) );
  2584. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2585. 64 * sizeof(stack->stack_data[0]) );
  2586. }
  2587. /// Pushes a value into the stack
  2588. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2589. /// Pushes a value into the stack (with reference)
  2590. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2591. {
  2592. if( stack->sp >= stack->sp_max )
  2593. stack_expand(stack);
  2594. stack->stack_data[stack->sp].type = type;
  2595. stack->stack_data[stack->sp].u.num = val;
  2596. stack->stack_data[stack->sp].ref = ref;
  2597. stack->sp++;
  2598. return &stack->stack_data[stack->sp-1];
  2599. }
  2600. /// Pushes a string into the stack
  2601. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2602. {
  2603. if( stack->sp >= stack->sp_max )
  2604. stack_expand(stack);
  2605. stack->stack_data[stack->sp].type = type;
  2606. stack->stack_data[stack->sp].u.str = str;
  2607. stack->stack_data[stack->sp].ref = NULL;
  2608. stack->sp++;
  2609. return &stack->stack_data[stack->sp-1];
  2610. }
  2611. /// Pushes a retinfo into the stack
  2612. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2613. {
  2614. if( stack->sp >= stack->sp_max )
  2615. stack_expand(stack);
  2616. stack->stack_data[stack->sp].type = C_RETINFO;
  2617. stack->stack_data[stack->sp].u.ri = ri;
  2618. stack->stack_data[stack->sp].ref = ref;
  2619. stack->sp++;
  2620. return &stack->stack_data[stack->sp-1];
  2621. }
  2622. /// Pushes a copy of the target position into the stack
  2623. struct script_data* push_copy(struct script_stack* stack, int pos)
  2624. {
  2625. switch( stack->stack_data[pos].type )
  2626. {
  2627. case C_CONSTSTR:
  2628. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2629. break;
  2630. case C_STR:
  2631. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2632. break;
  2633. case C_RETINFO:
  2634. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2635. exit(1);
  2636. break;
  2637. default:
  2638. return push_val2(
  2639. stack,stack->stack_data[pos].type,
  2640. stack->stack_data[pos].u.num,
  2641. stack->stack_data[pos].ref
  2642. );
  2643. break;
  2644. }
  2645. }
  2646. /// Removes the values in indexes [start,end[ from the stack.
  2647. /// Adjusts all stack pointers.
  2648. void pop_stack(struct script_state* st, int start, int end)
  2649. {
  2650. struct script_stack* stack = st->stack;
  2651. struct script_data* data;
  2652. int i;
  2653. if( start < 0 )
  2654. start = 0;
  2655. if( end > stack->sp )
  2656. end = stack->sp;
  2657. if( start >= end )
  2658. return;// nothing to pop
  2659. // free stack elements
  2660. for( i = start; i < end; i++ )
  2661. {
  2662. data = &stack->stack_data[i];
  2663. if( data->type == C_STR )
  2664. aFree(data->u.str);
  2665. if( data->type == C_RETINFO )
  2666. {
  2667. struct script_retinfo* ri = data->u.ri;
  2668. if( ri->var_function )
  2669. script_free_vars(ri->var_function);
  2670. if( data->ref )
  2671. aFree(data->ref);
  2672. aFree(ri);
  2673. }
  2674. data->type = C_NOP;
  2675. }
  2676. // move the rest of the elements
  2677. if( stack->sp > end )
  2678. {
  2679. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2680. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2681. stack->stack_data[i].type = C_NOP;
  2682. }
  2683. // adjust stack pointers
  2684. if( st->start > end ) st->start -= end - start;
  2685. else if( st->start > start ) st->start = start;
  2686. if( st->end > end ) st->end -= end - start;
  2687. else if( st->end > start ) st->end = start;
  2688. if( stack->defsp > end ) stack->defsp -= end - start;
  2689. else if( stack->defsp > start ) stack->defsp = start;
  2690. stack->sp -= end - start;
  2691. }
  2692. ///
  2693. ///
  2694. ///
  2695. /*==========================================
  2696. * Release script dependent variable, dependent variable of function
  2697. *------------------------------------------*/
  2698. void script_free_vars(struct DBMap* storage)
  2699. {
  2700. if( storage )
  2701. {// destroy the storage construct containing the variables
  2702. db_destroy(storage);
  2703. }
  2704. }
  2705. void script_free_code(struct script_code* code)
  2706. {
  2707. script_free_vars( code->script_vars );
  2708. aFree( code->script_buf );
  2709. aFree( code );
  2710. }
  2711. /// Creates a new script state.
  2712. ///
  2713. /// @param script Script code
  2714. /// @param pos Position in the code
  2715. /// @param rid Who is running the script (attached player)
  2716. /// @param oid Where the code is being run (npc 'object')
  2717. /// @return Script state
  2718. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2719. {
  2720. struct script_state* st;
  2721. CREATE(st, struct script_state, 1);
  2722. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2723. st->stack->sp = 0;
  2724. st->stack->sp_max = 64;
  2725. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2726. st->stack->defsp = st->stack->sp;
  2727. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2728. st->state = RUN;
  2729. st->script = script;
  2730. //st->scriptroot = script;
  2731. st->pos = pos;
  2732. st->rid = rid;
  2733. st->oid = oid;
  2734. st->sleep.timer = INVALID_TIMER;
  2735. st->npc_item_flag = battle_config.item_enabled_npc;
  2736. return st;
  2737. }
  2738. /// Frees a script state.
  2739. ///
  2740. /// @param st Script state
  2741. void script_free_state(struct script_state* st)
  2742. {
  2743. if(st->bk_st)
  2744. {// backup was not restored
  2745. 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);
  2746. }
  2747. if( st->sleep.timer != INVALID_TIMER )
  2748. delete_timer(st->sleep.timer, run_script_timer);
  2749. script_free_vars(st->stack->var_function);
  2750. pop_stack(st, 0, st->stack->sp);
  2751. aFree(st->stack->stack_data);
  2752. aFree(st->stack);
  2753. st->stack = NULL;
  2754. st->pos = -1;
  2755. aFree(st);
  2756. }
  2757. //
  2758. // Main execution unit
  2759. //
  2760. /*==========================================
  2761. * Read command
  2762. *------------------------------------------*/
  2763. c_op get_com(unsigned char *script,int *pos)
  2764. {
  2765. int i = 0, j = 0;
  2766. if(script[*pos]>=0x80){
  2767. return C_INT;
  2768. }
  2769. while(script[*pos]>=0x40){
  2770. i=script[(*pos)++]<<j;
  2771. j+=6;
  2772. }
  2773. return (c_op)(i+(script[(*pos)++]<<j));
  2774. }
  2775. /*==========================================
  2776. * Income figures
  2777. *------------------------------------------*/
  2778. int get_num(unsigned char *script,int *pos)
  2779. {
  2780. int i,j;
  2781. i=0; j=0;
  2782. while(script[*pos]>=0xc0){
  2783. i+=(script[(*pos)++]&0x7f)<<j;
  2784. j+=6;
  2785. }
  2786. return i+((script[(*pos)++]&0x7f)<<j);
  2787. }
  2788. /*==========================================
  2789. * Remove the value from the stack
  2790. *------------------------------------------*/
  2791. int pop_val(struct script_state* st)
  2792. {
  2793. if(st->stack->sp<=0)
  2794. return 0;
  2795. st->stack->sp--;
  2796. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2797. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2798. return st->stack->stack_data[st->stack->sp].u.num;
  2799. return 0;
  2800. }
  2801. /// Ternary operators
  2802. /// test ? if_true : if_false
  2803. void op_3(struct script_state* st, int op)
  2804. {
  2805. struct script_data* data;
  2806. int flag = 0;
  2807. data = script_getdatatop(st, -3);
  2808. get_val(st, data);
  2809. if( data_isstring(data) )
  2810. flag = data->u.str[0];// "" -> false
  2811. else if( data_isint(data) )
  2812. flag = data->u.num;// 0 -> false
  2813. else
  2814. {
  2815. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2816. script_reportdata(data);
  2817. script_reportsrc(st);
  2818. script_removetop(st, -3, 0);
  2819. script_pushnil(st);
  2820. return;
  2821. }
  2822. if( flag )
  2823. script_pushcopytop(st, -2);
  2824. else
  2825. script_pushcopytop(st, -1);
  2826. script_removetop(st, -4, -1);
  2827. }
  2828. /// Binary string operators
  2829. /// s1 EQ s2 -> i
  2830. /// s1 NE s2 -> i
  2831. /// s1 GT s2 -> i
  2832. /// s1 GE s2 -> i
  2833. /// s1 LT s2 -> i
  2834. /// s1 LE s2 -> i
  2835. /// s1 ADD s2 -> s
  2836. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2837. {
  2838. int a = 0;
  2839. switch(op){
  2840. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2841. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2842. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2843. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2844. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2845. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2846. case C_ADD:
  2847. {
  2848. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2849. strcpy(buf, s1);
  2850. strcat(buf, s2);
  2851. script_pushstr(st, buf);
  2852. return;
  2853. }
  2854. default:
  2855. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2856. script_reportsrc(st);
  2857. script_pushnil(st);
  2858. st->state = END;
  2859. return;
  2860. }
  2861. script_pushint(st,a);
  2862. }
  2863. /// Binary number operators
  2864. /// i OP i -> i
  2865. void op_2num(struct script_state* st, int op, int i1, int i2)
  2866. {
  2867. int ret;
  2868. double ret_double;
  2869. switch( op )
  2870. {
  2871. case C_AND: ret = i1 & i2; break;
  2872. case C_OR: ret = i1 | i2; break;
  2873. case C_XOR: ret = i1 ^ i2; break;
  2874. case C_LAND: ret = (i1 && i2); break;
  2875. case C_LOR: ret = (i1 || i2); break;
  2876. case C_EQ: ret = (i1 == i2); break;
  2877. case C_NE: ret = (i1 != i2); break;
  2878. case C_GT: ret = (i1 > i2); break;
  2879. case C_GE: ret = (i1 >= i2); break;
  2880. case C_LT: ret = (i1 < i2); break;
  2881. case C_LE: ret = (i1 <= i2); break;
  2882. case C_R_SHIFT: ret = i1>>i2; break;
  2883. case C_L_SHIFT: ret = i1<<i2; break;
  2884. case C_DIV:
  2885. case C_MOD:
  2886. if( i2 == 0 )
  2887. {
  2888. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2889. script_reportsrc(st);
  2890. script_pushnil(st);
  2891. st->state = END;
  2892. return;
  2893. }
  2894. else if( op == C_DIV )
  2895. ret = i1 / i2;
  2896. else//if( op == C_MOD )
  2897. ret = i1 % i2;
  2898. break;
  2899. default:
  2900. switch( op )
  2901. {// operators that can overflow/underflow
  2902. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2903. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2904. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2905. default:
  2906. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2907. script_reportsrc(st);
  2908. script_pushnil(st);
  2909. return;
  2910. }
  2911. if( ret_double < (double)INT_MIN )
  2912. {
  2913. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2914. script_reportsrc(st);
  2915. ret = INT_MIN;
  2916. }
  2917. else if( ret_double > (double)INT_MAX )
  2918. {
  2919. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2920. script_reportsrc(st);
  2921. ret = INT_MAX;
  2922. }
  2923. }
  2924. script_pushint(st, ret);
  2925. }
  2926. /// Binary operators
  2927. void op_2(struct script_state *st, int op)
  2928. {
  2929. struct script_data* left, leftref;
  2930. struct script_data* right;
  2931. leftref.type = C_NOP;
  2932. left = script_getdatatop(st, -2);
  2933. right = script_getdatatop(st, -1);
  2934. if (st->op2ref) {
  2935. if (data_isreference(left)) {
  2936. leftref = *left;
  2937. }
  2938. st->op2ref = 0;
  2939. }
  2940. get_val(st, left);
  2941. get_val(st, right);
  2942. // automatic conversions
  2943. switch( op )
  2944. {
  2945. case C_ADD:
  2946. if( data_isint(left) && data_isstring(right) )
  2947. {// convert int-string to string-string
  2948. conv_str(st, left);
  2949. }
  2950. else if( data_isstring(left) && data_isint(right) )
  2951. {// convert string-int to string-string
  2952. conv_str(st, right);
  2953. }
  2954. break;
  2955. }
  2956. if( data_isstring(left) && data_isstring(right) )
  2957. {// ss => op_2str
  2958. op_2str(st, op, left->u.str, right->u.str);
  2959. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  2960. if (leftref.type != C_NOP)
  2961. {
  2962. aFree(left->u.str);
  2963. *left = leftref;
  2964. }
  2965. }
  2966. else if( data_isint(left) && data_isint(right) )
  2967. {// ii => op_2num
  2968. int i1 = left->u.num;
  2969. int i2 = right->u.num;
  2970. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  2971. op_2num(st, op, i1, i2);
  2972. if (leftref.type != C_NOP)
  2973. *left = leftref;
  2974. }
  2975. else
  2976. {// invalid argument
  2977. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2978. script_reportdata(left);
  2979. script_reportdata(right);
  2980. script_reportsrc(st);
  2981. script_removetop(st, -2, 0);
  2982. script_pushnil(st);
  2983. st->state = END;
  2984. }
  2985. }
  2986. /// Unary operators
  2987. /// NEG i -> i
  2988. /// NOT i -> i
  2989. /// LNOT i -> i
  2990. void op_1(struct script_state* st, int op)
  2991. {
  2992. struct script_data* data;
  2993. int i1;
  2994. data = script_getdatatop(st, -1);
  2995. get_val(st, data);
  2996. if( !data_isint(data) )
  2997. {// not a number
  2998. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2999. script_reportdata(data);
  3000. script_reportsrc(st);
  3001. script_pushnil(st);
  3002. st->state = END;
  3003. return;
  3004. }
  3005. i1 = data->u.num;
  3006. script_removetop(st, -1, 0);
  3007. switch( op )
  3008. {
  3009. case C_NEG: i1 = -i1; break;
  3010. case C_NOT: i1 = ~i1; break;
  3011. case C_LNOT: i1 = !i1; break;
  3012. default:
  3013. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3014. script_reportsrc(st);
  3015. script_pushnil(st);
  3016. st->state = END;
  3017. return;
  3018. }
  3019. script_pushint(st, i1);
  3020. }
  3021. /// Checks the type of all arguments passed to a built-in function.
  3022. ///
  3023. /// @param st Script state whose stack arguments should be inspected.
  3024. /// @param func Built-in function for which the arguments are intended.
  3025. static void script_check_buildin_argtype(struct script_state* st, int func)
  3026. {
  3027. char type;
  3028. int idx, invalid = 0;
  3029. script_function* sf = &buildin_func[str_data[func].val];
  3030. for( idx = 2; script_hasdata(st, idx); idx++ )
  3031. {
  3032. struct script_data* data = script_getdata(st, idx);
  3033. type = sf->arg[idx-2];
  3034. if( type == '?' || type == '*' )
  3035. {// optional argument or unknown number of optional parameters ( no types are after this )
  3036. break;
  3037. }
  3038. else if( type == 0 )
  3039. {// more arguments than necessary ( should not happen, as it is checked before )
  3040. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3041. invalid++;
  3042. break;
  3043. }
  3044. else
  3045. {
  3046. const char* name = NULL;
  3047. if( data_isreference(data) )
  3048. {// get name for variables to determine the type they refer to
  3049. name = reference_getname(data);
  3050. }
  3051. switch( type )
  3052. {
  3053. case 'v':
  3054. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3055. {// variant
  3056. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3057. script_reportdata(data);
  3058. invalid++;
  3059. }
  3060. break;
  3061. case 's':
  3062. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3063. {// string
  3064. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3065. script_reportdata(data);
  3066. invalid++;
  3067. }
  3068. break;
  3069. case 'i':
  3070. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3071. {// int ( params and constants are always int )
  3072. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3073. script_reportdata(data);
  3074. invalid++;
  3075. }
  3076. break;
  3077. case 'r':
  3078. if( !data_isreference(data) )
  3079. {// variables
  3080. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3081. script_reportdata(data);
  3082. invalid++;
  3083. }
  3084. break;
  3085. case 'l':
  3086. if( !data_islabel(data) && !data_isfunclabel(data) )
  3087. {// label
  3088. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3089. script_reportdata(data);
  3090. invalid++;
  3091. }
  3092. break;
  3093. }
  3094. }
  3095. }
  3096. if(invalid)
  3097. {
  3098. ShowDebug("Function: %s\n", get_str(func));
  3099. script_reportsrc(st);
  3100. }
  3101. }
  3102. /// Executes a buildin command.
  3103. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3104. int run_func(struct script_state *st)
  3105. {
  3106. struct script_data* data;
  3107. int i,start_sp,end_sp,func;
  3108. end_sp = st->stack->sp;// position after the last argument
  3109. for( i = end_sp-1; i > 0 ; --i )
  3110. if( st->stack->stack_data[i].type == C_ARG )
  3111. break;
  3112. if( i == 0 )
  3113. {
  3114. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3115. st->state = END;
  3116. script_reportsrc(st);
  3117. return 1;
  3118. }
  3119. start_sp = i-1;// C_NAME of the command
  3120. st->start = start_sp;
  3121. st->end = end_sp;
  3122. data = &st->stack->stack_data[st->start];
  3123. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  3124. func = data->u.num;
  3125. else
  3126. {
  3127. ShowError("script:run_func: not a buildin command.\n");
  3128. script_reportdata(data);
  3129. script_reportsrc(st);
  3130. st->state = END;
  3131. return 1;
  3132. }
  3133. if( script_config.warn_func_mismatch_argtypes )
  3134. {
  3135. script_check_buildin_argtype(st, func);
  3136. }
  3137. if(str_data[func].func){
  3138. if (str_data[func].func(st)) //Report error
  3139. script_reportsrc(st);
  3140. } else {
  3141. 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));
  3142. script_reportsrc(st);
  3143. st->state = END;
  3144. }
  3145. // Stack's datum are used when re-running functions [Eoe]
  3146. if( st->state == RERUNLINE )
  3147. return 0;
  3148. pop_stack(st, st->start, st->end);
  3149. if( st->state == RETFUNC )
  3150. {// return from a user-defined function
  3151. struct script_retinfo* ri;
  3152. int olddefsp = st->stack->defsp;
  3153. int nargs;
  3154. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3155. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3156. {
  3157. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3158. script_reportsrc(st);
  3159. st->state = END;
  3160. return 1;
  3161. }
  3162. script_free_vars( st->stack->var_function );
  3163. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3164. nargs = ri->nargs;
  3165. st->pos = ri->pos;
  3166. st->script = ri->script;
  3167. st->stack->var_function = ri->var_function;
  3168. st->stack->defsp = ri->defsp;
  3169. memset(ri, 0, sizeof(struct script_retinfo));
  3170. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3171. st->state = GOTO;
  3172. }
  3173. return 0;
  3174. }
  3175. /*==========================================
  3176. * script execution
  3177. *------------------------------------------*/
  3178. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3179. {
  3180. struct script_state *st;
  3181. if( rootscript == NULL || pos < 0 )
  3182. return;
  3183. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3184. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3185. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3186. st = script_alloc_state(rootscript, pos, rid, oid);
  3187. run_script_main(st);
  3188. }
  3189. void script_stop_sleeptimers(int id)
  3190. {
  3191. struct script_state* st;
  3192. for(;;)
  3193. {
  3194. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3195. if( st == NULL )
  3196. break; // no more sleep timers
  3197. script_free_state(st);
  3198. }
  3199. }
  3200. /*==========================================
  3201. * Delete the specified node from sleep_db
  3202. *------------------------------------------*/
  3203. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3204. {
  3205. struct linkdb_node *retnode;
  3206. if( n == NULL)
  3207. return NULL;
  3208. if( n->prev == NULL )
  3209. sleep_db = n->next;
  3210. else
  3211. n->prev->next = n->next;
  3212. if( n->next )
  3213. n->next->prev = n->prev;
  3214. retnode = n->next;
  3215. aFree( n );
  3216. return retnode; // The following; return retnode
  3217. }
  3218. /*==========================================
  3219. * Timer function for sleep
  3220. *------------------------------------------*/
  3221. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3222. {
  3223. struct script_state *st = (struct script_state *)data;
  3224. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3225. TBL_PC *sd = map_id2sd(st->rid);
  3226. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3227. { //Character mismatch. Cancel execution.
  3228. st->rid = 0;
  3229. st->state = END;
  3230. }
  3231. while( node && st->sleep.timer != INVALID_TIMER ) {
  3232. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3233. script_erase_sleepdb(node);
  3234. st->sleep.timer = INVALID_TIMER;
  3235. break;
  3236. }
  3237. node = node->next;
  3238. }
  3239. if(st->state != RERUNLINE)
  3240. st->sleep.tick = 0;
  3241. run_script_main(st);
  3242. return 0;
  3243. }
  3244. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3245. ///
  3246. /// @param st Script state to detach.
  3247. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3248. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3249. {
  3250. struct map_session_data* sd;
  3251. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3252. sd->st = st->bk_st;
  3253. sd->npc_id = st->bk_npcid;
  3254. if(st->bk_st) {
  3255. //Remove tag for removal.
  3256. st->bk_st = NULL;
  3257. st->bk_npcid = 0;
  3258. } else if(dequeue_event) {
  3259. /**
  3260. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3261. **/
  3262. #if SECURE_NPCTIMEOUT
  3263. /**
  3264. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3265. **/
  3266. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3267. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3268. sd->npc_idle_timer = INVALID_TIMER;
  3269. }
  3270. #endif
  3271. npc_event_dequeue(sd);
  3272. }
  3273. }
  3274. else if(st->bk_st)
  3275. {// rid was set to 0, before detaching the script state
  3276. 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);
  3277. script_reportsrc(st->bk_st);
  3278. script_free_state(st->bk_st);
  3279. st->bk_st = NULL;
  3280. }
  3281. }
  3282. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3283. ///
  3284. /// @param st Script state to attach.
  3285. static void script_attach_state(struct script_state* st)
  3286. {
  3287. struct map_session_data* sd;
  3288. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3289. {
  3290. if(st!=sd->st)
  3291. {
  3292. if(st->bk_st)
  3293. {// there is already a backup
  3294. 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);
  3295. }
  3296. st->bk_st = sd->st;
  3297. st->bk_npcid = sd->npc_id;
  3298. }
  3299. sd->st = st;
  3300. sd->npc_id = st->oid;
  3301. sd->npc_item_flag = st->npc_item_flag; // load default.
  3302. /**
  3303. * For the Secure NPC Timeout option (check config/Secure.h) [RR]
  3304. **/
  3305. #if SECURE_NPCTIMEOUT
  3306. if( sd->npc_idle_timer == INVALID_TIMER )
  3307. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3308. sd->npc_idle_tick = gettick();
  3309. #endif
  3310. }
  3311. }
  3312. /*==========================================
  3313. * The main part of the script execution
  3314. *------------------------------------------*/
  3315. void run_script_main(struct script_state *st)
  3316. {
  3317. int cmdcount = script_config.check_cmdcount;
  3318. int gotocount = script_config.check_gotocount;
  3319. TBL_PC *sd;
  3320. struct script_stack *stack=st->stack;
  3321. struct npc_data *nd;
  3322. script_attach_state(st);
  3323. nd = map_id2nd(st->oid);
  3324. if( nd && map[nd->bl.m].instance_id > 0 )
  3325. st->instance_id = map[nd->bl.m].instance_id;
  3326. if(st->state == RERUNLINE) {
  3327. run_func(st);
  3328. if(st->state == GOTO)
  3329. st->state = RUN;
  3330. } else if(st->state != END)
  3331. st->state = RUN;
  3332. while(st->state == RUN)
  3333. {
  3334. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3335. switch(c){
  3336. case C_EOL:
  3337. if( stack->defsp > stack->sp )
  3338. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3339. else
  3340. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3341. break;
  3342. case C_INT:
  3343. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3344. break;
  3345. case C_POS:
  3346. case C_NAME:
  3347. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3348. st->pos+=3;
  3349. break;
  3350. case C_ARG:
  3351. push_val(stack,c,0);
  3352. break;
  3353. case C_STR:
  3354. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3355. while(st->script->script_buf[st->pos++]);
  3356. break;
  3357. case C_FUNC:
  3358. run_func(st);
  3359. if(st->state==GOTO){
  3360. st->state = RUN;
  3361. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3362. ShowError("run_script: infinity loop !\n");
  3363. script_reportsrc(st);
  3364. st->state=END;
  3365. }
  3366. }
  3367. break;
  3368. case C_REF:
  3369. st->op2ref = 1;
  3370. break;
  3371. case C_NEG:
  3372. case C_NOT:
  3373. case C_LNOT:
  3374. op_1(st ,c);
  3375. break;
  3376. case C_ADD:
  3377. case C_SUB:
  3378. case C_MUL:
  3379. case C_DIV:
  3380. case C_MOD:
  3381. case C_EQ:
  3382. case C_NE:
  3383. case C_GT:
  3384. case C_GE:
  3385. case C_LT:
  3386. case C_LE:
  3387. case C_AND:
  3388. case C_OR:
  3389. case C_XOR:
  3390. case C_LAND:
  3391. case C_LOR:
  3392. case C_R_SHIFT:
  3393. case C_L_SHIFT:
  3394. op_2(st, c);
  3395. break;
  3396. case C_OP3:
  3397. op_3(st, c);
  3398. break;
  3399. case C_NOP:
  3400. st->state=END;
  3401. break;
  3402. default:
  3403. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3404. st->state=END;
  3405. break;
  3406. }
  3407. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3408. ShowError("run_script: infinity loop !\n");
  3409. script_reportsrc(st);
  3410. st->state=END;
  3411. }
  3412. }
  3413. if(st->sleep.tick > 0) {
  3414. //Restore previous script
  3415. script_detach_state(st, false);
  3416. //Delay execution
  3417. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3418. st->sleep.charid = sd?sd->status.char_id:0;
  3419. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3420. run_script_timer, st->sleep.charid, (intptr_t)st);
  3421. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3422. }
  3423. else if(st->state != END && st->rid){
  3424. //Resume later (st is already attached to player).
  3425. if(st->bk_st) {
  3426. ShowWarning("Unable to restore stack! Double continuation!\n");
  3427. //Report BOTH scripts to see if that can help somehow.
  3428. ShowDebug("Previous script (lost):\n");
  3429. script_reportsrc(st->bk_st);
  3430. ShowDebug("Current script:\n");
  3431. script_reportsrc(st);
  3432. script_free_state(st->bk_st);
  3433. st->bk_st = NULL;
  3434. }
  3435. } else {
  3436. //Dispose of script.
  3437. if ((sd = map_id2sd(st->rid))!=NULL)
  3438. { //Restore previous stack and save char.
  3439. if(sd->state.using_fake_npc){
  3440. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3441. sd->state.using_fake_npc = 0;
  3442. }
  3443. //Restore previous script if any.
  3444. script_detach_state(st, true);
  3445. if (sd->state.reg_dirty&2)
  3446. intif_saveregistry(sd,2);
  3447. if (sd->state.reg_dirty&1)
  3448. intif_saveregistry(sd,1);
  3449. }
  3450. script_free_state(st);
  3451. st = NULL;
  3452. }
  3453. }
  3454. int script_config_read(char *cfgName)
  3455. {
  3456. int i;
  3457. char line[1024],w1[1024],w2[1024];
  3458. FILE *fp;
  3459. fp=fopen(cfgName,"r");
  3460. if(fp==NULL){
  3461. ShowError("File not found: %s\n", cfgName);
  3462. return 1;
  3463. }
  3464. while(fgets(line, sizeof(line), fp))
  3465. {
  3466. if(line[0] == '/' && line[1] == '/')
  3467. continue;
  3468. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3469. if(i!=2)
  3470. continue;
  3471. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3472. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3473. }
  3474. else if(strcmpi(w1,"check_cmdcount")==0) {
  3475. script_config.check_cmdcount = config_switch(w2);
  3476. }
  3477. else if(strcmpi(w1,"check_gotocount")==0) {
  3478. script_config.check_gotocount = config_switch(w2);
  3479. }
  3480. else if(strcmpi(w1,"input_min_value")==0) {
  3481. script_config.input_min_value = config_switch(w2);
  3482. }
  3483. else if(strcmpi(w1,"input_max_value")==0) {
  3484. script_config.input_max_value = config_switch(w2);
  3485. }
  3486. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3487. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3488. }
  3489. else if(strcmpi(w1,"import")==0){
  3490. script_config_read(w2);
  3491. }
  3492. else {
  3493. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3494. }
  3495. }
  3496. fclose(fp);
  3497. return 0;
  3498. }
  3499. /**
  3500. * @see DBApply
  3501. */
  3502. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3503. {
  3504. struct script_code *code = db_data2ptr(data);
  3505. if (code)
  3506. script_free_code(code);
  3507. return 0;
  3508. }
  3509. void script_run_autobonus(const char *autobonus, int id, int pos)
  3510. {
  3511. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3512. if( script )
  3513. {
  3514. current_equip_item_index = pos;
  3515. run_script(script,0,id,0);
  3516. }
  3517. }
  3518. void script_add_autobonus(const char *autobonus)
  3519. {
  3520. if( strdb_get(autobonus_db, autobonus) == NULL )
  3521. {
  3522. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3523. if( script )
  3524. strdb_put(autobonus_db, autobonus, script);
  3525. }
  3526. }
  3527. /// resets a temporary character array variable to given value
  3528. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3529. {
  3530. int key;
  3531. uint8 idx;
  3532. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3533. {
  3534. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3535. return;
  3536. }
  3537. key = add_str(varname);
  3538. if( is_string_variable(varname) )
  3539. {
  3540. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3541. {
  3542. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3543. }
  3544. }
  3545. else
  3546. {
  3547. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3548. {
  3549. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3550. }
  3551. }
  3552. }
  3553. /// sets a temporary character array variable element idx to given value
  3554. /// @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.
  3555. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3556. {
  3557. int key;
  3558. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3559. {
  3560. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3561. return;
  3562. }
  3563. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3564. {
  3565. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3566. return;
  3567. }
  3568. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3569. if( is_string_variable(varname) )
  3570. {
  3571. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3572. }
  3573. else
  3574. {
  3575. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3576. }
  3577. if( refcache )
  3578. {// save to avoid repeated add_str calls
  3579. refcache[0] = key;
  3580. }
  3581. }
  3582. #ifdef BETA_THREAD_TEST
  3583. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3584. /* used to receive items the queryThread has already processed */
  3585. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3586. int i, cursor = 0;
  3587. bool allOk = true;
  3588. EnterSpinLock(&queryThreadLock);
  3589. for( i = 0; i < queryThreadData.count; i++ ) {
  3590. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3591. if( !entry->ok ) {
  3592. allOk = false;
  3593. continue;
  3594. }
  3595. run_script_main(entry->st);
  3596. entry->st = NULL;/* empty entries */
  3597. aFree(entry);
  3598. queryThreadData.entry[i] = NULL;
  3599. }
  3600. if( allOk ) {
  3601. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3602. delete_timer(queryThreadData.timer, queryThread_timer);
  3603. queryThreadData.timer = INVALID_TIMER;
  3604. }
  3605. /* now lets clear the mess. */
  3606. for( i = 0; i < queryThreadData.count; i++ ) {
  3607. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3608. if( entry == NULL )
  3609. continue;/* entry on hold */
  3610. /* move */
  3611. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3612. cursor++;
  3613. }
  3614. queryThreadData.count = cursor;
  3615. LeaveSpinLock(&queryThreadLock);
  3616. return 0;
  3617. }
  3618. void queryThread_add(struct script_state *st, bool type) {
  3619. int idx = 0;
  3620. struct queryThreadEntry* entry = NULL;
  3621. EnterSpinLock(&queryThreadLock);
  3622. if( queryThreadData.count++ != 0 )
  3623. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3624. idx = queryThreadData.count-1;
  3625. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3626. entry = queryThreadData.entry[idx];
  3627. entry->st = st;
  3628. entry->ok = false;
  3629. entry->type = type;
  3630. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3631. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3632. }
  3633. LeaveSpinLock(&queryThreadLock);
  3634. /* unlock the queryThread */
  3635. racond_signal(queryThreadCond);
  3636. }
  3637. /* adds a new log to the queue */
  3638. void queryThread_log(char * entry, int length) {
  3639. int idx = logThreadData.count;
  3640. EnterSpinLock(&queryThreadLock);
  3641. if( logThreadData.count++ != 0 )
  3642. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3643. CREATE(logThreadData.entry[idx], char, length + 1 );
  3644. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3645. LeaveSpinLock(&queryThreadLock);
  3646. /* unlock the queryThread */
  3647. racond_signal(queryThreadCond);
  3648. }
  3649. /* queryThread_main */
  3650. static void *queryThread_main(void *x) {
  3651. Sql *queryThread_handle = Sql_Malloc();
  3652. int i;
  3653. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) )
  3654. exit(EXIT_FAILURE);
  3655. if( strlen(default_codepage) > 0 )
  3656. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3657. Sql_ShowDebug(queryThread_handle);
  3658. if( log_config.sql_logs ) {
  3659. logmysql_handle = Sql_Malloc();
  3660. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) )
  3661. exit(EXIT_FAILURE);
  3662. if( strlen(default_codepage) > 0 )
  3663. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3664. Sql_ShowDebug(logmysql_handle);
  3665. }
  3666. while( 1 ) {
  3667. if(queryThreadTerminate > 0)
  3668. break;
  3669. EnterSpinLock(&queryThreadLock);
  3670. /* mess with queryThreadData within the lock */
  3671. for( i = 0; i < queryThreadData.count; i++ ) {
  3672. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3673. if( entry->ok )
  3674. continue;
  3675. else if ( !entry->st || !entry->st->stack ) {
  3676. entry->ok = true;/* dispose */
  3677. continue;
  3678. }
  3679. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3680. entry->ok = true;/* we're done with this */
  3681. }
  3682. /* also check for any logs in need to be sent */
  3683. if( log_config.sql_logs ) {
  3684. for( i = 0; i < logThreadData.count; i++ ) {
  3685. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3686. Sql_ShowDebug(logmysql_handle);
  3687. aFree(logThreadData.entry[i]);
  3688. }
  3689. logThreadData.count = 0;
  3690. }
  3691. LeaveSpinLock(&queryThreadLock);
  3692. ramutex_lock( queryThreadMutex );
  3693. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3694. ramutex_unlock( queryThreadMutex );
  3695. }
  3696. Sql_Free(queryThread_handle);
  3697. if( log_config.sql_logs ) {
  3698. Sql_Free(logmysql_handle);
  3699. }
  3700. return NULL;
  3701. }
  3702. #endif
  3703. /*==========================================
  3704. * Destructor
  3705. *------------------------------------------*/
  3706. int do_final_script() {
  3707. int i;
  3708. #ifdef DEBUG_HASH
  3709. if (battle_config.etc_log)
  3710. {
  3711. FILE *fp = fopen("hash_dump.txt","wt");
  3712. if(fp) {
  3713. int count[SCRIPT_HASH_SIZE];
  3714. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3715. int n=0;
  3716. int min=INT_MAX,max=0,zero=0;
  3717. double mean=0.0f;
  3718. double median=0.0f;
  3719. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3720. memset(count, 0, sizeof(count));
  3721. fprintf(fp,"num : hash : data_name\n");
  3722. fprintf(fp,"---------------------------------------------------------------\n");
  3723. for(i=LABEL_START; i<str_num; i++) {
  3724. unsigned int h = calc_hash(get_str(i));
  3725. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3726. ++count[h];
  3727. }
  3728. fprintf(fp,"--------------------\n\n");
  3729. memset(count2, 0, sizeof(count2));
  3730. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3731. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3732. if(min > count[i])
  3733. min = count[i]; // minimun count of collision
  3734. if(max < count[i])
  3735. max = count[i]; // maximun count of collision
  3736. if(count[i] == 0)
  3737. zero++;
  3738. ++count2[count[i]];
  3739. }
  3740. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3741. for( i=min; i <= max; ++i ){
  3742. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3743. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3744. }
  3745. for( i=min; i <= max; ++i ){
  3746. n += count2[i];
  3747. if( n*2 >= SCRIPT_HASH_SIZE )
  3748. {
  3749. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3750. median = (i+i+1)/2.0f;
  3751. else
  3752. median = i;
  3753. break;
  3754. }
  3755. }
  3756. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3757. fclose(fp);
  3758. }
  3759. }
  3760. #endif
  3761. mapreg_final();
  3762. db_destroy(scriptlabel_db);
  3763. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3764. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3765. if(sleep_db) {
  3766. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3767. while(n) {
  3768. struct script_state *st = (struct script_state *)n->data;
  3769. script_free_state(st);
  3770. n = n->next;
  3771. }
  3772. linkdb_final(&sleep_db);
  3773. }
  3774. if (str_data)
  3775. aFree(str_data);
  3776. if (str_buf)
  3777. aFree(str_buf);
  3778. for( i = 0; i < atcmd_binding_count; i++ ) {
  3779. aFree(atcmd_binding[i]);
  3780. }
  3781. if( atcmd_binding_count != 0 )
  3782. aFree(atcmd_binding);
  3783. #ifdef BETA_THREAD_TEST
  3784. /* QueryThread */
  3785. InterlockedIncrement(&queryThreadTerminate);
  3786. racond_signal(queryThreadCond);
  3787. rathread_wait(queryThread, NULL);
  3788. // Destroy cond var and mutex.
  3789. racond_destroy( queryThreadCond );
  3790. ramutex_destroy( queryThreadMutex );
  3791. /* Clear missing vars */
  3792. for( i = 0; i < queryThreadData.count; i++ ) {
  3793. aFree(queryThreadData.entry[i]);
  3794. }
  3795. aFree(queryThreadData.entry);
  3796. for( i = 0; i < logThreadData.count; i++ ) {
  3797. aFree(logThreadData.entry[i]);
  3798. }
  3799. aFree(logThreadData.entry);
  3800. #endif
  3801. return 0;
  3802. }
  3803. /*==========================================
  3804. * Initialization
  3805. *------------------------------------------*/
  3806. int do_init_script() {
  3807. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3808. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3809. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3810. mapreg_init();
  3811. #ifdef BETA_THREAD_TEST
  3812. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  3813. queryThreadData.count = 0;
  3814. CREATE(logThreadData.entry, char *, 1);
  3815. logThreadData.count = 0;
  3816. /* QueryThread Start */
  3817. InitializeSpinLock(&queryThreadLock);
  3818. queryThreadData.timer = INVALID_TIMER;
  3819. queryThreadTerminate = 0;
  3820. queryThreadMutex = ramutex_create();
  3821. queryThreadCond = racond_create();
  3822. queryThread = rathread_create(queryThread_main, NULL);
  3823. if(queryThread == NULL){
  3824. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  3825. exit(EXIT_FAILURE);
  3826. }
  3827. add_timer_func_list(queryThread_timer, "queryThread_timer");
  3828. #endif
  3829. return 0;
  3830. }
  3831. int script_reload() {
  3832. int i;
  3833. #ifdef BETA_THREAD_TEST
  3834. /* we're reloading so any queries undergoing should be...exterminated. */
  3835. EnterSpinLock(&queryThreadLock);
  3836. for( i = 0; i < queryThreadData.count; i++ ) {
  3837. aFree(queryThreadData.entry[i]);
  3838. }
  3839. queryThreadData.count = 0;
  3840. if( queryThreadData.timer != INVALID_TIMER ) {
  3841. delete_timer(queryThreadData.timer, queryThread_timer);
  3842. queryThreadData.timer = INVALID_TIMER;
  3843. }
  3844. LeaveSpinLock(&queryThreadLock);
  3845. #endif
  3846. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  3847. db_clear(scriptlabel_db);
  3848. // @commands (script based)
  3849. // Clear bindings
  3850. for( i = 0; i < atcmd_binding_count; i++ ) {
  3851. aFree(atcmd_binding[i]);
  3852. }
  3853. if( atcmd_binding_count != 0 )
  3854. aFree(atcmd_binding);
  3855. atcmd_binding_count = 0;
  3856. if(sleep_db) {
  3857. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3858. while(n) {
  3859. struct script_state *st = (struct script_state *)n->data;
  3860. script_free_state(st);
  3861. n = n->next;
  3862. }
  3863. linkdb_final(&sleep_db);
  3864. }
  3865. mapreg_reload();
  3866. return 0;
  3867. }
  3868. //-----------------------------------------------------------------------------
  3869. // buildin functions
  3870. //
  3871. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3872. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3873. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3874. /////////////////////////////////////////////////////////////////////
  3875. // NPC interaction
  3876. //
  3877. /// Appends a message to the npc dialog.
  3878. /// If a dialog doesn't exist yet, one is created.
  3879. ///
  3880. /// mes "<message>";
  3881. BUILDIN_FUNC(mes)
  3882. {
  3883. TBL_PC* sd = script_rid2sd(st);
  3884. if( sd == NULL )
  3885. return 0;
  3886. if( !script_hasdata(st, 3) )
  3887. {// only a single line detected in the script
  3888. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3889. }
  3890. else
  3891. {// parse multiple lines as they exist
  3892. int i;
  3893. for( i = 2; script_hasdata(st, i); i++ )
  3894. {
  3895. // send the message to the client
  3896. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  3897. }
  3898. }
  3899. return 0;
  3900. }
  3901. /// Displays the button 'next' in the npc dialog.
  3902. /// The dialog text is cleared and the script continues when the button is pressed.
  3903. ///
  3904. /// next;
  3905. BUILDIN_FUNC(next)
  3906. {
  3907. TBL_PC* sd;
  3908. sd = script_rid2sd(st);
  3909. if( sd == NULL )
  3910. return 0;
  3911. st->state = STOP;
  3912. clif_scriptnext(sd, st->oid);
  3913. return 0;
  3914. }
  3915. /// Ends the script and displays the button 'close' on the npc dialog.
  3916. /// The dialog is closed when the button is pressed.
  3917. ///
  3918. /// close;
  3919. BUILDIN_FUNC(close)
  3920. {
  3921. TBL_PC* sd;
  3922. sd = script_rid2sd(st);
  3923. if( sd == NULL )
  3924. return 0;
  3925. st->state = END; //Should be CLOSE, but breaks backwards compatibility.
  3926. clif_scriptclose(sd, st->oid);
  3927. return 0;
  3928. }
  3929. /// Displays the button 'close' on the npc dialog.
  3930. /// The dialog is closed and the script continues when the button is pressed.
  3931. ///
  3932. /// close2;
  3933. BUILDIN_FUNC(close2)
  3934. {
  3935. TBL_PC* sd;
  3936. sd = script_rid2sd(st);
  3937. if( sd == NULL )
  3938. return 0;
  3939. st->state = STOP;
  3940. clif_scriptclose(sd, st->oid);
  3941. return 0;
  3942. }
  3943. /// Counts the number of valid and total number of options in 'str'
  3944. /// If max_count > 0 the counting stops when that valid option is reached
  3945. /// total is incremented for each option (NULL is supported)
  3946. static int menu_countoptions(const char* str, int max_count, int* total)
  3947. {
  3948. int count = 0;
  3949. int bogus_total;
  3950. if( total == NULL )
  3951. total = &bogus_total;
  3952. ++(*total);
  3953. // initial empty options
  3954. while( *str == ':' )
  3955. {
  3956. ++str;
  3957. ++(*total);
  3958. }
  3959. // count menu options
  3960. while( *str != '\0' )
  3961. {
  3962. ++count;
  3963. --max_count;
  3964. if( max_count == 0 )
  3965. break;
  3966. while( *str != ':' && *str != '\0' )
  3967. ++str;
  3968. while( *str == ':' )
  3969. {
  3970. ++str;
  3971. ++(*total);
  3972. }
  3973. }
  3974. return count;
  3975. }
  3976. /// Displays a menu with options and goes to the target label.
  3977. /// The script is stopped if cancel is pressed.
  3978. /// Options with no text are not displayed in the client.
  3979. ///
  3980. /// Options can be grouped together, separated by the character ':' in the text:
  3981. /// ex: menu "A:B:C",L_target;
  3982. /// All these options go to the specified target label.
  3983. ///
  3984. /// The index of the selected option is put in the variable @menu.
  3985. /// Indexes start with 1 and are consistent with grouped and empty options.
  3986. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3987. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3988. ///
  3989. /// NOTE: the client closes the npc dialog when cancel is pressed
  3990. ///
  3991. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3992. BUILDIN_FUNC(menu)
  3993. {
  3994. int i;
  3995. const char* text;
  3996. TBL_PC* sd;
  3997. sd = script_rid2sd(st);
  3998. if( sd == NULL )
  3999. return 0;
  4000. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4001. if( sd->state.menu_or_input == 0 )
  4002. {
  4003. struct StringBuf buf;
  4004. struct script_data* data;
  4005. if( script_lastdata(st) % 2 == 0 )
  4006. {// argument count is not even (1st argument is at index 2)
  4007. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4008. st->state = END;
  4009. return 1;
  4010. }
  4011. StringBuf_Init(&buf);
  4012. sd->npc_menu = 0;
  4013. for( i = 2; i < script_lastdata(st); i += 2 )
  4014. {
  4015. // menu options
  4016. text = script_getstr(st, i);
  4017. // target label
  4018. data = script_getdata(st, i+1);
  4019. if( !data_islabel(data) )
  4020. {// not a label
  4021. StringBuf_Destroy(&buf);
  4022. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4023. script_reportdata(data);
  4024. st->state = END;
  4025. return 1;
  4026. }
  4027. // append option(s)
  4028. if( text[0] == '\0' )
  4029. continue;// empty string, ignore
  4030. if( sd->npc_menu > 0 )
  4031. StringBuf_AppendStr(&buf, ":");
  4032. StringBuf_AppendStr(&buf, text);
  4033. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4034. }
  4035. st->state = RERUNLINE;
  4036. sd->state.menu_or_input = 1;
  4037. /**
  4038. * menus beyond this length crash the client (see bugreport:6402)
  4039. **/
  4040. if( StringBuf_Length(&buf) >= 2047 ) {
  4041. struct npc_data * nd = map_id2nd(st->oid);
  4042. char* menu;
  4043. CREATE(menu, char, 2048);
  4044. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4045. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4046. clif_scriptmenu(sd, st->oid, menu);
  4047. aFree(menu);
  4048. } else
  4049. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4050. StringBuf_Destroy(&buf);
  4051. if( sd->npc_menu >= 0xff )
  4052. {// 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
  4053. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4054. script_reportsrc(st);
  4055. }
  4056. }
  4057. else if( sd->npc_menu == 0xff )
  4058. {// Cancel was pressed
  4059. sd->state.menu_or_input = 0;
  4060. st->state = END;
  4061. }
  4062. else
  4063. {// goto target label
  4064. int menu = 0;
  4065. sd->state.menu_or_input = 0;
  4066. if( sd->npc_menu <= 0 )
  4067. {
  4068. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4069. st->state = END;
  4070. return 1;
  4071. }
  4072. // get target label
  4073. for( i = 2; i < script_lastdata(st); i += 2 )
  4074. {
  4075. text = script_getstr(st, i);
  4076. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4077. if( sd->npc_menu <= 0 )
  4078. break;// entry found
  4079. }
  4080. if( sd->npc_menu > 0 )
  4081. {// Invalid selection
  4082. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4083. st->state = END;
  4084. return 1;
  4085. }
  4086. if( !data_islabel(script_getdata(st, i + 1)) )
  4087. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4088. ShowError("script:menu: unexpected data in label argument\n");
  4089. script_reportdata(script_getdata(st, i + 1));
  4090. st->state = END;
  4091. return 1;
  4092. }
  4093. pc_setreg(sd, add_str("@menu"), menu);
  4094. st->pos = script_getnum(st, i + 1);
  4095. st->state = GOTO;
  4096. }
  4097. return 0;
  4098. }
  4099. /// Displays a menu with options and returns the selected option.
  4100. /// Behaves like 'menu' without the target labels.
  4101. ///
  4102. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4103. ///
  4104. /// @see menu
  4105. BUILDIN_FUNC(select)
  4106. {
  4107. int i;
  4108. const char* text;
  4109. TBL_PC* sd;
  4110. sd = script_rid2sd(st);
  4111. if( sd == NULL )
  4112. return 0;
  4113. if( sd->state.menu_or_input == 0 ) {
  4114. struct StringBuf buf;
  4115. StringBuf_Init(&buf);
  4116. sd->npc_menu = 0;
  4117. for( i = 2; i <= script_lastdata(st); ++i ) {
  4118. text = script_getstr(st, i);
  4119. if( sd->npc_menu > 0 )
  4120. StringBuf_AppendStr(&buf, ":");
  4121. StringBuf_AppendStr(&buf, text);
  4122. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4123. }
  4124. st->state = RERUNLINE;
  4125. sd->state.menu_or_input = 1;
  4126. /**
  4127. * menus beyond this length crash the client (see bugreport:6402)
  4128. **/
  4129. if( StringBuf_Length(&buf) >= 2047 ) {
  4130. struct npc_data * nd = map_id2nd(st->oid);
  4131. char* menu;
  4132. CREATE(menu, char, 2048);
  4133. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4134. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4135. clif_scriptmenu(sd, st->oid, menu);
  4136. aFree(menu);
  4137. } else
  4138. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4139. StringBuf_Destroy(&buf);
  4140. if( sd->npc_menu >= 0xff ) {
  4141. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4142. script_reportsrc(st);
  4143. }
  4144. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4145. sd->state.menu_or_input = 0;
  4146. st->state = END;
  4147. } else {// return selected option
  4148. int menu = 0;
  4149. sd->state.menu_or_input = 0;
  4150. for( i = 2; i <= script_lastdata(st); ++i ) {
  4151. text = script_getstr(st, i);
  4152. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4153. if( sd->npc_menu <= 0 )
  4154. break;// entry found
  4155. }
  4156. pc_setreg(sd, add_str("@menu"), menu);
  4157. script_pushint(st, menu);
  4158. st->state = RUN;
  4159. }
  4160. return 0;
  4161. }
  4162. /// Displays a menu with options and returns the selected option.
  4163. /// Behaves like 'menu' without the target labels, except when cancel is
  4164. /// pressed.
  4165. /// When cancel is pressed, the script continues and 255 is returned.
  4166. ///
  4167. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4168. ///
  4169. /// @see menu
  4170. BUILDIN_FUNC(prompt)
  4171. {
  4172. int i;
  4173. const char *text;
  4174. TBL_PC* sd;
  4175. sd = script_rid2sd(st);
  4176. if( sd == NULL )
  4177. return 0;
  4178. if( sd->state.menu_or_input == 0 )
  4179. {
  4180. struct StringBuf buf;
  4181. StringBuf_Init(&buf);
  4182. sd->npc_menu = 0;
  4183. for( i = 2; i <= script_lastdata(st); ++i )
  4184. {
  4185. text = script_getstr(st, i);
  4186. if( sd->npc_menu > 0 )
  4187. StringBuf_AppendStr(&buf, ":");
  4188. StringBuf_AppendStr(&buf, text);
  4189. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4190. }
  4191. st->state = RERUNLINE;
  4192. sd->state.menu_or_input = 1;
  4193. /**
  4194. * menus beyond this length crash the client (see bugreport:6402)
  4195. **/
  4196. if( StringBuf_Length(&buf) >= 2047 ) {
  4197. struct npc_data * nd = map_id2nd(st->oid);
  4198. char* menu;
  4199. CREATE(menu, char, 2048);
  4200. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4201. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4202. clif_scriptmenu(sd, st->oid, menu);
  4203. aFree(menu);
  4204. } else
  4205. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4206. StringBuf_Destroy(&buf);
  4207. if( sd->npc_menu >= 0xff )
  4208. {
  4209. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4210. script_reportsrc(st);
  4211. }
  4212. }
  4213. else if( sd->npc_menu == 0xff )
  4214. {// Cancel was pressed
  4215. sd->state.menu_or_input = 0;
  4216. pc_setreg(sd, add_str("@menu"), 0xff);
  4217. script_pushint(st, 0xff);
  4218. st->state = RUN;
  4219. }
  4220. else
  4221. {// return selected option
  4222. int menu = 0;
  4223. sd->state.menu_or_input = 0;
  4224. for( i = 2; i <= script_lastdata(st); ++i )
  4225. {
  4226. text = script_getstr(st, i);
  4227. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4228. if( sd->npc_menu <= 0 )
  4229. break;// entry found
  4230. }
  4231. pc_setreg(sd, add_str("@menu"), menu);
  4232. script_pushint(st, menu);
  4233. st->state = RUN;
  4234. }
  4235. return 0;
  4236. }
  4237. /////////////////////////////////////////////////////////////////////
  4238. // ...
  4239. //
  4240. /// Jumps to the target script label.
  4241. ///
  4242. /// goto <label>;
  4243. BUILDIN_FUNC(goto)
  4244. {
  4245. if( !data_islabel(script_getdata(st,2)) )
  4246. {
  4247. ShowError("script:goto: not a label\n");
  4248. script_reportdata(script_getdata(st,2));
  4249. st->state = END;
  4250. return 1;
  4251. }
  4252. st->pos = script_getnum(st,2);
  4253. st->state = GOTO;
  4254. return 0;
  4255. }
  4256. /*==========================================
  4257. * user-defined function call
  4258. *------------------------------------------*/
  4259. BUILDIN_FUNC(callfunc)
  4260. {
  4261. int i, j;
  4262. struct script_retinfo* ri;
  4263. struct script_code* scr;
  4264. const char* str = script_getstr(st,2);
  4265. DBMap **ref = NULL;
  4266. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4267. if( !scr )
  4268. {
  4269. ShowError("script:callfunc: function not found! [%s]\n", str);
  4270. st->state = END;
  4271. return 1;
  4272. }
  4273. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4274. {
  4275. struct script_data* data = push_copy(st->stack,i);
  4276. if( data_isreference(data) && !data->ref )
  4277. {
  4278. const char* name = reference_getname(data);
  4279. if( name[0] == '.' ) {
  4280. if(!ref){
  4281. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4282. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4283. }
  4284. data->ref = ref;
  4285. }
  4286. }
  4287. }
  4288. CREATE(ri, struct script_retinfo, 1);
  4289. ri->script = st->script;// script code
  4290. ri->var_function = st->stack->var_function;// scope variables
  4291. ri->pos = st->pos;// script location
  4292. ri->nargs = j;// argument count
  4293. ri->defsp = st->stack->defsp;// default stack pointer
  4294. push_retinfo(st->stack, ri, ref);
  4295. st->pos = 0;
  4296. st->script = scr;
  4297. st->stack->defsp = st->stack->sp;
  4298. st->state = GOTO;
  4299. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4300. return 0;
  4301. }
  4302. /*==========================================
  4303. * subroutine call
  4304. *------------------------------------------*/
  4305. BUILDIN_FUNC(callsub)
  4306. {
  4307. int i,j;
  4308. struct script_retinfo* ri;
  4309. int pos = script_getnum(st,2);
  4310. DBMap **ref = NULL;
  4311. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4312. {
  4313. ShowError("script:callsub: argument is not a label\n");
  4314. script_reportdata(script_getdata(st,2));
  4315. st->state = END;
  4316. return 1;
  4317. }
  4318. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4319. {
  4320. struct script_data* data = push_copy(st->stack,i);
  4321. if( data_isreference(data) && !data->ref )
  4322. {
  4323. const char* name = reference_getname(data);
  4324. if( name[0] == '.' && name[1] == '@' ) {
  4325. if ( !ref ) {
  4326. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4327. ref[0] = st->stack->var_function;
  4328. }
  4329. data->ref = ref;
  4330. }
  4331. }
  4332. }
  4333. CREATE(ri, struct script_retinfo, 1);
  4334. ri->script = st->script;// script code
  4335. ri->var_function = st->stack->var_function;// scope variables
  4336. ri->pos = st->pos;// script location
  4337. ri->nargs = j;// argument count
  4338. ri->defsp = st->stack->defsp;// default stack pointer
  4339. push_retinfo(st->stack, ri, ref);
  4340. st->pos = pos;
  4341. st->stack->defsp = st->stack->sp;
  4342. st->state = GOTO;
  4343. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4344. return 0;
  4345. }
  4346. /// Retrieves an argument provided to callfunc/callsub.
  4347. /// If the argument doesn't exist
  4348. ///
  4349. /// getarg(<index>{,<default_value>}) -> <value>
  4350. BUILDIN_FUNC(getarg)
  4351. {
  4352. struct script_retinfo* ri;
  4353. int idx;
  4354. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4355. {
  4356. ShowError("script:getarg: no callfunc or callsub!\n");
  4357. st->state = END;
  4358. return 1;
  4359. }
  4360. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4361. idx = script_getnum(st,2);
  4362. if( idx >= 0 && idx < ri->nargs )
  4363. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4364. else if( script_hasdata(st,3) )
  4365. script_pushcopy(st, 3);
  4366. else
  4367. {
  4368. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4369. st->state = END;
  4370. return 1;
  4371. }
  4372. return 0;
  4373. }
  4374. /// Returns from the current function, optionaly returning a value from the functions.
  4375. /// Don't use outside script functions.
  4376. ///
  4377. /// return;
  4378. /// return <value>;
  4379. BUILDIN_FUNC(return)
  4380. {
  4381. if( script_hasdata(st,2) )
  4382. {// return value
  4383. struct script_data* data;
  4384. script_pushcopy(st, 2);
  4385. data = script_getdatatop(st, -1);
  4386. if( data_isreference(data) )
  4387. {
  4388. const char* name = reference_getname(data);
  4389. if( name[0] == '.' && name[1] == '@' )
  4390. {// scope variable
  4391. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4392. get_val(st, data);// current scope, convert to value
  4393. }
  4394. else if( name[0] == '.' && !data->ref )
  4395. {// script variable, link to current script
  4396. data->ref = &st->script->script_vars;
  4397. }
  4398. }
  4399. }
  4400. else
  4401. {// no return value
  4402. script_pushnil(st);
  4403. }
  4404. st->state = RETFUNC;
  4405. return 0;
  4406. }
  4407. /// Returns a random number from 0 to <range>-1.
  4408. /// Or returns a random number from <min> to <max>.
  4409. /// If <min> is greater than <max>, their numbers are switched.
  4410. /// rand(<range>) -> <int>
  4411. /// rand(<min>,<max>) -> <int>
  4412. BUILDIN_FUNC(rand)
  4413. {
  4414. int range;
  4415. int min;
  4416. int max;
  4417. if( script_hasdata(st,3) )
  4418. {// min,max
  4419. min = script_getnum(st,2);
  4420. max = script_getnum(st,3);
  4421. if( max < min )
  4422. swap(min, max);
  4423. range = max - min + 1;
  4424. }
  4425. else
  4426. {// range
  4427. min = 0;
  4428. range = script_getnum(st,2);
  4429. }
  4430. if( range <= 1 )
  4431. script_pushint(st, min);
  4432. else
  4433. script_pushint(st, rnd()%range + min);
  4434. return 0;
  4435. }
  4436. /*==========================================
  4437. * Warp sd to str,x,y or Random or SavePoint/Save
  4438. *------------------------------------------*/
  4439. BUILDIN_FUNC(warp)
  4440. {
  4441. int ret;
  4442. int x,y;
  4443. const char* str;
  4444. TBL_PC* sd;
  4445. sd = script_rid2sd(st);
  4446. if( sd == NULL )
  4447. return 0;
  4448. str = script_getstr(st,2);
  4449. x = script_getnum(st,3);
  4450. y = script_getnum(st,4);
  4451. if(strcmp(str,"Random")==0)
  4452. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4453. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4454. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4455. else
  4456. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4457. if( ret ) {
  4458. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4459. script_reportsrc(st);
  4460. }
  4461. return 0;
  4462. }
  4463. /*==========================================
  4464. * Warp a specified area
  4465. *------------------------------------------*/
  4466. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4467. {
  4468. int x2,y2,x3,y3;
  4469. unsigned int index;
  4470. index = va_arg(ap,unsigned int);
  4471. x2 = va_arg(ap,int);
  4472. y2 = va_arg(ap,int);
  4473. x3 = va_arg(ap,int);
  4474. y3 = va_arg(ap,int);
  4475. if(index == 0)
  4476. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4477. else if(x3 && y3) {
  4478. int max, tx, ty, j = 0;
  4479. // choose a suitable max number of attempts
  4480. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4481. max = 1000;
  4482. // find a suitable map cell
  4483. do {
  4484. tx = rnd()%(x3-x2+1)+x2;
  4485. ty = rnd()%(y3-y2+1)+y2;
  4486. j++;
  4487. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4488. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4489. }
  4490. else
  4491. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4492. return 0;
  4493. }
  4494. BUILDIN_FUNC(areawarp)
  4495. {
  4496. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4497. unsigned int index;
  4498. const char *str;
  4499. const char *mapname;
  4500. mapname = script_getstr(st,2);
  4501. x0 = script_getnum(st,3);
  4502. y0 = script_getnum(st,4);
  4503. x1 = script_getnum(st,5);
  4504. y1 = script_getnum(st,6);
  4505. str = script_getstr(st,7);
  4506. x2 = script_getnum(st,8);
  4507. y2 = script_getnum(st,9);
  4508. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4509. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4510. x3 = 0;
  4511. y3 = 0;
  4512. } else if( x3 && y3 ) {
  4513. // normalize x3/y3 coordinates
  4514. if( x3 < x2 ) swap(x3,x2);
  4515. if( y3 < y2 ) swap(y3,y2);
  4516. }
  4517. }
  4518. if( (m = map_mapname2mapid(mapname)) < 0 )
  4519. return 0;
  4520. if( strcmp(str,"Random") == 0 )
  4521. index = 0;
  4522. else if( !(index=mapindex_name2id(str)) )
  4523. return 0;
  4524. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4525. return 0;
  4526. }
  4527. /*==========================================
  4528. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4529. *------------------------------------------*/
  4530. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4531. {
  4532. int hp, sp;
  4533. hp = va_arg(ap, int);
  4534. sp = va_arg(ap, int);
  4535. pc_percentheal((TBL_PC *)bl,hp,sp);
  4536. return 0;
  4537. }
  4538. BUILDIN_FUNC(areapercentheal)
  4539. {
  4540. int hp,sp,m;
  4541. const char *mapname;
  4542. int x0,y0,x1,y1;
  4543. mapname=script_getstr(st,2);
  4544. x0=script_getnum(st,3);
  4545. y0=script_getnum(st,4);
  4546. x1=script_getnum(st,5);
  4547. y1=script_getnum(st,6);
  4548. hp=script_getnum(st,7);
  4549. sp=script_getnum(st,8);
  4550. if( (m=map_mapname2mapid(mapname))< 0)
  4551. return 0;
  4552. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4553. return 0;
  4554. }
  4555. /*==========================================
  4556. * warpchar [LuzZza]
  4557. * Useful for warp one player from
  4558. * another player npc-session.
  4559. * Using: warpchar "mapname",x,y,Char_ID;
  4560. *------------------------------------------*/
  4561. BUILDIN_FUNC(warpchar)
  4562. {
  4563. int x,y,a;
  4564. const char *str;
  4565. TBL_PC *sd;
  4566. str=script_getstr(st,2);
  4567. x=script_getnum(st,3);
  4568. y=script_getnum(st,4);
  4569. a=script_getnum(st,5);
  4570. sd = map_charid2sd(a);
  4571. if( sd == NULL )
  4572. return 0;
  4573. if(strcmp(str, "Random") == 0)
  4574. pc_randomwarp(sd, CLR_TELEPORT);
  4575. else
  4576. if(strcmp(str, "SavePoint") == 0)
  4577. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4578. else
  4579. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4580. return 0;
  4581. }
  4582. /*==========================================
  4583. * Warpparty - [Fredzilla] [Paradox924X]
  4584. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4585. * If 'from_mapname' is specified, only the party members on that map will be warped
  4586. *------------------------------------------*/
  4587. BUILDIN_FUNC(warpparty)
  4588. {
  4589. TBL_PC *sd = NULL;
  4590. TBL_PC *pl_sd;
  4591. struct party_data* p;
  4592. int type;
  4593. int mapindex;
  4594. int i;
  4595. const char* str = script_getstr(st,2);
  4596. int x = script_getnum(st,3);
  4597. int y = script_getnum(st,4);
  4598. int p_id = script_getnum(st,5);
  4599. const char* str2 = NULL;
  4600. if ( script_hasdata(st,6) )
  4601. str2 = script_getstr(st,6);
  4602. p = party_search(p_id);
  4603. if(!p)
  4604. return 0;
  4605. type = ( strcmp(str,"Random")==0 ) ? 0
  4606. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4607. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4608. : ( strcmp(str,"Leader")==0 ) ? 3
  4609. : 4;
  4610. switch (type)
  4611. {
  4612. case 3:
  4613. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4614. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4615. return 0;
  4616. pl_sd = p->data[i].sd;
  4617. mapindex = pl_sd->mapindex;
  4618. x = pl_sd->bl.x;
  4619. y = pl_sd->bl.y;
  4620. break;
  4621. case 4:
  4622. mapindex = mapindex_name2id(str);
  4623. break;
  4624. case 2:
  4625. //"SavePoint" uses save point of the currently attached player
  4626. if (( sd = script_rid2sd(st) ) == NULL )
  4627. return 0;
  4628. default:
  4629. mapindex = 0;
  4630. break;
  4631. }
  4632. for (i = 0; i < MAX_PARTY; i++)
  4633. {
  4634. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4635. continue;
  4636. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4637. continue;
  4638. if( pc_isdead(pl_sd) )
  4639. continue;
  4640. switch( type )
  4641. {
  4642. case 0: // Random
  4643. if(!map[pl_sd->bl.m].flag.nowarp)
  4644. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4645. break;
  4646. case 1: // SavePointAll
  4647. if(!map[pl_sd->bl.m].flag.noreturn)
  4648. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4649. break;
  4650. case 2: // SavePoint
  4651. if(!map[pl_sd->bl.m].flag.noreturn)
  4652. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4653. break;
  4654. case 3: // Leader
  4655. case 4: // m,x,y
  4656. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4657. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4658. break;
  4659. }
  4660. }
  4661. return 0;
  4662. }
  4663. /*==========================================
  4664. * Warpguild - [Fredzilla]
  4665. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4666. *------------------------------------------*/
  4667. BUILDIN_FUNC(warpguild)
  4668. {
  4669. TBL_PC *sd = NULL;
  4670. TBL_PC *pl_sd;
  4671. struct guild* g;
  4672. struct s_mapiterator* iter;
  4673. int type;
  4674. const char* str = script_getstr(st,2);
  4675. int x = script_getnum(st,3);
  4676. int y = script_getnum(st,4);
  4677. int gid = script_getnum(st,5);
  4678. g = guild_search(gid);
  4679. if( g == NULL )
  4680. return 0;
  4681. type = ( strcmp(str,"Random")==0 ) ? 0
  4682. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4683. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4684. : 3;
  4685. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4686. {// "SavePoint" uses save point of the currently attached player
  4687. return 0;
  4688. }
  4689. iter = mapit_getallusers();
  4690. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4691. {
  4692. if( pl_sd->status.guild_id != gid )
  4693. continue;
  4694. switch( type )
  4695. {
  4696. case 0: // Random
  4697. if(!map[pl_sd->bl.m].flag.nowarp)
  4698. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4699. break;
  4700. case 1: // SavePointAll
  4701. if(!map[pl_sd->bl.m].flag.noreturn)
  4702. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4703. break;
  4704. case 2: // SavePoint
  4705. if(!map[pl_sd->bl.m].flag.noreturn)
  4706. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4707. break;
  4708. case 3: // m,x,y
  4709. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4710. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4711. break;
  4712. }
  4713. }
  4714. mapit_free(iter);
  4715. return 0;
  4716. }
  4717. /*==========================================
  4718. * Force Heal a player (hp and sp)
  4719. *------------------------------------------*/
  4720. BUILDIN_FUNC(heal)
  4721. {
  4722. TBL_PC *sd;
  4723. int hp,sp;
  4724. sd = script_rid2sd(st);
  4725. if (!sd) return 0;
  4726. hp=script_getnum(st,2);
  4727. sp=script_getnum(st,3);
  4728. status_heal(&sd->bl, hp, sp, 1);
  4729. return 0;
  4730. }
  4731. /*==========================================
  4732. * Heal a player by item (get vit bonus etc)
  4733. *------------------------------------------*/
  4734. BUILDIN_FUNC(itemheal)
  4735. {
  4736. TBL_PC *sd;
  4737. int hp,sp;
  4738. hp=script_getnum(st,2);
  4739. sp=script_getnum(st,3);
  4740. if(potion_flag==1) {
  4741. potion_hp = hp;
  4742. potion_sp = sp;
  4743. return 0;
  4744. }
  4745. sd = script_rid2sd(st);
  4746. if (!sd) return 0;
  4747. pc_itemheal(sd,sd->itemid,hp,sp);
  4748. return 0;
  4749. }
  4750. /*==========================================
  4751. *
  4752. *------------------------------------------*/
  4753. BUILDIN_FUNC(percentheal)
  4754. {
  4755. int hp,sp;
  4756. TBL_PC* sd;
  4757. hp=script_getnum(st,2);
  4758. sp=script_getnum(st,3);
  4759. if(potion_flag==1) {
  4760. potion_per_hp = hp;
  4761. potion_per_sp = sp;
  4762. return 0;
  4763. }
  4764. sd = script_rid2sd(st);
  4765. if( sd == NULL )
  4766. return 0;
  4767. #ifdef RENEWAL
  4768. if( sd->sc.data[SC_EXTREMITYFIST2] )
  4769. sp = 0;
  4770. #endif
  4771. pc_percentheal(sd,hp,sp);
  4772. return 0;
  4773. }
  4774. /*==========================================
  4775. *
  4776. *------------------------------------------*/
  4777. BUILDIN_FUNC(jobchange)
  4778. {
  4779. int job, upper=-1;
  4780. job=script_getnum(st,2);
  4781. if( script_hasdata(st,3) )
  4782. upper=script_getnum(st,3);
  4783. if (pcdb_checkid(job))
  4784. {
  4785. TBL_PC* sd;
  4786. sd = script_rid2sd(st);
  4787. if( sd == NULL )
  4788. return 0;
  4789. pc_jobchange(sd, job, upper);
  4790. }
  4791. return 0;
  4792. }
  4793. /*==========================================
  4794. *
  4795. *------------------------------------------*/
  4796. BUILDIN_FUNC(jobname)
  4797. {
  4798. int class_=script_getnum(st,2);
  4799. script_pushconststr(st, (char*)job_name(class_));
  4800. return 0;
  4801. }
  4802. /// Get input from the player.
  4803. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4804. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4805. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4806. /// shorter than 'min' and 0 otherwise.
  4807. ///
  4808. /// input(<var>{,<min>{,<max>}}) -> <int>
  4809. BUILDIN_FUNC(input)
  4810. {
  4811. TBL_PC* sd;
  4812. struct script_data* data;
  4813. int uid;
  4814. const char* name;
  4815. int min;
  4816. int max;
  4817. sd = script_rid2sd(st);
  4818. if( sd == NULL )
  4819. return 0;
  4820. data = script_getdata(st,2);
  4821. if( !data_isreference(data) ){
  4822. ShowError("script:input: not a variable\n");
  4823. script_reportdata(data);
  4824. st->state = END;
  4825. return 1;
  4826. }
  4827. uid = reference_getuid(data);
  4828. name = reference_getname(data);
  4829. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4830. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4831. if( !sd->state.menu_or_input )
  4832. { // first invocation, display npc input box
  4833. sd->state.menu_or_input = 1;
  4834. st->state = RERUNLINE;
  4835. if( is_string_variable(name) )
  4836. clif_scriptinputstr(sd,st->oid);
  4837. else
  4838. clif_scriptinput(sd,st->oid);
  4839. }
  4840. else
  4841. { // take received text/value and store it in the designated variable
  4842. sd->state.menu_or_input = 0;
  4843. if( is_string_variable(name) )
  4844. {
  4845. int len = (int)strlen(sd->npc_str);
  4846. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4847. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4848. }
  4849. else
  4850. {
  4851. int amount = sd->npc_amount;
  4852. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  4853. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4854. }
  4855. st->state = RUN;
  4856. }
  4857. return 0;
  4858. }
  4859. // declare the copyarray method here for future reference
  4860. BUILDIN_FUNC(copyarray);
  4861. /// Sets the value of a variable.
  4862. /// The value is converted to the type of the variable.
  4863. ///
  4864. /// set(<variable>,<value>) -> <variable>
  4865. BUILDIN_FUNC(set)
  4866. {
  4867. TBL_PC* sd = NULL;
  4868. struct script_data* data;
  4869. //struct script_data* datavalue;
  4870. int num;
  4871. const char* name;
  4872. char prefix;
  4873. data = script_getdata(st,2);
  4874. //datavalue = script_getdata(st,3);
  4875. if( !data_isreference(data) )
  4876. {
  4877. ShowError("script:set: not a variable\n");
  4878. script_reportdata(script_getdata(st,2));
  4879. st->state = END;
  4880. return 1;
  4881. }
  4882. num = reference_getuid(data);
  4883. name = reference_getname(data);
  4884. prefix = *name;
  4885. if( not_server_variable(prefix) )
  4886. {
  4887. sd = script_rid2sd(st);
  4888. if( sd == NULL )
  4889. {
  4890. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4891. return 0;
  4892. }
  4893. }
  4894. #if 0
  4895. if( data_isreference(datavalue) )
  4896. {// the value being referenced is a variable
  4897. const char* namevalue = reference_getname(datavalue);
  4898. if( !not_array_variable(*namevalue) )
  4899. {// array variable being copied into another array variable
  4900. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  4901. {// player must be attached in order to copy a player variable
  4902. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  4903. return 0;
  4904. }
  4905. if( is_string_variable(namevalue) != is_string_variable(name) )
  4906. {// non-matching array value types
  4907. ShowWarning("script:set: two array variables do not match in type.\n");
  4908. return 0;
  4909. }
  4910. // push the maximum number of array values to the stack
  4911. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  4912. // call the copy array method directly
  4913. return buildin_copyarray(st);
  4914. }
  4915. }
  4916. #endif
  4917. if( is_string_variable(name) )
  4918. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4919. else
  4920. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  4921. // return a copy of the variable reference
  4922. script_pushcopy(st,2);
  4923. return 0;
  4924. }
  4925. /////////////////////////////////////////////////////////////////////
  4926. /// Array variables
  4927. ///
  4928. /// Returns the size of the specified array
  4929. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  4930. {
  4931. int32 ret = idx;
  4932. if( isstring )
  4933. {
  4934. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4935. {
  4936. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4937. if( str && *str )
  4938. ret = idx + 1;
  4939. script_removetop(st, -1, 0);
  4940. }
  4941. }
  4942. else
  4943. {
  4944. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  4945. {
  4946. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  4947. if( num )
  4948. ret = idx + 1;
  4949. script_removetop(st, -1, 0);
  4950. }
  4951. }
  4952. return ret;
  4953. }
  4954. /// Sets values of an array, from the starting index.
  4955. /// ex: setarray arr[1],1,2,3;
  4956. ///
  4957. /// setarray <array variable>,<value1>{,<value2>...};
  4958. BUILDIN_FUNC(setarray)
  4959. {
  4960. struct script_data* data;
  4961. const char* name;
  4962. int32 start;
  4963. int32 end;
  4964. int32 id;
  4965. int32 i;
  4966. TBL_PC* sd = NULL;
  4967. data = script_getdata(st, 2);
  4968. if( !data_isreference(data) )
  4969. {
  4970. ShowError("script:setarray: not a variable\n");
  4971. script_reportdata(data);
  4972. st->state = END;
  4973. return 1;// not a variable
  4974. }
  4975. id = reference_getid(data);
  4976. start = reference_getindex(data);
  4977. name = reference_getname(data);
  4978. if( not_array_variable(*name) )
  4979. {
  4980. ShowError("script:setarray: illegal scope\n");
  4981. script_reportdata(data);
  4982. st->state = END;
  4983. return 1;// not supported
  4984. }
  4985. if( not_server_variable(*name) )
  4986. {
  4987. sd = script_rid2sd(st);
  4988. if( sd == NULL )
  4989. return 0;// no player attached
  4990. }
  4991. end = start + script_lastdata(st) - 2;
  4992. if( end > SCRIPT_MAX_ARRAYSIZE )
  4993. end = SCRIPT_MAX_ARRAYSIZE;
  4994. if( is_string_variable(name) )
  4995. {// string array
  4996. for( i = 3; start < end; ++start, ++i )
  4997. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4998. }
  4999. else
  5000. {// int array
  5001. for( i = 3; start < end; ++start, ++i )
  5002. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5003. }
  5004. return 0;
  5005. }
  5006. /// Sets count values of an array, from the starting index.
  5007. /// ex: cleararray arr[0],0,1;
  5008. ///
  5009. /// cleararray <array variable>,<value>,<count>;
  5010. BUILDIN_FUNC(cleararray)
  5011. {
  5012. struct script_data* data;
  5013. const char* name;
  5014. int32 start;
  5015. int32 end;
  5016. int32 id;
  5017. void* v;
  5018. TBL_PC* sd = NULL;
  5019. data = script_getdata(st, 2);
  5020. if( !data_isreference(data) )
  5021. {
  5022. ShowError("script:cleararray: not a variable\n");
  5023. script_reportdata(data);
  5024. st->state = END;
  5025. return 1;// not a variable
  5026. }
  5027. id = reference_getid(data);
  5028. start = reference_getindex(data);
  5029. name = reference_getname(data);
  5030. if( not_array_variable(*name) )
  5031. {
  5032. ShowError("script:cleararray: illegal scope\n");
  5033. script_reportdata(data);
  5034. st->state = END;
  5035. return 1;// not supported
  5036. }
  5037. if( not_server_variable(*name) )
  5038. {
  5039. sd = script_rid2sd(st);
  5040. if( sd == NULL )
  5041. return 0;// no player attached
  5042. }
  5043. if( is_string_variable(name) )
  5044. v = (void*)script_getstr(st, 3);
  5045. else
  5046. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5047. end = start + script_getnum(st, 4);
  5048. if( end > SCRIPT_MAX_ARRAYSIZE )
  5049. end = SCRIPT_MAX_ARRAYSIZE;
  5050. for( ; start < end; ++start )
  5051. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5052. return 0;
  5053. }
  5054. /// Copies data from one array to another.
  5055. /// ex: copyarray arr[0],arr[2],2;
  5056. ///
  5057. /// copyarray <destination array variable>,<source array variable>,<count>;
  5058. BUILDIN_FUNC(copyarray)
  5059. {
  5060. struct script_data* data1;
  5061. struct script_data* data2;
  5062. const char* name1;
  5063. const char* name2;
  5064. int32 idx1;
  5065. int32 idx2;
  5066. int32 id1;
  5067. int32 id2;
  5068. void* v;
  5069. int32 i;
  5070. int32 count;
  5071. TBL_PC* sd = NULL;
  5072. data1 = script_getdata(st, 2);
  5073. data2 = script_getdata(st, 3);
  5074. if( !data_isreference(data1) || !data_isreference(data2) )
  5075. {
  5076. ShowError("script:copyarray: not a variable\n");
  5077. script_reportdata(data1);
  5078. script_reportdata(data2);
  5079. st->state = END;
  5080. return 1;// not a variable
  5081. }
  5082. id1 = reference_getid(data1);
  5083. id2 = reference_getid(data2);
  5084. idx1 = reference_getindex(data1);
  5085. idx2 = reference_getindex(data2);
  5086. name1 = reference_getname(data1);
  5087. name2 = reference_getname(data2);
  5088. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5089. {
  5090. ShowError("script:copyarray: illegal scope\n");
  5091. script_reportdata(data1);
  5092. script_reportdata(data2);
  5093. st->state = END;
  5094. return 1;// not supported
  5095. }
  5096. if( is_string_variable(name1) != is_string_variable(name2) )
  5097. {
  5098. ShowError("script:copyarray: type mismatch\n");
  5099. script_reportdata(data1);
  5100. script_reportdata(data2);
  5101. st->state = END;
  5102. return 1;// data type mismatch
  5103. }
  5104. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5105. {
  5106. sd = script_rid2sd(st);
  5107. if( sd == NULL )
  5108. return 0;// no player attached
  5109. }
  5110. count = script_getnum(st, 4);
  5111. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5112. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5113. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5114. return 0;// nothing to copy
  5115. if( id1 == id2 && idx1 > idx2 )
  5116. {// destination might be overlapping the source - copy in reverse order
  5117. for( i = count - 1; i >= 0; --i )
  5118. {
  5119. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5120. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5121. script_removetop(st, -1, 0);
  5122. }
  5123. }
  5124. else
  5125. {// normal copy
  5126. for( i = 0; i < count; ++i )
  5127. {
  5128. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5129. {
  5130. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5131. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5132. script_removetop(st, -1, 0);
  5133. }
  5134. else// out of range - assume ""/0
  5135. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5136. }
  5137. }
  5138. return 0;
  5139. }
  5140. /// Returns the size of the array.
  5141. /// Assumes that everything before the starting index exists.
  5142. /// ex: getarraysize(arr[3])
  5143. ///
  5144. /// getarraysize(<array variable>) -> <int>
  5145. BUILDIN_FUNC(getarraysize)
  5146. {
  5147. struct script_data* data;
  5148. const char* name;
  5149. data = script_getdata(st, 2);
  5150. if( !data_isreference(data) )
  5151. {
  5152. ShowError("script:getarraysize: not a variable\n");
  5153. script_reportdata(data);
  5154. script_pushnil(st);
  5155. st->state = END;
  5156. return 1;// not a variable
  5157. }
  5158. name = reference_getname(data);
  5159. if( not_array_variable(*name) )
  5160. {
  5161. ShowError("script:getarraysize: illegal scope\n");
  5162. script_reportdata(data);
  5163. script_pushnil(st);
  5164. st->state = END;
  5165. return 1;// not supported
  5166. }
  5167. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5168. return 0;
  5169. }
  5170. /// Deletes count or all the elements in an array, from the starting index.
  5171. /// ex: deletearray arr[4],2;
  5172. ///
  5173. /// deletearray <array variable>;
  5174. /// deletearray <array variable>,<count>;
  5175. BUILDIN_FUNC(deletearray)
  5176. {
  5177. struct script_data* data;
  5178. const char* name;
  5179. int start;
  5180. int end;
  5181. int id;
  5182. TBL_PC *sd = NULL;
  5183. data = script_getdata(st, 2);
  5184. if( !data_isreference(data) )
  5185. {
  5186. ShowError("script:deletearray: not a variable\n");
  5187. script_reportdata(data);
  5188. st->state = END;
  5189. return 1;// not a variable
  5190. }
  5191. id = reference_getid(data);
  5192. start = reference_getindex(data);
  5193. name = reference_getname(data);
  5194. if( not_array_variable(*name) )
  5195. {
  5196. ShowError("script:deletearray: illegal scope\n");
  5197. script_reportdata(data);
  5198. st->state = END;
  5199. return 1;// not supported
  5200. }
  5201. if( not_server_variable(*name) )
  5202. {
  5203. sd = script_rid2sd(st);
  5204. if( sd == NULL )
  5205. return 0;// no player attached
  5206. }
  5207. end = SCRIPT_MAX_ARRAYSIZE;
  5208. if( start >= end )
  5209. return 0;// nothing to free
  5210. if( script_hasdata(st,3) )
  5211. {
  5212. int count = script_getnum(st, 3);
  5213. if( count > end - start )
  5214. count = end - start;
  5215. if( count <= 0 )
  5216. return 0;// nothing to free
  5217. // move rest of the elements backward
  5218. for( ; start + count < end; ++start )
  5219. {
  5220. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5221. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5222. script_removetop(st, -1, 0);
  5223. }
  5224. }
  5225. // clear the rest of the array
  5226. if( is_string_variable(name) )
  5227. {
  5228. for( ; start < end; ++start )
  5229. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5230. }
  5231. else
  5232. {
  5233. for( ; start < end; ++start )
  5234. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5235. }
  5236. return 0;
  5237. }
  5238. /// Returns a reference to the target index of the array variable.
  5239. /// Equivalent to var[index].
  5240. ///
  5241. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5242. BUILDIN_FUNC(getelementofarray)
  5243. {
  5244. struct script_data* data;
  5245. const char* name;
  5246. int32 id;
  5247. int i;
  5248. data = script_getdata(st, 2);
  5249. if( !data_isreference(data) )
  5250. {
  5251. ShowError("script:getelementofarray: not a variable\n");
  5252. script_reportdata(data);
  5253. script_pushnil(st);
  5254. st->state = END;
  5255. return 1;// not a variable
  5256. }
  5257. id = reference_getid(data);
  5258. name = reference_getname(data);
  5259. if( not_array_variable(*name) )
  5260. {
  5261. ShowError("script:getelementofarray: illegal scope\n");
  5262. script_reportdata(data);
  5263. script_pushnil(st);
  5264. st->state = END;
  5265. return 1;// not supported
  5266. }
  5267. i = script_getnum(st, 3);
  5268. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5269. {
  5270. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5271. script_reportdata(data);
  5272. script_pushnil(st);
  5273. st->state = END;
  5274. return 1;// out of range
  5275. }
  5276. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5277. return 0;
  5278. }
  5279. /////////////////////////////////////////////////////////////////////
  5280. /// ...
  5281. ///
  5282. /*==========================================
  5283. *
  5284. *------------------------------------------*/
  5285. BUILDIN_FUNC(setlook)
  5286. {
  5287. int type,val;
  5288. TBL_PC* sd;
  5289. type=script_getnum(st,2);
  5290. val=script_getnum(st,3);
  5291. sd = script_rid2sd(st);
  5292. if( sd == NULL )
  5293. return 0;
  5294. pc_changelook(sd,type,val);
  5295. return 0;
  5296. }
  5297. BUILDIN_FUNC(changelook)
  5298. { // As setlook but only client side
  5299. int type,val;
  5300. TBL_PC* sd;
  5301. type=script_getnum(st,2);
  5302. val=script_getnum(st,3);
  5303. sd = script_rid2sd(st);
  5304. if( sd == NULL )
  5305. return 0;
  5306. clif_changelook(&sd->bl,type,val);
  5307. return 0;
  5308. }
  5309. /*==========================================
  5310. *
  5311. *------------------------------------------*/
  5312. BUILDIN_FUNC(cutin)
  5313. {
  5314. TBL_PC* sd;
  5315. sd = script_rid2sd(st);
  5316. if( sd == NULL )
  5317. return 0;
  5318. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5319. return 0;
  5320. }
  5321. /*==========================================
  5322. *
  5323. *------------------------------------------*/
  5324. BUILDIN_FUNC(viewpoint)
  5325. {
  5326. int type,x,y,id,color;
  5327. TBL_PC* sd;
  5328. type=script_getnum(st,2);
  5329. x=script_getnum(st,3);
  5330. y=script_getnum(st,4);
  5331. id=script_getnum(st,5);
  5332. color=script_getnum(st,6);
  5333. sd = script_rid2sd(st);
  5334. if( sd == NULL )
  5335. return 0;
  5336. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5337. return 0;
  5338. }
  5339. /*==========================================
  5340. *
  5341. *------------------------------------------*/
  5342. BUILDIN_FUNC(countitem)
  5343. {
  5344. int nameid, i;
  5345. int count = 0;
  5346. struct item_data* id = NULL;
  5347. struct script_data* data;
  5348. TBL_PC* sd = script_rid2sd(st);
  5349. if (!sd) {
  5350. script_pushint(st,0);
  5351. return 0;
  5352. }
  5353. data = script_getdata(st,2);
  5354. get_val(st, data); // convert into value in case of a variable
  5355. if( data_isstring(data) )
  5356. {// item name
  5357. id = itemdb_searchname(conv_str(st, data));
  5358. }
  5359. else
  5360. {// item id
  5361. id = itemdb_exists(conv_num(st, data));
  5362. }
  5363. if( id == NULL )
  5364. {
  5365. ShowError("buildin_countitem: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5366. script_pushint(st,0);
  5367. return 1;
  5368. }
  5369. nameid = id->nameid;
  5370. for(i = 0; i < MAX_INVENTORY; i++)
  5371. if(sd->status.inventory[i].nameid == nameid)
  5372. count += sd->status.inventory[i].amount;
  5373. script_pushint(st,count);
  5374. return 0;
  5375. }
  5376. /*==========================================
  5377. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5378. * returns number of items that meet the conditions
  5379. *------------------------------------------*/
  5380. BUILDIN_FUNC(countitem2)
  5381. {
  5382. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5383. int count = 0;
  5384. int i;
  5385. struct item_data* id = NULL;
  5386. struct script_data* data;
  5387. TBL_PC* sd = script_rid2sd(st);
  5388. if (!sd) {
  5389. script_pushint(st,0);
  5390. return 0;
  5391. }
  5392. data = script_getdata(st,2);
  5393. get_val(st, data); // convert into value in case of a variable
  5394. if( data_isstring(data) )
  5395. {// item name
  5396. id = itemdb_searchname(conv_str(st, data));
  5397. }
  5398. else
  5399. {// item id
  5400. id = itemdb_exists(conv_num(st, data));
  5401. }
  5402. if( id == NULL )
  5403. {
  5404. ShowError("buildin_countitem2: Invalid item '%s'.\n", script_getstr(st,2)); // returns string, regardless of what it was
  5405. script_pushint(st,0);
  5406. return 1;
  5407. }
  5408. nameid = id->nameid;
  5409. iden = script_getnum(st,3);
  5410. ref = script_getnum(st,4);
  5411. attr = script_getnum(st,5);
  5412. c1 = (short)script_getnum(st,6);
  5413. c2 = (short)script_getnum(st,7);
  5414. c3 = (short)script_getnum(st,8);
  5415. c4 = (short)script_getnum(st,9);
  5416. for(i = 0; i < MAX_INVENTORY; i++)
  5417. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5418. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5419. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5420. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5421. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5422. sd->status.inventory[i].card[3] == c4
  5423. )
  5424. count += sd->status.inventory[i].amount;
  5425. script_pushint(st,count);
  5426. return 0;
  5427. }
  5428. /*==========================================
  5429. * Check if item with this amount can fit in inventory
  5430. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5431. * Return
  5432. * 0 : fail
  5433. * 1 : success (npc side only)
  5434. *------------------------------------------*/
  5435. BUILDIN_FUNC(checkweight)
  5436. {
  5437. int nameid, amount, slots, amount2=0;
  5438. unsigned int weight=0, i, nbargs;
  5439. struct item_data* id = NULL;
  5440. struct map_session_data* sd;
  5441. struct script_data* data;
  5442. if( ( sd = script_rid2sd(st) ) == NULL ){
  5443. return 0;
  5444. }
  5445. nbargs = script_lastdata(st)+1;
  5446. if(nbargs%2){
  5447. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5448. script_pushint(st,0);
  5449. return 1;
  5450. }
  5451. slots = pc_inventoryblank(sd); //nb of empty slot
  5452. for(i=2; i<nbargs; i=i+2){
  5453. data = script_getdata(st,i);
  5454. get_val(st, data); // convert into value in case of a variable
  5455. if( data_isstring(data) ){// item name
  5456. id = itemdb_searchname(conv_str(st, data));
  5457. } else {// item id
  5458. id = itemdb_exists(conv_num(st, data));
  5459. }
  5460. if( id == NULL ) {
  5461. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5462. script_pushint(st,0);
  5463. return 1;
  5464. }
  5465. nameid = id->nameid;
  5466. amount = script_getnum(st,i+1);
  5467. if( amount < 1 ) {
  5468. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5469. script_pushint(st,0);
  5470. return 1;
  5471. }
  5472. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5473. if( weight + sd->weight > sd->max_weight )
  5474. {// too heavy
  5475. script_pushint(st,0);
  5476. return 0;
  5477. }
  5478. switch( pc_checkadditem(sd, nameid, amount) )
  5479. {
  5480. case CHKADDITEM_EXIST:
  5481. // item is already in inventory, but there is still space for the requested amount
  5482. break;
  5483. case CHKADDITEM_NEW:
  5484. if( itemdb_isstackable(nameid) ) {// stackable
  5485. amount2++;
  5486. if( slots < amount2 ) {
  5487. script_pushint(st,0);
  5488. return 0;
  5489. }
  5490. }
  5491. else {// non-stackable
  5492. amount2 += amount;
  5493. if( slots < amount2){
  5494. script_pushint(st,0);
  5495. return 0;
  5496. }
  5497. }
  5498. break;
  5499. case CHKADDITEM_OVERAMOUNT:
  5500. script_pushint(st,0);
  5501. return 0;
  5502. }
  5503. }
  5504. script_pushint(st,1);
  5505. return 0;
  5506. }
  5507. BUILDIN_FUNC(checkweight2)
  5508. {
  5509. //variable sub checkweight
  5510. int32 nameid=-1, amount=-1;
  5511. int i=0, amount2=0, slots=0, weight=0;
  5512. short fail=0;
  5513. //variable for array parsing
  5514. struct script_data* data_it;
  5515. struct script_data* data_nb;
  5516. const char* name_it;
  5517. const char* name_nb;
  5518. int32 id_it, id_nb;
  5519. int32 idx_it, idx_nb;
  5520. int nb_it, nb_nb; //array size
  5521. TBL_PC *sd = script_rid2sd(st);
  5522. nullpo_retr(1,sd);
  5523. data_it = script_getdata(st, 2);
  5524. data_nb = script_getdata(st, 3);
  5525. if( !data_isreference(data_it) || !data_isreference(data_nb))
  5526. {
  5527. ShowError("script:checkweight2: parameter not a variable\n");
  5528. script_pushint(st,0);
  5529. return 1;// not a variable
  5530. }
  5531. id_it = reference_getid(data_it);
  5532. id_nb = reference_getid(data_nb);
  5533. idx_it = reference_getindex(data_it);
  5534. idx_nb = reference_getindex(data_nb);
  5535. name_it = reference_getname(data_it);
  5536. name_nb = reference_getname(data_nb);
  5537. if( not_array_variable(*name_it) || not_array_variable(*name_nb))
  5538. {
  5539. ShowError("script:checkweight2: illegal scope\n");
  5540. script_pushint(st,0);
  5541. return 1;// not supported
  5542. }
  5543. if(is_string_variable(name_it) || is_string_variable(name_nb)){
  5544. ShowError("script:checkweight2: illegal type, need int\n");
  5545. script_pushint(st,0);
  5546. return 1;// not supported
  5547. }
  5548. nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it));
  5549. nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb));
  5550. if(nb_it != nb_nb){
  5551. ShowError("Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5552. fail = 1;
  5553. }
  5554. slots = pc_inventoryblank(sd);
  5555. for(i=0; i<nb_it; i++){
  5556. nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5557. script_removetop(st, -1, 0);
  5558. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5559. script_removetop(st, -1, 0);
  5560. if(fail) continue; //cpntonie to depop rest
  5561. if(itemdb_exists(nameid) == NULL ){
  5562. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  5563. fail=1;
  5564. continue;
  5565. }
  5566. if(amount < 0 ){
  5567. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  5568. fail = 1;
  5569. continue;
  5570. }
  5571. weight += itemdb_weight(nameid)*amount;
  5572. if( weight + sd->weight > sd->max_weight ){
  5573. fail = 1;
  5574. continue;
  5575. }
  5576. switch( pc_checkadditem(sd, nameid, amount) ) {
  5577. case CHKADDITEM_EXIST:
  5578. // item is already in inventory, but there is still space for the requested amount
  5579. break;
  5580. case CHKADDITEM_NEW:
  5581. if( itemdb_isstackable(nameid) ){// stackable
  5582. amount2++;
  5583. if( slots < amount2 )
  5584. fail = 1;
  5585. }
  5586. else {// non-stackable
  5587. amount2 += amount;
  5588. if( slots < amount2 ){
  5589. fail = 1;
  5590. }
  5591. }
  5592. break;
  5593. case CHKADDITEM_OVERAMOUNT:
  5594. fail = 1;
  5595. } //end switch
  5596. } //end loop DO NOT break it prematurly we need to depop all stack
  5597. fail?script_pushint(st,0):script_pushint(st,1);
  5598. return 0;
  5599. }
  5600. /*==========================================
  5601. * getitem <item id>,<amount>{,<account ID>};
  5602. * getitem "<item name>",<amount>{,<account ID>};
  5603. *------------------------------------------*/
  5604. BUILDIN_FUNC(getitem)
  5605. {
  5606. int nameid,amount,get_count,i,flag = 0;
  5607. struct item it;
  5608. TBL_PC *sd;
  5609. struct script_data *data;
  5610. data=script_getdata(st,2);
  5611. get_val(st,data);
  5612. if( data_isstring(data) )
  5613. {// "<item name>"
  5614. const char *name=conv_str(st,data);
  5615. struct item_data *item_data = itemdb_searchname(name);
  5616. if( item_data == NULL ){
  5617. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5618. return 1; //No item created.
  5619. }
  5620. nameid=item_data->nameid;
  5621. } else if( data_isint(data) )
  5622. {// <item id>
  5623. nameid=conv_num(st,data);
  5624. //Violet Box, Blue Box, etc - random item pick
  5625. if( nameid < 0 ) {
  5626. nameid = -nameid;
  5627. flag = 1;
  5628. }
  5629. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5630. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5631. return 1; //No item created.
  5632. }
  5633. } else {
  5634. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5635. return 1;
  5636. }
  5637. // <amount>
  5638. if( (amount=script_getnum(st,3)) <= 0)
  5639. return 0; //return if amount <=0, skip the useles iteration
  5640. memset(&it,0,sizeof(it));
  5641. it.nameid=nameid;
  5642. if(!flag)
  5643. it.identify=1;
  5644. else
  5645. it.identify=itemdb_isidentified(nameid);
  5646. if( script_hasdata(st,4) )
  5647. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5648. else
  5649. sd=script_rid2sd(st); // Attached player
  5650. if( sd == NULL ) // no target
  5651. return 0;
  5652. //Check if it's stackable.
  5653. if (!itemdb_isstackable(nameid))
  5654. get_count = 1;
  5655. else
  5656. get_count = amount;
  5657. for (i = 0; i < amount; i += get_count)
  5658. {
  5659. // if not pet egg
  5660. if (!pet_create_egg(sd, nameid))
  5661. {
  5662. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5663. {
  5664. clif_additem(sd, 0, 0, flag);
  5665. if( pc_candrop(sd,&it) )
  5666. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5667. }
  5668. }
  5669. }
  5670. return 0;
  5671. }
  5672. /*==========================================
  5673. *
  5674. *------------------------------------------*/
  5675. BUILDIN_FUNC(getitem2)
  5676. {
  5677. int nameid,amount,get_count,i,flag = 0;
  5678. int iden,ref,attr,c1,c2,c3,c4;
  5679. struct item_data *item_data;
  5680. struct item item_tmp;
  5681. TBL_PC *sd;
  5682. struct script_data *data;
  5683. if( script_hasdata(st,11) )
  5684. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5685. else
  5686. sd=script_rid2sd(st); // Attached player
  5687. if( sd == NULL ) // no target
  5688. return 0;
  5689. data=script_getdata(st,2);
  5690. get_val(st,data);
  5691. if( data_isstring(data) ){
  5692. const char *name=conv_str(st,data);
  5693. struct item_data *item_data = itemdb_searchname(name);
  5694. if( item_data )
  5695. nameid=item_data->nameid;
  5696. else
  5697. nameid=UNKNOWN_ITEM_ID;
  5698. }else
  5699. nameid=conv_num(st,data);
  5700. amount=script_getnum(st,3);
  5701. iden=script_getnum(st,4);
  5702. ref=script_getnum(st,5);
  5703. attr=script_getnum(st,6);
  5704. c1=(short)script_getnum(st,7);
  5705. c2=(short)script_getnum(st,8);
  5706. c3=(short)script_getnum(st,9);
  5707. c4=(short)script_getnum(st,10);
  5708. if(nameid<0) { // Invalide nameid
  5709. nameid = -nameid;
  5710. flag = 1;
  5711. }
  5712. if(nameid > 0) {
  5713. memset(&item_tmp,0,sizeof(item_tmp));
  5714. item_data=itemdb_exists(nameid);
  5715. if (item_data == NULL)
  5716. return -1;
  5717. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5718. if(ref > MAX_REFINE) ref = MAX_REFINE;
  5719. }
  5720. else if(item_data->type==IT_PETEGG) {
  5721. iden = 1;
  5722. ref = 0;
  5723. }
  5724. else {
  5725. iden = 1;
  5726. ref = attr = 0;
  5727. }
  5728. item_tmp.nameid=nameid;
  5729. if(!flag)
  5730. item_tmp.identify=iden;
  5731. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5732. item_tmp.identify=0;
  5733. item_tmp.refine=ref;
  5734. item_tmp.attribute=attr;
  5735. item_tmp.card[0]=(short)c1;
  5736. item_tmp.card[1]=(short)c2;
  5737. item_tmp.card[2]=(short)c3;
  5738. item_tmp.card[3]=(short)c4;
  5739. //Check if it's stackable.
  5740. if (!itemdb_isstackable(nameid))
  5741. get_count = 1;
  5742. else
  5743. get_count = amount;
  5744. for (i = 0; i < amount; i += get_count)
  5745. {
  5746. // if not pet egg
  5747. if (!pet_create_egg(sd, nameid))
  5748. {
  5749. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5750. {
  5751. clif_additem(sd, 0, 0, flag);
  5752. if( pc_candrop(sd,&item_tmp) )
  5753. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5754. }
  5755. }
  5756. }
  5757. }
  5758. return 0;
  5759. }
  5760. /*==========================================
  5761. * rentitem <item id>,<seconds>
  5762. * rentitem "<item name>",<seconds>
  5763. *------------------------------------------*/
  5764. BUILDIN_FUNC(rentitem)
  5765. {
  5766. struct map_session_data *sd;
  5767. struct script_data *data;
  5768. struct item it;
  5769. int seconds;
  5770. int nameid = 0, flag;
  5771. data = script_getdata(st,2);
  5772. get_val(st,data);
  5773. if( (sd = script_rid2sd(st)) == NULL )
  5774. return 0;
  5775. if( data_isstring(data) )
  5776. {
  5777. const char *name = conv_str(st,data);
  5778. struct item_data *itd = itemdb_searchname(name);
  5779. if( itd == NULL )
  5780. {
  5781. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5782. return 1;
  5783. }
  5784. nameid = itd->nameid;
  5785. }
  5786. else if( data_isint(data) )
  5787. {
  5788. nameid = conv_num(st,data);
  5789. if( nameid <= 0 || !itemdb_exists(nameid) )
  5790. {
  5791. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  5792. return 1;
  5793. }
  5794. }
  5795. else
  5796. {
  5797. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5798. return 1;
  5799. }
  5800. seconds = script_getnum(st,3);
  5801. memset(&it, 0, sizeof(it));
  5802. it.nameid = nameid;
  5803. it.identify = 1;
  5804. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5805. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5806. {
  5807. clif_additem(sd, 0, 0, flag);
  5808. return 1;
  5809. }
  5810. return 0;
  5811. }
  5812. /*==========================================
  5813. * gets an item with someone's name inscribed [Skotlex]
  5814. * getinscribeditem item_num, character_name
  5815. * Returned Qty is always 1, only works on equip-able
  5816. * equipment
  5817. *------------------------------------------*/
  5818. BUILDIN_FUNC(getnameditem)
  5819. {
  5820. int nameid;
  5821. struct item item_tmp;
  5822. TBL_PC *sd, *tsd;
  5823. struct script_data *data;
  5824. sd = script_rid2sd(st);
  5825. if (sd == NULL)
  5826. { //Player not attached!
  5827. script_pushint(st,0);
  5828. return 0;
  5829. }
  5830. data=script_getdata(st,2);
  5831. get_val(st,data);
  5832. if( data_isstring(data) ){
  5833. const char *name=conv_str(st,data);
  5834. struct item_data *item_data = itemdb_searchname(name);
  5835. if( item_data == NULL)
  5836. { //Failed
  5837. script_pushint(st,0);
  5838. return 0;
  5839. }
  5840. nameid = item_data->nameid;
  5841. }else
  5842. nameid = conv_num(st,data);
  5843. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5844. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5845. script_pushint(st,0);
  5846. return 0;
  5847. }
  5848. data=script_getdata(st,3);
  5849. get_val(st,data);
  5850. if( data_isstring(data) ) //Char Name
  5851. tsd=map_nick2sd(conv_str(st,data));
  5852. else //Char Id was given
  5853. tsd=map_charid2sd(conv_num(st,data));
  5854. if( tsd == NULL )
  5855. { //Failed
  5856. script_pushint(st,0);
  5857. return 0;
  5858. }
  5859. memset(&item_tmp,0,sizeof(item_tmp));
  5860. item_tmp.nameid=nameid;
  5861. item_tmp.amount=1;
  5862. item_tmp.identify=1;
  5863. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5864. item_tmp.card[2]=tsd->status.char_id;
  5865. item_tmp.card[3]=tsd->status.char_id >> 16;
  5866. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  5867. script_pushint(st,0);
  5868. return 0; //Failed to add item, we will not drop if they don't fit
  5869. }
  5870. script_pushint(st,1);
  5871. return 0;
  5872. }
  5873. /*==========================================
  5874. * gets a random item ID from an item group [Skotlex]
  5875. * groupranditem group_num
  5876. *------------------------------------------*/
  5877. BUILDIN_FUNC(grouprandomitem)
  5878. {
  5879. int group;
  5880. group = script_getnum(st,2);
  5881. script_pushint(st,itemdb_searchrandomid(group));
  5882. return 0;
  5883. }
  5884. /*==========================================
  5885. *
  5886. *------------------------------------------*/
  5887. BUILDIN_FUNC(makeitem)
  5888. {
  5889. int nameid,amount,flag = 0;
  5890. int x,y,m;
  5891. const char *mapname;
  5892. struct item item_tmp;
  5893. struct script_data *data;
  5894. data=script_getdata(st,2);
  5895. get_val(st,data);
  5896. if( data_isstring(data) ){
  5897. const char *name=conv_str(st,data);
  5898. struct item_data *item_data = itemdb_searchname(name);
  5899. if( item_data )
  5900. nameid=item_data->nameid;
  5901. else
  5902. nameid=UNKNOWN_ITEM_ID;
  5903. }else
  5904. nameid=conv_num(st,data);
  5905. amount=script_getnum(st,3);
  5906. mapname =script_getstr(st,4);
  5907. x =script_getnum(st,5);
  5908. y =script_getnum(st,6);
  5909. if(strcmp(mapname,"this")==0)
  5910. {
  5911. TBL_PC *sd;
  5912. sd = script_rid2sd(st);
  5913. if (!sd) return 0; //Failed...
  5914. m=sd->bl.m;
  5915. } else
  5916. m=map_mapname2mapid(mapname);
  5917. if(nameid<0) {
  5918. nameid = -nameid;
  5919. flag = 1;
  5920. }
  5921. if(nameid > 0) {
  5922. memset(&item_tmp,0,sizeof(item_tmp));
  5923. item_tmp.nameid=nameid;
  5924. if(!flag)
  5925. item_tmp.identify=1;
  5926. else
  5927. item_tmp.identify=itemdb_isidentified(nameid);
  5928. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5929. }
  5930. return 0;
  5931. }
  5932. /// Counts / deletes the current item given by idx.
  5933. /// Used by buildin_delitem_search
  5934. /// Relies on all input data being already fully valid.
  5935. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, bool delete_items)
  5936. {
  5937. int delamount;
  5938. struct item* inv = &sd->status.inventory[idx];
  5939. delamount = ( amount[0] < inv->amount ) ? amount[0] : inv->amount;
  5940. if( delete_items )
  5941. {
  5942. if( sd->inventory_data[idx]->type == IT_PETEGG && inv->card[0] == CARD0_PET )
  5943. {// delete associated pet
  5944. intif_delete_petdata(MakeDWord(inv->card[1], inv->card[2]));
  5945. }
  5946. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  5947. }
  5948. amount[0]-= delamount;
  5949. }
  5950. /// Searches for item(s) and checks, if there is enough of them.
  5951. /// Used by delitem and delitem2
  5952. /// Relies on all input data being already fully valid.
  5953. /// @param exact_match will also match item attributes and cards, not just name id
  5954. /// @return true when all items could be deleted, false when there were not enough items to delete
  5955. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match)
  5956. {
  5957. bool delete_items = false;
  5958. int i, amount, important;
  5959. struct item* inv;
  5960. // prefer always non-equipped items
  5961. it->equip = 0;
  5962. // when searching for nameid only, prefer additionally
  5963. if( !exact_match )
  5964. {
  5965. // non-refined items
  5966. it->refine = 0;
  5967. // card-less items
  5968. memset(it->card, 0, sizeof(it->card));
  5969. }
  5970. for(;;)
  5971. {
  5972. amount = it->amount;
  5973. important = 0;
  5974. // 1st pass -- less important items / exact match
  5975. for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  5976. {
  5977. inv = &sd->status.inventory[i];
  5978. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  5979. {// wrong/invalid item
  5980. continue;
  5981. }
  5982. if( inv->equip != it->equip || inv->refine != it->refine )
  5983. {// not matching attributes
  5984. important++;
  5985. continue;
  5986. }
  5987. if( exact_match )
  5988. {
  5989. if( inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  5990. {// not matching exact attributes
  5991. continue;
  5992. }
  5993. }
  5994. else
  5995. {
  5996. if( sd->inventory_data[i]->type == IT_PETEGG )
  5997. {
  5998. if( inv->card[0] == CARD0_PET && CheckForCharServer() )
  5999. {// pet which cannot be deleted
  6000. continue;
  6001. }
  6002. }
  6003. else if( memcmp(inv->card, it->card, sizeof(inv->card)) )
  6004. {// named/carded item
  6005. important++;
  6006. continue;
  6007. }
  6008. }
  6009. // count / delete item
  6010. buildin_delitem_delete(sd, i, &amount, delete_items);
  6011. }
  6012. // 2nd pass -- any matching item
  6013. if( amount == 0 || important == 0 )
  6014. {// either everything was already consumed or no items were skipped
  6015. ;
  6016. }
  6017. else for( i = 0; amount && i < ARRAYLENGTH(sd->status.inventory); i++ )
  6018. {
  6019. inv = &sd->status.inventory[i];
  6020. if( !inv->nameid || !sd->inventory_data[i] || inv->nameid != it->nameid )
  6021. {// wrong/invalid item
  6022. continue;
  6023. }
  6024. if( sd->inventory_data[i]->type == IT_PETEGG && inv->card[0] == CARD0_PET && CheckForCharServer() )
  6025. {// pet which cannot be deleted
  6026. continue;
  6027. }
  6028. if( exact_match )
  6029. {
  6030. if( inv->refine != it->refine || inv->identify != it->identify || inv->attribute != it->attribute || memcmp(inv->card, it->card, sizeof(inv->card)) )
  6031. {// not matching attributes
  6032. continue;
  6033. }
  6034. }
  6035. // count / delete item
  6036. buildin_delitem_delete(sd, i, &amount, delete_items);
  6037. }
  6038. if( amount )
  6039. {// not enough items
  6040. return false;
  6041. }
  6042. else if( delete_items )
  6043. {// we are done with the work
  6044. return true;
  6045. }
  6046. else
  6047. {// get rid of the items now
  6048. delete_items = true;
  6049. }
  6050. }
  6051. }
  6052. /// Deletes items from the target/attached player.
  6053. /// Prioritizes ordinary items.
  6054. ///
  6055. /// delitem <item id>,<amount>{,<account id>}
  6056. /// delitem "<item name>",<amount>{,<account id>}
  6057. BUILDIN_FUNC(delitem)
  6058. {
  6059. TBL_PC *sd;
  6060. struct item it;
  6061. struct script_data *data;
  6062. if( script_hasdata(st,4) )
  6063. {
  6064. int account_id = script_getnum(st,4);
  6065. sd = map_id2sd(account_id); // <account id>
  6066. if( sd == NULL )
  6067. {
  6068. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  6069. st->state = END;
  6070. return 1;
  6071. }
  6072. }
  6073. else
  6074. {
  6075. sd = script_rid2sd(st);// attached player
  6076. if( sd == NULL )
  6077. return 0;
  6078. }
  6079. data = script_getdata(st,2);
  6080. get_val(st,data);
  6081. if( data_isstring(data) )
  6082. {
  6083. const char* item_name = conv_str(st,data);
  6084. struct item_data* id = itemdb_searchname(item_name);
  6085. if( id == NULL )
  6086. {
  6087. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  6088. st->state = END;
  6089. return 1;
  6090. }
  6091. it.nameid = id->nameid;// "<item name>"
  6092. }
  6093. else
  6094. {
  6095. it.nameid = conv_num(st,data);// <item id>
  6096. if( !itemdb_exists( it.nameid ) )
  6097. {
  6098. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6099. st->state = END;
  6100. return 1;
  6101. }
  6102. }
  6103. it.amount=script_getnum(st,3);
  6104. if( it.amount <= 0 )
  6105. return 0;// nothing to do
  6106. if( buildin_delitem_search(sd, &it, false) )
  6107. {// success
  6108. return 0;
  6109. }
  6110. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6111. st->state = END;
  6112. clif_scriptclose(sd, st->oid);
  6113. return 1;
  6114. }
  6115. /// Deletes items from the target/attached player.
  6116. ///
  6117. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6118. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6119. BUILDIN_FUNC(delitem2)
  6120. {
  6121. TBL_PC *sd;
  6122. struct item it;
  6123. struct script_data *data;
  6124. if( script_hasdata(st,11) )
  6125. {
  6126. int account_id = script_getnum(st,11);
  6127. sd = map_id2sd(account_id); // <account id>
  6128. if( sd == NULL )
  6129. {
  6130. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  6131. st->state = END;
  6132. return 1;
  6133. }
  6134. }
  6135. else
  6136. {
  6137. sd = script_rid2sd(st);// attached player
  6138. if( sd == NULL )
  6139. return 0;
  6140. }
  6141. data = script_getdata(st,2);
  6142. get_val(st,data);
  6143. if( data_isstring(data) )
  6144. {
  6145. const char* item_name = conv_str(st,data);
  6146. struct item_data* id = itemdb_searchname(item_name);
  6147. if( id == NULL )
  6148. {
  6149. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  6150. st->state = END;
  6151. return 1;
  6152. }
  6153. it.nameid = id->nameid;// "<item name>"
  6154. }
  6155. else
  6156. {
  6157. it.nameid = conv_num(st,data);// <item id>
  6158. if( !itemdb_exists( it.nameid ) )
  6159. {
  6160. ShowError("script:delitem: unknown item \"%d\".\n", it.nameid);
  6161. st->state = END;
  6162. return 1;
  6163. }
  6164. }
  6165. it.amount=script_getnum(st,3);
  6166. it.identify=script_getnum(st,4);
  6167. it.refine=script_getnum(st,5);
  6168. it.attribute=script_getnum(st,6);
  6169. it.card[0]=(short)script_getnum(st,7);
  6170. it.card[1]=(short)script_getnum(st,8);
  6171. it.card[2]=(short)script_getnum(st,9);
  6172. it.card[3]=(short)script_getnum(st,10);
  6173. if( it.amount <= 0 )
  6174. return 0;// nothing to do
  6175. if( buildin_delitem_search(sd, &it, true) )
  6176. {// success
  6177. return 0;
  6178. }
  6179. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", it.amount, sd->status.account_id, it.nameid);
  6180. st->state = END;
  6181. clif_scriptclose(sd, st->oid);
  6182. return 1;
  6183. }
  6184. /*==========================================
  6185. * Enables/Disables use of items while in an NPC [Skotlex]
  6186. *------------------------------------------*/
  6187. BUILDIN_FUNC(enableitemuse)
  6188. {
  6189. TBL_PC *sd;
  6190. sd=script_rid2sd(st);
  6191. if (sd)
  6192. st->npc_item_flag = sd->npc_item_flag = 1;
  6193. return 0;
  6194. }
  6195. BUILDIN_FUNC(disableitemuse)
  6196. {
  6197. TBL_PC *sd;
  6198. sd=script_rid2sd(st);
  6199. if (sd)
  6200. st->npc_item_flag = sd->npc_item_flag = 0;
  6201. return 0;
  6202. }
  6203. /*==========================================
  6204. * return the basic stats of sd
  6205. * chk pc_readparam for available type
  6206. *------------------------------------------*/
  6207. BUILDIN_FUNC(readparam)
  6208. {
  6209. int type;
  6210. TBL_PC *sd;
  6211. type=script_getnum(st,2);
  6212. if( script_hasdata(st,3) )
  6213. sd=map_nick2sd(script_getstr(st,3));
  6214. else
  6215. sd=script_rid2sd(st);
  6216. if(sd==NULL){
  6217. script_pushint(st,-1);
  6218. return 0;
  6219. }
  6220. script_pushint(st,pc_readparam(sd,type));
  6221. return 0;
  6222. }
  6223. /*==========================================
  6224. * Return charid identification
  6225. * return by @num :
  6226. * 0 : char_id
  6227. * 1 : party_id
  6228. * 2 : guild_id
  6229. * 3 : account_id
  6230. * 4 : bg_id
  6231. *------------------------------------------*/
  6232. BUILDIN_FUNC(getcharid)
  6233. {
  6234. int num;
  6235. TBL_PC *sd;
  6236. num = script_getnum(st,2);
  6237. if( script_hasdata(st,3) )
  6238. sd=map_nick2sd(script_getstr(st,3));
  6239. else
  6240. sd=script_rid2sd(st);
  6241. if(sd==NULL){
  6242. script_pushint(st,0); //return 0, according docs
  6243. return 0;
  6244. }
  6245. switch( num ) {
  6246. case 0: script_pushint(st,sd->status.char_id); break;
  6247. case 1: script_pushint(st,sd->status.party_id); break;
  6248. case 2: script_pushint(st,sd->status.guild_id); break;
  6249. case 3: script_pushint(st,sd->status.account_id); break;
  6250. case 4: script_pushint(st,sd->bg_id); break;
  6251. default:
  6252. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6253. script_pushint(st,0);
  6254. break;
  6255. }
  6256. return 0;
  6257. }
  6258. /*==========================================
  6259. * returns the GID of an NPC
  6260. *------------------------------------------*/
  6261. BUILDIN_FUNC(getnpcid)
  6262. {
  6263. int num = script_getnum(st,2);
  6264. struct npc_data* nd = NULL;
  6265. if( script_hasdata(st,3) )
  6266. {// unique npc name
  6267. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6268. {
  6269. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6270. script_pushint(st,0);
  6271. return 1;
  6272. }
  6273. }
  6274. switch (num) {
  6275. case 0:
  6276. script_pushint(st,nd ? nd->bl.id : st->oid);
  6277. break;
  6278. default:
  6279. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6280. script_pushint(st,0);
  6281. return 1;
  6282. }
  6283. return 0;
  6284. }
  6285. /*==========================================
  6286. * Return the name of the party_id
  6287. * null if not found
  6288. *------------------------------------------*/
  6289. BUILDIN_FUNC(getpartyname)
  6290. {
  6291. int party_id;
  6292. struct party_data* p;
  6293. party_id = script_getnum(st,2);
  6294. if( ( p = party_search(party_id) ) != NULL )
  6295. {
  6296. script_pushstrcopy(st,p->party.name);
  6297. }
  6298. else
  6299. {
  6300. script_pushconststr(st,"null");
  6301. }
  6302. return 0;
  6303. }
  6304. /*==========================================
  6305. * Get the information of the members of a party by type
  6306. * @party_id, @type
  6307. * return by @type :
  6308. * - : nom des membres
  6309. * 1 : char_id des membres
  6310. * 2 : account_id des membres
  6311. *------------------------------------------*/
  6312. BUILDIN_FUNC(getpartymember)
  6313. {
  6314. struct party_data *p;
  6315. int i,j=0,type=0;
  6316. p=party_search(script_getnum(st,2));
  6317. if( script_hasdata(st,3) )
  6318. type=script_getnum(st,3);
  6319. if(p!=NULL){
  6320. for(i=0;i<MAX_PARTY;i++){
  6321. if(p->party.member[i].account_id){
  6322. switch (type) {
  6323. case 2:
  6324. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6325. break;
  6326. case 1:
  6327. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6328. break;
  6329. default:
  6330. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6331. }
  6332. j++;
  6333. }
  6334. }
  6335. }
  6336. mapreg_setreg(add_str("$@partymembercount"),j);
  6337. return 0;
  6338. }
  6339. /*==========================================
  6340. * Retrieves party leader. if flag is specified,
  6341. * return some of the leader data. Otherwise, return name.
  6342. *------------------------------------------*/
  6343. BUILDIN_FUNC(getpartyleader)
  6344. {
  6345. int party_id, type = 0, i=0;
  6346. struct party_data *p;
  6347. party_id=script_getnum(st,2);
  6348. if( script_hasdata(st,3) )
  6349. type=script_getnum(st,3);
  6350. p=party_search(party_id);
  6351. if (p) //Search leader
  6352. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6353. if (!p || i == MAX_PARTY) { //leader not found
  6354. if (type)
  6355. script_pushint(st,-1);
  6356. else
  6357. script_pushconststr(st,"null");
  6358. return 0;
  6359. }
  6360. switch (type) {
  6361. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6362. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6363. case 3: script_pushint(st,p->party.member[i].class_); break;
  6364. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6365. case 5: script_pushint(st,p->party.member[i].lv); break;
  6366. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6367. }
  6368. return 0;
  6369. }
  6370. /*==========================================
  6371. * Return the name of the @guild_id
  6372. * null if not found
  6373. *------------------------------------------*/
  6374. BUILDIN_FUNC(getguildname)
  6375. {
  6376. int guild_id;
  6377. struct guild* g;
  6378. guild_id = script_getnum(st,2);
  6379. if( ( g = guild_search(guild_id) ) != NULL )
  6380. {
  6381. script_pushstrcopy(st,g->name);
  6382. }
  6383. else
  6384. {
  6385. script_pushconststr(st,"null");
  6386. }
  6387. return 0;
  6388. }
  6389. /*==========================================
  6390. * Return the name of the guild master of @guild_id
  6391. * null if not found
  6392. *------------------------------------------*/
  6393. BUILDIN_FUNC(getguildmaster)
  6394. {
  6395. int guild_id;
  6396. struct guild* g;
  6397. guild_id = script_getnum(st,2);
  6398. if( ( g = guild_search(guild_id) ) != NULL )
  6399. {
  6400. script_pushstrcopy(st,g->member[0].name);
  6401. }
  6402. else
  6403. {
  6404. script_pushconststr(st,"null");
  6405. }
  6406. return 0;
  6407. }
  6408. BUILDIN_FUNC(getguildmasterid)
  6409. {
  6410. int guild_id;
  6411. struct guild* g;
  6412. guild_id = script_getnum(st,2);
  6413. if( ( g = guild_search(guild_id) ) != NULL )
  6414. {
  6415. script_pushint(st,g->member[0].char_id);
  6416. }
  6417. else
  6418. {
  6419. script_pushint(st,0);
  6420. }
  6421. return 0;
  6422. }
  6423. /*==========================================
  6424. * Get char string information by type :
  6425. * Return by @type :
  6426. * 0 : char_name
  6427. * 1 : party_name or ""
  6428. * 2 : guild_name or ""
  6429. * 3 : map_name
  6430. * - : ""
  6431. *------------------------------------------*/
  6432. BUILDIN_FUNC(strcharinfo)
  6433. {
  6434. TBL_PC *sd;
  6435. int num;
  6436. struct guild* g;
  6437. struct party_data* p;
  6438. sd=script_rid2sd(st);
  6439. if (!sd) { //Avoid crashing....
  6440. script_pushconststr(st,"");
  6441. return 0;
  6442. }
  6443. num=script_getnum(st,2);
  6444. switch(num){
  6445. case 0:
  6446. script_pushstrcopy(st,sd->status.name);
  6447. break;
  6448. case 1:
  6449. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  6450. script_pushstrcopy(st,p->party.name);
  6451. } else {
  6452. script_pushconststr(st,"");
  6453. }
  6454. break;
  6455. case 2:
  6456. if( ( g = sd->guild ) != NULL ) {
  6457. script_pushstrcopy(st,g->name);
  6458. } else {
  6459. script_pushconststr(st,"");
  6460. }
  6461. break;
  6462. case 3:
  6463. script_pushconststr(st,map[sd->bl.m].name);
  6464. break;
  6465. default:
  6466. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6467. script_pushconststr(st,"");
  6468. break;
  6469. }
  6470. return 0;
  6471. }
  6472. /*==========================================
  6473. * Get npc string information by type
  6474. * return by @type:
  6475. * 0 : name
  6476. * 1 : str#
  6477. * 2 : #str
  6478. * 3 : ::str
  6479. * 4 : map name
  6480. *------------------------------------------*/
  6481. BUILDIN_FUNC(strnpcinfo)
  6482. {
  6483. TBL_NPC* nd;
  6484. int num;
  6485. char *buf,*name=NULL;
  6486. nd = map_id2nd(st->oid);
  6487. if (!nd) {
  6488. script_pushconststr(st, "");
  6489. return 0;
  6490. }
  6491. num = script_getnum(st,2);
  6492. switch(num){
  6493. case 0: // display name
  6494. name = aStrdup(nd->name);
  6495. break;
  6496. case 1: // visible part of display name
  6497. if((buf = strchr(nd->name,'#')) != NULL)
  6498. {
  6499. name = aStrdup(nd->name);
  6500. name[buf - nd->name] = 0;
  6501. } else // Return the name, there is no '#' present
  6502. name = aStrdup(nd->name);
  6503. break;
  6504. case 2: // # fragment
  6505. if((buf = strchr(nd->name,'#')) != NULL)
  6506. name = aStrdup(buf+1);
  6507. break;
  6508. case 3: // unique name
  6509. name = aStrdup(nd->exname);
  6510. break;
  6511. case 4: // map name
  6512. name = aStrdup(map[nd->bl.m].name);
  6513. break;
  6514. }
  6515. if(name)
  6516. script_pushstr(st, name);
  6517. else
  6518. script_pushconststr(st, "");
  6519. return 0;
  6520. }
  6521. // aegis->athena slot position conversion table
  6522. 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};
  6523. /*==========================================
  6524. * GetEquipID(Pos); Pos: 1-14
  6525. *------------------------------------------*/
  6526. BUILDIN_FUNC(getequipid)
  6527. {
  6528. int i, num;
  6529. TBL_PC* sd;
  6530. struct item_data* item;
  6531. sd = script_rid2sd(st);
  6532. if( sd == NULL )
  6533. return 0;
  6534. num = script_getnum(st,2) - 1;
  6535. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6536. {
  6537. script_pushint(st,-1);
  6538. return 0;
  6539. }
  6540. // get inventory position of item
  6541. i = pc_checkequip(sd,equip[num]);
  6542. if( i < 0 )
  6543. {
  6544. script_pushint(st,-1);
  6545. return 0;
  6546. }
  6547. item = sd->inventory_data[i];
  6548. if( item != 0 )
  6549. script_pushint(st,item->nameid);
  6550. else
  6551. script_pushint(st,0);
  6552. return 0;
  6553. }
  6554. /*==========================================
  6555. * Get the equipement name at pos
  6556. * return item jname or ""
  6557. *------------------------------------------*/
  6558. BUILDIN_FUNC(getequipname)
  6559. {
  6560. int i, num;
  6561. TBL_PC* sd;
  6562. struct item_data* item;
  6563. sd = script_rid2sd(st);
  6564. if( sd == NULL )
  6565. return 0;
  6566. num = script_getnum(st,2) - 1;
  6567. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6568. {
  6569. script_pushconststr(st,"");
  6570. return 0;
  6571. }
  6572. // get inventory position of item
  6573. i = pc_checkequip(sd,equip[num]);
  6574. if( i < 0 )
  6575. {
  6576. script_pushconststr(st,"");
  6577. return 0;
  6578. }
  6579. item = sd->inventory_data[i];
  6580. if( item != 0 )
  6581. script_pushstrcopy(st,item->jname);
  6582. else
  6583. script_pushconststr(st,"");
  6584. return 0;
  6585. }
  6586. /*==========================================
  6587. * getbrokenid [Valaris]
  6588. *------------------------------------------*/
  6589. BUILDIN_FUNC(getbrokenid)
  6590. {
  6591. int i,num,id=0,brokencounter=0;
  6592. TBL_PC *sd;
  6593. sd = script_rid2sd(st);
  6594. if( sd == NULL )
  6595. return 0;
  6596. num=script_getnum(st,2);
  6597. for(i=0; i<MAX_INVENTORY; i++) {
  6598. if(sd->status.inventory[i].attribute){
  6599. brokencounter++;
  6600. if(num==brokencounter){
  6601. id=sd->status.inventory[i].nameid;
  6602. break;
  6603. }
  6604. }
  6605. }
  6606. script_pushint(st,id);
  6607. return 0;
  6608. }
  6609. /*==========================================
  6610. * repair [Valaris]
  6611. *------------------------------------------*/
  6612. BUILDIN_FUNC(repair)
  6613. {
  6614. int i,num;
  6615. int repaircounter=0;
  6616. TBL_PC *sd;
  6617. sd = script_rid2sd(st);
  6618. if( sd == NULL )
  6619. return 0;
  6620. num=script_getnum(st,2);
  6621. for(i=0; i<MAX_INVENTORY; i++) {
  6622. if(sd->status.inventory[i].attribute){
  6623. repaircounter++;
  6624. if(num==repaircounter){
  6625. sd->status.inventory[i].attribute=0;
  6626. clif_equiplist(sd);
  6627. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6628. clif_misceffect(&sd->bl, 3);
  6629. break;
  6630. }
  6631. }
  6632. }
  6633. return 0;
  6634. }
  6635. /*==========================================
  6636. * repairall
  6637. *------------------------------------------*/
  6638. BUILDIN_FUNC(repairall)
  6639. {
  6640. int i, repaircounter = 0;
  6641. TBL_PC *sd;
  6642. sd = script_rid2sd(st);
  6643. if(sd == NULL)
  6644. return 0;
  6645. for(i = 0; i < MAX_INVENTORY; i++)
  6646. {
  6647. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  6648. {
  6649. sd->status.inventory[i].attribute = 0;
  6650. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  6651. repaircounter++;
  6652. }
  6653. }
  6654. if(repaircounter)
  6655. {
  6656. clif_misceffect(&sd->bl, 3);
  6657. clif_equiplist(sd);
  6658. }
  6659. return 0;
  6660. }
  6661. /*==========================================
  6662. * Chk if player have something equiped at pos
  6663. *------------------------------------------*/
  6664. BUILDIN_FUNC(getequipisequiped)
  6665. {
  6666. int i = -1,num;
  6667. TBL_PC *sd;
  6668. num = script_getnum(st,2);
  6669. sd = script_rid2sd(st);
  6670. if( sd == NULL )
  6671. return 0;
  6672. if (num > 0 && num <= ARRAYLENGTH(equip))
  6673. i=pc_checkequip(sd,equip[num-1]);
  6674. if(i >= 0)
  6675. script_pushint(st,1);
  6676. else
  6677. script_pushint(st,0);
  6678. return 0;
  6679. }
  6680. /*==========================================
  6681. * Chk if the player have something equiped at pos
  6682. * if so chk if this item ain't marked not refinable or rental
  6683. * return (npc)
  6684. * 1 : true
  6685. * 0 : false
  6686. *------------------------------------------*/
  6687. BUILDIN_FUNC(getequipisenableref)
  6688. {
  6689. int i = -1,num;
  6690. TBL_PC *sd;
  6691. num = script_getnum(st,2);
  6692. sd = script_rid2sd(st);
  6693. if( sd == NULL )
  6694. return 0;
  6695. if( num > 0 && num <= ARRAYLENGTH(equip) )
  6696. i = pc_checkequip(sd,equip[num-1]);
  6697. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  6698. script_pushint(st,1);
  6699. else
  6700. script_pushint(st,0);
  6701. return 0;
  6702. }
  6703. /*==========================================
  6704. * Chk if the item equiped at pos is identify (huh ?)
  6705. * return (npc)
  6706. * 1 : true
  6707. * 0 : false
  6708. *------------------------------------------*/
  6709. BUILDIN_FUNC(getequipisidentify)
  6710. {
  6711. int i = -1,num;
  6712. TBL_PC *sd;
  6713. num = script_getnum(st,2);
  6714. sd = script_rid2sd(st);
  6715. if( sd == NULL )
  6716. return 0;
  6717. if (num > 0 && num <= ARRAYLENGTH(equip))
  6718. i=pc_checkequip(sd,equip[num-1]);
  6719. if(i >= 0)
  6720. script_pushint(st,sd->status.inventory[i].identify);
  6721. else
  6722. script_pushint(st,0);
  6723. return 0;
  6724. }
  6725. /*==========================================
  6726. * Get the item refined value at pos
  6727. * return (npc)
  6728. * x : refine amount
  6729. * 0 : false (not refined)
  6730. *------------------------------------------*/
  6731. BUILDIN_FUNC(getequiprefinerycnt)
  6732. {
  6733. int i = -1,num;
  6734. TBL_PC *sd;
  6735. num = script_getnum(st,2);
  6736. sd = script_rid2sd(st);
  6737. if( sd == NULL )
  6738. return 0;
  6739. if (num > 0 && num <= ARRAYLENGTH(equip))
  6740. i=pc_checkequip(sd,equip[num-1]);
  6741. if(i >= 0)
  6742. script_pushint(st,sd->status.inventory[i].refine);
  6743. else
  6744. script_pushint(st,0);
  6745. return 0;
  6746. }
  6747. /*==========================================
  6748. * Get the weapon level value at pos
  6749. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  6750. * return (npc)
  6751. * x : weapon level
  6752. * 0 : false
  6753. *------------------------------------------*/
  6754. BUILDIN_FUNC(getequipweaponlv)
  6755. {
  6756. int i = -1,num;
  6757. TBL_PC *sd;
  6758. num = script_getnum(st,2);
  6759. sd = script_rid2sd(st);
  6760. if( sd == NULL )
  6761. return 0;
  6762. if (num > 0 && num <= ARRAYLENGTH(equip))
  6763. i=pc_checkequip(sd,equip[num-1]);
  6764. if(i >= 0 && sd->inventory_data[i])
  6765. script_pushint(st,sd->inventory_data[i]->wlv);
  6766. else
  6767. script_pushint(st,0);
  6768. return 0;
  6769. }
  6770. /*==========================================
  6771. * Get the item refine chance (from refine.txt) for item at pos
  6772. * return (npc)
  6773. * x : refine chance
  6774. * 0 : false (max refine level or unequip..)
  6775. *------------------------------------------*/
  6776. BUILDIN_FUNC(getequippercentrefinery)
  6777. {
  6778. int i = -1,num;
  6779. TBL_PC *sd;
  6780. num = script_getnum(st,2);
  6781. sd = script_rid2sd(st);
  6782. if( sd == NULL )
  6783. return 0;
  6784. if (num > 0 && num <= ARRAYLENGTH(equip))
  6785. i=pc_checkequip(sd,equip[num-1]);
  6786. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6787. script_pushint(st,status_get_refine_chance(itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  6788. else
  6789. script_pushint(st,0);
  6790. return 0;
  6791. }
  6792. /*==========================================
  6793. * Refine +1 item at pos and log and display refine
  6794. *------------------------------------------*/
  6795. BUILDIN_FUNC(successrefitem)
  6796. {
  6797. int i=-1,num,ep;
  6798. TBL_PC *sd;
  6799. num = script_getnum(st,2);
  6800. sd = script_rid2sd(st);
  6801. if( sd == NULL )
  6802. return 0;
  6803. if (num > 0 && num <= ARRAYLENGTH(equip))
  6804. i=pc_checkequip(sd,equip[num-1]);
  6805. if(i >= 0) {
  6806. ep=sd->status.inventory[i].equip;
  6807. //Logs items, got from (N)PC scripts [Lupus]
  6808. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6809. sd->status.inventory[i].refine++;
  6810. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6811. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6812. clif_delitem(sd,i,1,3);
  6813. //Logs items, got from (N)PC scripts [Lupus]
  6814. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6815. clif_additem(sd,i,1,0);
  6816. pc_equipitem(sd,i,ep);
  6817. clif_misceffect(&sd->bl,3);
  6818. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6819. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6820. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6821. ){ // Fame point system [DracoRPG]
  6822. switch (sd->inventory_data[i]->wlv){
  6823. case 1:
  6824. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6825. break;
  6826. case 2:
  6827. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6828. break;
  6829. case 3:
  6830. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6831. break;
  6832. }
  6833. }
  6834. }
  6835. return 0;
  6836. }
  6837. /*==========================================
  6838. * Show a failed Refine +1 attempt
  6839. *------------------------------------------*/
  6840. BUILDIN_FUNC(failedrefitem)
  6841. {
  6842. int i=-1,num;
  6843. TBL_PC *sd;
  6844. num = script_getnum(st,2);
  6845. sd = script_rid2sd(st);
  6846. if( sd == NULL )
  6847. return 0;
  6848. if (num > 0 && num <= ARRAYLENGTH(equip))
  6849. i=pc_checkequip(sd,equip[num-1]);
  6850. if(i >= 0) {
  6851. sd->status.inventory[i].refine = 0;
  6852. pc_unequipitem(sd,i,3); //recalculate bonus
  6853. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  6854. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6855. clif_misceffect(&sd->bl,2); // display failure effect
  6856. }
  6857. return 0;
  6858. }
  6859. /*==========================================
  6860. * Downgrades an Equipment Part by -1 . [Masao]
  6861. *------------------------------------------*/
  6862. BUILDIN_FUNC(downrefitem)
  6863. {
  6864. int i = -1,num,ep;
  6865. TBL_PC *sd;
  6866. num = script_getnum(st,2);
  6867. sd = script_rid2sd(st);
  6868. if( sd == NULL )
  6869. return 0;
  6870. if (num > 0 && num <= ARRAYLENGTH(equip))
  6871. i = pc_checkequip(sd,equip[num-1]);
  6872. if(i >= 0) {
  6873. ep = sd->status.inventory[i].equip;
  6874. //Logs items, got from (N)PC scripts [Lupus]
  6875. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  6876. sd->status.inventory[i].refine++;
  6877. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  6878. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine = sd->status.inventory[i].refine - 2);
  6879. clif_delitem(sd,i,1,3);
  6880. //Logs items, got from (N)PC scripts [Lupus]
  6881. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  6882. clif_additem(sd,i,1,0);
  6883. pc_equipitem(sd,i,ep);
  6884. clif_misceffect(&sd->bl,2);
  6885. }
  6886. return 0;
  6887. }
  6888. /*==========================================
  6889. * Delete the item equipped at pos.
  6890. *------------------------------------------*/
  6891. BUILDIN_FUNC(delequip)
  6892. {
  6893. int i=-1,num;
  6894. TBL_PC *sd;
  6895. num = script_getnum(st,2);
  6896. sd = script_rid2sd(st);
  6897. if( sd == NULL )
  6898. return 0;
  6899. if (num > 0 && num <= ARRAYLENGTH(equip))
  6900. i=pc_checkequip(sd,equip[num-1]);
  6901. if(i >= 0) {
  6902. pc_unequipitem(sd,i,3); //recalculate bonus
  6903. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  6904. }
  6905. return 0;
  6906. }
  6907. /*==========================================
  6908. *
  6909. *------------------------------------------*/
  6910. BUILDIN_FUNC(statusup)
  6911. {
  6912. int type;
  6913. TBL_PC *sd;
  6914. type=script_getnum(st,2);
  6915. sd = script_rid2sd(st);
  6916. if( sd == NULL )
  6917. return 0;
  6918. pc_statusup(sd,type);
  6919. return 0;
  6920. }
  6921. /*==========================================
  6922. *
  6923. *------------------------------------------*/
  6924. BUILDIN_FUNC(statusup2)
  6925. {
  6926. int type,val;
  6927. TBL_PC *sd;
  6928. type=script_getnum(st,2);
  6929. val=script_getnum(st,3);
  6930. sd = script_rid2sd(st);
  6931. if( sd == NULL )
  6932. return 0;
  6933. pc_statusup2(sd,type,val);
  6934. return 0;
  6935. }
  6936. /// See 'doc/item_bonus.txt'
  6937. ///
  6938. /// bonus <bonus type>,<val1>;
  6939. /// bonus2 <bonus type>,<val1>,<val2>;
  6940. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6941. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6942. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6943. BUILDIN_FUNC(bonus)
  6944. {
  6945. int type;
  6946. int val1;
  6947. int val2 = 0;
  6948. int val3 = 0;
  6949. int val4 = 0;
  6950. int val5 = 0;
  6951. TBL_PC* sd;
  6952. sd = script_rid2sd(st);
  6953. if( sd == NULL )
  6954. return 0; // no player attached
  6955. type = script_getnum(st,2);
  6956. switch( type ) {
  6957. case SP_AUTOSPELL:
  6958. case SP_AUTOSPELL_WHENHIT:
  6959. case SP_AUTOSPELL_ONSKILL:
  6960. case SP_SKILL_ATK:
  6961. case SP_SKILL_HEAL:
  6962. case SP_SKILL_HEAL2:
  6963. case SP_ADD_SKILL_BLOW:
  6964. case SP_CASTRATE:
  6965. case SP_ADDEFF_ONSKILL:
  6966. case SP_SKILL_USE_SP_RATE:
  6967. case SP_SKILL_COOLDOWN:
  6968. case SP_SKILL_FIXEDCAST:
  6969. case SP_SKILL_VARIABLECAST:
  6970. case SP_VARCASTRATE:
  6971. case SP_FIXCASTRATE:
  6972. case SP_SKILL_USE_SP:
  6973. // these bonuses support skill names
  6974. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6975. break;
  6976. default:
  6977. val1 = script_getnum(st,3);
  6978. break;
  6979. }
  6980. switch( script_lastdata(st)-2 ) {
  6981. case 1:
  6982. pc_bonus(sd, type, val1);
  6983. break;
  6984. case 2:
  6985. val2 = script_getnum(st,4);
  6986. pc_bonus2(sd, type, val1, val2);
  6987. break;
  6988. case 3:
  6989. val2 = script_getnum(st,4);
  6990. val3 = script_getnum(st,5);
  6991. pc_bonus3(sd, type, val1, val2, val3);
  6992. break;
  6993. case 4:
  6994. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  6995. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  6996. else
  6997. val2 = script_getnum(st,4);
  6998. val3 = script_getnum(st,5);
  6999. val4 = script_getnum(st,6);
  7000. pc_bonus4(sd, type, val1, val2, val3, val4);
  7001. break;
  7002. case 5:
  7003. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  7004. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7005. else
  7006. val2 = script_getnum(st,4);
  7007. val3 = script_getnum(st,5);
  7008. val4 = script_getnum(st,6);
  7009. val5 = script_getnum(st,7);
  7010. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7011. break;
  7012. default:
  7013. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7014. break;
  7015. }
  7016. return 0;
  7017. }
  7018. BUILDIN_FUNC(autobonus)
  7019. {
  7020. unsigned int dur;
  7021. short rate;
  7022. short atk_type = 0;
  7023. TBL_PC* sd;
  7024. const char *bonus_script, *other_script = NULL;
  7025. sd = script_rid2sd(st);
  7026. if( sd == NULL )
  7027. return 0; // no player attached
  7028. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7029. return 0;
  7030. rate = script_getnum(st,3);
  7031. dur = script_getnum(st,4);
  7032. bonus_script = script_getstr(st,2);
  7033. if( !rate || !dur || !bonus_script )
  7034. return 0;
  7035. if( script_hasdata(st,5) )
  7036. atk_type = script_getnum(st,5);
  7037. if( script_hasdata(st,6) )
  7038. other_script = script_getstr(st,6);
  7039. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7040. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7041. {
  7042. script_add_autobonus(bonus_script);
  7043. if( other_script )
  7044. script_add_autobonus(other_script);
  7045. }
  7046. return 0;
  7047. }
  7048. BUILDIN_FUNC(autobonus2)
  7049. {
  7050. unsigned int dur;
  7051. short rate;
  7052. short atk_type = 0;
  7053. TBL_PC* sd;
  7054. const char *bonus_script, *other_script = NULL;
  7055. sd = script_rid2sd(st);
  7056. if( sd == NULL )
  7057. return 0; // no player attached
  7058. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7059. return 0;
  7060. rate = script_getnum(st,3);
  7061. dur = script_getnum(st,4);
  7062. bonus_script = script_getstr(st,2);
  7063. if( !rate || !dur || !bonus_script )
  7064. return 0;
  7065. if( script_hasdata(st,5) )
  7066. atk_type = script_getnum(st,5);
  7067. if( script_hasdata(st,6) )
  7068. other_script = script_getstr(st,6);
  7069. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7070. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  7071. {
  7072. script_add_autobonus(bonus_script);
  7073. if( other_script )
  7074. script_add_autobonus(other_script);
  7075. }
  7076. return 0;
  7077. }
  7078. BUILDIN_FUNC(autobonus3)
  7079. {
  7080. unsigned int dur;
  7081. short rate,atk_type;
  7082. TBL_PC* sd;
  7083. const char *bonus_script, *other_script = NULL;
  7084. sd = script_rid2sd(st);
  7085. if( sd == NULL )
  7086. return 0; // no player attached
  7087. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  7088. return 0;
  7089. rate = script_getnum(st,3);
  7090. dur = script_getnum(st,4);
  7091. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7092. bonus_script = script_getstr(st,2);
  7093. if( !rate || !dur || !atk_type || !bonus_script )
  7094. return 0;
  7095. if( script_hasdata(st,6) )
  7096. other_script = script_getstr(st,6);
  7097. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7098. bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  7099. {
  7100. script_add_autobonus(bonus_script);
  7101. if( other_script )
  7102. script_add_autobonus(other_script);
  7103. }
  7104. return 0;
  7105. }
  7106. /// Changes the level of a player skill.
  7107. /// <flag> defaults to 1
  7108. /// <flag>=0 : set the level of the skill
  7109. /// <flag>=1 : set the temporary level of the skill
  7110. /// <flag>=2 : add to the level of the skill
  7111. ///
  7112. /// skill <skill id>,<level>,<flag>
  7113. /// skill <skill id>,<level>
  7114. /// skill "<skill name>",<level>,<flag>
  7115. /// skill "<skill name>",<level>
  7116. BUILDIN_FUNC(skill)
  7117. {
  7118. int id;
  7119. int level;
  7120. int flag = 1;
  7121. TBL_PC* sd;
  7122. sd = script_rid2sd(st);
  7123. if( sd == NULL )
  7124. return 0;// no player attached, report source
  7125. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7126. level = script_getnum(st,3);
  7127. if( script_hasdata(st,4) )
  7128. flag = script_getnum(st,4);
  7129. pc_skill(sd, id, level, flag);
  7130. return 0;
  7131. }
  7132. /// Changes the level of a player skill.
  7133. /// like skill, but <flag> defaults to 2
  7134. ///
  7135. /// addtoskill <skill id>,<amount>,<flag>
  7136. /// addtoskill <skill id>,<amount>
  7137. /// addtoskill "<skill name>",<amount>,<flag>
  7138. /// addtoskill "<skill name>",<amount>
  7139. ///
  7140. /// @see skill
  7141. BUILDIN_FUNC(addtoskill)
  7142. {
  7143. int id;
  7144. int level;
  7145. int flag = 2;
  7146. TBL_PC* sd;
  7147. sd = script_rid2sd(st);
  7148. if( sd == NULL )
  7149. return 0;// no player attached, report source
  7150. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7151. level = script_getnum(st,3);
  7152. if( script_hasdata(st,4) )
  7153. flag = script_getnum(st,4);
  7154. pc_skill(sd, id, level, flag);
  7155. return 0;
  7156. }
  7157. /// Increases the level of a guild skill.
  7158. ///
  7159. /// guildskill <skill id>,<amount>;
  7160. /// guildskill "<skill name>",<amount>;
  7161. BUILDIN_FUNC(guildskill)
  7162. {
  7163. int id;
  7164. int level;
  7165. TBL_PC* sd;
  7166. int i;
  7167. sd = script_rid2sd(st);
  7168. if( sd == NULL )
  7169. return 0;// no player attached, report source
  7170. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7171. level = script_getnum(st,3);
  7172. for( i=0; i < level; i++ )
  7173. guild_skillup(sd, id);
  7174. return 0;
  7175. }
  7176. /// Returns the level of the player skill.
  7177. ///
  7178. /// getskilllv(<skill id>) -> <level>
  7179. /// getskilllv("<skill name>") -> <level>
  7180. BUILDIN_FUNC(getskilllv)
  7181. {
  7182. int id;
  7183. TBL_PC* sd;
  7184. sd = script_rid2sd(st);
  7185. if( sd == NULL )
  7186. return 0;// no player attached, report source
  7187. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7188. script_pushint(st, pc_checkskill(sd,id));
  7189. return 0;
  7190. }
  7191. /// Returns the level of the guild skill.
  7192. ///
  7193. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7194. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7195. BUILDIN_FUNC(getgdskilllv)
  7196. {
  7197. int guild_id;
  7198. uint16 skill_id;
  7199. struct guild* g;
  7200. guild_id = script_getnum(st,2);
  7201. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7202. g = guild_search(guild_id);
  7203. if( g == NULL )
  7204. script_pushint(st, -1);
  7205. else
  7206. script_pushint(st, guild_checkskill(g,skill_id));
  7207. return 0;
  7208. }
  7209. /// Returns the 'basic_skill_check' setting.
  7210. /// This config determines if the server checks the skill level of NV_BASIC
  7211. /// before allowing the basic actions.
  7212. ///
  7213. /// basicskillcheck() -> <bool>
  7214. BUILDIN_FUNC(basicskillcheck)
  7215. {
  7216. script_pushint(st, battle_config.basic_skill_check);
  7217. return 0;
  7218. }
  7219. /// Returns the GM level of the player.
  7220. ///
  7221. /// getgmlevel() -> <level>
  7222. BUILDIN_FUNC(getgmlevel)
  7223. {
  7224. TBL_PC* sd;
  7225. sd = script_rid2sd(st);
  7226. if( sd == NULL )
  7227. return 0;// no player attached, report source
  7228. script_pushint(st, pc_get_group_level(sd));
  7229. return 0;
  7230. }
  7231. /// Returns the group ID of the player.
  7232. ///
  7233. /// getgroupid() -> <int>
  7234. BUILDIN_FUNC(getgroupid)
  7235. {
  7236. TBL_PC* sd;
  7237. sd = script_rid2sd(st);
  7238. if (sd == NULL)
  7239. return 1; // no player attached, report source
  7240. script_pushint(st, pc_get_group_id(sd));
  7241. return 0;
  7242. }
  7243. /// Terminates the execution of this script instance.
  7244. ///
  7245. /// end
  7246. BUILDIN_FUNC(end)
  7247. {
  7248. st->state = END;
  7249. return 0;
  7250. }
  7251. /// Checks if the player has that effect state (option).
  7252. ///
  7253. /// checkoption(<option>) -> <bool>
  7254. BUILDIN_FUNC(checkoption)
  7255. {
  7256. int option;
  7257. TBL_PC* sd;
  7258. sd = script_rid2sd(st);
  7259. if( sd == NULL )
  7260. return 0;// no player attached, report source
  7261. option = script_getnum(st,2);
  7262. if( sd->sc.option&option )
  7263. script_pushint(st, 1);
  7264. else
  7265. script_pushint(st, 0);
  7266. return 0;
  7267. }
  7268. /// Checks if the player is in that body state (opt1).
  7269. ///
  7270. /// checkoption1(<opt1>) -> <bool>
  7271. BUILDIN_FUNC(checkoption1)
  7272. {
  7273. int opt1;
  7274. TBL_PC* sd;
  7275. sd = script_rid2sd(st);
  7276. if( sd == NULL )
  7277. return 0;// no player attached, report source
  7278. opt1 = script_getnum(st,2);
  7279. if( sd->sc.opt1 == opt1 )
  7280. script_pushint(st, 1);
  7281. else
  7282. script_pushint(st, 0);
  7283. return 0;
  7284. }
  7285. /// Checks if the player has that health state (opt2).
  7286. ///
  7287. /// checkoption2(<opt2>) -> <bool>
  7288. BUILDIN_FUNC(checkoption2)
  7289. {
  7290. int opt2;
  7291. TBL_PC* sd;
  7292. sd = script_rid2sd(st);
  7293. if( sd == NULL )
  7294. return 0;// no player attached, report source
  7295. opt2 = script_getnum(st,2);
  7296. if( sd->sc.opt2&opt2 )
  7297. script_pushint(st, 1);
  7298. else
  7299. script_pushint(st, 0);
  7300. return 0;
  7301. }
  7302. /// Changes the effect state (option) of the player.
  7303. /// <flag> defaults to 1
  7304. /// <flag>=0 : removes the option
  7305. /// <flag>=other : adds the option
  7306. ///
  7307. /// setoption <option>,<flag>;
  7308. /// setoption <option>;
  7309. BUILDIN_FUNC(setoption)
  7310. {
  7311. int option;
  7312. int flag = 1;
  7313. TBL_PC* sd;
  7314. sd = script_rid2sd(st);
  7315. if( sd == NULL )
  7316. return 0;// no player attached, report source
  7317. option = script_getnum(st,2);
  7318. if( script_hasdata(st,3) )
  7319. flag = script_getnum(st,3);
  7320. else if( !option ){// Request to remove everything.
  7321. flag = 0;
  7322. option = OPTION_FALCON|OPTION_RIDING;
  7323. #ifndef NEW_CARTS
  7324. option |= OPTION_CART;
  7325. #endif
  7326. }
  7327. if( flag ){// Add option
  7328. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7329. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7330. pc_setoption(sd, sd->sc.option|option);
  7331. } else// Remove option
  7332. pc_setoption(sd, sd->sc.option&~option);
  7333. return 0;
  7334. }
  7335. /// Returns if the player has a cart.
  7336. ///
  7337. /// checkcart() -> <bool>
  7338. ///
  7339. /// @author Valaris
  7340. BUILDIN_FUNC(checkcart)
  7341. {
  7342. TBL_PC* sd;
  7343. sd = script_rid2sd(st);
  7344. if( sd == NULL )
  7345. return 0;// no player attached, report source
  7346. if( pc_iscarton(sd) )
  7347. script_pushint(st, 1);
  7348. else
  7349. script_pushint(st, 0);
  7350. return 0;
  7351. }
  7352. /// Sets the cart of the player.
  7353. /// <type> defaults to 1
  7354. /// <type>=0 : removes the cart
  7355. /// <type>=1 : Normal cart
  7356. /// <type>=2 : Wooden cart
  7357. /// <type>=3 : Covered cart with flowers and ferns
  7358. /// <type>=4 : Wooden cart with a Panda doll on the back
  7359. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7360. ///
  7361. /// setcart <type>;
  7362. /// setcart;
  7363. BUILDIN_FUNC(setcart)
  7364. {
  7365. int type = 1;
  7366. TBL_PC* sd;
  7367. sd = script_rid2sd(st);
  7368. if( sd == NULL )
  7369. return 0;// no player attached, report source
  7370. if( script_hasdata(st,2) )
  7371. type = script_getnum(st,2);
  7372. pc_setcart(sd, type);
  7373. return 0;
  7374. }
  7375. /// Returns if the player has a falcon.
  7376. ///
  7377. /// checkfalcon() -> <bool>
  7378. ///
  7379. /// @author Valaris
  7380. BUILDIN_FUNC(checkfalcon)
  7381. {
  7382. TBL_PC* sd;
  7383. sd = script_rid2sd(st);
  7384. if( sd == NULL )
  7385. return 0;// no player attached, report source
  7386. if( pc_isfalcon(sd) )
  7387. script_pushint(st, 1);
  7388. else
  7389. script_pushint(st, 0);
  7390. return 0;
  7391. }
  7392. /// Sets if the player has a falcon or not.
  7393. /// <flag> defaults to 1
  7394. ///
  7395. /// setfalcon <flag>;
  7396. /// setfalcon;
  7397. BUILDIN_FUNC(setfalcon)
  7398. {
  7399. int flag = 1;
  7400. TBL_PC* sd;
  7401. sd = script_rid2sd(st);
  7402. if( sd == NULL )
  7403. return 0;// no player attached, report source
  7404. if( script_hasdata(st,2) )
  7405. flag = script_getnum(st,2);
  7406. pc_setfalcon(sd, flag);
  7407. return 0;
  7408. }
  7409. /// Returns if the player is riding.
  7410. ///
  7411. /// checkriding() -> <bool>
  7412. ///
  7413. /// @author Valaris
  7414. BUILDIN_FUNC(checkriding)
  7415. {
  7416. TBL_PC* sd;
  7417. sd = script_rid2sd(st);
  7418. if( sd == NULL )
  7419. return 0;// no player attached, report source
  7420. if( pc_isriding(sd) || pc_isridingwug(sd) || pc_isridingdragon(sd) )
  7421. script_pushint(st, 1);
  7422. else
  7423. script_pushint(st, 0);
  7424. return 0;
  7425. }
  7426. /// Sets if the player is riding.
  7427. /// <flag> defaults to 1
  7428. ///
  7429. /// setriding <flag>;
  7430. /// setriding;
  7431. BUILDIN_FUNC(setriding)
  7432. {
  7433. int flag = 1;
  7434. TBL_PC* sd;
  7435. sd = script_rid2sd(st);
  7436. if( sd == NULL )
  7437. return 0;// no player attached, report source
  7438. if( script_hasdata(st,2) )
  7439. flag = script_getnum(st,2);
  7440. pc_setriding(sd, flag);
  7441. return 0;
  7442. }
  7443. /// Returns if the player has a warg.
  7444. ///
  7445. /// checkwug() -> <bool>
  7446. ///
  7447. BUILDIN_FUNC(checkwug)
  7448. {
  7449. TBL_PC* sd;
  7450. sd = script_rid2sd(st);
  7451. if( sd == NULL )
  7452. return 0;// no player attached, report source
  7453. if( pc_iswug(sd) || pc_isridingwug(sd) )
  7454. script_pushint(st, 1);
  7455. else
  7456. script_pushint(st, 0);
  7457. return 0;
  7458. }
  7459. /// Returns if the player is wearing MADO Gear.
  7460. ///
  7461. /// checkmadogear() -> <bool>
  7462. ///
  7463. BUILDIN_FUNC(checkmadogear)
  7464. {
  7465. TBL_PC* sd;
  7466. sd = script_rid2sd(st);
  7467. if( sd == NULL )
  7468. return 0;// no player attached, report source
  7469. if( pc_ismadogear(sd) )
  7470. script_pushint(st, 1);
  7471. else
  7472. script_pushint(st, 0);
  7473. return 0;
  7474. }
  7475. /// Sets if the player is riding MADO Gear.
  7476. /// <flag> defaults to 1
  7477. ///
  7478. /// setmadogear <flag>;
  7479. /// setmadogear;
  7480. BUILDIN_FUNC(setmadogear)
  7481. {
  7482. int flag = 1;
  7483. TBL_PC* sd;
  7484. sd = script_rid2sd(st);
  7485. if( sd == NULL )
  7486. return 0;// no player attached, report source
  7487. if( script_hasdata(st,2) )
  7488. flag = script_getnum(st,2);
  7489. pc_setmadogear(sd, flag);
  7490. return 0;
  7491. }
  7492. /// Sets the save point of the player.
  7493. ///
  7494. /// save "<map name>",<x>,<y>
  7495. /// savepoint "<map name>",<x>,<y>
  7496. BUILDIN_FUNC(savepoint)
  7497. {
  7498. int x;
  7499. int y;
  7500. short map;
  7501. const char* str;
  7502. TBL_PC* sd;
  7503. sd = script_rid2sd(st);
  7504. if( sd == NULL )
  7505. return 0;// no player attached, report source
  7506. str = script_getstr(st, 2);
  7507. x = script_getnum(st,3);
  7508. y = script_getnum(st,4);
  7509. map = mapindex_name2id(str);
  7510. if( map )
  7511. pc_setsavepoint(sd, map, x, y);
  7512. return 0;
  7513. }
  7514. /*==========================================
  7515. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  7516. *------------------------------------------*/
  7517. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  7518. {
  7519. int type;
  7520. time_t timer;
  7521. struct tm *t;
  7522. type=script_getnum(st,2);
  7523. switch(type){
  7524. case 2:
  7525. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  7526. // from the system clock.)
  7527. script_pushint(st,(int)time(NULL));
  7528. break;
  7529. case 1:
  7530. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  7531. time(&timer);
  7532. t=localtime(&timer);
  7533. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  7534. break;
  7535. case 0:
  7536. default:
  7537. //type 0:(System Ticks)
  7538. script_pushint(st,gettick());
  7539. break;
  7540. }
  7541. return 0;
  7542. }
  7543. /*==========================================
  7544. * GetTime(Type);
  7545. * 1: Sec 2: Min 3: Hour
  7546. * 4: WeekDay 5: MonthDay 6: Month
  7547. * 7: Year
  7548. *------------------------------------------*/
  7549. BUILDIN_FUNC(gettime) /* Asgard Version */
  7550. {
  7551. int type;
  7552. time_t timer;
  7553. struct tm *t;
  7554. type=script_getnum(st,2);
  7555. time(&timer);
  7556. t=localtime(&timer);
  7557. switch(type){
  7558. case 1://Sec(0~59)
  7559. script_pushint(st,t->tm_sec);
  7560. break;
  7561. case 2://Min(0~59)
  7562. script_pushint(st,t->tm_min);
  7563. break;
  7564. case 3://Hour(0~23)
  7565. script_pushint(st,t->tm_hour);
  7566. break;
  7567. case 4://WeekDay(0~6)
  7568. script_pushint(st,t->tm_wday);
  7569. break;
  7570. case 5://MonthDay(01~31)
  7571. script_pushint(st,t->tm_mday);
  7572. break;
  7573. case 6://Month(01~12)
  7574. script_pushint(st,t->tm_mon+1);
  7575. break;
  7576. case 7://Year(20xx)
  7577. script_pushint(st,t->tm_year+1900);
  7578. break;
  7579. case 8://Year Day(01~366)
  7580. script_pushint(st,t->tm_yday+1);
  7581. break;
  7582. default://(format error)
  7583. script_pushint(st,-1);
  7584. break;
  7585. }
  7586. return 0;
  7587. }
  7588. /*==========================================
  7589. * GetTimeStr("TimeFMT", Length);
  7590. *------------------------------------------*/
  7591. BUILDIN_FUNC(gettimestr)
  7592. {
  7593. char *tmpstr;
  7594. const char *fmtstr;
  7595. int maxlen;
  7596. time_t now = time(NULL);
  7597. fmtstr=script_getstr(st,2);
  7598. maxlen=script_getnum(st,3);
  7599. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  7600. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  7601. tmpstr[maxlen]='\0';
  7602. script_pushstr(st,tmpstr);
  7603. return 0;
  7604. }
  7605. /*==========================================
  7606. * Open player storage
  7607. *------------------------------------------*/
  7608. BUILDIN_FUNC(openstorage)
  7609. {
  7610. TBL_PC* sd;
  7611. sd = script_rid2sd(st);
  7612. if( sd == NULL )
  7613. return 0;
  7614. storage_storageopen(sd);
  7615. return 0;
  7616. }
  7617. BUILDIN_FUNC(guildopenstorage)
  7618. {
  7619. TBL_PC* sd;
  7620. int ret;
  7621. sd = script_rid2sd(st);
  7622. if( sd == NULL )
  7623. return 0;
  7624. ret = storage_guild_storageopen(sd);
  7625. script_pushint(st,ret);
  7626. return 0;
  7627. }
  7628. /*==========================================
  7629. * Make player use a skill trought item usage
  7630. *------------------------------------------*/
  7631. /// itemskill <skill id>,<level>
  7632. /// itemskill "<skill name>",<level>
  7633. BUILDIN_FUNC(itemskill)
  7634. {
  7635. int id;
  7636. int lv;
  7637. TBL_PC* sd;
  7638. sd = script_rid2sd(st);
  7639. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  7640. return 0;
  7641. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7642. lv = script_getnum(st,3);
  7643. sd->skillitem=id;
  7644. sd->skillitemlv=lv;
  7645. clif_item_skill(sd,id,lv);
  7646. return 0;
  7647. }
  7648. /*==========================================
  7649. * Attempt to create an item
  7650. *------------------------------------------*/
  7651. BUILDIN_FUNC(produce)
  7652. {
  7653. int trigger;
  7654. TBL_PC* sd;
  7655. sd = script_rid2sd(st);
  7656. if( sd == NULL )
  7657. return 0;
  7658. trigger=script_getnum(st,2);
  7659. clif_skill_produce_mix_list(sd, -1, trigger);
  7660. return 0;
  7661. }
  7662. /*==========================================
  7663. *
  7664. *------------------------------------------*/
  7665. BUILDIN_FUNC(cooking)
  7666. {
  7667. int trigger;
  7668. TBL_PC* sd;
  7669. sd = script_rid2sd(st);
  7670. if( sd == NULL )
  7671. return 0;
  7672. trigger=script_getnum(st,2);
  7673. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  7674. return 0;
  7675. }
  7676. /*==========================================
  7677. * Create a pet
  7678. *------------------------------------------*/
  7679. BUILDIN_FUNC(makepet)
  7680. {
  7681. TBL_PC* sd;
  7682. int id,pet_id;
  7683. id=script_getnum(st,2);
  7684. sd = script_rid2sd(st);
  7685. if( sd == NULL )
  7686. return 0;
  7687. pet_id = search_petDB_index(id, PET_CLASS);
  7688. if (pet_id < 0)
  7689. pet_id = search_petDB_index(id, PET_EGG);
  7690. if (pet_id >= 0 && sd) {
  7691. sd->catch_target_class = pet_db[pet_id].class_;
  7692. intif_create_pet(
  7693. sd->status.account_id, sd->status.char_id,
  7694. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7695. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7696. 100, 0, 1, pet_db[pet_id].jname);
  7697. }
  7698. return 0;
  7699. }
  7700. /*==========================================
  7701. * Give player exp base,job * quest_exp_rate/100
  7702. *------------------------------------------*/
  7703. BUILDIN_FUNC(getexp)
  7704. {
  7705. TBL_PC* sd;
  7706. int base=0,job=0;
  7707. double bonus;
  7708. sd = script_rid2sd(st);
  7709. if( sd == NULL )
  7710. return 0;
  7711. base=script_getnum(st,2);
  7712. job =script_getnum(st,3);
  7713. if(base<0 || job<0)
  7714. return 0;
  7715. // bonus for npc-given exp
  7716. bonus = battle_config.quest_exp_rate / 100.;
  7717. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7718. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7719. pc_gainexp(sd, NULL, base, job, true);
  7720. return 0;
  7721. }
  7722. /*==========================================
  7723. * Gain guild exp [Celest]
  7724. *------------------------------------------*/
  7725. BUILDIN_FUNC(guildgetexp)
  7726. {
  7727. TBL_PC* sd;
  7728. int exp;
  7729. sd = script_rid2sd(st);
  7730. if( sd == NULL )
  7731. return 0;
  7732. exp = script_getnum(st,2);
  7733. if(exp < 0)
  7734. return 0;
  7735. if(sd && sd->status.guild_id > 0)
  7736. guild_getexp (sd, exp);
  7737. return 0;
  7738. }
  7739. /*==========================================
  7740. * Changes the guild master of a guild [Skotlex]
  7741. *------------------------------------------*/
  7742. BUILDIN_FUNC(guildchangegm)
  7743. {
  7744. TBL_PC *sd;
  7745. int guild_id;
  7746. const char *name;
  7747. guild_id = script_getnum(st,2);
  7748. name = script_getstr(st,3);
  7749. sd=map_nick2sd(name);
  7750. if (!sd)
  7751. script_pushint(st,0);
  7752. else
  7753. script_pushint(st,guild_gm_change(guild_id, sd));
  7754. return 0;
  7755. }
  7756. /*==========================================
  7757. * Spawn a monster :
  7758. @mapn,x,y : location
  7759. @str : monster name
  7760. @class_ : mob_id
  7761. @amount : nb to spawn
  7762. @event : event to attach to mob
  7763. *------------------------------------------*/
  7764. BUILDIN_FUNC(monster)
  7765. {
  7766. const char* mapn = script_getstr(st,2);
  7767. int x = script_getnum(st,3);
  7768. int y = script_getnum(st,4);
  7769. const char* str = script_getstr(st,5);
  7770. int class_ = script_getnum(st,6);
  7771. int amount = script_getnum(st,7);
  7772. const char* event = "";
  7773. unsigned int size = SZ_SMALL;
  7774. unsigned int ai = AI_NONE;
  7775. struct map_session_data* sd;
  7776. int16 m;
  7777. if (script_hasdata(st, 8))
  7778. {
  7779. event = script_getstr(st, 8);
  7780. check_event(st, event);
  7781. }
  7782. if (script_hasdata(st, 9))
  7783. {
  7784. size = script_getnum(st, 9);
  7785. if (size > 3)
  7786. {
  7787. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7788. return 1;
  7789. }
  7790. }
  7791. if (script_hasdata(st, 10))
  7792. {
  7793. ai = script_getnum(st, 10);
  7794. if (ai > 4)
  7795. {
  7796. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7797. return 1;
  7798. }
  7799. }
  7800. if (class_ >= 0 && !mobdb_checkid(class_))
  7801. {
  7802. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7803. return 1;
  7804. }
  7805. sd = map_id2sd(st->rid);
  7806. if (sd && strcmp(mapn, "this") == 0)
  7807. m = sd->bl.m;
  7808. else
  7809. {
  7810. m = map_mapname2mapid(mapn);
  7811. if (map[m].flag.src4instance && st->instance_id)
  7812. { // Try to redirect to the instance map, not the src map
  7813. if ((m = instance_mapid2imapid(m, st->instance_id)) < 0)
  7814. {
  7815. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7816. return 1;
  7817. }
  7818. }
  7819. }
  7820. mob_once_spawn(sd, m, x, y, str, class_, amount, event, size, ai);
  7821. return 0;
  7822. }
  7823. /*==========================================
  7824. * Request List of Monster Drops
  7825. *------------------------------------------*/
  7826. BUILDIN_FUNC(getmobdrops)
  7827. {
  7828. int class_ = script_getnum(st,2);
  7829. int i, j = 0;
  7830. struct mob_db *mob;
  7831. if( !mobdb_checkid(class_) )
  7832. {
  7833. script_pushint(st, 0);
  7834. return 0;
  7835. }
  7836. mob = mob_db(class_);
  7837. for( i = 0; i < MAX_MOB_DROP; i++ )
  7838. {
  7839. if( mob->dropitem[i].nameid < 1 )
  7840. continue;
  7841. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  7842. continue;
  7843. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  7844. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  7845. j++;
  7846. }
  7847. mapreg_setreg(add_str("$@MobDrop_count"), j);
  7848. script_pushint(st, 1);
  7849. return 0;
  7850. }
  7851. /*==========================================
  7852. * Same as monster but randomize location in x0,x1,y0,y1 area
  7853. *------------------------------------------*/
  7854. BUILDIN_FUNC(areamonster)
  7855. {
  7856. const char* mapn = script_getstr(st,2);
  7857. int x0 = script_getnum(st,3);
  7858. int y0 = script_getnum(st,4);
  7859. int x1 = script_getnum(st,5);
  7860. int y1 = script_getnum(st,6);
  7861. const char* str = script_getstr(st,7);
  7862. int class_ = script_getnum(st,8);
  7863. int amount = script_getnum(st,9);
  7864. const char* event = "";
  7865. unsigned int size = SZ_SMALL;
  7866. unsigned int ai = AI_NONE;
  7867. struct map_session_data* sd;
  7868. int16 m;
  7869. if (script_hasdata(st,10))
  7870. {
  7871. event = script_getstr(st, 10);
  7872. check_event(st, event);
  7873. }
  7874. if (script_hasdata(st, 11))
  7875. {
  7876. size = script_getnum(st, 11);
  7877. if (size > 3)
  7878. {
  7879. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  7880. return 1;
  7881. }
  7882. }
  7883. if (script_hasdata(st, 12))
  7884. {
  7885. ai = script_getnum(st, 12);
  7886. if (ai > 4)
  7887. {
  7888. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  7889. return 1;
  7890. }
  7891. }
  7892. sd = map_id2sd(st->rid);
  7893. if (sd && strcmp(mapn, "this") == 0)
  7894. m = sd->bl.m;
  7895. else
  7896. {
  7897. m = map_mapname2mapid(mapn);
  7898. if (map[m].flag.src4instance && st->instance_id)
  7899. { // Try to redirect to the instance map, not the src map
  7900. if ((m = instance_mapid2imapid(m, st->instance_id)) < 0)
  7901. {
  7902. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  7903. return 1;
  7904. }
  7905. }
  7906. }
  7907. mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, amount, event, size, ai);
  7908. return 0;
  7909. }
  7910. /*==========================================
  7911. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  7912. *------------------------------------------*/
  7913. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  7914. { //same fix but with killmonster instead - stripping events from mobs.
  7915. TBL_MOB* md = (TBL_MOB*)bl;
  7916. char *event=va_arg(ap,char *);
  7917. int allflag=va_arg(ap,int);
  7918. md->state.npc_killmonster = 1;
  7919. if(!allflag){
  7920. if(strcmp(event,md->npc_event)==0)
  7921. status_kill(bl);
  7922. }else{
  7923. if(!md->spawn)
  7924. status_kill(bl);
  7925. }
  7926. md->state.npc_killmonster = 0;
  7927. return 0;
  7928. }
  7929. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7930. {
  7931. TBL_MOB* md = (TBL_MOB*)bl;
  7932. char *event=va_arg(ap,char *);
  7933. int allflag=va_arg(ap,int);
  7934. if(!allflag){
  7935. if(strcmp(event,md->npc_event)==0)
  7936. status_kill(bl);
  7937. }else{
  7938. if(!md->spawn)
  7939. status_kill(bl);
  7940. }
  7941. return 0;
  7942. }
  7943. BUILDIN_FUNC(killmonster)
  7944. {
  7945. const char *mapname,*event;
  7946. int16 m,allflag=0;
  7947. mapname=script_getstr(st,2);
  7948. event=script_getstr(st,3);
  7949. if(strcmp(event,"All")==0)
  7950. allflag = 1;
  7951. else
  7952. check_event(st, event);
  7953. if( (m=map_mapname2mapid(mapname))<0 )
  7954. return 0;
  7955. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7956. return 0;
  7957. if( script_hasdata(st,4) ) {
  7958. if ( script_getnum(st,4) == 1 ) {
  7959. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7960. return 0;
  7961. }
  7962. }
  7963. map_freeblock_lock();
  7964. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  7965. map_freeblock_unlock();
  7966. return 0;
  7967. }
  7968. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  7969. { //Strips the event from the mob if it's killed the old method.
  7970. struct mob_data *md;
  7971. md = BL_CAST(BL_MOB, bl);
  7972. if (md->npc_event[0])
  7973. md->npc_event[0] = 0;
  7974. status_kill(bl);
  7975. return 0;
  7976. }
  7977. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7978. {
  7979. status_kill(bl);
  7980. return 0;
  7981. }
  7982. BUILDIN_FUNC(killmonsterall)
  7983. {
  7984. const char *mapname;
  7985. int16 m;
  7986. mapname=script_getstr(st,2);
  7987. if( (m = map_mapname2mapid(mapname))<0 )
  7988. return 0;
  7989. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  7990. return 0;
  7991. if( script_hasdata(st,3) ) {
  7992. if ( script_getnum(st,3) == 1 ) {
  7993. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  7994. return 0;
  7995. }
  7996. }
  7997. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  7998. return 0;
  7999. }
  8000. /*==========================================
  8001. * Creates a clone of a player.
  8002. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8003. *------------------------------------------*/
  8004. BUILDIN_FUNC(clone)
  8005. {
  8006. TBL_PC *sd, *msd=NULL;
  8007. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  8008. unsigned int duration = 0;
  8009. const char *map,*event="";
  8010. map=script_getstr(st,2);
  8011. x=script_getnum(st,3);
  8012. y=script_getnum(st,4);
  8013. event=script_getstr(st,5);
  8014. char_id=script_getnum(st,6);
  8015. if( script_hasdata(st,7) )
  8016. master_id=script_getnum(st,7);
  8017. if( script_hasdata(st,8) )
  8018. mode=script_getnum(st,8);
  8019. if( script_hasdata(st,9) )
  8020. flag=script_getnum(st,9);
  8021. if( script_hasdata(st,10) )
  8022. duration=script_getnum(st,10);
  8023. check_event(st, event);
  8024. m = map_mapname2mapid(map);
  8025. if (m < 0) return 0;
  8026. sd = map_charid2sd(char_id);
  8027. if (master_id) {
  8028. msd = map_charid2sd(master_id);
  8029. if (msd)
  8030. master_id = msd->bl.id;
  8031. else
  8032. master_id = 0;
  8033. }
  8034. if (sd) //Return ID of newly crafted clone.
  8035. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8036. else //Failed to create clone.
  8037. script_pushint(st,0);
  8038. return 0;
  8039. }
  8040. /*==========================================
  8041. *------------------------------------------*/
  8042. BUILDIN_FUNC(doevent)
  8043. {
  8044. const char* event = script_getstr(st,2);
  8045. struct map_session_data* sd;
  8046. if( ( sd = script_rid2sd(st) ) == NULL )
  8047. {
  8048. return 0;
  8049. }
  8050. check_event(st, event);
  8051. npc_event(sd, event, 0);
  8052. return 0;
  8053. }
  8054. /*==========================================
  8055. *------------------------------------------*/
  8056. BUILDIN_FUNC(donpcevent)
  8057. {
  8058. const char* event = script_getstr(st,2);
  8059. check_event(st, event);
  8060. if( !npc_event_do(event) ) {
  8061. struct npc_data * nd = map_id2nd(st->oid);
  8062. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8063. script_pushint(st, 0);
  8064. } else
  8065. script_pushint(st, 1);
  8066. return 0;
  8067. }
  8068. /// for Aegis compatibility
  8069. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8070. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8071. {
  8072. const char* npc = script_getstr(st,2);
  8073. const char* command = script_getstr(st,3);
  8074. char event[EVENT_NAME_LENGTH];
  8075. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8076. check_event(st, event);
  8077. npc_event_do(event);
  8078. return 0;
  8079. }
  8080. /*==========================================
  8081. *------------------------------------------*/
  8082. BUILDIN_FUNC(addtimer)
  8083. {
  8084. int tick = script_getnum(st,2);
  8085. const char* event = script_getstr(st, 3);
  8086. TBL_PC* sd;
  8087. check_event(st, event);
  8088. sd = script_rid2sd(st);
  8089. if( sd == NULL )
  8090. return 0;
  8091. pc_addeventtimer(sd,tick,event);
  8092. return 0;
  8093. }
  8094. /*==========================================
  8095. *------------------------------------------*/
  8096. BUILDIN_FUNC(deltimer)
  8097. {
  8098. const char *event;
  8099. TBL_PC* sd;
  8100. event=script_getstr(st, 2);
  8101. sd = script_rid2sd(st);
  8102. if( sd == NULL )
  8103. return 0;
  8104. check_event(st, event);
  8105. pc_deleventtimer(sd,event);
  8106. return 0;
  8107. }
  8108. /*==========================================
  8109. *------------------------------------------*/
  8110. BUILDIN_FUNC(addtimercount)
  8111. {
  8112. const char *event;
  8113. int tick;
  8114. TBL_PC* sd;
  8115. event=script_getstr(st, 2);
  8116. tick=script_getnum(st,3);
  8117. sd = script_rid2sd(st);
  8118. if( sd == NULL )
  8119. return 0;
  8120. check_event(st, event);
  8121. pc_addeventtimercount(sd,event,tick);
  8122. return 0;
  8123. }
  8124. /*==========================================
  8125. *------------------------------------------*/
  8126. BUILDIN_FUNC(initnpctimer)
  8127. {
  8128. struct npc_data *nd;
  8129. int flag = 0;
  8130. if( script_hasdata(st,3) )
  8131. { //Two arguments: NPC name and attach flag.
  8132. nd = npc_name2id(script_getstr(st, 2));
  8133. flag = script_getnum(st,3);
  8134. }
  8135. else if( script_hasdata(st,2) )
  8136. { //Check if argument is numeric (flag) or string (npc name)
  8137. struct script_data *data;
  8138. data = script_getdata(st,2);
  8139. get_val(st,data);
  8140. if( data_isstring(data) ) //NPC name
  8141. nd = npc_name2id(conv_str(st, data));
  8142. else if( data_isint(data) ) //Flag
  8143. {
  8144. nd = (struct npc_data *)map_id2bl(st->oid);
  8145. flag = conv_num(st,data);
  8146. }
  8147. else
  8148. {
  8149. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8150. return 1;
  8151. }
  8152. }
  8153. else
  8154. nd = (struct npc_data *)map_id2bl(st->oid);
  8155. if( !nd )
  8156. return 0;
  8157. if( flag ) //Attach
  8158. {
  8159. TBL_PC* sd = script_rid2sd(st);
  8160. if( sd == NULL )
  8161. return 0;
  8162. nd->u.scr.rid = sd->bl.id;
  8163. }
  8164. nd->u.scr.timertick = 0;
  8165. npc_settimerevent_tick(nd,0);
  8166. npc_timerevent_start(nd, st->rid);
  8167. return 0;
  8168. }
  8169. /*==========================================
  8170. *------------------------------------------*/
  8171. BUILDIN_FUNC(startnpctimer)
  8172. {
  8173. struct npc_data *nd;
  8174. int flag = 0;
  8175. if( script_hasdata(st,3) )
  8176. { //Two arguments: NPC name and attach flag.
  8177. nd = npc_name2id(script_getstr(st, 2));
  8178. flag = script_getnum(st,3);
  8179. }
  8180. else if( script_hasdata(st,2) )
  8181. { //Check if argument is numeric (flag) or string (npc name)
  8182. struct script_data *data;
  8183. data = script_getdata(st,2);
  8184. get_val(st,data);
  8185. if( data_isstring(data) ) //NPC name
  8186. nd = npc_name2id(conv_str(st, data));
  8187. else if( data_isint(data) ) //Flag
  8188. {
  8189. nd = (struct npc_data *)map_id2bl(st->oid);
  8190. flag = conv_num(st,data);
  8191. }
  8192. else
  8193. {
  8194. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8195. return 1;
  8196. }
  8197. }
  8198. else
  8199. nd=(struct npc_data *)map_id2bl(st->oid);
  8200. if( !nd )
  8201. return 0;
  8202. if( flag ) //Attach
  8203. {
  8204. TBL_PC* sd = script_rid2sd(st);
  8205. if( sd == NULL )
  8206. return 0;
  8207. nd->u.scr.rid = sd->bl.id;
  8208. }
  8209. npc_timerevent_start(nd, st->rid);
  8210. return 0;
  8211. }
  8212. /*==========================================
  8213. *------------------------------------------*/
  8214. BUILDIN_FUNC(stopnpctimer)
  8215. {
  8216. struct npc_data *nd;
  8217. int flag = 0;
  8218. if( script_hasdata(st,3) )
  8219. { //Two arguments: NPC name and attach flag.
  8220. nd = npc_name2id(script_getstr(st, 2));
  8221. flag = script_getnum(st,3);
  8222. }
  8223. else if( script_hasdata(st,2) )
  8224. { //Check if argument is numeric (flag) or string (npc name)
  8225. struct script_data *data;
  8226. data = script_getdata(st,2);
  8227. get_val(st,data);
  8228. if( data_isstring(data) ) //NPC name
  8229. nd = npc_name2id(conv_str(st, data));
  8230. else if( data_isint(data) ) //Flag
  8231. {
  8232. nd = (struct npc_data *)map_id2bl(st->oid);
  8233. flag = conv_num(st,data);
  8234. }
  8235. else
  8236. {
  8237. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8238. return 1;
  8239. }
  8240. }
  8241. else
  8242. nd=(struct npc_data *)map_id2bl(st->oid);
  8243. if( !nd )
  8244. return 0;
  8245. if( flag ) //Detach
  8246. nd->u.scr.rid = 0;
  8247. npc_timerevent_stop(nd);
  8248. return 0;
  8249. }
  8250. /*==========================================
  8251. *------------------------------------------*/
  8252. BUILDIN_FUNC(getnpctimer)
  8253. {
  8254. struct npc_data *nd;
  8255. TBL_PC *sd;
  8256. int type = script_getnum(st,2);
  8257. int val = 0;
  8258. if( script_hasdata(st,3) )
  8259. nd = npc_name2id(script_getstr(st,3));
  8260. else
  8261. nd = (struct npc_data *)map_id2bl(st->oid);
  8262. if( !nd || nd->bl.type != BL_NPC )
  8263. {
  8264. script_pushint(st,0);
  8265. ShowError("getnpctimer: Invalid NPC.\n");
  8266. return 1;
  8267. }
  8268. switch( type )
  8269. {
  8270. case 0: val = npc_gettimerevent_tick(nd); break;
  8271. case 1:
  8272. if( nd->u.scr.rid )
  8273. {
  8274. sd = map_id2sd(nd->u.scr.rid);
  8275. if( !sd )
  8276. {
  8277. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8278. break;
  8279. }
  8280. val = (sd->npc_timer_id != INVALID_TIMER);
  8281. }
  8282. else
  8283. val = (nd->u.scr.timerid != INVALID_TIMER);
  8284. break;
  8285. case 2: val = nd->u.scr.timeramount; break;
  8286. }
  8287. script_pushint(st,val);
  8288. return 0;
  8289. }
  8290. /*==========================================
  8291. *------------------------------------------*/
  8292. BUILDIN_FUNC(setnpctimer)
  8293. {
  8294. int tick;
  8295. struct npc_data *nd;
  8296. tick = script_getnum(st,2);
  8297. if( script_hasdata(st,3) )
  8298. nd = npc_name2id(script_getstr(st,3));
  8299. else
  8300. nd = (struct npc_data *)map_id2bl(st->oid);
  8301. if( !nd || nd->bl.type != BL_NPC )
  8302. {
  8303. script_pushint(st,1);
  8304. ShowError("setnpctimer: Invalid NPC.\n");
  8305. return 1;
  8306. }
  8307. npc_settimerevent_tick(nd,tick);
  8308. script_pushint(st,0);
  8309. return 0;
  8310. }
  8311. /*==========================================
  8312. * attaches the player rid to the timer [Celest]
  8313. *------------------------------------------*/
  8314. BUILDIN_FUNC(attachnpctimer)
  8315. {
  8316. TBL_PC *sd;
  8317. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8318. if( !nd || nd->bl.type != BL_NPC )
  8319. {
  8320. script_pushint(st,1);
  8321. ShowError("setnpctimer: Invalid NPC.\n");
  8322. return 1;
  8323. }
  8324. if( script_hasdata(st,2) )
  8325. sd = map_nick2sd(script_getstr(st,2));
  8326. else
  8327. sd = script_rid2sd(st);
  8328. if( !sd )
  8329. {
  8330. script_pushint(st,1);
  8331. ShowWarning("attachnpctimer: Invalid player.\n");
  8332. return 1;
  8333. }
  8334. nd->u.scr.rid = sd->bl.id;
  8335. script_pushint(st,0);
  8336. return 0;
  8337. }
  8338. /*==========================================
  8339. * detaches a player rid from the timer [Celest]
  8340. *------------------------------------------*/
  8341. BUILDIN_FUNC(detachnpctimer)
  8342. {
  8343. struct npc_data *nd;
  8344. if( script_hasdata(st,2) )
  8345. nd = npc_name2id(script_getstr(st,2));
  8346. else
  8347. nd = (struct npc_data *)map_id2bl(st->oid);
  8348. if( !nd || nd->bl.type != BL_NPC )
  8349. {
  8350. script_pushint(st,1);
  8351. ShowError("detachnpctimer: Invalid NPC.\n");
  8352. return 1;
  8353. }
  8354. nd->u.scr.rid = 0;
  8355. script_pushint(st,0);
  8356. return 0;
  8357. }
  8358. /*==========================================
  8359. * To avoid "player not attached" script errors, this function is provided,
  8360. * it checks if there is a player attached to the current script. [Skotlex]
  8361. * If no, returns 0, if yes, returns the account_id of the attached player.
  8362. *------------------------------------------*/
  8363. BUILDIN_FUNC(playerattached)
  8364. {
  8365. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8366. script_pushint(st,0);
  8367. else
  8368. script_pushint(st,st->rid);
  8369. return 0;
  8370. }
  8371. /*==========================================
  8372. *------------------------------------------*/
  8373. BUILDIN_FUNC(announce)
  8374. {
  8375. const char *mes = script_getstr(st,2);
  8376. int flag = script_getnum(st,3);
  8377. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8378. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8379. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8380. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8381. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8382. if (flag&0x0f) // Broadcast source or broadcast region defined
  8383. {
  8384. send_target target;
  8385. 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
  8386. if (bl == NULL)
  8387. return 0;
  8388. flag &= 0x07;
  8389. target = (flag == 1) ? ALL_SAMEMAP :
  8390. (flag == 2) ? AREA :
  8391. (flag == 3) ? SELF :
  8392. ALL_CLIENT;
  8393. if (fontColor)
  8394. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8395. else
  8396. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&0xf0, target);
  8397. }
  8398. else
  8399. {
  8400. if (fontColor)
  8401. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8402. else
  8403. intif_broadcast(mes, (int)strlen(mes)+1, flag&0xf0);
  8404. }
  8405. return 0;
  8406. }
  8407. /*==========================================
  8408. *------------------------------------------*/
  8409. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8410. {
  8411. char *mes = va_arg(ap, char *);
  8412. int len = va_arg(ap, int);
  8413. int type = va_arg(ap, int);
  8414. char *fontColor = va_arg(ap, char *);
  8415. short fontType = (short)va_arg(ap, int);
  8416. short fontSize = (short)va_arg(ap, int);
  8417. short fontAlign = (short)va_arg(ap, int);
  8418. short fontY = (short)va_arg(ap, int);
  8419. if (fontColor)
  8420. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8421. else
  8422. clif_broadcast(bl, mes, len, type, SELF);
  8423. return 0;
  8424. }
  8425. BUILDIN_FUNC(mapannounce)
  8426. {
  8427. const char *mapname = script_getstr(st,2);
  8428. const char *mes = script_getstr(st,3);
  8429. int flag = script_getnum(st,4);
  8430. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8431. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8432. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8433. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8434. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8435. int16 m;
  8436. if ((m = map_mapname2mapid(mapname)) < 0)
  8437. return 0;
  8438. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8439. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8440. return 0;
  8441. }
  8442. /*==========================================
  8443. *------------------------------------------*/
  8444. BUILDIN_FUNC(areaannounce)
  8445. {
  8446. const char *mapname = script_getstr(st,2);
  8447. int x0 = script_getnum(st,3);
  8448. int y0 = script_getnum(st,4);
  8449. int x1 = script_getnum(st,5);
  8450. int y1 = script_getnum(st,6);
  8451. const char *mes = script_getstr(st,7);
  8452. int flag = script_getnum(st,8);
  8453. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  8454. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  8455. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  8456. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  8457. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  8458. int16 m;
  8459. if ((m = map_mapname2mapid(mapname)) < 0)
  8460. return 0;
  8461. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8462. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  8463. return 0;
  8464. }
  8465. /*==========================================
  8466. *------------------------------------------*/
  8467. BUILDIN_FUNC(getusers)
  8468. {
  8469. int flag, val = 0;
  8470. struct map_session_data* sd;
  8471. struct block_list* bl = NULL;
  8472. flag = script_getnum(st,2);
  8473. switch(flag&0x07)
  8474. {
  8475. case 0:
  8476. if(flag&0x8)
  8477. {// npc
  8478. bl = map_id2bl(st->oid);
  8479. }
  8480. else if((sd = script_rid2sd(st))!=NULL)
  8481. {// pc
  8482. bl = &sd->bl;
  8483. }
  8484. if(bl)
  8485. {
  8486. val = map[bl->m].users;
  8487. }
  8488. break;
  8489. case 1:
  8490. val = map_getusers();
  8491. break;
  8492. default:
  8493. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8494. script_pushint(st,0);
  8495. return 1;
  8496. }
  8497. script_pushint(st,val);
  8498. return 0;
  8499. }
  8500. /*==========================================
  8501. * Works like @WHO - displays all online users names in window
  8502. *------------------------------------------*/
  8503. BUILDIN_FUNC(getusersname)
  8504. {
  8505. TBL_PC *sd, *pl_sd;
  8506. int /*disp_num=1,*/ group_level = 0;
  8507. struct s_mapiterator* iter;
  8508. sd = script_rid2sd(st);
  8509. if (!sd) return 0;
  8510. group_level = pc_get_group_level(sd);
  8511. iter = mapit_getallusers();
  8512. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  8513. {
  8514. if (pc_has_permission(pl_sd, PC_PERM_HIDE_SESSION) && pc_get_group_level(pl_sd) > group_level)
  8515. continue; // skip hidden sessions
  8516. /* Temporary fix for bugreport:1023.
  8517. * Do not uncomment unless you want thousands of 'next' buttons.
  8518. if((disp_num++)%10==0)
  8519. clif_scriptnext(sd,st->oid);*/
  8520. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  8521. }
  8522. mapit_free(iter);
  8523. return 0;
  8524. }
  8525. /*==========================================
  8526. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  8527. *------------------------------------------*/
  8528. BUILDIN_FUNC(getmapguildusers)
  8529. {
  8530. const char *str;
  8531. int16 m;
  8532. int gid;
  8533. int i=0,c=0;
  8534. struct guild *g = NULL;
  8535. str=script_getstr(st,2);
  8536. gid=script_getnum(st,3);
  8537. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  8538. script_pushint(st,-1);
  8539. return 0;
  8540. }
  8541. g = guild_search(gid);
  8542. if (g){
  8543. for(i = 0; i < g->max_member; i++)
  8544. {
  8545. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  8546. c++;
  8547. }
  8548. }
  8549. script_pushint(st,c);
  8550. return 0;
  8551. }
  8552. /*==========================================
  8553. *------------------------------------------*/
  8554. BUILDIN_FUNC(getmapusers)
  8555. {
  8556. const char *str;
  8557. int16 m;
  8558. str=script_getstr(st,2);
  8559. if( (m=map_mapname2mapid(str))< 0){
  8560. script_pushint(st,-1);
  8561. return 0;
  8562. }
  8563. script_pushint(st,map[m].users);
  8564. return 0;
  8565. }
  8566. /*==========================================
  8567. *------------------------------------------*/
  8568. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  8569. {
  8570. int *users=va_arg(ap,int *);
  8571. (*users)++;
  8572. return 0;
  8573. }
  8574. BUILDIN_FUNC(getareausers)
  8575. {
  8576. const char *str;
  8577. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  8578. str=script_getstr(st,2);
  8579. x0=script_getnum(st,3);
  8580. y0=script_getnum(st,4);
  8581. x1=script_getnum(st,5);
  8582. y1=script_getnum(st,6);
  8583. if( (m=map_mapname2mapid(str))< 0){
  8584. script_pushint(st,-1);
  8585. return 0;
  8586. }
  8587. map_foreachinarea(buildin_getareausers_sub,
  8588. m,x0,y0,x1,y1,BL_PC,&users);
  8589. script_pushint(st,users);
  8590. return 0;
  8591. }
  8592. /*==========================================
  8593. *------------------------------------------*/
  8594. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  8595. {
  8596. int item=va_arg(ap,int);
  8597. int *amount=va_arg(ap,int *);
  8598. struct flooritem_data *drop=(struct flooritem_data *)bl;
  8599. if(drop->item_data.nameid==item)
  8600. (*amount)+=drop->item_data.amount;
  8601. return 0;
  8602. }
  8603. BUILDIN_FUNC(getareadropitem)
  8604. {
  8605. const char *str;
  8606. int16 m,x0,y0,x1,y1;
  8607. int item,amount=0;
  8608. struct script_data *data;
  8609. str=script_getstr(st,2);
  8610. x0=script_getnum(st,3);
  8611. y0=script_getnum(st,4);
  8612. x1=script_getnum(st,5);
  8613. y1=script_getnum(st,6);
  8614. data=script_getdata(st,7);
  8615. get_val(st,data);
  8616. if( data_isstring(data) ){
  8617. const char *name=conv_str(st,data);
  8618. struct item_data *item_data = itemdb_searchname(name);
  8619. item=UNKNOWN_ITEM_ID;
  8620. if( item_data )
  8621. item=item_data->nameid;
  8622. }else
  8623. item=conv_num(st,data);
  8624. if( (m=map_mapname2mapid(str))< 0){
  8625. script_pushint(st,-1);
  8626. return 0;
  8627. }
  8628. map_foreachinarea(buildin_getareadropitem_sub,
  8629. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  8630. script_pushint(st,amount);
  8631. return 0;
  8632. }
  8633. /*==========================================
  8634. *------------------------------------------*/
  8635. BUILDIN_FUNC(enablenpc)
  8636. {
  8637. const char *str;
  8638. str=script_getstr(st,2);
  8639. npc_enable(str,1);
  8640. return 0;
  8641. }
  8642. /*==========================================
  8643. *------------------------------------------*/
  8644. BUILDIN_FUNC(disablenpc)
  8645. {
  8646. const char *str;
  8647. str=script_getstr(st,2);
  8648. npc_enable(str,0);
  8649. return 0;
  8650. }
  8651. /*==========================================
  8652. *------------------------------------------*/
  8653. BUILDIN_FUNC(hideoffnpc)
  8654. {
  8655. const char *str;
  8656. str=script_getstr(st,2);
  8657. npc_enable(str,2);
  8658. return 0;
  8659. }
  8660. /*==========================================
  8661. *------------------------------------------*/
  8662. BUILDIN_FUNC(hideonnpc)
  8663. {
  8664. const char *str;
  8665. str=script_getstr(st,2);
  8666. npc_enable(str,4);
  8667. return 0;
  8668. }
  8669. /// Starts a status effect on the target unit or on the attached player.
  8670. ///
  8671. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  8672. BUILDIN_FUNC(sc_start)
  8673. {
  8674. struct block_list* bl;
  8675. enum sc_type type;
  8676. int tick;
  8677. int val1;
  8678. int val4 = 0;
  8679. type = (sc_type)script_getnum(st,2);
  8680. tick = script_getnum(st,3);
  8681. val1 = script_getnum(st,4);
  8682. if( script_hasdata(st,5) )
  8683. bl = map_id2bl(script_getnum(st,5));
  8684. else
  8685. bl = map_id2bl(st->rid);
  8686. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8687. {// When there isn't a duration specified, try to get it from the skill_db
  8688. tick = skill_get_time(status_sc2skill(type), val1);
  8689. }
  8690. if( potion_flag == 1 && potion_target )
  8691. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8692. bl = map_id2bl(potion_target);
  8693. tick /= 2;// Thrown potions only last half.
  8694. val4 = 1;// Mark that this was a thrown sc_effect
  8695. }
  8696. if( bl )
  8697. status_change_start(NULL, bl, type, 10000, val1, 0, 0, val4, tick, 2);
  8698. return 0;
  8699. }
  8700. /// Starts a status effect on the target unit or on the attached player.
  8701. ///
  8702. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  8703. BUILDIN_FUNC(sc_start2)
  8704. {
  8705. struct block_list* bl;
  8706. enum sc_type type;
  8707. int tick;
  8708. int val1;
  8709. int val4 = 0;
  8710. int rate;
  8711. type = (sc_type)script_getnum(st,2);
  8712. tick = script_getnum(st,3);
  8713. val1 = script_getnum(st,4);
  8714. rate = script_getnum(st,5);
  8715. if( script_hasdata(st,6) )
  8716. bl = map_id2bl(script_getnum(st,6));
  8717. else
  8718. bl = map_id2bl(st->rid);
  8719. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8720. {// When there isn't a duration specified, try to get it from the skill_db
  8721. tick = skill_get_time(status_sc2skill(type), val1);
  8722. }
  8723. if( potion_flag == 1 && potion_target )
  8724. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8725. bl = map_id2bl(potion_target);
  8726. tick /= 2;// Thrown potions only last half.
  8727. val4 = 1;// Mark that this was a thrown sc_effect
  8728. }
  8729. if( bl )
  8730. status_change_start(NULL, bl, type, rate, val1, 0, 0, val4, tick, 2);
  8731. return 0;
  8732. }
  8733. /// Starts a status effect on the target unit or on the attached player.
  8734. ///
  8735. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  8736. BUILDIN_FUNC(sc_start4)
  8737. {
  8738. struct block_list* bl;
  8739. enum sc_type type;
  8740. int tick;
  8741. int val1;
  8742. int val2;
  8743. int val3;
  8744. int val4;
  8745. type = (sc_type)script_getnum(st,2);
  8746. tick = script_getnum(st,3);
  8747. val1 = script_getnum(st,4);
  8748. val2 = script_getnum(st,5);
  8749. val3 = script_getnum(st,6);
  8750. val4 = script_getnum(st,7);
  8751. if( script_hasdata(st,8) )
  8752. bl = map_id2bl(script_getnum(st,8));
  8753. else
  8754. bl = map_id2bl(st->rid);
  8755. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  8756. {// When there isn't a duration specified, try to get it from the skill_db
  8757. tick = skill_get_time(status_sc2skill(type), val1);
  8758. }
  8759. if( potion_flag == 1 && potion_target )
  8760. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  8761. bl = map_id2bl(potion_target);
  8762. tick /= 2;// Thrown potions only last half.
  8763. }
  8764. if( bl )
  8765. status_change_start(NULL, bl, type, 10000, val1, val2, val3, val4, tick, 2);
  8766. return 0;
  8767. }
  8768. /// Ends one or all status effects on the target unit or on the attached player.
  8769. ///
  8770. /// sc_end <effect_id>{,<unit_id>};
  8771. BUILDIN_FUNC(sc_end)
  8772. {
  8773. struct block_list* bl;
  8774. int type;
  8775. type = script_getnum(st, 2);
  8776. if (script_hasdata(st, 3))
  8777. bl = map_id2bl(script_getnum(st, 3));
  8778. else
  8779. bl = map_id2bl(st->rid);
  8780. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  8781. bl = map_id2bl(potion_target);
  8782. if (!bl)
  8783. return 0;
  8784. if (type >= 0 && type < SC_MAX)
  8785. {
  8786. struct status_change *sc = status_get_sc(bl);
  8787. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  8788. if (!sce)
  8789. return 0;
  8790. switch (type)
  8791. {
  8792. case SC_WEIGHT50:
  8793. case SC_WEIGHT90:
  8794. case SC_NOCHAT:
  8795. case SC_PUSH_CART:
  8796. return 0;
  8797. default:
  8798. break;
  8799. }
  8800. //This should help status_change_end force disabling the SC in case it has no limit.
  8801. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  8802. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  8803. }
  8804. else
  8805. status_change_clear(bl, 3); // remove all effects
  8806. return 0;
  8807. }
  8808. /*==========================================
  8809. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  8810. *------------------------------------------*/
  8811. BUILDIN_FUNC(getscrate)
  8812. {
  8813. struct block_list *bl;
  8814. int type,rate;
  8815. type=script_getnum(st,2);
  8816. rate=script_getnum(st,3);
  8817. if( script_hasdata(st,4) ) //get for the bl assigned
  8818. bl = map_id2bl(script_getnum(st,4));
  8819. else
  8820. bl = map_id2bl(st->rid);
  8821. if (bl)
  8822. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, 0);
  8823. script_pushint(st,rate);
  8824. return 0;
  8825. }
  8826. /*==========================================
  8827. * getstatus <type>{, <info>};
  8828. *------------------------------------------*/
  8829. BUILDIN_FUNC(getstatus)
  8830. {
  8831. int id, type;
  8832. struct map_session_data* sd = script_rid2sd(st);
  8833. if( sd == NULL )
  8834. {// no player attached
  8835. return 0;
  8836. }
  8837. id = script_getnum(st, 2);
  8838. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  8839. if( id <= SC_NONE || id >= SC_MAX )
  8840. {// invalid status type given
  8841. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  8842. return 0;
  8843. }
  8844. if( sd->sc.count == 0 || !sd->sc.data[id] )
  8845. {// no status is active
  8846. script_pushint(st, 0);
  8847. return 0;
  8848. }
  8849. switch( type )
  8850. {
  8851. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  8852. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  8853. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  8854. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  8855. case 5:
  8856. {
  8857. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  8858. if( timer )
  8859. {// return the amount of time remaining
  8860. script_pushint(st, timer->tick - gettick());
  8861. }
  8862. }
  8863. break;
  8864. default: script_pushint(st, 1); break;
  8865. }
  8866. return 0;
  8867. }
  8868. /*==========================================
  8869. *
  8870. *------------------------------------------*/
  8871. BUILDIN_FUNC(debugmes)
  8872. {
  8873. const char *str;
  8874. str=script_getstr(st,2);
  8875. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  8876. return 0;
  8877. }
  8878. /*==========================================
  8879. *------------------------------------------*/
  8880. BUILDIN_FUNC(catchpet)
  8881. {
  8882. int pet_id;
  8883. TBL_PC *sd;
  8884. pet_id= script_getnum(st,2);
  8885. sd=script_rid2sd(st);
  8886. if( sd == NULL )
  8887. return 0;
  8888. pet_catch_process1(sd,pet_id);
  8889. return 0;
  8890. }
  8891. /*==========================================
  8892. * [orn]
  8893. *------------------------------------------*/
  8894. BUILDIN_FUNC(homunculus_evolution)
  8895. {
  8896. TBL_PC *sd;
  8897. sd=script_rid2sd(st);
  8898. if( sd == NULL )
  8899. return 0;
  8900. if(merc_is_hom_active(sd->hd))
  8901. {
  8902. if (sd->hd->homunculus.intimacy > 91000)
  8903. merc_hom_evolution(sd->hd);
  8904. else
  8905. clif_emotion(&sd->hd->bl, E_SWT);
  8906. }
  8907. return 0;
  8908. }
  8909. /*==========================================
  8910. * Checks for vaporized morph state
  8911. * and deletes ITEMID_STRANGE_EMBRYO.
  8912. *------------------------------------------*/
  8913. BUILDIN_FUNC(homunculus_mutate)
  8914. {
  8915. int homun_id, m_class, m_id, i;
  8916. TBL_PC *sd;
  8917. sd = script_rid2sd(st);
  8918. if( sd == NULL || sd->hd == NULL )
  8919. return 0;
  8920. if(script_hasdata(st,2))
  8921. homun_id = script_getnum(st,2);
  8922. else
  8923. homun_id = 6048 + (rnd() % 4);
  8924. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  8925. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8926. m_id = hom_class2mapid(homun_id);
  8927. i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  8928. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  8929. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  8930. merc_call_homunculus(sd); // Respawn homunculus.
  8931. hom_mutate(sd->hd, homun_id);
  8932. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  8933. script_pushint(st, 1);
  8934. return 0;
  8935. } else
  8936. clif_emotion(&sd->bl, E_SWT);
  8937. } else
  8938. clif_emotion(&sd->bl, E_SWT);
  8939. script_pushint(st, 0);
  8940. return 0;
  8941. }
  8942. /*==========================================
  8943. * Puts homunculus into morph state
  8944. * and gives ITEMID_STRANGE_EMBRYO.
  8945. *------------------------------------------*/
  8946. BUILDIN_FUNC(morphembryo)
  8947. {
  8948. struct item item_tmp;
  8949. int m_class, i=0;
  8950. TBL_PC *sd;
  8951. sd = script_rid2sd(st);
  8952. if( sd == NULL || sd->hd == NULL )
  8953. return 0;
  8954. if( merc_is_hom_active(sd->hd) ) {
  8955. m_class = hom_class2mapid(sd->hd->homunculus.class_);
  8956. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  8957. memset(&item_tmp, 0, sizeof(item_tmp));
  8958. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  8959. item_tmp.identify = 1;
  8960. if( item_tmp.nameid == 0 || (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  8961. clif_additem(sd, 0, 0, i);
  8962. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  8963. } else {
  8964. merc_hom_vaporize(sd, HOM_ST_MORPH);
  8965. script_pushint(st, 1);
  8966. return 0;
  8967. }
  8968. } else
  8969. clif_emotion(&sd->hd->bl, E_SWT);
  8970. } else
  8971. clif_emotion(&sd->bl, E_SWT);
  8972. script_pushint(st, 0);
  8973. return 0;
  8974. }
  8975. // [Zephyrus]
  8976. BUILDIN_FUNC(homunculus_shuffle)
  8977. {
  8978. TBL_PC *sd;
  8979. sd=script_rid2sd(st);
  8980. if( sd == NULL )
  8981. return 0;
  8982. if(merc_is_hom_active(sd->hd))
  8983. merc_hom_shuffle(sd->hd);
  8984. return 0;
  8985. }
  8986. /*==========================================
  8987. * Check for homunculus state.
  8988. * Return: -1 = No homunculus
  8989. * 0 = Homunculus is active
  8990. * 1 = Homunculus is vaporized (rest)
  8991. * 2 = Homunculus is in morph state
  8992. *------------------------------------------*/
  8993. BUILDIN_FUNC(checkhomcall)
  8994. {
  8995. TBL_PC *sd = script_rid2sd(st);
  8996. TBL_HOM *hd;
  8997. if( sd == NULL )
  8998. return 0;
  8999. hd = sd->hd;
  9000. if( !hd )
  9001. script_pushint(st, -1);
  9002. else
  9003. script_pushint(st, hd->homunculus.vaporize);
  9004. return 0;
  9005. }
  9006. //These two functions bring the eA MAPID_* class functionality to scripts.
  9007. BUILDIN_FUNC(eaclass)
  9008. {
  9009. int class_;
  9010. if( script_hasdata(st,2) )
  9011. class_ = script_getnum(st,2);
  9012. else {
  9013. TBL_PC *sd;
  9014. sd=script_rid2sd(st);
  9015. if (!sd) {
  9016. script_pushint(st,-1);
  9017. return 0;
  9018. }
  9019. class_ = sd->status.class_;
  9020. }
  9021. script_pushint(st,pc_jobid2mapid(class_));
  9022. return 0;
  9023. }
  9024. BUILDIN_FUNC(roclass)
  9025. {
  9026. int class_ =script_getnum(st,2);
  9027. int sex;
  9028. if( script_hasdata(st,3) )
  9029. sex = script_getnum(st,3);
  9030. else {
  9031. TBL_PC *sd;
  9032. if (st->rid && (sd=script_rid2sd(st)))
  9033. sex = sd->status.sex;
  9034. else
  9035. sex = 1; //Just use male when not found.
  9036. }
  9037. script_pushint(st,pc_mapid2jobid(class_, sex));
  9038. return 0;
  9039. }
  9040. /*==========================================
  9041. * Tells client to open a hatching window, used for pet incubator
  9042. *------------------------------------------*/
  9043. BUILDIN_FUNC(birthpet)
  9044. {
  9045. TBL_PC *sd;
  9046. sd=script_rid2sd(st);
  9047. if( sd == NULL )
  9048. return 0;
  9049. if( sd->status.pet_id )
  9050. {// do not send egg list, when you already have a pet
  9051. return 0;
  9052. }
  9053. clif_sendegg(sd);
  9054. return 0;
  9055. }
  9056. /*==========================================
  9057. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  9058. * @type
  9059. * 1 : make like after rebirth
  9060. * 2 : blvl,jlvl=1, skillpoint=0
  9061. * 3 : don't reset skill, blvl=1
  9062. * 4 : jlvl=0
  9063. *------------------------------------------*/
  9064. BUILDIN_FUNC(resetlvl)
  9065. {
  9066. TBL_PC *sd;
  9067. int type=script_getnum(st,2);
  9068. sd=script_rid2sd(st);
  9069. if( sd == NULL )
  9070. return 0;
  9071. pc_resetlvl(sd,type);
  9072. return 0;
  9073. }
  9074. /*==========================================
  9075. * Reset a player status point
  9076. *------------------------------------------*/
  9077. BUILDIN_FUNC(resetstatus)
  9078. {
  9079. TBL_PC *sd;
  9080. sd=script_rid2sd(st);
  9081. pc_resetstate(sd);
  9082. return 0;
  9083. }
  9084. /*==========================================
  9085. * script command resetskill
  9086. *------------------------------------------*/
  9087. BUILDIN_FUNC(resetskill)
  9088. {
  9089. TBL_PC *sd;
  9090. sd=script_rid2sd(st);
  9091. pc_resetskill(sd,1);
  9092. return 0;
  9093. }
  9094. /*==========================================
  9095. * Counts total amount of skill points.
  9096. *------------------------------------------*/
  9097. BUILDIN_FUNC(skillpointcount)
  9098. {
  9099. TBL_PC *sd;
  9100. sd=script_rid2sd(st);
  9101. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9102. return 0;
  9103. }
  9104. /*==========================================
  9105. *
  9106. *------------------------------------------*/
  9107. BUILDIN_FUNC(changebase)
  9108. {
  9109. TBL_PC *sd=NULL;
  9110. int vclass;
  9111. if( script_hasdata(st,3) )
  9112. sd=map_id2sd(script_getnum(st,3));
  9113. else
  9114. sd=script_rid2sd(st);
  9115. if(sd == NULL)
  9116. return 0;
  9117. vclass = script_getnum(st,2);
  9118. if(vclass == JOB_WEDDING)
  9119. {
  9120. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9121. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9122. )
  9123. return 0;
  9124. }
  9125. if(!sd->disguise && vclass != sd->vd.class_) {
  9126. status_set_viewdata(&sd->bl, vclass);
  9127. //Updated client view. Base, Weapon and Cloth Colors.
  9128. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9129. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9130. if (sd->vd.cloth_color)
  9131. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9132. clif_skillinfoblock(sd);
  9133. }
  9134. return 0;
  9135. }
  9136. /*==========================================
  9137. * Unequip all item and request for a changesex to char-serv
  9138. *------------------------------------------*/
  9139. BUILDIN_FUNC(changesex)
  9140. {
  9141. int i;
  9142. TBL_PC *sd = NULL;
  9143. sd = script_rid2sd(st);
  9144. pc_resetskill(sd,4);
  9145. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9146. for( i=0; i<EQI_MAX; i++ )
  9147. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  9148. chrif_changesex(sd);
  9149. return 0;
  9150. }
  9151. /*==========================================
  9152. * Works like 'announce' but outputs in the common chat window
  9153. *------------------------------------------*/
  9154. BUILDIN_FUNC(globalmes)
  9155. {
  9156. struct block_list *bl = map_id2bl(st->oid);
  9157. struct npc_data *nd = (struct npc_data *)bl;
  9158. const char *name=NULL,*mes;
  9159. mes=script_getstr(st,2);
  9160. if(mes==NULL) return 0;
  9161. if(script_hasdata(st,3)){ // npc name to display
  9162. name=script_getstr(st,3);
  9163. } else {
  9164. name=nd->name; //use current npc name
  9165. }
  9166. npc_globalmessage(name,mes); // broadcast to all players connected
  9167. return 0;
  9168. }
  9169. /////////////////////////////////////////////////////////////////////
  9170. // NPC waiting room (chat room)
  9171. //
  9172. /// Creates a waiting room (chat room) for this npc.
  9173. ///
  9174. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9175. BUILDIN_FUNC(waitingroom)
  9176. {
  9177. struct npc_data* nd;
  9178. int pub = 1;
  9179. const char* title = script_getstr(st, 2);
  9180. int limit = script_getnum(st, 3);
  9181. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9182. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9183. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9184. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9185. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9186. nd = (struct npc_data *)map_id2bl(st->oid);
  9187. if( nd != NULL )
  9188. chat_createnpcchat(nd, title, limit, pub, trigger, ev, zeny, minLvl, maxLvl);
  9189. return 0;
  9190. }
  9191. /// Removes the waiting room of the current or target npc.
  9192. ///
  9193. /// delwaitingroom "<npc_name>";
  9194. /// delwaitingroom;
  9195. BUILDIN_FUNC(delwaitingroom)
  9196. {
  9197. struct npc_data* nd;
  9198. if( script_hasdata(st,2) )
  9199. nd = npc_name2id(script_getstr(st, 2));
  9200. else
  9201. nd = (struct npc_data *)map_id2bl(st->oid);
  9202. if( nd != NULL )
  9203. chat_deletenpcchat(nd);
  9204. return 0;
  9205. }
  9206. /// Kicks all the players from the waiting room of the current or target npc.
  9207. ///
  9208. /// kickwaitingroomall "<npc_name>";
  9209. /// kickwaitingroomall;
  9210. BUILDIN_FUNC(waitingroomkickall)
  9211. {
  9212. struct npc_data* nd;
  9213. struct chat_data* cd;
  9214. if( script_hasdata(st,2) )
  9215. nd = npc_name2id(script_getstr(st,2));
  9216. else
  9217. nd = (struct npc_data *)map_id2bl(st->oid);
  9218. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9219. chat_npckickall(cd);
  9220. return 0;
  9221. }
  9222. /// Enables the waiting room event of the current or target npc.
  9223. ///
  9224. /// enablewaitingroomevent "<npc_name>";
  9225. /// enablewaitingroomevent;
  9226. BUILDIN_FUNC(enablewaitingroomevent)
  9227. {
  9228. struct npc_data* nd;
  9229. struct chat_data* cd;
  9230. if( script_hasdata(st,2) )
  9231. nd = npc_name2id(script_getstr(st, 2));
  9232. else
  9233. nd = (struct npc_data *)map_id2bl(st->oid);
  9234. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9235. chat_enableevent(cd);
  9236. return 0;
  9237. }
  9238. /// Disables the waiting room event of the current or target npc.
  9239. ///
  9240. /// disablewaitingroomevent "<npc_name>";
  9241. /// disablewaitingroomevent;
  9242. BUILDIN_FUNC(disablewaitingroomevent)
  9243. {
  9244. struct npc_data *nd;
  9245. struct chat_data *cd;
  9246. if( script_hasdata(st,2) )
  9247. nd = npc_name2id(script_getstr(st, 2));
  9248. else
  9249. nd = (struct npc_data *)map_id2bl(st->oid);
  9250. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9251. chat_disableevent(cd);
  9252. return 0;
  9253. }
  9254. /// Returns info on the waiting room of the current or target npc.
  9255. /// Returns -1 if the type unknown
  9256. /// <type>=0 : current number of users
  9257. /// <type>=1 : maximum number of users allowed
  9258. /// <type>=2 : the number of users that trigger the event
  9259. /// <type>=3 : if the trigger is disabled
  9260. /// <type>=4 : the title of the waiting room
  9261. /// <type>=5 : the password of the waiting room
  9262. /// <type>=16 : the name of the waiting room event
  9263. /// <type>=32 : if the waiting room is full
  9264. /// <type>=33 : if there are enough users to trigger the event
  9265. ///
  9266. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9267. /// getwaitingroomstate(<type>) -> <info>
  9268. BUILDIN_FUNC(getwaitingroomstate)
  9269. {
  9270. struct npc_data *nd;
  9271. struct chat_data *cd;
  9272. int type;
  9273. type = script_getnum(st,2);
  9274. if( script_hasdata(st,3) )
  9275. nd = npc_name2id(script_getstr(st, 3));
  9276. else
  9277. nd = (struct npc_data *)map_id2bl(st->oid);
  9278. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9279. {
  9280. script_pushint(st, -1);
  9281. return 0;
  9282. }
  9283. switch(type)
  9284. {
  9285. case 0: script_pushint(st, cd->users); break;
  9286. case 1: script_pushint(st, cd->limit); break;
  9287. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9288. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9289. case 4: script_pushstrcopy(st, cd->title); break;
  9290. case 5: script_pushstrcopy(st, cd->pass); break;
  9291. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9292. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9293. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9294. default: script_pushint(st, -1); break;
  9295. }
  9296. return 0;
  9297. }
  9298. /// Warps the trigger or target amount of players to the target map and position.
  9299. /// Players are automatically removed from the waiting room.
  9300. /// Those waiting the longest will get warped first.
  9301. /// The target map can be "Random" for a random position in the current map,
  9302. /// and "SavePoint" for the savepoint map+position.
  9303. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9304. ///
  9305. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9306. /// The total number of teleported players is put into $@warpwaitingpcnum
  9307. ///
  9308. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9309. /// warpwaitingpc "<map name>",<x>,<y>;
  9310. BUILDIN_FUNC(warpwaitingpc)
  9311. {
  9312. int x;
  9313. int y;
  9314. int i;
  9315. int n;
  9316. const char* map_name;
  9317. struct npc_data* nd;
  9318. struct chat_data* cd;
  9319. TBL_PC* sd;
  9320. nd = (struct npc_data *)map_id2bl(st->oid);
  9321. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9322. return 0;
  9323. map_name = script_getstr(st,2);
  9324. x = script_getnum(st,3);
  9325. y = script_getnum(st,4);
  9326. n = cd->trigger&0x7f;
  9327. if( script_hasdata(st,5) )
  9328. n = script_getnum(st,5);
  9329. for( i = 0; i < n && cd->users > 0; i++ )
  9330. {
  9331. sd = cd->usersd[0];
  9332. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9333. {// can't teleport on this map
  9334. break;
  9335. }
  9336. if( cd->zeny )
  9337. {// fee set
  9338. if( (uint32)sd->status.zeny < cd->zeny )
  9339. {// no zeny to cover set fee
  9340. break;
  9341. }
  9342. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  9343. }
  9344. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9345. if( strcmp(map_name,"Random") == 0 )
  9346. pc_randomwarp(sd,CLR_TELEPORT);
  9347. else if( strcmp(map_name,"SavePoint") == 0 )
  9348. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9349. else
  9350. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9351. }
  9352. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9353. return 0;
  9354. }
  9355. /////////////////////////////////////////////////////////////////////
  9356. // ...
  9357. //
  9358. /// Detaches a character from a script.
  9359. ///
  9360. /// @param st Script state to detach the character from.
  9361. static void script_detach_rid(struct script_state* st)
  9362. {
  9363. if(st->rid)
  9364. {
  9365. script_detach_state(st, false);
  9366. st->rid = 0;
  9367. }
  9368. }
  9369. /*==========================================
  9370. * Attach sd char id to script and detach current one if any
  9371. *------------------------------------------*/
  9372. BUILDIN_FUNC(attachrid)
  9373. {
  9374. int rid = script_getnum(st,2);
  9375. if (map_id2sd(rid) != NULL) {
  9376. script_detach_rid(st);
  9377. st->rid = rid;
  9378. script_attach_state(st);
  9379. script_pushint(st,1);
  9380. } else
  9381. script_pushint(st,0);
  9382. return 0;
  9383. }
  9384. /*==========================================
  9385. * Detach script to rid
  9386. *------------------------------------------*/
  9387. BUILDIN_FUNC(detachrid)
  9388. {
  9389. script_detach_rid(st);
  9390. return 0;
  9391. }
  9392. /*==========================================
  9393. * Chk if account connected, (and charid from account if specified)
  9394. *------------------------------------------*/
  9395. BUILDIN_FUNC(isloggedin)
  9396. {
  9397. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  9398. if (script_hasdata(st,3) && sd &&
  9399. sd->status.char_id != script_getnum(st,3))
  9400. sd = NULL;
  9401. push_val(st->stack,C_INT,sd!=NULL);
  9402. return 0;
  9403. }
  9404. /*==========================================
  9405. *
  9406. *------------------------------------------*/
  9407. BUILDIN_FUNC(setmapflagnosave)
  9408. {
  9409. int16 m,x,y;
  9410. unsigned short mapindex;
  9411. const char *str,*str2;
  9412. str=script_getstr(st,2);
  9413. str2=script_getstr(st,3);
  9414. x=script_getnum(st,4);
  9415. y=script_getnum(st,5);
  9416. m = map_mapname2mapid(str);
  9417. mapindex = mapindex_name2id(str2);
  9418. if(m >= 0 && mapindex) {
  9419. map[m].flag.nosave=1;
  9420. map[m].save.map=mapindex;
  9421. map[m].save.x=x;
  9422. map[m].save.y=y;
  9423. }
  9424. return 0;
  9425. }
  9426. BUILDIN_FUNC(getmapflag)
  9427. {
  9428. int16 m,i;
  9429. const char *str;
  9430. str=script_getstr(st,2);
  9431. i=script_getnum(st,3);
  9432. m = map_mapname2mapid(str);
  9433. if(m >= 0) {
  9434. switch(i) {
  9435. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  9436. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  9437. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  9438. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  9439. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  9440. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  9441. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  9442. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  9443. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  9444. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  9445. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  9446. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  9447. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  9448. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  9449. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  9450. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  9451. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  9452. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  9453. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  9454. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  9455. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  9456. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  9457. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  9458. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  9459. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  9460. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  9461. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  9462. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  9463. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  9464. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  9465. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  9466. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  9467. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  9468. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  9469. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  9470. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  9471. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  9472. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  9473. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  9474. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  9475. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  9476. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  9477. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  9478. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  9479. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  9480. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  9481. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  9482. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  9483. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  9484. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  9485. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  9486. }
  9487. }
  9488. return 0;
  9489. }
  9490. /* pvp timer handling */
  9491. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  9492. TBL_PC* sd = (TBL_PC*)bl;
  9493. if (sd->pvp_timer == INVALID_TIMER) {
  9494. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  9495. sd->pvp_rank = 0;
  9496. sd->pvp_lastusers = 0;
  9497. sd->pvp_point = 5;
  9498. sd->pvp_won = 0;
  9499. sd->pvp_lost = 0;
  9500. }
  9501. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  9502. return 0;
  9503. }
  9504. BUILDIN_FUNC(setmapflag)
  9505. {
  9506. int16 m,i;
  9507. const char *str;
  9508. int val=0;
  9509. str=script_getstr(st,2);
  9510. i=script_getnum(st,3);
  9511. if(script_hasdata(st,4)){
  9512. val=script_getnum(st,4);
  9513. }
  9514. m = map_mapname2mapid(str);
  9515. if(m >= 0) {
  9516. switch(i) {
  9517. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  9518. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  9519. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  9520. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  9521. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  9522. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  9523. case MF_PVP:
  9524. map[m].flag.pvp = 1;
  9525. if( !battle_config.pk_mode ) {
  9526. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  9527. }
  9528. break;
  9529. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  9530. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  9531. case MF_GVG:
  9532. map[m].flag.gvg = 1;
  9533. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9534. break;
  9535. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  9536. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  9537. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  9538. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  9539. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  9540. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  9541. case MF_SNOW: map[m].flag.snow = 1; break;
  9542. case MF_FOG: map[m].flag.fog = 1; break;
  9543. case MF_SAKURA: map[m].flag.sakura = 1; break;
  9544. case MF_LEAVES: map[m].flag.leaves = 1; break;
  9545. case MF_NOGO: map[m].flag.nogo = 1; break;
  9546. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  9547. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  9548. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  9549. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  9550. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  9551. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  9552. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  9553. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  9554. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  9555. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  9556. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  9557. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  9558. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  9559. case MF_RESTRICTED:
  9560. map[m].zone |= 1<<(val+1);
  9561. map[m].flag.restricted=1;
  9562. break;
  9563. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  9564. case MF_NODROP: map[m].flag.nodrop = 1; break;
  9565. case MF_JEXP: map[m].jexp = (val <= 0) ? 100 : val; break;
  9566. case MF_BEXP: map[m].bexp = (val <= 0) ? 100 : val; break;
  9567. case MF_NOVENDING: map[m].flag.novending = 1; break;
  9568. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  9569. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  9570. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  9571. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  9572. case MF_TOWN: map[m].flag.town = 1; break;
  9573. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  9574. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  9575. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  9576. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  9577. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  9578. case MF_RESET: map[m].flag.reset = 1; break;
  9579. }
  9580. }
  9581. return 0;
  9582. }
  9583. BUILDIN_FUNC(removemapflag)
  9584. {
  9585. int16 m,i;
  9586. const char *str;
  9587. int val=0;
  9588. str=script_getstr(st,2);
  9589. i=script_getnum(st,3);
  9590. if(script_hasdata(st,4)){
  9591. val=script_getnum(st,4);
  9592. }
  9593. m = map_mapname2mapid(str);
  9594. if(m >= 0) {
  9595. switch(i) {
  9596. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  9597. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  9598. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  9599. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  9600. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  9601. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  9602. case MF_PVP:
  9603. map[m].flag.pvp = 0;
  9604. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9605. break;
  9606. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  9607. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  9608. case MF_GVG:
  9609. map[m].flag.gvg = 0;
  9610. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9611. break;
  9612. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  9613. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  9614. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  9615. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  9616. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  9617. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  9618. case MF_SNOW: map[m].flag.snow = 0; break;
  9619. case MF_FOG: map[m].flag.fog = 0; break;
  9620. case MF_SAKURA: map[m].flag.sakura = 0; break;
  9621. case MF_LEAVES: map[m].flag.leaves = 0; break;
  9622. case MF_NOGO: map[m].flag.nogo = 0; break;
  9623. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  9624. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  9625. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  9626. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  9627. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  9628. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  9629. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  9630. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  9631. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  9632. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  9633. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  9634. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  9635. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  9636. case MF_RESTRICTED:
  9637. map[m].zone ^= 1<<(val+1);
  9638. if (map[m].zone == 0){
  9639. map[m].flag.restricted=0;
  9640. }
  9641. break;
  9642. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  9643. case MF_NODROP: map[m].flag.nodrop = 0; break;
  9644. case MF_JEXP: map[m].jexp = 0; break;
  9645. case MF_BEXP: map[m].bexp = 0; break;
  9646. case MF_NOVENDING: map[m].flag.novending = 0; break;
  9647. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  9648. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  9649. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  9650. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  9651. case MF_TOWN: map[m].flag.town = 0; break;
  9652. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  9653. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  9654. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  9655. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  9656. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  9657. case MF_RESET: map[m].flag.reset = 0; break;
  9658. }
  9659. }
  9660. return 0;
  9661. }
  9662. BUILDIN_FUNC(pvpon)
  9663. {
  9664. int16 m;
  9665. const char *str;
  9666. TBL_PC* sd = NULL;
  9667. struct s_mapiterator* iter;
  9668. str = script_getstr(st,2);
  9669. m = map_mapname2mapid(str);
  9670. if( m < 0 || map[m].flag.pvp )
  9671. return 0; // nothing to do
  9672. map[m].flag.pvp = 1;
  9673. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  9674. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  9675. return 0;
  9676. iter = mapit_getallusers();
  9677. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9678. {
  9679. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  9680. continue; // not applicable
  9681. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  9682. sd->pvp_rank = 0;
  9683. sd->pvp_lastusers = 0;
  9684. sd->pvp_point = 5;
  9685. sd->pvp_won = 0;
  9686. sd->pvp_lost = 0;
  9687. }
  9688. mapit_free(iter);
  9689. return 0;
  9690. }
  9691. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  9692. {
  9693. TBL_PC* sd = (TBL_PC*)bl;
  9694. clif_pvpset(sd, 0, 0, 2);
  9695. if (sd->pvp_timer != INVALID_TIMER) {
  9696. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  9697. sd->pvp_timer = INVALID_TIMER;
  9698. }
  9699. return 0;
  9700. }
  9701. BUILDIN_FUNC(pvpoff)
  9702. {
  9703. int16 m;
  9704. const char *str;
  9705. str=script_getstr(st,2);
  9706. m = map_mapname2mapid(str);
  9707. if(m < 0 || !map[m].flag.pvp)
  9708. return 0; //fixed Lupus
  9709. map[m].flag.pvp = 0;
  9710. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9711. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  9712. return 0;
  9713. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  9714. return 0;
  9715. }
  9716. BUILDIN_FUNC(gvgon)
  9717. {
  9718. int16 m;
  9719. const char *str;
  9720. str=script_getstr(st,2);
  9721. m = map_mapname2mapid(str);
  9722. if(m >= 0 && !map[m].flag.gvg) {
  9723. map[m].flag.gvg = 1;
  9724. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  9725. }
  9726. return 0;
  9727. }
  9728. BUILDIN_FUNC(gvgoff)
  9729. {
  9730. int16 m;
  9731. const char *str;
  9732. str=script_getstr(st,2);
  9733. m = map_mapname2mapid(str);
  9734. if(m >= 0 && map[m].flag.gvg) {
  9735. map[m].flag.gvg = 0;
  9736. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  9737. }
  9738. return 0;
  9739. }
  9740. /*==========================================
  9741. * Shows an emoticon on top of the player/npc
  9742. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  9743. *------------------------------------------*/
  9744. //Optional second parameter added by [Skotlex]
  9745. BUILDIN_FUNC(emotion)
  9746. {
  9747. int type;
  9748. int player=0;
  9749. type=script_getnum(st,2);
  9750. if(type < 0 || type > 100)
  9751. return 0;
  9752. if( script_hasdata(st,3) )
  9753. player=script_getnum(st,3);
  9754. if (player) {
  9755. TBL_PC *sd = NULL;
  9756. if( script_hasdata(st,4) )
  9757. sd = map_nick2sd(script_getstr(st,4));
  9758. else
  9759. sd = script_rid2sd(st);
  9760. if (sd)
  9761. clif_emotion(&sd->bl,type);
  9762. } else
  9763. if( script_hasdata(st,4) )
  9764. {
  9765. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  9766. if(nd)
  9767. clif_emotion(&nd->bl,type);
  9768. }
  9769. else
  9770. clif_emotion(map_id2bl(st->oid),type);
  9771. return 0;
  9772. }
  9773. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  9774. {
  9775. int16 m=va_arg(ap,int);
  9776. int g_id=va_arg(ap,int);
  9777. int flag=va_arg(ap,int);
  9778. if(!sd || sd->bl.m != m)
  9779. return 0;
  9780. if(
  9781. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  9782. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  9783. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  9784. )
  9785. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  9786. return 1;
  9787. }
  9788. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  9789. {
  9790. struct mob_data *md=(struct mob_data *)bl;
  9791. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  9792. status_kill(bl);
  9793. return 0;
  9794. }
  9795. BUILDIN_FUNC(maprespawnguildid)
  9796. {
  9797. const char *mapname=script_getstr(st,2);
  9798. int g_id=script_getnum(st,3);
  9799. int flag=script_getnum(st,4);
  9800. int16 m=map_mapname2mapid(mapname);
  9801. if(m == -1)
  9802. return 0;
  9803. //Catch ALL players (in case some are 'between maps' on execution time)
  9804. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  9805. if (flag&4) //Remove script mobs.
  9806. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  9807. return 0;
  9808. }
  9809. BUILDIN_FUNC(agitstart)
  9810. {
  9811. if(agit_flag==1) return 0; // Agit already Start.
  9812. agit_flag=1;
  9813. guild_agit_start();
  9814. return 0;
  9815. }
  9816. BUILDIN_FUNC(agitend)
  9817. {
  9818. if(agit_flag==0) return 0; // Agit already End.
  9819. agit_flag=0;
  9820. guild_agit_end();
  9821. return 0;
  9822. }
  9823. BUILDIN_FUNC(agitstart2)
  9824. {
  9825. if(agit2_flag==1) return 0; // Agit2 already Start.
  9826. agit2_flag=1;
  9827. guild_agit2_start();
  9828. return 0;
  9829. }
  9830. BUILDIN_FUNC(agitend2)
  9831. {
  9832. if(agit2_flag==0) return 0; // Agit2 already End.
  9833. agit2_flag=0;
  9834. guild_agit2_end();
  9835. return 0;
  9836. }
  9837. /*==========================================
  9838. * Returns whether woe is on or off. // choice script
  9839. *------------------------------------------*/
  9840. BUILDIN_FUNC(agitcheck)
  9841. {
  9842. script_pushint(st,agit_flag);
  9843. return 0;
  9844. }
  9845. /*==========================================
  9846. * Returns whether woese is on or off. // choice script
  9847. *------------------------------------------*/
  9848. BUILDIN_FUNC(agitcheck2)
  9849. {
  9850. script_pushint(st,agit2_flag);
  9851. return 0;
  9852. }
  9853. /// Sets the guild_id of this npc.
  9854. ///
  9855. /// flagemblem <guild_id>;
  9856. BUILDIN_FUNC(flagemblem)
  9857. {
  9858. TBL_NPC* nd;
  9859. int g_id = script_getnum(st,2);
  9860. if(g_id < 0) return 0;
  9861. nd = (TBL_NPC*)map_id2nd(st->oid);
  9862. if( nd == NULL ) {
  9863. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  9864. } else if( nd->subtype != SCRIPT ) {
  9865. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  9866. } else {
  9867. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  9868. nd->u.scr.guild_id = g_id;
  9869. clif_guild_emblem_area(&nd->bl);
  9870. /* guild flag caching */
  9871. if( g_id ) /* adding a id */
  9872. guild_flag_add(nd);
  9873. else if( changed ) /* removing a flag */
  9874. guild_flag_remove(nd);
  9875. }
  9876. return 0;
  9877. }
  9878. BUILDIN_FUNC(getcastlename)
  9879. {
  9880. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9881. struct guild_castle* gc = guild_mapname2gc(mapname);
  9882. const char* name = (gc) ? gc->castle_name : "";
  9883. script_pushstrcopy(st,name);
  9884. return 0;
  9885. }
  9886. BUILDIN_FUNC(getcastledata)
  9887. {
  9888. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9889. int index = script_getnum(st,3);
  9890. struct guild_castle *gc = guild_mapname2gc(mapname);
  9891. if (gc == NULL) {
  9892. script_pushint(st,0);
  9893. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9894. return 1;
  9895. }
  9896. switch (index) {
  9897. case 1:
  9898. script_pushint(st,gc->guild_id); break;
  9899. case 2:
  9900. script_pushint(st,gc->economy); break;
  9901. case 3:
  9902. script_pushint(st,gc->defense); break;
  9903. case 4:
  9904. script_pushint(st,gc->triggerE); break;
  9905. case 5:
  9906. script_pushint(st,gc->triggerD); break;
  9907. case 6:
  9908. script_pushint(st,gc->nextTime); break;
  9909. case 7:
  9910. script_pushint(st,gc->payTime); break;
  9911. case 8:
  9912. script_pushint(st,gc->createTime); break;
  9913. case 9:
  9914. script_pushint(st,gc->visibleC); break;
  9915. default:
  9916. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  9917. script_pushint(st,gc->guardian[index-10].visible);
  9918. break;
  9919. }
  9920. script_pushint(st,0);
  9921. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9922. return 1;
  9923. }
  9924. return 0;
  9925. }
  9926. BUILDIN_FUNC(setcastledata)
  9927. {
  9928. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9929. int index = script_getnum(st,3);
  9930. int value = script_getnum(st,4);
  9931. struct guild_castle *gc = guild_mapname2gc(mapname);
  9932. if (gc == NULL) {
  9933. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  9934. return 1;
  9935. }
  9936. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  9937. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  9938. return 1;
  9939. }
  9940. guild_castledatasave(gc->castle_id, index, value);
  9941. return 0;
  9942. }
  9943. /* =====================================================================
  9944. * ---------------------------------------------------------------------*/
  9945. BUILDIN_FUNC(requestguildinfo)
  9946. {
  9947. int guild_id=script_getnum(st,2);
  9948. const char *event=NULL;
  9949. if( script_hasdata(st,3) ){
  9950. event=script_getstr(st,3);
  9951. check_event(st, event);
  9952. }
  9953. if(guild_id>0)
  9954. guild_npc_request_info(guild_id,event);
  9955. return 0;
  9956. }
  9957. /// Returns the number of cards that have been compounded onto the specified equipped item.
  9958. /// getequipcardcnt(<equipment slot>);
  9959. BUILDIN_FUNC(getequipcardcnt)
  9960. {
  9961. int i=-1,j,num;
  9962. TBL_PC *sd;
  9963. int count;
  9964. num=script_getnum(st,2);
  9965. sd=script_rid2sd(st);
  9966. if (num > 0 && num <= ARRAYLENGTH(equip))
  9967. i=pc_checkequip(sd,equip[num-1]);
  9968. if (i < 0 || !sd->inventory_data[i]) {
  9969. script_pushint(st,0);
  9970. return 0;
  9971. }
  9972. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9973. {
  9974. script_pushint(st,0);
  9975. return 0;
  9976. }
  9977. count = 0;
  9978. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  9979. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  9980. count++;
  9981. script_pushint(st,count);
  9982. return 0;
  9983. }
  9984. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  9985. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  9986. /// successremovecards <slot>;
  9987. BUILDIN_FUNC(successremovecards) {
  9988. int i=-1,j,c,cardflag=0;
  9989. TBL_PC* sd = script_rid2sd(st);
  9990. int num = script_getnum(st,2);
  9991. if (num > 0 && num <= ARRAYLENGTH(equip))
  9992. i=pc_checkequip(sd,equip[num-1]);
  9993. if (i < 0 || !sd->inventory_data[i]) {
  9994. return 0;
  9995. }
  9996. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  9997. return 0;
  9998. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  9999. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10000. int flag;
  10001. struct item item_tmp;
  10002. memset(&item_tmp,0,sizeof(item_tmp));
  10003. cardflag = 1;
  10004. item_tmp.nameid = sd->status.inventory[i].card[c];
  10005. item_tmp.identify = 1;
  10006. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10007. clif_additem(sd,0,0,flag);
  10008. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10009. }
  10010. }
  10011. }
  10012. if(cardflag == 1) {//if card was remove remplace item with no card
  10013. int flag;
  10014. struct item item_tmp;
  10015. memset(&item_tmp,0,sizeof(item_tmp));
  10016. item_tmp.nameid = sd->status.inventory[i].nameid;
  10017. item_tmp.identify = 1;
  10018. item_tmp.refine = sd->status.inventory[i].refine;
  10019. item_tmp.attribute = sd->status.inventory[i].attribute;
  10020. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10021. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10022. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10023. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10024. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10025. clif_additem(sd,0,0,flag);
  10026. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10027. }
  10028. clif_misceffect(&sd->bl,3);
  10029. }
  10030. return 0;
  10031. }
  10032. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10033. /// failedremovecards <slot>, <type>;
  10034. /// <type>=0 : will destroy both the item and the cards.
  10035. /// <type>=1 : will keep the item, but destroy the cards.
  10036. /// <type>=2 : will keep the cards, but destroy the item.
  10037. /// <type>=? : will just display the failure effect.
  10038. BUILDIN_FUNC(failedremovecards) {
  10039. int i=-1,j,c,cardflag=0;
  10040. TBL_PC* sd = script_rid2sd(st);
  10041. int num = script_getnum(st,2);
  10042. int typefail = script_getnum(st,3);
  10043. if (num > 0 && num <= ARRAYLENGTH(equip))
  10044. i=pc_checkequip(sd,equip[num-1]);
  10045. if (i < 0 || !sd->inventory_data[i])
  10046. return 0;
  10047. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10048. return 0;
  10049. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10050. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10051. cardflag = 1;
  10052. if(typefail == 2) {// add cards to inventory, clear
  10053. int flag;
  10054. struct item item_tmp;
  10055. memset(&item_tmp,0,sizeof(item_tmp));
  10056. item_tmp.nameid = sd->status.inventory[i].card[c];
  10057. item_tmp.identify = 1;
  10058. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10059. clif_additem(sd,0,0,flag);
  10060. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10061. }
  10062. }
  10063. }
  10064. }
  10065. if(cardflag == 1) {
  10066. if(typefail == 0 || typefail == 2){ // destroy the item
  10067. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10068. }
  10069. if(typefail == 1){ // destroy the card
  10070. int flag;
  10071. struct item item_tmp;
  10072. memset(&item_tmp,0,sizeof(item_tmp));
  10073. item_tmp.nameid = sd->status.inventory[i].nameid;
  10074. item_tmp.identify = 1;
  10075. item_tmp.refine = sd->status.inventory[i].refine;
  10076. item_tmp.attribute = sd->status.inventory[i].attribute;
  10077. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10078. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10079. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10080. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10081. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10082. clif_additem(sd,0,0,flag);
  10083. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10084. }
  10085. }
  10086. clif_misceffect(&sd->bl,2);
  10087. }
  10088. return 0;
  10089. }
  10090. /* ================================================================
  10091. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  10092. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  10093. * improved by [Lance]
  10094. * ================================================================*/
  10095. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  10096. {
  10097. int x,y,m,check_val=0,check_ID=0,i=0;
  10098. struct guild *g = NULL;
  10099. struct party_data *p = NULL;
  10100. const char *str;
  10101. const char *mapname;
  10102. unsigned int index;
  10103. mapname=script_getstr(st,2);
  10104. str=script_getstr(st,3);
  10105. x=script_getnum(st,4);
  10106. y=script_getnum(st,5);
  10107. if(script_hasdata(st,7)){
  10108. check_val=script_getnum(st,6);
  10109. check_ID=script_getnum(st,7);
  10110. }
  10111. if((m=map_mapname2mapid(mapname))< 0)
  10112. return 0;
  10113. if(!(index=mapindex_name2id(str)))
  10114. return 0;
  10115. switch(check_val){
  10116. case 1:
  10117. g = guild_search(check_ID);
  10118. if (g){
  10119. for( i=0; i < g->max_member; i++)
  10120. {
  10121. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  10122. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  10123. }
  10124. }
  10125. }
  10126. break;
  10127. case 2:
  10128. p = party_search(check_ID);
  10129. if(p){
  10130. for(i=0;i<MAX_PARTY; i++){
  10131. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  10132. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  10133. }
  10134. }
  10135. }
  10136. break;
  10137. default:
  10138. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  10139. break;
  10140. }
  10141. return 0;
  10142. }
  10143. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  10144. {
  10145. char *event=va_arg(ap,char *);
  10146. struct mob_data *md = ((struct mob_data *)bl);
  10147. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  10148. return 1;
  10149. return 0;
  10150. }
  10151. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  10152. {
  10153. const char *mapname,*event;
  10154. int16 m;
  10155. mapname=script_getstr(st,2);
  10156. event=script_getstr(st,3);
  10157. if( strcmp(event, "all") == 0 )
  10158. event = NULL;
  10159. else
  10160. check_event(st, event);
  10161. if( strcmp(mapname, "this") == 0 ) {
  10162. struct map_session_data *sd = script_rid2sd(st);
  10163. if( sd )
  10164. m = sd->bl.m;
  10165. else {
  10166. script_pushint(st,-1);
  10167. return 0;
  10168. }
  10169. }
  10170. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  10171. script_pushint(st,-1);
  10172. return 0;
  10173. }
  10174. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  10175. {
  10176. script_pushint(st,-1);
  10177. return 0;
  10178. }
  10179. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  10180. return 0;
  10181. }
  10182. BUILDIN_FUNC(marriage)
  10183. {
  10184. const char *partner=script_getstr(st,2);
  10185. TBL_PC *sd=script_rid2sd(st);
  10186. TBL_PC *p_sd=map_nick2sd(partner);
  10187. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  10188. script_pushint(st,0);
  10189. return 0;
  10190. }
  10191. script_pushint(st,1);
  10192. return 0;
  10193. }
  10194. BUILDIN_FUNC(wedding_effect)
  10195. {
  10196. TBL_PC *sd=script_rid2sd(st);
  10197. struct block_list *bl;
  10198. if(sd==NULL) {
  10199. bl=map_id2bl(st->oid);
  10200. } else
  10201. bl=&sd->bl;
  10202. clif_wedding_effect(bl);
  10203. return 0;
  10204. }
  10205. BUILDIN_FUNC(divorce)
  10206. {
  10207. TBL_PC *sd=script_rid2sd(st);
  10208. if(sd==NULL || pc_divorce(sd) < 0){
  10209. script_pushint(st,0);
  10210. return 0;
  10211. }
  10212. script_pushint(st,1);
  10213. return 0;
  10214. }
  10215. BUILDIN_FUNC(ispartneron)
  10216. {
  10217. TBL_PC *sd=script_rid2sd(st);
  10218. if(sd==NULL || !pc_ismarried(sd) ||
  10219. map_charid2sd(sd->status.partner_id) == NULL) {
  10220. script_pushint(st,0);
  10221. return 0;
  10222. }
  10223. script_pushint(st,1);
  10224. return 0;
  10225. }
  10226. BUILDIN_FUNC(getpartnerid)
  10227. {
  10228. TBL_PC *sd=script_rid2sd(st);
  10229. if (sd == NULL) {
  10230. script_pushint(st,0);
  10231. return 0;
  10232. }
  10233. script_pushint(st,sd->status.partner_id);
  10234. return 0;
  10235. }
  10236. BUILDIN_FUNC(getchildid)
  10237. {
  10238. TBL_PC *sd=script_rid2sd(st);
  10239. if (sd == NULL) {
  10240. script_pushint(st,0);
  10241. return 0;
  10242. }
  10243. script_pushint(st,sd->status.child);
  10244. return 0;
  10245. }
  10246. BUILDIN_FUNC(getmotherid)
  10247. {
  10248. TBL_PC *sd=script_rid2sd(st);
  10249. if (sd == NULL) {
  10250. script_pushint(st,0);
  10251. return 0;
  10252. }
  10253. script_pushint(st,sd->status.mother);
  10254. return 0;
  10255. }
  10256. BUILDIN_FUNC(getfatherid)
  10257. {
  10258. TBL_PC *sd=script_rid2sd(st);
  10259. if (sd == NULL) {
  10260. script_pushint(st,0);
  10261. return 0;
  10262. }
  10263. script_pushint(st,sd->status.father);
  10264. return 0;
  10265. }
  10266. BUILDIN_FUNC(warppartner)
  10267. {
  10268. int x,y;
  10269. unsigned short mapindex;
  10270. const char *str;
  10271. TBL_PC *sd=script_rid2sd(st);
  10272. TBL_PC *p_sd=NULL;
  10273. if(sd==NULL || !pc_ismarried(sd) ||
  10274. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  10275. script_pushint(st,0);
  10276. return 0;
  10277. }
  10278. str=script_getstr(st,2);
  10279. x=script_getnum(st,3);
  10280. y=script_getnum(st,4);
  10281. mapindex = mapindex_name2id(str);
  10282. if (mapindex) {
  10283. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  10284. script_pushint(st,1);
  10285. } else
  10286. script_pushint(st,0);
  10287. return 0;
  10288. }
  10289. /*================================================
  10290. * Script for Displaying MOB Information [Valaris]
  10291. *------------------------------------------------*/
  10292. BUILDIN_FUNC(strmobinfo)
  10293. {
  10294. int num=script_getnum(st,2);
  10295. int class_=script_getnum(st,3);
  10296. if(!mobdb_checkid(class_))
  10297. {
  10298. if (num < 3) //requested a string
  10299. script_pushconststr(st,"");
  10300. else
  10301. script_pushint(st,0);
  10302. return 0;
  10303. }
  10304. switch (num) {
  10305. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  10306. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  10307. case 3: script_pushint(st,mob_db(class_)->lv); break;
  10308. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  10309. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  10310. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  10311. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  10312. default:
  10313. script_pushint(st,0);
  10314. break;
  10315. }
  10316. return 0;
  10317. }
  10318. /*==========================================
  10319. * Summon guardians [Valaris]
  10320. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  10321. *------------------------------------------*/
  10322. BUILDIN_FUNC(guardian)
  10323. {
  10324. int class_=0,x=0,y=0,guardian=0;
  10325. const char *str,*map,*evt="";
  10326. struct script_data *data;
  10327. bool has_index = false;
  10328. map =script_getstr(st,2);
  10329. x =script_getnum(st,3);
  10330. y =script_getnum(st,4);
  10331. str =script_getstr(st,5);
  10332. class_=script_getnum(st,6);
  10333. if( script_hasdata(st,8) )
  10334. {// "<event label>",<guardian index>
  10335. evt=script_getstr(st,7);
  10336. guardian=script_getnum(st,8);
  10337. has_index = true;
  10338. } else if( script_hasdata(st,7) ){
  10339. data=script_getdata(st,7);
  10340. get_val(st,data);
  10341. if( data_isstring(data) )
  10342. {// "<event label>"
  10343. evt=script_getstr(st,7);
  10344. } else if( data_isint(data) )
  10345. {// <guardian index>
  10346. guardian=script_getnum(st,7);
  10347. has_index = true;
  10348. } else {
  10349. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  10350. script_reportdata(data);
  10351. return 1;
  10352. }
  10353. }
  10354. check_event(st, evt);
  10355. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  10356. return 0;
  10357. }
  10358. /*==========================================
  10359. * Invisible Walls [Zephyrus]
  10360. *------------------------------------------*/
  10361. BUILDIN_FUNC(setwall)
  10362. {
  10363. const char *map, *name;
  10364. int x, y, m, size, dir;
  10365. bool shootable;
  10366. map = script_getstr(st,2);
  10367. x = script_getnum(st,3);
  10368. y = script_getnum(st,4);
  10369. size = script_getnum(st,5);
  10370. dir = script_getnum(st,6);
  10371. shootable = script_getnum(st,7);
  10372. name = script_getstr(st,8);
  10373. if( (m = map_mapname2mapid(map)) < 0 )
  10374. return 0; // Invalid Map
  10375. map_iwall_set(m, x, y, size, dir, shootable, name);
  10376. return 0;
  10377. }
  10378. BUILDIN_FUNC(delwall)
  10379. {
  10380. const char *name = script_getstr(st,2);
  10381. map_iwall_remove(name);
  10382. return 0;
  10383. }
  10384. /// Retrieves various information about the specified guardian.
  10385. ///
  10386. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  10387. /// type: 0 - whether it is deployed or not
  10388. /// 1 - maximum hp
  10389. /// 2 - current hp
  10390. ///
  10391. BUILDIN_FUNC(guardianinfo)
  10392. {
  10393. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10394. int id = script_getnum(st,3);
  10395. int type = script_getnum(st,4);
  10396. struct guild_castle* gc = guild_mapname2gc(mapname);
  10397. struct mob_data* gd;
  10398. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  10399. {
  10400. script_pushint(st,-1);
  10401. return 0;
  10402. }
  10403. if( type == 0 )
  10404. script_pushint(st, gc->guardian[id].visible);
  10405. else
  10406. if( !gc->guardian[id].visible )
  10407. script_pushint(st,-1);
  10408. else
  10409. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  10410. script_pushint(st,-1);
  10411. else
  10412. {
  10413. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  10414. else if( type == 2 ) script_pushint(st,gd->status.hp);
  10415. else
  10416. script_pushint(st,-1);
  10417. }
  10418. return 0;
  10419. }
  10420. /*==========================================
  10421. * Get the item name by item_id or null
  10422. *------------------------------------------*/
  10423. BUILDIN_FUNC(getitemname)
  10424. {
  10425. int item_id=0;
  10426. struct item_data *i_data;
  10427. char *item_name;
  10428. struct script_data *data;
  10429. data=script_getdata(st,2);
  10430. get_val(st,data);
  10431. if( data_isstring(data) ){
  10432. const char *name=conv_str(st,data);
  10433. struct item_data *item_data = itemdb_searchname(name);
  10434. if( item_data )
  10435. item_id=item_data->nameid;
  10436. }else
  10437. item_id=conv_num(st,data);
  10438. i_data = itemdb_exists(item_id);
  10439. if (i_data == NULL)
  10440. {
  10441. script_pushconststr(st,"null");
  10442. return 0;
  10443. }
  10444. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  10445. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  10446. script_pushstr(st,item_name);
  10447. return 0;
  10448. }
  10449. /*==========================================
  10450. * Returns number of slots an item has. [Skotlex]
  10451. *------------------------------------------*/
  10452. BUILDIN_FUNC(getitemslots)
  10453. {
  10454. int item_id;
  10455. struct item_data *i_data;
  10456. item_id=script_getnum(st,2);
  10457. i_data = itemdb_exists(item_id);
  10458. if (i_data)
  10459. script_pushint(st,i_data->slot);
  10460. else
  10461. script_pushint(st,-1);
  10462. return 0;
  10463. }
  10464. // TODO: add matk here if needed/once we get rid of RENEWAL
  10465. /*==========================================
  10466. * Returns some values of an item [Lupus]
  10467. * Price, Weight, etc...
  10468. getiteminfo(itemID,n), where n
  10469. 0 value_buy;
  10470. 1 value_sell;
  10471. 2 type;
  10472. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10473. if = 0, then monsters don't drop it at all (rare or a quest item)
  10474. if = -1, then this item is sold in NPC shops only
  10475. 4 sex;
  10476. 5 equip;
  10477. 6 weight;
  10478. 7 atk;
  10479. 8 def;
  10480. 9 range;
  10481. 10 slot;
  10482. 11 look;
  10483. 12 elv;
  10484. 13 wlv;
  10485. 14 view id
  10486. *------------------------------------------*/
  10487. BUILDIN_FUNC(getiteminfo)
  10488. {
  10489. int item_id,n;
  10490. int *item_arr;
  10491. struct item_data *i_data;
  10492. item_id = script_getnum(st,2);
  10493. n = script_getnum(st,3);
  10494. i_data = itemdb_exists(item_id);
  10495. if (i_data && n>=0 && n<=14) {
  10496. item_arr = (int*)&i_data->value_buy;
  10497. script_pushint(st,item_arr[n]);
  10498. } else
  10499. script_pushint(st,-1);
  10500. return 0;
  10501. }
  10502. /*==========================================
  10503. * Set some values of an item [Lupus]
  10504. * Price, Weight, etc...
  10505. setiteminfo(itemID,n,Value), where n
  10506. 0 value_buy;
  10507. 1 value_sell;
  10508. 2 type;
  10509. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  10510. if = 0, then monsters don't drop it at all (rare or a quest item)
  10511. if = -1, then this item is sold in NPC shops only
  10512. 4 sex;
  10513. 5 equip;
  10514. 6 weight;
  10515. 7 atk;
  10516. 8 def;
  10517. 9 range;
  10518. 10 slot;
  10519. 11 look;
  10520. 12 elv;
  10521. 13 wlv;
  10522. 14 view id
  10523. * Returns Value or -1 if the wrong field's been set
  10524. *------------------------------------------*/
  10525. BUILDIN_FUNC(setiteminfo)
  10526. {
  10527. int item_id,n,value;
  10528. int *item_arr;
  10529. struct item_data *i_data;
  10530. item_id = script_getnum(st,2);
  10531. n = script_getnum(st,3);
  10532. value = script_getnum(st,4);
  10533. i_data = itemdb_exists(item_id);
  10534. if (i_data && n>=0 && n<=14) {
  10535. item_arr = (int*)&i_data->value_buy;
  10536. item_arr[n] = value;
  10537. script_pushint(st,value);
  10538. } else
  10539. script_pushint(st,-1);
  10540. return 0;
  10541. }
  10542. /*==========================================
  10543. * Returns value from equipped item slot n [Lupus]
  10544. getequipcardid(num,slot)
  10545. where
  10546. num = eqip position slot
  10547. slot = 0,1,2,3 (Card Slot N)
  10548. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  10549. it's useful when you want to check item cards or if it's signed
  10550. Useful for such quests as "Sign this refined item with players name" etc
  10551. Hat[0] +4 -> Player's Hat[0] +4
  10552. *------------------------------------------*/
  10553. BUILDIN_FUNC(getequipcardid)
  10554. {
  10555. int i=-1,num,slot;
  10556. TBL_PC *sd;
  10557. num=script_getnum(st,2);
  10558. slot=script_getnum(st,3);
  10559. sd=script_rid2sd(st);
  10560. if (num > 0 && num <= ARRAYLENGTH(equip))
  10561. i=pc_checkequip(sd,equip[num-1]);
  10562. if(i >= 0 && slot>=0 && slot<4)
  10563. script_pushint(st,sd->status.inventory[i].card[slot]);
  10564. else
  10565. script_pushint(st,0);
  10566. return 0;
  10567. }
  10568. /*==========================================
  10569. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  10570. *------------------------------------------*/
  10571. BUILDIN_FUNC(petskillbonus)
  10572. {
  10573. struct pet_data *pd;
  10574. TBL_PC *sd=script_rid2sd(st);
  10575. if(sd==NULL || sd->pd==NULL)
  10576. return 0;
  10577. pd=sd->pd;
  10578. if (pd->bonus)
  10579. { //Clear previous bonus
  10580. if (pd->bonus->timer != INVALID_TIMER)
  10581. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  10582. } else //init
  10583. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  10584. pd->bonus->type=script_getnum(st,2);
  10585. pd->bonus->val=script_getnum(st,3);
  10586. pd->bonus->duration=script_getnum(st,4);
  10587. pd->bonus->delay=script_getnum(st,5);
  10588. if (pd->state.skillbonus == 1)
  10589. pd->state.skillbonus=0; // waiting state
  10590. // wait for timer to start
  10591. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10592. pd->bonus->timer = INVALID_TIMER;
  10593. else
  10594. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  10595. return 0;
  10596. }
  10597. /*==========================================
  10598. * pet looting [Valaris] //Rewritten by [Skotlex]
  10599. *------------------------------------------*/
  10600. BUILDIN_FUNC(petloot)
  10601. {
  10602. int max;
  10603. struct pet_data *pd;
  10604. TBL_PC *sd=script_rid2sd(st);
  10605. if(sd==NULL || sd->pd==NULL)
  10606. return 0;
  10607. max=script_getnum(st,2);
  10608. if(max < 1)
  10609. max = 1; //Let'em loot at least 1 item.
  10610. else if (max > MAX_PETLOOT_SIZE)
  10611. max = MAX_PETLOOT_SIZE;
  10612. pd = sd->pd;
  10613. if (pd->loot != NULL)
  10614. { //Release whatever was there already and reallocate memory
  10615. pet_lootitem_drop(pd, pd->msd);
  10616. aFree(pd->loot->item);
  10617. }
  10618. else
  10619. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  10620. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  10621. pd->loot->max=max;
  10622. pd->loot->count = 0;
  10623. pd->loot->weight = 0;
  10624. return 0;
  10625. }
  10626. /*==========================================
  10627. * Set arrays with info of all sd inventory :
  10628. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  10629. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  10630. * @inventorylist_card(0..3), @inventorylist_expire
  10631. * @inventorylist_count = scalar
  10632. *------------------------------------------*/
  10633. BUILDIN_FUNC(getinventorylist)
  10634. {
  10635. TBL_PC *sd=script_rid2sd(st);
  10636. char card_var[NAME_LENGTH];
  10637. int i,j=0,k;
  10638. if(!sd) return 0;
  10639. for(i=0;i<MAX_INVENTORY;i++){
  10640. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  10641. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  10642. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  10643. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  10644. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  10645. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  10646. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  10647. for (k = 0; k < MAX_SLOTS; k++)
  10648. {
  10649. sprintf(card_var, "@inventorylist_card%d",k+1);
  10650. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  10651. }
  10652. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  10653. j++;
  10654. }
  10655. }
  10656. pc_setreg(sd,add_str("@inventorylist_count"),j);
  10657. return 0;
  10658. }
  10659. BUILDIN_FUNC(getskilllist)
  10660. {
  10661. TBL_PC *sd=script_rid2sd(st);
  10662. int i,j=0;
  10663. if(!sd) return 0;
  10664. for(i=0;i<MAX_SKILL;i++){
  10665. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  10666. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  10667. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  10668. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  10669. j++;
  10670. }
  10671. }
  10672. pc_setreg(sd,add_str("@skilllist_count"),j);
  10673. return 0;
  10674. }
  10675. BUILDIN_FUNC(clearitem)
  10676. {
  10677. TBL_PC *sd=script_rid2sd(st);
  10678. int i;
  10679. if(sd==NULL) return 0;
  10680. for (i=0; i<MAX_INVENTORY; i++) {
  10681. if (sd->status.inventory[i].amount) {
  10682. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  10683. }
  10684. }
  10685. return 0;
  10686. }
  10687. /*==========================================
  10688. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  10689. *------------------------------------------*/
  10690. BUILDIN_FUNC(disguise)
  10691. {
  10692. int id;
  10693. TBL_PC* sd = script_rid2sd(st);
  10694. if (sd == NULL) return 0;
  10695. id = script_getnum(st,2);
  10696. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  10697. pc_disguise(sd, id);
  10698. script_pushint(st,id);
  10699. } else
  10700. script_pushint(st,0);
  10701. return 0;
  10702. }
  10703. /*==========================================
  10704. * Undisguise Player (returns 1 if success, 0 on fail)
  10705. *------------------------------------------*/
  10706. BUILDIN_FUNC(undisguise)
  10707. {
  10708. TBL_PC* sd = script_rid2sd(st);
  10709. if (sd == NULL) return 0;
  10710. if (sd->disguise) {
  10711. pc_disguise(sd, 0);
  10712. script_pushint(st,0);
  10713. } else {
  10714. script_pushint(st,1);
  10715. }
  10716. return 0;
  10717. }
  10718. /*==========================================
  10719. * Transform a bl to another _class,
  10720. * @type unused
  10721. *------------------------------------------*/
  10722. BUILDIN_FUNC(classchange)
  10723. {
  10724. int _class,type;
  10725. struct block_list *bl=map_id2bl(st->oid);
  10726. if(bl==NULL) return 0;
  10727. _class=script_getnum(st,2);
  10728. type=script_getnum(st,3);
  10729. clif_class_change(bl,_class,type);
  10730. return 0;
  10731. }
  10732. /*==========================================
  10733. * Display an effect
  10734. *------------------------------------------*/
  10735. BUILDIN_FUNC(misceffect)
  10736. {
  10737. int type;
  10738. type=script_getnum(st,2);
  10739. if(st->oid && st->oid != fake_nd->bl.id) {
  10740. struct block_list *bl = map_id2bl(st->oid);
  10741. if (bl)
  10742. clif_specialeffect(bl,type,AREA);
  10743. } else{
  10744. TBL_PC *sd=script_rid2sd(st);
  10745. if(sd)
  10746. clif_specialeffect(&sd->bl,type,AREA);
  10747. }
  10748. return 0;
  10749. }
  10750. /*==========================================
  10751. * Play a BGM on a single client [Rikter/Yommy]
  10752. *------------------------------------------*/
  10753. BUILDIN_FUNC(playBGM)
  10754. {
  10755. const char* name;
  10756. struct map_session_data* sd;
  10757. if( ( sd = script_rid2sd(st) ) != NULL )
  10758. {
  10759. name = script_getstr(st,2);
  10760. clif_playBGM(sd, name);
  10761. }
  10762. return 0;
  10763. }
  10764. static int playBGM_sub(struct block_list* bl,va_list ap)
  10765. {
  10766. const char* name = va_arg(ap,const char*);
  10767. clif_playBGM(BL_CAST(BL_PC, bl), name);
  10768. return 0;
  10769. }
  10770. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  10771. {
  10772. const char* name = va_arg(args, const char*);
  10773. clif_playBGM(sd, name);
  10774. return 0;
  10775. }
  10776. /*==========================================
  10777. * Play a BGM on multiple client [Rikter/Yommy]
  10778. *------------------------------------------*/
  10779. BUILDIN_FUNC(playBGMall)
  10780. {
  10781. const char* name;
  10782. name = script_getstr(st,2);
  10783. if( script_hasdata(st,7) )
  10784. {// specified part of map
  10785. const char* map = script_getstr(st,3);
  10786. int x0 = script_getnum(st,4);
  10787. int y0 = script_getnum(st,5);
  10788. int x1 = script_getnum(st,6);
  10789. int y1 = script_getnum(st,7);
  10790. map_foreachinarea(playBGM_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name);
  10791. }
  10792. else if( script_hasdata(st,3) )
  10793. {// entire map
  10794. const char* map = script_getstr(st,3);
  10795. map_foreachinmap(playBGM_sub, map_mapname2mapid(map), BL_PC, name);
  10796. }
  10797. else
  10798. {// entire server
  10799. map_foreachpc(&playBGM_foreachpc_sub, name);
  10800. }
  10801. return 0;
  10802. }
  10803. /*==========================================
  10804. * Play a .wav sound for sd
  10805. *------------------------------------------*/
  10806. BUILDIN_FUNC(soundeffect)
  10807. {
  10808. TBL_PC* sd = script_rid2sd(st);
  10809. const char* name = script_getstr(st,2);
  10810. int type = script_getnum(st,3);
  10811. if(sd)
  10812. {
  10813. clif_soundeffect(sd,&sd->bl,name,type);
  10814. }
  10815. return 0;
  10816. }
  10817. int soundeffect_sub(struct block_list* bl,va_list ap)
  10818. {
  10819. char* name = va_arg(ap,char*);
  10820. int type = va_arg(ap,int);
  10821. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  10822. return 0;
  10823. }
  10824. /*==========================================
  10825. * Play a sound effect (.wav) on multiple clients
  10826. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  10827. *------------------------------------------*/
  10828. BUILDIN_FUNC(soundeffectall)
  10829. {
  10830. struct block_list* bl;
  10831. const char* name;
  10832. int type;
  10833. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  10834. if (!bl)
  10835. return 0;
  10836. name = script_getstr(st,2);
  10837. type = script_getnum(st,3);
  10838. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  10839. if(!script_hasdata(st,4))
  10840. { // area around
  10841. clif_soundeffectall(bl, name, type, AREA);
  10842. }
  10843. else
  10844. if(!script_hasdata(st,5))
  10845. { // entire map
  10846. const char* map = script_getstr(st,4);
  10847. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  10848. }
  10849. else
  10850. if(script_hasdata(st,8))
  10851. { // specified part of map
  10852. const char* map = script_getstr(st,4);
  10853. int x0 = script_getnum(st,5);
  10854. int y0 = script_getnum(st,6);
  10855. int x1 = script_getnum(st,7);
  10856. int y1 = script_getnum(st,8);
  10857. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  10858. }
  10859. else
  10860. {
  10861. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  10862. }
  10863. return 0;
  10864. }
  10865. /*==========================================
  10866. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  10867. *------------------------------------------*/
  10868. BUILDIN_FUNC(petrecovery)
  10869. {
  10870. struct pet_data *pd;
  10871. TBL_PC *sd=script_rid2sd(st);
  10872. if(sd==NULL || sd->pd==NULL)
  10873. return 0;
  10874. pd=sd->pd;
  10875. if (pd->recovery)
  10876. { //Halt previous bonus
  10877. if (pd->recovery->timer != INVALID_TIMER)
  10878. delete_timer(pd->recovery->timer, pet_recovery_timer);
  10879. } else //Init
  10880. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  10881. pd->recovery->type = (sc_type)script_getnum(st,2);
  10882. pd->recovery->delay = script_getnum(st,3);
  10883. pd->recovery->timer = INVALID_TIMER;
  10884. return 0;
  10885. }
  10886. /*==========================================
  10887. * pet healing [Valaris] //Rewritten by [Skotlex]
  10888. *------------------------------------------*/
  10889. BUILDIN_FUNC(petheal)
  10890. {
  10891. struct pet_data *pd;
  10892. TBL_PC *sd=script_rid2sd(st);
  10893. if(sd==NULL || sd->pd==NULL)
  10894. return 0;
  10895. pd=sd->pd;
  10896. if (pd->s_skill)
  10897. { //Clear previous skill
  10898. if (pd->s_skill->timer != INVALID_TIMER)
  10899. {
  10900. if (pd->s_skill->id)
  10901. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10902. else
  10903. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10904. }
  10905. } else //init memory
  10906. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10907. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  10908. //Use the lv as the amount to heal
  10909. pd->s_skill->lv=script_getnum(st,2);
  10910. pd->s_skill->delay=script_getnum(st,3);
  10911. pd->s_skill->hp=script_getnum(st,4);
  10912. pd->s_skill->sp=script_getnum(st,5);
  10913. //Use delay as initial offset to avoid skill/heal exploits
  10914. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10915. pd->s_skill->timer = INVALID_TIMER;
  10916. else
  10917. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  10918. return 0;
  10919. }
  10920. /*==========================================
  10921. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  10922. *------------------------------------------*/
  10923. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  10924. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  10925. BUILDIN_FUNC(petskillattack)
  10926. {
  10927. struct pet_data *pd;
  10928. TBL_PC *sd=script_rid2sd(st);
  10929. if(sd==NULL || sd->pd==NULL)
  10930. return 0;
  10931. pd=sd->pd;
  10932. if (pd->a_skill == NULL)
  10933. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10934. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10935. pd->a_skill->lv=script_getnum(st,3);
  10936. pd->a_skill->div_ = 0;
  10937. pd->a_skill->rate=script_getnum(st,4);
  10938. pd->a_skill->bonusrate=script_getnum(st,5);
  10939. return 0;
  10940. }
  10941. /*==========================================
  10942. * pet attack skills [Valaris]
  10943. *------------------------------------------*/
  10944. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  10945. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  10946. BUILDIN_FUNC(petskillattack2)
  10947. {
  10948. struct pet_data *pd;
  10949. TBL_PC *sd=script_rid2sd(st);
  10950. if(sd==NULL || sd->pd==NULL)
  10951. return 0;
  10952. pd=sd->pd;
  10953. if (pd->a_skill == NULL)
  10954. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  10955. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10956. pd->a_skill->lv=script_getnum(st,3);
  10957. pd->a_skill->div_ = script_getnum(st,4);
  10958. pd->a_skill->rate=script_getnum(st,5);
  10959. pd->a_skill->bonusrate=script_getnum(st,6);
  10960. return 0;
  10961. }
  10962. /*==========================================
  10963. * pet support skills [Skotlex]
  10964. *------------------------------------------*/
  10965. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  10966. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  10967. BUILDIN_FUNC(petskillsupport)
  10968. {
  10969. struct pet_data *pd;
  10970. TBL_PC *sd=script_rid2sd(st);
  10971. if(sd==NULL || sd->pd==NULL)
  10972. return 0;
  10973. pd=sd->pd;
  10974. if (pd->s_skill)
  10975. { //Clear previous skill
  10976. if (pd->s_skill->timer != INVALID_TIMER)
  10977. {
  10978. if (pd->s_skill->id)
  10979. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  10980. else
  10981. delete_timer(pd->s_skill->timer, pet_heal_timer);
  10982. }
  10983. } else //init memory
  10984. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  10985. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  10986. pd->s_skill->lv=script_getnum(st,3);
  10987. pd->s_skill->delay=script_getnum(st,4);
  10988. pd->s_skill->hp=script_getnum(st,5);
  10989. pd->s_skill->sp=script_getnum(st,6);
  10990. //Use delay as initial offset to avoid skill/heal exploits
  10991. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  10992. pd->s_skill->timer = INVALID_TIMER;
  10993. else
  10994. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  10995. return 0;
  10996. }
  10997. /*==========================================
  10998. * Scripted skill effects [Celest]
  10999. *------------------------------------------*/
  11000. /// skilleffect <skill id>,<level>
  11001. /// skilleffect "<skill name>",<level>
  11002. BUILDIN_FUNC(skilleffect)
  11003. {
  11004. TBL_PC *sd;
  11005. uint16 skill_id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11006. uint16 skill_lv=script_getnum(st,3);
  11007. sd=script_rid2sd(st);
  11008. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11009. return 0;
  11010. }
  11011. /*==========================================
  11012. * NPC skill effects [Valaris]
  11013. *------------------------------------------*/
  11014. /// npcskilleffect <skill id>,<level>,<x>,<y>
  11015. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  11016. BUILDIN_FUNC(npcskilleffect)
  11017. {
  11018. struct block_list *bl= map_id2bl(st->oid);
  11019. uint16 skill_id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11020. uint16 skill_lv=script_getnum(st,3);
  11021. int x=script_getnum(st,4);
  11022. int y=script_getnum(st,5);
  11023. if (bl)
  11024. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  11025. return 0;
  11026. }
  11027. /*==========================================
  11028. * Special effects [Valaris]
  11029. *------------------------------------------*/
  11030. BUILDIN_FUNC(specialeffect)
  11031. {
  11032. struct block_list *bl=map_id2bl(st->oid);
  11033. int type = script_getnum(st,2);
  11034. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11035. if(bl==NULL)
  11036. return 0;
  11037. if( script_hasdata(st,4) )
  11038. {
  11039. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11040. if(nd)
  11041. clif_specialeffect(&nd->bl, type, target);
  11042. }
  11043. else
  11044. {
  11045. if (target == SELF) {
  11046. TBL_PC *sd=script_rid2sd(st);
  11047. if (sd)
  11048. clif_specialeffect_single(bl,type,sd->fd);
  11049. } else {
  11050. clif_specialeffect(bl, type, target);
  11051. }
  11052. }
  11053. return 0;
  11054. }
  11055. BUILDIN_FUNC(specialeffect2)
  11056. {
  11057. TBL_PC *sd=script_rid2sd(st);
  11058. int type = script_getnum(st,2);
  11059. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11060. if( script_hasdata(st,4) )
  11061. sd = map_nick2sd(script_getstr(st,4));
  11062. if (sd)
  11063. clif_specialeffect(&sd->bl, type, target);
  11064. return 0;
  11065. }
  11066. /*==========================================
  11067. * Nude [Valaris]
  11068. *------------------------------------------*/
  11069. BUILDIN_FUNC(nude)
  11070. {
  11071. TBL_PC *sd = script_rid2sd(st);
  11072. int i, calcflag = 0;
  11073. if( sd == NULL )
  11074. return 0;
  11075. for( i = 0 ; i < EQI_MAX; i++ ) {
  11076. if( sd->equip_index[ i ] >= 0 ) {
  11077. if( !calcflag )
  11078. calcflag = 1;
  11079. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  11080. }
  11081. }
  11082. if( calcflag )
  11083. status_calc_pc(sd,0);
  11084. return 0;
  11085. }
  11086. /*==========================================
  11087. * gmcommand [MouseJstr]
  11088. *------------------------------------------*/
  11089. BUILDIN_FUNC(atcommand)
  11090. {
  11091. TBL_PC dummy_sd;
  11092. TBL_PC* sd;
  11093. int fd;
  11094. const char* cmd;
  11095. cmd = script_getstr(st,2);
  11096. if (st->rid) {
  11097. sd = script_rid2sd(st);
  11098. fd = sd->fd;
  11099. } else { //Use a dummy character.
  11100. sd = &dummy_sd;
  11101. fd = 0;
  11102. memset(&dummy_sd, 0, sizeof(TBL_PC));
  11103. if (st->oid)
  11104. {
  11105. struct block_list* bl = map_id2bl(st->oid);
  11106. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  11107. if (bl->type == BL_NPC)
  11108. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  11109. }
  11110. }
  11111. if (!is_atcommand(fd, sd, cmd, 0)) {
  11112. ShowWarning("script: buildin_atcommand: failed to execute command '%s'\n", cmd);
  11113. script_reportsrc(st);
  11114. return 1;
  11115. }
  11116. return 0;
  11117. }
  11118. /*==========================================
  11119. * Displays a message for the player only (like system messages like "you got an apple" )
  11120. *------------------------------------------*/
  11121. BUILDIN_FUNC(dispbottom)
  11122. {
  11123. TBL_PC *sd=script_rid2sd(st);
  11124. const char *message;
  11125. message=script_getstr(st,2);
  11126. if(sd)
  11127. clif_disp_onlyself(sd,message,(int)strlen(message));
  11128. return 0;
  11129. }
  11130. /*==========================================
  11131. * All The Players Full Recovery
  11132. * (HP/SP full restore and resurrect if need)
  11133. *------------------------------------------*/
  11134. BUILDIN_FUNC(recovery)
  11135. {
  11136. TBL_PC* sd;
  11137. struct s_mapiterator* iter;
  11138. iter = mapit_getallusers();
  11139. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11140. {
  11141. if(pc_isdead(sd))
  11142. status_revive(&sd->bl, 100, 100);
  11143. else
  11144. status_percent_heal(&sd->bl, 100, 100);
  11145. clif_displaymessage(sd->fd,msg_txt(sd,680));
  11146. }
  11147. mapit_free(iter);
  11148. return 0;
  11149. }
  11150. /*==========================================
  11151. * Get your pet info: getpetinfo(n)
  11152. * n -> 0:pet_id 1:pet_class 2:pet_name
  11153. * 3:friendly 4:hungry, 5: rename flag.
  11154. *------------------------------------------*/
  11155. BUILDIN_FUNC(getpetinfo)
  11156. {
  11157. TBL_PC *sd=script_rid2sd(st);
  11158. TBL_PET *pd;
  11159. int type=script_getnum(st,2);
  11160. if(!sd || !sd->pd) {
  11161. if (type == 2)
  11162. script_pushconststr(st,"null");
  11163. else
  11164. script_pushint(st,0);
  11165. return 0;
  11166. }
  11167. pd = sd->pd;
  11168. switch(type){
  11169. case 0: script_pushint(st,pd->pet.pet_id); break;
  11170. case 1: script_pushint(st,pd->pet.class_); break;
  11171. case 2: script_pushstrcopy(st,pd->pet.name); break;
  11172. case 3: script_pushint(st,pd->pet.intimate); break;
  11173. case 4: script_pushint(st,pd->pet.hungry); break;
  11174. case 5: script_pushint(st,pd->pet.rename_flag); break;
  11175. default:
  11176. script_pushint(st,0);
  11177. break;
  11178. }
  11179. return 0;
  11180. }
  11181. /*==========================================
  11182. * Get your homunculus info: gethominfo(n)
  11183. * n -> 0:hom_id 1:class 2:name
  11184. * 3:friendly 4:hungry, 5: rename flag.
  11185. * 6: level
  11186. *------------------------------------------*/
  11187. BUILDIN_FUNC(gethominfo)
  11188. {
  11189. TBL_PC *sd=script_rid2sd(st);
  11190. TBL_HOM *hd;
  11191. int type=script_getnum(st,2);
  11192. hd = sd?sd->hd:NULL;
  11193. if(!hd) {
  11194. if (type == 2)
  11195. script_pushconststr(st,"null");
  11196. else
  11197. script_pushint(st,0);
  11198. return 0;
  11199. }
  11200. switch(type){
  11201. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  11202. case 1: script_pushint(st,hd->homunculus.class_); break;
  11203. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  11204. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  11205. case 4: script_pushint(st,hd->homunculus.hunger); break;
  11206. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  11207. case 6: script_pushint(st,hd->homunculus.level); break;
  11208. default:
  11209. script_pushint(st,0);
  11210. break;
  11211. }
  11212. return 0;
  11213. }
  11214. /// Retrieves information about character's mercenary
  11215. /// getmercinfo <type>[,<char id>];
  11216. BUILDIN_FUNC(getmercinfo)
  11217. {
  11218. int type, char_id;
  11219. struct map_session_data* sd;
  11220. struct mercenary_data* md;
  11221. type = script_getnum(st,2);
  11222. if( script_hasdata(st,3) )
  11223. {
  11224. char_id = script_getnum(st,3);
  11225. if( ( sd = map_charid2sd(char_id) ) == NULL )
  11226. {
  11227. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  11228. script_pushnil(st);
  11229. return 1;
  11230. }
  11231. }
  11232. else
  11233. {
  11234. if( ( sd = script_rid2sd(st) ) == NULL )
  11235. {
  11236. script_pushnil(st);
  11237. return 0;
  11238. }
  11239. }
  11240. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  11241. switch( type )
  11242. {
  11243. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  11244. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  11245. case 2:
  11246. if( md )
  11247. script_pushstrcopy(st,md->db->name);
  11248. else
  11249. script_pushconststr(st,"");
  11250. break;
  11251. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  11252. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  11253. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  11254. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  11255. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  11256. default:
  11257. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  11258. script_pushnil(st);
  11259. return 1;
  11260. }
  11261. return 0;
  11262. }
  11263. /*==========================================
  11264. * Shows wether your inventory(and equips) contain
  11265. selected card or not.
  11266. checkequipedcard(4001);
  11267. *------------------------------------------*/
  11268. BUILDIN_FUNC(checkequipedcard)
  11269. {
  11270. TBL_PC *sd=script_rid2sd(st);
  11271. if(sd){
  11272. int n,i,c=0;
  11273. c=script_getnum(st,2);
  11274. for(i=0;i<MAX_INVENTORY;i++){
  11275. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  11276. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  11277. continue;
  11278. for(n=0;n<sd->inventory_data[i]->slot;n++){
  11279. if(sd->status.inventory[i].card[n]==c){
  11280. script_pushint(st,1);
  11281. return 0;
  11282. }
  11283. }
  11284. }
  11285. }
  11286. }
  11287. script_pushint(st,0);
  11288. return 0;
  11289. }
  11290. BUILDIN_FUNC(jump_zero)
  11291. {
  11292. int sel;
  11293. sel=script_getnum(st,2);
  11294. if(!sel) {
  11295. int pos;
  11296. if( !data_islabel(script_getdata(st,3)) ){
  11297. ShowError("script: jump_zero: not label !\n");
  11298. st->state=END;
  11299. return 1;
  11300. }
  11301. pos=script_getnum(st,3);
  11302. st->pos=pos;
  11303. st->state=GOTO;
  11304. }
  11305. return 0;
  11306. }
  11307. /*==========================================
  11308. * movenpc [MouseJstr]
  11309. *------------------------------------------*/
  11310. BUILDIN_FUNC(movenpc)
  11311. {
  11312. TBL_NPC *nd = NULL;
  11313. const char *npc;
  11314. int x,y;
  11315. npc = script_getstr(st,2);
  11316. x = script_getnum(st,3);
  11317. y = script_getnum(st,4);
  11318. if ((nd = npc_name2id(npc)) == NULL)
  11319. return -1;
  11320. if (script_hasdata(st,5))
  11321. nd->ud.dir = script_getnum(st,5) % 8;
  11322. npc_movenpc(nd, x, y);
  11323. return 0;
  11324. }
  11325. /*==========================================
  11326. * message [MouseJstr]
  11327. *------------------------------------------*/
  11328. BUILDIN_FUNC(message)
  11329. {
  11330. const char *msg,*player;
  11331. TBL_PC *pl_sd = NULL;
  11332. player = script_getstr(st,2);
  11333. msg = script_getstr(st,3);
  11334. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  11335. return 0;
  11336. clif_displaymessage(pl_sd->fd, msg);
  11337. return 0;
  11338. }
  11339. /*==========================================
  11340. * npctalk (sends message to surrounding area)
  11341. *------------------------------------------*/
  11342. BUILDIN_FUNC(npctalk)
  11343. {
  11344. const char* str;
  11345. char name[NAME_LENGTH], message[256];
  11346. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  11347. str = script_getstr(st,2);
  11348. if(nd)
  11349. {
  11350. safestrncpy(name, nd->name, sizeof(name));
  11351. strtok(name, "#"); // discard extra name identifier if present
  11352. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  11353. clif_disp_overhead(&nd->bl, message);
  11354. }
  11355. return 0;
  11356. }
  11357. // change npc walkspeed [Valaris]
  11358. BUILDIN_FUNC(npcspeed)
  11359. {
  11360. struct npc_data* nd;
  11361. int speed;
  11362. speed = script_getnum(st,2);
  11363. nd =(struct npc_data *)map_id2bl(st->oid);
  11364. if( nd )
  11365. {
  11366. nd->speed = speed;
  11367. nd->ud.state.speed_changed = 1;
  11368. }
  11369. return 0;
  11370. }
  11371. // make an npc walk to a position [Valaris]
  11372. BUILDIN_FUNC(npcwalkto)
  11373. {
  11374. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11375. int x=0,y=0;
  11376. x=script_getnum(st,2);
  11377. y=script_getnum(st,3);
  11378. if(nd) {
  11379. if (!nd->status.hp) {
  11380. status_calc_npc(nd, true);
  11381. } else {
  11382. status_calc_npc(nd, false);
  11383. }
  11384. unit_walktoxy(&nd->bl,x,y,0);
  11385. }
  11386. return 0;
  11387. }
  11388. // stop an npc's movement [Valaris]
  11389. BUILDIN_FUNC(npcstop)
  11390. {
  11391. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  11392. if(nd) {
  11393. unit_stop_walking(&nd->bl,1|4);
  11394. }
  11395. return 0;
  11396. }
  11397. /*==========================================
  11398. * getlook char info. getlook(arg)
  11399. *------------------------------------------*/
  11400. BUILDIN_FUNC(getlook)
  11401. {
  11402. int type,val;
  11403. TBL_PC *sd;
  11404. sd=script_rid2sd(st);
  11405. type=script_getnum(st,2);
  11406. val=-1;
  11407. switch(type) {
  11408. case LOOK_HAIR: val=sd->status.hair; break; //1
  11409. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  11410. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  11411. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  11412. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  11413. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  11414. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  11415. case LOOK_SHIELD: val=sd->status.shield; break; //8
  11416. case LOOK_SHOES: break; //9
  11417. case LOOK_ROBE: val=sd->status.robe; break; //12
  11418. }
  11419. script_pushint(st,val);
  11420. return 0;
  11421. }
  11422. /*==========================================
  11423. * get char save point. argument: 0- map name, 1- x, 2- y
  11424. *------------------------------------------*/
  11425. BUILDIN_FUNC(getsavepoint)
  11426. {
  11427. TBL_PC* sd;
  11428. int type;
  11429. sd = script_rid2sd(st);
  11430. if (sd == NULL) {
  11431. script_pushint(st,0);
  11432. return 0;
  11433. }
  11434. type = script_getnum(st,2);
  11435. switch(type) {
  11436. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  11437. case 1: script_pushint(st,sd->status.save_point.x); break;
  11438. case 2: script_pushint(st,sd->status.save_point.y); break;
  11439. default:
  11440. script_pushint(st,0);
  11441. break;
  11442. }
  11443. return 0;
  11444. }
  11445. /*==========================================
  11446. * Get position for char/npc/pet/mob objects. Added by Lorky
  11447. *
  11448. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  11449. * where type:
  11450. * MapName$ - String variable for output map name
  11451. * MapX - Integer variable for output coord X
  11452. * MapY - Integer variable for output coord Y
  11453. * type - type of object
  11454. * 0 - Character coord
  11455. * 1 - NPC coord
  11456. * 2 - Pet coord
  11457. * 3 - Mob coord (not released)
  11458. * 4 - Homun coord
  11459. * 5 - Mercenary coord
  11460. * 6 - Elemental coord
  11461. * CharName$ - Name object. If miss or "this" the current object
  11462. *
  11463. * Return:
  11464. * 0 - success
  11465. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  11466. *------------------------------------------*/
  11467. BUILDIN_FUNC(getmapxy)
  11468. {
  11469. struct block_list *bl = NULL;
  11470. TBL_PC *sd=NULL;
  11471. int num;
  11472. const char *name;
  11473. char prefix;
  11474. int x,y,type;
  11475. char mapname[MAP_NAME_LENGTH];
  11476. if( !data_isreference(script_getdata(st,2)) ){
  11477. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  11478. script_pushint(st,-1);
  11479. return 1;
  11480. }
  11481. if( !data_isreference(script_getdata(st,3)) ){
  11482. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  11483. script_pushint(st,-1);
  11484. return 1;
  11485. }
  11486. if( !data_isreference(script_getdata(st,4)) ){
  11487. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  11488. script_pushint(st,-1);
  11489. return 1;
  11490. }
  11491. // Possible needly check function parameters on C_STR,C_INT,C_INT
  11492. type=script_getnum(st,5);
  11493. switch (type){
  11494. case 0: //Get Character Position
  11495. if( script_hasdata(st,6) )
  11496. sd=map_nick2sd(script_getstr(st,6));
  11497. else
  11498. sd=script_rid2sd(st);
  11499. if (sd)
  11500. bl = &sd->bl;
  11501. break;
  11502. case 1: //Get NPC Position
  11503. if( script_hasdata(st,6) )
  11504. {
  11505. struct npc_data *nd;
  11506. nd=npc_name2id(script_getstr(st,6));
  11507. if (nd)
  11508. bl = &nd->bl;
  11509. } else //In case the origin is not an npc?
  11510. bl=map_id2bl(st->oid);
  11511. break;
  11512. case 2: //Get Pet Position
  11513. if(script_hasdata(st,6))
  11514. sd=map_nick2sd(script_getstr(st,6));
  11515. else
  11516. sd=script_rid2sd(st);
  11517. if (sd && sd->pd)
  11518. bl = &sd->pd->bl;
  11519. break;
  11520. case 3: //Get Mob Position
  11521. break; //Not supported?
  11522. case 4: //Get Homun Position
  11523. if(script_hasdata(st,6))
  11524. sd=map_nick2sd(script_getstr(st,6));
  11525. else
  11526. sd=script_rid2sd(st);
  11527. if (sd && sd->hd)
  11528. bl = &sd->hd->bl;
  11529. break;
  11530. case 5: //Get Mercenary Position
  11531. if(script_hasdata(st,6))
  11532. sd=map_nick2sd(script_getstr(st,6));
  11533. else
  11534. sd=script_rid2sd(st);
  11535. if (sd && sd->md)
  11536. bl = &sd->md->bl;
  11537. break;
  11538. case 6: //Get Elemental Position
  11539. if(script_hasdata(st,6))
  11540. sd=map_nick2sd(script_getstr(st,6));
  11541. else
  11542. sd=script_rid2sd(st);
  11543. if (sd && sd->ed)
  11544. bl = &sd->ed->bl;
  11545. break;
  11546. default:
  11547. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  11548. script_pushint(st,-1);
  11549. return 1;
  11550. }
  11551. if (!bl) { //No object found.
  11552. script_pushint(st,-1);
  11553. return 0;
  11554. }
  11555. x= bl->x;
  11556. y= bl->y;
  11557. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  11558. //Set MapName$
  11559. num=st->stack->stack_data[st->start+2].u.num;
  11560. name=get_str(num&0x00ffffff);
  11561. prefix=*name;
  11562. if(not_server_variable(prefix))
  11563. sd=script_rid2sd(st);
  11564. else
  11565. sd=NULL;
  11566. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  11567. //Set MapX
  11568. num=st->stack->stack_data[st->start+3].u.num;
  11569. name=get_str(num&0x00ffffff);
  11570. prefix=*name;
  11571. if(not_server_variable(prefix))
  11572. sd=script_rid2sd(st);
  11573. else
  11574. sd=NULL;
  11575. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  11576. //Set MapY
  11577. num=st->stack->stack_data[st->start+4].u.num;
  11578. name=get_str(num&0x00ffffff);
  11579. prefix=*name;
  11580. if(not_server_variable(prefix))
  11581. sd=script_rid2sd(st);
  11582. else
  11583. sd=NULL;
  11584. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  11585. //Return Success value
  11586. script_pushint(st,0);
  11587. return 0;
  11588. }
  11589. /*==========================================
  11590. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  11591. *------------------------------------------*/
  11592. BUILDIN_FUNC(logmes)
  11593. {
  11594. const char *str;
  11595. TBL_PC* sd;
  11596. sd = script_rid2sd(st);
  11597. if( sd == NULL )
  11598. return 1;
  11599. str = script_getstr(st,2);
  11600. log_npc(sd,str);
  11601. return 0;
  11602. }
  11603. BUILDIN_FUNC(summon)
  11604. {
  11605. int _class, timeout=0;
  11606. const char *str,*event="";
  11607. TBL_PC *sd;
  11608. struct mob_data *md;
  11609. int tick = gettick();
  11610. sd=script_rid2sd(st);
  11611. if (!sd) return 0;
  11612. str =script_getstr(st,2);
  11613. _class=script_getnum(st,3);
  11614. if( script_hasdata(st,4) )
  11615. timeout=script_getnum(st,4);
  11616. if( script_hasdata(st,5) ){
  11617. event=script_getstr(st,5);
  11618. check_event(st, event);
  11619. }
  11620. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  11621. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  11622. if (md) {
  11623. md->master_id=sd->bl.id;
  11624. md->special_state.ai = AI_ATTACK;
  11625. if( md->deletetimer != INVALID_TIMER )
  11626. delete_timer(md->deletetimer, mob_timer_delete);
  11627. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  11628. mob_spawn (md); //Now it is ready for spawning.
  11629. clif_specialeffect(&md->bl,344,AREA);
  11630. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  11631. }
  11632. return 0;
  11633. }
  11634. /*==========================================
  11635. * Checks whether it is daytime/nighttime
  11636. *------------------------------------------*/
  11637. BUILDIN_FUNC(isnight)
  11638. {
  11639. script_pushint(st,(night_flag == 1));
  11640. return 0;
  11641. }
  11642. BUILDIN_FUNC(isday)
  11643. {
  11644. script_pushint(st,(night_flag == 0));
  11645. return 0;
  11646. }
  11647. /*================================================
  11648. * Check how many items/cards in the list are
  11649. * equipped - used for 2/15's cards patch [celest]
  11650. *------------------------------------------------*/
  11651. BUILDIN_FUNC(isequippedcnt)
  11652. {
  11653. TBL_PC *sd;
  11654. int i, j, k, id = 1;
  11655. int ret = 0;
  11656. sd = script_rid2sd(st);
  11657. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11658. script_pushint(st,0);
  11659. return 0;
  11660. }
  11661. for (i=0; id!=0; i++) {
  11662. FETCH (i+2, id) else id = 0;
  11663. if (id <= 0)
  11664. continue;
  11665. for (j=0; j<EQI_MAX; j++) {
  11666. int index;
  11667. index = sd->equip_index[j];
  11668. if(index < 0) continue;
  11669. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11670. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11671. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11672. if(!sd->inventory_data[index])
  11673. continue;
  11674. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  11675. if (sd->inventory_data[index]->nameid == id)
  11676. ret+= sd->status.inventory[index].amount;
  11677. } else { //Count cards.
  11678. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11679. continue; //No cards
  11680. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11681. if (sd->status.inventory[index].card[k] == id)
  11682. ret++; //[Lupus]
  11683. }
  11684. }
  11685. }
  11686. }
  11687. script_pushint(st,ret);
  11688. return 0;
  11689. }
  11690. /*================================================
  11691. * Check whether another card has been
  11692. * equipped - used for 2/15's cards patch [celest]
  11693. * -- Items checked cannot be reused in another
  11694. * card set to prevent exploits
  11695. *------------------------------------------------*/
  11696. BUILDIN_FUNC(isequipped)
  11697. {
  11698. TBL_PC *sd;
  11699. int i, j, k, id = 1;
  11700. int index, flag;
  11701. int ret = -1;
  11702. //Original hash to reverse it when full check fails.
  11703. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  11704. sd = script_rid2sd(st);
  11705. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  11706. script_pushint(st,0);
  11707. return 0;
  11708. }
  11709. setitem_hash = sd->bonus.setitem_hash;
  11710. setitem_hash2 = sd->bonus.setitem_hash2;
  11711. for (i=0; id!=0; i++) {
  11712. FETCH (i+2, id) else id = 0;
  11713. if (id <= 0)
  11714. continue;
  11715. flag = 0;
  11716. for (j=0; j<EQI_MAX; j++) {
  11717. index = sd->equip_index[j];
  11718. if(index < 0) continue;
  11719. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  11720. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  11721. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  11722. if(!sd->inventory_data[index])
  11723. continue;
  11724. if (itemdb_type(id) != IT_CARD) {
  11725. if (sd->inventory_data[index]->nameid != id)
  11726. continue;
  11727. flag = 1;
  11728. break;
  11729. } else { //Cards
  11730. if (sd->inventory_data[index]->slot == 0 ||
  11731. itemdb_isspecial(sd->status.inventory[index].card[0]))
  11732. continue;
  11733. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  11734. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  11735. unsigned int hash = 0;
  11736. if (sd->status.inventory[index].card[k] != id)
  11737. continue;
  11738. hash = 1<<((j<5?j:j-5)*4 + k);
  11739. // check if card is already used by another set
  11740. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  11741. continue;
  11742. // We have found a match
  11743. flag = 1;
  11744. // Set hash so this card cannot be used by another
  11745. if (j<5)
  11746. sd->bonus.setitem_hash |= hash;
  11747. else
  11748. sd->bonus.setitem_hash2 |= hash;
  11749. break;
  11750. }
  11751. }
  11752. if (flag) break; //Card found
  11753. }
  11754. if (ret == -1)
  11755. ret = flag;
  11756. else
  11757. ret &= flag;
  11758. if (!ret) break;
  11759. }
  11760. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  11761. sd->bonus.setitem_hash = setitem_hash;
  11762. sd->bonus.setitem_hash2 = setitem_hash2;
  11763. }
  11764. script_pushint(st,ret);
  11765. return 0;
  11766. }
  11767. /*================================================
  11768. * Check how many given inserted cards in the CURRENT
  11769. * weapon - used for 2/15's cards patch [Lupus]
  11770. *------------------------------------------------*/
  11771. BUILDIN_FUNC(cardscnt)
  11772. {
  11773. TBL_PC *sd;
  11774. int i, k, id = 1;
  11775. int ret = 0;
  11776. int index;
  11777. sd = script_rid2sd(st);
  11778. for (i=0; id!=0; i++) {
  11779. FETCH (i+2, id) else id = 0;
  11780. if (id <= 0)
  11781. continue;
  11782. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  11783. if(index < 0) continue;
  11784. if(!sd->inventory_data[index])
  11785. continue;
  11786. if(itemdb_type(id) != IT_CARD) {
  11787. if (sd->inventory_data[index]->nameid == id)
  11788. ret+= sd->status.inventory[index].amount;
  11789. } else {
  11790. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  11791. continue;
  11792. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  11793. if (sd->status.inventory[index].card[k] == id)
  11794. ret++;
  11795. }
  11796. }
  11797. }
  11798. script_pushint(st,ret);
  11799. // script_pushint(st,current_equip_item_index);
  11800. return 0;
  11801. }
  11802. /*=======================================================
  11803. * Returns the refined number of the current item, or an
  11804. * item with inventory index specified
  11805. *-------------------------------------------------------*/
  11806. BUILDIN_FUNC(getrefine)
  11807. {
  11808. TBL_PC *sd;
  11809. if ((sd = script_rid2sd(st))!= NULL)
  11810. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  11811. else
  11812. script_pushint(st,0);
  11813. return 0;
  11814. }
  11815. /*=======================================================
  11816. * Day/Night controls
  11817. *-------------------------------------------------------*/
  11818. BUILDIN_FUNC(night)
  11819. {
  11820. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  11821. return 0;
  11822. }
  11823. BUILDIN_FUNC(day)
  11824. {
  11825. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  11826. return 0;
  11827. }
  11828. //=======================================================
  11829. // Unequip [Spectre]
  11830. //-------------------------------------------------------
  11831. BUILDIN_FUNC(unequip)
  11832. {
  11833. int i;
  11834. size_t num;
  11835. TBL_PC *sd;
  11836. num = script_getnum(st,2);
  11837. sd = script_rid2sd(st);
  11838. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  11839. {
  11840. i = pc_checkequip(sd,equip[num-1]);
  11841. if (i >= 0)
  11842. pc_unequipitem(sd,i,1|2);
  11843. }
  11844. return 0;
  11845. }
  11846. BUILDIN_FUNC(equip)
  11847. {
  11848. int nameid=0,i;
  11849. TBL_PC *sd;
  11850. struct item_data *item_data;
  11851. sd = script_rid2sd(st);
  11852. nameid=script_getnum(st,2);
  11853. if((item_data = itemdb_exists(nameid)) == NULL)
  11854. {
  11855. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  11856. return 1;
  11857. }
  11858. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  11859. if( i < MAX_INVENTORY )
  11860. pc_equipitem(sd,i,item_data->equip);
  11861. return 0;
  11862. }
  11863. BUILDIN_FUNC(autoequip)
  11864. {
  11865. int nameid, flag;
  11866. struct item_data *item_data;
  11867. nameid=script_getnum(st,2);
  11868. flag=script_getnum(st,3);
  11869. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  11870. {
  11871. ShowError("buildin_autoequip: Invalid item '%d'.\n", nameid);
  11872. return 1;
  11873. }
  11874. if( !itemdb_isequip2(item_data) )
  11875. {
  11876. ShowError("buildin_autoequip: Item '%d' cannot be equipped.\n", nameid);
  11877. return 1;
  11878. }
  11879. item_data->flag.autoequip = flag>0?1:0;
  11880. return 0;
  11881. }
  11882. BUILDIN_FUNC(setbattleflag)
  11883. {
  11884. const char *flag, *value;
  11885. flag = script_getstr(st,2);
  11886. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  11887. if (battle_set_value(flag, value) == 0)
  11888. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  11889. else
  11890. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  11891. return 0;
  11892. }
  11893. BUILDIN_FUNC(getbattleflag)
  11894. {
  11895. const char *flag;
  11896. flag = script_getstr(st,2);
  11897. script_pushint(st,battle_get_value(flag));
  11898. return 0;
  11899. }
  11900. //=======================================================
  11901. // strlen [Valaris]
  11902. //-------------------------------------------------------
  11903. BUILDIN_FUNC(getstrlen)
  11904. {
  11905. const char *str = script_getstr(st,2);
  11906. int len = (str) ? (int)strlen(str) : 0;
  11907. script_pushint(st,len);
  11908. return 0;
  11909. }
  11910. //=======================================================
  11911. // isalpha [Valaris]
  11912. //-------------------------------------------------------
  11913. BUILDIN_FUNC(charisalpha)
  11914. {
  11915. const char *str=script_getstr(st,2);
  11916. int pos=script_getnum(st,3);
  11917. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  11918. script_pushint(st,val);
  11919. return 0;
  11920. }
  11921. //=======================================================
  11922. // charisupper <str>, <index>
  11923. //-------------------------------------------------------
  11924. BUILDIN_FUNC(charisupper)
  11925. {
  11926. const char *str = script_getstr(st,2);
  11927. int pos = script_getnum(st,3);
  11928. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  11929. script_pushint(st,val);
  11930. return 0;
  11931. }
  11932. //=======================================================
  11933. // charislower <str>, <index>
  11934. //-------------------------------------------------------
  11935. BUILDIN_FUNC(charislower)
  11936. {
  11937. const char *str = script_getstr(st,2);
  11938. int pos = script_getnum(st,3);
  11939. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  11940. script_pushint(st,val);
  11941. return 0;
  11942. }
  11943. //=======================================================
  11944. // charat <str>, <index>
  11945. //-------------------------------------------------------
  11946. BUILDIN_FUNC(charat) {
  11947. const char *str = script_getstr(st,2);
  11948. int pos = script_getnum(st,3);
  11949. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  11950. char output[2];
  11951. output[0] = str[pos];
  11952. output[1] = '\0';
  11953. script_pushstrcopy(st, output);
  11954. } else
  11955. script_pushconststr(st, "");
  11956. return 0;
  11957. }
  11958. //=======================================================
  11959. // setchar <string>, <char>, <index>
  11960. //-------------------------------------------------------
  11961. BUILDIN_FUNC(setchar)
  11962. {
  11963. const char *str = script_getstr(st,2);
  11964. const char *c = script_getstr(st,3);
  11965. int index = script_getnum(st,4);
  11966. char *output = aStrdup(str);
  11967. if(index >= 0 && index < strlen(output))
  11968. output[index] = *c;
  11969. script_pushstr(st, output);
  11970. return 0;
  11971. }
  11972. //=======================================================
  11973. // insertchar <string>, <char>, <index>
  11974. //-------------------------------------------------------
  11975. BUILDIN_FUNC(insertchar)
  11976. {
  11977. const char *str = script_getstr(st,2);
  11978. const char *c = script_getstr(st,3);
  11979. int index = script_getnum(st,4);
  11980. char *output;
  11981. size_t len = strlen(str);
  11982. if(index < 0)
  11983. index = 0;
  11984. else if(index > len)
  11985. index = len;
  11986. output = (char*)aMalloc(len + 2);
  11987. memcpy(output, str, index);
  11988. output[index] = c[0];
  11989. memcpy(&output[index+1], &str[index], len - index);
  11990. output[len+1] = '\0';
  11991. script_pushstr(st, output);
  11992. return 0;
  11993. }
  11994. //=======================================================
  11995. // delchar <string>, <index>
  11996. //-------------------------------------------------------
  11997. BUILDIN_FUNC(delchar)
  11998. {
  11999. const char *str = script_getstr(st,2);
  12000. int index = script_getnum(st,3);
  12001. char *output;
  12002. size_t len = strlen(str);
  12003. if(index < 0 || index > len) {
  12004. //return original
  12005. output = aStrdup(str);
  12006. script_pushstr(st, output);
  12007. return 0;
  12008. }
  12009. output = (char*)aMalloc(len);
  12010. memcpy(output, str, index);
  12011. memcpy(&output[index], &str[index+1], len - index);
  12012. script_pushstr(st, output);
  12013. return 0;
  12014. }
  12015. //=======================================================
  12016. // strtoupper <str>
  12017. //-------------------------------------------------------
  12018. BUILDIN_FUNC(strtoupper)
  12019. {
  12020. const char *str = script_getstr(st,2);
  12021. char *output = aStrdup(str);
  12022. char *cursor = output;
  12023. while (*cursor != '\0') {
  12024. *cursor = TOUPPER(*cursor);
  12025. cursor++;
  12026. }
  12027. script_pushstr(st, output);
  12028. return 0;
  12029. }
  12030. //=======================================================
  12031. // strtolower <str>
  12032. //-------------------------------------------------------
  12033. BUILDIN_FUNC(strtolower)
  12034. {
  12035. const char *str = script_getstr(st,2);
  12036. char *output = aStrdup(str);
  12037. char *cursor = output;
  12038. while (*cursor != '\0') {
  12039. *cursor = TOLOWER(*cursor);
  12040. cursor++;
  12041. }
  12042. script_pushstr(st, output);
  12043. return 0;
  12044. }
  12045. //=======================================================
  12046. // substr <str>, <start>, <end>
  12047. //-------------------------------------------------------
  12048. BUILDIN_FUNC(substr)
  12049. {
  12050. const char *str = script_getstr(st,2);
  12051. char *output;
  12052. int start = script_getnum(st,3);
  12053. int end = script_getnum(st,4);
  12054. int len = 0;
  12055. if(start >= 0 && end < strlen(str) && start <= end) {
  12056. len = end - start + 1;
  12057. output = (char*)aMalloc(len + 1);
  12058. memcpy(output, &str[start], len);
  12059. } else
  12060. output = (char*)aMalloc(1);
  12061. output[len] = '\0';
  12062. script_pushstr(st, output);
  12063. return 0;
  12064. }
  12065. //=======================================================
  12066. // explode <dest_string_array>, <str>, <delimiter>
  12067. // Note: delimiter is limited to 1 char
  12068. //-------------------------------------------------------
  12069. BUILDIN_FUNC(explode)
  12070. {
  12071. struct script_data* data = script_getdata(st, 2);
  12072. const char *str = script_getstr(st,3);
  12073. const char delimiter = script_getstr(st, 4)[0];
  12074. int32 id;
  12075. size_t len = strlen(str);
  12076. int i = 0, j = 0;
  12077. int start;
  12078. char *temp;
  12079. const char* name;
  12080. TBL_PC* sd = NULL;
  12081. temp = (char*)aMalloc(len + 1);
  12082. if( !data_isreference(data) )
  12083. {
  12084. ShowError("script:explode: not a variable\n");
  12085. script_reportdata(data);
  12086. st->state = END;
  12087. return 1;// not a variable
  12088. }
  12089. id = reference_getid(data);
  12090. start = reference_getindex(data);
  12091. name = reference_getname(data);
  12092. if( not_array_variable(*name) )
  12093. {
  12094. ShowError("script:explode: illegal scope\n");
  12095. script_reportdata(data);
  12096. st->state = END;
  12097. return 1;// not supported
  12098. }
  12099. if( !is_string_variable(name) )
  12100. {
  12101. ShowError("script:explode: not string array\n");
  12102. script_reportdata(data);
  12103. st->state = END;
  12104. return 1;// data type mismatch
  12105. }
  12106. if( not_server_variable(*name) )
  12107. {
  12108. sd = script_rid2sd(st);
  12109. if( sd == NULL )
  12110. return 0;// no player attached
  12111. }
  12112. while(str[i] != '\0') {
  12113. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  12114. temp[j] = '\0';
  12115. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  12116. j = 0;
  12117. ++i;
  12118. } else {
  12119. temp[j++] = str[i++];
  12120. }
  12121. }
  12122. //set last string
  12123. temp[j] = '\0';
  12124. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  12125. aFree(temp);
  12126. return 0;
  12127. }
  12128. //=======================================================
  12129. // implode <string_array>
  12130. // implode <string_array>, <glue>
  12131. //-------------------------------------------------------
  12132. BUILDIN_FUNC(implode)
  12133. {
  12134. struct script_data* data = script_getdata(st, 2);
  12135. const char *glue = NULL, *name, *temp;
  12136. int32 glue_len = 0, array_size, id;
  12137. size_t len = 0;
  12138. int i, k = 0;
  12139. TBL_PC* sd = NULL;
  12140. char *output;
  12141. if( !data_isreference(data) )
  12142. {
  12143. ShowError("script:implode: not a variable\n");
  12144. script_reportdata(data);
  12145. st->state = END;
  12146. return 1;// not a variable
  12147. }
  12148. id = reference_getid(data);
  12149. name = reference_getname(data);
  12150. if( not_array_variable(*name) )
  12151. {
  12152. ShowError("script:implode: illegal scope\n");
  12153. script_reportdata(data);
  12154. st->state = END;
  12155. return 1;// not supported
  12156. }
  12157. if( !is_string_variable(name) )
  12158. {
  12159. ShowError("script:implode: not string array\n");
  12160. script_reportdata(data);
  12161. st->state = END;
  12162. return 1;// data type mismatch
  12163. }
  12164. if( not_server_variable(*name) )
  12165. {
  12166. sd = script_rid2sd(st);
  12167. if( sd == NULL )
  12168. return 0;// no player attached
  12169. }
  12170. //count chars
  12171. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  12172. if(array_size == -1) //empty array check (AmsTaff)
  12173. {
  12174. ShowWarning("script:implode: array length = 0\n");
  12175. output = (char*)aMalloc(sizeof(char)*5);
  12176. sprintf(output,"%s","NULL");
  12177. } else {
  12178. for(i = 0; i <= array_size; ++i) {
  12179. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12180. len += strlen(temp);
  12181. script_removetop(st, -1, 0);
  12182. }
  12183. //allocate mem
  12184. if( script_hasdata(st,3) ) {
  12185. glue = script_getstr(st,3);
  12186. glue_len = strlen(glue);
  12187. len += glue_len * (array_size);
  12188. }
  12189. output = (char*)aMalloc(len + 1);
  12190. //build output
  12191. for(i = 0; i < array_size; ++i) {
  12192. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12193. len = strlen(temp);
  12194. memcpy(&output[k], temp, len);
  12195. k += len;
  12196. if(glue_len != 0) {
  12197. memcpy(&output[k], glue, glue_len);
  12198. k += glue_len;
  12199. }
  12200. script_removetop(st, -1, 0);
  12201. }
  12202. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  12203. len = strlen(temp);
  12204. memcpy(&output[k], temp, len);
  12205. k += len;
  12206. script_removetop(st, -1, 0);
  12207. output[k] = '\0';
  12208. }
  12209. script_pushstr(st, output);
  12210. return 0;
  12211. }
  12212. //=======================================================
  12213. // sprintf(<format>, ...);
  12214. // Implements C sprintf, except format %n. The resulting string is
  12215. // returned, instead of being saved in variable by reference.
  12216. //-------------------------------------------------------
  12217. BUILDIN_FUNC(sprintf)
  12218. {
  12219. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  12220. const char* format;
  12221. char* p;
  12222. char* q;
  12223. char* buf = NULL;
  12224. char* buf2 = NULL;
  12225. struct script_data* data;
  12226. StringBuf final_buf;
  12227. // Fetch init data
  12228. format = script_getstr(st, 2);
  12229. argc = script_lastdata(st)-2;
  12230. len = strlen(format);
  12231. // Skip parsing, where no parsing is required.
  12232. if(len==0){
  12233. script_pushconststr(st,"");
  12234. return 0;
  12235. }
  12236. // Pessimistic alloc
  12237. CREATE(buf, char, len+1);
  12238. // Need not be parsed, just solve stuff like %%.
  12239. if(argc==0){
  12240. memcpy(buf,format,len+1);
  12241. script_pushstrcopy(st, buf);
  12242. aFree(buf);
  12243. return 0;
  12244. }
  12245. safestrncpy(buf, format, len+1);
  12246. // Issue sprintf for each parameter
  12247. StringBuf_Init(&final_buf);
  12248. q = buf;
  12249. while((p = strchr(q, '%'))!=NULL){
  12250. if(p!=q){
  12251. len = p-q+1;
  12252. if(buf2_len<len){
  12253. RECREATE(buf2, char, len);
  12254. buf2_len = len;
  12255. }
  12256. safestrncpy(buf2, q, len);
  12257. StringBuf_AppendStr(&final_buf, buf2);
  12258. q = p;
  12259. }
  12260. p = q+1;
  12261. if(*p=='%'){ // %%
  12262. StringBuf_AppendStr(&final_buf, "%");
  12263. q+=2;
  12264. continue;
  12265. }
  12266. if(*p=='n'){ // %n
  12267. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  12268. script_reportsrc(st);
  12269. q+=2;
  12270. continue;
  12271. }
  12272. if(arg>=argc){
  12273. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  12274. if(buf) aFree(buf);
  12275. if(buf2) aFree(buf2);
  12276. StringBuf_Destroy(&final_buf);
  12277. script_pushconststr(st,"");
  12278. return 1;
  12279. }
  12280. if((p = strchr(q+1, '%'))==NULL){
  12281. p = strchr(q, 0); // EOS
  12282. }
  12283. len = p-q+1;
  12284. if(buf2_len<len){
  12285. RECREATE(buf2, char, len);
  12286. buf2_len = len;
  12287. }
  12288. safestrncpy(buf2, q, len);
  12289. q = p;
  12290. // Note: This assumes the passed value being the correct
  12291. // type to the current format specifier. If not, the server
  12292. // probably crashes or returns anything else, than expected,
  12293. // but it would behave in normal code the same way so it's
  12294. // the scripter's responsibility.
  12295. data = script_getdata(st, arg+3);
  12296. if(data_isstring(data)){ // String
  12297. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12298. }else if(data_isint(data)){ // Number
  12299. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12300. }else if(data_isreference(data)){ // Variable
  12301. char* name = reference_getname(data);
  12302. if(name[strlen(name)-1]=='$'){ // var Str
  12303. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  12304. }else{ // var Int
  12305. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  12306. }
  12307. }else{ // Unsupported type
  12308. ShowError("buildin_sprintf: Unknown argument type!\n");
  12309. if(buf) aFree(buf);
  12310. if(buf2) aFree(buf2);
  12311. StringBuf_Destroy(&final_buf);
  12312. script_pushconststr(st,"");
  12313. return 1;
  12314. }
  12315. arg++;
  12316. }
  12317. // Append anything left
  12318. if(*q){
  12319. StringBuf_AppendStr(&final_buf, q);
  12320. }
  12321. // Passed more, than needed
  12322. if(arg<argc){
  12323. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  12324. script_reportsrc(st);
  12325. }
  12326. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  12327. if(buf) aFree(buf);
  12328. if(buf2) aFree(buf2);
  12329. StringBuf_Destroy(&final_buf);
  12330. return 0;
  12331. }
  12332. //=======================================================
  12333. // sscanf(<str>, <format>, ...);
  12334. // Implements C sscanf.
  12335. //-------------------------------------------------------
  12336. BUILDIN_FUNC(sscanf){
  12337. unsigned int argc, arg = 0, len;
  12338. struct script_data* data;
  12339. struct map_session_data* sd = NULL;
  12340. const char* str;
  12341. const char* format;
  12342. const char* p;
  12343. const char* q;
  12344. char* buf = NULL;
  12345. char* buf_p;
  12346. char* ref_str = NULL;
  12347. int ref_int;
  12348. // Get data
  12349. str = script_getstr(st, 2);
  12350. format = script_getstr(st, 3);
  12351. argc = script_lastdata(st)-3;
  12352. len = strlen(format);
  12353. CREATE(buf, char, len*2+1);
  12354. // Issue sscanf for each parameter
  12355. *buf = 0;
  12356. q = format;
  12357. while((p = strchr(q, '%'))){
  12358. if(p!=q){
  12359. strncat(buf, q, (size_t)(p-q));
  12360. q = p;
  12361. }
  12362. p = q+1;
  12363. if(*p=='*' || *p=='%'){ // Skip
  12364. strncat(buf, q, 2);
  12365. q+=2;
  12366. continue;
  12367. }
  12368. if(arg>=argc){
  12369. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  12370. script_pushint(st, -1);
  12371. if(buf) aFree(buf);
  12372. if(ref_str) aFree(ref_str);
  12373. return 1;
  12374. }
  12375. if((p = strchr(q+1, '%'))==NULL){
  12376. p = strchr(q, 0); // EOS
  12377. }
  12378. len = p-q;
  12379. strncat(buf, q, len);
  12380. q = p;
  12381. // Validate output
  12382. data = script_getdata(st, arg+4);
  12383. if(!data_isreference(data) || !reference_tovariable(data)){
  12384. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  12385. script_pushint(st, -1);
  12386. if(buf) aFree(buf);
  12387. if(ref_str) aFree(ref_str);
  12388. return 1;
  12389. }
  12390. buf_p = reference_getname(data);
  12391. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  12392. script_pushint(st, -1);
  12393. if(buf) aFree(buf);
  12394. if(ref_str) aFree(ref_str);
  12395. return 0;
  12396. }
  12397. // Save value if any
  12398. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  12399. if(ref_str==NULL){
  12400. CREATE(ref_str, char, strlen(str)+1);
  12401. }
  12402. if(sscanf(str, buf, ref_str)==0){
  12403. break;
  12404. }
  12405. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  12406. } else { // Number
  12407. if(sscanf(str, buf, &ref_int)==0){
  12408. break;
  12409. }
  12410. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  12411. }
  12412. arg++;
  12413. // Disable used format (%... -> %*...)
  12414. buf_p = strchr(buf, 0);
  12415. memmove(buf_p-len+2, buf_p-len+1, len);
  12416. *(buf_p-len+1) = '*';
  12417. }
  12418. script_pushint(st, arg);
  12419. if(buf) aFree(buf);
  12420. if(ref_str) aFree(ref_str);
  12421. return 0;
  12422. }
  12423. //=======================================================
  12424. // strpos(<haystack>, <needle>)
  12425. // strpos(<haystack>, <needle>, <offset>)
  12426. //
  12427. // Implements PHP style strpos. Adapted from code from
  12428. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  12429. //-------------------------------------------------------
  12430. BUILDIN_FUNC(strpos) {
  12431. const char *haystack = script_getstr(st,2);
  12432. const char *needle = script_getstr(st,3);
  12433. int i;
  12434. size_t len;
  12435. if( script_hasdata(st,4) )
  12436. i = script_getnum(st,4);
  12437. else
  12438. i = 0;
  12439. if (needle[0] == '\0') {
  12440. script_pushint(st, -1);
  12441. return 0;
  12442. }
  12443. len = strlen(haystack);
  12444. for ( ; i < len; ++i ) {
  12445. if ( haystack[i] == *needle ) {
  12446. // matched starting char -- loop through remaining chars
  12447. const char *h, *n;
  12448. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  12449. if ( *h != *n ) {
  12450. break;
  12451. }
  12452. }
  12453. if ( !*n ) { // matched all of 'needle' to null termination
  12454. script_pushint(st, i);
  12455. return 0;
  12456. }
  12457. }
  12458. }
  12459. script_pushint(st, -1);
  12460. return 0;
  12461. }
  12462. //===============================================================
  12463. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  12464. //
  12465. // Note: Finds all instances of <search> in <input> and replaces
  12466. // with <replace>. If specified will only replace as many
  12467. // instances as specified in <count>. By default will be case
  12468. // sensitive.
  12469. //---------------------------------------------------------------
  12470. BUILDIN_FUNC(replacestr)
  12471. {
  12472. const char *input = script_getstr(st, 2);
  12473. const char *find = script_getstr(st, 3);
  12474. const char *replace = script_getstr(st, 4);
  12475. size_t inputlen = strlen(input);
  12476. size_t findlen = strlen(find);
  12477. struct StringBuf output;
  12478. bool usecase = true;
  12479. int count = 0;
  12480. int numFinds = 0;
  12481. int i = 0, f = 0;
  12482. if(findlen == 0) {
  12483. ShowError("script:replacestr: Invalid search length.\n");
  12484. st->state = END;
  12485. return 1;
  12486. }
  12487. if(script_hasdata(st, 5)) {
  12488. if( !script_isstring(st,5) )
  12489. usecase = script_getnum(st, 5) != 0;
  12490. else {
  12491. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  12492. st->state = END;
  12493. return 1;
  12494. }
  12495. }
  12496. if(script_hasdata(st, 6)) {
  12497. count = script_getnum(st, 6);
  12498. if(count == 0) {
  12499. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  12500. st->state = END;
  12501. return 1;
  12502. }
  12503. }
  12504. StringBuf_Init(&output);
  12505. for(; i < inputlen; i++) {
  12506. if(count && count == numFinds) { //found enough, stop looking
  12507. break;
  12508. }
  12509. for(f = 0; f <= findlen; f++) {
  12510. if(f == findlen) { //complete match
  12511. numFinds++;
  12512. StringBuf_AppendStr(&output, replace);
  12513. i += findlen - 1;
  12514. break;
  12515. } else {
  12516. if(usecase) {
  12517. if((i + f) > inputlen || input[i + f] != find[f]) {
  12518. StringBuf_Printf(&output, "%c", input[i]);
  12519. break;
  12520. }
  12521. } else {
  12522. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12523. StringBuf_Printf(&output, "%c", input[i]);
  12524. break;
  12525. }
  12526. }
  12527. }
  12528. }
  12529. }
  12530. //append excess after enough found
  12531. if(i < inputlen)
  12532. StringBuf_AppendStr(&output, &(input[i]));
  12533. script_pushstrcopy(st, StringBuf_Value(&output));
  12534. StringBuf_Destroy(&output);
  12535. return 0;
  12536. }
  12537. //========================================================
  12538. // countstr <input>, <search>{, <usecase>}
  12539. //
  12540. // Note: Counts the number of times <search> occurs in
  12541. // <input>. By default will be case sensitive.
  12542. //--------------------------------------------------------
  12543. BUILDIN_FUNC(countstr)
  12544. {
  12545. const char *input = script_getstr(st, 2);
  12546. const char *find = script_getstr(st, 3);
  12547. size_t inputlen = strlen(input);
  12548. size_t findlen = strlen(find);
  12549. bool usecase = true;
  12550. int numFinds = 0;
  12551. int i = 0, f = 0;
  12552. if(findlen == 0) {
  12553. ShowError("script:countstr: Invalid search length.\n");
  12554. st->state = END;
  12555. return 1;
  12556. }
  12557. if(script_hasdata(st, 4)) {
  12558. if( !script_isstring(st,4) )
  12559. usecase = script_getnum(st, 4) != 0;
  12560. else {
  12561. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  12562. st->state = END;
  12563. return 1;
  12564. }
  12565. }
  12566. for(; i < inputlen; i++) {
  12567. for(f = 0; f <= findlen; f++) {
  12568. if(f == findlen) { //complete match
  12569. numFinds++;
  12570. i += findlen - 1;
  12571. break;
  12572. } else {
  12573. if(usecase) {
  12574. if((i + f) > inputlen || input[i + f] != find[f]) {
  12575. break;
  12576. }
  12577. } else {
  12578. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  12579. break;
  12580. }
  12581. }
  12582. }
  12583. }
  12584. }
  12585. script_pushint(st, numFinds);
  12586. return 0;
  12587. }
  12588. /// Changes the display name and/or display class of the npc.
  12589. /// Returns 0 is successful, 1 if the npc does not exist.
  12590. ///
  12591. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  12592. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  12593. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  12594. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  12595. BUILDIN_FUNC(setnpcdisplay)
  12596. {
  12597. const char* name;
  12598. const char* newname = NULL;
  12599. int class_ = -1, size = -1;
  12600. struct script_data* data;
  12601. struct npc_data* nd;
  12602. name = script_getstr(st,2);
  12603. data = script_getdata(st,3);
  12604. if( script_hasdata(st,4) )
  12605. class_ = script_getnum(st,4);
  12606. if( script_hasdata(st,5) )
  12607. size = script_getnum(st,5);
  12608. get_val(st, data);
  12609. if( data_isstring(data) )
  12610. newname = conv_str(st,data);
  12611. else if( data_isint(data) )
  12612. class_ = conv_num(st,data);
  12613. else
  12614. {
  12615. ShowError("script:setnpcdisplay: expected a string or number\n");
  12616. script_reportdata(data);
  12617. return 1;
  12618. }
  12619. nd = npc_name2id(name);
  12620. if( nd == NULL )
  12621. {// not found
  12622. script_pushint(st,1);
  12623. return 0;
  12624. }
  12625. // update npc
  12626. if( newname )
  12627. npc_setdisplayname(nd, newname);
  12628. if( size != -1 && size != (int)nd->size )
  12629. nd->size = size;
  12630. else
  12631. size = -1;
  12632. if( class_ != -1 && nd->class_ != class_ )
  12633. npc_setclass(nd, class_);
  12634. else if( size != -1 )
  12635. { // Required to update the visual size
  12636. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  12637. clif_spawn(&nd->bl);
  12638. }
  12639. script_pushint(st,0);
  12640. return 0;
  12641. }
  12642. BUILDIN_FUNC(atoi)
  12643. {
  12644. const char *value;
  12645. value = script_getstr(st,2);
  12646. script_pushint(st,atoi(value));
  12647. return 0;
  12648. }
  12649. // case-insensitive substring search [lordalfa]
  12650. BUILDIN_FUNC(compare)
  12651. {
  12652. const char *message;
  12653. const char *cmpstring;
  12654. message = script_getstr(st,2);
  12655. cmpstring = script_getstr(st,3);
  12656. script_pushint(st,(stristr(message,cmpstring) != NULL));
  12657. return 0;
  12658. }
  12659. // [zBuffer] List of mathematics commands --->
  12660. BUILDIN_FUNC(sqrt)
  12661. {
  12662. double i, a;
  12663. i = script_getnum(st,2);
  12664. a = sqrt(i);
  12665. script_pushint(st,(int)a);
  12666. return 0;
  12667. }
  12668. BUILDIN_FUNC(pow)
  12669. {
  12670. double i, a, b;
  12671. a = script_getnum(st,2);
  12672. b = script_getnum(st,3);
  12673. i = pow(a,b);
  12674. script_pushint(st,(int)i);
  12675. return 0;
  12676. }
  12677. BUILDIN_FUNC(distance)
  12678. {
  12679. int x0, y0, x1, y1;
  12680. x0 = script_getnum(st,2);
  12681. y0 = script_getnum(st,3);
  12682. x1 = script_getnum(st,4);
  12683. y1 = script_getnum(st,5);
  12684. script_pushint(st,distance_xy(x0,y0,x1,y1));
  12685. return 0;
  12686. }
  12687. // <--- [zBuffer] List of mathematics commands
  12688. BUILDIN_FUNC(md5)
  12689. {
  12690. const char *tmpstr;
  12691. char *md5str;
  12692. tmpstr = script_getstr(st,2);
  12693. md5str = (char *)aMalloc((32+1)*sizeof(char));
  12694. MD5_String(tmpstr, md5str);
  12695. script_pushstr(st, md5str);
  12696. return 0;
  12697. }
  12698. // [zBuffer] List of dynamic var commands --->
  12699. BUILDIN_FUNC(setd)
  12700. {
  12701. TBL_PC *sd=NULL;
  12702. char varname[100];
  12703. const char *buffer;
  12704. int elem;
  12705. buffer = script_getstr(st, 2);
  12706. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  12707. elem = 0;
  12708. if( not_server_variable(*varname) )
  12709. {
  12710. sd = script_rid2sd(st);
  12711. if( sd == NULL )
  12712. {
  12713. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  12714. return 0;
  12715. }
  12716. }
  12717. if( is_string_variable(varname) ) {
  12718. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  12719. } else {
  12720. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  12721. }
  12722. return 0;
  12723. }
  12724. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  12725. {
  12726. int i, j;
  12727. TBL_PC* sd = NULL;
  12728. const char* query;
  12729. struct script_data* data;
  12730. const char* name;
  12731. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  12732. int num_vars;
  12733. int num_cols;
  12734. // check target variables
  12735. for( i = 3; script_hasdata(st,i); ++i ) {
  12736. data = script_getdata(st, i);
  12737. if( data_isreference(data) ) { // it's a variable
  12738. name = reference_getname(data);
  12739. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  12740. sd = script_rid2sd(st);
  12741. if( sd == NULL ) { // no player attached
  12742. script_reportdata(data);
  12743. st->state = END;
  12744. return 1;
  12745. }
  12746. }
  12747. if( not_array_variable(*name) )
  12748. max_rows = 1;// not an array, limit to one row
  12749. } else {
  12750. ShowError("script:query_sql: not a variable\n");
  12751. script_reportdata(data);
  12752. st->state = END;
  12753. return 1;
  12754. }
  12755. }
  12756. num_vars = i - 3;
  12757. // Execute the query
  12758. query = script_getstr(st,2);
  12759. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  12760. Sql_ShowDebug(handle);
  12761. script_pushint(st, 0);
  12762. return 1;
  12763. }
  12764. if( Sql_NumRows(handle) == 0 ) { // No data received
  12765. Sql_FreeResult(handle);
  12766. script_pushint(st, 0);
  12767. return 0;
  12768. }
  12769. // Count the number of columns to store
  12770. num_cols = Sql_NumColumns(handle);
  12771. if( num_vars < num_cols ) {
  12772. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  12773. script_reportsrc(st);
  12774. } else if( num_vars > num_cols ) {
  12775. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  12776. script_reportsrc(st);
  12777. }
  12778. // Store data
  12779. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  12780. for( j = 0; j < num_vars; ++j ) {
  12781. char* str = NULL;
  12782. if( j < num_cols )
  12783. Sql_GetData(handle, j, &str, NULL);
  12784. data = script_getdata(st, j+3);
  12785. name = reference_getname(data);
  12786. if( is_string_variable(name) )
  12787. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  12788. else
  12789. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  12790. }
  12791. }
  12792. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  12793. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  12794. script_reportsrc(st);
  12795. }
  12796. // Free data
  12797. Sql_FreeResult(handle);
  12798. script_pushint(st, i);
  12799. return 0;
  12800. }
  12801. BUILDIN_FUNC(query_sql) {
  12802. #ifdef BETA_THREAD_TEST
  12803. if( st->state != RERUNLINE ) {
  12804. queryThread_add(st,false);
  12805. st->state = RERUNLINE;/* will continue when the query is finished running. */
  12806. } else
  12807. st->state = RUN;
  12808. return 0;
  12809. #else
  12810. return buildin_query_sql_sub(st, mmysql_handle);
  12811. #endif
  12812. }
  12813. BUILDIN_FUNC(query_logsql) {
  12814. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  12815. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  12816. script_pushint(st,-1);
  12817. return 1;
  12818. }
  12819. #ifdef BETA_THREAD_TEST
  12820. if( st->state != RERUNLINE ) {
  12821. queryThread_add(st,true);
  12822. st->state = RERUNLINE;/* will continue when the query is finished running. */
  12823. } else
  12824. st->state = RUN;
  12825. return 0;
  12826. #else
  12827. return buildin_query_sql_sub(st, logmysql_handle);
  12828. #endif
  12829. }
  12830. //Allows escaping of a given string.
  12831. BUILDIN_FUNC(escape_sql)
  12832. {
  12833. const char *str;
  12834. char *esc_str;
  12835. size_t len;
  12836. str = script_getstr(st,2);
  12837. len = strlen(str);
  12838. esc_str = (char*)aMalloc(len*2+1);
  12839. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  12840. script_pushstr(st, esc_str);
  12841. return 0;
  12842. }
  12843. BUILDIN_FUNC(getd)
  12844. {
  12845. char varname[100];
  12846. const char *buffer;
  12847. int elem;
  12848. buffer = script_getstr(st, 2);
  12849. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  12850. elem = 0;
  12851. // Push the 'pointer' so it's more flexible [Lance]
  12852. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  12853. return 0;
  12854. }
  12855. // <--- [zBuffer] List of dynamic var commands
  12856. // Pet stat [Lance]
  12857. BUILDIN_FUNC(petstat)
  12858. {
  12859. TBL_PC *sd = NULL;
  12860. struct pet_data *pd;
  12861. int flag = script_getnum(st,2);
  12862. sd = script_rid2sd(st);
  12863. if(!sd || !sd->status.pet_id || !sd->pd){
  12864. if(flag == 2)
  12865. script_pushconststr(st, "");
  12866. else
  12867. script_pushint(st,0);
  12868. return 0;
  12869. }
  12870. pd = sd->pd;
  12871. switch(flag){
  12872. case 1: script_pushint(st,(int)pd->pet.class_); break;
  12873. case 2: script_pushstrcopy(st, pd->pet.name); break;
  12874. case 3: script_pushint(st,(int)pd->pet.level); break;
  12875. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  12876. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  12877. default:
  12878. script_pushint(st,0);
  12879. break;
  12880. }
  12881. return 0;
  12882. }
  12883. BUILDIN_FUNC(callshop)
  12884. {
  12885. TBL_PC *sd = NULL;
  12886. struct npc_data *nd;
  12887. const char *shopname;
  12888. int flag = 0;
  12889. sd = script_rid2sd(st);
  12890. if (!sd) {
  12891. script_pushint(st,0);
  12892. return 0;
  12893. }
  12894. shopname = script_getstr(st, 2);
  12895. if( script_hasdata(st,3) )
  12896. flag = script_getnum(st,3);
  12897. nd = npc_name2id(shopname);
  12898. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  12899. {
  12900. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  12901. script_pushint(st,0);
  12902. return 1;
  12903. }
  12904. if( nd->subtype == SHOP )
  12905. {
  12906. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  12907. sd->state.callshop = 1;
  12908. switch( flag )
  12909. {
  12910. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  12911. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  12912. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  12913. }
  12914. }
  12915. else
  12916. clif_cashshop_show(sd, nd);
  12917. sd->npc_shopid = nd->bl.id;
  12918. script_pushint(st,1);
  12919. return 0;
  12920. }
  12921. BUILDIN_FUNC(npcshopitem)
  12922. {
  12923. const char* npcname = script_getstr(st, 2);
  12924. struct npc_data* nd = npc_name2id(npcname);
  12925. int n, i;
  12926. int amount;
  12927. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12928. { //Not found.
  12929. script_pushint(st,0);
  12930. return 0;
  12931. }
  12932. // get the count of new entries
  12933. amount = (script_lastdata(st)-2)/2;
  12934. // generate new shop item list
  12935. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  12936. for( n = 0, i = 3; n < amount; n++, i+=2 )
  12937. {
  12938. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12939. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12940. }
  12941. nd->u.shop.count = n;
  12942. script_pushint(st,1);
  12943. return 0;
  12944. }
  12945. BUILDIN_FUNC(npcshopadditem)
  12946. {
  12947. const char* npcname = script_getstr(st,2);
  12948. struct npc_data* nd = npc_name2id(npcname);
  12949. int n, i;
  12950. int amount;
  12951. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12952. { //Not found.
  12953. script_pushint(st,0);
  12954. return 0;
  12955. }
  12956. // get the count of new entries
  12957. amount = (script_lastdata(st)-2)/2;
  12958. // append new items to existing shop item list
  12959. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  12960. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  12961. {
  12962. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  12963. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  12964. }
  12965. nd->u.shop.count = n;
  12966. script_pushint(st,1);
  12967. return 0;
  12968. }
  12969. BUILDIN_FUNC(npcshopdelitem)
  12970. {
  12971. const char* npcname = script_getstr(st,2);
  12972. struct npc_data* nd = npc_name2id(npcname);
  12973. unsigned int nameid;
  12974. int n, i;
  12975. int amount;
  12976. int size;
  12977. if( !nd || ( nd->subtype != SHOP && nd->subtype != CASHSHOP ) )
  12978. { //Not found.
  12979. script_pushint(st,0);
  12980. return 0;
  12981. }
  12982. amount = script_lastdata(st)-2;
  12983. size = nd->u.shop.count;
  12984. // remove specified items from the shop item list
  12985. for( i = 3; i < 3 + amount; i++ )
  12986. {
  12987. nameid = script_getnum(st,i);
  12988. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  12989. if( n < size )
  12990. {
  12991. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  12992. size--;
  12993. }
  12994. }
  12995. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  12996. nd->u.shop.count = size;
  12997. script_pushint(st,1);
  12998. return 0;
  12999. }
  13000. //Sets a script to attach to a shop npc.
  13001. BUILDIN_FUNC(npcshopattach)
  13002. {
  13003. const char* npcname = script_getstr(st,2);
  13004. struct npc_data* nd = npc_name2id(npcname);
  13005. int flag = 1;
  13006. if( script_hasdata(st,3) )
  13007. flag = script_getnum(st,3);
  13008. if( !nd || nd->subtype != SHOP )
  13009. { //Not found.
  13010. script_pushint(st,0);
  13011. return 0;
  13012. }
  13013. if (flag)
  13014. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  13015. else
  13016. nd->master_nd = NULL;
  13017. script_pushint(st,1);
  13018. return 0;
  13019. }
  13020. /*==========================================
  13021. * Returns some values of an item [Lupus]
  13022. * Price, Weight, etc...
  13023. setitemscript(itemID,"{new item bonus script}",[n]);
  13024. Where n:
  13025. 0 - script
  13026. 1 - Equip script
  13027. 2 - Unequip script
  13028. *------------------------------------------*/
  13029. BUILDIN_FUNC(setitemscript)
  13030. {
  13031. int item_id,n=0;
  13032. const char *script;
  13033. struct item_data *i_data;
  13034. struct script_code **dstscript;
  13035. item_id = script_getnum(st,2);
  13036. script = script_getstr(st,3);
  13037. if( script_hasdata(st,4) )
  13038. n=script_getnum(st,4);
  13039. i_data = itemdb_exists(item_id);
  13040. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  13041. script_pushint(st,0);
  13042. return 0;
  13043. }
  13044. switch (n) {
  13045. case 2:
  13046. dstscript = &i_data->unequip_script;
  13047. break;
  13048. case 1:
  13049. dstscript = &i_data->equip_script;
  13050. break;
  13051. default:
  13052. dstscript = &i_data->script;
  13053. break;
  13054. }
  13055. if(*dstscript)
  13056. script_free_code(*dstscript);
  13057. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  13058. script_pushint(st,1);
  13059. return 0;
  13060. }
  13061. /* Work In Progress [Lupus]
  13062. BUILDIN_FUNC(addmonsterdrop)
  13063. {
  13064. int class_,item_id,chance;
  13065. class_=script_getnum(st,2);
  13066. item_id=script_getnum(st,3);
  13067. chance=script_getnum(st,4);
  13068. if(class_>1000 && item_id>500 && chance>0) {
  13069. script_pushint(st,1);
  13070. } else {
  13071. script_pushint(st,0);
  13072. }
  13073. }
  13074. BUILDIN_FUNC(delmonsterdrop)
  13075. {
  13076. int class_,item_id;
  13077. class_=script_getnum(st,2);
  13078. item_id=script_getnum(st,3);
  13079. if(class_>1000 && item_id>500) {
  13080. script_pushint(st,1);
  13081. } else {
  13082. script_pushint(st,0);
  13083. }
  13084. }
  13085. */
  13086. /*==========================================
  13087. * Returns some values of a monster [Lupus]
  13088. * Name, Level, race, size, etc...
  13089. getmonsterinfo(monsterID,queryIndex);
  13090. *------------------------------------------*/
  13091. BUILDIN_FUNC(getmonsterinfo)
  13092. {
  13093. struct mob_db *mob;
  13094. int mob_id;
  13095. mob_id = script_getnum(st,2);
  13096. if (!mobdb_checkid(mob_id)) {
  13097. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  13098. if ( !script_getnum(st,3) ) //requested a string
  13099. script_pushconststr(st,"null");
  13100. else
  13101. script_pushint(st,-1);
  13102. return -1;
  13103. }
  13104. mob = mob_db(mob_id);
  13105. switch ( script_getnum(st,3) ) {
  13106. case 0: script_pushstrcopy(st,mob->jname); break;
  13107. case 1: script_pushint(st,mob->lv); break;
  13108. case 2: script_pushint(st,mob->status.max_hp); break;
  13109. case 3: script_pushint(st,mob->base_exp); break;
  13110. case 4: script_pushint(st,mob->job_exp); break;
  13111. case 5: script_pushint(st,mob->status.rhw.atk); break;
  13112. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  13113. case 7: script_pushint(st,mob->status.def); break;
  13114. case 8: script_pushint(st,mob->status.mdef); break;
  13115. case 9: script_pushint(st,mob->status.str); break;
  13116. case 10: script_pushint(st,mob->status.agi); break;
  13117. case 11: script_pushint(st,mob->status.vit); break;
  13118. case 12: script_pushint(st,mob->status.int_); break;
  13119. case 13: script_pushint(st,mob->status.dex); break;
  13120. case 14: script_pushint(st,mob->status.luk); break;
  13121. case 15: script_pushint(st,mob->status.rhw.range); break;
  13122. case 16: script_pushint(st,mob->range2); break;
  13123. case 17: script_pushint(st,mob->range3); break;
  13124. case 18: script_pushint(st,mob->status.size); break;
  13125. case 19: script_pushint(st,mob->status.race); break;
  13126. case 20: script_pushint(st,mob->status.def_ele); break;
  13127. case 21: script_pushint(st,mob->status.mode); break;
  13128. case 22: script_pushint(st,mob->mexp); break;
  13129. default: script_pushint(st,-1); //wrong Index
  13130. }
  13131. return 0;
  13132. }
  13133. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  13134. {
  13135. TBL_PC *sd = NULL;
  13136. if(script_hasdata(st,2))
  13137. sd = map_nick2sd(script_getstr(st,2));
  13138. else
  13139. sd = script_rid2sd(st);
  13140. if(sd)
  13141. script_pushint(st, sd->state.autotrade ? 2 : sd->state.vending);
  13142. else
  13143. script_pushint(st,0);
  13144. return 0;
  13145. }
  13146. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  13147. {
  13148. TBL_PC *sd = NULL;
  13149. if(script_hasdata(st,2))
  13150. sd = map_nick2sd(script_getstr(st,2));
  13151. else
  13152. sd = script_rid2sd(st);
  13153. if(sd)
  13154. script_pushint(st,(sd->chatID != 0));
  13155. else
  13156. script_pushint(st,0);
  13157. return 0;
  13158. }
  13159. BUILDIN_FUNC(checkidle)
  13160. {
  13161. TBL_PC *sd = NULL;
  13162. if (script_hasdata(st, 2))
  13163. sd = map_nick2sd(script_getstr(st, 2));
  13164. else
  13165. sd = script_rid2sd(st);
  13166. if (sd)
  13167. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  13168. else
  13169. script_pushint(st, 0);
  13170. return 0;
  13171. }
  13172. BUILDIN_FUNC(searchitem)
  13173. {
  13174. struct script_data* data = script_getdata(st, 2);
  13175. const char *itemname = script_getstr(st,3);
  13176. struct item_data *items[MAX_SEARCH];
  13177. int count;
  13178. char* name;
  13179. int32 start;
  13180. int32 id;
  13181. int32 i;
  13182. TBL_PC* sd = NULL;
  13183. if ((items[0] = itemdb_exists(atoi(itemname))))
  13184. count = 1;
  13185. else {
  13186. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  13187. if (count > MAX_SEARCH) count = MAX_SEARCH;
  13188. }
  13189. if (!count) {
  13190. script_pushint(st, 0);
  13191. return 0;
  13192. }
  13193. if( !data_isreference(data) )
  13194. {
  13195. ShowError("script:searchitem: not a variable\n");
  13196. script_reportdata(data);
  13197. st->state = END;
  13198. return 1;// not a variable
  13199. }
  13200. id = reference_getid(data);
  13201. start = reference_getindex(data);
  13202. name = reference_getname(data);
  13203. if( not_array_variable(*name) )
  13204. {
  13205. ShowError("script:searchitem: illegal scope\n");
  13206. script_reportdata(data);
  13207. st->state = END;
  13208. return 1;// not supported
  13209. }
  13210. if( not_server_variable(*name) )
  13211. {
  13212. sd = script_rid2sd(st);
  13213. if( sd == NULL )
  13214. return 0;// no player attached
  13215. }
  13216. if( is_string_variable(name) )
  13217. {// string array
  13218. ShowError("script:searchitem: not an integer array reference\n");
  13219. script_reportdata(data);
  13220. st->state = END;
  13221. return 1;// not supported
  13222. }
  13223. for( i = 0; i < count; ++start, ++i )
  13224. {// Set array
  13225. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  13226. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  13227. }
  13228. script_pushint(st, count);
  13229. return 0;
  13230. }
  13231. int axtoi(const char *hexStg)
  13232. {
  13233. int n = 0; // position in string
  13234. int16 m = 0; // position in digit[] to shift
  13235. int count; // loop index
  13236. int intValue = 0; // integer value of hex string
  13237. int digit[11]; // hold values to convert
  13238. while (n < 10) {
  13239. if (hexStg[n]=='\0')
  13240. break;
  13241. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  13242. digit[n] = hexStg[n] & 0x0f; //convert to int
  13243. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  13244. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13245. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  13246. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  13247. else break;
  13248. n++;
  13249. }
  13250. count = n;
  13251. m = n - 1;
  13252. n = 0;
  13253. while(n < count) {
  13254. // digit[n] is value of hex digit at position n
  13255. // (m << 2) is the number of positions to shift
  13256. // OR the bits into return value
  13257. intValue = intValue | (digit[n] << (m << 2));
  13258. m--; // adjust the position to set
  13259. n++; // next digit to process
  13260. }
  13261. return (intValue);
  13262. }
  13263. // [Lance] Hex string to integer converter
  13264. BUILDIN_FUNC(axtoi)
  13265. {
  13266. const char *hex = script_getstr(st,2);
  13267. script_pushint(st,axtoi(hex));
  13268. return 0;
  13269. }
  13270. // [zBuffer] List of player cont commands --->
  13271. BUILDIN_FUNC(rid2name)
  13272. {
  13273. struct block_list *bl = NULL;
  13274. int rid = script_getnum(st,2);
  13275. if((bl = map_id2bl(rid)))
  13276. {
  13277. switch(bl->type) {
  13278. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  13279. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  13280. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  13281. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  13282. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  13283. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  13284. default:
  13285. ShowError("buildin_rid2name: BL type unknown.\n");
  13286. script_pushconststr(st,"");
  13287. break;
  13288. }
  13289. } else {
  13290. ShowError("buildin_rid2name: invalid RID\n");
  13291. script_pushconststr(st,"(null)");
  13292. }
  13293. return 0;
  13294. }
  13295. BUILDIN_FUNC(pcblockmove)
  13296. {
  13297. int id, flag;
  13298. TBL_PC *sd = NULL;
  13299. id = script_getnum(st,2);
  13300. flag = script_getnum(st,3);
  13301. if(id)
  13302. sd = map_id2sd(id);
  13303. else
  13304. sd = script_rid2sd(st);
  13305. if(sd)
  13306. sd->state.blockedmove = flag > 0;
  13307. return 0;
  13308. }
  13309. BUILDIN_FUNC(pcfollow)
  13310. {
  13311. int id, targetid;
  13312. TBL_PC *sd = NULL;
  13313. id = script_getnum(st,2);
  13314. targetid = script_getnum(st,3);
  13315. if(id)
  13316. sd = map_id2sd(id);
  13317. else
  13318. sd = script_rid2sd(st);
  13319. if(sd)
  13320. pc_follow(sd, targetid);
  13321. return 0;
  13322. }
  13323. BUILDIN_FUNC(pcstopfollow)
  13324. {
  13325. int id;
  13326. TBL_PC *sd = NULL;
  13327. id = script_getnum(st,2);
  13328. if(id)
  13329. sd = map_id2sd(id);
  13330. else
  13331. sd = script_rid2sd(st);
  13332. if(sd)
  13333. pc_stop_following(sd);
  13334. return 0;
  13335. }
  13336. // <--- [zBuffer] List of player cont commands
  13337. // [zBuffer] List of mob control commands --->
  13338. //## TODO always return if the request/whatever was successfull [FlavioJS]
  13339. /// Makes the unit walk to target position or map
  13340. /// Returns if it was successfull
  13341. ///
  13342. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  13343. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  13344. BUILDIN_FUNC(unitwalk)
  13345. {
  13346. struct block_list* bl;
  13347. bl = map_id2bl(script_getnum(st,2));
  13348. if( bl == NULL )
  13349. {
  13350. script_pushint(st, 0);
  13351. }
  13352. else if( script_hasdata(st,4) )
  13353. {
  13354. int x = script_getnum(st,3);
  13355. int y = script_getnum(st,4);
  13356. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  13357. }
  13358. else
  13359. {
  13360. int map_id = script_getnum(st,3);
  13361. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  13362. }
  13363. return 0;
  13364. }
  13365. /// Kills the unit
  13366. ///
  13367. /// unitkill <unit_id>;
  13368. BUILDIN_FUNC(unitkill)
  13369. {
  13370. struct block_list* bl = map_id2bl(script_getnum(st,2));
  13371. if( bl != NULL )
  13372. status_kill(bl);
  13373. return 0;
  13374. }
  13375. /// Warps the unit to the target position in the target map
  13376. /// Returns if it was successfull
  13377. ///
  13378. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  13379. BUILDIN_FUNC(unitwarp)
  13380. {
  13381. int unit_id;
  13382. int map;
  13383. short x;
  13384. short y;
  13385. struct block_list* bl;
  13386. const char *mapname;
  13387. unit_id = script_getnum(st,2);
  13388. mapname = script_getstr(st, 3);
  13389. x = (short)script_getnum(st,4);
  13390. y = (short)script_getnum(st,5);
  13391. if (!unit_id) //Warp the script's runner
  13392. bl = map_id2bl(st->rid);
  13393. else
  13394. bl = map_id2bl(unit_id);
  13395. if( strcmp(mapname,"this") == 0 )
  13396. map = bl?bl->m:-1;
  13397. else
  13398. map = map_mapname2mapid(mapname);
  13399. if( map >= 0 && bl != NULL )
  13400. script_pushint(st, unit_warp(bl,map,x,y,CLR_OUTSIGHT));
  13401. else
  13402. script_pushint(st, 0);
  13403. return 0;
  13404. }
  13405. /// Makes the unit attack the target.
  13406. /// If the unit is a player and <action type> is not 0, it does a continuous
  13407. /// attack instead of a single attack.
  13408. /// Returns if the request was successfull.
  13409. ///
  13410. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  13411. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  13412. BUILDIN_FUNC(unitattack)
  13413. {
  13414. struct block_list* unit_bl;
  13415. struct block_list* target_bl = NULL;
  13416. struct script_data* data;
  13417. int actiontype = 0;
  13418. // get unit
  13419. unit_bl = map_id2bl(script_getnum(st,2));
  13420. if( unit_bl == NULL ) {
  13421. script_pushint(st, 0);
  13422. return 0;
  13423. }
  13424. data = script_getdata(st, 3);
  13425. get_val(st, data);
  13426. if( data_isstring(data) )
  13427. {
  13428. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  13429. if( sd != NULL )
  13430. target_bl = &sd->bl;
  13431. } else
  13432. target_bl = map_id2bl(conv_num(st, data));
  13433. // request the attack
  13434. if( target_bl == NULL )
  13435. {
  13436. script_pushint(st, 0);
  13437. return 0;
  13438. }
  13439. // get actiontype
  13440. if( script_hasdata(st,4) )
  13441. actiontype = script_getnum(st,4);
  13442. switch( unit_bl->type )
  13443. {
  13444. case BL_PC:
  13445. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  13446. script_pushint(st, 1);
  13447. return 0;
  13448. case BL_MOB:
  13449. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  13450. break;
  13451. case BL_PET:
  13452. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  13453. break;
  13454. default:
  13455. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  13456. script_pushint(st, 0);
  13457. return 1;
  13458. }
  13459. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  13460. return 0;
  13461. }
  13462. /// Makes the unit stop attacking and moving
  13463. ///
  13464. /// unitstop <unit_id>;
  13465. BUILDIN_FUNC(unitstop)
  13466. {
  13467. int unit_id;
  13468. struct block_list* bl;
  13469. unit_id = script_getnum(st,2);
  13470. bl = map_id2bl(unit_id);
  13471. if( bl != NULL )
  13472. {
  13473. unit_stop_attack(bl);
  13474. unit_stop_walking(bl,4);
  13475. if( bl->type == BL_MOB )
  13476. ((TBL_MOB*)bl)->target_id = 0;
  13477. }
  13478. return 0;
  13479. }
  13480. /// Makes the unit say the message
  13481. ///
  13482. /// unittalk <unit_id>,"<message>";
  13483. BUILDIN_FUNC(unittalk)
  13484. {
  13485. int unit_id;
  13486. const char* message;
  13487. struct block_list* bl;
  13488. unit_id = script_getnum(st,2);
  13489. message = script_getstr(st, 3);
  13490. bl = map_id2bl(unit_id);
  13491. if( bl != NULL )
  13492. {
  13493. struct StringBuf sbuf;
  13494. StringBuf_Init(&sbuf);
  13495. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  13496. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  13497. if( bl->type == BL_PC )
  13498. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  13499. StringBuf_Destroy(&sbuf);
  13500. }
  13501. return 0;
  13502. }
  13503. /// Makes the unit do an emotion
  13504. ///
  13505. /// unitemote <unit_id>,<emotion>;
  13506. ///
  13507. /// @see e_* in const.txt
  13508. BUILDIN_FUNC(unitemote)
  13509. {
  13510. int unit_id;
  13511. int emotion;
  13512. struct block_list* bl;
  13513. unit_id = script_getnum(st,2);
  13514. emotion = script_getnum(st,3);
  13515. bl = map_id2bl(unit_id);
  13516. if( bl != NULL )
  13517. clif_emotion(bl, emotion);
  13518. return 0;
  13519. }
  13520. /// Makes the unit cast the skill on the target or self if no target is specified
  13521. ///
  13522. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  13523. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  13524. BUILDIN_FUNC(unitskilluseid)
  13525. {
  13526. int unit_id;
  13527. uint16 skill_id;
  13528. uint16 skill_lv;
  13529. int target_id;
  13530. struct block_list* bl;
  13531. unit_id = script_getnum(st,2);
  13532. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13533. skill_lv = script_getnum(st,4);
  13534. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  13535. bl = map_id2bl(unit_id);
  13536. if( bl != NULL )
  13537. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  13538. return 0;
  13539. }
  13540. /// Makes the unit cast the skill on the target position.
  13541. ///
  13542. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  13543. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  13544. BUILDIN_FUNC(unitskillusepos)
  13545. {
  13546. int unit_id;
  13547. uint16 skill_id;
  13548. uint16 skill_lv;
  13549. int skill_x;
  13550. int skill_y;
  13551. struct block_list* bl;
  13552. unit_id = script_getnum(st,2);
  13553. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  13554. skill_lv = script_getnum(st,4);
  13555. skill_x = script_getnum(st,5);
  13556. skill_y = script_getnum(st,6);
  13557. bl = map_id2bl(unit_id);
  13558. if( bl != NULL )
  13559. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  13560. return 0;
  13561. }
  13562. // <--- [zBuffer] List of mob control commands
  13563. /// Pauses the execution of the script, detaching the player
  13564. ///
  13565. /// sleep <mili seconds>;
  13566. BUILDIN_FUNC(sleep)
  13567. {
  13568. int ticks;
  13569. ticks = script_getnum(st,2);
  13570. // detach the player
  13571. script_detach_rid(st);
  13572. if( ticks <= 0 )
  13573. {// do nothing
  13574. }
  13575. else if( st->sleep.tick == 0 )
  13576. {// sleep for the target amount of time
  13577. st->state = RERUNLINE;
  13578. st->sleep.tick = ticks;
  13579. }
  13580. else
  13581. {// sleep time is over
  13582. st->state = RUN;
  13583. st->sleep.tick = 0;
  13584. }
  13585. return 0;
  13586. }
  13587. /// Pauses the execution of the script, keeping the player attached
  13588. /// Returns if a player is still attached
  13589. ///
  13590. /// sleep2(<mili secconds>) -> <bool>
  13591. BUILDIN_FUNC(sleep2)
  13592. {
  13593. int ticks;
  13594. ticks = script_getnum(st,2);
  13595. if( ticks <= 0 )
  13596. {// do nothing
  13597. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13598. }
  13599. else if( !st->sleep.tick )
  13600. {// sleep for the target amount of time
  13601. st->state = RERUNLINE;
  13602. st->sleep.tick = ticks;
  13603. }
  13604. else
  13605. {// sleep time is over
  13606. st->state = RUN;
  13607. st->sleep.tick = 0;
  13608. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  13609. }
  13610. return 0;
  13611. }
  13612. /// Awakes all the sleep timers of the target npc
  13613. ///
  13614. /// awake "<npc name>";
  13615. BUILDIN_FUNC(awake)
  13616. {
  13617. struct npc_data* nd;
  13618. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  13619. nd = npc_name2id(script_getstr(st, 2));
  13620. if( nd == NULL ) {
  13621. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  13622. return 1;
  13623. }
  13624. while( node )
  13625. {
  13626. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  13627. {// sleep timer for the npc
  13628. struct script_state* tst = (struct script_state*)node->data;
  13629. TBL_PC* sd = map_id2sd(tst->rid);
  13630. if( tst->sleep.timer == INVALID_TIMER )
  13631. {// already awake ???
  13632. node = node->next;
  13633. continue;
  13634. }
  13635. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  13636. {// char not online anymore / another char of the same account is online - Cancel execution
  13637. tst->state = END;
  13638. tst->rid = 0;
  13639. }
  13640. delete_timer(tst->sleep.timer, run_script_timer);
  13641. node = script_erase_sleepdb(node);
  13642. tst->sleep.timer = INVALID_TIMER;
  13643. if(tst->state != RERUNLINE)
  13644. tst->sleep.tick = 0;
  13645. run_script_main(tst);
  13646. }
  13647. else
  13648. {
  13649. node = node->next;
  13650. }
  13651. }
  13652. return 0;
  13653. }
  13654. /// Returns a reference to a variable of the target NPC.
  13655. /// Returns 0 if an error occurs.
  13656. ///
  13657. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  13658. BUILDIN_FUNC(getvariableofnpc)
  13659. {
  13660. struct script_data* data;
  13661. const char* name;
  13662. struct npc_data* nd;
  13663. data = script_getdata(st,2);
  13664. if( !data_isreference(data) )
  13665. {// Not a reference (aka varaible name)
  13666. ShowError("script:getvariableofnpc: not a variable\n");
  13667. script_reportdata(data);
  13668. script_pushnil(st);
  13669. st->state = END;
  13670. return 1;
  13671. }
  13672. name = reference_getname(data);
  13673. if( *name != '.' || name[1] == '@' )
  13674. {// not a npc variable
  13675. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  13676. script_reportdata(data);
  13677. script_pushnil(st);
  13678. st->state = END;
  13679. return 1;
  13680. }
  13681. nd = npc_name2id(script_getstr(st,3));
  13682. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  13683. {// NPC not found or has no script
  13684. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  13685. script_pushnil(st);
  13686. st->state = END;
  13687. return 1;
  13688. }
  13689. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  13690. return 0;
  13691. }
  13692. /// Opens a warp portal.
  13693. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  13694. ///
  13695. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  13696. ///
  13697. /// @author blackhole89
  13698. BUILDIN_FUNC(warpportal)
  13699. {
  13700. int spx;
  13701. int spy;
  13702. unsigned short mapindex;
  13703. int tpx;
  13704. int tpy;
  13705. struct skill_unit_group* group;
  13706. struct block_list* bl;
  13707. bl = map_id2bl(st->oid);
  13708. if( bl == NULL )
  13709. {
  13710. ShowError("script:warpportal: npc is needed\n");
  13711. return 1;
  13712. }
  13713. spx = script_getnum(st,2);
  13714. spy = script_getnum(st,3);
  13715. mapindex = mapindex_name2id(script_getstr(st, 4));
  13716. tpx = script_getnum(st,5);
  13717. tpy = script_getnum(st,6);
  13718. if( mapindex == 0 )
  13719. return 0;// map not found
  13720. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  13721. if( group == NULL )
  13722. return 0;// failed
  13723. group->val1 = (group->val1<<16)|(short)0;
  13724. group->val2 = (tpx<<16) | tpy;
  13725. group->val3 = mapindex;
  13726. return 0;
  13727. }
  13728. BUILDIN_FUNC(openmail)
  13729. {
  13730. TBL_PC* sd;
  13731. sd = script_rid2sd(st);
  13732. if( sd == NULL )
  13733. return 0;
  13734. mail_openmail(sd);
  13735. return 0;
  13736. }
  13737. BUILDIN_FUNC(openauction)
  13738. {
  13739. TBL_PC* sd;
  13740. sd = script_rid2sd(st);
  13741. if( sd == NULL )
  13742. return 0;
  13743. clif_Auction_openwindow(sd);
  13744. return 0;
  13745. }
  13746. /// Retrieves the value of the specified flag of the specified cell.
  13747. ///
  13748. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  13749. ///
  13750. /// @see cell_chk* constants in const.txt for the types
  13751. BUILDIN_FUNC(checkcell)
  13752. {
  13753. int16 m = map_mapname2mapid(script_getstr(st,2));
  13754. int16 x = script_getnum(st,3);
  13755. int16 y = script_getnum(st,4);
  13756. cell_chk type = (cell_chk)script_getnum(st,5);
  13757. script_pushint(st, map_getcell(m, x, y, type));
  13758. return 0;
  13759. }
  13760. /// Modifies flags of cells in the specified area.
  13761. ///
  13762. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  13763. ///
  13764. /// @see cell_* constants in const.txt for the types
  13765. BUILDIN_FUNC(setcell)
  13766. {
  13767. int16 m = map_mapname2mapid(script_getstr(st,2));
  13768. int16 x1 = script_getnum(st,3);
  13769. int16 y1 = script_getnum(st,4);
  13770. int16 x2 = script_getnum(st,5);
  13771. int16 y2 = script_getnum(st,6);
  13772. cell_t type = (cell_t)script_getnum(st,7);
  13773. bool flag = (bool)script_getnum(st,8);
  13774. int x,y;
  13775. if( x1 > x2 ) swap(x1,x2);
  13776. if( y1 > y2 ) swap(y1,y2);
  13777. for( y = y1; y <= y2; ++y )
  13778. for( x = x1; x <= x2; ++x )
  13779. map_setcell(m, x, y, type, flag);
  13780. return 0;
  13781. }
  13782. /*==========================================
  13783. * Mercenary Commands
  13784. *------------------------------------------*/
  13785. BUILDIN_FUNC(mercenary_create)
  13786. {
  13787. struct map_session_data *sd;
  13788. int class_, contract_time;
  13789. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  13790. return 0;
  13791. class_ = script_getnum(st,2);
  13792. if( !merc_class(class_) )
  13793. return 0;
  13794. contract_time = script_getnum(st,3);
  13795. merc_create(sd, class_, contract_time);
  13796. return 0;
  13797. }
  13798. BUILDIN_FUNC(mercenary_heal)
  13799. {
  13800. struct map_session_data *sd = script_rid2sd(st);
  13801. int hp, sp;
  13802. if( sd == NULL || sd->md == NULL )
  13803. return 0;
  13804. hp = script_getnum(st,2);
  13805. sp = script_getnum(st,3);
  13806. status_heal(&sd->md->bl, hp, sp, 0);
  13807. return 0;
  13808. }
  13809. BUILDIN_FUNC(mercenary_sc_start)
  13810. {
  13811. struct map_session_data *sd = script_rid2sd(st);
  13812. enum sc_type type;
  13813. int tick, val1;
  13814. if( sd == NULL || sd->md == NULL )
  13815. return 0;
  13816. type = (sc_type)script_getnum(st,2);
  13817. tick = script_getnum(st,3);
  13818. val1 = script_getnum(st,4);
  13819. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  13820. return 0;
  13821. }
  13822. BUILDIN_FUNC(mercenary_get_calls)
  13823. {
  13824. struct map_session_data *sd = script_rid2sd(st);
  13825. int guild;
  13826. if( sd == NULL )
  13827. return 0;
  13828. guild = script_getnum(st,2);
  13829. switch( guild )
  13830. {
  13831. case ARCH_MERC_GUILD:
  13832. script_pushint(st,sd->status.arch_calls);
  13833. break;
  13834. case SPEAR_MERC_GUILD:
  13835. script_pushint(st,sd->status.spear_calls);
  13836. break;
  13837. case SWORD_MERC_GUILD:
  13838. script_pushint(st,sd->status.sword_calls);
  13839. break;
  13840. default:
  13841. script_pushint(st,0);
  13842. break;
  13843. }
  13844. return 0;
  13845. }
  13846. BUILDIN_FUNC(mercenary_set_calls)
  13847. {
  13848. struct map_session_data *sd = script_rid2sd(st);
  13849. int guild, value, *calls;
  13850. if( sd == NULL )
  13851. return 0;
  13852. guild = script_getnum(st,2);
  13853. value = script_getnum(st,3);
  13854. switch( guild )
  13855. {
  13856. case ARCH_MERC_GUILD:
  13857. calls = &sd->status.arch_calls;
  13858. break;
  13859. case SPEAR_MERC_GUILD:
  13860. calls = &sd->status.spear_calls;
  13861. break;
  13862. case SWORD_MERC_GUILD:
  13863. calls = &sd->status.sword_calls;
  13864. break;
  13865. default:
  13866. return 0; // Invalid Guild
  13867. }
  13868. *calls += value;
  13869. *calls = cap_value(*calls, 0, INT_MAX);
  13870. return 0;
  13871. }
  13872. BUILDIN_FUNC(mercenary_get_faith)
  13873. {
  13874. struct map_session_data *sd = script_rid2sd(st);
  13875. int guild;
  13876. if( sd == NULL )
  13877. return 0;
  13878. guild = script_getnum(st,2);
  13879. switch( guild )
  13880. {
  13881. case ARCH_MERC_GUILD:
  13882. script_pushint(st,sd->status.arch_faith);
  13883. break;
  13884. case SPEAR_MERC_GUILD:
  13885. script_pushint(st,sd->status.spear_faith);
  13886. break;
  13887. case SWORD_MERC_GUILD:
  13888. script_pushint(st,sd->status.sword_faith);
  13889. break;
  13890. default:
  13891. script_pushint(st,0);
  13892. break;
  13893. }
  13894. return 0;
  13895. }
  13896. BUILDIN_FUNC(mercenary_set_faith)
  13897. {
  13898. struct map_session_data *sd = script_rid2sd(st);
  13899. int guild, value, *calls;
  13900. if( sd == NULL )
  13901. return 0;
  13902. guild = script_getnum(st,2);
  13903. value = script_getnum(st,3);
  13904. switch( guild )
  13905. {
  13906. case ARCH_MERC_GUILD:
  13907. calls = &sd->status.arch_faith;
  13908. break;
  13909. case SPEAR_MERC_GUILD:
  13910. calls = &sd->status.spear_faith;
  13911. break;
  13912. case SWORD_MERC_GUILD:
  13913. calls = &sd->status.sword_faith;
  13914. break;
  13915. default:
  13916. return 0; // Invalid Guild
  13917. }
  13918. *calls += value;
  13919. *calls = cap_value(*calls, 0, INT_MAX);
  13920. if( mercenary_get_guild(sd->md) == guild )
  13921. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  13922. return 0;
  13923. }
  13924. /*------------------------------------------
  13925. * Book Reading
  13926. *------------------------------------------*/
  13927. BUILDIN_FUNC(readbook)
  13928. {
  13929. struct map_session_data *sd;
  13930. int book_id, page;
  13931. if( (sd = script_rid2sd(st)) == NULL )
  13932. return 0;
  13933. book_id = script_getnum(st,2);
  13934. page = script_getnum(st,3);
  13935. clif_readbook(sd->fd, book_id, page);
  13936. return 0;
  13937. }
  13938. /******************
  13939. Questlog script commands
  13940. *******************/
  13941. BUILDIN_FUNC(setquest)
  13942. {
  13943. struct map_session_data *sd = script_rid2sd(st);
  13944. nullpo_ret(sd);
  13945. quest_add(sd, script_getnum(st, 2));
  13946. return 0;
  13947. }
  13948. BUILDIN_FUNC(erasequest)
  13949. {
  13950. struct map_session_data *sd = script_rid2sd(st);
  13951. nullpo_ret(sd);
  13952. quest_delete(sd, script_getnum(st, 2));
  13953. return 0;
  13954. }
  13955. BUILDIN_FUNC(completequest)
  13956. {
  13957. struct map_session_data *sd = script_rid2sd(st);
  13958. nullpo_ret(sd);
  13959. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  13960. return 0;
  13961. }
  13962. BUILDIN_FUNC(changequest)
  13963. {
  13964. struct map_session_data *sd = script_rid2sd(st);
  13965. nullpo_ret(sd);
  13966. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  13967. return 0;
  13968. }
  13969. BUILDIN_FUNC(checkquest)
  13970. {
  13971. struct map_session_data *sd = script_rid2sd(st);
  13972. quest_check_type type = HAVEQUEST;
  13973. nullpo_ret(sd);
  13974. if( script_hasdata(st, 3) )
  13975. type = (quest_check_type)script_getnum(st, 3);
  13976. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  13977. return 0;
  13978. }
  13979. BUILDIN_FUNC(showevent)
  13980. {
  13981. TBL_PC *sd = script_rid2sd(st);
  13982. struct npc_data *nd = map_id2nd(st->oid);
  13983. int state, color;
  13984. if( sd == NULL || nd == NULL )
  13985. return 0;
  13986. state = script_getnum(st, 2);
  13987. color = script_getnum(st, 3);
  13988. if( color < 0 || color > 3 )
  13989. color = 0; // set default color
  13990. clif_quest_show_event(sd, &nd->bl, state, color);
  13991. return 0;
  13992. }
  13993. /*==========================================
  13994. * BattleGround System
  13995. *------------------------------------------*/
  13996. BUILDIN_FUNC(waitingroom2bg)
  13997. {
  13998. struct npc_data *nd;
  13999. struct chat_data *cd;
  14000. const char *map_name, *ev = "", *dev = "";
  14001. int x, y, i, mapindex = 0, bg_id, n;
  14002. struct map_session_data *sd;
  14003. if( script_hasdata(st,7) )
  14004. nd = npc_name2id(script_getstr(st,7));
  14005. else
  14006. nd = (struct npc_data *)map_id2bl(st->oid);
  14007. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  14008. {
  14009. script_pushint(st,0);
  14010. return 0;
  14011. }
  14012. map_name = script_getstr(st,2);
  14013. if( strcmp(map_name,"-") != 0 )
  14014. {
  14015. mapindex = mapindex_name2id(map_name);
  14016. if( mapindex == 0 )
  14017. { // Invalid Map
  14018. script_pushint(st,0);
  14019. return 0;
  14020. }
  14021. }
  14022. x = script_getnum(st,3);
  14023. y = script_getnum(st,4);
  14024. ev = script_getstr(st,5); // Logout Event
  14025. dev = script_getstr(st,6); // Die Event
  14026. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  14027. { // Creation failed
  14028. script_pushint(st,0);
  14029. return 0;
  14030. }
  14031. n = cd->users;
  14032. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  14033. {
  14034. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  14035. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  14036. else
  14037. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  14038. }
  14039. mapreg_setreg(add_str("$@arenamembersnum"), i);
  14040. script_pushint(st,bg_id);
  14041. return 0;
  14042. }
  14043. BUILDIN_FUNC(waitingroom2bg_single)
  14044. {
  14045. const char* map_name;
  14046. struct npc_data *nd;
  14047. struct chat_data *cd;
  14048. struct map_session_data *sd;
  14049. int x, y, mapindex, bg_id;
  14050. bg_id = script_getnum(st,2);
  14051. map_name = script_getstr(st,3);
  14052. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14053. return 0; // Invalid Map
  14054. x = script_getnum(st,4);
  14055. y = script_getnum(st,5);
  14056. nd = npc_name2id(script_getstr(st,6));
  14057. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  14058. return 0;
  14059. if( (sd = cd->usersd[0]) == NULL )
  14060. return 0;
  14061. if( bg_team_join(bg_id, sd) )
  14062. {
  14063. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  14064. script_pushint(st,1);
  14065. }
  14066. else
  14067. script_pushint(st,0);
  14068. return 0;
  14069. }
  14070. BUILDIN_FUNC(bg_team_setxy)
  14071. {
  14072. struct battleground_data *bg;
  14073. int bg_id;
  14074. bg_id = script_getnum(st,2);
  14075. if( (bg = bg_team_search(bg_id)) == NULL )
  14076. return 0;
  14077. bg->x = script_getnum(st,3);
  14078. bg->y = script_getnum(st,4);
  14079. return 0;
  14080. }
  14081. BUILDIN_FUNC(bg_warp)
  14082. {
  14083. int x, y, mapindex, bg_id;
  14084. const char* map_name;
  14085. bg_id = script_getnum(st,2);
  14086. map_name = script_getstr(st,3);
  14087. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  14088. return 0; // Invalid Map
  14089. x = script_getnum(st,4);
  14090. y = script_getnum(st,5);
  14091. bg_team_warp(bg_id, mapindex, x, y);
  14092. return 0;
  14093. }
  14094. BUILDIN_FUNC(bg_monster)
  14095. {
  14096. int class_ = 0, x = 0, y = 0, bg_id = 0;
  14097. const char *str,*map, *evt="";
  14098. bg_id = script_getnum(st,2);
  14099. map = script_getstr(st,3);
  14100. x = script_getnum(st,4);
  14101. y = script_getnum(st,5);
  14102. str = script_getstr(st,6);
  14103. class_ = script_getnum(st,7);
  14104. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  14105. check_event(st, evt);
  14106. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  14107. return 0;
  14108. }
  14109. BUILDIN_FUNC(bg_monster_set_team)
  14110. {
  14111. struct mob_data *md;
  14112. struct block_list *mbl;
  14113. int id = script_getnum(st,2),
  14114. bg_id = script_getnum(st,3);
  14115. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  14116. return 0;
  14117. md = (TBL_MOB *)mbl;
  14118. md->bg_id = bg_id;
  14119. mob_stop_attack(md);
  14120. mob_stop_walking(md, 0);
  14121. md->target_id = md->attacked_id = 0;
  14122. clif_charnameack(0, &md->bl);
  14123. return 0;
  14124. }
  14125. BUILDIN_FUNC(bg_leave)
  14126. {
  14127. struct map_session_data *sd = script_rid2sd(st);
  14128. if( sd == NULL || !sd->bg_id )
  14129. return 0;
  14130. bg_team_leave(sd,0);
  14131. return 0;
  14132. }
  14133. BUILDIN_FUNC(bg_destroy)
  14134. {
  14135. int bg_id = script_getnum(st,2);
  14136. bg_team_delete(bg_id);
  14137. return 0;
  14138. }
  14139. BUILDIN_FUNC(bg_getareausers)
  14140. {
  14141. const char *str;
  14142. int16 m, x0, y0, x1, y1;
  14143. int bg_id;
  14144. int i = 0, c = 0;
  14145. struct battleground_data *bg = NULL;
  14146. struct map_session_data *sd;
  14147. bg_id = script_getnum(st,2);
  14148. str = script_getstr(st,3);
  14149. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  14150. {
  14151. script_pushint(st,0);
  14152. return 0;
  14153. }
  14154. x0 = script_getnum(st,4);
  14155. y0 = script_getnum(st,5);
  14156. x1 = script_getnum(st,6);
  14157. y1 = script_getnum(st,7);
  14158. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  14159. {
  14160. if( (sd = bg->members[i].sd) == NULL )
  14161. continue;
  14162. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  14163. continue;
  14164. c++;
  14165. }
  14166. script_pushint(st,c);
  14167. return 0;
  14168. }
  14169. BUILDIN_FUNC(bg_updatescore)
  14170. {
  14171. const char *str;
  14172. int16 m;
  14173. str = script_getstr(st,2);
  14174. if( (m = map_mapname2mapid(str)) < 0 )
  14175. return 0;
  14176. map[m].bgscore_lion = script_getnum(st,3);
  14177. map[m].bgscore_eagle = script_getnum(st,4);
  14178. clif_bg_updatescore(m);
  14179. return 0;
  14180. }
  14181. BUILDIN_FUNC(bg_get_data)
  14182. {
  14183. struct battleground_data *bg;
  14184. int bg_id = script_getnum(st,2),
  14185. type = script_getnum(st,3);
  14186. if( (bg = bg_team_search(bg_id)) == NULL )
  14187. {
  14188. script_pushint(st,0);
  14189. return 0;
  14190. }
  14191. switch( type )
  14192. {
  14193. case 0: script_pushint(st, bg->count); break;
  14194. default:
  14195. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  14196. break;
  14197. }
  14198. return 0;
  14199. }
  14200. /*==========================================
  14201. * Instancing Script Commands
  14202. *------------------------------------------*/
  14203. BUILDIN_FUNC(instance_create)
  14204. {
  14205. const char *name;
  14206. int party_id, res;
  14207. name = script_getstr(st, 2);
  14208. party_id = script_getnum(st, 3);
  14209. res = instance_create(party_id, name);
  14210. if( res == -4 ) // Already exists
  14211. {
  14212. script_pushint(st, -1);
  14213. return 0;
  14214. }
  14215. else if( res < 0 )
  14216. {
  14217. const char *err;
  14218. switch(res)
  14219. {
  14220. case -3: err = "No free instances"; break;
  14221. case -2: err = "Invalid party ID"; break;
  14222. case -1: err = "Invalid type"; break;
  14223. default: err = "Unknown"; break;
  14224. }
  14225. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  14226. script_pushint(st, -2);
  14227. return 0;
  14228. }
  14229. script_pushint(st, res);
  14230. return 0;
  14231. }
  14232. BUILDIN_FUNC(instance_destroy)
  14233. {
  14234. int instance_id;
  14235. struct map_session_data *sd;
  14236. struct party_data *p;
  14237. if( script_hasdata(st, 2) )
  14238. instance_id = script_getnum(st, 2);
  14239. else if( st->instance_id )
  14240. instance_id = st->instance_id;
  14241. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14242. instance_id = p->instance_id;
  14243. else return 0;
  14244. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  14245. {
  14246. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  14247. return 0;
  14248. }
  14249. instance_destroy(instance_id);
  14250. return 0;
  14251. }
  14252. BUILDIN_FUNC(instance_attachmap)
  14253. {
  14254. const char *name;
  14255. int16 m;
  14256. int instance_id;
  14257. bool usebasename = false;
  14258. name = script_getstr(st,2);
  14259. instance_id = script_getnum(st,3);
  14260. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  14261. usebasename = true;
  14262. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  14263. {
  14264. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  14265. script_pushconststr(st, "");
  14266. return 0;
  14267. }
  14268. script_pushconststr(st, map[m].name);
  14269. return 0;
  14270. }
  14271. BUILDIN_FUNC(instance_detachmap)
  14272. {
  14273. struct map_session_data *sd;
  14274. struct party_data *p;
  14275. const char *str;
  14276. int16 m;
  14277. int instance_id;
  14278. str = script_getstr(st, 2);
  14279. if( script_hasdata(st, 3) )
  14280. instance_id = script_getnum(st, 3);
  14281. else if( st->instance_id )
  14282. instance_id = st->instance_id;
  14283. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14284. instance_id = p->instance_id;
  14285. else return 0;
  14286. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  14287. {
  14288. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  14289. return 0;
  14290. }
  14291. instance_del_map(m);
  14292. return 0;
  14293. }
  14294. BUILDIN_FUNC(instance_attach)
  14295. {
  14296. int instance_id;
  14297. instance_id = script_getnum(st, 2);
  14298. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  14299. return 0;
  14300. st->instance_id = instance_id;
  14301. return 0;
  14302. }
  14303. BUILDIN_FUNC(instance_id)
  14304. {
  14305. int instance_id;
  14306. if( script_hasdata(st, 2) )
  14307. {
  14308. struct map_session_data *sd;
  14309. struct party_data *p;
  14310. int type;
  14311. type = script_getnum(st, 2);
  14312. if( type == 0 )
  14313. instance_id = st->instance_id;
  14314. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  14315. instance_id = p->instance_id;
  14316. else
  14317. instance_id = 0;
  14318. }
  14319. else
  14320. instance_id = st->instance_id;
  14321. script_pushint(st, instance_id);
  14322. return 0;
  14323. }
  14324. BUILDIN_FUNC(instance_set_timeout)
  14325. {
  14326. int progress_timeout, idle_timeout;
  14327. int instance_id;
  14328. struct map_session_data *sd;
  14329. struct party_data *p;
  14330. progress_timeout = script_getnum(st, 2);
  14331. idle_timeout = script_getnum(st, 3);
  14332. if( script_hasdata(st, 4) )
  14333. instance_id = script_getnum(st, 4);
  14334. else if( st->instance_id )
  14335. instance_id = st->instance_id;
  14336. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14337. instance_id = p->instance_id;
  14338. else return 0;
  14339. if( instance_id > 0 )
  14340. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  14341. return 0;
  14342. }
  14343. BUILDIN_FUNC(instance_init)
  14344. {
  14345. int instance_id = script_getnum(st, 2);
  14346. if( instance[instance_id].state != INSTANCE_IDLE )
  14347. {
  14348. ShowError("instance_init: instance already initialized.\n");
  14349. return 0;
  14350. }
  14351. instance_init(instance_id);
  14352. return 0;
  14353. }
  14354. BUILDIN_FUNC(instance_announce)
  14355. {
  14356. int instance_id = script_getnum(st,2);
  14357. const char *mes = script_getstr(st,3);
  14358. int flag = script_getnum(st,4);
  14359. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  14360. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  14361. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  14362. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  14363. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  14364. int i;
  14365. struct map_session_data *sd;
  14366. struct party_data *p;
  14367. if( instance_id == 0 )
  14368. {
  14369. if( st->instance_id )
  14370. instance_id = st->instance_id;
  14371. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14372. instance_id = p->instance_id;
  14373. else return 0;
  14374. }
  14375. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  14376. return 0;
  14377. for( i = 0; i < instance[instance_id].num_map; i++ )
  14378. map_foreachinmap(buildin_announce_sub, instance[instance_id].map[i], BL_PC,
  14379. mes, strlen(mes)+1, flag&0xf0, fontColor, fontType, fontSize, fontAlign, fontY);
  14380. return 0;
  14381. }
  14382. BUILDIN_FUNC(instance_npcname)
  14383. {
  14384. const char *str;
  14385. int instance_id = 0;
  14386. struct map_session_data *sd;
  14387. struct party_data *p;
  14388. struct npc_data *nd;
  14389. str = script_getstr(st, 2);
  14390. if( script_hasdata(st, 3) )
  14391. instance_id = script_getnum(st, 3);
  14392. else if( st->instance_id )
  14393. instance_id = st->instance_id;
  14394. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14395. instance_id = p->instance_id;
  14396. if( instance_id && (nd = npc_name2id(str)) != NULL )
  14397. {
  14398. static char npcname[NAME_LENGTH];
  14399. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  14400. script_pushconststr(st,npcname);
  14401. }
  14402. else
  14403. {
  14404. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  14405. st->state = END;
  14406. return 1;
  14407. }
  14408. return 0;
  14409. }
  14410. BUILDIN_FUNC(has_instance)
  14411. {
  14412. struct map_session_data *sd;
  14413. struct party_data *p;
  14414. const char *str;
  14415. int16 m;
  14416. int instance_id = 0;
  14417. str = script_getstr(st, 2);
  14418. if( script_hasdata(st, 3) )
  14419. instance_id = script_getnum(st, 3);
  14420. else if( st->instance_id )
  14421. instance_id = st->instance_id;
  14422. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  14423. instance_id = p->instance_id;
  14424. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  14425. {
  14426. script_pushconststr(st, "");
  14427. return 0;
  14428. }
  14429. script_pushconststr(st, map[m].name);
  14430. return 0;
  14431. }
  14432. BUILDIN_FUNC(instance_warpall)
  14433. {
  14434. struct map_session_data *pl_sd;
  14435. int16 m, i;
  14436. int instance_id;
  14437. const char *mapn;
  14438. int x, y;
  14439. unsigned short mapindex;
  14440. struct party_data *p = NULL;
  14441. mapn = script_getstr(st,2);
  14442. x = script_getnum(st,3);
  14443. y = script_getnum(st,4);
  14444. if( script_hasdata(st,5) )
  14445. instance_id = script_getnum(st,5);
  14446. else if( st->instance_id )
  14447. instance_id = st->instance_id;
  14448. else if( (pl_sd = script_rid2sd(st)) != NULL && pl_sd->status.party_id && (p = party_search(pl_sd->status.party_id)) != NULL && p->instance_id )
  14449. instance_id = p->instance_id;
  14450. else return 0;
  14451. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  14452. return 0;
  14453. if( !(p = party_search(instance[instance_id].party_id)) )
  14454. return 0;
  14455. mapindex = map_id2index(m);
  14456. for( i = 0; i < MAX_PARTY; i++ )
  14457. if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == st->instance_id ) pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  14458. return 0;
  14459. }
  14460. /*==========================================
  14461. * instance_check_party [malufett]
  14462. * Values:
  14463. * party_id : Party ID of the invoking character. [Required Parameter]
  14464. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  14465. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  14466. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  14467. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  14468. * Example 2: instance_check_party (getcharid(1),1,1,99);
  14469. *------------------------------------------*/
  14470. BUILDIN_FUNC(instance_check_party)
  14471. {
  14472. struct map_session_data *pl_sd;
  14473. int amount, min, max, i, party_id, c = 0;
  14474. struct party_data *p = NULL;
  14475. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  14476. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  14477. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  14478. if( min < 1 || min > MAX_LEVEL){
  14479. ShowError("instance_check_party: Invalid min level, %d\n", min);
  14480. return 0;
  14481. }else if( max < 1 || max > MAX_LEVEL){
  14482. ShowError("instance_check_party: Invalid max level, %d\n", max);
  14483. return 0;
  14484. }
  14485. if( script_hasdata(st,2) )
  14486. party_id = script_getnum(st,2);
  14487. else return 0;
  14488. if( !(p = party_search(party_id)) ){
  14489. script_pushint(st, 0); // Returns false if party does not exist.
  14490. return 0;
  14491. }
  14492. for( i = 0; i < MAX_PARTY; i++ )
  14493. if( (pl_sd = p->data[i].sd) )
  14494. if(map_id2bl(pl_sd->bl.id)){
  14495. if(pl_sd->status.base_level < min){
  14496. script_pushint(st, 0);
  14497. return 0;
  14498. }else if(pl_sd->status.base_level > max){
  14499. script_pushint(st, 0);
  14500. return 0;
  14501. }
  14502. c++;
  14503. }
  14504. if(c < amount){
  14505. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  14506. }else
  14507. script_pushint(st, 1);
  14508. return 0;
  14509. }
  14510. /*==========================================
  14511. * Custom Fonts
  14512. *------------------------------------------*/
  14513. BUILDIN_FUNC(setfont)
  14514. {
  14515. struct map_session_data *sd = script_rid2sd(st);
  14516. int font = script_getnum(st,2);
  14517. if( sd == NULL )
  14518. return 0;
  14519. if( sd->user_font != font )
  14520. sd->user_font = font;
  14521. else
  14522. sd->user_font = 0;
  14523. clif_font(sd);
  14524. return 0;
  14525. }
  14526. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  14527. {
  14528. TBL_MOB* md = (TBL_MOB*)bl;
  14529. struct block_list *tbl;
  14530. int mobid = va_arg(ap,int);
  14531. uint16 skill_id = va_arg(ap,int);
  14532. uint16 skill_lv = va_arg(ap,int);
  14533. int casttime = va_arg(ap,int);
  14534. int cancel = va_arg(ap,int);
  14535. int emotion = va_arg(ap,int);
  14536. int target = va_arg(ap,int);
  14537. if( md->class_ != mobid )
  14538. return 0;
  14539. // 0:self, 1:target, 2:master, default:random
  14540. switch( target )
  14541. {
  14542. case 0: tbl = map_id2bl(md->bl.id); break;
  14543. case 1: tbl = map_id2bl(md->target_id); break;
  14544. case 2: tbl = map_id2bl(md->master_id); break;
  14545. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  14546. }
  14547. if( !tbl )
  14548. return 0;
  14549. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  14550. unit_skillcastcancel(bl,0);
  14551. if( skill_get_casttype(skill_id) == CAST_GROUND )
  14552. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  14553. else
  14554. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  14555. clif_emotion(&md->bl, emotion);
  14556. return 0;
  14557. }
  14558. /*==========================================
  14559. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  14560. *------------------------------------------*/
  14561. BUILDIN_FUNC(areamobuseskill)
  14562. {
  14563. struct block_list center;
  14564. int16 m;
  14565. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  14566. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  14567. {
  14568. ShowError("areamobuseskill: invalid map name.\n");
  14569. return 0;
  14570. }
  14571. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  14572. return 0;
  14573. center.m = m;
  14574. center.x = script_getnum(st,3);
  14575. center.y = script_getnum(st,4);
  14576. range = script_getnum(st,5);
  14577. mobid = script_getnum(st,6);
  14578. skill_id = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  14579. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  14580. skill_lv = battle_config.mob_max_skilllvl;
  14581. casttime = script_getnum(st,9);
  14582. cancel = script_getnum(st,10);
  14583. emotion = script_getnum(st,11);
  14584. target = script_getnum(st,12);
  14585. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  14586. return 0;
  14587. }
  14588. BUILDIN_FUNC(progressbar)
  14589. {
  14590. struct map_session_data * sd = script_rid2sd(st);
  14591. const char * color;
  14592. unsigned int second;
  14593. if( !st || !sd )
  14594. return 0;
  14595. st->state = STOP;
  14596. color = script_getstr(st,2);
  14597. second = script_getnum(st,3);
  14598. sd->progressbar.npc_id = st->oid;
  14599. sd->progressbar.timeout = gettick() + second*1000;
  14600. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  14601. return 0;
  14602. }
  14603. BUILDIN_FUNC(pushpc)
  14604. {
  14605. uint8 dir;
  14606. int cells, dx, dy;
  14607. struct map_session_data* sd;
  14608. if((sd = script_rid2sd(st))==NULL)
  14609. {
  14610. return 0;
  14611. }
  14612. dir = script_getnum(st,2);
  14613. cells = script_getnum(st,3);
  14614. if(dir>7)
  14615. {
  14616. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  14617. script_reportsrc(st);
  14618. dir%= 8; // trim spin-over
  14619. }
  14620. if(!cells)
  14621. {// zero distance
  14622. return 0;
  14623. }
  14624. else if(cells<0)
  14625. {// pushing backwards
  14626. dir = (dir+4)%8; // turn around
  14627. cells = -cells;
  14628. }
  14629. dx = dirx[dir];
  14630. dy = diry[dir];
  14631. unit_blown(&sd->bl, dx, dy, cells, 0);
  14632. return 0;
  14633. }
  14634. /// Invokes buying store preparation window
  14635. /// buyingstore <slots>;
  14636. BUILDIN_FUNC(buyingstore)
  14637. {
  14638. struct map_session_data* sd;
  14639. if( ( sd = script_rid2sd(st) ) == NULL )
  14640. {
  14641. return 0;
  14642. }
  14643. if( npc_isnear(&sd->bl) ) {
  14644. char output[150];
  14645. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  14646. clif_displaymessage(sd->fd, output);
  14647. return 0;
  14648. }
  14649. buyingstore_setup(sd, script_getnum(st,2));
  14650. return 0;
  14651. }
  14652. /// Invokes search store info window
  14653. /// searchstores <uses>,<effect>;
  14654. BUILDIN_FUNC(searchstores)
  14655. {
  14656. unsigned short effect;
  14657. unsigned int uses;
  14658. struct map_session_data* sd;
  14659. if( ( sd = script_rid2sd(st) ) == NULL )
  14660. {
  14661. return 0;
  14662. }
  14663. uses = script_getnum(st,2);
  14664. effect = script_getnum(st,3);
  14665. if( !uses )
  14666. {
  14667. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  14668. return 1;
  14669. }
  14670. if( effect > 1 )
  14671. {
  14672. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  14673. return 1;
  14674. }
  14675. searchstore_open(sd, uses, effect);
  14676. return 0;
  14677. }
  14678. /// Displays a number as large digital clock.
  14679. /// showdigit <value>[,<type>];
  14680. BUILDIN_FUNC(showdigit)
  14681. {
  14682. unsigned int type = 0;
  14683. int value;
  14684. struct map_session_data* sd;
  14685. if( ( sd = script_rid2sd(st) ) == NULL )
  14686. {
  14687. return 0;
  14688. }
  14689. value = script_getnum(st,2);
  14690. if( script_hasdata(st,3) )
  14691. {
  14692. type = script_getnum(st,3);
  14693. if( type > 3 )
  14694. {
  14695. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  14696. return 1;
  14697. }
  14698. }
  14699. clif_showdigit(sd, (unsigned char)type, value);
  14700. return 0;
  14701. }
  14702. /**
  14703. * Rune Knight
  14704. **/
  14705. BUILDIN_FUNC(makerune) {
  14706. TBL_PC* sd;
  14707. if( (sd = script_rid2sd(st)) == NULL )
  14708. return 0;
  14709. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  14710. sd->itemid = script_getnum(st,2);
  14711. return 0;
  14712. }
  14713. /**
  14714. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  14715. **/
  14716. BUILDIN_FUNC(checkdragon) {
  14717. TBL_PC* sd;
  14718. if( (sd = script_rid2sd(st)) == NULL )
  14719. return 0;
  14720. if( pc_isridingdragon(sd) )
  14721. script_pushint(st,1);
  14722. else
  14723. script_pushint(st,0);
  14724. return 0;
  14725. }
  14726. /**
  14727. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  14728. * - Toggles the dragon on a RK if he can mount;
  14729. * @param Color - when not provided uses the green dragon;
  14730. * - 1 : Green Dragon
  14731. * - 2 : Brown Dragon
  14732. * - 3 : Gray Dragon
  14733. * - 4 : Blue Dragon
  14734. * - 5 : Red Dragon
  14735. **/
  14736. BUILDIN_FUNC(setdragon) {
  14737. TBL_PC* sd;
  14738. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  14739. if( (sd = script_rid2sd(st)) == NULL )
  14740. return 0;
  14741. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  14742. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  14743. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  14744. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  14745. script_pushint(st,1);
  14746. } else {//Not mounted; Mount now.
  14747. unsigned int option = OPTION_DRAGON1;
  14748. if( color ) {
  14749. option = ( color == 1 ? OPTION_DRAGON1 :
  14750. color == 2 ? OPTION_DRAGON2 :
  14751. color == 3 ? OPTION_DRAGON3 :
  14752. color == 4 ? OPTION_DRAGON4 :
  14753. color == 5 ? OPTION_DRAGON5 : 0);
  14754. if( !option ) {
  14755. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  14756. option = OPTION_DRAGON1;
  14757. }
  14758. }
  14759. pc_setoption(sd, sd->sc.option|option);
  14760. script_pushint(st,1);
  14761. }
  14762. return 0;
  14763. }
  14764. /**
  14765. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  14766. **/
  14767. BUILDIN_FUNC(ismounting) {
  14768. TBL_PC* sd;
  14769. if( (sd = script_rid2sd(st)) == NULL )
  14770. return 0;
  14771. if( sd->sc.option&OPTION_MOUNTING )
  14772. script_pushint(st,1);
  14773. else
  14774. script_pushint(st,0);
  14775. return 0;
  14776. }
  14777. /**
  14778. * setmounting() returns 1 on success or 0 otherwise
  14779. * - Toggles new mounts on a player when he can mount
  14780. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  14781. * - Will unmount the player is he is already mounting
  14782. **/
  14783. BUILDIN_FUNC(setmounting) {
  14784. TBL_PC* sd;
  14785. if( (sd = script_rid2sd(st)) == NULL )
  14786. return 0;
  14787. if( sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) )
  14788. script_pushint(st,0);//can't mount with one of these
  14789. else {
  14790. if( sd->sc.option&OPTION_MOUNTING )
  14791. pc_setoption(sd, sd->sc.option&~OPTION_MOUNTING);//release mount
  14792. else
  14793. pc_setoption(sd, sd->sc.option|OPTION_MOUNTING);//mount
  14794. script_pushint(st,1);//in both cases, return 1.
  14795. }
  14796. return 0;
  14797. }
  14798. /**
  14799. * Retrieves quantity of arguments provided to callfunc/callsub.
  14800. * getargcount() -> amount of arguments received in a function
  14801. **/
  14802. BUILDIN_FUNC(getargcount) {
  14803. struct script_retinfo* ri;
  14804. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  14805. ShowError("script:getargcount: used out of function or callsub label!\n");
  14806. st->state = END;
  14807. return 1;
  14808. }
  14809. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  14810. script_pushint(st, ri->nargs);
  14811. return 0;
  14812. }
  14813. /**
  14814. * getcharip(<account ID>/<character ID>/<character name>)
  14815. **/
  14816. BUILDIN_FUNC(getcharip)
  14817. {
  14818. struct map_session_data* sd = NULL;
  14819. /* check if a character name is specified */
  14820. if( script_hasdata(st, 2) )
  14821. {
  14822. if (script_isstring(st, 2))
  14823. sd = map_nick2sd(script_getstr(st, 2));
  14824. else if (script_isint(st, 2) || script_getnum(st, 2))
  14825. {
  14826. int id = 0;
  14827. id = script_getnum(st, 2);
  14828. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  14829. }
  14830. }
  14831. else
  14832. sd = script_rid2sd(st);
  14833. /* check for sd and IP */
  14834. if (!sd || !session[sd->fd]->client_addr)
  14835. {
  14836. script_pushconststr(st, "");
  14837. return 0;
  14838. }
  14839. /* return the client ip_addr converted for output */
  14840. if (sd && sd->fd && session[sd->fd])
  14841. {
  14842. /* initiliaze */
  14843. const char *ip_addr = NULL;
  14844. uint32 ip;
  14845. /* set ip, ip_addr and convert to ip and push str */
  14846. ip = session[sd->fd]->client_addr;
  14847. ip_addr = ip2str(ip, NULL);
  14848. script_pushstrcopy(st, ip_addr);
  14849. }
  14850. return 0;
  14851. }
  14852. /**
  14853. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  14854. **/
  14855. BUILDIN_FUNC(is_function) {
  14856. const char* str = script_getstr(st,2);
  14857. if( strdb_exists(userfunc_db, str) )
  14858. script_pushint(st,1);
  14859. else
  14860. script_pushint(st,0);
  14861. return 0;
  14862. }
  14863. /**
  14864. * get_revision() -> retrieves the current svn revision (if available)
  14865. **/
  14866. BUILDIN_FUNC(get_revision) {
  14867. const char * revision;
  14868. if ( (revision = get_svn_revision()) != 0 )
  14869. script_pushint(st,atoi(revision));
  14870. else
  14871. script_pushint(st,-1);//unknown
  14872. return 0;
  14873. }
  14874. /**
  14875. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  14876. **/
  14877. BUILDIN_FUNC(freeloop) {
  14878. if( script_getnum(st,2) )
  14879. st->freeloop = 1;
  14880. else
  14881. st->freeloop = 0;
  14882. script_pushint(st, st->freeloop);
  14883. return 0;
  14884. }
  14885. /**
  14886. * @commands (script based)
  14887. **/
  14888. BUILDIN_FUNC(bindatcmd) {
  14889. const char* atcmd;
  14890. const char* eventName;
  14891. int i, level = 0, level2 = 0;
  14892. bool create = false;
  14893. atcmd = script_getstr(st,2);
  14894. eventName = script_getstr(st,3);
  14895. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  14896. atcmd++;
  14897. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  14898. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  14899. if( atcmd_binding_count == 0 ) {
  14900. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  14901. create = true;
  14902. } else {
  14903. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  14904. if( i < atcmd_binding_count ) {/* update existent entry */
  14905. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  14906. atcmd_binding[i]->level = level;
  14907. atcmd_binding[i]->level2 = level2;
  14908. } else
  14909. create = true;
  14910. }
  14911. if( create ) {
  14912. i = atcmd_binding_count;
  14913. if( atcmd_binding_count++ != 0 )
  14914. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  14915. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  14916. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  14917. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  14918. atcmd_binding[i]->level = level;
  14919. atcmd_binding[i]->level2 = level2;
  14920. }
  14921. return 0;
  14922. }
  14923. BUILDIN_FUNC(unbindatcmd) {
  14924. const char* atcmd;
  14925. int i = 0;
  14926. atcmd = script_getstr(st, 2);
  14927. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  14928. atcmd++;
  14929. if( atcmd_binding_count == 0 ) {
  14930. script_pushint(st, 0);
  14931. return 0;
  14932. }
  14933. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  14934. if( i < atcmd_binding_count ) {
  14935. int cursor = 0;
  14936. aFree(atcmd_binding[i]);
  14937. atcmd_binding[i] = NULL;
  14938. /* compact the list now that we freed a slot somewhere */
  14939. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  14940. if( atcmd_binding[i] == NULL )
  14941. continue;
  14942. if( cursor != i ) {
  14943. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  14944. }
  14945. cursor++;
  14946. }
  14947. if( (atcmd_binding_count = cursor) == 0 )
  14948. aFree(atcmd_binding);
  14949. script_pushint(st, 1);
  14950. } else
  14951. script_pushint(st, 0);/* not found */
  14952. return 0;
  14953. }
  14954. BUILDIN_FUNC(useatcmd)
  14955. {
  14956. TBL_PC dummy_sd;
  14957. TBL_PC* sd;
  14958. int fd;
  14959. const char* cmd;
  14960. cmd = script_getstr(st,2);
  14961. if( st->rid )
  14962. {
  14963. sd = script_rid2sd(st);
  14964. fd = sd->fd;
  14965. }
  14966. else
  14967. { // Use a dummy character.
  14968. sd = &dummy_sd;
  14969. fd = 0;
  14970. memset(&dummy_sd, 0, sizeof(TBL_PC));
  14971. if( st->oid )
  14972. {
  14973. struct block_list* bl = map_id2bl(st->oid);
  14974. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  14975. if( bl->type == BL_NPC )
  14976. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  14977. }
  14978. }
  14979. // compatibility with previous implementation (deprecated!)
  14980. if( cmd[0] != atcommand_symbol )
  14981. {
  14982. cmd += strlen(sd->status.name);
  14983. while( *cmd != atcommand_symbol && *cmd != 0 )
  14984. cmd++;
  14985. }
  14986. is_atcommand(fd, sd, cmd, 1);
  14987. return 0;
  14988. }
  14989. BUILDIN_FUNC(checkre)
  14990. {
  14991. int num;
  14992. num=script_getnum(st,2);
  14993. switch(num){
  14994. case 0:
  14995. #ifdef RENEWAL
  14996. script_pushint(st, 1);
  14997. #else
  14998. script_pushint(st, 0);
  14999. #endif
  15000. break;
  15001. case 1:
  15002. #ifdef RENEWAL_CAST
  15003. script_pushint(st, 1);
  15004. #else
  15005. script_pushint(st, 0);
  15006. #endif
  15007. break;
  15008. case 2:
  15009. #ifdef RENEWAL_DROP
  15010. script_pushint(st, 1);
  15011. #else
  15012. script_pushint(st, 0);
  15013. #endif
  15014. break;
  15015. case 3:
  15016. #ifdef RENEWAL_EXP
  15017. script_pushint(st, 1);
  15018. #else
  15019. script_pushint(st, 0);
  15020. #endif
  15021. break;
  15022. case 4:
  15023. #ifdef RENEWAL_LVDMG
  15024. script_pushint(st, 1);
  15025. #else
  15026. script_pushint(st, 0);
  15027. #endif
  15028. break;
  15029. case 5:
  15030. #ifdef RENEWAL_EDP
  15031. script_pushint(st, 1);
  15032. #else
  15033. script_pushint(st, 0);
  15034. #endif
  15035. break;
  15036. case 6:
  15037. #ifdef RENEWAL_ASPD
  15038. script_pushint(st, 1);
  15039. #else
  15040. script_pushint(st, 0);
  15041. #endif
  15042. break;
  15043. default:
  15044. ShowWarning("buildin_checkre: unknown parameter.\n");
  15045. break;
  15046. }
  15047. return 0;
  15048. }
  15049. /* getrandgroupitem <group_id>,<quantity> */
  15050. BUILDIN_FUNC(getrandgroupitem) {
  15051. TBL_PC* sd;
  15052. int i, get_count = 0, flag, nameid, group = script_getnum(st, 2), qty = script_getnum(st,3);
  15053. struct item item_tmp;
  15054. if( !( sd = script_rid2sd(st) ) )
  15055. return 0;
  15056. if( qty <= 0 ) {
  15057. ShowError("getrandgroupitem: qty is <= 0!\n");
  15058. return 1;
  15059. }
  15060. if( (nameid = itemdb_searchrandomid(group)) == UNKNOWN_ITEM_ID ) {
  15061. return 1; //ensure valid itemid
  15062. }
  15063. memset(&item_tmp,0,sizeof(item_tmp));
  15064. item_tmp.nameid = nameid;
  15065. item_tmp.identify = itemdb_isidentified(nameid);
  15066. //Check if it's stackable.
  15067. if (!itemdb_isstackable(nameid))
  15068. get_count = 1;
  15069. else
  15070. get_count = qty;
  15071. for (i = 0; i < qty; i += get_count) {
  15072. // if not pet egg
  15073. if (!pet_create_egg(sd, nameid)) {
  15074. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT))) {
  15075. clif_additem(sd, 0, 0, flag);
  15076. if( pc_candrop(sd,&item_tmp) )
  15077. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  15078. }
  15079. }
  15080. }
  15081. return 0;
  15082. }
  15083. /* cleanmap <map_name>;
  15084. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  15085. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  15086. {
  15087. nullpo_ret(bl);
  15088. map_clearflooritem(bl);
  15089. return 0;
  15090. }
  15091. BUILDIN_FUNC(cleanmap)
  15092. {
  15093. const char *map;
  15094. int16 m = -1;
  15095. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  15096. map = script_getstr(st, 2);
  15097. m = map_mapname2mapid(map);
  15098. if (!m)
  15099. return 1;
  15100. if ((script_lastdata(st) - 2) < 4) {
  15101. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  15102. } else {
  15103. x0 = script_getnum(st, 3);
  15104. y0 = script_getnum(st, 4);
  15105. x1 = script_getnum(st, 5);
  15106. y1 = script_getnum(st, 6);
  15107. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  15108. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  15109. } else {
  15110. ShowError("cleanarea: invalid coordinate defined!\n");
  15111. return 1;
  15112. }
  15113. }
  15114. return 0;
  15115. }
  15116. /* Cast a skill on the attached player.
  15117. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  15118. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  15119. BUILDIN_FUNC(npcskill)
  15120. {
  15121. uint16 skill_id;
  15122. unsigned short skill_level;
  15123. unsigned int stat_point;
  15124. unsigned int npc_level;
  15125. struct npc_data *nd;
  15126. struct map_session_data *sd;
  15127. skill_id = script_isstring(st, 2) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  15128. skill_level = script_getnum(st, 3);
  15129. stat_point = script_getnum(st, 4);
  15130. npc_level = script_getnum(st, 5);
  15131. sd = script_rid2sd(st);
  15132. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  15133. if (stat_point > battle_config.max_third_parameter) {
  15134. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  15135. return 1;
  15136. }
  15137. if (npc_level > MAX_LEVEL) {
  15138. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  15139. return 1;
  15140. }
  15141. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  15142. return 1;
  15143. }
  15144. nd->level = npc_level;
  15145. nd->stat_point = stat_point;
  15146. if (!nd->status.hp) {
  15147. status_calc_npc(nd, true);
  15148. } else {
  15149. status_calc_npc(nd, false);
  15150. }
  15151. if (skill_get_inf(skill_id)&INF_GROUND_SKILL) {
  15152. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  15153. } else {
  15154. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  15155. }
  15156. return 0;
  15157. }
  15158. /* Consumes an item.
  15159. * consumeitem <item id>;
  15160. * consumeitem "<item name>"; */
  15161. BUILDIN_FUNC(consumeitem)
  15162. {
  15163. TBL_NPC *nd;
  15164. TBL_PC *sd;
  15165. struct script_data *data;
  15166. struct item_data *item_data;
  15167. nullpo_retr( 1, ( sd = script_rid2sd( st ) ) );
  15168. nullpo_retr( 1, ( nd = (TBL_NPC *)map_id2bl( sd->npc_id ) ) );
  15169. data = script_getdata( st, 2 );
  15170. get_val( st, data );
  15171. if( data_isstring( data ) ){
  15172. const char *name = conv_str( st, data );
  15173. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  15174. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  15175. return 1;
  15176. }
  15177. }else if( data_isint( data ) ){
  15178. int nameid = conv_num( st, data );
  15179. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  15180. ShowError("buildin_consumeitem: Nonexistant item %d requested.\n", nameid );
  15181. return 1;
  15182. }
  15183. }else{
  15184. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).", data->type );
  15185. return 1;
  15186. }
  15187. run_script( item_data->script, 0, sd->bl.id, nd->bl.id );
  15188. return 0;
  15189. }
  15190. /* Make a player sit/stand.
  15191. * sit {"<character name>"};
  15192. * stand {"<character name>"};
  15193. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing). */
  15194. BUILDIN_FUNC(sit)
  15195. {
  15196. TBL_PC *sd;
  15197. if( script_hasdata(st, 2) )
  15198. sd = map_nick2sd(script_getstr(st, 2));
  15199. else
  15200. sd = script_rid2sd(st);
  15201. if( sd == NULL)
  15202. return 0;
  15203. if( pc_issit(sd) ) {
  15204. pc_setstand(sd);
  15205. skill_sit(sd, 0);
  15206. clif_standing(&sd->bl);
  15207. } else {
  15208. unit_stop_walking(&sd->bl, 1|4);
  15209. pc_setsit(sd);
  15210. skill_sit(sd, 1);
  15211. clif_sitting(&sd->bl);
  15212. }
  15213. return 0;
  15214. }
  15215. // declarations that were supposed to be exported from npc_chat.c
  15216. #ifdef PCRE_SUPPORT
  15217. BUILDIN_FUNC(defpattern);
  15218. BUILDIN_FUNC(activatepset);
  15219. BUILDIN_FUNC(deactivatepset);
  15220. BUILDIN_FUNC(deletepset);
  15221. #endif
  15222. /// script command definitions
  15223. /// for an explanation on args, see add_buildin_func
  15224. struct script_function buildin_func[] = {
  15225. // NPC interaction
  15226. BUILDIN_DEF(mes,"s*"),
  15227. BUILDIN_DEF(next,""),
  15228. BUILDIN_DEF(close,""),
  15229. BUILDIN_DEF(close2,""),
  15230. BUILDIN_DEF(menu,"sl*"),
  15231. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  15232. BUILDIN_DEF(prompt,"s*"),
  15233. //
  15234. BUILDIN_DEF(goto,"l"),
  15235. BUILDIN_DEF(callsub,"l*"),
  15236. BUILDIN_DEF(callfunc,"s*"),
  15237. BUILDIN_DEF(return,"?"),
  15238. BUILDIN_DEF(getarg,"i?"),
  15239. BUILDIN_DEF(jobchange,"i?"),
  15240. BUILDIN_DEF(jobname,"i"),
  15241. BUILDIN_DEF(input,"r??"),
  15242. BUILDIN_DEF(warp,"sii"),
  15243. BUILDIN_DEF(areawarp,"siiiisii??"),
  15244. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  15245. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  15246. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  15247. BUILDIN_DEF(setlook,"ii"),
  15248. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  15249. BUILDIN_DEF(set,"rv"),
  15250. BUILDIN_DEF(setarray,"rv*"),
  15251. BUILDIN_DEF(cleararray,"rvi"),
  15252. BUILDIN_DEF(copyarray,"rri"),
  15253. BUILDIN_DEF(getarraysize,"r"),
  15254. BUILDIN_DEF(deletearray,"r?"),
  15255. BUILDIN_DEF(getelementofarray,"ri"),
  15256. BUILDIN_DEF(getitem,"vi?"),
  15257. BUILDIN_DEF(rentitem,"vi"),
  15258. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  15259. BUILDIN_DEF(getnameditem,"vv"),
  15260. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  15261. BUILDIN_DEF(makeitem,"visii"),
  15262. BUILDIN_DEF(delitem,"vi?"),
  15263. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  15264. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  15265. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  15266. BUILDIN_DEF(cutin,"si"),
  15267. BUILDIN_DEF(viewpoint,"iiiii"),
  15268. BUILDIN_DEF(heal,"ii"),
  15269. BUILDIN_DEF(itemheal,"ii"),
  15270. BUILDIN_DEF(percentheal,"ii"),
  15271. BUILDIN_DEF(rand,"i?"),
  15272. BUILDIN_DEF(countitem,"v"),
  15273. BUILDIN_DEF(countitem2,"viiiiiii"),
  15274. BUILDIN_DEF(checkweight,"vi*"),
  15275. BUILDIN_DEF(checkweight2,"rr"),
  15276. BUILDIN_DEF(readparam,"i?"),
  15277. BUILDIN_DEF(getcharid,"i?"),
  15278. BUILDIN_DEF(getnpcid,"i?"),
  15279. BUILDIN_DEF(getpartyname,"i"),
  15280. BUILDIN_DEF(getpartymember,"i?"),
  15281. BUILDIN_DEF(getpartyleader,"i?"),
  15282. BUILDIN_DEF(getguildname,"i"),
  15283. BUILDIN_DEF(getguildmaster,"i"),
  15284. BUILDIN_DEF(getguildmasterid,"i"),
  15285. BUILDIN_DEF(strcharinfo,"i"),
  15286. BUILDIN_DEF(strnpcinfo,"i"),
  15287. BUILDIN_DEF(getequipid,"i"),
  15288. BUILDIN_DEF(getequipname,"i"),
  15289. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  15290. BUILDIN_DEF(repair,"i"), // [Valaris]
  15291. BUILDIN_DEF(repairall,""),
  15292. BUILDIN_DEF(getequipisequiped,"i"),
  15293. BUILDIN_DEF(getequipisenableref,"i"),
  15294. BUILDIN_DEF(getequipisidentify,"i"),
  15295. BUILDIN_DEF(getequiprefinerycnt,"i"),
  15296. BUILDIN_DEF(getequipweaponlv,"i"),
  15297. BUILDIN_DEF(getequippercentrefinery,"i"),
  15298. BUILDIN_DEF(successrefitem,"i"),
  15299. BUILDIN_DEF(failedrefitem,"i"),
  15300. BUILDIN_DEF(downrefitem,"i"),
  15301. BUILDIN_DEF(statusup,"i"),
  15302. BUILDIN_DEF(statusup2,"ii"),
  15303. BUILDIN_DEF(bonus,"iv"),
  15304. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  15305. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  15306. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  15307. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  15308. BUILDIN_DEF(autobonus,"sii??"),
  15309. BUILDIN_DEF(autobonus2,"sii??"),
  15310. BUILDIN_DEF(autobonus3,"siiv?"),
  15311. BUILDIN_DEF(skill,"vi?"),
  15312. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  15313. BUILDIN_DEF(guildskill,"vi"),
  15314. BUILDIN_DEF(getskilllv,"v"),
  15315. BUILDIN_DEF(getgdskilllv,"iv"),
  15316. BUILDIN_DEF(basicskillcheck,""),
  15317. BUILDIN_DEF(getgmlevel,""),
  15318. BUILDIN_DEF(getgroupid,""),
  15319. BUILDIN_DEF(end,""),
  15320. BUILDIN_DEF(checkoption,"i"),
  15321. BUILDIN_DEF(setoption,"i?"),
  15322. BUILDIN_DEF(setcart,"?"),
  15323. BUILDIN_DEF(checkcart,""),
  15324. BUILDIN_DEF(setfalcon,"?"),
  15325. BUILDIN_DEF(checkfalcon,""),
  15326. BUILDIN_DEF(setriding,"?"),
  15327. BUILDIN_DEF(checkriding,""),
  15328. BUILDIN_DEF(checkwug,""),
  15329. BUILDIN_DEF(checkmadogear,""),
  15330. BUILDIN_DEF(setmadogear,"?"),
  15331. BUILDIN_DEF2(savepoint,"save","sii"),
  15332. BUILDIN_DEF(savepoint,"sii"),
  15333. BUILDIN_DEF(gettimetick,"i"),
  15334. BUILDIN_DEF(gettime,"i"),
  15335. BUILDIN_DEF(gettimestr,"si"),
  15336. BUILDIN_DEF(openstorage,""),
  15337. BUILDIN_DEF(guildopenstorage,""),
  15338. BUILDIN_DEF(itemskill,"vi"),
  15339. BUILDIN_DEF(produce,"i"),
  15340. BUILDIN_DEF(cooking,"i"),
  15341. BUILDIN_DEF(monster,"siisii???"),
  15342. BUILDIN_DEF(getmobdrops,"i"),
  15343. BUILDIN_DEF(areamonster,"siiiisii???"),
  15344. BUILDIN_DEF(killmonster,"ss?"),
  15345. BUILDIN_DEF(killmonsterall,"s?"),
  15346. BUILDIN_DEF(clone,"siisi????"),
  15347. BUILDIN_DEF(doevent,"s"),
  15348. BUILDIN_DEF(donpcevent,"s"),
  15349. BUILDIN_DEF(cmdothernpc,"ss"),
  15350. BUILDIN_DEF(addtimer,"is"),
  15351. BUILDIN_DEF(deltimer,"s"),
  15352. BUILDIN_DEF(addtimercount,"si"),
  15353. BUILDIN_DEF(initnpctimer,"??"),
  15354. BUILDIN_DEF(stopnpctimer,"??"),
  15355. BUILDIN_DEF(startnpctimer,"??"),
  15356. BUILDIN_DEF(setnpctimer,"i?"),
  15357. BUILDIN_DEF(getnpctimer,"i?"),
  15358. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  15359. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  15360. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  15361. BUILDIN_DEF(announce,"si?????"),
  15362. BUILDIN_DEF(mapannounce,"ssi?????"),
  15363. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  15364. BUILDIN_DEF(getusers,"i"),
  15365. BUILDIN_DEF(getmapguildusers,"si"),
  15366. BUILDIN_DEF(getmapusers,"s"),
  15367. BUILDIN_DEF(getareausers,"siiii"),
  15368. BUILDIN_DEF(getareadropitem,"siiiiv"),
  15369. BUILDIN_DEF(enablenpc,"s"),
  15370. BUILDIN_DEF(disablenpc,"s"),
  15371. BUILDIN_DEF(hideoffnpc,"s"),
  15372. BUILDIN_DEF(hideonnpc,"s"),
  15373. BUILDIN_DEF(sc_start,"iii?"),
  15374. BUILDIN_DEF(sc_start2,"iiii?"),
  15375. BUILDIN_DEF(sc_start4,"iiiiii?"),
  15376. BUILDIN_DEF(sc_end,"i?"),
  15377. BUILDIN_DEF(getstatus, "i?"),
  15378. BUILDIN_DEF(getscrate,"ii?"),
  15379. BUILDIN_DEF(debugmes,"s"),
  15380. BUILDIN_DEF2(catchpet,"pet","i"),
  15381. BUILDIN_DEF2(birthpet,"bpet",""),
  15382. BUILDIN_DEF(resetlvl,"i"),
  15383. BUILDIN_DEF(resetstatus,""),
  15384. BUILDIN_DEF(resetskill,""),
  15385. BUILDIN_DEF(skillpointcount,""),
  15386. BUILDIN_DEF(changebase,"i?"),
  15387. BUILDIN_DEF(changesex,""),
  15388. BUILDIN_DEF(waitingroom,"si?????"),
  15389. BUILDIN_DEF(delwaitingroom,"?"),
  15390. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  15391. BUILDIN_DEF(enablewaitingroomevent,"?"),
  15392. BUILDIN_DEF(disablewaitingroomevent,"?"),
  15393. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  15394. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  15395. BUILDIN_DEF(getwaitingroomstate,"i?"),
  15396. BUILDIN_DEF(warpwaitingpc,"sii?"),
  15397. BUILDIN_DEF(attachrid,"i"),
  15398. BUILDIN_DEF(detachrid,""),
  15399. BUILDIN_DEF(isloggedin,"i?"),
  15400. BUILDIN_DEF(setmapflagnosave,"ssii"),
  15401. BUILDIN_DEF(getmapflag,"si"),
  15402. BUILDIN_DEF(setmapflag,"si?"),
  15403. BUILDIN_DEF(removemapflag,"si?"),
  15404. BUILDIN_DEF(pvpon,"s"),
  15405. BUILDIN_DEF(pvpoff,"s"),
  15406. BUILDIN_DEF(gvgon,"s"),
  15407. BUILDIN_DEF(gvgoff,"s"),
  15408. BUILDIN_DEF(emotion,"i??"),
  15409. BUILDIN_DEF(maprespawnguildid,"sii"),
  15410. BUILDIN_DEF(agitstart,""), // <Agit>
  15411. BUILDIN_DEF(agitend,""),
  15412. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  15413. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  15414. BUILDIN_DEF(getcastlename,"s"),
  15415. BUILDIN_DEF(getcastledata,"si"),
  15416. BUILDIN_DEF(setcastledata,"sii"),
  15417. BUILDIN_DEF(requestguildinfo,"i?"),
  15418. BUILDIN_DEF(getequipcardcnt,"i"),
  15419. BUILDIN_DEF(successremovecards,"i"),
  15420. BUILDIN_DEF(failedremovecards,"ii"),
  15421. BUILDIN_DEF(marriage,"s"),
  15422. BUILDIN_DEF2(wedding_effect,"wedding",""),
  15423. BUILDIN_DEF(divorce,""),
  15424. BUILDIN_DEF(ispartneron,""),
  15425. BUILDIN_DEF(getpartnerid,""),
  15426. BUILDIN_DEF(getchildid,""),
  15427. BUILDIN_DEF(getmotherid,""),
  15428. BUILDIN_DEF(getfatherid,""),
  15429. BUILDIN_DEF(warppartner,"sii"),
  15430. BUILDIN_DEF(getitemname,"v"),
  15431. BUILDIN_DEF(getitemslots,"i"),
  15432. BUILDIN_DEF(makepet,"i"),
  15433. BUILDIN_DEF(getexp,"ii"),
  15434. BUILDIN_DEF(getinventorylist,""),
  15435. BUILDIN_DEF(getskilllist,""),
  15436. BUILDIN_DEF(clearitem,""),
  15437. BUILDIN_DEF(classchange,"ii"),
  15438. BUILDIN_DEF(misceffect,"i"),
  15439. BUILDIN_DEF(playBGM,"s"),
  15440. BUILDIN_DEF(playBGMall,"s?????"),
  15441. BUILDIN_DEF(soundeffect,"si"),
  15442. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  15443. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  15444. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  15445. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  15446. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  15447. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  15448. BUILDIN_DEF(petloot,"i"), // [Valaris]
  15449. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  15450. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  15451. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  15452. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  15453. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  15454. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  15455. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  15456. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  15457. BUILDIN_DEF(nude,""), // nude command [Valaris]
  15458. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  15459. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  15460. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  15461. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  15462. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  15463. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  15464. BUILDIN_DEF(mobcount,"ss"),
  15465. BUILDIN_DEF(getlook,"i"),
  15466. BUILDIN_DEF(getsavepoint,"i"),
  15467. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  15468. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  15469. BUILDIN_DEF(npcstop,""), // [Valaris]
  15470. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  15471. BUILDIN_DEF(checkoption1,"i"),
  15472. BUILDIN_DEF(checkoption2,"i"),
  15473. BUILDIN_DEF(guildgetexp,"i"),
  15474. BUILDIN_DEF(guildchangegm,"is"),
  15475. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  15476. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  15477. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  15478. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  15479. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  15480. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  15481. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  15482. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  15483. BUILDIN_DEF(night,""), // sets the server to night time
  15484. BUILDIN_DEF(day,""), // sets the server to day time
  15485. #ifdef PCRE_SUPPORT
  15486. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  15487. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  15488. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  15489. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  15490. #endif
  15491. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  15492. BUILDIN_DEF(getusersname,""),
  15493. BUILDIN_DEF(recovery,""),
  15494. BUILDIN_DEF(getpetinfo,"i"),
  15495. BUILDIN_DEF(gethominfo,"i"),
  15496. BUILDIN_DEF(getmercinfo,"i?"),
  15497. BUILDIN_DEF(checkequipedcard,"i"),
  15498. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  15499. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  15500. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  15501. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  15502. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  15503. BUILDIN_DEF(charat,"si"),
  15504. BUILDIN_DEF(setchar,"ssi"),
  15505. BUILDIN_DEF(insertchar,"ssi"),
  15506. BUILDIN_DEF(delchar,"si"),
  15507. BUILDIN_DEF(strtoupper,"s"),
  15508. BUILDIN_DEF(strtolower,"s"),
  15509. BUILDIN_DEF(charisupper, "si"),
  15510. BUILDIN_DEF(charislower, "si"),
  15511. BUILDIN_DEF(substr,"sii"),
  15512. BUILDIN_DEF(explode, "rss"),
  15513. BUILDIN_DEF(implode, "r?"),
  15514. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  15515. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  15516. BUILDIN_DEF(strpos,"ss?"),
  15517. BUILDIN_DEF(replacestr,"sss??"),
  15518. BUILDIN_DEF(countstr,"ss?"),
  15519. BUILDIN_DEF(setnpcdisplay,"sv??"),
  15520. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  15521. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  15522. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  15523. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  15524. // [zBuffer] List of mathematics commands --->
  15525. BUILDIN_DEF(sqrt,"i"),
  15526. BUILDIN_DEF(pow,"ii"),
  15527. BUILDIN_DEF(distance,"iiii"),
  15528. // <--- [zBuffer] List of mathematics commands
  15529. BUILDIN_DEF(md5,"s"),
  15530. // [zBuffer] List of dynamic var commands --->
  15531. BUILDIN_DEF(getd,"s"),
  15532. BUILDIN_DEF(setd,"sv"),
  15533. // <--- [zBuffer] List of dynamic var commands
  15534. BUILDIN_DEF(petstat,"i"),
  15535. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  15536. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  15537. BUILDIN_DEF(npcshopadditem,"sii*"),
  15538. BUILDIN_DEF(npcshopdelitem,"si*"),
  15539. BUILDIN_DEF(npcshopattach,"s?"),
  15540. BUILDIN_DEF(equip,"i"),
  15541. BUILDIN_DEF(autoequip,"ii"),
  15542. BUILDIN_DEF(setbattleflag,"si"),
  15543. BUILDIN_DEF(getbattleflag,"s"),
  15544. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  15545. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  15546. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  15547. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  15548. BUILDIN_DEF(axtoi,"s"),
  15549. BUILDIN_DEF(query_sql,"s*"),
  15550. BUILDIN_DEF(query_logsql,"s*"),
  15551. BUILDIN_DEF(escape_sql,"v"),
  15552. BUILDIN_DEF(atoi,"s"),
  15553. // [zBuffer] List of player cont commands --->
  15554. BUILDIN_DEF(rid2name,"i"),
  15555. BUILDIN_DEF(pcfollow,"ii"),
  15556. BUILDIN_DEF(pcstopfollow,"i"),
  15557. BUILDIN_DEF(pcblockmove,"ii"),
  15558. // <--- [zBuffer] List of player cont commands
  15559. // [zBuffer] List of mob control commands --->
  15560. BUILDIN_DEF(unitwalk,"ii?"),
  15561. BUILDIN_DEF(unitkill,"i"),
  15562. BUILDIN_DEF(unitwarp,"isii"),
  15563. BUILDIN_DEF(unitattack,"iv?"),
  15564. BUILDIN_DEF(unitstop,"i"),
  15565. BUILDIN_DEF(unittalk,"is"),
  15566. BUILDIN_DEF(unitemote,"ii"),
  15567. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  15568. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  15569. // <--- [zBuffer] List of mob control commands
  15570. BUILDIN_DEF(sleep,"i"),
  15571. BUILDIN_DEF(sleep2,"i"),
  15572. BUILDIN_DEF(awake,"s"),
  15573. BUILDIN_DEF(getvariableofnpc,"rs"),
  15574. BUILDIN_DEF(warpportal,"iisii"),
  15575. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  15576. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  15577. BUILDIN_DEF(morphembryo,""),
  15578. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  15579. BUILDIN_DEF(checkhomcall,""),
  15580. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  15581. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  15582. BUILDIN_DEF(checkvending,"?"),
  15583. BUILDIN_DEF(checkchatting,"?"),
  15584. BUILDIN_DEF(checkidle,"?"),
  15585. BUILDIN_DEF(openmail,""),
  15586. BUILDIN_DEF(openauction,""),
  15587. BUILDIN_DEF(checkcell,"siii"),
  15588. BUILDIN_DEF(setcell,"siiiiii"),
  15589. BUILDIN_DEF(setwall,"siiiiis"),
  15590. BUILDIN_DEF(delwall,"s"),
  15591. BUILDIN_DEF(searchitem,"rs"),
  15592. BUILDIN_DEF(mercenary_create,"ii"),
  15593. BUILDIN_DEF(mercenary_heal,"ii"),
  15594. BUILDIN_DEF(mercenary_sc_start,"iii"),
  15595. BUILDIN_DEF(mercenary_get_calls,"i"),
  15596. BUILDIN_DEF(mercenary_get_faith,"i"),
  15597. BUILDIN_DEF(mercenary_set_calls,"ii"),
  15598. BUILDIN_DEF(mercenary_set_faith,"ii"),
  15599. BUILDIN_DEF(readbook,"ii"),
  15600. BUILDIN_DEF(setfont,"i"),
  15601. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  15602. BUILDIN_DEF(progressbar,"si"),
  15603. BUILDIN_DEF(pushpc,"ii"),
  15604. BUILDIN_DEF(buyingstore,"i"),
  15605. BUILDIN_DEF(searchstores,"ii"),
  15606. BUILDIN_DEF(showdigit,"i?"),
  15607. // WoE SE
  15608. BUILDIN_DEF(agitstart2,""),
  15609. BUILDIN_DEF(agitend2,""),
  15610. BUILDIN_DEF(agitcheck2,""),
  15611. // BattleGround
  15612. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  15613. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  15614. BUILDIN_DEF(bg_team_setxy,"iii"),
  15615. BUILDIN_DEF(bg_warp,"isii"),
  15616. BUILDIN_DEF(bg_monster,"isiisi?"),
  15617. BUILDIN_DEF(bg_monster_set_team,"ii"),
  15618. BUILDIN_DEF(bg_leave,""),
  15619. BUILDIN_DEF(bg_destroy,"i"),
  15620. BUILDIN_DEF(areapercentheal,"siiiiii"),
  15621. BUILDIN_DEF(bg_get_data,"ii"),
  15622. BUILDIN_DEF(bg_getareausers,"isiiii"),
  15623. BUILDIN_DEF(bg_updatescore,"sii"),
  15624. // Instancing
  15625. BUILDIN_DEF(instance_create,"si"),
  15626. BUILDIN_DEF(instance_destroy,"?"),
  15627. BUILDIN_DEF(instance_attachmap,"si?"),
  15628. BUILDIN_DEF(instance_detachmap,"s?"),
  15629. BUILDIN_DEF(instance_attach,"i"),
  15630. BUILDIN_DEF(instance_id,"?"),
  15631. BUILDIN_DEF(instance_set_timeout,"ii?"),
  15632. BUILDIN_DEF(instance_init,"i"),
  15633. BUILDIN_DEF(instance_announce,"isi?????"),
  15634. BUILDIN_DEF(instance_npcname,"s?"),
  15635. BUILDIN_DEF(has_instance,"s?"),
  15636. BUILDIN_DEF(instance_warpall,"sii?"),
  15637. BUILDIN_DEF(instance_check_party,"i???"),
  15638. /**
  15639. * 3rd-related
  15640. **/
  15641. BUILDIN_DEF(makerune,"i"),
  15642. BUILDIN_DEF(checkdragon,""),//[Ind]
  15643. BUILDIN_DEF(setdragon,"?"),//[Ind]
  15644. BUILDIN_DEF(ismounting,""),//[Ind]
  15645. BUILDIN_DEF(setmounting,""),//[Ind]
  15646. BUILDIN_DEF(checkre,"i"),
  15647. /**
  15648. * rAthena and beyond!
  15649. **/
  15650. BUILDIN_DEF(getargcount,""),
  15651. BUILDIN_DEF(getcharip,"?"),
  15652. BUILDIN_DEF(is_function,"s"),
  15653. BUILDIN_DEF(get_revision,""),
  15654. BUILDIN_DEF(freeloop,"i"),
  15655. BUILDIN_DEF(getrandgroupitem,"ii"),
  15656. BUILDIN_DEF(cleanmap,"s"),
  15657. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  15658. BUILDIN_DEF(npcskill,"viii"),
  15659. BUILDIN_DEF(consumeitem,"v"),
  15660. BUILDIN_DEF(delequip,"i"),
  15661. BUILDIN_DEF(sit,"?"),
  15662. BUILDIN_DEF2(sit,"stand","?"),
  15663. /**
  15664. * @commands (script based)
  15665. **/
  15666. BUILDIN_DEF(bindatcmd, "ss??"),
  15667. BUILDIN_DEF(unbindatcmd, "s"),
  15668. BUILDIN_DEF(useatcmd, "s"),
  15669. //Quest Log System [Inkfish]
  15670. BUILDIN_DEF(setquest, "i"),
  15671. BUILDIN_DEF(erasequest, "i"),
  15672. BUILDIN_DEF(completequest, "i"),
  15673. BUILDIN_DEF(checkquest, "i?"),
  15674. BUILDIN_DEF(changequest, "ii"),
  15675. BUILDIN_DEF(showevent, "ii"),
  15676. {NULL,NULL,NULL},
  15677. };