script.c 523 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333
  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 "storage.h"
  25. #include "pet.h"
  26. #include "mapreg.h"
  27. #include "homunculus.h"
  28. #include "instance.h"
  29. #include "mercenary.h"
  30. #include "intif.h"
  31. #include "chat.h"
  32. #include "battleground.h"
  33. #include "party.h"
  34. #include "mail.h"
  35. #include "script.h"
  36. #include "quest.h"
  37. #include "elemental.h"
  38. #ifdef PCRE_SUPPORT
  39. #include "../../3rdparty/pcre/include/pcre.h" // preg_match
  40. #endif
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <math.h>
  45. #ifndef WIN32
  46. #endif
  47. #include <setjmp.h>
  48. #include <errno.h>
  49. #ifdef BETA_THREAD_TEST
  50. #include "../common/atomic.h"
  51. #include "../common/spinlock.h"
  52. #include "../common/thread.h"
  53. #include "../common/mutex.h"
  54. #endif
  55. ///////////////////////////////////////////////////////////////////////////////
  56. //## TODO possible enhancements: [FlavioJS]
  57. // - 'callfunc' supporting labels in the current npc "::LabelName"
  58. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  59. // - 'function FuncName;' function declarations reverting to global functions
  60. // if local label isn't found
  61. // - join callfunc and callsub's functionality
  62. // - remove dynamic allocation in add_word()
  63. // - remove GETVALUE / SETVALUE
  64. // - clean up the set_reg / set_val / setd_sub mess
  65. // - detect invalid label references at parse-time
  66. //
  67. // struct script_state* st;
  68. //
  69. /// Returns the script_data at the target index
  70. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  71. /// Returns if the stack contains data at the target index
  72. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  73. /// Returns the index of the last data in the stack
  74. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  75. /// Pushes an int into the stack
  76. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  77. /// Pushes a string into the stack (script engine frees it automatically)
  78. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  79. /// Pushes a copy of a string into the stack
  80. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  81. /// Pushes a constant string into the stack (must never change or be freed)
  82. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  83. /// Pushes a nil into the stack
  84. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  85. /// Pushes a copy of the data in the target index
  86. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  87. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  88. #define script_isint(st,i) data_isint(script_getdata(st,i))
  89. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  90. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  91. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  92. // Returns name of currently running function
  93. #define script_getfuncname(st) ( st->funcname )
  94. // Note: "top" functions/defines use indexes relative to the top of the stack
  95. // -1 is the index of the data at the top
  96. /// Returns the script_data at the target index relative to the top of the stack
  97. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  98. /// Pushes a copy of the data in the target index relative to the top of the stack
  99. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  100. /// Removes the range of values [start,end[ relative to the top of the stack
  101. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  102. //
  103. // struct script_data* data;
  104. //
  105. /// Returns if the script data is a string
  106. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  107. /// Returns if the script data is an int
  108. #define data_isint(data) ( (data)->type == C_INT )
  109. /// Returns if the script data is a reference
  110. #define data_isreference(data) ( (data)->type == C_NAME )
  111. /// Returns if the script data is a label
  112. #define data_islabel(data) ( (data)->type == C_POS )
  113. /// Returns if the script data is an internal script function label
  114. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  115. /// Returns if this is a reference to a constant
  116. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  117. /// Returns if this a reference to a param
  118. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  119. /// Returns if this a reference to a variable
  120. //##TODO confirm it's C_NAME [FlavioJS]
  121. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  122. /// Returns the unique id of the reference (id and index)
  123. #define reference_getuid(data) ( (data)->u.num )
  124. /// Returns the id of the reference
  125. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  126. /// Returns the array index of the reference
  127. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  128. /// Returns the name of the reference
  129. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  130. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  131. #define reference_getref(data) ( (data)->ref )
  132. /// Returns the value of the constant
  133. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  134. /// Returns the type of param
  135. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  136. /// Composes the uid of a reference from the id and the index
  137. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  138. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  139. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  140. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  141. #define FETCH(n, t) \
  142. if( script_hasdata(st,n) ) \
  143. (t)=script_getnum(st,n);
  144. /// Maximum amount of elements in script arrays
  145. #define SCRIPT_MAX_ARRAYSIZE 128
  146. #define SCRIPT_CMD_SUCCESS 0 ///when a buildin cmd was correctly done
  147. #define SCRIPT_CMD_FAILURE 1 ///when an errors appear in cmd, show_debug will follow
  148. #define SCRIPT_BLOCK_SIZE 512
  149. enum { LABEL_NEXTLINE=1,LABEL_START };
  150. /// temporary buffer for passing around compiled bytecode
  151. /// @see add_scriptb, set_label, parse_script
  152. static unsigned char* script_buf = NULL;
  153. static int script_pos = 0, script_size = 0;
  154. static inline int GETVALUE(const unsigned char* buf, int i)
  155. {
  156. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  157. }
  158. static inline void SETVALUE(unsigned char* buf, int i, int n)
  159. {
  160. buf[i] = GetByte(n, 0);
  161. buf[i+1] = GetByte(n, 1);
  162. buf[i+2] = GetByte(n, 2);
  163. }
  164. // String buffer structures.
  165. // str_data stores string information
  166. static struct str_data_struct {
  167. enum c_op type;
  168. int str;
  169. int backpatch;
  170. int label;
  171. int (*func)(struct script_state *st);
  172. int val;
  173. int next;
  174. } *str_data = NULL;
  175. static int str_data_size = 0; // size of the data
  176. static int str_num = LABEL_START; // next id to be assigned
  177. // str_buf holds the strings themselves
  178. static char *str_buf;
  179. static int str_size = 0; // size of the buffer
  180. static int str_pos = 0; // next position to be assigned
  181. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  182. #define SCRIPT_HASH_SIZE 1021
  183. int str_hash[SCRIPT_HASH_SIZE];
  184. // Specifies which string hashing method to use
  185. //#define SCRIPT_HASH_DJB2
  186. //#define SCRIPT_HASH_SDBM
  187. #define SCRIPT_HASH_ELF
  188. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  189. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  190. static int parse_options=0;
  191. DBMap* script_get_label_db(void){ return scriptlabel_db; }
  192. DBMap* script_get_userfunc_db(void){ return userfunc_db; }
  193. // important buildin function references for usage in scripts
  194. static int buildin_set_ref = 0;
  195. static int buildin_callsub_ref = 0;
  196. static int buildin_callfunc_ref = 0;
  197. static int buildin_getelementofarray_ref = 0;
  198. // Caches compiled autoscript item code.
  199. // Note: This is not cleared when reloading itemdb.
  200. static DBMap* autobonus_db=NULL; // char* script -> char* bytecode
  201. struct Script_Config script_config = {
  202. 1, // warn_func_mismatch_argtypes
  203. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  204. 0, INT_MAX, // input_min_value/input_max_value
  205. "OnPCDieEvent", //die_event_name
  206. "OnPCKillEvent", //kill_pc_event_name
  207. "OnNPCKillEvent", //kill_mob_event_name
  208. "OnPCLoginEvent", //login_event_name
  209. "OnPCLogoutEvent", //logout_event_name
  210. "OnPCLoadMapEvent", //loadmap_event_name
  211. "OnPCBaseLvUpEvent", //baselvup_event_name
  212. "OnPCJobLvUpEvent", //joblvup_event_name
  213. "OnPCStatCalcEvent", //stat_calc_event_name
  214. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  215. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  216. };
  217. static jmp_buf error_jump;
  218. static char* error_msg;
  219. static const char* error_pos;
  220. static int error_report; // if the error should produce output
  221. // Used by disp_warning_message
  222. static const char* parser_current_src;
  223. static const char* parser_current_file;
  224. static int parser_current_line;
  225. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  226. // [Eoe / jA 1080, 1081, 1094, 1164]
  227. enum curly_type {
  228. TYPE_NULL = 0,
  229. TYPE_IF,
  230. TYPE_SWITCH,
  231. TYPE_WHILE,
  232. TYPE_FOR,
  233. TYPE_DO,
  234. TYPE_USERFUNC,
  235. TYPE_ARGLIST // function argument list
  236. };
  237. enum e_arglist
  238. {
  239. ARGLIST_UNDEFINED = 0,
  240. ARGLIST_NO_PAREN = 1,
  241. ARGLIST_PAREN = 2,
  242. };
  243. static struct {
  244. struct {
  245. enum curly_type type;
  246. int index;
  247. int count;
  248. int flag;
  249. struct linkdb_node *case_label;
  250. } curly[256]; // Information right parenthesis
  251. int curly_count; // The number of right brackets
  252. int index; // Number of the syntax used in the script
  253. } syntax;
  254. const char* parse_curly_close(const char* p);
  255. const char* parse_syntax_close(const char* p);
  256. const char* parse_syntax_close_sub(const char* p,int* flag);
  257. const char* parse_syntax(const char* p);
  258. static int parse_syntax_for_flag = 0;
  259. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  260. extern unsigned int current_equip_combo_pos;
  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. int script_instancegetid(struct script_state *st);
  300. enum {
  301. MF_NOMEMO, //0
  302. MF_NOTELEPORT,
  303. MF_NOSAVE,
  304. MF_NOBRANCH,
  305. MF_NOPENALTY,
  306. MF_NOZENYPENALTY,
  307. MF_PVP,
  308. MF_PVP_NOPARTY,
  309. MF_PVP_NOGUILD,
  310. MF_GVG,
  311. MF_GVG_NOPARTY, //10
  312. MF_NOTRADE,
  313. MF_NOSKILL,
  314. MF_NOWARP,
  315. MF_PARTYLOCK,
  316. MF_NOICEWALL,
  317. MF_SNOW,
  318. MF_FOG,
  319. MF_SAKURA,
  320. MF_LEAVES,
  321. /**
  322. * No longer available, keeping here just in case it's back someday. [Ind]
  323. **/
  324. //MF_RAIN, //20
  325. // 21 free
  326. MF_NOGO = 22,
  327. MF_CLOUDS,
  328. MF_CLOUDS2,
  329. MF_FIREWORKS,
  330. MF_GVG_CASTLE,
  331. MF_GVG_DUNGEON,
  332. MF_NIGHTENABLED,
  333. MF_NOBASEEXP,
  334. MF_NOJOBEXP, //30
  335. MF_NOMOBLOOT,
  336. MF_NOMVPLOOT,
  337. MF_NORETURN,
  338. MF_NOWARPTO,
  339. MF_NIGHTMAREDROP,
  340. MF_RESTRICTED,
  341. MF_NOCOMMAND,
  342. MF_NODROP,
  343. MF_JEXP,
  344. MF_BEXP, //40
  345. MF_NOVENDING,
  346. MF_LOADEVENT,
  347. MF_NOCHAT,
  348. MF_NOEXPPENALTY,
  349. MF_GUILDLOCK,
  350. MF_TOWN,
  351. MF_AUTOTRADE,
  352. MF_ALLOWKS,
  353. MF_MONSTER_NOTELEPORT,
  354. MF_PVP_NOCALCRANK, //50
  355. MF_BATTLEGROUND,
  356. MF_RESET,
  357. MF_CHANNELAUTOJOIN,
  358. MF_NOUSECART,
  359. MF_NOITEMCONSUMPTION,
  360. MF_SUMSTARTMIRACLE,
  361. MF_NOMINEEFFECT,
  362. MF_NOLOCKON,
  363. MF_NOTOMB,
  364. MF_SKILL_DAMAGE //60
  365. };
  366. const char* script_op2name(int op)
  367. {
  368. #define RETURN_OP_NAME(type) case type: return #type
  369. switch( op )
  370. {
  371. RETURN_OP_NAME(C_NOP);
  372. RETURN_OP_NAME(C_POS);
  373. RETURN_OP_NAME(C_INT);
  374. RETURN_OP_NAME(C_PARAM);
  375. RETURN_OP_NAME(C_FUNC);
  376. RETURN_OP_NAME(C_STR);
  377. RETURN_OP_NAME(C_CONSTSTR);
  378. RETURN_OP_NAME(C_ARG);
  379. RETURN_OP_NAME(C_NAME);
  380. RETURN_OP_NAME(C_EOL);
  381. RETURN_OP_NAME(C_RETINFO);
  382. RETURN_OP_NAME(C_USERFUNC);
  383. RETURN_OP_NAME(C_USERFUNC_POS);
  384. // operators
  385. RETURN_OP_NAME(C_OP3);
  386. RETURN_OP_NAME(C_LOR);
  387. RETURN_OP_NAME(C_LAND);
  388. RETURN_OP_NAME(C_LE);
  389. RETURN_OP_NAME(C_LT);
  390. RETURN_OP_NAME(C_GE);
  391. RETURN_OP_NAME(C_GT);
  392. RETURN_OP_NAME(C_EQ);
  393. RETURN_OP_NAME(C_NE);
  394. RETURN_OP_NAME(C_XOR);
  395. RETURN_OP_NAME(C_OR);
  396. RETURN_OP_NAME(C_AND);
  397. RETURN_OP_NAME(C_ADD);
  398. RETURN_OP_NAME(C_SUB);
  399. RETURN_OP_NAME(C_MUL);
  400. RETURN_OP_NAME(C_DIV);
  401. RETURN_OP_NAME(C_MOD);
  402. RETURN_OP_NAME(C_NEG);
  403. RETURN_OP_NAME(C_LNOT);
  404. RETURN_OP_NAME(C_NOT);
  405. RETURN_OP_NAME(C_R_SHIFT);
  406. RETURN_OP_NAME(C_L_SHIFT);
  407. default:
  408. ShowDebug("script_op2name: unexpected op=%d\n", op);
  409. return "???";
  410. }
  411. #undef RETURN_OP_NAME
  412. }
  413. #ifdef DEBUG_DUMP_STACK
  414. static void script_dump_stack(struct script_state* st)
  415. {
  416. int i;
  417. ShowMessage("\tstart = %d\n", st->start);
  418. ShowMessage("\tend = %d\n", st->end);
  419. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  420. ShowMessage("\tsp = %d\n", st->stack->sp);
  421. for( i = 0; i < st->stack->sp; ++i )
  422. {
  423. struct script_data* data = &st->stack->stack_data[i];
  424. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  425. switch( data->type )
  426. {
  427. case C_INT:
  428. case C_POS:
  429. ShowMessage(" %d\n", data->u.num);
  430. break;
  431. case C_STR:
  432. case C_CONSTSTR:
  433. ShowMessage(" \"%s\"\n", data->u.str);
  434. break;
  435. case C_NAME:
  436. 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));
  437. break;
  438. case C_RETINFO:
  439. {
  440. struct script_retinfo* ri = data->u.ri;
  441. 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);
  442. }
  443. break;
  444. default:
  445. ShowMessage("\n");
  446. break;
  447. }
  448. }
  449. }
  450. #endif
  451. /// Reports on the console the src of a script error.
  452. static void script_reportsrc(struct script_state *st)
  453. {
  454. struct block_list* bl;
  455. if( st->oid == 0 )
  456. return; //Can't report source.
  457. bl = map_id2bl(st->oid);
  458. if( bl == NULL )
  459. return;
  460. switch( bl->type )
  461. {
  462. case BL_NPC:
  463. if( bl->m >= 0 )
  464. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  465. else
  466. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  467. break;
  468. default:
  469. if( bl->m >= 0 )
  470. 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);
  471. else
  472. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  473. break;
  474. }
  475. }
  476. /// Reports on the console information about the script data.
  477. static void script_reportdata(struct script_data* data)
  478. {
  479. if( data == NULL )
  480. return;
  481. switch( data->type )
  482. {
  483. case C_NOP:// no value
  484. ShowDebug("Data: nothing (nil)\n");
  485. break;
  486. case C_INT:// number
  487. ShowDebug("Data: number value=%d\n", data->u.num);
  488. break;
  489. case C_STR:
  490. case C_CONSTSTR:// string
  491. if( data->u.str )
  492. {
  493. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  494. }
  495. else
  496. {
  497. ShowDebug("Data: string value=NULL\n");
  498. }
  499. break;
  500. case C_NAME:// reference
  501. if( reference_tovariable(data) )
  502. {// variable
  503. const char* name = reference_getname(data);
  504. if( not_array_variable(*name) )
  505. ShowDebug("Data: variable name='%s'\n", name);
  506. else
  507. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  508. }
  509. else if( reference_toconstant(data) )
  510. {// constant
  511. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  512. }
  513. else if( reference_toparam(data) )
  514. {// param
  515. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  516. }
  517. else
  518. {// ???
  519. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  520. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  521. }
  522. break;
  523. case C_POS:// label
  524. ShowDebug("Data: label pos=%d\n", data->u.num);
  525. break;
  526. default:
  527. ShowDebug("Data: %s\n", script_op2name(data->type));
  528. break;
  529. }
  530. }
  531. /// Reports on the console information about the current built-in function.
  532. static void script_reportfunc(struct script_state* st)
  533. {
  534. int params, id;
  535. struct script_data* data;
  536. if( !script_hasdata(st,0) )
  537. {// no stack
  538. return;
  539. }
  540. data = script_getdata(st,0);
  541. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  542. {// script currently not executing a built-in function or corrupt stack
  543. return;
  544. }
  545. id = reference_getid(data);
  546. params = script_lastdata(st)-1;
  547. if( params > 0 )
  548. {
  549. int i;
  550. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  551. for( i = 2; i <= script_lastdata(st); i++ )
  552. {
  553. script_reportdata(script_getdata(st,i));
  554. }
  555. }
  556. else
  557. {
  558. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  559. }
  560. }
  561. /*==========================================
  562. * Output error message
  563. *------------------------------------------*/
  564. static void disp_error_message2(const char *mes,const char *pos,int report)
  565. {
  566. error_msg = aStrdup(mes);
  567. error_pos = pos;
  568. error_report = report;
  569. longjmp( error_jump, 1 );
  570. }
  571. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  572. static void disp_warning_message(const char *mes, const char *pos) {
  573. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  574. }
  575. /// Checks event parameter validity
  576. static void check_event(struct script_state *st, const char *evt)
  577. {
  578. if( evt && evt[0] && !stristr(evt, "::On") )
  579. {
  580. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  581. script_reportsrc(st);
  582. }
  583. }
  584. /*==========================================
  585. * Hashes the input string
  586. *------------------------------------------*/
  587. static unsigned int calc_hash(const char* p)
  588. {
  589. unsigned int h;
  590. #if defined(SCRIPT_HASH_DJB2)
  591. h = 5381;
  592. while( *p ) // hash*33 + c
  593. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  594. #elif defined(SCRIPT_HASH_SDBM)
  595. h = 0;
  596. while( *p ) // hash*65599 + c
  597. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  598. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  599. h = 0;
  600. while( *p ){
  601. unsigned int g;
  602. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  603. g = h & 0xF0000000;
  604. if( g )
  605. {
  606. h ^= g >> 24;
  607. h &= ~g;
  608. }
  609. }
  610. #else // athena hash
  611. h = 0;
  612. while( *p )
  613. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  614. #endif
  615. return h % SCRIPT_HASH_SIZE;
  616. }
  617. /*==========================================
  618. * str_data manipulation functions
  619. *------------------------------------------*/
  620. /// Looks up string using the provided id.
  621. const char* get_str(int id)
  622. {
  623. Assert( id >= LABEL_START && id < str_size );
  624. return str_buf+str_data[id].str;
  625. }
  626. /// Returns the uid of the string, or -1.
  627. static int search_str(const char* p)
  628. {
  629. int i;
  630. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  631. if( strcasecmp(get_str(i),p) == 0 )
  632. return i;
  633. return -1;
  634. }
  635. /// Stores a copy of the string and returns its id.
  636. /// If an identical string is already present, returns its id instead.
  637. int add_str(const char* p)
  638. {
  639. int h;
  640. int len;
  641. h = calc_hash(p);
  642. if( str_hash[h] == 0 )
  643. {// empty bucket, add new node here
  644. str_hash[h] = str_num;
  645. }
  646. else
  647. {// scan for end of list, or occurence of identical string
  648. int i;
  649. for( i = str_hash[h]; ; i = str_data[i].next )
  650. {
  651. if( strcasecmp(get_str(i),p) == 0 )
  652. return i; // string already in list
  653. if( str_data[i].next == 0 )
  654. break; // reached the end
  655. }
  656. // append node to end of list
  657. str_data[i].next = str_num;
  658. }
  659. // grow list if neccessary
  660. if( str_num >= str_data_size )
  661. {
  662. str_data_size += 128;
  663. RECREATE(str_data,struct str_data_struct,str_data_size);
  664. memset(str_data + (str_data_size - 128), '\0', 128);
  665. }
  666. len=(int)strlen(p);
  667. // grow string buffer if neccessary
  668. while( str_pos+len+1 >= str_size )
  669. {
  670. str_size += 256;
  671. RECREATE(str_buf,char,str_size);
  672. memset(str_buf + (str_size - 256), '\0', 256);
  673. }
  674. safestrncpy(str_buf+str_pos, p, len+1);
  675. str_data[str_num].type = C_NOP;
  676. str_data[str_num].str = str_pos;
  677. str_data[str_num].next = 0;
  678. str_data[str_num].func = NULL;
  679. str_data[str_num].backpatch = -1;
  680. str_data[str_num].label = -1;
  681. str_pos += len+1;
  682. return str_num++;
  683. }
  684. /// Appends 1 byte to the script buffer.
  685. static void add_scriptb(int a)
  686. {
  687. if( script_pos+1 >= script_size )
  688. {
  689. script_size += SCRIPT_BLOCK_SIZE;
  690. RECREATE(script_buf,unsigned char,script_size);
  691. }
  692. script_buf[script_pos++] = (uint8)(a);
  693. }
  694. /// Appends a c_op value to the script buffer.
  695. /// The value is variable-length encoded into 8-bit blocks.
  696. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  697. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  698. static void add_scriptc(int a)
  699. {
  700. while( a >= 0x40 )
  701. {
  702. add_scriptb((a&0x3f)|0x40);
  703. a = (a - 0x40) >> 6;
  704. }
  705. add_scriptb(a);
  706. }
  707. /// Appends an integer value to the script buffer.
  708. /// The value is variable-length encoded into 8-bit blocks.
  709. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  710. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  711. static void add_scripti(int a)
  712. {
  713. while( a >= 0x40 )
  714. {
  715. add_scriptb((a&0x3f)|0xc0);
  716. a = (a - 0x40) >> 6;
  717. }
  718. add_scriptb(a|0x80);
  719. }
  720. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  721. ///
  722. /// @param l The id of the str_data entry
  723. // Maximum up to 16M
  724. static void add_scriptl(int l)
  725. {
  726. int backpatch = str_data[l].backpatch;
  727. switch(str_data[l].type){
  728. case C_POS:
  729. case C_USERFUNC_POS:
  730. add_scriptc(C_POS);
  731. add_scriptb(str_data[l].label);
  732. add_scriptb(str_data[l].label>>8);
  733. add_scriptb(str_data[l].label>>16);
  734. break;
  735. case C_NOP:
  736. case C_USERFUNC:
  737. // Embedded data backpatch there is a possibility of label
  738. add_scriptc(C_NAME);
  739. str_data[l].backpatch = script_pos;
  740. add_scriptb(backpatch);
  741. add_scriptb(backpatch>>8);
  742. add_scriptb(backpatch>>16);
  743. break;
  744. case C_INT:
  745. add_scripti(abs(str_data[l].val));
  746. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  747. add_scriptc(C_NEG);
  748. break;
  749. default: // assume C_NAME
  750. add_scriptc(C_NAME);
  751. add_scriptb(l);
  752. add_scriptb(l>>8);
  753. add_scriptb(l>>16);
  754. break;
  755. }
  756. }
  757. /*==========================================
  758. * Resolve the label
  759. *------------------------------------------*/
  760. void set_label(int l,int pos, const char* script_pos_cur)
  761. {
  762. int i;
  763. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  764. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  765. disp_error_message("set_label: invalid label name",script_pos_cur);
  766. return;
  767. }
  768. if(str_data[l].label!=-1){
  769. disp_error_message("set_label: dup label ",script_pos_cur);
  770. return;
  771. }
  772. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  773. str_data[l].label=pos;
  774. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  775. int next=GETVALUE(script_buf,i);
  776. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  777. SETVALUE(script_buf,i,pos);
  778. i=next;
  779. }
  780. }
  781. /// Skips spaces and/or comments.
  782. const char* skip_space(const char* p)
  783. {
  784. if( p == NULL )
  785. return NULL;
  786. for(;;)
  787. {
  788. while( ISSPACE(*p) )
  789. ++p;
  790. if( *p == '/' && p[1] == '/' )
  791. {// line comment
  792. while(*p && *p!='\n')
  793. ++p;
  794. }
  795. else if( *p == '/' && p[1] == '*' )
  796. {// block comment
  797. p += 2;
  798. for(;;)
  799. {
  800. if( *p == '\0' ) {
  801. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  802. return p;
  803. }
  804. if( *p == '*' && p[1] == '/' )
  805. {// end of block comment
  806. p += 2;
  807. break;
  808. }
  809. ++p;
  810. }
  811. }
  812. else
  813. break;
  814. }
  815. return p;
  816. }
  817. /// Skips a word.
  818. /// A word consists of undercores and/or alphanumeric characters,
  819. /// and valid variable prefixes/postfixes.
  820. static
  821. const char* skip_word(const char* p)
  822. {
  823. // prefix
  824. switch( *p )
  825. {
  826. case '@':// temporary char variable
  827. ++p; break;
  828. case '#':// account variable
  829. p += ( p[1] == '#' ? 2 : 1 ); break;
  830. case '\'':// instance variable
  831. ++p; break;
  832. case '.':// npc variable
  833. p += ( p[1] == '@' ? 2 : 1 ); break;
  834. case '$':// global variable
  835. p += ( p[1] == '@' ? 2 : 1 ); break;
  836. }
  837. while( ISALNUM(*p) || *p == '_' )
  838. ++p;
  839. // postfix
  840. if( *p == '$' )// string
  841. p++;
  842. return p;
  843. }
  844. /// Adds a word to str_data.
  845. /// @see skip_word
  846. /// @see add_str
  847. static
  848. int add_word(const char* p)
  849. {
  850. char* word;
  851. int len;
  852. int i;
  853. // Check for a word
  854. len = skip_word(p) - p;
  855. if( len == 0 )
  856. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  857. // Duplicate the word
  858. word = (char*)aMalloc(len+1);
  859. memcpy(word, p, len);
  860. word[len] = 0;
  861. // add the word
  862. i = add_str(word);
  863. aFree(word);
  864. return i;
  865. }
  866. /// Parses a function call.
  867. /// The argument list can have parenthesis or not.
  868. /// The number of arguments is checked.
  869. static
  870. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  871. {
  872. const char* p2;
  873. const char* arg=NULL;
  874. int func;
  875. func = add_word(p);
  876. if( str_data[func].type == C_FUNC ){
  877. // buildin function
  878. add_scriptl(func);
  879. add_scriptc(C_ARG);
  880. arg = buildin_func[str_data[func].val].arg;
  881. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  882. // script defined function
  883. add_scriptl(buildin_callsub_ref);
  884. add_scriptc(C_ARG);
  885. add_scriptl(func);
  886. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  887. if( *arg == 0 )
  888. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  889. if( *arg != '*' )
  890. ++arg; // count func as argument
  891. } else {
  892. #ifdef SCRIPT_CALLFUNC_CHECK
  893. const char* name = get_str(func);
  894. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  895. #endif
  896. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  897. #ifdef SCRIPT_CALLFUNC_CHECK
  898. } else {;
  899. add_scriptl(buildin_callfunc_ref);
  900. add_scriptc(C_ARG);
  901. add_scriptc(C_STR);
  902. while( *name ) add_scriptb(*name ++);
  903. add_scriptb(0);
  904. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  905. if( *arg != '*' ) ++ arg;
  906. }
  907. #endif
  908. }
  909. p = skip_word(p);
  910. p = skip_space(p);
  911. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  912. syntax.curly[syntax.curly_count].count = 0;
  913. if( *p == ';' )
  914. {// <func name> ';'
  915. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  916. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  917. {// <func name> '(' ')'
  918. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  919. p = p2;
  920. /*
  921. } else if( 0 && require_paren && *p != '(' )
  922. {// <func name>
  923. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  924. */
  925. } else
  926. {// <func name> <arg list>
  927. if( require_paren ){
  928. if( *p != '(' )
  929. disp_error_message("need '('",p);
  930. ++p; // skip '('
  931. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  932. } else if( *p == '(' ){
  933. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  934. } else {
  935. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  936. }
  937. ++syntax.curly_count;
  938. while( *arg ) {
  939. p2=parse_subexpr(p,-1);
  940. if( p == p2 )
  941. break; // not an argument
  942. if( *arg != '*' )
  943. ++arg; // next argument
  944. p=skip_space(p2);
  945. if( *arg == 0 || *p != ',' )
  946. break; // no more arguments
  947. ++p; // skip comma
  948. }
  949. --syntax.curly_count;
  950. }
  951. if( *arg && *arg != '?' && *arg != '*' )
  952. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  953. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  954. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  955. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  956. if( *p != ')' )
  957. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  958. ++p;
  959. }
  960. add_scriptc(C_FUNC);
  961. return p;
  962. }
  963. /// Processes end of logical script line.
  964. /// @param first When true, only fix up scheduling data is initialized
  965. /// @param p Script position for error reporting in set_label
  966. static void parse_nextline(bool first, const char* p)
  967. {
  968. if( !first )
  969. {
  970. add_scriptc(C_EOL); // mark end of line for stack cleanup
  971. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  972. }
  973. // initialize data for new '-' label fix up scheduling
  974. str_data[LABEL_NEXTLINE].type = C_NOP;
  975. str_data[LABEL_NEXTLINE].backpatch = -1;
  976. str_data[LABEL_NEXTLINE].label = -1;
  977. }
  978. /// Parse a variable assignment using the direct equals operator
  979. /// @param p script position where the function should run from
  980. /// @return NULL if not a variable assignment, the new position otherwise
  981. const char* parse_variable(const char* p) {
  982. int word;
  983. c_op type = C_NOP;
  984. const char *p2 = NULL;
  985. const char *var = p;
  986. // skip the variable where applicable
  987. p = skip_word(p);
  988. p = skip_space(p);
  989. if( p == NULL ) {// end of the line or invalid buffer
  990. return NULL;
  991. }
  992. if( *p == '[' ) {// array variable so process the array as appropriate
  993. int i,j;
  994. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  995. if( *p ++ == ']' && --(j) == 0 ) break;
  996. if( *p == '[' ) ++ j;
  997. }
  998. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  999. disp_error_message("Missing right expression or closing bracket for variable.", p);
  1000. }
  1001. }
  1002. if( type == C_NOP &&
  1003. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  1004. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  1005. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  1006. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  1007. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  1008. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  1009. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  1010. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  1011. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  1012. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  1013. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  1014. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  1015. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  1016. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  1017. ) )
  1018. {// failed to find a matching operator combination so invalid
  1019. return NULL;
  1020. }
  1021. switch( type ) {
  1022. case C_EQ: {// incremental modifier
  1023. p = skip_space( &p[1] );
  1024. }
  1025. break;
  1026. case C_L_SHIFT:
  1027. case C_R_SHIFT: {// left or right shift modifier
  1028. p = skip_space( &p[3] );
  1029. }
  1030. break;
  1031. default: {// normal incremental command
  1032. p = skip_space( &p[2] );
  1033. }
  1034. }
  1035. if( p == NULL ) {// end of line or invalid buffer
  1036. return NULL;
  1037. }
  1038. // push the set function onto the stack
  1039. add_scriptl(buildin_set_ref);
  1040. add_scriptc(C_ARG);
  1041. // always append parenthesis to avoid errors
  1042. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  1043. syntax.curly[syntax.curly_count].count = 0;
  1044. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  1045. // increment the total curly count for the position in the script
  1046. ++ syntax.curly_count;
  1047. // parse the variable currently being modified
  1048. word = add_word(var);
  1049. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  1050. {// cannot assign a variable which exists as a function or label
  1051. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  1052. }
  1053. if( p2 ) {// process the variable index
  1054. const char* p3 = NULL;
  1055. // push the getelementofarray method into the stack
  1056. add_scriptl(buildin_getelementofarray_ref);
  1057. add_scriptc(C_ARG);
  1058. add_scriptl(word);
  1059. // process the sub-expression for this assignment
  1060. p3 = parse_subexpr(p2 + 1, 1);
  1061. p3 = skip_space(p3);
  1062. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1063. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1064. }
  1065. // push the closing function stack operator onto the stack
  1066. add_scriptc(C_FUNC);
  1067. p3 ++;
  1068. } else {// simply push the variable or value onto the stack
  1069. add_scriptl(word);
  1070. }
  1071. if( type != C_EQ )
  1072. add_scriptc(C_REF);
  1073. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1074. add_scripti(1);
  1075. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1076. } else {// process the value as an expression
  1077. p = parse_subexpr(p, -1);
  1078. if( type != C_EQ )
  1079. {// push the type of modifier onto the stack
  1080. add_scriptc(type);
  1081. }
  1082. }
  1083. // decrement the curly count for the position within the script
  1084. -- syntax.curly_count;
  1085. // close the script by appending the function operator
  1086. add_scriptc(C_FUNC);
  1087. // push the buffer from the method
  1088. return p;
  1089. }
  1090. /*
  1091. * Checks whether the gives string is a number literal
  1092. *
  1093. * Mainly necessary to differentiate between number literals and NPC name
  1094. * constants, since several of those start with a digit.
  1095. *
  1096. * All this does is to check if the string begins with an optional + or - sign,
  1097. * followed by a hexadecimal or decimal number literal literal and is NOT
  1098. * followed by a underscore or letter.
  1099. *
  1100. * @author : Hercules.ws
  1101. * @param p Pointer to the string to check
  1102. * @return Whether the string is a number literal
  1103. */
  1104. bool is_number(const char *p) {
  1105. const char *np;
  1106. if (!p)
  1107. return false;
  1108. if (*p == '-' || *p == '+')
  1109. p++;
  1110. np = p;
  1111. if (*p == '0' && p[1] == 'x') {
  1112. p+=2;
  1113. np = p;
  1114. // Hexadecimal
  1115. while (ISXDIGIT(*np))
  1116. np++;
  1117. } else {
  1118. // Decimal
  1119. while (ISDIGIT(*np))
  1120. np++;
  1121. }
  1122. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1123. return true;
  1124. return false;
  1125. }
  1126. /*==========================================
  1127. * Analysis section
  1128. *------------------------------------------*/
  1129. const char* parse_simpleexpr(const char *p)
  1130. {
  1131. long long i;
  1132. p=skip_space(p);
  1133. if(*p==';' || *p==',')
  1134. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1135. if(*p=='('){
  1136. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1137. ++syntax.curly[i].count;
  1138. p=parse_subexpr(p+1,-1);
  1139. p=skip_space(p);
  1140. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1141. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1142. ){
  1143. if( *p == ',' ){
  1144. syntax.curly[i].flag = ARGLIST_PAREN;
  1145. return p;
  1146. } else
  1147. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1148. }
  1149. if( *p != ')' )
  1150. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1151. ++p;
  1152. } else if(is_number(p)) {
  1153. char *np;
  1154. while(*p == '0' && ISDIGIT(p[1])) p++;
  1155. i=strtoll(p,&np,0);
  1156. if( i < INT_MIN ) {
  1157. i = INT_MIN;
  1158. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1159. } else if( i > INT_MAX ) {
  1160. i = INT_MAX;
  1161. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1162. }
  1163. add_scripti((int)i);
  1164. p=np;
  1165. } else if(*p=='"'){
  1166. add_scriptc(C_STR);
  1167. p++;
  1168. while( *p && *p != '"' ){
  1169. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1170. {
  1171. char buf[8];
  1172. size_t len = skip_escaped_c(p) - p;
  1173. size_t n = sv_unescape_c(buf, p, len);
  1174. if( n != 1 )
  1175. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1176. p += len;
  1177. add_scriptb(*buf);
  1178. continue;
  1179. }
  1180. else if( *p == '\n' )
  1181. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1182. add_scriptb(*p++);
  1183. }
  1184. if(!*p)
  1185. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1186. add_scriptb(0);
  1187. p++; //'"'
  1188. } else {
  1189. int l;
  1190. const char* pv;
  1191. // label , register , function etc
  1192. if(skip_word(p)==p)
  1193. disp_error_message("parse_simpleexpr: unexpected character",p);
  1194. l=add_word(p);
  1195. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1196. return parse_callfunc(p,1,0);
  1197. #ifdef SCRIPT_CALLFUNC_CHECK
  1198. else {
  1199. const char* name = get_str(l);
  1200. if( strdb_get(userfunc_db,name) != NULL ) {
  1201. return parse_callfunc(p,1,1);
  1202. }
  1203. }
  1204. #endif
  1205. if( (pv = parse_variable(p)) )
  1206. {// successfully processed a variable assignment
  1207. return pv;
  1208. }
  1209. p=skip_word(p);
  1210. if( *p == '[' ){
  1211. // array(name[i] => getelementofarray(name,i) )
  1212. add_scriptl(buildin_getelementofarray_ref);
  1213. add_scriptc(C_ARG);
  1214. add_scriptl(l);
  1215. p=parse_subexpr(p+1,-1);
  1216. p=skip_space(p);
  1217. if( *p != ']' )
  1218. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1219. ++p;
  1220. add_scriptc(C_FUNC);
  1221. }else
  1222. add_scriptl(l);
  1223. }
  1224. return p;
  1225. }
  1226. /*==========================================
  1227. * Analysis of the expression
  1228. *------------------------------------------*/
  1229. const char* parse_subexpr(const char* p,int limit)
  1230. {
  1231. int op,opl,len;
  1232. p=skip_space(p);
  1233. if( *p == '-' ){
  1234. const char* tmpp = skip_space(p+1);
  1235. if( *tmpp == ';' || *tmpp == ',' ){
  1236. add_scriptl(LABEL_NEXTLINE);
  1237. p++;
  1238. return p;
  1239. }
  1240. }
  1241. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1242. p=parse_subexpr(p+1,10);
  1243. add_scriptc(op);
  1244. } else
  1245. p=parse_simpleexpr(p);
  1246. p=skip_space(p);
  1247. while((
  1248. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1249. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1250. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1251. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1252. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1253. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1254. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1255. (op=C_AND,opl=6,len=1,*p=='&') ||
  1256. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1257. (op=C_OR,opl=5,len=1,*p=='|') ||
  1258. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1259. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1260. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1261. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1262. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1263. (op=C_GT,opl=3,len=1,*p=='>') ||
  1264. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1265. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1266. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1267. p+=len;
  1268. if(op == C_OP3) {
  1269. p=parse_subexpr(p,-1);
  1270. p=skip_space(p);
  1271. if( *(p++) != ':')
  1272. disp_error_message("parse_subexpr: expected ':'", p-1);
  1273. p=parse_subexpr(p,-1);
  1274. } else {
  1275. p=parse_subexpr(p,opl);
  1276. }
  1277. add_scriptc(op);
  1278. p=skip_space(p);
  1279. }
  1280. return p; /* return first untreated operator */
  1281. }
  1282. /*==========================================
  1283. * Evaluation of the expression
  1284. *------------------------------------------*/
  1285. const char* parse_expr(const char *p)
  1286. {
  1287. switch(*p){
  1288. case ')': case ';': case ':': case '[': case ']':
  1289. case '}':
  1290. disp_error_message("parse_expr: unexpected character",p);
  1291. }
  1292. p=parse_subexpr(p,-1);
  1293. return p;
  1294. }
  1295. /*==========================================
  1296. * Analysis of the line
  1297. *------------------------------------------*/
  1298. const char* parse_line(const char* p)
  1299. {
  1300. const char* p2;
  1301. p=skip_space(p);
  1302. if(*p==';') {
  1303. //Close decision for if(); for(); while();
  1304. p = parse_syntax_close(p + 1);
  1305. return p;
  1306. }
  1307. if(*p==')' && parse_syntax_for_flag)
  1308. return p+1;
  1309. p = skip_space(p);
  1310. if(p[0] == '{') {
  1311. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1312. syntax.curly[syntax.curly_count].count = -1;
  1313. syntax.curly[syntax.curly_count].index = -1;
  1314. syntax.curly_count++;
  1315. return p + 1;
  1316. } else if(p[0] == '}') {
  1317. return parse_curly_close(p);
  1318. }
  1319. // Syntax-related processing
  1320. p2 = parse_syntax(p);
  1321. if(p2 != NULL)
  1322. return p2;
  1323. // attempt to process a variable assignment
  1324. p2 = parse_variable(p);
  1325. if( p2 != NULL )
  1326. {// variable assignment processed so leave the method
  1327. return parse_syntax_close(p2 + 1);
  1328. }
  1329. p = parse_callfunc(p,0,0);
  1330. p = skip_space(p);
  1331. if(parse_syntax_for_flag) {
  1332. if( *p != ')' )
  1333. disp_error_message("parse_line: expected ')'",p);
  1334. } else {
  1335. if( *p != ';' )
  1336. disp_error_message("parse_line: expected ';'",p);
  1337. }
  1338. //Binding decision for if(), for(), while()
  1339. p = parse_syntax_close(p+1);
  1340. return p;
  1341. }
  1342. // { ... } Closing process
  1343. const char* parse_curly_close(const char* p)
  1344. {
  1345. if(syntax.curly_count <= 0) {
  1346. disp_error_message("parse_curly_close: unexpected string",p);
  1347. return p + 1;
  1348. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1349. syntax.curly_count--;
  1350. //Close decision if, for , while
  1351. p = parse_syntax_close(p + 1);
  1352. return p;
  1353. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1354. //Closing switch()
  1355. int pos = syntax.curly_count-1;
  1356. char label[256];
  1357. int l;
  1358. // Remove temporary variables
  1359. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1360. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1361. parse_line(label);
  1362. syntax.curly_count--;
  1363. // Go to the end pointer unconditionally
  1364. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1365. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1366. parse_line(label);
  1367. syntax.curly_count--;
  1368. // You are here labeled
  1369. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1370. l=add_str(label);
  1371. set_label(l,script_pos, p);
  1372. if(syntax.curly[pos].flag) {
  1373. //Exists default
  1374. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1375. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1376. parse_line(label);
  1377. syntax.curly_count--;
  1378. }
  1379. // Label end
  1380. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1381. l=add_str(label);
  1382. set_label(l,script_pos, p);
  1383. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1384. syntax.curly_count--;
  1385. //Closing decision if, for , while
  1386. p = parse_syntax_close(p + 1);
  1387. return p;
  1388. } else {
  1389. disp_error_message("parse_curly_close: unexpected string",p);
  1390. return p + 1;
  1391. }
  1392. }
  1393. // Syntax-related processing
  1394. // break, case, continue, default, do, for, function,
  1395. // if, switch, while ? will handle this internally.
  1396. const char* parse_syntax(const char* p)
  1397. {
  1398. const char *p2 = skip_word(p);
  1399. switch(*p) {
  1400. case 'B':
  1401. case 'b':
  1402. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1403. // break Processing
  1404. char label[256];
  1405. int pos = syntax.curly_count - 1;
  1406. while(pos >= 0) {
  1407. if(syntax.curly[pos].type == TYPE_DO) {
  1408. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1409. break;
  1410. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1411. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1412. break;
  1413. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1414. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1415. break;
  1416. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1417. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1418. break;
  1419. }
  1420. pos--;
  1421. }
  1422. if(pos < 0) {
  1423. disp_error_message("parse_syntax: unexpected 'break'",p);
  1424. } else {
  1425. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1426. parse_line(label);
  1427. syntax.curly_count--;
  1428. }
  1429. p = skip_space(p2);
  1430. if(*p != ';')
  1431. disp_error_message("parse_syntax: expected ';'",p);
  1432. // Closing decision if, for , while
  1433. p = parse_syntax_close(p + 1);
  1434. return p;
  1435. }
  1436. break;
  1437. case 'c':
  1438. case 'C':
  1439. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1440. //Processing case
  1441. int pos = syntax.curly_count-1;
  1442. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1443. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1444. return p+1;
  1445. } else {
  1446. char label[256];
  1447. int l,v;
  1448. char *np;
  1449. if(syntax.curly[pos].count != 1) {
  1450. //Jump for FALLTHRU
  1451. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1452. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1453. parse_line(label);
  1454. syntax.curly_count--;
  1455. // You are here labeled
  1456. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1457. l=add_str(label);
  1458. set_label(l,script_pos, p);
  1459. }
  1460. //Decision statement switch
  1461. p = skip_space(p2);
  1462. if(p == p2) {
  1463. disp_error_message("parse_syntax: expected a space ' '",p);
  1464. }
  1465. // check whether case label is integer or not
  1466. if(is_number(p)) {
  1467. //Numeric value
  1468. v = (int)strtol(p,&np,0);
  1469. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1470. p++;
  1471. p = skip_word(p);
  1472. if(np != p)
  1473. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1474. } else {
  1475. //Check for constants
  1476. p2 = skip_word(p);
  1477. v = (int)(size_t) (p2-p); // length of word at p2
  1478. memcpy(label,p,v);
  1479. label[v]='\0';
  1480. if( !script_get_constant(label, &v) )
  1481. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1482. p = skip_word(p);
  1483. }
  1484. p = skip_space(p);
  1485. if(*p != ':')
  1486. disp_error_message("parse_syntax: expect ':'",p);
  1487. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1488. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1489. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1490. // Bad I do not parse twice
  1491. p2 = parse_line(label);
  1492. parse_line(p2);
  1493. syntax.curly_count--;
  1494. if(syntax.curly[pos].count != 1) {
  1495. // Label after the completion of FALLTHRU
  1496. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1497. l=add_str(label);
  1498. set_label(l,script_pos,p);
  1499. }
  1500. // check duplication of case label [Rayce]
  1501. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1502. disp_error_message("parse_syntax: dup 'case'",p);
  1503. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1504. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1505. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1506. parse_line(label);
  1507. syntax.curly_count--;
  1508. syntax.curly[pos].count++;
  1509. }
  1510. return p + 1;
  1511. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1512. // Processing continue
  1513. char label[256];
  1514. int pos = syntax.curly_count - 1;
  1515. while(pos >= 0) {
  1516. if(syntax.curly[pos].type == TYPE_DO) {
  1517. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1518. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1519. break;
  1520. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1521. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1522. break;
  1523. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1524. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1525. break;
  1526. }
  1527. pos--;
  1528. }
  1529. if(pos < 0) {
  1530. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1531. } else {
  1532. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1533. parse_line(label);
  1534. syntax.curly_count--;
  1535. }
  1536. p = skip_space(p2);
  1537. if(*p != ';')
  1538. disp_error_message("parse_syntax: expected ';'",p);
  1539. //Closing decision if, for , while
  1540. p = parse_syntax_close(p + 1);
  1541. return p;
  1542. }
  1543. break;
  1544. case 'd':
  1545. case 'D':
  1546. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1547. // Switch - default processing
  1548. int pos = syntax.curly_count-1;
  1549. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1550. disp_error_message("parse_syntax: unexpected 'default'",p);
  1551. } else if(syntax.curly[pos].flag) {
  1552. disp_error_message("parse_syntax: dup 'default'",p);
  1553. } else {
  1554. char label[256];
  1555. int l;
  1556. // Put the label location
  1557. p = skip_space(p2);
  1558. if(*p != ':') {
  1559. disp_error_message("parse_syntax: expected ':'",p);
  1560. }
  1561. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1562. l=add_str(label);
  1563. set_label(l,script_pos,p);
  1564. // Skip to the next link w/o condition
  1565. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1566. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1567. parse_line(label);
  1568. syntax.curly_count--;
  1569. // The default label
  1570. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1571. l=add_str(label);
  1572. set_label(l,script_pos,p);
  1573. syntax.curly[syntax.curly_count - 1].flag = 1;
  1574. syntax.curly[pos].count++;
  1575. }
  1576. return p + 1;
  1577. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1578. int l;
  1579. char label[256];
  1580. p=skip_space(p2);
  1581. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1582. syntax.curly[syntax.curly_count].count = 1;
  1583. syntax.curly[syntax.curly_count].index = syntax.index++;
  1584. syntax.curly[syntax.curly_count].flag = 0;
  1585. // Label of the (do) form here
  1586. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1587. l=add_str(label);
  1588. set_label(l,script_pos,p);
  1589. syntax.curly_count++;
  1590. return p;
  1591. }
  1592. break;
  1593. case 'f':
  1594. case 'F':
  1595. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1596. int l;
  1597. char label[256];
  1598. int pos = syntax.curly_count;
  1599. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1600. syntax.curly[syntax.curly_count].count = 1;
  1601. syntax.curly[syntax.curly_count].index = syntax.index++;
  1602. syntax.curly[syntax.curly_count].flag = 0;
  1603. syntax.curly_count++;
  1604. p=skip_space(p2);
  1605. if(*p != '(')
  1606. disp_error_message("parse_syntax: expected '('",p);
  1607. p++;
  1608. // Execute the initialization statement
  1609. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1610. p=parse_line(p);
  1611. syntax.curly_count--;
  1612. // Form the start of label decision
  1613. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1614. l=add_str(label);
  1615. set_label(l,script_pos,p);
  1616. p=skip_space(p);
  1617. if(*p == ';') {
  1618. // For (; Because the pattern of always true ;)
  1619. ;
  1620. } else {
  1621. // Skip to the end point if the condition is false
  1622. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1623. add_scriptl(add_str("jump_zero"));
  1624. add_scriptc(C_ARG);
  1625. p=parse_expr(p);
  1626. p=skip_space(p);
  1627. add_scriptl(add_str(label));
  1628. add_scriptc(C_FUNC);
  1629. }
  1630. if(*p != ';')
  1631. disp_error_message("parse_syntax: expected ';'",p);
  1632. p++;
  1633. // Skip to the beginning of the loop
  1634. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1635. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1636. parse_line(label);
  1637. syntax.curly_count--;
  1638. // Labels to form the next loop
  1639. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1640. l=add_str(label);
  1641. set_label(l,script_pos,p);
  1642. // Process the next time you enter the loop
  1643. // A ')' last for; flag to be treated as'
  1644. parse_syntax_for_flag = 1;
  1645. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1646. p=parse_line(p);
  1647. syntax.curly_count--;
  1648. parse_syntax_for_flag = 0;
  1649. // Skip to the determination process conditions
  1650. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1651. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1652. parse_line(label);
  1653. syntax.curly_count--;
  1654. // Loop start labeling
  1655. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1656. l=add_str(label);
  1657. set_label(l,script_pos,p);
  1658. return p;
  1659. }
  1660. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1661. {// internal script function
  1662. const char *func_name;
  1663. func_name = skip_space(p2);
  1664. p = skip_word(func_name);
  1665. if( p == func_name )
  1666. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1667. p2 = skip_space(p);
  1668. if( *p2 == ';' )
  1669. {// function <name> ;
  1670. // function declaration - just register the name
  1671. int l;
  1672. l = add_word(func_name);
  1673. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1674. str_data[l].type = C_USERFUNC;
  1675. else if( str_data[l].type == C_USERFUNC )
  1676. ; // already registered
  1677. else
  1678. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1679. // Close condition of if, for, while
  1680. p = parse_syntax_close(p2 + 1);
  1681. return p;
  1682. }
  1683. else if(*p2 == '{')
  1684. {// function <name> <line/block of code>
  1685. char label[256];
  1686. int l;
  1687. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1688. syntax.curly[syntax.curly_count].count = 1;
  1689. syntax.curly[syntax.curly_count].index = syntax.index++;
  1690. syntax.curly[syntax.curly_count].flag = 0;
  1691. ++syntax.curly_count;
  1692. // Jump over the function code
  1693. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1694. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1695. ++syntax.curly_count;
  1696. parse_line(label);
  1697. --syntax.curly_count;
  1698. // Set the position of the function (label)
  1699. l=add_word(func_name);
  1700. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1701. {
  1702. str_data[l].type = C_USERFUNC;
  1703. set_label(l, script_pos, p);
  1704. if( parse_options&SCRIPT_USE_LABEL_DB )
  1705. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1706. }
  1707. else
  1708. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1709. return skip_space(p);
  1710. }
  1711. else
  1712. {
  1713. disp_error_message("expect ';' or '{' at function syntax",p);
  1714. }
  1715. }
  1716. break;
  1717. case 'i':
  1718. case 'I':
  1719. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1720. // If process
  1721. char label[256];
  1722. p=skip_space(p2);
  1723. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1724. disp_error_message("need '('",p);
  1725. }
  1726. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1727. syntax.curly[syntax.curly_count].count = 1;
  1728. syntax.curly[syntax.curly_count].index = syntax.index++;
  1729. syntax.curly[syntax.curly_count].flag = 0;
  1730. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1731. syntax.curly_count++;
  1732. add_scriptl(add_str("jump_zero"));
  1733. add_scriptc(C_ARG);
  1734. p=parse_expr(p);
  1735. p=skip_space(p);
  1736. add_scriptl(add_str(label));
  1737. add_scriptc(C_FUNC);
  1738. return p;
  1739. }
  1740. break;
  1741. case 's':
  1742. case 'S':
  1743. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1744. // Processing of switch ()
  1745. char label[256];
  1746. p=skip_space(p2);
  1747. if(*p != '(') {
  1748. disp_error_message("need '('",p);
  1749. }
  1750. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1751. syntax.curly[syntax.curly_count].count = 1;
  1752. syntax.curly[syntax.curly_count].index = syntax.index++;
  1753. syntax.curly[syntax.curly_count].flag = 0;
  1754. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1755. syntax.curly_count++;
  1756. add_scriptl(add_str("set"));
  1757. add_scriptc(C_ARG);
  1758. add_scriptl(add_str(label));
  1759. p=parse_expr(p);
  1760. p=skip_space(p);
  1761. if(*p != '{') {
  1762. disp_error_message("parse_syntax: expected '{'",p);
  1763. }
  1764. add_scriptc(C_FUNC);
  1765. return p + 1;
  1766. }
  1767. break;
  1768. case 'w':
  1769. case 'W':
  1770. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1771. int l;
  1772. char label[256];
  1773. p=skip_space(p2);
  1774. if(*p != '(') {
  1775. disp_error_message("need '('",p);
  1776. }
  1777. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1778. syntax.curly[syntax.curly_count].count = 1;
  1779. syntax.curly[syntax.curly_count].index = syntax.index++;
  1780. syntax.curly[syntax.curly_count].flag = 0;
  1781. // Form the start of label decision
  1782. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1783. l=add_str(label);
  1784. set_label(l,script_pos,p);
  1785. // Skip to the end point if the condition is false
  1786. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1787. syntax.curly_count++;
  1788. add_scriptl(add_str("jump_zero"));
  1789. add_scriptc(C_ARG);
  1790. p=parse_expr(p);
  1791. p=skip_space(p);
  1792. add_scriptl(add_str(label));
  1793. add_scriptc(C_FUNC);
  1794. return p;
  1795. }
  1796. break;
  1797. }
  1798. return NULL;
  1799. }
  1800. const char* parse_syntax_close(const char *p) {
  1801. // If (...) for (...) hoge (); as to make sure closed closed once again
  1802. int flag;
  1803. do {
  1804. p = parse_syntax_close_sub(p,&flag);
  1805. } while(flag);
  1806. return p;
  1807. }
  1808. // Close judgment if, for, while, of do
  1809. // flag == 1 : closed
  1810. // flag == 0 : not closed
  1811. const char* parse_syntax_close_sub(const char* p,int* flag)
  1812. {
  1813. char label[256];
  1814. int pos = syntax.curly_count - 1;
  1815. int l;
  1816. *flag = 1;
  1817. if(syntax.curly_count <= 0) {
  1818. *flag = 0;
  1819. return p;
  1820. } else if(syntax.curly[pos].type == TYPE_IF) {
  1821. const char *bp = p;
  1822. const char *p2;
  1823. // if-block and else-block end is a new line
  1824. parse_nextline(false, p);
  1825. // Skip to the last location if
  1826. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1827. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1828. parse_line(label);
  1829. syntax.curly_count--;
  1830. // Put the label of the location
  1831. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1832. l=add_str(label);
  1833. set_label(l,script_pos,p);
  1834. syntax.curly[pos].count++;
  1835. p = skip_space(p);
  1836. p2 = skip_word(p);
  1837. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1838. // else or else - if
  1839. p = skip_space(p2);
  1840. p2 = skip_word(p);
  1841. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1842. // else - if
  1843. p=skip_space(p2);
  1844. if(*p != '(') {
  1845. disp_error_message("need '('",p);
  1846. }
  1847. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1848. add_scriptl(add_str("jump_zero"));
  1849. add_scriptc(C_ARG);
  1850. p=parse_expr(p);
  1851. p=skip_space(p);
  1852. add_scriptl(add_str(label));
  1853. add_scriptc(C_FUNC);
  1854. *flag = 0;
  1855. return p;
  1856. } else {
  1857. // else
  1858. if(!syntax.curly[pos].flag) {
  1859. syntax.curly[pos].flag = 1;
  1860. *flag = 0;
  1861. return p;
  1862. }
  1863. }
  1864. }
  1865. // Close if
  1866. syntax.curly_count--;
  1867. // Put the label of the final location
  1868. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1869. l=add_str(label);
  1870. set_label(l,script_pos,p);
  1871. if(syntax.curly[pos].flag == 1) {
  1872. // Because the position of the pointer is the same if not else for this
  1873. return bp;
  1874. }
  1875. return p;
  1876. } else if(syntax.curly[pos].type == TYPE_DO) {
  1877. int l2;
  1878. char label2[256];
  1879. const char *p2;
  1880. if(syntax.curly[pos].flag) {
  1881. // (Come here continue) to form the label here
  1882. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1883. l2=add_str(label2);
  1884. set_label(l2,script_pos,p);
  1885. }
  1886. // Skip to the end point if the condition is false
  1887. p = skip_space(p);
  1888. p2 = skip_word(p);
  1889. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1890. disp_error_message("parse_syntax: expected 'while'",p);
  1891. p = skip_space(p2);
  1892. if(*p != '(') {
  1893. disp_error_message("need '('",p);
  1894. }
  1895. // do-block end is a new line
  1896. parse_nextline(false, p);
  1897. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1898. add_scriptl(add_str("jump_zero"));
  1899. add_scriptc(C_ARG);
  1900. p=parse_expr(p);
  1901. p=skip_space(p);
  1902. add_scriptl(add_str(label2));
  1903. add_scriptc(C_FUNC);
  1904. // Skip to the starting point
  1905. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1906. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1907. parse_line(label2);
  1908. syntax.curly_count--;
  1909. // Form label of the end point conditions
  1910. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1911. l2=add_str(label2);
  1912. set_label(l2,script_pos,p);
  1913. p = skip_space(p);
  1914. if(*p != ';') {
  1915. disp_error_message("parse_syntax: expected ';'",p);
  1916. return p+1;
  1917. }
  1918. p++;
  1919. syntax.curly_count--;
  1920. return p;
  1921. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1922. // for-block end is a new line
  1923. parse_nextline(false, p);
  1924. // Skip to the next loop
  1925. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1926. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1927. parse_line(label);
  1928. syntax.curly_count--;
  1929. // End for labeling
  1930. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1931. l=add_str(label);
  1932. set_label(l,script_pos,p);
  1933. syntax.curly_count--;
  1934. return p;
  1935. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1936. // while-block end is a new line
  1937. parse_nextline(false, p);
  1938. // Skip to the decision while
  1939. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1940. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1941. parse_line(label);
  1942. syntax.curly_count--;
  1943. // End while labeling
  1944. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1945. l=add_str(label);
  1946. set_label(l,script_pos,p);
  1947. syntax.curly_count--;
  1948. return p;
  1949. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1950. int pos2 = syntax.curly_count-1;
  1951. char label2[256];
  1952. int l2;
  1953. // Back
  1954. sprintf(label2,"return;");
  1955. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1956. parse_line(label2);
  1957. syntax.curly_count--;
  1958. // Put the label of the location
  1959. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1960. l2=add_str(label2);
  1961. set_label(l2,script_pos,p);
  1962. syntax.curly_count--;
  1963. return p;
  1964. } else {
  1965. *flag = 0;
  1966. return p;
  1967. }
  1968. }
  1969. /*==========================================
  1970. * Added built-in functions
  1971. *------------------------------------------*/
  1972. static void add_buildin_func(void)
  1973. {
  1974. int i;
  1975. for( i = 0; buildin_func[i].func; i++ )
  1976. {
  1977. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1978. // 'v' - value (either string or int or reference)
  1979. // 's' - string
  1980. // 'i' - int
  1981. // 'r' - reference (of a variable)
  1982. // 'l' - label
  1983. // '?' - one optional parameter
  1984. // '*' - unknown number of optional parameters
  1985. const char* p = buildin_func[i].arg;
  1986. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1987. while( *p == '?' ) ++p;
  1988. if( *p == '*' ) ++p;
  1989. if( *p != 0){
  1990. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1991. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1992. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1993. } else {
  1994. int n = add_str(buildin_func[i].name);
  1995. str_data[n].type = C_FUNC;
  1996. str_data[n].val = i;
  1997. str_data[n].func = buildin_func[i].func;
  1998. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1999. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  2000. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  2001. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  2002. }
  2003. }
  2004. }
  2005. /// Retrieves the value of a constant.
  2006. bool script_get_constant(const char* name, int* value)
  2007. {
  2008. int n = search_str(name);
  2009. if( n == -1 || str_data[n].type != C_INT )
  2010. {// not found or not a constant
  2011. return false;
  2012. }
  2013. value[0] = str_data[n].val;
  2014. return true;
  2015. }
  2016. /// Creates new constant or parameter with given value.
  2017. void script_set_constant(const char* name, int value, bool isparameter)
  2018. {
  2019. int n = add_str(name);
  2020. if( str_data[n].type == C_NOP )
  2021. {// new
  2022. str_data[n].type = isparameter ? C_PARAM : C_INT;
  2023. str_data[n].val = value;
  2024. }
  2025. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  2026. {// existing parameter or constant
  2027. 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);
  2028. }
  2029. else
  2030. {// existing name
  2031. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  2032. }
  2033. }
  2034. /*==========================================
  2035. * Reading constant databases
  2036. * const.txt
  2037. *------------------------------------------*/
  2038. static void read_constdb(void)
  2039. {
  2040. FILE *fp;
  2041. char line[1024],name[1024],val[1024];
  2042. int type;
  2043. int entries=0, skipped=0, linenum=0;
  2044. sprintf(line, "%s/const.txt", db_path);
  2045. fp=fopen(line, "r");
  2046. if(fp==NULL){
  2047. ShowError("can't read %s\n", line);
  2048. return ;
  2049. }
  2050. while(fgets(line, sizeof(line), fp))
  2051. {
  2052. linenum++;
  2053. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  2054. continue;
  2055. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2056. continue;
  2057. type=0;
  2058. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%d",name,val,&type)>=2 ||
  2059. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %d",name,val,&type)>=2){
  2060. entries++;
  2061. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2062. }
  2063. else {
  2064. skipped++;
  2065. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2066. }
  2067. }
  2068. fclose(fp);
  2069. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2070. if(skipped){
  2071. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2072. }
  2073. }
  2074. /**
  2075. * Sets source-end constants for NPC scripts to access.
  2076. **/
  2077. void script_hardcoded_constants(void) {
  2078. /* server defines */
  2079. script_set_constant("PACKETVER",PACKETVER,false);
  2080. script_set_constant("MAX_LEVEL",MAX_LEVEL,false);
  2081. script_set_constant("MAX_STORAGE",MAX_STORAGE,false);
  2082. script_set_constant("MAX_INVENTORY",MAX_INVENTORY,false);
  2083. script_set_constant("MAX_CART",MAX_INVENTORY,false);
  2084. script_set_constant("MAX_ZENY",MAX_ZENY,false);
  2085. script_set_constant("MAX_PARTY",MAX_PARTY,false);
  2086. script_set_constant("MAX_GUILD",MAX_GUILD,false);
  2087. script_set_constant("MAX_GUILDLEVEL",MAX_GUILDLEVEL,false);
  2088. script_set_constant("MAX_GUILD_STORAGE",MAX_GUILD_STORAGE,false);
  2089. script_set_constant("MAX_BG_MEMBERS",MAX_BG_MEMBERS,false);
  2090. script_set_constant("MAX_CHAT_USERS",MAX_CHAT_USERS,false);
  2091. script_set_constant("VIP_SCRIPT",VIP_SCRIPT,false);
  2092. script_set_constant("MIN_STORAGE",MIN_STORAGE,false);
  2093. /* status options */
  2094. script_set_constant("Option_Nothing",OPTION_NOTHING,false);
  2095. script_set_constant("Option_Sight",OPTION_SIGHT,false);
  2096. script_set_constant("Option_Hide",OPTION_HIDE,false);
  2097. script_set_constant("Option_Cloak",OPTION_CLOAK,false);
  2098. script_set_constant("Option_Falcon",OPTION_FALCON,false);
  2099. script_set_constant("Option_Riding",OPTION_RIDING,false);
  2100. script_set_constant("Option_Invisible",OPTION_INVISIBLE,false);
  2101. script_set_constant("Option_Orcish",OPTION_ORCISH,false);
  2102. script_set_constant("Option_Wedding",OPTION_WEDDING,false);
  2103. script_set_constant("Option_Ruwach",OPTION_RUWACH,false);
  2104. script_set_constant("Option_Chasewalk",OPTION_CHASEWALK,false);
  2105. script_set_constant("Option_Flying",OPTION_FLYING,false);
  2106. script_set_constant("Option_Xmas",OPTION_XMAS,false);
  2107. script_set_constant("Option_Transform",OPTION_TRANSFORM,false);
  2108. script_set_constant("Option_Summer",OPTION_SUMMER,false);
  2109. script_set_constant("Option_Dragon1",OPTION_DRAGON1,false);
  2110. script_set_constant("Option_Wug",OPTION_WUG,false);
  2111. script_set_constant("Option_Wugrider",OPTION_WUGRIDER,false);
  2112. script_set_constant("Option_Madogear",OPTION_MADOGEAR,false);
  2113. script_set_constant("Option_Dragon2",OPTION_DRAGON2,false);
  2114. script_set_constant("Option_Dragon3",OPTION_DRAGON3,false);
  2115. script_set_constant("Option_Dragon4",OPTION_DRAGON4,false);
  2116. script_set_constant("Option_Dragon5",OPTION_DRAGON5,false);
  2117. script_set_constant("Option_Hanbok",OPTION_HANBOK,false);
  2118. script_set_constant("Option_Oktoberfest",OPTION_OKTOBERFEST,false);
  2119. /* status option compounds */
  2120. script_set_constant("Option_Dragon",OPTION_DRAGON,false);
  2121. script_set_constant("Option_Costume",OPTION_COSTUME,false);
  2122. /* sc_start flags */
  2123. script_set_constant("SCSTART_NONE",SCSTART_NONE,false);
  2124. script_set_constant("SCSTART_NOAVOID",SCSTART_NOAVOID,false);
  2125. script_set_constant("SCSTART_NOTICKDEF",SCSTART_NOTICKDEF,false);
  2126. script_set_constant("SCSTART_LOADED",SCSTART_LOADED,false);
  2127. script_set_constant("SCSTART_NORATEDEF",SCSTART_NORATEDEF,false);
  2128. script_set_constant("SCSTART_NOICON",SCSTART_NOICON,false);
  2129. }
  2130. /*==========================================
  2131. * Display emplacement line of script
  2132. *------------------------------------------*/
  2133. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2134. {
  2135. int i;
  2136. if( p == NULL || !p[0] ) return NULL;
  2137. if( line < 0 )
  2138. StringBuf_Printf(buf, "*% 5d : ", -line);
  2139. else
  2140. StringBuf_Printf(buf, " % 5d : ", line);
  2141. for(i=0;p[i] && p[i] != '\n';i++){
  2142. if(p + i != mark)
  2143. StringBuf_Printf(buf, "%c", p[i]);
  2144. else
  2145. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2146. }
  2147. StringBuf_AppendStr(buf, "\n");
  2148. return p+i+(p[i] == '\n' ? 1 : 0);
  2149. }
  2150. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2151. // Find the line where the error occurred
  2152. int j;
  2153. int line = start_line;
  2154. const char *p;
  2155. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2156. for(p=src;p && *p;line++){
  2157. const char *lineend=strchr(p,'\n');
  2158. if(lineend==NULL || error_pos_cur<lineend){
  2159. break;
  2160. }
  2161. for( j = 0; j < 4; j++ ) {
  2162. linestart[j] = linestart[j+1];
  2163. }
  2164. linestart[4] = p;
  2165. p=lineend+1;
  2166. }
  2167. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2168. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2169. for(j = 0; j < 5; j++ ) {
  2170. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2171. }
  2172. p = script_print_line(buf, p, error_pos_cur, -line);
  2173. for(j = 0; j < 5; j++) {
  2174. p = script_print_line(buf, p, NULL, line + j + 1 );
  2175. }
  2176. }
  2177. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2178. StringBuf buf;
  2179. StringBuf_Init(&buf);
  2180. StringBuf_AppendStr(&buf, "\a\n");
  2181. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2182. ShowError("%s", StringBuf_Value(&buf));
  2183. StringBuf_Destroy(&buf);
  2184. }
  2185. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2186. StringBuf buf;
  2187. StringBuf_Init(&buf);
  2188. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2189. ShowWarning("%s", StringBuf_Value(&buf));
  2190. StringBuf_Destroy(&buf);
  2191. }
  2192. /*==========================================
  2193. * Analysis of the script
  2194. *------------------------------------------*/
  2195. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2196. {
  2197. const char *p,*tmpp;
  2198. int i;
  2199. struct script_code* code = NULL;
  2200. static int first=1;
  2201. char end;
  2202. bool unresolved_names = false;
  2203. parser_current_src = src;
  2204. parser_current_file = file;
  2205. parser_current_line = line;
  2206. if( src == NULL )
  2207. return NULL;// empty script
  2208. memset(&syntax,0,sizeof(syntax));
  2209. if(first){
  2210. add_buildin_func();
  2211. read_constdb();
  2212. script_hardcoded_constants();
  2213. first=0;
  2214. }
  2215. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2216. script_pos=0;
  2217. script_size=SCRIPT_BLOCK_SIZE;
  2218. parse_nextline(true, NULL);
  2219. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2220. if( options&SCRIPT_USE_LABEL_DB )
  2221. db_clear(scriptlabel_db);
  2222. parse_options = options;
  2223. if( setjmp( error_jump ) != 0 ) {
  2224. //Restore program state when script has problems. [from jA]
  2225. int j;
  2226. const int size = ARRAYLENGTH(syntax.curly);
  2227. if( error_report )
  2228. script_error(src,file,line,error_msg,error_pos);
  2229. aFree( error_msg );
  2230. aFree( script_buf );
  2231. script_pos = 0;
  2232. script_size = 0;
  2233. script_buf = NULL;
  2234. for(j=LABEL_START;j<str_num;j++)
  2235. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2236. for(j=0; j<size; j++)
  2237. linkdb_final(&syntax.curly[j].case_label);
  2238. return NULL;
  2239. }
  2240. parse_syntax_for_flag=0;
  2241. p=src;
  2242. p=skip_space(p);
  2243. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2244. {// does not require brackets around the script
  2245. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2246. {// empty script and can return NULL
  2247. aFree( script_buf );
  2248. script_pos = 0;
  2249. script_size = 0;
  2250. script_buf = NULL;
  2251. return NULL;
  2252. }
  2253. end = '\0';
  2254. }
  2255. else
  2256. {// requires brackets around the script
  2257. if( *p != '{' )
  2258. disp_error_message("not found '{'",p);
  2259. p = skip_space(p+1);
  2260. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2261. {// empty script and can return NULL
  2262. aFree( script_buf );
  2263. script_pos = 0;
  2264. script_size = 0;
  2265. script_buf = NULL;
  2266. return NULL;
  2267. }
  2268. end = '}';
  2269. }
  2270. // clear references of labels, variables and internal functions
  2271. for(i=LABEL_START;i<str_num;i++){
  2272. if(
  2273. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2274. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2275. ){
  2276. str_data[i].type=C_NOP;
  2277. str_data[i].backpatch=-1;
  2278. str_data[i].label=-1;
  2279. }
  2280. }
  2281. while( syntax.curly_count != 0 || *p != end )
  2282. {
  2283. if( *p == '\0' )
  2284. disp_error_message("unexpected end of script",p);
  2285. // Special handling only label
  2286. tmpp=skip_space(skip_word(p));
  2287. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2288. i=add_word(p);
  2289. set_label(i,script_pos,p);
  2290. if( parse_options&SCRIPT_USE_LABEL_DB )
  2291. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2292. p=tmpp+1;
  2293. p=skip_space(p);
  2294. continue;
  2295. }
  2296. // All other lumped
  2297. p=parse_line(p);
  2298. p=skip_space(p);
  2299. parse_nextline(false, p);
  2300. }
  2301. add_scriptc(C_NOP);
  2302. // trim code to size
  2303. script_size = script_pos;
  2304. RECREATE(script_buf,unsigned char,script_pos);
  2305. // default unknown references to variables
  2306. for(i=LABEL_START;i<str_num;i++){
  2307. if(str_data[i].type==C_NOP){
  2308. int j;
  2309. str_data[i].type=C_NAME;
  2310. str_data[i].label=i;
  2311. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2312. int next=GETVALUE(script_buf,j);
  2313. SETVALUE(script_buf,j,i);
  2314. j=next;
  2315. }
  2316. }
  2317. else if( str_data[i].type == C_USERFUNC )
  2318. {// 'function name;' without follow-up code
  2319. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2320. unresolved_names = true;
  2321. }
  2322. }
  2323. if( unresolved_names )
  2324. {
  2325. disp_error_message("parse_script: unresolved function references", p);
  2326. }
  2327. #ifdef DEBUG_DISP
  2328. for(i=0;i<script_pos;i++){
  2329. if((i&15)==0) ShowMessage("%04x : ",i);
  2330. ShowMessage("%02x ",script_buf[i]);
  2331. if((i&15)==15) ShowMessage("\n");
  2332. }
  2333. ShowMessage("\n");
  2334. #endif
  2335. #ifdef DEBUG_DISASM
  2336. {
  2337. int i = 0,j;
  2338. while(i < script_pos) {
  2339. c_op op = get_com(script_buf,&i);
  2340. ShowMessage("%06x %s", i, script_op2name(op));
  2341. j = i;
  2342. switch(op) {
  2343. case C_INT:
  2344. ShowMessage(" %d", get_num(script_buf,&i));
  2345. break;
  2346. case C_POS:
  2347. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2348. i += 3;
  2349. break;
  2350. case C_NAME:
  2351. j = (*(int*)(script_buf+i)&0xffffff);
  2352. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2353. i += 3;
  2354. break;
  2355. case C_STR:
  2356. j = strlen(script_buf + i);
  2357. ShowMessage(" %s", script_buf + i);
  2358. i += j+1;
  2359. break;
  2360. }
  2361. ShowMessage(CL_CLL"\n");
  2362. }
  2363. }
  2364. #endif
  2365. CREATE(code,struct script_code,1);
  2366. code->script_buf = script_buf;
  2367. code->script_size = script_size;
  2368. code->script_vars = idb_alloc(DB_OPT_RELEASE_DATA);
  2369. return code;
  2370. }
  2371. /// Returns the player attached to this script, identified by the rid.
  2372. /// If there is no player attached, the script is terminated.
  2373. TBL_PC *script_rid2sd(struct script_state *st)
  2374. {
  2375. TBL_PC *sd=map_id2sd(st->rid);
  2376. if(!sd){
  2377. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2378. script_reportfunc(st);
  2379. script_reportsrc(st);
  2380. st->state = END;
  2381. }
  2382. return sd;
  2383. }
  2384. /// Dereferences a variable/constant, replacing it with a copy of the value.
  2385. ///
  2386. /// @param st Script state
  2387. /// @param data Variable/constant
  2388. void get_val(struct script_state* st, struct script_data* data)
  2389. {
  2390. const char* name;
  2391. char prefix;
  2392. char postfix;
  2393. TBL_PC* sd = NULL;
  2394. if( !data_isreference(data) )
  2395. return;// not a variable/constant
  2396. name = reference_getname(data);
  2397. prefix = name[0];
  2398. postfix = name[strlen(name) - 1];
  2399. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2400. if( !reference_toconstant(data) && not_server_variable(prefix) )
  2401. {
  2402. sd = script_rid2sd(st);
  2403. if( sd == NULL )
  2404. {// needs player attached
  2405. if( postfix == '$' )
  2406. {// string variable
  2407. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2408. data->type = C_CONSTSTR;
  2409. data->u.str = "";
  2410. }
  2411. else
  2412. {// integer variable
  2413. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2414. data->type = C_INT;
  2415. data->u.num = 0;
  2416. }
  2417. return;
  2418. }
  2419. }
  2420. if( postfix == '$' )
  2421. {// string variable
  2422. switch( prefix )
  2423. {
  2424. case '@':
  2425. data->u.str = pc_readregstr(sd, data->u.num);
  2426. break;
  2427. case '$':
  2428. data->u.str = mapreg_readregstr(data->u.num);
  2429. break;
  2430. case '#':
  2431. if( name[1] == '#' )
  2432. data->u.str = pc_readaccountreg2str(sd, name);// global
  2433. else
  2434. data->u.str = pc_readaccountregstr(sd, name);// local
  2435. break;
  2436. case '.':
  2437. {
  2438. struct DBMap* n =
  2439. data->ref ? *data->ref:
  2440. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2441. st->script->script_vars;// npc variable
  2442. if( n )
  2443. data->u.str = (char*)idb_get(n,reference_getuid(data));
  2444. else
  2445. data->u.str = NULL;
  2446. }
  2447. break;
  2448. case '\'':
  2449. {
  2450. int instance_id = script_instancegetid(st);
  2451. if( instance_id )
  2452. data->u.str = (char*)idb_get(instance_data[instance_id].vars,reference_getuid(data));
  2453. else {
  2454. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2455. data->u.str = NULL;
  2456. }
  2457. }
  2458. break;
  2459. default:
  2460. data->u.str = pc_readglobalreg_str(sd, name);
  2461. break;
  2462. }
  2463. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2464. {// empty string
  2465. data->type = C_CONSTSTR;
  2466. data->u.str = "";
  2467. }
  2468. else
  2469. {// duplicate string
  2470. data->type = C_STR;
  2471. data->u.str = aStrdup(data->u.str);
  2472. }
  2473. }
  2474. else
  2475. {// integer variable
  2476. data->type = C_INT;
  2477. if( reference_toconstant(data) )
  2478. {
  2479. data->u.num = reference_getconstant(data);
  2480. }
  2481. else if( reference_toparam(data) )
  2482. {
  2483. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2484. }
  2485. else
  2486. switch( prefix )
  2487. {
  2488. case '@':
  2489. data->u.num = pc_readreg(sd, data->u.num);
  2490. break;
  2491. case '$':
  2492. data->u.num = mapreg_readreg(data->u.num);
  2493. break;
  2494. case '#':
  2495. if( name[1] == '#' )
  2496. data->u.num = pc_readaccountreg2(sd, name);// global
  2497. else
  2498. data->u.num = pc_readaccountreg(sd, name);// local
  2499. break;
  2500. case '.':
  2501. {
  2502. struct DBMap* n =
  2503. data->ref ? *data->ref:
  2504. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2505. st->script->script_vars;// npc variable
  2506. if( n )
  2507. data->u.num = (int)idb_iget(n,reference_getuid(data));
  2508. else
  2509. data->u.num = 0;
  2510. }
  2511. break;
  2512. case '\'':
  2513. {
  2514. int instance_id = script_instancegetid(st);
  2515. if( instance_id )
  2516. data->u.num = (int)idb_iget(instance_data[instance_id].vars,reference_getuid(data));
  2517. else {
  2518. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2519. data->u.num = 0;
  2520. }
  2521. }
  2522. break;
  2523. default:
  2524. data->u.num = pc_readglobalreg(sd, name);
  2525. break;
  2526. }
  2527. }
  2528. return;
  2529. }
  2530. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
  2531. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2532. /// The value is left in the top of the stack and needs to be removed manually.
  2533. void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
  2534. {
  2535. struct script_data* data;
  2536. push_val2(st->stack, C_NAME, uid, ref);
  2537. data = script_getdatatop(st, -1);
  2538. get_val(st, data);
  2539. return (data->type == C_INT ? (void*)__64BPRTSIZE(data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2540. }
  2541. /*==========================================
  2542. * Stores the value of a script variable
  2543. * Return value is 0 on fail, 1 on success.
  2544. *------------------------------------------*/
  2545. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
  2546. {
  2547. char prefix = name[0];
  2548. if( is_string_variable(name) ) {// string variable
  2549. const char* str = (const char*)value;
  2550. switch (prefix) {
  2551. case '@':
  2552. return pc_setregstr(sd, num, str);
  2553. case '$':
  2554. return mapreg_setregstr(num, str);
  2555. case '#':
  2556. return (name[1] == '#') ?
  2557. pc_setaccountreg2str(sd, name, str) :
  2558. pc_setaccountregstr(sd, name, str);
  2559. case '.':
  2560. {
  2561. struct DBMap* n;
  2562. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2563. if( n ) {
  2564. idb_remove(n, num);
  2565. if (str[0]) idb_put(n, num, aStrdup(str));
  2566. }
  2567. }
  2568. return 1;
  2569. case '\'':
  2570. {
  2571. int instance_id = script_instancegetid(st);
  2572. if( instance_id ) {
  2573. idb_remove(instance_data[instance_id].vars, num);
  2574. if( str[0] ) idb_put(instance_data[instance_id].vars, num, aStrdup(str));
  2575. }
  2576. return 1;
  2577. }
  2578. default:
  2579. return pc_setglobalreg_str(sd, name, str);
  2580. }
  2581. } else {// integer variable
  2582. int val = (int)__64BPRTSIZE(value);
  2583. if(str_data[num&0x00ffffff].type == C_PARAM)
  2584. {
  2585. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2586. {
  2587. if( st != NULL )
  2588. {
  2589. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2590. script_reportsrc(st);
  2591. st->state = END;
  2592. }
  2593. return 0;
  2594. }
  2595. return 1;
  2596. }
  2597. switch (prefix) {
  2598. case '@':
  2599. return pc_setreg(sd, num, val);
  2600. case '$':
  2601. return mapreg_setreg(num, val);
  2602. case '#':
  2603. return (name[1] == '#') ?
  2604. pc_setaccountreg2(sd, name, val) :
  2605. pc_setaccountreg(sd, name, val);
  2606. case '.':
  2607. {
  2608. struct DBMap* n;
  2609. n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
  2610. if( n ) {
  2611. idb_remove(n, num);
  2612. if( val != 0 )
  2613. idb_iput(n, num, val);
  2614. }
  2615. }
  2616. return 1;
  2617. case '\'':
  2618. {
  2619. int instance_id = script_instancegetid(st);
  2620. if( instance_id ) {
  2621. idb_remove(instance_data[instance_id].vars, num);
  2622. if( val != 0 )
  2623. idb_iput(instance_data[instance_id].vars, num, val);
  2624. }
  2625. return 1;
  2626. }
  2627. default:
  2628. return pc_setglobalreg(sd, name, val);
  2629. }
  2630. }
  2631. }
  2632. int set_var(TBL_PC* sd, char* name, void* val)
  2633. {
  2634. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2635. }
  2636. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
  2637. {
  2638. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2639. }
  2640. /// Converts the data to a string
  2641. const char* conv_str(struct script_state* st, struct script_data* data)
  2642. {
  2643. char* p;
  2644. get_val(st, data);
  2645. if( data_isstring(data) )
  2646. {// nothing to convert
  2647. }
  2648. else if( data_isint(data) )
  2649. {// int -> string
  2650. CREATE(p, char, ITEM_NAME_LENGTH);
  2651. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2652. p[ITEM_NAME_LENGTH-1] = '\0';
  2653. data->type = C_STR;
  2654. data->u.str = p;
  2655. }
  2656. else if( data_isreference(data) )
  2657. {// reference -> string
  2658. //##TODO when does this happen (check get_val) [FlavioJS]
  2659. data->type = C_CONSTSTR;
  2660. data->u.str = reference_getname(data);
  2661. }
  2662. else
  2663. {// unsupported data type
  2664. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2665. script_reportdata(data);
  2666. script_reportsrc(st);
  2667. data->type = C_CONSTSTR;
  2668. data->u.str = "";
  2669. }
  2670. return data->u.str;
  2671. }
  2672. /// Converts the data to an int
  2673. int conv_num(struct script_state* st, struct script_data* data)
  2674. {
  2675. get_val(st, data);
  2676. if( data_isint(data) )
  2677. {// nothing to convert
  2678. }
  2679. else if( data_isstring(data) )
  2680. {// string -> int
  2681. // the result does not overflow or underflow, it is capped instead
  2682. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2683. char* p = data->u.str;
  2684. long num;
  2685. errno = 0;
  2686. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2687. if( errno == ERANGE
  2688. #if LONG_MAX > INT_MAX
  2689. || num < INT_MIN || num > INT_MAX
  2690. #endif
  2691. )
  2692. {
  2693. if( num <= INT_MIN )
  2694. {
  2695. num = INT_MIN;
  2696. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2697. }
  2698. else//if( num >= INT_MAX )
  2699. {
  2700. num = INT_MAX;
  2701. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2702. }
  2703. script_reportdata(data);
  2704. script_reportsrc(st);
  2705. }
  2706. if( data->type == C_STR )
  2707. aFree(p);
  2708. data->type = C_INT;
  2709. data->u.num = (int)num;
  2710. }
  2711. #if 0
  2712. // FIXME this function is being used to retrieve the position of labels and
  2713. // probably other stuff [FlavioJS]
  2714. else
  2715. {// unsupported data type
  2716. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2717. script_reportdata(data);
  2718. script_reportsrc(st);
  2719. data->type = C_INT;
  2720. data->u.num = 0;
  2721. }
  2722. #endif
  2723. return data->u.num;
  2724. }
  2725. //
  2726. // Stack operations
  2727. //
  2728. /// Increases the size of the stack
  2729. void stack_expand(struct script_stack* stack)
  2730. {
  2731. stack->sp_max += 64;
  2732. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2733. stack->sp_max * sizeof(stack->stack_data[0]) );
  2734. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2735. 64 * sizeof(stack->stack_data[0]) );
  2736. }
  2737. /// Pushes a value into the stack
  2738. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2739. /// Pushes a value into the stack (with reference)
  2740. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
  2741. {
  2742. if( stack->sp >= stack->sp_max )
  2743. stack_expand(stack);
  2744. stack->stack_data[stack->sp].type = type;
  2745. stack->stack_data[stack->sp].u.num = val;
  2746. stack->stack_data[stack->sp].ref = ref;
  2747. stack->sp++;
  2748. return &stack->stack_data[stack->sp-1];
  2749. }
  2750. /// Pushes a string into the stack
  2751. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2752. {
  2753. if( stack->sp >= stack->sp_max )
  2754. stack_expand(stack);
  2755. stack->stack_data[stack->sp].type = type;
  2756. stack->stack_data[stack->sp].u.str = str;
  2757. stack->stack_data[stack->sp].ref = NULL;
  2758. stack->sp++;
  2759. return &stack->stack_data[stack->sp-1];
  2760. }
  2761. /// Pushes a retinfo into the stack
  2762. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, DBMap **ref)
  2763. {
  2764. if( stack->sp >= stack->sp_max )
  2765. stack_expand(stack);
  2766. stack->stack_data[stack->sp].type = C_RETINFO;
  2767. stack->stack_data[stack->sp].u.ri = ri;
  2768. stack->stack_data[stack->sp].ref = ref;
  2769. stack->sp++;
  2770. return &stack->stack_data[stack->sp-1];
  2771. }
  2772. /// Pushes a copy of the target position into the stack
  2773. struct script_data* push_copy(struct script_stack* stack, int pos)
  2774. {
  2775. switch( stack->stack_data[pos].type )
  2776. {
  2777. case C_CONSTSTR:
  2778. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2779. break;
  2780. case C_STR:
  2781. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2782. break;
  2783. case C_RETINFO:
  2784. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2785. exit(1);
  2786. break;
  2787. default:
  2788. return push_val2(
  2789. stack,stack->stack_data[pos].type,
  2790. stack->stack_data[pos].u.num,
  2791. stack->stack_data[pos].ref
  2792. );
  2793. break;
  2794. }
  2795. }
  2796. /// Removes the values in indexes [start,end] from the stack.
  2797. /// Adjusts all stack pointers.
  2798. void pop_stack(struct script_state* st, int start, int end)
  2799. {
  2800. struct script_stack* stack = st->stack;
  2801. struct script_data* data;
  2802. int i;
  2803. if( start < 0 )
  2804. start = 0;
  2805. if( end > stack->sp )
  2806. end = stack->sp;
  2807. if( start >= end )
  2808. return;// nothing to pop
  2809. // free stack elements
  2810. for( i = start; i < end; i++ )
  2811. {
  2812. data = &stack->stack_data[i];
  2813. if( data->type == C_STR )
  2814. aFree(data->u.str);
  2815. if( data->type == C_RETINFO )
  2816. {
  2817. struct script_retinfo* ri = data->u.ri;
  2818. if( ri->var_function )
  2819. script_free_vars(ri->var_function);
  2820. if( data->ref )
  2821. aFree(data->ref);
  2822. aFree(ri);
  2823. }
  2824. data->type = C_NOP;
  2825. }
  2826. // move the rest of the elements
  2827. if( stack->sp > end )
  2828. {
  2829. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2830. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2831. stack->stack_data[i].type = C_NOP;
  2832. }
  2833. // adjust stack pointers
  2834. if( st->start > end ) st->start -= end - start;
  2835. else if( st->start > start ) st->start = start;
  2836. if( st->end > end ) st->end -= end - start;
  2837. else if( st->end > start ) st->end = start;
  2838. if( stack->defsp > end ) stack->defsp -= end - start;
  2839. else if( stack->defsp > start ) stack->defsp = start;
  2840. stack->sp -= end - start;
  2841. }
  2842. ///
  2843. ///
  2844. ///
  2845. /*==========================================
  2846. * Release script dependent variable, dependent variable of function
  2847. *------------------------------------------*/
  2848. void script_free_vars(struct DBMap* storage)
  2849. {
  2850. if( storage )
  2851. {// destroy the storage construct containing the variables
  2852. db_destroy(storage);
  2853. }
  2854. }
  2855. void script_free_code(struct script_code* code)
  2856. {
  2857. nullpo_retv(code);
  2858. script_free_vars( code->script_vars );
  2859. aFree( code->script_buf );
  2860. aFree( code );
  2861. }
  2862. /// Creates a new script state.
  2863. ///
  2864. /// @param script Script code
  2865. /// @param pos Position in the code
  2866. /// @param rid Who is running the script (attached player)
  2867. /// @param oid Where the code is being run (npc 'object')
  2868. /// @return Script state
  2869. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2870. {
  2871. struct script_state* st;
  2872. CREATE(st, struct script_state, 1);
  2873. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2874. st->stack->sp = 0;
  2875. st->stack->sp_max = 64;
  2876. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2877. st->stack->defsp = st->stack->sp;
  2878. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  2879. st->state = RUN;
  2880. st->script = script;
  2881. //st->scriptroot = script;
  2882. st->pos = pos;
  2883. st->rid = rid;
  2884. st->oid = oid;
  2885. st->sleep.timer = INVALID_TIMER;
  2886. st->npc_item_flag = battle_config.item_enabled_npc;
  2887. return st;
  2888. }
  2889. /// Frees a script state.
  2890. ///
  2891. /// @param st Script state
  2892. void script_free_state(struct script_state* st)
  2893. {
  2894. if(st->bk_st)
  2895. {// backup was not restored
  2896. 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);
  2897. }
  2898. if( st->sleep.timer != INVALID_TIMER )
  2899. delete_timer(st->sleep.timer, run_script_timer);
  2900. script_free_vars(st->stack->var_function);
  2901. pop_stack(st, 0, st->stack->sp);
  2902. aFree(st->stack->stack_data);
  2903. aFree(st->stack);
  2904. st->stack = NULL;
  2905. st->pos = -1;
  2906. aFree(st);
  2907. }
  2908. //
  2909. // Main execution unit
  2910. //
  2911. /*==========================================
  2912. * Read command
  2913. *------------------------------------------*/
  2914. c_op get_com(unsigned char *script,int *pos)
  2915. {
  2916. int i = 0, j = 0;
  2917. if(script[*pos]>=0x80){
  2918. return C_INT;
  2919. }
  2920. while(script[*pos]>=0x40){
  2921. i=script[(*pos)++]<<j;
  2922. j+=6;
  2923. }
  2924. return (c_op)(i+(script[(*pos)++]<<j));
  2925. }
  2926. /*==========================================
  2927. * Income figures
  2928. *------------------------------------------*/
  2929. int get_num(unsigned char *script,int *pos)
  2930. {
  2931. int i,j;
  2932. i=0; j=0;
  2933. while(script[*pos]>=0xc0){
  2934. i+=(script[(*pos)++]&0x7f)<<j;
  2935. j+=6;
  2936. }
  2937. return i+((script[(*pos)++]&0x7f)<<j);
  2938. }
  2939. /*==========================================
  2940. * Remove the value from the stack
  2941. *------------------------------------------*/
  2942. int pop_val(struct script_state* st)
  2943. {
  2944. if(st->stack->sp<=0)
  2945. return 0;
  2946. st->stack->sp--;
  2947. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2948. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2949. return st->stack->stack_data[st->stack->sp].u.num;
  2950. return 0;
  2951. }
  2952. /// Ternary operators
  2953. /// test ? if_true : if_false
  2954. void op_3(struct script_state* st, int op)
  2955. {
  2956. struct script_data* data;
  2957. int flag = 0;
  2958. data = script_getdatatop(st, -3);
  2959. get_val(st, data);
  2960. if( data_isstring(data) )
  2961. flag = data->u.str[0];// "" -> false
  2962. else if( data_isint(data) )
  2963. flag = data->u.num;// 0 -> false
  2964. else
  2965. {
  2966. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2967. script_reportdata(data);
  2968. script_reportsrc(st);
  2969. script_removetop(st, -3, 0);
  2970. script_pushnil(st);
  2971. return;
  2972. }
  2973. if( flag )
  2974. script_pushcopytop(st, -2);
  2975. else
  2976. script_pushcopytop(st, -1);
  2977. script_removetop(st, -4, -1);
  2978. }
  2979. /// Binary string operators
  2980. /// s1 EQ s2 -> i
  2981. /// s1 NE s2 -> i
  2982. /// s1 GT s2 -> i
  2983. /// s1 GE s2 -> i
  2984. /// s1 LT s2 -> i
  2985. /// s1 LE s2 -> i
  2986. /// s1 ADD s2 -> s
  2987. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2988. {
  2989. int a = 0;
  2990. switch(op){
  2991. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2992. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2993. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2994. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2995. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2996. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2997. case C_ADD:
  2998. {
  2999. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3000. strcpy(buf, s1);
  3001. strcat(buf, s2);
  3002. script_pushstr(st, buf);
  3003. return;
  3004. }
  3005. default:
  3006. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3007. script_reportsrc(st);
  3008. script_pushnil(st);
  3009. st->state = END;
  3010. return;
  3011. }
  3012. script_pushint(st,a);
  3013. }
  3014. /// Binary number operators
  3015. /// i OP i -> i
  3016. void op_2num(struct script_state* st, int op, int i1, int i2)
  3017. {
  3018. int ret;
  3019. double ret_double;
  3020. switch( op )
  3021. {
  3022. case C_AND: ret = i1 & i2; break;
  3023. case C_OR: ret = i1 | i2; break;
  3024. case C_XOR: ret = i1 ^ i2; break;
  3025. case C_LAND: ret = (i1 && i2); break;
  3026. case C_LOR: ret = (i1 || i2); break;
  3027. case C_EQ: ret = (i1 == i2); break;
  3028. case C_NE: ret = (i1 != i2); break;
  3029. case C_GT: ret = (i1 > i2); break;
  3030. case C_GE: ret = (i1 >= i2); break;
  3031. case C_LT: ret = (i1 < i2); break;
  3032. case C_LE: ret = (i1 <= i2); break;
  3033. case C_R_SHIFT: ret = i1>>i2; break;
  3034. case C_L_SHIFT: ret = i1<<i2; break;
  3035. case C_DIV:
  3036. case C_MOD:
  3037. if( i2 == 0 )
  3038. {
  3039. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3040. script_reportsrc(st);
  3041. script_pushnil(st);
  3042. st->state = END;
  3043. return;
  3044. }
  3045. else if( op == C_DIV )
  3046. ret = i1 / i2;
  3047. else//if( op == C_MOD )
  3048. ret = i1 % i2;
  3049. break;
  3050. default:
  3051. switch( op )
  3052. {// operators that can overflow/underflow
  3053. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3054. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3055. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3056. default:
  3057. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3058. script_reportsrc(st);
  3059. script_pushnil(st);
  3060. return;
  3061. }
  3062. if( ret_double < (double)INT_MIN )
  3063. {
  3064. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3065. script_reportsrc(st);
  3066. ret = INT_MIN;
  3067. }
  3068. else if( ret_double > (double)INT_MAX )
  3069. {
  3070. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3071. script_reportsrc(st);
  3072. ret = INT_MAX;
  3073. }
  3074. }
  3075. script_pushint(st, ret);
  3076. }
  3077. /// Binary operators
  3078. void op_2(struct script_state *st, int op)
  3079. {
  3080. struct script_data* left, leftref;
  3081. struct script_data* right;
  3082. leftref.type = C_NOP;
  3083. left = script_getdatatop(st, -2);
  3084. right = script_getdatatop(st, -1);
  3085. if (st->op2ref) {
  3086. if (data_isreference(left)) {
  3087. leftref = *left;
  3088. }
  3089. st->op2ref = 0;
  3090. }
  3091. get_val(st, left);
  3092. get_val(st, right);
  3093. // automatic conversions
  3094. switch( op )
  3095. {
  3096. case C_ADD:
  3097. if( data_isint(left) && data_isstring(right) )
  3098. {// convert int-string to string-string
  3099. conv_str(st, left);
  3100. }
  3101. else if( data_isstring(left) && data_isint(right) )
  3102. {// convert string-int to string-string
  3103. conv_str(st, right);
  3104. }
  3105. break;
  3106. }
  3107. if( data_isstring(left) && data_isstring(right) )
  3108. {// ss => op_2str
  3109. op_2str(st, op, left->u.str, right->u.str);
  3110. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3111. if (leftref.type != C_NOP)
  3112. {
  3113. if (left->type == C_STR) // don't free C_CONSTSTR
  3114. aFree(left->u.str);
  3115. *left = leftref;
  3116. }
  3117. }
  3118. else if( data_isint(left) && data_isint(right) )
  3119. {// ii => op_2num
  3120. int i1 = left->u.num;
  3121. int i2 = right->u.num;
  3122. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3123. op_2num(st, op, i1, i2);
  3124. if (leftref.type != C_NOP)
  3125. *left = leftref;
  3126. }
  3127. else
  3128. {// invalid argument
  3129. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3130. script_reportdata(left);
  3131. script_reportdata(right);
  3132. script_reportsrc(st);
  3133. script_removetop(st, -2, 0);
  3134. script_pushnil(st);
  3135. st->state = END;
  3136. }
  3137. }
  3138. /// Unary operators
  3139. /// NEG i -> i
  3140. /// NOT i -> i
  3141. /// LNOT i -> i
  3142. void op_1(struct script_state* st, int op)
  3143. {
  3144. struct script_data* data;
  3145. int i1;
  3146. data = script_getdatatop(st, -1);
  3147. get_val(st, data);
  3148. if( !data_isint(data) )
  3149. {// not a number
  3150. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3151. script_reportdata(data);
  3152. script_reportsrc(st);
  3153. script_pushnil(st);
  3154. st->state = END;
  3155. return;
  3156. }
  3157. i1 = data->u.num;
  3158. script_removetop(st, -1, 0);
  3159. switch( op )
  3160. {
  3161. case C_NEG: i1 = -i1; break;
  3162. case C_NOT: i1 = ~i1; break;
  3163. case C_LNOT: i1 = !i1; break;
  3164. default:
  3165. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3166. script_reportsrc(st);
  3167. script_pushnil(st);
  3168. st->state = END;
  3169. return;
  3170. }
  3171. script_pushint(st, i1);
  3172. }
  3173. /// Checks the type of all arguments passed to a built-in function.
  3174. ///
  3175. /// @param st Script state whose stack arguments should be inspected.
  3176. /// @param func Built-in function for which the arguments are intended.
  3177. static void script_check_buildin_argtype(struct script_state* st, int func)
  3178. {
  3179. int idx, invalid = 0;
  3180. for( idx = 2; script_hasdata(st, idx); idx++ )
  3181. {
  3182. struct script_data* data = script_getdata(st, idx);
  3183. script_function* sf = &buildin_func[str_data[func].val];
  3184. char type = sf->arg[idx-2];
  3185. if( type == '?' || type == '*' )
  3186. {// optional argument or unknown number of optional parameters ( no types are after this )
  3187. break;
  3188. }
  3189. else if( type == 0 )
  3190. {// more arguments than necessary ( should not happen, as it is checked before )
  3191. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3192. invalid++;
  3193. break;
  3194. }
  3195. else
  3196. {
  3197. const char* name = NULL;
  3198. if( data_isreference(data) )
  3199. {// get name for variables to determine the type they refer to
  3200. name = reference_getname(data);
  3201. }
  3202. switch( type )
  3203. {
  3204. case 'v':
  3205. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3206. {// variant
  3207. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3208. script_reportdata(data);
  3209. invalid++;
  3210. }
  3211. break;
  3212. case 's':
  3213. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3214. {// string
  3215. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3216. script_reportdata(data);
  3217. invalid++;
  3218. }
  3219. break;
  3220. case 'i':
  3221. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3222. {// int ( params and constants are always int )
  3223. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3224. script_reportdata(data);
  3225. invalid++;
  3226. }
  3227. break;
  3228. case 'r':
  3229. if( !data_isreference(data) )
  3230. {// variables
  3231. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3232. script_reportdata(data);
  3233. invalid++;
  3234. }
  3235. break;
  3236. case 'l':
  3237. if( !data_islabel(data) && !data_isfunclabel(data) )
  3238. {// label
  3239. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3240. script_reportdata(data);
  3241. invalid++;
  3242. }
  3243. break;
  3244. }
  3245. }
  3246. }
  3247. if(invalid)
  3248. {
  3249. ShowDebug("Function: %s\n", get_str(func));
  3250. script_reportsrc(st);
  3251. }
  3252. }
  3253. /// Executes a buildin command.
  3254. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3255. int run_func(struct script_state *st)
  3256. {
  3257. struct script_data* data;
  3258. int i,start_sp,end_sp,func;
  3259. end_sp = st->stack->sp;// position after the last argument
  3260. for( i = end_sp-1; i > 0 ; --i )
  3261. if( st->stack->stack_data[i].type == C_ARG )
  3262. break;
  3263. if( i == 0 ) {
  3264. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3265. st->state = END;
  3266. script_reportsrc(st);
  3267. return 1;
  3268. }
  3269. start_sp = i-1;// C_NAME of the command
  3270. st->start = start_sp;
  3271. st->end = end_sp;
  3272. data = &st->stack->stack_data[st->start];
  3273. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3274. func = data->u.num;
  3275. st->funcname = reference_getname(data);
  3276. } else {
  3277. ShowError("script:run_func: not a buildin command.\n");
  3278. script_reportdata(data);
  3279. script_reportsrc(st);
  3280. st->state = END;
  3281. return 1;
  3282. }
  3283. if( script_config.warn_func_mismatch_argtypes ) {
  3284. script_check_buildin_argtype(st, func);
  3285. }
  3286. if(str_data[func].func) {
  3287. if (str_data[func].func(st)) //Report error
  3288. script_reportsrc(st);
  3289. } else {
  3290. 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));
  3291. script_reportsrc(st);
  3292. st->state = END;
  3293. }
  3294. // Stack's datum are used when re-running functions [Eoe]
  3295. if( st->state == RERUNLINE )
  3296. return 0;
  3297. pop_stack(st, st->start, st->end);
  3298. if( st->state == RETFUNC ) {// return from a user-defined function
  3299. struct script_retinfo* ri;
  3300. int olddefsp = st->stack->defsp;
  3301. int nargs;
  3302. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3303. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3304. {
  3305. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3306. script_reportsrc(st);
  3307. st->state = END;
  3308. return 1;
  3309. }
  3310. script_free_vars( st->stack->var_function );
  3311. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3312. nargs = ri->nargs;
  3313. st->pos = ri->pos;
  3314. st->script = ri->script;
  3315. st->stack->var_function = ri->var_function;
  3316. st->stack->defsp = ri->defsp;
  3317. memset(ri, 0, sizeof(struct script_retinfo));
  3318. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3319. st->state = GOTO;
  3320. }
  3321. return 0;
  3322. }
  3323. /*==========================================
  3324. * script execution
  3325. *------------------------------------------*/
  3326. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  3327. {
  3328. struct script_state *st;
  3329. if( rootscript == NULL || pos < 0 )
  3330. return;
  3331. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3332. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3333. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3334. st = script_alloc_state(rootscript, pos, rid, oid);
  3335. run_script_main(st);
  3336. }
  3337. void script_stop_sleeptimers(int id)
  3338. {
  3339. for(;;)
  3340. {
  3341. struct script_state* st = (struct script_state*)linkdb_erase(&sleep_db,(void*)__64BPRTSIZE(id));
  3342. if( st == NULL )
  3343. break; // no more sleep timers
  3344. script_free_state(st);
  3345. }
  3346. }
  3347. /*==========================================
  3348. * Delete the specified node from sleep_db
  3349. *------------------------------------------*/
  3350. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  3351. {
  3352. struct linkdb_node *retnode;
  3353. if( n == NULL)
  3354. return NULL;
  3355. if( n->prev == NULL )
  3356. sleep_db = n->next;
  3357. else
  3358. n->prev->next = n->next;
  3359. if( n->next )
  3360. n->next->prev = n->prev;
  3361. retnode = n->next;
  3362. aFree( n );
  3363. return retnode; // The following; return retnode
  3364. }
  3365. /*==========================================
  3366. * Timer function for sleep
  3367. *------------------------------------------*/
  3368. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3369. {
  3370. struct script_state *st = (struct script_state *)data;
  3371. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  3372. TBL_PC *sd = map_id2sd(st->rid);
  3373. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  3374. { //Character mismatch. Cancel execution.
  3375. st->rid = 0;
  3376. st->state = END;
  3377. }
  3378. while( node && st->sleep.timer != INVALID_TIMER ) {
  3379. if( (int)__64BPRTSIZE(node->key) == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  3380. script_erase_sleepdb(node);
  3381. st->sleep.timer = INVALID_TIMER;
  3382. break;
  3383. }
  3384. node = node->next;
  3385. }
  3386. if(st->state != RERUNLINE)
  3387. st->sleep.tick = 0;
  3388. run_script_main(st);
  3389. return 0;
  3390. }
  3391. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3392. ///
  3393. /// @param st Script state to detach.
  3394. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3395. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3396. {
  3397. struct map_session_data* sd;
  3398. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3399. sd->st = st->bk_st;
  3400. sd->npc_id = st->bk_npcid;
  3401. sd->state.disable_atcommand_on_npc = 0;
  3402. if(st->bk_st) {
  3403. //Remove tag for removal.
  3404. st->bk_st = NULL;
  3405. st->bk_npcid = 0;
  3406. } else if(dequeue_event) {
  3407. #ifdef SECURE_NPCTIMEOUT
  3408. /**
  3409. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3410. **/
  3411. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3412. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3413. sd->npc_idle_timer = INVALID_TIMER;
  3414. }
  3415. #endif
  3416. npc_event_dequeue(sd);
  3417. }
  3418. }
  3419. else if(st->bk_st)
  3420. {// rid was set to 0, before detaching the script state
  3421. 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);
  3422. script_reportsrc(st->bk_st);
  3423. script_free_state(st->bk_st);
  3424. st->bk_st = NULL;
  3425. }
  3426. }
  3427. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3428. ///
  3429. /// @param st Script state to attach.
  3430. static void script_attach_state(struct script_state* st)
  3431. {
  3432. struct map_session_data* sd;
  3433. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3434. {
  3435. if(st!=sd->st)
  3436. {
  3437. if(st->bk_st)
  3438. {// there is already a backup
  3439. 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);
  3440. }
  3441. st->bk_st = sd->st;
  3442. st->bk_npcid = sd->npc_id;
  3443. }
  3444. sd->st = st;
  3445. sd->npc_id = st->oid;
  3446. sd->npc_item_flag = st->npc_item_flag; // load default.
  3447. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3448. #ifdef SECURE_NPCTIMEOUT
  3449. if( sd->npc_idle_timer == INVALID_TIMER )
  3450. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3451. sd->npc_idle_tick = gettick();
  3452. #endif
  3453. }
  3454. }
  3455. /*==========================================
  3456. * The main part of the script execution
  3457. *------------------------------------------*/
  3458. void run_script_main(struct script_state *st)
  3459. {
  3460. int cmdcount = script_config.check_cmdcount;
  3461. int gotocount = script_config.check_gotocount;
  3462. TBL_PC *sd;
  3463. struct script_stack *stack=st->stack;
  3464. script_attach_state(st);
  3465. if(st->state == RERUNLINE) {
  3466. run_func(st);
  3467. if(st->state == GOTO)
  3468. st->state = RUN;
  3469. } else if(st->state != END)
  3470. st->state = RUN;
  3471. while(st->state == RUN)
  3472. {
  3473. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3474. switch(c){
  3475. case C_EOL:
  3476. if( stack->defsp > stack->sp )
  3477. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3478. else
  3479. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3480. break;
  3481. case C_INT:
  3482. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3483. break;
  3484. case C_POS:
  3485. case C_NAME:
  3486. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3487. st->pos+=3;
  3488. break;
  3489. case C_ARG:
  3490. push_val(stack,c,0);
  3491. break;
  3492. case C_STR:
  3493. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3494. while(st->script->script_buf[st->pos++]);
  3495. break;
  3496. case C_FUNC:
  3497. run_func(st);
  3498. if(st->state==GOTO){
  3499. st->state = RUN;
  3500. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3501. ShowError("run_script: infinity loop !\n");
  3502. script_reportsrc(st);
  3503. st->state=END;
  3504. }
  3505. }
  3506. break;
  3507. case C_REF:
  3508. st->op2ref = 1;
  3509. break;
  3510. case C_NEG:
  3511. case C_NOT:
  3512. case C_LNOT:
  3513. op_1(st ,c);
  3514. break;
  3515. case C_ADD:
  3516. case C_SUB:
  3517. case C_MUL:
  3518. case C_DIV:
  3519. case C_MOD:
  3520. case C_EQ:
  3521. case C_NE:
  3522. case C_GT:
  3523. case C_GE:
  3524. case C_LT:
  3525. case C_LE:
  3526. case C_AND:
  3527. case C_OR:
  3528. case C_XOR:
  3529. case C_LAND:
  3530. case C_LOR:
  3531. case C_R_SHIFT:
  3532. case C_L_SHIFT:
  3533. op_2(st, c);
  3534. break;
  3535. case C_OP3:
  3536. op_3(st, c);
  3537. break;
  3538. case C_NOP:
  3539. st->state=END;
  3540. break;
  3541. default:
  3542. ShowError("unknown command : %d @ %d\n",c,st->pos);
  3543. st->state=END;
  3544. break;
  3545. }
  3546. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3547. ShowError("run_script: infinity loop !\n");
  3548. script_reportsrc(st);
  3549. st->state=END;
  3550. }
  3551. }
  3552. if(st->sleep.tick > 0) {
  3553. //Restore previous script
  3554. script_detach_state(st, false);
  3555. //Delay execution
  3556. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3557. st->sleep.charid = sd?sd->status.char_id:0;
  3558. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3559. run_script_timer, st->sleep.charid, (intptr_t)st);
  3560. linkdb_insert(&sleep_db, (void*)__64BPRTSIZE(st->oid), st);
  3561. }
  3562. else if(st->state != END && st->rid){
  3563. //Resume later (st is already attached to player).
  3564. if(st->bk_st) {
  3565. ShowWarning("Unable to restore stack! Double continuation!\n");
  3566. //Report BOTH scripts to see if that can help somehow.
  3567. ShowDebug("Previous script (lost):\n");
  3568. script_reportsrc(st->bk_st);
  3569. ShowDebug("Current script:\n");
  3570. script_reportsrc(st);
  3571. script_free_state(st->bk_st);
  3572. st->bk_st = NULL;
  3573. }
  3574. } else {
  3575. //Dispose of script.
  3576. if ((sd = map_id2sd(st->rid))!=NULL)
  3577. { //Restore previous stack and save char.
  3578. if(sd->state.using_fake_npc){
  3579. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3580. sd->state.using_fake_npc = 0;
  3581. }
  3582. //Restore previous script if any.
  3583. script_detach_state(st, true);
  3584. if (sd->state.reg_dirty&2)
  3585. intif_saveregistry(sd,2);
  3586. if (sd->state.reg_dirty&1)
  3587. intif_saveregistry(sd,1);
  3588. }
  3589. script_free_state(st);
  3590. }
  3591. }
  3592. int script_config_read(char *cfgName)
  3593. {
  3594. int i;
  3595. char line[1024],w1[1024],w2[1024];
  3596. FILE *fp;
  3597. fp=fopen(cfgName,"r");
  3598. if(fp==NULL){
  3599. ShowError("File not found: %s\n", cfgName);
  3600. return 1;
  3601. }
  3602. while(fgets(line, sizeof(line), fp))
  3603. {
  3604. if(line[0] == '/' && line[1] == '/')
  3605. continue;
  3606. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3607. if(i!=2)
  3608. continue;
  3609. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3610. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3611. }
  3612. else if(strcmpi(w1,"check_cmdcount")==0) {
  3613. script_config.check_cmdcount = config_switch(w2);
  3614. }
  3615. else if(strcmpi(w1,"check_gotocount")==0) {
  3616. script_config.check_gotocount = config_switch(w2);
  3617. }
  3618. else if(strcmpi(w1,"input_min_value")==0) {
  3619. script_config.input_min_value = config_switch(w2);
  3620. }
  3621. else if(strcmpi(w1,"input_max_value")==0) {
  3622. script_config.input_max_value = config_switch(w2);
  3623. }
  3624. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3625. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3626. }
  3627. else if(strcmpi(w1,"import")==0){
  3628. script_config_read(w2);
  3629. }
  3630. else {
  3631. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3632. }
  3633. }
  3634. fclose(fp);
  3635. return 0;
  3636. }
  3637. /**
  3638. * @see DBApply
  3639. */
  3640. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3641. {
  3642. struct script_code *code = db_data2ptr(data);
  3643. if (code)
  3644. script_free_code(code);
  3645. return 0;
  3646. }
  3647. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3648. {
  3649. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3650. if( script )
  3651. {
  3652. int j;
  3653. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == pos );
  3654. if( j < EQI_MAX ) {
  3655. //Single item autobonus
  3656. current_equip_item_index = sd->equip_index[j];
  3657. current_equip_combo_pos = 0;
  3658. } else {
  3659. //Combo autobonus
  3660. current_equip_item_index = -1;
  3661. current_equip_combo_pos = pos;
  3662. }
  3663. run_script(script,0,sd->bl.id,0);
  3664. }
  3665. }
  3666. void script_add_autobonus(const char *autobonus)
  3667. {
  3668. if( strdb_get(autobonus_db, autobonus) == NULL )
  3669. {
  3670. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3671. if( script )
  3672. strdb_put(autobonus_db, autobonus, script);
  3673. }
  3674. }
  3675. /// resets a temporary character array variable to given value
  3676. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3677. {
  3678. int key;
  3679. uint8 idx;
  3680. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3681. {
  3682. ShowError("script_cleararray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3683. return;
  3684. }
  3685. key = add_str(varname);
  3686. if( is_string_variable(varname) )
  3687. {
  3688. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3689. {
  3690. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3691. }
  3692. }
  3693. else
  3694. {
  3695. for( idx = 0; idx < SCRIPT_MAX_ARRAYSIZE; idx++ )
  3696. {
  3697. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3698. }
  3699. }
  3700. }
  3701. /// sets a temporary character array variable element idx to given value
  3702. /// @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.
  3703. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint8 idx, void* value, int* refcache)
  3704. {
  3705. int key;
  3706. if( not_array_variable(varname[0]) || !not_server_variable(varname[0]) )
  3707. {
  3708. ShowError("script_setarray_pc: Variable '%s' has invalid scope (char_id=%d).\n", varname, sd->status.char_id);
  3709. return;
  3710. }
  3711. if( idx >= SCRIPT_MAX_ARRAYSIZE )
  3712. {
  3713. ShowError("script_setarray_pc: Variable '%s' has invalid index '%d' (char_id=%d).\n", varname, (int)idx, sd->status.char_id);
  3714. return;
  3715. }
  3716. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3717. if( is_string_variable(varname) )
  3718. {
  3719. pc_setregstr(sd, reference_uid(key, idx), (const char*)value);
  3720. }
  3721. else
  3722. {
  3723. pc_setreg(sd, reference_uid(key, idx), (int)__64BPRTSIZE(value));
  3724. }
  3725. if( refcache )
  3726. {// save to avoid repeated add_str calls
  3727. refcache[0] = key;
  3728. }
  3729. }
  3730. #ifdef BETA_THREAD_TEST
  3731. int buildin_query_sql_sub(struct script_state* st, Sql* handle);
  3732. /* used to receive items the queryThread has already processed */
  3733. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3734. int i, cursor = 0;
  3735. bool allOk = true;
  3736. EnterSpinLock(&queryThreadLock);
  3737. for( i = 0; i < queryThreadData.count; i++ ) {
  3738. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3739. if( !entry->ok ) {
  3740. allOk = false;
  3741. continue;
  3742. }
  3743. run_script_main(entry->st);
  3744. entry->st = NULL;/* empty entries */
  3745. aFree(entry);
  3746. queryThreadData.entry[i] = NULL;
  3747. }
  3748. if( allOk ) {
  3749. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3750. delete_timer(queryThreadData.timer, queryThread_timer);
  3751. queryThreadData.timer = INVALID_TIMER;
  3752. }
  3753. /* now lets clear the mess. */
  3754. for( i = 0; i < queryThreadData.count; i++ ) {
  3755. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3756. if( entry == NULL )
  3757. continue;/* entry on hold */
  3758. /* move */
  3759. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3760. cursor++;
  3761. }
  3762. queryThreadData.count = cursor;
  3763. LeaveSpinLock(&queryThreadLock);
  3764. return 0;
  3765. }
  3766. void queryThread_add(struct script_state *st, bool type) {
  3767. int idx = 0;
  3768. struct queryThreadEntry* entry = NULL;
  3769. EnterSpinLock(&queryThreadLock);
  3770. if( queryThreadData.count++ != 0 )
  3771. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3772. idx = queryThreadData.count-1;
  3773. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3774. entry = queryThreadData.entry[idx];
  3775. entry->st = st;
  3776. entry->ok = false;
  3777. entry->type = type;
  3778. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3779. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3780. }
  3781. LeaveSpinLock(&queryThreadLock);
  3782. /* unlock the queryThread */
  3783. racond_signal(queryThreadCond);
  3784. }
  3785. /* adds a new log to the queue */
  3786. void queryThread_log(char * entry, int length) {
  3787. int idx = logThreadData.count;
  3788. EnterSpinLock(&queryThreadLock);
  3789. if( logThreadData.count++ != 0 )
  3790. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3791. CREATE(logThreadData.entry[idx], char, length + 1 );
  3792. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3793. LeaveSpinLock(&queryThreadLock);
  3794. /* unlock the queryThread */
  3795. racond_signal(queryThreadCond);
  3796. }
  3797. /* queryThread_main */
  3798. static void *queryThread_main(void *x) {
  3799. Sql *queryThread_handle = Sql_Malloc();
  3800. int i;
  3801. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  3802. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3803. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  3804. Sql_ShowDebug(queryThread_handle);
  3805. Sql_Free(queryThread_handle);
  3806. exit(EXIT_FAILURE);
  3807. }
  3808. if( strlen(default_codepage) > 0 )
  3809. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3810. Sql_ShowDebug(queryThread_handle);
  3811. if( log_config.sql_logs ) {
  3812. logmysql_handle = Sql_Malloc();
  3813. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  3814. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3815. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  3816. Sql_ShowDebug(logmysql_handle);
  3817. Sql_Free(logmysql_handle);
  3818. exit(EXIT_FAILURE);
  3819. }
  3820. if( strlen(default_codepage) > 0 )
  3821. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  3822. Sql_ShowDebug(logmysql_handle);
  3823. }
  3824. while( 1 ) {
  3825. if(queryThreadTerminate > 0)
  3826. break;
  3827. EnterSpinLock(&queryThreadLock);
  3828. /* mess with queryThreadData within the lock */
  3829. for( i = 0; i < queryThreadData.count; i++ ) {
  3830. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3831. if( entry->ok )
  3832. continue;
  3833. else if ( !entry->st || !entry->st->stack ) {
  3834. entry->ok = true;/* dispose */
  3835. continue;
  3836. }
  3837. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  3838. entry->ok = true;/* we're done with this */
  3839. }
  3840. /* also check for any logs in need to be sent */
  3841. if( log_config.sql_logs ) {
  3842. for( i = 0; i < logThreadData.count; i++ ) {
  3843. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  3844. Sql_ShowDebug(logmysql_handle);
  3845. aFree(logThreadData.entry[i]);
  3846. }
  3847. logThreadData.count = 0;
  3848. }
  3849. LeaveSpinLock(&queryThreadLock);
  3850. ramutex_lock( queryThreadMutex );
  3851. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  3852. ramutex_unlock( queryThreadMutex );
  3853. }
  3854. Sql_Free(queryThread_handle);
  3855. if( log_config.sql_logs ) {
  3856. Sql_Free(logmysql_handle);
  3857. }
  3858. return NULL;
  3859. }
  3860. #endif
  3861. /*==========================================
  3862. * Destructor
  3863. *------------------------------------------*/
  3864. void do_final_script() {
  3865. int i;
  3866. #ifdef DEBUG_HASH
  3867. if (battle_config.etc_log)
  3868. {
  3869. FILE *fp = fopen("hash_dump.txt","wt");
  3870. if(fp) {
  3871. int count[SCRIPT_HASH_SIZE];
  3872. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3873. int n=0;
  3874. int min=INT_MAX,max=0,zero=0;
  3875. double mean=0.0f;
  3876. double median=0.0f;
  3877. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3878. memset(count, 0, sizeof(count));
  3879. fprintf(fp,"num : hash : data_name\n");
  3880. fprintf(fp,"---------------------------------------------------------------\n");
  3881. for(i=LABEL_START; i<str_num; i++) {
  3882. unsigned int h = calc_hash(get_str(i));
  3883. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3884. ++count[h];
  3885. }
  3886. fprintf(fp,"--------------------\n\n");
  3887. memset(count2, 0, sizeof(count2));
  3888. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3889. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3890. if(min > count[i])
  3891. min = count[i]; // minimun count of collision
  3892. if(max < count[i])
  3893. max = count[i]; // maximun count of collision
  3894. if(count[i] == 0)
  3895. zero++;
  3896. ++count2[count[i]];
  3897. }
  3898. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3899. for( i=min; i <= max; ++i ){
  3900. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3901. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3902. }
  3903. for( i=min; i <= max; ++i ){
  3904. n += count2[i];
  3905. if( n*2 >= SCRIPT_HASH_SIZE )
  3906. {
  3907. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3908. median = (i+i+1)/2.0f;
  3909. else
  3910. median = i;
  3911. break;
  3912. }
  3913. }
  3914. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3915. fclose(fp);
  3916. }
  3917. }
  3918. #endif
  3919. mapreg_final();
  3920. db_destroy(scriptlabel_db);
  3921. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  3922. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  3923. if(sleep_db) {
  3924. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3925. while(n) {
  3926. struct script_state *st = (struct script_state *)n->data;
  3927. script_free_state(st);
  3928. n = n->next;
  3929. }
  3930. linkdb_final(&sleep_db);
  3931. }
  3932. if (str_data)
  3933. aFree(str_data);
  3934. if (str_buf)
  3935. aFree(str_buf);
  3936. for( i = 0; i < atcmd_binding_count; i++ ) {
  3937. aFree(atcmd_binding[i]);
  3938. }
  3939. if( atcmd_binding_count != 0 )
  3940. aFree(atcmd_binding);
  3941. #ifdef BETA_THREAD_TEST
  3942. /* QueryThread */
  3943. InterlockedIncrement(&queryThreadTerminate);
  3944. racond_signal(queryThreadCond);
  3945. rathread_wait(queryThread, NULL);
  3946. // Destroy cond var and mutex.
  3947. racond_destroy( queryThreadCond );
  3948. ramutex_destroy( queryThreadMutex );
  3949. /* Clear missing vars */
  3950. for( i = 0; i < queryThreadData.count; i++ ) {
  3951. aFree(queryThreadData.entry[i]);
  3952. }
  3953. aFree(queryThreadData.entry);
  3954. for( i = 0; i < logThreadData.count; i++ ) {
  3955. aFree(logThreadData.entry[i]);
  3956. }
  3957. aFree(logThreadData.entry);
  3958. #endif
  3959. }
  3960. /*==========================================
  3961. * Initialization
  3962. *------------------------------------------*/
  3963. void do_init_script(void) {
  3964. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3965. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY,50);
  3966. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  3967. mapreg_init();
  3968. #ifdef BETA_THREAD_TEST
  3969. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  3970. queryThreadData.count = 0;
  3971. CREATE(logThreadData.entry, char *, 1);
  3972. logThreadData.count = 0;
  3973. /* QueryThread Start */
  3974. InitializeSpinLock(&queryThreadLock);
  3975. queryThreadData.timer = INVALID_TIMER;
  3976. queryThreadTerminate = 0;
  3977. queryThreadMutex = ramutex_create();
  3978. queryThreadCond = racond_create();
  3979. queryThread = rathread_create(queryThread_main, NULL);
  3980. if(queryThread == NULL){
  3981. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  3982. exit(EXIT_FAILURE);
  3983. }
  3984. add_timer_func_list(queryThread_timer, "queryThread_timer");
  3985. #endif
  3986. }
  3987. void script_reload(void) {
  3988. int i;
  3989. #ifdef BETA_THREAD_TEST
  3990. /* we're reloading so any queries undergoing should be...exterminated. */
  3991. EnterSpinLock(&queryThreadLock);
  3992. for( i = 0; i < queryThreadData.count; i++ ) {
  3993. aFree(queryThreadData.entry[i]);
  3994. }
  3995. queryThreadData.count = 0;
  3996. if( queryThreadData.timer != INVALID_TIMER ) {
  3997. delete_timer(queryThreadData.timer, queryThread_timer);
  3998. queryThreadData.timer = INVALID_TIMER;
  3999. }
  4000. LeaveSpinLock(&queryThreadLock);
  4001. #endif
  4002. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4003. db_clear(scriptlabel_db);
  4004. // @commands (script based)
  4005. // Clear bindings
  4006. for( i = 0; i < atcmd_binding_count; i++ ) {
  4007. aFree(atcmd_binding[i]);
  4008. }
  4009. if( atcmd_binding_count != 0 )
  4010. aFree(atcmd_binding);
  4011. atcmd_binding_count = 0;
  4012. if(sleep_db) {
  4013. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  4014. while(n) {
  4015. struct script_state *st = (struct script_state *)n->data;
  4016. script_free_state(st);
  4017. n = n->next;
  4018. }
  4019. linkdb_final(&sleep_db);
  4020. }
  4021. mapreg_reload();
  4022. }
  4023. //-----------------------------------------------------------------------------
  4024. // buildin functions
  4025. //
  4026. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4027. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4028. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4029. /////////////////////////////////////////////////////////////////////
  4030. // NPC interaction
  4031. //
  4032. /// Appends a message to the npc dialog.
  4033. /// If a dialog doesn't exist yet, one is created.
  4034. ///
  4035. /// mes "<message>";
  4036. BUILDIN_FUNC(mes)
  4037. {
  4038. TBL_PC* sd = script_rid2sd(st);
  4039. if( sd == NULL )
  4040. return 0;
  4041. if( !script_hasdata(st, 3) )
  4042. {// only a single line detected in the script
  4043. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4044. }
  4045. else
  4046. {// parse multiple lines as they exist
  4047. int i;
  4048. for( i = 2; script_hasdata(st, i); i++ )
  4049. {
  4050. // send the message to the client
  4051. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4052. }
  4053. }
  4054. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4055. return SCRIPT_CMD_SUCCESS;
  4056. }
  4057. /// Displays the button 'next' in the npc dialog.
  4058. /// The dialog text is cleared and the script continues when the button is pressed.
  4059. ///
  4060. /// next;
  4061. BUILDIN_FUNC(next)
  4062. {
  4063. TBL_PC* sd;
  4064. sd = script_rid2sd(st);
  4065. if( sd == NULL )
  4066. return 0;
  4067. #ifdef SECURE_NPCTIMEOUT
  4068. sd->npc_idle_type = NPCT_WAIT;
  4069. #endif
  4070. st->state = STOP;
  4071. clif_scriptnext(sd, st->oid);
  4072. return SCRIPT_CMD_SUCCESS;
  4073. }
  4074. /// Ends the script and displays the button 'close' on the npc dialog.
  4075. /// The dialog is closed when the button is pressed.
  4076. ///
  4077. /// close;
  4078. BUILDIN_FUNC(close)
  4079. {
  4080. TBL_PC* sd;
  4081. sd = script_rid2sd(st);
  4082. if( sd == NULL )
  4083. return 0;
  4084. if( !st->mes_active ) {
  4085. TBL_NPC* nd = map_id2nd(st->oid);
  4086. st->state = END; // Keep backwards compatibility.
  4087. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4088. } else {
  4089. st->state = CLOSE;
  4090. st->mes_active = 0;
  4091. }
  4092. clif_scriptclose(sd, st->oid);
  4093. return SCRIPT_CMD_SUCCESS;
  4094. }
  4095. /// Displays the button 'close' on the npc dialog.
  4096. /// The dialog is closed and the script continues when the button is pressed.
  4097. ///
  4098. /// close2;
  4099. BUILDIN_FUNC(close2)
  4100. {
  4101. TBL_PC* sd;
  4102. sd = script_rid2sd(st);
  4103. if( sd == NULL )
  4104. return 0;
  4105. st->state = STOP;
  4106. if( st->mes_active )
  4107. st->mes_active = 0;
  4108. clif_scriptclose(sd, st->oid);
  4109. return SCRIPT_CMD_SUCCESS;
  4110. }
  4111. /// Counts the number of valid and total number of options in 'str'
  4112. /// If max_count > 0 the counting stops when that valid option is reached
  4113. /// total is incremented for each option (NULL is supported)
  4114. static int menu_countoptions(const char* str, int max_count, int* total)
  4115. {
  4116. int count = 0;
  4117. int bogus_total;
  4118. if( total == NULL )
  4119. total = &bogus_total;
  4120. ++(*total);
  4121. // initial empty options
  4122. while( *str == ':' )
  4123. {
  4124. ++str;
  4125. ++(*total);
  4126. }
  4127. // count menu options
  4128. while( *str != '\0' )
  4129. {
  4130. ++count;
  4131. --max_count;
  4132. if( max_count == 0 )
  4133. break;
  4134. while( *str != ':' && *str != '\0' )
  4135. ++str;
  4136. while( *str == ':' )
  4137. {
  4138. ++str;
  4139. ++(*total);
  4140. }
  4141. }
  4142. return count;
  4143. }
  4144. /// Displays a menu with options and goes to the target label.
  4145. /// The script is stopped if cancel is pressed.
  4146. /// Options with no text are not displayed in the client.
  4147. ///
  4148. /// Options can be grouped together, separated by the character ':' in the text:
  4149. /// ex: menu "A:B:C",L_target;
  4150. /// All these options go to the specified target label.
  4151. ///
  4152. /// The index of the selected option is put in the variable @menu.
  4153. /// Indexes start with 1 and are consistent with grouped and empty options.
  4154. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4155. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4156. ///
  4157. /// NOTE: the client closes the npc dialog when cancel is pressed
  4158. ///
  4159. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4160. BUILDIN_FUNC(menu)
  4161. {
  4162. int i;
  4163. const char* text;
  4164. TBL_PC* sd;
  4165. sd = script_rid2sd(st);
  4166. if( sd == NULL )
  4167. return 0;
  4168. #ifdef SECURE_NPCTIMEOUT
  4169. sd->npc_idle_type = NPCT_MENU;
  4170. #endif
  4171. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4172. if( sd->state.menu_or_input == 0 )
  4173. {
  4174. struct StringBuf buf;
  4175. if( script_lastdata(st) % 2 == 0 )
  4176. {// argument count is not even (1st argument is at index 2)
  4177. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4178. st->state = END;
  4179. return SCRIPT_CMD_FAILURE;
  4180. }
  4181. StringBuf_Init(&buf);
  4182. sd->npc_menu = 0;
  4183. for( i = 2; i < script_lastdata(st); i += 2 )
  4184. {
  4185. struct script_data* data;
  4186. // menu options
  4187. text = script_getstr(st, i);
  4188. // target label
  4189. data = script_getdata(st, i+1);
  4190. if( !data_islabel(data) )
  4191. {// not a label
  4192. StringBuf_Destroy(&buf);
  4193. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4194. script_reportdata(data);
  4195. st->state = END;
  4196. return SCRIPT_CMD_FAILURE;
  4197. }
  4198. // append option(s)
  4199. if( text[0] == '\0' )
  4200. continue;// empty string, ignore
  4201. if( sd->npc_menu > 0 )
  4202. StringBuf_AppendStr(&buf, ":");
  4203. StringBuf_AppendStr(&buf, text);
  4204. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4205. }
  4206. st->state = RERUNLINE;
  4207. sd->state.menu_or_input = 1;
  4208. /**
  4209. * menus beyond this length crash the client (see bugreport:6402)
  4210. **/
  4211. if( StringBuf_Length(&buf) >= 2047 ) {
  4212. struct npc_data * nd = map_id2nd(st->oid);
  4213. char* menu;
  4214. CREATE(menu, char, 2048);
  4215. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4216. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4217. clif_scriptmenu(sd, st->oid, menu);
  4218. aFree(menu);
  4219. } else
  4220. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4221. StringBuf_Destroy(&buf);
  4222. if( sd->npc_menu >= 0xff )
  4223. {// 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
  4224. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4225. script_reportsrc(st);
  4226. }
  4227. }
  4228. else if( sd->npc_menu == 0xff )
  4229. {// Cancel was pressed
  4230. sd->state.menu_or_input = 0;
  4231. st->state = END;
  4232. }
  4233. else
  4234. {// goto target label
  4235. int menu = 0;
  4236. sd->state.menu_or_input = 0;
  4237. if( sd->npc_menu <= 0 )
  4238. {
  4239. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4240. st->state = END;
  4241. return SCRIPT_CMD_FAILURE;
  4242. }
  4243. // get target label
  4244. for( i = 2; i < script_lastdata(st); i += 2 )
  4245. {
  4246. text = script_getstr(st, i);
  4247. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4248. if( sd->npc_menu <= 0 )
  4249. break;// entry found
  4250. }
  4251. if( sd->npc_menu > 0 )
  4252. {// Invalid selection
  4253. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4254. st->state = END;
  4255. return SCRIPT_CMD_FAILURE;
  4256. }
  4257. if( !data_islabel(script_getdata(st, i + 1)) )
  4258. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4259. ShowError("script:menu: unexpected data in label argument\n");
  4260. script_reportdata(script_getdata(st, i + 1));
  4261. st->state = END;
  4262. return SCRIPT_CMD_FAILURE;
  4263. }
  4264. pc_setreg(sd, add_str("@menu"), menu);
  4265. st->pos = script_getnum(st, i + 1);
  4266. st->state = GOTO;
  4267. }
  4268. return SCRIPT_CMD_SUCCESS;
  4269. }
  4270. /// Displays a menu with options and returns the selected option.
  4271. /// Behaves like 'menu' without the target labels.
  4272. ///
  4273. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4274. ///
  4275. /// @see menu
  4276. BUILDIN_FUNC(select)
  4277. {
  4278. int i;
  4279. const char* text;
  4280. TBL_PC* sd;
  4281. sd = script_rid2sd(st);
  4282. if( sd == NULL )
  4283. return 0;
  4284. #ifdef SECURE_NPCTIMEOUT
  4285. sd->npc_idle_type = NPCT_MENU;
  4286. #endif
  4287. if( sd->state.menu_or_input == 0 ) {
  4288. struct StringBuf buf;
  4289. StringBuf_Init(&buf);
  4290. sd->npc_menu = 0;
  4291. for( i = 2; i <= script_lastdata(st); ++i ) {
  4292. text = script_getstr(st, i);
  4293. if( sd->npc_menu > 0 )
  4294. StringBuf_AppendStr(&buf, ":");
  4295. StringBuf_AppendStr(&buf, text);
  4296. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4297. }
  4298. st->state = RERUNLINE;
  4299. sd->state.menu_or_input = 1;
  4300. /**
  4301. * menus beyond this length crash the client (see bugreport:6402)
  4302. **/
  4303. if( StringBuf_Length(&buf) >= 2047 ) {
  4304. struct npc_data * nd = map_id2nd(st->oid);
  4305. char* menu;
  4306. CREATE(menu, char, 2048);
  4307. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4308. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4309. clif_scriptmenu(sd, st->oid, menu);
  4310. aFree(menu);
  4311. } else
  4312. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4313. StringBuf_Destroy(&buf);
  4314. if( sd->npc_menu >= 0xff ) {
  4315. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4316. script_reportsrc(st);
  4317. }
  4318. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4319. sd->state.menu_or_input = 0;
  4320. st->state = END;
  4321. } else {// return selected option
  4322. int menu = 0;
  4323. sd->state.menu_or_input = 0;
  4324. for( i = 2; i <= script_lastdata(st); ++i ) {
  4325. text = script_getstr(st, i);
  4326. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4327. if( sd->npc_menu <= 0 )
  4328. break;// entry found
  4329. }
  4330. pc_setreg(sd, add_str("@menu"), menu);
  4331. script_pushint(st, menu);
  4332. st->state = RUN;
  4333. }
  4334. return SCRIPT_CMD_SUCCESS;
  4335. }
  4336. /// Displays a menu with options and returns the selected option.
  4337. /// Behaves like 'menu' without the target labels, except when cancel is
  4338. /// pressed.
  4339. /// When cancel is pressed, the script continues and 255 is returned.
  4340. ///
  4341. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4342. ///
  4343. /// @see menu
  4344. BUILDIN_FUNC(prompt)
  4345. {
  4346. int i;
  4347. const char *text;
  4348. TBL_PC* sd;
  4349. sd = script_rid2sd(st);
  4350. if( sd == NULL )
  4351. return 0;
  4352. #ifdef SECURE_NPCTIMEOUT
  4353. sd->npc_idle_type = NPCT_MENU;
  4354. #endif
  4355. if( sd->state.menu_or_input == 0 )
  4356. {
  4357. struct StringBuf buf;
  4358. StringBuf_Init(&buf);
  4359. sd->npc_menu = 0;
  4360. for( i = 2; i <= script_lastdata(st); ++i )
  4361. {
  4362. text = script_getstr(st, i);
  4363. if( sd->npc_menu > 0 )
  4364. StringBuf_AppendStr(&buf, ":");
  4365. StringBuf_AppendStr(&buf, text);
  4366. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4367. }
  4368. st->state = RERUNLINE;
  4369. sd->state.menu_or_input = 1;
  4370. /**
  4371. * menus beyond this length crash the client (see bugreport:6402)
  4372. **/
  4373. if( StringBuf_Length(&buf) >= 2047 ) {
  4374. struct npc_data * nd = map_id2nd(st->oid);
  4375. char* menu;
  4376. CREATE(menu, char, 2048);
  4377. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4378. ShowWarning("NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4379. clif_scriptmenu(sd, st->oid, menu);
  4380. aFree(menu);
  4381. } else
  4382. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4383. StringBuf_Destroy(&buf);
  4384. if( sd->npc_menu >= 0xff )
  4385. {
  4386. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4387. script_reportsrc(st);
  4388. }
  4389. }
  4390. else if( sd->npc_menu == 0xff )
  4391. {// Cancel was pressed
  4392. sd->state.menu_or_input = 0;
  4393. pc_setreg(sd, add_str("@menu"), 0xff);
  4394. script_pushint(st, 0xff);
  4395. st->state = RUN;
  4396. }
  4397. else
  4398. {// return selected option
  4399. int menu = 0;
  4400. sd->state.menu_or_input = 0;
  4401. for( i = 2; i <= script_lastdata(st); ++i )
  4402. {
  4403. text = script_getstr(st, i);
  4404. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4405. if( sd->npc_menu <= 0 )
  4406. break;// entry found
  4407. }
  4408. pc_setreg(sd, add_str("@menu"), menu);
  4409. script_pushint(st, menu);
  4410. st->state = RUN;
  4411. }
  4412. return SCRIPT_CMD_SUCCESS;
  4413. }
  4414. /////////////////////////////////////////////////////////////////////
  4415. // ...
  4416. //
  4417. /// Jumps to the target script label.
  4418. ///
  4419. /// goto <label>;
  4420. BUILDIN_FUNC(goto)
  4421. {
  4422. if( !data_islabel(script_getdata(st,2)) )
  4423. {
  4424. ShowError("script:goto: not a label\n");
  4425. script_reportdata(script_getdata(st,2));
  4426. st->state = END;
  4427. return 1;
  4428. }
  4429. st->pos = script_getnum(st,2);
  4430. st->state = GOTO;
  4431. return SCRIPT_CMD_SUCCESS;
  4432. }
  4433. /*==========================================
  4434. * user-defined function call
  4435. *------------------------------------------*/
  4436. BUILDIN_FUNC(callfunc)
  4437. {
  4438. int i, j;
  4439. struct script_retinfo* ri;
  4440. struct script_code* scr;
  4441. const char* str = script_getstr(st,2);
  4442. DBMap **ref = NULL;
  4443. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4444. if( !scr )
  4445. {
  4446. ShowError("script:callfunc: function not found! [%s]\n", str);
  4447. st->state = END;
  4448. return 1;
  4449. }
  4450. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4451. {
  4452. struct script_data* data = push_copy(st->stack,i);
  4453. if( data_isreference(data) && !data->ref )
  4454. {
  4455. const char* name = reference_getname(data);
  4456. if( name[0] == '.' ) {
  4457. if(!ref){
  4458. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4459. ref[0] = (name[1] == '@' ? st->stack->var_function : st->script->script_vars);
  4460. }
  4461. data->ref = ref;
  4462. }
  4463. }
  4464. }
  4465. CREATE(ri, struct script_retinfo, 1);
  4466. ri->script = st->script;// script code
  4467. ri->var_function = st->stack->var_function;// scope variables
  4468. ri->pos = st->pos;// script location
  4469. ri->nargs = j;// argument count
  4470. ri->defsp = st->stack->defsp;// default stack pointer
  4471. push_retinfo(st->stack, ri, ref);
  4472. st->pos = 0;
  4473. st->script = scr;
  4474. st->stack->defsp = st->stack->sp;
  4475. st->state = GOTO;
  4476. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4477. return SCRIPT_CMD_SUCCESS;
  4478. }
  4479. /*==========================================
  4480. * subroutine call
  4481. *------------------------------------------*/
  4482. BUILDIN_FUNC(callsub)
  4483. {
  4484. int i,j;
  4485. struct script_retinfo* ri;
  4486. int pos = script_getnum(st,2);
  4487. DBMap **ref = NULL;
  4488. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  4489. {
  4490. ShowError("script:callsub: argument is not a label\n");
  4491. script_reportdata(script_getdata(st,2));
  4492. st->state = END;
  4493. return 1;
  4494. }
  4495. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  4496. {
  4497. struct script_data* data = push_copy(st->stack,i);
  4498. if( data_isreference(data) && !data->ref )
  4499. {
  4500. const char* name = reference_getname(data);
  4501. if( name[0] == '.' && name[1] == '@' ) {
  4502. if ( !ref ) {
  4503. ref = (struct DBMap**)aCalloc(sizeof(struct DBMap*), 1);
  4504. ref[0] = st->stack->var_function;
  4505. }
  4506. data->ref = ref;
  4507. }
  4508. }
  4509. }
  4510. CREATE(ri, struct script_retinfo, 1);
  4511. ri->script = st->script;// script code
  4512. ri->var_function = st->stack->var_function;// scope variables
  4513. ri->pos = st->pos;// script location
  4514. ri->nargs = j;// argument count
  4515. ri->defsp = st->stack->defsp;// default stack pointer
  4516. push_retinfo(st->stack, ri, ref);
  4517. st->pos = pos;
  4518. st->stack->defsp = st->stack->sp;
  4519. st->state = GOTO;
  4520. st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
  4521. return SCRIPT_CMD_SUCCESS;
  4522. }
  4523. /// Retrieves an argument provided to callfunc/callsub.
  4524. /// If the argument doesn't exist
  4525. ///
  4526. /// getarg(<index>{,<default_value>}) -> <value>
  4527. BUILDIN_FUNC(getarg)
  4528. {
  4529. struct script_retinfo* ri;
  4530. int idx;
  4531. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4532. {
  4533. ShowError("script:getarg: no callfunc or callsub!\n");
  4534. st->state = END;
  4535. return 1;
  4536. }
  4537. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4538. idx = script_getnum(st,2);
  4539. if( idx >= 0 && idx < ri->nargs )
  4540. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4541. else if( script_hasdata(st,3) )
  4542. script_pushcopy(st, 3);
  4543. else
  4544. {
  4545. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4546. st->state = END;
  4547. return 1;
  4548. }
  4549. return SCRIPT_CMD_SUCCESS;
  4550. }
  4551. /// Returns from the current function, optionaly returning a value from the functions.
  4552. /// Don't use outside script functions.
  4553. ///
  4554. /// return;
  4555. /// return <value>;
  4556. BUILDIN_FUNC(return)
  4557. {
  4558. if( script_hasdata(st,2) )
  4559. {// return value
  4560. struct script_data* data;
  4561. script_pushcopy(st, 2);
  4562. data = script_getdatatop(st, -1);
  4563. if( data_isreference(data) )
  4564. {
  4565. const char* name = reference_getname(data);
  4566. if( name[0] == '.' && name[1] == '@' )
  4567. {// scope variable
  4568. if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
  4569. get_val(st, data);// current scope, convert to value
  4570. }
  4571. else if( name[0] == '.' && !data->ref )
  4572. {// script variable, link to current script
  4573. data->ref = &st->script->script_vars;
  4574. }
  4575. }
  4576. }
  4577. else
  4578. {// no return value
  4579. script_pushnil(st);
  4580. }
  4581. st->state = RETFUNC;
  4582. return SCRIPT_CMD_SUCCESS;
  4583. }
  4584. /// Returns a random number from 0 to <range>-1.
  4585. /// Or returns a random number from <min> to <max>.
  4586. /// If <min> is greater than <max>, their numbers are switched.
  4587. /// rand(<range>) -> <int>
  4588. /// rand(<min>,<max>) -> <int>
  4589. BUILDIN_FUNC(rand)
  4590. {
  4591. int range;
  4592. int min;
  4593. if( script_hasdata(st,3) )
  4594. {// min,max
  4595. int max = script_getnum(st,3);
  4596. min = script_getnum(st,2);
  4597. if( max < min )
  4598. swap(min, max);
  4599. range = max - min + 1;
  4600. }
  4601. else
  4602. {// range
  4603. min = 0;
  4604. range = script_getnum(st,2);
  4605. }
  4606. if( range <= 1 )
  4607. script_pushint(st, min);
  4608. else
  4609. script_pushint(st, rnd()%range + min);
  4610. return SCRIPT_CMD_SUCCESS;
  4611. }
  4612. /*==========================================
  4613. * Warp sd to str,x,y or Random or SavePoint/Save
  4614. *------------------------------------------*/
  4615. BUILDIN_FUNC(warp)
  4616. {
  4617. int ret;
  4618. int x,y;
  4619. const char* str;
  4620. TBL_PC* sd;
  4621. sd = script_rid2sd(st);
  4622. if( sd == NULL )
  4623. return 0;
  4624. str = script_getstr(st,2);
  4625. x = script_getnum(st,3);
  4626. y = script_getnum(st,4);
  4627. if(strcmp(str,"Random")==0)
  4628. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4629. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4630. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4631. else
  4632. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4633. if( ret ) {
  4634. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4635. return 1;
  4636. }
  4637. return SCRIPT_CMD_SUCCESS;
  4638. }
  4639. /*==========================================
  4640. * Warp a specified area
  4641. *------------------------------------------*/
  4642. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4643. {
  4644. int x2,y2,x3,y3;
  4645. unsigned int index;
  4646. index = va_arg(ap,unsigned int);
  4647. x2 = va_arg(ap,int);
  4648. y2 = va_arg(ap,int);
  4649. x3 = va_arg(ap,int);
  4650. y3 = va_arg(ap,int);
  4651. if(index == 0)
  4652. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4653. else if(x3 && y3) {
  4654. int max, tx, ty, j = 0;
  4655. // choose a suitable max number of attempts
  4656. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4657. max = 1000;
  4658. // find a suitable map cell
  4659. do {
  4660. tx = rnd()%(x3-x2+1)+x2;
  4661. ty = rnd()%(y3-y2+1)+y2;
  4662. j++;
  4663. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4664. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4665. }
  4666. else
  4667. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4668. return 0;
  4669. }
  4670. BUILDIN_FUNC(areawarp)
  4671. {
  4672. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4673. unsigned int index;
  4674. const char *str;
  4675. const char *mapname;
  4676. mapname = script_getstr(st,2);
  4677. x0 = script_getnum(st,3);
  4678. y0 = script_getnum(st,4);
  4679. x1 = script_getnum(st,5);
  4680. y1 = script_getnum(st,6);
  4681. str = script_getstr(st,7);
  4682. x2 = script_getnum(st,8);
  4683. y2 = script_getnum(st,9);
  4684. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4685. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4686. x3 = 0;
  4687. y3 = 0;
  4688. } else if( x3 && y3 ) {
  4689. // normalize x3/y3 coordinates
  4690. if( x3 < x2 ) swap(x3,x2);
  4691. if( y3 < y2 ) swap(y3,y2);
  4692. }
  4693. }
  4694. if( (m = map_mapname2mapid(mapname)) < 0 )
  4695. return 0;
  4696. if( strcmp(str,"Random") == 0 )
  4697. index = 0;
  4698. else if( !(index=mapindex_name2id(str)) )
  4699. return 0;
  4700. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4701. return SCRIPT_CMD_SUCCESS;
  4702. }
  4703. /*==========================================
  4704. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4705. *------------------------------------------*/
  4706. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4707. {
  4708. int hp, sp;
  4709. hp = va_arg(ap, int);
  4710. sp = va_arg(ap, int);
  4711. pc_percentheal((TBL_PC *)bl,hp,sp);
  4712. return 0;
  4713. }
  4714. BUILDIN_FUNC(areapercentheal)
  4715. {
  4716. int hp,sp,m;
  4717. const char *mapname;
  4718. int x0,y0,x1,y1;
  4719. mapname=script_getstr(st,2);
  4720. x0=script_getnum(st,3);
  4721. y0=script_getnum(st,4);
  4722. x1=script_getnum(st,5);
  4723. y1=script_getnum(st,6);
  4724. hp=script_getnum(st,7);
  4725. sp=script_getnum(st,8);
  4726. if( (m=map_mapname2mapid(mapname))< 0)
  4727. return 0;
  4728. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4729. return SCRIPT_CMD_SUCCESS;
  4730. }
  4731. /*==========================================
  4732. * warpchar [LuzZza]
  4733. * Useful for warp one player from
  4734. * another player npc-session.
  4735. * Using: warpchar "mapname",x,y,Char_ID;
  4736. *------------------------------------------*/
  4737. BUILDIN_FUNC(warpchar)
  4738. {
  4739. int x,y,a;
  4740. const char *str;
  4741. TBL_PC *sd;
  4742. str=script_getstr(st,2);
  4743. x=script_getnum(st,3);
  4744. y=script_getnum(st,4);
  4745. a=script_getnum(st,5);
  4746. sd = map_charid2sd(a);
  4747. if( sd == NULL )
  4748. return 0;
  4749. if(strcmp(str, "Random") == 0)
  4750. pc_randomwarp(sd, CLR_TELEPORT);
  4751. else
  4752. if(strcmp(str, "SavePoint") == 0)
  4753. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4754. else
  4755. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4756. return SCRIPT_CMD_SUCCESS;
  4757. }
  4758. /*==========================================
  4759. * Warpparty - [Fredzilla] [Paradox924X]
  4760. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4761. * If 'from_mapname' is specified, only the party members on that map will be warped
  4762. *------------------------------------------*/
  4763. BUILDIN_FUNC(warpparty)
  4764. {
  4765. TBL_PC *sd = NULL;
  4766. TBL_PC *pl_sd;
  4767. struct party_data* p;
  4768. int type;
  4769. int mapindex;
  4770. int i;
  4771. const char* str = script_getstr(st,2);
  4772. int x = script_getnum(st,3);
  4773. int y = script_getnum(st,4);
  4774. int p_id = script_getnum(st,5);
  4775. const char* str2 = NULL;
  4776. if ( script_hasdata(st,6) )
  4777. str2 = script_getstr(st,6);
  4778. p = party_search(p_id);
  4779. if(!p)
  4780. return 0;
  4781. type = ( strcmp(str,"Random")==0 ) ? 0
  4782. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4783. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4784. : ( strcmp(str,"Leader")==0 ) ? 3
  4785. : 4;
  4786. switch (type)
  4787. {
  4788. case 3:
  4789. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4790. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4791. return 0;
  4792. pl_sd = p->data[i].sd;
  4793. mapindex = pl_sd->mapindex;
  4794. x = pl_sd->bl.x;
  4795. y = pl_sd->bl.y;
  4796. break;
  4797. case 4:
  4798. mapindex = mapindex_name2id(str);
  4799. break;
  4800. case 2:
  4801. //"SavePoint" uses save point of the currently attached player
  4802. if (( sd = script_rid2sd(st) ) == NULL )
  4803. return 0;
  4804. default:
  4805. mapindex = 0;
  4806. break;
  4807. }
  4808. for (i = 0; i < MAX_PARTY; i++)
  4809. {
  4810. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  4811. continue;
  4812. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  4813. continue;
  4814. if( pc_isdead(pl_sd) )
  4815. continue;
  4816. switch( type )
  4817. {
  4818. case 0: // Random
  4819. if(!map[pl_sd->bl.m].flag.nowarp)
  4820. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4821. break;
  4822. case 1: // SavePointAll
  4823. if(!map[pl_sd->bl.m].flag.noreturn)
  4824. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4825. break;
  4826. case 2: // SavePoint
  4827. if(!map[pl_sd->bl.m].flag.noreturn)
  4828. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4829. break;
  4830. case 3: // Leader
  4831. case 4: // m,x,y
  4832. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4833. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  4834. break;
  4835. }
  4836. }
  4837. return SCRIPT_CMD_SUCCESS;
  4838. }
  4839. /*==========================================
  4840. * Warpguild - [Fredzilla]
  4841. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4842. *------------------------------------------*/
  4843. BUILDIN_FUNC(warpguild)
  4844. {
  4845. TBL_PC *sd = NULL;
  4846. TBL_PC *pl_sd;
  4847. struct guild* g;
  4848. struct s_mapiterator* iter;
  4849. int type;
  4850. const char* str = script_getstr(st,2);
  4851. int x = script_getnum(st,3);
  4852. int y = script_getnum(st,4);
  4853. int gid = script_getnum(st,5);
  4854. g = guild_search(gid);
  4855. if( g == NULL )
  4856. return 0;
  4857. type = ( strcmp(str,"Random")==0 ) ? 0
  4858. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4859. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4860. : 3;
  4861. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  4862. {// "SavePoint" uses save point of the currently attached player
  4863. return 0;
  4864. }
  4865. iter = mapit_getallusers();
  4866. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4867. {
  4868. if( pl_sd->status.guild_id != gid )
  4869. continue;
  4870. switch( type )
  4871. {
  4872. case 0: // Random
  4873. if(!map[pl_sd->bl.m].flag.nowarp)
  4874. pc_randomwarp(pl_sd,CLR_TELEPORT);
  4875. break;
  4876. case 1: // SavePointAll
  4877. if(!map[pl_sd->bl.m].flag.noreturn)
  4878. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  4879. break;
  4880. case 2: // SavePoint
  4881. if(!map[pl_sd->bl.m].flag.noreturn)
  4882. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4883. break;
  4884. case 3: // m,x,y
  4885. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4886. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  4887. break;
  4888. }
  4889. }
  4890. mapit_free(iter);
  4891. return SCRIPT_CMD_SUCCESS;
  4892. }
  4893. /*==========================================
  4894. * Force Heal a player (hp and sp)
  4895. *------------------------------------------*/
  4896. BUILDIN_FUNC(heal)
  4897. {
  4898. TBL_PC *sd;
  4899. int hp,sp;
  4900. sd = script_rid2sd(st);
  4901. if (!sd) return 0;
  4902. hp=script_getnum(st,2);
  4903. sp=script_getnum(st,3);
  4904. status_heal(&sd->bl, hp, sp, 1);
  4905. return SCRIPT_CMD_SUCCESS;
  4906. }
  4907. /*==========================================
  4908. * Heal a player by item (get vit bonus etc)
  4909. *------------------------------------------*/
  4910. BUILDIN_FUNC(itemheal)
  4911. {
  4912. TBL_PC *sd;
  4913. int hp,sp;
  4914. hp=script_getnum(st,2);
  4915. sp=script_getnum(st,3);
  4916. if(potion_flag==1) {
  4917. potion_hp = hp;
  4918. potion_sp = sp;
  4919. return 0;
  4920. }
  4921. sd = script_rid2sd(st);
  4922. if (!sd) return 0;
  4923. pc_itemheal(sd,sd->itemid,hp,sp);
  4924. return SCRIPT_CMD_SUCCESS;
  4925. }
  4926. /*==========================================
  4927. *
  4928. *------------------------------------------*/
  4929. BUILDIN_FUNC(percentheal)
  4930. {
  4931. int hp,sp;
  4932. TBL_PC* sd;
  4933. hp=script_getnum(st,2);
  4934. sp=script_getnum(st,3);
  4935. if(potion_flag==1) {
  4936. potion_per_hp = hp;
  4937. potion_per_sp = sp;
  4938. return 0;
  4939. }
  4940. sd = script_rid2sd(st);
  4941. if( sd == NULL )
  4942. return 0;
  4943. #ifdef RENEWAL
  4944. if( sd->sc.data[SC_EXTREMITYFIST2] )
  4945. sp = 0;
  4946. #endif
  4947. pc_percentheal(sd,hp,sp);
  4948. return SCRIPT_CMD_SUCCESS;
  4949. }
  4950. /*==========================================
  4951. *
  4952. *------------------------------------------*/
  4953. BUILDIN_FUNC(jobchange)
  4954. {
  4955. int job, upper=-1;
  4956. job=script_getnum(st,2);
  4957. if( script_hasdata(st,3) )
  4958. upper=script_getnum(st,3);
  4959. if (pcdb_checkid(job))
  4960. {
  4961. TBL_PC* sd;
  4962. sd = script_rid2sd(st);
  4963. if( sd == NULL )
  4964. return 0;
  4965. pc_jobchange(sd, job, upper);
  4966. }
  4967. return SCRIPT_CMD_SUCCESS;
  4968. }
  4969. /*==========================================
  4970. *
  4971. *------------------------------------------*/
  4972. BUILDIN_FUNC(jobname)
  4973. {
  4974. int class_=script_getnum(st,2);
  4975. script_pushconststr(st, (char*)job_name(class_));
  4976. return SCRIPT_CMD_SUCCESS;
  4977. }
  4978. /// Get input from the player.
  4979. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4980. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4981. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4982. /// shorter than 'min' and 0 otherwise.
  4983. ///
  4984. /// input(<var>{,<min>{,<max>}}) -> <int>
  4985. BUILDIN_FUNC(input)
  4986. {
  4987. TBL_PC* sd;
  4988. struct script_data* data;
  4989. int uid;
  4990. const char* name;
  4991. int min;
  4992. int max;
  4993. sd = script_rid2sd(st);
  4994. if( sd == NULL )
  4995. return 0;
  4996. data = script_getdata(st,2);
  4997. if( !data_isreference(data) ){
  4998. ShowError("script:input: not a variable\n");
  4999. script_reportdata(data);
  5000. st->state = END;
  5001. return 1;
  5002. }
  5003. uid = reference_getuid(data);
  5004. name = reference_getname(data);
  5005. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5006. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5007. #ifdef SECURE_NPCTIMEOUT
  5008. sd->npc_idle_type = NPCT_WAIT;
  5009. #endif
  5010. if( !sd->state.menu_or_input )
  5011. { // first invocation, display npc input box
  5012. sd->state.menu_or_input = 1;
  5013. st->state = RERUNLINE;
  5014. if( is_string_variable(name) )
  5015. clif_scriptinputstr(sd,st->oid);
  5016. else
  5017. clif_scriptinput(sd,st->oid);
  5018. }
  5019. else
  5020. { // take received text/value and store it in the designated variable
  5021. sd->state.menu_or_input = 0;
  5022. if( is_string_variable(name) )
  5023. {
  5024. int len = (int)strlen(sd->npc_str);
  5025. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5026. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5027. }
  5028. else
  5029. {
  5030. int amount = sd->npc_amount;
  5031. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5032. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5033. }
  5034. st->state = RUN;
  5035. }
  5036. return SCRIPT_CMD_SUCCESS;
  5037. }
  5038. // declare the copyarray method here for future reference
  5039. BUILDIN_FUNC(copyarray);
  5040. /// Sets the value of a variable.
  5041. /// The value is converted to the type of the variable.
  5042. ///
  5043. /// set(<variable>,<value>) -> <variable>
  5044. BUILDIN_FUNC(set)
  5045. {
  5046. TBL_PC* sd = NULL;
  5047. struct script_data* data;
  5048. //struct script_data* datavalue;
  5049. int num;
  5050. const char* name;
  5051. char prefix;
  5052. data = script_getdata(st,2);
  5053. //datavalue = script_getdata(st,3);
  5054. if( !data_isreference(data) )
  5055. {
  5056. ShowError("script:set: not a variable\n");
  5057. script_reportdata(script_getdata(st,2));
  5058. st->state = END;
  5059. return 1;
  5060. }
  5061. num = reference_getuid(data);
  5062. name = reference_getname(data);
  5063. prefix = *name;
  5064. if( not_server_variable(prefix) )
  5065. {
  5066. sd = script_rid2sd(st);
  5067. if( sd == NULL )
  5068. {
  5069. ShowError("script:set: no player attached for player variable '%s'\n", name);
  5070. return 0;
  5071. }
  5072. }
  5073. #if 0
  5074. if( data_isreference(datavalue) )
  5075. {// the value being referenced is a variable
  5076. const char* namevalue = reference_getname(datavalue);
  5077. if( !not_array_variable(*namevalue) )
  5078. {// array variable being copied into another array variable
  5079. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5080. {// player must be attached in order to copy a player variable
  5081. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5082. return 0;
  5083. }
  5084. if( is_string_variable(namevalue) != is_string_variable(name) )
  5085. {// non-matching array value types
  5086. ShowWarning("script:set: two array variables do not match in type.\n");
  5087. return 0;
  5088. }
  5089. // push the maximum number of array values to the stack
  5090. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5091. // call the copy array method directly
  5092. return buildin_copyarray(st);
  5093. }
  5094. }
  5095. #endif
  5096. if( is_string_variable(name) )
  5097. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5098. else
  5099. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5100. // return a copy of the variable reference
  5101. script_pushcopy(st,2);
  5102. return SCRIPT_CMD_SUCCESS;
  5103. }
  5104. /////////////////////////////////////////////////////////////////////
  5105. /// Array variables
  5106. ///
  5107. /// Returns the size of the specified array
  5108. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
  5109. {
  5110. int32 ret = idx;
  5111. if( isstring )
  5112. {
  5113. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5114. {
  5115. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  5116. if( str && *str )
  5117. ret = idx + 1;
  5118. script_removetop(st, -1, 0);
  5119. }
  5120. }
  5121. else
  5122. {
  5123. for( ; idx < SCRIPT_MAX_ARRAYSIZE; ++idx )
  5124. {
  5125. int32 num = (int32)__64BPRTSIZE(get_val2(st, reference_uid(id, idx), ref));
  5126. if( num )
  5127. ret = idx + 1;
  5128. script_removetop(st, -1, 0);
  5129. }
  5130. }
  5131. return ret;
  5132. }
  5133. /// Sets values of an array, from the starting index.
  5134. /// ex: setarray arr[1],1,2,3;
  5135. ///
  5136. /// setarray <array variable>,<value1>{,<value2>...};
  5137. BUILDIN_FUNC(setarray)
  5138. {
  5139. struct script_data* data;
  5140. const char* name;
  5141. int32 start;
  5142. int32 end;
  5143. int32 id;
  5144. int32 i;
  5145. TBL_PC* sd = NULL;
  5146. data = script_getdata(st, 2);
  5147. if( !data_isreference(data) )
  5148. {
  5149. ShowError("script:setarray: not a variable\n");
  5150. script_reportdata(data);
  5151. st->state = END;
  5152. return 1;// not a variable
  5153. }
  5154. id = reference_getid(data);
  5155. start = reference_getindex(data);
  5156. name = reference_getname(data);
  5157. if( not_array_variable(*name) )
  5158. {
  5159. ShowError("script:setarray: illegal scope\n");
  5160. script_reportdata(data);
  5161. st->state = END;
  5162. return 1;// not supported
  5163. }
  5164. if( not_server_variable(*name) )
  5165. {
  5166. sd = script_rid2sd(st);
  5167. if( sd == NULL )
  5168. return 0;// no player attached
  5169. }
  5170. end = start + script_lastdata(st) - 2;
  5171. if( end > SCRIPT_MAX_ARRAYSIZE )
  5172. end = SCRIPT_MAX_ARRAYSIZE;
  5173. if( is_string_variable(name) )
  5174. {// string array
  5175. for( i = 3; start < end; ++start, ++i )
  5176. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5177. }
  5178. else
  5179. {// int array
  5180. for( i = 3; start < end; ++start, ++i )
  5181. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5182. }
  5183. return SCRIPT_CMD_SUCCESS;
  5184. }
  5185. /// Sets count values of an array, from the starting index.
  5186. /// ex: cleararray arr[0],0,1;
  5187. ///
  5188. /// cleararray <array variable>,<value>,<count>;
  5189. BUILDIN_FUNC(cleararray)
  5190. {
  5191. struct script_data* data;
  5192. const char* name;
  5193. int32 start;
  5194. int32 end;
  5195. int32 id;
  5196. void* v;
  5197. TBL_PC* sd = NULL;
  5198. data = script_getdata(st, 2);
  5199. if( !data_isreference(data) )
  5200. {
  5201. ShowError("script:cleararray: not a variable\n");
  5202. script_reportdata(data);
  5203. st->state = END;
  5204. return 1;// not a variable
  5205. }
  5206. id = reference_getid(data);
  5207. start = reference_getindex(data);
  5208. name = reference_getname(data);
  5209. if( not_array_variable(*name) )
  5210. {
  5211. ShowError("script:cleararray: illegal scope\n");
  5212. script_reportdata(data);
  5213. st->state = END;
  5214. return 1;// not supported
  5215. }
  5216. if( not_server_variable(*name) )
  5217. {
  5218. sd = script_rid2sd(st);
  5219. if( sd == NULL )
  5220. return 0;// no player attached
  5221. }
  5222. if( is_string_variable(name) )
  5223. v = (void*)script_getstr(st, 3);
  5224. else
  5225. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5226. end = start + script_getnum(st, 4);
  5227. if( end > SCRIPT_MAX_ARRAYSIZE )
  5228. end = SCRIPT_MAX_ARRAYSIZE;
  5229. for( ; start < end; ++start )
  5230. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5231. return SCRIPT_CMD_SUCCESS;
  5232. }
  5233. /// Copies data from one array to another.
  5234. /// ex: copyarray arr[0],arr[2],2;
  5235. ///
  5236. /// copyarray <destination array variable>,<source array variable>,<count>;
  5237. BUILDIN_FUNC(copyarray)
  5238. {
  5239. struct script_data* data1;
  5240. struct script_data* data2;
  5241. const char* name1;
  5242. const char* name2;
  5243. int32 idx1;
  5244. int32 idx2;
  5245. int32 id1;
  5246. int32 id2;
  5247. void* v;
  5248. int32 i;
  5249. int32 count;
  5250. TBL_PC* sd = NULL;
  5251. data1 = script_getdata(st, 2);
  5252. data2 = script_getdata(st, 3);
  5253. if( !data_isreference(data1) || !data_isreference(data2) )
  5254. {
  5255. ShowError("script:copyarray: not a variable\n");
  5256. script_reportdata(data1);
  5257. script_reportdata(data2);
  5258. st->state = END;
  5259. return 1;// not a variable
  5260. }
  5261. id1 = reference_getid(data1);
  5262. id2 = reference_getid(data2);
  5263. idx1 = reference_getindex(data1);
  5264. idx2 = reference_getindex(data2);
  5265. name1 = reference_getname(data1);
  5266. name2 = reference_getname(data2);
  5267. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5268. {
  5269. ShowError("script:copyarray: illegal scope\n");
  5270. script_reportdata(data1);
  5271. script_reportdata(data2);
  5272. st->state = END;
  5273. return 1;// not supported
  5274. }
  5275. if( is_string_variable(name1) != is_string_variable(name2) )
  5276. {
  5277. ShowError("script:copyarray: type mismatch\n");
  5278. script_reportdata(data1);
  5279. script_reportdata(data2);
  5280. st->state = END;
  5281. return 1;// data type mismatch
  5282. }
  5283. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5284. {
  5285. sd = script_rid2sd(st);
  5286. if( sd == NULL )
  5287. return 0;// no player attached
  5288. }
  5289. count = script_getnum(st, 4);
  5290. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5291. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5292. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5293. return 0;// nothing to copy
  5294. if( id1 == id2 && idx1 > idx2 )
  5295. {// destination might be overlapping the source - copy in reverse order
  5296. for( i = count - 1; i >= 0; --i )
  5297. {
  5298. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5299. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5300. script_removetop(st, -1, 0);
  5301. }
  5302. }
  5303. else
  5304. {// normal copy
  5305. for( i = 0; i < count; ++i )
  5306. {
  5307. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5308. {
  5309. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5310. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5311. script_removetop(st, -1, 0);
  5312. }
  5313. else// out of range - assume ""/0
  5314. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5315. }
  5316. }
  5317. return SCRIPT_CMD_SUCCESS;
  5318. }
  5319. /// Returns the size of the array.
  5320. /// Assumes that everything before the starting index exists.
  5321. /// ex: getarraysize(arr[3])
  5322. ///
  5323. /// getarraysize(<array variable>) -> <int>
  5324. BUILDIN_FUNC(getarraysize)
  5325. {
  5326. struct script_data* data;
  5327. const char* name;
  5328. data = script_getdata(st, 2);
  5329. if( !data_isreference(data) )
  5330. {
  5331. ShowError("script:getarraysize: not a variable\n");
  5332. script_reportdata(data);
  5333. script_pushnil(st);
  5334. st->state = END;
  5335. return 1;// not a variable
  5336. }
  5337. name = reference_getname(data);
  5338. if( not_array_variable(*name) )
  5339. {
  5340. ShowError("script:getarraysize: illegal scope\n");
  5341. script_reportdata(data);
  5342. script_pushnil(st);
  5343. st->state = END;
  5344. return 1;// not supported
  5345. }
  5346. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5347. return SCRIPT_CMD_SUCCESS;
  5348. }
  5349. /// Deletes count or all the elements in an array, from the starting index.
  5350. /// ex: deletearray arr[4],2;
  5351. ///
  5352. /// deletearray <array variable>;
  5353. /// deletearray <array variable>,<count>;
  5354. BUILDIN_FUNC(deletearray)
  5355. {
  5356. struct script_data* data;
  5357. const char* name;
  5358. int start;
  5359. int end;
  5360. int id;
  5361. TBL_PC *sd = NULL;
  5362. data = script_getdata(st, 2);
  5363. if( !data_isreference(data) )
  5364. {
  5365. ShowError("script:deletearray: not a variable\n");
  5366. script_reportdata(data);
  5367. st->state = END;
  5368. return 1;// not a variable
  5369. }
  5370. id = reference_getid(data);
  5371. start = reference_getindex(data);
  5372. name = reference_getname(data);
  5373. if( not_array_variable(*name) )
  5374. {
  5375. ShowError("script:deletearray: illegal scope\n");
  5376. script_reportdata(data);
  5377. st->state = END;
  5378. return 1;// not supported
  5379. }
  5380. if( not_server_variable(*name) )
  5381. {
  5382. sd = script_rid2sd(st);
  5383. if( sd == NULL )
  5384. return 0;// no player attached
  5385. }
  5386. end = SCRIPT_MAX_ARRAYSIZE;
  5387. if( start >= end )
  5388. return 0;// nothing to free
  5389. if( script_hasdata(st,3) )
  5390. {
  5391. int count = script_getnum(st, 3);
  5392. if( count > end - start )
  5393. count = end - start;
  5394. if( count <= 0 )
  5395. return 0;// nothing to free
  5396. // move rest of the elements backward
  5397. for( ; start + count < end; ++start )
  5398. {
  5399. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5400. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5401. script_removetop(st, -1, 0);
  5402. }
  5403. }
  5404. // clear the rest of the array
  5405. if( is_string_variable(name) )
  5406. {
  5407. for( ; start < end; ++start )
  5408. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5409. }
  5410. else
  5411. {
  5412. for( ; start < end; ++start )
  5413. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5414. }
  5415. return SCRIPT_CMD_SUCCESS;
  5416. }
  5417. /// Returns a reference to the target index of the array variable.
  5418. /// Equivalent to var[index].
  5419. ///
  5420. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5421. BUILDIN_FUNC(getelementofarray)
  5422. {
  5423. struct script_data* data;
  5424. const char* name;
  5425. int32 id;
  5426. int i;
  5427. data = script_getdata(st, 2);
  5428. if( !data_isreference(data) )
  5429. {
  5430. ShowError("script:getelementofarray: not a variable\n");
  5431. script_reportdata(data);
  5432. script_pushnil(st);
  5433. st->state = END;
  5434. return 1;// not a variable
  5435. }
  5436. id = reference_getid(data);
  5437. name = reference_getname(data);
  5438. if( not_array_variable(*name) )
  5439. {
  5440. ShowError("script:getelementofarray: illegal scope\n");
  5441. script_reportdata(data);
  5442. script_pushnil(st);
  5443. st->state = END;
  5444. return 1;// not supported
  5445. }
  5446. i = script_getnum(st, 3);
  5447. if( i < 0 || i >= SCRIPT_MAX_ARRAYSIZE )
  5448. {
  5449. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5450. script_reportdata(data);
  5451. script_pushnil(st);
  5452. st->state = END;
  5453. return 1;// out of range
  5454. }
  5455. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5456. return SCRIPT_CMD_SUCCESS;
  5457. }
  5458. /////////////////////////////////////////////////////////////////////
  5459. /// ...
  5460. ///
  5461. /*==========================================
  5462. *
  5463. *------------------------------------------*/
  5464. BUILDIN_FUNC(setlook)
  5465. {
  5466. int type,val;
  5467. TBL_PC* sd;
  5468. type=script_getnum(st,2);
  5469. val=script_getnum(st,3);
  5470. sd = script_rid2sd(st);
  5471. if( sd == NULL )
  5472. return 0;
  5473. pc_changelook(sd,type,val);
  5474. return SCRIPT_CMD_SUCCESS;
  5475. }
  5476. BUILDIN_FUNC(changelook)
  5477. { // As setlook but only client side
  5478. int type,val;
  5479. TBL_PC* sd;
  5480. type=script_getnum(st,2);
  5481. val=script_getnum(st,3);
  5482. sd = script_rid2sd(st);
  5483. if( sd == NULL )
  5484. return 0;
  5485. clif_changelook(&sd->bl,type,val);
  5486. return SCRIPT_CMD_SUCCESS;
  5487. }
  5488. /*==========================================
  5489. *
  5490. *------------------------------------------*/
  5491. BUILDIN_FUNC(cutin)
  5492. {
  5493. TBL_PC* sd;
  5494. sd = script_rid2sd(st);
  5495. if( sd == NULL )
  5496. return 0;
  5497. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5498. return SCRIPT_CMD_SUCCESS;
  5499. }
  5500. /*==========================================
  5501. *
  5502. *------------------------------------------*/
  5503. BUILDIN_FUNC(viewpoint)
  5504. {
  5505. int type,x,y,id,color;
  5506. TBL_PC* sd;
  5507. type=script_getnum(st,2);
  5508. x=script_getnum(st,3);
  5509. y=script_getnum(st,4);
  5510. id=script_getnum(st,5);
  5511. color=script_getnum(st,6);
  5512. sd = script_rid2sd(st);
  5513. if( sd == NULL )
  5514. return 0;
  5515. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5516. return SCRIPT_CMD_SUCCESS;
  5517. }
  5518. /// Returns number of items in inventory/cart/storage
  5519. /// countitem <nameID>{,<accountID>});
  5520. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5521. /// cartcountitem <nameID>{,<accountID>});
  5522. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5523. /// storagecountitem <nameID>{,<accountID>});
  5524. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5525. BUILDIN_FUNC(countitem)
  5526. {
  5527. int i = 0, count = 0, aid = 3;
  5528. struct item_data* id = NULL;
  5529. struct script_data* data;
  5530. char *command = (char *)script_getfuncname(st);
  5531. uint8 loc = 0;
  5532. uint16 size;
  5533. struct item *items;
  5534. TBL_PC *sd = NULL;
  5535. if( command[strlen(command)-1] == '2' ) {
  5536. i = 1;
  5537. aid = 10;
  5538. }
  5539. if( script_hasdata(st,aid) ) {
  5540. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5541. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5542. st->state = END;
  5543. return SCRIPT_CMD_FAILURE;
  5544. }
  5545. }
  5546. else {
  5547. if( !(sd = script_rid2sd(st)) )
  5548. return SCRIPT_CMD_SUCCESS;
  5549. }
  5550. if( !strncmp(command, "cart", 4) ) {
  5551. loc = 1;
  5552. size = MAX_CART;
  5553. items = sd->status.cart;
  5554. }
  5555. else if( !strncmp(command, "storage", 7) ) {
  5556. loc = 2;
  5557. size = MAX_STORAGE;
  5558. items = sd->status.storage.items;
  5559. }
  5560. //TODO: 3 - Guild Storage
  5561. else {
  5562. size = MAX_INVENTORY;
  5563. items = sd->status.inventory;
  5564. }
  5565. if( loc == 1 && !pc_iscarton(sd) ) {
  5566. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5567. script_pushint(st,-1);
  5568. return SCRIPT_CMD_SUCCESS;
  5569. }
  5570. data = script_getdata(st, 2);
  5571. get_val(st, data); // Convert into value in case of a variable
  5572. if( data_isstring(data) ) // item name
  5573. id = itemdb_searchname(conv_str(st, data));
  5574. else // item id
  5575. id = itemdb_exists(conv_num(st, data));
  5576. if( id == NULL ) {
  5577. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5578. script_pushint(st,0);
  5579. return 1;
  5580. }
  5581. if( !i ) { // For count/cart/storagecountitem function
  5582. unsigned short nameid = id->nameid;
  5583. for( i = 0; i < size; i++ )
  5584. if( &items[i] && items[i].nameid == nameid )
  5585. count += items[i].amount;
  5586. }
  5587. else { // For count/cart/storagecountitem2 function
  5588. unsigned short nameid;
  5589. int iden, ref, attr, c1, c2, c3, c4;
  5590. nameid = id->nameid;
  5591. iden = script_getnum(st,3);
  5592. ref = script_getnum(st,4);
  5593. attr = script_getnum(st,5);
  5594. c1 = script_getnum(st,6);
  5595. c2 = script_getnum(st,7);
  5596. c3 = script_getnum(st,8);
  5597. c4 = script_getnum(st,9);
  5598. for( i = 0; i < size; i++ )
  5599. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5600. items[i].amount > 0 && items[i].identify == iden &&
  5601. items[i].refine == ref && items[i].attribute == attr &&
  5602. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5603. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5604. {
  5605. count += items[i].amount;
  5606. }
  5607. }
  5608. script_pushint(st, count);
  5609. return SCRIPT_CMD_SUCCESS;
  5610. }
  5611. int checkweight_sub(TBL_PC *sd, int nbargs, unsigned short *eitemid, int32 *eamount)
  5612. {
  5613. struct item_data* id = NULL;
  5614. unsigned short nameid;
  5615. int amount;
  5616. uint32 weight=0;
  5617. uint16 amount2=0,slots,i;
  5618. slots = pc_inventoryblank(sd); //nb of empty slot
  5619. for(i=0; i<nbargs; i++) {
  5620. if(!eitemid[i])
  5621. continue;
  5622. id = itemdb_exists(eitemid[i]);
  5623. if( id == NULL ) {
  5624. ShowError("checkweight_sub: Invalid item '%hu'.\n", eitemid[i]);
  5625. return 0;
  5626. }
  5627. nameid = id->nameid;
  5628. amount = eamount[i];
  5629. if( amount < 1 ) {
  5630. ShowError("checkweight_sub: Invalid amount '%d'.\n", eamount[i]);
  5631. return 0;
  5632. }
  5633. weight += (id->weight)*amount; //total weight for all chk
  5634. if( weight + sd->weight > sd->max_weight ) // too heavy
  5635. return 0;
  5636. switch( pc_checkadditem(sd, nameid, amount) ) {
  5637. case CHKADDITEM_EXIST: // item is already in inventory, but there is still space for the requested amount
  5638. break;
  5639. case CHKADDITEM_NEW:
  5640. if( itemdb_isstackable(nameid) )
  5641. amount2++; // stackable
  5642. else
  5643. amount2 += amount; // non-stackable
  5644. if( slots < amount2)
  5645. return 0;
  5646. break;
  5647. case CHKADDITEM_OVERAMOUNT:
  5648. return 0;
  5649. }
  5650. }
  5651. return 1;
  5652. }
  5653. /*==========================================
  5654. * Check if item with this amount can fit in inventory
  5655. * Checking : weight, stack amount >32k, slots amount >(MAX_INVENTORY)
  5656. * Return
  5657. * 0 : fail
  5658. * 1 : success (npc side only)
  5659. *------------------------------------------*/
  5660. BUILDIN_FUNC(checkweight)
  5661. {
  5662. struct map_session_data* sd;
  5663. struct item_data* id = NULL;
  5664. unsigned short nameid[SCRIPT_MAX_ARRAYSIZE];
  5665. int32 amount[SCRIPT_MAX_ARRAYSIZE];
  5666. uint16 nbargs,i,j=0;
  5667. if( ( sd = script_rid2sd(st) ) == NULL )
  5668. return 0;
  5669. nbargs = script_lastdata(st)+1;
  5670. if(nbargs%2) {
  5671. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5672. script_pushint(st,0);
  5673. return 1;
  5674. }
  5675. for(i=2; i<nbargs; i=i+2) {
  5676. struct script_data* data = script_getdata(st,i);
  5677. get_val(st, data); // Convert into value in case of a variable
  5678. if( data_isstring(data) ) // item name
  5679. id = itemdb_searchname(conv_str(st, data));
  5680. else // item id
  5681. id = itemdb_exists(conv_num(st, data));
  5682. if( id == NULL ) {
  5683. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5684. script_pushint(st,0);
  5685. return 1;
  5686. }
  5687. nameid[j] = id->nameid;
  5688. amount[j] = script_getnum(st,i+1);
  5689. j++;
  5690. }
  5691. script_pushint(st,checkweight_sub(sd,(nbargs-2)/2,nameid,amount));
  5692. return SCRIPT_CMD_SUCCESS;
  5693. }
  5694. BUILDIN_FUNC(checkweight2){
  5695. //variable sub checkweight
  5696. unsigned short nameid[SCRIPT_MAX_ARRAYSIZE];
  5697. int32 amount[SCRIPT_MAX_ARRAYSIZE], i;
  5698. //variable for array parsing
  5699. struct script_data* data_it;
  5700. struct script_data* data_nb;
  5701. const char* name_it;
  5702. const char* name_nb;
  5703. int32 id_it, id_nb;
  5704. int32 idx_it, idx_nb;
  5705. int nb_it, nb_nb; //array size
  5706. TBL_PC *sd = script_rid2sd(st);
  5707. nullpo_retr(1,sd);
  5708. data_it = script_getdata(st, 2);
  5709. data_nb = script_getdata(st, 3);
  5710. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5711. ShowError("buildin_checkweight2: parameter not a variable\n");
  5712. script_pushint(st,0);
  5713. return 1;// not a variable
  5714. }
  5715. id_it = reference_getid(data_it);
  5716. id_nb = reference_getid(data_nb);
  5717. idx_it = reference_getindex(data_it);
  5718. idx_nb = reference_getindex(data_nb);
  5719. name_it = reference_getname(data_it);
  5720. name_nb = reference_getname(data_nb);
  5721. if( not_array_variable(*name_it) || not_array_variable(*name_nb)) {
  5722. ShowError("buildin_checkweight2: illegal scope\n");
  5723. script_pushint(st,0);
  5724. return 1;// not supported
  5725. }
  5726. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5727. ShowError("buildin_checkweight2: illegal type, need int\n");
  5728. script_pushint(st,0);
  5729. return 1;// not supported
  5730. }
  5731. nb_it = getarraysize(st, id_it, idx_it, 0, reference_getref(data_it));
  5732. nb_nb = getarraysize(st, id_nb, idx_nb, 0, reference_getref(data_nb));
  5733. if(nb_it != nb_nb) {
  5734. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5735. script_pushint(st,0);
  5736. return 1;
  5737. }
  5738. for(i=0; i<nb_it; i++) {
  5739. nameid[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5740. script_removetop(st, -1, 0);
  5741. amount[i] = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5742. script_removetop(st, -1, 0);
  5743. } //end loop DO NOT break it prematurly we need to depop all stack
  5744. script_pushint(st,checkweight_sub(sd,nb_it,nameid,amount)); //push result of sub to script
  5745. return SCRIPT_CMD_SUCCESS;
  5746. }
  5747. /*==========================================
  5748. * getitem <item id>,<amount>{,<account ID>};
  5749. * getitem "<item name>",<amount>{,<account ID>};
  5750. *
  5751. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5752. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5753. * Type:
  5754. * 0 - No bound
  5755. * 1 - Account Bound
  5756. * 2 - Guild Bound
  5757. * 3 - Party Bound
  5758. * 4 - Character Bound
  5759. *------------------------------------------*/
  5760. BUILDIN_FUNC(getitem)
  5761. {
  5762. int amount, get_count, i;
  5763. unsigned short nameid;
  5764. struct item it;
  5765. TBL_PC *sd;
  5766. struct script_data *data;
  5767. unsigned char flag = 0;
  5768. const char* command = script_getfuncname(st);
  5769. struct item_data *id = NULL;
  5770. data = script_getdata(st,2);
  5771. get_val(st,data);
  5772. if( data_isstring(data) ) {// "<item name>"
  5773. const char *name = conv_str(st,data);
  5774. id = itemdb_searchname(name);
  5775. if( id == NULL ){
  5776. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5777. return SCRIPT_CMD_FAILURE; //No item created.
  5778. }
  5779. nameid = id->nameid;
  5780. } else if( data_isint(data) ) {// <item id>
  5781. nameid = conv_num(st,data);
  5782. if( !(id = itemdb_exists(nameid)) ){
  5783. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5784. return SCRIPT_CMD_FAILURE; //No item created.
  5785. }
  5786. } else {
  5787. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5788. return SCRIPT_CMD_FAILURE;
  5789. }
  5790. // <amount>
  5791. if( (amount = script_getnum(st,3)) <= 0)
  5792. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  5793. memset(&it,0,sizeof(it));
  5794. it.nameid = nameid;
  5795. it.identify = 1;
  5796. it.bound = BOUND_NONE;
  5797. if( !strcmp(command,"getitembound") ) {
  5798. char bound = script_getnum(st,4);
  5799. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5800. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5801. return SCRIPT_CMD_FAILURE;
  5802. }
  5803. if( script_hasdata(st,5) )
  5804. sd = map_id2sd(script_getnum(st,5));
  5805. else
  5806. sd = script_rid2sd(st); // Attached player
  5807. it.bound = bound;
  5808. } else if( script_hasdata(st,4) )
  5809. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  5810. else
  5811. sd = script_rid2sd(st); // Attached player
  5812. if( sd == NULL ) // no target
  5813. return SCRIPT_CMD_SUCCESS;
  5814. //Check if it's stackable.
  5815. if (!itemdb_isstackable2(id))
  5816. get_count = 1;
  5817. else
  5818. get_count = amount;
  5819. for (i = 0; i < amount; i += get_count)
  5820. {
  5821. // if not pet egg
  5822. if (!pet_create_egg(sd, nameid))
  5823. {
  5824. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  5825. {
  5826. clif_additem(sd, 0, 0, flag);
  5827. if( pc_candrop(sd,&it) )
  5828. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5829. }
  5830. }
  5831. }
  5832. return SCRIPT_CMD_SUCCESS;
  5833. }
  5834. /*==========================================
  5835. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  5836. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  5837. *
  5838. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  5839. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  5840. * Type:
  5841. * 0 - No bound
  5842. * 1 - Account Bound
  5843. * 2 - Guild Bound
  5844. * 3 - Party Bound
  5845. * 4 - Character Bound
  5846. *------------------------------------------*/
  5847. BUILDIN_FUNC(getitem2)
  5848. {
  5849. int amount;
  5850. unsigned short nameid;
  5851. int iden, ref, attr;
  5852. unsigned short c1, c2, c3, c4;
  5853. char bound = BOUND_NONE;
  5854. struct item_data *item_data = NULL;
  5855. struct item item_tmp;
  5856. TBL_PC *sd;
  5857. struct script_data *data;
  5858. const char* command = script_getfuncname(st);
  5859. if( !strcmp(command,"getitembound2") ) {
  5860. bound = script_getnum(st,11);
  5861. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5862. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  5863. return SCRIPT_CMD_FAILURE;
  5864. }
  5865. if( script_hasdata(st,12) )
  5866. sd = map_id2sd(script_getnum(st,12));
  5867. else
  5868. sd = script_rid2sd(st); // Attached player
  5869. } else if( script_hasdata(st,11) )
  5870. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  5871. else
  5872. sd = script_rid2sd(st); // Attached player
  5873. if( sd == NULL ) // no target
  5874. return SCRIPT_CMD_SUCCESS;
  5875. data = script_getdata(st,2);
  5876. get_val(st,data);
  5877. if( data_isstring(data) ) {
  5878. const char *name = conv_str(st,data);
  5879. if( (item_data = itemdb_searchname(name)) == NULL ){
  5880. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  5881. return SCRIPT_CMD_FAILURE; //No item created.
  5882. }
  5883. nameid = item_data->nameid;
  5884. } else {
  5885. nameid = conv_num(st,data);
  5886. if( (item_data = itemdb_exists(nameid)) == NULL ){
  5887. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  5888. return SCRIPT_CMD_FAILURE; //No item created.
  5889. }
  5890. }
  5891. amount = script_getnum(st,3);
  5892. iden = script_getnum(st,4);
  5893. ref = script_getnum(st,5);
  5894. attr = script_getnum(st,6);
  5895. c1 = (unsigned short)script_getnum(st,7);
  5896. c2 = (unsigned short)script_getnum(st,8);
  5897. c3 = (unsigned short)script_getnum(st,9);
  5898. c4 = (unsigned short)script_getnum(st,10);
  5899. if( item_data ) {
  5900. int get_count = 0, i;
  5901. memset(&item_tmp,0,sizeof(item_tmp));
  5902. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  5903. if(ref > MAX_REFINE)
  5904. ref = MAX_REFINE;
  5905. }
  5906. else if( item_data->type == IT_PETEGG ) {
  5907. iden = 1;
  5908. ref = 0;
  5909. }
  5910. else {
  5911. iden = 1;
  5912. ref = attr = 0;
  5913. }
  5914. item_tmp.nameid = nameid;
  5915. item_tmp.identify = iden;
  5916. item_tmp.refine = ref;
  5917. item_tmp.attribute = attr;
  5918. item_tmp.card[0] = c1;
  5919. item_tmp.card[1] = c2;
  5920. item_tmp.card[2] = c3;
  5921. item_tmp.card[3] = c4;
  5922. item_tmp.bound = bound;
  5923. //Check if it's stackable.
  5924. if (!itemdb_isstackable2(item_data))
  5925. get_count = 1;
  5926. else
  5927. get_count = amount;
  5928. for (i = 0; i < amount; i += get_count)
  5929. {
  5930. // if not pet egg
  5931. if (!pet_create_egg(sd, nameid))
  5932. {
  5933. unsigned char flag = 0;
  5934. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  5935. {
  5936. clif_additem(sd, 0, 0, flag);
  5937. if( pc_candrop(sd,&item_tmp) )
  5938. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  5939. }
  5940. }
  5941. }
  5942. }
  5943. return SCRIPT_CMD_SUCCESS;
  5944. }
  5945. /** Gives rental item to player
  5946. * rentitem <item id>,<seconds>
  5947. * rentitem "<item name>",<seconds>
  5948. */
  5949. BUILDIN_FUNC(rentitem) {
  5950. struct map_session_data *sd;
  5951. struct script_data *data;
  5952. struct item it;
  5953. int seconds;
  5954. unsigned short nameid = 0;
  5955. unsigned char flag = 0;
  5956. data = script_getdata(st,2);
  5957. get_val(st,data);
  5958. if( (sd = script_rid2sd(st)) == NULL )
  5959. return SCRIPT_CMD_SUCCESS;
  5960. if( data_isstring(data) )
  5961. {
  5962. const char *name = conv_str(st,data);
  5963. struct item_data *itd = itemdb_searchname(name);
  5964. if( itd == NULL )
  5965. {
  5966. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  5967. return SCRIPT_CMD_FAILURE;
  5968. }
  5969. nameid = itd->nameid;
  5970. }
  5971. else if( data_isint(data) )
  5972. {
  5973. nameid = conv_num(st,data);
  5974. if( nameid == 0 || !itemdb_exists(nameid) )
  5975. {
  5976. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  5977. return SCRIPT_CMD_FAILURE;
  5978. }
  5979. }
  5980. else
  5981. {
  5982. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  5983. return SCRIPT_CMD_FAILURE;
  5984. }
  5985. seconds = script_getnum(st,3);
  5986. memset(&it, 0, sizeof(it));
  5987. it.nameid = nameid;
  5988. it.identify = 1;
  5989. it.expire_time = (unsigned int)(time(NULL) + seconds);
  5990. it.bound = BOUND_NONE;
  5991. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  5992. {
  5993. clif_additem(sd, 0, 0, flag);
  5994. return SCRIPT_CMD_FAILURE;
  5995. }
  5996. return SCRIPT_CMD_SUCCESS;
  5997. }
  5998. /** Gives rental item to player with advanced option
  5999. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6000. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6001. */
  6002. BUILDIN_FUNC(rentitem2) {
  6003. struct map_session_data *sd;
  6004. struct script_data *data;
  6005. struct item it;
  6006. struct item_data *id;
  6007. int seconds;
  6008. unsigned short nameid = 0;
  6009. unsigned char flag = 0;
  6010. int iden,ref,attr,c1,c2,c3,c4;
  6011. data = script_getdata(st,2);
  6012. get_val(st,data);
  6013. if( (sd = script_rid2sd(st)) == NULL )
  6014. return SCRIPT_CMD_SUCCESS;
  6015. if( data_isstring(data) ) {
  6016. const char *name = conv_str(st,data);
  6017. id = itemdb_searchname(name);
  6018. if( id == NULL ) {
  6019. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6020. return SCRIPT_CMD_FAILURE;
  6021. }
  6022. nameid = id->nameid;
  6023. }
  6024. else if( data_isint(data) ) {
  6025. nameid = conv_num(st,data);
  6026. if( !(id = itemdb_search(nameid))) {
  6027. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6028. return SCRIPT_CMD_FAILURE;
  6029. }
  6030. }
  6031. else {
  6032. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6033. return SCRIPT_CMD_FAILURE;
  6034. }
  6035. seconds = script_getnum(st,3);
  6036. iden = script_getnum(st,4);
  6037. ref = script_getnum(st,5);
  6038. attr = script_getnum(st,6);
  6039. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6040. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6041. }
  6042. else if (id->type==IT_PETEGG) {
  6043. iden = 1;
  6044. ref = 0;
  6045. }
  6046. else {
  6047. iden = 1;
  6048. ref = attr = 0;
  6049. }
  6050. c1 = (short)script_getnum(st,7);
  6051. c2 = (short)script_getnum(st,8);
  6052. c3 = (short)script_getnum(st,9);
  6053. c4 = (short)script_getnum(st,10);
  6054. memset(&it, 0, sizeof(it));
  6055. it.nameid = nameid;
  6056. it.identify = iden;
  6057. it.refine = ref;
  6058. it.attribute = attr;
  6059. it.card[0] = (short)c1;
  6060. it.card[1] = (short)c2;
  6061. it.card[2] = (short)c3;
  6062. it.card[3] = (short)c4;
  6063. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6064. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6065. clif_additem(sd, 0, 0, flag);
  6066. return SCRIPT_CMD_FAILURE;
  6067. }
  6068. return SCRIPT_CMD_SUCCESS;
  6069. }
  6070. /*==========================================
  6071. * gets an item with someone's name inscribed [Skotlex]
  6072. * getinscribeditem item_num, character_name
  6073. * Returned Qty is always 1, only works on equip-able
  6074. * equipment
  6075. *------------------------------------------*/
  6076. BUILDIN_FUNC(getnameditem)
  6077. {
  6078. unsigned short nameid;
  6079. struct item item_tmp;
  6080. TBL_PC *sd, *tsd;
  6081. struct script_data *data;
  6082. sd = script_rid2sd(st);
  6083. if (sd == NULL)
  6084. { //Player not attached!
  6085. script_pushint(st,0);
  6086. return 0;
  6087. }
  6088. data=script_getdata(st,2);
  6089. get_val(st,data);
  6090. if( data_isstring(data) ){
  6091. const char *name=conv_str(st,data);
  6092. struct item_data *item_data = itemdb_searchname(name);
  6093. if( item_data == NULL)
  6094. { //Failed
  6095. script_pushint(st,0);
  6096. return 0;
  6097. }
  6098. nameid = item_data->nameid;
  6099. }else
  6100. nameid = conv_num(st,data);
  6101. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6102. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6103. script_pushint(st,0);
  6104. return 0;
  6105. }
  6106. data=script_getdata(st,3);
  6107. get_val(st,data);
  6108. if( data_isstring(data) ) //Char Name
  6109. tsd=map_nick2sd(conv_str(st,data));
  6110. else //Char Id was given
  6111. tsd=map_charid2sd(conv_num(st,data));
  6112. if( tsd == NULL )
  6113. { //Failed
  6114. script_pushint(st,0);
  6115. return 0;
  6116. }
  6117. memset(&item_tmp,0,sizeof(item_tmp));
  6118. item_tmp.nameid=nameid;
  6119. item_tmp.amount=1;
  6120. item_tmp.identify=1;
  6121. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6122. item_tmp.card[2]=tsd->status.char_id;
  6123. item_tmp.card[3]=tsd->status.char_id >> 16;
  6124. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6125. script_pushint(st,0);
  6126. return 0; //Failed to add item, we will not drop if they don't fit
  6127. }
  6128. script_pushint(st,1);
  6129. return SCRIPT_CMD_SUCCESS;
  6130. }
  6131. /*==========================================
  6132. * gets a random item ID from an item group [Skotlex]
  6133. * groupranditem <group_num>{,<sub_group>};
  6134. *------------------------------------------*/
  6135. BUILDIN_FUNC(grouprandomitem)
  6136. {
  6137. script_pushint(st,itemdb_searchrandomid(script_getnum(st,2),script_getnum(st,3)));
  6138. return SCRIPT_CMD_SUCCESS;
  6139. }
  6140. /**
  6141. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6142. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6143. */
  6144. BUILDIN_FUNC(makeitem) {
  6145. int16 nameid;
  6146. uint16 amount, flag = 0, x, y;
  6147. const char *mapname;
  6148. int m;
  6149. struct item item_tmp;
  6150. struct script_data *data;
  6151. data = script_getdata(st,2);
  6152. get_val(st,data);
  6153. if( data_isstring(data) ){
  6154. const char *name = conv_str(st,data);
  6155. struct item_data *item_data = itemdb_searchname(name);
  6156. if( item_data )
  6157. nameid = item_data->nameid;
  6158. else
  6159. nameid = UNKNOWN_ITEM_ID;
  6160. }
  6161. else
  6162. nameid = conv_num(st,data);
  6163. amount = script_getnum(st,3);
  6164. mapname = script_getstr(st,4);
  6165. x = script_getnum(st,5);
  6166. y = script_getnum(st,6);
  6167. if(strcmp(mapname,"this")==0) {
  6168. TBL_PC *sd;
  6169. sd = script_rid2sd(st);
  6170. if (!sd)
  6171. return SCRIPT_CMD_SUCCESS; //Failed...
  6172. m = sd->bl.m;
  6173. } else
  6174. m = map_mapname2mapid(mapname);
  6175. if(nameid<0) {
  6176. nameid = -nameid;
  6177. flag = 1;
  6178. }
  6179. if(nameid > 0) {
  6180. memset(&item_tmp,0,sizeof(item_tmp));
  6181. item_tmp.nameid = nameid;
  6182. if(!flag)
  6183. item_tmp.identify = 1;
  6184. else
  6185. item_tmp.identify = itemdb_isidentified(nameid);
  6186. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  6187. }
  6188. return SCRIPT_CMD_SUCCESS;
  6189. }
  6190. /**
  6191. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6192. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6193. */
  6194. BUILDIN_FUNC(makeitem2) {
  6195. uint16 nameid, amount, x, y;
  6196. const char *mapname;
  6197. int m;
  6198. struct item item_tmp;
  6199. struct script_data *data;
  6200. struct item_data *id;
  6201. data = script_getdata(st,2);
  6202. get_val(st,data);
  6203. if( data_isstring(data) ){
  6204. const char *name = conv_str(st,data);
  6205. struct item_data *item_data = itemdb_searchname(name);
  6206. if( item_data )
  6207. nameid = item_data->nameid;
  6208. else
  6209. nameid = UNKNOWN_ITEM_ID;
  6210. }
  6211. else
  6212. nameid = conv_num(st,data);
  6213. amount = script_getnum(st,3);
  6214. mapname = script_getstr(st,4);
  6215. x = script_getnum(st,5);
  6216. y = script_getnum(st,6);
  6217. if (strcmp(mapname,"this")==0) {
  6218. TBL_PC *sd;
  6219. sd = script_rid2sd(st);
  6220. if (!sd)
  6221. return SCRIPT_CMD_SUCCESS; //Failed...
  6222. m = sd->bl.m;
  6223. }
  6224. else
  6225. m = map_mapname2mapid(mapname);
  6226. if ((id = itemdb_search(nameid))) {
  6227. char iden, ref, attr;
  6228. memset(&item_tmp,0,sizeof(item_tmp));
  6229. item_tmp.nameid = nameid;
  6230. iden = (char)script_getnum(st,7);
  6231. ref = (char)script_getnum(st,8);
  6232. attr = (char)script_getnum(st,9);
  6233. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6234. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6235. }
  6236. else if (id->type==IT_PETEGG) {
  6237. iden = 1;
  6238. ref = 0;
  6239. }
  6240. else {
  6241. iden = 1;
  6242. ref = attr = 0;
  6243. }
  6244. item_tmp.identify = iden;
  6245. item_tmp.refine = ref;
  6246. item_tmp.attribute = attr;
  6247. item_tmp.card[0] = script_getnum(st,10);
  6248. item_tmp.card[1] = script_getnum(st,11);
  6249. item_tmp.card[2] = script_getnum(st,12);
  6250. item_tmp.card[3] = script_getnum(st,13);
  6251. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4);
  6252. }
  6253. else
  6254. return SCRIPT_CMD_FAILURE;
  6255. return SCRIPT_CMD_SUCCESS;
  6256. }
  6257. /// Counts / deletes the current item given by idx.
  6258. /// Used by buildin_delitem_search
  6259. /// Relies on all input data being already fully valid.
  6260. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6261. {
  6262. int delamount;
  6263. struct item *itm = NULL;
  6264. switch(loc) {
  6265. case 1: // cart
  6266. itm = &sd->status.cart[idx];
  6267. break;
  6268. case 2: // storage
  6269. itm = &sd->status.storage.items[idx];
  6270. break;
  6271. default: //inventory
  6272. itm = &sd->status.inventory[idx];
  6273. break;
  6274. }
  6275. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6276. if( delete_items )
  6277. {
  6278. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6279. {// delete associated pet
  6280. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6281. }
  6282. switch(loc) {
  6283. case 1:
  6284. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6285. break;
  6286. case 2:
  6287. storage_delitem(sd,idx,delamount);
  6288. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6289. break;
  6290. default:
  6291. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6292. break;
  6293. }
  6294. }
  6295. amount[0]-= delamount;
  6296. }
  6297. /// Searches for item(s) and checks, if there is enough of them.
  6298. /// Used by delitem and delitem2
  6299. /// Relies on all input data being already fully valid.
  6300. /// @param exact_match will also match item attributes and cards, not just name id
  6301. /// @return true when all items could be deleted, false when there were not enough items to delete
  6302. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6303. {
  6304. bool delete_items = false;
  6305. int i, amount, size;
  6306. struct item *items;
  6307. // prefer always non-equipped items
  6308. it->equip = 0;
  6309. // when searching for nameid only, prefer additionally
  6310. if( !exact_match )
  6311. {
  6312. // non-refined items
  6313. it->refine = 0;
  6314. // card-less items
  6315. memset(it->card, 0, sizeof(it->card));
  6316. }
  6317. switch(loc) {
  6318. case 1: // cart
  6319. size = MAX_CART;
  6320. items = sd->status.cart;
  6321. break;
  6322. case 2: // storage
  6323. size = MAX_STORAGE;
  6324. items = sd->status.storage.items;
  6325. break;
  6326. default: //inventory
  6327. size = MAX_INVENTORY;
  6328. items = sd->status.inventory;
  6329. break;
  6330. }
  6331. for(;;)
  6332. {
  6333. unsigned short important = 0;
  6334. amount = it->amount;
  6335. // 1st pass -- less important items / exact match
  6336. for( i = 0; amount && i < size; i++ )
  6337. {
  6338. struct item *itm = NULL;
  6339. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6340. {// wrong/invalid item
  6341. continue;
  6342. }
  6343. if( itm->equip != it->equip || itm->refine != it->refine )
  6344. {// not matching attributes
  6345. important++;
  6346. continue;
  6347. }
  6348. if( exact_match )
  6349. {
  6350. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6351. {// not matching exact attributes
  6352. continue;
  6353. }
  6354. }
  6355. else
  6356. {
  6357. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6358. {
  6359. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6360. {// pet which cannot be deleted
  6361. continue;
  6362. }
  6363. }
  6364. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6365. {// named/carded item
  6366. important++;
  6367. continue;
  6368. }
  6369. }
  6370. // count / delete item
  6371. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6372. }
  6373. // 2nd pass -- any matching item
  6374. if( amount == 0 || important == 0 )
  6375. {// either everything was already consumed or no items were skipped
  6376. ;
  6377. }
  6378. else for( i = 0; amount && i < size; i++ )
  6379. {
  6380. struct item *itm = NULL;
  6381. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6382. {// wrong/invalid item
  6383. continue;
  6384. }
  6385. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6386. {// pet which cannot be deleted
  6387. continue;
  6388. }
  6389. if( exact_match )
  6390. {
  6391. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6392. {// not matching attributes
  6393. continue;
  6394. }
  6395. }
  6396. // count / delete item
  6397. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6398. }
  6399. if( amount )
  6400. {// not enough items
  6401. return false;
  6402. }
  6403. else if( delete_items )
  6404. {// we are done with the work
  6405. return true;
  6406. }
  6407. else
  6408. {// get rid of the items now
  6409. delete_items = true;
  6410. }
  6411. }
  6412. }
  6413. /// Deletes items from the target/attached player.
  6414. /// Prioritizes ordinary items.
  6415. ///
  6416. /// delitem <item id>,<amount>{,<account id>}
  6417. /// delitem "<item name>",<amount>{,<account id>}
  6418. /// cartdelitem <item id>,<amount>{,<account id>}
  6419. /// cartdelitem "<item name>",<amount>{,<account id>}
  6420. /// storagedelitem <item id>,<amount>{,<account id>}
  6421. /// storagedelitem "<item name>",<amount>{,<account id>}
  6422. BUILDIN_FUNC(delitem)
  6423. {
  6424. TBL_PC *sd;
  6425. struct item it;
  6426. struct script_data *data;
  6427. uint8 loc = 0;
  6428. char* command = (char*)script_getfuncname(st);
  6429. if(!strncmp(command, "cart", 4))
  6430. loc = 1;
  6431. else if(!strncmp(command, "storage", 7))
  6432. loc = 2;
  6433. //TODO: 3 - Guild Storage
  6434. if( script_hasdata(st,4) )
  6435. {
  6436. uint32 account_id = script_getnum(st,4);
  6437. sd = map_id2sd(account_id); // <account id>
  6438. if( sd == NULL )
  6439. {
  6440. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6441. st->state = END;
  6442. return 1;
  6443. }
  6444. }
  6445. else
  6446. {
  6447. sd = script_rid2sd(st);// attached player
  6448. if( sd == NULL )
  6449. return 0;
  6450. }
  6451. if (loc == 1 && !pc_iscarton(sd)) {
  6452. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6453. return SCRIPT_CMD_FAILURE;
  6454. }
  6455. data = script_getdata(st,2);
  6456. get_val(st,data);
  6457. if( data_isstring(data) )
  6458. {
  6459. const char* item_name = conv_str(st,data);
  6460. struct item_data* id = itemdb_searchname(item_name);
  6461. if( id == NULL )
  6462. {
  6463. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6464. st->state = END;
  6465. return 1;
  6466. }
  6467. it.nameid = id->nameid;// "<item name>"
  6468. }
  6469. else
  6470. {
  6471. it.nameid = conv_num(st,data);// <item id>
  6472. if( !itemdb_exists( it.nameid ) )
  6473. {
  6474. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6475. st->state = END;
  6476. return 1;
  6477. }
  6478. }
  6479. it.amount=script_getnum(st,3);
  6480. if( it.amount <= 0 )
  6481. return 0;// nothing to do
  6482. if( buildin_delitem_search(sd, &it, false, loc) )
  6483. {// success
  6484. return SCRIPT_CMD_SUCCESS;
  6485. }
  6486. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6487. st->state = END;
  6488. st->mes_active = 0;
  6489. clif_scriptclose(sd, st->oid);
  6490. return SCRIPT_CMD_FAILURE;
  6491. }
  6492. /// Deletes items from the target/attached player.
  6493. ///
  6494. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6495. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6496. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6497. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6498. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6499. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6500. BUILDIN_FUNC(delitem2)
  6501. {
  6502. TBL_PC *sd;
  6503. struct item it;
  6504. struct script_data *data;
  6505. uint8 loc = 0;
  6506. char* command = (char*)script_getfuncname(st);
  6507. if(!strncmp(command, "cart", 4))
  6508. loc = 1;
  6509. else if(!strncmp(command, "storage", 7))
  6510. loc = 2;
  6511. //TODO: 3 - Guild Storage
  6512. if( script_hasdata(st,11) )
  6513. {
  6514. uint32 account_id = script_getnum(st,11);
  6515. sd = map_id2sd(account_id); // <account id>
  6516. if( sd == NULL )
  6517. {
  6518. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6519. st->state = END;
  6520. return 1;
  6521. }
  6522. }
  6523. else
  6524. {
  6525. sd = script_rid2sd(st);// attached player
  6526. if( sd == NULL )
  6527. return 0;
  6528. }
  6529. if (loc == 1 && !pc_iscarton(sd)) {
  6530. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6531. script_pushint(st,-1);
  6532. return SCRIPT_CMD_FAILURE;
  6533. }
  6534. data = script_getdata(st,2);
  6535. get_val(st,data);
  6536. if( data_isstring(data) )
  6537. {
  6538. const char* item_name = conv_str(st,data);
  6539. struct item_data* id = itemdb_searchname(item_name);
  6540. if( id == NULL )
  6541. {
  6542. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6543. st->state = END;
  6544. return 1;
  6545. }
  6546. it.nameid = id->nameid;// "<item name>"
  6547. }
  6548. else
  6549. {
  6550. it.nameid = conv_num(st,data);// <item id>
  6551. if( !itemdb_exists( it.nameid ) )
  6552. {
  6553. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6554. st->state = END;
  6555. return 1;
  6556. }
  6557. }
  6558. it.amount=script_getnum(st,3);
  6559. it.identify=script_getnum(st,4);
  6560. it.refine=script_getnum(st,5);
  6561. it.attribute=script_getnum(st,6);
  6562. it.card[0]=(short)script_getnum(st,7);
  6563. it.card[1]=(short)script_getnum(st,8);
  6564. it.card[2]=(short)script_getnum(st,9);
  6565. it.card[3]=(short)script_getnum(st,10);
  6566. if( it.amount <= 0 )
  6567. return 0;// nothing to do
  6568. if( buildin_delitem_search(sd, &it, true, loc) )
  6569. {// success
  6570. return SCRIPT_CMD_SUCCESS;
  6571. }
  6572. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6573. st->state = END;
  6574. st->mes_active = 0;
  6575. clif_scriptclose(sd, st->oid);
  6576. return SCRIPT_CMD_FAILURE;
  6577. }
  6578. /*==========================================
  6579. * Enables/Disables use of items while in an NPC [Skotlex]
  6580. *------------------------------------------*/
  6581. BUILDIN_FUNC(enableitemuse)
  6582. {
  6583. TBL_PC *sd;
  6584. sd=script_rid2sd(st);
  6585. if (sd)
  6586. st->npc_item_flag = sd->npc_item_flag = 1;
  6587. return SCRIPT_CMD_SUCCESS;
  6588. }
  6589. BUILDIN_FUNC(disableitemuse)
  6590. {
  6591. TBL_PC *sd;
  6592. sd=script_rid2sd(st);
  6593. if (sd)
  6594. st->npc_item_flag = sd->npc_item_flag = 0;
  6595. return SCRIPT_CMD_SUCCESS;
  6596. }
  6597. /*==========================================
  6598. * Returns a character's specified stat.
  6599. * Check pc_readparam for available options.
  6600. *------------------------------------------*/
  6601. BUILDIN_FUNC(readparam)
  6602. {
  6603. int type;
  6604. TBL_PC *sd;
  6605. type=script_getnum(st,2);
  6606. if( script_hasdata(st,3) )
  6607. sd=map_nick2sd(script_getstr(st,3));
  6608. else
  6609. sd=script_rid2sd(st);
  6610. if(sd==NULL){
  6611. script_pushint(st,-1);
  6612. return 0;
  6613. }
  6614. script_pushint(st,pc_readparam(sd,type));
  6615. return SCRIPT_CMD_SUCCESS;
  6616. }
  6617. /*==========================================
  6618. * Return charid identification
  6619. * return by @num :
  6620. * 0 : char_id
  6621. * 1 : party_id
  6622. * 2 : guild_id
  6623. * 3 : account_id
  6624. * 4 : bg_id
  6625. *------------------------------------------*/
  6626. BUILDIN_FUNC(getcharid)
  6627. {
  6628. int num;
  6629. TBL_PC *sd;
  6630. num = script_getnum(st,2);
  6631. if( script_hasdata(st,3) )
  6632. sd=map_nick2sd(script_getstr(st,3));
  6633. else
  6634. sd=script_rid2sd(st);
  6635. if(sd==NULL){
  6636. script_pushint(st,0); //return 0, according docs
  6637. return 0;
  6638. }
  6639. switch( num ) {
  6640. case 0: script_pushint(st,sd->status.char_id); break;
  6641. case 1: script_pushint(st,sd->status.party_id); break;
  6642. case 2: script_pushint(st,sd->status.guild_id); break;
  6643. case 3: script_pushint(st,sd->status.account_id); break;
  6644. case 4: script_pushint(st,sd->bg_id); break;
  6645. default:
  6646. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6647. script_pushint(st,0);
  6648. break;
  6649. }
  6650. return SCRIPT_CMD_SUCCESS;
  6651. }
  6652. /*==========================================
  6653. * returns the GID of an NPC
  6654. *------------------------------------------*/
  6655. BUILDIN_FUNC(getnpcid)
  6656. {
  6657. int num = script_getnum(st,2);
  6658. struct npc_data* nd = NULL;
  6659. if( script_hasdata(st,3) )
  6660. {// unique npc name
  6661. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6662. {
  6663. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6664. script_pushint(st,0);
  6665. return 1;
  6666. }
  6667. }
  6668. switch (num) {
  6669. case 0:
  6670. script_pushint(st,nd ? nd->bl.id : st->oid);
  6671. break;
  6672. default:
  6673. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6674. script_pushint(st,0);
  6675. return 1;
  6676. }
  6677. return SCRIPT_CMD_SUCCESS;
  6678. }
  6679. /*==========================================
  6680. * Return the name of the party_id
  6681. * null if not found
  6682. *------------------------------------------*/
  6683. BUILDIN_FUNC(getpartyname)
  6684. {
  6685. int party_id;
  6686. struct party_data* p;
  6687. party_id = script_getnum(st,2);
  6688. if( ( p = party_search(party_id) ) != NULL )
  6689. {
  6690. script_pushstrcopy(st,p->party.name);
  6691. }
  6692. else
  6693. {
  6694. script_pushconststr(st,"null");
  6695. }
  6696. return SCRIPT_CMD_SUCCESS;
  6697. }
  6698. /*==========================================
  6699. * Get the information of the members of a party by type
  6700. * @party_id, @type
  6701. * return by @type :
  6702. * - : nom des membres
  6703. * 1 : char_id des membres
  6704. * 2 : account_id des membres
  6705. *------------------------------------------*/
  6706. BUILDIN_FUNC(getpartymember)
  6707. {
  6708. struct party_data *p;
  6709. unsigned char j = 0;
  6710. p = party_search(script_getnum(st,2));
  6711. if (p != NULL) {
  6712. int type = 0;
  6713. unsigned char i;
  6714. if (script_hasdata(st,3))
  6715. type = script_getnum(st,3);
  6716. for (i = 0; i < MAX_PARTY; i++) {
  6717. if (p->party.member[i].account_id) {
  6718. switch (type) {
  6719. case 2:
  6720. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6721. break;
  6722. case 1:
  6723. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6724. break;
  6725. default:
  6726. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6727. break;
  6728. }
  6729. j++;
  6730. }
  6731. }
  6732. }
  6733. mapreg_setreg(add_str("$@partymembercount"),j);
  6734. return SCRIPT_CMD_SUCCESS;
  6735. }
  6736. /*==========================================
  6737. * Retrieves party leader. if flag is specified,
  6738. * return some of the leader data. Otherwise, return name.
  6739. *------------------------------------------*/
  6740. BUILDIN_FUNC(getpartyleader)
  6741. {
  6742. int party_id, type = 0, i=0;
  6743. struct party_data *p;
  6744. party_id=script_getnum(st,2);
  6745. if( script_hasdata(st,3) )
  6746. type=script_getnum(st,3);
  6747. p=party_search(party_id);
  6748. if (p) //Search leader
  6749. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6750. if (!p || i == MAX_PARTY) { //leader not found
  6751. if (type)
  6752. script_pushint(st,-1);
  6753. else
  6754. script_pushconststr(st,"null");
  6755. return 0;
  6756. }
  6757. switch (type) {
  6758. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6759. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6760. case 3: script_pushint(st,p->party.member[i].class_); break;
  6761. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6762. case 5: script_pushint(st,p->party.member[i].lv); break;
  6763. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6764. }
  6765. return SCRIPT_CMD_SUCCESS;
  6766. }
  6767. /*==========================================
  6768. * Return the name of the @guild_id
  6769. * null if not found
  6770. *------------------------------------------*/
  6771. BUILDIN_FUNC(getguildname)
  6772. {
  6773. int guild_id;
  6774. struct guild* g;
  6775. guild_id = script_getnum(st,2);
  6776. if( ( g = guild_search(guild_id) ) != NULL )
  6777. script_pushstrcopy(st,g->name);
  6778. else
  6779. script_pushconststr(st,"null");
  6780. return SCRIPT_CMD_SUCCESS;
  6781. }
  6782. /*==========================================
  6783. * Return the name of the guild master of @guild_id
  6784. * null if not found
  6785. *------------------------------------------*/
  6786. BUILDIN_FUNC(getguildmaster)
  6787. {
  6788. int guild_id;
  6789. struct guild* g;
  6790. guild_id = script_getnum(st,2);
  6791. if( ( g = guild_search(guild_id) ) != NULL )
  6792. script_pushstrcopy(st,g->member[0].name);
  6793. else
  6794. script_pushconststr(st,"null");
  6795. return SCRIPT_CMD_SUCCESS;
  6796. }
  6797. BUILDIN_FUNC(getguildmasterid)
  6798. {
  6799. int guild_id;
  6800. struct guild* g;
  6801. guild_id = script_getnum(st,2);
  6802. if( ( g = guild_search(guild_id) ) != NULL )
  6803. script_pushint(st,g->member[0].char_id);
  6804. else
  6805. script_pushint(st,0);
  6806. return SCRIPT_CMD_SUCCESS;
  6807. }
  6808. /*==========================================
  6809. * Get char string information by type :
  6810. * Return by @type :
  6811. * 0 : char_name
  6812. * 1 : party_name or ""
  6813. * 2 : guild_name or ""
  6814. * 3 : map_name
  6815. * - : ""
  6816. *------------------------------------------*/
  6817. BUILDIN_FUNC(strcharinfo)
  6818. {
  6819. TBL_PC *sd;
  6820. int num;
  6821. struct guild* g;
  6822. struct party_data* p;
  6823. sd=script_rid2sd(st);
  6824. if (!sd) { //Avoid crashing....
  6825. script_pushconststr(st,"");
  6826. return 0;
  6827. }
  6828. num=script_getnum(st,2);
  6829. switch(num){
  6830. case 0:
  6831. script_pushstrcopy(st,sd->status.name);
  6832. break;
  6833. case 1:
  6834. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  6835. script_pushstrcopy(st,p->party.name);
  6836. } else {
  6837. script_pushconststr(st,"");
  6838. }
  6839. break;
  6840. case 2:
  6841. if( ( g = sd->guild ) != NULL ) {
  6842. script_pushstrcopy(st,g->name);
  6843. } else {
  6844. script_pushconststr(st,"");
  6845. }
  6846. break;
  6847. case 3:
  6848. script_pushconststr(st,map[sd->bl.m].name);
  6849. break;
  6850. default:
  6851. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  6852. script_pushconststr(st,"");
  6853. break;
  6854. }
  6855. return SCRIPT_CMD_SUCCESS;
  6856. }
  6857. /*==========================================
  6858. * Get npc string information by type
  6859. * return by @type:
  6860. * 0 : name
  6861. * 1 : str#
  6862. * 2 : #str
  6863. * 3 : ::str
  6864. * 4 : map name
  6865. *------------------------------------------*/
  6866. BUILDIN_FUNC(strnpcinfo)
  6867. {
  6868. TBL_NPC* nd;
  6869. int num;
  6870. char *buf,*name=NULL;
  6871. nd = map_id2nd(st->oid);
  6872. if (!nd) {
  6873. script_pushconststr(st, "");
  6874. return 0;
  6875. }
  6876. num = script_getnum(st,2);
  6877. switch(num){
  6878. case 0: // display name
  6879. name = aStrdup(nd->name);
  6880. break;
  6881. case 1: // visible part of display name
  6882. if((buf = strchr(nd->name,'#')) != NULL)
  6883. {
  6884. name = aStrdup(nd->name);
  6885. name[buf - nd->name] = 0;
  6886. } else // Return the name, there is no '#' present
  6887. name = aStrdup(nd->name);
  6888. break;
  6889. case 2: // # fragment
  6890. if((buf = strchr(nd->name,'#')) != NULL)
  6891. name = aStrdup(buf+1);
  6892. break;
  6893. case 3: // unique name
  6894. name = aStrdup(nd->exname);
  6895. break;
  6896. case 4: // map name
  6897. name = aStrdup(map[nd->bl.m].name);
  6898. break;
  6899. }
  6900. if(name)
  6901. script_pushstr(st, name);
  6902. else
  6903. script_pushconststr(st, "");
  6904. return SCRIPT_CMD_SUCCESS;
  6905. }
  6906. /// aegis->athena slot position conversion table
  6907. /// Index is used by EQI_ in db/const.txt
  6908. static unsigned int equip[] = {
  6909. EQP_HEAD_TOP,
  6910. EQP_ARMOR,
  6911. EQP_HAND_L,
  6912. EQP_HAND_R,
  6913. EQP_GARMENT,
  6914. EQP_SHOES,
  6915. EQP_ACC_L,
  6916. EQP_ACC_R,
  6917. EQP_HEAD_MID,
  6918. EQP_HEAD_LOW,
  6919. EQP_COSTUME_HEAD_LOW,
  6920. EQP_COSTUME_HEAD_MID,
  6921. EQP_COSTUME_HEAD_TOP,
  6922. EQP_COSTUME_GARMENT,
  6923. EQP_AMMO,
  6924. EQP_SHADOW_ARMOR,
  6925. EQP_SHADOW_WEAPON,
  6926. EQP_SHADOW_SHIELD,
  6927. EQP_SHADOW_SHOES,
  6928. EQP_SHADOW_ACC_R,
  6929. EQP_SHADOW_ACC_L
  6930. };
  6931. /*==========================================
  6932. * GetEquipID(Pos); Pos: 1-14
  6933. *------------------------------------------*/
  6934. BUILDIN_FUNC(getequipid)
  6935. {
  6936. int i, num;
  6937. TBL_PC* sd;
  6938. struct item_data* item;
  6939. sd = script_rid2sd(st);
  6940. if( sd == NULL )
  6941. return 0;
  6942. num = script_getnum(st,2) - 1;
  6943. if( num < 0 || num >= ARRAYLENGTH(equip) )
  6944. {
  6945. script_pushint(st,-1);
  6946. return 0;
  6947. }
  6948. // get inventory position of item
  6949. i = pc_checkequip(sd,equip[num]);
  6950. if( i < 0 )
  6951. {
  6952. script_pushint(st,-1);
  6953. return 0;
  6954. }
  6955. item = sd->inventory_data[i];
  6956. if( item != 0 )
  6957. script_pushint(st,item->nameid);
  6958. else
  6959. script_pushint(st,0);
  6960. return SCRIPT_CMD_SUCCESS;
  6961. }
  6962. /*==========================================
  6963. * GetEquipUniqueID(Pos); Pos: 1-14
  6964. *------------------------------------------*/
  6965. BUILDIN_FUNC(getequipuniqueid)
  6966. {
  6967. int i, num;
  6968. TBL_PC* sd;
  6969. struct item* item;
  6970. sd = script_rid2sd(st);
  6971. if (sd == NULL)
  6972. return 0;
  6973. num = script_getnum(st,2) - 1;
  6974. if (num < 0 || num >= ARRAYLENGTH(equip)) {
  6975. script_pushconststr(st, "");
  6976. return 0;
  6977. }
  6978. // get inventory position of item
  6979. i = pc_checkequip(sd,equip[num]);
  6980. if (i < 0) {
  6981. script_pushconststr(st, "");
  6982. return 0;
  6983. }
  6984. item = &sd->status.inventory[i];
  6985. if (item != 0) {
  6986. char buf[256];
  6987. memset(buf, 0, sizeof(buf));
  6988. snprintf(buf, sizeof(buf)-1, "%llu", (unsigned long long)item->unique_id);
  6989. script_pushstr(st, buf);
  6990. } else
  6991. script_pushconststr(st, "");
  6992. return SCRIPT_CMD_SUCCESS;
  6993. }
  6994. /*==========================================
  6995. * Get the equipement name at pos
  6996. * return item jname or ""
  6997. *------------------------------------------*/
  6998. BUILDIN_FUNC(getequipname)
  6999. {
  7000. int i, num;
  7001. TBL_PC* sd;
  7002. struct item_data* item;
  7003. sd = script_rid2sd(st);
  7004. if( sd == NULL )
  7005. return 0;
  7006. num = script_getnum(st,2) - 1;
  7007. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7008. {
  7009. script_pushconststr(st,"");
  7010. return 0;
  7011. }
  7012. // get inventory position of item
  7013. i = pc_checkequip(sd,equip[num]);
  7014. if( i < 0 )
  7015. {
  7016. script_pushconststr(st,"");
  7017. return 0;
  7018. }
  7019. item = sd->inventory_data[i];
  7020. if( item != 0 )
  7021. script_pushstrcopy(st,item->jname);
  7022. else
  7023. script_pushconststr(st,"");
  7024. return SCRIPT_CMD_SUCCESS;
  7025. }
  7026. /*==========================================
  7027. * getbrokenid [Valaris]
  7028. *------------------------------------------*/
  7029. BUILDIN_FUNC(getbrokenid)
  7030. {
  7031. int i,num,id=0,brokencounter=0;
  7032. TBL_PC *sd;
  7033. sd = script_rid2sd(st);
  7034. if( sd == NULL )
  7035. return 0;
  7036. num=script_getnum(st,2);
  7037. for(i=0; i<MAX_INVENTORY; i++) {
  7038. if(sd->status.inventory[i].attribute){
  7039. brokencounter++;
  7040. if(num==brokencounter){
  7041. id=sd->status.inventory[i].nameid;
  7042. break;
  7043. }
  7044. }
  7045. }
  7046. script_pushint(st,id);
  7047. return SCRIPT_CMD_SUCCESS;
  7048. }
  7049. /*==========================================
  7050. * repair [Valaris]
  7051. *------------------------------------------*/
  7052. BUILDIN_FUNC(repair)
  7053. {
  7054. int i,num;
  7055. int repaircounter=0;
  7056. TBL_PC *sd;
  7057. sd = script_rid2sd(st);
  7058. if( sd == NULL )
  7059. return 0;
  7060. num=script_getnum(st,2);
  7061. for(i=0; i<MAX_INVENTORY; i++) {
  7062. if(sd->status.inventory[i].attribute){
  7063. repaircounter++;
  7064. if(num==repaircounter){
  7065. sd->status.inventory[i].attribute=0;
  7066. clif_equiplist(sd);
  7067. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  7068. clif_misceffect(&sd->bl, 3);
  7069. break;
  7070. }
  7071. }
  7072. }
  7073. return SCRIPT_CMD_SUCCESS;
  7074. }
  7075. /*==========================================
  7076. * repairall
  7077. *------------------------------------------*/
  7078. BUILDIN_FUNC(repairall)
  7079. {
  7080. int i, repaircounter = 0;
  7081. TBL_PC *sd;
  7082. sd = script_rid2sd(st);
  7083. if(sd == NULL)
  7084. return 0;
  7085. for(i = 0; i < MAX_INVENTORY; i++)
  7086. {
  7087. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  7088. {
  7089. sd->status.inventory[i].attribute = 0;
  7090. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  7091. repaircounter++;
  7092. }
  7093. }
  7094. if(repaircounter)
  7095. {
  7096. clif_misceffect(&sd->bl, 3);
  7097. clif_equiplist(sd);
  7098. }
  7099. return SCRIPT_CMD_SUCCESS;
  7100. }
  7101. /*==========================================
  7102. * Chk if player have something equiped at pos
  7103. *------------------------------------------*/
  7104. BUILDIN_FUNC(getequipisequiped)
  7105. {
  7106. int i = -1,num;
  7107. TBL_PC *sd;
  7108. num = script_getnum(st,2);
  7109. sd = script_rid2sd(st);
  7110. if( sd == NULL )
  7111. return 0;
  7112. if (num > 0 && num <= ARRAYLENGTH(equip))
  7113. i=pc_checkequip(sd,equip[num-1]);
  7114. if(i >= 0)
  7115. script_pushint(st,1);
  7116. else
  7117. script_pushint(st,0);
  7118. return SCRIPT_CMD_SUCCESS;
  7119. }
  7120. /*==========================================
  7121. * Chk if the player have something equiped at pos
  7122. * if so chk if this item ain't marked not refinable or rental
  7123. * return (npc)
  7124. * 1 : true
  7125. * 0 : false
  7126. *------------------------------------------*/
  7127. BUILDIN_FUNC(getequipisenableref)
  7128. {
  7129. int i = -1,num;
  7130. TBL_PC *sd;
  7131. num = script_getnum(st,2);
  7132. sd = script_rid2sd(st);
  7133. if( sd == NULL )
  7134. return 0;
  7135. if( num > 0 && num <= ARRAYLENGTH(equip) )
  7136. i = pc_checkequip(sd,equip[num-1]);
  7137. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  7138. script_pushint(st,1);
  7139. else
  7140. script_pushint(st,0);
  7141. return SCRIPT_CMD_SUCCESS;
  7142. }
  7143. /*==========================================
  7144. * Get the item refined value at pos
  7145. * return (npc)
  7146. * x : refine amount
  7147. * 0 : false (not refined)
  7148. *------------------------------------------*/
  7149. BUILDIN_FUNC(getequiprefinerycnt)
  7150. {
  7151. int i = -1,num;
  7152. TBL_PC *sd;
  7153. num = script_getnum(st,2);
  7154. sd = script_rid2sd(st);
  7155. if( sd == NULL )
  7156. return 0;
  7157. if (num > 0 && num <= ARRAYLENGTH(equip))
  7158. i=pc_checkequip(sd,equip[num-1]);
  7159. if(i >= 0)
  7160. script_pushint(st,sd->status.inventory[i].refine);
  7161. else
  7162. script_pushint(st,0);
  7163. return SCRIPT_CMD_SUCCESS;
  7164. }
  7165. /*==========================================
  7166. * Get the weapon level value at pos
  7167. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7168. * return (npc)
  7169. * x : weapon level
  7170. * 0 : false
  7171. *------------------------------------------*/
  7172. BUILDIN_FUNC(getequipweaponlv)
  7173. {
  7174. int i = -1,num;
  7175. TBL_PC *sd;
  7176. num = script_getnum(st,2);
  7177. sd = script_rid2sd(st);
  7178. if( sd == NULL )
  7179. return 0;
  7180. if (num > 0 && num <= ARRAYLENGTH(equip))
  7181. i=pc_checkequip(sd,equip[num-1]);
  7182. if(i >= 0 && sd->inventory_data[i])
  7183. script_pushint(st,sd->inventory_data[i]->wlv);
  7184. else
  7185. script_pushint(st,0);
  7186. return SCRIPT_CMD_SUCCESS;
  7187. }
  7188. /*==========================================
  7189. * Get the item refine chance (from refine.txt) for item at pos
  7190. * return (npc)
  7191. * x : refine chance
  7192. * 0 : false (max refine level or unequip..)
  7193. *------------------------------------------*/
  7194. BUILDIN_FUNC(getequippercentrefinery)
  7195. {
  7196. int i = -1,num;
  7197. TBL_PC *sd;
  7198. num = script_getnum(st,2);
  7199. sd = script_rid2sd(st);
  7200. if( sd == NULL )
  7201. return 0;
  7202. if (num > 0 && num <= ARRAYLENGTH(equip))
  7203. i = pc_checkequip(sd,equip[num-1]);
  7204. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  7205. script_pushint(st,status_get_refine_chance((enum refine_type)itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  7206. else
  7207. script_pushint(st,0);
  7208. return SCRIPT_CMD_SUCCESS;
  7209. }
  7210. /*==========================================
  7211. * Refine +1 item at pos and log and display refine
  7212. *------------------------------------------*/
  7213. BUILDIN_FUNC(successrefitem) {
  7214. short i = -1, up = 1;
  7215. int pos;
  7216. TBL_PC *sd;
  7217. pos = script_getnum(st,2);
  7218. sd = script_rid2sd(st);
  7219. if (sd == NULL)
  7220. return 0;
  7221. if (script_hasdata(st, 3))
  7222. up = script_getnum(st, 3);
  7223. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7224. i = pc_checkequip(sd,equip[pos-1]);
  7225. if (i >= 0) {
  7226. unsigned int ep = sd->status.inventory[i].equip;
  7227. //Logs items, got from (N)PC scripts [Lupus]
  7228. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7229. if (sd->status.inventory[i].refine >= MAX_REFINE) {
  7230. script_pushint(st, MAX_REFINE);
  7231. return SCRIPT_CMD_SUCCESS;
  7232. }
  7233. sd->status.inventory[i].refine += up;
  7234. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7235. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7236. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  7237. clif_delitem(sd,i,1,3);
  7238. //Logs items, got from (N)PC scripts [Lupus]
  7239. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7240. clif_additem(sd,i,1,0);
  7241. pc_equipitem(sd,i,ep);
  7242. clif_misceffect(&sd->bl,3);
  7243. if (sd->status.inventory[i].refine == MAX_REFINE &&
  7244. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  7245. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3]))
  7246. { // Fame point system [DracoRPG]
  7247. switch (sd->inventory_data[i]->wlv){
  7248. case 1:
  7249. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7250. break;
  7251. case 2:
  7252. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7253. break;
  7254. case 3:
  7255. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7256. break;
  7257. }
  7258. }
  7259. script_pushint(st, sd->status.inventory[i].refine);
  7260. return SCRIPT_CMD_SUCCESS;
  7261. }
  7262. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7263. script_pushint(st, -1);
  7264. return SCRIPT_CMD_FAILURE;
  7265. }
  7266. /*==========================================
  7267. * Show a failed Refine +1 attempt
  7268. *------------------------------------------*/
  7269. BUILDIN_FUNC(failedrefitem) {
  7270. short i = -1;
  7271. int pos;
  7272. TBL_PC *sd;
  7273. pos = script_getnum(st,2);
  7274. sd = script_rid2sd(st);
  7275. if (sd == NULL)
  7276. return 0;
  7277. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7278. i = pc_checkequip(sd,equip[pos-1]);
  7279. if (i >= 0) {
  7280. sd->status.inventory[i].refine = 0;
  7281. pc_unequipitem(sd,i,3); //recalculate bonus
  7282. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  7283. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7284. clif_misceffect(&sd->bl,2); // display failure effect
  7285. script_pushint(st, 1);
  7286. return SCRIPT_CMD_SUCCESS;
  7287. }
  7288. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7289. script_pushint(st, 0);
  7290. return SCRIPT_CMD_FAILURE;
  7291. }
  7292. /*==========================================
  7293. * Downgrades an Equipment Part by -1 . [Masao]
  7294. *------------------------------------------*/
  7295. BUILDIN_FUNC(downrefitem) {
  7296. short i = -1, down = 1;
  7297. int pos;
  7298. TBL_PC *sd;
  7299. sd = script_rid2sd(st);
  7300. if( sd == NULL )
  7301. return 0;
  7302. pos = script_getnum(st,2);
  7303. if (script_hasdata(st, 3))
  7304. down = script_getnum(st, 3);
  7305. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7306. i = pc_checkequip(sd,equip[pos-1]);
  7307. if (i >= 0) {
  7308. unsigned int ep = sd->status.inventory[i].equip;
  7309. //Logs items, got from (N)PC scripts [Lupus]
  7310. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7311. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7312. sd->status.inventory[i].refine -= down;
  7313. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7314. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  7315. clif_delitem(sd,i,1,3);
  7316. //Logs items, got from (N)PC scripts [Lupus]
  7317. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7318. clif_additem(sd,i,1,0);
  7319. pc_equipitem(sd,i,ep);
  7320. clif_misceffect(&sd->bl,2);
  7321. script_pushint(st, sd->status.inventory[i].refine);
  7322. return SCRIPT_CMD_SUCCESS;
  7323. }
  7324. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7325. script_pushint(st, -1);
  7326. return SCRIPT_CMD_FAILURE;
  7327. }
  7328. /*==========================================
  7329. * Delete the item equipped at pos.
  7330. *------------------------------------------*/
  7331. BUILDIN_FUNC(delequip) {
  7332. short i = -1;
  7333. int pos;
  7334. int8 ret;
  7335. TBL_PC *sd;
  7336. pos = script_getnum(st,2);
  7337. sd = script_rid2sd(st);
  7338. if (sd == NULL)
  7339. return 0;
  7340. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7341. i = pc_checkequip(sd,equip[pos-1]);
  7342. if (i >= 0) {
  7343. pc_unequipitem(sd,i,3); //recalculate bonus
  7344. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7345. }
  7346. else {
  7347. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7348. st->state = END;
  7349. return SCRIPT_CMD_FAILURE;
  7350. }
  7351. script_pushint(st,ret);
  7352. return SCRIPT_CMD_SUCCESS;
  7353. }
  7354. /*==========================================
  7355. * Break the item equipped at pos.
  7356. *------------------------------------------*/
  7357. BUILDIN_FUNC(breakequip) {
  7358. short i = -1;
  7359. int pos;
  7360. TBL_PC *sd;
  7361. pos = script_getnum(st,2);
  7362. sd = script_rid2sd(st);
  7363. if (sd == NULL)
  7364. return 0;
  7365. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7366. i = pc_checkequip(sd,equip[pos-1]);
  7367. if (i >= 0) {
  7368. sd->status.inventory[i].attribute = 1;
  7369. pc_unequipitem(sd,i,3);
  7370. clif_equiplist(sd);
  7371. script_pushint(st,1);
  7372. return SCRIPT_CMD_SUCCESS;
  7373. }
  7374. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7375. script_pushint(st,0);
  7376. return SCRIPT_CMD_FAILURE;
  7377. }
  7378. /*==========================================
  7379. *
  7380. *------------------------------------------*/
  7381. BUILDIN_FUNC(statusup)
  7382. {
  7383. int type;
  7384. TBL_PC *sd;
  7385. type=script_getnum(st,2);
  7386. sd = script_rid2sd(st);
  7387. if( sd == NULL )
  7388. return 0;
  7389. pc_statusup(sd, type, 1);
  7390. return SCRIPT_CMD_SUCCESS;
  7391. }
  7392. /*==========================================
  7393. *
  7394. *------------------------------------------*/
  7395. BUILDIN_FUNC(statusup2)
  7396. {
  7397. int type,val;
  7398. TBL_PC *sd;
  7399. type=script_getnum(st,2);
  7400. val=script_getnum(st,3);
  7401. sd = script_rid2sd(st);
  7402. if( sd == NULL )
  7403. return 0;
  7404. pc_statusup2(sd,type,val);
  7405. return SCRIPT_CMD_SUCCESS;
  7406. }
  7407. /// See 'doc/item_bonus.txt'
  7408. ///
  7409. /// bonus <bonus type>,<val1>;
  7410. /// bonus2 <bonus type>,<val1>,<val2>;
  7411. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7412. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7413. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7414. BUILDIN_FUNC(bonus)
  7415. {
  7416. int type;
  7417. int val1;
  7418. int val2 = 0;
  7419. int val3 = 0;
  7420. int val4 = 0;
  7421. int val5 = 0;
  7422. TBL_PC* sd;
  7423. struct script_data *data;
  7424. sd = script_rid2sd(st);
  7425. if( sd == NULL )
  7426. return 0; // no player attached
  7427. type = script_getnum(st,2);
  7428. switch( type ) {
  7429. case SP_AUTOSPELL:
  7430. case SP_AUTOSPELL_WHENHIT:
  7431. case SP_AUTOSPELL_ONSKILL:
  7432. case SP_SKILL_ATK:
  7433. case SP_SKILL_HEAL:
  7434. case SP_SKILL_HEAL2:
  7435. case SP_ADD_SKILL_BLOW:
  7436. case SP_CASTRATE:
  7437. case SP_ADDEFF_ONSKILL:
  7438. case SP_SKILL_USE_SP_RATE:
  7439. case SP_SKILL_COOLDOWN:
  7440. case SP_SKILL_FIXEDCAST:
  7441. case SP_SKILL_VARIABLECAST:
  7442. case SP_VARCASTRATE:
  7443. case SP_FIXCASTRATE:
  7444. case SP_SKILL_USE_SP:
  7445. // these bonuses support skill names
  7446. data = script_getdata(st, 3);
  7447. get_val(st, data); // Convert into value in case of a variable
  7448. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7449. break;
  7450. default:
  7451. val1 = script_getnum(st,3);
  7452. break;
  7453. }
  7454. switch( script_lastdata(st)-2 ) {
  7455. case 1:
  7456. pc_bonus(sd, type, val1);
  7457. break;
  7458. case 2:
  7459. val2 = script_getnum(st,4);
  7460. pc_bonus2(sd, type, val1, val2);
  7461. break;
  7462. case 3:
  7463. val2 = script_getnum(st,4);
  7464. val3 = script_getnum(st,5);
  7465. pc_bonus3(sd, type, val1, val2, val3);
  7466. break;
  7467. case 4:
  7468. data = script_getdata(st, 4);
  7469. get_val(st, data); // Convert into value in case of a variable
  7470. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7471. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7472. else
  7473. val2 = script_getnum(st,4);
  7474. val3 = script_getnum(st,5);
  7475. val4 = script_getnum(st,6);
  7476. pc_bonus4(sd, type, val1, val2, val3, val4);
  7477. break;
  7478. case 5:
  7479. data = script_getdata(st, 4);
  7480. get_val(st, data); // Convert into value in case of a variable
  7481. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7482. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7483. else
  7484. val2 = script_getnum(st,4);
  7485. val3 = script_getnum(st,5);
  7486. val4 = script_getnum(st,6);
  7487. val5 = script_getnum(st,7);
  7488. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7489. break;
  7490. default:
  7491. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7492. break;
  7493. }
  7494. return SCRIPT_CMD_SUCCESS;
  7495. }
  7496. BUILDIN_FUNC(autobonus)
  7497. {
  7498. unsigned int dur, pos;
  7499. short rate;
  7500. short atk_type = 0;
  7501. TBL_PC* sd;
  7502. const char *bonus_script, *other_script = NULL;
  7503. sd = script_rid2sd(st);
  7504. if( sd == NULL )
  7505. return 0; // no player attached
  7506. if (current_equip_combo_pos)
  7507. pos = current_equip_combo_pos;
  7508. else
  7509. pos = sd->status.inventory[current_equip_item_index].equip;
  7510. if((sd->state.autobonus&pos) == pos)
  7511. return 0;
  7512. rate = script_getnum(st,3);
  7513. dur = script_getnum(st,4);
  7514. bonus_script = script_getstr(st,2);
  7515. if( !rate || !dur || !pos || !bonus_script )
  7516. return 0;
  7517. if( script_hasdata(st,5) )
  7518. atk_type = script_getnum(st,5);
  7519. if( script_hasdata(st,6) )
  7520. other_script = script_getstr(st,6);
  7521. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7522. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7523. {
  7524. script_add_autobonus(bonus_script);
  7525. if( other_script )
  7526. script_add_autobonus(other_script);
  7527. }
  7528. return SCRIPT_CMD_SUCCESS;
  7529. }
  7530. BUILDIN_FUNC(autobonus2)
  7531. {
  7532. unsigned int dur, pos;
  7533. short rate;
  7534. short atk_type = 0;
  7535. TBL_PC* sd;
  7536. const char *bonus_script, *other_script = NULL;
  7537. sd = script_rid2sd(st);
  7538. if( sd == NULL )
  7539. return 0; // no player attached
  7540. if (current_equip_combo_pos)
  7541. pos = current_equip_combo_pos;
  7542. else
  7543. pos = sd->status.inventory[current_equip_item_index].equip;
  7544. if((sd->state.autobonus&pos) == pos)
  7545. return 0;
  7546. rate = script_getnum(st,3);
  7547. dur = script_getnum(st,4);
  7548. bonus_script = script_getstr(st,2);
  7549. if( !rate || !dur || !pos || !bonus_script )
  7550. return 0;
  7551. if( script_hasdata(st,5) )
  7552. atk_type = script_getnum(st,5);
  7553. if( script_hasdata(st,6) )
  7554. other_script = script_getstr(st,6);
  7555. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7556. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7557. {
  7558. script_add_autobonus(bonus_script);
  7559. if( other_script )
  7560. script_add_autobonus(other_script);
  7561. }
  7562. return SCRIPT_CMD_SUCCESS;
  7563. }
  7564. BUILDIN_FUNC(autobonus3)
  7565. {
  7566. unsigned int dur, pos;
  7567. short rate,atk_type;
  7568. TBL_PC* sd;
  7569. const char *bonus_script, *other_script = NULL;
  7570. struct script_data *data;
  7571. sd = script_rid2sd(st);
  7572. if( sd == NULL )
  7573. return 0; // no player attached
  7574. if (current_equip_combo_pos)
  7575. pos = current_equip_combo_pos;
  7576. else
  7577. pos = sd->status.inventory[current_equip_item_index].equip;
  7578. if((sd->state.autobonus&pos) == pos)
  7579. return 0;
  7580. rate = script_getnum(st,3);
  7581. dur = script_getnum(st,4);
  7582. data = script_getdata(st, 5);
  7583. get_val(st, data); // Convert into value in case of a variable
  7584. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7585. bonus_script = script_getstr(st,2);
  7586. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7587. return 0;
  7588. if( script_hasdata(st,6) )
  7589. other_script = script_getstr(st,6);
  7590. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7591. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7592. {
  7593. script_add_autobonus(bonus_script);
  7594. if( other_script )
  7595. script_add_autobonus(other_script);
  7596. }
  7597. return SCRIPT_CMD_SUCCESS;
  7598. }
  7599. /// Changes the level of a player skill.
  7600. /// <flag> defaults to 1
  7601. /// <flag>=0 : set the level of the skill
  7602. /// <flag>=1 : set the temporary level of the skill
  7603. /// <flag>=2 : add to the level of the skill
  7604. ///
  7605. /// skill <skill id>,<level>,<flag>
  7606. /// skill <skill id>,<level>
  7607. /// skill "<skill name>",<level>,<flag>
  7608. /// skill "<skill name>",<level>
  7609. BUILDIN_FUNC(skill)
  7610. {
  7611. int id;
  7612. int level;
  7613. int flag = ADDSKILL_TEMP;
  7614. TBL_PC* sd;
  7615. struct script_data *data;
  7616. const char* command = script_getfuncname(st);
  7617. sd = script_rid2sd(st);
  7618. if( sd == NULL )
  7619. return 0;// no player attached, report source
  7620. if (strcmpi(command, "addtoskill") == 0)
  7621. flag = ADDSKILL_TEMP_ADDLEVEL;
  7622. data = script_getdata(st, 2);
  7623. get_val(st, data); // Convert into value in case of a variable
  7624. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7625. level = script_getnum(st,3);
  7626. if( script_hasdata(st,4) )
  7627. flag = script_getnum(st,4);
  7628. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  7629. return SCRIPT_CMD_SUCCESS;
  7630. }
  7631. /// Increases the level of a guild skill.
  7632. ///
  7633. /// guildskill <skill id>,<amount>;
  7634. /// guildskill "<skill name>",<amount>;
  7635. BUILDIN_FUNC(guildskill)
  7636. {
  7637. int id;
  7638. int level;
  7639. TBL_PC* sd;
  7640. int i;
  7641. struct script_data *data;
  7642. sd = script_rid2sd(st);
  7643. if( sd == NULL )
  7644. return 0;// no player attached, report source
  7645. data = script_getdata(st, 2);
  7646. get_val(st, data); // Convert into value in case of a variable
  7647. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7648. level = script_getnum(st,3);
  7649. for( i=0; i < level; i++ )
  7650. guild_skillup(sd, id);
  7651. return SCRIPT_CMD_SUCCESS;
  7652. }
  7653. /// Returns the level of the player skill.
  7654. ///
  7655. /// getskilllv(<skill id>) -> <level>
  7656. /// getskilllv("<skill name>") -> <level>
  7657. BUILDIN_FUNC(getskilllv)
  7658. {
  7659. int id;
  7660. TBL_PC* sd;
  7661. struct script_data *data;
  7662. sd = script_rid2sd(st);
  7663. if( sd == NULL )
  7664. return 0;// no player attached, report source
  7665. data = script_getdata(st, 2);
  7666. get_val(st, data); // Convert into value in case of a variable
  7667. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7668. script_pushint(st, pc_checkskill(sd,id));
  7669. return SCRIPT_CMD_SUCCESS;
  7670. }
  7671. /// Returns the level of the guild skill.
  7672. ///
  7673. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7674. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7675. BUILDIN_FUNC(getgdskilllv)
  7676. {
  7677. int guild_id;
  7678. uint16 skill_id;
  7679. struct guild* g;
  7680. struct script_data *data;
  7681. guild_id = script_getnum(st,2);
  7682. data = script_getdata(st, 3);
  7683. get_val(st, data); // Convert into value in case of a variable
  7684. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7685. g = guild_search(guild_id);
  7686. if( g == NULL )
  7687. script_pushint(st, -1);
  7688. else
  7689. script_pushint(st, guild_checkskill(g,skill_id));
  7690. return SCRIPT_CMD_SUCCESS;
  7691. }
  7692. /// Returns the 'basic_skill_check' setting.
  7693. /// This config determines if the server checks the skill level of NV_BASIC
  7694. /// before allowing the basic actions.
  7695. ///
  7696. /// basicskillcheck() -> <bool>
  7697. BUILDIN_FUNC(basicskillcheck)
  7698. {
  7699. script_pushint(st, battle_config.basic_skill_check);
  7700. return SCRIPT_CMD_SUCCESS;
  7701. }
  7702. /// Returns the GM level of the player.
  7703. ///
  7704. /// getgmlevel() -> <level>
  7705. BUILDIN_FUNC(getgmlevel)
  7706. {
  7707. TBL_PC* sd;
  7708. sd = script_rid2sd(st);
  7709. if( sd == NULL )
  7710. return 0;// no player attached, report source
  7711. script_pushint(st, pc_get_group_level(sd));
  7712. return SCRIPT_CMD_SUCCESS;
  7713. }
  7714. /// Returns the group ID of the player.
  7715. ///
  7716. /// getgroupid() -> <int>
  7717. BUILDIN_FUNC(getgroupid)
  7718. {
  7719. TBL_PC* sd;
  7720. sd = script_rid2sd(st);
  7721. if (sd == NULL)
  7722. return 1; // no player attached, report source
  7723. script_pushint(st, pc_get_group_id(sd));
  7724. return SCRIPT_CMD_SUCCESS;
  7725. }
  7726. /// Terminates the execution of this script instance.
  7727. ///
  7728. /// end
  7729. BUILDIN_FUNC(end)
  7730. {
  7731. TBL_PC* sd;
  7732. sd = map_id2sd(st->rid);
  7733. st->state = END;
  7734. if( st->mes_active )
  7735. st->mes_active = 0;
  7736. if( sd )
  7737. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7738. return SCRIPT_CMD_SUCCESS;
  7739. }
  7740. /// Checks if the player has that effect state (option).
  7741. ///
  7742. /// checkoption(<option>) -> <bool>
  7743. BUILDIN_FUNC(checkoption)
  7744. {
  7745. int option;
  7746. TBL_PC* sd;
  7747. sd = script_rid2sd(st);
  7748. if( sd == NULL )
  7749. return 0;// no player attached, report source
  7750. option = script_getnum(st,2);
  7751. if( sd->sc.option&option )
  7752. script_pushint(st, 1);
  7753. else
  7754. script_pushint(st, 0);
  7755. return SCRIPT_CMD_SUCCESS;
  7756. }
  7757. /// Checks if the player is in that body state (opt1).
  7758. ///
  7759. /// checkoption1(<opt1>) -> <bool>
  7760. BUILDIN_FUNC(checkoption1)
  7761. {
  7762. int opt1;
  7763. TBL_PC* sd;
  7764. sd = script_rid2sd(st);
  7765. if( sd == NULL )
  7766. return 0;// no player attached, report source
  7767. opt1 = script_getnum(st,2);
  7768. if( sd->sc.opt1 == opt1 )
  7769. script_pushint(st, 1);
  7770. else
  7771. script_pushint(st, 0);
  7772. return SCRIPT_CMD_SUCCESS;
  7773. }
  7774. /// Checks if the player has that health state (opt2).
  7775. ///
  7776. /// checkoption2(<opt2>) -> <bool>
  7777. BUILDIN_FUNC(checkoption2)
  7778. {
  7779. int opt2;
  7780. TBL_PC* sd;
  7781. sd = script_rid2sd(st);
  7782. if( sd == NULL )
  7783. return 0;// no player attached, report source
  7784. opt2 = script_getnum(st,2);
  7785. if( sd->sc.opt2&opt2 )
  7786. script_pushint(st, 1);
  7787. else
  7788. script_pushint(st, 0);
  7789. return SCRIPT_CMD_SUCCESS;
  7790. }
  7791. /// Changes the effect state (option) of the player.
  7792. /// <flag> defaults to 1
  7793. /// <flag>=0 : removes the option
  7794. /// <flag>=other : adds the option
  7795. ///
  7796. /// setoption <option>,<flag>;
  7797. /// setoption <option>;
  7798. BUILDIN_FUNC(setoption)
  7799. {
  7800. int option;
  7801. int flag = 1;
  7802. TBL_PC* sd;
  7803. sd = script_rid2sd(st);
  7804. if( sd == NULL )
  7805. return 0;// no player attached, report source
  7806. option = script_getnum(st,2);
  7807. if( script_hasdata(st,3) )
  7808. flag = script_getnum(st,3);
  7809. else if( !option ){// Request to remove everything.
  7810. flag = 0;
  7811. option = OPTION_FALCON|OPTION_RIDING;
  7812. #ifndef NEW_CARTS
  7813. option |= OPTION_CART;
  7814. #endif
  7815. }
  7816. if( flag ){// Add option
  7817. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  7818. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  7819. pc_setoption(sd, sd->sc.option|option);
  7820. } else// Remove option
  7821. pc_setoption(sd, sd->sc.option&~option);
  7822. return SCRIPT_CMD_SUCCESS;
  7823. }
  7824. /// Returns if the player has a cart.
  7825. ///
  7826. /// checkcart() -> <bool>
  7827. ///
  7828. /// @author Valaris
  7829. BUILDIN_FUNC(checkcart)
  7830. {
  7831. TBL_PC* sd;
  7832. sd = script_rid2sd(st);
  7833. if( sd == NULL )
  7834. return 0;// no player attached, report source
  7835. if( pc_iscarton(sd) )
  7836. script_pushint(st, 1);
  7837. else
  7838. script_pushint(st, 0);
  7839. return SCRIPT_CMD_SUCCESS;
  7840. }
  7841. /// Sets the cart of the player.
  7842. /// <type> defaults to 1
  7843. /// <type>=0 : removes the cart
  7844. /// <type>=1 : Normal cart
  7845. /// <type>=2 : Wooden cart
  7846. /// <type>=3 : Covered cart with flowers and ferns
  7847. /// <type>=4 : Wooden cart with a Panda doll on the back
  7848. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  7849. ///
  7850. /// setcart <type>;
  7851. /// setcart;
  7852. BUILDIN_FUNC(setcart)
  7853. {
  7854. int type = 1;
  7855. TBL_PC* sd;
  7856. sd = script_rid2sd(st);
  7857. if( sd == NULL )
  7858. return 0;// no player attached, report source
  7859. if( script_hasdata(st,2) )
  7860. type = script_getnum(st,2);
  7861. pc_setcart(sd, type);
  7862. return SCRIPT_CMD_SUCCESS;
  7863. }
  7864. /// Returns if the player has a falcon.
  7865. ///
  7866. /// checkfalcon() -> <bool>
  7867. ///
  7868. /// @author Valaris
  7869. BUILDIN_FUNC(checkfalcon)
  7870. {
  7871. TBL_PC* sd;
  7872. sd = script_rid2sd(st);
  7873. if( sd == NULL )
  7874. return 0;// no player attached, report source
  7875. if( pc_isfalcon(sd) )
  7876. script_pushint(st, 1);
  7877. else
  7878. script_pushint(st, 0);
  7879. return SCRIPT_CMD_SUCCESS;
  7880. }
  7881. /// Sets if the player has a falcon or not.
  7882. /// <flag> defaults to 1
  7883. ///
  7884. /// setfalcon <flag>;
  7885. /// setfalcon;
  7886. BUILDIN_FUNC(setfalcon)
  7887. {
  7888. int flag = 1;
  7889. TBL_PC* sd;
  7890. sd = script_rid2sd(st);
  7891. if( sd == NULL )
  7892. return 0;// no player attached, report source
  7893. if( script_hasdata(st,2) )
  7894. flag = script_getnum(st,2);
  7895. pc_setfalcon(sd, flag);
  7896. return SCRIPT_CMD_SUCCESS;
  7897. }
  7898. /// Returns if the player is riding.
  7899. ///
  7900. /// checkriding() -> <bool>
  7901. ///
  7902. /// @author Valaris
  7903. BUILDIN_FUNC(checkriding)
  7904. {
  7905. TBL_PC* sd;
  7906. sd = script_rid2sd(st);
  7907. if( sd == NULL )
  7908. return 0;// no player attached, report source
  7909. if( pc_isriding(sd) )
  7910. script_pushint(st, 1);
  7911. else
  7912. script_pushint(st, 0);
  7913. return SCRIPT_CMD_SUCCESS;
  7914. }
  7915. /// Sets if the player is riding.
  7916. /// <flag> defaults to 1
  7917. ///
  7918. /// setriding <flag>;
  7919. /// setriding;
  7920. BUILDIN_FUNC(setriding)
  7921. {
  7922. int flag = 1;
  7923. TBL_PC* sd;
  7924. sd = script_rid2sd(st);
  7925. if( sd == NULL )
  7926. return 0;// no player attached, report source
  7927. if( script_hasdata(st,2) )
  7928. flag = script_getnum(st,2);
  7929. pc_setriding(sd, flag);
  7930. return SCRIPT_CMD_SUCCESS;
  7931. }
  7932. /// Returns if the player has a warg.
  7933. ///
  7934. /// checkwug() -> <bool>
  7935. ///
  7936. BUILDIN_FUNC(checkwug)
  7937. {
  7938. TBL_PC* sd;
  7939. sd = script_rid2sd(st);
  7940. if( sd == NULL )
  7941. return 0;// no player attached, report source
  7942. if( pc_iswug(sd) || pc_isridingwug(sd) )
  7943. script_pushint(st, 1);
  7944. else
  7945. script_pushint(st, 0);
  7946. return SCRIPT_CMD_SUCCESS;
  7947. }
  7948. /// Returns if the player is wearing MADO Gear.
  7949. ///
  7950. /// checkmadogear() -> <bool>
  7951. ///
  7952. BUILDIN_FUNC(checkmadogear)
  7953. {
  7954. TBL_PC* sd;
  7955. sd = script_rid2sd(st);
  7956. if( sd == NULL )
  7957. return 0;// no player attached, report source
  7958. if( pc_ismadogear(sd) )
  7959. script_pushint(st, 1);
  7960. else
  7961. script_pushint(st, 0);
  7962. return SCRIPT_CMD_SUCCESS;
  7963. }
  7964. /// Sets if the player is riding MADO Gear.
  7965. /// <flag> defaults to 1
  7966. ///
  7967. /// setmadogear <flag>;
  7968. /// setmadogear;
  7969. BUILDIN_FUNC(setmadogear)
  7970. {
  7971. int flag = 1;
  7972. TBL_PC* sd;
  7973. sd = script_rid2sd(st);
  7974. if( sd == NULL )
  7975. return 0;// no player attached, report source
  7976. if( script_hasdata(st,2) )
  7977. flag = script_getnum(st,2);
  7978. pc_setmadogear(sd, flag);
  7979. return SCRIPT_CMD_SUCCESS;
  7980. }
  7981. /// Sets the save point of the player.
  7982. ///
  7983. /// save "<map name>",<x>,<y>
  7984. /// savepoint "<map name>",<x>,<y>
  7985. BUILDIN_FUNC(savepoint)
  7986. {
  7987. int x;
  7988. int y;
  7989. short map_idx;
  7990. const char* str;
  7991. TBL_PC* sd;
  7992. sd = script_rid2sd(st);
  7993. if( sd == NULL )
  7994. return 0;// no player attached, report source
  7995. str = script_getstr(st, 2);
  7996. x = script_getnum(st,3);
  7997. y = script_getnum(st,4);
  7998. map_idx = mapindex_name2id(str);
  7999. if( map_idx )
  8000. pc_setsavepoint(sd, map_idx, x, y);
  8001. return SCRIPT_CMD_SUCCESS;
  8002. }
  8003. /*==========================================
  8004. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8005. *------------------------------------------*/
  8006. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8007. {
  8008. int type;
  8009. time_t timer;
  8010. struct tm *t;
  8011. type=script_getnum(st,2);
  8012. switch(type){
  8013. case 2:
  8014. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8015. // from the system clock.)
  8016. script_pushint(st,(int)time(NULL));
  8017. break;
  8018. case 1:
  8019. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8020. time(&timer);
  8021. t=localtime(&timer);
  8022. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8023. break;
  8024. case 0:
  8025. default:
  8026. //type 0:(System Ticks)
  8027. script_pushint(st,gettick());
  8028. break;
  8029. }
  8030. return SCRIPT_CMD_SUCCESS;
  8031. }
  8032. /*==========================================
  8033. * GetTime(Type);
  8034. * 1: Sec 2: Min 3: Hour
  8035. * 4: WeekDay 5: MonthDay 6: Month
  8036. * 7: Year
  8037. *------------------------------------------*/
  8038. BUILDIN_FUNC(gettime) /* Asgard Version */
  8039. {
  8040. int type;
  8041. time_t timer;
  8042. struct tm *t;
  8043. type=script_getnum(st,2);
  8044. time(&timer);
  8045. t=localtime(&timer);
  8046. switch(type){
  8047. case 1://Sec(0~59)
  8048. script_pushint(st,t->tm_sec);
  8049. break;
  8050. case 2://Min(0~59)
  8051. script_pushint(st,t->tm_min);
  8052. break;
  8053. case 3://Hour(0~23)
  8054. script_pushint(st,t->tm_hour);
  8055. break;
  8056. case 4://WeekDay(0~6)
  8057. script_pushint(st,t->tm_wday);
  8058. break;
  8059. case 5://MonthDay(01~31)
  8060. script_pushint(st,t->tm_mday);
  8061. break;
  8062. case 6://Month(01~12)
  8063. script_pushint(st,t->tm_mon+1);
  8064. break;
  8065. case 7://Year(20xx)
  8066. script_pushint(st,t->tm_year+1900);
  8067. break;
  8068. case 8://Year Day(01~366)
  8069. script_pushint(st,t->tm_yday+1);
  8070. break;
  8071. default://(format error)
  8072. script_pushint(st,-1);
  8073. break;
  8074. }
  8075. return SCRIPT_CMD_SUCCESS;
  8076. }
  8077. /*==========================================
  8078. * GetTimeStr("TimeFMT", Length);
  8079. *------------------------------------------*/
  8080. BUILDIN_FUNC(gettimestr)
  8081. {
  8082. char *tmpstr;
  8083. const char *fmtstr;
  8084. int maxlen;
  8085. time_t now = time(NULL);
  8086. fmtstr=script_getstr(st,2);
  8087. maxlen=script_getnum(st,3);
  8088. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8089. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8090. tmpstr[maxlen]='\0';
  8091. script_pushstr(st,tmpstr);
  8092. return SCRIPT_CMD_SUCCESS;
  8093. }
  8094. /*==========================================
  8095. * Open player storage
  8096. *------------------------------------------*/
  8097. BUILDIN_FUNC(openstorage)
  8098. {
  8099. TBL_PC* sd;
  8100. sd = script_rid2sd(st);
  8101. if( sd == NULL )
  8102. return 0;
  8103. storage_storageopen(sd);
  8104. return SCRIPT_CMD_SUCCESS;
  8105. }
  8106. BUILDIN_FUNC(guildopenstorage)
  8107. {
  8108. TBL_PC* sd;
  8109. int ret;
  8110. sd = script_rid2sd(st);
  8111. if( sd == NULL )
  8112. return 0;
  8113. ret = gstorage_storageopen(sd);
  8114. script_pushint(st,ret);
  8115. return SCRIPT_CMD_SUCCESS;
  8116. }
  8117. /*==========================================
  8118. * Make player use a skill trought item usage
  8119. *------------------------------------------*/
  8120. /// itemskill <skill id>,<level>
  8121. /// itemskill "<skill name>",<level>
  8122. BUILDIN_FUNC(itemskill)
  8123. {
  8124. int id;
  8125. int lv;
  8126. TBL_PC* sd;
  8127. struct script_data *data;
  8128. sd = script_rid2sd(st);
  8129. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8130. return 0;
  8131. data = script_getdata(st, 2);
  8132. get_val(st, data); // Convert into value in case of a variable
  8133. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8134. lv = script_getnum(st,3);
  8135. sd->skillitem=id;
  8136. sd->skillitemlv=lv;
  8137. clif_item_skill(sd,id,lv);
  8138. return SCRIPT_CMD_SUCCESS;
  8139. }
  8140. /*==========================================
  8141. * Attempt to create an item
  8142. *------------------------------------------*/
  8143. BUILDIN_FUNC(produce)
  8144. {
  8145. int trigger;
  8146. TBL_PC* sd;
  8147. sd = script_rid2sd(st);
  8148. if( sd == NULL )
  8149. return 0;
  8150. trigger=script_getnum(st,2);
  8151. clif_skill_produce_mix_list(sd, -1, trigger);
  8152. return SCRIPT_CMD_SUCCESS;
  8153. }
  8154. /*==========================================
  8155. *
  8156. *------------------------------------------*/
  8157. BUILDIN_FUNC(cooking)
  8158. {
  8159. int trigger;
  8160. TBL_PC* sd;
  8161. sd = script_rid2sd(st);
  8162. if( sd == NULL )
  8163. return 0;
  8164. trigger=script_getnum(st,2);
  8165. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8166. return SCRIPT_CMD_SUCCESS;
  8167. }
  8168. /*==========================================
  8169. * Create a pet
  8170. *------------------------------------------*/
  8171. BUILDIN_FUNC(makepet)
  8172. {
  8173. TBL_PC* sd;
  8174. int id,pet_id;
  8175. id=script_getnum(st,2);
  8176. sd = script_rid2sd(st);
  8177. if( sd == NULL )
  8178. return 0;
  8179. pet_id = search_petDB_index(id, PET_CLASS);
  8180. if (pet_id < 0)
  8181. pet_id = search_petDB_index(id, PET_EGG);
  8182. if (pet_id >= 0 && sd) {
  8183. sd->catch_target_class = pet_db[pet_id].class_;
  8184. intif_create_pet(
  8185. sd->status.account_id, sd->status.char_id,
  8186. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8187. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8188. 100, 0, 1, pet_db[pet_id].jname);
  8189. }
  8190. return SCRIPT_CMD_SUCCESS;
  8191. }
  8192. /*==========================================
  8193. * Give player exp base,job * quest_exp_rate/100
  8194. *------------------------------------------*/
  8195. BUILDIN_FUNC(getexp)
  8196. {
  8197. TBL_PC* sd;
  8198. int base=0,job=0;
  8199. double bonus;
  8200. sd = script_rid2sd(st);
  8201. if( sd == NULL )
  8202. return 0;
  8203. base=script_getnum(st,2);
  8204. job =script_getnum(st,3);
  8205. if(base<0 || job<0)
  8206. return 0;
  8207. // bonus for npc-given exp
  8208. bonus = battle_config.quest_exp_rate / 100.;
  8209. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8210. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8211. pc_gainexp(sd, NULL, base, job, true);
  8212. return SCRIPT_CMD_SUCCESS;
  8213. }
  8214. /*==========================================
  8215. * Gain guild exp [Celest]
  8216. *------------------------------------------*/
  8217. BUILDIN_FUNC(guildgetexp)
  8218. {
  8219. TBL_PC* sd;
  8220. int exp;
  8221. sd = script_rid2sd(st);
  8222. if( sd == NULL )
  8223. return 0;
  8224. exp = script_getnum(st,2);
  8225. if(exp < 0)
  8226. return 0;
  8227. if(sd && sd->status.guild_id > 0)
  8228. guild_getexp (sd, exp);
  8229. return SCRIPT_CMD_SUCCESS;
  8230. }
  8231. /*==========================================
  8232. * Changes the guild master of a guild [Skotlex]
  8233. *------------------------------------------*/
  8234. BUILDIN_FUNC(guildchangegm)
  8235. {
  8236. TBL_PC *sd;
  8237. int guild_id;
  8238. const char *name;
  8239. guild_id = script_getnum(st,2);
  8240. name = script_getstr(st,3);
  8241. sd=map_nick2sd(name);
  8242. if (!sd)
  8243. script_pushint(st,0);
  8244. else
  8245. script_pushint(st,guild_gm_change(guild_id, sd));
  8246. return SCRIPT_CMD_SUCCESS;
  8247. }
  8248. /*==========================================
  8249. * Spawn a monster:
  8250. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8251. *------------------------------------------*/
  8252. BUILDIN_FUNC(monster)
  8253. {
  8254. const char* mapn = script_getstr(st,2);
  8255. int x = script_getnum(st,3);
  8256. int y = script_getnum(st,4);
  8257. const char* str = script_getstr(st,5);
  8258. int class_ = script_getnum(st,6);
  8259. int amount = script_getnum(st,7);
  8260. const char* event = "";
  8261. unsigned int size = SZ_SMALL;
  8262. unsigned int ai = AI_NONE;
  8263. struct map_session_data* sd;
  8264. int16 m;
  8265. int i;
  8266. if (script_hasdata(st, 8)) {
  8267. event = script_getstr(st, 8);
  8268. check_event(st, event);
  8269. }
  8270. if (script_hasdata(st, 9)) {
  8271. size = script_getnum(st, 9);
  8272. if (size > 3) {
  8273. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8274. return 1;
  8275. }
  8276. }
  8277. if (script_hasdata(st, 10)) {
  8278. ai = script_getnum(st, 10);
  8279. if (ai >= AI_MAX) {
  8280. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8281. return 1;
  8282. }
  8283. }
  8284. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8285. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8286. return 1;
  8287. }
  8288. sd = map_id2sd(st->rid);
  8289. if (sd && strcmp(mapn, "this") == 0)
  8290. m = sd->bl.m;
  8291. else
  8292. m = map_mapname2mapid(mapn);
  8293. for(i = 0; i < amount; i++) { //not optimised
  8294. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8295. if (mobid)
  8296. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8297. }
  8298. return SCRIPT_CMD_SUCCESS;
  8299. }
  8300. /*==========================================
  8301. * Request List of Monster Drops
  8302. *------------------------------------------*/
  8303. BUILDIN_FUNC(getmobdrops)
  8304. {
  8305. int class_ = script_getnum(st,2);
  8306. int i, j = 0;
  8307. struct mob_db *mob;
  8308. if( !mobdb_checkid(class_) )
  8309. {
  8310. script_pushint(st, 0);
  8311. return 0;
  8312. }
  8313. mob = mob_db(class_);
  8314. for( i = 0; i < MAX_MOB_DROP; i++ )
  8315. {
  8316. if( mob->dropitem[i].nameid < 1 )
  8317. continue;
  8318. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8319. continue;
  8320. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8321. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8322. j++;
  8323. }
  8324. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8325. script_pushint(st, 1);
  8326. return SCRIPT_CMD_SUCCESS;
  8327. }
  8328. /*==========================================
  8329. * Spawn a monster in a random location
  8330. * in x0,x1,y0,y1 area.
  8331. *------------------------------------------*/
  8332. BUILDIN_FUNC(areamonster)
  8333. {
  8334. const char* mapn = script_getstr(st,2);
  8335. int x0 = script_getnum(st,3);
  8336. int y0 = script_getnum(st,4);
  8337. int x1 = script_getnum(st,5);
  8338. int y1 = script_getnum(st,6);
  8339. const char* str = script_getstr(st,7);
  8340. int class_ = script_getnum(st,8);
  8341. int amount = script_getnum(st,9);
  8342. const char* event = "";
  8343. unsigned int size = SZ_SMALL;
  8344. unsigned int ai = AI_NONE;
  8345. struct map_session_data* sd;
  8346. int16 m;
  8347. int i;
  8348. if (script_hasdata(st,10)) {
  8349. event = script_getstr(st, 10);
  8350. check_event(st, event);
  8351. }
  8352. if (script_hasdata(st, 11)) {
  8353. size = script_getnum(st, 11);
  8354. if (size > 3) {
  8355. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8356. return 1;
  8357. }
  8358. }
  8359. if (script_hasdata(st, 12)) {
  8360. ai = script_getnum(st, 12);
  8361. if (ai >= AI_MAX) {
  8362. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8363. return 1;
  8364. }
  8365. }
  8366. sd = map_id2sd(st->rid);
  8367. if (sd && strcmp(mapn, "this") == 0)
  8368. m = sd->bl.m;
  8369. else
  8370. m = map_mapname2mapid(mapn);
  8371. for(i = 0; i < amount; i++) { //not optimised
  8372. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8373. if (mobid)
  8374. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8375. }
  8376. return SCRIPT_CMD_SUCCESS;
  8377. }
  8378. /*==========================================
  8379. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8380. *------------------------------------------*/
  8381. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8382. { //same fix but with killmonster instead - stripping events from mobs.
  8383. TBL_MOB* md = (TBL_MOB*)bl;
  8384. char *event=va_arg(ap,char *);
  8385. int allflag=va_arg(ap,int);
  8386. md->state.npc_killmonster = 1;
  8387. if(!allflag){
  8388. if(strcmp(event,md->npc_event)==0)
  8389. status_kill(bl);
  8390. }else{
  8391. if(!md->spawn)
  8392. status_kill(bl);
  8393. }
  8394. md->state.npc_killmonster = 0;
  8395. return 0;
  8396. }
  8397. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8398. {
  8399. TBL_MOB* md = (TBL_MOB*)bl;
  8400. char *event=va_arg(ap,char *);
  8401. int allflag=va_arg(ap,int);
  8402. if(!allflag){
  8403. if(strcmp(event,md->npc_event)==0)
  8404. status_kill(bl);
  8405. }else{
  8406. if(!md->spawn)
  8407. status_kill(bl);
  8408. }
  8409. return 0;
  8410. }
  8411. BUILDIN_FUNC(killmonster)
  8412. {
  8413. const char *mapname,*event;
  8414. int16 m,allflag=0;
  8415. mapname=script_getstr(st,2);
  8416. event=script_getstr(st,3);
  8417. if(strcmp(event,"All")==0)
  8418. allflag = 1;
  8419. else
  8420. check_event(st, event);
  8421. if( (m=map_mapname2mapid(mapname))<0 )
  8422. return 0;
  8423. if( script_hasdata(st,4) ) {
  8424. if ( script_getnum(st,4) == 1 ) {
  8425. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8426. return 0;
  8427. }
  8428. }
  8429. map_freeblock_lock();
  8430. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8431. map_freeblock_unlock();
  8432. return SCRIPT_CMD_SUCCESS;
  8433. }
  8434. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8435. { //Strips the event from the mob if it's killed the old method.
  8436. struct mob_data *md;
  8437. md = BL_CAST(BL_MOB, bl);
  8438. if (md->npc_event[0])
  8439. md->npc_event[0] = 0;
  8440. status_kill(bl);
  8441. return 0;
  8442. }
  8443. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8444. {
  8445. status_kill(bl);
  8446. return 0;
  8447. }
  8448. BUILDIN_FUNC(killmonsterall)
  8449. {
  8450. const char *mapname;
  8451. int16 m;
  8452. mapname=script_getstr(st,2);
  8453. if( (m = map_mapname2mapid(mapname))<0 )
  8454. return 0;
  8455. if( script_hasdata(st,3) ) {
  8456. if ( script_getnum(st,3) == 1 ) {
  8457. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8458. return 0;
  8459. }
  8460. }
  8461. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8462. return SCRIPT_CMD_SUCCESS;
  8463. }
  8464. /*==========================================
  8465. * Creates a clone of a player.
  8466. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8467. *------------------------------------------*/
  8468. BUILDIN_FUNC(clone)
  8469. {
  8470. TBL_PC *sd, *msd=NULL;
  8471. uint32 char_id;
  8472. int master_id=0,x,y, mode = 0, flag = 0, m;
  8473. unsigned int duration = 0;
  8474. const char *mapname,*event;
  8475. mapname=script_getstr(st,2);
  8476. x=script_getnum(st,3);
  8477. y=script_getnum(st,4);
  8478. event=script_getstr(st,5);
  8479. char_id=script_getnum(st,6);
  8480. if( script_hasdata(st,7) )
  8481. master_id=script_getnum(st,7);
  8482. if( script_hasdata(st,8) )
  8483. mode=script_getnum(st,8);
  8484. if( script_hasdata(st,9) )
  8485. flag=script_getnum(st,9);
  8486. if( script_hasdata(st,10) )
  8487. duration=script_getnum(st,10);
  8488. check_event(st, event);
  8489. m = map_mapname2mapid(mapname);
  8490. if (m < 0) return 0;
  8491. sd = map_charid2sd(char_id);
  8492. if (master_id) {
  8493. msd = map_charid2sd(master_id);
  8494. if (msd)
  8495. master_id = msd->bl.id;
  8496. else
  8497. master_id = 0;
  8498. }
  8499. if (sd) //Return ID of newly crafted clone.
  8500. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8501. else //Failed to create clone.
  8502. script_pushint(st,0);
  8503. return SCRIPT_CMD_SUCCESS;
  8504. }
  8505. /*==========================================
  8506. *------------------------------------------*/
  8507. BUILDIN_FUNC(doevent)
  8508. {
  8509. const char* event = script_getstr(st,2);
  8510. struct map_session_data* sd;
  8511. if( ( sd = script_rid2sd(st) ) == NULL )
  8512. {
  8513. return 0;
  8514. }
  8515. check_event(st, event);
  8516. npc_event(sd, event, 0);
  8517. return SCRIPT_CMD_SUCCESS;
  8518. }
  8519. /*==========================================
  8520. *------------------------------------------*/
  8521. BUILDIN_FUNC(donpcevent)
  8522. {
  8523. const char* event = script_getstr(st,2);
  8524. check_event(st, event);
  8525. if( !npc_event_do(event) ) {
  8526. struct npc_data * nd = map_id2nd(st->oid);
  8527. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8528. script_pushint(st, 0);
  8529. } else
  8530. script_pushint(st, 1);
  8531. return SCRIPT_CMD_SUCCESS;
  8532. }
  8533. /// for Aegis compatibility
  8534. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8535. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8536. {
  8537. const char* npc = script_getstr(st,2);
  8538. const char* command = script_getstr(st,3);
  8539. char event[EVENT_NAME_LENGTH];
  8540. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8541. check_event(st, event);
  8542. npc_event_do(event);
  8543. return SCRIPT_CMD_SUCCESS;
  8544. }
  8545. /*==========================================
  8546. *------------------------------------------*/
  8547. BUILDIN_FUNC(addtimer)
  8548. {
  8549. int tick = script_getnum(st,2);
  8550. const char* event = script_getstr(st, 3);
  8551. TBL_PC* sd;
  8552. check_event(st, event);
  8553. sd = script_rid2sd(st);
  8554. if( sd == NULL )
  8555. return 0;
  8556. if (!pc_addeventtimer(sd,tick,event)) {
  8557. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8558. return SCRIPT_CMD_FAILURE;
  8559. }
  8560. return SCRIPT_CMD_SUCCESS;
  8561. }
  8562. /*==========================================
  8563. *------------------------------------------*/
  8564. BUILDIN_FUNC(deltimer)
  8565. {
  8566. const char *event;
  8567. TBL_PC* sd;
  8568. event=script_getstr(st, 2);
  8569. sd = script_rid2sd(st);
  8570. if( sd == NULL )
  8571. return 0;
  8572. check_event(st, event);
  8573. pc_deleventtimer(sd,event);
  8574. return SCRIPT_CMD_SUCCESS;
  8575. }
  8576. /*==========================================
  8577. *------------------------------------------*/
  8578. BUILDIN_FUNC(addtimercount)
  8579. {
  8580. const char *event;
  8581. int tick;
  8582. TBL_PC* sd;
  8583. tick=script_getnum(st,2);
  8584. event=script_getstr(st,3);
  8585. sd = script_rid2sd(st);
  8586. if( sd == NULL )
  8587. return 0;
  8588. check_event(st, event);
  8589. pc_addeventtimercount(sd,event,tick);
  8590. return SCRIPT_CMD_SUCCESS;
  8591. }
  8592. /*==========================================
  8593. *------------------------------------------*/
  8594. BUILDIN_FUNC(initnpctimer)
  8595. {
  8596. struct npc_data *nd;
  8597. int flag = 0;
  8598. if( script_hasdata(st,3) )
  8599. { //Two arguments: NPC name and attach flag.
  8600. nd = npc_name2id(script_getstr(st, 2));
  8601. flag = script_getnum(st,3);
  8602. }
  8603. else if( script_hasdata(st,2) )
  8604. { //Check if argument is numeric (flag) or string (npc name)
  8605. struct script_data *data;
  8606. data = script_getdata(st,2);
  8607. get_val(st,data);
  8608. if( data_isstring(data) ) //NPC name
  8609. nd = npc_name2id(conv_str(st, data));
  8610. else if( data_isint(data) ) //Flag
  8611. {
  8612. nd = (struct npc_data *)map_id2bl(st->oid);
  8613. flag = conv_num(st,data);
  8614. }
  8615. else
  8616. {
  8617. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8618. return 1;
  8619. }
  8620. }
  8621. else
  8622. nd = (struct npc_data *)map_id2bl(st->oid);
  8623. if( !nd )
  8624. return 0;
  8625. if( flag ) //Attach
  8626. {
  8627. TBL_PC* sd = script_rid2sd(st);
  8628. if( sd == NULL )
  8629. return 0;
  8630. nd->u.scr.rid = sd->bl.id;
  8631. }
  8632. nd->u.scr.timertick = 0;
  8633. npc_settimerevent_tick(nd,0);
  8634. npc_timerevent_start(nd, st->rid);
  8635. return SCRIPT_CMD_SUCCESS;
  8636. }
  8637. /*==========================================
  8638. *------------------------------------------*/
  8639. BUILDIN_FUNC(startnpctimer)
  8640. {
  8641. struct npc_data *nd;
  8642. int flag = 0;
  8643. if( script_hasdata(st,3) )
  8644. { //Two arguments: NPC name and attach flag.
  8645. nd = npc_name2id(script_getstr(st, 2));
  8646. flag = script_getnum(st,3);
  8647. }
  8648. else if( script_hasdata(st,2) )
  8649. { //Check if argument is numeric (flag) or string (npc name)
  8650. struct script_data *data;
  8651. data = script_getdata(st,2);
  8652. get_val(st,data);
  8653. if( data_isstring(data) ) //NPC name
  8654. nd = npc_name2id(conv_str(st, data));
  8655. else if( data_isint(data) ) //Flag
  8656. {
  8657. nd = (struct npc_data *)map_id2bl(st->oid);
  8658. flag = conv_num(st,data);
  8659. }
  8660. else
  8661. {
  8662. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8663. return 1;
  8664. }
  8665. }
  8666. else
  8667. nd=(struct npc_data *)map_id2bl(st->oid);
  8668. if( !nd )
  8669. return 0;
  8670. if( flag ) //Attach
  8671. {
  8672. TBL_PC* sd = script_rid2sd(st);
  8673. if( sd == NULL )
  8674. return 0;
  8675. nd->u.scr.rid = sd->bl.id;
  8676. }
  8677. npc_timerevent_start(nd, st->rid);
  8678. return SCRIPT_CMD_SUCCESS;
  8679. }
  8680. /*==========================================
  8681. *------------------------------------------*/
  8682. BUILDIN_FUNC(stopnpctimer)
  8683. {
  8684. struct npc_data *nd;
  8685. int flag = 0;
  8686. if( script_hasdata(st,3) )
  8687. { //Two arguments: NPC name and attach flag.
  8688. nd = npc_name2id(script_getstr(st, 2));
  8689. flag = script_getnum(st,3);
  8690. }
  8691. else if( script_hasdata(st,2) )
  8692. { //Check if argument is numeric (flag) or string (npc name)
  8693. struct script_data *data;
  8694. data = script_getdata(st,2);
  8695. get_val(st,data);
  8696. if( data_isstring(data) ) //NPC name
  8697. nd = npc_name2id(conv_str(st, data));
  8698. else if( data_isint(data) ) //Flag
  8699. {
  8700. nd = (struct npc_data *)map_id2bl(st->oid);
  8701. flag = conv_num(st,data);
  8702. }
  8703. else
  8704. {
  8705. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8706. return 1;
  8707. }
  8708. }
  8709. else
  8710. nd=(struct npc_data *)map_id2bl(st->oid);
  8711. if( !nd )
  8712. return 0;
  8713. if( flag ) //Detach
  8714. nd->u.scr.rid = 0;
  8715. npc_timerevent_stop(nd);
  8716. return SCRIPT_CMD_SUCCESS;
  8717. }
  8718. /*==========================================
  8719. *------------------------------------------*/
  8720. BUILDIN_FUNC(getnpctimer)
  8721. {
  8722. struct npc_data *nd;
  8723. TBL_PC *sd;
  8724. int type = script_getnum(st,2);
  8725. int val = 0;
  8726. if( script_hasdata(st,3) )
  8727. nd = npc_name2id(script_getstr(st,3));
  8728. else
  8729. nd = (struct npc_data *)map_id2bl(st->oid);
  8730. if( !nd || nd->bl.type != BL_NPC )
  8731. {
  8732. script_pushint(st,0);
  8733. ShowError("getnpctimer: Invalid NPC.\n");
  8734. return 1;
  8735. }
  8736. switch( type )
  8737. {
  8738. case 0: val = npc_gettimerevent_tick(nd); break;
  8739. case 1:
  8740. if( nd->u.scr.rid )
  8741. {
  8742. sd = map_id2sd(nd->u.scr.rid);
  8743. if( !sd )
  8744. {
  8745. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8746. break;
  8747. }
  8748. val = (sd->npc_timer_id != INVALID_TIMER);
  8749. }
  8750. else
  8751. val = (nd->u.scr.timerid != INVALID_TIMER);
  8752. break;
  8753. case 2: val = nd->u.scr.timeramount; break;
  8754. }
  8755. script_pushint(st,val);
  8756. return SCRIPT_CMD_SUCCESS;
  8757. }
  8758. /*==========================================
  8759. *------------------------------------------*/
  8760. BUILDIN_FUNC(setnpctimer)
  8761. {
  8762. int tick;
  8763. struct npc_data *nd;
  8764. tick = script_getnum(st,2);
  8765. if( script_hasdata(st,3) )
  8766. nd = npc_name2id(script_getstr(st,3));
  8767. else
  8768. nd = (struct npc_data *)map_id2bl(st->oid);
  8769. if( !nd || nd->bl.type != BL_NPC )
  8770. {
  8771. script_pushint(st,1);
  8772. ShowError("setnpctimer: Invalid NPC.\n");
  8773. return 1;
  8774. }
  8775. npc_settimerevent_tick(nd,tick);
  8776. script_pushint(st,0);
  8777. return SCRIPT_CMD_SUCCESS;
  8778. }
  8779. /*==========================================
  8780. * attaches the player rid to the timer [Celest]
  8781. *------------------------------------------*/
  8782. BUILDIN_FUNC(attachnpctimer)
  8783. {
  8784. TBL_PC *sd;
  8785. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  8786. if( !nd || nd->bl.type != BL_NPC )
  8787. {
  8788. script_pushint(st,1);
  8789. ShowError("setnpctimer: Invalid NPC.\n");
  8790. return 1;
  8791. }
  8792. if( script_hasdata(st,2) )
  8793. sd = map_nick2sd(script_getstr(st,2));
  8794. else
  8795. sd = script_rid2sd(st);
  8796. if( !sd )
  8797. {
  8798. script_pushint(st,1);
  8799. ShowWarning("attachnpctimer: Invalid player.\n");
  8800. return 1;
  8801. }
  8802. nd->u.scr.rid = sd->bl.id;
  8803. script_pushint(st,0);
  8804. return SCRIPT_CMD_SUCCESS;
  8805. }
  8806. /*==========================================
  8807. * detaches a player rid from the timer [Celest]
  8808. *------------------------------------------*/
  8809. BUILDIN_FUNC(detachnpctimer)
  8810. {
  8811. struct npc_data *nd;
  8812. if( script_hasdata(st,2) )
  8813. nd = npc_name2id(script_getstr(st,2));
  8814. else
  8815. nd = (struct npc_data *)map_id2bl(st->oid);
  8816. if( !nd || nd->bl.type != BL_NPC )
  8817. {
  8818. script_pushint(st,1);
  8819. ShowError("detachnpctimer: Invalid NPC.\n");
  8820. return 1;
  8821. }
  8822. nd->u.scr.rid = 0;
  8823. script_pushint(st,0);
  8824. return SCRIPT_CMD_SUCCESS;
  8825. }
  8826. /*==========================================
  8827. * To avoid "player not attached" script errors, this function is provided,
  8828. * it checks if there is a player attached to the current script. [Skotlex]
  8829. * If no, returns 0, if yes, returns the account_id of the attached player.
  8830. *------------------------------------------*/
  8831. BUILDIN_FUNC(playerattached)
  8832. {
  8833. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  8834. script_pushint(st,0);
  8835. else
  8836. script_pushint(st,st->rid);
  8837. return SCRIPT_CMD_SUCCESS;
  8838. }
  8839. /*==========================================
  8840. *------------------------------------------*/
  8841. BUILDIN_FUNC(announce)
  8842. {
  8843. const char *mes = script_getstr(st,2);
  8844. int flag = script_getnum(st,3);
  8845. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  8846. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : 0x190; // default fontType (FW_NORMAL)
  8847. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  8848. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  8849. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  8850. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  8851. {
  8852. send_target target;
  8853. struct block_list *bl = (flag&BC_NPC) ? map_id2bl(st->oid) : (struct block_list *)script_rid2sd(st); // If bc_npc flag is set, use NPC as broadcast source
  8854. if (bl == NULL)
  8855. return 0;
  8856. switch (flag&BC_TARGET_MASK) {
  8857. case BC_MAP: target = ALL_SAMEMAP; break;
  8858. case BC_AREA: target = AREA; break;
  8859. case BC_SELF: target = SELF; break;
  8860. default: target = ALL_CLIENT; break; // BC_ALL
  8861. }
  8862. if (fontColor)
  8863. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  8864. else
  8865. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  8866. }
  8867. else
  8868. {
  8869. if (fontColor)
  8870. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  8871. else
  8872. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  8873. }
  8874. return SCRIPT_CMD_SUCCESS;
  8875. }
  8876. /*==========================================
  8877. *------------------------------------------*/
  8878. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  8879. {
  8880. char *mes = va_arg(ap, char *);
  8881. int len = va_arg(ap, int);
  8882. int type = va_arg(ap, int);
  8883. char *fontColor = va_arg(ap, char *);
  8884. short fontType = (short)va_arg(ap, int);
  8885. short fontSize = (short)va_arg(ap, int);
  8886. short fontAlign = (short)va_arg(ap, int);
  8887. short fontY = (short)va_arg(ap, int);
  8888. if (fontColor)
  8889. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  8890. else
  8891. clif_broadcast(bl, mes, len, type, SELF);
  8892. return 0;
  8893. }
  8894. BUILDIN_FUNC(mapannounce)
  8895. {
  8896. const char *mapname = script_getstr(st,2);
  8897. const char *mes = script_getstr(st,3);
  8898. int flag = script_getnum(st,4);
  8899. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  8900. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  8901. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  8902. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  8903. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  8904. int16 m;
  8905. if ((m = map_mapname2mapid(mapname)) < 0)
  8906. return 0;
  8907. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  8908. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  8909. return SCRIPT_CMD_SUCCESS;
  8910. }
  8911. /*==========================================
  8912. *------------------------------------------*/
  8913. BUILDIN_FUNC(areaannounce)
  8914. {
  8915. const char *mapname = script_getstr(st,2);
  8916. int x0 = script_getnum(st,3);
  8917. int y0 = script_getnum(st,4);
  8918. int x1 = script_getnum(st,5);
  8919. int y1 = script_getnum(st,6);
  8920. const char *mes = script_getstr(st,7);
  8921. int flag = script_getnum(st,8);
  8922. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  8923. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : 0x190; // default fontType (FW_NORMAL)
  8924. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  8925. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  8926. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  8927. int16 m;
  8928. if ((m = map_mapname2mapid(mapname)) < 0)
  8929. return 0;
  8930. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  8931. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  8932. return SCRIPT_CMD_SUCCESS;
  8933. }
  8934. /*==========================================
  8935. *------------------------------------------*/
  8936. BUILDIN_FUNC(getusers)
  8937. {
  8938. int flag, val = 0;
  8939. struct map_session_data* sd;
  8940. struct block_list* bl = NULL;
  8941. flag = script_getnum(st,2);
  8942. switch(flag&0x07)
  8943. {
  8944. case 0:
  8945. if(flag&0x8)
  8946. {// npc
  8947. bl = map_id2bl(st->oid);
  8948. }
  8949. else if((sd = script_rid2sd(st))!=NULL)
  8950. {// pc
  8951. bl = &sd->bl;
  8952. }
  8953. if(bl)
  8954. {
  8955. val = map[bl->m].users;
  8956. }
  8957. break;
  8958. case 1:
  8959. val = map_getusers();
  8960. break;
  8961. default:
  8962. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  8963. script_pushint(st,0);
  8964. return 1;
  8965. }
  8966. script_pushint(st,val);
  8967. return SCRIPT_CMD_SUCCESS;
  8968. }
  8969. /*==========================================
  8970. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  8971. *------------------------------------------*/
  8972. BUILDIN_FUNC(getmapguildusers)
  8973. {
  8974. const char *str;
  8975. int16 m;
  8976. int gid;
  8977. int c=0;
  8978. struct guild *g = NULL;
  8979. str=script_getstr(st,2);
  8980. gid=script_getnum(st,3);
  8981. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  8982. script_pushint(st,-1);
  8983. return 0;
  8984. }
  8985. g = guild_search(gid);
  8986. if (g){
  8987. unsigned short i;
  8988. for(i = 0; i < g->max_member; i++)
  8989. {
  8990. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  8991. c++;
  8992. }
  8993. }
  8994. script_pushint(st,c);
  8995. return SCRIPT_CMD_SUCCESS;
  8996. }
  8997. /*==========================================
  8998. *------------------------------------------*/
  8999. BUILDIN_FUNC(getmapusers)
  9000. {
  9001. const char *str;
  9002. int16 m;
  9003. str=script_getstr(st,2);
  9004. if( (m=map_mapname2mapid(str))< 0){
  9005. script_pushint(st,-1);
  9006. return 0;
  9007. }
  9008. script_pushint(st,map[m].users);
  9009. return SCRIPT_CMD_SUCCESS;
  9010. }
  9011. /*==========================================
  9012. *------------------------------------------*/
  9013. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9014. {
  9015. int *users=va_arg(ap,int *);
  9016. (*users)++;
  9017. return 0;
  9018. }
  9019. BUILDIN_FUNC(getareausers)
  9020. {
  9021. const char *str;
  9022. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9023. str=script_getstr(st,2);
  9024. x0=script_getnum(st,3);
  9025. y0=script_getnum(st,4);
  9026. x1=script_getnum(st,5);
  9027. y1=script_getnum(st,6);
  9028. if( (m=map_mapname2mapid(str))< 0){
  9029. script_pushint(st,-1);
  9030. return 0;
  9031. }
  9032. map_foreachinarea(buildin_getareausers_sub,
  9033. m,x0,y0,x1,y1,BL_PC,&users);
  9034. script_pushint(st,users);
  9035. return SCRIPT_CMD_SUCCESS;
  9036. }
  9037. /*==========================================
  9038. *------------------------------------------*/
  9039. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9040. {
  9041. int nameid=va_arg(ap,int);
  9042. int *amount=va_arg(ap,int *);
  9043. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9044. if(drop->item.nameid==nameid)
  9045. (*amount)+=drop->item.amount;
  9046. return 0;
  9047. }
  9048. BUILDIN_FUNC(getareadropitem)
  9049. {
  9050. const char *str;
  9051. int16 m,x0,y0,x1,y1;
  9052. int nameid,amount=0;
  9053. struct script_data *data;
  9054. str=script_getstr(st,2);
  9055. x0=script_getnum(st,3);
  9056. y0=script_getnum(st,4);
  9057. x1=script_getnum(st,5);
  9058. y1=script_getnum(st,6);
  9059. data=script_getdata(st,7);
  9060. get_val(st,data);
  9061. if( data_isstring(data) ){
  9062. const char *name=conv_str(st,data);
  9063. struct item_data *item_data = itemdb_searchname(name);
  9064. nameid=UNKNOWN_ITEM_ID;
  9065. if( item_data )
  9066. nameid=item_data->nameid;
  9067. }else
  9068. nameid=conv_num(st,data);
  9069. if( (m=map_mapname2mapid(str))< 0){
  9070. script_pushint(st,-1);
  9071. return 0;
  9072. }
  9073. map_foreachinarea(buildin_getareadropitem_sub,
  9074. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9075. script_pushint(st,amount);
  9076. return SCRIPT_CMD_SUCCESS;
  9077. }
  9078. /*==========================================
  9079. *------------------------------------------*/
  9080. BUILDIN_FUNC(enablenpc)
  9081. {
  9082. const char *str;
  9083. str=script_getstr(st,2);
  9084. npc_enable(str,1);
  9085. return SCRIPT_CMD_SUCCESS;
  9086. }
  9087. /*==========================================
  9088. *------------------------------------------*/
  9089. BUILDIN_FUNC(disablenpc)
  9090. {
  9091. const char *str;
  9092. str=script_getstr(st,2);
  9093. npc_enable(str,0);
  9094. return SCRIPT_CMD_SUCCESS;
  9095. }
  9096. /*==========================================
  9097. *------------------------------------------*/
  9098. BUILDIN_FUNC(hideoffnpc)
  9099. {
  9100. const char *str;
  9101. str=script_getstr(st,2);
  9102. npc_enable(str,2);
  9103. return SCRIPT_CMD_SUCCESS;
  9104. }
  9105. /*==========================================
  9106. *------------------------------------------*/
  9107. BUILDIN_FUNC(hideonnpc)
  9108. {
  9109. const char *str;
  9110. str=script_getstr(st,2);
  9111. npc_enable(str,4);
  9112. return SCRIPT_CMD_SUCCESS;
  9113. }
  9114. /* Starts a status effect on the target unit or on the attached player.
  9115. *
  9116. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9117. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9118. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9119. * <flag>
  9120. * &1: Cannot be avoided (it has to start)
  9121. * &2: Tick should not be reduced (by vit, luk, lv, etc)
  9122. * &4: sc_data loaded, no value has to be altered.
  9123. * &8: rate should not be reduced
  9124. */
  9125. BUILDIN_FUNC(sc_start)
  9126. {
  9127. TBL_NPC * nd = map_id2nd(st->oid);
  9128. struct block_list* bl;
  9129. enum sc_type type;
  9130. int tick, val1, val2, val3, val4=0, rate, flag;
  9131. char start_type;
  9132. const char* command = script_getfuncname(st);
  9133. if(strstr(command, "4"))
  9134. start_type = 4;
  9135. else if(strstr(command, "2"))
  9136. start_type = 2;
  9137. else
  9138. start_type = 1;
  9139. type = (sc_type)script_getnum(st,2);
  9140. tick = script_getnum(st,3);
  9141. val1 = script_getnum(st,4);
  9142. //If from NPC we make default flag 1 to be unavoidable
  9143. if(nd && nd->bl.id == fake_nd->bl.id)
  9144. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):2;
  9145. else
  9146. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):1;
  9147. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9148. if(script_hasdata(st,(6+start_type)))
  9149. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9150. else
  9151. bl = map_id2bl(st->rid);
  9152. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9153. {// When there isn't a duration specified, try to get it from the skill_db
  9154. tick = skill_get_time(status_sc2skill(type), val1);
  9155. }
  9156. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9157. bl = map_id2bl(potion_target);
  9158. tick /= 2;// Thrown potions only last half.
  9159. val4 = 1;// Mark that this was a thrown sc_effect
  9160. }
  9161. if(!bl)
  9162. return 0;
  9163. switch(start_type) {
  9164. case 1:
  9165. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9166. break;
  9167. case 2:
  9168. val2 = script_getnum(st,5);
  9169. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9170. break;
  9171. case 4:
  9172. val2 = script_getnum(st,5);
  9173. val3 = script_getnum(st,6);
  9174. val4 = script_getnum(st,7);
  9175. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9176. break;
  9177. }
  9178. return SCRIPT_CMD_SUCCESS;
  9179. }
  9180. /// Ends one or all status effects on the target unit or on the attached player.
  9181. ///
  9182. /// sc_end <effect_id>{,<unit_id>};
  9183. BUILDIN_FUNC(sc_end)
  9184. {
  9185. struct block_list* bl;
  9186. int type;
  9187. type = script_getnum(st, 2);
  9188. if (script_hasdata(st, 3))
  9189. bl = map_id2bl(script_getnum(st, 3));
  9190. else
  9191. bl = map_id2bl(st->rid);
  9192. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9193. bl = map_id2bl(potion_target);
  9194. if (!bl)
  9195. return 0;
  9196. if (type >= 0 && type < SC_MAX) {
  9197. struct status_change *sc = status_get_sc(bl);
  9198. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9199. if (!sce)
  9200. return 0;
  9201. switch (type) {
  9202. case SC_WEIGHT50:
  9203. case SC_WEIGHT90:
  9204. case SC_NOCHAT:
  9205. case SC_PUSH_CART:
  9206. case SC_ALL_RIDING:
  9207. case SC_STYLE_CHANGE:
  9208. case SC_MONSTER_TRANSFORM:
  9209. case SC_MTF_ASPD:
  9210. case SC_MTF_RANGEATK:
  9211. case SC_MTF_MATK:
  9212. case SC_MTF_MLEATKED:
  9213. case SC_MTF_CRIDAMAGE:
  9214. case SC_MTF_ASPD2:
  9215. case SC_MTF_RANGEATK2:
  9216. case SC_MTF_MATK2:
  9217. case SC_MTF_MHP:
  9218. case SC_MTF_MSP:
  9219. case SC_MTF_PUMPKIN:
  9220. case SC_MTF_HITFLEE:
  9221. return 0;
  9222. default:
  9223. break;
  9224. }
  9225. //This should help status_change_end force disabling the SC in case it has no limit.
  9226. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9227. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9228. } else
  9229. status_change_clear(bl, 3); // remove all effects
  9230. return SCRIPT_CMD_SUCCESS;
  9231. }
  9232. /*==========================================
  9233. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9234. *------------------------------------------*/
  9235. BUILDIN_FUNC(getscrate)
  9236. {
  9237. struct block_list *bl;
  9238. int type,rate;
  9239. type=script_getnum(st,2);
  9240. rate=script_getnum(st,3);
  9241. if( script_hasdata(st,4) ) //get for the bl assigned
  9242. bl = map_id2bl(script_getnum(st,4));
  9243. else
  9244. bl = map_id2bl(st->rid);
  9245. if (bl)
  9246. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9247. script_pushint(st,rate);
  9248. return SCRIPT_CMD_SUCCESS;
  9249. }
  9250. /*==========================================
  9251. * getstatus <type>{, <info>};
  9252. *------------------------------------------*/
  9253. BUILDIN_FUNC(getstatus)
  9254. {
  9255. int id, type;
  9256. struct map_session_data* sd = script_rid2sd(st);
  9257. if( sd == NULL )
  9258. {// no player attached
  9259. return 0;
  9260. }
  9261. id = script_getnum(st, 2);
  9262. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9263. if( id <= SC_NONE || id >= SC_MAX )
  9264. {// invalid status type given
  9265. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9266. return 0;
  9267. }
  9268. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9269. {// no status is active
  9270. script_pushint(st, 0);
  9271. return 0;
  9272. }
  9273. switch( type )
  9274. {
  9275. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9276. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9277. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9278. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9279. case 5:
  9280. {
  9281. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9282. if( timer )
  9283. {// return the amount of time remaining
  9284. script_pushint(st, timer->tick - gettick());
  9285. }
  9286. }
  9287. break;
  9288. default: script_pushint(st, 1); break;
  9289. }
  9290. return SCRIPT_CMD_SUCCESS;
  9291. }
  9292. /*==========================================
  9293. *
  9294. *------------------------------------------*/
  9295. BUILDIN_FUNC(debugmes)
  9296. {
  9297. const char *str;
  9298. str=script_getstr(st,2);
  9299. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9300. return SCRIPT_CMD_SUCCESS;
  9301. }
  9302. /*==========================================
  9303. *------------------------------------------*/
  9304. BUILDIN_FUNC(catchpet)
  9305. {
  9306. int pet_id;
  9307. TBL_PC *sd;
  9308. pet_id= script_getnum(st,2);
  9309. sd=script_rid2sd(st);
  9310. if( sd == NULL )
  9311. return 0;
  9312. pet_catch_process1(sd,pet_id);
  9313. return SCRIPT_CMD_SUCCESS;
  9314. }
  9315. /*==========================================
  9316. * [orn]
  9317. *------------------------------------------*/
  9318. BUILDIN_FUNC(homunculus_evolution)
  9319. {
  9320. TBL_PC *sd;
  9321. sd=script_rid2sd(st);
  9322. if( sd == NULL )
  9323. return 0;
  9324. if(hom_is_active(sd->hd))
  9325. {
  9326. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9327. hom_evolution(sd->hd);
  9328. else
  9329. clif_emotion(&sd->hd->bl, E_SWT);
  9330. }
  9331. return SCRIPT_CMD_SUCCESS;
  9332. }
  9333. /*==========================================
  9334. * Checks for vaporized morph state
  9335. * and deletes ITEMID_STRANGE_EMBRYO.
  9336. *------------------------------------------*/
  9337. BUILDIN_FUNC(homunculus_mutate)
  9338. {
  9339. int homun_id;
  9340. TBL_PC *sd;
  9341. sd = script_rid2sd(st);
  9342. if( sd == NULL || sd->hd == NULL )
  9343. return 0;
  9344. if(script_hasdata(st,2))
  9345. homun_id = script_getnum(st,2);
  9346. else
  9347. homun_id = 6048 + (rnd() % 4);
  9348. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9349. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9350. int m_id = hom_class2mapid(homun_id);
  9351. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9352. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9353. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9354. hom_call(sd); // Respawn homunculus.
  9355. hom_mutate(sd->hd, homun_id);
  9356. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9357. script_pushint(st, 1);
  9358. return 0;
  9359. } else
  9360. clif_emotion(&sd->bl, E_SWT);
  9361. } else
  9362. clif_emotion(&sd->bl, E_SWT);
  9363. script_pushint(st, 0);
  9364. return SCRIPT_CMD_SUCCESS;
  9365. }
  9366. /*==========================================
  9367. * Puts homunculus into morph state
  9368. * and gives ITEMID_STRANGE_EMBRYO.
  9369. *------------------------------------------*/
  9370. BUILDIN_FUNC(morphembryo)
  9371. {
  9372. struct item item_tmp;
  9373. TBL_PC *sd;
  9374. sd = script_rid2sd(st);
  9375. if( sd == NULL || sd->hd == NULL )
  9376. return 0;
  9377. if( hom_is_active(sd->hd) ) {
  9378. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9379. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9380. char i;
  9381. memset(&item_tmp, 0, sizeof(item_tmp));
  9382. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9383. item_tmp.identify = 1;
  9384. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9385. clif_additem(sd, 0, 0, i);
  9386. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9387. } else {
  9388. hom_vaporize(sd, HOM_ST_MORPH);
  9389. script_pushint(st, 1);
  9390. return 0;
  9391. }
  9392. } else
  9393. clif_emotion(&sd->hd->bl, E_SWT);
  9394. } else
  9395. clif_emotion(&sd->bl, E_SWT);
  9396. script_pushint(st, 0);
  9397. return SCRIPT_CMD_SUCCESS;
  9398. }
  9399. // [Zephyrus]
  9400. BUILDIN_FUNC(homunculus_shuffle)
  9401. {
  9402. TBL_PC *sd;
  9403. sd=script_rid2sd(st);
  9404. if( sd == NULL )
  9405. return 0;
  9406. if(hom_is_active(sd->hd))
  9407. hom_shuffle(sd->hd);
  9408. return SCRIPT_CMD_SUCCESS;
  9409. }
  9410. /*==========================================
  9411. * Check for homunculus state.
  9412. * Return: -1 = No homunculus
  9413. * 0 = Homunculus is active
  9414. * 1 = Homunculus is vaporized (rest)
  9415. * 2 = Homunculus is in morph state
  9416. *------------------------------------------*/
  9417. BUILDIN_FUNC(checkhomcall)
  9418. {
  9419. TBL_PC *sd = script_rid2sd(st);
  9420. TBL_HOM *hd;
  9421. if( sd == NULL )
  9422. return 0;
  9423. hd = sd->hd;
  9424. if( !hd )
  9425. script_pushint(st, -1);
  9426. else
  9427. script_pushint(st, hd->homunculus.vaporize);
  9428. return SCRIPT_CMD_SUCCESS;
  9429. }
  9430. //These two functions bring the eA MAPID_* class functionality to scripts.
  9431. BUILDIN_FUNC(eaclass)
  9432. {
  9433. int class_;
  9434. if( script_hasdata(st,2) )
  9435. class_ = script_getnum(st,2);
  9436. else {
  9437. TBL_PC *sd;
  9438. sd=script_rid2sd(st);
  9439. if (!sd) {
  9440. script_pushint(st,-1);
  9441. return 0;
  9442. }
  9443. class_ = sd->status.class_;
  9444. }
  9445. script_pushint(st,pc_jobid2mapid(class_));
  9446. return SCRIPT_CMD_SUCCESS;
  9447. }
  9448. BUILDIN_FUNC(roclass)
  9449. {
  9450. int class_ =script_getnum(st,2);
  9451. int sex;
  9452. if( script_hasdata(st,3) )
  9453. sex = script_getnum(st,3);
  9454. else {
  9455. TBL_PC *sd;
  9456. if (st->rid && (sd=script_rid2sd(st)))
  9457. sex = sd->status.sex;
  9458. else
  9459. sex = 1; //Just use male when not found.
  9460. }
  9461. script_pushint(st,pc_mapid2jobid(class_, sex));
  9462. return SCRIPT_CMD_SUCCESS;
  9463. }
  9464. /*==========================================
  9465. * Tells client to open a hatching window, used for pet incubator
  9466. *------------------------------------------*/
  9467. BUILDIN_FUNC(birthpet)
  9468. {
  9469. TBL_PC *sd;
  9470. sd=script_rid2sd(st);
  9471. if( sd == NULL )
  9472. return 0;
  9473. if( sd->status.pet_id )
  9474. {// do not send egg list, when you already have a pet
  9475. return 0;
  9476. }
  9477. clif_sendegg(sd);
  9478. return SCRIPT_CMD_SUCCESS;
  9479. }
  9480. /*==========================================
  9481. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  9482. * @type
  9483. * 1 : make like after rebirth
  9484. * 2 : blvl,jlvl=1, skillpoint=0
  9485. * 3 : don't reset skill, blvl=1
  9486. * 4 : jlvl=0
  9487. *------------------------------------------*/
  9488. BUILDIN_FUNC(resetlvl)
  9489. {
  9490. TBL_PC *sd;
  9491. int type=script_getnum(st,2);
  9492. sd=script_rid2sd(st);
  9493. if( sd == NULL )
  9494. return 0;
  9495. pc_resetlvl(sd,type);
  9496. return SCRIPT_CMD_SUCCESS;
  9497. }
  9498. /*==========================================
  9499. * Reset a player status point
  9500. *------------------------------------------*/
  9501. BUILDIN_FUNC(resetstatus)
  9502. {
  9503. TBL_PC *sd;
  9504. sd=script_rid2sd(st);
  9505. pc_resetstate(sd);
  9506. return SCRIPT_CMD_SUCCESS;
  9507. }
  9508. /*==========================================
  9509. * script command resetskill
  9510. *------------------------------------------*/
  9511. BUILDIN_FUNC(resetskill)
  9512. {
  9513. TBL_PC *sd;
  9514. sd=script_rid2sd(st);
  9515. pc_resetskill(sd,1);
  9516. return SCRIPT_CMD_SUCCESS;
  9517. }
  9518. /*==========================================
  9519. * Counts total amount of skill points.
  9520. *------------------------------------------*/
  9521. BUILDIN_FUNC(skillpointcount)
  9522. {
  9523. TBL_PC *sd;
  9524. sd=script_rid2sd(st);
  9525. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9526. return SCRIPT_CMD_SUCCESS;
  9527. }
  9528. /*==========================================
  9529. *
  9530. *------------------------------------------*/
  9531. BUILDIN_FUNC(changebase)
  9532. {
  9533. TBL_PC *sd=NULL;
  9534. int vclass;
  9535. if( script_hasdata(st,3) )
  9536. sd=map_id2sd(script_getnum(st,3));
  9537. else
  9538. sd=script_rid2sd(st);
  9539. if(sd == NULL)
  9540. return 0;
  9541. vclass = script_getnum(st,2);
  9542. if(vclass == JOB_WEDDING)
  9543. {
  9544. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9545. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9546. )
  9547. return 0;
  9548. }
  9549. if(!sd->disguise && vclass != sd->vd.class_) {
  9550. status_set_viewdata(&sd->bl, vclass);
  9551. //Updated client view. Base, Weapon and Cloth Colors.
  9552. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9553. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9554. if (sd->vd.cloth_color)
  9555. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9556. clif_skillinfoblock(sd);
  9557. }
  9558. return SCRIPT_CMD_SUCCESS;
  9559. }
  9560. /*==========================================
  9561. * Unequip all item and request for a changesex to char-serv
  9562. *------------------------------------------*/
  9563. BUILDIN_FUNC(changesex)
  9564. {
  9565. int i;
  9566. TBL_PC *sd = NULL;
  9567. sd = script_rid2sd(st);
  9568. pc_resetskill(sd,4);
  9569. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9570. for( i=0; i<EQI_MAX; i++ )
  9571. if( sd->equip_index[i] >= 0 ) pc_unequipitem(sd, sd->equip_index[i], 3);
  9572. chrif_changesex(sd);
  9573. return SCRIPT_CMD_SUCCESS;
  9574. }
  9575. /*==========================================
  9576. * Works like 'announce' but outputs in the common chat window
  9577. *------------------------------------------*/
  9578. BUILDIN_FUNC(globalmes)
  9579. {
  9580. struct block_list *bl = map_id2bl(st->oid);
  9581. struct npc_data *nd = (struct npc_data *)bl;
  9582. const char *name=NULL,*mes;
  9583. mes=script_getstr(st,2);
  9584. if(mes==NULL) return 0;
  9585. if(script_hasdata(st,3)){ // npc name to display
  9586. name=script_getstr(st,3);
  9587. } else {
  9588. name=nd->name; //use current npc name
  9589. }
  9590. npc_globalmessage(name,mes); // broadcast to all players connected
  9591. return SCRIPT_CMD_SUCCESS;
  9592. }
  9593. /////////////////////////////////////////////////////////////////////
  9594. // NPC waiting room (chat room)
  9595. //
  9596. /// Creates a waiting room (chat room) for this npc.
  9597. ///
  9598. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9599. BUILDIN_FUNC(waitingroom)
  9600. {
  9601. struct npc_data* nd;
  9602. const char* title = script_getstr(st, 2);
  9603. int limit = script_getnum(st, 3);
  9604. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9605. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9606. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9607. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9608. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9609. nd = (struct npc_data *)map_id2bl(st->oid);
  9610. if( nd != NULL )
  9611. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  9612. return SCRIPT_CMD_SUCCESS;
  9613. }
  9614. /// Removes the waiting room of the current or target npc.
  9615. ///
  9616. /// delwaitingroom "<npc_name>";
  9617. /// delwaitingroom;
  9618. BUILDIN_FUNC(delwaitingroom)
  9619. {
  9620. struct npc_data* nd;
  9621. if( script_hasdata(st,2) )
  9622. nd = npc_name2id(script_getstr(st, 2));
  9623. else
  9624. nd = (struct npc_data *)map_id2bl(st->oid);
  9625. if( nd != NULL )
  9626. chat_deletenpcchat(nd);
  9627. return SCRIPT_CMD_SUCCESS;
  9628. }
  9629. /// Kicks all the players from the waiting room of the current or target npc.
  9630. ///
  9631. /// kickwaitingroomall "<npc_name>";
  9632. /// kickwaitingroomall;
  9633. BUILDIN_FUNC(waitingroomkickall)
  9634. {
  9635. struct npc_data* nd;
  9636. struct chat_data* cd;
  9637. if( script_hasdata(st,2) )
  9638. nd = npc_name2id(script_getstr(st,2));
  9639. else
  9640. nd = (struct npc_data *)map_id2bl(st->oid);
  9641. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9642. chat_npckickall(cd);
  9643. return SCRIPT_CMD_SUCCESS;
  9644. }
  9645. /// Enables the waiting room event of the current or target npc.
  9646. ///
  9647. /// enablewaitingroomevent "<npc_name>";
  9648. /// enablewaitingroomevent;
  9649. BUILDIN_FUNC(enablewaitingroomevent)
  9650. {
  9651. struct npc_data* nd;
  9652. struct chat_data* cd;
  9653. if( script_hasdata(st,2) )
  9654. nd = npc_name2id(script_getstr(st, 2));
  9655. else
  9656. nd = (struct npc_data *)map_id2bl(st->oid);
  9657. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9658. chat_enableevent(cd);
  9659. return SCRIPT_CMD_SUCCESS;
  9660. }
  9661. /// Disables the waiting room event of the current or target npc.
  9662. ///
  9663. /// disablewaitingroomevent "<npc_name>";
  9664. /// disablewaitingroomevent;
  9665. BUILDIN_FUNC(disablewaitingroomevent)
  9666. {
  9667. struct npc_data *nd;
  9668. struct chat_data *cd;
  9669. if( script_hasdata(st,2) )
  9670. nd = npc_name2id(script_getstr(st, 2));
  9671. else
  9672. nd = (struct npc_data *)map_id2bl(st->oid);
  9673. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9674. chat_disableevent(cd);
  9675. return SCRIPT_CMD_SUCCESS;
  9676. }
  9677. /// Returns info on the waiting room of the current or target npc.
  9678. /// Returns -1 if the type unknown
  9679. /// <type>=0 : current number of users
  9680. /// <type>=1 : maximum number of users allowed
  9681. /// <type>=2 : the number of users that trigger the event
  9682. /// <type>=3 : if the trigger is disabled
  9683. /// <type>=4 : the title of the waiting room
  9684. /// <type>=5 : the password of the waiting room
  9685. /// <type>=16 : the name of the waiting room event
  9686. /// <type>=32 : if the waiting room is full
  9687. /// <type>=33 : if there are enough users to trigger the event
  9688. ///
  9689. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9690. /// getwaitingroomstate(<type>) -> <info>
  9691. BUILDIN_FUNC(getwaitingroomstate)
  9692. {
  9693. struct npc_data *nd;
  9694. struct chat_data *cd;
  9695. int type;
  9696. type = script_getnum(st,2);
  9697. if( script_hasdata(st,3) )
  9698. nd = npc_name2id(script_getstr(st, 3));
  9699. else
  9700. nd = (struct npc_data *)map_id2bl(st->oid);
  9701. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9702. {
  9703. script_pushint(st, -1);
  9704. return 0;
  9705. }
  9706. switch(type)
  9707. {
  9708. case 0: script_pushint(st, cd->users); break;
  9709. case 1: script_pushint(st, cd->limit); break;
  9710. case 2: script_pushint(st, cd->trigger&0x7f); break;
  9711. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  9712. case 4: script_pushstrcopy(st, cd->title); break;
  9713. case 5: script_pushstrcopy(st, cd->pass); break;
  9714. case 16: script_pushstrcopy(st, cd->npc_event);break;
  9715. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  9716. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  9717. default: script_pushint(st, -1); break;
  9718. }
  9719. return SCRIPT_CMD_SUCCESS;
  9720. }
  9721. /// Warps the trigger or target amount of players to the target map and position.
  9722. /// Players are automatically removed from the waiting room.
  9723. /// Those waiting the longest will get warped first.
  9724. /// The target map can be "Random" for a random position in the current map,
  9725. /// and "SavePoint" for the savepoint map+position.
  9726. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  9727. ///
  9728. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  9729. /// The total number of teleported players is put into $@warpwaitingpcnum
  9730. ///
  9731. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  9732. /// warpwaitingpc "<map name>",<x>,<y>;
  9733. BUILDIN_FUNC(warpwaitingpc)
  9734. {
  9735. int x;
  9736. int y;
  9737. int i;
  9738. int n;
  9739. const char* map_name;
  9740. struct npc_data* nd;
  9741. struct chat_data* cd;
  9742. nd = (struct npc_data *)map_id2bl(st->oid);
  9743. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  9744. return 0;
  9745. map_name = script_getstr(st,2);
  9746. x = script_getnum(st,3);
  9747. y = script_getnum(st,4);
  9748. n = cd->trigger&0x7f;
  9749. if( script_hasdata(st,5) )
  9750. n = script_getnum(st,5);
  9751. for( i = 0; i < n && cd->users > 0; i++ )
  9752. {
  9753. TBL_PC* sd = cd->usersd[0];
  9754. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  9755. {// can't teleport on this map
  9756. break;
  9757. }
  9758. if( cd->zeny )
  9759. {// fee set
  9760. if( (uint32)sd->status.zeny < cd->zeny )
  9761. {// no zeny to cover set fee
  9762. break;
  9763. }
  9764. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  9765. }
  9766. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  9767. if( strcmp(map_name,"Random") == 0 )
  9768. pc_randomwarp(sd,CLR_TELEPORT);
  9769. else if( strcmp(map_name,"SavePoint") == 0 )
  9770. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  9771. else
  9772. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  9773. }
  9774. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  9775. return SCRIPT_CMD_SUCCESS;
  9776. }
  9777. /////////////////////////////////////////////////////////////////////
  9778. // ...
  9779. //
  9780. /// Detaches a character from a script.
  9781. ///
  9782. /// @param st Script state to detach the character from.
  9783. static void script_detach_rid(struct script_state* st)
  9784. {
  9785. if(st->rid)
  9786. {
  9787. script_detach_state(st, false);
  9788. st->rid = 0;
  9789. }
  9790. }
  9791. /*=========================================================================
  9792. * Attaches a set of RIDs to the current script. [digitalhamster]
  9793. * addrid(<type>{,<flag>{,<parameters>}});
  9794. * <type>:
  9795. * 0 : All players in the server.
  9796. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  9797. * 2 : Party members of a specified party ID.
  9798. * [ Parameters: <party id> ]
  9799. * 3 : Guild members of a specified guild ID.
  9800. * [ Parameters: <guild id> ]
  9801. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  9802. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  9803. * Account ID: The specified account ID.
  9804. * <flag>:
  9805. * 0 : Players are always attached. (default)
  9806. * 1 : Players currently running another script will not be attached.
  9807. *-------------------------------------------------------------------------*/
  9808. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  9809. {
  9810. int forceflag;
  9811. struct map_session_data *sd = (TBL_PC *)bl;
  9812. struct script_state* st;
  9813. st = va_arg(ap,struct script_state*);
  9814. forceflag = va_arg(ap,int);
  9815. if(!forceflag || !sd->st)
  9816. if(sd->status.account_id != st->rid)
  9817. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9818. return 0;
  9819. }
  9820. BUILDIN_FUNC(addrid)
  9821. {
  9822. struct s_mapiterator* iter;
  9823. struct block_list *bl;
  9824. TBL_PC *sd;
  9825. if(st->rid < 1) {
  9826. st->state = END;
  9827. bl = map_id2bl(st->oid);
  9828. } else
  9829. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  9830. iter = mapit_getallusers();
  9831. switch(script_getnum(st,2)) {
  9832. case 0:
  9833. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9834. if(!script_getnum(st,3) || !sd->st)
  9835. if(sd->status.account_id != st->rid) //attached player already runs.
  9836. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9837. }
  9838. break;
  9839. case 1:
  9840. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9841. if(!script_getnum(st,3) || !sd->st)
  9842. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  9843. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9844. }
  9845. break;
  9846. case 2:
  9847. if(script_getnum(st,4) == 0) {
  9848. script_pushint(st,0);
  9849. return 0;
  9850. }
  9851. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9852. if(!script_getnum(st,3) || !sd->st)
  9853. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  9854. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9855. }
  9856. break;
  9857. case 3:
  9858. if(script_getnum(st,4) == 0) {
  9859. script_pushint(st,0);
  9860. return 0;
  9861. }
  9862. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  9863. if(!script_getnum(st,3) || !sd->st)
  9864. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  9865. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  9866. }
  9867. break;
  9868. case 4:
  9869. map_foreachinarea(buildin_addrid_sub,
  9870. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  9871. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  9872. break;
  9873. default:
  9874. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  9875. script_pushint(st,0);
  9876. return 0;
  9877. }
  9878. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  9879. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  9880. script_pushint(st,1);
  9881. }
  9882. return 0;
  9883. }
  9884. mapit_free(iter);
  9885. script_pushint(st,1);
  9886. return SCRIPT_CMD_SUCCESS;
  9887. }
  9888. /*==========================================
  9889. * Attach sd char id to script and detach current one if any
  9890. *------------------------------------------*/
  9891. BUILDIN_FUNC(attachrid)
  9892. {
  9893. int rid = script_getnum(st,2);
  9894. if (map_id2sd(rid) != NULL) {
  9895. script_detach_rid(st);
  9896. st->rid = rid;
  9897. script_attach_state(st);
  9898. script_pushint(st,1);
  9899. } else
  9900. script_pushint(st,0);
  9901. return SCRIPT_CMD_SUCCESS;
  9902. }
  9903. /*==========================================
  9904. * Detach script to rid
  9905. *------------------------------------------*/
  9906. BUILDIN_FUNC(detachrid)
  9907. {
  9908. script_detach_rid(st);
  9909. return SCRIPT_CMD_SUCCESS;
  9910. }
  9911. /*==========================================
  9912. * Chk if account connected, (and charid from account if specified)
  9913. *------------------------------------------*/
  9914. BUILDIN_FUNC(isloggedin)
  9915. {
  9916. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  9917. if (script_hasdata(st,3) && sd &&
  9918. sd->status.char_id != script_getnum(st,3))
  9919. sd = NULL;
  9920. push_val(st->stack,C_INT,sd!=NULL);
  9921. return SCRIPT_CMD_SUCCESS;
  9922. }
  9923. /*==========================================
  9924. *
  9925. *------------------------------------------*/
  9926. BUILDIN_FUNC(setmapflagnosave)
  9927. {
  9928. int16 m,x,y;
  9929. unsigned short mapindex;
  9930. const char *str,*str2;
  9931. str=script_getstr(st,2);
  9932. str2=script_getstr(st,3);
  9933. x=script_getnum(st,4);
  9934. y=script_getnum(st,5);
  9935. m = map_mapname2mapid(str);
  9936. mapindex = mapindex_name2id(str2);
  9937. if(m >= 0 && mapindex) {
  9938. map[m].flag.nosave=1;
  9939. map[m].save.map=mapindex;
  9940. map[m].save.x=x;
  9941. map[m].save.y=y;
  9942. }
  9943. return SCRIPT_CMD_SUCCESS;
  9944. }
  9945. BUILDIN_FUNC(getmapflag)
  9946. {
  9947. int16 m,i;
  9948. const char *str;
  9949. str=script_getstr(st,2);
  9950. i=script_getnum(st,3);
  9951. m = map_mapname2mapid(str);
  9952. if(m >= 0) {
  9953. switch(i) {
  9954. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  9955. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  9956. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  9957. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  9958. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  9959. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  9960. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  9961. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  9962. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  9963. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  9964. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  9965. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  9966. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  9967. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  9968. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  9969. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  9970. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  9971. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  9972. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  9973. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  9974. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  9975. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  9976. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  9977. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  9978. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  9979. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  9980. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  9981. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  9982. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  9983. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  9984. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  9985. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  9986. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  9987. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  9988. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  9989. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  9990. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  9991. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  9992. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  9993. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  9994. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  9995. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  9996. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  9997. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  9998. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  9999. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10000. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10001. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10002. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10003. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10004. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10005. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10006. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10007. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10008. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10009. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10010. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10011. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10012. #ifdef ADJUST_SKILL_DAMAGE
  10013. case MF_SKILL_DAMAGE:
  10014. {
  10015. int ret_val=0, type=0;
  10016. FETCH(4,type);
  10017. switch (type) {
  10018. case 1: ret_val = map[m].adjust.damage.pc; break;
  10019. case 2: ret_val = map[m].adjust.damage.mob; break;
  10020. case 3: ret_val = map[m].adjust.damage.boss; break;
  10021. case 4: ret_val = map[m].adjust.damage.other; break;
  10022. case 5: ret_val = map[m].adjust.damage.caster; break;
  10023. default: ret_val = map[m].flag.skill_damage; break;
  10024. }
  10025. script_pushint(st,ret_val); break;
  10026. } break;
  10027. #endif
  10028. }
  10029. }
  10030. return SCRIPT_CMD_SUCCESS;
  10031. }
  10032. /* pvp timer handling */
  10033. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10034. TBL_PC* sd = (TBL_PC*)bl;
  10035. if (sd->pvp_timer == INVALID_TIMER) {
  10036. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10037. sd->pvp_rank = 0;
  10038. sd->pvp_lastusers = 0;
  10039. sd->pvp_point = 5;
  10040. sd->pvp_won = 0;
  10041. sd->pvp_lost = 0;
  10042. }
  10043. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  10044. clif_maptypeproperty2(&sd->bl,SELF);
  10045. return 0;
  10046. }
  10047. BUILDIN_FUNC(setmapflag)
  10048. {
  10049. int16 m,i;
  10050. const char *str;
  10051. int val=0;
  10052. str=script_getstr(st,2);
  10053. i=script_getnum(st,3);
  10054. FETCH(4,val);
  10055. m = map_mapname2mapid(str);
  10056. if(m >= 0) {
  10057. switch(i) {
  10058. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10059. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10060. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10061. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10062. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10063. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10064. case MF_PVP:
  10065. map[m].flag.pvp = 1;
  10066. if( !battle_config.pk_mode ) {
  10067. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10068. }
  10069. break;
  10070. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10071. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10072. case MF_GVG: {
  10073. struct block_list bl;
  10074. map[m].flag.gvg = 1;
  10075. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10076. bl.type = BL_NUL;
  10077. bl.m = m;
  10078. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10079. }
  10080. break;
  10081. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10082. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10083. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10084. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10085. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10086. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10087. case MF_SNOW: map[m].flag.snow = 1; break;
  10088. case MF_FOG: map[m].flag.fog = 1; break;
  10089. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10090. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10091. case MF_NOGO: map[m].flag.nogo = 1; break;
  10092. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10093. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10094. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10095. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10096. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10097. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10098. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10099. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10100. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10101. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10102. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10103. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10104. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10105. case MF_RESTRICTED:
  10106. map[m].zone |= 1<<(val+1);
  10107. map[m].flag.restricted=1;
  10108. break;
  10109. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10110. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10111. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10112. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10113. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10114. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10115. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10116. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10117. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10118. case MF_TOWN: map[m].flag.town = 1; break;
  10119. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10120. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10121. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10122. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10123. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10124. case MF_RESET: map[m].flag.reset = 1; break;
  10125. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10126. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10127. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10128. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10129. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10130. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10131. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10132. #ifdef ADJUST_SKILL_DAMAGE
  10133. case MF_SKILL_DAMAGE:
  10134. {
  10135. int type=0;
  10136. FETCH(5,type);
  10137. switch (type) {
  10138. case 1: map[m].adjust.damage.pc = val; break;
  10139. case 2: map[m].adjust.damage.mob = val; break;
  10140. case 3: map[m].adjust.damage.boss = val; break;
  10141. case 4: map[m].adjust.damage.other = val; break;
  10142. case 5: map[m].adjust.damage.caster = val; break;
  10143. }
  10144. map[m].flag.skill_damage = 1;
  10145. } break;
  10146. #endif
  10147. }
  10148. }
  10149. return SCRIPT_CMD_SUCCESS;
  10150. }
  10151. BUILDIN_FUNC(removemapflag)
  10152. {
  10153. int16 m,i;
  10154. const char *str;
  10155. int val=0;
  10156. str=script_getstr(st,2);
  10157. i=script_getnum(st,3);
  10158. FETCH(4,val);
  10159. m = map_mapname2mapid(str);
  10160. if(m >= 0) {
  10161. switch(i) {
  10162. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10163. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10164. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10165. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10166. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10167. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10168. case MF_PVP: {
  10169. struct block_list bl;
  10170. bl.type = BL_NUL;
  10171. bl.m = m;
  10172. map[m].flag.pvp = 0;
  10173. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10174. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10175. }
  10176. break;
  10177. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10178. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10179. case MF_GVG: {
  10180. struct block_list bl;
  10181. bl.type = BL_NUL;
  10182. bl.m = m;
  10183. map[m].flag.gvg = 0;
  10184. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10185. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10186. }
  10187. break;
  10188. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10189. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10190. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10191. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10192. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10193. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10194. case MF_SNOW: map[m].flag.snow = 0; break;
  10195. case MF_FOG: map[m].flag.fog = 0; break;
  10196. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10197. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10198. case MF_NOGO: map[m].flag.nogo = 0; break;
  10199. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10200. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10201. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10202. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10203. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10204. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10205. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10206. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10207. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10208. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10209. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10210. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10211. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10212. case MF_RESTRICTED:
  10213. map[m].zone ^= 1<<(val+1);
  10214. if (map[m].zone == 0){
  10215. map[m].flag.restricted=0;
  10216. }
  10217. break;
  10218. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10219. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10220. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10221. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10222. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10223. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10224. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10225. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10226. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10227. case MF_TOWN: map[m].flag.town = 0; break;
  10228. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10229. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10230. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10231. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10232. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10233. case MF_RESET: map[m].flag.reset = 0; break;
  10234. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10235. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10236. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10237. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10238. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10239. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10240. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10241. #ifdef ADJUST_SKILL_DAMAGE
  10242. case MF_SKILL_DAMAGE:
  10243. {
  10244. map[m].flag.skill_damage = 0;
  10245. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10246. if (map[m].skill_damage.count)
  10247. map_skill_damage_free(&map[m]);
  10248. } break;
  10249. #endif
  10250. }
  10251. }
  10252. return SCRIPT_CMD_SUCCESS;
  10253. }
  10254. BUILDIN_FUNC(pvpon)
  10255. {
  10256. int16 m;
  10257. const char *str;
  10258. TBL_PC* sd = NULL;
  10259. struct s_mapiterator* iter;
  10260. struct block_list bl;
  10261. str = script_getstr(st,2);
  10262. m = map_mapname2mapid(str);
  10263. if( m < 0 || map[m].flag.pvp )
  10264. return 0; // nothing to do
  10265. map[m].flag.pvp = 1;
  10266. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10267. bl.type = BL_NUL;
  10268. bl.m = m;
  10269. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10270. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10271. return 0;
  10272. iter = mapit_getallusers();
  10273. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10274. {
  10275. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10276. continue; // not applicable
  10277. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10278. sd->pvp_rank = 0;
  10279. sd->pvp_lastusers = 0;
  10280. sd->pvp_point = 5;
  10281. sd->pvp_won = 0;
  10282. sd->pvp_lost = 0;
  10283. }
  10284. mapit_free(iter);
  10285. return SCRIPT_CMD_SUCCESS;
  10286. }
  10287. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10288. {
  10289. TBL_PC* sd = (TBL_PC*)bl;
  10290. clif_pvpset(sd, 0, 0, 2);
  10291. if (sd->pvp_timer != INVALID_TIMER) {
  10292. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10293. sd->pvp_timer = INVALID_TIMER;
  10294. }
  10295. return 0;
  10296. }
  10297. BUILDIN_FUNC(pvpoff)
  10298. {
  10299. int16 m;
  10300. const char *str;
  10301. struct block_list bl;
  10302. str=script_getstr(st,2);
  10303. m = map_mapname2mapid(str);
  10304. if(m < 0 || !map[m].flag.pvp)
  10305. return 0; //fixed Lupus
  10306. map[m].flag.pvp = 0;
  10307. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10308. bl.type = BL_NUL;
  10309. bl.m = m;
  10310. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10311. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10312. return 0;
  10313. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10314. return SCRIPT_CMD_SUCCESS;
  10315. }
  10316. BUILDIN_FUNC(gvgon)
  10317. {
  10318. int16 m;
  10319. const char *str;
  10320. struct block_list bl;
  10321. str=script_getstr(st,2);
  10322. m = map_mapname2mapid(str);
  10323. if(m >= 0 && !map[m].flag.gvg) {
  10324. map[m].flag.gvg = 1;
  10325. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10326. bl.type = BL_NUL;
  10327. bl.m = m;
  10328. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10329. }
  10330. return SCRIPT_CMD_SUCCESS;
  10331. }
  10332. BUILDIN_FUNC(gvgoff)
  10333. {
  10334. int16 m;
  10335. const char *str;
  10336. str=script_getstr(st,2);
  10337. m = map_mapname2mapid(str);
  10338. if(m >= 0 && map[m].flag.gvg) {
  10339. struct block_list bl;
  10340. map[m].flag.gvg = 0;
  10341. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10342. bl.type = BL_NUL;
  10343. bl.m = m;
  10344. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10345. }
  10346. return SCRIPT_CMD_SUCCESS;
  10347. }
  10348. /*==========================================
  10349. * Shows an emoticon on top of the player/npc
  10350. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10351. *------------------------------------------*/
  10352. //Optional second parameter added by [Skotlex]
  10353. BUILDIN_FUNC(emotion)
  10354. {
  10355. int type;
  10356. int player=0;
  10357. type=script_getnum(st,2);
  10358. if(type < 0 || type > 100)
  10359. return 0;
  10360. if( script_hasdata(st,3) )
  10361. player=script_getnum(st,3);
  10362. if (player) {
  10363. TBL_PC *sd = NULL;
  10364. if( script_hasdata(st,4) )
  10365. sd = map_nick2sd(script_getstr(st,4));
  10366. else
  10367. sd = script_rid2sd(st);
  10368. if (sd)
  10369. clif_emotion(&sd->bl,type);
  10370. } else
  10371. if( script_hasdata(st,4) )
  10372. {
  10373. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10374. if(nd)
  10375. clif_emotion(&nd->bl,type);
  10376. }
  10377. else
  10378. clif_emotion(map_id2bl(st->oid),type);
  10379. return SCRIPT_CMD_SUCCESS;
  10380. }
  10381. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10382. {
  10383. int16 m=va_arg(ap,int);
  10384. int g_id=va_arg(ap,int);
  10385. int flag=va_arg(ap,int);
  10386. if(!sd || sd->bl.m != m)
  10387. return 0;
  10388. if(
  10389. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10390. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10391. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10392. )
  10393. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10394. return 1;
  10395. }
  10396. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10397. {
  10398. struct mob_data *md=(struct mob_data *)bl;
  10399. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10400. status_kill(bl);
  10401. return 0;
  10402. }
  10403. /*
  10404. * Function to kick guild members out of a map and to their save points.
  10405. * m : mapid
  10406. * g_id : owner guild id
  10407. * flag & 1 : Warp out owners
  10408. * flag & 2 : Warp out outsiders
  10409. * flag & 4 : reserved for mobs
  10410. * */
  10411. BUILDIN_FUNC(maprespawnguildid)
  10412. {
  10413. const char *mapname=script_getstr(st,2);
  10414. int g_id=script_getnum(st,3);
  10415. int flag=script_getnum(st,4);
  10416. int16 m=map_mapname2mapid(mapname);
  10417. if(m == -1)
  10418. return 0;
  10419. //Catch ALL players (in case some are 'between maps' on execution time)
  10420. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10421. if (flag&4) //Remove script mobs.
  10422. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10423. return SCRIPT_CMD_SUCCESS;
  10424. }
  10425. BUILDIN_FUNC(agitstart)
  10426. {
  10427. if(agit_flag==1) return SCRIPT_CMD_SUCCESS; // Agit already Start.
  10428. agit_flag=1;
  10429. guild_agit_start();
  10430. return SCRIPT_CMD_SUCCESS;
  10431. }
  10432. BUILDIN_FUNC(agitend)
  10433. {
  10434. if(agit_flag==0) return SCRIPT_CMD_SUCCESS; // Agit already End.
  10435. agit_flag=0;
  10436. guild_agit_end();
  10437. return SCRIPT_CMD_SUCCESS;
  10438. }
  10439. BUILDIN_FUNC(agitstart2)
  10440. {
  10441. if(agit2_flag==1) return SCRIPT_CMD_SUCCESS; // Agit2 already Start.
  10442. agit2_flag=1;
  10443. guild_agit2_start();
  10444. return SCRIPT_CMD_SUCCESS;
  10445. }
  10446. BUILDIN_FUNC(agitend2)
  10447. {
  10448. if(agit2_flag==0) return SCRIPT_CMD_SUCCESS; // Agit2 already End.
  10449. agit2_flag=0;
  10450. guild_agit2_end();
  10451. return SCRIPT_CMD_SUCCESS;
  10452. }
  10453. /*==========================================
  10454. * Returns whether woe is on or off. // choice script
  10455. *------------------------------------------*/
  10456. BUILDIN_FUNC(agitcheck)
  10457. {
  10458. script_pushint(st,agit_flag);
  10459. return SCRIPT_CMD_SUCCESS;
  10460. }
  10461. /*==========================================
  10462. * Returns whether woese is on or off. // choice script
  10463. *------------------------------------------*/
  10464. BUILDIN_FUNC(agitcheck2)
  10465. {
  10466. script_pushint(st,agit2_flag);
  10467. return SCRIPT_CMD_SUCCESS;
  10468. }
  10469. /// Sets the guild_id of this npc.
  10470. ///
  10471. /// flagemblem <guild_id>;
  10472. BUILDIN_FUNC(flagemblem)
  10473. {
  10474. TBL_NPC* nd;
  10475. int g_id = script_getnum(st,2);
  10476. if(g_id < 0) return 0;
  10477. nd = (TBL_NPC*)map_id2nd(st->oid);
  10478. if( nd == NULL ) {
  10479. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10480. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10481. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10482. } else {
  10483. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10484. nd->u.scr.guild_id = g_id;
  10485. clif_guild_emblem_area(&nd->bl);
  10486. /* guild flag caching */
  10487. if( g_id ) /* adding a id */
  10488. guild_flag_add(nd);
  10489. else if( changed ) /* removing a flag */
  10490. guild_flag_remove(nd);
  10491. }
  10492. return SCRIPT_CMD_SUCCESS;
  10493. }
  10494. BUILDIN_FUNC(getcastlename)
  10495. {
  10496. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10497. struct guild_castle* gc = guild_mapname2gc(mapname);
  10498. const char* name = (gc) ? gc->castle_name : "";
  10499. script_pushstrcopy(st,name);
  10500. return SCRIPT_CMD_SUCCESS;
  10501. }
  10502. BUILDIN_FUNC(getcastledata)
  10503. {
  10504. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10505. int index = script_getnum(st,3);
  10506. struct guild_castle *gc = guild_mapname2gc(mapname);
  10507. if (gc == NULL) {
  10508. script_pushint(st,0);
  10509. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10510. return 1;
  10511. }
  10512. switch (index) {
  10513. case 1:
  10514. script_pushint(st,gc->guild_id); break;
  10515. case 2:
  10516. script_pushint(st,gc->economy); break;
  10517. case 3:
  10518. script_pushint(st,gc->defense); break;
  10519. case 4:
  10520. script_pushint(st,gc->triggerE); break;
  10521. case 5:
  10522. script_pushint(st,gc->triggerD); break;
  10523. case 6:
  10524. script_pushint(st,gc->nextTime); break;
  10525. case 7:
  10526. script_pushint(st,gc->payTime); break;
  10527. case 8:
  10528. script_pushint(st,gc->createTime); break;
  10529. case 9:
  10530. script_pushint(st,gc->visibleC); break;
  10531. default:
  10532. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10533. script_pushint(st,gc->guardian[index-10].visible);
  10534. break;
  10535. }
  10536. script_pushint(st,0);
  10537. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10538. return 1;
  10539. }
  10540. return SCRIPT_CMD_SUCCESS;
  10541. }
  10542. BUILDIN_FUNC(setcastledata)
  10543. {
  10544. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10545. int index = script_getnum(st,3);
  10546. int value = script_getnum(st,4);
  10547. struct guild_castle *gc = guild_mapname2gc(mapname);
  10548. if (gc == NULL) {
  10549. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10550. return 1;
  10551. }
  10552. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10553. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10554. return 1;
  10555. }
  10556. guild_castledatasave(gc->castle_id, index, value);
  10557. return SCRIPT_CMD_SUCCESS;
  10558. }
  10559. /* =====================================================================
  10560. * ---------------------------------------------------------------------*/
  10561. BUILDIN_FUNC(requestguildinfo)
  10562. {
  10563. int guild_id=script_getnum(st,2);
  10564. const char *event=NULL;
  10565. if( script_hasdata(st,3) ){
  10566. event=script_getstr(st,3);
  10567. check_event(st, event);
  10568. }
  10569. if(guild_id>0)
  10570. guild_npc_request_info(guild_id,event);
  10571. return SCRIPT_CMD_SUCCESS;
  10572. }
  10573. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10574. /// getequipcardcnt(<equipment slot>);
  10575. BUILDIN_FUNC(getequipcardcnt)
  10576. {
  10577. int i=-1,j,num;
  10578. TBL_PC *sd;
  10579. int count;
  10580. num=script_getnum(st,2);
  10581. sd=script_rid2sd(st);
  10582. if (num > 0 && num <= ARRAYLENGTH(equip))
  10583. i=pc_checkequip(sd,equip[num-1]);
  10584. if (i < 0 || !sd->inventory_data[i]) {
  10585. script_pushint(st,0);
  10586. return 0;
  10587. }
  10588. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10589. {
  10590. script_pushint(st,0);
  10591. return 0;
  10592. }
  10593. count = 0;
  10594. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  10595. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  10596. count++;
  10597. script_pushint(st,count);
  10598. return SCRIPT_CMD_SUCCESS;
  10599. }
  10600. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  10601. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  10602. /// successremovecards <slot>;
  10603. BUILDIN_FUNC(successremovecards) {
  10604. int i=-1,c,cardflag=0;
  10605. TBL_PC* sd = script_rid2sd(st);
  10606. int num = script_getnum(st,2);
  10607. if (num > 0 && num <= ARRAYLENGTH(equip))
  10608. i=pc_checkequip(sd,equip[num-1]);
  10609. if (i < 0 || !sd->inventory_data[i]) {
  10610. return 0;
  10611. }
  10612. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10613. return 0;
  10614. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10615. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  10616. unsigned char flag = 0;
  10617. struct item item_tmp;
  10618. memset(&item_tmp,0,sizeof(item_tmp));
  10619. cardflag = 1;
  10620. item_tmp.nameid = sd->status.inventory[i].card[c];
  10621. item_tmp.identify = 1;
  10622. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  10623. clif_additem(sd,0,0,flag);
  10624. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10625. }
  10626. }
  10627. }
  10628. if(cardflag == 1) {//if card was remove remplace item with no card
  10629. unsigned char flag = 0, j;
  10630. struct item item_tmp;
  10631. memset(&item_tmp,0,sizeof(item_tmp));
  10632. item_tmp.nameid = sd->status.inventory[i].nameid;
  10633. item_tmp.identify = 1;
  10634. item_tmp.refine = sd->status.inventory[i].refine;
  10635. item_tmp.attribute = sd->status.inventory[i].attribute;
  10636. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10637. item_tmp.bound = sd->status.inventory[i].bound;
  10638. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10639. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10640. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  10641. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  10642. clif_additem(sd,0,0,flag);
  10643. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10644. }
  10645. clif_misceffect(&sd->bl,3);
  10646. }
  10647. return SCRIPT_CMD_SUCCESS;
  10648. }
  10649. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  10650. /// failedremovecards <slot>, <type>;
  10651. /// <type>=0 : will destroy both the item and the cards.
  10652. /// <type>=1 : will keep the item, but destroy the cards.
  10653. /// <type>=2 : will keep the cards, but destroy the item.
  10654. /// <type>=? : will just display the failure effect.
  10655. BUILDIN_FUNC(failedremovecards) {
  10656. int i=-1,c,cardflag=0;
  10657. TBL_PC* sd = script_rid2sd(st);
  10658. int num = script_getnum(st,2);
  10659. int typefail = script_getnum(st,3);
  10660. if (num > 0 && num <= ARRAYLENGTH(equip))
  10661. i=pc_checkequip(sd,equip[num-1]);
  10662. if (i < 0 || !sd->inventory_data[i])
  10663. return 0;
  10664. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  10665. return 0;
  10666. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  10667. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  10668. cardflag = 1;
  10669. if(typefail == 2) {// add cards to inventory, clear
  10670. unsigned char flag = 0;
  10671. struct item item_tmp;
  10672. memset(&item_tmp,0,sizeof(item_tmp));
  10673. item_tmp.nameid = sd->status.inventory[i].card[c];
  10674. item_tmp.identify = 1;
  10675. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10676. clif_additem(sd,0,0,flag);
  10677. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10678. }
  10679. }
  10680. }
  10681. }
  10682. if(cardflag == 1) {
  10683. if(typefail == 0 || typefail == 2){ // destroy the item
  10684. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10685. }
  10686. if(typefail == 1){ // destroy the card
  10687. unsigned char flag = 0, j;
  10688. struct item item_tmp;
  10689. memset(&item_tmp,0,sizeof(item_tmp));
  10690. item_tmp.nameid = sd->status.inventory[i].nameid;
  10691. item_tmp.identify = 1;
  10692. item_tmp.refine = sd->status.inventory[i].refine;
  10693. item_tmp.attribute = sd->status.inventory[i].attribute;
  10694. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  10695. item_tmp.bound = sd->status.inventory[i].bound;
  10696. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  10697. item_tmp.card[j]=sd->status.inventory[i].card[j];
  10698. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  10699. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  10700. clif_additem(sd,0,0,flag);
  10701. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  10702. }
  10703. }
  10704. clif_misceffect(&sd->bl,2);
  10705. }
  10706. return SCRIPT_CMD_SUCCESS;
  10707. }
  10708. /* ================================================================
  10709. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  10710. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  10711. * improved by [Lance]
  10712. * ================================================================*/
  10713. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  10714. {
  10715. int x,y,m,check_val=0,check_ID=0,i=0;
  10716. struct guild *g = NULL;
  10717. struct party_data *p = NULL;
  10718. const char *str;
  10719. const char *mapname;
  10720. unsigned int index;
  10721. mapname=script_getstr(st,2);
  10722. str=script_getstr(st,3);
  10723. x=script_getnum(st,4);
  10724. y=script_getnum(st,5);
  10725. if(script_hasdata(st,7)){
  10726. check_val=script_getnum(st,6);
  10727. check_ID=script_getnum(st,7);
  10728. }
  10729. if((m=map_mapname2mapid(mapname))< 0)
  10730. return 0;
  10731. if(!(index=mapindex_name2id(str)))
  10732. return 0;
  10733. switch(check_val){
  10734. case 1:
  10735. g = guild_search(check_ID);
  10736. if (g){
  10737. for( i=0; i < g->max_member; i++)
  10738. {
  10739. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  10740. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  10741. }
  10742. }
  10743. }
  10744. break;
  10745. case 2:
  10746. p = party_search(check_ID);
  10747. if(p){
  10748. for(i=0;i<MAX_PARTY; i++){
  10749. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  10750. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  10751. }
  10752. }
  10753. }
  10754. break;
  10755. default:
  10756. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  10757. break;
  10758. }
  10759. return SCRIPT_CMD_SUCCESS;
  10760. }
  10761. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  10762. {
  10763. char *event=va_arg(ap,char *);
  10764. struct mob_data *md = ((struct mob_data *)bl);
  10765. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  10766. return 1;
  10767. return SCRIPT_CMD_SUCCESS;
  10768. }
  10769. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  10770. {
  10771. const char *mapname,*event;
  10772. int16 m;
  10773. mapname=script_getstr(st,2);
  10774. event=script_getstr(st,3);
  10775. if( strcmp(event, "all") == 0 )
  10776. event = NULL;
  10777. else
  10778. check_event(st, event);
  10779. if( strcmp(mapname, "this") == 0 ) {
  10780. struct map_session_data *sd = script_rid2sd(st);
  10781. if( sd )
  10782. m = sd->bl.m;
  10783. else {
  10784. script_pushint(st,-1);
  10785. return 0;
  10786. }
  10787. }
  10788. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  10789. script_pushint(st,-1);
  10790. return 0;
  10791. }
  10792. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  10793. return SCRIPT_CMD_SUCCESS;
  10794. }
  10795. BUILDIN_FUNC(marriage)
  10796. {
  10797. const char *partner=script_getstr(st,2);
  10798. TBL_PC *sd=script_rid2sd(st);
  10799. TBL_PC *p_sd=map_nick2sd(partner);
  10800. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  10801. script_pushint(st,0);
  10802. return 0;
  10803. }
  10804. script_pushint(st,1);
  10805. return SCRIPT_CMD_SUCCESS;
  10806. }
  10807. BUILDIN_FUNC(wedding_effect)
  10808. {
  10809. TBL_PC *sd=script_rid2sd(st);
  10810. struct block_list *bl;
  10811. if(sd==NULL) {
  10812. bl=map_id2bl(st->oid);
  10813. } else
  10814. bl=&sd->bl;
  10815. clif_wedding_effect(bl);
  10816. return SCRIPT_CMD_SUCCESS;
  10817. }
  10818. BUILDIN_FUNC(divorce)
  10819. {
  10820. TBL_PC *sd=script_rid2sd(st);
  10821. if(!sd || !pc_divorce(sd)){
  10822. script_pushint(st,0);
  10823. return 0;
  10824. }
  10825. script_pushint(st,1);
  10826. return SCRIPT_CMD_SUCCESS;
  10827. }
  10828. BUILDIN_FUNC(ispartneron)
  10829. {
  10830. TBL_PC *sd=script_rid2sd(st);
  10831. if(sd==NULL || !pc_ismarried(sd) ||
  10832. map_charid2sd(sd->status.partner_id) == NULL) {
  10833. script_pushint(st,0);
  10834. return 0;
  10835. }
  10836. script_pushint(st,1);
  10837. return SCRIPT_CMD_SUCCESS;
  10838. }
  10839. BUILDIN_FUNC(getpartnerid)
  10840. {
  10841. TBL_PC *sd=script_rid2sd(st);
  10842. if (sd == NULL) {
  10843. script_pushint(st,0);
  10844. return 0;
  10845. }
  10846. script_pushint(st,sd->status.partner_id);
  10847. return SCRIPT_CMD_SUCCESS;
  10848. }
  10849. BUILDIN_FUNC(getchildid)
  10850. {
  10851. TBL_PC *sd=script_rid2sd(st);
  10852. if (sd == NULL) {
  10853. script_pushint(st,0);
  10854. return 0;
  10855. }
  10856. script_pushint(st,sd->status.child);
  10857. return SCRIPT_CMD_SUCCESS;
  10858. }
  10859. BUILDIN_FUNC(getmotherid)
  10860. {
  10861. TBL_PC *sd=script_rid2sd(st);
  10862. if (sd == NULL) {
  10863. script_pushint(st,0);
  10864. return 0;
  10865. }
  10866. script_pushint(st,sd->status.mother);
  10867. return SCRIPT_CMD_SUCCESS;
  10868. }
  10869. BUILDIN_FUNC(getfatherid)
  10870. {
  10871. TBL_PC *sd=script_rid2sd(st);
  10872. if (sd == NULL) {
  10873. script_pushint(st,0);
  10874. return 0;
  10875. }
  10876. script_pushint(st,sd->status.father);
  10877. return SCRIPT_CMD_SUCCESS;
  10878. }
  10879. BUILDIN_FUNC(warppartner)
  10880. {
  10881. int x,y;
  10882. unsigned short mapindex;
  10883. const char *str;
  10884. TBL_PC *sd=script_rid2sd(st);
  10885. TBL_PC *p_sd=NULL;
  10886. if(sd==NULL || !pc_ismarried(sd) ||
  10887. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  10888. script_pushint(st,0);
  10889. return 0;
  10890. }
  10891. str=script_getstr(st,2);
  10892. x=script_getnum(st,3);
  10893. y=script_getnum(st,4);
  10894. mapindex = mapindex_name2id(str);
  10895. if (mapindex) {
  10896. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  10897. script_pushint(st,1);
  10898. } else
  10899. script_pushint(st,0);
  10900. return SCRIPT_CMD_SUCCESS;
  10901. }
  10902. /*================================================
  10903. * Script for Displaying MOB Information [Valaris]
  10904. *------------------------------------------------*/
  10905. BUILDIN_FUNC(strmobinfo)
  10906. {
  10907. int num=script_getnum(st,2);
  10908. int class_=script_getnum(st,3);
  10909. if(!mobdb_checkid(class_))
  10910. {
  10911. if (num < 3) //requested a string
  10912. script_pushconststr(st,"");
  10913. else
  10914. script_pushint(st,0);
  10915. return 0;
  10916. }
  10917. switch (num) {
  10918. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  10919. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  10920. case 3: script_pushint(st,mob_db(class_)->lv); break;
  10921. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  10922. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  10923. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  10924. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  10925. default:
  10926. script_pushint(st,0);
  10927. break;
  10928. }
  10929. return SCRIPT_CMD_SUCCESS;
  10930. }
  10931. /*==========================================
  10932. * Summon guardians [Valaris]
  10933. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  10934. *------------------------------------------*/
  10935. BUILDIN_FUNC(guardian)
  10936. {
  10937. int class_=0,x=0,y=0,guardian=0;
  10938. const char *str,*mapname,*evt="";
  10939. bool has_index = false;
  10940. mapname =script_getstr(st,2);
  10941. x =script_getnum(st,3);
  10942. y =script_getnum(st,4);
  10943. str =script_getstr(st,5);
  10944. class_=script_getnum(st,6);
  10945. if( script_hasdata(st,8) )
  10946. {// "<event label>",<guardian index>
  10947. evt=script_getstr(st,7);
  10948. guardian=script_getnum(st,8);
  10949. has_index = true;
  10950. } else if( script_hasdata(st,7) ){
  10951. struct script_data *data = script_getdata(st,7);
  10952. get_val(st,data);
  10953. if( data_isstring(data) )
  10954. {// "<event label>"
  10955. evt=script_getstr(st,7);
  10956. } else if( data_isint(data) )
  10957. {// <guardian index>
  10958. guardian=script_getnum(st,7);
  10959. has_index = true;
  10960. } else {
  10961. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  10962. script_reportdata(data);
  10963. return 1;
  10964. }
  10965. }
  10966. check_event(st, evt);
  10967. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  10968. return SCRIPT_CMD_SUCCESS;
  10969. }
  10970. /*==========================================
  10971. * Invisible Walls [Zephyrus]
  10972. *------------------------------------------*/
  10973. BUILDIN_FUNC(setwall)
  10974. {
  10975. const char *mapname, *name;
  10976. int x, y, m, size, dir;
  10977. bool shootable;
  10978. mapname = script_getstr(st,2);
  10979. x = script_getnum(st,3);
  10980. y = script_getnum(st,4);
  10981. size = script_getnum(st,5);
  10982. dir = script_getnum(st,6);
  10983. shootable = script_getnum(st,7);
  10984. name = script_getstr(st,8);
  10985. if( (m = map_mapname2mapid(mapname)) < 0 )
  10986. return 0; // Invalid Map
  10987. map_iwall_set(m, x, y, size, dir, shootable, name);
  10988. return SCRIPT_CMD_SUCCESS;
  10989. }
  10990. BUILDIN_FUNC(delwall)
  10991. {
  10992. const char *name = script_getstr(st,2);
  10993. map_iwall_remove(name);
  10994. return SCRIPT_CMD_SUCCESS;
  10995. }
  10996. /// Retrieves various information about the specified guardian.
  10997. ///
  10998. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  10999. /// type: 0 - whether it is deployed or not
  11000. /// 1 - maximum hp
  11001. /// 2 - current hp
  11002. ///
  11003. BUILDIN_FUNC(guardianinfo)
  11004. {
  11005. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11006. int id = script_getnum(st,3);
  11007. int type = script_getnum(st,4);
  11008. struct guild_castle* gc = guild_mapname2gc(mapname);
  11009. struct mob_data* gd;
  11010. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11011. {
  11012. script_pushint(st,-1);
  11013. return 0;
  11014. }
  11015. if( type == 0 )
  11016. script_pushint(st, gc->guardian[id].visible);
  11017. else
  11018. if( !gc->guardian[id].visible )
  11019. script_pushint(st,-1);
  11020. else
  11021. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11022. script_pushint(st,-1);
  11023. else
  11024. {
  11025. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11026. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11027. else
  11028. script_pushint(st,-1);
  11029. }
  11030. return SCRIPT_CMD_SUCCESS;
  11031. }
  11032. /*==========================================
  11033. * Get the item name by item_id or null
  11034. *------------------------------------------*/
  11035. BUILDIN_FUNC(getitemname)
  11036. {
  11037. unsigned short item_id = 0;
  11038. struct item_data *i_data;
  11039. char *item_name;
  11040. struct script_data *data;
  11041. data=script_getdata(st,2);
  11042. get_val(st,data);
  11043. if( data_isstring(data) ){
  11044. const char *name=conv_str(st,data);
  11045. struct item_data *item_data = itemdb_searchname(name);
  11046. if( item_data )
  11047. item_id=item_data->nameid;
  11048. }else
  11049. item_id=conv_num(st,data);
  11050. i_data = itemdb_exists(item_id);
  11051. if (i_data == NULL)
  11052. {
  11053. script_pushconststr(st,"null");
  11054. return 0;
  11055. }
  11056. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11057. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11058. script_pushstr(st,item_name);
  11059. return SCRIPT_CMD_SUCCESS;
  11060. }
  11061. /*==========================================
  11062. * Returns number of slots an item has. [Skotlex]
  11063. *------------------------------------------*/
  11064. BUILDIN_FUNC(getitemslots)
  11065. {
  11066. unsigned short item_id;
  11067. struct item_data *i_data;
  11068. item_id=script_getnum(st,2);
  11069. i_data = itemdb_exists(item_id);
  11070. if (i_data)
  11071. script_pushint(st,i_data->slot);
  11072. else
  11073. script_pushint(st,-1);
  11074. return SCRIPT_CMD_SUCCESS;
  11075. }
  11076. // TODO: add matk here if needed/once we get rid of RENEWAL
  11077. /*==========================================
  11078. * Returns some values of an item [Lupus]
  11079. * Price, Weight, etc...
  11080. getiteminfo(itemID,n), where n
  11081. 0 value_buy;
  11082. 1 value_sell;
  11083. 2 type;
  11084. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11085. if = 0, then monsters don't drop it at all (rare or a quest item)
  11086. if = -1, then this item is sold in NPC shops only
  11087. 4 sex;
  11088. 5 equip;
  11089. 6 weight;
  11090. 7 atk;
  11091. 8 def;
  11092. 9 range;
  11093. 10 slot;
  11094. 11 look;
  11095. 12 elv;
  11096. 13 wlv;
  11097. 14 view id
  11098. 15 eLvmax
  11099. 16 matk (renewal)
  11100. *------------------------------------------*/
  11101. BUILDIN_FUNC(getiteminfo)
  11102. {
  11103. unsigned short item_id,n;
  11104. struct item_data *i_data;
  11105. item_id = script_getnum(st,2);
  11106. n = script_getnum(st,3);
  11107. i_data = itemdb_exists(item_id);
  11108. if (i_data && n <= 16) {
  11109. int *item_arr = (int*)&i_data->value_buy;
  11110. #ifndef RENEWAL
  11111. if (n == 16)
  11112. script_pushint(st,0);
  11113. else
  11114. #endif
  11115. script_pushint(st,item_arr[n]);
  11116. } else
  11117. script_pushint(st,-1);
  11118. return SCRIPT_CMD_SUCCESS;
  11119. }
  11120. /*==========================================
  11121. * Set some values of an item [Lupus]
  11122. * Price, Weight, etc...
  11123. setiteminfo(itemID,n,Value), where n
  11124. 0 value_buy;
  11125. 1 value_sell;
  11126. 2 type;
  11127. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11128. if = 0, then monsters don't drop it at all (rare or a quest item)
  11129. if = -1, then this item is sold in NPC shops only
  11130. 4 sex;
  11131. 5 equip;
  11132. 6 weight;
  11133. 7 atk;
  11134. 8 def;
  11135. 9 range;
  11136. 10 slot;
  11137. 11 look;
  11138. 12 elv;
  11139. 13 wlv;
  11140. 14 view id
  11141. * Returns Value or -1 if the wrong field's been set
  11142. *------------------------------------------*/
  11143. BUILDIN_FUNC(setiteminfo)
  11144. {
  11145. unsigned short item_id;
  11146. int n,value;
  11147. struct item_data *i_data;
  11148. item_id = script_getnum(st,2);
  11149. n = script_getnum(st,3);
  11150. value = script_getnum(st,4);
  11151. i_data = itemdb_exists(item_id);
  11152. if (i_data && n>=0 && n<=14) {
  11153. int *item_arr = (int*)&i_data->value_buy;
  11154. item_arr[n] = value;
  11155. script_pushint(st,value);
  11156. } else
  11157. script_pushint(st,-1);
  11158. return SCRIPT_CMD_SUCCESS;
  11159. }
  11160. /*==========================================
  11161. * Returns value from equipped item slot n [Lupus]
  11162. getequipcardid(num,slot)
  11163. where
  11164. num = eqip position slot
  11165. slot = 0,1,2,3 (Card Slot N)
  11166. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11167. it's useful when you want to check item cards or if it's signed
  11168. Useful for such quests as "Sign this refined item with players name" etc
  11169. Hat[0] +4 -> Player's Hat[0] +4
  11170. *------------------------------------------*/
  11171. BUILDIN_FUNC(getequipcardid)
  11172. {
  11173. int i=-1,num,slot;
  11174. TBL_PC *sd;
  11175. num=script_getnum(st,2);
  11176. slot=script_getnum(st,3);
  11177. sd=script_rid2sd(st);
  11178. if (num > 0 && num <= ARRAYLENGTH(equip))
  11179. i=pc_checkequip(sd,equip[num-1]);
  11180. if(i >= 0 && slot>=0 && slot<4)
  11181. script_pushint(st,sd->status.inventory[i].card[slot]);
  11182. else
  11183. script_pushint(st,0);
  11184. return SCRIPT_CMD_SUCCESS;
  11185. }
  11186. /*==========================================
  11187. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11188. *------------------------------------------*/
  11189. BUILDIN_FUNC(petskillbonus)
  11190. {
  11191. struct pet_data *pd;
  11192. TBL_PC *sd = script_rid2sd(st);
  11193. if(sd == NULL || sd->pd == NULL)
  11194. return SCRIPT_CMD_FAILURE;
  11195. pd = sd->pd;
  11196. if (pd->bonus)
  11197. { //Clear previous bonus
  11198. if (pd->bonus->timer != INVALID_TIMER)
  11199. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11200. } else //init
  11201. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11202. pd->bonus->type = script_getnum(st,2);
  11203. pd->bonus->val = script_getnum(st,3);
  11204. pd->bonus->duration = script_getnum(st,4);
  11205. pd->bonus->delay = script_getnum(st,5);
  11206. if (pd->state.skillbonus == 1)
  11207. pd->state.skillbonus = 0; // waiting state
  11208. // wait for timer to start
  11209. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11210. pd->bonus->timer = INVALID_TIMER;
  11211. else
  11212. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11213. return SCRIPT_CMD_SUCCESS;
  11214. }
  11215. /*==========================================
  11216. * pet looting [Valaris] //Rewritten by [Skotlex]
  11217. *------------------------------------------*/
  11218. BUILDIN_FUNC(petloot)
  11219. {
  11220. int max;
  11221. struct pet_data *pd;
  11222. TBL_PC *sd=script_rid2sd(st);
  11223. if(sd==NULL || sd->pd==NULL)
  11224. return 0;
  11225. max=script_getnum(st,2);
  11226. if(max < 1)
  11227. max = 1; //Let'em loot at least 1 item.
  11228. else if (max > MAX_PETLOOT_SIZE)
  11229. max = MAX_PETLOOT_SIZE;
  11230. pd = sd->pd;
  11231. if (pd->loot != NULL)
  11232. { //Release whatever was there already and reallocate memory
  11233. pet_lootitem_drop(pd, pd->master);
  11234. aFree(pd->loot->item);
  11235. }
  11236. else
  11237. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11238. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11239. pd->loot->max=max;
  11240. pd->loot->count = 0;
  11241. pd->loot->weight = 0;
  11242. return SCRIPT_CMD_SUCCESS;
  11243. }
  11244. /*==========================================
  11245. * Set arrays with info of all sd inventory :
  11246. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11247. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11248. * @inventorylist_card(0..3), @inventorylist_expire
  11249. * @inventorylist_count = scalar
  11250. *------------------------------------------*/
  11251. BUILDIN_FUNC(getinventorylist)
  11252. {
  11253. TBL_PC *sd=script_rid2sd(st);
  11254. char card_var[NAME_LENGTH];
  11255. int i,j=0,k;
  11256. if(!sd) return 0;
  11257. for(i=0;i<MAX_INVENTORY;i++){
  11258. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  11259. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  11260. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  11261. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  11262. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  11263. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  11264. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  11265. for (k = 0; k < MAX_SLOTS; k++)
  11266. {
  11267. sprintf(card_var, "@inventorylist_card%d",k+1);
  11268. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  11269. }
  11270. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  11271. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  11272. j++;
  11273. }
  11274. }
  11275. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11276. return SCRIPT_CMD_SUCCESS;
  11277. }
  11278. BUILDIN_FUNC(getskilllist)
  11279. {
  11280. TBL_PC *sd=script_rid2sd(st);
  11281. int i,j=0;
  11282. if(!sd) return 0;
  11283. for(i=0;i<MAX_SKILL;i++){
  11284. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11285. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11286. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11287. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11288. j++;
  11289. }
  11290. }
  11291. pc_setreg(sd,add_str("@skilllist_count"),j);
  11292. return SCRIPT_CMD_SUCCESS;
  11293. }
  11294. BUILDIN_FUNC(clearitem)
  11295. {
  11296. TBL_PC *sd=script_rid2sd(st);
  11297. int i;
  11298. if(sd==NULL) return 0;
  11299. for (i=0; i<MAX_INVENTORY; i++) {
  11300. if (sd->status.inventory[i].amount) {
  11301. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11302. }
  11303. }
  11304. return SCRIPT_CMD_SUCCESS;
  11305. }
  11306. /*==========================================
  11307. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11308. *------------------------------------------*/
  11309. BUILDIN_FUNC(disguise)
  11310. {
  11311. int id;
  11312. TBL_PC* sd = script_rid2sd(st);
  11313. if (sd == NULL) return 0;
  11314. id = script_getnum(st,2);
  11315. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11316. pc_disguise(sd, id);
  11317. script_pushint(st,id);
  11318. } else
  11319. script_pushint(st,0);
  11320. return SCRIPT_CMD_SUCCESS;
  11321. }
  11322. /*==========================================
  11323. * Undisguise Player (returns 1 if success, 0 on fail)
  11324. *------------------------------------------*/
  11325. BUILDIN_FUNC(undisguise)
  11326. {
  11327. TBL_PC* sd = script_rid2sd(st);
  11328. if (sd == NULL) return 0;
  11329. if (sd->disguise) {
  11330. pc_disguise(sd, 0);
  11331. script_pushint(st,0);
  11332. } else {
  11333. script_pushint(st,1);
  11334. }
  11335. return SCRIPT_CMD_SUCCESS;
  11336. }
  11337. /*==========================================
  11338. * Transform a bl to another _class,
  11339. * @type unused
  11340. *------------------------------------------*/
  11341. BUILDIN_FUNC(classchange)
  11342. {
  11343. int _class,type;
  11344. struct block_list *bl=map_id2bl(st->oid);
  11345. if(bl==NULL) return 0;
  11346. _class=script_getnum(st,2);
  11347. type=script_getnum(st,3);
  11348. clif_class_change(bl,_class,type);
  11349. return SCRIPT_CMD_SUCCESS;
  11350. }
  11351. /*==========================================
  11352. * Display an effect
  11353. *------------------------------------------*/
  11354. BUILDIN_FUNC(misceffect)
  11355. {
  11356. int type;
  11357. type=script_getnum(st,2);
  11358. if(st->oid && st->oid != fake_nd->bl.id) {
  11359. struct block_list *bl = map_id2bl(st->oid);
  11360. if (bl)
  11361. clif_specialeffect(bl,type,AREA);
  11362. } else{
  11363. TBL_PC *sd=script_rid2sd(st);
  11364. if(sd)
  11365. clif_specialeffect(&sd->bl,type,AREA);
  11366. }
  11367. return SCRIPT_CMD_SUCCESS;
  11368. }
  11369. /*==========================================
  11370. * Play a BGM on a single client [Rikter/Yommy]
  11371. *------------------------------------------*/
  11372. BUILDIN_FUNC(playBGM)
  11373. {
  11374. struct map_session_data* sd;
  11375. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11376. clif_playBGM(sd, script_getstr(st,2));
  11377. }
  11378. return SCRIPT_CMD_SUCCESS;
  11379. }
  11380. static int playBGM_sub(struct block_list* bl,va_list ap)
  11381. {
  11382. const char* name = va_arg(ap,const char*);
  11383. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11384. return 0;
  11385. }
  11386. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11387. {
  11388. const char* name = va_arg(args, const char*);
  11389. clif_playBGM(sd, name);
  11390. return 0;
  11391. }
  11392. /*==========================================
  11393. * Play a BGM on multiple client [Rikter/Yommy]
  11394. *------------------------------------------*/
  11395. BUILDIN_FUNC(playBGMall)
  11396. {
  11397. const char* name;
  11398. name = script_getstr(st,2);
  11399. if( script_hasdata(st,7) ) {// specified part of map
  11400. const char* mapname = script_getstr(st,3);
  11401. int x0 = script_getnum(st,4);
  11402. int y0 = script_getnum(st,5);
  11403. int x1 = script_getnum(st,6);
  11404. int y1 = script_getnum(st,7);
  11405. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11406. }
  11407. else if( script_hasdata(st,3) ) {// entire map
  11408. const char* mapname = script_getstr(st,3);
  11409. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11410. }
  11411. else {// entire server
  11412. map_foreachpc(&playBGM_foreachpc_sub, name);
  11413. }
  11414. return SCRIPT_CMD_SUCCESS;
  11415. }
  11416. /*==========================================
  11417. * Play a .wav sound for sd
  11418. *------------------------------------------*/
  11419. BUILDIN_FUNC(soundeffect)
  11420. {
  11421. TBL_PC* sd = script_rid2sd(st);
  11422. const char* name = script_getstr(st,2);
  11423. int type = script_getnum(st,3);
  11424. if(sd) {
  11425. clif_soundeffect(sd,&sd->bl,name,type);
  11426. }
  11427. return SCRIPT_CMD_SUCCESS;
  11428. }
  11429. int soundeffect_sub(struct block_list* bl,va_list ap)
  11430. {
  11431. char* name = va_arg(ap,char*);
  11432. int type = va_arg(ap,int);
  11433. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11434. return 0;
  11435. }
  11436. /*==========================================
  11437. * Play a sound effect (.wav) on multiple clients
  11438. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11439. *------------------------------------------*/
  11440. BUILDIN_FUNC(soundeffectall)
  11441. {
  11442. struct block_list* bl;
  11443. const char* name;
  11444. int type;
  11445. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11446. if (!bl)
  11447. return 0;
  11448. name = script_getstr(st,2);
  11449. type = script_getnum(st,3);
  11450. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11451. if(!script_hasdata(st,4))
  11452. { // area around
  11453. clif_soundeffectall(bl, name, type, AREA);
  11454. }
  11455. else
  11456. if(!script_hasdata(st,5))
  11457. { // entire map
  11458. const char* mapname = script_getstr(st,4);
  11459. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  11460. }
  11461. else
  11462. if(script_hasdata(st,8))
  11463. { // specified part of map
  11464. const char* mapname = script_getstr(st,4);
  11465. int x0 = script_getnum(st,5);
  11466. int y0 = script_getnum(st,6);
  11467. int x1 = script_getnum(st,7);
  11468. int y1 = script_getnum(st,8);
  11469. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  11470. }
  11471. else
  11472. {
  11473. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11474. }
  11475. return SCRIPT_CMD_SUCCESS;
  11476. }
  11477. /*==========================================
  11478. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11479. *------------------------------------------*/
  11480. BUILDIN_FUNC(petrecovery)
  11481. {
  11482. struct pet_data *pd;
  11483. TBL_PC *sd=script_rid2sd(st);
  11484. int sc;
  11485. if(sd == NULL || sd->pd == NULL)
  11486. return SCRIPT_CMD_FAILURE;
  11487. sc = script_getnum(st,2);
  11488. if (sc <= SC_NONE || sc >= SC_MAX) {
  11489. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  11490. return SCRIPT_CMD_FAILURE;
  11491. }
  11492. pd = sd->pd;
  11493. if (pd->recovery)
  11494. { //Halt previous bonus
  11495. if (pd->recovery->timer != INVALID_TIMER)
  11496. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11497. } else //Init
  11498. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11499. pd->recovery->type = (sc_type)sc;
  11500. pd->recovery->delay = script_getnum(st,3);
  11501. pd->recovery->timer = INVALID_TIMER;
  11502. return SCRIPT_CMD_SUCCESS;
  11503. }
  11504. /*==========================================
  11505. * pet healing [Valaris] //Rewritten by [Skotlex]
  11506. *------------------------------------------*/
  11507. BUILDIN_FUNC(petheal)
  11508. {
  11509. struct pet_data *pd;
  11510. TBL_PC *sd=script_rid2sd(st);
  11511. if(sd==NULL || sd->pd==NULL)
  11512. return 0;
  11513. pd=sd->pd;
  11514. if (pd->s_skill)
  11515. { //Clear previous skill
  11516. if (pd->s_skill->timer != INVALID_TIMER)
  11517. {
  11518. if (pd->s_skill->id)
  11519. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11520. else
  11521. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11522. }
  11523. } else //init memory
  11524. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11525. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11526. //Use the lv as the amount to heal
  11527. pd->s_skill->lv=script_getnum(st,2);
  11528. pd->s_skill->delay=script_getnum(st,3);
  11529. pd->s_skill->hp=script_getnum(st,4);
  11530. pd->s_skill->sp=script_getnum(st,5);
  11531. //Use delay as initial offset to avoid skill/heal exploits
  11532. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11533. pd->s_skill->timer = INVALID_TIMER;
  11534. else
  11535. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11536. return SCRIPT_CMD_SUCCESS;
  11537. }
  11538. /*==========================================
  11539. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11540. *------------------------------------------*/
  11541. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11542. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11543. BUILDIN_FUNC(petskillattack)
  11544. {
  11545. struct pet_data *pd;
  11546. struct script_data *data;
  11547. TBL_PC *sd = script_rid2sd(st);
  11548. int id = 0;
  11549. if(sd == NULL || sd->pd == NULL)
  11550. return SCRIPT_CMD_FAILURE;
  11551. data = script_getdata(st, 2);
  11552. get_val(st, data);
  11553. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11554. if (!id) {
  11555. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  11556. return SCRIPT_CMD_FAILURE;
  11557. }
  11558. pd = sd->pd;
  11559. if (pd->a_skill == NULL)
  11560. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11561. pd->a_skill->id = id;
  11562. pd->a_skill->damage = 0;
  11563. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  11564. pd->a_skill->div_ = 0;
  11565. pd->a_skill->rate = script_getnum(st,4);
  11566. pd->a_skill->bonusrate = script_getnum(st,5);
  11567. return SCRIPT_CMD_SUCCESS;
  11568. }
  11569. /*==========================================
  11570. * pet attack skills [Valaris]
  11571. *------------------------------------------*/
  11572. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  11573. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  11574. BUILDIN_FUNC(petskillattack2)
  11575. {
  11576. struct pet_data *pd;
  11577. struct script_data *data;
  11578. TBL_PC *sd = script_rid2sd(st);
  11579. int id = 0;
  11580. if(sd == NULL || sd->pd == NULL)
  11581. return SCRIPT_CMD_FAILURE;
  11582. data = script_getdata(st, 2);
  11583. get_val(st, data);
  11584. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11585. if (!id) {
  11586. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  11587. return SCRIPT_CMD_FAILURE;
  11588. }
  11589. pd = sd->pd;
  11590. if (pd->a_skill == NULL)
  11591. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  11592. pd->a_skill->id = id;
  11593. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  11594. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  11595. pd->a_skill->div_ = script_getnum(st,4);
  11596. pd->a_skill->rate = script_getnum(st,5);
  11597. pd->a_skill->bonusrate = script_getnum(st,6);
  11598. return SCRIPT_CMD_SUCCESS;
  11599. }
  11600. /*==========================================
  11601. * pet support skills [Skotlex]
  11602. *------------------------------------------*/
  11603. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  11604. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  11605. BUILDIN_FUNC(petskillsupport)
  11606. {
  11607. struct pet_data *pd;
  11608. struct script_data *data;
  11609. TBL_PC *sd = script_rid2sd(st);
  11610. int id = 0;
  11611. if(sd == NULL || sd->pd == NULL)
  11612. return SCRIPT_CMD_FAILURE;
  11613. data = script_getdata(st, 2);
  11614. get_val(st, data);
  11615. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  11616. if (!id) {
  11617. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  11618. return SCRIPT_CMD_FAILURE;
  11619. }
  11620. pd = sd->pd;
  11621. if (pd->s_skill)
  11622. { //Clear previous skill
  11623. if (pd->s_skill->timer != INVALID_TIMER)
  11624. {
  11625. if (pd->s_skill->id)
  11626. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11627. else
  11628. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11629. }
  11630. } else //init memory
  11631. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11632. pd->s_skill->id = id;
  11633. pd->s_skill->lv = script_getnum(st,3);
  11634. pd->s_skill->delay = script_getnum(st,4);
  11635. pd->s_skill->hp = script_getnum(st,5);
  11636. pd->s_skill->sp = script_getnum(st,6);
  11637. //Use delay as initial offset to avoid skill/heal exploits
  11638. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11639. pd->s_skill->timer = INVALID_TIMER;
  11640. else
  11641. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  11642. return SCRIPT_CMD_SUCCESS;
  11643. }
  11644. /*==========================================
  11645. * Scripted skill effects [Celest]
  11646. *------------------------------------------*/
  11647. /// skilleffect <skill id>,<level>
  11648. /// skilleffect "<skill name>",<level>
  11649. BUILDIN_FUNC(skilleffect)
  11650. {
  11651. TBL_PC *sd;
  11652. uint16 skill_id, skill_lv;
  11653. struct script_data *data = script_getdata(st, 2);
  11654. get_val(st, data); // Convert into value in case of a variable
  11655. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11656. skill_lv = script_getnum(st,3);
  11657. sd = script_rid2sd(st);
  11658. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  11659. * which leaves the server thinking it still is sitting. */
  11660. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  11661. skill_sit(sd, 0);
  11662. clif_standing(&sd->bl);
  11663. }
  11664. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  11665. return SCRIPT_CMD_SUCCESS;
  11666. }
  11667. /*==========================================
  11668. * NPC skill effects [Valaris]
  11669. *------------------------------------------*/
  11670. /// npcskilleffect <skill id>,<level>,<x>,<y>
  11671. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  11672. BUILDIN_FUNC(npcskilleffect)
  11673. {
  11674. struct block_list *bl= map_id2bl(st->oid);
  11675. uint16 skill_id, skill_lv;
  11676. int x, y;
  11677. struct script_data *data = script_getdata(st, 2);
  11678. get_val(st, data); // Convert into value in case of a variable
  11679. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  11680. skill_lv=script_getnum(st,3);
  11681. x=script_getnum(st,4);
  11682. y=script_getnum(st,5);
  11683. if (bl)
  11684. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  11685. return SCRIPT_CMD_SUCCESS;
  11686. }
  11687. /*==========================================
  11688. * Special effects [Valaris]
  11689. *------------------------------------------*/
  11690. BUILDIN_FUNC(specialeffect)
  11691. {
  11692. struct block_list *bl=map_id2bl(st->oid);
  11693. int type = script_getnum(st,2);
  11694. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11695. if(bl==NULL)
  11696. return 0;
  11697. if( script_hasdata(st,4) )
  11698. {
  11699. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  11700. if(nd)
  11701. clif_specialeffect(&nd->bl, type, target);
  11702. }
  11703. else
  11704. {
  11705. if (target == SELF) {
  11706. TBL_PC *sd=script_rid2sd(st);
  11707. if (sd)
  11708. clif_specialeffect_single(bl,type,sd->fd);
  11709. } else {
  11710. clif_specialeffect(bl, type, target);
  11711. }
  11712. }
  11713. return SCRIPT_CMD_SUCCESS;
  11714. }
  11715. BUILDIN_FUNC(specialeffect2)
  11716. {
  11717. TBL_PC *sd=script_rid2sd(st);
  11718. int type = script_getnum(st,2);
  11719. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  11720. if( script_hasdata(st,4) )
  11721. sd = map_nick2sd(script_getstr(st,4));
  11722. if (sd)
  11723. clif_specialeffect(&sd->bl, type, target);
  11724. return SCRIPT_CMD_SUCCESS;
  11725. }
  11726. /*==========================================
  11727. * Nude [Valaris]
  11728. *------------------------------------------*/
  11729. BUILDIN_FUNC(nude)
  11730. {
  11731. TBL_PC *sd = script_rid2sd(st);
  11732. int i, calcflag = 0;
  11733. if( sd == NULL )
  11734. return 0;
  11735. for( i = 0 ; i < EQI_MAX; i++ ) {
  11736. if( sd->equip_index[ i ] >= 0 ) {
  11737. if( !calcflag )
  11738. calcflag = 1;
  11739. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  11740. }
  11741. }
  11742. if( calcflag )
  11743. status_calc_pc(sd,SCO_NONE);
  11744. return SCRIPT_CMD_SUCCESS;
  11745. }
  11746. int atcommand_sub(struct script_state* st,int type) {
  11747. TBL_PC *sd, dummy_sd;
  11748. int fd;
  11749. const char *cmd;
  11750. cmd = script_getstr(st,2);
  11751. if (st->rid) {
  11752. sd = script_rid2sd(st);
  11753. fd = sd->fd;
  11754. } else { //Use a dummy character.
  11755. sd = &dummy_sd;
  11756. fd = 0;
  11757. memset(&dummy_sd, 0, sizeof(TBL_PC));
  11758. if (st->oid) {
  11759. struct block_list* bl = map_id2bl(st->oid);
  11760. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  11761. if (bl->type == BL_NPC)
  11762. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  11763. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(map_default.mapname);
  11764. }
  11765. // Init Group ID, Level, & permissions
  11766. sd->group_id = sd->group_level = 99;
  11767. sd->permissions |= PC_PERM_ALLPERMISSION;
  11768. }
  11769. if (!is_atcommand(fd, sd, cmd, type)) {
  11770. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  11771. script_reportsrc(st);
  11772. return SCRIPT_CMD_FAILURE;
  11773. }
  11774. return SCRIPT_CMD_SUCCESS;
  11775. }
  11776. /*==========================================
  11777. * gmcommand [MouseJstr]
  11778. *------------------------------------------*/
  11779. BUILDIN_FUNC(atcommand) {
  11780. return atcommand_sub(st,0);
  11781. }
  11782. /** Displays a message for the player only (like system messages like "you got an apple" )
  11783. * dispbottom("<message>"{,<color>})
  11784. * @param message
  11785. * @param color Hex color default (Green)
  11786. */
  11787. BUILDIN_FUNC(dispbottom)
  11788. {
  11789. TBL_PC *sd=script_rid2sd(st);
  11790. int color = 0;
  11791. const char *message;
  11792. message = script_getstr(st,2);
  11793. if (script_hasdata(st,3))
  11794. color = script_getnum(st,3); // <color>
  11795. if(sd) {
  11796. if (script_hasdata(st,3))
  11797. clif_messagecolor2(sd, color, message); // [Napster]
  11798. else
  11799. clif_disp_onlyself(sd, message, (int)strlen(message));
  11800. }
  11801. return SCRIPT_CMD_SUCCESS;
  11802. }
  11803. /*===================================
  11804. * Heal portion of recovery command
  11805. *-----------------------------------*/
  11806. int recovery_sub(struct map_session_data* sd, int revive)
  11807. {
  11808. if(revive&(1|4) && pc_isdead(sd)) {
  11809. status_revive(&sd->bl, 100, 100);
  11810. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  11811. clif_specialeffect(&sd->bl, 77, AREA);
  11812. } else if(revive&(1|2) && !pc_isdead(sd)) {
  11813. status_percent_heal(&sd->bl, 100, 100);
  11814. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  11815. }
  11816. return SCRIPT_CMD_SUCCESS;
  11817. }
  11818. /*=========================================================================
  11819. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  11820. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  11821. * <type> determines <option>:
  11822. * 0 : char_id
  11823. * 1 : party_id
  11824. * 2 : guild_id
  11825. * 3 : map_name
  11826. * 4 : all characters
  11827. * <revive_flag>:
  11828. * 1 : Revive and heal all players (default)
  11829. * 2 : Heal living players only
  11830. * 4 : Revive dead players only
  11831. * <map name>:
  11832. * for types 1-2 : map_name (null = all maps)
  11833. *-------------------------------------------------------------------------*/
  11834. BUILDIN_FUNC(recovery)
  11835. {
  11836. TBL_PC *sd;
  11837. int map_idx = 0, type = 0, revive = 1;
  11838. type = script_getnum(st,2);
  11839. if(script_hasdata(st,4))
  11840. revive = script_getnum(st,4);
  11841. switch(type) {
  11842. case 0:
  11843. if(script_hasdata(st,3))
  11844. sd=map_charid2sd(script_getnum(st,3));
  11845. else if((sd = script_rid2sd(st)) == NULL)
  11846. return 0; //If we don't have sd by now, bail out
  11847. recovery_sub(sd, revive);
  11848. break;
  11849. case 1:
  11850. {
  11851. struct party_data* p;
  11852. //When no party given, we use invoker party
  11853. int p_id = 0, i;
  11854. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11855. map_idx = map_mapname2mapid(script_getstr(st,5));
  11856. if(map_idx < 1) { //But we'll check anyways
  11857. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11858. return 1;
  11859. }
  11860. }
  11861. if(script_hasdata(st,3))
  11862. p_id = script_getnum(st,3);
  11863. else if((sd = script_rid2sd(st)))
  11864. p_id = sd->status.party_id;
  11865. p = party_search(p_id);
  11866. if(p == NULL)
  11867. return 0;
  11868. for (i = 0; i < MAX_PARTY; i++) {
  11869. struct map_session_data* pl_sd;
  11870. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  11871. || (map_idx && pl_sd->bl.m != map_idx))
  11872. continue;
  11873. recovery_sub(pl_sd, revive);
  11874. }
  11875. break;
  11876. }
  11877. case 2:
  11878. {
  11879. struct guild* g;
  11880. //When no guild given, we use invoker guild
  11881. int g_id = 0, i;
  11882. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  11883. map_idx = map_mapname2mapid(script_getstr(st,5));
  11884. if(map_idx < 1) { //But we'll check anyways
  11885. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  11886. return 1;
  11887. }
  11888. }
  11889. if(script_hasdata(st,3))
  11890. g_id = script_getnum(st,3);
  11891. else if((sd = script_rid2sd(st)))
  11892. g_id = sd->status.guild_id;
  11893. g = guild_search(g_id);
  11894. if(g == NULL)
  11895. return 0;
  11896. for (i = 0; i < MAX_GUILD; i++) {
  11897. struct map_session_data* pl_sd;
  11898. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  11899. || (map_idx && pl_sd->bl.m != map_idx))
  11900. continue;
  11901. recovery_sub(pl_sd, revive);
  11902. }
  11903. break;
  11904. }
  11905. case 3:
  11906. if(script_hasdata(st,3))
  11907. map_idx = map_mapname2mapid(script_getstr(st,3));
  11908. else if((sd = script_rid2sd(st)))
  11909. map_idx = sd->bl.m;
  11910. if(map_idx < 1)
  11911. return 1; //No sd and no map given - return
  11912. case 4:
  11913. {
  11914. struct s_mapiterator *iter;
  11915. struct script_data *data = script_getdata(st, 3);
  11916. get_val(st, data); // Convert into value in case of a variable
  11917. if(script_hasdata(st,3) && !data_isstring(data))
  11918. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  11919. iter = mapit_getallusers();
  11920. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  11921. if(type == 3 && sd->bl.m != map_idx)
  11922. continue;
  11923. recovery_sub(sd, revive);
  11924. }
  11925. mapit_free(iter);
  11926. break;
  11927. }
  11928. default:
  11929. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  11930. script_pushint(st,-1);
  11931. return 1;
  11932. }
  11933. script_pushint(st,1); //Successfully executed without errors
  11934. return SCRIPT_CMD_SUCCESS;
  11935. }
  11936. /*==========================================
  11937. * Get your pet info: getpetinfo(n)
  11938. * n -> 0:pet_id 1:pet_class 2:pet_name
  11939. * 3:friendly 4:hungry, 5: rename flag.6:level
  11940. *------------------------------------------*/
  11941. BUILDIN_FUNC(getpetinfo)
  11942. {
  11943. TBL_PC *sd=script_rid2sd(st);
  11944. TBL_PET *pd;
  11945. int type=script_getnum(st,2);
  11946. if(!sd || !sd->pd) {
  11947. if (type == 2)
  11948. script_pushconststr(st,"null");
  11949. else
  11950. script_pushint(st,0);
  11951. return 0;
  11952. }
  11953. pd = sd->pd;
  11954. switch(type){
  11955. case 0: script_pushint(st,pd->pet.pet_id); break;
  11956. case 1: script_pushint(st,pd->pet.class_); break;
  11957. case 2: script_pushstrcopy(st,pd->pet.name); break;
  11958. case 3: script_pushint(st,pd->pet.intimate); break;
  11959. case 4: script_pushint(st,pd->pet.hungry); break;
  11960. case 5: script_pushint(st,pd->pet.rename_flag); break;
  11961. case 6: script_pushint(st,(int)pd->pet.level); break;
  11962. default:
  11963. script_pushint(st,0);
  11964. break;
  11965. }
  11966. return SCRIPT_CMD_SUCCESS;
  11967. }
  11968. /*==========================================
  11969. * Get your homunculus info: gethominfo(n)
  11970. * n -> 0:hom_id 1:class 2:name
  11971. * 3:friendly 4:hungry, 5: rename flag.
  11972. * 6: level
  11973. *------------------------------------------*/
  11974. BUILDIN_FUNC(gethominfo)
  11975. {
  11976. TBL_PC *sd=script_rid2sd(st);
  11977. TBL_HOM *hd;
  11978. int type=script_getnum(st,2);
  11979. hd = sd?sd->hd:NULL;
  11980. if(!hd) {
  11981. if (type == 2)
  11982. script_pushconststr(st,"null");
  11983. else
  11984. script_pushint(st,0);
  11985. return 0;
  11986. }
  11987. switch(type){
  11988. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  11989. case 1: script_pushint(st,hd->homunculus.class_); break;
  11990. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  11991. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  11992. case 4: script_pushint(st,hd->homunculus.hunger); break;
  11993. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  11994. case 6: script_pushint(st,hd->homunculus.level); break;
  11995. default:
  11996. script_pushint(st,0);
  11997. break;
  11998. }
  11999. return SCRIPT_CMD_SUCCESS;
  12000. }
  12001. /// Retrieves information about character's mercenary
  12002. /// getmercinfo <type>[,<char id>];
  12003. BUILDIN_FUNC(getmercinfo)
  12004. {
  12005. int type;
  12006. struct map_session_data* sd;
  12007. struct mercenary_data* md;
  12008. type = script_getnum(st,2);
  12009. if( script_hasdata(st,3) )
  12010. {
  12011. int char_id = script_getnum(st,3);
  12012. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12013. {
  12014. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12015. script_pushnil(st);
  12016. return 1;
  12017. }
  12018. }
  12019. else
  12020. {
  12021. if( ( sd = script_rid2sd(st) ) == NULL )
  12022. {
  12023. script_pushnil(st);
  12024. return 0;
  12025. }
  12026. }
  12027. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12028. switch( type )
  12029. {
  12030. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12031. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12032. case 2:
  12033. if( md )
  12034. script_pushstrcopy(st,md->db->name);
  12035. else
  12036. script_pushconststr(st,"");
  12037. break;
  12038. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12039. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12040. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12041. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12042. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12043. default:
  12044. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12045. script_pushnil(st);
  12046. return 1;
  12047. }
  12048. return SCRIPT_CMD_SUCCESS;
  12049. }
  12050. /*==========================================
  12051. * Shows wether your inventory(and equips) contain
  12052. selected card or not.
  12053. checkequipedcard(4001);
  12054. *------------------------------------------*/
  12055. BUILDIN_FUNC(checkequipedcard)
  12056. {
  12057. TBL_PC *sd=script_rid2sd(st);
  12058. if(sd){
  12059. int n,i,c=0;
  12060. c=script_getnum(st,2);
  12061. for(i=0;i<MAX_INVENTORY;i++){
  12062. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  12063. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  12064. continue;
  12065. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12066. if(sd->status.inventory[i].card[n]==c){
  12067. script_pushint(st,1);
  12068. return 0;
  12069. }
  12070. }
  12071. }
  12072. }
  12073. }
  12074. script_pushint(st,0);
  12075. return SCRIPT_CMD_SUCCESS;
  12076. }
  12077. BUILDIN_FUNC(jump_zero)
  12078. {
  12079. int sel;
  12080. sel=script_getnum(st,2);
  12081. if(!sel) {
  12082. int pos;
  12083. if( !data_islabel(script_getdata(st,3)) ){
  12084. ShowError("script: jump_zero: not label !\n");
  12085. st->state=END;
  12086. return 1;
  12087. }
  12088. pos=script_getnum(st,3);
  12089. st->pos=pos;
  12090. st->state=GOTO;
  12091. }
  12092. return SCRIPT_CMD_SUCCESS;
  12093. }
  12094. /*==========================================
  12095. * movenpc [MouseJstr]
  12096. *------------------------------------------*/
  12097. BUILDIN_FUNC(movenpc)
  12098. {
  12099. TBL_NPC *nd = NULL;
  12100. const char *npc;
  12101. int x,y;
  12102. npc = script_getstr(st,2);
  12103. x = script_getnum(st,3);
  12104. y = script_getnum(st,4);
  12105. if ((nd = npc_name2id(npc)) == NULL){
  12106. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12107. return -1;
  12108. }
  12109. if (script_hasdata(st,5))
  12110. nd->ud.dir = script_getnum(st,5) % 8;
  12111. npc_movenpc(nd, x, y);
  12112. return SCRIPT_CMD_SUCCESS;
  12113. }
  12114. /*==========================================
  12115. * message [MouseJstr]
  12116. *------------------------------------------*/
  12117. BUILDIN_FUNC(message)
  12118. {
  12119. const char *msg,*player;
  12120. TBL_PC *pl_sd = NULL;
  12121. player = script_getstr(st,2);
  12122. msg = script_getstr(st,3);
  12123. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12124. return 0;
  12125. clif_displaymessage(pl_sd->fd, msg);
  12126. return SCRIPT_CMD_SUCCESS;
  12127. }
  12128. /*==========================================
  12129. * npctalk (sends message to surrounding area)
  12130. *------------------------------------------*/
  12131. BUILDIN_FUNC(npctalk)
  12132. {
  12133. const char* str;
  12134. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  12135. str = script_getstr(st,2);
  12136. if(nd)
  12137. {
  12138. char name[NAME_LENGTH], message[256];
  12139. safestrncpy(name, nd->name, sizeof(name));
  12140. strtok(name, "#"); // discard extra name identifier if present
  12141. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  12142. clif_disp_overhead(&nd->bl, message);
  12143. }
  12144. return SCRIPT_CMD_SUCCESS;
  12145. }
  12146. // change npc walkspeed [Valaris]
  12147. BUILDIN_FUNC(npcspeed)
  12148. {
  12149. struct npc_data* nd;
  12150. int speed;
  12151. speed = script_getnum(st,2);
  12152. nd =(struct npc_data *)map_id2bl(st->oid);
  12153. if( nd ) {
  12154. nd->speed = speed;
  12155. nd->ud.state.speed_changed = 1;
  12156. }
  12157. return SCRIPT_CMD_SUCCESS;
  12158. }
  12159. // make an npc walk to a position [Valaris]
  12160. BUILDIN_FUNC(npcwalkto)
  12161. {
  12162. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12163. int x=0,y=0;
  12164. x=script_getnum(st,2);
  12165. y=script_getnum(st,3);
  12166. if(nd) {
  12167. if (!nd->status.hp)
  12168. status_calc_npc(nd, SCO_FIRST);
  12169. else
  12170. status_calc_npc(nd, SCO_NONE);
  12171. unit_walktoxy(&nd->bl,x,y,0);
  12172. }
  12173. return SCRIPT_CMD_SUCCESS;
  12174. }
  12175. // stop an npc's movement [Valaris]
  12176. BUILDIN_FUNC(npcstop)
  12177. {
  12178. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12179. if(nd) {
  12180. unit_stop_walking(&nd->bl,1|4);
  12181. }
  12182. return SCRIPT_CMD_SUCCESS;
  12183. }
  12184. /*==========================================
  12185. * getlook char info. getlook(arg)
  12186. *------------------------------------------*/
  12187. BUILDIN_FUNC(getlook)
  12188. {
  12189. int type,val;
  12190. TBL_PC *sd;
  12191. sd=script_rid2sd(st);
  12192. type=script_getnum(st,2);
  12193. val=-1;
  12194. switch(type) {
  12195. case LOOK_HAIR: val=sd->status.hair; break; //1
  12196. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12197. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12198. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12199. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12200. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12201. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12202. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12203. case LOOK_SHOES: break; //9
  12204. case LOOK_ROBE: val=sd->status.robe; break; //12
  12205. }
  12206. script_pushint(st,val);
  12207. return SCRIPT_CMD_SUCCESS;
  12208. }
  12209. /*==========================================
  12210. * get char save point. argument: 0- map name, 1- x, 2- y
  12211. *------------------------------------------*/
  12212. BUILDIN_FUNC(getsavepoint)
  12213. {
  12214. TBL_PC* sd;
  12215. int type;
  12216. sd = script_rid2sd(st);
  12217. if (sd == NULL) {
  12218. script_pushint(st,0);
  12219. return 0;
  12220. }
  12221. type = script_getnum(st,2);
  12222. switch(type) {
  12223. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12224. case 1: script_pushint(st,sd->status.save_point.x); break;
  12225. case 2: script_pushint(st,sd->status.save_point.y); break;
  12226. default:
  12227. script_pushint(st,0);
  12228. break;
  12229. }
  12230. return SCRIPT_CMD_SUCCESS;
  12231. }
  12232. /*==========================================
  12233. * Get position for char/npc/pet/mob objects. Added by Lorky
  12234. *
  12235. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  12236. * where type:
  12237. * MapName$ - String variable for output map name
  12238. * MapX - Integer variable for output coord X
  12239. * MapY - Integer variable for output coord Y
  12240. * type - type of object
  12241. * 0 - Character coord
  12242. * 1 - NPC coord
  12243. * 2 - Pet coord
  12244. * 3 - Mob coord (not released)
  12245. * 4 - Homun coord
  12246. * 5 - Mercenary coord
  12247. * 6 - Elemental coord
  12248. * CharName$ - Name object. If miss or "this" the current object
  12249. *
  12250. * Return:
  12251. * 0 - success
  12252. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  12253. *------------------------------------------*/
  12254. BUILDIN_FUNC(getmapxy)
  12255. {
  12256. struct block_list *bl = NULL;
  12257. TBL_PC *sd=NULL;
  12258. int num;
  12259. const char *name;
  12260. char prefix;
  12261. int x,y,type;
  12262. char mapname[MAP_NAME_LENGTH];
  12263. if( !data_isreference(script_getdata(st,2)) ){
  12264. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  12265. script_pushint(st,-1);
  12266. return 1;
  12267. }
  12268. if( !data_isreference(script_getdata(st,3)) ){
  12269. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  12270. script_pushint(st,-1);
  12271. return 1;
  12272. }
  12273. if( !data_isreference(script_getdata(st,4)) ){
  12274. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  12275. script_pushint(st,-1);
  12276. return 1;
  12277. }
  12278. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12279. type=script_getnum(st,5);
  12280. switch (type){
  12281. case 0: //Get Character Position
  12282. if( script_hasdata(st,6) )
  12283. sd=map_nick2sd(script_getstr(st,6));
  12284. else
  12285. sd=script_rid2sd(st);
  12286. if (sd)
  12287. bl = &sd->bl;
  12288. break;
  12289. case 1: //Get NPC Position
  12290. if( script_hasdata(st,6) )
  12291. {
  12292. struct npc_data *nd;
  12293. nd=npc_name2id(script_getstr(st,6));
  12294. if (nd)
  12295. bl = &nd->bl;
  12296. } else //In case the origin is not an npc?
  12297. bl=map_id2bl(st->oid);
  12298. break;
  12299. case 2: //Get Pet Position
  12300. if(script_hasdata(st,6))
  12301. sd=map_nick2sd(script_getstr(st,6));
  12302. else
  12303. sd=script_rid2sd(st);
  12304. if (sd && sd->pd)
  12305. bl = &sd->pd->bl;
  12306. break;
  12307. case 3: //Get Mob Position
  12308. break; //Not supported?
  12309. case 4: //Get Homun Position
  12310. if(script_hasdata(st,6))
  12311. sd=map_nick2sd(script_getstr(st,6));
  12312. else
  12313. sd=script_rid2sd(st);
  12314. if (sd && sd->hd)
  12315. bl = &sd->hd->bl;
  12316. break;
  12317. case 5: //Get Mercenary Position
  12318. if(script_hasdata(st,6))
  12319. sd=map_nick2sd(script_getstr(st,6));
  12320. else
  12321. sd=script_rid2sd(st);
  12322. if (sd && sd->md)
  12323. bl = &sd->md->bl;
  12324. break;
  12325. case 6: //Get Elemental Position
  12326. if(script_hasdata(st,6))
  12327. sd=map_nick2sd(script_getstr(st,6));
  12328. else
  12329. sd=script_rid2sd(st);
  12330. if (sd && sd->ed)
  12331. bl = &sd->ed->bl;
  12332. break;
  12333. default:
  12334. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  12335. script_pushint(st,-1);
  12336. return 1;
  12337. }
  12338. if (!bl) { //No object found.
  12339. script_pushint(st,-1);
  12340. return 0;
  12341. }
  12342. x= bl->x;
  12343. y= bl->y;
  12344. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12345. //Set MapName$
  12346. num=st->stack->stack_data[st->start+2].u.num;
  12347. name=get_str(num&0x00ffffff);
  12348. prefix=*name;
  12349. if(not_server_variable(prefix))
  12350. sd=script_rid2sd(st);
  12351. else
  12352. sd=NULL;
  12353. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12354. //Set MapX
  12355. num=st->stack->stack_data[st->start+3].u.num;
  12356. name=get_str(num&0x00ffffff);
  12357. prefix=*name;
  12358. if(not_server_variable(prefix))
  12359. sd=script_rid2sd(st);
  12360. else
  12361. sd=NULL;
  12362. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12363. //Set MapY
  12364. num=st->stack->stack_data[st->start+4].u.num;
  12365. name=get_str(num&0x00ffffff);
  12366. prefix=*name;
  12367. if(not_server_variable(prefix))
  12368. sd=script_rid2sd(st);
  12369. else
  12370. sd=NULL;
  12371. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12372. //Return Success value
  12373. script_pushint(st,0);
  12374. return SCRIPT_CMD_SUCCESS;
  12375. }
  12376. /*==========================================
  12377. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12378. *------------------------------------------*/
  12379. BUILDIN_FUNC(logmes)
  12380. {
  12381. const char *str;
  12382. TBL_PC* sd;
  12383. sd = script_rid2sd(st);
  12384. if( sd == NULL )
  12385. return 1;
  12386. str = script_getstr(st,2);
  12387. log_npc(sd,str);
  12388. return SCRIPT_CMD_SUCCESS;
  12389. }
  12390. BUILDIN_FUNC(summon)
  12391. {
  12392. int _class, timeout=0;
  12393. const char *str,*event="";
  12394. TBL_PC *sd;
  12395. struct mob_data *md;
  12396. int tick = gettick();
  12397. sd=script_rid2sd(st);
  12398. if (!sd) return 0;
  12399. str =script_getstr(st,2);
  12400. _class=script_getnum(st,3);
  12401. if( script_hasdata(st,4) )
  12402. timeout=script_getnum(st,4);
  12403. if( script_hasdata(st,5) ){
  12404. event=script_getstr(st,5);
  12405. check_event(st, event);
  12406. }
  12407. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12408. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12409. if (md) {
  12410. md->master_id=sd->bl.id;
  12411. md->special_state.ai = AI_ATTACK;
  12412. if( md->deletetimer != INVALID_TIMER )
  12413. delete_timer(md->deletetimer, mob_timer_delete);
  12414. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12415. mob_spawn (md); //Now it is ready for spawning.
  12416. clif_specialeffect(&md->bl,344,AREA);
  12417. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12418. }
  12419. script_pushint(st, md->bl.id);
  12420. return SCRIPT_CMD_SUCCESS;
  12421. }
  12422. /*==========================================
  12423. * Checks whether it is daytime/nighttime
  12424. *------------------------------------------*/
  12425. BUILDIN_FUNC(isnight)
  12426. {
  12427. script_pushint(st,(night_flag == 1));
  12428. return SCRIPT_CMD_SUCCESS;
  12429. }
  12430. BUILDIN_FUNC(isday)
  12431. {
  12432. script_pushint(st,(night_flag == 0));
  12433. return SCRIPT_CMD_SUCCESS;
  12434. }
  12435. /*================================================
  12436. * Check how many items/cards in the list are
  12437. * equipped - used for 2/15's cards patch [celest]
  12438. *------------------------------------------------*/
  12439. BUILDIN_FUNC(isequippedcnt)
  12440. {
  12441. TBL_PC *sd;
  12442. int i, id = 1;
  12443. int ret = 0;
  12444. sd = script_rid2sd(st);
  12445. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12446. script_pushint(st,0);
  12447. return 0;
  12448. }
  12449. for (i=0; id!=0; i++) {
  12450. short j;
  12451. FETCH (i+2, id) else id = 0;
  12452. if (id <= 0)
  12453. continue;
  12454. for (j=0; j<EQI_MAX; j++) {
  12455. short index = sd->equip_index[j];
  12456. if(index < 0)
  12457. continue;
  12458. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12459. continue;
  12460. if(!sd->inventory_data[index])
  12461. continue;
  12462. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12463. if (sd->inventory_data[index]->nameid == id)
  12464. ret+= sd->status.inventory[index].amount;
  12465. } else { //Count cards.
  12466. short k;
  12467. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12468. continue; //No cards
  12469. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12470. if (sd->status.inventory[index].card[k] == id)
  12471. ret++; //[Lupus]
  12472. }
  12473. }
  12474. }
  12475. }
  12476. script_pushint(st,ret);
  12477. return SCRIPT_CMD_SUCCESS;
  12478. }
  12479. /*================================================
  12480. * Check whether another card has been
  12481. * equipped - used for 2/15's cards patch [celest]
  12482. * -- Items checked cannot be reused in another
  12483. * card set to prevent exploits
  12484. *------------------------------------------------*/
  12485. BUILDIN_FUNC(isequipped)
  12486. {
  12487. TBL_PC *sd;
  12488. int i, id = 1;
  12489. int ret = -1;
  12490. //Original hash to reverse it when full check fails.
  12491. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12492. sd = script_rid2sd(st);
  12493. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12494. script_pushint(st,0);
  12495. return SCRIPT_CMD_SUCCESS;
  12496. }
  12497. setitem_hash = sd->bonus.setitem_hash;
  12498. setitem_hash2 = sd->bonus.setitem_hash2;
  12499. for (i=0; id!=0; i++) {
  12500. int flag = 0;
  12501. short j;
  12502. FETCH (i+2, id) else id = 0;
  12503. if (id <= 0)
  12504. continue;
  12505. for (j=0; j<EQI_MAX; j++) {
  12506. short index = sd->equip_index[j];
  12507. if(index < 0)
  12508. continue;
  12509. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  12510. continue;
  12511. if(!sd->inventory_data[index])
  12512. continue;
  12513. if (itemdb_type(id) != IT_CARD) {
  12514. if (sd->inventory_data[index]->nameid != id)
  12515. continue;
  12516. flag = 1;
  12517. break;
  12518. } else { //Cards
  12519. short k;
  12520. if (sd->inventory_data[index]->slot == 0 ||
  12521. itemdb_isspecial(sd->status.inventory[index].card[0]))
  12522. continue;
  12523. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  12524. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  12525. unsigned int hash = 0;
  12526. if (sd->status.inventory[index].card[k] != id)
  12527. continue;
  12528. hash = 1<<((j<5?j:j-5)*4 + k);
  12529. // check if card is already used by another set
  12530. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  12531. continue;
  12532. // We have found a match
  12533. flag = 1;
  12534. // Set hash so this card cannot be used by another
  12535. if (j<5)
  12536. sd->bonus.setitem_hash |= hash;
  12537. else
  12538. sd->bonus.setitem_hash2 |= hash;
  12539. break;
  12540. }
  12541. }
  12542. if (flag) break; //Card found
  12543. }
  12544. if (ret == -1)
  12545. ret = flag;
  12546. else
  12547. ret &= flag;
  12548. if (!ret) break;
  12549. }
  12550. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  12551. sd->bonus.setitem_hash = setitem_hash;
  12552. sd->bonus.setitem_hash2 = setitem_hash2;
  12553. }
  12554. script_pushint(st,ret);
  12555. return SCRIPT_CMD_SUCCESS;
  12556. }
  12557. /*================================================
  12558. * Check how many given inserted cards in the CURRENT
  12559. * weapon - used for 2/15's cards patch [Lupus]
  12560. *------------------------------------------------*/
  12561. BUILDIN_FUNC(cardscnt)
  12562. {
  12563. TBL_PC *sd;
  12564. int i, k, id = 1;
  12565. int ret = 0;
  12566. int index;
  12567. sd = script_rid2sd(st);
  12568. for (i=0; id!=0; i++) {
  12569. FETCH (i+2, id) else id = 0;
  12570. if (id <= 0)
  12571. continue;
  12572. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  12573. if(index < 0) continue;
  12574. if(!sd->inventory_data[index])
  12575. continue;
  12576. if(itemdb_type(id) != IT_CARD) {
  12577. if (sd->inventory_data[index]->nameid == id)
  12578. ret+= sd->status.inventory[index].amount;
  12579. } else {
  12580. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12581. continue;
  12582. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12583. if (sd->status.inventory[index].card[k] == id)
  12584. ret++;
  12585. }
  12586. }
  12587. }
  12588. script_pushint(st,ret);
  12589. // script_pushint(st,current_equip_item_index);
  12590. return SCRIPT_CMD_SUCCESS;
  12591. }
  12592. /*=======================================================
  12593. * Returns the refined number of the current item, or an
  12594. * item with inventory index specified
  12595. *-------------------------------------------------------*/
  12596. BUILDIN_FUNC(getrefine)
  12597. {
  12598. TBL_PC *sd;
  12599. if ((sd = script_rid2sd(st))!= NULL)
  12600. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  12601. else
  12602. script_pushint(st,0);
  12603. return SCRIPT_CMD_SUCCESS;
  12604. }
  12605. /*=======================================================
  12606. * Day/Night controls
  12607. *-------------------------------------------------------*/
  12608. BUILDIN_FUNC(night)
  12609. {
  12610. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  12611. return SCRIPT_CMD_SUCCESS;
  12612. }
  12613. BUILDIN_FUNC(day)
  12614. {
  12615. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  12616. return SCRIPT_CMD_SUCCESS;
  12617. }
  12618. //=======================================================
  12619. // Unequip [Spectre]
  12620. //-------------------------------------------------------
  12621. BUILDIN_FUNC(unequip) {
  12622. int pos;
  12623. TBL_PC *sd;
  12624. if (!(sd = script_rid2sd(st)))
  12625. return SCRIPT_CMD_SUCCESS;
  12626. pos = script_getnum(st,2);
  12627. if (pos >= 1 && pos <= ARRAYLENGTH(equip)) {
  12628. short i = pc_checkequip(sd,equip[pos-1]);
  12629. if (i >= 0) {
  12630. pc_unequipitem(sd,i,1|2);
  12631. script_pushint(st, 1);
  12632. return SCRIPT_CMD_SUCCESS;
  12633. }
  12634. }
  12635. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  12636. script_pushint(st, 0);
  12637. return SCRIPT_CMD_FAILURE;
  12638. }
  12639. BUILDIN_FUNC(equip) {
  12640. unsigned short nameid = 0;
  12641. TBL_PC *sd;
  12642. struct item_data *item_data;
  12643. if (!(sd = script_rid2sd(st)))
  12644. return SCRIPT_CMD_SUCCESS;
  12645. nameid = script_getnum(st,2);
  12646. if ((item_data = itemdb_exists(nameid))) {
  12647. int i;
  12648. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  12649. if (i < MAX_INVENTORY) {
  12650. pc_equipitem(sd,i,item_data->equip);
  12651. script_pushint(st,1);
  12652. return SCRIPT_CMD_SUCCESS;
  12653. }
  12654. }
  12655. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  12656. script_pushint(st,0);
  12657. return SCRIPT_CMD_FAILURE;
  12658. }
  12659. BUILDIN_FUNC(autoequip)
  12660. {
  12661. unsigned short nameid;
  12662. int flag;
  12663. struct item_data *item_data;
  12664. nameid=script_getnum(st,2);
  12665. flag=script_getnum(st,3);
  12666. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  12667. {
  12668. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  12669. return SCRIPT_CMD_FAILURE;
  12670. }
  12671. if( !itemdb_isequip2(item_data) )
  12672. {
  12673. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  12674. return SCRIPT_CMD_FAILURE;
  12675. }
  12676. item_data->flag.autoequip = flag>0?1:0;
  12677. return SCRIPT_CMD_SUCCESS;
  12678. }
  12679. BUILDIN_FUNC(setbattleflag)
  12680. {
  12681. const char *flag, *value;
  12682. flag = script_getstr(st,2);
  12683. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  12684. if (battle_set_value(flag, value) == 0)
  12685. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  12686. else
  12687. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  12688. return SCRIPT_CMD_SUCCESS;
  12689. }
  12690. BUILDIN_FUNC(getbattleflag)
  12691. {
  12692. const char *flag;
  12693. flag = script_getstr(st,2);
  12694. script_pushint(st,battle_get_value(flag));
  12695. return SCRIPT_CMD_SUCCESS;
  12696. }
  12697. //=======================================================
  12698. // strlen [Valaris]
  12699. //-------------------------------------------------------
  12700. BUILDIN_FUNC(getstrlen)
  12701. {
  12702. const char *str = script_getstr(st,2);
  12703. int len = (str) ? (int)strlen(str) : 0;
  12704. script_pushint(st,len);
  12705. return SCRIPT_CMD_SUCCESS;
  12706. }
  12707. //=======================================================
  12708. // isalpha [Valaris]
  12709. //-------------------------------------------------------
  12710. BUILDIN_FUNC(charisalpha)
  12711. {
  12712. const char *str=script_getstr(st,2);
  12713. int pos=script_getnum(st,3);
  12714. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  12715. script_pushint(st,val);
  12716. return SCRIPT_CMD_SUCCESS;
  12717. }
  12718. //=======================================================
  12719. // charisupper <str>, <index>
  12720. //-------------------------------------------------------
  12721. BUILDIN_FUNC(charisupper)
  12722. {
  12723. const char *str = script_getstr(st,2);
  12724. int pos = script_getnum(st,3);
  12725. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  12726. script_pushint(st,val);
  12727. return SCRIPT_CMD_SUCCESS;
  12728. }
  12729. //=======================================================
  12730. // charislower <str>, <index>
  12731. //-------------------------------------------------------
  12732. BUILDIN_FUNC(charislower)
  12733. {
  12734. const char *str = script_getstr(st,2);
  12735. int pos = script_getnum(st,3);
  12736. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  12737. script_pushint(st,val);
  12738. return SCRIPT_CMD_SUCCESS;
  12739. }
  12740. //=======================================================
  12741. // charat <str>, <index>
  12742. //-------------------------------------------------------
  12743. BUILDIN_FUNC(charat) {
  12744. const char *str = script_getstr(st,2);
  12745. int pos = script_getnum(st,3);
  12746. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  12747. char output[2];
  12748. output[0] = str[pos];
  12749. output[1] = '\0';
  12750. script_pushstrcopy(st, output);
  12751. } else
  12752. script_pushconststr(st, "");
  12753. return SCRIPT_CMD_SUCCESS;
  12754. }
  12755. //=======================================================
  12756. // setchar <string>, <char>, <index>
  12757. //-------------------------------------------------------
  12758. BUILDIN_FUNC(setchar)
  12759. {
  12760. const char *str = script_getstr(st,2);
  12761. const char *c = script_getstr(st,3);
  12762. int index = script_getnum(st,4);
  12763. char *output = aStrdup(str);
  12764. if(index >= 0 && index < strlen(output))
  12765. output[index] = *c;
  12766. script_pushstr(st, output);
  12767. return SCRIPT_CMD_SUCCESS;
  12768. }
  12769. //=======================================================
  12770. // insertchar <string>, <char>, <index>
  12771. //-------------------------------------------------------
  12772. BUILDIN_FUNC(insertchar)
  12773. {
  12774. const char *str = script_getstr(st,2);
  12775. const char *c = script_getstr(st,3);
  12776. int index = script_getnum(st,4);
  12777. char *output;
  12778. size_t len = strlen(str);
  12779. if(index < 0)
  12780. index = 0;
  12781. else if(index > len)
  12782. index = len;
  12783. output = (char*)aMalloc(len + 2);
  12784. memcpy(output, str, index);
  12785. output[index] = c[0];
  12786. memcpy(&output[index+1], &str[index], len - index);
  12787. output[len+1] = '\0';
  12788. script_pushstr(st, output);
  12789. return SCRIPT_CMD_SUCCESS;
  12790. }
  12791. //=======================================================
  12792. // delchar <string>, <index>
  12793. //-------------------------------------------------------
  12794. BUILDIN_FUNC(delchar)
  12795. {
  12796. const char *str = script_getstr(st,2);
  12797. int index = script_getnum(st,3);
  12798. char *output;
  12799. size_t len = strlen(str);
  12800. if(index < 0 || index > len) {
  12801. //return original
  12802. output = aStrdup(str);
  12803. script_pushstr(st, output);
  12804. return 0;
  12805. }
  12806. output = (char*)aMalloc(len);
  12807. memcpy(output, str, index);
  12808. memcpy(&output[index], &str[index+1], len - index);
  12809. script_pushstr(st, output);
  12810. return SCRIPT_CMD_SUCCESS;
  12811. }
  12812. //=======================================================
  12813. // strtoupper <str>
  12814. //-------------------------------------------------------
  12815. BUILDIN_FUNC(strtoupper)
  12816. {
  12817. const char *str = script_getstr(st,2);
  12818. char *output = aStrdup(str);
  12819. char *cursor = output;
  12820. while (*cursor != '\0') {
  12821. *cursor = TOUPPER(*cursor);
  12822. cursor++;
  12823. }
  12824. script_pushstr(st, output);
  12825. return SCRIPT_CMD_SUCCESS;
  12826. }
  12827. //=======================================================
  12828. // strtolower <str>
  12829. //-------------------------------------------------------
  12830. BUILDIN_FUNC(strtolower)
  12831. {
  12832. const char *str = script_getstr(st,2);
  12833. char *output = aStrdup(str);
  12834. char *cursor = output;
  12835. while (*cursor != '\0') {
  12836. *cursor = TOLOWER(*cursor);
  12837. cursor++;
  12838. }
  12839. script_pushstr(st, output);
  12840. return SCRIPT_CMD_SUCCESS;
  12841. }
  12842. //=======================================================
  12843. // substr <str>, <start>, <end>
  12844. //-------------------------------------------------------
  12845. BUILDIN_FUNC(substr)
  12846. {
  12847. const char *str = script_getstr(st,2);
  12848. char *output;
  12849. int start = script_getnum(st,3);
  12850. int end = script_getnum(st,4);
  12851. int len = 0;
  12852. if(start >= 0 && end < strlen(str) && start <= end) {
  12853. len = end - start + 1;
  12854. output = (char*)aMalloc(len + 1);
  12855. memcpy(output, &str[start], len);
  12856. } else
  12857. output = (char*)aMalloc(1);
  12858. output[len] = '\0';
  12859. script_pushstr(st, output);
  12860. return SCRIPT_CMD_SUCCESS;
  12861. }
  12862. //=======================================================
  12863. // explode <dest_string_array>, <str>, <delimiter>
  12864. // Note: delimiter is limited to 1 char
  12865. //-------------------------------------------------------
  12866. BUILDIN_FUNC(explode)
  12867. {
  12868. struct script_data* data = script_getdata(st, 2);
  12869. const char *str = script_getstr(st,3);
  12870. const char delimiter = script_getstr(st, 4)[0];
  12871. int32 id;
  12872. size_t len = strlen(str);
  12873. int i = 0, j = 0;
  12874. int start;
  12875. char *temp;
  12876. const char* name;
  12877. TBL_PC* sd = NULL;
  12878. temp = (char*)aMalloc(len + 1);
  12879. if( !data_isreference(data) ) {
  12880. ShowError("script:explode: not a variable\n");
  12881. script_reportdata(data);
  12882. st->state = END;
  12883. return 1;// not a variable
  12884. }
  12885. id = reference_getid(data);
  12886. start = reference_getindex(data);
  12887. name = reference_getname(data);
  12888. if( not_array_variable(*name) ) {
  12889. ShowError("script:explode: illegal scope\n");
  12890. script_reportdata(data);
  12891. st->state = END;
  12892. return 1;// not supported
  12893. }
  12894. if( !is_string_variable(name) ) {
  12895. ShowError("script:explode: not string array\n");
  12896. script_reportdata(data);
  12897. st->state = END;
  12898. return 1;// data type mismatch
  12899. }
  12900. if( not_server_variable(*name) ) {
  12901. sd = script_rid2sd(st);
  12902. if( sd == NULL )
  12903. return 0;// no player attached
  12904. }
  12905. while(str[i] != '\0') {
  12906. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  12907. temp[j] = '\0';
  12908. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  12909. j = 0;
  12910. ++i;
  12911. } else {
  12912. temp[j++] = str[i++];
  12913. }
  12914. }
  12915. //set last string
  12916. temp[j] = '\0';
  12917. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  12918. aFree(temp);
  12919. return SCRIPT_CMD_SUCCESS;
  12920. }
  12921. //=======================================================
  12922. // implode <string_array>
  12923. // implode <string_array>, <glue>
  12924. //-------------------------------------------------------
  12925. BUILDIN_FUNC(implode)
  12926. {
  12927. struct script_data* data = script_getdata(st, 2);
  12928. const char *name;
  12929. int32 glue_len = 0, array_size, id;
  12930. char *output;
  12931. if( !data_isreference(data) ) {
  12932. ShowError("script:implode: not a variable\n");
  12933. script_reportdata(data);
  12934. st->state = END;
  12935. return 1;// not a variable
  12936. }
  12937. id = reference_getid(data);
  12938. name = reference_getname(data);
  12939. if( not_array_variable(*name) ) {
  12940. ShowError("script:implode: illegal scope\n");
  12941. script_reportdata(data);
  12942. st->state = END;
  12943. return 1;// not supported
  12944. }
  12945. if( !is_string_variable(name) ) {
  12946. ShowError("script:implode: not string array\n");
  12947. script_reportdata(data);
  12948. st->state = END;
  12949. return 1;// data type mismatch
  12950. }
  12951. if( not_server_variable(*name) ) {
  12952. TBL_PC* sd = script_rid2sd(st);
  12953. if( sd == NULL )
  12954. return 0;// no player attached
  12955. }
  12956. //count chars
  12957. array_size = getarraysize(st, id, reference_getindex(data), is_string_variable(name), reference_getref(data)) - 1;
  12958. if(array_size == -1) { //empty array check (AmsTaff)
  12959. ShowWarning("script:implode: array length = 0\n");
  12960. output = (char*)aMalloc(sizeof(char)*5);
  12961. sprintf(output,"%s","NULL");
  12962. } else {
  12963. const char *glue = NULL, *temp;
  12964. size_t len = 0;
  12965. int i, k = 0;
  12966. for(i = 0; i <= array_size; ++i) {
  12967. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12968. len += strlen(temp);
  12969. script_removetop(st, -1, 0);
  12970. }
  12971. //allocate mem
  12972. if( script_hasdata(st,3) ) {
  12973. glue = script_getstr(st,3);
  12974. glue_len = strlen(glue);
  12975. len += glue_len * (array_size);
  12976. }
  12977. output = (char*)aMalloc(len + 1);
  12978. //build output
  12979. for(i = 0; i < array_size; ++i) {
  12980. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  12981. len = strlen(temp);
  12982. memcpy(&output[k], temp, len);
  12983. k += len;
  12984. if(glue_len != 0) {
  12985. memcpy(&output[k], glue, glue_len);
  12986. k += glue_len;
  12987. }
  12988. script_removetop(st, -1, 0);
  12989. }
  12990. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  12991. len = strlen(temp);
  12992. memcpy(&output[k], temp, len);
  12993. k += len;
  12994. script_removetop(st, -1, 0);
  12995. output[k] = '\0';
  12996. }
  12997. script_pushstr(st, output);
  12998. return SCRIPT_CMD_SUCCESS;
  12999. }
  13000. //=======================================================
  13001. // sprintf(<format>, ...);
  13002. // Implements C sprintf, except format %n. The resulting string is
  13003. // returned, instead of being saved in variable by reference.
  13004. //-------------------------------------------------------
  13005. BUILDIN_FUNC(sprintf)
  13006. {
  13007. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13008. const char* format;
  13009. char* p;
  13010. char* q;
  13011. char* buf = NULL;
  13012. char* buf2 = NULL;
  13013. struct script_data* data;
  13014. StringBuf final_buf;
  13015. // Fetch init data
  13016. format = script_getstr(st, 2);
  13017. argc = script_lastdata(st)-2;
  13018. len = strlen(format);
  13019. // Skip parsing, where no parsing is required.
  13020. if(len == 0) {
  13021. script_pushconststr(st,"");
  13022. return 0;
  13023. }
  13024. // Pessimistic alloc
  13025. CREATE(buf, char, len+1);
  13026. // Need not be parsed, just solve stuff like %%.
  13027. if(argc == 0) {
  13028. memcpy(buf,format,len+1);
  13029. script_pushstrcopy(st, buf);
  13030. aFree(buf);
  13031. return 0;
  13032. }
  13033. safestrncpy(buf, format, len+1);
  13034. // Issue sprintf for each parameter
  13035. StringBuf_Init(&final_buf);
  13036. q = buf;
  13037. while((p = strchr(q, '%')) != NULL) {
  13038. if(p != q) {
  13039. len = p - q + 1;
  13040. if(buf2_len < len) {
  13041. RECREATE(buf2, char, len);
  13042. buf2_len = len;
  13043. }
  13044. safestrncpy(buf2, q, len);
  13045. StringBuf_AppendStr(&final_buf, buf2);
  13046. q = p;
  13047. }
  13048. p = q + 1;
  13049. if(*p == '%') { // %%
  13050. StringBuf_AppendStr(&final_buf, "%");
  13051. q+=2;
  13052. continue;
  13053. }
  13054. if(*p == 'n') { // %n
  13055. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13056. script_reportsrc(st);
  13057. q+=2;
  13058. continue;
  13059. }
  13060. if(arg >= argc) {
  13061. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13062. if(buf) aFree(buf);
  13063. if(buf2) aFree(buf2);
  13064. StringBuf_Destroy(&final_buf);
  13065. script_pushconststr(st,"");
  13066. return 1;
  13067. }
  13068. if((p = strchr(q+1, '%')) == NULL)
  13069. p = strchr(q, 0); // EOS
  13070. len = p - q + 1;
  13071. if(buf2_len < len) {
  13072. RECREATE(buf2, char, len);
  13073. buf2_len = len;
  13074. }
  13075. safestrncpy(buf2, q, len);
  13076. q = p;
  13077. // Note: This assumes the passed value being the correct
  13078. // type to the current format specifier. If not, the server
  13079. // probably crashes or returns anything else, than expected,
  13080. // but it would behave in normal code the same way so it's
  13081. // the scripter's responsibility.
  13082. data = script_getdata(st, arg+3);
  13083. if(data_isstring(data)) // String
  13084. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13085. else if(data_isint(data)) // Number
  13086. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13087. else if(data_isreference(data)) { // Variable
  13088. char* name = reference_getname(data);
  13089. if(name[strlen(name)-1]=='$') // var Str
  13090. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13091. else // var Int
  13092. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13093. } else { // Unsupported type
  13094. ShowError("buildin_sprintf: Unknown argument type!\n");
  13095. if(buf) aFree(buf);
  13096. if(buf2) aFree(buf2);
  13097. StringBuf_Destroy(&final_buf);
  13098. script_pushconststr(st,"");
  13099. return 1;
  13100. }
  13101. arg++;
  13102. }
  13103. // Append anything left
  13104. if(*q)
  13105. StringBuf_AppendStr(&final_buf, q);
  13106. // Passed more, than needed
  13107. if(arg < argc) {
  13108. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13109. script_reportsrc(st);
  13110. }
  13111. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13112. if(buf) aFree(buf);
  13113. if(buf2) aFree(buf2);
  13114. StringBuf_Destroy(&final_buf);
  13115. return SCRIPT_CMD_SUCCESS;
  13116. }
  13117. //=======================================================
  13118. // sscanf(<str>, <format>, ...);
  13119. // Implements C sscanf.
  13120. //-------------------------------------------------------
  13121. BUILDIN_FUNC(sscanf){
  13122. unsigned int argc, arg = 0, len;
  13123. struct script_data* data;
  13124. struct map_session_data* sd = NULL;
  13125. const char* str;
  13126. const char* format;
  13127. const char* p;
  13128. const char* q;
  13129. char* buf = NULL;
  13130. char* buf_p;
  13131. char* ref_str = NULL;
  13132. int ref_int;
  13133. // Get data
  13134. str = script_getstr(st, 2);
  13135. format = script_getstr(st, 3);
  13136. argc = script_lastdata(st)-3;
  13137. len = strlen(format);
  13138. if (len != 0 && strlen(str) == 0) {
  13139. // If the source string is empty but the format string is not, we return -1
  13140. // according to the C specs. (if the format string is also empty, we shall
  13141. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13142. script_pushint(st, -1);
  13143. return SCRIPT_CMD_SUCCESS;
  13144. }
  13145. CREATE(buf, char, len*2+1);
  13146. // Issue sscanf for each parameter
  13147. *buf = 0;
  13148. q = format;
  13149. while((p = strchr(q, '%'))){
  13150. if(p!=q){
  13151. strncat(buf, q, (size_t)(p-q));
  13152. q = p;
  13153. }
  13154. p = q+1;
  13155. if(*p=='*' || *p=='%'){ // Skip
  13156. strncat(buf, q, 2);
  13157. q+=2;
  13158. continue;
  13159. }
  13160. if(arg>=argc){
  13161. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13162. script_pushint(st, -1);
  13163. if(buf) aFree(buf);
  13164. if(ref_str) aFree(ref_str);
  13165. return 1;
  13166. }
  13167. if((p = strchr(q+1, '%'))==NULL){
  13168. p = strchr(q, 0); // EOS
  13169. }
  13170. len = p-q;
  13171. strncat(buf, q, len);
  13172. q = p;
  13173. // Validate output
  13174. data = script_getdata(st, arg+4);
  13175. if(!data_isreference(data) || !reference_tovariable(data)){
  13176. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13177. script_pushint(st, -1);
  13178. if(buf) aFree(buf);
  13179. if(ref_str) aFree(ref_str);
  13180. return 1;
  13181. }
  13182. buf_p = reference_getname(data);
  13183. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13184. script_pushint(st, -1);
  13185. if(buf) aFree(buf);
  13186. if(ref_str) aFree(ref_str);
  13187. return 0;
  13188. }
  13189. // Save value if any
  13190. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13191. if(ref_str==NULL){
  13192. CREATE(ref_str, char, strlen(str)+1);
  13193. }
  13194. if(sscanf(str, buf, ref_str)==0){
  13195. break;
  13196. }
  13197. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13198. } else { // Number
  13199. if(sscanf(str, buf, &ref_int)==0){
  13200. break;
  13201. }
  13202. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13203. }
  13204. arg++;
  13205. // Disable used format (%... -> %*...)
  13206. buf_p = strchr(buf, 0);
  13207. memmove(buf_p-len+2, buf_p-len+1, len);
  13208. *(buf_p-len+1) = '*';
  13209. }
  13210. script_pushint(st, arg);
  13211. if(buf) aFree(buf);
  13212. if(ref_str) aFree(ref_str);
  13213. return SCRIPT_CMD_SUCCESS;
  13214. }
  13215. //=======================================================
  13216. // strpos(<haystack>, <needle>)
  13217. // strpos(<haystack>, <needle>, <offset>)
  13218. //
  13219. // Implements PHP style strpos. Adapted from code from
  13220. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13221. //-------------------------------------------------------
  13222. BUILDIN_FUNC(strpos) {
  13223. const char *haystack = script_getstr(st,2);
  13224. const char *needle = script_getstr(st,3);
  13225. int i;
  13226. size_t len;
  13227. if( script_hasdata(st,4) )
  13228. i = script_getnum(st,4);
  13229. else
  13230. i = 0;
  13231. if (needle[0] == '\0') {
  13232. script_pushint(st, -1);
  13233. return 0;
  13234. }
  13235. len = strlen(haystack);
  13236. for ( ; i < len; ++i ) {
  13237. if ( haystack[i] == *needle ) {
  13238. // matched starting char -- loop through remaining chars
  13239. const char *h, *n;
  13240. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13241. if ( *h != *n ) {
  13242. break;
  13243. }
  13244. }
  13245. if ( !*n ) { // matched all of 'needle' to null termination
  13246. script_pushint(st, i);
  13247. return 0;
  13248. }
  13249. }
  13250. }
  13251. script_pushint(st, -1);
  13252. return SCRIPT_CMD_SUCCESS;
  13253. }
  13254. //===============================================================
  13255. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13256. //
  13257. // Note: Finds all instances of <search> in <input> and replaces
  13258. // with <replace>. If specified will only replace as many
  13259. // instances as specified in <count>. By default will be case
  13260. // sensitive.
  13261. //---------------------------------------------------------------
  13262. BUILDIN_FUNC(replacestr)
  13263. {
  13264. const char *input = script_getstr(st, 2);
  13265. const char *find = script_getstr(st, 3);
  13266. const char *replace = script_getstr(st, 4);
  13267. size_t inputlen = strlen(input);
  13268. size_t findlen = strlen(find);
  13269. struct StringBuf output;
  13270. bool usecase = true;
  13271. int count = 0;
  13272. int numFinds = 0;
  13273. int i = 0, f = 0;
  13274. if(findlen == 0) {
  13275. ShowError("script:replacestr: Invalid search length.\n");
  13276. st->state = END;
  13277. return 1;
  13278. }
  13279. if(script_hasdata(st, 5)) {
  13280. struct script_data *data = script_getdata(st, 5);
  13281. get_val(st, data); // Convert into value in case of a variable
  13282. if( !data_isstring(data) )
  13283. usecase = script_getnum(st, 5) != 0;
  13284. else {
  13285. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13286. st->state = END;
  13287. return 1;
  13288. }
  13289. }
  13290. if(script_hasdata(st, 6)) {
  13291. count = script_getnum(st, 6);
  13292. if(count == 0) {
  13293. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13294. st->state = END;
  13295. return 1;
  13296. }
  13297. }
  13298. StringBuf_Init(&output);
  13299. for(; i < inputlen; i++) {
  13300. if(count && count == numFinds) { //found enough, stop looking
  13301. break;
  13302. }
  13303. for(f = 0; f <= findlen; f++) {
  13304. if(f == findlen) { //complete match
  13305. numFinds++;
  13306. StringBuf_AppendStr(&output, replace);
  13307. i += findlen - 1;
  13308. break;
  13309. } else {
  13310. if(usecase) {
  13311. if((i + f) > inputlen || input[i + f] != find[f]) {
  13312. StringBuf_Printf(&output, "%c", input[i]);
  13313. break;
  13314. }
  13315. } else {
  13316. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13317. StringBuf_Printf(&output, "%c", input[i]);
  13318. break;
  13319. }
  13320. }
  13321. }
  13322. }
  13323. }
  13324. //append excess after enough found
  13325. if(i < inputlen)
  13326. StringBuf_AppendStr(&output, &(input[i]));
  13327. script_pushstrcopy(st, StringBuf_Value(&output));
  13328. StringBuf_Destroy(&output);
  13329. return SCRIPT_CMD_SUCCESS;
  13330. }
  13331. //========================================================
  13332. // countstr <input>, <search>{, <usecase>}
  13333. //
  13334. // Note: Counts the number of times <search> occurs in
  13335. // <input>. By default will be case sensitive.
  13336. //--------------------------------------------------------
  13337. BUILDIN_FUNC(countstr)
  13338. {
  13339. const char *input = script_getstr(st, 2);
  13340. const char *find = script_getstr(st, 3);
  13341. size_t inputlen = strlen(input);
  13342. size_t findlen = strlen(find);
  13343. bool usecase = true;
  13344. int numFinds = 0;
  13345. int i = 0, f = 0;
  13346. if(findlen == 0) {
  13347. ShowError("script:countstr: Invalid search length.\n");
  13348. st->state = END;
  13349. return 1;
  13350. }
  13351. if(script_hasdata(st, 4)) {
  13352. struct script_data *data;
  13353. data = script_getdata(st, 4);
  13354. get_val(st, data); // Convert into value in case of a variable
  13355. if( !data_isstring(data) )
  13356. usecase = script_getnum(st, 4) != 0;
  13357. else {
  13358. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13359. st->state = END;
  13360. return 1;
  13361. }
  13362. }
  13363. for(; i < inputlen; i++) {
  13364. for(f = 0; f <= findlen; f++) {
  13365. if(f == findlen) { //complete match
  13366. numFinds++;
  13367. i += findlen - 1;
  13368. break;
  13369. } else {
  13370. if(usecase) {
  13371. if((i + f) > inputlen || input[i + f] != find[f]) {
  13372. break;
  13373. }
  13374. } else {
  13375. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13376. break;
  13377. }
  13378. }
  13379. }
  13380. }
  13381. }
  13382. script_pushint(st, numFinds);
  13383. return SCRIPT_CMD_SUCCESS;
  13384. }
  13385. /// Changes the display name and/or display class of the npc.
  13386. /// Returns 0 is successful, 1 if the npc does not exist.
  13387. ///
  13388. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13389. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13390. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13391. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13392. BUILDIN_FUNC(setnpcdisplay)
  13393. {
  13394. const char* name;
  13395. const char* newname = NULL;
  13396. int class_ = -1, size = -1;
  13397. struct script_data* data;
  13398. struct npc_data* nd;
  13399. name = script_getstr(st,2);
  13400. data = script_getdata(st,3);
  13401. if( script_hasdata(st,4) )
  13402. class_ = script_getnum(st,4);
  13403. if( script_hasdata(st,5) )
  13404. size = script_getnum(st,5);
  13405. get_val(st, data);
  13406. if( data_isstring(data) )
  13407. newname = conv_str(st,data);
  13408. else if( data_isint(data) )
  13409. class_ = conv_num(st,data);
  13410. else
  13411. {
  13412. ShowError("script:setnpcdisplay: expected string or number\n");
  13413. script_reportdata(data);
  13414. return 1;
  13415. }
  13416. nd = npc_name2id(name);
  13417. if( nd == NULL )
  13418. {// not found
  13419. script_pushint(st,1);
  13420. return 0;
  13421. }
  13422. // update npc
  13423. if( newname )
  13424. npc_setdisplayname(nd, newname);
  13425. if( size != -1 && size != (int)nd->size )
  13426. nd->size = size;
  13427. else
  13428. size = -1;
  13429. if( class_ != -1 && nd->class_ != class_ )
  13430. npc_setclass(nd, class_);
  13431. else if( size != -1 )
  13432. { // Required to update the visual size
  13433. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13434. clif_spawn(&nd->bl);
  13435. }
  13436. script_pushint(st,0);
  13437. return SCRIPT_CMD_SUCCESS;
  13438. }
  13439. BUILDIN_FUNC(atoi)
  13440. {
  13441. const char *value;
  13442. value = script_getstr(st,2);
  13443. script_pushint(st,atoi(value));
  13444. return SCRIPT_CMD_SUCCESS;
  13445. }
  13446. BUILDIN_FUNC(axtoi)
  13447. {
  13448. const char *hex = script_getstr(st,2);
  13449. long value = strtol(hex, NULL, 16);
  13450. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13451. value = cap_value(value, INT_MIN, INT_MAX);
  13452. #endif
  13453. script_pushint(st, (int)value);
  13454. return SCRIPT_CMD_SUCCESS;
  13455. }
  13456. BUILDIN_FUNC(strtol)
  13457. {
  13458. const char *string = script_getstr(st, 2);
  13459. int base = script_getnum(st, 3);
  13460. long value = strtol(string, NULL, base);
  13461. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13462. value = cap_value(value, INT_MIN, INT_MAX);
  13463. #endif
  13464. script_pushint(st, (int)value);
  13465. return SCRIPT_CMD_SUCCESS;
  13466. }
  13467. // case-insensitive substring search [lordalfa]
  13468. BUILDIN_FUNC(compare)
  13469. {
  13470. const char *message;
  13471. const char *cmpstring;
  13472. message = script_getstr(st,2);
  13473. cmpstring = script_getstr(st,3);
  13474. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13475. return SCRIPT_CMD_SUCCESS;
  13476. }
  13477. BUILDIN_FUNC(strcmp)
  13478. {
  13479. const char *str1;
  13480. const char *str2;
  13481. str1 = script_getstr(st,2);
  13482. str2 = script_getstr(st,3);
  13483. script_pushint(st,strcmp(str1, str2));
  13484. return SCRIPT_CMD_SUCCESS;
  13485. }
  13486. // [zBuffer] List of mathematics commands --->
  13487. BUILDIN_FUNC(sqrt)
  13488. {
  13489. double i, a;
  13490. i = script_getnum(st,2);
  13491. a = sqrt(i);
  13492. script_pushint(st,(int)a);
  13493. return SCRIPT_CMD_SUCCESS;
  13494. }
  13495. BUILDIN_FUNC(pow)
  13496. {
  13497. double i, a, b;
  13498. a = script_getnum(st,2);
  13499. b = script_getnum(st,3);
  13500. i = pow(a,b);
  13501. script_pushint(st,(int)i);
  13502. return SCRIPT_CMD_SUCCESS;
  13503. }
  13504. BUILDIN_FUNC(distance)
  13505. {
  13506. int x0, y0, x1, y1;
  13507. x0 = script_getnum(st,2);
  13508. y0 = script_getnum(st,3);
  13509. x1 = script_getnum(st,4);
  13510. y1 = script_getnum(st,5);
  13511. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13512. return SCRIPT_CMD_SUCCESS;
  13513. }
  13514. // <--- [zBuffer] List of mathematics commands
  13515. BUILDIN_FUNC(md5)
  13516. {
  13517. const char *tmpstr;
  13518. char *md5str;
  13519. tmpstr = script_getstr(st,2);
  13520. md5str = (char *)aMalloc((32+1)*sizeof(char));
  13521. MD5_String(tmpstr, md5str);
  13522. script_pushstr(st, md5str);
  13523. return SCRIPT_CMD_SUCCESS;
  13524. }
  13525. // [zBuffer] List of dynamic var commands --->
  13526. BUILDIN_FUNC(setd)
  13527. {
  13528. TBL_PC *sd=NULL;
  13529. char varname[100];
  13530. const char *buffer;
  13531. int elem;
  13532. buffer = script_getstr(st, 2);
  13533. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13534. elem = 0;
  13535. if( not_server_variable(*varname) )
  13536. {
  13537. sd = script_rid2sd(st);
  13538. if( sd == NULL )
  13539. {
  13540. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  13541. return 0;
  13542. }
  13543. }
  13544. if( is_string_variable(varname) ) {
  13545. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  13546. } else {
  13547. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  13548. }
  13549. return SCRIPT_CMD_SUCCESS;
  13550. }
  13551. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  13552. {
  13553. int i, j;
  13554. TBL_PC* sd = NULL;
  13555. const char* query;
  13556. struct script_data* data;
  13557. const char* name;
  13558. int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  13559. int num_vars;
  13560. int num_cols;
  13561. // check target variables
  13562. for( i = 3; script_hasdata(st,i); ++i ) {
  13563. data = script_getdata(st, i);
  13564. if( data_isreference(data) ) { // it's a variable
  13565. name = reference_getname(data);
  13566. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  13567. sd = script_rid2sd(st);
  13568. if( sd == NULL ) { // no player attached
  13569. script_reportdata(data);
  13570. st->state = END;
  13571. return 1;
  13572. }
  13573. }
  13574. if( not_array_variable(*name) )
  13575. max_rows = 1;// not an array, limit to one row
  13576. } else {
  13577. ShowError("script:query_sql: not a variable\n");
  13578. script_reportdata(data);
  13579. st->state = END;
  13580. return 1;
  13581. }
  13582. }
  13583. num_vars = i - 3;
  13584. // Execute the query
  13585. query = script_getstr(st,2);
  13586. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  13587. Sql_ShowDebug(handle);
  13588. script_pushint(st, -1);
  13589. return 1;
  13590. }
  13591. if( Sql_NumRows(handle) == 0 ) { // No data received
  13592. Sql_FreeResult(handle);
  13593. script_pushint(st, -1);
  13594. return 0;
  13595. }
  13596. // Count the number of columns to store
  13597. num_cols = Sql_NumColumns(handle);
  13598. if( num_vars < num_cols ) {
  13599. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  13600. script_reportsrc(st);
  13601. } else if( num_vars > num_cols ) {
  13602. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  13603. script_reportsrc(st);
  13604. }
  13605. // Store data
  13606. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  13607. for( j = 0; j < num_vars; ++j ) {
  13608. char* str = NULL;
  13609. if( j < num_cols )
  13610. Sql_GetData(handle, j, &str, NULL);
  13611. data = script_getdata(st, j+3);
  13612. name = reference_getname(data);
  13613. if( is_string_variable(name) )
  13614. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  13615. else
  13616. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  13617. }
  13618. }
  13619. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  13620. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  13621. script_reportsrc(st);
  13622. }
  13623. // Free data
  13624. Sql_FreeResult(handle);
  13625. script_pushint(st, i);
  13626. return SCRIPT_CMD_SUCCESS;
  13627. }
  13628. BUILDIN_FUNC(query_sql) {
  13629. #ifdef BETA_THREAD_TEST
  13630. if( st->state != RERUNLINE ) {
  13631. queryThread_add(st,false);
  13632. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13633. } else
  13634. st->state = RUN;
  13635. return 0;
  13636. #else
  13637. return buildin_query_sql_sub(st, qsmysql_handle);
  13638. #endif
  13639. }
  13640. BUILDIN_FUNC(query_logsql) {
  13641. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  13642. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  13643. script_pushint(st,-1);
  13644. return 1;
  13645. }
  13646. #ifdef BETA_THREAD_TEST
  13647. if( st->state != RERUNLINE ) {
  13648. queryThread_add(st,true);
  13649. st->state = RERUNLINE;/* will continue when the query is finished running. */
  13650. } else
  13651. st->state = RUN;
  13652. return 0;
  13653. #else
  13654. return buildin_query_sql_sub(st, logmysql_handle);
  13655. #endif
  13656. }
  13657. //Allows escaping of a given string.
  13658. BUILDIN_FUNC(escape_sql)
  13659. {
  13660. const char *str;
  13661. char *esc_str;
  13662. size_t len;
  13663. str = script_getstr(st,2);
  13664. len = strlen(str);
  13665. esc_str = (char*)aMalloc(len*2+1);
  13666. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  13667. script_pushstr(st, esc_str);
  13668. return SCRIPT_CMD_SUCCESS;
  13669. }
  13670. BUILDIN_FUNC(getd)
  13671. {
  13672. char varname[100];
  13673. const char *buffer;
  13674. int elem;
  13675. buffer = script_getstr(st, 2);
  13676. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  13677. elem = 0;
  13678. // Push the 'pointer' so it's more flexible [Lance]
  13679. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  13680. return SCRIPT_CMD_SUCCESS;
  13681. }
  13682. BUILDIN_FUNC(callshop)
  13683. {
  13684. TBL_PC *sd = NULL;
  13685. struct npc_data *nd;
  13686. const char *shopname;
  13687. int flag = 0;
  13688. sd = script_rid2sd(st);
  13689. if (!sd) {
  13690. script_pushint(st,0);
  13691. return 0;
  13692. }
  13693. shopname = script_getstr(st, 2);
  13694. if( script_hasdata(st,3) )
  13695. flag = script_getnum(st,3);
  13696. nd = npc_name2id(shopname);
  13697. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP) )
  13698. {
  13699. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  13700. script_pushint(st,0);
  13701. return 1;
  13702. }
  13703. if( nd->subtype == NPCTYPE_SHOP || nd->subtype == NPCTYPE_ITEMSHOP || nd->subtype == NPCTYPE_POINTSHOP )
  13704. {
  13705. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  13706. sd->state.callshop = 1;
  13707. switch( flag )
  13708. {
  13709. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  13710. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  13711. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  13712. }
  13713. }
  13714. else
  13715. clif_cashshop_show(sd, nd);
  13716. sd->npc_shopid = nd->bl.id;
  13717. script_pushint(st,1);
  13718. return SCRIPT_CMD_SUCCESS;
  13719. }
  13720. BUILDIN_FUNC(npcshopitem)
  13721. {
  13722. const char* npcname = script_getstr(st, 2);
  13723. struct npc_data* nd = npc_name2id(npcname);
  13724. int n, i;
  13725. int amount;
  13726. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP ) )
  13727. { //Not found.
  13728. script_pushint(st,0);
  13729. return 0;
  13730. }
  13731. // get the count of new entries
  13732. amount = (script_lastdata(st)-2)/2;
  13733. // generate new shop item list
  13734. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  13735. for( n = 0, i = 3; n < amount; n++, i+=2 )
  13736. {
  13737. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13738. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13739. }
  13740. nd->u.shop.count = n;
  13741. script_pushint(st,1);
  13742. return SCRIPT_CMD_SUCCESS;
  13743. }
  13744. BUILDIN_FUNC(npcshopadditem)
  13745. {
  13746. const char* npcname = script_getstr(st,2);
  13747. struct npc_data* nd = npc_name2id(npcname);
  13748. int n, i;
  13749. int amount;
  13750. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP ) )
  13751. { //Not found.
  13752. script_pushint(st,0);
  13753. return 0;
  13754. }
  13755. // get the count of new entries
  13756. amount = (script_lastdata(st)-2)/2;
  13757. // append new items to existing shop item list
  13758. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  13759. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  13760. {
  13761. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  13762. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  13763. }
  13764. nd->u.shop.count = n;
  13765. script_pushint(st,1);
  13766. return SCRIPT_CMD_SUCCESS;
  13767. }
  13768. BUILDIN_FUNC(npcshopdelitem)
  13769. {
  13770. const char* npcname = script_getstr(st,2);
  13771. struct npc_data* nd = npc_name2id(npcname);
  13772. int n, i;
  13773. int amount;
  13774. int size;
  13775. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP ) ) { // Not found.
  13776. script_pushint(st,0);
  13777. return 0;
  13778. }
  13779. amount = script_lastdata(st)-2;
  13780. size = nd->u.shop.count;
  13781. // remove specified items from the shop item list
  13782. for( i = 3; i < 3 + amount; i++ ) {
  13783. unsigned short nameid = script_getnum(st,i);
  13784. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  13785. if( n < size ) {
  13786. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  13787. size--;
  13788. }
  13789. }
  13790. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  13791. nd->u.shop.count = size;
  13792. script_pushint(st,1);
  13793. return SCRIPT_CMD_SUCCESS;
  13794. }
  13795. //Sets a script to attach to a shop npc.
  13796. BUILDIN_FUNC(npcshopattach)
  13797. {
  13798. const char* npcname = script_getstr(st,2);
  13799. struct npc_data* nd = npc_name2id(npcname);
  13800. int flag = 1;
  13801. if( script_hasdata(st,3) )
  13802. flag = script_getnum(st,3);
  13803. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP ) )
  13804. { //Not found.
  13805. script_pushint(st,0);
  13806. return 0;
  13807. }
  13808. if (flag)
  13809. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  13810. else
  13811. nd->master_nd = NULL;
  13812. script_pushint(st,1);
  13813. return SCRIPT_CMD_SUCCESS;
  13814. }
  13815. /*==========================================
  13816. * Returns some values of an item [Lupus]
  13817. * Price, Weight, etc...
  13818. setitemscript(itemID,"{new item bonus script}",[n]);
  13819. Where n:
  13820. 0 - script
  13821. 1 - Equip script
  13822. 2 - Unequip script
  13823. *------------------------------------------*/
  13824. BUILDIN_FUNC(setitemscript)
  13825. {
  13826. unsigned short item_id;
  13827. int n = 0;
  13828. const char *script;
  13829. struct item_data *i_data;
  13830. struct script_code **dstscript;
  13831. item_id = script_getnum(st,2);
  13832. script = script_getstr(st,3);
  13833. if( script_hasdata(st,4) )
  13834. n=script_getnum(st,4);
  13835. i_data = itemdb_exists(item_id);
  13836. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  13837. script_pushint(st,0);
  13838. return 0;
  13839. }
  13840. switch (n) {
  13841. case 2:
  13842. dstscript = &i_data->unequip_script;
  13843. break;
  13844. case 1:
  13845. dstscript = &i_data->equip_script;
  13846. break;
  13847. default:
  13848. dstscript = &i_data->script;
  13849. break;
  13850. }
  13851. if(*dstscript)
  13852. script_free_code(*dstscript);
  13853. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  13854. script_pushint(st,1);
  13855. return SCRIPT_CMD_SUCCESS;
  13856. }
  13857. /*=======================================================
  13858. * Add or Update a mob drop temporarily [Akinari]
  13859. * Original Idea By: [Lupus]
  13860. *
  13861. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  13862. *
  13863. * If given an item the mob already drops, the rate
  13864. * is updated to the new rate. Rate cannot exceed 10000
  13865. * Returns 1 if succeeded (added/updated a mob drop)
  13866. *-------------------------------------------------------*/
  13867. BUILDIN_FUNC(addmonsterdrop)
  13868. {
  13869. struct mob_db *mob;
  13870. struct script_data *data;
  13871. unsigned short item_id;
  13872. int rate;
  13873. data = script_getdata(st, 2);
  13874. get_val(st, data); // Convert into value in case of a variable
  13875. if(data_isstring(data))
  13876. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13877. else
  13878. mob = mob_db(script_getnum(st,2));
  13879. item_id=script_getnum(st,3);
  13880. rate=script_getnum(st,4);
  13881. if(!itemdb_exists(item_id)){
  13882. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  13883. return 1;
  13884. }
  13885. if(mob) { //We got a valid monster, check for available drop slot
  13886. unsigned char i, c = 0;
  13887. for(i = 0; i < MAX_MOB_DROP; i++) {
  13888. if(mob->dropitem[i].nameid) {
  13889. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  13890. c = i;
  13891. break;
  13892. }
  13893. continue;
  13894. }
  13895. if(!c) //Accept first available slot only
  13896. c = i;
  13897. }
  13898. if(c) { //Fill in the slot with the item and rate
  13899. mob->dropitem[c].nameid = item_id;
  13900. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  13901. script_pushint(st,1);
  13902. } else //No place to put the new drop
  13903. script_pushint(st,0);
  13904. } else {
  13905. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13906. return 1;
  13907. }
  13908. return SCRIPT_CMD_SUCCESS;
  13909. }
  13910. /*=======================================================
  13911. * Delete a mob drop temporarily [Akinari]
  13912. * Original Idea By: [Lupus]
  13913. *
  13914. * delmonsterdrop <mob_id or name>,<item_id>;
  13915. *
  13916. * Returns 1 if succeeded (deleted a mob drop)
  13917. *-------------------------------------------------------*/
  13918. BUILDIN_FUNC(delmonsterdrop)
  13919. {
  13920. struct mob_db *mob;
  13921. struct script_data *data;
  13922. unsigned short item_id;
  13923. data = script_getdata(st, 2);
  13924. get_val(st, data); // Convert into value in case of a variable
  13925. if(data_isstring(data))
  13926. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  13927. else
  13928. mob = mob_db(script_getnum(st,2));
  13929. item_id=script_getnum(st,3);
  13930. if(!itemdb_exists(item_id)){
  13931. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  13932. return 1;
  13933. }
  13934. if(mob) { //We got a valid monster, check for item drop on monster
  13935. unsigned char i;
  13936. for(i = 0; i < MAX_MOB_DROP; i++) {
  13937. if(mob->dropitem[i].nameid == item_id) {
  13938. mob->dropitem[i].nameid = 0;
  13939. mob->dropitem[i].p = 0;
  13940. script_pushint(st,1);
  13941. return 0;
  13942. }
  13943. }
  13944. //No drop on that monster
  13945. script_pushint(st,0);
  13946. } else {
  13947. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  13948. return 1;
  13949. }
  13950. return SCRIPT_CMD_SUCCESS;
  13951. }
  13952. /*==========================================
  13953. * Returns some values of a monster [Lupus]
  13954. * Name, Level, race, size, etc...
  13955. getmonsterinfo(monsterID,queryIndex);
  13956. *------------------------------------------*/
  13957. BUILDIN_FUNC(getmonsterinfo)
  13958. {
  13959. struct mob_db *mob;
  13960. int mob_id;
  13961. mob_id = script_getnum(st,2);
  13962. if (!mobdb_checkid(mob_id)) {
  13963. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  13964. if ( !script_getnum(st,3) ) //requested a string
  13965. script_pushconststr(st,"null");
  13966. else
  13967. script_pushint(st,-1);
  13968. return 0;
  13969. }
  13970. mob = mob_db(mob_id);
  13971. switch ( script_getnum(st,3) ) {
  13972. case 0: script_pushstrcopy(st,mob->jname); break;
  13973. case 1: script_pushint(st,mob->lv); break;
  13974. case 2: script_pushint(st,mob->status.max_hp); break;
  13975. case 3: script_pushint(st,mob->base_exp); break;
  13976. case 4: script_pushint(st,mob->job_exp); break;
  13977. case 5: script_pushint(st,mob->status.rhw.atk); break;
  13978. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  13979. case 7: script_pushint(st,mob->status.def); break;
  13980. case 8: script_pushint(st,mob->status.mdef); break;
  13981. case 9: script_pushint(st,mob->status.str); break;
  13982. case 10: script_pushint(st,mob->status.agi); break;
  13983. case 11: script_pushint(st,mob->status.vit); break;
  13984. case 12: script_pushint(st,mob->status.int_); break;
  13985. case 13: script_pushint(st,mob->status.dex); break;
  13986. case 14: script_pushint(st,mob->status.luk); break;
  13987. case 15: script_pushint(st,mob->status.rhw.range); break;
  13988. case 16: script_pushint(st,mob->range2); break;
  13989. case 17: script_pushint(st,mob->range3); break;
  13990. case 18: script_pushint(st,mob->status.size); break;
  13991. case 19: script_pushint(st,mob->status.race); break;
  13992. case 20: script_pushint(st,mob->status.def_ele); break;
  13993. case 21: script_pushint(st,mob->status.mode); break;
  13994. case 22: script_pushint(st,mob->mexp); break;
  13995. default: script_pushint(st,-1); //wrong Index
  13996. }
  13997. return SCRIPT_CMD_SUCCESS;
  13998. }
  13999. /**
  14000. * Check player's vending/buyingstore/autotrade state
  14001. * checkvending({"<Player Name>"})
  14002. * @author [Nab4]
  14003. */
  14004. BUILDIN_FUNC(checkvending) {
  14005. TBL_PC *sd = NULL;
  14006. if (script_hasdata(st,2)) {
  14007. if (!(sd = map_nick2sd(script_getstr(st,2)))) {
  14008. ShowError("buildin_checkvending: Player '%s' is not online!\n", script_getstr(st,2));
  14009. return SCRIPT_CMD_FAILURE;
  14010. }
  14011. }
  14012. else
  14013. sd = script_rid2sd(st);
  14014. if (!sd) {
  14015. script_pushint(st,0);
  14016. return SCRIPT_CMD_SUCCESS;
  14017. }
  14018. else {
  14019. int8 ret = 0;
  14020. if (sd->state.vending)
  14021. ret = 1;
  14022. else if (sd->state.buyingstore)
  14023. ret = 4;
  14024. if (sd->state.autotrade)
  14025. ret |= 2;
  14026. script_pushint(st, ret);
  14027. }
  14028. return SCRIPT_CMD_SUCCESS;
  14029. }
  14030. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14031. {
  14032. TBL_PC *sd = NULL;
  14033. if(script_hasdata(st,2))
  14034. sd = map_nick2sd(script_getstr(st,2));
  14035. else
  14036. sd = script_rid2sd(st);
  14037. if(sd)
  14038. script_pushint(st,(sd->chatID != 0));
  14039. else
  14040. script_pushint(st,0);
  14041. return SCRIPT_CMD_SUCCESS;
  14042. }
  14043. BUILDIN_FUNC(checkidle)
  14044. {
  14045. TBL_PC *sd = NULL;
  14046. if (script_hasdata(st, 2))
  14047. sd = map_nick2sd(script_getstr(st, 2));
  14048. else
  14049. sd = script_rid2sd(st);
  14050. if (sd)
  14051. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14052. else
  14053. script_pushint(st, 0);
  14054. return SCRIPT_CMD_SUCCESS;
  14055. }
  14056. BUILDIN_FUNC(searchitem)
  14057. {
  14058. struct script_data* data = script_getdata(st, 2);
  14059. const char *itemname = script_getstr(st,3);
  14060. struct item_data *items[MAX_SEARCH];
  14061. int count;
  14062. char* name;
  14063. int32 start;
  14064. int32 id;
  14065. int32 i;
  14066. TBL_PC* sd = NULL;
  14067. if ((items[0] = itemdb_exists(atoi(itemname))))
  14068. count = 1;
  14069. else {
  14070. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14071. if (count > MAX_SEARCH) count = MAX_SEARCH;
  14072. }
  14073. if (!count) {
  14074. script_pushint(st, 0);
  14075. return 0;
  14076. }
  14077. if( !data_isreference(data) )
  14078. {
  14079. ShowError("script:searchitem: not a variable\n");
  14080. script_reportdata(data);
  14081. st->state = END;
  14082. return 1;// not a variable
  14083. }
  14084. id = reference_getid(data);
  14085. start = reference_getindex(data);
  14086. name = reference_getname(data);
  14087. if( not_array_variable(*name) )
  14088. {
  14089. ShowError("script:searchitem: illegal scope\n");
  14090. script_reportdata(data);
  14091. st->state = END;
  14092. return 1;// not supported
  14093. }
  14094. if( not_server_variable(*name) )
  14095. {
  14096. sd = script_rid2sd(st);
  14097. if( sd == NULL )
  14098. return 0;// no player attached
  14099. }
  14100. if( is_string_variable(name) )
  14101. {// string array
  14102. ShowError("script:searchitem: not an integer array reference\n");
  14103. script_reportdata(data);
  14104. st->state = END;
  14105. return 1;// not supported
  14106. }
  14107. for( i = 0; i < count; ++start, ++i )
  14108. {// Set array
  14109. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14110. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14111. }
  14112. script_pushint(st, count);
  14113. return SCRIPT_CMD_SUCCESS;
  14114. }
  14115. // [zBuffer] List of player cont commands --->
  14116. BUILDIN_FUNC(rid2name)
  14117. {
  14118. struct block_list *bl = NULL;
  14119. int rid = script_getnum(st,2);
  14120. if((bl = map_id2bl(rid)))
  14121. {
  14122. switch(bl->type) {
  14123. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14124. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14125. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14126. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14127. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14128. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14129. default:
  14130. ShowError("buildin_rid2name: BL type unknown.\n");
  14131. script_pushconststr(st,"");
  14132. break;
  14133. }
  14134. } else {
  14135. ShowError("buildin_rid2name: invalid RID\n");
  14136. script_pushconststr(st,"(null)");
  14137. }
  14138. return SCRIPT_CMD_SUCCESS;
  14139. }
  14140. BUILDIN_FUNC(pcblockmove)
  14141. {
  14142. int id, flag;
  14143. TBL_PC *sd = NULL;
  14144. id = script_getnum(st,2);
  14145. flag = script_getnum(st,3);
  14146. if(id)
  14147. sd = map_id2sd(id);
  14148. else
  14149. sd = script_rid2sd(st);
  14150. if(sd)
  14151. sd->state.blockedmove = flag > 0;
  14152. return SCRIPT_CMD_SUCCESS;
  14153. }
  14154. BUILDIN_FUNC(pcfollow)
  14155. {
  14156. int id, targetid;
  14157. TBL_PC *sd = NULL;
  14158. id = script_getnum(st,2);
  14159. targetid = script_getnum(st,3);
  14160. if(id)
  14161. sd = map_id2sd(id);
  14162. else
  14163. sd = script_rid2sd(st);
  14164. if(sd)
  14165. pc_follow(sd, targetid);
  14166. return SCRIPT_CMD_SUCCESS;
  14167. }
  14168. BUILDIN_FUNC(pcstopfollow)
  14169. {
  14170. int id;
  14171. TBL_PC *sd = NULL;
  14172. id = script_getnum(st,2);
  14173. if(id)
  14174. sd = map_id2sd(id);
  14175. else
  14176. sd = script_rid2sd(st);
  14177. if(sd)
  14178. pc_stop_following(sd);
  14179. return SCRIPT_CMD_SUCCESS;
  14180. }
  14181. // <--- [zBuffer] List of player cont commands
  14182. // [zBuffer] List of unit control commands --->
  14183. /// Gets specific live information of a bl.
  14184. ///
  14185. /// getunitdata <unit id>,<arrayname>;
  14186. BUILDIN_FUNC(getunitdata)
  14187. {
  14188. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14189. struct block_list* bl;
  14190. TBL_MOB* md = NULL;
  14191. TBL_HOM* hd = NULL;
  14192. TBL_MER* mc = NULL;
  14193. TBL_PET* pd = NULL;
  14194. TBL_ELEM* ed = NULL;
  14195. TBL_NPC* nd = NULL;
  14196. int num;
  14197. char* name;
  14198. if (!data_isreference(script_getdata(st, 3))) {
  14199. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14200. return SCRIPT_CMD_FAILURE;
  14201. }
  14202. bl = map_id2bl(script_getnum(st, 2));
  14203. if (!bl) {
  14204. ShowWarning("buildin_getunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14205. return SCRIPT_CMD_FAILURE;
  14206. }
  14207. switch (bl->type) {
  14208. case BL_MOB: md = map_id2md(bl->id); break;
  14209. case BL_HOM: hd = map_id2hd(bl->id); break;
  14210. case BL_PET: pd = map_id2pd(bl->id); break;
  14211. case BL_MER: mc = map_id2mc(bl->id); break;
  14212. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14213. case BL_NPC: nd = map_id2nd(bl->id); break;
  14214. }
  14215. num = st->stack->stack_data[st->start+3].u.num;
  14216. name = (char *)(str_buf+str_data[num&0x00ffffff].str);
  14217. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),script_getref(st,3))
  14218. switch(bl->type) {
  14219. case BL_MOB:
  14220. if (!md) {
  14221. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14222. return SCRIPT_CMD_FAILURE;
  14223. }
  14224. getunitdata_sub(0, md->status.size);
  14225. getunitdata_sub(1, md->level);
  14226. getunitdata_sub(2, md->status.hp);
  14227. getunitdata_sub(3, md->status.max_hp);
  14228. getunitdata_sub(4, md->master_id);
  14229. getunitdata_sub(5, md->bl.m);
  14230. getunitdata_sub(6, md->bl.x);
  14231. getunitdata_sub(7, md->bl.y);
  14232. getunitdata_sub(8, md->status.speed);
  14233. getunitdata_sub(9, md->status.mode);
  14234. getunitdata_sub(10,md->special_state.ai);
  14235. getunitdata_sub(11,md->sc.option);
  14236. getunitdata_sub(12,md->vd->sex);
  14237. getunitdata_sub(13,md->vd->class_);
  14238. getunitdata_sub(14,md->vd->hair_style);
  14239. getunitdata_sub(15,md->vd->hair_color);
  14240. getunitdata_sub(16,md->vd->head_bottom);
  14241. getunitdata_sub(17,md->vd->head_mid);
  14242. getunitdata_sub(18,md->vd->head_top);
  14243. getunitdata_sub(19,md->vd->cloth_color);
  14244. getunitdata_sub(20,md->vd->shield);
  14245. getunitdata_sub(21,md->vd->weapon);
  14246. getunitdata_sub(22,md->vd->shield);
  14247. getunitdata_sub(23,md->ud.dir);
  14248. getunitdata_sub(24,md->status.str);
  14249. getunitdata_sub(25,md->status.agi);
  14250. getunitdata_sub(26,md->status.vit);
  14251. getunitdata_sub(27,md->status.int_);
  14252. getunitdata_sub(28,md->status.dex);
  14253. getunitdata_sub(29,md->status.luk);
  14254. getunitdata_sub(30,md->state.copy_master_mode);
  14255. getunitdata_sub(31,md->ud.immune_attack);
  14256. break;
  14257. case BL_HOM:
  14258. if (!hd) {
  14259. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14260. return SCRIPT_CMD_FAILURE;
  14261. }
  14262. getunitdata_sub(0, hd->base_status.size);
  14263. getunitdata_sub(1, hd->homunculus.level);
  14264. getunitdata_sub(2, hd->homunculus.hp);
  14265. getunitdata_sub(3, hd->homunculus.max_hp);
  14266. getunitdata_sub(4, hd->homunculus.sp);
  14267. getunitdata_sub(5, hd->homunculus.max_sp);
  14268. getunitdata_sub(6, hd->homunculus.char_id);
  14269. getunitdata_sub(7, hd->bl.m);
  14270. getunitdata_sub(8, hd->bl.x);
  14271. getunitdata_sub(9, hd->bl.y);
  14272. getunitdata_sub(10,hd->homunculus.hunger);
  14273. getunitdata_sub(11,hd->homunculus.intimacy);
  14274. getunitdata_sub(12,hd->base_status.speed);
  14275. getunitdata_sub(13,hd->ud.dir);
  14276. getunitdata_sub(14,hd->ud.canmove_tick);
  14277. getunitdata_sub(15,hd->base_status.str);
  14278. getunitdata_sub(16,hd->base_status.agi);
  14279. getunitdata_sub(17,hd->base_status.vit);
  14280. getunitdata_sub(18,hd->base_status.int_);
  14281. getunitdata_sub(19,hd->base_status.dex);
  14282. getunitdata_sub(20,hd->base_status.luk);
  14283. getunitdata_sub(21,hd->ud.immune_attack);
  14284. break;
  14285. case BL_PET:
  14286. if (!pd) {
  14287. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14288. return SCRIPT_CMD_FAILURE;
  14289. }
  14290. getunitdata_sub(0, pd->status.size);
  14291. getunitdata_sub(1, pd->pet.level);
  14292. getunitdata_sub(2, pd->status.hp);
  14293. getunitdata_sub(3, pd->status.max_hp);
  14294. getunitdata_sub(4, pd->pet.account_id);
  14295. getunitdata_sub(5, pd->bl.m);
  14296. getunitdata_sub(6, pd->bl.x);
  14297. getunitdata_sub(7, pd->bl.y);
  14298. getunitdata_sub(8, pd->pet.hungry);
  14299. getunitdata_sub(9, pd->pet.intimate);
  14300. getunitdata_sub(10,pd->status.speed);
  14301. getunitdata_sub(11,pd->ud.dir);
  14302. getunitdata_sub(12,pd->ud.canmove_tick);
  14303. getunitdata_sub(13,pd->status.str);
  14304. getunitdata_sub(14,pd->status.agi);
  14305. getunitdata_sub(15,pd->status.vit);
  14306. getunitdata_sub(16,pd->status.int_);
  14307. getunitdata_sub(17,pd->status.dex);
  14308. getunitdata_sub(18,pd->status.luk);
  14309. getunitdata_sub(19,pd->ud.immune_attack);
  14310. break;
  14311. case BL_MER:
  14312. if (!mc) {
  14313. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14314. return SCRIPT_CMD_FAILURE;
  14315. }
  14316. getunitdata_sub(0, mc->base_status.size);
  14317. getunitdata_sub(1, mc->base_status.hp);
  14318. getunitdata_sub(2, mc->base_status.max_hp);
  14319. getunitdata_sub(3, mc->mercenary.char_id);
  14320. getunitdata_sub(4, mc->bl.m);
  14321. getunitdata_sub(5, mc->bl.x);
  14322. getunitdata_sub(6, mc->bl.y);
  14323. getunitdata_sub(7, mc->mercenary.kill_count);
  14324. getunitdata_sub(8, mc->mercenary.life_time);
  14325. getunitdata_sub(9, mc->base_status.speed);
  14326. getunitdata_sub(10,mc->ud.dir);
  14327. getunitdata_sub(11,mc->ud.canmove_tick);
  14328. getunitdata_sub(12,mc->base_status.str);
  14329. getunitdata_sub(13,mc->base_status.agi);
  14330. getunitdata_sub(14,mc->base_status.vit);
  14331. getunitdata_sub(15,mc->base_status.int_);
  14332. getunitdata_sub(16,mc->base_status.dex);
  14333. getunitdata_sub(17,mc->base_status.luk);
  14334. getunitdata_sub(18,mc->ud.immune_attack);
  14335. break;
  14336. case BL_ELEM:
  14337. if (!ed) {
  14338. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  14339. return SCRIPT_CMD_FAILURE;
  14340. }
  14341. getunitdata_sub(0, ed->base_status.size);
  14342. getunitdata_sub(1, ed->elemental.hp);
  14343. getunitdata_sub(2, ed->elemental.max_hp);
  14344. getunitdata_sub(3, ed->elemental.sp);
  14345. getunitdata_sub(4, ed->elemental.max_sp);
  14346. getunitdata_sub(5, ed->elemental.char_id);
  14347. getunitdata_sub(6, ed->bl.m);
  14348. getunitdata_sub(7, ed->bl.x);
  14349. getunitdata_sub(8, ed->bl.y);
  14350. getunitdata_sub(9, ed->elemental.life_time);
  14351. getunitdata_sub(10,ed->elemental.mode);
  14352. getunitdata_sub(11,ed->base_status.speed);
  14353. getunitdata_sub(12,ed->ud.dir);
  14354. getunitdata_sub(13,ed->ud.canmove_tick);
  14355. getunitdata_sub(14,ed->base_status.str);
  14356. getunitdata_sub(15,ed->base_status.agi);
  14357. getunitdata_sub(16,ed->base_status.vit);
  14358. getunitdata_sub(17,ed->base_status.int_);
  14359. getunitdata_sub(18,ed->base_status.dex);
  14360. getunitdata_sub(19,ed->base_status.luk);
  14361. getunitdata_sub(20,ed->ud.immune_attack);
  14362. break;
  14363. case BL_NPC:
  14364. if (!nd) {
  14365. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  14366. return SCRIPT_CMD_FAILURE;
  14367. }
  14368. getunitdata_sub(0, nd->class_);
  14369. getunitdata_sub(1, nd->level);
  14370. getunitdata_sub(2, nd->status.hp);
  14371. getunitdata_sub(3, nd->status.max_hp);
  14372. getunitdata_sub(4, nd->bl.m);
  14373. getunitdata_sub(5, nd->bl.x);
  14374. getunitdata_sub(6, nd->bl.y);
  14375. getunitdata_sub(7, nd->ud.dir);
  14376. getunitdata_sub(8, nd->status.str);
  14377. getunitdata_sub(9, nd->status.agi);
  14378. getunitdata_sub(10,nd->status.vit);
  14379. getunitdata_sub(11,nd->status.int_);
  14380. getunitdata_sub(12,nd->status.dex);
  14381. getunitdata_sub(13,nd->status.luk);
  14382. break;
  14383. default:
  14384. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  14385. return SCRIPT_CMD_FAILURE;
  14386. }
  14387. #undef getunitdata_sub
  14388. return SCRIPT_CMD_SUCCESS;
  14389. }
  14390. /// Changes the live data of a bl.
  14391. ///
  14392. /// setunitdata <unit id>,<type>,<value>;
  14393. BUILDIN_FUNC(setunitdata)
  14394. {
  14395. struct block_list* bl = NULL;
  14396. TBL_MOB* md = NULL;
  14397. TBL_HOM* hd = NULL;
  14398. TBL_MER* mc = NULL;
  14399. TBL_PET* pd = NULL;
  14400. TBL_ELEM* ed = NULL;
  14401. TBL_NPC* nd = NULL;
  14402. int type, value;
  14403. bl = map_id2bl(script_getnum(st, 2));
  14404. if (!bl) {
  14405. ShowWarning("buildin_setunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14406. return SCRIPT_CMD_FAILURE;
  14407. }
  14408. switch (bl->type) {
  14409. case BL_MOB: md = map_id2md(bl->id); break;
  14410. case BL_HOM: hd = map_id2hd(bl->id); break;
  14411. case BL_PET: pd = map_id2pd(bl->id); break;
  14412. case BL_MER: mc = map_id2mc(bl->id); break;
  14413. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14414. case BL_NPC: nd = map_id2nd(bl->id); break;
  14415. }
  14416. type = script_getnum(st, 3);
  14417. value = script_getnum(st, 4);
  14418. switch (bl->type) {
  14419. case BL_MOB:
  14420. if (!md) {
  14421. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  14422. return SCRIPT_CMD_FAILURE;
  14423. }
  14424. switch (type) {
  14425. case 0: md->status.size = (unsigned char)value; break;
  14426. case 1: md->level = (unsigned short)value; break;
  14427. case 2: md->status.hp = (unsigned int)value; break;
  14428. case 3: md->status.max_hp = (unsigned int)value; break;
  14429. case 4: md->master_id = value; break;
  14430. case 5: md->bl.m = (short)value; break;
  14431. case 6: md->bl.x = (short)value; break;
  14432. case 7: md->bl.y = (short)value; break;
  14433. case 8: md->status.speed = (unsigned short)value; break;
  14434. case 9: md->status.mode = (enum e_mode)value; break;
  14435. case 10: md->special_state.ai = (enum mob_ai)value; break;
  14436. case 11: md->sc.option = (unsigned short)value; break;
  14437. case 12: md->vd->sex = (char)value; break;
  14438. case 13: md->vd->class_ = (unsigned short)value; break;
  14439. case 14: md->vd->hair_style = (unsigned short)value; break;
  14440. case 15: md->vd->hair_color = (unsigned short)value; break;
  14441. case 16: md->vd->head_bottom = (unsigned short)value; break;
  14442. case 17: md->vd->head_mid = (unsigned short)value; break;
  14443. case 18: md->vd->head_top = (unsigned short)value; break;
  14444. case 19: md->vd->cloth_color = (unsigned short)value; break;
  14445. case 20: md->vd->shield = (unsigned short)value; break;
  14446. case 21: md->vd->weapon = (unsigned short)value; break;
  14447. case 22: md->vd->shield = (unsigned short)value; break;
  14448. case 23: md->ud.dir = (unsigned char)value; break;
  14449. case 24: md->status.str = (unsigned int)value; break;
  14450. case 25: md->status.agi = (unsigned int)value; break;
  14451. case 26: md->status.vit = (unsigned int)value; break;
  14452. case 27: md->status.int_ = (unsigned int)value; break;
  14453. case 28: md->status.dex = (unsigned int)value; break;
  14454. case 29: md->status.luk = (unsigned int)value; break;
  14455. case 30: md->state.copy_master_mode = value > 0 ? 1 : 0; break;
  14456. case 31: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14457. default:
  14458. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  14459. return SCRIPT_CMD_FAILURE;
  14460. }
  14461. break;
  14462. case BL_HOM:
  14463. if (!hd) {
  14464. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  14465. return SCRIPT_CMD_FAILURE;
  14466. }
  14467. switch (type) {
  14468. case 0: hd->base_status.size = (unsigned char)value; break;
  14469. case 1: hd->homunculus.level = (unsigned short)value; break;
  14470. case 2: hd->homunculus.hp = (unsigned int)value; break;
  14471. case 3: hd->homunculus.max_hp = (unsigned int)value; break;
  14472. case 4: hd->homunculus.sp = (unsigned int)value; break;
  14473. case 5: hd->homunculus.max_sp = (unsigned int)value; break;
  14474. case 6: hd->homunculus.char_id = (unsigned int)value; break;
  14475. case 7: hd->bl.m = (short)value; break;
  14476. case 8: hd->bl.x = (short)value; break;
  14477. case 9: hd->bl.y = (short)value; break;
  14478. case 10: hd->homunculus.hunger = (short)value; break;
  14479. case 11: hd->homunculus.intimacy = (unsigned int)value; break;
  14480. case 12: hd->base_status.speed = (unsigned short)value; break;
  14481. case 13: hd->ud.dir = (unsigned char)value; break;
  14482. case 14: hd->ud.canmove_tick = value > 0 ? 1 : 0; break;
  14483. case 15: hd->base_status.str = (unsigned int)value; break;
  14484. case 16: hd->base_status.agi = (unsigned int)value; break;
  14485. case 17: hd->base_status.vit = (unsigned int)value; break;
  14486. case 18: hd->base_status.int_ = (unsigned int)value; break;
  14487. case 19: hd->base_status.dex = (unsigned int)value; break;
  14488. case 20: hd->base_status.luk = (unsigned int)value; break;
  14489. case 21: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14490. default:
  14491. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  14492. return SCRIPT_CMD_FAILURE;
  14493. }
  14494. break;
  14495. case BL_PET:
  14496. if (!pd) {
  14497. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  14498. return SCRIPT_CMD_FAILURE;
  14499. }
  14500. switch (type) {
  14501. case 0: pd->status.size = (unsigned char)value; break;
  14502. case 1: pd->pet.level = (unsigned short)value; break;
  14503. case 2: pd->status.hp = (unsigned int)value; break;
  14504. case 3: pd->status.max_hp = (unsigned int)value; break;
  14505. case 4: pd->pet.account_id = (unsigned int)value; break;
  14506. case 5: pd->bl.m = (short)value; break;
  14507. case 6: pd->bl.x = (short)value; break;
  14508. case 7: pd->bl.y = (short)value; break;
  14509. case 8: pd->pet.hungry = (short)value; break;
  14510. case 9: pd->pet.intimate = (unsigned int)value; break;
  14511. case 10: pd->status.speed = (unsigned short)value; break;
  14512. case 11: pd->ud.dir = (unsigned char)value; break;
  14513. case 12: pd->ud.canmove_tick = value > 0 ? 1 : 0; break;
  14514. case 13: pd->status.str = (unsigned int)value; break;
  14515. case 14: pd->status.agi = (unsigned int)value; break;
  14516. case 15: pd->status.vit = (unsigned int)value; break;
  14517. case 16: pd->status.int_ = (unsigned int)value; break;
  14518. case 17: pd->status.dex = (unsigned int)value; break;
  14519. case 18: pd->status.luk = (unsigned int)value; break;
  14520. case 19: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14521. default:
  14522. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  14523. return SCRIPT_CMD_FAILURE;
  14524. }
  14525. break;
  14526. case BL_MER:
  14527. if (!mc) {
  14528. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  14529. return SCRIPT_CMD_FAILURE;
  14530. }
  14531. switch (type) {
  14532. case 0: mc->base_status.size = (unsigned char)value; break;
  14533. case 1: mc->base_status.hp = (unsigned int)value; break;
  14534. case 2: mc->base_status.max_hp = (unsigned int)value; break;
  14535. case 3: mc->mercenary.char_id = (unsigned int)value; break;
  14536. case 4: mc->bl.m = (short)value; break;
  14537. case 5: mc->bl.x = (short)value; break;
  14538. case 6: mc->bl.y = (short)value; break;
  14539. case 7: mc->mercenary.kill_count = (unsigned int)value; break;
  14540. case 8: mc->mercenary.life_time = (unsigned int)value; break;
  14541. case 9: mc->base_status.speed = (unsigned short)value; break;
  14542. case 10: mc->ud.dir = (unsigned char)value; break;
  14543. case 11: mc->ud.canmove_tick = value > 0 ? 1 : 0; break;
  14544. case 12: mc->base_status.str = (unsigned int)value; break;
  14545. case 13: mc->base_status.agi = (unsigned int)value; break;
  14546. case 14: mc->base_status.vit = (unsigned int)value; break;
  14547. case 15: mc->base_status.int_ = (unsigned int)value; break;
  14548. case 16: mc->base_status.dex = (unsigned int)value; break;
  14549. case 17: mc->base_status.luk = (unsigned int)value; break;
  14550. case 18: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14551. default:
  14552. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  14553. return SCRIPT_CMD_FAILURE;
  14554. }
  14555. break;
  14556. case BL_ELEM:
  14557. if (!ed) {
  14558. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  14559. return SCRIPT_CMD_FAILURE;
  14560. }
  14561. switch (type) {
  14562. case 0: ed->base_status.size = (unsigned char)value; break;
  14563. case 1: ed->elemental.hp = (unsigned int)value; break;
  14564. case 2: ed->elemental.max_hp = (unsigned int)value; break;
  14565. case 3: ed->elemental.sp = (unsigned int)value; break;
  14566. case 4: ed->elemental.max_sp = (unsigned int)value; break;
  14567. case 5: ed->elemental.char_id = (unsigned int)value; break;
  14568. case 6: ed->bl.m = (short)value; break;
  14569. case 7: ed->bl.x = (short)value; break;
  14570. case 8: ed->bl.y = (short)value; break;
  14571. case 9: ed->elemental.life_time = (unsigned int)value; break;
  14572. case 10: ed->elemental.mode = (unsigned int)value; break;
  14573. case 11: ed->base_status.speed = (unsigned short)value; break;
  14574. case 12: ed->ud.dir = (unsigned char)value; break;
  14575. case 13: ed->ud.canmove_tick = value > 0 ? 1 : 0; break;
  14576. case 14: ed->base_status.str = (unsigned int)value; break;
  14577. case 15: ed->base_status.agi = (unsigned int)value; break;
  14578. case 16: ed->base_status.vit = (unsigned int)value; break;
  14579. case 17: ed->base_status.int_ = (unsigned int)value; break;
  14580. case 18: ed->base_status.dex = (unsigned int)value; break;
  14581. case 19: ed->base_status.luk = (unsigned int)value; break;
  14582. case 20: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  14583. default:
  14584. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  14585. return SCRIPT_CMD_FAILURE;
  14586. }
  14587. break;
  14588. case BL_NPC:
  14589. if (!md) {
  14590. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  14591. return SCRIPT_CMD_FAILURE;
  14592. }
  14593. switch (type) {
  14594. case 0: nd->class_ = (unsigned int)value; break;
  14595. case 1: nd->level = (unsigned int)value; break;
  14596. case 2: nd->status.hp = (unsigned int)value; break;
  14597. case 3: nd->status.max_hp = (unsigned int)value; break;
  14598. case 4: nd->bl.m = (short)value; break;
  14599. case 5: nd->bl.x = (short)value; break;
  14600. case 6: nd->bl.y = (short)value; break;
  14601. case 7: nd->ud.dir = (unsigned char)value; break;
  14602. case 8: nd->status.str = (unsigned int)value; break;
  14603. case 9: nd->status.agi = (unsigned int)value; break;
  14604. case 10: nd->status.vit = (unsigned int)value; break;
  14605. case 11: nd->status.int_ = (unsigned int)value; break;
  14606. case 12: nd->status.dex = (unsigned int)value; break;
  14607. case 13: nd->status.luk = (unsigned int)value; break;
  14608. default:
  14609. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  14610. return SCRIPT_CMD_FAILURE;
  14611. }
  14612. break;
  14613. default:
  14614. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  14615. return SCRIPT_CMD_FAILURE;
  14616. }
  14617. return SCRIPT_CMD_SUCCESS;
  14618. }
  14619. /// Gets the name of a bl.
  14620. /// Supported types are [MOB|HOM|PET|NPC].
  14621. /// MER and ELEM don't support custom names.
  14622. ///
  14623. /// getunitname <unit id>;
  14624. BUILDIN_FUNC(getunitname)
  14625. {
  14626. struct block_list* bl = NULL;
  14627. bl = map_id2bl(script_getnum(st, 2));
  14628. if (!bl) {
  14629. ShowWarning("buildin_getunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14630. return SCRIPT_CMD_FAILURE;
  14631. }
  14632. script_pushstrcopy(st, status_get_name(bl));
  14633. return SCRIPT_CMD_SUCCESS;
  14634. }
  14635. /// Changes the name of a bl.
  14636. /// Supported types are [MOB|HOM|PET].
  14637. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  14638. ///
  14639. /// setunitname <unit id>,<name>;
  14640. BUILDIN_FUNC(setunitname)
  14641. {
  14642. struct block_list* bl = NULL;
  14643. TBL_MOB* md = NULL;
  14644. TBL_HOM* hd = NULL;
  14645. TBL_PET* pd = NULL;
  14646. bl = map_id2bl(script_getnum(st, 2));
  14647. if (!bl) {
  14648. ShowWarning("buildin_setunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14649. return SCRIPT_CMD_FAILURE;
  14650. }
  14651. switch (bl->type) {
  14652. case BL_MOB: md = map_id2md(bl->id); break;
  14653. case BL_HOM: hd = map_id2hd(bl->id); break;
  14654. case BL_PET: pd = map_id2pd(bl->id); break;
  14655. }
  14656. switch (bl->type) {
  14657. case BL_MOB:
  14658. if (!md) {
  14659. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  14660. return SCRIPT_CMD_FAILURE;
  14661. }
  14662. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  14663. break;
  14664. case BL_HOM:
  14665. if (!hd) {
  14666. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  14667. return SCRIPT_CMD_FAILURE;
  14668. }
  14669. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  14670. break;
  14671. case BL_PET:
  14672. if (!pd) {
  14673. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  14674. return SCRIPT_CMD_FAILURE;
  14675. }
  14676. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  14677. break;
  14678. default:
  14679. ShowWarning("buildin_setunitname: Unknown object type!\n");
  14680. return SCRIPT_CMD_FAILURE;
  14681. }
  14682. clif_charnameack(0, bl); // Send update to client.
  14683. return SCRIPT_CMD_SUCCESS;
  14684. }
  14685. /// Makes the unit walk to target position or map.
  14686. /// Returns if it was successful.
  14687. ///
  14688. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  14689. /// unitwalk(<unit_id>,<target_id>) -> <bool>
  14690. BUILDIN_FUNC(unitwalk)
  14691. {
  14692. struct block_list* bl;
  14693. bl = map_id2bl(script_getnum(st,2));
  14694. if (!bl)
  14695. script_pushint(st, 0);
  14696. else if( script_hasdata(st,4) ) {
  14697. int x = script_getnum(st,3);
  14698. int y = script_getnum(st,4);
  14699. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  14700. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  14701. } else {
  14702. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  14703. if (!tbl) {
  14704. ShowError("buildin_unitwalk: Bad target destination.\n");
  14705. script_pushint(st, 0);
  14706. return SCRIPT_CMD_FAILURE;
  14707. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  14708. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  14709. }
  14710. return SCRIPT_CMD_SUCCESS;
  14711. }
  14712. /// Kills the unit.
  14713. ///
  14714. /// unitkill <unit_id>;
  14715. BUILDIN_FUNC(unitkill)
  14716. {
  14717. struct block_list* bl = map_id2bl(script_getnum(st,2));
  14718. if (bl != NULL)
  14719. status_kill(bl);
  14720. return SCRIPT_CMD_SUCCESS;
  14721. }
  14722. /// Warps the unit to the target position in the target map.
  14723. /// Returns if it was successful.
  14724. ///
  14725. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  14726. BUILDIN_FUNC(unitwarp)
  14727. {
  14728. int unit_id;
  14729. int map_idx;
  14730. short x;
  14731. short y;
  14732. struct block_list* bl;
  14733. const char *mapname;
  14734. unit_id = script_getnum(st,2);
  14735. mapname = script_getstr(st, 3);
  14736. x = (short)script_getnum(st,4);
  14737. y = (short)script_getnum(st,5);
  14738. if (!unit_id) //Warp the script's runner
  14739. bl = map_id2bl(st->rid);
  14740. else
  14741. bl = map_id2bl(unit_id);
  14742. if (!strcmp(mapname,"this"))
  14743. map_idx = bl?bl->m:-1;
  14744. else
  14745. map_idx = map_mapname2mapid(mapname);
  14746. if (map_idx >= 0 && bl != NULL)
  14747. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  14748. else
  14749. script_pushint(st, 0);
  14750. return SCRIPT_CMD_SUCCESS;
  14751. }
  14752. /// Makes the unit attack the target.
  14753. /// If the unit is a player and <action type> is not 0, it does a continuous
  14754. /// attack instead of a single attack.
  14755. /// Returns if the request was successful.
  14756. ///
  14757. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  14758. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  14759. BUILDIN_FUNC(unitattack)
  14760. {
  14761. struct block_list* unit_bl;
  14762. struct block_list* target_bl = NULL;
  14763. struct script_data* data;
  14764. int actiontype = 0;
  14765. unit_bl = map_id2bl(script_getnum(st,2));
  14766. if (!unit_bl) {
  14767. script_pushint(st, 0);
  14768. return SCRIPT_CMD_FAILURE;
  14769. }
  14770. data = script_getdata(st, 3);
  14771. get_val(st, data);
  14772. if (data_isstring(data)) {
  14773. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  14774. if( sd != NULL )
  14775. target_bl = &sd->bl;
  14776. } else
  14777. target_bl = map_id2bl(conv_num(st, data));
  14778. if (!target_bl) {
  14779. script_pushint(st, 0);
  14780. return SCRIPT_CMD_FAILURE;
  14781. }
  14782. if (script_hasdata(st,4))
  14783. actiontype = script_getnum(st,4);
  14784. switch(unit_bl->type) {
  14785. case BL_PC:
  14786. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  14787. script_pushint(st, 1);
  14788. return SCRIPT_CMD_SUCCESS;
  14789. case BL_MOB:
  14790. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  14791. break;
  14792. case BL_PET:
  14793. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  14794. break;
  14795. default:
  14796. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  14797. script_pushint(st, 0);
  14798. return SCRIPT_CMD_FAILURE;
  14799. }
  14800. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  14801. return SCRIPT_CMD_SUCCESS;
  14802. }
  14803. /// Makes the unit stop attacking.
  14804. ///
  14805. /// unitstopattack <unit_id>;
  14806. BUILDIN_FUNC(unitstopattack)
  14807. {
  14808. int unit_id;
  14809. struct block_list* bl;
  14810. unit_id = script_getnum(st,2);
  14811. bl = map_id2bl(unit_id);
  14812. if (bl != NULL) {
  14813. unit_stop_attack(bl);
  14814. if (bl->type == BL_MOB)
  14815. ((TBL_MOB*)bl)->target_id = 0;
  14816. }
  14817. return SCRIPT_CMD_SUCCESS;
  14818. }
  14819. /// Makes the unit stop walking.
  14820. ///
  14821. /// unitstopwalk <unit_id>;
  14822. BUILDIN_FUNC(unitstopwalk)
  14823. {
  14824. int unit_id;
  14825. struct block_list* bl;
  14826. unit_id = script_getnum(st,2);
  14827. bl = map_id2bl(unit_id);
  14828. if (bl != NULL) {
  14829. unit_stop_walking(bl,4);
  14830. if (bl->type == BL_MOB)
  14831. ((TBL_MOB*)bl)->target_id = 0;
  14832. }
  14833. return SCRIPT_CMD_SUCCESS;
  14834. }
  14835. /// Makes the unit say the given message.
  14836. ///
  14837. /// unittalk <unit_id>,"<message>";
  14838. BUILDIN_FUNC(unittalk)
  14839. {
  14840. int unit_id;
  14841. const char* message;
  14842. struct block_list* bl;
  14843. unit_id = script_getnum(st,2);
  14844. message = script_getstr(st, 3);
  14845. bl = map_id2bl(unit_id);
  14846. if (bl != NULL) {
  14847. struct StringBuf sbuf;
  14848. StringBuf_Init(&sbuf);
  14849. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  14850. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  14851. StringBuf_Destroy(&sbuf);
  14852. }
  14853. return SCRIPT_CMD_SUCCESS;
  14854. }
  14855. /// Makes the unit do an emotion.
  14856. ///
  14857. /// unitemote <unit_id>,<emotion>;
  14858. ///
  14859. /// @see e_* in const.txt
  14860. BUILDIN_FUNC(unitemote)
  14861. {
  14862. int unit_id;
  14863. int emotion;
  14864. struct block_list* bl;
  14865. unit_id = script_getnum(st,2);
  14866. emotion = script_getnum(st,3);
  14867. bl = map_id2bl(unit_id);
  14868. if (bl != NULL)
  14869. clif_emotion(bl, emotion);
  14870. return SCRIPT_CMD_SUCCESS;
  14871. }
  14872. /// Makes the unit cast the skill on the target or self if no target is specified.
  14873. ///
  14874. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  14875. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  14876. BUILDIN_FUNC(unitskilluseid)
  14877. {
  14878. int unit_id, target_id, casttime;
  14879. uint16 skill_id, skill_lv;
  14880. struct block_list* bl;
  14881. struct script_data *data;
  14882. unit_id = script_getnum(st,2);
  14883. data = script_getdata(st, 3);
  14884. get_val(st, data); // Convert into value in case of a variable
  14885. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  14886. skill_lv = script_getnum(st,4);
  14887. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  14888. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  14889. bl = map_id2bl(unit_id);
  14890. if (bl != NULL)
  14891. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  14892. return SCRIPT_CMD_SUCCESS;
  14893. }
  14894. /// Makes the unit cast the skill on the target position.
  14895. ///
  14896. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  14897. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  14898. BUILDIN_FUNC(unitskillusepos)
  14899. {
  14900. int unit_id, skill_x, skill_y, casttime;
  14901. uint16 skill_id, skill_lv;
  14902. struct block_list* bl;
  14903. struct script_data *data;
  14904. unit_id = script_getnum(st,2);
  14905. data = script_getdata(st, 3);
  14906. get_val(st, data); // Convert into value in case of a variable
  14907. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  14908. skill_lv = script_getnum(st,4);
  14909. skill_x = script_getnum(st,5);
  14910. skill_y = script_getnum(st,6);
  14911. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  14912. bl = map_id2bl(unit_id);
  14913. if (bl != NULL)
  14914. unit_skilluse_pos2(bl, skill_x, skill_y, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  14915. return SCRIPT_CMD_SUCCESS;
  14916. }
  14917. // <--- [zBuffer] List of mob control commands
  14918. /// Pauses the execution of the script, detaching the player
  14919. ///
  14920. /// sleep <mili seconds>;
  14921. BUILDIN_FUNC(sleep)
  14922. {
  14923. int ticks;
  14924. ticks = script_getnum(st,2);
  14925. // detach the player
  14926. script_detach_rid(st);
  14927. if( ticks <= 0 )
  14928. {// do nothing
  14929. }
  14930. else if( st->sleep.tick == 0 )
  14931. {// sleep for the target amount of time
  14932. st->state = RERUNLINE;
  14933. st->sleep.tick = ticks;
  14934. }
  14935. else
  14936. {// sleep time is over
  14937. st->state = RUN;
  14938. st->sleep.tick = 0;
  14939. }
  14940. return SCRIPT_CMD_SUCCESS;
  14941. }
  14942. /// Pauses the execution of the script, keeping the player attached
  14943. /// Returns if a player is still attached
  14944. ///
  14945. /// sleep2(<mili secconds>) -> <bool>
  14946. BUILDIN_FUNC(sleep2)
  14947. {
  14948. int ticks;
  14949. ticks = script_getnum(st,2);
  14950. if( ticks <= 0 )
  14951. {// do nothing
  14952. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  14953. }
  14954. else if( !st->sleep.tick )
  14955. {// sleep for the target amount of time
  14956. st->state = RERUNLINE;
  14957. st->sleep.tick = ticks;
  14958. }
  14959. else
  14960. {// sleep time is over
  14961. st->state = RUN;
  14962. st->sleep.tick = 0;
  14963. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  14964. }
  14965. return SCRIPT_CMD_SUCCESS;
  14966. }
  14967. /// Awakes all the sleep timers of the target npc
  14968. ///
  14969. /// awake "<npc name>";
  14970. BUILDIN_FUNC(awake)
  14971. {
  14972. struct npc_data* nd;
  14973. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  14974. nd = npc_name2id(script_getstr(st, 2));
  14975. if( nd == NULL ) {
  14976. ShowError("awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  14977. return 1;
  14978. }
  14979. while( node )
  14980. {
  14981. if( (int)__64BPRTSIZE(node->key) == nd->bl.id )
  14982. {// sleep timer for the npc
  14983. struct script_state* tst = (struct script_state*)node->data;
  14984. TBL_PC* sd = map_id2sd(tst->rid);
  14985. if( tst->sleep.timer == INVALID_TIMER )
  14986. {// already awake ???
  14987. node = node->next;
  14988. continue;
  14989. }
  14990. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  14991. {// char not online anymore / another char of the same account is online - Cancel execution
  14992. tst->state = END;
  14993. tst->rid = 0;
  14994. }
  14995. delete_timer(tst->sleep.timer, run_script_timer);
  14996. node = script_erase_sleepdb(node);
  14997. tst->sleep.timer = INVALID_TIMER;
  14998. if(tst->state != RERUNLINE)
  14999. tst->sleep.tick = 0;
  15000. run_script_main(tst);
  15001. }
  15002. else
  15003. {
  15004. node = node->next;
  15005. }
  15006. }
  15007. return SCRIPT_CMD_SUCCESS;
  15008. }
  15009. /// Returns a reference to a variable of the target NPC.
  15010. /// Returns 0 if an error occurs.
  15011. ///
  15012. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15013. BUILDIN_FUNC(getvariableofnpc)
  15014. {
  15015. struct script_data* data;
  15016. const char* name;
  15017. struct npc_data* nd;
  15018. data = script_getdata(st,2);
  15019. if( !data_isreference(data) )
  15020. {// Not a reference (aka varaible name)
  15021. ShowError("script:getvariableofnpc: not a variable\n");
  15022. script_reportdata(data);
  15023. script_pushnil(st);
  15024. st->state = END;
  15025. return 1;
  15026. }
  15027. name = reference_getname(data);
  15028. if( *name != '.' || name[1] == '@' )
  15029. {// not a npc variable
  15030. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  15031. script_reportdata(data);
  15032. script_pushnil(st);
  15033. st->state = END;
  15034. return 1;
  15035. }
  15036. nd = npc_name2id(script_getstr(st,3));
  15037. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15038. {// NPC not found or has no script
  15039. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15040. script_pushnil(st);
  15041. st->state = END;
  15042. return 1;
  15043. }
  15044. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  15045. return SCRIPT_CMD_SUCCESS;
  15046. }
  15047. /// Opens a warp portal.
  15048. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15049. ///
  15050. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15051. ///
  15052. /// @author blackhole89
  15053. BUILDIN_FUNC(warpportal)
  15054. {
  15055. int spx;
  15056. int spy;
  15057. unsigned short mapindex;
  15058. int tpx;
  15059. int tpy;
  15060. struct skill_unit_group* group;
  15061. struct block_list* bl;
  15062. bl = map_id2bl(st->oid);
  15063. if( bl == NULL ) {
  15064. ShowError("buildin_warpportal: NPC is needed\n");
  15065. return SCRIPT_CMD_FAILURE;
  15066. }
  15067. spx = script_getnum(st,2);
  15068. spy = script_getnum(st,3);
  15069. mapindex = mapindex_name2id(script_getstr(st, 4));
  15070. tpx = script_getnum(st,5);
  15071. tpy = script_getnum(st,6);
  15072. if( mapindex == 0 ) {
  15073. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15074. return SCRIPT_CMD_FAILURE;
  15075. }
  15076. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15077. if( group == NULL )
  15078. return SCRIPT_CMD_FAILURE;// failed
  15079. group->val1 = (group->val1<<16)|(short)0;
  15080. group->val2 = (tpx<<16) | tpy;
  15081. group->val3 = mapindex;
  15082. return SCRIPT_CMD_SUCCESS;
  15083. }
  15084. BUILDIN_FUNC(openmail)
  15085. {
  15086. TBL_PC* sd;
  15087. sd = script_rid2sd(st);
  15088. if( sd == NULL )
  15089. return 0;
  15090. mail_openmail(sd);
  15091. return SCRIPT_CMD_SUCCESS;
  15092. }
  15093. BUILDIN_FUNC(openauction)
  15094. {
  15095. TBL_PC* sd;
  15096. sd = script_rid2sd(st);
  15097. if( sd == NULL )
  15098. return 0;
  15099. if( !battle_config.feature_auction ) {
  15100. clif_colormes(sd, color_table[COLOR_RED], msg_txt(sd, 517));
  15101. return 0;
  15102. }
  15103. clif_Auction_openwindow(sd);
  15104. return SCRIPT_CMD_SUCCESS;
  15105. }
  15106. /// Retrieves the value of the specified flag of the specified cell.
  15107. ///
  15108. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15109. ///
  15110. /// @see cell_chk* constants in const.txt for the types
  15111. BUILDIN_FUNC(checkcell)
  15112. {
  15113. int16 m = map_mapname2mapid(script_getstr(st,2));
  15114. int16 x = script_getnum(st,3);
  15115. int16 y = script_getnum(st,4);
  15116. cell_chk type = (cell_chk)script_getnum(st,5);
  15117. script_pushint(st, map_getcell(m, x, y, type));
  15118. return SCRIPT_CMD_SUCCESS;
  15119. }
  15120. /// Modifies flags of cells in the specified area.
  15121. ///
  15122. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  15123. ///
  15124. /// @see cell_* constants in const.txt for the types
  15125. BUILDIN_FUNC(setcell)
  15126. {
  15127. int16 m = map_mapname2mapid(script_getstr(st,2));
  15128. int16 x1 = script_getnum(st,3);
  15129. int16 y1 = script_getnum(st,4);
  15130. int16 x2 = script_getnum(st,5);
  15131. int16 y2 = script_getnum(st,6);
  15132. cell_t type = (cell_t)script_getnum(st,7);
  15133. bool flag = (bool)script_getnum(st,8);
  15134. int x,y;
  15135. if( x1 > x2 ) swap(x1,x2);
  15136. if( y1 > y2 ) swap(y1,y2);
  15137. for( y = y1; y <= y2; ++y )
  15138. for( x = x1; x <= x2; ++x )
  15139. map_setcell(m, x, y, type, flag);
  15140. return SCRIPT_CMD_SUCCESS;
  15141. }
  15142. /*==========================================
  15143. * Mercenary Commands
  15144. *------------------------------------------*/
  15145. BUILDIN_FUNC(mercenary_create)
  15146. {
  15147. struct map_session_data *sd;
  15148. int class_, contract_time;
  15149. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  15150. return 0;
  15151. class_ = script_getnum(st,2);
  15152. if( !mercenary_class(class_) )
  15153. return 0;
  15154. contract_time = script_getnum(st,3);
  15155. mercenary_create(sd, class_, contract_time);
  15156. script_pushint(st, sd->md->bl.id);
  15157. return SCRIPT_CMD_SUCCESS;
  15158. }
  15159. BUILDIN_FUNC(mercenary_heal)
  15160. {
  15161. struct map_session_data *sd = script_rid2sd(st);
  15162. int hp, sp;
  15163. if( sd == NULL || sd->md == NULL )
  15164. return 0;
  15165. hp = script_getnum(st,2);
  15166. sp = script_getnum(st,3);
  15167. status_heal(&sd->md->bl, hp, sp, 0);
  15168. return SCRIPT_CMD_SUCCESS;
  15169. }
  15170. BUILDIN_FUNC(mercenary_sc_start)
  15171. {
  15172. struct map_session_data *sd = script_rid2sd(st);
  15173. enum sc_type type;
  15174. int tick, val1;
  15175. if( sd == NULL || sd->md == NULL )
  15176. return 0;
  15177. type = (sc_type)script_getnum(st,2);
  15178. tick = script_getnum(st,3);
  15179. val1 = script_getnum(st,4);
  15180. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  15181. return SCRIPT_CMD_SUCCESS;
  15182. }
  15183. BUILDIN_FUNC(mercenary_get_calls)
  15184. {
  15185. struct map_session_data *sd = script_rid2sd(st);
  15186. int guild;
  15187. if( sd == NULL )
  15188. return 0;
  15189. guild = script_getnum(st,2);
  15190. switch( guild )
  15191. {
  15192. case ARCH_MERC_GUILD:
  15193. script_pushint(st,sd->status.arch_calls);
  15194. break;
  15195. case SPEAR_MERC_GUILD:
  15196. script_pushint(st,sd->status.spear_calls);
  15197. break;
  15198. case SWORD_MERC_GUILD:
  15199. script_pushint(st,sd->status.sword_calls);
  15200. break;
  15201. default:
  15202. script_pushint(st,0);
  15203. break;
  15204. }
  15205. return SCRIPT_CMD_SUCCESS;
  15206. }
  15207. BUILDIN_FUNC(mercenary_set_calls)
  15208. {
  15209. struct map_session_data *sd = script_rid2sd(st);
  15210. int guild, value, *calls;
  15211. if( sd == NULL )
  15212. return 0;
  15213. guild = script_getnum(st,2);
  15214. value = script_getnum(st,3);
  15215. switch( guild )
  15216. {
  15217. case ARCH_MERC_GUILD:
  15218. calls = &sd->status.arch_calls;
  15219. break;
  15220. case SPEAR_MERC_GUILD:
  15221. calls = &sd->status.spear_calls;
  15222. break;
  15223. case SWORD_MERC_GUILD:
  15224. calls = &sd->status.sword_calls;
  15225. break;
  15226. default:
  15227. return 0; // Invalid Guild
  15228. }
  15229. *calls += value;
  15230. *calls = cap_value(*calls, 0, INT_MAX);
  15231. return SCRIPT_CMD_SUCCESS;
  15232. }
  15233. BUILDIN_FUNC(mercenary_get_faith)
  15234. {
  15235. struct map_session_data *sd = script_rid2sd(st);
  15236. int guild;
  15237. if( sd == NULL )
  15238. return 0;
  15239. guild = script_getnum(st,2);
  15240. switch( guild )
  15241. {
  15242. case ARCH_MERC_GUILD:
  15243. script_pushint(st,sd->status.arch_faith);
  15244. break;
  15245. case SPEAR_MERC_GUILD:
  15246. script_pushint(st,sd->status.spear_faith);
  15247. break;
  15248. case SWORD_MERC_GUILD:
  15249. script_pushint(st,sd->status.sword_faith);
  15250. break;
  15251. default:
  15252. script_pushint(st,0);
  15253. break;
  15254. }
  15255. return SCRIPT_CMD_SUCCESS;
  15256. }
  15257. BUILDIN_FUNC(mercenary_set_faith)
  15258. {
  15259. struct map_session_data *sd = script_rid2sd(st);
  15260. int guild, value, *calls;
  15261. if( sd == NULL )
  15262. return 0;
  15263. guild = script_getnum(st,2);
  15264. value = script_getnum(st,3);
  15265. switch( guild )
  15266. {
  15267. case ARCH_MERC_GUILD:
  15268. calls = &sd->status.arch_faith;
  15269. break;
  15270. case SPEAR_MERC_GUILD:
  15271. calls = &sd->status.spear_faith;
  15272. break;
  15273. case SWORD_MERC_GUILD:
  15274. calls = &sd->status.sword_faith;
  15275. break;
  15276. default:
  15277. return 0; // Invalid Guild
  15278. }
  15279. *calls += value;
  15280. *calls = cap_value(*calls, 0, INT_MAX);
  15281. if( mercenary_get_guild(sd->md) == guild )
  15282. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  15283. return SCRIPT_CMD_SUCCESS;
  15284. }
  15285. /*------------------------------------------
  15286. * Book Reading
  15287. *------------------------------------------*/
  15288. BUILDIN_FUNC(readbook)
  15289. {
  15290. struct map_session_data *sd;
  15291. int book_id, page;
  15292. if( (sd = script_rid2sd(st)) == NULL )
  15293. return 0;
  15294. book_id = script_getnum(st,2);
  15295. page = script_getnum(st,3);
  15296. clif_readbook(sd->fd, book_id, page);
  15297. return SCRIPT_CMD_SUCCESS;
  15298. }
  15299. /******************
  15300. Questlog script commands
  15301. *******************/
  15302. BUILDIN_FUNC(questinfo)
  15303. {
  15304. TBL_NPC* nd = map_id2nd(st->oid);
  15305. int quest_id, icon;
  15306. struct questinfo qi;
  15307. if( nd == NULL || nd->bl.m == -1 )
  15308. return true;
  15309. quest_id = script_getnum(st, 2);
  15310. icon = script_getnum(st, 3);
  15311. #if PACKETVER >= 20120410
  15312. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  15313. icon = 9999; // Default to nothing if icon id is invalid.
  15314. #else
  15315. if(icon < 0 || icon > 7)
  15316. icon = 0;
  15317. else
  15318. icon = icon + 1;
  15319. #endif
  15320. qi.quest_id = quest_id;
  15321. qi.icon = (unsigned char)icon;
  15322. qi.nd = nd;
  15323. if( script_hasdata(st, 4) ) {
  15324. int color = script_getnum(st, 4);
  15325. if( color < 0 || color > 3 ) {
  15326. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  15327. script_reportfunc(st);
  15328. color = 0;
  15329. }
  15330. qi.color = (unsigned char)color;
  15331. }
  15332. qi.hasJob = false;
  15333. if(script_hasdata(st, 5)) {
  15334. int job = script_getnum(st, 5);
  15335. if (!pcdb_checkid(job))
  15336. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  15337. else {
  15338. qi.hasJob = true;
  15339. qi.job = (unsigned short)job;
  15340. }
  15341. }
  15342. map_add_questinfo(nd->bl.m,&qi);
  15343. return true;
  15344. }
  15345. BUILDIN_FUNC(setquest)
  15346. {
  15347. struct map_session_data *sd = script_rid2sd(st);
  15348. unsigned short i;
  15349. int quest_id;
  15350. nullpo_retr(1, sd);
  15351. quest_id = script_getnum(st, 2);
  15352. quest_add(sd, quest_id);
  15353. // If questinfo is set, remove quest bubble once quest is set.
  15354. for(i = 0; i < map[sd->bl.m].qi_count; i++) {
  15355. struct questinfo *qi = &map[sd->bl.m].qi_data[i];
  15356. if( qi->quest_id == quest_id ) {
  15357. #if PACKETVER >= 20120410
  15358. clif_quest_show_event(sd, &qi->nd->bl, 9999, 0);
  15359. #else
  15360. clif_quest_show_event(sd, &qi->nd->bl, 0, 0);
  15361. #endif
  15362. }
  15363. }
  15364. return SCRIPT_CMD_SUCCESS;
  15365. }
  15366. BUILDIN_FUNC(erasequest)
  15367. {
  15368. struct map_session_data *sd = script_rid2sd(st);
  15369. nullpo_ret(sd);
  15370. quest_delete(sd, script_getnum(st, 2));
  15371. return SCRIPT_CMD_SUCCESS;
  15372. }
  15373. BUILDIN_FUNC(completequest)
  15374. {
  15375. struct map_session_data *sd = script_rid2sd(st);
  15376. nullpo_ret(sd);
  15377. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  15378. return SCRIPT_CMD_SUCCESS;
  15379. }
  15380. BUILDIN_FUNC(changequest)
  15381. {
  15382. struct map_session_data *sd = script_rid2sd(st);
  15383. nullpo_ret(sd);
  15384. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  15385. return SCRIPT_CMD_SUCCESS;
  15386. }
  15387. BUILDIN_FUNC(checkquest)
  15388. {
  15389. struct map_session_data *sd = script_rid2sd(st);
  15390. enum quest_check_type type = HAVEQUEST;
  15391. nullpo_ret(sd);
  15392. if( script_hasdata(st, 3) )
  15393. type = (enum quest_check_type)script_getnum(st, 3);
  15394. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  15395. return SCRIPT_CMD_SUCCESS;
  15396. }
  15397. BUILDIN_FUNC(isbegin_quest)
  15398. {
  15399. struct map_session_data *sd = script_rid2sd(st);
  15400. int i;
  15401. nullpo_ret(sd);
  15402. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  15403. script_pushint(st, i + (i < 1));
  15404. return SCRIPT_CMD_SUCCESS;
  15405. }
  15406. BUILDIN_FUNC(showevent)
  15407. {
  15408. TBL_PC *sd = script_rid2sd(st);
  15409. struct npc_data *nd = map_id2nd(st->oid);
  15410. int icon, color = 0;
  15411. if( sd == NULL || nd == NULL )
  15412. return 0;
  15413. icon = script_getnum(st, 2);
  15414. if( script_hasdata(st, 3) ) {
  15415. color = script_getnum(st, 3);
  15416. if( color < 0 || color > 3 ) {
  15417. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  15418. script_reportfunc(st);
  15419. color = 0;
  15420. }
  15421. }
  15422. #if PACKETVER >= 20120410
  15423. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  15424. icon = 9999; // Default to nothing if icon id is invalid.
  15425. #else
  15426. if(icon < 0 || icon > 7)
  15427. icon = 0;
  15428. else
  15429. icon = icon + 1;
  15430. #endif
  15431. clif_quest_show_event(sd, &nd->bl, icon, color);
  15432. return SCRIPT_CMD_SUCCESS;
  15433. }
  15434. /*==========================================
  15435. * BattleGround System
  15436. *------------------------------------------*/
  15437. BUILDIN_FUNC(waitingroom2bg)
  15438. {
  15439. struct npc_data *nd;
  15440. struct chat_data *cd;
  15441. const char *map_name, *ev = "", *dev = "";
  15442. int x, y, mapindex = 0, bg_id, n;
  15443. unsigned char i;
  15444. if( script_hasdata(st,7) )
  15445. nd = npc_name2id(script_getstr(st,7));
  15446. else
  15447. nd = (struct npc_data *)map_id2bl(st->oid);
  15448. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  15449. {
  15450. script_pushint(st,0);
  15451. return 0;
  15452. }
  15453. map_name = script_getstr(st,2);
  15454. if( strcmp(map_name,"-") != 0 )
  15455. {
  15456. mapindex = mapindex_name2id(map_name);
  15457. if( mapindex == 0 )
  15458. { // Invalid Map
  15459. script_pushint(st,0);
  15460. return 0;
  15461. }
  15462. }
  15463. x = script_getnum(st,3);
  15464. y = script_getnum(st,4);
  15465. ev = script_getstr(st,5); // Logout Event
  15466. dev = script_getstr(st,6); // Die Event
  15467. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  15468. { // Creation failed
  15469. script_pushint(st,0);
  15470. return 0;
  15471. }
  15472. n = cd->users;
  15473. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  15474. {
  15475. struct map_session_data *sd;
  15476. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  15477. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), sd->bl.id);
  15478. else
  15479. mapreg_setreg(reference_uid(add_str("$@arenamembers"), i), 0);
  15480. }
  15481. mapreg_setreg(add_str("$@arenamembersnum"), i);
  15482. script_pushint(st,bg_id);
  15483. return SCRIPT_CMD_SUCCESS;
  15484. }
  15485. BUILDIN_FUNC(waitingroom2bg_single)
  15486. {
  15487. const char* map_name;
  15488. struct npc_data *nd;
  15489. struct chat_data *cd;
  15490. struct map_session_data *sd;
  15491. int x, y, mapindex, bg_id;
  15492. bg_id = script_getnum(st,2);
  15493. map_name = script_getstr(st,3);
  15494. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  15495. return 0; // Invalid Map
  15496. x = script_getnum(st,4);
  15497. y = script_getnum(st,5);
  15498. nd = npc_name2id(script_getstr(st,6));
  15499. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  15500. return 0;
  15501. if( (sd = cd->usersd[0]) == NULL )
  15502. return 0;
  15503. if( bg_team_join(bg_id, sd) )
  15504. {
  15505. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  15506. script_pushint(st,1);
  15507. }
  15508. else
  15509. script_pushint(st,0);
  15510. return SCRIPT_CMD_SUCCESS;
  15511. }
  15512. BUILDIN_FUNC(bg_team_setxy)
  15513. {
  15514. struct battleground_data *bg;
  15515. int bg_id;
  15516. bg_id = script_getnum(st,2);
  15517. if( (bg = bg_team_search(bg_id)) == NULL )
  15518. return 0;
  15519. bg->x = script_getnum(st,3);
  15520. bg->y = script_getnum(st,4);
  15521. return SCRIPT_CMD_SUCCESS;
  15522. }
  15523. BUILDIN_FUNC(bg_warp)
  15524. {
  15525. int x, y, mapindex, bg_id;
  15526. const char* map_name;
  15527. bg_id = script_getnum(st,2);
  15528. map_name = script_getstr(st,3);
  15529. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  15530. return 0; // Invalid Map
  15531. x = script_getnum(st,4);
  15532. y = script_getnum(st,5);
  15533. bg_team_warp(bg_id, mapindex, x, y);
  15534. return SCRIPT_CMD_SUCCESS;
  15535. }
  15536. BUILDIN_FUNC(bg_monster)
  15537. {
  15538. int class_ = 0, x = 0, y = 0, bg_id = 0;
  15539. const char *str,*mapname, *evt="";
  15540. bg_id = script_getnum(st,2);
  15541. mapname = script_getstr(st,3);
  15542. x = script_getnum(st,4);
  15543. y = script_getnum(st,5);
  15544. str = script_getstr(st,6);
  15545. class_ = script_getnum(st,7);
  15546. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  15547. check_event(st, evt);
  15548. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  15549. return SCRIPT_CMD_SUCCESS;
  15550. }
  15551. BUILDIN_FUNC(bg_monster_set_team)
  15552. {
  15553. struct mob_data *md;
  15554. struct block_list *mbl;
  15555. int id = script_getnum(st,2),
  15556. bg_id = script_getnum(st,3);
  15557. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  15558. return 0;
  15559. md = (TBL_MOB *)mbl;
  15560. md->bg_id = bg_id;
  15561. mob_stop_attack(md);
  15562. mob_stop_walking(md, 0);
  15563. md->target_id = md->attacked_id = 0;
  15564. clif_charnameack(0, &md->bl);
  15565. return SCRIPT_CMD_SUCCESS;
  15566. }
  15567. BUILDIN_FUNC(bg_leave)
  15568. {
  15569. struct map_session_data *sd = script_rid2sd(st);
  15570. if( sd == NULL || !sd->bg_id )
  15571. return 0;
  15572. bg_team_leave(sd,0);
  15573. return SCRIPT_CMD_SUCCESS;
  15574. }
  15575. BUILDIN_FUNC(bg_destroy)
  15576. {
  15577. int bg_id = script_getnum(st,2);
  15578. bg_team_delete(bg_id);
  15579. return SCRIPT_CMD_SUCCESS;
  15580. }
  15581. BUILDIN_FUNC(bg_getareausers)
  15582. {
  15583. const char *str;
  15584. int16 m, x0, y0, x1, y1;
  15585. int bg_id;
  15586. int i = 0, c = 0;
  15587. struct battleground_data *bg = NULL;
  15588. bg_id = script_getnum(st,2);
  15589. str = script_getstr(st,3);
  15590. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  15591. {
  15592. script_pushint(st,0);
  15593. return 0;
  15594. }
  15595. x0 = script_getnum(st,4);
  15596. y0 = script_getnum(st,5);
  15597. x1 = script_getnum(st,6);
  15598. y1 = script_getnum(st,7);
  15599. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  15600. {
  15601. struct map_session_data *sd;
  15602. if( (sd = bg->members[i].sd) == NULL )
  15603. continue;
  15604. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  15605. continue;
  15606. c++;
  15607. }
  15608. script_pushint(st,c);
  15609. return SCRIPT_CMD_SUCCESS;
  15610. }
  15611. BUILDIN_FUNC(bg_updatescore)
  15612. {
  15613. const char *str;
  15614. int16 m;
  15615. str = script_getstr(st,2);
  15616. if( (m = map_mapname2mapid(str)) < 0 )
  15617. return 0;
  15618. map[m].bgscore_lion = script_getnum(st,3);
  15619. map[m].bgscore_eagle = script_getnum(st,4);
  15620. clif_bg_updatescore(m);
  15621. return SCRIPT_CMD_SUCCESS;
  15622. }
  15623. BUILDIN_FUNC(bg_get_data)
  15624. {
  15625. struct battleground_data *bg;
  15626. int bg_id = script_getnum(st,2),
  15627. type = script_getnum(st,3);
  15628. if( (bg = bg_team_search(bg_id)) == NULL )
  15629. {
  15630. script_pushint(st,0);
  15631. return 0;
  15632. }
  15633. switch( type )
  15634. {
  15635. case 0: script_pushint(st, bg->count); break;
  15636. default:
  15637. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  15638. break;
  15639. }
  15640. return SCRIPT_CMD_SUCCESS;
  15641. }
  15642. /*==========================================
  15643. * Instancing System
  15644. *------------------------------------------*/
  15645. //Returns an Instance ID
  15646. //Checks NPC first, then if player is attached we check party
  15647. int script_instancegetid(struct script_state* st)
  15648. {
  15649. short instance_id = 0;
  15650. struct npc_data *nd;
  15651. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  15652. instance_id = nd->instance_id;
  15653. else {
  15654. struct map_session_data *sd;
  15655. struct party_data *p;
  15656. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  15657. instance_id = p->instance_id;
  15658. }
  15659. return instance_id;
  15660. }
  15661. /*==========================================
  15662. * Creates the instance
  15663. * Returns Instance ID if created successfully
  15664. *------------------------------------------*/
  15665. BUILDIN_FUNC(instance_create)
  15666. {
  15667. struct map_session_data *sd;
  15668. if((sd = script_rid2sd(st)) == NULL)
  15669. return -1;
  15670. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  15671. return SCRIPT_CMD_SUCCESS;
  15672. }
  15673. /*==========================================
  15674. * Destroys an instance (unofficial)
  15675. * Officially instances are only destroyed by timeout
  15676. *
  15677. * instance_destroy {<instance_id>};
  15678. *------------------------------------------*/
  15679. BUILDIN_FUNC(instance_destroy)
  15680. {
  15681. short instance_id;
  15682. if( script_hasdata(st,2) )
  15683. instance_id = script_getnum(st,2);
  15684. else
  15685. instance_id = script_instancegetid(st);
  15686. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  15687. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  15688. return 0;
  15689. }
  15690. instance_destroy(instance_id);
  15691. return SCRIPT_CMD_SUCCESS;
  15692. }
  15693. /*==========================================
  15694. * Warps player to instance
  15695. * Results:
  15696. * 0: Success
  15697. * 1: Character not in party
  15698. * 2: Party doesn't have instance
  15699. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  15700. *------------------------------------------*/
  15701. BUILDIN_FUNC(instance_enter)
  15702. {
  15703. struct map_session_data *sd;
  15704. if((sd = script_rid2sd(st)) != NULL){
  15705. if (script_hasdata(st,3) && script_hasdata(st,4))
  15706. script_pushint(st,instance_enter_position(sd,script_getstr(st, 2),script_getnum(st, 3),script_getnum(st, 4)));
  15707. else
  15708. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  15709. }
  15710. else
  15711. return 1;
  15712. return SCRIPT_CMD_SUCCESS;
  15713. }
  15714. /*==========================================
  15715. * Returns the name of a duplicated NPC
  15716. *
  15717. * instance_npcname <npc_name>{,<instance_id>};
  15718. * <npc_name> is the full name of an NPC.
  15719. *------------------------------------------*/
  15720. BUILDIN_FUNC(instance_npcname)
  15721. {
  15722. const char *str;
  15723. short instance_id = 0;
  15724. struct npc_data *nd;
  15725. str = script_getstr(st,2);
  15726. if( script_hasdata(st,3) )
  15727. instance_id = script_getnum(st,3);
  15728. else
  15729. instance_id = script_instancegetid(st);
  15730. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  15731. static char npcname[NAME_LENGTH];
  15732. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  15733. script_pushconststr(st,npcname);
  15734. } else {
  15735. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  15736. st->state = END;
  15737. return 1;
  15738. }
  15739. return SCRIPT_CMD_SUCCESS;
  15740. }
  15741. /*==========================================
  15742. * Returns the name of a duplicated map
  15743. *
  15744. * instance_mapname <map_name>{,<instance_id>};
  15745. *------------------------------------------*/
  15746. BUILDIN_FUNC(instance_mapname)
  15747. {
  15748. const char *str;
  15749. int16 m;
  15750. short instance_id = 0;
  15751. str = script_getstr(st,2);
  15752. if( script_hasdata(st,3) )
  15753. instance_id = script_getnum(st,3);
  15754. else
  15755. instance_id = script_instancegetid(st);
  15756. // Check that instance mapname is a valid map
  15757. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  15758. script_pushconststr(st, "");
  15759. else
  15760. script_pushconststr(st, map[m].name);
  15761. return SCRIPT_CMD_SUCCESS;
  15762. }
  15763. /*==========================================
  15764. * Returns an Instance ID
  15765. *------------------------------------------*/
  15766. BUILDIN_FUNC(instance_id)
  15767. {
  15768. short instance_id;
  15769. instance_id = script_instancegetid(st);
  15770. if(!instance_id) {
  15771. //ShowError("script:instance_id: No instance attached to NPC or player");
  15772. script_pushint(st, 0);
  15773. return 1;
  15774. }
  15775. script_pushint(st, instance_id);
  15776. return SCRIPT_CMD_SUCCESS;
  15777. }
  15778. /*==========================================
  15779. * Warps all players inside an instance
  15780. *
  15781. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  15782. *------------------------------------------*/
  15783. BUILDIN_FUNC(instance_warpall)
  15784. {
  15785. struct party_data *p;
  15786. int16 m, i;
  15787. short instance_id;
  15788. const char *mapn;
  15789. int x, y;
  15790. mapn = script_getstr(st,2);
  15791. x = script_getnum(st,3);
  15792. y = script_getnum(st,4);
  15793. if( script_hasdata(st,5) )
  15794. instance_id = script_getnum(st,5);
  15795. else
  15796. instance_id = script_instancegetid(st);
  15797. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  15798. return 0;
  15799. if( !(p = party_search(instance_data[instance_id].party_id)) )
  15800. return 0;
  15801. for( i = 0; i < MAX_PARTY; i++ ) {
  15802. struct map_session_data *pl_sd;
  15803. if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == instance_id ) pc_setpos(pl_sd,map_id2index(m),x,y,CLR_TELEPORT);
  15804. }
  15805. return SCRIPT_CMD_SUCCESS;
  15806. }
  15807. /*==========================================
  15808. * Broadcasts to all maps inside an instance
  15809. *
  15810. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  15811. * Using -1 for <instance id> will auto-detect the id.
  15812. *------------------------------------------*/
  15813. BUILDIN_FUNC(instance_announce) {
  15814. int instance_id = script_getnum(st,2);
  15815. const char *mes = script_getstr(st,3);
  15816. int flag = script_getnum(st,4);
  15817. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  15818. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : 0x190; // default fontType (FW_NORMAL)
  15819. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  15820. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  15821. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  15822. int i;
  15823. if( instance_id == -1 ) {
  15824. instance_id = script_instancegetid(st);
  15825. }
  15826. if( !instance_id && &instance_data[instance_id] != NULL)
  15827. return true;
  15828. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  15829. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  15830. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  15831. return SCRIPT_CMD_SUCCESS;
  15832. }
  15833. /*==========================================
  15834. * instance_check_party [malufett]
  15835. * Values:
  15836. * party_id : Party ID of the invoking character. [Required Parameter]
  15837. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  15838. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  15839. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  15840. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  15841. * Example 2: instance_check_party (getcharid(1),1,1,99);
  15842. *------------------------------------------*/
  15843. BUILDIN_FUNC(instance_check_party)
  15844. {
  15845. int amount, min, max, i, party_id, c = 0;
  15846. struct party_data *p = NULL;
  15847. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  15848. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  15849. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  15850. if( min < 1 || min > MAX_LEVEL) {
  15851. ShowError("script:check_party: Invalid min level, %d\n", min);
  15852. return 1;
  15853. } else if( max < 1 || max > MAX_LEVEL) {
  15854. ShowError("script:check_party: Invalid max level, %d\n", max);
  15855. return 1;
  15856. }
  15857. if( script_hasdata(st,2) )
  15858. party_id = script_getnum(st,2);
  15859. else return 0;
  15860. if( !(p = party_search(party_id)) ) {
  15861. script_pushint(st, 0); // Returns false if party does not exist.
  15862. return 0;
  15863. }
  15864. for( i = 0; i < MAX_PARTY; i++ ) {
  15865. struct map_session_data *pl_sd;
  15866. if( (pl_sd = p->data[i].sd) )
  15867. if(map_id2bl(pl_sd->bl.id)) {
  15868. if(pl_sd->status.base_level < min) {
  15869. script_pushint(st, 0);
  15870. return 0;
  15871. } else if(pl_sd->status.base_level > max) {
  15872. script_pushint(st, 0);
  15873. return 0;
  15874. }
  15875. c++;
  15876. }
  15877. }
  15878. if(c < amount)
  15879. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  15880. else
  15881. script_pushint(st, 1);
  15882. return SCRIPT_CMD_SUCCESS;
  15883. }
  15884. /*==========================================
  15885. * Custom Fonts
  15886. *------------------------------------------*/
  15887. BUILDIN_FUNC(setfont)
  15888. {
  15889. struct map_session_data *sd = script_rid2sd(st);
  15890. int font = script_getnum(st,2);
  15891. if( sd == NULL )
  15892. return 0;
  15893. if( sd->status.font != font )
  15894. sd->status.font = font;
  15895. else
  15896. sd->status.font = 0;
  15897. clif_font(sd);
  15898. return SCRIPT_CMD_SUCCESS;
  15899. }
  15900. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  15901. {
  15902. TBL_MOB* md = (TBL_MOB*)bl;
  15903. struct block_list *tbl;
  15904. int mobid = va_arg(ap,int);
  15905. uint16 skill_id = va_arg(ap,int);
  15906. uint16 skill_lv = va_arg(ap,int);
  15907. int casttime = va_arg(ap,int);
  15908. int cancel = va_arg(ap,int);
  15909. int emotion = va_arg(ap,int);
  15910. int target = va_arg(ap,int);
  15911. if( md->mob_id != mobid )
  15912. return 0;
  15913. // 0:self, 1:target, 2:master, default:random
  15914. switch( target ) {
  15915. case 0: tbl = map_id2bl(md->bl.id); break;
  15916. case 1: tbl = map_id2bl(md->target_id); break;
  15917. case 2: tbl = map_id2bl(md->master_id); break;
  15918. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  15919. }
  15920. if( !tbl )
  15921. return 0;
  15922. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  15923. unit_skillcastcancel(bl,0);
  15924. if( skill_get_casttype(skill_id) == CAST_GROUND )
  15925. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  15926. else
  15927. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  15928. clif_emotion(&md->bl, emotion);
  15929. return SCRIPT_CMD_SUCCESS;
  15930. }
  15931. /*==========================================
  15932. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  15933. *------------------------------------------*/
  15934. BUILDIN_FUNC(areamobuseskill)
  15935. {
  15936. struct block_list center;
  15937. struct script_data *data;
  15938. int16 m;
  15939. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  15940. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  15941. ShowError("areamobuseskill: invalid map name.\n");
  15942. return 0;
  15943. }
  15944. center.m = m;
  15945. center.x = script_getnum(st,3);
  15946. center.y = script_getnum(st,4);
  15947. range = script_getnum(st,5);
  15948. mobid = script_getnum(st,6);
  15949. data = script_getdata(st, 7);
  15950. get_val(st, data); // Convert into value in case of a variable
  15951. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  15952. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  15953. skill_lv = battle_config.mob_max_skilllvl;
  15954. casttime = script_getnum(st,9);
  15955. cancel = script_getnum(st,10);
  15956. emotion = script_getnum(st,11);
  15957. target = script_getnum(st,12);
  15958. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  15959. return SCRIPT_CMD_SUCCESS;
  15960. }
  15961. BUILDIN_FUNC(progressbar)
  15962. {
  15963. struct map_session_data * sd = script_rid2sd(st);
  15964. const char * color;
  15965. unsigned int second;
  15966. if( !st || !sd )
  15967. return 0;
  15968. st->state = STOP;
  15969. color = script_getstr(st,2);
  15970. second = script_getnum(st,3);
  15971. sd->progressbar.npc_id = st->oid;
  15972. sd->progressbar.timeout = gettick() + second*1000;
  15973. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  15974. return SCRIPT_CMD_SUCCESS;
  15975. }
  15976. BUILDIN_FUNC(pushpc)
  15977. {
  15978. uint8 dir;
  15979. int cells, dx, dy;
  15980. struct map_session_data* sd;
  15981. if((sd = script_rid2sd(st))==NULL)
  15982. {
  15983. return 0;
  15984. }
  15985. dir = script_getnum(st,2);
  15986. cells = script_getnum(st,3);
  15987. if(dir>7)
  15988. {
  15989. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  15990. script_reportsrc(st);
  15991. dir%= 8; // trim spin-over
  15992. }
  15993. if(!cells)
  15994. {// zero distance
  15995. return 0;
  15996. }
  15997. else if(cells<0)
  15998. {// pushing backwards
  15999. dir = (dir+4)%8; // turn around
  16000. cells = -cells;
  16001. }
  16002. dx = dirx[dir];
  16003. dy = diry[dir];
  16004. unit_blown(&sd->bl, dx, dy, cells, 0);
  16005. return SCRIPT_CMD_SUCCESS;
  16006. }
  16007. /// Invokes buying store preparation window
  16008. /// buyingstore <slots>;
  16009. BUILDIN_FUNC(buyingstore)
  16010. {
  16011. struct map_session_data* sd;
  16012. if( ( sd = script_rid2sd(st) ) == NULL )
  16013. {
  16014. return 0;
  16015. }
  16016. if( npc_isnear(&sd->bl) ) {
  16017. char output[150];
  16018. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  16019. clif_displaymessage(sd->fd, output);
  16020. return 0;
  16021. }
  16022. buyingstore_setup(sd, script_getnum(st,2));
  16023. return SCRIPT_CMD_SUCCESS;
  16024. }
  16025. /// Invokes search store info window
  16026. /// searchstores <uses>,<effect>;
  16027. BUILDIN_FUNC(searchstores)
  16028. {
  16029. unsigned short effect;
  16030. unsigned int uses;
  16031. struct map_session_data* sd;
  16032. if( ( sd = script_rid2sd(st) ) == NULL )
  16033. {
  16034. return 0;
  16035. }
  16036. uses = script_getnum(st,2);
  16037. effect = script_getnum(st,3);
  16038. if( !uses )
  16039. {
  16040. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  16041. return 1;
  16042. }
  16043. if( effect > 1 )
  16044. {
  16045. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  16046. return 1;
  16047. }
  16048. searchstore_open(sd, uses, effect);
  16049. return SCRIPT_CMD_SUCCESS;
  16050. }
  16051. /// Displays a number as large digital clock.
  16052. /// showdigit <value>[,<type>];
  16053. BUILDIN_FUNC(showdigit)
  16054. {
  16055. unsigned int type = 0;
  16056. int value;
  16057. struct map_session_data* sd;
  16058. if( ( sd = script_rid2sd(st) ) == NULL )
  16059. {
  16060. return 0;
  16061. }
  16062. value = script_getnum(st,2);
  16063. if( script_hasdata(st,3) )
  16064. {
  16065. type = script_getnum(st,3);
  16066. if( type > 3 )
  16067. {
  16068. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  16069. return 1;
  16070. }
  16071. }
  16072. clif_showdigit(sd, (unsigned char)type, value);
  16073. return SCRIPT_CMD_SUCCESS;
  16074. }
  16075. /**
  16076. * Rune Knight
  16077. **/
  16078. BUILDIN_FUNC(makerune) {
  16079. TBL_PC* sd;
  16080. if( (sd = script_rid2sd(st)) == NULL )
  16081. return 0;
  16082. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  16083. sd->itemid = script_getnum(st,2);
  16084. return SCRIPT_CMD_SUCCESS;
  16085. }
  16086. /**
  16087. * checkdragon() returns 1 if mounting a dragon or 0 otherwise.
  16088. **/
  16089. BUILDIN_FUNC(checkdragon) {
  16090. TBL_PC* sd;
  16091. if( (sd = script_rid2sd(st)) == NULL )
  16092. return 0;
  16093. if( pc_isridingdragon(sd) )
  16094. script_pushint(st,1);
  16095. else
  16096. script_pushint(st,0);
  16097. return SCRIPT_CMD_SUCCESS;
  16098. }
  16099. /**
  16100. * setdragon({optional Color}) returns 1 on success or 0 otherwise
  16101. * - Toggles the dragon on a RK if he can mount;
  16102. * @param Color - when not provided uses the green dragon;
  16103. * - 1 : Green Dragon
  16104. * - 2 : Brown Dragon
  16105. * - 3 : Gray Dragon
  16106. * - 4 : Blue Dragon
  16107. * - 5 : Red Dragon
  16108. **/
  16109. BUILDIN_FUNC(setdragon) {
  16110. TBL_PC* sd;
  16111. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  16112. if( (sd = script_rid2sd(st)) == NULL )
  16113. return 0;
  16114. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  16115. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  16116. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  16117. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  16118. script_pushint(st,1);
  16119. } else {//Not mounted; Mount now.
  16120. unsigned int option = OPTION_DRAGON1;
  16121. if( color ) {
  16122. option = ( color == 1 ? OPTION_DRAGON1 :
  16123. color == 2 ? OPTION_DRAGON2 :
  16124. color == 3 ? OPTION_DRAGON3 :
  16125. color == 4 ? OPTION_DRAGON4 :
  16126. color == 5 ? OPTION_DRAGON5 : 0);
  16127. if( !option ) {
  16128. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  16129. option = OPTION_DRAGON1;
  16130. }
  16131. }
  16132. pc_setoption(sd, sd->sc.option|option);
  16133. script_pushint(st,1);
  16134. }
  16135. return SCRIPT_CMD_SUCCESS;
  16136. }
  16137. /**
  16138. * ismounting() returns 1 if mounting a new mount or 0 otherwise
  16139. **/
  16140. BUILDIN_FUNC(ismounting) {
  16141. TBL_PC* sd;
  16142. if( (sd = script_rid2sd(st)) == NULL )
  16143. return 0;
  16144. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16145. script_pushint(st,1);
  16146. else
  16147. script_pushint(st,0);
  16148. return SCRIPT_CMD_SUCCESS;
  16149. }
  16150. /**
  16151. * setmounting() returns 1 on success or 0 otherwise
  16152. * - Toggles new mounts on a player when he can mount
  16153. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  16154. * - Will unmount the player is he is already mounting
  16155. **/
  16156. BUILDIN_FUNC(setmounting) {
  16157. TBL_PC* sd;
  16158. if( (sd = script_rid2sd(st)) == NULL )
  16159. return 0;
  16160. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  16161. clif_msgtable(sd->fd, NEED_REINS_OF_MOUNT);
  16162. script_pushint(st,0); //can't mount with one of these
  16163. } else {
  16164. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  16165. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  16166. else
  16167. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  16168. script_pushint(st,1);//in both cases, return 1.
  16169. }
  16170. return SCRIPT_CMD_SUCCESS;
  16171. }
  16172. /**
  16173. * Retrieves quantity of arguments provided to callfunc/callsub.
  16174. * getargcount() -> amount of arguments received in a function
  16175. **/
  16176. BUILDIN_FUNC(getargcount) {
  16177. struct script_retinfo* ri;
  16178. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  16179. ShowError("script:getargcount: used out of function or callsub label!\n");
  16180. st->state = END;
  16181. return 1;
  16182. }
  16183. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  16184. script_pushint(st, ri->nargs);
  16185. return SCRIPT_CMD_SUCCESS;
  16186. }
  16187. /**
  16188. * getcharip(<account ID>/<character ID>/<character name>)
  16189. **/
  16190. BUILDIN_FUNC(getcharip)
  16191. {
  16192. struct map_session_data* sd = NULL;
  16193. /* check if a character name is specified */
  16194. if( script_hasdata(st, 2) )
  16195. {
  16196. struct script_data *data;
  16197. data = script_getdata(st, 2);
  16198. get_val(st, data); // Convert into value in case of a variable
  16199. if (data_isstring(data))
  16200. sd = map_nick2sd(script_getstr(st, 2));
  16201. else if (data_isint(data) || script_getnum(st, 2))
  16202. {
  16203. int id = 0;
  16204. id = script_getnum(st, 2);
  16205. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  16206. }
  16207. }
  16208. else
  16209. sd = script_rid2sd(st);
  16210. /* check for sd and IP */
  16211. if (!sd || !session[sd->fd]->client_addr)
  16212. {
  16213. script_pushconststr(st, "");
  16214. return 0;
  16215. }
  16216. /* return the client ip_addr converted for output */
  16217. if (sd && sd->fd && session[sd->fd])
  16218. {
  16219. /* initiliaze */
  16220. const char *ip_addr = NULL;
  16221. uint32 ip;
  16222. /* set ip, ip_addr and convert to ip and push str */
  16223. ip = session[sd->fd]->client_addr;
  16224. ip_addr = ip2str(ip, NULL);
  16225. script_pushstrcopy(st, ip_addr);
  16226. }
  16227. return SCRIPT_CMD_SUCCESS;
  16228. }
  16229. /**
  16230. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  16231. **/
  16232. BUILDIN_FUNC(is_function) {
  16233. const char* str = script_getstr(st,2);
  16234. if( strdb_exists(userfunc_db, str) )
  16235. script_pushint(st,1);
  16236. else
  16237. script_pushint(st,0);
  16238. return SCRIPT_CMD_SUCCESS;
  16239. }
  16240. /**
  16241. * get_revision() -> retrieves the current svn revision (if available)
  16242. **/
  16243. BUILDIN_FUNC(get_revision) {
  16244. const char *svn = get_svn_revision();
  16245. if ( svn[0] != UNKNOWN_VERSION )
  16246. script_pushint(st,atoi(svn));
  16247. else
  16248. script_pushint(st,-1); //unknown
  16249. return SCRIPT_CMD_SUCCESS;
  16250. }
  16251. /* get_hash() -> retrieves the current git hash (if available)*/
  16252. BUILDIN_FUNC(get_githash) {
  16253. const char* git = get_git_hash();
  16254. char buf[CHAT_SIZE_MAX];
  16255. safestrncpy(buf,git,strlen(git)+1);
  16256. if ( git[0] != UNKNOWN_VERSION )
  16257. script_pushstr(st,buf);
  16258. else
  16259. script_pushstr(st,"Unknown"); //unknown
  16260. return SCRIPT_CMD_SUCCESS;
  16261. }
  16262. /**
  16263. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  16264. **/
  16265. BUILDIN_FUNC(freeloop) {
  16266. if( script_hasdata(st,2) ) {
  16267. if( script_getnum(st,2) )
  16268. st->freeloop = 1;
  16269. else
  16270. st->freeloop = 0;
  16271. }
  16272. script_pushint(st, st->freeloop);
  16273. return SCRIPT_CMD_SUCCESS;
  16274. }
  16275. /**
  16276. * @commands (script based)
  16277. **/
  16278. BUILDIN_FUNC(bindatcmd) {
  16279. const char* atcmd;
  16280. const char* eventName;
  16281. int i, level = 0, level2 = 0;
  16282. bool create = false;
  16283. atcmd = script_getstr(st,2);
  16284. eventName = script_getstr(st,3);
  16285. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  16286. atcmd++;
  16287. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  16288. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  16289. if( atcmd_binding_count == 0 ) {
  16290. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  16291. create = true;
  16292. } else {
  16293. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  16294. if( i < atcmd_binding_count ) {/* update existent entry */
  16295. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  16296. atcmd_binding[i]->level = level;
  16297. atcmd_binding[i]->level2 = level2;
  16298. } else
  16299. create = true;
  16300. }
  16301. if( create ) {
  16302. i = atcmd_binding_count;
  16303. if( atcmd_binding_count++ != 0 )
  16304. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  16305. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  16306. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  16307. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  16308. atcmd_binding[i]->level = level;
  16309. atcmd_binding[i]->level2 = level2;
  16310. }
  16311. return SCRIPT_CMD_SUCCESS;
  16312. }
  16313. BUILDIN_FUNC(unbindatcmd) {
  16314. const char* atcmd;
  16315. int i = 0;
  16316. atcmd = script_getstr(st, 2);
  16317. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  16318. atcmd++;
  16319. if( atcmd_binding_count == 0 ) {
  16320. script_pushint(st, 0);
  16321. return 0;
  16322. }
  16323. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  16324. if( i < atcmd_binding_count ) {
  16325. int cursor = 0;
  16326. aFree(atcmd_binding[i]);
  16327. atcmd_binding[i] = NULL;
  16328. /* compact the list now that we freed a slot somewhere */
  16329. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  16330. if( atcmd_binding[i] == NULL )
  16331. continue;
  16332. if( cursor != i ) {
  16333. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  16334. }
  16335. cursor++;
  16336. }
  16337. if( (atcmd_binding_count = cursor) == 0 )
  16338. aFree(atcmd_binding);
  16339. script_pushint(st, 1);
  16340. } else
  16341. script_pushint(st, 0);/* not found */
  16342. return SCRIPT_CMD_SUCCESS;
  16343. }
  16344. BUILDIN_FUNC(useatcmd) {
  16345. return atcommand_sub(st,3);
  16346. }
  16347. BUILDIN_FUNC(checkre)
  16348. {
  16349. int num;
  16350. num=script_getnum(st,2);
  16351. switch(num){
  16352. case 0:
  16353. #ifdef RENEWAL
  16354. script_pushint(st, 1);
  16355. #else
  16356. script_pushint(st, 0);
  16357. #endif
  16358. break;
  16359. case 1:
  16360. #ifdef RENEWAL_CAST
  16361. script_pushint(st, 1);
  16362. #else
  16363. script_pushint(st, 0);
  16364. #endif
  16365. break;
  16366. case 2:
  16367. #ifdef RENEWAL_DROP
  16368. script_pushint(st, 1);
  16369. #else
  16370. script_pushint(st, 0);
  16371. #endif
  16372. break;
  16373. case 3:
  16374. #ifdef RENEWAL_EXP
  16375. script_pushint(st, 1);
  16376. #else
  16377. script_pushint(st, 0);
  16378. #endif
  16379. break;
  16380. case 4:
  16381. #ifdef RENEWAL_LVDMG
  16382. script_pushint(st, 1);
  16383. #else
  16384. script_pushint(st, 0);
  16385. #endif
  16386. break;
  16387. case 5:
  16388. #ifdef RENEWAL_ASPD
  16389. script_pushint(st, 1);
  16390. #else
  16391. script_pushint(st, 0);
  16392. #endif
  16393. break;
  16394. default:
  16395. ShowWarning("buildin_checkre: unknown parameter.\n");
  16396. break;
  16397. }
  16398. return SCRIPT_CMD_SUCCESS;
  16399. }
  16400. /* getrandgroupitem <group_id>,<quantity>{,<sub_group>} */
  16401. BUILDIN_FUNC(getrandgroupitem) {
  16402. TBL_PC* sd;
  16403. int i, get_count = 0;
  16404. unsigned short nameid;
  16405. uint16 group = script_getnum(st,2), qty = script_getnum(st,3);
  16406. uint8 sub_group = script_getnum(st,4);
  16407. struct item item_tmp;
  16408. if (!( sd = script_rid2sd(st)))
  16409. return SCRIPT_CMD_SUCCESS;
  16410. if (!group) {
  16411. ShowError("getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  16412. return SCRIPT_CMD_FAILURE;
  16413. }
  16414. if ((nameid = itemdb_searchrandomid(group,sub_group)) == UNKNOWN_ITEM_ID) {
  16415. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  16416. }
  16417. memset(&item_tmp,0,sizeof(item_tmp));
  16418. item_tmp.nameid = nameid;
  16419. item_tmp.identify = itemdb_isidentified(nameid);
  16420. if (!qty)
  16421. qty = itemdb_get_randgroupitem_count(group,sub_group,nameid);
  16422. //Check if it's stackable.
  16423. if (!itemdb_isstackable(nameid)) {
  16424. item_tmp.amount = 1;
  16425. get_count = qty;
  16426. }
  16427. else {
  16428. item_tmp.amount = qty;
  16429. get_count = 1;
  16430. }
  16431. for (i = 0; i < get_count; i++) {
  16432. // if not pet egg
  16433. if (!pet_create_egg(sd, nameid)) {
  16434. unsigned char flag = 0;
  16435. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  16436. clif_additem(sd,0,0,flag);
  16437. if (pc_candrop(sd,&item_tmp))
  16438. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  16439. }
  16440. }
  16441. }
  16442. return SCRIPT_CMD_SUCCESS;
  16443. }
  16444. /* getgroupitem <group_id>;
  16445. * Gives item(s) to the attached player based on item group contents
  16446. */
  16447. BUILDIN_FUNC(getgroupitem) {
  16448. TBL_PC *sd;
  16449. int group_id = script_getnum(st,2);
  16450. if (!(sd = script_rid2sd(st)))
  16451. return SCRIPT_CMD_SUCCESS;
  16452. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  16453. ShowError("getgroupitem: Invalid group id '%d' specified.\n",group_id);
  16454. return SCRIPT_CMD_FAILURE;
  16455. }
  16456. return SCRIPT_CMD_SUCCESS;
  16457. }
  16458. /* cleanmap <map_name>;
  16459. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  16460. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  16461. {
  16462. nullpo_ret(bl);
  16463. map_clearflooritem(bl);
  16464. return SCRIPT_CMD_SUCCESS;
  16465. }
  16466. BUILDIN_FUNC(cleanmap)
  16467. {
  16468. const char *mapname;
  16469. int16 m;
  16470. int16 x0 = 0, y0 = 0, x1 = 0, y1 = 0;
  16471. mapname = script_getstr(st, 2);
  16472. m = map_mapname2mapid(mapname);
  16473. if (!m)
  16474. return 1;
  16475. if ((script_lastdata(st) - 2) < 4) {
  16476. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  16477. } else {
  16478. x0 = script_getnum(st, 3);
  16479. y0 = script_getnum(st, 4);
  16480. x1 = script_getnum(st, 5);
  16481. y1 = script_getnum(st, 6);
  16482. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  16483. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  16484. } else {
  16485. ShowError("cleanarea: invalid coordinate defined!\n");
  16486. return 1;
  16487. }
  16488. }
  16489. return SCRIPT_CMD_SUCCESS;
  16490. }
  16491. /* Cast a skill on the attached player.
  16492. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  16493. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  16494. BUILDIN_FUNC(npcskill)
  16495. {
  16496. uint16 skill_id;
  16497. unsigned short skill_level;
  16498. unsigned int stat_point;
  16499. unsigned int npc_level;
  16500. struct npc_data *nd;
  16501. struct map_session_data *sd;
  16502. struct script_data *data;
  16503. data = script_getdata(st, 2);
  16504. get_val(st, data); // Convert into value in case of a variable
  16505. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  16506. skill_level = script_getnum(st, 3);
  16507. stat_point = script_getnum(st, 4);
  16508. npc_level = script_getnum(st, 5);
  16509. sd = script_rid2sd(st);
  16510. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  16511. if (stat_point > battle_config.max_third_parameter) {
  16512. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  16513. return 1;
  16514. }
  16515. if (npc_level > MAX_LEVEL) {
  16516. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  16517. return 1;
  16518. }
  16519. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  16520. return 1;
  16521. }
  16522. nd->level = npc_level;
  16523. nd->stat_point = stat_point;
  16524. if (!nd->status.hp)
  16525. status_calc_npc(nd, SCO_FIRST);
  16526. else
  16527. status_calc_npc(nd, SCO_NONE);
  16528. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  16529. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  16530. else
  16531. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  16532. return SCRIPT_CMD_SUCCESS;
  16533. }
  16534. /* Consumes an item.
  16535. * consumeitem <item id>;
  16536. * consumeitem "<item name>";
  16537. * @param item: Item ID or name
  16538. */
  16539. BUILDIN_FUNC(consumeitem)
  16540. {
  16541. TBL_PC *sd;
  16542. struct script_data *data;
  16543. struct item_data *item_data;
  16544. nullpo_retr( 1, ( sd = script_rid2sd( st ) ) );
  16545. data = script_getdata( st, 2 );
  16546. get_val( st, data );
  16547. if( data_isstring( data ) ){
  16548. const char *name = conv_str( st, data );
  16549. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  16550. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  16551. return SCRIPT_CMD_FAILURE;
  16552. }
  16553. }else if( data_isint( data ) ){
  16554. unsigned short nameid = conv_num( st, data );
  16555. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  16556. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  16557. return SCRIPT_CMD_FAILURE;
  16558. }
  16559. }else{
  16560. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  16561. return SCRIPT_CMD_FAILURE;
  16562. }
  16563. run_script( item_data->script, 0, sd->bl.id, 0 );
  16564. return SCRIPT_CMD_SUCCESS;
  16565. }
  16566. /** Makes a player sit/stand.
  16567. * sit {"<character name>"};
  16568. * stand {"<character name>"};
  16569. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  16570. * @param name: Player name that will be invoked
  16571. */
  16572. BUILDIN_FUNC(sit)
  16573. {
  16574. TBL_PC *sd;
  16575. if( script_hasdata(st, 2) )
  16576. sd = map_nick2sd(script_getstr(st, 2));
  16577. else
  16578. sd = script_rid2sd(st);
  16579. if( sd == NULL)
  16580. return SCRIPT_CMD_FAILURE;
  16581. if( !pc_issit(sd) ) {
  16582. pc_setsit(sd);
  16583. skill_sit(sd, 1);
  16584. clif_sitting(&sd->bl);
  16585. }
  16586. return SCRIPT_CMD_SUCCESS;
  16587. }
  16588. /** Makes player to stand
  16589. * @param name: Player name that will be set
  16590. */
  16591. BUILDIN_FUNC(stand)
  16592. {
  16593. TBL_PC *sd;
  16594. if( script_hasdata(st, 2) )
  16595. sd = map_nick2sd(script_getstr(st, 2));
  16596. else
  16597. sd = script_rid2sd(st);
  16598. if( sd == NULL)
  16599. return SCRIPT_CMD_FAILURE;
  16600. if( pc_issit(sd) && pc_setstand(sd, false)) {
  16601. skill_sit(sd, 0);
  16602. clif_standing(&sd->bl);
  16603. }
  16604. return SCRIPT_CMD_SUCCESS;
  16605. }
  16606. /** Creates an array of bounded item IDs
  16607. * countbound {<type>};
  16608. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  16609. * @return amt: Amount of items found
  16610. */
  16611. BUILDIN_FUNC(countbound)
  16612. {
  16613. int i, type, j = 0, k = 0;
  16614. TBL_PC *sd;
  16615. if( (sd = script_rid2sd(st)) == NULL )
  16616. return SCRIPT_CMD_FAILURE;
  16617. type = script_getnum(st,2);
  16618. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  16619. if( sd->status.inventory[i].nameid > 0 && (
  16620. (!type && sd->status.inventory[i].bound) || (type && sd->status.inventory[i].bound == type)
  16621. ))
  16622. {
  16623. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  16624. k++;
  16625. j += sd->status.inventory[i].amount;
  16626. }
  16627. }
  16628. script_pushint(st,j);
  16629. return SCRIPT_CMD_SUCCESS;
  16630. }
  16631. /** Creates new party
  16632. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  16633. * @param party_name: String of party name that will be created
  16634. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  16635. * @param item_share: 0-Each Take. 1-Party Share
  16636. * @param item_share_type: 0-Each Take. 1-Even Share
  16637. * @return val: Result value
  16638. * -3 - party name is exist
  16639. * -2 - player is in party already
  16640. * -1 - player is not found
  16641. * 0 - unknown error
  16642. * 1 - success, will return party id $@party_create_id
  16643. */
  16644. BUILDIN_FUNC(party_create)
  16645. {
  16646. char party_name[NAME_LENGTH];
  16647. int item1 = 0, item2 = 0;
  16648. TBL_PC *sd;
  16649. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  16650. script_pushint(st,-1);
  16651. return SCRIPT_CMD_FAILURE;
  16652. }
  16653. if( sd->status.party_id ) {
  16654. script_pushint(st,-2);
  16655. return SCRIPT_CMD_FAILURE;
  16656. }
  16657. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  16658. trim(party_name);
  16659. if( party_searchname(party_name) ) {
  16660. script_pushint(st,-3);
  16661. return SCRIPT_CMD_FAILURE;
  16662. }
  16663. if( script_getnum(st,4) )
  16664. item1 = 1;
  16665. if( script_getnum(st,5) )
  16666. item2 = 1;
  16667. party_create_byscript = 1;
  16668. script_pushint(st,party_create(sd,party_name,item1,item2));
  16669. return SCRIPT_CMD_SUCCESS;
  16670. }
  16671. /** Adds player to specified party
  16672. * party_addmember <party id>,<char id>;
  16673. * @param party_id: The party that will be entered by player
  16674. * @param char_id: Char id of player that will be joined to the party
  16675. * @return val: Result value
  16676. * -4 - party is full
  16677. * -3 - party is not found
  16678. * -2 - player is in party already
  16679. * -1 - player is not found
  16680. * 0 - unknown error
  16681. * 1 - success
  16682. */
  16683. BUILDIN_FUNC(party_addmember)
  16684. {
  16685. int party_id = script_getnum(st,2);
  16686. TBL_PC *sd;
  16687. struct party_data *party;
  16688. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  16689. script_pushint(st,-1);
  16690. return SCRIPT_CMD_FAILURE;
  16691. }
  16692. if( sd->status.party_id ) {
  16693. script_pushint(st,-2);
  16694. return SCRIPT_CMD_FAILURE;
  16695. }
  16696. if( !(party = party_search(party_id)) ) {
  16697. script_pushint(st,-3);
  16698. return SCRIPT_CMD_FAILURE;
  16699. }
  16700. if( party->party.count >= MAX_PARTY ) {
  16701. script_pushint(st,-4);
  16702. return SCRIPT_CMD_FAILURE;
  16703. }
  16704. sd->party_invite = party_id;
  16705. script_pushint(st,party_add_member(party_id,sd));
  16706. return SCRIPT_CMD_SUCCESS;
  16707. }
  16708. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  16709. * party_delmember {<char id>,<party_id>};
  16710. * @param: char_id
  16711. * @param: party_id
  16712. * @return val: Result value
  16713. * -3 - player is not in party
  16714. * -2 - party is not found
  16715. * -1 - player is not found
  16716. * 0 - unknown error
  16717. * 1 - success
  16718. */
  16719. BUILDIN_FUNC(party_delmember)
  16720. {
  16721. TBL_PC *sd = NULL;
  16722. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  16723. script_pushint(st,-1);
  16724. return SCRIPT_CMD_FAILURE;
  16725. }
  16726. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  16727. script_pushint(st,party_removemember2(sd,0,0));
  16728. else
  16729. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  16730. return SCRIPT_CMD_SUCCESS;
  16731. }
  16732. /** Changes party leader of specified party (even the leader is offline)
  16733. * party_changeleader <party id>,<char id>;
  16734. * @param party_id: ID of party
  16735. * @param char_id: Char ID of new leader
  16736. * @return val: Result value
  16737. * -4 - player is party leader already
  16738. * -3 - player is not in this party
  16739. * -2 - player is not found
  16740. * -1 - party is not found
  16741. * 0 - unknown error
  16742. * 1 - success */
  16743. BUILDIN_FUNC(party_changeleader)
  16744. {
  16745. int i, party_id = script_getnum(st,2);
  16746. TBL_PC *sd = NULL;
  16747. TBL_PC *tsd = NULL;
  16748. struct party_data *party = NULL;
  16749. if( !(party = party_search(party_id)) ) {
  16750. script_pushint(st,-1);
  16751. return SCRIPT_CMD_FAILURE;
  16752. }
  16753. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  16754. script_pushint(st,-2);
  16755. return SCRIPT_CMD_FAILURE;
  16756. }
  16757. if( tsd->status.party_id != party_id ) {
  16758. script_pushint(st,-3);
  16759. return SCRIPT_CMD_FAILURE;
  16760. }
  16761. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  16762. if( i >= MAX_PARTY ) { //this is should impossible!
  16763. script_pushint(st,0);
  16764. return SCRIPT_CMD_FAILURE;
  16765. }
  16766. if( party->data[i].sd == tsd ) {
  16767. script_pushint(st,-4);
  16768. return SCRIPT_CMD_FAILURE;
  16769. }
  16770. script_pushint(st,party_changeleader(sd,tsd,party));
  16771. return SCRIPT_CMD_SUCCESS;
  16772. }
  16773. /** Changes party option
  16774. * party_changeoption <party id>,<option>,<flag>;
  16775. * @param party_id: ID of party that will be changed
  16776. * @param option: Type of option
  16777. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  16778. */
  16779. BUILDIN_FUNC(party_changeoption)
  16780. {
  16781. struct party_data *party;
  16782. if( !(party = party_search(script_getnum(st,2))) ) {
  16783. script_pushint(st,-1);
  16784. return SCRIPT_CMD_FAILURE;
  16785. }
  16786. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  16787. return SCRIPT_CMD_SUCCESS;
  16788. }
  16789. /** Destroys party with party id.
  16790. * party_destroy <party id>;
  16791. * @param party_id: ID of party that will be destroyed
  16792. */
  16793. BUILDIN_FUNC(party_destroy)
  16794. {
  16795. int i;
  16796. struct party_data *party;
  16797. if( !(party = party_search(script_getnum(st,2))) ) {
  16798. script_pushint(st,0);
  16799. return SCRIPT_CMD_FAILURE;
  16800. }
  16801. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  16802. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  16803. int j;
  16804. for( j = 0; j < MAX_PARTY; j++ ) {
  16805. TBL_PC *sd = party->data[j].sd;
  16806. if(sd)
  16807. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  16808. else if( party->party.member[j].char_id )
  16809. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  16810. }
  16811. party_broken(party->party.party_id);
  16812. script_pushint(st,1);
  16813. }
  16814. else //leader leave = party broken
  16815. script_pushint(st,party_leave(party->data[i].sd));
  16816. return SCRIPT_CMD_SUCCESS;
  16817. }
  16818. /** Checks if a player's client version meets a required version or date.
  16819. * @param type: 0 - check by version number; 1 - check by date
  16820. * @param data: Input
  16821. */
  16822. BUILDIN_FUNC(is_clientver) {
  16823. TBL_PC *sd = NULL;
  16824. int type = script_getnum(st,2);
  16825. int data = script_getnum(st,3);
  16826. int ret = 0;
  16827. if (script_hasdata(st,4))
  16828. sd = map_charid2sd(script_getnum(st,4));
  16829. else
  16830. sd = script_rid2sd(st);
  16831. if (sd == NULL) {
  16832. script_pushint(st,0);
  16833. return SCRIPT_CMD_FAILURE;
  16834. }
  16835. switch(type){
  16836. case 0:
  16837. ret = (sd->packet_ver >= data)?1:0;
  16838. break;
  16839. case 1:
  16840. ret = (sd->packet_ver >= date2version(data))?1:0;
  16841. break;
  16842. }
  16843. script_pushint(st,ret);
  16844. return SCRIPT_CMD_SUCCESS;
  16845. }
  16846. /** Returns various information about a player's VIP status. Need to enable VIP system
  16847. * vip_status <type>,{"<character name>"};
  16848. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  16849. * @param name: Character name (Optional)
  16850. */
  16851. BUILDIN_FUNC(vip_status) {
  16852. #ifdef VIP_ENABLE
  16853. TBL_PC *sd;
  16854. char vip_str[26];
  16855. time_t now = time(NULL);
  16856. int type = script_getnum(st, 2);
  16857. if (script_hasdata(st, 3))
  16858. sd = map_nick2sd(script_getstr(st, 3));
  16859. else
  16860. sd = script_rid2sd(st);
  16861. if (sd == NULL)
  16862. return SCRIPT_CMD_FAILURE;
  16863. switch(type) {
  16864. case 1: // Get VIP status.
  16865. script_pushint(st, pc_isvip(sd));
  16866. break;
  16867. case 2: // Get VIP expire date.
  16868. if (pc_isvip(sd)) {
  16869. time_t viptime = sd->vip.time;
  16870. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  16871. vip_str[25] = '\0';
  16872. script_pushstrcopy(st, vip_str);
  16873. } else
  16874. script_pushint(st, 0);
  16875. break;
  16876. case 3: // Get remaining time.
  16877. if (pc_isvip(sd)) {
  16878. time_t viptime_remain = sd->vip.time - now;
  16879. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  16880. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  16881. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  16882. script_pushstrcopy(st, vip_str);
  16883. } else
  16884. script_pushint(st, 0);
  16885. break;
  16886. }
  16887. #else
  16888. script_pushint(st, 0);
  16889. #endif
  16890. return SCRIPT_CMD_SUCCESS;
  16891. }
  16892. /** Adds or removes VIP time in minutes. Need to enable VIP system
  16893. * vip_time <time in mn>,{"<character name>"};
  16894. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  16895. * @param name: Character name (optional)
  16896. */
  16897. BUILDIN_FUNC(vip_time) {
  16898. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  16899. TBL_PC *sd;
  16900. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  16901. if (script_hasdata(st, 3))
  16902. sd = map_nick2sd(script_getstr(st, 3));
  16903. else
  16904. sd = script_rid2sd(st);
  16905. if (sd == NULL)
  16906. return SCRIPT_CMD_FAILURE;
  16907. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  16908. #endif
  16909. return SCRIPT_CMD_SUCCESS;
  16910. }
  16911. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  16912. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  16913. * @param monster: Monster ID or name
  16914. * @param duration: Transform duration in millisecond (ms)
  16915. * @param sc_type: Type of SC that will be affected during the transformation
  16916. * @param val1: Value for SC
  16917. * @param val2: Value for SC
  16918. * @param val3: Value for SC
  16919. * @param val4: Value for SC
  16920. */
  16921. BUILDIN_FUNC(montransform) {
  16922. TBL_PC *sd;
  16923. enum sc_type type;
  16924. int tick, mob_id, val1, val2, val3, val4;
  16925. struct script_data *data;
  16926. val1 = val2 = val3 = val4 = 0;
  16927. if( (sd = script_rid2sd(st)) == NULL )
  16928. return 1;
  16929. data = script_getdata(st, 2);
  16930. get_val(st, data); // Convert into value in case of a variable
  16931. if( data_isstring(data) )
  16932. mob_id = mobdb_searchname(script_getstr(st, 2));
  16933. else
  16934. mob_id = mobdb_checkid(script_getnum(st, 2));
  16935. tick = script_getnum(st, 3);
  16936. if (script_hasdata(st, 4))
  16937. type = (sc_type)script_getnum(st, 4);
  16938. else
  16939. type = SC_NONE;
  16940. if (mob_id == 0) {
  16941. if( data_isstring(data) )
  16942. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  16943. else
  16944. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  16945. return SCRIPT_CMD_FAILURE;
  16946. }
  16947. if (mob_id == MOBID_EMPERIUM) {
  16948. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  16949. return SCRIPT_CMD_FAILURE;
  16950. }
  16951. if (!(type >= SC_NONE && type < SC_MAX)) {
  16952. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  16953. return SCRIPT_CMD_FAILURE;
  16954. }
  16955. if (script_hasdata(st, 5))
  16956. val1 = script_getnum(st, 5);
  16957. if (script_hasdata(st, 6))
  16958. val2 = script_getnum(st, 6);
  16959. if (script_hasdata(st, 7))
  16960. val3 = script_getnum(st, 7);
  16961. if (script_hasdata(st, 8))
  16962. val4 = script_getnum(st, 8);
  16963. if (tick != 0) {
  16964. char msg[CHAT_SIZE_MAX];
  16965. struct mob_db *monster = mob_db(mob_id);
  16966. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  16967. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  16968. return SCRIPT_CMD_FAILURE;
  16969. }
  16970. if (sd->disguise){
  16971. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  16972. return SCRIPT_CMD_FAILURE;
  16973. }
  16974. sprintf(msg, msg_txt(sd,728), monster->name); // Traaaansformation-!! %s form!!
  16975. clif_showscript(&sd->bl, msg);
  16976. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  16977. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  16978. if (script_hasdata(st, 4))
  16979. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  16980. }
  16981. return SCRIPT_CMD_SUCCESS;
  16982. }
  16983. /**
  16984. * Attach script to player for certain duration
  16985. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  16986. * @param "script code"
  16987. * @param duration
  16988. * @param flag
  16989. * @param icon
  16990. * @param char_id
  16991. * @author [Cydh]
  16992. **/
  16993. BUILDIN_FUNC(bonus_script) {
  16994. uint16 flag = 0;
  16995. int16 icon = SI_BLANK;
  16996. uint32 dur;
  16997. uint8 type = 0;
  16998. TBL_PC* sd;
  16999. const char *script_str = NULL;
  17000. struct s_bonus_script_entry *entry = NULL;
  17001. if (script_hasdata(st,7)) {
  17002. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  17003. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  17004. return SCRIPT_CMD_FAILURE;
  17005. }
  17006. }
  17007. else
  17008. sd = script_rid2sd(st);
  17009. if (sd == NULL)
  17010. return SCRIPT_CMD_FAILURE;
  17011. script_str = script_getstr(st,2);
  17012. dur = 1000 * abs(script_getnum(st,3));
  17013. FETCH(4, flag);
  17014. FETCH(5, type);
  17015. FETCH(6, icon);
  17016. // No Script string, No Duration!
  17017. if (script_str[0] == '\0' || !dur) {
  17018. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  17019. return SCRIPT_CMD_FAILURE;
  17020. }
  17021. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  17022. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  17023. return SCRIPT_CMD_FAILURE;
  17024. }
  17025. if (icon <= SI_BLANK || icon >= SI_MAX)
  17026. icon = SI_BLANK;
  17027. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  17028. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  17029. status_calc_pc(sd,SCO_NONE);
  17030. }
  17031. return SCRIPT_CMD_SUCCESS;
  17032. }
  17033. /**
  17034. * Removes all bonus script from player
  17035. * bonus_script_clear {<flag>,{<char_id>}};
  17036. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  17037. * @param char_id Clear script from this character
  17038. * @author [Cydh]
  17039. */
  17040. BUILDIN_FUNC(bonus_script_clear) {
  17041. TBL_PC* sd;
  17042. bool flag = false;
  17043. if (script_hasdata(st,2))
  17044. flag = script_getnum(st,2);
  17045. if (script_hasdata(st,3))
  17046. sd = map_charid2sd(script_getnum(st,3));
  17047. else
  17048. sd = script_rid2sd(st);
  17049. if (sd == NULL)
  17050. return SCRIPT_CMD_FAILURE;
  17051. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  17052. return SCRIPT_CMD_SUCCESS;
  17053. }
  17054. /** Allows player to use atcommand while talking with NPC
  17055. * enable_command;
  17056. * @author [Cydh], [Kichi]
  17057. */
  17058. BUILDIN_FUNC(enable_command) {
  17059. TBL_PC* sd = script_rid2sd(st);
  17060. if (!sd)
  17061. return SCRIPT_CMD_FAILURE;
  17062. sd->state.disable_atcommand_on_npc = 0;
  17063. return SCRIPT_CMD_SUCCESS;
  17064. }
  17065. /** Prevents player to use atcommand while talking with NPC
  17066. * disable_command;
  17067. * @author [Cydh], [Kichi]
  17068. */
  17069. BUILDIN_FUNC(disable_command) {
  17070. TBL_PC* sd = script_rid2sd(st);
  17071. if (!sd)
  17072. return SCRIPT_CMD_FAILURE;
  17073. sd->state.disable_atcommand_on_npc = 1;
  17074. return SCRIPT_CMD_SUCCESS;
  17075. }
  17076. /** Get the information of the members of a guild by type.
  17077. * getguildmember <guild_id>{,<type>};
  17078. * @param guild_id: ID of guild
  17079. * @param type: Type of option (optional)
  17080. */
  17081. BUILDIN_FUNC(getguildmember)
  17082. {
  17083. unsigned char j = 0;
  17084. struct guild *g = NULL;
  17085. g = guild_search(script_getnum(st,2));
  17086. if (g) {
  17087. unsigned char i, type = 0;
  17088. if (script_hasdata(st,3))
  17089. type = (unsigned char)script_getnum(st,3);
  17090. for (i = 0; i < MAX_GUILD; i++) {
  17091. if (g->member[i].account_id) {
  17092. switch (type) {
  17093. case 2:
  17094. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  17095. break;
  17096. case 1:
  17097. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  17098. break;
  17099. default:
  17100. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  17101. break;
  17102. }
  17103. j++;
  17104. }
  17105. }
  17106. }
  17107. mapreg_setreg(add_str("$@guildmembercount"), j);
  17108. return SCRIPT_CMD_SUCCESS;
  17109. }
  17110. /** Adds spirit ball to player for 'duration' in milisecond
  17111. * addspiritball <count>,<duration>{,<char_id>};
  17112. * @param count How many spirit ball will be added
  17113. * @param duration How long spiritball is active until it disappears
  17114. * @param char_id Target player (Optional)
  17115. * @author [Cydh]
  17116. */
  17117. BUILDIN_FUNC(addspiritball) {
  17118. uint8 i, count = script_getnum(st,2);
  17119. uint16 duration = script_getnum(st,3);
  17120. struct map_session_data *sd = NULL;
  17121. if (count == 0)
  17122. return SCRIPT_CMD_SUCCESS;
  17123. if (script_hasdata(st,4)) {
  17124. if (!script_isstring(st,4))
  17125. sd = map_charid2sd(script_getnum(st,4));
  17126. else
  17127. sd = map_nick2sd(script_getstr(st,4));
  17128. }
  17129. else
  17130. sd = script_rid2sd(st);
  17131. if (!sd)
  17132. return SCRIPT_CMD_FAILURE;
  17133. for (i = 0; i < count; i++)
  17134. pc_addspiritball(sd,duration,10);
  17135. return SCRIPT_CMD_SUCCESS;
  17136. }
  17137. /** Deletes the spirit ball(s) from player
  17138. * delspiritball <count>{,<char_id>};
  17139. * @param count How many spirit ball will be deleted
  17140. * @param char_id Target player (Optional)
  17141. * @author [Cydh]
  17142. */
  17143. BUILDIN_FUNC(delspiritball) {
  17144. uint8 count = script_getnum(st,2);
  17145. struct map_session_data *sd = NULL;
  17146. if (count == 0)
  17147. count = 1;
  17148. if (script_hasdata(st,3)) {
  17149. if (!script_isstring(st,3))
  17150. sd = map_charid2sd(script_getnum(st,3));
  17151. else
  17152. sd = map_nick2sd(script_getstr(st,3));
  17153. }
  17154. else
  17155. sd = script_rid2sd(st);
  17156. if (!sd)
  17157. return SCRIPT_CMD_FAILURE;
  17158. pc_delspiritball(sd,count,0);
  17159. return SCRIPT_CMD_SUCCESS;
  17160. }
  17161. /** Counts the spirit ball that player has
  17162. * countspiritball {,<char_id>};
  17163. * @param char_id Target player (Optional)
  17164. * @author [Cydh]
  17165. */
  17166. BUILDIN_FUNC(countspiritball) {
  17167. struct map_session_data *sd;
  17168. if (script_hasdata(st,2)) {
  17169. if (!script_isstring(st,2))
  17170. sd = map_charid2sd(script_getnum(st,2));
  17171. else
  17172. sd = map_nick2sd(script_getstr(st,2));
  17173. }
  17174. else
  17175. sd = script_rid2sd(st);
  17176. if (!sd)
  17177. return SCRIPT_CMD_FAILURE;
  17178. script_pushint(st,sd->spiritball);
  17179. return SCRIPT_CMD_SUCCESS;
  17180. }
  17181. /** Merges separated stackable items because of guid
  17182. * mergeitem {<item_id>};
  17183. * mergeitem {"<item name>"};
  17184. * @param item Item ID/Name for merging specific item (Optional)
  17185. * @author [Cydh]
  17186. */
  17187. BUILDIN_FUNC(mergeitem) {
  17188. struct map_session_data *sd = script_rid2sd(st);
  17189. struct item *items = NULL;
  17190. uint16 i, count = 0;
  17191. int nameid = 0;
  17192. if (!sd)
  17193. return SCRIPT_CMD_SUCCESS;
  17194. if (script_hasdata(st, 2)) {
  17195. struct script_data *data = script_getdata(st, 2);
  17196. struct item_data *id;
  17197. get_val(st, data);
  17198. if (data_isstring(data)) {// "<item name>"
  17199. const char *name = conv_str(st,data);
  17200. if (!(id = itemdb_searchname(name))) {
  17201. ShowError("buildin_mergeitem: Nonexistant item %s requested.\n", name);
  17202. script_pushint(st, count);
  17203. return SCRIPT_CMD_FAILURE;
  17204. }
  17205. nameid = id->nameid;
  17206. }
  17207. else if (data_isint(data)) {// <item id>
  17208. nameid = conv_num(st,data);
  17209. if (!(id = itemdb_exists(nameid))) {
  17210. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  17211. script_pushint(st, count);
  17212. return SCRIPT_CMD_FAILURE;
  17213. }
  17214. }
  17215. }
  17216. for (i = 0; i < MAX_INVENTORY; i++) {
  17217. struct item *it = &sd->status.inventory[i];
  17218. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  17219. continue;
  17220. if ((!nameid || (nameid == it->nameid))) {
  17221. uint8 k;
  17222. if (!count) {
  17223. CREATE(items, struct item, 1);
  17224. memcpy(&items[count++], it, sizeof(struct item));
  17225. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  17226. continue;
  17227. }
  17228. for (k = 0; k < count; k++) {
  17229. // Find Match
  17230. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  17231. items[k].amount += it->amount;
  17232. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  17233. break;
  17234. }
  17235. }
  17236. if (k >= count) {
  17237. // New entry
  17238. RECREATE(items, struct item, count+1);
  17239. memcpy(&items[count++], it, sizeof(struct item));
  17240. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  17241. }
  17242. }
  17243. }
  17244. if (!items) // Nothing todo here
  17245. return SCRIPT_CMD_SUCCESS;
  17246. // Retrieve the items
  17247. for (i = 0; i < count; i++) {
  17248. uint8 flag = 0;
  17249. if (!&items[i])
  17250. continue;
  17251. items[i].id = 0;
  17252. items[i].unique_id = 0;
  17253. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  17254. clif_additem(sd, i, items[i].amount, flag);
  17255. }
  17256. aFree(items);
  17257. script_pushint(st, count);
  17258. return SCRIPT_CMD_SUCCESS;
  17259. }
  17260. #include "../custom/script.inc"
  17261. // declarations that were supposed to be exported from npc_chat.c
  17262. #ifdef PCRE_SUPPORT
  17263. BUILDIN_FUNC(defpattern);
  17264. BUILDIN_FUNC(activatepset);
  17265. BUILDIN_FUNC(deactivatepset);
  17266. BUILDIN_FUNC(deletepset);
  17267. #endif
  17268. /** Regular expression matching
  17269. * preg_match(<pattern>,<string>{,<offset>})
  17270. */
  17271. BUILDIN_FUNC(preg_match) {
  17272. #ifdef PCRE_SUPPORT
  17273. pcre *re;
  17274. pcre_extra *pcreExtra;
  17275. const char *error;
  17276. int erroffset, r, offset = 0;
  17277. int subStrVec[30];
  17278. const char* pattern = script_getstr(st,2);
  17279. const char* subject = script_getstr(st,3);
  17280. if (script_hasdata(st,4))
  17281. offset = script_getnum(st,4);
  17282. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  17283. pcreExtra = pcre_study(re, 0, &error);
  17284. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  17285. pcre_free(re);
  17286. if (pcreExtra != NULL)
  17287. pcre_free(pcreExtra);
  17288. if (r < 0)
  17289. script_pushint(st,0);
  17290. else
  17291. script_pushint(st,(r > 0) ? r : 30 / 3);
  17292. return SCRIPT_CMD_SUCCESS;
  17293. #else
  17294. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  17295. script_pushint(st,0);
  17296. return SCRIPT_CMD_SUCCESS;
  17297. #endif
  17298. }
  17299. /// script command definitions
  17300. /// for an explanation on args, see add_buildin_func
  17301. struct script_function buildin_func[] = {
  17302. // NPC interaction
  17303. BUILDIN_DEF(mes,"s*"),
  17304. BUILDIN_DEF(next,""),
  17305. BUILDIN_DEF(close,""),
  17306. BUILDIN_DEF(close2,""),
  17307. BUILDIN_DEF(menu,"sl*"),
  17308. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  17309. BUILDIN_DEF(prompt,"s*"),
  17310. //
  17311. BUILDIN_DEF(goto,"l"),
  17312. BUILDIN_DEF(callsub,"l*"),
  17313. BUILDIN_DEF(callfunc,"s*"),
  17314. BUILDIN_DEF(return,"?"),
  17315. BUILDIN_DEF(getarg,"i?"),
  17316. BUILDIN_DEF(jobchange,"i?"),
  17317. BUILDIN_DEF(jobname,"i"),
  17318. BUILDIN_DEF(input,"r??"),
  17319. BUILDIN_DEF(warp,"sii"),
  17320. BUILDIN_DEF(areawarp,"siiiisii??"),
  17321. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  17322. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  17323. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  17324. BUILDIN_DEF(setlook,"ii"),
  17325. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  17326. BUILDIN_DEF(set,"rv"),
  17327. BUILDIN_DEF(setarray,"rv*"),
  17328. BUILDIN_DEF(cleararray,"rvi"),
  17329. BUILDIN_DEF(copyarray,"rri"),
  17330. BUILDIN_DEF(getarraysize,"r"),
  17331. BUILDIN_DEF(deletearray,"r?"),
  17332. BUILDIN_DEF(getelementofarray,"ri"),
  17333. BUILDIN_DEF(getitem,"vi?"),
  17334. BUILDIN_DEF(rentitem,"vi"),
  17335. BUILDIN_DEF(rentitem2,"viiiiiiii"),
  17336. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  17337. BUILDIN_DEF(getnameditem,"vv"),
  17338. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  17339. BUILDIN_DEF(makeitem,"visii"),
  17340. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  17341. BUILDIN_DEF(delitem,"vi?"),
  17342. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  17343. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  17344. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  17345. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  17346. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  17347. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  17348. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  17349. BUILDIN_DEF(cutin,"si"),
  17350. BUILDIN_DEF(viewpoint,"iiiii"),
  17351. BUILDIN_DEF(heal,"ii"),
  17352. BUILDIN_DEF(itemheal,"ii"),
  17353. BUILDIN_DEF(percentheal,"ii"),
  17354. BUILDIN_DEF(rand,"i?"),
  17355. BUILDIN_DEF(countitem,"v?"),
  17356. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  17357. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  17358. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  17359. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  17360. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  17361. BUILDIN_DEF(checkweight,"vi*"),
  17362. BUILDIN_DEF(checkweight2,"rr"),
  17363. BUILDIN_DEF(readparam,"i?"),
  17364. BUILDIN_DEF(getcharid,"i?"),
  17365. BUILDIN_DEF(getnpcid,"i?"),
  17366. BUILDIN_DEF(getpartyname,"i"),
  17367. BUILDIN_DEF(getpartymember,"i?"),
  17368. BUILDIN_DEF(getpartyleader,"i?"),
  17369. BUILDIN_DEF(getguildname,"i"),
  17370. BUILDIN_DEF(getguildmaster,"i"),
  17371. BUILDIN_DEF(getguildmasterid,"i"),
  17372. BUILDIN_DEF(strcharinfo,"i"),
  17373. BUILDIN_DEF(strnpcinfo,"i"),
  17374. BUILDIN_DEF(getequipid,"i"),
  17375. BUILDIN_DEF(getequipuniqueid,"i"),
  17376. BUILDIN_DEF(getequipname,"i"),
  17377. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  17378. BUILDIN_DEF(repair,"i"), // [Valaris]
  17379. BUILDIN_DEF(repairall,""),
  17380. BUILDIN_DEF(getequipisequiped,"i"),
  17381. BUILDIN_DEF(getequipisenableref,"i"),
  17382. BUILDIN_DEF(getequiprefinerycnt,"i"),
  17383. BUILDIN_DEF(getequipweaponlv,"i"),
  17384. BUILDIN_DEF(getequippercentrefinery,"i"),
  17385. BUILDIN_DEF(successrefitem,"i?"),
  17386. BUILDIN_DEF(failedrefitem,"i"),
  17387. BUILDIN_DEF(downrefitem,"i?"),
  17388. BUILDIN_DEF(statusup,"i"),
  17389. BUILDIN_DEF(statusup2,"ii"),
  17390. BUILDIN_DEF(bonus,"iv"),
  17391. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  17392. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  17393. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  17394. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  17395. BUILDIN_DEF(autobonus,"sii??"),
  17396. BUILDIN_DEF(autobonus2,"sii??"),
  17397. BUILDIN_DEF(autobonus3,"siiv?"),
  17398. BUILDIN_DEF(skill,"vi?"),
  17399. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  17400. BUILDIN_DEF(guildskill,"vi"),
  17401. BUILDIN_DEF(getskilllv,"v"),
  17402. BUILDIN_DEF(getgdskilllv,"iv"),
  17403. BUILDIN_DEF(basicskillcheck,""),
  17404. BUILDIN_DEF(getgmlevel,""),
  17405. BUILDIN_DEF(getgroupid,""),
  17406. BUILDIN_DEF(end,""),
  17407. BUILDIN_DEF(checkoption,"i"),
  17408. BUILDIN_DEF(setoption,"i?"),
  17409. BUILDIN_DEF(setcart,"?"),
  17410. BUILDIN_DEF(checkcart,""),
  17411. BUILDIN_DEF(setfalcon,"?"),
  17412. BUILDIN_DEF(checkfalcon,""),
  17413. BUILDIN_DEF(setriding,"?"),
  17414. BUILDIN_DEF(checkriding,""),
  17415. BUILDIN_DEF(checkwug,""),
  17416. BUILDIN_DEF(checkmadogear,""),
  17417. BUILDIN_DEF(setmadogear,"?"),
  17418. BUILDIN_DEF2(savepoint,"save","sii"),
  17419. BUILDIN_DEF(savepoint,"sii"),
  17420. BUILDIN_DEF(gettimetick,"i"),
  17421. BUILDIN_DEF(gettime,"i"),
  17422. BUILDIN_DEF(gettimestr,"si"),
  17423. BUILDIN_DEF(openstorage,""),
  17424. BUILDIN_DEF(guildopenstorage,""),
  17425. BUILDIN_DEF(itemskill,"vi"),
  17426. BUILDIN_DEF(produce,"i"),
  17427. BUILDIN_DEF(cooking,"i"),
  17428. BUILDIN_DEF(monster,"siisii???"),
  17429. BUILDIN_DEF(getmobdrops,"i"),
  17430. BUILDIN_DEF(areamonster,"siiiisii???"),
  17431. BUILDIN_DEF(killmonster,"ss?"),
  17432. BUILDIN_DEF(killmonsterall,"s?"),
  17433. BUILDIN_DEF(clone,"siisi????"),
  17434. BUILDIN_DEF(doevent,"s"),
  17435. BUILDIN_DEF(donpcevent,"s"),
  17436. BUILDIN_DEF(cmdothernpc,"ss"),
  17437. BUILDIN_DEF(addtimer,"is"),
  17438. BUILDIN_DEF(deltimer,"s"),
  17439. BUILDIN_DEF(addtimercount,"is"),
  17440. BUILDIN_DEF(initnpctimer,"??"),
  17441. BUILDIN_DEF(stopnpctimer,"??"),
  17442. BUILDIN_DEF(startnpctimer,"??"),
  17443. BUILDIN_DEF(setnpctimer,"i?"),
  17444. BUILDIN_DEF(getnpctimer,"i?"),
  17445. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  17446. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  17447. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  17448. BUILDIN_DEF(announce,"si?????"),
  17449. BUILDIN_DEF(mapannounce,"ssi?????"),
  17450. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  17451. BUILDIN_DEF(getusers,"i"),
  17452. BUILDIN_DEF(getmapguildusers,"si"),
  17453. BUILDIN_DEF(getmapusers,"s"),
  17454. BUILDIN_DEF(getareausers,"siiii"),
  17455. BUILDIN_DEF(getareadropitem,"siiiiv"),
  17456. BUILDIN_DEF(enablenpc,"s"),
  17457. BUILDIN_DEF(disablenpc,"s"),
  17458. BUILDIN_DEF(hideoffnpc,"s"),
  17459. BUILDIN_DEF(hideonnpc,"s"),
  17460. BUILDIN_DEF(sc_start,"iii???"),
  17461. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  17462. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  17463. BUILDIN_DEF(sc_end,"i?"),
  17464. BUILDIN_DEF(getstatus, "i?"),
  17465. BUILDIN_DEF(getscrate,"ii?"),
  17466. BUILDIN_DEF(debugmes,"s"),
  17467. BUILDIN_DEF2(catchpet,"pet","i"),
  17468. BUILDIN_DEF2(birthpet,"bpet",""),
  17469. BUILDIN_DEF(resetlvl,"i"),
  17470. BUILDIN_DEF(resetstatus,""),
  17471. BUILDIN_DEF(resetskill,""),
  17472. BUILDIN_DEF(skillpointcount,""),
  17473. BUILDIN_DEF(changebase,"i?"),
  17474. BUILDIN_DEF(changesex,""),
  17475. BUILDIN_DEF(waitingroom,"si?????"),
  17476. BUILDIN_DEF(delwaitingroom,"?"),
  17477. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  17478. BUILDIN_DEF(enablewaitingroomevent,"?"),
  17479. BUILDIN_DEF(disablewaitingroomevent,"?"),
  17480. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  17481. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  17482. BUILDIN_DEF(getwaitingroomstate,"i?"),
  17483. BUILDIN_DEF(warpwaitingpc,"sii?"),
  17484. BUILDIN_DEF(attachrid,"i"),
  17485. BUILDIN_DEF(addrid,"i?????"),
  17486. BUILDIN_DEF(detachrid,""),
  17487. BUILDIN_DEF(isloggedin,"i?"),
  17488. BUILDIN_DEF(setmapflagnosave,"ssii"),
  17489. BUILDIN_DEF(getmapflag,"si?"),
  17490. BUILDIN_DEF(setmapflag,"si??"),
  17491. BUILDIN_DEF(removemapflag,"si?"),
  17492. BUILDIN_DEF(pvpon,"s"),
  17493. BUILDIN_DEF(pvpoff,"s"),
  17494. BUILDIN_DEF(gvgon,"s"),
  17495. BUILDIN_DEF(gvgoff,"s"),
  17496. BUILDIN_DEF(emotion,"i??"),
  17497. BUILDIN_DEF(maprespawnguildid,"sii"),
  17498. BUILDIN_DEF(agitstart,""), // <Agit>
  17499. BUILDIN_DEF(agitend,""),
  17500. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  17501. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  17502. BUILDIN_DEF(getcastlename,"s"),
  17503. BUILDIN_DEF(getcastledata,"si"),
  17504. BUILDIN_DEF(setcastledata,"sii"),
  17505. BUILDIN_DEF(requestguildinfo,"i?"),
  17506. BUILDIN_DEF(getequipcardcnt,"i"),
  17507. BUILDIN_DEF(successremovecards,"i"),
  17508. BUILDIN_DEF(failedremovecards,"ii"),
  17509. BUILDIN_DEF(marriage,"s"),
  17510. BUILDIN_DEF2(wedding_effect,"wedding",""),
  17511. BUILDIN_DEF(divorce,""),
  17512. BUILDIN_DEF(ispartneron,""),
  17513. BUILDIN_DEF(getpartnerid,""),
  17514. BUILDIN_DEF(getchildid,""),
  17515. BUILDIN_DEF(getmotherid,""),
  17516. BUILDIN_DEF(getfatherid,""),
  17517. BUILDIN_DEF(warppartner,"sii"),
  17518. BUILDIN_DEF(getitemname,"v"),
  17519. BUILDIN_DEF(getitemslots,"i"),
  17520. BUILDIN_DEF(makepet,"i"),
  17521. BUILDIN_DEF(getexp,"ii"),
  17522. BUILDIN_DEF(getinventorylist,""),
  17523. BUILDIN_DEF(getskilllist,""),
  17524. BUILDIN_DEF(clearitem,""),
  17525. BUILDIN_DEF(classchange,"ii"),
  17526. BUILDIN_DEF(misceffect,"i"),
  17527. BUILDIN_DEF(playBGM,"s"),
  17528. BUILDIN_DEF(playBGMall,"s?????"),
  17529. BUILDIN_DEF(soundeffect,"si"),
  17530. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  17531. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  17532. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  17533. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  17534. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  17535. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  17536. BUILDIN_DEF(petloot,"i"), // [Valaris]
  17537. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  17538. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  17539. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  17540. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  17541. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  17542. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  17543. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  17544. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  17545. BUILDIN_DEF(nude,""), // nude command [Valaris]
  17546. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  17547. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  17548. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  17549. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  17550. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  17551. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  17552. BUILDIN_DEF(mobcount,"ss"),
  17553. BUILDIN_DEF(getlook,"i"),
  17554. BUILDIN_DEF(getsavepoint,"i"),
  17555. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  17556. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  17557. BUILDIN_DEF(npcstop,""), // [Valaris]
  17558. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  17559. BUILDIN_DEF(checkoption1,"i"),
  17560. BUILDIN_DEF(checkoption2,"i"),
  17561. BUILDIN_DEF(guildgetexp,"i"),
  17562. BUILDIN_DEF(guildchangegm,"is"),
  17563. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  17564. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  17565. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  17566. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  17567. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  17568. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  17569. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  17570. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  17571. BUILDIN_DEF(night,""), // sets the server to night time
  17572. BUILDIN_DEF(day,""), // sets the server to day time
  17573. #ifdef PCRE_SUPPORT
  17574. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  17575. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  17576. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  17577. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  17578. #endif
  17579. BUILDIN_DEF(preg_match,"ss?"),
  17580. BUILDIN_DEF(dispbottom,"s?"), //added from jA [Lupus]
  17581. BUILDIN_DEF(recovery,"i???"),
  17582. BUILDIN_DEF(getpetinfo,"i"),
  17583. BUILDIN_DEF(gethominfo,"i"),
  17584. BUILDIN_DEF(getmercinfo,"i?"),
  17585. BUILDIN_DEF(checkequipedcard,"i"),
  17586. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  17587. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  17588. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  17589. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  17590. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  17591. BUILDIN_DEF(charat,"si"),
  17592. BUILDIN_DEF(setchar,"ssi"),
  17593. BUILDIN_DEF(insertchar,"ssi"),
  17594. BUILDIN_DEF(delchar,"si"),
  17595. BUILDIN_DEF(strtoupper,"s"),
  17596. BUILDIN_DEF(strtolower,"s"),
  17597. BUILDIN_DEF(charisupper, "si"),
  17598. BUILDIN_DEF(charislower, "si"),
  17599. BUILDIN_DEF(substr,"sii"),
  17600. BUILDIN_DEF(explode, "rss"),
  17601. BUILDIN_DEF(implode, "r?"),
  17602. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  17603. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  17604. BUILDIN_DEF(strpos,"ss?"),
  17605. BUILDIN_DEF(replacestr,"sss??"),
  17606. BUILDIN_DEF(countstr,"ss?"),
  17607. BUILDIN_DEF(setnpcdisplay,"sv??"),
  17608. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  17609. BUILDIN_DEF(strcmp,"ss"),
  17610. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  17611. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  17612. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  17613. // [zBuffer] List of mathematics commands --->
  17614. BUILDIN_DEF(sqrt,"i"),
  17615. BUILDIN_DEF(pow,"ii"),
  17616. BUILDIN_DEF(distance,"iiii"),
  17617. // <--- [zBuffer] List of mathematics commands
  17618. BUILDIN_DEF(md5,"s"),
  17619. // [zBuffer] List of dynamic var commands --->
  17620. BUILDIN_DEF(getd,"s"),
  17621. BUILDIN_DEF(setd,"sv"),
  17622. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  17623. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  17624. BUILDIN_DEF(npcshopadditem,"sii*"),
  17625. BUILDIN_DEF(npcshopdelitem,"si*"),
  17626. BUILDIN_DEF(npcshopattach,"s?"),
  17627. BUILDIN_DEF(equip,"i"),
  17628. BUILDIN_DEF(autoequip,"ii"),
  17629. BUILDIN_DEF(setbattleflag,"si"),
  17630. BUILDIN_DEF(getbattleflag,"s"),
  17631. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  17632. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  17633. BUILDIN_DEF(undisguise,""), //undisguise player. Lupus
  17634. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  17635. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  17636. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  17637. BUILDIN_DEF(axtoi,"s"),
  17638. BUILDIN_DEF(query_sql,"s*"),
  17639. BUILDIN_DEF(query_logsql,"s*"),
  17640. BUILDIN_DEF(escape_sql,"v"),
  17641. BUILDIN_DEF(atoi,"s"),
  17642. BUILDIN_DEF(strtol,"si"),
  17643. // [zBuffer] List of player cont commands --->
  17644. BUILDIN_DEF(rid2name,"i"),
  17645. BUILDIN_DEF(pcfollow,"ii"),
  17646. BUILDIN_DEF(pcstopfollow,"i"),
  17647. BUILDIN_DEF(pcblockmove,"ii"),
  17648. // <--- [zBuffer] List of player cont commands
  17649. // [zBuffer] List of mob control commands --->
  17650. BUILDIN_DEF(getunitname,"i"),
  17651. BUILDIN_DEF(setunitname,"is"),
  17652. BUILDIN_DEF(getunitdata,"i*"),
  17653. BUILDIN_DEF(setunitdata,"iii"),
  17654. BUILDIN_DEF(unitwalk,"ii?"),
  17655. BUILDIN_DEF(unitkill,"i"),
  17656. BUILDIN_DEF(unitwarp,"isii"),
  17657. BUILDIN_DEF(unitattack,"iv?"),
  17658. BUILDIN_DEF(unitstopattack,"i"),
  17659. BUILDIN_DEF(unitstopwalk,"i"),
  17660. BUILDIN_DEF(unittalk,"is"),
  17661. BUILDIN_DEF(unitemote,"ii"),
  17662. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  17663. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  17664. // <--- [zBuffer] List of mob control commands
  17665. BUILDIN_DEF(sleep,"i"),
  17666. BUILDIN_DEF(sleep2,"i"),
  17667. BUILDIN_DEF(awake,"s"),
  17668. BUILDIN_DEF(getvariableofnpc,"rs"),
  17669. BUILDIN_DEF(warpportal,"iisii"),
  17670. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  17671. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  17672. BUILDIN_DEF(morphembryo,""),
  17673. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  17674. BUILDIN_DEF(checkhomcall,""),
  17675. BUILDIN_DEF(eaclass,"?"), //[Skotlex]
  17676. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  17677. BUILDIN_DEF(checkvending,"?"),
  17678. BUILDIN_DEF(checkchatting,"?"),
  17679. BUILDIN_DEF(checkidle,"?"),
  17680. BUILDIN_DEF(openmail,""),
  17681. BUILDIN_DEF(openauction,""),
  17682. BUILDIN_DEF(checkcell,"siii"),
  17683. BUILDIN_DEF(setcell,"siiiiii"),
  17684. BUILDIN_DEF(setwall,"siiiiis"),
  17685. BUILDIN_DEF(delwall,"s"),
  17686. BUILDIN_DEF(searchitem,"rs"),
  17687. BUILDIN_DEF(mercenary_create,"ii"),
  17688. BUILDIN_DEF(mercenary_heal,"ii"),
  17689. BUILDIN_DEF(mercenary_sc_start,"iii"),
  17690. BUILDIN_DEF(mercenary_get_calls,"i"),
  17691. BUILDIN_DEF(mercenary_get_faith,"i"),
  17692. BUILDIN_DEF(mercenary_set_calls,"ii"),
  17693. BUILDIN_DEF(mercenary_set_faith,"ii"),
  17694. BUILDIN_DEF(readbook,"ii"),
  17695. BUILDIN_DEF(setfont,"i"),
  17696. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  17697. BUILDIN_DEF(progressbar,"si"),
  17698. BUILDIN_DEF(pushpc,"ii"),
  17699. BUILDIN_DEF(buyingstore,"i"),
  17700. BUILDIN_DEF(searchstores,"ii"),
  17701. BUILDIN_DEF(showdigit,"i?"),
  17702. // WoE SE
  17703. BUILDIN_DEF(agitstart2,""),
  17704. BUILDIN_DEF(agitend2,""),
  17705. BUILDIN_DEF(agitcheck2,""),
  17706. // BattleGround
  17707. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  17708. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  17709. BUILDIN_DEF(bg_team_setxy,"iii"),
  17710. BUILDIN_DEF(bg_warp,"isii"),
  17711. BUILDIN_DEF(bg_monster,"isiisi?"),
  17712. BUILDIN_DEF(bg_monster_set_team,"ii"),
  17713. BUILDIN_DEF(bg_leave,""),
  17714. BUILDIN_DEF(bg_destroy,"i"),
  17715. BUILDIN_DEF(areapercentheal,"siiiiii"),
  17716. BUILDIN_DEF(bg_get_data,"ii"),
  17717. BUILDIN_DEF(bg_getareausers,"isiiii"),
  17718. BUILDIN_DEF(bg_updatescore,"sii"),
  17719. // Instancing
  17720. BUILDIN_DEF(instance_create,"s"),
  17721. BUILDIN_DEF(instance_destroy,"?"),
  17722. BUILDIN_DEF(instance_id,""),
  17723. BUILDIN_DEF(instance_enter,"s??"),
  17724. BUILDIN_DEF(instance_npcname,"s?"),
  17725. BUILDIN_DEF(instance_mapname,"s?"),
  17726. BUILDIN_DEF(instance_warpall,"sii?"),
  17727. BUILDIN_DEF(instance_announce,"isi?????"),
  17728. BUILDIN_DEF(instance_check_party,"i???"),
  17729. /**
  17730. * 3rd-related
  17731. **/
  17732. BUILDIN_DEF(makerune,"i"),
  17733. BUILDIN_DEF(checkdragon,""),//[Ind]
  17734. BUILDIN_DEF(setdragon,"?"),//[Ind]
  17735. BUILDIN_DEF(ismounting,""),//[Ind]
  17736. BUILDIN_DEF(setmounting,""),//[Ind]
  17737. BUILDIN_DEF(checkre,"i"),
  17738. /**
  17739. * rAthena and beyond!
  17740. **/
  17741. BUILDIN_DEF(getargcount,""),
  17742. BUILDIN_DEF(getcharip,"?"),
  17743. BUILDIN_DEF(is_function,"s"),
  17744. BUILDIN_DEF(get_revision,""),
  17745. BUILDIN_DEF(get_githash,""),
  17746. BUILDIN_DEF(freeloop,"?"),
  17747. BUILDIN_DEF(getrandgroupitem,"ii?"),
  17748. BUILDIN_DEF(cleanmap,"s"),
  17749. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  17750. BUILDIN_DEF(npcskill,"viii"),
  17751. BUILDIN_DEF(consumeitem,"v"),
  17752. BUILDIN_DEF(delequip,"i"),
  17753. BUILDIN_DEF(breakequip,"i"),
  17754. BUILDIN_DEF(sit,"?"),
  17755. BUILDIN_DEF(stand,"?"),
  17756. //@commands (script based)
  17757. BUILDIN_DEF(bindatcmd, "ss??"),
  17758. BUILDIN_DEF(unbindatcmd, "s"),
  17759. BUILDIN_DEF(useatcmd, "s"),
  17760. //Quest Log System [Inkfish]
  17761. BUILDIN_DEF(questinfo, "ii??"),
  17762. BUILDIN_DEF(setquest, "i"),
  17763. BUILDIN_DEF(erasequest, "i"),
  17764. BUILDIN_DEF(completequest, "i"),
  17765. BUILDIN_DEF(checkquest, "i?"),
  17766. BUILDIN_DEF(isbegin_quest,"i"),
  17767. BUILDIN_DEF(changequest, "ii"),
  17768. BUILDIN_DEF(showevent, "i?"),
  17769. //Bound items [Xantara] & [Akinari]
  17770. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  17771. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  17772. BUILDIN_DEF(countbound, "?"),
  17773. // Party related
  17774. BUILDIN_DEF(party_create,"s???"),
  17775. BUILDIN_DEF(party_addmember,"ii"),
  17776. BUILDIN_DEF(party_delmember,"??"),
  17777. BUILDIN_DEF(party_changeleader,"ii"),
  17778. BUILDIN_DEF(party_changeoption,"iii"),
  17779. BUILDIN_DEF(party_destroy,"i"),
  17780. BUILDIN_DEF(is_clientver,"ii?"),
  17781. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  17782. BUILDIN_DEF(vip_status,"i?"),
  17783. BUILDIN_DEF(vip_time,"i?"),
  17784. BUILDIN_DEF(bonus_script,"si????"),
  17785. BUILDIN_DEF(bonus_script_clear,"??"),
  17786. BUILDIN_DEF(getgroupitem,"i"),
  17787. BUILDIN_DEF(enable_command,""),
  17788. BUILDIN_DEF(disable_command,""),
  17789. BUILDIN_DEF(getguildmember,"i?"),
  17790. BUILDIN_DEF(addspiritball,"ii?"),
  17791. BUILDIN_DEF(delspiritball,"i?"),
  17792. BUILDIN_DEF(countspiritball,"?"),
  17793. BUILDIN_DEF(mergeitem,"?"),
  17794. #include "../custom/script_def.inc"
  17795. {NULL,NULL,NULL},
  17796. };